From patchwork Mon Jul 31 11:50:16 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Darnley X-Patchwork-Id: 4532 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.1.85 with SMTP id 82csp2311925vsb; Mon, 31 Jul 2017 04:50:48 -0700 (PDT) X-Received: by 10.28.168.84 with SMTP id r81mr10245236wme.148.1501501848457; Mon, 31 Jul 2017 04:50:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1501501848; cv=none; d=google.com; s=arc-20160816; b=1GNuTkzfSPHhEhI9NLMgbcKSDtwfsjzf5HXJ5EokVFoaKvkEImI/F4CEpP94AMhmOD jdigpeBHXJtg/4kfr41x3iMIdupkKx72zBQzx3bE49wnIUUsDh/fFy8pzxS0p+QUD7A6 lRgFtsO/yycCuM1V8exGLkppUlgiBav/VTsCaFBo8qIz2Ml9gMa1ETRtWzFN0knuIJC3 O+AcU3PPmwNKb55M+fzMnLnOXsYHHO1jNv4h7Y+wTaY1kxWLMq5fWf2YurycFmE6BhjW iG1z6EwvjS5mgSGILZ4wQEBQXLKgwV4Xuagc9QgBqUl1jFIBImokip/AmtnF+zJb25jt tBsw== 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:arc-authentication-results; bh=xldWxZEasUoT6uf6BMTc9BdeGb6vFg6VN6wqCrza4ys=; b=dWp5A6SC7tAL/ZPi+yFTyQiJA4kmb8dVH3bBAL5bBRz/UUPkV2A6WDLn8YFNBKZrH7 6YDQIJ2xHtABoMp1tRngRQOLfYJ8VMjT/wV7I9iDOyqpr3oYxHFdwT3sxX2n30VgjVSp pXCyenG+V/OsNOKw7ey1Rp/sBpPfZF1M1Y04t5T4un7f/npuAjfhc4r8Vtmhot9cofpv lNZnN3Y2n9GjisR6YD7XpE7LIFGPWfy2tF1IMEyKfxo9V9imdS7M7zXZfMTQUktpaR2U 9dBsuSmXpWOtN9TsgnpcLLlbKm78B2wIYalHiyf11lyvqP1kSEMcqL7R4ArxIHv4yRq6 gdUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@ob-encoder-com.20150623.gappssmtp.com header.s=20150623 header.b=2LFxmVMh; 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 g132si390864wmf.106.2017.07.31.04.50.48; Mon, 31 Jul 2017 04:50:48 -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=@ob-encoder-com.20150623.gappssmtp.com header.s=20150623 header.b=2LFxmVMh; 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 C498A689DA1; Mon, 31 Jul 2017 14:50:27 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f67.google.com (mail-wm0-f67.google.com [74.125.82.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 40CF66898EB for ; Mon, 31 Jul 2017 14:50:20 +0300 (EEST) Received: by mail-wm0-f67.google.com with SMTP id q189so24185911wmd.0 for ; Mon, 31 Jul 2017 04:50:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ob-encoder-com.20150623.gappssmtp.com; s=20150623; h=sender:from:to:subject:date:message-id:in-reply-to:references; bh=9it4lxLFEfl4vMh+pLswUSDjblJeCNeN4BQKPqgQ3YE=; b=2LFxmVMhet8USrAQJwCc/BimbDltODxZVk/BJ1JVfqWv2311f1mjcmTe5QgY/jhDyw 1/Keh6E7BKEsDl8+BEvzQc2Gg//vmrtVmoVVRdScHmOTpmzCfiSWXS5PM8vFWRWDex6a 9UcFswJPhI9S+4WL3ZwcZgYuc2yMykBlEyWqJ7IPXevtd8yr5G62el6NH+l7l/Mo+dC3 he8W11wiANCyFr8gmxSnni+2HwNRfTLOizOmGy4K7Kd2mCdhPGNgi7K0gVUZT724ha3G XFJBbknpSKrLjdrnkAKiLdUC4ztXgv8K7Iy5kcC1ruKRfcMx0C1fBbhMiiHzTqWUA3x5 HflQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:subject:date:message-id :in-reply-to:references; bh=9it4lxLFEfl4vMh+pLswUSDjblJeCNeN4BQKPqgQ3YE=; b=poqMzQ8GqSAqNPS+fkaL8/EPsZXIywMH6lvi+PGjJ9LJYw24bBzdJwiQd0wywRH2lJ XPAhL6hFvDLjyGLKSs1FQWcF3NDGHxLyJ5eNshHXxS6a7Hjx3/vjUXzKqLe8O/OI0mKQ gihnN+so95lyglr4A+LpYdOFC4fqXFlid3c0Knw9itDmEebRC7Zdd95ULFPx8dC8kD9y qHMkcDSkFshhtDTwtu5FCShD/GciNvpqrMh52kXip314w7qdjdVWwE/exTeK+P33ioE7 B6yiMZpuo14wIhswmYIdV6dSHDjTSqcStI+8czgYTZho3OxInJDyy5/DRefMMq4DpnIC O1Xg== X-Gm-Message-State: AIVw1105/whhluL42Sa3KQVUQWlhX/c885pCmTXZ36H5s4Wa8X5PHq1i E1Jd6ZHCEi4MtdzZ61M= X-Received: by 10.80.214.135 with SMTP id r7mr10561548edi.259.1501501822168; Mon, 31 Jul 2017 04:50:22 -0700 (PDT) Received: from Highwind.systemlords.lan (d51A44418.access.telenet.be. [81.164.68.24]) by smtp.gmail.com with ESMTPSA id m53sm14830400edc.29.2017.07.31.04.50.21 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 31 Jul 2017 04:50:21 -0700 (PDT) From: James Darnley To: FFmpeg development discussions and patches Date: Mon, 31 Jul 2017 13:50:16 +0200 Message-Id: <20170731115016.6854-3-jdarnley@obe.tv> X-Mailer: git-send-email 2.13.3 In-Reply-To: <20170731115016.6854-1-jdarnley@obe.tv> References: <20170731115016.6854-1-jdarnley@obe.tv> Subject: [FFmpeg-devel] [PATCH 3/3] avcodec/vc2enc: constant quantiser encoding 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" --- libavcodec/vc2enc.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 90 insertions(+), 2 deletions(-) diff --git a/libavcodec/vc2enc.c b/libavcodec/vc2enc.c index 5dec88c81b..ca8c8075fd 100644 --- a/libavcodec/vc2enc.c +++ b/libavcodec/vc2enc.c @@ -182,6 +182,8 @@ typedef struct VC2EncContext { /* Parse code state */ uint32_t next_parse_offset; enum DiracParseCodes last_parse_code; + + int const_quant; } VC2EncContext; static av_always_inline void put_vc2_ue_uint(PutBitContext *pb, uint32_t val) @@ -933,6 +935,71 @@ static int dwt_plane(AVCodecContext *avctx, void *arg) return 0; } +static int constant_quantiser_slice_sizes(VC2EncContext *s, int quant_idx) +{ + int bytes = 0; + + for (int i = 0; i < s->num_x * s->num_y; i++) { + SliceArgs *slice = &s->slice_args[i]; + int x, y; + uint8_t quants[MAX_DWT_LEVELS][4]; + int bits = 0, p, level, orientation; + + bits += 8*s->prefix_bytes; + bits += 8; /* quant_idx */ + + for (level = 0; level < s->wavelet_depth; level++) + for (orientation = !!level; orientation < 4; orientation++) + quants[level][orientation] = FFMAX(quant_idx - s->quant[level][orientation], 0); + + for (p = 0; p < 3; p++) { + int bytes_start, bytes_len, pad_s, pad_c; + bytes_start = bits >> 3; + bits += 8; + for (level = 0; level < s->wavelet_depth; level++) { + for (orientation = !!level; orientation < 4; orientation++) { + SubBand *b = &s->plane[p].band[level][orientation]; + + const int q_idx = quants[level][orientation]; + const uint8_t *len_lut = &s->coef_lut_len[q_idx*COEF_LUT_TAB]; + const int qfactor = ff_dirac_qscale_tab[q_idx]; + + const int left = b->width * slice->x / s->num_x; + const int right = b->width *(slice->x+1) / s->num_x; + const int top = b->height * slice->y / s->num_y; + const int bottom = b->height *(slice->y+1) / s->num_y; + + dwtcoef *buf = b->buf + top * b->stride; + + for (y = top; y < bottom; y++) { + for (x = left; x < right; x++) { + uint32_t c_abs = FFABS(buf[x]); + if (c_abs < COEF_LUT_TAB) { + bits += len_lut[c_abs]; + } else { + c_abs = QUANT(c_abs, qfactor); + bits += count_vc2_ue_uint(c_abs); + bits += !!c_abs; + } + } + buf += b->stride; + } + } + } + bits += FFALIGN(bits, 8) - bits; + bytes_len = (bits >> 3) - bytes_start - 1; + pad_s = FFALIGN(bytes_len, s->size_scaler)/s->size_scaler; + pad_c = (pad_s*s->size_scaler) - bytes_len; + bits += pad_c*8; + } + + slice->bytes = SSIZE_ROUND(bits >> 3); + bytes += SSIZE_ROUND(bits >> 3); + } + + return bytes; +} + static int encode_frame(VC2EncContext *s, AVPacket *avpkt, const AVFrame *frame, const char *aux_data, const int header_size, int field) { @@ -951,7 +1018,13 @@ static int encode_frame(VC2EncContext *s, AVPacket *avpkt, const AVFrame *frame, sizeof(TransformArgs)); /* Calculate per-slice quantizers and sizes */ - max_frame_bytes = header_size + calc_slice_sizes(s); + max_frame_bytes = header_size; + if(s->avctx->flags & AV_CODEC_FLAG_QSCALE) { + max_frame_bytes += constant_quantiser_slice_sizes(s, s->const_quant); + } + else { + max_frame_bytes += calc_slice_sizes(s); + } if (field < 2) { ret = ff_alloc_packet2(s->avctx, avpkt, @@ -1060,7 +1133,7 @@ static av_cold int vc2_encode_init(AVCodecContext *avctx) { Plane *p; SubBand *b; - int i, j, level, o, shift; + int i, j, level, o, shift, const_quant = 0; const AVPixFmtDescriptor *fmt = av_pix_fmt_desc_get(avctx->pix_fmt); const int depth = fmt->comp[0].depth; VC2EncContext *s = avctx->priv_data; @@ -1108,6 +1181,20 @@ static av_cold int vc2_encode_init(AVCodecContext *avctx) if (s->interlaced) av_log(avctx, AV_LOG_WARNING, "Interlacing enabled!\n"); + if (avctx->flags & AV_CODEC_FLAG_QSCALE) { + const int quant_max = FF_ARRAY_ELEMS(ff_dirac_qscale_tab); + const_quant = avctx->global_quality / FF_QP2LAMBDA; + + if (const_quant < 0 || const_quant >= quant_max) { + av_log(avctx, AV_LOG_ERROR, "constant quantiser (%d) outside valid range [%d..%d]\n", + const_quant, 0, quant_max); + return AVERROR(EINVAL); + } + av_log(avctx, AV_LOG_WARNING, "encoding with constant quantiser (%d)\n", + const_quant); + s->const_quant = const_quant; + } + if ((s->slice_width & (s->slice_width - 1)) || (s->slice_height & (s->slice_height - 1))) { av_log(avctx, AV_LOG_ERROR, "Slice size is not a power of two!\n"); @@ -1206,6 +1293,7 @@ static av_cold int vc2_encode_init(AVCodecContext *avctx) args->ctx = s; args->x = x; args->y = y; + args->quant_idx = const_quant; } } }