29 #include "libavutil/avassert.h"
30 #include "libavutil/common.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/opt.h"
37 #if HAVE_OPENJPEG_1_5_OPENJPEG_H
38 # include <openjpeg-1.5/openjpeg.h>
40 # include <openjpeg.h>
79 opj_image_cmptparm_t cmptparm[4] = {{0}};
85 OPJ_COLOR_SPACE color_space = CLRSPC_UNKNOWN;
87 sub_dx[0] = sub_dx[3] = 1;
88 sub_dy[0] = sub_dy[3] = 1;
98 color_space = CLRSPC_GRAY;
111 color_space = CLRSPC_SRGB;
146 color_space = CLRSPC_SYCC;
150 "The requested pixel format '%s' is not supported\n",
155 for (i = 0; i < numcomps; i++) {
158 cmptparm[i].sgnd = 0;
159 cmptparm[i].dx = sub_dx[i];
160 cmptparm[i].dy = sub_dy[i];
161 cmptparm[i].w = (avctx->
width + sub_dx[i] - 1) / sub_dx[i];
162 cmptparm[i].h = (avctx->
height + sub_dy[i] - 1) / sub_dy[i];
165 img = opj_image_create(numcomps, cmptparm, color_space);
171 img->x1 = (avctx->
width - 1) * parameters->subsampling_dx + 1;
172 img->y1 = (avctx->
height - 1) * parameters->subsampling_dy + 1;
182 opj_set_default_encoder_parameters(&ctx->
enc_params);
238 ctx->
stream = opj_cio_open((opj_common_ptr)ctx->
compress, NULL, 0);
251 memset(&ctx->
event_mgr, 0,
sizeof(opj_event_mgr_t));
260 opj_cio_close(ctx->
stream);
262 opj_destroy_compress(ctx->
compress);
264 opj_image_destroy(ctx->
image);
277 const int numcomps = image->numcomps;
279 for (compno = 0; compno < numcomps; ++compno) {
280 if (image->comps[compno].w > frame->
linesize[0] / numcomps) {
286 for (compno = 0; compno < numcomps; ++compno) {
287 for (y = 0; y < avctx->
height; ++
y) {
288 image_line = image->comps[compno].data + y * image->comps[compno].w;
289 frame_index = y * frame->
linesize[0] + compno;
290 for (x = 0; x < avctx->
width; ++x) {
291 image_line[x] = frame->
data[0][frame_index];
292 frame_index += numcomps;
294 for (; x < image->comps[compno].w; ++x) {
295 image_line[x] = image_line[x - 1];
298 for (; y < image->comps[compno].h; ++
y) {
299 image_line = image->comps[compno].data + y * image->comps[compno].w;
300 for (x = 0; x < image->comps[compno].w; ++x) {
301 image_line[x] = image_line[x - image->comps[compno].w];
317 const int numcomps = image->numcomps;
318 uint16_t *frame_ptr = (uint16_t*)frame->
data[0];
320 for (compno = 0; compno < numcomps; ++compno) {
321 if (image->comps[compno].w > frame->
linesize[0] / numcomps) {
327 for (compno = 0; compno < numcomps; ++compno) {
328 for (y = 0; y < avctx->
height; ++
y) {
329 image_line = image->comps[compno].data + y * image->comps[compno].w;
330 frame_index = y * (frame->
linesize[0] / 2) + compno;
331 for (x = 0; x < avctx->
width; ++x) {
332 image_line[x] = frame_ptr[frame_index] >> 4;
333 frame_index += numcomps;
335 for (; x < image->comps[compno].w; ++x) {
336 image_line[x] = image_line[x - 1];
339 for (; y < image->comps[compno].h; ++
y) {
340 image_line = image->comps[compno].data + y * image->comps[compno].w;
341 for (x = 0; x < image->comps[compno].w; ++x) {
342 image_line[x] = image_line[x - image->comps[compno].w];
357 const int numcomps = image->numcomps;
358 uint16_t *frame_ptr = (uint16_t*)frame->
data[0];
360 for (compno = 0; compno < numcomps; ++compno) {
361 if (image->comps[compno].w > frame->
linesize[0] / numcomps) {
367 for (compno = 0; compno < numcomps; ++compno) {
368 for (y = 0; y < avctx->
height; ++
y) {
369 image_line = image->comps[compno].data + y * image->comps[compno].w;
370 frame_index = y * (frame->
linesize[0] / 2) + compno;
371 for (x = 0; x < avctx->
width; ++x) {
372 image_line[x] = frame_ptr[frame_index];
373 frame_index += numcomps;
375 for (; x < image->comps[compno].w; ++x) {
376 image_line[x] = image_line[x - 1];
379 for (; y < image->comps[compno].h; ++
y) {
380 image_line = image->comps[compno].data + y * image->comps[compno].w;
381 for (x = 0; x < image->comps[compno].w; ++x) {
382 image_line[x] = image_line[x - image->comps[compno].w];
399 const int numcomps = image->numcomps;
401 for (compno = 0; compno < numcomps; ++compno) {
402 if (image->comps[compno].w > frame->
linesize[compno]) {
408 for (compno = 0; compno < numcomps; ++compno) {
409 width = avctx->
width / image->comps[compno].dx;
410 height = avctx->
height / image->comps[compno].dy;
412 image_line = image->comps[compno].data + y * image->comps[compno].w;
413 frame_index = y * frame->
linesize[compno];
414 for (x = 0; x <
width; ++x)
415 image_line[x] = frame->
data[compno][frame_index++];
416 for (; x < image->comps[compno].w; ++x) {
417 image_line[x] = image_line[x - 1];
420 for (; y < image->comps[compno].h; ++
y) {
421 image_line = image->comps[compno].data + y * image->comps[compno].w;
422 for (x = 0; x < image->comps[compno].w; ++x) {
423 image_line[x] = image_line[x - image->comps[compno].w];
440 const int numcomps = image->numcomps;
443 for (compno = 0; compno < numcomps; ++compno) {
444 if (image->comps[compno].w > frame->
linesize[compno]) {
450 for (compno = 0; compno < numcomps; ++compno) {
451 width = avctx->
width / image->comps[compno].dx;
452 height = avctx->
height / image->comps[compno].dy;
453 frame_ptr = (uint16_t*)frame->
data[compno];
455 image_line = image->comps[compno].data + y * image->comps[compno].w;
456 frame_index = y * (frame->
linesize[compno] / 2);
457 for (x = 0; x <
width; ++x)
458 image_line[x] = frame_ptr[frame_index++];
459 for (; x < image->comps[compno].w; ++x) {
460 image_line[x] = image_line[x - 1];
463 for (; y < image->comps[compno].h; ++
y) {
464 image_line = image->comps[compno].data + y * image->comps[compno].w;
465 for (x = 0; x < image->comps[compno].w; ++x) {
466 image_line[x] = image_line[x - image->comps[compno].w];
478 opj_cinfo_t *compress = ctx->
compress;
479 opj_image_t *image = ctx->
image;
480 opj_cio_t *stream = ctx->
stream;
558 "The frame's pixel format '%s' is not supported\n",
566 "Could not copy the frame data to the internal image buffer\n");
571 if (!opj_encode(compress, stream, image, NULL)) {
576 len = cio_tell(stream);
581 memcpy(pkt->
data, stream->buffer, len);
591 opj_cio_close(ctx->
stream);
593 opj_destroy_compress(ctx->
compress);
595 opj_image_destroy(ctx->
image);
601 #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
602 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
604 {
"format",
"Codec Format",
OFFSET(format),
AV_OPT_TYPE_INT, { .i64 = CODEC_JP2 }, CODEC_J2K, CODEC_JP2,
VE,
"format" },
611 {
"cinema_mode",
"Digital Cinema",
OFFSET(cinema_mode),
AV_OPT_TYPE_INT, { .i64 = OFF }, OFF, CINEMA4K_24,
VE,
"cinema_mode" },
616 {
"prog_order",
"Progression Order",
OFFSET(prog_order),
AV_OPT_TYPE_INT, { .i64 = LRCP }, LRCP, CPRL,
VE,
"prog_order" },
638 .
name =
"libopenjpeg",
#define AV_PIX_FMT_YUV444P12
#define AV_PIX_FMT_YUV422P16
#define AV_PIX_FMT_GBR24P
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and/or allocate data.
static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
This structure describes decoded (raw) audio or video data.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
static av_cold int init(AVCodecContext *avctx)
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
AVCodec ff_libopenjpeg_encoder
packed RGB 8:8:8, 24bpp, RGBRGB...
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
#define AV_PIX_FMT_GBRP10
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
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.
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static const AVClass openjpeg_class
static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
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)
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
#define AV_PIX_FMT_YUVA420P16
opj_cparameters_t enc_params
static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
const char * av_default_item_name(void *ctx)
Return the context name.
#define AV_PIX_FMT_YUVA444P10
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define AV_PIX_FMT_YUVA420P10
const char * name
Name of the codec implementation.
static void warning_callback(const char *msg, void *data)
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
#define AV_PIX_FMT_RGBA64
const OptionDef options[]
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
#define AV_PIX_FMT_YUV422P14
#define AV_PIX_FMT_GRAY16
uint16_t depth_minus1
number of bits in the component minus 1
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.
#define AV_PIX_FMT_YUVA422P9
#define AV_PIX_FMT_YUV444P14
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AV_PIX_FMT_YUV420P12
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
Libavcodec external API header.
AVPixelFormat
Pixel format.
#define AV_PIX_FMT_YUVA422P10
#define AV_PIX_FMT_YUVA444P9
int flags
A combination of AV_PKT_FLAG values.
#define AV_PIX_FMT_GBRP12
#define AV_PIX_FMT_YUV444P16
uint8_t nb_components
The number of components each pixel has, (1-4)
int width
picture width / height.
#define AV_PIX_FMT_YUV422P12
#define AV_PIX_FMT_GBRP16
static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx)
#define AV_PIX_FMT_YUVA444P16
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y 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 void close(AVCodecParserContext *s)
#define AV_PIX_FMT_YUV444P9
static int libopenjpeg_copy_packed12(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
BYTE int const BYTE int int int height
Describe the class of an AVClass context structure.
opj_event_mgr_t event_mgr
AVFrame * coded_frame
the picture in the bitstream
#define AV_PIX_FMT_GBRP14
#define AV_PIX_FMT_YUV420P9
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
static void info_callback(const char *msg, void *data)
int global_quality
Global quality for codecs which cannot change it per frame.
#define AV_PIX_FMT_YUV420P16
#define AV_PIX_FMT_YUV422P9
static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
common internal api header.
#define AV_PIX_FMT_YUV444P10
#define AV_PIX_FMT_YUV420P14
#define AV_PIX_FMT_YUVA422P16
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
#define AV_PIX_FMT_YUV420P10
static void error_callback(const char *msg, void *data)
#define AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUVA420P9
This structure stores compressed data.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)