]> git.sesse.net Git - nageru/blob - x264encode.cpp
Make it possible to set x264 preset/tune from the command line.
[nageru] / x264encode.cpp
1 #include <string.h>
2 #include <unistd.h>
3
4 #include "defs.h"
5 #include "flags.h"
6 #include "mux.h"
7 #include "timebase.h"
8 #include "x264encode.h"
9
10 extern "C" {
11 #include <libavformat/avformat.h>
12 }
13
14 using namespace std;
15
16 X264Encoder::X264Encoder(Mux *mux)
17         : mux(mux)
18 {
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));
22         }
23         encoder_thread = thread(&X264Encoder::encoder_thread_func, this);
24 }
25
26 X264Encoder::~X264Encoder()
27 {
28         // TODO: close x264
29 }
30
31 void X264Encoder::add_frame(int64_t pts, const uint8_t *data)
32 {
33         QueuedFrame qf;
34         qf.pts = pts;
35
36         {
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);
40                         return;
41                 }
42
43                 qf.data = free_frames.front();
44                 free_frames.pop();
45         }
46
47         memcpy(qf.data, data, WIDTH * HEIGHT * 2);
48
49         {
50                 lock_guard<mutex> lock(mu);
51                 queued_frames.push(qf);
52                 queued_frames_nonempty.notify_all();
53         }
54 }
55         
56 void X264Encoder::end_encoding()
57 {
58         // TODO
59 }
60
61 void X264Encoder::init_x264()
62 {
63         x264_param_t param;
64         x264_param_default_preset(&param, global_flags.x264_preset.c_str(), global_flags.x264_tune.c_str());
65
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.
73
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.
80
81         // 4.5 Mbit/sec, CBR.
82         param.rc.i_rc_method = X264_RC_ABR;
83         param.rc.i_bitrate = 4500;
84
85         // One-second VBV.
86         param.rc.i_vbv_max_bitrate = 4500;
87         param.rc.i_vbv_buffer_size = 4500;
88
89         // TODO: more flags here, via x264_param_parse().
90
91         x264_param_apply_profile(&param, "high");
92
93         x264 = x264_encoder_open(&param);
94         if (x264 == nullptr) {
95                 fprintf(stderr, "ERROR: x264 initialization failed.\n");
96                 exit(1);
97         }
98 }
99
100 void X264Encoder::encoder_thread_func()
101 {
102         nice(5);  // Note that x264 further nices some of its threads.
103         init_x264();
104
105         for ( ;; ) {
106                 QueuedFrame qf;
107
108                 // Wait for a queued frame, then dequeue it.
109                 {
110                         unique_lock<mutex> lock(mu);
111                         queued_frames_nonempty.wait(lock, [this]() { return !queued_frames.empty(); });
112                         qf = queued_frames.front();
113                         queued_frames.pop();
114                 }
115
116                 encode_frame(qf);
117                 
118                 {
119                         lock_guard<mutex> lock(mu);
120                         free_frames.push(qf.data);
121                 }
122         }
123 }
124
125 void X264Encoder::encode_frame(X264Encoder::QueuedFrame qf)
126 {
127         x264_picture_t pic;
128         x264_nal_t *nal = nullptr;
129         int num_nal = 0;
130
131         x264_picture_init(&pic);
132
133         // TODO: Delayed frames.
134         pic.i_pts = qf.pts;
135         pic.img.i_csp = X264_CSP_NV12;
136         pic.img.i_plane = 2;
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);
141
142         x264_encoder_encode(x264, &nal, &num_nal, &pic, &pic);
143
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;
149         }
150
151         unique_ptr<uint8_t[]> data(new uint8_t[num_bytes]);
152         uint8_t *ptr = data.get();
153
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;
157         }
158
159         AVPacket pkt;
160         memset(&pkt, 0, sizeof(pkt));
161         pkt.buf = nullptr;
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;
167         } else {
168                 pkt.flags = 0;
169         }
170
171         mux->add_packet(pkt, pic.i_pts, pic.i_dts);
172 }