]> git.sesse.net Git - nageru/blobdiff - shared/mux.cpp
Implement SRT output.
[nageru] / shared / mux.cpp
index adec53de7923171842d90822a7ec8600b9e78fd0..819638c480c04f22a98a6364ecf1b1cb59ef569c 100644 (file)
@@ -50,6 +50,13 @@ struct PacketBefore {
 Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const string &video_extradata, const AVCodecParameters *audio_codecpar, AVColorSpace color_space, int time_base, function<void(int64_t)> write_callback, WriteStrategy write_strategy, const vector<MuxMetrics *> &metrics, WithSubtitles with_subtitles)
        : write_strategy(write_strategy), avctx(avctx), write_callback(write_callback), metrics(metrics)
 {
+       // MPEG-TS ostensibly needs some conversions (e.g. for differing start codes),
+       // so let FFmpeg insert them as needed in case we are muxing to that.
+       // Curiously enough, things actually seem to go quite fine without
+       // (and it also seems FFmpeg's MPEG-TS muxer automatically does stuff like
+       // repeat PPS/SPS before keyframes for us), but it can't hurt.
+       avctx->flags |= AVFMT_FLAG_AUTO_BSF;
+
        AVStream *avstream_video = avformat_new_stream(avctx, nullptr);
        if (avstream_video == nullptr) {
                fprintf(stderr, "avformat_new_stream() failed\n");
@@ -59,9 +66,8 @@ Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const
        avstream_video->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
        if (video_codec == CODEC_H264) {
                avstream_video->codecpar->codec_id = AV_CODEC_ID_H264;
-       } else if (video_codec == CODEC_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);
+       } else if (video_codec == CODEC_AV1) {
+               avstream_video->codecpar->codec_id = AV_CODEC_ID_AV1;
        } else {
                assert(video_codec == CODEC_MJPEG);
                avstream_video->codecpar->codec_id = AV_CODEC_ID_MJPEG;
@@ -84,7 +90,7 @@ Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const
        avstream_video->codecpar->field_order = AV_FIELD_PROGRESSIVE;
 
        if (!video_extradata.empty()) {
-               avstream_video->codecpar->extradata = (uint8_t *)av_malloc(video_extradata.size());
+               avstream_video->codecpar->extradata = (uint8_t *)av_malloc(video_extradata.size() + AV_INPUT_BUFFER_PADDING_SIZE);
                avstream_video->codecpar->extradata_size = video_extradata.size();
                memcpy(avstream_video->codecpar->extradata, video_extradata.data(), video_extradata.size());
        }
@@ -123,9 +129,12 @@ Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const
        for (pair<string, string> opt : opts) {
                av_dict_set(&options, opt.first.c_str(), opt.second.c_str(), 0);
        }
-       if (avformat_write_header(avctx, &options) < 0) {
-               fprintf(stderr, "avformat_write_header() failed\n");
-               abort();
+       int err = avformat_write_header(avctx, &options);
+       if (err < 0) {
+               char errbuf[AV_ERROR_MAX_STRING_SIZE];
+               av_strerror(err, errbuf, sizeof(errbuf));
+               fprintf(stderr, "avformat_write_header() failed: %s\n", errbuf);
+               exit(EXIT_FAILURE);
        }
        for (MuxMetrics *metric : metrics) {
                metric->metric_written_bytes += avctx->pb->pos;
@@ -162,6 +171,8 @@ Mux::~Mux()
 
 void Mux::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts, AVRational timebase, int stream_index_override)
 {
+       assert(pts >= dts);
+
        AVPacket pkt_copy;
        av_init_packet(&pkt_copy);
        if (av_packet_ref(&pkt_copy, &pkt) < 0) {
@@ -205,9 +216,12 @@ void Mux::write_packet_or_die(const AVPacket &pkt, int64_t unscaled_pts)
                }
        }
        int64_t old_pos = avctx->pb->pos;
-       if (av_interleaved_write_frame(avctx, const_cast<AVPacket *>(&pkt)) < 0) {
-               fprintf(stderr, "av_interleaved_write_frame() failed\n");
-               abort();
+       int err = av_interleaved_write_frame(avctx, const_cast<AVPacket *>(&pkt));
+       if (err < 0) {
+               char errbuf[AV_ERROR_MAX_STRING_SIZE];
+               av_strerror(err, errbuf, sizeof(errbuf));
+               fprintf(stderr, "av_interleaved_write_frame() failed: %s\n", errbuf);
+               exit(EXIT_FAILURE);
        }
        avio_flush(avctx->pb);
        for (MuxMetrics *metric : metrics) {