00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "avcodec.h"
00028 #include "libavutil/intreadwrite.h"
00029 #define OPJ_STATIC
00030 #include <openjpeg.h>
00031
00032 #define JP2_SIG_TYPE 0x6A502020
00033 #define JP2_SIG_VALUE 0x0D0A870A
00034
00035 typedef struct {
00036 opj_dparameters_t dec_params;
00037 AVFrame image;
00038 } LibOpenJPEGContext;
00039
00040 static int check_image_attributes(opj_image_t *image)
00041 {
00042 return(image->comps[0].dx == image->comps[1].dx &&
00043 image->comps[1].dx == image->comps[2].dx &&
00044 image->comps[0].dy == image->comps[1].dy &&
00045 image->comps[1].dy == image->comps[2].dy &&
00046 image->comps[0].prec == image->comps[1].prec &&
00047 image->comps[1].prec == image->comps[2].prec);
00048 }
00049
00050 static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
00051 {
00052 LibOpenJPEGContext *ctx = avctx->priv_data;
00053
00054 opj_set_default_decoder_parameters(&ctx->dec_params);
00055 avctx->coded_frame = &ctx->image;
00056 return 0;
00057 }
00058
00059 static int libopenjpeg_decode_frame(AVCodecContext *avctx,
00060 void *data, int *data_size,
00061 const uint8_t *buf, int buf_size)
00062 {
00063 LibOpenJPEGContext *ctx = avctx->priv_data;
00064 AVFrame *picture = &ctx->image, *output = data;
00065 opj_dinfo_t *dec;
00066 opj_cio_t *stream;
00067 opj_image_t *image;
00068 int width, height, has_alpha = 0, ret = -1;
00069 int x, y, index;
00070 uint8_t *img_ptr;
00071 int adjust[4];
00072
00073 *data_size = 0;
00074
00075
00076 if((AV_RB32(buf) == 12) &&
00077 (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
00078 (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
00079 dec = opj_create_decompress(CODEC_JP2);
00080 } else {
00081 dec = opj_create_decompress(CODEC_J2K);
00082 }
00083
00084 if(!dec) {
00085 av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
00086 return -1;
00087 }
00088 opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
00089
00090
00091 opj_setup_decoder(dec, &ctx->dec_params);
00092 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
00093 if(!stream) {
00094 av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n");
00095 opj_destroy_decompress(dec);
00096 return -1;
00097 }
00098
00099
00100 image = opj_decode_with_info(dec, stream, NULL);
00101 opj_cio_close(stream);
00102 if(!image) {
00103 av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
00104 opj_destroy_decompress(dec);
00105 return -1;
00106 }
00107 width = image->comps[0].w;
00108 height = image->comps[0].h;
00109 if(avcodec_check_dimensions(avctx, width, height) < 0) {
00110 av_log(avctx, AV_LOG_ERROR, "%dx%d dimension invalid.\n", width, height);
00111 goto done;
00112 }
00113 avcodec_set_dimensions(avctx, width, height);
00114
00115 switch(image->numcomps)
00116 {
00117 case 1: avctx->pix_fmt = PIX_FMT_GRAY8;
00118 break;
00119 case 3: if(check_image_attributes(image)) {
00120 avctx->pix_fmt = PIX_FMT_RGB24;
00121 } else {
00122 avctx->pix_fmt = PIX_FMT_GRAY8;
00123 av_log(avctx, AV_LOG_ERROR, "Only first component will be used.\n");
00124 }
00125 break;
00126 case 4: has_alpha = 1;
00127 avctx->pix_fmt = PIX_FMT_RGB32;
00128 break;
00129 default: av_log(avctx, AV_LOG_ERROR, "%d components unsupported.\n", image->numcomps);
00130 goto done;
00131 }
00132
00133 if(picture->data[0])
00134 avctx->release_buffer(avctx, picture);
00135
00136 if(avctx->get_buffer(avctx, picture) < 0) {
00137 av_log(avctx, AV_LOG_ERROR, "Couldn't allocate image buffer.\n");
00138 return -1;
00139 }
00140
00141 for(x = 0; x < image->numcomps; x++) {
00142 adjust[x] = FFMAX(image->comps[x].prec - 8, 0);
00143 }
00144
00145 for(y = 0; y < height; y++) {
00146 index = y*width;
00147 img_ptr = picture->data[0] + y*picture->linesize[0];
00148 for(x = 0; x < width; x++, index++) {
00149 *img_ptr++ = image->comps[0].data[index] >> adjust[0];
00150 if(image->numcomps > 2 && check_image_attributes(image)) {
00151 *img_ptr++ = image->comps[1].data[index] >> adjust[1];
00152 *img_ptr++ = image->comps[2].data[index] >> adjust[2];
00153 if(has_alpha)
00154 *img_ptr++ = image->comps[3].data[index] >> adjust[3];
00155 }
00156 }
00157 }
00158
00159 *output = ctx->image;
00160 *data_size = sizeof(AVPicture);
00161 ret = buf_size;
00162
00163 done:
00164 opj_image_destroy(image);
00165 opj_destroy_decompress(dec);
00166 return ret;
00167 }
00168
00169 static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
00170 {
00171 LibOpenJPEGContext *ctx = avctx->priv_data;
00172
00173 if(ctx->image.data[0])
00174 avctx->release_buffer(avctx, &ctx->image);
00175 return 0 ;
00176 }
00177
00178
00179 AVCodec libopenjpeg_decoder = {
00180 "libopenjpeg",
00181 CODEC_TYPE_VIDEO,
00182 CODEC_ID_JPEG2000,
00183 sizeof(LibOpenJPEGContext),
00184 libopenjpeg_decode_init,
00185 NULL,
00186 libopenjpeg_decode_close,
00187 libopenjpeg_decode_frame,
00188 NULL,
00189 .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG based JPEG 2000 decoder"),
00190 } ;