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)
79 static void qtrle_decode_8bpp(QtrleContext *s)
86 int row_ptr, pixel_ptr;
87 int row_inc = s->frame.linesize[0];
88 unsigned char pi1, pi2, pi3, pi4; /* 4 palette indices */
89 unsigned char *rgb = s->frame.data[0];
90 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
92 /* check if this frame is even supposed to change */
96 /* start after the chunk size */
99 /* fetch the header */
101 header = BE_16(&s->buf[stream_ptr]);
104 /* if a header is present, fetch additional decoding parameters */
105 if (header & 0x0008) {
107 start_line = BE_16(&s->buf[stream_ptr]);
109 lines_to_change = BE_16(&s->buf[stream_ptr]);
113 lines_to_change = s->avctx->height;
116 row_ptr = row_inc * start_line;
117 while (lines_to_change--) {
119 pixel_ptr = row_ptr + (4 * (s->buf[stream_ptr++] - 1));
121 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
123 /* there's another skip code in the stream */
125 pixel_ptr += (4 * (s->buf[stream_ptr++] - 1));
126 } else if (rle_code < 0) {
127 /* decode the run length code */
128 rle_code = -rle_code;
129 /* get the next 4 bytes from the stream, treat them as palette
130 * indices, and output them rle_code times */
132 pi1 = s->buf[stream_ptr++];
133 pi2 = s->buf[stream_ptr++];
134 pi3 = s->buf[stream_ptr++];
135 pi4 = s->buf[stream_ptr++];
137 CHECK_PIXEL_PTR(rle_code * 4);
140 rgb[pixel_ptr++] = pi1;
141 rgb[pixel_ptr++] = pi2;
142 rgb[pixel_ptr++] = pi3;
143 rgb[pixel_ptr++] = pi4;
146 /* copy the same pixel directly to output 4 times */
148 CHECK_STREAM_PTR(rle_code);
149 CHECK_PIXEL_PTR(rle_code);
152 rgb[pixel_ptr++] = s->buf[stream_ptr++];
160 static void qtrle_decode_16bpp(QtrleContext *s)
166 signed char rle_code;
167 int row_ptr, pixel_ptr;
168 int row_inc = s->frame.linesize[0];
169 unsigned short rgb16;
170 unsigned char *rgb = s->frame.data[0];
171 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
173 /* check if this frame is even supposed to change */
177 /* start after the chunk size */
180 /* fetch the header */
182 header = BE_16(&s->buf[stream_ptr]);
185 /* if a header is present, fetch additional decoding parameters */
186 if (header & 0x0008) {
188 start_line = BE_16(&s->buf[stream_ptr]);
190 lines_to_change = BE_16(&s->buf[stream_ptr]);
194 lines_to_change = s->avctx->height;
197 row_ptr = row_inc * start_line;
198 while (lines_to_change--) {
200 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 2;
202 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
204 /* there's another skip code in the stream */
206 pixel_ptr += (s->buf[stream_ptr++] - 1) * 2;
207 } else if (rle_code < 0) {
208 /* decode the run length code */
209 rle_code = -rle_code;
211 rgb16 = BE_16(&s->buf[stream_ptr]);
214 CHECK_PIXEL_PTR(rle_code * 2);
217 *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
221 CHECK_STREAM_PTR(rle_code * 2);
222 CHECK_PIXEL_PTR(rle_code * 2);
224 /* copy pixels directly to output */
226 rgb16 = BE_16(&s->buf[stream_ptr]);
228 *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
237 static void qtrle_decode_24bpp(QtrleContext *s)
243 signed char rle_code;
244 int row_ptr, pixel_ptr;
245 int row_inc = s->frame.linesize[0];
246 unsigned char r, g, b;
247 unsigned char *rgb = s->frame.data[0];
248 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
250 /* check if this frame is even supposed to change */
254 /* start after the chunk size */
257 /* fetch the header */
259 header = BE_16(&s->buf[stream_ptr]);
262 /* if a header is present, fetch additional decoding parameters */
263 if (header & 0x0008) {
265 start_line = BE_16(&s->buf[stream_ptr]);
267 lines_to_change = BE_16(&s->buf[stream_ptr]);
271 lines_to_change = s->avctx->height;
274 row_ptr = row_inc * start_line;
275 while (lines_to_change--) {
277 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 3;
279 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
281 /* there's another skip code in the stream */
283 pixel_ptr += (s->buf[stream_ptr++] - 1) * 3;
284 } else if (rle_code < 0) {
285 /* decode the run length code */
286 rle_code = -rle_code;
288 r = s->buf[stream_ptr++];
289 g = s->buf[stream_ptr++];
290 b = s->buf[stream_ptr++];
292 CHECK_PIXEL_PTR(rle_code * 3);
295 rgb[pixel_ptr++] = r;
296 rgb[pixel_ptr++] = g;
297 rgb[pixel_ptr++] = b;
300 CHECK_STREAM_PTR(rle_code * 3);
301 CHECK_PIXEL_PTR(rle_code * 3);
303 /* copy pixels directly to output */
305 rgb[pixel_ptr++] = s->buf[stream_ptr++];
306 rgb[pixel_ptr++] = s->buf[stream_ptr++];
307 rgb[pixel_ptr++] = s->buf[stream_ptr++];
315 static void qtrle_decode_32bpp(QtrleContext *s)
321 signed char rle_code;
322 int row_ptr, pixel_ptr;
323 int row_inc = s->frame.linesize[0];
324 unsigned char r, g, b;
326 unsigned char *rgb = s->frame.data[0];
327 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
329 /* check if this frame is even supposed to change */
333 /* start after the chunk size */
336 /* fetch the header */
338 header = BE_16(&s->buf[stream_ptr]);
341 /* if a header is present, fetch additional decoding parameters */
342 if (header & 0x0008) {
344 start_line = BE_16(&s->buf[stream_ptr]);
346 lines_to_change = BE_16(&s->buf[stream_ptr]);
350 lines_to_change = s->avctx->height;
353 row_ptr = row_inc * start_line;
354 while (lines_to_change--) {
356 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 4;
358 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
360 /* there's another skip code in the stream */
362 pixel_ptr += (s->buf[stream_ptr++] - 1) * 4;
363 } else if (rle_code < 0) {
364 /* decode the run length code */
365 rle_code = -rle_code;
367 stream_ptr++; /* skip the alpha (?) byte */
368 r = s->buf[stream_ptr++];
369 g = s->buf[stream_ptr++];
370 b = s->buf[stream_ptr++];
371 argb = (r << 16) | (g << 8) | (b << 0);
373 CHECK_PIXEL_PTR(rle_code * 4);
376 *(unsigned int *)(&rgb[pixel_ptr]) = argb;
380 CHECK_STREAM_PTR(rle_code * 4);
381 CHECK_PIXEL_PTR(rle_code * 4);
383 /* copy pixels directly to output */
385 stream_ptr++; /* skip the alpha (?) byte */
386 r = s->buf[stream_ptr++];
387 g = s->buf[stream_ptr++];
388 b = s->buf[stream_ptr++];
389 argb = (r << 16) | (g << 8) | (b << 0);
390 *(unsigned int *)(&rgb[pixel_ptr]) = argb;
399 static int qtrle_decode_init(AVCodecContext *avctx)
401 QtrleContext *s = (QtrleContext *)avctx->priv_data;
404 switch (avctx->bits_per_sample) {
413 avctx->pix_fmt = PIX_FMT_PAL8;
417 avctx->pix_fmt = PIX_FMT_RGB555;
421 avctx->pix_fmt = PIX_FMT_RGB24;
425 avctx->pix_fmt = PIX_FMT_RGBA32;
429 av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
430 avctx->bits_per_sample);
433 avctx->has_b_frames = 0;
434 dsputil_init(&s->dsp, avctx);
436 s->frame.data[0] = NULL;
441 static int qtrle_decode_frame(AVCodecContext *avctx,
442 void *data, int *data_size,
443 uint8_t *buf, int buf_size)
445 QtrleContext *s = (QtrleContext *)avctx->priv_data;
447 /* no supplementary picture */
454 s->frame.reference = 1;
455 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
456 FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
457 if (avctx->reget_buffer(avctx, &s->frame)) {
458 av_log (s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
462 switch (avctx->bits_per_sample) {
465 qtrle_decode_1bpp(s);
470 qtrle_decode_2bpp(s);
475 qtrle_decode_4bpp(s);
480 qtrle_decode_8bpp(s);
481 /* make the palette available on the way out */
482 memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
483 if (s->avctx->palctrl->palette_changed) {
484 s->frame.palette_has_changed = 1;
485 s->avctx->palctrl->palette_changed = 0;
490 qtrle_decode_16bpp(s);
494 qtrle_decode_24bpp(s);
498 qtrle_decode_32bpp(s);
502 av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
503 avctx->bits_per_sample);
507 *data_size = sizeof(AVFrame);
508 *(AVFrame*)data = s->frame;
510 /* always report that the buffer was completely consumed */
514 static int qtrle_decode_end(AVCodecContext *avctx)
516 QtrleContext *s = (QtrleContext *)avctx->priv_data;
518 if (s->frame.data[0])
519 avctx->release_buffer(avctx, &s->frame);
524 AVCodec qtrle_decoder = {
528 sizeof(QtrleContext),