Merge commit '9b56d5c11488656254e9aed8d964ef2b7c2ff5e6'
[ffmpeg.git] / libavformat / mux.c
index 4a81e36..bf7a472 100644 (file)
@@ -1,21 +1,21 @@
 /*
- * muxing functions for use within Libav
+ * muxing functions for use within FFmpeg
  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  *
- * This file is part of Libav.
+ * This file is part of FFmpeg.
  *
- * Libav is free software; you can redistribute it and/or
+ * FFmpeg is free software; you can redistribute it and/or
  * 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.
  *
- * Libav is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
  * 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
- * License along with Libav; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
@@ -27,6 +27,7 @@
 #include "libavutil/opt.h"
 #include "libavutil/dict.h"
 #include "libavutil/pixdesc.h"
+#include "libavutil/timestamp.h"
 #include "metadata.h"
 #include "id3v2.h"
 #include "libavutil/avassert.h"
 #include "network.h"
 #endif
 
-#undef NDEBUG
-#include <assert.h>
-
 /**
  * @file
- * muxing functions for use within Libav
+ * muxing functions for use within libavformat
+ */
+
+/* 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(FFFrac *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(FFFrac *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;
+}
+
+AVRational ff_choose_timebase(AVFormatContext *s, AVStream *st, int min_precision)
+{
+    AVRational q;
+    int j;
+
+    q = st->time_base;
+
+    for (j=2; j<14; j+= 1+(j>2))
+        while (q.den / q.num < min_precision && q.num % j == 0)
+            q.num /= j;
+    while (q.den / q.num < min_precision && q.den < (1<<24))
+        q.den <<= 1;
+
+    return q;
+}
+
+enum AVChromaLocation ff_choose_chroma_location(AVFormatContext *s, AVStream *st)
+{
+    AVCodecContext *avctx = st->codec;
+    const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(avctx->pix_fmt);
+
+    if (avctx->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
+        return avctx->chroma_sample_location;
+
+    if (pix_desc) {
+        if (pix_desc->log2_chroma_h == 0) {
+            return AVCHROMA_LOC_TOPLEFT;
+        } else if (pix_desc->log2_chroma_w == 1 && pix_desc->log2_chroma_h == 1) {
+            if (avctx->field_order == AV_FIELD_UNKNOWN || avctx->field_order == AV_FIELD_PROGRESSIVE) {
+                switch (avctx->codec_id) {
+                case AV_CODEC_ID_MJPEG:
+                case AV_CODEC_ID_MPEG1VIDEO: return AVCHROMA_LOC_CENTER;
+                }
+            }
+            if (avctx->field_order == AV_FIELD_UNKNOWN || avctx->field_order != AV_FIELD_PROGRESSIVE) {
+                switch (avctx->codec_id) {
+                case AV_CODEC_ID_MPEG2VIDEO: return AVCHROMA_LOC_LEFT;
+                }
+            }
+        }
+    }
+
+    return AVCHROMA_LOC_UNSPECIFIED;
+
+}
+
+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;
+}
 
 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;
+    int64_t tag  = -1;
 
     /**
      * Check that tag + id is in the table
@@ -79,7 +226,7 @@ static int validate_codec_tag(AVFormatContext *s, AVStream *st)
     }
     if (id != AV_CODEC_ID_NONE)
         return 0;
-    if (tag && (s->strict_std_compliance >= FF_COMPLIANCE_NORMAL))
+    if (tag >= 0 && (s->strict_std_compliance >= FF_COMPLIANCE_NORMAL))
         return 0;
     return 1;
 }
@@ -92,21 +239,38 @@ static int init_muxer(AVFormatContext *s, AVDictionary **options)
     AVDictionary *tmp = NULL;
     AVCodecContext *codec = NULL;
     AVOutputFormat *of = s->oformat;
+    AVDictionaryEntry *e;
 
     if (options)
         av_dict_copy(&tmp, *options, 0);
 
     if ((ret = av_opt_set_dict(s, &tmp)) < 0)
         goto fail;
+    if (s->priv_data && s->oformat->priv_class && *(const AVClass**)s->priv_data==s->oformat->priv_class &&
+        (ret = av_opt_set_dict2(s->priv_data, &tmp, AV_OPT_SEARCH_CHILDREN)) < 0)
+        goto fail;
 
+    if (s->nb_streams && s->streams[0]->codec->flags & AV_CODEC_FLAG_BITEXACT) {
+        if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
 #if FF_API_LAVF_BITEXACT
-    if (s->nb_streams && s->streams[0]->codec->flags & AV_CODEC_FLAG_BITEXACT)
-        s->flags |= AVFMT_FLAG_BITEXACT;
+            av_log(s, AV_LOG_WARNING,
+                   "Setting the AVFormatContext to bitexact mode, because "
+                   "the AVCodecContext is in that mode. This behavior will "
+                   "change in the future. To keep the current behavior, set "
+                   "AVFormatContext.flags |= AVFMT_FLAG_BITEXACT.\n");
+            s->flags |= AVFMT_FLAG_BITEXACT;
+#else
+            av_log(s, AV_LOG_WARNING,
+                   "The AVFormatContext is not in set to bitexact mode, only "
+                   "the AVCodecContext. If this is not intended, set "
+                   "AVFormatContext.flags |= AVFMT_FLAG_BITEXACT.\n");
 #endif
+        }
+    }
 
     // some sanity checks
     if (s->nb_streams == 0 && !(of->flags & AVFMT_NOSTREAMS)) {
-        av_log(s, AV_LOG_ERROR, "no streams\n");
+        av_log(s, AV_LOG_ERROR, "No streams to mux were specified\n");
         ret = AVERROR(EINVAL);
         goto fail;
     }
@@ -152,18 +316,18 @@ FF_ENABLE_DEPRECATION_WARNINGS
                 ret = AVERROR(EINVAL);
                 goto fail;
             }
-
-            if (av_cmp_q(st->sample_aspect_ratio,
-                         codec->sample_aspect_ratio)) {
+            if (av_cmp_q(st->sample_aspect_ratio, codec->sample_aspect_ratio)
+                && fabs(av_q2d(st->sample_aspect_ratio) - av_q2d(codec->sample_aspect_ratio)) > 0.004*av_q2d(st->sample_aspect_ratio)
+            ) {
                 if (st->sample_aspect_ratio.num != 0 &&
                     st->sample_aspect_ratio.den != 0 &&
-                    codec->sample_aspect_ratio.den != 0 &&
+                    codec->sample_aspect_ratio.num != 0 &&
                     codec->sample_aspect_ratio.den != 0) {
                     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,
-                            codec->sample_aspect_ratio.num,
-                            codec->sample_aspect_ratio.den);
+                           "(%d/%d) and encoder layer (%d/%d)\n",
+                           st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
+                           codec->sample_aspect_ratio.num,
+                           codec->sample_aspect_ratio.den);
                     ret = AVERROR(EINVAL);
                     goto fail;
                 }
@@ -172,21 +336,23 @@ FF_ENABLE_DEPRECATION_WARNINGS
         }
 
         if (of->codec_tag) {
-            if (codec->codec_tag &&
-                codec->codec_id == AV_CODEC_ID_RAWVIDEO &&
-                !av_codec_get_tag(of->codec_tag, codec->codec_id) &&
-                !validate_codec_tag(s, st)) {
+            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)) {
                 // the current rawvideo encoding system ends up setting
-                // the wrong codec_tag for avi, we override it here
+                // the wrong codec_tag for avi/mov, we override it here
                 codec->codec_tag = 0;
             }
             if (codec->codec_tag) {
                 if (!validate_codec_tag(s, st)) {
-                    char tagbuf[32];
+                    char tagbuf[32], tagbuf2[32];
                     av_get_codec_tag_string(tagbuf, sizeof(tagbuf), codec->codec_tag);
+                    av_get_codec_tag_string(tagbuf2, sizeof(tagbuf2), av_codec_get_tag(s->oformat->codec_tag, codec->codec_id));
                     av_log(s, AV_LOG_ERROR,
-                           "Tag %s/0x%08x incompatible with output codec id '%d'\n",
-                           tagbuf, codec->codec_tag, codec->codec_id);
+                           "Tag %s/0x%08x incompatible with output codec id '%d' (%s)\n",
+                           tagbuf, codec->codec_tag, codec->codec_id, tagbuf2);
                     ret = AVERROR_INVALIDDATA;
                     goto fail;
                 }
@@ -213,7 +379,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
         if (of->priv_class) {
             *(const AVClass **)s->priv_data = of->priv_class;
             av_opt_set_defaults(s->priv_data);
-            if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
+            if ((ret = av_opt_set_dict2(s->priv_data, &tmp, AV_OPT_SEARCH_CHILDREN)) < 0)
                 goto fail;
         }
     }
@@ -221,6 +387,12 @@ FF_ENABLE_DEPRECATION_WARNINGS
     /* set muxer identification string */
     if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
         av_dict_set(&s->metadata, "encoder", LIBAVFORMAT_IDENT, 0);
+    } else {
+        av_dict_set(&s->metadata, "encoder", NULL, 0);
+    }
+
+    for (e = NULL; e = av_dict_get(s->metadata, "encoder-", e, AV_DICT_IGNORE_SUFFIX); ) {
+        av_dict_set(&s->metadata, e->key, NULL, 0);
     }
 
     if (options) {
@@ -235,20 +407,65 @@ fail:
     return ret;
 }
 
+static int init_pts(AVFormatContext *s)
+{
+    int i;
+    AVStream *st;
+
+    /* 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 (!st->priv_pts)
+            st->priv_pts = av_mallocz(sizeof(*st->priv_pts));
+        if (!st->priv_pts)
+            return AVERROR(ENOMEM);
+
+        if (den != AV_NOPTS_VALUE) {
+            if (den <= 0)
+                return AVERROR_INVALIDDATA;
+
+            frac_init(st->priv_pts, 0, 0, den);
+        }
+    }
+
+    return 0;
+}
+
 int avformat_write_header(AVFormatContext *s, AVDictionary **options)
 {
     int ret = 0;
 
-    if (ret = init_muxer(s, options))
+    if ((ret = init_muxer(s, options)) < 0)
         return ret;
 
     if (s->oformat->write_header) {
         ret = s->oformat->write_header(s);
+        if (ret >= 0 && s->pb && s->pb->error < 0)
+            ret = s->pb->error;
         if (ret < 0)
             return ret;
+        if (s->flush_packets && s->pb && s->pb->error >= 0 && s->flags & AVFMT_FLAG_FLUSH_PACKETS)
+            avio_flush(s->pb);
     }
 
-    if (s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_AUTO) {
+    if ((ret = init_pts(s)) < 0)
+        return ret;
+
+    if (s->avoid_negative_ts < 0) {
+        av_assert2(s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_AUTO);
         if (s->oformat->flags & (AVFMT_TS_NEGATIVE | AVFMT_NOTIMESTAMPS)) {
             s->avoid_negative_ts = 0;
         } else
@@ -258,17 +475,31 @@ int avformat_write_header(AVFormatContext *s, AVDictionary **options)
     return 0;
 }
 
+#define AV_PKT_FLAG_UNCODED_FRAME 0x2000
+
+/* Note: using sizeof(AVFrame) from outside lavu is unsafe in general, but
+   it is only being used internally to this file as a consistency check.
+   The value is chosen to be very unlikely to appear on its own and to cause
+   immediate failure if used anywhere as a real size. */
+#define UNCODED_FRAME_PACKET_SIZE (INT_MIN / 3 * 2 + (int)sizeof(AVFrame))
+
+
 //FIXME merge with compute_pkt_fields
 static int compute_pkt_fields2(AVFormatContext *s, AVStream *st, AVPacket *pkt)
 {
-    int delay = FFMAX(st->codec->has_b_frames, !!st->codec->max_b_frames);
+    int delay = FFMAX(st->codec->has_b_frames, st->codec->max_b_frames > 0);
     int num, den, i;
+    int frame_size;
 
-    av_log(s, AV_LOG_TRACE, "compute_pkt_fields2: pts:%" PRId64 " dts:%" PRId64 " cur_dts:%" PRId64 " b:%d size:%d st:%d\n",
-            pkt->pts, pkt->dts, st->cur_dts, delay, pkt->size, pkt->stream_index);
+    if (s->debug & FF_FDEBUG_TS)
+        av_log(s, AV_LOG_TRACE, "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);
 
-/*    if(pkt->pts == AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE)
- *      return AVERROR(EINVAL);*/
+    if (pkt->duration < 0 && st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE) {
+        av_log(s, AV_LOG_WARNING, "Packet with invalid duration %"PRId64" in stream %d\n",
+               pkt->duration, pkt->stream_index);
+        pkt->duration = 0;
+    }
 
     /* duration field */
     if (pkt->duration == 0) {
@@ -281,6 +512,18 @@ static int compute_pkt_fields2(AVFormatContext *s, AVStream *st, AVPacket *pkt)
     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) {
+        static int warned;
+        if (!warned) {
+            av_log(s, AV_LOG_WARNING, "Encoder did not produce proper pts, making some up.\n");
+            warned = 1;
+        }
+        pkt->dts =
+//        pkt->pts= st->cur_dts;
+            pkt->pts = st->priv_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;
@@ -294,71 +537,140 @@ static int compute_pkt_fields2(AVFormatContext *s, AVStream *st, AVPacket *pkt)
 
     if (st->cur_dts && st->cur_dts != AV_NOPTS_VALUE &&
         ((!(s->oformat->flags & AVFMT_TS_NONSTRICT) &&
+          st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE &&
           st->cur_dts >= pkt->dts) || st->cur_dts > pkt->dts)) {
         av_log(s, AV_LOG_ERROR,
-               "Application provided invalid, non monotonically increasing dts to muxer in stream %d: %" PRId64 " >= %" PRId64 "\n",
-               st->index, st->cur_dts, pkt->dts);
+               "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));
         return AVERROR(EINVAL);
     }
     if (pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts) {
         av_log(s, AV_LOG_ERROR,
-               "pts %" PRId64 " < dts %" PRId64 " in stream %d\n",
-               pkt->pts, pkt->dts,
+               "pts (%s) < dts (%s) in stream %d\n",
+               av_ts2str(pkt->pts), av_ts2str(pkt->dts),
                st->index);
         return AVERROR(EINVAL);
     }
 
-    av_log(s, AV_LOG_TRACE, "av_write_frame: pts2:%"PRId64" dts2:%"PRId64"\n",
-            pkt->pts, pkt->dts);
-    st->cur_dts = pkt->dts;
+    if (s->debug & FF_FDEBUG_TS)
+        av_log(s, AV_LOG_TRACE, "av_write_frame: pts2:%s dts2:%s\n",
+            av_ts2str(pkt->pts), av_ts2str(pkt->dts));
 
+    st->cur_dts = pkt->dts;
+    st->priv_pts->val = pkt->dts;
+
+    /* update pts */
+    switch (st->codec->codec_type) {
+    case AVMEDIA_TYPE_AUDIO:
+        frame_size = (pkt->flags & AV_PKT_FLAG_UNCODED_FRAME) ?
+                     ((AVFrame *)pkt->data)->nb_samples :
+                     av_get_audio_frame_duration(st->codec, pkt->size);
+
+        /* 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->priv_pts->num != st->priv_pts->den >> 1 || st->priv_pts->val)) {
+            frac_add(st->priv_pts, (int64_t)st->time_base.den * frame_size);
+        }
+        break;
+    case AVMEDIA_TYPE_VIDEO:
+        frac_add(st->priv_pts, (int64_t)st->time_base.den * st->codec->time_base.num);
+        break;
+    }
     return 0;
 }
 
-/*
+/**
+ * Make timestamps non negative, move side data from payload to internal struct, call muxer, and restore
+ * sidedata.
+ *
  * FIXME: this function should NEVER get undefined pts/dts beside when the
  * AVFMT_NOTIMESTAMPS is set.
  * Those additional safety checks should be dropped once the correct checks
  * are set in the callers.
  */
-
 static int write_packet(AVFormatContext *s, AVPacket *pkt)
 {
-    int ret;
+    int ret, did_split;
+
+    if (s->output_ts_offset) {
+        AVStream *st = s->streams[pkt->stream_index];
+        int64_t offset = av_rescale_q(s->output_ts_offset, AV_TIME_BASE_Q, st->time_base);
+
+        if (pkt->dts != AV_NOPTS_VALUE)
+            pkt->dts += offset;
+        if (pkt->pts != AV_NOPTS_VALUE)
+            pkt->pts += offset;
+    }
+
     if (s->avoid_negative_ts > 0) {
-        AVRational time_base = s->streams[pkt->stream_index]->time_base;
-        int64_t offset = 0;
+        AVStream *st = s->streams[pkt->stream_index];
+        int64_t offset = st->mux_ts_offset;
+        int64_t ts = s->internal->avoid_negative_ts_use_pts ? pkt->pts : pkt->dts;
+
+        if (s->internal->offset == AV_NOPTS_VALUE && ts != AV_NOPTS_VALUE &&
+            (ts < 0 || s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO)) {
+            s->internal->offset = -ts;
+            s->internal->offset_timebase = st->time_base;
+        }
 
-        if (s->internal->offset == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE &&
-            (pkt->dts < 0 || s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO)) {
-            s->internal->offset = -pkt->dts;
-            s->internal->offset_timebase = time_base;
+        if (s->internal->offset != AV_NOPTS_VALUE && !offset) {
+            offset = st->mux_ts_offset =
+                av_rescale_q_rnd(s->internal->offset,
+                                 s->internal->offset_timebase,
+                                 st->time_base,
+                                 AV_ROUND_UP);
         }
-        if (s->internal->offset != AV_NOPTS_VALUE)
-            offset = av_rescale_q(s->internal->offset, s->internal->offset_timebase, time_base);
 
         if (pkt->dts != AV_NOPTS_VALUE)
             pkt->dts += offset;
         if (pkt->pts != AV_NOPTS_VALUE)
             pkt->pts += offset;
 
-        if (pkt->dts != AV_NOPTS_VALUE && pkt->dts < 0) {
-            av_log(s, AV_LOG_WARNING,
-                   "Packets poorly interleaved, failed to avoid negative "
-                   "timestamp %"PRId64" in stream %d.\n"
-                   "Try -max_interleave_delta 0 as a possible workaround.\n",
-                   pkt->dts, pkt->stream_index);
+        if (s->internal->avoid_negative_ts_use_pts) {
+            if (pkt->pts != AV_NOPTS_VALUE && pkt->pts < 0) {
+                av_log(s, AV_LOG_WARNING, "failed to avoid negative "
+                    "pts %s in stream %d.\n"
+                    "Try -avoid_negative_ts 1 as a possible workaround.\n",
+                    av_ts2str(pkt->dts),
+                    pkt->stream_index
+                );
+            }
+        } else {
+            av_assert2(pkt->dts == AV_NOPTS_VALUE || pkt->dts >= 0 || s->max_interleave_delta > 0);
+            if (pkt->dts != AV_NOPTS_VALUE && pkt->dts < 0) {
+                av_log(s, AV_LOG_WARNING,
+                    "Packets poorly interleaved, failed to avoid negative "
+                    "timestamp %s in stream %d.\n"
+                    "Try -max_interleave_delta 0 as a possible workaround.\n",
+                    av_ts2str(pkt->dts),
+                    pkt->stream_index
+                );
+            }
         }
     }
-    ret = s->oformat->write_packet(s, pkt);
+
+    did_split = av_packet_split_side_data(pkt);
+    if ((pkt->flags & AV_PKT_FLAG_UNCODED_FRAME)) {
+        AVFrame *frame = (AVFrame *)pkt->data;
+        av_assert0(pkt->size == UNCODED_FRAME_PACKET_SIZE);
+        ret = s->oformat->write_uncoded_frame(s, pkt->stream_index, &frame, 0);
+        av_frame_free(&frame);
+    } else {
+        ret = s->oformat->write_packet(s, pkt);
+    }
 
     if (s->pb && ret >= 0) {
-        if (s->flags & AVFMT_FLAG_FLUSH_PACKETS)
+        if (s->flush_packets && s->flags & AVFMT_FLAG_FLUSH_PACKETS)
             avio_flush(s->pb);
         if (s->pb->error < 0)
             ret = s->pb->error;
     }
 
+
+    if (did_split)
+        av_packet_merge_side_data(pkt);
+
     return ret;
 }
 
@@ -390,8 +702,14 @@ int av_write_frame(AVFormatContext *s, AVPacket *pkt)
         return ret;
 
     if (!pkt) {
-        if (s->oformat->flags & AVFMT_ALLOW_FLUSH)
-            return s->oformat->write_packet(s, pkt);
+        if (s->oformat->flags & AVFMT_ALLOW_FLUSH) {
+            ret = s->oformat->write_packet(s, NULL);
+            if (s->flush_packets && s->pb && s->pb->error >= 0 && s->flags & AVFMT_FLAG_FLUSH_PACKETS)
+                avio_flush(s->pb);
+            if (ret >= 0 && s->pb && s->pb->error < 0)
+                ret = s->pb->error;
+            return ret;
+        }
         return 1;
     }
 
@@ -401,21 +719,31 @@ int av_write_frame(AVFormatContext *s, AVPacket *pkt)
         return ret;
 
     ret = write_packet(s, pkt);
+    if (ret >= 0 && s->pb && s->pb->error < 0)
+        ret = s->pb->error;
 
     if (ret >= 0)
         s->streams[pkt->stream_index]->nb_frames++;
     return ret;
 }
 
+#define CHUNK_START 0x1000
+
 int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
                              int (*compare)(AVFormatContext *, AVPacket *, AVPacket *))
 {
     int ret;
     AVPacketList **next_point, *this_pktl;
+    AVStream *st   = s->streams[pkt->stream_index];
+    int chunked    = s->max_chunk_size || s->max_chunk_duration;
 
     this_pktl      = av_mallocz(sizeof(AVPacketList));
     if (!this_pktl)
         return AVERROR(ENOMEM);
+    if ((pkt->flags & AV_PKT_FLAG_UNCODED_FRAME)) {
+        av_assert0(pkt->size == UNCODED_FRAME_PACKET_SIZE);
+        av_assert0(((AVFrame *)pkt->data)->buf);
+    }
 
     if ((ret = av_packet_ref(&this_pktl->pkt, pkt)) < 0) {
         av_free(this_pktl);
@@ -423,20 +751,44 @@ int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
     }
 
     if (s->streams[pkt->stream_index]->last_in_packet_buffer) {
-        next_point = &(s->streams[pkt->stream_index]->last_in_packet_buffer->next);
-    } else
+        next_point = &(st->last_in_packet_buffer->next);
+    } else {
         next_point = &s->internal->packet_buffer;
+    }
 
+    if (chunked) {
+        uint64_t max= av_rescale_q_rnd(s->max_chunk_duration, AV_TIME_BASE_Q, st->time_base, AV_ROUND_UP);
+        st->interleaver_chunk_size     += pkt->size;
+        st->interleaver_chunk_duration += pkt->duration;
+        if (   (s->max_chunk_size && st->interleaver_chunk_size > s->max_chunk_size)
+            || (max && st->interleaver_chunk_duration           > max)) {
+            st->interleaver_chunk_size      = 0;
+            this_pktl->pkt.flags |= CHUNK_START;
+            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;
+        }
+    }
     if (*next_point) {
+        if (chunked && !(this_pktl->pkt.flags & CHUNK_START))
+            goto next_non_null;
+
         if (compare(s, &s->internal->packet_buffer_end->pkt, pkt)) {
-            while (!compare(s, &(*next_point)->pkt, pkt))
+            while (   *next_point
+                   && ((chunked && !((*next_point)->pkt.flags&CHUNK_START))
+                       || !compare(s, &(*next_point)->pkt, pkt)))
                 next_point = &(*next_point)->next;
-            goto next_non_null;
+            if (*next_point)
+                goto next_non_null;
         } else {
             next_point = &(s->internal->packet_buffer_end->next);
         }
     }
-    assert(!*next_point);
+    av_assert1(!*next_point);
 
     s->internal->packet_buffer_end = this_pktl;
 next_non_null:
@@ -458,6 +810,16 @@ static int interleave_compare_dts(AVFormatContext *s, AVPacket *next,
     AVStream *st2 = s->streams[next->stream_index];
     int comp      = av_compare_ts(next->dts, st2->time_base, pkt->dts,
                                   st->time_base);
+    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);
+    }
 
     if (comp == 0)
         return pkt->stream_index < next->stream_index;
@@ -469,6 +831,7 @@ int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
 {
     AVPacketList *pktl;
     int stream_count = 0;
+    int noninterleaved_count = 0;
     int i, ret;
 
     if (pkt) {
@@ -476,7 +839,24 @@ int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
             return ret;
     }
 
-    if (s->max_interleave_delta > 0 && s->internal->packet_buffer && !flush) {
+    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_ATTACHMENT &&
+                   s->streams[i]->codec->codec_id != AV_CODEC_ID_VP8 &&
+                   s->streams[i]->codec->codec_id != AV_CODEC_ID_VP9) {
+            ++noninterleaved_count;
+        }
+    }
+
+    if (s->internal->nb_interleaved_streams == stream_count)
+        flush = 1;
+
+    if (s->max_interleave_delta > 0 &&
+        s->internal->packet_buffer &&
+        !flush &&
+        s->internal->nb_interleaved_streams == stream_count+noninterleaved_count
+    ) {
         AVPacket *top_pkt = &s->internal->packet_buffer->pkt;
         int64_t delta_dts = INT64_MIN;
         int64_t top_dts = av_rescale_q(top_pkt->dts,
@@ -494,7 +874,6 @@ int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
                                     s->streams[i]->time_base,
                                     AV_TIME_BASE_Q);
             delta_dts = FFMAX(delta_dts, last_dts - top_dts);
-            stream_count++;
         }
 
         if (delta_dts > s->max_interleave_delta) {
@@ -504,23 +883,22 @@ int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
                    delta_dts, s->max_interleave_delta);
             flush = 1;
         }
-    } else {
-        for (i = 0; i < s->nb_streams; i++)
-            stream_count += !!s->streams[i]->last_in_packet_buffer;
     }
 
-
-    if (stream_count && (s->internal->nb_interleaved_streams == stream_count || flush)) {
+    if (stream_count && flush) {
+        AVStream *st;
         pktl = s->internal->packet_buffer;
         *out = pktl->pkt;
+        st   = s->streams[out->stream_index];
 
         s->internal->packet_buffer = pktl->next;
         if (!s->internal->packet_buffer)
             s->internal->packet_buffer_end = NULL;
 
-        if (s->streams[out->stream_index]->last_in_packet_buffer == pktl)
-            s->streams[out->stream_index]->last_in_packet_buffer = NULL;
+        if (st->last_in_packet_buffer == pktl)
+            st->last_in_packet_buffer = NULL;
         av_freep(&pktl);
+
         return 1;
     } else {
         av_init_packet(out);
@@ -559,8 +937,10 @@ int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
     if (pkt) {
         AVStream *st = s->streams[pkt->stream_index];
 
-        av_log(s, AV_LOG_TRACE, "av_interleaved_write_frame size:%d dts:%" PRId64 " pts:%" PRId64 "\n",
-                pkt->size, pkt->dts, pkt->pts);
+        if (s->debug & FF_FDEBUG_TS)
+            av_log(s, AV_LOG_TRACE, "av_interleaved_write_frame size:%d dts:%s pts:%s\n",
+                pkt->size, av_ts2str(pkt->dts), av_ts2str(pkt->pts));
+
         if ((ret = compute_pkt_fields2(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
             goto fail;
 
@@ -592,6 +972,8 @@ int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
 
         if (ret < 0)
             return ret;
+        if(s->pb && s->pb->error)
+            return s->pb->error;
     }
 fail:
     av_packet_unref(pkt);
@@ -605,7 +987,7 @@ int av_write_trailer(AVFormatContext *s)
     for (;; ) {
         AVPacket pkt;
         ret = interleave_packet(s, &pkt, NULL, 1);
-        if (ret < 0) //FIXME cleanup needed for ret<0 ?
+        if (ret < 0)
             goto fail;
         if (!ret)
             break;
@@ -618,15 +1000,22 @@ int av_write_trailer(AVFormatContext *s)
 
         if (ret < 0)
             goto fail;
+        if(s->pb && s->pb->error)
+            goto fail;
     }
 
+fail:
     if (s->oformat->write_trailer)
+        if (ret >= 0) {
         ret = s->oformat->write_trailer(s);
+        } else {
+            s->oformat->write_trailer(s);
+        }
 
-    if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
-        avio_flush(s->pb);
-
-fail:
+    if (s->pb)
+       avio_flush(s->pb);
+    if (ret == 0)
+       ret = s->pb ? s->pb->error : 0;
     for (i = 0; i < s->nb_streams; i++) {
         av_freep(&s->streams[i]->priv_data);
         av_freep(&s->streams[i]->index_entries);
@@ -637,10 +1026,20 @@ fail:
     return ret;
 }
 
+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;
+}
+
 int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt,
-                     AVFormatContext *src)
+                     AVFormatContext *src, int interleave)
 {
     AVPacket local_pkt;
+    int ret;
 
     local_pkt = *pkt;
     local_pkt.stream_index = dst_stream;
@@ -652,5 +1051,63 @@ int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt,
         local_pkt.dts = av_rescale_q(pkt->dts,
                                      src->streams[pkt->stream_index]->time_base,
                                      dst->streams[dst_stream]->time_base);
-    return av_write_frame(dst, &local_pkt);
+    if (pkt->duration)
+        local_pkt.duration = av_rescale_q(pkt->duration,
+                                          src->streams[pkt->stream_index]->time_base,
+                                          dst->streams[dst_stream]->time_base);
+
+    if (interleave) ret = av_interleaved_write_frame(dst, &local_pkt);
+    else            ret = av_write_frame(dst, &local_pkt);
+    pkt->buf = local_pkt.buf;
+    pkt->side_data       = local_pkt.side_data;
+    pkt->side_data_elems = local_pkt.side_data_elems;
+    return ret;
+}
+
+static int av_write_uncoded_frame_internal(AVFormatContext *s, int stream_index,
+                                           AVFrame *frame, int interleaved)
+{
+    AVPacket pkt, *pktp;
+
+    av_assert0(s->oformat);
+    if (!s->oformat->write_uncoded_frame)
+        return AVERROR(ENOSYS);
+
+    if (!frame) {
+        pktp = NULL;
+    } else {
+        pktp = &pkt;
+        av_init_packet(&pkt);
+        pkt.data = (void *)frame;
+        pkt.size         = UNCODED_FRAME_PACKET_SIZE;
+        pkt.pts          =
+        pkt.dts          = frame->pts;
+        pkt.duration     = av_frame_get_pkt_duration(frame);
+        pkt.stream_index = stream_index;
+        pkt.flags |= AV_PKT_FLAG_UNCODED_FRAME;
+    }
+
+    return interleaved ? av_interleaved_write_frame(s, pktp) :
+                         av_write_frame(s, pktp);
+}
+
+int av_write_uncoded_frame(AVFormatContext *s, int stream_index,
+                           AVFrame *frame)
+{
+    return av_write_uncoded_frame_internal(s, stream_index, frame, 0);
+}
+
+int av_interleaved_write_uncoded_frame(AVFormatContext *s, int stream_index,
+                                       AVFrame *frame)
+{
+    return av_write_uncoded_frame_internal(s, stream_index, frame, 1);
+}
+
+int av_write_uncoded_frame_query(AVFormatContext *s, int stream_index)
+{
+    av_assert0(s->oformat);
+    if (!s->oformat->write_uncoded_frame)
+        return AVERROR(ENOSYS);
+    return s->oformat->write_uncoded_frame(s, stream_index, NULL,
+                                           AV_WRITE_UNCODED_FRAME_QUERY);
 }