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.

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