]> git.sesse.net Git - nageru/blob - mux.cpp
When doing a cut, do the shutdown in a separate thread.
[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 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)
15         : avctx(avctx), keyframe_signal_receiver(keyframe_signal_receiver)
16 {
17         AVCodec *codec_video = avcodec_find_encoder((video_codec == CODEC_H264) ? AV_CODEC_ID_H264 : AV_CODEC_ID_RAWVIDEO);
18         avstream_video = avformat_new_stream(avctx, codec_video);
19         if (avstream_video == nullptr) {
20                 fprintf(stderr, "avformat_new_stream() failed\n");
21                 exit(1);
22         }
23         avstream_video->time_base = AVRational{1, time_base};
24         avstream_video->codec->codec_type = AVMEDIA_TYPE_VIDEO;
25         if (video_codec == CODEC_H264) {
26                 avstream_video->codec->codec_id = AV_CODEC_ID_H264;
27         } else {
28                 assert(video_codec == CODEC_NV12);
29                 avstream_video->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
30                 avstream_video->codec->codec_tag = avcodec_pix_fmt_to_codec_tag(AV_PIX_FMT_NV12);
31         }
32         avstream_video->codec->width = width;
33         avstream_video->codec->height = height;
34         avstream_video->codec->time_base = AVRational{1, time_base};
35         avstream_video->codec->ticks_per_frame = 1;  // or 2?
36
37         // Colorspace details. Closely correspond to settings in EffectChain_finalize,
38         // as noted in each comment.
39         // Note that the H.264 stream also contains this information and depending on the
40         // mux, this might simply get ignored. See sps_rbsp().
41         avstream_video->codec->color_primaries = AVCOL_PRI_BT709;  // RGB colorspace (inout_format.color_space).
42         avstream_video->codec->color_trc = AVCOL_TRC_UNSPECIFIED;  // Gamma curve (inout_format.gamma_curve).
43         avstream_video->codec->colorspace = AVCOL_SPC_SMPTE170M;  // YUV colorspace (output_ycbcr_format.luma_coefficients).
44         avstream_video->codec->color_range = AVCOL_RANGE_MPEG;  // Full vs. limited range (output_ycbcr_format.full_range).
45         avstream_video->codec->chroma_sample_location = AVCHROMA_LOC_LEFT;  // Chroma sample location. See chroma_offset_0[] in Mixer::subsample_chroma().
46         avstream_video->codec->field_order = AV_FIELD_PROGRESSIVE;
47
48         if (!video_extradata.empty()) {
49                 avstream_video->codec->extradata = (uint8_t *)av_malloc(video_extradata.size());
50                 avstream_video->codec->extradata_size = video_extradata.size();
51                 memcpy(avstream_video->codec->extradata, video_extradata.data(), video_extradata.size());
52         }
53
54         avstream_audio = avformat_new_stream(avctx, nullptr);
55         if (avstream_audio == nullptr) {
56                 fprintf(stderr, "avformat_new_stream() failed\n");
57                 exit(1);
58         }
59         avstream_audio->time_base = AVRational{1, time_base};
60         avcodec_copy_context(avstream_audio->codec, audio_ctx);
61
62         AVDictionary *options = NULL;
63         vector<pair<string, string>> opts = MUX_OPTS;
64         for (pair<string, string> opt : opts) {
65                 av_dict_set(&options, opt.first.c_str(), opt.second.c_str(), 0);
66         }
67         if (avformat_write_header(avctx, &options) < 0) {
68                 fprintf(stderr, "avformat_write_header() failed\n");
69                 exit(1);
70         }
71
72         // Make sure the header is written before the constructor exits.
73         avio_flush(avctx->pb);
74 }
75
76 Mux::~Mux()
77 {
78         av_write_trailer(avctx);
79         av_free(avctx->pb->buffer);
80         av_free(avctx->pb);
81         avformat_free_context(avctx);
82 }
83
84 void Mux::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts)
85 {
86         AVPacket pkt_copy;
87         if (av_copy_packet(&pkt_copy, &pkt) < 0) {
88                 fprintf(stderr, "av_copy_packet() failed\n");
89                 exit(1);
90         }
91         if (pkt.stream_index == 0) {
92                 pkt_copy.pts = av_rescale_q(pts, AVRational{1, TIMEBASE}, avstream_video->time_base);
93                 pkt_copy.dts = av_rescale_q(dts, AVRational{1, TIMEBASE}, avstream_video->time_base);
94                 pkt_copy.duration = av_rescale_q(pkt.duration, AVRational{1, TIMEBASE}, avstream_video->time_base);
95         } else if (pkt.stream_index == 1) {
96                 pkt_copy.pts = av_rescale_q(pts, AVRational{1, TIMEBASE}, avstream_audio->time_base);
97                 pkt_copy.dts = av_rescale_q(dts, AVRational{1, TIMEBASE}, avstream_audio->time_base);
98                 pkt_copy.duration = av_rescale_q(pkt.duration, AVRational{1, TIMEBASE}, avstream_audio->time_base);
99         } else {
100                 assert(false);
101         }
102
103         if (keyframe_signal_receiver) {
104                 if (pkt.flags & AV_PKT_FLAG_KEY) {
105                         av_write_frame(avctx, nullptr);
106                         keyframe_signal_receiver->signal_keyframe();
107                 }
108         }
109
110         {
111                 lock_guard<mutex> lock(mu);
112                 if (plug_count > 0) {
113                         plugged_packets.push_back(av_packet_clone(&pkt_copy));
114                 } else if (av_interleaved_write_frame(avctx, &pkt_copy) < 0) {
115                         fprintf(stderr, "av_interleaved_write_frame() failed\n");
116                         exit(1);
117                 }
118         }
119
120         av_packet_unref(&pkt_copy);
121 }
122
123 void Mux::plug()
124 {
125         lock_guard<mutex> lock(mu);
126         ++plug_count;
127 }
128
129 void Mux::unplug()
130 {
131         lock_guard<mutex> lock(mu);
132         if (--plug_count > 0) {
133                 return;
134         }
135         assert(plug_count >= 0);
136
137         sort(plugged_packets.begin(), plugged_packets.end(), [](const AVPacket *a, const AVPacket *b) {
138                 int64_t a_dts = (a->dts == AV_NOPTS_VALUE ? a->pts : a->dts);
139                 int64_t b_dts = (b->dts == AV_NOPTS_VALUE ? b->pts : b->dts);
140                 if (a_dts != b_dts) {
141                         return a_dts < b_dts;
142                 } else {
143                         return a->pts < b->pts;
144                 }
145         });
146
147         for (AVPacket *pkt : plugged_packets) {
148                 if (av_interleaved_write_frame(avctx, pkt) < 0) {
149                         fprintf(stderr, "av_interleaved_write_frame() failed\n");
150                         exit(1);
151                 }
152                 av_packet_free(&pkt);
153         }
154         plugged_packets.clear();
155 }