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.

248 lines
7.6KB

  1. /*
  2. * Bink demuxer
  3. * Copyright (c) 2008-2010 Peter Ross (pross@xvid.org)
  4. * Copyright (c) 2009 Daniel Verkamp (daniel@drv.nu)
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file libavformat/bink.c
  24. * Bink demuxer
  25. *
  26. * Technical details here:
  27. * http://wiki.multimedia.cx/index.php?title=Bink_Container
  28. */
  29. #include "libavutil/intreadwrite.h"
  30. #include "avformat.h"
  31. enum BinkAudFlags {
  32. BINK_AUD_16BITS = 0x4000, ///< prefer 16-bit output
  33. BINK_AUD_STEREO = 0x2000,
  34. BINK_AUD_USEDCT = 0x1000,
  35. };
  36. #define BINK_EXTRADATA_SIZE 1
  37. #define BINK_MAX_AUDIO_TRACKS 256
  38. #define BINK_MAX_WIDTH 7680
  39. #define BINK_MAX_HEIGHT 4800
  40. typedef struct {
  41. uint32_t file_size;
  42. uint32_t total_frames;
  43. uint32_t num_audio_tracks;
  44. int current_track; ///< audio track to return in next packet
  45. int64_t video_pts;
  46. int64_t audio_pts[BINK_MAX_AUDIO_TRACKS];
  47. uint32_t remain_packet_size;
  48. } BinkDemuxContext;
  49. static int probe(AVProbeData *p)
  50. {
  51. const uint8_t *b = p->buf;
  52. if ( b[0] == 'B' && b[1] == 'I' && b[2] == 'K' &&
  53. (b[3] == 'b' || b[3] == 'f' || b[3] == 'g' || b[3] == 'h' || b[3] == 'i') &&
  54. AV_RL32(b+8) > 0 && // num_frames
  55. AV_RL32(b+20) > 0 && AV_RL32(b+20) <= BINK_MAX_WIDTH &&
  56. AV_RL32(b+24) > 0 && AV_RL32(b+24) <= BINK_MAX_HEIGHT &&
  57. AV_RL32(b+28) > 0 && AV_RL32(b+32) > 0) // fps num,den
  58. return AVPROBE_SCORE_MAX;
  59. return 0;
  60. }
  61. static int read_header(AVFormatContext *s, AVFormatParameters *ap)
  62. {
  63. BinkDemuxContext *bink = s->priv_data;
  64. ByteIOContext *pb = s->pb;
  65. uint32_t fps_num, fps_den;
  66. AVStream *vst, *ast;
  67. unsigned int i;
  68. uint32_t pos, prev_pos;
  69. uint16_t flags;
  70. int keyframe;
  71. vst = av_new_stream(s, 0);
  72. if (!vst)
  73. return AVERROR(ENOMEM);
  74. vst->codec->codec_tag = get_le32(pb);
  75. bink->file_size = get_le32(pb) + 8;
  76. bink->total_frames = get_le32(pb);
  77. if (bink->total_frames > 1000000) {
  78. av_log(s, AV_LOG_ERROR, "invalid header: more than 1000000 frames\n");
  79. return AVERROR(EIO);
  80. }
  81. if (get_le32(pb) > bink->file_size) {
  82. av_log(s, AV_LOG_ERROR,
  83. "invalid header: largest frame size greater than file size\n");
  84. return AVERROR(EIO);
  85. }
  86. url_fskip(pb, 4);
  87. vst->codec->width = get_le32(pb);
  88. vst->codec->height = get_le32(pb);
  89. fps_num = get_le32(pb);
  90. fps_den = get_le32(pb);
  91. if (fps_num == 0 || fps_den == 0) {
  92. av_log(s, AV_LOG_ERROR, "invalid header: invalid fps (%d/%d)\n", fps_num, fps_den);
  93. return AVERROR(EIO);
  94. }
  95. av_set_pts_info(vst, 64, fps_den, fps_num);
  96. url_fskip(pb, 4);
  97. vst->codec->codec_type = CODEC_TYPE_VIDEO;
  98. vst->codec->codec_id = CODEC_ID_BINKVIDEO;
  99. bink->num_audio_tracks = get_le32(pb);
  100. if (bink->num_audio_tracks > BINK_MAX_AUDIO_TRACKS) {
  101. av_log(s, AV_LOG_ERROR,
  102. "invalid header: more than "AV_STRINGIFY(BINK_MAX_AUDIO_TRACKS)" audio tracks (%d)\n",
  103. bink->num_audio_tracks);
  104. return AVERROR(EIO);
  105. }
  106. if (bink->num_audio_tracks) {
  107. url_fskip(pb, 4 * bink->num_audio_tracks);
  108. for (i = 0; i < bink->num_audio_tracks; i++) {
  109. ast = av_new_stream(s, 1);
  110. if (!ast)
  111. return AVERROR(ENOMEM);
  112. ast->codec->codec_type = CODEC_TYPE_AUDIO;
  113. ast->codec->codec_tag = 0;
  114. ast->codec->sample_rate = get_le16(pb);
  115. av_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
  116. flags = get_le16(pb);
  117. ast->codec->codec_id = flags & BINK_AUD_USEDCT ?
  118. CODEC_ID_BINKAUDIO_DCT : CODEC_ID_BINKAUDIO_RDFT;
  119. ast->codec->channels = flags & BINK_AUD_STEREO ? 2 : 1;
  120. }
  121. url_fskip(pb, 4 * bink->num_audio_tracks);
  122. }
  123. /* frame index table */
  124. pos = get_le32(pb) & ~1;
  125. for (i = 0; i < bink->total_frames; i++) {
  126. prev_pos = pos;
  127. if (i == bink->total_frames - 1) {
  128. pos = bink->file_size;
  129. keyframe = 0;
  130. } else {
  131. pos = get_le32(pb);
  132. keyframe = pos & 1;
  133. pos &= ~1;
  134. }
  135. if (pos <= prev_pos) {
  136. av_log(s, AV_LOG_ERROR, "invalid frame index table\n");
  137. return AVERROR(EIO);
  138. }
  139. av_add_index_entry(vst, pos, i, pos - prev_pos, 0,
  140. keyframe ? AVINDEX_KEYFRAME : 0);
  141. }
  142. url_fskip(pb, 4);
  143. bink->current_track = -1;
  144. return 0;
  145. }
  146. static int read_packet(AVFormatContext *s, AVPacket *pkt)
  147. {
  148. BinkDemuxContext *bink = s->priv_data;
  149. ByteIOContext *pb = s->pb;
  150. int ret;
  151. if (bink->current_track < 0) {
  152. int index_entry;
  153. AVStream *st = s->streams[0]; // stream 0 is video stream with index
  154. if (bink->video_pts >= bink->total_frames)
  155. return AVERROR(EIO);
  156. index_entry = av_index_search_timestamp(st, bink->video_pts,
  157. AVSEEK_FLAG_ANY);
  158. if (index_entry < 0) {
  159. av_log(s, AV_LOG_ERROR,
  160. "could not find index entry for frame %"PRId64"\n",
  161. bink->video_pts);
  162. return AVERROR(EIO);
  163. }
  164. bink->remain_packet_size = st->index_entries[index_entry].size;
  165. bink->current_track = 0;
  166. }
  167. if (bink->current_track < bink->num_audio_tracks) {
  168. uint32_t audio_size = get_le32(pb);
  169. if (audio_size > bink->remain_packet_size - 4) {
  170. av_log(s, AV_LOG_ERROR,
  171. "frame %"PRId64": audio size in header (%u) > size of packet left (%u)\n",
  172. bink->video_pts, audio_size, bink->remain_packet_size);
  173. return AVERROR(EIO);
  174. }
  175. bink->remain_packet_size -= 4 + audio_size;
  176. bink->current_track++;
  177. if (audio_size > 0) {
  178. /* Each audio packet reports the number of decompressed samples
  179. (in bytes). We use this value to calcuate the audio PTS */
  180. int reported_size = get_le32(pb) / (2 * s->streams[bink->current_track]->codec->channels);
  181. url_fseek(pb, -4, SEEK_CUR);
  182. /* get one audio packet per track */
  183. if ((ret = av_get_packet(pb, pkt, audio_size))
  184. != audio_size)
  185. return ret;
  186. pkt->stream_index = bink->current_track;
  187. pkt->pts = bink->audio_pts[bink->current_track - 1] += reported_size;
  188. return 0;
  189. }
  190. }
  191. /* get video packet */
  192. if ((ret = av_get_packet(pb, pkt, bink->remain_packet_size))
  193. != bink->remain_packet_size)
  194. return ret;
  195. pkt->stream_index = 0;
  196. pkt->pts = bink->video_pts++;
  197. pkt->flags |= PKT_FLAG_KEY;
  198. /* -1 instructs the next call to read_packet() to read the next frame */
  199. bink->current_track = -1;
  200. return 0;
  201. }
  202. AVInputFormat bink_demuxer = {
  203. "bink",
  204. NULL_IF_CONFIG_SMALL("Bink"),
  205. sizeof(BinkDemuxContext),
  206. probe,
  207. read_header,
  208. read_packet,
  209. };