]> git.sesse.net Git - nageru/blobdiff - h264encode.cpp
Refcount the input frames directly instead of trying to free them after-the-fact...
[nageru] / h264encode.cpp
index fe51b9e8d06a50ae4739c676d039a497fa058a12..19cd2fc3f7ba99dad675e8575326e597df44e907 100644 (file)
@@ -1,28 +1,34 @@
 //#include "sysdeps.h"
+#include "h264encode.h"
+
+#include <EGL/eglplatform.h>
+#include <X11/X.h>
+#include <X11/Xlib.h>
+#include <assert.h>
+#include <epoxy/egl.h>
+#include <libavcodec/avcodec.h>
+#include <libavformat/avio.h>
+#include <libavutil/mathematics.h>
+#include <libavutil/rational.h>
+#include <libdrm/drm_fourcc.h>
+#include <stdint.h>
 #include <stdio.h>
-#include <string.h>
 #include <stdlib.h>
-#include <getopt.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/time.h>
-#include <sys/mman.h>
-#include <fcntl.h>
-#include <assert.h>
-#include <pthread.h>
-#include <errno.h>
-#include <math.h>
+#include <string.h>
 #include <va/va.h>
-#include <va/va_x11.h>
-#include <va/va_enc_h264.h>
 #include <va/va_drmcommon.h>
-#include <libdrm/drm_fourcc.h>
-#include <thread>
+#include <va/va_enc_h264.h>
+#include <va/va_x11.h>
+#include <condition_variable>
 #include <mutex>
 #include <queue>
-#include <condition_variable>
-#include "h264encode.h"
+#include <string>
+#include <thread>
+
+#include "context.h"
+
+class QOpenGLContext;
+class QSurface;
 
 #define CHECK_VASTATUS(va_status, func)                                 \
     if (va_status != VA_STATUS_SUCCESS) {                               \
@@ -97,7 +103,6 @@ static  int h264_maxref = (1<<16|1);
 static  int h264_entropy_mode = 1; /* cabac */
 
 static  char *coded_fn = NULL;
-static  FILE *coded_fp = NULL;
 
 static  int frame_width = 176;
 static  int frame_height = 144;
@@ -790,12 +795,6 @@ static int process_cmdline(int argc, char *argv[])
             coded_fn = strdup("./test.264");
     }
     
-    /* store coded data into a file */
-    coded_fp = fopen(coded_fn, "w+");
-    if (coded_fp == NULL) {
-        printf("Open file %s failed, exit\n", coded_fn);
-        exit(1);
-    }
 
     frame_width_mbaligned = (frame_width + 15) & (~15);
     frame_height_mbaligned = (frame_height + 15) & (~15);
@@ -1582,8 +1581,6 @@ int H264Encoder::save_codeddata(unsigned long long display_order, unsigned long
     CHECK_VASTATUS(va_status, "vaMapBuffer");
     while (buf_list != NULL) {
         data.append(reinterpret_cast<const char *>(buf_list->buf), buf_list->size);
-        if (coded_fp != nullptr)
-            coded_size += fwrite(buf_list->buf, 1, buf_list->size, coded_fp);
         buf_list = (VACodedBufferSegment *) buf_list->next;
 
         frame_size += coded_size;
@@ -1751,9 +1748,6 @@ H264Encoder::H264Encoder(QSurface *surface, int width, int height, const char *o
                exit(1);
        }
 
-       coded_fp = fopen("dump.h264", "wb");
-       assert(coded_fp != NULL);
-
        frame_width = width;
        frame_height = height;
        frame_width_mbaligned = (frame_width + 15) & (~15);
@@ -1874,11 +1868,11 @@ bool H264Encoder::begin_frame(GLuint *y_tex, GLuint *cbcr_tex)
        return true;
 }
 
-void H264Encoder::end_frame(GLsync fence, const std::vector<FrameAllocator::Frame> &input_frames_to_release)
+void H264Encoder::end_frame(RefCountedGLsync fence, const std::vector<RefCountedFrame> &input_frames)
 {
        {
                unique_lock<mutex> lock(frame_queue_mutex);
-               pending_frames[current_storage_frame++] = PendingFrame{ fence, input_frames_to_release };
+               pending_frames[current_storage_frame++] = PendingFrame{ fence, input_frames };
        }
        frame_queue_nonempty.notify_one();
 }
@@ -1904,14 +1898,10 @@ void H264Encoder::copy_thread_func()
                }
 
                // Wait for the GPU to be done with the frame.
-               glClientWaitSync(frame.fence, 0, 0);
-               glDeleteSync(frame.fence);
+               glClientWaitSync(frame.fence.get(), 0, 0);
 
                // Release back any input frames we needed to render this frame.
-               // (Actually, those that were needed one output frame ago.)
-               for (FrameAllocator::Frame input_frame : frame.input_frames_to_release) {
-                       input_frame.owner->release_frame(input_frame);
-               }
+               frame.input_frames.clear();
 
                // Unmap the image.
                GLSurface *surf = &gl_surfaces[current_frame_display % SURFACE_NUM];