]> git.sesse.net Git - ffmpeg/blob - libavcodec/libopenjpegdec.c
libopenjpeg: introduce lowres and lowqual private options
[ffmpeg] / libavcodec / libopenjpegdec.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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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/opt.h"
28 #include "libavutil/imgutils.h"
29 #include "avcodec.h"
30 #include "libavutil/intreadwrite.h"
31 #include "thread.h"
32 #define  OPJ_STATIC
33 #include <openjpeg.h>
34
35 #define JP2_SIG_TYPE    0x6A502020
36 #define JP2_SIG_VALUE   0x0D0A870A
37
38 typedef struct {
39     AVClass *class;
40     opj_dparameters_t dec_params;
41     AVFrame image;
42     int lowres;
43     int lowqual;
44 } LibOpenJPEGContext;
45
46 static int check_image_attributes(opj_image_t *image)
47 {
48     return image->comps[0].dx == image->comps[1].dx &&
49            image->comps[1].dx == image->comps[2].dx &&
50            image->comps[0].dy == image->comps[1].dy &&
51            image->comps[1].dy == image->comps[2].dy &&
52            image->comps[0].prec == image->comps[1].prec &&
53            image->comps[1].prec == image->comps[2].prec;
54 }
55
56 static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
57 {
58     LibOpenJPEGContext *ctx = avctx->priv_data;
59
60     opj_set_default_decoder_parameters(&ctx->dec_params);
61     avctx->coded_frame = &ctx->image;
62     return 0;
63 }
64
65 static av_cold int libopenjpeg_decode_init_thread_copy(AVCodecContext *avctx)
66 {
67     LibOpenJPEGContext *ctx = avctx->priv_data;
68
69     avctx->coded_frame = &ctx->image;
70     return 0;
71 }
72
73 static int libopenjpeg_decode_frame(AVCodecContext *avctx,
74                                     void *data, int *data_size,
75                                     AVPacket *avpkt)
76 {
77     uint8_t *buf = avpkt->data;
78     int buf_size = avpkt->size;
79     LibOpenJPEGContext *ctx = avctx->priv_data;
80     AVFrame *picture = &ctx->image, *output = data;
81     opj_dinfo_t *dec;
82     opj_cio_t *stream;
83     opj_image_t *image;
84     int width, height, has_alpha = 0, ret = -1;
85     int x, y, index;
86     uint8_t *img_ptr;
87     int adjust[4];
88
89     *data_size = 0;
90
91     // Check if input is a raw jpeg2k codestream or in jp2 wrapping
92     if((AV_RB32(buf) == 12) &&
93        (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
94        (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
95         dec = opj_create_decompress(CODEC_JP2);
96     } else {
97         // If the AVPacket contains a jp2c box, then skip to
98         // the starting byte of the codestream.
99         if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
100             buf += 8;
101         dec = opj_create_decompress(CODEC_J2K);
102     }
103
104     if(!dec) {
105         av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
106         return -1;
107     }
108     opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
109
110     ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
111     ctx->dec_params.cp_reduce = ctx->lowres;
112     ctx->dec_params.cp_layer  = ctx->lowqual;
113     // Tie decoder with decoding parameters
114     opj_setup_decoder(dec, &ctx->dec_params);
115     stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
116     if(!stream) {
117         av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n");
118         opj_destroy_decompress(dec);
119         return -1;
120     }
121
122     // Decode the header only
123     image = opj_decode_with_info(dec, stream, NULL);
124     opj_cio_close(stream);
125     if(!image) {
126         av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
127         opj_destroy_decompress(dec);
128         return -1;
129     }
130     width  = image->x1 - image->x0;
131     height = image->y1 - image->y0;
132
133     if (ctx->lowres) {
134         width  = (width + (1 << ctx->lowres) - 1) >> ctx->lowres;
135         height = (height + (1 << ctx->lowres) - 1) >> ctx->lowres;
136     }
137
138     if(av_image_check_size(width, height, 0, avctx) < 0) {
139         av_log(avctx, AV_LOG_ERROR, "%dx%d dimension invalid.\n", width, height);
140         goto done;
141     }
142     avcodec_set_dimensions(avctx, width, height);
143
144     switch(image->numcomps)
145     {
146         case 1:  avctx->pix_fmt = PIX_FMT_GRAY8;
147                  break;
148         case 3:  if(check_image_attributes(image)) {
149                      avctx->pix_fmt = PIX_FMT_RGB24;
150                  } else {
151                      avctx->pix_fmt = PIX_FMT_GRAY8;
152                      av_log(avctx, AV_LOG_ERROR, "Only first component will be used.\n");
153                  }
154                  break;
155         case 4:  has_alpha = 1;
156                  avctx->pix_fmt = PIX_FMT_RGBA;
157                  break;
158         default: av_log(avctx, AV_LOG_ERROR, "%d components unsupported.\n", image->numcomps);
159                  goto done;
160     }
161
162     if(picture->data[0])
163         ff_thread_release_buffer(avctx, picture);
164
165     if(ff_thread_get_buffer(avctx, picture) < 0){
166         av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n");
167         return -1;
168     }
169
170     ff_thread_finish_setup(avctx);
171
172     ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
173     // Tie decoder with decoding parameters
174     opj_setup_decoder(dec, &ctx->dec_params);
175     stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
176     if(!stream) {
177         av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n");
178         opj_destroy_decompress(dec);
179         return -1;
180     }
181
182     // Decode the codestream
183     image = opj_decode_with_info(dec, stream, NULL);
184     opj_cio_close(stream);
185
186     for(x = 0; x < image->numcomps; x++) {
187         adjust[x] = FFMAX(image->comps[x].prec - 8, 0);
188     }
189
190     for(y = 0; y < avctx->height; y++) {
191         index = y*avctx->width;
192         img_ptr = picture->data[0] + y*picture->linesize[0];
193         for(x = 0; x < avctx->width; x++, index++) {
194             *img_ptr++ = image->comps[0].data[index] >> adjust[0];
195             if(image->numcomps > 2 && check_image_attributes(image)) {
196                 *img_ptr++ = image->comps[1].data[index] >> adjust[1];
197                 *img_ptr++ = image->comps[2].data[index] >> adjust[2];
198                 if(has_alpha)
199                     *img_ptr++ = image->comps[3].data[index] >> adjust[3];
200             }
201         }
202     }
203
204     *output    = ctx->image;
205     *data_size = sizeof(AVPicture);
206     ret = buf_size;
207
208 done:
209     opj_image_destroy(image);
210     opj_destroy_decompress(dec);
211     return ret;
212 }
213
214 static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
215 {
216     LibOpenJPEGContext *ctx = avctx->priv_data;
217
218     if(ctx->image.data[0])
219         ff_thread_release_buffer(avctx, &ctx->image);
220     return 0 ;
221 }
222
223 #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
224 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
225
226 static const AVOption options[] = {
227     { "lowqual",       "Limit the number of layers used for decoding",         OFFSET(lowqual),        AV_OPT_TYPE_INT,   { 0 }, 0, INT_MAX, VD },
228     { "lowres",        "Lower the decoding resolution by a power of two",      OFFSET(lowres),         AV_OPT_TYPE_INT,   { 0 }, 0, INT_MAX, VD },
229     { NULL },
230 };
231
232 static const AVClass class = {
233     .class_name = "libopenjpeg",
234     .item_name  = av_default_item_name,
235     .option     = options,
236     .version    = LIBAVUTIL_VERSION_INT,
237 };
238
239 AVCodec ff_libopenjpeg_decoder = {
240     .name             = "libopenjpeg",
241     .type             = AVMEDIA_TYPE_VIDEO,
242     .id               = CODEC_ID_JPEG2000,
243     .priv_data_size   = sizeof(LibOpenJPEGContext),
244     .init             = libopenjpeg_decode_init,
245     .close            = libopenjpeg_decode_close,
246     .decode           = libopenjpeg_decode_frame,
247     .capabilities     = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
248     .long_name        = NULL_IF_CONFIG_SMALL("OpenJPEG based JPEG 2000 decoder"),
249     .priv_class       = &class,
250     .init_thread_copy = ONLY_IF_THREADS_ENABLED(libopenjpeg_decode_init_thread_copy),
251 };