From patchwork Tue Sep 18 22:30:59 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Thompson X-Patchwork-Id: 10379 Delivered-To: ffmpegpatchwork@gmail.com Received: by 2002:a02:12c4:0:0:0:0:0 with SMTP id 65-v6csp13352jap; Tue, 18 Sep 2018 15:34:46 -0700 (PDT) X-Google-Smtp-Source: ANB0VdYynYvHmifpVJbOswsZ2rSxOC+wYK+O0yuRomm0psN+3ZfUyiawM1UfW+mYfTD+ZAATiGQW X-Received: by 2002:adf:ef51:: with SMTP id c17-v6mr25418325wrp.182.1537310086043; Tue, 18 Sep 2018 15:34:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537310086; cv=none; d=google.com; s=arc-20160816; b=C0e+UBd4eB/J7tPOgaiB5LvP5iyoS6CBlZreBx8dsJu7vw4X8tt2mqtTtpsXvQq7UU pix3MylBMDUuYXMAg4RRr/H1uitAVl+hw/bEiyfz5Z2evLJYMMN3HWDbjG0uWRnDqnGz +2ROeP2kNmwBWWUUA4iSKAxnhevdDREiYlIuhy1Dj6CmpOzoovhkHUVCJjyNkOe/t9R5 GzaC5gKr7ckvI76+z68Pv70h3nwjcBJteESNqzL4nU1BYBCL3Z5E1vIwgKZcbK7KIRiz jen8n+5VhmopvuvqYQocn5pYU/AkSnviMtfUUokw5ppKuyNFr9RiNVgz+uq39bbW7sm/ 54NA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to; bh=7ilS/KxiOc2T+n14VDo4GY8cS7odv4jmXlyCVSPmwNI=; b=XCwjgJ3dukFD1nOqTEeZu0mJyFbH51bTLOT9HY9+z3TisyUWVCkDWoe3qcvyI1pO4W pYjNB/qtaWEJuMqh3564eVkFj30Wesbbb5R+7D702KQ5P025RfbCwynjZnwLEB1B+wGx PnuyLNJtVlvq8MxPo+9SVS3W1Pomnz8vkZ+g+04sZgLEYV9Nx0wgU21ylPitw0h6+EfC wR+hmKmGuBHQAIOOsqPtewkmXv9WzidNjcN2H6cfspPwOp86xQpKrAg/15Amm7927dO/ vPozridS4J/JRZlCG0S7ESX9+5xQvM0+gfLn712bJb6mzT5aZ5bhH78GUOd7R+xMg4ij ghbA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@jkqxz-net.20150623.gappssmtp.com header.s=20150623 header.b=LT5FVG15; 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 v10-v6si3364909wrw.271.2018.09.18.15.34.36; Tue, 18 Sep 2018 15:34:46 -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; dkim=neutral (body hash did not verify) header.i=@jkqxz-net.20150623.gappssmtp.com header.s=20150623 header.b=LT5FVG15; 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 B0B2D68A5FF; Wed, 19 Sep 2018 01:31:34 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 52FF368A58B for ; Wed, 19 Sep 2018 01:31:31 +0300 (EEST) Received: by mail-wm1-f46.google.com with SMTP id f21-v6so4128161wmc.5 for ; Tue, 18 Sep 2018 15:31:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jkqxz-net.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:in-reply-to:references; bh=JIOUcDYLK4HpQEBPDJDUArkouWgUfGyIIZ3QF7Q8qfI=; b=LT5FVG15F0UwF0aAwQJ3otHyxjLc6rEQGB+S8VBa+Z6WFRHJQF05ecGKDQR55NKCNm qv/G60KSuylizpGqX0HKmVSE/KL+5Kdm2W4Jp4UW29bFmxSBRckoUW/u6QMCzTxvjIhw VEb5rVfOvgkJRyPQIpv1P5QNF5qm/4RiljzGAxdrQdpBt8aDQVslXmUulnkaVg2Er0bm vMjwwcBlXSZDTK7yJbjZqW2c0v9KvDSUWE9YTY6THt6DRkWXmNxzGghLBB/o+0XWiInB Q4776fWJ0tVUpmHPS4G1olHO1V2+ZgDcp7H4mum66janfhszWd58Fnj/fxFVgs6Vwk8T kYzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=JIOUcDYLK4HpQEBPDJDUArkouWgUfGyIIZ3QF7Q8qfI=; b=OWiDxdeSNjVmbp16dexeUmnCFCutt6zSUpw5imAv6vMB5T7FDxz6Scf9tGrBxJQOW2 VFWNA8AFYTXdQly08SgUtpfddV1Rz/Oq4OXDtsi1NBYaac9NtIy8CKtEfwUGtnvbZ7Lw 7qXJDjcasH0O4Wi7u2G4E7dbNozVA9y7LC0n8j1Rn1Was17LDKbzq9gNXpZi1F0OyMcU 1f8HcGrRvL8MlIdIudbMBY85lBdBOC9QsBzL8HE9yrShQZPy8+mey7SmMN6fGdblJViX L5XcqqN627JRAfAE2X1Ayb/NPOrL2naxboFw38YnF0bAz4vsc6f5Zdy/oKMcvGEj400j 6JhQ== X-Gm-Message-State: APzg51BENVLvdgJll96UtlW2nYdTHxuPurqoNfiMQ0LPahTmFWtsdZyj 5I72ASI7oYySGD4LQDCS6ciSWFZ096s= X-Received: by 2002:a1c:c64c:: with SMTP id w73-v6mr17607355wmf.98.1537309903437; Tue, 18 Sep 2018 15:31:43 -0700 (PDT) Received: from rywe.jkqxz.net (cpc91242-cmbg18-2-0-cust650.5-4.cable.virginm.net. [82.8.130.139]) by smtp.gmail.com with ESMTPSA id e141-v6sm4863826wmd.32.2018.09.18.15.31.42 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 18 Sep 2018 15:31:42 -0700 (PDT) From: Mark Thompson To: ffmpeg-devel@ffmpeg.org Date: Tue, 18 Sep 2018 23:30:59 +0100 Message-Id: <20180918223116.14322-22-sw@jkqxz.net> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180918223116.14322-1-sw@jkqxz.net> References: <20180918223116.14322-1-sw@jkqxz.net> Subject: [FFmpeg-devel] [PATCH v4 21/38] h264_metadata: Add option to set the level of the stream X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" --- doc/bitstream_filters.texi | 9 ++++ libavcodec/h264_metadata_bsf.c | 90 ++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi index 7d7e97503a..d948c6d658 100644 --- a/doc/bitstream_filters.texi +++ b/doc/bitstream_filters.texi @@ -215,6 +215,15 @@ insert the string ``hello'' associated with the given UUID. @item delete_filler Deletes both filler NAL units and filler SEI messages. +@item level +Set the level in the SPS. Refer to H.264 section A.3 and tables A-1 +to A-5. + +The argument must be the name of a level (for example, @samp{4.2}), a +level_idc value (for example, @samp{42}), or the special name @samp{auto} +indicating that the filter should attempt to guess the level from the +input stream properties. + @end table @section h264_mp4toannexb diff --git a/libavcodec/h264_metadata_bsf.c b/libavcodec/h264_metadata_bsf.c index 90ad4aad98..991fcfa537 100644 --- a/libavcodec/h264_metadata_bsf.c +++ b/libavcodec/h264_metadata_bsf.c @@ -25,6 +25,7 @@ #include "cbs.h" #include "cbs_h264.h" #include "h264.h" +#include "h264_levels.h" #include "h264_sei.h" enum { @@ -39,6 +40,11 @@ enum { FLIP_VERTICAL = 2, }; +enum { + LEVEL_UNSET = -2, + LEVEL_AUTO = -1, +}; + typedef struct H264MetadataContext { const AVClass *class; @@ -74,6 +80,8 @@ typedef struct H264MetadataContext { int display_orientation; double rotate; int flip; + + int level; } H264MetadataContext; @@ -208,6 +216,58 @@ static int h264_metadata_update_sps(AVBSFContext *bsf, CROP(bottom, crop_unit_y); #undef CROP + if (ctx->level != LEVEL_UNSET) { + int level_idc; + + if (ctx->level == LEVEL_AUTO) { + const H264LevelDescriptor *desc; + int64_t bit_rate; + int width, height; + + if (sps->vui.nal_hrd_parameters_present_flag) { + bit_rate = (sps->vui.nal_hrd_parameters.bit_rate_value_minus1[0] + 1) * + (1 << (sps->vui.nal_hrd_parameters.bit_rate_scale + 6)); + } else if (sps->vui.vcl_hrd_parameters_present_flag) { + bit_rate = (sps->vui.vcl_hrd_parameters.bit_rate_value_minus1[0] + 1) * + (1 << (sps->vui.vcl_hrd_parameters.bit_rate_scale + 6)); + // Adjust for VCL vs. NAL limits. + bit_rate = bit_rate * 6 / 5; + } else { + bit_rate = 0; + } + + width = 16 * (sps->pic_width_in_mbs_minus1 + 1); + height = 16 * (sps->pic_height_in_map_units_minus1 + 1) * + (2 - sps->frame_mbs_only_flag); + + desc = ff_h264_guess_level(sps->profile_idc, bit_rate, + width, height, + sps->vui.max_dec_frame_buffering); + if (desc) { + level_idc = desc->level_idc; + } else { + av_log(bsf, AV_LOG_WARNING, "Stream does not appear to " + "conform to any level: using level 6.2.\n"); + level_idc = 62; + } + } else { + level_idc = ctx->level; + } + + if (level_idc == 9) { + if (sps->profile_idc == 66 || + sps->profile_idc == 77 || + sps->profile_idc == 88) { + sps->level_idc = 10; + sps->constraint_set3_flag = 1; + } else { + sps->level_idc = 9; + } + } else { + sps->level_idc = level_idc; + } + } + if (need_vui) sps->vui_parameters_present_flag = 1; @@ -683,6 +743,36 @@ static const AVOption h264_metadata_options[] = { 0, AV_OPT_TYPE_CONST, { .i64 = FLIP_VERTICAL }, .flags = FLAGS, .unit = "flip" }, + { "level", "Set level (table A-1)", + OFFSET(level), AV_OPT_TYPE_INT, + { .i64 = LEVEL_UNSET }, LEVEL_UNSET, 0xff, FLAGS, "level" }, + { "auto", "Attempt to guess level from stream properties", + 0, AV_OPT_TYPE_CONST, + { .i64 = LEVEL_AUTO }, .flags = FLAGS, .unit = "level" }, +#define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \ + { .i64 = value }, .flags = FLAGS, .unit = "level" + { LEVEL("1", 10) }, + { LEVEL("1b", 9) }, + { LEVEL("1.1", 11) }, + { LEVEL("1.2", 12) }, + { LEVEL("1.3", 13) }, + { LEVEL("2", 20) }, + { LEVEL("2.1", 21) }, + { LEVEL("2.2", 22) }, + { LEVEL("3", 30) }, + { LEVEL("3.1", 31) }, + { LEVEL("3.2", 32) }, + { LEVEL("4", 40) }, + { LEVEL("4.1", 41) }, + { LEVEL("4.2", 42) }, + { LEVEL("5", 50) }, + { LEVEL("5.1", 51) }, + { LEVEL("5.2", 52) }, + { LEVEL("6", 60) }, + { LEVEL("6.1", 61) }, + { LEVEL("6.2", 62) }, +#undef LEVEL + { NULL } };