xmv.c 17 KB

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