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