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.

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