]> git.sesse.net Git - ffmpeg/blob - libavcodec/libopenjpeg.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / libopenjpeg.c
1 /*
2  * JPEG 2000 decoding support via OpenJPEG
3  * Copyright (c) 2009 Jaikrishnan Menon <realityman@gmx.net>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23 * @file
24 * JPEG 2000 decoder using libopenjpeg
25 */
26
27 #include "libavutil/imgutils.h"
28 #include "avcodec.h"
29 #include "libavutil/intreadwrite.h"
30 #define  OPJ_STATIC
31 #include <openjpeg.h>
32
33 #define JP2_SIG_TYPE    0x6A502020
34 #define JP2_SIG_VALUE   0x0D0A870A
35
36 typedef struct {
37     opj_dparameters_t dec_params;
38     AVFrame image;
39 } LibOpenJPEGContext;
40
41 static int check_image_attributes(opj_image_t *image)
42 {
43     return image->comps[0].dx == image->comps[1].dx &&
44            image->comps[1].dx == image->comps[2].dx &&
45            image->comps[0].dy == image->comps[1].dy &&
46            image->comps[1].dy == image->comps[2].dy &&
47            image->comps[0].prec == image->comps[1].prec &&
48            image->comps[1].prec == image->comps[2].prec;
49 }
50
51 static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
52 {
53     LibOpenJPEGContext *ctx = avctx->priv_data;
54
55     opj_set_default_decoder_parameters(&ctx->dec_params);
56     avcodec_get_frame_defaults(&ctx->image);
57     avctx->coded_frame = &ctx->image;
58     return 0;
59 }
60
61 static int libopenjpeg_decode_frame(AVCodecContext *avctx,
62                                     void *data, int *data_size,
63                                     AVPacket *avpkt)
64 {
65     uint8_t *buf = avpkt->data;
66     int buf_size = avpkt->size;
67     LibOpenJPEGContext *ctx = avctx->priv_data;
68     AVFrame *picture = &ctx->image, *output = data;
69     opj_dinfo_t *dec;
70     opj_cio_t *stream;
71     opj_image_t *image;
72     int width, height, has_alpha = 0, ret = -1;
73     int x, y, index;
74     uint8_t *img_ptr;
75     int adjust[4];
76
77     *data_size = 0;
78
79     // Check if input is a raw jpeg2k codestream or in jp2 wrapping
80     if((AV_RB32(buf) == 12) &&
81        (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
82        (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
83         dec = opj_create_decompress(CODEC_JP2);
84     } else {
85         // If the AVPacket contains a jp2c box, then skip to
86         // the starting byte of the codestream.
87         if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
88             buf += 8;
89         dec = opj_create_decompress(CODEC_J2K);
90     }
91
92     if(!dec) {
93         av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
94         return -1;
95     }
96     opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
97
98     ctx->dec_params.cp_reduce = avctx->lowres;
99     // Tie decoder with decoding parameters
100     opj_setup_decoder(dec, &ctx->dec_params);
101     stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
102     if(!stream) {
103         av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n");
104         opj_destroy_decompress(dec);
105         return -1;
106     }
107
108     // Decode the codestream
109     image = opj_decode_with_info(dec, stream, NULL);
110     opj_cio_close(stream);
111     if(!image) {
112         av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
113         opj_destroy_decompress(dec);
114         return -1;
115     }
116     width  = image->comps[0].w << avctx->lowres;
117     height = image->comps[0].h << avctx->lowres;
118     if(av_image_check_size(width, height, 0, avctx) < 0) {
119         av_log(avctx, AV_LOG_ERROR, "%dx%d dimension invalid.\n", width, height);
120         goto done;
121     }
122     avcodec_set_dimensions(avctx, width, height);
123
124     switch(image->numcomps)
125     {
126         case 1:  avctx->pix_fmt = PIX_FMT_GRAY8;
127                  break;
128         case 3:  if(check_image_attributes(image)) {
129                      avctx->pix_fmt = PIX_FMT_RGB24;
130                  } else {
131                      avctx->pix_fmt = PIX_FMT_GRAY8;
132                      av_log(avctx, AV_LOG_ERROR, "Only first component will be used.\n");
133                  }
134                  break;
135         case 4:  has_alpha = 1;
136                  avctx->pix_fmt = PIX_FMT_RGBA;
137                  break;
138         default: av_log(avctx, AV_LOG_ERROR, "%d components unsupported.\n", image->numcomps);
139                  goto done;
140     }
141
142     if(picture->data[0])
143         avctx->release_buffer(avctx, picture);
144
145     if(avctx->get_buffer(avctx, picture) < 0) {
146         av_log(avctx, AV_LOG_ERROR, "Couldn't allocate image buffer.\n");
147         return -1;
148     }
149
150     for(x = 0; x < image->numcomps; x++) {
151         adjust[x] = FFMAX(image->comps[x].prec - 8, 0);
152     }
153
154     for(y = 0; y < avctx->height; y++) {
155         index = y*avctx->width;
156         img_ptr = picture->data[0] + y*picture->linesize[0];
157         for(x = 0; x < avctx->width; x++, index++) {
158             *img_ptr++ = image->comps[0].data[index] >> adjust[0];
159             if(image->numcomps > 2 && check_image_attributes(image)) {
160                 *img_ptr++ = image->comps[1].data[index] >> adjust[1];
161                 *img_ptr++ = image->comps[2].data[index] >> adjust[2];
162                 if(has_alpha)
163                     *img_ptr++ = image->comps[3].data[index] >> adjust[3];
164             }
165         }
166     }
167
168     *output    = ctx->image;
169     *data_size = sizeof(AVPicture);
170     ret = buf_size;
171
172 done:
173     opj_image_destroy(image);
174     opj_destroy_decompress(dec);
175     return ret;
176 }
177
178 static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
179 {
180     LibOpenJPEGContext *ctx = avctx->priv_data;
181
182     if(ctx->image.data[0])
183         avctx->release_buffer(avctx, &ctx->image);
184     return 0 ;
185 }
186
187
188 AVCodec ff_libopenjpeg_decoder = {
189     .name           = "libopenjpeg",
190     .type           = AVMEDIA_TYPE_VIDEO,
191     .id             = CODEC_ID_JPEG2000,
192     .priv_data_size = sizeof(LibOpenJPEGContext),
193     .init           = libopenjpeg_decode_init,
194     .close          = libopenjpeg_decode_close,
195     .decode         = libopenjpeg_decode_frame,
196     .capabilities   = CODEC_CAP_DR1,
197     .max_lowres     = 5,
198     .long_name      = NULL_IF_CONFIG_SMALL("OpenJPEG based JPEG 2000 decoder"),
199 };