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
32
37
38 #include <openjpeg.h>
39
40 #define JP2_SIG_TYPE 0x6A502020
41 #define JP2_SIG_VALUE 0x0D0A870A
42
43 // pix_fmts with lower bpp have to be listed before
44 // similar pix_fmts with higher bpp.
45 #define RGB_PIXEL_FORMATS AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA, \
46 AV_PIX_FMT_RGB48, AV_PIX_FMT_RGBA64
47
48 #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8, AV_PIX_FMT_YA8, \
49 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, \
50 AV_PIX_FMT_GRAY16, AV_PIX_FMT_YA16
51
52 #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUVA420P, \
53 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA422P, \
54 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, \
55 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, \
56 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9, \
57 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, \
58 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10, \
59 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, \
60 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14, \
61 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, \
62 AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16
63
64 #define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
65
68 };
71 };
74 };
77 };
78
84
86 {
88 }
89
91 {
93 }
94
96 {
98 }
99
105
107 {
109 int remaining;
110
111 if (reader->
pos == reader->
size) {
112 return (OPJ_SIZE_T)-1;
113 }
114 remaining = reader->
size - reader->
pos;
115 if (nb_bytes > remaining) {
116 nb_bytes = remaining;
117 }
118 memcpy(out_buffer, reader->
buffer + reader->
pos, nb_bytes);
119 reader->
pos += (
int)nb_bytes;
120 return nb_bytes;
121 }
122
124 {
126 if (nb_bytes < 0) {
127 if (reader->
pos == 0) {
128 return (OPJ_SIZE_T)-1;
129 }
130 if (nb_bytes + reader->
pos < 0) {
131 nb_bytes = -reader->
pos;
132 }
133 } else {
134 int remaining;
135
136 if (reader->
pos == reader->
size) {
137 return (OPJ_SIZE_T)-1;
138 }
139 remaining = reader->
size - reader->
pos;
140 if (nb_bytes > remaining) {
141 nb_bytes = remaining;
142 }
143 }
144 reader->
pos += (
int)nb_bytes;
145 return nb_bytes;
146 }
147
149 {
151 if (nb_bytes < 0 || nb_bytes > reader->
size) {
152 return OPJ_FALSE;
153 }
154 reader->
pos = (
int)nb_bytes;
155 return OPJ_TRUE;
156 }
157
159 {
161 int match = 1;
162
163 if (
desc->nb_components != image->numcomps) {
164 return 0;
165 }
166
167 switch (
desc->nb_components) {
168 case 4:
169 match = match &&
170 desc->comp[3].depth >= image->comps[3].prec &&
171 1 == image->comps[3].dx &&
172 1 == image->comps[3].dy;
173 case 3:
174 match = match &&
175 desc->comp[2].depth >= image->comps[2].prec &&
176 1 <<
desc->log2_chroma_w == image->comps[2].dx &&
177 1 <<
desc->log2_chroma_h == image->comps[2].dy;
178 case 2:
179 match = match &&
180 desc->comp[1].depth >= image->comps[1].prec &&
181 1 <<
desc->log2_chroma_w == image->comps[1].dx &&
182 1 <<
desc->log2_chroma_h == image->comps[1].dy;
183 case 1:
184 match = match &&
185 desc->comp[0].depth >= image->comps[0].prec &&
186 1 == image->comps[0].dx &&
187 1 == image->comps[0].dy;
188 default:
189 break;
190 }
191
192 return match;
193 }
194
198 int possible_fmts_nb = 0;
199
200 switch (image->color_space) {
201 case OPJ_CLRSPC_SRGB:
204 break;
205 case OPJ_CLRSPC_GRAY:
208 break;
209 case OPJ_CLRSPC_SYCC:
212 break;
213 default:
216 break;
217 }
218
221 return possible_fmts[
index];
222 }
223
225 }
226
228 {
230 int i, component_plane;
231
233 return 0;
234
235 component_plane =
desc->comp[0].plane;
236 for (
i = 1;
i <
desc->nb_components;
i++)
237 if (component_plane !=
desc->comp[
i].plane)
238 return 0;
239 return 1;
240 }
241
243 uint8_t *img_ptr;
245 for (y = 0; y < picture->
height; y++) {
249 for (
c = 0;
c < image->numcomps;
c++)
250 *img_ptr++ = 0x80 * image->comps[
c].sgnd + image->comps[
c].data[
index];
251 }
252 }
253
255 uint16_t *img_ptr;
259 for (x = 0; x < image->numcomps; x++)
261
262 for (y = 0; y < picture->
height; y++) {
264 img_ptr = (uint16_t *) (picture->
data[0] + y * picture->
linesize[0]);
266 for (
c = 0;
c < image->numcomps;
c++)
267 *img_ptr++ = (1 << image->comps[
c].prec - 1) * image->comps[
c].sgnd +
269 }
270 }
271
273 int *comp_data;
274 uint8_t *img_ptr;
276
278 comp_data = image->comps[
index].data;
279 for (y = 0; y < image->comps[
index].h; y++) {
281 for (x = 0; x < image->comps[
index].w; x++) {
282 *img_ptr = 0x80 * image->comps[
index].sgnd + *comp_data;
283 img_ptr++;
284 comp_data++;
285 }
286 }
287 }
288 }
289
291 int *comp_data;
292 uint16_t *img_ptr;
296 for (x = 0; x < image->numcomps; x++)
298
300 comp_data = image->comps[
index].data;
301 for (y = 0; y < image->comps[
index].h; y++) {
303 for (x = 0; x < image->comps[
index].w; x++) {
304 *img_ptr = (1 << image->comps[
index].prec - 1) * image->comps[
index].sgnd +
306 img_ptr++;
307 comp_data++;
308 }
309 }
310 }
311 }
312
314 {
316
317 opj_set_default_decoder_parameters(&
ctx->dec_params);
318 return 0;
319 }
320
323 {
324 const uint8_t *buf = avpkt->
data;
325 int buf_size = avpkt->
size;
329 int pixel_size = 0;
330 int ispacked = 0;
332 opj_image_t *image =
NULL;
334 opj_codec_t *dec =
NULL;
335 opj_stream_t *stream =
NULL;
336
337 *got_frame = 0;
338
339 // Check if input is a raw jpeg2k codestream or in jp2 wrapping
343 dec = opj_create_decompress(OPJ_CODEC_JP2);
344 } else {
345 /* If the AVPacket contains a jp2c box, then skip to
346 * the starting byte of the codestream. */
348 buf += 8;
349 dec = opj_create_decompress(OPJ_CODEC_J2K);
350 }
351
352 if (!dec) {
355 goto done;
356 }
357
363 goto done;
364 }
365
366 ctx->dec_params.cp_layer =
ctx->lowqual;
367 ctx->dec_params.cp_reduce = avctx->
lowres;
368
369 // Tie decoder with decoding parameters
370 opj_setup_decoder(dec, &
ctx->dec_params);
371
372 stream = opj_stream_default_create(OPJ_STREAM_READ);
373
374 if (!stream) {
376 "Codestream could not be opened for reading.\n");
378 goto done;
379 }
380
384 opj_stream_set_user_data(stream, &reader,
NULL);
385 opj_stream_set_user_data_length(stream, avpkt->
size);
386 // Decode the header only.
387 ret = !opj_read_header(stream, dec, &image);
388
392 goto done;
393 }
394
395 width = image->x1 - image->x0;
396 height = image->y1 - image->y0;
397
400 goto done;
401
405
408
412 goto done;
413 }
414 for (
i = 0;
i < image->numcomps;
i++)
417
419 goto done;
420
421 ret = !opj_decode(dec, stream, image);
422
426 goto done;
427 }
428
429 for (
i = 0;
i < image->numcomps;
i++) {
430 if (!image->comps[
i].data) {
432 "Image component %d contains no data.\n",
i);
434 goto done;
435 }
436 }
437
439 pixel_size =
desc->comp[0].step;
441
442 switch (pixel_size) {
443 case 1:
444 if (ispacked) {
446 } else {
448 }
449 break;
450 case 2:
451 if (ispacked) {
453 } else {
455 }
456 break;
457 case 3:
458 case 4:
459 if (ispacked) {
461 }
462 break;
463 case 6:
464 case 8:
465 if (ispacked) {
467 }
468 break;
469 default:
472 goto done;
473 }
474
475 *got_frame = 1;
479
480 done:
481 opj_image_destroy(image);
482 opj_stream_destroy(stream);
483 opj_destroy_codec(dec);
485 }
486
487 #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
488 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
489
491 { "lowqual", "Limit the number of layers used for decoding",
494 };
495
501 };
502
504 .
p.
name =
"libopenjpeg",
509 .p.max_lowres = 31,
511 .p.wrapper_name = "libopenjpeg",
515 };