FFmpeg  1.2.12
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 
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 
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 
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 
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  if(FFABS(ret) > 0xFFFF)
146  return -0x10000;
147  /* update state */
148  state->A[Q] += FFABS(ret) - RItype;
149  ret *= state->twonear;
150  ff_jpegls_downscale_state(state, Q);
151 
152  return ret;
153 }
154 
158 static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s, void *last, void *dst, int last2, int w, int stride, int comp, int bits){
159  int i, x = 0;
160  int Ra, Rb, Rc, Rd;
161  int D0, D1, D2;
162 
163  while(x < w) {
164  int err, pred;
165 
166  /* compute gradients */
167  Ra = x ? R(dst, x - stride) : R(last, x);
168  Rb = R(last, x);
169  Rc = x ? R(last, x - stride) : last2;
170  Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
171  D0 = Rd - Rb;
172  D1 = Rb - Rc;
173  D2 = Rc - Ra;
174  /* run mode */
175  if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
176  int r;
177  int RItype;
178 
179  /* decode full runs while available */
180  while(get_bits1(&s->gb)) {
181  int r;
182  r = 1 << ff_log2_run[state->run_index[comp]];
183  if(x + r * stride > w) {
184  r = (w - x) / stride;
185  }
186  for(i = 0; i < r; i++) {
187  W(dst, x, Ra);
188  x += stride;
189  }
190  /* if EOL reached, we stop decoding */
191  if(r != (1 << ff_log2_run[state->run_index[comp]]))
192  return;
193  if(state->run_index[comp] < 31)
194  state->run_index[comp]++;
195  if(x + stride > w)
196  return;
197  }
198  /* decode aborted run */
199  r = ff_log2_run[state->run_index[comp]];
200  if(r)
201  r = get_bits_long(&s->gb, r);
202  if(x + r * stride > w) {
203  r = (w - x) / stride;
204  }
205  for(i = 0; i < r; i++) {
206  W(dst, x, Ra);
207  x += stride;
208  }
209 
210  if (x >= w) {
211  av_log(NULL, AV_LOG_ERROR, "run overflow\n");
212  return;
213  }
214 
215  /* decode run termination value */
216  Rb = R(last, x);
217  RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
218  err = ls_get_code_runterm(&s->gb, state, RItype, ff_log2_run[state->run_index[comp]]);
219  if(state->run_index[comp])
220  state->run_index[comp]--;
221 
222  if(state->near && RItype){
223  pred = Ra + err;
224  } else {
225  if(Rb < Ra)
226  pred = Rb - err;
227  else
228  pred = Rb + err;
229  }
230  } else { /* regular mode */
231  int context, sign;
232 
233  context = ff_jpegls_quantize(state, D0) * 81 + ff_jpegls_quantize(state, D1) * 9 + ff_jpegls_quantize(state, D2);
234  pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
235 
236  if(context < 0){
237  context = -context;
238  sign = 1;
239  }else{
240  sign = 0;
241  }
242 
243  if(sign){
244  pred = av_clip(pred - state->C[context], 0, state->maxval);
245  err = -ls_get_code_regular(&s->gb, state, context);
246  } else {
247  pred = av_clip(pred + state->C[context], 0, state->maxval);
248  err = ls_get_code_regular(&s->gb, state, context);
249  }
250 
251  /* we have to do something more for near-lossless coding */
252  pred += err;
253  }
254  if(state->near){
255  if(pred < -state->near)
256  pred += state->range * state->twonear;
257  else if(pred > state->maxval + state->near)
258  pred -= state->range * state->twonear;
259  pred = av_clip(pred, 0, state->maxval);
260  }
261 
262  pred &= state->maxval;
263  W(dst, x, pred);
264  x += stride;
265  }
266 }
267 
268 int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv){
269  int i, t = 0;
270  uint8_t *zero, *last, *cur;
271  JLSState *state;
272  int off = 0, stride = 1, width, shift;
273 
274  zero = av_mallocz(s->picture.linesize[0]);
275  last = zero;
276  cur = s->picture.data[0];
277 
278  state = av_mallocz(sizeof(JLSState));
279  /* initialize JPEG-LS state from JPEG parameters */
280  state->near = near;
281  state->bpp = (s->bits < 2) ? 2 : s->bits;
282  state->maxval = s->maxval;
283  state->T1 = s->t1;
284  state->T2 = s->t2;
285  state->T3 = s->t3;
286  state->reset = s->reset;
288  ff_jpegls_init_state(state);
289 
290  if(s->bits <= 8)
291  shift = point_transform + (8 - s->bits);
292  else
293  shift = point_transform + (16 - s->bits);
294 
295  if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
296  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",
297  s->width, s->height, state->near, state->maxval,
298  state->T1, state->T2, state->T3,
299  state->reset, state->limit, state->qbpp, state->range);
300  av_log(s->avctx, AV_LOG_DEBUG, "JPEG params: ILV=%i Pt=%i BPP=%i, scan = %i\n",
301  ilv, point_transform, s->bits, s->cur_scan);
302  }
303  if(ilv == 0) { /* separate planes */
304  stride = (s->nb_components > 1) ? 3 : 1;
305  off = av_clip(s->cur_scan - 1, 0, stride - 1);
306  width = s->width * stride;
307  cur += off;
308  for(i = 0; i < s->height; i++) {
309  if(s->bits <= 8){
310  ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
311  t = last[0];
312  }else{
313  ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
314  t = *((uint16_t*)last);
315  }
316  last = cur;
317  cur += s->picture.linesize[0];
318 
319  if (s->restart_interval && !--s->restart_count) {
320  align_get_bits(&s->gb);
321  skip_bits(&s->gb, 16); /* skip RSTn */
322  }
323  }
324  } else if(ilv == 1) { /* line interleaving */
325  int j;
326  int Rc[3] = {0, 0, 0};
327  stride = (s->nb_components > 1) ? 3 : 1;
328  memset(cur, 0, s->picture.linesize[0]);
329  width = s->width * stride;
330  for(i = 0; i < s->height; i++) {
331  for(j = 0; j < stride; j++) {
332  ls_decode_line(state, s, last + j, cur + j, Rc[j], width, stride, j, 8);
333  Rc[j] = last[j];
334 
335  if (s->restart_interval && !--s->restart_count) {
336  align_get_bits(&s->gb);
337  skip_bits(&s->gb, 16); /* skip RSTn */
338  }
339  }
340  last = cur;
341  cur += s->picture.linesize[0];
342  }
343  } else if(ilv == 2) { /* sample interleaving */
344  av_log(s->avctx, AV_LOG_ERROR, "Sample interleaved images are not supported.\n");
345  av_free(state);
346  av_free(zero);
347  return -1;
348  }
349 
350  if(shift){ /* we need to do point transform or normalize samples */
351  int x, w;
352 
353  w = s->width * s->nb_components;
354 
355  if(s->bits <= 8){
356  uint8_t *src = s->picture.data[0];
357 
358  for(i = 0; i < s->height; i++){
359  for(x = off; x < w; x+= stride){
360  src[x] <<= shift;
361  }
362  src += s->picture.linesize[0];
363  }
364  }else{
365  uint16_t *src = (uint16_t*) s->picture.data[0];
366 
367  for(i = 0; i < s->height; i++){
368  for(x = 0; x < w; x++){
369  src[x] <<= shift;
370  }
371  src += s->picture.linesize[0]/2;
372  }
373  }
374  }
375  av_free(state);
376  av_free(zero);
377 
378  return 0;
379 }
380 
381 
383  .name = "jpegls",
384  .type = AVMEDIA_TYPE_VIDEO,
385  .id = AV_CODEC_ID_JPEGLS,
386  .priv_data_size = sizeof(MJpegDecodeContext),
390  .capabilities = CODEC_CAP_DR1,
391  .long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),
392 };