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