1 // A wrapper around x264, to encode video in higher quality than Quick Sync
2 // can give us. We maintain a queue of uncompressed Y'CbCr frames (of 50 frames,
3 // so a little under 100 MB at 720p), then have a separate thread pull out
4 // those threads as fast as we can to give it to x264 for encoding.
6 // TODO: We use x264's “speedcontrol” patch if available, so that quality is
7 // automatically scaled up or down to content and available CPU time.
9 // The encoding threads are niced down because mixing is more important than
10 // encoding; if we lose frames in mixing, we'll lose frames to disk _and_
11 // to the stream, as where if we lose frames in encoding, we'll lose frames
12 // to the stream only, so the latter is strictly better. More importantly,
13 // this allows speedcontrol (when implemented) to do its thing without
14 // disturbing the mixer.
17 #define _X264ENCODE_H 1
24 #include <condition_variable>
30 #include <unordered_map>
33 #include <libavformat/avformat.h>
36 #include <movit/image_format.h>
38 #include "print_latency.h"
41 class X264SpeedControl;
45 X264Encoder(AVOutputFormat *oformat); // Does not take ownership.
47 // Called after the last frame. Will block; once this returns,
48 // the last data is flushed.
51 // Must be called before first frame. Does not take ownership.
52 void set_mux(Mux *mux) { this->mux = mux; }
54 // <data> is taken to be raw NV12 data of WIDTHxHEIGHT resolution.
56 void add_frame(int64_t pts, int64_t duration, movit::YCbCrLumaCoefficients ycbcr_coefficients, const uint8_t *data, const ReceivedTimestamps &received_ts);
58 std::string get_global_headers() const {
59 while (!x264_init_done) {
62 return global_headers;
65 void change_bitrate(unsigned rate_kbit) {
66 new_bitrate_kbit = rate_kbit;
71 int64_t pts, duration;
72 movit::YCbCrLumaCoefficients ycbcr_coefficients;
74 ReceivedTimestamps received_ts;
76 void encoder_thread_func();
78 void encode_frame(QueuedFrame qf);
80 // One big memory chunk of all 50 (or whatever) frames, allocated in
81 // the constructor. All data functions just use pointers into this
83 std::unique_ptr<uint8_t[]> frame_pool;
86 bool wants_global_headers;
88 std::string global_headers;
89 std::string buffered_sei; // Will be output before first frame, if any.
91 std::thread encoder_thread;
92 std::atomic<bool> x264_init_done{false};
93 std::atomic<bool> should_quit{false};
95 std::unique_ptr<X264SpeedControl> speed_control;
97 std::function<void(x264_param_t *)> bitrate_override_func;
99 std::atomic<unsigned> new_bitrate_kbit{0}; // 0 for no change.
101 // Protects everything below it.
104 // Frames that are not being encoded or waiting to be encoded,
105 // so that add_frame() can use new ones.
106 std::queue<uint8_t *> free_frames;
108 // Frames that are waiting to be encoded (ie., add_frame() has been
109 // called, but they are not picked up for encoding yet).
110 std::queue<QueuedFrame> queued_frames;
112 // Whenever the state of <queued_frames> changes.
113 std::condition_variable queued_frames_nonempty;
115 // Key is the pts of the frame.
116 std::unordered_map<int64_t, ReceivedTimestamps> frames_being_encoded;
119 #endif // !defined(_X264ENCODE_H)