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.

563 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. {
  99. XMVDemuxContext *xmv = s->priv_data;
  100. AVIOContext *pb = s->pb;
  101. AVStream *vst = NULL;
  102. uint32_t file_version;
  103. uint32_t this_packet_size;
  104. uint16_t audio_track;
  105. avio_skip(pb, 4); /* Next packet size */
  106. this_packet_size = avio_rl32(pb);
  107. avio_skip(pb, 4); /* Max packet size */
  108. avio_skip(pb, 4); /* "xobX" */
  109. file_version = avio_rl32(pb);
  110. if ((file_version != 4) && (file_version != 2))
  111. av_log_ask_for_sample(s, "Found uncommon version %d\n", file_version);
  112. /* Video track */
  113. vst = avformat_new_stream(s, NULL);
  114. if (!vst)
  115. return AVERROR(ENOMEM);
  116. avpriv_set_pts_info(vst, 32, 1, 1000);
  117. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  118. vst->codec->codec_id = CODEC_ID_WMV2;
  119. vst->codec->codec_tag = MKBETAG('W', 'M', 'V', '2');
  120. vst->codec->width = avio_rl32(pb);
  121. vst->codec->height = avio_rl32(pb);
  122. vst->duration = avio_rl32(pb);
  123. xmv->video.stream_index = vst->index;
  124. /* Audio tracks */
  125. xmv->audio_track_count = avio_rl16(pb);
  126. avio_skip(pb, 2); /* Unknown (padding?) */
  127. xmv->audio = av_malloc(xmv->audio_track_count * sizeof(XMVAudioPacket));
  128. if (!xmv->audio)
  129. return AVERROR(ENOMEM);
  130. for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) {
  131. XMVAudioPacket *packet = &xmv->audio[audio_track];
  132. AVStream *ast = NULL;
  133. packet->compression = avio_rl16(pb);
  134. packet->channels = avio_rl16(pb);
  135. packet->sample_rate = avio_rl32(pb);
  136. packet->bits_per_sample = avio_rl16(pb);
  137. packet->flags = avio_rl16(pb);
  138. if (!packet->channels) {
  139. av_log(s, AV_LOG_ERROR, "0 channels\n");
  140. return AVERROR(EINVAL);
  141. }
  142. packet->bit_rate = packet->bits_per_sample *
  143. packet->sample_rate *
  144. packet->channels;
  145. packet->block_align = 36 * packet->channels;
  146. packet->block_samples = 64;
  147. packet->codec_id = ff_wav_codec_get_id(packet->compression,
  148. packet->bits_per_sample);
  149. packet->stream_index = -1;
  150. packet->frame_size = 0;
  151. packet->block_count = 0;
  152. /* TODO: ADPCM'd 5.1 sound is encoded in three separate streams.
  153. * Those need to be interleaved to a proper 5.1 stream. */
  154. if (packet->flags & XMV_AUDIO_ADPCM51)
  155. av_log(s, AV_LOG_WARNING, "Unsupported 5.1 ADPCM audio stream "
  156. "(0x%04X)\n", packet->flags);
  157. ast = avformat_new_stream(s, NULL);
  158. if (!ast)
  159. return AVERROR(ENOMEM);
  160. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  161. ast->codec->codec_id = packet->codec_id;
  162. ast->codec->codec_tag = packet->compression;
  163. ast->codec->channels = packet->channels;
  164. ast->codec->sample_rate = packet->sample_rate;
  165. ast->codec->bits_per_coded_sample = packet->bits_per_sample;
  166. ast->codec->bit_rate = packet->bit_rate;
  167. ast->codec->block_align = 36 * packet->channels;
  168. avpriv_set_pts_info(ast, 32, packet->block_samples, packet->sample_rate);
  169. packet->stream_index = ast->index;
  170. ast->duration = vst->duration;
  171. }
  172. /* Initialize the packet context */
  173. xmv->next_packet_offset = avio_tell(pb);
  174. xmv->next_packet_size = this_packet_size - xmv->next_packet_offset;
  175. xmv->stream_count = xmv->audio_track_count + 1;
  176. return 0;
  177. }
  178. static void xmv_read_extradata(uint8_t *extradata, AVIOContext *pb)
  179. {
  180. /* Read the XMV extradata */
  181. uint32_t data = avio_rl32(pb);
  182. int mspel_bit = !!(data & 0x01);
  183. int loop_filter = !!(data & 0x02);
  184. int abt_flag = !!(data & 0x04);
  185. int j_type_bit = !!(data & 0x08);
  186. int top_left_mv_flag = !!(data & 0x10);
  187. int per_mb_rl_bit = !!(data & 0x20);
  188. int slice_count = (data >> 6) & 7;
  189. /* Write it back as standard WMV2 extradata */
  190. data = 0;
  191. data |= mspel_bit << 15;
  192. data |= loop_filter << 14;
  193. data |= abt_flag << 13;
  194. data |= j_type_bit << 12;
  195. data |= top_left_mv_flag << 11;
  196. data |= per_mb_rl_bit << 10;
  197. data |= slice_count << 7;
  198. AV_WB32(extradata, data);
  199. }
  200. static int xmv_process_packet_header(AVFormatContext *s)
  201. {
  202. XMVDemuxContext *xmv = s->priv_data;
  203. AVIOContext *pb = s->pb;
  204. uint8_t data[8];
  205. uint16_t audio_track;
  206. uint64_t data_offset;
  207. /* Next packet size */
  208. xmv->next_packet_size = avio_rl32(pb);
  209. /* Packet video header */
  210. if (avio_read(pb, data, 8) != 8)
  211. return AVERROR(EIO);
  212. xmv->video.data_size = AV_RL32(data) & 0x007FFFFF;
  213. xmv->video.current_frame = 0;
  214. xmv->video.frame_count = (AV_RL32(data) >> 23) & 0xFF;
  215. xmv->video.has_extradata = (data[3] & 0x80) != 0;
  216. /* Adding the audio data sizes and the video data size keeps you 4 bytes
  217. * short for every audio track. But as playing around with XMV files with
  218. * ADPCM audio showed, taking the extra 4 bytes from the audio data gives
  219. * you either completely distorted audio or click (when skipping the
  220. * remaining 68 bytes of the ADPCM block). Substracting 4 bytes for every
  221. * audio track from the video data works at least for the audio. Probably
  222. * some alignment thing?
  223. * The video data has (always?) lots of padding, so it should work out...
  224. */
  225. xmv->video.data_size -= xmv->audio_track_count * 4;
  226. xmv->current_stream = 0;
  227. if (!xmv->video.frame_count) {
  228. xmv->video.frame_count = 1;
  229. xmv->current_stream = xmv->stream_count > 1;
  230. }
  231. /* Packet audio header */
  232. for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) {
  233. XMVAudioPacket *packet = &xmv->audio[audio_track];
  234. if (avio_read(pb, data, 4) != 4)
  235. return AVERROR(EIO);
  236. packet->data_size = AV_RL32(data) & 0x007FFFFF;
  237. if ((packet->data_size == 0) && (audio_track != 0))
  238. /* This happens when I create an XMV with several identical audio
  239. * streams. From the size calculations, duplicating the previous
  240. * stream's size works out, but the track data itself is silent.
  241. * Maybe this should also redirect the offset to the previous track?
  242. */
  243. packet->data_size = xmv->audio[audio_track - 1].data_size;
  244. /* Carve up the audio data in frame_count slices */
  245. packet->frame_size = packet->data_size / xmv->video.frame_count;
  246. packet->frame_size -= packet->frame_size % packet->block_align;
  247. }
  248. /* Packet data offsets */
  249. data_offset = avio_tell(pb);
  250. xmv->video.data_offset = data_offset;
  251. data_offset += xmv->video.data_size;
  252. for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) {
  253. xmv->audio[audio_track].data_offset = data_offset;
  254. data_offset += xmv->audio[audio_track].data_size;
  255. }
  256. /* Video frames header */
  257. /* Read new video extra data */
  258. if (xmv->video.data_size > 0) {
  259. if (xmv->video.has_extradata) {
  260. xmv_read_extradata(xmv->video.extradata, pb);
  261. xmv->video.data_size -= 4;
  262. xmv->video.data_offset += 4;
  263. if (xmv->video.stream_index >= 0) {
  264. AVStream *vst = s->streams[xmv->video.stream_index];
  265. assert(xmv->video.stream_index < s->nb_streams);
  266. if (vst->codec->extradata_size < 4) {
  267. av_free(vst->codec->extradata);
  268. vst->codec->extradata =
  269. av_malloc(4 + FF_INPUT_BUFFER_PADDING_SIZE);
  270. vst->codec->extradata_size = 4;
  271. }
  272. memcpy(vst->codec->extradata, xmv->video.extradata, 4);
  273. }
  274. }
  275. }
  276. return 0;
  277. }
  278. static int xmv_fetch_new_packet(AVFormatContext *s)
  279. {
  280. XMVDemuxContext *xmv = s->priv_data;
  281. AVIOContext *pb = s->pb;
  282. int result;
  283. /* Seek to it */
  284. xmv->this_packet_offset = xmv->next_packet_offset;
  285. if (avio_seek(pb, xmv->this_packet_offset, SEEK_SET) != xmv->this_packet_offset)
  286. return AVERROR(EIO);
  287. /* Update the size */
  288. xmv->this_packet_size = xmv->next_packet_size;
  289. if (xmv->this_packet_size < (12 + xmv->audio_track_count * 4))
  290. return AVERROR(EIO);
  291. /* Process the header */
  292. result = xmv_process_packet_header(s);
  293. if (result)
  294. return result;
  295. /* Update the offset */
  296. xmv->next_packet_offset = xmv->this_packet_offset + xmv->this_packet_size;
  297. return 0;
  298. }
  299. static int xmv_fetch_audio_packet(AVFormatContext *s,
  300. AVPacket *pkt, uint32_t stream)
  301. {
  302. XMVDemuxContext *xmv = s->priv_data;
  303. AVIOContext *pb = s->pb;
  304. XMVAudioPacket *audio = &xmv->audio[stream];
  305. uint32_t data_size;
  306. uint32_t block_count;
  307. int result;
  308. /* Seek to it */
  309. if (avio_seek(pb, audio->data_offset, SEEK_SET) != audio->data_offset)
  310. return AVERROR(EIO);
  311. if ((xmv->video.current_frame + 1) < xmv->video.frame_count)
  312. /* Not the last frame, get at most frame_size bytes. */
  313. data_size = FFMIN(audio->frame_size, audio->data_size);
  314. else
  315. /* Last frame, get the rest. */
  316. data_size = audio->data_size;
  317. /* Read the packet */
  318. result = av_get_packet(pb, pkt, data_size);
  319. if (result <= 0)
  320. return result;
  321. pkt->stream_index = audio->stream_index;
  322. /* Calculate the PTS */
  323. block_count = data_size / audio->block_align;
  324. pkt->duration = block_count;
  325. pkt->pts = audio->block_count;
  326. pkt->dts = AV_NOPTS_VALUE;
  327. audio->block_count += block_count;
  328. /* Advance offset */
  329. audio->data_size -= data_size;
  330. audio->data_offset += data_size;
  331. return 0;
  332. }
  333. static int xmv_fetch_video_packet(AVFormatContext *s,
  334. AVPacket *pkt)
  335. {
  336. XMVDemuxContext *xmv = s->priv_data;
  337. AVIOContext *pb = s->pb;
  338. XMVVideoPacket *video = &xmv->video;
  339. int result;
  340. uint32_t frame_header;
  341. uint32_t frame_size, frame_timestamp;
  342. uint8_t *data, *end;
  343. /* Seek to it */
  344. if (avio_seek(pb, video->data_offset, SEEK_SET) != video->data_offset)
  345. return AVERROR(EIO);
  346. /* Read the frame header */
  347. frame_header = avio_rl32(pb);
  348. frame_size = (frame_header & 0x1FFFF) * 4 + 4;
  349. frame_timestamp = (frame_header >> 17);
  350. if ((frame_size + 4) > video->data_size)
  351. return AVERROR(EIO);
  352. /* Get the packet data */
  353. result = av_get_packet(pb, pkt, frame_size);
  354. if (result != frame_size)
  355. return result;
  356. /* Contrary to normal WMV2 video, the bit stream in XMV's
  357. * WMV2 is little-endian.
  358. * TODO: This manual swap is of course suboptimal.
  359. */
  360. for (data = pkt->data, end = pkt->data + frame_size; data < end; data += 4)
  361. AV_WB32(data, AV_RL32(data));
  362. pkt->stream_index = video->stream_index;
  363. /* Calculate the PTS */
  364. video->last_pts = frame_timestamp + video->pts;
  365. pkt->duration = 0;
  366. pkt->pts = video->last_pts;
  367. pkt->dts = AV_NOPTS_VALUE;
  368. video->pts += frame_timestamp;
  369. /* Keyframe? */
  370. pkt->flags = (pkt->data[0] & 0x80) ? 0 : AV_PKT_FLAG_KEY;
  371. /* Advance offset */
  372. video->data_size -= frame_size + 4;
  373. video->data_offset += frame_size + 4;
  374. return 0;
  375. }
  376. static int xmv_read_packet(AVFormatContext *s,
  377. AVPacket *pkt)
  378. {
  379. XMVDemuxContext *xmv = s->priv_data;
  380. int result;
  381. if (xmv->video.current_frame == xmv->video.frame_count) {
  382. /* No frames left in this packet, so we fetch a new one */
  383. result = xmv_fetch_new_packet(s);
  384. if (result)
  385. return result;
  386. }
  387. if (xmv->current_stream == 0) {
  388. /* Fetch a video frame */
  389. result = xmv_fetch_video_packet(s, pkt);
  390. if (result)
  391. return result;
  392. } else {
  393. /* Fetch an audio frame */
  394. result = xmv_fetch_audio_packet(s, pkt, xmv->current_stream - 1);
  395. if (result)
  396. return result;
  397. }
  398. /* Increase our counters */
  399. if (++xmv->current_stream >= xmv->stream_count) {
  400. xmv->current_stream = 0;
  401. xmv->video.current_frame += 1;
  402. }
  403. return 0;
  404. }
  405. static int xmv_read_close(AVFormatContext *s)
  406. {
  407. XMVDemuxContext *xmv = s->priv_data;
  408. av_freep(&xmv->audio);
  409. return 0;
  410. }
  411. AVInputFormat ff_xmv_demuxer = {
  412. .name = "xmv",
  413. .long_name = NULL_IF_CONFIG_SMALL("Microsoft XMV"),
  414. .priv_data_size = sizeof(XMVDemuxContext),
  415. .read_probe = xmv_probe,
  416. .read_header = xmv_read_header,
  417. .read_packet = xmv_read_packet,
  418. .read_close = xmv_read_close,
  419. };