From patchwork Tue Aug 18 19:40:34 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gautam Ramakrishnan X-Patchwork-Id: 21712 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 8FC6844AE61 for ; Tue, 18 Aug 2020 22:40:55 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6C32D6880BB; Tue, 18 Aug 2020 22:40:55 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 29F0C6800E4 for ; Tue, 18 Aug 2020 22:40:49 +0300 (EEST) Received: by mail-pf1-f177.google.com with SMTP id m71so10482179pfd.1 for ; Tue, 18 Aug 2020 12:40:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=7ZEN6M3fWe1v/I4OPrhDDQHBRN20V7FlDe/um2xOvqY=; b=W12F1xerkpiFWXdJdAUuFhrlUc7VK7/8aeu1MjfGqqIy3VZSXv7Il1/1H2hBhjsQ/f 4vuQaKtb+PasF8MWFWUN3R0RaK7g/2Ir8lZTgmpjBWEcCWDOto8fl0UmIg0NcKfL9WX+ TmixCMANBDKq0m9T9nu0o0GVQ3Ue6rzGIY3hNDY37bAd41rR7Sq3djF/8i5vadZaVL0O 34rZBw61SG4445zRJR6pkQd06pfuPt9B0nI58x5iJn8Q8ith/LosVtukieiHonHpqQRf 9xm2sSe2QPaGAW8b7nJz8qT5rSmxIlOZf1mP3E1OtmCw1/vzaqm82CXLaF5o102zsjsl xj0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=7ZEN6M3fWe1v/I4OPrhDDQHBRN20V7FlDe/um2xOvqY=; b=eqKRw3hT3d3H3oGaCKIoEJnWRb2L4oPTJVlvt6oWhy5ckstQe/OxgkY1QODVzANKsR aft8ujduZudbNeovasLvhcwRWE17S5astxFkT7mlrQShCHNYoCUpPiNoBjHcu7pxgY/o y+kP+uFWlVowITD6ic6rbo4kE2Uj1Wu+FKJZhqeaNoyv076CtlHVmCmIQi81frorJMho R9j4i5ZvBsw+kdoFFfM5lK07Usv7vvPasURC5pBkGB9NiquspVf5oUbJQnZJYubLA1p+ ZEU/QpDw4HfM8awaDnnf8yTRDs5bqjaamAq2nXCCiAbexn6u20Rlf7LprAKtJawvX7Wf RYTw== X-Gm-Message-State: AOAM530xQG7N1KlJrSu1bAEYU6EFm7h+t6XG3G9aYp9XPL3ebC684yRz jRmPWpgt5dB+6pyjZwW2Xp/8aNVLOvY= X-Google-Smtp-Source: ABdhPJxVTle4HY3C4BQ9PoKDB89QWqTX4tLQf7DC8khCKmcBifE6EVtV2UzJeQQAuL6TjIDpBEDqqg== X-Received: by 2002:a62:6887:: with SMTP id d129mr16718231pfc.279.1597779647008; Tue, 18 Aug 2020 12:40:47 -0700 (PDT) Received: from localhost.localdomain ([122.166.129.193]) by smtp.gmail.com with ESMTPSA id z77sm27140885pfc.199.2020.08.18.12.40.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Aug 2020 12:40:46 -0700 (PDT) From: gautamramk@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Wed, 19 Aug 2020 01:10:34 +0530 Message-Id: <20200818194037.25422-1-gautamramk@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH 1/4] libavcodec/jpeg2000: Make tag tree functions non static 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 Cc: Gautam Ramakrishnan MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Gautam Ramakrishnan This patch makes the tag_tree_zero() and tag_tree_size() functions non static and callable from other files. --- libavcodec/jpeg2000.c | 4 ++-- libavcodec/jpeg2000.h | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c index 1aca31ffa4..35e21f54a4 100644 --- a/libavcodec/jpeg2000.c +++ b/libavcodec/jpeg2000.c @@ -39,7 +39,7 @@ /* tag tree routines */ /* allocate the memory for tag tree */ -static int32_t tag_tree_size(int w, int h) +int32_t tag_tree_size(int w, int h) { int64_t res = 0; while (w > 1 || h > 1) { @@ -82,7 +82,7 @@ static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, int h) return res; } -static void tag_tree_zero(Jpeg2000TgtNode *t, int w, int h) +void tag_tree_zero(Jpeg2000TgtNode *t, int w, int h) { int i, siz = tag_tree_size(w, h); diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h index 5b0627c3dc..4ea7daa5da 100644 --- a/libavcodec/jpeg2000.h +++ b/libavcodec/jpeg2000.h @@ -290,4 +290,7 @@ static inline int needs_termination(int style, int passno) { return 0; } +int32_t tag_tree_size(int w, int h); +void tag_tree_zero(Jpeg2000TgtNode *t, int w, int h); + #endif /* AVCODEC_JPEG2000_H */ From patchwork Tue Aug 18 19:40:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gautam Ramakrishnan X-Patchwork-Id: 21713 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 6400B44AE61 for ; Tue, 18 Aug 2020 22:40:57 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 51F90688084; Tue, 18 Aug 2020 22:40:57 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E859B6880DE for ; Tue, 18 Aug 2020 22:40:50 +0300 (EEST) Received: by mail-pf1-f177.google.com with SMTP id d188so10472869pfd.2 for ; Tue, 18 Aug 2020 12:40:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=0C9PCjKTQOo0kIRYPr5Pe4W+v/IPKX5vYgb3lyso38Y=; b=BBw9jt/qfIP6cEZlRI85lWseBuzqIW+ZEPACTDdqTgAuiUTsfn8AbGYgSH1zpKGvex hQd3N4Lz737e6zBnWKS38WXjzJDx4pJckmfdHd2sDexzYcoYRrlFHxqwdJVMlGRgJhS0 THIsmVRpHqkEf55qsC1AhwRLDpzku9dWkbmo28iSpKW21KOi4etj89szTgk4ZT8154Pp 2Y1yf8XV6TDvas9ALEY6xTamQQ0MXiSWNP0yYlnzzhJDrqNHgruqZGO62qxaaFX9RNYf YrreaxGgCXOb4yYRaGJQCVmFO9i3LTDyr/Q0hxpQrC5x7CPpYtOZS1cJ2rvbVfgC6+9A 8PCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=0C9PCjKTQOo0kIRYPr5Pe4W+v/IPKX5vYgb3lyso38Y=; b=eNHYLmAzTwp06cJ4YEOTnl6xFwAVvJJ1UhdJgzH08XKpDM58OqC4pK2DJNgj/IrJZZ VBxfeXbS2+W6z2kcFwqGqNjtjV8BYf7FVuWHOZZCo3zi5ZAmg5MJ+XLpsuo4HXySiEbk 5PEUAwBQpcFkAG+mLak1Q+hIiMI6HM06ya7LKv8tyF8gLI/noAvQiw7phZmpq0BHHzFH pC7UvOJOBMs+rRH32JijeSv6HxWxvJHQ9O4KN4gduO1nECzk1FiyM7yBr8al954a7o+f QbyVaPzr8wF0ClxojX5YozdwJoLTnEWD1BKlpfSoHZQR9KQB2aO7lpUx0g8h8IGkhF40 5XDA== X-Gm-Message-State: AOAM532XdiNPUA+j+JbuQt9EjTkivPfm/bQ1RQiMOgb07adnxh1YZsFf yKqk1VUCqqEJQXW6AsDiop6vcQsiQ3Q= X-Google-Smtp-Source: ABdhPJwbDdrmL4cFjrGHypjHUJ3zExHu3sYLo1p37zC1dV6JeXNqNWStJyrHpgo2UH8XnY+EW3HMsw== X-Received: by 2002:a63:571c:: with SMTP id l28mr14906228pgb.168.1597779649061; Tue, 18 Aug 2020 12:40:49 -0700 (PDT) Received: from localhost.localdomain ([122.166.129.193]) by smtp.gmail.com with ESMTPSA id z77sm27140885pfc.199.2020.08.18.12.40.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Aug 2020 12:40:48 -0700 (PDT) From: gautamramk@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Wed, 19 Aug 2020 01:10:35 +0530 Message-Id: <20200818194037.25422-2-gautamramk@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200818194037.25422-1-gautamramk@gmail.com> References: <20200818194037.25422-1-gautamramk@gmail.com> Subject: [FFmpeg-devel] [PATCH 2/4] libavcodec/j2kenc: Fix tag tree coding 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 Cc: Gautam Ramakrishnan MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Gautam Ramakrishnan This patch fixes tag tree coding for JPEG2000 encoder. --- libavcodec/j2kenc.c | 43 +++++++++++++++++++++++++------------------ libavcodec/jpeg2000.c | 1 + libavcodec/jpeg2000.h | 1 + 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c index 16863f8e8c..1c31e48d61 100644 --- a/libavcodec/j2kenc.c +++ b/libavcodec/j2kenc.c @@ -239,30 +239,37 @@ static void j2k_flush(Jpeg2000EncoderContext *s) /* tag tree routines */ /** code the value stored in node */ -static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold) +static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold, int log) { Jpeg2000TgtNode *stack[30]; - int sp = 1, curval = 0; - stack[0] = node; + int sp = -1, curval = 0; - node = node->parent; - while(node){ - if (node->vis){ - curval = node->val; - break; - } - node->vis++; - stack[sp++] = node; + while(node->parent){ + stack[++sp] = node; node = node->parent; } - while(--sp >= 0){ - if (stack[sp]->val >= threshold){ - put_bits(s, 0, threshold - curval); - break; + + while (1) { + if (curval > node->temp_val) + node->temp_val = curval; + else { + curval = node->temp_val; } - put_bits(s, 0, stack[sp]->val - curval); - put_bits(s, 1, 1); - curval = stack[sp]->val; + while (curval < threshold) { + if (curval >= node->val) { + if (!node->vis) { + node->vis = 1; + put_bits(s, 1, 1); + } + break; + } + put_bits(s, 0, 1); + curval++; + } + node->temp_val = curval; + if (sp < 0) + break; + node = stack[sp--]; } } diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c index 35e21f54a4..7a34a36121 100644 --- a/libavcodec/jpeg2000.c +++ b/libavcodec/jpeg2000.c @@ -88,6 +88,7 @@ void tag_tree_zero(Jpeg2000TgtNode *t, int w, int h) for (i = 0; i < siz; i++) { t[i].val = 0; + t[i].temp_val = 0; t[i].vis = 0; } } diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h index 4ea7daa5da..e9f4a51261 100644 --- a/libavcodec/jpeg2000.h +++ b/libavcodec/jpeg2000.h @@ -127,6 +127,7 @@ typedef struct Jpeg2000T1Context { typedef struct Jpeg2000TgtNode { uint8_t val; + uint8_t temp_val; uint8_t vis; struct Jpeg2000TgtNode *parent; } Jpeg2000TgtNode; From patchwork Tue Aug 18 19:40:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gautam Ramakrishnan X-Patchwork-Id: 21723 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 3B63544B082 for ; Wed, 19 Aug 2020 00:25:29 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1A52D6880CA; Wed, 19 Aug 2020 00:25:29 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-vs1-f68.google.com (mail-vs1-f68.google.com [209.85.217.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7811068096F for ; Wed, 19 Aug 2020 00:25:23 +0300 (EEST) Received: by mail-vs1-f68.google.com with SMTP id k25so10886088vsm.11 for ; Tue, 18 Aug 2020 14:25:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=oTWlO+1Whot8oAKPq49qkCCP27LN3zzb0RrwMt/D86Q=; b=YhQfSzmk8E/iKO0w4jFSd4fWGofWoVQ2fW6oag1edJrEehmS+jCfdCtCiCGAMNh+9n frsghpErNQpHBNSf/9/DhfauvgE/7zKKXVbjRCI8p0HvcAfnvmTrYUGYl5IO9tMoOpKH YZ42vCjfp28x1dfQkwal0VR0tQGmAY3+0ED5eD1DXumJ5yzq4EQsfP/nk3EXgOhjeneN 3rGB4qGe7ZopaW0yiuwdwl+VtAftC+3wmtm/NxBFomVa3nN6xgIzfDykOmmLYRwHIx4D LYX7U1ZungQGiyCFnQfP6i5W+G3nqxirHwEv8zO7CHkaSdTToNcboJgSBYgcyPhg7jEf +4Jw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=oTWlO+1Whot8oAKPq49qkCCP27LN3zzb0RrwMt/D86Q=; b=i5NaU5qgFQ+lTyPUpeggysdc42e662WPPGqXPJqD6yJNX9qTNzYgC2VebgN0+eunJ3 0XxP5EgG6/OJphXtyqkogoxvxIUIPejBOez9sbgDLcBiv8fI6u0zPKGbWwyevQ/SIffm vU85LqTRRhXNK06tV2JmBfnY86zVxxZeK3joJHowBsJdH1Kz5NDHZWM10z7fhoPLJJi7 8/zFUiQVk1wHAYEyMzmHROHK4x8ln+MZ+Dfvi8b9qggFKpGxy9NZ6RKIt6ZhUBslcR0z 1kW567MU7EtlJiNV59xfpH64t2hDTfCUz95EMMvSoKbHcjADOZtInDafWD4dt33wZ/zA mcZQ== X-Gm-Message-State: AOAM530POguR+j6CwXdaraM+q3Wp0OtBsXJrsD1hYI//Ic5UqDyTGv9Z zWWf/jieX9H9rPaqqmmgn5/4AVdQQ64= X-Google-Smtp-Source: ABdhPJySkN97ebUVM5bajpxbPfAeLt6mLHyLo0AF93WFDkiDUF83rm5GTDBReo6mc6EVGP5SUiP7IQ== X-Received: by 2002:aa7:9a1c:: with SMTP id w28mr13019150pfj.116.1597779651404; Tue, 18 Aug 2020 12:40:51 -0700 (PDT) Received: from localhost.localdomain ([122.166.129.193]) by smtp.gmail.com with ESMTPSA id z77sm27140885pfc.199.2020.08.18.12.40.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Aug 2020 12:40:50 -0700 (PDT) From: gautamramk@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Wed, 19 Aug 2020 01:10:36 +0530 Message-Id: <20200818194037.25422-3-gautamramk@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200818194037.25422-1-gautamramk@gmail.com> References: <20200818194037.25422-1-gautamramk@gmail.com> Subject: [FFmpeg-devel] [PATCH 3/4] libavcodec/jpeg2000: Modify cleanup 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 Cc: Gautam Ramakrishnan MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Gautam Ramakrishnan This patch makes the ff_jpeg2000_cleanup function take in an extra parameter which indicates whether it is called from the encoder or decoder. --- libavcodec/j2kenc.c | 2 +- libavcodec/jpeg2000.c | 2 +- libavcodec/jpeg2000.h | 2 +- libavcodec/jpeg2000dec.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c index 1c31e48d61..264bae3f73 100644 --- a/libavcodec/j2kenc.c +++ b/libavcodec/j2kenc.c @@ -1218,7 +1218,7 @@ static void cleanup(Jpeg2000EncoderContext *s) for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){ for (compno = 0; compno < s->ncomponents; compno++){ Jpeg2000Component *comp = s->tile[tileno].comp + compno; - ff_jpeg2000_cleanup(comp, codsty); + ff_jpeg2000_cleanup(comp, codsty, 1); } av_freep(&s->tile[tileno].comp); } diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c index 7a34a36121..88e6ebc8b3 100644 --- a/libavcodec/jpeg2000.c +++ b/libavcodec/jpeg2000.c @@ -580,7 +580,7 @@ void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty) } } -void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty) +void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, int isencoder) { int reslevelno, bandno, precno; for (reslevelno = 0; diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h index e9f4a51261..611aea1665 100644 --- a/libavcodec/jpeg2000.h +++ b/libavcodec/jpeg2000.h @@ -272,7 +272,7 @@ int ff_jpeg2000_init_component(Jpeg2000Component *comp, void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty); -void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty); +void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, int isencoder); static inline int needs_termination(int style, int passno) { if (style & JPEG2000_CBLK_BYPASS) { diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c index 624542c2f8..c5192d007f 100644 --- a/libavcodec/jpeg2000dec.c +++ b/libavcodec/jpeg2000dec.c @@ -2123,7 +2123,7 @@ static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s) Jpeg2000Component *comp = s->tile[tileno].comp + compno; Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno; - ff_jpeg2000_cleanup(comp, codsty); + ff_jpeg2000_cleanup(comp, codsty, 0); } av_freep(&s->tile[tileno].comp); av_freep(&s->tile[tileno].packed_headers); From patchwork Tue Aug 18 19:40:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gautam Ramakrishnan X-Patchwork-Id: 21715 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 3E67644BD10 for ; Tue, 18 Aug 2020 23:07:19 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1E46D6880DE; Tue, 18 Aug 2020 23:07:19 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-oi1-f196.google.com (mail-oi1-f196.google.com [209.85.167.196]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B26D968080C for ; Tue, 18 Aug 2020 23:07:11 +0300 (EEST) Received: by mail-oi1-f196.google.com with SMTP id k4so19043032oik.2 for ; Tue, 18 Aug 2020 13:07:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=lM4xY2gszfFIP5DIlX20v5r21wUTgmEP725JL4lruBI=; b=LQBGVa+E2AAV8yWlt6dncHISpv7zEWFY0gMvTziBowpdfgWH2jtOujilB2T439wQwn fbHbaeomISOcgObKzYhjFzYT4OUJgPgXDTmbEK3ypVXocYCl1T/aGcCajU58yXJpJMDP 3sWU4ge6BysfFDV8mkOKyIwWkOOYn5leDK2PkBQtGp317dkHim95X9ZX2MbMsaY5Y5Nb Ay47P2c+9WwfMX3Xv6HyvJA7Bck8Yt8nnXPPiF1Xp6tNIlQDPMEUcr0eJIyv4vx6dZqg gzV1MvJ70YKTn+ATox8XvYisnzr1fhH2gKNyQAKbH453pFCJluY56cRaNYbrTsbxMujP QRBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=lM4xY2gszfFIP5DIlX20v5r21wUTgmEP725JL4lruBI=; b=deWkB3T3MzBi5dDE1FyN8c6c79dRdD2wug/oNO6AwjC8aDeDCbasa24qEjK6Ukdd9X f3S4rwDa1nZBivB9DgrmXkiNE/+RVVS4E9FVJXaq+mCoNUfSwp9HkOauhjwKpN2dco8+ +BYkkBFl61KRFcdBkG87IjsJYkQxIIxvD0FQQb14yGINfY8oF8fFA/8/bxErF0PbVhMx UGiYtyV37Zo/J6t9Tj3xlcE4sPqyJmb78ITFAcbX9LKG0VKbPNTUxe7GvA3JRivCqspN N/KoIjF7lkH/Kw3sfqwLyuMUOPvo2D/woSzVnvUDytnQDuYwF9kg0VLf9CYqHXaxS9NC K1Ng== X-Gm-Message-State: AOAM531wsCZYN4WiYwUIYNTftWJcNS20JAnlR5kyUldKJb6t/B6Zzl88 efilBhxVFh9U1MMdhKnsn2mV1p3Y+Gk= X-Google-Smtp-Source: ABdhPJxiKlAHgSxCqeRGMCsLOnEok7KTF0lulbe/s+qosFCkhkcrYUXBxrwohrHybLcjphrpm5GJGA== X-Received: by 2002:a17:90a:f2c7:: with SMTP id gt7mr1154874pjb.204.1597779653571; Tue, 18 Aug 2020 12:40:53 -0700 (PDT) Received: from localhost.localdomain ([122.166.129.193]) by smtp.gmail.com with ESMTPSA id z77sm27140885pfc.199.2020.08.18.12.40.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Aug 2020 12:40:53 -0700 (PDT) From: gautamramk@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Wed, 19 Aug 2020 01:10:37 +0530 Message-Id: <20200818194037.25422-4-gautamramk@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200818194037.25422-1-gautamramk@gmail.com> References: <20200818194037.25422-1-gautamramk@gmail.com> Subject: [FFmpeg-devel] [PATCH 4/4] libavcodec/j2kenc: Support for multiple layers 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 Cc: Gautam Ramakrishnan MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Gautam Ramakrishnan This patch allows setting a compression ratio and to set multiple layers. The user has to input a compression ratio for each layer. The per layer compression ration can be set as follows: -layer_rates "r1,r2,...rn" for to create 'n' layers. --- libavcodec/j2kenc.c | 441 ++++++++++++++++++++++++++++++++++-------- libavcodec/jpeg2000.c | 14 +- libavcodec/jpeg2000.h | 10 + 3 files changed, 382 insertions(+), 83 deletions(-) diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c index 264bae3f73..3882fa8474 100644 --- a/libavcodec/j2kenc.c +++ b/libavcodec/j2kenc.c @@ -100,6 +100,7 @@ static const int dwt_norms[2][4][10] = { // [dwt_type][band][rlevel] (multiplied typedef struct { Jpeg2000Component *comp; + double *layer_rates; } Jpeg2000Tile; typedef struct { @@ -126,12 +127,15 @@ typedef struct { Jpeg2000QuantStyle qntsty; Jpeg2000Tile *tile; + int layer_rates[100]; int format; int pred; int sop; int eph; int prog; + int nlayers; + char *lr_str; } Jpeg2000EncoderContext; @@ -239,7 +243,7 @@ static void j2k_flush(Jpeg2000EncoderContext *s) /* tag tree routines */ /** code the value stored in node */ -static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold, int log) +static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold) { Jpeg2000TgtNode *stack[30]; int sp = -1, curval = 0; @@ -332,7 +336,7 @@ static int put_cod(Jpeg2000EncoderContext *s) bytestream_put_byte(&s->buf, scod); // Scod // SGcod bytestream_put_byte(&s->buf, s->prog); // progression level - bytestream_put_be16(&s->buf, 1); // num of layers + bytestream_put_be16(&s->buf, s->nlayers); // num of layers if(s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){ bytestream_put_byte(&s->buf, 0); // unspecified }else{ @@ -411,6 +415,28 @@ static uint8_t *put_sot(Jpeg2000EncoderContext *s, int tileno) return psotptr; } +static void compute_rates(Jpeg2000EncoderContext* s) +{ + int i, j; + int layno; + for (i = 0; i < s->numYtiles; i++) { + for (j = 0; j < s->numXtiles; j++) { + Jpeg2000Tile *tile = &s->tile[s->numXtiles * i + j]; + int tilew = tile->comp->coord[0][1] - tile->comp->coord[0][0]; + int tileh = tile->comp->coord[1][1] - tile->comp->coord[1][0]; + for (layno = 0; layno < s->nlayers; layno++) { + if (s->layer_rates[layno] > 0.0f) { + tile->layer_rates[layno] = (double)(tilew * tileh) * s->ncomponents * s->cbps[0] / (s->layer_rates[layno] * 8); + //av_log(s, AV_LOG_WARNING, "Layer %d rate is %lf\n", layno, tile->layer_rates[layno]); + } else { + tile->layer_rates[layno] = 0; + } + } + } + } + +} + /** * compute the sizes of tiles, resolution levels, bands, etc. * allocate memory for them @@ -448,6 +474,10 @@ static int init_tiles(Jpeg2000EncoderContext *s) for (j = 0; j < 2; j++) comp->coord[i][j] = comp->coord_o[i][j] = ff_jpeg2000_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]); + tile->layer_rates = av_mallocz_array(s->ncomponents, sizeof(*tile->layer_rates)); + if (!tile->layer_rates) + return AVERROR(ENOMEM); + if ((ret = ff_jpeg2000_init_component(comp, codsty, qntsty, @@ -459,6 +489,7 @@ static int init_tiles(Jpeg2000EncoderContext *s) return ret; } } + compute_rates(s); return 0; } @@ -701,6 +732,8 @@ static void encode_cblk(Jpeg2000EncoderContext *s, Jpeg2000T1Context *t1, Jpeg20 } cblk->passes[passno].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno].flushed, &cblk->passes[passno].flushed_len); + cblk->passes[passno].rate -= cblk->passes[passno].flushed_len; + wmsedec += (int64_t)nmsedec << (2*bpno); cblk->passes[passno].disto = wmsedec; @@ -733,10 +766,12 @@ static void putnumpasses(Jpeg2000EncoderContext *s, int n) } -static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int precno, - uint8_t *expn, int numgbits, int packetno) +static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int layno, + int precno, uint8_t *expn, int numgbits, int packetno, + int nlayers) { int bandno, empty = 1; + int i; // init bitstream *s->buf = 0; s->bit_index = 0; @@ -748,18 +783,61 @@ static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, in } // header + if (!layno) { + for (bandno = 0; bandno < rlevel->nbands; bandno++){ + Jpeg2000Band *band = rlevel->band + bandno; + if (rlevel->band[bandno].coord[0][0] < rlevel->band[bandno].coord[0][1] + && rlevel->band[bandno].coord[1][0] < rlevel->band[bandno].coord[1][1]){ + Jpeg2000Prec *prec = band->prec + precno; + int nb_cblks = prec->nb_codeblocks_height * prec->nb_codeblocks_width; + int pos; + tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, prec->nb_codeblocks_height); + tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, prec->nb_codeblocks_height); + for (pos = 0; pos < nb_cblks; pos++) { + Jpeg2000Cblk *cblk = &prec->cblk[pos]; + prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - cblk->nonzerobits; + cblk->incl = 0; + cblk->lblock = 3; + tag_tree_update(prec->zerobits + pos); + for (i = 0; i < nlayers; i++) { + if (cblk->layers[i].npasses > 0) { + prec->cblkincl[pos].val = i; + break; + } + } + if (i == nlayers) + prec->cblkincl[pos].val = i; + tag_tree_update(prec->cblkincl + pos); + } + } + } + } + // is the packet empty? for (bandno = 0; bandno < rlevel->nbands; bandno++){ + Jpeg2000Band *band = rlevel->band + bandno; if (rlevel->band[bandno].coord[0][0] < rlevel->band[bandno].coord[0][1] && rlevel->band[bandno].coord[1][0] < rlevel->band[bandno].coord[1][1]){ - empty = 0; - break; + Jpeg2000Prec *prec = band->prec + precno; + int nb_cblks = prec->nb_codeblocks_height * prec->nb_codeblocks_width; + int pos; + for (pos = 0; pos < nb_cblks; pos++) { + Jpeg2000Cblk *cblk = &prec->cblk[pos]; + if (cblk->layers[layno].npasses) { + empty = 0; + break; + } + } + if (!empty) + break; } } put_bits(s, !empty, 1); if (empty){ j2k_flush(s); + if (s->eph) + bytestream_put_be16(&s->buf, JPEG2000_EPH); return 0; } @@ -775,40 +853,44 @@ static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, in for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++){ for (xi = 0; xi < cblknw; xi++, pos++){ - prec->cblkincl[pos].val = prec->cblk[yi * cblknw + xi].ninclpasses == 0; - tag_tree_update(prec->cblkincl + pos); - prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - prec->cblk[yi * cblknw + xi].nonzerobits; - tag_tree_update(prec->zerobits + pos); - } - } - - for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++){ - for (xi = 0; xi < cblknw; xi++, pos++){ - int pad = 0, llen, length; + int llen = 0, length; Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi; if (s->buf_end - s->buf < 20) // approximately return -1; // inclusion information - tag_tree_code(s, prec->cblkincl + pos, 1); - if (!cblk->ninclpasses) + if (!cblk->incl) + tag_tree_code(s, prec->cblkincl + pos, layno + 1); + else { + put_bits(s, cblk->layers[layno].npasses > 0, 1); + } + + if (!cblk->layers[layno].npasses) continue; + // zerobits information - tag_tree_code(s, prec->zerobits + pos, 100); + if (!cblk->incl) { + tag_tree_code(s, prec->zerobits + pos, 100); + cblk->incl = 1; + } + // number of passes - putnumpasses(s, cblk->ninclpasses); + putnumpasses(s, cblk->layers[layno].npasses); - length = cblk->passes[cblk->ninclpasses-1].rate; - llen = av_log2(length) - av_log2(cblk->ninclpasses) - 2; - if (llen < 0){ - pad = -llen; - llen = 0; + length = cblk->layers[layno].data_len; + if (layno == nlayers - 1 && cblk->layers->cum_passes){ + length += cblk->passes[cblk->layers->cum_passes-1].flushed_len; } + if (cblk->lblock + av_log2(cblk->layers[layno].npasses) < av_log2(length) + 1) { + llen = av_log2(length) + 1 - cblk->lblock - av_log2(cblk->layers[layno].npasses); + } + // length of code block + cblk->lblock += llen; put_bits(s, 1, llen); put_bits(s, 0, 1); - put_num(s, length, av_log2(length)+1+pad); + put_num(s, length, cblk->lblock + av_log2(cblk->layers[layno].npasses)); } } } @@ -825,13 +907,14 @@ static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, in int xi; for (xi = 0; xi < cblknw; xi++){ Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi; - if (cblk->ninclpasses){ - if (s->buf_end - s->buf < cblk->passes[cblk->ninclpasses-1].rate) + if (cblk->layers[layno].npasses){ + if (s->buf_end - s->buf < cblk->layers[layno].data_len + 2) return -1; - bytestream_put_buffer(&s->buf, cblk->data + 1, cblk->passes[cblk->ninclpasses-1].rate - - cblk->passes[cblk->ninclpasses-1].flushed_len); - bytestream_put_buffer(&s->buf, cblk->passes[cblk->ninclpasses-1].flushed, - cblk->passes[cblk->ninclpasses-1].flushed_len); + bytestream_put_buffer(&s->buf, cblk->layers[layno].data_start + 1, cblk->layers[layno].data_len); + if (layno == nlayers - 1 && cblk->layers->cum_passes){ + bytestream_put_buffer(&s->buf, cblk->passes[cblk->layers->cum_passes-1].flushed, + cblk->passes[cblk->layers->cum_passes-1].flushed_len); + } } } } @@ -839,9 +922,9 @@ static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, in return 0; } -static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno) +static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno, int nlayers) { - int compno, reslevelno, ret; + int compno, reslevelno, layno, ret; Jpeg2000CodingStyle *codsty = &s->codsty; Jpeg2000QuantStyle *qntsty = &s->qntsty; int packetno = 0; @@ -858,29 +941,33 @@ static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int til av_log(s->avctx, AV_LOG_DEBUG, "tier2\n"); // lay-rlevel-comp-pos progression - switch (s->prog) { + switch (s->prog) { case JPEG2000_PGOD_LRCP: - for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){ - for (compno = 0; compno < s->ncomponents; compno++){ - int precno; - Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno; - for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){ - if ((ret = encode_packet(s, reslevel, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0), - qntsty->nguardbits, packetno++)) < 0) - return ret; + for (layno = 0; layno < nlayers; layno++){ + for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){ + for (compno = 0; compno < s->ncomponents; compno++){ + int precno; + Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno; + for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){ + if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0), + qntsty->nguardbits, packetno++, nlayers)) < 0) + return ret; + } } } } break; case JPEG2000_PGOD_RLCP: for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){ - for (compno = 0; compno < s->ncomponents; compno++){ - int precno; - Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno; - for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){ - if ((ret = encode_packet(s, reslevel, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0), - qntsty->nguardbits, packetno++)) < 0) - return ret; + for (layno = 0; layno < nlayers; layno++){ + for (compno = 0; compno < s->ncomponents; compno++){ + int precno; + Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno; + for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){ + if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0), + qntsty->nguardbits, packetno++, nlayers)) < 0) + return ret; + } } } } @@ -935,10 +1022,11 @@ static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int til prcx, prcy, reslevel->num_precincts_x, reslevel->num_precincts_y); continue; } - - if ((ret = encode_packet(s, reslevel, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0), - qntsty->nguardbits, packetno++)) < 0) - return ret; + for (layno = 0; layno < nlayers; layno++){ + if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0), + qntsty->nguardbits, packetno++, nlayers)) < 0) + return ret; + } } } } @@ -1001,9 +1089,11 @@ static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int til prcx, prcy, reslevel->num_precincts_x, reslevel->num_precincts_y); continue; } - if ((ret = encode_packet(s, reslevel, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0), - qntsty->nguardbits, packetno++)) < 0) - return ret; + for (layno = 0; layno < nlayers; layno++){ + if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0), + qntsty->nguardbits, packetno++, nlayers)) < 0) + return ret; + } } } } @@ -1062,9 +1152,11 @@ static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int til prcx, prcy, reslevel->num_precincts_x, reslevel->num_precincts_y); continue; } - if ((ret = encode_packet(s, reslevel, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0), - qntsty->nguardbits, packetno++)) < 0) - return ret; + for (layno = 0; layno < nlayers; layno++){ + if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0), + qntsty->nguardbits, packetno++, nlayers)) < 0) + return ret; + } } } } @@ -1076,27 +1168,98 @@ static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int til return 0; } -static int getcut(Jpeg2000Cblk *cblk, int64_t lambda, int dwt_norm) +static void makelayer(Jpeg2000EncoderContext *s, int layno, double thresh, Jpeg2000Tile* tile, int final) { - int passno, res = 0; - for (passno = 0; passno < cblk->npasses; passno++){ - int dr; - int64_t dd; - - dr = cblk->passes[passno].rate - - (res ? cblk->passes[res-1].rate:0); - dd = cblk->passes[passno].disto - - (res ? cblk->passes[res-1].disto:0); - - if (((dd * dwt_norm) >> WMSEDEC_SHIFT) * dwt_norm >= dr * lambda) - res = passno+1; + int compno, resno, bandno, precno, cblkno; + int passno; + + for (compno = 0; compno < s->ncomponents; compno++) { + Jpeg2000Component *comp = &tile->comp[compno]; + + for (resno = 0; resno < s->codsty.nreslevels; resno++) { + Jpeg2000ResLevel *reslevel = comp->reslevel + resno; + + for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){ + for (bandno = 0; bandno < reslevel->nbands ; bandno++){ + Jpeg2000Band *band = reslevel->band + bandno; + Jpeg2000Prec *prec = band->prec + precno; + + for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){ + Jpeg2000Cblk *cblk = prec->cblk + cblkno; + Jpeg2000Layer *layer = &cblk->layers[layno]; + int n; + + if (layno == 0) { + cblk->ninclpasses = 0; + } + + n = cblk->ninclpasses; + + if (thresh < 0) { + n = cblk->npasses; + } else { + for (passno = cblk->ninclpasses; passno < cblk->npasses; passno++) { + int32_t dr; + double dd; + Jpeg2000Pass *pass = &cblk->passes[passno]; + + if (n == 0) { + dr = pass->rate; + dd = (double)pass->disto; + } else { + dr = pass->rate - cblk->passes[n - 1].rate; + dd = (double)pass->disto - (double)cblk->passes[n-1].disto; + } + + if (!dr) { + if (dd) { + n = passno + 1; + } + continue; + } + + if (thresh - (dd / dr) < DBL_EPSILON) + n = passno + 1; + } + } + layer->npasses = n - cblk->ninclpasses; + layer->cum_passes = n; + + if (layer->npasses == 0) { + layer->disto = 0; + layer->data_len = 0; + continue; + } + + if (cblk->ninclpasses == 0) { + layer->data_len = cblk->passes[n - 1].rate; + layer->data_start = cblk->data; + layer->disto = cblk->passes[n - 1].disto; + } else { + layer->data_len = cblk->passes[n - 1].rate - cblk->passes[cblk->ninclpasses - 1].rate; + layer->data_start = cblk->data + cblk->passes[cblk->ninclpasses - 1].rate; + layer->disto = cblk->passes[n - 1].disto - + cblk->passes[cblk->ninclpasses - 1].disto; + } + if (final) { + cblk->ninclpasses = n; + } + } + } + } + } } - return res; } -static void truncpasses(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile) +static void makelayers(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile) { - int precno, compno, reslevelno, bandno, cblkno, lev; + int precno, compno, reslevelno, bandno, cblkno, lev, passno, layno; + int i; + double min = DBL_MAX; + double max = 0; + double thresh; + int tile_disto = 0; + Jpeg2000CodingStyle *codsty = &s->codsty; for (compno = 0; compno < s->ncomponents; compno++){ @@ -1107,20 +1270,70 @@ static void truncpasses(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile) for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){ for (bandno = 0; bandno < reslevel->nbands ; bandno++){ - int bandpos = bandno + (reslevelno > 0); Jpeg2000Band *band = reslevel->band + bandno; Jpeg2000Prec *prec = band->prec + precno; for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){ Jpeg2000Cblk *cblk = prec->cblk + cblkno; + for (passno = 0; passno < cblk->npasses; passno++) { + Jpeg2000Pass *pass = &cblk->passes[passno]; + int dr; + double dd, drslope; + + tile_disto += pass->disto; + if (passno == 0) { + dr = (int32_t)pass->rate; + dd = (double)pass->disto; + } else { + dr = (int32_t)(pass->rate) - cblk->passes[passno - 1].rate; + dd = (double)pass->disto - (double)cblk->passes[passno - 1].disto; + } + + if (dr <= 0) + continue; + + drslope = dd / dr; + if (drslope < min) + min = drslope; - cblk->ninclpasses = getcut(cblk, s->lambda, - (int64_t)dwt_norms[codsty->transform == FF_DWT53][bandpos][lev] * (int64_t)band->i_stepsize >> 15); + if (drslope > max) + max = drslope; + } } } } } } + + for (layno = 0; layno < s->nlayers; layno++) { + double lo = min; + double hi = max; + double stable_thresh = 0; + double good_thresh = 0; + if (!s->layer_rates[layno]) { + good_thresh = -1; + } else { + for (i = 0; i < 128; i++) { + uint8_t *stream_pos = s->buf; + int ret; + thresh = (lo + hi) / 2; + makelayer(s, layno, thresh, tile, 0); + ret = encode_packets(s, tile, (int)(tile - s->tile), layno + 1); + memset(stream_pos, 0, s->buf - stream_pos); + if ((s->buf - stream_pos > ceil(tile->layer_rates[layno])) || ret < 0) { + lo = thresh; + s->buf = stream_pos; + continue; + } + hi = thresh; + stable_thresh = thresh; + s->buf = stream_pos; + } + } + if (good_thresh >= 0) + good_thresh = stable_thresh == 0 ? thresh : stable_thresh; + makelayer(s, layno, good_thresh, tile, 1); + } } static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno) @@ -1203,8 +1416,8 @@ static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno } av_log(s->avctx, AV_LOG_DEBUG, "rate control\n"); - truncpasses(s, tile); - if ((ret = encode_packets(s, tile, tileno)) < 0) + makelayers(s, tile); + if ((ret = encode_packets(s, tile, tileno, s->nlayers)) < 0) return ret; av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n"); return 0; @@ -1221,6 +1434,7 @@ static void cleanup(Jpeg2000EncoderContext *s) ff_jpeg2000_cleanup(comp, codsty, 1); } av_freep(&s->tile[tileno].comp); + av_freep(&s->tile[tileno].layer_rates); } av_freep(&s->tile); } @@ -1379,6 +1593,64 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, return 0; } +static int inline check_number(char* st, int* ret) { + int stlen = strlen(st); + int i; + *ret = 0; + if (stlen <= 0) { + return AVERROR_INVALIDDATA; + } + for (i = 0; i < stlen; i++) { + if (st[i] >= '0' && st[i] <= '9') { + *ret = (*ret) * 10 + (st[i] - '0'); + } else { + return AVERROR_INVALIDDATA; + } + } + return 0; +} + +static int parse_layer_rates(Jpeg2000EncoderContext *s) +{ + int i; + char* token; + int rate; + int nlayers = 0; + if (!s->lr_str) { + s->nlayers = 1; + s->layer_rates[0] = 0; + return 0; + } + + token = strtok(s->lr_str, ","); + if (!check_number(token, &rate)) { + s->layer_rates[0] = rate <= 1 ? 0:rate; + nlayers++; + } else { + return AVERROR_INVALIDDATA; + } + + while (1) { + token = strtok(NULL, ","); + if (!token) + break; + if (!check_number(token, &rate)) { + s->layer_rates[nlayers] = rate <= 1 ? 0:rate; + nlayers++; + } else { + return AVERROR_INVALIDDATA; + } + } + + for (i = 1; i < nlayers; i++) { + if (s->layer_rates[i] >= s->layer_rates[i-1]) { + return AVERROR_INVALIDDATA; + } + } + s->nlayers = nlayers; + return 0; +} + static av_cold int j2kenc_init(AVCodecContext *avctx) { int i, ret; @@ -1388,6 +1660,11 @@ static av_cold int j2kenc_init(AVCodecContext *avctx) s->avctx = avctx; av_log(s->avctx, AV_LOG_DEBUG, "init\n"); + if (parse_layer_rates(s)) { + av_log(s, AV_LOG_WARNING, "Layer rates invalid. Shall encode with 1 layer.\n"); + s->nlayers = 1; + s->layer_rates[0] = 0; + } #if FF_API_PRIVATE_OPT FF_DISABLE_DEPRECATION_WARNINGS @@ -1408,6 +1685,7 @@ FF_ENABLE_DEPRECATION_WARNINGS memset(codsty->log2_prec_heights, 15, sizeof(codsty->log2_prec_heights)); codsty->nreslevels2decode= codsty->nreslevels = 7; + codsty->nlayers = s->nlayers; codsty->log2_cblk_width = 4; codsty->log2_cblk_height = 4; codsty->transform = s->pred ? FF_DWT53 : FF_DWT97_INT; @@ -1489,6 +1767,7 @@ static const AVOption options[] = { { "rpcl", NULL, OFFSET(prog), AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_RPCL }, 0, 0, VE, "prog" }, { "pcrl", NULL, OFFSET(prog), AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_PCRL }, 0, 0, VE, "prog" }, { "cprl", NULL, OFFSET(prog), AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_CPRL }, 0, 0, VE, "prog" }, + { "layer_rates", "Layer Rates", OFFSET(lr_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VE }, { NULL } }; diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c index 88e6ebc8b3..7c5261e995 100644 --- a/libavcodec/jpeg2000.c +++ b/libavcodec/jpeg2000.c @@ -261,9 +261,11 @@ static void init_band_stepsize(AVCodecContext *avctx, band->f_stepsize *= 0.5; } -static int init_prec(Jpeg2000Band *band, +static int init_prec(AVCodecContext *avctx, + Jpeg2000Band *band, Jpeg2000ResLevel *reslevel, Jpeg2000Component *comp, + Jpeg2000CodingStyle *codsty, int precno, int bandno, int reslevelno, int log2_band_prec_width, int log2_band_prec_height) @@ -366,6 +368,12 @@ static int init_prec(Jpeg2000Band *band, cblk->lblock = 3; cblk->length = 0; cblk->npasses = 0; + if (av_codec_is_encoder(avctx->codec)) { + cblk->layers = av_mallocz_array(codsty->nlayers, sizeof(*cblk->layers)); + if (!cblk->layers) + return AVERROR(ENOMEM); + } + } return 0; @@ -439,7 +447,7 @@ static int init_band(AVCodecContext *avctx, return AVERROR(ENOMEM); for (precno = 0; precno < nb_precincts; precno++) { - ret = init_prec(band, reslevel, comp, + ret = init_prec(avctx, band, reslevel, comp, codsty, precno, bandno, reslevelno, log2_band_prec_width, log2_band_prec_height); if (ret < 0) @@ -614,6 +622,8 @@ void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, i av_freep(&cblk->passes); av_freep(&cblk->lengthinc); av_freep(&cblk->data_start); + if (isencoder) + av_freep(&cblk->layers); } av_freep(&prec->cblk); } diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h index 611aea1665..b338ba6bc9 100644 --- a/libavcodec/jpeg2000.h +++ b/libavcodec/jpeg2000.h @@ -162,10 +162,19 @@ typedef struct Jpeg2000Pass { int flushed_len; } Jpeg2000Pass; +typedef struct Jpeg2000Layer { + uint8_t *data_start; + int data_len; + int npasses; + double disto; + int cum_passes; +} Jpeg2000Layer; + typedef struct Jpeg2000Cblk { uint8_t npasses; uint8_t ninclpasses; // number coding of passes included in codestream uint8_t nonzerobits; + uint8_t incl; uint16_t length; uint16_t *lengthinc; uint8_t nb_lengthinc; @@ -176,6 +185,7 @@ typedef struct Jpeg2000Cblk { int nb_terminationsinc; int *data_start; Jpeg2000Pass *passes; + Jpeg2000Layer *layers; int coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}} } Jpeg2000Cblk; // code block