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 RGBA32.
44 typedef struct QtrleContext {
46 AVCodecContext *avctx;
55 #define CHECK_STREAM_PTR(n) \
56 if ((stream_ptr + n) > s->size) { \
57 av_log (s->avctx, AV_LOG_INFO, "Problem: stream_ptr out of bounds (%d >= %d)\n", \
58 stream_ptr + n, s->size); \
62 #define CHECK_PIXEL_PTR(n) \
63 if ((pixel_ptr + n > pixel_limit) || (pixel_ptr + n < 0)) { \
64 av_log (s->avctx, AV_LOG_INFO, "Problem: pixel_ptr = %d, pixel_limit = %d\n", \
65 pixel_ptr + n, pixel_limit); \
69 static void qtrle_decode_1bpp(QtrleContext *s)
73 static void qtrle_decode_2bpp(QtrleContext *s)
77 static void qtrle_decode_4bpp(QtrleContext *s)
84 int row_ptr, pixel_ptr;
85 int row_inc = s->frame.linesize[0];
86 unsigned char pi1, pi2, pi3, pi4, pi5, pi6, pi7, pi8; /* 8 palette indices */
87 unsigned char *rgb = s->frame.data[0];
88 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
90 /* check if this frame is even supposed to change */
94 /* start after the chunk size */
97 /* fetch the header */
99 header = BE_16(&s->buf[stream_ptr]);
102 /* if a header is present, fetch additional decoding parameters */
103 if (header & 0x0008) {
105 start_line = BE_16(&s->buf[stream_ptr]);
107 lines_to_change = BE_16(&s->buf[stream_ptr]);
111 lines_to_change = s->avctx->height;
114 row_ptr = row_inc * start_line;
115 while (lines_to_change--) {
117 pixel_ptr = row_ptr + (8 * (s->buf[stream_ptr++] - 1));
119 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
121 /* there's another skip code in the stream */
123 pixel_ptr += (8 * (s->buf[stream_ptr++] - 1));
124 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
125 } else if (rle_code < 0) {
126 /* decode the run length code */
127 rle_code = -rle_code;
128 /* get the next 4 bytes from the stream, treat them as palette
129 * indices, and output them rle_code times */
131 pi1 = ((s->buf[stream_ptr]) >> 4) & 0x0f;
132 pi2 = (s->buf[stream_ptr++]) & 0x0f;
133 pi3 = ((s->buf[stream_ptr]) >> 4) & 0x0f;
134 pi4 = (s->buf[stream_ptr++]) & 0x0f;
135 pi5 = ((s->buf[stream_ptr]) >> 4) & 0x0f;
136 pi6 = (s->buf[stream_ptr++]) & 0x0f;
137 pi7 = ((s->buf[stream_ptr]) >> 4) & 0x0f;
138 pi8 = (s->buf[stream_ptr++]) & 0x0f;
140 CHECK_PIXEL_PTR(rle_code * 8);
143 rgb[pixel_ptr++] = pi1;
144 rgb[pixel_ptr++] = pi2;
145 rgb[pixel_ptr++] = pi3;
146 rgb[pixel_ptr++] = pi4;
147 rgb[pixel_ptr++] = pi5;
148 rgb[pixel_ptr++] = pi6;
149 rgb[pixel_ptr++] = pi7;
150 rgb[pixel_ptr++] = pi8;
153 /* copy the same pixel directly to output 4 times */
155 CHECK_STREAM_PTR(rle_code);
156 CHECK_PIXEL_PTR(rle_code*2);
159 rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 4) & 0x0f;
160 rgb[pixel_ptr++] = (s->buf[stream_ptr++]) & 0x0f;
168 static void qtrle_decode_8bpp(QtrleContext *s)
175 int row_ptr, pixel_ptr;
176 int row_inc = s->frame.linesize[0];
177 unsigned char pi1, pi2, pi3, pi4; /* 4 palette indices */
178 unsigned char *rgb = s->frame.data[0];
179 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
181 /* check if this frame is even supposed to change */
185 /* start after the chunk size */
188 /* fetch the header */
190 header = BE_16(&s->buf[stream_ptr]);
193 /* if a header is present, fetch additional decoding parameters */
194 if (header & 0x0008) {
196 start_line = BE_16(&s->buf[stream_ptr]);
198 lines_to_change = BE_16(&s->buf[stream_ptr]);
202 lines_to_change = s->avctx->height;
205 row_ptr = row_inc * start_line;
206 while (lines_to_change--) {
208 pixel_ptr = row_ptr + (4 * (s->buf[stream_ptr++] - 1));
210 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
212 /* there's another skip code in the stream */
214 pixel_ptr += (4 * (s->buf[stream_ptr++] - 1));
215 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
216 } else if (rle_code < 0) {
217 /* decode the run length code */
218 rle_code = -rle_code;
219 /* get the next 4 bytes from the stream, treat them as palette
220 * indices, and output them rle_code times */
222 pi1 = s->buf[stream_ptr++];
223 pi2 = s->buf[stream_ptr++];
224 pi3 = s->buf[stream_ptr++];
225 pi4 = s->buf[stream_ptr++];
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_STREAM_PTR(rle_code);
239 CHECK_PIXEL_PTR(rle_code);
242 rgb[pixel_ptr++] = s->buf[stream_ptr++];
250 static void qtrle_decode_16bpp(QtrleContext *s)
257 int row_ptr, pixel_ptr;
258 int row_inc = s->frame.linesize[0];
259 unsigned short rgb16;
260 unsigned char *rgb = s->frame.data[0];
261 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
263 /* check if this frame is even supposed to change */
267 /* start after the chunk size */
270 /* fetch the header */
272 header = BE_16(&s->buf[stream_ptr]);
275 /* if a header is present, fetch additional decoding parameters */
276 if (header & 0x0008) {
278 start_line = BE_16(&s->buf[stream_ptr]);
280 lines_to_change = BE_16(&s->buf[stream_ptr]);
284 lines_to_change = s->avctx->height;
287 row_ptr = row_inc * start_line;
288 while (lines_to_change--) {
290 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 2;
292 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
294 /* there's another skip code in the stream */
296 pixel_ptr += (s->buf[stream_ptr++] - 1) * 2;
297 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
298 } else if (rle_code < 0) {
299 /* decode the run length code */
300 rle_code = -rle_code;
302 rgb16 = BE_16(&s->buf[stream_ptr]);
305 CHECK_PIXEL_PTR(rle_code * 2);
308 *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
312 CHECK_STREAM_PTR(rle_code * 2);
313 CHECK_PIXEL_PTR(rle_code * 2);
315 /* copy pixels directly to output */
317 rgb16 = BE_16(&s->buf[stream_ptr]);
319 *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
328 static void qtrle_decode_24bpp(QtrleContext *s)
335 int row_ptr, pixel_ptr;
336 int row_inc = s->frame.linesize[0];
337 unsigned char r, g, b;
338 unsigned char *rgb = s->frame.data[0];
339 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
341 /* check if this frame is even supposed to change */
345 /* start after the chunk size */
348 /* fetch the header */
350 header = BE_16(&s->buf[stream_ptr]);
353 /* if a header is present, fetch additional decoding parameters */
354 if (header & 0x0008) {
356 start_line = BE_16(&s->buf[stream_ptr]);
358 lines_to_change = BE_16(&s->buf[stream_ptr]);
362 lines_to_change = s->avctx->height;
365 row_ptr = row_inc * start_line;
366 while (lines_to_change--) {
368 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 3;
370 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
372 /* there's another skip code in the stream */
374 pixel_ptr += (s->buf[stream_ptr++] - 1) * 3;
375 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
376 } else if (rle_code < 0) {
377 /* decode the run length code */
378 rle_code = -rle_code;
380 r = s->buf[stream_ptr++];
381 g = s->buf[stream_ptr++];
382 b = s->buf[stream_ptr++];
384 CHECK_PIXEL_PTR(rle_code * 3);
387 rgb[pixel_ptr++] = r;
388 rgb[pixel_ptr++] = g;
389 rgb[pixel_ptr++] = b;
392 CHECK_STREAM_PTR(rle_code * 3);
393 CHECK_PIXEL_PTR(rle_code * 3);
395 /* copy pixels directly to output */
397 rgb[pixel_ptr++] = s->buf[stream_ptr++];
398 rgb[pixel_ptr++] = s->buf[stream_ptr++];
399 rgb[pixel_ptr++] = s->buf[stream_ptr++];
407 static void qtrle_decode_32bpp(QtrleContext *s)
414 int row_ptr, pixel_ptr;
415 int row_inc = s->frame.linesize[0];
416 unsigned char a, r, g, b;
418 unsigned char *rgb = s->frame.data[0];
419 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
421 /* check if this frame is even supposed to change */
425 /* start after the chunk size */
428 /* fetch the header */
430 header = BE_16(&s->buf[stream_ptr]);
433 /* if a header is present, fetch additional decoding parameters */
434 if (header & 0x0008) {
436 start_line = BE_16(&s->buf[stream_ptr]);
438 lines_to_change = BE_16(&s->buf[stream_ptr]);
442 lines_to_change = s->avctx->height;
445 row_ptr = row_inc * start_line;
446 while (lines_to_change--) {
448 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 4;
450 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
452 /* there's another skip code in the stream */
454 pixel_ptr += (s->buf[stream_ptr++] - 1) * 4;
455 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
456 } else if (rle_code < 0) {
457 /* decode the run length code */
458 rle_code = -rle_code;
460 a = s->buf[stream_ptr++];
461 r = s->buf[stream_ptr++];
462 g = s->buf[stream_ptr++];
463 b = s->buf[stream_ptr++];
464 argb = (a << 24) | (r << 16) | (g << 8) | (b << 0);
466 CHECK_PIXEL_PTR(rle_code * 4);
469 *(unsigned int *)(&rgb[pixel_ptr]) = argb;
473 CHECK_STREAM_PTR(rle_code * 4);
474 CHECK_PIXEL_PTR(rle_code * 4);
476 /* copy pixels directly to output */
478 a = s->buf[stream_ptr++];
479 r = s->buf[stream_ptr++];
480 g = s->buf[stream_ptr++];
481 b = s->buf[stream_ptr++];
482 argb = (a << 24) | (r << 16) | (g << 8) | (b << 0);
483 *(unsigned int *)(&rgb[pixel_ptr]) = argb;
492 static int qtrle_decode_init(AVCodecContext *avctx)
494 QtrleContext *s = (QtrleContext *)avctx->priv_data;
497 switch (avctx->bits_per_sample) {
506 avctx->pix_fmt = PIX_FMT_PAL8;
510 avctx->pix_fmt = PIX_FMT_RGB555;
514 avctx->pix_fmt = PIX_FMT_RGB24;
518 avctx->pix_fmt = PIX_FMT_RGBA32;
522 av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
523 avctx->bits_per_sample);
526 avctx->has_b_frames = 0;
527 dsputil_init(&s->dsp, avctx);
529 s->frame.data[0] = NULL;
534 static int qtrle_decode_frame(AVCodecContext *avctx,
535 void *data, int *data_size,
536 uint8_t *buf, int buf_size)
538 QtrleContext *s = (QtrleContext *)avctx->priv_data;
543 s->frame.reference = 1;
544 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
545 FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
546 if (avctx->reget_buffer(avctx, &s->frame)) {
547 av_log (s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
551 switch (avctx->bits_per_sample) {
554 qtrle_decode_1bpp(s);
559 qtrle_decode_2bpp(s);
564 qtrle_decode_4bpp(s);
565 /* make the palette available on the way out */
566 memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
567 if (s->avctx->palctrl->palette_changed) {
568 s->frame.palette_has_changed = 1;
569 s->avctx->palctrl->palette_changed = 0;
575 qtrle_decode_8bpp(s);
576 /* make the palette available on the way out */
577 memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
578 if (s->avctx->palctrl->palette_changed) {
579 s->frame.palette_has_changed = 1;
580 s->avctx->palctrl->palette_changed = 0;
585 qtrle_decode_16bpp(s);
589 qtrle_decode_24bpp(s);
593 qtrle_decode_32bpp(s);
597 av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
598 avctx->bits_per_sample);
602 *data_size = sizeof(AVFrame);
603 *(AVFrame*)data = s->frame;
605 /* always report that the buffer was completely consumed */
609 static int qtrle_decode_end(AVCodecContext *avctx)
611 QtrleContext *s = (QtrleContext *)avctx->priv_data;
613 if (s->frame.data[0])
614 avctx->release_buffer(avctx, &s->frame);
619 AVCodec qtrle_decoder = {
623 sizeof(QtrleContext),