Update avcodec to 20080825

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@27551 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
David McPaul
2008-09-15 14:05:40 +00:00
parent 67bd3f8a49
commit 4e70d9bc18
8 changed files with 2030 additions and 970 deletions
@@ -1,9 +1,42 @@
/* /*
* jfdctfst.c * jfdctfst.c
* *
* Copyright (C) 1994-1996, Thomas G. Lane.
* This file is part of the Independent JPEG Group's software. * This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file. *
* The authors make NO WARRANTY or representation, either express or implied,
* with respect to this software, its quality, accuracy, merchantability, or
* fitness for a particular purpose. This software is provided "AS IS", and
* you, its user, assume the entire risk as to its quality and accuracy.
*
* This software is copyright (C) 1994-1996, Thomas G. Lane.
* All Rights Reserved except as specified below.
*
* Permission is hereby granted to use, copy, modify, and distribute this
* software (or portions thereof) for any purpose, without fee, subject to
* these conditions:
* (1) If any part of the source code for this software is distributed, then
* this README file must be included, with this copyright and no-warranty
* notice unaltered; and any additions, deletions, or changes to the original
* files must be clearly indicated in accompanying documentation.
* (2) If only executable code is distributed, then the accompanying
* documentation must state that "this software is based in part on the work
* of the Independent JPEG Group".
* (3) Permission for use of this software is granted only if the user accepts
* full responsibility for any undesirable consequences; the authors accept
* NO LIABILITY for damages of any kind.
*
* These conditions apply to any software derived from or based on the IJG
* code, not just to the unmodified library. If you use our work, you ought
* to acknowledge us.
*
* Permission is NOT granted for the use of any IJG author's name or company
* name in advertising or publicity relating to this software or products
* derived from it. This software may be referred to only as "the Independent
* JPEG Group's software".
*
* We specifically permit and encourage the use of this software as the basis
* of commercial products, provided that all warranty or liability claims are
* assumed by the product vendor.
* *
* This file contains a fast, not so accurate integer implementation of the * This file contains a fast, not so accurate integer implementation of the
* forward DCT (Discrete Cosine Transform). * forward DCT (Discrete Cosine Transform).
@@ -112,7 +145,7 @@
#define MULTIPLY(var,const) ((DCTELEM) DESCALE((var) * (const), CONST_BITS)) #define MULTIPLY(var,const) ((DCTELEM) DESCALE((var) * (const), CONST_BITS))
static always_inline void row_fdct(DCTELEM * data){ static av_always_inline void row_fdct(DCTELEM * data){
int_fast16_t tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; int_fast16_t tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
int_fast16_t tmp10, tmp11, tmp12, tmp13; int_fast16_t tmp10, tmp11, tmp12, tmp13;
int_fast16_t z1, z2, z3, z4, z5, z11, z13; int_fast16_t z1, z2, z3, z4, z5, z11, z13;
@@ -1,9 +1,42 @@
/* /*
* jfdctint.c * jfdctint.c
* *
* Copyright (C) 1991-1996, Thomas G. Lane.
* This file is part of the Independent JPEG Group's software. * This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file. *
* The authors make NO WARRANTY or representation, either express or implied,
* with respect to this software, its quality, accuracy, merchantability, or
* fitness for a particular purpose. This software is provided "AS IS", and
* you, its user, assume the entire risk as to its quality and accuracy.
*
* This software is copyright (C) 1991-1996, Thomas G. Lane.
* All Rights Reserved except as specified below.
*
* Permission is hereby granted to use, copy, modify, and distribute this
* software (or portions thereof) for any purpose, without fee, subject to
* these conditions:
* (1) If any part of the source code for this software is distributed, then
* this README file must be included, with this copyright and no-warranty
* notice unaltered; and any additions, deletions, or changes to the original
* files must be clearly indicated in accompanying documentation.
* (2) If only executable code is distributed, then the accompanying
* documentation must state that "this software is based in part on the work
* of the Independent JPEG Group".
* (3) Permission for use of this software is granted only if the user accepts
* full responsibility for any undesirable consequences; the authors accept
* NO LIABILITY for damages of any kind.
*
* These conditions apply to any software derived from or based on the IJG
* code, not just to the unmodified library. If you use our work, you ought
* to acknowledge us.
*
* Permission is NOT granted for the use of any IJG author's name or company
* name in advertising or publicity relating to this software or products
* derived from it. This software may be referred to only as "the Independent
* JPEG Group's software".
*
* We specifically permit and encourage the use of this software as the basis
* of commercial products, provided that all warranty or liability claims are
* assumed by the product vendor.
* *
* This file contains a slow-but-accurate integer implementation of the * This file contains a slow-but-accurate integer implementation of the
* forward DCT (Discrete Cosine Transform). * forward DCT (Discrete Cosine Transform).
@@ -148,7 +181,7 @@
#endif #endif
static always_inline void row_fdct(DCTELEM * data){ static av_always_inline void row_fdct(DCTELEM * data){
int_fast32_t tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; int_fast32_t tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
int_fast32_t tmp10, tmp11, tmp12, tmp13; int_fast32_t tmp10, tmp11, tmp12, tmp13;
int_fast32_t z1, z2, z3, z4, z5; int_fast32_t z1, z2, z3, z4, z5;
@@ -0,0 +1,89 @@
/*
* JPEG-LS common code
* Copyright (c) 2003 Michael Niedermayer
* Copyright (c) 2006 Konstantin Shishkov
*
* 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
*/
/**
* @file jpegls.c
* JPEG-LS common code.
*/
#include "jpegls.h"
void ff_jpegls_init_state(JLSState *state){
int i;
state->twonear = state->near * 2 + 1;
state->range = ((state->maxval + state->twonear - 1) / state->twonear) + 1;
// QBPP = ceil(log2(RANGE))
for(state->qbpp = 0; (1 << state->qbpp) < state->range; state->qbpp++);
if(state->bpp < 8)
state->limit = 16 + 2 * state->bpp - state->qbpp;
else
state->limit = (4 * state->bpp) - state->qbpp;
for(i = 0; i < 367; i++) {
state->A[i] = FFMAX((state->range + 32) >> 6, 2);
state->N[i] = 1;
}
}
/**
* Custom value clipping function used in T1, T2, T3 calculation
*/
static inline int iso_clip(int v, int vmin, int vmax){
if(v > vmax || v < vmin) return vmin;
else return v;
}
void ff_jpegls_reset_coding_parameters(JLSState *s, int reset_all){
const int basic_t1= 3;
const int basic_t2= 7;
const int basic_t3= 21;
int factor;
if(s->maxval==0 || reset_all) s->maxval= (1 << s->bpp) - 1;
if(s->maxval >=128){
factor= (FFMIN(s->maxval, 4095) + 128)>>8;
if(s->T1==0 || reset_all)
s->T1= iso_clip(factor*(basic_t1-2) + 2 + 3*s->near, s->near+1, s->maxval);
if(s->T2==0 || reset_all)
s->T2= iso_clip(factor*(basic_t2-3) + 3 + 5*s->near, s->T1, s->maxval);
if(s->T3==0 || reset_all)
s->T3= iso_clip(factor*(basic_t3-4) + 4 + 7*s->near, s->T2, s->maxval);
}else{
factor= 256 / (s->maxval + 1);
if(s->T1==0 || reset_all)
s->T1= iso_clip(FFMAX(2, basic_t1/factor + 3*s->near), s->near+1, s->maxval);
if(s->T2==0 || reset_all)
s->T2= iso_clip(FFMAX(3, basic_t2/factor + 5*s->near), s->T1, s->maxval);
if(s->T3==0 || reset_all)
s->T3= iso_clip(FFMAX(4, basic_t3/factor + 7*s->near), s->T2, s->maxval);
}
if(s->reset==0 || reset_all) s->reset= 64;
// av_log(NULL, AV_LOG_DEBUG, "[JPEG-LS RESET] T=%i,%i,%i\n", s->T1, s->T2, s->T3);
}
@@ -0,0 +1,111 @@
/*
* JPEG-LS common code
* Copyright (c) 2003 Michael Niedermayer
* Copyright (c) 2006 Konstantin Shishkov
*
* 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
*/
/**
* @file jpegls.h
* JPEG-LS common code.
*/
#ifndef FFMPEG_JPEGLS_H
#define FFMPEG_JPEGLS_H
#include "avcodec.h"
typedef struct JpeglsContext{
AVCodecContext *avctx;
AVFrame picture;
}JpeglsContext;
typedef struct JLSState{
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];
}JLSState;
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
*/
static inline int ff_jpegls_quantize(JLSState *s, int v){ //FIXME optimize
if(v==0) return 0;
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;
return 4;
}
}
/**
* Calculate JPEG-LS codec values
*/
void ff_jpegls_reset_coding_parameters(JLSState *s, int reset_all);
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;
}
state->N[Q]++;
}
static inline int ff_jpegls_update_state_regular(JLSState *state, int Q, int err){
state->A[Q] += FFABS(err);
err *= state->twonear;
state->B[Q] += err;
ff_jpegls_downscale_state(state, Q);
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)
state->C[Q]--;
}else if(state->B[Q] > 0){
state->B[Q]= FFMIN(state->B[Q] - state->N[Q], 0);
if(state->C[Q] < 127)
state->C[Q]++;
}
return err;
}
#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))
#endif /* FFMPEG_JPEGLS_H */
@@ -0,0 +1,376 @@
/*
* JPEG-LS decoder
* Copyright (c) 2003 Michael Niedermayer
* Copyright (c) 2006 Konstantin Shishkov
*
* 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
*/
/**
* @file jpeglsdec.c
* JPEG-LS decoder.
*/
#include "avcodec.h"
#include "bitstream.h"
#include "golomb.h"
#include "mjpeg.h"
#include "mjpegdec.h"
#include "jpegls.h"
#include "jpeglsdec.h"
/*
* Uncomment this to significantly speed up decoding of broken JPEG-LS
* (or test broken JPEG-LS decoder) and slow down ordinary decoding a bit.
*
* There is no Golomb code with length >= 32 bits possible, so check and
* avoid situation of 32 zeros, FFmpeg Golomb decoder is painfully slow
* on this errors.
*/
//#define JLS_BROKEN
/**
* Decode LSE block with initialization parameters
*/
int ff_jpegls_decode_lse(MJpegDecodeContext *s)
{
int len, id;
/* XXX: verify len field validity */
len = get_bits(&s->gb, 16);
id = get_bits(&s->gb, 8);
switch(id){
case 1:
s->maxval= get_bits(&s->gb, 16);
s->t1= get_bits(&s->gb, 16);
s->t2= get_bits(&s->gb, 16);
s->t3= get_bits(&s->gb, 16);
s->reset= get_bits(&s->gb, 16);
// ff_jpegls_reset_coding_parameters(s, 0);
//FIXME quant table?
break;
case 2:
case 3:
av_log(s->avctx, AV_LOG_ERROR, "palette not supported\n");
return -1;
case 4:
av_log(s->avctx, AV_LOG_ERROR, "oversize image not supported\n");
return -1;
default:
av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
return -1;
}
// av_log(s->avctx, AV_LOG_DEBUG, "ID=%i, T=%i,%i,%i\n", id, s->t1, s->t2, s->t3);
return 0;
}
/**
* Get context-dependent Golomb code, decode it and update context
*/
static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q){
int k, ret;
for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
#ifdef JLS_BROKEN
if(!show_bits_long(gb, 32))return -1;
#endif
ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
/* decode mapped error */
if(ret & 1)
ret = -((ret + 1) >> 1);
else
ret >>= 1;
/* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
if(!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
ret = -(ret + 1);
ret= ff_jpegls_update_state_regular(state, Q, ret);
return ret;
}
/**
* Get Golomb code, decode it and update state for run termination
*/
static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state, int RItype, int limit_add){
int k, ret, temp, map;
int Q = 365 + RItype;
temp= state->A[Q];
if(RItype)
temp += state->N[Q] >> 1;
for(k = 0; (state->N[Q] << k) < temp; k++);
#ifdef JLS_BROKEN
if(!show_bits_long(gb, 32))return -1;
#endif
ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1, state->qbpp);
/* decode mapped error */
map = 0;
if(!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
map = 1;
ret += RItype + map;
if(ret & 1){
ret = map - ((ret + 1) >> 1);
state->B[Q]++;
} else {
ret = ret >> 1;
}
/* update state */
state->A[Q] += FFABS(ret) - RItype;
ret *= state->twonear;
ff_jpegls_downscale_state(state, Q);
return ret;
}
/**
* Decode one line of image
*/
static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s, void *last, void *dst, int last2, int w, int stride, int comp, int bits){
int i, x = 0;
int Ra, Rb, Rc, Rd;
int D0, D1, D2;
while(x < w) {
int err, pred;
/* compute gradients */
Ra = x ? R(dst, x - stride) : R(last, x);
Rb = R(last, x);
Rc = x ? R(last, x - stride) : last2;
Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
D0 = Rd - Rb;
D1 = Rb - Rc;
D2 = Rc - Ra;
/* run mode */
if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
int r;
int RItype;
/* decode full runs while available */
while(get_bits1(&s->gb)) {
int r;
r = 1 << ff_log2_run[state->run_index[comp]];
if(x + r * stride > w) {
r = (w - x) / stride;
}
for(i = 0; i < r; i++) {
W(dst, x, Ra);
x += stride;
}
/* if EOL reached, we stop decoding */
if(r != (1 << ff_log2_run[state->run_index[comp]]))
return;
if(state->run_index[comp] < 31)
state->run_index[comp]++;
if(x + stride > w)
return;
}
/* decode aborted run */
r = ff_log2_run[state->run_index[comp]];
if(r)
r = get_bits_long(&s->gb, r);
for(i = 0; i < r; i++) {
W(dst, x, Ra);
x += stride;
}
/* decode run termination value */
Rb = R(last, x);
RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
err = ls_get_code_runterm(&s->gb, state, RItype, ff_log2_run[state->run_index[comp]]);
if(state->run_index[comp])
state->run_index[comp]--;
if(state->near && RItype){
pred = Ra + err;
} else {
if(Rb < Ra)
pred = Rb - err;
else
pred = Rb + err;
}
} else { /* regular mode */
int context, sign;
context = ff_jpegls_quantize(state, D0) * 81 + ff_jpegls_quantize(state, D1) * 9 + ff_jpegls_quantize(state, D2);
pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
if(context < 0){
context = -context;
sign = 1;
}else{
sign = 0;
}
if(sign){
pred = av_clip(pred - state->C[context], 0, state->maxval);
err = -ls_get_code_regular(&s->gb, state, context);
} else {
pred = av_clip(pred + state->C[context], 0, state->maxval);
err = ls_get_code_regular(&s->gb, state, context);
}
/* we have to do something more for near-lossless coding */
pred += err;
}
if(state->near){
if(pred < -state->near)
pred += state->range * state->twonear;
else if(pred > state->maxval + state->near)
pred -= state->range * state->twonear;
pred = av_clip(pred, 0, state->maxval);
}
pred &= state->maxval;
W(dst, x, pred);
x += stride;
}
}
int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv){
int i, t = 0;
uint8_t *zero, *last, *cur;
JLSState *state;
int off = 0, stride = 1, width, shift;
zero = av_mallocz(s->picture.linesize[0]);
last = zero;
cur = s->picture.data[0];
state = av_mallocz(sizeof(JLSState));
/* initialize JPEG-LS state from JPEG parameters */
state->near = near;
state->bpp = (s->bits < 2) ? 2 : s->bits;
state->maxval = s->maxval;
state->T1 = s->t1;
state->T2 = s->t2;
state->T3 = s->t3;
state->reset = s->reset;
ff_jpegls_reset_coding_parameters(state, 0);
ff_jpegls_init_state(state);
if(s->bits <= 8)
shift = point_transform + (8 - s->bits);
else
shift = point_transform + (16 - s->bits);
// av_log(s->avctx, AV_LOG_DEBUG, "JPEG-LS params: %ix%i NEAR=%i MV=%i T(%i,%i,%i) RESET=%i, LIMIT=%i, qbpp=%i, RANGE=%i\n",s->width,s->height,state->near,state->maxval,state->T1,state->T2,state->T3,state->reset,state->limit,state->qbpp, state->range);
// av_log(s->avctx, AV_LOG_DEBUG, "JPEG params: ILV=%i Pt=%i BPP=%i, scan = %i\n", ilv, point_transform, s->bits, s->cur_scan);
if(ilv == 0) { /* separate planes */
off = s->cur_scan - 1;
stride = (s->nb_components > 1) ? 3 : 1;
width = s->width * stride;
cur += off;
for(i = 0; i < s->height; i++) {
if(s->bits <= 8){
ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
t = last[0];
}else{
ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
t = *((uint16_t*)last);
}
last = cur;
cur += s->picture.linesize[0];
if (s->restart_interval && !--s->restart_count) {
align_get_bits(&s->gb);
skip_bits(&s->gb, 16); /* skip RSTn */
}
}
} else if(ilv == 1) { /* line interleaving */
int j;
int Rc[3] = {0, 0, 0};
memset(cur, 0, s->picture.linesize[0]);
width = s->width * 3;
for(i = 0; i < s->height; i++) {
for(j = 0; j < 3; j++) {
ls_decode_line(state, s, last + j, cur + j, Rc[j], width, 3, j, 8);
Rc[j] = last[j];
if (s->restart_interval && !--s->restart_count) {
align_get_bits(&s->gb);
skip_bits(&s->gb, 16); /* skip RSTn */
}
}
last = cur;
cur += s->picture.linesize[0];
}
} else if(ilv == 2) { /* sample interleaving */
av_log(s->avctx, AV_LOG_ERROR, "Sample interleaved images are not supported.\n");
av_free(state);
av_free(zero);
return -1;
}
if(shift){ /* we need to do point transform or normalize samples */
int x, w;
w = s->width * s->nb_components;
if(s->bits <= 8){
uint8_t *src = s->picture.data[0];
for(i = 0; i < s->height; i++){
for(x = off; x < w; x+= stride){
src[x] <<= shift;
}
src += s->picture.linesize[0];
}
}else{
uint16_t *src = (uint16_t*) s->picture.data[0];
for(i = 0; i < s->height; i++){
for(x = 0; x < w; x++){
src[x] <<= shift;
}
src += s->picture.linesize[0]/2;
}
}
}
av_free(state);
av_free(zero);
return 0;
}
AVCodec jpegls_decoder = {
"jpegls",
CODEC_TYPE_VIDEO,
CODEC_ID_JPEGLS,
sizeof(MJpegDecodeContext),
ff_mjpeg_decode_init,
NULL,
ff_mjpeg_decode_end,
ff_mjpeg_decode_frame,
CODEC_CAP_DR1,
.long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),
};
@@ -0,0 +1,41 @@
/*
* JPEG-LS decoder
* Copyright (c) 2003 Michael Niedermayer
* Copyright (c) 2006 Konstantin Shishkov
*
* 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
*/
/**
* @file jpeglsdec.h
* JPEG-LS decoder.
*/
#ifndef FFMPEG_JPEGLSDEC_H
#define FFMPEG_JPEGLSDEC_H
#include "mjpeg.h"
#include "mjpegdec.h"
/**
* Decode LSE block with initialization parameters
*/
int ff_jpegls_decode_lse(MJpegDecodeContext *s);
int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv);
#endif /* FFMPEG_JPEGLSDEC_H */
@@ -0,0 +1,394 @@
/*
* JPEG-LS encoder
* Copyright (c) 2003 Michael Niedermayer
* Copyright (c) 2006 Konstantin Shishkov
*
* 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
*/
/**
* @file jpeglsenc.c
* JPEG-LS encoder.
*/
#include "avcodec.h"
#include "bitstream.h"
#include "golomb.h"
#include "dsputil.h"
#include "mjpeg.h"
#include "jpegls.h"
/**
* Encode error from regular symbol
*/
static inline void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q, int err){
int k;
int val;
int map;
for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
map = !state->near && !k && (2 * state->B[Q] <= -state->N[Q]);
if(err < 0)
err += state->range;
if(err >= ((state->range + 1) >> 1)) {
err -= state->range;
val = 2 * FFABS(err) - 1 - map;
} else
val = 2 * err + map;
set_ur_golomb_jpegls(pb, val, k, state->limit, state->qbpp);
ff_jpegls_update_state_regular(state, Q, err);
}
/**
* Encode error from run termination
*/
static inline void ls_encode_runterm(JLSState *state, PutBitContext *pb, int RItype, int err, int limit_add){
int k;
int val, map;
int Q = 365 + RItype;
int temp;
temp = state->A[Q];
if(RItype)
temp += state->N[Q] >> 1;
for(k = 0; (state->N[Q] << k) < temp; k++);
map = 0;
if(!k && err && (2 * state->B[Q] < state->N[Q]))
map = 1;
if(err < 0)
val = - (2 * err) - 1 - RItype + map;
else
val = 2 * err - RItype - map;
set_ur_golomb_jpegls(pb, val, k, state->limit - limit_add - 1, state->qbpp);
if(err < 0)
state->B[Q]++;
state->A[Q] += (val + 1 - RItype) >> 1;
ff_jpegls_downscale_state(state, Q);
}
/**
* Encode run value as specified by JPEG-LS standard
*/
static inline void ls_encode_run(JLSState *state, PutBitContext *pb, int run, int comp, int trail){
while(run >= (1 << ff_log2_run[state->run_index[comp]])){
put_bits(pb, 1, 1);
run -= 1 << ff_log2_run[state->run_index[comp]];
if(state->run_index[comp] < 31)
state->run_index[comp]++;
}
/* if hit EOL, encode another full run, else encode aborted run */
if(!trail && run) {
put_bits(pb, 1, 1);
}else if(trail){
put_bits(pb, 1, 0);
if(ff_log2_run[state->run_index[comp]])
put_bits(pb, ff_log2_run[state->run_index[comp]], run);
}
}
/**
* Encode one line of image
*/
static inline void ls_encode_line(JLSState *state, PutBitContext *pb, void *last, void *cur, int last2, int w, int stride, int comp, int bits){
int x = 0;
int Ra, Rb, Rc, Rd;
int D0, D1, D2;
while(x < w) {
int err, pred, sign;
/* compute gradients */
Ra = x ? R(cur, x - stride) : R(last, x);
Rb = R(last, x);
Rc = x ? R(last, x - stride) : last2;
Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
D0 = Rd - Rb;
D1 = Rb - Rc;
D2 = Rc - Ra;
/* run mode */
if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
int RUNval, RItype, run;
run = 0;
RUNval = Ra;
while(x < w && (FFABS(R(cur, x) - RUNval) <= state->near)){
run++;
W(cur, x, Ra);
x += stride;
}
ls_encode_run(state, pb, run, comp, x < w);
if(x >= w)
return;
Rb = R(last, x);
RItype = (FFABS(Ra - Rb) <= state->near);
pred = RItype ? Ra : Rb;
err = R(cur, x) - pred;
if(!RItype && Ra > Rb)
err = -err;
if(state->near){
if(err > 0)
err = (state->near + err) / state->twonear;
else
err = -(state->near - err) / state->twonear;
if(RItype || (Rb >= Ra))
Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
else
Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
W(cur, x, Ra);
}
if(err < 0)
err += state->range;
if(err >= ((state->range + 1) >> 1))
err -= state->range;
ls_encode_runterm(state, pb, RItype, err, ff_log2_run[state->run_index[comp]]);
if(state->run_index[comp] > 0)
state->run_index[comp]--;
} else { /* regular mode */
int context;
context = ff_jpegls_quantize(state, D0) * 81 + ff_jpegls_quantize(state, D1) * 9 + ff_jpegls_quantize(state, D2);
pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
if(context < 0){
context = -context;
sign = 1;
pred = av_clip(pred - state->C[context], 0, state->maxval);
err = pred - R(cur, x);
}else{
sign = 0;
pred = av_clip(pred + state->C[context], 0, state->maxval);
err = R(cur, x) - pred;
}
if(state->near){
if(err > 0)
err = (state->near + err) / state->twonear;
else
err = -(state->near - err) / state->twonear;
if(!sign)
Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
else
Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
W(cur, x, Ra);
}
ls_encode_regular(state, pb, context, err);
}
x += stride;
}
}
static void ls_store_lse(JLSState *state, PutBitContext *pb){
/* Test if we have default params and don't need to store LSE */
JLSState state2;
memset(&state2, 0, sizeof(JLSState));
state2.bpp = state->bpp;
state2.near = state->near;
ff_jpegls_reset_coding_parameters(&state2, 1);
if(state->T1 == state2.T1 && state->T2 == state2.T2 && state->T3 == state2.T3 && state->reset == state2.reset)
return;
/* store LSE type 1 */
put_marker(pb, LSE);
put_bits(pb, 16, 13);
put_bits(pb, 8, 1);
put_bits(pb, 16, state->maxval);
put_bits(pb, 16, state->T1);
put_bits(pb, 16, state->T2);
put_bits(pb, 16, state->T3);
put_bits(pb, 16, state->reset);
}
static int encode_picture_ls(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
JpeglsContext * const s = avctx->priv_data;
AVFrame *pict = data;
AVFrame * const p= (AVFrame*)&s->picture;
const int near = avctx->prediction_method;
PutBitContext pb, pb2;
GetBitContext gb;
uint8_t *buf2, *zero, *cur, *last;
JLSState *state;
int i, size;
int comps;
buf2 = av_malloc(buf_size);
init_put_bits(&pb, buf, buf_size);
init_put_bits(&pb2, buf2, buf_size);
*p = *pict;
p->pict_type= FF_I_TYPE;
p->key_frame= 1;
if(avctx->pix_fmt == PIX_FMT_GRAY8 || avctx->pix_fmt == PIX_FMT_GRAY16)
comps = 1;
else
comps = 3;
/* write our own JPEG header, can't use mjpeg_picture_header */
put_marker(&pb, SOI);
put_marker(&pb, SOF48);
put_bits(&pb, 16, 8 + comps * 3); // header size depends on components
put_bits(&pb, 8, (avctx->pix_fmt == PIX_FMT_GRAY16) ? 16 : 8); // bpp
put_bits(&pb, 16, avctx->height);
put_bits(&pb, 16, avctx->width);
put_bits(&pb, 8, comps); // components
for(i = 1; i <= comps; i++) {
put_bits(&pb, 8, i); // component ID
put_bits(&pb, 8, 0x11); // subsampling: none
put_bits(&pb, 8, 0); // Tiq, used by JPEG-LS ext
}
put_marker(&pb, SOS);
put_bits(&pb, 16, 6 + comps * 2);
put_bits(&pb, 8, comps);
for(i = 1; i <= comps; i++) {
put_bits(&pb, 8, i); // component ID
put_bits(&pb, 8, 0); // mapping index: none
}
put_bits(&pb, 8, near);
put_bits(&pb, 8, (comps > 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line
put_bits(&pb, 8, 0); // point transform: none
state = av_mallocz(sizeof(JLSState));
/* initialize JPEG-LS state from JPEG parameters */
state->near = near;
state->bpp = (avctx->pix_fmt == PIX_FMT_GRAY16) ? 16 : 8;
ff_jpegls_reset_coding_parameters(state, 0);
ff_jpegls_init_state(state);
ls_store_lse(state, &pb);
zero = av_mallocz(p->linesize[0]);
last = zero;
cur = p->data[0];
if(avctx->pix_fmt == PIX_FMT_GRAY8){
int t = 0;
for(i = 0; i < avctx->height; i++) {
ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 8);
t = last[0];
last = cur;
cur += p->linesize[0];
}
}else if(avctx->pix_fmt == PIX_FMT_GRAY16){
int t = 0;
for(i = 0; i < avctx->height; i++) {
ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 16);
t = *((uint16_t*)last);
last = cur;
cur += p->linesize[0];
}
}else if(avctx->pix_fmt == PIX_FMT_RGB24){
int j, width;
int Rc[3] = {0, 0, 0};
width = avctx->width * 3;
for(i = 0; i < avctx->height; i++) {
for(j = 0; j < 3; j++) {
ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j, 8);
Rc[j] = last[j];
}
last = cur;
cur += s->picture.linesize[0];
}
}else if(avctx->pix_fmt == PIX_FMT_BGR24){
int j, width;
int Rc[3] = {0, 0, 0};
width = avctx->width * 3;
for(i = 0; i < avctx->height; i++) {
for(j = 2; j >= 0; j--) {
ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j, 8);
Rc[j] = last[j];
}
last = cur;
cur += s->picture.linesize[0];
}
}
av_free(zero);
av_free(state);
// the specification says that after doing 0xff escaping unused bits in the
// last byte must be set to 0, so just append 7 "optional" zero-bits to
// avoid special-casing.
put_bits(&pb2, 7, 0);
size = put_bits_count(&pb2);
flush_put_bits(&pb2);
/* do escape coding */
init_get_bits(&gb, buf2, size);
size -= 7;
while(get_bits_count(&gb) < size){
int v;
v = get_bits(&gb, 8);
put_bits(&pb, 8, v);
if(v == 0xFF){
v = get_bits(&gb, 7);
put_bits(&pb, 8, v);
}
}
align_put_bits(&pb);
av_free(buf2);
/* End of image */
put_marker(&pb, EOI);
flush_put_bits(&pb);
emms_c();
return put_bits_count(&pb) >> 3;
}
static av_cold int encode_init_ls(AVCodecContext *ctx) {
JpeglsContext *c = (JpeglsContext*)ctx->priv_data;
c->avctx = ctx;
ctx->coded_frame = &c->picture;
if(ctx->pix_fmt != PIX_FMT_GRAY8 && ctx->pix_fmt != PIX_FMT_GRAY16 && ctx->pix_fmt != PIX_FMT_RGB24 && ctx->pix_fmt != PIX_FMT_BGR24){
av_log(ctx, AV_LOG_ERROR, "Only grayscale and RGB24/BGR24 images are supported\n");
return -1;
}
return 0;
}
AVCodec jpegls_encoder = { //FIXME avoid MPV_* lossless JPEG should not need them
"jpegls",
CODEC_TYPE_VIDEO,
CODEC_ID_JPEGLS,
sizeof(JpeglsContext),
encode_init_ls,
encode_picture_ls,
NULL,
.pix_fmts= (enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB24, PIX_FMT_GRAY8, PIX_FMT_GRAY16, PIX_FMT_NONE},
.long_name= NULL_IF_CONFIG_SMALL("JPEG-LS"),
};
@@ -1,9 +1,42 @@
/* /*
* jrevdct.c * jrevdct.c
* *
* Copyright (C) 1991, 1992, Thomas G. Lane.
* This file is part of the Independent JPEG Group's software. * This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file. *
* The authors make NO WARRANTY or representation, either express or implied,
* with respect to this software, its quality, accuracy, merchantability, or
* fitness for a particular purpose. This software is provided "AS IS", and
* you, its user, assume the entire risk as to its quality and accuracy.
*
* This software is copyright (C) 1991, 1992, Thomas G. Lane.
* All Rights Reserved except as specified below.
*
* Permission is hereby granted to use, copy, modify, and distribute this
* software (or portions thereof) for any purpose, without fee, subject to
* these conditions:
* (1) If any part of the source code for this software is distributed, then
* this README file must be included, with this copyright and no-warranty
* notice unaltered; and any additions, deletions, or changes to the original
* files must be clearly indicated in accompanying documentation.
* (2) If only executable code is distributed, then the accompanying
* documentation must state that "this software is based in part on the work
* of the Independent JPEG Group".
* (3) Permission for use of this software is granted only if the user accepts
* full responsibility for any undesirable consequences; the authors accept
* NO LIABILITY for damages of any kind.
*
* These conditions apply to any software derived from or based on the IJG
* code, not just to the unmodified library. If you use our work, you ought
* to acknowledge us.
*
* Permission is NOT granted for the use of any IJG author's name or company
* name in advertising or publicity relating to this software or products
* derived from it. This software may be referred to only as "the Independent
* JPEG Group's software".
*
* We specifically permit and encourage the use of this software as the basis
* of commercial products, provided that all warranty or liability claims are
* assumed by the product vendor.
* *
* This file contains the basic inverse-DCT transformation subroutine. * This file contains the basic inverse-DCT transformation subroutine.
* *
@@ -235,9 +268,7 @@ void j_rev_dct(DCTBLOCK data)
/* The rotator is sqrt(2)*c(-6). */ /* The rotator is sqrt(2)*c(-6). */
{ {
if (d6) { if (d6) {
if (d4) {
if (d2) { if (d2) {
if (d0) {
/* d0 != 0, d2 != 0, d4 != 0, d6 != 0 */ /* d0 != 0, d2 != 0, d4 != 0, d6 != 0 */
z1 = MULTIPLY(d2 + d6, FIX_0_541196100); z1 = MULTIPLY(d2 + d6, FIX_0_541196100);
tmp2 = z1 + MULTIPLY(-d6, FIX_1_847759065); tmp2 = z1 + MULTIPLY(-d6, FIX_1_847759065);
@@ -251,20 +282,6 @@ void j_rev_dct(DCTBLOCK data)
tmp11 = tmp1 + tmp2; tmp11 = tmp1 + tmp2;
tmp12 = tmp1 - tmp2; tmp12 = tmp1 - tmp2;
} else { } else {
/* d0 == 0, d2 != 0, d4 != 0, d6 != 0 */
z1 = MULTIPLY(d2 + d6, FIX_0_541196100);
tmp2 = z1 + MULTIPLY(-d6, FIX_1_847759065);
tmp3 = z1 + MULTIPLY(d2, FIX_0_765366865);
tmp0 = d4 << CONST_BITS;
tmp10 = tmp0 + tmp3;
tmp13 = tmp0 - tmp3;
tmp11 = tmp2 - tmp0;
tmp12 = -(tmp0 + tmp2);
}
} else {
if (d0) {
/* d0 != 0, d2 == 0, d4 != 0, d6 != 0 */ /* d0 != 0, d2 == 0, d4 != 0, d6 != 0 */
tmp2 = MULTIPLY(-d6, FIX_1_306562965); tmp2 = MULTIPLY(-d6, FIX_1_306562965);
tmp3 = MULTIPLY(d6, FIX_0_541196100); tmp3 = MULTIPLY(d6, FIX_0_541196100);
@@ -276,72 +293,9 @@ void j_rev_dct(DCTBLOCK data)
tmp13 = tmp0 - tmp3; tmp13 = tmp0 - tmp3;
tmp11 = tmp1 + tmp2; tmp11 = tmp1 + tmp2;
tmp12 = tmp1 - tmp2; tmp12 = tmp1 - tmp2;
} else {
/* d0 == 0, d2 == 0, d4 != 0, d6 != 0 */
tmp2 = MULTIPLY(-d6, FIX_1_306562965);
tmp3 = MULTIPLY(d6, FIX_0_541196100);
tmp0 = d4 << CONST_BITS;
tmp10 = tmp0 + tmp3;
tmp13 = tmp0 - tmp3;
tmp11 = tmp2 - tmp0;
tmp12 = -(tmp0 + tmp2);
}
} }
} else { } else {
if (d2) { if (d2) {
if (d0) {
/* d0 != 0, d2 != 0, d4 == 0, d6 != 0 */
z1 = MULTIPLY(d2 + d6, FIX_0_541196100);
tmp2 = z1 + MULTIPLY(-d6, FIX_1_847759065);
tmp3 = z1 + MULTIPLY(d2, FIX_0_765366865);
tmp0 = d0 << CONST_BITS;
tmp10 = tmp0 + tmp3;
tmp13 = tmp0 - tmp3;
tmp11 = tmp0 + tmp2;
tmp12 = tmp0 - tmp2;
} else {
/* d0 == 0, d2 != 0, d4 == 0, d6 != 0 */
z1 = MULTIPLY(d2 + d6, FIX_0_541196100);
tmp2 = z1 + MULTIPLY(-d6, FIX_1_847759065);
tmp3 = z1 + MULTIPLY(d2, FIX_0_765366865);
tmp10 = tmp3;
tmp13 = -tmp3;
tmp11 = tmp2;
tmp12 = -tmp2;
}
} else {
if (d0) {
/* d0 != 0, d2 == 0, d4 == 0, d6 != 0 */
tmp2 = MULTIPLY(-d6, FIX_1_306562965);
tmp3 = MULTIPLY(d6, FIX_0_541196100);
tmp0 = d0 << CONST_BITS;
tmp10 = tmp0 + tmp3;
tmp13 = tmp0 - tmp3;
tmp11 = tmp0 + tmp2;
tmp12 = tmp0 - tmp2;
} else {
/* d0 == 0, d2 == 0, d4 == 0, d6 != 0 */
tmp2 = MULTIPLY(-d6, FIX_1_306562965);
tmp3 = MULTIPLY(d6, FIX_0_541196100);
tmp10 = tmp3;
tmp13 = -tmp3;
tmp11 = tmp2;
tmp12 = -tmp2;
}
}
}
} else {
if (d4) {
if (d2) {
if (d0) {
/* d0 != 0, d2 != 0, d4 != 0, d6 == 0 */ /* d0 != 0, d2 != 0, d4 != 0, d6 == 0 */
tmp2 = MULTIPLY(d2, FIX_0_541196100); tmp2 = MULTIPLY(d2, FIX_0_541196100);
tmp3 = MULTIPLY(d2, FIX_1_306562965); tmp3 = MULTIPLY(d2, FIX_1_306562965);
@@ -354,60 +308,9 @@ void j_rev_dct(DCTBLOCK data)
tmp11 = tmp1 + tmp2; tmp11 = tmp1 + tmp2;
tmp12 = tmp1 - tmp2; tmp12 = tmp1 - tmp2;
} else { } else {
/* d0 == 0, d2 != 0, d4 != 0, d6 == 0 */
tmp2 = MULTIPLY(d2, FIX_0_541196100);
tmp3 = MULTIPLY(d2, FIX_1_306562965);
tmp0 = d4 << CONST_BITS;
tmp10 = tmp0 + tmp3;
tmp13 = tmp0 - tmp3;
tmp11 = tmp2 - tmp0;
tmp12 = -(tmp0 + tmp2);
}
} else {
if (d0) {
/* d0 != 0, d2 == 0, d4 != 0, d6 == 0 */ /* d0 != 0, d2 == 0, d4 != 0, d6 == 0 */
tmp10 = tmp13 = (d0 + d4) << CONST_BITS; tmp10 = tmp13 = (d0 + d4) << CONST_BITS;
tmp11 = tmp12 = (d0 - d4) << CONST_BITS; tmp11 = tmp12 = (d0 - d4) << CONST_BITS;
} else {
/* d0 == 0, d2 == 0, d4 != 0, d6 == 0 */
tmp10 = tmp13 = d4 << CONST_BITS;
tmp11 = tmp12 = -tmp10;
}
}
} else {
if (d2) {
if (d0) {
/* d0 != 0, d2 != 0, d4 == 0, d6 == 0 */
tmp2 = MULTIPLY(d2, FIX_0_541196100);
tmp3 = MULTIPLY(d2, FIX_1_306562965);
tmp0 = d0 << CONST_BITS;
tmp10 = tmp0 + tmp3;
tmp13 = tmp0 - tmp3;
tmp11 = tmp0 + tmp2;
tmp12 = tmp0 - tmp2;
} else {
/* d0 == 0, d2 != 0, d4 == 0, d6 == 0 */
tmp2 = MULTIPLY(d2, FIX_0_541196100);
tmp3 = MULTIPLY(d2, FIX_1_306562965);
tmp10 = tmp3;
tmp13 = -tmp3;
tmp11 = tmp2;
tmp12 = -tmp2;
}
} else {
if (d0) {
/* d0 != 0, d2 == 0, d4 == 0, d6 == 0 */
tmp10 = tmp13 = tmp11 = tmp12 = d0 << CONST_BITS;
} else {
/* d0 == 0, d2 == 0, d4 == 0, d6 == 0 */
tmp10 = tmp13 = tmp11 = tmp12 = 0;
}
}
} }
} }
@@ -711,9 +614,7 @@ void j_rev_dct(DCTBLOCK data)
/* Even part: reverse the even part of the forward DCT. */ /* Even part: reverse the even part of the forward DCT. */
/* The rotator is sqrt(2)*c(-6). */ /* The rotator is sqrt(2)*c(-6). */
if (d6) { if (d6) {
if (d4) {
if (d2) { if (d2) {
if (d0) {
/* d0 != 0, d2 != 0, d4 != 0, d6 != 0 */ /* d0 != 0, d2 != 0, d4 != 0, d6 != 0 */
z1 = MULTIPLY(d2 + d6, FIX_0_541196100); z1 = MULTIPLY(d2 + d6, FIX_0_541196100);
tmp2 = z1 + MULTIPLY(-d6, FIX_1_847759065); tmp2 = z1 + MULTIPLY(-d6, FIX_1_847759065);
@@ -727,20 +628,6 @@ void j_rev_dct(DCTBLOCK data)
tmp11 = tmp1 + tmp2; tmp11 = tmp1 + tmp2;
tmp12 = tmp1 - tmp2; tmp12 = tmp1 - tmp2;
} else { } else {
/* d0 == 0, d2 != 0, d4 != 0, d6 != 0 */
z1 = MULTIPLY(d2 + d6, FIX_0_541196100);
tmp2 = z1 + MULTIPLY(-d6, FIX_1_847759065);
tmp3 = z1 + MULTIPLY(d2, FIX_0_765366865);
tmp0 = d4 << CONST_BITS;
tmp10 = tmp0 + tmp3;
tmp13 = tmp0 - tmp3;
tmp11 = tmp2 - tmp0;
tmp12 = -(tmp0 + tmp2);
}
} else {
if (d0) {
/* d0 != 0, d2 == 0, d4 != 0, d6 != 0 */ /* d0 != 0, d2 == 0, d4 != 0, d6 != 0 */
tmp2 = MULTIPLY(-d6, FIX_1_306562965); tmp2 = MULTIPLY(-d6, FIX_1_306562965);
tmp3 = MULTIPLY(d6, FIX_0_541196100); tmp3 = MULTIPLY(d6, FIX_0_541196100);
@@ -752,72 +639,9 @@ void j_rev_dct(DCTBLOCK data)
tmp13 = tmp0 - tmp3; tmp13 = tmp0 - tmp3;
tmp11 = tmp1 + tmp2; tmp11 = tmp1 + tmp2;
tmp12 = tmp1 - tmp2; tmp12 = tmp1 - tmp2;
} else {
/* d0 == 0, d2 == 0, d4 != 0, d6 != 0 */
tmp2 = MULTIPLY(-d6, FIX_1_306562965);
tmp3 = MULTIPLY(d6, FIX_0_541196100);
tmp0 = d4 << CONST_BITS;
tmp10 = tmp0 + tmp3;
tmp13 = tmp0 - tmp3;
tmp11 = tmp2 - tmp0;
tmp12 = -(tmp0 + tmp2);
}
} }
} else { } else {
if (d2) { if (d2) {
if (d0) {
/* d0 != 0, d2 != 0, d4 == 0, d6 != 0 */
z1 = MULTIPLY(d2 + d6, FIX_0_541196100);
tmp2 = z1 + MULTIPLY(-d6, FIX_1_847759065);
tmp3 = z1 + MULTIPLY(d2, FIX_0_765366865);
tmp0 = d0 << CONST_BITS;
tmp10 = tmp0 + tmp3;
tmp13 = tmp0 - tmp3;
tmp11 = tmp0 + tmp2;
tmp12 = tmp0 - tmp2;
} else {
/* d0 == 0, d2 != 0, d4 == 0, d6 != 0 */
z1 = MULTIPLY(d2 + d6, FIX_0_541196100);
tmp2 = z1 + MULTIPLY(-d6, FIX_1_847759065);
tmp3 = z1 + MULTIPLY(d2, FIX_0_765366865);
tmp10 = tmp3;
tmp13 = -tmp3;
tmp11 = tmp2;
tmp12 = -tmp2;
}
} else {
if (d0) {
/* d0 != 0, d2 == 0, d4 == 0, d6 != 0 */
tmp2 = MULTIPLY(-d6, FIX_1_306562965);
tmp3 = MULTIPLY(d6, FIX_0_541196100);
tmp0 = d0 << CONST_BITS;
tmp10 = tmp0 + tmp3;
tmp13 = tmp0 - tmp3;
tmp11 = tmp0 + tmp2;
tmp12 = tmp0 - tmp2;
} else {
/* d0 == 0, d2 == 0, d4 == 0, d6 != 0 */
tmp2 = MULTIPLY(-d6, FIX_1_306562965);
tmp3 = MULTIPLY(d6, FIX_0_541196100);
tmp10 = tmp3;
tmp13 = -tmp3;
tmp11 = tmp2;
tmp12 = -tmp2;
}
}
}
} else {
if (d4) {
if (d2) {
if (d0) {
/* d0 != 0, d2 != 0, d4 != 0, d6 == 0 */ /* d0 != 0, d2 != 0, d4 != 0, d6 == 0 */
tmp2 = MULTIPLY(d2, FIX_0_541196100); tmp2 = MULTIPLY(d2, FIX_0_541196100);
tmp3 = MULTIPLY(d2, FIX_1_306562965); tmp3 = MULTIPLY(d2, FIX_1_306562965);
@@ -830,60 +654,9 @@ void j_rev_dct(DCTBLOCK data)
tmp11 = tmp1 + tmp2; tmp11 = tmp1 + tmp2;
tmp12 = tmp1 - tmp2; tmp12 = tmp1 - tmp2;
} else { } else {
/* d0 == 0, d2 != 0, d4 != 0, d6 == 0 */
tmp2 = MULTIPLY(d2, FIX_0_541196100);
tmp3 = MULTIPLY(d2, FIX_1_306562965);
tmp0 = d4 << CONST_BITS;
tmp10 = tmp0 + tmp3;
tmp13 = tmp0 - tmp3;
tmp11 = tmp2 - tmp0;
tmp12 = -(tmp0 + tmp2);
}
} else {
if (d0) {
/* d0 != 0, d2 == 0, d4 != 0, d6 == 0 */ /* d0 != 0, d2 == 0, d4 != 0, d6 == 0 */
tmp10 = tmp13 = (d0 + d4) << CONST_BITS; tmp10 = tmp13 = (d0 + d4) << CONST_BITS;
tmp11 = tmp12 = (d0 - d4) << CONST_BITS; tmp11 = tmp12 = (d0 - d4) << CONST_BITS;
} else {
/* d0 == 0, d2 == 0, d4 != 0, d6 == 0 */
tmp10 = tmp13 = d4 << CONST_BITS;
tmp11 = tmp12 = -tmp10;
}
}
} else {
if (d2) {
if (d0) {
/* d0 != 0, d2 != 0, d4 == 0, d6 == 0 */
tmp2 = MULTIPLY(d2, FIX_0_541196100);
tmp3 = MULTIPLY(d2, FIX_1_306562965);
tmp0 = d0 << CONST_BITS;
tmp10 = tmp0 + tmp3;
tmp13 = tmp0 - tmp3;
tmp11 = tmp0 + tmp2;
tmp12 = tmp0 - tmp2;
} else {
/* d0 == 0, d2 != 0, d4 == 0, d6 == 0 */
tmp2 = MULTIPLY(d2, FIX_0_541196100);
tmp3 = MULTIPLY(d2, FIX_1_306562965);
tmp10 = tmp3;
tmp13 = -tmp3;
tmp11 = tmp2;
tmp12 = -tmp2;
}
} else {
if (d0) {
/* d0 != 0, d2 == 0, d4 == 0, d6 == 0 */
tmp10 = tmp13 = tmp11 = tmp12 = d0 << CONST_BITS;
} else {
/* d0 == 0, d2 == 0, d4 == 0, d6 == 0 */
tmp10 = tmp13 = tmp11 = tmp12 = 0;
}
}
} }
} }
@@ -1172,5 +945,215 @@ void j_rev_dct(DCTBLOCK data)
} }
} }
#undef DCTSIZE
#define DCTSIZE 4
#define DCTSTRIDE 8
void j_rev_dct4(DCTBLOCK data)
{
int32_t tmp0, tmp1, tmp2, tmp3;
int32_t tmp10, tmp11, tmp12, tmp13;
int32_t z1;
int32_t d0, d2, d4, d6;
register DCTELEM *dataptr;
int rowctr;
/* Pass 1: process rows. */
/* Note results are scaled up by sqrt(8) compared to a true IDCT; */
/* furthermore, we scale the results by 2**PASS1_BITS. */
data[0] += 4;
dataptr = data;
for (rowctr = DCTSIZE-1; rowctr >= 0; rowctr--) {
/* Due to quantization, we will usually find that many of the input
* coefficients are zero, especially the AC terms. We can exploit this
* by short-circuiting the IDCT calculation for any row in which all
* the AC terms are zero. In that case each output is equal to the
* DC coefficient (with scale factor as needed).
* With typical images and quantization tables, half or more of the
* row DCT calculations can be simplified this way.
*/
register int *idataptr = (int*)dataptr;
d0 = dataptr[0];
d2 = dataptr[1];
d4 = dataptr[2];
d6 = dataptr[3];
if ((d2 | d4 | d6) == 0) {
/* AC terms all zero */
if (d0) {
/* Compute a 32 bit value to assign. */
DCTELEM dcval = (DCTELEM) (d0 << PASS1_BITS);
register int v = (dcval & 0xffff) | ((dcval << 16) & 0xffff0000);
idataptr[0] = v;
idataptr[1] = v;
}
dataptr += DCTSTRIDE; /* advance pointer to next row */
continue;
}
/* Even part: reverse the even part of the forward DCT. */
/* The rotator is sqrt(2)*c(-6). */
if (d6) {
if (d2) {
/* d0 != 0, d2 != 0, d4 != 0, d6 != 0 */
z1 = MULTIPLY(d2 + d6, FIX_0_541196100);
tmp2 = z1 + MULTIPLY(-d6, FIX_1_847759065);
tmp3 = z1 + MULTIPLY(d2, FIX_0_765366865);
tmp0 = (d0 + d4) << CONST_BITS;
tmp1 = (d0 - d4) << CONST_BITS;
tmp10 = tmp0 + tmp3;
tmp13 = tmp0 - tmp3;
tmp11 = tmp1 + tmp2;
tmp12 = tmp1 - tmp2;
} else {
/* d0 != 0, d2 == 0, d4 != 0, d6 != 0 */
tmp2 = MULTIPLY(-d6, FIX_1_306562965);
tmp3 = MULTIPLY(d6, FIX_0_541196100);
tmp0 = (d0 + d4) << CONST_BITS;
tmp1 = (d0 - d4) << CONST_BITS;
tmp10 = tmp0 + tmp3;
tmp13 = tmp0 - tmp3;
tmp11 = tmp1 + tmp2;
tmp12 = tmp1 - tmp2;
}
} else {
if (d2) {
/* d0 != 0, d2 != 0, d4 != 0, d6 == 0 */
tmp2 = MULTIPLY(d2, FIX_0_541196100);
tmp3 = MULTIPLY(d2, FIX_1_306562965);
tmp0 = (d0 + d4) << CONST_BITS;
tmp1 = (d0 - d4) << CONST_BITS;
tmp10 = tmp0 + tmp3;
tmp13 = tmp0 - tmp3;
tmp11 = tmp1 + tmp2;
tmp12 = tmp1 - tmp2;
} else {
/* d0 != 0, d2 == 0, d4 != 0, d6 == 0 */
tmp10 = tmp13 = (d0 + d4) << CONST_BITS;
tmp11 = tmp12 = (d0 - d4) << CONST_BITS;
}
}
/* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
dataptr[0] = (DCTELEM) DESCALE(tmp10, CONST_BITS-PASS1_BITS);
dataptr[1] = (DCTELEM) DESCALE(tmp11, CONST_BITS-PASS1_BITS);
dataptr[2] = (DCTELEM) DESCALE(tmp12, CONST_BITS-PASS1_BITS);
dataptr[3] = (DCTELEM) DESCALE(tmp13, CONST_BITS-PASS1_BITS);
dataptr += DCTSTRIDE; /* advance pointer to next row */
}
/* Pass 2: process columns. */
/* Note that we must descale the results by a factor of 8 == 2**3, */
/* and also undo the PASS1_BITS scaling. */
dataptr = data;
for (rowctr = DCTSIZE-1; rowctr >= 0; rowctr--) {
/* Columns of zeroes can be exploited in the same way as we did with rows.
* However, the row calculation has created many nonzero AC terms, so the
* simplification applies less often (typically 5% to 10% of the time).
* On machines with very fast multiplication, it's possible that the
* test takes more time than it's worth. In that case this section
* may be commented out.
*/
d0 = dataptr[DCTSTRIDE*0];
d2 = dataptr[DCTSTRIDE*1];
d4 = dataptr[DCTSTRIDE*2];
d6 = dataptr[DCTSTRIDE*3];
/* Even part: reverse the even part of the forward DCT. */
/* The rotator is sqrt(2)*c(-6). */
if (d6) {
if (d2) {
/* d0 != 0, d2 != 0, d4 != 0, d6 != 0 */
z1 = MULTIPLY(d2 + d6, FIX_0_541196100);
tmp2 = z1 + MULTIPLY(-d6, FIX_1_847759065);
tmp3 = z1 + MULTIPLY(d2, FIX_0_765366865);
tmp0 = (d0 + d4) << CONST_BITS;
tmp1 = (d0 - d4) << CONST_BITS;
tmp10 = tmp0 + tmp3;
tmp13 = tmp0 - tmp3;
tmp11 = tmp1 + tmp2;
tmp12 = tmp1 - tmp2;
} else {
/* d0 != 0, d2 == 0, d4 != 0, d6 != 0 */
tmp2 = MULTIPLY(-d6, FIX_1_306562965);
tmp3 = MULTIPLY(d6, FIX_0_541196100);
tmp0 = (d0 + d4) << CONST_BITS;
tmp1 = (d0 - d4) << CONST_BITS;
tmp10 = tmp0 + tmp3;
tmp13 = tmp0 - tmp3;
tmp11 = tmp1 + tmp2;
tmp12 = tmp1 - tmp2;
}
} else {
if (d2) {
/* d0 != 0, d2 != 0, d4 != 0, d6 == 0 */
tmp2 = MULTIPLY(d2, FIX_0_541196100);
tmp3 = MULTIPLY(d2, FIX_1_306562965);
tmp0 = (d0 + d4) << CONST_BITS;
tmp1 = (d0 - d4) << CONST_BITS;
tmp10 = tmp0 + tmp3;
tmp13 = tmp0 - tmp3;
tmp11 = tmp1 + tmp2;
tmp12 = tmp1 - tmp2;
} else {
/* d0 != 0, d2 == 0, d4 != 0, d6 == 0 */
tmp10 = tmp13 = (d0 + d4) << CONST_BITS;
tmp11 = tmp12 = (d0 - d4) << CONST_BITS;
}
}
/* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
dataptr[DCTSTRIDE*0] = tmp10 >> (CONST_BITS+PASS1_BITS+3);
dataptr[DCTSTRIDE*1] = tmp11 >> (CONST_BITS+PASS1_BITS+3);
dataptr[DCTSTRIDE*2] = tmp12 >> (CONST_BITS+PASS1_BITS+3);
dataptr[DCTSTRIDE*3] = tmp13 >> (CONST_BITS+PASS1_BITS+3);
dataptr++; /* advance pointer to next column */
}
}
void j_rev_dct2(DCTBLOCK data){
int d00, d01, d10, d11;
data[0] += 4;
d00 = data[0+0*DCTSTRIDE] + data[1+0*DCTSTRIDE];
d01 = data[0+0*DCTSTRIDE] - data[1+0*DCTSTRIDE];
d10 = data[0+1*DCTSTRIDE] + data[1+1*DCTSTRIDE];
d11 = data[0+1*DCTSTRIDE] - data[1+1*DCTSTRIDE];
data[0+0*DCTSTRIDE]= (d00 + d10)>>3;
data[1+0*DCTSTRIDE]= (d01 + d11)>>3;
data[0+1*DCTSTRIDE]= (d00 - d10)>>3;
data[1+1*DCTSTRIDE]= (d01 - d11)>>3;
}
void j_rev_dct1(DCTBLOCK data){
data[0] = (data[0] + 4)>>3;
}
#undef FIX #undef FIX
#undef CONST_BITS #undef CONST_BITS