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.

229 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., 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 LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
  32. #define LE_32(x) ((((uint8_t*)(x))[3] << 24) | \
  33. (((uint8_t*)(x))[2] << 16) | \
  34. (((uint8_t*)(x))[1] << 8) | \
  35. ((uint8_t*)(x))[0])
  36. #define FLIC_FILE_MAGIC_1 0xAF11
  37. #define FLIC_FILE_MAGIC_2 0xAF12
  38. #define FLIC_CHUNK_MAGIC_1 0xF1FA
  39. #define FLIC_CHUNK_MAGIC_2 0xF5FA
  40. #define FLIC_MC_PTS_INC 6000 /* pts increment for Magic Carpet game FLIs */
  41. #define FLIC_DEFAULT_PTS_INC 6000 /* for FLIs that have 0 speed */
  42. #define FLIC_HEADER_SIZE 128
  43. #define FLIC_PREAMBLE_SIZE 6
  44. typedef struct FlicDemuxContext {
  45. int frame_pts_inc;
  46. int64_t pts;
  47. int video_stream_index;
  48. } FlicDemuxContext;
  49. static int flic_probe(AVProbeData *p)
  50. {
  51. int magic_number;
  52. if (p->buf_size < 6)
  53. return 0;
  54. magic_number = LE_16(&p->buf[4]);
  55. if ((magic_number != FLIC_FILE_MAGIC_1) &&
  56. (magic_number != FLIC_FILE_MAGIC_2))
  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 -EIO;
  73. magic_number = LE_16(&header[4]);
  74. speed = LE_32(&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 = LE_16(&header[0x08]);
  84. st->codec.height = LE_16(&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. /* set the pts reference (1 pts = 1/90000) */
  92. s->pts_num = 1;
  93. s->pts_den = 90000;
  94. /* Time to figure out the framerate: If there is a FLIC chunk magic
  95. * number at offset 0x10, assume this is from the Bullfrog game,
  96. * Magic Carpet. */
  97. if (LE_16(&header[0x10]) == FLIC_CHUNK_MAGIC_1) {
  98. flic->frame_pts_inc = FLIC_MC_PTS_INC;
  99. /* rewind the stream since the first chunk is at offset 12 */
  100. url_fseek(pb, 12, SEEK_SET);
  101. /* send over abbreviated FLIC header chunk */
  102. av_free(st->codec.extradata);
  103. st->codec.extradata_size = 12;
  104. st->codec.extradata = av_malloc(12);
  105. memcpy(st->codec.extradata, header, 12);
  106. } else if (magic_number == FLIC_FILE_MAGIC_1) {
  107. /*
  108. * in this case, the speed (n) is number of 1/70s ticks between frames:
  109. *
  110. * pts n * frame #
  111. * -------- = ----------- => pts = n * (90000/70) * frame #
  112. * 90000 70
  113. *
  114. * therefore, the frame pts increment = n * 1285.7
  115. */
  116. flic->frame_pts_inc = speed * 1285.7;
  117. } else if (magic_number == FLIC_FILE_MAGIC_2) {
  118. /*
  119. * in this case, the speed (n) is number of milliseconds between frames:
  120. *
  121. * pts n * frame #
  122. * -------- = ----------- => pts = n * 90 * frame #
  123. * 90000 1000
  124. *
  125. * therefore, the frame pts increment = n * 90
  126. */
  127. flic->frame_pts_inc = speed * 90;
  128. } else
  129. return AVERROR_INVALIDDATA;
  130. if (flic->frame_pts_inc == 0)
  131. flic->frame_pts_inc = FLIC_DEFAULT_PTS_INC;
  132. return 0;
  133. }
  134. static int flic_read_packet(AVFormatContext *s,
  135. AVPacket *pkt)
  136. {
  137. FlicDemuxContext *flic = (FlicDemuxContext *)s->priv_data;
  138. ByteIOContext *pb = &s->pb;
  139. int packet_read = 0;
  140. unsigned int size;
  141. int magic;
  142. int ret = 0;
  143. unsigned char preamble[FLIC_PREAMBLE_SIZE];
  144. while (!packet_read) {
  145. if ((ret = get_buffer(pb, preamble, FLIC_PREAMBLE_SIZE)) !=
  146. FLIC_PREAMBLE_SIZE) {
  147. ret = -EIO;
  148. break;
  149. }
  150. size = LE_32(&preamble[0]);
  151. magic = LE_16(&preamble[4]);
  152. if ((magic == FLIC_CHUNK_MAGIC_1) || (magic == FLIC_CHUNK_MAGIC_2)) {
  153. if (av_new_packet(pkt, size)) {
  154. ret = -EIO;
  155. break;
  156. }
  157. pkt->stream_index = flic->video_stream_index;
  158. pkt->pts = flic->pts;
  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 = -EIO;
  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. static AVInputFormat flic_iformat = {
  181. "flic",
  182. "FLI/FLC animation format",
  183. sizeof(FlicDemuxContext),
  184. flic_probe,
  185. flic_read_header,
  186. flic_read_packet,
  187. flic_read_close,
  188. };
  189. int flic_init(void)
  190. {
  191. av_register_input_format(&flic_iformat);
  192. return 0;
  193. }