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 // The encoding threads are niced down because mixing is more important than
7 // encoding; if we lose frames in mixing, we'll lose frames to disk _and_
8 // to the stream, as where if we lose frames in encoding, we'll lose frames
9 // to the stream only, so the latter is strictly better. More importantly,
10 // this allows speedcontrol to do its thing without disturbing the mixer.
13 #define _X264ENCODE_H 1
20 #include <condition_variable>
26 #include <unordered_map>
30 #include <libavformat/avformat.h>
33 #include <movit/image_format.h>
35 #include "print_latency.h"
38 class X264SpeedControl;
42 X264Encoder(AVOutputFormat *oformat); // Does not take ownership.
44 // Called after the last frame. Will block; once this returns,
45 // the last data is flushed.
48 // Must be called before first frame. Does not take ownership.
49 void add_mux(Mux *mux) { muxes.push_back(mux); }
51 // <data> is taken to be raw NV12 data of WIDTHxHEIGHT resolution.
53 void add_frame(int64_t pts, int64_t duration, movit::YCbCrLumaCoefficients ycbcr_coefficients, const uint8_t *data, const ReceivedTimestamps &received_ts);
55 std::string get_global_headers() const {
56 while (!x264_init_done) {
59 return global_headers;
62 void change_bitrate(unsigned rate_kbit) {
63 new_bitrate_kbit = rate_kbit;
68 int64_t pts, duration;
69 movit::YCbCrLumaCoefficients ycbcr_coefficients;
71 ReceivedTimestamps received_ts;
73 void encoder_thread_func();
75 void encode_frame(QueuedFrame qf);
77 // One big memory chunk of all 50 (or whatever) frames, allocated in
78 // the constructor. All data functions just use pointers into this
80 std::unique_ptr<uint8_t[]> frame_pool;
82 std::vector<Mux *> muxes;
83 bool wants_global_headers;
85 std::string global_headers;
86 std::string buffered_sei; // Will be output before first frame, if any.
88 std::thread encoder_thread;
89 std::atomic<bool> x264_init_done{false};
90 std::atomic<bool> should_quit{false};
92 std::unique_ptr<X264SpeedControl> speed_control;
94 std::function<void(x264_param_t *)> bitrate_override_func;
96 std::atomic<unsigned> new_bitrate_kbit{0}; // 0 for no change.
98 // Protects everything below it.
101 // Frames that are not being encoded or waiting to be encoded,
102 // so that add_frame() can use new ones.
103 std::queue<uint8_t *> free_frames;
105 // Frames that are waiting to be encoded (ie., add_frame() has been
106 // called, but they are not picked up for encoding yet).
107 std::queue<QueuedFrame> queued_frames;
109 // Whenever the state of <queued_frames> changes.
110 std::condition_variable queued_frames_nonempty;
112 // Key is the pts of the frame.
113 std::unordered_map<int64_t, ReceivedTimestamps> frames_being_encoded;
116 #endif // !defined(_X264ENCODE_H)