2
* Quicktime Animation (RLE) Video Decoder
3
* Copyright (C) 2004 the ffmpeg project
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public
7
* License as published by the Free Software Foundation; either
8
* version 2 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
* QT RLE Video Decoder by Mike Melanson (melanson@pcisys.net)
24
* For more information about the QT RLE format, visit:
25
* http://www.pcisys.net/~melanson/codecs/
27
* The QT RLE decoder has seven modes of operation:
28
* 1, 2, 4, 8, 16, 24, and 32 bits per pixel. For modes 1, 2, 4, and 8
29
* the decoder outputs PAL8 colorspace data. 16-bit data yields RGB555
30
* data. 24-bit data is RGB24 and 32-bit data is RGBA32.
42
typedef struct QtrleContext {
44
AVCodecContext *avctx;
53
#define CHECK_STREAM_PTR(n) \
54
if ((stream_ptr + n) > s->size) { \
55
av_log (s->avctx, AV_LOG_INFO, "Problem: stream_ptr out of bounds (%d >= %d)\n", \
56
stream_ptr + n, s->size); \
60
#define CHECK_PIXEL_PTR(n) \
61
if ((pixel_ptr + n > pixel_limit) || (pixel_ptr + n < 0)) { \
62
av_log (s->avctx, AV_LOG_INFO, "Problem: pixel_ptr = %d, pixel_limit = %d\n", \
63
pixel_ptr + n, pixel_limit); \
67
static void qtrle_decode_1bpp(QtrleContext *s)
71
static void qtrle_decode_2bpp(QtrleContext *s)
75
static void qtrle_decode_4bpp(QtrleContext *s)
82
int row_ptr, pixel_ptr;
83
int row_inc = s->frame.linesize[0];
84
unsigned char pi1, pi2, pi3, pi4, pi5, pi6, pi7, pi8; /* 8 palette indices */
85
unsigned char *rgb = s->frame.data[0];
86
int pixel_limit = s->frame.linesize[0] * s->avctx->height;
88
/* check if this frame is even supposed to change */
92
/* start after the chunk size */
95
/* fetch the header */
97
header = BE_16(&s->buf[stream_ptr]);
100
/* if a header is present, fetch additional decoding parameters */
101
if (header & 0x0008) {
103
start_line = BE_16(&s->buf[stream_ptr]);
105
lines_to_change = BE_16(&s->buf[stream_ptr]);
109
lines_to_change = s->avctx->height;
112
row_ptr = row_inc * start_line;
113
while (lines_to_change--) {
115
pixel_ptr = row_ptr + (8 * (s->buf[stream_ptr++] - 1));
117
while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
119
/* there's another skip code in the stream */
121
pixel_ptr += (8 * (s->buf[stream_ptr++] - 1));
122
CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
123
} else if (rle_code < 0) {
124
/* decode the run length code */
125
rle_code = -rle_code;
126
/* get the next 4 bytes from the stream, treat them as palette
127
* indices, and output them rle_code times */
129
pi1 = ((s->buf[stream_ptr]) >> 4) & 0x0f;
130
pi2 = (s->buf[stream_ptr++]) & 0x0f;
131
pi3 = ((s->buf[stream_ptr]) >> 4) & 0x0f;
132
pi4 = (s->buf[stream_ptr++]) & 0x0f;
133
pi5 = ((s->buf[stream_ptr]) >> 4) & 0x0f;
134
pi6 = (s->buf[stream_ptr++]) & 0x0f;
135
pi7 = ((s->buf[stream_ptr]) >> 4) & 0x0f;
136
pi8 = (s->buf[stream_ptr++]) & 0x0f;
138
CHECK_PIXEL_PTR(rle_code * 8);
141
rgb[pixel_ptr++] = pi1;
142
rgb[pixel_ptr++] = pi2;
143
rgb[pixel_ptr++] = pi3;
144
rgb[pixel_ptr++] = pi4;
145
rgb[pixel_ptr++] = pi5;
146
rgb[pixel_ptr++] = pi6;
147
rgb[pixel_ptr++] = pi7;
148
rgb[pixel_ptr++] = pi8;
151
/* copy the same pixel directly to output 4 times */
153
CHECK_STREAM_PTR(rle_code);
154
CHECK_PIXEL_PTR(rle_code*2);
157
rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 4) & 0x0f;
158
rgb[pixel_ptr++] = (s->buf[stream_ptr++]) & 0x0f;
166
static void qtrle_decode_8bpp(QtrleContext *s)
173
int row_ptr, pixel_ptr;
174
int row_inc = s->frame.linesize[0];
175
unsigned char pi1, pi2, pi3, pi4; /* 4 palette indices */
176
unsigned char *rgb = s->frame.data[0];
177
int pixel_limit = s->frame.linesize[0] * s->avctx->height;
179
/* check if this frame is even supposed to change */
183
/* start after the chunk size */
186
/* fetch the header */
188
header = BE_16(&s->buf[stream_ptr]);
191
/* if a header is present, fetch additional decoding parameters */
192
if (header & 0x0008) {
194
start_line = BE_16(&s->buf[stream_ptr]);
196
lines_to_change = BE_16(&s->buf[stream_ptr]);
200
lines_to_change = s->avctx->height;
203
row_ptr = row_inc * start_line;
204
while (lines_to_change--) {
206
pixel_ptr = row_ptr + (4 * (s->buf[stream_ptr++] - 1));
208
while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
210
/* there's another skip code in the stream */
212
pixel_ptr += (4 * (s->buf[stream_ptr++] - 1));
213
CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
214
} else if (rle_code < 0) {
215
/* decode the run length code */
216
rle_code = -rle_code;
217
/* get the next 4 bytes from the stream, treat them as palette
218
* indices, and output them rle_code times */
220
pi1 = s->buf[stream_ptr++];
221
pi2 = s->buf[stream_ptr++];
222
pi3 = s->buf[stream_ptr++];
223
pi4 = s->buf[stream_ptr++];
225
CHECK_PIXEL_PTR(rle_code * 4);
228
rgb[pixel_ptr++] = pi1;
229
rgb[pixel_ptr++] = pi2;
230
rgb[pixel_ptr++] = pi3;
231
rgb[pixel_ptr++] = pi4;
234
/* copy the same pixel directly to output 4 times */
236
CHECK_STREAM_PTR(rle_code);
237
CHECK_PIXEL_PTR(rle_code);
240
rgb[pixel_ptr++] = s->buf[stream_ptr++];
248
static void qtrle_decode_16bpp(QtrleContext *s)
255
int row_ptr, pixel_ptr;
256
int row_inc = s->frame.linesize[0];
257
unsigned short rgb16;
258
unsigned char *rgb = s->frame.data[0];
259
int pixel_limit = s->frame.linesize[0] * s->avctx->height;
261
/* check if this frame is even supposed to change */
265
/* start after the chunk size */
268
/* fetch the header */
270
header = BE_16(&s->buf[stream_ptr]);
273
/* if a header is present, fetch additional decoding parameters */
274
if (header & 0x0008) {
276
start_line = BE_16(&s->buf[stream_ptr]);
278
lines_to_change = BE_16(&s->buf[stream_ptr]);
282
lines_to_change = s->avctx->height;
285
row_ptr = row_inc * start_line;
286
while (lines_to_change--) {
288
pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 2;
290
while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
292
/* there's another skip code in the stream */
294
pixel_ptr += (s->buf[stream_ptr++] - 1) * 2;
295
CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
296
} else if (rle_code < 0) {
297
/* decode the run length code */
298
rle_code = -rle_code;
300
rgb16 = BE_16(&s->buf[stream_ptr]);
303
CHECK_PIXEL_PTR(rle_code * 2);
306
*(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
310
CHECK_STREAM_PTR(rle_code * 2);
311
CHECK_PIXEL_PTR(rle_code * 2);
313
/* copy pixels directly to output */
315
rgb16 = BE_16(&s->buf[stream_ptr]);
317
*(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
326
static void qtrle_decode_24bpp(QtrleContext *s)
333
int row_ptr, pixel_ptr;
334
int row_inc = s->frame.linesize[0];
335
unsigned char r, g, b;
336
unsigned char *rgb = s->frame.data[0];
337
int pixel_limit = s->frame.linesize[0] * s->avctx->height;
339
/* check if this frame is even supposed to change */
343
/* start after the chunk size */
346
/* fetch the header */
348
header = BE_16(&s->buf[stream_ptr]);
351
/* if a header is present, fetch additional decoding parameters */
352
if (header & 0x0008) {
354
start_line = BE_16(&s->buf[stream_ptr]);
356
lines_to_change = BE_16(&s->buf[stream_ptr]);
360
lines_to_change = s->avctx->height;
363
row_ptr = row_inc * start_line;
364
while (lines_to_change--) {
366
pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 3;
368
while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
370
/* there's another skip code in the stream */
372
pixel_ptr += (s->buf[stream_ptr++] - 1) * 3;
373
CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
374
} else if (rle_code < 0) {
375
/* decode the run length code */
376
rle_code = -rle_code;
378
r = s->buf[stream_ptr++];
379
g = s->buf[stream_ptr++];
380
b = s->buf[stream_ptr++];
382
CHECK_PIXEL_PTR(rle_code * 3);
385
rgb[pixel_ptr++] = r;
386
rgb[pixel_ptr++] = g;
387
rgb[pixel_ptr++] = b;
390
CHECK_STREAM_PTR(rle_code * 3);
391
CHECK_PIXEL_PTR(rle_code * 3);
393
/* copy pixels directly to output */
395
rgb[pixel_ptr++] = s->buf[stream_ptr++];
396
rgb[pixel_ptr++] = s->buf[stream_ptr++];
397
rgb[pixel_ptr++] = s->buf[stream_ptr++];
405
static void qtrle_decode_32bpp(QtrleContext *s)
412
int row_ptr, pixel_ptr;
413
int row_inc = s->frame.linesize[0];
414
unsigned char a, r, g, b;
416
unsigned char *rgb = s->frame.data[0];
417
int pixel_limit = s->frame.linesize[0] * s->avctx->height;
419
/* check if this frame is even supposed to change */
423
/* start after the chunk size */
426
/* fetch the header */
428
header = BE_16(&s->buf[stream_ptr]);
431
/* if a header is present, fetch additional decoding parameters */
432
if (header & 0x0008) {
434
start_line = BE_16(&s->buf[stream_ptr]);
436
lines_to_change = BE_16(&s->buf[stream_ptr]);
440
lines_to_change = s->avctx->height;
443
row_ptr = row_inc * start_line;
444
while (lines_to_change--) {
446
pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 4;
448
while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
450
/* there's another skip code in the stream */
452
pixel_ptr += (s->buf[stream_ptr++] - 1) * 4;
453
CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
454
} else if (rle_code < 0) {
455
/* decode the run length code */
456
rle_code = -rle_code;
458
a = s->buf[stream_ptr++];
459
r = s->buf[stream_ptr++];
460
g = s->buf[stream_ptr++];
461
b = s->buf[stream_ptr++];
462
argb = (a << 24) | (r << 16) | (g << 8) | (b << 0);
464
CHECK_PIXEL_PTR(rle_code * 4);
467
*(unsigned int *)(&rgb[pixel_ptr]) = argb;
471
CHECK_STREAM_PTR(rle_code * 4);
472
CHECK_PIXEL_PTR(rle_code * 4);
474
/* copy pixels directly to output */
476
a = s->buf[stream_ptr++];
477
r = s->buf[stream_ptr++];
478
g = s->buf[stream_ptr++];
479
b = s->buf[stream_ptr++];
480
argb = (a << 24) | (r << 16) | (g << 8) | (b << 0);
481
*(unsigned int *)(&rgb[pixel_ptr]) = argb;
490
static int qtrle_decode_init(AVCodecContext *avctx)
492
QtrleContext *s = (QtrleContext *)avctx->priv_data;
495
switch (avctx->bits_per_sample) {
504
avctx->pix_fmt = PIX_FMT_PAL8;
508
avctx->pix_fmt = PIX_FMT_RGB555;
512
avctx->pix_fmt = PIX_FMT_RGB24;
516
avctx->pix_fmt = PIX_FMT_RGBA32;
520
av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
521
avctx->bits_per_sample);
524
avctx->has_b_frames = 0;
525
dsputil_init(&s->dsp, avctx);
527
s->frame.data[0] = NULL;
532
static int qtrle_decode_frame(AVCodecContext *avctx,
533
void *data, int *data_size,
534
uint8_t *buf, int buf_size)
536
QtrleContext *s = (QtrleContext *)avctx->priv_data;
541
s->frame.reference = 1;
542
s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
543
FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
544
if (avctx->reget_buffer(avctx, &s->frame)) {
545
av_log (s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
549
switch (avctx->bits_per_sample) {
552
qtrle_decode_1bpp(s);
557
qtrle_decode_2bpp(s);
562
qtrle_decode_4bpp(s);
563
/* make the palette available on the way out */
564
memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
565
if (s->avctx->palctrl->palette_changed) {
566
s->frame.palette_has_changed = 1;
567
s->avctx->palctrl->palette_changed = 0;
573
qtrle_decode_8bpp(s);
574
/* make the palette available on the way out */
575
memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
576
if (s->avctx->palctrl->palette_changed) {
577
s->frame.palette_has_changed = 1;
578
s->avctx->palctrl->palette_changed = 0;
583
qtrle_decode_16bpp(s);
587
qtrle_decode_24bpp(s);
591
qtrle_decode_32bpp(s);
595
av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
596
avctx->bits_per_sample);
600
*data_size = sizeof(AVFrame);
601
*(AVFrame*)data = s->frame;
603
/* always report that the buffer was completely consumed */
607
static int qtrle_decode_end(AVCodecContext *avctx)
609
QtrleContext *s = (QtrleContext *)avctx->priv_data;
611
if (s->frame.data[0])
612
avctx->release_buffer(avctx, &s->frame);
617
AVCodec qtrle_decoder = {
621
sizeof(QtrleContext),