3 * Copyright (c) 2003 Michael Niedermayer
4 * Copyright (c) 2006 Konstantin Shishkov
6 * This file is part of FFmpeg.
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.
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.
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
35 #include "jpeglsdec.h"
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.
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
48 * Decode LSE block with initialization parameters
50 int ff_jpegls_decode_lse(MJpegDecodeContext *s)
54 skip_bits(&s->gb, 16); /* length: FIXME: verify field validity */
55 id = get_bits(&s->gb, 8);
59 s->maxval = get_bits(&s->gb, 16);
60 s->t1 = get_bits(&s->gb, 16);
61 s->t2 = get_bits(&s->gb, 16);
62 s->t3 = get_bits(&s->gb, 16);
63 s->reset = get_bits(&s->gb, 16);
65 // ff_jpegls_reset_coding_parameters(s, 0);
70 av_log(s->avctx, AV_LOG_ERROR, "palette not supported\n");
71 return AVERROR(ENOSYS);
73 av_log(s->avctx, AV_LOG_ERROR, "oversize image not supported\n");
74 return AVERROR(ENOSYS);
76 av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
77 return AVERROR_INVALIDDATA;
79 av_dlog(s->avctx, "ID=%i, T=%i,%i,%i\n", id, s->t1, s->t2, s->t3);
85 * Get context-dependent Golomb code, decode it and update context
87 static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q)
91 for (k = 0; (state->N[Q] << k) < state->A[Q]; k++)
95 if (!show_bits_long(gb, 32))
98 ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
100 /* decode mapped error */
102 ret = -(ret + 1 >> 1);
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]))
110 ret = ff_jpegls_update_state_regular(state, Q, ret);
116 * Get Golomb code, decode it and update state for run termination
118 static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state,
119 int RItype, int limit_add)
121 int k, ret, temp, map;
122 int Q = 365 + RItype;
126 temp += state->N[Q] >> 1;
128 for (k = 0; (state->N[Q] << k) < temp; k++)
132 if (!show_bits_long(gb, 32))
135 ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1,
138 /* decode mapped error */
140 if (!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
145 ret = map - (ret + 1 >> 1);
151 if(FFABS(ret) > 0xFFFF)
154 state->A[Q] += FFABS(ret) - RItype;
155 ret *= state->twonear;
156 ff_jpegls_downscale_state(state, Q);
162 * Decode one line of image
164 static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s,
165 void *last, void *dst, int last2, int w,
166 int stride, int comp, int bits)
175 /* compute gradients */
176 Ra = x ? R(dst, x - stride) : R(last, x);
178 Rc = x ? R(last, x - stride) : last2;
179 Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
184 if ((FFABS(D0) <= state->near) &&
185 (FFABS(D1) <= state->near) &&
186 (FFABS(D2) <= state->near)) {
190 /* decode full runs while available */
191 while (get_bits1(&s->gb)) {
193 r = 1 << ff_log2_run[state->run_index[comp]];
194 if (x + r * stride > w)
195 r = (w - x) / stride;
196 for (i = 0; i < r; i++) {
200 /* if EOL reached, we stop decoding */
201 if (r != 1 << ff_log2_run[state->run_index[comp]])
203 if (state->run_index[comp] < 31)
204 state->run_index[comp]++;
208 /* decode aborted run */
209 r = ff_log2_run[state->run_index[comp]];
211 r = get_bits_long(&s->gb, r);
212 if (x + r * stride > w) {
213 r = (w - x) / stride;
215 for (i = 0; i < r; i++) {
220 /* decode run termination value */
222 RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
223 err = ls_get_code_runterm(&s->gb, state, RItype,
224 ff_log2_run[state->run_index[comp]]);
225 if (state->run_index[comp])
226 state->run_index[comp]--;
228 if (state->near && RItype) {
236 } else { /* regular mode */
239 context = ff_jpegls_quantize(state, D0) * 81 +
240 ff_jpegls_quantize(state, D1) * 9 +
241 ff_jpegls_quantize(state, D2);
242 pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
252 pred = av_clip(pred - state->C[context], 0, state->maxval);
253 err = -ls_get_code_regular(&s->gb, state, context);
255 pred = av_clip(pred + state->C[context], 0, state->maxval);
256 err = ls_get_code_regular(&s->gb, state, context);
259 /* we have to do something more for near-lossless coding */
263 if (pred < -state->near)
264 pred += state->range * state->twonear;
265 else if (pred > state->maxval + state->near)
266 pred -= state->range * state->twonear;
267 pred = av_clip(pred, 0, state->maxval);
270 pred &= state->maxval;
276 int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near,
277 int point_transform, int ilv)
280 uint8_t *zero, *last, *cur;
282 int off = 0, stride = 1, width, shift, ret = 0;
284 zero = av_mallocz(s->picture_ptr->linesize[0]);
286 cur = s->picture_ptr->data[0];
288 state = av_mallocz(sizeof(JLSState));
289 /* initialize JPEG-LS state from JPEG parameters */
291 state->bpp = (s->bits < 2) ? 2 : s->bits;
292 state->maxval = s->maxval;
296 state->reset = s->reset;
297 ff_jpegls_reset_coding_parameters(state, 0);
298 ff_jpegls_init_state(state);
301 shift = point_transform + (8 - s->bits);
303 shift = point_transform + (16 - s->bits);
305 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
306 av_log(s->avctx, AV_LOG_DEBUG,
307 "JPEG-LS params: %ix%i NEAR=%i MV=%i T(%i,%i,%i) "
308 "RESET=%i, LIMIT=%i, qbpp=%i, RANGE=%i\n",
309 s->width, s->height, state->near, state->maxval,
310 state->T1, state->T2, state->T3,
311 state->reset, state->limit, state->qbpp, state->range);
312 av_log(s->avctx, AV_LOG_DEBUG, "JPEG params: ILV=%i Pt=%i BPP=%i, scan = %i\n",
313 ilv, point_transform, s->bits, s->cur_scan);
315 if (ilv == 0) { /* separate planes */
316 if (s->cur_scan > s->nb_components) {
317 ret = AVERROR_INVALIDDATA;
320 stride = (s->nb_components > 1) ? 3 : 1;
321 off = av_clip(s->cur_scan - 1, 0, stride - 1);
322 width = s->width * stride;
324 for (i = 0; i < s->height; i++) {
326 ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
329 ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
330 t = *((uint16_t *)last);
333 cur += s->picture_ptr->linesize[0];
335 if (s->restart_interval && !--s->restart_count) {
336 align_get_bits(&s->gb);
337 skip_bits(&s->gb, 16); /* skip RSTn */
340 } else if (ilv == 1) { /* line interleaving */
342 int Rc[3] = { 0, 0, 0 };
343 stride = (s->nb_components > 1) ? 3 : 1;
344 memset(cur, 0, s->picture_ptr->linesize[0]);
345 width = s->width * stride;
346 for (i = 0; i < s->height; i++) {
347 for (j = 0; j < stride; j++) {
348 ls_decode_line(state, s, last + j, cur + j,
349 Rc[j], width, stride, j, 8);
352 if (s->restart_interval && !--s->restart_count) {
353 align_get_bits(&s->gb);
354 skip_bits(&s->gb, 16); /* skip RSTn */
358 cur += s->picture_ptr->linesize[0];
360 } else if (ilv == 2) { /* sample interleaving */
361 avpriv_report_missing_feature(s->avctx, "Sample interleaved images");
362 ret = AVERROR_PATCHWELCOME;
366 if (s->xfrm && s->nb_components == 3) {
369 w = s->width * s->nb_components;
372 uint8_t *src = s->picture_ptr->data[0];
374 for (i = 0; i < s->height; i++) {
377 for (x = off; x < w; x += 3) {
378 src[x ] += src[x+1] + 128;
379 src[x+2] += src[x+1] + 128;
383 for (x = off; x < w; x += 3) {
384 src[x ] += src[x+1] + 128;
385 src[x+2] += ((src[x ] + src[x+1])>>1) + 128;
389 for (x = off; x < w; x += 3) {
390 int g = src[x+0] - ((src[x+2]+src[x+1])>>2) + 64;
391 src[x+0] = src[x+2] + g + 128;
392 src[x+2] = src[x+1] + g + 128;
397 for (x = off; x < w; x += 3) {
398 int r = src[x+0] - (( 359 * (src[x+2]-128) + 490) >> 8);
399 int g = src[x+0] - (( 88 * (src[x+1]-128) - 183 * (src[x+2]-128) + 30) >> 8);
400 int b = src[x+0] + ((454 * (src[x+1]-128) + 574) >> 8);
401 src[x+0] = av_clip_uint8(r);
402 src[x+1] = av_clip_uint8(g);
403 src[x+2] = av_clip_uint8(b);
407 src += s->picture_ptr->linesize[0];
410 avpriv_report_missing_feature(s->avctx, "16bit xfrm");
413 if (shift) { /* we need to do point transform or normalize samples */
416 w = s->width * s->nb_components;
419 uint8_t *src = s->picture_ptr->data[0];
421 for (i = 0; i < s->height; i++) {
422 for (x = off; x < w; x += stride)
424 src += s->picture_ptr->linesize[0];
427 uint16_t *src = (uint16_t *)s->picture_ptr->data[0];
429 for (i = 0; i < s->height; i++) {
430 for (x = 0; x < w; x++)
432 src += s->picture_ptr->linesize[0] / 2;
444 AVCodec ff_jpegls_decoder = {
446 .long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),
447 .type = AVMEDIA_TYPE_VIDEO,
448 .id = AV_CODEC_ID_JPEGLS,
449 .priv_data_size = sizeof(MJpegDecodeContext),
450 .init = ff_mjpeg_decode_init,
451 .close = ff_mjpeg_decode_end,
452 .decode = ff_mjpeg_decode_frame,
453 .capabilities = CODEC_CAP_DR1,