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 BBC41464D4 for ; Wed, 19 Jul 2023 22:21:33 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8513668C641; Thu, 20 Jul 2023 01:21:07 +0300 (EEST) Received: from mail-oa1-f44.google.com (mail-oa1-f44.google.com [209.85.160.44]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5889468C45C for ; Thu, 20 Jul 2023 01:20:58 +0300 (EEST) Received: by mail-oa1-f44.google.com with SMTP id 586e51a60fabf-1ba79f16f4cso116303fac.3 for ; Wed, 19 Jul 2023 15:20:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689805256; x=1690410056; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=3RytYDPd4ypmRrS3LU89Fe8Uznpz6xBHs7ROEgZgzh0=; b=qVgvBdKOTFwCj9yfYd79Shc2iZIUrLAwXa0bUzdmOsJ9k8c2YC6+yFFFg3tezeyjR8 ZBc1Qy7IpdWoCF12ySfNC5COvi35eOOWX39Ja9RLXRi7hoGOt8jHq9y27ezfRR56GZbe pZX1BEDMEYDTD+yCSJ+cwcQpftrGrgem+5eE5a/GFzuQ7NYZQlam9e13kl7lwRoH0WlP oiWOipBSFknXccAar9tI3xJrdWfwlP5lGdz+JYejhmSEIBEA9CSUYLtkSo6QYbGDM0Qq /URHcltphwUzMF+5xZse4Lnb4abyJ3ncyht72XiPoy4LJcL7PgYDv9u5o4ckMNBRkmQx bM7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689805256; x=1690410056; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3RytYDPd4ypmRrS3LU89Fe8Uznpz6xBHs7ROEgZgzh0=; b=eeOx/J1Iu3lFdhNnZW1DwkCncTVFEuwb0sPSD9Fw/fAtrjWRG/288SheQ4isXGqmHG sE4Bz7Ebcn9PWu0DYoSv4QK2F7dbH+YmB1c8tSUmMK3SSKmBnpKZtHAKF76MYMme4wbc XpwpVhMDQ+fa+inJdz8Ned0nDRCVSZhXLbIn3Pq0nLC+gpbst+L2Wrsie8beGa1bps0D 5r6vcLa0lPjttibk4cqbG2y18i0n7WYpwL8fotHGptgvyqSy8HnGjdUaSSWmmv68JFWq /PtGFCl4nmourbJFj9XqYpw2kJHq5vuSR4qH14gQcwJ4Yx//7XabJA42ea7Vn6s4lgHR 7Ueg== X-Gm-Message-State: ABy/qLYd9/AwAt1AGdFTcWNSdkr+T6Hx29W8ktUqWoeEKbQiDECFfnU+ LjSSiO8jCCdjd63/FVqTjk0bHGzPFz4= X-Google-Smtp-Source: APBJJlHmIaqx5YnytgIlf4cv1GC8WyLRasmn1+i5vo70Yi9Wd1BojwAbToC2+tfn0kyQfuxGJn6ZZg== X-Received: by 2002:a05:6870:d20c:b0:19f:2c0e:f865 with SMTP id g12-20020a056870d20c00b0019f2c0ef865mr24117144oac.7.1689805256638; Wed, 19 Jul 2023 15:20:56 -0700 (PDT) Received: from localhost.localdomain (host197.190-225-105.telecom.net.ar. [190.225.105.197]) by smtp.gmail.com with ESMTPSA id k2-20020a056870350200b001b36c56e6a7sm2371370oah.44.2023.07.19.15.20.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Jul 2023 15:20:56 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Wed, 19 Jul 2023 19:20:42 -0300 Message-ID: <20230719222043.59743-4-jamrial@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230719222043.59743-1-jamrial@gmail.com> References: <20230719222043.59743-1-jamrial@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 4/5] avformat/matroskaenc: support writing cropping values 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: Signed-off-by: James Almer --- libavformat/matroskaenc.c | 45 +++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index 41e13b273d..7bfe7b002a 100644 --- a/libavformat/matroskaenc.c +++ b/libavformat/matroskaenc.c @@ -1654,8 +1654,11 @@ static int mkv_write_track_video(AVFormatContext *s, MatroskaMuxContext *mkv, const AVDictionaryEntry *tag; int display_width_div = 1, display_height_div = 1; uint8_t color_space[4], projection_private[20]; + uint8_t *cropping; EBML_WRITER(MAX_FIELD_ORDER_ELEMS + MAX_STEREO_MODE_ELEMS + MAX_VIDEO_COLOR_ELEMS + MAX_VIDEO_PROJECTION_ELEMS + 8); + int cropped_width = par->width, cropped_height = par->height; + size_t cropped_size; int ret; ebml_writer_open_master(&writer, MATROSKA_ID_TRACKVIDEO); @@ -1679,25 +1682,53 @@ static int mkv_write_track_video(AVFormatContext *s, MatroskaMuxContext *mkv, ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEOALPHAMODE, 1); } + cropping = av_stream_get_side_data(st, AV_PKT_DATA_FRAME_CROPPING, &cropped_size); + if (cropping) { + uint32_t top, bottom, left, right; + if (cropped_size != sizeof(uint32_t) * 4) + return AVERROR_INVALIDDATA; + + top = AV_RL32(cropping + 0); + bottom = AV_RL32(cropping + 4); + left = AV_RL32(cropping + 8); + right = AV_RL32(cropping + 12); + + if (left >= INT_MAX - right || + top >= INT_MAX - bottom || + (left + right) >= par->width || + (top + bottom) >= par->height) { + av_log(s, AV_LOG_ERROR, "Invalid cropping dimensions in stream side data\n"); + return AVERROR(EINVAL); + } + + ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEOPIXELCROPB, bottom); + ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEOPIXELCROPT, top); + ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEOPIXELCROPL, left); + ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEOPIXELCROPR, right); + + cropped_width -= left + right; + cropped_height -= top + bottom; + } + // write DisplayWidth and DisplayHeight, they contain the size of // a single source view and/or the display aspect ratio if (st->sample_aspect_ratio.num) { - int64_t d_width = av_rescale(par->width, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den); + int64_t d_width = av_rescale(cropped_width, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den); if (d_width > INT_MAX) { av_log(s, AV_LOG_ERROR, "Overflow in display width\n"); return AVERROR(EINVAL); } - if (d_width != par->width || display_width_div != 1 || display_height_div != 1) { + if (d_width != cropped_width || display_width_div != 1 || display_height_div != 1) { if (IS_WEBM(mkv) || display_width_div != 1 || display_height_div != 1) { ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEODISPLAYWIDTH, d_width / display_width_div); ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEODISPLAYHEIGHT, - par->height / display_height_div); + cropped_height / display_height_div); } else { AVRational display_aspect_ratio; av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den, - par->width * (int64_t)st->sample_aspect_ratio.num, - par->height * (int64_t)st->sample_aspect_ratio.den, + cropped_width * (int64_t)st->sample_aspect_ratio.num, + cropped_height * (int64_t)st->sample_aspect_ratio.den, 1024 * 1024); ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEODISPLAYWIDTH, display_aspect_ratio.num); @@ -1709,9 +1740,9 @@ static int mkv_write_track_video(AVFormatContext *s, MatroskaMuxContext *mkv, } } else if (display_width_div != 1 || display_height_div != 1) { ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEODISPLAYWIDTH, - par->width / display_width_div); + cropped_width / display_width_div); ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEODISPLAYHEIGHT, - par->height / display_height_div); + cropped_height / display_height_div); } else if (!IS_WEBM(mkv)) ebml_writer_add_uint(&writer, MATROSKA_ID_VIDEODISPLAYUNIT, MATROSKA_VIDEO_DISPLAYUNIT_UNKNOWN); -- 2.41.0 _______________________________________________ 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".