1 /*
2  * JPEG2000 image encoder
3  * Copyright (c) 2007 Kamil Nowosad
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  * **********************************************************************************************************************
22  *
23  *
24  *
25  * This source code incorporates work covered by the following copyright and
26  * permission notice:
27  *
28  * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
29  * Copyright (c) 2002-2007, Professor Benoit Macq
30  * Copyright (c) 2001-2003, David Janssens
31  * Copyright (c) 2002-2003, Yannick Verschueren
32  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
33  * Copyright (c) 2005, Herve Drolon, FreeImage Team
34  * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
35  * Copyright (c) 2020, Gautam Ramakrishnan <gautamramk@gmail.com>
36  * All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
48  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
51  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
52  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
53  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
54  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
55  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
57  * POSSIBILITY OF SUCH DAMAGE.
58  */
59 
60 
61 /**
62  * JPEG2000 image encoder
63  * @file
64  * @author Kamil Nowosad
65  */
66 
67 #include <float.h>
68 #include "avcodec.h"
69 #include "codec_internal.h"
70 #include "encode.h"
71 #include "bytestream.h"
72 #include "jpeg2000.h"
73 #include "version.h"
74 #include "libavutil/common.h"
75 #include "libavutil/pixdesc.h"
76 #include "libavutil/opt.h"
77 #include "libavutil/intreadwrite.h"
78 #include "libavutil/avstring.h"
79 #include "libavutil/thread.h"
80 
81 #define NMSEDEC_BITS 7
82 #define NMSEDEC_FRACBITS (NMSEDEC_BITS-1)
83 #define WMSEDEC_SHIFT 13 ///< must be >= 13
84 #define LAMBDA_SCALE (100000000LL << (WMSEDEC_SHIFT - 13))
85 
86 #define CODEC_JP2 1
87 #define CODEC_J2K 0
88 
89 static int lut_nmsedec_ref [1<<NMSEDEC_BITS],
90            lut_nmsedec_ref0[1<<NMSEDEC_BITS],
91            lut_nmsedec_sig [1<<NMSEDEC_BITS],
92            lut_nmsedec_sig0[1<<NMSEDEC_BITS];
93 
94 static const int dwt_norms[2][4][10] = { // [dwt_type][band][rlevel] (multiplied by 10000)
95     {{10000, 19650, 41770,  84030, 169000, 338400,  676900, 1353000, 2706000, 5409000},
96      {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
97      {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
98      {20800, 38650, 83070, 171800, 347100, 695900, 1393000, 2786000, 5572000}},
99 
100     {{10000, 15000, 27500, 53750, 106800, 213400, 426700, 853300, 1707000, 3413000},
101      {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
102      {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
103      { 7186,  9218, 15860, 30430,  60190, 120100, 240000, 479700,  959300}}
104 };
105 
106 typedef struct {
107    Jpeg2000Component *comp;
108    double *layer_rates;
109 } Jpeg2000Tile;
110 
111 typedef struct {
112     AVClass *class;
113     AVCodecContext *avctx;
114     const AVFrame *picture;
115 
116     int width, height; ///< image width and height
117     uint8_t cbps[4]; ///< bits per sample in particular components
118     int chroma_shift[2];
119     uint8_t planar;
120     int ncomponents;
121     int tile_width, tile_height; ///< tile size
122     int numXtiles, numYtiles;
123 
124     uint8_t *buf_start;
125     uint8_t *buf;
126     uint8_t *buf_end;
127     int bit_index;
128 
129     int64_t lambda;
130 
131     Jpeg2000CodingStyle codsty;
132     Jpeg2000QuantStyle  qntsty;
133 
134     Jpeg2000Tile *tile;
135     int layer_rates[100];
136     uint8_t compression_rate_enc; ///< Is compression done using compression ratio?
137 
138     int format;
139     int pred;
140     int sop;
141     int eph;
142     int prog;
143     int nlayers;
144     char *lr_str;
145 } Jpeg2000EncoderContext;
146 
147 
148 /* debug */
149 #if 0
150 #undef ifprintf
151 #undef printf
152 
153 static void nspaces(FILE *fd, int n)
154 {
155     while(n--) putc(' ', fd);
156 }
157 
158 static void printcomp(Jpeg2000Component *comp)
159 {
160     int i;
161     for (i = 0; i < comp->y1 - comp->y0; i++)
162         ff_jpeg2000_printv(comp->i_data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
163 }
164 
165 static void dump(Jpeg2000EncoderContext *s, FILE *fd)
166 {
167     int tileno, compno, reslevelno, bandno, precno;
168     fprintf(fd, "XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
169                 "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
170                 "tiles:\n",
171             s->width, s->height, s->tile_width, s->tile_height,
172             s->numXtiles, s->numYtiles, s->ncomponents);
173     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
174         Jpeg2000Tile *tile = s->tile + tileno;
175         nspaces(fd, 2);
176         fprintf(fd, "tile %d:\n", tileno);
177         for(compno = 0; compno < s->ncomponents; compno++){
178             Jpeg2000Component *comp = tile->comp + compno;
179             nspaces(fd, 4);
180             fprintf(fd, "component %d:\n", compno);
181             nspaces(fd, 4);
182             fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
183                         comp->x0, comp->x1, comp->y0, comp->y1);
184             for(reslevelno = 0; reslevelno < s->nreslevels; reslevelno++){
185                 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
186                 nspaces(fd, 6);
187                 fprintf(fd, "reslevel %d:\n", reslevelno);
188                 nspaces(fd, 6);
189                 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
190                         reslevel->x0, reslevel->x1, reslevel->y0,
191                         reslevel->y1, reslevel->nbands);
192                 for(bandno = 0; bandno < reslevel->nbands; bandno++){
193                     Jpeg2000Band *band = reslevel->band + bandno;
194                     nspaces(fd, 8);
195                     fprintf(fd, "band %d:\n", bandno);
196                     nspaces(fd, 8);
197                     fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
198                                 "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
199                                 band->x0, band->x1,
200                                 band->y0, band->y1,
201                                 band->codeblock_width, band->codeblock_height,
202                                 band->cblknx, band->cblkny);
203                     for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
204                         Jpeg2000Prec *prec = band->prec + precno;
205                         nspaces(fd, 10);
206                         fprintf(fd, "prec %d:\n", precno);
207                         nspaces(fd, 10);
208                         fprintf(fd, "xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
209                                      prec->xi0, prec->xi1, prec->yi0, prec->yi1);
210                     }
211                 }
212             }
213         }
214     }
215 }
216 #endif
217 
218 /* bitstream routines */
219 
220 /** put n times val bit */
put_bits(Jpeg2000EncoderContext *s, int val, int n)221 static void put_bits(Jpeg2000EncoderContext *s, int val, int n) // TODO: optimize
222 {
223     while (n-- > 0){
224         if (s->bit_index == 8)
225         {
226             s->bit_index = *s->buf == 0xff;
227             *(++s->buf) = 0;
228         }
229         *s->buf |= val << (7 - s->bit_index++);
230     }
231 }
232 
233 /** put n least significant bits of a number num */
put_num(Jpeg2000EncoderContext *s, int num, int n)234 static void put_num(Jpeg2000EncoderContext *s, int num, int n)
235 {
236     while(--n >= 0)
237         put_bits(s, (num >> n) & 1, 1);
238 }
239 
240 /** flush the bitstream */
j2k_flush(Jpeg2000EncoderContext *s)241 static void j2k_flush(Jpeg2000EncoderContext *s)
242 {
243     if (s->bit_index){
244         s->bit_index = 0;
245         s->buf++;
246     }
247 }
248 
249 /* tag tree routines */
250 
251 /** code the value stored in node */
tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold)252 static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold)
253 {
254     Jpeg2000TgtNode *stack[30];
255     int sp = -1, curval = 0;
256 
257     while(node->parent){
258         stack[++sp] = node;
259         node = node->parent;
260     }
261 
262     while (1) {
263         if (curval > node->temp_val)
264             node->temp_val = curval;
265         else {
266             curval = node->temp_val;
267         }
268 
269         if (node->val >= threshold) {
270             put_bits(s, 0, threshold - curval);
271             curval = threshold;
272         } else {
273             put_bits(s, 0, node->val - curval);
274             curval = node->val;
275             if (!node->vis) {
276                 put_bits(s, 1, 1);
277                 node->vis = 1;
278             }
279         }
280 
281         node->temp_val = curval;
282         if (sp < 0)
283             break;
284         node = stack[sp--];
285     }
286 }
287 
288 /** update the value in node */
tag_tree_update(Jpeg2000TgtNode *node)289 static void tag_tree_update(Jpeg2000TgtNode *node)
290 {
291     while (node->parent){
292         if (node->parent->val <= node->val)
293             break;
294         node->parent->val = node->val;
295         node = node->parent;
296     }
297 }
298 
put_siz(Jpeg2000EncoderContext *s)299 static int put_siz(Jpeg2000EncoderContext *s)
300 {
301     int i;
302 
303     if (s->buf_end - s->buf < 40 + 3 * s->ncomponents)
304         return -1;
305 
306     bytestream_put_be16(&s->buf, JPEG2000_SIZ);
307     bytestream_put_be16(&s->buf, 38 + 3 * s->ncomponents); // Lsiz
308     bytestream_put_be16(&s->buf, 0); // Rsiz
309     bytestream_put_be32(&s->buf, s->width); // width
310     bytestream_put_be32(&s->buf, s->height); // height
311     bytestream_put_be32(&s->buf, 0); // X0Siz
312     bytestream_put_be32(&s->buf, 0); // Y0Siz
313 
314     bytestream_put_be32(&s->buf, s->tile_width); // XTSiz
315     bytestream_put_be32(&s->buf, s->tile_height); // YTSiz
316     bytestream_put_be32(&s->buf, 0); // XT0Siz
317     bytestream_put_be32(&s->buf, 0); // YT0Siz
318     bytestream_put_be16(&s->buf, s->ncomponents); // CSiz
319 
320     for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
321         bytestream_put_byte(&s->buf, s->cbps[i] - 1);
322         bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[0]:1);
323         bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[1]:1);
324     }
325     return 0;
326 }
327 
put_cod(Jpeg2000EncoderContext *s)328 static int put_cod(Jpeg2000EncoderContext *s)
329 {
330     Jpeg2000CodingStyle *codsty = &s->codsty;
331     uint8_t scod = 0;
332 
333     if (s->buf_end - s->buf < 14)
334         return -1;
335 
336     bytestream_put_be16(&s->buf, JPEG2000_COD);
337     bytestream_put_be16(&s->buf, 12); // Lcod
338     if (s->sop)
339         scod |= JPEG2000_CSTY_SOP;
340     if (s->eph)
341         scod |= JPEG2000_CSTY_EPH;
342     bytestream_put_byte(&s->buf, scod);  // Scod
343     // SGcod
344     bytestream_put_byte(&s->buf, s->prog); // progression level
345     bytestream_put_be16(&s->buf, s->nlayers); // num of layers
346     if(s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
347         bytestream_put_byte(&s->buf, 0); // unspecified
348     }else{
349         bytestream_put_byte(&s->buf, 0); // unspecified
350     }
351     // SPcod
352     bytestream_put_byte(&s->buf, codsty->nreslevels - 1); // num of decomp. levels
353     bytestream_put_byte(&s->buf, codsty->log2_cblk_width-2); // cblk width
354     bytestream_put_byte(&s->buf, codsty->log2_cblk_height-2); // cblk height
355     bytestream_put_byte(&s->buf, 0); // cblk style
356     bytestream_put_byte(&s->buf, codsty->transform == FF_DWT53); // transformation
357     return 0;
358 }
359 
put_qcd(Jpeg2000EncoderContext *s, int compno)360 static int put_qcd(Jpeg2000EncoderContext *s, int compno)
361 {
362     int i, size;
363     Jpeg2000CodingStyle *codsty = &s->codsty;
364     Jpeg2000QuantStyle  *qntsty = &s->qntsty;
365 
366     if (qntsty->quantsty == JPEG2000_QSTY_NONE)
367         size = 4 + 3 * (codsty->nreslevels-1);
368     else // QSTY_SE
369         size = 5 + 6 * (codsty->nreslevels-1);
370 
371     if (s->buf_end - s->buf < size + 2)
372         return -1;
373 
374     bytestream_put_be16(&s->buf, JPEG2000_QCD);
375     bytestream_put_be16(&s->buf, size);  // LQcd
376     bytestream_put_byte(&s->buf, (qntsty->nguardbits << 5) | qntsty->quantsty);  // Sqcd
377     if (qntsty->quantsty == JPEG2000_QSTY_NONE)
378         for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
379             bytestream_put_byte(&s->buf, qntsty->expn[i] << 3);
380     else // QSTY_SE
381         for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
382             bytestream_put_be16(&s->buf, (qntsty->expn[i] << 11) | qntsty->mant[i]);
383     return 0;
384 }
385 
put_com(Jpeg2000EncoderContext *s, int compno)386 static int put_com(Jpeg2000EncoderContext *s, int compno)
387 {
388     int size = 4 + strlen(LIBAVCODEC_IDENT);
389 
390     if (s->avctx->flags & AV_CODEC_FLAG_BITEXACT)
391         return 0;
392 
393     if (s->buf_end - s->buf < size + 2)
394         return -1;
395 
396     bytestream_put_be16(&s->buf, JPEG2000_COM);
397     bytestream_put_be16(&s->buf, size);
398     bytestream_put_be16(&s->buf, 1); // General use (ISO/IEC 8859-15 (Latin) values)
399 
400     bytestream_put_buffer(&s->buf, LIBAVCODEC_IDENT, strlen(LIBAVCODEC_IDENT));
401 
402     return 0;
403 }
404 
put_sot(Jpeg2000EncoderContext *s, int tileno)405 static uint8_t *put_sot(Jpeg2000EncoderContext *s, int tileno)
406 {
407     uint8_t *psotptr;
408 
409     if (s->buf_end - s->buf < 12)
410         return NULL;
411 
412     bytestream_put_be16(&s->buf, JPEG2000_SOT);
413     bytestream_put_be16(&s->buf, 10); // Lsot
414     bytestream_put_be16(&s->buf, tileno); // Isot
415 
416     psotptr = s->buf;
417     bytestream_put_be32(&s->buf, 0); // Psot (filled in later)
418 
419     bytestream_put_byte(&s->buf, 0); // TPsot
420     bytestream_put_byte(&s->buf, 1); // TNsot
421     return psotptr;
422 }
423 
compute_rates(Jpeg2000EncoderContext* s)424 static void compute_rates(Jpeg2000EncoderContext* s)
425 {
426     int i, j;
427     int layno, compno;
428     for (i = 0; i < s->numYtiles; i++) {
429         for (j = 0; j < s->numXtiles; j++) {
430             Jpeg2000Tile *tile = &s->tile[s->numXtiles * i + j];
431             for (compno = 0; compno < s->ncomponents; compno++) {
432                 int tilew = tile->comp[compno].coord[0][1] - tile->comp[compno].coord[0][0];
433                 int tileh = tile->comp[compno].coord[1][1] - tile->comp[compno].coord[1][0];
434                 int scale = (compno?1 << s->chroma_shift[0]:1) * (compno?1 << s->chroma_shift[1]:1);
435                 for (layno = 0; layno < s->nlayers; layno++) {
436                     if (s->layer_rates[layno] > 0) {
437                         tile->layer_rates[layno] += (double)(tilew * tileh) * s->ncomponents * s->cbps[compno] /
438                                                     (double)(s->layer_rates[layno] * 8 * scale);
439                     } else {
440                         tile->layer_rates[layno] = 0.0;
441                     }
442                 }
443             }
444         }
445     }
446 
447 }
448 
449 /**
450  * compute the sizes of tiles, resolution levels, bands, etc.
451  * allocate memory for them
452  * divide the input image into tile-components
453  */
init_tiles(Jpeg2000EncoderContext *s)454 static int init_tiles(Jpeg2000EncoderContext *s)
455 {
456     int tileno, tilex, tiley, compno;
457     Jpeg2000CodingStyle *codsty = &s->codsty;
458     Jpeg2000QuantStyle  *qntsty = &s->qntsty;
459 
460     s->numXtiles = ff_jpeg2000_ceildiv(s->width, s->tile_width);
461     s->numYtiles = ff_jpeg2000_ceildiv(s->height, s->tile_height);
462 
463     s->tile = av_calloc(s->numXtiles, s->numYtiles * sizeof(Jpeg2000Tile));
464     if (!s->tile)
465         return AVERROR(ENOMEM);
466     for (tileno = 0, tiley = 0; tiley < s->numYtiles; tiley++)
467         for (tilex = 0; tilex < s->numXtiles; tilex++, tileno++){
468             Jpeg2000Tile *tile = s->tile + tileno;
469 
470             tile->comp = av_calloc(s->ncomponents, sizeof(*tile->comp));
471             if (!tile->comp)
472                 return AVERROR(ENOMEM);
473 
474             tile->layer_rates = av_calloc(s->nlayers, sizeof(*tile->layer_rates));
475             if (!tile->layer_rates)
476                 return AVERROR(ENOMEM);
477 
478             for (compno = 0; compno < s->ncomponents; compno++){
479                 Jpeg2000Component *comp = tile->comp + compno;
480                 int ret, i, j;
481 
482                 comp->coord[0][0] = comp->coord_o[0][0] = tilex * s->tile_width;
483                 comp->coord[0][1] = comp->coord_o[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
484                 comp->coord[1][0] = comp->coord_o[1][0] = tiley * s->tile_height;
485                 comp->coord[1][1] = comp->coord_o[1][1] = FFMIN((tiley+1)*s->tile_height, s->height);
486                 if (compno > 0)
487                     for (i = 0; i < 2; i++)
488                         for (j = 0; j < 2; j++)
489                             comp->coord[i][j] = comp->coord_o[i][j] = ff_jpeg2000_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
490 
491                 if ((ret = ff_jpeg2000_init_component(comp,
492                                                 codsty,
493                                                 qntsty,
494                                                 s->cbps[compno],
495                                                 compno?1<<s->chroma_shift[0]:1,
496                                                 compno?1<<s->chroma_shift[1]:1,
497                                                 s->avctx
498                                                )) < 0)
499                     return ret;
500             }
501         }
502     compute_rates(s);
503     return 0;
504 }
505 
506 #define COPY_FRAME(D, PIXEL)                                                                                                \
507     static void copy_frame_ ##D(Jpeg2000EncoderContext *s)                                                                  \
508     {                                                                                                                       \
509         int tileno, compno, i, y, x;                                                                                        \
510         PIXEL *line;                                                                                                        \
511         for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){                                                   \
512             Jpeg2000Tile *tile = s->tile + tileno;                                                                          \
513             if (s->planar){                                                                                                 \
514                 for (compno = 0; compno < s->ncomponents; compno++){                                                        \
515                     Jpeg2000Component *comp = tile->comp + compno;                                                          \
516                     int *dst = comp->i_data;                                                                                \
517                     int cbps = s->cbps[compno];                                                                             \
518                     line = (PIXEL*)s->picture->data[compno]                                                                 \
519                            + comp->coord[1][0] * (s->picture->linesize[compno] / sizeof(PIXEL))                             \
520                            + comp->coord[0][0];                                                                             \
521                     for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){                                                \
522                         PIXEL *ptr = line;                                                                                  \
523                         for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++)                                             \
524                             *dst++ = *ptr++ - (1 << (cbps - 1));                                                            \
525                         line += s->picture->linesize[compno] / sizeof(PIXEL);                                               \
526                     }                                                                                                       \
527                 }                                                                                                           \
528             } else{                                                                                                         \
529                 line = (PIXEL*)s->picture->data[0] + tile->comp[0].coord[1][0] * (s->picture->linesize[0] / sizeof(PIXEL))  \
530                        + tile->comp[0].coord[0][0] * s->ncomponents;                                                        \
531                                                                                                                             \
532                 i = 0;                                                                                                      \
533                 for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){                                    \
534                     PIXEL *ptr = line;                                                                                      \
535                     for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){                           \
536                         for (compno = 0; compno < s->ncomponents; compno++){                                                \
537                             int cbps = s->cbps[compno];                                                                     \
538                             tile->comp[compno].i_data[i] = *ptr++  - (1 << (cbps - 1));                                     \
539                         }                                                                                                   \
540                     }                                                                                                       \
541                     line += s->picture->linesize[0] / sizeof(PIXEL);                                                        \
542                 }                                                                                                           \
543             }                                                                                                               \
544         }                                                                                                                   \
545     }
546 
547 COPY_FRAME(8, uint8_t)
548 COPY_FRAME(16, uint16_t)
549 
init_quantization(Jpeg2000EncoderContext *s)550 static void init_quantization(Jpeg2000EncoderContext *s)
551 {
552     int compno, reslevelno, bandno;
553     Jpeg2000QuantStyle  *qntsty = &s->qntsty;
554     Jpeg2000CodingStyle *codsty = &s->codsty;
555 
556     for (compno = 0; compno < s->ncomponents; compno++){
557         int gbandno = 0;
558         for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
559             int nbands, lev = codsty->nreslevels - reslevelno - 1;
560             nbands = reslevelno ? 3 : 1;
561             for (bandno = 0; bandno < nbands; bandno++, gbandno++){
562                 int expn, mant = 0;
563 
564                 if (codsty->transform == FF_DWT97_INT){
565                     int bandpos = bandno + (reslevelno>0),
566                         ss = 81920000 / dwt_norms[0][bandpos][lev],
567                         log = av_log2(ss);
568                     mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
569                     expn = s->cbps[compno] - log + 13;
570                 } else
571                     expn = ((bandno&2)>>1) + (reslevelno>0) + s->cbps[compno];
572 
573                 qntsty->expn[gbandno] = expn;
574                 qntsty->mant[gbandno] = mant;
575             }
576         }
577     }
578 }
579 
init_luts(void)580 static void init_luts(void)
581 {
582     int i, a,
583         mask = ~((1<<NMSEDEC_FRACBITS)-1);
584 
585     for (i = 0; i < (1 << NMSEDEC_BITS); i++){
586         lut_nmsedec_sig[i]  = FFMAX((3 * i << (13 - NMSEDEC_FRACBITS)) - (9 << 11), 0);
587         lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
588 
589         a = (i >> (NMSEDEC_BITS-2)&2) + 1;
590         lut_nmsedec_ref[i]  = FFMAX((a - 2) * (i << (13 - NMSEDEC_FRACBITS)) +
591                                     (1 << 13) - (a * a << 11), 0);
592         lut_nmsedec_ref0[i] = FFMAX(((i * i - (i << NMSEDEC_BITS) + (1 << 2 * NMSEDEC_FRACBITS) + (1 << (NMSEDEC_FRACBITS - 1))) & mask)
593                                     << 1, 0);
594     }
595     ff_jpeg2000_init_tier1_luts();
596 }
597 
598 /* tier-1 routines */
getnmsedec_sig(int x, int bpno)599 static int getnmsedec_sig(int x, int bpno)
600 {
601     if (bpno > NMSEDEC_FRACBITS)
602         return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
603     return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
604 }
605 
getnmsedec_ref(int x, int bpno)606 static int getnmsedec_ref(int x, int bpno)
607 {
608     if (bpno > NMSEDEC_FRACBITS)
609         return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
610     return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
611 }
612 
encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)613 static void encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
614 {
615     int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
616     for (y0 = 0; y0 < height; y0 += 4)
617         for (x = 0; x < width; x++)
618             for (y = y0; y < height && y < y0+4; y++){
619                 if (!(t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG) && (t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB)){
620                     int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno),
621                         bit = t1->data[(y) * t1->stride + x] & mask ? 1 : 0;
622                     ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
623                     if (bit){
624                         int xorbit;
625                         int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
626                         ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
627                         *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
628                         ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
629                     }
630                     t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_VIS;
631                 }
632             }
633 }
634 
encode_refpass(Jpeg2000T1Context *t1, int width, int height, int *nmsedec, int bpno)635 static void encode_refpass(Jpeg2000T1Context *t1, int width, int height, int *nmsedec, int bpno)
636 {
637     int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
638     for (y0 = 0; y0 < height; y0 += 4)
639         for (x = 0; x < width; x++)
640             for (y = y0; y < height && y < y0+4; y++)
641                 if ((t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG){
642                     int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y+1) * t1->stride + x+1]);
643                     *nmsedec += getnmsedec_ref(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
644                     ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
645                     t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_REF;
646                 }
647 }
648 
encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)649 static void encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
650 {
651     int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
652     for (y0 = 0; y0 < height; y0 += 4)
653         for (x = 0; x < width; x++){
654             if (y0 + 3 < height && !(
655             (t1->flags[(y0+1) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
656             (t1->flags[(y0+2) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
657             (t1->flags[(y0+3) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
658             (t1->flags[(y0+4) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG))))
659             {
660                 // aggregation mode
661                 int rlen;
662                 for (rlen = 0; rlen < 4; rlen++)
663                     if (t1->data[(y0+rlen) * t1->stride + x] & mask)
664                         break;
665                 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
666                 if (rlen == 4)
667                     continue;
668                 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
669                 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
670                 for (y = y0 + rlen; y < y0 + 4; y++){
671                     if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
672                         int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
673                         if (y > y0 + rlen)
674                             ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
675                         if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
676                             int xorbit;
677                             int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
678                             *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
679                             ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
680                             ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
681                         }
682                     }
683                     t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
684                 }
685             } else{
686                 for (y = y0; y < y0 + 4 && y < height; y++){
687                     if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
688                         int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
689                         ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
690                         if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
691                             int xorbit;
692                             int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
693                             *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
694                             ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
695                             ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
696                         }
697                     }
698                     t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
699                 }
700             }
701         }
702 }
703 
encode_cblk(Jpeg2000EncoderContext *s, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, Jpeg2000Tile *tile, int width, int height, int bandpos, int lev)704 static void encode_cblk(Jpeg2000EncoderContext *s, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, Jpeg2000Tile *tile,
705                         int width, int height, int bandpos, int lev)
706 {
707     int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
708     int64_t wmsedec = 0;
709 
710     memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
711 
712     for (y = 0; y < height; y++){
713         for (x = 0; x < width; x++){
714             if (t1->data[(y) * t1->stride + x] < 0){
715                 t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_SGN;
716                 t1->data[(y) * t1->stride + x] = -t1->data[(y) * t1->stride + x];
717             }
718             max = FFMAX(max, t1->data[(y) * t1->stride + x]);
719         }
720     }
721 
722     if (max == 0){
723         cblk->nonzerobits = 0;
724     } else{
725         cblk->nonzerobits = av_log2(max) + 1 - NMSEDEC_FRACBITS;
726     }
727     bpno = cblk->nonzerobits - 1;
728 
729     cblk->data[0] = 0;
730     ff_mqc_initenc(&t1->mqc, cblk->data + 1);
731 
732     for (passno = 0; bpno >= 0; passno++){
733         nmsedec=0;
734 
735         switch(pass_t){
736             case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
737                     break;
738             case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
739                     break;
740             case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
741                     break;
742         }
743 
744         cblk->passes[passno].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno].flushed, &cblk->passes[passno].flushed_len);
745         cblk->passes[passno].rate -= cblk->passes[passno].flushed_len;
746 
747         wmsedec += (int64_t)nmsedec << (2*bpno);
748         cblk->passes[passno].disto = wmsedec;
749 
750         if (++pass_t == 3){
751             pass_t = 0;
752             bpno--;
753         }
754     }
755     cblk->npasses = passno;
756     cblk->ninclpasses = passno;
757 
758     if (passno) {
759         cblk->passes[passno-1].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno-1].flushed, &cblk->passes[passno-1].flushed_len);
760         cblk->passes[passno-1].rate -= cblk->passes[passno-1].flushed_len;
761     }
762 }
763 
764 /* tier-2 routines: */
765 
putnumpasses(Jpeg2000EncoderContext *s, int n)766 static void putnumpasses(Jpeg2000EncoderContext *s, int n)
767 {
768     if (n == 1)
769         put_num(s, 0, 1);
770     else if (n == 2)
771         put_num(s, 2, 2);
772     else if (n <= 5)
773         put_num(s, 0xc | (n-3), 4);
774     else if (n <= 36)
775         put_num(s, 0x1e0 | (n-6), 9);
776     else
777         put_num(s, 0xff80 | (n-37), 16);
778 }
779 
780 
encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int layno, int precno, uint8_t *expn, int numgbits, int packetno, int nlayers)781 static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int layno,
782                          int precno, uint8_t *expn, int numgbits, int packetno,
783                          int nlayers)
784 {
785     int bandno, empty = 1;
786     int i;
787     // init bitstream
788     *s->buf = 0;
789     s->bit_index = 0;
790 
791     if (s->sop) {
792         bytestream_put_be16(&s->buf, JPEG2000_SOP);
793         bytestream_put_be16(&s->buf, 4);
794         bytestream_put_be16(&s->buf, packetno);
795     }
796     // header
797 
798     if (!layno) {
799         for (bandno = 0; bandno < rlevel->nbands; bandno++) {
800             Jpeg2000Band *band = rlevel->band + bandno;
801             if (band->coord[0][0] < band->coord[0][1]
802             &&  band->coord[1][0] < band->coord[1][1]) {
803                 Jpeg2000Prec *prec = band->prec + precno;
804                 int nb_cblks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
805                 int pos;
806                 ff_tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, prec->nb_codeblocks_height, 99);
807                 ff_tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, prec->nb_codeblocks_height, 99);
808                 for (pos = 0; pos < nb_cblks; pos++) {
809                     Jpeg2000Cblk *cblk = &prec->cblk[pos];
810                     prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - cblk->nonzerobits;
811                     cblk->incl = 0;
812                     cblk->lblock = 3;
813                     tag_tree_update(prec->zerobits + pos);
814                     for (i = 0; i < nlayers; i++) {
815                         if (cblk->layers[i].npasses > 0) {
816                             prec->cblkincl[pos].val = i;
817                             break;
818                         }
819                     }
820                     if (i == nlayers)
821                         prec->cblkincl[pos].val = i;
822                     tag_tree_update(prec->cblkincl + pos);
823                 }
824             }
825         }
826     }
827 
828     // is the packet empty?
829     for (bandno = 0; bandno < rlevel->nbands; bandno++){
830         Jpeg2000Band *band = rlevel->band + bandno;
831         if (band->coord[0][0] < band->coord[0][1]
832         &&  band->coord[1][0] < band->coord[1][1]) {
833             Jpeg2000Prec *prec = band->prec + precno;
834             int nb_cblks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
835             int pos;
836             for (pos = 0; pos < nb_cblks; pos++) {
837                 Jpeg2000Cblk *cblk = &prec->cblk[pos];
838                 if (cblk->layers[layno].npasses) {
839                     empty = 0;
840                     break;
841                 }
842             }
843             if (!empty)
844                 break;
845         }
846     }
847 
848     put_bits(s, !empty, 1);
849     if (empty){
850         j2k_flush(s);
851         if (s->eph)
852             bytestream_put_be16(&s->buf, JPEG2000_EPH);
853         return 0;
854     }
855 
856     for (bandno = 0; bandno < rlevel->nbands; bandno++) {
857         Jpeg2000Band *band = rlevel->band + bandno;
858         Jpeg2000Prec *prec = band->prec + precno;
859         int yi, xi, pos;
860         int cblknw = prec->nb_codeblocks_width;
861 
862         if (band->coord[0][0] == band->coord[0][1]
863         ||  band->coord[1][0] == band->coord[1][1])
864             continue;
865 
866         for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++) {
867             for (xi = 0; xi < cblknw; xi++, pos++){
868                 int llen = 0, length;
869                 Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
870 
871                 if (s->buf_end - s->buf < 20) // approximately
872                     return -1;
873 
874                 // inclusion information
875                 if (!cblk->incl)
876                     tag_tree_code(s, prec->cblkincl + pos, layno + 1);
877                 else {
878                     put_bits(s, cblk->layers[layno].npasses > 0, 1);
879                 }
880 
881                 if (!cblk->layers[layno].npasses)
882                     continue;
883 
884                 // zerobits information
885                 if (!cblk->incl) {
886                     tag_tree_code(s, prec->zerobits + pos, 100);
887                     cblk->incl = 1;
888                 }
889 
890                 // number of passes
891                 putnumpasses(s, cblk->layers[layno].npasses);
892 
893                 length = cblk->layers[layno].data_len;
894                 if (layno == nlayers - 1 && cblk->layers[layno].cum_passes){
895                     length += cblk->passes[cblk->layers[layno].cum_passes-1].flushed_len;
896                 }
897                 if (cblk->lblock + av_log2(cblk->layers[layno].npasses) < av_log2(length) + 1) {
898                     llen = av_log2(length) + 1 - cblk->lblock - av_log2(cblk->layers[layno].npasses);
899                 }
900 
901                 // length of code block
902                 cblk->lblock += llen;
903                 put_bits(s, 1, llen);
904                 put_bits(s, 0, 1);
905                 put_num(s, length, cblk->lblock + av_log2(cblk->layers[layno].npasses));
906             }
907         }
908     }
909     j2k_flush(s);
910     if (s->eph) {
911         bytestream_put_be16(&s->buf, JPEG2000_EPH);
912     }
913 
914     for (bandno = 0; bandno < rlevel->nbands; bandno++) {
915         Jpeg2000Band *band = rlevel->band + bandno;
916         Jpeg2000Prec *prec = band->prec + precno;
917         int yi, cblknw = prec->nb_codeblocks_width;
918         for (yi =0; yi < prec->nb_codeblocks_height; yi++) {
919             int xi;
920             for (xi = 0; xi < cblknw; xi++){
921                 Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
922                 if (cblk->layers[layno].npasses) {
923                     if (s->buf_end - s->buf < cblk->layers[layno].data_len + 2)
924                         return -1;
925                     bytestream_put_buffer(&s->buf, cblk->layers[layno].data_start + 1, cblk->layers[layno].data_len);
926                     if (layno == nlayers - 1 && cblk->layers[layno].cum_passes) {
927                         bytestream_put_buffer(&s->buf, cblk->passes[cblk->layers[layno].cum_passes-1].flushed,
928                                                        cblk->passes[cblk->layers[layno].cum_passes-1].flushed_len);
929                     }
930                 }
931             }
932         }
933     }
934     return 0;
935 }
936 
encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno, int nlayers)937 static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno, int nlayers)
938 {
939     int compno, reslevelno, layno, ret;
940     Jpeg2000CodingStyle *codsty = &s->codsty;
941     Jpeg2000QuantStyle  *qntsty = &s->qntsty;
942     int packetno = 0;
943     int step_x, step_y;
944     int x, y;
945     int tile_coord[2][2];
946     int col = tileno % s->numXtiles;
947     int row = tileno / s->numXtiles;
948 
949     tile_coord[0][0] = col * s->tile_width;
950     tile_coord[0][1] = FFMIN(tile_coord[0][0] + s->tile_width, s->width);
951     tile_coord[1][0] = row * s->tile_height;
952     tile_coord[1][1] = FFMIN(tile_coord[1][0] + s->tile_height, s->height);
953 
954     av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
955     // lay-rlevel-comp-pos progression
956     switch (s->prog) {
957     case JPEG2000_PGOD_LRCP:
958     for (layno = 0; layno < nlayers; layno++) {
959         for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
960             for (compno = 0; compno < s->ncomponents; compno++){
961                 int precno;
962                 Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
963                 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
964                     if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
965                                 qntsty->nguardbits, packetno++, nlayers)) < 0)
966                         return ret;
967                 }
968             }
969         }
970     }
971     break;
972     case JPEG2000_PGOD_RLCP:
973     for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
974         for (layno = 0; layno < nlayers; layno++) {
975             for (compno = 0; compno < s->ncomponents; compno++){
976                 int precno;
977                 Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
978                 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
979                     if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
980                                 qntsty->nguardbits, packetno++, nlayers)) < 0)
981                         return ret;
982                 }
983             }
984         }
985     }
986     break;
987     case JPEG2000_PGOD_RPCL:
988     for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
989         int precno;
990         step_x = 30;
991         step_y = 30;
992         for (compno = 0; compno < s->ncomponents; compno++) {
993             Jpeg2000Component *comp     = tile->comp + compno;
994             if (reslevelno < codsty->nreslevels) {
995                 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
996                 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
997                 step_x = FFMIN(step_x, rlevel->log2_prec_width  + reducedresno);
998                 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
999             }
1000         }
1001 
1002         step_x = 1<<step_x;
1003         step_y = 1<<step_y;
1004         for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
1005             for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
1006                 for (compno = 0; compno < s->ncomponents; compno++) {
1007                     Jpeg2000Component *comp     = tile->comp + compno;
1008                     uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1009                     Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1010                     int log_subsampling[2] = { compno?s->chroma_shift[0]:0, compno?s->chroma_shift[1]:0};
1011                     unsigned prcx, prcy;
1012                     int trx0, try0;
1013 
1014                     trx0 = ff_jpeg2000_ceildivpow2(tile_coord[0][0], log_subsampling[0] + reducedresno);
1015                     try0 = ff_jpeg2000_ceildivpow2(tile_coord[1][0], log_subsampling[1] + reducedresno);
1016 
1017                     if (!(y % ((uint64_t)1 << (reslevel->log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
1018                         (y == tile_coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_height)))))
1019                         continue;
1020 
1021                     if (!(x % ((uint64_t)1 << (reslevel->log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
1022                         (x == tile_coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_width)))))
1023                         continue;
1024 
1025                     // check if a precinct exists
1026                     prcx   = ff_jpeg2000_ceildivpow2(x, log_subsampling[0] + reducedresno) >> reslevel->log2_prec_width;
1027                     prcy   = ff_jpeg2000_ceildivpow2(y, log_subsampling[1] + reducedresno) >> reslevel->log2_prec_height;
1028                     prcx  -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> reslevel->log2_prec_width;
1029                     prcy  -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> reslevel->log2_prec_height;
1030                     precno = prcx + reslevel->num_precincts_x * prcy;
1031 
1032                     if (prcx >= reslevel->num_precincts_x || prcy >= reslevel->num_precincts_y) {
1033                         av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1034                                prcx, prcy, reslevel->num_precincts_x, reslevel->num_precincts_y);
1035                         continue;
1036                     }
1037                     for (layno = 0; layno < nlayers; layno++) {
1038                         if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
1039                                 qntsty->nguardbits, packetno++, nlayers)) < 0)
1040                             return ret;
1041                         }
1042                     }
1043                 }
1044             }
1045     }
1046     break;
1047     case JPEG2000_PGOD_PCRL:
1048         step_x = 32;
1049         step_y = 32;
1050         for (compno = 0; compno < s->ncomponents; compno++) {
1051             Jpeg2000Component *comp     = tile->comp + compno;
1052 
1053             for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1054                 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1055                 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1056                 step_x = FFMIN(step_x, rlevel->log2_prec_width  + reducedresno);
1057                 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1058             }
1059         }
1060         if (step_x >= 31 || step_y >= 31){
1061             avpriv_request_sample(s->avctx, "PCRL with large step");
1062             return AVERROR_PATCHWELCOME;
1063         }
1064         step_x = 1<<step_x;
1065         step_y = 1<<step_y;
1066 
1067         for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
1068             for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
1069                 for (compno = 0; compno < s->ncomponents; compno++) {
1070                     Jpeg2000Component *comp     = tile->comp + compno;
1071                     int log_subsampling[2] = { compno?s->chroma_shift[0]:0, compno?s->chroma_shift[1]:0};
1072 
1073                     for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1074                         unsigned prcx, prcy;
1075                         int precno;
1076                         uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1077                         Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1078                         int trx0, try0;
1079 
1080                         trx0 = ff_jpeg2000_ceildivpow2(tile_coord[0][0], log_subsampling[0] + reducedresno);
1081                         try0 = ff_jpeg2000_ceildivpow2(tile_coord[1][0], log_subsampling[1] + reducedresno);
1082 
1083                         if (!(y % ((uint64_t)1 << (reslevel->log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
1084                             (y == tile_coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_height)))))
1085                             continue;
1086 
1087                         if (!(x % ((uint64_t)1 << (reslevel->log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
1088                             (x == tile_coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_width)))))
1089                             continue;
1090 
1091                         // check if a precinct exists
1092                         prcx   = ff_jpeg2000_ceildivpow2(x, log_subsampling[0] + reducedresno) >> reslevel->log2_prec_width;
1093                         prcy   = ff_jpeg2000_ceildivpow2(y, log_subsampling[1] + reducedresno) >> reslevel->log2_prec_height;
1094                         prcx  -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> reslevel->log2_prec_width;
1095                         prcy  -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> reslevel->log2_prec_height;
1096 
1097                         precno = prcx + reslevel->num_precincts_x * prcy;
1098 
1099                         if (prcx >= reslevel->num_precincts_x || prcy >= reslevel->num_precincts_y) {
1100                             av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1101                                    prcx, prcy, reslevel->num_precincts_x, reslevel->num_precincts_y);
1102                             continue;
1103                         }
1104                         for (layno = 0; layno < nlayers; layno++) {
1105                             if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
1106                                     qntsty->nguardbits, packetno++, nlayers)) < 0)
1107                                 return ret;
1108                         }
1109                     }
1110                 }
1111             }
1112         }
1113     break;
1114     case JPEG2000_PGOD_CPRL:
1115         for (compno = 0; compno < s->ncomponents; compno++) {
1116             Jpeg2000Component *comp     = tile->comp + compno;
1117             int log_subsampling[2] = { compno?s->chroma_shift[0]:0, compno?s->chroma_shift[1]:0};
1118             step_x = 32;
1119             step_y = 32;
1120 
1121             for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1122                 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1123                 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1124                 step_x = FFMIN(step_x, rlevel->log2_prec_width  + reducedresno);
1125                 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1126             }
1127             if (step_x >= 31 || step_y >= 31){
1128                 avpriv_request_sample(s->avctx, "CPRL with large step");
1129                 return AVERROR_PATCHWELCOME;
1130             }
1131             step_x = 1<<step_x;
1132             step_y = 1<<step_y;
1133 
1134             for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
1135                 for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
1136                     for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1137                         unsigned prcx, prcy;
1138                         int precno;
1139                         int trx0, try0;
1140                         uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
1141                         Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1142 
1143                         trx0 = ff_jpeg2000_ceildivpow2(tile_coord[0][0], log_subsampling[0] + reducedresno);
1144                         try0 = ff_jpeg2000_ceildivpow2(tile_coord[1][0], log_subsampling[1] + reducedresno);
1145 
1146                         if (!(y % ((uint64_t)1 << (reslevel->log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
1147                             (y == tile_coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_height)))))
1148                             continue;
1149 
1150                         if (!(x % ((uint64_t)1 << (reslevel->log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
1151                             (x == tile_coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_width)))))
1152                             continue;
1153 
1154                         // check if a precinct exists
1155                         prcx   = ff_jpeg2000_ceildivpow2(x, log_subsampling[0] + reducedresno) >> reslevel->log2_prec_width;
1156                         prcy   = ff_jpeg2000_ceildivpow2(y, log_subsampling[1] + reducedresno) >> reslevel->log2_prec_height;
1157                         prcx  -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> reslevel->log2_prec_width;
1158                         prcy  -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> reslevel->log2_prec_height;
1159 
1160                         precno = prcx + reslevel->num_precincts_x * prcy;
1161 
1162                         if (prcx >= reslevel->num_precincts_x || prcy >= reslevel->num_precincts_y) {
1163                             av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1164                                    prcx, prcy, reslevel->num_precincts_x, reslevel->num_precincts_y);
1165                             continue;
1166                         }
1167                         for (layno = 0; layno < nlayers; layno++) {
1168                             if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
1169                                     qntsty->nguardbits, packetno++, nlayers)) < 0)
1170                                 return ret;
1171                         }
1172                     }
1173                 }
1174             }
1175         }
1176 
1177     }
1178 
1179     av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
1180     return 0;
1181 }
1182 
makelayer(Jpeg2000EncoderContext *s, int layno, double thresh, Jpeg2000Tile* tile, int final)1183 static void makelayer(Jpeg2000EncoderContext *s, int layno, double thresh, Jpeg2000Tile* tile, int final)
1184 {
1185     int compno, resno, bandno, precno, cblkno;
1186     int passno;
1187 
1188     for (compno = 0; compno < s->ncomponents; compno++) {
1189         Jpeg2000Component *comp = &tile->comp[compno];
1190 
1191         for (resno = 0; resno < s->codsty.nreslevels; resno++) {
1192             Jpeg2000ResLevel *reslevel = comp->reslevel + resno;
1193 
1194             for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
1195                 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
1196                     Jpeg2000Band *band = reslevel->band + bandno;
1197                     Jpeg2000Prec *prec = band->prec + precno;
1198 
1199                     for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
1200                         Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1201                         Jpeg2000Layer *layer = &cblk->layers[layno];
1202                         int n;
1203 
1204                         if (layno == 0) {
1205                             cblk->ninclpasses = 0;
1206                         }
1207 
1208                         n = cblk->ninclpasses;
1209 
1210                         if (thresh < 0) {
1211                             n = cblk->npasses;
1212                         } else {
1213                             for (passno = cblk->ninclpasses; passno < cblk->npasses; passno++) {
1214                                 int32_t dr;
1215                                 double dd;
1216                                 Jpeg2000Pass *pass = &cblk->passes[passno];
1217 
1218                                 if (n == 0) {
1219                                     dr = pass->rate;
1220                                     dd = pass->disto;
1221                                 } else {
1222                                     dr = pass->rate - cblk->passes[n - 1].rate;
1223                                     dd = pass->disto - cblk->passes[n-1].disto;
1224                                 }
1225 
1226                                 if (!dr) {
1227                                     if (dd != 0.0) {
1228                                         n = passno + 1;
1229                                     }
1230                                     continue;
1231                                 }
1232 
1233                                 if (thresh - (dd / dr) < DBL_EPSILON)
1234                                     n = passno + 1;
1235                             }
1236                         }
1237                         layer->npasses = n - cblk->ninclpasses;
1238                         layer->cum_passes = n;
1239 
1240                         if (layer->npasses == 0) {
1241                             layer->disto = 0;
1242                             layer->data_len = 0;
1243                             continue;
1244                         }
1245 
1246                         if (cblk->ninclpasses == 0) {
1247                             layer->data_len = cblk->passes[n - 1].rate;
1248                             layer->data_start = cblk->data;
1249                             layer->disto = cblk->passes[n - 1].disto;
1250                         } else {
1251                             layer->data_len = cblk->passes[n - 1].rate - cblk->passes[cblk->ninclpasses - 1].rate;
1252                             layer->data_start = cblk->data + cblk->passes[cblk->ninclpasses - 1].rate;
1253                             layer->disto = cblk->passes[n - 1].disto -
1254                                            cblk->passes[cblk->ninclpasses - 1].disto;
1255                         }
1256                         if (final) {
1257                             cblk->ninclpasses = n;
1258                         }
1259                     }
1260                 }
1261             }
1262         }
1263     }
1264 }
1265 
makelayers(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)1266 static void makelayers(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
1267 {
1268     int precno, compno, reslevelno, bandno, cblkno, lev, passno, layno;
1269     int i;
1270     double min = DBL_MAX;
1271     double max = 0;
1272     double thresh;
1273 
1274     Jpeg2000CodingStyle *codsty = &s->codsty;
1275 
1276     for (compno = 0; compno < s->ncomponents; compno++){
1277         Jpeg2000Component *comp = tile->comp + compno;
1278 
1279         for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
1280             Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1281 
1282             for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
1283                 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
1284                     Jpeg2000Band *band = reslevel->band + bandno;
1285                     Jpeg2000Prec *prec = band->prec + precno;
1286 
1287                     for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
1288                         Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1289                         for (passno = 0; passno < cblk->npasses; passno++) {
1290                             Jpeg2000Pass *pass = &cblk->passes[passno];
1291                             int dr;
1292                             double dd, drslope;
1293 
1294                             if (passno == 0) {
1295                                 dr = (int32_t)pass->rate;
1296                                 dd = pass->disto;
1297                             } else {
1298                                 dr = (int32_t)(pass->rate - cblk->passes[passno - 1].rate);
1299                                 dd = pass->disto - cblk->passes[passno - 1].disto;
1300                             }
1301 
1302                             if (dr <= 0)
1303                                 continue;
1304 
1305                             drslope = dd / dr;
1306                             if (drslope < min)
1307                                 min = drslope;
1308 
1309                             if (drslope > max)
1310                                 max = drslope;
1311                         }
1312                     }
1313                 }
1314             }
1315         }
1316     }
1317 
1318     for (layno = 0; layno < s->nlayers; layno++) {
1319         double lo = min;
1320         double hi = max;
1321         double stable_thresh = 0.0;
1322         double good_thresh = 0.0;
1323         if (!s->layer_rates[layno]) {
1324             good_thresh = -1.0;
1325         } else {
1326             for (i = 0; i < 128; i++) {
1327                 uint8_t *stream_pos = s->buf;
1328                 int ret;
1329                 thresh = (lo + hi) / 2;
1330                 makelayer(s, layno, thresh, tile, 0);
1331                 ret = encode_packets(s, tile, (int)(tile - s->tile), layno + 1);
1332                 memset(stream_pos, 0, s->buf - stream_pos);
1333                 if ((s->buf - stream_pos > ceil(tile->layer_rates[layno])) || ret < 0) {
1334                     lo = thresh;
1335                     s->buf = stream_pos;
1336                     continue;
1337                 }
1338                 hi = thresh;
1339                 stable_thresh = thresh;
1340                 s->buf = stream_pos;
1341             }
1342         }
1343         if (good_thresh >= 0.0)
1344             good_thresh = stable_thresh == 0.0 ? thresh : stable_thresh;
1345         makelayer(s, layno, good_thresh, tile, 1);
1346     }
1347 }
1348 
getcut(Jpeg2000Cblk *cblk, int64_t lambda, int dwt_norm)1349 static int getcut(Jpeg2000Cblk *cblk, int64_t lambda, int dwt_norm)
1350 {
1351     int passno, res = 0;
1352     for (passno = 0; passno < cblk->npasses; passno++){
1353         int dr;
1354         int64_t dd;
1355 
1356         dr = cblk->passes[passno].rate
1357            - (res ? cblk->passes[res-1].rate : 0);
1358         dd = cblk->passes[passno].disto
1359            - (res ? cblk->passes[res-1].disto : 0);
1360 
1361         if (((dd * dwt_norm) >> WMSEDEC_SHIFT) * dwt_norm >= dr * lambda)
1362             res = passno+1;
1363     }
1364     return res;
1365 }
1366 
truncpasses(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)1367 static void truncpasses(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
1368 {
1369     int precno, compno, reslevelno, bandno, cblkno, lev;
1370     Jpeg2000CodingStyle *codsty = &s->codsty;
1371 
1372     for (compno = 0; compno < s->ncomponents; compno++){
1373         Jpeg2000Component *comp = tile->comp + compno;
1374 
1375         for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
1376             Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1377 
1378             for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
1379                 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
1380                     int bandpos = bandno + (reslevelno > 0);
1381                     Jpeg2000Band *band = reslevel->band + bandno;
1382                     Jpeg2000Prec *prec = band->prec + precno;
1383 
1384                     for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
1385                         Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1386 
1387                         cblk->ninclpasses = getcut(cblk, s->lambda,
1388                                 (int64_t)dwt_norms[codsty->transform == FF_DWT53][bandpos][lev] * (int64_t)band->i_stepsize >> 15);
1389                         cblk->layers[0].data_start = cblk->data;
1390                         cblk->layers[0].cum_passes = cblk->ninclpasses;
1391                         cblk->layers[0].npasses = cblk->ninclpasses;
1392                         if (cblk->ninclpasses)
1393                             cblk->layers[0].data_len = cblk->passes[cblk->ninclpasses - 1].rate;
1394                     }
1395                 }
1396             }
1397         }
1398     }
1399 }
1400 
encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)1401 static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
1402 {
1403     int compno, reslevelno, bandno, ret;
1404     Jpeg2000T1Context t1;
1405     Jpeg2000CodingStyle *codsty = &s->codsty;
1406     for (compno = 0; compno < s->ncomponents; compno++){
1407         Jpeg2000Component *comp = s->tile[tileno].comp + compno;
1408 
1409         t1.stride = (1<<codsty->log2_cblk_width) + 2;
1410 
1411         av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
1412         if ((ret = ff_dwt_encode(&comp->dwt, comp->i_data)) < 0)
1413             return ret;
1414         av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
1415 
1416         for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
1417             Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1418 
1419             for (bandno = 0; bandno < reslevel->nbands ; bandno++){
1420                 Jpeg2000Band *band = reslevel->band + bandno;
1421                 Jpeg2000Prec *prec = band->prec; // we support only 1 precinct per band ATM in the encoder
1422                 int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
1423                 yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
1424                 y0 = yy0;
1425                 yy1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[1][0] + 1, band->log2_cblk_height) << band->log2_cblk_height,
1426                             band->coord[1][1]) - band->coord[1][0] + yy0;
1427 
1428                 if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
1429                     continue;
1430 
1431                 bandpos = bandno + (reslevelno > 0);
1432 
1433                 for (cblky = 0; cblky < prec->nb_codeblocks_height; cblky++){
1434                     if (reslevelno == 0 || bandno == 1)
1435                         xx0 = 0;
1436                     else
1437                         xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
1438                     x0 = xx0;
1439                     xx1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[0][0] + 1, band->log2_cblk_width) << band->log2_cblk_width,
1440                                 band->coord[0][1]) - band->coord[0][0] + xx0;
1441 
1442                     for (cblkx = 0; cblkx < prec->nb_codeblocks_width; cblkx++, cblkno++){
1443                         int y, x;
1444                         if (codsty->transform == FF_DWT53){
1445                             for (y = yy0; y < yy1; y++){
1446                                 int *ptr = t1.data + (y-yy0)*t1.stride;
1447                                 for (x = xx0; x < xx1; x++){
1448                                     *ptr++ = comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] * (1 << NMSEDEC_FRACBITS);
1449                                 }
1450                             }
1451                         } else{
1452                             for (y = yy0; y < yy1; y++){
1453                                 int *ptr = t1.data + (y-yy0)*t1.stride;
1454                                 for (x = xx0; x < xx1; x++){
1455                                     *ptr = (comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]);
1456                                     *ptr = (int64_t)*ptr * (int64_t)(16384 * 65536 / band->i_stepsize) >> 15 - NMSEDEC_FRACBITS;
1457                                     ptr++;
1458                                 }
1459                             }
1460                         }
1461                         if (!prec->cblk[cblkno].data)
1462                             prec->cblk[cblkno].data = av_malloc(1 + 8192);
1463                         if (!prec->cblk[cblkno].passes)
1464                             prec->cblk[cblkno].passes = av_malloc_array(JPEG2000_MAX_PASSES, sizeof (*prec->cblk[cblkno].passes));
1465                         if (!prec->cblk[cblkno].data || !prec->cblk[cblkno].passes)
1466                             return AVERROR(ENOMEM);
1467                         encode_cblk(s, &t1, prec->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
1468                                     bandpos, codsty->nreslevels - reslevelno - 1);
1469                         xx0 = xx1;
1470                         xx1 = FFMIN(xx1 + (1 << band->log2_cblk_width), band->coord[0][1] - band->coord[0][0] + x0);
1471                     }
1472                     yy0 = yy1;
1473                     yy1 = FFMIN(yy1 + (1 << band->log2_cblk_height), band->coord[1][1] - band->coord[1][0] + y0);
1474                 }
1475             }
1476         }
1477         av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
1478     }
1479 
1480     av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
1481     if (s->compression_rate_enc)
1482         makelayers(s, tile);
1483     else
1484         truncpasses(s, tile);
1485 
1486     if ((ret = encode_packets(s, tile, tileno, s->nlayers)) < 0)
1487         return ret;
1488     av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
1489     return 0;
1490 }
1491 
cleanup(Jpeg2000EncoderContext *s)1492 static void cleanup(Jpeg2000EncoderContext *s)
1493 {
1494     int tileno, compno;
1495     Jpeg2000CodingStyle *codsty = &s->codsty;
1496 
1497     if (!s->tile)
1498         return;
1499     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1500         if (s->tile[tileno].comp) {
1501             for (compno = 0; compno < s->ncomponents; compno++){
1502                 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
1503                 ff_jpeg2000_cleanup(comp, codsty);
1504             }
1505             av_freep(&s->tile[tileno].comp);
1506         }
1507         av_freep(&s->tile[tileno].layer_rates);
1508     }
1509     av_freep(&s->tile);
1510 }
1511 
reinit(Jpeg2000EncoderContext *s)1512 static void reinit(Jpeg2000EncoderContext *s)
1513 {
1514     int tileno, compno;
1515     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1516         Jpeg2000Tile *tile = s->tile + tileno;
1517         for (compno = 0; compno < s->ncomponents; compno++)
1518             ff_jpeg2000_reinit(tile->comp + compno, &s->codsty);
1519     }
1520 }
1521 
update_size(uint8_t *size, const uint8_t *end)1522 static void update_size(uint8_t *size, const uint8_t *end)
1523 {
1524     AV_WB32(size, end-size);
1525 }
1526 
encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)1527 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
1528                         const AVFrame *pict, int *got_packet)
1529 {
1530     int tileno, ret;
1531     Jpeg2000EncoderContext *s = avctx->priv_data;
1532     uint8_t *chunkstart, *jp2cstart, *jp2hstart;
1533     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
1534 
1535     if ((ret = ff_alloc_packet(avctx, pkt, avctx->width*avctx->height*9 + AV_INPUT_BUFFER_MIN_SIZE)) < 0)
1536         return ret;
1537 
1538     // init:
1539     s->buf = s->buf_start = pkt->data;
1540     s->buf_end = pkt->data + pkt->size;
1541 
1542     s->picture = pict;
1543 
1544     s->lambda = s->picture->quality * LAMBDA_SCALE;
1545 
1546     if (s->cbps[0] > 8)
1547         copy_frame_16(s);
1548     else
1549         copy_frame_8(s);
1550 
1551     reinit(s);
1552 
1553     if (s->format == CODEC_JP2) {
1554         av_assert0(s->buf == pkt->data);
1555 
1556         bytestream_put_be32(&s->buf, 0x0000000C);
1557         bytestream_put_be32(&s->buf, 0x6A502020);
1558         bytestream_put_be32(&s->buf, 0x0D0A870A);
1559 
1560         chunkstart = s->buf;
1561         bytestream_put_be32(&s->buf, 0);
1562         bytestream_put_buffer(&s->buf, "ftyp", 4);
1563         bytestream_put_buffer(&s->buf, "jp2\040\040", 4);
1564         bytestream_put_be32(&s->buf, 0);
1565         bytestream_put_buffer(&s->buf, "jp2\040", 4);
1566         update_size(chunkstart, s->buf);
1567 
1568         jp2hstart = s->buf;
1569         bytestream_put_be32(&s->buf, 0);
1570         bytestream_put_buffer(&s->buf, "jp2h", 4);
1571 
1572         chunkstart = s->buf;
1573         bytestream_put_be32(&s->buf, 0);
1574         bytestream_put_buffer(&s->buf, "ihdr", 4);
1575         bytestream_put_be32(&s->buf, avctx->height);
1576         bytestream_put_be32(&s->buf, avctx->width);
1577         bytestream_put_be16(&s->buf, s->ncomponents);
1578         bytestream_put_byte(&s->buf, s->cbps[0]);
1579         bytestream_put_byte(&s->buf, 7);
1580         bytestream_put_byte(&s->buf, 0);
1581         bytestream_put_byte(&s->buf, 0);
1582         update_size(chunkstart, s->buf);
1583 
1584         chunkstart = s->buf;
1585         bytestream_put_be32(&s->buf, 0);
1586         bytestream_put_buffer(&s->buf, "colr", 4);
1587         bytestream_put_byte(&s->buf, 1);
1588         bytestream_put_byte(&s->buf, 0);
1589         bytestream_put_byte(&s->buf, 0);
1590         if ((desc->flags & AV_PIX_FMT_FLAG_RGB) || avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1591             bytestream_put_be32(&s->buf, 16);
1592         } else if (s->ncomponents == 1) {
1593             bytestream_put_be32(&s->buf, 17);
1594         } else {
1595             bytestream_put_be32(&s->buf, 18);
1596         }
1597         update_size(chunkstart, s->buf);
1598         if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1599             int i;
1600             uint8_t *palette = pict->data[1];
1601             chunkstart = s->buf;
1602             bytestream_put_be32(&s->buf, 0);
1603             bytestream_put_buffer(&s->buf, "pclr", 4);
1604             bytestream_put_be16(&s->buf, AVPALETTE_COUNT);
1605             bytestream_put_byte(&s->buf, 3); // colour channels
1606             bytestream_put_be24(&s->buf, 0x070707); //colour depths
1607             for (i = 0; i < AVPALETTE_COUNT; i++) {
1608                 bytestream_put_be24(&s->buf, HAVE_BIGENDIAN ? AV_RB24(palette + 1) : AV_RL24(palette));
1609                 palette += 4;
1610             }
1611             update_size(chunkstart, s->buf);
1612             chunkstart = s->buf;
1613             bytestream_put_be32(&s->buf, 0);
1614             bytestream_put_buffer(&s->buf, "cmap", 4);
1615             for (i = 0; i < 3; i++) {
1616                 bytestream_put_be16(&s->buf, 0); // component
1617                 bytestream_put_byte(&s->buf, 1); // palette mapping
1618                 bytestream_put_byte(&s->buf, i); // index
1619             }
1620             update_size(chunkstart, s->buf);
1621         }
1622         update_size(jp2hstart, s->buf);
1623 
1624         jp2cstart = s->buf;
1625         bytestream_put_be32(&s->buf, 0);
1626         bytestream_put_buffer(&s->buf, "jp2c", 4);
1627     }
1628 
1629     if (s->buf_end - s->buf < 2)
1630         return -1;
1631     bytestream_put_be16(&s->buf, JPEG2000_SOC);
1632     if ((ret = put_siz(s)) < 0)
1633         return ret;
1634     if ((ret = put_cod(s)) < 0)
1635         return ret;
1636     if ((ret = put_qcd(s, 0)) < 0)
1637         return ret;
1638     if ((ret = put_com(s, 0)) < 0)
1639         return ret;
1640 
1641     for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1642         uint8_t *psotptr;
1643         if (!(psotptr = put_sot(s, tileno)))
1644             return -1;
1645         if (s->buf_end - s->buf < 2)
1646             return -1;
1647         bytestream_put_be16(&s->buf, JPEG2000_SOD);
1648         if ((ret = encode_tile(s, s->tile + tileno, tileno)) < 0)
1649             return ret;
1650         bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
1651     }
1652     if (s->buf_end - s->buf < 2)
1653         return -1;
1654     bytestream_put_be16(&s->buf, JPEG2000_EOC);
1655 
1656     if (s->format == CODEC_JP2)
1657         update_size(jp2cstart, s->buf);
1658 
1659     av_log(s->avctx, AV_LOG_DEBUG, "end\n");
1660     pkt->size = s->buf - s->buf_start;
1661     *got_packet = 1;
1662 
1663     return 0;
1664 }
1665 
parse_layer_rates(Jpeg2000EncoderContext *s)1666 static int parse_layer_rates(Jpeg2000EncoderContext *s)
1667 {
1668     int i;
1669     char *token;
1670     char *saveptr = NULL;
1671     int rate;
1672     int nlayers = 0;
1673     if (!s->lr_str) {
1674         s->nlayers = 1;
1675         s->layer_rates[0] = 0;
1676         s->compression_rate_enc = 0;
1677         return 0;
1678     }
1679 
1680     token = av_strtok(s->lr_str, ",", &saveptr);
1681     if (token && (rate = strtol(token, NULL, 10))) {
1682             s->layer_rates[0] = rate <= 1 ? 0:rate;
1683             nlayers++;
1684     } else {
1685             return AVERROR_INVALIDDATA;
1686     }
1687 
1688     while (1) {
1689         token = av_strtok(NULL, ",", &saveptr);
1690         if (!token)
1691             break;
1692         if (rate = strtol(token, NULL, 10)) {
1693             if (nlayers >= 100) {
1694                 return AVERROR_INVALIDDATA;
1695             }
1696             s->layer_rates[nlayers] = rate <= 1 ? 0:rate;
1697             nlayers++;
1698         } else {
1699             return AVERROR_INVALIDDATA;
1700         }
1701     }
1702 
1703     for (i = 1; i < nlayers; i++) {
1704         if (s->layer_rates[i] >= s->layer_rates[i-1]) {
1705             return AVERROR_INVALIDDATA;
1706         }
1707     }
1708     s->nlayers = nlayers;
1709     s->compression_rate_enc = 1;
1710     return 0;
1711 }
1712 
j2kenc_init(AVCodecContext *avctx)1713 static av_cold int j2kenc_init(AVCodecContext *avctx)
1714 {
1715     static AVOnce init_static_once = AV_ONCE_INIT;
1716     int i, ret;
1717     Jpeg2000EncoderContext *s = avctx->priv_data;
1718     Jpeg2000CodingStyle *codsty = &s->codsty;
1719     Jpeg2000QuantStyle  *qntsty = &s->qntsty;
1720     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
1721 
1722     s->avctx = avctx;
1723     av_log(s->avctx, AV_LOG_DEBUG, "init\n");
1724     if (parse_layer_rates(s)) {
1725         av_log(s, AV_LOG_WARNING, "Layer rates invalid. Encoding with 1 layer based on quality metric.\n");
1726         s->nlayers = 1;
1727         s->layer_rates[0] = 0;
1728         s->compression_rate_enc = 0;
1729     }
1730 
1731     if (avctx->pix_fmt == AV_PIX_FMT_PAL8 && (s->pred != FF_DWT97_INT || s->format != CODEC_JP2)) {
1732         av_log(s->avctx, AV_LOG_WARNING, "Forcing lossless jp2 for pal8\n");
1733         s->pred = 1;
1734         s->format = CODEC_JP2;
1735     }
1736 
1737     // defaults:
1738     // TODO: implement setting non-standard precinct size
1739     memset(codsty->log2_prec_widths , 15, sizeof(codsty->log2_prec_widths ));
1740     memset(codsty->log2_prec_heights, 15, sizeof(codsty->log2_prec_heights));
1741     codsty->nreslevels2decode=
1742     codsty->nreslevels       = 7;
1743     codsty->nlayers          = s->nlayers;
1744     codsty->log2_cblk_width  = 4;
1745     codsty->log2_cblk_height = 4;
1746     codsty->transform        = s->pred ? FF_DWT53 : FF_DWT97_INT;
1747 
1748     qntsty->nguardbits       = 1;
1749 
1750     if ((s->tile_width  & (s->tile_width -1)) ||
1751         (s->tile_height & (s->tile_height-1))) {
1752         av_log(avctx, AV_LOG_WARNING, "Tile dimension not a power of 2\n");
1753     }
1754 
1755     if (codsty->transform == FF_DWT53)
1756         qntsty->quantsty = JPEG2000_QSTY_NONE;
1757     else
1758         qntsty->quantsty = JPEG2000_QSTY_SE;
1759 
1760     s->width = avctx->width;
1761     s->height = avctx->height;
1762 
1763     s->ncomponents = desc->nb_components;
1764     for (i = 0; i < 3; i++) {
1765         s->cbps[i] = desc->comp[i].depth;
1766     }
1767 
1768     if ((desc->flags & AV_PIX_FMT_FLAG_PLANAR) && s->ncomponents > 1) {
1769         s->planar = 1;
1770         ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt,
1771                                                s->chroma_shift, s->chroma_shift + 1);
1772         if (ret)
1773             return ret;
1774     }
1775 
1776     ff_thread_once(&init_static_once, init_luts);
1777 
1778     init_quantization(s);
1779     if ((ret=init_tiles(s)) < 0)
1780         return ret;
1781 
1782     av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
1783 
1784     return 0;
1785 }
1786 
j2kenc_destroy(AVCodecContext *avctx)1787 static int j2kenc_destroy(AVCodecContext *avctx)
1788 {
1789     Jpeg2000EncoderContext *s = avctx->priv_data;
1790 
1791     cleanup(s);
1792     return 0;
1793 }
1794 
1795 // taken from the libopenjpeg wraper so it matches
1796 
1797 #define OFFSET(x) offsetof(Jpeg2000EncoderContext, x)
1798 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1799 static const AVOption options[] = {
1800     { "format",        "Codec Format",      OFFSET(format),        AV_OPT_TYPE_INT,   { .i64 = CODEC_JP2   }, CODEC_J2K, CODEC_JP2,   VE, "format"      },
1801     { "j2k",           NULL,                0,                     AV_OPT_TYPE_CONST, { .i64 = CODEC_J2K   }, 0,         0,           VE, "format"      },
1802     { "jp2",           NULL,                0,                     AV_OPT_TYPE_CONST, { .i64 = CODEC_JP2   }, 0,         0,           VE, "format"      },
1803     { "tile_width",    "Tile Width",        OFFSET(tile_width),    AV_OPT_TYPE_INT,   { .i64 = 256         }, 1,     1<<30,           VE, },
1804     { "tile_height",   "Tile Height",       OFFSET(tile_height),   AV_OPT_TYPE_INT,   { .i64 = 256         }, 1,     1<<30,           VE, },
1805     { "pred",          "DWT Type",          OFFSET(pred),          AV_OPT_TYPE_INT,   { .i64 = 0           }, 0,         1,           VE, "pred"        },
1806     { "dwt97int",      NULL,                0,                     AV_OPT_TYPE_CONST, { .i64 = 0           }, INT_MIN, INT_MAX,       VE, "pred"        },
1807     { "dwt53",         NULL,                0,                     AV_OPT_TYPE_CONST, { .i64 = 1           }, INT_MIN, INT_MAX,       VE, "pred"        },
1808     { "sop",           "SOP marker",        OFFSET(sop),           AV_OPT_TYPE_INT,   { .i64 = 0           }, 0,         1,           VE, },
1809     { "eph",           "EPH marker",        OFFSET(eph),           AV_OPT_TYPE_INT,   { .i64 = 0           }, 0,         1,           VE, },
1810     { "prog",          "Progression Order", OFFSET(prog),          AV_OPT_TYPE_INT,   { .i64 = 0           }, JPEG2000_PGOD_LRCP,         JPEG2000_PGOD_CPRL,           VE, "prog" },
1811     { "lrcp",          NULL,                0,                     AV_OPT_TYPE_CONST,  { .i64 = JPEG2000_PGOD_LRCP }, 0,         0,           VE, "prog" },
1812     { "rlcp",          NULL,                0,                     AV_OPT_TYPE_CONST,  { .i64 = JPEG2000_PGOD_RLCP }, 0,         0,           VE, "prog" },
1813     { "rpcl",          NULL,                0,                     AV_OPT_TYPE_CONST,  { .i64 = JPEG2000_PGOD_RPCL }, 0,         0,           VE, "prog" },
1814     { "pcrl",          NULL,                0,                     AV_OPT_TYPE_CONST,  { .i64 = JPEG2000_PGOD_PCRL }, 0,         0,           VE, "prog" },
1815     { "cprl",          NULL,                0,                     AV_OPT_TYPE_CONST,  { .i64 = JPEG2000_PGOD_CPRL }, 0,         0,           VE, "prog" },
1816     { "layer_rates",   "Layer Rates",       OFFSET(lr_str),        AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VE },
1817     { NULL }
1818 };
1819 
1820 static const AVClass j2k_class = {
1821     .class_name = "jpeg 2000 encoder",
1822     .item_name  = av_default_item_name,
1823     .option     = options,
1824     .version    = LIBAVUTIL_VERSION_INT,
1825 };
1826 
1827 const FFCodec ff_jpeg2000_encoder = {
1828     .p.name         = "jpeg2000",
1829     .p.long_name    = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1830     .p.type         = AVMEDIA_TYPE_VIDEO,
1831     .p.id           = AV_CODEC_ID_JPEG2000,
1832     .priv_data_size = sizeof(Jpeg2000EncoderContext),
1833     .init           = j2kenc_init,
1834     FF_CODEC_ENCODE_CB(encode_frame),
1835     .close          = j2kenc_destroy,
1836     .p.pix_fmts     = (const enum AVPixelFormat[]) {
1837         AV_PIX_FMT_RGB24, AV_PIX_FMT_YUV444P, AV_PIX_FMT_GRAY8,
1838         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
1839         AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
1840         AV_PIX_FMT_PAL8,
1841         AV_PIX_FMT_RGB48, AV_PIX_FMT_GRAY16,
1842         AV_PIX_FMT_NONE
1843     },
1844     .p.priv_class   = &j2k_class,
1845     .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
1846 };
1847