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.

284 lines
8.8KB

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