libopenjpegdec.c 13.8 KB
Newer Older
J
Jai Menon 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * JPEG 2000 decoding support via OpenJPEG
 * Copyright (c) 2009 Jaikrishnan Menon <realityman@gmx.net>
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
23 24 25 26 27
 * @file
 * JPEG 2000 decoder using libopenjpeg
 */

#define  OPJ_STATIC
J
Jai Menon 已提交
28

29
#include "libavutil/common.h"
J
Jai Menon 已提交
30
#include "libavutil/intreadwrite.h"
31
#include "libavutil/imgutils.h"
32
#include "libavutil/pixfmt.h"
33 34
#include "libavutil/opt.h"
#include "avcodec.h"
35
#include "thread.h"
J
Jai Menon 已提交
36

37 38 39 40 41 42
#if HAVE_OPENJPEG_1_5_OPENJPEG_H
# include <openjpeg-1.5/openjpeg.h>
#else
# include <openjpeg.h>
#endif

J
Jai Menon 已提交
43 44 45
#define JP2_SIG_TYPE    0x6A502020
#define JP2_SIG_VALUE   0x0D0A870A

46 47
// pix_fmts with lower bpp have to be listed before
// similar pix_fmts with higher bpp.
48 49 50 51 52 53
#define RGB_PIXEL_FORMATS   AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
#define GRAY_PIXEL_FORMATS  AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16
#define YUV_PIXEL_FORMATS   AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
                            AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
                            AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
                            AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
54
                            AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
55
                            AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
56
                            AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
57 58
                            AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
                            AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
59 60
                            AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
                            AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
61

62 63
#define XYZ_PIXEL_FORMATS  AV_PIX_FMT_XYZ12

64 65 66
static const enum AVPixelFormat libopenjpeg_rgb_pix_fmts[]  = {RGB_PIXEL_FORMATS};
static const enum AVPixelFormat libopenjpeg_gray_pix_fmts[] = {GRAY_PIXEL_FORMATS};
static const enum AVPixelFormat libopenjpeg_yuv_pix_fmts[]  = {YUV_PIXEL_FORMATS};
67 68 69 70
static const enum AVPixelFormat libopenjpeg_all_pix_fmts[]  = {RGB_PIXEL_FORMATS,
                                                               GRAY_PIXEL_FORMATS,
                                                               YUV_PIXEL_FORMATS,
                                                               XYZ_PIXEL_FORMATS};
71

J
Jai Menon 已提交
72
typedef struct {
73
    AVClass *class;
J
Jai Menon 已提交
74
    opj_dparameters_t dec_params;
75
    int lowqual;
J
Jai Menon 已提交
76 77
} LibOpenJPEGContext;

78
static inline int libopenjpeg_matches_pix_fmt(const opj_image_t *image, enum AVPixelFormat pix_fmt)
J
Jai Menon 已提交
79
{
80
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
81
    int match = 1;
82

83
    if (desc->nb_components != image->numcomps) {
84
        return 0;
85 86
    }

87
    switch (desc->nb_components) {
88
    case 4: match = match && desc->comp[3].depth_minus1 + 1 >= image->comps[3].prec &&
89 90
                             1 == image->comps[3].dx &&
                             1 == image->comps[3].dy;
91 92 93 94 95 96 97
    case 3: match = match && desc->comp[2].depth_minus1 + 1 >= image->comps[2].prec &&
                             1 << desc->log2_chroma_w == image->comps[2].dx &&
                             1 << desc->log2_chroma_h == image->comps[2].dy;
    case 2: match = match && desc->comp[1].depth_minus1 + 1 >= image->comps[1].prec &&
                             1 << desc->log2_chroma_w == image->comps[1].dx &&
                             1 << desc->log2_chroma_h == image->comps[1].dy;
    case 1: match = match && desc->comp[0].depth_minus1 + 1 >= image->comps[0].prec &&
98 99 100 101
                             1 == image->comps[0].dx &&
                             1 == image->comps[0].dy;
    default:
        break;
102 103
    }

104 105
    return match;
}
106

107
static inline enum AVPixelFormat libopenjpeg_guess_pix_fmt(const opj_image_t *image) {
108
    int index;
109
    const enum AVPixelFormat *possible_fmts = NULL;
110 111 112 113 114
    int possible_fmts_nb = 0;

    switch (image->color_space) {
    case CLRSPC_SRGB:
        possible_fmts = libopenjpeg_rgb_pix_fmts;
115
        possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_rgb_pix_fmts);
116 117 118
        break;
    case CLRSPC_GRAY:
        possible_fmts = libopenjpeg_gray_pix_fmts;
119
        possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_gray_pix_fmts);
120 121 122
        break;
    case CLRSPC_SYCC:
        possible_fmts = libopenjpeg_yuv_pix_fmts;
123
        possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_yuv_pix_fmts);
124 125 126
        break;
    default:
        possible_fmts = libopenjpeg_all_pix_fmts;
127
        possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_all_pix_fmts);
128
        break;
129
    }
130

131 132 133 134
    for (index = 0; index < possible_fmts_nb; ++index) {
        if (libopenjpeg_matches_pix_fmt(image, possible_fmts[index])) {
            return possible_fmts[index];
        }
135 136
    }

137
    return AV_PIX_FMT_NONE;
138 139
}

140
static inline int libopenjpeg_ispacked(enum AVPixelFormat pix_fmt)
141
{
142
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
143
    int i, component_plane;
144

145
    if (pix_fmt == AV_PIX_FMT_GRAY16)
146 147
        return 0;

148 149 150
    component_plane = desc->comp[0].plane;
    for (i = 1; i < desc->nb_components; i++) {
        if (component_plane != desc->comp[i].plane)
151 152 153 154 155 156 157 158
            return 0;
    }
    return 1;
}

static inline void libopenjpeg_copy_to_packed8(AVFrame *picture, opj_image_t *image) {
    uint8_t *img_ptr;
    int index, x, y, c;
159
    for (y = 0; y < picture->height; y++) {
160 161
        index = y*picture->width;
        img_ptr = picture->data[0] + y*picture->linesize[0];
162 163
        for (x = 0; x < picture->width; x++, index++) {
            for (c = 0; c < image->numcomps; c++) {
164 165 166 167 168 169
                *img_ptr++ = image->comps[c].data[index];
            }
        }
    }
}

170 171 172 173
static inline void libopenjpeg_copy_to_packed16(AVFrame *picture, opj_image_t *image) {
    uint16_t *img_ptr;
    int index, x, y, c;
    int adjust[4];
174
    for (x = 0; x < image->numcomps; x++)
175
        adjust[x] = FFMAX(FFMIN(16 - image->comps[x].prec, 8), 0);
176

177 178 179 180 181 182 183 184 185 186 187
    for (y = 0; y < picture->height; y++) {
        index = y*picture->width;
        img_ptr = (uint16_t*) (picture->data[0] + y*picture->linesize[0]);
        for (x = 0; x < picture->width; x++, index++) {
            for (c = 0; c < image->numcomps; c++) {
                *img_ptr++ = image->comps[c].data[index] << adjust[c];
            }
        }
    }
}

188 189 190 191 192
static inline void libopenjpeg_copyto8(AVFrame *picture, opj_image_t *image) {
    int *comp_data;
    uint8_t *img_ptr;
    int index, x, y;

193
    for (index = 0; index < image->numcomps; index++) {
194
        comp_data = image->comps[index].data;
195
        for (y = 0; y < image->comps[index].h; y++) {
196
            img_ptr = picture->data[index] + y * picture->linesize[index];
197
            for (x = 0; x < image->comps[index].w; x++) {
198 199 200 201 202 203 204 205 206 207 208 209
                *img_ptr = (uint8_t) *comp_data;
                img_ptr++;
                comp_data++;
            }
        }
    }
}

static inline void libopenjpeg_copyto16(AVFrame *picture, opj_image_t *image) {
    int *comp_data;
    uint16_t *img_ptr;
    int index, x, y;
210
    for (index = 0; index < image->numcomps; index++) {
211
        comp_data = image->comps[index].data;
212
        for (y = 0; y < image->comps[index].h; y++) {
213
            img_ptr = (uint16_t*) (picture->data[index] + y * picture->linesize[index]);
214
            for (x = 0; x < image->comps[index].w; x++) {
215 216 217 218 219 220
                *img_ptr = *comp_data;
                img_ptr++;
                comp_data++;
            }
        }
    }
J
Jai Menon 已提交
221 222 223 224 225 226 227
}

static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
{
    LibOpenJPEGContext *ctx = avctx->priv_data;

    opj_set_default_decoder_parameters(&ctx->dec_params);
228 229 230
    return 0;
}

J
Jai Menon 已提交
231
static int libopenjpeg_decode_frame(AVCodecContext *avctx,
232
                                    void *data, int *got_frame,
233
                                    AVPacket *avpkt)
J
Jai Menon 已提交
234
{
235
    uint8_t *buf = avpkt->data;
236
    int buf_size = avpkt->size;
J
Jai Menon 已提交
237
    LibOpenJPEGContext *ctx = avctx->priv_data;
238 239
    ThreadFrame frame = { .f = data };
    AVFrame *picture  = data;
240
    const AVPixFmtDescriptor *desc;
J
Jai Menon 已提交
241 242 243
    opj_dinfo_t *dec;
    opj_cio_t *stream;
    opj_image_t *image;
244 245 246
    int width, height, ret = -1;
    int pixel_size = 0;
    int ispacked = 0;
247
    int i;
J
Jai Menon 已提交
248

249
    *got_frame = 0;
J
Jai Menon 已提交
250 251

    // Check if input is a raw jpeg2k codestream or in jp2 wrapping
252 253 254
    if ((AV_RB32(buf)     == 12)           &&
        (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
        (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
J
Jai Menon 已提交
255
        dec = opj_create_decompress(CODEC_JP2);
J
Jai Menon 已提交
256
    } else {
257 258
        /* If the AVPacket contains a jp2c box, then skip to
         * the starting byte of the codestream. */
259 260
        if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
            buf += 8;
J
Jai Menon 已提交
261
        dec = opj_create_decompress(CODEC_J2K);
J
Jai Menon 已提交
262 263
    }

264
    if (!dec) {
J
Jai Menon 已提交
265 266 267 268
        av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
        return -1;
    }
    opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
269
    ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
270
    ctx->dec_params.cp_layer          = ctx->lowqual;
J
Jai Menon 已提交
271 272 273
    // Tie decoder with decoding parameters
    opj_setup_decoder(dec, &ctx->dec_params);
    stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
274 275 276 277

    if (!stream) {
        av_log(avctx, AV_LOG_ERROR,
               "Codestream could not be opened for reading.\n");
J
Jai Menon 已提交
278 279 280 281
        opj_destroy_decompress(dec);
        return -1;
    }

282
    // Decode the header only.
J
Jai Menon 已提交
283 284
    image = opj_decode_with_info(dec, stream, NULL);
    opj_cio_close(stream);
285 286

    if (!image) {
J
Jai Menon 已提交
287 288 289 290
        av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
        opj_destroy_decompress(dec);
        return -1;
    }
291

292 293
    width  = image->x1 - image->x0;
    height = image->y1 - image->y0;
294

295 296 297
    if (av_image_check_size(width, height, 0, avctx) < 0) {
        av_log(avctx, AV_LOG_ERROR,
               "%dx%d dimension invalid.\n", width, height);
J
Jai Menon 已提交
298 299
        goto done;
    }
300

J
Jai Menon 已提交
301 302
    avcodec_set_dimensions(avctx, width, height);

303
    if (avctx->pix_fmt != AV_PIX_FMT_NONE)
304
        if (!libopenjpeg_matches_pix_fmt(image, avctx->pix_fmt))
305
            avctx->pix_fmt = AV_PIX_FMT_NONE;
306

307
    if (avctx->pix_fmt == AV_PIX_FMT_NONE)
308 309
        avctx->pix_fmt = libopenjpeg_guess_pix_fmt(image);

310
    if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
311 312
        av_log(avctx, AV_LOG_ERROR, "Unable to determine pixel format\n");
        goto done;
J
Jai Menon 已提交
313
    }
314 315 316
    for (i = 0; i < image->numcomps; i++)
        if (image->comps[i].prec > avctx->bits_per_raw_sample)
            avctx->bits_per_raw_sample = image->comps[i].prec;
J
Jai Menon 已提交
317

318
    if (ff_thread_get_buffer(avctx, &frame, 0) < 0)
319
        goto done;
J
Jai Menon 已提交
320

321
    ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
M
Michael Niedermayer 已提交
322
    ctx->dec_params.cp_reduce = avctx->lowres;
323
    // Tie decoder with decoding parameters.
324 325
    opj_setup_decoder(dec, &ctx->dec_params);
    stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
326 327 328
    if (!stream) {
        av_log(avctx, AV_LOG_ERROR,
               "Codestream could not be opened for reading.\n");
329
        goto done;
J
Jai Menon 已提交
330 331
    }

332
    opj_image_destroy(image);
333
    // Decode the codestream
334 335
    image = opj_decode_with_info(dec, stream, NULL);
    opj_cio_close(stream);
336

337
    if (!image) {
338
        av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
339
        goto done;
340
    }
341

342 343
    desc = av_pix_fmt_desc_get(avctx->pix_fmt);
    pixel_size = desc->comp[0].step_minus1 + 1;
344
    ispacked = libopenjpeg_ispacked(avctx->pix_fmt);
J
Jai Menon 已提交
345

346 347 348 349 350 351
    switch (pixel_size) {
    case 1:
        if (ispacked) {
            libopenjpeg_copy_to_packed8(picture, image);
        } else {
            libopenjpeg_copyto8(picture, image);
J
Jai Menon 已提交
352
        }
353 354
        break;
    case 2:
355 356 357 358 359
        if (ispacked) {
            libopenjpeg_copy_to_packed8(picture, image);
        } else {
            libopenjpeg_copyto16(picture, image);
        }
360 361
        break;
    case 3:
362
    case 4:
363 364 365 366
        if (ispacked) {
            libopenjpeg_copy_to_packed8(picture, image);
        }
        break;
367
    case 6:
368
    case 8:
369 370 371 372
        if (ispacked) {
            libopenjpeg_copy_to_packed16(picture, image);
        }
        break;
373 374 375
    default:
        av_log(avctx, AV_LOG_ERROR, "unsupported pixel size %d\n", pixel_size);
        goto done;
J
Jai Menon 已提交
376 377
    }

378
    *got_frame = 1;
379
    ret        = buf_size;
J
Jai Menon 已提交
380 381 382 383 384 385 386

done:
    opj_image_destroy(image);
    opj_destroy_decompress(dec);
    return ret;
}

387 388 389 390
#define OFFSET(x) offsetof(LibOpenJPEGContext, x)
#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM

static const AVOption options[] = {
391
    { "lowqual", "Limit the number of layers used for decoding",    OFFSET(lowqual), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VD },
392 393 394 395 396 397 398 399 400
    { NULL },
};

static const AVClass class = {
    .class_name = "libopenjpeg",
    .item_name  = av_default_item_name,
    .option     = options,
    .version    = LIBAVUTIL_VERSION_INT,
};
J
Jai Menon 已提交
401

402
AVCodec ff_libopenjpeg_decoder = {
403 404
    .name             = "libopenjpeg",
    .type             = AVMEDIA_TYPE_VIDEO,
405
    .id               = AV_CODEC_ID_JPEG2000,
406 407 408 409
    .priv_data_size   = sizeof(LibOpenJPEGContext),
    .init             = libopenjpeg_decode_init,
    .decode           = libopenjpeg_decode_frame,
    .capabilities     = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
410
    .max_lowres       = 31,
411
    .long_name        = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
412
    .priv_class       = &class,
413
};