2011-05-23 23:13:34 +02:00
|
|
|
/*
|
|
|
|
* JPEG2000 image encoder
|
|
|
|
* Copyright (c) 2007 Kamil Nowosad
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2016-03-31 22:02:14 +02:00
|
|
|
*
|
|
|
|
* **********************************************************************************************************************
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* This source code incorporates work covered by the following copyright and
|
|
|
|
* permission notice:
|
|
|
|
*
|
|
|
|
* Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
|
|
|
|
* Copyright (c) 2002-2007, Professor Benoit Macq
|
|
|
|
* Copyright (c) 2001-2003, David Janssens
|
|
|
|
* Copyright (c) 2002-2003, Yannick Verschueren
|
|
|
|
* Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
|
|
|
|
* Copyright (c) 2005, Herve Drolon, FreeImage Team
|
|
|
|
* Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
2011-05-23 23:13:34 +02:00
|
|
|
*/
|
|
|
|
|
2016-03-31 22:02:14 +02:00
|
|
|
|
2011-05-23 23:13:34 +02:00
|
|
|
/**
|
|
|
|
* JPEG2000 image encoder
|
|
|
|
* @file
|
|
|
|
* @author Kamil Nowosad
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <float.h>
|
|
|
|
#include "avcodec.h"
|
2012-03-22 04:24:55 +01:00
|
|
|
#include "internal.h"
|
2011-05-23 23:13:34 +02:00
|
|
|
#include "bytestream.h"
|
2013-05-30 14:40:58 +02:00
|
|
|
#include "jpeg2000.h"
|
2011-05-23 23:13:34 +02:00
|
|
|
#include "libavutil/common.h"
|
2017-11-06 22:39:16 +01:00
|
|
|
#include "libavutil/pixdesc.h"
|
2015-06-19 05:13:58 +02:00
|
|
|
#include "libavutil/opt.h"
|
2018-05-22 02:21:04 +02:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2011-05-23 23:13:34 +02:00
|
|
|
|
|
|
|
#define NMSEDEC_BITS 7
|
|
|
|
#define NMSEDEC_FRACBITS (NMSEDEC_BITS-1)
|
|
|
|
#define WMSEDEC_SHIFT 13 ///< must be >= 13
|
|
|
|
#define LAMBDA_SCALE (100000000LL << (WMSEDEC_SHIFT - 13))
|
|
|
|
|
2015-06-19 05:13:58 +02:00
|
|
|
#define CODEC_JP2 1
|
|
|
|
#define CODEC_J2K 0
|
|
|
|
|
2011-05-23 23:13:34 +02:00
|
|
|
static int lut_nmsedec_ref [1<<NMSEDEC_BITS],
|
|
|
|
lut_nmsedec_ref0[1<<NMSEDEC_BITS],
|
|
|
|
lut_nmsedec_sig [1<<NMSEDEC_BITS],
|
|
|
|
lut_nmsedec_sig0[1<<NMSEDEC_BITS];
|
|
|
|
|
|
|
|
static const int dwt_norms[2][4][10] = { // [dwt_type][band][rlevel] (multiplied by 10000)
|
|
|
|
{{10000, 19650, 41770, 84030, 169000, 338400, 676900, 1353000, 2706000, 5409000},
|
|
|
|
{20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
|
|
|
|
{20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
|
|
|
|
{20800, 38650, 83070, 171800, 347100, 695900, 1393000, 2786000, 5572000}},
|
|
|
|
|
|
|
|
{{10000, 15000, 27500, 53750, 106800, 213400, 426700, 853300, 1707000, 3413000},
|
|
|
|
{10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
|
|
|
|
{10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
|
|
|
|
{ 7186, 9218, 15860, 30430, 60190, 120100, 240000, 479700, 959300}}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000Component *comp;
|
|
|
|
} Jpeg2000Tile;
|
2011-05-23 23:13:34 +02:00
|
|
|
|
|
|
|
typedef struct {
|
2015-06-19 05:13:58 +02:00
|
|
|
AVClass *class;
|
2011-05-23 23:13:34 +02:00
|
|
|
AVCodecContext *avctx;
|
2013-12-17 16:27:36 +01:00
|
|
|
const AVFrame *picture;
|
2011-05-23 23:13:34 +02:00
|
|
|
|
|
|
|
int width, height; ///< image width and height
|
|
|
|
uint8_t cbps[4]; ///< bits per sample in particular components
|
|
|
|
int chroma_shift[2];
|
|
|
|
uint8_t planar;
|
|
|
|
int ncomponents;
|
|
|
|
int tile_width, tile_height; ///< tile size
|
|
|
|
int numXtiles, numYtiles;
|
|
|
|
|
|
|
|
uint8_t *buf_start;
|
|
|
|
uint8_t *buf;
|
|
|
|
uint8_t *buf_end;
|
|
|
|
int bit_index;
|
|
|
|
|
|
|
|
int64_t lambda;
|
|
|
|
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000CodingStyle codsty;
|
|
|
|
Jpeg2000QuantStyle qntsty;
|
2011-05-23 23:13:34 +02:00
|
|
|
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000Tile *tile;
|
2015-06-19 05:13:58 +02:00
|
|
|
|
|
|
|
int format;
|
2016-02-03 17:49:19 +01:00
|
|
|
int pred;
|
2020-08-04 14:58:02 +02:00
|
|
|
int sop;
|
|
|
|
int eph;
|
2020-08-06 19:39:32 +02:00
|
|
|
int prog;
|
2013-05-21 21:19:05 +02:00
|
|
|
} Jpeg2000EncoderContext;
|
2011-05-23 23:13:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* debug */
|
|
|
|
#if 0
|
|
|
|
#undef ifprintf
|
|
|
|
#undef printf
|
|
|
|
|
|
|
|
static void nspaces(FILE *fd, int n)
|
|
|
|
{
|
|
|
|
while(n--) putc(' ', fd);
|
|
|
|
}
|
|
|
|
|
2013-05-21 21:19:05 +02:00
|
|
|
static void printcomp(Jpeg2000Component *comp)
|
2011-05-23 23:13:34 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < comp->y1 - comp->y0; i++)
|
2013-05-30 14:40:58 +02:00
|
|
|
ff_jpeg2000_printv(comp->i_data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
|
2011-05-23 23:13:34 +02:00
|
|
|
}
|
|
|
|
|
2013-05-21 21:19:05 +02:00
|
|
|
static void dump(Jpeg2000EncoderContext *s, FILE *fd)
|
2011-05-23 23:13:34 +02:00
|
|
|
{
|
|
|
|
int tileno, compno, reslevelno, bandno, precno;
|
|
|
|
fprintf(fd, "XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
|
|
|
|
"numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
|
|
|
|
"tiles:\n",
|
|
|
|
s->width, s->height, s->tile_width, s->tile_height,
|
|
|
|
s->numXtiles, s->numYtiles, s->ncomponents);
|
|
|
|
for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000Tile *tile = s->tile + tileno;
|
2011-05-23 23:13:34 +02:00
|
|
|
nspaces(fd, 2);
|
|
|
|
fprintf(fd, "tile %d:\n", tileno);
|
|
|
|
for(compno = 0; compno < s->ncomponents; compno++){
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000Component *comp = tile->comp + compno;
|
2011-05-23 23:13:34 +02:00
|
|
|
nspaces(fd, 4);
|
|
|
|
fprintf(fd, "component %d:\n", compno);
|
|
|
|
nspaces(fd, 4);
|
|
|
|
fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
|
|
|
|
comp->x0, comp->x1, comp->y0, comp->y1);
|
|
|
|
for(reslevelno = 0; reslevelno < s->nreslevels; reslevelno++){
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
|
2011-05-23 23:13:34 +02:00
|
|
|
nspaces(fd, 6);
|
|
|
|
fprintf(fd, "reslevel %d:\n", reslevelno);
|
|
|
|
nspaces(fd, 6);
|
|
|
|
fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
|
|
|
|
reslevel->x0, reslevel->x1, reslevel->y0,
|
|
|
|
reslevel->y1, reslevel->nbands);
|
|
|
|
for(bandno = 0; bandno < reslevel->nbands; bandno++){
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000Band *band = reslevel->band + bandno;
|
2011-05-23 23:13:34 +02:00
|
|
|
nspaces(fd, 8);
|
|
|
|
fprintf(fd, "band %d:\n", bandno);
|
|
|
|
nspaces(fd, 8);
|
|
|
|
fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
|
|
|
|
"codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
|
|
|
|
band->x0, band->x1,
|
|
|
|
band->y0, band->y1,
|
|
|
|
band->codeblock_width, band->codeblock_height,
|
|
|
|
band->cblknx, band->cblkny);
|
|
|
|
for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000Prec *prec = band->prec + precno;
|
2011-05-23 23:13:34 +02:00
|
|
|
nspaces(fd, 10);
|
|
|
|
fprintf(fd, "prec %d:\n", precno);
|
|
|
|
nspaces(fd, 10);
|
|
|
|
fprintf(fd, "xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
|
|
|
|
prec->xi0, prec->xi1, prec->yi0, prec->yi1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* bitstream routines */
|
|
|
|
|
|
|
|
/** put n times val bit */
|
2013-05-21 21:19:05 +02:00
|
|
|
static void put_bits(Jpeg2000EncoderContext *s, int val, int n) // TODO: optimize
|
2011-05-23 23:13:34 +02:00
|
|
|
{
|
|
|
|
while (n-- > 0){
|
|
|
|
if (s->bit_index == 8)
|
|
|
|
{
|
|
|
|
s->bit_index = *s->buf == 0xff;
|
|
|
|
*(++s->buf) = 0;
|
|
|
|
}
|
|
|
|
*s->buf |= val << (7 - s->bit_index++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** put n least significant bits of a number num */
|
2013-05-21 21:19:05 +02:00
|
|
|
static void put_num(Jpeg2000EncoderContext *s, int num, int n)
|
2011-05-23 23:13:34 +02:00
|
|
|
{
|
|
|
|
while(--n >= 0)
|
|
|
|
put_bits(s, (num >> n) & 1, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** flush the bitstream */
|
2013-05-21 21:19:05 +02:00
|
|
|
static void j2k_flush(Jpeg2000EncoderContext *s)
|
2011-05-23 23:13:34 +02:00
|
|
|
{
|
|
|
|
if (s->bit_index){
|
|
|
|
s->bit_index = 0;
|
|
|
|
s->buf++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* tag tree routines */
|
|
|
|
|
|
|
|
/** code the value stored in node */
|
2013-05-21 21:19:05 +02:00
|
|
|
static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold)
|
2011-05-23 23:13:34 +02:00
|
|
|
{
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000TgtNode *stack[30];
|
2011-05-23 23:13:34 +02:00
|
|
|
int sp = 1, curval = 0;
|
|
|
|
stack[0] = node;
|
|
|
|
|
|
|
|
node = node->parent;
|
|
|
|
while(node){
|
|
|
|
if (node->vis){
|
|
|
|
curval = node->val;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
node->vis++;
|
|
|
|
stack[sp++] = node;
|
|
|
|
node = node->parent;
|
|
|
|
}
|
|
|
|
while(--sp >= 0){
|
|
|
|
if (stack[sp]->val >= threshold){
|
|
|
|
put_bits(s, 0, threshold - curval);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
put_bits(s, 0, stack[sp]->val - curval);
|
|
|
|
put_bits(s, 1, 1);
|
|
|
|
curval = stack[sp]->val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** update the value in node */
|
2013-05-21 21:19:05 +02:00
|
|
|
static void tag_tree_update(Jpeg2000TgtNode *node)
|
2011-05-23 23:13:34 +02:00
|
|
|
{
|
|
|
|
int lev = 0;
|
|
|
|
while (node->parent){
|
|
|
|
if (node->parent->val <= node->val)
|
|
|
|
break;
|
|
|
|
node->parent->val = node->val;
|
|
|
|
node = node->parent;
|
|
|
|
lev++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-21 21:19:05 +02:00
|
|
|
static int put_siz(Jpeg2000EncoderContext *s)
|
2011-05-23 23:13:34 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (s->buf_end - s->buf < 40 + 3 * s->ncomponents)
|
|
|
|
return -1;
|
|
|
|
|
2013-05-21 21:27:56 +02:00
|
|
|
bytestream_put_be16(&s->buf, JPEG2000_SIZ);
|
2011-05-23 23:13:34 +02:00
|
|
|
bytestream_put_be16(&s->buf, 38 + 3 * s->ncomponents); // Lsiz
|
|
|
|
bytestream_put_be16(&s->buf, 0); // Rsiz
|
|
|
|
bytestream_put_be32(&s->buf, s->width); // width
|
|
|
|
bytestream_put_be32(&s->buf, s->height); // height
|
|
|
|
bytestream_put_be32(&s->buf, 0); // X0Siz
|
|
|
|
bytestream_put_be32(&s->buf, 0); // Y0Siz
|
|
|
|
|
|
|
|
bytestream_put_be32(&s->buf, s->tile_width); // XTSiz
|
|
|
|
bytestream_put_be32(&s->buf, s->tile_height); // YTSiz
|
|
|
|
bytestream_put_be32(&s->buf, 0); // XT0Siz
|
|
|
|
bytestream_put_be32(&s->buf, 0); // YT0Siz
|
|
|
|
bytestream_put_be16(&s->buf, s->ncomponents); // CSiz
|
|
|
|
|
|
|
|
for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
|
2020-07-29 17:49:10 +02:00
|
|
|
bytestream_put_byte(&s->buf, s->cbps[i] - 1);
|
2011-05-23 23:13:34 +02:00
|
|
|
bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[0]:1);
|
|
|
|
bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[1]:1);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-21 21:19:05 +02:00
|
|
|
static int put_cod(Jpeg2000EncoderContext *s)
|
2011-05-23 23:13:34 +02:00
|
|
|
{
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000CodingStyle *codsty = &s->codsty;
|
2020-08-04 14:58:02 +02:00
|
|
|
uint8_t scod = 0;
|
2011-05-23 23:13:34 +02:00
|
|
|
|
|
|
|
if (s->buf_end - s->buf < 14)
|
|
|
|
return -1;
|
|
|
|
|
2013-05-21 21:27:56 +02:00
|
|
|
bytestream_put_be16(&s->buf, JPEG2000_COD);
|
2011-05-23 23:13:34 +02:00
|
|
|
bytestream_put_be16(&s->buf, 12); // Lcod
|
2020-08-04 14:58:02 +02:00
|
|
|
if (s->sop)
|
|
|
|
scod |= JPEG2000_CSTY_SOP;
|
|
|
|
if (s->eph)
|
|
|
|
scod |= JPEG2000_CSTY_EPH;
|
|
|
|
bytestream_put_byte(&s->buf, scod); // Scod
|
2011-05-23 23:13:34 +02:00
|
|
|
// SGcod
|
2020-08-06 19:39:32 +02:00
|
|
|
bytestream_put_byte(&s->buf, s->prog); // progression level
|
2011-05-23 23:13:34 +02:00
|
|
|
bytestream_put_be16(&s->buf, 1); // num of layers
|
2012-10-08 20:54:00 +02:00
|
|
|
if(s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
|
2015-06-19 04:51:59 +02:00
|
|
|
bytestream_put_byte(&s->buf, 0); // unspecified
|
2011-09-27 21:02:02 +02:00
|
|
|
}else{
|
|
|
|
bytestream_put_byte(&s->buf, 0); // unspecified
|
|
|
|
}
|
2011-05-23 23:13:34 +02:00
|
|
|
// SPcod
|
|
|
|
bytestream_put_byte(&s->buf, codsty->nreslevels - 1); // num of decomp. levels
|
|
|
|
bytestream_put_byte(&s->buf, codsty->log2_cblk_width-2); // cblk width
|
|
|
|
bytestream_put_byte(&s->buf, codsty->log2_cblk_height-2); // cblk height
|
|
|
|
bytestream_put_byte(&s->buf, 0); // cblk style
|
2013-05-28 21:58:48 +02:00
|
|
|
bytestream_put_byte(&s->buf, codsty->transform == FF_DWT53); // transformation
|
2011-05-23 23:13:34 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-21 21:19:05 +02:00
|
|
|
static int put_qcd(Jpeg2000EncoderContext *s, int compno)
|
2011-05-23 23:13:34 +02:00
|
|
|
{
|
|
|
|
int i, size;
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000CodingStyle *codsty = &s->codsty;
|
|
|
|
Jpeg2000QuantStyle *qntsty = &s->qntsty;
|
2011-05-23 23:13:34 +02:00
|
|
|
|
2013-05-21 21:27:56 +02:00
|
|
|
if (qntsty->quantsty == JPEG2000_QSTY_NONE)
|
2011-05-23 23:13:34 +02:00
|
|
|
size = 4 + 3 * (codsty->nreslevels-1);
|
|
|
|
else // QSTY_SE
|
|
|
|
size = 5 + 6 * (codsty->nreslevels-1);
|
|
|
|
|
|
|
|
if (s->buf_end - s->buf < size + 2)
|
|
|
|
return -1;
|
|
|
|
|
2013-05-21 21:27:56 +02:00
|
|
|
bytestream_put_be16(&s->buf, JPEG2000_QCD);
|
2011-05-23 23:13:34 +02:00
|
|
|
bytestream_put_be16(&s->buf, size); // LQcd
|
|
|
|
bytestream_put_byte(&s->buf, (qntsty->nguardbits << 5) | qntsty->quantsty); // Sqcd
|
2013-05-21 21:27:56 +02:00
|
|
|
if (qntsty->quantsty == JPEG2000_QSTY_NONE)
|
2011-05-23 23:13:34 +02:00
|
|
|
for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
|
|
|
|
bytestream_put_byte(&s->buf, qntsty->expn[i] << 3);
|
|
|
|
else // QSTY_SE
|
|
|
|
for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
|
|
|
|
bytestream_put_be16(&s->buf, (qntsty->expn[i] << 11) | qntsty->mant[i]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-30 13:45:03 +02:00
|
|
|
static int put_com(Jpeg2000EncoderContext *s, int compno)
|
|
|
|
{
|
|
|
|
int size = 4 + strlen(LIBAVCODEC_IDENT);
|
|
|
|
|
2015-07-27 21:14:31 +02:00
|
|
|
if (s->avctx->flags & AV_CODEC_FLAG_BITEXACT)
|
2015-06-30 13:45:03 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (s->buf_end - s->buf < size + 2)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
bytestream_put_be16(&s->buf, JPEG2000_COM);
|
|
|
|
bytestream_put_be16(&s->buf, size);
|
|
|
|
bytestream_put_be16(&s->buf, 1); // General use (ISO/IEC 8859-15 (Latin) values)
|
|
|
|
|
|
|
|
bytestream_put_buffer(&s->buf, LIBAVCODEC_IDENT, strlen(LIBAVCODEC_IDENT));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-21 21:19:05 +02:00
|
|
|
static uint8_t *put_sot(Jpeg2000EncoderContext *s, int tileno)
|
2011-05-23 23:13:34 +02:00
|
|
|
{
|
|
|
|
uint8_t *psotptr;
|
|
|
|
|
|
|
|
if (s->buf_end - s->buf < 12)
|
2011-09-28 15:18:49 +02:00
|
|
|
return NULL;
|
2011-05-23 23:13:34 +02:00
|
|
|
|
2013-05-21 21:27:56 +02:00
|
|
|
bytestream_put_be16(&s->buf, JPEG2000_SOT);
|
2011-05-23 23:13:34 +02:00
|
|
|
bytestream_put_be16(&s->buf, 10); // Lsot
|
|
|
|
bytestream_put_be16(&s->buf, tileno); // Isot
|
|
|
|
|
|
|
|
psotptr = s->buf;
|
|
|
|
bytestream_put_be32(&s->buf, 0); // Psot (filled in later)
|
|
|
|
|
|
|
|
bytestream_put_byte(&s->buf, 0); // TPsot
|
|
|
|
bytestream_put_byte(&s->buf, 1); // TNsot
|
|
|
|
return psotptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* compute the sizes of tiles, resolution levels, bands, etc.
|
|
|
|
* allocate memory for them
|
|
|
|
* divide the input image into tile-components
|
|
|
|
*/
|
2013-05-21 21:19:05 +02:00
|
|
|
static int init_tiles(Jpeg2000EncoderContext *s)
|
2011-05-23 23:13:34 +02:00
|
|
|
{
|
|
|
|
int tileno, tilex, tiley, compno;
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000CodingStyle *codsty = &s->codsty;
|
|
|
|
Jpeg2000QuantStyle *qntsty = &s->qntsty;
|
2011-05-23 23:13:34 +02:00
|
|
|
|
2013-05-26 13:54:32 +02:00
|
|
|
s->numXtiles = ff_jpeg2000_ceildiv(s->width, s->tile_width);
|
|
|
|
s->numYtiles = ff_jpeg2000_ceildiv(s->height, s->tile_height);
|
2011-05-23 23:13:34 +02:00
|
|
|
|
2014-04-08 22:39:47 +02:00
|
|
|
s->tile = av_malloc_array(s->numXtiles, s->numYtiles * sizeof(Jpeg2000Tile));
|
2011-05-23 23:13:34 +02:00
|
|
|
if (!s->tile)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
for (tileno = 0, tiley = 0; tiley < s->numYtiles; tiley++)
|
|
|
|
for (tilex = 0; tilex < s->numXtiles; tilex++, tileno++){
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000Tile *tile = s->tile + tileno;
|
2011-05-23 23:13:34 +02:00
|
|
|
|
2014-04-08 22:39:47 +02:00
|
|
|
tile->comp = av_mallocz_array(s->ncomponents, sizeof(Jpeg2000Component));
|
2011-05-23 23:13:34 +02:00
|
|
|
if (!tile->comp)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
for (compno = 0; compno < s->ncomponents; compno++){
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000Component *comp = tile->comp + compno;
|
2011-05-23 23:13:34 +02:00
|
|
|
int ret, i, j;
|
|
|
|
|
2013-05-27 22:55:14 +02:00
|
|
|
comp->coord[0][0] = comp->coord_o[0][0] = tilex * s->tile_width;
|
|
|
|
comp->coord[0][1] = comp->coord_o[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
|
|
|
|
comp->coord[1][0] = comp->coord_o[1][0] = tiley * s->tile_height;
|
|
|
|
comp->coord[1][1] = comp->coord_o[1][1] = FFMIN((tiley+1)*s->tile_height, s->height);
|
2011-05-23 23:13:34 +02:00
|
|
|
if (compno > 0)
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
for (j = 0; j < 2; j++)
|
2013-05-27 22:55:14 +02:00
|
|
|
comp->coord[i][j] = comp->coord_o[i][j] = ff_jpeg2000_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
|
2011-05-23 23:13:34 +02:00
|
|
|
|
2015-05-16 18:37:55 +02:00
|
|
|
if ((ret = ff_jpeg2000_init_component(comp,
|
2013-05-27 17:46:54 +02:00
|
|
|
codsty,
|
|
|
|
qntsty,
|
|
|
|
s->cbps[compno],
|
|
|
|
compno?1<<s->chroma_shift[0]:1,
|
|
|
|
compno?1<<s->chroma_shift[1]:1,
|
|
|
|
s->avctx
|
2015-05-16 18:37:55 +02:00
|
|
|
)) < 0)
|
2011-05-23 23:13:34 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-29 17:49:10 +02:00
|
|
|
#define COPY_FRAME(D, PIXEL) \
|
|
|
|
static void copy_frame_ ##D(Jpeg2000EncoderContext *s) \
|
|
|
|
{ \
|
|
|
|
int tileno, compno, i, y, x; \
|
|
|
|
PIXEL *line; \
|
|
|
|
for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){ \
|
|
|
|
Jpeg2000Tile *tile = s->tile + tileno; \
|
|
|
|
if (s->planar){ \
|
|
|
|
for (compno = 0; compno < s->ncomponents; compno++){ \
|
|
|
|
Jpeg2000Component *comp = tile->comp + compno; \
|
|
|
|
int *dst = comp->i_data; \
|
|
|
|
int cbps = s->cbps[compno]; \
|
|
|
|
line = (PIXEL*)s->picture->data[compno] \
|
|
|
|
+ comp->coord[1][0] * (s->picture->linesize[compno] / sizeof(PIXEL)) \
|
|
|
|
+ comp->coord[0][0]; \
|
|
|
|
for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){ \
|
|
|
|
PIXEL *ptr = line; \
|
|
|
|
for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++) \
|
|
|
|
*dst++ = *ptr++ - (1 << (cbps - 1)); \
|
|
|
|
line += s->picture->linesize[compno] / sizeof(PIXEL); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} else{ \
|
|
|
|
line = (PIXEL*)s->picture->data[0] + tile->comp[0].coord[1][0] * (s->picture->linesize[0] / sizeof(PIXEL)) \
|
|
|
|
+ tile->comp[0].coord[0][0] * s->ncomponents; \
|
|
|
|
\
|
|
|
|
i = 0; \
|
|
|
|
for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){ \
|
|
|
|
PIXEL *ptr = line; \
|
|
|
|
for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){ \
|
|
|
|
for (compno = 0; compno < s->ncomponents; compno++){ \
|
|
|
|
int cbps = s->cbps[compno]; \
|
|
|
|
tile->comp[compno].i_data[i] = *ptr++ - (1 << (cbps - 1)); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
line += s->picture->linesize[0] / sizeof(PIXEL); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} \
|
2011-05-23 23:13:34 +02:00
|
|
|
}
|
2020-07-29 17:49:10 +02:00
|
|
|
|
|
|
|
COPY_FRAME(8, uint8_t)
|
|
|
|
COPY_FRAME(16, uint16_t)
|
2011-05-23 23:13:34 +02:00
|
|
|
|
2013-05-21 21:19:05 +02:00
|
|
|
static void init_quantization(Jpeg2000EncoderContext *s)
|
2011-05-23 23:13:34 +02:00
|
|
|
{
|
|
|
|
int compno, reslevelno, bandno;
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000QuantStyle *qntsty = &s->qntsty;
|
|
|
|
Jpeg2000CodingStyle *codsty = &s->codsty;
|
2011-05-23 23:13:34 +02:00
|
|
|
|
|
|
|
for (compno = 0; compno < s->ncomponents; compno++){
|
|
|
|
int gbandno = 0;
|
|
|
|
for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
|
|
|
|
int nbands, lev = codsty->nreslevels - reslevelno - 1;
|
|
|
|
nbands = reslevelno ? 3 : 1;
|
|
|
|
for (bandno = 0; bandno < nbands; bandno++, gbandno++){
|
2015-03-25 14:32:16 +01:00
|
|
|
int expn, mant = 0;
|
2011-05-23 23:13:34 +02:00
|
|
|
|
2013-05-28 21:58:48 +02:00
|
|
|
if (codsty->transform == FF_DWT97_INT){
|
2011-05-23 23:13:34 +02:00
|
|
|
int bandpos = bandno + (reslevelno>0),
|
|
|
|
ss = 81920000 / dwt_norms[0][bandpos][lev],
|
|
|
|
log = av_log2(ss);
|
|
|
|
mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
|
|
|
|
expn = s->cbps[compno] - log + 13;
|
|
|
|
} else
|
|
|
|
expn = ((bandno&2)>>1) + (reslevelno>0) + s->cbps[compno];
|
|
|
|
|
|
|
|
qntsty->expn[gbandno] = expn;
|
|
|
|
qntsty->mant[gbandno] = mant;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-30 20:11:50 +02:00
|
|
|
static void init_luts(void)
|
2011-05-23 23:13:34 +02:00
|
|
|
{
|
|
|
|
int i, a,
|
|
|
|
mask = ~((1<<NMSEDEC_FRACBITS)-1);
|
|
|
|
|
|
|
|
for (i = 0; i < (1 << NMSEDEC_BITS); i++){
|
2020-01-21 01:23:47 +01:00
|
|
|
lut_nmsedec_sig[i] = FFMAX((3 * i << (13 - NMSEDEC_FRACBITS)) - (9 << 11), 0);
|
2011-05-23 23:13:34 +02:00
|
|
|
lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
|
|
|
|
|
|
|
|
a = (i >> (NMSEDEC_BITS-2)&2) + 1;
|
2020-01-21 01:23:47 +01:00
|
|
|
lut_nmsedec_ref[i] = FFMAX((a - 2) * (i << (13 - NMSEDEC_FRACBITS)) +
|
|
|
|
(1 << 13) - (a * a << 11), 0);
|
|
|
|
lut_nmsedec_ref0[i] = FFMAX(((i * i - (i << NMSEDEC_BITS) + (1 << 2 * NMSEDEC_FRACBITS) + (1 << (NMSEDEC_FRACBITS - 1))) & mask)
|
2011-05-23 23:13:34 +02:00
|
|
|
<< 1, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* tier-1 routines */
|
|
|
|
static int getnmsedec_sig(int x, int bpno)
|
|
|
|
{
|
|
|
|
if (bpno > NMSEDEC_FRACBITS)
|
|
|
|
return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
|
|
|
|
return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
|
|
|
|
}
|
|
|
|
|
|
|
|
static int getnmsedec_ref(int x, int bpno)
|
|
|
|
{
|
|
|
|
if (bpno > NMSEDEC_FRACBITS)
|
|
|
|
return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
|
|
|
|
return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
|
|
|
|
}
|
|
|
|
|
2013-05-21 21:19:05 +02:00
|
|
|
static void encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
|
2011-05-23 23:13:34 +02:00
|
|
|
{
|
|
|
|
int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
|
|
|
|
for (y0 = 0; y0 < height; y0 += 4)
|
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
for (y = y0; y < height && y < y0+4; y++){
|
2015-06-26 00:14:01 +02:00
|
|
|
if (!(t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG) && (t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB)){
|
|
|
|
int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno),
|
|
|
|
bit = t1->data[(y) * t1->stride + x] & mask ? 1 : 0;
|
2011-05-23 23:13:34 +02:00
|
|
|
ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
|
|
|
|
if (bit){
|
|
|
|
int xorbit;
|
2015-06-26 00:14:01 +02:00
|
|
|
int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
|
|
|
|
ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
|
|
|
|
*nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
|
|
|
|
ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
|
2011-05-23 23:13:34 +02:00
|
|
|
}
|
2015-06-26 00:14:01 +02:00
|
|
|
t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_VIS;
|
2011-05-23 23:13:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-21 21:19:05 +02:00
|
|
|
static void encode_refpass(Jpeg2000T1Context *t1, int width, int height, int *nmsedec, int bpno)
|
2011-05-23 23:13:34 +02:00
|
|
|
{
|
|
|
|
int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
|
|
|
|
for (y0 = 0; y0 < height; y0 += 4)
|
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
for (y = y0; y < height && y < y0+4; y++)
|
2015-06-26 00:14:01 +02:00
|
|
|
if ((t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG){
|
|
|
|
int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y+1) * t1->stride + x+1]);
|
|
|
|
*nmsedec += getnmsedec_ref(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
|
|
|
|
ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
|
|
|
|
t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_REF;
|
2011-05-23 23:13:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-21 21:19:05 +02:00
|
|
|
static void encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
|
2011-05-23 23:13:34 +02:00
|
|
|
{
|
|
|
|
int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
|
|
|
|
for (y0 = 0; y0 < height; y0 += 4)
|
|
|
|
for (x = 0; x < width; x++){
|
|
|
|
if (y0 + 3 < height && !(
|
2015-06-26 00:14:01 +02:00
|
|
|
(t1->flags[(y0+1) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
|
|
|
|
(t1->flags[(y0+2) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
|
|
|
|
(t1->flags[(y0+3) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
|
|
|
|
(t1->flags[(y0+4) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG))))
|
2011-05-23 23:13:34 +02:00
|
|
|
{
|
|
|
|
// aggregation mode
|
|
|
|
int rlen;
|
|
|
|
for (rlen = 0; rlen < 4; rlen++)
|
2015-06-26 00:14:01 +02:00
|
|
|
if (t1->data[(y0+rlen) * t1->stride + x] & mask)
|
2011-05-23 23:13:34 +02:00
|
|
|
break;
|
|
|
|
ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
|
|
|
|
if (rlen == 4)
|
|
|
|
continue;
|
|
|
|
ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
|
|
|
|
ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
|
|
|
|
for (y = y0 + rlen; y < y0 + 4; y++){
|
2015-06-26 00:14:01 +02:00
|
|
|
if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
|
|
|
|
int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
|
2011-05-23 23:13:34 +02:00
|
|
|
if (y > y0 + rlen)
|
2015-06-26 00:14:01 +02:00
|
|
|
ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
|
|
|
|
if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
|
2011-05-23 23:13:34 +02:00
|
|
|
int xorbit;
|
2015-06-26 00:14:01 +02:00
|
|
|
int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
|
|
|
|
*nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
|
|
|
|
ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
|
|
|
|
ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
|
2011-05-23 23:13:34 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-26 00:14:01 +02:00
|
|
|
t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
|
2011-05-23 23:13:34 +02:00
|
|
|
}
|
|
|
|
} else{
|
|
|
|
for (y = y0; y < y0 + 4 && y < height; y++){
|
2015-06-26 00:14:01 +02:00
|
|
|
if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
|
|
|
|
int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
|
|
|
|
ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
|
|
|
|
if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
|
2011-05-23 23:13:34 +02:00
|
|
|
int xorbit;
|
2015-06-26 00:14:01 +02:00
|
|
|
int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
|
|
|
|
*nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
|
|
|
|
ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
|
|
|
|
ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
|
2011-05-23 23:13:34 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-26 00:14:01 +02:00
|
|
|
t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
|
2011-05-23 23:13:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-21 21:19:05 +02:00
|
|
|
static void encode_cblk(Jpeg2000EncoderContext *s, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, Jpeg2000Tile *tile,
|
2011-05-23 23:13:34 +02:00
|
|
|
int width, int height, int bandpos, int lev)
|
|
|
|
{
|
|
|
|
int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
|
|
|
|
int64_t wmsedec = 0;
|
|
|
|
|
2015-06-26 00:14:01 +02:00
|
|
|
memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
|
2011-05-23 23:13:34 +02:00
|
|
|
|
|
|
|
for (y = 0; y < height; y++){
|
|
|
|
for (x = 0; x < width; x++){
|
2015-06-26 00:14:01 +02:00
|
|
|
if (t1->data[(y) * t1->stride + x] < 0){
|
|
|
|
t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_SGN;
|
|
|
|
t1->data[(y) * t1->stride + x] = -t1->data[(y) * t1->stride + x];
|
2011-05-23 23:13:34 +02:00
|
|
|
}
|
2015-06-26 00:14:01 +02:00
|
|
|
max = FFMAX(max, t1->data[(y) * t1->stride + x]);
|
2011-05-23 23:13:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (max == 0){
|
|
|
|
cblk->nonzerobits = 0;
|
|
|
|
bpno = 0;
|
|
|
|
} else{
|
|
|
|
cblk->nonzerobits = av_log2(max) + 1 - NMSEDEC_FRACBITS;
|
|
|
|
bpno = cblk->nonzerobits - 1;
|
|
|
|
}
|
|
|
|
|
2017-11-18 01:33:18 +01:00
|
|
|
cblk->data[0] = 0;
|
|
|
|
ff_mqc_initenc(&t1->mqc, cblk->data + 1);
|
2011-05-23 23:13:34 +02:00
|
|
|
|
|
|
|
for (passno = 0; bpno >= 0; passno++){
|
|
|
|
nmsedec=0;
|
|
|
|
|
|
|
|
switch(pass_t){
|
|
|
|
case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
|
|
|
|
break;
|
|
|
|
case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
|
|
|
|
break;
|
|
|
|
case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-06-17 20:13:05 +02:00
|
|
|
cblk->passes[passno].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno].flushed, &cblk->passes[passno].flushed_len);
|
2011-05-23 23:13:34 +02:00
|
|
|
wmsedec += (int64_t)nmsedec << (2*bpno);
|
|
|
|
cblk->passes[passno].disto = wmsedec;
|
|
|
|
|
|
|
|
if (++pass_t == 3){
|
|
|
|
pass_t = 0;
|
|
|
|
bpno--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cblk->npasses = passno;
|
|
|
|
cblk->ninclpasses = passno;
|
|
|
|
|
2017-11-30 23:42:04 +01:00
|
|
|
if (passno)
|
|
|
|
cblk->passes[passno-1].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno-1].flushed, &cblk->passes[passno-1].flushed_len);
|
2011-05-23 23:13:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* tier-2 routines: */
|
|
|
|
|
2013-05-21 21:19:05 +02:00
|
|
|
static void putnumpasses(Jpeg2000EncoderContext *s, int n)
|
2011-05-23 23:13:34 +02:00
|
|
|
{
|
|
|
|
if (n == 1)
|
|
|
|
put_num(s, 0, 1);
|
|
|
|
else if (n == 2)
|
|
|
|
put_num(s, 2, 2);
|
|
|
|
else if (n <= 5)
|
|
|
|
put_num(s, 0xc | (n-3), 4);
|
|
|
|
else if (n <= 36)
|
|
|
|
put_num(s, 0x1e0 | (n-6), 9);
|
|
|
|
else
|
|
|
|
put_num(s, 0xff80 | (n-37), 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-21 21:19:05 +02:00
|
|
|
static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int precno,
|
2020-08-04 14:58:02 +02:00
|
|
|
uint8_t *expn, int numgbits, int packetno)
|
2011-05-23 23:13:34 +02:00
|
|
|
{
|
|
|
|
int bandno, empty = 1;
|
|
|
|
// init bitstream
|
|
|
|
*s->buf = 0;
|
|
|
|
s->bit_index = 0;
|
|
|
|
|
2020-08-04 14:58:02 +02:00
|
|
|
if (s->sop) {
|
|
|
|
bytestream_put_be16(&s->buf, JPEG2000_SOP);
|
|
|
|
bytestream_put_be16(&s->buf, 4);
|
|
|
|
bytestream_put_be16(&s->buf, packetno);
|
|
|
|
}
|
2011-05-23 23:13:34 +02:00
|
|
|
// header
|
|
|
|
|
|
|
|
// is the packet empty?
|
|
|
|
for (bandno = 0; bandno < rlevel->nbands; 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
put_bits(s, !empty, 1);
|
|
|
|
if (empty){
|
|
|
|
j2k_flush(s);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (bandno = 0; bandno < rlevel->nbands; bandno++){
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000Band *band = rlevel->band + bandno;
|
|
|
|
Jpeg2000Prec *prec = band->prec + precno;
|
2011-05-23 23:13:34 +02:00
|
|
|
int yi, xi, pos;
|
2013-05-27 15:53:42 +02:00
|
|
|
int cblknw = prec->nb_codeblocks_width;
|
2011-05-23 23:13:34 +02:00
|
|
|
|
|
|
|
if (band->coord[0][0] == band->coord[0][1]
|
|
|
|
|| band->coord[1][0] == band->coord[1][1])
|
|
|
|
continue;
|
|
|
|
|
2013-05-27 15:53:42 +02:00
|
|
|
for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++){
|
|
|
|
for (xi = 0; xi < cblknw; xi++, pos++){
|
2013-05-27 02:18:43 +02:00
|
|
|
prec->cblkincl[pos].val = prec->cblk[yi * cblknw + xi].ninclpasses == 0;
|
2011-05-23 23:13:34 +02:00
|
|
|
tag_tree_update(prec->cblkincl + pos);
|
2013-05-27 02:18:43 +02:00
|
|
|
prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - prec->cblk[yi * cblknw + xi].nonzerobits;
|
2011-05-23 23:13:34 +02:00
|
|
|
tag_tree_update(prec->zerobits + pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-27 15:53:42 +02:00
|
|
|
for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++){
|
|
|
|
for (xi = 0; xi < cblknw; xi++, pos++){
|
2011-05-23 23:13:34 +02:00
|
|
|
int pad = 0, llen, length;
|
2013-05-27 02:18:43 +02:00
|
|
|
Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
|
2011-05-23 23:13:34 +02:00
|
|
|
|
|
|
|
if (s->buf_end - s->buf < 20) // approximately
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// inclusion information
|
|
|
|
tag_tree_code(s, prec->cblkincl + pos, 1);
|
|
|
|
if (!cblk->ninclpasses)
|
|
|
|
continue;
|
|
|
|
// zerobits information
|
|
|
|
tag_tree_code(s, prec->zerobits + pos, 100);
|
|
|
|
// number of passes
|
|
|
|
putnumpasses(s, cblk->ninclpasses);
|
|
|
|
|
|
|
|
length = cblk->passes[cblk->ninclpasses-1].rate;
|
|
|
|
llen = av_log2(length) - av_log2(cblk->ninclpasses) - 2;
|
|
|
|
if (llen < 0){
|
|
|
|
pad = -llen;
|
|
|
|
llen = 0;
|
|
|
|
}
|
|
|
|
// length of code block
|
|
|
|
put_bits(s, 1, llen);
|
|
|
|
put_bits(s, 0, 1);
|
|
|
|
put_num(s, length, av_log2(length)+1+pad);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
j2k_flush(s);
|
2020-08-04 14:58:02 +02:00
|
|
|
if (s->eph) {
|
|
|
|
bytestream_put_be16(&s->buf, JPEG2000_EPH);
|
|
|
|
}
|
|
|
|
|
2011-05-23 23:13:34 +02:00
|
|
|
for (bandno = 0; bandno < rlevel->nbands; bandno++){
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000Band *band = rlevel->band + bandno;
|
|
|
|
Jpeg2000Prec *prec = band->prec + precno;
|
2013-05-27 15:53:42 +02:00
|
|
|
int yi, cblknw = prec->nb_codeblocks_width;
|
|
|
|
for (yi =0; yi < prec->nb_codeblocks_height; yi++){
|
2011-05-23 23:13:34 +02:00
|
|
|
int xi;
|
2013-05-27 15:53:42 +02:00
|
|
|
for (xi = 0; xi < cblknw; xi++){
|
2013-05-27 02:18:43 +02:00
|
|
|
Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
|
2011-05-23 23:13:34 +02:00
|
|
|
if (cblk->ninclpasses){
|
|
|
|
if (s->buf_end - s->buf < cblk->passes[cblk->ninclpasses-1].rate)
|
|
|
|
return -1;
|
2017-11-18 01:33:18 +01:00
|
|
|
bytestream_put_buffer(&s->buf, cblk->data + 1, cblk->passes[cblk->ninclpasses-1].rate
|
2015-06-17 20:13:05 +02:00
|
|
|
- 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);
|
2011-05-23 23:13:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-21 21:19:05 +02:00
|
|
|
static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
|
2011-05-23 23:13:34 +02:00
|
|
|
{
|
|
|
|
int compno, reslevelno, ret;
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000CodingStyle *codsty = &s->codsty;
|
|
|
|
Jpeg2000QuantStyle *qntsty = &s->qntsty;
|
2020-08-04 14:58:02 +02:00
|
|
|
int packetno = 0;
|
2020-08-06 19:39:32 +02:00
|
|
|
int step_x, step_y;
|
|
|
|
int x, y;
|
|
|
|
int tile_coord[2][2];
|
|
|
|
int col = tileno % s->numXtiles;
|
|
|
|
int row = tileno / s->numXtiles;
|
|
|
|
|
|
|
|
tile_coord[0][0] = col * s->tile_width;
|
|
|
|
tile_coord[0][1] = FFMIN(tile_coord[0][0] + s->tile_width, s->width);
|
|
|
|
tile_coord[1][0] = row * s->tile_height;
|
|
|
|
tile_coord[1][1] = FFMIN(tile_coord[1][0] + s->tile_height, s->height);
|
|
|
|
|
2011-05-23 23:13:34 +02:00
|
|
|
av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
|
|
|
|
// lay-rlevel-comp-pos progression
|
2020-08-06 19:39:32 +02:00
|
|
|
switch (s->prog) {
|
|
|
|
case JPEG2000_PGOD_LRCP:
|
2011-05-23 23:13:34 +02:00
|
|
|
for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
|
|
|
|
for (compno = 0; compno < s->ncomponents; compno++){
|
|
|
|
int precno;
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
|
2011-05-23 23:13:34 +02:00
|
|
|
for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
|
2015-05-16 18:37:55 +02:00
|
|
|
if ((ret = encode_packet(s, reslevel, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
|
2020-08-04 14:58:02 +02:00
|
|
|
qntsty->nguardbits, packetno++)) < 0)
|
2011-05-23 23:13:34 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-06 19:39:32 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case JPEG2000_PGOD_RPCL:
|
|
|
|
for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
|
|
|
|
int precno;
|
|
|
|
step_x = 30;
|
|
|
|
step_y = 30;
|
|
|
|
for (compno = 0; compno < s->ncomponents; compno++) {
|
|
|
|
Jpeg2000Component *comp = tile->comp + compno;
|
|
|
|
if (reslevelno < codsty->nreslevels) {
|
|
|
|
uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
|
|
|
|
Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
|
|
|
|
step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
|
|
|
|
step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
step_x = 1<<step_x;
|
|
|
|
step_y = 1<<step_y;
|
|
|
|
for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
|
|
|
|
for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
|
|
|
|
for (compno = 0; compno < s->ncomponents; compno++) {
|
|
|
|
Jpeg2000Component *comp = tile->comp + compno;
|
|
|
|
uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
|
|
|
|
Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
|
|
|
|
int log_subsampling[2] = { compno?s->chroma_shift[0]:0, compno?s->chroma_shift[1]:0};
|
|
|
|
unsigned prcx, prcy;
|
|
|
|
int trx0, try0;
|
|
|
|
|
|
|
|
trx0 = ff_jpeg2000_ceildivpow2(tile_coord[0][0], log_subsampling[0] + reducedresno);
|
|
|
|
try0 = ff_jpeg2000_ceildivpow2(tile_coord[1][0], log_subsampling[1] + reducedresno);
|
|
|
|
|
|
|
|
if (!(y % ((uint64_t)1 << (reslevel->log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
|
|
|
|
(y == tile_coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_height)))))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!(x % ((uint64_t)1 << (reslevel->log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
|
|
|
|
(x == tile_coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_width)))))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// check if a precinct exists
|
|
|
|
prcx = ff_jpeg2000_ceildivpow2(x, log_subsampling[0] + reducedresno) >> reslevel->log2_prec_width;
|
|
|
|
prcy = ff_jpeg2000_ceildivpow2(y, log_subsampling[1] + reducedresno) >> reslevel->log2_prec_height;
|
|
|
|
prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> reslevel->log2_prec_width;
|
|
|
|
prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> reslevel->log2_prec_height;
|
|
|
|
precno = prcx + reslevel->num_precincts_x * prcy;
|
|
|
|
|
|
|
|
if (prcx >= reslevel->num_precincts_x || prcy >= reslevel->num_precincts_y) {
|
|
|
|
av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case JPEG2000_PGOD_PCRL:
|
|
|
|
step_x = 32;
|
|
|
|
step_y = 32;
|
|
|
|
for (compno = 0; compno < s->ncomponents; compno++) {
|
|
|
|
Jpeg2000Component *comp = tile->comp + compno;
|
|
|
|
|
|
|
|
for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
|
|
|
|
uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
|
|
|
|
Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
|
|
|
|
step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
|
|
|
|
step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (step_x >= 31 || step_y >= 31){
|
|
|
|
avpriv_request_sample(s->avctx, "PCRL with large step");
|
|
|
|
return AVERROR_PATCHWELCOME;
|
|
|
|
}
|
|
|
|
step_x = 1<<step_x;
|
|
|
|
step_y = 1<<step_y;
|
|
|
|
|
|
|
|
for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
|
|
|
|
for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
|
|
|
|
for (compno = 0; compno < s->ncomponents; compno++) {
|
|
|
|
Jpeg2000Component *comp = tile->comp + compno;
|
|
|
|
int log_subsampling[2] = { compno?s->chroma_shift[0]:0, compno?s->chroma_shift[1]:0};
|
|
|
|
|
|
|
|
for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
|
|
|
|
unsigned prcx, prcy;
|
|
|
|
int precno;
|
|
|
|
uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
|
|
|
|
Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
|
|
|
|
int trx0, try0;
|
|
|
|
|
|
|
|
trx0 = ff_jpeg2000_ceildivpow2(tile_coord[0][0], log_subsampling[0] + reducedresno);
|
|
|
|
try0 = ff_jpeg2000_ceildivpow2(tile_coord[1][0], log_subsampling[1] + reducedresno);
|
|
|
|
|
|
|
|
if (!(y % ((uint64_t)1 << (reslevel->log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
|
|
|
|
(y == tile_coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_height)))))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!(x % ((uint64_t)1 << (reslevel->log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
|
|
|
|
(x == tile_coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_width)))))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// check if a precinct exists
|
|
|
|
prcx = ff_jpeg2000_ceildivpow2(x, log_subsampling[0] + reducedresno) >> reslevel->log2_prec_width;
|
|
|
|
prcy = ff_jpeg2000_ceildivpow2(y, log_subsampling[1] + reducedresno) >> reslevel->log2_prec_height;
|
|
|
|
prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> reslevel->log2_prec_width;
|
|
|
|
prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> reslevel->log2_prec_height;
|
|
|
|
|
|
|
|
precno = prcx + reslevel->num_precincts_x * prcy;
|
|
|
|
|
|
|
|
if (prcx >= reslevel->num_precincts_x || prcy >= reslevel->num_precincts_y) {
|
|
|
|
av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case JPEG2000_PGOD_CPRL:
|
|
|
|
for (compno = 0; compno < s->ncomponents; compno++) {
|
|
|
|
Jpeg2000Component *comp = tile->comp + compno;
|
|
|
|
int log_subsampling[2] = { compno?s->chroma_shift[0]:0, compno?s->chroma_shift[1]:0};
|
|
|
|
step_x = 32;
|
|
|
|
step_y = 32;
|
|
|
|
|
|
|
|
for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
|
|
|
|
uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
|
|
|
|
Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
|
|
|
|
step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
|
|
|
|
step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
|
|
|
|
}
|
|
|
|
if (step_x >= 31 || step_y >= 31){
|
|
|
|
avpriv_request_sample(s->avctx, "CPRL with large step");
|
|
|
|
return AVERROR_PATCHWELCOME;
|
|
|
|
}
|
|
|
|
step_x = 1<<step_x;
|
|
|
|
step_y = 1<<step_y;
|
|
|
|
|
|
|
|
for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
|
|
|
|
for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
|
|
|
|
for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
|
|
|
|
unsigned prcx, prcy;
|
|
|
|
int precno;
|
|
|
|
int trx0, try0;
|
|
|
|
uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
|
|
|
|
Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
|
|
|
|
|
|
|
|
trx0 = ff_jpeg2000_ceildivpow2(tile_coord[0][0], log_subsampling[0] + reducedresno);
|
|
|
|
try0 = ff_jpeg2000_ceildivpow2(tile_coord[1][0], log_subsampling[1] + reducedresno);
|
|
|
|
|
|
|
|
if (!(y % ((uint64_t)1 << (reslevel->log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
|
|
|
|
(y == tile_coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_height)))))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!(x % ((uint64_t)1 << (reslevel->log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
|
|
|
|
(x == tile_coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_width)))))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// check if a precinct exists
|
|
|
|
prcx = ff_jpeg2000_ceildivpow2(x, log_subsampling[0] + reducedresno) >> reslevel->log2_prec_width;
|
|
|
|
prcy = ff_jpeg2000_ceildivpow2(y, log_subsampling[1] + reducedresno) >> reslevel->log2_prec_height;
|
|
|
|
prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> reslevel->log2_prec_width;
|
|
|
|
prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> reslevel->log2_prec_height;
|
|
|
|
|
|
|
|
precno = prcx + reslevel->num_precincts_x * prcy;
|
|
|
|
|
|
|
|
if (prcx >= reslevel->num_precincts_x || prcy >= reslevel->num_precincts_y) {
|
|
|
|
av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-05-23 23:13:34 +02:00
|
|
|
av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-21 21:19:05 +02:00
|
|
|
static int getcut(Jpeg2000Cblk *cblk, int64_t lambda, int dwt_norm)
|
2011-05-23 23:13:34 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-05-21 21:19:05 +02:00
|
|
|
static void truncpasses(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
|
2011-05-23 23:13:34 +02:00
|
|
|
{
|
2013-05-27 02:18:43 +02:00
|
|
|
int precno, compno, reslevelno, bandno, cblkno, lev;
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000CodingStyle *codsty = &s->codsty;
|
2011-05-23 23:13:34 +02:00
|
|
|
|
|
|
|
for (compno = 0; compno < s->ncomponents; compno++){
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000Component *comp = tile->comp + compno;
|
2011-05-23 23:13:34 +02:00
|
|
|
|
|
|
|
for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
|
2011-05-23 23:13:34 +02:00
|
|
|
|
2013-05-27 02:18:43 +02:00
|
|
|
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;
|
2011-05-23 23:13:34 +02:00
|
|
|
|
2013-05-27 02:18:43 +02:00
|
|
|
for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
|
|
|
|
Jpeg2000Cblk *cblk = prec->cblk + cblkno;
|
2011-05-23 23:13:34 +02:00
|
|
|
|
2013-05-27 02:18:43 +02:00
|
|
|
cblk->ninclpasses = getcut(cblk, s->lambda,
|
2013-07-13 02:24:56 +02:00
|
|
|
(int64_t)dwt_norms[codsty->transform == FF_DWT53][bandpos][lev] * (int64_t)band->i_stepsize >> 15);
|
2013-05-27 02:18:43 +02:00
|
|
|
}
|
2011-05-23 23:13:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-21 21:19:05 +02:00
|
|
|
static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
|
2011-05-23 23:13:34 +02:00
|
|
|
{
|
|
|
|
int compno, reslevelno, bandno, ret;
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000T1Context t1;
|
|
|
|
Jpeg2000CodingStyle *codsty = &s->codsty;
|
2011-05-23 23:13:34 +02:00
|
|
|
for (compno = 0; compno < s->ncomponents; compno++){
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000Component *comp = s->tile[tileno].comp + compno;
|
2011-05-23 23:13:34 +02:00
|
|
|
|
2015-06-26 00:14:01 +02:00
|
|
|
t1.stride = (1<<codsty->log2_cblk_width) + 2;
|
|
|
|
|
2011-05-23 23:13:34 +02:00
|
|
|
av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
|
2015-05-16 18:37:55 +02:00
|
|
|
if ((ret = ff_dwt_encode(&comp->dwt, comp->i_data)) < 0)
|
2011-05-23 23:13:34 +02:00
|
|
|
return ret;
|
|
|
|
av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
|
|
|
|
|
|
|
|
for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
|
2011-05-23 23:13:34 +02:00
|
|
|
|
|
|
|
for (bandno = 0; bandno < reslevel->nbands ; bandno++){
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000Band *band = reslevel->band + bandno;
|
2013-05-27 15:55:29 +02:00
|
|
|
Jpeg2000Prec *prec = band->prec; // we support only 1 precinct per band ATM in the encoder
|
2011-05-23 23:13:34 +02:00
|
|
|
int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
|
|
|
|
yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
|
|
|
|
y0 = yy0;
|
2013-05-26 21:50:17 +02:00
|
|
|
yy1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[1][0] + 1, band->log2_cblk_height) << band->log2_cblk_height,
|
2011-05-23 23:13:34 +02:00
|
|
|
band->coord[1][1]) - band->coord[1][0] + yy0;
|
|
|
|
|
|
|
|
if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
bandpos = bandno + (reslevelno > 0);
|
|
|
|
|
2013-05-27 15:55:29 +02:00
|
|
|
for (cblky = 0; cblky < prec->nb_codeblocks_height; cblky++){
|
2011-05-23 23:13:34 +02:00
|
|
|
if (reslevelno == 0 || bandno == 1)
|
|
|
|
xx0 = 0;
|
|
|
|
else
|
|
|
|
xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
|
|
|
|
x0 = xx0;
|
2013-05-26 21:50:17 +02:00
|
|
|
xx1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[0][0] + 1, band->log2_cblk_width) << band->log2_cblk_width,
|
2011-05-23 23:13:34 +02:00
|
|
|
band->coord[0][1]) - band->coord[0][0] + xx0;
|
|
|
|
|
2013-05-27 15:55:29 +02:00
|
|
|
for (cblkx = 0; cblkx < prec->nb_codeblocks_width; cblkx++, cblkno++){
|
2011-05-23 23:13:34 +02:00
|
|
|
int y, x;
|
|
|
|
if (codsty->transform == FF_DWT53){
|
|
|
|
for (y = yy0; y < yy1; y++){
|
2015-06-26 00:14:01 +02:00
|
|
|
int *ptr = t1.data + (y-yy0)*t1.stride;
|
2011-05-23 23:13:34 +02:00
|
|
|
for (x = xx0; x < xx1; x++){
|
2020-01-21 01:23:47 +01:00
|
|
|
*ptr++ = comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] * (1 << NMSEDEC_FRACBITS);
|
2011-05-23 23:13:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else{
|
|
|
|
for (y = yy0; y < yy1; y++){
|
2015-06-26 00:14:01 +02:00
|
|
|
int *ptr = t1.data + (y-yy0)*t1.stride;
|
2011-05-23 23:13:34 +02:00
|
|
|
for (x = xx0; x < xx1; x++){
|
2013-05-30 14:11:03 +02:00
|
|
|
*ptr = (comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]);
|
2013-07-13 02:24:56 +02:00
|
|
|
*ptr = (int64_t)*ptr * (int64_t)(16384 * 65536 / band->i_stepsize) >> 15 - NMSEDEC_FRACBITS;
|
2012-10-15 03:18:08 +02:00
|
|
|
ptr++;
|
2011-05-23 23:13:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-24 03:49:55 +01:00
|
|
|
if (!prec->cblk[cblkno].data)
|
|
|
|
prec->cblk[cblkno].data = av_malloc(1 + 8192);
|
2017-11-30 21:51:56 +01:00
|
|
|
if (!prec->cblk[cblkno].passes)
|
|
|
|
prec->cblk[cblkno].passes = av_malloc_array(JPEG2000_MAX_PASSES, sizeof (*prec->cblk[cblkno].passes));
|
|
|
|
if (!prec->cblk[cblkno].data || !prec->cblk[cblkno].passes)
|
2017-11-18 01:33:18 +01:00
|
|
|
return AVERROR(ENOMEM);
|
2013-05-27 15:55:29 +02:00
|
|
|
encode_cblk(s, &t1, prec->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
|
2011-05-23 23:13:34 +02:00
|
|
|
bandpos, codsty->nreslevels - reslevelno - 1);
|
|
|
|
xx0 = xx1;
|
2013-05-26 21:50:17 +02:00
|
|
|
xx1 = FFMIN(xx1 + (1 << band->log2_cblk_width), band->coord[0][1] - band->coord[0][0] + x0);
|
2011-05-23 23:13:34 +02:00
|
|
|
}
|
|
|
|
yy0 = yy1;
|
2013-05-26 21:50:17 +02:00
|
|
|
yy1 = FFMIN(yy1 + (1 << band->log2_cblk_height), band->coord[1][1] - band->coord[1][0] + y0);
|
2011-05-23 23:13:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
|
|
|
|
truncpasses(s, tile);
|
2015-05-16 18:37:55 +02:00
|
|
|
if ((ret = encode_packets(s, tile, tileno)) < 0)
|
2011-05-23 23:13:34 +02:00
|
|
|
return ret;
|
|
|
|
av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-21 21:19:05 +02:00
|
|
|
static void cleanup(Jpeg2000EncoderContext *s)
|
2011-05-23 23:13:34 +02:00
|
|
|
{
|
|
|
|
int tileno, compno;
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000CodingStyle *codsty = &s->codsty;
|
2011-05-23 23:13:34 +02:00
|
|
|
|
|
|
|
for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
|
|
|
|
for (compno = 0; compno < s->ncomponents; compno++){
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000Component *comp = s->tile[tileno].comp + compno;
|
2013-05-30 14:40:58 +02:00
|
|
|
ff_jpeg2000_cleanup(comp, codsty);
|
2011-05-23 23:13:34 +02:00
|
|
|
}
|
|
|
|
av_freep(&s->tile[tileno].comp);
|
|
|
|
}
|
|
|
|
av_freep(&s->tile);
|
|
|
|
}
|
|
|
|
|
2013-05-21 21:19:05 +02:00
|
|
|
static void reinit(Jpeg2000EncoderContext *s)
|
2011-05-23 23:13:34 +02:00
|
|
|
{
|
|
|
|
int tileno, compno;
|
|
|
|
for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000Tile *tile = s->tile + tileno;
|
2011-05-23 23:13:34 +02:00
|
|
|
for (compno = 0; compno < s->ncomponents; compno++)
|
2013-05-30 14:40:58 +02:00
|
|
|
ff_jpeg2000_reinit(tile->comp + compno, &s->codsty);
|
2011-05-23 23:13:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-19 05:13:58 +02:00
|
|
|
static void update_size(uint8_t *size, const uint8_t *end)
|
|
|
|
{
|
|
|
|
AV_WB32(size, end-size);
|
|
|
|
}
|
|
|
|
|
2012-02-24 04:12:40 +01:00
|
|
|
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
|
|
|
|
const AVFrame *pict, int *got_packet)
|
2011-05-23 23:13:34 +02:00
|
|
|
{
|
|
|
|
int tileno, ret;
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000EncoderContext *s = avctx->priv_data;
|
2015-06-19 05:13:58 +02:00
|
|
|
uint8_t *chunkstart, *jp2cstart, *jp2hstart;
|
2011-05-23 23:13:34 +02:00
|
|
|
|
2015-07-27 22:53:16 +02:00
|
|
|
if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*9 + AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
|
2012-02-24 04:12:40 +01:00
|
|
|
return ret;
|
|
|
|
|
2011-05-23 23:13:34 +02:00
|
|
|
// init:
|
2012-02-24 04:12:40 +01:00
|
|
|
s->buf = s->buf_start = pkt->data;
|
|
|
|
s->buf_end = pkt->data + pkt->size;
|
2011-05-23 23:13:34 +02:00
|
|
|
|
2013-12-17 16:27:36 +01:00
|
|
|
s->picture = pict;
|
2011-05-23 23:13:34 +02:00
|
|
|
|
2013-12-17 16:27:36 +01:00
|
|
|
s->lambda = s->picture->quality * LAMBDA_SCALE;
|
2011-05-23 23:13:34 +02:00
|
|
|
|
2020-07-29 17:49:10 +02:00
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_BGR48 || avctx->pix_fmt == AV_PIX_FMT_GRAY16)
|
|
|
|
copy_frame_16(s);
|
|
|
|
else
|
|
|
|
copy_frame_8(s);
|
|
|
|
|
2011-05-23 23:13:34 +02:00
|
|
|
reinit(s);
|
|
|
|
|
2015-06-19 05:13:58 +02:00
|
|
|
if (s->format == CODEC_JP2) {
|
|
|
|
av_assert0(s->buf == pkt->data);
|
|
|
|
|
|
|
|
bytestream_put_be32(&s->buf, 0x0000000C);
|
|
|
|
bytestream_put_be32(&s->buf, 0x6A502020);
|
|
|
|
bytestream_put_be32(&s->buf, 0x0D0A870A);
|
|
|
|
|
|
|
|
chunkstart = s->buf;
|
|
|
|
bytestream_put_be32(&s->buf, 0);
|
|
|
|
bytestream_put_buffer(&s->buf, "ftyp", 4);
|
|
|
|
bytestream_put_buffer(&s->buf, "jp2\040\040", 4);
|
|
|
|
bytestream_put_be32(&s->buf, 0);
|
2015-06-30 14:42:31 +02:00
|
|
|
bytestream_put_buffer(&s->buf, "jp2\040", 4);
|
2015-06-19 05:13:58 +02:00
|
|
|
update_size(chunkstart, s->buf);
|
|
|
|
|
|
|
|
jp2hstart = s->buf;
|
|
|
|
bytestream_put_be32(&s->buf, 0);
|
|
|
|
bytestream_put_buffer(&s->buf, "jp2h", 4);
|
|
|
|
|
|
|
|
chunkstart = s->buf;
|
|
|
|
bytestream_put_be32(&s->buf, 0);
|
|
|
|
bytestream_put_buffer(&s->buf, "ihdr", 4);
|
|
|
|
bytestream_put_be32(&s->buf, avctx->height);
|
|
|
|
bytestream_put_be32(&s->buf, avctx->width);
|
|
|
|
bytestream_put_be16(&s->buf, s->ncomponents);
|
|
|
|
bytestream_put_byte(&s->buf, s->cbps[0]);
|
|
|
|
bytestream_put_byte(&s->buf, 7);
|
|
|
|
bytestream_put_byte(&s->buf, 0);
|
|
|
|
bytestream_put_byte(&s->buf, 0);
|
|
|
|
update_size(chunkstart, s->buf);
|
|
|
|
|
|
|
|
chunkstart = s->buf;
|
|
|
|
bytestream_put_be32(&s->buf, 0);
|
|
|
|
bytestream_put_buffer(&s->buf, "colr", 4);
|
|
|
|
bytestream_put_byte(&s->buf, 1);
|
|
|
|
bytestream_put_byte(&s->buf, 0);
|
|
|
|
bytestream_put_byte(&s->buf, 0);
|
2018-05-22 02:21:04 +02:00
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_RGB24 || avctx->pix_fmt == AV_PIX_FMT_PAL8) {
|
2015-06-19 05:13:58 +02:00
|
|
|
bytestream_put_be32(&s->buf, 16);
|
2018-05-22 02:21:04 +02:00
|
|
|
} else if (s->ncomponents == 1) {
|
|
|
|
bytestream_put_be32(&s->buf, 17);
|
2015-06-19 05:13:58 +02:00
|
|
|
} else {
|
|
|
|
bytestream_put_be32(&s->buf, 18);
|
|
|
|
}
|
|
|
|
update_size(chunkstart, s->buf);
|
2018-05-22 02:21:04 +02:00
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
|
|
|
|
int i;
|
|
|
|
uint8_t *palette = pict->data[1];
|
|
|
|
chunkstart = s->buf;
|
|
|
|
bytestream_put_be32(&s->buf, 0);
|
|
|
|
bytestream_put_buffer(&s->buf, "pclr", 4);
|
|
|
|
bytestream_put_be16(&s->buf, AVPALETTE_COUNT);
|
|
|
|
bytestream_put_byte(&s->buf, 3); // colour channels
|
|
|
|
bytestream_put_be24(&s->buf, 0x070707); //colour depths
|
|
|
|
for (i = 0; i < AVPALETTE_COUNT; i++) {
|
|
|
|
bytestream_put_be24(&s->buf, HAVE_BIGENDIAN ? AV_RB24(palette + 1) : AV_RL24(palette));
|
|
|
|
palette += 4;
|
|
|
|
}
|
|
|
|
update_size(chunkstart, s->buf);
|
|
|
|
chunkstart = s->buf;
|
|
|
|
bytestream_put_be32(&s->buf, 0);
|
|
|
|
bytestream_put_buffer(&s->buf, "cmap", 4);
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
bytestream_put_be16(&s->buf, 0); // component
|
|
|
|
bytestream_put_byte(&s->buf, 1); // palette mapping
|
|
|
|
bytestream_put_byte(&s->buf, i); // index
|
|
|
|
}
|
|
|
|
update_size(chunkstart, s->buf);
|
|
|
|
}
|
2015-06-19 05:13:58 +02:00
|
|
|
update_size(jp2hstart, s->buf);
|
|
|
|
|
|
|
|
jp2cstart = s->buf;
|
|
|
|
bytestream_put_be32(&s->buf, 0);
|
|
|
|
bytestream_put_buffer(&s->buf, "jp2c", 4);
|
|
|
|
}
|
|
|
|
|
2011-05-23 23:13:34 +02:00
|
|
|
if (s->buf_end - s->buf < 2)
|
|
|
|
return -1;
|
2013-05-21 21:27:56 +02:00
|
|
|
bytestream_put_be16(&s->buf, JPEG2000_SOC);
|
2015-05-16 18:37:55 +02:00
|
|
|
if ((ret = put_siz(s)) < 0)
|
2011-05-23 23:13:34 +02:00
|
|
|
return ret;
|
2015-05-16 18:37:55 +02:00
|
|
|
if ((ret = put_cod(s)) < 0)
|
2011-05-23 23:13:34 +02:00
|
|
|
return ret;
|
2015-05-16 18:37:55 +02:00
|
|
|
if ((ret = put_qcd(s, 0)) < 0)
|
2011-05-23 23:13:34 +02:00
|
|
|
return ret;
|
2015-06-30 13:45:03 +02:00
|
|
|
if ((ret = put_com(s, 0)) < 0)
|
|
|
|
return ret;
|
2011-05-23 23:13:34 +02:00
|
|
|
|
|
|
|
for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
|
|
|
|
uint8_t *psotptr;
|
2011-09-28 15:18:49 +02:00
|
|
|
if (!(psotptr = put_sot(s, tileno)))
|
|
|
|
return -1;
|
2011-05-23 23:13:34 +02:00
|
|
|
if (s->buf_end - s->buf < 2)
|
|
|
|
return -1;
|
2013-05-21 21:27:56 +02:00
|
|
|
bytestream_put_be16(&s->buf, JPEG2000_SOD);
|
2015-05-16 18:37:55 +02:00
|
|
|
if ((ret = encode_tile(s, s->tile + tileno, tileno)) < 0)
|
2011-05-23 23:13:34 +02:00
|
|
|
return ret;
|
|
|
|
bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
|
|
|
|
}
|
|
|
|
if (s->buf_end - s->buf < 2)
|
|
|
|
return -1;
|
2013-05-21 21:27:56 +02:00
|
|
|
bytestream_put_be16(&s->buf, JPEG2000_EOC);
|
2011-05-23 23:13:34 +02:00
|
|
|
|
2015-06-19 05:13:58 +02:00
|
|
|
if (s->format == CODEC_JP2)
|
|
|
|
update_size(jp2cstart, s->buf);
|
|
|
|
|
2011-05-23 23:13:34 +02:00
|
|
|
av_log(s->avctx, AV_LOG_DEBUG, "end\n");
|
2012-02-24 04:12:40 +01:00
|
|
|
pkt->size = s->buf - s->buf_start;
|
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
|
|
|
*got_packet = 1;
|
|
|
|
|
|
|
|
return 0;
|
2011-05-23 23:13:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int j2kenc_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
int i, ret;
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000EncoderContext *s = avctx->priv_data;
|
|
|
|
Jpeg2000CodingStyle *codsty = &s->codsty;
|
|
|
|
Jpeg2000QuantStyle *qntsty = &s->qntsty;
|
2011-05-23 23:13:34 +02:00
|
|
|
|
|
|
|
s->avctx = avctx;
|
|
|
|
av_log(s->avctx, AV_LOG_DEBUG, "init\n");
|
|
|
|
|
2016-02-03 17:49:19 +01:00
|
|
|
#if FF_API_PRIVATE_OPT
|
|
|
|
FF_DISABLE_DEPRECATION_WARNINGS
|
|
|
|
if (avctx->prediction_method)
|
|
|
|
s->pred = avctx->prediction_method;
|
|
|
|
FF_ENABLE_DEPRECATION_WARNINGS
|
|
|
|
#endif
|
|
|
|
|
2018-05-22 02:21:04 +02:00
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_PAL8 && (s->pred != FF_DWT97_INT || s->format != CODEC_JP2)) {
|
|
|
|
av_log(s->avctx, AV_LOG_WARNING, "Forcing lossless jp2 for pal8\n");
|
|
|
|
s->pred = FF_DWT97_INT;
|
|
|
|
s->format = CODEC_JP2;
|
|
|
|
}
|
|
|
|
|
2011-05-23 23:13:34 +02:00
|
|
|
// defaults:
|
|
|
|
// TODO: implement setting non-standard precinct size
|
2013-05-26 21:50:17 +02:00
|
|
|
memset(codsty->log2_prec_widths , 15, sizeof(codsty->log2_prec_widths ));
|
|
|
|
memset(codsty->log2_prec_heights, 15, sizeof(codsty->log2_prec_heights));
|
2013-05-27 22:55:14 +02:00
|
|
|
codsty->nreslevels2decode=
|
2011-05-23 23:13:34 +02:00
|
|
|
codsty->nreslevels = 7;
|
|
|
|
codsty->log2_cblk_width = 4;
|
|
|
|
codsty->log2_cblk_height = 4;
|
2016-02-03 17:49:19 +01:00
|
|
|
codsty->transform = s->pred ? FF_DWT53 : FF_DWT97_INT;
|
2011-05-23 23:13:34 +02:00
|
|
|
|
|
|
|
qntsty->nguardbits = 1;
|
|
|
|
|
2015-07-03 18:23:08 +02:00
|
|
|
if ((s->tile_width & (s->tile_width -1)) ||
|
|
|
|
(s->tile_height & (s->tile_height-1))) {
|
|
|
|
av_log(avctx, AV_LOG_WARNING, "Tile dimension not a power of 2\n");
|
|
|
|
}
|
2011-05-23 23:13:34 +02:00
|
|
|
|
|
|
|
if (codsty->transform == FF_DWT53)
|
2013-05-21 21:27:56 +02:00
|
|
|
qntsty->quantsty = JPEG2000_QSTY_NONE;
|
2011-05-23 23:13:34 +02:00
|
|
|
else
|
2013-05-21 21:27:56 +02:00
|
|
|
qntsty->quantsty = JPEG2000_QSTY_SE;
|
2011-05-23 23:13:34 +02:00
|
|
|
|
|
|
|
s->width = avctx->width;
|
|
|
|
s->height = avctx->height;
|
|
|
|
|
2020-07-29 17:49:10 +02:00
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_GRAY16 || avctx->pix_fmt == AV_PIX_FMT_RGB48)
|
|
|
|
s->cbps[i] = 16;
|
|
|
|
else
|
|
|
|
s->cbps[i] = 8;
|
|
|
|
}
|
2011-05-23 23:13:34 +02:00
|
|
|
|
2020-07-29 17:49:10 +02:00
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_RGB24 || avctx->pix_fmt == AV_PIX_FMT_RGB48){
|
2011-05-23 23:13:34 +02:00
|
|
|
s->ncomponents = 3;
|
2020-07-29 17:49:10 +02:00
|
|
|
} else if (avctx->pix_fmt == AV_PIX_FMT_GRAY8 || avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY16){
|
2011-05-23 23:13:34 +02:00
|
|
|
s->ncomponents = 1;
|
|
|
|
} else{ // planar YUV
|
|
|
|
s->planar = 1;
|
|
|
|
s->ncomponents = 3;
|
2017-11-06 22:39:16 +01:00
|
|
|
ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt,
|
|
|
|
s->chroma_shift, s->chroma_shift + 1);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2011-05-23 23:13:34 +02:00
|
|
|
}
|
|
|
|
|
2013-05-26 17:42:25 +02:00
|
|
|
ff_jpeg2000_init_tier1_luts();
|
2013-07-12 23:01:43 +02:00
|
|
|
ff_mqc_init_context_tables();
|
2011-05-23 23:13:34 +02:00
|
|
|
init_luts();
|
|
|
|
|
|
|
|
init_quantization(s);
|
2015-05-16 18:37:55 +02:00
|
|
|
if ((ret=init_tiles(s)) < 0)
|
2011-05-23 23:13:34 +02:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int j2kenc_destroy(AVCodecContext *avctx)
|
|
|
|
{
|
2013-05-21 21:19:05 +02:00
|
|
|
Jpeg2000EncoderContext *s = avctx->priv_data;
|
2011-05-23 23:13:34 +02:00
|
|
|
|
|
|
|
cleanup(s);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-19 05:13:58 +02:00
|
|
|
// taken from the libopenjpeg wraper so it matches
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(Jpeg2000EncoderContext, x)
|
|
|
|
#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
|
|
|
|
static const AVOption options[] = {
|
2015-06-19 05:13:59 +02:00
|
|
|
{ "format", "Codec Format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = CODEC_JP2 }, CODEC_J2K, CODEC_JP2, VE, "format" },
|
2015-06-19 05:13:58 +02:00
|
|
|
{ "j2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_J2K }, 0, 0, VE, "format" },
|
|
|
|
{ "jp2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_JP2 }, 0, 0, VE, "format" },
|
2015-07-03 18:23:08 +02:00
|
|
|
{ "tile_width", "Tile Width", OFFSET(tile_width), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, 1<<30, VE, },
|
|
|
|
{ "tile_height", "Tile Height", OFFSET(tile_height), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, 1<<30, VE, },
|
2016-02-03 17:49:19 +01:00
|
|
|
{ "pred", "DWT Type", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, "pred" },
|
|
|
|
{ "dwt97int", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "pred" },
|
|
|
|
{ "dwt53", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "pred" },
|
2020-08-04 14:58:02 +02:00
|
|
|
{ "sop", "SOP marker", OFFSET(sop), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, },
|
|
|
|
{ "eph", "EPH marker", OFFSET(eph), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, },
|
2020-08-06 19:39:32 +02:00
|
|
|
{ "prog", "Progression Order", OFFSET(prog), AV_OPT_TYPE_INT, { .i64 = 0 }, JPEG2000_PGOD_LRCP, JPEG2000_PGOD_CPRL, VE, "prog" },
|
|
|
|
{ "lrcp", NULL, OFFSET(prog), AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_LRCP }, 0, 0, VE, "prog" },
|
|
|
|
{ "rlcp", NULL, OFFSET(prog), AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_RLCP }, 0, 0, VE, "prog" },
|
|
|
|
{ "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" },
|
2015-06-19 05:13:58 +02:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass j2k_class = {
|
|
|
|
.class_name = "jpeg 2000 encoder",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
2013-05-31 02:12:22 +02:00
|
|
|
AVCodec ff_jpeg2000_encoder = {
|
|
|
|
.name = "jpeg2000",
|
2013-10-04 15:33:20 +02:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
|
2011-12-30 20:00:53 +01:00
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-08-07 22:45:46 +02:00
|
|
|
.id = AV_CODEC_ID_JPEG2000,
|
2013-05-21 21:19:05 +02:00
|
|
|
.priv_data_size = sizeof(Jpeg2000EncoderContext),
|
2011-12-30 20:00:53 +01:00
|
|
|
.init = j2kenc_init,
|
2012-02-24 04:12:40 +01:00
|
|
|
.encode2 = encode_frame,
|
2011-12-30 20:00:53 +01:00
|
|
|
.close = j2kenc_destroy,
|
2012-10-08 20:54:00 +02:00
|
|
|
.pix_fmts = (const enum AVPixelFormat[]) {
|
|
|
|
AV_PIX_FMT_RGB24, AV_PIX_FMT_YUV444P, AV_PIX_FMT_GRAY8,
|
2015-07-05 23:49:17 +02:00
|
|
|
AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
|
|
|
|
AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
|
2018-05-22 02:21:04 +02:00
|
|
|
AV_PIX_FMT_PAL8,
|
2020-07-29 17:49:10 +02:00
|
|
|
AV_PIX_FMT_RGB48, AV_PIX_FMT_GRAY16,
|
2012-10-08 20:54:00 +02:00
|
|
|
AV_PIX_FMT_NONE
|
2015-06-19 05:13:58 +02:00
|
|
|
},
|
|
|
|
.priv_class = &j2k_class,
|
2011-05-23 23:13:34 +02:00
|
|
|
};
|