Replace 5 with AOT_SBR when referring to the MPEG-4 audio object type.
[FFMpeg-mirror/lagarith.git] / libavcodec / qtrle.c
blobdcb76ecfe50ccc075840c70c285bc25d01588e04
1 /*
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
22 /**
23 * @file libavcodec/qtrle.c
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.
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
38 #include "libavutil/intreadwrite.h"
39 #include "avcodec.h"
41 typedef struct QtrleContext {
43 AVCodecContext *avctx;
44 AVFrame frame;
46 const unsigned char *buf;
47 int size;
49 } QtrleContext;
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); \
55 return; \
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); \
62 return; \
63 } \
65 static void qtrle_decode_1bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
67 int rle_code;
68 int pixel_ptr = 0;
69 int row_inc = s->frame.linesize[0];
70 unsigned char pi0, pi1; /* 2 8-pixel values */
71 unsigned char *rgb = s->frame.data[0];
72 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
73 int skip;
75 while (lines_to_change) {
76 CHECK_STREAM_PTR(2);
77 skip = s->buf[stream_ptr++];
78 rle_code = (signed char)s->buf[stream_ptr++];
79 if (rle_code == 0)
80 break;
81 if(skip & 0x80) {
82 lines_to_change--;
83 row_ptr += row_inc;
84 pixel_ptr = row_ptr + 2 * (skip & 0x7f);
85 } else
86 pixel_ptr += 2 * skip;
87 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
89 if (rle_code < 0) {
90 /* decode the run length code */
91 rle_code = -rle_code;
92 /* get the next 2 bytes from the stream, treat them as groups
93 * of 8 pixels, and output them rle_code times */
94 CHECK_STREAM_PTR(2);
95 pi0 = s->buf[stream_ptr++];
96 pi1 = s->buf[stream_ptr++];
97 CHECK_PIXEL_PTR(rle_code * 2);
99 while (rle_code--) {
100 rgb[pixel_ptr++] = pi0;
101 rgb[pixel_ptr++] = pi1;
103 } else {
104 /* copy the same pixel directly to output 2 times */
105 rle_code *= 2;
106 CHECK_STREAM_PTR(rle_code);
107 CHECK_PIXEL_PTR(rle_code);
109 while (rle_code--)
110 rgb[pixel_ptr++] = s->buf[stream_ptr++];
115 static inline void qtrle_decode_2n4bpp(QtrleContext *s, int stream_ptr,
116 int row_ptr, int lines_to_change, int bpp)
118 int rle_code, i;
119 int pixel_ptr;
120 int row_inc = s->frame.linesize[0];
121 unsigned char pi[16]; /* 16 palette indices */
122 unsigned char *rgb = s->frame.data[0];
123 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
124 int num_pixels = (bpp == 4) ? 8 : 16;
126 while (lines_to_change--) {
127 CHECK_STREAM_PTR(2);
128 pixel_ptr = row_ptr + (num_pixels * (s->buf[stream_ptr++] - 1));
130 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
131 if (rle_code == 0) {
132 /* there's another skip code in the stream */
133 CHECK_STREAM_PTR(1);
134 pixel_ptr += (num_pixels * (s->buf[stream_ptr++] - 1));
135 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
136 } else if (rle_code < 0) {
137 /* decode the run length code */
138 rle_code = -rle_code;
139 /* get the next 4 bytes from the stream, treat them as palette
140 * indexes, and output them rle_code times */
141 CHECK_STREAM_PTR(4);
142 for (i = num_pixels-1; i >= 0; i--) {
143 pi[num_pixels-1-i] = (s->buf[stream_ptr] >> ((i*bpp) & 0x07)) & ((1<<bpp)-1);
144 stream_ptr+= ((i & ((num_pixels>>2)-1)) == 0);
146 CHECK_PIXEL_PTR(rle_code * num_pixels);
147 while (rle_code--) {
148 for (i = 0; i < num_pixels; i++)
149 rgb[pixel_ptr++] = pi[i];
151 } else {
152 /* copy the same pixel directly to output 4 times */
153 rle_code *= 4;
154 CHECK_STREAM_PTR(rle_code);
155 CHECK_PIXEL_PTR(rle_code*(num_pixels>>2));
156 while (rle_code--) {
157 if(bpp == 4) {
158 rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 4) & 0x0f;
159 rgb[pixel_ptr++] = (s->buf[stream_ptr++]) & 0x0f;
160 } else {
161 rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 6) & 0x03;
162 rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 4) & 0x03;
163 rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 2) & 0x03;
164 rgb[pixel_ptr++] = (s->buf[stream_ptr++]) & 0x03;
169 row_ptr += row_inc;
173 static void qtrle_decode_8bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
175 int rle_code;
176 int pixel_ptr;
177 int row_inc = s->frame.linesize[0];
178 unsigned char pi1, pi2, pi3, pi4; /* 4 palette indexes */
179 unsigned char *rgb = s->frame.data[0];
180 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
182 while (lines_to_change--) {
183 CHECK_STREAM_PTR(2);
184 pixel_ptr = row_ptr + (4 * (s->buf[stream_ptr++] - 1));
186 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
187 if (rle_code == 0) {
188 /* there's another skip code in the stream */
189 CHECK_STREAM_PTR(1);
190 pixel_ptr += (4 * (s->buf[stream_ptr++] - 1));
191 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
192 } else if (rle_code < 0) {
193 /* decode the run length code */
194 rle_code = -rle_code;
195 /* get the next 4 bytes from the stream, treat them as palette
196 * indexes, and output them rle_code times */
197 CHECK_STREAM_PTR(4);
198 pi1 = s->buf[stream_ptr++];
199 pi2 = s->buf[stream_ptr++];
200 pi3 = s->buf[stream_ptr++];
201 pi4 = s->buf[stream_ptr++];
203 CHECK_PIXEL_PTR(rle_code * 4);
205 while (rle_code--) {
206 rgb[pixel_ptr++] = pi1;
207 rgb[pixel_ptr++] = pi2;
208 rgb[pixel_ptr++] = pi3;
209 rgb[pixel_ptr++] = pi4;
211 } else {
212 /* copy the same pixel directly to output 4 times */
213 rle_code *= 4;
214 CHECK_STREAM_PTR(rle_code);
215 CHECK_PIXEL_PTR(rle_code);
217 while (rle_code--) {
218 rgb[pixel_ptr++] = s->buf[stream_ptr++];
222 row_ptr += row_inc;
226 static void qtrle_decode_16bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
228 int rle_code;
229 int pixel_ptr;
230 int row_inc = s->frame.linesize[0];
231 unsigned short rgb16;
232 unsigned char *rgb = s->frame.data[0];
233 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
235 while (lines_to_change--) {
236 CHECK_STREAM_PTR(2);
237 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 2;
239 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
240 if (rle_code == 0) {
241 /* there's another skip code in the stream */
242 CHECK_STREAM_PTR(1);
243 pixel_ptr += (s->buf[stream_ptr++] - 1) * 2;
244 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
245 } else if (rle_code < 0) {
246 /* decode the run length code */
247 rle_code = -rle_code;
248 CHECK_STREAM_PTR(2);
249 rgb16 = AV_RB16(&s->buf[stream_ptr]);
250 stream_ptr += 2;
252 CHECK_PIXEL_PTR(rle_code * 2);
254 while (rle_code--) {
255 *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
256 pixel_ptr += 2;
258 } else {
259 CHECK_STREAM_PTR(rle_code * 2);
260 CHECK_PIXEL_PTR(rle_code * 2);
262 /* copy pixels directly to output */
263 while (rle_code--) {
264 rgb16 = AV_RB16(&s->buf[stream_ptr]);
265 stream_ptr += 2;
266 *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
267 pixel_ptr += 2;
271 row_ptr += row_inc;
275 static void qtrle_decode_24bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
277 int rle_code;
278 int pixel_ptr;
279 int row_inc = s->frame.linesize[0];
280 unsigned char r, g, b;
281 unsigned char *rgb = s->frame.data[0];
282 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
284 while (lines_to_change--) {
285 CHECK_STREAM_PTR(2);
286 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 3;
288 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
289 if (rle_code == 0) {
290 /* there's another skip code in the stream */
291 CHECK_STREAM_PTR(1);
292 pixel_ptr += (s->buf[stream_ptr++] - 1) * 3;
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;
297 CHECK_STREAM_PTR(3);
298 r = s->buf[stream_ptr++];
299 g = s->buf[stream_ptr++];
300 b = s->buf[stream_ptr++];
302 CHECK_PIXEL_PTR(rle_code * 3);
304 while (rle_code--) {
305 rgb[pixel_ptr++] = r;
306 rgb[pixel_ptr++] = g;
307 rgb[pixel_ptr++] = b;
309 } else {
310 CHECK_STREAM_PTR(rle_code * 3);
311 CHECK_PIXEL_PTR(rle_code * 3);
313 /* copy pixels directly to output */
314 while (rle_code--) {
315 rgb[pixel_ptr++] = s->buf[stream_ptr++];
316 rgb[pixel_ptr++] = s->buf[stream_ptr++];
317 rgb[pixel_ptr++] = s->buf[stream_ptr++];
321 row_ptr += row_inc;
325 static void qtrle_decode_32bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
327 int rle_code;
328 int pixel_ptr;
329 int row_inc = s->frame.linesize[0];
330 unsigned char a, r, g, b;
331 unsigned int argb;
332 unsigned char *rgb = s->frame.data[0];
333 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
335 while (lines_to_change--) {
336 CHECK_STREAM_PTR(2);
337 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 4;
339 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
340 if (rle_code == 0) {
341 /* there's another skip code in the stream */
342 CHECK_STREAM_PTR(1);
343 pixel_ptr += (s->buf[stream_ptr++] - 1) * 4;
344 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
345 } else if (rle_code < 0) {
346 /* decode the run length code */
347 rle_code = -rle_code;
348 CHECK_STREAM_PTR(4);
349 a = s->buf[stream_ptr++];
350 r = s->buf[stream_ptr++];
351 g = s->buf[stream_ptr++];
352 b = s->buf[stream_ptr++];
353 argb = (a << 24) | (r << 16) | (g << 8) | (b << 0);
355 CHECK_PIXEL_PTR(rle_code * 4);
357 while (rle_code--) {
358 *(unsigned int *)(&rgb[pixel_ptr]) = argb;
359 pixel_ptr += 4;
361 } else {
362 CHECK_STREAM_PTR(rle_code * 4);
363 CHECK_PIXEL_PTR(rle_code * 4);
365 /* copy pixels directly to output */
366 while (rle_code--) {
367 a = s->buf[stream_ptr++];
368 r = s->buf[stream_ptr++];
369 g = s->buf[stream_ptr++];
370 b = s->buf[stream_ptr++];
371 argb = (a << 24) | (r << 16) | (g << 8) | (b << 0);
372 *(unsigned int *)(&rgb[pixel_ptr]) = argb;
373 pixel_ptr += 4;
377 row_ptr += row_inc;
381 static av_cold int qtrle_decode_init(AVCodecContext *avctx)
383 QtrleContext *s = avctx->priv_data;
385 s->avctx = avctx;
386 switch (avctx->bits_per_coded_sample) {
387 case 1:
388 case 33:
389 avctx->pix_fmt = PIX_FMT_MONOWHITE;
390 break;
392 case 2:
393 case 4:
394 case 8:
395 case 34:
396 case 36:
397 case 40:
398 avctx->pix_fmt = PIX_FMT_PAL8;
399 break;
401 case 16:
402 avctx->pix_fmt = PIX_FMT_RGB555;
403 break;
405 case 24:
406 avctx->pix_fmt = PIX_FMT_RGB24;
407 break;
409 case 32:
410 avctx->pix_fmt = PIX_FMT_RGB32;
411 break;
413 default:
414 av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
415 avctx->bits_per_coded_sample);
416 break;
419 s->frame.data[0] = NULL;
421 return 0;
424 static int qtrle_decode_frame(AVCodecContext *avctx,
425 void *data, int *data_size,
426 AVPacket *avpkt)
428 const uint8_t *buf = avpkt->data;
429 int buf_size = avpkt->size;
430 QtrleContext *s = avctx->priv_data;
431 int header, start_line;
432 int stream_ptr, height, row_ptr;
433 int has_palette = 0;
435 s->buf = buf;
436 s->size = buf_size;
438 s->frame.reference = 1;
439 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
440 FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
441 if (avctx->reget_buffer(avctx, &s->frame)) {
442 av_log (s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
443 return -1;
446 /* check if this frame is even supposed to change */
447 if (s->size < 8)
448 goto done;
450 /* start after the chunk size */
451 stream_ptr = 4;
453 /* fetch the header */
454 header = AV_RB16(&s->buf[stream_ptr]);
455 stream_ptr += 2;
457 /* if a header is present, fetch additional decoding parameters */
458 if (header & 0x0008) {
459 if(s->size < 14)
460 goto done;
461 start_line = AV_RB16(&s->buf[stream_ptr]);
462 stream_ptr += 4;
463 height = AV_RB16(&s->buf[stream_ptr]);
464 stream_ptr += 4;
465 } else {
466 start_line = 0;
467 height = s->avctx->height;
469 row_ptr = s->frame.linesize[0] * start_line;
471 switch (avctx->bits_per_coded_sample) {
472 case 1:
473 case 33:
474 qtrle_decode_1bpp(s, stream_ptr, row_ptr, height);
475 break;
477 case 2:
478 case 34:
479 qtrle_decode_2n4bpp(s, stream_ptr, row_ptr, height, 2);
480 has_palette = 1;
481 break;
483 case 4:
484 case 36:
485 qtrle_decode_2n4bpp(s, stream_ptr, row_ptr, height, 4);
486 has_palette = 1;
487 break;
489 case 8:
490 case 40:
491 qtrle_decode_8bpp(s, stream_ptr, row_ptr, height);
492 has_palette = 1;
493 break;
495 case 16:
496 qtrle_decode_16bpp(s, stream_ptr, row_ptr, height);
497 break;
499 case 24:
500 qtrle_decode_24bpp(s, stream_ptr, row_ptr, height);
501 break;
503 case 32:
504 qtrle_decode_32bpp(s, stream_ptr, row_ptr, height);
505 break;
507 default:
508 av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
509 avctx->bits_per_coded_sample);
510 break;
513 if(has_palette) {
514 /* make the palette available on the way out */
515 memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
516 if (s->avctx->palctrl->palette_changed) {
517 s->frame.palette_has_changed = 1;
518 s->avctx->palctrl->palette_changed = 0;
522 done:
523 *data_size = sizeof(AVFrame);
524 *(AVFrame*)data = s->frame;
526 /* always report that the buffer was completely consumed */
527 return buf_size;
530 static av_cold int qtrle_decode_end(AVCodecContext *avctx)
532 QtrleContext *s = avctx->priv_data;
534 if (s->frame.data[0])
535 avctx->release_buffer(avctx, &s->frame);
537 return 0;
540 AVCodec qtrle_decoder = {
541 "qtrle",
542 CODEC_TYPE_VIDEO,
543 CODEC_ID_QTRLE,
544 sizeof(QtrleContext),
545 qtrle_decode_init,
546 NULL,
547 qtrle_decode_end,
548 qtrle_decode_frame,
549 CODEC_CAP_DR1,
550 .long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),