2 * Quicktime Animation (RLE) Video Decoder
3 * Copyright (C) 2004 the ffmpeg project
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * QT RLE Video Decoder by Mike Melanson (melanson@pcisys.net)
24 * For more information about the QT RLE format, visit:
25 * http://www.pcisys.net/~melanson/codecs/
27 * The QT RLE decoder has seven modes of operation:
28 * 1, 2, 4, 8, 16, 24, and 32 bits per pixel. For modes 1, 2, 4, and 8
29 * the decoder outputs PAL8 colorspace data. 16-bit data yields RGB555
30 * data. 24-bit data is RGB24 and 32-bit data is RGBA32.
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) { \
62 av_log (s->avctx, AV_LOG_INFO, "Problem: pixel_ptr >= pixel_limit (%d >= %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 = BE_16(&s->buf[stream_ptr]);
100 /* if a header is present, fetch additional decoding parameters */
101 if (header & 0x0008) {
103 start_line = BE_16(&s->buf[stream_ptr]);
105 lines_to_change = BE_16(&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 } else if (rle_code < 0) {
123 /* decode the run length code */
124 rle_code = -rle_code;
125 /* get the next 4 bytes from the stream, treat them as palette
126 * indices, and output them rle_code times */
128 pi1 = ((s->buf[stream_ptr]) >> 4) & 0x0f;
129 pi2 = (s->buf[stream_ptr++]) & 0x0f;
130 pi3 = ((s->buf[stream_ptr]) >> 4) & 0x0f;
131 pi4 = (s->buf[stream_ptr++]) & 0x0f;
132 pi5 = ((s->buf[stream_ptr]) >> 4) & 0x0f;
133 pi6 = (s->buf[stream_ptr++]) & 0x0f;
134 pi7 = ((s->buf[stream_ptr]) >> 4) & 0x0f;
135 pi8 = (s->buf[stream_ptr++]) & 0x0f;
137 CHECK_PIXEL_PTR(rle_code * 8);
140 rgb[pixel_ptr++] = pi1;
141 rgb[pixel_ptr++] = pi2;
142 rgb[pixel_ptr++] = pi3;
143 rgb[pixel_ptr++] = pi4;
144 rgb[pixel_ptr++] = pi5;
145 rgb[pixel_ptr++] = pi6;
146 rgb[pixel_ptr++] = pi7;
147 rgb[pixel_ptr++] = pi8;
150 /* copy the same pixel directly to output 4 times */
152 CHECK_STREAM_PTR(rle_code);
153 CHECK_PIXEL_PTR(rle_code*2);
156 rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 4) & 0x0f;
157 rgb[pixel_ptr++] = (s->buf[stream_ptr++]) & 0x0f;
165 static void qtrle_decode_8bpp(QtrleContext *s)
172 int row_ptr, pixel_ptr;
173 int row_inc = s->frame.linesize[0];
174 unsigned char pi1, pi2, pi3, pi4; /* 4 palette indices */
175 unsigned char *rgb = s->frame.data[0];
176 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
178 /* check if this frame is even supposed to change */
182 /* start after the chunk size */
185 /* fetch the header */
187 header = BE_16(&s->buf[stream_ptr]);
190 /* if a header is present, fetch additional decoding parameters */
191 if (header & 0x0008) {
193 start_line = BE_16(&s->buf[stream_ptr]);
195 lines_to_change = BE_16(&s->buf[stream_ptr]);
199 lines_to_change = s->avctx->height;
202 row_ptr = row_inc * start_line;
203 while (lines_to_change--) {
205 pixel_ptr = row_ptr + (4 * (s->buf[stream_ptr++] - 1));
207 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
209 /* there's another skip code in the stream */
211 pixel_ptr += (4 * (s->buf[stream_ptr++] - 1));
212 } else if (rle_code < 0) {
213 /* decode the run length code */
214 rle_code = -rle_code;
215 /* get the next 4 bytes from the stream, treat them as palette
216 * indices, and output them rle_code times */
218 pi1 = s->buf[stream_ptr++];
219 pi2 = s->buf[stream_ptr++];
220 pi3 = s->buf[stream_ptr++];
221 pi4 = s->buf[stream_ptr++];
223 CHECK_PIXEL_PTR(rle_code * 4);
226 rgb[pixel_ptr++] = pi1;
227 rgb[pixel_ptr++] = pi2;
228 rgb[pixel_ptr++] = pi3;
229 rgb[pixel_ptr++] = pi4;
232 /* copy the same pixel directly to output 4 times */
234 CHECK_STREAM_PTR(rle_code);
235 CHECK_PIXEL_PTR(rle_code);
238 rgb[pixel_ptr++] = s->buf[stream_ptr++];
246 static void qtrle_decode_16bpp(QtrleContext *s)
252 signed char rle_code;
253 int row_ptr, pixel_ptr;
254 int row_inc = s->frame.linesize[0];
255 unsigned short rgb16;
256 unsigned char *rgb = s->frame.data[0];
257 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
259 /* check if this frame is even supposed to change */
263 /* start after the chunk size */
266 /* fetch the header */
268 header = BE_16(&s->buf[stream_ptr]);
271 /* if a header is present, fetch additional decoding parameters */
272 if (header & 0x0008) {
274 start_line = BE_16(&s->buf[stream_ptr]);
276 lines_to_change = BE_16(&s->buf[stream_ptr]);
280 lines_to_change = s->avctx->height;
283 row_ptr = row_inc * start_line;
284 while (lines_to_change--) {
286 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 2;
288 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
290 /* there's another skip code in the stream */
292 pixel_ptr += (s->buf[stream_ptr++] - 1) * 2;
293 } else if (rle_code < 0) {
294 /* decode the run length code */
295 rle_code = -rle_code;
297 rgb16 = BE_16(&s->buf[stream_ptr]);
300 CHECK_PIXEL_PTR(rle_code * 2);
303 *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
307 CHECK_STREAM_PTR(rle_code * 2);
308 CHECK_PIXEL_PTR(rle_code * 2);
310 /* copy pixels directly to output */
312 rgb16 = BE_16(&s->buf[stream_ptr]);
314 *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
323 static void qtrle_decode_24bpp(QtrleContext *s)
329 signed char rle_code;
330 int row_ptr, pixel_ptr;
331 int row_inc = s->frame.linesize[0];
332 unsigned char r, g, b;
333 unsigned char *rgb = s->frame.data[0];
334 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
336 /* check if this frame is even supposed to change */
340 /* start after the chunk size */
343 /* fetch the header */
345 header = BE_16(&s->buf[stream_ptr]);
348 /* if a header is present, fetch additional decoding parameters */
349 if (header & 0x0008) {
351 start_line = BE_16(&s->buf[stream_ptr]);
353 lines_to_change = BE_16(&s->buf[stream_ptr]);
357 lines_to_change = s->avctx->height;
360 row_ptr = row_inc * start_line;
361 while (lines_to_change--) {
363 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 3;
365 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
367 /* there's another skip code in the stream */
369 pixel_ptr += (s->buf[stream_ptr++] - 1) * 3;
370 } else if (rle_code < 0) {
371 /* decode the run length code */
372 rle_code = -rle_code;
374 r = s->buf[stream_ptr++];
375 g = s->buf[stream_ptr++];
376 b = s->buf[stream_ptr++];
378 CHECK_PIXEL_PTR(rle_code * 3);
381 rgb[pixel_ptr++] = r;
382 rgb[pixel_ptr++] = g;
383 rgb[pixel_ptr++] = b;
386 CHECK_STREAM_PTR(rle_code * 3);
387 CHECK_PIXEL_PTR(rle_code * 3);
389 /* copy pixels directly to output */
391 rgb[pixel_ptr++] = s->buf[stream_ptr++];
392 rgb[pixel_ptr++] = s->buf[stream_ptr++];
393 rgb[pixel_ptr++] = s->buf[stream_ptr++];
401 static void qtrle_decode_32bpp(QtrleContext *s)
407 signed char rle_code;
408 int row_ptr, pixel_ptr;
409 int row_inc = s->frame.linesize[0];
410 unsigned char r, g, b;
412 unsigned char *rgb = s->frame.data[0];
413 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
415 /* check if this frame is even supposed to change */
419 /* start after the chunk size */
422 /* fetch the header */
424 header = BE_16(&s->buf[stream_ptr]);
427 /* if a header is present, fetch additional decoding parameters */
428 if (header & 0x0008) {
430 start_line = BE_16(&s->buf[stream_ptr]);
432 lines_to_change = BE_16(&s->buf[stream_ptr]);
436 lines_to_change = s->avctx->height;
439 row_ptr = row_inc * start_line;
440 while (lines_to_change--) {
442 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 4;
444 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
446 /* there's another skip code in the stream */
448 pixel_ptr += (s->buf[stream_ptr++] - 1) * 4;
449 } else if (rle_code < 0) {
450 /* decode the run length code */
451 rle_code = -rle_code;
453 stream_ptr++; /* skip the alpha (?) byte */
454 r = s->buf[stream_ptr++];
455 g = s->buf[stream_ptr++];
456 b = s->buf[stream_ptr++];
457 argb = (r << 16) | (g << 8) | (b << 0);
459 CHECK_PIXEL_PTR(rle_code * 4);
462 *(unsigned int *)(&rgb[pixel_ptr]) = argb;
466 CHECK_STREAM_PTR(rle_code * 4);
467 CHECK_PIXEL_PTR(rle_code * 4);
469 /* copy pixels directly to output */
471 stream_ptr++; /* skip the alpha (?) byte */
472 r = s->buf[stream_ptr++];
473 g = s->buf[stream_ptr++];
474 b = s->buf[stream_ptr++];
475 argb = (r << 16) | (g << 8) | (b << 0);
476 *(unsigned int *)(&rgb[pixel_ptr]) = argb;
485 static int qtrle_decode_init(AVCodecContext *avctx)
487 QtrleContext *s = (QtrleContext *)avctx->priv_data;
490 switch (avctx->bits_per_sample) {
499 avctx->pix_fmt = PIX_FMT_PAL8;
503 avctx->pix_fmt = PIX_FMT_RGB555;
507 avctx->pix_fmt = PIX_FMT_RGB24;
511 avctx->pix_fmt = PIX_FMT_RGBA32;
515 av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
516 avctx->bits_per_sample);
519 avctx->has_b_frames = 0;
520 dsputil_init(&s->dsp, avctx);
522 s->frame.data[0] = NULL;
527 static int qtrle_decode_frame(AVCodecContext *avctx,
528 void *data, int *data_size,
529 uint8_t *buf, int buf_size)
531 QtrleContext *s = (QtrleContext *)avctx->priv_data;
533 /* no supplementary picture */
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 = (QtrleContext *)avctx->priv_data;
610 if (s->frame.data[0])
611 avctx->release_buffer(avctx, &s->frame);
616 AVCodec qtrle_decoder = {
620 sizeof(QtrleContext),