]> git.sesse.net Git - ffmpeg/blob - libavcodec/libopenjpegdec.c
Merge commit '0a75d1da23b8659ec49391469bb592da12760077'
[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 #define  OPJ_STATIC
28 #include <openjpeg.h>
29
30 #include "libavutil/common.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/pixfmt.h"
34 #include "libavutil/opt.h"
35 #include "avcodec.h"
36 #include "thread.h"
37
38 #define JP2_SIG_TYPE    0x6A502020
39 #define JP2_SIG_VALUE   0x0D0A870A
40
41 // pix_fmts with lower bpp have to be listed before
42 // similar pix_fmts with higher bpp.
43 #define RGB_PIXEL_FORMATS   AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
44 #define GRAY_PIXEL_FORMATS  AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16
45 #define YUV_PIXEL_FORMATS   AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
46                             AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
47                             AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
48                             AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
49                             AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
50                             AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
51                             AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
52                             AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16
53
54 static const enum AVPixelFormat libopenjpeg_rgb_pix_fmts[]  = {RGB_PIXEL_FORMATS};
55 static const enum AVPixelFormat libopenjpeg_gray_pix_fmts[] = {GRAY_PIXEL_FORMATS};
56 static const enum AVPixelFormat libopenjpeg_yuv_pix_fmts[]  = {YUV_PIXEL_FORMATS};
57 static const enum AVPixelFormat libopenjpeg_all_pix_fmts[]  = {RGB_PIXEL_FORMATS,GRAY_PIXEL_FORMATS,YUV_PIXEL_FORMATS};
58
59 typedef struct {
60     AVClass *class;
61     opj_dparameters_t dec_params;
62     AVFrame image;
63     int lowqual;
64 } LibOpenJPEGContext;
65
66 static inline int libopenjpeg_matches_pix_fmt(const opj_image_t *image, enum AVPixelFormat pix_fmt)
67 {
68     AVPixFmtDescriptor descriptor = av_pix_fmt_descriptors[pix_fmt];
69     int match = 1;
70
71     if (descriptor.nb_components != image->numcomps) {
72         return 0;
73     }
74
75     switch (descriptor.nb_components) {
76     case 4: match = match && descriptor.comp[3].depth_minus1 + 1 >= image->comps[3].prec &&
77                              1 == image->comps[3].dx &&
78                              1 == image->comps[3].dy;
79     case 3: match = match && descriptor.comp[2].depth_minus1 + 1 >= image->comps[2].prec &&
80                              1 << descriptor.log2_chroma_w == image->comps[2].dx &&
81                              1 << descriptor.log2_chroma_h == image->comps[2].dy;
82     case 2: match = match && descriptor.comp[1].depth_minus1 + 1 >= image->comps[1].prec &&
83                              1 << descriptor.log2_chroma_w == image->comps[1].dx &&
84                              1 << descriptor.log2_chroma_h == image->comps[1].dy;
85     case 1: match = match && descriptor.comp[0].depth_minus1 + 1 >= image->comps[0].prec &&
86                              1 == image->comps[0].dx &&
87                              1 == image->comps[0].dy;
88     default:
89         break;
90     }
91
92     return match;
93 }
94
95 static inline enum AVPixelFormat libopenjpeg_guess_pix_fmt(const opj_image_t *image) {
96     int index;
97     const enum AVPixelFormat *possible_fmts = NULL;
98     int possible_fmts_nb = 0;
99
100     switch (image->color_space) {
101     case CLRSPC_SRGB:
102         possible_fmts = libopenjpeg_rgb_pix_fmts;
103         possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_rgb_pix_fmts);
104         break;
105     case CLRSPC_GRAY:
106         possible_fmts = libopenjpeg_gray_pix_fmts;
107         possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_gray_pix_fmts);
108         break;
109     case CLRSPC_SYCC:
110         possible_fmts = libopenjpeg_yuv_pix_fmts;
111         possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_yuv_pix_fmts);
112         break;
113     default:
114         possible_fmts = libopenjpeg_all_pix_fmts;
115         possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_all_pix_fmts);
116         break;
117     }
118
119     for (index = 0; index < possible_fmts_nb; ++index) {
120         if (libopenjpeg_matches_pix_fmt(image, possible_fmts[index])) {
121             return possible_fmts[index];
122         }
123     }
124
125     return AV_PIX_FMT_NONE;
126 }
127
128 static inline int libopenjpeg_ispacked(enum AVPixelFormat pix_fmt) {
129     int i, component_plane;
130
131     if (pix_fmt == AV_PIX_FMT_GRAY16)
132         return 0;
133
134     component_plane = av_pix_fmt_descriptors[pix_fmt].comp[0].plane;
135     for (i = 1; i < av_pix_fmt_descriptors[pix_fmt].nb_components; i++) {
136         if (component_plane != av_pix_fmt_descriptors[pix_fmt].comp[i].plane)
137             return 0;
138     }
139     return 1;
140 }
141
142 static inline void libopenjpeg_copy_to_packed8(AVFrame *picture, opj_image_t *image) {
143     uint8_t *img_ptr;
144     int index, x, y, c;
145     for (y = 0; y < picture->height; y++) {
146         index = y*picture->width;
147         img_ptr = picture->data[0] + y*picture->linesize[0];
148         for (x = 0; x < picture->width; x++, index++) {
149             for (c = 0; c < image->numcomps; c++) {
150                 *img_ptr++ = image->comps[c].data[index];
151             }
152         }
153     }
154 }
155
156 static inline void libopenjpeg_copy_to_packed16(AVFrame *picture, opj_image_t *image) {
157     uint16_t *img_ptr;
158     int index, x, y, c;
159     int adjust[4];
160     for (x = 0; x < image->numcomps; x++)
161         adjust[x] = FFMAX(FFMIN(16 - image->comps[x].prec, 8), 0);
162
163     for (y = 0; y < picture->height; y++) {
164         index = y*picture->width;
165         img_ptr = (uint16_t*) (picture->data[0] + y*picture->linesize[0]);
166         for (x = 0; x < picture->width; x++, index++) {
167             for (c = 0; c < image->numcomps; c++) {
168                 *img_ptr++ = image->comps[c].data[index] << adjust[c];
169             }
170         }
171     }
172 }
173
174 static inline void libopenjpeg_copyto8(AVFrame *picture, opj_image_t *image) {
175     int *comp_data;
176     uint8_t *img_ptr;
177     int index, x, y;
178
179     for (index = 0; index < image->numcomps; index++) {
180         comp_data = image->comps[index].data;
181         for (y = 0; y < image->comps[index].h; y++) {
182             img_ptr = picture->data[index] + y * picture->linesize[index];
183             for (x = 0; x < image->comps[index].w; x++) {
184                 *img_ptr = (uint8_t) *comp_data;
185                 img_ptr++;
186                 comp_data++;
187             }
188         }
189     }
190 }
191
192 static inline void libopenjpeg_copyto16(AVFrame *picture, opj_image_t *image) {
193     int *comp_data;
194     uint16_t *img_ptr;
195     int index, x, y;
196     for (index = 0; index < image->numcomps; index++) {
197         comp_data = image->comps[index].data;
198         for (y = 0; y < image->comps[index].h; y++) {
199             img_ptr = (uint16_t*) (picture->data[index] + y * picture->linesize[index]);
200             for (x = 0; x < image->comps[index].w; x++) {
201                 *img_ptr = *comp_data;
202                 img_ptr++;
203                 comp_data++;
204             }
205         }
206     }
207 }
208
209 static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
210 {
211     LibOpenJPEGContext *ctx = avctx->priv_data;
212
213     opj_set_default_decoder_parameters(&ctx->dec_params);
214     avcodec_get_frame_defaults(&ctx->image);
215     avctx->coded_frame = &ctx->image;
216     return 0;
217 }
218
219 static av_cold int libopenjpeg_decode_init_thread_copy(AVCodecContext *avctx)
220 {
221     LibOpenJPEGContext *ctx = avctx->priv_data;
222
223     avctx->coded_frame = &ctx->image;
224     return 0;
225 }
226
227 static int libopenjpeg_decode_frame(AVCodecContext *avctx,
228                                     void *data, int *data_size,
229                                     AVPacket *avpkt)
230 {
231     uint8_t *buf = avpkt->data;
232     int buf_size = avpkt->size;
233     LibOpenJPEGContext *ctx = avctx->priv_data;
234     AVFrame *picture = &ctx->image, *output = data;
235     opj_dinfo_t *dec;
236     opj_cio_t *stream;
237     opj_image_t *image;
238     int width, height, ret = -1;
239     int pixel_size = 0;
240     int ispacked = 0;
241     int i;
242
243     *data_size = 0;
244
245     // Check if input is a raw jpeg2k codestream or in jp2 wrapping
246     if ((AV_RB32(buf)     == 12)           &&
247         (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
248         (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
249         dec = opj_create_decompress(CODEC_JP2);
250     } else {
251         /* If the AVPacket contains a jp2c box, then skip to
252          * the starting byte of the codestream. */
253         if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
254             buf += 8;
255         dec = opj_create_decompress(CODEC_J2K);
256     }
257
258     if (!dec) {
259         av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
260         return -1;
261     }
262     opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
263     ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
264     ctx->dec_params.cp_layer          = ctx->lowqual;
265     // Tie decoder with decoding parameters
266     opj_setup_decoder(dec, &ctx->dec_params);
267     stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
268
269     if (!stream) {
270         av_log(avctx, AV_LOG_ERROR,
271                "Codestream could not be opened for reading.\n");
272         opj_destroy_decompress(dec);
273         return -1;
274     }
275
276     // Decode the header only.
277     image = opj_decode_with_info(dec, stream, NULL);
278     opj_cio_close(stream);
279
280     if (!image) {
281         av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
282         opj_destroy_decompress(dec);
283         return -1;
284     }
285
286     width  = image->x1 - image->x0;
287     height = image->y1 - image->y0;
288
289     if (av_image_check_size(width, height, 0, avctx) < 0) {
290         av_log(avctx, AV_LOG_ERROR,
291                "%dx%d dimension invalid.\n", width, height);
292         goto done;
293     }
294
295     avcodec_set_dimensions(avctx, width, height);
296
297     if (avctx->pix_fmt != AV_PIX_FMT_NONE)
298         if (!libopenjpeg_matches_pix_fmt(image, avctx->pix_fmt))
299             avctx->pix_fmt = AV_PIX_FMT_NONE;
300
301     if (avctx->pix_fmt == AV_PIX_FMT_NONE)
302         avctx->pix_fmt = libopenjpeg_guess_pix_fmt(image);
303
304     if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
305         av_log(avctx, AV_LOG_ERROR, "Unable to determine pixel format\n");
306         goto done;
307     }
308     for (i = 0; i < image->numcomps; i++)
309         if (image->comps[i].prec > avctx->bits_per_raw_sample)
310             avctx->bits_per_raw_sample = image->comps[i].prec;
311
312     if (picture->data[0])
313         ff_thread_release_buffer(avctx, picture);
314
315     if (ff_thread_get_buffer(avctx, picture) < 0) {
316         av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n");
317         goto done;
318     }
319
320     ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
321     ctx->dec_params.cp_reduce = avctx->lowres;
322     // Tie decoder with decoding parameters.
323     opj_setup_decoder(dec, &ctx->dec_params);
324     stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
325     if (!stream) {
326         av_log(avctx, AV_LOG_ERROR,
327                "Codestream could not be opened for reading.\n");
328         goto done;
329     }
330
331     opj_image_destroy(image);
332     // Decode the codestream
333     image = opj_decode_with_info(dec, stream, NULL);
334     opj_cio_close(stream);
335
336     if (!image) {
337         av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
338         goto done;
339     }
340
341     pixel_size = av_pix_fmt_descriptors[avctx->pix_fmt].comp[0].step_minus1 + 1;
342     ispacked = libopenjpeg_ispacked(avctx->pix_fmt);
343
344     switch (pixel_size) {
345     case 1:
346         if (ispacked) {
347             libopenjpeg_copy_to_packed8(picture, image);
348         } else {
349             libopenjpeg_copyto8(picture, image);
350         }
351         break;
352     case 2:
353         if (ispacked) {
354             libopenjpeg_copy_to_packed8(picture, image);
355         } else {
356             libopenjpeg_copyto16(picture, image);
357         }
358         break;
359     case 3:
360     case 4:
361         if (ispacked) {
362             libopenjpeg_copy_to_packed8(picture, image);
363         }
364         break;
365     case 6:
366     case 8:
367         if (ispacked) {
368             libopenjpeg_copy_to_packed16(picture, image);
369         }
370         break;
371     default:
372         av_log(avctx, AV_LOG_ERROR, "unsupported pixel size %d\n", pixel_size);
373         goto done;
374     }
375
376     *output    = ctx->image;
377     *data_size = sizeof(AVPicture);
378     ret        = buf_size;
379
380 done:
381     opj_image_destroy(image);
382     opj_destroy_decompress(dec);
383     return ret;
384 }
385
386 static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
387 {
388     LibOpenJPEGContext *ctx = avctx->priv_data;
389
390     if (ctx->image.data[0])
391         ff_thread_release_buffer(avctx, &ctx->image);
392     return 0;
393 }
394
395 #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
396 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
397
398 static const AVOption options[] = {
399     { "lowqual", "Limit the number of layers used for decoding",    OFFSET(lowqual), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VD },
400     { NULL },
401 };
402
403 static const AVClass class = {
404     .class_name = "libopenjpeg",
405     .item_name  = av_default_item_name,
406     .option     = options,
407     .version    = LIBAVUTIL_VERSION_INT,
408 };
409
410 AVCodec ff_libopenjpeg_decoder = {
411     .name             = "libopenjpeg",
412     .type             = AVMEDIA_TYPE_VIDEO,
413     .id               = AV_CODEC_ID_JPEG2000,
414     .priv_data_size   = sizeof(LibOpenJPEGContext),
415     .init             = libopenjpeg_decode_init,
416     .close            = libopenjpeg_decode_close,
417     .decode           = libopenjpeg_decode_frame,
418     .capabilities     = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
419     .max_lowres       = 31,
420     .long_name        = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
421     .priv_class       = &class,
422     .init_thread_copy = ONLY_IF_THREADS_ENABLED(libopenjpeg_decode_init_thread_copy),
423 };