Line | Branch | Exec | Source |
---|---|---|---|
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 | /** | ||
24 | * @file | ||
25 | * Microsoft XMV demuxer | ||
26 | */ | ||
27 | |||
28 | #include <inttypes.h> | ||
29 | |||
30 | #include "libavutil/intreadwrite.h" | ||
31 | #include "libavutil/mem.h" | ||
32 | |||
33 | #include "avformat.h" | ||
34 | #include "demux.h" | ||
35 | #include "internal.h" | ||
36 | #include "riff.h" | ||
37 | #include "libavutil/avassert.h" | ||
38 | |||
39 | /** The min size of an XMV header. */ | ||
40 | #define XMV_MIN_HEADER_SIZE 36 | ||
41 | |||
42 | /** Audio flag: ADPCM'd 5.1 stream, front left / right channels */ | ||
43 | #define XMV_AUDIO_ADPCM51_FRONTLEFTRIGHT 1 | ||
44 | /** Audio flag: ADPCM'd 5.1 stream, front center / low frequency channels */ | ||
45 | #define XMV_AUDIO_ADPCM51_FRONTCENTERLOW 2 | ||
46 | /** Audio flag: ADPCM'd 5.1 stream, rear left / right channels */ | ||
47 | #define XMV_AUDIO_ADPCM51_REARLEFTRIGHT 4 | ||
48 | |||
49 | /** Audio flag: Any of the ADPCM'd 5.1 stream flags. */ | ||
50 | #define XMV_AUDIO_ADPCM51 (XMV_AUDIO_ADPCM51_FRONTLEFTRIGHT | \ | ||
51 | XMV_AUDIO_ADPCM51_FRONTCENTERLOW | \ | ||
52 | XMV_AUDIO_ADPCM51_REARLEFTRIGHT) | ||
53 | |||
54 | #define XMV_BLOCK_ALIGN_SIZE 36 | ||
55 | |||
56 | /** A video packet with an XMV file. */ | ||
57 | typedef struct XMVVideoPacket { | ||
58 | int created; | ||
59 | int stream_index; ///< The decoder stream index for this video packet. | ||
60 | |||
61 | uint32_t data_size; ///< The size of the remaining video data. | ||
62 | uint64_t data_offset; ///< The offset of the video data within the file. | ||
63 | |||
64 | uint32_t current_frame; ///< The current frame within this video packet. | ||
65 | uint32_t frame_count; ///< The amount of frames within this video packet. | ||
66 | |||
67 | int has_extradata; ///< Does the video packet contain extra data? | ||
68 | uint8_t extradata[4]; ///< The extra data | ||
69 | |||
70 | int64_t last_pts; ///< PTS of the last video frame. | ||
71 | int64_t pts; ///< PTS of the most current video frame. | ||
72 | } XMVVideoPacket; | ||
73 | |||
74 | /** An audio packet with an XMV file. */ | ||
75 | typedef struct XMVAudioPacket { | ||
76 | int created; | ||
77 | int stream_index; ///< The decoder stream index for this audio packet. | ||
78 | |||
79 | /* Stream format properties. */ | ||
80 | uint16_t compression; ///< The type of compression. | ||
81 | uint16_t channels; ///< Number of channels. | ||
82 | int32_t sample_rate; ///< Sampling rate. | ||
83 | uint16_t bits_per_sample; ///< Bits per compressed sample. | ||
84 | uint64_t bit_rate; ///< Bits of compressed data per second. | ||
85 | uint16_t flags; ///< Flags | ||
86 | unsigned block_align; ///< Bytes per compressed block. | ||
87 | uint16_t block_samples; ///< Decompressed samples per compressed block. | ||
88 | |||
89 | enum AVCodecID codec_id; ///< The codec ID of the compression scheme. | ||
90 | |||
91 | uint32_t data_size; ///< The size of the remaining audio data. | ||
92 | uint64_t data_offset; ///< The offset of the audio data within the file. | ||
93 | |||
94 | uint32_t frame_size; ///< Number of bytes to put into an audio frame. | ||
95 | |||
96 | uint64_t block_count; ///< Running counter of decompressed audio block. | ||
97 | } XMVAudioPacket; | ||
98 | |||
99 | /** Context for demuxing an XMV file. */ | ||
100 | typedef struct XMVDemuxContext { | ||
101 | uint16_t audio_track_count; ///< Number of audio track in this file. | ||
102 | |||
103 | uint32_t this_packet_size; ///< Size of the current packet. | ||
104 | uint32_t next_packet_size; ///< Size of the next packet. | ||
105 | |||
106 | uint64_t this_packet_offset; ///< Offset of the current packet. | ||
107 | uint64_t next_packet_offset; ///< Offset of the next packet. | ||
108 | |||
109 | uint16_t current_stream; ///< The index of the stream currently handling. | ||
110 | uint16_t stream_count; ///< The number of streams in this file. | ||
111 | |||
112 | uint32_t video_duration; | ||
113 | uint32_t video_width; | ||
114 | uint32_t video_height; | ||
115 | |||
116 | XMVVideoPacket video; ///< The video packet contained in each packet. | ||
117 | XMVAudioPacket *audio; ///< The audio packets contained in each packet. | ||
118 | } XMVDemuxContext; | ||
119 | |||
120 | 7128 | static int xmv_probe(const AVProbeData *p) | |
121 | { | ||
122 | uint32_t file_version; | ||
123 | |||
124 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7127 times.
|
7128 | if (p->buf_size < XMV_MIN_HEADER_SIZE) |
125 | 1 | return 0; | |
126 | |||
127 | 7127 | file_version = AV_RL32(p->buf + 16); | |
128 |
4/4✓ Branch 0 taken 6880 times.
✓ Branch 1 taken 247 times.
✓ Branch 2 taken 6846 times.
✓ Branch 3 taken 34 times.
|
7127 | if ((file_version == 0) || (file_version > 4)) |
129 | 7093 | return 0; | |
130 | |||
131 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 33 times.
|
34 | if (!memcmp(p->buf + 12, "xobX", 4)) |
132 | 1 | return AVPROBE_SCORE_MAX; | |
133 | |||
134 | 33 | return 0; | |
135 | } | ||
136 | |||
137 | 1 | static int xmv_read_close(AVFormatContext *s) | |
138 | { | ||
139 | 1 | XMVDemuxContext *xmv = s->priv_data; | |
140 | |||
141 | 1 | av_freep(&xmv->audio); | |
142 | |||
143 | 1 | return 0; | |
144 | } | ||
145 | |||
146 | 1 | static int xmv_read_header(AVFormatContext *s) | |
147 | { | ||
148 | 1 | XMVDemuxContext *xmv = s->priv_data; | |
149 | 1 | AVIOContext *pb = s->pb; | |
150 | |||
151 | uint32_t file_version; | ||
152 | uint32_t this_packet_size; | ||
153 | uint16_t audio_track; | ||
154 | |||
155 | 1 | s->ctx_flags |= AVFMTCTX_NOHEADER; | |
156 | |||
157 | 1 | avio_skip(pb, 4); /* Next packet size */ | |
158 | |||
159 | 1 | this_packet_size = avio_rl32(pb); | |
160 | |||
161 | 1 | avio_skip(pb, 4); /* Max packet size */ | |
162 | 1 | avio_skip(pb, 4); /* "xobX" */ | |
163 | |||
164 | 1 | file_version = avio_rl32(pb); | |
165 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1 | if ((file_version != 4) && (file_version != 2)) |
166 | ✗ | avpriv_request_sample(s, "Uncommon version %"PRIu32"", file_version); | |
167 | |||
168 | /* Video tracks */ | ||
169 | |||
170 | 1 | xmv->video_width = avio_rl32(pb); | |
171 | 1 | xmv->video_height = avio_rl32(pb); | |
172 | 1 | xmv->video_duration = avio_rl32(pb); | |
173 | |||
174 | /* Audio tracks */ | ||
175 | |||
176 | 1 | xmv->audio_track_count = avio_rl16(pb); | |
177 | |||
178 | 1 | avio_skip(pb, 2); /* Unknown (padding?) */ | |
179 | |||
180 | 1 | xmv->audio = av_calloc(xmv->audio_track_count, sizeof(*xmv->audio)); | |
181 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!xmv->audio) |
182 | ✗ | return AVERROR(ENOMEM); | |
183 | |||
184 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) { |
185 | 1 | XMVAudioPacket *packet = &xmv->audio[audio_track]; | |
186 | |||
187 | 1 | packet->compression = avio_rl16(pb); | |
188 | 1 | packet->channels = avio_rl16(pb); | |
189 | 1 | packet->sample_rate = avio_rl32(pb); | |
190 | 1 | packet->bits_per_sample = avio_rl16(pb); | |
191 | 1 | packet->flags = avio_rl16(pb); | |
192 | |||
193 | 1 | packet->bit_rate = (uint64_t)packet->bits_per_sample * | |
194 | 1 | packet->sample_rate * | |
195 | 1 | packet->channels; | |
196 | 1 | packet->block_align = XMV_BLOCK_ALIGN_SIZE * packet->channels; | |
197 | 1 | packet->block_samples = 64; | |
198 | 2 | packet->codec_id = ff_wav_codec_get_id(packet->compression, | |
199 | 1 | packet->bits_per_sample); | |
200 | |||
201 | 1 | packet->stream_index = -1; | |
202 | |||
203 | 1 | packet->frame_size = 0; | |
204 | 1 | packet->block_count = 0; | |
205 | |||
206 | /* TODO: ADPCM'd 5.1 sound is encoded in three separate streams. | ||
207 | * Those need to be interleaved to a proper 5.1 stream. */ | ||
208 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (packet->flags & XMV_AUDIO_ADPCM51) |
209 | ✗ | av_log(s, AV_LOG_WARNING, "Unsupported 5.1 ADPCM audio stream " | |
210 | ✗ | "(0x%04X)\n", packet->flags); | |
211 | |||
212 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | if (!packet->channels || packet->sample_rate <= 0 || |
213 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | packet->channels >= UINT16_MAX / XMV_BLOCK_ALIGN_SIZE) { |
214 | ✗ | av_log(s, AV_LOG_ERROR, "Invalid parameters for audio track %"PRIu16".\n", | |
215 | audio_track); | ||
216 | ✗ | return AVERROR_INVALIDDATA; | |
217 | } | ||
218 | } | ||
219 | |||
220 | |||
221 | /* Initialize the packet context */ | ||
222 | |||
223 | 1 | xmv->next_packet_offset = avio_tell(pb); | |
224 | 1 | xmv->next_packet_size = this_packet_size - xmv->next_packet_offset; | |
225 | 1 | xmv->stream_count = xmv->audio_track_count + 1; | |
226 | |||
227 | 1 | return 0; | |
228 | } | ||
229 | |||
230 | 1 | static void xmv_read_extradata(uint8_t *extradata, AVIOContext *pb) | |
231 | { | ||
232 | /* Read the XMV extradata */ | ||
233 | |||
234 | 1 | uint32_t data = avio_rl32(pb); | |
235 | |||
236 | 1 | int mspel_bit = !!(data & 0x01); | |
237 | 1 | int loop_filter = !!(data & 0x02); | |
238 | 1 | int abt_flag = !!(data & 0x04); | |
239 | 1 | int j_type_bit = !!(data & 0x08); | |
240 | 1 | int top_left_mv_flag = !!(data & 0x10); | |
241 | 1 | int per_mb_rl_bit = !!(data & 0x20); | |
242 | 1 | int slice_count = (data >> 6) & 7; | |
243 | |||
244 | /* Write it back as standard WMV2 extradata */ | ||
245 | |||
246 | 1 | data = 0; | |
247 | |||
248 | 1 | data |= mspel_bit << 15; | |
249 | 1 | data |= loop_filter << 14; | |
250 | 1 | data |= abt_flag << 13; | |
251 | 1 | data |= j_type_bit << 12; | |
252 | 1 | data |= top_left_mv_flag << 11; | |
253 | 1 | data |= per_mb_rl_bit << 10; | |
254 | 1 | data |= slice_count << 7; | |
255 | |||
256 | 1 | AV_WB32(extradata, data); | |
257 | 1 | } | |
258 | |||
259 | 18 | static int xmv_process_packet_header(AVFormatContext *s) | |
260 | { | ||
261 | 18 | XMVDemuxContext *xmv = s->priv_data; | |
262 | 18 | AVIOContext *pb = s->pb; | |
263 | int ret; | ||
264 | |||
265 | uint8_t data[8]; | ||
266 | uint16_t audio_track; | ||
267 | uint64_t data_offset; | ||
268 | |||
269 | /* Next packet size */ | ||
270 | 18 | xmv->next_packet_size = avio_rl32(pb); | |
271 | |||
272 | /* Packet video header */ | ||
273 | |||
274 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
|
18 | if (avio_read(pb, data, 8) != 8) |
275 | ✗ | return AVERROR(EIO); | |
276 | |||
277 | 18 | xmv->video.data_size = AV_RL32(data) & 0x007FFFFF; | |
278 | |||
279 | 18 | xmv->video.current_frame = 0; | |
280 | 18 | xmv->video.frame_count = (AV_RL32(data) >> 23) & 0xFF; | |
281 | |||
282 | 18 | xmv->video.has_extradata = (data[3] & 0x80) != 0; | |
283 | |||
284 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 17 times.
|
18 | if (!xmv->video.created) { |
285 | 1 | AVStream *vst = avformat_new_stream(s, NULL); | |
286 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!vst) |
287 | ✗ | return AVERROR(ENOMEM); | |
288 | |||
289 | 1 | avpriv_set_pts_info(vst, 32, 1, 1000); | |
290 | |||
291 | 1 | vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; | |
292 | 1 | vst->codecpar->codec_id = AV_CODEC_ID_WMV2; | |
293 | 1 | vst->codecpar->codec_tag = MKBETAG('W', 'M', 'V', '2'); | |
294 | 1 | vst->codecpar->width = xmv->video_width; | |
295 | 1 | vst->codecpar->height = xmv->video_height; | |
296 | |||
297 | 1 | vst->duration = xmv->video_duration; | |
298 | |||
299 | 1 | xmv->video.stream_index = vst->index; | |
300 | |||
301 | 1 | xmv->video.created = 1; | |
302 | } | ||
303 | |||
304 | /* Adding the audio data sizes and the video data size keeps you 4 bytes | ||
305 | * short for every audio track. But as playing around with XMV files with | ||
306 | * ADPCM audio showed, taking the extra 4 bytes from the audio data gives | ||
307 | * you either completely distorted audio or click (when skipping the | ||
308 | * remaining 68 bytes of the ADPCM block). Subtracting 4 bytes for every | ||
309 | * audio track from the video data works at least for the audio. Probably | ||
310 | * some alignment thing? | ||
311 | * The video data has (always?) lots of padding, so it should work out... | ||
312 | */ | ||
313 | 18 | xmv->video.data_size -= xmv->audio_track_count * 4; | |
314 | |||
315 | 18 | xmv->current_stream = 0; | |
316 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 17 times.
|
18 | if (!xmv->video.frame_count) { |
317 | 1 | xmv->video.frame_count = 1; | |
318 | 1 | xmv->current_stream = xmv->stream_count > 1; | |
319 | } | ||
320 | |||
321 | /* Packet audio header */ | ||
322 | |||
323 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
|
36 | for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) { |
324 | 18 | XMVAudioPacket *packet = &xmv->audio[audio_track]; | |
325 | |||
326 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
|
18 | if (avio_read(pb, data, 4) != 4) |
327 | ✗ | return AVERROR(EIO); | |
328 | |||
329 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 17 times.
|
18 | if (!packet->created) { |
330 | 1 | AVStream *ast = avformat_new_stream(s, NULL); | |
331 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!ast) |
332 | ✗ | return AVERROR(ENOMEM); | |
333 | |||
334 | 1 | ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
335 | 1 | ast->codecpar->codec_id = packet->codec_id; | |
336 | 1 | ast->codecpar->codec_tag = packet->compression; | |
337 | 1 | ast->codecpar->ch_layout.nb_channels = packet->channels; | |
338 | 1 | ast->codecpar->sample_rate = packet->sample_rate; | |
339 | 1 | ast->codecpar->bits_per_coded_sample = packet->bits_per_sample; | |
340 | 1 | ast->codecpar->bit_rate = packet->bit_rate; | |
341 | 1 | ast->codecpar->block_align = 36 * packet->channels; | |
342 | |||
343 | 1 | avpriv_set_pts_info(ast, 32, packet->block_samples, packet->sample_rate); | |
344 | |||
345 | 1 | packet->stream_index = ast->index; | |
346 | |||
347 | 1 | ast->duration = xmv->video_duration; | |
348 | |||
349 | 1 | packet->created = 1; | |
350 | } | ||
351 | |||
352 | 18 | packet->data_size = AV_RL32(data) & 0x007FFFFF; | |
353 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
18 | if ((packet->data_size == 0) && (audio_track != 0)) |
354 | /* This happens when I create an XMV with several identical audio | ||
355 | * streams. From the size calculations, duplicating the previous | ||
356 | * stream's size works out, but the track data itself is silent. | ||
357 | * Maybe this should also redirect the offset to the previous track? | ||
358 | */ | ||
359 | ✗ | packet->data_size = xmv->audio[audio_track - 1].data_size; | |
360 | |||
361 | /* Carve up the audio data in frame_count slices */ | ||
362 | 18 | packet->frame_size = packet->data_size / xmv->video.frame_count; | |
363 | 18 | packet->frame_size -= packet->frame_size % packet->block_align; | |
364 | } | ||
365 | |||
366 | /* Packet data offsets */ | ||
367 | |||
368 | 18 | data_offset = avio_tell(pb); | |
369 | |||
370 | 18 | xmv->video.data_offset = data_offset; | |
371 | 18 | data_offset += xmv->video.data_size; | |
372 | |||
373 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
|
36 | for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) { |
374 | 18 | xmv->audio[audio_track].data_offset = data_offset; | |
375 | 18 | data_offset += xmv->audio[audio_track].data_size; | |
376 | } | ||
377 | |||
378 | /* Video frames header */ | ||
379 | |||
380 | /* Read new video extra data */ | ||
381 |
1/2✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
|
18 | if (xmv->video.data_size > 0) { |
382 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 17 times.
|
18 | if (xmv->video.has_extradata) { |
383 | 1 | xmv_read_extradata(xmv->video.extradata, pb); | |
384 | |||
385 | 1 | xmv->video.data_size -= 4; | |
386 | 1 | xmv->video.data_offset += 4; | |
387 | |||
388 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (xmv->video.stream_index >= 0) { |
389 | 1 | AVStream *vst = s->streams[xmv->video.stream_index]; | |
390 | |||
391 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | av_assert0(xmv->video.stream_index < s->nb_streams); |
392 | |||
393 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (vst->codecpar->extradata_size < 4) { |
394 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if ((ret = ff_alloc_extradata(vst->codecpar, 4)) < 0) |
395 | ✗ | return ret; | |
396 | } | ||
397 | |||
398 | 1 | memcpy(vst->codecpar->extradata, xmv->video.extradata, 4); | |
399 | } | ||
400 | } | ||
401 | } | ||
402 | |||
403 | 18 | return 0; | |
404 | } | ||
405 | |||
406 | 19 | static int xmv_fetch_new_packet(AVFormatContext *s) | |
407 | { | ||
408 | 19 | XMVDemuxContext *xmv = s->priv_data; | |
409 | 19 | AVIOContext *pb = s->pb; | |
410 | int result; | ||
411 | |||
412 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
|
19 | if (xmv->this_packet_offset == xmv->next_packet_offset) |
413 | ✗ | return AVERROR_EOF; | |
414 | |||
415 | /* Seek to it */ | ||
416 | 19 | xmv->this_packet_offset = xmv->next_packet_offset; | |
417 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
|
19 | if (avio_seek(pb, xmv->this_packet_offset, SEEK_SET) != xmv->this_packet_offset) |
418 | ✗ | return AVERROR(EIO); | |
419 | |||
420 | /* Update the size */ | ||
421 | 19 | xmv->this_packet_size = xmv->next_packet_size; | |
422 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 18 times.
|
19 | if (xmv->this_packet_size < (12 + xmv->audio_track_count * 4)) |
423 | 1 | return AVERROR(EIO); | |
424 | |||
425 | /* Process the header */ | ||
426 | 18 | result = xmv_process_packet_header(s); | |
427 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
|
18 | if (result) |
428 | ✗ | return result; | |
429 | |||
430 | /* Update the offset */ | ||
431 | 18 | xmv->next_packet_offset = xmv->this_packet_offset + xmv->this_packet_size; | |
432 | |||
433 | 18 | return 0; | |
434 | } | ||
435 | |||
436 | 91 | static int xmv_fetch_audio_packet(AVFormatContext *s, | |
437 | AVPacket *pkt, uint32_t stream) | ||
438 | { | ||
439 | 91 | XMVDemuxContext *xmv = s->priv_data; | |
440 | 91 | AVIOContext *pb = s->pb; | |
441 | 91 | XMVAudioPacket *audio = &xmv->audio[stream]; | |
442 | |||
443 | uint32_t data_size; | ||
444 | uint32_t block_count; | ||
445 | int result; | ||
446 | |||
447 | /* Seek to it */ | ||
448 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 91 times.
|
91 | if (avio_seek(pb, audio->data_offset, SEEK_SET) != audio->data_offset) |
449 | ✗ | return AVERROR(EIO); | |
450 | |||
451 |
2/2✓ Branch 0 taken 73 times.
✓ Branch 1 taken 18 times.
|
91 | if ((xmv->video.current_frame + 1) < xmv->video.frame_count) |
452 | /* Not the last frame, get at most frame_size bytes. */ | ||
453 | 73 | data_size = FFMIN(audio->frame_size, audio->data_size); | |
454 | else | ||
455 | /* Last frame, get the rest. */ | ||
456 | 18 | data_size = audio->data_size; | |
457 | |||
458 | /* Read the packet */ | ||
459 | 91 | result = av_get_packet(pb, pkt, data_size); | |
460 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 91 times.
|
91 | if (result <= 0) |
461 | ✗ | return result; | |
462 | |||
463 | 91 | pkt->stream_index = audio->stream_index; | |
464 | |||
465 | /* Calculate the PTS */ | ||
466 | |||
467 | 91 | block_count = data_size / audio->block_align; | |
468 | |||
469 | 91 | pkt->duration = block_count; | |
470 | 91 | pkt->pts = audio->block_count; | |
471 | 91 | pkt->dts = AV_NOPTS_VALUE; | |
472 | |||
473 | 91 | audio->block_count += block_count; | |
474 | |||
475 | /* Advance offset */ | ||
476 | 91 | audio->data_size -= data_size; | |
477 | 91 | audio->data_offset += data_size; | |
478 | |||
479 | 91 | return 0; | |
480 | } | ||
481 | |||
482 | 90 | static int xmv_fetch_video_packet(AVFormatContext *s, | |
483 | AVPacket *pkt) | ||
484 | { | ||
485 | 90 | XMVDemuxContext *xmv = s->priv_data; | |
486 | 90 | AVIOContext *pb = s->pb; | |
487 | 90 | XMVVideoPacket *video = &xmv->video; | |
488 | |||
489 | int result; | ||
490 | uint32_t frame_header; | ||
491 | uint32_t frame_size, frame_timestamp; | ||
492 | uint8_t *data, *end; | ||
493 | |||
494 | /* Seek to it */ | ||
495 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 90 times.
|
90 | if (avio_seek(pb, video->data_offset, SEEK_SET) != video->data_offset) |
496 | ✗ | return AVERROR(EIO); | |
497 | |||
498 | /* Read the frame header */ | ||
499 | 90 | frame_header = avio_rl32(pb); | |
500 | |||
501 | 90 | frame_size = (frame_header & 0x1FFFF) * 4 + 4; | |
502 | 90 | frame_timestamp = (frame_header >> 17); | |
503 | |||
504 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
|
90 | if ((frame_size + 4) > video->data_size) |
505 | ✗ | return AVERROR(EIO); | |
506 | |||
507 | /* Get the packet data */ | ||
508 | 90 | result = av_get_packet(pb, pkt, frame_size); | |
509 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
|
90 | if (result != frame_size) |
510 | ✗ | return result; | |
511 | |||
512 | /* Contrary to normal WMV2 video, the bit stream in XMV's | ||
513 | * WMV2 is little-endian. | ||
514 | * TODO: This manual swap is of course suboptimal. | ||
515 | */ | ||
516 |
2/2✓ Branch 0 taken 135125 times.
✓ Branch 1 taken 90 times.
|
135215 | for (data = pkt->data, end = pkt->data + frame_size; data < end; data += 4) |
517 | 135125 | AV_WB32(data, AV_RL32(data)); | |
518 | |||
519 | 90 | pkt->stream_index = video->stream_index; | |
520 | |||
521 | /* Calculate the PTS */ | ||
522 | |||
523 | 90 | video->last_pts = frame_timestamp + video->pts; | |
524 | |||
525 | 90 | pkt->duration = 0; | |
526 | 90 | pkt->pts = video->last_pts; | |
527 | 90 | pkt->dts = AV_NOPTS_VALUE; | |
528 | |||
529 | 90 | video->pts += frame_timestamp; | |
530 | |||
531 | /* Keyframe? */ | ||
532 | 90 | pkt->flags = (pkt->data[0] & 0x80) ? 0 : AV_PKT_FLAG_KEY; | |
533 | |||
534 | /* Advance offset */ | ||
535 | 90 | video->data_size -= frame_size + 4; | |
536 | 90 | video->data_offset += frame_size + 4; | |
537 | |||
538 | 90 | return 0; | |
539 | } | ||
540 | |||
541 | 182 | static int xmv_read_packet(AVFormatContext *s, | |
542 | AVPacket *pkt) | ||
543 | { | ||
544 | 182 | XMVDemuxContext *xmv = s->priv_data; | |
545 | int result; | ||
546 | |||
547 |
2/2✓ Branch 0 taken 19 times.
✓ Branch 1 taken 163 times.
|
182 | if (xmv->video.current_frame == xmv->video.frame_count) { |
548 | /* No frames left in this packet, so we fetch a new one */ | ||
549 | |||
550 | 19 | result = xmv_fetch_new_packet(s); | |
551 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 18 times.
|
19 | if (result) |
552 | 1 | return result; | |
553 | } | ||
554 | |||
555 |
2/2✓ Branch 0 taken 90 times.
✓ Branch 1 taken 91 times.
|
181 | if (xmv->current_stream == 0) { |
556 | /* Fetch a video frame */ | ||
557 | |||
558 | 90 | result = xmv_fetch_video_packet(s, pkt); | |
559 | } else { | ||
560 | /* Fetch an audio frame */ | ||
561 | |||
562 | 91 | result = xmv_fetch_audio_packet(s, pkt, xmv->current_stream - 1); | |
563 | } | ||
564 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 181 times.
|
181 | if (result) { |
565 | ✗ | xmv->current_stream = 0; | |
566 | ✗ | xmv->video.current_frame = xmv->video.frame_count; | |
567 | ✗ | return result; | |
568 | } | ||
569 | |||
570 | |||
571 | /* Increase our counters */ | ||
572 |
2/2✓ Branch 0 taken 91 times.
✓ Branch 1 taken 90 times.
|
181 | if (++xmv->current_stream >= xmv->stream_count) { |
573 | 91 | xmv->current_stream = 0; | |
574 | 91 | xmv->video.current_frame += 1; | |
575 | } | ||
576 | |||
577 | 181 | return 0; | |
578 | } | ||
579 | |||
580 | const FFInputFormat ff_xmv_demuxer = { | ||
581 | .p.name = "xmv", | ||
582 | .p.long_name = NULL_IF_CONFIG_SMALL("Microsoft XMV"), | ||
583 | .p.extensions = "xmv", | ||
584 | .priv_data_size = sizeof(XMVDemuxContext), | ||
585 | .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP, | ||
586 | .read_probe = xmv_probe, | ||
587 | .read_header = xmv_read_header, | ||
588 | .read_packet = xmv_read_packet, | ||
589 | .read_close = xmv_read_close, | ||
590 | }; | ||
591 |