• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/jpeglsenc.c

Go to the documentation of this file.
00001 /*
00002  * JPEG-LS encoder
00003  * Copyright (c) 2003 Michael Niedermayer
00004  * Copyright (c) 2006 Konstantin Shishkov
00005  *
00006  * This file is part of FFmpeg.
00007  *
00008  * FFmpeg is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * FFmpeg is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with FFmpeg; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00028 #include "avcodec.h"
00029 #include "get_bits.h"
00030 #include "golomb.h"
00031 #include "mathops.h"
00032 #include "dsputil.h"
00033 #include "mjpeg.h"
00034 #include "jpegls.h"
00035 
00036 
00040 static inline void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q, int err){
00041     int k;
00042     int val;
00043     int map;
00044 
00045     for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
00046 
00047     map = !state->near && !k && (2 * state->B[Q] <= -state->N[Q]);
00048 
00049     if(err < 0)
00050         err += state->range;
00051     if(err >= ((state->range + 1) >> 1)) {
00052         err -= state->range;
00053         val = 2 * FFABS(err) - 1 - map;
00054     } else
00055         val = 2 * err + map;
00056 
00057     set_ur_golomb_jpegls(pb, val, k, state->limit, state->qbpp);
00058 
00059     ff_jpegls_update_state_regular(state, Q, err);
00060 }
00061 
00065 static inline void ls_encode_runterm(JLSState *state, PutBitContext *pb, int RItype, int err, int limit_add){
00066     int k;
00067     int val, map;
00068     int Q = 365 + RItype;
00069     int temp;
00070 
00071     temp = state->A[Q];
00072     if(RItype)
00073         temp += state->N[Q] >> 1;
00074     for(k = 0; (state->N[Q] << k) < temp; k++);
00075     map = 0;
00076     if(!k && err && (2 * state->B[Q] < state->N[Q]))
00077         map = 1;
00078 
00079     if(err < 0)
00080         val = - (2 * err) - 1 - RItype + map;
00081     else
00082         val = 2 * err - RItype - map;
00083     set_ur_golomb_jpegls(pb, val, k, state->limit - limit_add - 1, state->qbpp);
00084 
00085     if(err < 0)
00086         state->B[Q]++;
00087     state->A[Q] += (val + 1 - RItype) >> 1;
00088 
00089     ff_jpegls_downscale_state(state, Q);
00090 }
00091 
00095 static inline void ls_encode_run(JLSState *state, PutBitContext *pb, int run, int comp, int trail){
00096     while(run >= (1 << ff_log2_run[state->run_index[comp]])){
00097         put_bits(pb, 1, 1);
00098         run -= 1 << ff_log2_run[state->run_index[comp]];
00099         if(state->run_index[comp] < 31)
00100             state->run_index[comp]++;
00101     }
00102     /* if hit EOL, encode another full run, else encode aborted run */
00103     if(!trail && run) {
00104         put_bits(pb, 1, 1);
00105     }else if(trail){
00106         put_bits(pb, 1, 0);
00107         if(ff_log2_run[state->run_index[comp]])
00108             put_bits(pb, ff_log2_run[state->run_index[comp]], run);
00109     }
00110 }
00111 
00115 static inline void ls_encode_line(JLSState *state, PutBitContext *pb, void *last, void *cur, int last2, int w, int stride, int comp, int bits){
00116     int x = 0;
00117     int Ra, Rb, Rc, Rd;
00118     int D0, D1, D2;
00119 
00120     while(x < w) {
00121         int err, pred, sign;
00122 
00123         /* compute gradients */
00124         Ra = x ? R(cur, x - stride) : R(last, x);
00125         Rb = R(last, x);
00126         Rc = x ? R(last, x - stride) : last2;
00127         Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
00128         D0 = Rd - Rb;
00129         D1 = Rb - Rc;
00130         D2 = Rc - Ra;
00131 
00132         /* run mode */
00133         if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
00134             int RUNval, RItype, run;
00135 
00136             run = 0;
00137             RUNval = Ra;
00138             while(x < w && (FFABS(R(cur, x) - RUNval) <= state->near)){
00139                 run++;
00140                 W(cur, x, Ra);
00141                 x += stride;
00142             }
00143             ls_encode_run(state, pb, run, comp, x < w);
00144             if(x >= w)
00145                 return;
00146             Rb = R(last, x);
00147             RItype = (FFABS(Ra - Rb) <= state->near);
00148             pred = RItype ? Ra : Rb;
00149             err = R(cur, x) - pred;
00150 
00151             if(!RItype && Ra > Rb)
00152                 err = -err;
00153 
00154             if(state->near){
00155                 if(err > 0)
00156                     err = (state->near + err) / state->twonear;
00157                 else
00158                     err = -(state->near - err) / state->twonear;
00159 
00160                 if(RItype || (Rb >= Ra))
00161                     Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
00162                 else
00163                     Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
00164                 W(cur, x, Ra);
00165             }
00166             if(err < 0)
00167                 err += state->range;
00168             if(err >= ((state->range + 1) >> 1))
00169                 err -= state->range;
00170 
00171             ls_encode_runterm(state, pb, RItype, err, ff_log2_run[state->run_index[comp]]);
00172 
00173             if(state->run_index[comp] > 0)
00174                 state->run_index[comp]--;
00175         } else { /* regular mode */
00176             int context;
00177 
00178             context = ff_jpegls_quantize(state, D0) * 81 + ff_jpegls_quantize(state, D1) * 9 + ff_jpegls_quantize(state, D2);
00179             pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
00180 
00181             if(context < 0){
00182                 context = -context;
00183                 sign = 1;
00184                 pred = av_clip(pred - state->C[context], 0, state->maxval);
00185                 err = pred - R(cur, x);
00186             }else{
00187                 sign = 0;
00188                 pred = av_clip(pred + state->C[context], 0, state->maxval);
00189                 err = R(cur, x) - pred;
00190             }
00191 
00192             if(state->near){
00193                 if(err > 0)
00194                     err = (state->near + err) / state->twonear;
00195                 else
00196                     err = -(state->near - err) / state->twonear;
00197                 if(!sign)
00198                     Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
00199                 else
00200                     Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
00201                 W(cur, x, Ra);
00202             }
00203 
00204             ls_encode_regular(state, pb, context, err);
00205         }
00206         x += stride;
00207     }
00208 }
00209 
00210 static void ls_store_lse(JLSState *state, PutBitContext *pb){
00211     /* Test if we have default params and don't need to store LSE */
00212     JLSState state2;
00213     memset(&state2, 0, sizeof(JLSState));
00214     state2.bpp = state->bpp;
00215     state2.near = state->near;
00216     ff_jpegls_reset_coding_parameters(&state2, 1);
00217     if(state->T1 == state2.T1 && state->T2 == state2.T2 && state->T3 == state2.T3 && state->reset == state2.reset)
00218         return;
00219     /* store LSE type 1 */
00220     put_marker(pb, LSE);
00221     put_bits(pb, 16, 13);
00222     put_bits(pb, 8,   1);
00223     put_bits(pb, 16, state->maxval);
00224     put_bits(pb, 16, state->T1);
00225     put_bits(pb, 16, state->T2);
00226     put_bits(pb, 16, state->T3);
00227     put_bits(pb, 16, state->reset);
00228 }
00229 
00230 static int encode_picture_ls(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
00231     JpeglsContext * const s = avctx->priv_data;
00232     AVFrame *pict = data;
00233     AVFrame * const p= (AVFrame*)&s->picture;
00234     const int near = avctx->prediction_method;
00235     PutBitContext pb, pb2;
00236     GetBitContext gb;
00237     uint8_t *buf2, *zero, *cur, *last;
00238     JLSState *state;
00239     int i, size;
00240     int comps;
00241 
00242     buf2 = av_malloc(buf_size);
00243 
00244     init_put_bits(&pb, buf, buf_size);
00245     init_put_bits(&pb2, buf2, buf_size);
00246 
00247     *p = *pict;
00248     p->pict_type= FF_I_TYPE;
00249     p->key_frame= 1;
00250 
00251     if(avctx->pix_fmt == PIX_FMT_GRAY8 || avctx->pix_fmt == PIX_FMT_GRAY16)
00252         comps = 1;
00253     else
00254         comps = 3;
00255 
00256     /* write our own JPEG header, can't use mjpeg_picture_header */
00257     put_marker(&pb, SOI);
00258     put_marker(&pb, SOF48);
00259     put_bits(&pb, 16, 8 + comps * 3); // header size depends on components
00260     put_bits(&pb,  8, (avctx->pix_fmt == PIX_FMT_GRAY16) ? 16 : 8); // bpp
00261     put_bits(&pb, 16, avctx->height);
00262     put_bits(&pb, 16, avctx->width);
00263     put_bits(&pb,  8, comps);         // components
00264     for(i = 1; i <= comps; i++) {
00265         put_bits(&pb,  8, i);    // component ID
00266         put_bits(&pb,  8, 0x11); // subsampling: none
00267         put_bits(&pb,  8, 0);    // Tiq, used by JPEG-LS ext
00268     }
00269 
00270     put_marker(&pb, SOS);
00271     put_bits(&pb, 16, 6 + comps * 2);
00272     put_bits(&pb,  8, comps);
00273     for(i = 1; i <= comps; i++) {
00274         put_bits(&pb,  8, i);  // component ID
00275         put_bits(&pb,  8, 0);  // mapping index: none
00276     }
00277     put_bits(&pb,  8, near);
00278     put_bits(&pb,  8, (comps > 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line
00279     put_bits(&pb,  8, 0); // point transform: none
00280 
00281     state = av_mallocz(sizeof(JLSState));
00282     /* initialize JPEG-LS state from JPEG parameters */
00283     state->near = near;
00284     state->bpp = (avctx->pix_fmt == PIX_FMT_GRAY16) ? 16 : 8;
00285     ff_jpegls_reset_coding_parameters(state, 0);
00286     ff_jpegls_init_state(state);
00287 
00288     ls_store_lse(state, &pb);
00289 
00290     zero = av_mallocz(p->linesize[0]);
00291     last = zero;
00292     cur = p->data[0];
00293     if(avctx->pix_fmt == PIX_FMT_GRAY8){
00294         int t = 0;
00295 
00296         for(i = 0; i < avctx->height; i++) {
00297             ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0,  8);
00298             t = last[0];
00299             last = cur;
00300             cur += p->linesize[0];
00301         }
00302     }else if(avctx->pix_fmt == PIX_FMT_GRAY16){
00303         int t = 0;
00304 
00305         for(i = 0; i < avctx->height; i++) {
00306             ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 16);
00307             t = *((uint16_t*)last);
00308             last = cur;
00309             cur += p->linesize[0];
00310         }
00311     }else if(avctx->pix_fmt == PIX_FMT_RGB24){
00312         int j, width;
00313         int Rc[3] = {0, 0, 0};
00314 
00315         width = avctx->width * 3;
00316         for(i = 0; i < avctx->height; i++) {
00317             for(j = 0; j < 3; j++) {
00318                 ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j, 8);
00319                 Rc[j] = last[j];
00320             }
00321             last = cur;
00322             cur += s->picture.linesize[0];
00323         }
00324     }else if(avctx->pix_fmt == PIX_FMT_BGR24){
00325         int j, width;
00326         int Rc[3] = {0, 0, 0};
00327 
00328         width = avctx->width * 3;
00329         for(i = 0; i < avctx->height; i++) {
00330             for(j = 2; j >= 0; j--) {
00331                 ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j, 8);
00332                 Rc[j] = last[j];
00333             }
00334             last = cur;
00335             cur += s->picture.linesize[0];
00336         }
00337     }
00338 
00339     av_free(zero);
00340     av_free(state);
00341 
00342     // the specification says that after doing 0xff escaping unused bits in the
00343     // last byte must be set to 0, so just append 7 "optional" zero-bits to
00344     // avoid special-casing.
00345     put_bits(&pb2, 7, 0);
00346     size = put_bits_count(&pb2);
00347     flush_put_bits(&pb2);
00348     /* do escape coding */
00349     init_get_bits(&gb, buf2, size);
00350     size -= 7;
00351     while(get_bits_count(&gb) < size){
00352         int v;
00353         v = get_bits(&gb, 8);
00354         put_bits(&pb, 8, v);
00355         if(v == 0xFF){
00356             v = get_bits(&gb, 7);
00357             put_bits(&pb, 8, v);
00358         }
00359     }
00360     align_put_bits(&pb);
00361     av_free(buf2);
00362 
00363     /* End of image */
00364     put_marker(&pb, EOI);
00365     flush_put_bits(&pb);
00366 
00367     emms_c();
00368 
00369     return put_bits_count(&pb) >> 3;
00370 }
00371 
00372 static av_cold int encode_init_ls(AVCodecContext *ctx) {
00373     JpeglsContext *c = (JpeglsContext*)ctx->priv_data;
00374 
00375     c->avctx = ctx;
00376     ctx->coded_frame = &c->picture;
00377 
00378     if(ctx->pix_fmt != PIX_FMT_GRAY8 && ctx->pix_fmt != PIX_FMT_GRAY16 && ctx->pix_fmt != PIX_FMT_RGB24 && ctx->pix_fmt != PIX_FMT_BGR24){
00379         av_log(ctx, AV_LOG_ERROR, "Only grayscale and RGB24/BGR24 images are supported\n");
00380         return -1;
00381     }
00382     return 0;
00383 }
00384 
00385 AVCodec jpegls_encoder = { //FIXME avoid MPV_* lossless JPEG should not need them
00386     "jpegls",
00387     AVMEDIA_TYPE_VIDEO,
00388     CODEC_ID_JPEGLS,
00389     sizeof(JpeglsContext),
00390     encode_init_ls,
00391     encode_picture_ls,
00392     NULL,
00393     .pix_fmts= (const enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB24, PIX_FMT_GRAY8, PIX_FMT_GRAY16, PIX_FMT_NONE},
00394     .long_name= NULL_IF_CONFIG_SMALL("JPEG-LS"),
00395 };

Generated on Fri Sep 16 2011 17:17:38 for FFmpeg by  doxygen 1.7.1