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