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