You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
7.0KB

  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_PTS_INC 6000 /* pts increment for Magic Carpet game FLIs */
  40. #define FLIC_DEFAULT_PTS_INC 6000 /* for FLIs that have 0 speed */
  41. #define FLIC_HEADER_SIZE 128
  42. #define FLIC_PREAMBLE_SIZE 6
  43. typedef struct FlicDemuxContext {
  44. int frame_pts_inc;
  45. int64_t pts;
  46. int video_stream_index;
  47. } FlicDemuxContext;
  48. static int flic_probe(AVProbeData *p)
  49. {
  50. int magic_number;
  51. magic_number = AV_RL16(&p->buf[4]);
  52. if ((magic_number != FLIC_FILE_MAGIC_1) &&
  53. (magic_number != FLIC_FILE_MAGIC_2) &&
  54. (magic_number != FLIC_FILE_MAGIC_3))
  55. return 0;
  56. return AVPROBE_SCORE_MAX;
  57. }
  58. static int flic_read_header(AVFormatContext *s,
  59. AVFormatParameters *ap)
  60. {
  61. FlicDemuxContext *flic = s->priv_data;
  62. ByteIOContext *pb = &s->pb;
  63. unsigned char header[FLIC_HEADER_SIZE];
  64. AVStream *st;
  65. int speed;
  66. int magic_number;
  67. flic->pts = 0;
  68. /* load the whole header and pull out the width and height */
  69. if (get_buffer(pb, header, FLIC_HEADER_SIZE) != FLIC_HEADER_SIZE)
  70. return AVERROR(EIO);
  71. magic_number = AV_RL16(&header[4]);
  72. speed = AV_RL32(&header[0x10]);
  73. /* initialize the decoder streams */
  74. st = av_new_stream(s, 0);
  75. if (!st)
  76. return AVERROR(ENOMEM);
  77. flic->video_stream_index = st->index;
  78. st->codec->codec_type = CODEC_TYPE_VIDEO;
  79. st->codec->codec_id = CODEC_ID_FLIC;
  80. st->codec->codec_tag = 0; /* no fourcc */
  81. st->codec->width = AV_RL16(&header[0x08]);
  82. st->codec->height = AV_RL16(&header[0x0A]);
  83. if (!st->codec->width || !st->codec->height) {
  84. /* Ugly hack needed for the following sample: */
  85. /* http://samples.mplayerhq.hu/fli-flc/fli-bugs/specular.flc */
  86. av_log(s, AV_LOG_WARNING,
  87. "File with no specified width/height. Trying 640x480.\n");
  88. st->codec->width = 640;
  89. st->codec->height = 480;
  90. }
  91. /* send over the whole 128-byte FLIC header */
  92. st->codec->extradata_size = FLIC_HEADER_SIZE;
  93. st->codec->extradata = av_malloc(FLIC_HEADER_SIZE);
  94. memcpy(st->codec->extradata, header, FLIC_HEADER_SIZE);
  95. av_set_pts_info(st, 33, 1, 90000);
  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. flic->frame_pts_inc = FLIC_MC_PTS_INC;
  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. /*
  110. * in this case, the speed (n) is number of 1/70s ticks between frames:
  111. *
  112. * pts n * frame #
  113. * -------- = ----------- => pts = n * (90000/70) * frame #
  114. * 90000 70
  115. *
  116. * therefore, the frame pts increment = n * 1285.7
  117. */
  118. flic->frame_pts_inc = speed * 1285.7;
  119. } else if ((magic_number == FLIC_FILE_MAGIC_2) ||
  120. (magic_number == FLIC_FILE_MAGIC_3)) {
  121. /*
  122. * in this case, the speed (n) is number of milliseconds between frames:
  123. *
  124. * pts n * frame #
  125. * -------- = ----------- => pts = n * 90 * frame #
  126. * 90000 1000
  127. *
  128. * therefore, the frame pts increment = n * 90
  129. */
  130. flic->frame_pts_inc = speed * 90;
  131. } else {
  132. av_log(s, AV_LOG_INFO, "Invalid or unsupported magic chunk in file\n");
  133. return AVERROR_INVALIDDATA;
  134. }
  135. if (flic->frame_pts_inc == 0)
  136. flic->frame_pts_inc = FLIC_DEFAULT_PTS_INC;
  137. return 0;
  138. }
  139. static int flic_read_packet(AVFormatContext *s,
  140. AVPacket *pkt)
  141. {
  142. FlicDemuxContext *flic = s->priv_data;
  143. ByteIOContext *pb = &s->pb;
  144. int packet_read = 0;
  145. unsigned int size;
  146. int magic;
  147. int ret = 0;
  148. unsigned char preamble[FLIC_PREAMBLE_SIZE];
  149. while (!packet_read) {
  150. if ((ret = get_buffer(pb, preamble, FLIC_PREAMBLE_SIZE)) !=
  151. FLIC_PREAMBLE_SIZE) {
  152. ret = AVERROR(EIO);
  153. break;
  154. }
  155. size = AV_RL32(&preamble[0]);
  156. magic = AV_RL16(&preamble[4]);
  157. if (((magic == FLIC_CHUNK_MAGIC_1) || (magic == FLIC_CHUNK_MAGIC_2)) && size > FLIC_PREAMBLE_SIZE) {
  158. if (av_new_packet(pkt, size)) {
  159. ret = AVERROR(EIO);
  160. break;
  161. }
  162. pkt->stream_index = flic->video_stream_index;
  163. pkt->pts = flic->pts;
  164. pkt->pos = url_ftell(pb);
  165. memcpy(pkt->data, preamble, FLIC_PREAMBLE_SIZE);
  166. ret = get_buffer(pb, pkt->data + FLIC_PREAMBLE_SIZE,
  167. size - FLIC_PREAMBLE_SIZE);
  168. if (ret != size - FLIC_PREAMBLE_SIZE) {
  169. av_free_packet(pkt);
  170. ret = AVERROR(EIO);
  171. }
  172. flic->pts += flic->frame_pts_inc;
  173. packet_read = 1;
  174. } else {
  175. /* not interested in this chunk */
  176. url_fseek(pb, size - 6, SEEK_CUR);
  177. }
  178. }
  179. return ret;
  180. }
  181. static int flic_read_close(AVFormatContext *s)
  182. {
  183. // FlicDemuxContext *flic = s->priv_data;
  184. return 0;
  185. }
  186. AVInputFormat flic_demuxer = {
  187. "flic",
  188. "FLI/FLC/FLX animation format",
  189. sizeof(FlicDemuxContext),
  190. flic_probe,
  191. flic_read_header,
  192. flic_read_packet,
  193. flic_read_close,
  194. };