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