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