2007-05-14 01:16:56 +02:00
|
|
|
/*
|
|
|
|
* JPEG-LS common code
|
|
|
|
* Copyright (c) 2003 Michael Niedermayer
|
|
|
|
* Copyright (c) 2006 Konstantin Shishkov
|
|
|
|
*
|
2011-03-18 18:35:10 +01:00
|
|
|
* This file is part of Libav.
|
2007-05-14 01:16:56 +02:00
|
|
|
*
|
2011-03-18 18:35:10 +01:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2007-05-14 01:16:56 +02:00
|
|
|
* 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.
|
|
|
|
*
|
2011-03-18 18:35:10 +01:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2007-05-14 01:16:56 +02:00
|
|
|
* 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
|
2011-03-18 18:35:10 +01:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2007-05-14 01:16:56 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 16:45:34 +02:00
|
|
|
* @file
|
2007-05-14 01:16:56 +02:00
|
|
|
* JPEG-LS common code.
|
|
|
|
*/
|
|
|
|
|
2008-08-31 09:39:47 +02:00
|
|
|
#ifndef AVCODEC_JPEGLS_H
|
|
|
|
#define AVCODEC_JPEGLS_H
|
2007-05-14 01:16:56 +02:00
|
|
|
|
2012-08-06 15:49:32 +02:00
|
|
|
#include "libavutil/common.h"
|
2013-05-15 19:03:53 +02:00
|
|
|
#include "avcodec.h"
|
2007-05-14 01:16:56 +02:00
|
|
|
|
2013-05-15 19:03:53 +02:00
|
|
|
typedef struct JpeglsContext {
|
2007-05-14 01:16:56 +02:00
|
|
|
AVCodecContext *avctx;
|
|
|
|
AVFrame picture;
|
2013-05-15 19:03:53 +02:00
|
|
|
} JpeglsContext;
|
2007-05-14 01:16:56 +02:00
|
|
|
|
2013-05-15 19:03:53 +02:00
|
|
|
typedef struct JLSState {
|
2007-05-14 01:16:56 +02:00
|
|
|
int T1, T2, T3;
|
|
|
|
int A[367], B[367], C[365], N[367];
|
|
|
|
int limit, reset, bpp, qbpp, maxval, range;
|
|
|
|
int near, twonear;
|
|
|
|
int run_index[3];
|
2013-05-15 19:03:53 +02:00
|
|
|
} JLSState;
|
2007-05-14 01:16:56 +02:00
|
|
|
|
|
|
|
extern const uint8_t ff_log2_run[32];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate initial JPEG-LS parameters
|
|
|
|
*/
|
|
|
|
void ff_jpegls_init_state(JLSState *state);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate quantized gradient value, used for context determination
|
|
|
|
*/
|
2013-05-15 19:03:53 +02:00
|
|
|
static inline int ff_jpegls_quantize(JLSState *s, int v)
|
|
|
|
{
|
|
|
|
if (v == 0)
|
2007-05-14 01:16:56 +02:00
|
|
|
return 0;
|
2013-05-15 19:03:53 +02:00
|
|
|
if (v < 0) {
|
|
|
|
if (v <= -s->T3)
|
|
|
|
return -4;
|
|
|
|
if (v <= -s->T2)
|
|
|
|
return -3;
|
|
|
|
if (v <= -s->T1)
|
|
|
|
return -2;
|
|
|
|
if (v < -s->near)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
if (v <= s->near)
|
|
|
|
return 0;
|
|
|
|
if (v < s->T1)
|
|
|
|
return 1;
|
|
|
|
if (v < s->T2)
|
|
|
|
return 2;
|
|
|
|
if (v < s->T3)
|
|
|
|
return 3;
|
2007-05-14 01:16:56 +02:00
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate JPEG-LS codec values
|
|
|
|
*/
|
|
|
|
void ff_jpegls_reset_coding_parameters(JLSState *s, int reset_all);
|
|
|
|
|
2013-05-15 19:03:53 +02:00
|
|
|
static inline void ff_jpegls_downscale_state(JLSState *state, int Q)
|
|
|
|
{
|
|
|
|
if (state->N[Q] == state->reset) {
|
|
|
|
state->A[Q] >>= 1;
|
|
|
|
state->B[Q] >>= 1;
|
|
|
|
state->N[Q] >>= 1;
|
2007-05-14 01:16:56 +02:00
|
|
|
}
|
|
|
|
state->N[Q]++;
|
|
|
|
}
|
|
|
|
|
2013-05-15 19:03:53 +02:00
|
|
|
static inline int ff_jpegls_update_state_regular(JLSState *state,
|
|
|
|
int Q, int err)
|
|
|
|
{
|
2007-05-14 01:16:56 +02:00
|
|
|
state->A[Q] += FFABS(err);
|
2013-05-15 19:03:53 +02:00
|
|
|
err *= state->twonear;
|
2007-05-14 01:16:56 +02:00
|
|
|
state->B[Q] += err;
|
|
|
|
|
|
|
|
ff_jpegls_downscale_state(state, Q);
|
|
|
|
|
2013-05-15 19:03:53 +02:00
|
|
|
if (state->B[Q] <= -state->N[Q]) {
|
|
|
|
state->B[Q] = FFMAX(state->B[Q] + state->N[Q], 1 - state->N[Q]);
|
|
|
|
if (state->C[Q] > -128)
|
2007-05-14 01:16:56 +02:00
|
|
|
state->C[Q]--;
|
2013-05-15 19:03:53 +02:00
|
|
|
} else if (state->B[Q] > 0) {
|
|
|
|
state->B[Q] = FFMIN(state->B[Q] - state->N[Q], 0);
|
|
|
|
if (state->C[Q] < 127)
|
2007-05-14 01:16:56 +02:00
|
|
|
state->C[Q]++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2013-05-15 19:03:53 +02:00
|
|
|
#define R(a, i) (bits == 8 ? ((uint8_t *)(a))[i] : ((uint16_t *)(a))[i])
|
|
|
|
#define W(a, i, v) (bits == 8 ? (((uint8_t *)(a))[i] = v) : (((uint16_t *)(a))[i] = v))
|
2007-05-14 01:16:56 +02:00
|
|
|
|
2008-08-31 09:39:47 +02:00
|
|
|
#endif /* AVCODEC_JPEGLS_H */
|