comparison qtrle.c @ 1783:66ae3c109d90 libavcodec

initial commit for Quicktime Animation (RLE) video decoder; bit depths 32, 24, and 16 are working; 8bpp is partially working; 4, 2, and 1 bpp are not supported yet
author melanson
date Thu, 05 Feb 2004 05:22:44 +0000
parents
children db067aa9fc2b
comparison
equal deleted inserted replaced
1782:82c182e52a54 1783:66ae3c109d90
1 /*
2 * Quicktime Animation (RLE) Video Decoder
3 * Copyright (C) 2004 the ffmpeg project
4 *
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.
9 *
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.
14 *
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
18 *
19 */
20
21 /**
22 * @file qtrle.c
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/
26 *
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 RGB24
30 * data. 24-bit data is RGB888 and 32-bit data is RGBA32.
31 */
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37
38 #include "common.h"
39 #include "avcodec.h"
40 #include "dsputil.h"
41
42 typedef struct QtrleContext {
43
44 AVCodecContext *avctx;
45 DSPContext dsp;
46 AVFrame frame;
47
48 unsigned char *buf;
49 int size;
50
51 } QtrleContext;
52
53 #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
54
55 #define CHECK_STREAM_PTR(n) \
56 if ((stream_ptr + n) > s->size) { \
57 printf ("QT RLE problem: stream_ptr out of bounds (%d >= %d)\n", \
58 stream_ptr + n, s->size); \
59 return; \
60 }
61
62 #define CHECK_PIXEL_PTR(n) \
63 if (pixel_ptr + n > pixel_limit) { \
64 printf ("QT RLE problem: pixel_ptr >= pixel_limit (%d >= %d)\n", \
65 pixel_ptr + n, pixel_limit); \
66 return; \
67 } \
68
69 static void qtrle_decode_1bpp(QtrleContext *s)
70 {
71 }
72
73 static void qtrle_decode_2bpp(QtrleContext *s)
74 {
75 }
76
77 static void qtrle_decode_4bpp(QtrleContext *s)
78 {
79 }
80
81 static void qtrle_decode_8bpp(QtrleContext *s)
82 {
83 int stream_ptr;
84 int header;
85 int start_line;
86 int lines_to_change;
87 signed char rle_code;
88 int row_ptr, pixel_ptr;
89 int row_inc = s->frame.linesize[0];
90 unsigned char pi1, pi2, pi3, pi4; /* 4 palette indices */
91 unsigned char *rgb = s->frame.data[0];
92 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
93
94 /* check if this frame is even supposed to change */
95 if (s->size < 8)
96 return;
97
98 /* start after the chunk size */
99 stream_ptr = 4;
100
101 /* fetch the header */
102 CHECK_STREAM_PTR(2);
103 header = BE_16(&s->buf[stream_ptr]);
104 stream_ptr += 2;
105
106 /* if a header is present, fetch additional decoding parameters */
107 if (header & 0x0008) {
108 CHECK_STREAM_PTR(8);
109 start_line = BE_16(&s->buf[stream_ptr]);
110 stream_ptr += 4;
111 lines_to_change = BE_16(&s->buf[stream_ptr]);
112 stream_ptr += 4;
113 } else {
114 start_line = 0;
115 lines_to_change = s->avctx->height;
116 }
117
118 row_ptr = row_inc * start_line;
119 while (lines_to_change--) {
120 CHECK_STREAM_PTR(2);
121 pixel_ptr = row_ptr + (4 * (s->buf[stream_ptr++] - 1));
122
123 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
124 if (rle_code == 0) {
125 /* there's another skip code in the stream */
126 CHECK_STREAM_PTR(1);
127 pixel_ptr += (4 * (s->buf[stream_ptr++] - 1));
128 } else if (rle_code < 0) {
129 /* decode the run length code */
130 rle_code = -rle_code;
131 /* get the next 4 bytes from the stream, treat them as palette
132 * indices, and output them rle_code times */
133 CHECK_STREAM_PTR(4);
134 pi1 = s->buf[stream_ptr++];
135 pi2 = s->buf[stream_ptr++];
136 pi3 = s->buf[stream_ptr++];
137 pi4 = s->buf[stream_ptr++];
138
139 CHECK_PIXEL_PTR(rle_code * 4);
140
141 while (rle_code--) {
142 rgb[pixel_ptr++] = pi1;
143 rgb[pixel_ptr++] = pi2;
144 rgb[pixel_ptr++] = pi3;
145 rgb[pixel_ptr++] = pi4;
146 }
147 } else {
148 /* copy the same pixel directly to output 4 times */
149 rle_code *= 4;
150 CHECK_STREAM_PTR(rle_code);
151 CHECK_PIXEL_PTR(rle_code);
152
153 while (rle_code--) {
154 rgb[pixel_ptr++] = s->buf[stream_ptr++];
155 }
156 }
157 }
158 row_ptr += row_inc;
159 }
160 }
161
162 static void qtrle_decode_16bpp(QtrleContext *s)
163 {
164 int stream_ptr;
165 int header;
166 int start_line;
167 int lines_to_change;
168 signed char rle_code;
169 int row_ptr, pixel_ptr;
170 int row_inc = s->frame.linesize[0];
171 unsigned short rgb16;
172 unsigned char *rgb = s->frame.data[0];
173 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
174
175 /* check if this frame is even supposed to change */
176 if (s->size < 8)
177 return;
178
179 /* start after the chunk size */
180 stream_ptr = 4;
181
182 /* fetch the header */
183 CHECK_STREAM_PTR(2);
184 header = BE_16(&s->buf[stream_ptr]);
185 stream_ptr += 2;
186
187 /* if a header is present, fetch additional decoding parameters */
188 if (header & 0x0008) {
189 CHECK_STREAM_PTR(8);
190 start_line = BE_16(&s->buf[stream_ptr]);
191 stream_ptr += 4;
192 lines_to_change = BE_16(&s->buf[stream_ptr]);
193 stream_ptr += 4;
194 } else {
195 start_line = 0;
196 lines_to_change = s->avctx->height;
197 }
198
199 row_ptr = row_inc * start_line;
200 while (lines_to_change--) {
201 CHECK_STREAM_PTR(2);
202 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 2;
203
204 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
205 if (rle_code == 0) {
206 /* there's another skip code in the stream */
207 CHECK_STREAM_PTR(1);
208 pixel_ptr += (s->buf[stream_ptr++] - 1) * 2;
209 } else if (rle_code < 0) {
210 /* decode the run length code */
211 rle_code = -rle_code;
212 CHECK_STREAM_PTR(2);
213 rgb16 = BE_16(&s->buf[stream_ptr]);
214 stream_ptr += 2;
215
216 CHECK_PIXEL_PTR(rle_code * 2);
217
218 while (rle_code--) {
219 *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
220 pixel_ptr += 2;
221 }
222 } else {
223 CHECK_STREAM_PTR(rle_code * 2);
224 CHECK_PIXEL_PTR(rle_code * 2);
225
226 /* copy pixels directly to output */
227 while (rle_code--) {
228 rgb16 = BE_16(&s->buf[stream_ptr]);
229 stream_ptr += 2;
230 *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
231 pixel_ptr += 2;
232 }
233 }
234 }
235 row_ptr += row_inc;
236 }
237 }
238
239 static void qtrle_decode_24bpp(QtrleContext *s)
240 {
241 int stream_ptr;
242 int header;
243 int start_line;
244 int lines_to_change;
245 signed char rle_code;
246 int row_ptr, pixel_ptr;
247 int row_inc = s->frame.linesize[0];
248 unsigned char r, g, b;
249 unsigned char *rgb = s->frame.data[0];
250 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
251
252 /* check if this frame is even supposed to change */
253 if (s->size < 8)
254 return;
255
256 /* start after the chunk size */
257 stream_ptr = 4;
258
259 /* fetch the header */
260 CHECK_STREAM_PTR(2);
261 header = BE_16(&s->buf[stream_ptr]);
262 stream_ptr += 2;
263
264 /* if a header is present, fetch additional decoding parameters */
265 if (header & 0x0008) {
266 CHECK_STREAM_PTR(8);
267 start_line = BE_16(&s->buf[stream_ptr]);
268 stream_ptr += 4;
269 lines_to_change = BE_16(&s->buf[stream_ptr]);
270 stream_ptr += 4;
271 } else {
272 start_line = 0;
273 lines_to_change = s->avctx->height;
274 }
275
276 row_ptr = row_inc * start_line;
277 while (lines_to_change--) {
278 CHECK_STREAM_PTR(2);
279 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 3;
280
281 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
282 if (rle_code == 0) {
283 /* there's another skip code in the stream */
284 CHECK_STREAM_PTR(1);
285 pixel_ptr += (s->buf[stream_ptr++] - 1) * 3;
286 } else if (rle_code < 0) {
287 /* decode the run length code */
288 rle_code = -rle_code;
289 CHECK_STREAM_PTR(3);
290 r = s->buf[stream_ptr++];
291 g = s->buf[stream_ptr++];
292 b = s->buf[stream_ptr++];
293
294 CHECK_PIXEL_PTR(rle_code * 3);
295
296 while (rle_code--) {
297 rgb[pixel_ptr++] = r;
298 rgb[pixel_ptr++] = g;
299 rgb[pixel_ptr++] = b;
300 }
301 } else {
302 CHECK_STREAM_PTR(rle_code * 3);
303 CHECK_PIXEL_PTR(rle_code * 3);
304
305 /* copy pixels directly to output */
306 while (rle_code--) {
307 rgb[pixel_ptr++] = s->buf[stream_ptr++];
308 rgb[pixel_ptr++] = s->buf[stream_ptr++];
309 rgb[pixel_ptr++] = s->buf[stream_ptr++];
310 }
311 }
312 }
313 row_ptr += row_inc;
314 }
315 }
316
317 static void qtrle_decode_32bpp(QtrleContext *s)
318 {
319 int stream_ptr;
320 int header;
321 int start_line;
322 int lines_to_change;
323 signed char rle_code;
324 int row_ptr, pixel_ptr;
325 int row_inc = s->frame.linesize[0];
326 unsigned char r, g, b;
327 unsigned int argb;
328 unsigned char *rgb = s->frame.data[0];
329 int pixel_limit = s->frame.linesize[0] * s->avctx->height;
330
331 /* check if this frame is even supposed to change */
332 if (s->size < 8)
333 return;
334
335 /* start after the chunk size */
336 stream_ptr = 4;
337
338 /* fetch the header */
339 CHECK_STREAM_PTR(2);
340 header = BE_16(&s->buf[stream_ptr]);
341 stream_ptr += 2;
342
343 /* if a header is present, fetch additional decoding parameters */
344 if (header & 0x0008) {
345 CHECK_STREAM_PTR(8);
346 start_line = BE_16(&s->buf[stream_ptr]);
347 stream_ptr += 4;
348 lines_to_change = BE_16(&s->buf[stream_ptr]);
349 stream_ptr += 4;
350 } else {
351 start_line = 0;
352 lines_to_change = s->avctx->height;
353 }
354
355 row_ptr = row_inc * start_line;
356 while (lines_to_change--) {
357 CHECK_STREAM_PTR(2);
358 pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 4;
359
360 while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
361 if (rle_code == 0) {
362 /* there's another skip code in the stream */
363 CHECK_STREAM_PTR(1);
364 pixel_ptr += (s->buf[stream_ptr++] - 1) * 4;
365 } else if (rle_code < 0) {
366 /* decode the run length code */
367 rle_code = -rle_code;
368 CHECK_STREAM_PTR(4);
369 stream_ptr++; /* skip the alpha (?) byte */
370 r = s->buf[stream_ptr++];
371 g = s->buf[stream_ptr++];
372 b = s->buf[stream_ptr++];
373 argb = (r << 16) | (g << 8) | (b << 0);
374
375 CHECK_PIXEL_PTR(rle_code * 4);
376
377 while (rle_code--) {
378 *(unsigned int *)(&rgb[pixel_ptr]) = argb;
379 pixel_ptr += 4;
380 }
381 } else {
382 CHECK_STREAM_PTR(rle_code * 4);
383 CHECK_PIXEL_PTR(rle_code * 4);
384
385 /* copy pixels directly to output */
386 while (rle_code--) {
387 stream_ptr++; /* skip the alpha (?) byte */
388 r = s->buf[stream_ptr++];
389 g = s->buf[stream_ptr++];
390 b = s->buf[stream_ptr++];
391 argb = (r << 16) | (g << 8) | (b << 0);
392 *(unsigned int *)(&rgb[pixel_ptr]) = argb;
393 pixel_ptr += 4;
394 }
395 }
396 }
397 row_ptr += row_inc;
398 }
399 }
400
401 static int qtrle_decode_init(AVCodecContext *avctx)
402 {
403 QtrleContext *s = (QtrleContext *)avctx->priv_data;
404
405 s->avctx = avctx;
406 switch (avctx->bits_per_sample) {
407 case 1:
408 case 2:
409 case 4:
410 case 8:
411 case 33:
412 case 34:
413 case 36:
414 case 40:
415 avctx->pix_fmt = PIX_FMT_PAL8;
416 break;
417
418 case 16:
419 avctx->pix_fmt = PIX_FMT_RGB555;
420 break;
421
422 case 24:
423 avctx->pix_fmt = PIX_FMT_RGB24;
424 break;
425
426 case 32:
427 avctx->pix_fmt = PIX_FMT_RGBA32;
428 break;
429
430 default:
431 printf ("QT RLE: Unsupported colorspace: %d bits/sample?\n",
432 avctx->bits_per_sample);
433 break;
434 }
435 avctx->has_b_frames = 0;
436 dsputil_init(&s->dsp, avctx);
437
438 s->frame.data[0] = NULL;
439
440 return 0;
441 }
442
443 static int qtrle_decode_frame(AVCodecContext *avctx,
444 void *data, int *data_size,
445 uint8_t *buf, int buf_size)
446 {
447 QtrleContext *s = (QtrleContext *)avctx->priv_data;
448
449 /* no supplementary picture */
450 if (buf_size == 0)
451 return 0;
452
453 s->buf = buf;
454 s->size = buf_size;
455
456 s->frame.reference = 1;
457 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
458 FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
459 if (avctx->reget_buffer(avctx, &s->frame)) {
460 printf ("reget_buffer() failed\n");
461 return -1;
462 }
463
464 switch (avctx->bits_per_sample) {
465 case 1:
466 case 33:
467 qtrle_decode_1bpp(s);
468 break;
469
470 case 2:
471 case 34:
472 qtrle_decode_2bpp(s);
473 break;
474
475 case 4:
476 case 36:
477 qtrle_decode_4bpp(s);
478 break;
479
480 case 8:
481 case 40:
482 qtrle_decode_8bpp(s);
483 /* make the palette available on the way out */
484 memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
485 if (s->avctx->palctrl->palette_changed) {
486 s->frame.palette_has_changed = 1;
487 s->avctx->palctrl->palette_changed = 0;
488 }
489 break;
490
491 case 16:
492 qtrle_decode_16bpp(s);
493 break;
494
495 case 24:
496 qtrle_decode_24bpp(s);
497 break;
498
499 case 32:
500 qtrle_decode_32bpp(s);
501 break;
502
503 default:
504 printf ("QT RLE: Unsupported colorspace: %d bits/sample?\n",
505 avctx->bits_per_sample);
506 break;
507 }
508
509 *data_size = sizeof(AVFrame);
510 *(AVFrame*)data = s->frame;
511
512 /* always report that the buffer was completely consumed */
513 return buf_size;
514 }
515
516 static int qtrle_decode_end(AVCodecContext *avctx)
517 {
518 QtrleContext *s = (QtrleContext *)avctx->priv_data;
519
520 if (s->frame.data[0])
521 avctx->release_buffer(avctx, &s->frame);
522
523 return 0;
524 }
525
526 AVCodec qtrle_decoder = {
527 "qtrle",
528 CODEC_TYPE_VIDEO,
529 CODEC_ID_QTRLE,
530 sizeof(QtrleContext),
531 qtrle_decode_init,
532 NULL,
533 qtrle_decode_end,
534 qtrle_decode_frame,
535 CODEC_CAP_DR1,
536 };
537