Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

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