* JPEG 2000 image decoder
*/
+#include <inttypes.h>
+
+#include "libavutil/attributes.h"
#include "libavutil/common.h"
#include "libavutil/opt.h"
#include "avcodec.h"
#include "internal.h"
#include "thread.h"
#include "jpeg2000.h"
+#include "jpeg2000dsp.h"
#define JP2_SIG_TYPE 0x6A502020
#define JP2_SIG_VALUE 0x0D0A870A
int16_t curtileno;
Jpeg2000Tile *tile;
+ Jpeg2000DSPContext dsp;
/*options parameters*/
int reduction_factor;
return AVERROR_INVALIDDATA;
}
- if (ncomponents > 3) {
+ if (ncomponents > 4) {
avpriv_request_sample(s->avctx, "Support for %d components",
s->ncomponents);
return AVERROR_PATCHWELCOME;
if (tmp.mct && s->ncomponents < 3) {
av_log(s->avctx, AV_LOG_ERROR,
- "MCT %d with too few components (%d)\n",
+ "MCT %"PRIu8" with too few components (%d)\n",
tmp.mct, s->ncomponents);
return AVERROR_INVALIDDATA;
}
bytestream2_get_byteu(&s->g); // TNsot
if (Psot > bytestream2_get_bytes_left(&s->g) + n + 2) {
- av_log(s->avctx, AV_LOG_ERROR, "Psot %d too big\n", Psot);
+ av_log(s->avctx, AV_LOG_ERROR, "Psot %"PRIu32" too big\n", Psot);
return AVERROR_INVALIDDATA;
}
if (TPsot >= FF_ARRAY_ELEMS(s->tile[Isot].tile_part)) {
- avpriv_request_sample(s->avctx, "Support for %d components", TPsot);
+ avpriv_request_sample(s->avctx, "Support for %"PRIu8" components", TPsot);
return AVERROR_PATCHWELCOME;
}
}
cblk->length += cblk->lengthinc;
cblk->lengthinc = 0;
+
+ if (cblk->length > sizeof(cblk->data)) {
+ av_log(s->avctx, AV_LOG_ERROR,
+ "Block length %"PRIu16" > data size %zd\n",
+ cblk->length, sizeof(cblk->data));
+ return AVERROR_INVALIDDATA;
+ }
}
}
return 0;
}
}
-/* Inverse ICT parameters in float and integer.
- * int value = (float value) * (1<<16) */
-static const float f_ict_params[4] = {
- 1.402f,
- 0.34413f,
- 0.71414f,
- 1.772f
-};
-static const int i_ict_params[4] = {
- 91881,
- 22553,
- 46802,
- 116130
-};
-
-static void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
+static inline void mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
{
int i, csize = 1;
- int32_t *src[3], i0, i1, i2;
- float *srcf[3], i0f, i1f, i2f;
+ void *src[3];
for (i = 0; i < 3; i++)
if (tile->codsty[0].transform == FF_DWT97)
- srcf[i] = tile->comp[i].f_data;
+ src[i] = tile->comp[i].f_data;
else
- src [i] = tile->comp[i].i_data;
+ src[i] = tile->comp[i].i_data;
for (i = 0; i < 2; i++)
csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
- switch (tile->codsty[0].transform) {
- case FF_DWT97:
- for (i = 0; i < csize; i++) {
- i0f = *srcf[0] + (f_ict_params[0] * *srcf[2]);
- i1f = *srcf[0] - (f_ict_params[1] * *srcf[1])
- - (f_ict_params[2] * *srcf[2]);
- i2f = *srcf[0] + (f_ict_params[3] * *srcf[1]);
- *srcf[0]++ = i0f;
- *srcf[1]++ = i1f;
- *srcf[2]++ = i2f;
- }
- break;
- case FF_DWT97_INT:
- for (i = 0; i < csize; i++) {
- i0 = *src[0] + (((i_ict_params[0] * *src[2]) + (1 << 15)) >> 16);
- i1 = *src[0] - (((i_ict_params[1] * *src[1]) + (1 << 15)) >> 16)
- - (((i_ict_params[2] * *src[2]) + (1 << 15)) >> 16);
- i2 = *src[0] + (((i_ict_params[3] * *src[1]) + (1 << 15)) >> 16);
- *src[0]++ = i0;
- *src[1]++ = i1;
- *src[2]++ = i2;
- }
- break;
- case FF_DWT53:
- for (i = 0; i < csize; i++) {
- i1 = *src[0] - (*src[2] + *src[1] >> 2);
- i0 = i1 + *src[2];
- i2 = i1 + *src[1];
- *src[0]++ = i0;
- *src[1]++ = i1;
- *src[2]++ = i2;
- }
- break;
- }
+
+ s->dsp.mct_decode[tile->codsty[0].transform](src[0], src[1], src[2], csize);
}
static int jpeg2000_decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
if (s->precision <= 8) {
for (compno = 0; compno < s->ncomponents; compno++) {
Jpeg2000Component *comp = tile->comp + compno;
+ Jpeg2000CodingStyle *codsty = tile->codsty + compno;
float *datap = comp->f_data;
int32_t *i_datap = comp->i_data;
int cbps = s->cbps[compno];
x = tile->comp[compno].coord[0][0] - s->image_offset_x;
dst = line + x * s->ncomponents + compno;
- if (tile->codsty->transform == FF_DWT97) {
+ if (codsty->transform == FF_DWT97) {
for (; x < w; x += s->cdx[compno]) {
int val = lrintf(*datap) + (1 << (cbps - 1));
/* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
} else {
for (compno = 0; compno < s->ncomponents; compno++) {
Jpeg2000Component *comp = tile->comp + compno;
+ Jpeg2000CodingStyle *codsty = tile->codsty + compno;
float *datap = comp->f_data;
int32_t *i_datap = comp->i_data;
uint16_t *linel;
uint16_t *dst;
x = tile->comp[compno].coord[0][0] - s->image_offset_x;
dst = linel + (x * s->ncomponents + compno);
- if (tile->codsty->transform == FF_DWT97) {
+ if (codsty->transform == FF_DWT97) {
for (; x < w; x += s-> cdx[compno]) {
int val = lrintf(*datap) + (1 << (cbps - 1));
/* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
av_log(s->avctx, AV_LOG_ERROR, "Missing SOT\n");
return AVERROR_INVALIDDATA;
}
+ if (!s->tile) {
+ av_log(s->avctx, AV_LOG_ERROR, "Missing SIZ\n");
+ return AVERROR_INVALIDDATA;
+ }
tile = s->tile + s->curtileno;
tp = tile->tile_part + tile->tp_idx;
break;
default:
av_log(s->avctx, AV_LOG_ERROR,
- "unsupported marker 0x%.4X at pos 0x%X\n",
+ "unsupported marker 0x%.4"PRIX16" at pos 0x%X\n",
marker, bytestream2_tell(&s->g) - 4);
bytestream2_skip(&s->g, len - 2);
break;
}
if (bytestream2_tell(&s->g) - oldpos != len || ret) {
av_log(s->avctx, AV_LOG_ERROR,
- "error during processing marker segment %.4x\n", marker);
+ "error during processing marker segment %.4"PRIx16"\n",
+ marker);
return ret ? ret : -1;
}
}
return 0;
}
+static av_cold int jpeg2000_decode_init(AVCodecContext *avctx)
+{
+ Jpeg2000DecoderContext *s = avctx->priv_data;
+
+ ff_jpeg2000dsp_init(&s->dsp);
+
+ return 0;
+}
+
static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
int *got_frame, AVPacket *avpkt)
{
return ret;
}
-static void jpeg2000_init_static_data(AVCodec *codec)
+static av_cold void jpeg2000_init_static_data(AVCodec *codec)
{
ff_jpeg2000_init_tier1_luts();
+ ff_mqc_init_context_tables();
}
#define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
.capabilities = CODEC_CAP_FRAME_THREADS,
.priv_data_size = sizeof(Jpeg2000DecoderContext),
.init_static_data = jpeg2000_init_static_data,
+ .init = jpeg2000_decode_init,
.decode = jpeg2000_decode_frame,
.priv_class = &class,
.profiles = NULL_IF_CONFIG_SMALL(profiles)