1 /*
2 * Quicktime Animation (RLE) Video Decoder
3 * Copyright (c) 2004 The FFmpeg Project
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 /**
23 * @file
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/
27 *
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.
32 */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37
41
45
49
50 #define CHECK_PIXEL_PTR(n) \
51 if ((pixel_ptr + n > pixel_limit) || (pixel_ptr + n < 0)) { \
52 av_log (s->avctx, AV_LOG_ERROR, "Problem: pixel_ptr = %d, pixel_limit = %d\n",\
53 pixel_ptr + n, pixel_limit); \
54 return; \
55 } \
56
58 {
59 int rle_code;
60 int pixel_ptr;
62 uint8_t pi0, pi1;
/* 2 8-pixel values */
65 int skip;
66 /* skip & 0x80 appears to mean 'start a new line', which can be interpreted
67 * as 'go to next line' during the decoding of a frame but is 'go to first
68 * line' at the beginning. Since we always interpret it as 'go to next line'
69 * in the decoding loop (which makes code simpler/faster), the first line
70 * would not be counted, so we count one more.
71 * See: https://trac.ffmpeg.org/ticket/226
72 * In the following decoding loop, row_ptr will be the position of the
73 * current row. */
74
75 row_ptr -= row_inc;
76 pixel_ptr = row_ptr;
77 lines_to_change++;
78 while (lines_to_change) {
79 skip = bytestream2_get_byte(&s->
g);
80 rle_code = (int8_t)bytestream2_get_byte(&s->
g);
81 if (rle_code == 0)
82 break;
83 if(skip & 0x80) {
84 lines_to_change--;
85 row_ptr += row_inc;
86 pixel_ptr = row_ptr + 2 * 8 * (skip & 0x7f);
87 } else
88 pixel_ptr += 2 * 8 * skip;
90
91 if(rle_code == -1)
92 continue;
93
94 if (rle_code < 0) {
95 /* decode the run length code */
96 rle_code = -rle_code;
97 /* get the next 2 bytes from the stream, treat them as groups
98 * of 8 pixels, and output them rle_code times */
99
100 pi0 = bytestream2_get_byte(&s->
g);
101 pi1 = bytestream2_get_byte(&s->
g);
103
104 while (rle_code--) {
105 rgb[pixel_ptr++] = (pi0 >> 7) & 0x01;
106 rgb[pixel_ptr++] = (pi0 >> 6) & 0x01;
107 rgb[pixel_ptr++] = (pi0 >> 5) & 0x01;
108 rgb[pixel_ptr++] = (pi0 >> 4) & 0x01;
109 rgb[pixel_ptr++] = (pi0 >> 3) & 0x01;
110 rgb[pixel_ptr++] = (pi0 >> 2) & 0x01;
111 rgb[pixel_ptr++] = (pi0 >> 1) & 0x01;
112 rgb[pixel_ptr++] = pi0 & 0x01;
113 rgb[pixel_ptr++] = (pi1 >> 7) & 0x01;
114 rgb[pixel_ptr++] = (pi1 >> 6) & 0x01;
115 rgb[pixel_ptr++] = (pi1 >> 5) & 0x01;
116 rgb[pixel_ptr++] = (pi1 >> 4) & 0x01;
117 rgb[pixel_ptr++] = (pi1 >> 3) & 0x01;
118 rgb[pixel_ptr++] = (pi1 >> 2) & 0x01;
119 rgb[pixel_ptr++] = (pi1 >> 1) & 0x01;
120 rgb[pixel_ptr++] = pi1 & 0x01;
121 }
122 } else {
123 /* copy the same pixel directly to output 2 times */
124 rle_code *= 2;
126
127 while (rle_code--) {
128 int x = bytestream2_get_byte(&s->
g);
129 rgb[pixel_ptr++] = (x >> 7) & 0x01;
130 rgb[pixel_ptr++] = (x >> 6) & 0x01;
131 rgb[pixel_ptr++] = (x >> 5) & 0x01;
132 rgb[pixel_ptr++] = (x >> 4) & 0x01;
133 rgb[pixel_ptr++] = (x >> 3) & 0x01;
134 rgb[pixel_ptr++] = (x >> 2) & 0x01;
135 rgb[pixel_ptr++] = (x >> 1) & 0x01;
136 rgb[pixel_ptr++] = x & 0x01;
137 }
138 }
139 }
140 }
141
143 int lines_to_change, int bpp)
144 {
145 int rle_code, i;
146 int pixel_ptr;
148 uint8_t pi[16];
/* 16 palette indices */
151 int num_pixels = (bpp == 4) ? 8 : 16;
152
153 while (lines_to_change--) {
154 pixel_ptr = row_ptr + (num_pixels * (bytestream2_get_byte(&s->
g) - 1));
156
157 while ((rle_code = (int8_t)bytestream2_get_byte(&s->
g)) != -1) {
158 if (rle_code == 0) {
159 /* there's another skip code in the stream */
160 pixel_ptr += (num_pixels * (bytestream2_get_byte(&s->
g) - 1));
162 } else if (rle_code < 0) {
163 /* decode the run length code */
164 rle_code = -rle_code;
165 /* get the next 4 bytes from the stream, treat them as palette
166 * indexes, and output them rle_code times */
167 for (i = num_pixels-1; i >= 0; i--) {
168 pi[num_pixels-1-i] = (bytestream2_peek_byte(&s->
g) >> ((i*bpp) & 0x07)) & ((1<<bpp)-1);
170 }
172 while (rle_code--) {
173 memcpy(&rgb[pixel_ptr], &pi, num_pixels);
174 pixel_ptr += num_pixels;
175 }
176 } else {
177 /* copy the same pixel directly to output 4 times */
178 rle_code *= 4;
180 while (rle_code--) {
181 if(bpp == 4) {
182 int x = bytestream2_get_byte(&s->
g);
183 rgb[pixel_ptr++] = (x >> 4) & 0x0f;
184 rgb[pixel_ptr++] = x & 0x0f;
185 } else {
186 int x = bytestream2_get_byte(&s->
g);
187 rgb[pixel_ptr++] = (x >> 6) & 0x03;
188 rgb[pixel_ptr++] = (x >> 4) & 0x03;
189 rgb[pixel_ptr++] = (x >> 2) & 0x03;
190 rgb[pixel_ptr++] = x & 0x03;
191 }
192 }
193 }
194 }
195 row_ptr += row_inc;
196 }
197 }
198
200 {
201 int rle_code;
202 int pixel_ptr;
204 uint8_t pi1, pi2, pi3, pi4;
/* 4 palette indexes */
207
208 while (lines_to_change--) {
209 pixel_ptr = row_ptr + (4 * (bytestream2_get_byte(&s->
g) - 1));
211
212 while ((rle_code = (int8_t)bytestream2_get_byte(&s->
g)) != -1) {
213 if (rle_code == 0) {
214 /* there's another skip code in the stream */
215 pixel_ptr += (4 * (bytestream2_get_byte(&s->
g) - 1));
217 } else if (rle_code < 0) {
218 /* decode the run length code */
219 rle_code = -rle_code;
220 /* get the next 4 bytes from the stream, treat them as palette
221 * indexes, and output them rle_code times */
222 pi1 = bytestream2_get_byte(&s->
g);
223 pi2 = bytestream2_get_byte(&s->
g);
224 pi3 = bytestream2_get_byte(&s->
g);
225 pi4 = bytestream2_get_byte(&s->
g);
226
228
229 while (rle_code--) {
230 rgb[pixel_ptr++] = pi1;
231 rgb[pixel_ptr++] = pi2;
232 rgb[pixel_ptr++] = pi3;
233 rgb[pixel_ptr++] = pi4;
234 }
235 } else {
236 /* copy the same pixel directly to output 4 times */
237 rle_code *= 4;
239
241 pixel_ptr += rle_code;
242 }
243 }
244 row_ptr += row_inc;
245 }
246 }
247
249 {
250 int rle_code;
251 int pixel_ptr;
253 uint16_t rgb16;
256
257 while (lines_to_change--) {
258 pixel_ptr = row_ptr + (bytestream2_get_byte(&s->
g) - 1) * 2;
260
261 while ((rle_code = (int8_t)bytestream2_get_byte(&s->
g)) != -1) {
262 if (rle_code == 0) {
263 /* there's another skip code in the stream */
264 pixel_ptr += (bytestream2_get_byte(&s->
g) - 1) * 2;
266 } else if (rle_code < 0) {
267 /* decode the run length code */
268 rle_code = -rle_code;
269 rgb16 = bytestream2_get_be16(&s->
g);
270
272
273 while (rle_code--) {
274 *(uint16_t *)(&rgb[pixel_ptr]) = rgb16;
275 pixel_ptr += 2;
276 }
277 } else {
279
280 /* copy pixels directly to output */
281 while (rle_code--) {
282 rgb16 = bytestream2_get_be16(&s->
g);
283 *(uint16_t *)(&rgb[pixel_ptr]) = rgb16;
284 pixel_ptr += 2;
285 }
286 }
287 }
288 row_ptr += row_inc;
289 }
290 }
291
293 {
294 int rle_code;
295 int pixel_ptr;
300
301 while (lines_to_change--) {
302 pixel_ptr = row_ptr + (bytestream2_get_byte(&s->
g) - 1) * 3;
304
305 while ((rle_code = (int8_t)bytestream2_get_byte(&s->
g)) != -1) {
306 if (rle_code == 0) {
307 /* there's another skip code in the stream */
308 pixel_ptr += (bytestream2_get_byte(&s->
g) - 1) * 3;
310 } else if (rle_code < 0) {
311 /* decode the run length code */
312 rle_code = -rle_code;
313 r = bytestream2_get_byte(&s->
g);
314 g = bytestream2_get_byte(&s->
g);
315 b = bytestream2_get_byte(&s->
g);
316
318
319 while (rle_code--) {
320 rgb[pixel_ptr++] =
r;
321 rgb[pixel_ptr++] =
g;
322 rgb[pixel_ptr++] =
b;
323 }
324 } else {
326
327 /* copy pixels directly to output */
328 while (rle_code--) {
329 rgb[pixel_ptr++] = bytestream2_get_byte(&s->
g);
330 rgb[pixel_ptr++] = bytestream2_get_byte(&s->
g);
331 rgb[pixel_ptr++] = bytestream2_get_byte(&s->
g);
332 }
333 }
334 }
335 row_ptr += row_inc;
336 }
337 }
338
340 {
341 int rle_code;
342 int pixel_ptr;
344 unsigned int argb;
347
348 while (lines_to_change--) {
349 pixel_ptr = row_ptr + (bytestream2_get_byte(&s->
g) - 1) * 4;
351
352 while ((rle_code = (int8_t)bytestream2_get_byte(&s->
g)) != -1) {
353 if (rle_code == 0) {
354 /* there's another skip code in the stream */
355 pixel_ptr += (bytestream2_get_byte(&s->
g) - 1) * 4;
357 } else if (rle_code < 0) {
358 /* decode the run length code */
359 rle_code = -rle_code;
360 argb = bytestream2_get_be32(&s->
g);
361
363
364 while (rle_code--) {
366 pixel_ptr += 4;
367 }
368 } else {
370
371 /* copy pixels directly to output */
372 while (rle_code--) {
373 argb = bytestream2_get_be32(&s->
g);
375 pixel_ptr += 4;
376 }
377 }
378 }
379 row_ptr += row_inc;
380 }
381 }
382
384 {
386
389 case 1:
390 case 2:
391 case 4:
392 case 8:
393 case 33:
394 case 34:
395 case 36:
396 case 40:
398 break;
399
400 case 16:
402 break;
403
404 case 24:
406 break;
407
408 case 32:
410 break;
411
412 default:
416 }
417
421
422 return 0;
423 }
424
426 void *
data,
int *got_frame,
428 {
432 int has_palette = 0;
433 int ret;
434
437 return ret;
438
439 /* check if this frame is even supposed to change */
441 goto done;
442
443 /* start after the chunk size */
445
446 /* fetch the header */
447 header = bytestream2_get_be16(&s->
g);
448
449 /* if a header is present, fetch additional decoding parameters */
450 if (header & 0x0008) {
451 if (avpkt->
size < 14)
452 goto done;
453 start_line = bytestream2_get_be16(&s->
g);
455 height = bytestream2_get_be16(&s->
g);
458 goto done;
459 } else {
460 start_line = 0;
462 }
464
466 case 1:
467 case 33:
469 has_palette = 1;
470 break;
471
472 case 2:
473 case 34:
475 has_palette = 1;
476 break;
477
478 case 4:
479 case 36:
481 has_palette = 1;
482 break;
483
484 case 8:
485 case 40:
487 has_palette = 1;
488 break;
489
490 case 16:
492 break;
493
494 case 24:
496 break;
497
498 case 32:
500 break;
501
502 default:
505 break;
506 }
507
508 if(has_palette) {
510
511 if (pal) {
514 }
515
516 /* make the palette available on the way out */
518 }
519
520 done:
522 return ret;
523 *got_frame = 1;
524
525 /* always report that the buffer was completely consumed */
527 }
528
530 {
532
534
535 return 0;
536 }
537
548 };
static void qtrle_decode_2n4bpp(QtrleContext *s, int row_ptr, int lines_to_change, int bpp)
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
This structure describes decoded (raw) audio or video data.
ptrdiff_t const GLvoid * data
packed RGB 8:8:8, 24bpp, RGBRGB...
static av_cold int init(AVCodecContext *avctx)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
static void qtrle_decode_24bpp(QtrleContext *s, int row_ptr, int lines_to_change)
8 bit with AV_PIX_FMT_RGB32 palette
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
static av_cold int qtrle_decode_init(AVCodecContext *avctx)
static void qtrle_decode_1bpp(QtrleContext *s, int row_ptr, int lines_to_change)
static const uint8_t header[24]
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
const char * name
Name of the codec implementation.
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
Identical in function to av_frame_make_writable(), except it uses ff_get_buffer() to allocate the buf...
static void qtrle_decode_8bpp(QtrleContext *s, int row_ptr, int lines_to_change)
#define CHECK_PIXEL_PTR(n)
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
main external API structure.
BYTE int const BYTE int int int height
int palette_has_changed
Tell user application that palette has changed from previous frame.
static void qtrle_decode_32bpp(QtrleContext *s, int row_ptr, int lines_to_change)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
common internal api header.
static int qtrle_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
#define AV_PIX_FMT_RGB555
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
uint8_t * av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
static void qtrle_decode_16bpp(QtrleContext *s, int row_ptr, int lines_to_change)
static av_cold int qtrle_decode_end(AVCodecContext *avctx)
This structure stores compressed data.
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.