FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/j2kenc.c
Date: 2024-12-12 01:08:13
Exec Total Coverage
Lines: 662 991 66.8%
Functions: 35 37 94.6%
Branches: 378 686 55.1%

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