2 * Quicktime Animation (RLE) Video Decoder
3 * Copyright (C) 2004 The FFmpeg project
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * 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
24 * QT RLE Video Decoder by Mike Melanson (melanson@pcisys.net)
25 * For more information about the QT RLE format, visit:
26 * http://www.pcisys.net/~melanson/codecs/
28 * The QT RLE decoder has seven modes of operation:
29 * 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.
40 #include "bytestream.h"
43 typedef struct QtrleContext {
44 AVCodecContext *avctx;
51 #define CHECK_PIXEL_PTR(n) \
52 if ((pixel_ptr + n > pixel_limit) || (pixel_ptr + n < 0)) { \
53 av_log (s->avctx, AV_LOG_ERROR, "Problem: pixel_ptr = %d, pixel_limit = %d\n",\
54 pixel_ptr + n, pixel_limit); \
58 static void qtrle_decode_1bpp(QtrleContext *s, int row_ptr, int lines_to_change)
62 int row_inc = s->frame->linesize[0];
63 uint8_t pi0, pi1; /* 2 8-pixel values */
64 uint8_t *rgb = s->frame->data[0];
65 int pixel_limit = s->frame->linesize[0] * s->avctx->height;
67 /* skip & 0x80 appears to mean 'start a new line', which can be interpreted
68 * as 'go to next line' during the decoding of a frame but is 'go to first
69 * line' at the beginning. Since we always interpret it as 'go to next line'
70 * in the decoding loop (which makes code simpler/faster), the first line
71 * would not be counted, so we count one more.
72 * See: https://trac.ffmpeg.org/ticket/226
73 * In the following decoding loop, row_ptr will be the position of the
79 while (lines_to_change) {
80 skip = bytestream2_get_byte(&s->g);
81 rle_code = (int8_t)bytestream2_get_byte(&s->g);
87 pixel_ptr = row_ptr + 2 * 8 * (skip & 0x7f);
89 pixel_ptr += 2 * 8 * skip;
90 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
96 /* decode the run length code */
98 /* get the next 2 bytes from the stream, treat them as groups
99 * of 8 pixels, and output them rle_code times */
101 pi0 = bytestream2_get_byte(&s->g);
102 pi1 = bytestream2_get_byte(&s->g);
103 CHECK_PIXEL_PTR(rle_code * 2 * 8);
106 rgb[pixel_ptr++] = (pi0 >> 7) & 0x01;
107 rgb[pixel_ptr++] = (pi0 >> 6) & 0x01;
108 rgb[pixel_ptr++] = (pi0 >> 5) & 0x01;
109 rgb[pixel_ptr++] = (pi0 >> 4) & 0x01;
110 rgb[pixel_ptr++] = (pi0 >> 3) & 0x01;
111 rgb[pixel_ptr++] = (pi0 >> 2) & 0x01;
112 rgb[pixel_ptr++] = (pi0 >> 1) & 0x01;
113 rgb[pixel_ptr++] = pi0 & 0x01;
114 rgb[pixel_ptr++] = (pi1 >> 7) & 0x01;
115 rgb[pixel_ptr++] = (pi1 >> 6) & 0x01;
116 rgb[pixel_ptr++] = (pi1 >> 5) & 0x01;
117 rgb[pixel_ptr++] = (pi1 >> 4) & 0x01;
118 rgb[pixel_ptr++] = (pi1 >> 3) & 0x01;
119 rgb[pixel_ptr++] = (pi1 >> 2) & 0x01;
120 rgb[pixel_ptr++] = (pi1 >> 1) & 0x01;
121 rgb[pixel_ptr++] = pi1 & 0x01;
124 /* copy the same pixel directly to output 2 times */
126 CHECK_PIXEL_PTR(rle_code * 8);
129 int x = bytestream2_get_byte(&s->g);
130 rgb[pixel_ptr++] = (x >> 7) & 0x01;
131 rgb[pixel_ptr++] = (x >> 6) & 0x01;
132 rgb[pixel_ptr++] = (x >> 5) & 0x01;
133 rgb[pixel_ptr++] = (x >> 4) & 0x01;
134 rgb[pixel_ptr++] = (x >> 3) & 0x01;
135 rgb[pixel_ptr++] = (x >> 2) & 0x01;
136 rgb[pixel_ptr++] = (x >> 1) & 0x01;
137 rgb[pixel_ptr++] = x & 0x01;
143 static inline void qtrle_decode_2n4bpp(QtrleContext *s, int row_ptr,
144 int lines_to_change, int bpp)
148 int row_inc = s->frame->linesize[0];
149 uint8_t pi[16]; /* 16 palette indices */
150 uint8_t *rgb = s->frame->data[0];
151 int pixel_limit = s->frame->linesize[0] * s->avctx->height;
152 int num_pixels = (bpp == 4) ? 8 : 16;
154 while (lines_to_change--) {
155 pixel_ptr = row_ptr + (num_pixels * (bytestream2_get_byte(&s->g) - 1));
158 while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
159 if (bytestream2_get_bytes_left(&s->g) < 1)
162 /* there's another skip code in the stream */
163 pixel_ptr += (num_pixels * (bytestream2_get_byte(&s->g) - 1));
164 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
165 } else if (rle_code < 0) {
166 /* decode the run length code */
167 rle_code = -rle_code;
168 /* get the next 4 bytes from the stream, treat them as palette
169 * indexes, and output them rle_code times */
170 for (i = num_pixels-1; i >= 0; i--) {
171 pi[num_pixels-1-i] = (bytestream2_peek_byte(&s->g) >> ((i*bpp) & 0x07)) & ((1<<bpp)-1);
172 bytestream2_skip(&s->g, ((i & ((num_pixels>>2)-1)) == 0));
174 CHECK_PIXEL_PTR(rle_code * num_pixels);
176 memcpy(&rgb[pixel_ptr], &pi, num_pixels);
177 pixel_ptr += num_pixels;
180 /* copy the same pixel directly to output 4 times */
182 CHECK_PIXEL_PTR(rle_code*(num_pixels>>2));
185 int x = bytestream2_get_byte(&s->g);
186 rgb[pixel_ptr++] = (x >> 4) & 0x0f;
187 rgb[pixel_ptr++] = x & 0x0f;
189 int x = bytestream2_get_byte(&s->g);
190 rgb[pixel_ptr++] = (x >> 6) & 0x03;
191 rgb[pixel_ptr++] = (x >> 4) & 0x03;
192 rgb[pixel_ptr++] = (x >> 2) & 0x03;
193 rgb[pixel_ptr++] = x & 0x03;
202 static void qtrle_decode_8bpp(QtrleContext *s, int row_ptr, int lines_to_change)
206 int row_inc = s->frame->linesize[0];
207 uint8_t pi1, pi2, pi3, pi4; /* 4 palette indexes */
208 uint8_t *rgb = s->frame->data[0];
209 int pixel_limit = s->frame->linesize[0] * s->avctx->height;
211 while (lines_to_change--) {
212 pixel_ptr = row_ptr + (4 * (bytestream2_get_byte(&s->g) - 1));
215 while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
216 if (bytestream2_get_bytes_left(&s->g) < 1)
219 /* there's another skip code in the stream */
220 pixel_ptr += (4 * (bytestream2_get_byte(&s->g) - 1));
221 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
222 } else if (rle_code < 0) {
223 /* decode the run length code */
224 rle_code = -rle_code;
225 /* get the next 4 bytes from the stream, treat them as palette
226 * indexes, and output them rle_code times */
227 pi1 = bytestream2_get_byte(&s->g);
228 pi2 = bytestream2_get_byte(&s->g);
229 pi3 = bytestream2_get_byte(&s->g);
230 pi4 = bytestream2_get_byte(&s->g);
232 CHECK_PIXEL_PTR(rle_code * 4);
235 rgb[pixel_ptr++] = pi1;
236 rgb[pixel_ptr++] = pi2;
237 rgb[pixel_ptr++] = pi3;
238 rgb[pixel_ptr++] = pi4;
241 /* copy the same pixel directly to output 4 times */
243 CHECK_PIXEL_PTR(rle_code);
245 bytestream2_get_buffer(&s->g, &rgb[pixel_ptr], rle_code);
246 pixel_ptr += rle_code;
253 static void qtrle_decode_16bpp(QtrleContext *s, int row_ptr, int lines_to_change)
257 int row_inc = s->frame->linesize[0];
259 uint8_t *rgb = s->frame->data[0];
260 int pixel_limit = s->frame->linesize[0] * s->avctx->height;
262 while (lines_to_change--) {
263 pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 2;
266 while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
267 if (bytestream2_get_bytes_left(&s->g) < 1)
270 /* there's another skip code in the stream */
271 pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 2;
272 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
273 } else if (rle_code < 0) {
274 /* decode the run length code */
275 rle_code = -rle_code;
276 rgb16 = bytestream2_get_be16(&s->g);
278 CHECK_PIXEL_PTR(rle_code * 2);
281 *(uint16_t *)(&rgb[pixel_ptr]) = rgb16;
285 CHECK_PIXEL_PTR(rle_code * 2);
287 /* copy pixels directly to output */
289 rgb16 = bytestream2_get_be16(&s->g);
290 *(uint16_t *)(&rgb[pixel_ptr]) = rgb16;
299 static void qtrle_decode_24bpp(QtrleContext *s, int row_ptr, int lines_to_change)
301 int rle_code, rle_code_half;
303 int row_inc = s->frame->linesize[0];
306 uint8_t *rgb = s->frame->data[0];
307 int pixel_limit = s->frame->linesize[0] * s->avctx->height;
309 while (lines_to_change--) {
310 pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 3;
313 while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
314 if (bytestream2_get_bytes_left(&s->g) < 1)
317 /* there's another skip code in the stream */
318 pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 3;
319 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
320 } else if (rle_code < 0) {
321 /* decode the run length code */
322 rle_code = -rle_code;
323 rg = bytestream2_get_ne16(&s->g);
324 b = bytestream2_get_byte(&s->g);
326 CHECK_PIXEL_PTR(rle_code * 3);
329 AV_WN16(rgb + pixel_ptr, rg);
330 rgb[pixel_ptr + 2] = b;
334 CHECK_PIXEL_PTR(rle_code * 3);
336 rle_code_half = rle_code / 2;
338 while (rle_code_half--) { /* copy 2 raw rgb value at the same time */
339 AV_WN32(rgb + pixel_ptr, bytestream2_get_ne32(&s->g)); /* rgbr */
340 AV_WN16(rgb + pixel_ptr + 4, bytestream2_get_ne16(&s->g)); /* rgbr */
344 if (rle_code % 2 != 0){ /* not even raw value */
345 AV_WN16(rgb + pixel_ptr, bytestream2_get_ne16(&s->g));
346 rgb[pixel_ptr + 2] = bytestream2_get_byte(&s->g);
355 static void qtrle_decode_32bpp(QtrleContext *s, int row_ptr, int lines_to_change)
357 int rle_code, rle_code_half;
359 int row_inc = s->frame->linesize[0];
361 uint8_t *rgb = s->frame->data[0];
362 int pixel_limit = s->frame->linesize[0] * s->avctx->height;
364 while (lines_to_change--) {
365 pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 4;
368 while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
369 if (bytestream2_get_bytes_left(&s->g) < 1)
372 /* there's another skip code in the stream */
373 pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 4;
374 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
375 } else if (rle_code < 0) {
376 /* decode the run length code */
377 rle_code = -rle_code;
378 argb = bytestream2_get_ne32(&s->g);
380 CHECK_PIXEL_PTR(rle_code * 4);
383 AV_WN32A(rgb + pixel_ptr, argb);
387 CHECK_PIXEL_PTR(rle_code * 4);
389 /* copy pixels directly to output */
390 rle_code_half = rle_code / 2;
391 while (rle_code_half--) { /* copy 2 argb raw value at the same time */
392 AV_WN64(rgb + pixel_ptr, bytestream2_get_ne64(&s->g));
396 if (rle_code % 2 != 0){ /* not even raw value */
397 AV_WN32A(rgb + pixel_ptr, bytestream2_get_ne32(&s->g));
406 static av_cold int qtrle_decode_init(AVCodecContext *avctx)
408 QtrleContext *s = avctx->priv_data;
411 switch (avctx->bits_per_coded_sample) {
420 avctx->pix_fmt = AV_PIX_FMT_PAL8;
424 avctx->pix_fmt = AV_PIX_FMT_RGB555;
428 avctx->pix_fmt = AV_PIX_FMT_RGB24;
432 avctx->pix_fmt = AV_PIX_FMT_ARGB;
436 av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
437 avctx->bits_per_coded_sample);
438 return AVERROR_INVALIDDATA;
441 s->frame = av_frame_alloc();
443 return AVERROR(ENOMEM);
448 static int qtrle_decode_frame(AVCodecContext *avctx,
449 void *data, int *got_frame,
452 QtrleContext *s = avctx->priv_data;
453 int header, start_line;
459 bytestream2_init(&s->g, avpkt->data, avpkt->size);
461 /* check if this frame is even supposed to change */
462 if (avpkt->size < 8) {
467 /* start after the chunk size */
468 size = bytestream2_get_be32(&s->g) & 0x3FFFFFFF;
469 if (size - avpkt->size > size * (int64_t)avctx->discard_damaged_percentage / 100)
470 return AVERROR_INVALIDDATA;
473 /* fetch the header */
474 header = bytestream2_get_be16(&s->g);
476 /* if a header is present, fetch additional decoding parameters */
477 if (header & 0x0008) {
478 if (avpkt->size < 14) {
482 start_line = bytestream2_get_be16(&s->g);
483 bytestream2_skip(&s->g, 2);
484 height = bytestream2_get_be16(&s->g);
485 bytestream2_skip(&s->g, 2);
486 if (height > s->avctx->height - start_line) {
492 height = s->avctx->height;
494 if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
497 row_ptr = s->frame->linesize[0] * start_line;
499 switch (avctx->bits_per_coded_sample) {
502 qtrle_decode_1bpp(s, row_ptr, height);
508 qtrle_decode_2n4bpp(s, row_ptr, height, 2);
514 qtrle_decode_2n4bpp(s, row_ptr, height, 4);
520 qtrle_decode_8bpp(s, row_ptr, height);
525 qtrle_decode_16bpp(s, row_ptr, height);
529 qtrle_decode_24bpp(s, row_ptr, height);
533 qtrle_decode_32bpp(s, row_ptr, height);
537 av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
538 avctx->bits_per_coded_sample);
544 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &size);
546 if (pal && size == AVPALETTE_SIZE) {
547 s->frame->palette_has_changed = 1;
548 memcpy(s->pal, pal, AVPALETTE_SIZE);
550 av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", size);
553 /* make the palette available on the way out */
554 memcpy(s->frame->data[1], s->pal, AVPALETTE_SIZE);
558 if (!s->frame->data[0])
559 return AVERROR_INVALIDDATA;
561 // ff_reget_buffer() isn't needed when frames don't change, so just update
563 ret = ff_decode_frame_props(avctx, s->frame);
568 if ((ret = av_frame_ref(data, s->frame)) < 0)
572 /* always report that the buffer was completely consumed */
576 static void qtrle_decode_flush(AVCodecContext *avctx)
578 QtrleContext *s = avctx->priv_data;
580 av_frame_unref(s->frame);
583 static av_cold int qtrle_decode_end(AVCodecContext *avctx)
585 QtrleContext *s = avctx->priv_data;
587 av_frame_free(&s->frame);
592 AVCodec ff_qtrle_decoder = {
594 .long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),
595 .type = AVMEDIA_TYPE_VIDEO,
596 .id = AV_CODEC_ID_QTRLE,
597 .priv_data_size = sizeof(QtrleContext),
598 .init = qtrle_decode_init,
599 .close = qtrle_decode_end,
600 .decode = qtrle_decode_frame,
601 .flush = qtrle_decode_flush,
602 .capabilities = AV_CODEC_CAP_DR1,