Mercurial > libavcodec.hg
annotate libopenjpeg.c @ 10075:e944ff8f487a libavcodec
TwinVQ decoder
| author | vitor |
|---|---|
| date | Sun, 23 Aug 2009 01:55:54 +0000 |
| parents | 00581e706e1d |
| children | a050e8857f00 |
| rev | line source |
|---|---|
| 8747 | 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 libavcodec/libopenjpeg.c | |
| 24 * JPEG 2000 decoder using libopenjpeg | |
| 25 */ | |
| 26 | |
| 27 #include "avcodec.h" | |
| 28 #include "libavutil/intreadwrite.h" | |
| 29 #define OPJ_STATIC | |
|
8787
9575568668c4
Use default system include path for an installed OpenJPEG library.
diego
parents:
8747
diff
changeset
|
30 #include <openjpeg.h> |
| 8747 | 31 |
| 32 #define JP2_SIG_TYPE 0x6A502020 | |
| 33 #define JP2_SIG_VALUE 0x0D0A870A | |
| 34 | |
| 35 typedef struct { | |
| 36 opj_dparameters_t dec_params; | |
| 37 AVFrame image; | |
| 38 } LibOpenJPEGContext; | |
| 39 | |
| 40 static int check_image_attributes(opj_image_t *image) | |
| 41 { | |
| 42 return(image->comps[0].dx == image->comps[1].dx && | |
| 43 image->comps[1].dx == image->comps[2].dx && | |
| 44 image->comps[0].dy == image->comps[1].dy && | |
| 45 image->comps[1].dy == image->comps[2].dy && | |
| 46 image->comps[0].prec == image->comps[1].prec && | |
| 47 image->comps[1].prec == image->comps[2].prec); | |
| 48 } | |
| 49 | |
| 50 static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx) | |
| 51 { | |
| 52 LibOpenJPEGContext *ctx = avctx->priv_data; | |
| 53 | |
| 54 opj_set_default_decoder_parameters(&ctx->dec_params); | |
| 55 avctx->coded_frame = &ctx->image; | |
| 56 return 0; | |
| 57 } | |
| 58 | |
| 59 static int libopenjpeg_decode_frame(AVCodecContext *avctx, | |
| 60 void *data, int *data_size, | |
|
9355
54bc8a2727b0
Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents:
8787
diff
changeset
|
61 AVPacket *avpkt) |
| 8747 | 62 { |
|
9355
54bc8a2727b0
Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents:
8787
diff
changeset
|
63 const uint8_t *buf = avpkt->data; |
|
54bc8a2727b0
Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents:
8787
diff
changeset
|
64 int buf_size = avpkt->size; |
| 8747 | 65 LibOpenJPEGContext *ctx = avctx->priv_data; |
| 66 AVFrame *picture = &ctx->image, *output = data; | |
| 67 opj_dinfo_t *dec; | |
| 68 opj_cio_t *stream; | |
| 69 opj_image_t *image; | |
| 70 int width, height, has_alpha = 0, ret = -1; | |
| 71 int x, y, index; | |
| 72 uint8_t *img_ptr; | |
| 73 int adjust[4]; | |
| 74 | |
| 75 *data_size = 0; | |
| 76 | |
| 77 // Check if input is a raw jpeg2k codestream or in jp2 wrapping | |
| 78 if((AV_RB32(buf) == 12) && | |
| 79 (AV_RB32(buf + 4) == JP2_SIG_TYPE) && | |
| 80 (AV_RB32(buf + 8) == JP2_SIG_VALUE)) { | |
| 81 dec = opj_create_decompress(CODEC_JP2); | |
| 82 } else { | |
| 83 dec = opj_create_decompress(CODEC_J2K); | |
| 84 } | |
| 85 | |
| 86 if(!dec) { | |
| 87 av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n"); | |
| 88 return -1; | |
| 89 } | |
| 90 opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL); | |
| 91 | |
| 9715 | 92 ctx->dec_params.cp_reduce = avctx->lowres; |
| 8747 | 93 // Tie decoder with decoding parameters |
| 94 opj_setup_decoder(dec, &ctx->dec_params); | |
| 95 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size); | |
| 96 if(!stream) { | |
| 97 av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n"); | |
| 98 opj_destroy_decompress(dec); | |
| 99 return -1; | |
| 100 } | |
| 101 | |
| 102 // Decode the codestream | |
| 103 image = opj_decode_with_info(dec, stream, NULL); | |
| 104 opj_cio_close(stream); | |
| 105 if(!image) { | |
| 106 av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n"); | |
| 107 opj_destroy_decompress(dec); | |
| 108 return -1; | |
| 109 } | |
| 9715 | 110 width = image->comps[0].w << avctx->lowres; |
| 111 height = image->comps[0].h << avctx->lowres; | |
| 8747 | 112 if(avcodec_check_dimensions(avctx, width, height) < 0) { |
| 113 av_log(avctx, AV_LOG_ERROR, "%dx%d dimension invalid.\n", width, height); | |
| 114 goto done; | |
| 115 } | |
| 116 avcodec_set_dimensions(avctx, width, height); | |
| 117 | |
| 118 switch(image->numcomps) | |
| 119 { | |
| 120 case 1: avctx->pix_fmt = PIX_FMT_GRAY8; | |
| 121 break; | |
| 122 case 3: if(check_image_attributes(image)) { | |
| 123 avctx->pix_fmt = PIX_FMT_RGB24; | |
| 124 } else { | |
| 125 avctx->pix_fmt = PIX_FMT_GRAY8; | |
| 126 av_log(avctx, AV_LOG_ERROR, "Only first component will be used.\n"); | |
| 127 } | |
| 128 break; | |
| 129 case 4: has_alpha = 1; | |
| 130 avctx->pix_fmt = PIX_FMT_RGB32; | |
| 131 break; | |
| 132 default: av_log(avctx, AV_LOG_ERROR, "%d components unsupported.\n", image->numcomps); | |
| 133 goto done; | |
| 134 } | |
| 135 | |
| 136 if(picture->data[0]) | |
| 137 avctx->release_buffer(avctx, picture); | |
| 138 | |
| 139 if(avctx->get_buffer(avctx, picture) < 0) { | |
| 140 av_log(avctx, AV_LOG_ERROR, "Couldn't allocate image buffer.\n"); | |
| 141 return -1; | |
| 142 } | |
| 143 | |
| 144 for(x = 0; x < image->numcomps; x++) { | |
| 145 adjust[x] = FFMAX(image->comps[x].prec - 8, 0); | |
| 146 } | |
| 147 | |
|
9714
5df11d49abb7
Use dimensions stored in AVCodecContext instead of local variables.
jai_menon
parents:
9355
diff
changeset
|
148 for(y = 0; y < avctx->height; y++) { |
|
5df11d49abb7
Use dimensions stored in AVCodecContext instead of local variables.
jai_menon
parents:
9355
diff
changeset
|
149 index = y*avctx->width; |
| 8747 | 150 img_ptr = picture->data[0] + y*picture->linesize[0]; |
|
9714
5df11d49abb7
Use dimensions stored in AVCodecContext instead of local variables.
jai_menon
parents:
9355
diff
changeset
|
151 for(x = 0; x < avctx->width; x++, index++) { |
| 8747 | 152 *img_ptr++ = image->comps[0].data[index] >> adjust[0]; |
| 153 if(image->numcomps > 2 && check_image_attributes(image)) { | |
| 154 *img_ptr++ = image->comps[1].data[index] >> adjust[1]; | |
| 155 *img_ptr++ = image->comps[2].data[index] >> adjust[2]; | |
| 156 if(has_alpha) | |
| 157 *img_ptr++ = image->comps[3].data[index] >> adjust[3]; | |
| 158 } | |
| 159 } | |
| 160 } | |
| 161 | |
| 162 *output = ctx->image; | |
| 163 *data_size = sizeof(AVPicture); | |
| 164 ret = buf_size; | |
| 165 | |
| 166 done: | |
| 167 opj_image_destroy(image); | |
| 168 opj_destroy_decompress(dec); | |
| 169 return ret; | |
| 170 } | |
| 171 | |
| 172 static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx) | |
| 173 { | |
| 174 LibOpenJPEGContext *ctx = avctx->priv_data; | |
| 175 | |
| 176 if(ctx->image.data[0]) | |
| 177 avctx->release_buffer(avctx, &ctx->image); | |
| 178 return 0 ; | |
| 179 } | |
| 180 | |
| 181 | |
| 182 AVCodec libopenjpeg_decoder = { | |
| 183 "libopenjpeg", | |
| 184 CODEC_TYPE_VIDEO, | |
| 185 CODEC_ID_JPEG2000, | |
| 186 sizeof(LibOpenJPEGContext), | |
| 187 libopenjpeg_decode_init, | |
| 188 NULL, | |
| 189 libopenjpeg_decode_close, | |
| 190 libopenjpeg_decode_frame, | |
|
9804
00581e706e1d
libopenjpeg wrapper uses get_buffer, set CODEC_CAP_DR1
bcoudurier
parents:
9715
diff
changeset
|
191 CODEC_CAP_DR1, |
| 8747 | 192 .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG based JPEG 2000 decoder"), |
| 193 } ; |
