]> git.sesse.net Git - nageru/blob - mux.cpp
Scale duration correctly.
[nageru] / mux.cpp
1 #include <assert.h>
2
3 #include <mutex>
4 #include <string>
5 #include <vector>
6
7 #include "defs.h"
8 #include "mux.h"
9 #include "timebase.h"
10
11 using namespace std;
12
13 Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const AVCodec *codec_audio, int time_base, int bit_rate, KeyFrameSignalReceiver *keyframe_signal_receiver)
14         : avctx(avctx), keyframe_signal_receiver(keyframe_signal_receiver)
15 {
16         AVCodec *codec_video = avcodec_find_encoder((video_codec == CODEC_H264) ? AV_CODEC_ID_H264 : AV_CODEC_ID_RAWVIDEO);
17         avstream_video = avformat_new_stream(avctx, codec_video);
18         if (avstream_video == nullptr) {
19                 fprintf(stderr, "avformat_new_stream() failed\n");
20                 exit(1);
21         }
22         avstream_video->time_base = AVRational{1, time_base};
23         avstream_video->codec->codec_type = AVMEDIA_TYPE_VIDEO;
24         if (video_codec == CODEC_H264) {
25                 avstream_video->codec->codec_id = AV_CODEC_ID_H264;
26         } else {
27                 assert(video_codec == CODEC_NV12);
28                 avstream_video->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
29                 avstream_video->codec->codec_tag = avcodec_pix_fmt_to_codec_tag(AV_PIX_FMT_NV12);
30         }
31         avstream_video->codec->width = width;
32         avstream_video->codec->height = height;
33         avstream_video->codec->time_base = AVRational{1, time_base};
34         avstream_video->codec->ticks_per_frame = 1;  // or 2?
35
36         // Colorspace details. Closely correspond to settings in EffectChain_finalize,
37         // as noted in each comment.
38         // Note that the H.264 stream also contains this information and depending on the
39         // mux, this might simply get ignored. See sps_rbsp().
40         avstream_video->codec->color_primaries = AVCOL_PRI_BT709;  // RGB colorspace (inout_format.color_space).
41         avstream_video->codec->color_trc = AVCOL_TRC_UNSPECIFIED;  // Gamma curve (inout_format.gamma_curve).
42         avstream_video->codec->colorspace = AVCOL_SPC_SMPTE170M;  // YUV colorspace (output_ycbcr_format.luma_coefficients).
43         avstream_video->codec->color_range = AVCOL_RANGE_MPEG;  // Full vs. limited range (output_ycbcr_format.full_range).
44         avstream_video->codec->chroma_sample_location = AVCHROMA_LOC_LEFT;  // Chroma sample location. See chroma_offset_0[] in Mixer::subsample_chroma().
45         avstream_video->codec->field_order = AV_FIELD_PROGRESSIVE;
46         if (avctx->oformat->flags & AVFMT_GLOBALHEADER) {
47                 avstream_video->codec->flags = AV_CODEC_FLAG_GLOBAL_HEADER;
48         }
49
50         avstream_audio = avformat_new_stream(avctx, codec_audio);
51         if (avstream_audio == nullptr) {
52                 fprintf(stderr, "avformat_new_stream() failed\n");
53                 exit(1);
54         }
55         avstream_audio->time_base = AVRational{1, time_base};
56         avstream_audio->codec->bit_rate = bit_rate;
57         avstream_audio->codec->sample_rate = OUTPUT_FREQUENCY;
58         avstream_audio->codec->channels = 2;
59         avstream_audio->codec->channel_layout = AV_CH_LAYOUT_STEREO;
60         avstream_audio->codec->time_base = AVRational{1, time_base};
61         if (avctx->oformat->flags & AVFMT_GLOBALHEADER) {
62                 avstream_audio->codec->flags = AV_CODEC_FLAG_GLOBAL_HEADER;
63         }
64
65         AVDictionary *options = NULL;
66         vector<pair<string, string>> opts = MUX_OPTS;
67         for (pair<string, string> opt : opts) {
68                 av_dict_set(&options, opt.first.c_str(), opt.second.c_str(), 0);
69         }
70         if (avformat_write_header(avctx, &options) < 0) {
71                 fprintf(stderr, "avformat_write_header() failed\n");
72                 exit(1);
73         }
74
75         // Make sure the header is written before the constructor exits.
76         avio_flush(avctx->pb);
77 }
78
79 Mux::~Mux()
80 {
81         av_write_trailer(avctx);
82         av_free(avctx->pb->buffer);
83         av_free(avctx->pb);
84         avformat_free_context(avctx);
85 }
86
87 void Mux::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts)
88 {
89         AVPacket pkt_copy;
90         if (av_copy_packet(&pkt_copy, &pkt) < 0) {
91                 fprintf(stderr, "av_copy_packet() failed\n");
92                 exit(1);
93         }
94         if (pkt.stream_index == 0) {
95                 pkt_copy.pts = av_rescale_q(pts, AVRational{1, TIMEBASE}, avstream_video->time_base);
96                 pkt_copy.dts = av_rescale_q(dts, AVRational{1, TIMEBASE}, avstream_video->time_base);
97                 pkt_copy.duration = av_rescale_q(pkt.duration, AVRational{1, TIMEBASE}, avstream_video->time_base);
98         } else if (pkt.stream_index == 1) {
99                 pkt_copy.pts = av_rescale_q(pts, AVRational{1, TIMEBASE}, avstream_audio->time_base);
100                 pkt_copy.dts = av_rescale_q(dts, AVRational{1, TIMEBASE}, avstream_audio->time_base);
101                 pkt_copy.duration = av_rescale_q(pkt.duration, AVRational{1, TIMEBASE}, avstream_audio->time_base);
102         } else {
103                 assert(false);
104         }
105
106         if (keyframe_signal_receiver) {
107                 if (pkt.flags & AV_PKT_FLAG_KEY) {
108                         if (avctx->oformat->flags & AVFMT_ALLOW_FLUSH) {
109                                 av_write_frame(avctx, nullptr);
110                         }
111                         keyframe_signal_receiver->signal_keyframe();
112                 }
113         }
114
115         {
116                 lock_guard<mutex> lock(ctx_mu);
117                 if (av_interleaved_write_frame(avctx, &pkt_copy) < 0) {
118                         fprintf(stderr, "av_interleaved_write_frame() failed\n");
119                         exit(1);
120                 }
121         }
122
123         av_packet_unref(&pkt_copy);
124 }