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.

220 lines
6.7KB

  1. /*
  2. * FLI/FLC Animation File Demuxer
  3. * Copyright (c) 2003 The ffmpeg Project
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. /**
  20. * @file flic.c
  21. * FLI/FLC file demuxer
  22. * by Mike Melanson (melanson@pcisys.net)
  23. * for more information on the .fli/.flc file format and all of its many
  24. * variations, visit:
  25. * http://www.compuphase.com/flic.htm
  26. *
  27. * This demuxer handles standard 0xAF11- and 0xAF12-type FLIs. It also
  28. * handles special FLIs from the PC game "Magic Carpet".
  29. */
  30. #include "avformat.h"
  31. #define FLIC_FILE_MAGIC_1 0xAF11
  32. #define FLIC_FILE_MAGIC_2 0xAF12
  33. #define FLIC_FILE_MAGIC_3 0xAF44 /* Flic Type for Extended FLX Format which
  34. originated in Dave's Targa Animator (DTA) */
  35. #define FLIC_CHUNK_MAGIC_1 0xF1FA
  36. #define FLIC_CHUNK_MAGIC_2 0xF5FA
  37. #define FLIC_MC_PTS_INC 6000 /* pts increment for Magic Carpet game FLIs */
  38. #define FLIC_DEFAULT_PTS_INC 6000 /* for FLIs that have 0 speed */
  39. #define FLIC_HEADER_SIZE 128
  40. #define FLIC_PREAMBLE_SIZE 6
  41. typedef struct FlicDemuxContext {
  42. int frame_pts_inc;
  43. int64_t pts;
  44. int video_stream_index;
  45. } FlicDemuxContext;
  46. static int flic_probe(AVProbeData *p)
  47. {
  48. int magic_number;
  49. if (p->buf_size < 6)
  50. return 0;
  51. magic_number = LE_16(&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 = (FlicDemuxContext *)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_IO;
  71. magic_number = LE_16(&header[4]);
  72. speed = LE_32(&header[0x10]);
  73. /* initialize the decoder streams */
  74. st = av_new_stream(s, 0);
  75. if (!st)
  76. return AVERROR_NOMEM;
  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 = LE_16(&header[0x08]);
  82. st->codec->height = LE_16(&header[0x0A]);
  83. if (!st->codec->width || !st->codec->height)
  84. return AVERROR_INVALIDDATA;
  85. /* send over the whole 128-byte FLIC header */
  86. st->codec->extradata_size = FLIC_HEADER_SIZE;
  87. st->codec->extradata = av_malloc(FLIC_HEADER_SIZE);
  88. memcpy(st->codec->extradata, header, FLIC_HEADER_SIZE);
  89. av_set_pts_info(st, 33, 1, 90000);
  90. /* Time to figure out the framerate: If there is a FLIC chunk magic
  91. * number at offset 0x10, assume this is from the Bullfrog game,
  92. * Magic Carpet. */
  93. if (LE_16(&header[0x10]) == FLIC_CHUNK_MAGIC_1) {
  94. flic->frame_pts_inc = FLIC_MC_PTS_INC;
  95. /* rewind the stream since the first chunk is at offset 12 */
  96. url_fseek(pb, 12, SEEK_SET);
  97. /* send over abbreviated FLIC header chunk */
  98. av_free(st->codec->extradata);
  99. st->codec->extradata_size = 12;
  100. st->codec->extradata = av_malloc(12);
  101. memcpy(st->codec->extradata, header, 12);
  102. } else if (magic_number == FLIC_FILE_MAGIC_1) {
  103. /*
  104. * in this case, the speed (n) is number of 1/70s ticks between frames:
  105. *
  106. * pts n * frame #
  107. * -------- = ----------- => pts = n * (90000/70) * frame #
  108. * 90000 70
  109. *
  110. * therefore, the frame pts increment = n * 1285.7
  111. */
  112. flic->frame_pts_inc = speed * 1285.7;
  113. } else if ((magic_number == FLIC_FILE_MAGIC_2) ||
  114. (magic_number == FLIC_FILE_MAGIC_3)) {
  115. /*
  116. * in this case, the speed (n) is number of milliseconds between frames:
  117. *
  118. * pts n * frame #
  119. * -------- = ----------- => pts = n * 90 * frame #
  120. * 90000 1000
  121. *
  122. * therefore, the frame pts increment = n * 90
  123. */
  124. flic->frame_pts_inc = speed * 90;
  125. } else
  126. return AVERROR_INVALIDDATA;
  127. if (flic->frame_pts_inc == 0)
  128. flic->frame_pts_inc = FLIC_DEFAULT_PTS_INC;
  129. return 0;
  130. }
  131. static int flic_read_packet(AVFormatContext *s,
  132. AVPacket *pkt)
  133. {
  134. FlicDemuxContext *flic = (FlicDemuxContext *)s->priv_data;
  135. ByteIOContext *pb = &s->pb;
  136. int packet_read = 0;
  137. unsigned int size;
  138. int magic;
  139. int ret = 0;
  140. unsigned char preamble[FLIC_PREAMBLE_SIZE];
  141. while (!packet_read) {
  142. if ((ret = get_buffer(pb, preamble, FLIC_PREAMBLE_SIZE)) !=
  143. FLIC_PREAMBLE_SIZE) {
  144. ret = AVERROR_IO;
  145. break;
  146. }
  147. size = LE_32(&preamble[0]);
  148. magic = LE_16(&preamble[4]);
  149. if (((magic == FLIC_CHUNK_MAGIC_1) || (magic == FLIC_CHUNK_MAGIC_2)) && size > FLIC_PREAMBLE_SIZE) {
  150. if (av_new_packet(pkt, size)) {
  151. ret = AVERROR_IO;
  152. break;
  153. }
  154. pkt->stream_index = flic->video_stream_index;
  155. pkt->pts = flic->pts;
  156. pkt->pos = url_ftell(pb);
  157. memcpy(pkt->data, preamble, FLIC_PREAMBLE_SIZE);
  158. ret = get_buffer(pb, pkt->data + FLIC_PREAMBLE_SIZE,
  159. size - FLIC_PREAMBLE_SIZE);
  160. if (ret != size - FLIC_PREAMBLE_SIZE) {
  161. av_free_packet(pkt);
  162. ret = AVERROR_IO;
  163. }
  164. flic->pts += flic->frame_pts_inc;
  165. packet_read = 1;
  166. } else {
  167. /* not interested in this chunk */
  168. url_fseek(pb, size - 6, SEEK_CUR);
  169. }
  170. }
  171. return ret;
  172. }
  173. static int flic_read_close(AVFormatContext *s)
  174. {
  175. // FlicDemuxContext *flic = (FlicDemuxContext *)s->priv_data;
  176. return 0;
  177. }
  178. AVInputFormat flic_demuxer = {
  179. "flic",
  180. "FLI/FLC/FLX animation format",
  181. sizeof(FlicDemuxContext),
  182. flic_probe,
  183. flic_read_header,
  184. flic_read_packet,
  185. flic_read_close,
  186. };