2 * JPEG-LS encoder and decoder
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
27 * JPEG-LS encoder and decoder.
30 typedef struct JpeglsContext{
31 AVCodecContext *avctx;
35 typedef struct JLSState{
37 int A[367], B[367], C[365], N[367];
38 int limit, reset, bpp, qbpp, maxval, range;
43 static const uint8_t log2_run[32]={
44 0, 0, 0, 0, 1, 1, 1, 1,
45 2, 2, 2, 2, 3, 3, 3, 3,
46 4, 4, 5, 5, 6, 6, 7, 7,
47 8, 9,10,11,12,13,14,15
51 * Uncomment this to significantly speed up decoding of broken JPEG-LS
52 * (or test broken JPEG-LS decoder) and slow down ordinary decoding a bit.
54 * There is no Golomb code with length >= 32 bits possible, so check and
55 * avoid situation of 32 zeros, FFmpeg Golomb decoder is painfully slow
60 /********** Functions for both encoder and decoder **********/
63 * Calculate initial JPEG-LS parameters
65 static void ls_init_state(JLSState *state){
68 state->twonear = state->near * 2 + 1;
69 state->range = ((state->maxval + state->twonear - 1) / state->twonear) + 1;
71 // QBPP = ceil(log2(RANGE))
72 for(state->qbpp = 0; (1 << state->qbpp) < state->range; state->qbpp++);
75 state->limit = 16 + 2 * state->bpp - state->qbpp;
77 state->limit = (4 * state->bpp) - state->qbpp;
79 for(i = 0; i < 367; i++) {
80 state->A[i] = FFMAX((state->range + 32) >> 6, 2);
87 * Calculate quantized gradient value, used for context determination
89 static inline int quantize(JLSState *s, int v){ //FIXME optimize
92 if(v <= -s->T3) return -4;
93 if(v <= -s->T2) return -3;
94 if(v <= -s->T1) return -2;
95 if(v < -s->near) return -1;
98 if(v <= s->near) return 0;
99 if(v < s->T1) return 1;
100 if(v < s->T2) return 2;
101 if(v < s->T3) return 3;
107 * Custom value clipping function used in T1, T2, T3 calculation
109 static inline int iso_clip(int v, int vmin, int vmax){
110 if(v > vmax || v < vmin) return vmin;
115 * Calculate JPEG-LS codec values
117 static void reset_ls_coding_parameters(JLSState *s, int reset_all){
118 const int basic_t1= 3;
119 const int basic_t2= 7;
120 const int basic_t3= 21;
123 if(s->maxval==0 || reset_all) s->maxval= (1 << s->bpp) - 1;
126 factor= (FFMIN(s->maxval, 4095) + 128)>>8;
128 if(s->T1==0 || reset_all)
129 s->T1= iso_clip(factor*(basic_t1-2) + 2 + 3*s->near, s->near+1, s->maxval);
130 if(s->T2==0 || reset_all)
131 s->T2= iso_clip(factor*(basic_t2-3) + 3 + 5*s->near, s->T1, s->maxval);
132 if(s->T3==0 || reset_all)
133 s->T3= iso_clip(factor*(basic_t3-4) + 4 + 7*s->near, s->T2, s->maxval);
135 factor= 256 / (s->maxval + 1);
137 if(s->T1==0 || reset_all)
138 s->T1= iso_clip(FFMAX(2, basic_t1/factor + 3*s->near), s->near+1, s->maxval);
139 if(s->T2==0 || reset_all)
140 s->T2= iso_clip(FFMAX(3, basic_t2/factor + 5*s->near), s->T1, s->maxval);
141 if(s->T3==0 || reset_all)
142 s->T3= iso_clip(FFMAX(4, basic_t3/factor + 6*s->near), s->T2, s->maxval);
145 if(s->reset==0 || reset_all) s->reset= 64;
146 // av_log(NULL, AV_LOG_DEBUG, "[JPEG-LS RESET] T=%i,%i,%i\n", s->T1, s->T2, s->T3);
150 /********** Decoder-specific functions **********/
153 * Decode LSE block with initialization parameters
155 static int decode_lse(MJpegDecodeContext *s)
159 /* XXX: verify len field validity */
160 len = get_bits(&s->gb, 16);
161 id = get_bits(&s->gb, 8);
165 s->maxval= get_bits(&s->gb, 16);
166 s->t1= get_bits(&s->gb, 16);
167 s->t2= get_bits(&s->gb, 16);
168 s->t3= get_bits(&s->gb, 16);
169 s->reset= get_bits(&s->gb, 16);
171 // reset_ls_coding_parameters(s, 0);
176 av_log(s->avctx, AV_LOG_ERROR, "palette not supported\n");
179 av_log(s->avctx, AV_LOG_ERROR, "oversize image not supported\n");
182 av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
185 // av_log(s->avctx, AV_LOG_DEBUG, "ID=%i, T=%i,%i,%i\n", id, s->t1, s->t2, s->t3);
190 static void inline downscale_state(JLSState *state, int Q){
191 if(state->N[Q] == state->reset){
199 static inline int update_state_regular(JLSState *state, int Q, int err){
200 state->A[Q] += FFABS(err);
201 err *= state->twonear;
204 downscale_state(state, Q);
206 if(state->B[Q] <= -state->N[Q]) {
207 state->B[Q]= FFMAX(state->B[Q] + state->N[Q], 1-state->N[Q]);
208 if(state->C[Q] > -128)
210 }else if(state->B[Q] > 0){
211 state->B[Q]= FFMIN(state->B[Q] - state->N[Q], 0);
212 if(state->C[Q] < 127)
220 * Get context-dependent Golomb code, decode it and update context
222 static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q){
225 for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
228 if(!show_bits_long(gb, 32))return -1;
230 ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
232 /* decode mapped error */
234 ret = -((ret + 1) >> 1);
238 /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
239 if(!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
242 ret= update_state_regular(state, Q, ret);
248 * Get Golomb code, decode it and update state for run termination
250 static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state, int RItype, int limit_add){
251 int k, ret, temp, map;
252 int Q = 365 + RItype;
256 temp += state->N[Q] >> 1;
258 for(k = 0; (state->N[Q] << k) < temp; k++);
261 if(!show_bits_long(gb, 32))return -1;
263 ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1, state->qbpp);
265 /* decode mapped error */
267 if(!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
272 ret = map - ((ret + 1) >> 1);
279 state->A[Q] += FFABS(ret) - RItype;
280 ret *= state->twonear;
281 downscale_state(state, Q);
286 #define R(a, i ) (bits == 8 ? ((uint8_t*)(a))[i] : ((uint16_t*)(a))[i] )
287 #define W(a, i, v) (bits == 8 ? (((uint8_t*)(a))[i]=v) : (((uint16_t*)(a))[i]=v))
289 * Decode one line of image
291 static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s, void *last, void *dst, int last2, int w, int stride, int comp, int bits){
299 /* compute gradients */
300 Ra = x ? R(dst, x - stride) : R(last, x);
302 Rc = x ? R(last, x - stride) : last2;
303 Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
308 if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
312 /* decode full runs while available */
313 while(get_bits1(&s->gb)) {
315 r = 1 << log2_run[state->run_index[comp]];
316 if(x + r * stride > w) {
317 r = (w - x) / stride;
319 for(i = 0; i < r; i++) {
323 /* if EOL reached, we stop decoding */
324 if(r != (1 << log2_run[state->run_index[comp]]))
326 if(state->run_index[comp] < 31)
327 state->run_index[comp]++;
331 /* decode aborted run */
332 r = log2_run[state->run_index[comp]];
334 r = get_bits_long(&s->gb, r);
335 for(i = 0; i < r; i++) {
340 /* decode run termination value */
342 RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
343 err = ls_get_code_runterm(&s->gb, state, RItype, log2_run[state->run_index[comp]]);
344 if(state->run_index[comp])
345 state->run_index[comp]--;
347 if(state->near && RItype){
355 } else { /* regular mode */
358 context = quantize(state, D0) * 81 + quantize(state, D1) * 9 + quantize(state, D2);
359 pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
369 pred = clip(pred - state->C[context], 0, state->maxval);
370 err = -ls_get_code_regular(&s->gb, state, context);
372 pred = clip(pred + state->C[context], 0, state->maxval);
373 err = ls_get_code_regular(&s->gb, state, context);
376 /* we have to do something more for near-lossless coding */
380 if(pred < -state->near)
381 pred += state->range * state->twonear;
382 else if(pred > state->maxval + state->near)
383 pred -= state->range * state->twonear;
384 pred = clip(pred, 0, state->maxval);
392 static int ls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv){
394 uint8_t *zero, *last, *cur;
396 int off = 0, stride = 1, width, shift;
398 zero = av_mallocz(s->picture.linesize[0]);
400 cur = s->picture.data[0];
402 state = av_mallocz(sizeof(JLSState));
403 /* initialize JPEG-LS state from JPEG parameters */
405 state->bpp = (s->bits < 2) ? 2 : s->bits;
406 state->maxval = s->maxval;
410 state->reset = s->reset;
411 reset_ls_coding_parameters(state, 0);
412 ls_init_state(state);
415 shift = point_transform + (8 - s->bits);
417 shift = point_transform + (16 - s->bits);
419 // 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);
420 // 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);
421 if(ilv == 0) { /* separate planes */
422 off = s->cur_scan - 1;
423 stride = (s->nb_components > 1) ? 3 : 1;
424 width = s->width * stride;
426 for(i = 0; i < s->height; i++) {
428 ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
431 ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
432 t = *((uint16_t*)last);
435 cur += s->picture.linesize[0];
437 if (s->restart_interval && !--s->restart_count) {
438 align_get_bits(&s->gb);
439 skip_bits(&s->gb, 16); /* skip RSTn */
442 } else if(ilv == 1) { /* line interleaving */
444 int Rc[3] = {0, 0, 0};
445 memset(cur, 0, s->picture.linesize[0]);
446 width = s->width * 3;
447 for(i = 0; i < s->height; i++) {
448 for(j = 0; j < 3; j++) {
449 ls_decode_line(state, s, last + j, cur + j, Rc[j], width, 3, j, 8);
452 if (s->restart_interval && !--s->restart_count) {
453 align_get_bits(&s->gb);
454 skip_bits(&s->gb, 16); /* skip RSTn */
458 cur += s->picture.linesize[0];
460 } else if(ilv == 2) { /* sample interleaving */
461 av_log(s->avctx, AV_LOG_ERROR, "Sample interleaved images are not supported.\n");
467 if(shift){ /* we need to do point transform or normalize samples */
470 w = s->width * s->nb_components;
473 uint8_t *src = s->picture.data[0];
475 for(i = 0; i < s->height; i++){
476 for(x = off; x < w; x+= stride){
479 src += s->picture.linesize[0];
482 uint16_t *src = s->picture.data[0];
484 for(i = 0; i < s->height; i++){
485 for(x = 0; x < w; x++){
488 src += s->picture.linesize[0]/2;
498 #if defined(CONFIG_ENCODERS) && defined(CONFIG_JPEGLS_ENCODER)
499 /********** Encoder-specific functions **********/
502 * Encode error from regular symbol
504 static inline void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q, int err){
509 for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
511 map = !state->near && !k && (2 * state->B[Q] <= -state->N[Q]);
515 if(err >= ((state->range + 1) >> 1)) {
517 val = 2 * FFABS(err) - 1 - map;
521 set_ur_golomb_jpegls(pb, val, k, state->limit, state->qbpp);
523 update_state_regular(state, Q, err);
527 * Encode error from run termination
529 static inline void ls_encode_runterm(JLSState *state, PutBitContext *pb, int RItype, int err, int limit_add){
532 int Q = 365 + RItype;
537 temp += state->N[Q] >> 1;
538 for(k = 0; (state->N[Q] << k) < temp; k++);
540 if(!k && err && (2 * state->B[Q] < state->N[Q]))
544 val = - (2 * err) - 1 - RItype + map;
546 val = 2 * err - RItype - map;
547 set_ur_golomb_jpegls(pb, val, k, state->limit - limit_add - 1, state->qbpp);
551 state->A[Q] += (val + 1 - RItype) >> 1;
553 downscale_state(state, Q);
557 * Encode run value as specified by JPEG-LS standard
559 static inline void ls_encode_run(JLSState *state, PutBitContext *pb, int run, int comp, int trail){
560 while(run >= (1 << log2_run[state->run_index[comp]])){
562 run -= 1 << log2_run[state->run_index[comp]];
563 if(state->run_index[comp] < 31)
564 state->run_index[comp]++;
566 /* if hit EOL, encode another full run, else encode aborted run */
571 if(log2_run[state->run_index[comp]])
572 put_bits(pb, log2_run[state->run_index[comp]], run);
577 * Encode one line of image
579 static inline void ls_encode_line(JLSState *state, PutBitContext *pb, void *last, void *cur, int last2, int w, int stride, int comp, int bits){
587 /* compute gradients */
588 Ra = x ? R(cur, x - stride) : R(last, x);
590 Rc = x ? R(last, x - stride) : last2;
591 Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
597 if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
598 int RUNval, RItype, run;
602 while(x < w && (FFABS(R(cur, x) - RUNval) <= state->near)){
607 ls_encode_run(state, pb, run, comp, x < w);
611 RItype = (FFABS(Ra - Rb) <= state->near);
612 pred = RItype ? Ra : Rb;
613 err = R(cur, x) - pred;
615 if(!RItype && Ra > Rb)
620 err = (state->near + err) / state->twonear;
622 err = -(state->near - err) / state->twonear;
624 if(RItype || (Rb >= Ra))
625 Ra = clip(pred + err * state->twonear, 0, state->maxval);
627 Ra = clip(pred - err * state->twonear, 0, state->maxval);
632 if(err >= ((state->range + 1) >> 1))
635 ls_encode_runterm(state, pb, RItype, err, log2_run[state->run_index[comp]]);
637 if(state->run_index[comp] > 0)
638 state->run_index[comp]--;
639 } else { /* regular mode */
642 context = quantize(state, D0) * 81 + quantize(state, D1) * 9 + quantize(state, D2);
643 pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
648 pred = clip(pred - state->C[context], 0, state->maxval);
649 err = pred - R(cur, x);
652 pred = clip(pred + state->C[context], 0, state->maxval);
653 err = R(cur, x) - pred;
658 err = (state->near + err) / state->twonear;
660 err = -(state->near - err) / state->twonear;
662 Ra = clip(pred + err * state->twonear, 0, state->maxval);
664 Ra = clip(pred - err * state->twonear, 0, state->maxval);
668 ls_encode_regular(state, pb, context, err);
674 static void ls_store_lse(JLSState *state, PutBitContext *pb){
675 /* Test if we have default params and don't need to store LSE */
677 memset(&state2, 0, sizeof(JLSState));
678 state2.bpp = state->bpp;
679 state2.near = state->near;
680 reset_ls_coding_parameters(&state2, 1);
681 if(state->T1 == state2.T1 && state->T2 == state2.T2 && state->T3 == state2.T3 && state->reset == state2.reset)
683 /* store LSE type 1 */
685 put_bits(pb, 16, 13);
687 put_bits(pb, 16, state->maxval);
688 put_bits(pb, 16, state->T1);
689 put_bits(pb, 16, state->T2);
690 put_bits(pb, 16, state->T3);
691 put_bits(pb, 16, state->reset);
694 static int encode_picture_ls(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
695 JpeglsContext * const s = avctx->priv_data;
696 AVFrame *pict = data;
697 AVFrame * const p= (AVFrame*)&s->picture;
698 const int near = avctx->prediction_method;
699 PutBitContext pb, pb2;
701 uint8_t *buf2, *zero, *cur, *last;
706 buf2 = av_malloc(buf_size);
708 init_put_bits(&pb, buf, buf_size);
709 init_put_bits(&pb2, buf2, buf_size);
712 p->pict_type= FF_I_TYPE;
715 if(avctx->pix_fmt == PIX_FMT_GRAY8 || avctx->pix_fmt == PIX_FMT_GRAY16)
720 /* write our own JPEG header, can't use mjpeg_picture_header */
721 put_marker(&pb, SOI);
722 put_marker(&pb, SOF48);
723 put_bits(&pb, 16, 8 + comps * 3); // header size depends on components
724 put_bits(&pb, 8, (avctx->pix_fmt == PIX_FMT_GRAY16) ? 16 : 8); // bpp
725 put_bits(&pb, 16, avctx->height);
726 put_bits(&pb, 16, avctx->width);
727 put_bits(&pb, 8, comps); // components
728 for(i = 1; i <= comps; i++) {
729 put_bits(&pb, 8, i); // component ID
730 put_bits(&pb, 8, 0x11); // subsampling: none
731 put_bits(&pb, 8, 0); // Tiq, used by JPEG-LS ext
734 put_marker(&pb, SOS);
735 put_bits(&pb, 16, 6 + comps * 2);
736 put_bits(&pb, 8, comps);
737 for(i = 1; i <= comps; i++) {
738 put_bits(&pb, 8, i); // component ID
739 put_bits(&pb, 8, 0); // mapping index: none
741 put_bits(&pb, 8, near);
742 put_bits(&pb, 8, (comps > 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line
743 put_bits(&pb, 8, 0); // point transform: none
745 state = av_mallocz(sizeof(JLSState));
746 /* initialize JPEG-LS state from JPEG parameters */
748 state->bpp = (avctx->pix_fmt == PIX_FMT_GRAY16) ? 16 : 8;
749 reset_ls_coding_parameters(state, 0);
750 ls_init_state(state);
752 ls_store_lse(state, &pb);
754 zero = av_mallocz(p->linesize[0]);
757 if(avctx->pix_fmt == PIX_FMT_GRAY8){
760 for(i = 0; i < avctx->height; i++) {
761 ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 8);
764 cur += p->linesize[0];
766 }else if(avctx->pix_fmt == PIX_FMT_GRAY16){
769 for(i = 0; i < avctx->height; i++) {
770 ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 16);
771 t = *((uint16_t*)last);
773 cur += p->linesize[0];
775 }else if(avctx->pix_fmt == PIX_FMT_RGB24){
777 int Rc[3] = {0, 0, 0};
779 width = avctx->width * 3;
780 for(i = 0; i < avctx->height; i++) {
781 for(j = 0; j < 3; j++) {
782 ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j, 8);
786 cur += s->picture.linesize[0];
788 }else if(avctx->pix_fmt == PIX_FMT_BGR24){
790 int Rc[3] = {0, 0, 0};
792 width = avctx->width * 3;
793 for(i = 0; i < avctx->height; i++) {
794 for(j = 2; j >= 0; j--) {
795 ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j, 8);
799 cur += s->picture.linesize[0];
806 flush_put_bits(&pb2);
807 /* do escape coding */
808 size = put_bits_count(&pb2) >> 3;
809 init_get_bits(&gb, buf2, size);
810 while(get_bits_count(&gb) < size * 8){
812 v = get_bits(&gb, 8);
815 v = get_bits(&gb, 7);
823 put_marker(&pb, EOI);
828 return put_bits_count(&pb) >> 3;
831 static int encode_init_ls(AVCodecContext *ctx) {
832 JpeglsContext *c = (JpeglsContext*)ctx->priv_data;
835 ctx->coded_frame = &c->picture;
837 if(ctx->pix_fmt != PIX_FMT_GRAY8 && ctx->pix_fmt != PIX_FMT_GRAY16 && ctx->pix_fmt != PIX_FMT_RGB24 && ctx->pix_fmt != PIX_FMT_BGR24){
838 av_log(ctx, AV_LOG_ERROR, "Only grayscale and RGB24/BGR24 images are supported\n");
844 AVCodec jpegls_encoder = { //FIXME avoid MPV_* lossless jpeg shouldnt need them
848 sizeof(JpeglsContext),
852 .pix_fmts= (enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB24, PIX_FMT_GRAY8, PIX_FMT_GRAY16, -1},