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