]> git.sesse.net Git - ffmpeg/blob - libavformat/mux.c
dsputil_alpha.h: Add missing stddef.h header to fix standalone compilation
[ffmpeg] / libavformat / mux.c
1 /*
2  * muxing functions for use within Libav
3  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
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 /* #define DEBUG */
23
24 #include "avformat.h"
25 #include "avio_internal.h"
26 #include "internal.h"
27 #include "libavcodec/internal.h"
28 #include "libavcodec/bytestream.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/dict.h"
31 #include "libavutil/pixdesc.h"
32 #include "metadata.h"
33 #include "id3v2.h"
34 #include "libavutil/avassert.h"
35 #include "libavutil/avstring.h"
36 #include "libavutil/mathematics.h"
37 #include "libavutil/parseutils.h"
38 #include "libavutil/time.h"
39 #include "riff.h"
40 #include "audiointerleave.h"
41 #include "url.h"
42 #include <stdarg.h>
43 #if CONFIG_NETWORK
44 #include "network.h"
45 #endif
46
47 #undef NDEBUG
48 #include <assert.h>
49
50 /**
51  * @file
52  * muxing functions for use within Libav
53  */
54
55 /* fraction handling */
56
57 /**
58  * f = val + (num / den) + 0.5.
59  *
60  * 'num' is normalized so that it is such as 0 <= num < den.
61  *
62  * @param f fractional number
63  * @param val integer value
64  * @param num must be >= 0
65  * @param den must be >= 1
66  */
67 static void frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den)
68 {
69     num += (den >> 1);
70     if (num >= den) {
71         val += num / den;
72         num  = num % den;
73     }
74     f->val = val;
75     f->num = num;
76     f->den = den;
77 }
78
79 /**
80  * Fractional addition to f: f = f + (incr / f->den).
81  *
82  * @param f fractional number
83  * @param incr increment, can be positive or negative
84  */
85 static void frac_add(AVFrac *f, int64_t incr)
86 {
87     int64_t num, den;
88
89     num = f->num + incr;
90     den = f->den;
91     if (num < 0) {
92         f->val += num / den;
93         num     = num % den;
94         if (num < 0) {
95             num += den;
96             f->val--;
97         }
98     } else if (num >= den) {
99         f->val += num / den;
100         num     = num % den;
101     }
102     f->num = num;
103 }
104
105 static int validate_codec_tag(AVFormatContext *s, AVStream *st)
106 {
107     const AVCodecTag *avctag;
108     int n;
109     enum AVCodecID id = AV_CODEC_ID_NONE;
110     unsigned int tag  = 0;
111
112     /**
113      * Check that tag + id is in the table
114      * If neither is in the table -> OK
115      * If tag is in the table with another id -> FAIL
116      * If id is in the table with another tag -> FAIL unless strict < normal
117      */
118     for (n = 0; s->oformat->codec_tag[n]; n++) {
119         avctag = s->oformat->codec_tag[n];
120         while (avctag->id != AV_CODEC_ID_NONE) {
121             if (avpriv_toupper4(avctag->tag) == avpriv_toupper4(st->codec->codec_tag)) {
122                 id = avctag->id;
123                 if (id == st->codec->codec_id)
124                     return 1;
125             }
126             if (avctag->id == st->codec->codec_id)
127                 tag = avctag->tag;
128             avctag++;
129         }
130     }
131     if (id != AV_CODEC_ID_NONE)
132         return 0;
133     if (tag && (st->codec->strict_std_compliance >= FF_COMPLIANCE_NORMAL))
134         return 0;
135     return 1;
136 }
137
138
139 static int init_muxer(AVFormatContext *s, AVDictionary **options)
140 {
141     int ret = 0, i;
142     AVStream *st;
143     AVDictionary *tmp = NULL;
144     AVCodecContext *codec = NULL;
145     AVOutputFormat *of = s->oformat;
146
147     if (options)
148         av_dict_copy(&tmp, *options, 0);
149
150     if ((ret = av_opt_set_dict(s, &tmp)) < 0)
151         goto fail;
152
153     // some sanity checks
154     if (s->nb_streams == 0 && !(of->flags & AVFMT_NOSTREAMS)) {
155         av_log(s, AV_LOG_ERROR, "no streams\n");
156         ret = AVERROR(EINVAL);
157         goto fail;
158     }
159
160     for (i = 0; i < s->nb_streams; i++) {
161         st    = s->streams[i];
162         codec = st->codec;
163
164         switch (codec->codec_type) {
165         case AVMEDIA_TYPE_AUDIO:
166             if (codec->sample_rate <= 0) {
167                 av_log(s, AV_LOG_ERROR, "sample rate not set\n");
168                 ret = AVERROR(EINVAL);
169                 goto fail;
170             }
171             if (!codec->block_align)
172                 codec->block_align = codec->channels *
173                                      av_get_bits_per_sample(codec->codec_id) >> 3;
174             break;
175         case AVMEDIA_TYPE_VIDEO:
176             if (codec->time_base.num <= 0 ||
177                 codec->time_base.den <= 0) { //FIXME audio too?
178                 av_log(s, AV_LOG_ERROR, "time base not set\n");
179                 ret = AVERROR(EINVAL);
180                 goto fail;
181             }
182
183             if ((codec->width <= 0 || codec->height <= 0) &&
184                 !(of->flags & AVFMT_NODIMENSIONS)) {
185                 av_log(s, AV_LOG_ERROR, "dimensions not set\n");
186                 ret = AVERROR(EINVAL);
187                 goto fail;
188             }
189
190             if (av_cmp_q(st->sample_aspect_ratio,
191                          codec->sample_aspect_ratio)) {
192                 av_log(s, AV_LOG_ERROR, "Aspect ratio mismatch between muxer "
193                                         "(%d/%d) and encoder layer (%d/%d)\n",
194                        st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
195                        codec->sample_aspect_ratio.num,
196                        codec->sample_aspect_ratio.den);
197                 ret = AVERROR(EINVAL);
198                 goto fail;
199             }
200             break;
201         }
202
203         if (of->codec_tag) {
204             if (codec->codec_tag &&
205                 codec->codec_id == AV_CODEC_ID_RAWVIDEO &&
206                 !av_codec_get_tag(of->codec_tag, codec->codec_id) &&
207                 !validate_codec_tag(s, st)) {
208                 // the current rawvideo encoding system ends up setting
209                 // the wrong codec_tag for avi, we override it here
210                 codec->codec_tag = 0;
211             }
212             if (codec->codec_tag) {
213                 if (!validate_codec_tag(s, st)) {
214                     char tagbuf[32];
215                     av_get_codec_tag_string(tagbuf, sizeof(tagbuf), codec->codec_tag);
216                     av_log(s, AV_LOG_ERROR,
217                            "Tag %s/0x%08x incompatible with output codec id '%d'\n",
218                            tagbuf, codec->codec_tag, codec->codec_id);
219                     ret = AVERROR_INVALIDDATA;
220                     goto fail;
221                 }
222             } else
223                 codec->codec_tag = av_codec_get_tag(of->codec_tag, codec->codec_id);
224         }
225
226         if (of->flags & AVFMT_GLOBALHEADER &&
227             !(codec->flags & CODEC_FLAG_GLOBAL_HEADER))
228             av_log(s, AV_LOG_WARNING,
229                    "Codec for stream %d does not use global headers "
230                    "but container format requires global headers\n", i);
231     }
232
233     if (!s->priv_data && of->priv_data_size > 0) {
234         s->priv_data = av_mallocz(of->priv_data_size);
235         if (!s->priv_data) {
236             ret = AVERROR(ENOMEM);
237             goto fail;
238         }
239         if (of->priv_class) {
240             *(const AVClass **)s->priv_data = of->priv_class;
241             av_opt_set_defaults(s->priv_data);
242             if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
243                 goto fail;
244         }
245     }
246
247     /* set muxer identification string */
248     if (s->nb_streams && !(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
249         av_dict_set(&s->metadata, "encoder", LIBAVFORMAT_IDENT, 0);
250     }
251
252     if (options) {
253          av_dict_free(options);
254          *options = tmp;
255     }
256
257     return 0;
258
259 fail:
260     av_dict_free(&tmp);
261     return ret;
262 }
263
264 static int init_pts(AVFormatContext *s)
265 {
266     int i;
267     AVStream *st;
268
269     /* init PTS generation */
270     for (i = 0; i < s->nb_streams; i++) {
271         int64_t den = AV_NOPTS_VALUE;
272         st = s->streams[i];
273
274         switch (st->codec->codec_type) {
275         case AVMEDIA_TYPE_AUDIO:
276             den = (int64_t)st->time_base.num * st->codec->sample_rate;
277             break;
278         case AVMEDIA_TYPE_VIDEO:
279             den = (int64_t)st->time_base.num * st->codec->time_base.den;
280             break;
281         default:
282             break;
283         }
284         if (den != AV_NOPTS_VALUE) {
285             if (den <= 0)
286                 return AVERROR_INVALIDDATA;
287
288             frac_init(&st->pts, 0, 0, den);
289         }
290     }
291
292     return 0;
293 }
294
295 int avformat_write_header(AVFormatContext *s, AVDictionary **options)
296 {
297     int ret = 0;
298
299     if (ret = init_muxer(s, options))
300         return ret;
301
302     if (s->oformat->write_header) {
303         ret = s->oformat->write_header(s);
304         if (ret < 0)
305             return ret;
306     }
307
308     if ((ret = init_pts(s)) < 0)
309         return ret;
310
311     return 0;
312 }
313
314 //FIXME merge with compute_pkt_fields
315 static int compute_pkt_fields2(AVFormatContext *s, AVStream *st, AVPacket *pkt)
316 {
317     int delay = FFMAX(st->codec->has_b_frames, !!st->codec->max_b_frames);
318     int num, den, frame_size, i;
319
320     av_dlog(s, "compute_pkt_fields2: pts:%" PRId64 " dts:%" PRId64 " cur_dts:%" PRId64 " b:%d size:%d st:%d\n",
321             pkt->pts, pkt->dts, st->cur_dts, delay, pkt->size, pkt->stream_index);
322
323 /*    if(pkt->pts == AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE)
324  *      return AVERROR(EINVAL);*/
325
326     /* duration field */
327     if (pkt->duration == 0) {
328         ff_compute_frame_duration(&num, &den, st, NULL, pkt);
329         if (den && num) {
330             pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den * st->codec->ticks_per_frame, den * (int64_t)st->time_base.num);
331         }
332     }
333
334     if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && delay == 0)
335         pkt->pts = pkt->dts;
336
337     //XXX/FIXME this is a temporary hack until all encoders output pts
338     if ((pkt->pts == 0 || pkt->pts == AV_NOPTS_VALUE) && pkt->dts == AV_NOPTS_VALUE && !delay) {
339         pkt->dts =
340 //        pkt->pts= st->cur_dts;
341             pkt->pts = st->pts.val;
342     }
343
344     //calculate dts from pts
345     if (pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
346         st->pts_buffer[0] = pkt->pts;
347         for (i = 1; i < delay + 1 && st->pts_buffer[i] == AV_NOPTS_VALUE; i++)
348             st->pts_buffer[i] = pkt->pts + (i - delay - 1) * pkt->duration;
349         for (i = 0; i<delay && st->pts_buffer[i] > st->pts_buffer[i + 1]; i++)
350             FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i + 1]);
351
352         pkt->dts = st->pts_buffer[0];
353     }
354
355     if (st->cur_dts && st->cur_dts != AV_NOPTS_VALUE &&
356         ((!(s->oformat->flags & AVFMT_TS_NONSTRICT) &&
357           st->cur_dts >= pkt->dts) || st->cur_dts > pkt->dts)) {
358         av_log(s, AV_LOG_ERROR,
359                "Application provided invalid, non monotonically increasing dts to muxer in stream %d: %" PRId64 " >= %" PRId64 "\n",
360                st->index, st->cur_dts, pkt->dts);
361         return AVERROR(EINVAL);
362     }
363     if (pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts) {
364         av_log(s, AV_LOG_ERROR, "pts < dts in stream %d\n", st->index);
365         return AVERROR(EINVAL);
366     }
367
368     av_dlog(s, "av_write_frame: pts2:%"PRId64" dts2:%"PRId64"\n",
369             pkt->pts, pkt->dts);
370     st->cur_dts = pkt->dts;
371     st->pts.val = pkt->dts;
372
373     /* update pts */
374     switch (st->codec->codec_type) {
375     case AVMEDIA_TYPE_AUDIO:
376         frame_size = ff_get_audio_frame_size(st->codec, pkt->size, 1);
377
378         /* HACK/FIXME, we skip the initial 0 size packets as they are most
379          * likely equal to the encoder delay, but it would be better if we
380          * had the real timestamps from the encoder */
381         if (frame_size >= 0 && (pkt->size || st->pts.num != st->pts.den >> 1 || st->pts.val)) {
382             frac_add(&st->pts, (int64_t)st->time_base.den * frame_size);
383         }
384         break;
385     case AVMEDIA_TYPE_VIDEO:
386         frac_add(&st->pts, (int64_t)st->time_base.den * st->codec->time_base.num);
387         break;
388     default:
389         break;
390     }
391     return 0;
392 }
393
394 int av_write_frame(AVFormatContext *s, AVPacket *pkt)
395 {
396     int ret;
397
398     if (!pkt) {
399         if (s->oformat->flags & AVFMT_ALLOW_FLUSH)
400             return s->oformat->write_packet(s, pkt);
401         return 1;
402     }
403
404     ret = compute_pkt_fields2(s, s->streams[pkt->stream_index], pkt);
405
406     if (ret < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
407         return ret;
408
409     ret = s->oformat->write_packet(s, pkt);
410
411     if (ret >= 0)
412         s->streams[pkt->stream_index]->nb_frames++;
413     return ret;
414 }
415
416 void ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
417                               int (*compare)(AVFormatContext *, AVPacket *, AVPacket *))
418 {
419     AVPacketList **next_point, *this_pktl;
420
421     this_pktl      = av_mallocz(sizeof(AVPacketList));
422     this_pktl->pkt = *pkt;
423     pkt->destruct  = NULL;           // do not free original but only the copy
424     av_dup_packet(&this_pktl->pkt);  // duplicate the packet if it uses non-alloced memory
425
426     if (s->streams[pkt->stream_index]->last_in_packet_buffer) {
427         next_point = &(s->streams[pkt->stream_index]->last_in_packet_buffer->next);
428     } else
429         next_point = &s->packet_buffer;
430
431     if (*next_point) {
432         if (compare(s, &s->packet_buffer_end->pkt, pkt)) {
433             while (!compare(s, &(*next_point)->pkt, pkt))
434                 next_point = &(*next_point)->next;
435             goto next_non_null;
436         } else {
437             next_point = &(s->packet_buffer_end->next);
438         }
439     }
440     assert(!*next_point);
441
442     s->packet_buffer_end = this_pktl;
443 next_non_null:
444
445     this_pktl->next = *next_point;
446
447     s->streams[pkt->stream_index]->last_in_packet_buffer =
448         *next_point                                      = this_pktl;
449 }
450
451 static int ff_interleave_compare_dts(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
452 {
453     AVStream *st  = s->streams[pkt->stream_index];
454     AVStream *st2 = s->streams[next->stream_index];
455     int comp      = av_compare_ts(next->dts, st2->time_base, pkt->dts,
456                                   st->time_base);
457
458     if (comp == 0)
459         return pkt->stream_index < next->stream_index;
460     return comp > 0;
461 }
462
463 int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
464                                  AVPacket *pkt, int flush)
465 {
466     AVPacketList *pktl;
467     int stream_count = 0;
468     int i;
469
470     if (pkt) {
471         ff_interleave_add_packet(s, pkt, ff_interleave_compare_dts);
472     }
473
474     for (i = 0; i < s->nb_streams; i++)
475         stream_count += !!s->streams[i]->last_in_packet_buffer;
476
477     if (stream_count && (s->nb_streams == stream_count || flush)) {
478         pktl = s->packet_buffer;
479         *out = pktl->pkt;
480
481         s->packet_buffer = pktl->next;
482         if (!s->packet_buffer)
483             s->packet_buffer_end = NULL;
484
485         if (s->streams[out->stream_index]->last_in_packet_buffer == pktl)
486             s->streams[out->stream_index]->last_in_packet_buffer = NULL;
487         av_freep(&pktl);
488         return 1;
489     } else {
490         av_init_packet(out);
491         return 0;
492     }
493 }
494
495 #if FF_API_INTERLEAVE_PACKET
496 int av_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
497                                  AVPacket *pkt, int flush)
498 {
499     return ff_interleave_packet_per_dts(s, out, pkt, flush);
500 }
501
502 #endif
503
504 /**
505  * Interleave an AVPacket correctly so it can be muxed.
506  * @param out the interleaved packet will be output here
507  * @param in the input packet
508  * @param flush 1 if no further packets are available as input and all
509  *              remaining packets should be output
510  * @return 1 if a packet was output, 0 if no packet could be output,
511  *         < 0 if an error occurred
512  */
513 static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush)
514 {
515     if (s->oformat->interleave_packet) {
516         int ret = s->oformat->interleave_packet(s, out, in, flush);
517         if (in)
518             av_free_packet(in);
519         return ret;
520     } else
521         return ff_interleave_packet_per_dts(s, out, in, flush);
522 }
523
524 int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
525 {
526     int ret, flush = 0;
527
528     if (pkt) {
529         AVStream *st = s->streams[pkt->stream_index];
530
531         //FIXME/XXX/HACK drop zero sized packets
532         if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && pkt->size == 0)
533             return 0;
534
535         av_dlog(s, "av_interleaved_write_frame size:%d dts:%" PRId64 " pts:%" PRId64 "\n",
536                 pkt->size, pkt->dts, pkt->pts);
537         if ((ret = compute_pkt_fields2(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
538             return ret;
539
540         if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
541             return AVERROR(EINVAL);
542     } else {
543         av_dlog(s, "av_interleaved_write_frame FLUSH\n");
544         flush = 1;
545     }
546
547     for (;; ) {
548         AVPacket opkt;
549         int ret = interleave_packet(s, &opkt, pkt, flush);
550         if (ret <= 0) //FIXME cleanup needed for ret<0 ?
551             return ret;
552
553         ret = s->oformat->write_packet(s, &opkt);
554         if (ret >= 0)
555             s->streams[opkt.stream_index]->nb_frames++;
556
557         av_free_packet(&opkt);
558         pkt = NULL;
559
560         if (ret < 0)
561             return ret;
562     }
563 }
564
565 int av_write_trailer(AVFormatContext *s)
566 {
567     int ret, i;
568
569     for (;; ) {
570         AVPacket pkt;
571         ret = interleave_packet(s, &pkt, NULL, 1);
572         if (ret < 0) //FIXME cleanup needed for ret<0 ?
573             goto fail;
574         if (!ret)
575             break;
576
577         ret = s->oformat->write_packet(s, &pkt);
578         if (ret >= 0)
579             s->streams[pkt.stream_index]->nb_frames++;
580
581         av_free_packet(&pkt);
582
583         if (ret < 0)
584             goto fail;
585     }
586
587     if (s->oformat->write_trailer)
588         ret = s->oformat->write_trailer(s);
589
590     if (!(s->oformat->flags & AVFMT_NOFILE))
591         avio_flush(s->pb);
592
593 fail:
594     for (i = 0; i < s->nb_streams; i++) {
595         av_freep(&s->streams[i]->priv_data);
596         av_freep(&s->streams[i]->index_entries);
597     }
598     if (s->oformat->priv_class)
599         av_opt_free(s->priv_data);
600     av_freep(&s->priv_data);
601     return ret;
602 }