jpeglsdec.c
Go to the documentation of this file.
1 /*
2  * JPEG-LS decoder
3  * Copyright (c) 2003 Michael Niedermayer
4  * Copyright (c) 2006 Konstantin Shishkov
5  *
6  * This file is part of Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
28 #include "avcodec.h"
29 #include "get_bits.h"
30 #include "golomb.h"
31 #include "mathops.h"
32 #include "mjpeg.h"
33 #include "mjpegdec.h"
34 #include "jpegls.h"
35 #include "jpeglsdec.h"
36 
37 
38 /*
39 * Uncomment this to significantly speed up decoding of broken JPEG-LS
40 * (or test broken JPEG-LS decoder) and slow down ordinary decoding a bit.
41 *
42 * There is no Golomb code with length >= 32 bits possible, so check and
43 * avoid situation of 32 zeros, Libav Golomb decoder is painfully slow
44 * on this errors.
45 */
46 //#define JLS_BROKEN
47 
48 
53 {
54  int len, id;
55 
56  /* XXX: verify len field validity */
57  len = get_bits(&s->gb, 16);
58  id = get_bits(&s->gb, 8);
59 
60  switch(id){
61  case 1:
62  s->maxval= get_bits(&s->gb, 16);
63  s->t1= get_bits(&s->gb, 16);
64  s->t2= get_bits(&s->gb, 16);
65  s->t3= get_bits(&s->gb, 16);
66  s->reset= get_bits(&s->gb, 16);
67 
68 // ff_jpegls_reset_coding_parameters(s, 0);
69  //FIXME quant table?
70  break;
71  case 2:
72  case 3:
73  av_log(s->avctx, AV_LOG_ERROR, "palette not supported\n");
74  return AVERROR(ENOSYS);
75  case 4:
76  av_log(s->avctx, AV_LOG_ERROR, "oversize image not supported\n");
77  return AVERROR(ENOSYS);
78  default:
79  av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
80  return AVERROR_INVALIDDATA;
81  }
82 // av_log(s->avctx, AV_LOG_DEBUG, "ID=%i, T=%i,%i,%i\n", id, s->t1, s->t2, s->t3);
83 
84  return 0;
85 }
86 
90 static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q){
91  int k, ret;
92 
93  for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
94 
95 #ifdef JLS_BROKEN
96  if(!show_bits_long(gb, 32))return -1;
97 #endif
98  ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
99 
100  /* decode mapped error */
101  if(ret & 1)
102  ret = -((ret + 1) >> 1);
103  else
104  ret >>= 1;
105 
106  /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
107  if(!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
108  ret = -(ret + 1);
109 
110  ret= ff_jpegls_update_state_regular(state, Q, ret);
111 
112  return ret;
113 }
114 
118 static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state, int RItype, int limit_add){
119  int k, ret, temp, map;
120  int Q = 365 + RItype;
121 
122  temp= state->A[Q];
123  if(RItype)
124  temp += state->N[Q] >> 1;
125 
126  for(k = 0; (state->N[Q] << k) < temp; k++);
127 
128 #ifdef JLS_BROKEN
129  if(!show_bits_long(gb, 32))return -1;
130 #endif
131  ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1, state->qbpp);
132 
133  /* decode mapped error */
134  map = 0;
135  if(!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
136  map = 1;
137  ret += RItype + map;
138 
139  if(ret & 1){
140  ret = map - ((ret + 1) >> 1);
141  state->B[Q]++;
142  } else {
143  ret = ret >> 1;
144  }
145 
146  /* update state */
147  state->A[Q] += FFABS(ret) - RItype;
148  ret *= state->twonear;
149  ff_jpegls_downscale_state(state, Q);
150 
151  return ret;
152 }
153 
157 static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s, void *last, void *dst, int last2, int w, int stride, int comp, int bits){
158  int i, x = 0;
159  int Ra, Rb, Rc, Rd;
160  int D0, D1, D2;
161 
162  while(x < w) {
163  int err, pred;
164 
165  /* compute gradients */
166  Ra = x ? R(dst, x - stride) : R(last, x);
167  Rb = R(last, x);
168  Rc = x ? R(last, x - stride) : last2;
169  Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
170  D0 = Rd - Rb;
171  D1 = Rb - Rc;
172  D2 = Rc - Ra;
173  /* run mode */
174  if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
175  int r;
176  int RItype;
177 
178  /* decode full runs while available */
179  while(get_bits1(&s->gb)) {
180  int r;
181  r = 1 << ff_log2_run[state->run_index[comp]];
182  if(x + r * stride > w) {
183  r = (w - x) / stride;
184  }
185  for(i = 0; i < r; i++) {
186  W(dst, x, Ra);
187  x += stride;
188  }
189  /* if EOL reached, we stop decoding */
190  if(r != (1 << ff_log2_run[state->run_index[comp]]))
191  return;
192  if(state->run_index[comp] < 31)
193  state->run_index[comp]++;
194  if(x + stride > w)
195  return;
196  }
197  /* decode aborted run */
198  r = ff_log2_run[state->run_index[comp]];
199  if(r)
200  r = get_bits_long(&s->gb, r);
201  for(i = 0; i < r; i++) {
202  W(dst, x, Ra);
203  x += stride;
204  }
205 
206  /* decode run termination value */
207  Rb = R(last, x);
208  RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
209  err = ls_get_code_runterm(&s->gb, state, RItype, ff_log2_run[state->run_index[comp]]);
210  if(state->run_index[comp])
211  state->run_index[comp]--;
212 
213  if(state->near && RItype){
214  pred = Ra + err;
215  } else {
216  if(Rb < Ra)
217  pred = Rb - err;
218  else
219  pred = Rb + err;
220  }
221  } else { /* regular mode */
222  int context, sign;
223 
224  context = ff_jpegls_quantize(state, D0) * 81 + ff_jpegls_quantize(state, D1) * 9 + ff_jpegls_quantize(state, D2);
225  pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
226 
227  if(context < 0){
228  context = -context;
229  sign = 1;
230  }else{
231  sign = 0;
232  }
233 
234  if(sign){
235  pred = av_clip(pred - state->C[context], 0, state->maxval);
236  err = -ls_get_code_regular(&s->gb, state, context);
237  } else {
238  pred = av_clip(pred + state->C[context], 0, state->maxval);
239  err = ls_get_code_regular(&s->gb, state, context);
240  }
241 
242  /* we have to do something more for near-lossless coding */
243  pred += err;
244  }
245  if(state->near){
246  if(pred < -state->near)
247  pred += state->range * state->twonear;
248  else if(pred > state->maxval + state->near)
249  pred -= state->range * state->twonear;
250  pred = av_clip(pred, 0, state->maxval);
251  }
252 
253  pred &= state->maxval;
254  W(dst, x, pred);
255  x += stride;
256  }
257 }
258 
259 int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv){
260  int i, t = 0;
261  uint8_t *zero, *last, *cur;
262  JLSState *state;
263  int off = 0, stride = 1, width, shift, ret = 0;
264 
265  zero = av_mallocz(s->picture_ptr->linesize[0]);
266  last = zero;
267  cur = s->picture_ptr->data[0];
268 
269  state = av_mallocz(sizeof(JLSState));
270  /* initialize JPEG-LS state from JPEG parameters */
271  state->near = near;
272  state->bpp = (s->bits < 2) ? 2 : s->bits;
273  state->maxval = s->maxval;
274  state->T1 = s->t1;
275  state->T2 = s->t2;
276  state->T3 = s->t3;
277  state->reset = s->reset;
279  ff_jpegls_init_state(state);
280 
281  if(s->bits <= 8)
282  shift = point_transform + (8 - s->bits);
283  else
284  shift = point_transform + (16 - s->bits);
285 
286 // 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);
287 // 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);
288  if(ilv == 0) { /* separate planes */
289  if (s->cur_scan > s->nb_components) {
290  ret = AVERROR_INVALIDDATA;
291  goto end;
292  }
293  off = s->cur_scan - 1;
294  stride = (s->nb_components > 1) ? 3 : 1;
295  width = s->width * stride;
296  cur += off;
297  for(i = 0; i < s->height; i++) {
298  if(s->bits <= 8){
299  ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
300  t = last[0];
301  }else{
302  ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
303  t = *((uint16_t*)last);
304  }
305  last = cur;
306  cur += s->picture_ptr->linesize[0];
307 
308  if (s->restart_interval && !--s->restart_count) {
309  align_get_bits(&s->gb);
310  skip_bits(&s->gb, 16); /* skip RSTn */
311  }
312  }
313  } else if(ilv == 1) { /* line interleaving */
314  int j;
315  int Rc[3] = {0, 0, 0};
316  memset(cur, 0, s->picture_ptr->linesize[0]);
317  width = s->width * 3;
318  for(i = 0; i < s->height; i++) {
319  for(j = 0; j < 3; j++) {
320  ls_decode_line(state, s, last + j, cur + j, Rc[j], width, 3, j, 8);
321  Rc[j] = last[j];
322 
323  if (s->restart_interval && !--s->restart_count) {
324  align_get_bits(&s->gb);
325  skip_bits(&s->gb, 16); /* skip RSTn */
326  }
327  }
328  last = cur;
329  cur += s->picture_ptr->linesize[0];
330  }
331  } else if (ilv == 2) { /* sample interleaving */
332  av_log(s->avctx, AV_LOG_ERROR, "Sample interleaved images are not supported.\n");
333  ret = AVERROR_PATCHWELCOME;
334  goto end;
335  }
336 
337  if(shift){ /* we need to do point transform or normalize samples */
338  int x, w;
339 
340  w = s->width * s->nb_components;
341 
342  if(s->bits <= 8){
343  uint8_t *src = s->picture_ptr->data[0];
344 
345  for(i = 0; i < s->height; i++){
346  for(x = off; x < w; x+= stride){
347  src[x] <<= shift;
348  }
349  src += s->picture_ptr->linesize[0];
350  }
351  }else{
352  uint16_t *src = (uint16_t*) s->picture_ptr->data[0];
353 
354  for(i = 0; i < s->height; i++){
355  for(x = 0; x < w; x++){
356  src[x] <<= shift;
357  }
358  src += s->picture_ptr->linesize[0]/2;
359  }
360  }
361  }
362 
363 end:
364  av_free(state);
365  av_free(zero);
366 
367  return ret;
368 }
369 
370 
372  .name = "jpegls",
373  .type = AVMEDIA_TYPE_VIDEO,
374  .id = CODEC_ID_JPEGLS,
375  .priv_data_size = sizeof(MJpegDecodeContext),
379  .capabilities = CODEC_CAP_DR1,
380  .long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),
381 };
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
Definition: get_bits.h:327
int reset
Definition: jpegls.h:41
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:154
const uint8_t ff_log2_run[41]
Definition: bitstream.c:35
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int T2
Definition: jpegls.h:39
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:237
static void align_get_bits(GetBitContext *s)
Definition: get_bits.h:378
int C[365]
Definition: jpegls.h:40
int width
Definition: rotozoom.c:164
int twonear
Definition: jpegls.h:42
static int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len)
read unsigned golomb rice code (jpegls).
Definition: golomb.h:285
int limit
Definition: jpegls.h:41
int stride
Definition: mace.c:143
AVCodec.
Definition: avcodec.h:3189
JPEG-LS decoder.
MJPEG encoder and decoder.
int bpp
Definition: jpegls.h:41
int maxval
Definition: jpegls.h:41
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
uint8_t bits
Definition: crc.c:31
AVFrame * picture_ptr
Definition: mjpegdec.h:87
#define R
Definition: dsputil.c:1977
bitstream reader API header.
static float t
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:336
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:719
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:137
#define AVERROR(e)
Definition: error.h:43
av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
Definition: mjpegdec.c:1655
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
#define zero
Definition: regdef.h:64
int run_index[3]
Definition: jpegls.h:43
void ff_jpegls_reset_coding_parameters(JLSState *s, int reset_all)
Calculate JPEG-LS codec values.
Definition: jpegls.c:59
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
const char * name
Name of the codec implementation.
Definition: avcodec.h:3196
static int ff_jpegls_update_state_regular(JLSState *state, int Q, int err)
Definition: jpegls.h:88
int B[367]
Definition: jpegls.h:40
int ff_jpegls_decode_lse(MJpegDecodeContext *s)
Decode LSE block with initialization parameters.
Definition: jpeglsdec.c:52
int off
Definition: dsputil_bfin.c:28
int near
Definition: jpegls.h:42
#define FFABS(a)
Definition: common.h:50
GetBitContext gb
Definition: mjpegdec.h:43
static const float pred[4]
Definition: siprdata.h:259
int qbpp
Definition: jpegls.h:41
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
Definition: error.h:57
external API header
static void ls_decode_line(JLSState *state, MJpegDecodeContext *s, void *last, void *dst, int last2, int w, int stride, int comp, int bits)
Decode one line of image.
Definition: jpeglsdec.c:157
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:327
int A[367]
Definition: jpegls.h:40
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:268
int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv)
Definition: jpeglsdec.c:259
struct MJpegDecodeContext MJpegDecodeContext
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:260
static int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q)
Get context-dependent Golomb code, decode it and update context.
Definition: jpeglsdec.c:90
#define W(a, i, v)
Definition: jpegls.h:109
JPEG-LS common code.
int T1
Definition: jpegls.h:39
int range
Definition: jpegls.h:41
int N[367]
Definition: jpegls.h:40
#define mid_pred
Definition: mathops.h:88
int reset
context halfing intervall ?rename
Definition: mjpegdec.h:68
enum CodecID id
Definition: mxfenc.c:85
AVCodec ff_jpegls_decoder
Definition: jpeglsdec.c:371
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:301
static uint32_t state
Definition: trasher.c:25
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
Definition: mjpegdec.c:85
int height
Definition: gxfenc.c:73
#define r(n)
Definition: regs.h:32
static int ls_get_code_runterm(GetBitContext *gb, JLSState *state, int RItype, int limit_add)
Get Golomb code, decode it and update state for run termination.
Definition: jpeglsdec.c:118
AVCodecContext * avctx
Definition: mjpegdec.h:42
static int ff_jpegls_quantize(JLSState *s, int v)
Calculate quantized gradient value, used for context determination.
Definition: jpegls.h:56
int len
static void comp(unsigned char *dst, int dst_stride, unsigned char *src, int src_stride, int add)
Definition: eamad.c:75
int T3
Definition: jpegls.h:39
void ff_jpegls_init_state(JLSState *state)
Calculate initial JPEG-LS parameters.
Definition: jpegls.c:30
MJPEG decoder.
exp golomb vlc stuff
int ff_mjpeg_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
Definition: mjpegdec.c:1463
static void ff_jpegls_downscale_state(JLSState *state, int Q)
Definition: jpegls.h:79