From patchwork Tue Dec 20 15:37:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitrii Ovchinnikov X-Patchwork-Id: 39736 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:bc95:b0:ad:ade2:bfd2 with SMTP id fx21csp5059176pzb; Tue, 20 Dec 2022 07:37:39 -0800 (PST) X-Google-Smtp-Source: AA0mqf5RIZkt6T2DN83OyzdQKICwWWcHhQ/lUI9oUqwIjn8CnTPKVaJzIiJ8DIOIcGzYYZ80AHNO X-Received: by 2002:a17:906:3008:b0:7b5:73aa:9984 with SMTP id 8-20020a170906300800b007b573aa9984mr40634995ejz.14.1671550659637; Tue, 20 Dec 2022 07:37:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671550659; cv=none; d=google.com; s=arc-20160816; b=crNvCbkB/Kp1zl5I8d5arIqoffw2lbMXI1cGPJxwMudmUknJHCrh2po95QtKsqZmHN 4tj7LsqJVTyTVVVSXI3CJi1Hwx/x3Q8QIXdVCLxh+IssI0PZTHoCNyvRlKFsx+QLWebh u7fhtCwHULYleg7JVjCZZGERq+P4rtvTZ0ZeYKew23ev6Nd8s8+uirdHx0PPKPTZyF3W sW6EADhtlHZbReO5EQ1NOjp/fyg2a6DSQgeBK06ribLJ67S/Gb1d0P7HJzmaO9znsnwu MHlhYA00XA289SQnRP5HDx99RftwqHEEZpQ7kwyfCVUbDlVGf/ITpdDluIKA9yCv1fUn x4Kg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:delivered-to; bh=5EGZ+q0BP8clLuG4qvQlgHV7pNfC6M5DHqzDNKK+k2o=; b=Zc6hehwNw7IgNLWkvS0Z++HymlyFstCTwL2mDy8wgb9fQC+3vAprQJbBXb0hYPY+km Sv9GFbLtmnabGEJeh1S1bwUBQ+Tr7/MPvh1PgBh3ZUAXRLI1YM5hu1Zo9H8jOLK2BFwL WsUzoA/4EPq6uBkXL7sk8ePq4Br/zgzj+A5eWFBDr6A3T0uETRLtxKB5QLnPTblL/zhp WcUr1gRDKWlu9fdcDq1G/5mClyvOEWnq1YtEnu3WJVVEdWBUYtXaYuz8kayD8BSdlcID dfzxy9aQJBGYF2SPBMMy2iFxUuALNYCmTkjKZvvzQYedEKn9n5cJpFi4hRroQgPr3qDJ xbnA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=PEq93SNv; 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id jl7-20020a17090775c700b007aa1aa8ca57si346973ejc.859.2022.12.20.07.37.39; Tue, 20 Dec 2022 07:37:39 -0800 (PST) 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; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=PEq93SNv; 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C302268BA97; Tue, 20 Dec 2022 17:37:27 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D58F9680305 for ; Tue, 20 Dec 2022 17:37:20 +0200 (EET) Received: by mail-wr1-f41.google.com with SMTP id bx10so12159703wrb.0 for ; Tue, 20 Dec 2022 07:37:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=LwMkKzNv8tO1FOx4JOqUFLi+vYw7HszstG18w+CFIys=; b=PEq93SNv4Clmz6DZMTWnKv5wtiDlTWMzDvJiJcHrtEPrCSOqs4bOHQZ+4LRuuYkfIG T/uaMDg5mNQG/21ijM0nU04gVvvhWG7r4kyXRutEY7scF3cPft2tdhkxgLhxhTeCeX6a gjlqHXzZFW36MF8YJx1NeXYAkOoIlFruripW0fIg3PEbR5twTYcB6nhXV9x2jO8UJFtO Ii0kTn44wkAgjPH9yp5ZlgRa4tPu/R+vAXs/+WeTZ4DWSshhBlXlUnM0ggz5LaS971dx mempnvlCtrK3hSmZDcFWADcsPvDbL9J0xo45hCeF9xw2e0gmEguM1gk0CQm/D9eyvdrT r4dA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LwMkKzNv8tO1FOx4JOqUFLi+vYw7HszstG18w+CFIys=; b=Er+9/+XqqXf8b+fMaseZ+2fcuJV65U+DDgiZtvzym9/UAHaJSZCDkh98L3jDMJ+5zY w5Wyn0A3oHJ3+mBDbgM+kEyGH06DBWbg0nnANkFd3a311w3+XqVEp3GVVd7olhopu48d dKCx5Rxn1A9XZVEgknrD0QF692JR3i+cTA2sdmDY7hBA3A70lYH+/5xKtvI8zvb9HfTT io1A5k4LhrUbW0uQ0pzruhkGotVgygBUnM5NQIUWkddZNJbydzLj9GyHRVR5uIfGjY5p bdMPrggLMXxMbf2Am8AL3eN92Mt1qBaEbepsIHZxlQHkN+/4EUDS5ka4s+yo/Mfp7gMX jvxw== X-Gm-Message-State: AFqh2kr0aAkvvY5P7KEs9Z5r4aKUDmHBBwzv0n78bl6xvfmDn9NxcwgK zhpw+/LbMecixvbkPeEl0CPfFcUk4Ro= X-Received: by 2002:adf:e6c4:0:b0:255:96ed:950b with SMTP id y4-20020adfe6c4000000b0025596ed950bmr17296694wrm.60.1671550639978; Tue, 20 Dec 2022 07:37:19 -0800 (PST) Received: from dovchinn.amd.com (109-93-70-158.dynamic.isp.telekom.rs. [109.93.70.158]) by smtp.gmail.com with ESMTPSA id y1-20020adfd081000000b002421a8f4fa6sm13167716wrh.92.2022.12.20.07.37.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Dec 2022 07:37:19 -0800 (PST) From: Dmitrii Ovchinnikov X-Google-Original-From: Dmitrii Ovchinnikov To: ffmpeg-devel@ffmpeg.org Date: Tue, 20 Dec 2022 16:37:10 +0100 Message-Id: <20221220153711.1831-2-Dmitriy.Ovchinnikov@amd.com> X-Mailer: git-send-email 2.38.1.windows.1 In-Reply-To: <20221220153711.1831-1-Dmitriy.Ovchinnikov@amd.com> References: <20221220153711.1831-1-Dmitriy.Ovchinnikov@amd.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [crop support for matroska demuxer, V4 2/3] libavcodec: Public code to support container crop 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 Cc: Dmitrii Ovchinnikov Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 8gTUY2mrjifO From: Dmitrii Ovchinnikov Support both simple and receive_frame api The container crop information is applied additional to frame crop information --- libavcodec/codec_par.c | 30 ++++++++++++++--------- libavcodec/decode.c | 54 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 11 deletions(-) diff --git a/libavcodec/codec_par.c b/libavcodec/codec_par.c index abda649aa8..9738402434 100644 --- a/libavcodec/codec_par.c +++ b/libavcodec/codec_par.c @@ -115,17 +115,21 @@ int avcodec_parameters_from_context(AVCodecParameters *par, switch (par->codec_type) { case AVMEDIA_TYPE_VIDEO: - par->format = codec->pix_fmt; - par->width = codec->width; - par->height = codec->height; - par->field_order = codec->field_order; - par->color_range = codec->color_range; - par->color_primaries = codec->color_primaries; - par->color_trc = codec->color_trc; - par->color_space = codec->colorspace; - par->chroma_location = codec->chroma_sample_location; - par->sample_aspect_ratio = codec->sample_aspect_ratio; - par->video_delay = codec->has_b_frames; + par->format = codec->pix_fmt; + par->width = codec->width; + par->height = codec->height; + par->container_crop_top = codec->container_crop_top; + par->container_crop_left = codec->container_crop_left; + par->container_crop_bottom = codec->container_crop_bottom; + par->container_crop_right = codec->container_crop_right; + par->field_order = codec->field_order; + par->color_range = codec->color_range; + par->color_primaries = codec->color_primaries; + par->color_trc = codec->color_trc; + par->color_space = codec->colorspace; + par->chroma_location = codec->chroma_sample_location; + par->sample_aspect_ratio = codec->sample_aspect_ratio; + par->video_delay = codec->has_b_frames; break; case AVMEDIA_TYPE_AUDIO: par->format = codec->sample_fmt; @@ -199,6 +203,10 @@ int avcodec_parameters_to_context(AVCodecContext *codec, codec->pix_fmt = par->format; codec->width = par->width; codec->height = par->height; + codec->container_crop_top = par->container_crop_top; + codec->container_crop_left = par->container_crop_left; + codec->container_crop_bottom = par->container_crop_bottom; + codec->container_crop_right = par->container_crop_right; codec->field_order = par->field_order; codec->color_range = par->color_range; codec->color_primaries = par->color_primaries; diff --git a/libavcodec/decode.c b/libavcodec/decode.c index 6be2d3d6ed..9e44fcb293 100644 --- a/libavcodec/decode.c +++ b/libavcodec/decode.c @@ -693,6 +693,60 @@ static int apply_cropping(AVCodecContext *avctx, AVFrame *frame) if (!avctx->apply_cropping) return 0; + if (avctx->container_apply_cropping == FF_CONTAINER_CROPPING_ADDITION) + { + /*check if container parameter and elementary streaming cropping parameters are saft for apploying */ + if (avctx->container_crop_left + frame->crop_left >= INT_MAX - (avctx->container_crop_right + frame->crop_right) || + avctx->container_crop_top + frame->crop_top >= INT_MAX - (avctx->container_crop_bottom + frame->crop_bottom) || + (avctx->container_crop_left + frame->crop_left + avctx->container_crop_right + frame->crop_right) >= frame->width || + (avctx->container_crop_top + frame->crop_top + avctx->container_crop_bottom + frame->crop_bottom) >= frame->height) { + av_log(avctx, AV_LOG_WARNING, + "Apply container and elementary stream corpping parametes error: " + "container cropping paramters " + "%d/%d/%d/%d " + "elementary stream croping paramters " + "%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER" " + "(frame size %dx%d). This is a bug, please report it\n", + avctx->container_crop_left, avctx->container_crop_right, avctx->container_crop_top, avctx->container_crop_bottom, + frame->crop_left, frame->crop_right, frame->crop_top, frame->crop_bottom, + frame->width, frame->height); + frame->crop_left = 0; + frame->crop_right = 0; + frame->crop_top = 0; + frame->crop_bottom = 0; + return 0; + } + + frame->crop_top += (size_t)avctx->container_crop_top; + frame->crop_left += (size_t)avctx->container_crop_left; + frame->crop_bottom += (size_t)avctx->container_crop_bottom; + frame->crop_right += (size_t)avctx->container_crop_right; + }else if (avctx->container_apply_cropping == FF_CONTAINER_CROPPING_OVERWRITE) { + + /*check the croppping parameters from container are reasonable and correct*/ + if (avctx->container_crop_left >= INT_MAX - avctx->container_crop_right || + avctx->container_crop_top >= INT_MAX - avctx->container_crop_bottom || + (avctx->container_crop_left + avctx->container_crop_right) >= frame->width || + (avctx->container_crop_top + avctx->container_crop_bottom) >= frame->height) { + av_log(avctx, AV_LOG_WARNING, + "Invalid container cropping information set by a demuxer: " + "%d/%d/%d/%d " + "(frame size %dx%d). This is a bug, please report it\n", + avctx->container_crop_left, avctx->container_crop_right, avctx->container_crop_top, avctx->container_crop_bottom, + frame->width, frame->height); + frame->crop_left = 0; + frame->crop_right = 0; + frame->crop_top = 0; + frame->crop_bottom = 0; + return 0; + } + + frame->crop_top = (size_t)avctx->container_crop_top; + frame->crop_left = (size_t)avctx->container_crop_left; + frame->crop_bottom = (size_t)avctx->container_crop_bottom; + frame->crop_right = (size_t)avctx->container_crop_right; + } + return av_frame_apply_cropping(frame, avctx->flags & AV_CODEC_FLAG_UNALIGNED ? AV_FRAME_CROP_UNALIGNED : 0); }