00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #define OPJ_STATIC
00028 #include <openjpeg.h>
00029
00030 #include "libavutil/common.h"
00031 #include "libavutil/intreadwrite.h"
00032 #include "libavutil/imgutils.h"
00033 #include "libavutil/pixfmt.h"
00034 #include "libavutil/opt.h"
00035 #include "avcodec.h"
00036 #include "thread.h"
00037
00038 #define JP2_SIG_TYPE 0x6A502020
00039 #define JP2_SIG_VALUE 0x0D0A870A
00040
00041
00042
00043 #define RGB_PIXEL_FORMATS PIX_FMT_RGB24,PIX_FMT_RGBA,PIX_FMT_RGB48,PIX_FMT_RGBA64
00044 #define GRAY_PIXEL_FORMATS PIX_FMT_GRAY8,PIX_FMT_GRAY8A,PIX_FMT_GRAY16
00045 #define YUV_PIXEL_FORMATS PIX_FMT_YUV410P,PIX_FMT_YUV411P,PIX_FMT_YUVA420P, \
00046 PIX_FMT_YUV420P,PIX_FMT_YUV422P,PIX_FMT_YUVA422P, \
00047 PIX_FMT_YUV440P,PIX_FMT_YUV444P,PIX_FMT_YUVA444P, \
00048 PIX_FMT_YUV420P9,PIX_FMT_YUV422P9,PIX_FMT_YUV444P9, \
00049 PIX_FMT_YUV420P10,PIX_FMT_YUV422P10,PIX_FMT_YUV444P10, \
00050 PIX_FMT_YUV420P12,PIX_FMT_YUV422P12,PIX_FMT_YUV444P12, \
00051 PIX_FMT_YUV420P14,PIX_FMT_YUV422P14,PIX_FMT_YUV444P14, \
00052 PIX_FMT_YUV420P16,PIX_FMT_YUV422P16,PIX_FMT_YUV444P16
00053
00054 static const enum PixelFormat libopenjpeg_rgb_pix_fmts[] = {RGB_PIXEL_FORMATS};
00055 static const enum PixelFormat libopenjpeg_gray_pix_fmts[] = {GRAY_PIXEL_FORMATS};
00056 static const enum PixelFormat libopenjpeg_yuv_pix_fmts[] = {YUV_PIXEL_FORMATS};
00057 static const enum PixelFormat libopenjpeg_all_pix_fmts[] = {RGB_PIXEL_FORMATS,GRAY_PIXEL_FORMATS,YUV_PIXEL_FORMATS};
00058
00059 typedef struct {
00060 AVClass *class;
00061 opj_dparameters_t dec_params;
00062 AVFrame image;
00063 int lowqual;
00064 } LibOpenJPEGContext;
00065
00066 static inline int libopenjpeg_matches_pix_fmt(const opj_image_t *image, enum PixelFormat pix_fmt)
00067 {
00068 AVPixFmtDescriptor descriptor = av_pix_fmt_descriptors[pix_fmt];
00069 int match = 1;
00070
00071 if (descriptor.nb_components != image->numcomps) {
00072 return 0;
00073 }
00074
00075 switch (descriptor.nb_components) {
00076 case 4: match = match && descriptor.comp[3].depth_minus1 + 1 >= image->comps[3].prec &&
00077 1 == image->comps[3].dx &&
00078 1 == image->comps[3].dy;
00079 case 3: match = match && descriptor.comp[2].depth_minus1 + 1 >= image->comps[2].prec &&
00080 1 << descriptor.log2_chroma_w == image->comps[2].dx &&
00081 1 << descriptor.log2_chroma_h == image->comps[2].dy;
00082 case 2: match = match && descriptor.comp[1].depth_minus1 + 1 >= image->comps[1].prec &&
00083 1 << descriptor.log2_chroma_w == image->comps[1].dx &&
00084 1 << descriptor.log2_chroma_h == image->comps[1].dy;
00085 case 1: match = match && descriptor.comp[0].depth_minus1 + 1 >= image->comps[0].prec &&
00086 1 == image->comps[0].dx &&
00087 1 == image->comps[0].dy;
00088 default:
00089 break;
00090 }
00091
00092 return match;
00093 }
00094
00095 static inline enum PixelFormat libopenjpeg_guess_pix_fmt(const opj_image_t *image) {
00096 int index;
00097 const enum PixelFormat *possible_fmts = NULL;
00098 int possible_fmts_nb = 0;
00099
00100 switch (image->color_space) {
00101 case CLRSPC_SRGB:
00102 possible_fmts = libopenjpeg_rgb_pix_fmts;
00103 possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_rgb_pix_fmts);
00104 break;
00105 case CLRSPC_GRAY:
00106 possible_fmts = libopenjpeg_gray_pix_fmts;
00107 possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_gray_pix_fmts);
00108 break;
00109 case CLRSPC_SYCC:
00110 possible_fmts = libopenjpeg_yuv_pix_fmts;
00111 possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_yuv_pix_fmts);
00112 break;
00113 default:
00114 possible_fmts = libopenjpeg_all_pix_fmts;
00115 possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_all_pix_fmts);
00116 break;
00117 }
00118
00119 for (index = 0; index < possible_fmts_nb; ++index) {
00120 if (libopenjpeg_matches_pix_fmt(image, possible_fmts[index])) {
00121 return possible_fmts[index];
00122 }
00123 }
00124
00125 return PIX_FMT_NONE;
00126 }
00127
00128 static inline int libopenjpeg_ispacked(enum PixelFormat pix_fmt) {
00129 int i, component_plane;
00130
00131 if (pix_fmt == PIX_FMT_GRAY16)
00132 return 0;
00133
00134 component_plane = av_pix_fmt_descriptors[pix_fmt].comp[0].plane;
00135 for (i = 1; i < av_pix_fmt_descriptors[pix_fmt].nb_components; i++) {
00136 if (component_plane != av_pix_fmt_descriptors[pix_fmt].comp[i].plane)
00137 return 0;
00138 }
00139 return 1;
00140 }
00141
00142 static inline void libopenjpeg_copy_to_packed8(AVFrame *picture, opj_image_t *image) {
00143 uint8_t *img_ptr;
00144 int index, x, y, c;
00145 for (y = 0; y < picture->height; y++) {
00146 index = y*picture->width;
00147 img_ptr = picture->data[0] + y*picture->linesize[0];
00148 for (x = 0; x < picture->width; x++, index++) {
00149 for (c = 0; c < image->numcomps; c++) {
00150 *img_ptr++ = image->comps[c].data[index];
00151 }
00152 }
00153 }
00154 }
00155
00156 static inline void libopenjpeg_copy_to_packed16(AVFrame *picture, opj_image_t *image) {
00157 uint16_t *img_ptr;
00158 int index, x, y, c;
00159 int adjust[4];
00160 for (x = 0; x < image->numcomps; x++)
00161 adjust[x] = FFMAX(FFMIN(16 - image->comps[x].prec, 8), 0);
00162
00163 for (y = 0; y < picture->height; y++) {
00164 index = y*picture->width;
00165 img_ptr = (uint16_t*) (picture->data[0] + y*picture->linesize[0]);
00166 for (x = 0; x < picture->width; x++, index++) {
00167 for (c = 0; c < image->numcomps; c++) {
00168 *img_ptr++ = image->comps[c].data[index] << adjust[c];
00169 }
00170 }
00171 }
00172 }
00173
00174 static inline void libopenjpeg_copyto8(AVFrame *picture, opj_image_t *image) {
00175 int *comp_data;
00176 uint8_t *img_ptr;
00177 int index, x, y;
00178
00179 for (index = 0; index < image->numcomps; index++) {
00180 comp_data = image->comps[index].data;
00181 for (y = 0; y < image->comps[index].h; y++) {
00182 img_ptr = picture->data[index] + y * picture->linesize[index];
00183 for (x = 0; x < image->comps[index].w; x++) {
00184 *img_ptr = (uint8_t) *comp_data;
00185 img_ptr++;
00186 comp_data++;
00187 }
00188 }
00189 }
00190 }
00191
00192 static inline void libopenjpeg_copyto16(AVFrame *picture, opj_image_t *image) {
00193 int *comp_data;
00194 uint16_t *img_ptr;
00195 int index, x, y;
00196 for (index = 0; index < image->numcomps; index++) {
00197 comp_data = image->comps[index].data;
00198 for (y = 0; y < image->comps[index].h; y++) {
00199 img_ptr = (uint16_t*) (picture->data[index] + y * picture->linesize[index]);
00200 for (x = 0; x < image->comps[index].w; x++) {
00201 *img_ptr = *comp_data;
00202 img_ptr++;
00203 comp_data++;
00204 }
00205 }
00206 }
00207 }
00208
00209 static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
00210 {
00211 LibOpenJPEGContext *ctx = avctx->priv_data;
00212
00213 opj_set_default_decoder_parameters(&ctx->dec_params);
00214 avcodec_get_frame_defaults(&ctx->image);
00215 avctx->coded_frame = &ctx->image;
00216 return 0;
00217 }
00218
00219 static av_cold int libopenjpeg_decode_init_thread_copy(AVCodecContext *avctx)
00220 {
00221 LibOpenJPEGContext *ctx = avctx->priv_data;
00222
00223 avctx->coded_frame = &ctx->image;
00224 return 0;
00225 }
00226
00227 static int libopenjpeg_decode_frame(AVCodecContext *avctx,
00228 void *data, int *data_size,
00229 AVPacket *avpkt)
00230 {
00231 uint8_t *buf = avpkt->data;
00232 int buf_size = avpkt->size;
00233 LibOpenJPEGContext *ctx = avctx->priv_data;
00234 AVFrame *picture = &ctx->image, *output = data;
00235 opj_dinfo_t *dec;
00236 opj_cio_t *stream;
00237 opj_image_t *image;
00238 int width, height, ret = -1;
00239 int pixel_size = 0;
00240 int ispacked = 0;
00241 int i;
00242
00243 *data_size = 0;
00244
00245
00246 if ((AV_RB32(buf) == 12) &&
00247 (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
00248 (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
00249 dec = opj_create_decompress(CODEC_JP2);
00250 } else {
00251
00252
00253 if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
00254 buf += 8;
00255 dec = opj_create_decompress(CODEC_J2K);
00256 }
00257
00258 if (!dec) {
00259 av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
00260 return -1;
00261 }
00262 opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
00263 ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
00264 ctx->dec_params.cp_layer = ctx->lowqual;
00265
00266 opj_setup_decoder(dec, &ctx->dec_params);
00267 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
00268
00269 if (!stream) {
00270 av_log(avctx, AV_LOG_ERROR,
00271 "Codestream could not be opened for reading.\n");
00272 opj_destroy_decompress(dec);
00273 return -1;
00274 }
00275
00276
00277 image = opj_decode_with_info(dec, stream, NULL);
00278 opj_cio_close(stream);
00279
00280 if (!image) {
00281 av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
00282 opj_destroy_decompress(dec);
00283 return -1;
00284 }
00285
00286 width = image->x1 - image->x0;
00287 height = image->y1 - image->y0;
00288
00289 if (av_image_check_size(width, height, 0, avctx) < 0) {
00290 av_log(avctx, AV_LOG_ERROR,
00291 "%dx%d dimension invalid.\n", width, height);
00292 goto done;
00293 }
00294
00295 avcodec_set_dimensions(avctx, width, height);
00296
00297 if (avctx->pix_fmt != PIX_FMT_NONE)
00298 if (!libopenjpeg_matches_pix_fmt(image, avctx->pix_fmt))
00299 avctx->pix_fmt = PIX_FMT_NONE;
00300
00301 if (avctx->pix_fmt == PIX_FMT_NONE)
00302 avctx->pix_fmt = libopenjpeg_guess_pix_fmt(image);
00303
00304 if (avctx->pix_fmt == PIX_FMT_NONE) {
00305 av_log(avctx, AV_LOG_ERROR, "Unable to determine pixel format\n");
00306 goto done;
00307 }
00308 for (i = 0; i < image->numcomps; i++)
00309 if (image->comps[i].prec > avctx->bits_per_raw_sample)
00310 avctx->bits_per_raw_sample = image->comps[i].prec;
00311
00312 if (picture->data[0])
00313 ff_thread_release_buffer(avctx, picture);
00314
00315 if (ff_thread_get_buffer(avctx, picture) < 0) {
00316 av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n");
00317 goto done;
00318 }
00319
00320 ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
00321 ctx->dec_params.cp_reduce = avctx->lowres;
00322
00323 opj_setup_decoder(dec, &ctx->dec_params);
00324 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
00325 if (!stream) {
00326 av_log(avctx, AV_LOG_ERROR,
00327 "Codestream could not be opened for reading.\n");
00328 goto done;
00329 }
00330
00331 opj_image_destroy(image);
00332
00333 image = opj_decode_with_info(dec, stream, NULL);
00334 opj_cio_close(stream);
00335
00336 if (!image) {
00337 av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
00338 goto done;
00339 }
00340
00341 pixel_size = av_pix_fmt_descriptors[avctx->pix_fmt].comp[0].step_minus1 + 1;
00342 ispacked = libopenjpeg_ispacked(avctx->pix_fmt);
00343
00344 switch (pixel_size) {
00345 case 1:
00346 if (ispacked) {
00347 libopenjpeg_copy_to_packed8(picture, image);
00348 } else {
00349 libopenjpeg_copyto8(picture, image);
00350 }
00351 break;
00352 case 2:
00353 if (ispacked) {
00354 libopenjpeg_copy_to_packed8(picture, image);
00355 } else {
00356 libopenjpeg_copyto16(picture, image);
00357 }
00358 break;
00359 case 3:
00360 case 4:
00361 if (ispacked) {
00362 libopenjpeg_copy_to_packed8(picture, image);
00363 }
00364 break;
00365 case 6:
00366 case 8:
00367 if (ispacked) {
00368 libopenjpeg_copy_to_packed16(picture, image);
00369 }
00370 break;
00371 default:
00372 av_log(avctx, AV_LOG_ERROR, "unsupported pixel size %d\n", pixel_size);
00373 goto done;
00374 }
00375
00376 *output = ctx->image;
00377 *data_size = sizeof(AVPicture);
00378 ret = buf_size;
00379
00380 done:
00381 opj_image_destroy(image);
00382 opj_destroy_decompress(dec);
00383 return ret;
00384 }
00385
00386 static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
00387 {
00388 LibOpenJPEGContext *ctx = avctx->priv_data;
00389
00390 if (ctx->image.data[0])
00391 ff_thread_release_buffer(avctx, &ctx->image);
00392 return 0;
00393 }
00394
00395 #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
00396 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
00397
00398 static const AVOption options[] = {
00399 { "lowqual", "Limit the number of layers used for decoding", OFFSET(lowqual), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VD },
00400 { NULL },
00401 };
00402
00403 static const AVClass class = {
00404 .class_name = "libopenjpeg",
00405 .item_name = av_default_item_name,
00406 .option = options,
00407 .version = LIBAVUTIL_VERSION_INT,
00408 };
00409
00410 AVCodec ff_libopenjpeg_decoder = {
00411 .name = "libopenjpeg",
00412 .type = AVMEDIA_TYPE_VIDEO,
00413 .id = AV_CODEC_ID_JPEG2000,
00414 .priv_data_size = sizeof(LibOpenJPEGContext),
00415 .init = libopenjpeg_decode_init,
00416 .close = libopenjpeg_decode_close,
00417 .decode = libopenjpeg_decode_frame,
00418 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
00419 .max_lowres = 31,
00420 .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
00421 .priv_class = &class,
00422 .init_thread_copy = ONLY_IF_THREADS_ENABLED(libopenjpeg_decode_init_thread_copy),
00423 };