diff options
-rw-r--r-- | Changelog | 5 | ||||
-rw-r--r-- | doc/ffmpeg-doc.texi | 10 | ||||
-rw-r--r-- | libavcodec/Makefile | 2 | ||||
-rw-r--r-- | libavcodec/allcodecs.c | 1 | ||||
-rw-r--r-- | libavcodec/avcodec.h | 2 | ||||
-rw-r--r-- | libavcodec/flicvideo.c | 461 | ||||
-rw-r--r-- | libavformat/Makefile | 2 | ||||
-rw-r--r-- | libavformat/allformats.c | 1 | ||||
-rw-r--r-- | libavformat/avformat.h | 3 | ||||
-rw-r--r-- | libavformat/flic.c | 228 |
10 files changed, 711 insertions, 4 deletions
@@ -2,11 +2,14 @@ version <next>: - DV encoder, DV muxer - Microsoft RLE video decoder - Microsoft Video-1 decoder +- Apple Graphics (SMC) decoder - Apple Video (RPZA) decoder - Cinepak decoder - Sega FILM (CPK) file demuxer -- Westwood multimedia support (VQA/AUD file demuxer, audio decoder) +- Westwood multimedia support (VQA & AUD files) - Id Quake II CIN playback support +- 8BPS video decoder +- FLIC playback support version 0.4.8: diff --git a/doc/ffmpeg-doc.texi b/doc/ffmpeg-doc.texi index 2532709d89..ee19541099 100644 --- a/doc/ffmpeg-doc.texi +++ b/doc/ffmpeg-doc.texi @@ -613,6 +613,7 @@ library: @item Raw MJPEG @tab X @tab X @item Raw MPEG video @tab X @tab X @item Raw PCM8/16 bits, mulaw/Alaw@tab X @tab X +@item Raw CRI ADX audio @tab X @tab X @item SUN AU format @tab X @tab X @item NUT @tab X @tab X @tab NUT Open Container Format @item Quicktime @tab X @tab X @@ -635,6 +636,8 @@ library: @tab Multimedia formats used in Westwood Studios games @item Id Cinematic (.cin) @tab @tab X @tab Used in Quake II +@item FLIC format @tab @tab X +@tab .fli/.flc files @end multitable @code{X} means that the encoding (resp. decoding) is supported. @@ -695,6 +698,7 @@ following image formats are supported: @item Id RoQ @tab @tab X @tab used in Quake III, Jedi Knight 2, other computer games @item Xan/WC3 @tab @tab X @tab used in Wing Commander III .MVE files @item Interplay Video @tab @tab X @tab used in Interplay .MVE files +@item Apple Graphics @tab @tab X @tab fourcc: 'smc ' @item Apple Video @tab @tab X @tab fourcc: rpza @item Cinepak @tab @tab X @item Microsoft RLE @tab @tab X @@ -702,6 +706,7 @@ following image formats are supported: @item Westwood VQA @tab @tab X @item Id Cinematic Video @tab @tab X @tab used in Quake II @item Planar RGB @tab @tab X @tab fourcc: 8BPS +@item FLIC video @tab @tab X @end multitable @code{X} means that the encoding (resp. decoding) is supported. @@ -731,7 +736,10 @@ solutions. @item Duck DK4 IMA ADPCM @tab @tab X @tab used in some Sega Saturn console games @item Westwood Studios IMA ADPCM @tab @tab X -@tab used in Westwood Studios games likes Command and Conquer +@tab used in Westwood Studios games like Command and Conquer +@item CD-ROM XA ADPCM @tab @tab X +@item CRI ADX ADPCM @tab X @tab X +@tab used in Sega Dreamcast games @item RA144 @tab @tab X @tab Real 14400 bit/s codec @item RA288 @tab @tab X diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 6271aa3ca9..d5accc2d05 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -19,7 +19,7 @@ OBJS= common.o utils.o mem.o allcodecs.o \ vp3.o asv1.o 4xm.o cabac.o ffv1.o ra144.o ra288.o vcr1.o cljr.o \ roqvideo.o dpcm.o interplayvideo.o xan.o rpza.o cinepak.o msrle.o \ msvideo1.o vqavideo.o idcinvideo.o adx.o rational.o faandct.o 8bps.o \ - smc.o parser.o + smc.o parser.o flicvideo.o ifeq ($(AMR_NB),yes) ifeq ($(AMR_NB_FIXED),yes) diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index f57ff0b509..b7af5bed96 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -137,6 +137,7 @@ void avcodec_register_all(void) register_avcodec(&idcin_decoder); register_avcodec(&eightbps_decoder); register_avcodec(&smc_decoder); + register_avcodec(&flic_decoder); #ifdef CONFIG_AC3 register_avcodec(&ac3_decoder); #endif diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 64d0ce1644..0e5d1cd0f6 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -86,6 +86,7 @@ enum CodecID { CODEC_ID_IDCIN, CODEC_ID_8BPS, CODEC_ID_SMC, + CODEC_ID_FLIC, /* various pcm "codecs" */ CODEC_ID_PCM_S16LE, @@ -1574,6 +1575,7 @@ extern AVCodec vqa_decoder; extern AVCodec idcin_decoder; extern AVCodec eightbps_decoder; extern AVCodec smc_decoder; +extern AVCodec flic_decoder; extern AVCodec ra_144_decoder; extern AVCodec ra_288_decoder; extern AVCodec roq_dpcm_decoder; diff --git a/libavcodec/flicvideo.c b/libavcodec/flicvideo.c new file mode 100644 index 0000000000..b5cd366b96 --- /dev/null +++ b/libavcodec/flicvideo.c @@ -0,0 +1,461 @@ +/* + * FLI/FLC Animation Video Decoder + * Copyright (C) 2003 the ffmpeg project + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +/** + * @file flic.c + * Autodesk Animator FLI/FLC Video Decoder + * by Mike Melanson (melanson@pcisys.net) + * for more information on the .fli/.flc file format and all of its many + * variations, visit: + * http://www.compuphase.com/flic.htm + * + * This decoder outputs PAL8 colorspace data. To use this decoder, be + * sure that your demuxer sends the FLI file header to the decoder via + * the extradata chunk in AVCodecContext. The chunk should be 128 bytes + * large. The only exception is for FLI files from the game "Magic Carpet", + * in which the header is only 12 bytes. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "common.h" +#include "avcodec.h" +#include "bswap.h" + +#define FLI_256_COLOR 4 +#define FLI_DELTA 7 +#define FLI_COLOR 11 +#define FLI_LC 12 +#define FLI_BLACK 13 +#define FLI_BRUN 15 +#define FLI_COPY 16 +#define FLI_MINI 18 + +#define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0]) +#define LE_32(x) ((((uint8_t*)(x))[3] << 24) | \ + (((uint8_t*)(x))[2] << 16) | \ + (((uint8_t*)(x))[1] << 8) | \ + ((uint8_t*)(x))[0]) + +typedef struct FlicDecodeContext { + AVCodecContext *avctx; + AVFrame frame; + AVFrame prev_frame; + + unsigned int palette[256]; + int new_palette; + int fli_type; /* either 0xAF11 or 0xAF12, affects palette resolution */ +} FlicDecodeContext; + +static int flic_decode_init(AVCodecContext *avctx) +{ + FlicDecodeContext *s = (FlicDecodeContext *)avctx->priv_data; + unsigned char *fli_header = (unsigned char *)avctx->extradata; + + s->avctx = avctx; + avctx->pix_fmt = PIX_FMT_PAL8; + avctx->has_b_frames = 0; + + if (s->avctx->extradata_size == 12) { + /* special case for magic carpet FLIs */ + s->fli_type = 0xAF13; + } else if (s->avctx->extradata_size == 128) { + s->fli_type = LE_16(&fli_header[4]); + } else { + printf (" FLI video: expected extradata of 12 or 128 bytes\n"); + return -1; + } + + s->frame.data[0] = s->prev_frame.data[0] = NULL; + s->new_palette = 0; + + return 0; +} + +static int flic_decode_frame(AVCodecContext *avctx, + void *data, int *data_size, + uint8_t *buf, int buf_size) +{ + FlicDecodeContext *s = (FlicDecodeContext *)avctx->priv_data; + + int stream_ptr = 0; + int stream_ptr_after_color_chunk; + int pixel_ptr; + int palette_ptr; + unsigned char palette_idx1; + unsigned char palette_idx2; + + unsigned int frame_size; + int num_chunks; + + unsigned int chunk_size; + int chunk_type; + + int i, j; + + int color_packets; + int color_changes; + int color_shift; + unsigned char r, g, b; + + int lines; + int compressed_lines; + int starting_line; + signed short line_packets; + int y_ptr; + signed char byte_run; + int pixel_skip; + int pixel_countdown; + int height_countdown; + unsigned char *pixels; + unsigned char *prev_pixels; + + s->frame.reference = 1; + if (avctx->get_buffer(avctx, &s->frame) < 0) { + fprintf(stderr, " FLI: get_buffer() failed\n"); + return -1; + } + + pixels = s->frame.data[0]; + prev_pixels = s->prev_frame.data[0]; + + frame_size = LE_32(&buf[stream_ptr]); + stream_ptr += 6; /* skip the magic number */ + num_chunks = LE_16(&buf[stream_ptr]); + stream_ptr += 10; /* skip padding */ + + frame_size -= 16; + + /* if there is no data, copy previous frame */ + if (frame_size == 0) { + memcpy(pixels, prev_pixels, s->frame.linesize[0] * s->avctx->height); + } + + /* iterate through the chunks */ + while ((frame_size > 0) && (num_chunks > 0)) { + chunk_size = LE_32(&buf[stream_ptr]); + stream_ptr += 4; + chunk_type = LE_16(&buf[stream_ptr]); + stream_ptr += 2; + + switch (chunk_type) { + case FLI_256_COLOR: + case FLI_COLOR: + stream_ptr_after_color_chunk = stream_ptr + chunk_size - 6; + s->new_palette = 1; + + /* check special case: If this file is from the Magic Carpet + * game and uses 6-bit colors even though it reports 256-color + * chunks in a 0xAF12-type file (fli_type is set to 0xAF13 during + * initialization) */ + if ((chunk_type == FLI_256_COLOR) && (s->fli_type != 0xAF13)) + color_shift = 0; + else + color_shift = 2; + /* set up the palette */ + color_packets = LE_16(&buf[stream_ptr]); + stream_ptr += 2; + palette_ptr = 0; + for (i = 0; i < color_packets; i++) { + /* first byte is how many colors to skip */ + palette_ptr += buf[stream_ptr++]; + + /* next byte indicates how many entries to change */ + color_changes = buf[stream_ptr++]; + + /* if there are 0 color changes, there are actually 256 */ + if (color_changes == 0) + color_changes = 256; + + for (j = 0; j < color_changes; j++) { + + /* wrap around, for good measure */ + if (palette_ptr >= 256) + palette_ptr = 0; + + r = buf[stream_ptr++] << color_shift; + g = buf[stream_ptr++] << color_shift; + b = buf[stream_ptr++] << color_shift; + s->palette[palette_ptr++] = (r << 16) | (g << 8) | b; + } + } + + /* color chunks sometimes have weird 16-bit alignment issues; + * therefore, take the hardline approach and set the stream_ptr + * to the value calculated w.r.t. the size specified by the color + * chunk header */ + stream_ptr = stream_ptr_after_color_chunk; + + break; + + case FLI_DELTA: + y_ptr = 0; + height_countdown = s->avctx->height; + compressed_lines = LE_16(&buf[stream_ptr]); + stream_ptr += 2; + while (compressed_lines > 0) { + line_packets = LE_16(&buf[stream_ptr]); + stream_ptr += 2; + if (line_packets < 0) { + line_packets = -line_packets; + /* height_countdown was already decremented once on + * this iteration */ + height_countdown -= line_packets; + /* copy the skipped lines from the previous frame */ + while (line_packets--) { + memcpy(&pixels[y_ptr], &prev_pixels[y_ptr], + s->avctx->width); + y_ptr += s->frame.linesize[0]; + } + } else { + height_countdown--; + compressed_lines--; + pixel_ptr = y_ptr; + pixel_countdown = s->avctx->width; + for (i = 0; i < line_packets; i++) { + /* account for the skip bytes */ + pixel_skip = buf[stream_ptr++]; + memcpy(&pixels[pixel_ptr], &prev_pixels[pixel_ptr], + pixel_skip); + pixel_ptr += pixel_skip; + pixel_countdown -= pixel_skip; + byte_run = buf[stream_ptr++]; + if (byte_run < 0) { + byte_run = -byte_run; + palette_idx1 = buf[stream_ptr++]; + palette_idx2 = buf[stream_ptr++]; + for (j = 0; j < byte_run; j++, pixel_countdown -= 2) { + pixels[pixel_ptr++] = palette_idx1; + pixels[pixel_ptr++] = palette_idx2; + } + } else { + for (j = 0; j < byte_run * 2; j++, pixel_countdown--) { + palette_idx1 = buf[stream_ptr++]; + pixels[pixel_ptr++] = palette_idx1; + } + } + } + + /* copy the remaining pixels in the line from the + * previous frame */ + memcpy(&pixels[pixel_ptr], &prev_pixels[pixel_ptr], + pixel_countdown); + + y_ptr += s->frame.linesize[0]; + } + } + + /* copy the remainder of the lines from the previous frame */ + while (height_countdown--) { + memcpy(&pixels[y_ptr], &prev_pixels[y_ptr], s->avctx->width); + y_ptr += s->frame.linesize[0]; + } + break; + + case FLI_LC: + /* line compressed */ + height_countdown = s->avctx->height; + starting_line = LE_16(&buf[stream_ptr]); + stream_ptr += 2; + + /* copy from the previous frame all of the lines prior to the + * starting line */ + y_ptr = 0; + height_countdown -= starting_line; + while (starting_line--) { + memcpy(&pixels[y_ptr], &prev_pixels[y_ptr], s->avctx->width); + y_ptr += s->frame.linesize[0]; + } + + compressed_lines = LE_16(&buf[stream_ptr]); + stream_ptr += 2; + while (compressed_lines > 0) { + pixel_ptr = y_ptr; + pixel_countdown = s->avctx->width; + line_packets = buf[stream_ptr++]; + if (line_packets > 0) { + for (i = 0; i < line_packets; i++) { + /* account for the skip bytes */ + pixel_skip = buf[stream_ptr++]; + memcpy(&pixels[pixel_ptr], + &prev_pixels[pixel_ptr], pixel_skip); + pixel_ptr += pixel_skip; + pixel_countdown -= pixel_skip; + byte_run = buf[stream_ptr++]; + if (byte_run > 0) { + for (j = 0; j < byte_run; j++, pixel_countdown--) { + palette_idx1 = buf[stream_ptr++]; + pixels[pixel_ptr++] = palette_idx1; + } + } else { + byte_run = -byte_run; + palette_idx1 = buf[stream_ptr++]; + for (j = 0; j < byte_run; j++, pixel_countdown--) { + pixels[pixel_ptr++] = palette_idx1; + } + } + } + } + + /* copy the remainder of the line from the previous frame */ + memcpy(&pixels[pixel_ptr], &prev_pixels[pixel_ptr], + pixel_countdown); + + y_ptr += s->frame.linesize[0]; + compressed_lines--; + height_countdown--; + } + + /* copy the remainder of the lines from the previous frame */ + while (height_countdown--) { + memcpy(&pixels[y_ptr], &prev_pixels[y_ptr], s->avctx->width); + y_ptr += s->frame.linesize[0]; + } + break; + + case FLI_BLACK: + /* set the whole frame to color 0 (which is usually black) */ + memset(pixels, 0, + s->frame.linesize[0] * s->avctx->height); + break; + + case FLI_BRUN: + /* Byte run compression: This chunk type only occurs in the first + * FLI frame and it will update the entire frame. */ + y_ptr = 0; + for (lines = 0; lines < s->avctx->height; lines++) { + pixel_ptr = y_ptr; + /* disregard the line packets; instead, iterate through all + * pixels on a row */ + stream_ptr++; + pixel_countdown = s->avctx->width; + while (pixel_countdown > 0) { + byte_run = buf[stream_ptr++]; + if (byte_run > 0) { + palette_idx1 = buf[stream_ptr++]; + for (j = 0; j < byte_run; j++) { + pixels[pixel_ptr++] = palette_idx1; + pixel_countdown--; + if (pixel_countdown < 0) + printf ("fli warning: pixel_countdown < 0 (%d)\n", + pixel_countdown); + } + } else { /* copy bytes if byte_run < 0 */ + byte_run = -byte_run; + for (j = 0; j < byte_run; j++) { + palette_idx1 = buf[stream_ptr++]; + pixels[pixel_ptr++] = palette_idx1; + pixel_countdown--; + if (pixel_countdown < 0) + printf ("fli warning: pixel_countdown < 0 (%d)\n", + pixel_countdown); + } + } + } + + y_ptr += s->frame.linesize[0]; + } + break; + + case FLI_COPY: + /* copy the chunk (uncompressed frame) to the ghost image and + * schedule the whole frame to be updated */ + if (chunk_size - 6 > s->avctx->width * s->avctx->height) { + printf( + "FLI: in chunk FLI_COPY : source data (%d bytes) bigger than" \ + " image, skipping chunk\n", + chunk_size - 6); + stream_ptr += chunk_size - 6; + } else { + for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height; + y_ptr += s->frame.linesize[0]) { + memcpy(&pixels[y_ptr], &buf[stream_ptr], + s->avctx->width); + stream_ptr += s->avctx->width; + } + } + break; + + case FLI_MINI: + /* some sort of a thumbnail? disregard this chunk... */ + stream_ptr += chunk_size - 6; + break; + + default: + printf ("FLI: Unrecognized chunk type: %d\n", chunk_type); + break; + } + + frame_size -= chunk_size; + num_chunks--; + } + + /* by the end of the chunk, the stream ptr should equal the frame + * size (minus 1, possibly); if it doesn't, issue a warning */ + if ((stream_ptr != buf_size) && (stream_ptr != buf_size - 1)) + printf (" warning: processed FLI chunk where chunk size = %d\n" \ + " and final chunk ptr = %d\n", + buf_size, stream_ptr); + + /* make the palette available on the way out */ +// if (s->new_palette) { + if (1) { + memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE); + s->frame.palette_has_changed = 1; + s->new_palette = 0; + } + + if (s->prev_frame.data[0]) + avctx->release_buffer(avctx, &s->prev_frame); + + /* shuffle frames */ + s->prev_frame = s->frame; + + *data_size=sizeof(AVFrame); + *(AVFrame*)data = s->frame; + + return buf_size; +} + +static int flic_decode_end(AVCodecContext *avctx) +{ + FlicDecodeContext *s = avctx->priv_data; + + if (s->prev_frame.data[0]) + avctx->release_buffer(avctx, &s->prev_frame); + + return 0; +} + +AVCodec flic_decoder = { + "flic", + CODEC_TYPE_VIDEO, + CODEC_ID_FLIC, + sizeof(FlicDecodeContext), + flic_decode_init, + NULL, + flic_decode_end, + flic_decode_frame, + CODEC_CAP_DR1, + NULL +}; diff --git a/libavformat/Makefile b/libavformat/Makefile index bfda4761cb..0984c72a8d 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -15,7 +15,7 @@ PPOBJS= OBJS+=mpeg.o mpegts.o mpegtsenc.o ffm.o crc.o img.o raw.o rm.o \ avienc.o avidec.o wav.o swf.o au.o gif.o mov.o mpjpeg.o dv.o \ yuv4mpeg.o 4xm.o flvenc.o flvdec.o movenc.o psxstr.o idroq.o ipmovie.o \ - nut.o wc3movie.o mp3.o westwood.o segafilm.o idcin.o + nut.o wc3movie.o mp3.o westwood.o segafilm.o idcin.o flic.o ifeq ($(CONFIG_RISKY),yes) OBJS+= asf.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index cc213f940a..9446c0c1a7 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -69,6 +69,7 @@ void av_register_all(void) westwood_init(); film_init(); idcin_init(); + flic_init(); #if defined(AMR_NB) || defined(AMR_NB_FIXED) || defined(AMR_WB) amr_init(); diff --git a/libavformat/avformat.h b/libavformat/avformat.h index 8aaedeb319..cf81c88f7a 100644 --- a/libavformat/avformat.h +++ b/libavformat/avformat.h @@ -479,6 +479,9 @@ int film_init(void); /* idcin.c */ int idcin_init(void); +/* flic.c */ +int flic_init(void); + #include "rtp.h" #include "rtsp.h" diff --git a/libavformat/flic.c b/libavformat/flic.c new file mode 100644 index 0000000000..cab689a8ef --- /dev/null +++ b/libavformat/flic.c @@ -0,0 +1,228 @@ +/* + * FLI/FLC Animation File Demuxer + * Copyright (c) 2003 The ffmpeg Project + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/** + * @file flic.c + * FLI/FLC file demuxer + * by Mike Melanson (melanson@pcisys.net) + * for more information on the .fli/.flc file format and all of its many + * variations, visit: + * http://www.compuphase.com/flic.htm + * + * This demuxer handles standard 0xAF11- and 0xAF12-type FLIs. It also + * handles special FLIs from the PC game "Magic Carpet". + */ + +#include "avformat.h" + +#define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0]) +#define LE_32(x) ((((uint8_t*)(x))[3] << 24) | \ + (((uint8_t*)(x))[2] << 16) | \ + (((uint8_t*)(x))[1] << 8) | \ + ((uint8_t*)(x))[0]) + +#define FLIC_FILE_MAGIC_1 0xAF11 +#define FLIC_FILE_MAGIC_2 0xAF12 +#define FLIC_CHUNK_MAGIC_1 0xF1FA +#define FLIC_CHUNK_MAGIC_2 0xF5FA +#define FLIC_MC_PTS_INC 6000 /* pts increment for Magic Carpet game FLIs */ +#define FLIC_DEFAULT_PTS_INC 6000 /* for FLIs that have 0 speed */ + +#define FLIC_HEADER_SIZE 128 +#define FLIC_PREAMBLE_SIZE 6 + +typedef struct FlicDemuxContext { + int frame_pts_inc; + int64_t pts; + int video_stream_index; +} FlicDemuxContext; + +static int flic_probe(AVProbeData *p) +{ + int magic_number; + + if (p->buf_size < 6) + return 0; + + magic_number = LE_16(&p->buf[4]); + if ((magic_number != FLIC_FILE_MAGIC_1) && + (magic_number != FLIC_FILE_MAGIC_2)) + return 0; + + return AVPROBE_SCORE_MAX; +} + +static int flic_read_header(AVFormatContext *s, + AVFormatParameters *ap) +{ + FlicDemuxContext *flic = (FlicDemuxContext *)s->priv_data; + ByteIOContext *pb = &s->pb; + unsigned char header[FLIC_HEADER_SIZE]; + AVStream *st; + int speed; + int magic_number; + + flic->pts = 0; + + /* load the whole header and pull out the width and height */ + if (get_buffer(pb, header, FLIC_HEADER_SIZE) != FLIC_HEADER_SIZE) + return -EIO; + + magic_number = LE_16(&header[4]); + speed = LE_32(&header[0x10]); + + /* initialize the decoder streams */ + st = av_new_stream(s, 0); + if (!st) + return AVERROR_NOMEM; + flic->video_stream_index = st->index; + st->codec.codec_type = CODEC_TYPE_VIDEO; + st->codec.codec_id = CODEC_ID_FLIC; + st->codec.codec_tag = 0; /* no fourcc */ + st->codec.width = LE_16(&header[0x08]); + st->codec.height = LE_16(&header[0x0A]); + + if (!st->codec.width || !st->codec.height) + return AVERROR_INVALIDDATA; + + /* send over the whole 128-byte FLIC header */ + st->codec.extradata_size = FLIC_HEADER_SIZE; + st->codec.extradata = av_malloc(FLIC_HEADER_SIZE); + memcpy(st->codec.extradata, header, FLIC_HEADER_SIZE); + + /* set the pts reference (1 pts = 1/90000) */ + s->pts_num = 1; + s->pts_den = 90000; + + /* Time to figure out the framerate: If there is a FLIC chunk magic + * number at offset 0x10, assume this is from the Bullfrog game, + * Magic Carpet. */ + if (LE_16(&header[0x10]) == FLIC_CHUNK_MAGIC_1) { + + flic->frame_pts_inc = FLIC_MC_PTS_INC; + + /* rewind the stream since the first chunk is at offset 12 */ + url_fseek(pb, 12, SEEK_SET); + + /* send over abbreviated FLIC header chunk */ + av_free(st->codec.extradata); + st->codec.extradata_size = 12; + st->codec.extradata = av_malloc(12); + memcpy(st->codec.extradata, header, 12); + + } else if (magic_number == FLIC_FILE_MAGIC_1) { + /* + * in this case, the speed (n) is number of 1/70s ticks between frames: + * + * pts n * frame # + * -------- = ----------- => pts = n * (90000/70) * frame # + * 90000 70 + * + * therefore, the frame pts increment = n * 1285.7 + */ + flic->frame_pts_inc = speed * 1285.7; + } else if (magic_number == FLIC_FILE_MAGIC_2) { + /* + * in this case, the speed (n) is number of milliseconds between frames: + * + * pts n * frame # + * -------- = ----------- => pts = n * 90 * frame # + * 90000 1000 + * + * therefore, the frame pts increment = n * 90 + */ + flic->frame_pts_inc = speed * 90; + } else + return AVERROR_INVALIDDATA; + + if (flic->frame_pts_inc == 0) + flic->frame_pts_inc = FLIC_DEFAULT_PTS_INC; + + return 0; +} + +static int flic_read_packet(AVFormatContext *s, + AVPacket *pkt) +{ + FlicDemuxContext *flic = (FlicDemuxContext *)s->priv_data; + ByteIOContext *pb = &s->pb; + int packet_read = 0; + unsigned int size; + int magic; + int ret = 0; + unsigned char preamble[FLIC_PREAMBLE_SIZE]; + + while (!packet_read) { + + if ((ret = get_buffer(pb, preamble, FLIC_PREAMBLE_SIZE)) != + FLIC_PREAMBLE_SIZE) { + ret = -EIO; + break; + } + + size = LE_32(&preamble[0]); + magic = LE_16(&preamble[4]); + + if ((magic == FLIC_CHUNK_MAGIC_1) || (magic == FLIC_CHUNK_MAGIC_2)) { + if (av_new_packet(pkt, size)) { + ret = -EIO; + break; + } + pkt->stream_index = flic->video_stream_index; + pkt->pts = flic->pts; + memcpy(pkt->data, preamble, FLIC_PREAMBLE_SIZE); + ret = get_buffer(pb, pkt->data + FLIC_PREAMBLE_SIZE, + size - FLIC_PREAMBLE_SIZE); + if (ret != size - FLIC_PREAMBLE_SIZE) { + av_free_packet(pkt); + ret = -EIO; + } + flic->pts += flic->frame_pts_inc; + packet_read = 1; + } else { + /* not interested in this chunk */ + url_fseek(pb, size - 6, SEEK_CUR); + } + } + + return ret; +} + +static int flic_read_close(AVFormatContext *s) +{ +// FlicDemuxContext *flic = (FlicDemuxContext *)s->priv_data; + + return 0; +} + +static AVInputFormat flic_iformat = { + "flic", + "FLI/FLC animation format", + sizeof(FlicDemuxContext), + flic_probe, + flic_read_header, + flic_read_packet, + flic_read_close, +}; + +int flic_init(void) +{ + av_register_input_format(&flic_iformat); + return 0; +} |