flic.c 6.3 KB

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