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.
39 #include "bytestream.h"
41 typedef struct QtrleContext {
42 AVCodecContext *avctx;
49 #define CHECK_PIXEL_PTR(n) \
50 if ((pixel_ptr + n > pixel_limit) || (pixel_ptr + n < 0)) { \
51 av_log (s->avctx, AV_LOG_INFO, "Problem: pixel_ptr = %d, pixel_limit = %d\n", \
52 pixel_ptr + n, pixel_limit); \
56 static void qtrle_decode_1bpp(QtrleContext *s, int row_ptr, int lines_to_change)
60 int row_inc = s->frame.linesize[0];
61 unsigned char pi0, pi1; /* 2 8-pixel values */
62 unsigned char *rgb = s->frame.data[0];
63 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
65 /* skip & 0x80 appears to mean 'start a new line', which can be interpreted
66 * as 'go to next line' during the decoding of a frame but is 'go to first
67 * line' at the beginning. Since we always interpret it as 'go to next line'
68 * in the decoding loop (which makes code simpler/faster), the first line
69 * would not be counted, so we count one more.
70 * See: https://ffmpeg.org/trac/ffmpeg/ticket/226
71 * In the following decoding loop, row_ptr will be the position of the
77 while (lines_to_change) {
78 skip = bytestream2_get_byte(&s->g);
79 rle_code = (signed char)bytestream2_get_byte(&s->g);
85 pixel_ptr = row_ptr + 2 * (skip & 0x7f);
87 pixel_ptr += 2 * skip;
88 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
94 /* decode the run length code */
96 /* get the next 2 bytes from the stream, treat them as groups
97 * of 8 pixels, and output them rle_code times */
99 pi0 = bytestream2_get_byte(&s->g);
100 pi1 = bytestream2_get_byte(&s->g);
101 CHECK_PIXEL_PTR(rle_code * 2);
104 rgb[pixel_ptr++] = pi0;
105 rgb[pixel_ptr++] = pi1;
108 /* copy the same pixel directly to output 2 times */
110 CHECK_PIXEL_PTR(rle_code);
113 rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
118 static inline void qtrle_decode_2n4bpp(QtrleContext *s, int row_ptr,
119 int lines_to_change, int bpp)
123 int row_inc = s->frame.linesize[0];
124 unsigned char pi[16]; /* 16 palette indices */
125 unsigned char *rgb = s->frame.data[0];
126 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
127 int num_pixels = (bpp == 4) ? 8 : 16;
129 while (lines_to_change--) {
130 pixel_ptr = row_ptr + (num_pixels * (bytestream2_get_byte(&s->g) - 1));
131 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
133 while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
135 /* there's another skip code in the stream */
136 pixel_ptr += (num_pixels * (bytestream2_get_byte(&s->g) - 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 */
143 for (i = num_pixels-1; i >= 0; i--) {
144 pi[num_pixels-1-i] = (bytestream2_peek_byte(&s->g) >> ((i*bpp) & 0x07)) & ((1<<bpp)-1);
145 bytestream2_skip(&s->g, ((i & ((num_pixels>>2)-1)) == 0));
147 CHECK_PIXEL_PTR(rle_code * num_pixels);
149 for (i = 0; i < num_pixels; i++)
150 rgb[pixel_ptr++] = pi[i];
153 /* copy the same pixel directly to output 4 times */
155 CHECK_PIXEL_PTR(rle_code*(num_pixels>>2));
158 int x = bytestream2_get_byte(&s->g);
159 rgb[pixel_ptr++] = (x >> 4) & 0x0f;
160 rgb[pixel_ptr++] = x & 0x0f;
162 int x = bytestream2_get_byte(&s->g);
163 rgb[pixel_ptr++] = (x >> 6) & 0x03;
164 rgb[pixel_ptr++] = (x >> 4) & 0x03;
165 rgb[pixel_ptr++] = (x >> 2) & 0x03;
166 rgb[pixel_ptr++] = x & 0x03;
175 static void qtrle_decode_8bpp(QtrleContext *s, 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--) {
185 pixel_ptr = row_ptr + (4 * (bytestream2_get_byte(&s->g) - 1));
186 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
188 while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
190 /* there's another skip code in the stream */
191 pixel_ptr += (4 * (bytestream2_get_byte(&s->g) - 1));
192 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
193 } else if (rle_code < 0) {
194 /* decode the run length code */
195 rle_code = -rle_code;
196 /* get the next 4 bytes from the stream, treat them as palette
197 * indexes, and output them rle_code times */
198 pi1 = bytestream2_get_byte(&s->g);
199 pi2 = bytestream2_get_byte(&s->g);
200 pi3 = bytestream2_get_byte(&s->g);
201 pi4 = bytestream2_get_byte(&s->g);
203 CHECK_PIXEL_PTR(rle_code * 4);
206 rgb[pixel_ptr++] = pi1;
207 rgb[pixel_ptr++] = pi2;
208 rgb[pixel_ptr++] = pi3;
209 rgb[pixel_ptr++] = pi4;
212 /* copy the same pixel directly to output 4 times */
214 CHECK_PIXEL_PTR(rle_code);
217 rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
225 static void qtrle_decode_16bpp(QtrleContext *s, int row_ptr, int lines_to_change)
229 int row_inc = s->frame.linesize[0];
230 unsigned short rgb16;
231 unsigned char *rgb = s->frame.data[0];
232 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
234 while (lines_to_change--) {
235 pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 2;
236 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
238 while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
240 /* there's another skip code in the stream */
241 pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 2;
242 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
243 } else if (rle_code < 0) {
244 /* decode the run length code */
245 rle_code = -rle_code;
246 rgb16 = bytestream2_get_be16(&s->g);
248 CHECK_PIXEL_PTR(rle_code * 2);
251 *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
255 CHECK_PIXEL_PTR(rle_code * 2);
257 /* copy pixels directly to output */
259 rgb16 = bytestream2_get_be16(&s->g);
260 *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
269 static void qtrle_decode_24bpp(QtrleContext *s, int row_ptr, int lines_to_change)
273 int row_inc = s->frame.linesize[0];
274 unsigned char r, g, b;
275 unsigned char *rgb = s->frame.data[0];
276 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
278 while (lines_to_change--) {
279 pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 3;
280 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
282 while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
284 /* there's another skip code in the stream */
285 pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 3;
286 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
287 } else if (rle_code < 0) {
288 /* decode the run length code */
289 rle_code = -rle_code;
290 r = bytestream2_get_byte(&s->g);
291 g = bytestream2_get_byte(&s->g);
292 b = bytestream2_get_byte(&s->g);
294 CHECK_PIXEL_PTR(rle_code * 3);
297 rgb[pixel_ptr++] = r;
298 rgb[pixel_ptr++] = g;
299 rgb[pixel_ptr++] = b;
302 CHECK_PIXEL_PTR(rle_code * 3);
304 /* copy pixels directly to output */
306 rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
307 rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
308 rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
316 static void qtrle_decode_32bpp(QtrleContext *s, int row_ptr, int lines_to_change)
320 int row_inc = s->frame.linesize[0];
322 unsigned char *rgb = s->frame.data[0];
323 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
325 while (lines_to_change--) {
326 pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 4;
327 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
329 while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
331 /* there's another skip code in the stream */
332 pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 4;
333 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
334 } else if (rle_code < 0) {
335 /* decode the run length code */
336 rle_code = -rle_code;
337 argb = bytestream2_get_be32(&s->g);
339 CHECK_PIXEL_PTR(rle_code * 4);
342 AV_WN32A(rgb + pixel_ptr, argb);
346 CHECK_PIXEL_PTR(rle_code * 4);
348 /* copy pixels directly to output */
350 argb = bytestream2_get_be32(&s->g);
351 AV_WN32A(rgb + pixel_ptr, argb);
360 static av_cold int qtrle_decode_init(AVCodecContext *avctx)
362 QtrleContext *s = avctx->priv_data;
365 switch (avctx->bits_per_coded_sample) {
368 avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
377 avctx->pix_fmt = AV_PIX_FMT_PAL8;
381 avctx->pix_fmt = AV_PIX_FMT_RGB555;
385 avctx->pix_fmt = AV_PIX_FMT_RGB24;
389 avctx->pix_fmt = AV_PIX_FMT_RGB32;
393 av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
394 avctx->bits_per_coded_sample);
395 return AVERROR_INVALIDDATA;
398 avcodec_get_frame_defaults(&s->frame);
399 s->frame.data[0] = NULL;
404 static int qtrle_decode_frame(AVCodecContext *avctx,
405 void *data, int *got_frame,
408 QtrleContext *s = avctx->priv_data;
409 int header, start_line;
414 bytestream2_init(&s->g, avpkt->data, avpkt->size);
415 s->frame.reference = 3;
416 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
417 FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
418 if ((ret = avctx->reget_buffer(avctx, &s->frame)) < 0) {
419 av_log (s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
423 /* check if this frame is even supposed to change */
427 /* start after the chunk size */
428 bytestream2_seek(&s->g, 4, SEEK_SET);
430 /* fetch the header */
431 header = bytestream2_get_be16(&s->g);
433 /* if a header is present, fetch additional decoding parameters */
434 if (header & 0x0008) {
435 if (avpkt->size < 14)
437 start_line = bytestream2_get_be16(&s->g);
438 bytestream2_skip(&s->g, 2);
439 height = bytestream2_get_be16(&s->g);
440 bytestream2_skip(&s->g, 2);
441 if (height > s->avctx->height - start_line)
445 height = s->avctx->height;
447 row_ptr = s->frame.linesize[0] * start_line;
449 switch (avctx->bits_per_coded_sample) {
452 qtrle_decode_1bpp(s, row_ptr, height);
457 qtrle_decode_2n4bpp(s, row_ptr, height, 2);
463 qtrle_decode_2n4bpp(s, row_ptr, height, 4);
469 qtrle_decode_8bpp(s, row_ptr, height);
474 qtrle_decode_16bpp(s, row_ptr, height);
478 qtrle_decode_24bpp(s, row_ptr, height);
482 qtrle_decode_32bpp(s, row_ptr, height);
486 av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
487 avctx->bits_per_coded_sample);
492 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
495 s->frame.palette_has_changed = 1;
496 memcpy(s->pal, pal, AVPALETTE_SIZE);
499 /* make the palette available on the way out */
500 memcpy(s->frame.data[1], s->pal, AVPALETTE_SIZE);
505 *(AVFrame*)data = s->frame;
507 /* always report that the buffer was completely consumed */
511 static av_cold int qtrle_decode_end(AVCodecContext *avctx)
513 QtrleContext *s = avctx->priv_data;
515 if (s->frame.data[0])
516 avctx->release_buffer(avctx, &s->frame);
521 AVCodec ff_qtrle_decoder = {
523 .type = AVMEDIA_TYPE_VIDEO,
524 .id = AV_CODEC_ID_QTRLE,
525 .priv_data_size = sizeof(QtrleContext),
526 .init = qtrle_decode_init,
527 .close = qtrle_decode_end,
528 .decode = qtrle_decode_frame,
529 .capabilities = CODEC_CAP_DR1,
530 .long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),