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 = av_clip(pred - state->C[context], 0, state->maxval);
370
err = -ls_get_code_regular(&s->gb, state, context);
372
pred = av_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 = av_clip(pred, 0, state->maxval);
387
pred &= state->maxval;
393
static int ls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv){
395
uint8_t *zero, *last, *cur;
397
int off = 0, stride = 1, width, shift;
399
zero = av_mallocz(s->picture.linesize[0]);
401
cur = s->picture.data[0];
403
state = av_mallocz(sizeof(JLSState));
404
/* initialize JPEG-LS state from JPEG parameters */
406
state->bpp = (s->bits < 2) ? 2 : s->bits;
407
state->maxval = s->maxval;
411
state->reset = s->reset;
412
reset_ls_coding_parameters(state, 0);
413
ls_init_state(state);
416
shift = point_transform + (8 - s->bits);
418
shift = point_transform + (16 - s->bits);
420
// 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);
421
// 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);
422
if(ilv == 0) { /* separate planes */
423
off = s->cur_scan - 1;
424
stride = (s->nb_components > 1) ? 3 : 1;
425
width = s->width * stride;
427
for(i = 0; i < s->height; i++) {
429
ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
432
ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
433
t = *((uint16_t*)last);
436
cur += s->picture.linesize[0];
438
if (s->restart_interval && !--s->restart_count) {
439
align_get_bits(&s->gb);
440
skip_bits(&s->gb, 16); /* skip RSTn */
443
} else if(ilv == 1) { /* line interleaving */
445
int Rc[3] = {0, 0, 0};
446
memset(cur, 0, s->picture.linesize[0]);
447
width = s->width * 3;
448
for(i = 0; i < s->height; i++) {
449
for(j = 0; j < 3; j++) {
450
ls_decode_line(state, s, last + j, cur + j, Rc[j], width, 3, j, 8);
453
if (s->restart_interval && !--s->restart_count) {
454
align_get_bits(&s->gb);
455
skip_bits(&s->gb, 16); /* skip RSTn */
459
cur += s->picture.linesize[0];
461
} else if(ilv == 2) { /* sample interleaving */
462
av_log(s->avctx, AV_LOG_ERROR, "Sample interleaved images are not supported.\n");
468
if(shift){ /* we need to do point transform or normalize samples */
471
w = s->width * s->nb_components;
474
uint8_t *src = s->picture.data[0];
476
for(i = 0; i < s->height; i++){
477
for(x = off; x < w; x+= stride){
480
src += s->picture.linesize[0];
483
uint16_t *src = (uint16_t*) s->picture.data[0];
485
for(i = 0; i < s->height; i++){
486
for(x = 0; x < w; x++){
489
src += s->picture.linesize[0]/2;
499
#if defined(CONFIG_ENCODERS) && defined(CONFIG_JPEGLS_ENCODER)
500
/********** Encoder-specific functions **********/
503
* Encode error from regular symbol
505
static inline void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q, int err){
510
for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
512
map = !state->near && !k && (2 * state->B[Q] <= -state->N[Q]);
516
if(err >= ((state->range + 1) >> 1)) {
518
val = 2 * FFABS(err) - 1 - map;
522
set_ur_golomb_jpegls(pb, val, k, state->limit, state->qbpp);
524
update_state_regular(state, Q, err);
528
* Encode error from run termination
530
static inline void ls_encode_runterm(JLSState *state, PutBitContext *pb, int RItype, int err, int limit_add){
533
int Q = 365 + RItype;
538
temp += state->N[Q] >> 1;
539
for(k = 0; (state->N[Q] << k) < temp; k++);
541
if(!k && err && (2 * state->B[Q] < state->N[Q]))
545
val = - (2 * err) - 1 - RItype + map;
547
val = 2 * err - RItype - map;
548
set_ur_golomb_jpegls(pb, val, k, state->limit - limit_add - 1, state->qbpp);
552
state->A[Q] += (val + 1 - RItype) >> 1;
554
downscale_state(state, Q);
558
* Encode run value as specified by JPEG-LS standard
560
static inline void ls_encode_run(JLSState *state, PutBitContext *pb, int run, int comp, int trail){
561
while(run >= (1 << log2_run[state->run_index[comp]])){
563
run -= 1 << log2_run[state->run_index[comp]];
564
if(state->run_index[comp] < 31)
565
state->run_index[comp]++;
567
/* if hit EOL, encode another full run, else encode aborted run */
572
if(log2_run[state->run_index[comp]])
573
put_bits(pb, log2_run[state->run_index[comp]], run);
578
* Encode one line of image
580
static inline void ls_encode_line(JLSState *state, PutBitContext *pb, void *last, void *cur, int last2, int w, int stride, int comp, int bits){
588
/* compute gradients */
589
Ra = x ? R(cur, x - stride) : R(last, x);
591
Rc = x ? R(last, x - stride) : last2;
592
Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
598
if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
599
int RUNval, RItype, run;
603
while(x < w && (FFABS(R(cur, x) - RUNval) <= state->near)){
608
ls_encode_run(state, pb, run, comp, x < w);
612
RItype = (FFABS(Ra - Rb) <= state->near);
613
pred = RItype ? Ra : Rb;
614
err = R(cur, x) - pred;
616
if(!RItype && Ra > Rb)
621
err = (state->near + err) / state->twonear;
623
err = -(state->near - err) / state->twonear;
625
if(RItype || (Rb >= Ra))
626
Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
628
Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
633
if(err >= ((state->range + 1) >> 1))
636
ls_encode_runterm(state, pb, RItype, err, log2_run[state->run_index[comp]]);
638
if(state->run_index[comp] > 0)
639
state->run_index[comp]--;
640
} else { /* regular mode */
643
context = quantize(state, D0) * 81 + quantize(state, D1) * 9 + quantize(state, D2);
644
pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
649
pred = av_clip(pred - state->C[context], 0, state->maxval);
650
err = pred - R(cur, x);
653
pred = av_clip(pred + state->C[context], 0, state->maxval);
654
err = R(cur, x) - pred;
659
err = (state->near + err) / state->twonear;
661
err = -(state->near - err) / state->twonear;
663
Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
665
Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
669
ls_encode_regular(state, pb, context, err);
675
static void ls_store_lse(JLSState *state, PutBitContext *pb){
676
/* Test if we have default params and don't need to store LSE */
678
memset(&state2, 0, sizeof(JLSState));
679
state2.bpp = state->bpp;
680
state2.near = state->near;
681
reset_ls_coding_parameters(&state2, 1);
682
if(state->T1 == state2.T1 && state->T2 == state2.T2 && state->T3 == state2.T3 && state->reset == state2.reset)
684
/* store LSE type 1 */
686
put_bits(pb, 16, 13);
688
put_bits(pb, 16, state->maxval);
689
put_bits(pb, 16, state->T1);
690
put_bits(pb, 16, state->T2);
691
put_bits(pb, 16, state->T3);
692
put_bits(pb, 16, state->reset);
695
static int encode_picture_ls(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
696
JpeglsContext * const s = avctx->priv_data;
697
AVFrame *pict = data;
698
AVFrame * const p= (AVFrame*)&s->picture;
699
const int near = avctx->prediction_method;
700
PutBitContext pb, pb2;
702
uint8_t *buf2, *zero, *cur, *last;
707
buf2 = av_malloc(buf_size);
709
init_put_bits(&pb, buf, buf_size);
710
init_put_bits(&pb2, buf2, buf_size);
713
p->pict_type= FF_I_TYPE;
716
if(avctx->pix_fmt == PIX_FMT_GRAY8 || avctx->pix_fmt == PIX_FMT_GRAY16)
721
/* write our own JPEG header, can't use mjpeg_picture_header */
722
put_marker(&pb, SOI);
723
put_marker(&pb, SOF48);
724
put_bits(&pb, 16, 8 + comps * 3); // header size depends on components
725
put_bits(&pb, 8, (avctx->pix_fmt == PIX_FMT_GRAY16) ? 16 : 8); // bpp
726
put_bits(&pb, 16, avctx->height);
727
put_bits(&pb, 16, avctx->width);
728
put_bits(&pb, 8, comps); // components
729
for(i = 1; i <= comps; i++) {
730
put_bits(&pb, 8, i); // component ID
731
put_bits(&pb, 8, 0x11); // subsampling: none
732
put_bits(&pb, 8, 0); // Tiq, used by JPEG-LS ext
735
put_marker(&pb, SOS);
736
put_bits(&pb, 16, 6 + comps * 2);
737
put_bits(&pb, 8, comps);
738
for(i = 1; i <= comps; i++) {
739
put_bits(&pb, 8, i); // component ID
740
put_bits(&pb, 8, 0); // mapping index: none
742
put_bits(&pb, 8, near);
743
put_bits(&pb, 8, (comps > 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line
744
put_bits(&pb, 8, 0); // point transform: none
746
state = av_mallocz(sizeof(JLSState));
747
/* initialize JPEG-LS state from JPEG parameters */
749
state->bpp = (avctx->pix_fmt == PIX_FMT_GRAY16) ? 16 : 8;
750
reset_ls_coding_parameters(state, 0);
751
ls_init_state(state);
753
ls_store_lse(state, &pb);
755
zero = av_mallocz(p->linesize[0]);
758
if(avctx->pix_fmt == PIX_FMT_GRAY8){
761
for(i = 0; i < avctx->height; i++) {
762
ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 8);
765
cur += p->linesize[0];
767
}else if(avctx->pix_fmt == PIX_FMT_GRAY16){
770
for(i = 0; i < avctx->height; i++) {
771
ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 16);
772
t = *((uint16_t*)last);
774
cur += p->linesize[0];
776
}else if(avctx->pix_fmt == PIX_FMT_RGB24){
778
int Rc[3] = {0, 0, 0};
780
width = avctx->width * 3;
781
for(i = 0; i < avctx->height; i++) {
782
for(j = 0; j < 3; j++) {
783
ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j, 8);
787
cur += s->picture.linesize[0];
789
}else if(avctx->pix_fmt == PIX_FMT_BGR24){
791
int Rc[3] = {0, 0, 0};
793
width = avctx->width * 3;
794
for(i = 0; i < avctx->height; i++) {
795
for(j = 2; j >= 0; j--) {
796
ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j, 8);
800
cur += s->picture.linesize[0];
807
// the specification says that after doing 0xff escaping unused bits in the
808
// last byte must be set to 0, so just append 7 "optional" zero-bits to
809
// avoid special-casing.
810
put_bits(&pb2, 7, 0);
811
size = put_bits_count(&pb2);
812
flush_put_bits(&pb2);
813
/* do escape coding */
814
init_get_bits(&gb, buf2, size);
816
while(get_bits_count(&gb) < size){
818
v = get_bits(&gb, 8);
821
v = get_bits(&gb, 7);
829
put_marker(&pb, EOI);
834
return put_bits_count(&pb) >> 3;
837
static int encode_init_ls(AVCodecContext *ctx) {
838
JpeglsContext *c = (JpeglsContext*)ctx->priv_data;
841
ctx->coded_frame = &c->picture;
843
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){
844
av_log(ctx, AV_LOG_ERROR, "Only grayscale and RGB24/BGR24 images are supported\n");
850
AVCodec jpegls_encoder = { //FIXME avoid MPV_* lossless jpeg shouldnt need them
854
sizeof(JpeglsContext),
858
.pix_fmts= (enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB24, PIX_FMT_GRAY8, PIX_FMT_GRAY16, -1},