73 int remaining = packet->
size - writer->
pos;
74 if (nb_bytes > remaining) {
75 OPJ_SIZE_T needed = nb_bytes - remaining;
77 if (needed > max_growth) {
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) {
349 const int linesize[4], opj_image_t *image)
356 const int numcomps = image->numcomps;
358 for (compno = 0; compno < numcomps; ++compno) {
359 if (image->comps[compno].w > linesize[0] / numcomps) {
365 for (compno = 0; compno < numcomps; ++compno) {
366 for (y = 0; y < avctx->
height; ++y) {
367 image_line = image->comps[compno].data + y * image->comps[compno].w;
368 frame_index = y * linesize[0] + compno;
369 for (x = 0; x < avctx->
width; ++x) {
370 image_line[x] =
src[0][frame_index];
371 frame_index += numcomps;
373 for (; x < image->comps[compno].w; ++x) {
374 image_line[x] = image_line[x - 1];
377 for (; y < image->comps[compno].h; ++y) {
378 image_line = image->comps[compno].data + y * image->comps[compno].w;
379 for (x = 0; x < image->comps[compno].w; ++x) {
380 image_line[x] = image_line[x - (int)image->comps[compno].w];
390 const int linesize[4], opj_image_t *image)
396 const int numcomps = image->numcomps;
397 const uint16_t *frame_ptr = (
const uint16_t *)
src[0];
399 for (compno = 0; compno < numcomps; ++compno) {
400 if (image->comps[compno].w > linesize[0] / numcomps) {
406 for (compno = 0; compno < numcomps; ++compno) {
407 for (y = 0; y < avctx->
height; ++y) {
408 image_line = image->comps[compno].data + y * image->comps[compno].w;
409 frame_index = y * (linesize[0] / 2) + compno;
410 for (x = 0; x < avctx->
width; ++x) {
411 image_line[x] = frame_ptr[frame_index] >> 4;
412 frame_index += numcomps;
414 for (; x < image->comps[compno].w; ++x) {
415 image_line[x] = image_line[x - 1];
418 for (; y < image->comps[compno].h; ++y) {
419 image_line = image->comps[compno].data + y * image->comps[compno].w;
420 for (x = 0; x < image->comps[compno].w; ++x) {
421 image_line[x] = image_line[x - (int)image->comps[compno].w];
430 const int linesize[4], opj_image_t *image)
437 const int numcomps = image->numcomps;
438 const uint16_t *frame_ptr = (
const uint16_t*)
src[0];
440 for (compno = 0; compno < numcomps; ++compno) {
441 if (image->comps[compno].w > linesize[0] / numcomps) {
447 for (compno = 0; compno < numcomps; ++compno) {
448 for (y = 0; y < avctx->
height; ++y) {
449 image_line = image->comps[compno].data + y * image->comps[compno].w;
450 frame_index = y * (linesize[0] / 2) + compno;
451 for (x = 0; x < avctx->
width; ++x) {
452 image_line[x] = frame_ptr[frame_index];
453 frame_index += numcomps;
455 for (; x < image->comps[compno].w; ++x) {
456 image_line[x] = image_line[x - 1];
459 for (; y < image->comps[compno].h; ++y) {
460 image_line = image->comps[compno].data + y * image->comps[compno].w;
461 for (x = 0; x < image->comps[compno].w; ++x) {
462 image_line[x] = image_line[x - (int)image->comps[compno].w];
471 const int linesize[4], opj_image_t *image)
480 const int numcomps = image->numcomps;
482 for (compno = 0; compno < numcomps; ++compno) {
483 if (image->comps[compno].w > linesize[compno]) {
489 for (compno = 0; compno < numcomps; ++compno) {
490 width = (avctx->
width + image->comps[compno].dx - 1) / image->comps[compno].dx;
491 height = (avctx->
height + image->comps[compno].dy - 1) / image->comps[compno].dy;
492 for (y = 0; y <
height; ++y) {
493 image_line = image->comps[compno].data + y * image->comps[compno].w;
494 frame_index = y * linesize[compno];
495 for (x = 0; x <
width; ++x)
496 image_line[x] =
src[compno][frame_index++];
497 for (; x < image->comps[compno].w; ++x) {
498 image_line[x] = image_line[x - 1];
501 for (; y < image->comps[compno].h; ++y) {
502 image_line = image->comps[compno].data + y * image->comps[compno].w;
503 for (x = 0; x < image->comps[compno].w; ++x) {
504 image_line[x] = image_line[x - (int)image->comps[compno].w];
513 const int linesize[4], opj_image_t *image)
522 const int numcomps = image->numcomps;
524 for (compno = 0; compno < numcomps; ++compno) {
525 if (image->comps[compno].w > linesize[compno]) {
531 for (compno = 0; compno < numcomps; ++compno) {
532 const uint16_t *frame_ptr = (
const uint16_t *)
src[compno];
533 width = (avctx->
width + image->comps[compno].dx - 1) / image->comps[compno].dx;
534 height = (avctx->
height + image->comps[compno].dy - 1) / image->comps[compno].dy;
535 for (y = 0; y <
height; ++y) {
536 image_line = image->comps[compno].data + y * image->comps[compno].w;
537 frame_index = y * (linesize[compno] / 2);
538 for (x = 0; x <
width; ++x)
539 image_line[x] = frame_ptr[frame_index++];
540 for (; x < image->comps[compno].w; ++x) {
541 image_line[x] = image_line[x - 1];
544 for (; y < image->comps[compno].h; ++y) {
545 image_line = image->comps[compno].data + y * image->comps[compno].w;
546 for (x = 0; x < image->comps[compno].w; ++x) {
547 image_line[x] = image_line[x - (int)image->comps[compno].w];
562 opj_codec_t *compress =
NULL;
563 opj_stream_t *stream =
NULL;
567 int linesize[4] = {
frame->linesize[0],
frame->linesize[1],
598 linesize[0] =
frame->linesize[2];
599 linesize[1] =
frame->linesize[0];
600 linesize[2] =
frame->linesize[1];
651 "The frame's pixel format '%s' is not supported\n",
660 "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)) {
716 opj_stream_destroy(stream);
717 opj_destroy_codec(compress);
718 opj_image_destroy(image);
722#define OFFSET(x) offsetof(LibOpenJPEGContext, x)
723#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
732 {
"cinema_mode",
"Digital Cinema",
OFFSET(cinema_mode),
AV_OPT_TYPE_INT, { .i64 = OPJ_OFF }, OPJ_OFF, OPJ_CINEMA4K_24,
VE, .unit =
"cinema_mode" },
737 {
"prog_order",
"Progression Order",
OFFSET(prog_order),
AV_OPT_TYPE_INT, { .i64 = OPJ_LRCP }, OPJ_LRCP, OPJ_CPRL,
VE, .unit =
"prog_order" },
751 .class_name =
"libopenjpeg",
758 .p.name =
"libopenjpeg",
788 .p.wrapper_name =
"libopenjpeg",
static const char *const format[]
const FFCodec ff_libopenjpeg_encoder
static AVFormatContext * ctx
Libavcodec external API header.
#define i(width, name, range_min, range_max)
static int FUNC user_data(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawUserData *current)
#define CODEC_PIXFMTS(...)
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
common internal and external API header
int(* init)(AVBSFContext *ctx)
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
static void stream_seek(VideoState *is, int64_t pos, int64_t rel, int by_bytes)
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
@ AV_OPT_TYPE_INT
Underlying C type is int.
#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_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
int av_grow_packet(AVPacket *pkt, int grow_by)
Increase packet size, correctly zeroing padding.
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
#define AVERROR_EXTERNAL
Generic error in an external library.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
#define LIBAVUTIL_VERSION_INT
static int libopenjpeg_copy_packed12(AVCodecContext *avctx, const uint8_t *src[4], const int linesize[4], opj_image_t *image)
static void error_callback(const char *msg, void *data)
static opj_image_t * mj2_create_image(AVCodecContext *avctx, opj_cparameters_t *parameters)
static void cinema_parameters(opj_cparameters_t *p)
static const AVClass openjpeg_class
static void warning_callback(const char *msg, void *data)
static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const uint8_t *src[4], const int linesize[4], opj_image_t *image)
static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const uint8_t *src[4], const int linesize[4], opj_image_t *image)
static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const uint8_t *src[4], const int linesize[4], opj_image_t *image)
static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx)
static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const uint8_t *src[4], const int linesize[4], opj_image_t *image)
static OPJ_SIZE_T stream_write(void *out_buffer, OPJ_SIZE_T nb_bytes, void *user_data)
static OPJ_BOOL stream_seek(OPJ_OFF_T nb_bytes, void *user_data)
static OPJ_OFF_T stream_skip(OPJ_OFF_T nb_bytes, void *user_data)
static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
static void info_callback(const char *msg, void *data)
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
#define AV_PIX_FMT_YUV420P16
#define AV_PIX_FMT_YUV444P12
#define AV_PIX_FMT_YUV444P9
#define AV_PIX_FMT_YUV420P10
#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_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
@ 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_GRAY16
#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
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.
This structure stores compressed data.
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
opj_cparameters_t enc_params