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.

292 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 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 <inttypes.h>
  30. #include "libavutil/channel_layout.h"
  31. #include "libavutil/intreadwrite.h"
  32. #include "avformat.h"
  33. #include "internal.h"
  34. enum BinkAudFlags {
  35. BINK_AUD_16BITS = 0x4000, ///< prefer 16-bit output
  36. BINK_AUD_STEREO = 0x2000,
  37. BINK_AUD_USEDCT = 0x1000,
  38. };
  39. #define BINK_EXTRADATA_SIZE 1
  40. #define BINK_MAX_AUDIO_TRACKS 256
  41. #define BINK_MAX_WIDTH 7680
  42. #define BINK_MAX_HEIGHT 4800
  43. typedef struct BinkDemuxContext {
  44. uint32_t file_size;
  45. uint32_t num_audio_tracks;
  46. int current_track; ///< audio track to return in next packet
  47. int64_t video_pts;
  48. int64_t audio_pts[BINK_MAX_AUDIO_TRACKS];
  49. uint32_t remain_packet_size;
  50. } BinkDemuxContext;
  51. static int probe(AVProbeData *p)
  52. {
  53. const uint8_t *b = p->buf;
  54. if ( b[0] == 'B' && b[1] == 'I' && b[2] == 'K' &&
  55. (b[3] == 'b' || b[3] == 'f' || b[3] == 'g' || b[3] == 'h' || b[3] == 'i') &&
  56. AV_RL32(b+8) > 0 && // num_frames
  57. AV_RL32(b+20) > 0 && AV_RL32(b+20) <= BINK_MAX_WIDTH &&
  58. AV_RL32(b+24) > 0 && AV_RL32(b+24) <= BINK_MAX_HEIGHT &&
  59. AV_RL32(b+28) > 0 && AV_RL32(b+32) > 0) // fps num,den
  60. return AVPROBE_SCORE_MAX;
  61. return 0;
  62. }
  63. static int read_header(AVFormatContext *s)
  64. {
  65. BinkDemuxContext *bink = s->priv_data;
  66. AVIOContext *pb = s->pb;
  67. uint32_t fps_num, fps_den;
  68. AVStream *vst, *ast;
  69. unsigned int i;
  70. uint32_t pos, next_pos;
  71. uint16_t flags;
  72. int keyframe;
  73. vst = avformat_new_stream(s, NULL);
  74. if (!vst)
  75. return AVERROR(ENOMEM);
  76. vst->codecpar->codec_tag = avio_rl32(pb);
  77. bink->file_size = avio_rl32(pb) + 8;
  78. vst->duration = avio_rl32(pb);
  79. if (vst->duration > 1000000) {
  80. av_log(s, AV_LOG_ERROR, "invalid header: more than 1000000 frames\n");
  81. return AVERROR(EIO);
  82. }
  83. if (avio_rl32(pb) > bink->file_size) {
  84. av_log(s, AV_LOG_ERROR,
  85. "invalid header: largest frame size greater than file size\n");
  86. return AVERROR(EIO);
  87. }
  88. avio_skip(pb, 4);
  89. vst->codecpar->width = avio_rl32(pb);
  90. vst->codecpar->height = avio_rl32(pb);
  91. fps_num = avio_rl32(pb);
  92. fps_den = avio_rl32(pb);
  93. if (fps_num == 0 || fps_den == 0) {
  94. av_log(s, AV_LOG_ERROR,
  95. "invalid header: invalid fps (%"PRIu32"/%"PRIu32")\n",
  96. 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->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  102. vst->codecpar->codec_id = AV_CODEC_ID_BINKVIDEO;
  103. vst->codecpar->extradata = av_mallocz(4 + AV_INPUT_BUFFER_PADDING_SIZE);
  104. if (!vst->codecpar->extradata)
  105. return AVERROR(ENOMEM);
  106. vst->codecpar->extradata_size = 4;
  107. avio_read(pb, vst->codecpar->extradata, 4);
  108. bink->num_audio_tracks = avio_rl32(pb);
  109. if (bink->num_audio_tracks > BINK_MAX_AUDIO_TRACKS) {
  110. av_log(s, AV_LOG_ERROR,
  111. "invalid header: more than "AV_STRINGIFY(BINK_MAX_AUDIO_TRACKS)" audio tracks (%"PRIu32")\n",
  112. bink->num_audio_tracks);
  113. return AVERROR(EIO);
  114. }
  115. if (bink->num_audio_tracks) {
  116. avio_skip(pb, 4 * bink->num_audio_tracks);
  117. for (i = 0; i < bink->num_audio_tracks; i++) {
  118. ast = avformat_new_stream(s, NULL);
  119. if (!ast)
  120. return AVERROR(ENOMEM);
  121. ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  122. ast->codecpar->codec_tag = 0;
  123. ast->codecpar->sample_rate = avio_rl16(pb);
  124. avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
  125. flags = avio_rl16(pb);
  126. ast->codecpar->codec_id = flags & BINK_AUD_USEDCT ?
  127. AV_CODEC_ID_BINKAUDIO_DCT : AV_CODEC_ID_BINKAUDIO_RDFT;
  128. if (flags & BINK_AUD_STEREO) {
  129. ast->codecpar->channels = 2;
  130. ast->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
  131. } else {
  132. ast->codecpar->channels = 1;
  133. ast->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
  134. }
  135. ast->codecpar->extradata = av_mallocz(4 + AV_INPUT_BUFFER_PADDING_SIZE);
  136. if (!ast->codecpar->extradata)
  137. return AVERROR(ENOMEM);
  138. ast->codecpar->extradata_size = 4;
  139. AV_WL32(ast->codecpar->extradata, vst->codecpar->codec_tag);
  140. }
  141. for (i = 0; i < bink->num_audio_tracks; i++)
  142. s->streams[i + 1]->id = avio_rl32(pb);
  143. }
  144. /* frame index table */
  145. next_pos = avio_rl32(pb);
  146. for (i = 0; i < vst->duration; i++) {
  147. pos = next_pos;
  148. if (i == vst->duration - 1) {
  149. next_pos = bink->file_size;
  150. keyframe = 0;
  151. } else {
  152. next_pos = avio_rl32(pb);
  153. keyframe = pos & 1;
  154. }
  155. pos &= ~1;
  156. next_pos &= ~1;
  157. if (next_pos <= pos) {
  158. av_log(s, AV_LOG_ERROR, "invalid frame index table\n");
  159. return AVERROR(EIO);
  160. }
  161. av_add_index_entry(vst, pos, i, next_pos - pos, 0,
  162. keyframe ? AVINDEX_KEYFRAME : 0);
  163. }
  164. avio_skip(pb, 4);
  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(EIO);
  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 (%"PRIu32") > size of packet left (%"PRIu32")\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 calculate 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]->codecpar->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 & AVIO_SEEKABLE_NORMAL))
  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. };