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.

559 lines
17KB

  1. /*
  2. * Microsoft XMV demuxer
  3. * Copyright (c) 2011 Sven Hesse <drmccoy@drmccoy.de>
  4. * Copyright (c) 2011 Matthew Hoops <clone2727@gmail.com>
  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. * Microsoft XMV demuxer
  25. */
  26. #include <stdint.h>
  27. #include "libavutil/intreadwrite.h"
  28. #include "avformat.h"
  29. #include "internal.h"
  30. #include "riff.h"
  31. /** The min size of an XMV header. */
  32. #define XMV_MIN_HEADER_SIZE 36
  33. /** Audio flag: ADPCM'd 5.1 stream, front left / right channels */
  34. #define XMV_AUDIO_ADPCM51_FRONTLEFTRIGHT 1
  35. /** Audio flag: ADPCM'd 5.1 stream, front center / low frequency channels */
  36. #define XMV_AUDIO_ADPCM51_FRONTCENTERLOW 2
  37. /** Audio flag: ADPCM'd 5.1 stream, rear left / right channels */
  38. #define XMV_AUDIO_ADPCM51_REARLEFTRIGHT 4
  39. /** Audio flag: Any of the ADPCM'd 5.1 stream flags. */
  40. #define XMV_AUDIO_ADPCM51 (XMV_AUDIO_ADPCM51_FRONTLEFTRIGHT | \
  41. XMV_AUDIO_ADPCM51_FRONTCENTERLOW | \
  42. XMV_AUDIO_ADPCM51_REARLEFTRIGHT)
  43. /** A video packet with an XMV file. */
  44. typedef struct XMVVideoPacket {
  45. int stream_index; ///< The decoder stream index for this video packet.
  46. uint32_t data_size; ///< The size of the remaining video data.
  47. uint64_t data_offset; ///< The offset of the video data within the file.
  48. uint32_t current_frame; ///< The current frame within this video packet.
  49. uint32_t frame_count; ///< The amount of frames within this video packet.
  50. int has_extradata; ///< Does the video packet contain extra data?
  51. uint8_t extradata[4]; ///< The extra data
  52. int64_t last_pts; ///< PTS of the last video frame.
  53. int64_t pts; ///< PTS of the most current video frame.
  54. } XMVVideoPacket;
  55. /** An audio packet with an XMV file. */
  56. typedef struct XMVAudioPacket {
  57. int stream_index; ///< The decoder stream index for this audio packet.
  58. /* Stream format properties. */
  59. uint16_t compression; ///< The type of compression.
  60. uint16_t channels; ///< Number of channels.
  61. uint32_t sample_rate; ///< Sampling rate.
  62. uint16_t bits_per_sample; ///< Bits per compressed sample.
  63. uint32_t bit_rate; ///< Bits of compressed data per second.
  64. uint16_t flags; ///< Flags
  65. uint16_t block_align; ///< Bytes per compressed block.
  66. uint16_t block_samples; ///< Decompressed samples per compressed block.
  67. enum CodecID codec_id; ///< The codec ID of the compression scheme.
  68. uint32_t data_size; ///< The size of the remaining audio data.
  69. uint64_t data_offset; ///< The offset of the audio data within the file.
  70. uint32_t frame_size; ///< Number of bytes to put into an audio frame.
  71. uint64_t block_count; ///< Running counter of decompressed audio block.
  72. } XMVAudioPacket;
  73. /** Context for demuxing an XMV file. */
  74. typedef struct XMVDemuxContext {
  75. uint16_t audio_track_count; ///< Number of audio track in this file.
  76. uint32_t this_packet_size; ///< Size of the current packet.
  77. uint32_t next_packet_size; ///< Size of the next packet.
  78. uint64_t this_packet_offset; ///< Offset of the current packet.
  79. uint64_t next_packet_offset; ///< Offset of the next packet.
  80. uint16_t current_stream; ///< The index of the stream currently handling.
  81. uint16_t stream_count; ///< The number of streams in this file.
  82. XMVVideoPacket video; ///< The video packet contained in each packet.
  83. XMVAudioPacket *audio; ///< The audio packets contained in each packet.
  84. } XMVDemuxContext;
  85. static int xmv_probe(AVProbeData *p)
  86. {
  87. uint32_t file_version;
  88. if (p->buf_size < XMV_MIN_HEADER_SIZE)
  89. return 0;
  90. file_version = AV_RL32(p->buf + 16);
  91. if ((file_version == 0) || (file_version > 4))
  92. return 0;
  93. if (!memcmp(p->buf + 12, "xobX", 4))
  94. return AVPROBE_SCORE_MAX;
  95. return 0;
  96. }
  97. static int xmv_read_header(AVFormatContext *s,
  98. AVFormatParameters *ap)
  99. {
  100. XMVDemuxContext *xmv = s->priv_data;
  101. AVIOContext *pb = s->pb;
  102. AVStream *vst = NULL;
  103. uint32_t file_version;
  104. uint32_t this_packet_size;
  105. uint16_t audio_track;
  106. avio_skip(pb, 4); /* Next packet size */
  107. this_packet_size = avio_rl32(pb);
  108. avio_skip(pb, 4); /* Max packet size */
  109. avio_skip(pb, 4); /* "xobX" */
  110. file_version = avio_rl32(pb);
  111. if ((file_version != 4) && (file_version != 2))
  112. av_log_ask_for_sample(s, "Found uncommon version %d\n", file_version);
  113. /* Video track */
  114. vst = avformat_new_stream(s, NULL);
  115. if (!vst)
  116. return AVERROR(ENOMEM);
  117. avpriv_set_pts_info(vst, 32, 1, 1000);
  118. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  119. vst->codec->codec_id = CODEC_ID_WMV2;
  120. vst->codec->codec_tag = MKBETAG('W', 'M', 'V', '2');
  121. vst->codec->width = avio_rl32(pb);
  122. vst->codec->height = avio_rl32(pb);
  123. vst->duration = avio_rl32(pb);
  124. xmv->video.stream_index = vst->index;
  125. /* Audio tracks */
  126. xmv->audio_track_count = avio_rl16(pb);
  127. avio_skip(pb, 2); /* Unknown (padding?) */
  128. xmv->audio = av_malloc(xmv->audio_track_count * sizeof(XMVAudioPacket));
  129. if (!xmv->audio)
  130. return AVERROR(ENOMEM);
  131. for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) {
  132. XMVAudioPacket *packet = &xmv->audio[audio_track];
  133. AVStream *ast = NULL;
  134. packet->compression = avio_rl16(pb);
  135. packet->channels = avio_rl16(pb);
  136. packet->sample_rate = avio_rl32(pb);
  137. packet->bits_per_sample = avio_rl16(pb);
  138. packet->flags = avio_rl16(pb);
  139. packet->bit_rate = packet->bits_per_sample *
  140. packet->sample_rate *
  141. packet->channels;
  142. packet->block_align = 36 * packet->channels;
  143. packet->block_samples = 64;
  144. packet->codec_id = ff_wav_codec_get_id(packet->compression,
  145. packet->bits_per_sample);
  146. packet->stream_index = -1;
  147. packet->frame_size = 0;
  148. packet->block_count = 0;
  149. /* TODO: ADPCM'd 5.1 sound is encoded in three separate streams.
  150. * Those need to be interleaved to a proper 5.1 stream. */
  151. if (packet->flags & XMV_AUDIO_ADPCM51)
  152. av_log(s, AV_LOG_WARNING, "Unsupported 5.1 ADPCM audio stream "
  153. "(0x%04X)\n", packet->flags);
  154. ast = avformat_new_stream(s, NULL);
  155. if (!ast)
  156. return AVERROR(ENOMEM);
  157. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  158. ast->codec->codec_id = packet->codec_id;
  159. ast->codec->codec_tag = packet->compression;
  160. ast->codec->channels = packet->channels;
  161. ast->codec->sample_rate = packet->sample_rate;
  162. ast->codec->bits_per_coded_sample = packet->bits_per_sample;
  163. ast->codec->bit_rate = packet->bit_rate;
  164. ast->codec->block_align = 36 * packet->channels;
  165. avpriv_set_pts_info(ast, 32, packet->block_samples, packet->sample_rate);
  166. packet->stream_index = ast->index;
  167. ast->duration = vst->duration;
  168. }
  169. /* Initialize the packet context */
  170. xmv->next_packet_offset = avio_tell(pb);
  171. xmv->next_packet_size = this_packet_size - xmv->next_packet_offset;
  172. xmv->stream_count = xmv->audio_track_count + 1;
  173. return 0;
  174. }
  175. static void xmv_read_extradata(uint8_t *extradata, AVIOContext *pb)
  176. {
  177. /* Read the XMV extradata */
  178. uint32_t data = avio_rl32(pb);
  179. int mspel_bit = !!(data & 0x01);
  180. int loop_filter = !!(data & 0x02);
  181. int abt_flag = !!(data & 0x04);
  182. int j_type_bit = !!(data & 0x08);
  183. int top_left_mv_flag = !!(data & 0x10);
  184. int per_mb_rl_bit = !!(data & 0x20);
  185. int slice_count = (data >> 6) & 7;
  186. /* Write it back as standard WMV2 extradata */
  187. data = 0;
  188. data |= mspel_bit << 15;
  189. data |= loop_filter << 14;
  190. data |= abt_flag << 13;
  191. data |= j_type_bit << 12;
  192. data |= top_left_mv_flag << 11;
  193. data |= per_mb_rl_bit << 10;
  194. data |= slice_count << 7;
  195. AV_WB32(extradata, data);
  196. }
  197. static int xmv_process_packet_header(AVFormatContext *s)
  198. {
  199. XMVDemuxContext *xmv = s->priv_data;
  200. AVIOContext *pb = s->pb;
  201. uint8_t data[8];
  202. uint16_t audio_track;
  203. uint64_t data_offset;
  204. /* Next packet size */
  205. xmv->next_packet_size = avio_rl32(pb);
  206. /* Packet video header */
  207. if (avio_read(pb, data, 8) != 8)
  208. return AVERROR(EIO);
  209. xmv->video.data_size = AV_RL32(data) & 0x007FFFFF;
  210. xmv->video.current_frame = 0;
  211. xmv->video.frame_count = (AV_RL32(data) >> 23) & 0xFF;
  212. xmv->video.has_extradata = (data[3] & 0x80) != 0;
  213. /* Adding the audio data sizes and the video data size keeps you 4 bytes
  214. * short for every audio track. But as playing around with XMV files with
  215. * ADPCM audio showed, taking the extra 4 bytes from the audio data gives
  216. * you either completely distorted audio or click (when skipping the
  217. * remaining 68 bytes of the ADPCM block). Substracting 4 bytes for every
  218. * audio track from the video data works at least for the audio. Probably
  219. * some alignment thing?
  220. * The video data has (always?) lots of padding, so it should work out...
  221. */
  222. xmv->video.data_size -= xmv->audio_track_count * 4;
  223. xmv->current_stream = 0;
  224. if (!xmv->video.frame_count) {
  225. xmv->video.frame_count = 1;
  226. xmv->current_stream = 1;
  227. }
  228. /* Packet audio header */
  229. for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) {
  230. XMVAudioPacket *packet = &xmv->audio[audio_track];
  231. if (avio_read(pb, data, 4) != 4)
  232. return AVERROR(EIO);
  233. packet->data_size = AV_RL32(data) & 0x007FFFFF;
  234. if ((packet->data_size == 0) && (audio_track != 0))
  235. /* This happens when I create an XMV with several identical audio
  236. * streams. From the size calculations, duplicating the previous
  237. * stream's size works out, but the track data itself is silent.
  238. * Maybe this should also redirect the offset to the previous track?
  239. */
  240. packet->data_size = xmv->audio[audio_track - 1].data_size;
  241. /* Carve up the audio data in frame_count slices */
  242. packet->frame_size = packet->data_size / xmv->video.frame_count;
  243. packet->frame_size -= packet->frame_size % packet->block_align;
  244. }
  245. /* Packet data offsets */
  246. data_offset = avio_tell(pb);
  247. xmv->video.data_offset = data_offset;
  248. data_offset += xmv->video.data_size;
  249. for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) {
  250. xmv->audio[audio_track].data_offset = data_offset;
  251. data_offset += xmv->audio[audio_track].data_size;
  252. }
  253. /* Video frames header */
  254. /* Read new video extra data */
  255. if (xmv->video.data_size > 0) {
  256. if (xmv->video.has_extradata) {
  257. xmv_read_extradata(xmv->video.extradata, pb);
  258. xmv->video.data_size -= 4;
  259. xmv->video.data_offset += 4;
  260. if (xmv->video.stream_index >= 0) {
  261. AVStream *vst = s->streams[xmv->video.stream_index];
  262. assert(xmv->video.stream_index < s->nb_streams);
  263. if (vst->codec->extradata_size < 4) {
  264. av_free(vst->codec->extradata);
  265. vst->codec->extradata =
  266. av_malloc(4 + FF_INPUT_BUFFER_PADDING_SIZE);
  267. vst->codec->extradata_size = 4;
  268. }
  269. memcpy(vst->codec->extradata, xmv->video.extradata, 4);
  270. }
  271. }
  272. }
  273. return 0;
  274. }
  275. static int xmv_fetch_new_packet(AVFormatContext *s)
  276. {
  277. XMVDemuxContext *xmv = s->priv_data;
  278. AVIOContext *pb = s->pb;
  279. int result;
  280. /* Seek to it */
  281. xmv->this_packet_offset = xmv->next_packet_offset;
  282. if (avio_seek(pb, xmv->this_packet_offset, SEEK_SET) != xmv->this_packet_offset)
  283. return AVERROR(EIO);
  284. /* Update the size */
  285. xmv->this_packet_size = xmv->next_packet_size;
  286. if (xmv->this_packet_size < (12 + xmv->audio_track_count * 4))
  287. return AVERROR(EIO);
  288. /* Process the header */
  289. result = xmv_process_packet_header(s);
  290. if (result)
  291. return result;
  292. /* Update the offset */
  293. xmv->next_packet_offset = xmv->this_packet_offset + xmv->this_packet_size;
  294. return 0;
  295. }
  296. static int xmv_fetch_audio_packet(AVFormatContext *s,
  297. AVPacket *pkt, uint32_t stream)
  298. {
  299. XMVDemuxContext *xmv = s->priv_data;
  300. AVIOContext *pb = s->pb;
  301. XMVAudioPacket *audio = &xmv->audio[stream];
  302. uint32_t data_size;
  303. uint32_t block_count;
  304. int result;
  305. /* Seek to it */
  306. if (avio_seek(pb, audio->data_offset, SEEK_SET) != audio->data_offset)
  307. return AVERROR(EIO);
  308. if ((xmv->video.current_frame + 1) < xmv->video.frame_count)
  309. /* Not the last frame, get at most frame_size bytes. */
  310. data_size = FFMIN(audio->frame_size, audio->data_size);
  311. else
  312. /* Last frame, get the rest. */
  313. data_size = audio->data_size;
  314. /* Read the packet */
  315. result = av_get_packet(pb, pkt, data_size);
  316. if (result <= 0)
  317. return result;
  318. pkt->stream_index = audio->stream_index;
  319. /* Calculate the PTS */
  320. block_count = data_size / audio->block_align;
  321. pkt->duration = block_count;
  322. pkt->pts = audio->block_count;
  323. pkt->dts = AV_NOPTS_VALUE;
  324. audio->block_count += block_count;
  325. /* Advance offset */
  326. audio->data_size -= data_size;
  327. audio->data_offset += data_size;
  328. return 0;
  329. }
  330. static int xmv_fetch_video_packet(AVFormatContext *s,
  331. AVPacket *pkt)
  332. {
  333. XMVDemuxContext *xmv = s->priv_data;
  334. AVIOContext *pb = s->pb;
  335. XMVVideoPacket *video = &xmv->video;
  336. int result;
  337. uint32_t frame_header;
  338. uint32_t frame_size, frame_timestamp;
  339. uint8_t *data, *end;
  340. /* Seek to it */
  341. if (avio_seek(pb, video->data_offset, SEEK_SET) != video->data_offset)
  342. return AVERROR(EIO);
  343. /* Read the frame header */
  344. frame_header = avio_rl32(pb);
  345. frame_size = (frame_header & 0x1FFFF) * 4 + 4;
  346. frame_timestamp = (frame_header >> 17);
  347. if ((frame_size + 4) > video->data_size)
  348. return AVERROR(EIO);
  349. /* Get the packet data */
  350. result = av_get_packet(pb, pkt, frame_size);
  351. if (result != frame_size)
  352. return result;
  353. /* Contrary to normal WMV2 video, the bit stream in XMV's
  354. * WMV2 is little-endian.
  355. * TODO: This manual swap is of course suboptimal.
  356. */
  357. for (data = pkt->data, end = pkt->data + frame_size; data < end; data += 4)
  358. AV_WB32(data, AV_RL32(data));
  359. pkt->stream_index = video->stream_index;
  360. /* Calculate the PTS */
  361. video->last_pts = frame_timestamp + video->pts;
  362. pkt->duration = 0;
  363. pkt->pts = video->last_pts;
  364. pkt->dts = AV_NOPTS_VALUE;
  365. video->pts += frame_timestamp;
  366. /* Keyframe? */
  367. pkt->flags = (pkt->data[0] & 0x80) ? 0 : AV_PKT_FLAG_KEY;
  368. /* Advance offset */
  369. video->data_size -= frame_size + 4;
  370. video->data_offset += frame_size + 4;
  371. return 0;
  372. }
  373. static int xmv_read_packet(AVFormatContext *s,
  374. AVPacket *pkt)
  375. {
  376. XMVDemuxContext *xmv = s->priv_data;
  377. int result;
  378. if (xmv->video.current_frame == xmv->video.frame_count) {
  379. /* No frames left in this packet, so we fetch a new one */
  380. result = xmv_fetch_new_packet(s);
  381. if (result)
  382. return result;
  383. }
  384. if (xmv->current_stream == 0) {
  385. /* Fetch a video frame */
  386. result = xmv_fetch_video_packet(s, pkt);
  387. if (result)
  388. return result;
  389. } else {
  390. /* Fetch an audio frame */
  391. result = xmv_fetch_audio_packet(s, pkt, xmv->current_stream - 1);
  392. if (result)
  393. return result;
  394. }
  395. /* Increase our counters */
  396. if (++xmv->current_stream >= xmv->stream_count) {
  397. xmv->current_stream = 0;
  398. xmv->video.current_frame += 1;
  399. }
  400. return 0;
  401. }
  402. static int xmv_read_close(AVFormatContext *s)
  403. {
  404. XMVDemuxContext *xmv = s->priv_data;
  405. av_free(xmv->audio);
  406. return 0;
  407. }
  408. AVInputFormat ff_xmv_demuxer = {
  409. .name = "xmv",
  410. .long_name = NULL_IF_CONFIG_SMALL("Microsoft XMV"),
  411. .priv_data_size = sizeof(XMVDemuxContext),
  412. .read_probe = xmv_probe,
  413. .read_header = xmv_read_header,
  414. .read_packet = xmv_read_packet,
  415. .read_close = xmv_read_close,
  416. };