bink.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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/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. ByteIOContext *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 = get_le32(pb);
  74. bink->file_size = get_le32(pb) + 8;
  75. vst->duration = get_le32(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 (get_le32(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. url_fskip(pb, 4);
  86. vst->codec->width = get_le32(pb);
  87. vst->codec->height = get_le32(pb);
  88. fps_num = get_le32(pb);
  89. fps_den = get_le32(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. get_buffer(pb, vst->codec->extradata, 4);
  100. bink->num_audio_tracks = get_le32(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. url_fskip(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 = 0;
  115. ast->codec->sample_rate = get_le16(pb);
  116. av_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
  117. flags = get_le16(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. url_fskip(pb, 4 * bink->num_audio_tracks);
  123. }
  124. /* frame index table */
  125. next_pos = get_le32(pb);
  126. for (i = 0; i < vst->duration; i++) {
  127. pos = next_pos;
  128. if (i == vst->duration - 1) {
  129. next_pos = bink->file_size;
  130. keyframe = 0;
  131. } else {
  132. next_pos = get_le32(pb);
  133. keyframe = pos & 1;
  134. }
  135. pos &= ~1;
  136. next_pos &= ~1;
  137. if (next_pos <= pos) {
  138. av_log(s, AV_LOG_ERROR, "invalid frame index table\n");
  139. return AVERROR(EIO);
  140. }
  141. av_add_index_entry(vst, pos, i, next_pos - pos, 0,
  142. keyframe ? AVINDEX_KEYFRAME : 0);
  143. }
  144. url_fskip(pb, 4);
  145. bink->current_track = -1;
  146. return 0;
  147. }
  148. static int read_packet(AVFormatContext *s, AVPacket *pkt)
  149. {
  150. BinkDemuxContext *bink = s->priv_data;
  151. ByteIOContext *pb = s->pb;
  152. int ret;
  153. if (bink->current_track < 0) {
  154. int index_entry;
  155. AVStream *st = s->streams[0]; // stream 0 is video stream with index
  156. if (bink->video_pts >= st->duration)
  157. return AVERROR(EIO);
  158. index_entry = av_index_search_timestamp(st, bink->video_pts,
  159. AVSEEK_FLAG_ANY);
  160. if (index_entry < 0) {
  161. av_log(s, AV_LOG_ERROR,
  162. "could not find index entry for frame %"PRId64"\n",
  163. bink->video_pts);
  164. return AVERROR(EIO);
  165. }
  166. bink->remain_packet_size = st->index_entries[index_entry].size;
  167. bink->current_track = 0;
  168. }
  169. while (bink->current_track < bink->num_audio_tracks) {
  170. uint32_t audio_size = get_le32(pb);
  171. if (audio_size > bink->remain_packet_size - 4) {
  172. av_log(s, AV_LOG_ERROR,
  173. "frame %"PRId64": audio size in header (%u) > size of packet left (%u)\n",
  174. bink->video_pts, audio_size, bink->remain_packet_size);
  175. return AVERROR(EIO);
  176. }
  177. bink->remain_packet_size -= 4 + audio_size;
  178. bink->current_track++;
  179. if (audio_size >= 4) {
  180. /* get one audio packet per track */
  181. if ((ret = av_get_packet(pb, pkt, audio_size)) < 0)
  182. return ret;
  183. pkt->stream_index = bink->current_track;
  184. pkt->pts = bink->audio_pts[bink->current_track - 1];
  185. /* Each audio packet reports the number of decompressed samples
  186. (in bytes). We use this value to calcuate the audio PTS */
  187. if (pkt->size >= 4)
  188. bink->audio_pts[bink->current_track -1] +=
  189. AV_RL32(pkt->data) / (2 * s->streams[bink->current_track]->codec->channels);
  190. return 0;
  191. } else {
  192. url_fseek(pb, audio_size, SEEK_CUR);
  193. }
  194. }
  195. /* get video packet */
  196. if ((ret = av_get_packet(pb, pkt, bink->remain_packet_size)) < 0)
  197. return ret;
  198. pkt->stream_index = 0;
  199. pkt->pts = bink->video_pts++;
  200. pkt->flags |= AV_PKT_FLAG_KEY;
  201. /* -1 instructs the next call to read_packet() to read the next frame */
  202. bink->current_track = -1;
  203. return 0;
  204. }
  205. static int read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  206. {
  207. BinkDemuxContext *bink = s->priv_data;
  208. AVStream *vst = s->streams[0];
  209. if (url_is_streamed(s->pb))
  210. return -1;
  211. /* seek to the first frame */
  212. url_fseek(s->pb, vst->index_entries[0].pos, SEEK_SET);
  213. bink->video_pts = 0;
  214. memset(bink->audio_pts, 0, sizeof(bink->audio_pts));
  215. bink->current_track = -1;
  216. return 0;
  217. }
  218. AVInputFormat ff_bink_demuxer = {
  219. "bink",
  220. NULL_IF_CONFIG_SMALL("Bink"),
  221. sizeof(BinkDemuxContext),
  222. probe,
  223. read_header,
  224. read_packet,
  225. NULL,
  226. read_seek,
  227. };