Signed-off-by: Diego Biurrun <diego@biurrun.de>
Sven Hesse authored on 2011/08/17 04:26:45... | ... |
@@ -259,6 +259,8 @@ library: |
259 | 259 |
@tab Multimedia format used in Westwood Studios games. |
260 | 260 |
@item Westwood Studios VQA @tab @tab X |
261 | 261 |
@tab Multimedia format used in Westwood Studios games. |
262 |
+@item XMV @tab @tab X |
|
263 |
+ @tab Microsoft video container used in Xbox games. |
|
262 | 264 |
@item xWMA @tab @tab X |
263 | 265 |
@tab Microsoft audio container used by XAudio 2. |
264 | 266 |
@item YUV4MPEG pipe @tab X @tab X |
... | ... |
@@ -298,6 +298,7 @@ OBJS-$(CONFIG_WTV_DEMUXER) += wtv.o asfdec.o asf.o asfcrypt.o \ |
298 | 298 |
avlanguage.o mpegts.o isom.o riff.o |
299 | 299 |
OBJS-$(CONFIG_WV_DEMUXER) += wv.o apetag.o |
300 | 300 |
OBJS-$(CONFIG_XA_DEMUXER) += xa.o |
301 |
+OBJS-$(CONFIG_XMV_DEMUXER) += xmv.o |
|
301 | 302 |
OBJS-$(CONFIG_XWMA_DEMUXER) += xwma.o riff.o |
302 | 303 |
OBJS-$(CONFIG_YOP_DEMUXER) += yop.o |
303 | 304 |
OBJS-$(CONFIG_YUV4MPEGPIPE_MUXER) += yuv4mpeg.o |
... | ... |
@@ -225,6 +225,7 @@ void av_register_all(void) |
225 | 225 |
REGISTER_DEMUXER (WTV, wtv); |
226 | 226 |
REGISTER_DEMUXER (WV, wv); |
227 | 227 |
REGISTER_DEMUXER (XA, xa); |
228 |
+ REGISTER_DEMUXER (XMV, xmv); |
|
228 | 229 |
REGISTER_DEMUXER (XWMA, xwma); |
229 | 230 |
REGISTER_DEMUXER (YOP, yop); |
230 | 231 |
REGISTER_MUXDEMUX (YUV4MPEGPIPE, yuv4mpegpipe); |
... | ... |
@@ -24,7 +24,7 @@ |
24 | 24 |
#include "libavutil/avutil.h" |
25 | 25 |
|
26 | 26 |
#define LIBAVFORMAT_VERSION_MAJOR 53 |
27 |
-#define LIBAVFORMAT_VERSION_MINOR 4 |
|
27 |
+#define LIBAVFORMAT_VERSION_MINOR 5 |
|
28 | 28 |
#define LIBAVFORMAT_VERSION_MICRO 0 |
29 | 29 |
|
30 | 30 |
#define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ |
31 | 31 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,576 @@ |
0 |
+/* |
|
1 |
+ * Microsoft XMV demuxer |
|
2 |
+ * Copyright (c) 2011 Sven Hesse <drmccoy@drmccoy.de> |
|
3 |
+ * Copyright (c) 2011 Matthew Hoops <clone2727@gmail.com> |
|
4 |
+ * |
|
5 |
+ * This file is part of Libav. |
|
6 |
+ * |
|
7 |
+ * Libav is free software; you can redistribute it and/or |
|
8 |
+ * modify it under the terms of the GNU Lesser General Public |
|
9 |
+ * License as published by the Free Software Foundation; either |
|
10 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
11 |
+ * |
|
12 |
+ * Libav is distributed in the hope that it will be useful, |
|
13 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
15 |
+ * Lesser General Public License for more details. |
|
16 |
+ * |
|
17 |
+ * You should have received a copy of the GNU Lesser General Public |
|
18 |
+ * License along with Libav; if not, write to the Free Software |
|
19 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
20 |
+ */ |
|
21 |
+ |
|
22 |
+/** |
|
23 |
+ * @file |
|
24 |
+ * Microsoft XMV demuxer |
|
25 |
+ */ |
|
26 |
+ |
|
27 |
+#include <stdint.h> |
|
28 |
+ |
|
29 |
+#include "libavutil/intreadwrite.h" |
|
30 |
+ |
|
31 |
+#include "avformat.h" |
|
32 |
+#include "riff.h" |
|
33 |
+ |
|
34 |
+#define XMV_MIN_HEADER_SIZE 36 |
|
35 |
+ |
|
36 |
+#define XMV_AUDIO_ADPCM51_FRONTLEFTRIGHT 1 |
|
37 |
+#define XMV_AUDIO_ADPCM51_FRONTCENTERLOW 2 |
|
38 |
+#define XMV_AUDIO_ADPCM51_REARLEFTRIGHT 4 |
|
39 |
+ |
|
40 |
+#define XMV_AUDIO_ADPCM51 (XMV_AUDIO_ADPCM51_FRONTLEFTRIGHT | \ |
|
41 |
+ XMV_AUDIO_ADPCM51_FRONTCENTERLOW | \ |
|
42 |
+ XMV_AUDIO_ADPCM51_REARLEFTRIGHT) |
|
43 |
+ |
|
44 |
+typedef struct XMVAudioTrack { |
|
45 |
+ uint16_t compression; |
|
46 |
+ uint16_t channels; |
|
47 |
+ uint32_t sample_rate; |
|
48 |
+ uint16_t bits_per_sample; |
|
49 |
+ uint32_t bit_rate; |
|
50 |
+ uint16_t flags; |
|
51 |
+ uint16_t block_align; |
|
52 |
+ uint16_t block_samples; |
|
53 |
+ |
|
54 |
+ enum CodecID codec_id; |
|
55 |
+} XMVAudioTrack; |
|
56 |
+ |
|
57 |
+typedef struct XMVVideoPacket { |
|
58 |
+ /* The decoder stream index for this video packet. */ |
|
59 |
+ int stream_index; |
|
60 |
+ |
|
61 |
+ uint32_t data_size; |
|
62 |
+ uint32_t data_offset; |
|
63 |
+ |
|
64 |
+ uint32_t current_frame; |
|
65 |
+ uint32_t frame_count; |
|
66 |
+ |
|
67 |
+ /* Does the video packet contain extra data? */ |
|
68 |
+ int has_extradata; |
|
69 |
+ |
|
70 |
+ /* Extra data */ |
|
71 |
+ uint8_t extradata[4]; |
|
72 |
+ |
|
73 |
+ int64_t last_pts; |
|
74 |
+ int64_t pts; |
|
75 |
+} XMVVideoPacket; |
|
76 |
+ |
|
77 |
+typedef struct XMVAudioPacket { |
|
78 |
+ /* The decoder stream index for this audio packet. */ |
|
79 |
+ int stream_index; |
|
80 |
+ |
|
81 |
+ /* The audio track this packet encodes. */ |
|
82 |
+ XMVAudioTrack *track; |
|
83 |
+ |
|
84 |
+ uint32_t data_size; |
|
85 |
+ uint32_t data_offset; |
|
86 |
+ |
|
87 |
+ uint32_t frame_size; |
|
88 |
+ |
|
89 |
+ uint32_t block_count; |
|
90 |
+} XMVAudioPacket; |
|
91 |
+ |
|
92 |
+typedef struct XMVDemuxContext { |
|
93 |
+ uint16_t audio_track_count; |
|
94 |
+ |
|
95 |
+ XMVAudioTrack *audio_tracks; |
|
96 |
+ |
|
97 |
+ uint32_t this_packet_size; |
|
98 |
+ uint32_t next_packet_size; |
|
99 |
+ |
|
100 |
+ uint32_t this_packet_offset; |
|
101 |
+ uint32_t next_packet_offset; |
|
102 |
+ |
|
103 |
+ uint16_t current_stream; |
|
104 |
+ uint16_t stream_count; |
|
105 |
+ |
|
106 |
+ XMVVideoPacket video; |
|
107 |
+ XMVAudioPacket *audio; |
|
108 |
+} XMVDemuxContext; |
|
109 |
+ |
|
110 |
+static int xmv_probe(AVProbeData *p) |
|
111 |
+{ |
|
112 |
+ uint32_t file_version; |
|
113 |
+ |
|
114 |
+ if (p->buf_size < XMV_MIN_HEADER_SIZE) |
|
115 |
+ return 0; |
|
116 |
+ |
|
117 |
+ file_version = AV_RL32(p->buf + 16); |
|
118 |
+ if ((file_version == 0) || (file_version > 4)) |
|
119 |
+ return 0; |
|
120 |
+ |
|
121 |
+ if (!memcmp(p->buf + 12, "xobX", 4)) |
|
122 |
+ return AVPROBE_SCORE_MAX; |
|
123 |
+ |
|
124 |
+ return 0; |
|
125 |
+} |
|
126 |
+ |
|
127 |
+static int xmv_read_header(AVFormatContext *s, |
|
128 |
+ AVFormatParameters *ap) |
|
129 |
+{ |
|
130 |
+ XMVDemuxContext *xmv = s->priv_data; |
|
131 |
+ AVIOContext *pb = s->pb; |
|
132 |
+ AVStream *vst = NULL; |
|
133 |
+ |
|
134 |
+ uint32_t file_version; |
|
135 |
+ uint32_t this_packet_size; |
|
136 |
+ uint16_t audio_track; |
|
137 |
+ |
|
138 |
+ avio_skip(pb, 4); /* Next packet size */ |
|
139 |
+ |
|
140 |
+ this_packet_size = avio_rl32(pb); |
|
141 |
+ |
|
142 |
+ avio_skip(pb, 4); /* Max packet size */ |
|
143 |
+ avio_skip(pb, 4); /* "xobX" */ |
|
144 |
+ |
|
145 |
+ file_version = avio_rl32(pb); |
|
146 |
+ if ((file_version != 4) && (file_version != 2)) |
|
147 |
+ av_log_ask_for_sample(s, "Found uncommon version %d\n", file_version); |
|
148 |
+ |
|
149 |
+ |
|
150 |
+ /* Video track */ |
|
151 |
+ |
|
152 |
+ vst = av_new_stream(s, 0); |
|
153 |
+ if (!vst) |
|
154 |
+ return AVERROR(ENOMEM); |
|
155 |
+ |
|
156 |
+ av_set_pts_info(vst, 32, 1, 1000); |
|
157 |
+ |
|
158 |
+ vst->codec->codec_type = AVMEDIA_TYPE_VIDEO; |
|
159 |
+ vst->codec->codec_id = CODEC_ID_WMV2; |
|
160 |
+ vst->codec->codec_tag = MKBETAG('W', 'M', 'V', '2'); |
|
161 |
+ vst->codec->width = avio_rl32(pb); |
|
162 |
+ vst->codec->height = avio_rl32(pb); |
|
163 |
+ |
|
164 |
+ vst->duration = avio_rl32(pb); |
|
165 |
+ |
|
166 |
+ xmv->video.stream_index = vst->index; |
|
167 |
+ |
|
168 |
+ /* Audio tracks */ |
|
169 |
+ |
|
170 |
+ xmv->audio_track_count = avio_rl16(pb); |
|
171 |
+ |
|
172 |
+ avio_skip(pb, 2); /* Unknown (padding?) */ |
|
173 |
+ |
|
174 |
+ xmv->audio_tracks = av_malloc(xmv->audio_track_count * sizeof(XMVAudioTrack)); |
|
175 |
+ if (!xmv->audio_tracks) |
|
176 |
+ return AVERROR(ENOMEM); |
|
177 |
+ |
|
178 |
+ xmv->audio = av_malloc(xmv->audio_track_count * sizeof(XMVAudioPacket)); |
|
179 |
+ if (!xmv->audio) |
|
180 |
+ return AVERROR(ENOMEM); |
|
181 |
+ |
|
182 |
+ for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) { |
|
183 |
+ XMVAudioTrack *track = &xmv->audio_tracks[audio_track]; |
|
184 |
+ XMVAudioPacket *packet = &xmv->audio [audio_track]; |
|
185 |
+ AVStream *ast = NULL; |
|
186 |
+ |
|
187 |
+ track->compression = avio_rl16(pb); |
|
188 |
+ track->channels = avio_rl16(pb); |
|
189 |
+ track->sample_rate = avio_rl32(pb); |
|
190 |
+ track->bits_per_sample = avio_rl16(pb); |
|
191 |
+ track->flags = avio_rl16(pb); |
|
192 |
+ |
|
193 |
+ track->bit_rate = track->bits_per_sample * |
|
194 |
+ track->sample_rate * |
|
195 |
+ track->channels; |
|
196 |
+ track->block_align = 36 * track->channels; |
|
197 |
+ track->block_samples = 64; |
|
198 |
+ track->codec_id = ff_wav_codec_get_id(track->compression, |
|
199 |
+ track->bits_per_sample); |
|
200 |
+ |
|
201 |
+ packet->track = track; |
|
202 |
+ packet->stream_index = -1; |
|
203 |
+ |
|
204 |
+ packet->frame_size = 0; |
|
205 |
+ packet->block_count = 0; |
|
206 |
+ |
|
207 |
+ /* TODO: ADPCM'd 5.1 sound is encoded in three separate streams. |
|
208 |
+ * Those need to be interleaved to a proper 5.1 stream. */ |
|
209 |
+ if (track->flags & XMV_AUDIO_ADPCM51) |
|
210 |
+ av_log(s, AV_LOG_WARNING, "Unsupported 5.1 ADPCM audio stream " |
|
211 |
+ "(0x%04X)\n", track->flags); |
|
212 |
+ |
|
213 |
+ ast = av_new_stream(s, audio_track); |
|
214 |
+ if (!ast) |
|
215 |
+ return AVERROR(ENOMEM); |
|
216 |
+ |
|
217 |
+ ast->codec->codec_type = AVMEDIA_TYPE_AUDIO; |
|
218 |
+ ast->codec->codec_id = track->codec_id; |
|
219 |
+ ast->codec->codec_tag = track->compression; |
|
220 |
+ ast->codec->channels = track->channels; |
|
221 |
+ ast->codec->sample_rate = track->sample_rate; |
|
222 |
+ ast->codec->bits_per_coded_sample = track->bits_per_sample; |
|
223 |
+ ast->codec->bit_rate = track->bit_rate; |
|
224 |
+ ast->codec->block_align = 36 * track->channels; |
|
225 |
+ |
|
226 |
+ av_set_pts_info(ast, 32, track->block_samples, track->sample_rate); |
|
227 |
+ |
|
228 |
+ packet->stream_index = ast->index; |
|
229 |
+ |
|
230 |
+ ast->duration = vst->duration; |
|
231 |
+ } |
|
232 |
+ |
|
233 |
+ |
|
234 |
+ /** Initialize the packet context */ |
|
235 |
+ |
|
236 |
+ xmv->next_packet_offset = avio_tell(pb); |
|
237 |
+ |
|
238 |
+ xmv->next_packet_size = this_packet_size - xmv->next_packet_offset; |
|
239 |
+ xmv->this_packet_size = 0; |
|
240 |
+ |
|
241 |
+ xmv->video.current_frame = 0; |
|
242 |
+ xmv->video.frame_count = 0; |
|
243 |
+ xmv->video.pts = 0; |
|
244 |
+ xmv->video.last_pts = 0; |
|
245 |
+ |
|
246 |
+ xmv->current_stream = 0; |
|
247 |
+ xmv->stream_count = xmv->audio_track_count + 1; |
|
248 |
+ |
|
249 |
+ return 0; |
|
250 |
+} |
|
251 |
+ |
|
252 |
+static void xmv_read_extradata(uint8_t *extradata, AVIOContext *pb) |
|
253 |
+{ |
|
254 |
+ /* Read the XMV extradata */ |
|
255 |
+ |
|
256 |
+ uint32_t data = avio_rl32(pb); |
|
257 |
+ |
|
258 |
+ int mspel_bit = !!(data & 0x01); |
|
259 |
+ int loop_filter = !!(data & 0x02); |
|
260 |
+ int abt_flag = !!(data & 0x04); |
|
261 |
+ int j_type_bit = !!(data & 0x08); |
|
262 |
+ int top_left_mv_flag = !!(data & 0x10); |
|
263 |
+ int per_mb_rl_bit = !!(data & 0x20); |
|
264 |
+ int slice_count = (data >> 6) & 7; |
|
265 |
+ |
|
266 |
+ /* Write it back as standard WMV2 extradata */ |
|
267 |
+ |
|
268 |
+ data = 0; |
|
269 |
+ |
|
270 |
+ data |= mspel_bit << 15; |
|
271 |
+ data |= loop_filter << 14; |
|
272 |
+ data |= abt_flag << 13; |
|
273 |
+ data |= j_type_bit << 12; |
|
274 |
+ data |= top_left_mv_flag << 11; |
|
275 |
+ data |= per_mb_rl_bit << 10; |
|
276 |
+ data |= slice_count << 7; |
|
277 |
+ |
|
278 |
+ AV_WB32(extradata, data); |
|
279 |
+} |
|
280 |
+ |
|
281 |
+static int xmv_process_packet_header(AVFormatContext *s) |
|
282 |
+{ |
|
283 |
+ XMVDemuxContext *xmv = s->priv_data; |
|
284 |
+ AVIOContext *pb = s->pb; |
|
285 |
+ |
|
286 |
+ uint8_t data[8]; |
|
287 |
+ uint16_t audio_track; |
|
288 |
+ uint32_t data_offset; |
|
289 |
+ |
|
290 |
+ /* Next packet size */ |
|
291 |
+ xmv->next_packet_size = avio_rl32(pb); |
|
292 |
+ |
|
293 |
+ /* Packet video header */ |
|
294 |
+ |
|
295 |
+ if (avio_read(pb, data, 8) != 8) |
|
296 |
+ return AVERROR(EIO); |
|
297 |
+ |
|
298 |
+ xmv->video.data_size = AV_RL32(data) & 0x007FFFFF; |
|
299 |
+ |
|
300 |
+ xmv->video.current_frame = 0; |
|
301 |
+ xmv->video.frame_count = (AV_RL32(data) >> 23) & 0xFF; |
|
302 |
+ |
|
303 |
+ xmv->video.has_extradata = (data[3] & 0x80) != 0; |
|
304 |
+ |
|
305 |
+ /* Adding the audio data sizes and the video data size keeps you 4 bytes |
|
306 |
+ * short for every audio track. But as playing around with XMV files with |
|
307 |
+ * ADPCM audio showed, taking the extra 4 bytes from the audio data gives |
|
308 |
+ * you either completely distorted audio or click (when skipping the |
|
309 |
+ * remaining 68 bytes of the ADPCM block). Substracting 4 bytes for every |
|
310 |
+ * audio track from the video data works at least for the audio. Probably |
|
311 |
+ * some alignment thing? |
|
312 |
+ * The video data has (always?) lots of padding, so it should work out... |
|
313 |
+ */ |
|
314 |
+ xmv->video.data_size -= xmv->audio_track_count * 4; |
|
315 |
+ |
|
316 |
+ xmv->current_stream = 0; |
|
317 |
+ if (!xmv->video.frame_count) { |
|
318 |
+ xmv->video.frame_count = 1; |
|
319 |
+ xmv->current_stream = 1; |
|
320 |
+ } |
|
321 |
+ |
|
322 |
+ /* Packet audio header */ |
|
323 |
+ |
|
324 |
+ for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) { |
|
325 |
+ XMVAudioPacket *packet = &xmv->audio[audio_track]; |
|
326 |
+ |
|
327 |
+ if (avio_read(pb, data, 4) != 4) |
|
328 |
+ return AVERROR(EIO); |
|
329 |
+ |
|
330 |
+ packet->data_size = AV_RL32(data) & 0x007FFFFF; |
|
331 |
+ if ((packet->data_size == 0) && (audio_track != 0)) |
|
332 |
+ /* This happens when I create an XMV with several identical audio |
|
333 |
+ * streams. From the size calculations, duplicating the previous |
|
334 |
+ * stream's size works out, but the track data itself is silent. |
|
335 |
+ * Maybe this should also redirect the offset to the previous track? |
|
336 |
+ */ |
|
337 |
+ packet->data_size = xmv->audio[audio_track - 1].data_size; |
|
338 |
+ |
|
339 |
+ /** Carve up the audio data in frame_count slices */ |
|
340 |
+ packet->frame_size = packet->data_size / xmv->video.frame_count; |
|
341 |
+ packet->frame_size -= packet->frame_size % packet->track->block_align; |
|
342 |
+ } |
|
343 |
+ |
|
344 |
+ /* Packet data offsets */ |
|
345 |
+ |
|
346 |
+ data_offset = avio_tell(pb); |
|
347 |
+ |
|
348 |
+ xmv->video.data_offset = data_offset; |
|
349 |
+ data_offset += xmv->video.data_size; |
|
350 |
+ |
|
351 |
+ for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) { |
|
352 |
+ xmv->audio[audio_track].data_offset = data_offset; |
|
353 |
+ data_offset += xmv->audio[audio_track].data_size; |
|
354 |
+ } |
|
355 |
+ |
|
356 |
+ /* Video frames header */ |
|
357 |
+ |
|
358 |
+ /* Read new video extra data */ |
|
359 |
+ if (xmv->video.data_size > 0) { |
|
360 |
+ if (xmv->video.has_extradata) { |
|
361 |
+ xmv_read_extradata(xmv->video.extradata, pb); |
|
362 |
+ |
|
363 |
+ xmv->video.data_size -= 4; |
|
364 |
+ xmv->video.data_offset += 4; |
|
365 |
+ |
|
366 |
+ if (xmv->video.stream_index >= 0) { |
|
367 |
+ AVStream *vst = s->streams[xmv->video.stream_index]; |
|
368 |
+ |
|
369 |
+ assert(xmv->video.stream_index < s->nb_streams); |
|
370 |
+ |
|
371 |
+ if (vst->codec->extradata_size < 4) { |
|
372 |
+ av_free(vst->codec->extradata); |
|
373 |
+ |
|
374 |
+ vst->codec->extradata = |
|
375 |
+ av_malloc(4 + FF_INPUT_BUFFER_PADDING_SIZE); |
|
376 |
+ vst->codec->extradata_size = 4; |
|
377 |
+ } |
|
378 |
+ |
|
379 |
+ memcpy(vst->codec->extradata, xmv->video.extradata, 4); |
|
380 |
+ } |
|
381 |
+ } |
|
382 |
+ } |
|
383 |
+ |
|
384 |
+ return 0; |
|
385 |
+} |
|
386 |
+ |
|
387 |
+static int xmv_fetch_new_packet(AVFormatContext *s) |
|
388 |
+{ |
|
389 |
+ XMVDemuxContext *xmv = s->priv_data; |
|
390 |
+ AVIOContext *pb = s->pb; |
|
391 |
+ int result; |
|
392 |
+ |
|
393 |
+ /* Seek to it */ |
|
394 |
+ xmv->this_packet_offset = xmv->next_packet_offset; |
|
395 |
+ if (avio_seek(pb, xmv->this_packet_offset, SEEK_SET) != xmv->this_packet_offset) |
|
396 |
+ return AVERROR(EIO); |
|
397 |
+ |
|
398 |
+ /* Update the size */ |
|
399 |
+ xmv->this_packet_size = xmv->next_packet_size; |
|
400 |
+ if (xmv->this_packet_size < (12 + xmv->audio_track_count * 4)) |
|
401 |
+ return AVERROR(EIO); |
|
402 |
+ |
|
403 |
+ /* Process the header */ |
|
404 |
+ result = xmv_process_packet_header(s); |
|
405 |
+ if (result) |
|
406 |
+ return result; |
|
407 |
+ |
|
408 |
+ /* Update the offset */ |
|
409 |
+ xmv->next_packet_offset = xmv->this_packet_offset + xmv->this_packet_size; |
|
410 |
+ |
|
411 |
+ return 0; |
|
412 |
+} |
|
413 |
+ |
|
414 |
+static int xmv_fetch_audio_packet(AVFormatContext *s, |
|
415 |
+ AVPacket *pkt, uint32_t stream) |
|
416 |
+{ |
|
417 |
+ XMVDemuxContext *xmv = s->priv_data; |
|
418 |
+ AVIOContext *pb = s->pb; |
|
419 |
+ XMVAudioPacket *audio = &xmv->audio[stream]; |
|
420 |
+ |
|
421 |
+ uint32_t data_size; |
|
422 |
+ uint32_t block_count; |
|
423 |
+ int result; |
|
424 |
+ |
|
425 |
+ /* Seek to it */ |
|
426 |
+ if (avio_seek(pb, audio->data_offset, SEEK_SET) != audio->data_offset) |
|
427 |
+ return AVERROR(EIO); |
|
428 |
+ |
|
429 |
+ if ((xmv->video.current_frame + 1) < xmv->video.frame_count) |
|
430 |
+ /* Not the last frame, get at most frame_size bytes. */ |
|
431 |
+ data_size = FFMIN(audio->frame_size, audio->data_size); |
|
432 |
+ else |
|
433 |
+ /* Last frame, get the rest. */ |
|
434 |
+ data_size = audio->data_size; |
|
435 |
+ |
|
436 |
+ /* Read the packet */ |
|
437 |
+ result = av_get_packet(pb, pkt, data_size); |
|
438 |
+ if (result <= 0) |
|
439 |
+ return result; |
|
440 |
+ |
|
441 |
+ pkt->stream_index = audio->stream_index; |
|
442 |
+ |
|
443 |
+ /* Calculate the PTS */ |
|
444 |
+ |
|
445 |
+ block_count = data_size / audio->track->block_align; |
|
446 |
+ |
|
447 |
+ pkt->duration = block_count; |
|
448 |
+ pkt->pts = audio->block_count; |
|
449 |
+ pkt->dts = AV_NOPTS_VALUE; |
|
450 |
+ |
|
451 |
+ audio->block_count += block_count; |
|
452 |
+ |
|
453 |
+ /* Advance offset */ |
|
454 |
+ audio->data_size -= data_size; |
|
455 |
+ audio->data_offset += data_size; |
|
456 |
+ |
|
457 |
+ return 0; |
|
458 |
+} |
|
459 |
+ |
|
460 |
+static int xmv_fetch_video_packet(AVFormatContext *s, |
|
461 |
+ AVPacket *pkt) |
|
462 |
+{ |
|
463 |
+ XMVDemuxContext *xmv = s->priv_data; |
|
464 |
+ AVIOContext *pb = s->pb; |
|
465 |
+ XMVVideoPacket *video = &xmv->video; |
|
466 |
+ |
|
467 |
+ int result; |
|
468 |
+ uint32_t frame_header; |
|
469 |
+ uint32_t frame_size, frame_timestamp; |
|
470 |
+ uint32_t i; |
|
471 |
+ |
|
472 |
+ /* Seek to it */ |
|
473 |
+ if (avio_seek(pb, video->data_offset, SEEK_SET) != video->data_offset) |
|
474 |
+ return AVERROR(EIO); |
|
475 |
+ |
|
476 |
+ /* Read the frame header */ |
|
477 |
+ frame_header = avio_rl32(pb); |
|
478 |
+ |
|
479 |
+ frame_size = (frame_header & 0x1FFFF) * 4 + 4; |
|
480 |
+ frame_timestamp = (frame_header >> 17); |
|
481 |
+ |
|
482 |
+ if ((frame_size + 4) > video->data_size) |
|
483 |
+ return AVERROR(EIO); |
|
484 |
+ |
|
485 |
+ /* Create the packet */ |
|
486 |
+ result = av_new_packet(pkt, frame_size); |
|
487 |
+ if (result) |
|
488 |
+ return result; |
|
489 |
+ |
|
490 |
+ /* Contrary to normal WMV2 video, the bit stream in XMV's |
|
491 |
+ * WMV2 is little-endian. |
|
492 |
+ * TODO: This manual swap is of course suboptimal. |
|
493 |
+ */ |
|
494 |
+ for (i = 0; i < frame_size; i += 4) |
|
495 |
+ AV_WB32(pkt->data + i, avio_rl32(pb)); |
|
496 |
+ |
|
497 |
+ pkt->stream_index = video->stream_index; |
|
498 |
+ |
|
499 |
+ /* Calculate the PTS */ |
|
500 |
+ |
|
501 |
+ video->last_pts = frame_timestamp + video->pts; |
|
502 |
+ |
|
503 |
+ pkt->duration = 0; |
|
504 |
+ pkt->pts = video->last_pts; |
|
505 |
+ pkt->dts = AV_NOPTS_VALUE; |
|
506 |
+ |
|
507 |
+ video->pts += frame_timestamp; |
|
508 |
+ |
|
509 |
+ /* Keyframe? */ |
|
510 |
+ pkt->flags = (pkt->data[0] & 0x80) ? 0 : AV_PKT_FLAG_KEY; |
|
511 |
+ |
|
512 |
+ /* Advance offset */ |
|
513 |
+ video->data_size -= frame_size + 4; |
|
514 |
+ video->data_offset += frame_size + 4; |
|
515 |
+ |
|
516 |
+ return 0; |
|
517 |
+} |
|
518 |
+ |
|
519 |
+static int xmv_read_packet(AVFormatContext *s, |
|
520 |
+ AVPacket *pkt) |
|
521 |
+{ |
|
522 |
+ XMVDemuxContext *xmv = s->priv_data; |
|
523 |
+ int result; |
|
524 |
+ |
|
525 |
+ if (xmv->video.current_frame == xmv->video.frame_count) { |
|
526 |
+ /* No frames left in this packet, so we fetch a new one */ |
|
527 |
+ |
|
528 |
+ result = xmv_fetch_new_packet(s); |
|
529 |
+ if (result) |
|
530 |
+ return result; |
|
531 |
+ } |
|
532 |
+ |
|
533 |
+ if (xmv->current_stream == 0) { |
|
534 |
+ /* Fetch a video frame */ |
|
535 |
+ |
|
536 |
+ result = xmv_fetch_video_packet(s, pkt); |
|
537 |
+ if (result) |
|
538 |
+ return result; |
|
539 |
+ |
|
540 |
+ } else { |
|
541 |
+ /* Fetch an audio frame */ |
|
542 |
+ |
|
543 |
+ result = xmv_fetch_audio_packet(s, pkt, xmv->current_stream - 1); |
|
544 |
+ if (result) |
|
545 |
+ return result; |
|
546 |
+ } |
|
547 |
+ |
|
548 |
+ /* Increase our counters */ |
|
549 |
+ if (++xmv->current_stream >= xmv->stream_count) { |
|
550 |
+ xmv->current_stream = 0; |
|
551 |
+ xmv->video.current_frame += 1; |
|
552 |
+ } |
|
553 |
+ |
|
554 |
+ return 0; |
|
555 |
+} |
|
556 |
+ |
|
557 |
+static int xmv_read_close(AVFormatContext *s) |
|
558 |
+{ |
|
559 |
+ XMVDemuxContext *xmv = s->priv_data; |
|
560 |
+ |
|
561 |
+ av_free(xmv->audio); |
|
562 |
+ av_free(xmv->audio_tracks); |
|
563 |
+ |
|
564 |
+ return 0; |
|
565 |
+} |
|
566 |
+ |
|
567 |
+AVInputFormat ff_xmv_demuxer = { |
|
568 |
+ .name = "xmv", |
|
569 |
+ .long_name = NULL_IF_CONFIG_SMALL("Microsoft XMV"), |
|
570 |
+ .priv_data_size = sizeof(XMVDemuxContext), |
|
571 |
+ .read_probe = xmv_probe, |
|
572 |
+ .read_header = xmv_read_header, |
|
573 |
+ .read_packet = xmv_read_packet, |
|
574 |
+ .read_close = xmv_read_close, |
|
575 |
+}; |