]> git.sesse.net Git - nageru/commitdiff
Send the AVCodecContext for audio to the stream mux instead of constructing a fake...
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 25 Apr 2016 19:17:25 +0000 (21:17 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 25 Apr 2016 20:20:32 +0000 (22:20 +0200)
audio_encoder.h
mux.cpp
mux.h
quicksync_encoder.cpp
video_encoder.cpp

index 682cc47414698973e26646e260c8e3e2f1001c63..d627a9c281a6abd10bd42cc5e51f098a46b79412 100644 (file)
@@ -25,7 +25,7 @@ public:
        void encode_audio(const std::vector<float> &audio, int64_t audio_pts);
        void encode_last_audio();
 
-       const AVCodec *get_codec() { return ctx->codec; }
+       const AVCodecContext *get_ctx() { return ctx; }
 
 private:
        void encode_audio_one_frame(const float *audio, size_t num_samples, int64_t audio_pts);
diff --git a/mux.cpp b/mux.cpp
index 161194ed3d98b65364ad1fa3fbb5c0ca508ca678..ece11e117057167777023459bcc051e34b983c00 100644 (file)
--- a/mux.cpp
+++ b/mux.cpp
@@ -10,7 +10,7 @@
 
 using namespace std;
 
-Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const AVCodec *codec_audio, int time_base, int bit_rate, KeyFrameSignalReceiver *keyframe_signal_receiver)
+Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const AVCodecContext *audio_ctx, int time_base, KeyFrameSignalReceiver *keyframe_signal_receiver)
        : avctx(avctx), keyframe_signal_receiver(keyframe_signal_receiver)
 {
        AVCodec *codec_video = avcodec_find_encoder((video_codec == CODEC_H264) ? AV_CODEC_ID_H264 : AV_CODEC_ID_RAWVIDEO);
@@ -47,20 +47,13 @@ Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const
                avstream_video->codec->flags = AV_CODEC_FLAG_GLOBAL_HEADER;
        }
 
-       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 = 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;
diff --git a/mux.h b/mux.h
index d645916d758c1fa9435cbb39e8fc135b9aaad721..1dd967c40f2e7f3d379c168cf4485d10f18c2def 100644 (file)
--- a/mux.h
+++ b/mux.h
@@ -25,7 +25,7 @@ public:
        };
 
        // Takes ownership of avctx. <keyframe_signal_receiver> can be nullptr.
-       Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const AVCodec *codec_audio, int time_base, int bit_rate, KeyFrameSignalReceiver *keyframe_signal_receiver);
+       Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const AVCodecContext *audio_ctx, int time_base, KeyFrameSignalReceiver *keyframe_signal_receiver);
        ~Mux();
        void add_packet(const AVPacket &pkt, int64_t pts, int64_t dts);
 
index 5ebd454edfdb635ba630123c92adf4e68486830c..fbbde94cb4abfb37ccb820ee39ee43128e194384 100644 (file)
@@ -1949,7 +1949,7 @@ void QuickSyncEncoderImpl::open_output_file(const std::string &filename)
                exit(1);
        }
 
-       file_mux.reset(new Mux(avctx, frame_width, frame_height, Mux::CODEC_H264, file_audio_encoder->get_codec(), TIMEBASE, DEFAULT_AUDIO_OUTPUT_BIT_RATE, nullptr));
+       file_mux.reset(new Mux(avctx, frame_width, frame_height, Mux::CODEC_H264, file_audio_encoder->get_ctx(), TIMEBASE, nullptr));
 }
 
 void QuickSyncEncoderImpl::encode_thread_func()
index 910d4b70fd1c248b29f8deb31cffdc7345da4e92..cae4328f2a5e4fe974ffde3dd52494fabc43223a 100644 (file)
@@ -91,17 +91,6 @@ void VideoEncoder::open_output_stream()
        assert(oformat != nullptr);
        avctx->oformat = oformat;
 
-       string codec_name;
-       int bit_rate;
-
-       if (global_flags.stream_audio_codec_name.empty()) {
-               codec_name = AUDIO_OUTPUT_CODEC_NAME;
-               bit_rate = DEFAULT_AUDIO_OUTPUT_BIT_RATE;
-       } else {
-               codec_name = global_flags.stream_audio_codec_name;
-               bit_rate = global_flags.stream_audio_codec_bitrate;
-       }
-
        uint8_t *buf = (uint8_t *)av_malloc(MUX_BUFFER_SIZE);
        avctx->pb = avio_alloc_context(buf, MUX_BUFFER_SIZE, 1, this, nullptr, &VideoEncoder::write_packet_thunk, nullptr);
 
@@ -113,15 +102,10 @@ void VideoEncoder::open_output_stream()
        }
 
        avctx->flags = AVFMT_FLAG_CUSTOM_IO;
-       AVCodec *codec_audio = avcodec_find_encoder_by_name(codec_name.c_str());
-       if (codec_audio == nullptr) {
-               fprintf(stderr, "ERROR: Could not find codec '%s'\n", codec_name.c_str());
-               exit(1);
-       }
 
        int time_base = global_flags.stream_coarse_timebase ? COARSE_TIMEBASE : TIMEBASE;
        stream_mux_writing_header = true;
-       stream_mux.reset(new Mux(avctx, width, height, video_codec, codec_audio, time_base, bit_rate, this));
+       stream_mux.reset(new Mux(avctx, width, height, video_codec, stream_audio_encoder->get_ctx(), time_base, this));
        stream_mux_writing_header = false;
        httpd->set_header(stream_mux_header);
        stream_mux_header.clear();