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;
75 /* skip & 0x80 appears to mean 'start a new line', which can be interpreted
76 * as 'go to next line' during the decoding of a frame but is 'go to first
77 * line' at the beginning. Since we always interpret it as 'go to next line'
78 * in the decoding loop (which makes code simpler/faster), the first line
79 * would not be counted, so we count one more.
80 * See: https://ffmpeg.org/trac/ffmpeg/ticket/226
81 * In the following decoding loop, row_ptr will be the position of the
85 while (lines_to_change) {
87 skip = s->buf[stream_ptr++];
88 rle_code = (signed char)s->buf[stream_ptr++];
93 pixel_ptr = row_ptr + 2 * (skip & 0x7f);
96 pixel_ptr += 2 * skip;
97 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
103 /* decode the run length code */
104 rle_code = -rle_code;
105 /* get the next 2 bytes from the stream, treat them as groups
106 * of 8 pixels, and output them rle_code times */
108 pi0 = s->buf[stream_ptr++];
109 pi1 = s->buf[stream_ptr++];
110 CHECK_PIXEL_PTR(rle_code * 2);
113 rgb[pixel_ptr++] = pi0;
114 rgb[pixel_ptr++] = pi1;
117 /* copy the same pixel directly to output 2 times */
119 CHECK_STREAM_PTR(rle_code);
120 CHECK_PIXEL_PTR(rle_code);
123 rgb[pixel_ptr++] = s->buf[stream_ptr++];
128 static inline void qtrle_decode_2n4bpp(QtrleContext *s, int stream_ptr,
129 int row_ptr, int lines_to_change, int bpp)
133 int row_inc = s->frame.linesize[0];
134 unsigned char pi[16]; /* 16 palette indices */
135 unsigned char *rgb = s->frame.data[0];
136 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
137 int num_pixels = (bpp == 4) ? 8 : 16;
139 while (lines_to_change--) {
141 pixel_ptr = row_ptr + (num_pixels * (s->buf[stream_ptr++] - 1));
142 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
144 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
146 /* there's another skip code in the stream */
148 pixel_ptr += (num_pixels * (s->buf[stream_ptr++] - 1));
149 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
150 } else if (rle_code < 0) {
151 /* decode the run length code */
152 rle_code = -rle_code;
153 /* get the next 4 bytes from the stream, treat them as palette
154 * indexes, and output them rle_code times */
156 for (i = num_pixels-1; i >= 0; i--) {
157 pi[num_pixels-1-i] = (s->buf[stream_ptr] >> ((i*bpp) & 0x07)) & ((1<<bpp)-1);
158 stream_ptr+= ((i & ((num_pixels>>2)-1)) == 0);
160 CHECK_PIXEL_PTR(rle_code * num_pixels);
162 for (i = 0; i < num_pixels; i++)
163 rgb[pixel_ptr++] = pi[i];
166 /* copy the same pixel directly to output 4 times */
168 CHECK_STREAM_PTR(rle_code);
169 CHECK_PIXEL_PTR(rle_code*(num_pixels>>2));
172 rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 4) & 0x0f;
173 rgb[pixel_ptr++] = (s->buf[stream_ptr++]) & 0x0f;
175 rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 6) & 0x03;
176 rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 4) & 0x03;
177 rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 2) & 0x03;
178 rgb[pixel_ptr++] = (s->buf[stream_ptr++]) & 0x03;
187 static void qtrle_decode_8bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
191 int row_inc = s->frame.linesize[0];
192 unsigned char pi1, pi2, pi3, pi4; /* 4 palette indexes */
193 unsigned char *rgb = s->frame.data[0];
194 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
196 while (lines_to_change--) {
198 pixel_ptr = row_ptr + (4 * (s->buf[stream_ptr++] - 1));
199 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
201 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
203 /* there's another skip code in the stream */
205 pixel_ptr += (4 * (s->buf[stream_ptr++] - 1));
206 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
207 } else if (rle_code < 0) {
208 /* decode the run length code */
209 rle_code = -rle_code;
210 /* get the next 4 bytes from the stream, treat them as palette
211 * indexes, and output them rle_code times */
213 pi1 = s->buf[stream_ptr++];
214 pi2 = s->buf[stream_ptr++];
215 pi3 = s->buf[stream_ptr++];
216 pi4 = s->buf[stream_ptr++];
218 CHECK_PIXEL_PTR(rle_code * 4);
221 rgb[pixel_ptr++] = pi1;
222 rgb[pixel_ptr++] = pi2;
223 rgb[pixel_ptr++] = pi3;
224 rgb[pixel_ptr++] = pi4;
227 /* copy the same pixel directly to output 4 times */
229 CHECK_STREAM_PTR(rle_code);
230 CHECK_PIXEL_PTR(rle_code);
233 rgb[pixel_ptr++] = s->buf[stream_ptr++];
241 static void qtrle_decode_16bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
245 int row_inc = s->frame.linesize[0];
246 unsigned short rgb16;
247 unsigned char *rgb = s->frame.data[0];
248 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
250 while (lines_to_change--) {
252 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 2;
253 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
255 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
257 /* there's another skip code in the stream */
259 pixel_ptr += (s->buf[stream_ptr++] - 1) * 2;
260 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
261 } else if (rle_code < 0) {
262 /* decode the run length code */
263 rle_code = -rle_code;
265 rgb16 = AV_RB16(&s->buf[stream_ptr]);
268 CHECK_PIXEL_PTR(rle_code * 2);
271 *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
275 CHECK_STREAM_PTR(rle_code * 2);
276 CHECK_PIXEL_PTR(rle_code * 2);
278 /* copy pixels directly to output */
280 rgb16 = AV_RB16(&s->buf[stream_ptr]);
282 *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
291 static void qtrle_decode_24bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
295 int row_inc = s->frame.linesize[0];
296 unsigned char r, g, b;
297 unsigned char *rgb = s->frame.data[0];
298 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
300 while (lines_to_change--) {
302 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 3;
303 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
305 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
307 /* there's another skip code in the stream */
309 pixel_ptr += (s->buf[stream_ptr++] - 1) * 3;
310 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
311 } else if (rle_code < 0) {
312 /* decode the run length code */
313 rle_code = -rle_code;
315 r = s->buf[stream_ptr++];
316 g = s->buf[stream_ptr++];
317 b = s->buf[stream_ptr++];
319 CHECK_PIXEL_PTR(rle_code * 3);
322 rgb[pixel_ptr++] = r;
323 rgb[pixel_ptr++] = g;
324 rgb[pixel_ptr++] = b;
327 CHECK_STREAM_PTR(rle_code * 3);
328 CHECK_PIXEL_PTR(rle_code * 3);
330 /* copy pixels directly to output */
332 rgb[pixel_ptr++] = s->buf[stream_ptr++];
333 rgb[pixel_ptr++] = s->buf[stream_ptr++];
334 rgb[pixel_ptr++] = s->buf[stream_ptr++];
342 static void qtrle_decode_32bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
346 int row_inc = s->frame.linesize[0];
348 unsigned char *rgb = s->frame.data[0];
349 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
351 while (lines_to_change--) {
353 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 4;
354 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
356 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
358 /* there's another skip code in the stream */
360 pixel_ptr += (s->buf[stream_ptr++] - 1) * 4;
361 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
362 } else if (rle_code < 0) {
363 /* decode the run length code */
364 rle_code = -rle_code;
366 argb = AV_RB32(s->buf + stream_ptr);
369 CHECK_PIXEL_PTR(rle_code * 4);
372 AV_WN32A(rgb + pixel_ptr, argb);
376 CHECK_STREAM_PTR(rle_code * 4);
377 CHECK_PIXEL_PTR(rle_code * 4);
379 /* copy pixels directly to output */
381 argb = AV_RB32(s->buf + stream_ptr);
382 AV_WN32A(rgb + pixel_ptr, argb);
392 static av_cold int qtrle_decode_init(AVCodecContext *avctx)
394 QtrleContext *s = avctx->priv_data;
397 switch (avctx->bits_per_coded_sample) {
400 avctx->pix_fmt = PIX_FMT_MONOWHITE;
409 avctx->pix_fmt = PIX_FMT_PAL8;
413 avctx->pix_fmt = PIX_FMT_RGB555;
417 avctx->pix_fmt = PIX_FMT_RGB24;
421 avctx->pix_fmt = PIX_FMT_RGB32;
425 av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
426 avctx->bits_per_coded_sample);
430 avcodec_get_frame_defaults(&s->frame);
431 s->frame.data[0] = NULL;
436 static int qtrle_decode_frame(AVCodecContext *avctx,
437 void *data, int *data_size,
440 const uint8_t *buf = avpkt->data;
441 int buf_size = avpkt->size;
442 QtrleContext *s = avctx->priv_data;
443 int header, start_line;
444 int stream_ptr, height, row_ptr;
450 s->frame.reference = 3;
451 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
452 FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
453 if (avctx->reget_buffer(avctx, &s->frame)) {
454 av_log (s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
458 /* check if this frame is even supposed to change */
462 /* start after the chunk size */
465 /* fetch the header */
466 header = AV_RB16(&s->buf[stream_ptr]);
469 /* if a header is present, fetch additional decoding parameters */
470 if (header & 0x0008) {
473 start_line = AV_RB16(&s->buf[stream_ptr]);
475 height = AV_RB16(&s->buf[stream_ptr]);
477 if (height > s->avctx->height - start_line)
481 height = s->avctx->height;
483 row_ptr = s->frame.linesize[0] * start_line;
485 switch (avctx->bits_per_coded_sample) {
488 qtrle_decode_1bpp(s, stream_ptr, row_ptr, height);
493 qtrle_decode_2n4bpp(s, stream_ptr, row_ptr, height, 2);
499 qtrle_decode_2n4bpp(s, stream_ptr, row_ptr, height, 4);
505 qtrle_decode_8bpp(s, stream_ptr, row_ptr, height);
510 qtrle_decode_16bpp(s, stream_ptr, row_ptr, height);
514 qtrle_decode_24bpp(s, stream_ptr, row_ptr, height);
518 qtrle_decode_32bpp(s, stream_ptr, row_ptr, height);
522 av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
523 avctx->bits_per_coded_sample);
528 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
531 s->frame.palette_has_changed = 1;
532 memcpy(s->pal, pal, AVPALETTE_SIZE);
535 /* make the palette available on the way out */
536 memcpy(s->frame.data[1], s->pal, AVPALETTE_SIZE);
540 *data_size = sizeof(AVFrame);
541 *(AVFrame*)data = s->frame;
543 /* always report that the buffer was completely consumed */
547 static av_cold int qtrle_decode_end(AVCodecContext *avctx)
549 QtrleContext *s = avctx->priv_data;
551 if (s->frame.data[0])
552 avctx->release_buffer(avctx, &s->frame);
557 AVCodec ff_qtrle_decoder = {
559 .type = AVMEDIA_TYPE_VIDEO,
560 .id = CODEC_ID_QTRLE,
561 .priv_data_size = sizeof(QtrleContext),
562 .init = qtrle_decode_init,
563 .close = qtrle_decode_end,
564 .decode = qtrle_decode_frame,
565 .capabilities = CODEC_CAP_DR1,
566 .long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),