]> git.sesse.net Git - ffmpeg/blob - libavcodec/libopenjpegdec.c
Merge remote-tracking branch 'qatar/master'
[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 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 "libavutil/pixfmt.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     opj_dparameters_t dec_params;
40     AVFrame image;
41 } LibOpenJPEGContext;
42
43 static enum PixelFormat check_image_attributes(AVCodecContext *avctx, opj_image_t *image)
44 {
45     opj_image_comp_t c0 = image->comps[0];
46     opj_image_comp_t c1 = image->comps[1];
47     opj_image_comp_t c2 = image->comps[2];
48     int compRatio = 0;
49     compRatio |= c0.dx << 15 | c0.dy << 12;
50     compRatio |= c1.dx << 9  | c1.dy << 6;
51     compRatio |= c2.dx << 3  | c2.dy;
52
53     switch (compRatio) {
54     case 0111111: goto libopenjpeg_yuv444_rgb;
55     case 0112121: goto libopenjpeg_yuv422;
56     case 0112222: goto libopenjpeg_yuv420;
57     default: goto libopenjpeg_rgb;
58     }
59
60 libopenjpeg_yuv420:
61     switch (c0.prec) {
62     case 8:  return PIX_FMT_YUV420P;
63     case 9:  return PIX_FMT_YUV420P9;
64     case 10: return PIX_FMT_YUV420P10;
65     case 16: return PIX_FMT_YUV420P16;
66     }
67
68 libopenjpeg_yuv422:
69     switch (c0.prec) {
70     case 8:  return PIX_FMT_YUV422P;
71     case 9:  return PIX_FMT_YUV422P9;
72     case 10: return PIX_FMT_YUV422P10;
73     case 16: return PIX_FMT_YUV422P16;
74     }
75
76 libopenjpeg_yuv444_rgb:
77     switch (c0.prec) {
78     case 8:  return PIX_FMT_RGB24;
79     case 9:  return PIX_FMT_YUV444P9;
80     case 10: return PIX_FMT_YUV444P10;
81     case 16: return PIX_FMT_YUV444P16;
82     }
83
84 libopenjpeg_rgb:
85     switch (c0.prec) {
86     case 8: return PIX_FMT_RGB24;
87     default: return PIX_FMT_RGB48;
88     }
89
90     return PIX_FMT_RGB24;
91 }
92
93 static inline int libopenjpeg_ispacked(enum PixelFormat pix_fmt) {
94     int i, component_plane;
95     component_plane = av_pix_fmt_descriptors[pix_fmt].comp[0].plane;
96     for(i = 1; i < av_pix_fmt_descriptors[pix_fmt].nb_components; i++) {
97         if (component_plane != av_pix_fmt_descriptors[pix_fmt].comp[i].plane)
98             return 0;
99     }
100     return 1;
101 }
102
103 static inline void libopenjpeg_copy_to_packed8(AVFrame *picture, opj_image_t *image) {
104     uint8_t *img_ptr;
105     int index, x, y, c;
106     for(y = 0; y < picture->height; y++) {
107         index = y*picture->width;
108         img_ptr = picture->data[0] + y*picture->linesize[0];
109         for(x = 0; x < picture->width; x++, index++) {
110             for(c = 0; c < image->numcomps; c++) {
111                 *img_ptr++ = image->comps[c].data[index];
112             }
113         }
114     }
115 }
116
117 static inline void libopenjpeg_copy_to_packed16(AVFrame *picture, opj_image_t *image) {
118     uint16_t *img_ptr;
119     int index, x, y, c;
120     int adjust[4];
121     for (x = 0; x < image->numcomps; x++) {
122         adjust[x] = FFMAX(FFMIN(16 - image->comps[x].prec, 8), 0);
123     }
124     for (y = 0; y < picture->height; y++) {
125         index = y*picture->width;
126         img_ptr = (uint16_t*) (picture->data[0] + y*picture->linesize[0]);
127         for (x = 0; x < picture->width; x++, index++) {
128             for (c = 0; c < image->numcomps; c++) {
129                 *img_ptr++ = image->comps[c].data[index] << adjust[c];
130             }
131         }
132     }
133 }
134
135 static inline void libopenjpeg_copyto8(AVFrame *picture, opj_image_t *image) {
136     int *comp_data;
137     uint8_t *img_ptr;
138     int index, x, y;
139
140     for(index = 0; index < image->numcomps; index++) {
141         comp_data = image->comps[index].data;
142         img_ptr = picture->data[index];
143         for(y = 0; y < image->comps[index].h; y++) {
144             for(x = 0; x < image->comps[index].w; x++) {
145                 *img_ptr = (uint8_t) *comp_data;
146                 img_ptr++;
147                 comp_data++;
148             }
149         }
150     }
151 }
152
153 static inline void libopenjpeg_copyto16(AVFrame *picture, opj_image_t *image) {
154     int *comp_data;
155     uint16_t *img_ptr;
156     int index, x, y;
157     for(index = 0; index < image->numcomps; index++) {
158         comp_data = image->comps[index].data;
159         img_ptr = (uint16_t*) picture->data[index];
160         for(y = 0; y < image->comps[index].h; y++) {
161             for(x = 0; x < image->comps[index].w; x++) {
162                 *img_ptr = *comp_data;
163                 img_ptr++;
164                 comp_data++;
165             }
166         }
167     }
168 }
169
170 static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
171 {
172     LibOpenJPEGContext *ctx = avctx->priv_data;
173
174     opj_set_default_decoder_parameters(&ctx->dec_params);
175     avcodec_get_frame_defaults(&ctx->image);
176     avctx->coded_frame = &ctx->image;
177     return 0;
178 }
179
180 static av_cold int libopenjpeg_decode_init_thread_copy(AVCodecContext *avctx)
181 {
182     LibOpenJPEGContext *ctx = avctx->priv_data;
183
184     avctx->coded_frame = &ctx->image;
185     return 0;
186 }
187
188 static int libopenjpeg_decode_frame(AVCodecContext *avctx,
189                                     void *data, int *data_size,
190                                     AVPacket *avpkt)
191 {
192     uint8_t *buf = avpkt->data;
193     int buf_size = avpkt->size;
194     LibOpenJPEGContext *ctx = avctx->priv_data;
195     AVFrame *picture = &ctx->image, *output = data;
196     opj_dinfo_t *dec;
197     opj_cio_t *stream;
198     opj_image_t *image;
199     int width, height, ret = -1;
200     int pixel_size = 0;
201     int ispacked = 0;
202
203     *data_size = 0;
204
205     // Check if input is a raw jpeg2k codestream or in jp2 wrapping
206     if((AV_RB32(buf) == 12) &&
207        (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
208        (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
209         dec = opj_create_decompress(CODEC_JP2);
210     } else {
211         // If the AVPacket contains a jp2c box, then skip to
212         // the starting byte of the codestream.
213         if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
214             buf += 8;
215         dec = opj_create_decompress(CODEC_J2K);
216     }
217
218     if(!dec) {
219         av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
220         return -1;
221     }
222     opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
223
224     ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
225     // Tie decoder with decoding parameters
226     opj_setup_decoder(dec, &ctx->dec_params);
227     stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
228     if(!stream) {
229         av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n");
230         opj_destroy_decompress(dec);
231         return -1;
232     }
233
234     // Decode the header only
235     image = opj_decode_with_info(dec, stream, NULL);
236     opj_cio_close(stream);
237     if(!image) {
238         av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
239         opj_destroy_decompress(dec);
240         return -1;
241     }
242     width  = image->x1 - image->x0;
243     height = image->y1 - image->y0;
244     if(av_image_check_size(width, height, 0, avctx) < 0) {
245         av_log(avctx, AV_LOG_ERROR, "%dx%d dimension invalid.\n", width, height);
246         goto done;
247     }
248     avcodec_set_dimensions(avctx, width, height);
249
250     switch (image->numcomps) {
251     case 1:  avctx->pix_fmt = PIX_FMT_GRAY8;
252              break;
253     case 3:  avctx->pix_fmt = check_image_attributes(avctx, image);
254              break;
255     case 4:  avctx->pix_fmt = PIX_FMT_RGBA;
256              break;
257     default: av_log(avctx, AV_LOG_ERROR, "%d components unsupported.\n", image->numcomps);
258              goto done;
259     }
260
261     if(picture->data[0])
262         ff_thread_release_buffer(avctx, picture);
263
264     if(ff_thread_get_buffer(avctx, picture) < 0){
265         av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n");
266         return -1;
267     }
268
269     ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
270     ctx->dec_params.cp_reduce = avctx->lowres;
271     // Tie decoder with decoding parameters
272     opj_setup_decoder(dec, &ctx->dec_params);
273     stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
274     if(!stream) {
275         av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n");
276         opj_destroy_decompress(dec);
277         return -1;
278     }
279
280     // Decode the codestream
281     image = opj_decode_with_info(dec, stream, NULL);
282     opj_cio_close(stream);
283
284     pixel_size = av_pix_fmt_descriptors[avctx->pix_fmt].comp[0].step_minus1 + 1;
285     ispacked = libopenjpeg_ispacked(avctx->pix_fmt);
286
287     switch (pixel_size) {
288     case 1:
289         if (ispacked) {
290             libopenjpeg_copy_to_packed8(picture, image);
291         } else {
292             libopenjpeg_copyto8(picture, image);
293         }
294         break;
295     case 2:
296         libopenjpeg_copyto16(picture, image);
297         break;
298     case 3:
299         if (ispacked) {
300             libopenjpeg_copy_to_packed8(picture, image);
301         }
302         break;
303     case 6:
304         if (ispacked) {
305             libopenjpeg_copy_to_packed16(picture, image);
306         }
307         break;
308     default:
309         av_log(avctx, AV_LOG_ERROR, "unsupported pixel size %d\n", pixel_size);
310         goto done;
311     }
312
313     *output    = ctx->image;
314     *data_size = sizeof(AVPicture);
315     ret = buf_size;
316
317 done:
318     opj_image_destroy(image);
319     opj_destroy_decompress(dec);
320     return ret;
321 }
322
323 static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
324 {
325     LibOpenJPEGContext *ctx = avctx->priv_data;
326
327     if(ctx->image.data[0])
328         ff_thread_release_buffer(avctx, &ctx->image);
329     return 0 ;
330 }
331
332
333 AVCodec ff_libopenjpeg_decoder = {
334     .name           = "libopenjpeg",
335     .type           = AVMEDIA_TYPE_VIDEO,
336     .id             = CODEC_ID_JPEG2000,
337     .priv_data_size = sizeof(LibOpenJPEGContext),
338     .init           = libopenjpeg_decode_init,
339     .close          = libopenjpeg_decode_close,
340     .decode         = libopenjpeg_decode_frame,
341     .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
342     .max_lowres     = 5,
343     .long_name      = NULL_IF_CONFIG_SMALL("OpenJPEG based JPEG 2000 decoder"),
344     .init_thread_copy = ONLY_IF_THREADS_ENABLED(libopenjpeg_decode_init_thread_copy)
345 };