6 #include "x264encode.h"
9 #include <libavformat/avformat.h>
14 X264Encoder::X264Encoder(HTTPD *httpd)
17 frame_pool.reset(new uint8_t[WIDTH * HEIGHT * 2 * X264_QUEUE_LENGTH]);
18 for (unsigned i = 0; i < X264_QUEUE_LENGTH; ++i) {
19 free_frames.push(frame_pool.get() + i * (WIDTH * HEIGHT * 2));
21 encoder_thread = thread(&X264Encoder::encoder_thread_func, this);
24 X264Encoder::~X264Encoder()
29 void X264Encoder::add_frame(int64_t pts, const uint8_t *data)
35 lock_guard<mutex> lock(mu);
36 if (free_frames.empty()) {
37 fprintf(stderr, "WARNING: x264 queue full, dropping frame with pts %ld\n", pts);
41 qf.data = free_frames.front();
45 memcpy(qf.data, data, WIDTH * HEIGHT * 2);
48 lock_guard<mutex> lock(mu);
49 queued_frames.push(qf);
50 queued_frames_nonempty.notify_all();
54 void X264Encoder::end_encoding()
59 void X264Encoder::init_x264()
62 x264_param_default_preset(¶m, "ultrafast", "film"); // TODO: flags
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 x264 = x264_encoder_open(¶m);
92 if (x264 == nullptr) {
93 fprintf(stderr, "ERROR: x264 initialization failed.\n");
98 void X264Encoder::encoder_thread_func()
100 nice(5); // Note that x264 further nices some of its threads.
106 // Wait for a queued frame, then dequeue it.
108 unique_lock<mutex> lock(mu);
109 queued_frames_nonempty.wait(lock, [this]() { return !queued_frames.empty(); });
110 qf = queued_frames.front();
117 lock_guard<mutex> lock(mu);
118 free_frames.push(qf.data);
123 void X264Encoder::encode_frame(X264Encoder::QueuedFrame qf)
126 x264_nal_t *nal = nullptr;
129 x264_picture_init(&pic);
131 // TODO: Delayed frames.
133 pic.img.i_csp = X264_CSP_NV12;
135 pic.img.plane[0] = qf.data;
136 pic.img.i_stride[0] = WIDTH;
137 pic.img.plane[1] = qf.data + WIDTH * HEIGHT;
138 pic.img.i_stride[1] = WIDTH / 2 * sizeof(uint16_t);
140 x264_encoder_encode(x264, &nal, &num_nal, &pic, &pic);
142 // We really need one AVPacket for the entire frame, it seems,
143 // so combine it all.
144 size_t num_bytes = 0;
145 for (int i = 0; i < num_nal; ++i) {
146 num_bytes += nal[i].i_payload;
149 unique_ptr<uint8_t[]> data(new uint8_t[num_bytes]);
150 uint8_t *ptr = data.get();
152 for (int i = 0; i < num_nal; ++i) {
153 memcpy(ptr, nal[i].p_payload, nal[i].i_payload);
154 ptr += nal[i].i_payload;
158 memset(&pkt, 0, sizeof(pkt));
160 pkt.data = data.get();
161 pkt.size = num_bytes;
162 pkt.stream_index = 0;
163 if (pic.b_keyframe) {
164 pkt.flags = AV_PKT_FLAG_KEY;
169 httpd->add_packet(pkt, pic.i_pts, pic.i_dts);