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