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