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.
42 typedef struct QtrleContext {
44 AVCodecContext *avctx;
53 #define CHECK_STREAM_PTR(n) \
54 if ((stream_ptr + n) > s->size) { \
55 av_log (s->avctx, AV_LOG_INFO, "Problem: stream_ptr out of bounds (%d >= %d)\n", \
56 stream_ptr + n, s->size); \
60 #define CHECK_PIXEL_PTR(n) \
61 if ((pixel_ptr + n > pixel_limit) || (pixel_ptr + n < 0)) { \
62 av_log (s->avctx, AV_LOG_INFO, "Problem: pixel_ptr = %d, pixel_limit = %d\n", \
63 pixel_ptr + n, pixel_limit); \
67 static void qtrle_decode_1bpp(QtrleContext *s)
71 static void qtrle_decode_2bpp(QtrleContext *s)
75 static void qtrle_decode_4bpp(QtrleContext *s)
82 int row_ptr, pixel_ptr;
83 int row_inc = s->frame.linesize[0];
84 unsigned char pi1, pi2, pi3, pi4, pi5, pi6, pi7, pi8; /* 8 palette indices */
85 unsigned char *rgb = s->frame.data[0];
86 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
88 /* check if this frame is even supposed to change */
92 /* start after the chunk size */
95 /* fetch the header */
97 header = AV_RB16(&s->buf[stream_ptr]);
100 /* if a header is present, fetch additional decoding parameters */
101 if (header & 0x0008) {
103 start_line = AV_RB16(&s->buf[stream_ptr]);
105 lines_to_change = AV_RB16(&s->buf[stream_ptr]);
109 lines_to_change = s->avctx->height;
112 row_ptr = row_inc * start_line;
113 while (lines_to_change--) {
115 pixel_ptr = row_ptr + (8 * (s->buf[stream_ptr++] - 1));
117 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
119 /* there's another skip code in the stream */
121 pixel_ptr += (8 * (s->buf[stream_ptr++] - 1));
122 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
123 } else if (rle_code < 0) {
124 /* decode the run length code */
125 rle_code = -rle_code;
126 /* get the next 4 bytes from the stream, treat them as palette
127 * indices, and output them rle_code times */
129 pi1 = ((s->buf[stream_ptr]) >> 4) & 0x0f;
130 pi2 = (s->buf[stream_ptr++]) & 0x0f;
131 pi3 = ((s->buf[stream_ptr]) >> 4) & 0x0f;
132 pi4 = (s->buf[stream_ptr++]) & 0x0f;
133 pi5 = ((s->buf[stream_ptr]) >> 4) & 0x0f;
134 pi6 = (s->buf[stream_ptr++]) & 0x0f;
135 pi7 = ((s->buf[stream_ptr]) >> 4) & 0x0f;
136 pi8 = (s->buf[stream_ptr++]) & 0x0f;
138 CHECK_PIXEL_PTR(rle_code * 8);
141 rgb[pixel_ptr++] = pi1;
142 rgb[pixel_ptr++] = pi2;
143 rgb[pixel_ptr++] = pi3;
144 rgb[pixel_ptr++] = pi4;
145 rgb[pixel_ptr++] = pi5;
146 rgb[pixel_ptr++] = pi6;
147 rgb[pixel_ptr++] = pi7;
148 rgb[pixel_ptr++] = pi8;
151 /* copy the same pixel directly to output 4 times */
153 CHECK_STREAM_PTR(rle_code);
154 CHECK_PIXEL_PTR(rle_code*2);
157 rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 4) & 0x0f;
158 rgb[pixel_ptr++] = (s->buf[stream_ptr++]) & 0x0f;
166 static void qtrle_decode_8bpp(QtrleContext *s)
173 int row_ptr, pixel_ptr;
174 int row_inc = s->frame.linesize[0];
175 unsigned char pi1, pi2, pi3, pi4; /* 4 palette indices */
176 unsigned char *rgb = s->frame.data[0];
177 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
179 /* check if this frame is even supposed to change */
183 /* start after the chunk size */
186 /* fetch the header */
188 header = AV_RB16(&s->buf[stream_ptr]);
191 /* if a header is present, fetch additional decoding parameters */
192 if (header & 0x0008) {
194 start_line = AV_RB16(&s->buf[stream_ptr]);
196 lines_to_change = AV_RB16(&s->buf[stream_ptr]);
200 lines_to_change = s->avctx->height;
203 row_ptr = row_inc * start_line;
204 while (lines_to_change--) {
206 pixel_ptr = row_ptr + (4 * (s->buf[stream_ptr++] - 1));
208 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
210 /* there's another skip code in the stream */
212 pixel_ptr += (4 * (s->buf[stream_ptr++] - 1));
213 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
214 } else if (rle_code < 0) {
215 /* decode the run length code */
216 rle_code = -rle_code;
217 /* get the next 4 bytes from the stream, treat them as palette
218 * indices, and output them rle_code times */
220 pi1 = s->buf[stream_ptr++];
221 pi2 = s->buf[stream_ptr++];
222 pi3 = s->buf[stream_ptr++];
223 pi4 = s->buf[stream_ptr++];
225 CHECK_PIXEL_PTR(rle_code * 4);
228 rgb[pixel_ptr++] = pi1;
229 rgb[pixel_ptr++] = pi2;
230 rgb[pixel_ptr++] = pi3;
231 rgb[pixel_ptr++] = pi4;
234 /* copy the same pixel directly to output 4 times */
236 CHECK_STREAM_PTR(rle_code);
237 CHECK_PIXEL_PTR(rle_code);
240 rgb[pixel_ptr++] = s->buf[stream_ptr++];
248 static void qtrle_decode_16bpp(QtrleContext *s)
255 int row_ptr, pixel_ptr;
256 int row_inc = s->frame.linesize[0];
257 unsigned short rgb16;
258 unsigned char *rgb = s->frame.data[0];
259 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
261 /* check if this frame is even supposed to change */
265 /* start after the chunk size */
268 /* fetch the header */
270 header = AV_RB16(&s->buf[stream_ptr]);
273 /* if a header is present, fetch additional decoding parameters */
274 if (header & 0x0008) {
276 start_line = AV_RB16(&s->buf[stream_ptr]);
278 lines_to_change = AV_RB16(&s->buf[stream_ptr]);
282 lines_to_change = s->avctx->height;
285 row_ptr = row_inc * start_line;
286 while (lines_to_change--) {
288 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 2;
290 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
292 /* there's another skip code in the stream */
294 pixel_ptr += (s->buf[stream_ptr++] - 1) * 2;
295 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
296 } else if (rle_code < 0) {
297 /* decode the run length code */
298 rle_code = -rle_code;
300 rgb16 = AV_RB16(&s->buf[stream_ptr]);
303 CHECK_PIXEL_PTR(rle_code * 2);
306 *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
310 CHECK_STREAM_PTR(rle_code * 2);
311 CHECK_PIXEL_PTR(rle_code * 2);
313 /* copy pixels directly to output */
315 rgb16 = AV_RB16(&s->buf[stream_ptr]);
317 *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
326 static void qtrle_decode_24bpp(QtrleContext *s)
333 int row_ptr, pixel_ptr;
334 int row_inc = s->frame.linesize[0];
335 unsigned char r, g, b;
336 unsigned char *rgb = s->frame.data[0];
337 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
339 /* check if this frame is even supposed to change */
343 /* start after the chunk size */
346 /* fetch the header */
348 header = AV_RB16(&s->buf[stream_ptr]);
351 /* if a header is present, fetch additional decoding parameters */
352 if (header & 0x0008) {
354 start_line = AV_RB16(&s->buf[stream_ptr]);
356 lines_to_change = AV_RB16(&s->buf[stream_ptr]);
360 lines_to_change = s->avctx->height;
363 row_ptr = row_inc * start_line;
364 while (lines_to_change--) {
366 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 3;
368 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
370 /* there's another skip code in the stream */
372 pixel_ptr += (s->buf[stream_ptr++] - 1) * 3;
373 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
374 } else if (rle_code < 0) {
375 /* decode the run length code */
376 rle_code = -rle_code;
378 r = s->buf[stream_ptr++];
379 g = s->buf[stream_ptr++];
380 b = s->buf[stream_ptr++];
382 CHECK_PIXEL_PTR(rle_code * 3);
385 rgb[pixel_ptr++] = r;
386 rgb[pixel_ptr++] = g;
387 rgb[pixel_ptr++] = b;
390 CHECK_STREAM_PTR(rle_code * 3);
391 CHECK_PIXEL_PTR(rle_code * 3);
393 /* copy pixels directly to output */
395 rgb[pixel_ptr++] = s->buf[stream_ptr++];
396 rgb[pixel_ptr++] = s->buf[stream_ptr++];
397 rgb[pixel_ptr++] = s->buf[stream_ptr++];
405 static void qtrle_decode_32bpp(QtrleContext *s)
412 int row_ptr, pixel_ptr;
413 int row_inc = s->frame.linesize[0];
414 unsigned char a, r, g, b;
416 unsigned char *rgb = s->frame.data[0];
417 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
419 /* check if this frame is even supposed to change */
423 /* start after the chunk size */
426 /* fetch the header */
428 header = AV_RB16(&s->buf[stream_ptr]);
431 /* if a header is present, fetch additional decoding parameters */
432 if (header & 0x0008) {
434 start_line = AV_RB16(&s->buf[stream_ptr]);
436 lines_to_change = AV_RB16(&s->buf[stream_ptr]);
440 lines_to_change = s->avctx->height;
443 row_ptr = row_inc * start_line;
444 while (lines_to_change--) {
446 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 4;
448 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
450 /* there's another skip code in the stream */
452 pixel_ptr += (s->buf[stream_ptr++] - 1) * 4;
453 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
454 } else if (rle_code < 0) {
455 /* decode the run length code */
456 rle_code = -rle_code;
458 a = s->buf[stream_ptr++];
459 r = s->buf[stream_ptr++];
460 g = s->buf[stream_ptr++];
461 b = s->buf[stream_ptr++];
462 argb = (a << 24) | (r << 16) | (g << 8) | (b << 0);
464 CHECK_PIXEL_PTR(rle_code * 4);
467 *(unsigned int *)(&rgb[pixel_ptr]) = argb;
471 CHECK_STREAM_PTR(rle_code * 4);
472 CHECK_PIXEL_PTR(rle_code * 4);
474 /* copy pixels directly to output */
476 a = s->buf[stream_ptr++];
477 r = s->buf[stream_ptr++];
478 g = s->buf[stream_ptr++];
479 b = s->buf[stream_ptr++];
480 argb = (a << 24) | (r << 16) | (g << 8) | (b << 0);
481 *(unsigned int *)(&rgb[pixel_ptr]) = argb;
490 static int qtrle_decode_init(AVCodecContext *avctx)
492 QtrleContext *s = avctx->priv_data;
495 switch (avctx->bits_per_sample) {
504 avctx->pix_fmt = PIX_FMT_PAL8;
508 avctx->pix_fmt = PIX_FMT_RGB555;
512 avctx->pix_fmt = PIX_FMT_RGB24;
516 avctx->pix_fmt = PIX_FMT_RGB32;
520 av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
521 avctx->bits_per_sample);
524 dsputil_init(&s->dsp, avctx);
526 s->frame.data[0] = NULL;
531 static int qtrle_decode_frame(AVCodecContext *avctx,
532 void *data, int *data_size,
533 uint8_t *buf, int buf_size)
535 QtrleContext *s = avctx->priv_data;
540 s->frame.reference = 1;
541 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
542 FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
543 if (avctx->reget_buffer(avctx, &s->frame)) {
544 av_log (s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
548 switch (avctx->bits_per_sample) {
551 qtrle_decode_1bpp(s);
556 qtrle_decode_2bpp(s);
561 qtrle_decode_4bpp(s);
562 /* make the palette available on the way out */
563 memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
564 if (s->avctx->palctrl->palette_changed) {
565 s->frame.palette_has_changed = 1;
566 s->avctx->palctrl->palette_changed = 0;
572 qtrle_decode_8bpp(s);
573 /* make the palette available on the way out */
574 memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
575 if (s->avctx->palctrl->palette_changed) {
576 s->frame.palette_has_changed = 1;
577 s->avctx->palctrl->palette_changed = 0;
582 qtrle_decode_16bpp(s);
586 qtrle_decode_24bpp(s);
590 qtrle_decode_32bpp(s);
594 av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
595 avctx->bits_per_sample);
599 *data_size = sizeof(AVFrame);
600 *(AVFrame*)data = s->frame;
602 /* always report that the buffer was completely consumed */
606 static int qtrle_decode_end(AVCodecContext *avctx)
608 QtrleContext *s = avctx->priv_data;
610 if (s->frame.data[0])
611 avctx->release_buffer(avctx, &s->frame);
616 AVCodec qtrle_decoder = {
620 sizeof(QtrleContext),