libavcodec/jpeglsdec.c
6ba04c2b
 /*
  * 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
  */
 
 /**
ba87f080
  * @file
6ba04c2b
  * JPEG-LS decoder.
  */
 
 #include "avcodec.h"
9106a698
 #include "get_bits.h"
6ba04c2b
 #include "golomb.h"
199436b9
 #include "mathops.h"
6ba04c2b
 #include "mjpeg.h"
5acd8d9e
 #include "mjpegdec.h"
6ba04c2b
 #include "jpegls.h"
 #include "jpeglsdec.h"
 
 /*
ff4fc5ef
  * 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
39354d60
  * avoid situation of 32 zeros, FFmpeg Golomb decoder is painfully slow
ff4fc5ef
  * on this errors.
  */
6ba04c2b
 //#define JLS_BROKEN
 
 /**
  * Decode LSE block with initialization parameters
  */
 int ff_jpegls_decode_lse(MJpegDecodeContext *s)
 {
727af82a
     int id;
6ba04c2b
 
727af82a
     skip_bits(&s->gb, 16);  /* length: FIXME: verify field validity */
6ba04c2b
     id = get_bits(&s->gb, 8);
 
ff4fc5ef
     switch (id) {
6ba04c2b
     case 1:
ff4fc5ef
         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);
6ba04c2b
 
 //        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");
a5a0ef5e
         return AVERROR(ENOSYS);
6ba04c2b
     case 4:
         av_log(s->avctx, AV_LOG_ERROR, "oversize image not supported\n");
a5a0ef5e
         return AVERROR(ENOSYS);
6ba04c2b
     default:
         av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
a5a0ef5e
         return AVERROR_INVALIDDATA;
6ba04c2b
     }
1218777f
     av_dlog(s->avctx, "ID=%i, T=%i,%i,%i\n", id, s->t1, s->t2, s->t3);
6ba04c2b
 
     return 0;
 }
 
 /**
  * Get context-dependent Golomb code, decode it and update context
  */
ff4fc5ef
 static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q)
 {
6ba04c2b
     int k, ret;
 
ff4fc5ef
     for (k = 0; (state->N[Q] << k) < state->A[Q]; k++)
         ;
6ba04c2b
 
 #ifdef JLS_BROKEN
ff4fc5ef
     if (!show_bits_long(gb, 32))
         return -1;
6ba04c2b
 #endif
     ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
 
     /* decode mapped error */
ff4fc5ef
     if (ret & 1)
9cacdabd
         ret = -(ret + 1 >> 1);
6ba04c2b
     else
         ret >>= 1;
 
     /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
ff4fc5ef
     if (!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
6ba04c2b
         ret = -(ret + 1);
 
ff4fc5ef
     ret = ff_jpegls_update_state_regular(state, Q, ret);
6ba04c2b
 
     return ret;
 }
 
 /**
  * Get Golomb code, decode it and update state for run termination
  */
ff4fc5ef
 static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state,
                                       int RItype, int limit_add)
 {
6ba04c2b
     int k, ret, temp, map;
     int Q = 365 + RItype;
 
ff4fc5ef
     temp = state->A[Q];
     if (RItype)
6ba04c2b
         temp += state->N[Q] >> 1;
 
ff4fc5ef
     for (k = 0; (state->N[Q] << k) < temp; k++)
         ;
6ba04c2b
 
 #ifdef JLS_BROKEN
ff4fc5ef
     if (!show_bits_long(gb, 32))
         return -1;
6ba04c2b
 #endif
ff4fc5ef
     ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1,
                                state->qbpp);
6ba04c2b
 
     /* decode mapped error */
     map = 0;
ff4fc5ef
     if (!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
6ba04c2b
         map = 1;
     ret += RItype + map;
 
ff4fc5ef
     if (ret & 1) {
9cacdabd
         ret = map - (ret + 1 >> 1);
6ba04c2b
         state->B[Q]++;
     } else {
         ret = ret >> 1;
     }
 
5c1e9d37
     if(FFABS(ret) > 0xFFFF)
         return -0x10000;
6ba04c2b
     /* update state */
     state->A[Q] += FFABS(ret) - RItype;
ff4fc5ef
     ret         *= state->twonear;
6ba04c2b
     ff_jpegls_downscale_state(state, Q);
 
     return ret;
 }
 
 /**
  * Decode one line of image
  */
ff4fc5ef
 static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s,
                                   void *last, void *dst, int last2, int w,
                                   int stride, int comp, int bits)
 {
6ba04c2b
     int i, x = 0;
     int Ra, Rb, Rc, Rd;
     int D0, D1, D2;
 
ff4fc5ef
     while (x < w) {
6ba04c2b
         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 */
ff4fc5ef
         if ((FFABS(D0) <= state->near) &&
             (FFABS(D1) <= state->near) &&
             (FFABS(D2) <= state->near)) {
6ba04c2b
             int r;
             int RItype;
 
             /* decode full runs while available */
ff4fc5ef
             while (get_bits1(&s->gb)) {
6ba04c2b
                 int r;
                 r = 1 << ff_log2_run[state->run_index[comp]];
ff4fc5ef
                 if (x + r * stride > w)
6ba04c2b
                     r = (w - x) / stride;
ff4fc5ef
                 for (i = 0; i < r; i++) {
6ba04c2b
                     W(dst, x, Ra);
                     x += stride;
                 }
                 /* if EOL reached, we stop decoding */
9cacdabd
                 if (r != 1 << ff_log2_run[state->run_index[comp]])
6ba04c2b
                     return;
ff4fc5ef
                 if (state->run_index[comp] < 31)
6ba04c2b
                     state->run_index[comp]++;
ff4fc5ef
                 if (x + stride > w)
6ba04c2b
                     return;
             }
             /* decode aborted run */
             r = ff_log2_run[state->run_index[comp]];
ff4fc5ef
             if (r)
6ba04c2b
                 r = get_bits_long(&s->gb, r);
39354d60
             if (x + r * stride > w) {
00ab9cda
                 r = (w - x) / stride;
             }
ff4fc5ef
             for (i = 0; i < r; i++) {
6ba04c2b
                 W(dst, x, Ra);
                 x += stride;
             }
 
             /* decode run termination value */
ff4fc5ef
             Rb     = R(last, x);
6ba04c2b
             RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
ff4fc5ef
             err    = ls_get_code_runterm(&s->gb, state, RItype,
                                          ff_log2_run[state->run_index[comp]]);
             if (state->run_index[comp])
6ba04c2b
                 state->run_index[comp]--;
 
ff4fc5ef
             if (state->near && RItype) {
6ba04c2b
                 pred = Ra + err;
             } else {
ff4fc5ef
                 if (Rb < Ra)
6ba04c2b
                     pred = Rb - err;
                 else
                     pred = Rb + err;
             }
         } else { /* regular mode */
             int context, sign;
 
ff4fc5ef
             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);
6ba04c2b
 
ff4fc5ef
             if (context < 0) {
6ba04c2b
                 context = -context;
ff4fc5ef
                 sign    = 1;
             } else {
6ba04c2b
                 sign = 0;
             }
 
ff4fc5ef
             if (sign) {
6ba04c2b
                 pred = av_clip(pred - state->C[context], 0, state->maxval);
ff4fc5ef
                 err  = -ls_get_code_regular(&s->gb, state, context);
6ba04c2b
             } else {
                 pred = av_clip(pred + state->C[context], 0, state->maxval);
ff4fc5ef
                 err  = ls_get_code_regular(&s->gb, state, context);
6ba04c2b
             }
 
             /* we have to do something more for near-lossless coding */
             pred += err;
         }
ff4fc5ef
         if (state->near) {
             if (pred < -state->near)
6ba04c2b
                 pred += state->range * state->twonear;
ff4fc5ef
             else if (pred > state->maxval + state->near)
6ba04c2b
                 pred -= state->range * state->twonear;
             pred = av_clip(pred, 0, state->maxval);
         }
 
         pred &= state->maxval;
         W(dst, x, pred);
         x += stride;
     }
 }
 
ff4fc5ef
 int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near,
                              int point_transform, int ilv)
 {
6ba04c2b
     int i, t = 0;
     uint8_t *zero, *last, *cur;
     JLSState *state;
4a4107b4
     int off = 0, stride = 1, width, shift, ret = 0;
6ba04c2b
 
f448478a
     zero = av_mallocz(s->picture_ptr->linesize[0]);
6ba04c2b
     last = zero;
f448478a
     cur  = s->picture_ptr->data[0];
6ba04c2b
 
     state = av_mallocz(sizeof(JLSState));
     /* initialize JPEG-LS state from JPEG parameters */
ff4fc5ef
     state->near   = near;
     state->bpp    = (s->bits < 2) ? 2 : s->bits;
6ba04c2b
     state->maxval = s->maxval;
ff4fc5ef
     state->T1     = s->t1;
     state->T2     = s->t2;
     state->T3     = s->t3;
     state->reset  = s->reset;
6ba04c2b
     ff_jpegls_reset_coding_parameters(state, 0);
     ff_jpegls_init_state(state);
 
ff4fc5ef
     if (s->bits <= 8)
6ba04c2b
         shift = point_transform + (8 - s->bits);
     else
         shift = point_transform + (16 - s->bits);
 
c39916bc
     if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
39354d60
         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",
c39916bc
                 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);
     }
ff4fc5ef
     if (ilv == 0) { /* separate planes */
abad3749
         if (s->cur_scan > s->nb_components) {
             ret = AVERROR_INVALIDDATA;
             goto end;
         }
6ba04c2b
         stride = (s->nb_components > 1) ? 3 : 1;
39354d60
         off    = av_clip(s->cur_scan - 1, 0, stride - 1);
ff4fc5ef
         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);
6ba04c2b
                 t = last[0];
ff4fc5ef
             } else {
6ba04c2b
                 ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
ff4fc5ef
                 t = *((uint16_t *)last);
6ba04c2b
             }
             last = cur;
f448478a
             cur += s->picture_ptr->linesize[0];
6ba04c2b
 
             if (s->restart_interval && !--s->restart_count) {
                 align_get_bits(&s->gb);
                 skip_bits(&s->gb, 16); /* skip RSTn */
             }
         }
ff4fc5ef
     } else if (ilv == 1) { /* line interleaving */
6ba04c2b
         int j;
ff4fc5ef
         int Rc[3] = { 0, 0, 0 };
2d6f317d
         stride = (s->nb_components > 1) ? 3 : 1;
f448478a
         memset(cur, 0, s->picture_ptr->linesize[0]);
2d6f317d
         width = s->width * stride;
ff4fc5ef
         for (i = 0; i < s->height; i++) {
39354d60
             for (j = 0; j < stride; j++) {
ff4fc5ef
                 ls_decode_line(state, s, last + j, cur + j,
39354d60
                                Rc[j], width, stride, j, 8);
6ba04c2b
                 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;
f448478a
             cur += s->picture_ptr->linesize[0];
6ba04c2b
         }
ff4fc5ef
     } else if (ilv == 2) { /* sample interleaving */
a5a0ef5e
         avpriv_report_missing_feature(s->avctx, "Sample interleaved images");
4a4107b4
         ret = AVERROR_PATCHWELCOME;
         goto end;
6ba04c2b
     }
 
8024b488
     if (s->xfrm && s->nb_components == 3) {
         int x, w;
 
         w = s->width * s->nb_components;
 
         if (s->bits <= 8) {
f448478a
             uint8_t *src = s->picture_ptr->data[0];
8024b488
 
             for (i = 0; i < s->height; i++) {
                 switch(s->xfrm) {
                 case 1:
                     for (x = off; x < w; x += 3) {
                         src[x  ] += src[x+1] + 128;
                         src[x+2] += src[x+1] + 128;
                     }
                     break;
                 case 2:
                     for (x = off; x < w; x += 3) {
                         src[x  ] += src[x+1] + 128;
                         src[x+2] += ((src[x  ] + src[x+1])>>1) + 128;
                     }
                     break;
                 case 3:
                     for (x = off; x < w; x += 3) {
                         int g = src[x+0] - ((src[x+2]+src[x+1])>>2) + 64;
                         src[x+0] = src[x+2] + g + 128;
                         src[x+2] = src[x+1] + g + 128;
                         src[x+1] = g;
                     }
                     break;
8aea97a4
                 case 4:
                     for (x = off; x < w; x += 3) {
                         int r    = src[x+0] - ((                       359 * (src[x+2]-128) + 490) >> 8);
                         int g    = src[x+0] - (( 88 * (src[x+1]-128) - 183 * (src[x+2]-128) +  30) >> 8);
                         int b    = src[x+0] + ((454 * (src[x+1]-128)                        + 574) >> 8);
                         src[x+0] = av_clip_uint8(r);
                         src[x+1] = av_clip_uint8(g);
                         src[x+2] = av_clip_uint8(b);
                     }
                     break;
8024b488
                 }
f448478a
                 src += s->picture_ptr->linesize[0];
8024b488
             }
         }else
             avpriv_report_missing_feature(s->avctx, "16bit xfrm");
     }
 
ff4fc5ef
     if (shift) { /* we need to do point transform or normalize samples */
6ba04c2b
         int x, w;
 
         w = s->width * s->nb_components;
 
ff4fc5ef
         if (s->bits <= 8) {
f448478a
             uint8_t *src = s->picture_ptr->data[0];
6ba04c2b
 
ff4fc5ef
             for (i = 0; i < s->height; i++) {
                 for (x = off; x < w; x += stride)
6ba04c2b
                     src[x] <<= shift;
f448478a
                 src += s->picture_ptr->linesize[0];
6ba04c2b
             }
ff4fc5ef
         } else {
f448478a
             uint16_t *src = (uint16_t *)s->picture_ptr->data[0];
6ba04c2b
 
ff4fc5ef
             for (i = 0; i < s->height; i++) {
                 for (x = 0; x < w; x++)
6ba04c2b
                     src[x] <<= shift;
f448478a
                 src += s->picture_ptr->linesize[0] / 2;
6ba04c2b
             }
         }
     }
4a4107b4
 
 end:
6ba04c2b
     av_free(state);
     av_free(zero);
 
4a4107b4
     return ret;
6ba04c2b
 }
5acd8d9e
 
e7e2df27
 AVCodec ff_jpegls_decoder = {
ec6402b7
     .name           = "jpegls",
ff4fc5ef
     .long_name      = NULL_IF_CONFIG_SMALL("JPEG-LS"),
ec6402b7
     .type           = AVMEDIA_TYPE_VIDEO,
36ef5369
     .id             = AV_CODEC_ID_JPEGLS,
ec6402b7
     .priv_data_size = sizeof(MJpegDecodeContext),
     .init           = ff_mjpeg_decode_init,
     .close          = ff_mjpeg_decode_end,
     .decode         = ff_mjpeg_decode_frame,
     .capabilities   = CODEC_CAP_DR1,
5acd8d9e
 };