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.

288 lines
9.1KB

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