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