xmv.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. #define XMV_BLOCK_ALIGN_SIZE 36
  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 CodecID 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_free(xmv->audio);
  102. return 0;
  103. }
  104. static int xmv_read_header(AVFormatContext *s,
  105. AVFormatParameters *ap)
  106. {
  107. XMVDemuxContext *xmv = s->priv_data;
  108. AVIOContext *pb = s->pb;
  109. AVStream *vst = NULL;
  110. uint32_t file_version;
  111. uint32_t this_packet_size;
  112. uint16_t audio_track;
  113. int ret;
  114. avio_skip(pb, 4); /* Next packet size */
  115. this_packet_size = avio_rl32(pb);
  116. avio_skip(pb, 4); /* Max packet size */
  117. avio_skip(pb, 4); /* "xobX" */
  118. file_version = avio_rl32(pb);
  119. if ((file_version != 4) && (file_version != 2))
  120. av_log_ask_for_sample(s, "Found uncommon version %d\n", file_version);
  121. /* Video track */
  122. vst = avformat_new_stream(s, NULL);
  123. if (!vst)
  124. return AVERROR(ENOMEM);
  125. avpriv_set_pts_info(vst, 32, 1, 1000);
  126. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  127. vst->codec->codec_id = CODEC_ID_WMV2;
  128. vst->codec->codec_tag = MKBETAG('W', 'M', 'V', '2');
  129. vst->codec->width = avio_rl32(pb);
  130. vst->codec->height = avio_rl32(pb);
  131. vst->duration = avio_rl32(pb);
  132. xmv->video.stream_index = vst->index;
  133. /* Audio tracks */
  134. xmv->audio_track_count = avio_rl16(pb);
  135. avio_skip(pb, 2); /* Unknown (padding?) */
  136. xmv->audio = av_malloc(xmv->audio_track_count * sizeof(XMVAudioPacket));
  137. if (!xmv->audio) {
  138. ret = AVERROR(ENOMEM);
  139. goto fail;
  140. }
  141. for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) {
  142. XMVAudioPacket *packet = &xmv->audio[audio_track];
  143. AVStream *ast = NULL;
  144. packet->compression = avio_rl16(pb);
  145. packet->channels = avio_rl16(pb);
  146. packet->sample_rate = avio_rl32(pb);
  147. packet->bits_per_sample = avio_rl16(pb);
  148. packet->flags = avio_rl16(pb);
  149. packet->bit_rate = packet->bits_per_sample *
  150. packet->sample_rate *
  151. packet->channels;
  152. packet->block_align = XMV_BLOCK_ALIGN_SIZE * packet->channels;
  153. packet->block_samples = 64;
  154. packet->codec_id = ff_wav_codec_get_id(packet->compression,
  155. packet->bits_per_sample);
  156. packet->stream_index = -1;
  157. packet->frame_size = 0;
  158. packet->block_count = 0;
  159. /* TODO: ADPCM'd 5.1 sound is encoded in three separate streams.
  160. * Those need to be interleaved to a proper 5.1 stream. */
  161. if (packet->flags & XMV_AUDIO_ADPCM51)
  162. av_log(s, AV_LOG_WARNING, "Unsupported 5.1 ADPCM audio stream "
  163. "(0x%04X)\n", packet->flags);
  164. if (!packet->channels || !packet->sample_rate ||
  165. packet->channels >= UINT16_MAX / XMV_BLOCK_ALIGN_SIZE) {
  166. av_log(s, AV_LOG_ERROR, "Invalid parameters for audio track %d.\n",
  167. audio_track);
  168. ret = AVERROR_INVALIDDATA;
  169. goto fail;
  170. }
  171. ast = avformat_new_stream(s, NULL);
  172. if (!ast) {
  173. ret = AVERROR(ENOMEM);
  174. goto fail;
  175. }
  176. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  177. ast->codec->codec_id = packet->codec_id;
  178. ast->codec->codec_tag = packet->compression;
  179. ast->codec->channels = packet->channels;
  180. ast->codec->sample_rate = packet->sample_rate;
  181. ast->codec->bits_per_coded_sample = packet->bits_per_sample;
  182. ast->codec->bit_rate = packet->bit_rate;
  183. ast->codec->block_align = 36 * packet->channels;
  184. avpriv_set_pts_info(ast, 32, packet->block_samples, packet->sample_rate);
  185. packet->stream_index = ast->index;
  186. ast->duration = vst->duration;
  187. }
  188. /* Initialize the packet context */
  189. xmv->next_packet_offset = avio_tell(pb);
  190. xmv->next_packet_size = this_packet_size - xmv->next_packet_offset;
  191. xmv->stream_count = xmv->audio_track_count + 1;
  192. return 0;
  193. fail:
  194. xmv_read_close(s);
  195. return ret;
  196. }
  197. static void xmv_read_extradata(uint8_t *extradata, AVIOContext *pb)
  198. {
  199. /* Read the XMV extradata */
  200. uint32_t data = avio_rl32(pb);
  201. int mspel_bit = !!(data & 0x01);
  202. int loop_filter = !!(data & 0x02);
  203. int abt_flag = !!(data & 0x04);
  204. int j_type_bit = !!(data & 0x08);
  205. int top_left_mv_flag = !!(data & 0x10);
  206. int per_mb_rl_bit = !!(data & 0x20);
  207. int slice_count = (data >> 6) & 7;
  208. /* Write it back as standard WMV2 extradata */
  209. data = 0;
  210. data |= mspel_bit << 15;
  211. data |= loop_filter << 14;
  212. data |= abt_flag << 13;
  213. data |= j_type_bit << 12;
  214. data |= top_left_mv_flag << 11;
  215. data |= per_mb_rl_bit << 10;
  216. data |= slice_count << 7;
  217. AV_WB32(extradata, data);
  218. }
  219. static int xmv_process_packet_header(AVFormatContext *s)
  220. {
  221. XMVDemuxContext *xmv = s->priv_data;
  222. AVIOContext *pb = s->pb;
  223. uint8_t data[8];
  224. uint16_t audio_track;
  225. uint64_t data_offset;
  226. /* Next packet size */
  227. xmv->next_packet_size = avio_rl32(pb);
  228. /* Packet video header */
  229. if (avio_read(pb, data, 8) != 8)
  230. return AVERROR(EIO);
  231. xmv->video.data_size = AV_RL32(data) & 0x007FFFFF;
  232. xmv->video.current_frame = 0;
  233. xmv->video.frame_count = (AV_RL32(data) >> 23) & 0xFF;
  234. xmv->video.has_extradata = (data[3] & 0x80) != 0;
  235. /* Adding the audio data sizes and the video data size keeps you 4 bytes
  236. * short for every audio track. But as playing around with XMV files with
  237. * ADPCM audio showed, taking the extra 4 bytes from the audio data gives
  238. * you either completely distorted audio or click (when skipping the
  239. * remaining 68 bytes of the ADPCM block). Substracting 4 bytes for every
  240. * audio track from the video data works at least for the audio. Probably
  241. * some alignment thing?
  242. * The video data has (always?) lots of padding, so it should work out...
  243. */
  244. xmv->video.data_size -= xmv->audio_track_count * 4;
  245. xmv->current_stream = 0;
  246. if (!xmv->video.frame_count) {
  247. xmv->video.frame_count = 1;
  248. xmv->current_stream = xmv->stream_count > 1;
  249. }
  250. /* Packet audio header */
  251. for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) {
  252. XMVAudioPacket *packet = &xmv->audio[audio_track];
  253. if (avio_read(pb, data, 4) != 4)
  254. return AVERROR(EIO);
  255. packet->data_size = AV_RL32(data) & 0x007FFFFF;
  256. if ((packet->data_size == 0) && (audio_track != 0))
  257. /* This happens when I create an XMV with several identical audio
  258. * streams. From the size calculations, duplicating the previous
  259. * stream's size works out, but the track data itself is silent.
  260. * Maybe this should also redirect the offset to the previous track?
  261. */
  262. packet->data_size = xmv->audio[audio_track - 1].data_size;
  263. /* Carve up the audio data in frame_count slices */
  264. packet->frame_size = packet->data_size / xmv->video.frame_count;
  265. packet->frame_size -= packet->frame_size % packet->block_align;
  266. }
  267. /* Packet data offsets */
  268. data_offset = avio_tell(pb);
  269. xmv->video.data_offset = data_offset;
  270. data_offset += xmv->video.data_size;
  271. for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) {
  272. xmv->audio[audio_track].data_offset = data_offset;
  273. data_offset += xmv->audio[audio_track].data_size;
  274. }
  275. /* Video frames header */
  276. /* Read new video extra data */
  277. if (xmv->video.data_size > 0) {
  278. if (xmv->video.has_extradata) {
  279. xmv_read_extradata(xmv->video.extradata, pb);
  280. xmv->video.data_size -= 4;
  281. xmv->video.data_offset += 4;
  282. if (xmv->video.stream_index >= 0) {
  283. AVStream *vst = s->streams[xmv->video.stream_index];
  284. assert(xmv->video.stream_index < s->nb_streams);
  285. if (vst->codec->extradata_size < 4) {
  286. av_free(vst->codec->extradata);
  287. vst->codec->extradata =
  288. av_malloc(4 + FF_INPUT_BUFFER_PADDING_SIZE);
  289. vst->codec->extradata_size = 4;
  290. }
  291. memcpy(vst->codec->extradata, xmv->video.extradata, 4);
  292. }
  293. }
  294. }
  295. return 0;
  296. }
  297. static int xmv_fetch_new_packet(AVFormatContext *s)
  298. {
  299. XMVDemuxContext *xmv = s->priv_data;
  300. AVIOContext *pb = s->pb;
  301. int result;
  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. };