From patchwork Sat Jul 15 10:45:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 42690 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:6da1:b0:131:a7d0:bc6d with SMTP id gl33csp4239003pzb; Sat, 15 Jul 2023 03:46:38 -0700 (PDT) X-Google-Smtp-Source: APBJJlHJhMDJM+XOU3QQX+Ne9wVlReWN6Nc+wC05ZM4ZDKw0h2zU4wa2JlAzUR0/fV8jFmG/3Mg+ X-Received: by 2002:a50:ec98:0:b0:51e:720:3ae7 with SMTP id e24-20020a50ec98000000b0051e07203ae7mr6596341edr.3.1689417998186; Sat, 15 Jul 2023 03:46:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689417998; cv=none; d=google.com; s=arc-20160816; b=sCVal+/iKK6p9YxEIqBrQyRgFH4/ik7LxWp2pnNpGs4/lpTdq80XEux9d0fxIlOhN/ 1p6k7Exm4/GG06tC8q5JgJvd4MqpaWMXYj+9NUUnFKWgOt/AjiDH4YZZRmq1sw2Ml7qg TqyYlu9TKIuHsqGDmRC4S2HSwhhwwT3elCECUyoKihDVWHfvhiVKeBMQRlHSmHjpsww7 BjAVpsy+tvgrzUZ4RfmC8VZP6fxcgGazBpwoeJaoNQaHCvju+fBqo/BNFtCInZ2aXami ha27IMU937I1lGqRvPvZ3P5TyH56AuQyYG6A9EwNhLKY+jBwthGQgdfhuZ2QtZSge75t +WxA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:message-id:date:to:from :delivered-to; bh=Ahc+PlaUOMFgyq5ZszQET1I0AcWzmb6+4tq25fC2BQ0=; fh=8ZF2QtiKOjBtyn9wuO2cMeZaLsE4aq02Bq77gGK61E8=; b=m8KM/wthewV9zI64Nj3ZmMTj5a3NiPEIAfRzDWiTBMOJG37cuLHfneCwdy4kGNAE1g F9ZFIWi6Kljwd20Pu2ob+dIbO/IUELwG8zEUHrQp8PSnTTBsHkVFzWGEWa25X7dPDwib 90LoZLV0D63H4yVqXpIM29nWbrB0GMmVqLpaS68sGv27DtFAlhM6wECQTXGwtyu1gBAe 47z8btMSjgvzga6MLpoJ4aqqmISZbuWizvQaBgkWGgwrL50UX4FAI8DjegBP1WqZVuP6 tiV4O++s18nMoQauF79qU25rfix3gxe2P5cZ7/TeNWZt3ss4Ai54n5RZaCsDfCuHK2fZ tgSw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id d4-20020a056402078400b0051fea43cedfsi1374119edy.384.2023.07.15.03.46.37; Sat, 15 Jul 2023 03:46:38 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0EAD068C558; Sat, 15 Jul 2023 13:46:34 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7CBE068C4C7 for ; Sat, 15 Jul 2023 13:46:27 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 3FBC8240D30 for ; Sat, 15 Jul 2023 12:46:27 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id rc4mWfaoZ5r7 for ; Sat, 15 Jul 2023 12:46:26 +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 7395D2404EA for ; Sat, 15 Jul 2023 12:46:26 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id F39E03A0153 for ; Sat, 15 Jul 2023 12:46:19 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Sat, 15 Jul 2023 12:45:25 +0200 Message-Id: <20230715104611.17902-1-anton@khirnov.net> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 01/47] 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: XAfMCZZrX5Nv 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 deprecated 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 6458414b5f..2d45fa7c7c 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);