⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 h263.c

📁 Trolltech公司发布的图形界面操作系统。可在qt-embedded-2.3.10平台上编译为嵌入式图形界面操作系统。
💻 C
📖 第 1 页 / 共 5 页
字号:
/* * H263/MPEG4 backend for ffmpeg encoder and decoder * Copyright (c) 2000,2001 Fabrice Bellard. * H263+ support. * Copyright (c) 2001 Juan J. Sierralta P. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA * * ac prediction encoding, b-frame support, error resilience, optimizations, * qpel decoding, gmc decoding, interlaced decoding,  * by Michael Niedermayer <michaelni@gmx.at> *//** * @file h263.c * h263/mpeg4 codec. */ //#define DEBUG#include <limits.h>#include "common.h"#include "dsputil.h"#include "avcodec.h"#include "mpegvideo.h"#include "h263data.h"#include "mpeg4data.h"//#undef NDEBUG//#include <assert.h>#define INTRA_MCBPC_VLC_BITS 6#define INTER_MCBPC_VLC_BITS 7#define CBPY_VLC_BITS 6#define MV_VLC_BITS 9#define DC_VLC_BITS 9#define SPRITE_TRAJ_VLC_BITS 6#define MB_TYPE_B_VLC_BITS 4#define TEX_VLC_BITS 9#ifdef CONFIG_ENCODERSstatic void h263_encode_block(MpegEncContext * s, DCTELEM * block,                              int n);static void h263_encode_motion(MpegEncContext * s, int val, int fcode);static void h263p_encode_umotion(MpegEncContext * s, int val);static inline void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block,                               int n, int dc, uint8_t *scan_table,                                PutBitContext *dc_pb, PutBitContext *ac_pb);#endifstatic int h263_decode_motion(MpegEncContext * s, int pred, int fcode);static int h263p_decode_umotion(MpegEncContext * s, int pred);static int h263_decode_block(MpegEncContext * s, DCTELEM * block,                             int n, int coded);static inline int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr);static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,                              int n, int coded, int intra, int rvlc);static int h263_pred_dc(MpegEncContext * s, int n, uint16_t **dc_val_ptr);#ifdef CONFIG_ENCODERSstatic void mpeg4_encode_visual_object_header(MpegEncContext * s);static void mpeg4_encode_vol_header(MpegEncContext * s, int vo_number, int vol_number);#endif //CONFIG_ENCODERSstatic void mpeg4_decode_sprite_trajectory(MpegEncContext * s);static inline int ff_mpeg4_pred_dc(MpegEncContext * s, int n, uint16_t **dc_val_ptr, int *dir_ptr);#ifdef CONFIG_ENCODERSstatic uint8_t uni_DCtab_lum_len[512];static uint8_t uni_DCtab_chrom_len[512];static uint16_t uni_DCtab_lum_bits[512];static uint16_t uni_DCtab_chrom_bits[512];static uint8_t (*mv_penalty)[MAX_MV*2+1]= NULL;static uint8_t fcode_tab[MAX_MV*2+1];static uint8_t umv_fcode_tab[MAX_MV*2+1];static uint32_t uni_mpeg4_intra_rl_bits[64*64*2*2];static uint8_t  uni_mpeg4_intra_rl_len [64*64*2*2];static uint32_t uni_mpeg4_inter_rl_bits[64*64*2*2];static uint8_t  uni_mpeg4_inter_rl_len [64*64*2*2];//#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128 + (run)*256 + (level))//#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run) + (level)*64)#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run)*128 + (level))/* mpeg4intermax level: 24/6max run: 53/63intramax level: 53/16max run: 29/41*/#endif#if 0 //3IV1 is quite rare and tis slows things down a tiny bit#define IS_3IV1 s->avctx->codec_tag == ff_get_fourcc("3IV1")#else #define IS_3IV1 0#endifint h263_get_picture_format(int width, int height){    int format;    if (width == 128 && height == 96)        format = 1;    else if (width == 176 && height == 144)        format = 2;    else if (width == 352 && height == 288)        format = 3;    else if (width == 704 && height == 576)        format = 4;    else if (width == 1408 && height == 1152)        format = 5;    else        format = 7;    return format;}#ifdef CONFIG_ENCODERSstatic void aspect_to_info(MpegEncContext * s, AVRational aspect){    int i;    if(aspect.num==0) aspect= (AVRational){1,1};    for(i=1; i<6; i++){        if(av_cmp_q(pixel_aspect[i], aspect) == 0){            s->aspect_ratio_info=i;            return;        }    }        s->aspect_ratio_info= FF_ASPECT_EXTENDED;}void ff_flv_encode_picture_header(MpegEncContext * s, int picture_number){      int format;      align_put_bits(&s->pb);      put_bits(&s->pb, 17, 1);      put_bits(&s->pb, 5, (s->h263_flv-1)); /* 0: h263 escape codes 1: 11-bit escape codes */      put_bits(&s->pb, 8, (((int64_t)s->picture_number * 30 * s->avctx->frame_rate_base) /                            s->avctx->frame_rate) & 0xff); /* TemporalReference */      if (s->width == 352 && s->height == 288)        format = 2;      else if (s->width == 176 && s->height == 144)        format = 3;      else if (s->width == 128 && s->height == 96)        format = 4;      else if (s->width == 320 && s->height == 240)        format = 5;      else if (s->width == 160 && s->height == 120)        format = 6;      else if (s->width <= 255 && s->height <= 255)        format = 0; /* use 1 byte width & height */      else        format = 1; /* use 2 bytes width & height */      put_bits(&s->pb, 3, format); /* PictureSize */      if (format == 0) {        put_bits(&s->pb, 8, s->width);        put_bits(&s->pb, 8, s->height);      } else if (format == 1) {        put_bits(&s->pb, 16, s->width);        put_bits(&s->pb, 16, s->height);      }      put_bits(&s->pb, 2, s->pict_type == P_TYPE); /* PictureType */      put_bits(&s->pb, 1, 1); /* DeblockingFlag: on */      put_bits(&s->pb, 5, s->qscale); /* Quantizer */      put_bits(&s->pb, 1, 0); /* ExtraInformation */      if(s->h263_aic){        s->y_dc_scale_table=           s->c_dc_scale_table= h263_aic_dc_scale_table;      }else{        s->y_dc_scale_table=          s->c_dc_scale_table= ff_mpeg1_dc_scale_table;      }}void h263_encode_picture_header(MpegEncContext * s, int picture_number){    int format;    align_put_bits(&s->pb);    /* Update the pointer to last GOB */    s->ptr_lastgob = pbBufPtr(&s->pb);    s->gob_number = 0;    put_bits(&s->pb, 22, 0x20); /* PSC */    put_bits(&s->pb, 8, (((int64_t)s->picture_number * 30 * s->avctx->frame_rate_base) /                          s->avctx->frame_rate) & 0xff);    put_bits(&s->pb, 1, 1);	/* marker */    put_bits(&s->pb, 1, 0);	/* h263 id */    put_bits(&s->pb, 1, 0);	/* split screen off */    put_bits(&s->pb, 1, 0);	/* camera  off */    put_bits(&s->pb, 1, 0);	/* freeze picture release off */        format = h263_get_picture_format(s->width, s->height);    if (!s->h263_plus) {        /* H.263v1 */        put_bits(&s->pb, 3, format);        put_bits(&s->pb, 1, (s->pict_type == P_TYPE));        /* By now UMV IS DISABLED ON H.263v1, since the restrictions        of H.263v1 UMV implies to check the predicted MV after        calculation of the current MB to see if we're on the limits */        put_bits(&s->pb, 1, 0);	/* unrestricted motion vector: off */        put_bits(&s->pb, 1, 0);	/* SAC: off */        put_bits(&s->pb, 1, 0);	/* advanced prediction mode: off */        put_bits(&s->pb, 1, 0);	/* not PB frame */        put_bits(&s->pb, 5, s->qscale);        put_bits(&s->pb, 1, 0);	/* Continuous Presence Multipoint mode: off */    } else {        /* H.263v2 */        /* H.263 Plus PTYPE */        put_bits(&s->pb, 3, 7);        put_bits(&s->pb,3,1); /* Update Full Extended PTYPE */        if (format == 7)            put_bits(&s->pb,3,6); /* Custom Source Format */        else            put_bits(&s->pb, 3, format);                    put_bits(&s->pb,1,0); /* Custom PCF: off */        s->umvplus = s->unrestricted_mv;        put_bits(&s->pb, 1, s->umvplus); /* Unrestricted Motion Vector */        put_bits(&s->pb,1,0); /* SAC: off */        put_bits(&s->pb,1,0); /* Advanced Prediction Mode: off */        put_bits(&s->pb,1,s->h263_aic); /* Advanced Intra Coding */        put_bits(&s->pb,1,0); /* Deblocking Filter: off */        put_bits(&s->pb,1,0); /* Slice Structured: off */        put_bits(&s->pb,1,0); /* Reference Picture Selection: off */        put_bits(&s->pb,1,0); /* Independent Segment Decoding: off */        put_bits(&s->pb,1,0); /* Alternative Inter VLC: off */        put_bits(&s->pb,1,0); /* Modified Quantization: off */        put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */        put_bits(&s->pb,3,0); /* Reserved */		        put_bits(&s->pb, 3, s->pict_type == P_TYPE);		        put_bits(&s->pb,1,0); /* Reference Picture Resampling: off */        put_bits(&s->pb,1,0); /* Reduced-Resolution Update: off */        put_bits(&s->pb,1,s->no_rounding); /* Rounding Type */        put_bits(&s->pb,2,0); /* Reserved */        put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */		        /* This should be here if PLUSPTYPE */        put_bits(&s->pb, 1, 0);	/* Continuous Presence Multipoint mode: off */				if (format == 7) {            /* Custom Picture Format (CPFMT) */            aspect_to_info(s, s->avctx->sample_aspect_ratio);            put_bits(&s->pb,4,s->aspect_ratio_info);            put_bits(&s->pb,9,(s->width >> 2) - 1);            put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */            put_bits(&s->pb,9,(s->height >> 2));            if (s->aspect_ratio_info == FF_ASPECT_EXTENDED){                put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num);                put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den);	    }        }                /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */        if (s->umvplus)//            put_bits(&s->pb,1,1); /* Limited according tables of Annex D */            put_bits(&s->pb,2,1); /* unlimited */        put_bits(&s->pb, 5, s->qscale);    }    put_bits(&s->pb, 1, 0);	/* no PEI */    if(s->h263_aic){         s->y_dc_scale_table=          s->c_dc_scale_table= h263_aic_dc_scale_table;    }else{        s->y_dc_scale_table=        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;    }}/** * Encodes a group of blocks header. */int h263_encode_gob_header(MpegEncContext * s, int mb_line){           align_put_bits(&s->pb);           flush_put_bits(&s->pb);           /* Call the RTP callback to send the last GOB */           if (s->rtp_callback) {               int pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;               s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);           }           put_bits(&s->pb, 17, 1); /* GBSC */           s->gob_number = mb_line / s->gob_index;           put_bits(&s->pb, 5, s->gob_number); /* GN */           put_bits(&s->pb, 2, s->pict_type == I_TYPE); /* GFID */           put_bits(&s->pb, 5, s->qscale); /* GQUANT */           //fprintf(stderr,"\nGOB: %2d size: %d", s->gob_number - 1, pdif);    return 0;}static inline int get_block_rate(MpegEncContext * s, DCTELEM block[64], int block_last_index, uint8_t scantable[64]){    int last=0;    int j;    int rate=0;    for(j=1; j<=block_last_index; j++){        const int index= scantable[j];        int level= block[index];        if(level){            level+= 64;            if((level&(~127)) == 0){                if(j<block_last_index) rate+= s->intra_ac_vlc_length     [UNI_AC_ENC_INDEX(j-last-1, level)];                else                   rate+= s->intra_ac_vlc_last_length[UNI_AC_ENC_INDEX(j-last-1, level)];            }else                rate += s->ac_esc_length;            level-= 64;            last= j;        }    }        return rate;}static inline int decide_ac_pred(MpegEncContext * s, DCTELEM block[6][64], int dir[6], uint8_t *st[6], int zigzag_last_index[6]){    int score= 0;    int i, n;    int8_t * const qscale_table= s->current_picture.qscale_table;    memcpy(zigzag_last_index, s->block_last_index, sizeof(int)*6);        for(n=0; n<6; n++){        int16_t *ac_val, *ac_val1;                score -= get_block_rate(s, block[n], s->block_last_index[n], s->intra_scantable.permutated);        ac_val = s->ac_val[0][0] + s->block_index[n] * 16;        ac_val1= ac_val;        if(dir[n]){            const int xy= s->mb_x + s->mb_y*s->mb_stride - s->mb_stride;            /* top prediction */            ac_val-= s->block_wrap[n]*16;            if(s->mb_y==0 || s->qscale == qscale_table[xy] || n==2 || n==3){                /* same qscale */                for(i=1; i<8; i++){                    const int level= block[n][s->dsp.idct_permutation[i   ]];                    block[n][s->dsp.idct_permutation[i   ]] = level - ac_val[i+8];                    ac_val1[i  ]=    block[n][s->dsp.idct_permutation[i<<3]];                    ac_val1[i+8]= level;                }            }else{                /* different qscale, we must rescale */                for(i=1; i<8; i++){                    const int level= block[n][s->dsp.idct_permutation[i   ]];                    block[n][s->dsp.idct_permutation[i   ]] = level - ROUNDED_DIV(ac_val[i + 8]*qscale_table[xy], s->qscale);                    ac_val1[i  ]=    block[n][s->dsp.idct_permutation[i<<3]];                    ac_val1[i+8]= level;                }            }            st[n]= s->intra_h_scantable.permutated;        }else{            const int xy= s->mb_x-1 + s->mb_y*s->mb_stride;            /* left prediction */            ac_val-= 16;            if(s->mb_x==0 || s->qscale == qscale_table[xy] || n==1 || n==3){                /* same qscale */                for(i=1; i<8; i++){                    const int level= block[n][s->dsp.idct_permutation[i<<3]];                    block[n][s->dsp.idct_permutation[i<<3]]= level - ac_val[i];                    ac_val1[i  ]= level;                    ac_val1[i+8]=    block[n][s->dsp.idct_permutation[i   ]];                }            }else{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -