]> git.sesse.net Git - nageru/blob - mux.cpp
Let settings follow buses when editing the mapping.
[nageru] / mux.cpp
1 #include <assert.h>
2
3 #include <algorithm>
4 #include <mutex>
5 #include <string>
6 #include <vector>
7
8 #include "defs.h"
9 #include "mux.h"
10 #include "timebase.h"
11
12 using namespace std;
13
14 struct PacketBefore {
15         PacketBefore(const AVFormatContext *ctx) : ctx(ctx) {}
16
17         bool operator() (const AVPacket *a, const AVPacket *b) const {
18                 int64_t a_dts = (a->dts == AV_NOPTS_VALUE ? a->pts : a->dts);
19                 int64_t b_dts = (b->dts == AV_NOPTS_VALUE ? b->pts : b->dts);
20                 AVRational a_timebase = ctx->streams[a->stream_index]->time_base;
21                 AVRational b_timebase = ctx->streams[b->stream_index]->time_base;
22                 if (av_compare_ts(a_dts, a_timebase, b_dts, b_timebase) != 0) {
23                         return av_compare_ts(a_dts, a_timebase, b_dts, b_timebase) < 0;
24                 } else {
25                         return av_compare_ts(a->pts, a_timebase, b->pts, b_timebase) < 0;
26                 }
27         }
28
29         const AVFormatContext * const ctx;
30 };
31
32 Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const string &video_extradata, const AVCodecParameters *audio_codecpar, int time_base, std::function<void(int64_t)> write_callback)
33         : avctx(avctx), write_callback(write_callback)
34 {
35         avstream_video = avformat_new_stream(avctx, nullptr);
36         if (avstream_video == nullptr) {
37                 fprintf(stderr, "avformat_new_stream() failed\n");
38                 exit(1);
39         }
40         avstream_video->time_base = AVRational{1, time_base};
41         avstream_video->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
42         if (video_codec == CODEC_H264) {
43                 avstream_video->codecpar->codec_id = AV_CODEC_ID_H264;
44         } else {
45                 assert(video_codec == CODEC_NV12);
46                 avstream_video->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
47                 avstream_video->codecpar->codec_tag = avcodec_pix_fmt_to_codec_tag(AV_PIX_FMT_NV12);
48         }
49         avstream_video->codecpar->width = width;
50         avstream_video->codecpar->height = height;
51
52         // Colorspace details. Closely correspond to settings in EffectChain_finalize,
53         // as noted in each comment.
54         // Note that the H.264 stream also contains this information and depending on the
55         // mux, this might simply get ignored. See sps_rbsp().
56         avstream_video->codecpar->color_primaries = AVCOL_PRI_BT709;  // RGB colorspace (inout_format.color_space).
57         avstream_video->codecpar->color_trc = AVCOL_TRC_UNSPECIFIED;  // Gamma curve (inout_format.gamma_curve).
58         avstream_video->codecpar->color_space = AVCOL_SPC_SMPTE170M;  // YUV colorspace (output_ycbcr_format.luma_coefficients).
59         avstream_video->codecpar->color_range = AVCOL_RANGE_MPEG;  // Full vs. limited range (output_ycbcr_format.full_range).
60         avstream_video->codecpar->chroma_location = AVCHROMA_LOC_LEFT;  // Chroma sample location. See chroma_offset_0[] in Mixer::subsample_chroma().
61         avstream_video->codecpar->field_order = AV_FIELD_PROGRESSIVE;
62
63         if (!video_extradata.empty()) {
64                 avstream_video->codecpar->extradata = (uint8_t *)av_malloc(video_extradata.size());
65                 avstream_video->codecpar->extradata_size = video_extradata.size();
66                 memcpy(avstream_video->codecpar->extradata, video_extradata.data(), video_extradata.size());
67         }
68
69         avstream_audio = avformat_new_stream(avctx, nullptr);
70         if (avstream_audio == nullptr) {
71                 fprintf(stderr, "avformat_new_stream() failed\n");
72                 exit(1);
73         }
74         avstream_audio->time_base = AVRational{1, time_base};
75         if (avcodec_parameters_copy(avstream_audio->codecpar, audio_codecpar) < 0) {
76                 fprintf(stderr, "avcodec_parameters_copy() failed\n");
77                 exit(1);
78         }
79
80         AVDictionary *options = NULL;
81         vector<pair<string, string>> opts = MUX_OPTS;
82         for (pair<string, string> opt : opts) {
83                 av_dict_set(&options, opt.first.c_str(), opt.second.c_str(), 0);
84         }
85         if (avformat_write_header(avctx, &options) < 0) {
86                 fprintf(stderr, "avformat_write_header() failed\n");
87                 exit(1);
88         }
89
90         // Make sure the header is written before the constructor exits.
91         avio_flush(avctx->pb);
92 }
93
94 Mux::~Mux()
95 {
96         av_write_trailer(avctx);
97         if (!(avctx->oformat->flags & AVFMT_NOFILE) &&
98             !(avctx->flags & AVFMT_FLAG_CUSTOM_IO)) {
99                 avio_closep(&avctx->pb);
100         }
101         avformat_free_context(avctx);
102 }
103
104 void Mux::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts)
105 {
106         AVPacket pkt_copy;
107         if (av_copy_packet(&pkt_copy, &pkt) < 0) {
108                 fprintf(stderr, "av_copy_packet() failed\n");
109                 exit(1);
110         }
111         if (pkt.stream_index == 0) {
112                 pkt_copy.pts = av_rescale_q(pts, AVRational{1, TIMEBASE}, avstream_video->time_base);
113                 pkt_copy.dts = av_rescale_q(dts, AVRational{1, TIMEBASE}, avstream_video->time_base);
114                 pkt_copy.duration = av_rescale_q(pkt.duration, AVRational{1, TIMEBASE}, avstream_video->time_base);
115         } else if (pkt.stream_index == 1) {
116                 pkt_copy.pts = av_rescale_q(pts, AVRational{1, TIMEBASE}, avstream_audio->time_base);
117                 pkt_copy.dts = av_rescale_q(dts, AVRational{1, TIMEBASE}, avstream_audio->time_base);
118                 pkt_copy.duration = av_rescale_q(pkt.duration, AVRational{1, TIMEBASE}, avstream_audio->time_base);
119         } else {
120                 assert(false);
121         }
122
123         {
124                 lock_guard<mutex> lock(mu);
125                 if (plug_count > 0) {
126                         plugged_packets.push_back(av_packet_clone(&pkt_copy));
127                 } else {
128                         write_packet_or_die(pkt_copy);
129                 }
130         }
131
132         av_packet_unref(&pkt_copy);
133
134         // Note: This will be wrong in the case of plugged packets, but that only happens
135         // for network streams, not for files, and write callbacks are only really relevant
136         // for files. (We don't want to do this from write_packet_or_die, as it only has
137         // the rescaled pts, which is unsuitable for callback.)
138         if (pkt.stream_index == 0 && write_callback != nullptr) {
139                 write_callback(pts);
140         }
141 }
142
143 void Mux::write_packet_or_die(const AVPacket &pkt)
144 {
145         if (av_interleaved_write_frame(avctx, const_cast<AVPacket *>(&pkt)) < 0) {
146                 fprintf(stderr, "av_interleaved_write_frame() failed\n");
147                 exit(1);
148         }
149         avio_flush(avctx->pb);
150 }
151
152 void Mux::plug()
153 {
154         lock_guard<mutex> lock(mu);
155         ++plug_count;
156 }
157
158 void Mux::unplug()
159 {
160         lock_guard<mutex> lock(mu);
161         if (--plug_count > 0) {
162                 return;
163         }
164         assert(plug_count >= 0);
165
166         sort(plugged_packets.begin(), plugged_packets.end(), PacketBefore(avctx));
167
168         for (AVPacket *pkt : plugged_packets) {
169                 write_packet_or_die(*pkt);
170                 av_packet_free(&pkt);
171         }
172         plugged_packets.clear();
173 }