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.
41 typedef struct QtrleContext
{
43 AVCodecContext
*avctx
;
46 const unsigned char *buf
;
51 #define CHECK_STREAM_PTR(n) \
52 if ((stream_ptr + n) > s->size) { \
53 av_log (s->avctx, AV_LOG_INFO, "Problem: stream_ptr out of bounds (%d >= %d)\n", \
54 stream_ptr + n, s->size); \
58 #define CHECK_PIXEL_PTR(n) \
59 if ((pixel_ptr + n > pixel_limit) || (pixel_ptr + n < 0)) { \
60 av_log (s->avctx, AV_LOG_INFO, "Problem: pixel_ptr = %d, pixel_limit = %d\n", \
61 pixel_ptr + n, pixel_limit); \
65 static void qtrle_decode_1bpp(QtrleContext *s)
69 static void qtrle_decode_2bpp(QtrleContext
*s
)
73 static void qtrle_decode_4bpp(QtrleContext
*s
)
80 int row_ptr
, pixel_ptr
;
81 int row_inc
= s
->frame
.linesize
[0];
82 unsigned char pi1
, pi2
, pi3
, pi4
, pi5
, pi6
, pi7
, pi8
; /* 8 palette indexes */
83 unsigned char *rgb
= s
->frame
.data
[0];
84 int pixel_limit
= s
->frame
.linesize
[0] * s
->avctx
->height
;
86 /* check if this frame is even supposed to change */
90 /* start after the chunk size */
93 /* fetch the header */
95 header
= AV_RB16(&s
->buf
[stream_ptr
]);
98 /* if a header is present, fetch additional decoding parameters */
99 if (header
& 0x0008) {
101 start_line
= AV_RB16(&s
->buf
[stream_ptr
]);
103 lines_to_change
= AV_RB16(&s
->buf
[stream_ptr
]);
107 lines_to_change
= s
->avctx
->height
;
110 row_ptr
= row_inc
* start_line
;
111 while (lines_to_change
--) {
113 pixel_ptr
= row_ptr
+ (8 * (s
->buf
[stream_ptr
++] - 1));
115 while ((rle_code
= (signed char)s
->buf
[stream_ptr
++]) != -1) {
117 /* there's another skip code in the stream */
119 pixel_ptr
+= (8 * (s
->buf
[stream_ptr
++] - 1));
120 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
121 } else if (rle_code
< 0) {
122 /* decode the run length code */
123 rle_code
= -rle_code
;
124 /* get the next 4 bytes from the stream, treat them as palette
125 * indexes, and output them rle_code times */
127 pi1
= ((s
->buf
[stream_ptr
]) >> 4) & 0x0f;
128 pi2
= (s
->buf
[stream_ptr
++]) & 0x0f;
129 pi3
= ((s
->buf
[stream_ptr
]) >> 4) & 0x0f;
130 pi4
= (s
->buf
[stream_ptr
++]) & 0x0f;
131 pi5
= ((s
->buf
[stream_ptr
]) >> 4) & 0x0f;
132 pi6
= (s
->buf
[stream_ptr
++]) & 0x0f;
133 pi7
= ((s
->buf
[stream_ptr
]) >> 4) & 0x0f;
134 pi8
= (s
->buf
[stream_ptr
++]) & 0x0f;
136 CHECK_PIXEL_PTR(rle_code
* 8);
139 rgb
[pixel_ptr
++] = pi1
;
140 rgb
[pixel_ptr
++] = pi2
;
141 rgb
[pixel_ptr
++] = pi3
;
142 rgb
[pixel_ptr
++] = pi4
;
143 rgb
[pixel_ptr
++] = pi5
;
144 rgb
[pixel_ptr
++] = pi6
;
145 rgb
[pixel_ptr
++] = pi7
;
146 rgb
[pixel_ptr
++] = pi8
;
149 /* copy the same pixel directly to output 4 times */
151 CHECK_STREAM_PTR(rle_code
);
152 CHECK_PIXEL_PTR(rle_code
*2);
155 rgb
[pixel_ptr
++] = ((s
->buf
[stream_ptr
]) >> 4) & 0x0f;
156 rgb
[pixel_ptr
++] = (s
->buf
[stream_ptr
++]) & 0x0f;
164 static void qtrle_decode_8bpp(QtrleContext
*s
)
171 int row_ptr
, pixel_ptr
;
172 int row_inc
= s
->frame
.linesize
[0];
173 unsigned char pi1
, pi2
, pi3
, pi4
; /* 4 palette indexes */
174 unsigned char *rgb
= s
->frame
.data
[0];
175 int pixel_limit
= s
->frame
.linesize
[0] * s
->avctx
->height
;
177 /* check if this frame is even supposed to change */
181 /* start after the chunk size */
184 /* fetch the header */
186 header
= AV_RB16(&s
->buf
[stream_ptr
]);
189 /* if a header is present, fetch additional decoding parameters */
190 if (header
& 0x0008) {
192 start_line
= AV_RB16(&s
->buf
[stream_ptr
]);
194 lines_to_change
= AV_RB16(&s
->buf
[stream_ptr
]);
198 lines_to_change
= s
->avctx
->height
;
201 row_ptr
= row_inc
* start_line
;
202 while (lines_to_change
--) {
204 pixel_ptr
= row_ptr
+ (4 * (s
->buf
[stream_ptr
++] - 1));
206 while ((rle_code
= (signed char)s
->buf
[stream_ptr
++]) != -1) {
208 /* there's another skip code in the stream */
210 pixel_ptr
+= (4 * (s
->buf
[stream_ptr
++] - 1));
211 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
212 } else if (rle_code
< 0) {
213 /* decode the run length code */
214 rle_code
= -rle_code
;
215 /* get the next 4 bytes from the stream, treat them as palette
216 * indexes, and output them rle_code times */
218 pi1
= s
->buf
[stream_ptr
++];
219 pi2
= s
->buf
[stream_ptr
++];
220 pi3
= s
->buf
[stream_ptr
++];
221 pi4
= s
->buf
[stream_ptr
++];
223 CHECK_PIXEL_PTR(rle_code
* 4);
226 rgb
[pixel_ptr
++] = pi1
;
227 rgb
[pixel_ptr
++] = pi2
;
228 rgb
[pixel_ptr
++] = pi3
;
229 rgb
[pixel_ptr
++] = pi4
;
232 /* copy the same pixel directly to output 4 times */
234 CHECK_STREAM_PTR(rle_code
);
235 CHECK_PIXEL_PTR(rle_code
);
238 rgb
[pixel_ptr
++] = s
->buf
[stream_ptr
++];
246 static void qtrle_decode_16bpp(QtrleContext
*s
)
253 int row_ptr
, pixel_ptr
;
254 int row_inc
= s
->frame
.linesize
[0];
255 unsigned short rgb16
;
256 unsigned char *rgb
= s
->frame
.data
[0];
257 int pixel_limit
= s
->frame
.linesize
[0] * s
->avctx
->height
;
259 /* check if this frame is even supposed to change */
263 /* start after the chunk size */
266 /* fetch the header */
268 header
= AV_RB16(&s
->buf
[stream_ptr
]);
271 /* if a header is present, fetch additional decoding parameters */
272 if (header
& 0x0008) {
274 start_line
= AV_RB16(&s
->buf
[stream_ptr
]);
276 lines_to_change
= AV_RB16(&s
->buf
[stream_ptr
]);
280 lines_to_change
= s
->avctx
->height
;
283 row_ptr
= row_inc
* start_line
;
284 while (lines_to_change
--) {
286 pixel_ptr
= row_ptr
+ (s
->buf
[stream_ptr
++] - 1) * 2;
288 while ((rle_code
= (signed char)s
->buf
[stream_ptr
++]) != -1) {
290 /* there's another skip code in the stream */
292 pixel_ptr
+= (s
->buf
[stream_ptr
++] - 1) * 2;
293 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
294 } else if (rle_code
< 0) {
295 /* decode the run length code */
296 rle_code
= -rle_code
;
298 rgb16
= AV_RB16(&s
->buf
[stream_ptr
]);
301 CHECK_PIXEL_PTR(rle_code
* 2);
304 *(unsigned short *)(&rgb
[pixel_ptr
]) = rgb16
;
308 CHECK_STREAM_PTR(rle_code
* 2);
309 CHECK_PIXEL_PTR(rle_code
* 2);
311 /* copy pixels directly to output */
313 rgb16
= AV_RB16(&s
->buf
[stream_ptr
]);
315 *(unsigned short *)(&rgb
[pixel_ptr
]) = rgb16
;
324 static void qtrle_decode_24bpp(QtrleContext
*s
)
331 int row_ptr
, pixel_ptr
;
332 int row_inc
= s
->frame
.linesize
[0];
333 unsigned char r
, g
, b
;
334 unsigned char *rgb
= s
->frame
.data
[0];
335 int pixel_limit
= s
->frame
.linesize
[0] * s
->avctx
->height
;
337 /* check if this frame is even supposed to change */
341 /* start after the chunk size */
344 /* fetch the header */
346 header
= AV_RB16(&s
->buf
[stream_ptr
]);
349 /* if a header is present, fetch additional decoding parameters */
350 if (header
& 0x0008) {
352 start_line
= AV_RB16(&s
->buf
[stream_ptr
]);
354 lines_to_change
= AV_RB16(&s
->buf
[stream_ptr
]);
358 lines_to_change
= s
->avctx
->height
;
361 row_ptr
= row_inc
* start_line
;
362 while (lines_to_change
--) {
364 pixel_ptr
= row_ptr
+ (s
->buf
[stream_ptr
++] - 1) * 3;
366 while ((rle_code
= (signed char)s
->buf
[stream_ptr
++]) != -1) {
368 /* there's another skip code in the stream */
370 pixel_ptr
+= (s
->buf
[stream_ptr
++] - 1) * 3;
371 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
372 } else if (rle_code
< 0) {
373 /* decode the run length code */
374 rle_code
= -rle_code
;
376 r
= s
->buf
[stream_ptr
++];
377 g
= s
->buf
[stream_ptr
++];
378 b
= s
->buf
[stream_ptr
++];
380 CHECK_PIXEL_PTR(rle_code
* 3);
383 rgb
[pixel_ptr
++] = r
;
384 rgb
[pixel_ptr
++] = g
;
385 rgb
[pixel_ptr
++] = b
;
388 CHECK_STREAM_PTR(rle_code
* 3);
389 CHECK_PIXEL_PTR(rle_code
* 3);
391 /* copy pixels directly to output */
393 rgb
[pixel_ptr
++] = s
->buf
[stream_ptr
++];
394 rgb
[pixel_ptr
++] = s
->buf
[stream_ptr
++];
395 rgb
[pixel_ptr
++] = s
->buf
[stream_ptr
++];
403 static void qtrle_decode_32bpp(QtrleContext
*s
)
410 int row_ptr
, pixel_ptr
;
411 int row_inc
= s
->frame
.linesize
[0];
412 unsigned char a
, r
, g
, b
;
414 unsigned char *rgb
= s
->frame
.data
[0];
415 int pixel_limit
= s
->frame
.linesize
[0] * s
->avctx
->height
;
417 /* check if this frame is even supposed to change */
421 /* start after the chunk size */
424 /* fetch the header */
426 header
= AV_RB16(&s
->buf
[stream_ptr
]);
429 /* if a header is present, fetch additional decoding parameters */
430 if (header
& 0x0008) {
432 start_line
= AV_RB16(&s
->buf
[stream_ptr
]);
434 lines_to_change
= AV_RB16(&s
->buf
[stream_ptr
]);
438 lines_to_change
= s
->avctx
->height
;
441 row_ptr
= row_inc
* start_line
;
442 while (lines_to_change
--) {
444 pixel_ptr
= row_ptr
+ (s
->buf
[stream_ptr
++] - 1) * 4;
446 while ((rle_code
= (signed char)s
->buf
[stream_ptr
++]) != -1) {
448 /* there's another skip code in the stream */
450 pixel_ptr
+= (s
->buf
[stream_ptr
++] - 1) * 4;
451 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
452 } else if (rle_code
< 0) {
453 /* decode the run length code */
454 rle_code
= -rle_code
;
456 a
= s
->buf
[stream_ptr
++];
457 r
= s
->buf
[stream_ptr
++];
458 g
= s
->buf
[stream_ptr
++];
459 b
= s
->buf
[stream_ptr
++];
460 argb
= (a
<< 24) | (r
<< 16) | (g
<< 8) | (b
<< 0);
462 CHECK_PIXEL_PTR(rle_code
* 4);
465 *(unsigned int *)(&rgb
[pixel_ptr
]) = argb
;
469 CHECK_STREAM_PTR(rle_code
* 4);
470 CHECK_PIXEL_PTR(rle_code
* 4);
472 /* copy pixels directly to output */
474 a
= s
->buf
[stream_ptr
++];
475 r
= s
->buf
[stream_ptr
++];
476 g
= s
->buf
[stream_ptr
++];
477 b
= s
->buf
[stream_ptr
++];
478 argb
= (a
<< 24) | (r
<< 16) | (g
<< 8) | (b
<< 0);
479 *(unsigned int *)(&rgb
[pixel_ptr
]) = argb
;
488 static av_cold
int qtrle_decode_init(AVCodecContext
*avctx
)
490 QtrleContext
*s
= avctx
->priv_data
;
493 switch (avctx
->bits_per_sample
) {
502 avctx
->pix_fmt
= PIX_FMT_PAL8
;
506 avctx
->pix_fmt
= PIX_FMT_RGB555
;
510 avctx
->pix_fmt
= PIX_FMT_RGB24
;
514 avctx
->pix_fmt
= PIX_FMT_RGB32
;
518 av_log (avctx
, AV_LOG_ERROR
, "Unsupported colorspace: %d bits/sample?\n",
519 avctx
->bits_per_sample
);
523 s
->frame
.data
[0] = NULL
;
528 static int qtrle_decode_frame(AVCodecContext
*avctx
,
529 void *data
, int *data_size
,
530 const uint8_t *buf
, int buf_size
)
532 QtrleContext
*s
= avctx
->priv_data
;
537 s
->frame
.reference
= 1;
538 s
->frame
.buffer_hints
= FF_BUFFER_HINTS_VALID
| FF_BUFFER_HINTS_PRESERVE
|
539 FF_BUFFER_HINTS_REUSABLE
| FF_BUFFER_HINTS_READABLE
;
540 if (avctx
->reget_buffer(avctx
, &s
->frame
)) {
541 av_log (s
->avctx
, AV_LOG_ERROR
, "reget_buffer() failed\n");
545 switch (avctx
->bits_per_sample
) {
548 qtrle_decode_1bpp(s
);
553 qtrle_decode_2bpp(s
);
558 qtrle_decode_4bpp(s
);
559 /* make the palette available on the way out */
560 memcpy(s
->frame
.data
[1], s
->avctx
->palctrl
->palette
, AVPALETTE_SIZE
);
561 if (s
->avctx
->palctrl
->palette_changed
) {
562 s
->frame
.palette_has_changed
= 1;
563 s
->avctx
->palctrl
->palette_changed
= 0;
569 qtrle_decode_8bpp(s
);
570 /* make the palette available on the way out */
571 memcpy(s
->frame
.data
[1], s
->avctx
->palctrl
->palette
, AVPALETTE_SIZE
);
572 if (s
->avctx
->palctrl
->palette_changed
) {
573 s
->frame
.palette_has_changed
= 1;
574 s
->avctx
->palctrl
->palette_changed
= 0;
579 qtrle_decode_16bpp(s
);
583 qtrle_decode_24bpp(s
);
587 qtrle_decode_32bpp(s
);
591 av_log (s
->avctx
, AV_LOG_ERROR
, "Unsupported colorspace: %d bits/sample?\n",
592 avctx
->bits_per_sample
);
596 *data_size
= sizeof(AVFrame
);
597 *(AVFrame
*)data
= s
->frame
;
599 /* always report that the buffer was completely consumed */
603 static av_cold
int qtrle_decode_end(AVCodecContext
*avctx
)
605 QtrleContext
*s
= avctx
->priv_data
;
607 if (s
->frame
.data
[0])
608 avctx
->release_buffer(avctx
, &s
->frame
);
613 AVCodec qtrle_decoder
= {
617 sizeof(QtrleContext
),
623 .long_name
= "QuickTime Animation (RLE) video",