FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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  * Uncomment this to significantly speed up decoding of broken JPEG-LS
39  * (or test broken JPEG-LS decoder) and slow down ordinary decoding a bit.
40  *
41  * There is no Golomb code with length >= 32 bits possible, so check and
42  * avoid situation of 32 zeros, FFmpeg Golomb decoder is painfully slow
43  * on this errors.
44  */
45 //#define JLS_BROKEN
46 
47 /**
48  * Decode LSE block with initialization parameters
49  */
51 {
52  int id;
53  int tid, wt, maxtab, i, j;
54 
55  int len = get_bits(&s->gb, 16);
56  id = get_bits(&s->gb, 8);
57 
58  switch (id) {
59  case 1:
60  if (len < 13)
61  return AVERROR_INVALIDDATA;
62 
63  s->maxval = get_bits(&s->gb, 16);
64  s->t1 = get_bits(&s->gb, 16);
65  s->t2 = get_bits(&s->gb, 16);
66  s->t3 = get_bits(&s->gb, 16);
67  s->reset = get_bits(&s->gb, 16);
68 
69  if(s->avctx->debug & FF_DEBUG_PICT_INFO) {
70  av_log(s->avctx, AV_LOG_DEBUG, "Coding parameters maxval:%d T1:%d T2:%d T3:%d reset:%d\n",
71  s->maxval, s->t1, s->t2, s->t3, s->reset);
72  }
73 
74 // ff_jpegls_reset_coding_parameters(s, 0);
75  //FIXME quant table?
76  break;
77  case 2:
78  s->palette_index = 0;
79  case 3:
80  tid= get_bits(&s->gb, 8);
81  wt = get_bits(&s->gb, 8);
82 
83  if (len < 5)
84  return AVERROR_INVALIDDATA;
85 
86  if (wt < 1 || wt > MAX_COMPONENTS) {
87  avpriv_request_sample(s->avctx, "wt %d", wt);
88  return AVERROR_PATCHWELCOME;
89  }
90 
91  if (!s->maxval)
92  maxtab = 255;
93  else if ((5 + wt*(s->maxval+1)) < 65535)
94  maxtab = s->maxval;
95  else
96  maxtab = 65530/wt - 1;
97 
98  if(s->avctx->debug & FF_DEBUG_PICT_INFO) {
99  av_log(s->avctx, AV_LOG_DEBUG, "LSE palette %d tid:%d wt:%d maxtab:%d\n", id, tid, wt, maxtab);
100  }
101  if (maxtab >= 256) {
102  avpriv_request_sample(s->avctx, ">8bit palette");
103  return AVERROR_PATCHWELCOME;
104  }
105  maxtab = FFMIN(maxtab, (len - 5) / wt + s->palette_index);
106 
107  if (s->palette_index > maxtab)
108  return AVERROR_INVALIDDATA;
109 
110  if ((s->avctx->pix_fmt == AV_PIX_FMT_GRAY8 || s->avctx->pix_fmt == AV_PIX_FMT_PAL8) &&
112  uint32_t *pal = (uint32_t *)s->picture_ptr->data[1];
113  s->picture_ptr->format =
115  for (i=s->palette_index; i<=maxtab; i++) {
116  pal[i] = 0;
117  for (j=0; j<wt; j++) {
118  pal[i] |= get_bits(&s->gb, 8) << (8*(wt-j-1));
119  }
120  }
121  s->palette_index = i;
122  }
123  break;
124  case 4:
125  avpriv_request_sample(s->avctx, "oversize image");
126  return AVERROR(ENOSYS);
127  default:
128  av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
129  return AVERROR_INVALIDDATA;
130  }
131  av_dlog(s->avctx, "ID=%i, T=%i,%i,%i\n", id, s->t1, s->t2, s->t3);
132 
133  return 0;
134 }
135 
136 /**
137  * Get context-dependent Golomb code, decode it and update context
138  */
139 static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q)
140 {
141  int k, ret;
142 
143  for (k = 0; (state->N[Q] << k) < state->A[Q]; k++)
144  ;
145 
146 #ifdef JLS_BROKEN
147  if (!show_bits_long(gb, 32))
148  return -1;
149 #endif
150  ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
151 
152  /* decode mapped error */
153  if (ret & 1)
154  ret = -(ret + 1 >> 1);
155  else
156  ret >>= 1;
157 
158  /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
159  if (!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
160  ret = -(ret + 1);
161 
162  ret = ff_jpegls_update_state_regular(state, Q, ret);
163 
164  return ret;
165 }
166 
167 /**
168  * Get Golomb code, decode it and update state for run termination
169  */
171  int RItype, int limit_add)
172 {
173  int k, ret, temp, map;
174  int Q = 365 + RItype;
175 
176  temp = state->A[Q];
177  if (RItype)
178  temp += state->N[Q] >> 1;
179 
180  for (k = 0; (state->N[Q] << k) < temp; k++)
181  ;
182 
183 #ifdef JLS_BROKEN
184  if (!show_bits_long(gb, 32))
185  return -1;
186 #endif
187  ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1,
188  state->qbpp);
189 
190  /* decode mapped error */
191  map = 0;
192  if (!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
193  map = 1;
194  ret += RItype + map;
195 
196  if (ret & 1) {
197  ret = map - (ret + 1 >> 1);
198  state->B[Q]++;
199  } else {
200  ret = ret >> 1;
201  }
202 
203  if(FFABS(ret) > 0xFFFF)
204  return -0x10000;
205  /* update state */
206  state->A[Q] += FFABS(ret) - RItype;
207  ret *= state->twonear;
208  ff_jpegls_downscale_state(state, Q);
209 
210  return ret;
211 }
212 
213 /**
214  * Decode one line of image
215  */
217  void *last, void *dst, int last2, int w,
218  int stride, int comp, int bits)
219 {
220  int i, x = 0;
221  int Ra, Rb, Rc, Rd;
222  int D0, D1, D2;
223 
224  while (x < w) {
225  int err, pred;
226 
227  /* compute gradients */
228  Ra = x ? R(dst, x - stride) : R(last, x);
229  Rb = R(last, x);
230  Rc = x ? R(last, x - stride) : last2;
231  Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
232  D0 = Rd - Rb;
233  D1 = Rb - Rc;
234  D2 = Rc - Ra;
235  /* run mode */
236  if ((FFABS(D0) <= state->near) &&
237  (FFABS(D1) <= state->near) &&
238  (FFABS(D2) <= state->near)) {
239  int r;
240  int RItype;
241 
242  /* decode full runs while available */
243  while (get_bits1(&s->gb)) {
244  int r;
245  r = 1 << ff_log2_run[state->run_index[comp]];
246  if (x + r * stride > w)
247  r = (w - x) / stride;
248  for (i = 0; i < r; i++) {
249  W(dst, x, Ra);
250  x += stride;
251  }
252  /* if EOL reached, we stop decoding */
253  if (r != 1 << ff_log2_run[state->run_index[comp]])
254  return;
255  if (state->run_index[comp] < 31)
256  state->run_index[comp]++;
257  if (x + stride > w)
258  return;
259  }
260  /* decode aborted run */
261  r = ff_log2_run[state->run_index[comp]];
262  if (r)
263  r = get_bits_long(&s->gb, r);
264  if (x + r * stride > w) {
265  r = (w - x) / stride;
266  }
267  for (i = 0; i < r; i++) {
268  W(dst, x, Ra);
269  x += stride;
270  }
271 
272  if (x >= w) {
273  av_log(NULL, AV_LOG_ERROR, "run overflow\n");
274  return;
275  }
276 
277  /* decode run termination value */
278  Rb = R(last, x);
279  RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
280  err = ls_get_code_runterm(&s->gb, state, RItype,
281  ff_log2_run[state->run_index[comp]]);
282  if (state->run_index[comp])
283  state->run_index[comp]--;
284 
285  if (state->near && RItype) {
286  pred = Ra + err;
287  } else {
288  if (Rb < Ra)
289  pred = Rb - err;
290  else
291  pred = Rb + err;
292  }
293  } else { /* regular mode */
294  int context, sign;
295 
296  context = ff_jpegls_quantize(state, D0) * 81 +
297  ff_jpegls_quantize(state, D1) * 9 +
298  ff_jpegls_quantize(state, D2);
299  pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
300 
301  if (context < 0) {
302  context = -context;
303  sign = 1;
304  } else {
305  sign = 0;
306  }
307 
308  if (sign) {
309  pred = av_clip(pred - state->C[context], 0, state->maxval);
310  err = -ls_get_code_regular(&s->gb, state, context);
311  } else {
312  pred = av_clip(pred + state->C[context], 0, state->maxval);
313  err = ls_get_code_regular(&s->gb, state, context);
314  }
315 
316  /* we have to do something more for near-lossless coding */
317  pred += err;
318  }
319  if (state->near) {
320  if (pred < -state->near)
321  pred += state->range * state->twonear;
322  else if (pred > state->maxval + state->near)
323  pred -= state->range * state->twonear;
324  pred = av_clip(pred, 0, state->maxval);
325  }
326 
327  pred &= state->maxval;
328  W(dst, x, pred);
329  x += stride;
330  }
331 }
332 
334  int point_transform, int ilv)
335 {
336  int i, t = 0;
337  uint8_t *zero, *last, *cur;
338  JLSState *state;
339  int off = 0, stride = 1, width, shift, ret = 0;
340 
341  zero = av_mallocz(s->picture_ptr->linesize[0]);
342  last = zero;
343  cur = s->picture_ptr->data[0];
344 
345  state = av_mallocz(sizeof(JLSState));
346  /* initialize JPEG-LS state from JPEG parameters */
347  state->near = near;
348  state->bpp = (s->bits < 2) ? 2 : s->bits;
349  state->maxval = s->maxval;
350  state->T1 = s->t1;
351  state->T2 = s->t2;
352  state->T3 = s->t3;
353  state->reset = s->reset;
355  ff_jpegls_init_state(state);
356 
357  if (s->bits <= 8)
358  shift = point_transform + (8 - s->bits);
359  else
360  shift = point_transform + (16 - s->bits);
361 
362  if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
364  "JPEG-LS params: %ix%i NEAR=%i MV=%i T(%i,%i,%i) "
365  "RESET=%i, LIMIT=%i, qbpp=%i, RANGE=%i\n",
366  s->width, s->height, state->near, state->maxval,
367  state->T1, state->T2, state->T3,
368  state->reset, state->limit, state->qbpp, state->range);
369  av_log(s->avctx, AV_LOG_DEBUG, "JPEG params: ILV=%i Pt=%i BPP=%i, scan = %i\n",
370  ilv, point_transform, s->bits, s->cur_scan);
371  }
372  if (ilv == 0) { /* separate planes */
373  if (s->cur_scan > s->nb_components) {
374  ret = AVERROR_INVALIDDATA;
375  goto end;
376  }
377  stride = (s->nb_components > 1) ? 3 : 1;
378  off = av_clip(s->cur_scan - 1, 0, stride - 1);
379  width = s->width * stride;
380  cur += off;
381  for (i = 0; i < s->height; i++) {
382  if (s->bits <= 8) {
383  ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
384  t = last[0];
385  } else {
386  ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
387  t = *((uint16_t *)last);
388  }
389  last = cur;
390  cur += s->picture_ptr->linesize[0];
391 
392  if (s->restart_interval && !--s->restart_count) {
393  align_get_bits(&s->gb);
394  skip_bits(&s->gb, 16); /* skip RSTn */
395  }
396  }
397  } else if (ilv == 1) { /* line interleaving */
398  int j;
399  int Rc[3] = { 0, 0, 0 };
400  stride = (s->nb_components > 1) ? 3 : 1;
401  memset(cur, 0, s->picture_ptr->linesize[0]);
402  width = s->width * stride;
403  for (i = 0; i < s->height; i++) {
404  for (j = 0; j < stride; j++) {
405  ls_decode_line(state, s, last + j, cur + j,
406  Rc[j], width, stride, j, 8);
407  Rc[j] = last[j];
408 
409  if (s->restart_interval && !--s->restart_count) {
410  align_get_bits(&s->gb);
411  skip_bits(&s->gb, 16); /* skip RSTn */
412  }
413  }
414  last = cur;
415  cur += s->picture_ptr->linesize[0];
416  }
417  } else if (ilv == 2) { /* sample interleaving */
418  avpriv_report_missing_feature(s->avctx, "Sample interleaved images");
419  ret = AVERROR_PATCHWELCOME;
420  goto end;
421  }
422 
423  if (s->xfrm && s->nb_components == 3) {
424  int x, w;
425 
426  w = s->width * s->nb_components;
427 
428  if (s->bits <= 8) {
429  uint8_t *src = s->picture_ptr->data[0];
430 
431  for (i = 0; i < s->height; i++) {
432  switch(s->xfrm) {
433  case 1:
434  for (x = off; x < w; x += 3) {
435  src[x ] += src[x+1] + 128;
436  src[x+2] += src[x+1] + 128;
437  }
438  break;
439  case 2:
440  for (x = off; x < w; x += 3) {
441  src[x ] += src[x+1] + 128;
442  src[x+2] += ((src[x ] + src[x+1])>>1) + 128;
443  }
444  break;
445  case 3:
446  for (x = off; x < w; x += 3) {
447  int g = src[x+0] - ((src[x+2]+src[x+1])>>2) + 64;
448  src[x+0] = src[x+2] + g + 128;
449  src[x+2] = src[x+1] + g + 128;
450  src[x+1] = g;
451  }
452  break;
453  case 4:
454  for (x = off; x < w; x += 3) {
455  int r = src[x+0] - (( 359 * (src[x+2]-128) + 490) >> 8);
456  int g = src[x+0] - (( 88 * (src[x+1]-128) - 183 * (src[x+2]-128) + 30) >> 8);
457  int b = src[x+0] + ((454 * (src[x+1]-128) + 574) >> 8);
458  src[x+0] = av_clip_uint8(r);
459  src[x+1] = av_clip_uint8(g);
460  src[x+2] = av_clip_uint8(b);
461  }
462  break;
463  }
464  src += s->picture_ptr->linesize[0];
465  }
466  }else
467  avpriv_report_missing_feature(s->avctx, "16bit xfrm");
468  }
469 
470  if (shift) { /* we need to do point transform or normalize samples */
471  int x, w;
472 
473  w = s->width * s->nb_components;
474 
475  if (s->bits <= 8) {
476  uint8_t *src = s->picture_ptr->data[0];
477 
478  for (i = 0; i < s->height; i++) {
479  for (x = off; x < w; x += stride)
480  src[x] <<= shift;
481  src += s->picture_ptr->linesize[0];
482  }
483  } else {
484  uint16_t *src = (uint16_t *)s->picture_ptr->data[0];
485 
486  for (i = 0; i < s->height; i++) {
487  for (x = 0; x < w; x++)
488  src[x] <<= shift;
489  src += s->picture_ptr->linesize[0] / 2;
490  }
491  }
492  }
493 
494 end:
495  av_free(state);
496  av_free(zero);
497 
498  return ret;
499 }
500 
502  .name = "jpegls",
503  .long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),
504  .type = AVMEDIA_TYPE_VIDEO,
505  .id = AV_CODEC_ID_JPEGLS,
506  .priv_data_size = sizeof(MJpegDecodeContext),
508  .close = ff_mjpeg_decode_end,
510  .capabilities = CODEC_CAP_DR1,
511 };