]> git.sesse.net Git - nageru/blob - quicksync_encoder_impl.h
In QuickSyncEncoderImpl, make RefPicList* local variables; no need to hold permanent...
[nageru] / quicksync_encoder_impl.h
1 #ifndef _QUICKSYNC_ENCODER_IMPL_H
2 #define _QUICKSYNC_ENCODER_IMPL_H 1
3
4 #include <epoxy/egl.h>
5 #include <va/va.h>
6
7 #include <condition_variable>
8 #include <map>
9 #include <memory>
10 #include <mutex>
11 #include <queue>
12 #include <string>
13 #include <stack>
14 #include <thread>
15
16 #include "audio_encoder.h"
17 #include "defs.h"
18 #include "timebase.h"
19 #include "print_latency.h"
20
21 #define SURFACE_NUM 16 /* 16 surfaces for source YUV */
22 #define MAX_NUM_REF1 16 // Seemingly a hardware-fixed value, not related to SURFACE_NUM
23 #define MAX_NUM_REF2 32 // Seemingly a hardware-fixed value, not related to SURFACE_NUM
24
25 struct __bitstream {
26     unsigned int *buffer;
27     int bit_offset;
28     int max_size_in_dword;
29 };
30 typedef struct __bitstream bitstream;
31
32 class QuickSyncEncoderImpl {
33 public:
34         QuickSyncEncoderImpl(const std::string &filename, movit::ResourcePool *resource_pool, QSurface *surface, const std::string &va_display, int width, int height, AVOutputFormat *oformat, X264Encoder *x264_encoder, DiskSpaceEstimator *disk_space_estimator);
35         ~QuickSyncEncoderImpl();
36         void add_audio(int64_t pts, std::vector<float> audio);
37         bool begin_frame(GLuint *y_tex, GLuint *cbcr_tex);
38         RefCountedGLsync end_frame(int64_t pts, int64_t duration, const std::vector<RefCountedFrame> &input_frames);
39         void shutdown();
40         void release_gl_resources();
41         void set_stream_mux(Mux *mux)
42         {
43                 stream_mux = mux;
44         }
45
46         // So we never get negative dts.
47         int64_t global_delay() const {
48                 return int64_t(ip_period - 1) * (TIMEBASE / MAX_FPS);
49         }
50
51 private:
52         struct storage_task {
53                 unsigned long long display_order;
54                 int frame_type;
55                 std::vector<float> audio;
56                 int64_t pts, dts, duration;
57                 ReceivedTimestamps received_ts;
58         };
59         struct PendingFrame {
60                 RefCountedGLsync fence;
61                 std::vector<RefCountedFrame> input_frames;
62                 int64_t pts, duration;
63         };
64
65         void open_output_file(const std::string &filename);
66         void encode_thread_func();
67         void encode_remaining_frames_as_p(int encoding_frame_num, int gop_start_display_frame_num, int64_t last_dts);
68         void add_packet_for_uncompressed_frame(int64_t pts, int64_t duration, const uint8_t *data);
69         void pass_frame(PendingFrame frame, int display_frame_num, int64_t pts, int64_t duration);
70         void encode_frame(PendingFrame frame, int encoding_frame_num, int display_frame_num, int gop_start_display_frame_num,
71                           int frame_type, int64_t pts, int64_t dts, int64_t duration);
72         void storage_task_thread();
73         void storage_task_enqueue(storage_task task);
74         void save_codeddata(storage_task task);
75         int render_packedsequence();
76         int render_packedpicture();
77         void render_packedslice();
78         int render_sequence();
79         int render_picture(int frame_type, int display_frame_num, int gop_start_display_frame_num);
80         void sps_rbsp(bitstream *bs);
81         void pps_rbsp(bitstream *bs);
82         int build_packed_pic_buffer(unsigned char **header_buffer);
83         int render_slice(int encoding_frame_num, int display_frame_num, int gop_start_display_frame_num, int frame_type);
84         void slice_header(bitstream *bs);
85         int build_packed_seq_buffer(unsigned char **header_buffer);
86         int build_packed_slice_buffer(unsigned char **header_buffer);
87         int init_va(const std::string &va_display);
88         int deinit_va();
89         void enable_zerocopy_if_possible();
90         VADisplay va_open_display(const std::string &va_display);
91         void va_close_display(VADisplay va_dpy);
92         int setup_encode();
93         void release_encode();
94         void update_ReferenceFrames(int frame_type);
95         void update_RefPicList_P(VAPictureH264 RefPicList0_P[MAX_NUM_REF2]);
96         void update_RefPicList_B(VAPictureH264 RefPicList0_B[MAX_NUM_REF2], VAPictureH264 RefPicList1_B[MAX_NUM_REF2]);
97
98         bool is_shutdown = false;
99         bool has_released_gl_resources = false;
100         bool use_zerocopy;
101         int drm_fd = -1;
102
103         std::thread encode_thread, storage_thread;
104
105         std::mutex storage_task_queue_mutex;
106         std::condition_variable storage_task_queue_changed;
107         int srcsurface_status[SURFACE_NUM];  // protected by storage_task_queue_mutex
108         std::queue<storage_task> storage_task_queue;  // protected by storage_task_queue_mutex
109         bool storage_thread_should_quit = false;  // protected by storage_task_queue_mutex
110
111         std::mutex frame_queue_mutex;
112         std::condition_variable frame_queue_nonempty;
113         bool encode_thread_should_quit = false;  // under frame_queue_mutex
114
115         int current_storage_frame;
116
117         std::queue<PendingFrame> pending_video_frames;  // under frame_queue_mutex
118         movit::ResourcePool *resource_pool;
119         QSurface *surface;
120
121         // Frames that are done rendering and passed on to x264 (if enabled),
122         // but have not been encoded by Quick Sync yet, and thus also not freed.
123         // The key is the display frame number.
124         std::map<int, PendingFrame> reorder_buffer;
125         int quicksync_encoding_frame_num = 0;
126
127         std::unique_ptr<AudioEncoder> file_audio_encoder;
128
129         X264Encoder *x264_encoder;  // nullptr if not using x264.
130
131         Mux* stream_mux = nullptr;  // To HTTP.
132         std::unique_ptr<Mux> file_mux;  // To local disk.
133
134         Display *x11_display = nullptr;
135
136         // Encoder parameters
137         VADisplay va_dpy;
138         VAProfile h264_profile = (VAProfile)~0;
139         VAConfigAttrib config_attrib[VAConfigAttribTypeMax];
140         int config_attrib_num = 0, enc_packed_header_idx;
141
142         struct GLSurface {
143                 VASurfaceID src_surface, ref_surface;
144                 VABufferID coded_buf;
145
146                 VAImage surface_image;
147                 GLuint y_tex, cbcr_tex;
148
149                 // Only if use_zerocopy == true.
150                 EGLImage y_egl_image, cbcr_egl_image;
151
152                 // Only if use_zerocopy == false.
153                 GLuint pbo;
154                 uint8_t *y_ptr, *cbcr_ptr;
155                 size_t y_offset, cbcr_offset;
156         };
157         GLSurface gl_surfaces[SURFACE_NUM];
158
159         VAConfigID config_id;
160         VAContextID context_id;
161         VAEncSequenceParameterBufferH264 seq_param;
162         VAEncPictureParameterBufferH264 pic_param;
163         VAEncSliceParameterBufferH264 slice_param;
164         VAPictureH264 CurrentCurrPic;
165         VAPictureH264 ReferenceFrames[MAX_NUM_REF1];
166
167         // Static quality settings.
168         static constexpr unsigned int frame_bitrate = 15000000 / 60;  // Doesn't really matter; only initial_qp does.
169         static constexpr unsigned int num_ref_frames = 2;
170         static constexpr int initial_qp = 15;
171         static constexpr int minimal_qp = 0;
172         static constexpr int intra_period = 30;
173         static constexpr int intra_idr_period = MAX_FPS;  // About a second; more at lower frame rates. Not ideal.
174
175         // Quality settings that are meant to be static, but might be overridden
176         // by the profile.
177         int constraint_set_flag = 0;
178         int h264_packedheader = 0; /* support pack header? */
179         int h264_maxref = (1<<16|1);
180         int h264_entropy_mode = 1; /* cabac */
181         int ip_period = 3;
182
183         int rc_mode = -1;
184         unsigned int current_frame_num = 0;
185         unsigned int numShortTerm = 0;
186
187         int frame_width;
188         int frame_height;
189         int frame_width_mbaligned;
190         int frame_height_mbaligned;
191
192         DiskSpaceEstimator *disk_space_estimator;
193 };
194
195 #endif  // !defined(_QUICKSYNC_ENCODER_IMPL_H)