From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 4845946E18 for ; Thu, 13 Jul 2023 11:01:57 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8F32868C73F; Thu, 13 Jul 2023 13:58:19 +0300 (EEST) Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7B42068C693 for ; Thu, 13 Jul 2023 13:57:56 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 483762404EE for ; Thu, 13 Jul 2023 12:57:56 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id EjJgDNSp6UgB for ; Thu, 13 Jul 2023 12:57:55 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id D593F240D20 for ; Thu, 13 Jul 2023 12:57:41 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 10F373A1E4B for ; Thu, 13 Jul 2023 12:57:36 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 13 Jul 2023 12:55:44 +0200 Message-Id: <20230713105553.21052-24-anton@khirnov.net> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230713105553.21052-1-anton@khirnov.net> References: <20230713105553.21052-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 24/33] fftools/ffmpeg_mux_init: handle pixel format endianness X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: When -pix_fmt designates a BE/LE pixel format, it gets translated into the native one by av_get_pix_fmt(). This may not always be the best choice, as the encoder might only support one endianness. In such a case, explicitly choose the endianness supported by the encoder. While this is currently redundant with choose_pixel_fmt() in ffmpeg_filter.c, the latter code will be removed in following commits. --- fftools/ffmpeg_mux_init.c | 55 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c index 2dba76b82b..4434e6b70d 100644 --- a/fftools/ffmpeg_mux_init.c +++ b/fftools/ffmpeg_mux_init.c @@ -495,6 +495,54 @@ static int parse_matrix_coeffs(void *logctx, uint16_t *dest, const char *str) return 0; } +static int fmt_in_list(const int *formats, int format) +{ + for (; *formats != -1; formats++) + if (*formats == format) + return 1; + return 0; +} + +static enum AVPixelFormat pix_fmt_parse(OutputStream *ost, const char *name) +{ + const enum AVPixelFormat *fmts = ost->enc_ctx->codec->pix_fmts; + enum AVPixelFormat fmt; + + fmt = av_get_pix_fmt(name); + if (fmt == AV_PIX_FMT_NONE) { + av_log(ost, AV_LOG_FATAL, "Unknown pixel format requested: %s.\n", name); + return AV_PIX_FMT_NONE; + } + + /* when the user specified-format is an alias for an endianness-specific + * one (e.g. rgb48 -> rgb48be/le), it gets translated into the native + * endianness by av_get_pix_fmt(); + * the following code handles the case when the native endianness is not + * supported by the encoder, but the other one is */ + if (fmts && !fmt_in_list(fmts, fmt)) { + const char *name_canonical = av_get_pix_fmt_name(fmt); + int len = strlen(name_canonical); + + if (strcmp(name, name_canonical) && + (!strcmp(name_canonical + len - 2, "le") || + !strcmp(name_canonical + len - 2, "be"))) { + char name_other[64]; + enum AVPixelFormat fmt_other; + + snprintf(name_other, sizeof(name_other), "%s%ce", + name, name_canonical[len - 2] == 'l' ? 'b' : 'l'); + fmt_other = av_get_pix_fmt(name_other); + if (fmt_other != AV_PIX_FMT_NONE && fmt_in_list(fmts, fmt_other)) { + av_log(ost, AV_LOG_VERBOSE, "Mapping pixel format %s->%s\n", + name, name_other); + fmt = fmt_other; + } + } + } + + return fmt; +} + static int new_stream_video(Muxer *mux, const OptionsContext *o, OutputStream *ost) { @@ -558,9 +606,10 @@ static int new_stream_video(Muxer *mux, const OptionsContext *o, if (!*++frame_pix_fmt) frame_pix_fmt = NULL; } - if (frame_pix_fmt && (video_enc->pix_fmt = av_get_pix_fmt(frame_pix_fmt)) == AV_PIX_FMT_NONE) { - av_log(ost, AV_LOG_FATAL, "Unknown pixel format requested: %s.\n", frame_pix_fmt); - return AVERROR(EINVAL); + if (frame_pix_fmt) { + video_enc->pix_fmt = pix_fmt_parse(ost, frame_pix_fmt); + if (video_enc->pix_fmt == AV_PIX_FMT_NONE) + return AVERROR(EINVAL); } MATCH_PER_STREAM_OPT(intra_matrices, str, intra_matrix, oc, st); -- 2.40.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".