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 */
100 /* decode the run length code */
101 rle_code = -rle_code;
102 /* get the next 2 bytes from the stream, treat them as groups
103 * of 8 pixels, and output them rle_code times */
105 pi0 = s->buf[stream_ptr++];
106 pi1 = s->buf[stream_ptr++];
107 CHECK_PIXEL_PTR(rle_code * 2);
110 rgb[pixel_ptr++] = pi0;
111 rgb[pixel_ptr++] = pi1;
114 /* copy the same pixel directly to output 2 times */
116 CHECK_STREAM_PTR(rle_code);
117 CHECK_PIXEL_PTR(rle_code);
120 rgb[pixel_ptr++] = s->buf[stream_ptr++];
125 static inline void qtrle_decode_2n4bpp(QtrleContext *s, int stream_ptr,
126 int row_ptr, int lines_to_change, int bpp)
130 int row_inc = s->frame.linesize[0];
131 unsigned char pi[16]; /* 16 palette indices */
132 unsigned char *rgb = s->frame.data[0];
133 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
134 int num_pixels = (bpp == 4) ? 8 : 16;
136 while (lines_to_change--) {
138 pixel_ptr = row_ptr + (num_pixels * (s->buf[stream_ptr++] - 1));
139 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
141 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
143 /* there's another skip code in the stream */
145 pixel_ptr += (num_pixels * (s->buf[stream_ptr++] - 1));
146 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
147 } else if (rle_code < 0) {
148 /* decode the run length code */
149 rle_code = -rle_code;
150 /* get the next 4 bytes from the stream, treat them as palette
151 * indexes, and output them rle_code times */
153 for (i = num_pixels-1; i >= 0; i--) {
154 pi[num_pixels-1-i] = (s->buf[stream_ptr] >> ((i*bpp) & 0x07)) & ((1<<bpp)-1);
155 stream_ptr+= ((i & ((num_pixels>>2)-1)) == 0);
157 CHECK_PIXEL_PTR(rle_code * num_pixels);
159 for (i = 0; i < num_pixels; i++)
160 rgb[pixel_ptr++] = pi[i];
163 /* copy the same pixel directly to output 4 times */
165 CHECK_STREAM_PTR(rle_code);
166 CHECK_PIXEL_PTR(rle_code*(num_pixels>>2));
169 rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 4) & 0x0f;
170 rgb[pixel_ptr++] = (s->buf[stream_ptr++]) & 0x0f;
172 rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 6) & 0x03;
173 rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 4) & 0x03;
174 rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 2) & 0x03;
175 rgb[pixel_ptr++] = (s->buf[stream_ptr++]) & 0x03;
184 static void qtrle_decode_8bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
188 int row_inc = s->frame.linesize[0];
189 unsigned char pi1, pi2, pi3, pi4; /* 4 palette indexes */
190 unsigned char *rgb = s->frame.data[0];
191 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
193 while (lines_to_change--) {
195 pixel_ptr = row_ptr + (4 * (s->buf[stream_ptr++] - 1));
196 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
198 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
200 /* there's another skip code in the stream */
202 pixel_ptr += (4 * (s->buf[stream_ptr++] - 1));
203 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
204 } else if (rle_code < 0) {
205 /* decode the run length code */
206 rle_code = -rle_code;
207 /* get the next 4 bytes from the stream, treat them as palette
208 * indexes, and output them rle_code times */
210 pi1 = s->buf[stream_ptr++];
211 pi2 = s->buf[stream_ptr++];
212 pi3 = s->buf[stream_ptr++];
213 pi4 = s->buf[stream_ptr++];
215 CHECK_PIXEL_PTR(rle_code * 4);
218 rgb[pixel_ptr++] = pi1;
219 rgb[pixel_ptr++] = pi2;
220 rgb[pixel_ptr++] = pi3;
221 rgb[pixel_ptr++] = pi4;
224 /* copy the same pixel directly to output 4 times */
226 CHECK_STREAM_PTR(rle_code);
227 CHECK_PIXEL_PTR(rle_code);
230 rgb[pixel_ptr++] = s->buf[stream_ptr++];
238 static void qtrle_decode_16bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
242 int row_inc = s->frame.linesize[0];
243 unsigned short rgb16;
244 unsigned char *rgb = s->frame.data[0];
245 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
247 while (lines_to_change--) {
249 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 2;
250 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
252 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
254 /* there's another skip code in the stream */
256 pixel_ptr += (s->buf[stream_ptr++] - 1) * 2;
257 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
258 } else if (rle_code < 0) {
259 /* decode the run length code */
260 rle_code = -rle_code;
262 rgb16 = AV_RB16(&s->buf[stream_ptr]);
265 CHECK_PIXEL_PTR(rle_code * 2);
268 *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
272 CHECK_STREAM_PTR(rle_code * 2);
273 CHECK_PIXEL_PTR(rle_code * 2);
275 /* copy pixels directly to output */
277 rgb16 = AV_RB16(&s->buf[stream_ptr]);
279 *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
288 static void qtrle_decode_24bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
292 int row_inc = s->frame.linesize[0];
293 unsigned char r, g, b;
294 unsigned char *rgb = s->frame.data[0];
295 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
297 while (lines_to_change--) {
299 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 3;
300 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
302 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
304 /* there's another skip code in the stream */
306 pixel_ptr += (s->buf[stream_ptr++] - 1) * 3;
307 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
308 } else if (rle_code < 0) {
309 /* decode the run length code */
310 rle_code = -rle_code;
312 r = s->buf[stream_ptr++];
313 g = s->buf[stream_ptr++];
314 b = s->buf[stream_ptr++];
316 CHECK_PIXEL_PTR(rle_code * 3);
319 rgb[pixel_ptr++] = r;
320 rgb[pixel_ptr++] = g;
321 rgb[pixel_ptr++] = b;
324 CHECK_STREAM_PTR(rle_code * 3);
325 CHECK_PIXEL_PTR(rle_code * 3);
327 /* copy pixels directly to output */
329 rgb[pixel_ptr++] = s->buf[stream_ptr++];
330 rgb[pixel_ptr++] = s->buf[stream_ptr++];
331 rgb[pixel_ptr++] = s->buf[stream_ptr++];
339 static void qtrle_decode_32bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
343 int row_inc = s->frame.linesize[0];
345 unsigned char *rgb = s->frame.data[0];
346 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
348 while (lines_to_change--) {
350 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 4;
351 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
353 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
355 /* there's another skip code in the stream */
357 pixel_ptr += (s->buf[stream_ptr++] - 1) * 4;
358 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
359 } else if (rle_code < 0) {
360 /* decode the run length code */
361 rle_code = -rle_code;
363 argb = AV_RB32(s->buf + stream_ptr);
366 CHECK_PIXEL_PTR(rle_code * 4);
369 AV_WN32A(rgb + pixel_ptr, argb);
373 CHECK_STREAM_PTR(rle_code * 4);
374 CHECK_PIXEL_PTR(rle_code * 4);
376 /* copy pixels directly to output */
378 argb = AV_RB32(s->buf + stream_ptr);
379 AV_WN32A(rgb + pixel_ptr, argb);
389 static av_cold int qtrle_decode_init(AVCodecContext *avctx)
391 QtrleContext *s = avctx->priv_data;
394 switch (avctx->bits_per_coded_sample) {
397 avctx->pix_fmt = PIX_FMT_MONOWHITE;
406 avctx->pix_fmt = PIX_FMT_PAL8;
410 avctx->pix_fmt = PIX_FMT_RGB555;
414 avctx->pix_fmt = PIX_FMT_RGB24;
418 avctx->pix_fmt = PIX_FMT_RGB32;
422 av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
423 avctx->bits_per_coded_sample);
427 avcodec_get_frame_defaults(&s->frame);
428 s->frame.data[0] = NULL;
433 static int qtrle_decode_frame(AVCodecContext *avctx,
434 void *data, int *data_size,
437 const uint8_t *buf = avpkt->data;
438 int buf_size = avpkt->size;
439 QtrleContext *s = avctx->priv_data;
440 int header, start_line;
441 int stream_ptr, height, row_ptr;
447 s->frame.reference = 3;
448 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
449 FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
450 if (avctx->reget_buffer(avctx, &s->frame)) {
451 av_log (s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
455 /* check if this frame is even supposed to change */
459 /* start after the chunk size */
462 /* fetch the header */
463 header = AV_RB16(&s->buf[stream_ptr]);
466 /* if a header is present, fetch additional decoding parameters */
467 if (header & 0x0008) {
470 start_line = AV_RB16(&s->buf[stream_ptr]);
472 height = AV_RB16(&s->buf[stream_ptr]);
474 if (height > s->avctx->height - start_line)
478 height = s->avctx->height;
480 row_ptr = s->frame.linesize[0] * start_line;
482 switch (avctx->bits_per_coded_sample) {
485 qtrle_decode_1bpp(s, stream_ptr, row_ptr, height);
490 qtrle_decode_2n4bpp(s, stream_ptr, row_ptr, height, 2);
496 qtrle_decode_2n4bpp(s, stream_ptr, row_ptr, height, 4);
502 qtrle_decode_8bpp(s, stream_ptr, row_ptr, height);
507 qtrle_decode_16bpp(s, stream_ptr, row_ptr, height);
511 qtrle_decode_24bpp(s, stream_ptr, row_ptr, height);
515 qtrle_decode_32bpp(s, stream_ptr, row_ptr, height);
519 av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
520 avctx->bits_per_coded_sample);
525 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
528 s->frame.palette_has_changed = 1;
529 memcpy(s->pal, pal, AVPALETTE_SIZE);
532 /* make the palette available on the way out */
533 memcpy(s->frame.data[1], s->pal, AVPALETTE_SIZE);
537 *data_size = sizeof(AVFrame);
538 *(AVFrame*)data = s->frame;
540 /* always report that the buffer was completely consumed */
544 static av_cold int qtrle_decode_end(AVCodecContext *avctx)
546 QtrleContext *s = avctx->priv_data;
548 if (s->frame.data[0])
549 avctx->release_buffer(avctx, &s->frame);
554 AVCodec ff_qtrle_decoder = {
556 .type = AVMEDIA_TYPE_VIDEO,
557 .id = CODEC_ID_QTRLE,
558 .priv_data_size = sizeof(QtrleContext),
559 .init = qtrle_decode_init,
560 .close = qtrle_decode_end,
561 .decode = qtrle_decode_frame,
562 .capabilities = CODEC_CAP_DR1,
563 .long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),