mux.c 26.1 KB
Newer Older
1
/*
2
 * muxing functions for use within FFmpeg
3 4
 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 9 10 11
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19 20 21 22 23 24 25 26 27 28 29 30 31
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/* #define DEBUG */

#include "avformat.h"
#include "avio_internal.h"
#include "internal.h"
#include "libavcodec/internal.h"
#include "libavcodec/bytestream.h"
#include "libavutil/opt.h"
#include "libavutil/dict.h"
#include "libavutil/pixdesc.h"
32
#include "libavutil/timestamp.h"
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include "metadata.h"
#include "id3v2.h"
#include "libavutil/avassert.h"
#include "libavutil/avstring.h"
#include "libavutil/mathematics.h"
#include "libavutil/parseutils.h"
#include "libavutil/time.h"
#include "riff.h"
#include "audiointerleave.h"
#include "url.h"
#include <stdarg.h>
#if CONFIG_NETWORK
#include "network.h"
#endif

#undef NDEBUG
#include <assert.h>

/**
 * @file
53
 * muxing functions for use within libavformat
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
 */

/* fraction handling */

/**
 * f = val + (num / den) + 0.5.
 *
 * 'num' is normalized so that it is such as 0 <= num < den.
 *
 * @param f fractional number
 * @param val integer value
 * @param num must be >= 0
 * @param den must be >= 1
 */
static void frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den)
{
    num += (den >> 1);
    if (num >= den) {
        val += num / den;
        num  = num % den;
    }
    f->val = val;
    f->num = num;
    f->den = den;
}

/**
 * Fractional addition to f: f = f + (incr / f->den).
 *
 * @param f fractional number
 * @param incr increment, can be positive or negative
 */
static void frac_add(AVFrac *f, int64_t incr)
{
    int64_t num, den;

    num = f->num + incr;
    den = f->den;
    if (num < 0) {
        f->val += num / den;
        num     = num % den;
        if (num < 0) {
            num += den;
            f->val--;
        }
    } else if (num >= den) {
        f->val += num / den;
        num     = num % den;
    }
    f->num = num;
}

106 107 108 109 110 111 112 113 114 115
AVRational ff_choose_timebase(AVFormatContext *s, AVStream *st, int min_precission)
{
    AVRational q;
    int j;

    if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
        q = (AVRational){1, st->codec->sample_rate};
    } else {
        q = st->codec->time_base;
    }
116
    for (j=2; j<14; j+= 1+(j>2))
117 118 119 120 121 122 123 124
        while (q.den / q.num < min_precission && q.num % j == 0)
            q.num /= j;
    while (q.den / q.num < min_precission && q.den < (1<<24))
        q.den <<= 1;

    return q;
}

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
int avformat_alloc_output_context2(AVFormatContext **avctx, AVOutputFormat *oformat,
                                   const char *format, const char *filename)
{
    AVFormatContext *s = avformat_alloc_context();
    int ret = 0;

    *avctx = NULL;
    if (!s)
        goto nomem;

    if (!oformat) {
        if (format) {
            oformat = av_guess_format(format, NULL, NULL);
            if (!oformat) {
                av_log(s, AV_LOG_ERROR, "Requested output format '%s' is not a suitable output format\n", format);
                ret = AVERROR(EINVAL);
                goto error;
            }
        } else {
            oformat = av_guess_format(NULL, filename, NULL);
            if (!oformat) {
                ret = AVERROR(EINVAL);
                av_log(s, AV_LOG_ERROR, "Unable to find a suitable output format for '%s'\n",
                       filename);
                goto error;
            }
        }
    }

    s->oformat = oformat;
    if (s->oformat->priv_data_size > 0) {
        s->priv_data = av_mallocz(s->oformat->priv_data_size);
        if (!s->priv_data)
            goto nomem;
        if (s->oformat->priv_class) {
            *(const AVClass**)s->priv_data= s->oformat->priv_class;
            av_opt_set_defaults(s->priv_data);
        }
    } else
        s->priv_data = NULL;

    if (filename)
        av_strlcpy(s->filename, filename, sizeof(s->filename));
    *avctx = s;
    return 0;
nomem:
    av_log(s, AV_LOG_ERROR, "Out of memory\n");
    ret = AVERROR(ENOMEM);
error:
    avformat_free_context(s);
    return ret;
}

#if FF_API_ALLOC_OUTPUT_CONTEXT
AVFormatContext *avformat_alloc_output_context(const char *format,
                                               AVOutputFormat *oformat, const char *filename)
{
    AVFormatContext *avctx;
    int ret = avformat_alloc_output_context2(&avctx, oformat, format, filename);
    return ret < 0 ? NULL : avctx;
}
#endif

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
static int validate_codec_tag(AVFormatContext *s, AVStream *st)
{
    const AVCodecTag *avctag;
    int n;
    enum AVCodecID id = AV_CODEC_ID_NONE;
    unsigned int tag  = 0;

    /**
     * Check that tag + id is in the table
     * If neither is in the table -> OK
     * If tag is in the table with another id -> FAIL
     * If id is in the table with another tag -> FAIL unless strict < normal
     */
    for (n = 0; s->oformat->codec_tag[n]; n++) {
        avctag = s->oformat->codec_tag[n];
        while (avctag->id != AV_CODEC_ID_NONE) {
            if (avpriv_toupper4(avctag->tag) == avpriv_toupper4(st->codec->codec_tag)) {
                id = avctag->id;
                if (id == st->codec->codec_id)
                    return 1;
            }
            if (avctag->id == st->codec->codec_id)
                tag = avctag->tag;
            avctag++;
        }
    }
    if (id != AV_CODEC_ID_NONE)
        return 0;
    if (tag && (st->codec->strict_std_compliance >= FF_COMPLIANCE_NORMAL))
        return 0;
    return 1;
}

221 222

static int init_muxer(AVFormatContext *s, AVDictionary **options)
223 224 225 226
{
    int ret = 0, i;
    AVStream *st;
    AVDictionary *tmp = NULL;
227 228
    AVCodecContext *codec = NULL;
    AVOutputFormat *of = s->oformat;
229 230 231

    if (options)
        av_dict_copy(&tmp, *options, 0);
232

233 234
    if ((ret = av_opt_set_dict(s, &tmp)) < 0)
        goto fail;
235 236 237
    if (s->priv_data && s->oformat->priv_class && *(const AVClass**)s->priv_data==s->oformat->priv_class &&
        (ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
        goto fail;
238 239

    // some sanity checks
240
    if (s->nb_streams == 0 && !(of->flags & AVFMT_NOSTREAMS)) {
241 242 243 244 245 246
        av_log(s, AV_LOG_ERROR, "no streams\n");
        ret = AVERROR(EINVAL);
        goto fail;
    }

    for (i = 0; i < s->nb_streams; i++) {
247 248
        st    = s->streams[i];
        codec = st->codec;
249

250
        switch (codec->codec_type) {
251
        case AVMEDIA_TYPE_AUDIO:
252
            if (codec->sample_rate <= 0) {
253 254 255 256
                av_log(s, AV_LOG_ERROR, "sample rate not set\n");
                ret = AVERROR(EINVAL);
                goto fail;
            }
257 258 259
            if (!codec->block_align)
                codec->block_align = codec->channels *
                                     av_get_bits_per_sample(codec->codec_id) >> 3;
260 261
            break;
        case AVMEDIA_TYPE_VIDEO:
262 263
            if (codec->time_base.num <= 0 ||
                codec->time_base.den <= 0) { //FIXME audio too?
264 265 266 267
                av_log(s, AV_LOG_ERROR, "time base not set\n");
                ret = AVERROR(EINVAL);
                goto fail;
            }
268 269 270

            if ((codec->width <= 0 || codec->height <= 0) &&
                !(of->flags & AVFMT_NODIMENSIONS)) {
271 272 273 274
                av_log(s, AV_LOG_ERROR, "dimensions not set\n");
                ret = AVERROR(EINVAL);
                goto fail;
            }
275 276
            if (av_cmp_q(st->sample_aspect_ratio, codec->sample_aspect_ratio)
                && FFABS(av_q2d(st->sample_aspect_ratio) - av_q2d(codec->sample_aspect_ratio)) > 0.004*av_q2d(st->sample_aspect_ratio)
277
            ) {
278 279 280
                av_log(s, AV_LOG_ERROR, "Aspect ratio mismatch between muxer "
                                        "(%d/%d) and encoder layer (%d/%d)\n",
                       st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
281 282
                       codec->sample_aspect_ratio.num,
                       codec->sample_aspect_ratio.den);
283 284 285 286 287 288
                ret = AVERROR(EINVAL);
                goto fail;
            }
            break;
        }

289
        if (of->codec_tag) {
290 291 292 293 294
            if (   codec->codec_tag
                && codec->codec_id == AV_CODEC_ID_RAWVIDEO
                && (   av_codec_get_tag(of->codec_tag, codec->codec_id) == 0
                    || av_codec_get_tag(of->codec_tag, codec->codec_id) == MKTAG('r', 'a', 'w', ' '))
                && !validate_codec_tag(s, st)) {
295
                // the current rawvideo encoding system ends up setting
296
                // the wrong codec_tag for avi/mov, we override it here
297
                codec->codec_tag = 0;
298
            }
299
            if (codec->codec_tag) {
300
                if (!validate_codec_tag(s, st)) {
301
                    char tagbuf[32], cortag[32];
302
                    av_get_codec_tag_string(tagbuf, sizeof(tagbuf), codec->codec_tag);
303
                    av_get_codec_tag_string(cortag, sizeof(cortag), av_codec_get_tag(s->oformat->codec_tag, codec->codec_id));
304
                    av_log(s, AV_LOG_ERROR,
305
                           "Tag %s/0x%08x incompatible with output codec id '%d' (%s)\n",
306
                           tagbuf, codec->codec_tag, codec->codec_id, cortag);
307 308 309 310
                    ret = AVERROR_INVALIDDATA;
                    goto fail;
                }
            } else
311
                codec->codec_tag = av_codec_get_tag(of->codec_tag, codec->codec_id);
312 313
        }

314 315 316 317 318
        if (of->flags & AVFMT_GLOBALHEADER &&
            !(codec->flags & CODEC_FLAG_GLOBAL_HEADER))
            av_log(s, AV_LOG_WARNING,
                   "Codec for stream %d does not use global headers "
                   "but container format requires global headers\n", i);
319 320
    }

321 322
    if (!s->priv_data && of->priv_data_size > 0) {
        s->priv_data = av_mallocz(of->priv_data_size);
323 324 325 326
        if (!s->priv_data) {
            ret = AVERROR(ENOMEM);
            goto fail;
        }
327 328
        if (of->priv_class) {
            *(const AVClass **)s->priv_data = of->priv_class;
329 330 331 332 333 334 335 336 337 338 339
            av_opt_set_defaults(s->priv_data);
            if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
                goto fail;
        }
    }

    /* set muxer identification string */
    if (s->nb_streams && !(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
        av_dict_set(&s->metadata, "encoder", LIBAVFORMAT_IDENT, 0);
    }

340 341 342
    if (options) {
         av_dict_free(options);
         *options = tmp;
343 344
    }

345 346 347 348 349 350 351 352 353 354 355 356
    return 0;

fail:
    av_dict_free(&tmp);
    return ret;
}

static int init_pts(AVFormatContext *s)
{
    int i;
    AVStream *st;

357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
    /* init PTS generation */
    for (i = 0; i < s->nb_streams; i++) {
        int64_t den = AV_NOPTS_VALUE;
        st = s->streams[i];

        switch (st->codec->codec_type) {
        case AVMEDIA_TYPE_AUDIO:
            den = (int64_t)st->time_base.num * st->codec->sample_rate;
            break;
        case AVMEDIA_TYPE_VIDEO:
            den = (int64_t)st->time_base.num * st->codec->time_base.den;
            break;
        default:
            break;
        }
        if (den != AV_NOPTS_VALUE) {
373 374 375
            if (den <= 0)
                return AVERROR_INVALIDDATA;

376 377 378 379
            frac_init(&st->pts, 0, 0, den);
        }
    }

380 381 382 383 384 385 386 387 388 389 390 391
    return 0;
}

int avformat_write_header(AVFormatContext *s, AVDictionary **options)
{
    int ret = 0;

    if (ret = init_muxer(s, options))
        return ret;

    if (s->oformat->write_header) {
        ret = s->oformat->write_header(s);
392 393
        if (ret >= 0 && s->pb && s->pb->error < 0)
            ret = s->pb->error;
394 395
        if (ret < 0)
            return ret;
396
    }
397

398
    if ((ret = init_pts(s)) < 0)
399 400
        return ret;

401 402 403 404 405 406
    return 0;
}

//FIXME merge with compute_pkt_fields
static int compute_pkt_fields2(AVFormatContext *s, AVStream *st, AVPacket *pkt)
{
407
    int delay = FFMAX(st->codec->has_b_frames, st->codec->max_b_frames > 0);
408 409
    int num, den, frame_size, i;

410 411
    av_dlog(s, "compute_pkt_fields2: pts:%s dts:%s cur_dts:%s b:%d size:%d st:%d\n",
            av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(st->cur_dts), delay, pkt->size, pkt->stream_index);
412 413 414 415 416 417 418 419 420 421 422 423 424 425

    /* duration field */
    if (pkt->duration == 0) {
        ff_compute_frame_duration(&num, &den, st, NULL, pkt);
        if (den && num) {
            pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den * st->codec->ticks_per_frame, den * (int64_t)st->time_base.num);
        }
    }

    if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && delay == 0)
        pkt->pts = pkt->dts;

    //XXX/FIXME this is a temporary hack until all encoders output pts
    if ((pkt->pts == 0 || pkt->pts == AV_NOPTS_VALUE) && pkt->dts == AV_NOPTS_VALUE && !delay) {
426 427 428 429 430
        static int warned;
        if (!warned) {
            av_log(s, AV_LOG_WARNING, "Encoder did not produce proper pts, making some up.\n");
            warned = 1;
        }
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
        pkt->dts =
//        pkt->pts= st->cur_dts;
            pkt->pts = st->pts.val;
    }

    //calculate dts from pts
    if (pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
        st->pts_buffer[0] = pkt->pts;
        for (i = 1; i < delay + 1 && st->pts_buffer[i] == AV_NOPTS_VALUE; i++)
            st->pts_buffer[i] = pkt->pts + (i - delay - 1) * pkt->duration;
        for (i = 0; i<delay && st->pts_buffer[i] > st->pts_buffer[i + 1]; i++)
            FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i + 1]);

        pkt->dts = st->pts_buffer[0];
    }

    if (st->cur_dts && st->cur_dts != AV_NOPTS_VALUE &&
        ((!(s->oformat->flags & AVFMT_TS_NONSTRICT) &&
          st->cur_dts >= pkt->dts) || st->cur_dts > pkt->dts)) {
        av_log(s, AV_LOG_ERROR,
451 452
               "Application provided invalid, non monotonically increasing dts to muxer in stream %d: %s >= %s\n",
               st->index, av_ts2str(st->cur_dts), av_ts2str(pkt->dts));
453 454 455
        return AVERROR(EINVAL);
    }
    if (pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts) {
456 457
        av_log(s, AV_LOG_ERROR, "pts (%s) < dts (%s) in stream %d\n",
               av_ts2str(pkt->pts), av_ts2str(pkt->dts), st->index);
458 459 460
        return AVERROR(EINVAL);
    }

461 462
    av_dlog(s, "av_write_frame: pts2:%s dts2:%s\n",
            av_ts2str(pkt->pts), av_ts2str(pkt->dts));
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
    st->cur_dts = pkt->dts;
    st->pts.val = pkt->dts;

    /* update pts */
    switch (st->codec->codec_type) {
    case AVMEDIA_TYPE_AUDIO:
        frame_size = ff_get_audio_frame_size(st->codec, pkt->size, 1);

        /* HACK/FIXME, we skip the initial 0 size packets as they are most
         * likely equal to the encoder delay, but it would be better if we
         * had the real timestamps from the encoder */
        if (frame_size >= 0 && (pkt->size || st->pts.num != st->pts.den >> 1 || st->pts.val)) {
            frac_add(&st->pts, (int64_t)st->time_base.den * frame_size);
        }
        break;
    case AVMEDIA_TYPE_VIDEO:
        frac_add(&st->pts, (int64_t)st->time_base.den * st->codec->time_base.num);
        break;
    default:
        break;
    }
    return 0;
}

487 488 489 490 491 492
/**
 * Move side data from payload to internal struct, call muxer, and restore
 * original packet.
 */
static inline int split_write_packet(AVFormatContext *s, AVPacket *pkt)
{
493
    int ret, did_split;
494

495 496 497 498
    did_split = av_packet_split_side_data(pkt);
    ret = s->oformat->write_packet(s, pkt);
    if (did_split)
        av_packet_merge_side_data(pkt);
499 500 501
    return ret;
}

502 503 504 505 506
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
{
    int ret;

    if (!pkt) {
507
        if (s->oformat->flags & AVFMT_ALLOW_FLUSH) {
508
            ret = s->oformat->write_packet(s, NULL);
509 510 511 512
            if (ret >= 0 && s->pb && s->pb->error < 0)
                ret = s->pb->error;
            return ret;
        }
513 514 515 516 517 518 519 520
        return 1;
    }

    ret = compute_pkt_fields2(s, s->streams[pkt->stream_index], pkt);

    if (ret < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
        return ret;

521
    ret = split_write_packet(s, pkt);
522 523
    if (ret >= 0 && s->pb && s->pb->error < 0)
        ret = s->pb->error;
524 525 526 527 528 529

    if (ret >= 0)
        s->streams[pkt->stream_index]->nb_frames++;
    return ret;
}

530 531 532
#define CHUNK_START 0x1000

int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
533 534 535
                              int (*compare)(AVFormatContext *, AVPacket *, AVPacket *))
{
    AVPacketList **next_point, *this_pktl;
536 537
    AVStream *st   = s->streams[pkt->stream_index];
    int chunked    = s->max_chunk_size || s->max_chunk_duration;
538 539

    this_pktl      = av_mallocz(sizeof(AVPacketList));
540 541
    if (!this_pktl)
        return AVERROR(ENOMEM);
542 543
    this_pktl->pkt = *pkt;
    pkt->destruct  = NULL;           // do not free original but only the copy
544
    av_dup_packet(&this_pktl->pkt);  // duplicate the packet if it uses non-allocated memory
545 546

    if (s->streams[pkt->stream_index]->last_in_packet_buffer) {
547 548
        next_point = &(st->last_in_packet_buffer->next);
    } else {
549
        next_point = &s->packet_buffer;
550
    }
551

552 553
    if (chunked) {
        uint64_t max= av_rescale_q_rnd(s->max_chunk_duration, AV_TIME_BASE_Q, st->time_base, AV_ROUND_UP);
554 555
        st->interleaver_chunk_size     += pkt->size;
        st->interleaver_chunk_duration += pkt->duration;
556 557
        if (   (s->max_chunk_size && st->interleaver_chunk_size > s->max_chunk_size)
            || (max && st->interleaver_chunk_duration           > max)) {
558
            st->interleaver_chunk_size      = 0;
559
            this_pktl->pkt.flags |= CHUNK_START;
560 561 562 563 564 565 566
            if (max && st->interleaver_chunk_duration > max) {
                int64_t syncoffset = (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)*max/2;
                int64_t syncto = av_rescale(pkt->dts + syncoffset, 1, max)*max - syncoffset;

                st->interleaver_chunk_duration += (pkt->dts - syncto)/8 - max;
            } else
                st->interleaver_chunk_duration = 0;
567
        }
568 569 570 571
    }
    if (*next_point) {
        if (chunked && !(this_pktl->pkt.flags & CHUNK_START))
            goto next_non_null;
572

573
        if (compare(s, &s->packet_buffer_end->pkt, pkt)) {
574 575 576
            while (   *next_point
                   && ((chunked && !((*next_point)->pkt.flags&CHUNK_START))
                       || !compare(s, &(*next_point)->pkt, pkt)))
577
                next_point = &(*next_point)->next;
578 579
            if (*next_point)
                goto next_non_null;
580 581 582 583
        } else {
            next_point = &(s->packet_buffer_end->next);
        }
    }
584
    av_assert1(!*next_point);
585 586 587 588 589 590 591 592

    s->packet_buffer_end = this_pktl;
next_non_null:

    this_pktl->next = *next_point;

    s->streams[pkt->stream_index]->last_in_packet_buffer =
        *next_point                                      = this_pktl;
593
    return 0;
594 595 596 597 598 599 600 601
}

static int ff_interleave_compare_dts(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
{
    AVStream *st  = s->streams[pkt->stream_index];
    AVStream *st2 = s->streams[next->stream_index];
    int comp      = av_compare_ts(next->dts, st2->time_base, pkt->dts,
                                  st->time_base);
602 603 604 605 606 607 608 609 610 611
    if (s->audio_preload && ((st->codec->codec_type == AVMEDIA_TYPE_AUDIO) != (st2->codec->codec_type == AVMEDIA_TYPE_AUDIO))) {
        int64_t ts = av_rescale_q(pkt ->dts, st ->time_base, AV_TIME_BASE_Q) - s->audio_preload*(st ->codec->codec_type == AVMEDIA_TYPE_AUDIO);
        int64_t ts2= av_rescale_q(next->dts, st2->time_base, AV_TIME_BASE_Q) - s->audio_preload*(st2->codec->codec_type == AVMEDIA_TYPE_AUDIO);
        if (ts == ts2) {
            ts= ( pkt ->dts* st->time_base.num*AV_TIME_BASE - s->audio_preload*(int64_t)(st ->codec->codec_type == AVMEDIA_TYPE_AUDIO)* st->time_base.den)*st2->time_base.den
               -( next->dts*st2->time_base.num*AV_TIME_BASE - s->audio_preload*(int64_t)(st2->codec->codec_type == AVMEDIA_TYPE_AUDIO)*st2->time_base.den)* st->time_base.den;
            ts2=0;
        }
        comp= (ts>ts2) - (ts<ts2);
    }
612 613 614 615 616 617 618 619 620 621

    if (comp == 0)
        return pkt->stream_index < next->stream_index;
    return comp > 0;
}

int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
                                 AVPacket *pkt, int flush)
{
    AVPacketList *pktl;
622 623 624
    int stream_count = 0, noninterleaved_count = 0;
    int64_t delta_dts_max = 0;
    int i, ret;
625 626

    if (pkt) {
627 628 629
        ret = ff_interleave_add_packet(s, pkt, ff_interleave_compare_dts);
        if (ret < 0)
            return ret;
630 631
    }

632 633 634 635 636 637 638
    for (i = 0; i < s->nb_streams; i++) {
        if (s->streams[i]->last_in_packet_buffer) {
            ++stream_count;
        } else if (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
            ++noninterleaved_count;
        }
    }
639

640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
    if (s->nb_streams == stream_count) {
        flush = 1;
    } else if (!flush) {
        for (i=0; i < s->nb_streams; i++) {
            if (s->streams[i]->last_in_packet_buffer) {
                int64_t delta_dts =
                    av_rescale_q(s->streams[i]->last_in_packet_buffer->pkt.dts,
                                s->streams[i]->time_base,
                                AV_TIME_BASE_Q) -
                    av_rescale_q(s->packet_buffer->pkt.dts,
                                s->streams[s->packet_buffer->pkt.stream_index]->time_base,
                                AV_TIME_BASE_Q);
                delta_dts_max= FFMAX(delta_dts_max, delta_dts);
            }
        }
        if (s->nb_streams == stream_count+noninterleaved_count &&
           delta_dts_max > 20*AV_TIME_BASE) {
            av_log(s, AV_LOG_DEBUG, "flushing with %d noninterleaved\n", noninterleaved_count);
            flush = 1;
        }
    }
    if (stream_count && flush) {
        AVStream *st;
663 664
        pktl = s->packet_buffer;
        *out = pktl->pkt;
665
        st   = s->streams[out->stream_index];
666 667 668 669 670

        s->packet_buffer = pktl->next;
        if (!s->packet_buffer)
            s->packet_buffer_end = NULL;

671 672
        if (st->last_in_packet_buffer == pktl)
            st->last_in_packet_buffer = NULL;
673
        av_freep(&pktl);
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691

        if (s->avoid_negative_ts > 0) {
            if (out->dts != AV_NOPTS_VALUE) {
                if (!st->mux_ts_offset && out->dts < 0) {
                    for (i = 0; i < s->nb_streams; i++) {
                        s->streams[i]->mux_ts_offset =
                            av_rescale_q_rnd(-out->dts,
                                             st->time_base,
                                             s->streams[i]->time_base,
                                             AV_ROUND_UP);
                    }
                }
                out->dts += st->mux_ts_offset;
            }
            if (out->pts != AV_NOPTS_VALUE)
                out->pts += st->mux_ts_offset;
        }

692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
        return 1;
    } else {
        av_init_packet(out);
        return 0;
    }
}

#if FF_API_INTERLEAVE_PACKET
int av_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
                                 AVPacket *pkt, int flush)
{
    return ff_interleave_packet_per_dts(s, out, pkt, flush);
}

#endif

/**
 * Interleave an AVPacket correctly so it can be muxed.
 * @param out the interleaved packet will be output here
 * @param in the input packet
 * @param flush 1 if no further packets are available as input and all
 *              remaining packets should be output
 * @return 1 if a packet was output, 0 if no packet could be output,
 *         < 0 if an error occurred
 */
static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush)
{
    if (s->oformat->interleave_packet) {
        int ret = s->oformat->interleave_packet(s, out, in, flush);
        if (in)
            av_free_packet(in);
        return ret;
    } else
        return ff_interleave_packet_per_dts(s, out, in, flush);
}

int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
{
    int ret, flush = 0;

    if (pkt) {
        AVStream *st = s->streams[pkt->stream_index];

        //FIXME/XXX/HACK drop zero sized packets
        if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && pkt->size == 0)
            return 0;

739 740
        av_dlog(s, "av_interleaved_write_frame size:%d dts:%s pts:%s\n",
                pkt->size, av_ts2str(pkt->dts), av_ts2str(pkt->pts));
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
        if ((ret = compute_pkt_fields2(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
            return ret;

        if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
            return AVERROR(EINVAL);
    } else {
        av_dlog(s, "av_interleaved_write_frame FLUSH\n");
        flush = 1;
    }

    for (;; ) {
        AVPacket opkt;
        int ret = interleave_packet(s, &opkt, pkt, flush);
        if (ret <= 0) //FIXME cleanup needed for ret<0 ?
            return ret;

757
        ret = split_write_packet(s, &opkt);
758 759 760 761 762 763 764 765
        if (ret >= 0)
            s->streams[opkt.stream_index]->nb_frames++;

        av_free_packet(&opkt);
        pkt = NULL;

        if (ret < 0)
            return ret;
766 767
        if(s->pb && s->pb->error)
            return s->pb->error;
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
    }
}

int av_write_trailer(AVFormatContext *s)
{
    int ret, i;

    for (;; ) {
        AVPacket pkt;
        ret = interleave_packet(s, &pkt, NULL, 1);
        if (ret < 0) //FIXME cleanup needed for ret<0 ?
            goto fail;
        if (!ret)
            break;

783
        ret = split_write_packet(s, &pkt);
784 785 786 787 788 789 790
        if (ret >= 0)
            s->streams[pkt.stream_index]->nb_frames++;

        av_free_packet(&pkt);

        if (ret < 0)
            goto fail;
791 792
        if(s->pb && s->pb->error)
            goto fail;
793 794 795 796 797 798
    }

    if (s->oformat->write_trailer)
        ret = s->oformat->write_trailer(s);

fail:
799 800 801 802
    if (s->pb)
       avio_flush(s->pb);
    if (ret == 0)
       ret = s->pb ? s->pb->error : 0;
803 804 805 806 807 808 809 810 811
    for (i = 0; i < s->nb_streams; i++) {
        av_freep(&s->streams[i]->priv_data);
        av_freep(&s->streams[i]->index_entries);
    }
    if (s->oformat->priv_class)
        av_opt_free(s->priv_data);
    av_freep(&s->priv_data);
    return ret;
}
812 813 814 815 816 817 818 819

int av_get_output_timestamp(struct AVFormatContext *s, int stream,
                            int64_t *dts, int64_t *wall)
{
    if (!s->oformat || !s->oformat->get_output_timestamp)
        return AVERROR(ENOSYS);
    s->oformat->get_output_timestamp(s, stream, dts, wall);
    return 0;
820
}