qtrle.c 18.1 KB
Newer Older
1 2
/*
 * Quicktime Animation (RLE) Video Decoder
3
 * Copyright (c) 2004 The FFmpeg Project
4
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 21 22
 */

/**
23
 * @file
24 25 26 27 28 29
 * QT RLE Video Decoder by Mike Melanson (melanson@pcisys.net)
 * For more information about the QT RLE format, visit:
 *   http://www.pcisys.net/~melanson/codecs/
 *
 * The QT RLE decoder has seven modes of operation:
 * 1, 2, 4, 8, 16, 24, and 32 bits per pixel. For modes 1, 2, 4, and 8
30
 * the decoder outputs PAL8 colorspace data. 16-bit data yields RGB555
31
 * data. 24-bit data is RGB24 and 32-bit data is RGB32.
32 33 34 35 36 37 38
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "avcodec.h"
39
#include "bytestream.h"
40
#include "internal.h"
41 42 43

typedef struct QtrleContext {
    AVCodecContext *avctx;
44
    AVFrame *frame;
45

46
    GetByteContext g;
47
    uint32_t pal[256];
48 49
} QtrleContext;

50 51
#define CHECK_PIXEL_PTR(n)                                                            \
    if ((pixel_ptr + n > pixel_limit) || (pixel_ptr + n < 0)) {                       \
52
        av_log (s->avctx, AV_LOG_ERROR, "Problem: pixel_ptr = %d, pixel_limit = %d\n",\
53 54 55
                pixel_ptr + n, pixel_limit);                                          \
        return;                                                                       \
    }                                                                                 \
56

57
static void qtrle_decode_1bpp(QtrleContext *s, int row_ptr, int lines_to_change)
58
{
59
    int rle_code;
60
    int pixel_ptr;
61
    int row_inc = s->frame->linesize[0];
62
    uint8_t pi0, pi1;  /* 2 8-pixel values */
63 64
    uint8_t *rgb = s->frame->data[0];
    int pixel_limit = s->frame->linesize[0] * s->avctx->height;
65
    int skip;
66 67 68 69 70
    /* skip & 0x80 appears to mean 'start a new line', which can be interpreted
     * as 'go to next line' during the decoding of a frame but is 'go to first
     * line' at the beginning. Since we always interpret it as 'go to next line'
     * in the decoding loop (which makes code simpler/faster), the first line
     * would not be counted, so we count one more.
71
     * See: https://trac.ffmpeg.org/ticket/226
72
     * In the following decoding loop, row_ptr will be the position of the
73
     * current row. */
74

75 76 77
    row_ptr  -= row_inc;
    pixel_ptr = row_ptr;
    lines_to_change++;
78
    while (lines_to_change) {
79
        skip     =              bytestream2_get_byte(&s->g);
80
        rle_code = (int8_t)bytestream2_get_byte(&s->g);
81 82 83 84
        if (rle_code == 0)
            break;
        if(skip & 0x80) {
            lines_to_change--;
85
            row_ptr += row_inc;
86
            pixel_ptr = row_ptr + 2 * 8 * (skip & 0x7f);
87
        } else
88
            pixel_ptr += 2 * 8 * skip;
89 90
        CHECK_PIXEL_PTR(0);  /* make sure pixel_ptr is positive */

91 92 93
        if(rle_code == -1)
            continue;

94 95 96 97 98
        if (rle_code < 0) {
            /* decode the run length code */
            rle_code = -rle_code;
            /* get the next 2 bytes from the stream, treat them as groups
             * of 8 pixels, and output them rle_code times */
99 100 101

            pi0 = bytestream2_get_byte(&s->g);
            pi1 = bytestream2_get_byte(&s->g);
102
            CHECK_PIXEL_PTR(rle_code * 2 * 8);
103 104

            while (rle_code--) {
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
                rgb[pixel_ptr++] = (pi0 >> 7) & 0x01;
                rgb[pixel_ptr++] = (pi0 >> 6) & 0x01;
                rgb[pixel_ptr++] = (pi0 >> 5) & 0x01;
                rgb[pixel_ptr++] = (pi0 >> 4) & 0x01;
                rgb[pixel_ptr++] = (pi0 >> 3) & 0x01;
                rgb[pixel_ptr++] = (pi0 >> 2) & 0x01;
                rgb[pixel_ptr++] = (pi0 >> 1) & 0x01;
                rgb[pixel_ptr++] =  pi0       & 0x01;
                rgb[pixel_ptr++] = (pi1 >> 7) & 0x01;
                rgb[pixel_ptr++] = (pi1 >> 6) & 0x01;
                rgb[pixel_ptr++] = (pi1 >> 5) & 0x01;
                rgb[pixel_ptr++] = (pi1 >> 4) & 0x01;
                rgb[pixel_ptr++] = (pi1 >> 3) & 0x01;
                rgb[pixel_ptr++] = (pi1 >> 2) & 0x01;
                rgb[pixel_ptr++] = (pi1 >> 1) & 0x01;
                rgb[pixel_ptr++] =  pi1       & 0x01;
121 122 123 124
            }
        } else {
            /* copy the same pixel directly to output 2 times */
            rle_code *= 2;
125
            CHECK_PIXEL_PTR(rle_code * 8);
126

127 128 129 130 131 132 133 134 135 136 137
            while (rle_code--) {
                int x = bytestream2_get_byte(&s->g);
                rgb[pixel_ptr++] = (x >> 7) & 0x01;
                rgb[pixel_ptr++] = (x >> 6) & 0x01;
                rgb[pixel_ptr++] = (x >> 5) & 0x01;
                rgb[pixel_ptr++] = (x >> 4) & 0x01;
                rgb[pixel_ptr++] = (x >> 3) & 0x01;
                rgb[pixel_ptr++] = (x >> 2) & 0x01;
                rgb[pixel_ptr++] = (x >> 1) & 0x01;
                rgb[pixel_ptr++] =  x       & 0x01;
            }
138 139
        }
    }
140 141
}

142 143
static inline void qtrle_decode_2n4bpp(QtrleContext *s, int row_ptr,
                                       int lines_to_change, int bpp)
144
{
145
    int rle_code, i;
146
    int pixel_ptr;
147
    int row_inc = s->frame->linesize[0];
148
    uint8_t pi[16];  /* 16 palette indices */
149 150
    uint8_t *rgb = s->frame->data[0];
    int pixel_limit = s->frame->linesize[0] * s->avctx->height;
151
    int num_pixels = (bpp == 4) ? 8 : 16;
152 153

    while (lines_to_change--) {
154
        pixel_ptr = row_ptr + (num_pixels * (bytestream2_get_byte(&s->g) - 1));
155
        CHECK_PIXEL_PTR(0);
156

157
        while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
158 159
            if (rle_code == 0) {
                /* there's another skip code in the stream */
160
                pixel_ptr += (num_pixels * (bytestream2_get_byte(&s->g) - 1));
161
                CHECK_PIXEL_PTR(0);  /* make sure pixel_ptr is positive */
162 163 164 165
            } else if (rle_code < 0) {
                /* decode the run length code */
                rle_code = -rle_code;
                /* get the next 4 bytes from the stream, treat them as palette
166
                 * indexes, and output them rle_code times */
167
                for (i = num_pixels-1; i >= 0; i--) {
168 169
                    pi[num_pixels-1-i] = (bytestream2_peek_byte(&s->g) >> ((i*bpp) & 0x07)) & ((1<<bpp)-1);
                    bytestream2_skip(&s->g, ((i & ((num_pixels>>2)-1)) == 0));
170 171
                }
                CHECK_PIXEL_PTR(rle_code * num_pixels);
172
                while (rle_code--) {
Paul B Mahol's avatar
Paul B Mahol committed
173 174
                    memcpy(&rgb[pixel_ptr], &pi, num_pixels);
                    pixel_ptr += num_pixels;
175 176 177 178
                }
            } else {
                /* copy the same pixel directly to output 4 times */
                rle_code *= 4;
179
                CHECK_PIXEL_PTR(rle_code*(num_pixels>>2));
180
                while (rle_code--) {
181
                    if(bpp == 4) {
182 183 184
                        int x = bytestream2_get_byte(&s->g);
                        rgb[pixel_ptr++] = (x >> 4) & 0x0f;
                        rgb[pixel_ptr++] =  x       & 0x0f;
185
                    } else {
186 187 188 189 190
                        int x = bytestream2_get_byte(&s->g);
                        rgb[pixel_ptr++] = (x >> 6) & 0x03;
                        rgb[pixel_ptr++] = (x >> 4) & 0x03;
                        rgb[pixel_ptr++] = (x >> 2) & 0x03;
                        rgb[pixel_ptr++] =  x       & 0x03;
191
                    }
192 193 194 195 196
                }
            }
        }
        row_ptr += row_inc;
    }
197 198
}

199
static void qtrle_decode_8bpp(QtrleContext *s, int row_ptr, int lines_to_change)
200
{
201
    int rle_code;
202
    int pixel_ptr;
203
    int row_inc = s->frame->linesize[0];
204
    uint8_t pi1, pi2, pi3, pi4;  /* 4 palette indexes */
205 206
    uint8_t *rgb = s->frame->data[0];
    int pixel_limit = s->frame->linesize[0] * s->avctx->height;
207 208

    while (lines_to_change--) {
209
        pixel_ptr = row_ptr + (4 * (bytestream2_get_byte(&s->g) - 1));
210
        CHECK_PIXEL_PTR(0);
211

212
        while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
213 214
            if (rle_code == 0) {
                /* there's another skip code in the stream */
215
                pixel_ptr += (4 * (bytestream2_get_byte(&s->g) - 1));
216
                CHECK_PIXEL_PTR(0);  /* make sure pixel_ptr is positive */
217 218 219 220
            } else if (rle_code < 0) {
                /* decode the run length code */
                rle_code = -rle_code;
                /* get the next 4 bytes from the stream, treat them as palette
221
                 * indexes, and output them rle_code times */
222 223 224 225
                pi1 = bytestream2_get_byte(&s->g);
                pi2 = bytestream2_get_byte(&s->g);
                pi3 = bytestream2_get_byte(&s->g);
                pi4 = bytestream2_get_byte(&s->g);
226 227 228 229 230 231 232 233 234 235 236 237 238 239

                CHECK_PIXEL_PTR(rle_code * 4);

                while (rle_code--) {
                    rgb[pixel_ptr++] = pi1;
                    rgb[pixel_ptr++] = pi2;
                    rgb[pixel_ptr++] = pi3;
                    rgb[pixel_ptr++] = pi4;
                }
            } else {
                /* copy the same pixel directly to output 4 times */
                rle_code *= 4;
                CHECK_PIXEL_PTR(rle_code);

240 241
                bytestream2_get_buffer(&s->g, &rgb[pixel_ptr], rle_code);
                pixel_ptr += rle_code;
242 243 244 245 246 247
            }
        }
        row_ptr += row_inc;
    }
}

248
static void qtrle_decode_16bpp(QtrleContext *s, int row_ptr, int lines_to_change)
249
{
250
    int rle_code;
251
    int pixel_ptr;
252
    int row_inc = s->frame->linesize[0];
253
    uint16_t rgb16;
254 255
    uint8_t *rgb = s->frame->data[0];
    int pixel_limit = s->frame->linesize[0] * s->avctx->height;
256 257

    while (lines_to_change--) {
258
        pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 2;
259
        CHECK_PIXEL_PTR(0);
260

261
        while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
262 263
            if (rle_code == 0) {
                /* there's another skip code in the stream */
264
                pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 2;
265
                CHECK_PIXEL_PTR(0);  /* make sure pixel_ptr is positive */
266 267 268
            } else if (rle_code < 0) {
                /* decode the run length code */
                rle_code = -rle_code;
269
                rgb16 = bytestream2_get_be16(&s->g);
270 271 272 273

                CHECK_PIXEL_PTR(rle_code * 2);

                while (rle_code--) {
274
                    *(uint16_t *)(&rgb[pixel_ptr]) = rgb16;
275 276 277 278 279 280 281
                    pixel_ptr += 2;
                }
            } else {
                CHECK_PIXEL_PTR(rle_code * 2);

                /* copy pixels directly to output */
                while (rle_code--) {
282
                    rgb16 = bytestream2_get_be16(&s->g);
283
                    *(uint16_t *)(&rgb[pixel_ptr]) = rgb16;
284 285 286 287 288 289 290 291
                    pixel_ptr += 2;
                }
            }
        }
        row_ptr += row_inc;
    }
}

292
static void qtrle_decode_24bpp(QtrleContext *s, int row_ptr, int lines_to_change)
293
{
294
    int rle_code;
295
    int pixel_ptr;
296
    int row_inc = s->frame->linesize[0];
297
    uint8_t r, g, b;
298 299
    uint8_t *rgb = s->frame->data[0];
    int pixel_limit = s->frame->linesize[0] * s->avctx->height;
300 301

    while (lines_to_change--) {
302
        pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 3;
303
        CHECK_PIXEL_PTR(0);
304

305
        while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
306 307
            if (rle_code == 0) {
                /* there's another skip code in the stream */
308
                pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 3;
309
                CHECK_PIXEL_PTR(0);  /* make sure pixel_ptr is positive */
310 311 312
            } else if (rle_code < 0) {
                /* decode the run length code */
                rle_code = -rle_code;
313 314 315
                r = bytestream2_get_byte(&s->g);
                g = bytestream2_get_byte(&s->g);
                b = bytestream2_get_byte(&s->g);
316 317 318 319 320 321 322 323 324 325 326 327 328

                CHECK_PIXEL_PTR(rle_code * 3);

                while (rle_code--) {
                    rgb[pixel_ptr++] = r;
                    rgb[pixel_ptr++] = g;
                    rgb[pixel_ptr++] = b;
                }
            } else {
                CHECK_PIXEL_PTR(rle_code * 3);

                /* copy pixels directly to output */
                while (rle_code--) {
329 330 331
                    rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
                    rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
                    rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
332 333 334 335 336 337 338
                }
            }
        }
        row_ptr += row_inc;
    }
}

339
static void qtrle_decode_32bpp(QtrleContext *s, int row_ptr, int lines_to_change)
340
{
341
    int rle_code;
342
    int pixel_ptr;
343
    int row_inc = s->frame->linesize[0];
344
    unsigned int argb;
345 346
    uint8_t *rgb = s->frame->data[0];
    int pixel_limit = s->frame->linesize[0] * s->avctx->height;
347 348

    while (lines_to_change--) {
349
        pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 4;
350
        CHECK_PIXEL_PTR(0);
351

352
        while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
353 354
            if (rle_code == 0) {
                /* there's another skip code in the stream */
355
                pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 4;
356
                CHECK_PIXEL_PTR(0);  /* make sure pixel_ptr is positive */
357 358 359
            } else if (rle_code < 0) {
                /* decode the run length code */
                rle_code = -rle_code;
360
                argb = bytestream2_get_be32(&s->g);
361 362 363 364

                CHECK_PIXEL_PTR(rle_code * 4);

                while (rle_code--) {
365
                    AV_WN32A(rgb + pixel_ptr, argb);
366 367 368 369 370 371 372
                    pixel_ptr += 4;
                }
            } else {
                CHECK_PIXEL_PTR(rle_code * 4);

                /* copy pixels directly to output */
                while (rle_code--) {
373
                    argb = bytestream2_get_be32(&s->g);
374 375
                    AV_WN32A(rgb + pixel_ptr, argb);
                    pixel_ptr  += 4;
376 377 378 379 380 381 382
                }
            }
        }
        row_ptr += row_inc;
    }
}

383
static av_cold int qtrle_decode_init(AVCodecContext *avctx)
384
{
385
    QtrleContext *s = avctx->priv_data;
386 387

    s->avctx = avctx;
388
    switch (avctx->bits_per_coded_sample) {
389 390 391 392
    case 1:
    case 2:
    case 4:
    case 8:
393
    case 33:
394 395 396
    case 34:
    case 36:
    case 40:
397
        avctx->pix_fmt = AV_PIX_FMT_PAL8;
398 399 400
        break;

    case 16:
401
        avctx->pix_fmt = AV_PIX_FMT_RGB555;
402 403 404
        break;

    case 24:
405
        avctx->pix_fmt = AV_PIX_FMT_RGB24;
406 407 408
        break;

    case 32:
409
        avctx->pix_fmt = AV_PIX_FMT_RGB32;
410 411 412
        break;

    default:
Roberto Togni's avatar
Roberto Togni committed
413
        av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
414
            avctx->bits_per_coded_sample);
415
        return AVERROR_INVALIDDATA;
416 417
    }

418 419 420
    s->frame = av_frame_alloc();
    if (!s->frame)
        return AVERROR(ENOMEM);
421 422 423 424 425

    return 0;
}

static int qtrle_decode_frame(AVCodecContext *avctx,
426
                              void *data, int *got_frame,
427
                              AVPacket *avpkt)
428
{
429
    QtrleContext *s = avctx->priv_data;
430
    int header, start_line;
431
    int height, row_ptr;
432
    int has_palette = 0;
433
    int ret;
434

435
    bytestream2_init(&s->g, avpkt->data, avpkt->size);
436
    if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
437
        return ret;
438

439
    /* check if this frame is even supposed to change */
440
    if (avpkt->size < 8)
441 442 443
        goto done;

    /* start after the chunk size */
444
    bytestream2_seek(&s->g, 4, SEEK_SET);
445 446

    /* fetch the header */
447
    header = bytestream2_get_be16(&s->g);
448 449 450

    /* if a header is present, fetch additional decoding parameters */
    if (header & 0x0008) {
451
        if (avpkt->size < 14)
452
            goto done;
453 454 455 456
        start_line = bytestream2_get_be16(&s->g);
        bytestream2_skip(&s->g, 2);
        height     = bytestream2_get_be16(&s->g);
        bytestream2_skip(&s->g, 2);
457 458
        if (height > s->avctx->height - start_line)
            goto done;
459 460
    } else {
        start_line = 0;
461
        height     = s->avctx->height;
462
    }
463
    row_ptr = s->frame->linesize[0] * start_line;
464

465
    switch (avctx->bits_per_coded_sample) {
466 467
    case 1:
    case 33:
468
        qtrle_decode_1bpp(s, row_ptr, height);
469
        has_palette = 1;
470 471 472 473
        break;

    case 2:
    case 34:
474
        qtrle_decode_2n4bpp(s, row_ptr, height, 2);
475
        has_palette = 1;
476 477 478 479
        break;

    case 4:
    case 36:
480
        qtrle_decode_2n4bpp(s, row_ptr, height, 4);
481
        has_palette = 1;
482 483 484 485
        break;

    case 8:
    case 40:
486
        qtrle_decode_8bpp(s, row_ptr, height);
487
        has_palette = 1;
488 489 490
        break;

    case 16:
491
        qtrle_decode_16bpp(s, row_ptr, height);
492 493 494
        break;

    case 24:
495
        qtrle_decode_24bpp(s, row_ptr, height);
496 497 498
        break;

    case 32:
499
        qtrle_decode_32bpp(s, row_ptr, height);
500 501 502
        break;

    default:
Roberto Togni's avatar
Roberto Togni committed
503
        av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
504
            avctx->bits_per_coded_sample);
505 506
        break;
    }
507 508

    if(has_palette) {
509 510 511
        const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);

        if (pal) {
512
            s->frame->palette_has_changed = 1;
513
            memcpy(s->pal, pal, AVPALETTE_SIZE);
514
        }
515 516

        /* make the palette available on the way out */
517
        memcpy(s->frame->data[1], s->pal, AVPALETTE_SIZE);
518 519
    }

520
done:
521
    if ((ret = av_frame_ref(data, s->frame)) < 0)
522
        return ret;
523
    *got_frame      = 1;
524 525

    /* always report that the buffer was completely consumed */
526
    return avpkt->size;
527 528
}

529
static av_cold int qtrle_decode_end(AVCodecContext *avctx)
530
{
531
    QtrleContext *s = avctx->priv_data;
532

533
    av_frame_free(&s->frame);
534 535 536 537

    return 0;
}

538
AVCodec ff_qtrle_decoder = {
539
    .name           = "qtrle",
540
    .long_name      = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),
541
    .type           = AVMEDIA_TYPE_VIDEO,
542
    .id             = AV_CODEC_ID_QTRLE,
543 544 545 546
    .priv_data_size = sizeof(QtrleContext),
    .init           = qtrle_decode_init,
    .close          = qtrle_decode_end,
    .decode         = qtrle_decode_frame,
547
    .capabilities   = AV_CODEC_CAP_DR1,
548
};