8 #include "x264_encoder.h"
11 #include <libavformat/avformat.h>
16 X264Encoder::X264Encoder(AVOutputFormat *oformat)
17 : wants_global_headers(oformat->flags & AVFMT_GLOBALHEADER)
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()
29 queued_frames_nonempty.notify_all();
30 encoder_thread.join();
33 void X264Encoder::add_frame(int64_t pts, int64_t duration, const uint8_t *data)
37 qf.duration = duration;
40 lock_guard<mutex> lock(mu);
41 if (free_frames.empty()) {
42 fprintf(stderr, "WARNING: x264 queue full, dropping frame with pts %ld\n", pts);
46 qf.data = free_frames.front();
50 memcpy(qf.data, data, WIDTH * HEIGHT * 2);
53 lock_guard<mutex> lock(mu);
54 queued_frames.push(qf);
55 queued_frames_nonempty.notify_all();
59 void X264Encoder::init_x264()
62 x264_param_default_preset(¶m, global_flags.x264_preset.c_str(), global_flags.x264_tune.c_str());
64 param.i_width = WIDTH;
65 param.i_height = HEIGHT;
66 param.i_csp = X264_CSP_NV12;
67 param.b_vfr_input = 1;
68 param.i_timebase_num = 1;
69 param.i_timebase_den = TIMEBASE;
70 param.i_keyint_max = 50; // About one second.
72 // NOTE: These should be in sync with the ones in h264encode.cpp (sbs_rbsp()).
73 param.vui.i_vidformat = 5; // Unspecified.
74 param.vui.b_fullrange = 0;
75 param.vui.i_colorprim = 1; // BT.709.
76 param.vui.i_transfer = 2; // Unspecified (since we use sRGB).
77 param.vui.i_colmatrix = 6; // BT.601/SMPTE 170M.
80 param.rc.i_rc_method = X264_RC_ABR;
81 param.rc.i_bitrate = 4500;
84 param.rc.i_vbv_max_bitrate = 4500;
85 param.rc.i_vbv_buffer_size = 4500;
87 // TODO: more flags here, via x264_param_parse().
89 x264_param_apply_profile(¶m, "high");
91 param.b_repeat_headers = !wants_global_headers;
93 x264 = x264_encoder_open(¶m);
94 if (x264 == nullptr) {
95 fprintf(stderr, "ERROR: x264 initialization failed.\n");
99 if (wants_global_headers) {
103 x264_encoder_headers(x264, &nal, &num_nal);
105 for (int i = 0; i < num_nal; ++i) {
106 if (nal[i].i_type == NAL_SEI) {
107 // Don't put the SEI in extradata; make it part of the first frame instead.
108 buffered_sei += string((const char *)nal[i].p_payload, nal[i].i_payload);
110 global_headers += string((const char *)nal[i].p_payload, nal[i].i_payload);
116 void X264Encoder::encoder_thread_func()
118 nice(5); // Note that x264 further nices some of its threads.
126 // Wait for a queued frame, then dequeue it.
128 unique_lock<mutex> lock(mu);
129 queued_frames_nonempty.wait(lock, [this]() { return !queued_frames.empty() || should_quit; });
130 if (!queued_frames.empty()) {
131 qf = queued_frames.front();
139 frames_left = !queued_frames.empty();
145 lock_guard<mutex> lock(mu);
146 free_frames.push(qf.data);
149 // We should quit only if the should_quit flag is set _and_ we have nothing
151 } while (!should_quit || frames_left || x264_encoder_delayed_frames(x264) > 0);
153 x264_encoder_close(x264);
156 void X264Encoder::encode_frame(X264Encoder::QueuedFrame qf)
158 x264_nal_t *nal = nullptr;
163 x264_picture_init(&pic);
166 pic.img.i_csp = X264_CSP_NV12;
168 pic.img.plane[0] = qf.data;
169 pic.img.i_stride[0] = WIDTH;
170 pic.img.plane[1] = qf.data + WIDTH * HEIGHT;
171 pic.img.i_stride[1] = WIDTH / 2 * sizeof(uint16_t);
172 pic.opaque = reinterpret_cast<void *>(intptr_t(qf.duration));
174 x264_encoder_encode(x264, &nal, &num_nal, &pic, &pic);
176 x264_encoder_encode(x264, &nal, &num_nal, nullptr, &pic);
179 // We really need one AVPacket for the entire frame, it seems,
180 // so combine it all.
181 size_t num_bytes = buffered_sei.size();
182 for (int i = 0; i < num_nal; ++i) {
183 num_bytes += nal[i].i_payload;
186 unique_ptr<uint8_t[]> data(new uint8_t[num_bytes]);
187 uint8_t *ptr = data.get();
189 if (!buffered_sei.empty()) {
190 memcpy(ptr, buffered_sei.data(), buffered_sei.size());
191 ptr += buffered_sei.size();
192 buffered_sei.clear();
194 for (int i = 0; i < num_nal; ++i) {
195 memcpy(ptr, nal[i].p_payload, nal[i].i_payload);
196 ptr += nal[i].i_payload;
200 memset(&pkt, 0, sizeof(pkt));
202 pkt.data = data.get();
203 pkt.size = num_bytes;
204 pkt.stream_index = 0;
205 if (pic.b_keyframe) {
206 pkt.flags = AV_PKT_FLAG_KEY;
210 pkt.duration = reinterpret_cast<intptr_t>(pic.opaque);
212 mux->add_packet(pkt, pic.i_pts, pic.i_dts);