bink.c 8.6 KB

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