qtrle.c
Go to the documentation of this file.
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 "avcodec.h"
39 #include "bytestream.h"
40 #include "internal.h"
41 
42 typedef struct QtrleContext {
45 
47  uint32_t pal[256];
48 } QtrleContext;
49 
50 #define CHECK_PIXEL_PTR(n) \
51  if ((pixel_ptr + n > pixel_limit) || (pixel_ptr + n < 0)) { \
52  av_log (s->avctx, AV_LOG_ERROR, "Problem: pixel_ptr = %d, pixel_limit = %d\n",\
53  pixel_ptr + n, pixel_limit); \
54  return; \
55  } \
56 
57 static void qtrle_decode_1bpp(QtrleContext *s, int row_ptr, int lines_to_change)
58 {
59  int rle_code;
60  int pixel_ptr;
61  int row_inc = s->frame.linesize[0];
62  unsigned char pi0, pi1; /* 2 8-pixel values */
63  unsigned char *rgb = s->frame.data[0];
64  int pixel_limit = s->frame.linesize[0] * s->avctx->height;
65  int skip;
66  /* skip & 0x80 appears to mean 'start a new line', which can be interpreted
67  * as 'go to next line' during the decoding of a frame but is 'go to first
68  * line' at the beginning. Since we always interpret it as 'go to next line'
69  * in the decoding loop (which makes code simpler/faster), the first line
70  * would not be counted, so we count one more.
71  * See: https://ffmpeg.org/trac/ffmpeg/ticket/226
72  * In the following decoding loop, row_ptr will be the position of the
73  * current row. */
74 
75  row_ptr -= row_inc;
76  pixel_ptr = row_ptr;
77  lines_to_change++;
78  while (lines_to_change) {
79  skip = bytestream2_get_byte(&s->g);
80  rle_code = (signed char)bytestream2_get_byte(&s->g);
81  if (rle_code == 0)
82  break;
83  if(skip & 0x80) {
84  lines_to_change--;
85  row_ptr += row_inc;
86  pixel_ptr = row_ptr + 2 * (skip & 0x7f);
87  } else
88  pixel_ptr += 2 * skip;
89  CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
90 
91  if(rle_code == -1)
92  continue;
93 
94  if (rle_code < 0) {
95  /* decode the run length code */
96  rle_code = -rle_code;
97  /* get the next 2 bytes from the stream, treat them as groups
98  * of 8 pixels, and output them rle_code times */
99 
100  pi0 = bytestream2_get_byte(&s->g);
101  pi1 = bytestream2_get_byte(&s->g);
102  CHECK_PIXEL_PTR(rle_code * 2);
103 
104  while (rle_code--) {
105  rgb[pixel_ptr++] = pi0;
106  rgb[pixel_ptr++] = pi1;
107  }
108  } else {
109  /* copy the same pixel directly to output 2 times */
110  rle_code *= 2;
111  CHECK_PIXEL_PTR(rle_code);
112 
113  while (rle_code--)
114  rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
115  }
116  }
117 }
118 
119 static inline void qtrle_decode_2n4bpp(QtrleContext *s, int row_ptr,
120  int lines_to_change, int bpp)
121 {
122  int rle_code, i;
123  int pixel_ptr;
124  int row_inc = s->frame.linesize[0];
125  unsigned char pi[16]; /* 16 palette indices */
126  unsigned char *rgb = s->frame.data[0];
127  int pixel_limit = s->frame.linesize[0] * s->avctx->height;
128  int num_pixels = (bpp == 4) ? 8 : 16;
129 
130  while (lines_to_change--) {
131  pixel_ptr = row_ptr + (num_pixels * (bytestream2_get_byte(&s->g) - 1));
132  CHECK_PIXEL_PTR(0);
133 
134  while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
135  if (rle_code == 0) {
136  /* there's another skip code in the stream */
137  pixel_ptr += (num_pixels * (bytestream2_get_byte(&s->g) - 1));
138  CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
139  } else if (rle_code < 0) {
140  /* decode the run length code */
141  rle_code = -rle_code;
142  /* get the next 4 bytes from the stream, treat them as palette
143  * indexes, and output them rle_code times */
144  for (i = num_pixels-1; i >= 0; i--) {
145  pi[num_pixels-1-i] = (bytestream2_peek_byte(&s->g) >> ((i*bpp) & 0x07)) & ((1<<bpp)-1);
146  bytestream2_skip(&s->g, ((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_PIXEL_PTR(rle_code*(num_pixels>>2));
157  while (rle_code--) {
158  if(bpp == 4) {
159  int x = bytestream2_get_byte(&s->g);
160  rgb[pixel_ptr++] = (x >> 4) & 0x0f;
161  rgb[pixel_ptr++] = x & 0x0f;
162  } else {
163  int x = bytestream2_get_byte(&s->g);
164  rgb[pixel_ptr++] = (x >> 6) & 0x03;
165  rgb[pixel_ptr++] = (x >> 4) & 0x03;
166  rgb[pixel_ptr++] = (x >> 2) & 0x03;
167  rgb[pixel_ptr++] = x & 0x03;
168  }
169  }
170  }
171  }
172  row_ptr += row_inc;
173  }
174 }
175 
176 static void qtrle_decode_8bpp(QtrleContext *s, int row_ptr, int lines_to_change)
177 {
178  int rle_code;
179  int pixel_ptr;
180  int row_inc = s->frame.linesize[0];
181  unsigned char pi1, pi2, pi3, pi4; /* 4 palette indexes */
182  unsigned char *rgb = s->frame.data[0];
183  int pixel_limit = s->frame.linesize[0] * s->avctx->height;
184 
185  while (lines_to_change--) {
186  pixel_ptr = row_ptr + (4 * (bytestream2_get_byte(&s->g) - 1));
187  CHECK_PIXEL_PTR(0);
188 
189  while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
190  if (rle_code == 0) {
191  /* there's another skip code in the stream */
192  pixel_ptr += (4 * (bytestream2_get_byte(&s->g) - 1));
193  CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
194  } else if (rle_code < 0) {
195  /* decode the run length code */
196  rle_code = -rle_code;
197  /* get the next 4 bytes from the stream, treat them as palette
198  * indexes, and output them rle_code times */
199  pi1 = bytestream2_get_byte(&s->g);
200  pi2 = bytestream2_get_byte(&s->g);
201  pi3 = bytestream2_get_byte(&s->g);
202  pi4 = bytestream2_get_byte(&s->g);
203 
204  CHECK_PIXEL_PTR(rle_code * 4);
205 
206  while (rle_code--) {
207  rgb[pixel_ptr++] = pi1;
208  rgb[pixel_ptr++] = pi2;
209  rgb[pixel_ptr++] = pi3;
210  rgb[pixel_ptr++] = pi4;
211  }
212  } else {
213  /* copy the same pixel directly to output 4 times */
214  rle_code *= 4;
215  CHECK_PIXEL_PTR(rle_code);
216 
217  while (rle_code--) {
218  rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
219  }
220  }
221  }
222  row_ptr += row_inc;
223  }
224 }
225 
226 static void qtrle_decode_16bpp(QtrleContext *s, int row_ptr, int lines_to_change)
227 {
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;
234 
235  while (lines_to_change--) {
236  pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 2;
237  CHECK_PIXEL_PTR(0);
238 
239  while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
240  if (rle_code == 0) {
241  /* there's another skip code in the stream */
242  pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 2;
243  CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
244  } else if (rle_code < 0) {
245  /* decode the run length code */
246  rle_code = -rle_code;
247  rgb16 = bytestream2_get_be16(&s->g);
248 
249  CHECK_PIXEL_PTR(rle_code * 2);
250 
251  while (rle_code--) {
252  *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
253  pixel_ptr += 2;
254  }
255  } else {
256  CHECK_PIXEL_PTR(rle_code * 2);
257 
258  /* copy pixels directly to output */
259  while (rle_code--) {
260  rgb16 = bytestream2_get_be16(&s->g);
261  *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
262  pixel_ptr += 2;
263  }
264  }
265  }
266  row_ptr += row_inc;
267  }
268 }
269 
270 static void qtrle_decode_24bpp(QtrleContext *s, int row_ptr, int lines_to_change)
271 {
272  int rle_code;
273  int pixel_ptr;
274  int row_inc = s->frame.linesize[0];
275  unsigned char r, g, b;
276  unsigned char *rgb = s->frame.data[0];
277  int pixel_limit = s->frame.linesize[0] * s->avctx->height;
278 
279  while (lines_to_change--) {
280  pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 3;
281  CHECK_PIXEL_PTR(0);
282 
283  while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
284  if (rle_code == 0) {
285  /* there's another skip code in the stream */
286  pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 3;
287  CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
288  } else if (rle_code < 0) {
289  /* decode the run length code */
290  rle_code = -rle_code;
291  r = bytestream2_get_byte(&s->g);
292  g = bytestream2_get_byte(&s->g);
293  b = bytestream2_get_byte(&s->g);
294 
295  CHECK_PIXEL_PTR(rle_code * 3);
296 
297  while (rle_code--) {
298  rgb[pixel_ptr++] = r;
299  rgb[pixel_ptr++] = g;
300  rgb[pixel_ptr++] = b;
301  }
302  } else {
303  CHECK_PIXEL_PTR(rle_code * 3);
304 
305  /* copy pixels directly to output */
306  while (rle_code--) {
307  rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
308  rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
309  rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
310  }
311  }
312  }
313  row_ptr += row_inc;
314  }
315 }
316 
317 static void qtrle_decode_32bpp(QtrleContext *s, int row_ptr, int lines_to_change)
318 {
319  int rle_code;
320  int pixel_ptr;
321  int row_inc = s->frame.linesize[0];
322  unsigned int argb;
323  unsigned char *rgb = s->frame.data[0];
324  int pixel_limit = s->frame.linesize[0] * s->avctx->height;
325 
326  while (lines_to_change--) {
327  pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 4;
328  CHECK_PIXEL_PTR(0);
329 
330  while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
331  if (rle_code == 0) {
332  /* there's another skip code in the stream */
333  pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 4;
334  CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
335  } else if (rle_code < 0) {
336  /* decode the run length code */
337  rle_code = -rle_code;
338  argb = bytestream2_get_be32(&s->g);
339 
340  CHECK_PIXEL_PTR(rle_code * 4);
341 
342  while (rle_code--) {
343  AV_WN32A(rgb + pixel_ptr, argb);
344  pixel_ptr += 4;
345  }
346  } else {
347  CHECK_PIXEL_PTR(rle_code * 4);
348 
349  /* copy pixels directly to output */
350  while (rle_code--) {
351  argb = bytestream2_get_be32(&s->g);
352  AV_WN32A(rgb + pixel_ptr, argb);
353  pixel_ptr += 4;
354  }
355  }
356  }
357  row_ptr += row_inc;
358  }
359 }
360 
362 {
363  QtrleContext *s = avctx->priv_data;
364 
365  s->avctx = avctx;
366  switch (avctx->bits_per_coded_sample) {
367  case 1:
368  case 33:
369  avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
370  break;
371 
372  case 2:
373  case 4:
374  case 8:
375  case 34:
376  case 36:
377  case 40:
378  avctx->pix_fmt = AV_PIX_FMT_PAL8;
379  break;
380 
381  case 16:
382  avctx->pix_fmt = AV_PIX_FMT_RGB555;
383  break;
384 
385  case 24:
386  avctx->pix_fmt = AV_PIX_FMT_RGB24;
387  break;
388 
389  case 32:
390  avctx->pix_fmt = AV_PIX_FMT_RGB32;
391  break;
392 
393  default:
394  av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
395  avctx->bits_per_coded_sample);
396  return AVERROR_INVALIDDATA;
397  }
398 
400 
401  return 0;
402 }
403 
405  void *data, int *got_frame,
406  AVPacket *avpkt)
407 {
408  QtrleContext *s = avctx->priv_data;
409  int header, start_line;
410  int height, row_ptr;
411  int has_palette = 0;
412  int ret;
413 
414  bytestream2_init(&s->g, avpkt->data, avpkt->size);
415  if ((ret = ff_reget_buffer(avctx, &s->frame)) < 0)
416  return ret;
417 
418  /* check if this frame is even supposed to change */
419  if (avpkt->size < 8)
420  goto done;
421 
422  /* start after the chunk size */
423  bytestream2_seek(&s->g, 4, SEEK_SET);
424 
425  /* fetch the header */
426  header = bytestream2_get_be16(&s->g);
427 
428  /* if a header is present, fetch additional decoding parameters */
429  if (header & 0x0008) {
430  if (avpkt->size < 14)
431  goto done;
432  start_line = bytestream2_get_be16(&s->g);
433  bytestream2_skip(&s->g, 2);
434  height = bytestream2_get_be16(&s->g);
435  bytestream2_skip(&s->g, 2);
436  if (height > s->avctx->height - start_line)
437  goto done;
438  } else {
439  start_line = 0;
440  height = s->avctx->height;
441  }
442  row_ptr = s->frame.linesize[0] * start_line;
443 
444  switch (avctx->bits_per_coded_sample) {
445  case 1:
446  case 33:
447  qtrle_decode_1bpp(s, row_ptr, height);
448  break;
449 
450  case 2:
451  case 34:
452  qtrle_decode_2n4bpp(s, row_ptr, height, 2);
453  has_palette = 1;
454  break;
455 
456  case 4:
457  case 36:
458  qtrle_decode_2n4bpp(s, row_ptr, height, 4);
459  has_palette = 1;
460  break;
461 
462  case 8:
463  case 40:
464  qtrle_decode_8bpp(s, row_ptr, height);
465  has_palette = 1;
466  break;
467 
468  case 16:
469  qtrle_decode_16bpp(s, row_ptr, height);
470  break;
471 
472  case 24:
473  qtrle_decode_24bpp(s, row_ptr, height);
474  break;
475 
476  case 32:
477  qtrle_decode_32bpp(s, row_ptr, height);
478  break;
479 
480  default:
481  av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
482  avctx->bits_per_coded_sample);
483  break;
484  }
485 
486  if(has_palette) {
488 
489  if (pal) {
490  s->frame.palette_has_changed = 1;
491  memcpy(s->pal, pal, AVPALETTE_SIZE);
492  }
493 
494  /* make the palette available on the way out */
495  memcpy(s->frame.data[1], s->pal, AVPALETTE_SIZE);
496  }
497 
498 done:
499  if ((ret = av_frame_ref(data, &s->frame)) < 0)
500  return ret;
501  *got_frame = 1;
502 
503  /* always report that the buffer was completely consumed */
504  return avpkt->size;
505 }
506 
508 {
509  QtrleContext *s = avctx->priv_data;
510 
511  av_frame_unref(&s->frame);
512 
513  return 0;
514 }
515 
517  .name = "qtrle",
518  .type = AVMEDIA_TYPE_VIDEO,
519  .id = AV_CODEC_ID_QTRLE,
520  .priv_data_size = sizeof(QtrleContext),
524  .capabilities = CODEC_CAP_DR1,
525  .long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),
526 };
AVFrame frame
Definition: qtrle.c:44
static void qtrle_decode_2n4bpp(QtrleContext *s, int row_ptr, int lines_to_change, int bpp)
Definition: qtrle.c:119
const char * s
Definition: avisynth_c.h:668
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
GetByteContext g
Definition: qtrle.c:46
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:70
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:130
#define AV_WN32A(p, v)
Definition: intreadwrite.h:530
uint8_t
#define av_cold
Definition: attributes.h:78
static void qtrle_decode_24bpp(QtrleContext *s, int row_ptr, int lines_to_change)
Definition: qtrle.c:270
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:79
#define AVPALETTE_SIZE
Definition: pixfmt.h:33
#define b
Definition: input.c:42
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
AVCodecContext * avctx
Definition: qtrle.c:43
uint8_t * data
static av_cold int qtrle_decode_init(AVCodecContext *avctx)
Definition: qtrle.c:361
static void qtrle_decode_1bpp(QtrleContext *s, int row_ptr, int lines_to_change)
Definition: qtrle.c:57
struct QtrleContext QtrleContext
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Discrete Time axis x
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:159
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Spectrum Plot time data
const char * r
Definition: vf_curves.c:94
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
const char * name
Name of the codec implementation.
external API header
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
Identical in function to av_frame_make_writable(), except it uses ff_get_buffer() to allocate the buf...
ret
Definition: avfilter.c:821
static void qtrle_decode_8bpp(QtrleContext *s, int row_ptr, int lines_to_change)
Definition: qtrle.c:176
uint32_t pal[256]
Definition: qtrle.c:47
#define CHECK_PIXEL_PTR(n)
Definition: qtrle.c:50
NULL
Definition: eval.c:55
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
main external API structure.
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:375
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:259
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
synthesis window for stochastic i
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:280
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:330
static void qtrle_decode_32bpp(QtrleContext *s, int row_ptr, int lines_to_change)
Definition: qtrle.c:317
int av_frame_ref(AVFrame *dst, AVFrame *src)
Setup a new reference to the data described by an given frame.
Definition: frame.c:228
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
common internal api header.
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:77
static int qtrle_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: qtrle.c:404
#define AV_PIX_FMT_RGB555
Definition: pixfmt.h:269
AVCodec ff_qtrle_decoder
Definition: qtrle.c:516
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:203
uint8_t * av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:289
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
static void qtrle_decode_16bpp(QtrleContext *s, int row_ptr, int lines_to_change)
Definition: qtrle.c:226
static av_cold int qtrle_decode_end(AVCodecContext *avctx)
Definition: qtrle.c:507
This structure stores compressed data.