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 typedef struct {
00039 opj_dparameters_t dec_params;
00040 AVFrame image;
00041 } LibOpenJPEGContext;
00042
00043 static enum PixelFormat check_image_attributes(AVCodecContext *avctx, opj_image_t *image)
00044 {
00045 opj_image_comp_t c0 = image->comps[0];
00046 opj_image_comp_t c1 = image->comps[1];
00047 opj_image_comp_t c2 = image->comps[2];
00048 int compRatio = 0;
00049 compRatio |= c0.dx << 15 | c0.dy << 12;
00050 compRatio |= c1.dx << 9 | c1.dy << 6;
00051 compRatio |= c2.dx << 3 | c2.dy;
00052
00053 switch (compRatio) {
00054 case 0111111: goto libopenjpeg_yuv444_rgb;
00055 case 0111212: return PIX_FMT_YUV440P;
00056 case 0112121: goto libopenjpeg_yuv422;
00057 case 0112222: goto libopenjpeg_yuv420;
00058 default: goto libopenjpeg_rgb;
00059 }
00060
00061 libopenjpeg_yuv420:
00062 switch (c0.prec) {
00063 case 8: return PIX_FMT_YUV420P;
00064 case 9: return PIX_FMT_YUV420P9;
00065 case 10: return PIX_FMT_YUV420P10;
00066 case 16: return PIX_FMT_YUV420P16;
00067 }
00068
00069 libopenjpeg_yuv422:
00070 switch (c0.prec) {
00071 case 8: return PIX_FMT_YUV422P;
00072 case 9: return PIX_FMT_YUV422P9;
00073 case 10: return PIX_FMT_YUV422P10;
00074 case 16: return PIX_FMT_YUV422P16;
00075 }
00076
00077 libopenjpeg_yuv444_rgb:
00078 switch (c0.prec) {
00079 case 8: return PIX_FMT_RGB24;
00080 case 9: return PIX_FMT_YUV444P9;
00081 case 10: return PIX_FMT_YUV444P10;
00082 case 16: return PIX_FMT_YUV444P16;
00083 }
00084
00085 libopenjpeg_rgb:
00086 switch (c0.prec) {
00087 case 8: return PIX_FMT_RGB24;
00088 default: return PIX_FMT_RGB48;
00089 }
00090
00091 return PIX_FMT_RGB24;
00092 }
00093
00094 static int is_yuva420(opj_image_t *image)
00095 {
00096 return image->numcomps == 4 &&
00097 image->comps[0].dx == 1 && image->comps[0].dy == 1 &&
00098 image->comps[1].dx == 2 && image->comps[1].dy == 2 &&
00099 image->comps[2].dx == 2 && image->comps[2].dy == 2 &&
00100 image->comps[3].dx == 1 && image->comps[3].dy == 1;
00101 }
00102
00103 static inline int libopenjpeg_ispacked(enum PixelFormat pix_fmt) {
00104 int i, component_plane;
00105 component_plane = av_pix_fmt_descriptors[pix_fmt].comp[0].plane;
00106 for(i = 1; i < av_pix_fmt_descriptors[pix_fmt].nb_components; i++) {
00107 if (component_plane != av_pix_fmt_descriptors[pix_fmt].comp[i].plane)
00108 return 0;
00109 }
00110 return 1;
00111 }
00112
00113 static inline void libopenjpeg_copy_to_packed8(AVFrame *picture, opj_image_t *image) {
00114 uint8_t *img_ptr;
00115 int index, x, y, c;
00116 for(y = 0; y < picture->height; y++) {
00117 index = y*picture->width;
00118 img_ptr = picture->data[0] + y*picture->linesize[0];
00119 for(x = 0; x < picture->width; x++, index++) {
00120 for(c = 0; c < image->numcomps; c++) {
00121 *img_ptr++ = image->comps[c].data[index];
00122 }
00123 }
00124 }
00125 }
00126
00127 static inline void libopenjpeg_copy_to_packed16(AVFrame *picture, opj_image_t *image) {
00128 uint16_t *img_ptr;
00129 int index, x, y, c;
00130 int adjust[4];
00131 for (x = 0; x < image->numcomps; x++) {
00132 adjust[x] = FFMAX(FFMIN(16 - image->comps[x].prec, 8), 0);
00133 }
00134 for (y = 0; y < picture->height; y++) {
00135 index = y*picture->width;
00136 img_ptr = (uint16_t*) (picture->data[0] + y*picture->linesize[0]);
00137 for (x = 0; x < picture->width; x++, index++) {
00138 for (c = 0; c < image->numcomps; c++) {
00139 *img_ptr++ = image->comps[c].data[index] << adjust[c];
00140 }
00141 }
00142 }
00143 }
00144
00145 static inline void libopenjpeg_copyto8(AVFrame *picture, opj_image_t *image) {
00146 int *comp_data;
00147 uint8_t *img_ptr;
00148 int index, x, y;
00149
00150 for(index = 0; index < image->numcomps; index++) {
00151 comp_data = image->comps[index].data;
00152 for(y = 0; y < image->comps[index].h; y++) {
00153 img_ptr = picture->data[index] + y * picture->linesize[index];
00154 for(x = 0; x < image->comps[index].w; x++) {
00155 *img_ptr = (uint8_t) *comp_data;
00156 img_ptr++;
00157 comp_data++;
00158 }
00159 }
00160 }
00161 }
00162
00163 static inline void libopenjpeg_copyto16(AVFrame *picture, opj_image_t *image) {
00164 int *comp_data;
00165 uint16_t *img_ptr;
00166 int index, x, y;
00167 for(index = 0; index < image->numcomps; index++) {
00168 comp_data = image->comps[index].data;
00169 for(y = 0; y < image->comps[index].h; y++) {
00170 img_ptr = (uint16_t*) (picture->data[index] + y * picture->linesize[index]);
00171 for(x = 0; x < image->comps[index].w; x++) {
00172 *img_ptr = *comp_data;
00173 img_ptr++;
00174 comp_data++;
00175 }
00176 }
00177 }
00178 }
00179
00180 static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
00181 {
00182 LibOpenJPEGContext *ctx = avctx->priv_data;
00183
00184 opj_set_default_decoder_parameters(&ctx->dec_params);
00185 avcodec_get_frame_defaults(&ctx->image);
00186 avctx->coded_frame = &ctx->image;
00187 return 0;
00188 }
00189
00190 static av_cold int libopenjpeg_decode_init_thread_copy(AVCodecContext *avctx)
00191 {
00192 LibOpenJPEGContext *ctx = avctx->priv_data;
00193
00194 avctx->coded_frame = &ctx->image;
00195 return 0;
00196 }
00197
00198 static int libopenjpeg_decode_frame(AVCodecContext *avctx,
00199 void *data, int *data_size,
00200 AVPacket *avpkt)
00201 {
00202 uint8_t *buf = avpkt->data;
00203 int buf_size = avpkt->size;
00204 LibOpenJPEGContext *ctx = avctx->priv_data;
00205 AVFrame *picture = &ctx->image, *output = data;
00206 opj_dinfo_t *dec;
00207 opj_cio_t *stream;
00208 opj_image_t *image;
00209 int width, height, ret = -1;
00210 int pixel_size = 0;
00211 int ispacked = 0;
00212
00213 *data_size = 0;
00214
00215
00216 if((AV_RB32(buf) == 12) &&
00217 (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
00218 (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
00219 dec = opj_create_decompress(CODEC_JP2);
00220 } else {
00221
00222
00223 if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
00224 buf += 8;
00225 dec = opj_create_decompress(CODEC_J2K);
00226 }
00227
00228 if(!dec) {
00229 av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
00230 return -1;
00231 }
00232 opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
00233
00234 ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
00235
00236 opj_setup_decoder(dec, &ctx->dec_params);
00237 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
00238 if(!stream) {
00239 av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n");
00240 opj_destroy_decompress(dec);
00241 return -1;
00242 }
00243
00244
00245 image = opj_decode_with_info(dec, stream, NULL);
00246 opj_cio_close(stream);
00247 if(!image) {
00248 av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
00249 opj_destroy_decompress(dec);
00250 return -1;
00251 }
00252 width = image->x1 - image->x0;
00253 height = image->y1 - image->y0;
00254 if(av_image_check_size(width, height, 0, avctx) < 0) {
00255 av_log(avctx, AV_LOG_ERROR, "%dx%d dimension invalid.\n", width, height);
00256 goto done;
00257 }
00258 avcodec_set_dimensions(avctx, width, height);
00259
00260 switch (image->numcomps) {
00261 case 1: avctx->pix_fmt = PIX_FMT_GRAY8;
00262 break;
00263 case 3: avctx->pix_fmt = check_image_attributes(avctx, image);
00264 break;
00265 case 4: avctx->pix_fmt = is_yuva420(image) ? PIX_FMT_YUVA420P : PIX_FMT_RGBA;
00266 break;
00267 default: av_log(avctx, AV_LOG_ERROR, "%d components unsupported.\n", image->numcomps);
00268 goto done;
00269 }
00270
00271 if(picture->data[0])
00272 ff_thread_release_buffer(avctx, picture);
00273
00274 if(ff_thread_get_buffer(avctx, picture) < 0){
00275 av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n");
00276 return -1;
00277 }
00278
00279 ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
00280 ctx->dec_params.cp_reduce = avctx->lowres;
00281
00282 opj_setup_decoder(dec, &ctx->dec_params);
00283 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
00284 if(!stream) {
00285 av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n");
00286 opj_destroy_decompress(dec);
00287 return -1;
00288 }
00289
00290
00291 image = opj_decode_with_info(dec, stream, NULL);
00292 opj_cio_close(stream);
00293
00294 pixel_size = av_pix_fmt_descriptors[avctx->pix_fmt].comp[0].step_minus1 + 1;
00295 ispacked = libopenjpeg_ispacked(avctx->pix_fmt);
00296
00297 switch (pixel_size) {
00298 case 1:
00299 if (ispacked) {
00300 libopenjpeg_copy_to_packed8(picture, image);
00301 } else {
00302 libopenjpeg_copyto8(picture, image);
00303 }
00304 break;
00305 case 2:
00306 libopenjpeg_copyto16(picture, image);
00307 break;
00308 case 3:
00309 case 4:
00310 if (ispacked) {
00311 libopenjpeg_copy_to_packed8(picture, image);
00312 }
00313 break;
00314 case 6:
00315 if (ispacked) {
00316 libopenjpeg_copy_to_packed16(picture, image);
00317 }
00318 break;
00319 default:
00320 av_log(avctx, AV_LOG_ERROR, "unsupported pixel size %d\n", pixel_size);
00321 goto done;
00322 }
00323
00324 *output = ctx->image;
00325 *data_size = sizeof(AVPicture);
00326 ret = buf_size;
00327
00328 done:
00329 opj_image_destroy(image);
00330 opj_destroy_decompress(dec);
00331 return ret;
00332 }
00333
00334 static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
00335 {
00336 LibOpenJPEGContext *ctx = avctx->priv_data;
00337
00338 if(ctx->image.data[0])
00339 ff_thread_release_buffer(avctx, &ctx->image);
00340 return 0 ;
00341 }
00342
00343
00344 AVCodec ff_libopenjpeg_decoder = {
00345 .name = "libopenjpeg",
00346 .type = AVMEDIA_TYPE_VIDEO,
00347 .id = CODEC_ID_JPEG2000,
00348 .priv_data_size = sizeof(LibOpenJPEGContext),
00349 .init = libopenjpeg_decode_init,
00350 .close = libopenjpeg_decode_close,
00351 .decode = libopenjpeg_decode_frame,
00352 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
00353 .max_lowres = 5,
00354 .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG based JPEG 2000 decoder"),
00355 .init_thread_copy = ONLY_IF_THREADS_ENABLED(libopenjpeg_decode_init_thread_copy)
00356 };