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.

584 lines
16KB

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