xmv.c 17 KB

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