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 RGB24
30 * data. 24-bit data is RGB888 and 32-bit data is RGBA32.
42 typedef struct QtrleContext {
44 AVCodecContext *avctx;
53 #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
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) { \
64 av_log (s->avctx, AV_LOG_INFO, "Problem: pixel_ptr >= pixel_limit (%d >= %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)
81 static void qtrle_decode_8bpp(QtrleContext *s)
88 int row_ptr, pixel_ptr;
89 int row_inc = s->frame.linesize[0];
90 unsigned char pi1, pi2, pi3, pi4; /* 4 palette indices */
91 unsigned char *rgb = s->frame.data[0];
92 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
94 /* check if this frame is even supposed to change */
98 /* start after the chunk size */
101 /* fetch the header */
103 header = BE_16(&s->buf[stream_ptr]);
106 /* if a header is present, fetch additional decoding parameters */
107 if (header & 0x0008) {
109 start_line = BE_16(&s->buf[stream_ptr]);
111 lines_to_change = BE_16(&s->buf[stream_ptr]);
115 lines_to_change = s->avctx->height;
118 row_ptr = row_inc * start_line;
119 while (lines_to_change--) {
121 pixel_ptr = row_ptr + (4 * (s->buf[stream_ptr++] - 1));
123 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
125 /* there's another skip code in the stream */
127 pixel_ptr += (4 * (s->buf[stream_ptr++] - 1));
128 } else if (rle_code < 0) {
129 /* decode the run length code */
130 rle_code = -rle_code;
131 /* get the next 4 bytes from the stream, treat them as palette
132 * indices, and output them rle_code times */
134 pi1 = s->buf[stream_ptr++];
135 pi2 = s->buf[stream_ptr++];
136 pi3 = s->buf[stream_ptr++];
137 pi4 = s->buf[stream_ptr++];
139 CHECK_PIXEL_PTR(rle_code * 4);
142 rgb[pixel_ptr++] = pi1;
143 rgb[pixel_ptr++] = pi2;
144 rgb[pixel_ptr++] = pi3;
145 rgb[pixel_ptr++] = pi4;
148 /* copy the same pixel directly to output 4 times */
150 CHECK_STREAM_PTR(rle_code);
151 CHECK_PIXEL_PTR(rle_code);
154 rgb[pixel_ptr++] = s->buf[stream_ptr++];
162 static void qtrle_decode_16bpp(QtrleContext *s)
168 signed char rle_code;
169 int row_ptr, pixel_ptr;
170 int row_inc = s->frame.linesize[0];
171 unsigned short rgb16;
172 unsigned char *rgb = s->frame.data[0];
173 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
175 /* check if this frame is even supposed to change */
179 /* start after the chunk size */
182 /* fetch the header */
184 header = BE_16(&s->buf[stream_ptr]);
187 /* if a header is present, fetch additional decoding parameters */
188 if (header & 0x0008) {
190 start_line = BE_16(&s->buf[stream_ptr]);
192 lines_to_change = BE_16(&s->buf[stream_ptr]);
196 lines_to_change = s->avctx->height;
199 row_ptr = row_inc * start_line;
200 while (lines_to_change--) {
202 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 2;
204 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
206 /* there's another skip code in the stream */
208 pixel_ptr += (s->buf[stream_ptr++] - 1) * 2;
209 } else if (rle_code < 0) {
210 /* decode the run length code */
211 rle_code = -rle_code;
213 rgb16 = BE_16(&s->buf[stream_ptr]);
216 CHECK_PIXEL_PTR(rle_code * 2);
219 *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
223 CHECK_STREAM_PTR(rle_code * 2);
224 CHECK_PIXEL_PTR(rle_code * 2);
226 /* copy pixels directly to output */
228 rgb16 = BE_16(&s->buf[stream_ptr]);
230 *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
239 static void qtrle_decode_24bpp(QtrleContext *s)
245 signed char rle_code;
246 int row_ptr, pixel_ptr;
247 int row_inc = s->frame.linesize[0];
248 unsigned char r, g, b;
249 unsigned char *rgb = s->frame.data[0];
250 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
252 /* check if this frame is even supposed to change */
256 /* start after the chunk size */
259 /* fetch the header */
261 header = BE_16(&s->buf[stream_ptr]);
264 /* if a header is present, fetch additional decoding parameters */
265 if (header & 0x0008) {
267 start_line = BE_16(&s->buf[stream_ptr]);
269 lines_to_change = BE_16(&s->buf[stream_ptr]);
273 lines_to_change = s->avctx->height;
276 row_ptr = row_inc * start_line;
277 while (lines_to_change--) {
279 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 3;
281 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
283 /* there's another skip code in the stream */
285 pixel_ptr += (s->buf[stream_ptr++] - 1) * 3;
286 } else if (rle_code < 0) {
287 /* decode the run length code */
288 rle_code = -rle_code;
290 r = s->buf[stream_ptr++];
291 g = s->buf[stream_ptr++];
292 b = s->buf[stream_ptr++];
294 CHECK_PIXEL_PTR(rle_code * 3);
297 rgb[pixel_ptr++] = r;
298 rgb[pixel_ptr++] = g;
299 rgb[pixel_ptr++] = b;
302 CHECK_STREAM_PTR(rle_code * 3);
303 CHECK_PIXEL_PTR(rle_code * 3);
305 /* copy pixels directly to output */
307 rgb[pixel_ptr++] = s->buf[stream_ptr++];
308 rgb[pixel_ptr++] = s->buf[stream_ptr++];
309 rgb[pixel_ptr++] = s->buf[stream_ptr++];
317 static void qtrle_decode_32bpp(QtrleContext *s)
323 signed char rle_code;
324 int row_ptr, pixel_ptr;
325 int row_inc = s->frame.linesize[0];
326 unsigned char r, g, b;
328 unsigned char *rgb = s->frame.data[0];
329 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
331 /* check if this frame is even supposed to change */
335 /* start after the chunk size */
338 /* fetch the header */
340 header = BE_16(&s->buf[stream_ptr]);
343 /* if a header is present, fetch additional decoding parameters */
344 if (header & 0x0008) {
346 start_line = BE_16(&s->buf[stream_ptr]);
348 lines_to_change = BE_16(&s->buf[stream_ptr]);
352 lines_to_change = s->avctx->height;
355 row_ptr = row_inc * start_line;
356 while (lines_to_change--) {
358 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 4;
360 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
362 /* there's another skip code in the stream */
364 pixel_ptr += (s->buf[stream_ptr++] - 1) * 4;
365 } else if (rle_code < 0) {
366 /* decode the run length code */
367 rle_code = -rle_code;
369 stream_ptr++; /* skip the alpha (?) byte */
370 r = s->buf[stream_ptr++];
371 g = s->buf[stream_ptr++];
372 b = s->buf[stream_ptr++];
373 argb = (r << 16) | (g << 8) | (b << 0);
375 CHECK_PIXEL_PTR(rle_code * 4);
378 *(unsigned int *)(&rgb[pixel_ptr]) = argb;
382 CHECK_STREAM_PTR(rle_code * 4);
383 CHECK_PIXEL_PTR(rle_code * 4);
385 /* copy pixels directly to output */
387 stream_ptr++; /* skip the alpha (?) byte */
388 r = s->buf[stream_ptr++];
389 g = s->buf[stream_ptr++];
390 b = s->buf[stream_ptr++];
391 argb = (r << 16) | (g << 8) | (b << 0);
392 *(unsigned int *)(&rgb[pixel_ptr]) = argb;
401 static int qtrle_decode_init(AVCodecContext *avctx)
403 QtrleContext *s = (QtrleContext *)avctx->priv_data;
406 switch (avctx->bits_per_sample) {
415 avctx->pix_fmt = PIX_FMT_PAL8;
419 avctx->pix_fmt = PIX_FMT_RGB555;
423 avctx->pix_fmt = PIX_FMT_RGB24;
427 avctx->pix_fmt = PIX_FMT_RGBA32;
431 av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
432 avctx->bits_per_sample);
435 avctx->has_b_frames = 0;
436 dsputil_init(&s->dsp, avctx);
438 s->frame.data[0] = NULL;
443 static int qtrle_decode_frame(AVCodecContext *avctx,
444 void *data, int *data_size,
445 uint8_t *buf, int buf_size)
447 QtrleContext *s = (QtrleContext *)avctx->priv_data;
449 /* no supplementary picture */
456 s->frame.reference = 1;
457 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
458 FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
459 if (avctx->reget_buffer(avctx, &s->frame)) {
460 av_log (s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
464 switch (avctx->bits_per_sample) {
467 qtrle_decode_1bpp(s);
472 qtrle_decode_2bpp(s);
477 qtrle_decode_4bpp(s);
482 qtrle_decode_8bpp(s);
483 /* make the palette available on the way out */
484 memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
485 if (s->avctx->palctrl->palette_changed) {
486 s->frame.palette_has_changed = 1;
487 s->avctx->palctrl->palette_changed = 0;
492 qtrle_decode_16bpp(s);
496 qtrle_decode_24bpp(s);
500 qtrle_decode_32bpp(s);
504 av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
505 avctx->bits_per_sample);
509 *data_size = sizeof(AVFrame);
510 *(AVFrame*)data = s->frame;
512 /* always report that the buffer was completely consumed */
516 static int qtrle_decode_end(AVCodecContext *avctx)
518 QtrleContext *s = (QtrleContext *)avctx->priv_data;
520 if (s->frame.data[0])
521 avctx->release_buffer(avctx, &s->frame);
526 AVCodec qtrle_decoder = {
530 sizeof(QtrleContext),