73     int remaining = packet->
size - writer->
pos;
 
   74     if (nb_bytes > remaining) {
 
   75         OPJ_SIZE_T 
needed = nb_bytes - remaining;
 
   78             return (OPJ_SIZE_T)-1;
 
   81             return (OPJ_SIZE_T)-1;
 
   84     memcpy(packet->
data + writer->
pos, out_buffer, nb_bytes);
 
   85     writer->
pos += (
int)nb_bytes;
 
   94         if (writer->
pos == 0) {
 
   95             return (OPJ_SIZE_T)-1;
 
   97         if (nb_bytes + writer->
pos < 0) {
 
   98             nb_bytes = -writer->
pos;
 
  101         int remaining = packet->
size - writer->
pos;
 
  102         if (nb_bytes > remaining) {
 
  103             OPJ_SIZE_T 
needed = nb_bytes - remaining;
 
  105             if (
needed > max_growth) {
 
  106                 return (OPJ_SIZE_T)-1;
 
  109                 return (OPJ_SIZE_T)-1;
 
  113     writer->
pos += (
int)nb_bytes;
 
  124     if (nb_bytes > packet->
size) {
 
  130     writer->
pos = (
int)nb_bytes;
 
  147     p->image_offset_x0 = 0;
 
  148     p->image_offset_y0 = 0;
 
  151     p->cblockw_init = 32;
 
  152     p->cblockh_init = 32;
 
  156     p->prog_order = OPJ_CPRL;
 
  162     p->subsampling_dx = 1;
 
  163     p->subsampling_dy = 1;
 
  174     opj_image_cmptparm_t cmptparm[4] = {{0}};
 
  180     OPJ_COLOR_SPACE color_space = OPJ_CLRSPC_UNKNOWN;
 
  182     sub_dx[0] = sub_dx[3] = 1;
 
  183     sub_dy[0] = sub_dy[3] = 1;
 
  184     sub_dx[1] = sub_dx[2] = 1 << 
desc->log2_chroma_w;
 
  185     sub_dy[1] = sub_dy[2] = 1 << 
desc->log2_chroma_h;
 
  187     numcomps = 
desc->nb_components;
 
  197         color_space = OPJ_CLRSPC_GRAY;
 
  210         color_space = OPJ_CLRSPC_SRGB;
 
  245         color_space = OPJ_CLRSPC_SYCC;
 
  249                "The requested pixel format '%s' is not supported\n",
 
  254     for (
i = 0; 
i < numcomps; 
i++) {
 
  255         cmptparm[
i].prec = 
desc->comp[
i].depth;
 
  256         cmptparm[
i].bpp  = 
desc->comp[
i].depth;
 
  257         cmptparm[
i].sgnd = 0;
 
  258         cmptparm[
i].dx = sub_dx[
i];
 
  259         cmptparm[
i].dy = sub_dy[
i];
 
  260         cmptparm[
i].w = (avctx->
width + sub_dx[
i] - 1) / sub_dx[
i];
 
  261         cmptparm[
i].h = (avctx->
height + sub_dy[
i] - 1) / sub_dy[
i];
 
  264     img = opj_image_create(numcomps, cmptparm, color_space);
 
  273     img->x1 = (avctx->
width  - 1) * parameters->subsampling_dx + 1;
 
  274     img->y1 = (avctx->
height - 1) * parameters->subsampling_dy + 1;
 
  284     opj_set_default_encoder_parameters(&
ctx->enc_params);
 
  286     switch (
ctx->cinema_mode) {
 
  287     case OPJ_CINEMA2K_24:
 
  288         ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_2K;
 
  289         ctx->enc_params.max_cs_size = OPJ_CINEMA_24_CS;
 
  290         ctx->enc_params.max_comp_size = OPJ_CINEMA_24_COMP;
 
  292     case OPJ_CINEMA2K_48:
 
  293         ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_2K;
 
  294         ctx->enc_params.max_cs_size = OPJ_CINEMA_48_CS;
 
  295         ctx->enc_params.max_comp_size = OPJ_CINEMA_48_COMP;
 
  297     case OPJ_CINEMA4K_24:
 
  298         ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_4K;
 
  299         ctx->enc_params.max_cs_size = OPJ_CINEMA_24_CS;
 
  300         ctx->enc_params.max_comp_size = OPJ_CINEMA_24_COMP;
 
  304     switch (
ctx->profile) {
 
  306         if (
ctx->enc_params.rsiz == OPJ_PROFILE_CINEMA_4K) {
 
  310         ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_2K;
 
  313         if (
ctx->enc_params.rsiz == OPJ_PROFILE_CINEMA_2K) {
 
  317         ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_4K;
 
  323                "Invalid parameter pairing: cinema_mode and profile conflict.\n");
 
  327     if (!
ctx->numresolution) {
 
  328         ctx->numresolution = 6;
 
  330             ctx->numresolution --;
 
  333     ctx->enc_params.prog_order = 
ctx->prog_order;
 
  334     ctx->enc_params.numresolution = 
ctx->numresolution;
 
  335     ctx->enc_params.irreversible = 
ctx->irreversible;
 
  336     ctx->enc_params.cp_disto_alloc = 
ctx->disto_alloc;
 
  337     ctx->enc_params.cp_fixed_quality = 
ctx->fixed_quality;
 
  338     ctx->enc_params.tcp_numlayers = 1;
 
  341     if (
ctx->cinema_mode > 0) {
 
  355     const int numcomps = image->numcomps;
 
  357     for (compno = 0; compno < numcomps; ++compno) {
 
  358         if (image->comps[compno].w > 
frame->linesize[0] / numcomps) {
 
  364     for (compno = 0; compno < numcomps; ++compno) {
 
  365         for (y = 0; y < avctx->
height; ++y) {
 
  366             image_line = image->comps[compno].data + y * image->comps[compno].w;
 
  367             frame_index = y * 
frame->linesize[0] + compno;
 
  368             for (x = 0; x < avctx->
width; ++x) {
 
  369                 image_line[x] = 
frame->data[0][frame_index];
 
  370                 frame_index += numcomps;
 
  372             for (; x < image->comps[compno].w; ++x) {
 
  373                 image_line[x] = image_line[x - 1];
 
  376         for (; y < image->comps[compno].h; ++y) {
 
  377             image_line = image->comps[compno].data + y * image->comps[compno].w;
 
  378             for (x = 0; x < image->comps[compno].w; ++x) {
 
  379                 image_line[x] = image_line[x - (
int)image->comps[compno].w];
 
  394     const int numcomps  = image->numcomps;
 
  395     uint16_t *frame_ptr = (uint16_t *)
frame->data[0];
 
  397     for (compno = 0; compno < numcomps; ++compno) {
 
  398         if (image->comps[compno].w > 
frame->linesize[0] / numcomps) {
 
  404     for (compno = 0; compno < numcomps; ++compno) {
 
  405         for (y = 0; y < avctx->
height; ++y) {
 
  406             image_line = image->comps[compno].data + y * image->comps[compno].w;
 
  407             frame_index = y * (
frame->linesize[0] / 2) + compno;
 
  408             for (x = 0; x < avctx->
width; ++x) {
 
  409                 image_line[x] = frame_ptr[frame_index] >> 4;
 
  410                 frame_index += numcomps;
 
  412             for (; x < image->comps[compno].w; ++x) {
 
  413                 image_line[x] = image_line[x - 1];
 
  416         for (; y < image->comps[compno].h; ++y) {
 
  417             image_line = image->comps[compno].data + y * image->comps[compno].w;
 
  418             for (x = 0; x < image->comps[compno].w; ++x) {
 
  419                 image_line[x] = image_line[x - (
int)image->comps[compno].w];
 
  434     const int numcomps = image->numcomps;
 
  435     uint16_t *frame_ptr = (uint16_t*)
frame->data[0];
 
  437     for (compno = 0; compno < numcomps; ++compno) {
 
  438         if (image->comps[compno].w > 
frame->linesize[0] / numcomps) {
 
  444     for (compno = 0; compno < numcomps; ++compno) {
 
  445         for (y = 0; y < avctx->
height; ++y) {
 
  446             image_line = image->comps[compno].data + y * image->comps[compno].w;
 
  447             frame_index = y * (
frame->linesize[0] / 2) + compno;
 
  448             for (x = 0; x < avctx->
width; ++x) {
 
  449                 image_line[x] = frame_ptr[frame_index];
 
  450                 frame_index += numcomps;
 
  452             for (; x < image->comps[compno].w; ++x) {
 
  453                 image_line[x] = image_line[x - 1];
 
  456         for (; y < image->comps[compno].h; ++y) {
 
  457             image_line = image->comps[compno].data + y * image->comps[compno].w;
 
  458             for (x = 0; x < image->comps[compno].w; ++x) {
 
  459                 image_line[x] = image_line[x - (
int)image->comps[compno].w];
 
  476     const int numcomps = image->numcomps;
 
  478     for (compno = 0; compno < numcomps; ++compno) {
 
  479         if (image->comps[compno].w > 
frame->linesize[compno]) {
 
  485     for (compno = 0; compno < numcomps; ++compno) {
 
  486         width  = (avctx->
width + image->comps[compno].dx - 1) / image->comps[compno].dx;
 
  487         height = (avctx->
height + image->comps[compno].dy - 1) / image->comps[compno].dy;
 
  488         for (y = 0; y < 
height; ++y) {
 
  489             image_line = image->comps[compno].data + y * image->comps[compno].w;
 
  490             frame_index = y * 
frame->linesize[compno];
 
  491             for (x = 0; x < 
width; ++x)
 
  492                 image_line[x] = 
frame->data[compno][frame_index++];
 
  493             for (; x < image->comps[compno].w; ++x) {
 
  494                 image_line[x] = image_line[x - 1];
 
  497         for (; y < image->comps[compno].h; ++y) {
 
  498             image_line = image->comps[compno].data + y * image->comps[compno].w;
 
  499             for (x = 0; x < image->comps[compno].w; ++x) {
 
  500                 image_line[x] = image_line[x - (
int)image->comps[compno].w];
 
  517     const int numcomps = image->numcomps;
 
  520     for (compno = 0; compno < numcomps; ++compno) {
 
  521         if (image->comps[compno].w > 
frame->linesize[compno]) {
 
  527     for (compno = 0; compno < numcomps; ++compno) {
 
  528         width     = (avctx->
width + image->comps[compno].dx - 1) / image->comps[compno].dx;
 
  529         height    = (avctx->
height + image->comps[compno].dy - 1) / image->comps[compno].dy;
 
  530         frame_ptr = (uint16_t *)
frame->data[compno];
 
  532             image_line = image->comps[compno].data + y * image->comps[compno].w;
 
  533             frame_index = y * (
frame->linesize[compno] / 2);
 
  534             for (x = 0; x < 
width; ++x)
 
  535                 image_line[x] = frame_ptr[frame_index++];
 
  536             for (; x < image->comps[compno].w; ++x) {
 
  537                 image_line[x] = image_line[x - 1];
 
  540         for (; y < image->comps[compno].h; ++y) {
 
  541             image_line = image->comps[compno].data + y * image->comps[compno].w;
 
  542             for (x = 0; x < image->comps[compno].w; ++x) {
 
  543                 image_line[x] = image_line[x - (
int)image->comps[compno].w];
 
  559     opj_codec_t *compress   = 
NULL;
 
  560     opj_stream_t *stream    = 
NULL;
 
  650                "The frame's pixel format '%s' is not supported\n",
 
  659                "Could not copy the frame data to the internal image buffer\n");
 
  668     compress = opj_create_compress(
ctx->format);
 
  683     if (!opj_setup_encoder(compress, &
ctx->enc_params, image)) {
 
  688     stream = opj_stream_default_create(OPJ_STREAM_WRITE);
 
  700     opj_stream_set_user_data(stream, &writer, 
NULL);
 
  702     if (!opj_start_compress(compress, image, stream) ||
 
  703         !opj_encode(compress, stream) ||
 
  704         !opj_end_compress(compress, stream)) {
 
  717     opj_stream_destroy(stream);
 
  718     opj_destroy_codec(compress);
 
  719     opj_image_destroy(image);
 
  723 #define OFFSET(x) offsetof(LibOpenJPEGContext, x) 
  724 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM 
  733     { 
"cinema_mode",   
"Digital Cinema",    
OFFSET(cinema_mode),   
AV_OPT_TYPE_INT,   { .i64 = OPJ_OFF         }, OPJ_OFF,       OPJ_CINEMA4K_24, 
VE, 
"cinema_mode" },
 
  738     { 
"prog_order",    
"Progression Order", 
OFFSET(prog_order),    
AV_OPT_TYPE_INT,   { .i64 = OPJ_LRCP    }, OPJ_LRCP,  OPJ_CPRL,    
VE, 
"prog_order"  },
 
  759     .
name           = 
"libopenjpeg",
 
  788     .wrapper_name   = 
"libopenjpeg",