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