bink.c 8.7 KB

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