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.

294 lines
9.3KB

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