• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/libopenjpeg.c

Go to the documentation of this file.
00001 /*
00002  * JPEG 2000 decoding support via OpenJPEG
00003  * Copyright (c) 2009 Jaikrishnan Menon <realityman@gmx.net>
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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     avctx->coded_frame = &ctx->image;
00058     return 0;
00059 }
00060 
00061 static av_cold int libopenjpeg_decode_init_thread_copy(AVCodecContext *avctx)
00062 {
00063     LibOpenJPEGContext *ctx = avctx->priv_data;
00064 
00065     avctx->coded_frame = &ctx->image;
00066     return 0;
00067 }
00068 
00069 static int libopenjpeg_decode_frame(AVCodecContext *avctx,
00070                                     void *data, int *data_size,
00071                                     AVPacket *avpkt)
00072 {
00073     uint8_t *buf = avpkt->data;
00074     int buf_size = avpkt->size;
00075     LibOpenJPEGContext *ctx = avctx->priv_data;
00076     AVFrame *picture = &ctx->image, *output = data;
00077     opj_dinfo_t *dec;
00078     opj_cio_t *stream;
00079     opj_image_t *image;
00080     int width, height, has_alpha = 0, ret = -1;
00081     int x, y, index;
00082     uint8_t *img_ptr;
00083     int adjust[4];
00084 
00085     *data_size = 0;
00086 
00087     // Check if input is a raw jpeg2k codestream or in jp2 wrapping
00088     if((AV_RB32(buf) == 12) &&
00089        (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
00090        (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
00091         dec = opj_create_decompress(CODEC_JP2);
00092     } else {
00093         // If the AVPacket contains a jp2c box, then skip to
00094         // the starting byte of the codestream.
00095         if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
00096             buf += 8;
00097         dec = opj_create_decompress(CODEC_J2K);
00098     }
00099 
00100     if(!dec) {
00101         av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
00102         return -1;
00103     }
00104     opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
00105 
00106     ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
00107     // Tie decoder with decoding parameters
00108     opj_setup_decoder(dec, &ctx->dec_params);
00109     stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
00110     if(!stream) {
00111         av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n");
00112         opj_destroy_decompress(dec);
00113         return -1;
00114     }
00115 
00116     // Decode the header only
00117     image = opj_decode_with_info(dec, stream, NULL);
00118     opj_cio_close(stream);
00119     if(!image) {
00120         av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
00121         opj_destroy_decompress(dec);
00122         return -1;
00123     }
00124     width  = image->x1 - image->x0;
00125     height = image->y1 - image->y0;
00126     if(av_image_check_size(width, height, 0, avctx) < 0) {
00127         av_log(avctx, AV_LOG_ERROR, "%dx%d dimension invalid.\n", width, height);
00128         goto done;
00129     }
00130     avcodec_set_dimensions(avctx, width, height);
00131 
00132     switch(image->numcomps)
00133     {
00134         case 1:  avctx->pix_fmt = PIX_FMT_GRAY8;
00135                  break;
00136         case 3:  if(check_image_attributes(image)) {
00137                      avctx->pix_fmt = PIX_FMT_RGB24;
00138                  } else {
00139                      avctx->pix_fmt = PIX_FMT_GRAY8;
00140                      av_log(avctx, AV_LOG_ERROR, "Only first component will be used.\n");
00141                  }
00142                  break;
00143         case 4:  has_alpha = 1;
00144                  avctx->pix_fmt = PIX_FMT_RGBA;
00145                  break;
00146         default: av_log(avctx, AV_LOG_ERROR, "%d components unsupported.\n", image->numcomps);
00147                  goto done;
00148     }
00149 
00150     if(picture->data[0])
00151         ff_thread_release_buffer(avctx, picture);
00152 
00153     if(ff_thread_get_buffer(avctx, picture) < 0){
00154         av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n");
00155         return -1;
00156     }
00157 
00158     ff_thread_finish_setup(avctx);
00159 
00160     ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
00161     ctx->dec_params.cp_reduce = avctx->lowres;
00162     // Tie decoder with decoding parameters
00163     opj_setup_decoder(dec, &ctx->dec_params);
00164     stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
00165     if(!stream) {
00166         av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n");
00167         opj_destroy_decompress(dec);
00168         return -1;
00169     }
00170 
00171     // Decode the codestream
00172     image = opj_decode_with_info(dec, stream, NULL);
00173     opj_cio_close(stream);
00174 
00175     for(x = 0; x < image->numcomps; x++) {
00176         adjust[x] = FFMAX(image->comps[x].prec - 8, 0);
00177     }
00178 
00179     for(y = 0; y < avctx->height; y++) {
00180         index = y*avctx->width;
00181         img_ptr = picture->data[0] + y*picture->linesize[0];
00182         for(x = 0; x < avctx->width; x++, index++) {
00183             *img_ptr++ = image->comps[0].data[index] >> adjust[0];
00184             if(image->numcomps > 2 && check_image_attributes(image)) {
00185                 *img_ptr++ = image->comps[1].data[index] >> adjust[1];
00186                 *img_ptr++ = image->comps[2].data[index] >> adjust[2];
00187                 if(has_alpha)
00188                     *img_ptr++ = image->comps[3].data[index] >> adjust[3];
00189             }
00190         }
00191     }
00192 
00193     *output    = ctx->image;
00194     *data_size = sizeof(AVPicture);
00195     ret = buf_size;
00196 
00197 done:
00198     opj_image_destroy(image);
00199     opj_destroy_decompress(dec);
00200     return ret;
00201 }
00202 
00203 static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
00204 {
00205     LibOpenJPEGContext *ctx = avctx->priv_data;
00206 
00207     if(ctx->image.data[0])
00208         ff_thread_release_buffer(avctx, &ctx->image);
00209     return 0 ;
00210 }
00211 
00212 
00213 AVCodec ff_libopenjpeg_decoder = {
00214     .name           = "libopenjpeg",
00215     .type           = AVMEDIA_TYPE_VIDEO,
00216     .id             = CODEC_ID_JPEG2000,
00217     .priv_data_size = sizeof(LibOpenJPEGContext),
00218     .init           = libopenjpeg_decode_init,
00219     .close          = libopenjpeg_decode_close,
00220     .decode         = libopenjpeg_decode_frame,
00221     .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
00222     .max_lowres     = 5,
00223     .long_name      = NULL_IF_CONFIG_SMALL("OpenJPEG based JPEG 2000 decoder"),
00224     .init_thread_copy = ONLY_IF_THREADS_ENABLED(libopenjpeg_decode_init_thread_copy)
00225 };
Generated on Thu Jul 11 2013 15:38:20 for Libav by doxygen 1.7.1