]> git.sesse.net Git - nageru/blob - x264encode.cpp
Remove some debugging output.
[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         should_quit = true;
29         encoder_thread.join();
30 }
31
32 void X264Encoder::add_frame(int64_t pts, const uint8_t *data)
33 {
34         QueuedFrame qf;
35         qf.pts = pts;
36
37         {
38                 lock_guard<mutex> lock(mu);
39                 if (free_frames.empty()) {
40                         fprintf(stderr, "WARNING: x264 queue full, dropping frame with pts %ld\n", pts);
41                         return;
42                 }
43
44                 qf.data = free_frames.front();
45                 free_frames.pop();
46         }
47
48         memcpy(qf.data, data, WIDTH * HEIGHT * 2);
49
50         {
51                 lock_guard<mutex> lock(mu);
52                 queued_frames.push(qf);
53                 queued_frames_nonempty.notify_all();
54         }
55 }
56         
57 void X264Encoder::init_x264()
58 {
59         x264_param_t param;
60         x264_param_default_preset(&param, global_flags.x264_preset.c_str(), global_flags.x264_tune.c_str());
61
62         param.i_width = WIDTH;
63         param.i_height = HEIGHT;
64         param.i_csp = X264_CSP_NV12;
65         param.b_vfr_input = 1;
66         param.i_timebase_num = 1;
67         param.i_timebase_den = TIMEBASE;
68         param.i_keyint_max = 50; // About one second.
69
70         // NOTE: These should be in sync with the ones in h264encode.cpp (sbs_rbsp()).
71         param.vui.i_vidformat = 5;  // Unspecified.
72         param.vui.b_fullrange = 0;
73         param.vui.i_colorprim = 1;  // BT.709.
74         param.vui.i_transfer = 2;  // Unspecified (since we use sRGB).
75         param.vui.i_colmatrix = 6;  // BT.601/SMPTE 170M.
76
77         // 4.5 Mbit/sec, CBR.
78         param.rc.i_rc_method = X264_RC_ABR;
79         param.rc.i_bitrate = 4500;
80
81         // One-second VBV.
82         param.rc.i_vbv_max_bitrate = 4500;
83         param.rc.i_vbv_buffer_size = 4500;
84
85         // TODO: more flags here, via x264_param_parse().
86
87         x264_param_apply_profile(&param, "high");
88
89         x264 = x264_encoder_open(&param);
90         if (x264 == nullptr) {
91                 fprintf(stderr, "ERROR: x264 initialization failed.\n");
92                 exit(1);
93         }
94 }
95
96 void X264Encoder::encoder_thread_func()
97 {
98         nice(5);  // Note that x264 further nices some of its threads.
99         init_x264();
100
101         bool frames_left;
102
103         do {
104                 QueuedFrame qf;
105
106                 // Wait for a queued frame, then dequeue it.
107                 {
108                         unique_lock<mutex> lock(mu);
109                         queued_frames_nonempty.wait(lock, [this]() { return !queued_frames.empty() || should_quit; });
110                         if (!queued_frames.empty()) {
111                                 qf = queued_frames.front();
112                                 queued_frames.pop();
113                         } else {
114                                 qf.pts = -1;
115                                 qf.data = nullptr;
116                         }
117
118                         frames_left = !queued_frames.empty();
119                 }
120
121                 encode_frame(qf);
122                 
123                 {
124                         lock_guard<mutex> lock(mu);
125                         free_frames.push(qf.data);
126                 }
127
128                 // We should quit only if the should_quit flag is set _and_ we have nothing
129                 // in either queue.
130         } while (!should_quit || frames_left || x264_encoder_delayed_frames(x264) > 0);
131
132         x264_encoder_close(x264);
133 }
134
135 void X264Encoder::encode_frame(X264Encoder::QueuedFrame qf)
136 {
137         x264_nal_t *nal = nullptr;
138         int num_nal = 0;
139         x264_picture_t pic;
140
141         if (qf.data) {
142                 x264_picture_init(&pic);
143
144                 pic.i_pts = qf.pts;
145                 pic.img.i_csp = X264_CSP_NV12;
146                 pic.img.i_plane = 2;
147                 pic.img.plane[0] = qf.data;
148                 pic.img.i_stride[0] = WIDTH;
149                 pic.img.plane[1] = qf.data + WIDTH * HEIGHT;
150                 pic.img.i_stride[1] = WIDTH / 2 * sizeof(uint16_t);
151
152                 x264_encoder_encode(x264, &nal, &num_nal, &pic, &pic);
153         } else {
154                 x264_encoder_encode(x264, &nal, &num_nal, nullptr, &pic);
155         }
156
157         // We really need one AVPacket for the entire frame, it seems,
158         // so combine it all.
159         size_t num_bytes = 0;
160         for (int i = 0; i < num_nal; ++i) {
161                 num_bytes += nal[i].i_payload;
162         }
163
164         unique_ptr<uint8_t[]> data(new uint8_t[num_bytes]);
165         uint8_t *ptr = data.get();
166
167         for (int i = 0; i < num_nal; ++i) {
168                 memcpy(ptr, nal[i].p_payload, nal[i].i_payload);
169                 ptr += nal[i].i_payload;
170         }
171
172         AVPacket pkt;
173         memset(&pkt, 0, sizeof(pkt));
174         pkt.buf = nullptr;
175         pkt.data = data.get();
176         pkt.size = num_bytes;
177         pkt.stream_index = 0;
178         if (pic.b_keyframe) {
179                 pkt.flags = AV_PKT_FLAG_KEY;
180         } else {
181                 pkt.flags = 0;
182         }
183
184         mux->add_packet(pkt, pic.i_pts, pic.i_dts);
185 }