]> git.sesse.net Git - ffmpeg/blob - libavcodec/libopenjpegdec.c
Merge remote-tracking branch 'tjoppen/opatom_demuxing_and_seeking'
[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 typedef struct {
39     opj_dparameters_t dec_params;
40     AVFrame image;
41 } LibOpenJPEGContext;
42
43 static enum PixelFormat check_image_attributes(AVCodecContext *avctx, opj_image_t *image)
44 {
45     opj_image_comp_t c0 = image->comps[0];
46     opj_image_comp_t c1 = image->comps[1];
47     opj_image_comp_t c2 = image->comps[2];
48     int compRatio = 0;
49     compRatio |= c0.dx << 15 | c0.dy << 12;
50     compRatio |= c1.dx << 9  | c1.dy << 6;
51     compRatio |= c2.dx << 3  | c2.dy;
52
53     switch (compRatio) {
54     case 0111111: goto libopenjpeg_yuv444_rgb;
55     case 0112121: goto libopenjpeg_yuv422;
56     case 0112222: goto libopenjpeg_yuv420;
57     default: return PIX_FMT_RGB24;
58     }
59
60 libopenjpeg_yuv420:
61     switch (c0.prec) {
62     case 8:  return PIX_FMT_YUV420P;
63     case 9:  return PIX_FMT_YUV420P9;
64     case 10: return PIX_FMT_YUV420P10;
65     case 16: return PIX_FMT_YUV420P16;
66     }
67
68 libopenjpeg_yuv422:
69     switch (c0.prec) {
70     case 8:  return PIX_FMT_YUV422P;
71     case 9:  return PIX_FMT_YUV422P9;
72     case 10: return PIX_FMT_YUV422P10;
73     case 16: return PIX_FMT_YUV422P16;
74     }
75
76 libopenjpeg_yuv444_rgb:
77     switch (c0.prec) {
78     case 8:  return PIX_FMT_RGB24;
79     case 9:  return PIX_FMT_YUV444P9;
80     case 10: return PIX_FMT_YUV444P10;
81     case 16: return PIX_FMT_YUV444P16;
82     }
83     return PIX_FMT_RGB24;
84 }
85
86 static inline int libopenjpeg_ispacked(enum PixelFormat pix_fmt) {
87     int i, component_plane;
88     component_plane = av_pix_fmt_descriptors[pix_fmt].comp[0].plane;
89     for(i = 1; i < av_pix_fmt_descriptors[pix_fmt].nb_components; i++) {
90         if (component_plane != av_pix_fmt_descriptors[pix_fmt].comp[i].plane)
91             return 0;
92     }
93     return 1;
94 }
95
96 static inline void libopenjpeg_copy_to_packed8(AVFrame *picture, opj_image_t *image) {
97     uint8_t *img_ptr;
98     int index, x, y, c;
99     for(y = 0; y < picture->height; y++) {
100         index = y*picture->width;
101         img_ptr = picture->data[0] + y*picture->linesize[0];
102         for(x = 0; x < picture->width; x++, index++) {
103             for(c = 0; c < image->numcomps; c++) {
104                 *img_ptr++ = image->comps[c].data[index];
105             }
106         }
107     }
108 }
109
110 static inline void libopenjpeg_copyto8(AVFrame *picture, opj_image_t *image) {
111     int *comp_data;
112     uint8_t *img_ptr;
113     int index, x, y;
114
115     for(index = 0; index < image->numcomps; index++) {
116         comp_data = image->comps[index].data;
117         img_ptr = picture->data[index];
118         for(y = 0; y < image->comps[index].h; y++) {
119             for(x = 0; x < image->comps[index].w; x++) {
120                 *img_ptr = (uint8_t) *comp_data;
121                 img_ptr++;
122                 comp_data++;
123             }
124         }
125     }
126 }
127
128 static inline void libopenjpeg_copyto16(AVFrame *picture, opj_image_t *image) {
129     int *comp_data;
130     uint16_t *img_ptr;
131     int index, x, y;
132     for(index = 0; index < image->numcomps; index++) {
133         comp_data = image->comps[index].data;
134         img_ptr = (uint16_t*) picture->data[index];
135         for(y = 0; y < image->comps[index].h; y++) {
136             for(x = 0; x < image->comps[index].w; x++) {
137                 *img_ptr = *comp_data;
138                 img_ptr++;
139                 comp_data++;
140             }
141         }
142     }
143 }
144
145 static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
146 {
147     LibOpenJPEGContext *ctx = avctx->priv_data;
148
149     opj_set_default_decoder_parameters(&ctx->dec_params);
150     avcodec_get_frame_defaults(&ctx->image);
151     avctx->coded_frame = &ctx->image;
152     return 0;
153 }
154
155 static av_cold int libopenjpeg_decode_init_thread_copy(AVCodecContext *avctx)
156 {
157     LibOpenJPEGContext *ctx = avctx->priv_data;
158
159     avctx->coded_frame = &ctx->image;
160     return 0;
161 }
162
163 static int libopenjpeg_decode_frame(AVCodecContext *avctx,
164                                     void *data, int *data_size,
165                                     AVPacket *avpkt)
166 {
167     uint8_t *buf = avpkt->data;
168     int buf_size = avpkt->size;
169     LibOpenJPEGContext *ctx = avctx->priv_data;
170     AVFrame *picture = &ctx->image, *output = data;
171     opj_dinfo_t *dec;
172     opj_cio_t *stream;
173     opj_image_t *image;
174     int width, height, ret = -1;
175     int pixel_size = 0;
176     int ispacked = 0;
177
178     *data_size = 0;
179
180     // Check if input is a raw jpeg2k codestream or in jp2 wrapping
181     if((AV_RB32(buf) == 12) &&
182        (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
183        (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
184         dec = opj_create_decompress(CODEC_JP2);
185     } else {
186         // If the AVPacket contains a jp2c box, then skip to
187         // the starting byte of the codestream.
188         if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
189             buf += 8;
190         dec = opj_create_decompress(CODEC_J2K);
191     }
192
193     if(!dec) {
194         av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
195         return -1;
196     }
197     opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
198
199     ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
200     // Tie decoder with decoding parameters
201     opj_setup_decoder(dec, &ctx->dec_params);
202     stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
203     if(!stream) {
204         av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n");
205         opj_destroy_decompress(dec);
206         return -1;
207     }
208
209     // Decode the header only
210     image = opj_decode_with_info(dec, stream, NULL);
211     opj_cio_close(stream);
212     if(!image) {
213         av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
214         opj_destroy_decompress(dec);
215         return -1;
216     }
217     width  = image->x1 - image->x0;
218     height = image->y1 - image->y0;
219     if(av_image_check_size(width, height, 0, avctx) < 0) {
220         av_log(avctx, AV_LOG_ERROR, "%dx%d dimension invalid.\n", width, height);
221         goto done;
222     }
223     avcodec_set_dimensions(avctx, width, height);
224
225     switch (image->numcomps) {
226     case 1:  avctx->pix_fmt = PIX_FMT_GRAY8;
227              break;
228     case 3:  avctx->pix_fmt = check_image_attributes(avctx, image);
229              break;
230     case 4:  avctx->pix_fmt = PIX_FMT_RGBA;
231              break;
232     default: av_log(avctx, AV_LOG_ERROR, "%d components unsupported.\n", image->numcomps);
233              goto done;
234     }
235
236     if(picture->data[0])
237         ff_thread_release_buffer(avctx, picture);
238
239     if(ff_thread_get_buffer(avctx, picture) < 0){
240         av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n");
241         return -1;
242     }
243
244     ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
245     ctx->dec_params.cp_reduce = avctx->lowres;
246     // Tie decoder with decoding parameters
247     opj_setup_decoder(dec, &ctx->dec_params);
248     stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
249     if(!stream) {
250         av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n");
251         opj_destroy_decompress(dec);
252         return -1;
253     }
254
255     // Decode the codestream
256     image = opj_decode_with_info(dec, stream, NULL);
257     opj_cio_close(stream);
258
259     pixel_size = av_pix_fmt_descriptors[avctx->pix_fmt].comp[0].step_minus1 + 1;
260     ispacked = libopenjpeg_ispacked(avctx->pix_fmt);
261
262     switch (pixel_size) {
263     case 1:
264         if (ispacked) {
265             libopenjpeg_copy_to_packed8(picture, image);
266         } else {
267             libopenjpeg_copyto8(picture, image);
268         }
269         break;
270     case 2:
271         libopenjpeg_copyto16(picture, image);
272         break;
273     case 3:
274         if (ispacked) {
275             libopenjpeg_copy_to_packed8(picture, image);
276         }
277         break;
278     default:
279         av_log(avctx, AV_LOG_ERROR, "unsupported pixel size %d\n", pixel_size);
280         goto done;
281     }
282
283     *output    = ctx->image;
284     *data_size = sizeof(AVPicture);
285     ret = buf_size;
286
287 done:
288     opj_image_destroy(image);
289     opj_destroy_decompress(dec);
290     return ret;
291 }
292
293 static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
294 {
295     LibOpenJPEGContext *ctx = avctx->priv_data;
296
297     if(ctx->image.data[0])
298         ff_thread_release_buffer(avctx, &ctx->image);
299     return 0 ;
300 }
301
302
303 AVCodec ff_libopenjpeg_decoder = {
304     .name           = "libopenjpeg",
305     .type           = AVMEDIA_TYPE_VIDEO,
306     .id             = CODEC_ID_JPEG2000,
307     .priv_data_size = sizeof(LibOpenJPEGContext),
308     .init           = libopenjpeg_decode_init,
309     .close          = libopenjpeg_decode_close,
310     .decode         = libopenjpeg_decode_frame,
311     .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
312     .max_lowres     = 5,
313     .long_name      = NULL_IF_CONFIG_SMALL("OpenJPEG based JPEG 2000 decoder"),
314     .init_thread_copy = ONLY_IF_THREADS_ENABLED(libopenjpeg_decode_init_thread_copy)
315 };