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