FFmpeg coverage


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