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