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.

578 lines
18KB

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