2 * muxing functions for use within Libav
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
5 * This file is part of Libav.
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.
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.
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
25 #include "avio_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"
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"
40 #include "audiointerleave.h"
52 * muxing functions for use within Libav
55 /* fraction handling */
58 * f = val + (num / den) + 0.5.
60 * 'num' is normalized so that it is such as 0 <= num < den.
62 * @param f fractional number
63 * @param val integer value
64 * @param num must be >= 0
65 * @param den must be >= 1
67 static void frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den)
80 * Fractional addition to f: f = f + (incr / f->den).
82 * @param f fractional number
83 * @param incr increment, can be positive or negative
85 static void frac_add(AVFrac *f, int64_t incr)
98 } else if (num >= den) {
105 static int validate_codec_tag(AVFormatContext *s, AVStream *st)
107 const AVCodecTag *avctag;
109 enum AVCodecID id = AV_CODEC_ID_NONE;
110 unsigned int tag = 0;
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
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)) {
123 if (id == st->codec->codec_id)
126 if (avctag->id == st->codec->codec_id)
131 if (id != AV_CODEC_ID_NONE)
133 if (tag && (st->codec->strict_std_compliance >= FF_COMPLIANCE_NORMAL))
139 static int init_muxer(AVFormatContext *s, AVDictionary **options)
143 AVDictionary *tmp = NULL;
144 AVCodecContext *codec = NULL;
145 AVOutputFormat *of = s->oformat;
148 av_dict_copy(&tmp, *options, 0);
150 if ((ret = av_opt_set_dict(s, &tmp)) < 0)
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);
160 for (i = 0; i < s->nb_streams; i++) {
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);
171 if (!codec->block_align)
172 codec->block_align = codec->channels *
173 av_get_bits_per_sample(codec->codec_id) >> 3;
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);
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);
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);
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;
212 if (codec->codec_tag) {
213 if (!validate_codec_tag(s, st)) {
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;
223 codec->codec_tag = av_codec_get_tag(of->codec_tag, codec->codec_id);
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);
233 if (!s->priv_data && of->priv_data_size > 0) {
234 s->priv_data = av_mallocz(of->priv_data_size);
236 ret = AVERROR(ENOMEM);
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)
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);
253 av_dict_free(options);
264 static int init_pts(AVFormatContext *s)
269 /* init PTS generation */
270 for (i = 0; i < s->nb_streams; i++) {
271 int64_t den = AV_NOPTS_VALUE;
274 switch (st->codec->codec_type) {
275 case AVMEDIA_TYPE_AUDIO:
276 den = (int64_t)st->time_base.num * st->codec->sample_rate;
278 case AVMEDIA_TYPE_VIDEO:
279 den = (int64_t)st->time_base.num * st->codec->time_base.den;
284 if (den != AV_NOPTS_VALUE) {
286 return AVERROR_INVALIDDATA;
288 frac_init(&st->pts, 0, 0, den);
295 int avformat_write_header(AVFormatContext *s, AVDictionary **options)
299 if (ret = init_muxer(s, options))
302 if (s->oformat->write_header) {
303 ret = s->oformat->write_header(s);
308 if ((ret = init_pts(s) < 0))
314 //FIXME merge with compute_pkt_fields
315 static int compute_pkt_fields2(AVFormatContext *s, AVStream *st, AVPacket *pkt)
317 int delay = FFMAX(st->codec->has_b_frames, !!st->codec->max_b_frames);
318 int num, den, frame_size, i;
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);
323 /* if(pkt->pts == AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE)
324 * return AVERROR(EINVAL);*/
327 if (pkt->duration == 0) {
328 ff_compute_frame_duration(&num, &den, st, NULL, pkt);
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);
334 if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && delay == 0)
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) {
340 // pkt->pts= st->cur_dts;
341 pkt->pts = st->pts.val;
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]);
352 pkt->dts = st->pts_buffer[0];
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);
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);
368 av_dlog(s, "av_write_frame: pts2:%"PRId64" dts2:%"PRId64"\n",
370 st->cur_dts = pkt->dts;
371 st->pts.val = pkt->dts;
374 switch (st->codec->codec_type) {
375 case AVMEDIA_TYPE_AUDIO:
376 frame_size = ff_get_audio_frame_size(st->codec, pkt->size, 1);
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);
385 case AVMEDIA_TYPE_VIDEO:
386 frac_add(&st->pts, (int64_t)st->time_base.den * st->codec->time_base.num);
394 int av_write_frame(AVFormatContext *s, AVPacket *pkt)
399 if (s->oformat->flags & AVFMT_ALLOW_FLUSH)
400 return s->oformat->write_packet(s, pkt);
404 ret = compute_pkt_fields2(s, s->streams[pkt->stream_index], pkt);
406 if (ret < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
409 ret = s->oformat->write_packet(s, pkt);
412 s->streams[pkt->stream_index]->nb_frames++;
416 void ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
417 int (*compare)(AVFormatContext *, AVPacket *, AVPacket *))
419 AVPacketList **next_point, *this_pktl;
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
426 if (s->streams[pkt->stream_index]->last_in_packet_buffer) {
427 next_point = &(s->streams[pkt->stream_index]->last_in_packet_buffer->next);
429 next_point = &s->packet_buffer;
432 if (compare(s, &s->packet_buffer_end->pkt, pkt)) {
433 while (!compare(s, &(*next_point)->pkt, pkt))
434 next_point = &(*next_point)->next;
437 next_point = &(s->packet_buffer_end->next);
440 assert(!*next_point);
442 s->packet_buffer_end = this_pktl;
445 this_pktl->next = *next_point;
447 s->streams[pkt->stream_index]->last_in_packet_buffer =
448 *next_point = this_pktl;
451 static int ff_interleave_compare_dts(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
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,
459 return pkt->stream_index < next->stream_index;
463 int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
464 AVPacket *pkt, int flush)
467 int stream_count = 0;
471 ff_interleave_add_packet(s, pkt, ff_interleave_compare_dts);
474 for (i = 0; i < s->nb_streams; i++)
475 stream_count += !!s->streams[i]->last_in_packet_buffer;
477 if (stream_count && (s->nb_streams == stream_count || flush)) {
478 pktl = s->packet_buffer;
481 s->packet_buffer = pktl->next;
482 if (!s->packet_buffer)
483 s->packet_buffer_end = NULL;
485 if (s->streams[out->stream_index]->last_in_packet_buffer == pktl)
486 s->streams[out->stream_index]->last_in_packet_buffer = NULL;
495 #if FF_API_INTERLEAVE_PACKET
496 int av_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
497 AVPacket *pkt, int flush)
499 return ff_interleave_packet_per_dts(s, out, pkt, flush);
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
513 static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush)
515 if (s->oformat->interleave_packet) {
516 int ret = s->oformat->interleave_packet(s, out, in, flush);
521 return ff_interleave_packet_per_dts(s, out, in, flush);
524 int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
529 AVStream *st = s->streams[pkt->stream_index];
531 //FIXME/XXX/HACK drop zero sized packets
532 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && pkt->size == 0)
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))
540 if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
541 return AVERROR(EINVAL);
543 av_dlog(s, "av_interleaved_write_frame FLUSH\n");
549 int ret = interleave_packet(s, &opkt, pkt, flush);
550 if (ret <= 0) //FIXME cleanup needed for ret<0 ?
553 ret = s->oformat->write_packet(s, &opkt);
555 s->streams[opkt.stream_index]->nb_frames++;
557 av_free_packet(&opkt);
565 int av_write_trailer(AVFormatContext *s)
571 ret = interleave_packet(s, &pkt, NULL, 1);
572 if (ret < 0) //FIXME cleanup needed for ret<0 ?
577 ret = s->oformat->write_packet(s, &pkt);
579 s->streams[pkt.stream_index]->nb_frames++;
581 av_free_packet(&pkt);
587 if (s->oformat->write_trailer)
588 ret = s->oformat->write_trailer(s);
590 if (!(s->oformat->flags & AVFMT_NOFILE))
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);
598 if (s->oformat->priv_class)
599 av_opt_free(s->priv_data);
600 av_freep(&s->priv_data);