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
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
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
60
64
66 uint8_t cbps[4];
///< bits per sample in particular components
72
77
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
98 {
99 int i;
100 for (i = 0; i < comp->y1 - comp->y0; i++)
101 ff_jpeg2000_printv(comp->
i_data + i * (comp->x1 - comp->x0), comp->x1 - comp->x0);
102 }
103
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",
114 nspaces(fd, 2);
115 fprintf(fd, "tile %d:\n", tileno);
116 for(compno = 0; compno < s->
ncomponents; 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++){
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++){
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);
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 */
161 {
162 while (n-- > 0){
164 {
167 }
169 }
170 }
171
172 /** put n least significant bits of a number num */
174 {
175 while(--n >= 0)
177 }
178
179 /** flush the bitstream */
181 {
185 }
186 }
187
188 /* tag tree routines */
189
190 /** code the value stored in node */
192 {
194 int sp = 1, curval = 0;
195 stack[0] = node;
196
198 while(node){
201 break;
202 }
204 stack[sp++] = node;
206 }
207 while(--sp >= 0){
208 if (stack[sp]->
val >= threshold){
210 break;
211 }
214 curval = stack[
sp]->
val;
215 }
216 }
217
218 /** update the value in node */
220 {
221 int lev = 0;
224 break;
227 lev++;
228 }
229 }
230
232 {
233 int i;
234
236 return -1;
237
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
248 bytestream_put_be32(&s->
buf, 0);
// XT0Siz
249 bytestream_put_be32(&s->
buf, 0);
// YT0Siz
251
252 for (i = 0; i < s->
ncomponents; i++){
// Ssiz_i XRsiz_i, YRsiz_i
253 bytestream_put_byte(&s->
buf, 7);
256 }
257 return 0;
258 }
259
261 {
263
265 return -1;
266
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
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
282 bytestream_put_byte(&s->
buf, 0);
// cblk style
284 return 0;
285 }
286
288 {
292
295 else // QSTY_SE
297
299 return -1;
300
302 bytestream_put_be16(&s->
buf, size);
// LQcd
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
314 {
316
319
321 bytestream_put_be16(&s->
buf, 10);
// Lsot
322 bytestream_put_be16(&s->
buf, tileno);
// Isot
323
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;
342
345
349 for (tileno = 0, tiley = 0; tiley < s->
numYtiles; tiley++)
350 for (tilex = 0; tilex < s->
numXtiles; tilex++, tileno++){
352
356 for (compno = 0; compno < s->
ncomponents; compno++){
359
364 if (compno > 0)
365 for (i = 0; i < 2; i++)
366 for (j = 0; j < 2; j++)
368
370 codsty,
371 qntsty,
376 ))
378 }
379 }
380 return 0;
381 }
382
384 {
385 int tileno, compno, i,
y, x;
390 for (compno = 0; compno < s->
ncomponents; compno++){
396 for (y = comp->
coord[1][0]; y < comp->coord[1][1]; y++){
398 for (x = comp->
coord[0][0]; x < comp->coord[0][1]; x++)
399 *dst++ = *ptr++ - (1 << 7);
401 }
402 }
403 } else{
406
407 i = 0;
408 for (y = tile->
comp[0].
coord[1][0]; y < tile->comp[0].
coord[1][1]; y++){
410 for (x = tile->
comp[0].
coord[0][0]; x < tile->comp[0].
coord[0][1]; x++, i++){
411 for (compno = 0; compno < s->
ncomponents; compno++){
412 tile->
comp[compno].
i_data[i] = *ptr++ - (1 << 7);
413 }
414 }
416 }
417 }
418 }
419 }
420
422 {
423 int compno, reslevelno, bandno;
426
427 for (compno = 0; compno < s->
ncomponents; compno++){
428 int gbandno = 0;
429 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++){
430 int nbands, lev = codsty->
nreslevels - reslevelno - 1;
431 nbands = reslevelno ? 3 : 1;
432 for (bandno = 0; bandno < nbands; bandno++, gbandno++){
433 int expn, mant;
434
436 int bandpos = bandno + (reslevelno>0),
437 ss = 81920000 /
dwt_norms[0][bandpos][lev],
439 mant = (11 - log < 0 ? ss >> log - 11 : ss << 11 - log) & 0x7ff;
440 expn = s->
cbps[compno] - log + 13;
441 } else
442 expn = ((bandno&2)>>1) + (reslevelno>0) + s->
cbps[compno];
443
444 qntsty->
expn[gbandno] = expn;
445 qntsty->
mant[gbandno] = mant;
446 }
447 }
448 }
449 }
450
452 {
455
459
464 << 1, 0);
465 }
466 }
467
468 /* tier-1 routines */
470 {
474 }
475
477 {
481 }
482
484 {
486 for (y0 = 0; y0 <
height; y0 += 4)
487 for (x = 0; x <
width; x++)
488 for (y = y0; y < height && y < y0+4; y++){
491 bit = t1->
data[
y][x] & mask ? 1 : 0;
493 if (bit){
494 int xorbit;
499 }
501 }
502 }
503 }
504
506 {
508 for (y0 = 0; y0 <
height; y0 += 4)
509 for (x = 0; x <
width; x++)
510 for (y = y0; y < height && y < y0+4; y++)
516 }
517 }
518
520 {
522 for (y0 = 0; y0 <
height; y0 += 4)
523 for (x = 0; x <
width; x++){
524 if (y0 + 3 < height && !(
529 {
530 // aggregation mode
531 int rlen;
532 for (rlen = 0; rlen < 4; rlen++)
533 if (t1->
data[y0+rlen][x] & mask)
534 break;
536 if (rlen == 4)
537 continue;
540 for (y = y0 + rlen; y < y0 + 4; y++){
543 if (y > y0 + rlen)
545 if (t1->
data[y][x] & mask){
// newly significant
546 int xorbit;
551 }
552 }
554 }
555 } else{
556 for (y = y0; y < y0 + 4 && y <
height; y++){
560 if (t1->
data[y][x] & mask){
// newly significant
561 int xorbit;
566 }
567 }
569 }
570 }
571 }
572 }
573
576 {
577 int pass_t = 2, passno, x,
y, max=0, nmsedec, bpno;
578 int64_t wmsedec = 0;
579
580 for (y = 0; y < height+2; y++)
581 memset(t1->
flags[y], 0, (width+2)*
sizeof(
int));
582
583 for (y = 0; y <
height; y++){
584 for (x = 0; x <
width; x++){
585 if (t1->
data[y][x] < 0){
588 }
590 }
591 }
592
593 if (max == 0){
595 bpno = 0;
596 } else{
599 }
600
602
603 for (passno = 0; bpno >= 0; passno++){
604 nmsedec=0;
605
606 switch(pass_t){
607 case 0:
encode_sigpass(t1, width, height, bandpos, &nmsedec, bpno);
608 break;
610 break;
611 case 2:
encode_clnpass(t1, width, height, bandpos, &nmsedec, bpno);
612 break;
613 }
614
616 wmsedec += (int64_t)nmsedec << (2*bpno);
618
619 if (++pass_t == 3){
620 pass_t = 0;
621 bpno--;
622 }
623 }
626
627 // TODO: optional flush on each pass
629 }
630
631 /* tier-2 routines: */
632
634 {
635 if (n == 1)
637 else if (n == 2)
639 else if (n <= 5)
641 else if (n <= 36)
643 else
644 put_num(s, 0xff80 | (n-37), 16);
645 }
646
647
650 {
651 int bandno, empty = 1;
652
653 // init bitstream
656
657 // header
658
659 // is the packet empty?
660 for (bandno = 0; bandno < rlevel->
nbands; bandno++){
663 empty = 0;
664 break;
665 }
666 }
667
669 if (empty){
671 return 0;
672 }
673
674 for (bandno = 0; bandno < rlevel->
nbands; bandno++){
677 int yi, xi, pos;
679
682 continue;
683
685 for (xi = 0; xi < cblknw; xi++, pos++){
690 }
691 }
692
694 for (xi = 0; xi < cblknw; xi++, pos++){
695 int pad = 0, llen,
length;
697
698 if (s->
buf_end - s->
buf < 20)
// approximately
699 return -1;
700
701 // inclusion information
704 continue;
705 // zerobits information
707 // number of passes
709
712 if (llen < 0){
713 pad = -llen;
714 llen = 0;
715 }
716 // length of code block
720 }
721 }
722 }
724 for (bandno = 0; bandno < rlevel->
nbands; bandno++){
729 int xi;
730 for (xi = 0; xi < cblknw; xi++){
734 return -1;
736 }
737 }
738 }
739 }
740 return 0;
741 }
742
744 {
745 int compno, reslevelno,
ret;
748
750 // lay-rlevel-comp-pos progression
751 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++){
752 for (compno = 0; compno < s->
ncomponents; compno++){
753 int precno;
756 if (ret =
encode_packet(s, reslevel, precno, qntsty->
expn + (reslevelno ? 3*reslevelno-2 : 0),
759 }
760 }
761 }
763 return 0;
764 }
765
767 {
768 int passno, res = 0;
769 for (passno = 0; passno < cblk->
npasses; passno++){
770 int dr;
771 int64_t dd;
772
777
778 if (((dd * dwt_norm) >>
WMSEDEC_SHIFT) * dwt_norm >= dr * lambda)
779 res = passno+1;
780 }
781 return res;
782 }
783
785 {
786 int precno, compno, reslevelno, bandno, cblkno, lev;
788
789 for (compno = 0; compno < s->
ncomponents; compno++){
791
792 for (reslevelno = 0, lev = codsty->
nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++, lev--){
794
796 for (bandno = 0; bandno < reslevel->
nbands ; bandno++){
797 int bandpos = bandno + (reslevelno > 0);
800
803
806 }
807 }
808 }
809 }
810 }
811 }
812
814 {
815 int compno, reslevelno, bandno,
ret;
818 for (compno = 0; compno < s->
ncomponents; compno++){
820
825
826 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++){
828
829 for (bandno = 0; bandno < reslevel->
nbands ; bandno++){
831 Jpeg2000Prec *prec = band->
prec;
// we support only 1 precinct per band ATM in the encoder
832 int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
834 y0 = yy0;
837
839 continue;
840
841 bandpos = bandno + (reslevelno > 0);
842
844 if (reslevelno == 0 || bandno == 1)
845 xx0 = 0;
846 else
848 x0 = xx0;
851
855 for (y = yy0; y < yy1; y++){
856 int *ptr = t1.
data[y-yy0];
857 for (x = xx0; x < xx1; x++){
859 }
860 }
861 } else{
862 for (y = yy0; y < yy1; y++){
863 int *ptr = t1.
data[y-yy0];
864 for (x = xx0; x < xx1; x++){
867 ptr++;
868 }
869 }
870 }
872 bandpos, codsty->
nreslevels - reslevelno - 1);
873 xx0 = xx1;
875 }
876 yy0 = yy1;
878 }
879 }
880 }
882 }
883
889 return 0;
890 }
891
893 {
894 int tileno, compno;
896
898 for (compno = 0; compno < s->
ncomponents; compno++){
901 }
903 }
905 }
906
908 {
909 int tileno, compno;
912 for (compno = 0; compno < s->
ncomponents; compno++)
914 }
915 }
916
918 const AVFrame *pict,
int *got_packet)
919 {
922
925
926 // init:
929
931
933
936
938 return -1;
946
949 if (!(psotptr =
put_sot(s, tileno)))
950 return -1;
952 return -1;
956 bytestream_put_be32(&psotptr, s->
buf - psotptr + 6);
957 }
959 return -1;
961
965 *got_packet = 1;
966
967 return 0;
968 }
969
971 {
976
979
980 // defaults:
981 // TODO: implement setting non-standard precinct size
989
991
994
997 else
999
1002
1003 for (i = 0; i < 3; i++)
1005
1010 } else{ // planar YUV
1015 }
1016
1020
1024
1026
1027 return 0;
1028 }
1029
1031 {
1033
1035 return 0;
1036 }
1037
1050 /* AV_PIX_FMT_YUV420P,
1051 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
1052 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,*/
1054 }
1055 };