flic.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * FLI/FLC Animation File Demuxer
  3. * Copyright (c) 2003 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. * @file libavformat/flic.c
  23. * FLI/FLC file demuxer
  24. * by Mike Melanson (melanson@pcisys.net)
  25. * for more information on the .fli/.flc file format and all of its many
  26. * variations, visit:
  27. * http://www.compuphase.com/flic.htm
  28. *
  29. * This demuxer handles standard 0xAF11- and 0xAF12-type FLIs. It also
  30. * handles special FLIs from the PC game "Magic Carpet".
  31. */
  32. #include "libavutil/intreadwrite.h"
  33. #include "avformat.h"
  34. #define FLIC_FILE_MAGIC_1 0xAF11
  35. #define FLIC_FILE_MAGIC_2 0xAF12
  36. #define FLIC_FILE_MAGIC_3 0xAF44 /* Flic Type for Extended FLX Format which
  37. originated in Dave's Targa Animator (DTA) */
  38. #define FLIC_CHUNK_MAGIC_1 0xF1FA
  39. #define FLIC_CHUNK_MAGIC_2 0xF5FA
  40. #define FLIC_MC_SPEED 5 /* speed for Magic Carpet game FLIs */
  41. #define FLIC_DEFAULT_SPEED 5 /* for FLIs that have 0 speed */
  42. #define FLIC_HEADER_SIZE 128
  43. #define FLIC_PREAMBLE_SIZE 6
  44. typedef struct FlicDemuxContext {
  45. int video_stream_index;
  46. int frame_number;
  47. } FlicDemuxContext;
  48. static int flic_probe(AVProbeData *p)
  49. {
  50. int magic_number;
  51. if(p->buf_size < FLIC_HEADER_SIZE)
  52. return 0;
  53. magic_number = AV_RL16(&p->buf[4]);
  54. if ((magic_number != FLIC_FILE_MAGIC_1) &&
  55. (magic_number != FLIC_FILE_MAGIC_2) &&
  56. (magic_number != FLIC_FILE_MAGIC_3))
  57. return 0;
  58. if(AV_RL16(&p->buf[0x10]) != FLIC_CHUNK_MAGIC_1){
  59. if(AV_RL32(&p->buf[0x10]) > 2000)
  60. return 0;
  61. }
  62. if( AV_RL16(&p->buf[0x08]) > 4096
  63. || AV_RL16(&p->buf[0x0A]) > 4096)
  64. return 0;
  65. return AVPROBE_SCORE_MAX;
  66. }
  67. static int flic_read_header(AVFormatContext *s,
  68. AVFormatParameters *ap)
  69. {
  70. FlicDemuxContext *flic = s->priv_data;
  71. ByteIOContext *pb = s->pb;
  72. unsigned char header[FLIC_HEADER_SIZE];
  73. AVStream *st;
  74. int speed;
  75. int magic_number;
  76. flic->frame_number = 0;
  77. /* load the whole header and pull out the width and height */
  78. if (get_buffer(pb, header, FLIC_HEADER_SIZE) != FLIC_HEADER_SIZE)
  79. return AVERROR(EIO);
  80. magic_number = AV_RL16(&header[4]);
  81. speed = AV_RL32(&header[0x10]);
  82. if (speed == 0)
  83. speed = FLIC_DEFAULT_SPEED;
  84. /* initialize the decoder streams */
  85. st = av_new_stream(s, 0);
  86. if (!st)
  87. return AVERROR(ENOMEM);
  88. flic->video_stream_index = st->index;
  89. st->codec->codec_type = CODEC_TYPE_VIDEO;
  90. st->codec->codec_id = CODEC_ID_FLIC;
  91. st->codec->codec_tag = 0; /* no fourcc */
  92. st->codec->width = AV_RL16(&header[0x08]);
  93. st->codec->height = AV_RL16(&header[0x0A]);
  94. if (!st->codec->width || !st->codec->height) {
  95. /* Ugly hack needed for the following sample: */
  96. /* http://samples.mplayerhq.hu/fli-flc/fli-bugs/specular.flc */
  97. av_log(s, AV_LOG_WARNING,
  98. "File with no specified width/height. Trying 640x480.\n");
  99. st->codec->width = 640;
  100. st->codec->height = 480;
  101. }
  102. /* send over the whole 128-byte FLIC header */
  103. st->codec->extradata_size = FLIC_HEADER_SIZE;
  104. st->codec->extradata = av_malloc(FLIC_HEADER_SIZE);
  105. memcpy(st->codec->extradata, header, FLIC_HEADER_SIZE);
  106. /* Time to figure out the framerate: If there is a FLIC chunk magic
  107. * number at offset 0x10, assume this is from the Bullfrog game,
  108. * Magic Carpet. */
  109. if (AV_RL16(&header[0x10]) == FLIC_CHUNK_MAGIC_1) {
  110. av_set_pts_info(st, 64, FLIC_MC_SPEED, 70);
  111. /* rewind the stream since the first chunk is at offset 12 */
  112. url_fseek(pb, 12, SEEK_SET);
  113. /* send over abbreviated FLIC header chunk */
  114. av_free(st->codec->extradata);
  115. st->codec->extradata_size = 12;
  116. st->codec->extradata = av_malloc(12);
  117. memcpy(st->codec->extradata, header, 12);
  118. } else if (magic_number == FLIC_FILE_MAGIC_1) {
  119. av_set_pts_info(st, 64, speed, 70);
  120. } else if ((magic_number == FLIC_FILE_MAGIC_2) ||
  121. (magic_number == FLIC_FILE_MAGIC_3)) {
  122. av_set_pts_info(st, 64, speed, 1000);
  123. } else {
  124. av_log(s, AV_LOG_INFO, "Invalid or unsupported magic chunk in file\n");
  125. return AVERROR_INVALIDDATA;
  126. }
  127. return 0;
  128. }
  129. static int flic_read_packet(AVFormatContext *s,
  130. AVPacket *pkt)
  131. {
  132. FlicDemuxContext *flic = s->priv_data;
  133. ByteIOContext *pb = s->pb;
  134. int packet_read = 0;
  135. unsigned int size;
  136. int magic;
  137. int ret = 0;
  138. unsigned char preamble[FLIC_PREAMBLE_SIZE];
  139. while (!packet_read) {
  140. if ((ret = get_buffer(pb, preamble, FLIC_PREAMBLE_SIZE)) !=
  141. FLIC_PREAMBLE_SIZE) {
  142. ret = AVERROR(EIO);
  143. break;
  144. }
  145. size = AV_RL32(&preamble[0]);
  146. magic = AV_RL16(&preamble[4]);
  147. if (((magic == FLIC_CHUNK_MAGIC_1) || (magic == FLIC_CHUNK_MAGIC_2)) && size > FLIC_PREAMBLE_SIZE) {
  148. if (av_new_packet(pkt, size)) {
  149. ret = AVERROR(EIO);
  150. break;
  151. }
  152. pkt->stream_index = flic->video_stream_index;
  153. pkt->pts = flic->frame_number++;
  154. pkt->pos = url_ftell(pb);
  155. memcpy(pkt->data, preamble, FLIC_PREAMBLE_SIZE);
  156. ret = get_buffer(pb, pkt->data + FLIC_PREAMBLE_SIZE,
  157. size - FLIC_PREAMBLE_SIZE);
  158. if (ret != size - FLIC_PREAMBLE_SIZE) {
  159. av_free_packet(pkt);
  160. ret = AVERROR(EIO);
  161. }
  162. packet_read = 1;
  163. } else {
  164. /* not interested in this chunk */
  165. url_fseek(pb, size - 6, SEEK_CUR);
  166. }
  167. }
  168. return ret;
  169. }
  170. AVInputFormat flic_demuxer = {
  171. "flic",
  172. NULL_IF_CONFIG_SMALL("FLI/FLC/FLX animation format"),
  173. sizeof(FlicDemuxContext),
  174. flic_probe,
  175. flic_read_header,
  176. flic_read_packet,
  177. };