FFmpeg  2.6.9
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  * notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  * notice, this list of conditions and the following disclaimer in the
44  * documentation and/or other materials provided with the distribution.
45  *
46  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
47  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
50  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
52  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
56  * POSSIBILITY OF SUCH DAMAGE.
57  */
58 
59 
60 /**
61  * JPEG2000 image encoder
62  * @file
63  * @author Kamil Nowosad
64  */
65 
66 #include <float.h>
67 #include "avcodec.h"
68 #include "internal.h"
69 #include "bytestream.h"
70 #include "jpeg2000.h"
71 #include "libavutil/common.h"
72 
73 #define NMSEDEC_BITS 7
74 #define NMSEDEC_FRACBITS (NMSEDEC_BITS-1)
75 #define WMSEDEC_SHIFT 13 ///< must be >= 13
76 #define LAMBDA_SCALE (100000000LL << (WMSEDEC_SHIFT - 13))
77 
78 static int lut_nmsedec_ref [1<<NMSEDEC_BITS],
82 
83 static const int dwt_norms[2][4][10] = { // [dwt_type][band][rlevel] (multiplied by 10000)
84  {{10000, 19650, 41770, 84030, 169000, 338400, 676900, 1353000, 2706000, 5409000},
85  {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
86  {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
87  {20800, 38650, 83070, 171800, 347100, 695900, 1393000, 2786000, 5572000}},
88 
89  {{10000, 15000, 27500, 53750, 106800, 213400, 426700, 853300, 1707000, 3413000},
90  {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
91  {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
92  { 7186, 9218, 15860, 30430, 60190, 120100, 240000, 479700, 959300}}
93 };
94 
95 typedef struct {
97 } Jpeg2000Tile;
98 
99 typedef struct {
101  const AVFrame *picture;
102 
103  int width, height; ///< image width and height
104  uint8_t cbps[4]; ///< bits per sample in particular components
105  int chroma_shift[2];
108  int tile_width, tile_height; ///< tile size
109  int numXtiles, numYtiles;
110 
115 
116  int64_t lambda;
117 
120 
123 
124 
125 /* debug */
126 #if 0
127 #undef ifprintf
128 #undef printf
129 
130 static void nspaces(FILE *fd, int n)
131 {
132  while(n--) putc(' ', fd);
133 }
134 
135 static void printcomp(Jpeg2000Component *comp)
136 {
137  int i;
138  for (i = 0; i < comp->y1 - comp->y0; i++)
139  ff_jpeg2000_printv(comp->i_data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
140 }
141 
142 static void dump(Jpeg2000EncoderContext *s, FILE *fd)
143 {
144  int tileno, compno, reslevelno, bandno, precno;
145  fprintf(fd, "XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
146  "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
147  "tiles:\n",
148  s->width, s->height, s->tile_width, s->tile_height,
149  s->numXtiles, s->numYtiles, s->ncomponents);
150  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
151  Jpeg2000Tile *tile = s->tile + tileno;
152  nspaces(fd, 2);
153  fprintf(fd, "tile %d:\n", tileno);
154  for(compno = 0; compno < s->ncomponents; compno++){
155  Jpeg2000Component *comp = tile->comp + compno;
156  nspaces(fd, 4);
157  fprintf(fd, "component %d:\n", compno);
158  nspaces(fd, 4);
159  fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
160  comp->x0, comp->x1, comp->y0, comp->y1);
161  for(reslevelno = 0; reslevelno < s->nreslevels; reslevelno++){
162  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
163  nspaces(fd, 6);
164  fprintf(fd, "reslevel %d:\n", reslevelno);
165  nspaces(fd, 6);
166  fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
167  reslevel->x0, reslevel->x1, reslevel->y0,
168  reslevel->y1, reslevel->nbands);
169  for(bandno = 0; bandno < reslevel->nbands; bandno++){
170  Jpeg2000Band *band = reslevel->band + bandno;
171  nspaces(fd, 8);
172  fprintf(fd, "band %d:\n", bandno);
173  nspaces(fd, 8);
174  fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
175  "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
176  band->x0, band->x1,
177  band->y0, band->y1,
178  band->codeblock_width, band->codeblock_height,
179  band->cblknx, band->cblkny);
180  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
181  Jpeg2000Prec *prec = band->prec + precno;
182  nspaces(fd, 10);
183  fprintf(fd, "prec %d:\n", precno);
184  nspaces(fd, 10);
185  fprintf(fd, "xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
186  prec->xi0, prec->xi1, prec->yi0, prec->yi1);
187  }
188  }
189  }
190  }
191  }
192 }
193 #endif
194 
195 /* bitstream routines */
196 
197 /** put n times val bit */
198 static void put_bits(Jpeg2000EncoderContext *s, int val, int n) // TODO: optimize
199 {
200  while (n-- > 0){
201  if (s->bit_index == 8)
202  {
203  s->bit_index = *s->buf == 0xff;
204  *(++s->buf) = 0;
205  }
206  *s->buf |= val << (7 - s->bit_index++);
207  }
208 }
209 
210 /** put n least significant bits of a number num */
211 static void put_num(Jpeg2000EncoderContext *s, int num, int n)
212 {
213  while(--n >= 0)
214  put_bits(s, (num >> n) & 1, 1);
215 }
216 
217 /** flush the bitstream */
219 {
220  if (s->bit_index){
221  s->bit_index = 0;
222  s->buf++;
223  }
224 }
225 
226 /* tag tree routines */
227 
228 /** code the value stored in node */
229 static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold)
230 {
231  Jpeg2000TgtNode *stack[30];
232  int sp = 1, curval = 0;
233  stack[0] = node;
234 
235  node = node->parent;
236  while(node){
237  if (node->vis){
238  curval = node->val;
239  break;
240  }
241  node->vis++;
242  stack[sp++] = node;
243  node = node->parent;
244  }
245  while(--sp >= 0){
246  if (stack[sp]->val >= threshold){
247  put_bits(s, 0, threshold - curval);
248  break;
249  }
250  put_bits(s, 0, stack[sp]->val - curval);
251  put_bits(s, 1, 1);
252  curval = stack[sp]->val;
253  }
254 }
255 
256 /** update the value in node */
258 {
259  int lev = 0;
260  while (node->parent){
261  if (node->parent->val <= node->val)
262  break;
263  node->parent->val = node->val;
264  node = node->parent;
265  lev++;
266  }
267 }
268 
270 {
271  int i;
272 
273  if (s->buf_end - s->buf < 40 + 3 * s->ncomponents)
274  return -1;
275 
276  bytestream_put_be16(&s->buf, JPEG2000_SIZ);
277  bytestream_put_be16(&s->buf, 38 + 3 * s->ncomponents); // Lsiz
278  bytestream_put_be16(&s->buf, 0); // Rsiz
279  bytestream_put_be32(&s->buf, s->width); // width
280  bytestream_put_be32(&s->buf, s->height); // height
281  bytestream_put_be32(&s->buf, 0); // X0Siz
282  bytestream_put_be32(&s->buf, 0); // Y0Siz
283 
284  bytestream_put_be32(&s->buf, s->tile_width); // XTSiz
285  bytestream_put_be32(&s->buf, s->tile_height); // YTSiz
286  bytestream_put_be32(&s->buf, 0); // XT0Siz
287  bytestream_put_be32(&s->buf, 0); // YT0Siz
288  bytestream_put_be16(&s->buf, s->ncomponents); // CSiz
289 
290  for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
291  bytestream_put_byte(&s->buf, 7);
292  bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[0]:1);
293  bytestream_put_byte(&s->buf, i?1<<s->chroma_shift[1]:1);
294  }
295  return 0;
296 }
297 
299 {
300  Jpeg2000CodingStyle *codsty = &s->codsty;
301 
302  if (s->buf_end - s->buf < 14)
303  return -1;
304 
305  bytestream_put_be16(&s->buf, JPEG2000_COD);
306  bytestream_put_be16(&s->buf, 12); // Lcod
307  bytestream_put_byte(&s->buf, 0); // Scod
308  // SGcod
309  bytestream_put_byte(&s->buf, 0); // progression level
310  bytestream_put_be16(&s->buf, 1); // num of layers
311  if(s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
312  bytestream_put_byte(&s->buf, 2); // ICT
313  }else{
314  bytestream_put_byte(&s->buf, 0); // unspecified
315  }
316  // SPcod
317  bytestream_put_byte(&s->buf, codsty->nreslevels - 1); // num of decomp. levels
318  bytestream_put_byte(&s->buf, codsty->log2_cblk_width-2); // cblk width
319  bytestream_put_byte(&s->buf, codsty->log2_cblk_height-2); // cblk height
320  bytestream_put_byte(&s->buf, 0); // cblk style
321  bytestream_put_byte(&s->buf, codsty->transform == FF_DWT53); // transformation
322  return 0;
323 }
324 
325 static int put_qcd(Jpeg2000EncoderContext *s, int compno)
326 {
327  int i, size;
328  Jpeg2000CodingStyle *codsty = &s->codsty;
329  Jpeg2000QuantStyle *qntsty = &s->qntsty;
330 
331  if (qntsty->quantsty == JPEG2000_QSTY_NONE)
332  size = 4 + 3 * (codsty->nreslevels-1);
333  else // QSTY_SE
334  size = 5 + 6 * (codsty->nreslevels-1);
335 
336  if (s->buf_end - s->buf < size + 2)
337  return -1;
338 
339  bytestream_put_be16(&s->buf, JPEG2000_QCD);
340  bytestream_put_be16(&s->buf, size); // LQcd
341  bytestream_put_byte(&s->buf, (qntsty->nguardbits << 5) | qntsty->quantsty); // Sqcd
342  if (qntsty->quantsty == JPEG2000_QSTY_NONE)
343  for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
344  bytestream_put_byte(&s->buf, qntsty->expn[i] << 3);
345  else // QSTY_SE
346  for (i = 0; i < codsty->nreslevels * 3 - 2; i++)
347  bytestream_put_be16(&s->buf, (qntsty->expn[i] << 11) | qntsty->mant[i]);
348  return 0;
349 }
350 
351 static uint8_t *put_sot(Jpeg2000EncoderContext *s, int tileno)
352 {
353  uint8_t *psotptr;
354 
355  if (s->buf_end - s->buf < 12)
356  return NULL;
357 
358  bytestream_put_be16(&s->buf, JPEG2000_SOT);
359  bytestream_put_be16(&s->buf, 10); // Lsot
360  bytestream_put_be16(&s->buf, tileno); // Isot
361 
362  psotptr = s->buf;
363  bytestream_put_be32(&s->buf, 0); // Psot (filled in later)
364 
365  bytestream_put_byte(&s->buf, 0); // TPsot
366  bytestream_put_byte(&s->buf, 1); // TNsot
367  return psotptr;
368 }
369 
370 /**
371  * compute the sizes of tiles, resolution levels, bands, etc.
372  * allocate memory for them
373  * divide the input image into tile-components
374  */
376 {
377  int tileno, tilex, tiley, compno;
378  Jpeg2000CodingStyle *codsty = &s->codsty;
379  Jpeg2000QuantStyle *qntsty = &s->qntsty;
380 
383 
384  s->tile = av_malloc_array(s->numXtiles, s->numYtiles * sizeof(Jpeg2000Tile));
385  if (!s->tile)
386  return AVERROR(ENOMEM);
387  for (tileno = 0, tiley = 0; tiley < s->numYtiles; tiley++)
388  for (tilex = 0; tilex < s->numXtiles; tilex++, tileno++){
389  Jpeg2000Tile *tile = s->tile + tileno;
390 
391  tile->comp = av_mallocz_array(s->ncomponents, sizeof(Jpeg2000Component));
392  if (!tile->comp)
393  return AVERROR(ENOMEM);
394  for (compno = 0; compno < s->ncomponents; compno++){
395  Jpeg2000Component *comp = tile->comp + compno;
396  int ret, i, j;
397 
398  comp->coord[0][0] = comp->coord_o[0][0] = tilex * s->tile_width;
399  comp->coord[0][1] = comp->coord_o[0][1] = FFMIN((tilex+1)*s->tile_width, s->width);
400  comp->coord[1][0] = comp->coord_o[1][0] = tiley * s->tile_height;
401  comp->coord[1][1] = comp->coord_o[1][1] = FFMIN((tiley+1)*s->tile_height, s->height);
402  if (compno > 0)
403  for (i = 0; i < 2; i++)
404  for (j = 0; j < 2; j++)
405  comp->coord[i][j] = comp->coord_o[i][j] = ff_jpeg2000_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
406 
407  if (ret = ff_jpeg2000_init_component(comp,
408  codsty,
409  qntsty,
410  s->cbps[compno],
411  compno?1<<s->chroma_shift[0]:1,
412  compno?1<<s->chroma_shift[1]:1,
413  s->avctx
414  ))
415  return ret;
416  }
417  }
418  return 0;
419 }
420 
422 {
423  int tileno, compno, i, y, x;
424  uint8_t *line;
425  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
426  Jpeg2000Tile *tile = s->tile + tileno;
427  if (s->planar){
428  for (compno = 0; compno < s->ncomponents; compno++){
429  Jpeg2000Component *comp = tile->comp + compno;
430  int *dst = comp->i_data;
431  line = s->picture->data[compno]
432  + comp->coord[1][0] * s->picture->linesize[compno]
433  + comp->coord[0][0];
434  for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){
435  uint8_t *ptr = line;
436  for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++)
437  *dst++ = *ptr++ - (1 << 7);
438  line += s->picture->linesize[compno];
439  }
440  }
441  } else{
442  line = s->picture->data[0] + tile->comp[0].coord[1][0] * s->picture->linesize[0]
443  + tile->comp[0].coord[0][0] * s->ncomponents;
444 
445  i = 0;
446  for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){
447  uint8_t *ptr = line;
448  for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){
449  for (compno = 0; compno < s->ncomponents; compno++){
450  tile->comp[compno].i_data[i] = *ptr++ - (1 << 7);
451  }
452  }
453  line += s->picture->linesize[0];
454  }
455  }
456  }
457 }
458 
460 {
461  int compno, reslevelno, bandno;
462  Jpeg2000QuantStyle *qntsty = &s->qntsty;
463  Jpeg2000CodingStyle *codsty = &s->codsty;
464 
465  for (compno = 0; compno < s->ncomponents; compno++){
466  int gbandno = 0;
467  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
468  int nbands, lev = codsty->nreslevels - reslevelno - 1;
469  nbands = reslevelno ? 3 : 1;
470  for (bandno = 0; bandno < nbands; bandno++, gbandno++){
471  int expn, mant;
472 
473  if (codsty->transform == FF_DWT97_INT){
474  int bandpos = bandno + (reslevelno>0),
475  ss = 81920000 / dwt_norms[0][bandpos][lev],
476  log = av_log2(ss);
477  mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
478  expn = s->cbps[compno] - log + 13;
479  } else
480  expn = ((bandno&2)>>1) + (reslevelno>0) + s->cbps[compno];
481 
482  qntsty->expn[gbandno] = expn;
483  qntsty->mant[gbandno] = mant;
484  }
485  }
486  }
487 }
488 
489 static void init_luts(void)
490 {
491  int i, a,
492  mask = ~((1<<NMSEDEC_FRACBITS)-1);
493 
494  for (i = 0; i < (1 << NMSEDEC_BITS); i++){
495  lut_nmsedec_sig[i] = FFMAX(6*i - (9<<NMSEDEC_FRACBITS-1) << 12-NMSEDEC_FRACBITS, 0);
496  lut_nmsedec_sig0[i] = FFMAX((i*i + (1<<NMSEDEC_FRACBITS-1) & mask) << 1, 0);
497 
498  a = (i >> (NMSEDEC_BITS-2)&2) + 1;
499  lut_nmsedec_ref[i] = FFMAX((-2*i + (1<<NMSEDEC_FRACBITS) + a*i - (a*a<<NMSEDEC_FRACBITS-2))
500  << 13-NMSEDEC_FRACBITS, 0);
501  lut_nmsedec_ref0[i] = FFMAX(((i*i + (1-4*i << NMSEDEC_FRACBITS-1) + (1<<2*NMSEDEC_FRACBITS)) & mask)
502  << 1, 0);
503  }
504 }
505 
506 /* tier-1 routines */
507 static int getnmsedec_sig(int x, int bpno)
508 {
509  if (bpno > NMSEDEC_FRACBITS)
510  return lut_nmsedec_sig[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
511  return lut_nmsedec_sig0[x & ((1 << NMSEDEC_BITS) - 1)];
512 }
513 
514 static int getnmsedec_ref(int x, int bpno)
515 {
516  if (bpno > NMSEDEC_FRACBITS)
517  return lut_nmsedec_ref[(x >> (bpno - NMSEDEC_FRACBITS)) & ((1 << NMSEDEC_BITS) - 1)];
518  return lut_nmsedec_ref0[x & ((1 << NMSEDEC_BITS) - 1)];
519 }
520 
521 static void encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
522 {
523  int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
524  for (y0 = 0; y0 < height; y0 += 4)
525  for (x = 0; x < width; x++)
526  for (y = y0; y < height && y < y0+4; y++){
527  if (!(t1->flags[y+1][x+1] & JPEG2000_T1_SIG) && (t1->flags[y+1][x+1] & JPEG2000_T1_SIG_NB)){
528  int ctxno = ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1], bandno),
529  bit = t1->data[y][x] & mask ? 1 : 0;
530  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, bit);
531  if (bit){
532  int xorbit;
533  int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
534  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
535  *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
536  ff_jpeg2000_set_significance(t1, x, y, t1->flags[y+1][x+1] >> 15);
537  }
538  t1->flags[y+1][x+1] |= JPEG2000_T1_VIS;
539  }
540  }
541 }
542 
543 static void encode_refpass(Jpeg2000T1Context *t1, int width, int height, int *nmsedec, int bpno)
544 {
545  int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
546  for (y0 = 0; y0 < height; y0 += 4)
547  for (x = 0; x < width; x++)
548  for (y = y0; y < height && y < y0+4; y++)
549  if ((t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG){
550  int ctxno = ff_jpeg2000_getrefctxno(t1->flags[y+1][x+1]);
551  *nmsedec += getnmsedec_ref(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
552  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
553  t1->flags[y+1][x+1] |= JPEG2000_T1_REF;
554  }
555 }
556 
557 static void encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
558 {
559  int y0, x, y, mask = 1 << (bpno + NMSEDEC_FRACBITS);
560  for (y0 = 0; y0 < height; y0 += 4)
561  for (x = 0; x < width; x++){
562  if (y0 + 3 < height && !(
563  (t1->flags[y0+1][x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
564  (t1->flags[y0+2][x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
565  (t1->flags[y0+3][x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
566  (t1->flags[y0+4][x+1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG))))
567  {
568  // aggregation mode
569  int rlen;
570  for (rlen = 0; rlen < 4; rlen++)
571  if (t1->data[y0+rlen][x] & mask)
572  break;
573  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL, rlen != 4);
574  if (rlen == 4)
575  continue;
576  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen >> 1);
577  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI, rlen & 1);
578  for (y = y0 + rlen; y < y0 + 4; y++){
579  if (!(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
580  int ctxno = ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1], bandno);
581  if (y > y0 + rlen)
582  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
583  if (t1->data[y][x] & mask){ // newly significant
584  int xorbit;
585  int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
586  *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
587  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
588  ff_jpeg2000_set_significance(t1, x, y, t1->flags[y+1][x+1] >> 15);
589  }
590  }
591  t1->flags[y+1][x+1] &= ~JPEG2000_T1_VIS;
592  }
593  } else{
594  for (y = y0; y < y0 + 4 && y < height; y++){
595  if (!(t1->flags[y+1][x+1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))){
596  int ctxno = ff_jpeg2000_getsigctxno(t1->flags[y+1][x+1], bandno);
597  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, t1->data[y][x] & mask ? 1:0);
598  if (t1->data[y][x] & mask){ // newly significant
599  int xorbit;
600  int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y+1][x+1], &xorbit);
601  *nmsedec += getnmsedec_sig(t1->data[y][x], bpno + NMSEDEC_FRACBITS);
602  ff_mqc_encode(&t1->mqc, t1->mqc.cx_states + ctxno, (t1->flags[y+1][x+1] >> 15) ^ xorbit);
603  ff_jpeg2000_set_significance(t1, x, y, t1->flags[y+1][x+1] >> 15);
604  }
605  }
606  t1->flags[y+1][x+1] &= ~JPEG2000_T1_VIS;
607  }
608  }
609  }
610 }
611 
613  int width, int height, int bandpos, int lev)
614 {
615  int pass_t = 2, passno, x, y, max=0, nmsedec, bpno;
616  int64_t wmsedec = 0;
617 
618  for (y = 0; y < height+2; y++)
619  memset(t1->flags[y], 0, (width+2)*sizeof(int));
620 
621  for (y = 0; y < height; y++){
622  for (x = 0; x < width; x++){
623  if (t1->data[y][x] < 0){
624  t1->flags[y+1][x+1] |= JPEG2000_T1_SGN;
625  t1->data[y][x] = -t1->data[y][x];
626  }
627  max = FFMAX(max, t1->data[y][x]);
628  }
629  }
630 
631  if (max == 0){
632  cblk->nonzerobits = 0;
633  bpno = 0;
634  } else{
635  cblk->nonzerobits = av_log2(max) + 1 - NMSEDEC_FRACBITS;
636  bpno = cblk->nonzerobits - 1;
637  }
638 
639  ff_mqc_initenc(&t1->mqc, cblk->data);
640 
641  for (passno = 0; bpno >= 0; passno++){
642  nmsedec=0;
643 
644  switch(pass_t){
645  case 0: encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
646  break;
647  case 1: encode_refpass(t1, width, height, &nmsedec, bpno);
648  break;
649  case 2: encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
650  break;
651  }
652 
653  cblk->passes[passno].rate = 3 + ff_mqc_length(&t1->mqc);
654  wmsedec += (int64_t)nmsedec << (2*bpno);
655  cblk->passes[passno].disto = wmsedec;
656 
657  if (++pass_t == 3){
658  pass_t = 0;
659  bpno--;
660  }
661  }
662  cblk->npasses = passno;
663  cblk->ninclpasses = passno;
664 
665  // TODO: optional flush on each pass
666  cblk->passes[passno-1].rate = ff_mqc_flush(&t1->mqc);
667 }
668 
669 /* tier-2 routines: */
670 
672 {
673  if (n == 1)
674  put_num(s, 0, 1);
675  else if (n == 2)
676  put_num(s, 2, 2);
677  else if (n <= 5)
678  put_num(s, 0xc | (n-3), 4);
679  else if (n <= 36)
680  put_num(s, 0x1e0 | (n-6), 9);
681  else
682  put_num(s, 0xff80 | (n-37), 16);
683 }
684 
685 
686 static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int precno,
687  uint8_t *expn, int numgbits)
688 {
689  int bandno, empty = 1;
690 
691  // init bitstream
692  *s->buf = 0;
693  s->bit_index = 0;
694 
695  // header
696 
697  // is the packet empty?
698  for (bandno = 0; bandno < rlevel->nbands; bandno++){
699  if (rlevel->band[bandno].coord[0][0] < rlevel->band[bandno].coord[0][1]
700  && rlevel->band[bandno].coord[1][0] < rlevel->band[bandno].coord[1][1]){
701  empty = 0;
702  break;
703  }
704  }
705 
706  put_bits(s, !empty, 1);
707  if (empty){
708  j2k_flush(s);
709  return 0;
710  }
711 
712  for (bandno = 0; bandno < rlevel->nbands; bandno++){
713  Jpeg2000Band *band = rlevel->band + bandno;
714  Jpeg2000Prec *prec = band->prec + precno;
715  int yi, xi, pos;
716  int cblknw = prec->nb_codeblocks_width;
717 
718  if (band->coord[0][0] == band->coord[0][1]
719  || band->coord[1][0] == band->coord[1][1])
720  continue;
721 
722  for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++){
723  for (xi = 0; xi < cblknw; xi++, pos++){
724  prec->cblkincl[pos].val = prec->cblk[yi * cblknw + xi].ninclpasses == 0;
725  tag_tree_update(prec->cblkincl + pos);
726  prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - prec->cblk[yi * cblknw + xi].nonzerobits;
727  tag_tree_update(prec->zerobits + pos);
728  }
729  }
730 
731  for (pos=0, yi = 0; yi < prec->nb_codeblocks_height; yi++){
732  for (xi = 0; xi < cblknw; xi++, pos++){
733  int pad = 0, llen, length;
734  Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
735 
736  if (s->buf_end - s->buf < 20) // approximately
737  return -1;
738 
739  // inclusion information
740  tag_tree_code(s, prec->cblkincl + pos, 1);
741  if (!cblk->ninclpasses)
742  continue;
743  // zerobits information
744  tag_tree_code(s, prec->zerobits + pos, 100);
745  // number of passes
746  putnumpasses(s, cblk->ninclpasses);
747 
748  length = cblk->passes[cblk->ninclpasses-1].rate;
749  llen = av_log2(length) - av_log2(cblk->ninclpasses) - 2;
750  if (llen < 0){
751  pad = -llen;
752  llen = 0;
753  }
754  // length of code block
755  put_bits(s, 1, llen);
756  put_bits(s, 0, 1);
757  put_num(s, length, av_log2(length)+1+pad);
758  }
759  }
760  }
761  j2k_flush(s);
762  for (bandno = 0; bandno < rlevel->nbands; bandno++){
763  Jpeg2000Band *band = rlevel->band + bandno;
764  Jpeg2000Prec *prec = band->prec + precno;
765  int yi, cblknw = prec->nb_codeblocks_width;
766  for (yi =0; yi < prec->nb_codeblocks_height; yi++){
767  int xi;
768  for (xi = 0; xi < cblknw; xi++){
769  Jpeg2000Cblk *cblk = prec->cblk + yi * cblknw + xi;
770  if (cblk->ninclpasses){
771  if (s->buf_end - s->buf < cblk->passes[cblk->ninclpasses-1].rate)
772  return -1;
773  bytestream_put_buffer(&s->buf, cblk->data, cblk->passes[cblk->ninclpasses-1].rate);
774  }
775  }
776  }
777  }
778  return 0;
779 }
780 
781 static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
782 {
783  int compno, reslevelno, ret;
784  Jpeg2000CodingStyle *codsty = &s->codsty;
785  Jpeg2000QuantStyle *qntsty = &s->qntsty;
786 
787  av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
788  // lay-rlevel-comp-pos progression
789  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
790  for (compno = 0; compno < s->ncomponents; compno++){
791  int precno;
792  Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel + reslevelno;
793  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
794  if (ret = encode_packet(s, reslevel, precno, qntsty->expn + (reslevelno ? 3*reslevelno-2 : 0),
795  qntsty->nguardbits))
796  return ret;
797  }
798  }
799  }
800  av_log(s->avctx, AV_LOG_DEBUG, "after tier2\n");
801  return 0;
802 }
803 
804 static int getcut(Jpeg2000Cblk *cblk, int64_t lambda, int dwt_norm)
805 {
806  int passno, res = 0;
807  for (passno = 0; passno < cblk->npasses; passno++){
808  int dr;
809  int64_t dd;
810 
811  dr = cblk->passes[passno].rate
812  - (res ? cblk->passes[res-1].rate:0);
813  dd = cblk->passes[passno].disto
814  - (res ? cblk->passes[res-1].disto:0);
815 
816  if (((dd * dwt_norm) >> WMSEDEC_SHIFT) * dwt_norm >= dr * lambda)
817  res = passno+1;
818  }
819  return res;
820 }
821 
823 {
824  int precno, compno, reslevelno, bandno, cblkno, lev;
825  Jpeg2000CodingStyle *codsty = &s->codsty;
826 
827  for (compno = 0; compno < s->ncomponents; compno++){
828  Jpeg2000Component *comp = tile->comp + compno;
829 
830  for (reslevelno = 0, lev = codsty->nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
831  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
832 
833  for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++){
834  for (bandno = 0; bandno < reslevel->nbands ; bandno++){
835  int bandpos = bandno + (reslevelno > 0);
836  Jpeg2000Band *band = reslevel->band + bandno;
837  Jpeg2000Prec *prec = band->prec + precno;
838 
839  for (cblkno = 0; cblkno < prec->nb_codeblocks_height * prec->nb_codeblocks_width; cblkno++){
840  Jpeg2000Cblk *cblk = prec->cblk + cblkno;
841 
842  cblk->ninclpasses = getcut(cblk, s->lambda,
843  (int64_t)dwt_norms[codsty->transform == FF_DWT53][bandpos][lev] * (int64_t)band->i_stepsize >> 15);
844  }
845  }
846  }
847  }
848  }
849 }
850 
851 static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
852 {
853  int compno, reslevelno, bandno, ret;
855  Jpeg2000CodingStyle *codsty = &s->codsty;
856  for (compno = 0; compno < s->ncomponents; compno++){
857  Jpeg2000Component *comp = s->tile[tileno].comp + compno;
858 
859  av_log(s->avctx, AV_LOG_DEBUG,"dwt\n");
860  if (ret = ff_dwt_encode(&comp->dwt, comp->i_data))
861  return ret;
862  av_log(s->avctx, AV_LOG_DEBUG,"after dwt -> tier1\n");
863 
864  for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
865  Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
866 
867  for (bandno = 0; bandno < reslevel->nbands ; bandno++){
868  Jpeg2000Band *band = reslevel->band + bandno;
869  Jpeg2000Prec *prec = band->prec; // we support only 1 precinct per band ATM in the encoder
870  int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
871  yy0 = bandno == 0 ? 0 : comp->reslevel[reslevelno-1].coord[1][1] - comp->reslevel[reslevelno-1].coord[1][0];
872  y0 = yy0;
873  yy1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[1][0] + 1, band->log2_cblk_height) << band->log2_cblk_height,
874  band->coord[1][1]) - band->coord[1][0] + yy0;
875 
876  if (band->coord[0][0] == band->coord[0][1] || band->coord[1][0] == band->coord[1][1])
877  continue;
878 
879  bandpos = bandno + (reslevelno > 0);
880 
881  for (cblky = 0; cblky < prec->nb_codeblocks_height; cblky++){
882  if (reslevelno == 0 || bandno == 1)
883  xx0 = 0;
884  else
885  xx0 = comp->reslevel[reslevelno-1].coord[0][1] - comp->reslevel[reslevelno-1].coord[0][0];
886  x0 = xx0;
887  xx1 = FFMIN(ff_jpeg2000_ceildivpow2(band->coord[0][0] + 1, band->log2_cblk_width) << band->log2_cblk_width,
888  band->coord[0][1]) - band->coord[0][0] + xx0;
889 
890  for (cblkx = 0; cblkx < prec->nb_codeblocks_width; cblkx++, cblkno++){
891  int y, x;
892  if (codsty->transform == FF_DWT53){
893  for (y = yy0; y < yy1; y++){
894  int *ptr = t1.data[y-yy0];
895  for (x = xx0; x < xx1; x++){
896  *ptr++ = comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x] << NMSEDEC_FRACBITS;
897  }
898  }
899  } else{
900  for (y = yy0; y < yy1; y++){
901  int *ptr = t1.data[y-yy0];
902  for (x = xx0; x < xx1; x++){
903  *ptr = (comp->i_data[(comp->coord[0][1] - comp->coord[0][0]) * y + x]);
904  *ptr = (int64_t)*ptr * (int64_t)(16384 * 65536 / band->i_stepsize) >> 15 - NMSEDEC_FRACBITS;
905  ptr++;
906  }
907  }
908  }
909  encode_cblk(s, &t1, prec->cblk + cblkno, tile, xx1 - xx0, yy1 - yy0,
910  bandpos, codsty->nreslevels - reslevelno - 1);
911  xx0 = xx1;
912  xx1 = FFMIN(xx1 + (1 << band->log2_cblk_width), band->coord[0][1] - band->coord[0][0] + x0);
913  }
914  yy0 = yy1;
915  yy1 = FFMIN(yy1 + (1 << band->log2_cblk_height), band->coord[1][1] - band->coord[1][0] + y0);
916  }
917  }
918  }
919  av_log(s->avctx, AV_LOG_DEBUG, "after tier1\n");
920  }
921 
922  av_log(s->avctx, AV_LOG_DEBUG, "rate control\n");
923  truncpasses(s, tile);
924  if (ret = encode_packets(s, tile, tileno))
925  return ret;
926  av_log(s->avctx, AV_LOG_DEBUG, "after rate control\n");
927  return 0;
928 }
929 
931 {
932  int tileno, compno;
933  Jpeg2000CodingStyle *codsty = &s->codsty;
934 
935  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
936  for (compno = 0; compno < s->ncomponents; compno++){
937  Jpeg2000Component *comp = s->tile[tileno].comp + compno;
938  ff_jpeg2000_cleanup(comp, codsty);
939  }
940  av_freep(&s->tile[tileno].comp);
941  }
942  av_freep(&s->tile);
943 }
944 
946 {
947  int tileno, compno;
948  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
949  Jpeg2000Tile *tile = s->tile + tileno;
950  for (compno = 0; compno < s->ncomponents; compno++)
951  ff_jpeg2000_reinit(tile->comp + compno, &s->codsty);
952  }
953 }
954 
956  const AVFrame *pict, int *got_packet)
957 {
958  int tileno, ret;
959  Jpeg2000EncoderContext *s = avctx->priv_data;
960 
961  if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*9 + FF_MIN_BUFFER_SIZE)) < 0)
962  return ret;
963 
964  // init:
965  s->buf = s->buf_start = pkt->data;
966  s->buf_end = pkt->data + pkt->size;
967 
968  s->picture = pict;
969 
970  s->lambda = s->picture->quality * LAMBDA_SCALE;
971 
972  copy_frame(s);
973  reinit(s);
974 
975  if (s->buf_end - s->buf < 2)
976  return -1;
977  bytestream_put_be16(&s->buf, JPEG2000_SOC);
978  if (ret = put_siz(s))
979  return ret;
980  if (ret = put_cod(s))
981  return ret;
982  if (ret = put_qcd(s, 0))
983  return ret;
984 
985  for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
986  uint8_t *psotptr;
987  if (!(psotptr = put_sot(s, tileno)))
988  return -1;
989  if (s->buf_end - s->buf < 2)
990  return -1;
991  bytestream_put_be16(&s->buf, JPEG2000_SOD);
992  if (ret = encode_tile(s, s->tile + tileno, tileno))
993  return ret;
994  bytestream_put_be32(&psotptr, s->buf - psotptr + 6);
995  }
996  if (s->buf_end - s->buf < 2)
997  return -1;
998  bytestream_put_be16(&s->buf, JPEG2000_EOC);
999 
1000  av_log(s->avctx, AV_LOG_DEBUG, "end\n");
1001  pkt->size = s->buf - s->buf_start;
1002  pkt->flags |= AV_PKT_FLAG_KEY;
1003  *got_packet = 1;
1004 
1005  return 0;
1006 }
1007 
1009 {
1010  int i, ret;
1011  Jpeg2000EncoderContext *s = avctx->priv_data;
1012  Jpeg2000CodingStyle *codsty = &s->codsty;
1013  Jpeg2000QuantStyle *qntsty = &s->qntsty;
1014 
1015  s->avctx = avctx;
1016  av_log(s->avctx, AV_LOG_DEBUG, "init\n");
1017 
1018  // defaults:
1019  // TODO: implement setting non-standard precinct size
1020  memset(codsty->log2_prec_widths , 15, sizeof(codsty->log2_prec_widths ));
1021  memset(codsty->log2_prec_heights, 15, sizeof(codsty->log2_prec_heights));
1022  codsty->nreslevels2decode=
1023  codsty->nreslevels = 7;
1024  codsty->log2_cblk_width = 4;
1025  codsty->log2_cblk_height = 4;
1026  codsty->transform = avctx->prediction_method ? FF_DWT53 : FF_DWT97_INT;
1027 
1028  qntsty->nguardbits = 1;
1029 
1030  s->tile_width = 256;
1031  s->tile_height = 256;
1032 
1033  if (codsty->transform == FF_DWT53)
1034  qntsty->quantsty = JPEG2000_QSTY_NONE;
1035  else
1036  qntsty->quantsty = JPEG2000_QSTY_SE;
1037 
1038  s->width = avctx->width;
1039  s->height = avctx->height;
1040 
1041  for (i = 0; i < 3; i++)
1042  s->cbps[i] = 8;
1043 
1044  if (avctx->pix_fmt == AV_PIX_FMT_RGB24){
1045  s->ncomponents = 3;
1046  } else if (avctx->pix_fmt == AV_PIX_FMT_GRAY8){
1047  s->ncomponents = 1;
1048  } else{ // planar YUV
1049  s->planar = 1;
1050  s->ncomponents = 3;
1052  s->chroma_shift, s->chroma_shift + 1);
1053  }
1054 
1057  init_luts();
1058 
1059  init_quantization(s);
1060  if (ret=init_tiles(s))
1061  return ret;
1062 
1063  av_log(s->avctx, AV_LOG_DEBUG, "after init\n");
1064 
1065  return 0;
1066 }
1067 
1068 static int j2kenc_destroy(AVCodecContext *avctx)
1069 {
1070  Jpeg2000EncoderContext *s = avctx->priv_data;
1071 
1072  cleanup(s);
1073  return 0;
1074 }
1075 
1077  .name = "jpeg2000",
1078  .long_name = NULL_IF_CONFIG_SMALL("JPEG 2000"),
1079  .type = AVMEDIA_TYPE_VIDEO,
1080  .id = AV_CODEC_ID_JPEG2000,
1081  .priv_data_size = sizeof(Jpeg2000EncoderContext),
1082  .init = j2kenc_init,
1083  .encode2 = encode_frame,
1084  .close = j2kenc_destroy,
1085  .capabilities = CODEC_CAP_EXPERIMENTAL,
1086  .pix_fmts = (const enum AVPixelFormat[]) {
1088 /* AV_PIX_FMT_YUV420P,
1089  AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
1090  AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,*/
1092  }
1093 };
static int getcut(Jpeg2000Cblk *cblk, int64_t lambda, int dwt_norm)
Definition: j2kenc.c:804
uint8_t nguardbits
Definition: jpeg2000.h:151
#define NULL
Definition: coverity.c:32
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1724
const char const char void * val
Definition: avisynth_c.h:672
const char * s
Definition: avisynth_c.h:669
#define NMSEDEC_BITS
Definition: j2kenc.c:73
void av_cold ff_jpeg2000_init_tier1_luts(void)
Definition: jpeg2000.c:158
int flags[JPEG2000_MAX_CBLKW+2][JPEG2000_MAX_CBLKH+2]
Definition: jpeg2000.h:122
void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
Definition: jpeg2000.c:516
This structure describes decoded (raw) audio or video data.
Definition: frame.h:163
DWTContext dwt
Definition: jpeg2000.h:199
Jpeg2000TgtNode * cblkincl
Definition: jpeg2000.h:176
static void cleanup(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:930
#define JPEG2000_T1_SIG_NB
Definition: jpeg2000.h:84
AVCodec ff_jpeg2000_encoder
Definition: j2kenc.c:1076
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:73
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:198
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:70
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
uint8_t ninclpasses
Definition: jpeg2000.h:161
Jpeg2000QuantStyle qntsty
Definition: j2kenc.c:119
static int lut_nmsedec_ref[1<< NMSEDEC_BITS]
Definition: j2kenc.c:78
static av_cold int j2kenc_init(AVCodecContext *avctx)
Definition: j2kenc.c:1008
int size
Definition: avcodec.h:1161
AVCodecContext * avctx
Definition: j2kenc.c:100
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1442
uint16_t mant[JPEG2000_MAX_DECLEVELS *3]
Definition: jpeg2000.h:149
void ff_mqc_initenc(MqcState *mqc, uint8_t *bp)
initialize the encoder
Definition: mqcenc.c:68
static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold)
code the value stored in node
Definition: j2kenc.c:229
static AVPacket pkt
AVCodec.
Definition: avcodec.h:3173
uint16_t nb_codeblocks_height
Definition: jpeg2000.h:174
static void tag_tree_update(Jpeg2000TgtNode *node)
update the value in node
Definition: j2kenc.c:257
static int getnmsedec_ref(int x, int bpno)
Definition: j2kenc.c:514
uint8_t npasses
Definition: jpeg2000.h:160
Jpeg2000Pass passes[100]
Definition: jpeg2000.h:168
uint16_t log2_cblk_width
Definition: jpeg2000.h:183
if()
Definition: avfilter.c:975
uint8_t
#define av_cold
Definition: attributes.h:74
static int lut_nmsedec_ref0[1<< NMSEDEC_BITS]
Definition: j2kenc.c:78
static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int precno, uint8_t *expn, int numgbits)
Definition: j2kenc.c:686
Jpeg2000TgtNode * zerobits
Definition: jpeg2000.h:175
int64_t disto
Definition: jpeg2000.h:156
Jpeg2000Band * band
Definition: jpeg2000.h:194
uint8_t val
Definition: jpeg2000.h:127
uint8_t * data
Definition: avcodec.h:1160
static void j2k_flush(Jpeg2000EncoderContext *s)
flush the bitstream
Definition: j2kenc.c:218
uint16_t num_precincts_x
Definition: jpeg2000.h:192
#define sp
Definition: regdef.h:63
static int init_tiles(Jpeg2000EncoderContext *s)
compute the sizes of tiles, resolution levels, bands, etc.
Definition: j2kenc.c:375
#define JPEG2000_T1_VIS
Definition: jpeg2000.h:94
ptrdiff_t size
Definition: opengl_enc.c:101
#define av_log(a,...)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1206
uint8_t nonzerobits
Definition: jpeg2000.h:162
uint8_t log2_prec_widths[JPEG2000_MAX_RESLEVELS]
Definition: jpeg2000.h:143
static const uint16_t mask[17]
Definition: lzw.c:38
uint8_t * buf_end
Definition: j2kenc.c:113
void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1, int x, int y, int negative)
Definition: jpeg2000.c:170
int data[JPEG2000_MAX_CBLKW][JPEG2000_MAX_CBLKH]
Definition: jpeg2000.h:121
#define AVERROR(e)
Definition: error.h:43
static int put_cod(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:298
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:180
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:196
uint8_t log2_prec_heights[JPEG2000_MAX_RESLEVELS]
Definition: jpeg2000.h:144
#define t1
Definition: regdef.h:29
Definition: graph2dot.c:48
static void put_num(Jpeg2000EncoderContext *s, int num, int n)
put n least significant bits of a number num
Definition: j2kenc.c:211
GLsizei GLsizei * length
Definition: opengl_enc.c:115
const char * name
Name of the codec implementation.
Definition: avcodec.h:3180
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: j2kenc.c:955
const AVFrame * picture
Definition: j2kenc.c:101
Jpeg2000Cblk * cblk
Definition: jpeg2000.h:177
#define FFMAX(a, b)
Definition: common.h:79
Libavcodec external API header.
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1166
static int put_siz(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:269
static int lut_nmsedec_sig0[1<< NMSEDEC_BITS]
Definition: j2kenc.c:78
#define JPEG2000_T1_REF
Definition: jpeg2000.h:96
static int put_qcd(Jpeg2000EncoderContext *s, int compno)
Definition: j2kenc.c:325
static int getnmsedec_sig(int x, int bpno)
Definition: j2kenc.c:507
int tile_height
tile size
Definition: j2kenc.c:108
#define FFMIN(a, b)
Definition: common.h:81
uint8_t data[8192]
Definition: jpeg2000.h:167
struct Jpeg2000TgtNode * parent
Definition: jpeg2000.h:129
static void encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
Definition: j2kenc.c:521
float y
#define FF_MIN_BUFFER_SIZE
minimum encoding buffer size Used to avoid some checks during header writing.
Definition: avcodec.h:635
ret
Definition: avfilter.c:974
int width
picture width / height.
Definition: avcodec.h:1412
JPEG 2000 structures and defines common to encoder and decoder.
int i_stepsize
Definition: jpeg2000.h:184
static int j2kenc_destroy(AVCodecContext *avctx)
Definition: j2kenc.c:1068
#define MQC_CX_RL
Definition: mqc.h:34
uint16_t num_precincts_y
Definition: jpeg2000.h:192
static int ff_jpeg2000_getsigctxno(int flag, int bandno)
Definition: jpeg2000.h:231
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
Definition: frame.h:275
int n
Definition: avisynth_c.h:589
uint8_t cbps[4]
bits per sample in particular components
Definition: j2kenc.c:104
int ff_dwt_encode(DWTContext *s, void *t)
Definition: jpeg2000dwt.c:541
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:43
static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
Definition: j2kenc.c:851
static int ff_jpeg2000_ceildivpow2(int a, int b)
Definition: jpeg2000.h:207
uint16_t coord[2][2]
Definition: jpeg2000.h:182
static void encode_refpass(Jpeg2000T1Context *t1, int width, int height, int *nmsedec, int bpno)
Definition: j2kenc.c:543
uint8_t * buf_start
Definition: j2kenc.c:111
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:191
Jpeg2000CodingStyle codsty
Definition: j2kenc.c:118
main external API structure.
Definition: avcodec.h:1239
uint8_t vis
Definition: jpeg2000.h:128
void av_cold ff_mqc_init_context_tables(void)
MQ-coder Initialize context tables (QE, NLPS, NMPS)
Definition: mqc.c:97
static void init_luts(void)
Definition: j2kenc.c:489
static void encode_cblk(Jpeg2000EncoderContext *s, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, Jpeg2000Tile *tile, int width, int height, int bandpos, int lev)
Definition: j2kenc.c:612
BYTE int const BYTE int int int height
Definition: avisynth_c.h:714
uint8_t nbands
Definition: jpeg2000.h:190
#define JPEG2000_T1_SGN
Definition: jpeg2000.h:98
uint16_t nb_codeblocks_width
Definition: jpeg2000.h:173
uint16_t coord[2][2]
Definition: jpeg2000.h:202
int height
image width and height
Definition: j2kenc.c:103
Jpeg2000ResLevel * reslevel
Definition: jpeg2000.h:198
static int ff_jpeg2000_ceildiv(int a, int b)
Definition: jpeg2000.h:212
Jpeg2000Component * comp
Definition: j2kenc.c:96
void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
Definition: jpeg2000.c:495
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:174
uint8_t expn[JPEG2000_MAX_DECLEVELS *3]
Definition: jpeg2000.h:148
static void reinit(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:945
static uint8_t * put_sot(Jpeg2000EncoderContext *s, int tileno)
Definition: j2kenc.c:351
Jpeg2000Tile * tile
Definition: j2kenc.c:121
Y , 8bpp.
Definition: pixfmt.h:76
uint8_t quantsty
Definition: jpeg2000.h:150
common internal api header.
int ff_mqc_flush(MqcState *mqc)
flush the encoder [returns number of bytes encoded]
Definition: mqcenc.c:109
common internal and external API header
uint8_t log2_cblk_width
Definition: jpeg2000.h:135
static void copy_frame(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:421
int ff_mqc_length(MqcState *mqc)
number of encoded bytes
Definition: mqcenc.c:104
int prediction_method
prediction method (needed for huffyuv)
Definition: avcodec.h:1602
#define CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
Definition: avcodec.h:850
uint16_t coord_o[2][2]
Definition: jpeg2000.h:203
#define LAMBDA_SCALE
Definition: j2kenc.c:76
uint16_t log2_cblk_height
Definition: jpeg2000.h:183
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition: bytestream.h:368
#define MQC_CX_UNI
Definition: mqc.h:33
void * priv_data
Definition: avcodec.h:1281
int ff_jpeg2000_init_component(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, Jpeg2000QuantStyle *qntsty, int cbps, int dx, int dy, AVCodecContext *avctx)
Definition: jpeg2000.c:195
uint16_t rate
Definition: jpeg2000.h:155
static void putnumpasses(Jpeg2000EncoderContext *s, int n)
Definition: j2kenc.c:671
uint16_t coord[2][2]
Definition: jpeg2000.h:191
#define av_log2
Definition: intmath.h:105
static void truncpasses(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
Definition: j2kenc.c:822
Jpeg2000Prec * prec
Definition: jpeg2000.h:186
static void encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
Definition: j2kenc.c:557
#define JPEG2000_T1_SIG
Definition: jpeg2000.h:95
static int lut_nmsedec_sig[1<< NMSEDEC_BITS]
Definition: j2kenc.c:78
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:228
#define av_freep(p)
static void comp(unsigned char *dst, int dst_stride, unsigned char *src, int src_stride, int add)
Definition: eamad.c:83
MqcState mqc
Definition: jpeg2000.h:123
static int ff_jpeg2000_getrefctxno(int flag)
Definition: jpeg2000.h:240
uint8_t log2_cblk_height
Definition: jpeg2000.h:135
#define av_malloc_array(a, b)
#define NMSEDEC_FRACBITS
Definition: j2kenc.c:74
static void init_quantization(Jpeg2000EncoderContext *s)
Definition: j2kenc.c:459
static const int dwt_norms[2][4][10]
Definition: j2kenc.c:83
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
This structure stores compressed data.
Definition: avcodec.h:1137
uint8_t cx_states[19]
Definition: mqc.h:45
#define WMSEDEC_SHIFT
must be >= 13
Definition: j2kenc.c:75
static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
Definition: j2kenc.c:781
static int ff_jpeg2000_getsgnctxno(int flag, int *xorbit)
Definition: jpeg2000.h:249
static int width
void ff_mqc_encode(MqcState *mqc, uint8_t *cxstate, int d)
code bit d with context cx
Definition: mqcenc.c:78