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