FFmpeg
Loading...
Searching...
No Matches
j2kenc.c
Go to the documentation of this file.
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"
75#include "libavutil/common.h"
76#include "libavutil/mem.h"
77#include "libavutil/pixdesc.h"
78#include "libavutil/opt.h"
80#include "libavutil/avstring.h"
81#include "libavutil/thread.h"
82
83#define NMSEDEC_BITS 7
84#define NMSEDEC_FRACBITS (NMSEDEC_BITS-1)
85#define WMSEDEC_SHIFT 13 ///< must be >= 13
86#define LAMBDA_SCALE (100000000LL << (WMSEDEC_SHIFT - 13))
87
88#define CODEC_JP2 1
89#define CODEC_J2K 0
90
95
96static const int dwt_norms[2][4][10] = { // [dwt_type][band][rlevel] (multiplied by 10000)
97 {{10000, 19650, 41770, 84030, 169000, 338400, 676900, 1353000, 2706000, 5409000},
98 {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
99 {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
100 {20800, 38650, 83070, 171800, 347100, 695900, 1393000, 2786000, 5572000}},
101
102 {{10000, 15000, 27500, 53750, 106800, 213400, 426700, 853300, 1707000, 3413000},
103 {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
104 {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
105 { 7186, 9218, 15860, 30430, 60190, 120100, 240000, 479700, 959300}}
106};
107
108typedef struct {
110 double *layer_rates;
112
113typedef struct {
114 AVClass *class;
117
118 int width, height; ///< image width and height
119 uint8_t cbps[4]; ///< bits per sample in particular components
120 uint8_t comp_remap[4];
122 uint8_t planar;
124 int tile_width, tile_height; ///< tile size
126
127 uint8_t *buf_start;
128 uint8_t *buf;
129 uint8_t *buf_end;
131
132 uint64_t lambda;
133
136
138 int layer_rates[100];
139 uint8_t compression_rate_enc; ///< Is compression done using compression ratio?
140
142 int pred;
143 int sop;
144 int eph;
145 int prog;
147 char *lr_str;
149
150
151/* bitstream routines */
152
153/** put n times val bit */
154static void put_bits(Jpeg2000EncoderContext *s, int val, int n) // TODO: optimize
155{
156 val <<= 7;
157 while (n-- > 0){
158 if (s->bit_index == 8)
159 {
160 s->bit_index = *s->buf == 0xff;
161 *(++s->buf) = 0;
162 }
163 *s->buf |= val >> s->bit_index++;
164 }
165}
166
167/** put n least significant bits of a number num */
168static void put_num(Jpeg2000EncoderContext *s, int num, int n)
169{
170 while(--n >= 0)
171 put_bits(s, (num >> n) & 1, 1);
172}
173
174/** flush the bitstream */
176{
177 if (s->bit_index){
178 s->bit_index = 0;
179 s->buf++;
180 }
181}
182
183/* tag tree routines */
184
185/** code the value stored in node */
186static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold)
187{
188 Jpeg2000TgtNode *stack[30];
189 int sp = -1, curval = 0;
190
191 while(node->parent){
192 stack[++sp] = node;
193 node = node->parent;
194 }
195
196 while (1) {
197 if (curval > node->temp_val)
198 node->temp_val = curval;
199 else {
200 curval = node->temp_val;
201 }
202
203 if (node->val >= threshold) {
204 put_bits(s, 0, threshold - curval);
205 curval = threshold;
206 } else {
207 put_bits(s, 0, node->val - curval);
208 curval = node->val;
209 if (!node->vis) {
210 put_bits(s, 1, 1);
211 node->vis = 1;
212 }
213 }
214
215 node->temp_val = curval;
216 if (sp < 0)
217 break;
218 node = stack[sp--];
219 }
220}
221
222/** update the value in node */
224{
225 while (node->parent){
226 if (node->parent->val <= node->val)
227 break;
228 node->parent->val = node->val;
229 node = node->parent;
230 }
231}
232
234{
235 int i;
236
237 if (s->buf_end - s->buf < 40 + 3 * s->ncomponents)
238 return -1;
239
240 bytestream_put_be16(&s->buf, JPEG2000_SIZ);
241 bytestream_put_be16(&s->buf, 38 + 3 * s->ncomponents); // Lsiz
242 bytestream_put_be16(&s->buf, 0); // Rsiz
243 bytestream_put_be32(&s->buf, s->width); // width
244 bytestream_put_be32(&s->buf, s->height); // height
245 bytestream_put_be32(&s->buf, 0); // X0Siz
246 bytestream_put_be32(&s->buf, 0); // Y0Siz
247
248 bytestream_put_be32(&s->buf, s->tile_width); // XTSiz
249 bytestream_put_be32(&s->buf, s->tile_height); // YTSiz
250 bytestream_put_be32(&s->buf, 0); // XT0Siz
251 bytestream_put_be32(&s->buf, 0); // YT0Siz
252 bytestream_put_be16(&s->buf, s->ncomponents); // CSiz
253
254 for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
255 bytestream_put_byte(&s->buf, s->cbps[i] - 1);
256 bytestream_put_byte(&s->buf, (i+1&2)?1<<s->chroma_shift[0]:1);
257 bytestream_put_byte(&s->buf, (i+1&2)?1<<s->chroma_shift[1]:1);
258 }
259 return 0;
260}
261
263{
264 Jpeg2000CodingStyle *codsty = &s->codsty;
265 uint8_t scod = 0;
266
267 if (s->buf_end - s->buf < 14)
268 return -1;
269
270 bytestream_put_be16(&s->buf, JPEG2000_COD);
271 bytestream_put_be16(&s->buf, 12); // Lcod
272 if (s->sop)
273 scod |= JPEG2000_CSTY_SOP;
274 if (s->eph)
275 scod |= JPEG2000_CSTY_EPH;
276 bytestream_put_byte(&s->buf, scod); // Scod
277 // SGcod
278 bytestream_put_byte(&s->buf, s->prog); // progression level
279 bytestream_put_be16(&s->buf, s->nlayers); // num of layers
280 if(s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
281 bytestream_put_byte(&s->buf, 0); // unspecified
282 }else{
283 bytestream_put_byte(&s->buf, 0); // unspecified
284 }
285 // SPcod
286 bytestream_put_byte(&s->buf, codsty->nreslevels - 1); // num of decomp. levels
287 bytestream_put_byte(&s->buf, codsty->log2_cblk_width-2); // cblk width
288 bytestream_put_byte(&s->buf, codsty->log2_cblk_height-2); // cblk height
289 bytestream_put_byte(&s->buf, 0); // cblk style
290 bytestream_put_byte(&s->buf, codsty->transform == FF_DWT53); // transformation
291 return 0;
292}
293
294static int put_qcd(Jpeg2000EncoderContext *s, int compno)
295{
296 int i, size;
297 Jpeg2000CodingStyle *codsty = &s->codsty;
298 Jpeg2000QuantStyle *qntsty = &s->qntsty;
299
300 if (qntsty->quantsty == JPEG2000_QSTY_NONE)
301 size = 4 + 3 * (codsty->nreslevels-1);
302 else // QSTY_SE
303 size = 5 + 6 * (codsty->nreslevels-1);
304
305 if (s->buf_end - s->buf < size + 2)
306 return -1;
307
308 bytestream_put_be16(&s->buf, JPEG2000_QCD);
309 bytestream_put_be16(&s->buf, size); // LQcd
310 bytestream_put_byte(&s->buf, (qntsty->nguardbits << 5) | qntsty->quantsty); // Sqcd
311 if (qntsty->quantsty == JPEG2000_QSTY_NONE)
312 for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
313 bytestream_put_byte(&s->buf, qntsty->expn[i] << 3);
314 else // QSTY_SE
315 for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
316 bytestream_put_be16(&s->buf, (qntsty->expn[i] << 11) | qntsty->mant[i]);
317 return 0;
318}
319
320static int put_com(Jpeg2000EncoderContext *s, int compno)
321{
322 int size = 4 + strlen(LIBAVCODEC_IDENT);
323
324 if (s->avctx->flags & AV_CODEC_FLAG_BITEXACT)
325 return 0;
326
327 if (s->buf_end - s->buf < size + 2)
328 return -1;
329
330 bytestream_put_be16(&s->buf, JPEG2000_COM);
331 bytestream_put_be16(&s->buf, size);
332 bytestream_put_be16(&s->buf, 1); // General use (ISO/IEC 8859-15 (Latin) values)
333
335
336 return 0;
337}
338
339static uint8_t *put_sot(Jpeg2000EncoderContext *s, int tileno)
340{
341 uint8_t *psotptr;
342
343 if (s->buf_end - s->buf < 12)
344 return NULL;
345
346 bytestream_put_be16(&s->buf, JPEG2000_SOT);
347 bytestream_put_be16(&s->buf, 10); // Lsot
348 bytestream_put_be16(&s->buf, tileno); // Isot
349
350 psotptr = s->buf;
351 bytestream_put_be32(&s->buf, 0); // Psot (filled in later)
352
353 bytestream_put_byte(&s->buf, 0); // TPsot
354 bytestream_put_byte(&s->buf, 1); // TNsot
355 return psotptr;
356}
357
359{
360 int i, j;
361 int layno, compno;
362 for (i = 0; i < s->numYtiles; i++) {
363 for (j = 0; j < s->numXtiles; j++) {
364 Jpeg2000Tile *tile = &s->tile[s->numXtiles * i + j];
365 for (compno = 0; compno < s->ncomponents; compno++) {
366 int tilew = tile->comp[compno].coord[0][1] - tile->comp[compno].coord[0][0];
367 int tileh = tile->comp[compno].coord[1][1] - tile->comp[compno].coord[1][0];
368 int scale = ((compno+1&2)?1 << s->chroma_shift[0]:1) * ((compno+1&2)?1 << s->chroma_shift[1]:1);
369 for (layno = 0; layno < s->nlayers; layno++) {
370 if (s->layer_rates[layno] > 0) {
371 tile->layer_rates[layno] += (double)(tilew * tileh) * s->ncomponents * s->cbps[compno] /
372 (double)(s->layer_rates[layno] * 8 * scale);
373 } else {
374 tile->layer_rates[layno] = 0.0;
375 }
376 }
377 }
378 }
379 }
380
381}
382
383/**
384 * compute the sizes of tiles, resolution levels, bands, etc.
385 * allocate memory for them
386 * divide the input image into tile-components
387 */
389{
390 int tileno, tilex, tiley, compno;
391 Jpeg2000CodingStyle *codsty = &s->codsty;
392 Jpeg2000QuantStyle *qntsty = &s->qntsty;
393
394 s->numXtiles = ff_jpeg2000_ceildiv(s->width, s->tile_width);
395 s->numYtiles = ff_jpeg2000_ceildiv(s->height, s->tile_height);
396
397 s->tile = av_calloc(s->numXtiles, s->numYtiles * sizeof(Jpeg2000Tile));
398 if (!s->tile)
399 return AVERROR(ENOMEM);
400 for (tileno = 0, tiley = 0; tiley < s->numYtiles; tiley++)
401 for (tilex = 0; tilex < s->numXtiles; tilex++, tileno++){
402 Jpeg2000Tile *tile = s->tile + tileno;
403
404 tile->comp = av_calloc(s->ncomponents, sizeof(*tile->comp));
405 if (!tile->comp)
406 return AVERROR(ENOMEM);
407
408 tile->layer_rates = av_calloc(s->nlayers, sizeof(*tile->layer_rates));
409 if (!tile->layer_rates)
410 return AVERROR(ENOMEM);
411
412 for (compno = 0; compno < s->ncomponents; compno++){
413 Jpeg2000Component *comp = tile->comp + compno;
414 int ret, i, j;
415
416 comp->coord[0][0] = comp->coord_o[0][0] = tilex * s->tile_width;
417 comp->coord[0][1] = comp->coord_o[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
418 comp->coord[1][0] = comp->coord_o[1][0] = tiley * s->tile_height;
419 comp->coord[1][1] = comp->coord_o[1][1] = FFMIN((tiley+1)*s->tile_height, s->height);
420 if (compno + 1 & 2)
421 for (i = 0; i < 2; i++)
422 for (j = 0; j < 2; j++)
423 comp->coord[i][j] = comp->coord_o[i][j] = ff_jpeg2000_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
424
426 codsty,
427 qntsty,
428 s->cbps[compno],
429 (compno+1&2)?1<<s->chroma_shift[0]:1,
430 (compno+1&2)?1<<s->chroma_shift[1]:1,
431 s->avctx
432 )) < 0)
433 return ret;
434 }
435 }
437 return 0;
438}
439
440#define COPY_FRAME(D, PIXEL) \
441 static void copy_frame_ ##D(Jpeg2000EncoderContext *s) \
442 { \
443 int tileno, compno, i, y, x; \
444 const PIXEL *line; \
445 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){ \
446 Jpeg2000Tile *tile = s->tile + tileno; \
447 if (s->planar){ \
448 for (compno = 0; compno < s->ncomponents; compno++){ \
449 int icompno = s->comp_remap[compno]; \
450 Jpeg2000Component *comp = tile->comp + compno; \
451 int *dst = comp->i_data; \
452 int cbps = s->cbps[compno]; \
453 line = (const PIXEL*)s->picture->data[icompno] \
454 + comp->coord[1][0] * (s->picture->linesize[icompno] / sizeof(PIXEL)) \
455 + comp->coord[0][0]; \
456 for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){ \
457 const PIXEL *ptr = line; \
458 for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++) \
459 *dst++ = *ptr++ - (1 << (cbps - 1)); \
460 line += s->picture->linesize[icompno] / sizeof(PIXEL); \
461 } \
462 } \
463 } else{ \
464 line = (const PIXEL*)(s->picture->data[0] + tile->comp[0].coord[1][0] * s->picture->linesize[0]) \
465 + tile->comp[0].coord[0][0] * s->ncomponents; \
466 \
467 i = 0; \
468 for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){ \
469 const PIXEL *ptr = line; \
470 for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){ \
471 for (compno = 0; compno < s->ncomponents; compno++){ \
472 int cbps = s->cbps[compno]; \
473 tile->comp[compno].i_data[i] = *ptr++ - (1 << (cbps - 1)); \
474 } \
475 } \
476 line += s->picture->linesize[0] / sizeof(PIXEL); \
477 } \
478 } \
479 } \
480 }
481
482COPY_FRAME(8, uint8_t)
483COPY_FRAME(16, uint16_t)
484
486{
487 int compno, reslevelno, bandno;
488 Jpeg2000QuantStyle *qntsty = &s->qntsty;
489 Jpeg2000CodingStyle *codsty = &s->codsty;
490
491 for (compno = 0; compno < s->ncomponents; compno++){
492 int gbandno = 0;
493 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
494 int nbands, lev = codsty->nreslevels - reslevelno - 1;
495 nbands = reslevelno ? 3 : 1;
496 for (bandno = 0; bandno < nbands; bandno++, gbandno++){
497 int expn, mant = 0;
498
499 if (codsty->transform == FF_DWT97_INT){
500 int bandpos = bandno + (reslevelno>0),
501 ss = 81920000 / dwt_norms[0][bandpos][lev],
502 log = av_log2(ss);
503 mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
504 expn = s->cbps[compno] - log + 13;
505 } else
506 expn = ((bandno&2)>>1) + (reslevelno>0) + s->cbps[compno];
507
508 qntsty->expn[gbandno] = expn;
509 qntsty->mant[gbandno] = mant;
510 }
511 }
512 }
513}
514
515static av_cold void init_luts(void)
516{
517 int i, a,
518 mask = ~((1<<NMSEDEC_FRACBITS)-1);
519
520 for (i = 0; i < (1 << NMSEDEC_BITS); i++){
521 lut_nmsedec_sig[i] = FFMAX((3 * i << (13 - NMSEDEC_FRACBITS)) - (9 << 11), 0);
522 lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
523
524 a = (i >> (NMSEDEC_BITS-2)&2) + 1;
525 lut_nmsedec_ref[i] = FFMAX((a - 2) * (i << (13 - NMSEDEC_FRACBITS)) +
526 (1 << 13) - (a * a << 11), 0);
527 lut_nmsedec_ref0[i] = FFMAX(((i * i - (i << NMSEDEC_BITS) + (1 << 2 * NMSEDEC_FRACBITS) + (1 << (NMSEDEC_FRACBITS - 1))) & mask)
528 << 1, 0);
529 }
531}
532
533/* tier-1 routines */
534static int getnmsedec_sig(int x, int bpno)
535{
536 if (bpno > NMSEDEC_FRACBITS)
537 return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
538 return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
539}
540
541static int getnmsedec_ref(int x, int bpno)
542{
543 if (bpno > NMSEDEC_FRACBITS)
544 return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
545 return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
546}
547
548static void encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
549{
550 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
551 for (y0 = 0; y0 < height; y0 += 4)
552 for (x = 0; x < width; x++)
553 for (y = y0; y < height && y < y0+4; y++){
554 if (!(t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG) && (t1->flags[(y+1) * t1->stride + x+1] & JPEG2000_T1_SIG_NB)){
555 int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno),
556 bit = t1->data[(y) * t1->stride + x] & mask ? 1 : 0;
557 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
558 if (bit){
559 int xorbit;
560 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
561 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
562 *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
563 ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
564 }
565 t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_VIS;
566 }
567 }
568}
569
570static void encode_refpass(Jpeg2000T1Context *t1, int width, int height, int *nmsedec, int bpno)
571{
572 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
573 for (y0 = 0; y0 < height; y0 += 4)
574 for (x = 0; x < width; x++)
575 for (y = y0; y < height && y < y0+4; y++)
576 if ((t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG){
577 int ctxno = ff_jpeg2000_getrefctxno(t1->flags[(y+1) * t1->stride + x+1]);
578 *nmsedec += getnmsedec_ref(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
579 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
580 t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_REF;
581 }
582}
583
584static void encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
585{
586 int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
587 for (y0 = 0; y0 < height; y0 += 4)
588 for (x = 0; x < width; x++){
589 if (y0 + 3 < height && !(
590 (t1->flags[(y0+1) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
591 (t1->flags[(y0+2) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
592 (t1->flags[(y0+3) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
593 (t1->flags[(y0+4) * t1->stride + x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG))))
594 {
595 // aggregation mode
596 int rlen;
597 for (rlen = 0; rlen < 4; rlen++)
598 if (t1->data[(y0+rlen) * t1->stride + x] & mask)
599 break;
600 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
601 if (rlen == 4)
602 continue;
603 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
604 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
605 for (y = y0 + rlen; y < y0 + 4; y++){
606 if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
607 int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
608 if (y > y0 + rlen)
609 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
610 if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
611 int xorbit;
612 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
613 *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
614 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
615 ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
616 }
617 }
618 t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
619 }
620 } else{
621 for (y = y0; y < y0 + 4 && y < height; y++){
622 if (!(t1->flags[(y+1) * t1->stride + x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
623 int ctxno = ff_jpeg2000_getsigctxno(t1->flags[(y+1) * t1->stride + x+1], bandno);
624 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[(y) * t1->stride + x] & mask ? 1:0);
625 if (t1->data[(y) * t1->stride + x] & mask){ // newly significant
626 int xorbit;
627 int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[(y+1) * t1->stride + x+1], &xorbit);
628 *nmsedec += getnmsedec_sig(t1->data[(y) * t1->stride + x], bpno + NMSEDEC_FRACBITS);
629 ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[(y+1) * t1->stride + x+1] >> 15) ^ xorbit);
630 ff_jpeg2000_set_significance(t1, x, y, t1->flags[(y+1) * t1->stride + x+1] >> 15);
631 }
632 }
633 t1->flags[(y+1) * t1->stride + x+1] &= ~JPEG2000_T1_VIS;
634 }
635 }
636 }
637}
638
640 int width, int height, int bandpos, int lev)
641{
642 int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
643 int64_t wmsedec = 0;
644
645 memset(t1->flags, 0, t1->stride * (height + 2) * sizeof(*t1->flags));
646
647 for (y = 0; y < height; y++){
648 for (x = 0; x < width; x++){
649 if (t1->data[(y) * t1->stride + x] < 0){
650 t1->flags[(y+1) * t1->stride + x+1] |= JPEG2000_T1_SGN;
651 t1->data[(y) * t1->stride + x] = -t1->data[(y) * t1->stride + x];
652 }
653 max = FFMAX(max, t1->data[(y) * t1->stride + x]);
654 }
655 }
656
657 if (max == 0){
658 cblk->nonzerobits = 0;
659 } else{
661 }
662 bpno = cblk->nonzerobits - 1;
663
664 cblk->data[0] = 0;
665 ff_mqc_initenc(&t1->mqc, cblk->data + 1);
666
667 for (passno = 0; bpno >= 0; passno++){
668 nmsedec=0;
669
670 switch(pass_t){
671 case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
672 break;
673 case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
674 break;
675 case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
676 break;
677 }
678
679 cblk->passes[passno].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno].flushed, &cblk->passes[passno].flushed_len);
680 cblk->passes[passno].rate -= cblk->passes[passno].flushed_len;
681
682 wmsedec += (int64_t)nmsedec << (2*bpno);
683 cblk->passes[passno].disto = wmsedec;
684
685 if (++pass_t == 3){
686 pass_t = 0;
687 bpno--;
688 }
689 }
690 cblk->npasses = passno;
691 cblk->ninclpasses = passno;
692
693 if (passno) {
694 cblk->passes[passno-1].rate = ff_mqc_flush_to(&t1->mqc, cblk->passes[passno-1].flushed, &cblk->passes[passno-1].flushed_len);
695 cblk->passes[passno-1].rate -= cblk->passes[passno-1].flushed_len;
696 }
697}
698
699/* tier-2 routines: */
700
702{
703 if (n == 1)
704 put_num(s, 0, 1);
705 else if (n == 2)
706 put_num(s, 2, 2);
707 else if (n <= 5)
708 put_num(s, 0xc | (n-3), 4);
709 else if (n <= 36)
710 put_num(s, 0x1e0 | (n-6), 9);
711 else
712 put_num(s, 0xff80 | (n-37), 16);
713}
714
715
717 int precno, const uint8_t *expn, int numgbits, int packetno,
718 int nlayers)
719{
720 int bandno, empty = 1;
721 int i;
722 // init bitstream
723 *s->buf = 0;
724 s->bit_index = 0;
725
726 if (s->sop) {
727 bytestream_put_be16(&s->buf, JPEG2000_SOP);
728 bytestream_put_be16(&s->buf, 4);
729 bytestream_put_be16(&s->buf, packetno);
730 }
731 // header
732
733 if (!layno) {
734 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
735 Jpeg2000Band *band = rlevel->band + bandno;
736 if (band->coord[0][0] < band->coord[0][1]
737 && band->coord[1][0] < band->coord[1][1]) {
738 Jpeg2000Prec *prec = band->prec + precno;
739 int nb_cblks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
740 int pos;
743 for (pos = 0; pos < nb_cblks; pos++) {
744 Jpeg2000Cblk *cblk = &prec->cblk[pos];
745 prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - cblk->nonzerobits;
746 cblk->incl = 0;
747 cblk->lblock = 3;
749 for (i = 0; i < nlayers; i++) {
750 if (cblk->layers[i].npasses > 0) {
751 prec->cblkincl[pos].val = i;
752 break;
753 }
754 }
755 if (i == nlayers)
756 prec->cblkincl[pos].val = i;
758 }
759 }
760 }
761 }
762
763 // is the packet empty?
764 for (bandno = 0; bandno < rlevel->nbands; bandno++){
765 Jpeg2000Band *band = rlevel->band + bandno;
766 if (band->coord[0][0] < band->coord[0][1]
767 && band->coord[1][0] < band->coord[1][1]) {
768 Jpeg2000Prec *prec = band->prec + precno;
769 int nb_cblks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
770 int pos;
771 for (pos = 0; pos < nb_cblks; pos++) {
772 Jpeg2000Cblk *cblk = &prec->cblk[pos];
773 if (cblk->layers[layno].npasses) {
774 empty = 0;
775 break;
776 }
777 }
778 if (!empty)
779 break;
780 }
781 }
782
783 put_bits(s, !empty, 1);
784 if (empty){
785 j2k_flush(s);
786 if (s->eph)
787 bytestream_put_be16(&s->buf, JPEG2000_EPH);
788 return 0;
789 }
790
791 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
792 Jpeg2000Band *band = rlevel->band + bandno;
793 Jpeg2000Prec *prec = band->prec + precno;
794 int yi, xi, pos;
795 int cblknw = prec->nb_codeblocks_width;
796
797 if (band->coord[0][0] == band->coord[0][1]
798 || band->coord[1][0] == band->coord[1][1])
799 continue;
800
801 for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++) {
802 for (xi = 0; xi < cblknw; xi++, pos++){
803 int llen = 0, length;
804 Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
805
806 if (s->buf_end - s->buf < 20) // approximately
807 return -1;
808
809 // inclusion information
810 if (!cblk->incl)
811 tag_tree_code(s, prec->cblkincl + pos, layno + 1);
812 else {
813 put_bits(s, cblk->layers[layno].npasses > 0, 1);
814 }
815
816 if (!cblk->layers[layno].npasses)
817 continue;
818
819 // zerobits information
820 if (!cblk->incl) {
821 tag_tree_code(s, prec->zerobits + pos, 100);
822 cblk->incl = 1;
823 }
824
825 // number of passes
826 putnumpasses(s, cblk->layers[layno].npasses);
827
828 length = cblk->layers[layno].data_len;
829 if (layno == nlayers - 1 && cblk->layers[layno].cum_passes){
830 length += cblk->passes[cblk->layers[layno].cum_passes-1].flushed_len;
831 }
832 if (cblk->lblock + av_log2(cblk->layers[layno].npasses) < av_log2(length) + 1) {
833 llen = av_log2(length) + 1 - cblk->lblock - av_log2(cblk->layers[layno].npasses);
834 }
835
836 // length of code block
837 cblk->lblock += llen;
838 put_bits(s, 1, llen);
839 put_bits(s, 0, 1);
840 put_num(s, length, cblk->lblock + av_log2(cblk->layers[layno].npasses));
841 }
842 }
843 }
844 j2k_flush(s);
845 if (s->eph) {
846 bytestream_put_be16(&s->buf, JPEG2000_EPH);
847 }
848
849 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
850 Jpeg2000Band *band = rlevel->band + bandno;
851 Jpeg2000Prec *prec = band->prec + precno;
852 int yi, cblknw = prec->nb_codeblocks_width;
853 for (yi =0; yi < prec->nb_codeblocks_height; yi++) {
854 int xi;
855 for (xi = 0; xi < cblknw; xi++){
856 Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
857 if (cblk->layers[layno].npasses) {
858 if (s->buf_end - s->buf < cblk->layers[layno].data_len + 2)
859 return -1;
860 bytestream_put_buffer(&s->buf, cblk->layers[layno].data_start + 1, cblk->layers[layno].data_len);
861 if (layno == nlayers - 1 && cblk->layers[layno].cum_passes) {
862 bytestream_put_buffer(&s->buf, cblk->passes[cblk->layers[layno].cum_passes-1].flushed,
863 cblk->passes[cblk->layers[layno].cum_passes-1].flushed_len);
864 }
865 }
866 }
867 }
868 }
869 return 0;
870}
871
872static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno, int nlayers)
873{
874 int compno, reslevelno, layno, ret;
875 Jpeg2000CodingStyle *codsty = &s->codsty;
876 Jpeg2000QuantStyle *qntsty = &s->qntsty;
877 int packetno = 0;
878 int step_x, step_y;
879 int x, y;
880 int tile_coord[2][2];
881 int col = tileno % s->numXtiles;
882 int row = tileno / s->numXtiles;
883
884 tile_coord[0][0] = col * s->tile_width;
885 tile_coord[0][1] = FFMIN(tile_coord[0][0] + s->tile_width, s->width);
886 tile_coord[1][0] = row * s->tile_height;
887 tile_coord[1][1] = FFMIN(tile_coord[1][0] + s->tile_height, s->height);
888
889 av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
890 // lay-rlevel-comp-pos progression
891 switch (s->prog) {
893 for (layno = 0; layno < nlayers; layno++) {
894 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
895 for (compno = 0; compno < s->ncomponents; compno++){
896 int precno;
897 Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
898 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
899 if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
900 qntsty->nguardbits, packetno++, nlayers)) < 0)
901 return ret;
902 }
903 }
904 }
905 }
906 break;
908 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
909 for (layno = 0; layno < nlayers; layno++) {
910 for (compno = 0; compno < s->ncomponents; compno++){
911 int precno;
912 Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
913 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
914 if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
915 qntsty->nguardbits, packetno++, nlayers)) < 0)
916 return ret;
917 }
918 }
919 }
920 }
921 break;
923 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
924 int precno;
925 step_x = 30;
926 step_y = 30;
927 for (compno = 0; compno < s->ncomponents; compno++) {
928 Jpeg2000Component *comp = tile->comp + compno;
929 if (reslevelno < codsty->nreslevels) {
930 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
931 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
932 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
933 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
934 }
935 }
936
937 step_x = 1<<step_x;
938 step_y = 1<<step_y;
939 for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
940 for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
941 for (compno = 0; compno < s->ncomponents; compno++) {
942 Jpeg2000Component *comp = tile->comp + compno;
943 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
944 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
945 int log_subsampling[2] = { (compno+1&2)?s->chroma_shift[0]:0, (compno+1&2)?s->chroma_shift[1]:0};
946 unsigned prcx, prcy;
947 int trx0, try0;
948
949 trx0 = ff_jpeg2000_ceildivpow2(tile_coord[0][0], log_subsampling[0] + reducedresno);
950 try0 = ff_jpeg2000_ceildivpow2(tile_coord[1][0], log_subsampling[1] + reducedresno);
951
952 if (!(y % ((uint64_t)1 << (reslevel->log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
953 (y == tile_coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_height)))))
954 continue;
955
956 if (!(x % ((uint64_t)1 << (reslevel->log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
957 (x == tile_coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_width)))))
958 continue;
959
960 // check if a precinct exists
961 prcx = ff_jpeg2000_ceildivpow2(x, log_subsampling[0] + reducedresno) >> reslevel->log2_prec_width;
962 prcy = ff_jpeg2000_ceildivpow2(y, log_subsampling[1] + reducedresno) >> reslevel->log2_prec_height;
963 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> reslevel->log2_prec_width;
964 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> reslevel->log2_prec_height;
965 precno = prcx + reslevel->num_precincts_x * prcy;
966
967 if (prcx >= reslevel->num_precincts_x || prcy >= reslevel->num_precincts_y) {
968 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
969 prcx, prcy, reslevel->num_precincts_x, reslevel->num_precincts_y);
970 continue;
971 }
972 for (layno = 0; layno < nlayers; layno++) {
973 if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
974 qntsty->nguardbits, packetno++, nlayers)) < 0)
975 return ret;
976 }
977 }
978 }
979 }
980 }
981 break;
983 step_x = 32;
984 step_y = 32;
985 for (compno = 0; compno < s->ncomponents; compno++) {
986 Jpeg2000Component *comp = tile->comp + compno;
987
988 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
989 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
990 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
991 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
992 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
993 }
994 }
995 if (step_x >= 31 || step_y >= 31){
996 avpriv_request_sample(s->avctx, "PCRL with large step");
998 }
999 step_x = 1<<step_x;
1000 step_y = 1<<step_y;
1001
1002 for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
1003 for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
1004 for (compno = 0; compno < s->ncomponents; compno++) {
1005 Jpeg2000Component *comp = tile->comp + compno;
1006 int log_subsampling[2] = { (compno+1&2)?s->chroma_shift[0]:0, (compno+1&2)?s->chroma_shift[1]:0};
1007
1008 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1009 unsigned prcx, prcy;
1010 int precno;
1011 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1012 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
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
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_CPRL:
1050 for (compno = 0; compno < s->ncomponents; compno++) {
1051 Jpeg2000Component *comp = tile->comp + compno;
1052 int log_subsampling[2] = { (compno+1&2)?s->chroma_shift[0]:0, (compno+1&2)?s->chroma_shift[1]:0};
1053 step_x = 32;
1054 step_y = 32;
1055
1056 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1057 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1058 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
1059 step_x = FFMIN(step_x, rlevel->log2_prec_width + reducedresno);
1060 step_y = FFMIN(step_y, rlevel->log2_prec_height + reducedresno);
1061 }
1062 if (step_x >= 31 || step_y >= 31){
1063 avpriv_request_sample(s->avctx, "CPRL 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 (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
1072 unsigned prcx, prcy;
1073 int precno;
1074 int trx0, try0;
1075 uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; // ==> N_L - r
1076 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1077
1078 trx0 = ff_jpeg2000_ceildivpow2(tile_coord[0][0], log_subsampling[0] + reducedresno);
1079 try0 = ff_jpeg2000_ceildivpow2(tile_coord[1][0], log_subsampling[1] + reducedresno);
1080
1081 if (!(y % ((uint64_t)1 << (reslevel->log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
1082 (y == tile_coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_height)))))
1083 continue;
1084
1085 if (!(x % ((uint64_t)1 << (reslevel->log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
1086 (x == tile_coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + reslevel->log2_prec_width)))))
1087 continue;
1088
1089 // check if a precinct exists
1090 prcx = ff_jpeg2000_ceildivpow2(x, log_subsampling[0] + reducedresno) >> reslevel->log2_prec_width;
1091 prcy = ff_jpeg2000_ceildivpow2(y, log_subsampling[1] + reducedresno) >> reslevel->log2_prec_height;
1092 prcx -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], reducedresno) >> reslevel->log2_prec_width;
1093 prcy -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], reducedresno) >> reslevel->log2_prec_height;
1094
1095 precno = prcx + reslevel->num_precincts_x * prcy;
1096
1097 if (prcx >= reslevel->num_precincts_x || prcy >= reslevel->num_precincts_y) {
1098 av_log(s->avctx, AV_LOG_WARNING, "prc %d %d outside limits %d %d\n",
1099 prcx, prcy, reslevel->num_precincts_x, reslevel->num_precincts_y);
1100 continue;
1101 }
1102 for (layno = 0; layno < nlayers; layno++) {
1103 if ((ret = encode_packet(s, reslevel, layno, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
1104 qntsty->nguardbits, packetno++, nlayers)) < 0)
1105 return ret;
1106 }
1107 }
1108 }
1109 }
1110 }
1111
1112 }
1113
1114 av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
1115 return 0;
1116}
1117
1118static void makelayer(Jpeg2000EncoderContext *s, int layno, double thresh, Jpeg2000Tile* tile, int final)
1119{
1120 int compno, resno, bandno, precno, cblkno;
1121 int passno;
1122
1123 for (compno = 0; compno < s->ncomponents; compno++) {
1124 Jpeg2000Component *comp = &tile->comp[compno];
1125
1126 for (resno = 0; resno < s->codsty.nreslevels; resno++) {
1127 Jpeg2000ResLevel *reslevel = comp->reslevel + resno;
1128
1129 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
1130 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
1131 Jpeg2000Band *band = reslevel->band + bandno;
1132 Jpeg2000Prec *prec = band->prec + precno;
1133
1134 for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
1135 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1136 Jpeg2000Layer *layer = &cblk->layers[layno];
1137 int n;
1138
1139 if (layno == 0) {
1140 cblk->ninclpasses = 0;
1141 }
1142
1143 n = cblk->ninclpasses;
1144
1145 if (thresh < 0) {
1146 n = cblk->npasses;
1147 } else {
1148 for (passno = cblk->ninclpasses; passno < cblk->npasses; passno++) {
1149 int32_t dr;
1150 double dd;
1151 Jpeg2000Pass *pass = &cblk->passes[passno];
1152
1153 if (n == 0) {
1154 dr = pass->rate;
1155 dd = pass->disto;
1156 } else {
1157 dr = pass->rate - cblk->passes[n - 1].rate;
1158 dd = pass->disto - cblk->passes[n-1].disto;
1159 }
1160
1161 if (!dr) {
1162 if (dd != 0.0) {
1163 n = passno + 1;
1164 }
1165 continue;
1166 }
1167
1168 if (thresh - (dd / dr) < DBL_EPSILON)
1169 n = passno + 1;
1170 }
1171 }
1172 layer->npasses = n - cblk->ninclpasses;
1173 layer->cum_passes = n;
1174
1175 if (layer->npasses == 0) {
1176 layer->disto = 0;
1177 layer->data_len = 0;
1178 continue;
1179 }
1180
1181 if (cblk->ninclpasses == 0) {
1182 layer->data_len = cblk->passes[n - 1].rate;
1183 layer->data_start = cblk->data;
1184 layer->disto = cblk->passes[n - 1].disto;
1185 } else {
1186 layer->data_len = cblk->passes[n - 1].rate - cblk->passes[cblk->ninclpasses - 1].rate;
1187 layer->data_start = cblk->data + cblk->passes[cblk->ninclpasses - 1].rate;
1188 layer->disto = cblk->passes[n - 1].disto -
1189 cblk->passes[cblk->ninclpasses - 1].disto;
1190 }
1191 if (final) {
1192 cblk->ninclpasses = n;
1193 }
1194 }
1195 }
1196 }
1197 }
1198 }
1199}
1200
1202{
1203 int precno, compno, reslevelno, bandno, cblkno, passno, layno;
1204 int i;
1205 double min = DBL_MAX;
1206 double max = 0;
1207 double thresh;
1208
1209 Jpeg2000CodingStyle *codsty = &s->codsty;
1210
1211 for (compno = 0; compno < s->ncomponents; compno++){
1212 Jpeg2000Component *comp = tile->comp + compno;
1213
1214 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
1215 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1216
1217 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
1218 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
1219 Jpeg2000Band *band = reslevel->band + bandno;
1220 Jpeg2000Prec *prec = band->prec + precno;
1221
1222 for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
1223 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1224 for (passno = 0; passno < cblk->npasses; passno++) {
1225 Jpeg2000Pass *pass = &cblk->passes[passno];
1226 int dr;
1227 double dd, drslope;
1228
1229 if (passno == 0) {
1230 dr = (int32_t)pass->rate;
1231 dd = pass->disto;
1232 } else {
1233 dr = (int32_t)(pass->rate - cblk->passes[passno - 1].rate);
1234 dd = pass->disto - cblk->passes[passno - 1].disto;
1235 }
1236
1237 if (dr <= 0)
1238 continue;
1239
1240 drslope = dd / dr;
1241 if (drslope < min)
1242 min = drslope;
1243
1244 if (drslope > max)
1245 max = drslope;
1246 }
1247 }
1248 }
1249 }
1250 }
1251 }
1252
1253 for (layno = 0; layno < s->nlayers; layno++) {
1254 double lo = min;
1255 double hi = max;
1256 double stable_thresh = 0.0;
1257 double good_thresh = 0.0;
1258 if (!s->layer_rates[layno]) {
1259 good_thresh = -1.0;
1260 } else {
1261 for (i = 0; i < 128; i++) {
1262 uint8_t *stream_pos = s->buf;
1263 int ret;
1264 thresh = (lo + hi) / 2;
1265 makelayer(s, layno, thresh, tile, 0);
1266 ret = encode_packets(s, tile, (int)(tile - s->tile), layno + 1);
1267 memset(stream_pos, 0, s->buf - stream_pos);
1268 if ((s->buf - stream_pos > ceil(tile->layer_rates[layno])) || ret < 0) {
1269 lo = thresh;
1270 s->buf = stream_pos;
1271 continue;
1272 }
1273 hi = thresh;
1274 stable_thresh = thresh;
1275 s->buf = stream_pos;
1276 }
1277 }
1278 if (good_thresh >= 0.0)
1279 good_thresh = stable_thresh == 0.0 ? thresh : stable_thresh;
1280 makelayer(s, layno, good_thresh, tile, 1);
1281 }
1282}
1283
1284static int getcut(Jpeg2000Cblk *cblk, uint64_t lambda)
1285{
1286 int passno, res = 0;
1287 for (passno = 0; passno < cblk->npasses; passno++){
1288 int dr;
1289 int64_t dd;
1290
1291 dr = cblk->passes[passno].rate
1292 - (res ? cblk->passes[res-1].rate : 0);
1293 dd = cblk->passes[passno].disto
1294 - (res ? cblk->passes[res-1].disto : 0);
1295
1296 if (dd >= dr * lambda)
1297 res = passno+1;
1298 }
1299 return res;
1300}
1301
1303{
1304 int precno, compno, reslevelno, bandno, cblkno, lev;
1305 Jpeg2000CodingStyle *codsty = &s->codsty;
1306
1307 for (compno = 0; compno < s->ncomponents; compno++){
1308 Jpeg2000Component *comp = tile->comp + compno;
1309
1310 for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
1311 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1312
1313 for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
1314 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
1315 int bandpos = bandno + (reslevelno > 0);
1316 Jpeg2000Band *band = reslevel->band + bandno;
1317 Jpeg2000Prec *prec = band->prec + precno;
1318
1319 int64_t dwt_norm = dwt_norms[codsty->transform == FF_DWT53][bandpos][lev] * (int64_t)band->i_stepsize >> 15;
1320 int64_t lambda_prime = av_rescale(s->lambda, 1 << WMSEDEC_SHIFT, dwt_norm * dwt_norm);
1321 for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
1322 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
1323
1324 cblk->ninclpasses = getcut(cblk, lambda_prime);
1325 cblk->layers[0].data_start = cblk->data;
1326 cblk->layers[0].cum_passes = cblk->ninclpasses;
1327 cblk->layers[0].npasses = cblk->ninclpasses;
1328 if (cblk->ninclpasses)
1329 cblk->layers[0].data_len = cblk->passes[cblk->ninclpasses - 1].rate;
1330 }
1331 }
1332 }
1333 }
1334 }
1335}
1336
1338{
1339 int compno, reslevelno, bandno, ret;
1341 Jpeg2000CodingStyle *codsty = &s->codsty;
1342 for (compno = 0; compno < s->ncomponents; compno++){
1343 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
1344
1345 t1.stride = (1<<codsty->log2_cblk_width) + 2;
1346
1347 av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
1348 if ((ret = ff_dwt_encode(&comp->dwt, comp->i_data)) < 0)
1349 return ret;
1350 av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
1351
1352 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
1353 Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
1354
1355 for (bandno = 0; bandno < reslevel->nbands ; bandno++){
1356 Jpeg2000Band *band = reslevel->band + bandno;
1357 Jpeg2000Prec *prec = band->prec; // we support only 1 precinct per band ATM in the encoder
1358 int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
1359 yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
1360 y0 = yy0;
1361 yy1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[1][0] + 1, band->log2_cblk_height) << band->log2_cblk_height,
1362 band->coord[1][1]) - band->coord[1][0] + yy0;
1363
1364 if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
1365 continue;
1366
1367 bandpos = bandno + (reslevelno > 0);
1368
1369 for (cblky = 0; cblky < prec->nb_codeblocks_height; cblky++){
1370 if (reslevelno == 0 || bandno == 1)
1371 xx0 = 0;
1372 else
1373 xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
1374 x0 = xx0;
1375 xx1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[0][0] + 1, band->log2_cblk_width) << band->log2_cblk_width,
1376 band->coord[0][1]) - band->coord[0][0] + xx0;
1377
1378 for (cblkx = 0; cblkx < prec->nb_codeblocks_width; cblkx++, cblkno++){
1379 int y, x;
1380 if (codsty->transform == FF_DWT53){
1381 for (y = yy0; y < yy1; y++){
1382 int *ptr = t1.data + (y-yy0)*t1.stride;
1383 for (x = xx0; x < xx1; x++){
1384 *ptr++ = comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] * (1 << NMSEDEC_FRACBITS);
1385 }
1386 }
1387 } else{
1388 int64_t multiplier = (int64_t)(16384 * 65536 / band->i_stepsize);
1389 for (y = yy0; y < yy1; y++){
1390 int *ptr = t1.data + (y-yy0)*t1.stride;
1391 for (x = xx0; x < xx1; x++){
1392 *ptr = (comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]);
1393 *ptr = (int64_t)*ptr * multiplier >> 15 - NMSEDEC_FRACBITS;
1394 ptr++;
1395 }
1396 }
1397 }
1398 if (!prec->cblk[cblkno].data)
1399 prec->cblk[cblkno].data = av_malloc(1 + 8192);
1400 if (!prec->cblk[cblkno].passes)
1401 prec->cblk[cblkno].passes = av_malloc_array(JPEG2000_MAX_PASSES, sizeof (*prec->cblk[cblkno].passes));
1402 if (!prec->cblk[cblkno].data || !prec->cblk[cblkno].passes)
1403 return AVERROR(ENOMEM);
1404 encode_cblk(s, &t1, prec->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
1405 bandpos, codsty->nreslevels - reslevelno - 1);
1406 xx0 = xx1;
1407 xx1 = FFMIN(xx1 + (1 << band->log2_cblk_width), band->coord[0][1] - band->coord[0][0] + x0);
1408 }
1409 yy0 = yy1;
1410 yy1 = FFMIN(yy1 + (1 << band->log2_cblk_height), band->coord[1][1] - band->coord[1][0] + y0);
1411 }
1412 }
1413 }
1414 av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
1415 }
1416
1417 av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
1418 if (s->compression_rate_enc)
1419 makelayers(s, tile);
1420 else
1421 truncpasses(s, tile);
1422
1423 if ((ret = encode_packets(s, tile, tileno, s->nlayers)) < 0)
1424 return ret;
1425 av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
1426 return 0;
1427}
1428
1430{
1431 int tileno, compno;
1432 Jpeg2000CodingStyle *codsty = &s->codsty;
1433
1434 if (!s->tile)
1435 return;
1436 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1437 if (s->tile[tileno].comp) {
1438 for (compno = 0; compno < s->ncomponents; compno++){
1439 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
1440 ff_jpeg2000_cleanup(comp, codsty);
1441 }
1442 av_freep(&s->tile[tileno].comp);
1443 }
1444 av_freep(&s->tile[tileno].layer_rates);
1445 }
1446 av_freep(&s->tile);
1447}
1448
1450{
1451 int tileno, compno;
1452 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1453 Jpeg2000Tile *tile = s->tile + tileno;
1454 for (compno = 0; compno < s->ncomponents; compno++)
1455 ff_jpeg2000_reinit(tile->comp + compno, &s->codsty);
1456 }
1457}
1458
1459static void update_size(uint8_t *size, const uint8_t *end)
1460{
1461 AV_WB32(size, end-size);
1462}
1463
1465 const AVFrame *pict, int *got_packet)
1466{
1467 int tileno, ret;
1469 uint8_t *chunkstart, *jp2cstart, *jp2hstart;
1471
1472 if ((ret = ff_alloc_packet(avctx, pkt, avctx->width*avctx->height*9 + FF_INPUT_BUFFER_MIN_SIZE)) < 0)
1473 return ret;
1474
1475 // init:
1476 s->buf = s->buf_start = pkt->data;
1477 s->buf_end = pkt->data + pkt->size;
1478
1479 s->picture = pict;
1480
1481 s->lambda = s->picture->quality * LAMBDA_SCALE;
1482
1483 if (s->cbps[0] > 8)
1484 copy_frame_16(s);
1485 else
1486 copy_frame_8(s);
1487
1488 reinit(s);
1489
1490 if (s->format == CODEC_JP2) {
1491 av_assert0(s->buf == pkt->data);
1492
1493 bytestream_put_be32(&s->buf, 0x0000000C);
1494 bytestream_put_be32(&s->buf, 0x6A502020);
1495 bytestream_put_be32(&s->buf, 0x0D0A870A);
1496
1497 chunkstart = s->buf;
1498 bytestream_put_be32(&s->buf, 0);
1499 bytestream_put_buffer(&s->buf, "ftyp", 4);
1500 bytestream_put_buffer(&s->buf, "jp2\040\040", 4);
1501 bytestream_put_be32(&s->buf, 0);
1502 bytestream_put_buffer(&s->buf, "jp2\040", 4);
1503 update_size(chunkstart, s->buf);
1504
1505 jp2hstart = s->buf;
1506 bytestream_put_be32(&s->buf, 0);
1507 bytestream_put_buffer(&s->buf, "jp2h", 4);
1508
1509 chunkstart = s->buf;
1510 bytestream_put_be32(&s->buf, 0);
1511 bytestream_put_buffer(&s->buf, "ihdr", 4);
1512 bytestream_put_be32(&s->buf, avctx->height);
1513 bytestream_put_be32(&s->buf, avctx->width);
1514 bytestream_put_be16(&s->buf, s->ncomponents);
1515 bytestream_put_byte(&s->buf, s->cbps[0]);
1516 bytestream_put_byte(&s->buf, 7);
1517 bytestream_put_byte(&s->buf, 0);
1518 bytestream_put_byte(&s->buf, 0);
1519 update_size(chunkstart, s->buf);
1520
1521 chunkstart = s->buf;
1522 bytestream_put_be32(&s->buf, 0);
1523 bytestream_put_buffer(&s->buf, "colr", 4);
1524 bytestream_put_byte(&s->buf, 1);
1525 bytestream_put_byte(&s->buf, 0);
1526 bytestream_put_byte(&s->buf, 0);
1527 if ((desc->flags & AV_PIX_FMT_FLAG_RGB) || avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1528 bytestream_put_be32(&s->buf, 16);
1529 } else if (s->ncomponents == 1) {
1530 bytestream_put_be32(&s->buf, 17);
1531 } else {
1532 bytestream_put_be32(&s->buf, 18);
1533 }
1534 update_size(chunkstart, s->buf);
1535 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
1536 int i;
1537 const uint8_t *palette = pict->data[1];
1538 chunkstart = s->buf;
1539 bytestream_put_be32(&s->buf, 0);
1540 bytestream_put_buffer(&s->buf, "pclr", 4);
1541 bytestream_put_be16(&s->buf, AVPALETTE_COUNT);
1542 bytestream_put_byte(&s->buf, 3); // colour channels
1543 bytestream_put_be24(&s->buf, 0x070707); //colour depths
1544 for (i = 0; i < AVPALETTE_COUNT; i++) {
1545 bytestream_put_be24(&s->buf, HAVE_BIGENDIAN ? AV_RB24(palette + 1) : AV_RL24(palette));
1546 palette += 4;
1547 }
1548 update_size(chunkstart, s->buf);
1549 chunkstart = s->buf;
1550 bytestream_put_be32(&s->buf, 0);
1551 bytestream_put_buffer(&s->buf, "cmap", 4);
1552 for (i = 0; i < 3; i++) {
1553 bytestream_put_be16(&s->buf, 0); // component
1554 bytestream_put_byte(&s->buf, 1); // palette mapping
1555 bytestream_put_byte(&s->buf, i); // index
1556 }
1557 update_size(chunkstart, s->buf);
1558 }
1559 update_size(jp2hstart, s->buf);
1560
1561 jp2cstart = s->buf;
1562 bytestream_put_be32(&s->buf, 0);
1563 bytestream_put_buffer(&s->buf, "jp2c", 4);
1564 }
1565
1566 if (s->buf_end - s->buf < 2)
1567 return -1;
1568 bytestream_put_be16(&s->buf, JPEG2000_SOC);
1569 if ((ret = put_siz(s)) < 0)
1570 return ret;
1571 if ((ret = put_cod(s)) < 0)
1572 return ret;
1573 if ((ret = put_qcd(s, 0)) < 0)
1574 return ret;
1575 if ((ret = put_com(s, 0)) < 0)
1576 return ret;
1577
1578 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
1579 uint8_t *psotptr;
1580 if (!(psotptr = put_sot(s, tileno)))
1581 return -1;
1582 if (s->buf_end - s->buf < 2)
1583 return -1;
1584 bytestream_put_be16(&s->buf, JPEG2000_SOD);
1585 if ((ret = encode_tile(s, s->tile + tileno, tileno)) < 0)
1586 return ret;
1587 bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
1588 }
1589 if (s->buf_end - s->buf < 2)
1590 return -1;
1591 bytestream_put_be16(&s->buf, JPEG2000_EOC);
1592
1593 if (s->format == CODEC_JP2)
1594 update_size(jp2cstart, s->buf);
1595
1596 av_log(s->avctx, AV_LOG_DEBUG, "end\n");
1597 pkt->size = s->buf - s->buf_start;
1598 *got_packet = 1;
1599
1600 return 0;
1601}
1602
1604{
1605 int i;
1606 char *token;
1607 char *saveptr = NULL;
1608 int rate;
1609 int nlayers = 0;
1610 if (!s->lr_str) {
1611 s->nlayers = 1;
1612 s->layer_rates[0] = 0;
1613 s->compression_rate_enc = 0;
1614 return 0;
1615 }
1616
1617 token = av_strtok(s->lr_str, ",", &saveptr);
1618 if (token && (rate = strtol(token, NULL, 10))) {
1619 s->layer_rates[0] = rate <= 1 ? 0:rate;
1620 nlayers++;
1621 } else {
1622 return AVERROR_INVALIDDATA;
1623 }
1624
1625 while (1) {
1626 token = av_strtok(NULL, ",", &saveptr);
1627 if (!token)
1628 break;
1629 if (rate = strtol(token, NULL, 10)) {
1630 if (nlayers >= 100) {
1631 return AVERROR_INVALIDDATA;
1632 }
1633 s->layer_rates[nlayers] = rate <= 1 ? 0:rate;
1634 nlayers++;
1635 } else {
1636 return AVERROR_INVALIDDATA;
1637 }
1638 }
1639
1640 for (i = 1; i < nlayers; i++) {
1641 if (s->layer_rates[i] >= s->layer_rates[i-1]) {
1642 return AVERROR_INVALIDDATA;
1643 }
1644 }
1645 s->nlayers = nlayers;
1646 s->compression_rate_enc = 1;
1647 return 0;
1648}
1649
1651{
1652 static AVOnce init_static_once = AV_ONCE_INIT;
1653 int i, ret;
1655 Jpeg2000CodingStyle *codsty = &s->codsty;
1656 Jpeg2000QuantStyle *qntsty = &s->qntsty;
1658
1659 s->avctx = avctx;
1660 av_log(s->avctx, AV_LOG_DEBUG, "init\n");
1661 if (parse_layer_rates(s)) {
1662 av_log(avctx, AV_LOG_WARNING, "Layer rates invalid. Encoding with 1 layer based on quality metric.\n");
1663 s->nlayers = 1;
1664 s->layer_rates[0] = 0;
1665 s->compression_rate_enc = 0;
1666 }
1667
1668 if (avctx->pix_fmt == AV_PIX_FMT_PAL8 && (s->pred != FF_DWT97_INT || s->format != CODEC_JP2)) {
1669 av_log(s->avctx, AV_LOG_WARNING, "Forcing lossless jp2 for pal8\n");
1670 s->pred = 1;
1671 s->format = CODEC_JP2;
1672 }
1673
1674 // defaults:
1675 // TODO: implement setting non-standard precinct size
1676 memset(codsty->log2_prec_widths , 15, sizeof(codsty->log2_prec_widths ));
1677 memset(codsty->log2_prec_heights, 15, sizeof(codsty->log2_prec_heights));
1678 codsty->nreslevels2decode=
1679 codsty->nreslevels = 7;
1680 codsty->nlayers = s->nlayers;
1681 codsty->log2_cblk_width = 4;
1682 codsty->log2_cblk_height = 4;
1683 codsty->transform = s->pred ? FF_DWT53 : FF_DWT97_INT;
1684
1685 qntsty->nguardbits = 1;
1686
1687 if ((s->tile_width & (s->tile_width -1)) ||
1688 (s->tile_height & (s->tile_height-1))) {
1689 av_log(avctx, AV_LOG_WARNING, "Tile dimension not a power of 2\n");
1690 }
1691
1692 if (codsty->transform == FF_DWT53)
1693 qntsty->quantsty = JPEG2000_QSTY_NONE;
1694 else
1695 qntsty->quantsty = JPEG2000_QSTY_SE;
1696
1697 s->width = avctx->width;
1698 s->height = avctx->height;
1699
1700 s->ncomponents = desc->nb_components;
1701 for (i = 0; i < 4; i++) {
1702 s->cbps[i] = desc->comp[i].depth;
1703 s->comp_remap[i] = i; //default
1704 }
1705
1706 if ((desc->flags & AV_PIX_FMT_FLAG_PLANAR) && s->ncomponents > 1) {
1707 s->planar = 1;
1709 s->chroma_shift, s->chroma_shift + 1);
1710 if (ret)
1711 return ret;
1712 if (desc->flags & AV_PIX_FMT_FLAG_RGB) {
1713 s->comp_remap[0] = 2;
1714 s->comp_remap[1] = 0;
1715 s->comp_remap[2] = 1;
1716 }
1717 }
1718
1719 ff_thread_once(&init_static_once, init_luts);
1720
1722 if ((ret=init_tiles(s)) < 0)
1723 return ret;
1724
1725 av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
1726
1727 return 0;
1728}
1729
1731{
1733
1734 cleanup(s);
1735 return 0;
1736}
1737
1738// taken from the libopenjpeg wrapper so it matches
1739
1740#define OFFSET(x) offsetof(Jpeg2000EncoderContext, x)
1741#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1742static const AVOption options[] = {
1743 { "format", "Codec Format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = CODEC_JP2 }, CODEC_J2K, CODEC_JP2, VE, .unit = "format" },
1744 { "j2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_J2K }, 0, 0, VE, .unit = "format" },
1745 { "jp2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_JP2 }, 0, 0, VE, .unit = "format" },
1746 { "tile_width", "Tile Width", OFFSET(tile_width), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, 1<<30, VE, },
1747 { "tile_height", "Tile Height", OFFSET(tile_height), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, 1<<30, VE, },
1748 { "pred", "DWT Type", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, .unit = "pred" },
1749 { "dwt97int", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, .unit = "pred" },
1750 { "dwt53", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, .unit = "pred" },
1751 { "sop", "SOP marker", OFFSET(sop), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, },
1752 { "eph", "EPH marker", OFFSET(eph), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, },
1753 { "prog", "Progression Order", OFFSET(prog), AV_OPT_TYPE_INT, { .i64 = 0 }, JPEG2000_PGOD_LRCP, JPEG2000_PGOD_CPRL, VE, .unit = "prog" },
1754 { "lrcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_LRCP }, 0, 0, VE, .unit = "prog" },
1755 { "rlcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_RLCP }, 0, 0, VE, .unit = "prog" },
1756 { "rpcl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_RPCL }, 0, 0, VE, .unit = "prog" },
1757 { "pcrl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_PCRL }, 0, 0, VE, .unit = "prog" },
1758 { "cprl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = JPEG2000_PGOD_CPRL }, 0, 0, VE, .unit = "prog" },
1759 { "layer_rates", "Layer Rates", OFFSET(lr_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VE },
1760 { NULL }
1761};
1762
1763static const AVClass j2k_class = {
1764 .class_name = "jpeg 2000 encoder",
1765 .item_name = av_default_item_name,
1766 .option = options,
1767 .version = LIBAVUTIL_VERSION_INT,
1768};
1769
1771 .p.name = "jpeg2000",
1772 CODEC_LONG_NAME("JPEG 2000"),
1773 .p.type = AVMEDIA_TYPE_VIDEO,
1774 .p.id = AV_CODEC_ID_JPEG2000,
1777 .priv_data_size = sizeof(Jpeg2000EncoderContext),
1778 .init = j2kenc_init,
1780 .close = j2kenc_destroy,
1797
1799 .color_ranges = AVCOL_RANGE_MPEG,
1800 .p.priv_class = &j2k_class,
1801 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1802};
static double val(void *priv, double ch)
Definition aeval.c:77
static const char *const format[]
Definition af_aiir.c:444
const FFCodec ff_jpeg2000_encoder
Definition j2kenc.c:1770
#define VE
Definition amfenc_av1.c:30
int32_t
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition bytestream.h:372
static int FUNC tile(CodedBitstreamContext *ctx, RWContext *rw, APVRawTile *current, int tile_idx, uint32_t tile_size)
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define xi(width, name, var, range_min, range_max, subs,...)
Definition cbs_h264.c:115
#define bit(string, value)
Definition cbs_mpeg2.c:56
#define ss(width, name, subs,...)
Definition cbs_vp9.c:202
#define s(width, name)
Definition cbs_vp9.c:198
static LevelCodes lev[4+3+3]
Definition clearvideo.c:80
#define CODEC_PIXFMTS(...)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
common internal and external API header
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static __device__ float ceil(float a)
#define min(a, b)
#define max(a, b)
static AVPacket * pkt
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
Definition eamad.c:79
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition encode.c:62
#define FF_INPUT_BUFFER_MIN_SIZE
Used by some encoders as upper bound for the length of headers.
Definition encode.h:34
static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame, AVPacket *pkt)
Definition ffmpeg_enc.c:694
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition codec.h:147
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition avcodec.h:322
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition codec.h:98
@ AV_CODEC_ID_JPEG2000
Definition codec_id.h:138
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok().
Definition avstring.c:179
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
int a
#define av_log2
Definition intmath.h:84
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
#define AV_RL24(x)
#define AV_WB32(p, v)
#define AV_RB24(x)
static int init_tiles(Jpeg2000EncoderContext *s)
compute the sizes of tiles, resolution levels, bands, etc.
Definition j2kenc.c:388
static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int layno, int precno, const uint8_t *expn, int numgbits, int packetno, int nlayers)
Definition j2kenc.c:716
static int put_com(Jpeg2000EncoderContext *s, int compno)
Definition j2kenc.c:320
static int parse_layer_rates(Jpeg2000EncoderContext *s)
Definition j2kenc.c:1603
#define WMSEDEC_SHIFT
must be >= 13
Definition j2kenc.c:85
static void update_size(uint8_t *size, const uint8_t *end)
Definition j2kenc.c:1459
static void makelayers(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
Definition j2kenc.c:1201
static int getcut(Jpeg2000Cblk *cblk, uint64_t lambda)
Definition j2kenc.c:1284
static void makelayer(Jpeg2000EncoderContext *s, int layno, double thresh, Jpeg2000Tile *tile, int final)
Definition j2kenc.c:1118
static av_cold void init_luts(void)
Definition j2kenc.c:515
#define NMSEDEC_FRACBITS
Definition j2kenc.c:84
static void truncpasses(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
Definition j2kenc.c:1302
static void putnumpasses(Jpeg2000EncoderContext *s, int n)
Definition j2kenc.c:701
static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
Definition j2kenc.c:1337
static uint8_t * put_sot(Jpeg2000EncoderContext *s, int tileno)
Definition j2kenc.c:339
static void encode_refpass(Jpeg2000T1Context *t1, int width, int height, int *nmsedec, int bpno)
Definition j2kenc.c:570
static void reinit(Jpeg2000EncoderContext *s)
Definition j2kenc.c:1449
static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno, int nlayers)
Definition j2kenc.c:872
static av_cold int j2kenc_init(AVCodecContext *avctx)
Definition j2kenc.c:1650
static int j2kenc_destroy(AVCodecContext *avctx)
Definition j2kenc.c:1730
static void encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
Definition j2kenc.c:548
static void init_quantization(Jpeg2000EncoderContext *s)
Definition j2kenc.c:485
static int lut_nmsedec_sig0[1<< NMSEDEC_BITS]
Definition j2kenc.c:94
static void compute_rates(Jpeg2000EncoderContext *s)
Definition j2kenc.c:358
static void tag_tree_update(Jpeg2000TgtNode *node)
update the value in node
Definition j2kenc.c:223
static void cleanup(Jpeg2000EncoderContext *s)
Definition j2kenc.c:1429
static void put_num(Jpeg2000EncoderContext *s, int num, int n)
put n least significant bits of a number num
Definition j2kenc.c:168
static void j2k_flush(Jpeg2000EncoderContext *s)
flush the bitstream
Definition j2kenc.c:175
#define LAMBDA_SCALE
Definition j2kenc.c:86
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition j2kenc.c:1464
static int lut_nmsedec_sig[1<< NMSEDEC_BITS]
Definition j2kenc.c:93
#define CODEC_JP2
Definition j2kenc.c:88
static int lut_nmsedec_ref0[1<< NMSEDEC_BITS]
Definition j2kenc.c:92
#define NMSEDEC_BITS
Definition j2kenc.c:83
static void encode_cblk(Jpeg2000EncoderContext *s, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, Jpeg2000Tile *tile, int width, int height, int bandpos, int lev)
Definition j2kenc.c:639
static int lut_nmsedec_ref[1<< NMSEDEC_BITS]
Definition j2kenc.c:91
static int put_qcd(Jpeg2000EncoderContext *s, int compno)
Definition j2kenc.c:294
static int getnmsedec_sig(int x, int bpno)
Definition j2kenc.c:534
static int getnmsedec_ref(int x, int bpno)
Definition j2kenc.c:541
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition j2kenc.c:154
static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold)
code the value stored in node
Definition j2kenc.c:186
static const int dwt_norms[2][4][10]
Definition j2kenc.c:96
static const AVClass j2k_class
Definition j2kenc.c:1763
#define OFFSET(x)
Definition j2kenc.c:1740
static void encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
Definition j2kenc.c:584
static int put_siz(Jpeg2000EncoderContext *s)
Definition j2kenc.c:233
#define CODEC_J2K
Definition j2kenc.c:89
#define COPY_FRAME(D, PIXEL)
Definition j2kenc.c:440
static int put_cod(Jpeg2000EncoderContext *s)
Definition j2kenc.c:262
void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h, int val)
Definition jpeg2000.c:86
void av_cold ff_jpeg2000_init_tier1_luts(void)
Definition jpeg2000.c:173
void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
Definition jpeg2000.c:580
void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
Definition jpeg2000.c:601
int ff_jpeg2000_init_component(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, Jpeg2000QuantStyle *qntsty, const int cbps, int dx, int dy, AVCodecContext *avctx)
Definition jpeg2000.c:470
void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1, int x, int y, int negative)
Definition jpeg2000.c:179
JPEG 2000 structures and defines common to encoder and decoder.
#define JPEG2000_T1_SIG_NB
Definition jpeg2000.h:93
#define JPEG2000_CSTY_SOP
Definition jpeg2000.h:119
static int ff_jpeg2000_ceildivpow2(int a, int b)
Definition jpeg2000.h:244
#define JPEG2000_PGOD_PCRL
Definition jpeg2000.h:128
#define JPEG2000_T1_REF
Definition jpeg2000.h:105
@ JPEG2000_QSTY_NONE
Definition jpeg2000.h:73
@ JPEG2000_QSTY_SE
Definition jpeg2000.h:75
#define JPEG2000_T1_VIS
Definition jpeg2000.h:103
static int ff_jpeg2000_ceildiv(int a, int64_t b)
Definition jpeg2000.h:249
#define JPEG2000_CSTY_EPH
Definition jpeg2000.h:120
#define JPEG2000_PGOD_CPRL
Definition jpeg2000.h:129
#define JPEG2000_PGOD_RPCL
Definition jpeg2000.h:127
#define JPEG2000_MAX_PASSES
Definition jpeg2000.h:81
#define JPEG2000_PGOD_RLCP
Definition jpeg2000.h:126
static int ff_jpeg2000_getsgnctxno(int flag, int *xorbit)
Definition jpeg2000.h:286
static int ff_jpeg2000_getrefctxno(int flag)
Definition jpeg2000.h:277
@ JPEG2000_SOT
Definition jpeg2000.h:56
@ JPEG2000_COM
Definition jpeg2000.h:55
@ JPEG2000_SOP
Definition jpeg2000.h:57
@ JPEG2000_QCD
Definition jpeg2000.h:48
@ JPEG2000_SOD
Definition jpeg2000.h:59
@ JPEG2000_EPH
Definition jpeg2000.h:58
@ JPEG2000_COD
Definition jpeg2000.h:42
@ JPEG2000_SIZ
Definition jpeg2000.h:41
@ JPEG2000_EOC
Definition jpeg2000.h:60
@ JPEG2000_SOC
Definition jpeg2000.h:39
#define JPEG2000_T1_SGN
Definition jpeg2000.h:107
static int ff_jpeg2000_getsigctxno(int flag, int bandno)
Definition jpeg2000.h:268
#define JPEG2000_T1_SIG
Definition jpeg2000.h:104
#define JPEG2000_PGOD_LRCP
Definition jpeg2000.h:125
int ff_dwt_encode(DWTContext *s, void *t)
@ FF_DWT97_INT
Definition jpeg2000dwt.h:40
@ FF_DWT53
Definition jpeg2000dwt.h:39
Libavcodec version macros.
#define LIBAVCODEC_IDENT
Definition version.h:43
Macro definitions for various function/variable attributes.
#define av_cold
Definition attributes.h:117
#define AVOnce
Definition thread.h:202
static int ff_thread_once(char *control, void(*routine)(void))
Definition thread.h:205
#define AV_ONCE_INIT
Definition thread.h:203
const char * desc
Definition libsvtav1.c:83
static const uint16_t mask[17]
Definition lzw.c:38
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
void ff_mqc_encode(MqcState *mqc, uint8_t *cxstate, int d)
code bit d with context cx
Definition mqcenc.c:81
#define MQC_CX_UNI
Definition mqc.h:33
int ff_mqc_flush_to(MqcState *mqc, uint8_t *dst, int *dst_len)
flush the encoder [returns number of bytes encoded]
Definition mqcenc.c:119
#define MQC_CX_RL
Definition mqc.h:34
void ff_mqc_initenc(MqcState *mqc, uint8_t *bp)
initialize the encoder
Definition mqcenc.c:71
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition pixdesc.c:3488
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_FLAG_RGB
The pixel format contains RGB-like data (as opposed to YUV/grayscale).
Definition pixdesc.h:136
#define AV_PIX_FMT_FLAG_PLANAR
At least one pixel component is not in the first data plane.
Definition pixdesc.h:132
#define AV_PIX_FMT_GBRAP12
Definition pixfmt.h:569
#define AV_PIX_FMT_YUV420P16
Definition pixfmt.h:556
#define AV_PIX_FMT_YUV444P12
Definition pixfmt.h:552
#define AV_PIX_FMT_YUV444P9
Definition pixfmt.h:544
#define AV_PIX_FMT_YUV420P10
Definition pixfmt.h:545
#define AV_PIX_FMT_YUV440P12
Definition pixfmt.h:551
#define AV_PIX_FMT_GRAY9
Definition pixfmt.h:524
#define AV_PIX_FMT_GBRAP16
Definition pixfmt.h:571
#define AV_PIX_FMT_GBRP9
Definition pixfmt.h:563
#define AV_PIX_FMT_YUV422P9
Definition pixfmt.h:543
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition pixfmt.h:766
#define AV_PIX_FMT_YUVA444P10
Definition pixfmt.h:598
#define AV_PIX_FMT_YUVA420P16
Definition pixfmt.h:601
#define AV_PIX_FMT_YA16
Definition pixfmt.h:530
#define AV_PIX_FMT_YUV420P12
Definition pixfmt.h:549
#define AV_PIX_FMT_YUVA420P10
Definition pixfmt.h:596
#define AV_PIX_FMT_YUVA422P9
Definition pixfmt.h:594
#define AV_PIX_FMT_YUV422P12
Definition pixfmt.h:550
#define AV_PIX_FMT_GBRP10
Definition pixfmt.h:564
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:546
#define AV_PIX_FMT_GRAY12
Definition pixfmt.h:526
#define AV_PIX_FMT_RGBA64
Definition pixfmt.h:535
#define AV_PIX_FMT_GBRP12
Definition pixfmt.h:565
#define AV_PIX_FMT_YUV420P9
Definition pixfmt.h:542
#define AV_PIX_FMT_RGB48
Definition pixfmt.h:531
#define AV_PIX_FMT_YUVA420P9
Definition pixfmt.h:593
#define AV_PIX_FMT_YUVA422P10
Definition pixfmt.h:597
#define AV_PIX_FMT_YUV420P14
Definition pixfmt.h:553
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition pixfmt.h:75
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition pixfmt.h:106
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition pixfmt.h:108
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition pixfmt.h:79
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition pixfmt.h:80
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition pixfmt.h:100
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition pixfmt.h:174
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition pixfmt.h:212
@ AV_PIX_FMT_GBR24P
Definition pixfmt.h:166
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition pixfmt.h:173
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition pixfmt.h:84
@ AV_PIX_FMT_YA8
8 bits gray, 8 bits alpha
Definition pixfmt.h:140
#define AV_PIX_FMT_YUV422P14
Definition pixfmt.h:554
#define AVPALETTE_COUNT
Definition pixfmt.h:33
#define AV_PIX_FMT_GRAY10
Definition pixfmt.h:525
#define AV_PIX_FMT_GRAY14
Definition pixfmt.h:527
#define AV_PIX_FMT_YUV422P16
Definition pixfmt.h:557
#define AV_PIX_FMT_YUV440P10
Definition pixfmt.h:547
#define AV_PIX_FMT_GRAY16
Definition pixfmt.h:528
#define AV_PIX_FMT_GBRAP10
Definition pixfmt.h:568
#define AV_PIX_FMT_YUVA444P16
Definition pixfmt.h:603
#define AV_PIX_FMT_YUVA422P16
Definition pixfmt.h:602
#define AV_PIX_FMT_GBRP16
Definition pixfmt.h:567
#define AV_PIX_FMT_YUV444P14
Definition pixfmt.h:555
#define AV_PIX_FMT_YUVA444P9
Definition pixfmt.h:595
#define AV_PIX_FMT_GBRP14
Definition pixfmt.h:566
#define AV_PIX_FMT_YUV444P16
Definition pixfmt.h:558
#define AV_PIX_FMT_YUV444P10
Definition pixfmt.h:548
static const float pred[4]
Definition siprdata.h:259
unsigned int pos
Definition spdifenc.c:431
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
uint16_t log2_cblk_height
Definition jpeg2000.h:219
Jpeg2000Prec * prec
Definition jpeg2000.h:222
uint16_t log2_cblk_width
Definition jpeg2000.h:219
int coord[2][2]
Definition jpeg2000.h:218
uint8_t * data
Definition jpeg2000.h:192
uint8_t nonzerobits
Definition jpeg2000.h:186
uint8_t ninclpasses
Definition jpeg2000.h:185
Jpeg2000Pass * passes
Definition jpeg2000.h:197
uint8_t npasses
Definition jpeg2000.h:184
Jpeg2000Layer * layers
Definition jpeg2000.h:198
uint8_t lblock
Definition jpeg2000.h:191
uint8_t incl
Definition jpeg2000.h:187
uint8_t log2_cblk_width
Definition jpeg2000.h:148
uint8_t log2_prec_widths[JPEG2000_MAX_RESLEVELS]
Definition jpeg2000.h:156
uint8_t log2_cblk_height
Definition jpeg2000.h:149
uint8_t log2_prec_heights[JPEG2000_MAX_RESLEVELS]
Definition jpeg2000.h:157
uint8_t comp_remap[4]
Definition j2kenc.c:120
AVCodecContext * avctx
Definition j2kenc.c:115
Jpeg2000Tile * tile
Definition j2kenc.c:137
int tile_height
tile size
Definition j2kenc.c:124
Jpeg2000CodingStyle codsty
Definition j2kenc.c:134
uint8_t cbps[4]
bits per sample in particular components
Definition j2kenc.c:119
uint8_t * buf_start
Definition j2kenc.c:127
Jpeg2000QuantStyle qntsty
Definition j2kenc.c:135
const AVFrame * picture
Definition j2kenc.c:116
int height
image width and height
Definition j2kenc.c:118
uint8_t compression_rate_enc
Is compression done using compression ratio?
Definition j2kenc.c:139
double disto
Definition jpeg2000.h:179
uint8_t * data_start
Definition jpeg2000.h:176
uint8_t flushed[4]
Definition jpeg2000.h:171
int flushed_len
Definition jpeg2000.h:172
uint16_t rate
Definition jpeg2000.h:169
int64_t disto
Definition jpeg2000.h:170
Jpeg2000Cblk * cblk
Definition jpeg2000.h:212
Jpeg2000TgtNode * zerobits
Definition jpeg2000.h:210
Jpeg2000TgtNode * cblkincl
Definition jpeg2000.h:211
int nb_codeblocks_height
Definition jpeg2000.h:209
int nb_codeblocks_width
Definition jpeg2000.h:208
uint16_t mant[JPEG2000_MAX_DECLEVELS *3]
Definition jpeg2000.h:163
uint8_t expn[JPEG2000_MAX_DECLEVELS *3]
Definition jpeg2000.h:162
uint8_t log2_prec_width
Definition jpeg2000.h:229
Jpeg2000Band * band
Definition jpeg2000.h:230
uint8_t log2_prec_height
Definition jpeg2000.h:229
int data[6144]
Definition jpeg2000.h:132
uint16_t flags[6156]
Definition jpeg2000.h:133
uint8_t temp_val
Definition jpeg2000.h:140
struct Jpeg2000TgtNode * parent
Definition jpeg2000.h:142
double * layer_rates
Definition j2kenc.c:110
Jpeg2000Component * comp
Definition j2kenc.c:109
uint8_t cx_states[19]
Definition mqc.h:45
#define av_malloc_array(a, b)
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_log(a,...)
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
int size