]> git.sesse.net Git - nageru/blob - x264encode.cpp
4efa3774489d5e564ebbbf8f9f8df79930a19fbd
[nageru] / x264encode.cpp
1 #include <string.h>
2
3 #include "defs.h"
4 #include "httpd.h"
5 #include "timebase.h"
6 #include "x264encode.h"
7
8 extern "C" {
9 #include <libavformat/avformat.h>
10 }
11
12 using namespace std;
13
14 X264Encoder::X264Encoder(HTTPD *httpd)
15         : httpd(httpd)
16 {
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));
20         }
21         encoder_thread = thread(&X264Encoder::encoder_thread_func, this);
22 }
23
24 X264Encoder::~X264Encoder()
25 {
26         // TODO: close x264
27 }
28
29 void X264Encoder::add_frame(int64_t pts, const uint8_t *data)
30 {
31         QueuedFrame qf;
32         qf.pts = pts;
33
34         {
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);
38                         return;
39                 }
40
41                 qf.data = free_frames.front();
42                 free_frames.pop();
43         }
44
45         memcpy(qf.data, data, WIDTH * HEIGHT * 2);
46
47         {
48                 lock_guard<mutex> lock(mu);
49                 queued_frames.push(qf);
50                 queued_frames_nonempty.notify_all();
51         }
52 }
53         
54 void X264Encoder::end_encoding()
55 {
56         // TODO
57 }
58
59 void X264Encoder::init_x264()
60 {
61         x264_param_t param;
62         x264_param_default_preset(&param, "ultrafast", "film");  // TODO: flags
63
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.
71
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.
78
79         // 4.5 Mbit/sec, CBR.
80         param.rc.i_rc_method = X264_RC_ABR;
81         param.rc.i_bitrate = 4500;
82
83         // One-second VBV.
84         param.rc.i_vbv_max_bitrate = 4500;
85         param.rc.i_vbv_buffer_size = 4500;
86
87         // TODO: more flags here, via x264_param_parse().
88
89         x264_param_apply_profile(&param, "high");
90
91         x264 = x264_encoder_open(&param);
92         if (x264 == nullptr) {
93                 fprintf(stderr, "ERROR: x264 initialization failed.\n");
94                 exit(1);
95         }
96 }
97
98 void X264Encoder::encoder_thread_func()
99 {
100         nice(5);  // Note that x264 further nices some of its threads.
101         init_x264();
102
103         for ( ;; ) {
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(); });
110                         qf = queued_frames.front();
111                         queued_frames.pop();
112                 }
113
114                 encode_frame(qf);
115                 
116                 {
117                         lock_guard<mutex> lock(mu);
118                         free_frames.push(qf.data);
119                 }
120         }
121 }
122
123 void X264Encoder::encode_frame(X264Encoder::QueuedFrame qf)
124 {
125         x264_picture_t pic;
126         x264_nal_t *nal = nullptr;
127         int num_nal = 0;
128
129         x264_picture_init(&pic);
130
131         // TODO: Delayed frames.
132         pic.i_pts = qf.pts;
133         pic.img.i_csp = X264_CSP_NV12;
134         pic.img.i_plane = 2;
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);
139
140         x264_encoder_encode(x264, &nal, &num_nal, &pic, &pic);
141
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;
147         }
148
149         unique_ptr<uint8_t[]> data(new uint8_t[num_bytes]);
150         uint8_t *ptr = data.get();
151
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;
155         }
156
157         AVPacket pkt;
158         memset(&pkt, 0, sizeof(pkt));
159         pkt.buf = nullptr;
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;
165         } else {
166                 pkt.flags = 0;
167         }
168
169         httpd->add_packet(pkt, pic.i_pts, pic.i_dts);
170 }