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.

223 lines
6.5KB

  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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_CHUNK_MAGIC_1 0xF1FA
  34. #define FLIC_CHUNK_MAGIC_2 0xF5FA
  35. #define FLIC_MC_PTS_INC 6000 /* pts increment for Magic Carpet game FLIs */
  36. #define FLIC_DEFAULT_PTS_INC 6000 /* for FLIs that have 0 speed */
  37. #define FLIC_HEADER_SIZE 128
  38. #define FLIC_PREAMBLE_SIZE 6
  39. typedef struct FlicDemuxContext {
  40. int frame_pts_inc;
  41. int64_t pts;
  42. int video_stream_index;
  43. } FlicDemuxContext;
  44. static int flic_probe(AVProbeData *p)
  45. {
  46. int magic_number;
  47. if (p->buf_size < 6)
  48. return 0;
  49. magic_number = LE_16(&p->buf[4]);
  50. if ((magic_number != FLIC_FILE_MAGIC_1) &&
  51. (magic_number != FLIC_FILE_MAGIC_2))
  52. return 0;
  53. return AVPROBE_SCORE_MAX;
  54. }
  55. static int flic_read_header(AVFormatContext *s,
  56. AVFormatParameters *ap)
  57. {
  58. FlicDemuxContext *flic = (FlicDemuxContext *)s->priv_data;
  59. ByteIOContext *pb = &s->pb;
  60. unsigned char header[FLIC_HEADER_SIZE];
  61. AVStream *st;
  62. int speed;
  63. int magic_number;
  64. flic->pts = 0;
  65. /* load the whole header and pull out the width and height */
  66. if (get_buffer(pb, header, FLIC_HEADER_SIZE) != FLIC_HEADER_SIZE)
  67. return -EIO;
  68. magic_number = LE_16(&header[4]);
  69. speed = LE_32(&header[0x10]);
  70. /* initialize the decoder streams */
  71. st = av_new_stream(s, 0);
  72. if (!st)
  73. return AVERROR_NOMEM;
  74. flic->video_stream_index = st->index;
  75. st->codec.codec_type = CODEC_TYPE_VIDEO;
  76. st->codec.codec_id = CODEC_ID_FLIC;
  77. st->codec.codec_tag = 0; /* no fourcc */
  78. st->codec.width = LE_16(&header[0x08]);
  79. st->codec.height = LE_16(&header[0x0A]);
  80. if (!st->codec.width || !st->codec.height)
  81. return AVERROR_INVALIDDATA;
  82. /* send over the whole 128-byte FLIC header */
  83. st->codec.extradata_size = FLIC_HEADER_SIZE;
  84. st->codec.extradata = av_malloc(FLIC_HEADER_SIZE);
  85. memcpy(st->codec.extradata, header, FLIC_HEADER_SIZE);
  86. /* set the pts reference (1 pts = 1/90000) */
  87. s->pts_num = 1;
  88. s->pts_den = 90000;
  89. /* Time to figure out the framerate: If there is a FLIC chunk magic
  90. * number at offset 0x10, assume this is from the Bullfrog game,
  91. * Magic Carpet. */
  92. if (LE_16(&header[0x10]) == FLIC_CHUNK_MAGIC_1) {
  93. flic->frame_pts_inc = FLIC_MC_PTS_INC;
  94. /* rewind the stream since the first chunk is at offset 12 */
  95. url_fseek(pb, 12, SEEK_SET);
  96. /* send over abbreviated FLIC header chunk */
  97. av_free(st->codec.extradata);
  98. st->codec.extradata_size = 12;
  99. st->codec.extradata = av_malloc(12);
  100. memcpy(st->codec.extradata, header, 12);
  101. } else if (magic_number == FLIC_FILE_MAGIC_1) {
  102. /*
  103. * in this case, the speed (n) is number of 1/70s ticks between frames:
  104. *
  105. * pts n * frame #
  106. * -------- = ----------- => pts = n * (90000/70) * frame #
  107. * 90000 70
  108. *
  109. * therefore, the frame pts increment = n * 1285.7
  110. */
  111. flic->frame_pts_inc = speed * 1285.7;
  112. } else if (magic_number == FLIC_FILE_MAGIC_2) {
  113. /*
  114. * in this case, the speed (n) is number of milliseconds between frames:
  115. *
  116. * pts n * frame #
  117. * -------- = ----------- => pts = n * 90 * frame #
  118. * 90000 1000
  119. *
  120. * therefore, the frame pts increment = n * 90
  121. */
  122. flic->frame_pts_inc = speed * 90;
  123. } else
  124. return AVERROR_INVALIDDATA;
  125. if (flic->frame_pts_inc == 0)
  126. flic->frame_pts_inc = FLIC_DEFAULT_PTS_INC;
  127. return 0;
  128. }
  129. static int flic_read_packet(AVFormatContext *s,
  130. AVPacket *pkt)
  131. {
  132. FlicDemuxContext *flic = (FlicDemuxContext *)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 = -EIO;
  143. break;
  144. }
  145. size = LE_32(&preamble[0]);
  146. magic = LE_16(&preamble[4]);
  147. if ((magic == FLIC_CHUNK_MAGIC_1) || (magic == FLIC_CHUNK_MAGIC_2)) {
  148. if (av_new_packet(pkt, size)) {
  149. ret = -EIO;
  150. break;
  151. }
  152. pkt->stream_index = flic->video_stream_index;
  153. pkt->pts = flic->pts;
  154. memcpy(pkt->data, preamble, FLIC_PREAMBLE_SIZE);
  155. ret = get_buffer(pb, pkt->data + FLIC_PREAMBLE_SIZE,
  156. size - FLIC_PREAMBLE_SIZE);
  157. if (ret != size - FLIC_PREAMBLE_SIZE) {
  158. av_free_packet(pkt);
  159. ret = -EIO;
  160. }
  161. flic->pts += flic->frame_pts_inc;
  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. static int flic_read_close(AVFormatContext *s)
  171. {
  172. // FlicDemuxContext *flic = (FlicDemuxContext *)s->priv_data;
  173. return 0;
  174. }
  175. static AVInputFormat flic_iformat = {
  176. "flic",
  177. "FLI/FLC animation format",
  178. sizeof(FlicDemuxContext),
  179. flic_probe,
  180. flic_read_header,
  181. flic_read_packet,
  182. flic_read_close,
  183. };
  184. int flic_init(void)
  185. {
  186. av_register_input_format(&flic_iformat);
  187. return 0;
  188. }