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  av_log(avctx, AV_LOG_ERROR, "24Bpp FLC/FLX is unsupported due to no test files.\n");
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 
145  s->new_palette = 0;
146 
147  return 0;
148 }
149 
151  void *data, int *got_frame,
152  const uint8_t *buf, int buf_size)
153 {
154  FlicDecodeContext *s = avctx->priv_data;
155 
156  GetByteContext g2;
157  int pixel_ptr;
158  int palette_ptr;
159  unsigned char palette_idx1;
160  unsigned char palette_idx2;
161 
162  unsigned int frame_size;
163  int num_chunks;
164 
165  unsigned int chunk_size;
166  int chunk_type;
167 
168  int i, j, ret;
169 
170  int color_packets;
171  int color_changes;
172  int color_shift;
173  unsigned char r, g, b;
174 
175  int lines;
176  int compressed_lines;
177  int starting_line;
178  signed short line_packets;
179  int y_ptr;
180  int byte_run;
181  int pixel_skip;
182  int pixel_countdown;
183  unsigned char *pixels;
184  unsigned int pixel_limit;
185 
186  bytestream2_init(&g2, buf, buf_size);
187 
188  if ((ret = ff_reget_buffer(avctx, &s->frame)) < 0)
189  return ret;
190 
191  pixels = s->frame.data[0];
192  pixel_limit = s->avctx->height * s->frame.linesize[0];
193  if (buf_size < 16 || buf_size > INT_MAX - (3 * 256 + FF_INPUT_BUFFER_PADDING_SIZE))
194  return AVERROR_INVALIDDATA;
195  frame_size = bytestream2_get_le32(&g2);
196  if (frame_size > buf_size)
197  frame_size = buf_size;
198  bytestream2_skip(&g2, 2); /* skip the magic number */
199  num_chunks = bytestream2_get_le16(&g2);
200  bytestream2_skip(&g2, 8); /* skip padding */
201 
202  frame_size -= 16;
203 
204  /* iterate through the chunks */
205  while ((frame_size >= 6) && (num_chunks > 0)) {
206  int stream_ptr_after_chunk;
207  chunk_size = bytestream2_get_le32(&g2);
208  if (chunk_size > frame_size) {
209  av_log(avctx, AV_LOG_WARNING,
210  "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
211  chunk_size = frame_size;
212  }
213  stream_ptr_after_chunk = bytestream2_tell(&g2) - 4 + chunk_size;
214 
215  chunk_type = bytestream2_get_le16(&g2);
216 
217  switch (chunk_type) {
218  case FLI_256_COLOR:
219  case FLI_COLOR:
220  /* check special case: If this file is from the Magic Carpet
221  * game and uses 6-bit colors even though it reports 256-color
222  * chunks in a 0xAF12-type file (fli_type is set to 0xAF13 during
223  * initialization) */
224  if ((chunk_type == FLI_256_COLOR) && (s->fli_type != FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE))
225  color_shift = 0;
226  else
227  color_shift = 2;
228  /* set up the palette */
229  color_packets = bytestream2_get_le16(&g2);
230  palette_ptr = 0;
231  for (i = 0; i < color_packets; i++) {
232  /* first byte is how many colors to skip */
233  palette_ptr += bytestream2_get_byte(&g2);
234 
235  /* next byte indicates how many entries to change */
236  color_changes = bytestream2_get_byte(&g2);
237 
238  /* if there are 0 color changes, there are actually 256 */
239  if (color_changes == 0)
240  color_changes = 256;
241 
242  if (bytestream2_tell(&g2) + color_changes * 3 > stream_ptr_after_chunk)
243  break;
244 
245  for (j = 0; j < color_changes; j++) {
246  unsigned int entry;
247 
248  /* wrap around, for good measure */
249  if ((unsigned)palette_ptr >= 256)
250  palette_ptr = 0;
251 
252  r = bytestream2_get_byte(&g2) << color_shift;
253  g = bytestream2_get_byte(&g2) << color_shift;
254  b = bytestream2_get_byte(&g2) << color_shift;
255  entry = 0xFFU << 24 | r << 16 | g << 8 | b;
256  if (color_shift == 2)
257  entry |= entry >> 6 & 0x30303;
258  if (s->palette[palette_ptr] != entry)
259  s->new_palette = 1;
260  s->palette[palette_ptr++] = entry;
261  }
262  }
263  break;
264 
265  case FLI_DELTA:
266  y_ptr = 0;
267  compressed_lines = bytestream2_get_le16(&g2);
268  while (compressed_lines > 0) {
269  if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
270  break;
271  line_packets = bytestream2_get_le16(&g2);
272  if ((line_packets & 0xC000) == 0xC000) {
273  // line skip opcode
274  line_packets = -line_packets;
275  y_ptr += line_packets * s->frame.linesize[0];
276  } else if ((line_packets & 0xC000) == 0x4000) {
277  av_log(avctx, AV_LOG_ERROR, "Undefined opcode (%x) in DELTA_FLI\n", line_packets);
278  } else if ((line_packets & 0xC000) == 0x8000) {
279  // "last byte" opcode
280  pixel_ptr= y_ptr + s->frame.linesize[0] - 1;
281  CHECK_PIXEL_PTR(0);
282  pixels[pixel_ptr] = line_packets & 0xff;
283  } else {
284  compressed_lines--;
285  pixel_ptr = y_ptr;
286  CHECK_PIXEL_PTR(0);
287  pixel_countdown = s->avctx->width;
288  for (i = 0; i < line_packets; i++) {
289  if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
290  break;
291  /* account for the skip bytes */
292  pixel_skip = bytestream2_get_byte(&g2);
293  pixel_ptr += pixel_skip;
294  pixel_countdown -= pixel_skip;
295  byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
296  if (byte_run < 0) {
297  byte_run = -byte_run;
298  palette_idx1 = bytestream2_get_byte(&g2);
299  palette_idx2 = bytestream2_get_byte(&g2);
300  CHECK_PIXEL_PTR(byte_run * 2);
301  for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
302  pixels[pixel_ptr++] = palette_idx1;
303  pixels[pixel_ptr++] = palette_idx2;
304  }
305  } else {
306  CHECK_PIXEL_PTR(byte_run * 2);
307  if (bytestream2_tell(&g2) + byte_run * 2 > stream_ptr_after_chunk)
308  break;
309  for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
310  pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
311  }
312  }
313  }
314 
315  y_ptr += s->frame.linesize[0];
316  }
317  }
318  break;
319 
320  case FLI_LC:
321  /* line compressed */
322  starting_line = bytestream2_get_le16(&g2);
323  y_ptr = 0;
324  y_ptr += starting_line * s->frame.linesize[0];
325 
326  compressed_lines = bytestream2_get_le16(&g2);
327  while (compressed_lines > 0) {
328  pixel_ptr = y_ptr;
329  CHECK_PIXEL_PTR(0);
330  pixel_countdown = s->avctx->width;
331  if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
332  break;
333  line_packets = bytestream2_get_byte(&g2);
334  if (line_packets > 0) {
335  for (i = 0; i < line_packets; i++) {
336  /* account for the skip bytes */
337  if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
338  break;
339  pixel_skip = bytestream2_get_byte(&g2);
340  pixel_ptr += pixel_skip;
341  pixel_countdown -= pixel_skip;
342  byte_run = sign_extend(bytestream2_get_byte(&g2),8);
343  if (byte_run > 0) {
344  CHECK_PIXEL_PTR(byte_run);
345  if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
346  break;
347  for (j = 0; j < byte_run; j++, pixel_countdown--) {
348  pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
349  }
350  } else if (byte_run < 0) {
351  byte_run = -byte_run;
352  palette_idx1 = bytestream2_get_byte(&g2);
353  CHECK_PIXEL_PTR(byte_run);
354  for (j = 0; j < byte_run; j++, pixel_countdown--) {
355  pixels[pixel_ptr++] = palette_idx1;
356  }
357  }
358  }
359  }
360 
361  y_ptr += s->frame.linesize[0];
362  compressed_lines--;
363  }
364  break;
365 
366  case FLI_BLACK:
367  /* set the whole frame to color 0 (which is usually black) */
368  memset(pixels, 0,
369  s->frame.linesize[0] * s->avctx->height);
370  break;
371 
372  case FLI_BRUN:
373  /* Byte run compression: This chunk type only occurs in the first
374  * FLI frame and it will update the entire frame. */
375  y_ptr = 0;
376  for (lines = 0; lines < s->avctx->height; lines++) {
377  pixel_ptr = y_ptr;
378  /* disregard the line packets; instead, iterate through all
379  * pixels on a row */
380  bytestream2_skip(&g2, 1);
381  pixel_countdown = s->avctx->width;
382  while (pixel_countdown > 0) {
383  if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
384  break;
385  byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
386  if (!byte_run) {
387  av_log(avctx, AV_LOG_ERROR, "Invalid byte run value.\n");
388  return AVERROR_INVALIDDATA;
389  }
390 
391  if (byte_run > 0) {
392  palette_idx1 = bytestream2_get_byte(&g2);
393  CHECK_PIXEL_PTR(byte_run);
394  for (j = 0; j < byte_run; j++) {
395  pixels[pixel_ptr++] = palette_idx1;
396  pixel_countdown--;
397  if (pixel_countdown < 0)
398  av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
399  pixel_countdown, lines);
400  }
401  } else { /* copy bytes if byte_run < 0 */
402  byte_run = -byte_run;
403  CHECK_PIXEL_PTR(byte_run);
404  if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
405  break;
406  for (j = 0; j < byte_run; j++) {
407  pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
408  pixel_countdown--;
409  if (pixel_countdown < 0)
410  av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
411  pixel_countdown, lines);
412  }
413  }
414  }
415 
416  y_ptr += s->frame.linesize[0];
417  }
418  break;
419 
420  case FLI_COPY:
421  /* copy the chunk (uncompressed frame) */
422  if (chunk_size - 6 != s->avctx->width * s->avctx->height) {
423  av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
424  "has incorrect size, skipping chunk\n", chunk_size - 6);
425  bytestream2_skip(&g2, chunk_size - 6);
426  } else {
427  for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height;
428  y_ptr += s->frame.linesize[0]) {
429  bytestream2_get_buffer(&g2, &pixels[y_ptr],
430  s->avctx->width);
431  }
432  }
433  break;
434 
435  case FLI_MINI:
436  /* some sort of a thumbnail? disregard this chunk... */
437  break;
438 
439  default:
440  av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
441  break;
442  }
443 
444  if (stream_ptr_after_chunk - bytestream2_tell(&g2) > 0)
445  bytestream2_skip(&g2, stream_ptr_after_chunk - bytestream2_tell(&g2));
446 
447  frame_size -= chunk_size;
448  num_chunks--;
449  }
450 
451  /* by the end of the chunk, the stream ptr should equal the frame
452  * size (minus 1 or 2, possibly); if it doesn't, issue a warning */
453  if (bytestream2_get_bytes_left(&g2) > 2)
454  av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
455  "and final chunk ptr = %d\n", buf_size,
456  buf_size - bytestream2_get_bytes_left(&g2));
457 
458  /* make the palette available on the way out */
459  memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
460  if (s->new_palette) {
461  s->frame.palette_has_changed = 1;
462  s->new_palette = 0;
463  }
464 
465  if ((ret = av_frame_ref(data, &s->frame)) < 0)
466  return ret;
467 
468  *got_frame = 1;
469 
470  return buf_size;
471 }
472 
474  void *data, int *got_frame,
475  const uint8_t *buf, int buf_size)
476 {
477  /* Note, the only difference between the 15Bpp and 16Bpp */
478  /* Format is the pixel format, the packets are processed the same. */
479  FlicDecodeContext *s = avctx->priv_data;
480 
481  GetByteContext g2;
482  int pixel_ptr;
483  unsigned char palette_idx1;
484 
485  unsigned int frame_size;
486  int num_chunks;
487 
488  unsigned int chunk_size;
489  int chunk_type;
490 
491  int i, j, ret;
492 
493  int lines;
494  int compressed_lines;
495  signed short line_packets;
496  int y_ptr;
497  int byte_run;
498  int pixel_skip;
499  int pixel_countdown;
500  unsigned char *pixels;
501  int pixel;
502  unsigned int pixel_limit;
503 
504  bytestream2_init(&g2, buf, buf_size);
505 
506  if ((ret = ff_reget_buffer(avctx, &s->frame)) < 0)
507  return ret;
508 
509  pixels = s->frame.data[0];
510  pixel_limit = s->avctx->height * s->frame.linesize[0];
511 
512  frame_size = bytestream2_get_le32(&g2);
513  bytestream2_skip(&g2, 2); /* skip the magic number */
514  num_chunks = bytestream2_get_le16(&g2);
515  bytestream2_skip(&g2, 8); /* skip padding */
516  if (frame_size > buf_size)
517  frame_size = buf_size;
518 
519  frame_size -= 16;
520 
521  /* iterate through the chunks */
522  while ((frame_size > 0) && (num_chunks > 0)) {
523  int stream_ptr_after_chunk;
524  chunk_size = bytestream2_get_le32(&g2);
525  if (chunk_size > frame_size) {
526  av_log(avctx, AV_LOG_WARNING,
527  "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
528  chunk_size = frame_size;
529  }
530  stream_ptr_after_chunk = bytestream2_tell(&g2) - 4 + chunk_size;
531 
532  chunk_type = bytestream2_get_le16(&g2);
533 
534 
535  switch (chunk_type) {
536  case FLI_256_COLOR:
537  case FLI_COLOR:
538  /* For some reason, it seems that non-palettized flics do
539  * include one of these chunks in their first frame.
540  * Why I do not know, it seems rather extraneous. */
541  av_dlog(avctx,
542  "Unexpected Palette chunk %d in non-palettized FLC\n",
543  chunk_type);
544  bytestream2_skip(&g2, chunk_size - 6);
545  break;
546 
547  case FLI_DELTA:
548  case FLI_DTA_LC:
549  y_ptr = 0;
550  compressed_lines = bytestream2_get_le16(&g2);
551  while (compressed_lines > 0) {
552  if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
553  break;
554  line_packets = bytestream2_get_le16(&g2);
555  if (line_packets < 0) {
556  line_packets = -line_packets;
557  y_ptr += line_packets * s->frame.linesize[0];
558  } else {
559  compressed_lines--;
560  pixel_ptr = y_ptr;
561  CHECK_PIXEL_PTR(0);
562  pixel_countdown = s->avctx->width;
563  for (i = 0; i < line_packets; i++) {
564  /* account for the skip bytes */
565  if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
566  break;
567  pixel_skip = bytestream2_get_byte(&g2);
568  pixel_ptr += (pixel_skip*2); /* Pixel is 2 bytes wide */
569  pixel_countdown -= pixel_skip;
570  byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
571  if (byte_run < 0) {
572  byte_run = -byte_run;
573  pixel = bytestream2_get_le16(&g2);
574  CHECK_PIXEL_PTR(2 * byte_run);
575  for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
576  *((signed short*)(&pixels[pixel_ptr])) = pixel;
577  pixel_ptr += 2;
578  }
579  } else {
580  if (bytestream2_tell(&g2) + 2*byte_run > stream_ptr_after_chunk)
581  break;
582  CHECK_PIXEL_PTR(2 * byte_run);
583  for (j = 0; j < byte_run; j++, pixel_countdown--) {
584  *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
585  pixel_ptr += 2;
586  }
587  }
588  }
589 
590  y_ptr += s->frame.linesize[0];
591  }
592  }
593  break;
594 
595  case FLI_LC:
596  av_log(avctx, AV_LOG_ERROR, "Unexpected FLI_LC chunk in non-paletised FLC\n");
597  bytestream2_skip(&g2, chunk_size - 6);
598  break;
599 
600  case FLI_BLACK:
601  /* set the whole frame to 0x0000 which is black in both 15Bpp and 16Bpp modes. */
602  memset(pixels, 0x0000,
603  s->frame.linesize[0] * s->avctx->height);
604  break;
605 
606  case FLI_BRUN:
607  y_ptr = 0;
608  for (lines = 0; lines < s->avctx->height; lines++) {
609  pixel_ptr = y_ptr;
610  /* disregard the line packets; instead, iterate through all
611  * pixels on a row */
612  bytestream2_skip(&g2, 1);
613  pixel_countdown = (s->avctx->width * 2);
614 
615  while (pixel_countdown > 0) {
616  if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
617  break;
618  byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
619  if (byte_run > 0) {
620  palette_idx1 = bytestream2_get_byte(&g2);
621  CHECK_PIXEL_PTR(byte_run);
622  for (j = 0; j < byte_run; j++) {
623  pixels[pixel_ptr++] = palette_idx1;
624  pixel_countdown--;
625  if (pixel_countdown < 0)
626  av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) (linea%d)\n",
627  pixel_countdown, lines);
628  }
629  } else { /* copy bytes if byte_run < 0 */
630  byte_run = -byte_run;
631  if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
632  break;
633  CHECK_PIXEL_PTR(byte_run);
634  for (j = 0; j < byte_run; j++) {
635  palette_idx1 = bytestream2_get_byte(&g2);
636  pixels[pixel_ptr++] = palette_idx1;
637  pixel_countdown--;
638  if (pixel_countdown < 0)
639  av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
640  pixel_countdown, lines);
641  }
642  }
643  }
644 
645  /* Now FLX is strange, in that it is "byte" as opposed to "pixel" run length compressed.
646  * This does not give us any good opportunity to perform word endian conversion
647  * during decompression. So if it is required (i.e., this is not a LE target, we do
648  * a second pass over the line here, swapping the bytes.
649  */
650 #if HAVE_BIGENDIAN
651  pixel_ptr = y_ptr;
652  pixel_countdown = s->avctx->width;
653  while (pixel_countdown > 0) {
654  *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[pixel_ptr]);
655  pixel_ptr += 2;
656  }
657 #endif
658  y_ptr += s->frame.linesize[0];
659  }
660  break;
661 
662  case FLI_DTA_BRUN:
663  y_ptr = 0;
664  for (lines = 0; lines < s->avctx->height; lines++) {
665  pixel_ptr = y_ptr;
666  /* disregard the line packets; instead, iterate through all
667  * pixels on a row */
668  bytestream2_skip(&g2, 1);
669  pixel_countdown = s->avctx->width; /* Width is in pixels, not bytes */
670 
671  while (pixel_countdown > 0) {
672  if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
673  break;
674  byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
675  if (byte_run > 0) {
676  pixel = bytestream2_get_le16(&g2);
677  CHECK_PIXEL_PTR(2 * byte_run);
678  for (j = 0; j < byte_run; j++) {
679  *((signed short*)(&pixels[pixel_ptr])) = pixel;
680  pixel_ptr += 2;
681  pixel_countdown--;
682  if (pixel_countdown < 0)
683  av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
684  pixel_countdown);
685  }
686  } else { /* copy pixels if byte_run < 0 */
687  byte_run = -byte_run;
688  if (bytestream2_tell(&g2) + 2 * byte_run > stream_ptr_after_chunk)
689  break;
690  CHECK_PIXEL_PTR(2 * byte_run);
691  for (j = 0; j < byte_run; j++) {
692  *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
693  pixel_ptr += 2;
694  pixel_countdown--;
695  if (pixel_countdown < 0)
696  av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
697  pixel_countdown);
698  }
699  }
700  }
701 
702  y_ptr += s->frame.linesize[0];
703  }
704  break;
705 
706  case FLI_COPY:
707  case FLI_DTA_COPY:
708  /* copy the chunk (uncompressed frame) */
709  if (chunk_size - 6 > (unsigned int)(s->avctx->width * s->avctx->height)*2) {
710  av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
711  "bigger than image, skipping chunk\n", chunk_size - 6);
712  bytestream2_skip(&g2, chunk_size - 6);
713  } else {
714 
715  for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height;
716  y_ptr += s->frame.linesize[0]) {
717 
718  pixel_countdown = s->avctx->width;
719  pixel_ptr = 0;
720  while (pixel_countdown > 0) {
721  *((signed short*)(&pixels[y_ptr + pixel_ptr])) = bytestream2_get_le16(&g2);
722  pixel_ptr += 2;
723  pixel_countdown--;
724  }
725  }
726  }
727  break;
728 
729  case FLI_MINI:
730  /* some sort of a thumbnail? disregard this chunk... */
731  bytestream2_skip(&g2, chunk_size - 6);
732  break;
733 
734  default:
735  av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
736  break;
737  }
738 
739  frame_size -= chunk_size;
740  num_chunks--;
741  }
742 
743  /* by the end of the chunk, the stream ptr should equal the frame
744  * size (minus 1, possibly); if it doesn't, issue a warning */
745  if ((bytestream2_get_bytes_left(&g2) != 0) && (bytestream2_get_bytes_left(&g2) != 1))
746  av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
747  "and final chunk ptr = %d\n", buf_size, bytestream2_tell(&g2));
748 
749  if ((ret = av_frame_ref(data, &s->frame)) < 0)
750  return ret;
751 
752  *got_frame = 1;
753 
754  return buf_size;
755 }
756 
758  void *data, int *got_frame,
759  const uint8_t *buf, int buf_size)
760 {
761  av_log(avctx, AV_LOG_ERROR, "24Bpp FLC Unsupported due to lack of test files.\n");
762  return AVERROR_PATCHWELCOME;
763 }
764 
766  void *data, int *got_frame,
767  AVPacket *avpkt)
768 {
769  const uint8_t *buf = avpkt->data;
770  int buf_size = avpkt->size;
771  if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
772  return flic_decode_frame_8BPP(avctx, data, got_frame,
773  buf, buf_size);
774  }
775  else if ((avctx->pix_fmt == AV_PIX_FMT_RGB555) ||
776  (avctx->pix_fmt == AV_PIX_FMT_RGB565)) {
777  return flic_decode_frame_15_16BPP(avctx, data, got_frame,
778  buf, buf_size);
779  }
780  else if (avctx->pix_fmt == AV_PIX_FMT_BGR24) {
781  return flic_decode_frame_24BPP(avctx, data, got_frame,
782  buf, buf_size);
783  }
784 
785  /* Should not get here, ever as the pix_fmt is processed */
786  /* in flic_decode_init and the above if should deal with */
787  /* the finite set of possibilites allowable by here. */
788  /* But in case we do, just error out. */
789  av_log(avctx, AV_LOG_ERROR, "Unknown FLC format, my science cannot explain how this happened.\n");
790  return AVERROR_BUG;
791 }
792 
793 
795 {
796  FlicDecodeContext *s = avctx->priv_data;
797 
798  av_frame_unref(&s->frame);
799 
800  return 0;
801 }
802 
804  .name = "flic",
805  .type = AVMEDIA_TYPE_VIDEO,
806  .id = AV_CODEC_ID_FLIC,
807  .priv_data_size = sizeof(FlicDecodeContext),
811  .capabilities = CODEC_CAP_DR1,
812  .long_name = NULL_IF_CONFIG_SMALL("Autodesk Animator Flic video"),
813 };
#define FLI_DTA_COPY
Definition: flicvideo.c:57
AVCodecContext * avctx
Definition: flicvideo.c:73
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
#define FLI_TYPE_CODE
Definition: flicvideo.c:60
#define FLI_MINI
Definition: flicvideo.c:55
#define CHECK_PIXEL_PTR(n)
Definition: flicvideo.c:65
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:154
#define FLI_COPY
Definition: flicvideo.c:54
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_RL16
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
static int flic_decode_frame_15_16BPP(AVCodecContext *avctx, void *data, int *got_frame, const uint8_t *buf, int buf_size)
Definition: flicvideo.c:473
struct FlicDecodeContext FlicDecodeContext
initialize output if(nPeaks >3)%at least 3 peaks in spectrum for trying to find f0 nf0peaks
static int flic_decode_frame_8BPP(AVCodecContext *avctx, void *data, int *got_frame, const uint8_t *buf, int buf_size)
Definition: flicvideo.c:150
uint8_t
#define av_cold
Definition: attributes.h:78
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 FLI_COLOR
Definition: flicvideo.c:50
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
uint8_t * data
#define FLI_LC
Definition: flicvideo.c:51
static const uint8_t frame_size[4]
Definition: g723_1_data.h:58
#define U(x)
#define FLI_BRUN
Definition: flicvideo.c:53
static int flic_decode_frame_24BPP(AVCodecContext *avctx, void *data, int *got_frame, const uint8_t *buf, int buf_size)
Definition: flicvideo.c:757
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
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:258
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:149
#define FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE
Definition: flicvideo.c:63
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 depth
Definition: v4l.c:62
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
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...
FFT buffer for g
Definition: stft_peak.m:17
#define FLI_DTA_BRUN
Definition: flicvideo.c:56
ret
Definition: avfilter.c:821
int width
picture width / height.
#define FLC_FLX_TYPE_CODE
Definition: flicvideo.c:61
#define FLI_256_COLOR
Definition: flicvideo.c:48
#define AV_RL32
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:71
#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:183
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_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
void * buf
Definition: avisynth_c.h:594
#define FLI_DTA_LC
Definition: flicvideo.c:58
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
static int flic_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: flicvideo.c:765
static av_cold int flic_decode_end(AVCodecContext *avctx)
Definition: flicvideo.c:794
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
uint8_t pixel
Definition: tiny_ssim.c:40
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:123
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
#define FLI_BLACK
Definition: flicvideo.c:52
common internal api header.
#define AV_PIX_FMT_RGB555
Definition: pixfmt.h:269
unsigned int palette[256]
Definition: flicvideo.c:76
AVCodec ff_flic_decoder
Definition: flicvideo.c:803
#define AV_PIX_FMT_RGB565
Definition: pixfmt.h:268
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
This structure stores compressed data.
static av_cold int flic_decode_init(AVCodecContext *avctx)
Definition: flicvideo.c:81