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))
138 int avformat_write_header(AVFormatContext *s, AVDictionary **options)
142 AVDictionary *tmp = NULL;
145 av_dict_copy(&tmp, *options, 0);
146 if ((ret = av_opt_set_dict(s, &tmp)) < 0)
149 // some sanity checks
150 if (s->nb_streams == 0 && !(s->oformat->flags & AVFMT_NOSTREAMS)) {
151 av_log(s, AV_LOG_ERROR, "no streams\n");
152 ret = AVERROR(EINVAL);
156 for (i = 0; i < s->nb_streams; i++) {
159 switch (st->codec->codec_type) {
160 case AVMEDIA_TYPE_AUDIO:
161 if (st->codec->sample_rate <= 0) {
162 av_log(s, AV_LOG_ERROR, "sample rate not set\n");
163 ret = AVERROR(EINVAL);
166 if (!st->codec->block_align)
167 st->codec->block_align = st->codec->channels *
168 av_get_bits_per_sample(st->codec->codec_id) >> 3;
170 case AVMEDIA_TYPE_VIDEO:
171 if (st->codec->time_base.num <= 0 || st->codec->time_base.den <= 0) { //FIXME audio too?
172 av_log(s, AV_LOG_ERROR, "time base not set\n");
173 ret = AVERROR(EINVAL);
176 if ((st->codec->width <= 0 || st->codec->height <= 0) && !(s->oformat->flags & AVFMT_NODIMENSIONS)) {
177 av_log(s, AV_LOG_ERROR, "dimensions not set\n");
178 ret = AVERROR(EINVAL);
181 if (av_cmp_q(st->sample_aspect_ratio, st->codec->sample_aspect_ratio)) {
182 av_log(s, AV_LOG_ERROR, "Aspect ratio mismatch between muxer "
183 "(%d/%d) and encoder layer (%d/%d)\n",
184 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
185 st->codec->sample_aspect_ratio.num,
186 st->codec->sample_aspect_ratio.den);
187 ret = AVERROR(EINVAL);
193 if (s->oformat->codec_tag) {
194 if (st->codec->codec_tag && st->codec->codec_id == AV_CODEC_ID_RAWVIDEO && av_codec_get_tag(s->oformat->codec_tag, st->codec->codec_id) == 0 && !validate_codec_tag(s, st)) {
195 //the current rawvideo encoding system ends up setting the wrong codec_tag for avi, we override it here
196 st->codec->codec_tag = 0;
198 if (st->codec->codec_tag) {
199 if (!validate_codec_tag(s, st)) {
201 av_get_codec_tag_string(tagbuf, sizeof(tagbuf), st->codec->codec_tag);
202 av_log(s, AV_LOG_ERROR,
203 "Tag %s/0x%08x incompatible with output codec id '%d'\n",
204 tagbuf, st->codec->codec_tag, st->codec->codec_id);
205 ret = AVERROR_INVALIDDATA;
209 st->codec->codec_tag = av_codec_get_tag(s->oformat->codec_tag, st->codec->codec_id);
212 if (s->oformat->flags & AVFMT_GLOBALHEADER &&
213 !(st->codec->flags & CODEC_FLAG_GLOBAL_HEADER))
214 av_log(s, AV_LOG_WARNING, "Codec for stream %d does not use global headers but container format requires global headers\n", i);
217 if (!s->priv_data && s->oformat->priv_data_size > 0) {
218 s->priv_data = av_mallocz(s->oformat->priv_data_size);
220 ret = AVERROR(ENOMEM);
223 if (s->oformat->priv_class) {
224 *(const AVClass **)s->priv_data = s->oformat->priv_class;
225 av_opt_set_defaults(s->priv_data);
226 if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
231 /* set muxer identification string */
232 if (s->nb_streams && !(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
233 av_dict_set(&s->metadata, "encoder", LIBAVFORMAT_IDENT, 0);
236 if (s->oformat->write_header) {
237 ret = s->oformat->write_header(s);
242 /* init PTS generation */
243 for (i = 0; i < s->nb_streams; i++) {
244 int64_t den = AV_NOPTS_VALUE;
247 switch (st->codec->codec_type) {
248 case AVMEDIA_TYPE_AUDIO:
249 den = (int64_t)st->time_base.num * st->codec->sample_rate;
251 case AVMEDIA_TYPE_VIDEO:
252 den = (int64_t)st->time_base.num * st->codec->time_base.den;
257 if (den != AV_NOPTS_VALUE) {
259 ret = AVERROR_INVALIDDATA;
262 frac_init(&st->pts, 0, 0, den);
267 av_dict_free(options);
276 //FIXME merge with compute_pkt_fields
277 static int compute_pkt_fields2(AVFormatContext *s, AVStream *st, AVPacket *pkt)
279 int delay = FFMAX(st->codec->has_b_frames, !!st->codec->max_b_frames);
280 int num, den, frame_size, i;
282 av_dlog(s, "compute_pkt_fields2: pts:%" PRId64 " dts:%" PRId64 " cur_dts:%" PRId64 " b:%d size:%d st:%d\n",
283 pkt->pts, pkt->dts, st->cur_dts, delay, pkt->size, pkt->stream_index);
285 /* if(pkt->pts == AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE)
286 * return AVERROR(EINVAL);*/
289 if (pkt->duration == 0) {
290 ff_compute_frame_duration(&num, &den, st, NULL, pkt);
292 pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den * st->codec->ticks_per_frame, den * (int64_t)st->time_base.num);
296 if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && delay == 0)
299 //XXX/FIXME this is a temporary hack until all encoders output pts
300 if ((pkt->pts == 0 || pkt->pts == AV_NOPTS_VALUE) && pkt->dts == AV_NOPTS_VALUE && !delay) {
302 // pkt->pts= st->cur_dts;
303 pkt->pts = st->pts.val;
306 //calculate dts from pts
307 if (pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
308 st->pts_buffer[0] = pkt->pts;
309 for (i = 1; i < delay + 1 && st->pts_buffer[i] == AV_NOPTS_VALUE; i++)
310 st->pts_buffer[i] = pkt->pts + (i - delay - 1) * pkt->duration;
311 for (i = 0; i<delay && st->pts_buffer[i] > st->pts_buffer[i + 1]; i++)
312 FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i + 1]);
314 pkt->dts = st->pts_buffer[0];
317 if (st->cur_dts && st->cur_dts != AV_NOPTS_VALUE &&
318 ((!(s->oformat->flags & AVFMT_TS_NONSTRICT) &&
319 st->cur_dts >= pkt->dts) || st->cur_dts > pkt->dts)) {
320 av_log(s, AV_LOG_ERROR,
321 "Application provided invalid, non monotonically increasing dts to muxer in stream %d: %" PRId64 " >= %" PRId64 "\n",
322 st->index, st->cur_dts, pkt->dts);
323 return AVERROR(EINVAL);
325 if (pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts) {
326 av_log(s, AV_LOG_ERROR, "pts < dts in stream %d\n", st->index);
327 return AVERROR(EINVAL);
330 av_dlog(s, "av_write_frame: pts2:%"PRId64" dts2:%"PRId64"\n",
332 st->cur_dts = pkt->dts;
333 st->pts.val = pkt->dts;
336 switch (st->codec->codec_type) {
337 case AVMEDIA_TYPE_AUDIO:
338 frame_size = ff_get_audio_frame_size(st->codec, pkt->size, 1);
340 /* HACK/FIXME, we skip the initial 0 size packets as they are most
341 * likely equal to the encoder delay, but it would be better if we
342 * had the real timestamps from the encoder */
343 if (frame_size >= 0 && (pkt->size || st->pts.num != st->pts.den >> 1 || st->pts.val)) {
344 frac_add(&st->pts, (int64_t)st->time_base.den * frame_size);
347 case AVMEDIA_TYPE_VIDEO:
348 frac_add(&st->pts, (int64_t)st->time_base.den * st->codec->time_base.num);
356 int av_write_frame(AVFormatContext *s, AVPacket *pkt)
361 if (s->oformat->flags & AVFMT_ALLOW_FLUSH)
362 return s->oformat->write_packet(s, pkt);
366 ret = compute_pkt_fields2(s, s->streams[pkt->stream_index], pkt);
368 if (ret < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
371 ret = s->oformat->write_packet(s, pkt);
374 s->streams[pkt->stream_index]->nb_frames++;
378 void ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
379 int (*compare)(AVFormatContext *, AVPacket *, AVPacket *))
381 AVPacketList **next_point, *this_pktl;
383 this_pktl = av_mallocz(sizeof(AVPacketList));
384 this_pktl->pkt = *pkt;
385 pkt->destruct = NULL; // do not free original but only the copy
386 av_dup_packet(&this_pktl->pkt); // duplicate the packet if it uses non-alloced memory
388 if (s->streams[pkt->stream_index]->last_in_packet_buffer) {
389 next_point = &(s->streams[pkt->stream_index]->last_in_packet_buffer->next);
391 next_point = &s->packet_buffer;
394 if (compare(s, &s->packet_buffer_end->pkt, pkt)) {
395 while (!compare(s, &(*next_point)->pkt, pkt))
396 next_point = &(*next_point)->next;
399 next_point = &(s->packet_buffer_end->next);
402 assert(!*next_point);
404 s->packet_buffer_end = this_pktl;
407 this_pktl->next = *next_point;
409 s->streams[pkt->stream_index]->last_in_packet_buffer =
410 *next_point = this_pktl;
413 static int ff_interleave_compare_dts(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
415 AVStream *st = s->streams[pkt->stream_index];
416 AVStream *st2 = s->streams[next->stream_index];
417 int comp = av_compare_ts(next->dts, st2->time_base, pkt->dts,
421 return pkt->stream_index < next->stream_index;
425 int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
426 AVPacket *pkt, int flush)
429 int stream_count = 0;
433 ff_interleave_add_packet(s, pkt, ff_interleave_compare_dts);
436 for (i = 0; i < s->nb_streams; i++)
437 stream_count += !!s->streams[i]->last_in_packet_buffer;
439 if (stream_count && (s->nb_streams == stream_count || flush)) {
440 pktl = s->packet_buffer;
443 s->packet_buffer = pktl->next;
444 if (!s->packet_buffer)
445 s->packet_buffer_end = NULL;
447 if (s->streams[out->stream_index]->last_in_packet_buffer == pktl)
448 s->streams[out->stream_index]->last_in_packet_buffer = NULL;
457 #if FF_API_INTERLEAVE_PACKET
458 int av_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
459 AVPacket *pkt, int flush)
461 return ff_interleave_packet_per_dts(s, out, pkt, flush);
467 * Interleave an AVPacket correctly so it can be muxed.
468 * @param out the interleaved packet will be output here
469 * @param in the input packet
470 * @param flush 1 if no further packets are available as input and all
471 * remaining packets should be output
472 * @return 1 if a packet was output, 0 if no packet could be output,
473 * < 0 if an error occurred
475 static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush)
477 if (s->oformat->interleave_packet) {
478 int ret = s->oformat->interleave_packet(s, out, in, flush);
483 return ff_interleave_packet_per_dts(s, out, in, flush);
486 int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
491 AVStream *st = s->streams[pkt->stream_index];
493 //FIXME/XXX/HACK drop zero sized packets
494 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && pkt->size == 0)
497 av_dlog(s, "av_interleaved_write_frame size:%d dts:%" PRId64 " pts:%" PRId64 "\n",
498 pkt->size, pkt->dts, pkt->pts);
499 if ((ret = compute_pkt_fields2(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
502 if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
503 return AVERROR(EINVAL);
505 av_dlog(s, "av_interleaved_write_frame FLUSH\n");
511 int ret = interleave_packet(s, &opkt, pkt, flush);
512 if (ret <= 0) //FIXME cleanup needed for ret<0 ?
515 ret = s->oformat->write_packet(s, &opkt);
517 s->streams[opkt.stream_index]->nb_frames++;
519 av_free_packet(&opkt);
527 int av_write_trailer(AVFormatContext *s)
533 ret = interleave_packet(s, &pkt, NULL, 1);
534 if (ret < 0) //FIXME cleanup needed for ret<0 ?
539 ret = s->oformat->write_packet(s, &pkt);
541 s->streams[pkt.stream_index]->nb_frames++;
543 av_free_packet(&pkt);
549 if (s->oformat->write_trailer)
550 ret = s->oformat->write_trailer(s);
552 if (!(s->oformat->flags & AVFMT_NOFILE))
556 for (i = 0; i < s->nb_streams; i++) {
557 av_freep(&s->streams[i]->priv_data);
558 av_freep(&s->streams[i]->index_entries);
560 if (s->oformat->priv_class)
561 av_opt_free(s->priv_data);
562 av_freep(&s->priv_data);