1 /*
2 * JPEG2000 image encoder
3 * Copyright (c) 2007 Kamil Nowosad
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 *
21 * **********************************************************************************************************************
22 *
23 *
24 *
25 * This source code incorporates work covered by the following copyright and
26 * permission notice:
27 *
28 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
29 * Copyright (c) 2002-2007, Professor Benoit Macq
30 * Copyright (c) 2001-2003, David Janssens
31 * Copyright (c) 2002-2003, Yannick Verschueren
32 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
33 * Copyright (c) 2005, Herve Drolon, FreeImage Team
34 * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
35 * Copyright (c) 2020, Gautam Ramakrishnan <gautamramk@gmail.com>
36 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
48 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
51 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
52 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
53 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
54 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
55 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
57 * POSSIBILITY OF SUCH DAMAGE.
58 */
59
60
61 /**
62 * JPEG2000 image encoder
63 * @file
64 * @author Kamil Nowosad
65 */
66
80
81 #define NMSEDEC_BITS 7
82 #define NMSEDEC_FRACBITS (NMSEDEC_BITS-1)
83 #define WMSEDEC_SHIFT 13 ///< must be >= 13
84 #define LAMBDA_SCALE (100000000LL << (WMSEDEC_SHIFT - 13))
85
88
93
94 static const int dwt_norms[2][4][10] = {
// [dwt_type][band][rlevel] (multiplied by 10000)
95 {{10000, 19650, 41770, 84030, 169000, 338400, 676900, 1353000, 2706000, 5409000},
96 {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
97 {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
98 {20800, 38650, 83070, 171800, 347100, 695900, 1393000, 2786000, 5572000}},
99
100 {{10000, 15000, 27500, 53750, 106800, 213400, 426700, 853300, 1707000, 3413000},
101 {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
102 {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
103 { 7186, 9218, 15860, 30430, 60190, 120100, 240000, 479700, 959300}}
104 };
105
110
115
117 uint8_t cbps[4];
///< bits per sample in particular components
123
128
130
133
135 int layer_rates[100];
137
146
147
148 /* debug */
149 #if 0
150 #undef ifprintf
151 #undef printf
152
153 static void nspaces(FILE *fd, int n)
154 {
155 while(n--) putc(' ', fd);
156 }
157
159 {
163 }
164
166 {
167 int tileno, compno, reslevelno, bandno, precno;
168 fprintf(fd, "XSiz = %d, YSiz = %d, tile_width = %d, tile_height = %d\n"
169 "numXtiles = %d, numYtiles = %d, ncomponents = %d\n"
170 "tiles:\n",
171 s->width,
s->height,
s->tile_width,
s->tile_height,
172 s->numXtiles,
s->numYtiles,
s->ncomponents);
173 for (tileno = 0; tileno <
s->numXtiles *
s->numYtiles; tileno++){
175 nspaces(fd, 2);
176 fprintf(fd, "tile %d:\n", tileno);
177 for(compno = 0; compno <
s->ncomponents; compno++){
179 nspaces(fd, 4);
180 fprintf(fd, "component %d:\n", compno);
181 nspaces(fd, 4);
182 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d\n",
184 for(reslevelno = 0; reslevelno <
s->nreslevels; reslevelno++){
186 nspaces(fd, 6);
187 fprintf(fd, "reslevel %d:\n", reslevelno);
188 nspaces(fd, 6);
189 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d, nbands = %d\n",
190 reslevel->x0, reslevel->x1, reslevel->y0,
191 reslevel->y1, reslevel->
nbands);
192 for(bandno = 0; bandno < reslevel->
nbands; bandno++){
194 nspaces(fd, 8);
195 fprintf(fd, "band %d:\n", bandno);
196 nspaces(fd, 8);
197 fprintf(fd, "x0 = %d, x1 = %d, y0 = %d, y1 = %d,"
198 "codeblock_width = %d, codeblock_height = %d cblknx = %d cblkny = %d\n",
199 band->x0, band->x1,
200 band->y0, band->y1,
201 band->codeblock_width, band->codeblock_height,
202 band->cblknx, band->cblkny);
205 nspaces(fd, 10);
206 fprintf(fd, "prec %d:\n", precno);
207 nspaces(fd, 10);
208 fprintf(fd, "xi0 = %d, xi1 = %d, yi0 = %d, yi1 = %d\n",
209 prec->xi0, prec->xi1, prec->yi0, prec->yi1);
210 }
211 }
212 }
213 }
214 }
215 }
216 #endif
217
218 /* bitstream routines */
219
220 /** put n times val bit */
222 {
223 while (n-- > 0){
224 if (
s->bit_index == 8)
225 {
226 s->bit_index = *
s->buf == 0xff;
228 }
229 *
s->buf |=
val << (7 -
s->bit_index++);
230 }
231 }
232
233 /** put n least significant bits of a number num */
235 {
236 while(--n >= 0)
238 }
239
240 /** flush the bitstream */
242 {
246 }
247 }
248
249 /* tag tree routines */
250
251 /** code the value stored in node */
253 {
255 int sp = -1, curval = 0;
256
260 }
261
262 while (1) {
265 else {
267 }
268
269 if (node->
val >= threshold) {
271 curval = threshold;
272 } else {
278 }
279 }
280
283 break;
285 }
286 }
287
288 /** update the value in node */
290 {
293 break;
296 }
297 }
298
300 {
302
303 if (
s->buf_end -
s->buf < 40 + 3 *
s->ncomponents)
304 return -1;
305
307 bytestream_put_be16(&
s->buf, 38 + 3 *
s->ncomponents);
// Lsiz
308 bytestream_put_be16(&
s->buf, 0);
// Rsiz
309 bytestream_put_be32(&
s->buf,
s->width);
// width
310 bytestream_put_be32(&
s->buf,
s->height);
// height
311 bytestream_put_be32(&
s->buf, 0);
// X0Siz
312 bytestream_put_be32(&
s->buf, 0);
// Y0Siz
313
314 bytestream_put_be32(&
s->buf,
s->tile_width);
// XTSiz
315 bytestream_put_be32(&
s->buf,
s->tile_height);
// YTSiz
316 bytestream_put_be32(&
s->buf, 0);
// XT0Siz
317 bytestream_put_be32(&
s->buf, 0);
// YT0Siz
318 bytestream_put_be16(&
s->buf,
s->ncomponents);
// CSiz
319
320 for (
i = 0;
i <
s->ncomponents;
i++){
// Ssiz_i XRsiz_i, YRsiz_i
321 bytestream_put_byte(&
s->buf,
s->cbps[
i] - 1);
322 bytestream_put_byte(&
s->buf,
i?1<<
s->chroma_shift[0]:1);
323 bytestream_put_byte(&
s->buf,
i?1<<
s->chroma_shift[1]:1);
324 }
325 return 0;
326 }
327
329 {
331 uint8_t scod = 0;
332
333 if (
s->buf_end -
s->buf < 14)
334 return -1;
335
337 bytestream_put_be16(&
s->buf, 12);
// Lcod
342 bytestream_put_byte(&
s->buf, scod);
// Scod
343 // SGcod
344 bytestream_put_byte(&
s->buf,
s->prog);
// progression level
345 bytestream_put_be16(&
s->buf,
s->nlayers);
// num of layers
347 bytestream_put_byte(&
s->buf, 0);
// unspecified
348 }else{
349 bytestream_put_byte(&
s->buf, 0);
// unspecified
350 }
351 // SPcod
352 bytestream_put_byte(&
s->buf, codsty->
nreslevels - 1);
// num of decomp. levels
355 bytestream_put_byte(&
s->buf, 0);
// cblk style
357 return 0;
358 }
359
361 {
365
368 else // QSTY_SE
370
371 if (
s->buf_end -
s->buf <
size + 2)
372 return -1;
373
375 bytestream_put_be16(&
s->buf,
size);
// LQcd
379 bytestream_put_byte(&
s->buf, qntsty->
expn[
i] << 3);
380 else // QSTY_SE
382 bytestream_put_be16(&
s->buf, (qntsty->
expn[
i] << 11) | qntsty->
mant[
i]);
383 return 0;
384 }
385
387 {
389
391 return 0;
392
393 if (
s->buf_end -
s->buf <
size + 2)
394 return -1;
395
397 bytestream_put_be16(&
s->buf,
size);
398 bytestream_put_be16(&
s->buf, 1);
// General use (ISO/IEC 8859-15 (Latin) values)
399
401
402 return 0;
403 }
404
406 {
407 uint8_t *psotptr;
408
409 if (
s->buf_end -
s->buf < 12)
411
413 bytestream_put_be16(&
s->buf, 10);
// Lsot
414 bytestream_put_be16(&
s->buf, tileno);
// Isot
415
417 bytestream_put_be32(&
s->buf, 0);
// Psot (filled in later)
418
419 bytestream_put_byte(&
s->buf, 0);
// TPsot
420 bytestream_put_byte(&
s->buf, 1);
// TNsot
421 return psotptr;
422 }
423
425 {
427 int layno, compno;
428 for (
i = 0;
i <
s->numYtiles;
i++) {
429 for (j = 0; j <
s->numXtiles; j++) {
431 for (compno = 0; compno <
s->ncomponents; compno++) {
434 int scale = (compno?1 <<
s->chroma_shift[0]:1) * (compno?1 <<
s->chroma_shift[1]:1);
435 for (layno = 0; layno <
s->nlayers; layno++) {
436 if (
s->layer_rates[layno] > 0) {
439 } else {
441 }
442 }
443 }
444 }
445 }
446
447 }
448
449 /**
450 * compute the sizes of tiles, resolution levels, bands, etc.
451 * allocate memory for them
452 * divide the input image into tile-components
453 */
455 {
456 int tileno, tilex, tiley, compno;
459
462
466 for (tileno = 0, tiley = 0; tiley <
s->numYtiles; tiley++)
467 for (tilex = 0; tilex <
s->numXtiles; tilex++, tileno++){
469
473
477
478 for (compno = 0; compno <
s->ncomponents; compno++){
481
482 comp->coord[0][0] =
comp->coord_o[0][0] = tilex *
s->tile_width;
483 comp->coord[0][1] =
comp->coord_o[0][1] =
FFMIN((tilex+1)*
s->tile_width,
s->width);
484 comp->coord[1][0] =
comp->coord_o[1][0] = tiley *
s->tile_height;
485 comp->coord[1][1] =
comp->coord_o[1][1] =
FFMIN((tiley+1)*
s->tile_height,
s->height);
486 if (compno > 0)
487 for (
i = 0;
i < 2;
i++)
488 for (j = 0; j < 2; j++)
490
492 codsty,
493 qntsty,
495 compno?1<<
s->chroma_shift[0]:1,
496 compno?1<<
s->chroma_shift[1]:1,
498 )) < 0)
500 }
501 }
503 return 0;
504 }
505
506 #define COPY_FRAME(D, PIXEL) \
507 static void copy_frame_ ##D(Jpeg2000EncoderContext *s) \
508 { \
509 int tileno, compno, i, y, x; \
510 PIXEL *line; \
511 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){ \
512 Jpeg2000Tile *tile = s->tile + tileno; \
513 if (s->planar){ \
514 for (compno = 0; compno < s->ncomponents; compno++){ \
515 Jpeg2000Component *comp = tile->comp + compno; \
516 int *dst = comp->i_data; \
517 int cbps = s->cbps[compno]; \
518 line = (PIXEL*)s->picture->data[compno] \
519 + comp->coord[1][0] * (s->picture->linesize[compno] / sizeof(PIXEL)) \
520 + comp->coord[0][0]; \
521 for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){ \
522 PIXEL *ptr = line; \
523 for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++) \
524 *dst++ = *ptr++ - (1 << (cbps - 1)); \
525 line += s->picture->linesize[compno] / sizeof(PIXEL); \
526 } \
527 } \
528 } else{ \
529 line = (PIXEL*)s->picture->data[0] + tile->comp[0].coord[1][0] * (s->picture->linesize[0] / sizeof(PIXEL)) \
530 + tile->comp[0].coord[0][0] * s->ncomponents; \
531 \
532 i = 0; \
533 for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){ \
534 PIXEL *ptr = line; \
535 for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){ \
536 for (compno = 0; compno < s->ncomponents; compno++){ \
537 int cbps = s->cbps[compno]; \
538 tile->comp[compno].i_data[i] = *ptr++ - (1 << (cbps - 1)); \
539 } \
540 } \
541 line += s->picture->linesize[0] / sizeof(PIXEL); \
542 } \
543 } \
544 } \
545 }
546
549
551 {
552 int compno, reslevelno, bandno;
555
556 for (compno = 0; compno <
s->ncomponents; compno++){
557 int gbandno = 0;
558 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++){
560 nbands = reslevelno ? 3 : 1;
561 for (bandno = 0; bandno < nbands; bandno++, gbandno++){
562 int expn, mant = 0;
563
565 int bandpos = bandno + (reslevelno>0),
568 mant = (11 - log < 0 ? ss >> log - 11 :
ss << 11 - log) & 0x7ff;
569 expn =
s->cbps[compno] - log + 13;
570 } else
571 expn = ((bandno&2)>>1) + (reslevelno>0) +
s->cbps[compno];
572
573 qntsty->
expn[gbandno] = expn;
574 qntsty->
mant[gbandno] = mant;
575 }
576 }
577 }
578 }
579
581 {
584
588
591 (1 << 13) - (
a *
a << 11), 0);
593 << 1, 0);
594 }
596 }
597
598 /* tier-1 routines */
600 {
604 }
605
607 {
611 }
612
614 {
616 for (y0 = 0; y0 <
height; y0 += 4)
617 for (x = 0; x <
width; x++)
618 for (y = y0; y <
height && y < y0+4; y++){
621 bit =
t1->data[(y) *
t1->stride + x] &
mask ? 1 : 0;
624 int xorbit;
626 ff_mqc_encode(&
t1->mqc,
t1->mqc.cx_states + ctxno, (
t1->flags[(y+1) *
t1->stride + x+1] >> 15) ^ xorbit);
629 }
631 }
632 }
633 }
634
636 {
638 for (y0 = 0; y0 <
height; y0 += 4)
639 for (x = 0; x <
width; x++)
640 for (y = y0; y <
height && y < y0+4; y++)
646 }
647 }
648
650 {
652 for (y0 = 0; y0 <
height; y0 += 4)
653 for (x = 0; x <
width; x++){
659 {
660 // aggregation mode
661 int rlen;
662 for (rlen = 0; rlen < 4; rlen++)
663 if (
t1->data[(y0+rlen) *
t1->stride + x] &
mask)
664 break;
666 if (rlen == 4)
667 continue;
670 for (y = y0 + rlen; y < y0 + 4; y++){
673 if (y > y0 + rlen)
675 if (
t1->data[(y) *
t1->stride + x] &
mask){
// newly significant
676 int xorbit;
679 ff_mqc_encode(&
t1->mqc,
t1->mqc.cx_states + ctxno, (
t1->flags[(y+1) *
t1->stride + x+1] >> 15) ^ xorbit);
681 }
682 }
684 }
685 } else{
686 for (y = y0; y < y0 + 4 && y <
height; y++){
690 if (
t1->data[(y) *
t1->stride + x] &
mask){
// newly significant
691 int xorbit;
694 ff_mqc_encode(&
t1->mqc,
t1->mqc.cx_states + ctxno, (
t1->flags[(y+1) *
t1->stride + x+1] >> 15) ^ xorbit);
696 }
697 }
699 }
700 }
701 }
702 }
703
706 {
707 int pass_t = 2, passno, x, y,
max=0, nmsedec, bpno;
708 int64_t wmsedec = 0;
709
710 memset(
t1->flags, 0,
t1->stride * (
height + 2) *
sizeof(*
t1->flags));
711
712 for (y = 0; y <
height; y++){
713 for (x = 0; x <
width; x++){
714 if (
t1->data[(y) *
t1->stride + x] < 0){
716 t1->data[(y) *
t1->stride + x] = -
t1->data[(y) *
t1->stride + x];
717 }
719 }
720 }
721
724 bpno = 0;
725 } else{
728 }
729
732
733 for (passno = 0; bpno >= 0; passno++){
734 nmsedec=0;
735
736 switch(pass_t){
738 break;
740 break;
742 break;
743 }
744
747
748 wmsedec += (int64_t)nmsedec << (2*bpno);
750
751 if (++pass_t == 3){
752 pass_t = 0;
753 bpno--;
754 }
755 }
758
759 if (passno) {
762 }
763 }
764
765 /* tier-2 routines: */
766
768 {
769 if (n == 1)
771 else if (n == 2)
773 else if (n <= 5)
775 else if (n <= 36)
777 else
779 }
780
781
783 int precno, uint8_t *expn, int numgbits, int packetno,
784 int nlayers)
785 {
786 int bandno, empty = 1;
788 // init bitstream
791
794 bytestream_put_be16(&
s->buf, 4);
795 bytestream_put_be16(&
s->buf, packetno);
796 }
797 // header
798
799 if (!layno) {
800 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
815 for (
i = 0;
i < nlayers;
i++) {
818 break;
819 }
820 }
824 }
825 }
826 }
827 }
828
829 // is the packet empty?
830 for (bandno = 0; bandno < rlevel->
nbands; bandno++){
840 empty = 0;
841 break;
842 }
843 }
844 if (!empty)
845 break;
846 }
847 }
848
850 if (empty){
854 return 0;
855 }
856
857 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
862
865 continue;
866
869 int llen = 0, length;
871
872 if (
s->buf_end -
s->buf < 20)
// approximately
873 return -1;
874
875 // inclusion information
878 else {
880 }
881
883 continue;
884
885 // zerobits information
889 }
890
891 // number of passes
893
897 }
900 }
901
902 // length of code block
907 }
908 }
909 }
913 }
914
915 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
921 for (
xi = 0;
xi < cblknw;
xi++){
925 return -1;
930 }
931 }
932 }
933 }
934 }
935 return 0;
936 }
937
939 {
940 int compno, reslevelno, layno,
ret;
943 int packetno = 0;
944 int step_x, step_y;
945 int x, y;
946 int tile_coord[2][2];
947 int col = tileno %
s->numXtiles;
948 int row = tileno /
s->numXtiles;
949
950 tile_coord[0][0] = col *
s->tile_width;
951 tile_coord[0][1] =
FFMIN(tile_coord[0][0] +
s->tile_width,
s->width);
952 tile_coord[1][0] = row *
s->tile_height;
953 tile_coord[1][1] =
FFMIN(tile_coord[1][0] +
s->tile_height,
s->height);
954
956 // lay-rlevel-comp-pos progression
959 for (layno = 0; layno < nlayers; layno++) {
960 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++){
961 for (compno = 0; compno <
s->ncomponents; compno++){
962 int precno;
965 if ((
ret =
encode_packet(
s, reslevel, layno, precno, qntsty->
expn + (reslevelno ? 3*reslevelno-2 : 0),
966 qntsty->
nguardbits, packetno++, nlayers)) < 0)
968 }
969 }
970 }
971 }
972 break;
974 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++){
975 for (layno = 0; layno < nlayers; layno++) {
976 for (compno = 0; compno <
s->ncomponents; compno++){
977 int precno;
980 if ((
ret =
encode_packet(
s, reslevel, layno, precno, qntsty->
expn + (reslevelno ? 3*reslevelno-2 : 0),
981 qntsty->
nguardbits, packetno++, nlayers)) < 0)
983 }
984 }
985 }
986 }
987 break;
989 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++) {
990 int precno;
991 step_x = 30;
992 step_y = 30;
993 for (compno = 0; compno <
s->ncomponents; compno++) {
995 if (reslevelno < codsty->nreslevels) {
996 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
// ==> N_L - r
1000 }
1001 }
1002
1003 step_x = 1<<step_x;
1004 step_y = 1<<step_y;
1005 for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
1006 for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
1007 for (compno = 0; compno <
s->ncomponents; compno++) {
1009 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
// ==> N_L - r
1011 int log_subsampling[2] = { compno?
s->chroma_shift[0]:0, compno?
s->chroma_shift[1]:0};
1012 unsigned prcx, prcy;
1013 int trx0, try0;
1014
1017
1018 if (!(y % ((uint64_t)1 << (reslevel->
log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
1019 (y == tile_coord[1][0] && (try0 << reducedresno) % (1
U << (reducedresno + reslevel->
log2_prec_height)))))
1020 continue;
1021
1022 if (!(x % ((uint64_t)1 << (reslevel->
log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
1023 (x == tile_coord[0][0] && (trx0 << reducedresno) % (1
U << (reducedresno + reslevel->
log2_prec_width)))))
1024 continue;
1025
1026 // check if a precinct exists
1032
1036 continue;
1037 }
1038 for (layno = 0; layno < nlayers; layno++) {
1039 if ((
ret =
encode_packet(
s, reslevel, layno, precno, qntsty->
expn + (reslevelno ? 3*reslevelno-2 : 0),
1040 qntsty->
nguardbits, packetno++, nlayers)) < 0)
1042 }
1043 }
1044 }
1045 }
1046 }
1047 break;
1049 step_x = 32;
1050 step_y = 32;
1051 for (compno = 0; compno <
s->ncomponents; compno++) {
1053
1054 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++) {
1055 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
// ==> N_L - r
1059 }
1060 }
1061 if (step_x >= 31 || step_y >= 31){
1064 }
1065 step_x = 1<<step_x;
1066 step_y = 1<<step_y;
1067
1068 for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
1069 for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
1070 for (compno = 0; compno <
s->ncomponents; compno++) {
1072 int log_subsampling[2] = { compno?
s->chroma_shift[0]:0, compno?
s->chroma_shift[1]:0};
1073
1074 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++) {
1075 unsigned prcx, prcy;
1076 int precno;
1077 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
// ==> N_L - r
1079 int trx0, try0;
1080
1083
1084 if (!(y % ((uint64_t)1 << (reslevel->
log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
1085 (y == tile_coord[1][0] && (try0 << reducedresno) % (1
U << (reducedresno + reslevel->
log2_prec_height)))))
1086 continue;
1087
1088 if (!(x % ((uint64_t)1 << (reslevel->
log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
1089 (x == tile_coord[0][0] && (trx0 << reducedresno) % (1
U << (reducedresno + reslevel->
log2_prec_width)))))
1090 continue;
1091
1092 // check if a precinct exists
1097
1099
1103 continue;
1104 }
1105 for (layno = 0; layno < nlayers; layno++) {
1106 if ((
ret =
encode_packet(
s, reslevel, layno, precno, qntsty->
expn + (reslevelno ? 3*reslevelno-2 : 0),
1107 qntsty->
nguardbits, packetno++, nlayers)) < 0)
1109 }
1110 }
1111 }
1112 }
1113 }
1114 break;
1116 for (compno = 0; compno <
s->ncomponents; compno++) {
1118 int log_subsampling[2] = { compno?
s->chroma_shift[0]:0, compno?
s->chroma_shift[1]:0};
1119 step_x = 32;
1120 step_y = 32;
1121
1122 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++) {
1123 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
// ==> N_L - r
1127 }
1128 if (step_x >= 31 || step_y >= 31){
1131 }
1132 step_x = 1<<step_x;
1133 step_y = 1<<step_y;
1134
1135 for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
1136 for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
1137 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++) {
1138 unsigned prcx, prcy;
1139 int precno;
1140 int trx0, try0;
1141 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
// ==> N_L - r
1143
1146
1147 if (!(y % ((uint64_t)1 << (reslevel->
log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
1148 (y == tile_coord[1][0] && (try0 << reducedresno) % (1
U << (reducedresno + reslevel->
log2_prec_height)))))
1149 continue;
1150
1151 if (!(x % ((uint64_t)1 << (reslevel->
log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
1152 (x == tile_coord[0][0] && (trx0 << reducedresno) % (1
U << (reducedresno + reslevel->
log2_prec_width)))))
1153 continue;
1154
1155 // check if a precinct exists
1160
1162
1166 continue;
1167 }
1168 for (layno = 0; layno < nlayers; layno++) {
1169 if ((
ret =
encode_packet(
s, reslevel, layno, precno, qntsty->
expn + (reslevelno ? 3*reslevelno-2 : 0),
1170 qntsty->
nguardbits, packetno++, nlayers)) < 0)
1172 }
1173 }
1174 }
1175 }
1176 }
1177
1178 }
1179
1181 return 0;
1182 }
1183
1185 {
1186 int compno, resno, bandno, precno, cblkno;
1187 int passno;
1188
1189 for (compno = 0; compno <
s->ncomponents; compno++) {
1191
1192 for (resno = 0; resno <
s->codsty.nreslevels; resno++) {
1194
1196 for (bandno = 0; bandno < reslevel->
nbands ; bandno++){
1199
1203 int n;
1204
1205 if (layno == 0) {
1207 }
1208
1210
1211 if (thresh < 0) {
1213 } else {
1214 for (passno = cblk->
ninclpasses; passno < cblk->npasses; passno++) {
1216 double dd;
1218
1219 if (n == 0) {
1222 } else {
1225 }
1226
1227 if (!dr) {
1228 if (dd != 0.0) {
1229 n = passno + 1;
1230 }
1231 continue;
1232 }
1233
1234 if (thresh - (dd / dr) < DBL_EPSILON)
1235 n = passno + 1;
1236 }
1237 }
1240
1244 continue;
1245 }
1246
1251 } else {
1256 }
1257 if (final) {
1259 }
1260 }
1261 }
1262 }
1263 }
1264 }
1265 }
1266
1268 {
1269 int precno, compno, reslevelno, bandno, cblkno,
lev, passno, layno;
1271 double min = DBL_MAX;
1273 double thresh;
1274
1276
1277 for (compno = 0; compno <
s->ncomponents; compno++){
1279
1280 for (reslevelno = 0,
lev = codsty->
nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++,
lev--){
1282
1284 for (bandno = 0; bandno < reslevel->
nbands ; bandno++){
1287
1290 for (passno = 0; passno < cblk->
npasses; passno++) {
1292 int dr;
1293 double dd, drslope;
1294
1295 if (passno == 0) {
1298 } else {
1301 }
1302
1303 if (dr <= 0)
1304 continue;
1305
1306 drslope = dd / dr;
1309
1312 }
1313 }
1314 }
1315 }
1316 }
1317 }
1318
1319 for (layno = 0; layno <
s->nlayers; layno++) {
1322 double stable_thresh = 0.0;
1323 double good_thresh = 0.0;
1324 if (!
s->layer_rates[layno]) {
1325 good_thresh = -1.0;
1326 } else {
1327 for (
i = 0;
i < 128;
i++) {
1328 uint8_t *stream_pos =
s->buf;
1330 thresh = (lo + hi) / 2;
1333 memset(stream_pos, 0,
s->buf - stream_pos);
1335 lo = thresh;
1336 s->buf = stream_pos;
1337 continue;
1338 }
1339 hi = thresh;
1340 stable_thresh = thresh;
1341 s->buf = stream_pos;
1342 }
1343 }
1344 if (good_thresh >= 0.0)
1345 good_thresh = stable_thresh == 0.0 ? thresh : stable_thresh;
1347 }
1348 }
1349
1351 {
1352 int passno, res = 0;
1353 for (passno = 0; passno < cblk->
npasses; passno++){
1354 int dr;
1355 int64_t dd;
1356
1361
1362 if (((dd * dwt_norm) >>
WMSEDEC_SHIFT) * dwt_norm >= dr * lambda)
1363 res = passno+1;
1364 }
1365 return res;
1366 }
1367
1369 {
1370 int precno, compno, reslevelno, bandno, cblkno,
lev;
1372
1373 for (compno = 0; compno <
s->ncomponents; compno++){
1375
1376 for (reslevelno = 0,
lev = codsty->
nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++,
lev--){
1378
1380 for (bandno = 0; bandno < reslevel->
nbands ; bandno++){
1381 int bandpos = bandno + (reslevelno > 0);
1384
1387
1395 }
1396 }
1397 }
1398 }
1399 }
1400 }
1401
1403 {
1404 int compno, reslevelno, bandno,
ret;
1407 for (compno = 0; compno <
s->ncomponents; compno++){
1409
1411
1416
1417 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++){
1419
1420 for (bandno = 0; bandno < reslevel->
nbands ; bandno++){
1422 Jpeg2000Prec *prec = band->
prec;
// we support only 1 precinct per band ATM in the encoder
1423 int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
1424 yy0 = bandno == 0 ? 0 :
comp->reslevel[reslevelno-1].coord[1][1] -
comp->reslevel[reslevelno-1].coord[1][0];
1425 y0 = yy0;
1427 band->
coord[1][1]) - band->
coord[1][0] + yy0;
1428
1430 continue;
1431
1432 bandpos = bandno + (reslevelno > 0);
1433
1435 if (reslevelno == 0 || bandno == 1)
1436 xx0 = 0;
1437 else
1438 xx0 =
comp->reslevel[reslevelno-1].coord[0][1] -
comp->reslevel[reslevelno-1].coord[0][0];
1439 x0 = xx0;
1441 band->
coord[0][1]) - band->
coord[0][0] + xx0;
1442
1444 int y, x;
1446 for (y = yy0; y < yy1; y++){
1447 int *ptr =
t1.data + (y-yy0)*
t1.stride;
1448 for (x = xx0; x < xx1; x++){
1450 }
1451 }
1452 } else{
1453 for (y = yy0; y < yy1; y++){
1454 int *ptr =
t1.data + (y-yy0)*
t1.stride;
1455 for (x = xx0; x < xx1; x++){
1456 *ptr = (
comp->i_data[(
comp->coord[0][1] -
comp->coord[0][0]) * y + x]);
1458 ptr++;
1459 }
1460 }
1461 }
1469 bandpos, codsty->
nreslevels - reslevelno - 1);
1470 xx0 = xx1;
1472 }
1473 yy0 = yy1;
1475 }
1476 }
1477 }
1479 }
1480
1482 if (
s->compression_rate_enc)
1484 else
1486
1490 return 0;
1491 }
1492
1494 {
1495 int tileno, compno;
1497
1499 return;
1500 for (tileno = 0; tileno <
s->numXtiles *
s->numYtiles; tileno++){
1501 if (
s->tile[tileno].comp) {
1502 for (compno = 0; compno <
s->ncomponents; compno++){
1505 }
1507 }
1509 }
1511 }
1512
1514 {
1515 int tileno, compno;
1516 for (tileno = 0; tileno <
s->numXtiles *
s->numYtiles; tileno++){
1518 for (compno = 0; compno <
s->ncomponents; compno++)
1520 }
1521 }
1522
1524 {
1526 }
1527
1529 const AVFrame *pict,
int *got_packet)
1530 {
1533 uint8_t *chunkstart, *jp2cstart, *jp2hstart;
1534
1537
1538 // init:
1541
1543
1545
1548 else
1550
1552
1555
1556 bytestream_put_be32(&
s->buf, 0x0000000C);
1557 bytestream_put_be32(&
s->buf, 0x6A502020);
1558 bytestream_put_be32(&
s->buf, 0x0D0A870A);
1559
1560 chunkstart =
s->buf;
1561 bytestream_put_be32(&
s->buf, 0);
1564 bytestream_put_be32(&
s->buf, 0);
1567
1569 bytestream_put_be32(&
s->buf, 0);
1571
1572 chunkstart =
s->buf;
1573 bytestream_put_be32(&
s->buf, 0);
1575 bytestream_put_be32(&
s->buf, avctx->
height);
1576 bytestream_put_be32(&
s->buf, avctx->
width);
1577 bytestream_put_be16(&
s->buf,
s->ncomponents);
1578 bytestream_put_byte(&
s->buf,
s->cbps[0]);
1579 bytestream_put_byte(&
s->buf, 7);
1580 bytestream_put_byte(&
s->buf, 0);
1581 bytestream_put_byte(&
s->buf, 0);
1583
1584 chunkstart =
s->buf;
1585 bytestream_put_be32(&
s->buf, 0);
1587 bytestream_put_byte(&
s->buf, 1);
1588 bytestream_put_byte(&
s->buf, 0);
1589 bytestream_put_byte(&
s->buf, 0);
1591 bytestream_put_be32(&
s->buf, 16);
1592 }
else if (
s->ncomponents == 1) {
1593 bytestream_put_be32(&
s->buf, 17);
1594 } else {
1595 bytestream_put_be32(&
s->buf, 18);
1596 }
1600 uint8_t *palette = pict->
data[1];
1601 chunkstart =
s->buf;
1602 bytestream_put_be32(&
s->buf, 0);
1605 bytestream_put_byte(&
s->buf, 3);
// colour channels
1606 bytestream_put_be24(&
s->buf, 0x070707);
//colour depths
1608 bytestream_put_be24(&
s->buf, HAVE_BIGENDIAN ?
AV_RB24(palette + 1) :
AV_RL24(palette));
1609 palette += 4;
1610 }
1612 chunkstart =
s->buf;
1613 bytestream_put_be32(&
s->buf, 0);
1615 for (
i = 0;
i < 3;
i++) {
1616 bytestream_put_be16(&
s->buf, 0);
// component
1617 bytestream_put_byte(&
s->buf, 1);
// palette mapping
1618 bytestream_put_byte(&
s->buf,
i);
// index
1619 }
1621 }
1623
1625 bytestream_put_be32(&
s->buf, 0);
1627 }
1628
1629 if (
s->buf_end -
s->buf < 2)
1630 return -1;
1640
1641 for (tileno = 0; tileno <
s->numXtiles *
s->numYtiles; tileno++){
1642 uint8_t *psotptr;
1643 if (!(psotptr =
put_sot(
s, tileno)))
1644 return -1;
1645 if (
s->buf_end -
s->buf < 2)
1646 return -1;
1650 bytestream_put_be32(&psotptr,
s->buf - psotptr + 6);
1651 }
1652 if (
s->buf_end -
s->buf < 2)
1653 return -1;
1655
1658
1661 *got_packet = 1;
1662
1663 return 0;
1664 }
1665
1667 {
1669 char *token;
1670 char *saveptr =
NULL;
1671 int rate;
1672 int nlayers = 0;
1675 s->layer_rates[0] = 0;
1676 s->compression_rate_enc = 0;
1677 return 0;
1678 }
1679
1681 if (token && (rate = strtol(token,
NULL, 10))) {
1682 s->layer_rates[0] = rate <= 1 ? 0:rate;
1683 nlayers++;
1684 } else {
1686 }
1687
1688 while (1) {
1690 if (!token)
1691 break;
1692 if (rate = strtol(token,
NULL, 10)) {
1693 if (nlayers >= 100) {
1695 }
1696 s->layer_rates[nlayers] = rate <= 1 ? 0:rate;
1697 nlayers++;
1698 } else {
1700 }
1701 }
1702
1703 for (
i = 1;
i < nlayers;
i++) {
1704 if (
s->layer_rates[
i] >=
s->layer_rates[
i-1]) {
1706 }
1707 }
1708 s->nlayers = nlayers;
1709 s->compression_rate_enc = 1;
1710 return 0;
1711 }
1712
1714 {
1720
1726 s->layer_rates[0] = 0;
1727 s->compression_rate_enc = 0;
1728 }
1729
1734 }
1735
1736 // defaults:
1737 // TODO: implement setting non-standard precinct size
1746
1748
1749 if ((
s->tile_width & (
s->tile_width -1)) ||
1750 (
s->tile_height & (
s->tile_height-1))) {
1752 }
1753
1756 else
1758
1761
1762 for (
i = 0;
i < 3;
i++) {
1765 else
1767 }
1768
1773 } else{ // planar YUV
1777 s->chroma_shift,
s->chroma_shift + 1);
1780 }
1781
1783
1787
1789
1790 return 0;
1791 }
1792
1794 {
1796
1798 return 0;
1799 }
1800
1801 // taken from the libopenjpeg wraper so it matches
1802
1803 #define OFFSET(x) offsetof(Jpeg2000EncoderContext, x)
1804 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1824 };
1825
1831 };
1832
1834 .
p.
name =
"jpeg2000",
1849 },
1852 };