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