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.

222 lines
6.8KB

  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. if (p->buf_size < 6)
  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. return AVPROBE_SCORE_MAX;
  59. }
  60. static int flic_read_header(AVFormatContext *s,
  61. AVFormatParameters *ap)
  62. {
  63. FlicDemuxContext *flic = (FlicDemuxContext *)s->priv_data;
  64. ByteIOContext *pb = &s->pb;
  65. unsigned char header[FLIC_HEADER_SIZE];
  66. AVStream *st;
  67. int speed;
  68. int magic_number;
  69. flic->pts = 0;
  70. /* load the whole header and pull out the width and height */
  71. if (get_buffer(pb, header, FLIC_HEADER_SIZE) != FLIC_HEADER_SIZE)
  72. return AVERROR_IO;
  73. magic_number = AV_RL16(&header[4]);
  74. speed = AV_RL32(&header[0x10]);
  75. /* initialize the decoder streams */
  76. st = av_new_stream(s, 0);
  77. if (!st)
  78. return AVERROR_NOMEM;
  79. flic->video_stream_index = st->index;
  80. st->codec->codec_type = CODEC_TYPE_VIDEO;
  81. st->codec->codec_id = CODEC_ID_FLIC;
  82. st->codec->codec_tag = 0; /* no fourcc */
  83. st->codec->width = AV_RL16(&header[0x08]);
  84. st->codec->height = AV_RL16(&header[0x0A]);
  85. if (!st->codec->width || !st->codec->height)
  86. return AVERROR_INVALIDDATA;
  87. /* send over the whole 128-byte FLIC header */
  88. st->codec->extradata_size = FLIC_HEADER_SIZE;
  89. st->codec->extradata = av_malloc(FLIC_HEADER_SIZE);
  90. memcpy(st->codec->extradata, header, FLIC_HEADER_SIZE);
  91. av_set_pts_info(st, 33, 1, 90000);
  92. /* Time to figure out the framerate: If there is a FLIC chunk magic
  93. * number at offset 0x10, assume this is from the Bullfrog game,
  94. * Magic Carpet. */
  95. if (AV_RL16(&header[0x10]) == FLIC_CHUNK_MAGIC_1) {
  96. flic->frame_pts_inc = FLIC_MC_PTS_INC;
  97. /* rewind the stream since the first chunk is at offset 12 */
  98. url_fseek(pb, 12, SEEK_SET);
  99. /* send over abbreviated FLIC header chunk */
  100. av_free(st->codec->extradata);
  101. st->codec->extradata_size = 12;
  102. st->codec->extradata = av_malloc(12);
  103. memcpy(st->codec->extradata, header, 12);
  104. } else if (magic_number == FLIC_FILE_MAGIC_1) {
  105. /*
  106. * in this case, the speed (n) is number of 1/70s ticks between frames:
  107. *
  108. * pts n * frame #
  109. * -------- = ----------- => pts = n * (90000/70) * frame #
  110. * 90000 70
  111. *
  112. * therefore, the frame pts increment = n * 1285.7
  113. */
  114. flic->frame_pts_inc = speed * 1285.7;
  115. } else if ((magic_number == FLIC_FILE_MAGIC_2) ||
  116. (magic_number == FLIC_FILE_MAGIC_3)) {
  117. /*
  118. * in this case, the speed (n) is number of milliseconds between frames:
  119. *
  120. * pts n * frame #
  121. * -------- = ----------- => pts = n * 90 * frame #
  122. * 90000 1000
  123. *
  124. * therefore, the frame pts increment = n * 90
  125. */
  126. flic->frame_pts_inc = speed * 90;
  127. } else
  128. return AVERROR_INVALIDDATA;
  129. if (flic->frame_pts_inc == 0)
  130. flic->frame_pts_inc = FLIC_DEFAULT_PTS_INC;
  131. return 0;
  132. }
  133. static int flic_read_packet(AVFormatContext *s,
  134. AVPacket *pkt)
  135. {
  136. FlicDemuxContext *flic = (FlicDemuxContext *)s->priv_data;
  137. ByteIOContext *pb = &s->pb;
  138. int packet_read = 0;
  139. unsigned int size;
  140. int magic;
  141. int ret = 0;
  142. unsigned char preamble[FLIC_PREAMBLE_SIZE];
  143. while (!packet_read) {
  144. if ((ret = get_buffer(pb, preamble, FLIC_PREAMBLE_SIZE)) !=
  145. FLIC_PREAMBLE_SIZE) {
  146. ret = AVERROR_IO;
  147. break;
  148. }
  149. size = AV_RL32(&preamble[0]);
  150. magic = AV_RL16(&preamble[4]);
  151. if (((magic == FLIC_CHUNK_MAGIC_1) || (magic == FLIC_CHUNK_MAGIC_2)) && size > FLIC_PREAMBLE_SIZE) {
  152. if (av_new_packet(pkt, size)) {
  153. ret = AVERROR_IO;
  154. break;
  155. }
  156. pkt->stream_index = flic->video_stream_index;
  157. pkt->pts = flic->pts;
  158. pkt->pos = url_ftell(pb);
  159. memcpy(pkt->data, preamble, FLIC_PREAMBLE_SIZE);
  160. ret = get_buffer(pb, pkt->data + FLIC_PREAMBLE_SIZE,
  161. size - FLIC_PREAMBLE_SIZE);
  162. if (ret != size - FLIC_PREAMBLE_SIZE) {
  163. av_free_packet(pkt);
  164. ret = AVERROR_IO;
  165. }
  166. flic->pts += flic->frame_pts_inc;
  167. packet_read = 1;
  168. } else {
  169. /* not interested in this chunk */
  170. url_fseek(pb, size - 6, SEEK_CUR);
  171. }
  172. }
  173. return ret;
  174. }
  175. static int flic_read_close(AVFormatContext *s)
  176. {
  177. // FlicDemuxContext *flic = (FlicDemuxContext *)s->priv_data;
  178. return 0;
  179. }
  180. AVInputFormat flic_demuxer = {
  181. "flic",
  182. "FLI/FLC/FLX animation format",
  183. sizeof(FlicDemuxContext),
  184. flic_probe,
  185. flic_read_header,
  186. flic_read_packet,
  187. flic_read_close,
  188. };