]> git.sesse.net Git - nageru/blobdiff - nageru/quicksync_encoder.cpp
Move VADisplayWithCleanup into its own header file, in shared/.
[nageru] / nageru / quicksync_encoder.cpp
index 4883554c5b5d1b52e5146b666528dcee956fcf38..7ec9f71c5eced50096af99bd4baed2a433cd8a2b 100644 (file)
@@ -80,7 +80,7 @@ std::atomic<int64_t> metric_quick_sync_stalled_frames{0};
 #define CHECK_VASTATUS(va_status, func)                                 \
     if (va_status != VA_STATUS_SUCCESS) {                               \
         fprintf(stderr, "%s:%d (%s) failed with %d\n", __func__, __LINE__, func, va_status); \
-        exit(1);                                                        \
+        abort();                                                        \
     }
 
 #undef BUFFER_OFFSET
@@ -719,25 +719,15 @@ void QuickSyncEncoderImpl::enable_zerocopy_if_possible()
        } else if (global_flags.x264_video_to_http) {
                fprintf(stderr, "Disabling zerocopy H.264 encoding due to --http-x264-video.\n");
                use_zerocopy = false;
+       } else if (!global_flags.v4l_output_device.empty()) {
+               fprintf(stderr, "Disabling zerocopy H.264 encoding due to --v4l-output.\n");
+               use_zerocopy = false;
        } else {
                use_zerocopy = true;
        }
        global_flags.use_zerocopy = use_zerocopy;
 }
 
-VADisplayWithCleanup::~VADisplayWithCleanup()
-{
-       if (va_dpy != nullptr) {
-               vaTerminate(va_dpy);
-       }
-       if (x11_display != nullptr) {
-               XCloseDisplay(x11_display);
-       }
-       if (drm_fd != -1) {
-               close(drm_fd);
-       }
-}
-
 unique_ptr<VADisplayWithCleanup> va_open_display(const string &va_display)
 {
        if (va_display.empty() || va_display[0] != '/') {  // An X display.
@@ -822,7 +812,7 @@ int QuickSyncEncoderImpl::init_va(const string &va_display)
     va_dpy = try_open_va(va_display, &h264_profile, &error);
     if (va_dpy == nullptr) {
        fprintf(stderr, "error: %s\n", error.c_str());
-        exit(1);
+        abort();
     }
     if (!va_dpy->can_use_zerocopy) {
         use_zerocopy = false;
@@ -860,7 +850,7 @@ int QuickSyncEncoderImpl::init_va(const string &va_display)
     /* check the interested configattrib */
     if ((attrib[VAConfigAttribRTFormat].value & VA_RT_FORMAT_YUV420) == 0) {
         printf("Not find desired YUV420 RT format\n");
-        exit(1);
+        abort();
     } else {
         config_attrib[config_attrib_num].type = VAConfigAttribRTFormat;
         config_attrib[config_attrib_num].value = VA_RT_FORMAT_YUV420;
@@ -870,7 +860,7 @@ int QuickSyncEncoderImpl::init_va(const string &va_display)
     if (attrib[VAConfigAttribRateControl].value != VA_ATTRIB_NOT_SUPPORTED) {
         if (!(attrib[VAConfigAttribRateControl].value & VA_RC_CQP)) {
             fprintf(stderr, "ERROR: VA-API encoder does not support CQP mode.\n");
-            exit(1);
+            abort();
         }
 
         config_attrib[config_attrib_num].type = VAConfigAttribRateControl;
@@ -1547,6 +1537,10 @@ QuickSyncEncoderImpl::QuickSyncEncoderImpl(const std::string &filename, Resource
                memset(&slice_param, 0, sizeof(slice_param));
        }
 
+       if (!global_flags.v4l_output_device.empty()) {
+               v4l_output.reset(new V4LOutput(global_flags.v4l_output_device.c_str(), width, height));
+       }
+
        call_once(quick_sync_metrics_inited, [](){
                mixer_latency_histogram.init("mixer");
                qs_latency_histogram.init("quick_sync");
@@ -1564,7 +1558,7 @@ QuickSyncEncoderImpl::QuickSyncEncoderImpl(const std::string &filename, Resource
                if (!make_current(context, this->surface)) {
                        printf("display=%p surface=%p context=%p curr=%p err=%d\n", eglGetCurrentDisplay(), this->surface, context, eglGetCurrentContext(),
                                eglGetError());
-                       exit(1);
+                       abort();
                }
                encode_thread_func();
                delete_context(context);
@@ -1804,7 +1798,7 @@ void QuickSyncEncoderImpl::open_output_file(const std::string &filename)
        if (ret < 0) {
                char tmp[AV_ERROR_MAX_STRING_SIZE];
                fprintf(stderr, "%s: avio_open2() failed: %s\n", filename.c_str(), av_make_error_string(tmp, sizeof(tmp), ret));
-               exit(1);
+               abort();
        }
 
        string video_extradata;  // FIXME: See other comment about global headers.
@@ -1990,6 +1984,10 @@ void QuickSyncEncoderImpl::pass_frame(QuickSyncEncoderImpl::PendingFrame frame,
        } else if (global_flags.x264_video_to_http || global_flags.x264_video_to_disk) {
                x264_encoder->add_frame(pts, duration, frame.ycbcr_coefficients, data, received_ts);
        }
+
+       if (v4l_output != nullptr) {
+               v4l_output->send_frame(data);
+       }
 }
 
 void QuickSyncEncoderImpl::encode_frame(QuickSyncEncoderImpl::PendingFrame frame, int encoding_frame_num, int display_frame_num, int gop_start_display_frame_num,
@@ -2179,5 +2177,5 @@ string QuickSyncEncoder::get_usable_va_display()
        fprintf(stderr, "to expose Quick Sync. Alternatively, you can use --record-x264-video\n");
        fprintf(stderr, "to use software instead of hardware H.264 encoding, at the expense\n");
        fprintf(stderr, "of increased CPU usage and possibly bit rate.\n");
-       exit(1);
+       abort();
 }