From 79531ceacfc8d5b756edd46b7c859d09619084e7 Mon Sep 17 00:00:00 2001 From: Martin Pulec Date: Thu, 23 May 2019 14:49:38 +0200 Subject: [PATCH] Lavc: codec ordering to a separate function --- src/video_compress/libavcodec.cpp | 147 ++++++++++++++++-------------- 1 file changed, 77 insertions(+), 70 deletions(-) diff --git a/src/video_compress/libavcodec.cpp b/src/video_compress/libavcodec.cpp index 0564ff593..f251b326c 100644 --- a/src/video_compress/libavcodec.cpp +++ b/src/video_compress/libavcodec.cpp @@ -750,6 +750,81 @@ ADD_TO_PARAM(lavc_use_codec, "lavc-use-codec", " for NVENC encoder.\n" " Another possibility is to use yuv420p10le, yuv422p10le or yuv444p10le\n" " to force 10-bit encoding.\n"); +void fill_requested_pix_fmts(enum AVPixelFormat *fmts, int max_count, struct video_desc in_desc, + AVCodec *codec, int requested_subsampling) { + int total_pix_fmts = 0; + +#ifdef HWACC_VAAPI + if (regex_match(codec->name, regex(".*vaapi.*"))) { + fmts[total_pix_fmts++] = AV_PIX_FMT_VAAPI; + } +#endif + + if (ug_to_av_pixfmt_map.find(in_desc.color_spec) != ug_to_av_pixfmt_map.end()) { + fmts[total_pix_fmts++] = ug_to_av_pixfmt_map.find(in_desc.color_spec)->second; + } + + if (requested_subsampling == 0) { + // for interlaced formats, it is better to use either 422 or 444 + if (in_desc.interlacing == INTERLACED_MERGED) { + // 422 + memcpy(fmts + total_pix_fmts, + fmts422_8, sizeof(fmts422_8)); + total_pix_fmts += sizeof(fmts422_8) / sizeof(enum AVPixelFormat); + // 444 + memcpy(fmts + total_pix_fmts, + fmts444_8, sizeof(fmts444_8)); + total_pix_fmts += sizeof(fmts444_8) / sizeof(enum AVPixelFormat); + // 420 + memcpy(fmts + total_pix_fmts, + fmts420_8, sizeof(fmts420_8)); + total_pix_fmts += sizeof(fmts420_8) / sizeof(enum AVPixelFormat); + } else { + // 420 + memcpy(fmts + total_pix_fmts, + fmts420_8, sizeof(fmts420_8)); + total_pix_fmts += sizeof(fmts420_8) / sizeof(enum AVPixelFormat); + // 422 + memcpy(fmts + total_pix_fmts, + fmts422_8, sizeof(fmts422_8)); + total_pix_fmts += sizeof(fmts422_8) / sizeof(enum AVPixelFormat); + // 444 + memcpy(fmts + total_pix_fmts, + fmts444_8, sizeof(fmts444_8)); + total_pix_fmts += sizeof(fmts444_8) / sizeof(enum AVPixelFormat); + } + } else { + switch (requested_subsampling) { + case 420: + memcpy(fmts + total_pix_fmts, + fmts420_8, sizeof(fmts420_8)); + total_pix_fmts += sizeof(fmts420_8) / sizeof(enum AVPixelFormat); + break; + case 422: + memcpy(fmts + total_pix_fmts, + fmts422_8, sizeof(fmts422_8)); + total_pix_fmts += sizeof(fmts422_8) / sizeof(enum AVPixelFormat); + break; + case 444: + memcpy(fmts + total_pix_fmts, + fmts444_8, sizeof(fmts444_8)); + total_pix_fmts += sizeof(fmts444_8) / sizeof(enum AVPixelFormat); + break; + default: + abort(); + } + } + + if (get_commandline_param("lavc-use-codec")) { + const char *val = get_commandline_param("lavc-use-codec"); + fmts[0] = av_get_pix_fmt(val); + total_pix_fmts = 1; + } + + fmts[total_pix_fmts++] = AV_PIX_FMT_NONE; + + assert(total_pix_fmts <= max_count); +} static bool configure_with(struct state_video_compress_libav *s, struct video_desc desc) { @@ -818,76 +893,8 @@ static bool configure_with(struct state_video_compress_libav *s, struct video_de } enum AVPixelFormat requested_pix_fmts[100]; - int total_pix_fmts = 0; - -#ifdef HWACC_VAAPI - if (regex_match(codec->name, regex(".*vaapi.*"))) { - requested_pix_fmts[total_pix_fmts++] = AV_PIX_FMT_VAAPI; - } -#endif - - if (ug_to_av_pixfmt_map.find(desc.color_spec) != ug_to_av_pixfmt_map.end()) { - requested_pix_fmts[total_pix_fmts++] = ug_to_av_pixfmt_map.find(desc.color_spec)->second; - } - - if (s->requested_subsampling == 0) { - // for interlaced formats, it is better to use either 422 or 444 - if (desc.interlacing == INTERLACED_MERGED) { - // 422 - memcpy(requested_pix_fmts + total_pix_fmts, - fmts422_8, sizeof(fmts422_8)); - total_pix_fmts += sizeof(fmts422_8) / sizeof(enum AVPixelFormat); - // 444 - memcpy(requested_pix_fmts + total_pix_fmts, - fmts444_8, sizeof(fmts444_8)); - total_pix_fmts += sizeof(fmts444_8) / sizeof(enum AVPixelFormat); - // 420 - memcpy(requested_pix_fmts + total_pix_fmts, - fmts420_8, sizeof(fmts420_8)); - total_pix_fmts += sizeof(fmts420_8) / sizeof(enum AVPixelFormat); - } else { - // 420 - memcpy(requested_pix_fmts + total_pix_fmts, - fmts420_8, sizeof(fmts420_8)); - total_pix_fmts += sizeof(fmts420_8) / sizeof(enum AVPixelFormat); - // 422 - memcpy(requested_pix_fmts + total_pix_fmts, - fmts422_8, sizeof(fmts422_8)); - total_pix_fmts += sizeof(fmts422_8) / sizeof(enum AVPixelFormat); - // 444 - memcpy(requested_pix_fmts + total_pix_fmts, - fmts444_8, sizeof(fmts444_8)); - total_pix_fmts += sizeof(fmts444_8) / sizeof(enum AVPixelFormat); - } - } else { - switch (s->requested_subsampling) { - case 420: - memcpy(requested_pix_fmts + total_pix_fmts, - fmts420_8, sizeof(fmts420_8)); - total_pix_fmts += sizeof(fmts420_8) / sizeof(enum AVPixelFormat); - break; - case 422: - memcpy(requested_pix_fmts + total_pix_fmts, - fmts422_8, sizeof(fmts422_8)); - total_pix_fmts += sizeof(fmts422_8) / sizeof(enum AVPixelFormat); - break; - case 444: - memcpy(requested_pix_fmts + total_pix_fmts, - fmts444_8, sizeof(fmts444_8)); - total_pix_fmts += sizeof(fmts444_8) / sizeof(enum AVPixelFormat); - break; - default: - abort(); - } - } - - if (get_commandline_param("lavc-use-codec")) { - const char *val = get_commandline_param("lavc-use-codec"); - requested_pix_fmts[0] = av_get_pix_fmt(val); - total_pix_fmts = 1; - } - - requested_pix_fmts[total_pix_fmts++] = AV_PIX_FMT_NONE; + int max_pix_fmts = sizeof requested_pix_fmts / sizeof requested_pix_fmts[0]; + fill_requested_pix_fmts(requested_pix_fmts, max_pix_fmts, desc, codec, s->requested_subsampling); // Try to open the codec context // It is done in a loop because some pixel formats that are reported