37 #if HAVE_OPENJPEG_2_1_OPENJPEG_H 
   38 #  include <openjpeg-2.1/openjpeg.h> 
   39 #elif HAVE_OPENJPEG_2_0_OPENJPEG_H 
   40 #  include <openjpeg-2.0/openjpeg.h> 
   41 #elif HAVE_OPENJPEG_1_5_OPENJPEG_H 
   42 #  include <openjpeg-1.5/openjpeg.h> 
   44 #  include <openjpeg.h> 
   47 #if HAVE_OPENJPEG_2_1_OPENJPEG_H || HAVE_OPENJPEG_2_0_OPENJPEG_H 
   48 #  define OPENJPEG_MAJOR_VERSION 2 
   49 #  define OPJ(x) OPJ_##x 
   51 #  define OPENJPEG_MAJOR_VERSION 1 
   59 #if OPENJPEG_MAJOR_VERSION == 1 
   61 #endif // OPENJPEG_MAJOR_VERSION == 1 
   88 #if OPENJPEG_MAJOR_VERSION == 2 
   89 typedef struct PacketWriter {
 
   94 static OPJ_SIZE_T stream_write(
void *out_buffer, OPJ_SIZE_T nb_bytes, 
void *
user_data)
 
   98     int remaining = packet->
size - writer->pos;
 
   99     if (nb_bytes > remaining) {
 
  100         OPJ_SIZE_T needed = nb_bytes - remaining;
 
  102         if (needed > max_growth) {
 
  103             return (OPJ_SIZE_T)-1;
 
  106             return (OPJ_SIZE_T)-1;
 
  109     memcpy(packet->
data + writer->pos, out_buffer, nb_bytes);
 
  110     writer->pos += (int)nb_bytes;
 
  114 static OPJ_OFF_T stream_skip(OPJ_OFF_T nb_bytes, 
void *user_data)
 
  119         if (writer->pos == 0) {
 
  120             return (OPJ_SIZE_T)-1;
 
  122         if (nb_bytes + writer->pos < 0) {
 
  123             nb_bytes = -writer->pos;
 
  126         int remaining = packet->
size - writer->pos;
 
  127         if (nb_bytes > remaining) {
 
  128             OPJ_SIZE_T needed = nb_bytes - remaining;
 
  130             if (needed > max_growth) {
 
  131                 return (OPJ_SIZE_T)-1;
 
  134                 return (OPJ_SIZE_T)-1;
 
  138     writer->pos += (int)nb_bytes;
 
  142 static OPJ_BOOL 
stream_seek(OPJ_OFF_T nb_bytes, 
void *user_data)
 
  149     if (nb_bytes > packet->
size) {
 
  155     writer->pos = (int)nb_bytes;
 
  158 #endif // OPENJPEG_MAJOR_VERSION == 2 
  173     p->image_offset_x0 = 0;
 
  174     p->image_offset_y0 = 0;
 
  177     p->cblockw_init = 32;
 
  178     p->cblockh_init = 32;
 
  182     p->prog_order = 
OPJ(CPRL);
 
  188     p->subsampling_dx = 1;
 
  189     p->subsampling_dy = 1;
 
  200     opj_image_cmptparm_t cmptparm[4] = {{0}};
 
  206     OPJ_COLOR_SPACE color_space = 
OPJ(CLRSPC_UNKNOWN);
 
  208     sub_dx[0] = sub_dx[3] = 1;
 
  209     sub_dy[0] = sub_dy[3] = 1;
 
  220         color_space = 
OPJ(CLRSPC_GRAY);
 
  233         color_space = 
OPJ(CLRSPC_SRGB);
 
  268         color_space = 
OPJ(CLRSPC_SYCC);
 
  272                "The requested pixel format '%s' is not supported\n",
 
  277     for (i = 0; i < numcomps; i++) {
 
  280         cmptparm[i].sgnd = 0;
 
  281         cmptparm[i].dx = sub_dx[i];
 
  282         cmptparm[i].dy = sub_dy[i];
 
  283         cmptparm[i].w = (avctx->
width + sub_dx[i] - 1) / sub_dx[i];
 
  284         cmptparm[i].h = (avctx->
height + sub_dy[i] - 1) / sub_dy[i];
 
  287     img = opj_image_create(numcomps, cmptparm, color_space);
 
  296     img->x1 = (avctx->
width  - 1) * parameters->subsampling_dx + 1;
 
  297     img->y1 = (avctx->
height - 1) * parameters->subsampling_dy + 1;
 
  307     opj_set_default_encoder_parameters(&ctx->
enc_params);
 
  309 #if HAVE_OPENJPEG_2_1_OPENJPEG_H 
  311     case OPJ_CINEMA2K_24:
 
  313         ctx->
enc_params.max_cs_size = OPJ_CINEMA_24_CS;
 
  314         ctx->
enc_params.max_comp_size = OPJ_CINEMA_24_COMP;
 
  316     case OPJ_CINEMA2K_48:
 
  318         ctx->
enc_params.max_cs_size = OPJ_CINEMA_48_CS;
 
  319         ctx->
enc_params.max_comp_size = OPJ_CINEMA_48_COMP;
 
  321     case OPJ_CINEMA4K_24:
 
  323         ctx->
enc_params.max_cs_size = OPJ_CINEMA_24_CS;
 
  324         ctx->
enc_params.max_comp_size = OPJ_CINEMA_24_COMP;
 
  330         if (ctx->
enc_params.rsiz == OPJ_PROFILE_CINEMA_4K) {
 
  337         if (ctx->
enc_params.rsiz == OPJ_PROFILE_CINEMA_2K) {
 
  347                "Invalid parameter pairing: cinema_mode and profile conflict.\n");
 
  384     opj_image_destroy(ctx->
image);
 
  396     const int numcomps = image->numcomps;
 
  398     for (compno = 0; compno < numcomps; ++compno) {
 
  399         if (image->comps[compno].w > frame->
linesize[0] / numcomps) {
 
  405     for (compno = 0; compno < numcomps; ++compno) {
 
  406         for (y = 0; y < avctx->
height; ++y) {
 
  407             image_line = image->comps[compno].data + y * image->comps[compno].w;
 
  408             frame_index = y * frame->
linesize[0] + compno;
 
  409             for (x = 0; x < avctx->
width; ++x) {
 
  410                 image_line[x] = frame->
data[0][frame_index];
 
  411                 frame_index += numcomps;
 
  413             for (; x < image->comps[compno].w; ++x) {
 
  414                 image_line[x] = image_line[x - 1];
 
  417         for (; y < image->comps[compno].h; ++y) {
 
  418             image_line = image->comps[compno].data + y * image->comps[compno].w;
 
  419             for (x = 0; x < image->comps[compno].w; ++x) {
 
  420                 image_line[x] = image_line[x - image->comps[compno].w];
 
  435     const int numcomps  = image->numcomps;
 
  436     uint16_t *frame_ptr = (uint16_t *)frame->
data[0];
 
  438     for (compno = 0; compno < numcomps; ++compno) {
 
  439         if (image->comps[compno].w > frame->
linesize[0] / numcomps) {
 
  445     for (compno = 0; compno < numcomps; ++compno) {
 
  446         for (y = 0; y < avctx->
height; ++y) {
 
  447             image_line = image->comps[compno].data + y * image->comps[compno].w;
 
  448             frame_index = y * (frame->
linesize[0] / 2) + compno;
 
  449             for (x = 0; x < avctx->
width; ++x) {
 
  450                 image_line[x] = frame_ptr[frame_index] >> 4;
 
  451                 frame_index += numcomps;
 
  453             for (; x < image->comps[compno].w; ++x) {
 
  454                 image_line[x] = image_line[x - 1];
 
  457         for (; y < image->comps[compno].h; ++y) {
 
  458             image_line = image->comps[compno].data + y * image->comps[compno].w;
 
  459             for (x = 0; x < image->comps[compno].w; ++x) {
 
  460                 image_line[x] = image_line[x - image->comps[compno].w];
 
  475     const int numcomps = image->numcomps;
 
  476     uint16_t *frame_ptr = (uint16_t*)frame->
data[0];
 
  478     for (compno = 0; compno < numcomps; ++compno) {
 
  479         if (image->comps[compno].w > frame->
linesize[0] / numcomps) {
 
  485     for (compno = 0; compno < numcomps; ++compno) {
 
  486         for (y = 0; y < avctx->
height; ++y) {
 
  487             image_line = image->comps[compno].data + y * image->comps[compno].w;
 
  488             frame_index = y * (frame->
linesize[0] / 2) + compno;
 
  489             for (x = 0; x < avctx->
width; ++x) {
 
  490                 image_line[x] = frame_ptr[frame_index];
 
  491                 frame_index += numcomps;
 
  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 - image->comps[compno].w];
 
  517     const int numcomps = image->numcomps;
 
  519     for (compno = 0; compno < numcomps; ++compno) {
 
  520         if (image->comps[compno].w > frame->
linesize[compno]) {
 
  526     for (compno = 0; compno < numcomps; ++compno) {
 
  527         width  = avctx->
width / image->comps[compno].dx;
 
  528         height = avctx->
height / image->comps[compno].dy;
 
  529         for (y = 0; y < 
height; ++y) {
 
  530             image_line = image->comps[compno].data + y * image->comps[compno].w;
 
  531             frame_index = y * frame->
linesize[compno];
 
  532             for (x = 0; x < 
width; ++x)
 
  533                 image_line[x] = frame->
data[compno][frame_index++];
 
  534             for (; x < image->comps[compno].w; ++x) {
 
  535                 image_line[x] = image_line[x - 1];
 
  538         for (; y < image->comps[compno].h; ++y) {
 
  539             image_line = image->comps[compno].data + y * image->comps[compno].w;
 
  540             for (x = 0; x < image->comps[compno].w; ++x) {
 
  541                 image_line[x] = image_line[x - image->comps[compno].w];
 
  558     const int numcomps = image->numcomps;
 
  561     for (compno = 0; compno < numcomps; ++compno) {
 
  562         if (image->comps[compno].w > frame->
linesize[compno]) {
 
  568     for (compno = 0; compno < numcomps; ++compno) {
 
  569         width     = avctx->
width / image->comps[compno].dx;
 
  570         height    = avctx->
height / image->comps[compno].dy;
 
  571         frame_ptr = (uint16_t *)frame->
data[compno];
 
  573             image_line = image->comps[compno].data + y * image->comps[compno].w;
 
  574             frame_index = y * (frame->
linesize[compno] / 2);
 
  575             for (x = 0; x < 
width; ++x)
 
  576                 image_line[x] = frame_ptr[frame_index++];
 
  577             for (; x < image->comps[compno].w; ++x) {
 
  578                 image_line[x] = image_line[x - 1];
 
  581         for (; y < image->comps[compno].h; ++y) {
 
  582             image_line = image->comps[compno].data + y * image->comps[compno].w;
 
  583             for (x = 0; x < image->comps[compno].w; ++x) {
 
  584                 image_line[x] = image_line[x - image->comps[compno].w];
 
  596     opj_image_t *image      = ctx->
image;
 
  597 #if OPENJPEG_MAJOR_VERSION == 1 
  598     opj_cinfo_t *compress   = 
NULL;
 
  599     opj_cio_t *stream       = 
NULL;
 
  601 #else // OPENJPEG_MAJOR_VERSION == 2 
  602     opj_codec_t *compress   = 
NULL;
 
  603     opj_stream_t *stream    = 
NULL;
 
  604     PacketWriter writer     = { 0 };
 
  605 #endif // OPENJPEG_MAJOR_VERSION == 1 
  687                "The frame's pixel format '%s' is not supported\n",
 
  695                "Could not copy the frame data to the internal image buffer\n");
 
  699 #if OPENJPEG_MAJOR_VERSION == 2 
  703 #endif // OPENJPEG_MAJOR_VERSION == 2 
  705     compress = opj_create_compress(ctx->
format);
 
  712 #if OPENJPEG_MAJOR_VERSION == 1 
  713     opj_setup_encoder(compress, &ctx->
enc_params, image);
 
  714     stream = opj_cio_open((opj_common_ptr) compress, 
NULL, 0);
 
  715 #else // OPENJPEG_MAJOR_VERSION == 2 
  724     if (!opj_setup_encoder(compress, &ctx->
enc_params, image)) {
 
  729     stream = opj_stream_default_create(OPJ_STREAM_WRITE);
 
  730 #endif // OPENJPEG_MAJOR_VERSION == 1 
  737 #if OPENJPEG_MAJOR_VERSION == 1 
  742     opj_set_event_mgr((opj_common_ptr) compress, &ctx->
event_mgr, avctx);
 
  743     if (!opj_encode(compress, stream, image, 
NULL)) {
 
  749     len = cio_tell(stream);
 
  754     memcpy(pkt->
data, stream->buffer, len);
 
  755 #else // OPENJPEG_MAJOR_VERSION == 2 
  757     opj_stream_set_write_function(stream, stream_write);
 
  758     opj_stream_set_skip_function(stream, stream_skip);
 
  760 #if HAVE_OPENJPEG_2_1_OPENJPEG_H 
  761     opj_stream_set_user_data(stream, &writer, 
NULL);
 
  762 #elif HAVE_OPENJPEG_2_0_OPENJPEG_H 
  763     opj_stream_set_user_data(stream, &writer);
 
  765 #error Missing call to opj_stream_set_user_data 
  768     if (!opj_start_compress(compress, ctx->
image, stream) ||
 
  769         !opj_encode(compress, stream) ||
 
  770         !opj_end_compress(compress, stream)) {
 
  777 #endif // OPENJPEG_MAJOR_VERSION == 1 
  784 #if OPENJPEG_MAJOR_VERSION == 2 
  785     opj_stream_destroy(stream);
 
  786     opj_destroy_codec(compress);
 
  788     opj_cio_close(stream);
 
  789     opj_destroy_compress(compress);
 
  798     opj_image_destroy(ctx->
image);
 
  803 #define OFFSET(x) offsetof(LibOpenJPEGContext, x) 
  804 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM 
  840     .
name           = 
"libopenjpeg",
 
#define AV_PIX_FMT_YUVA422P16
static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
#define AV_PIX_FMT_YUVA422P9
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
This structure describes decoded (raw) audio or video data. 
ptrdiff_t const GLvoid * data
#define AV_PIX_FMT_YUVA420P10
#define AV_PIX_FMT_YUV444P14
8 bits gray, 8 bits alpha 
#define AV_PIX_FMT_YUVA422P10
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples) 
#define AV_LOG_WARNING
Something somehow does not look correct. 
#define LIBAVUTIL_VERSION_INT
packed RGB 8:8:8, 24bpp, RGBRGB... 
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding. 
static av_cold int init(AVCodecContext *avctx)
#define AV_PIX_FMT_RGBA64
AVCodec ff_libopenjpeg_encoder
#define AV_PIX_FMT_GBRP10
static void stream_seek(VideoState *is, int64_t pos, int64_t rel, int seek_by_bytes)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx. 
static const AVClass openjpeg_class
#define AV_PIX_FMT_YUV420P12
static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width. 
static av_cold int libopenjpeg_encode_close(AVCodecContext *avctx)
#define AV_CODEC_CAP_INTRA_ONLY
Codec is intra only. 
opj_cparameters_t enc_params
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples) 
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed. 
static void cinema_parameters(opj_cparameters_t *p)
#define AV_PIX_FMT_YUVA420P9
static void warning_callback(const char *msg, void *data)
#define AV_PIX_FMT_YUV444P16
#define AV_PIX_FMT_YUV422P12
#define AV_PIX_FMT_YUVA420P16
#define AV_PKT_FLAG_KEY
The packet contains a keyframe. 
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples) 
static opj_image_t * mj2_create_image(AVCodecContext *avctx, opj_cparameters_t *parameters)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered. 
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height. 
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g. 
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
AVS_Value void * user_data
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers. 
#define AV_PIX_FMT_YUVA444P16
simple assert() macros that are a bit more flexible than ISO C assert(). 
static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
const char * name
Name of the codec implementation. 
#define AV_PIX_FMT_YUV444P10
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading. 
packed RGBA 8:8:8:8, 32bpp, RGBARGBA... 
int flags
A combination of AV_PKT_FLAG values. 
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples) 
#define AV_PIX_FMT_YUV422P9
uint8_t nb_components
The number of components each pixel has, (1-4) 
#define AV_PIX_FMT_GBRP16
#define AV_PIX_FMT_GRAY16
int width
picture width / height. 
#define AV_PIX_FMT_YUVA444P10
#define AV_PIX_FMT_YUV444P9
#define AV_PIX_FMT_GBRP14
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src. 
static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx)
#define AV_PIX_FMT_YUV420P16
#define AV_PIX_FMT_YUV420P14
Libavcodec external API header. 
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line. 
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples) 
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
main external API structure. 
static int libopenjpeg_copy_packed12(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
#define AV_PIX_FMT_YUV420P10
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples) 
static const char * format
Describe the class of an AVClass context structure. 
opj_event_mgr_t event_mgr
#define AV_PIX_FMT_YUV420P9
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data. 
#define AV_PIX_FMT_GBR24P
static void info_callback(const char *msg, void *data)
static enum AVPixelFormat pix_fmts[]
#define AV_PIX_FMT_YUV422P14
#define AV_PIX_FMT_GBRP12
int global_quality
Global quality for codecs which cannot change it per frame. 
#define AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV444P12
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes. 
static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples) 
common internal api header. 
common internal and external API header 
#define AV_PIX_FMT_YUVA444P9
int av_grow_packet(AVPacket *pkt, int grow_by)
Increase packet size, correctly zeroing padding. 
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) 
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
static const AVOption options[]
static void error_callback(const char *msg, void *data)
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples) 
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. 
int depth
Number of bits in the component. 
#define AVERROR_EXTERNAL
Generic error in an external library. 
AVPixelFormat
Pixel format. 
This structure stores compressed data. 
#define AV_PIX_FMT_YUV422P16