annotate jpeg_ls.c @ 3882:6c03e983f866 libavcodec

Original Commit: r82 | ods15 | 2006-09-29 21:03:43 +0300 (Fri, 29 Sep 2006) | 2 lines fix channels to be correct for rc->type==2 before classes[] decleration
author ods15
date Mon, 02 Oct 2006 06:08:39 +0000
parents 7364a7e00e86
children c8c591fe26f8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
1 /*
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
2 * JPEG-LS encoder and decoder
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
3 * Copyright (c) 2003 Michael Niedermayer
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
4 * Copyright (c) 2006 Konstantin Shishkov
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
5 *
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
6 * This library is free software; you can redistribute it and/or
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
7 * modify it under the terms of the GNU Lesser General Public
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
8 * License as published by the Free Software Foundation; either
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
9 * version 2 of the License, or (at your option) any later version.
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
10 *
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
11 * This library is distributed in the hope that it will be useful,
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
14 * Lesser General Public License for more details.
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
15 *
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
16 * You should have received a copy of the GNU Lesser General Public
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
17 * License along with this library; if not, write to the Free Software
3036
0b546eab515d Update licensing information: The FSF changed postal address.
diego
parents: 2970
diff changeset
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
19 */
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
20
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
21 #include "golomb.h"
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
22
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
23 /**
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
24 * @file jpeg_ls.c
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
25 * JPEG-LS encoder and decoder.
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
26 */
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
27
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
28 typedef struct JpeglsContext{
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
29 AVCodecContext *avctx;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
30 AVFrame picture;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
31 }JpeglsContext;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
32
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
33 typedef struct JLSState{
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
34 int T1, T2, T3;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
35 int A[367], B[367], C[365], N[367];
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
36 int limit, reset, bpp, qbpp, maxval, range;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
37 int near, twonear;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
38 int run_index[3];
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
39 }JLSState;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
40
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
41 static const uint8_t log2_run[32]={
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
42 0, 0, 0, 0, 1, 1, 1, 1,
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
43 2, 2, 2, 2, 3, 3, 3, 3,
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
44 4, 4, 5, 5, 6, 6, 7, 7,
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
45 8, 9,10,11,12,13,14,15
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
46 };
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
47
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
48 /*
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
49 * Uncomment this to significantly speed up decoding of broken JPEG-LS
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
50 * (or test broken JPEG-LS decoder) and slow down ordinary decoding a bit.
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
51 *
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
52 * There is no Golomb code with length >= 32 bits possible, so check and
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
53 * avoid situation of 32 zeros, FFmpeg Golomb decoder is painfully slow
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
54 * on this errors.
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
55 */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
56 //#define JLS_BROKEN
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
57
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
58 /********** Functions for both encoder and decoder **********/
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
59
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
60 /**
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
61 * Calculate initial JPEG-LS parameters
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
62 */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
63 static void ls_init_state(JLSState *state){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
64 int i;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
65
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
66 state->twonear = state->near * 2 + 1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
67 state->range = ((state->maxval + state->twonear - 1) / state->twonear) + 1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
68
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
69 // QBPP = ceil(log2(RANGE))
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
70 for(state->qbpp = 0; (1 << state->qbpp) < state->range; state->qbpp++);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
71
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
72 if(state->bpp < 8)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
73 state->limit = 16 + 2 * state->bpp - state->qbpp;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
74 else
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
75 state->limit = (4 * state->bpp) - state->qbpp;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
76
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
77 for(i = 0; i < 367; i++) {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
78 state->A[i] = (state->range + 32) >> 6;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
79 if(state->A[i] < 2)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
80 state->A[i] = 2;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
81 state->N[i] = 1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
82 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
83
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
84 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
85
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
86 /**
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
87 * Calculate quantized gradient value, used for context determination
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
88 */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
89 static inline int quantize(JLSState *s, int v){ //FIXME optimize
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
90 if(v==0) return 0;
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
91 if(v < 0){
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
92 if(v <= -s->T3) return -4;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
93 if(v <= -s->T2) return -3;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
94 if(v <= -s->T1) return -2;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
95 if(v < -s->near) return -1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
96 return 0;
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
97 }else{
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
98 if(v <= s->near) return 0;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
99 if(v < s->T1) return 1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
100 if(v < s->T2) return 2;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
101 if(v < s->T3) return 3;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
102 return 4;
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
103 }
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
104 }
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
105
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
106 /**
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
107 * Custom value clipping function used in T1, T2, T3 calculation
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
108 */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
109 static inline int iso_clip(int v, int vmin, int vmax){
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
110 if(v > vmax || v < vmin) return vmin;
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
111 else return v;
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
112 }
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
113
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
114 /**
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
115 * Calculate JPEG-LS codec values
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
116 */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
117 static void reset_ls_coding_parameters(JLSState *s, int reset_all){
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
118 const int basic_t1= 3;
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
119 const int basic_t2= 7;
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
120 const int basic_t3= 21;
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
121 int factor;
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
122
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
123 if(s->maxval==0 || reset_all) s->maxval= (1 << s->bpp) - 1;
3063
f02d0b59279c Remove all stray tabs and trailing whitespace, this time for good.
diego
parents: 3036
diff changeset
124
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
125 if(s->maxval >=128){
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
126 factor= (FFMIN(s->maxval, 4095) + 128)>>8;
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
127
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
128 if(s->T1==0 || reset_all)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
129 s->T1= iso_clip(factor*(basic_t1-2) + 2 + 3*s->near, s->near+1, s->maxval);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
130 if(s->T2==0 || reset_all)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
131 s->T2= iso_clip(factor*(basic_t2-3) + 3 + 5*s->near, s->T1, s->maxval);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
132 if(s->T3==0 || reset_all)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
133 s->T3= iso_clip(factor*(basic_t3-4) + 4 + 7*s->near, s->T2, s->maxval);
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
134 }else{
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
135 factor= 256 / (s->maxval + 1);
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
136
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
137 if(s->T1==0 || reset_all)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
138 s->T1= iso_clip(FFMAX(2, basic_t1/factor + 3*s->near), s->near+1, s->maxval);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
139 if(s->T2==0 || reset_all)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
140 s->T2= iso_clip(FFMAX(3, basic_t2/factor + 5*s->near), s->T1, s->maxval);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
141 if(s->T3==0 || reset_all)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
142 s->T3= iso_clip(FFMAX(4, basic_t3/factor + 6*s->near), s->T2, s->maxval);
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
143 }
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
144
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
145 if(s->reset==0 || reset_all) s->reset= 64;
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
146 // av_log(NULL, AV_LOG_DEBUG, "[JPEG-LS RESET] T=%i,%i,%i\n", s->T1, s->T2, s->T3);
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
147 }
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
148
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
149
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
150 /********** Decoder-specific functions **********/
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
151
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
152 /**
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
153 * Decode LSE block with initialization parameters
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
154 */
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
155 static int decode_lse(MJpegDecodeContext *s)
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
156 {
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
157 int len, id;
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
158
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
159 /* XXX: verify len field validity */
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
160 len = get_bits(&s->gb, 16);
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
161 id = get_bits(&s->gb, 8);
3063
f02d0b59279c Remove all stray tabs and trailing whitespace, this time for good.
diego
parents: 3036
diff changeset
162
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
163 switch(id){
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
164 case 1:
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
165 s->maxval= get_bits(&s->gb, 16);
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
166 s->t1= get_bits(&s->gb, 16);
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
167 s->t2= get_bits(&s->gb, 16);
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
168 s->t3= get_bits(&s->gb, 16);
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
169 s->reset= get_bits(&s->gb, 16);
3063
f02d0b59279c Remove all stray tabs and trailing whitespace, this time for good.
diego
parents: 3036
diff changeset
170
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
171 // reset_ls_coding_parameters(s, 0);
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
172 //FIXME quant table?
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
173 break;
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
174 case 2:
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
175 case 3:
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
176 av_log(s->avctx, AV_LOG_ERROR, "palette not supported\n");
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
177 return -1;
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
178 case 4:
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
179 av_log(s->avctx, AV_LOG_ERROR, "oversize image not supported\n");
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
180 return -1;
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
181 default:
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
182 av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
183 return -1;
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
184 }
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
185 // av_log(s->avctx, AV_LOG_DEBUG, "ID=%i, T=%i,%i,%i\n", id, s->t1, s->t2, s->t3);
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
186
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
187 return 0;
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
188 }
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
189
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
190
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
191 /**
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
192 * Get context-dependent Golomb code, decode it and update context
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
193 */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
194 static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
195 int k, ret;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
196
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
197 for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
198
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
199 #ifdef JLS_BROKEN
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
200 if(!show_bits_long(gb, 32))return -1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
201 #endif
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
202 ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
203
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
204 /* decode mapped error */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
205 if(ret & 1)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
206 ret = -((ret + 1) >> 1);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
207 else
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
208 ret >>= 1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
209
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
210 /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
211 if(!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
212 ret = -(ret + 1);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
213
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
214 state->A[Q] += ABS(ret);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
215 ret *= state->twonear;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
216 state->B[Q] += ret;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
217
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
218 if(state->N[Q] == state->reset) {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
219 state->A[Q] >>= 1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
220 state->B[Q] >>= 1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
221 state->N[Q] >>= 1;
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
222 }
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
223 state->N[Q]++;
3063
f02d0b59279c Remove all stray tabs and trailing whitespace, this time for good.
diego
parents: 3036
diff changeset
224
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
225 if(state->B[Q] <= -state->N[Q]) {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
226 state->B[Q] += state->N[Q];
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
227 if(state->C[Q] > -128)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
228 state->C[Q]--;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
229 if(state->B[Q] <= -state->N[Q])
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
230 state->B[Q] = -state->N[Q] + 1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
231 }else if(state->B[Q] > 0){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
232 state->B[Q] -= state->N[Q];
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
233 if(state->C[Q] < 127)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
234 state->C[Q]++;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
235 if(state->B[Q] > 0)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
236 state->B[Q] = 0;
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
237 }
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
238
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
239 return ret;
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
240 }
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
241
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
242 /**
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
243 * Get Golomb code, decode it and update state for run termination
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
244 */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
245 static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state, int RItype, int limit_add){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
246 int k, ret, temp, map;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
247 int Q = 365 + RItype;
3063
f02d0b59279c Remove all stray tabs and trailing whitespace, this time for good.
diego
parents: 3036
diff changeset
248
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
249 if(!RItype)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
250 temp = state->A[Q];
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
251 else
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
252 temp = state->A[Q] + (state->N[Q] >> 1);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
253
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
254 for(k = 0; (state->N[Q] << k) < temp; k++);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
255
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
256 #ifdef JLS_BROKEN
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
257 if(!show_bits_long(gb, 32))return -1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
258 #endif
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
259 ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1, state->qbpp);
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
260
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
261 /* decode mapped error */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
262 map = 0;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
263 if(!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
264 map = 1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
265 ret += RItype + map;
3063
f02d0b59279c Remove all stray tabs and trailing whitespace, this time for good.
diego
parents: 3036
diff changeset
266
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
267 if(ret & 1){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
268 ret = map - ((ret + 1) >> 1);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
269 state->B[Q]++;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
270 } else {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
271 ret = ret >> 1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
272 }
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
273
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
274 /* update state */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
275 state->A[Q] += ABS(ret) - RItype;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
276 ret *= state->twonear;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
277 if(state->N[Q] == state->reset){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
278 state->A[Q] >>=1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
279 state->B[Q] >>=1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
280 state->N[Q] >>=1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
281 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
282 state->N[Q]++;
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
283
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
284 return ret;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
285 }
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
286
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
287 /**
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
288 * Decode one line of image
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
289 */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
290 static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s, uint8_t *last, uint8_t *dst, int last2, int w, int stride, int comp){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
291 int i, x = 0;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
292 int Ra, Rb, Rc, Rd;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
293 int D0, D1, D2;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
294
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
295 while(x < w) {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
296 int err, pred;
3063
f02d0b59279c Remove all stray tabs and trailing whitespace, this time for good.
diego
parents: 3036
diff changeset
297
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
298 /* compute gradients */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
299 Ra = x ? dst[x - stride] : last[x];
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
300 Rb = last[x];
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
301 Rc = x ? last[x - stride] : last2;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
302 Rd = (x >= w - stride) ? last[x] : last[x + stride];
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
303 D0 = Rd - Rb;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
304 D1 = Rb - Rc;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
305 D2 = Rc - Ra;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
306 /* run mode */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
307 if((ABS(D0) <= state->near) && (ABS(D1) <= state->near) && (ABS(D2) <= state->near)) {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
308 int r;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
309 int RItype;
3063
f02d0b59279c Remove all stray tabs and trailing whitespace, this time for good.
diego
parents: 3036
diff changeset
310
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
311 /* decode full runs while available */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
312 while(get_bits1(&s->gb)) {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
313 int r;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
314 r = 1 << log2_run[state->run_index[comp]];
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
315 if(x + r * stride > w) {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
316 r = (w - x) / stride;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
317 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
318 for(i = 0; i < r; i++) {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
319 dst[x] = Ra;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
320 x += stride;
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
321 }
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
322 /* if EOL reached, we stop decoding */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
323 if(r != (1 << log2_run[state->run_index[comp]]))
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
324 return;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
325 if(state->run_index[comp] < 31)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
326 state->run_index[comp]++;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
327 if(x + stride > w)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
328 return;
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
329 }
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
330 /* decode aborted run */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
331 r = log2_run[state->run_index[comp]];
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
332 if(r)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
333 r = get_bits_long(&s->gb, r);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
334 for(i = 0; i < r; i++) {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
335 dst[x] = Ra;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
336 x += stride;
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
337 }
3063
f02d0b59279c Remove all stray tabs and trailing whitespace, this time for good.
diego
parents: 3036
diff changeset
338
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
339 /* decode run termination value */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
340 Rb = last[x];
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
341 RItype = (ABS(Ra - Rb) <= state->near) ? 1 : 0;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
342 err = ls_get_code_runterm(&s->gb, state, RItype, log2_run[state->run_index[comp]]);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
343 if(state->run_index[comp])
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
344 state->run_index[comp]--;
3063
f02d0b59279c Remove all stray tabs and trailing whitespace, this time for good.
diego
parents: 3036
diff changeset
345
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
346 if(state->near && RItype){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
347 pred = Ra + err;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
348 } else {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
349 if(Rb < Ra)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
350 pred = Rb - err;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
351 else
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
352 pred = Rb + err;
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
353 }
3063
f02d0b59279c Remove all stray tabs and trailing whitespace, this time for good.
diego
parents: 3036
diff changeset
354
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
355 if(state->near){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
356 if(pred < -state->near)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
357 pred += state->range * state->twonear;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
358 else if(pred > state->maxval + state->near)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
359 pred -= state->range * state->twonear;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
360 pred = clip(pred, 0, state->maxval);
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
361 }
3063
f02d0b59279c Remove all stray tabs and trailing whitespace, this time for good.
diego
parents: 3036
diff changeset
362
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
363 dst[x] = pred;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
364 x += stride;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
365 } else { /* regular mode */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
366 int context, sign;
3063
f02d0b59279c Remove all stray tabs and trailing whitespace, this time for good.
diego
parents: 3036
diff changeset
367
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
368 context = quantize(state, D0) * 81 + quantize(state, D1) * 9 + quantize(state, D2);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
369 pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
370
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
371 if(context < 0){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
372 context = -context;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
373 sign = 1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
374 }else{
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
375 sign = 0;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
376 }
3063
f02d0b59279c Remove all stray tabs and trailing whitespace, this time for good.
diego
parents: 3036
diff changeset
377
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
378 if(sign){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
379 pred = clip(pred - state->C[context], 0, state->maxval);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
380 err = -ls_get_code_regular(&s->gb, state, context);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
381 } else {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
382 pred = clip(pred + state->C[context], 0, state->maxval);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
383 err = ls_get_code_regular(&s->gb, state, context);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
384 }
3063
f02d0b59279c Remove all stray tabs and trailing whitespace, this time for good.
diego
parents: 3036
diff changeset
385
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
386 /* we have to do something more for near-lossless coding */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
387 pred += err;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
388 if(state->near) {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
389 if(pred < -state->near)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
390 pred += state->range * state->twonear;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
391 else if(pred > state->maxval + state->near)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
392 pred -= state->range * state->twonear;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
393 pred = clip(pred, 0, state->maxval);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
394 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
395
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
396 dst[x] = pred;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
397 x += stride;
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
398 }
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
399 }
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
400 }
3063
f02d0b59279c Remove all stray tabs and trailing whitespace, this time for good.
diego
parents: 3036
diff changeset
401
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
402 static int ls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
403 int i, t = 0;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
404 uint8_t *zero, *last, *cur;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
405 JLSState *state;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
406 int off, stride, width;
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
407
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
408 zero = av_mallocz(s->picture.linesize[0]);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
409 last = zero;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
410 cur = s->picture.data[0];
3063
f02d0b59279c Remove all stray tabs and trailing whitespace, this time for good.
diego
parents: 3036
diff changeset
411
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
412 state = av_mallocz(sizeof(JLSState));
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
413 /* initialize JPEG-LS state from JPEG parameters */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
414 state->near = near;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
415 state->bpp = (s->bits < 2) ? 2 : s->bits;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
416 state->maxval = s->maxval;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
417 state->T1 = s->t1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
418 state->T2 = s->t2;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
419 state->T3 = s->t3;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
420 state->reset = s->reset;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
421 reset_ls_coding_parameters(state, 0);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
422 ls_init_state(state);
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
423
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
424 // 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);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
425 // 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);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
426 if(ilv == 0) { /* separate planes */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
427 off = s->cur_scan - 1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
428 stride = (s->nb_components > 1) ? 3 : 1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
429 width = s->width * stride;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
430 cur += off;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
431 for(i = 0; i < s->height; i++) {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
432 ls_decode_line(state, s, last, cur, t, width, stride, off);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
433 t = last[0];
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
434 last = cur;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
435 cur += s->picture.linesize[0];
3063
f02d0b59279c Remove all stray tabs and trailing whitespace, this time for good.
diego
parents: 3036
diff changeset
436
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
437 if (s->restart_interval && !--s->restart_count) {
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
438 align_get_bits(&s->gb);
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
439 skip_bits(&s->gb, 16); /* skip RSTn */
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
440 }
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
441 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
442 } else if(ilv == 1) { /* line interleaving */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
443 int j;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
444 int Rc[3] = {0, 0, 0};
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
445 memset(cur, 0, s->picture.linesize[0]);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
446 width = s->width * 3;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
447 for(i = 0; i < s->height; i++) {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
448 for(j = 0; j < 3; j++) {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
449 ls_decode_line(state, s, last + j, cur + j, Rc[j], width, 3, j);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
450 Rc[j] = last[j];
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
451
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
452 if (s->restart_interval && !--s->restart_count) {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
453 align_get_bits(&s->gb);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
454 skip_bits(&s->gb, 16); /* skip RSTn */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
455 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
456 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
457 last = cur;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
458 cur += s->picture.linesize[0];
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
459 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
460 } else if(ilv == 2) { /* sample interleaving */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
461 av_log(s->avctx, AV_LOG_ERROR, "Sample interleaved images are not supported.\n");
3773
7364a7e00e86 free memory before return
kostya
parents: 3079
diff changeset
462 av_free(state);
7364a7e00e86 free memory before return
kostya
parents: 3079
diff changeset
463 av_free(zero);
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
464 return -1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
465 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
466
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
467 av_free(state);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
468 av_free(zero);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
469
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
470 return 0;
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
471 }
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
472
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
473 #if defined(CONFIG_ENCODERS) && defined(CONFIG_JPEGLS_ENCODER)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
474 /********** Encoder-specific functions **********/
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
475
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
476 /**
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
477 * Encode error from regular symbol
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
478 */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
479 static inline void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q, int err){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
480 int k;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
481 int val;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
482 int map;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
483
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
484 for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
485
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
486 map = !state->near && !k && (2 * state->B[Q] <= -state->N[Q]);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
487
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
488 if(err < 0)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
489 err += state->range;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
490 if(err >= ((state->range + 1) >> 1)) {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
491 err -= state->range;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
492 val = 2 * ABS(err) - 1 - map;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
493 } else
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
494 val = 2 * err + map;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
495
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
496 set_ur_golomb_jpegls(pb, val, k, state->limit, state->qbpp);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
497
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
498 state->A[Q] += ABS(err);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
499 state->B[Q] += err * state->twonear;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
500
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
501 if(state->N[Q] == state->reset) {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
502 state->A[Q] >>= 1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
503 state->B[Q] >>= 1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
504 state->N[Q] >>= 1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
505 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
506 state->N[Q]++;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
507
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
508 if(state->B[Q] <= -state->N[Q]) {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
509 state->B[Q] += state->N[Q];
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
510 if(state->C[Q] > -128)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
511 state->C[Q]--;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
512 if(state->B[Q] <= -state->N[Q])
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
513 state->B[Q] = -state->N[Q] + 1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
514 }else if(state->B[Q] > 0){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
515 state->B[Q] -= state->N[Q];
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
516 if(state->C[Q] < 127)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
517 state->C[Q]++;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
518 if(state->B[Q] > 0)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
519 state->B[Q] = 0;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
520 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
521 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
522
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
523 /**
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
524 * Encode error from run termination
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
525 */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
526 static inline void ls_encode_runterm(JLSState *state, PutBitContext *pb, int RItype, int err, int limit_add){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
527 int k;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
528 int val, map;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
529 int Q = 365 + RItype;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
530 int temp;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
531
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
532 temp = state->A[Q];
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
533 if(RItype)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
534 temp += state->N[Q] >> 1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
535 for(k = 0; (state->N[Q] << k) < temp; k++);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
536 map = 0;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
537 if(!k && err && (2 * state->B[Q] < state->N[Q]))
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
538 map = 1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
539
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
540 if(err < 0)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
541 val = - (2 * err) - 1 - RItype + map;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
542 else
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
543 val = 2 * err - RItype - map;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
544 set_ur_golomb_jpegls(pb, val, k, state->limit - limit_add - 1, state->qbpp);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
545
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
546 if(err < 0)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
547 state->B[Q]++;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
548 state->A[Q] += (val + 1 - RItype) >> 1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
549
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
550 if(state->N[Q] == state->reset) {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
551 state->A[Q] >>= 1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
552 state->B[Q] >>= 1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
553 state->N[Q] >>= 1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
554 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
555 state->N[Q]++;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
556 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
557
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
558 /**
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
559 * Encode run value as specified by JPEG-LS standard
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
560 */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
561 static inline void ls_encode_run(JLSState *state, PutBitContext *pb, int run, int comp, int trail){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
562 while(run >= (1 << log2_run[state->run_index[comp]])){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
563 put_bits(pb, 1, 1);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
564 run -= 1 << log2_run[state->run_index[comp]];
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
565 if(state->run_index[comp] < 31)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
566 state->run_index[comp]++;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
567 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
568 /* if hit EOL, encode another full run, else encode aborted run */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
569 if(!trail && run) {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
570 put_bits(pb, 1, 1);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
571 }else if(trail){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
572 put_bits(pb, 1, 0);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
573 if(log2_run[state->run_index[comp]])
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
574 put_bits(pb, log2_run[state->run_index[comp]], run);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
575 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
576 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
577
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
578 /**
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
579 * Encode one line of image
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
580 */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
581 static inline void ls_encode_line(JLSState *state, PutBitContext *pb, uint8_t *last, uint8_t *cur, int last2, int w, int stride, int comp){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
582 int x = 0;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
583 int Ra, Rb, Rc, Rd;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
584 int D0, D1, D2;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
585
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
586 while(x < w) {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
587 int err, pred, sign;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
588
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
589 /* compute gradients */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
590 Ra = x ? cur[x - stride] : last[x];
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
591 Rb = last[x];
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
592 Rc = x ? last[x - stride] : last2;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
593 Rd = (x >= w - stride) ? last[x] : last[x + stride];
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
594 D0 = Rd - Rb;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
595 D1 = Rb - Rc;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
596 D2 = Rc - Ra;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
597
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
598 /* run mode */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
599 if((ABS(D0) <= state->near) && (ABS(D1) <= state->near) && (ABS(D2) <= state->near)) {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
600 int RUNval, RItype, run;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
601
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
602 run = 0;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
603 RUNval = Ra;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
604 while(x < w && (ABS(cur[x] - RUNval) <= state->near)){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
605 run++;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
606 cur[x] = Ra;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
607 x += stride;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
608 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
609 ls_encode_run(state, pb, run, comp, x < w);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
610 if(x >= w)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
611 return;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
612 Rb = last[x];
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
613 RItype = (ABS(Ra - Rb) <= state->near);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
614 pred = RItype ? Ra : Rb;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
615 err = cur[x] - pred;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
616
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
617 if(!RItype && Ra > Rb)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
618 err = -err;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
619
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
620 if(state->near){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
621 if(err > 0)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
622 err = (state->near + err) / state->twonear;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
623 else
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
624 err = -(state->near - err) / state->twonear;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
625
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
626 if(RItype || (Rb >= Ra))
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
627 Ra = clip(pred + err * state->twonear, 0, state->maxval);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
628 else
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
629 Ra = clip(pred - err * state->twonear, 0, state->maxval);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
630 cur[x] = Ra;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
631 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
632 if(err < 0)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
633 err += state->range;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
634 if(err >= ((state->range + 1) >> 1))
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
635 err -= state->range;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
636
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
637 ls_encode_runterm(state, pb, RItype, err, log2_run[state->run_index[comp]]);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
638
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
639 if(state->run_index[comp] > 0)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
640 state->run_index[comp]--;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
641 x += stride;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
642 } else { /* regular mode */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
643 int context;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
644
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
645 context = quantize(state, D0) * 81 + quantize(state, D1) * 9 + quantize(state, D2);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
646 pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
647
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
648 if(context < 0){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
649 context = -context;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
650 sign = 1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
651 pred = clip(pred - state->C[context], 0, state->maxval);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
652 err = pred - cur[x];
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
653 }else{
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
654 sign = 0;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
655 pred = clip(pred + state->C[context], 0, state->maxval);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
656 err = cur[x] - pred;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
657 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
658
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
659 if(state->near){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
660 if(err > 0)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
661 err = (state->near + err) / state->twonear;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
662 else
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
663 err = -(state->near - err) / state->twonear;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
664 if(!sign)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
665 Ra = clip(pred + err * state->twonear, 0, state->maxval);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
666 else
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
667 Ra = clip(pred - err * state->twonear, 0, state->maxval);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
668 cur[x] = Ra;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
669 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
670
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
671 ls_encode_regular(state, pb, context, err);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
672 x += stride;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
673 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
674 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
675 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
676
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
677 static void ls_store_lse(JLSState *state, PutBitContext *pb){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
678 /* Test if we have default params and don't need to store LSE */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
679 JLSState state2;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
680 memset(&state2, 0, sizeof(JLSState));
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
681 state2.bpp = 8;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
682 state2.near = state->near;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
683 reset_ls_coding_parameters(&state2, 1);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
684 if(state->T1 == state2.T1 && state->T2 == state2.T2 && state->T3 == state2.T3 && state->reset == state2.reset)
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
685 return;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
686 /* store LSE type 1 */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
687 put_marker(pb, LSE);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
688 put_bits(pb, 16, 13);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
689 put_bits(pb, 8, 1);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
690 put_bits(pb, 16, state->maxval);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
691 put_bits(pb, 16, state->T1);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
692 put_bits(pb, 16, state->T2);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
693 put_bits(pb, 16, state->T3);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
694 put_bits(pb, 16, state->reset);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
695 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
696
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
697 static int encode_picture_ls(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
698 JpeglsContext * const s = avctx->priv_data;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
699 AVFrame *pict = data;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
700 AVFrame * const p= (AVFrame*)&s->picture;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
701 const int near = avctx->prediction_method;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
702 PutBitContext pb, pb2;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
703 GetBitContext gb;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
704 uint8_t *buf2, *zero, *cur, *last;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
705 JLSState *state;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
706 int i, size;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
707 int comps;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
708
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
709 buf2 = av_malloc(buf_size);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
710
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
711 init_put_bits(&pb, buf, buf_size);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
712 init_put_bits(&pb2, buf2, buf_size);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
713
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
714 *p = *pict;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
715 p->pict_type= FF_I_TYPE;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
716 p->key_frame= 1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
717
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
718 comps = (avctx->pix_fmt == PIX_FMT_GRAY8) ? 1 : 3;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
719
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
720 /* write our own JPEG header, can't use mjpeg_picture_header */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
721 put_marker(&pb, SOI);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
722 put_marker(&pb, SOF48);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
723 put_bits(&pb, 16, 8 + comps * 3); // header size depends on components
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
724 put_bits(&pb, 8, 8); // bpp
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
725 put_bits(&pb, 16, avctx->height);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
726 put_bits(&pb, 16, avctx->width);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
727 put_bits(&pb, 8, comps); // components
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
728 for(i = 1; i <= comps; i++) {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
729 put_bits(&pb, 8, i); // component ID
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
730 put_bits(&pb, 8, 0x11); // subsampling: none
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
731 put_bits(&pb, 8, 0); // Tiq, used by JPEG-LS ext
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
732 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
733
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
734 put_marker(&pb, SOS);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
735 put_bits(&pb, 16, 6 + comps * 2);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
736 put_bits(&pb, 8, comps);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
737 for(i = 1; i <= comps; i++) {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
738 put_bits(&pb, 8, i); // component ID
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
739 put_bits(&pb, 8, 0); // mapping index: none
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
740 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
741 put_bits(&pb, 8, near);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
742 put_bits(&pb, 8, (comps > 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
743 put_bits(&pb, 8, 0); // point transform: none
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
744
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
745 state = av_mallocz(sizeof(JLSState));
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
746 /* initialize JPEG-LS state from JPEG parameters */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
747 state->near = near;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
748 state->bpp = 8;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
749 reset_ls_coding_parameters(state, 0);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
750 ls_init_state(state);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
751
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
752 ls_store_lse(state, &pb);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
753
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
754 zero = av_mallocz(p->linesize[0]);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
755 last = zero;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
756 cur = p->data[0];
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
757 if(avctx->pix_fmt == PIX_FMT_GRAY8){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
758 int t = 0;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
759
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
760 for(i = 0; i < avctx->height; i++) {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
761 ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
762 t = last[0];
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
763 last = cur;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
764 cur += p->linesize[0];
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
765 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
766 }else if(avctx->pix_fmt == PIX_FMT_RGB24){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
767 int j, width;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
768 int Rc[3] = {0, 0, 0};
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
769
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
770 width = avctx->width * 3;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
771 for(i = 0; i < avctx->height; i++) {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
772 for(j = 0; j < 3; j++) {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
773 ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
774 Rc[j] = last[j];
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
775 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
776 last = cur;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
777 cur += s->picture.linesize[0];
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
778 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
779 }else if(avctx->pix_fmt == PIX_FMT_BGR24){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
780 int j, width;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
781 int Rc[3] = {0, 0, 0};
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
782
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
783 width = avctx->width * 3;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
784 for(i = 0; i < avctx->height; i++) {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
785 for(j = 2; j >= 0; j--) {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
786 ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
787 Rc[j] = last[j];
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
788 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
789 last = cur;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
790 cur += s->picture.linesize[0];
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
791 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
792 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
793
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
794 av_free(zero);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
795 av_free(state);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
796
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
797 flush_put_bits(&pb2);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
798 /* do escape coding */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
799 size = put_bits_count(&pb2) >> 3;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
800 init_get_bits(&gb, buf2, size);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
801 while(get_bits_count(&gb) < size * 8){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
802 int v;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
803 v = get_bits(&gb, 8);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
804 put_bits(&pb, 8, v);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
805 if(v == 0xFF){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
806 v = get_bits(&gb, 7);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
807 put_bits(&pb, 8, v);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
808 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
809 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
810 align_put_bits(&pb);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
811 av_free(buf2);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
812
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
813 /* End of image */
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
814 put_marker(&pb, EOI);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
815 flush_put_bits(&pb);
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
816
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
817 emms_c();
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
818
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
819 return put_bits_count(&pb) >> 3;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
820 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
821
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
822 static int encode_init_ls(AVCodecContext *ctx) {
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
823 JpeglsContext *c = (JpeglsContext*)ctx->priv_data;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
824
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
825 c->avctx = ctx;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
826 ctx->coded_frame = &c->picture;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
827
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
828 if(ctx->pix_fmt != PIX_FMT_GRAY8 && ctx->pix_fmt != PIX_FMT_RGB24 && ctx->pix_fmt != PIX_FMT_BGR24){
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
829 av_log(ctx, AV_LOG_ERROR, "Only grayscale and RGB24/BGR24 images are supported\n");
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
830 return -1;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
831 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
832 return 0;
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
833 }
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
834
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
835 AVCodec jpegls_encoder = { //FIXME avoid MPV_* lossless jpeg shouldnt need them
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
836 "jpegls",
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
837 CODEC_TYPE_VIDEO,
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
838 CODEC_ID_JPEGLS,
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
839 sizeof(JpeglsContext),
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
840 encode_init_ls,
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
841 encode_picture_ls,
3078
1e63c12c2c71 JPEG-LS codec by (Kostya | kostya.forjunk gmail com)
michael
parents: 3063
diff changeset
842 NULL,
3079
545342e01885 list of supported pix_fmts
michael
parents: 3078
diff changeset
843 .pix_fmts= (enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB24, PIX_FMT_GRAY8, -1},
2970
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
844 };
583b6ac45c07 unfinished jpeg-ls codec (feel free to finish it ...)
michael
parents:
diff changeset
845 #endif