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.

283 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_get_extradata(vst->codec, pb, 4) < 0)
  101. return AVERROR(ENOMEM);
  102. bink->num_audio_tracks = avio_rl32(pb);
  103. if (bink->num_audio_tracks > BINK_MAX_AUDIO_TRACKS) {
  104. av_log(s, AV_LOG_ERROR,
  105. "invalid header: more than "AV_STRINGIFY(BINK_MAX_AUDIO_TRACKS)" audio tracks (%d)\n",
  106. bink->num_audio_tracks);
  107. return AVERROR(EIO);
  108. }
  109. if (bink->num_audio_tracks) {
  110. avio_skip(pb, 4 * bink->num_audio_tracks);
  111. for (i = 0; i < bink->num_audio_tracks; i++) {
  112. ast = avformat_new_stream(s, NULL);
  113. if (!ast)
  114. return AVERROR(ENOMEM);
  115. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  116. ast->codec->codec_tag = 0;
  117. ast->codec->sample_rate = avio_rl16(pb);
  118. avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
  119. flags = avio_rl16(pb);
  120. ast->codec->codec_id = flags & BINK_AUD_USEDCT ?
  121. AV_CODEC_ID_BINKAUDIO_DCT : AV_CODEC_ID_BINKAUDIO_RDFT;
  122. if (flags & BINK_AUD_STEREO) {
  123. ast->codec->channels = 2;
  124. ast->codec->channel_layout = AV_CH_LAYOUT_STEREO;
  125. } else {
  126. ast->codec->channels = 1;
  127. ast->codec->channel_layout = AV_CH_LAYOUT_MONO;
  128. }
  129. if (ff_alloc_extradata(ast->codec, 4))
  130. return AVERROR(ENOMEM);
  131. AV_WL32(ast->codec->extradata, vst->codec->codec_tag);
  132. }
  133. for (i = 0; i < bink->num_audio_tracks; i++)
  134. s->streams[i + 1]->id = avio_rl32(pb);
  135. }
  136. /* frame index table */
  137. next_pos = avio_rl32(pb);
  138. for (i = 0; i < vst->duration; i++) {
  139. pos = next_pos;
  140. if (i == vst->duration - 1) {
  141. next_pos = bink->file_size;
  142. keyframe = 0;
  143. } else {
  144. next_pos = avio_rl32(pb);
  145. keyframe = pos & 1;
  146. }
  147. pos &= ~1;
  148. next_pos &= ~1;
  149. if (next_pos <= pos) {
  150. av_log(s, AV_LOG_ERROR, "invalid frame index table\n");
  151. return AVERROR(EIO);
  152. }
  153. av_add_index_entry(vst, pos, i, next_pos - pos, 0,
  154. keyframe ? AVINDEX_KEYFRAME : 0);
  155. }
  156. avio_skip(pb, 4);
  157. bink->current_track = -1;
  158. return 0;
  159. }
  160. static int read_packet(AVFormatContext *s, AVPacket *pkt)
  161. {
  162. BinkDemuxContext *bink = s->priv_data;
  163. AVIOContext *pb = s->pb;
  164. int ret;
  165. if (bink->current_track < 0) {
  166. int index_entry;
  167. AVStream *st = s->streams[0]; // stream 0 is video stream with index
  168. if (bink->video_pts >= st->duration)
  169. return AVERROR_EOF;
  170. index_entry = av_index_search_timestamp(st, bink->video_pts,
  171. AVSEEK_FLAG_ANY);
  172. if (index_entry < 0) {
  173. av_log(s, AV_LOG_ERROR,
  174. "could not find index entry for frame %"PRId64"\n",
  175. bink->video_pts);
  176. return AVERROR(EIO);
  177. }
  178. bink->remain_packet_size = st->index_entries[index_entry].size;
  179. bink->current_track = 0;
  180. }
  181. while (bink->current_track < bink->num_audio_tracks) {
  182. uint32_t audio_size = avio_rl32(pb);
  183. if (audio_size > bink->remain_packet_size - 4) {
  184. av_log(s, AV_LOG_ERROR,
  185. "frame %"PRId64": audio size in header (%u) > size of packet left (%u)\n",
  186. bink->video_pts, audio_size, bink->remain_packet_size);
  187. return AVERROR(EIO);
  188. }
  189. bink->remain_packet_size -= 4 + audio_size;
  190. bink->current_track++;
  191. if (audio_size >= 4) {
  192. /* get one audio packet per track */
  193. if ((ret = av_get_packet(pb, pkt, audio_size)) < 0)
  194. return ret;
  195. pkt->stream_index = bink->current_track;
  196. pkt->pts = bink->audio_pts[bink->current_track - 1];
  197. /* Each audio packet reports the number of decompressed samples
  198. (in bytes). We use this value to calcuate the audio PTS */
  199. if (pkt->size >= 4)
  200. bink->audio_pts[bink->current_track -1] +=
  201. AV_RL32(pkt->data) / (2 * s->streams[bink->current_track]->codec->channels);
  202. return 0;
  203. } else {
  204. avio_skip(pb, audio_size);
  205. }
  206. }
  207. /* get video packet */
  208. if ((ret = av_get_packet(pb, pkt, bink->remain_packet_size)) < 0)
  209. return ret;
  210. pkt->stream_index = 0;
  211. pkt->pts = bink->video_pts++;
  212. pkt->flags |= AV_PKT_FLAG_KEY;
  213. /* -1 instructs the next call to read_packet() to read the next frame */
  214. bink->current_track = -1;
  215. return 0;
  216. }
  217. static int read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  218. {
  219. BinkDemuxContext *bink = s->priv_data;
  220. AVStream *vst = s->streams[0];
  221. if (!s->pb->seekable)
  222. return -1;
  223. /* seek to the first frame */
  224. if (avio_seek(s->pb, vst->index_entries[0].pos, SEEK_SET) < 0)
  225. return -1;
  226. bink->video_pts = 0;
  227. memset(bink->audio_pts, 0, sizeof(bink->audio_pts));
  228. bink->current_track = -1;
  229. return 0;
  230. }
  231. AVInputFormat ff_bink_demuxer = {
  232. .name = "bink",
  233. .long_name = NULL_IF_CONFIG_SMALL("Bink"),
  234. .priv_data_size = sizeof(BinkDemuxContext),
  235. .read_probe = probe,
  236. .read_header = read_header,
  237. .read_packet = read_packet,
  238. .read_seek = read_seek,
  239. };