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.

273 lines
8.4KB

  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
  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 num_audio_tracks;
  43. int current_track; ///< audio track to return in next packet
  44. int64_t video_pts;
  45. int64_t audio_pts[BINK_MAX_AUDIO_TRACKS];
  46. uint32_t remain_packet_size;
  47. } BinkDemuxContext;
  48. static int probe(AVProbeData *p)
  49. {
  50. const uint8_t *b = p->buf;
  51. if ( b[0] == 'B' && b[1] == 'I' && b[2] == 'K' &&
  52. (b[3] == 'b' || b[3] == 'f' || b[3] == 'g' || b[3] == 'h' || b[3] == 'i') &&
  53. AV_RL32(b+8) > 0 && // num_frames
  54. AV_RL32(b+20) > 0 && AV_RL32(b+20) <= BINK_MAX_WIDTH &&
  55. AV_RL32(b+24) > 0 && AV_RL32(b+24) <= BINK_MAX_HEIGHT &&
  56. AV_RL32(b+28) > 0 && AV_RL32(b+32) > 0) // fps num,den
  57. return AVPROBE_SCORE_MAX;
  58. return 0;
  59. }
  60. static int read_header(AVFormatContext *s, AVFormatParameters *ap)
  61. {
  62. BinkDemuxContext *bink = s->priv_data;
  63. AVIOContext *pb = s->pb;
  64. uint32_t fps_num, fps_den;
  65. AVStream *vst, *ast;
  66. unsigned int i;
  67. uint32_t pos, next_pos;
  68. uint16_t flags;
  69. int keyframe;
  70. vst = av_new_stream(s, 0);
  71. if (!vst)
  72. return AVERROR(ENOMEM);
  73. vst->codec->codec_tag = avio_rl32(pb);
  74. bink->file_size = avio_rl32(pb) + 8;
  75. vst->duration = avio_rl32(pb);
  76. if (vst->duration > 1000000) {
  77. av_log(s, AV_LOG_ERROR, "invalid header: more than 1000000 frames\n");
  78. return AVERROR(EIO);
  79. }
  80. if (avio_rl32(pb) > bink->file_size) {
  81. av_log(s, AV_LOG_ERROR,
  82. "invalid header: largest frame size greater than file size\n");
  83. return AVERROR(EIO);
  84. }
  85. avio_skip(pb, 4);
  86. vst->codec->width = avio_rl32(pb);
  87. vst->codec->height = avio_rl32(pb);
  88. fps_num = avio_rl32(pb);
  89. fps_den = avio_rl32(pb);
  90. if (fps_num == 0 || fps_den == 0) {
  91. av_log(s, AV_LOG_ERROR, "invalid header: invalid fps (%d/%d)\n", fps_num, fps_den);
  92. return AVERROR(EIO);
  93. }
  94. av_set_pts_info(vst, 64, fps_den, fps_num);
  95. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  96. vst->codec->codec_id = CODEC_ID_BINKVIDEO;
  97. vst->codec->extradata = av_mallocz(4 + FF_INPUT_BUFFER_PADDING_SIZE);
  98. vst->codec->extradata_size = 4;
  99. avio_read(pb, vst->codec->extradata, 4);
  100. bink->num_audio_tracks = avio_rl32(pb);
  101. if (bink->num_audio_tracks > BINK_MAX_AUDIO_TRACKS) {
  102. av_log(s, AV_LOG_ERROR,
  103. "invalid header: more than "AV_STRINGIFY(BINK_MAX_AUDIO_TRACKS)" audio tracks (%d)\n",
  104. bink->num_audio_tracks);
  105. return AVERROR(EIO);
  106. }
  107. if (bink->num_audio_tracks) {
  108. avio_skip(pb, 4 * bink->num_audio_tracks);
  109. for (i = 0; i < bink->num_audio_tracks; i++) {
  110. ast = av_new_stream(s, 1);
  111. if (!ast)
  112. return AVERROR(ENOMEM);
  113. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  114. ast->codec->sample_rate = avio_rl16(pb);
  115. av_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
  116. flags = avio_rl16(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. ast->codec->extradata = av_mallocz(1 + FF_INPUT_BUFFER_PADDING_SIZE);
  121. ast->codec->extradata_size = 1;
  122. ast->codec->extradata[0] = vst->codec->codec_tag == MKTAG('B','I','K','b');
  123. }
  124. for (i = 0; i < bink->num_audio_tracks; i++)
  125. s->streams[i + 1]->id = avio_rl32(pb);
  126. }
  127. /* frame index table */
  128. next_pos = avio_rl32(pb);
  129. for (i = 0; i < vst->duration; i++) {
  130. pos = next_pos;
  131. if (i == vst->duration - 1) {
  132. next_pos = bink->file_size;
  133. keyframe = 0;
  134. } else {
  135. next_pos = avio_rl32(pb);
  136. keyframe = pos & 1;
  137. }
  138. pos &= ~1;
  139. next_pos &= ~1;
  140. if (next_pos <= pos) {
  141. av_log(s, AV_LOG_ERROR, "invalid frame index table\n");
  142. return AVERROR(EIO);
  143. }
  144. av_add_index_entry(vst, pos, i, next_pos - pos, 0,
  145. keyframe ? AVINDEX_KEYFRAME : 0);
  146. }
  147. avio_skip(pb, 4);
  148. bink->current_track = -1;
  149. return 0;
  150. }
  151. static int read_packet(AVFormatContext *s, AVPacket *pkt)
  152. {
  153. BinkDemuxContext *bink = s->priv_data;
  154. AVIOContext *pb = s->pb;
  155. int ret;
  156. if (bink->current_track < 0) {
  157. int index_entry;
  158. AVStream *st = s->streams[0]; // stream 0 is video stream with index
  159. if (bink->video_pts >= st->duration)
  160. return AVERROR(EIO);
  161. index_entry = av_index_search_timestamp(st, bink->video_pts,
  162. AVSEEK_FLAG_ANY);
  163. if (index_entry < 0) {
  164. av_log(s, AV_LOG_ERROR,
  165. "could not find index entry for frame %"PRId64"\n",
  166. bink->video_pts);
  167. return AVERROR(EIO);
  168. }
  169. bink->remain_packet_size = st->index_entries[index_entry].size;
  170. bink->current_track = 0;
  171. }
  172. while (bink->current_track < bink->num_audio_tracks) {
  173. uint32_t audio_size = avio_rl32(pb);
  174. if (audio_size > bink->remain_packet_size - 4) {
  175. av_log(s, AV_LOG_ERROR,
  176. "frame %"PRId64": audio size in header (%u) > size of packet left (%u)\n",
  177. bink->video_pts, audio_size, bink->remain_packet_size);
  178. return AVERROR(EIO);
  179. }
  180. bink->remain_packet_size -= 4 + audio_size;
  181. bink->current_track++;
  182. if (audio_size >= 4) {
  183. /* get one audio packet per track */
  184. if ((ret = av_get_packet(pb, pkt, audio_size)) < 0)
  185. return ret;
  186. pkt->stream_index = bink->current_track;
  187. pkt->pts = bink->audio_pts[bink->current_track - 1];
  188. /* Each audio packet reports the number of decompressed samples
  189. (in bytes). We use this value to calcuate the audio PTS */
  190. if (pkt->size >= 4)
  191. bink->audio_pts[bink->current_track -1] +=
  192. AV_RL32(pkt->data) / (2 * s->streams[bink->current_track]->codec->channels);
  193. return 0;
  194. } else {
  195. avio_skip(pb, audio_size);
  196. }
  197. }
  198. /* get video packet */
  199. if ((ret = av_get_packet(pb, pkt, bink->remain_packet_size)) < 0)
  200. return ret;
  201. pkt->stream_index = 0;
  202. pkt->pts = bink->video_pts++;
  203. pkt->flags |= AV_PKT_FLAG_KEY;
  204. /* -1 instructs the next call to read_packet() to read the next frame */
  205. bink->current_track = -1;
  206. return 0;
  207. }
  208. static int read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  209. {
  210. BinkDemuxContext *bink = s->priv_data;
  211. AVStream *vst = s->streams[0];
  212. if (!s->pb->seekable)
  213. return -1;
  214. /* seek to the first frame */
  215. avio_seek(s->pb, vst->index_entries[0].pos, SEEK_SET);
  216. bink->video_pts = 0;
  217. memset(bink->audio_pts, 0, sizeof(bink->audio_pts));
  218. bink->current_track = -1;
  219. return 0;
  220. }
  221. AVInputFormat ff_bink_demuxer = {
  222. "bink",
  223. NULL_IF_CONFIG_SMALL("Bink"),
  224. sizeof(BinkDemuxContext),
  225. probe,
  226. read_header,
  227. read_packet,
  228. NULL,
  229. read_seek,
  230. };