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
25 * QT RLE Video Decoder by Mike Melanson (melanson@pcisys.net)
26 * For more information about the QT RLE format, visit:
27 * http://www.pcisys.net/~melanson/codecs/
29 * The QT RLE decoder has seven modes of operation:
30 * 1, 2, 4, 8, 16, 24, and 32 bits per pixel. For modes 1, 2, 4, and 8
31 * the decoder outputs PAL8 colorspace data. 16-bit data yields RGB555
32 * data. 24-bit data is RGB24 and 32-bit data is RGB32.
43 typedef struct QtrleContext {
45 AVCodecContext *avctx;
54 #define CHECK_STREAM_PTR(n) \
55 if ((stream_ptr + n) > s->size) { \
56 av_log (s->avctx, AV_LOG_INFO, "Problem: stream_ptr out of bounds (%d >= %d)\n", \
57 stream_ptr + n, s->size); \
61 #define CHECK_PIXEL_PTR(n) \
62 if ((pixel_ptr + n > pixel_limit) || (pixel_ptr + n < 0)) { \
63 av_log (s->avctx, AV_LOG_INFO, "Problem: pixel_ptr = %d, pixel_limit = %d\n", \
64 pixel_ptr + n, pixel_limit); \
68 static void qtrle_decode_1bpp(QtrleContext *s)
72 static void qtrle_decode_2bpp(QtrleContext *s)
76 static void qtrle_decode_4bpp(QtrleContext *s)
83 int row_ptr, pixel_ptr;
84 int row_inc = s->frame.linesize[0];
85 unsigned char pi1, pi2, pi3, pi4, pi5, pi6, pi7, pi8; /* 8 palette indices */
86 unsigned char *rgb = s->frame.data[0];
87 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
89 /* check if this frame is even supposed to change */
93 /* start after the chunk size */
96 /* fetch the header */
98 header = AV_RB16(&s->buf[stream_ptr]);
101 /* if a header is present, fetch additional decoding parameters */
102 if (header & 0x0008) {
104 start_line = AV_RB16(&s->buf[stream_ptr]);
106 lines_to_change = AV_RB16(&s->buf[stream_ptr]);
110 lines_to_change = s->avctx->height;
113 row_ptr = row_inc * start_line;
114 while (lines_to_change--) {
116 pixel_ptr = row_ptr + (8 * (s->buf[stream_ptr++] - 1));
118 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
120 /* there's another skip code in the stream */
122 pixel_ptr += (8 * (s->buf[stream_ptr++] - 1));
123 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
124 } else if (rle_code < 0) {
125 /* decode the run length code */
126 rle_code = -rle_code;
127 /* get the next 4 bytes from the stream, treat them as palette
128 * indices, and output them rle_code times */
130 pi1 = ((s->buf[stream_ptr]) >> 4) & 0x0f;
131 pi2 = (s->buf[stream_ptr++]) & 0x0f;
132 pi3 = ((s->buf[stream_ptr]) >> 4) & 0x0f;
133 pi4 = (s->buf[stream_ptr++]) & 0x0f;
134 pi5 = ((s->buf[stream_ptr]) >> 4) & 0x0f;
135 pi6 = (s->buf[stream_ptr++]) & 0x0f;
136 pi7 = ((s->buf[stream_ptr]) >> 4) & 0x0f;
137 pi8 = (s->buf[stream_ptr++]) & 0x0f;
139 CHECK_PIXEL_PTR(rle_code * 8);
142 rgb[pixel_ptr++] = pi1;
143 rgb[pixel_ptr++] = pi2;
144 rgb[pixel_ptr++] = pi3;
145 rgb[pixel_ptr++] = pi4;
146 rgb[pixel_ptr++] = pi5;
147 rgb[pixel_ptr++] = pi6;
148 rgb[pixel_ptr++] = pi7;
149 rgb[pixel_ptr++] = pi8;
152 /* copy the same pixel directly to output 4 times */
154 CHECK_STREAM_PTR(rle_code);
155 CHECK_PIXEL_PTR(rle_code*2);
158 rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 4) & 0x0f;
159 rgb[pixel_ptr++] = (s->buf[stream_ptr++]) & 0x0f;
167 static void qtrle_decode_8bpp(QtrleContext *s)
174 int row_ptr, pixel_ptr;
175 int row_inc = s->frame.linesize[0];
176 unsigned char pi1, pi2, pi3, pi4; /* 4 palette indices */
177 unsigned char *rgb = s->frame.data[0];
178 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
180 /* check if this frame is even supposed to change */
184 /* start after the chunk size */
187 /* fetch the header */
189 header = AV_RB16(&s->buf[stream_ptr]);
192 /* if a header is present, fetch additional decoding parameters */
193 if (header & 0x0008) {
195 start_line = AV_RB16(&s->buf[stream_ptr]);
197 lines_to_change = AV_RB16(&s->buf[stream_ptr]);
201 lines_to_change = s->avctx->height;
204 row_ptr = row_inc * start_line;
205 while (lines_to_change--) {
207 pixel_ptr = row_ptr + (4 * (s->buf[stream_ptr++] - 1));
209 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
211 /* there's another skip code in the stream */
213 pixel_ptr += (4 * (s->buf[stream_ptr++] - 1));
214 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
215 } else if (rle_code < 0) {
216 /* decode the run length code */
217 rle_code = -rle_code;
218 /* get the next 4 bytes from the stream, treat them as palette
219 * indices, and output them rle_code times */
221 pi1 = s->buf[stream_ptr++];
222 pi2 = s->buf[stream_ptr++];
223 pi3 = s->buf[stream_ptr++];
224 pi4 = s->buf[stream_ptr++];
226 CHECK_PIXEL_PTR(rle_code * 4);
229 rgb[pixel_ptr++] = pi1;
230 rgb[pixel_ptr++] = pi2;
231 rgb[pixel_ptr++] = pi3;
232 rgb[pixel_ptr++] = pi4;
235 /* copy the same pixel directly to output 4 times */
237 CHECK_STREAM_PTR(rle_code);
238 CHECK_PIXEL_PTR(rle_code);
241 rgb[pixel_ptr++] = s->buf[stream_ptr++];
249 static void qtrle_decode_16bpp(QtrleContext *s)
256 int row_ptr, pixel_ptr;
257 int row_inc = s->frame.linesize[0];
258 unsigned short rgb16;
259 unsigned char *rgb = s->frame.data[0];
260 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
262 /* check if this frame is even supposed to change */
266 /* start after the chunk size */
269 /* fetch the header */
271 header = AV_RB16(&s->buf[stream_ptr]);
274 /* if a header is present, fetch additional decoding parameters */
275 if (header & 0x0008) {
277 start_line = AV_RB16(&s->buf[stream_ptr]);
279 lines_to_change = AV_RB16(&s->buf[stream_ptr]);
283 lines_to_change = s->avctx->height;
286 row_ptr = row_inc * start_line;
287 while (lines_to_change--) {
289 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 2;
291 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
293 /* there's another skip code in the stream */
295 pixel_ptr += (s->buf[stream_ptr++] - 1) * 2;
296 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
297 } else if (rle_code < 0) {
298 /* decode the run length code */
299 rle_code = -rle_code;
301 rgb16 = AV_RB16(&s->buf[stream_ptr]);
304 CHECK_PIXEL_PTR(rle_code * 2);
307 *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
311 CHECK_STREAM_PTR(rle_code * 2);
312 CHECK_PIXEL_PTR(rle_code * 2);
314 /* copy pixels directly to output */
316 rgb16 = AV_RB16(&s->buf[stream_ptr]);
318 *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
327 static void qtrle_decode_24bpp(QtrleContext *s)
334 int row_ptr, pixel_ptr;
335 int row_inc = s->frame.linesize[0];
336 unsigned char r, g, b;
337 unsigned char *rgb = s->frame.data[0];
338 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
340 /* check if this frame is even supposed to change */
344 /* start after the chunk size */
347 /* fetch the header */
349 header = AV_RB16(&s->buf[stream_ptr]);
352 /* if a header is present, fetch additional decoding parameters */
353 if (header & 0x0008) {
355 start_line = AV_RB16(&s->buf[stream_ptr]);
357 lines_to_change = AV_RB16(&s->buf[stream_ptr]);
361 lines_to_change = s->avctx->height;
364 row_ptr = row_inc * start_line;
365 while (lines_to_change--) {
367 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 3;
369 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
371 /* there's another skip code in the stream */
373 pixel_ptr += (s->buf[stream_ptr++] - 1) * 3;
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;
379 r = s->buf[stream_ptr++];
380 g = s->buf[stream_ptr++];
381 b = s->buf[stream_ptr++];
383 CHECK_PIXEL_PTR(rle_code * 3);
386 rgb[pixel_ptr++] = r;
387 rgb[pixel_ptr++] = g;
388 rgb[pixel_ptr++] = b;
391 CHECK_STREAM_PTR(rle_code * 3);
392 CHECK_PIXEL_PTR(rle_code * 3);
394 /* copy pixels directly to output */
396 rgb[pixel_ptr++] = s->buf[stream_ptr++];
397 rgb[pixel_ptr++] = s->buf[stream_ptr++];
398 rgb[pixel_ptr++] = s->buf[stream_ptr++];
406 static void qtrle_decode_32bpp(QtrleContext *s)
413 int row_ptr, pixel_ptr;
414 int row_inc = s->frame.linesize[0];
415 unsigned char a, r, g, b;
417 unsigned char *rgb = s->frame.data[0];
418 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
420 /* check if this frame is even supposed to change */
424 /* start after the chunk size */
427 /* fetch the header */
429 header = AV_RB16(&s->buf[stream_ptr]);
432 /* if a header is present, fetch additional decoding parameters */
433 if (header & 0x0008) {
435 start_line = AV_RB16(&s->buf[stream_ptr]);
437 lines_to_change = AV_RB16(&s->buf[stream_ptr]);
441 lines_to_change = s->avctx->height;
444 row_ptr = row_inc * start_line;
445 while (lines_to_change--) {
447 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 4;
449 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
451 /* there's another skip code in the stream */
453 pixel_ptr += (s->buf[stream_ptr++] - 1) * 4;
454 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
455 } else if (rle_code < 0) {
456 /* decode the run length code */
457 rle_code = -rle_code;
459 a = s->buf[stream_ptr++];
460 r = s->buf[stream_ptr++];
461 g = s->buf[stream_ptr++];
462 b = s->buf[stream_ptr++];
463 argb = (a << 24) | (r << 16) | (g << 8) | (b << 0);
465 CHECK_PIXEL_PTR(rle_code * 4);
468 *(unsigned int *)(&rgb[pixel_ptr]) = argb;
472 CHECK_STREAM_PTR(rle_code * 4);
473 CHECK_PIXEL_PTR(rle_code * 4);
475 /* copy pixels directly to output */
477 a = s->buf[stream_ptr++];
478 r = s->buf[stream_ptr++];
479 g = s->buf[stream_ptr++];
480 b = s->buf[stream_ptr++];
481 argb = (a << 24) | (r << 16) | (g << 8) | (b << 0);
482 *(unsigned int *)(&rgb[pixel_ptr]) = argb;
491 static int qtrle_decode_init(AVCodecContext *avctx)
493 QtrleContext *s = avctx->priv_data;
496 switch (avctx->bits_per_sample) {
505 avctx->pix_fmt = PIX_FMT_PAL8;
509 avctx->pix_fmt = PIX_FMT_RGB555;
513 avctx->pix_fmt = PIX_FMT_RGB24;
517 avctx->pix_fmt = PIX_FMT_RGB32;
521 av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
522 avctx->bits_per_sample);
525 dsputil_init(&s->dsp, avctx);
527 s->frame.data[0] = NULL;
532 static int qtrle_decode_frame(AVCodecContext *avctx,
533 void *data, int *data_size,
534 uint8_t *buf, int buf_size)
536 QtrleContext *s = avctx->priv_data;
541 s->frame.reference = 1;
542 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
543 FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
544 if (avctx->reget_buffer(avctx, &s->frame)) {
545 av_log (s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
549 switch (avctx->bits_per_sample) {
552 qtrle_decode_1bpp(s);
557 qtrle_decode_2bpp(s);
562 qtrle_decode_4bpp(s);
563 /* make the palette available on the way out */
564 memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
565 if (s->avctx->palctrl->palette_changed) {
566 s->frame.palette_has_changed = 1;
567 s->avctx->palctrl->palette_changed = 0;
573 qtrle_decode_8bpp(s);
574 /* make the palette available on the way out */
575 memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
576 if (s->avctx->palctrl->palette_changed) {
577 s->frame.palette_has_changed = 1;
578 s->avctx->palctrl->palette_changed = 0;
583 qtrle_decode_16bpp(s);
587 qtrle_decode_24bpp(s);
591 qtrle_decode_32bpp(s);
595 av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
596 avctx->bits_per_sample);
600 *data_size = sizeof(AVFrame);
601 *(AVFrame*)data = s->frame;
603 /* always report that the buffer was completely consumed */
607 static int qtrle_decode_end(AVCodecContext *avctx)
609 QtrleContext *s = avctx->priv_data;
611 if (s->frame.data[0])
612 avctx->release_buffer(avctx, &s->frame);
617 AVCodec qtrle_decoder = {
621 sizeof(QtrleContext),