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