]> git.sesse.net Git - nageru/blob - x264_encoder.h
Write 1.4.0 changelog.
[nageru] / x264_encoder.h
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.
5 //
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.
8 //
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.
15
16 #ifndef _X264ENCODE_H
17 #define _X264ENCODE_H 1
18
19 #include <stdint.h>
20 #include <x264.h>
21 #include <atomic>
22 #include <condition_variable>
23 #include <memory>
24 #include <mutex>
25 #include <queue>
26 #include <string>
27 #include <thread>
28
29 extern "C" {
30 #include <libavformat/avformat.h>
31 }
32
33 class Mux;
34 class X264SpeedControl;
35
36 class X264Encoder {
37 public:
38         X264Encoder(AVOutputFormat *oformat);  // Does not take ownership.
39
40         // Called after the last frame. Will block; once this returns,
41         // the last data is flushed.
42         ~X264Encoder();
43
44         // Must be called before first frame. Does not take ownership.
45         void set_mux(Mux *mux) { this->mux = mux; }
46
47         // <data> is taken to be raw NV12 data of WIDTHxHEIGHT resolution.
48         // Does not block.
49         void add_frame(int64_t pts, int64_t duration, const uint8_t *data);
50
51         std::string get_global_headers() const { return global_headers; }
52
53         void change_bitrate(unsigned rate_kbit) {
54                 new_bitrate_kbit = rate_kbit;
55         }
56
57 private:
58         struct QueuedFrame {
59                 int64_t pts, duration;
60                 uint8_t *data;
61         };
62         void encoder_thread_func();
63         void init_x264();
64         void encode_frame(QueuedFrame qf);
65
66         // One big memory chunk of all 50 (or whatever) frames, allocated in
67         // the constructor. All data functions just use pointers into this
68         // pool.
69         std::unique_ptr<uint8_t[]> frame_pool;
70
71         Mux *mux = nullptr;
72         bool wants_global_headers;
73
74         std::string global_headers;
75         std::string buffered_sei;  // Will be output before first frame, if any.
76
77         std::thread encoder_thread;
78         std::atomic<bool> should_quit{false};
79         x264_t *x264;
80         std::unique_ptr<X264SpeedControl> speed_control;
81
82         std::atomic<unsigned> new_bitrate_kbit{0};  // 0 for no change.
83
84         // Protects everything below it.
85         std::mutex mu;
86
87         // Frames that are not being encoded or waiting to be encoded,
88         // so that add_frame() can use new ones.
89         std::queue<uint8_t *> free_frames;
90
91         // Frames that are waiting to be encoded (ie., add_frame() has been
92         // called, but they are not picked up for encoding yet).
93         std::queue<QueuedFrame> queued_frames;
94
95         // Whenever the state of <queued_frames> changes.
96         std::condition_variable queued_frames_nonempty;
97 };
98
99 #endif  // !defined(_X264ENCODE_H)