FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/j2kenc.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 645 987 65.3%
Functions: 34 37 91.9%
Branches: 359 696 51.6%

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/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 */
221 8966043 static void put_bits(Jpeg2000EncoderContext *s, int val, int n) // TODO: optimize
222 {
223
2/2
✓ Branch 0 taken 6458763 times.
✓ Branch 1 taken 8966043 times.
15424806 while (n-- > 0){
224
2/2
✓ Branch 0 taken 792476 times.
✓ Branch 1 taken 5666287 times.
6458763 if (s->bit_index == 8)
225 {
226 792476 s->bit_index = *s->buf == 0xff;
227 792476 *(++s->buf) = 0;
228 }
229 6458763 *s->buf |= val << (7 - s->bit_index++);
230 }
231 8966043 }
232
233 /** put n least significant bits of a number num */
234 761232 static void put_num(Jpeg2000EncoderContext *s, int num, int n)
235 {
236
2/2
✓ Branch 0 taken 4462402 times.
✓ Branch 1 taken 761232 times.
5223634 while(--n >= 0)
237 4462402 put_bits(s, (num >> n) & 1, 1);
238 761232 }
239
240 /** flush the bitstream */
241 27300 static void j2k_flush(Jpeg2000EncoderContext *s)
242 {
243
1/2
✓ Branch 0 taken 27300 times.
✗ Branch 1 not taken.
27300 if (s->bit_index){
244 27300 s->bit_index = 0;
245 27300 s->buf++;
246 }
247 27300 }
248
249 /* tag tree routines */
250
251 /** code the value stored in node */
252 794016 static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold)
253 {
254 Jpeg2000TgtNode *stack[30];
255 794016 int sp = -1, curval = 0;
256
257
2/2
✓ Branch 0 taken 1854337 times.
✓ Branch 1 taken 794016 times.
2648353 while(node->parent){
258 1854337 stack[++sp] = node;
259 1854337 node = node->parent;
260 }
261
262 while (1) {
263
2/2
✓ Branch 0 taken 467648 times.
✓ Branch 1 taken 2180705 times.
2648353 if (curval > node->temp_val)
264 467648 node->temp_val = curval;
265 else {
266 2180705 curval = node->temp_val;
267 }
268
269
2/2
✓ Branch 0 taken 38097 times.
✓ Branch 1 taken 2610256 times.
2648353 if (node->val >= threshold) {
270 38097 put_bits(s, 0, threshold - curval);
271 38097 curval = threshold;
272 } else {
273 2610256 put_bits(s, 0, node->val - curval);
274 2610256 curval = node->val;
275
2/2
✓ Branch 0 taken 1066756 times.
✓ Branch 1 taken 1543500 times.
2610256 if (!node->vis) {
276 1066756 put_bits(s, 1, 1);
277 1066756 node->vis = 1;
278 }
279 }
280
281 2648353 node->temp_val = curval;
282
2/2
✓ Branch 0 taken 794016 times.
✓ Branch 1 taken 1854337 times.
2648353 if (sp < 0)
283 794016 break;
284 1854337 node = stack[sp--];
285 }
286 794016 }
287
288 /** update the value in node */
289 826800 static void tag_tree_update(Jpeg2000TgtNode *node)
290 {
291
2/2
✓ Branch 0 taken 1062010 times.
✓ Branch 1 taken 158490 times.
1220500 while (node->parent){
292
2/2
✓ Branch 0 taken 668310 times.
✓ Branch 1 taken 393700 times.
1062010 if (node->parent->val <= node->val)
293 668310 break;
294 393700 node->parent->val = node->val;
295 393700 node = node->parent;
296 }
297 826800 }
298
299 400 static int put_siz(Jpeg2000EncoderContext *s)
300 {
301 int i;
302
303
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 400 times.
400 if (s->buf_end - s->buf < 40 + 3 * s->ncomponents)
304 return -1;
305
306 400 bytestream_put_be16(&s->buf, JPEG2000_SIZ);
307 400 bytestream_put_be16(&s->buf, 38 + 3 * s->ncomponents); // Lsiz
308 400 bytestream_put_be16(&s->buf, 0); // Rsiz
309 400 bytestream_put_be32(&s->buf, s->width); // width
310 400 bytestream_put_be32(&s->buf, s->height); // height
311 400 bytestream_put_be32(&s->buf, 0); // X0Siz
312 400 bytestream_put_be32(&s->buf, 0); // Y0Siz
313
314 400 bytestream_put_be32(&s->buf, s->tile_width); // XTSiz
315 400 bytestream_put_be32(&s->buf, s->tile_height); // YTSiz
316 400 bytestream_put_be32(&s->buf, 0); // XT0Siz
317 400 bytestream_put_be32(&s->buf, 0); // YT0Siz
318 400 bytestream_put_be16(&s->buf, s->ncomponents); // CSiz
319
320
2/2
✓ Branch 0 taken 1200 times.
✓ Branch 1 taken 400 times.
1600 for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
321 1200 bytestream_put_byte(&s->buf, s->cbps[i] - 1);
322
2/2
✓ Branch 0 taken 800 times.
✓ Branch 1 taken 400 times.
1200 bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[0]:1);
323
2/2
✓ Branch 0 taken 800 times.
✓ Branch 1 taken 400 times.
1200 bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[1]:1);
324 }
325 400 return 0;
326 }
327
328 400 static int put_cod(Jpeg2000EncoderContext *s)
329 {
330 400 Jpeg2000CodingStyle *codsty = &s->codsty;
331 400 uint8_t scod = 0;
332
333
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 400 times.
400 if (s->buf_end - s->buf < 14)
334 return -1;
335
336 400 bytestream_put_be16(&s->buf, JPEG2000_COD);
337 400 bytestream_put_be16(&s->buf, 12); // Lcod
338
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 400 times.
400 if (s->sop)
339 scod |= JPEG2000_CSTY_SOP;
340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 400 times.
400 if (s->eph)
341 scod |= JPEG2000_CSTY_EPH;
342 400 bytestream_put_byte(&s->buf, scod); // Scod
343 // SGcod
344 400 bytestream_put_byte(&s->buf, s->prog); // progression level
345 400 bytestream_put_be16(&s->buf, s->nlayers); // num of layers
346
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 400 times.
400 if(s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
347 bytestream_put_byte(&s->buf, 0); // unspecified
348 }else{
349 400 bytestream_put_byte(&s->buf, 0); // unspecified
350 }
351 // SPcod
352 400 bytestream_put_byte(&s->buf, codsty->nreslevels - 1); // num of decomp. levels
353 400 bytestream_put_byte(&s->buf, codsty->log2_cblk_width-2); // cblk width
354 400 bytestream_put_byte(&s->buf, codsty->log2_cblk_height-2); // cblk height
355 400 bytestream_put_byte(&s->buf, 0); // cblk style
356 400 bytestream_put_byte(&s->buf, codsty->transform == FF_DWT53); // transformation
357 400 return 0;
358 }
359
360 400 static int put_qcd(Jpeg2000EncoderContext *s, int compno)
361 {
362 int i, size;
363 400 Jpeg2000CodingStyle *codsty = &s->codsty;
364 400 Jpeg2000QuantStyle *qntsty = &s->qntsty;
365
366
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 200 times.
400 if (qntsty->quantsty == JPEG2000_QSTY_NONE)
367 200 size = 4 + 3 * (codsty->nreslevels-1);
368 else // QSTY_SE
369 200 size = 5 + 6 * (codsty->nreslevels-1);
370
371
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 400 times.
400 if (s->buf_end - s->buf < size + 2)
372 return -1;
373
374 400 bytestream_put_be16(&s->buf, JPEG2000_QCD);
375 400 bytestream_put_be16(&s->buf, size); // LQcd
376 400 bytestream_put_byte(&s->buf, (qntsty->nguardbits << 5) | qntsty->quantsty); // Sqcd
377
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 200 times.
400 if (qntsty->quantsty == JPEG2000_QSTY_NONE)
378
2/2
✓ Branch 0 taken 3800 times.
✓ Branch 1 taken 200 times.
4000 for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
379 3800 bytestream_put_byte(&s->buf, qntsty->expn[i] << 3);
380 else // QSTY_SE
381
2/2
✓ Branch 0 taken 3800 times.
✓ Branch 1 taken 200 times.
4000 for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
382 3800 bytestream_put_be16(&s->buf, (qntsty->expn[i] << 11) | qntsty->mant[i]);
383 400 return 0;
384 }
385
386 400 static int put_com(Jpeg2000EncoderContext *s, int compno)
387 {
388 400 int size = 4 + strlen(LIBAVCODEC_IDENT);
389
390
1/2
✓ Branch 0 taken 400 times.
✗ Branch 1 not taken.
400 if (s->avctx->flags & AV_CODEC_FLAG_BITEXACT)
391 400 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
405 1300 static uint8_t *put_sot(Jpeg2000EncoderContext *s, int tileno)
406 {
407 uint8_t *psotptr;
408
409
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1300 times.
1300 if (s->buf_end - s->buf < 12)
410 return NULL;
411
412 1300 bytestream_put_be16(&s->buf, JPEG2000_SOT);
413 1300 bytestream_put_be16(&s->buf, 10); // Lsot
414 1300 bytestream_put_be16(&s->buf, tileno); // Isot
415
416 1300 psotptr = s->buf;
417 1300 bytestream_put_be32(&s->buf, 0); // Psot (filled in later)
418
419 1300 bytestream_put_byte(&s->buf, 0); // TPsot
420 1300 bytestream_put_byte(&s->buf, 1); // TNsot
421 1300 return psotptr;
422 }
423
424 8 static void compute_rates(Jpeg2000EncoderContext* s)
425 {
426 int i, j;
427 int layno, compno;
428
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 8 times.
22 for (i = 0; i < s->numYtiles; i++) {
429
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 14 times.
40 for (j = 0; j < s->numXtiles; j++) {
430 26 Jpeg2000Tile *tile = &s->tile[s->numXtiles * i + j];
431
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 26 times.
104 for (compno = 0; compno < s->ncomponents; compno++) {
432 78 int tilew = tile->comp[compno].coord[0][1] - tile->comp[compno].coord[0][0];
433 78 int tileh = tile->comp[compno].coord[1][1] - tile->comp[compno].coord[1][0];
434
4/4
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 52 times.
✓ Branch 3 taken 26 times.
78 int scale = (compno?1 << s->chroma_shift[0]:1) * (compno?1 << s->chroma_shift[1]:1);
435
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 78 times.
156 for (layno = 0; layno < s->nlayers; layno++) {
436
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 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 78 tile->layer_rates[layno] = 0.0;
441 }
442 }
443 }
444 }
445 }
446
447 8 }
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 */
454 8 static int init_tiles(Jpeg2000EncoderContext *s)
455 {
456 int tileno, tilex, tiley, compno;
457 8 Jpeg2000CodingStyle *codsty = &s->codsty;
458 8 Jpeg2000QuantStyle *qntsty = &s->qntsty;
459
460 8 s->numXtiles = ff_jpeg2000_ceildiv(s->width, s->tile_width);
461 8 s->numYtiles = ff_jpeg2000_ceildiv(s->height, s->tile_height);
462
463 8 s->tile = av_calloc(s->numXtiles, s->numYtiles * sizeof(Jpeg2000Tile));
464
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!s->tile)
465 return AVERROR(ENOMEM);
466
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 8 times.
22 for (tileno = 0, tiley = 0; tiley < s->numYtiles; tiley++)
467
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 14 times.
40 for (tilex = 0; tilex < s->numXtiles; tilex++, tileno++){
468 26 Jpeg2000Tile *tile = s->tile + tileno;
469
470 26 tile->comp = av_calloc(s->ncomponents, sizeof(*tile->comp));
471
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (!tile->comp)
472 return AVERROR(ENOMEM);
473
474 26 tile->layer_rates = av_calloc(s->nlayers, sizeof(*tile->layer_rates));
475
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (!tile->layer_rates)
476 return AVERROR(ENOMEM);
477
478
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 26 times.
104 for (compno = 0; compno < s->ncomponents; compno++){
479 78 Jpeg2000Component *comp = tile->comp + compno;
480 int ret, i, j;
481
482 78 comp->coord[0][0] = comp->coord_o[0][0] = tilex * s->tile_width;
483 78 comp->coord[0][1] = comp->coord_o[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
484 78 comp->coord[1][0] = comp->coord_o[1][0] = tiley * s->tile_height;
485 78 comp->coord[1][1] = comp->coord_o[1][1] = FFMIN((tiley+1)*s->tile_height, s->height);
486
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 26 times.
78 if (compno > 0)
487
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 52 times.
156 for (i = 0; i < 2; i++)
488
2/2
✓ Branch 0 taken 208 times.
✓ Branch 1 taken 104 times.
312 for (j = 0; j < 2; j++)
489 208 comp->coord[i][j] = comp->coord_o[i][j] = ff_jpeg2000_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
490
491
5/6
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 52 times.
✓ Branch 3 taken 26 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 78 times.
208 if ((ret = ff_jpeg2000_init_component(comp,
492 codsty,
493 qntsty,
494 78 s->cbps[compno],
495 52 compno?1<<s->chroma_shift[0]:1,
496 52 compno?1<<s->chroma_shift[1]:1,
497 s->avctx
498 )) < 0)
499 return ret;
500 }
501 }
502 8 compute_rates(s);
503 8 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 const 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 = (const 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 const 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 = (const PIXEL*)(s->picture->data[0] + tile->comp[0].coord[1][0] * s->picture->linesize[0]) \
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 const 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
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)
548 COPY_FRAME(16, uint16_t)
549
550 8 static void init_quantization(Jpeg2000EncoderContext *s)
551 {
552 int compno, reslevelno, bandno;
553 8 Jpeg2000QuantStyle *qntsty = &s->qntsty;
554 8 Jpeg2000CodingStyle *codsty = &s->codsty;
555
556
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 8 times.
32 for (compno = 0; compno < s->ncomponents; compno++){
557 24 int gbandno = 0;
558
2/2
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 24 times.
192 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
559 168 int nbands, lev = codsty->nreslevels - reslevelno - 1;
560
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 24 times.
168 nbands = reslevelno ? 3 : 1;
561
2/2
✓ Branch 0 taken 456 times.
✓ Branch 1 taken 168 times.
624 for (bandno = 0; bandno < nbands; bandno++, gbandno++){
562 456 int expn, mant = 0;
563
564
2/2
✓ Branch 0 taken 228 times.
✓ Branch 1 taken 228 times.
456 if (codsty->transform == FF_DWT97_INT){
565 228 int bandpos = bandno + (reslevelno>0),
566 228 ss = 81920000 / dwt_norms[0][bandpos][lev],
567 228 log = av_log2(ss);
568
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 228 times.
228 mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
569 228 expn = s->cbps[compno] - log + 13;
570 } else
571 228 expn = ((bandno&2)>>1) + (reslevelno>0) + s->cbps[compno];
572
573 456 qntsty->expn[gbandno] = expn;
574 456 qntsty->mant[gbandno] = mant;
575 }
576 }
577 }
578 8 }
579
580 8 static void init_luts(void)
581 {
582 int i, a,
583 8 mask = ~((1<<NMSEDEC_FRACBITS)-1);
584
585
2/2
✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 8 times.
1032 for (i = 0; i < (1 << NMSEDEC_BITS); i++){
586 1024 lut_nmsedec_sig[i] = FFMAX((3 * i << (13 - NMSEDEC_FRACBITS)) - (9 << 11), 0);
587 1024 lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
588
589 1024 a = (i >> (NMSEDEC_BITS-2)&2) + 1;
590 1024 lut_nmsedec_ref[i] = FFMAX((a - 2) * (i << (13 - NMSEDEC_FRACBITS)) +
591 (1 << 13) - (a * a << 11), 0);
592 1024 lut_nmsedec_ref0[i] = FFMAX(((i * i - (i << NMSEDEC_BITS) + (1 << 2 * NMSEDEC_FRACBITS) + (1 << (NMSEDEC_FRACBITS - 1))) & mask)
593 << 1, 0);
594 }
595 8 ff_jpeg2000_init_tier1_luts();
596 8 }
597
598 /* tier-1 routines */
599 73779107 static int getnmsedec_sig(int x, int bpno)
600 {
601
2/2
✓ Branch 0 taken 57508511 times.
✓ Branch 1 taken 16270596 times.
73779107 if (bpno > NMSEDEC_FRACBITS)
602 57508511 return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
603 16270596 return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
604 }
605
606 155371831 static int getnmsedec_ref(int x, int bpno)
607 {
608
2/2
✓ Branch 0 taken 97863320 times.
✓ Branch 1 taken 57508511 times.
155371831 if (bpno > NMSEDEC_FRACBITS)
609 97863320 return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
610 57508511 return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
611 }
612
613 2341659 static void encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
614 {
615 2341659 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
616
2/2
✓ Branch 0 taken 8300904 times.
✓ Branch 1 taken 2341659 times.
10642563 for (y0 = 0; y0 < height; y0 += 4)
617
2/2
✓ Branch 0 taken 125381407 times.
✓ Branch 1 taken 8300904 times.
133682311 for (x = 0; x < width; x++)
618
4/4
✓ Branch 0 taken 591048536 times.
✓ Branch 1 taken 33618876 times.
✓ Branch 2 taken 499286005 times.
✓ Branch 3 taken 91762531 times.
624667412 for (y = y0; y < height && y < y0+4; y++){
619
4/4
✓ Branch 0 taken 343914174 times.
✓ Branch 1 taken 155371831 times.
✓ Branch 2 taken 163633824 times.
✓ Branch 3 taken 180280350 times.
499286005 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 163633824 int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno),
621 163633824 bit = t1->data[(y) * t1->stride + x] & mask ? 1 : 0;
622 163633824 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
623
2/2
✓ Branch 0 taken 59386202 times.
✓ Branch 1 taken 104247622 times.
163633824 if (bit){
624 int xorbit;
625 59386202 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
626 59386202 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
627 59386202 *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
628 59386202 ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
629 }
630 163633824 t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_VIS;
631 }
632 }
633 2341659 }
634
635 2341659 static void encode_refpass(Jpeg2000T1Context *t1, int width, int height, int *nmsedec, int bpno)
636 {
637 2341659 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
638
2/2
✓ Branch 0 taken 8300904 times.
✓ Branch 1 taken 2341659 times.
10642563 for (y0 = 0; y0 < height; y0 += 4)
639
2/2
✓ Branch 0 taken 125381407 times.
✓ Branch 1 taken 8300904 times.
133682311 for (x = 0; x < width; x++)
640
4/4
✓ Branch 0 taken 591048536 times.
✓ Branch 1 taken 33618876 times.
✓ Branch 2 taken 499286005 times.
✓ Branch 3 taken 91762531 times.
624667412 for (y = y0; y < height && y < y0+4; y++)
641
2/2
✓ Branch 0 taken 155371831 times.
✓ Branch 1 taken 343914174 times.
499286005 if ((t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG){
642 155371831 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y+1) * t1->stride + x+1]);
643 155371831 *nmsedec += getnmsedec_ref(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
644 155371831 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
645 155371831 t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_REF;
646 }
647 2341659 }
648
649 2755058 static void encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
650 {
651 2755058 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
652
2/2
✓ Branch 0 taken 9803903 times.
✓ Branch 1 taken 2755058 times.
12558961 for (y0 = 0; y0 < height; y0 += 4)
653
2/2
✓ Branch 0 taken 148353906 times.
✓ Branch 1 taken 9803903 times.
158157809 for (x = 0; x < width; x++){
654
2/2
✓ Branch 0 taken 147328814 times.
✓ Branch 1 taken 1025092 times.
148353906 if (y0 + 3 < height && !(
655
2/2
✓ Branch 0 taken 57872409 times.
✓ Branch 1 taken 89456405 times.
147328814 (t1->flags[(y0+1) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
656
2/2
✓ Branch 0 taken 52649523 times.
✓ Branch 1 taken 5222886 times.
57872409 (t1->flags[(y0+2) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
657
2/2
✓ Branch 0 taken 48304510 times.
✓ Branch 1 taken 4345013 times.
52649523 (t1->flags[(y0+3) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
658
2/2
✓ Branch 0 taken 46139132 times.
✓ Branch 1 taken 2165378 times.
48304510 (t1->flags[(y0+4) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG))))
659 3619464 {
660 // aggregation mode
661 int rlen;
662
2/2
✓ Branch 0 taken 179323436 times.
✓ Branch 1 taken 42519668 times.
221843104 for (rlen = 0; rlen < 4; rlen++)
663
2/2
✓ Branch 0 taken 3619464 times.
✓ Branch 1 taken 175703972 times.
179323436 if (t1->data[(y0+rlen) * t1->stride + x] & mask)
664 3619464 break;
665 46139132 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
666
2/2
✓ Branch 0 taken 42519668 times.
✓ Branch 1 taken 3619464 times.
46139132 if (rlen == 4)
667 42519668 continue;
668 3619464 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
669 3619464 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
670
2/2
✓ Branch 0 taken 8852556 times.
✓ Branch 1 taken 3619464 times.
12472020 for (y = y0 + rlen; y < y0 + 4; y++){
671
1/2
✓ Branch 0 taken 8852556 times.
✗ Branch 1 not taken.
8852556 if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
672 8852556 int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
673
2/2
✓ Branch 0 taken 5233092 times.
✓ Branch 1 taken 3619464 times.
8852556 if (y > y0 + rlen)
674 5233092 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
675
2/2
✓ Branch 0 taken 4325058 times.
✓ Branch 1 taken 4527498 times.
8852556 if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
676 int xorbit;
677 4325058 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
678 4325058 *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
679 4325058 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
680 4325058 ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
681 }
682 }
683 8852556 t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
684 }
685 } else{
686
4/4
✓ Branch 0 taken 407339768 times.
✓ Branch 1 taken 101189682 times.
✓ Branch 2 taken 406314676 times.
✓ Branch 3 taken 1025092 times.
508529450 for (y = y0; y < y0 + 4 && y < height; y++){
687
2/2
✓ Branch 0 taken 87309021 times.
✓ Branch 1 taken 319005655 times.
406314676 if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
688 87309021 int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
689 87309021 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
690
2/2
✓ Branch 0 taken 10067847 times.
✓ Branch 1 taken 77241174 times.
87309021 if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
691 int xorbit;
692 10067847 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
693 10067847 *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
694 10067847 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
695 10067847 ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
696 }
697 }
698 406314676 t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
699 }
700 }
701 }
702 2755058 }
703
704 413400 static void encode_cblk(Jpeg2000EncoderContext *s, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, Jpeg2000Tile *tile,
705 int width, int height, int bandpos, int lev)
706 {
707 413400 int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
708 413400 int64_t wmsedec = 0;
709
710 413400 memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
711
712
2/2
✓ Branch 0 taken 5959500 times.
✓ Branch 1 taken 413400 times.
6372900 for (y = 0; y < height; y++){
713
2/2
✓ Branch 0 taken 91585200 times.
✓ Branch 1 taken 5959500 times.
97544700 for (x = 0; x < width; x++){
714
2/2
✓ Branch 0 taken 35748311 times.
✓ Branch 1 taken 55836889 times.
91585200 if (t1->data[(y) * t1->stride + x] < 0){
715 35748311 t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_SGN;
716 35748311 t1->data[(y) * t1->stride + x] = -t1->data[(y) * t1->stride + x];
717 }
718 91585200 max = FFMAX(max, t1->data[(y) * t1->stride + x]);
719 }
720 }
721
722
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 413349 times.
413400 if (max == 0){
723 51 cblk->nonzerobits = 0;
724 51 bpno = 0;
725 } else{
726 413349 cblk->nonzerobits = av_log2(max) + 1 - NMSEDEC_FRACBITS;
727 413349 bpno = cblk->nonzerobits - 1;
728 }
729
730 413400 cblk->data[0] = 0;
731 413400 ff_mqc_initenc(&t1->mqc, cblk->data + 1);
732
733
2/2
✓ Branch 0 taken 7438376 times.
✓ Branch 1 taken 413400 times.
7851776 for (passno = 0; bpno >= 0; passno++){
734 7438376 nmsedec=0;
735
736
3/4
✓ Branch 0 taken 2341659 times.
✓ Branch 1 taken 2341659 times.
✓ Branch 2 taken 2755058 times.
✗ Branch 3 not taken.
7438376 switch(pass_t){
737 2341659 case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
738 2341659 break;
739 2341659 case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
740 2341659 break;
741 2755058 case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
742 2755058 break;
743 }
744
745 7438376 cblk->passes[passno].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno].flushed, &cblk->passes[passno].flushed_len);
746 7438376 cblk->passes[passno].rate -= cblk->passes[passno].flushed_len;
747
748 7438376 wmsedec += (int64_t)nmsedec << (2*bpno);
749 7438376 cblk->passes[passno].disto = wmsedec;
750
751
2/2
✓ Branch 0 taken 2755058 times.
✓ Branch 1 taken 4683318 times.
7438376 if (++pass_t == 3){
752 2755058 pass_t = 0;
753 2755058 bpno--;
754 }
755 }
756 413400 cblk->npasses = passno;
757 413400 cblk->ninclpasses = passno;
758
759
2/2
✓ Branch 0 taken 413399 times.
✓ Branch 1 taken 1 times.
413400 if (passno) {
760 413399 cblk->passes[passno-1].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno-1].flushed, &cblk->passes[passno-1].flushed_len);
761 413399 cblk->passes[passno-1].rate -= cblk->passes[passno-1].flushed_len;
762 }
763 413400 }
764
765 /* tier-2 routines: */
766
767 380616 static void putnumpasses(Jpeg2000EncoderContext *s, int n)
768 {
769
2/2
✓ Branch 0 taken 45405 times.
✓ Branch 1 taken 335211 times.
380616 if (n == 1)
770 45405 put_num(s, 0, 1);
771
2/2
✓ Branch 0 taken 13331 times.
✓ Branch 1 taken 321880 times.
335211 else if (n == 2)
772 13331 put_num(s, 2, 2);
773
2/2
✓ Branch 0 taken 111816 times.
✓ Branch 1 taken 210064 times.
321880 else if (n <= 5)
774 111816 put_num(s, 0xc | (n-3), 4);
775
2/2
✓ Branch 0 taken 210061 times.
✓ Branch 1 taken 3 times.
210064 else if (n <= 36)
776 210061 put_num(s, 0x1e0 | (n-6), 9);
777 else
778 3 put_num(s, 0xff80 | (n-37), 16);
779 380616 }
780
781
782 27300 static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int layno,
783 int precno, uint8_t *expn, int numgbits, int packetno,
784 int nlayers)
785 {
786 27300 int bandno, empty = 1;
787 int i;
788 // init bitstream
789 27300 *s->buf = 0;
790 27300 s->bit_index = 0;
791
792
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27300 times.
27300 if (s->sop) {
793 bytestream_put_be16(&s->buf, JPEG2000_SOP);
794 bytestream_put_be16(&s->buf, 4);
795 bytestream_put_be16(&s->buf, packetno);
796 }
797 // header
798
799
1/2
✓ Branch 0 taken 27300 times.
✗ Branch 1 not taken.
27300 if (!layno) {
800
2/2
✓ Branch 0 taken 74100 times.
✓ Branch 1 taken 27300 times.
101400 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
801 74100 Jpeg2000Band *band = rlevel->band + bandno;
802
1/2
✓ Branch 0 taken 74100 times.
✗ Branch 1 not taken.
74100 if (band->coord[0][0] < band->coord[0][1]
803
2/2
✓ Branch 0 taken 70500 times.
✓ Branch 1 taken 3600 times.
74100 && band->coord[1][0] < band->coord[1][1]) {
804 70500 Jpeg2000Prec *prec = band->prec + precno;
805 70500 int nb_cblks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
806 int pos;
807 70500 ff_tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, prec->nb_codeblocks_height, 99);
808 70500 ff_tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, prec->nb_codeblocks_height, 99);
809
2/2
✓ Branch 0 taken 413400 times.
✓ Branch 1 taken 70500 times.
483900 for (pos = 0; pos < nb_cblks; pos++) {
810 413400 Jpeg2000Cblk *cblk = &prec->cblk[pos];
811 413400 prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - cblk->nonzerobits;
812 413400 cblk->incl = 0;
813 413400 cblk->lblock = 3;
814 413400 tag_tree_update(prec->zerobits + pos);
815
2/2
✓ Branch 0 taken 413400 times.
✓ Branch 1 taken 32784 times.
446184 for (i = 0; i < nlayers; i++) {
816
2/2
✓ Branch 0 taken 380616 times.
✓ Branch 1 taken 32784 times.
413400 if (cblk->layers[i].npasses > 0) {
817 380616 prec->cblkincl[pos].val = i;
818 380616 break;
819 }
820 }
821
2/2
✓ Branch 0 taken 32784 times.
✓ Branch 1 taken 380616 times.
413400 if (i == nlayers)
822 32784 prec->cblkincl[pos].val = i;
823 413400 tag_tree_update(prec->cblkincl + pos);
824 }
825 }
826 }
827 }
828
829 // is the packet empty?
830
1/2
✓ Branch 0 taken 27309 times.
✗ Branch 1 not taken.
27309 for (bandno = 0; bandno < rlevel->nbands; bandno++){
831 27309 Jpeg2000Band *band = rlevel->band + bandno;
832
1/2
✓ Branch 0 taken 27309 times.
✗ Branch 1 not taken.
27309 if (band->coord[0][0] < band->coord[0][1]
833
1/2
✓ Branch 0 taken 27309 times.
✗ Branch 1 not taken.
27309 && band->coord[1][0] < band->coord[1][1]) {
834 27309 Jpeg2000Prec *prec = band->prec + precno;
835 27309 int nb_cblks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
836 int pos;
837
2/2
✓ Branch 0 taken 27554 times.
✓ Branch 1 taken 9 times.
27563 for (pos = 0; pos < nb_cblks; pos++) {
838 27554 Jpeg2000Cblk *cblk = &prec->cblk[pos];
839
2/2
✓ Branch 0 taken 27300 times.
✓ Branch 1 taken 254 times.
27554 if (cblk->layers[layno].npasses) {
840 27300 empty = 0;
841 27300 break;
842 }
843 }
844
2/2
✓ Branch 0 taken 27300 times.
✓ Branch 1 taken 9 times.
27309 if (!empty)
845 27300 break;
846 }
847 }
848
849 27300 put_bits(s, !empty, 1);
850
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27300 times.
27300 if (empty){
851 j2k_flush(s);
852 if (s->eph)
853 bytestream_put_be16(&s->buf, JPEG2000_EPH);
854 return 0;
855 }
856
857
2/2
✓ Branch 0 taken 74100 times.
✓ Branch 1 taken 27300 times.
101400 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
858 74100 Jpeg2000Band *band = rlevel->band + bandno;
859 74100 Jpeg2000Prec *prec = band->prec + precno;
860 int yi, xi, pos;
861 74100 int cblknw = prec->nb_codeblocks_width;
862
863
1/2
✓ Branch 0 taken 74100 times.
✗ Branch 1 not taken.
74100 if (band->coord[0][0] == band->coord[0][1]
864
2/2
✓ Branch 0 taken 3600 times.
✓ Branch 1 taken 70500 times.
74100 || band->coord[1][0] == band->coord[1][1])
865 3600 continue;
866
867
2/2
✓ Branch 0 taken 130800 times.
✓ Branch 1 taken 70500 times.
201300 for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++) {
868
2/2
✓ Branch 0 taken 413400 times.
✓ Branch 1 taken 130800 times.
544200 for (xi = 0; xi < cblknw; xi++, pos++){
869 413400 int llen = 0, length;
870 413400 Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
871
872
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 413400 times.
413400 if (s->buf_end - s->buf < 20) // approximately
873 return -1;
874
875 // inclusion information
876
1/2
✓ Branch 0 taken 413400 times.
✗ Branch 1 not taken.
413400 if (!cblk->incl)
877 413400 tag_tree_code(s, prec->cblkincl + pos, layno + 1);
878 else {
879 put_bits(s, cblk->layers[layno].npasses > 0, 1);
880 }
881
882
2/2
✓ Branch 0 taken 32784 times.
✓ Branch 1 taken 380616 times.
413400 if (!cblk->layers[layno].npasses)
883 32784 continue;
884
885 // zerobits information
886
1/2
✓ Branch 0 taken 380616 times.
✗ Branch 1 not taken.
380616 if (!cblk->incl) {
887 380616 tag_tree_code(s, prec->zerobits + pos, 100);
888 380616 cblk->incl = 1;
889 }
890
891 // number of passes
892 380616 putnumpasses(s, cblk->layers[layno].npasses);
893
894 380616 length = cblk->layers[layno].data_len;
895
2/4
✓ Branch 0 taken 380616 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 380616 times.
✗ Branch 3 not taken.
380616 if (layno == nlayers - 1 && cblk->layers[layno].cum_passes){
896 380616 length += cblk->passes[cblk->layers[layno].cum_passes-1].flushed_len;
897 }
898
2/2
✓ Branch 0 taken 113187 times.
✓ Branch 1 taken 267429 times.
380616 if (cblk->lblock + av_log2(cblk->layers[layno].npasses) < av_log2(length) + 1) {
899 113187 llen = av_log2(length) + 1 - cblk->lblock - av_log2(cblk->layers[layno].npasses);
900 }
901
902 // length of code block
903 380616 cblk->lblock += llen;
904 380616 put_bits(s, 1, llen);
905 380616 put_bits(s, 0, 1);
906 380616 put_num(s, length, cblk->lblock + av_log2(cblk->layers[layno].npasses));
907 }
908 }
909 }
910 27300 j2k_flush(s);
911
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27300 times.
27300 if (s->eph) {
912 bytestream_put_be16(&s->buf, JPEG2000_EPH);
913 }
914
915
2/2
✓ Branch 0 taken 74100 times.
✓ Branch 1 taken 27300 times.
101400 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
916 74100 Jpeg2000Band *band = rlevel->band + bandno;
917 74100 Jpeg2000Prec *prec = band->prec + precno;
918 74100 int yi, cblknw = prec->nb_codeblocks_width;
919
2/2
✓ Branch 0 taken 134400 times.
✓ Branch 1 taken 74100 times.
208500 for (yi =0; yi < prec->nb_codeblocks_height; yi++) {
920 int xi;
921
2/2
✓ Branch 0 taken 417000 times.
✓ Branch 1 taken 134400 times.
551400 for (xi = 0; xi < cblknw; xi++){
922 417000 Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
923
2/2
✓ Branch 0 taken 380616 times.
✓ Branch 1 taken 36384 times.
417000 if (cblk->layers[layno].npasses) {
924
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 380616 times.
380616 if (s->buf_end - s->buf < cblk->layers[layno].data_len + 2)
925 return -1;
926 380616 bytestream_put_buffer(&s->buf, cblk->layers[layno].data_start + 1, cblk->layers[layno].data_len);
927
2/4
✓ Branch 0 taken 380616 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 380616 times.
✗ Branch 3 not taken.
380616 if (layno == nlayers - 1 && cblk->layers[layno].cum_passes) {
928 380616 bytestream_put_buffer(&s->buf, cblk->passes[cblk->layers[layno].cum_passes-1].flushed,
929 380616 cblk->passes[cblk->layers[layno].cum_passes-1].flushed_len);
930 }
931 }
932 }
933 }
934 }
935 27300 return 0;
936 }
937
938 1300 static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno, int nlayers)
939 {
940 int compno, reslevelno, layno, ret;
941 1300 Jpeg2000CodingStyle *codsty = &s->codsty;
942 1300 Jpeg2000QuantStyle *qntsty = &s->qntsty;
943 1300 int packetno = 0;
944 int step_x, step_y;
945 int x, y;
946 int tile_coord[2][2];
947 1300 int col = tileno % s->numXtiles;
948 1300 int row = tileno / s->numXtiles;
949
950 1300 tile_coord[0][0] = col * s->tile_width;
951 1300 tile_coord[0][1] = FFMIN(tile_coord[0][0] + s->tile_width, s->width);
952 1300 tile_coord[1][0] = row * s->tile_height;
953 1300 tile_coord[1][1] = FFMIN(tile_coord[1][0] + s->tile_height, s->height);
954
955 1300 av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
956 // lay-rlevel-comp-pos progression
957
1/6
✓ Branch 0 taken 1300 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1300 switch (s->prog) {
958 1300 case JPEG2000_PGOD_LRCP:
959
2/2
✓ Branch 0 taken 1300 times.
✓ Branch 1 taken 1300 times.
2600 for (layno = 0; layno < nlayers; layno++) {
960
2/2
✓ Branch 0 taken 9100 times.
✓ Branch 1 taken 1300 times.
10400 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
961
2/2
✓ Branch 0 taken 27300 times.
✓ Branch 1 taken 9100 times.
36400 for (compno = 0; compno < s->ncomponents; compno++){
962 int precno;
963 27300 Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
964
2/2
✓ Branch 0 taken 27300 times.
✓ Branch 1 taken 27300 times.
54600 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
965
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 27300 times.
27300 if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
966
2/2
✓ Branch 0 taken 23400 times.
✓ Branch 1 taken 3900 times.
27300 qntsty->nguardbits, packetno++, nlayers)) < 0)
967 return ret;
968 }
969 }
970 }
971 }
972 1300 break;
973 case JPEG2000_PGOD_RLCP:
974 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
975 for (layno = 0; layno < nlayers; layno++) {
976 for (compno = 0; compno < s->ncomponents; compno++){
977 int precno;
978 Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
979 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
980 if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
981 qntsty->nguardbits, packetno++, nlayers)) < 0)
982 return ret;
983 }
984 }
985 }
986 }
987 break;
988 case JPEG2000_PGOD_RPCL:
989 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
990 int precno;
991 step_x = 30;
992 step_y = 30;
993 for (compno = 0; compno < s->ncomponents; compno++) {
994 Jpeg2000Component *comp = tile->comp + compno;
995 if (reslevelno < codsty->nreslevels) {
996 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
997 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
998 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
999 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1000 }
1001 }
1002
1003 step_x = 1<<step_x;
1004 step_y = 1<<step_y;
1005 for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
1006 for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
1007 for (compno = 0; compno < s->ncomponents; compno++) {
1008 Jpeg2000Component *comp = tile->comp + compno;
1009 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1010 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1011 int log_subsampling[2] = { compno?s->chroma_shift[0]:0, compno?s->chroma_shift[1]:0};
1012 unsigned prcx, prcy;
1013 int trx0, try0;
1014
1015 trx0 = ff_jpeg2000_ceildivpow2(tile_coord[0][0], log_subsampling[0] + reducedresno);
1016 try0 = ff_jpeg2000_ceildivpow2(tile_coord[1][0], log_subsampling[1] + reducedresno);
1017
1018 if (!(y % ((uint64_t)1 << (reslevel->log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
1019 (y == tile_coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_height)))))
1020 continue;
1021
1022 if (!(x % ((uint64_t)1 << (reslevel->log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
1023 (x == tile_coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_width)))))
1024 continue;
1025
1026 // check if a precinct exists
1027 prcx = ff_jpeg2000_ceildivpow2(x, log_subsampling[0] + reducedresno) >> reslevel->log2_prec_width;
1028 prcy = ff_jpeg2000_ceildivpow2(y, log_subsampling[1] + reducedresno) >> reslevel->log2_prec_height;
1029 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> reslevel->log2_prec_width;
1030 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> reslevel->log2_prec_height;
1031 precno = prcx + reslevel->num_precincts_x * prcy;
1032
1033 if (prcx >= reslevel->num_precincts_x || prcy >= reslevel->num_precincts_y) {
1034 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1035 prcx, prcy, reslevel->num_precincts_x, reslevel->num_precincts_y);
1036 continue;
1037 }
1038 for (layno = 0; layno < nlayers; layno++) {
1039 if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
1040 qntsty->nguardbits, packetno++, nlayers)) < 0)
1041 return ret;
1042 }
1043 }
1044 }
1045 }
1046 }
1047 break;
1048 case JPEG2000_PGOD_PCRL:
1049 step_x = 32;
1050 step_y = 32;
1051 for (compno = 0; compno < s->ncomponents; compno++) {
1052 Jpeg2000Component *comp = tile->comp + compno;
1053
1054 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1055 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1056 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1057 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1058 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1059 }
1060 }
1061 if (step_x >= 31 || step_y >= 31){
1062 avpriv_request_sample(s->avctx, "PCRL with large step");
1063 return AVERROR_PATCHWELCOME;
1064 }
1065 step_x = 1<<step_x;
1066 step_y = 1<<step_y;
1067
1068 for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
1069 for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
1070 for (compno = 0; compno < s->ncomponents; compno++) {
1071 Jpeg2000Component *comp = tile->comp + compno;
1072 int log_subsampling[2] = { compno?s->chroma_shift[0]:0, compno?s->chroma_shift[1]:0};
1073
1074 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1075 unsigned prcx, prcy;
1076 int precno;
1077 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1078 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1079 int trx0, try0;
1080
1081 trx0 = ff_jpeg2000_ceildivpow2(tile_coord[0][0], log_subsampling[0] + reducedresno);
1082 try0 = ff_jpeg2000_ceildivpow2(tile_coord[1][0], log_subsampling[1] + reducedresno);
1083
1084 if (!(y % ((uint64_t)1 << (reslevel->log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
1085 (y == tile_coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_height)))))
1086 continue;
1087
1088 if (!(x % ((uint64_t)1 << (reslevel->log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
1089 (x == tile_coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_width)))))
1090 continue;
1091
1092 // check if a precinct exists
1093 prcx = ff_jpeg2000_ceildivpow2(x, log_subsampling[0] + reducedresno) >> reslevel->log2_prec_width;
1094 prcy = ff_jpeg2000_ceildivpow2(y, log_subsampling[1] + reducedresno) >> reslevel->log2_prec_height;
1095 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> reslevel->log2_prec_width;
1096 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> reslevel->log2_prec_height;
1097
1098 precno = prcx + reslevel->num_precincts_x * prcy;
1099
1100 if (prcx >= reslevel->num_precincts_x || prcy >= reslevel->num_precincts_y) {
1101 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1102 prcx, prcy, reslevel->num_precincts_x, reslevel->num_precincts_y);
1103 continue;
1104 }
1105 for (layno = 0; layno < nlayers; layno++) {
1106 if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
1107 qntsty->nguardbits, packetno++, nlayers)) < 0)
1108 return ret;
1109 }
1110 }
1111 }
1112 }
1113 }
1114 break;
1115 case JPEG2000_PGOD_CPRL:
1116 for (compno = 0; compno < s->ncomponents; compno++) {
1117 Jpeg2000Component *comp = tile->comp + compno;
1118 int log_subsampling[2] = { compno?s->chroma_shift[0]:0, compno?s->chroma_shift[1]:0};
1119 step_x = 32;
1120 step_y = 32;
1121
1122 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1123 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1124 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1125 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1126 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1127 }
1128 if (step_x >= 31 || step_y >= 31){
1129 avpriv_request_sample(s->avctx, "CPRL with large step");
1130 return AVERROR_PATCHWELCOME;
1131 }
1132 step_x = 1<<step_x;
1133 step_y = 1<<step_y;
1134
1135 for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
1136 for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
1137 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1138 unsigned prcx, prcy;
1139 int precno;
1140 int trx0, try0;
1141 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1142 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1143
1144 trx0 = ff_jpeg2000_ceildivpow2(tile_coord[0][0], log_subsampling[0] + reducedresno);
1145 try0 = ff_jpeg2000_ceildivpow2(tile_coord[1][0], log_subsampling[1] + reducedresno);
1146
1147 if (!(y % ((uint64_t)1 << (reslevel->log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
1148 (y == tile_coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_height)))))
1149 continue;
1150
1151 if (!(x % ((uint64_t)1 << (reslevel->log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
1152 (x == tile_coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_width)))))
1153 continue;
1154
1155 // check if a precinct exists
1156 prcx = ff_jpeg2000_ceildivpow2(x, log_subsampling[0] + reducedresno) >> reslevel->log2_prec_width;
1157 prcy = ff_jpeg2000_ceildivpow2(y, log_subsampling[1] + reducedresno) >> reslevel->log2_prec_height;
1158 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> reslevel->log2_prec_width;
1159 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> reslevel->log2_prec_height;
1160
1161 precno = prcx + reslevel->num_precincts_x * prcy;
1162
1163 if (prcx >= reslevel->num_precincts_x || prcy >= reslevel->num_precincts_y) {
1164 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1165 prcx, prcy, reslevel->num_precincts_x, reslevel->num_precincts_y);
1166 continue;
1167 }
1168 for (layno = 0; layno < nlayers; layno++) {
1169 if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
1170 qntsty->nguardbits, packetno++, nlayers)) < 0)
1171 return ret;
1172 }
1173 }
1174 }
1175 }
1176 }
1177
1178 }
1179
1180 1300 av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
1181 1300 return 0;
1182 }
1183
1184 static void makelayer(Jpeg2000EncoderContext *s, int layno, double thresh, Jpeg2000Tile* tile, int final)
1185 {
1186 int compno, resno, bandno, precno, cblkno;
1187 int passno;
1188
1189 for (compno = 0; compno < s->ncomponents; compno++) {
1190 Jpeg2000Component *comp = &tile->comp[compno];
1191
1192 for (resno = 0; resno < s->codsty.nreslevels; resno++) {
1193 Jpeg2000ResLevel *reslevel = comp->reslevel + resno;
1194
1195 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
1196 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
1197 Jpeg2000Band *band = reslevel->band + bandno;
1198 Jpeg2000Prec *prec = band->prec + precno;
1199
1200 for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
1201 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1202 Jpeg2000Layer *layer = &cblk->layers[layno];
1203 int n;
1204
1205 if (layno == 0) {
1206 cblk->ninclpasses = 0;
1207 }
1208
1209 n = cblk->ninclpasses;
1210
1211 if (thresh < 0) {
1212 n = cblk->npasses;
1213 } else {
1214 for (passno = cblk->ninclpasses; passno < cblk->npasses; passno++) {
1215 int32_t dr;
1216 double dd;
1217 Jpeg2000Pass *pass = &cblk->passes[passno];
1218
1219 if (n == 0) {
1220 dr = pass->rate;
1221 dd = pass->disto;
1222 } else {
1223 dr = pass->rate - cblk->passes[n - 1].rate;
1224 dd = pass->disto - cblk->passes[n-1].disto;
1225 }
1226
1227 if (!dr) {
1228 if (dd != 0.0) {
1229 n = passno + 1;
1230 }
1231 continue;
1232 }
1233
1234 if (thresh - (dd / dr) < DBL_EPSILON)
1235 n = passno + 1;
1236 }
1237 }
1238 layer->npasses = n - cblk->ninclpasses;
1239 layer->cum_passes = n;
1240
1241 if (layer->npasses == 0) {
1242 layer->disto = 0;
1243 layer->data_len = 0;
1244 continue;
1245 }
1246
1247 if (cblk->ninclpasses == 0) {
1248 layer->data_len = cblk->passes[n - 1].rate;
1249 layer->data_start = cblk->data;
1250 layer->disto = cblk->passes[n - 1].disto;
1251 } else {
1252 layer->data_len = cblk->passes[n - 1].rate - cblk->passes[cblk->ninclpasses - 1].rate;
1253 layer->data_start = cblk->data + cblk->passes[cblk->ninclpasses - 1].rate;
1254 layer->disto = cblk->passes[n - 1].disto -
1255 cblk->passes[cblk->ninclpasses - 1].disto;
1256 }
1257 if (final) {
1258 cblk->ninclpasses = n;
1259 }
1260 }
1261 }
1262 }
1263 }
1264 }
1265 }
1266
1267 static void makelayers(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
1268 {
1269 int precno, compno, reslevelno, bandno, cblkno, lev, passno, layno;
1270 int i;
1271 double min = DBL_MAX;
1272 double max = 0;
1273 double thresh;
1274
1275 Jpeg2000CodingStyle *codsty = &s->codsty;
1276
1277 for (compno = 0; compno < s->ncomponents; compno++){
1278 Jpeg2000Component *comp = tile->comp + compno;
1279
1280 for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
1281 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1282
1283 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
1284 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
1285 Jpeg2000Band *band = reslevel->band + bandno;
1286 Jpeg2000Prec *prec = band->prec + precno;
1287
1288 for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
1289 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1290 for (passno = 0; passno < cblk->npasses; passno++) {
1291 Jpeg2000Pass *pass = &cblk->passes[passno];
1292 int dr;
1293 double dd, drslope;
1294
1295 if (passno == 0) {
1296 dr = (int32_t)pass->rate;
1297 dd = pass->disto;
1298 } else {
1299 dr = (int32_t)(pass->rate - cblk->passes[passno - 1].rate);
1300 dd = pass->disto - cblk->passes[passno - 1].disto;
1301 }
1302
1303 if (dr <= 0)
1304 continue;
1305
1306 drslope = dd / dr;
1307 if (drslope < min)
1308 min = drslope;
1309
1310 if (drslope > max)
1311 max = drslope;
1312 }
1313 }
1314 }
1315 }
1316 }
1317 }
1318
1319 for (layno = 0; layno < s->nlayers; layno++) {
1320 double lo = min;
1321 double hi = max;
1322 double stable_thresh = 0.0;
1323 double good_thresh = 0.0;
1324 if (!s->layer_rates[layno]) {
1325 good_thresh = -1.0;
1326 } else {
1327 for (i = 0; i < 128; i++) {
1328 uint8_t *stream_pos = s->buf;
1329 int ret;
1330 thresh = (lo + hi) / 2;
1331 makelayer(s, layno, thresh, tile, 0);
1332 ret = encode_packets(s, tile, (int)(tile - s->tile), layno + 1);
1333 memset(stream_pos, 0, s->buf - stream_pos);
1334 if ((s->buf - stream_pos > ceil(tile->layer_rates[layno])) || ret < 0) {
1335 lo = thresh;
1336 s->buf = stream_pos;
1337 continue;
1338 }
1339 hi = thresh;
1340 stable_thresh = thresh;
1341 s->buf = stream_pos;
1342 }
1343 }
1344 if (good_thresh >= 0.0)
1345 good_thresh = stable_thresh == 0.0 ? thresh : stable_thresh;
1346 makelayer(s, layno, good_thresh, tile, 1);
1347 }
1348 }
1349
1350 417000 static int getcut(Jpeg2000Cblk *cblk, int64_t lambda, int dwt_norm)
1351 {
1352 417000 int passno, res = 0;
1353
2/2
✓ Branch 0 taken 7438376 times.
✓ Branch 1 taken 417000 times.
7855376 for (passno = 0; passno < cblk->npasses; passno++){
1354 int dr;
1355 int64_t dd;
1356
1357 14876752 dr = cblk->passes[passno].rate
1358
2/2
✓ Branch 0 taken 6716839 times.
✓ Branch 1 taken 721537 times.
7438376 - (res ? cblk->passes[res-1].rate : 0);
1359 14876752 dd = cblk->passes[passno].disto
1360
2/2
✓ Branch 0 taken 6716839 times.
✓ Branch 1 taken 721537 times.
7438376 - (res ? cblk->passes[res-1].disto : 0);
1361
1362
2/2
✓ Branch 0 taken 2587785 times.
✓ Branch 1 taken 4850591 times.
7438376 if (((dd * dwt_norm) >> WMSEDEC_SHIFT) * dwt_norm >= dr * lambda)
1363 2587785 res = passno+1;
1364 }
1365 417000 return res;
1366 }
1367
1368 1300 static void truncpasses(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
1369 {
1370 int precno, compno, reslevelno, bandno, cblkno, lev;
1371 1300 Jpeg2000CodingStyle *codsty = &s->codsty;
1372
1373
2/2
✓ Branch 0 taken 3900 times.
✓ Branch 1 taken 1300 times.
5200 for (compno = 0; compno < s->ncomponents; compno++){
1374 3900 Jpeg2000Component *comp = tile->comp + compno;
1375
1376
2/2
✓ Branch 0 taken 27300 times.
✓ Branch 1 taken 3900 times.
31200 for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
1377 27300 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1378
1379
2/2
✓ Branch 0 taken 27300 times.
✓ Branch 1 taken 27300 times.
54600 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
1380
2/2
✓ Branch 0 taken 74100 times.
✓ Branch 1 taken 27300 times.
101400 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
1381 74100 int bandpos = bandno + (reslevelno > 0);
1382 74100 Jpeg2000Band *band = reslevel->band + bandno;
1383 74100 Jpeg2000Prec *prec = band->prec + precno;
1384
1385
2/2
✓ Branch 0 taken 417000 times.
✓ Branch 1 taken 74100 times.
491100 for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
1386 417000 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1387
1388 834000 cblk->ninclpasses = getcut(cblk, s->lambda,
1389 417000 (int64_t)dwt_norms[codsty->transform == FF_DWT53][bandpos][lev] * (int64_t)band->i_stepsize >> 15);
1390 417000 cblk->layers[0].data_start = cblk->data;
1391 417000 cblk->layers[0].cum_passes = cblk->ninclpasses;
1392 417000 cblk->layers[0].npasses = cblk->ninclpasses;
1393
2/2
✓ Branch 0 taken 380616 times.
✓ Branch 1 taken 36384 times.
417000 if (cblk->ninclpasses)
1394 380616 cblk->layers[0].data_len = cblk->passes[cblk->ninclpasses - 1].rate;
1395 }
1396 }
1397 }
1398 }
1399 }
1400 1300 }
1401
1402 1300 static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
1403 {
1404 int compno, reslevelno, bandno, ret;
1405 Jpeg2000T1Context t1;
1406 1300 Jpeg2000CodingStyle *codsty = &s->codsty;
1407
2/2
✓ Branch 0 taken 3900 times.
✓ Branch 1 taken 1300 times.
5200 for (compno = 0; compno < s->ncomponents; compno++){
1408 3900 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
1409
1410 3900 t1.stride = (1<<codsty->log2_cblk_width) + 2;
1411
1412 3900 av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
1413
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3900 times.
3900 if ((ret = ff_dwt_encode(&comp->dwt, comp->i_data)) < 0)
1414 return ret;
1415 3900 av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
1416
1417
2/2
✓ Branch 0 taken 27300 times.
✓ Branch 1 taken 3900 times.
31200 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
1418 27300 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1419
1420
2/2
✓ Branch 0 taken 74100 times.
✓ Branch 1 taken 27300 times.
101400 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
1421 74100 Jpeg2000Band *band = reslevel->band + bandno;
1422 74100 Jpeg2000Prec *prec = band->prec; // we support only 1 precinct per band ATM in the encoder
1423 74100 int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
1424
2/2
✓ Branch 0 taken 46800 times.
✓ Branch 1 taken 27300 times.
74100 yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
1425 74100 y0 = yy0;
1426
2/2
✓ Branch 1 taken 46200 times.
✓ Branch 2 taken 27900 times.
74100 yy1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[1][0] + 1, band->log2_cblk_height) << band->log2_cblk_height,
1427 74100 band->coord[1][1]) - band->coord[1][0] + yy0;
1428
1429
3/4
✓ Branch 0 taken 74100 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3600 times.
✓ Branch 3 taken 70500 times.
74100 if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
1430 3600 continue;
1431
1432 70500 bandpos = bandno + (reslevelno > 0);
1433
1434
2/2
✓ Branch 0 taken 130800 times.
✓ Branch 1 taken 70500 times.
201300 for (cblky = 0; cblky < prec->nb_codeblocks_height; cblky++){
1435
4/4
✓ Branch 0 taken 126900 times.
✓ Branch 1 taken 3900 times.
✓ Branch 2 taken 41700 times.
✓ Branch 3 taken 85200 times.
130800 if (reslevelno == 0 || bandno == 1)
1436 45600 xx0 = 0;
1437 else
1438 85200 xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
1439 130800 x0 = xx0;
1440
2/2
✓ Branch 1 taken 39900 times.
✓ Branch 2 taken 90900 times.
130800 xx1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[0][0] + 1, band->log2_cblk_width) << band->log2_cblk_width,
1441 130800 band->coord[0][1]) - band->coord[0][0] + xx0;
1442
1443
2/2
✓ Branch 0 taken 413400 times.
✓ Branch 1 taken 130800 times.
544200 for (cblkx = 0; cblkx < prec->nb_codeblocks_width; cblkx++, cblkno++){
1444 int y, x;
1445
2/2
✓ Branch 0 taken 206700 times.
✓ Branch 1 taken 206700 times.
413400 if (codsty->transform == FF_DWT53){
1446
2/2
✓ Branch 0 taken 2979750 times.
✓ Branch 1 taken 206700 times.
3186450 for (y = yy0; y < yy1; y++){
1447 2979750 int *ptr = t1.data + (y-yy0)*t1.stride;
1448
2/2
✓ Branch 0 taken 45792600 times.
✓ Branch 1 taken 2979750 times.
48772350 for (x = xx0; x < xx1; x++){
1449 45792600 *ptr++ = comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] * (1 << NMSEDEC_FRACBITS);
1450 }
1451 }
1452 } else{
1453
2/2
✓ Branch 0 taken 2979750 times.
✓ Branch 1 taken 206700 times.
3186450 for (y = yy0; y < yy1; y++){
1454 2979750 int *ptr = t1.data + (y-yy0)*t1.stride;
1455
2/2
✓ Branch 0 taken 45792600 times.
✓ Branch 1 taken 2979750 times.
48772350 for (x = xx0; x < xx1; x++){
1456 45792600 *ptr = (comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]);
1457 45792600 *ptr = (int64_t)*ptr * (int64_t)(16384 * 65536 / band->i_stepsize) >> 15 - NMSEDEC_FRACBITS;
1458 45792600 ptr++;
1459 }
1460 }
1461 }
1462
2/2
✓ Branch 0 taken 8268 times.
✓ Branch 1 taken 405132 times.
413400 if (!prec->cblk[cblkno].data)
1463 8268 prec->cblk[cblkno].data = av_malloc(1 + 8192);
1464
2/2
✓ Branch 0 taken 8268 times.
✓ Branch 1 taken 405132 times.
413400 if (!prec->cblk[cblkno].passes)
1465 8268 prec->cblk[cblkno].passes = av_malloc_array(JPEG2000_MAX_PASSES, sizeof (*prec->cblk[cblkno].passes));
1466
2/4
✓ Branch 0 taken 413400 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 413400 times.
413400 if (!prec->cblk[cblkno].data || !prec->cblk[cblkno].passes)
1467 return AVERROR(ENOMEM);
1468 413400 encode_cblk(s, &t1, prec->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
1469 413400 bandpos, codsty->nreslevels - reslevelno - 1);
1470 413400 xx0 = xx1;
1471 413400 xx1 = FFMIN(xx1 + (1 << band->log2_cblk_width), band->coord[0][1] - band->coord[0][0] + x0);
1472 }
1473 130800 yy0 = yy1;
1474 130800 yy1 = FFMIN(yy1 + (1 << band->log2_cblk_height), band->coord[1][1] - band->coord[1][0] + y0);
1475 }
1476 }
1477 }
1478 3900 av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
1479 }
1480
1481 1300 av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
1482
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1300 times.
1300 if (s->compression_rate_enc)
1483 makelayers(s, tile);
1484 else
1485 1300 truncpasses(s, tile);
1486
1487
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1300 times.
1300 if ((ret = encode_packets(s, tile, tileno, s->nlayers)) < 0)
1488 return ret;
1489 1300 av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
1490 1300 return 0;
1491 }
1492
1493 8 static void cleanup(Jpeg2000EncoderContext *s)
1494 {
1495 int tileno, compno;
1496 8 Jpeg2000CodingStyle *codsty = &s->codsty;
1497
1498
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!s->tile)
1499 return;
1500
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 8 times.
34 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1501
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 if (s->tile[tileno].comp) {
1502
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 26 times.
104 for (compno = 0; compno < s->ncomponents; compno++){
1503 78 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
1504 78 ff_jpeg2000_cleanup(comp, codsty);
1505 }
1506 26 av_freep(&s->tile[tileno].comp);
1507 }
1508 26 av_freep(&s->tile[tileno].layer_rates);
1509 }
1510 8 av_freep(&s->tile);
1511 }
1512
1513 400 static void reinit(Jpeg2000EncoderContext *s)
1514 {
1515 int tileno, compno;
1516
2/2
✓ Branch 0 taken 1300 times.
✓ Branch 1 taken 400 times.
1700 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1517 1300 Jpeg2000Tile *tile = s->tile + tileno;
1518
2/2
✓ Branch 0 taken 3900 times.
✓ Branch 1 taken 1300 times.
5200 for (compno = 0; compno < s->ncomponents; compno++)
1519 3900 ff_jpeg2000_reinit(tile->comp + compno, &s->codsty);
1520 }
1521 400 }
1522
1523 2000 static void update_size(uint8_t *size, const uint8_t *end)
1524 {
1525 2000 AV_WB32(size, end-size);
1526 2000 }
1527
1528 400 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
1529 const AVFrame *pict, int *got_packet)
1530 {
1531 int tileno, ret;
1532 400 Jpeg2000EncoderContext *s = avctx->priv_data;
1533 uint8_t *chunkstart, *jp2cstart, *jp2hstart;
1534
1535
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 400 times.
400 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 400 s->buf = s->buf_start = pkt->data;
1540 400 s->buf_end = pkt->data + pkt->size;
1541
1542 400 s->picture = pict;
1543
1544 400 s->lambda = s->picture->quality * LAMBDA_SCALE;
1545
1546
2/4
✓ Branch 0 taken 400 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 400 times.
400 if (avctx->pix_fmt == AV_PIX_FMT_BGR48 || avctx->pix_fmt == AV_PIX_FMT_GRAY16)
1547 copy_frame_16(s);
1548 else
1549 400 copy_frame_8(s);
1550
1551 400 reinit(s);
1552
1553
1/2
✓ Branch 0 taken 400 times.
✗ Branch 1 not taken.
400 if (s->format == CODEC_JP2) {
1554
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 400 times.
400 av_assert0(s->buf == pkt->data);
1555
1556 400 bytestream_put_be32(&s->buf, 0x0000000C);
1557 400 bytestream_put_be32(&s->buf, 0x6A502020);
1558 400 bytestream_put_be32(&s->buf, 0x0D0A870A);
1559
1560 400 chunkstart = s->buf;
1561 400 bytestream_put_be32(&s->buf, 0);
1562 400 bytestream_put_buffer(&s->buf, "ftyp", 4);
1563 400 bytestream_put_buffer(&s->buf, "jp2\040\040", 4);
1564 400 bytestream_put_be32(&s->buf, 0);
1565 400 bytestream_put_buffer(&s->buf, "jp2\040", 4);
1566 400 update_size(chunkstart, s->buf);
1567
1568 400 jp2hstart = s->buf;
1569 400 bytestream_put_be32(&s->buf, 0);
1570 400 bytestream_put_buffer(&s->buf, "jp2h", 4);
1571
1572 400 chunkstart = s->buf;
1573 400 bytestream_put_be32(&s->buf, 0);
1574 400 bytestream_put_buffer(&s->buf, "ihdr", 4);
1575 400 bytestream_put_be32(&s->buf, avctx->height);
1576 400 bytestream_put_be32(&s->buf, avctx->width);
1577 400 bytestream_put_be16(&s->buf, s->ncomponents);
1578 400 bytestream_put_byte(&s->buf, s->cbps[0]);
1579 400 bytestream_put_byte(&s->buf, 7);
1580 400 bytestream_put_byte(&s->buf, 0);
1581 400 bytestream_put_byte(&s->buf, 0);
1582 400 update_size(chunkstart, s->buf);
1583
1584 400 chunkstart = s->buf;
1585 400 bytestream_put_be32(&s->buf, 0);
1586 400 bytestream_put_buffer(&s->buf, "colr", 4);
1587 400 bytestream_put_byte(&s->buf, 1);
1588 400 bytestream_put_byte(&s->buf, 0);
1589 400 bytestream_put_byte(&s->buf, 0);
1590
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 400 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
400 if (avctx->pix_fmt == AV_PIX_FMT_RGB24 || avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1591 400 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 400 update_size(chunkstart, s->buf);
1598
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 400 times.
400 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1599 int i;
1600 const 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 400 update_size(jp2hstart, s->buf);
1623
1624 400 jp2cstart = s->buf;
1625 400 bytestream_put_be32(&s->buf, 0);
1626 400 bytestream_put_buffer(&s->buf, "jp2c", 4);
1627 }
1628
1629
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 400 times.
400 if (s->buf_end - s->buf < 2)
1630 return -1;
1631 400 bytestream_put_be16(&s->buf, JPEG2000_SOC);
1632
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 400 times.
400 if ((ret = put_siz(s)) < 0)
1633 return ret;
1634
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 400 times.
400 if ((ret = put_cod(s)) < 0)
1635 return ret;
1636
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 400 times.
400 if ((ret = put_qcd(s, 0)) < 0)
1637 return ret;
1638
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 400 times.
400 if ((ret = put_com(s, 0)) < 0)
1639 return ret;
1640
1641
2/2
✓ Branch 0 taken 1300 times.
✓ Branch 1 taken 400 times.
1700 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1642 uint8_t *psotptr;
1643
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1300 times.
1300 if (!(psotptr = put_sot(s, tileno)))
1644 return -1;
1645
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1300 times.
1300 if (s->buf_end - s->buf < 2)
1646 return -1;
1647 1300 bytestream_put_be16(&s->buf, JPEG2000_SOD);
1648
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1300 times.
1300 if ((ret = encode_tile(s, s->tile + tileno, tileno)) < 0)
1649 return ret;
1650 1300 bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
1651 }
1652
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 400 times.
400 if (s->buf_end - s->buf < 2)
1653 return -1;
1654 400 bytestream_put_be16(&s->buf, JPEG2000_EOC);
1655
1656
1/2
✓ Branch 0 taken 400 times.
✗ Branch 1 not taken.
400 if (s->format == CODEC_JP2)
1657 400 update_size(jp2cstart, s->buf);
1658
1659 400 av_log(s->avctx, AV_LOG_DEBUG, "end\n");
1660 400 pkt->size = s->buf - s->buf_start;
1661 400 *got_packet = 1;
1662
1663 400 return 0;
1664 }
1665
1666 8 static int parse_layer_rates(Jpeg2000EncoderContext *s)
1667 {
1668 int i;
1669 char *token;
1670 8 char *saveptr = NULL;
1671 int rate;
1672 8 int nlayers = 0;
1673
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (!s->lr_str) {
1674 8 s->nlayers = 1;
1675 8 s->layer_rates[0] = 0;
1676 8 s->compression_rate_enc = 0;
1677 8 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
1713 8 static av_cold int j2kenc_init(AVCodecContext *avctx)
1714 {
1715 static AVOnce init_static_once = AV_ONCE_INIT;
1716 int i, ret;
1717 8 Jpeg2000EncoderContext *s = avctx->priv_data;
1718 8 Jpeg2000CodingStyle *codsty = &s->codsty;
1719 8 Jpeg2000QuantStyle *qntsty = &s->qntsty;
1720
1721 8 s->avctx = avctx;
1722 8 av_log(s->avctx, AV_LOG_DEBUG, "init\n");
1723
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 if (parse_layer_rates(s)) {
1724 av_log(s, AV_LOG_WARNING, "Layer rates invalid. Encoding with 1 layer based on quality metric.\n");
1725 s->nlayers = 1;
1726 s->layer_rates[0] = 0;
1727 s->compression_rate_enc = 0;
1728 }
1729
1730
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
8 if (avctx->pix_fmt == AV_PIX_FMT_PAL8 && (s->pred != FF_DWT97_INT || s->format != CODEC_JP2)) {
1731 av_log(s->avctx, AV_LOG_WARNING, "Forcing lossless jp2 for pal8\n");
1732 s->pred = FF_DWT97_INT;
1733 s->format = CODEC_JP2;
1734 }
1735
1736 // defaults:
1737 // TODO: implement setting non-standard precinct size
1738 8 memset(codsty->log2_prec_widths , 15, sizeof(codsty->log2_prec_widths ));
1739 8 memset(codsty->log2_prec_heights, 15, sizeof(codsty->log2_prec_heights));
1740 8 codsty->nreslevels2decode=
1741 8 codsty->nreslevels = 7;
1742 8 codsty->nlayers = s->nlayers;
1743 8 codsty->log2_cblk_width = 4;
1744 8 codsty->log2_cblk_height = 4;
1745
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 codsty->transform = s->pred ? FF_DWT53 : FF_DWT97_INT;
1746
1747 8 qntsty->nguardbits = 1;
1748
1749
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if ((s->tile_width & (s->tile_width -1)) ||
1750
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 (s->tile_height & (s->tile_height-1))) {
1751 av_log(avctx, AV_LOG_WARNING, "Tile dimension not a power of 2\n");
1752 }
1753
1754
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 if (codsty->transform == FF_DWT53)
1755 4 qntsty->quantsty = JPEG2000_QSTY_NONE;
1756 else
1757 4 qntsty->quantsty = JPEG2000_QSTY_SE;
1758
1759 8 s->width = avctx->width;
1760 8 s->height = avctx->height;
1761
1762
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 8 times.
32 for (i = 0; i < 3; i++) {
1763
2/4
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
24 if (avctx->pix_fmt == AV_PIX_FMT_GRAY16 || avctx->pix_fmt == AV_PIX_FMT_RGB48)
1764 s->cbps[i] = 16;
1765 else
1766 24 s->cbps[i] = 8;
1767 }
1768
1769
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8 if (avctx->pix_fmt == AV_PIX_FMT_RGB24 || avctx->pix_fmt == AV_PIX_FMT_RGB48){
1770 8 s->ncomponents = 3;
1771 } else if (avctx->pix_fmt == AV_PIX_FMT_GRAY8 || avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY16){
1772 s->ncomponents = 1;
1773 } else{ // planar YUV
1774 s->planar = 1;
1775 s->ncomponents = 3;
1776 ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt,
1777 s->chroma_shift, s->chroma_shift + 1);
1778 if (ret)
1779 return ret;
1780 }
1781
1782 8 ff_thread_once(&init_static_once, init_luts);
1783
1784 8 init_quantization(s);
1785
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 if ((ret=init_tiles(s)) < 0)
1786 return ret;
1787
1788 8 av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
1789
1790 8 return 0;
1791 }
1792
1793 8 static int j2kenc_destroy(AVCodecContext *avctx)
1794 {
1795 8 Jpeg2000EncoderContext *s = avctx->priv_data;
1796
1797 8 cleanup(s);
1798 8 return 0;
1799 }
1800
1801 // taken from the libopenjpeg wraper so it matches
1802
1803 #define OFFSET(x) offsetof(Jpeg2000EncoderContext, x)
1804 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1805 static const AVOption options[] = {
1806 { "format", "Codec Format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = CODEC_JP2 }, CODEC_J2K, CODEC_JP2, VE, "format" },
1807 { "j2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_J2K }, 0, 0, VE, "format" },
1808 { "jp2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_JP2 }, 0, 0, VE, "format" },
1809 { "tile_width", "Tile Width", OFFSET(tile_width), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, 1<<30, VE, },
1810 { "tile_height", "Tile Height", OFFSET(tile_height), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, 1<<30, VE, },
1811 { "pred", "DWT Type", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, "pred" },
1812 { "dwt97int", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "pred" },
1813 { "dwt53", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "pred" },
1814 { "sop", "SOP marker", OFFSET(sop), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, },
1815 { "eph", "EPH marker", OFFSET(eph), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, },
1816 { "prog", "Progression Order", OFFSET(prog), AV_OPT_TYPE_INT, { .i64 = 0 }, JPEG2000_PGOD_LRCP, JPEG2000_PGOD_CPRL, VE, "prog" },
1817 { "lrcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_LRCP }, 0, 0, VE, "prog" },
1818 { "rlcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_RLCP }, 0, 0, VE, "prog" },
1819 { "rpcl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_RPCL }, 0, 0, VE, "prog" },
1820 { "pcrl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_PCRL }, 0, 0, VE, "prog" },
1821 { "cprl", NULL,