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  * JPEG2000 image encoder
24  * @file
25  * @author Kamil Nowosad
26  */
27 
28 #include <float.h>
29 #include "avcodec.h"
30 #include "internal.h"
31 #include "bytestream.h"
32 #include "j2k.h"
33 #include "libavutil/common.h"
34 
35 #define NMSEDEC_BITS 7
36 #define NMSEDEC_FRACBITS (NMSEDEC_BITS-1)
37 #define WMSEDEC_SHIFT 13 ///< must be >= 13
38 #define LAMBDA_SCALE (100000000LL << (WMSEDEC_SHIFT - 13))
39 
40 static int lut_nmsedec_ref [1<<NMSEDEC_BITS],
44 
45 static const int dwt_norms[2][4][10] = { // [dwt_type][band][rlevel] (multiplied by 10000)
46  {{10000, 19650, 41770, 84030, 169000, 338400, 676900, 1353000, 2706000, 5409000},
47  {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
48  {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
49  {20800, 38650, 83070, 171800, 347100, 695900, 1393000, 2786000, 5572000}},
50 
51  {{10000, 15000, 27500, 53750, 106800, 213400, 426700, 853300, 1707000, 3413000},
52  {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
53  {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
54  { 7186, 9218, 15860, 30430, 60190, 120100, 240000, 479700, 959300}}
55 };
56 
57 typedef struct {
59 } J2kTile;
60 
61 typedef struct {
64 
65  int width, height; ///< image width and height
66  uint8_t cbps[4]; ///< bits per sample in particular components
67  int chroma_shift[2];
70  int tile_width, tile_height; ///< tile size
71  int numXtiles, numYtiles;
72 
76  int bit_index;
77 
78  int64_t lambda;
79 
82 
85 
86 
87 /* debug */
88 #if 0
89 #undef ifprintf
90 #undef printf
91 
92 static void nspaces(FILE *fd, int n)
93 {
94  while(n--) putc(' ', fd);
95 }
96 
97 static void printcomp(J2kComponent *comp)
98 {
99  int i;
100  for (i = 0; i < comp->y1 - comp->y0; i++)
101  ff_j2k_printv(comp->data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
102 }
103 
104 static void dump(J2kEncoderContext *s, FILE *fd)
105 {
106  int tileno, compno, reslevelno, bandno, precno;
107  fprintf(fd, "XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
108  "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
109  "tiles:\n",
110  s->width, s->height, s->tile_width, s->tile_height,
111  s->numXtiles, s->numYtiles, s->ncomponents);
112  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
113  J2kTile *tile = s->tile + tileno;
114  nspaces(fd, 2);
115  fprintf(fd, "tile %d:\n", tileno);
116  for(compno = 0; compno < s->ncomponents; compno++){
117  J2kComponent *comp = tile->comp + compno;
118  nspaces(fd, 4);
119  fprintf(fd, "component %d:\n", compno);
120  nspaces(fd, 4);
121  fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
122  comp->x0, comp->x1, comp->y0, comp->y1);
123  for(reslevelno = 0; reslevelno < s->nreslevels; reslevelno++){
124  J2kResLevel *reslevel = comp->reslevel + reslevelno;
125  nspaces(fd, 6);
126  fprintf(fd, "reslevel %d:\n", reslevelno);
127  nspaces(fd, 6);
128  fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
129  reslevel->x0, reslevel->x1, reslevel->y0,
130  reslevel->y1, reslevel->nbands);
131  for(bandno = 0; bandno < reslevel->nbands; bandno++){
132  J2kBand *band = reslevel->band + bandno;
133  nspaces(fd, 8);
134  fprintf(fd, "band %d:\n", bandno);
135  nspaces(fd, 8);
136  fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
137  "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
138  band->x0, band->x1,
139  band->y0, band->y1,
140  band->codeblock_width, band->codeblock_height,
141  band->cblknx, band->cblkny);
142  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
143  J2kPrec *prec = band->prec + precno;
144  nspaces(fd, 10);
145  fprintf(fd, "prec %d:\n", precno);
146  nspaces(fd, 10);
147  fprintf(fd, "xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
148  prec->xi0, prec->xi1, prec->yi0, prec->yi1);
149  }
150  }
151  }
152  }
153  }
154 }
155 #endif
156 
157 /* bitstream routines */
158 
159 /** put n times val bit */
160 static void put_bits(J2kEncoderContext *s, int val, int n) // TODO: optimize
161 {
162  while (n-- > 0){
163  if (s->bit_index == 8)
164  {
165  s->bit_index = *s->buf == 0xff;
166  *(++s->buf) = 0;
167  }
168  *s->buf |= val << (7 - s->bit_index++);
169  }
170 }
171 
172 /** put n least significant bits of a number num */
173 static void put_num(J2kEncoderContext *s, int num, int n)
174 {
175  while(--n >= 0)
176  put_bits(s, (num >> n) & 1, 1);
177 }
178 
179 /** flush the bitstream */
181 {
182  if (s->bit_index){
183  s->bit_index = 0;
184  s->buf++;
185  }
186 }
187 
188 /* tag tree routines */
189 
190 /** code the value stored in node */
191 static void tag_tree_code(J2kEncoderContext *s, J2kTgtNode *node, int threshold)
192 {
193  J2kTgtNode *stack[30];
194  int sp = 1, curval = 0;
195  stack[0] = node;
196 
197  node = node->parent;
198  while(node){
199  if (node->vis){
200  curval = node->val;
201  break;
202  }
203  node->vis++;
204  stack[sp++] = node;
205  node = node->parent;
206  }
207  while(--sp >= 0){
208  if (stack[sp]->val >= threshold){
209  put_bits(s, 0, threshold - curval);
210  break;
211  }
212  put_bits(s, 0, stack[sp]->val - curval);
213  put_bits(s, 1, 1);
214  curval = stack[sp]->val;
215  }
216 }
217 
218 /** update the value in node */
219 static void tag_tree_update(J2kTgtNode *node)
220 {
221  int lev = 0;
222  while (node->parent){
223  if (node->parent->val <= node->val)
224  break;
225  node->parent->val = node->val;
226  node = node->parent;
227  lev++;
228  }
229 }
230 
232 {
233  int i;
234 
235  if (s->buf_end - s->buf < 40 + 3 * s->ncomponents)
236  return -1;
237 
238  bytestream_put_be16(&s->buf, J2K_SIZ);
239  bytestream_put_be16(&s->buf, 38 + 3 * s->ncomponents); // Lsiz
240  bytestream_put_be16(&s->buf, 0); // Rsiz
241  bytestream_put_be32(&s->buf, s->width); // width
242  bytestream_put_be32(&s->buf, s->height); // height
243  bytestream_put_be32(&s->buf, 0); // X0Siz
244  bytestream_put_be32(&s->buf, 0); // Y0Siz
245 
246  bytestream_put_be32(&s->buf, s->tile_width); // XTSiz
247  bytestream_put_be32(&s->buf, s->tile_height); // YTSiz
248  bytestream_put_be32(&s->buf, 0); // XT0Siz
249  bytestream_put_be32(&s->buf, 0); // YT0Siz
250  bytestream_put_be16(&s->buf, s->ncomponents); // CSiz
251 
252  for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
253  bytestream_put_byte(&s->buf, 7);
254  bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[0]:1);
255  bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[1]:1);
256  }
257  return 0;
258 }
259 
261 {
262  J2kCodingStyle *codsty = &s->codsty;
263 
264  if (s->buf_end - s->buf < 14)
265  return -1;
266 
267  bytestream_put_be16(&s->buf, J2K_COD);
268  bytestream_put_be16(&s->buf, 12); // Lcod
269  bytestream_put_byte(&s->buf, 0); // Scod
270  // SGcod
271  bytestream_put_byte(&s->buf, 0); // progression level
272  bytestream_put_be16(&s->buf, 1); // num of layers
273  if(s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
274  bytestream_put_byte(&s->buf, 2); // ICT
275  }else{
276  bytestream_put_byte(&s->buf, 0); // unspecified
277  }
278  // SPcod
279  bytestream_put_byte(&s->buf, codsty->nreslevels - 1); // num of decomp. levels
280  bytestream_put_byte(&s->buf, codsty->log2_cblk_width-2); // cblk width
281  bytestream_put_byte(&s->buf, codsty->log2_cblk_height-2); // cblk height
282  bytestream_put_byte(&s->buf, 0); // cblk style
283  bytestream_put_byte(&s->buf, codsty->transform); // transformation
284  return 0;
285 }
286 
287 static int put_qcd(J2kEncoderContext *s, int compno)
288 {
289  int i, size;
290  J2kCodingStyle *codsty = &s->codsty;
291  J2kQuantStyle *qntsty = &s->qntsty;
292 
293  if (qntsty->quantsty == J2K_QSTY_NONE)
294  size = 4 + 3 * (codsty->nreslevels-1);
295  else // QSTY_SE
296  size = 5 + 6 * (codsty->nreslevels-1);
297 
298  if (s->buf_end - s->buf < size + 2)
299  return -1;
300 
301  bytestream_put_be16(&s->buf, J2K_QCD);
302  bytestream_put_be16(&s->buf, size); // LQcd
303  bytestream_put_byte(&s->buf, (qntsty->nguardbits << 5) | qntsty->quantsty); // Sqcd
304  if (qntsty->quantsty == J2K_QSTY_NONE)
305  for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
306  bytestream_put_byte(&s->buf, qntsty->expn[i] << 3);
307  else // QSTY_SE
308  for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
309  bytestream_put_be16(&s->buf, (qntsty->expn[i] << 11) | qntsty->mant[i]);
310  return 0;
311 }
312 
313 static uint8_t *put_sot(J2kEncoderContext *s, int tileno)
314 {
315  uint8_t *psotptr;
316 
317  if (s->buf_end - s->buf < 12)
318  return NULL;
319 
320  bytestream_put_be16(&s->buf, J2K_SOT);
321  bytestream_put_be16(&s->buf, 10); // Lsot
322  bytestream_put_be16(&s->buf, tileno); // Isot
323 
324  psotptr = s->buf;
325  bytestream_put_be32(&s->buf, 0); // Psot (filled in later)
326 
327  bytestream_put_byte(&s->buf, 0); // TPsot
328  bytestream_put_byte(&s->buf, 1); // TNsot
329  return psotptr;
330 }
331 
332 /**
333  * compute the sizes of tiles, resolution levels, bands, etc.
334  * allocate memory for them
335  * divide the input image into tile-components
336  */
338 {
339  int tileno, tilex, tiley, compno;
340  J2kCodingStyle *codsty = &s->codsty;
341  J2kQuantStyle *qntsty = &s->qntsty;
342 
345 
346  s->tile = av_malloc(s->numXtiles * s->numYtiles * sizeof(J2kTile));
347  if (!s->tile)
348  return AVERROR(ENOMEM);
349  for (tileno = 0, tiley = 0; tiley < s->numYtiles; tiley++)
350  for (tilex = 0; tilex < s->numXtiles; tilex++, tileno++){
351  J2kTile *tile = s->tile + tileno;
352 
353  tile->comp = av_malloc(s->ncomponents * sizeof(J2kComponent));
354  if (!tile->comp)
355  return AVERROR(ENOMEM);
356  for (compno = 0; compno < s->ncomponents; compno++){
357  J2kComponent *comp = tile->comp + compno;
358  int ret, i, j;
359 
360  comp->coord[0][0] = tilex * s->tile_width;
361  comp->coord[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
362  comp->coord[1][0] = tiley * s->tile_height;
363  comp->coord[1][1] = FFMIN((tiley+1)*s->tile_height, s->height);
364  if (compno > 0)
365  for (i = 0; i < 2; i++)
366  for (j = 0; j < 2; j++)
367  comp->coord[i][j] = ff_j2k_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
368 
369  if (ret = ff_j2k_init_component(comp, codsty, qntsty, s->cbps[compno], compno?1<<s->chroma_shift[0]:1, compno?1<<s->chroma_shift[1]:1))
370  return ret;
371  }
372  }
373  return 0;
374 }
375 
377 {
378  int tileno, compno, i, y, x;
379  uint8_t *line;
380  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
381  J2kTile *tile = s->tile + tileno;
382  if (s->planar){
383  for (compno = 0; compno < s->ncomponents; compno++){
384  J2kComponent *comp = tile->comp + compno;
385  int *dst = comp->data;
386  line = s->picture.data[compno]
387  + comp->coord[1][0] * s->picture.linesize[compno]
388  + comp->coord[0][0];
389  for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){
390  uint8_t *ptr = line;
391  for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++)
392  *dst++ = *ptr++ - (1 << 7);
393  line += s->picture.linesize[compno];
394  }
395  }
396  } else{
397  line = s->picture.data[0] + tile->comp[0].coord[1][0] * s->picture.linesize[0]
398  + tile->comp[0].coord[0][0] * s->ncomponents;
399 
400  i = 0;
401  for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){
402  uint8_t *ptr = line;
403  for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){
404  for (compno = 0; compno < s->ncomponents; compno++){
405  tile->comp[compno].data[i] = *ptr++ - (1 << 7);
406  }
407  }
408  line += s->picture.linesize[0];
409  }
410  }
411  }
412 }
413 
415 {
416  int compno, reslevelno, bandno;
417  J2kQuantStyle *qntsty = &s->qntsty;
418  J2kCodingStyle *codsty = &s->codsty;
419 
420  for (compno = 0; compno < s->ncomponents; compno++){
421  int gbandno = 0;
422  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
423  int nbands, lev = codsty->nreslevels - reslevelno - 1;
424  nbands = reslevelno ? 3 : 1;
425  for (bandno = 0; bandno < nbands; bandno++, gbandno++){
426  int expn, mant;
427 
428  if (codsty->transform == FF_DWT97){
429  int bandpos = bandno + (reslevelno>0),
430  ss = 81920000 / dwt_norms[0][bandpos][lev],
431  log = av_log2(ss);
432  mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
433  expn = s->cbps[compno] - log + 13;
434  } else
435  expn = ((bandno&2)>>1) + (reslevelno>0) + s->cbps[compno];
436 
437  qntsty->expn[gbandno] = expn;
438  qntsty->mant[gbandno] = mant;
439  }
440  }
441  }
442 }
443 
444 static void init_luts(void)
445 {
446  int i, a,
447  mask = ~((1<<NMSEDEC_FRACBITS)-1);
448 
449  for (i = 0; i < (1 << NMSEDEC_BITS); i++){
450  lut_nmsedec_sig[i] = FFMAX(6*i - (9<<NMSEDEC_FRACBITS-1) << 12-NMSEDEC_FRACBITS, 0);
451  lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
452 
453  a = (i >> (NMSEDEC_BITS-2)&2) + 1;
454  lut_nmsedec_ref[i] = FFMAX((-2*i + (1<<NMSEDEC_FRACBITS) + a*i - (a*a<<NMSEDEC_FRACBITS-2))
455  << 13-NMSEDEC_FRACBITS, 0);
456  lut_nmsedec_ref0[i] = FFMAX(((i*i + (1-4*i << NMSEDEC_FRACBITS-1) + (1<<2*NMSEDEC_FRACBITS)) & mask)
457  << 1, 0);
458  }
459 }
460 
461 /* tier-1 routines */
462 static int getnmsedec_sig(int x, int bpno)
463 {
464  if (bpno > NMSEDEC_FRACBITS)
465  return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
466  return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
467 }
468 
469 static int getnmsedec_ref(int x, int bpno)
470 {
471  if (bpno > NMSEDEC_FRACBITS)
472  return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
473  return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
474 }
475 
476 static void encode_sigpass(J2kT1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
477 {
478  int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
479  int vert_causal_ctx_csty_loc_symbol;
480  for (y0 = 0; y0 < height; y0 += 4)
481  for (x = 0; x < width; x++)
482  for (y = y0; y < height && y < y0+4; y++){
483  if (!(t1->flags[y+1][x+1] & J2K_T1_SIG) && (t1->flags[y+1][x+1] & J2K_T1_SIG_NB)){
484  int ctxno = ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno, vert_causal_ctx_csty_loc_symbol),
485  bit = t1->data[y][x] & mask ? 1 : 0;
486  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
487  if (bit){
488  int xorbit;
489  int ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
490  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
491  *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
492  ff_j2k_set_significant(t1, x, y, t1->flags[y+1][x+1] >> 15);
493  }
494  t1->flags[y+1][x+1] |= J2K_T1_VIS;
495  }
496  }
497 }
498 
499 static void encode_refpass(J2kT1Context *t1, int width, int height, int *nmsedec, int bpno)
500 {
501  int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
502  for (y0 = 0; y0 < height; y0 += 4)
503  for (x = 0; x < width; x++)
504  for (y = y0; y < height && y < y0+4; y++)
505  if ((t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS)) == J2K_T1_SIG){
506  int ctxno = ff_j2k_getrefctxno(t1->flags[y+1][x+1]);
507  *nmsedec += getnmsedec_ref(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
508  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
509  t1->flags[y+1][x+1] |= J2K_T1_REF;
510  }
511 }
512 
513 static void encode_clnpass(J2kT1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
514 {
515  int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
516  int vert_causal_ctx_csty_loc_symbol;
517  for (y0 = 0; y0 < height; y0 += 4)
518  for (x = 0; x < width; x++){
519  if (y0 + 3 < height && !(
520  (t1->flags[y0+1][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
521  (t1->flags[y0+2][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
522  (t1->flags[y0+3][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG)) ||
523  (t1->flags[y0+4][x+1] & (J2K_T1_SIG_NB | J2K_T1_VIS | J2K_T1_SIG))))
524  {
525  // aggregation mode
526  int rlen;
527  for (rlen = 0; rlen < 4; rlen++)
528  if (t1->data[y0+rlen][x] & mask)
529  break;
530  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
531  if (rlen == 4)
532  continue;
533  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
534  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
535  for (y = y0 + rlen; y < y0 + 4; y++){
536  if (!(t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS))){
537  int ctxno = ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno, vert_causal_ctx_csty_loc_symbol);
538  if (y > y0 + rlen)
539  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
540  if (t1->data[y][x] & mask){ // newly significant
541  int xorbit;
542  int ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
543  *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
544  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
545  ff_j2k_set_significant(t1, x, y, t1->flags[y+1][x+1] >> 15);
546  }
547  }
548  t1->flags[y+1][x+1] &= ~J2K_T1_VIS;
549  }
550  } else{
551  for (y = y0; y < y0 + 4 && y < height; y++){
552  if (!(t1->flags[y+1][x+1] & (J2K_T1_SIG | J2K_T1_VIS))){
553  int ctxno = ff_j2k_getnbctxno(t1->flags[y+1][x+1], bandno, vert_causal_ctx_csty_loc_symbol);
554  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
555  if (t1->data[y][x] & mask){ // newly significant
556  int xorbit;
557  int ctxno = ff_j2k_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
558  *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
559  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
560  ff_j2k_set_significant(t1, x, y, t1->flags[y+1][x+1] >> 15);
561  }
562  }
563  t1->flags[y+1][x+1] &= ~J2K_T1_VIS;
564  }
565  }
566  }
567 }
568 
570  int width, int height, int bandpos, int lev)
571 {
572  int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
573  int64_t wmsedec = 0;
574 
575  for (y = 0; y < height+2; y++)
576  memset(t1->flags[y], 0, (width+2)*sizeof(int));
577 
578  for (y = 0; y < height; y++){
579  for (x = 0; x < width; x++){
580  if (t1->data[y][x] < 0){
581  t1->flags[y+1][x+1] |= J2K_T1_SGN;
582  t1->data[y][x] = -t1->data[y][x];
583  }
584  max = FFMAX(max, t1->data[y][x]);
585  }
586  }
587 
588  if (max == 0){
589  cblk->nonzerobits = 0;
590  bpno = 0;
591  } else{
592  cblk->nonzerobits = av_log2(max) + 1 - NMSEDEC_FRACBITS;
593  bpno = cblk->nonzerobits - 1;
594  }
595 
596  ff_mqc_initenc(&t1->mqc, cblk->data);
597 
598  for (passno = 0; bpno >= 0; passno++){
599  nmsedec=0;
600 
601  switch(pass_t){
602  case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
603  break;
604  case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
605  break;
606  case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
607  break;
608  }
609 
610  cblk->passes[passno].rate = 3 + ff_mqc_length(&t1->mqc);
611  wmsedec += (int64_t)nmsedec << (2*bpno);
612  cblk->passes[passno].disto = wmsedec;
613 
614  if (++pass_t == 3){
615  pass_t = 0;
616  bpno--;
617  }
618  }
619  cblk->npasses = passno;
620  cblk->ninclpasses = passno;
621 
622  // TODO: optional flush on each pass
623  cblk->passes[passno-1].rate = ff_mqc_flush(&t1->mqc);
624 }
625 
626 /* tier-2 routines: */
627 
628 static void putnumpasses(J2kEncoderContext *s, int n)
629 {
630  if (n == 1)
631  put_num(s, 0, 1);
632  else if (n == 2)
633  put_num(s, 2, 2);
634  else if (n <= 5)
635  put_num(s, 0xc | (n-3), 4);
636  else if (n <= 36)
637  put_num(s, 0x1e0 | (n-6), 9);
638  else
639  put_num(s, 0xff80 | (n-37), 16);
640 }
641 
642 
643 static int encode_packet(J2kEncoderContext *s, J2kResLevel *rlevel, int precno,
644  uint8_t *expn, int numgbits)
645 {
646  int bandno, empty = 1;
647 
648  // init bitstream
649  *s->buf = 0;
650  s->bit_index = 0;
651 
652  // header
653 
654  // is the packet empty?
655  for (bandno = 0; bandno < rlevel->nbands; bandno++){
656  if (rlevel->band[bandno].coord[0][0] < rlevel->band[bandno].coord[0][1]
657  && rlevel->band[bandno].coord[1][0] < rlevel->band[bandno].coord[1][1]){
658  empty = 0;
659  break;
660  }
661  }
662 
663  put_bits(s, !empty, 1);
664  if (empty){
665  j2k_flush(s);
666  return 0;
667  }
668 
669  for (bandno = 0; bandno < rlevel->nbands; bandno++){
670  J2kBand *band = rlevel->band + bandno;
671  J2kPrec *prec = band->prec + precno;
672  int yi, xi, pos;
673  int cblknw = prec->xi1 - prec->xi0;
674 
675  if (band->coord[0][0] == band->coord[0][1]
676  || band->coord[1][0] == band->coord[1][1])
677  continue;
678 
679  for (pos=0, yi = prec->yi0; yi < prec->yi1; yi++){
680  for (xi = prec->xi0; xi < prec->xi1; xi++, pos++){
681  prec->cblkincl[pos].val = band->cblk[yi * cblknw + xi].ninclpasses == 0;
682  tag_tree_update(prec->cblkincl + pos);
683  prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - band->cblk[yi * cblknw + xi].nonzerobits;
684  tag_tree_update(prec->zerobits + pos);
685  }
686  }
687 
688  for (pos=0, yi = prec->yi0; yi < prec->yi1; yi++){
689  for (xi = prec->xi0; xi < prec->xi1; xi++, pos++){
690  int pad = 0, llen, length;
691  J2kCblk *cblk = band->cblk + yi * cblknw + xi;
692 
693  if (s->buf_end - s->buf < 20) // approximately
694  return -1;
695 
696  // inclusion information
697  tag_tree_code(s, prec->cblkincl + pos, 1);
698  if (!cblk->ninclpasses)
699  continue;
700  // zerobits information
701  tag_tree_code(s, prec->zerobits + pos, 100);
702  // number of passes
703  putnumpasses(s, cblk->ninclpasses);
704 
705  length = cblk->passes[cblk->ninclpasses-1].rate;
706  llen = av_log2(length) - av_log2(cblk->ninclpasses) - 2;
707  if (llen < 0){
708  pad = -llen;
709  llen = 0;
710  }
711  // length of code block
712  put_bits(s, 1, llen);
713  put_bits(s, 0, 1);
714  put_num(s, length, av_log2(length)+1+pad);
715  }
716  }
717  }
718  j2k_flush(s);
719  for (bandno = 0; bandno < rlevel->nbands; bandno++){
720  J2kBand *band = rlevel->band + bandno;
721  J2kPrec *prec = band->prec + precno;
722  int yi, cblknw = prec->xi1 - prec->xi0;
723  for (yi = prec->yi0; yi < prec->yi1; yi++){
724  int xi;
725  for (xi = prec->xi0; xi < prec->xi1; xi++){
726  J2kCblk *cblk = band->cblk + yi * cblknw + xi;
727  if (cblk->ninclpasses){
728  if (s->buf_end - s->buf < cblk->passes[cblk->ninclpasses-1].rate)
729  return -1;
730  bytestream_put_buffer(&s->buf, cblk->data, cblk->passes[cblk->ninclpasses-1].rate);
731  }
732  }
733  }
734  }
735  return 0;
736 }
737 
738 static int encode_packets(J2kEncoderContext *s, J2kTile *tile, int tileno)
739 {
740  int compno, reslevelno, ret;
741  J2kCodingStyle *codsty = &s->codsty;
742  J2kQuantStyle *qntsty = &s->qntsty;
743 
744  av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
745  // lay-rlevel-comp-pos progression
746  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
747  for (compno = 0; compno < s->ncomponents; compno++){
748  int precno;
749  J2kResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
750  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
751  if (ret = encode_packet(s, reslevel, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
752  qntsty->nguardbits))
753  return ret;
754  }
755  }
756  }
757  av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
758  return 0;
759 }
760 
761 static int getcut(J2kCblk *cblk, int64_t lambda, int dwt_norm)
762 {
763  int passno, res = 0;
764  for (passno = 0; passno < cblk->npasses; passno++){
765  int dr;
766  int64_t dd;
767 
768  dr = cblk->passes[passno].rate
769  - (res ? cblk->passes[res-1].rate:0);
770  dd = cblk->passes[passno].disto
771  - (res ? cblk->passes[res-1].disto:0);
772 
773  if (((dd * dwt_norm) >> WMSEDEC_SHIFT) * dwt_norm >= dr * lambda)
774  res = passno+1;
775  }
776  return res;
777 }
778 
779 static void truncpasses(J2kEncoderContext *s, J2kTile *tile)
780 {
781  int compno, reslevelno, bandno, cblkno, lev;
782  J2kCodingStyle *codsty = &s->codsty;
783 
784  for (compno = 0; compno < s->ncomponents; compno++){
785  J2kComponent *comp = tile->comp + compno;
786 
787  for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
788  J2kResLevel *reslevel = comp->reslevel + reslevelno;
789 
790  for (bandno = 0; bandno < reslevel->nbands ; bandno++){
791  int bandpos = bandno + (reslevelno > 0);
792  J2kBand *band = reslevel->band + bandno;
793 
794  for (cblkno = 0; cblkno < band->cblknx * band->cblkny; cblkno++){
795  J2kCblk *cblk = band->cblk + cblkno;
796 
797  cblk->ninclpasses = getcut(cblk, s->lambda,
798  (int64_t)dwt_norms[codsty->transform][bandpos][lev] * (int64_t)band->stepsize >> 13);
799  }
800  }
801  }
802  }
803 }
804 
805 static int encode_tile(J2kEncoderContext *s, J2kTile *tile, int tileno)
806 {
807  int compno, reslevelno, bandno, ret;
809  J2kCodingStyle *codsty = &s->codsty;
810  for (compno = 0; compno < s->ncomponents; compno++){
811  J2kComponent *comp = s->tile[tileno].comp + compno;
812 
813  av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
814  if (ret = ff_j2k_dwt_encode(&comp->dwt, comp->data))
815  return ret;
816  av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
817 
818  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
819  J2kResLevel *reslevel = comp->reslevel + reslevelno;
820 
821  for (bandno = 0; bandno < reslevel->nbands ; bandno++){
822  J2kBand *band = reslevel->band + bandno;
823  int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
824  yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
825  y0 = yy0;
826  yy1 = FFMIN(ff_j2k_ceildiv(band->coord[1][0] + 1, band->codeblock_height) * band->codeblock_height,
827  band->coord[1][1]) - band->coord[1][0] + yy0;
828 
829  if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
830  continue;
831 
832  bandpos = bandno + (reslevelno > 0);
833 
834  for (cblky = 0; cblky < band->cblkny; cblky++){
835  if (reslevelno == 0 || bandno == 1)
836  xx0 = 0;
837  else
838  xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
839  x0 = xx0;
840  xx1 = FFMIN(ff_j2k_ceildiv(band->coord[0][0] + 1, band->codeblock_width) * band->codeblock_width,
841  band->coord[0][1]) - band->coord[0][0] + xx0;
842 
843  for (cblkx = 0; cblkx < band->cblknx; cblkx++, cblkno++){
844  int y, x;
845  if (codsty->transform == FF_DWT53){
846  for (y = yy0; y < yy1; y++){
847  int *ptr = t1.data[y-yy0];
848  for (x = xx0; x < xx1; x++){
849  *ptr++ = comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] << NMSEDEC_FRACBITS;
850  }
851  }
852  } else{
853  for (y = yy0; y < yy1; y++){
854  int *ptr = t1.data[y-yy0];
855  for (x = xx0; x < xx1; x++){
856  *ptr = (comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]);
857  *ptr = (int64_t)*ptr * (int64_t)(8192 * 8192 / band->stepsize) >> 13 - NMSEDEC_FRACBITS;
858  ptr++;
859  }
860  }
861  }
862  encode_cblk(s, &t1, band->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
863  bandpos, codsty->nreslevels - reslevelno - 1);
864  xx0 = xx1;
865  xx1 = FFMIN(xx1 + band->codeblock_width, band->coord[0][1] - band->coord[0][0] + x0);
866  }
867  yy0 = yy1;
868  yy1 = FFMIN(yy1 + band->codeblock_height, band->coord[1][1] - band->coord[1][0] + y0);
869  }
870  }
871  }
872  av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
873  }
874 
875  av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
876  truncpasses(s, tile);
877  if (ret = encode_packets(s, tile, tileno))
878  return ret;
879  av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
880  return 0;
881 }
882 
883 static void cleanup(J2kEncoderContext *s)
884 {
885  int tileno, compno;
886  J2kCodingStyle *codsty = &s->codsty;
887 
888  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
889  for (compno = 0; compno < s->ncomponents; compno++){
890  J2kComponent *comp = s->tile[tileno].comp + compno;
891  ff_j2k_cleanup(comp, codsty);
892  }
893  av_freep(&s->tile[tileno].comp);
894  }
895  av_freep(&s->tile);
896 }
897 
898 static void reinit(J2kEncoderContext *s)
899 {
900  int tileno, compno;
901  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
902  J2kTile *tile = s->tile + tileno;
903  for (compno = 0; compno < s->ncomponents; compno++)
904  ff_j2k_reinit(tile->comp + compno, &s->codsty);
905  }
906 }
907 
909  const AVFrame *pict, int *got_packet)
910 {
911  int tileno, ret;
912  J2kEncoderContext *s = avctx->priv_data;
913 
914  if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*9 + FF_MIN_BUFFER_SIZE)) < 0)
915  return ret;
916 
917  // init:
918  s->buf = s->buf_start = pkt->data;
919  s->buf_end = pkt->data + pkt->size;
920 
921  s->picture = *pict;
922  avctx->coded_frame= &s->picture;
923 
925 
926  copy_frame(s);
927  reinit(s);
928 
929  if (s->buf_end - s->buf < 2)
930  return -1;
931  bytestream_put_be16(&s->buf, J2K_SOC);
932  if (ret = put_siz(s))
933  return ret;
934  if (ret = put_cod(s))
935  return ret;
936  if (ret = put_qcd(s, 0))
937  return ret;
938 
939  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
940  uint8_t *psotptr;
941  if (!(psotptr = put_sot(s, tileno)))
942  return -1;
943  if (s->buf_end - s->buf < 2)
944  return -1;
945  bytestream_put_be16(&s->buf, J2K_SOD);
946  if (ret = encode_tile(s, s->tile + tileno, tileno))
947  return ret;
948  bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
949  }
950  if (s->buf_end - s->buf < 2)
951  return -1;
952  bytestream_put_be16(&s->buf, J2K_EOC);
953 
954  av_log(s->avctx, AV_LOG_DEBUG, "end\n");
955  pkt->size = s->buf - s->buf_start;
956  pkt->flags |= AV_PKT_FLAG_KEY;
957  *got_packet = 1;
958 
959  return 0;
960 }
961 
963 {
964  int i, ret;
965  J2kEncoderContext *s = avctx->priv_data;
966  J2kCodingStyle *codsty = &s->codsty;
967  J2kQuantStyle *qntsty = &s->qntsty;
968 
969  s->avctx = avctx;
970  av_log(s->avctx, AV_LOG_DEBUG, "init\n");
971 
972  // defaults:
973  // TODO: implement setting non-standard precinct size
974  codsty->log2_prec_width = 15;
975  codsty->log2_prec_height = 15;
976  codsty->nreslevels = 7;
977  codsty->log2_cblk_width = 4;
978  codsty->log2_cblk_height = 4;
979  codsty->transform = 1;
980 
981  qntsty->nguardbits = 1;
982 
983  s->tile_width = 256;
984  s->tile_height = 256;
985 
986  if (codsty->transform == FF_DWT53)
987  qntsty->quantsty = J2K_QSTY_NONE;
988  else
989  qntsty->quantsty = J2K_QSTY_SE;
990 
991  s->width = avctx->width;
992  s->height = avctx->height;
993 
994  for (i = 0; i < 3; i++)
995  s->cbps[i] = 8;
996 
997  if (avctx->pix_fmt == AV_PIX_FMT_RGB24){
998  s->ncomponents = 3;
999  } else if (avctx->pix_fmt == AV_PIX_FMT_GRAY8){
1000  s->ncomponents = 1;
1001  } else{ // planar YUV
1002  s->planar = 1;
1003  s->ncomponents = 3;
1005  s->chroma_shift, s->chroma_shift + 1);
1006  }
1007 
1009 
1010  init_luts();
1011 
1012  init_quantization(s);
1013  if (ret=init_tiles(s))
1014  return ret;
1015 
1016  av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
1017 
1018  return 0;
1019 }
1020 
1021 static int j2kenc_destroy(AVCodecContext *avctx)
1022 {
1023  J2kEncoderContext *s = avctx->priv_data;
1024 
1025  cleanup(s);
1026  return 0;
1027 }
1028 
1030  .name = "j2k",
1031  .type = AVMEDIA_TYPE_VIDEO,
1032  .id = AV_CODEC_ID_JPEG2000,
1033  .priv_data_size = sizeof(J2kEncoderContext),
1034  .init = j2kenc_init,
1035  .encode2 = encode_frame,
1036  .close = j2kenc_destroy,
1037  .capabilities = CODEC_CAP_EXPERIMENTAL,
1038  .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1039  .pix_fmts = (const enum AVPixelFormat[]) {
1041 /* AV_PIX_FMT_YUV420P,
1042  AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
1043  AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,*/
1045  }
1046 };
static int ff_j2k_getsgnctxno(int flag, int *xorbit)
Definition: j2k.h:224
static int encode_packets(J2kEncoderContext *s, J2kTile *tile, int tileno)
Definition: j2kenc.c:738
const char * s
Definition: avisynth_c.h:668
#define NMSEDEC_BITS
Definition: j2kenc.c:35
#define J2K_T1_REF
Definition: j2k.h:86
start of tile-part
Definition: j2k.h:50
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
uint8_t log2_prec_height
precinct size
Definition: j2k.h:121
Definition: j2k.h:158
static int init_tiles(J2kEncoderContext *s)
compute the sizes of tiles, resolution levels, bands, etc.
Definition: j2kenc.c:337
static void tag_tree_update(J2kTgtNode *node)
update the value in node
Definition: j2kenc.c:219
AVCodec ff_jpeg2000_encoder
Definition: j2kenc.c:1029
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:73
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:70
static int put_siz(J2kEncoderContext *s)
Definition: j2kenc.c:231
J2kCblk * cblk
Definition: j2k.h:164
AVFrame * coded_frame
the picture in the bitstream
if max(w)>1 w=0.9 *w/max(w)
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define J2K_T1_VIS
Definition: j2k.h:84
uint16_t cblknx
Definition: j2k.h:161
uint8_t log2_prec_width
Definition: j2k.h:121
static int lut_nmsedec_ref[1<< NMSEDEC_BITS]
Definition: j2kenc.c:40
uint8_t log2_cblk_height
exponent of codeblock size
Definition: j2k.h:117
static av_cold int j2kenc_init(AVCodecContext *avctx)
Definition: j2kenc.c:962
uint16_t num_precincts_y
number of precincts in x/y direction
Definition: j2k.h:170
static int put_qcd(J2kEncoderContext *s, int compno)
Definition: j2kenc.c:287
quantization style
Definition: j2k.h:58
int * data
Definition: j2k.h:178
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
uint8_t data[8192]
Definition: j2k.h:148
int64_t disto
Definition: j2k.h:137
void ff_mqc_initenc(MqcState *mqc, uint8_t *bp)
initialize the encoder
Definition: mqcenc.c:68
uint8_t log2_cblk_width
Definition: j2k.h:117
AVCodecContext * avctx
Definition: j2kenc.c:62
static int ff_j2k_ceildivpow2(int a, int b)
Definition: j2k.h:191
Definition: j2k.h:140
void ff_j2k_reinit(J2kComponent *comp, J2kCodingStyle *codsty)
Definition: j2k.c:348
static int encode_packet(J2kEncoderContext *s, J2kResLevel *rlevel, int precno, uint8_t *expn, int numgbits)
Definition: j2kenc.c:643
uint32_t stepsize
quantization stepsize (* 2^13)
Definition: j2k.h:162
struct J2kTgtNode * parent
Definition: j2k.h:112
int chroma_shift[2]
Definition: j2kenc.c:67
uint8_t nonzerobits
Definition: j2k.h:143
DWTContext dwt
Definition: j2k.h:177
void ff_j2k_cleanup(J2kComponent *comp, J2kCodingStyle *codsty)
Definition: j2k.c:369
uint8_t * buf_start
Definition: j2kenc.c:73
uint16_t yi1
codeblock indexes ([xi0, xi1))
Definition: j2k.h:153
static int getnmsedec_ref(int x, int bpno)
Definition: j2kenc.c:469
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
initialize output if(nPeaks >3)%at least 3 peaks in spectrum for trying to find f0 nf0peaks
int height
image width and height
Definition: j2kenc.c:65
uint16_t rate
Definition: j2k.h:136
J2kTile * tile
Definition: j2kenc.c:83
J2kComponent * comp
Definition: j2kdec.c:44
quantization default
Definition: j2k.h:42
uint8_t npasses
Definition: j2k.h:141
start of codestream
Definition: j2k.h:35
J2kPrec * prec
Definition: j2k.h:163
MqcState mqc
Definition: j2k.h:106
uint8_t
#define av_cold
Definition: attributes.h:78
JPEG2000 tables.
static int lut_nmsedec_ref0[1<< NMSEDEC_BITS]
Definition: j2kenc.c:40
uint8_t transform
DWT type.
Definition: j2k.h:119
uint8_t vis
Definition: j2k.h:111
static AVPacket pkt
Definition: demuxing.c:56
uint16_t codeblock_width
Definition: j2k.h:160
J2kTgtNode * cblkincl
Definition: j2k.h:155
static uint8_t * put_sot(J2kEncoderContext *s, int tileno)
Definition: j2kenc.c:313
AVFrame picture
Definition: j2kenc.c:63
static int encode_tile(J2kEncoderContext *s, J2kTile *tile, int tileno)
Definition: j2kenc.c:805
uint8_t * data
#define sp
Definition: regdef.h:63
uint8_t * buf_end
Definition: j2kenc.c:75
void ff_j2k_set_significant(J2kT1Context *t1, int x, int y, int negative)
Definition: j2k.c:181
static void put_num(J2kEncoderContext *s, int num, int n)
put n least significant bits of a number num
Definition: j2kenc.c:173
J2kTgtNode * zerobits
Definition: j2k.h:154
int ff_j2k_init_component(J2kComponent *comp, J2kCodingStyle *codsty, J2kQuantStyle *qntsty, int cbps, int dx, int dy)
Definition: j2k.c:202
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Discrete Time axis x
static void encode_cblk(J2kEncoderContext *s, J2kT1Context *t1, J2kCblk *cblk, J2kTile *tile, int width, int height, int bandpos, int lev)
Definition: j2kenc.c:569
int flags[J2K_MAX_CBLKW+2][J2K_MAX_CBLKH+2]
Definition: j2k.h:105
static const uint16_t mask[17]
Definition: lzw.c:37
uint16_t num_precincts_x
Definition: j2k.h:170
static int getcut(J2kCblk *cblk, int64_t lambda, int dwt_norm)
Definition: j2kenc.c:761
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define t1
Definition: regdef.h:29
static void cleanup(J2kEncoderContext *s)
Definition: j2kenc.c:883
Definition: j2kdec.c:43
int data[J2K_MAX_CBLKW][J2K_MAX_CBLKH]
Definition: j2k.h:104
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
const char * name
Name of the codec implementation.
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: j2kenc.c:908
J2kBand * band
Definition: j2k.h:172
static void put_bits(J2kEncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:160
#define FFMAX(a, b)
Definition: common.h:56
external API header
int size
int flags
A combination of AV_PKT_FLAG values.
coding style default
Definition: j2k.h:37
uint8_t cbps[4]
bits per sample in particular components
Definition: j2kenc.c:66
uint16_t codeblock_height
Definition: j2k.h:160
static int lut_nmsedec_sig0[1<< NMSEDEC_BITS]
Definition: j2kenc.c:40
static void tag_tree_code(J2kEncoderContext *s, J2kTgtNode *node, int threshold)
code the value stored in node
Definition: j2kenc.c:191
static int getnmsedec_sig(int x, int bpno)
Definition: j2kenc.c:462
uint16_t xi0
Definition: j2k.h:153
#define FFMIN(a, b)
Definition: common.h:58
#define FF_MIN_BUFFER_SIZE
minimum encoding buffer size Used to avoid some checks during header writing.
ret
Definition: avfilter.c:821
int width
picture width / height.
static int ff_j2k_getnbctxno(int flag, int bandno, int vert_causal_ctx_csty_symbol)
Definition: j2k.h:211
#define J2K_T1_SIG_NB
Definition: j2k.h:76
static int put_cod(J2kEncoderContext *s)
Definition: j2kenc.c:260
uint8_t planar
Definition: j2kenc.c:68
end of codestream
Definition: j2k.h:54
static int j2kenc_destroy(AVCodecContext *avctx)
Definition: j2kenc.c:1021
#define MQC_CX_RL
Definition: mqc.h:34
image and tile size
Definition: j2k.h:36
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
Definition: frame.h:185
uint16_t mant[32 *3]
quantization mantissa
Definition: j2k.h:130
uint8_t nbands
Definition: j2k.h:168
void avcodec_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: imgconvert.c:65
static void j2k_flush(J2kEncoderContext *s)
flush the bitstream
Definition: j2kenc.c:180
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
int64_t lambda
Definition: j2kenc.c:78
uint8_t nreslevels
number of resolution levels
Definition: j2k.h:116
NULL
Definition: eval.c:55
static int width
Definition: tests/utils.c:158
static void truncpasses(J2kEncoderContext *s, J2kTile *tile)
Definition: j2kenc.c:779
#define J2K_T1_SGN
Definition: j2k.h:88
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
static void encode_clnpass(J2kT1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
Definition: j2kenc.c:513
main external API structure.
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:375
static int ff_j2k_ceildiv(int a, int b)
Definition: j2k.h:196
static void encode_sigpass(J2kT1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
Definition: j2kenc.c:476
static void init_luts(void)
Definition: j2kenc.c:444
static void copy_frame(J2kEncoderContext *s)
Definition: j2kenc.c:376
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
J2kQuantStyle qntsty
Definition: j2kenc.c:81
synthesis window for stochastic i
int ff_j2k_dwt_encode(DWTContext *s, int *t)
Definition: j2k_dwt.c:357
static void init_quantization(J2kEncoderContext *s)
Definition: j2kenc.c:414
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
uint16_t cblkny
Definition: j2k.h:161
uint8_t * buf
Definition: j2kenc.c:74
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
static void putnumpasses(J2kEncoderContext *s, int n)
Definition: j2kenc.c:628
#define J2K_T1_SIG
Definition: j2k.h:85
start of data
Definition: j2k.h:53
Y , 8bpp.
Definition: pixfmt.h:76
uint16_t yi0
Definition: j2k.h:153
common internal api header.
int ff_mqc_flush(MqcState *mqc)
flush the encoder [returns number of bytes encoded]
Definition: mqcenc.c:109
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:162
common internal and external API header
int ff_mqc_length(MqcState *mqc)
number of encoded bytes
Definition: mqcenc.c:104
uint16_t coord[2][2]
border coordinates {{x0, x1}, {y0, y1}}
Definition: j2k.h:169
uint8_t val
Definition: j2k.h:110
#define CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
scalar expoounded
Definition: j2k.h:60
#define LAMBDA_SCALE
Definition: j2kenc.c:38
uint8_t ninclpasses
number coding of passes included in codestream
Definition: j2k.h:142
function y
Definition: D.m:1
uint16_t coord[2][2]
border coordinates {{x0, x1}, {y0, y1}}
Definition: j2k.h:179
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition: bytestream.h:337
uint8_t nguardbits
number of guard bits
Definition: j2k.h:132
#define MQC_CX_UNI
Definition: mqc.h:33
void ff_j2k_init_tier1_luts(void)
Definition: j2k.c:170
static void reinit(J2kEncoderContext *s)
Definition: j2kenc.c:898
#define av_log2
Definition: intmath.h:89
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
About Git write you should know how to use GIT properly Luckily Git comes with excellent documentation git help man git shows you the available git< command > help man git< command > shows information about the subcommand< command > The most comprehensive manual is the website Git Reference visit they are quite exhaustive You do not need a special username or password All you need is to provide a ssh public key to the Git server admin What follows now is a basic introduction to Git and some FFmpeg specific guidelines Read it at least if you are granted commit privileges to the FFmpeg project you are expected to be familiar with these rules I if not You can get git from etc no matter how small Every one of them has been saved from looking like a fool by this many times It s very easy for stray debug output or cosmetic modifications to slip please avoid problems through this extra level of scrutiny For cosmetics only commits you should e g by running git config global user name My Name git config global user email my email which is either set in your personal configuration file through git config core editor or set by one of the following environment VISUAL or EDITOR Log messages should be concise but descriptive Explain why you made a what you did will be obvious from the changes themselves most of the time Saying just bug fix or is bad Remember that people of varying skill levels look at and educate themselves while reading through your code Don t include filenames in log Git provides that information Possibly make the commit message have a descriptive first line
Definition: git-howto.txt:153
J2kResLevel * reslevel
Definition: j2k.h:176
uint8_t quantsty
quantization style
Definition: j2k.h:131
static int lut_nmsedec_sig[1<< NMSEDEC_BITS]
Definition: j2kenc.c:40
uint8_t expn[32 *3]
quantization exponent
Definition: j2k.h:129
J2kPass passes[100]
Definition: j2k.h:149
static void comp(unsigned char *dst, int dst_stride, unsigned char *src, int src_stride, int add)
Definition: eamad.c:71
Definition: j2k.h:152
uint16_t xi1
Definition: j2k.h:153
static int ff_j2k_getrefctxno(int flag)
Definition: j2k.h:216
uint16_t coord[2][2]
border coordinates {{x0, x1}, {y0, y1}}
Definition: j2k.h:159
const char int length
Definition: avisynth_c.h:668
#define NMSEDEC_FRACBITS
Definition: j2kenc.c:36
J2kCodingStyle codsty
Definition: j2kenc.c:80
static const int dwt_norms[2][4][10]
Definition: j2kenc.c:45
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
This structure stores compressed data.
int tile_height
tile size
Definition: j2kenc.c:70
static void encode_refpass(J2kT1Context *t1, int width, int height, int *nmsedec, int bpno)
Definition: j2kenc.c:499
uint8_t cx_states[19]
Definition: mqc.h:45
#define WMSEDEC_SHIFT
must be >= 13
Definition: j2kenc.c:37
void ff_mqc_encode(MqcState *mqc, uint8_t *cxstate, int d)
code bit d with context cx
Definition: mqcenc.c:78