X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=mux.cpp;h=d01c18038a893cfec7152d35d8e3b0f1131ed0fb;hb=96cb6414f85e0ef4d660b7bd56267303e80fcd05;hp=184fa1e11d549f33b8be9bceae5895a365cb4c90;hpb=1f1c3a3c29a80f7eb7fb261579b3b046d0034903;p=nageru diff --git a/mux.cpp b/mux.cpp index 184fa1e..d01c180 100644 --- a/mux.cpp +++ b/mux.cpp @@ -1,12 +1,29 @@ -#include +#include "mux.h" +#include +#include +#include +#include +#include #include #include #include +#include #include +extern "C" { +#include +#include +#include +#include +#include +#include +#include +} + #include "defs.h" -#include "mux.h" +#include "flags.h" +#include "metrics.h" #include "timebase.h" using namespace std; @@ -29,44 +46,48 @@ struct PacketBefore { 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, KeyFrameSignalReceiver *keyframe_signal_receiver) - : avctx(avctx), keyframe_signal_receiver(keyframe_signal_receiver) +Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const string &video_extradata, const AVCodecParameters *audio_codecpar, int time_base, std::function write_callback, const vector &metrics) + : avctx(avctx), write_callback(write_callback), metrics(metrics) { - AVCodec *codec_video = avcodec_find_encoder((video_codec == CODEC_H264) ? AV_CODEC_ID_H264 : AV_CODEC_ID_RAWVIDEO); - avstream_video = avformat_new_stream(avctx, codec_video); + avstream_video = avformat_new_stream(avctx, nullptr); if (avstream_video == nullptr) { fprintf(stderr, "avformat_new_stream() failed\n"); exit(1); } avstream_video->time_base = AVRational{1, time_base}; - avstream_video->codec->codec_type = AVMEDIA_TYPE_VIDEO; + avstream_video->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; if (video_codec == CODEC_H264) { - avstream_video->codec->codec_id = AV_CODEC_ID_H264; + avstream_video->codecpar->codec_id = AV_CODEC_ID_H264; } else { assert(video_codec == CODEC_NV12); - avstream_video->codec->codec_id = AV_CODEC_ID_RAWVIDEO; - avstream_video->codec->codec_tag = avcodec_pix_fmt_to_codec_tag(AV_PIX_FMT_NV12); + avstream_video->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO; + avstream_video->codecpar->codec_tag = avcodec_pix_fmt_to_codec_tag(AV_PIX_FMT_NV12); } - avstream_video->codec->width = width; - avstream_video->codec->height = height; - avstream_video->codec->time_base = AVRational{1, time_base}; - avstream_video->codec->ticks_per_frame = 1; // or 2? + avstream_video->codecpar->width = width; + avstream_video->codecpar->height = height; // Colorspace details. Closely correspond to settings in EffectChain_finalize, // as noted in each comment. // Note that the H.264 stream also contains this information and depending on the // mux, this might simply get ignored. See sps_rbsp(). - avstream_video->codec->color_primaries = AVCOL_PRI_BT709; // RGB colorspace (inout_format.color_space). - avstream_video->codec->color_trc = AVCOL_TRC_UNSPECIFIED; // Gamma curve (inout_format.gamma_curve). - avstream_video->codec->colorspace = AVCOL_SPC_SMPTE170M; // YUV colorspace (output_ycbcr_format.luma_coefficients). - 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; + // Note that there's no way to change this per-frame as the H.264 stream + // would like to be able to. + avstream_video->codecpar->color_primaries = AVCOL_PRI_BT709; // RGB colorspace (inout_format.color_space). + avstream_video->codecpar->color_trc = AVCOL_TRC_UNSPECIFIED; // Gamma curve (inout_format.gamma_curve). + // YUV colorspace (output_ycbcr_format.luma_coefficients). + if (global_flags.ycbcr_rec709_coefficients) { + avstream_video->codecpar->color_space = AVCOL_SPC_BT709; + } else { + avstream_video->codecpar->color_space = AVCOL_SPC_SMPTE170M; + } + avstream_video->codecpar->color_range = AVCOL_RANGE_MPEG; // Full vs. limited range (output_ycbcr_format.full_range). + avstream_video->codecpar->chroma_location = AVCHROMA_LOC_LEFT; // Chroma sample location. See chroma_offset_0[] in Mixer::subsample_chroma(). + avstream_video->codecpar->field_order = AV_FIELD_PROGRESSIVE; 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_video->codecpar->extradata = (uint8_t *)av_malloc(video_extradata.size()); + avstream_video->codecpar->extradata_size = video_extradata.size(); + memcpy(avstream_video->codecpar->extradata, video_extradata.data(), video_extradata.size()); } avstream_audio = avformat_new_stream(avctx, nullptr); @@ -75,7 +96,10 @@ Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const exit(1); } avstream_audio->time_base = AVRational{1, time_base}; - avcodec_copy_context(avstream_audio->codec, audio_ctx); + if (avcodec_parameters_copy(avstream_audio->codecpar, audio_codecpar) < 0) { + fprintf(stderr, "avcodec_parameters_copy() failed\n"); + exit(1); + } AVDictionary *options = NULL; vector> opts = MUX_OPTS; @@ -86,6 +110,9 @@ Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const fprintf(stderr, "avformat_write_header() failed\n"); exit(1); } + for (MuxMetrics *metric : metrics) { + metric->metric_written_bytes += avctx->pb->pos; + } // Make sure the header is written before the constructor exits. avio_flush(avctx->pb); @@ -93,9 +120,16 @@ Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const Mux::~Mux() { + int64_t old_pos = avctx->pb->pos; av_write_trailer(avctx); - av_free(avctx->pb->buffer); - av_free(avctx->pb); + for (MuxMetrics *metric : metrics) { + metric->metric_written_bytes += avctx->pb->pos - old_pos; + } + + if (!(avctx->oformat->flags & AVFMT_NOFILE) && + !(avctx->flags & AVFMT_FLAG_CUSTOM_IO)) { + avio_closep(&avctx->pb); + } avformat_free_context(avctx); } @@ -123,51 +157,41 @@ void Mux::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts) if (plug_count > 0) { plugged_packets.push_back(av_packet_clone(&pkt_copy)); } else { - add_interleaved_packet(pkt_copy); + write_packet_or_die(pkt_copy); } } av_packet_unref(&pkt_copy); -} -void Mux::add_interleaved_packet(const AVPacket &pkt) -{ - if (waiting_packets.empty() || waiting_packets.front()->stream_index == pkt.stream_index) { - // We could still get packets of the other type with earlier pts/dts, - // so we'll have to queue and wait. - waiting_packets.push(av_packet_clone(const_cast(&pkt))); - return; - } - - // Flush all the queued packets that are supposed to go before this. - PacketBefore before(avctx); - while (!waiting_packets.empty() && !before(&pkt, waiting_packets.front())) { - AVPacket *queued_pkt = waiting_packets.front(); - waiting_packets.pop(); - write_packet_with_signal(*queued_pkt); - av_packet_free(&queued_pkt); - } - - if (waiting_packets.empty()) { - waiting_packets.push(av_packet_clone(const_cast(&pkt))); - } else { - write_packet_with_signal(pkt); + // Note: This will be wrong in the case of plugged packets, but that only happens + // for network streams, not for files, and write callbacks are only really relevant + // for files. (We don't want to do this from write_packet_or_die, as it only has + // the rescaled pts, which is unsuitable for callback.) + if (pkt.stream_index == 0 && write_callback != nullptr) { + write_callback(pts); } } -void Mux::write_packet_with_signal(const AVPacket &pkt) +void Mux::write_packet_or_die(const AVPacket &pkt) { - if (keyframe_signal_receiver) { - if (pkt.flags & AV_PKT_FLAG_KEY) { - av_write_frame(avctx, nullptr); - keyframe_signal_receiver->signal_keyframe(); + for (MuxMetrics *metric : metrics) { + if (pkt.stream_index == 0) { + metric->metric_video_bytes += pkt.size; + } else if (pkt.stream_index == 1) { + metric->metric_audio_bytes += pkt.size; + } else { + assert(false); } } - if (av_write_frame(avctx, const_cast(&pkt)) < 0) { + int64_t old_pos = avctx->pb->pos; + if (av_interleaved_write_frame(avctx, const_cast(&pkt)) < 0) { fprintf(stderr, "av_interleaved_write_frame() failed\n"); exit(1); } avio_flush(avctx->pb); + for (MuxMetrics *metric : metrics) { + metric->metric_written_bytes += avctx->pb->pos - old_pos; + } } void Mux::plug() @@ -187,8 +211,21 @@ void Mux::unplug() sort(plugged_packets.begin(), plugged_packets.end(), PacketBefore(avctx)); for (AVPacket *pkt : plugged_packets) { - add_interleaved_packet(*pkt); + write_packet_or_die(*pkt); av_packet_free(&pkt); } plugged_packets.clear(); } + +void MuxMetrics::init(const vector> &labels) +{ + vector> labels_video = labels; + labels_video.emplace_back("stream", "video"); + global_metrics.add("mux_stream_bytes", labels_video, &metric_video_bytes); + + vector> labels_audio = labels; + labels_audio.emplace_back("stream", "audio"); + global_metrics.add("mux_stream_bytes", labels_audio, &metric_audio_bytes); + + global_metrics.add("mux_written_bytes", labels, &metric_written_bytes); +}