• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/jpeglsdec.c

Go to the documentation of this file.
00001 /*
00002  * JPEG-LS decoder
00003  * Copyright (c) 2003 Michael Niedermayer
00004  * Copyright (c) 2006 Konstantin Shishkov
00005  *
00006  * This file is part of FFmpeg.
00007  *
00008  * FFmpeg is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * FFmpeg is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with FFmpeg; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00028 #include "avcodec.h"
00029 #include "get_bits.h"
00030 #include "golomb.h"
00031 #include "mathops.h"
00032 #include "mjpeg.h"
00033 #include "mjpegdec.h"
00034 #include "jpegls.h"
00035 #include "jpeglsdec.h"
00036 
00037 
00038 /*
00039 * Uncomment this to significantly speed up decoding of broken JPEG-LS
00040 * (or test broken JPEG-LS decoder) and slow down ordinary decoding a bit.
00041 *
00042 * There is no Golomb code with length >= 32 bits possible, so check and
00043 * avoid situation of 32 zeros, FFmpeg Golomb decoder is painfully slow
00044 * on this errors.
00045 */
00046 //#define JLS_BROKEN
00047 
00048 
00052 int ff_jpegls_decode_lse(MJpegDecodeContext *s)
00053 {
00054     int len, id;
00055 
00056     /* XXX: verify len field validity */
00057     len = get_bits(&s->gb, 16);
00058     id = get_bits(&s->gb, 8);
00059 
00060     switch(id){
00061     case 1:
00062         s->maxval= get_bits(&s->gb, 16);
00063         s->t1= get_bits(&s->gb, 16);
00064         s->t2= get_bits(&s->gb, 16);
00065         s->t3= get_bits(&s->gb, 16);
00066         s->reset= get_bits(&s->gb, 16);
00067 
00068 //        ff_jpegls_reset_coding_parameters(s, 0);
00069         //FIXME quant table?
00070         break;
00071     case 2:
00072     case 3:
00073         av_log(s->avctx, AV_LOG_ERROR, "palette not supported\n");
00074         return -1;
00075     case 4:
00076         av_log(s->avctx, AV_LOG_ERROR, "oversize image not supported\n");
00077         return -1;
00078     default:
00079         av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
00080         return -1;
00081     }
00082 //    av_log(s->avctx, AV_LOG_DEBUG, "ID=%i, T=%i,%i,%i\n", id, s->t1, s->t2, s->t3);
00083 
00084     return 0;
00085 }
00086 
00090 static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q){
00091     int k, ret;
00092 
00093     for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
00094 
00095 #ifdef JLS_BROKEN
00096     if(!show_bits_long(gb, 32))return -1;
00097 #endif
00098     ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
00099 
00100     /* decode mapped error */
00101     if(ret & 1)
00102         ret = -((ret + 1) >> 1);
00103     else
00104         ret >>= 1;
00105 
00106     /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
00107     if(!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
00108         ret = -(ret + 1);
00109 
00110     ret= ff_jpegls_update_state_regular(state, Q, ret);
00111 
00112     return ret;
00113 }
00114 
00118 static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state, int RItype, int limit_add){
00119     int k, ret, temp, map;
00120     int Q = 365 + RItype;
00121 
00122     temp=  state->A[Q];
00123     if(RItype)
00124         temp += state->N[Q] >> 1;
00125 
00126     for(k = 0; (state->N[Q] << k) < temp; k++);
00127 
00128 #ifdef JLS_BROKEN
00129     if(!show_bits_long(gb, 32))return -1;
00130 #endif
00131     ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1, state->qbpp);
00132 
00133     /* decode mapped error */
00134     map = 0;
00135     if(!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
00136         map = 1;
00137     ret += RItype + map;
00138 
00139     if(ret & 1){
00140         ret = map - ((ret + 1) >> 1);
00141         state->B[Q]++;
00142     } else {
00143         ret = ret >> 1;
00144     }
00145 
00146     /* update state */
00147     state->A[Q] += FFABS(ret) - RItype;
00148     ret *= state->twonear;
00149     ff_jpegls_downscale_state(state, Q);
00150 
00151     return ret;
00152 }
00153 
00157 static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s, void *last, void *dst, int last2, int w, int stride, int comp, int bits){
00158     int i, x = 0;
00159     int Ra, Rb, Rc, Rd;
00160     int D0, D1, D2;
00161 
00162     while(x < w) {
00163         int err, pred;
00164 
00165         /* compute gradients */
00166         Ra = x ? R(dst, x - stride) : R(last, x);
00167         Rb = R(last, x);
00168         Rc = x ? R(last, x - stride) : last2;
00169         Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
00170         D0 = Rd - Rb;
00171         D1 = Rb - Rc;
00172         D2 = Rc - Ra;
00173         /* run mode */
00174         if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
00175             int r;
00176             int RItype;
00177 
00178             /* decode full runs while available */
00179             while(get_bits1(&s->gb)) {
00180                 int r;
00181                 r = 1 << ff_log2_run[state->run_index[comp]];
00182                 if(x + r * stride > w) {
00183                     r = (w - x) / stride;
00184                 }
00185                 for(i = 0; i < r; i++) {
00186                     W(dst, x, Ra);
00187                     x += stride;
00188                 }
00189                 /* if EOL reached, we stop decoding */
00190                 if(r != (1 << ff_log2_run[state->run_index[comp]]))
00191                     return;
00192                 if(state->run_index[comp] < 31)
00193                     state->run_index[comp]++;
00194                 if(x + stride > w)
00195                     return;
00196             }
00197             /* decode aborted run */
00198             r = ff_log2_run[state->run_index[comp]];
00199             if(r)
00200                 r = get_bits_long(&s->gb, r);
00201             for(i = 0; i < r; i++) {
00202                 W(dst, x, Ra);
00203                 x += stride;
00204             }
00205 
00206             /* decode run termination value */
00207             Rb = R(last, x);
00208             RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
00209             err = ls_get_code_runterm(&s->gb, state, RItype, ff_log2_run[state->run_index[comp]]);
00210             if(state->run_index[comp])
00211                 state->run_index[comp]--;
00212 
00213             if(state->near && RItype){
00214                 pred = Ra + err;
00215             } else {
00216                 if(Rb < Ra)
00217                     pred = Rb - err;
00218                 else
00219                     pred = Rb + err;
00220             }
00221         } else { /* regular mode */
00222             int context, sign;
00223 
00224             context = ff_jpegls_quantize(state, D0) * 81 + ff_jpegls_quantize(state, D1) * 9 + ff_jpegls_quantize(state, D2);
00225             pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
00226 
00227             if(context < 0){
00228                 context = -context;
00229                 sign = 1;
00230             }else{
00231                 sign = 0;
00232             }
00233 
00234             if(sign){
00235                 pred = av_clip(pred - state->C[context], 0, state->maxval);
00236                 err = -ls_get_code_regular(&s->gb, state, context);
00237             } else {
00238                 pred = av_clip(pred + state->C[context], 0, state->maxval);
00239                 err = ls_get_code_regular(&s->gb, state, context);
00240             }
00241 
00242             /* we have to do something more for near-lossless coding */
00243             pred += err;
00244         }
00245         if(state->near){
00246             if(pred < -state->near)
00247                 pred += state->range * state->twonear;
00248             else if(pred > state->maxval + state->near)
00249                 pred -= state->range * state->twonear;
00250             pred = av_clip(pred, 0, state->maxval);
00251         }
00252 
00253         pred &= state->maxval;
00254         W(dst, x, pred);
00255         x += stride;
00256     }
00257 }
00258 
00259 int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv){
00260     int i, t = 0;
00261     uint8_t *zero, *last, *cur;
00262     JLSState *state;
00263     int off = 0, stride = 1, width, shift;
00264 
00265     zero = av_mallocz(s->picture.linesize[0]);
00266     last = zero;
00267     cur = s->picture.data[0];
00268 
00269     state = av_mallocz(sizeof(JLSState));
00270     /* initialize JPEG-LS state from JPEG parameters */
00271     state->near = near;
00272     state->bpp = (s->bits < 2) ? 2 : s->bits;
00273     state->maxval = s->maxval;
00274     state->T1 = s->t1;
00275     state->T2 = s->t2;
00276     state->T3 = s->t3;
00277     state->reset = s->reset;
00278     ff_jpegls_reset_coding_parameters(state, 0);
00279     ff_jpegls_init_state(state);
00280 
00281     if(s->bits <= 8)
00282         shift = point_transform + (8 - s->bits);
00283     else
00284         shift = point_transform + (16 - s->bits);
00285 
00286 //    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);
00287 //    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);
00288     if(ilv == 0) { /* separate planes */
00289         off = s->cur_scan - 1;
00290         stride = (s->nb_components > 1) ? 3 : 1;
00291         width = s->width * stride;
00292         cur += off;
00293         for(i = 0; i < s->height; i++) {
00294             if(s->bits <= 8){
00295                 ls_decode_line(state, s, last, cur, t, width, stride, off,  8);
00296                 t = last[0];
00297             }else{
00298                 ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
00299                 t = *((uint16_t*)last);
00300             }
00301             last = cur;
00302             cur += s->picture.linesize[0];
00303 
00304             if (s->restart_interval && !--s->restart_count) {
00305                 align_get_bits(&s->gb);
00306                 skip_bits(&s->gb, 16); /* skip RSTn */
00307             }
00308         }
00309     } else if(ilv == 1) { /* line interleaving */
00310         int j;
00311         int Rc[3] = {0, 0, 0};
00312         memset(cur, 0, s->picture.linesize[0]);
00313         width = s->width * 3;
00314         for(i = 0; i < s->height; i++) {
00315             for(j = 0; j < 3; j++) {
00316                 ls_decode_line(state, s, last + j, cur + j, Rc[j], width, 3, j, 8);
00317                 Rc[j] = last[j];
00318 
00319                 if (s->restart_interval && !--s->restart_count) {
00320                     align_get_bits(&s->gb);
00321                     skip_bits(&s->gb, 16); /* skip RSTn */
00322                 }
00323             }
00324             last = cur;
00325             cur += s->picture.linesize[0];
00326         }
00327     } else if(ilv == 2) { /* sample interleaving */
00328         av_log(s->avctx, AV_LOG_ERROR, "Sample interleaved images are not supported.\n");
00329         av_free(state);
00330         av_free(zero);
00331         return -1;
00332     }
00333 
00334     if(shift){ /* we need to do point transform or normalize samples */
00335         int x, w;
00336 
00337         w = s->width * s->nb_components;
00338 
00339         if(s->bits <= 8){
00340             uint8_t *src = s->picture.data[0];
00341 
00342             for(i = 0; i < s->height; i++){
00343                 for(x = off; x < w; x+= stride){
00344                     src[x] <<= shift;
00345                 }
00346                 src += s->picture.linesize[0];
00347             }
00348         }else{
00349             uint16_t *src = (uint16_t*) s->picture.data[0];
00350 
00351             for(i = 0; i < s->height; i++){
00352                 for(x = 0; x < w; x++){
00353                     src[x] <<= shift;
00354                 }
00355                 src += s->picture.linesize[0]/2;
00356             }
00357         }
00358     }
00359     av_free(state);
00360     av_free(zero);
00361 
00362     return 0;
00363 }
00364 
00365 
00366 AVCodec jpegls_decoder = {
00367     "jpegls",
00368     AVMEDIA_TYPE_VIDEO,
00369     CODEC_ID_JPEGLS,
00370     sizeof(MJpegDecodeContext),
00371     ff_mjpeg_decode_init,
00372     NULL,
00373     ff_mjpeg_decode_end,
00374     ff_mjpeg_decode_frame,
00375     CODEC_CAP_DR1,
00376     .long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),
00377 };

Generated on Fri Sep 16 2011 17:17:38 for FFmpeg by  doxygen 1.7.1