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