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

libavcodec/zmbvenc.c

Go to the documentation of this file.
00001 /*
00002  * Zip Motion Blocks Video (ZMBV) encoder
00003  * Copyright (c) 2006 Konstantin Shishkov
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 
00030 #include "libavutil/intreadwrite.h"
00031 #include "avcodec.h"
00032 
00033 #include <zlib.h>
00034 
00035 #define ZMBV_KEYFRAME 1
00036 #define ZMBV_DELTAPAL 2
00037 
00038 #define ZMBV_BLOCK 16
00039 
00043 typedef struct ZmbvEncContext {
00044     AVCodecContext *avctx;
00045     AVFrame pic;
00046 
00047     int range;
00048     uint8_t *comp_buf, *work_buf;
00049     uint8_t pal[768];
00050     uint32_t pal2[256]; //for quick comparisons
00051     uint8_t *prev;
00052     int pstride;
00053     int comp_size;
00054     int keyint, curfrm;
00055     z_stream zstream;
00056 } ZmbvEncContext;
00057 
00058 static int score_tab[256];
00059 
00064 static inline int block_cmp(uint8_t *src, int stride, uint8_t *src2, int stride2, int bw, int bh)
00065 {
00066     int sum = 0;
00067     int i, j;
00068     uint8_t histogram[256]={0};
00069 
00070     for(j = 0; j < bh; j++){
00071         for(i = 0; i < bw; i++)
00072             histogram[src[i] ^ src2[i]]++;
00073         src += stride;
00074         src2 += stride2;
00075     }
00076 
00077     for(i=1; i<256; i++)
00078         sum+= score_tab[histogram[i]];
00079 
00080     return sum;
00081 }
00082 
00086 static int zmbv_me(ZmbvEncContext *c, uint8_t *src, int sstride, uint8_t *prev, int pstride,
00087                     int x, int y, int *mx, int *my)
00088 {
00089     int dx, dy, tx, ty, tv, bv, bw, bh;
00090 
00091     *mx = *my = 0;
00092     bw = FFMIN(ZMBV_BLOCK, c->avctx->width - x);
00093     bh = FFMIN(ZMBV_BLOCK, c->avctx->height - y);
00094     bv = block_cmp(src, sstride, prev, pstride, bw, bh);
00095     if(!bv) return 0;
00096     for(ty = FFMAX(y - c->range, 0); ty < FFMIN(y + c->range, c->avctx->height - bh); ty++){
00097         for(tx = FFMAX(x - c->range, 0); tx < FFMIN(x + c->range, c->avctx->width - bw); tx++){
00098             if(tx == x && ty == y) continue; // we already tested this block
00099             dx = tx - x;
00100             dy = ty - y;
00101             tv = block_cmp(src, sstride, prev + dx + dy*pstride, pstride, bw, bh);
00102             if(tv < bv){
00103                  bv = tv;
00104                  *mx = dx;
00105                  *my = dy;
00106                  if(!bv) return 0;
00107              }
00108          }
00109     }
00110     return bv;
00111 }
00112 
00113 static int encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void *data)
00114 {
00115     ZmbvEncContext * const c = avctx->priv_data;
00116     AVFrame *pict = data;
00117     AVFrame * const p = &c->pic;
00118     uint8_t *src, *prev;
00119     uint32_t *palptr;
00120     int zret = Z_OK;
00121     int len = 0;
00122     int keyframe, chpal;
00123     int fl;
00124     int work_size = 0;
00125     int bw, bh;
00126     int i, j;
00127 
00128     keyframe = !c->curfrm;
00129     c->curfrm++;
00130     if(c->curfrm == c->keyint)
00131         c->curfrm = 0;
00132     *p = *pict;
00133     p->pict_type= keyframe ? FF_I_TYPE : FF_P_TYPE;
00134     p->key_frame= keyframe;
00135     chpal = !keyframe && memcmp(p->data[1], c->pal2, 1024);
00136 
00137     fl = (keyframe ? ZMBV_KEYFRAME : 0) | (chpal ? ZMBV_DELTAPAL : 0);
00138     *buf++ = fl; len++;
00139     if(keyframe){
00140         deflateReset(&c->zstream);
00141         *buf++ = 0; len++; // hi ver
00142         *buf++ = 1; len++; // lo ver
00143         *buf++ = 1; len++; // comp
00144         *buf++ = 4; len++; // format - 8bpp
00145         *buf++ = ZMBV_BLOCK; len++; // block width
00146         *buf++ = ZMBV_BLOCK; len++; // block height
00147     }
00148     palptr = (uint32_t*)p->data[1];
00149     src = p->data[0];
00150     prev = c->prev;
00151     if(chpal){
00152         uint8_t tpal[3];
00153         for(i = 0; i < 256; i++){
00154             AV_WB24(tpal, palptr[i]);
00155             c->work_buf[work_size++] = tpal[0] ^ c->pal[i * 3 + 0];
00156             c->work_buf[work_size++] = tpal[1] ^ c->pal[i * 3 + 1];
00157             c->work_buf[work_size++] = tpal[2] ^ c->pal[i * 3 + 2];
00158             c->pal[i * 3 + 0] = tpal[0];
00159             c->pal[i * 3 + 1] = tpal[1];
00160             c->pal[i * 3 + 2] = tpal[2];
00161         }
00162         memcpy(c->pal2, p->data[1], 1024);
00163     }
00164     if(keyframe){
00165         for(i = 0; i < 256; i++){
00166             AV_WB24(c->pal+(i*3), palptr[i]);
00167         }
00168         memcpy(c->work_buf, c->pal, 768);
00169         memcpy(c->pal2, p->data[1], 1024);
00170         work_size = 768;
00171         for(i = 0; i < avctx->height; i++){
00172             memcpy(c->work_buf + work_size, src, avctx->width);
00173             src += p->linesize[0];
00174             work_size += avctx->width;
00175         }
00176     }else{
00177         int x, y, bh2, bw2;
00178         uint8_t *tsrc, *tprev;
00179         uint8_t *mv;
00180         int mx, my, bv;
00181 
00182         bw = (avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
00183         bh = (avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
00184         mv = c->work_buf + work_size;
00185         memset(c->work_buf + work_size, 0, (bw * bh * 2 + 3) & ~3);
00186         work_size += (bw * bh * 2 + 3) & ~3;
00187         /* for now just XOR'ing */
00188         for(y = 0; y < avctx->height; y += ZMBV_BLOCK) {
00189             bh2 = FFMIN(avctx->height - y, ZMBV_BLOCK);
00190             for(x = 0; x < avctx->width; x += ZMBV_BLOCK, mv += 2) {
00191                 bw2 = FFMIN(avctx->width - x, ZMBV_BLOCK);
00192 
00193                 tsrc = src + x;
00194                 tprev = prev + x;
00195 
00196                 bv = zmbv_me(c, tsrc, p->linesize[0], tprev, c->pstride, x, y, &mx, &my);
00197                 mv[0] = (mx << 1) | !!bv;
00198                 mv[1] = my << 1;
00199                 tprev += mx + my * c->pstride;
00200                 if(bv){
00201                     for(j = 0; j < bh2; j++){
00202                         for(i = 0; i < bw2; i++)
00203                             c->work_buf[work_size++] = tsrc[i] ^ tprev[i];
00204                         tsrc += p->linesize[0];
00205                         tprev += c->pstride;
00206                     }
00207                 }
00208             }
00209             src += p->linesize[0] * ZMBV_BLOCK;
00210             prev += c->pstride * ZMBV_BLOCK;
00211         }
00212     }
00213     /* save the previous frame */
00214     src = p->data[0];
00215     prev = c->prev;
00216     for(i = 0; i < avctx->height; i++){
00217         memcpy(prev, src, avctx->width);
00218         prev += c->pstride;
00219         src += p->linesize[0];
00220     }
00221 
00222     c->zstream.next_in = c->work_buf;
00223     c->zstream.avail_in = work_size;
00224     c->zstream.total_in = 0;
00225 
00226     c->zstream.next_out = c->comp_buf;
00227     c->zstream.avail_out = c->comp_size;
00228     c->zstream.total_out = 0;
00229     if((zret = deflate(&c->zstream, Z_SYNC_FLUSH)) != Z_OK){
00230         av_log(avctx, AV_LOG_ERROR, "Error compressing data\n");
00231         return -1;
00232     }
00233 
00234     memcpy(buf, c->comp_buf, c->zstream.total_out);
00235     return len + c->zstream.total_out;
00236 }
00237 
00238 
00242 static av_cold int encode_init(AVCodecContext *avctx)
00243 {
00244     ZmbvEncContext * const c = avctx->priv_data;
00245     int zret; // Zlib return code
00246     int i;
00247     int lvl = 9;
00248 
00249     for(i=1; i<256; i++)
00250         score_tab[i]= -i * log(i/(double)(ZMBV_BLOCK*ZMBV_BLOCK)) * (256/M_LN2);
00251 
00252     c->avctx = avctx;
00253 
00254     c->pic.data[0] = NULL;
00255     c->curfrm = 0;
00256     c->keyint = avctx->keyint_min;
00257     c->range = 8;
00258     if(avctx->me_range > 0)
00259         c->range = FFMIN(avctx->me_range, 127);
00260 
00261     if(avctx->compression_level >= 0)
00262         lvl = avctx->compression_level;
00263     if(lvl < 0 || lvl > 9){
00264         av_log(avctx, AV_LOG_ERROR, "Compression level should be 0-9, not %i\n", lvl);
00265         return -1;
00266     }
00267 
00268     if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
00269         return -1;
00270     }
00271 
00272     // Needed if zlib unused or init aborted before deflateInit
00273     memset(&(c->zstream), 0, sizeof(z_stream));
00274     c->comp_size = avctx->width * avctx->height + 1024 +
00275         ((avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * ((avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * 2 + 4;
00276     if ((c->work_buf = av_malloc(c->comp_size)) == NULL) {
00277         av_log(avctx, AV_LOG_ERROR, "Can't allocate work buffer.\n");
00278         return -1;
00279     }
00280     /* Conservative upper bound taken from zlib v1.2.1 source via lcl.c */
00281     c->comp_size = c->comp_size + ((c->comp_size + 7) >> 3) +
00282                            ((c->comp_size + 63) >> 6) + 11;
00283 
00284     /* Allocate compression buffer */
00285     if ((c->comp_buf = av_malloc(c->comp_size)) == NULL) {
00286         av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
00287         return -1;
00288     }
00289     c->pstride = (avctx->width + 15) & ~15;
00290     if ((c->prev = av_malloc(c->pstride * avctx->height)) == NULL) {
00291         av_log(avctx, AV_LOG_ERROR, "Can't allocate picture.\n");
00292         return -1;
00293     }
00294 
00295     c->zstream.zalloc = Z_NULL;
00296     c->zstream.zfree = Z_NULL;
00297     c->zstream.opaque = Z_NULL;
00298     zret = deflateInit(&(c->zstream), lvl);
00299     if (zret != Z_OK) {
00300         av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
00301         return -1;
00302     }
00303 
00304     avctx->coded_frame = (AVFrame*)&c->pic;
00305 
00306     return 0;
00307 }
00308 
00309 
00310 
00314 static av_cold int encode_end(AVCodecContext *avctx)
00315 {
00316     ZmbvEncContext * const c = avctx->priv_data;
00317 
00318     av_freep(&c->comp_buf);
00319     av_freep(&c->work_buf);
00320 
00321     deflateEnd(&(c->zstream));
00322     av_freep(&c->prev);
00323 
00324     return 0;
00325 }
00326 
00327 AVCodec zmbv_encoder = {
00328     "zmbv",
00329     CODEC_TYPE_VIDEO,
00330     CODEC_ID_ZMBV,
00331     sizeof(ZmbvEncContext),
00332     encode_init,
00333     encode_frame,
00334     encode_end,
00335     .pix_fmts = (enum PixelFormat[]){PIX_FMT_PAL8, PIX_FMT_NONE},
00336     .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
00337 };

Generated on Sat Feb 16 2013 09:23:14 for ffmpeg by  doxygen 1.7.1