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 "avcodec.h"
00029 #include "libavutil/intreadwrite.h"
00030 #include "thread.h"
00031 #define OPJ_STATIC
00032 #include <openjpeg.h>
00033
00034 #define JP2_SIG_TYPE 0x6A502020
00035 #define JP2_SIG_VALUE 0x0D0A870A
00036
00037 typedef struct {
00038 opj_dparameters_t dec_params;
00039 AVFrame image;
00040 } LibOpenJPEGContext;
00041
00042 static int check_image_attributes(opj_image_t *image)
00043 {
00044 return image->comps[0].dx == image->comps[1].dx &&
00045 image->comps[1].dx == image->comps[2].dx &&
00046 image->comps[0].dy == image->comps[1].dy &&
00047 image->comps[1].dy == image->comps[2].dy &&
00048 image->comps[0].prec == image->comps[1].prec &&
00049 image->comps[1].prec == image->comps[2].prec;
00050 }
00051
00052 static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
00053 {
00054 LibOpenJPEGContext *ctx = avctx->priv_data;
00055
00056 opj_set_default_decoder_parameters(&ctx->dec_params);
00057 avcodec_get_frame_defaults(&ctx->image);
00058 avctx->coded_frame = &ctx->image;
00059 return 0;
00060 }
00061
00062 static av_cold int libopenjpeg_decode_init_thread_copy(AVCodecContext *avctx)
00063 {
00064 LibOpenJPEGContext *ctx = avctx->priv_data;
00065
00066 avctx->coded_frame = &ctx->image;
00067 return 0;
00068 }
00069
00070 static int libopenjpeg_decode_frame(AVCodecContext *avctx,
00071 void *data, int *data_size,
00072 AVPacket *avpkt)
00073 {
00074 uint8_t *buf = avpkt->data;
00075 int buf_size = avpkt->size;
00076 LibOpenJPEGContext *ctx = avctx->priv_data;
00077 AVFrame *picture = &ctx->image, *output = data;
00078 opj_dinfo_t *dec;
00079 opj_cio_t *stream;
00080 opj_image_t *image;
00081 int width, height, has_alpha = 0, ret = -1;
00082 int x, y, index;
00083 uint8_t *img_ptr;
00084 int adjust[4];
00085
00086 *data_size = 0;
00087
00088
00089 if((AV_RB32(buf) == 12) &&
00090 (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
00091 (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
00092 dec = opj_create_decompress(CODEC_JP2);
00093 } else {
00094
00095
00096 if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
00097 buf += 8;
00098 dec = opj_create_decompress(CODEC_J2K);
00099 }
00100
00101 if(!dec) {
00102 av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
00103 return -1;
00104 }
00105 opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
00106
00107 ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
00108
00109 opj_setup_decoder(dec, &ctx->dec_params);
00110 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
00111 if(!stream) {
00112 av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n");
00113 opj_destroy_decompress(dec);
00114 return -1;
00115 }
00116
00117
00118 image = opj_decode_with_info(dec, stream, NULL);
00119 opj_cio_close(stream);
00120 if(!image) {
00121 av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
00122 opj_destroy_decompress(dec);
00123 return -1;
00124 }
00125 width = image->x1 - image->x0;
00126 height = image->y1 - image->y0;
00127 if(av_image_check_size(width, height, 0, avctx) < 0) {
00128 av_log(avctx, AV_LOG_ERROR, "%dx%d dimension invalid.\n", width, height);
00129 goto done;
00130 }
00131 avcodec_set_dimensions(avctx, width, height);
00132
00133 switch(image->numcomps)
00134 {
00135 case 1: avctx->pix_fmt = PIX_FMT_GRAY8;
00136 break;
00137 case 3: if(check_image_attributes(image)) {
00138 avctx->pix_fmt = PIX_FMT_RGB24;
00139 } else {
00140 avctx->pix_fmt = PIX_FMT_GRAY8;
00141 av_log(avctx, AV_LOG_ERROR, "Only first component will be used.\n");
00142 }
00143 break;
00144 case 4: has_alpha = 1;
00145 avctx->pix_fmt = PIX_FMT_RGBA;
00146 break;
00147 default: av_log(avctx, AV_LOG_ERROR, "%d components unsupported.\n", image->numcomps);
00148 goto done;
00149 }
00150
00151 if(picture->data[0])
00152 ff_thread_release_buffer(avctx, picture);
00153
00154 if(ff_thread_get_buffer(avctx, picture) < 0){
00155 av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n");
00156 return -1;
00157 }
00158
00159 ff_thread_finish_setup(avctx);
00160
00161 ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
00162 ctx->dec_params.cp_reduce = avctx->lowres;
00163
00164 opj_setup_decoder(dec, &ctx->dec_params);
00165 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
00166 if(!stream) {
00167 av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n");
00168 opj_destroy_decompress(dec);
00169 return -1;
00170 }
00171
00172
00173 image = opj_decode_with_info(dec, stream, NULL);
00174 opj_cio_close(stream);
00175
00176 for(x = 0; x < image->numcomps; x++) {
00177 adjust[x] = FFMAX(image->comps[x].prec - 8, 0);
00178 }
00179
00180 for(y = 0; y < avctx->height; y++) {
00181 index = y*avctx->width;
00182 img_ptr = picture->data[0] + y*picture->linesize[0];
00183 for(x = 0; x < avctx->width; x++, index++) {
00184 *img_ptr++ = image->comps[0].data[index] >> adjust[0];
00185 if(image->numcomps > 2 && check_image_attributes(image)) {
00186 *img_ptr++ = image->comps[1].data[index] >> adjust[1];
00187 *img_ptr++ = image->comps[2].data[index] >> adjust[2];
00188 if(has_alpha)
00189 *img_ptr++ = image->comps[3].data[index] >> adjust[3];
00190 }
00191 }
00192 }
00193
00194 *output = ctx->image;
00195 *data_size = sizeof(AVPicture);
00196 ret = buf_size;
00197
00198 done:
00199 opj_image_destroy(image);
00200 opj_destroy_decompress(dec);
00201 return ret;
00202 }
00203
00204 static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
00205 {
00206 LibOpenJPEGContext *ctx = avctx->priv_data;
00207
00208 if(ctx->image.data[0])
00209 ff_thread_release_buffer(avctx, &ctx->image);
00210 return 0 ;
00211 }
00212
00213
00214 AVCodec ff_libopenjpeg_decoder = {
00215 .name = "libopenjpeg",
00216 .type = AVMEDIA_TYPE_VIDEO,
00217 .id = CODEC_ID_JPEG2000,
00218 .priv_data_size = sizeof(LibOpenJPEGContext),
00219 .init = libopenjpeg_decode_init,
00220 .close = libopenjpeg_decode_close,
00221 .decode = libopenjpeg_decode_frame,
00222 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
00223 .max_lowres = 5,
00224 .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG based JPEG 2000 decoder"),
00225 .init_thread_copy = ONLY_IF_THREADS_ENABLED(libopenjpeg_decode_init_thread_copy)
00226 };