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.

285 lines
8.9KB

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