00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028 #include "avcodec.h"
00029 #include "bitstream.h"
00030 #include "golomb.h"
00031 #include "mathops.h"
00032 #include "mjpeg.h"
00033 #include "mjpegdec.h"
00034 #include "jpegls.h"
00035 #include "jpeglsdec.h"
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00052 int ff_jpegls_decode_lse(MJpegDecodeContext *s)
00053 {
00054 int len, id;
00055
00056
00057 len = get_bits(&s->gb, 16);
00058 id = get_bits(&s->gb, 8);
00059
00060 switch(id){
00061 case 1:
00062 s->maxval= get_bits(&s->gb, 16);
00063 s->t1= get_bits(&s->gb, 16);
00064 s->t2= get_bits(&s->gb, 16);
00065 s->t3= get_bits(&s->gb, 16);
00066 s->reset= get_bits(&s->gb, 16);
00067
00068
00069
00070 break;
00071 case 2:
00072 case 3:
00073 av_log(s->avctx, AV_LOG_ERROR, "palette not supported\n");
00074 return -1;
00075 case 4:
00076 av_log(s->avctx, AV_LOG_ERROR, "oversize image not supported\n");
00077 return -1;
00078 default:
00079 av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
00080 return -1;
00081 }
00082
00083
00084 return 0;
00085 }
00086
00090 static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q){
00091 int k, ret;
00092
00093 for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
00094
00095 #ifdef JLS_BROKEN
00096 if(!show_bits_long(gb, 32))return -1;
00097 #endif
00098 ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
00099
00100
00101 if(ret & 1)
00102 ret = -((ret + 1) >> 1);
00103 else
00104 ret >>= 1;
00105
00106
00107 if(!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
00108 ret = -(ret + 1);
00109
00110 ret= ff_jpegls_update_state_regular(state, Q, ret);
00111
00112 return ret;
00113 }
00114
00118 static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state, int RItype, int limit_add){
00119 int k, ret, temp, map;
00120 int Q = 365 + RItype;
00121
00122 temp= state->A[Q];
00123 if(RItype)
00124 temp += state->N[Q] >> 1;
00125
00126 for(k = 0; (state->N[Q] << k) < temp; k++);
00127
00128 #ifdef JLS_BROKEN
00129 if(!show_bits_long(gb, 32))return -1;
00130 #endif
00131 ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1, state->qbpp);
00132
00133
00134 map = 0;
00135 if(!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
00136 map = 1;
00137 ret += RItype + map;
00138
00139 if(ret & 1){
00140 ret = map - ((ret + 1) >> 1);
00141 state->B[Q]++;
00142 } else {
00143 ret = ret >> 1;
00144 }
00145
00146
00147 state->A[Q] += FFABS(ret) - RItype;
00148 ret *= state->twonear;
00149 ff_jpegls_downscale_state(state, Q);
00150
00151 return ret;
00152 }
00153
00157 static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s, void *last, void *dst, int last2, int w, int stride, int comp, int bits){
00158 int i, x = 0;
00159 int Ra, Rb, Rc, Rd;
00160 int D0, D1, D2;
00161
00162 while(x < w) {
00163 int err, pred;
00164
00165
00166 Ra = x ? R(dst, x - stride) : R(last, x);
00167 Rb = R(last, x);
00168 Rc = x ? R(last, x - stride) : last2;
00169 Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
00170 D0 = Rd - Rb;
00171 D1 = Rb - Rc;
00172 D2 = Rc - Ra;
00173
00174 if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
00175 int r;
00176 int RItype;
00177
00178
00179 while(get_bits1(&s->gb)) {
00180 int r;
00181 r = 1 << ff_log2_run[state->run_index[comp]];
00182 if(x + r * stride > w) {
00183 r = (w - x) / stride;
00184 }
00185 for(i = 0; i < r; i++) {
00186 W(dst, x, Ra);
00187 x += stride;
00188 }
00189
00190 if(r != (1 << ff_log2_run[state->run_index[comp]]))
00191 return;
00192 if(state->run_index[comp] < 31)
00193 state->run_index[comp]++;
00194 if(x + stride > w)
00195 return;
00196 }
00197
00198 r = ff_log2_run[state->run_index[comp]];
00199 if(r)
00200 r = get_bits_long(&s->gb, r);
00201 for(i = 0; i < r; i++) {
00202 W(dst, x, Ra);
00203 x += stride;
00204 }
00205
00206
00207 Rb = R(last, x);
00208 RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
00209 err = ls_get_code_runterm(&s->gb, state, RItype, ff_log2_run[state->run_index[comp]]);
00210 if(state->run_index[comp])
00211 state->run_index[comp]--;
00212
00213 if(state->near && RItype){
00214 pred = Ra + err;
00215 } else {
00216 if(Rb < Ra)
00217 pred = Rb - err;
00218 else
00219 pred = Rb + err;
00220 }
00221 } else {
00222 int context, sign;
00223
00224 context = ff_jpegls_quantize(state, D0) * 81 + ff_jpegls_quantize(state, D1) * 9 + ff_jpegls_quantize(state, D2);
00225 pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
00226
00227 if(context < 0){
00228 context = -context;
00229 sign = 1;
00230 }else{
00231 sign = 0;
00232 }
00233
00234 if(sign){
00235 pred = av_clip(pred - state->C[context], 0, state->maxval);
00236 err = -ls_get_code_regular(&s->gb, state, context);
00237 } else {
00238 pred = av_clip(pred + state->C[context], 0, state->maxval);
00239 err = ls_get_code_regular(&s->gb, state, context);
00240 }
00241
00242
00243 pred += err;
00244 }
00245 if(state->near){
00246 if(pred < -state->near)
00247 pred += state->range * state->twonear;
00248 else if(pred > state->maxval + state->near)
00249 pred -= state->range * state->twonear;
00250 pred = av_clip(pred, 0, state->maxval);
00251 }
00252
00253 pred &= state->maxval;
00254 W(dst, x, pred);
00255 x += stride;
00256 }
00257 }
00258
00259 int ff_jpegls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv){
00260 int i, t = 0;
00261 uint8_t *zero, *last, *cur;
00262 JLSState *state;
00263 int off = 0, stride = 1, width, shift;
00264
00265 zero = av_mallocz(s->picture.linesize[0]);
00266 last = zero;
00267 cur = s->picture.data[0];
00268
00269 state = av_mallocz(sizeof(JLSState));
00270
00271 state->near = near;
00272 state->bpp = (s->bits < 2) ? 2 : s->bits;
00273 state->maxval = s->maxval;
00274 state->T1 = s->t1;
00275 state->T2 = s->t2;
00276 state->T3 = s->t3;
00277 state->reset = s->reset;
00278 ff_jpegls_reset_coding_parameters(state, 0);
00279 ff_jpegls_init_state(state);
00280
00281 if(s->bits <= 8)
00282 shift = point_transform + (8 - s->bits);
00283 else
00284 shift = point_transform + (16 - s->bits);
00285
00286
00287
00288 if(ilv == 0) {
00289 off = s->cur_scan - 1;
00290 stride = (s->nb_components > 1) ? 3 : 1;
00291 width = s->width * stride;
00292 cur += off;
00293 for(i = 0; i < s->height; i++) {
00294 if(s->bits <= 8){
00295 ls_decode_line(state, s, last, cur, t, width, stride, off, 8);
00296 t = last[0];
00297 }else{
00298 ls_decode_line(state, s, last, cur, t, width, stride, off, 16);
00299 t = *((uint16_t*)last);
00300 }
00301 last = cur;
00302 cur += s->picture.linesize[0];
00303
00304 if (s->restart_interval && !--s->restart_count) {
00305 align_get_bits(&s->gb);
00306 skip_bits(&s->gb, 16);
00307 }
00308 }
00309 } else if(ilv == 1) {
00310 int j;
00311 int Rc[3] = {0, 0, 0};
00312 memset(cur, 0, s->picture.linesize[0]);
00313 width = s->width * 3;
00314 for(i = 0; i < s->height; i++) {
00315 for(j = 0; j < 3; j++) {
00316 ls_decode_line(state, s, last + j, cur + j, Rc[j], width, 3, j, 8);
00317 Rc[j] = last[j];
00318
00319 if (s->restart_interval && !--s->restart_count) {
00320 align_get_bits(&s->gb);
00321 skip_bits(&s->gb, 16);
00322 }
00323 }
00324 last = cur;
00325 cur += s->picture.linesize[0];
00326 }
00327 } else if(ilv == 2) {
00328 av_log(s->avctx, AV_LOG_ERROR, "Sample interleaved images are not supported.\n");
00329 av_free(state);
00330 av_free(zero);
00331 return -1;
00332 }
00333
00334 if(shift){
00335 int x, w;
00336
00337 w = s->width * s->nb_components;
00338
00339 if(s->bits <= 8){
00340 uint8_t *src = s->picture.data[0];
00341
00342 for(i = 0; i < s->height; i++){
00343 for(x = off; x < w; x+= stride){
00344 src[x] <<= shift;
00345 }
00346 src += s->picture.linesize[0];
00347 }
00348 }else{
00349 uint16_t *src = (uint16_t*) s->picture.data[0];
00350
00351 for(i = 0; i < s->height; i++){
00352 for(x = 0; x < w; x++){
00353 src[x] <<= shift;
00354 }
00355 src += s->picture.linesize[0]/2;
00356 }
00357 }
00358 }
00359 av_free(state);
00360 av_free(zero);
00361
00362 return 0;
00363 }
00364
00365
00366 AVCodec jpegls_decoder = {
00367 "jpegls",
00368 CODEC_TYPE_VIDEO,
00369 CODEC_ID_JPEGLS,
00370 sizeof(MJpegDecodeContext),
00371 ff_mjpeg_decode_init,
00372 NULL,
00373 ff_mjpeg_decode_end,
00374 ff_mjpeg_decode_frame,
00375 CODEC_CAP_DR1,
00376 .long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),
00377 };