7 #include "x264encode.h"
10 #include <libavformat/avformat.h>
15 X264Encoder::X264Encoder(Mux *mux)
18 frame_pool.reset(new uint8_t[WIDTH * HEIGHT * 2 * X264_QUEUE_LENGTH]);
19 for (unsigned i = 0; i < X264_QUEUE_LENGTH; ++i) {
20 free_frames.push(frame_pool.get() + i * (WIDTH * HEIGHT * 2));
22 encoder_thread = thread(&X264Encoder::encoder_thread_func, this);
25 X264Encoder::~X264Encoder()
30 void X264Encoder::add_frame(int64_t pts, const uint8_t *data)
36 lock_guard<mutex> lock(mu);
37 if (free_frames.empty()) {
38 fprintf(stderr, "WARNING: x264 queue full, dropping frame with pts %ld\n", pts);
42 qf.data = free_frames.front();
46 memcpy(qf.data, data, WIDTH * HEIGHT * 2);
49 lock_guard<mutex> lock(mu);
50 queued_frames.push(qf);
51 queued_frames_nonempty.notify_all();
55 void X264Encoder::end_encoding()
60 void X264Encoder::init_x264()
63 x264_param_default_preset(¶m, "ultrafast", "film"); // TODO: flags
65 param.i_width = WIDTH;
66 param.i_height = HEIGHT;
67 param.i_csp = X264_CSP_NV12;
68 param.b_vfr_input = 1;
69 param.i_timebase_num = 1;
70 param.i_timebase_den = TIMEBASE;
71 param.i_keyint_max = 50; // About one second.
73 // NOTE: These should be in sync with the ones in h264encode.cpp (sbs_rbsp()).
74 param.vui.i_vidformat = 5; // Unspecified.
75 param.vui.b_fullrange = 0;
76 param.vui.i_colorprim = 1; // BT.709.
77 param.vui.i_transfer = 2; // Unspecified (since we use sRGB).
78 param.vui.i_colmatrix = 6; // BT.601/SMPTE 170M.
81 param.rc.i_rc_method = X264_RC_ABR;
82 param.rc.i_bitrate = 4500;
85 param.rc.i_vbv_max_bitrate = 4500;
86 param.rc.i_vbv_buffer_size = 4500;
88 // TODO: more flags here, via x264_param_parse().
90 x264_param_apply_profile(¶m, "high");
92 x264 = x264_encoder_open(¶m);
93 if (x264 == nullptr) {
94 fprintf(stderr, "ERROR: x264 initialization failed.\n");
99 void X264Encoder::encoder_thread_func()
101 nice(5); // Note that x264 further nices some of its threads.
107 // Wait for a queued frame, then dequeue it.
109 unique_lock<mutex> lock(mu);
110 queued_frames_nonempty.wait(lock, [this]() { return !queued_frames.empty(); });
111 qf = queued_frames.front();
118 lock_guard<mutex> lock(mu);
119 free_frames.push(qf.data);
124 void X264Encoder::encode_frame(X264Encoder::QueuedFrame qf)
127 x264_nal_t *nal = nullptr;
130 x264_picture_init(&pic);
132 // TODO: Delayed frames.
134 pic.img.i_csp = X264_CSP_NV12;
136 pic.img.plane[0] = qf.data;
137 pic.img.i_stride[0] = WIDTH;
138 pic.img.plane[1] = qf.data + WIDTH * HEIGHT;
139 pic.img.i_stride[1] = WIDTH / 2 * sizeof(uint16_t);
141 x264_encoder_encode(x264, &nal, &num_nal, &pic, &pic);
143 // We really need one AVPacket for the entire frame, it seems,
144 // so combine it all.
145 size_t num_bytes = 0;
146 for (int i = 0; i < num_nal; ++i) {
147 num_bytes += nal[i].i_payload;
150 unique_ptr<uint8_t[]> data(new uint8_t[num_bytes]);
151 uint8_t *ptr = data.get();
153 for (int i = 0; i < num_nal; ++i) {
154 memcpy(ptr, nal[i].p_payload, nal[i].i_payload);
155 ptr += nal[i].i_payload;
159 memset(&pkt, 0, sizeof(pkt));
161 pkt.data = data.get();
162 pkt.size = num_bytes;
163 pkt.stream_index = 0;
164 if (pic.b_keyframe) {
165 pkt.flags = AV_PKT_FLAG_KEY;
170 mux->add_packet(pkt, pic.i_pts, pic.i_dts);