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.
39 #include "bytestream.h"
42 typedef struct QtrleContext {
43 AVCodecContext *avctx;
50 #define CHECK_PIXEL_PTR(n) \
51 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 pixel_ptr + n, pixel_limit); \
57 static void qtrle_decode_1bpp(QtrleContext *s, int row_ptr, int lines_to_change)
61 int row_inc = s->frame->linesize[0];
62 uint8_t pi0, pi1; /* 2 8-pixel values */
63 uint8_t *rgb = s->frame->data[0];
64 int pixel_limit = s->frame->linesize[0] * s->avctx->height;
66 /* skip & 0x80 appears to mean 'start a new line', which can be interpreted
67 * as 'go to next line' during the decoding of a frame but is 'go to first
68 * line' at the beginning. Since we always interpret it as 'go to next line'
69 * in the decoding loop (which makes code simpler/faster), the first line
70 * 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
78 while (lines_to_change) {
79 skip = bytestream2_get_byte(&s->g);
80 rle_code = (int8_t)bytestream2_get_byte(&s->g);
86 pixel_ptr = row_ptr + 2 * 8 * (skip & 0x7f);
88 pixel_ptr += 2 * 8 * skip;
89 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
95 /* decode the run length code */
97 /* get the next 2 bytes from the stream, treat them as groups
98 * of 8 pixels, and output them rle_code times */
100 pi0 = bytestream2_get_byte(&s->g);
101 pi1 = bytestream2_get_byte(&s->g);
102 CHECK_PIXEL_PTR(rle_code * 2 * 8);
105 rgb[pixel_ptr++] = (pi0 >> 7) & 0x01;
106 rgb[pixel_ptr++] = (pi0 >> 6) & 0x01;
107 rgb[pixel_ptr++] = (pi0 >> 5) & 0x01;
108 rgb[pixel_ptr++] = (pi0 >> 4) & 0x01;
109 rgb[pixel_ptr++] = (pi0 >> 3) & 0x01;
110 rgb[pixel_ptr++] = (pi0 >> 2) & 0x01;
111 rgb[pixel_ptr++] = (pi0 >> 1) & 0x01;
112 rgb[pixel_ptr++] = pi0 & 0x01;
113 rgb[pixel_ptr++] = (pi1 >> 7) & 0x01;
114 rgb[pixel_ptr++] = (pi1 >> 6) & 0x01;
115 rgb[pixel_ptr++] = (pi1 >> 5) & 0x01;
116 rgb[pixel_ptr++] = (pi1 >> 4) & 0x01;
117 rgb[pixel_ptr++] = (pi1 >> 3) & 0x01;
118 rgb[pixel_ptr++] = (pi1 >> 2) & 0x01;
119 rgb[pixel_ptr++] = (pi1 >> 1) & 0x01;
120 rgb[pixel_ptr++] = pi1 & 0x01;
123 /* copy the same pixel directly to output 2 times */
125 CHECK_PIXEL_PTR(rle_code * 8);
128 int x = bytestream2_get_byte(&s->g);
129 rgb[pixel_ptr++] = (x >> 7) & 0x01;
130 rgb[pixel_ptr++] = (x >> 6) & 0x01;
131 rgb[pixel_ptr++] = (x >> 5) & 0x01;
132 rgb[pixel_ptr++] = (x >> 4) & 0x01;
133 rgb[pixel_ptr++] = (x >> 3) & 0x01;
134 rgb[pixel_ptr++] = (x >> 2) & 0x01;
135 rgb[pixel_ptr++] = (x >> 1) & 0x01;
136 rgb[pixel_ptr++] = x & 0x01;
142 static inline void qtrle_decode_2n4bpp(QtrleContext *s, int row_ptr,
143 int lines_to_change, int bpp)
147 int row_inc = s->frame->linesize[0];
148 uint8_t pi[16]; /* 16 palette indices */
149 uint8_t *rgb = s->frame->data[0];
150 int pixel_limit = s->frame->linesize[0] * s->avctx->height;
151 int num_pixels = (bpp == 4) ? 8 : 16;
153 while (lines_to_change--) {
154 pixel_ptr = row_ptr + (num_pixels * (bytestream2_get_byte(&s->g) - 1));
157 while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
159 /* 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 } else if (rle_code < 0) {
163 /* decode the run length code */
164 rle_code = -rle_code;
165 /* 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 pi[num_pixels-1-i] = (bytestream2_peek_byte(&s->g) >> ((i*bpp) & 0x07)) & ((1<<bpp)-1);
169 bytestream2_skip(&s->g, ((i & ((num_pixels>>2)-1)) == 0));
171 CHECK_PIXEL_PTR(rle_code * num_pixels);
173 memcpy(&rgb[pixel_ptr], &pi, num_pixels);
174 pixel_ptr += num_pixels;
177 /* copy the same pixel directly to output 4 times */
179 CHECK_PIXEL_PTR(rle_code*(num_pixels>>2));
182 int x = bytestream2_get_byte(&s->g);
183 rgb[pixel_ptr++] = (x >> 4) & 0x0f;
184 rgb[pixel_ptr++] = x & 0x0f;
186 int x = bytestream2_get_byte(&s->g);
187 rgb[pixel_ptr++] = (x >> 6) & 0x03;
188 rgb[pixel_ptr++] = (x >> 4) & 0x03;
189 rgb[pixel_ptr++] = (x >> 2) & 0x03;
190 rgb[pixel_ptr++] = x & 0x03;
199 static void qtrle_decode_8bpp(QtrleContext *s, int row_ptr, int lines_to_change)
203 int row_inc = s->frame->linesize[0];
204 uint8_t pi1, pi2, pi3, pi4; /* 4 palette indexes */
205 uint8_t *rgb = s->frame->data[0];
206 int pixel_limit = s->frame->linesize[0] * s->avctx->height;
208 while (lines_to_change--) {
209 pixel_ptr = row_ptr + (4 * (bytestream2_get_byte(&s->g) - 1));
212 while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
214 /* 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 } else if (rle_code < 0) {
218 /* decode the run length code */
219 rle_code = -rle_code;
220 /* get the next 4 bytes from the stream, treat them as palette
221 * indexes, and output them rle_code times */
222 pi1 = bytestream2_get_byte(&s->g);
223 pi2 = bytestream2_get_byte(&s->g);
224 pi3 = bytestream2_get_byte(&s->g);
225 pi4 = bytestream2_get_byte(&s->g);
227 CHECK_PIXEL_PTR(rle_code * 4);
230 rgb[pixel_ptr++] = pi1;
231 rgb[pixel_ptr++] = pi2;
232 rgb[pixel_ptr++] = pi3;
233 rgb[pixel_ptr++] = pi4;
236 /* copy the same pixel directly to output 4 times */
238 CHECK_PIXEL_PTR(rle_code);
240 bytestream2_get_buffer(&s->g, &rgb[pixel_ptr], rle_code);
241 pixel_ptr += rle_code;
248 static void qtrle_decode_16bpp(QtrleContext *s, int row_ptr, int lines_to_change)
252 int row_inc = s->frame->linesize[0];
254 uint8_t *rgb = s->frame->data[0];
255 int pixel_limit = s->frame->linesize[0] * s->avctx->height;
257 while (lines_to_change--) {
258 pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 2;
261 while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
263 /* 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 } else if (rle_code < 0) {
267 /* decode the run length code */
268 rle_code = -rle_code;
269 rgb16 = bytestream2_get_be16(&s->g);
271 CHECK_PIXEL_PTR(rle_code * 2);
274 *(uint16_t *)(&rgb[pixel_ptr]) = rgb16;
278 CHECK_PIXEL_PTR(rle_code * 2);
280 /* copy pixels directly to output */
282 rgb16 = bytestream2_get_be16(&s->g);
283 *(uint16_t *)(&rgb[pixel_ptr]) = rgb16;
292 static void qtrle_decode_24bpp(QtrleContext *s, int row_ptr, int lines_to_change)
296 int row_inc = s->frame->linesize[0];
298 uint8_t *rgb = s->frame->data[0];
299 int pixel_limit = s->frame->linesize[0] * s->avctx->height;
301 while (lines_to_change--) {
302 pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 3;
305 while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
307 /* 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 } else if (rle_code < 0) {
311 /* decode the run length code */
312 rle_code = -rle_code;
313 r = bytestream2_get_byte(&s->g);
314 g = bytestream2_get_byte(&s->g);
315 b = bytestream2_get_byte(&s->g);
317 CHECK_PIXEL_PTR(rle_code * 3);
320 rgb[pixel_ptr++] = r;
321 rgb[pixel_ptr++] = g;
322 rgb[pixel_ptr++] = b;
325 CHECK_PIXEL_PTR(rle_code * 3);
327 /* copy pixels directly to output */
329 rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
330 rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
331 rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
339 static void qtrle_decode_32bpp(QtrleContext *s, int row_ptr, int lines_to_change)
343 int row_inc = s->frame->linesize[0];
345 uint8_t *rgb = s->frame->data[0];
346 int pixel_limit = s->frame->linesize[0] * s->avctx->height;
348 while (lines_to_change--) {
349 pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 4;
352 while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
354 /* 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 } else if (rle_code < 0) {
358 /* decode the run length code */
359 rle_code = -rle_code;
360 argb = bytestream2_get_be32(&s->g);
362 CHECK_PIXEL_PTR(rle_code * 4);
365 AV_WN32A(rgb + pixel_ptr, argb);
369 CHECK_PIXEL_PTR(rle_code * 4);
371 /* copy pixels directly to output */
373 argb = bytestream2_get_be32(&s->g);
374 AV_WN32A(rgb + pixel_ptr, argb);
383 static av_cold int qtrle_decode_init(AVCodecContext *avctx)
385 QtrleContext *s = avctx->priv_data;
388 switch (avctx->bits_per_coded_sample) {
397 avctx->pix_fmt = AV_PIX_FMT_PAL8;
401 avctx->pix_fmt = AV_PIX_FMT_RGB555;
405 avctx->pix_fmt = AV_PIX_FMT_RGB24;
409 avctx->pix_fmt = AV_PIX_FMT_RGB32;
413 av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
414 avctx->bits_per_coded_sample);
415 return AVERROR_INVALIDDATA;
418 s->frame = av_frame_alloc();
420 return AVERROR(ENOMEM);
425 static int qtrle_decode_frame(AVCodecContext *avctx,
426 void *data, int *got_frame,
429 QtrleContext *s = avctx->priv_data;
430 int header, start_line;
435 bytestream2_init(&s->g, avpkt->data, avpkt->size);
436 if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
439 /* check if this frame is even supposed to change */
443 /* start after the chunk size */
444 bytestream2_seek(&s->g, 4, SEEK_SET);
446 /* fetch the header */
447 header = bytestream2_get_be16(&s->g);
449 /* if a header is present, fetch additional decoding parameters */
450 if (header & 0x0008) {
451 if (avpkt->size < 14)
453 start_line = bytestream2_get_be16(&s->g);
454 bytestream2_skip(&s->g, 2);
455 height = bytestream2_get_be16(&s->g);
456 bytestream2_skip(&s->g, 2);
457 if (height > s->avctx->height - start_line)
461 height = s->avctx->height;
463 row_ptr = s->frame->linesize[0] * start_line;
465 switch (avctx->bits_per_coded_sample) {
468 qtrle_decode_1bpp(s, row_ptr, height);
474 qtrle_decode_2n4bpp(s, row_ptr, height, 2);
480 qtrle_decode_2n4bpp(s, row_ptr, height, 4);
486 qtrle_decode_8bpp(s, row_ptr, height);
491 qtrle_decode_16bpp(s, row_ptr, height);
495 qtrle_decode_24bpp(s, row_ptr, height);
499 qtrle_decode_32bpp(s, row_ptr, height);
503 av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
504 avctx->bits_per_coded_sample);
510 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &size);
512 if (pal && size == AVPALETTE_SIZE) {
513 s->frame->palette_has_changed = 1;
514 memcpy(s->pal, pal, AVPALETTE_SIZE);
516 av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", size);
519 /* make the palette available on the way out */
520 memcpy(s->frame->data[1], s->pal, AVPALETTE_SIZE);
524 if ((ret = av_frame_ref(data, s->frame)) < 0)
528 /* always report that the buffer was completely consumed */
532 static av_cold int qtrle_decode_end(AVCodecContext *avctx)
534 QtrleContext *s = avctx->priv_data;
536 av_frame_free(&s->frame);
541 AVCodec ff_qtrle_decoder = {
543 .long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),
544 .type = AVMEDIA_TYPE_VIDEO,
545 .id = AV_CODEC_ID_QTRLE,
546 .priv_data_size = sizeof(QtrleContext),
547 .init = qtrle_decode_init,
548 .close = qtrle_decode_end,
549 .decode = qtrle_decode_frame,
550 .capabilities = AV_CODEC_CAP_DR1,