FFmpeg  2.8.15
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
flicvideo.c
Go to the documentation of this file.
1 /*
2  * FLI/FLC Animation Video Decoder
3  * Copyright (c) 2003, 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  * Autodesk Animator FLI/FLC Video Decoder
25  * by Mike Melanson (melanson@pcisys.net)
26  * for more information on the .fli/.flc file format and all of its many
27  * variations, visit:
28  * http://www.compuphase.com/flic.htm
29  *
30  * This decoder outputs PAL8/RGB555/RGB565 and maybe one day RGB24
31  * colorspace data, depending on the FLC. To use this decoder, be
32  * sure that your demuxer sends the FLI file header to the decoder via
33  * the extradata chunk in AVCodecContext. The chunk should be 128 bytes
34  * large. The only exception is for FLI files from the game "Magic Carpet",
35  * in which the header is only 12 bytes.
36  */
37 
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 
42 #include "libavutil/intreadwrite.h"
43 #include "avcodec.h"
44 #include "bytestream.h"
45 #include "internal.h"
46 #include "mathops.h"
47 
48 #define FLI_256_COLOR 4
49 #define FLI_DELTA 7
50 #define FLI_COLOR 11
51 #define FLI_LC 12
52 #define FLI_BLACK 13
53 #define FLI_BRUN 15
54 #define FLI_COPY 16
55 #define FLI_MINI 18
56 #define FLI_DTA_BRUN 25
57 #define FLI_DTA_COPY 26
58 #define FLI_DTA_LC 27
59 
60 #define FLI_TYPE_CODE (0xAF11)
61 #define FLC_FLX_TYPE_CODE (0xAF12)
62 #define FLC_DTA_TYPE_CODE (0xAF44) /* Marks an "Extended FLC" comes from Dave's Targa Animator (DTA) */
63 #define FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE (0xAF13)
64 
65 #define CHECK_PIXEL_PTR(n) \
66  if (pixel_ptr + n > pixel_limit) { \
67  av_log (s->avctx, AV_LOG_ERROR, "Invalid pixel_ptr = %d > pixel_limit = %d\n", \
68  pixel_ptr + n, pixel_limit); \
69  return AVERROR_INVALIDDATA; \
70  } \
71 
72 typedef struct FlicDecodeContext {
75 
76  unsigned int palette[256];
78  int fli_type; /* either 0xAF11 or 0xAF12, affects palette resolution */
80 
82 {
83  FlicDecodeContext *s = avctx->priv_data;
84  unsigned char *fli_header = (unsigned char *)avctx->extradata;
85  int depth;
86 
87  if (avctx->extradata_size != 0 &&
88  avctx->extradata_size != 12 &&
89  avctx->extradata_size != 128 &&
90  avctx->extradata_size != 256 &&
91  avctx->extradata_size != 904 &&
92  avctx->extradata_size != 1024) {
93  av_log(avctx, AV_LOG_ERROR, "Unexpected extradata size %d\n", avctx->extradata_size);
94  return AVERROR_INVALIDDATA;
95  }
96 
97  s->avctx = avctx;
98 
99  if (s->avctx->extradata_size == 12) {
100  /* special case for magic carpet FLIs */
102  depth = 8;
103  } else if (avctx->extradata_size == 1024) {
104  uint8_t *ptr = avctx->extradata;
105  int i;
106 
107  for (i = 0; i < 256; i++) {
108  s->palette[i] = AV_RL32(ptr);
109  ptr += 4;
110  }
111  depth = 8;
112  /* FLI in MOV, see e.g. FFmpeg trac issue #626 */
113  } else if (avctx->extradata_size == 0 ||
114  avctx->extradata_size == 256 ||
115  /* see FFmpeg ticket #1234 */
116  avctx->extradata_size == 904) {
117  s->fli_type = FLI_TYPE_CODE;
118  depth = 8;
119  } else {
120  s->fli_type = AV_RL16(&fli_header[4]);
121  depth = AV_RL16(&fli_header[12]);
122  }
123 
124  if (depth == 0) {
125  depth = 8; /* Some FLC generators set depth to zero, when they mean 8Bpp. Fix up here */
126  }
127 
128  if ((s->fli_type == FLC_FLX_TYPE_CODE) && (depth == 16)) {
129  depth = 15; /* Original Autodesk FLX's say the depth is 16Bpp when it is really 15Bpp */
130  }
131 
132  switch (depth) {
133  case 8 : avctx->pix_fmt = AV_PIX_FMT_PAL8; break;
134  case 15 : avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
135  case 16 : avctx->pix_fmt = AV_PIX_FMT_RGB565; break;
136  case 24 : avctx->pix_fmt = AV_PIX_FMT_BGR24; /* Supposedly BGR, but havent any files to test with */
137  avpriv_request_sample(avctx, "24Bpp FLC/FLX");
138  return AVERROR_PATCHWELCOME;
139  default :
140  av_log(avctx, AV_LOG_ERROR, "Unknown FLC/FLX depth of %d Bpp is unsupported.\n",depth);
141  return AVERROR_INVALIDDATA;
142  }
143 
144  s->frame = av_frame_alloc();
145  if (!s->frame)
146  return AVERROR(ENOMEM);
147 
148  s->new_palette = 0;
149 
150  return 0;
151 }
152 
154  void *data, int *got_frame,
155  const uint8_t *buf, int buf_size)
156 {
157  FlicDecodeContext *s = avctx->priv_data;
158 
159  GetByteContext g2;
160  int pixel_ptr;
161  int palette_ptr;
162  unsigned char palette_idx1;
163  unsigned char palette_idx2;
164 
165  unsigned int frame_size;
166  int num_chunks;
167 
168  unsigned int chunk_size;
169  int chunk_type;
170 
171  int i, j, ret;
172 
173  int color_packets;
174  int color_changes;
175  int color_shift;
176  unsigned char r, g, b;
177 
178  int lines;
179  int compressed_lines;
180  int starting_line;
181  signed short line_packets;
182  int y_ptr;
183  int byte_run;
184  int pixel_skip;
185  int pixel_countdown;
186  unsigned char *pixels;
187  unsigned int pixel_limit;
188 
189  bytestream2_init(&g2, buf, buf_size);
190 
191  if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
192  return ret;
193 
194  pixels = s->frame->data[0];
195  pixel_limit = s->avctx->height * s->frame->linesize[0];
196  if (buf_size < 16 || buf_size > INT_MAX - (3 * 256 + AV_INPUT_BUFFER_PADDING_SIZE))
197  return AVERROR_INVALIDDATA;
198  frame_size = bytestream2_get_le32(&g2);
199  if (frame_size > buf_size)
200  frame_size = buf_size;
201  bytestream2_skip(&g2, 2); /* skip the magic number */
202  num_chunks = bytestream2_get_le16(&g2);
203  bytestream2_skip(&g2, 8); /* skip padding */
204 
205  if (frame_size < 16)
206  return AVERROR_INVALIDDATA;
207 
208  frame_size -= 16;
209 
210  /* iterate through the chunks */
211  while ((frame_size >= 6) && (num_chunks > 0) &&
212  bytestream2_get_bytes_left(&g2) >= 4) {
213  int stream_ptr_after_chunk;
214  chunk_size = bytestream2_get_le32(&g2);
215  if (chunk_size > frame_size) {
216  av_log(avctx, AV_LOG_WARNING,
217  "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
218  chunk_size = frame_size;
219  }
220  stream_ptr_after_chunk = bytestream2_tell(&g2) - 4 + chunk_size;
221 
222  chunk_type = bytestream2_get_le16(&g2);
223 
224  switch (chunk_type) {
225  case FLI_256_COLOR:
226  case FLI_COLOR:
227  /* check special case: If this file is from the Magic Carpet
228  * game and uses 6-bit colors even though it reports 256-color
229  * chunks in a 0xAF12-type file (fli_type is set to 0xAF13 during
230  * initialization) */
231  if ((chunk_type == FLI_256_COLOR) && (s->fli_type != FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE))
232  color_shift = 0;
233  else
234  color_shift = 2;
235  /* set up the palette */
236  color_packets = bytestream2_get_le16(&g2);
237  palette_ptr = 0;
238  for (i = 0; i < color_packets; i++) {
239  /* first byte is how many colors to skip */
240  palette_ptr += bytestream2_get_byte(&g2);
241 
242  /* next byte indicates how many entries to change */
243  color_changes = bytestream2_get_byte(&g2);
244 
245  /* if there are 0 color changes, there are actually 256 */
246  if (color_changes == 0)
247  color_changes = 256;
248 
249  if (bytestream2_tell(&g2) + color_changes * 3 > stream_ptr_after_chunk)
250  break;
251 
252  for (j = 0; j < color_changes; j++) {
253  unsigned int entry;
254 
255  /* wrap around, for good measure */
256  if ((unsigned)palette_ptr >= 256)
257  palette_ptr = 0;
258 
259  r = bytestream2_get_byte(&g2) << color_shift;
260  g = bytestream2_get_byte(&g2) << color_shift;
261  b = bytestream2_get_byte(&g2) << color_shift;
262  entry = 0xFFU << 24 | r << 16 | g << 8 | b;
263  if (color_shift == 2)
264  entry |= entry >> 6 & 0x30303;
265  if (s->palette[palette_ptr] != entry)
266  s->new_palette = 1;
267  s->palette[palette_ptr++] = entry;
268  }
269  }
270  break;
271 
272  case FLI_DELTA:
273  y_ptr = 0;
274  compressed_lines = bytestream2_get_le16(&g2);
275  while (compressed_lines > 0) {
276  if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
277  break;
278  if (y_ptr > pixel_limit)
279  return AVERROR_INVALIDDATA;
280  line_packets = bytestream2_get_le16(&g2);
281  if ((line_packets & 0xC000) == 0xC000) {
282  // line skip opcode
283  line_packets = -line_packets;
284  if (line_packets > s->avctx->height)
285  return AVERROR_INVALIDDATA;
286  y_ptr += line_packets * s->frame->linesize[0];
287  } else if ((line_packets & 0xC000) == 0x4000) {
288  av_log(avctx, AV_LOG_ERROR, "Undefined opcode (%x) in DELTA_FLI\n", line_packets);
289  } else if ((line_packets & 0xC000) == 0x8000) {
290  // "last byte" opcode
291  pixel_ptr= y_ptr + s->frame->linesize[0] - 1;
292  CHECK_PIXEL_PTR(0);
293  pixels[pixel_ptr] = line_packets & 0xff;
294  } else {
295  compressed_lines--;
296  pixel_ptr = y_ptr;
297  CHECK_PIXEL_PTR(0);
298  pixel_countdown = s->avctx->width;
299  for (i = 0; i < line_packets; i++) {
300  if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
301  break;
302  /* account for the skip bytes */
303  pixel_skip = bytestream2_get_byte(&g2);
304  pixel_ptr += pixel_skip;
305  pixel_countdown -= pixel_skip;
306  byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
307  if (byte_run < 0) {
308  byte_run = -byte_run;
309  palette_idx1 = bytestream2_get_byte(&g2);
310  palette_idx2 = bytestream2_get_byte(&g2);
311  CHECK_PIXEL_PTR(byte_run * 2);
312  for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
313  pixels[pixel_ptr++] = palette_idx1;
314  pixels[pixel_ptr++] = palette_idx2;
315  }
316  } else {
317  CHECK_PIXEL_PTR(byte_run * 2);
318  if (bytestream2_tell(&g2) + byte_run * 2 > stream_ptr_after_chunk)
319  break;
320  for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
321  pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
322  }
323  }
324  }
325 
326  y_ptr += s->frame->linesize[0];
327  }
328  }
329  break;
330 
331  case FLI_LC:
332  /* line compressed */
333  starting_line = bytestream2_get_le16(&g2);
334  if (starting_line >= s->avctx->height)
335  return AVERROR_INVALIDDATA;
336  y_ptr = 0;
337  y_ptr += starting_line * s->frame->linesize[0];
338 
339  compressed_lines = bytestream2_get_le16(&g2);
340  while (compressed_lines > 0) {
341  pixel_ptr = y_ptr;
342  CHECK_PIXEL_PTR(0);
343  pixel_countdown = s->avctx->width;
344  if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
345  break;
346  line_packets = bytestream2_get_byte(&g2);
347  if (line_packets > 0) {
348  for (i = 0; i < line_packets; i++) {
349  /* account for the skip bytes */
350  if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
351  break;
352  pixel_skip = bytestream2_get_byte(&g2);
353  pixel_ptr += pixel_skip;
354  pixel_countdown -= pixel_skip;
355  byte_run = sign_extend(bytestream2_get_byte(&g2),8);
356  if (byte_run > 0) {
357  CHECK_PIXEL_PTR(byte_run);
358  if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
359  break;
360  for (j = 0; j < byte_run; j++, pixel_countdown--) {
361  pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
362  }
363  } else if (byte_run < 0) {
364  byte_run = -byte_run;
365  palette_idx1 = bytestream2_get_byte(&g2);
366  CHECK_PIXEL_PTR(byte_run);
367  for (j = 0; j < byte_run; j++, pixel_countdown--) {
368  pixels[pixel_ptr++] = palette_idx1;
369  }
370  }
371  }
372  }
373 
374  y_ptr += s->frame->linesize[0];
375  compressed_lines--;
376  }
377  break;
378 
379  case FLI_BLACK:
380  /* set the whole frame to color 0 (which is usually black) */
381  memset(pixels, 0,
382  s->frame->linesize[0] * s->avctx->height);
383  break;
384 
385  case FLI_BRUN:
386  /* Byte run compression: This chunk type only occurs in the first
387  * FLI frame and it will update the entire frame. */
388  y_ptr = 0;
389  for (lines = 0; lines < s->avctx->height; lines++) {
390  pixel_ptr = y_ptr;
391  /* disregard the line packets; instead, iterate through all
392  * pixels on a row */
393  bytestream2_skip(&g2, 1);
394  pixel_countdown = s->avctx->width;
395  while (pixel_countdown > 0) {
396  if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
397  break;
398  byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
399  if (!byte_run) {
400  av_log(avctx, AV_LOG_ERROR, "Invalid byte run value.\n");
401  return AVERROR_INVALIDDATA;
402  }
403 
404  if (byte_run > 0) {
405  palette_idx1 = bytestream2_get_byte(&g2);
406  CHECK_PIXEL_PTR(byte_run);
407  for (j = 0; j < byte_run; j++) {
408  pixels[pixel_ptr++] = palette_idx1;
409  pixel_countdown--;
410  if (pixel_countdown < 0)
411  av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
412  pixel_countdown, lines);
413  }
414  } else { /* copy bytes if byte_run < 0 */
415  byte_run = -byte_run;
416  CHECK_PIXEL_PTR(byte_run);
417  if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
418  break;
419  for (j = 0; j < byte_run; j++) {
420  pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
421  pixel_countdown--;
422  if (pixel_countdown < 0)
423  av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
424  pixel_countdown, lines);
425  }
426  }
427  }
428 
429  y_ptr += s->frame->linesize[0];
430  }
431  break;
432 
433  case FLI_COPY:
434  /* copy the chunk (uncompressed frame) */
435  if (chunk_size - 6 != s->avctx->width * s->avctx->height) {
436  av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
437  "has incorrect size, skipping chunk\n", chunk_size - 6);
438  bytestream2_skip(&g2, chunk_size - 6);
439  } else {
440  for (y_ptr = 0; y_ptr < s->frame->linesize[0] * s->avctx->height;
441  y_ptr += s->frame->linesize[0]) {
442  bytestream2_get_buffer(&g2, &pixels[y_ptr],
443  s->avctx->width);
444  }
445  }
446  break;
447 
448  case FLI_MINI:
449  /* some sort of a thumbnail? disregard this chunk... */
450  break;
451 
452  default:
453  av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
454  break;
455  }
456 
457  if (stream_ptr_after_chunk - bytestream2_tell(&g2) > 0)
458  bytestream2_skip(&g2, stream_ptr_after_chunk - bytestream2_tell(&g2));
459 
460  frame_size -= chunk_size;
461  num_chunks--;
462  }
463 
464  /* by the end of the chunk, the stream ptr should equal the frame
465  * size (minus 1 or 2, possibly); if it doesn't, issue a warning */
466  if (bytestream2_get_bytes_left(&g2) > 2)
467  av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
468  "and final chunk ptr = %d\n", buf_size,
469  buf_size - bytestream2_get_bytes_left(&g2));
470 
471  /* make the palette available on the way out */
472  memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE);
473  if (s->new_palette) {
474  s->frame->palette_has_changed = 1;
475  s->new_palette = 0;
476  }
477 
478  if ((ret = av_frame_ref(data, s->frame)) < 0)
479  return ret;
480 
481  *got_frame = 1;
482 
483  return buf_size;
484 }
485 
487  void *data, int *got_frame,
488  const uint8_t *buf, int buf_size)
489 {
490  /* Note, the only difference between the 15Bpp and 16Bpp */
491  /* Format is the pixel format, the packets are processed the same. */
492  FlicDecodeContext *s = avctx->priv_data;
493 
494  GetByteContext g2;
495  int pixel_ptr;
496  unsigned char palette_idx1;
497 
498  unsigned int frame_size;
499  int num_chunks;
500 
501  unsigned int chunk_size;
502  int chunk_type;
503 
504  int i, j, ret;
505 
506  int lines;
507  int compressed_lines;
508  signed short line_packets;
509  int y_ptr;
510  int byte_run;
511  int pixel_skip;
512  int pixel_countdown;
513  unsigned char *pixels;
514  int pixel;
515  unsigned int pixel_limit;
516 
517  bytestream2_init(&g2, buf, buf_size);
518 
519  if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
520  return ret;
521 
522  pixels = s->frame->data[0];
523  pixel_limit = s->avctx->height * s->frame->linesize[0];
524 
525  frame_size = bytestream2_get_le32(&g2);
526  bytestream2_skip(&g2, 2); /* skip the magic number */
527  num_chunks = bytestream2_get_le16(&g2);
528  bytestream2_skip(&g2, 8); /* skip padding */
529  if (frame_size > buf_size)
530  frame_size = buf_size;
531 
532  if (frame_size < 16)
533  return AVERROR_INVALIDDATA;
534  frame_size -= 16;
535 
536  /* iterate through the chunks */
537  while ((frame_size > 0) && (num_chunks > 0) &&
538  bytestream2_get_bytes_left(&g2) >= 4) {
539  int stream_ptr_after_chunk;
540  chunk_size = bytestream2_get_le32(&g2);
541  if (chunk_size > frame_size) {
542  av_log(avctx, AV_LOG_WARNING,
543  "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
544  chunk_size = frame_size;
545  }
546  stream_ptr_after_chunk = bytestream2_tell(&g2) - 4 + chunk_size;
547 
548  chunk_type = bytestream2_get_le16(&g2);
549 
550 
551  switch (chunk_type) {
552  case FLI_256_COLOR:
553  case FLI_COLOR:
554  /* For some reason, it seems that non-palettized flics do
555  * include one of these chunks in their first frame.
556  * Why I do not know, it seems rather extraneous. */
557  ff_dlog(avctx,
558  "Unexpected Palette chunk %d in non-palettized FLC\n",
559  chunk_type);
560  bytestream2_skip(&g2, chunk_size - 6);
561  break;
562 
563  case FLI_DELTA:
564  case FLI_DTA_LC:
565  y_ptr = 0;
566  compressed_lines = bytestream2_get_le16(&g2);
567  while (compressed_lines > 0) {
568  if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
569  break;
570  if (y_ptr > pixel_limit)
571  return AVERROR_INVALIDDATA;
572  line_packets = bytestream2_get_le16(&g2);
573  if (line_packets < 0) {
574  line_packets = -line_packets;
575  if (line_packets > s->avctx->height)
576  return AVERROR_INVALIDDATA;
577  y_ptr += line_packets * s->frame->linesize[0];
578  } else {
579  compressed_lines--;
580  pixel_ptr = y_ptr;
581  CHECK_PIXEL_PTR(0);
582  pixel_countdown = s->avctx->width;
583  for (i = 0; i < line_packets; i++) {
584  /* account for the skip bytes */
585  if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
586  break;
587  pixel_skip = bytestream2_get_byte(&g2);
588  pixel_ptr += (pixel_skip*2); /* Pixel is 2 bytes wide */
589  pixel_countdown -= pixel_skip;
590  byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
591  if (byte_run < 0) {
592  byte_run = -byte_run;
593  pixel = bytestream2_get_le16(&g2);
594  CHECK_PIXEL_PTR(2 * byte_run);
595  for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
596  *((signed short*)(&pixels[pixel_ptr])) = pixel;
597  pixel_ptr += 2;
598  }
599  } else {
600  if (bytestream2_tell(&g2) + 2*byte_run > stream_ptr_after_chunk)
601  break;
602  CHECK_PIXEL_PTR(2 * byte_run);
603  for (j = 0; j < byte_run; j++, pixel_countdown--) {
604  *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
605  pixel_ptr += 2;
606  }
607  }
608  }
609 
610  y_ptr += s->frame->linesize[0];
611  }
612  }
613  break;
614 
615  case FLI_LC:
616  av_log(avctx, AV_LOG_ERROR, "Unexpected FLI_LC chunk in non-palettized FLC\n");
617  bytestream2_skip(&g2, chunk_size - 6);
618  break;
619 
620  case FLI_BLACK:
621  /* set the whole frame to 0x0000 which is black in both 15Bpp and 16Bpp modes. */
622  memset(pixels, 0x0000,
623  s->frame->linesize[0] * s->avctx->height);
624  break;
625 
626  case FLI_BRUN:
627  y_ptr = 0;
628  for (lines = 0; lines < s->avctx->height; lines++) {
629  pixel_ptr = y_ptr;
630  /* disregard the line packets; instead, iterate through all
631  * pixels on a row */
632  bytestream2_skip(&g2, 1);
633  pixel_countdown = (s->avctx->width * 2);
634 
635  while (pixel_countdown > 0) {
636  if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
637  break;
638  byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
639  if (byte_run > 0) {
640  palette_idx1 = bytestream2_get_byte(&g2);
641  CHECK_PIXEL_PTR(byte_run);
642  for (j = 0; j < byte_run; j++) {
643  pixels[pixel_ptr++] = palette_idx1;
644  pixel_countdown--;
645  if (pixel_countdown < 0)
646  av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) (linea%d)\n",
647  pixel_countdown, lines);
648  }
649  } else { /* copy bytes if byte_run < 0 */
650  byte_run = -byte_run;
651  if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
652  break;
653  CHECK_PIXEL_PTR(byte_run);
654  for (j = 0; j < byte_run; j++) {
655  palette_idx1 = bytestream2_get_byte(&g2);
656  pixels[pixel_ptr++] = palette_idx1;
657  pixel_countdown--;
658  if (pixel_countdown < 0)
659  av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
660  pixel_countdown, lines);
661  }
662  }
663  }
664 
665  /* Now FLX is strange, in that it is "byte" as opposed to "pixel" run length compressed.
666  * This does not give us any good opportunity to perform word endian conversion
667  * during decompression. So if it is required (i.e., this is not a LE target, we do
668  * a second pass over the line here, swapping the bytes.
669  */
670 #if HAVE_BIGENDIAN
671  pixel_ptr = y_ptr;
672  pixel_countdown = s->avctx->width;
673  while (pixel_countdown > 0) {
674  *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[pixel_ptr]);
675  pixel_ptr += 2;
676  }
677 #endif
678  y_ptr += s->frame->linesize[0];
679  }
680  break;
681 
682  case FLI_DTA_BRUN:
683  y_ptr = 0;
684  for (lines = 0; lines < s->avctx->height; lines++) {
685  pixel_ptr = y_ptr;
686  /* disregard the line packets; instead, iterate through all
687  * pixels on a row */
688  bytestream2_skip(&g2, 1);
689  pixel_countdown = s->avctx->width; /* Width is in pixels, not bytes */
690 
691  while (pixel_countdown > 0) {
692  if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
693  break;
694  byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
695  if (byte_run > 0) {
696  pixel = bytestream2_get_le16(&g2);
697  CHECK_PIXEL_PTR(2 * byte_run);
698  for (j = 0; j < byte_run; j++) {
699  *((signed short*)(&pixels[pixel_ptr])) = pixel;
700  pixel_ptr += 2;
701  pixel_countdown--;
702  if (pixel_countdown < 0)
703  av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
704  pixel_countdown);
705  }
706  } else { /* copy pixels if byte_run < 0 */
707  byte_run = -byte_run;
708  if (bytestream2_tell(&g2) + 2 * byte_run > stream_ptr_after_chunk)
709  break;
710  CHECK_PIXEL_PTR(2 * byte_run);
711  for (j = 0; j < byte_run; j++) {
712  *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
713  pixel_ptr += 2;
714  pixel_countdown--;
715  if (pixel_countdown < 0)
716  av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
717  pixel_countdown);
718  }
719  }
720  }
721 
722  y_ptr += s->frame->linesize[0];
723  }
724  break;
725 
726  case FLI_COPY:
727  case FLI_DTA_COPY:
728  /* copy the chunk (uncompressed frame) */
729  if (chunk_size - 6 > (unsigned int)(s->avctx->width * s->avctx->height)*2) {
730  av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
731  "bigger than image, skipping chunk\n", chunk_size - 6);
732  bytestream2_skip(&g2, chunk_size - 6);
733  } else {
734 
735  for (y_ptr = 0; y_ptr < s->frame->linesize[0] * s->avctx->height;
736  y_ptr += s->frame->linesize[0]) {
737 
738  pixel_countdown = s->avctx->width;
739  pixel_ptr = 0;
740  while (pixel_countdown > 0) {
741  *((signed short*)(&pixels[y_ptr + pixel_ptr])) = bytestream2_get_le16(&g2);
742  pixel_ptr += 2;
743  pixel_countdown--;
744  }
745  }
746  }
747  break;
748 
749  case FLI_MINI:
750  /* some sort of a thumbnail? disregard this chunk... */
751  bytestream2_skip(&g2, chunk_size - 6);
752  break;
753 
754  default:
755  av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
756  break;
757  }
758 
759  frame_size -= chunk_size;
760  num_chunks--;
761  }
762 
763  /* by the end of the chunk, the stream ptr should equal the frame
764  * size (minus 1, possibly); if it doesn't, issue a warning */
765  if ((bytestream2_get_bytes_left(&g2) != 0) && (bytestream2_get_bytes_left(&g2) != 1))
766  av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
767  "and final chunk ptr = %d\n", buf_size, bytestream2_tell(&g2));
768 
769  if ((ret = av_frame_ref(data, s->frame)) < 0)
770  return ret;
771 
772  *got_frame = 1;
773 
774  return buf_size;
775 }
776 
778  void *data, int *got_frame,
779  const uint8_t *buf, int buf_size)
780 {
781  av_log(avctx, AV_LOG_ERROR, "24Bpp FLC Unsupported due to lack of test files.\n");
782  return AVERROR_PATCHWELCOME;
783 }
784 
786  void *data, int *got_frame,
787  AVPacket *avpkt)
788 {
789  const uint8_t *buf = avpkt->data;
790  int buf_size = avpkt->size;
791  if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
792  return flic_decode_frame_8BPP(avctx, data, got_frame,
793  buf, buf_size);
794  }
795  else if ((avctx->pix_fmt == AV_PIX_FMT_RGB555) ||
796  (avctx->pix_fmt == AV_PIX_FMT_RGB565)) {
797  return flic_decode_frame_15_16BPP(avctx, data, got_frame,
798  buf, buf_size);
799  }
800  else if (avctx->pix_fmt == AV_PIX_FMT_BGR24) {
801  return flic_decode_frame_24BPP(avctx, data, got_frame,
802  buf, buf_size);
803  }
804 
805  /* Should not get here, ever as the pix_fmt is processed */
806  /* in flic_decode_init and the above if should deal with */
807  /* the finite set of possibilites allowable by here. */
808  /* But in case we do, just error out. */
809  av_log(avctx, AV_LOG_ERROR, "Unknown FLC format, my science cannot explain how this happened.\n");
810  return AVERROR_BUG;
811 }
812 
813 
815 {
816  FlicDecodeContext *s = avctx->priv_data;
817 
818  av_frame_free(&s->frame);
819 
820  return 0;
821 }
822 
824  .name = "flic",
825  .long_name = NULL_IF_CONFIG_SMALL("Autodesk Animator Flic video"),
826  .type = AVMEDIA_TYPE_VIDEO,
827  .id = AV_CODEC_ID_FLIC,
828  .priv_data_size = sizeof(FlicDecodeContext),
830  .close = flic_decode_end,
832  .capabilities = AV_CODEC_CAP_DR1,
833 };
#define FLI_DTA_COPY
Definition: flicvideo.c:57
AVCodecContext * avctx
Definition: flicvideo.c:73
const char * s
Definition: avisynth_c.h:631
#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:171
#define FLI_TYPE_CODE
Definition: flicvideo.c:60
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
#define FLI_MINI
Definition: flicvideo.c:55
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define CHECK_PIXEL_PTR(n)
Definition: flicvideo.c:65
const char * g
Definition: vf_curves.c:108
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define FLI_COPY
Definition: flicvideo.c:54
int size
Definition: avcodec.h:1434
const char * b
Definition: vf_curves.c:109
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1732
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
#define AV_RL16
Definition: intreadwrite.h:42
static int flic_decode_frame_15_16BPP(AVCodecContext *avctx, void *data, int *got_frame, const uint8_t *buf, int buf_size)
Definition: flicvideo.c:486
AVCodec.
Definition: avcodec.h:3482
static int flic_decode_frame_8BPP(AVCodecContext *avctx, void *data, int *got_frame, const uint8_t *buf, int buf_size)
Definition: flicvideo.c:153
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
uint8_t
#define av_cold
Definition: attributes.h:74
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:135
8 bit with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:74
#define AVPALETTE_SIZE
Definition: pixfmt.h:33
#define FLI_COLOR
Definition: flicvideo.c:50
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:366
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1627
uint8_t * data
Definition: avcodec.h:1433
#define ff_dlog(a,...)
#define FLI_LC
Definition: flicvideo.c:51
#define av_log(a,...)
#define U(x)
Definition: vp56_arith.h:37
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define FLI_BRUN
Definition: flicvideo.c:53
AVFrame * frame
Definition: flicvideo.c:74
#define AVERROR(e)
Definition: error.h:43
static int flic_decode_frame_24BPP(AVCodecContext *avctx, void *data, int *got_frame, const uint8_t *buf, int buf_size)
Definition: flicvideo.c:777
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:148
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:178
const char * r
Definition: vf_curves.c:107
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:263
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
#define FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE
Definition: flicvideo.c:63
const char * name
Name of the codec implementation.
Definition: avcodec.h:3489
Libavcodec external API header.
int depth
Definition: v4l.c:62
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...
Definition: utils.c:1097
#define FLI_DTA_BRUN
Definition: flicvideo.c:56
int width
picture width / height.
Definition: avcodec.h:1691
#define FLC_FLX_TYPE_CODE
Definition: flicvideo.c:61
#define FLI_256_COLOR
Definition: flicvideo.c:48
#define AV_RL32
Definition: intreadwrite.h:146
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:66
#define FLI_DELTA
Definition: flicvideo.c:49
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:188
int frame_size
Definition: mxfenc.c:1819
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
main external API structure.
Definition: avcodec.h:1512
void * buf
Definition: avisynth_c.h:553
#define FLI_DTA_LC
Definition: flicvideo.c:58
int extradata_size
Definition: avcodec.h:1628
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
static int flic_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: flicvideo.c:785
static av_cold int flic_decode_end(AVCodecContext *avctx)
Definition: flicvideo.c:814
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:377
uint8_t pixel
Definition: tiny_ssim.c:42
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:138
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
#define FLI_BLACK
Definition: flicvideo.c:52
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:521
common internal api header.
if(ret< 0)
Definition: vf_mcdeint.c:280
#define AV_PIX_FMT_RGB555
Definition: pixfmt.h:368
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:636
void * priv_data
Definition: avcodec.h:1554
int pixels
Definition: avisynth_c.h:298
unsigned int palette[256]
Definition: flicvideo.c:76
AVCodec ff_flic_decoder
Definition: flicvideo.c:823
#define AV_PIX_FMT_RGB565
Definition: pixfmt.h:367
This structure stores compressed data.
Definition: avcodec.h:1410
static av_cold int flic_decode_init(AVCodecContext *avctx)
Definition: flicvideo.c:81
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:857