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

📄 h263.c

📁 tcpmp播放器的flv插件
💻 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. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at> * * 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#define H263_MBTYPE_B_VLC_BITS 6#define CBPC_B_VLC_BITS 3#ifdef CONFIG_ENCODERSstatic void h263_encode_block(MpegEncContext * s, DCTELEM * block,                              int n);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 mpeg4_get_block_length(MpegEncContext * s, DCTELEM * block, int n, int intra_dc,                                uint8_t *scan_table);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, GetBitContext *gb);static inline int ff_mpeg4_pred_dc(MpegEncContext * s, int n, int level, int *dir_ptr, int encoding);#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];static uint8_t  uni_h263_intra_aic_rl_len [64*64*2*2];static uint8_t  uni_h263_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 it 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); //Picard
    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->time_base.num) / //FIXME use timestamp                           s->avctx->time_base.den) & 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= ff_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, coded_frame_rate, coded_frame_rate_base, i, temp_ref;    int best_clock_code=1;    int best_divisor=60;    int best_error= INT_MAX;       if(s->h263_plus){        for(i=0; i<2; i++){            int div, error;            div= (s->avctx->time_base.num*1800000LL + 500LL*s->avctx->time_base.den) / ((1000LL+i)*s->avctx->time_base.den);            div= clip(1, div, 127);            error= ABS(s->avctx->time_base.num*1800000LL - (1000LL+i)*s->avctx->time_base.den*div);            if(error < best_error){                best_error= error;                best_divisor= div;                best_clock_code= i;            }        }    }    s->custom_pcf= best_clock_code!=1 || best_divisor!=60;    coded_frame_rate= 1800000;    coded_frame_rate_base= (1000+best_clock_code)*best_divisor;    align_put_bits(&s->pb);    /* Update the pointer to last GOB */    s->ptr_lastgob = pbBufPtr(&s->pb);    put_bits(&s->pb, 22, 0x20); /* PSC */    temp_ref= s->picture_number * (int64_t)coded_frame_rate * s->avctx->time_base.num / //FIXME use timestamp                         (coded_frame_rate_base * (int64_t)s->avctx->time_base.den);    put_bits(&s->pb, 8, temp_ref & 0xff); /* TemporalReference */    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, s->obmc);	/* Advanced Prediction */        put_bits(&s->pb, 1, 0);	/* only I/P frames, no PB frame */        put_bits(&s->pb, 5, s->qscale);        put_bits(&s->pb, 1, 0);	/* Continuous Presence Multipoint mode: off */    } else {        int ufep=1;        /* H.263v2 */        /* H.263 Plus PTYPE */                put_bits(&s->pb, 3, 7);        put_bits(&s->pb,3,ufep); /* 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, s->custom_pcf);        put_bits(&s->pb,1, s->umvplus); /* Unrestricted Motion Vector */        put_bits(&s->pb,1,0); /* SAC: off */        put_bits(&s->pb,1,s->obmc); /* Advanced Prediction Mode */        put_bits(&s->pb,1,s->h263_aic); /* Advanced Intra Coding */        put_bits(&s->pb,1,s->loop_filter); /* Deblocking Filter */        put_bits(&s->pb,1,s->h263_slice_structured); /* Slice Structured */        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,s->alt_inter_vlc); /* Alternative Inter VLC */        put_bits(&s->pb,1,s->modified_quant); /* Modified Quantization: */        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);	    }        }        if(s->custom_pcf){            if(ufep){                put_bits(&s->pb, 1, best_clock_code);                put_bits(&s->pb, 7, best_divisor);            }            put_bits(&s->pb, 2, (temp_ref>>8)&3);        }                /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */        if (s->umvplus)//            put_bits(&s->pb,1,1); /* Limited according tables of Annex D *///FIXME check actual requested range            put_bits(&s->pb,2,1); /* unlimited */        if(s->h263_slice_structured)            put_bits(&s->pb,2,0); /* no weird submodes */        put_bits(&s->pb, 5, s->qscale);    }    put_bits(&s->pb, 1, 0);	/* no PEI */    if(s->h263_slice_structured){        put_bits(&s->pb, 1, 1);                assert(s->mb_x == 0 && s->mb_y == 0);        ff_h263_encode_mba(s);        put_bits(&s->pb, 1, 1);    }    if(s->h263_aic){         s->y_dc_scale_table=          s->c_dc_scale_table= ff_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. */void h263_encode_gob_header(MpegEncContext * s, int mb_line){    put_bits(&s->pb, 17, 1); /* GBSC */    if(s->h263_slice_structured){        put_bits(&s->pb, 1, 1);        ff_h263_encode_mba(s);        if(s->mb_num > 1583)            put_bits(&s->pb, 1, 1);        put_bits(&s->pb, 5, s->qscale); /* GQUANT */        put_bits(&s->pb, 1, 1);        put_bits(&s->pb, 2, s->pict_type == I_TYPE); /* GFID */    }else{        int gob_number= mb_line / s->gob_index;        put_bits(&s->pb, 5, gob_number); /* GN */        put_bits(&s->pb, 2, s->pict_type == I_TYPE); /* GFID */        put_bits(&s->pb, 5, s->qscale); /* GQUANT */    }}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){

⌨️ 快捷键说明

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