]> git.sesse.net Git - ffmpeg/blob - libavcodec/qtrle.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / qtrle.c
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
38 #include "libavutil/intreadwrite.h"
39 #include "avcodec.h"
40
41 typedef struct QtrleContext {
42
43     AVCodecContext *avctx;
44     AVFrame frame;
45
46     const unsigned char *buf;
47     int size;
48
49     uint32_t pal[256];
50 } QtrleContext;
51
52 #define CHECK_STREAM_PTR(n) \
53   if ((stream_ptr + n) > s->size) { \
54     av_log (s->avctx, AV_LOG_INFO, "Problem: stream_ptr out of bounds (%d >= %d)\n", \
55       stream_ptr + n, s->size); \
56     return; \
57   }
58
59 #define CHECK_PIXEL_PTR(n) \
60   if ((pixel_ptr + n > pixel_limit) || (pixel_ptr + n < 0)) { \
61     av_log (s->avctx, AV_LOG_INFO, "Problem: pixel_ptr = %d, pixel_limit = %d\n", \
62       pixel_ptr + n, pixel_limit); \
63     return; \
64   } \
65
66 static void qtrle_decode_1bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
67 {
68     int rle_code;
69     int pixel_ptr = 0;
70     int row_inc = s->frame.linesize[0];
71     unsigned char pi0, pi1;  /* 2 8-pixel values */
72     unsigned char *rgb = s->frame.data[0];
73     int pixel_limit = s->frame.linesize[0] * s->avctx->height;
74     int skip;
75
76     while (lines_to_change) {
77         CHECK_STREAM_PTR(2);
78         skip = s->buf[stream_ptr++];
79         rle_code = (signed char)s->buf[stream_ptr++];
80         if (rle_code == 0)
81             break;
82         if(skip & 0x80) {
83             lines_to_change--;
84             row_ptr += row_inc;
85             pixel_ptr = row_ptr + 2 * (skip & 0x7f);
86         } else
87             pixel_ptr += 2 * skip;
88         CHECK_PIXEL_PTR(0);  /* make sure pixel_ptr is positive */
89
90         if (rle_code < 0) {
91             /* decode the run length code */
92             rle_code = -rle_code;
93             /* get the next 2 bytes from the stream, treat them as groups
94              * of 8 pixels, and output them rle_code times */
95             CHECK_STREAM_PTR(2);
96             pi0 = s->buf[stream_ptr++];
97             pi1 = s->buf[stream_ptr++];
98             CHECK_PIXEL_PTR(rle_code * 2);
99
100             while (rle_code--) {
101                 rgb[pixel_ptr++] = pi0;
102                 rgb[pixel_ptr++] = pi1;
103             }
104         } else {
105             /* copy the same pixel directly to output 2 times */
106             rle_code *= 2;
107             CHECK_STREAM_PTR(rle_code);
108             CHECK_PIXEL_PTR(rle_code);
109
110             while (rle_code--)
111                 rgb[pixel_ptr++] = s->buf[stream_ptr++];
112         }
113     }
114 }
115
116 static inline void qtrle_decode_2n4bpp(QtrleContext *s, int stream_ptr,
117                              int row_ptr, int lines_to_change, int bpp)
118 {
119     int rle_code, i;
120     int pixel_ptr;
121     int row_inc = s->frame.linesize[0];
122     unsigned char pi[16];  /* 16 palette indices */
123     unsigned char *rgb = s->frame.data[0];
124     int pixel_limit = s->frame.linesize[0] * s->avctx->height;
125     int num_pixels = (bpp == 4) ? 8 : 16;
126
127     while (lines_to_change--) {
128         CHECK_STREAM_PTR(2);
129         pixel_ptr = row_ptr + (num_pixels * (s->buf[stream_ptr++] - 1));
130         CHECK_PIXEL_PTR(0);  /* make sure pixel_ptr is positive */
131
132         while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
133             if (rle_code == 0) {
134                 /* there's another skip code in the stream */
135                 CHECK_STREAM_PTR(1);
136                 pixel_ptr += (num_pixels * (s->buf[stream_ptr++] - 1));
137                 CHECK_PIXEL_PTR(0);  /* make sure pixel_ptr is positive */
138             } else if (rle_code < 0) {
139                 /* decode the run length code */
140                 rle_code = -rle_code;
141                 /* get the next 4 bytes from the stream, treat them as palette
142                  * indexes, and output them rle_code times */
143                 CHECK_STREAM_PTR(4);
144                 for (i = num_pixels-1; i >= 0; i--) {
145                     pi[num_pixels-1-i] = (s->buf[stream_ptr] >> ((i*bpp) & 0x07)) & ((1<<bpp)-1);
146                     stream_ptr+= ((i & ((num_pixels>>2)-1)) == 0);
147                 }
148                 CHECK_PIXEL_PTR(rle_code * num_pixels);
149                 while (rle_code--) {
150                     for (i = 0; i < num_pixels; i++)
151                         rgb[pixel_ptr++] = pi[i];
152                 }
153             } else {
154                 /* copy the same pixel directly to output 4 times */
155                 rle_code *= 4;
156                 CHECK_STREAM_PTR(rle_code);
157                 CHECK_PIXEL_PTR(rle_code*(num_pixels>>2));
158                 while (rle_code--) {
159                     if(bpp == 4) {
160                         rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 4) & 0x0f;
161                         rgb[pixel_ptr++] = (s->buf[stream_ptr++]) & 0x0f;
162                     } else {
163                         rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 6) & 0x03;
164                         rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 4) & 0x03;
165                         rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 2) & 0x03;
166                         rgb[pixel_ptr++] = (s->buf[stream_ptr++]) & 0x03;
167                     }
168                 }
169             }
170         }
171         row_ptr += row_inc;
172     }
173 }
174
175 static void qtrle_decode_8bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
176 {
177     int rle_code;
178     int pixel_ptr;
179     int row_inc = s->frame.linesize[0];
180     unsigned char pi1, pi2, pi3, pi4;  /* 4 palette indexes */
181     unsigned char *rgb = s->frame.data[0];
182     int pixel_limit = s->frame.linesize[0] * s->avctx->height;
183
184     while (lines_to_change--) {
185         CHECK_STREAM_PTR(2);
186         pixel_ptr = row_ptr + (4 * (s->buf[stream_ptr++] - 1));
187         CHECK_PIXEL_PTR(0);  /* make sure pixel_ptr is positive */
188
189         while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
190             if (rle_code == 0) {
191                 /* there's another skip code in the stream */
192                 CHECK_STREAM_PTR(1);
193                 pixel_ptr += (4 * (s->buf[stream_ptr++] - 1));
194                 CHECK_PIXEL_PTR(0);  /* make sure pixel_ptr is positive */
195             } else if (rle_code < 0) {
196                 /* decode the run length code */
197                 rle_code = -rle_code;
198                 /* get the next 4 bytes from the stream, treat them as palette
199                  * indexes, and output them rle_code times */
200                 CHECK_STREAM_PTR(4);
201                 pi1 = s->buf[stream_ptr++];
202                 pi2 = s->buf[stream_ptr++];
203                 pi3 = s->buf[stream_ptr++];
204                 pi4 = s->buf[stream_ptr++];
205
206                 CHECK_PIXEL_PTR(rle_code * 4);
207
208                 while (rle_code--) {
209                     rgb[pixel_ptr++] = pi1;
210                     rgb[pixel_ptr++] = pi2;
211                     rgb[pixel_ptr++] = pi3;
212                     rgb[pixel_ptr++] = pi4;
213                 }
214             } else {
215                 /* copy the same pixel directly to output 4 times */
216                 rle_code *= 4;
217                 CHECK_STREAM_PTR(rle_code);
218                 CHECK_PIXEL_PTR(rle_code);
219
220                 while (rle_code--) {
221                     rgb[pixel_ptr++] = s->buf[stream_ptr++];
222                 }
223             }
224         }
225         row_ptr += row_inc;
226     }
227 }
228
229 static void qtrle_decode_16bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
230 {
231     int rle_code;
232     int pixel_ptr;
233     int row_inc = s->frame.linesize[0];
234     unsigned short rgb16;
235     unsigned char *rgb = s->frame.data[0];
236     int pixel_limit = s->frame.linesize[0] * s->avctx->height;
237
238     while (lines_to_change--) {
239         CHECK_STREAM_PTR(2);
240         pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 2;
241         CHECK_PIXEL_PTR(0);  /* make sure pixel_ptr is positive */
242
243         while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
244             if (rle_code == 0) {
245                 /* there's another skip code in the stream */
246                 CHECK_STREAM_PTR(1);
247                 pixel_ptr += (s->buf[stream_ptr++] - 1) * 2;
248                 CHECK_PIXEL_PTR(0);  /* make sure pixel_ptr is positive */
249             } else if (rle_code < 0) {
250                 /* decode the run length code */
251                 rle_code = -rle_code;
252                 CHECK_STREAM_PTR(2);
253                 rgb16 = AV_RB16(&s->buf[stream_ptr]);
254                 stream_ptr += 2;
255
256                 CHECK_PIXEL_PTR(rle_code * 2);
257
258                 while (rle_code--) {
259                     *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
260                     pixel_ptr += 2;
261                 }
262             } else {
263                 CHECK_STREAM_PTR(rle_code * 2);
264                 CHECK_PIXEL_PTR(rle_code * 2);
265
266                 /* copy pixels directly to output */
267                 while (rle_code--) {
268                     rgb16 = AV_RB16(&s->buf[stream_ptr]);
269                     stream_ptr += 2;
270                     *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
271                     pixel_ptr += 2;
272                 }
273             }
274         }
275         row_ptr += row_inc;
276     }
277 }
278
279 static void qtrle_decode_24bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
280 {
281     int rle_code;
282     int pixel_ptr;
283     int row_inc = s->frame.linesize[0];
284     unsigned char r, g, b;
285     unsigned char *rgb = s->frame.data[0];
286     int pixel_limit = s->frame.linesize[0] * s->avctx->height;
287
288     while (lines_to_change--) {
289         CHECK_STREAM_PTR(2);
290         pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 3;
291         CHECK_PIXEL_PTR(0);  /* make sure pixel_ptr is positive */
292
293         while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
294             if (rle_code == 0) {
295                 /* there's another skip code in the stream */
296                 CHECK_STREAM_PTR(1);
297                 pixel_ptr += (s->buf[stream_ptr++] - 1) * 3;
298                 CHECK_PIXEL_PTR(0);  /* make sure pixel_ptr is positive */
299             } else if (rle_code < 0) {
300                 /* decode the run length code */
301                 rle_code = -rle_code;
302                 CHECK_STREAM_PTR(3);
303                 r = s->buf[stream_ptr++];
304                 g = s->buf[stream_ptr++];
305                 b = s->buf[stream_ptr++];
306
307                 CHECK_PIXEL_PTR(rle_code * 3);
308
309                 while (rle_code--) {
310                     rgb[pixel_ptr++] = r;
311                     rgb[pixel_ptr++] = g;
312                     rgb[pixel_ptr++] = b;
313                 }
314             } else {
315                 CHECK_STREAM_PTR(rle_code * 3);
316                 CHECK_PIXEL_PTR(rle_code * 3);
317
318                 /* copy pixels directly to output */
319                 while (rle_code--) {
320                     rgb[pixel_ptr++] = s->buf[stream_ptr++];
321                     rgb[pixel_ptr++] = s->buf[stream_ptr++];
322                     rgb[pixel_ptr++] = s->buf[stream_ptr++];
323                 }
324             }
325         }
326         row_ptr += row_inc;
327     }
328 }
329
330 static void qtrle_decode_32bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
331 {
332     int rle_code;
333     int pixel_ptr;
334     int row_inc = s->frame.linesize[0];
335     unsigned int argb;
336     unsigned char *rgb = s->frame.data[0];
337     int pixel_limit = s->frame.linesize[0] * s->avctx->height;
338
339     while (lines_to_change--) {
340         CHECK_STREAM_PTR(2);
341         pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 4;
342         CHECK_PIXEL_PTR(0);  /* make sure pixel_ptr is positive */
343
344         while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
345             if (rle_code == 0) {
346                 /* there's another skip code in the stream */
347                 CHECK_STREAM_PTR(1);
348                 pixel_ptr += (s->buf[stream_ptr++] - 1) * 4;
349                 CHECK_PIXEL_PTR(0);  /* make sure pixel_ptr is positive */
350             } else if (rle_code < 0) {
351                 /* decode the run length code */
352                 rle_code = -rle_code;
353                 CHECK_STREAM_PTR(4);
354                 argb = AV_RB32(s->buf + stream_ptr);
355                 stream_ptr += 4;
356
357                 CHECK_PIXEL_PTR(rle_code * 4);
358
359                 while (rle_code--) {
360                     AV_WN32A(rgb + pixel_ptr, argb);
361                     pixel_ptr += 4;
362                 }
363             } else {
364                 CHECK_STREAM_PTR(rle_code * 4);
365                 CHECK_PIXEL_PTR(rle_code * 4);
366
367                 /* copy pixels directly to output */
368                 while (rle_code--) {
369                     argb = AV_RB32(s->buf + stream_ptr);
370                     AV_WN32A(rgb + pixel_ptr, argb);
371                     stream_ptr += 4;
372                     pixel_ptr  += 4;
373                 }
374             }
375         }
376         row_ptr += row_inc;
377     }
378 }
379
380 static av_cold int qtrle_decode_init(AVCodecContext *avctx)
381 {
382     QtrleContext *s = avctx->priv_data;
383
384     s->avctx = avctx;
385     switch (avctx->bits_per_coded_sample) {
386     case 1:
387     case 33:
388         avctx->pix_fmt = PIX_FMT_MONOWHITE;
389         break;
390
391     case 2:
392     case 4:
393     case 8:
394     case 34:
395     case 36:
396     case 40:
397         avctx->pix_fmt = PIX_FMT_PAL8;
398         break;
399
400     case 16:
401         avctx->pix_fmt = PIX_FMT_RGB555;
402         break;
403
404     case 24:
405         avctx->pix_fmt = PIX_FMT_RGB24;
406         break;
407
408     case 32:
409         avctx->pix_fmt = PIX_FMT_RGB32;
410         break;
411
412     default:
413         av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
414             avctx->bits_per_coded_sample);
415         break;
416     }
417
418     avcodec_get_frame_defaults(&s->frame);
419     s->frame.data[0] = NULL;
420
421     return 0;
422 }
423
424 static int qtrle_decode_frame(AVCodecContext *avctx,
425                               void *data, int *data_size,
426                               AVPacket *avpkt)
427 {
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;
434
435     s->buf = buf;
436     s->size = buf_size;
437
438     s->frame.reference = 3;
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;
444     }
445
446     /* check if this frame is even supposed to change */
447     if (s->size < 8)
448         goto done;
449
450     /* start after the chunk size */
451     stream_ptr = 4;
452
453     /* fetch the header */
454     header = AV_RB16(&s->buf[stream_ptr]);
455     stream_ptr += 2;
456
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         if (height > s->avctx->height - start_line)
466             goto done;
467     } else {
468         start_line = 0;
469         height = s->avctx->height;
470     }
471     row_ptr = s->frame.linesize[0] * start_line;
472
473     switch (avctx->bits_per_coded_sample) {
474     case 1:
475     case 33:
476         qtrle_decode_1bpp(s, stream_ptr, row_ptr, height);
477         break;
478
479     case 2:
480     case 34:
481         qtrle_decode_2n4bpp(s, stream_ptr, row_ptr, height, 2);
482         has_palette = 1;
483         break;
484
485     case 4:
486     case 36:
487         qtrle_decode_2n4bpp(s, stream_ptr, row_ptr, height, 4);
488         has_palette = 1;
489         break;
490
491     case 8:
492     case 40:
493         qtrle_decode_8bpp(s, stream_ptr, row_ptr, height);
494         has_palette = 1;
495         break;
496
497     case 16:
498         qtrle_decode_16bpp(s, stream_ptr, row_ptr, height);
499         break;
500
501     case 24:
502         qtrle_decode_24bpp(s, stream_ptr, row_ptr, height);
503         break;
504
505     case 32:
506         qtrle_decode_32bpp(s, stream_ptr, row_ptr, height);
507         break;
508
509     default:
510         av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
511             avctx->bits_per_coded_sample);
512         break;
513     }
514
515     if(has_palette) {
516         const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
517
518         if (pal) {
519             s->frame.palette_has_changed = 1;
520             memcpy(s->pal, pal, AVPALETTE_SIZE);
521         }
522
523         /* make the palette available on the way out */
524         memcpy(s->frame.data[1], s->pal, AVPALETTE_SIZE);
525     }
526
527 done:
528     *data_size = sizeof(AVFrame);
529     *(AVFrame*)data = s->frame;
530
531     /* always report that the buffer was completely consumed */
532     return buf_size;
533 }
534
535 static av_cold int qtrle_decode_end(AVCodecContext *avctx)
536 {
537     QtrleContext *s = avctx->priv_data;
538
539     if (s->frame.data[0])
540         avctx->release_buffer(avctx, &s->frame);
541
542     return 0;
543 }
544
545 AVCodec ff_qtrle_decoder = {
546     .name           = "qtrle",
547     .type           = AVMEDIA_TYPE_VIDEO,
548     .id             = CODEC_ID_QTRLE,
549     .priv_data_size = sizeof(QtrleContext),
550     .init           = qtrle_decode_init,
551     .close          = qtrle_decode_end,
552     .decode         = qtrle_decode_frame,
553     .capabilities   = CODEC_CAP_DR1,
554     .long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),
555 };
556