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.

336 lines
11KB

  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 <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. #define SMUSH_BLOCK_SIZE 512
  44. typedef struct BinkDemuxContext {
  45. uint32_t file_size;
  46. uint32_t num_audio_tracks;
  47. int current_track; ///< audio track to return in next packet
  48. int64_t video_pts;
  49. int64_t audio_pts[BINK_MAX_AUDIO_TRACKS];
  50. uint32_t remain_packet_size;
  51. int flags;
  52. int smush_size;
  53. } BinkDemuxContext;
  54. static int probe(const AVProbeData *p)
  55. {
  56. const uint8_t *b = p->buf;
  57. int smush = AV_RN32(p->buf) == AV_RN32("SMUS");
  58. do {
  59. if (((b[0] == 'B' && b[1] == 'I' && b[2] == 'K' && /* Bink 1 */
  60. (b[3] == 'b' || b[3] == 'f' || b[3] == 'g' || b[3] == 'h' || b[3] == 'i' ||
  61. b[3] == 'k')) ||
  62. (b[0] == 'K' && b[1] == 'B' && b[2] == '2' && /* Bink 2 */
  63. (b[3] == 'a' || b[3] == 'd' || b[3] == 'f' || b[3] == 'g' || b[3] == 'h' ||
  64. b[3] == 'i' || b[3] == 'j' || b[3] == 'k'))) &&
  65. AV_RL32(b+8) > 0 && // num_frames
  66. AV_RL32(b+20) > 0 && AV_RL32(b+20) <= BINK_MAX_WIDTH &&
  67. AV_RL32(b+24) > 0 && AV_RL32(b+24) <= BINK_MAX_HEIGHT &&
  68. AV_RL32(b+28) > 0 && AV_RL32(b+32) > 0) // fps num,den
  69. return AVPROBE_SCORE_MAX;
  70. b += SMUSH_BLOCK_SIZE;
  71. } while (smush && b < p->buf + p->buf_size - 32);
  72. return 0;
  73. }
  74. static int read_header(AVFormatContext *s)
  75. {
  76. BinkDemuxContext *bink = s->priv_data;
  77. AVIOContext *pb = s->pb;
  78. uint32_t fps_num, fps_den;
  79. AVStream *vst, *ast;
  80. unsigned int i;
  81. uint32_t pos, next_pos;
  82. uint16_t flags;
  83. int next_keyframe = 1;
  84. int keyframe;
  85. int ret;
  86. uint32_t signature;
  87. uint8_t revision;
  88. vst = avformat_new_stream(s, NULL);
  89. if (!vst)
  90. return AVERROR(ENOMEM);
  91. vst->codecpar->codec_tag = avio_rl32(pb);
  92. if (vst->codecpar->codec_tag == AV_RL32("SMUS")) {
  93. do {
  94. bink->smush_size += SMUSH_BLOCK_SIZE;
  95. avio_skip(pb, SMUSH_BLOCK_SIZE - 4);
  96. vst->codecpar->codec_tag = avio_rl32(pb);
  97. } while (!avio_feof(pb) && (vst->codecpar->codec_tag & 0xFFFFFF) != AV_RL32("BIK"));
  98. if (avio_feof(pb)) {
  99. av_log(s, AV_LOG_ERROR, "invalid SMUSH header: BIK not found\n");
  100. return AVERROR_INVALIDDATA;
  101. }
  102. }
  103. bink->file_size = avio_rl32(pb) + 8;
  104. vst->duration = avio_rl32(pb);
  105. if (vst->duration > 1000000) {
  106. av_log(s, AV_LOG_ERROR, "invalid header: more than 1000000 frames\n");
  107. return AVERROR(EIO);
  108. }
  109. if (avio_rl32(pb) > bink->file_size) {
  110. av_log(s, AV_LOG_ERROR,
  111. "invalid header: largest frame size greater than file size\n");
  112. return AVERROR(EIO);
  113. }
  114. avio_skip(pb, 4);
  115. vst->codecpar->width = avio_rl32(pb);
  116. vst->codecpar->height = avio_rl32(pb);
  117. fps_num = avio_rl32(pb);
  118. fps_den = avio_rl32(pb);
  119. if (fps_num == 0 || fps_den == 0) {
  120. av_log(s, AV_LOG_ERROR,
  121. "invalid header: invalid fps (%"PRIu32"/%"PRIu32")\n",
  122. fps_num, fps_den);
  123. return AVERROR(EIO);
  124. }
  125. avpriv_set_pts_info(vst, 64, fps_den, fps_num);
  126. vst->avg_frame_rate = av_inv_q(vst->time_base);
  127. vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  128. vst->codecpar->codec_id = AV_CODEC_ID_BINKVIDEO;
  129. if ((vst->codecpar->codec_tag & 0xFFFFFF) == MKTAG('K', 'B', '2', 0)) {
  130. av_log(s, AV_LOG_WARNING, "Bink 2 video is not implemented\n");
  131. vst->codecpar->codec_id = AV_CODEC_ID_NONE;
  132. }
  133. if ((ret = ff_get_extradata(s, vst->codecpar, pb, 4)) < 0)
  134. return ret;
  135. bink->num_audio_tracks = avio_rl32(pb);
  136. if (bink->num_audio_tracks > BINK_MAX_AUDIO_TRACKS) {
  137. av_log(s, AV_LOG_ERROR,
  138. "invalid header: more than "AV_STRINGIFY(BINK_MAX_AUDIO_TRACKS)" audio tracks (%"PRIu32")\n",
  139. bink->num_audio_tracks);
  140. return AVERROR(EIO);
  141. }
  142. signature = (vst->codecpar->codec_tag & 0xFFFFFF);
  143. revision = ((vst->codecpar->codec_tag >> 24) % 0xFF);
  144. if ((signature == AV_RL32("BIK") && (revision == 'k')) ||
  145. (signature == AV_RL32("KB2") && (revision == 'i' || revision == 'j' || revision == 'k')))
  146. avio_skip(pb, 4); /* unknown new field */
  147. if (bink->num_audio_tracks) {
  148. avio_skip(pb, 4 * bink->num_audio_tracks); /* max decoded size */
  149. for (i = 0; i < bink->num_audio_tracks; i++) {
  150. ast = avformat_new_stream(s, NULL);
  151. if (!ast)
  152. return AVERROR(ENOMEM);
  153. ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  154. ast->codecpar->codec_tag = 0;
  155. ast->codecpar->sample_rate = avio_rl16(pb);
  156. avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
  157. flags = avio_rl16(pb);
  158. ast->codecpar->codec_id = flags & BINK_AUD_USEDCT ?
  159. AV_CODEC_ID_BINKAUDIO_DCT : AV_CODEC_ID_BINKAUDIO_RDFT;
  160. if (flags & BINK_AUD_STEREO) {
  161. ast->codecpar->channels = 2;
  162. ast->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
  163. } else {
  164. ast->codecpar->channels = 1;
  165. ast->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
  166. }
  167. if ((ret = ff_alloc_extradata(ast->codecpar, 4)) < 0)
  168. return ret;
  169. AV_WL32(ast->codecpar->extradata, vst->codecpar->codec_tag);
  170. }
  171. for (i = 0; i < bink->num_audio_tracks; i++)
  172. s->streams[i + 1]->id = avio_rl32(pb);
  173. }
  174. /* frame index table */
  175. next_pos = avio_rl32(pb);
  176. for (i = 0; i < vst->duration; i++) {
  177. pos = next_pos;
  178. keyframe = next_keyframe;
  179. if (i == vst->duration - 1) {
  180. next_pos = bink->file_size;
  181. next_keyframe = 0;
  182. } else {
  183. next_pos = avio_rl32(pb);
  184. next_keyframe = next_pos & 1;
  185. }
  186. pos &= ~1;
  187. next_pos &= ~1;
  188. if (next_pos <= pos) {
  189. av_log(s, AV_LOG_ERROR, "invalid frame index table\n");
  190. return AVERROR(EIO);
  191. }
  192. if ((ret = av_add_index_entry(vst, pos, i, next_pos - pos, 0,
  193. keyframe ? AVINDEX_KEYFRAME : 0)) < 0)
  194. return ret;
  195. }
  196. if (vst->index_entries)
  197. avio_seek(pb, vst->index_entries[0].pos + bink->smush_size, SEEK_SET);
  198. else
  199. avio_skip(pb, 4);
  200. bink->current_track = -1;
  201. return 0;
  202. }
  203. static int read_packet(AVFormatContext *s, AVPacket *pkt)
  204. {
  205. BinkDemuxContext *bink = s->priv_data;
  206. AVIOContext *pb = s->pb;
  207. int ret;
  208. if (bink->current_track < 0) {
  209. int index_entry;
  210. AVStream *st = s->streams[0]; // stream 0 is video stream with index
  211. if (bink->video_pts >= st->duration)
  212. return AVERROR_EOF;
  213. index_entry = av_index_search_timestamp(st, bink->video_pts,
  214. AVSEEK_FLAG_ANY);
  215. if (index_entry < 0) {
  216. av_log(s, AV_LOG_ERROR,
  217. "could not find index entry for frame %"PRId64"\n",
  218. bink->video_pts);
  219. return AVERROR(EIO);
  220. }
  221. bink->remain_packet_size = st->index_entries[index_entry].size;
  222. bink->flags = st->index_entries[index_entry].flags;
  223. bink->current_track = 0;
  224. }
  225. while (bink->current_track < bink->num_audio_tracks) {
  226. uint32_t audio_size = avio_rl32(pb);
  227. if (audio_size > bink->remain_packet_size - 4) {
  228. av_log(s, AV_LOG_ERROR,
  229. "frame %"PRId64": audio size in header (%"PRIu32") > size of packet left (%"PRIu32")\n",
  230. bink->video_pts, audio_size, bink->remain_packet_size);
  231. return AVERROR(EIO);
  232. }
  233. bink->remain_packet_size -= 4 + audio_size;
  234. bink->current_track++;
  235. if (audio_size >= 4) {
  236. /* get one audio packet per track */
  237. if ((ret = av_get_packet(pb, pkt, audio_size)) < 0)
  238. return ret;
  239. pkt->stream_index = bink->current_track;
  240. pkt->pts = bink->audio_pts[bink->current_track - 1];
  241. /* Each audio packet reports the number of decompressed samples
  242. (in bytes). We use this value to calculate the audio PTS */
  243. if (pkt->size >= 4)
  244. bink->audio_pts[bink->current_track -1] +=
  245. AV_RL32(pkt->data) / (2 * s->streams[bink->current_track]->codecpar->channels);
  246. return 0;
  247. } else {
  248. avio_skip(pb, audio_size);
  249. }
  250. }
  251. /* get video packet */
  252. if ((ret = av_get_packet(pb, pkt, bink->remain_packet_size)) < 0)
  253. return ret;
  254. pkt->stream_index = 0;
  255. pkt->pts = bink->video_pts++;
  256. if (bink->flags & AVINDEX_KEYFRAME)
  257. pkt->flags |= AV_PKT_FLAG_KEY;
  258. /* -1 instructs the next call to read_packet() to read the next frame */
  259. bink->current_track = -1;
  260. return 0;
  261. }
  262. static int read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  263. {
  264. BinkDemuxContext *bink = s->priv_data;
  265. AVStream *vst = s->streams[0];
  266. int64_t ret;
  267. if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL))
  268. return -1;
  269. /* seek to the first frame */
  270. ret = avio_seek(s->pb, vst->index_entries[0].pos + bink->smush_size, SEEK_SET);
  271. if (ret < 0)
  272. return ret;
  273. bink->video_pts = 0;
  274. memset(bink->audio_pts, 0, sizeof(bink->audio_pts));
  275. bink->current_track = -1;
  276. return 0;
  277. }
  278. AVInputFormat ff_bink_demuxer = {
  279. .name = "bink",
  280. .long_name = NULL_IF_CONFIG_SMALL("Bink"),
  281. .priv_data_size = sizeof(BinkDemuxContext),
  282. .read_probe = probe,
  283. .read_header = read_header,
  284. .read_packet = read_packet,
  285. .read_seek = read_seek,
  286. .flags = AVFMT_SHOW_IDS,
  287. };