]> git.sesse.net Git - nageru/blobdiff - quicksync_encoder_impl.h
Add the GPU memory metrics to the Grafana dashboard.
[nageru] / quicksync_encoder_impl.h
index 18461328a6def27b1264e980bcace8c546cf2592..41f7e32345f16fd1ba19f4cd1de637459c948659 100644 (file)
@@ -31,11 +31,22 @@ struct __bitstream {
 };
 typedef struct __bitstream bitstream;
 
+struct VADisplayWithCleanup {
+       ~VADisplayWithCleanup();
+
+       VADisplay va_dpy;
+       Display *x11_display = nullptr;
+       bool can_use_zerocopy = true;
+       int drm_fd = -1;
+};
+std::unique_ptr<VADisplayWithCleanup> va_open_display(const std::string &va_display);  // Can return nullptr on failure.
+
 class QuickSyncEncoderImpl {
 public:
        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);
        ~QuickSyncEncoderImpl();
        void add_audio(int64_t pts, std::vector<float> audio);
+       bool is_zerocopy() const;
        bool begin_frame(int64_t pts, int64_t duration, movit::YCbCrLumaCoefficients ycbcr_coefficients, const std::vector<RefCountedFrame> &input_frames, GLuint *y_tex, GLuint *cbcr_tex);
        RefCountedGLsync end_frame();
        void shutdown();
@@ -68,14 +79,13 @@ private:
                movit::YCbCrLumaCoefficients ycbcr_coefficients;
        };
        struct GLSurface {
-               GLuint y_tex, cbcr_tex;
-
                // Only if x264_video_to_disk == false.
                VASurfaceID src_surface, ref_surface;
                VABufferID coded_buf;
                VAImage surface_image;
 
                // Only if use_zerocopy == true (which implies x264_video_to_disk == false).
+               GLuint y_tex, cbcr_tex;
                EGLImage y_egl_image, cbcr_egl_image;
 
                // Only if use_zerocopy == false.
@@ -121,10 +131,7 @@ private:
        int build_packed_seq_buffer(movit::YCbCrLumaCoefficients ycbcr_coefficients, unsigned char **header_buffer);
        int build_packed_slice_buffer(unsigned char **header_buffer);
        int init_va(const std::string &va_display);
-       int deinit_va();
        void enable_zerocopy_if_possible();
-       VADisplay va_open_display(const std::string &va_display);
-       void va_close_display(VADisplay va_dpy);
        int setup_encode();
        void release_encode();
        void update_ReferenceFrames(int current_display_frame, int frame_type);
@@ -135,8 +142,7 @@ private:
 
        bool is_shutdown = false;
        bool has_released_gl_resources = false;
-       bool use_zerocopy;
-       int drm_fd = -1;
+       std::atomic<bool> use_zerocopy{false};
 
        std::thread encode_thread, storage_thread;
 
@@ -162,6 +168,7 @@ private:
        std::map<int, PendingFrame> reorder_buffer;
        int quicksync_encoding_frame_num = 0;
 
+       std::mutex file_audio_encoder_mutex;
        std::unique_ptr<AudioEncoder> file_audio_encoder;
 
        X264Encoder *x264_encoder;  // nullptr if not using x264.
@@ -169,10 +176,8 @@ private:
        Mux* stream_mux = nullptr;  // To HTTP.
        std::unique_ptr<Mux> file_mux;  // To local disk.
 
-       Display *x11_display = nullptr;
-
        // Encoder parameters
-       VADisplay va_dpy;
+       std::unique_ptr<VADisplayWithCleanup> va_dpy;
        VAProfile h264_profile = (VAProfile)~0;
        VAConfigAttrib config_attrib[VAConfigAttribTypeMax];
        int config_attrib_num = 0, enc_packed_header_idx;
@@ -213,7 +218,6 @@ private:
        int h264_entropy_mode = 1; /* cabac */
        int ip_period = 3;
 
-       int rc_mode = -1;
        unsigned int current_ref_frame_num = 0;  // Encoding frame order within this GOP, sans B-frames.
 
        int frame_width;