]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/mux.c
fate/mp3: use the f32le format as output
[ffmpeg] / libavformat / mux.c
index 9c2144acc11e05fc2a227cef10f0c3b91a77254b..3cff2647ed979d5f032fa7ec5d2edd8d56b25dfa 100644 (file)
@@ -79,7 +79,7 @@ static int validate_codec_tag(AVFormatContext *s, AVStream *st)
     }
     if (id != AV_CODEC_ID_NONE)
         return 0;
-    if (tag && (st->codec->strict_std_compliance >= FF_COMPLIANCE_NORMAL))
+    if (tag && (s->strict_std_compliance >= FF_COMPLIANCE_NORMAL))
         return 0;
     return 1;
 }
@@ -100,7 +100,7 @@ static int init_muxer(AVFormatContext *s, AVDictionary **options)
         goto fail;
 
 #if FF_API_LAVF_BITEXACT
-    if (s->nb_streams && s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)
+    if (s->nb_streams && s->streams[0]->codec->flags & AV_CODEC_FLAG_BITEXACT)
         s->flags |= AVFMT_FLAG_BITEXACT;
 #endif
 
@@ -195,7 +195,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
         }
 
         if (of->flags & AVFMT_GLOBALHEADER &&
-            !(codec->flags & CODEC_FLAG_GLOBAL_HEADER))
+            !(codec->flags & AV_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);
@@ -248,6 +248,13 @@ int avformat_write_header(AVFormatContext *s, AVDictionary **options)
             return ret;
     }
 
+    if (s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_AUTO) {
+        if (s->oformat->flags & (AVFMT_TS_NEGATIVE | AVFMT_NOTIMESTAMPS)) {
+            s->avoid_negative_ts = 0;
+        } else
+            s->avoid_negative_ts = AVFMT_AVOID_NEG_TS_MAKE_NON_NEGATIVE;
+    }
+
     return 0;
 }
 
@@ -257,7 +264,7 @@ 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 num, den, i;
 
-    av_dlog(s, "compute_pkt_fields2: pts:%" PRId64 " dts:%" PRId64 " cur_dts:%" PRId64 " b:%d size:%d st:%d\n",
+    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(pkt->pts == AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE)
@@ -265,7 +272,7 @@ static int compute_pkt_fields2(AVFormatContext *s, AVStream *st, AVPacket *pkt)
 
     /* duration field */
     if (pkt->duration == 0) {
-        ff_compute_frame_duration(&num, &den, st, NULL, pkt);
+        ff_compute_frame_duration(s, &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);
         }
@@ -294,11 +301,14 @@ static int compute_pkt_fields2(AVFormatContext *s, AVStream *st, AVPacket *pkt)
         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 < dts in stream %d\n", st->index);
+        av_log(s, AV_LOG_ERROR,
+               "pts %" PRId64 " < dts %" PRId64 " in stream %d\n",
+               pkt->pts, pkt->dts,
+               st->index);
         return AVERROR(EINVAL);
     }
 
-    av_dlog(s, "av_write_frame: pts2:%"PRId64" dts2:%"PRId64"\n",
+    av_log(s, AV_LOG_TRACE, "av_write_frame: pts2:%"PRId64" dts2:%"PRId64"\n",
             pkt->pts, pkt->dts);
     st->cur_dts = pkt->dts;
 
@@ -315,21 +325,30 @@ static int compute_pkt_fields2(AVFormatContext *s, AVStream *st, AVPacket *pkt)
 static int write_packet(AVFormatContext *s, AVPacket *pkt)
 {
     int ret;
-    if (!(s->oformat->flags & (AVFMT_TS_NEGATIVE | AVFMT_NOTIMESTAMPS))) {
+    if (s->avoid_negative_ts > 0) {
         AVRational time_base = s->streams[pkt->stream_index]->time_base;
         int64_t offset = 0;
 
-        if (!s->offset && pkt->dts != AV_NOPTS_VALUE && pkt->dts < 0) {
-            s->offset = -pkt->dts;
-            s->offset_timebase = 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->offset)
-            offset = av_rescale_q(s->offset, s->offset_timebase, time_base);
+        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);
+        }
     }
     ret = s->oformat->write_packet(s, pkt);
 
@@ -384,12 +403,15 @@ int av_write_frame(AVFormatContext *s, AVPacket *pkt)
     return ret;
 }
 
-void ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
-                              int (*compare)(AVFormatContext *, AVPacket *, AVPacket *))
+int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
+                             int (*compare)(AVFormatContext *, AVPacket *, AVPacket *))
 {
+    int ret;
     AVPacketList **next_point, *this_pktl;
 
     this_pktl      = av_mallocz(sizeof(AVPacketList));
+    if (!this_pktl)
+        return AVERROR(ENOMEM);
     this_pktl->pkt = *pkt;
 #if FF_API_DESTRUCT_PACKET
 FF_DISABLE_DEPRECATION_WARNINGS
@@ -397,31 +419,39 @@ FF_DISABLE_DEPRECATION_WARNINGS
 FF_ENABLE_DEPRECATION_WARNINGS
 #endif
     pkt->buf       = NULL;
-    av_dup_packet(&this_pktl->pkt);  // duplicate the packet if it uses non-alloced memory
+    pkt->side_data = NULL;
+    pkt->side_data_elems = 0;
+    // Duplicate the packet if it uses non-allocated memory
+    if ((ret = av_dup_packet(&this_pktl->pkt)) < 0) {
+        av_free(this_pktl);
+        return ret;
+    }
 
     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 = &s->packet_buffer;
+        next_point = &s->internal->packet_buffer;
 
     if (*next_point) {
-        if (compare(s, &s->packet_buffer_end->pkt, pkt)) {
+        if (compare(s, &s->internal->packet_buffer_end->pkt, pkt)) {
             while (!compare(s, &(*next_point)->pkt, pkt))
                 next_point = &(*next_point)->next;
             goto next_non_null;
         } else {
-            next_point = &(s->packet_buffer_end->next);
+            next_point = &(s->internal->packet_buffer_end->next);
         }
     }
     assert(!*next_point);
 
-    s->packet_buffer_end = this_pktl;
+    s->internal->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;
+
+    return 0;
 }
 
 static int interleave_compare_dts(AVFormatContext *s, AVPacket *next,
@@ -442,14 +472,15 @@ int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
 {
     AVPacketList *pktl;
     int stream_count = 0;
-    int i;
+    int i, ret;
 
     if (pkt) {
-        ff_interleave_add_packet(s, pkt, interleave_compare_dts);
+        if ((ret = ff_interleave_add_packet(s, pkt, interleave_compare_dts)) < 0)
+            return ret;
     }
 
-    if (s->max_interleave_delta > 0 && s->packet_buffer && !flush) {
-        AVPacket *top_pkt = &s->packet_buffer->pkt;
+    if (s->max_interleave_delta > 0 && s->internal->packet_buffer && !flush) {
+        AVPacket *top_pkt = &s->internal->packet_buffer->pkt;
         int64_t delta_dts = INT64_MIN;
         int64_t top_dts = av_rescale_q(top_pkt->dts,
                                        s->streams[top_pkt->stream_index]->time_base,
@@ -483,12 +514,12 @@ int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
 
 
     if (stream_count && (s->internal->nb_interleaved_streams == stream_count || flush)) {
-        pktl = s->packet_buffer;
+        pktl = s->internal->packet_buffer;
         *out = pktl->pkt;
 
-        s->packet_buffer = pktl->next;
-        if (!s->packet_buffer)
-            s->packet_buffer_end = NULL;
+        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;
@@ -531,7 +562,7 @@ int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
     if (pkt) {
         AVStream *st = s->streams[pkt->stream_index];
 
-        av_dlog(s, "av_interleaved_write_frame size:%d dts:%" PRId64 " pts:%" PRId64 "\n",
+        av_log(s, AV_LOG_TRACE, "av_interleaved_write_frame size:%d dts:%" PRId64 " pts:%" PRId64 "\n",
                 pkt->size, pkt->dts, pkt->pts);
         if ((ret = compute_pkt_fields2(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
             goto fail;
@@ -541,7 +572,7 @@ int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
             goto fail;
         }
     } else {
-        av_dlog(s, "av_interleaved_write_frame FLUSH\n");
+        av_log(s, AV_LOG_TRACE, "av_interleaved_write_frame FLUSH\n");
         flush = 1;
     }
 
@@ -595,7 +626,7 @@ int av_write_trailer(AVFormatContext *s)
     if (s->oformat->write_trailer)
         ret = s->oformat->write_trailer(s);
 
-    if (!(s->oformat->flags & AVFMT_NOFILE))
+    if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
         avio_flush(s->pb);
 
 fail: