bink.c 8.3 KB

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