diff --git a/include/upipe/uref_pic_flow.h b/include/upipe/uref_pic_flow.h index d3f8eafe5..a4d47adbf 100644 --- a/include/upipe/uref_pic_flow.h +++ b/include/upipe/uref_pic_flow.h @@ -403,6 +403,64 @@ static inline int uref_pic_flow_infer_dar(struct uref *uref, return UBASE_ERR_NONE; } +/** @This sets colour primaries value to flow def attribute. + * + * @param flow_def flow definition packet + * @param colour_primaries colour primaries value (ISO/IEC 23091-2:2019) + * @return an error code + */ +int uref_pic_flow_set_colour_primaries_val(struct uref *flow_def, + int colour_primaries); + +/** @This gets colour primaries value from flow def attribute. + * + * @param flow_def flow definition packet + * @param colour_primaries colour primaries value (ISO/IEC 23091-2:2019) + * @return an error code + */ +int uref_pic_flow_get_colour_primaries_val(struct uref *flow_def, + int *colour_primaries); + +/** @This sets transfer characteristics value to flow def attribute. + * + * @param flow_def flow definition packet + * @param transfer_characteristics transfer characteristics value + * (ISO/IEC 23091-2:2019) + * @return an error code + */ +int uref_pic_flow_set_transfer_characteristics_val(struct uref *flow_def, + int transfer_characteristics); + +/** @This gets transfer characteristics value from flow def attribute. + * + * @param flow_def flow definition packet + * @param transfer_characteristics transfer characteristics value + * (ISO/IEC 23091-2:2019) + * @return an error code + */ +int uref_pic_flow_get_transfer_characteristics_val(struct uref *flow_def, + int *transfer_characteristics); + +/** @This sets matrix coefficients value to flow def attribute. + * + * @param flow_def flow definition packet + * @param matrix_coefficients matrix coefficients value + * (ISO/IEC 23091-2:2019) + * @return an error code + */ +int uref_pic_flow_set_matrix_coefficients_val(struct uref *flow_def, + int matrix_coefficients); + +/** @This gets matrix coefficients value from flow def attribute. + * + * @param flow_def flow definition packet + * @param matrix_coefficients matrix coefficients value + * (ISO/IEC 23091-2:2019) + * @return an error code + */ +int uref_pic_flow_get_matrix_coefficients_val(struct uref *flow_def, + int *matrix_coefficients); + #ifdef __cplusplus } #endif diff --git a/lib/upipe-av/upipe_avcodec_encode.c b/lib/upipe-av/upipe_avcodec_encode.c index 5940ccc02..a1e366c6d 100644 --- a/lib/upipe-av/upipe_avcodec_encode.c +++ b/lib/upipe-av/upipe_avcodec_encode.c @@ -225,56 +225,6 @@ UPIPE_HELPER_UBUF_MGR(upipe_avcenc, ubuf_mgr, flow_format, ubuf_mgr_request, UPIPE_HELPER_UPUMP_MGR(upipe_avcenc, upump_mgr) UPIPE_HELPER_UPUMP(upipe_avcenc, upump_av_deal, upump_mgr) -/** @This allows to convert from Upipe color space to avcenc color space. */ -struct upipe_avcenc_color { - /** Upipe color */ - const char *upipe_color; - /** avcenc color */ - const char *avcenc_color; -}; - -/** @This converts transfer characteristics from Upipe to avcenc. */ -static const struct upipe_avcenc_color upipe_avcenc_color_trc[] = { - { "bt470m", "gamma22" }, - { "bt470bg", "gamma28" }, - { "log100", "log" }, - { "log316", "log_sqrt" }, - { "iec61966-2-4", "iec61966_2_4" }, - { "bt1361e", "bt1361" }, - { "iec61966-2-1", "iec61966_2_1" }, - { "bt2020-10", "bt2020-10bit" }, - { "bt2020-12", "bt2020-12bit" }, - { NULL, NULL } -}; - -/** @This converts matrix coefficients from Upipe to avcenc. */ -static const struct upipe_avcenc_color upipe_avcenc_color_space[] = { - { "GBR", "rgb" }, - { "YCgCo", "ycocg" }, - { "bt2020nc", "bt2020_ncl" }, - { "bt2020c", "bt2020_cl" }, - { NULL, NULL } -}; - -/** @internal @This converts a Upipe color space to avcenc color space. - * - * @param list conversion list - * @param upipe_color Upipe color - * @return avcenc color - */ -static const char * - upipe_avcenc_convert_color(const struct upipe_avcenc_color *list, - const char *upipe_color) -{ - int i = 0; - while (list[i].upipe_color != NULL) { - if (!strcmp(list[i].upipe_color, upipe_color)) - return list[i].avcenc_color; - i++; - } - return upipe_color; -} - /** @hidden */ static void upipe_avcenc_free(struct upipe *upipe); @@ -1560,36 +1510,20 @@ static int upipe_avcenc_set_flow_def(struct upipe *upipe, struct uref *flow_def) context->sample_aspect_ratio.den = sar.den; } - const char *content; - int ret; - content = ubase_check(uref_pic_flow_get_full_range( - flow_def)) ? "jpeg" : "mpeg"; - if ((ret = av_opt_set(context, "color_range", content, - AV_OPT_SEARCH_CHILDREN)) < 0) - upipe_err_va(upipe, "can't set option %s:%s (%d)", - "color_range", content, ret); - if (ubase_check(uref_pic_flow_get_colour_primaries( - flow_def, &content)) && - (ret = av_opt_set(context, "color_primaries", content, - AV_OPT_SEARCH_CHILDREN)) < 0) - upipe_err_va(upipe, "can't set option %s:%s (%d)", - "color_primaries", content, ret); - if (ubase_check(uref_pic_flow_get_transfer_characteristics( - flow_def, &content)) && - (ret = av_opt_set(context, "color_trc", - upipe_avcenc_convert_color( - upipe_avcenc_color_trc, content), - AV_OPT_SEARCH_CHILDREN)) < 0) - upipe_err_va(upipe, "can't set option %s:%s (%d)", - "color_trc", content, ret); - if (ubase_check(uref_pic_flow_get_matrix_coefficients( - flow_def, &content)) && - (ret = av_opt_set(context, "colorspace", - upipe_avcenc_convert_color( - upipe_avcenc_color_space, content), - AV_OPT_SEARCH_CHILDREN)) < 0) - upipe_err_va(upipe, "can't set option %s:%s (%d)", - "colorspace", content, ret); + context->color_range = + ubase_check(uref_pic_flow_get_full_range(flow_def)) ? + AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG; + + int val; + if (ubase_check( + uref_pic_flow_get_colour_primaries_val(flow_def, &val))) + context->color_primaries = val; + if (ubase_check( + uref_pic_flow_get_transfer_characteristics_val(flow_def, &val))) + context->color_trc = val; + if (ubase_check( + uref_pic_flow_get_matrix_coefficients_val(flow_def, &val))) + context->colorspace = val; if (!ubase_check(uref_pic_get_progressive(flow_def))) { context->flags |= AV_CODEC_FLAG_INTERLACED_DCT | diff --git a/lib/upipe-av/upipe_avfilter.c b/lib/upipe-av/upipe_avfilter.c index 80e77aa31..907f73ed1 100644 --- a/lib/upipe-av/upipe_avfilter.c +++ b/lib/upipe-av/upipe_avfilter.c @@ -2153,74 +2153,14 @@ static void upipe_avfilt_output_frame(struct upipe *upipe, if (frame->color_range == AVCOL_RANGE_JPEG) UBASE_ERROR(upipe, uref_pic_flow_set_full_range(flow_def_attr)) - const char *str = NULL; - switch (frame->color_primaries) { - case AVCOL_PRI_BT709: str = "bt709"; break; - case AVCOL_PRI_BT470M: str = "bt470m"; break; - case AVCOL_PRI_BT470BG: str = "bt470bg"; break; - case AVCOL_PRI_SMPTE170M: str = "smpte170m"; break; - case AVCOL_PRI_SMPTE240M: str = "smpte240m"; break; - case AVCOL_PRI_FILM: str = "film"; break; - case AVCOL_PRI_BT2020: str = "bt2020"; break; - case AVCOL_PRI_SMPTE428: str = "smpte428"; break; - case AVCOL_PRI_SMPTE431: str = "smpte431"; break; - case AVCOL_PRI_SMPTE432: str = "smpte432"; break; -#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(56, 34, 100) - case AVCOL_PRI_EBU3213: str = "ebu3213"; break; -#endif - default: break; - } - if (str != NULL) { - UBASE_ERROR(upipe, uref_pic_flow_set_colour_primaries( - flow_def_attr, str)) - } + UBASE_ERROR(upipe, uref_pic_flow_set_colour_primaries_val( + flow_def_attr, frame->color_primaries)) - str = NULL; - switch (frame->color_trc) { - case AVCOL_TRC_BT709: str = "bt709"; break; - case AVCOL_TRC_GAMMA22: str = "bt470m"; break; - case AVCOL_TRC_GAMMA28: str = "bt470bg"; break; - case AVCOL_TRC_SMPTE170M: str = "smpte170m"; break; - case AVCOL_TRC_SMPTE240M: str = "smpte240m"; break; - case AVCOL_TRC_LINEAR: str = "linear"; break; - case AVCOL_TRC_LOG: str = "log100"; break; - case AVCOL_TRC_LOG_SQRT: str = "log316"; break; - case AVCOL_TRC_IEC61966_2_4: str = "iec61966-2-4"; break; - case AVCOL_TRC_BT1361_ECG: str = "bt1361e"; break; - case AVCOL_TRC_IEC61966_2_1: str = "iec61966-2-1"; break; - case AVCOL_TRC_BT2020_10: str = "bt2020-10"; break; - case AVCOL_TRC_BT2020_12: str = "bt2020-12"; break; - case AVCOL_TRC_SMPTE2084: str = "smpte2084"; break; - case AVCOL_TRC_SMPTE428: str = "smpte428"; break; - case AVCOL_TRC_ARIB_STD_B67: str = "arib-std-b67"; break; - default: break; - } - if (str != NULL) { - UBASE_ERROR(upipe, uref_pic_flow_set_transfer_characteristics( - flow_def_attr, str)) - } + UBASE_ERROR(upipe, uref_pic_flow_set_transfer_characteristics_val( + flow_def_attr, frame->color_trc)) - str = NULL; - switch (frame->colorspace) { - case AVCOL_SPC_RGB: str = "GBR"; break; - case AVCOL_SPC_BT709: str = "bt709"; break; - case AVCOL_SPC_FCC: str = "fcc"; break; - case AVCOL_SPC_BT470BG: str = "bt470bg"; break; - case AVCOL_SPC_SMPTE170M: str = "smpte170m"; break; - case AVCOL_SPC_SMPTE240M: str = "smpte240m"; break; - case AVCOL_SPC_YCGCO: str = "YCgCo"; break; - case AVCOL_SPC_BT2020_NCL: str = "bt2020nc"; break; - case AVCOL_SPC_BT2020_CL: str = "bt2020c"; break; - case AVCOL_SPC_SMPTE2085: str = "smpte2085"; break; - case AVCOL_SPC_CHROMA_DERIVED_NCL: str = "chroma-nc"; break; - case AVCOL_SPC_CHROMA_DERIVED_CL: str = "chroma-c"; break; - case AVCOL_SPC_ICTCP: str = "ictcp"; break; - default: break; - } - if (str != NULL) { - UBASE_ERROR(upipe, uref_pic_flow_set_matrix_coefficients( - flow_def_attr, str)) - } + UBASE_ERROR(upipe, uref_pic_flow_set_matrix_coefficients_val( + flow_def_attr, frame->colorspace)) struct uref *flow_def = upipe_avfilt_store_flow_def_attr(upipe, flow_def_attr); @@ -2323,54 +2263,6 @@ static void upipe_avfilt_output_frame(struct upipe *upipe, upipe_avfilt_output(upipe, uref, upump_p); } -/** @This allows to convert from Upipe color space to av color space. */ -struct upipe_av_color { - /** Upipe color */ - const char *upipe_color; - /** av color */ - const char *av_color; -}; - -/** @This converts transfer characteristics from Upipe to av. */ -static const struct upipe_av_color upipe_av_color_transfer[] = { - { "bt470m", "gamma22" }, - { "bt470bg", "gamma28" }, - { "log100", "log" }, - { "log316", "log_sqrt" }, - { "iec61966-2-4", "iec61966_2_4" }, - { "bt1361e", "bt1361" }, - { "iec61966-2-1", "iec61966_2_1" }, - { "bt2020-10", "bt2020-10bit" }, - { "bt2020-12", "bt2020-12bit" }, - { NULL, NULL } -}; - -/** @This converts matrix coefficients from Upipe to av. */ -static const struct upipe_av_color upipe_av_color_space[] = { - { "GBR", "rgb" }, - { "YCgCo", "ycocg" }, - { "bt2020nc", "bt2020_ncl" }, - { "bt2020c", "bt2020_cl" }, - { NULL, NULL } -}; - -/** @internal @This converts a Upipe color space to av color space. - * - * @param list conversion list - * @param upipe_color Upipe color - * @return av color - */ -static const char *upipe_av_convert_color( - const struct upipe_av_color *list, - const char *upipe_color) -{ - for (int i = 0; list[i].upipe_color != NULL; i++) - if (!strcmp(list[i].upipe_color, upipe_color)) - return list[i].av_color; - - return upipe_color; -} - /** @internal @This converts an uref pic to an avframe. * * @param upipe description structure of the pipe @@ -2429,33 +2321,18 @@ static int upipe_avfilt_avframe_from_uref_pic(struct upipe *upipe, frame->interlaced_frame = !ubase_check(uref_pic_get_progressive(uref)); frame->top_field_first = ubase_check(uref_pic_get_tff(uref)); -#define frame_set(name, api, content) \ - if (ubase_check(ret)) { \ - if ((err = av_ ## api ## _from_name(content)) < 0) \ - upipe_err_va(upipe, "can't set option " #name "=%s: %s", \ - content, av_err2str(err)); \ - else frame->name = err; \ - } - struct uref *flow_def = upipe_avfilt->flow_def_input; - const char *content; - int err, ret = UBASE_ERR_NONE; frame->color_range = ubase_check(uref_pic_flow_get_full_range( flow_def)) ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG; - ret = uref_pic_flow_get_colour_primaries(flow_def, &content); - frame_set(color_primaries, color_primaries, content); - - ret = uref_pic_flow_get_transfer_characteristics(flow_def, &content); - frame_set(color_trc, color_transfer, - upipe_av_convert_color(upipe_av_color_transfer, content)); - - ret = uref_pic_flow_get_matrix_coefficients(flow_def, &content); - frame_set(colorspace, color_space, - upipe_av_convert_color(upipe_av_color_space, content)); - -#undef frame_set + int val; + if (ubase_check(uref_pic_flow_get_colour_primaries_val(flow_def, &val))) + frame->color_primaries = val; + if (ubase_check(uref_pic_flow_get_transfer_characteristics_val(flow_def, &val))) + frame->color_trc = val; + if (ubase_check(uref_pic_flow_get_matrix_coefficients_val(flow_def, &val))) + frame->colorspace = val; uint64_t number; if (ubase_check(uref_pic_get_number(uref, &number))) diff --git a/lib/upipe-framers/upipe_h264_framer.c b/lib/upipe-framers/upipe_h264_framer.c index c50d2b082..8495772c3 100644 --- a/lib/upipe-framers/upipe_h264_framer.c +++ b/lib/upipe-framers/upipe_h264_framer.c @@ -960,122 +960,29 @@ static bool upipe_h264f_activate_sps(struct upipe *upipe, uint32_t sps_id) UBASE_FATAL(upipe, uref_pic_flow_set_full_range(flow_def)) } - const char *colour_primaries_str = NULL; - switch (colour_primaries) { - case 1: - colour_primaries_str = "bt709"; - break; - case 4: - colour_primaries_str = "bt470m"; - break; - case 5: - colour_primaries_str = "bt470bg"; - break; - case 6: - colour_primaries_str = "smpte170m"; - break; - case 7: - colour_primaries_str = "smpte240m"; - break; - case 8: - colour_primaries_str = "film"; - break; - case 9: - colour_primaries_str = "bt2020"; - break; - default: - break; - } - if (colour_primaries_str != NULL) { - UBASE_FATAL(upipe, uref_pic_flow_set_colour_primaries(flow_def, - colour_primaries_str)) - } - - const char *transfer_characteristics_str = NULL; - switch (transfer_characteristics) { - case 1: - transfer_characteristics_str = "bt709"; - break; - case 4: - transfer_characteristics_str = "bt470m"; - break; - case 5: - transfer_characteristics_str = "bt470bg"; - break; - case 6: - transfer_characteristics_str = "smpte170m"; - break; - case 7: - transfer_characteristics_str = "smpte240m"; - break; - case 8: - transfer_characteristics_str = "linear"; - break; - case 9: - transfer_characteristics_str = "log100"; - break; - case 10: - transfer_characteristics_str = "log316"; - break; - case 11: - transfer_characteristics_str = "iec61966-2-4"; - break; - case 12: - transfer_characteristics_str = "bt1361e"; - break; - case 13: - transfer_characteristics_str = "iec61966-2-1"; - break; - case 14: - transfer_characteristics_str = "bt2020-10"; - break; - case 15: - transfer_characteristics_str = "bt2020-12"; - break; - default: - break; - } - if (transfer_characteristics_str != NULL) { - UBASE_FATAL(upipe, uref_pic_flow_set_transfer_characteristics(flow_def, - transfer_characteristics_str)) - } - - const char *matrix_coefficients_str = NULL; - switch (matrix_coefficients) { - case 0: - matrix_coefficients_str = "GBR"; - break; - case 1: - matrix_coefficients_str = "bt709"; - break; - case 4: - matrix_coefficients_str = "fcc"; - break; - case 5: - matrix_coefficients_str = "bt470bg"; - break; - case 6: - matrix_coefficients_str = "smpte170m"; - break; - case 7: - matrix_coefficients_str = "smpte240m"; - break; - case 8: - matrix_coefficients_str = "YCgCo"; - break; - case 9: - matrix_coefficients_str = "bt2020nc"; - break; - case 10: - matrix_coefficients_str = "bt2020c"; - break; - default: - break; - } - if (matrix_coefficients_str != NULL) { - UBASE_FATAL(upipe, uref_pic_flow_set_matrix_coefficients(flow_def, - matrix_coefficients_str)) - } + int err; + + err = uref_pic_flow_set_colour_primaries_val(flow_def, colour_primaries); + if (unlikely(err == UBASE_ERR_UNHANDLED)) + upipe_err_va(upipe, "unknown colour primaries %u", colour_primaries); + else if (unlikely(!ubase_check(err))) + upipe_throw_fatal(upipe, err); + + err = uref_pic_flow_set_transfer_characteristics_val( + flow_def, transfer_characteristics); + if (unlikely(err == UBASE_ERR_UNHANDLED)) + upipe_err_va(upipe, "unknown transfer characteristics %u", + transfer_characteristics); + else if (unlikely(!ubase_check(err))) + upipe_throw_fatal(upipe, err); + + err = uref_pic_flow_set_matrix_coefficients_val( + flow_def, matrix_coefficients); + if (unlikely(err == UBASE_ERR_UNHANDLED)) + upipe_err_va(upipe, "unknown matrix coefficients %u", + matrix_coefficients); + else if (unlikely(!ubase_check(err))) + upipe_throw_fatal(upipe, err); upipe_h264f->active_sps = sps_id; ubuf_block_stream_clean(s); diff --git a/lib/upipe-framers/upipe_h265_framer.c b/lib/upipe-framers/upipe_h265_framer.c index 3ced172d6..172a340c1 100644 --- a/lib/upipe-framers/upipe_h265_framer.c +++ b/lib/upipe-framers/upipe_h265_framer.c @@ -1154,122 +1154,29 @@ static bool upipe_h265f_activate_sps(struct upipe *upipe, uint32_t sps_id) UBASE_FATAL(upipe, uref_pic_flow_set_full_range(flow_def)) } - const char *colour_primaries_str = NULL; - switch (colour_primaries) { - case 1: - colour_primaries_str = "bt709"; - break; - case 4: - colour_primaries_str = "bt470m"; - break; - case 5: - colour_primaries_str = "bt470bg"; - break; - case 6: - colour_primaries_str = "smpte170m"; - break; - case 7: - colour_primaries_str = "smpte240m"; - break; - case 8: - colour_primaries_str = "film"; - break; - case 9: - colour_primaries_str = "bt2020"; - break; - default: - break; - } - if (colour_primaries_str != NULL) { - UBASE_FATAL(upipe, uref_pic_flow_set_colour_primaries(flow_def, - colour_primaries_str)) - } - - const char *transfer_characteristics_str = NULL; - switch (transfer_characteristics) { - case 1: - transfer_characteristics_str = "bt709"; - break; - case 4: - transfer_characteristics_str = "bt470m"; - break; - case 5: - transfer_characteristics_str = "bt470bg"; - break; - case 6: - transfer_characteristics_str = "smpte170m"; - break; - case 7: - transfer_characteristics_str = "smpte240m"; - break; - case 8: - transfer_characteristics_str = "linear"; - break; - case 9: - transfer_characteristics_str = "log100"; - break; - case 10: - transfer_characteristics_str = "log316"; - break; - case 11: - transfer_characteristics_str = "iec61966-2-4"; - break; - case 12: - transfer_characteristics_str = "bt1361e"; - break; - case 13: - transfer_characteristics_str = "iec61966-2-1"; - break; - case 14: - transfer_characteristics_str = "bt2020-10"; - break; - case 15: - transfer_characteristics_str = "bt2020-12"; - break; - default: - break; - } - if (transfer_characteristics_str != NULL) { - UBASE_FATAL(upipe, uref_pic_flow_set_transfer_characteristics(flow_def, - transfer_characteristics_str)) - } - - const char *matrix_coefficients_str = NULL; - switch (matrix_coefficients) { - case 0: - matrix_coefficients_str = "GBR"; - break; - case 1: - matrix_coefficients_str = "bt709"; - break; - case 4: - matrix_coefficients_str = "fcc"; - break; - case 5: - matrix_coefficients_str = "bt470bg"; - break; - case 6: - matrix_coefficients_str = "smpte170m"; - break; - case 7: - matrix_coefficients_str = "smpte240m"; - break; - case 8: - matrix_coefficients_str = "YCgCo"; - break; - case 9: - matrix_coefficients_str = "bt2020nc"; - break; - case 10: - matrix_coefficients_str = "bt2020c"; - break; - default: - break; - } - if (matrix_coefficients_str != NULL) { - UBASE_FATAL(upipe, uref_pic_flow_set_matrix_coefficients(flow_def, - matrix_coefficients_str)) - } + int err; + + err = uref_pic_flow_set_colour_primaries_val(flow_def, colour_primaries); + if (unlikely(err == UBASE_ERR_UNHANDLED)) + upipe_err_va(upipe, "unknown colour primaries %u", colour_primaries); + else if (unlikely(!ubase_check(err))) + upipe_throw_fatal(upipe, err); + + err = uref_pic_flow_set_transfer_characteristics_val( + flow_def, transfer_characteristics); + if (unlikely(err == UBASE_ERR_UNHANDLED)) + upipe_err_va(upipe, "unknown transfer characteristics %u", + transfer_characteristics); + else if (unlikely(!ubase_check(err))) + upipe_throw_fatal(upipe, err); + + err = uref_pic_flow_set_matrix_coefficients_val( + flow_def, matrix_coefficients); + if (unlikely(err == UBASE_ERR_UNHANDLED)) + upipe_err_va(upipe, "unknown matrix coefficients %u", + matrix_coefficients); + else if (unlikely(!ubase_check(err))) + upipe_throw_fatal(upipe, err); upipe_h265f->active_sps = sps_id; ubuf_block_stream_clean(s); diff --git a/lib/upipe-framers/upipe_h26x_common.c b/lib/upipe-framers/upipe_h26x_common.c index 32f3a184a..38b04112d 100644 --- a/lib/upipe-framers/upipe_h26x_common.c +++ b/lib/upipe-framers/upipe_h26x_common.c @@ -27,6 +27,7 @@ #include "upipe/ubuf_block.h" #include "upipe/ubuf_block_stream.h" #include "upipe/uref_block.h" +#include "upipe/uref_pic_flow.h" #include "upipe-framers/uref_h26x_flow.h" #include "upipe-framers/uref_h26x.h" #include "upipe-framers/upipe_h26x_common.h" diff --git a/lib/upipe-framers/upipe_mpgv_framer.c b/lib/upipe-framers/upipe_mpgv_framer.c index 1c44db41a..ba1df50ec 100644 --- a/lib/upipe-framers/upipe_mpgv_framer.c +++ b/lib/upipe-framers/upipe_mpgv_framer.c @@ -597,83 +597,29 @@ static bool upipe_mpgvf_parse_sequence(struct upipe *upipe) video_format_str)) } - const char *colour_primaries_str = NULL; - switch (colour_primaries) { - case 1: - colour_primaries_str = "bt709"; - break; - case 4: - colour_primaries_str = "bt470m"; - break; - case 5: - colour_primaries_str = "bt470bg"; - break; - case 6: - colour_primaries_str = "smpte170m"; - break; - case 7: - colour_primaries_str = "smpte240m"; - break; - default: - break; - } - if (colour_primaries_str != NULL) { - UBASE_FATAL(upipe, uref_pic_flow_set_colour_primaries(flow_def, - colour_primaries_str)) - } - - const char *transfer_characteristics_str = NULL; - switch (transfer_characteristics) { - case 1: - transfer_characteristics_str = "bt709"; - break; - case 4: - transfer_characteristics_str = "bt470m"; - break; - case 5: - transfer_characteristics_str = "bt470bg"; - break; - case 6: - transfer_characteristics_str = "smpte170m"; - break; - case 7: - transfer_characteristics_str = "smpte240m"; - break; - case 8: - transfer_characteristics_str = "linear"; - break; - default: - break; - } - if (transfer_characteristics_str != NULL) { - UBASE_FATAL(upipe, uref_pic_flow_set_transfer_characteristics(flow_def, - transfer_characteristics_str)) - } - - const char *matrix_coefficients_str = NULL; - switch (matrix_coefficients) { - case 1: - matrix_coefficients_str = "bt709"; - break; - case 4: - matrix_coefficients_str = "fcc"; - break; - case 5: - matrix_coefficients_str = "bt470bg"; - break; - case 6: - matrix_coefficients_str = "smpte170m"; - break; - case 7: - matrix_coefficients_str = "smpte240m"; - break; - default: - break; - } - if (matrix_coefficients_str != NULL) { - UBASE_FATAL(upipe, uref_pic_flow_set_matrix_coefficients(flow_def, - matrix_coefficients_str)) - } + int err; + + err = uref_pic_flow_set_colour_primaries_val(flow_def, colour_primaries); + if (unlikely(err == UBASE_ERR_UNHANDLED)) + upipe_err_va(upipe, "unknown colour primaries %u", colour_primaries); + else if (unlikely(!ubase_check(err))) + upipe_throw_fatal(upipe, err); + + err = uref_pic_flow_set_transfer_characteristics_val( + flow_def, transfer_characteristics); + if (unlikely(err == UBASE_ERR_UNHANDLED)) + upipe_err_va(upipe, "unknown transfer characteristics %u", + transfer_characteristics); + else if (unlikely(!ubase_check(err))) + upipe_throw_fatal(upipe, err); + + err = uref_pic_flow_set_matrix_coefficients_val( + flow_def, matrix_coefficients); + if (unlikely(err == UBASE_ERR_UNHANDLED)) + upipe_err_va(upipe, "unknown matrix coefficients %u", + matrix_coefficients); + else if (unlikely(!ubase_check(err))) + upipe_throw_fatal(upipe, err); upipe_mpgvf_store_flow_def(upipe, NULL); uref_free(upipe_mpgvf->flow_def_requested); diff --git a/lib/upipe-x264/upipe_x264.c b/lib/upipe-x264/upipe_x264.c index 0364396c6..8a1204573 100644 --- a/lib/upipe-x264/upipe_x264.c +++ b/lib/upipe-x264/upipe_x264.c @@ -449,28 +449,24 @@ static bool upipe_x264_open(struct upipe *upipe, int width, int height) content)) < 0) upipe_err_va(upipe, "can't set option %s:%s (%d)", "videoformat", content, ret); - content = - ubase_check(uref_pic_flow_get_full_range(upipe_x264->flow_def_input)) ? - "on" : "off"; - if ((ret = x264_param_parse(&upipe_x264->params, "fullrange", content)) < 0) - upipe_err_va(upipe, "can't set option %s:%s (%d)", - "fullrange", content, ret); - if (ubase_check(uref_pic_flow_get_colour_primaries( - upipe_x264->flow_def_input, &content)) && - (ret = x264_param_parse(&upipe_x264->params, "colorprim", content)) < 0) - upipe_err_va(upipe, "can't set option %s:%s (%d)", - "colorprim", content, ret); - if (ubase_check(uref_pic_flow_get_transfer_characteristics( - upipe_x264->flow_def_input, &content)) && - (ret = x264_param_parse(&upipe_x264->params, "transfer", content)) < 0) - upipe_err_va(upipe, "can't set option %s:%s (%d)", - "transfer", content, ret); - if (ubase_check(uref_pic_flow_get_matrix_coefficients( - upipe_x264->flow_def_input, &content)) && - (ret = x264_param_parse(&upipe_x264->params, "colormatrix", - content)) < 0) - upipe_err_va(upipe, "can't set option %s:%s (%d)", - "colormatrix", content, ret); + + upipe_x264->params.vui.b_fullrange = + ubase_check(uref_pic_flow_get_full_range(upipe_x264->flow_def_input)); + + if (!ubase_check(uref_pic_flow_get_colour_primaries_val( + upipe_x264->flow_def_input, + &upipe_x264->params.vui.i_colorprim))) + upipe_err(upipe, "cannot set colour primaries"); + + if (!ubase_check(uref_pic_flow_get_transfer_characteristics_val( + upipe_x264->flow_def_input, + &upipe_x264->params.vui.i_transfer))) + upipe_err(upipe, "cannot set transfer characteristics"); + + if (!ubase_check(uref_pic_flow_get_matrix_coefficients_val( + upipe_x264->flow_def_input, + &upipe_x264->params.vui.i_colmatrix))) + upipe_err(upipe, "cannot set matrix coefficients"); /* reconfigure encoder with new parameters and return */ if (unlikely(upipe_x264->encoder)) { diff --git a/lib/upipe/Makefile.am b/lib/upipe/Makefile.am index 249704662..d733bd754 100644 --- a/lib/upipe/Makefile.am +++ b/lib/upipe/Makefile.am @@ -14,6 +14,7 @@ libupipe_la_SOURCES = \ ubuf_sound_common.c \ ubuf_sound_mem.c \ udict_inline.c \ + uref_pic_flow.c \ uref_std.c \ uref_uri.c \ upipe_dump.c \ diff --git a/lib/upipe/uref_pic_flow.c b/lib/upipe/uref_pic_flow.c new file mode 100644 index 000000000..4316e1f68 --- /dev/null +++ b/lib/upipe/uref_pic_flow.c @@ -0,0 +1,121 @@ +/* + * Copyright (C) 2023 Clément Vasseur + * + * Authors: Clément Vasseur + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject + * to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include "upipe/ubase.h" +#include "upipe/uref_pic_flow.h" + +/** @This associates a numeric value to the corresponding string attribute. */ +struct value_desc { + int value; + const char *name; +}; + +/** @This lists colour primaries values */ +static const struct value_desc colour_primaries[] = { + { 1, "bt709" }, + { 2, NULL }, + { 4, "bt470m" }, + { 5, "bt470bg" }, + { 6, "smpte170m" }, + { 7, "smpte240m" }, + { 8, "film" }, + { 9, "bt2020" }, + { 10, "smpte428" }, + { 11, "smpte431" }, + { 12, "smpte432" }, + { 22, "ebu3213" }, +}; + +/** @This lists transfer characteristics values */ +static const struct value_desc transfer_characteristics[] = { + { 1, "bt709" }, + { 2, NULL }, + { 4, "bt470m" }, + { 5, "bt470bg" }, + { 6, "smpte170m" }, + { 7, "smpte240m" }, + { 8, "linear" }, + { 9, "log100" }, + { 10, "log316" }, + { 11, "iec61966-2-4" }, + { 12, "bt1361e" }, + { 13, "iec61966-2-1" }, + { 14, "bt2020-10" }, + { 15, "bt2020-12" }, + { 16, "smpte2084" }, + { 17, "smpte428" }, + { 18, "arib-std-b67" }, +}; + +/** @This lists matrix coefficients values */ +static const struct value_desc matrix_coefficients[] = { + { 0, "GBR" }, + { 1, "bt709" }, + { 2, NULL }, + { 4, "fcc" }, + { 5, "bt470bg" }, + { 6, "smpte170m" }, + { 7, "smpte240m" }, + { 8, "YCgCo" }, + { 9, "bt2020nc" }, + { 10, "bt2020c" }, + { 11, "smpte2085" }, + { 12, "chroma-nc" }, + { 13, "chroma-c" }, + { 14, "ictcp" }, +}; + +#define UREF_PIC_FLOW_VALUE_CONVERT(Name) \ +int uref_pic_flow_set_##Name##_val(struct uref *flow_def, int value) \ +{ \ + for (int i = 0; i < UBASE_ARRAY_SIZE(Name); i++) \ + if (Name[i].value == value) \ + return Name[i].name != NULL ? \ + uref_pic_flow_set_##Name(flow_def, Name[i].name) : \ + (uref_pic_flow_delete_##Name(flow_def), UBASE_ERR_NONE); \ + \ + return UBASE_ERR_UNHANDLED; \ +} \ + \ +int uref_pic_flow_get_##Name##_val(struct uref *flow_def, int *value) \ +{ \ + const char *name; \ + if (!ubase_check(uref_pic_flow_get_##Name(flow_def, &name))) { \ + *value = 2; \ + return UBASE_ERR_NONE; \ + } \ + \ + for (int i = 0; i < UBASE_ARRAY_SIZE(Name); i++) \ + if (Name[i].name != NULL && !strcmp(Name[i].name, name)) { \ + *value = Name[i].value; \ + return UBASE_ERR_NONE; \ + } \ + \ + return UBASE_ERR_INVALID; \ +} + +UREF_PIC_FLOW_VALUE_CONVERT(colour_primaries) +UREF_PIC_FLOW_VALUE_CONVERT(transfer_characteristics) +UREF_PIC_FLOW_VALUE_CONVERT(matrix_coefficients)