X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=nageru%2Fquicksync_encoder.cpp;h=9dc603b5ede7a0c1c2f6bbefcf59db713bd92408;hb=1e71f1ad456d64082a1a56bbc79fe0216e5fb5e3;hp=5200ce61b4911ab4e18692da0bfc17b483ae548d;hpb=392f9d1ccb835c05a3874c4bea163788b2c37024;p=nageru diff --git a/nageru/quicksync_encoder.cpp b/nageru/quicksync_encoder.cpp index 5200ce6..9dc603b 100644 --- a/nageru/quicksync_encoder.cpp +++ b/nageru/quicksync_encoder.cpp @@ -45,16 +45,16 @@ extern "C" { } // namespace #include "audio_encoder.h" -#include "context.h" +#include "shared/context.h" #include "defs.h" -#include "disk_space_estimator.h" -#include "ffmpeg_raii.h" +#include "shared/disk_space_estimator.h" +#include "shared/ffmpeg_raii.h" #include "flags.h" -#include "mux.h" +#include "shared/mux.h" #include "print_latency.h" #include "quicksync_encoder_impl.h" #include "ref_counted_frame.h" -#include "timebase.h" +#include "shared/timebase.h" #include "x264_encoder.h" using namespace movit; @@ -80,7 +80,7 @@ std::atomic 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 @@ -822,7 +822,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 +860,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 +870,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; @@ -1023,7 +1023,7 @@ void QuickSyncEncoderImpl::update_ReferenceFrames(int current_display_frame, int pic_param.CurrPic.frame_idx = current_ref_frame_num; CurrentCurrPic.flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE; - unique_lock lock(storage_task_queue_mutex); + lock_guard lock(storage_task_queue_mutex); // Insert the new frame at the start of the reference queue. reference_frames.push_front(ReferenceFrame{ CurrentCurrPic, current_display_frame }); @@ -1434,7 +1434,7 @@ void QuickSyncEncoderImpl::save_codeddata(GLSurface *surf, storage_task task) // this is weird. but it seems to put a new frame onto the queue void QuickSyncEncoderImpl::storage_task_enqueue(storage_task task) { - unique_lock lock(storage_task_queue_mutex); + lock_guard lock(storage_task_queue_mutex); storage_task_queue.push(move(task)); storage_task_queue_changed.notify_all(); } @@ -1468,7 +1468,7 @@ void QuickSyncEncoderImpl::storage_task_thread() // Unlock the frame, and all its references. { - unique_lock lock(storage_task_queue_mutex); + lock_guard lock(storage_task_queue_mutex); release_gl_surface(display_order); for (size_t frame_num : ref_display_frame_numbers) { @@ -1481,13 +1481,18 @@ void QuickSyncEncoderImpl::storage_task_thread() void QuickSyncEncoderImpl::release_encode() { for (unsigned i = 0; i < SURFACE_NUM; i++) { - vaDestroyBuffer(va_dpy->va_dpy, gl_surfaces[i].coded_buf); - vaDestroySurfaces(va_dpy->va_dpy, &gl_surfaces[i].src_surface, 1); - vaDestroySurfaces(va_dpy->va_dpy, &gl_surfaces[i].ref_surface, 1); + VAStatus va_status = vaDestroyBuffer(va_dpy->va_dpy, gl_surfaces[i].coded_buf); + CHECK_VASTATUS(va_status, "vaDestroyBuffer"); + va_status = vaDestroySurfaces(va_dpy->va_dpy, &gl_surfaces[i].src_surface, 1); + CHECK_VASTATUS(va_status, "vaDestroySurfaces"); + va_status = vaDestroySurfaces(va_dpy->va_dpy, &gl_surfaces[i].ref_surface, 1); + CHECK_VASTATUS(va_status, "vaDestroySurfaces"); } - vaDestroyContext(va_dpy->va_dpy, context_id); - vaDestroyConfig(va_dpy->va_dpy, config_id); + VAStatus va_status = vaDestroyContext(va_dpy->va_dpy, context_id); + CHECK_VASTATUS(va_status, "vaDestroyContext"); + va_status = vaDestroyConfig(va_dpy->va_dpy, config_id); + CHECK_VASTATUS(va_status, "vaDestroyConfig"); } void QuickSyncEncoderImpl::release_gl_resources() @@ -1559,7 +1564,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); @@ -1701,7 +1706,7 @@ RefCountedGLsync QuickSyncEncoderImpl::end_frame() GLenum type = global_flags.x264_bit_depth > 8 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_BYTE; GLSurface *surf; { - unique_lock lock(storage_task_queue_mutex); + lock_guard lock(storage_task_queue_mutex); surf = surface_for_frame[current_storage_frame]; assert(surf != nullptr); } @@ -1740,7 +1745,7 @@ RefCountedGLsync QuickSyncEncoderImpl::end_frame() check_error(); { - unique_lock lock(frame_queue_mutex); + lock_guard lock(frame_queue_mutex); current_video_frame.fence = fence; pending_video_frames.push(move(current_video_frame)); ++current_storage_frame; @@ -1756,13 +1761,13 @@ void QuickSyncEncoderImpl::shutdown() } { - unique_lock lock(frame_queue_mutex); + lock_guard lock(frame_queue_mutex); encode_thread_should_quit = true; frame_queue_nonempty.notify_all(); } encode_thread.join(); { - unique_lock lock(storage_task_queue_mutex); + lock_guard lock(storage_task_queue_mutex); storage_thread_should_quit = true; frame_queue_nonempty.notify_all(); storage_task_queue_changed.notify_all(); @@ -1792,15 +1797,14 @@ void QuickSyncEncoderImpl::open_output_file(const std::string &filename) { AVFormatContext *avctx = avformat_alloc_context(); avctx->oformat = av_guess_format(NULL, filename.c_str(), NULL); - assert(filename.size() < sizeof(avctx->filename) - 1); - strcpy(avctx->filename, filename.c_str()); + avctx->url = strdup(filename.c_str()); string url = "file:" + filename; int ret = avio_open2(&avctx->pb, url.c_str(), AVIO_FLAG_WRITE, &avctx->interrupt_callback, NULL); 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. @@ -1813,8 +1817,8 @@ void QuickSyncEncoderImpl::open_output_file(const std::string &filename) { lock_guard lock(file_audio_encoder_mutex); AVCodecParametersWithDeleter audio_codecpar = file_audio_encoder->get_codec_parameters(); - file_mux.reset(new Mux(avctx, frame_width, frame_height, Mux::CODEC_H264, video_extradata, audio_codecpar.get(), TIMEBASE, - std::bind(&DiskSpaceEstimator::report_write, disk_space_estimator, filename, _1), + file_mux.reset(new Mux(avctx, frame_width, frame_height, Mux::CODEC_H264, video_extradata, audio_codecpar.get(), get_color_space(global_flags.ycbcr_rec709_coefficients), TIMEBASE, + std::bind(&DiskSpaceEstimator::report_append, disk_space_estimator, filename, _1), Mux::WRITE_BACKGROUND, { ¤t_file_mux_metrics, &total_mux_metrics })); } @@ -1860,7 +1864,7 @@ void QuickSyncEncoderImpl::encode_thread_func() pass_frame(frame, display_frame_num, frame.pts, frame.duration); if (global_flags.x264_video_to_disk) { - unique_lock lock(storage_task_queue_mutex); + lock_guard lock(storage_task_queue_mutex); release_gl_surface(display_frame_num); continue; } @@ -1883,7 +1887,7 @@ void QuickSyncEncoderImpl::encode_thread_func() if (frame_type == FRAME_IDR) { // Release any reference frames from the previous GOP. { - unique_lock lock(storage_task_queue_mutex); + lock_guard lock(storage_task_queue_mutex); for (const ReferenceFrame &frame : reference_frames) { release_gl_surface(frame.display_number); } @@ -1939,8 +1943,6 @@ void QuickSyncEncoderImpl::add_packet_for_uncompressed_frame(int64_t pts, int64_ stream_mux->add_packet(pkt, pts, pts); } -namespace { - void memcpy_with_pitch(uint8_t *dst, const uint8_t *src, size_t src_width, size_t dst_pitch, size_t height) { if (src_width == dst_pitch) { @@ -1954,8 +1956,6 @@ void memcpy_with_pitch(uint8_t *dst, const uint8_t *src, size_t src_width, size_ } } -} // namespace - void QuickSyncEncoderImpl::pass_frame(QuickSyncEncoderImpl::PendingFrame frame, int display_frame_num, int64_t pts, int64_t duration) { // Wait for the GPU to be done with the frame. @@ -1980,7 +1980,7 @@ void QuickSyncEncoderImpl::pass_frame(QuickSyncEncoderImpl::PendingFrame frame, GLSurface *surf; { - unique_lock lock(storage_task_queue_mutex); + lock_guard lock(storage_task_queue_mutex); surf = surface_for_frame[display_frame_num]; assert(surf != nullptr); } @@ -1999,7 +1999,7 @@ void QuickSyncEncoderImpl::encode_frame(QuickSyncEncoderImpl::PendingFrame frame GLSurface *surf; { - unique_lock lock(storage_task_queue_mutex); + lock_guard lock(storage_task_queue_mutex); surf = surface_for_frame[display_frame_num]; assert(surf != nullptr); } @@ -2063,7 +2063,7 @@ void QuickSyncEncoderImpl::encode_frame(QuickSyncEncoderImpl::PendingFrame frame // Lock the references for this frame; otherwise, they could be // rendered to before this frame is done encoding. { - unique_lock lock(storage_task_queue_mutex); + lock_guard lock(storage_task_queue_mutex); for (const ReferenceFrame &frame : reference_frames) { assert(surface_for_frame.count(frame.display_number)); ++surface_for_frame[frame.display_number]->refcount; @@ -2179,5 +2179,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(); }