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