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