]> git.sesse.net Git - nageru/blobdiff - quicksync_encoder.cpp
Make QuickSyncEncoder get its textures from ResourcePool.
[nageru] / quicksync_encoder.cpp
index b3a832c3d0a19ed633a2bb9d61813b47b00594c9..5ebd454edfdb635ba630123c92adf4e68486830c 100644 (file)
@@ -1,6 +1,7 @@
 //#include "sysdeps.h"
 #include "quicksync_encoder.h"
 
+#include <movit/resource_pool.h>
 #include <movit/util.h>
 #include <EGL/eglplatform.h>
 #include <X11/X.h>
@@ -193,7 +194,7 @@ FrameReorderer::Frame FrameReorderer::get_first_frame()
 
 class QuickSyncEncoderImpl {
 public:
-       QuickSyncEncoderImpl(const std::string &filename, QSurface *surface, const string &va_display, int width, int height, Mux *stream_mux);
+       QuickSyncEncoderImpl(const std::string &filename, movit::ResourcePool *resource_pool, QSurface *surface, const string &va_display, int width, int height, Mux *stream_mux, AudioEncoder *stream_audio_encoder, X264Encoder *x264_encoder);
        ~QuickSyncEncoderImpl();
        void add_audio(int64_t pts, vector<float> audio);
        bool begin_frame(GLuint *y_tex, GLuint *cbcr_tex);
@@ -270,17 +271,18 @@ private:
 
        map<int, PendingFrame> pending_video_frames;  // under frame_queue_mutex
        map<int64_t, vector<float>> pending_audio_frames;  // under frame_queue_mutex
+       movit::ResourcePool *resource_pool;
        QSurface *surface;
 
        unique_ptr<AudioEncoder> file_audio_encoder;
-       unique_ptr<AudioEncoder> stream_audio_encoder;
+       AudioEncoder *stream_audio_encoder;
+
+       unique_ptr<FrameReorderer> reorderer;
+       X264Encoder *x264_encoder;  // nullptr if not using x264.
 
        Mux* stream_mux;  // To HTTP.
        unique_ptr<Mux> file_mux;  // To local disk.
 
-       unique_ptr<FrameReorderer> reorderer;
-       unique_ptr<X264Encoder> x264_encoder;  // nullptr if not using x264.
-
        Display *x11_display = nullptr;
 
        // Encoder parameters
@@ -1149,8 +1151,8 @@ int QuickSyncEncoderImpl::setup_encode()
     VAStatus va_status;
     VASurfaceID *tmp_surfaceid;
     int codedbuf_size, i;
-    static VASurfaceID src_surface[SURFACE_NUM];
-    static VASurfaceID ref_surface[SURFACE_NUM];
+    VASurfaceID src_surface[SURFACE_NUM];
+    VASurfaceID ref_surface[SURFACE_NUM];
     
     va_status = vaCreateConfig(va_dpy, h264_profile, VAEntrypointEncSlice,
             &config_attrib[0], config_attrib_num, &config_id);
@@ -1201,17 +1203,12 @@ int QuickSyncEncoderImpl::setup_encode()
     //glGenFramebuffers(SURFACE_NUM, fbos);
     
     for (i = 0; i < SURFACE_NUM; i++) {
-        glGenTextures(1, &gl_surfaces[i].y_tex);
-        glGenTextures(1, &gl_surfaces[i].cbcr_tex);
-
-        if (!use_zerocopy) {
-            // Create Y image.
-            glBindTexture(GL_TEXTURE_2D, gl_surfaces[i].y_tex);
-            glTexStorage2D(GL_TEXTURE_2D, 1, GL_R8, frame_width, frame_height);
-
-            // Create CbCr image.
-            glBindTexture(GL_TEXTURE_2D, gl_surfaces[i].cbcr_tex);
-            glTexStorage2D(GL_TEXTURE_2D, 1, GL_RG8, frame_width / 2, frame_height / 2);
+        if (use_zerocopy) {
+            gl_surfaces[i].y_tex = resource_pool->create_2d_texture(GL_R8, 1, 1);
+            gl_surfaces[i].cbcr_tex = resource_pool->create_2d_texture(GL_RG8, 1, 1);
+        } else {
+            gl_surfaces[i].y_tex = resource_pool->create_2d_texture(GL_R8, frame_width, frame_height);
+            gl_surfaces[i].cbcr_tex = resource_pool->create_2d_texture(GL_RG8, frame_width / 2, frame_height / 2);
 
             // Generate a PBO to read into. It doesn't necessarily fit 1:1 with the VA-API
             // buffers, due to potentially differing pitch.
@@ -1651,9 +1648,7 @@ void QuickSyncEncoderImpl::save_codeddata(storage_task task)
                }
 
                file_audio_encoder->encode_audio(audio, audio_pts + global_delay());
-               if (stream_audio_encoder) {
-                       stream_audio_encoder->encode_audio(audio, audio_pts + global_delay());
-               }
+               stream_audio_encoder->encode_audio(audio, audio_pts + global_delay());
 
                if (audio_pts == task.pts) break;
        }
@@ -1709,8 +1704,8 @@ int QuickSyncEncoderImpl::release_encode()
                        glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
                        glDeleteBuffers(1, &gl_surfaces[i].pbo);
                }
-               glDeleteTextures(1, &gl_surfaces[i].y_tex);
-               glDeleteTextures(1, &gl_surfaces[i].cbcr_tex);
+               resource_pool->release_2d_texture(gl_surfaces[i].y_tex);
+               resource_pool->release_2d_texture(gl_surfaces[i].cbcr_tex);
        }
 
        vaDestroyContext(va_dpy, context_id);
@@ -1732,18 +1727,12 @@ namespace {
 
 }  // namespace
 
-QuickSyncEncoderImpl::QuickSyncEncoderImpl(const std::string &filename, QSurface *surface, const string &va_display, int width, int height, Mux *stream_mux)
-       : current_storage_frame(0), surface(surface), stream_mux(stream_mux), frame_width(width), frame_height(height)
+QuickSyncEncoderImpl::QuickSyncEncoderImpl(const std::string &filename, movit::ResourcePool *resource_pool, QSurface *surface, const string &va_display, int width, int height, Mux *stream_mux, AudioEncoder *stream_audio_encoder, X264Encoder *x264_encoder)
+       : current_storage_frame(0), resource_pool(resource_pool), surface(surface), stream_audio_encoder(stream_audio_encoder), x264_encoder(x264_encoder), stream_mux(stream_mux), frame_width(width), frame_height(height)
 {
        file_audio_encoder.reset(new AudioEncoder(AUDIO_OUTPUT_CODEC_NAME, DEFAULT_AUDIO_OUTPUT_BIT_RATE));
        open_output_file(filename);
        file_audio_encoder->add_mux(file_mux.get());
-       if (global_flags.stream_audio_codec_name.empty()) {
-               file_audio_encoder->add_mux(stream_mux);
-       } else {
-               stream_audio_encoder.reset(new AudioEncoder(global_flags.stream_audio_codec_name, global_flags.stream_audio_codec_bitrate));
-               stream_audio_encoder->add_mux(stream_mux);
-       }
 
        frame_width_mbaligned = (frame_width + 15) & (~15);
        frame_height_mbaligned = (frame_height + 15) & (~15);
@@ -1755,7 +1744,9 @@ QuickSyncEncoderImpl::QuickSyncEncoderImpl(const std::string &filename, QSurface
                reorderer.reset(new FrameReorderer(ip_period - 1, frame_width, frame_height));
        }
        if (global_flags.x264_video_to_http) {
-               x264_encoder.reset(new X264Encoder(stream_mux));
+               assert(x264_encoder != nullptr);
+       } else {
+               assert(x264_encoder == nullptr);
        }
 
        init_va(va_display);
@@ -1780,6 +1771,7 @@ QuickSyncEncoderImpl::QuickSyncEncoderImpl(const std::string &filename, QSurface
                        exit(1);
                }
                encode_thread_func();
+               delete_context(context);
        });
 }
 
@@ -1927,7 +1919,6 @@ void QuickSyncEncoderImpl::shutdown()
                frame_queue_nonempty.notify_all();
        }
        encode_thread.join();
-       x264_encoder.reset();
        {
                unique_lock<mutex> lock(storage_task_queue_mutex);
                storage_thread_should_quit = true;
@@ -2056,10 +2047,8 @@ void QuickSyncEncoderImpl::encode_remaining_audio()
        pending_audio_frames.clear();
 
        // Encode any leftover audio in the queues, and also any delayed frames.
+       // Note: stream_audio_encoder is not owned by us, so don't call encode_last_audio().
        file_audio_encoder->encode_last_audio();
-       if (stream_audio_encoder) {
-               stream_audio_encoder->encode_last_audio();
-       }
 }
 
 void QuickSyncEncoderImpl::add_packet_for_uncompressed_frame(int64_t pts, int64_t duration, const uint8_t *data)
@@ -2181,8 +2170,8 @@ void QuickSyncEncoderImpl::encode_frame(QuickSyncEncoderImpl::PendingFrame frame
 }
 
 // Proxy object.
-QuickSyncEncoder::QuickSyncEncoder(const std::string &filename, QSurface *surface, const string &va_display, int width, int height, Mux *stream_mux)
-       : impl(new QuickSyncEncoderImpl(filename, surface, va_display, width, height, stream_mux)) {}
+QuickSyncEncoder::QuickSyncEncoder(const std::string &filename, movit::ResourcePool *resource_pool, QSurface *surface, const string &va_display, int width, int height, Mux *stream_mux, AudioEncoder *stream_audio_encoder, X264Encoder *x264_encoder)
+       : impl(new QuickSyncEncoderImpl(filename, resource_pool, surface, va_display, width, height, stream_mux, stream_audio_encoder, x264_encoder)) {}
 
 // Must be defined here because unique_ptr<> destructor needs to know the impl.
 QuickSyncEncoder::~QuickSyncEncoder() {}