8 #include "x264encode.h"
11 #include <libavformat/avformat.h>
16 X264Encoder::X264Encoder(Mux *mux)
19 frame_pool.reset(new uint8_t[WIDTH * HEIGHT * 2 * X264_QUEUE_LENGTH]);
20 for (unsigned i = 0; i < X264_QUEUE_LENGTH; ++i) {
21 free_frames.push(frame_pool.get() + i * (WIDTH * HEIGHT * 2));
23 encoder_thread = thread(&X264Encoder::encoder_thread_func, this);
26 X264Encoder::~X264Encoder()
31 void X264Encoder::add_frame(int64_t pts, const uint8_t *data)
37 lock_guard<mutex> lock(mu);
38 if (free_frames.empty()) {
39 fprintf(stderr, "WARNING: x264 queue full, dropping frame with pts %ld\n", pts);
43 qf.data = free_frames.front();
47 memcpy(qf.data, data, WIDTH * HEIGHT * 2);
50 lock_guard<mutex> lock(mu);
51 queued_frames.push(qf);
52 queued_frames_nonempty.notify_all();
56 void X264Encoder::end_encoding()
61 void X264Encoder::init_x264()
64 x264_param_default_preset(¶m, global_flags.x264_preset.c_str(), global_flags.x264_tune.c_str());
66 param.i_width = WIDTH;
67 param.i_height = HEIGHT;
68 param.i_csp = X264_CSP_NV12;
69 param.b_vfr_input = 1;
70 param.i_timebase_num = 1;
71 param.i_timebase_den = TIMEBASE;
72 param.i_keyint_max = 50; // About one second.
74 // NOTE: These should be in sync with the ones in h264encode.cpp (sbs_rbsp()).
75 param.vui.i_vidformat = 5; // Unspecified.
76 param.vui.b_fullrange = 0;
77 param.vui.i_colorprim = 1; // BT.709.
78 param.vui.i_transfer = 2; // Unspecified (since we use sRGB).
79 param.vui.i_colmatrix = 6; // BT.601/SMPTE 170M.
82 param.rc.i_rc_method = X264_RC_ABR;
83 param.rc.i_bitrate = 4500;
86 param.rc.i_vbv_max_bitrate = 4500;
87 param.rc.i_vbv_buffer_size = 4500;
89 // TODO: more flags here, via x264_param_parse().
91 x264_param_apply_profile(¶m, "high");
93 x264 = x264_encoder_open(¶m);
94 if (x264 == nullptr) {
95 fprintf(stderr, "ERROR: x264 initialization failed.\n");
100 void X264Encoder::encoder_thread_func()
102 nice(5); // Note that x264 further nices some of its threads.
108 // Wait for a queued frame, then dequeue it.
110 unique_lock<mutex> lock(mu);
111 queued_frames_nonempty.wait(lock, [this]() { return !queued_frames.empty(); });
112 qf = queued_frames.front();
119 lock_guard<mutex> lock(mu);
120 free_frames.push(qf.data);
125 void X264Encoder::encode_frame(X264Encoder::QueuedFrame qf)
128 x264_nal_t *nal = nullptr;
131 x264_picture_init(&pic);
133 // TODO: Delayed frames.
135 pic.img.i_csp = X264_CSP_NV12;
137 pic.img.plane[0] = qf.data;
138 pic.img.i_stride[0] = WIDTH;
139 pic.img.plane[1] = qf.data + WIDTH * HEIGHT;
140 pic.img.i_stride[1] = WIDTH / 2 * sizeof(uint16_t);
142 x264_encoder_encode(x264, &nal, &num_nal, &pic, &pic);
144 // We really need one AVPacket for the entire frame, it seems,
145 // so combine it all.
146 size_t num_bytes = 0;
147 for (int i = 0; i < num_nal; ++i) {
148 num_bytes += nal[i].i_payload;
151 unique_ptr<uint8_t[]> data(new uint8_t[num_bytes]);
152 uint8_t *ptr = data.get();
154 for (int i = 0; i < num_nal; ++i) {
155 memcpy(ptr, nal[i].p_payload, nal[i].i_payload);
156 ptr += nal[i].i_payload;
160 memset(&pkt, 0, sizeof(pkt));
162 pkt.data = data.get();
163 pkt.size = num_bytes;
164 pkt.stream_index = 0;
165 if (pic.b_keyframe) {
166 pkt.flags = AV_PKT_FLAG_KEY;
171 mux->add_packet(pkt, pic.i_pts, pic.i_dts);