]> git.sesse.net Git - nageru/blobdiff - mux.cpp
Use the new libavformat functionality for marking keyframes.
[nageru] / mux.cpp
diff --git a/mux.cpp b/mux.cpp
index 82d42abe705c75e2af79259252a68e5494e6763f..cc67eb616b466b4a4e42f89fe511eeba8c081e11 100644 (file)
--- a/mux.cpp
+++ b/mux.cpp
@@ -1,5 +1,7 @@
 #include <assert.h>
 
+#include <algorithm>
+#include <mutex>
 #include <string>
 #include <vector>
 
@@ -9,7 +11,25 @@
 
 using namespace std;
 
-Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, int time_base)
+struct PacketBefore {
+       PacketBefore(const AVFormatContext *ctx) : ctx(ctx) {}
+
+       bool operator() (const AVPacket *a, const AVPacket *b) const {
+               int64_t a_dts = (a->dts == AV_NOPTS_VALUE ? a->pts : a->dts);
+               int64_t b_dts = (b->dts == AV_NOPTS_VALUE ? b->pts : b->dts);
+               AVRational a_timebase = ctx->streams[a->stream_index]->time_base;
+               AVRational b_timebase = ctx->streams[b->stream_index]->time_base;
+               if (av_compare_ts(a_dts, a_timebase, b_dts, b_timebase) != 0) {
+                       return av_compare_ts(a_dts, a_timebase, b_dts, b_timebase) < 0;
+               } else {
+                       return av_compare_ts(a->pts, a_timebase, b->pts, b_timebase) < 0;
+               }
+       }
+
+       const AVFormatContext * const ctx;
+};
+
+Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const string &video_extradata, const AVCodecContext *audio_ctx, int time_base)
        : avctx(avctx)
 {
        AVCodec *codec_video = avcodec_find_encoder((video_codec == CODEC_H264) ? AV_CODEC_ID_H264 : AV_CODEC_ID_RAWVIDEO);
@@ -42,29 +62,20 @@ Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, int t
        avstream_video->codec->color_range = AVCOL_RANGE_MPEG;  // Full vs. limited range (output_ycbcr_format.full_range).
        avstream_video->codec->chroma_sample_location = AVCHROMA_LOC_LEFT;  // Chroma sample location. See chroma_offset_0[] in Mixer::subsample_chroma().
        avstream_video->codec->field_order = AV_FIELD_PROGRESSIVE;
-       if (avctx->oformat->flags & AVFMT_GLOBALHEADER) {
-               avstream_video->codec->flags = AV_CODEC_FLAG_GLOBAL_HEADER;
-       }
 
-       AVCodec *codec_audio = avcodec_find_encoder_by_name(AUDIO_OUTPUT_CODEC_NAME);
-       if (codec_audio == nullptr) {
-               fprintf(stderr, "ERROR: Could not find codec '%s'\n", AUDIO_OUTPUT_CODEC_NAME);
-               exit(1);
+       if (!video_extradata.empty()) {
+               avstream_video->codec->extradata = (uint8_t *)av_malloc(video_extradata.size());
+               avstream_video->codec->extradata_size = video_extradata.size();
+               memcpy(avstream_video->codec->extradata, video_extradata.data(), video_extradata.size());
        }
-       avstream_audio = avformat_new_stream(avctx, codec_audio);
+
+       avstream_audio = avformat_new_stream(avctx, nullptr);
        if (avstream_audio == nullptr) {
                fprintf(stderr, "avformat_new_stream() failed\n");
                exit(1);
        }
        avstream_audio->time_base = AVRational{1, time_base};
-       avstream_audio->codec->bit_rate = AUDIO_OUTPUT_BIT_RATE;
-       avstream_audio->codec->sample_rate = OUTPUT_FREQUENCY;
-       avstream_audio->codec->channels = 2;
-       avstream_audio->codec->channel_layout = AV_CH_LAYOUT_STEREO;
-       avstream_audio->codec->time_base = AVRational{1, time_base};
-       if (avctx->oformat->flags & AVFMT_GLOBALHEADER) {
-               avstream_audio->codec->flags = AV_CODEC_FLAG_GLOBAL_HEADER;
-       }
+       avcodec_copy_context(avstream_audio->codec, audio_ctx);
 
        AVDictionary *options = NULL;
        vector<pair<string, string>> opts = MUX_OPTS;
@@ -75,6 +86,9 @@ Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, int t
                fprintf(stderr, "avformat_write_header() failed\n");
                exit(1);
        }
+
+       // Make sure the header is written before the constructor exits.
+       avio_flush(avctx->pb);
 }
 
 Mux::~Mux()
@@ -87,29 +101,63 @@ Mux::~Mux()
 
 void Mux::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts)
 {
-       if (!seen_keyframe && !(pkt.stream_index == 0 && (pkt.flags & AV_PKT_FLAG_KEY))) {
-               // Wait until we see the first (video) key frame.
-               return;
-       }
-       seen_keyframe = true;
-
        AVPacket pkt_copy;
-       av_copy_packet(&pkt_copy, &pkt);
+       if (av_copy_packet(&pkt_copy, &pkt) < 0) {
+               fprintf(stderr, "av_copy_packet() failed\n");
+               exit(1);
+       }
        if (pkt.stream_index == 0) {
                pkt_copy.pts = av_rescale_q(pts, AVRational{1, TIMEBASE}, avstream_video->time_base);
                pkt_copy.dts = av_rescale_q(dts, AVRational{1, TIMEBASE}, avstream_video->time_base);
+               pkt_copy.duration = av_rescale_q(pkt.duration, AVRational{1, TIMEBASE}, avstream_video->time_base);
        } else if (pkt.stream_index == 1) {
                pkt_copy.pts = av_rescale_q(pts, AVRational{1, TIMEBASE}, avstream_audio->time_base);
                pkt_copy.dts = av_rescale_q(dts, AVRational{1, TIMEBASE}, avstream_audio->time_base);
+               pkt_copy.duration = av_rescale_q(pkt.duration, AVRational{1, TIMEBASE}, avstream_audio->time_base);
        } else {
                assert(false);
        }
 
-       if (av_interleaved_write_frame(avctx, &pkt_copy) < 0) {
+       {
+               lock_guard<mutex> lock(mu);
+               if (plug_count > 0) {
+                       plugged_packets.push_back(av_packet_clone(&pkt_copy));
+               } else {
+                       write_packet_or_die(pkt_copy);
+               }
+       }
+
+       av_packet_unref(&pkt_copy);
+}
+
+void Mux::write_packet_or_die(const AVPacket &pkt)
+{
+       if (av_interleaved_write_frame(avctx, const_cast<AVPacket *>(&pkt)) < 0) {
                fprintf(stderr, "av_interleaved_write_frame() failed\n");
                exit(1);
        }
+       avio_flush(avctx->pb);
+}
 
-       av_packet_unref(&pkt_copy);
+void Mux::plug()
+{
+       lock_guard<mutex> lock(mu);
+       ++plug_count;
 }
 
+void Mux::unplug()
+{
+       lock_guard<mutex> lock(mu);
+       if (--plug_count > 0) {
+               return;
+       }
+       assert(plug_count >= 0);
+
+       sort(plugged_packets.begin(), plugged_packets.end(), PacketBefore(avctx));
+
+       for (AVPacket *pkt : plugged_packets) {
+               write_packet_or_die(*pkt);
+               av_packet_free(&pkt);
+       }
+       plugged_packets.clear();
+}