]> git.sesse.net Git - nageru/blobdiff - shared/mux.cpp
Fix some jerkiness when playing back with no interpolation.
[nageru] / shared / mux.cpp
index da8e2eed22a68d8769c89b1e09c6208a0868028c..adec53de7923171842d90822a7ec8600b9e78fd0 100644 (file)
@@ -47,13 +47,13 @@ struct PacketBefore {
        const AVFormatContext * const ctx;
 };
 
-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)
+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)
 {
-       avstream_video = avformat_new_stream(avctx, nullptr);
+       AVStream *avstream_video = avformat_new_stream(avctx, nullptr);
        if (avstream_video == nullptr) {
                fprintf(stderr, "avformat_new_stream() failed\n");
-               exit(1);
+               abort();
        }
        avstream_video->time_base = AVRational{1, time_base};
        avstream_video->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
@@ -88,20 +88,34 @@ Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const
                avstream_video->codecpar->extradata_size = video_extradata.size();
                memcpy(avstream_video->codecpar->extradata, video_extradata.data(), video_extradata.size());
        }
+       streams.push_back(avstream_video);
 
        if (audio_codecpar != nullptr) {
-               avstream_audio = avformat_new_stream(avctx, nullptr);
+               AVStream *avstream_audio = avformat_new_stream(avctx, nullptr);
                if (avstream_audio == nullptr) {
                        fprintf(stderr, "avformat_new_stream() failed\n");
-                       exit(1);
+                       abort();
                }
                avstream_audio->time_base = AVRational{1, time_base};
                if (avcodec_parameters_copy(avstream_audio->codecpar, audio_codecpar) < 0) {
                        fprintf(stderr, "avcodec_parameters_copy() failed\n");
-                       exit(1);
+                       abort();
                }
-       } else {
-               avstream_audio = nullptr;
+               streams.push_back(avstream_audio);
+       }
+
+       if (with_subtitles == WITH_SUBTITLES) {
+               AVStream *avstream_subtitles = avformat_new_stream(avctx, nullptr);
+               if (avstream_subtitles == nullptr) {
+                       fprintf(stderr, "avformat_new_stream() failed\n");
+                       abort();
+               }
+               avstream_subtitles->time_base = AVRational{1, time_base};
+               avstream_subtitles->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
+               avstream_subtitles->codecpar->codec_id = AV_CODEC_ID_WEBVTT;
+               avstream_subtitles->disposition = AV_DISPOSITION_METADATA;
+               streams.push_back(avstream_subtitles);
+               subtitle_stream_idx = streams.size() - 1;
        }
 
        AVDictionary *options = NULL;
@@ -111,7 +125,7 @@ Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const
        }
        if (avformat_write_header(avctx, &options) < 0) {
                fprintf(stderr, "avformat_write_header() failed\n");
-               exit(1);
+               abort();
        }
        for (MuxMetrics *metric : metrics) {
                metric->metric_written_bytes += avctx->pb->pos;
@@ -152,22 +166,16 @@ void Mux::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts, AVRational t
        av_init_packet(&pkt_copy);
        if (av_packet_ref(&pkt_copy, &pkt) < 0) {
                fprintf(stderr, "av_copy_packet() failed\n");
-               exit(1);
+               abort();
        }
        if (stream_index_override != -1) {
                pkt_copy.stream_index = stream_index_override;
        }
-       if (pkt_copy.stream_index == 0) {
-               pkt_copy.pts = av_rescale_q(pts, timebase, avstream_video->time_base);
-               pkt_copy.dts = av_rescale_q(dts, timebase, avstream_video->time_base);
-               pkt_copy.duration = av_rescale_q(pkt.duration, timebase, avstream_video->time_base);
-       } else if (pkt_copy.stream_index == 1) {
-               pkt_copy.pts = av_rescale_q(pts, timebase, avstream_audio->time_base);
-               pkt_copy.dts = av_rescale_q(dts, timebase, avstream_audio->time_base);
-               pkt_copy.duration = av_rescale_q(pkt.duration, timebase, avstream_audio->time_base);
-       } else {
-               assert(false);
-       }
+       assert(size_t(pkt_copy.stream_index) < streams.size());
+       AVRational time_base = streams[pkt_copy.stream_index]->time_base;
+       pkt_copy.pts = av_rescale_q(pts, timebase, time_base);
+       pkt_copy.dts = av_rescale_q(dts, timebase, time_base);
+       pkt_copy.duration = av_rescale_q(pkt.duration, timebase, time_base);
 
        {
                lock_guard<mutex> lock(mu);