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