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
24 * QT RLE Video Decoder by Mike Melanson (melanson@pcisys.net)
25 * For more information about the QT RLE format, visit:
26 * http://www.pcisys.net/~melanson/codecs/
28 * The QT RLE decoder has seven modes of operation:
29 * 1, 2, 4, 8, 16, 24, and 32 bits per pixel. For modes 1, 2, 4, and 8
30 * the decoder outputs PAL8 colorspace data. 16-bit data yields RGB555
31 * data. 24-bit data is RGB24 and 32-bit data is RGB32.
38 #include "libavutil/intreadwrite.h"
41 typedef struct QtrleContext {
43 AVCodecContext *avctx;
46 const unsigned char *buf;
52 #define CHECK_STREAM_PTR(n) \
53 if ((stream_ptr + n) > s->size) { \
54 av_log (s->avctx, AV_LOG_INFO, "Problem: stream_ptr out of bounds (%d >= %d)\n", \
55 stream_ptr + n, s->size); \
59 #define CHECK_PIXEL_PTR(n) \
60 if ((pixel_ptr + n > pixel_limit) || (pixel_ptr + n < 0)) { \
61 av_log (s->avctx, AV_LOG_INFO, "Problem: pixel_ptr = %d, pixel_limit = %d\n", \
62 pixel_ptr + n, pixel_limit); \
66 static void qtrle_decode_1bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
70 int row_inc = s->frame.linesize[0];
71 unsigned char pi0, pi1; /* 2 8-pixel values */
72 unsigned char *rgb = s->frame.data[0];
73 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
76 while (lines_to_change) {
78 skip = s->buf[stream_ptr++];
79 rle_code = (signed char)s->buf[stream_ptr++];
85 pixel_ptr = row_ptr + 2 * (skip & 0x7f);
87 pixel_ptr += 2 * skip;
88 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
91 /* decode the run length code */
93 /* get the next 2 bytes from the stream, treat them as groups
94 * of 8 pixels, and output them rle_code times */
96 pi0 = s->buf[stream_ptr++];
97 pi1 = s->buf[stream_ptr++];
98 CHECK_PIXEL_PTR(rle_code * 2);
101 rgb[pixel_ptr++] = pi0;
102 rgb[pixel_ptr++] = pi1;
105 /* copy the same pixel directly to output 2 times */
107 CHECK_STREAM_PTR(rle_code);
108 CHECK_PIXEL_PTR(rle_code);
111 rgb[pixel_ptr++] = s->buf[stream_ptr++];
116 static inline void qtrle_decode_2n4bpp(QtrleContext *s, int stream_ptr,
117 int row_ptr, int lines_to_change, int bpp)
121 int row_inc = s->frame.linesize[0];
122 unsigned char pi[16]; /* 16 palette indices */
123 unsigned char *rgb = s->frame.data[0];
124 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
125 int num_pixels = (bpp == 4) ? 8 : 16;
127 while (lines_to_change--) {
129 pixel_ptr = row_ptr + (num_pixels * (s->buf[stream_ptr++] - 1));
130 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
132 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
134 /* there's another skip code in the stream */
136 pixel_ptr += (num_pixels * (s->buf[stream_ptr++] - 1));
137 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
138 } else if (rle_code < 0) {
139 /* decode the run length code */
140 rle_code = -rle_code;
141 /* get the next 4 bytes from the stream, treat them as palette
142 * indexes, and output them rle_code times */
144 for (i = num_pixels-1; i >= 0; i--) {
145 pi[num_pixels-1-i] = (s->buf[stream_ptr] >> ((i*bpp) & 0x07)) & ((1<<bpp)-1);
146 stream_ptr+= ((i & ((num_pixels>>2)-1)) == 0);
148 CHECK_PIXEL_PTR(rle_code * num_pixels);
150 for (i = 0; i < num_pixels; i++)
151 rgb[pixel_ptr++] = pi[i];
154 /* copy the same pixel directly to output 4 times */
156 CHECK_STREAM_PTR(rle_code);
157 CHECK_PIXEL_PTR(rle_code*(num_pixels>>2));
160 rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 4) & 0x0f;
161 rgb[pixel_ptr++] = (s->buf[stream_ptr++]) & 0x0f;
163 rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 6) & 0x03;
164 rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 4) & 0x03;
165 rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 2) & 0x03;
166 rgb[pixel_ptr++] = (s->buf[stream_ptr++]) & 0x03;
175 static void qtrle_decode_8bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
179 int row_inc = s->frame.linesize[0];
180 unsigned char pi1, pi2, pi3, pi4; /* 4 palette indexes */
181 unsigned char *rgb = s->frame.data[0];
182 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
184 while (lines_to_change--) {
186 pixel_ptr = row_ptr + (4 * (s->buf[stream_ptr++] - 1));
187 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
189 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
191 /* there's another skip code in the stream */
193 pixel_ptr += (4 * (s->buf[stream_ptr++] - 1));
194 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
195 } else if (rle_code < 0) {
196 /* decode the run length code */
197 rle_code = -rle_code;
198 /* get the next 4 bytes from the stream, treat them as palette
199 * indexes, and output them rle_code times */
201 pi1 = s->buf[stream_ptr++];
202 pi2 = s->buf[stream_ptr++];
203 pi3 = s->buf[stream_ptr++];
204 pi4 = s->buf[stream_ptr++];
206 CHECK_PIXEL_PTR(rle_code * 4);
209 rgb[pixel_ptr++] = pi1;
210 rgb[pixel_ptr++] = pi2;
211 rgb[pixel_ptr++] = pi3;
212 rgb[pixel_ptr++] = pi4;
215 /* copy the same pixel directly to output 4 times */
217 CHECK_STREAM_PTR(rle_code);
218 CHECK_PIXEL_PTR(rle_code);
221 rgb[pixel_ptr++] = s->buf[stream_ptr++];
229 static void qtrle_decode_16bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
233 int row_inc = s->frame.linesize[0];
234 unsigned short rgb16;
235 unsigned char *rgb = s->frame.data[0];
236 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
238 while (lines_to_change--) {
240 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 2;
241 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
243 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
245 /* there's another skip code in the stream */
247 pixel_ptr += (s->buf[stream_ptr++] - 1) * 2;
248 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
249 } else if (rle_code < 0) {
250 /* decode the run length code */
251 rle_code = -rle_code;
253 rgb16 = AV_RB16(&s->buf[stream_ptr]);
256 CHECK_PIXEL_PTR(rle_code * 2);
259 *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
263 CHECK_STREAM_PTR(rle_code * 2);
264 CHECK_PIXEL_PTR(rle_code * 2);
266 /* copy pixels directly to output */
268 rgb16 = AV_RB16(&s->buf[stream_ptr]);
270 *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
279 static void qtrle_decode_24bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
283 int row_inc = s->frame.linesize[0];
284 unsigned char r, g, b;
285 unsigned char *rgb = s->frame.data[0];
286 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
288 while (lines_to_change--) {
290 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 3;
291 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
293 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
295 /* there's another skip code in the stream */
297 pixel_ptr += (s->buf[stream_ptr++] - 1) * 3;
298 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
299 } else if (rle_code < 0) {
300 /* decode the run length code */
301 rle_code = -rle_code;
303 r = s->buf[stream_ptr++];
304 g = s->buf[stream_ptr++];
305 b = s->buf[stream_ptr++];
307 CHECK_PIXEL_PTR(rle_code * 3);
310 rgb[pixel_ptr++] = r;
311 rgb[pixel_ptr++] = g;
312 rgb[pixel_ptr++] = b;
315 CHECK_STREAM_PTR(rle_code * 3);
316 CHECK_PIXEL_PTR(rle_code * 3);
318 /* copy pixels directly to output */
320 rgb[pixel_ptr++] = s->buf[stream_ptr++];
321 rgb[pixel_ptr++] = s->buf[stream_ptr++];
322 rgb[pixel_ptr++] = s->buf[stream_ptr++];
330 static void qtrle_decode_32bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
334 int row_inc = s->frame.linesize[0];
335 unsigned char a, r, g, b;
337 unsigned char *rgb = s->frame.data[0];
338 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
340 while (lines_to_change--) {
342 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 4;
343 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
345 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
347 /* there's another skip code in the stream */
349 pixel_ptr += (s->buf[stream_ptr++] - 1) * 4;
350 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
351 } else if (rle_code < 0) {
352 /* decode the run length code */
353 rle_code = -rle_code;
355 a = s->buf[stream_ptr++];
356 r = s->buf[stream_ptr++];
357 g = s->buf[stream_ptr++];
358 b = s->buf[stream_ptr++];
359 argb = (a << 24) | (r << 16) | (g << 8) | (b << 0);
361 CHECK_PIXEL_PTR(rle_code * 4);
364 *(unsigned int *)(&rgb[pixel_ptr]) = argb;
368 CHECK_STREAM_PTR(rle_code * 4);
369 CHECK_PIXEL_PTR(rle_code * 4);
371 /* copy pixels directly to output */
373 a = s->buf[stream_ptr++];
374 r = s->buf[stream_ptr++];
375 g = s->buf[stream_ptr++];
376 b = s->buf[stream_ptr++];
377 argb = (a << 24) | (r << 16) | (g << 8) | (b << 0);
378 *(unsigned int *)(&rgb[pixel_ptr]) = argb;
387 static av_cold int qtrle_decode_init(AVCodecContext *avctx)
389 QtrleContext *s = avctx->priv_data;
392 switch (avctx->bits_per_coded_sample) {
395 avctx->pix_fmt = PIX_FMT_MONOWHITE;
404 avctx->pix_fmt = PIX_FMT_PAL8;
408 avctx->pix_fmt = PIX_FMT_RGB555;
412 avctx->pix_fmt = PIX_FMT_RGB24;
416 avctx->pix_fmt = PIX_FMT_RGB32;
420 av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
421 avctx->bits_per_coded_sample);
425 avcodec_get_frame_defaults(&s->frame);
426 s->frame.data[0] = NULL;
431 static int qtrle_decode_frame(AVCodecContext *avctx,
432 void *data, int *data_size,
435 const uint8_t *buf = avpkt->data;
436 int buf_size = avpkt->size;
437 QtrleContext *s = avctx->priv_data;
438 int header, start_line;
439 int stream_ptr, height, row_ptr;
445 s->frame.reference = 3;
446 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
447 FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
448 if (avctx->reget_buffer(avctx, &s->frame)) {
449 av_log (s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
453 /* check if this frame is even supposed to change */
457 /* start after the chunk size */
460 /* fetch the header */
461 header = AV_RB16(&s->buf[stream_ptr]);
464 /* if a header is present, fetch additional decoding parameters */
465 if (header & 0x0008) {
468 start_line = AV_RB16(&s->buf[stream_ptr]);
470 height = AV_RB16(&s->buf[stream_ptr]);
472 if (height > s->avctx->height - start_line)
476 height = s->avctx->height;
478 row_ptr = s->frame.linesize[0] * start_line;
480 switch (avctx->bits_per_coded_sample) {
483 qtrle_decode_1bpp(s, stream_ptr, row_ptr, height);
488 qtrle_decode_2n4bpp(s, stream_ptr, row_ptr, height, 2);
494 qtrle_decode_2n4bpp(s, stream_ptr, row_ptr, height, 4);
500 qtrle_decode_8bpp(s, stream_ptr, row_ptr, height);
505 qtrle_decode_16bpp(s, stream_ptr, row_ptr, height);
509 qtrle_decode_24bpp(s, stream_ptr, row_ptr, height);
513 qtrle_decode_32bpp(s, stream_ptr, row_ptr, height);
517 av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
518 avctx->bits_per_coded_sample);
523 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
526 s->frame.palette_has_changed = 1;
527 memcpy(s->pal, pal, AVPALETTE_SIZE);
530 /* make the palette available on the way out */
531 memcpy(s->frame.data[1], s->pal, AVPALETTE_SIZE);
535 *data_size = sizeof(AVFrame);
536 *(AVFrame*)data = s->frame;
538 /* always report that the buffer was completely consumed */
542 static av_cold int qtrle_decode_end(AVCodecContext *avctx)
544 QtrleContext *s = avctx->priv_data;
546 if (s->frame.data[0])
547 avctx->release_buffer(avctx, &s->frame);
552 AVCodec ff_qtrle_decoder = {
554 .type = AVMEDIA_TYPE_VIDEO,
555 .id = CODEC_ID_QTRLE,
556 .priv_data_size = sizeof(QtrleContext),
557 .init = qtrle_decode_init,
558 .close = qtrle_decode_end,
559 .decode = qtrle_decode_frame,
560 .capabilities = CODEC_CAP_DR1,
561 .long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),