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 FFmpeg.
7  *
8  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 /**
24  * @file
25  * JPEG-LS decoder.
26  */
27 
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, FFmpeg Golomb decoder is painfully slow
44 * on this errors.
45 */
46 //#define JLS_BROKEN
47 
48 
49 /**
50  * Decode LSE block with initialization parameters
51  */
53 {
54  int id;
55 
56  skip_bits(&s->gb, 16); /* length: FIXME: verify field validity */
57  id = get_bits(&s->gb, 8);
58 
59  switch(id){
60  case 1:
61  s->maxval= get_bits(&s->gb, 16);
62  s->t1= get_bits(&s->gb, 16);
63  s->t2= get_bits(&s->gb, 16);
64  s->t3= get_bits(&s->gb, 16);
65  s->reset= get_bits(&s->gb, 16);
66 
67 // ff_jpegls_reset_coding_parameters(s, 0);
68  //FIXME quant table?
69  break;
70  case 2:
71  case 3:
72  av_log(s->avctx, AV_LOG_ERROR, "palette not supported\n");
73  return -1;
74  case 4:
75  av_log(s->avctx, AV_LOG_ERROR, "oversize image not supported\n");
76  return -1;
77  default:
78  av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
79  return -1;
80  }
81  av_dlog(s->avctx, "ID=%i, T=%i,%i,%i\n", id, s->t1, s->t2, s->t3);
82 
83  return 0;
84 }
85 
86 /**
87  * Get context-dependent Golomb code, decode it and update context
88  */
89 static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q){
90  int k, ret;
91 
92  for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
93 
94 #ifdef JLS_BROKEN
95  if(!show_bits_long(gb, 32))return -1;
96 #endif
97  ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
98 
99  /* decode mapped error */
100  if(ret & 1)
101  ret = -((ret + 1) >> 1);
102  else
103  ret >>= 1;
104 
105  /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
106  if(!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
107  ret = -(ret + 1);
108 
109  ret= ff_jpegls_update_state_regular(state, Q, ret);
110 
111  return ret;
112 }
113 
114 /**
115  * Get Golomb code, decode it and update state for run termination
116  */
117 static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state, int RItype, int limit_add){
118  int k, ret, temp, map;
119  int Q = 365 + RItype;
120 
121  temp= state->A[Q];
122  if(RItype)
123  temp += state->N[Q] >> 1;
124 
125  for(k = 0; (state->N[Q] << k) < temp; k++);
126 
127 #ifdef JLS_BROKEN
128  if(!show_bits_long(gb, 32))return -1;
129 #endif
130  ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1, state->qbpp);
131 
132  /* decode mapped error */
133  map = 0;
134  if(!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
135  map = 1;
136  ret += RItype + map;
137 
138  if(ret & 1){
139  ret = map - ((ret + 1) >> 1);
140  state->B[Q]++;
141  } else {
142  ret = ret >> 1;
143  }
144 
145  /* update state */
146  state->A[Q] += FFABS(ret) - RItype;
147  ret *= state->twonear;
148  ff_jpegls_downscale_state(state, Q);
149 
150  return ret;
151 }
152 
153 /**
154  * Decode one line of image
155  */
156 static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s, void *last, void *dst, int last2, int w, int stride, int comp, int bits){
157  int i, x = 0;
158  int Ra, Rb, Rc, Rd;
159  int D0, D1, D2;
160 
161  while(x < w) {
162  int err, pred;
163 
164  /* compute gradients */
165  Ra = x ? R(dst, x - stride) : R(last, x);
166  Rb = R(last, x);
167  Rc = x ? R(last, x - stride) : last2;
168  Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
169  D0 = Rd - Rb;
170  D1 = Rb - Rc;
171  D2 = Rc - Ra;
172  /* run mode */
173  if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
174  int r;
175  int RItype;
176 
177  /* decode full runs while available */
178  while(get_bits1(&s->gb)) {
179  int r;
180  r = 1 << ff_log2_run[state->run_index[comp]];
181  if(x + r * stride > w) {
182  r = (w - x) / stride;
183  }
184  for(i = 0; i < r; i++) {
185  W(dst, x, Ra);
186  x += stride;
187  }
188  /* if EOL reached, we stop decoding */
189  if(r != (1 << ff_log2_run[state->run_index[comp]]))
190  return;
191  if(state->run_index[comp] < 31)
192  state->run_index[comp]++;
193  if(x + stride > w)
194  return;
195  }
196  /* decode aborted run */
197  r = ff_log2_run[state->run_index[comp]];
198  if(r)
199  r = get_bits_long(&s->gb, r);
200  if(x + r * stride > w) {
201  r = (w - x) / stride;
202  }
203  for(i = 0; i < r; i++) {
204  W(dst, x, Ra);
205  x += stride;
206  }
207 
208  /* decode run termination value */
209  Rb = R(last, x);
210  RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
211  err = ls_get_code_runterm(&s->gb, state, RItype, ff_log2_run[state->run_index[comp]]);
212  if(state->run_index[comp])
213  state->run_index[comp]--;
214 
215  if(state->near && RItype){
216  pred = Ra + err;
217  } else {
218  if(Rb < Ra)
219  pred = Rb - err;
220  else
221  pred = Rb + err;
222  }
223  } else { /* regular mode */
224  int context, sign;
225 
226  context = ff_jpegls_quantize(state, D0) * 81 + ff_jpegls_quantize(state, D1) * 9 + ff_jpegls_quantize(state, D2);
227  pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
228 
229  if(context < 0){
230  context = -context;
231  sign = 1;
232  }else{
233  sign = 0;
234  }
235 
236  if(sign){
237  pred = av_clip(pred - state->C[context], 0, state->maxval);
238  err = -ls_get_code_regular(&s->gb, state, context);
239  } else {
240  pred = av_clip(pred + state->C[context], 0, state->maxval);
241  err = ls_get_code_regular(&s->gb, state, context);
242  }
243 
244  /* we have to do something more for near-lossless coding */
245  pred += err;
246  }
247  if(state->near){
248  if(pred < -state->near)
249  pred += state->range * state->twonear;
250  else if(pred > state->maxval + state->near)
251  pred -= state->range * state->twonear;
252  pred = av_clip(pred, 0, state->maxval);
253  }
254 
255  pred &= state->maxval;
256  W(dst, x, pred);
257  x += stride;
258  }
259 }
260 
261 int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv){
262  int i, t = 0;
263  uint8_t *zero, *last, *cur;
264  JLSState *state;
265  int off = 0, stride = 1, width, shift;
266 
267  zero = av_mallocz(s->picture.linesize[0]);
268  last = zero;
269  cur = s->picture.data[0];
270 
271  state = av_mallocz(sizeof(JLSState));
272  /* initialize JPEG-LS state from JPEG parameters */
273  state->near = near;
274  state->bpp = (s->bits < 2) ? 2 : s->bits;
275  state->maxval = s->maxval;
276  state->T1 = s->t1;
277  state->T2 = s->t2;
278  state->T3 = s->t3;
279  state->reset = s->reset;
281  ff_jpegls_init_state(state);
282 
283  if(s->bits <= 8)
284  shift = point_transform + (8 - s->bits);
285  else
286  shift = point_transform + (16 - s->bits);
287 
288  if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
289  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",
290  s->width, s->height, state->near, state->maxval,
291  state->T1, state->T2, state->T3,
292  state->reset, state->limit, state->qbpp, state->range);
293  av_log(s->avctx, AV_LOG_DEBUG, "JPEG params: ILV=%i Pt=%i BPP=%i, scan = %i\n",
294  ilv, point_transform, s->bits, s->cur_scan);
295  }
296  if(ilv == 0) { /* separate planes */
297  stride = (s->nb_components > 1) ? 3 : 1;
298  off = av_clip(s->cur_scan - 1, 0, stride - 1);
299  width = s->width * stride;
300  cur += off;
301  for(i = 0; i < s->height; i++) {
302  if(s->bits <= 8){
303  ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
304  t = last[0];
305  }else{
306  ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
307  t = *((uint16_t*)last);
308  }
309  last = cur;
310  cur += s->picture.linesize[0];
311 
312  if (s->restart_interval && !--s->restart_count) {
313  align_get_bits(&s->gb);
314  skip_bits(&s->gb, 16); /* skip RSTn */
315  }
316  }
317  } else if(ilv == 1) { /* line interleaving */
318  int j;
319  int Rc[3] = {0, 0, 0};
320  stride = (s->nb_components > 1) ? 3 : 1;
321  memset(cur, 0, s->picture.linesize[0]);
322  width = s->width * stride;
323  for(i = 0; i < s->height; i++) {
324  for(j = 0; j < stride; j++) {
325  ls_decode_line(state, s, last + j, cur + j, Rc[j], width, stride, j, 8);
326  Rc[j] = last[j];
327 
328  if (s->restart_interval && !--s->restart_count) {
329  align_get_bits(&s->gb);
330  skip_bits(&s->gb, 16); /* skip RSTn */
331  }
332  }
333  last = cur;
334  cur += s->picture.linesize[0];
335  }
336  } else if(ilv == 2) { /* sample interleaving */
337  av_log(s->avctx, AV_LOG_ERROR, "Sample interleaved images are not supported.\n");
338  av_free(state);
339  av_free(zero);
340  return -1;
341  }
342 
343  if(shift){ /* we need to do point transform or normalize samples */
344  int x, w;
345 
346  w = s->width * s->nb_components;
347 
348  if(s->bits <= 8){
349  uint8_t *src = s->picture.data[0];
350 
351  for(i = 0; i < s->height; i++){
352  for(x = off; x < w; x+= stride){
353  src[x] <<= shift;
354  }
355  src += s->picture.linesize[0];
356  }
357  }else{
358  uint16_t *src = (uint16_t*) s->picture.data[0];
359 
360  for(i = 0; i < s->height; i++){
361  for(x = 0; x < w; x++){
362  src[x] <<= shift;
363  }
364  src += s->picture.linesize[0]/2;
365  }
366  }
367  }
368  av_free(state);
369  av_free(zero);
370 
371  return 0;
372 }
373 
374 
376  .name = "jpegls",
377  .type = AVMEDIA_TYPE_VIDEO,
378  .id = AV_CODEC_ID_JPEGLS,
379  .priv_data_size = sizeof(MJpegDecodeContext),
383  .capabilities = CODEC_CAP_DR1,
384  .long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),
385 };
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
Definition: get_bits.h:352
int reset
Definition: jpegls.h:42
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:205
const uint8_t ff_log2_run[41]
Definition: bitstream.c:37
const char * s
Definition: avisynth_c.h:668
int T2
Definition: jpegls.h:40
static int shift(int a, int b)
Definition: sonic.c:86
enum AVCodecID id
Definition: mxfenc.c:89
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
else temp
Definition: vf_mcdeint.c:148
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
AVFrame picture
Definition: mjpegdec.h:90
int C[365]
Definition: jpegls.h:41
int twonear
Definition: jpegls.h:43
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
static int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len)
read unsigned golomb rice code (jpegls).
Definition: golomb.h:294
int limit
Definition: jpegls.h:42
int stride
Definition: mace.c:144
JPEG-LS decoder.
MJPEG encoder and decoder.
output residual component w
int bpp
Definition: jpegls.h:42
int maxval
Definition: jpegls.h:42
uint8_t bits
Definition: crc.c:216
uint8_t
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
#define R
Definition: dsputil.c:2027
bitstream reader API header.
Discrete Time axis x
int ff_mjpeg_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: mjpegdec.c:1629
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
Definition: mjpegdec.c:1867
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
const char * r
Definition: vf_curves.c:94
#define zero
Definition: regdef.h:64
void ff_jpegls_reset_coding_parameters(JLSState *s, int reset_all)
Calculate JPEG-LS codec values.
Definition: jpegls.c:57
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
const char * name
Name of the codec implementation.
static int ff_jpegls_update_state_regular(JLSState *state, int Q, int err)
Definition: jpegls.h:89
int B[367]
Definition: jpegls.h:41
int ff_jpegls_decode_lse(MJpegDecodeContext *s)
Decode LSE block with initialization parameters.
Definition: jpeglsdec.c:52
external API header
int near
Definition: jpegls.h:43
ret
Definition: avfilter.c:821
t
Definition: genspecsines3.m:6
#define FFABS(a)
Definition: common.h:53
GetBitContext gb
Definition: mjpegdec.h:44
static const float pred[4]
Definition: siprdata.h:259
int qbpp
Definition: jpegls.h:42
for k
static int width
Definition: tests/utils.c:158
AVS_Value src
Definition: avisynth_c.h:523
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:156
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:375
int A[367]
Definition: jpegls.h:41
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:273
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv)
Definition: jpeglsdec.c:261
struct MJpegDecodeContext MJpegDecodeContext
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:265
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:89
JPEG-LS common code.
synthesis window for stochastic i
int T1
Definition: jpegls.h:40
int range
Definition: jpegls.h:42
int N[367]
Definition: jpegls.h:41
#define mid_pred
Definition: mathops.h:94
int reset
context halfing interval ?rename
Definition: mjpegdec.h:72
AVCodec ff_jpegls_decoder
Definition: jpeglsdec.c:375
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:306
static uint32_t state
Definition: trasher.c:27
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
#define FF_DEBUG_PICT_INFO
av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
Definition: mjpegdec.c:83
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:162
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:117
AVCodecContext * avctx
Definition: mjpegdec.h:43
static int ff_jpegls_quantize(JLSState *s, int v)
Calculate quantized gradient value, used for context determination.
Definition: jpegls.h:57
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:418
static void comp(unsigned char *dst, int dst_stride, unsigned char *src, int src_stride, int add)
Definition: eamad.c:71
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
int T3
Definition: jpegls.h:40
void ff_jpegls_init_state(JLSState *state)
Calculate initial JPEG-LS parameters.
Definition: jpegls.c:30
MJPEG decoder.
exp golomb vlc stuff
int run_index[4]
Definition: jpegls.h:44
static void ff_jpegls_downscale_state(JLSState *state, int Q)
Definition: jpegls.h:80
normalize window W
Definition: stft_peak.m:10