84#define NMSEDEC_FRACBITS (NMSEDEC_BITS-1)
85#define WMSEDEC_SHIFT 13
86#define LAMBDA_SCALE (100000000LL << (WMSEDEC_SHIFT - 13))
97 {{10000, 19650, 41770, 84030, 169000, 338400, 676900, 1353000, 2706000, 5409000},
98 {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
99 {20220, 39890, 83550, 170400, 342700, 686300, 1373000, 2746000, 5490000},
100 {20800, 38650, 83070, 171800, 347100, 695900, 1393000, 2786000, 5572000}},
102 {{10000, 15000, 27500, 53750, 106800, 213400, 426700, 853300, 1707000, 3413000},
103 {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
104 {10380, 15920, 29190, 57030, 113300, 226400, 452500, 904800, 1809000},
105 { 7186, 9218, 15860, 30430, 60190, 120100, 240000, 479700, 959300}}
157 if (
s->bit_index == 8)
159 s->bit_index = *
s->buf == 0xff;
162 *
s->buf |=
val << (7 -
s->bit_index++);
188 int sp = -1, curval = 0;
202 if (node->
val >= threshold) {
236 if (
s->buf_end -
s->buf < 40 + 3 *
s->ncomponents)
240 bytestream_put_be16(&
s->buf, 38 + 3 *
s->ncomponents);
241 bytestream_put_be16(&
s->buf, 0);
242 bytestream_put_be32(&
s->buf,
s->width);
243 bytestream_put_be32(&
s->buf,
s->height);
244 bytestream_put_be32(&
s->buf, 0);
245 bytestream_put_be32(&
s->buf, 0);
247 bytestream_put_be32(&
s->buf,
s->tile_width);
248 bytestream_put_be32(&
s->buf,
s->tile_height);
249 bytestream_put_be32(&
s->buf, 0);
250 bytestream_put_be32(&
s->buf, 0);
251 bytestream_put_be16(&
s->buf,
s->ncomponents);
253 for (
i = 0;
i <
s->ncomponents;
i++){
254 bytestream_put_byte(&
s->buf,
s->cbps[
i] - 1);
255 bytestream_put_byte(&
s->buf, (
i+1&2)?1<<
s->chroma_shift[0]:1);
256 bytestream_put_byte(&
s->buf, (
i+1&2)?1<<
s->chroma_shift[1]:1);
266 if (
s->buf_end -
s->buf < 14)
270 bytestream_put_be16(&
s->buf, 12);
275 bytestream_put_byte(&
s->buf, scod);
277 bytestream_put_byte(&
s->buf,
s->prog);
278 bytestream_put_be16(&
s->buf,
s->nlayers);
280 bytestream_put_byte(&
s->buf, 0);
282 bytestream_put_byte(&
s->buf, 0);
285 bytestream_put_byte(&
s->buf, codsty->
nreslevels - 1);
288 bytestream_put_byte(&
s->buf, 0);
304 if (
s->buf_end -
s->buf <
size + 2)
308 bytestream_put_be16(&
s->buf,
size);
312 bytestream_put_byte(&
s->buf, qntsty->
expn[
i] << 3);
315 bytestream_put_be16(&
s->buf, (qntsty->
expn[
i] << 11) | qntsty->
mant[
i]);
326 if (
s->buf_end -
s->buf <
size + 2)
330 bytestream_put_be16(&
s->buf,
size);
331 bytestream_put_be16(&
s->buf, 1);
342 if (
s->buf_end -
s->buf < 12)
346 bytestream_put_be16(&
s->buf, 10);
347 bytestream_put_be16(&
s->buf, tileno);
350 bytestream_put_be32(&
s->buf, 0);
352 bytestream_put_byte(&
s->buf, 0);
353 bytestream_put_byte(&
s->buf, 1);
361 for (
i = 0;
i <
s->numYtiles;
i++) {
362 for (j = 0; j <
s->numXtiles; j++) {
364 for (compno = 0; compno <
s->ncomponents; compno++) {
365 int tilew =
tile->comp[compno].coord[0][1] -
tile->comp[compno].coord[0][0];
366 int tileh =
tile->comp[compno].coord[1][1] -
tile->comp[compno].coord[1][0];
367 int scale = ((compno+1&2)?1 <<
s->chroma_shift[0]:1) * ((compno+1&2)?1 <<
s->chroma_shift[1]:1);
368 for (layno = 0; layno <
s->nlayers; layno++) {
369 if (
s->layer_rates[layno] > 0) {
370 tile->layer_rates[layno] += (
double)(tilew * tileh) *
s->ncomponents *
s->cbps[compno] /
373 tile->layer_rates[layno] = 0.0;
389 int tileno, tilex, tiley, compno;
399 for (tileno = 0, tiley = 0; tiley <
s->numYtiles; tiley++)
400 for (tilex = 0; tilex <
s->numXtiles; tilex++, tileno++){
408 if (!
tile->layer_rates)
411 for (compno = 0; compno <
s->ncomponents; compno++){
415 comp->coord[0][0] =
comp->coord_o[0][0] = tilex *
s->tile_width;
416 comp->coord[0][1] =
comp->coord_o[0][1] =
FFMIN((tilex+1)*
s->tile_width,
s->width);
417 comp->coord[1][0] =
comp->coord_o[1][0] = tiley *
s->tile_height;
418 comp->coord[1][1] =
comp->coord_o[1][1] =
FFMIN((tiley+1)*
s->tile_height,
s->height);
420 for (
i = 0;
i < 2;
i++)
421 for (j = 0; j < 2; j++)
428 (compno+1&2)?1<<
s->chroma_shift[0]:1,
429 (compno+1&2)?1<<
s->chroma_shift[1]:1,
439#define COPY_FRAME(D, PIXEL) \
440 static void copy_frame_ ##D(Jpeg2000EncoderContext *s) \
442 int tileno, compno, i, y, x; \
444 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){ \
445 Jpeg2000Tile *tile = s->tile + tileno; \
447 for (compno = 0; compno < s->ncomponents; compno++){ \
448 int icompno = s->comp_remap[compno]; \
449 Jpeg2000Component *comp = tile->comp + compno; \
450 int *dst = comp->i_data; \
451 int cbps = s->cbps[compno]; \
452 line = (const PIXEL*)s->picture->data[icompno] \
453 + comp->coord[1][0] * (s->picture->linesize[icompno] / sizeof(PIXEL)) \
454 + comp->coord[0][0]; \
455 for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){ \
456 const PIXEL *ptr = line; \
457 for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++) \
458 *dst++ = *ptr++ - (1 << (cbps - 1)); \
459 line += s->picture->linesize[icompno] / sizeof(PIXEL); \
463 line = (const PIXEL*)(s->picture->data[0] + tile->comp[0].coord[1][0] * s->picture->linesize[0]) \
464 + tile->comp[0].coord[0][0] * s->ncomponents; \
467 for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; y++){ \
468 const PIXEL *ptr = line; \
469 for (x = tile->comp[0].coord[0][0]; x < tile->comp[0].coord[0][1]; x++, i++){ \
470 for (compno = 0; compno < s->ncomponents; compno++){ \
471 int cbps = s->cbps[compno]; \
472 tile->comp[compno].i_data[i] = *ptr++ - (1 << (cbps - 1)); \
475 line += s->picture->linesize[0] / sizeof(PIXEL); \
486 int compno, reslevelno, bandno;
490 for (compno = 0; compno <
s->ncomponents; compno++){
492 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++){
494 nbands = reslevelno ? 3 : 1;
495 for (bandno = 0; bandno < nbands; bandno++, gbandno++){
499 int bandpos = bandno + (reslevelno>0),
502 mant = (11 - log < 0 ? ss >> log - 11 :
ss << 11 - log) & 0x7ff;
503 expn =
s->cbps[compno] - log + 13;
505 expn = ((bandno&2)>>1) + (reslevelno>0) +
s->cbps[compno];
507 qntsty->
expn[gbandno] = expn;
508 qntsty->
mant[gbandno] = mant;
525 (1 << 13) - (
a *
a << 11), 0);
550 for (y0 = 0; y0 <
height; y0 += 4)
551 for (x = 0; x <
width; x++)
552 for (y = y0; y <
height && y < y0+4; y++){
572 for (y0 = 0; y0 <
height; y0 += 4)
573 for (x = 0; x <
width; x++)
574 for (y = y0; y <
height && y < y0+4; y++)
586 for (y0 = 0; y0 <
height; y0 += 4)
587 for (x = 0; x <
width; x++){
596 for (rlen = 0; rlen < 4; rlen++)
604 for (y = y0 + rlen; y < y0 + 4; y++){
620 for (y = y0; y < y0 + 4 && y <
height; y++){
641 int pass_t = 2, passno, x, y,
max=0, nmsedec, bpno;
646 for (y = 0; y <
height; y++){
647 for (x = 0; x <
width; x++){
666 for (passno = 0; bpno >= 0; passno++){
681 wmsedec += (
int64_t)nmsedec << (2*bpno);
716 int precno,
const uint8_t *expn,
int numgbits,
int packetno,
719 int bandno, empty = 1;
727 bytestream_put_be16(&
s->buf, 4);
728 bytestream_put_be16(&
s->buf, packetno);
733 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
748 for (
i = 0;
i < nlayers;
i++) {
763 for (bandno = 0; bandno < rlevel->
nbands; bandno++){
790 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
802 int llen = 0, length;
805 if (
s->buf_end -
s->buf < 20)
848 for (bandno = 0; bandno < rlevel->
nbands; bandno++) {
854 for (
xi = 0;
xi < cblknw;
xi++){
873 int compno, reslevelno, layno, ret;
879 int tile_coord[2][2];
880 int col = tileno %
s->numXtiles;
881 int row = tileno /
s->numXtiles;
883 tile_coord[0][0] = col *
s->tile_width;
884 tile_coord[0][1] =
FFMIN(tile_coord[0][0] +
s->tile_width,
s->width);
885 tile_coord[1][0] = row *
s->tile_height;
886 tile_coord[1][1] =
FFMIN(tile_coord[1][0] +
s->tile_height,
s->height);
892 for (layno = 0; layno < nlayers; layno++) {
893 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++){
894 for (compno = 0; compno <
s->ncomponents; compno++){
898 if ((ret =
encode_packet(
s, reslevel, layno, precno, qntsty->
expn + (reslevelno ? 3*reslevelno-2 : 0),
899 qntsty->
nguardbits, packetno++, nlayers)) < 0)
907 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++){
908 for (layno = 0; layno < nlayers; layno++) {
909 for (compno = 0; compno <
s->ncomponents; compno++){
913 if ((ret =
encode_packet(
s, reslevel, layno, precno, qntsty->
expn + (reslevelno ? 3*reslevelno-2 : 0),
914 qntsty->
nguardbits, packetno++, nlayers)) < 0)
922 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++) {
926 for (compno = 0; compno <
s->ncomponents; compno++) {
928 if (reslevelno < codsty->nreslevels) {
929 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
938 for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
939 for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
940 for (compno = 0; compno <
s->ncomponents; compno++) {
942 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
944 int log_subsampling[2] = { (compno+1&2)?
s->chroma_shift[0]:0, (compno+1&2)?
s->chroma_shift[1]:0};
951 if (!(y % ((uint64_t)1 << (reslevel->
log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
952 (y == tile_coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + reslevel->
log2_prec_height)))))
955 if (!(x % ((uint64_t)1 << (reslevel->
log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
956 (x == tile_coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + reslevel->
log2_prec_width)))))
971 for (layno = 0; layno < nlayers; layno++) {
972 if ((ret =
encode_packet(
s, reslevel, layno, precno, qntsty->
expn + (reslevelno ? 3*reslevelno-2 : 0),
973 qntsty->
nguardbits, packetno++, nlayers)) < 0)
984 for (compno = 0; compno <
s->ncomponents; compno++) {
987 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++) {
988 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
994 if (step_x >= 31 || step_y >= 31){
1001 for (y = tile_coord[1][0]; y < tile_coord[1][1]; y = (y/step_y + 1)*step_y) {
1002 for (x = tile_coord[0][0]; x < tile_coord[0][1]; x = (x/step_x + 1)*step_x) {
1003 for (compno = 0; compno <
s->ncomponents; compno++) {
1005 int log_subsampling[2] = { (compno+1&2)?
s->chroma_shift[0]:0, (compno+1&2)?
s->chroma_shift[1]:0};
1007 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++) {
1008 unsigned prcx, prcy;
1010 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1017 if (!(y % ((uint64_t)1 << (reslevel->
log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
1018 (y == tile_coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + reslevel->
log2_prec_height)))))
1021 if (!(x % ((uint64_t)1 << (reslevel->
log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
1022 (x == tile_coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + reslevel->
log2_prec_width)))))
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)
1049 for (compno = 0; compno <
s->ncomponents; compno++) {
1051 int log_subsampling[2] = { (compno+1&2)?
s->chroma_shift[0]:0, (compno+1&2)?
s->chroma_shift[1]:0};
1055 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++) {
1056 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1061 if (step_x >= 31 || step_y >= 31){
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 (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++) {
1071 unsigned prcx, prcy;
1074 uint8_t reducedresno = codsty->
nreslevels - 1 -reslevelno;
1080 if (!(y % ((uint64_t)1 << (reslevel->
log2_prec_height + reducedresno + log_subsampling[1])) == 0 ||
1081 (y == tile_coord[1][0] && (try0 << reducedresno) % (1U << (reducedresno + reslevel->
log2_prec_height)))))
1084 if (!(x % ((uint64_t)1 << (reslevel->
log2_prec_width + reducedresno + log_subsampling[0])) == 0 ||
1085 (x == tile_coord[0][0] && (trx0 << reducedresno) % (1U << (reducedresno + reslevel->
log2_prec_width)))))
1101 for (layno = 0; layno < nlayers; layno++) {
1102 if ((ret =
encode_packet(
s, reslevel, layno, precno, qntsty->
expn + (reslevelno ? 3*reslevelno-2 : 0),
1103 qntsty->
nguardbits, packetno++, nlayers)) < 0)
1119 int compno, resno, bandno, precno, cblkno;
1122 for (compno = 0; compno <
s->ncomponents; compno++) {
1125 for (resno = 0; resno <
s->codsty.nreslevels; resno++) {
1129 for (bandno = 0; bandno < reslevel->
nbands ; bandno++){
1147 for (passno = cblk->
ninclpasses; passno < cblk->npasses; passno++) {
1167 if (thresh - (dd / dr) < DBL_EPSILON)
1202 int precno, compno, reslevelno, bandno, cblkno, passno, layno;
1204 double min = DBL_MAX;
1210 for (compno = 0; compno <
s->ncomponents; compno++){
1213 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++){
1217 for (bandno = 0; bandno < reslevel->
nbands ; bandno++){
1223 for (passno = 0; passno < cblk->
npasses; passno++) {
1252 for (layno = 0; layno <
s->nlayers; layno++) {
1255 double stable_thresh = 0.0;
1256 double good_thresh = 0.0;
1257 if (!
s->layer_rates[layno]) {
1260 for (
i = 0;
i < 128;
i++) {
1261 uint8_t *stream_pos =
s->buf;
1263 thresh = (lo + hi) / 2;
1266 memset(stream_pos, 0,
s->buf - stream_pos);
1267 if ((
s->buf - stream_pos >
ceil(
tile->layer_rates[layno])) || ret < 0) {
1269 s->buf = stream_pos;
1273 stable_thresh = thresh;
1274 s->buf = stream_pos;
1277 if (good_thresh >= 0.0)
1278 good_thresh = stable_thresh == 0.0 ? thresh : stable_thresh;
1285 int passno, res = 0;
1286 for (passno = 0; passno < cblk->
npasses; passno++){
1295 if (dd >= dr * lambda)
1303 int precno, compno, reslevelno, bandno, cblkno,
lev;
1306 for (compno = 0; compno <
s->ncomponents; compno++){
1309 for (reslevelno = 0,
lev = codsty->
nreslevels-1; reslevelno < codsty->nreslevels; reslevelno++,
lev--){
1313 for (bandno = 0; bandno < reslevel->
nbands ; bandno++){
1314 int bandpos = bandno + (reslevelno > 0);
1338 int compno, reslevelno, bandno, ret;
1341 for (compno = 0; compno <
s->ncomponents; compno++){
1351 for (reslevelno = 0; reslevelno < codsty->
nreslevels; reslevelno++){
1354 for (bandno = 0; bandno < reslevel->
nbands ; bandno++){
1357 int cblkx, cblky, cblkno=0, xx0, x0, xx1, y0, yy0, yy1, bandpos;
1358 yy0 = bandno == 0 ? 0 :
comp->reslevel[reslevelno-1].coord[1][1] -
comp->reslevel[reslevelno-1].coord[1][0];
1361 band->
coord[1][1]) - band->
coord[1][0] + yy0;
1366 bandpos = bandno + (reslevelno > 0);
1369 if (reslevelno == 0 || bandno == 1)
1372 xx0 =
comp->reslevel[reslevelno-1].coord[0][1] -
comp->reslevel[reslevelno-1].coord[0][0];
1375 band->
coord[0][1]) - band->
coord[0][0] + xx0;
1380 for (y = yy0; y < yy1; y++){
1382 for (x = xx0; x < xx1; x++){
1387 for (y = yy0; y < yy1; y++){
1389 for (x = xx0; x < xx1; x++){
1390 *ptr = (
comp->i_data[(
comp->coord[0][1] -
comp->coord[0][0]) * y + x]);
1403 bandpos, codsty->
nreslevels - reslevelno - 1);
1416 if (
s->compression_rate_enc)
1434 for (tileno = 0; tileno <
s->numXtiles *
s->numYtiles; tileno++){
1435 if (
s->tile[tileno].comp) {
1436 for (compno = 0; compno <
s->ncomponents; compno++){
1450 for (tileno = 0; tileno <
s->numXtiles *
s->numYtiles; tileno++){
1452 for (compno = 0; compno <
s->ncomponents; compno++)
1463 const AVFrame *pict,
int *got_packet)
1467 uint8_t *chunkstart, *jp2cstart, *jp2hstart;
1474 s->buf =
s->buf_start =
pkt->data;
1475 s->buf_end =
pkt->data +
pkt->size;
1491 bytestream_put_be32(&
s->buf, 0x0000000C);
1492 bytestream_put_be32(&
s->buf, 0x6A502020);
1493 bytestream_put_be32(&
s->buf, 0x0D0A870A);
1495 chunkstart =
s->buf;
1496 bytestream_put_be32(&
s->buf, 0);
1499 bytestream_put_be32(&
s->buf, 0);
1504 bytestream_put_be32(&
s->buf, 0);
1507 chunkstart =
s->buf;
1508 bytestream_put_be32(&
s->buf, 0);
1510 bytestream_put_be32(&
s->buf, avctx->
height);
1511 bytestream_put_be32(&
s->buf, avctx->
width);
1512 bytestream_put_be16(&
s->buf,
s->ncomponents);
1513 bytestream_put_byte(&
s->buf,
s->cbps[0]);
1514 bytestream_put_byte(&
s->buf, 7);
1515 bytestream_put_byte(&
s->buf, 0);
1516 bytestream_put_byte(&
s->buf, 0);
1519 chunkstart =
s->buf;
1520 bytestream_put_be32(&
s->buf, 0);
1522 bytestream_put_byte(&
s->buf, 1);
1523 bytestream_put_byte(&
s->buf, 0);
1524 bytestream_put_byte(&
s->buf, 0);
1526 bytestream_put_be32(&
s->buf, 16);
1527 }
else if (
s->ncomponents == 1) {
1528 bytestream_put_be32(&
s->buf, 17);
1530 bytestream_put_be32(&
s->buf, 18);
1535 const uint8_t *palette = pict->
data[1];
1536 chunkstart =
s->buf;
1537 bytestream_put_be32(&
s->buf, 0);
1540 bytestream_put_byte(&
s->buf, 3);
1541 bytestream_put_be24(&
s->buf, 0x070707);
1543 bytestream_put_be24(&
s->buf, HAVE_BIGENDIAN ?
AV_RB24(palette + 1) :
AV_RL24(palette));
1547 chunkstart =
s->buf;
1548 bytestream_put_be32(&
s->buf, 0);
1550 for (
i = 0;
i < 3;
i++) {
1551 bytestream_put_be16(&
s->buf, 0);
1552 bytestream_put_byte(&
s->buf, 1);
1553 bytestream_put_byte(&
s->buf,
i);
1560 bytestream_put_be32(&
s->buf, 0);
1564 if (
s->buf_end -
s->buf < 2)
1576 for (tileno = 0; tileno <
s->numXtiles *
s->numYtiles; tileno++){
1578 if (!(psotptr =
put_sot(
s, tileno)))
1580 if (
s->buf_end -
s->buf < 2)
1585 bytestream_put_be32(&psotptr,
s->buf - psotptr + 6);
1587 if (
s->buf_end -
s->buf < 2)
1595 pkt->size =
s->buf -
s->buf_start;
1605 char *saveptr =
NULL;
1610 s->layer_rates[0] = 0;
1611 s->compression_rate_enc = 0;
1616 if (token && (rate = strtol(token,
NULL, 10))) {
1617 s->layer_rates[0] = rate <= 1 ? 0:rate;
1627 if (rate = strtol(token,
NULL, 10)) {
1628 if (nlayers >= 100) {
1631 s->layer_rates[nlayers] = rate <= 1 ? 0:rate;
1638 for (
i = 1;
i < nlayers;
i++) {
1639 if (
s->layer_rates[
i] >=
s->layer_rates[
i-1]) {
1643 s->nlayers = nlayers;
1644 s->compression_rate_enc = 1;
1660 av_log(avctx,
AV_LOG_WARNING,
"Layer rates invalid. Encoding with 1 layer based on quality metric.\n");
1662 s->layer_rates[0] = 0;
1663 s->compression_rate_enc = 0;
1685 if ((
s->tile_width & (
s->tile_width -1)) ||
1686 (
s->tile_height & (
s->tile_height-1))) {
1698 s->ncomponents =
desc->nb_components;
1699 for (
i = 0;
i < 4;
i++) {
1700 s->cbps[
i] =
desc->comp[
i].depth;
1701 s->comp_remap[
i] =
i;
1707 s->chroma_shift,
s->chroma_shift + 1);
1711 s->comp_remap[0] = 2;
1712 s->comp_remap[1] = 0;
1713 s->comp_remap[2] = 1;
1738#define OFFSET(x) offsetof(Jpeg2000EncoderContext, x)
1739#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1762 .class_name =
"jpeg 2000 encoder",
1769 .p.name =
"jpeg2000",
static double val(void *priv, double ch)
static const char *const format[]
const FFCodec ff_jpeg2000_encoder
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Libavcodec external API header.
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
static int FUNC tile(CodedBitstreamContext *ctx, RWContext *rw, APVRawTile *current, int tile_idx, uint32_t tile_size)
#define i(width, name, range_min, range_max)
#define xi(width, name, var, range_min, range_max, subs,...)
#define bit(string, value)
#define ss(width, name, subs,...)
static LevelCodes lev[4+3+3]
#define CODEC_PIXFMTS(...)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
common internal and external API header
static __device__ float ceil(float a)
int(* init)(AVBSFContext *ctx)
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
#define FF_INPUT_BUFFER_MIN_SIZE
Used by some encoders as upper bound for the length of headers.
static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame, AVPacket *pkt)
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
@ AV_OPT_TYPE_INT
Underlying C type is int.
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
const char * av_default_item_name(void *ptr)
Return the context name.
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok().
#define LIBAVUTIL_VERSION_INT
static void scale(int *out, const int *in, const int w, const int h, const int shift)
static int init_tiles(Jpeg2000EncoderContext *s)
compute the sizes of tiles, resolution levels, bands, etc.
static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, int layno, int precno, const uint8_t *expn, int numgbits, int packetno, int nlayers)
static int put_com(Jpeg2000EncoderContext *s, int compno)
static int parse_layer_rates(Jpeg2000EncoderContext *s)
#define WMSEDEC_SHIFT
must be >= 13
static void update_size(uint8_t *size, const uint8_t *end)
static void makelayers(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
static int getcut(Jpeg2000Cblk *cblk, uint64_t lambda)
static void makelayer(Jpeg2000EncoderContext *s, int layno, double thresh, Jpeg2000Tile *tile, int final)
static av_cold void init_luts(void)
static void truncpasses(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile)
static void putnumpasses(Jpeg2000EncoderContext *s, int n)
static int encode_tile(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno)
static uint8_t * put_sot(Jpeg2000EncoderContext *s, int tileno)
static void encode_refpass(Jpeg2000T1Context *t1, int width, int height, int *nmsedec, int bpno)
static void reinit(Jpeg2000EncoderContext *s)
static int encode_packets(Jpeg2000EncoderContext *s, Jpeg2000Tile *tile, int tileno, int nlayers)
static av_cold int j2kenc_init(AVCodecContext *avctx)
static int j2kenc_destroy(AVCodecContext *avctx)
static void encode_sigpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
static void init_quantization(Jpeg2000EncoderContext *s)
static int lut_nmsedec_sig0[1<< NMSEDEC_BITS]
static void compute_rates(Jpeg2000EncoderContext *s)
static void tag_tree_update(Jpeg2000TgtNode *node)
update the value in node
static void cleanup(Jpeg2000EncoderContext *s)
static void put_num(Jpeg2000EncoderContext *s, int num, int n)
put n least significant bits of a number num
static void j2k_flush(Jpeg2000EncoderContext *s)
flush the bitstream
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
static int lut_nmsedec_sig[1<< NMSEDEC_BITS]
static int lut_nmsedec_ref0[1<< NMSEDEC_BITS]
static void encode_cblk(Jpeg2000EncoderContext *s, Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk, Jpeg2000Tile *tile, int width, int height, int bandpos, int lev)
static int lut_nmsedec_ref[1<< NMSEDEC_BITS]
static int put_qcd(Jpeg2000EncoderContext *s, int compno)
static int getnmsedec_sig(int x, int bpno)
static int getnmsedec_ref(int x, int bpno)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, int threshold)
code the value stored in node
static const int dwt_norms[2][4][10]
static const AVClass j2k_class
static void encode_clnpass(Jpeg2000T1Context *t1, int width, int height, int bandno, int *nmsedec, int bpno)
static int put_siz(Jpeg2000EncoderContext *s)
#define COPY_FRAME(D, PIXEL)
static int put_cod(Jpeg2000EncoderContext *s)
void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h, int val)
void av_cold ff_jpeg2000_init_tier1_luts(void)
void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
int ff_jpeg2000_init_component(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, Jpeg2000QuantStyle *qntsty, const int cbps, int dx, int dy, AVCodecContext *avctx)
void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1, int x, int y, int negative)
JPEG 2000 structures and defines common to encoder and decoder.
#define JPEG2000_T1_SIG_NB
#define JPEG2000_CSTY_SOP
static int ff_jpeg2000_ceildivpow2(int a, int b)
#define JPEG2000_PGOD_PCRL
static int ff_jpeg2000_ceildiv(int a, int64_t b)
#define JPEG2000_CSTY_EPH
#define JPEG2000_PGOD_CPRL
#define JPEG2000_PGOD_RPCL
#define JPEG2000_MAX_PASSES
#define JPEG2000_PGOD_RLCP
static int ff_jpeg2000_getsgnctxno(int flag, int *xorbit)
static int ff_jpeg2000_getrefctxno(int flag)
static int ff_jpeg2000_getsigctxno(int flag, int bandno)
#define JPEG2000_PGOD_LRCP
int ff_dwt_encode(DWTContext *s, void *t)
Libavcodec version macros.
Macro definitions for various function/variable attributes.
static int ff_thread_once(char *control, void(*routine)(void))
static const uint16_t mask[17]
void * av_calloc(size_t nmemb, size_t size)
Memory handling functions.
void ff_mqc_encode(MqcState *mqc, uint8_t *cxstate, int d)
code bit d with context cx
int ff_mqc_flush_to(MqcState *mqc, uint8_t *dst, int *dst_len)
flush the encoder [returns number of bytes encoded]
void ff_mqc_initenc(MqcState *mqc, uint8_t *bp)
initialize the encoder
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
#define AV_PIX_FMT_FLAG_RGB
The pixel format contains RGB-like data (as opposed to YUV/grayscale).
#define AV_PIX_FMT_FLAG_PLANAR
At least one pixel component is not in the first data plane.
#define AV_PIX_FMT_GBRAP12
#define AV_PIX_FMT_YUV420P16
#define AV_PIX_FMT_YUV444P12
#define AV_PIX_FMT_YUV444P9
#define AV_PIX_FMT_YUV420P10
#define AV_PIX_FMT_YUV440P12
#define AV_PIX_FMT_GBRAP16
#define AV_PIX_FMT_YUV422P9
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
#define AV_PIX_FMT_YUVA444P10
#define AV_PIX_FMT_YUVA420P16
#define AV_PIX_FMT_YUV420P12
#define AV_PIX_FMT_YUVA420P10
#define AV_PIX_FMT_YUVA422P9
#define AV_PIX_FMT_YUV422P12
#define AV_PIX_FMT_GBRP10
#define AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_GRAY12
#define AV_PIX_FMT_RGBA64
#define AV_PIX_FMT_GBRP12
#define AV_PIX_FMT_YUV420P9
#define AV_PIX_FMT_YUVA420P9
#define AV_PIX_FMT_YUVA422P10
#define AV_PIX_FMT_YUV420P14
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
@ AV_PIX_FMT_YA8
8 bits gray, 8 bits alpha
#define AV_PIX_FMT_YUV422P14
#define AV_PIX_FMT_GRAY10
#define AV_PIX_FMT_GRAY14
#define AV_PIX_FMT_YUV422P16
#define AV_PIX_FMT_YUV440P10
#define AV_PIX_FMT_GRAY16
#define AV_PIX_FMT_GBRAP10
#define AV_PIX_FMT_YUVA444P16
#define AV_PIX_FMT_YUVA422P16
#define AV_PIX_FMT_GBRP16
#define AV_PIX_FMT_YUV444P14
#define AV_PIX_FMT_YUVA444P9
#define AV_PIX_FMT_GBRP14
#define AV_PIX_FMT_YUV444P16
#define AV_PIX_FMT_YUV444P10
static const float pred[4]
Describe the class of an AVClass context structure.
main external API structure.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
int width
picture width / height.
This structure describes decoded (raw) audio or video data.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
This structure stores compressed data.
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
uint16_t log2_cblk_height
uint8_t log2_prec_widths[JPEG2000_MAX_RESLEVELS]
uint8_t log2_prec_heights[JPEG2000_MAX_RESLEVELS]
Jpeg2000CodingStyle codsty
uint8_t cbps[4]
bits per sample in particular components
Jpeg2000QuantStyle qntsty
int height
image width and height
uint8_t compression_rate_enc
Is compression done using compression ratio?
Jpeg2000TgtNode * zerobits
Jpeg2000TgtNode * cblkincl
uint16_t mant[JPEG2000_MAX_DECLEVELS *3]
uint8_t expn[JPEG2000_MAX_DECLEVELS *3]
struct Jpeg2000TgtNode * parent
#define av_malloc_array(a, b)
#define avpriv_request_sample(...)