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