Main Page | Class List | File List | Class Members | File Members

vid_sim.h File Reference

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "vid_config.h"
#include "vid_macros.h"

Go to the source code of this file.

Classes

struct  motionvector
struct  point
struct  pict_image
struct  pict
struct  slice
struct  macroblock
struct  mb_structure
struct  bits_counted
struct  results

Defines

#define MB_SIZE   16
#define MBC   88
#define MBR   72
#define PREF_NULL_VEC   100
#define PREF_16_VEC   200
#define PREF_PBDELTA_NULL_VEC   50
#define PSC   1
#define PSC_LENGTH   17
#define ESCAPE   7167
#define PCT_INTER   1
#define PCT_INTRA   0
#define ON   1
#define OFF   0
#define SF_SQCIF   1
#define SF_QCIF   2
#define SF_CIF   3
#define SF_4CIF   4
#define SF_16CIF   5
#define MODE_INTER   0
#define MODE_INTER_Q   1
#define MODE_INTER4V   2
#define MODE_INTRA   3
#define MODE_INTRA_Q   4
#define PBMODE_NORMAL   0
#define PBMODE_MVDB   1
#define PBMODE_CBPB_MVDB   2
#define NO_VEC   999

Typedefs

typedef motionvector MotionVector
typedef point Point
typedef pict_image PictImage
typedef pict Pict
typedef slice Slice
typedef macroblock Macroblock
typedef mb_structure MB_Structure
typedef bits_counted Bits
typedef results Results

Functions

void Help ()
void AdvancedHelp ()
int NextTwoPB (PictImage *p2, PictImage *bim, PictImage *p1, int bskip, int pskip, int seek_dist)
void PrintSNR (Results *res, int num)
void PrintResult (Bits *bits, int num_units, int num)
unsigned char * ReadImage (char *filename, int frame_no, int headerlength)
PictImageFillImage (unsigned char *in)
void WriteImage (PictImage *image, char *filename)
PictImageInitImage (int size)
void FreeImage (PictImage *image)
char * StripName (char *s)
int * MB_Encode (MB_Structure *mb_orig, int QP, int I)
int MB_Decode (int *qcoeff, MB_Structure *mb_recon, int QP, int I)
int Dct (int *block, int *coeff)
int idct (int *coeff, int *block)
void FillLumBlock (int x, int y, PictImage *image, MB_Structure *data)
void FillChromBlock (int x_curr, int y_curr, PictImage *image, MB_Structure *data)
void ReconImage (int i, int j, MB_Structure *data, PictImage *recon)
void CodeOneOrTwo (PictImage *curr, PictImage *B_image, PictImage *prev, PictImage *prev_recon, int QP, int frameskip, Bits *bits, Pict *pic, PictImage *B_recon, PictImage *recon)
PictImageCodeOneIntra (PictImage *curr, int QP, Bits *bits, Pict *pic)
void Dequant (int *qcoeff, int *rcoeff, int QP, int I)
void Quant (int *coeff, int *qcoeff, int QP, int I)
void CountBitsCoeff (int *qcoeff, int I, int CBP, Bits *bits, int ncoeffs)
int CodeCoeff (int Mode, int *qcoeff, int block, int ncoeffs)
int FindCBP (int *qcoeff, int Mode, int ncoeffs)
void CountBitsVectors (MotionVector *MV[5][MBR+1][MBC+2], Bits *bits, int i, int j, int Mode, int newgob, Pict *pic)
void FindPMV (MotionVector *MV[5][MBR+1][MBC+2], int x, int y, int *p0, int *p1, int block, int newgob, int half_pel)
void ZeroBits (Bits *bits)
void ZeroRes (Results *res)
void ZeroVec (MotionVector *MV)
void MarkVec (MotionVector *MV)
void CopyVec (MotionVector *MV1, MotionVector *MV2)
int EqualVec (MotionVector *MV2, MotionVector *MV1)
void AddBits (Bits *total, Bits *bits)
void AddRes (Results *total, Results *res, Pict *pic)
void AddBitsPicture (Bits *bits)
void FindMB (int x, int y, unsigned char *image, int MB[16][16])
MB_StructureMB_Recon (PictImage *prev_recon, MB_Structure *diff, int x_curr, int y_curr, MotionVector *MV)
MB_StructurePredict (PictImage *curr, PictImage *prev_recon, int x_curr, int y_curr, MotionVector *MV)
unsigned char * InterpolateImage (unsigned char *image, int w, int h)
void MotionEstimatePicture (unsigned char *curr, unsigned char *prev, unsigned char *prev_ipol, int seek_dist, MotionVector *MV[5][MBR+1][MBC+2], int gobsync)
void MotionEstimation (unsigned char *curr, unsigned char *prev, int x_curr, int y_curr, int xoff, int yoff, int seek_dist, MotionVector *MV[5][MBR+1][MBC+2], int *sad_0)
unsigned char * LoadArea (unsigned char *im, int x, int y, int x_size, int y_size, int lx)
int SAD_Macroblock (unsigned char *ii, unsigned char *act_block, int h_length, int Min_FRAME)
int SAD_MB_Bidir (unsigned char *ii, unsigned char *aa, unsigned char *bb, int width, int min_sofar)
int SAD_Block (unsigned char *ii, unsigned char *act_block, int h_length, int min_sofar)
int SAD_MB_integer (int *ii, int *act_block, int h_length, int min_sofar)
MB_StructurePredict_P (PictImage *curr_image, PictImage *prev_image, unsigned char *prev_ipol, int x_curr, int y_curr, MotionVector *fr[5][MBR+1][MBC+2], int PB)
MB_StructurePredict_B (PictImage *curr_image, PictImage *prev_image, unsigned char *prev_ipol, int x_curr, int y_curr, MotionVector *fr[5][MBR+1][MBC+2], MB_Structure *recon_P, int TR, int TRB)
void Clip (MB_Structure *data)
void FindForwLumPredPB (unsigned char *prev_ipol, int x_curr, int y_curr, MotionVector *fr, int *pred, int TRD, int TRB, int bdx, int bdy, int bs, int comp)
void FindBiDirLumPredPB (int *recon_P, MotionVector *fr, int *pred, int TRD, int TRB, int bdx, int bdy, int nh, int nv)
void FindBiDirChrPredPB (MB_Structure *recon_P, int dx, int dy, MB_Structure *pred)
void FindBiDirLimits (int vec, int *start, int *stop, int nhv)
void FindBiDirChromaLimits (int vec, int *start, int *stop)
void BiDirPredBlock (int xstart, int xstop, int ystart, int ystop, int xvec, int yvec, int *recon, int *pred, int bl)
void DoPredChrom_P (int x_curr, int y_curr, int dx, int dy, PictImage *curr, PictImage *prev, MB_Structure *pred_error)
void FindHalfPel (int x, int y, MotionVector *MV, unsigned char *prev, int *curr, int bs, int comp)
void FindPred (int x, int y, MotionVector *fr, unsigned char *prev, int *pred, int bs, int comp)
void FindPredOBMC (int x, int y, MotionVector *MV[5][MBR+1][MBC+2], unsigned char *prev, int *pred, int comp, int PB)
MB_StructureMB_Recon_P (PictImage *prev_image, unsigned char *prev_ipol, MB_Structure *diff, int x_curr, int y_curr, MotionVector *MV[5][MBR+1][MBC+2], int PB)
MB_StructureMB_Recon_B (PictImage *prev, MB_Structure *diff, unsigned char *prev_ipol, int x_curr, int y_curr, MotionVector *MV[5][MBR+1][MBC+2], MB_Structure *recon_P, int TR, int TRB)
void ReconLumBlock_P (int x, int y, MotionVector *fr, unsigned char *prev, int *data, int bs, int comp)
void ReconChromBlock_P (int x_curr, int y_curr, int dx, int dy, PictImage *prev, MB_Structure *data)
void FindChromBlock_P (int x_curr, int y_curr, int dx, int dy, PictImage *prev, MB_Structure *data)
void ComputeSNR (PictImage *im1, PictImage *im2, Results *res, int write)
void ZeroMBlock (MB_Structure *data)
int CountBitsPicture (Pict *pic)
void CountBitsMB (int Mode, int COD, int CBP, int CBPB, Pict *pic, Bits *bits)
int CountBitsSlice (int slice, int quant)
int ChooseMode (unsigned char *curr, int x_pos, int y_pos, int min_SAD)
int ModifyMode (int Mode, int dquant)
int * InterleaveCoeff (int *qp, int *qb, int bs)
void MakeEdgeImage (unsigned char *src, unsigned char *dst, int width, int height, int edge)
void BitPrint (int length, int val, char *bit)
int UpdateQuantizer (int mb, float QP_mean, int pict_type, float bit_rate, int mb_width, int mb_height, int bitcount)
int InitializeQuantizer (int pict_type, float bit_rate, float target_frame_rate, float QP_mean)
void InitializeRateControl ()
void UpdateRateControl (int bits)
void init_idctref (void)
void idctref (int *coeff, int *block)
void Count_sac_BitsCoeff (int *qcoeff, int I, int CBP, Bits *bits, int ncoeffs)
int Code_sac_Coeff (int Mode, int *qcoeff, int block, int ncoeffs)
int CodeTCoef (int mod_index, int position, int intra)
void Count_sac_BitsVectors (MotionVector *MV[5][MBR+1][MBC+2], Bits *bits, int i, int j, int Mode, int newgob, Pict *pic)
void Count_sac_BitsMB (int Mode, int COD, int CBP, int CBPB, Pict *pic, Bits *bits)
int AR_Encode (int index, int cumul_freq[])
int indexfn (int value, int table[], int max)
int bit_opp_bits (int)
int bit_in_psc_layer (int)
int encoder_flush ()
void initbits ()
void putbits (int, int)
int alignbits ()
int bitcount ()


Define Documentation

#define ESCAPE   7167
 

Definition at line 139 of file vid_sim.h.

Referenced by Code_sac_Coeff().

#define MB_SIZE   16
 

Definition at line 77 of file vid_sim.h.

Referenced by ChooseMode(), CodeOneIntra(), CodeOneOrTwo(), FindPredOBMC(), MB_Recon_B(), MB_Recon_P(), MotionEstimatePicture(), MotionEstimation(), NextTwoPB(), Predict_B(), and Predict_P().

#define MBC   88
 

Definition at line 108 of file vid_sim.h.

Referenced by CodeOneOrTwo(), and NextTwoPB().

#define MBR   72
 

Definition at line 109 of file vid_sim.h.

Referenced by CodeOneOrTwo(), and NextTwoPB().

#define MODE_INTER   0
 

Definition at line 152 of file vid_sim.h.

Referenced by CodeOneOrTwo(), MB_Recon_P(), and Predict_P().

#define MODE_INTER4V   2
 

Definition at line 154 of file vid_sim.h.

Referenced by FindPredOBMC().

#define MODE_INTER_Q   1
 

Definition at line 153 of file vid_sim.h.

Referenced by CodeOneOrTwo().

#define MODE_INTRA   3
 

Definition at line 155 of file vid_sim.h.

Referenced by Code_sac_Coeff(), CodeCoeff(), CodeOneOrTwo(), Count_sac_BitsCoeff(), Count_sac_BitsMB(), CountBitsCoeff(), CountBitsMB(), Dequant(), FindCBP(), FindPredOBMC(), Predict_B(), and Quant().

#define MODE_INTRA_Q   4
 

Definition at line 156 of file vid_sim.h.

Referenced by FindPredOBMC().

#define NO_VEC   999
 

Definition at line 162 of file vid_sim.h.

#define OFF   0
 

Definition at line 144 of file vid_sim.h.

#define ON   1
 

Definition at line 143 of file vid_sim.h.

#define PBMODE_CBPB_MVDB   2
 

Definition at line 160 of file vid_sim.h.

Referenced by Count_sac_BitsMB(), and CountBitsMB().

#define PBMODE_MVDB   1
 

Definition at line 159 of file vid_sim.h.

Referenced by Count_sac_BitsMB(), Count_sac_BitsVectors(), CountBitsMB(), and CountBitsVectors().

#define PBMODE_NORMAL   0
 

Definition at line 158 of file vid_sim.h.

Referenced by Count_sac_BitsMB(), and CountBitsMB().

#define PCT_INTER   1
 

Definition at line 141 of file vid_sim.h.

Referenced by CodeOneOrTwo().

#define PCT_INTRA   0
 

Definition at line 142 of file vid_sim.h.

#define PREF_16_VEC   200
 

Definition at line 113 of file vid_sim.h.

#define PREF_NULL_VEC   100
 

Definition at line 112 of file vid_sim.h.

#define PREF_PBDELTA_NULL_VEC   50
 

Definition at line 114 of file vid_sim.h.

#define PSC   1
 

Definition at line 136 of file vid_sim.h.

Referenced by CountBitsPicture(), and CountBitsSlice().

#define PSC_LENGTH   17
 

Definition at line 137 of file vid_sim.h.

Referenced by CountBitsPicture(), and CountBitsSlice().

#define SF_16CIF   5
 

Definition at line 150 of file vid_sim.h.

#define SF_4CIF   4
 

Definition at line 149 of file vid_sim.h.

#define SF_CIF   3
 

Definition at line 148 of file vid_sim.h.

#define SF_QCIF   2
 

Definition at line 147 of file vid_sim.h.

#define SF_SQCIF   1
 

Definition at line 146 of file vid_sim.h.


Typedef Documentation

typedef struct bits_counted Bits
 

Referenced by AddBits(), AddBitsPicture(), code_video(), CodeOneIntra(), CodeOneOrTwo(), Count_sac_BitsCoeff(), Count_sac_BitsMB(), Count_sac_BitsVectors(), CountBitsCoeff(), CountBitsMB(), CountBitsVectors(), new_video_codec(), PrintResult(), and ZeroBits().

typedef struct macroblock Macroblock
 

typedef struct mb_structure MB_Structure
 

Referenced by Clip(), CodeOneIntra(), CodeOneOrTwo(), DoPredChrom_P(), FillChromBlock(), FillLumBlock(), FindBiDirChrPredPB(), FindChromBlock_P(), MB_Decode(), MB_Encode(), MB_Recon_B(), MB_Recon_P(), Predict_B(), Predict_P(), ReconChromBlock_P(), ReconImage(), and ZeroMBlock().

typedef struct motionvector MotionVector
 

Referenced by CodeOneOrTwo(), CopyVec(), Count_sac_BitsVectors(), CountBitsVectors(), EqualVec(), FindBiDirLumPredPB(), FindForwLumPredPB(), FindHalfPel(), FindPMV(), FindPred(), FindPredOBMC(), MarkVec(), MB_Recon_B(), MB_Recon_P(), MotionEstimatePicture(), MotionEstimation(), NextTwoPB(), Predict_B(), Predict_P(), ReconLumBlock_P(), and ZeroVec().

typedef struct pict Pict
 

Referenced by AddRes(), CodeOneIntra(), CodeOneOrTwo(), Count_sac_BitsMB(), Count_sac_BitsVectors(), CountBitsMB(), CountBitsPicture(), CountBitsVectors(), FillImage(), and new_video_codec().

typedef struct pict_image PictImage
 

Referenced by CodeOneIntra(), CodeOneOrTwo(), ComputeSNR(), DoPredChrom_P(), FillChromBlock(), FillImage(), FillLumBlock(), FindChromBlock_P(), FreeImage(), InitImage(), MB_Recon_B(), MB_Recon_P(), NextTwoPB(), Predict_B(), Predict_P(), ReconChromBlock_P(), ReconImage(), and WriteImage().

typedef struct point Point
 

Referenced by FindHalfPel().

typedef struct results Results
 

Referenced by AddRes(), ComputeSNR(), new_video_codec(), PrintSNR(), and ZeroRes().

typedef struct slice Slice
 


Function Documentation

void AddBits Bits total,
Bits bits
 

Definition at line 1069 of file vid_countbit.c.

References Bits, bits_counted::C, bits_counted::CBPB, bits_counted::CBPCM, bits_counted::CBPY, bits_counted::COD, bits_counted::DQUANT, bits_counted::header, bits_counted::MODB, bits_counted::no_inter, bits_counted::no_inter4v, bits_counted::no_intra, bits_counted::total, bits_counted::vec, and bits_counted::Y.

Referenced by code_video().

01070 {
01071   total->Y += bits->Y;
01072   total->C += bits->C;
01073   total->vec += bits->vec;
01074   total->CBPY += bits->CBPY;
01075   total->CBPCM += bits->CBPCM;
01076   total->MODB += bits->MODB;
01077   total->CBPB += bits->CBPB;
01078   total->COD += bits->COD;
01079   total->DQUANT += bits->DQUANT;
01080   total->header += bits->header;
01081   total->total += bits->total;
01082   total->no_inter += bits->no_inter;
01083   total->no_inter4v += bits->no_inter4v;
01084   total->no_intra += bits->no_intra;
01085   return;
01086 }

void AddBitsPicture Bits bits  ) 
 

Definition at line 1096 of file vid_countbit.c.

References Bits, bits_counted::C, bits_counted::CBPB, bits_counted::CBPCM, bits_counted::CBPY, bits_counted::COD, bits_counted::DQUANT, bits_counted::header, bits_counted::MODB, bits_counted::total, bits_counted::vec, and bits_counted::Y.

Referenced by code_video(), and CodeOneOrTwo().

01097 {
01098   bits->total = 
01099     bits->Y + 
01100     bits->C + 
01101     bits->vec +  
01102     bits->CBPY + 
01103     bits->CBPCM + 
01104     bits->MODB +
01105     bits->CBPB +
01106     bits->COD + 
01107     bits->DQUANT +
01108     bits->header ;
01109 } 

void AddRes Results total,
Results res,
Pict pic
 

Definition at line 1087 of file vid_countbit.c.

References Pict, pict::QP_mean, results::QP_mean, Results, results::SNR_Cb, results::SNR_Cr, and results::SNR_l.

Referenced by code_video().

01088 {
01089   total->SNR_l += res->SNR_l;
01090   total->SNR_Cr += res->SNR_Cr;
01091   total->SNR_Cb += res->SNR_Cb;
01092   total->QP_mean += pic->QP_mean;
01093   return;
01094 }

void AdvancedHelp  ) 
 

int alignbits  ) 
 

Definition at line 115 of file vid_putbits.c.

References video_codec::outcnt, putbits(), and VidSt.

Referenced by code_video().

00116 {
00117   int ret_value;
00118   
00119   if ((VidSt->outcnt)!=8) {
00120     ret_value = (VidSt->outcnt);        /* outcnt is reset in call to putbits () */
00121     putbits ((VidSt->outcnt), 0);
00122     return ret_value;
00123   }
00124   else
00125     return 0;
00126 }

int AR_Encode int  index,
int  cumul_freq[]
 

Definition at line 86 of file vid_sac.c.

References bit_opp_bits(), bitcount(), low, and q1.

Referenced by Code_sac_Coeff(), CodeTCoef(), Count_sac_BitsMB(), and Count_sac_BitsVectors().

00087 {
00088   int bitcount=0;
00089 
00090   if (index<0) 
00091     return -1; /* Escape Code */
00092 
00093   length = high - low + 1;
00094   high = low - 1 + (length * cumul_freq[index]) / cumul_freq[0];
00095   low += (length * cumul_freq[index+1]) / cumul_freq[0];
00096 
00097   for ( ; ; ) {
00098     if (high < q2) {
00099       bitcount+=bit_opp_bits(0);
00100     }
00101     else if (low >= q2) {
00102       bitcount+=bit_opp_bits(1);        
00103       low -= q2; 
00104       high -= q2;
00105     }
00106     else if (low >= q1 && high < q3) {
00107       opposite_bits += 1; 
00108       low -= q1; 
00109       high -= q1;
00110     }
00111     else break;
00112  
00113     low *= 2; 
00114     high = 2*high+1;
00115   }
00116   return bitcount;
00117 }

void BiDirPredBlock int  xstart,
int  xstop,
int  ystart,
int  ystop,
int  xvec,
int  yvec,
int *  recon,
int *  pred,
int  bl
 

Definition at line 607 of file vid_pred.c.

References mmax, and mmin.

Referenced by FindBiDirChrPredPB(), and FindBiDirLumPredPB().

00609 {
00610   int i,j,pel;
00611   int xint, yint;
00612   int xh, yh;
00613 
00614   xint = xvec>>1;
00615   xh = xvec - 2*xint;
00616   yint = yvec>>1;
00617   yh = yvec - 2*yint;
00618 
00619   if (!xh && !yh) {
00620     for (j = ystart; j <= ystop; j++) {
00621       for (i = xstart; i <= xstop; i++) {
00622         pel = *(recon +(j+yint)*bl + i+xint);
00623         *(pred + j*bl + i) = (mmin(255,mmax(0,pel)) + *(pred + j*bl + i))>>1;
00624       }
00625     }
00626   }
00627   else if (!xh && yh) {
00628     for (j = ystart; j <= ystop; j++) {
00629       for (i = xstart; i <= xstop; i++) {
00630         pel = (*(recon +(j+yint)*bl + i+xint)       + 
00631                *(recon +(j+yint+yh)*bl + i+xint) + 1)>>1;
00632         *(pred + j*bl + i) = (pel + *(pred + j*bl + i))>>1;
00633       }
00634     }
00635   }
00636   else if (xh && !yh) {
00637     for (j = ystart; j <= ystop; j++) {
00638       for (i = xstart; i <= xstop; i++) {
00639         pel = (*(recon +(j+yint)*bl + i+xint)       + 
00640                *(recon +(j+yint)*bl + i+xint+xh) + 1)>>1;
00641         *(pred + j*bl + i) = (pel + *(pred + j*bl + i))>>1;
00642       }
00643     }
00644   }
00645   else { /* xh && yh */
00646     for (j = ystart; j <= ystop; j++) {
00647       for (i = xstart; i <= xstop; i++) {
00648         pel = (*(recon +(j+yint)*bl + i+xint)       + 
00649                *(recon +(j+yint+yh)*bl + i+xint) + 
00650                *(recon +(j+yint)*bl + i+xint+xh) + 
00651                *(recon +(j+yint+yh)*bl + i+xint+xh)+2)>>2;
00652         *(pred + j*bl + i) = (pel + *(pred + j*bl + i))>>1;
00653       }
00654     }
00655   }
00656   return;
00657 }

int bit_in_psc_layer int   ) 
 

Definition at line 190 of file vid_sac.c.

References bitcount(), putbits(), video_codec::tf, video_codec::trace, and VidSt.

Referenced by bit_opp_bits().

00191 {
00192   void putbits (int, int);
00193   int bitcount = 0;
00194 
00195   if (zerorun > 13) {
00196     if ((VidSt->trace))
00197       fprintf((VidSt->tf), "PSC emulation ... Bit stuffed.\n");
00198     putbits (1, 1);
00199     bitcount++;
00200     zerorun = 0;
00201   }
00202 
00203   putbits (1, bit);
00204   bitcount++;
00205 
00206   if (bit)
00207     zerorun = 0;
00208   else
00209     zerorun++;
00210 
00211   return bitcount;
00212 }

int bit_opp_bits int   ) 
 

Definition at line 119 of file vid_sac.c.

References bit_in_psc_layer(), and bitcount().

Referenced by AR_Encode(), and encoder_flush().

00120 {                                   
00121   int bitcount=0;
00122 
00123   bitcount = bit_in_psc_layer(bit);
00124 
00125   while(opposite_bits > 0){
00126     bitcount += bit_in_psc_layer(!bit);
00127     opposite_bits--;
00128   }
00129   return bitcount;
00130 }

int bitcount  ) 
 

Definition at line 130 of file vid_putbits.c.

References video_codec::bytecnt, video_codec::outcnt, and VidSt.

Referenced by AR_Encode(), bit_in_psc_layer(), bit_opp_bits(), encoder_flush(), and UpdateQuantizer().

00131 {
00132   return 8*(VidSt->bytecnt) + (8-(VidSt->outcnt));
00133 }

void BitPrint int  length,
int  val,
char *  bit
 

Definition at line 137 of file vid_putbits.c.

Referenced by putbits().

00138 {
00139   int m;
00140   
00141   m = length;
00142   bit[0] = '"';
00143   while (m--) {
00144     bit[length-m] = (val & (1<<m)) ? '1' : '0';
00145   }
00146   bit[length+1] = '"';
00147   bit[length+2] = '\n';
00148   bit[length+3] = '\0';
00149   return;
00150 }

int ChooseMode unsigned char *  curr,
int  x_pos,
int  y_pos,
int  min_SAD
 

Definition at line 1509 of file vid_pred.c.

References MB_SIZE, video_codec::pels, and VidSt.

Referenced by MotionEstimatePicture().

01510 {
01511   int i,j;
01512   int MB_mean = 0, A = 0;
01513   int y_off;
01514 
01515   for (j = 0; j < MB_SIZE; j++) {
01516     y_off = (y_pos + j) * (VidSt->pels);
01517     for (i = 0; i < MB_SIZE; i++) {
01518       MB_mean += *(curr + x_pos + i + y_off);
01519     }
01520   }
01521   MB_mean /= (MB_SIZE*MB_SIZE);
01522   for (j = 0; j < MB_SIZE; j++) {
01523     y_off = (y_pos + j) * (VidSt->pels);
01524     for (i = 0; i < MB_SIZE; i++) {
01525       A += abs( *(curr + x_pos + i + y_off) - MB_mean );
01526     }
01527   }
01528 
01529   if (A < (min_SAD - 500)) 
01530     return MODE_INTRA;
01531   else
01532     return MODE_INTER;
01533 }

void Clip MB_Structure data  ) 
 

Definition at line 1076 of file vid_coder.c.

References mb_structure::Cb, mb_structure::Cr, data, mb_structure::lum, MB_Structure, mmax, and mmin.

Referenced by CodeOneIntra(), and CodeOneOrTwo().

01077 {
01078   int m,n;
01079 
01080   for (n = 0; n < 16; n++) {
01081     for (m = 0; m < 16; m++) {
01082       data->lum[n][m] = mmin(255,mmax(0,data->lum[n][m]));
01083     }
01084   }
01085   for (n = 0; n < 8; n++) {
01086     for (m = 0; m < 8; m++) {
01087       data->Cr[n][m] = mmin(255,mmax(0,data->Cr[n][m]));
01088       data->Cb[n][m] = mmin(255,mmax(0,data->Cb[n][m]));
01089     }
01090   }
01091 }

int Code_sac_Coeff int  Mode,
int *  qcoeff,
int  block,
int  ncoeffs
 

Definition at line 526 of file vid_countbit.c.

References AR_Encode(), CodeTCoef(), cumf_INTRADC, cumf_LAST, cumf_LAST_intra, cumf_LEVEL, cumf_LEVEL_intra, cumf_RUN, cumf_RUN_intra, cumf_SIGN, ESCAPE, indexfn(), intradctab, lasttab, leveltab, MODE_INTRA, position, runtab, signtab, tcoeftab, video_codec::tf, video_codec::trace, and VidSt.

Referenced by Count_sac_BitsCoeff().

00527 {
00528   int j, bits, mod_index, intra;
00529   int prev_run, run, prev_level, level, first, prev_position, position;
00530   int prev_ind, ind, prev_s, s, length;
00531  
00532   run = bits = 0;
00533   first = 1; position = 0; intra = 0;
00534  
00535   level = s = ind = 0;
00536   prev_run = prev_level = prev_ind = prev_s = prev_position = 0;
00537  
00538   intra = (Mode == MODE_INTRA || Mode == MODE_INTRA_Q);
00539  
00540   for (j = block*ncoeffs; j< (block + 1)*ncoeffs; j++) {
00541  
00542     if (!(j%ncoeffs) && intra) {
00543       if ((VidSt->trace)) {
00544         fprintf((VidSt->tf),"DC: ");
00545       }
00546       if (qcoeff[block*ncoeffs]!=128)
00547         mod_index = indexfn(qcoeff[block*ncoeffs],intradctab,254);
00548       else
00549         mod_index = indexfn(255,intradctab,254);
00550       bits += AR_Encode(mod_index, cumf_INTRADC);
00551     }
00552     else {
00553  
00554       s = 0;
00555       /* Increment run if coeff is zero */
00556       if ((level = qcoeff[j]) == 0) {
00557         run++;
00558       }
00559       else {
00560         /* code run & level and count bits */
00561         if (level < 0) {
00562           s = 1;
00563           level = -level;
00564         }
00565         ind = level | run<<4;
00566         ind = ind | 0<<12; /* Not last coeff */
00567         position++;
00568  
00569         if (!first) {
00570           mod_index = indexfn(prev_ind, tcoeftab, 103);
00571           if (prev_level < 13 && prev_run < 64)
00572             length = CodeTCoef(mod_index, prev_position, intra);
00573           else
00574             length = -1;
00575  
00576           if (length == -1) {  /* Escape coding */
00577             if ((VidSt->trace)) {
00578               fprintf((VidSt->tf),"Escape coding:\n");
00579             }
00580  
00581             if (prev_s == 1) {prev_level = (prev_level^0xff)+1;}
00582  
00583             mod_index = indexfn(ESCAPE, tcoeftab, 103);
00584             bits += CodeTCoef(mod_index, prev_position, intra);
00585  
00586             if (intra)
00587               bits += AR_Encode(indexfn(0, lasttab, 2), cumf_LAST_intra);
00588             else
00589               bits += AR_Encode(indexfn(0, lasttab, 2), cumf_LAST);
00590  
00591             if (intra)
00592               bits += AR_Encode(indexfn(prev_run, runtab, 64), cumf_RUN_intra);
00593             else
00594               bits += AR_Encode(indexfn(prev_run, runtab, 64), cumf_RUN);
00595  
00596             if (intra)
00597               bits += AR_Encode(indexfn(prev_level, leveltab, 254), 
00598         cumf_LEVEL_intra);
00599             else
00600               bits += AR_Encode(indexfn(prev_level, leveltab, 254), 
00601         cumf_LEVEL);
00602  
00603           }
00604           else {
00605             bits += AR_Encode(indexfn(prev_s, signtab, 2), cumf_SIGN);
00606             bits += length;
00607           }
00608         }
00609  
00610         prev_run = run; prev_s = s;
00611         prev_level = level; prev_ind = ind;
00612         prev_position = position;
00613  
00614         run = first = 0;
00615  
00616       }
00617     }
00618   }
00619  
00620   /* Encode Last Coefficient */
00621  
00622   if (!first) {
00623     if ((VidSt->trace)) {
00624       fprintf((VidSt->tf),"Last coeff: ");
00625     }
00626     prev_ind = prev_ind | 1<<12;   /* last coeff */
00627     mod_index = indexfn(prev_ind, tcoeftab, 103);
00628  
00629     if (prev_level  < 13 && prev_run < 64)
00630       length = CodeTCoef(mod_index, prev_position, intra);
00631     else
00632       length = -1;
00633  
00634     if (length == -1) {  /* Escape coding */
00635       if ((VidSt->trace)) {
00636         fprintf((VidSt->tf),"Escape coding:\n");
00637       }
00638 
00639       if (prev_s == 1) {prev_level = (prev_level^0xff)+1;}
00640  
00641       mod_index = indexfn(ESCAPE, tcoeftab, 103);
00642       bits += CodeTCoef(mod_index, prev_position, intra);
00643  
00644       if (intra)
00645         bits += AR_Encode(indexfn(1, lasttab, 2), cumf_LAST_intra);
00646       else
00647         bits += AR_Encode(indexfn(1, lasttab, 2), cumf_LAST);
00648  
00649       if (intra)
00650         bits += AR_Encode(indexfn(prev_run, runtab, 64), cumf_RUN_intra);
00651       else
00652         bits += AR_Encode(indexfn(prev_run, runtab, 64), cumf_RUN);
00653  
00654       if (intra)
00655         bits += AR_Encode(indexfn(prev_level, leveltab, 254), cumf_LEVEL_intra);
00656       else
00657         bits += AR_Encode(indexfn(prev_level, leveltab, 254), cumf_LEVEL);
00658     }
00659     else {
00660       bits += AR_Encode(indexfn(prev_s, signtab, 2), cumf_SIGN);
00661       bits += length;
00662     }
00663   } /* last coeff */
00664  
00665   return bits;
00666 }

int CodeCoeff int  Mode,
int *  qcoeff,
int  block,
int  ncoeffs
 

Definition at line 361 of file vid_countbit.c.

References MODE_INTRA, put_coeff(), putbits(), video_codec::tf, video_codec::trace, and VidSt.

Referenced by CountBitsCoeff().

00362 {
00363   int j, bits;
00364   int prev_run, run, prev_level, level, first;
00365   int prev_s, s, length;
00366 
00367   run = bits = 0;
00368   first = 1;
00369   prev_run = prev_level = level = s = prev_s = 0;
00370   
00371   if ((VidSt->trace)) {
00372     fprintf((VidSt->tf),"Coeffs block %d:\n",block);
00373   }
00374 
00375   for (j = block*ncoeffs; j< (block + 1)*ncoeffs; j++) {
00376     /* Do this block's DC-coefficient first */
00377     if (!(j%ncoeffs) && (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)) {
00378       /* DC coeff */
00379       if ((VidSt->trace)) {
00380         fprintf((VidSt->tf),"DC: ");
00381       }
00382       if (qcoeff[block*ncoeffs] != 128)
00383         putbits(8,qcoeff[block*ncoeffs]);
00384       else
00385         putbits(8,255);
00386       bits += 8;
00387     }
00388     else {
00389       /* AC coeff */
00390       s = 0;
00391       /* Increment run if coeff is zero */
00392       if ((level = qcoeff[j]) == 0) {
00393         run++;
00394       }
00395       else {
00396         /* code run & level and count bits */
00397         if (level < 0) {
00398           s = 1;
00399           level = -level;
00400         }
00401 
00402         if (!first) {
00403           /* Encode the previous coefficient */
00404           if (prev_level  < 13 && prev_run < 64)
00405             length = put_coeff (prev_run, prev_level, 0);  
00406           else
00407             length = 0;
00408           if (length == 0) {  /* Escape coding */
00409             if ((VidSt->trace)) {
00410               fprintf((VidSt->tf),"Escape code: ");
00411             }
00412             if (prev_s == 1) {prev_level = (prev_level^0xff)+1;}
00413             putbits(7,3);       /* Escape code */
00414             if ((VidSt->trace))
00415               fprintf((VidSt->tf),"last: ");
00416             putbits(1,0);
00417             if ((VidSt->trace))
00418               fprintf((VidSt->tf),"run: ");
00419             putbits(6,prev_run);
00420             if ((VidSt->trace))
00421               fprintf((VidSt->tf),"level: ");
00422             putbits(8,prev_level);
00423             bits += 22;
00424           }
00425           else {
00426             putbits(1,prev_s);
00427             bits += length + 1;
00428           }
00429         }
00430         prev_run = run; prev_s = s;
00431         prev_level = level; 
00432 
00433         run = first = 0;
00434       }
00435     }
00436   }
00437   /* Encode the last coeff */
00438   if (!first) {
00439     if ((VidSt->trace)) {
00440       fprintf((VidSt->tf),"Last coeff: ");
00441     }
00442     if (prev_level  < 13 && prev_run < 64) 
00443       length = put_coeff (prev_run, prev_level, 1);   
00444     else
00445       length = 0;
00446     if (length == 0) {  /* Escape coding */
00447       if ((VidSt->trace)) {
00448         fprintf((VidSt->tf),"Escape code: ");
00449       }
00450       if (prev_s == 1) {prev_level = (prev_level^0xff)+1;}
00451       putbits (7,3);    /* Escape code */
00452       if ((VidSt->trace))
00453         fprintf((VidSt->tf),"last: ");
00454       putbits(1,1);
00455       if ((VidSt->trace))
00456         fprintf((VidSt->tf),"run: ");
00457       putbits(6,prev_run);
00458       if ((VidSt->trace))
00459         fprintf((VidSt->tf),"level: ");
00460       putbits(8,prev_level);
00461       bits += 22;
00462     }
00463     else {
00464       putbits(1,prev_s);
00465       bits += length + 1;
00466     }
00467   }
00468   return bits;
00469 }

PictImage* CodeOneIntra PictImage curr,
int  QP,
Bits bits,
Pict pic
 

Definition at line 435 of file vid_coder.c.

References Bits, Clip(), Count_sac_BitsCoeff(), Count_sac_BitsMB(), CountBitsCoeff(), CountBitsMB(), CountBitsPicture(), CountBitsSlice(), data, FillChromBlock(), FillLumBlock(), FindCBP(), bits_counted::header, InitImage(), video_codec::lines, pict::MB, MB_Decode(), MB_Encode(), MB_SIZE, MB_Structure, bits_counted::no_intra, video_codec::pels, Pict, PictImage, pict::QP_mean, pict::QUANT, ReconImage(), video_codec::syntax_arith_coding, pict::use_gobsync, VidSt, and ZeroBits().

Referenced by code_video().

00436 {
00437   PictImage *recon;
00438   MB_Structure *data = (MB_Structure *)malloc(sizeof(MB_Structure));
00439   int *qcoeff;
00440   int Mode = MODE_INTRA;
00441   int CBP,COD;
00442   int i,j;
00443 
00444   recon = InitImage((VidSt->pels)*(VidSt->lines));
00445   ZeroBits(bits);
00446   
00447   pic->QUANT = QP;
00448   bits->header += CountBitsPicture(pic);
00449 
00450   COD = 0; /* Every block is coded in Intra frame */
00451   for ( j = 0; j < (VidSt->lines)/MB_SIZE; j++) {
00452 
00453     /* insert sync in *every* slice if use_gobsync is chosen */
00454     if (pic->use_gobsync && j != 0)
00455       bits->header += CountBitsSlice(j,QP);
00456     for ( i = 0; i < (VidSt->pels)/MB_SIZE; i++) {
00457 
00458       pic->MB = i + j * ((VidSt->pels)/MB_SIZE);
00459       bits->no_intra++;
00460       FillLumBlock(i*MB_SIZE, j*MB_SIZE, curr, data);
00461       FillChromBlock(i*MB_SIZE, j*MB_SIZE, curr, data);
00462       qcoeff = MB_Encode(data, QP, Mode);
00463       CBP = FindCBP(qcoeff,Mode,64);
00464 
00465       if (!(VidSt->syntax_arith_coding)) {
00466         CountBitsMB(Mode,COD,CBP,0,pic,bits);
00467         CountBitsCoeff(qcoeff, Mode, CBP,bits,64);
00468       } else {
00469         Count_sac_BitsMB(Mode,COD,CBP,0,pic,bits);
00470         Count_sac_BitsCoeff(qcoeff, Mode, CBP,bits,64);
00471       }
00472 
00473       MB_Decode(qcoeff, data, QP, Mode);
00474       Clip(data);
00475       ReconImage(i,j,data,recon);
00476       free(qcoeff);
00477     }
00478   }
00479   pic->QP_mean = (float)QP;
00480 
00481 
00482   free(data);
00483   return recon;
00484 }

void CodeOneOrTwo PictImage curr,
PictImage B_image,
PictImage prev,
PictImage prev_recon,
int  QP,
int  frameskip,
Bits bits,
Pict pic,
PictImage B_recon,
PictImage recon
 

Definition at line 73 of file vid_coder.c.

References AddBitsPicture(), pict::bit_rate, Bits, pict::BQUANT, pict_image::Cb, Clip(), Count_sac_BitsCoeff(), Count_sac_BitsMB(), Count_sac_BitsVectors(), CountBitsCoeff(), CountBitsMB(), CountBitsPicture(), CountBitsSlice(), CountBitsVectors(), pict_image::Cr, pict::DQUANT, EqualVec(), FillChromBlock(), FillLumBlock(), FindCBP(), FreeImage(), bits_counted::header, InitializeQuantizer(), InitImage(), InterpolateImage(), video_codec::lines, video_codec::long_vectors, pict_image::lum, MakeEdgeImage(), MarkVec(), pict::MB, MB_Decode(), MB_Encode(), MB_Recon_B(), MB_Recon_P(), MB_SIZE, MB_Structure, MBC, MBR, mmax, mmin, pict::MODB, motionvector::Mode, MODE_INTER, MODE_INTER_Q, MODE_INTRA, ModifyMode(), MotionEstimatePicture(), MotionVector, video_codec::mv_outside_frame, bits_counted::no_inter, bits_counted::no_inter4v, bits_counted::no_intra, pict::PB, PCT_INTER, video_codec::pels, Pict, PictImage, Predict_B(), Predict_P(), pict::QP_mean, pict::QUANT, ReconImage(), pict::seek_dist, video_codec::syntax_arith_coding, pict::target_frame_rate, bits_counted::total, pict::TRB, UpdateQuantizer(), pict::use_gobsync, VidSt, motionvector::x, motionvector::y, ZeroBits(), ZeroMBlock(), and ZeroVec().

Referenced by code_video().

00076 {
00077   unsigned char *prev_ipol,*pi_edge=NULL,*pi,*orig_lum;
00078   PictImage *prev_recon=NULL, *pr_edge=NULL;
00079   MotionVector *MV[6][MBR+1][MBC+2];
00080   MotionVector ZERO = {0,0,0,0,0};
00081   MB_Structure *recon_data_P; 
00082   MB_Structure *recon_data_B=NULL; 
00083   MB_Structure *diff; 
00084   int *qcoeff_P;
00085   int *qcoeff_B=NULL;
00086 
00087   int Mode,B;  
00088   int CBP, CBPB=0;
00089   int bquant[] = {5,6,7,8};
00090   int QP_B;
00091   int newgob;
00092 
00093   int i,j,k;
00094 
00095   /* buffer control vars */
00096   float QP_cumulative = (float)0.0;
00097   int abs_mb_num = 0, QuantChangePostponed = 0;
00098   int QP_new, QP_prev, dquant, QP_xmitted=QP;
00099 
00100   ZeroBits(bits);
00101 
00102   /* interpolate image */
00103   if ((VidSt->mv_outside_frame)) {
00104     if ((VidSt->long_vectors)) {
00105       /* If the Extended Motion Vector range is used, motion vectors
00106          may point further out of the picture than in the normal range,
00107          and the edge images will have to be made larger */
00108       B = 16;
00109     }
00110     else {
00111       /* normal range */
00112       B = 8;
00113     }
00114     pi_edge = (unsigned char *)malloc(sizeof(char)*((VidSt->pels)+4*B)*((VidSt->lines)+4*B));
00115     if (pi_edge == NULL) {
00116       fprintf(stderr,"Couldn't allocate memory for pi_edge\n");
00117       exit(-1);
00118     }
00119     MakeEdgeImage(pr->lum,pi_edge + ((VidSt->pels) + 4*B)*2*B+2*B,(VidSt->pels),(VidSt->lines),2*B);
00120     pi = InterpolateImage(pi_edge, (VidSt->pels)+4*B, (VidSt->lines)+4*B);
00121     free(pi_edge);
00122     prev_ipol = pi + (2*(VidSt->pels) + 8*B) * 4*B + 4*B; 
00123     
00124     /* luma of non_interpolated image */
00125     pr_edge = InitImage(((VidSt->pels)+4*B)*((VidSt->lines)+4*B));
00126     MakeEdgeImage(prev->lum, pr_edge->lum + ((VidSt->pels) + 4*B)*2*B+2*B,
00127           (VidSt->pels),(VidSt->lines),2*B);
00128     orig_lum = pr_edge->lum + ((VidSt->pels) + 4*B)*2*B+2*B;
00129     
00130     /* non-interpolated image */
00131     MakeEdgeImage(pr->lum,pr_edge->lum + ((VidSt->pels)+4*B)*2*B + 2*B,(VidSt->pels),(VidSt->lines),2*B);
00132     MakeEdgeImage(pr->Cr,pr_edge->Cr + ((VidSt->pels)/2 + 2*B)*B + B,(VidSt->pels)/2,(VidSt->lines)/2,B);
00133     MakeEdgeImage(pr->Cb,pr_edge->Cb + ((VidSt->pels)/2 + 2*B)*B + B,(VidSt->pels)/2,(VidSt->lines)/2,B);
00134     
00135     prev_recon = (PictImage *)malloc(sizeof(PictImage));
00136     prev_recon->lum = pr_edge->lum + ((VidSt->pels) + 4*B)*2*B + 2*B;
00137     prev_recon->Cr = pr_edge->Cr + ((VidSt->pels)/2 + 2*B)*B + B;
00138     prev_recon->Cb = pr_edge->Cb + ((VidSt->pels)/2 + 2*B)*B + B;
00139   }
00140   else {
00141     pi = InterpolateImage(pr->lum,(VidSt->pels),(VidSt->lines));
00142     prev_ipol = pi;
00143     prev_recon = pr;
00144     orig_lum = prev->lum;
00145   }
00146 
00147   /* mark PMV's outside the frame */
00148   for (i = 1; i < ((VidSt->pels)>>4)+1; i++) {
00149     for (k = 0; k < 6; k++) {
00150       MV[k][0][i] = (MotionVector *)malloc(sizeof(MotionVector));
00151       MarkVec(MV[k][0][i]);
00152     }
00153     MV[0][0][i]->Mode = MODE_INTRA;
00154   }
00155   /* zero out PMV's outside the frame */
00156   for (i = 0; i < ((VidSt->lines)>>4)+1; i++) {
00157     for (k = 0; k < 6; k++) {
00158       MV[k][i][0] = (MotionVector *)malloc(sizeof(MotionVector));
00159       ZeroVec(MV[k][i][0]);
00160       MV[k][i][((VidSt->pels)>>4)+1] = (MotionVector *)malloc(sizeof(MotionVector));
00161       ZeroVec(MV[k][i][((VidSt->pels)>>4)+1]);
00162     }
00163     MV[0][i][0]->Mode = MODE_INTRA;
00164     MV[0][i][((VidSt->pels)>>4)+1]->Mode = MODE_INTRA;
00165   }
00166 
00167   /* Integer and half pel motion estimation */
00168   MotionEstimatePicture(curr->lum,prev_recon->lum,prev_ipol,
00169         pic->seek_dist,MV, pic->use_gobsync);
00170   /* note: integer pel motion estimation is now based on previous
00171      reconstructed image, not the previous original image. We have
00172      found that this works better for some sequences and not worse for
00173      others.  Note that it can not easily be changed back by
00174      substituting prev_recon->lum with orig_lum in the line above,
00175      because SAD for zero vector is not re-calculated in the half
00176      pel search. The half pel search has always been based on the
00177      previous reconstructed image */
00178 
00179 
00180   if (pic->bit_rate != 0) {
00181     /* Initialization routine for Rate Control */
00182     QP_new = InitializeQuantizer(PCT_INTER, (float)pic->bit_rate, 
00183                (pic->PB ? pic->target_frame_rate/2 : pic->target_frame_rate),
00184                pic->QP_mean);
00185     QP_xmitted = QP_prev = QP_new; 
00186   }
00187   else {
00188     QP_new = QP_xmitted = QP_prev = QP; /* Copy the passed value of QP */
00189   }
00190 
00191   dquant = 0; 
00192 
00193   for ( j = 0; j < (VidSt->lines)/MB_SIZE; j++) {
00194 
00195 
00196     if (pic->bit_rate != 0) {
00197       /* QP updated at the beginning of each row */
00198       AddBitsPicture(bits);
00199 
00200       QP_new =  UpdateQuantizer(abs_mb_num, pic->QP_mean, PCT_INTER, 
00201                                 (float)pic->bit_rate, (VidSt->pels)/MB_SIZE, 
00202                                 (VidSt->lines)/MB_SIZE, bits->total);
00203     }
00204 
00205     newgob = 0;
00206 
00207     if (j == 0) {
00208       pic->QUANT = QP_new;
00209       bits->header += CountBitsPicture(pic);
00210       QP_xmitted = QP_prev = QP_new;
00211     }
00212     else if (pic->use_gobsync && j%pic->use_gobsync == 0) {
00213       bits->header += CountBitsSlice(j,QP_new); /* insert gob sync */
00214       QP_xmitted = QP_prev = QP_new;
00215       newgob = 1;
00216     }
00217 
00218     for ( i = 0; i < (VidSt->pels)/MB_SIZE; i++) {
00219 
00220       /* Update of dquant, check and correct its limit */
00221       dquant = QP_new - QP_prev;
00222       if (dquant != 0 && i != 0 && MV[0][j+1][i+1]->Mode == MODE_INTER4V) {
00223         /* It is not possible to change the quantizer and at the same
00224            time use 8x8 vectors. Turning off 8x8 vectors is not
00225            possible at this stage because the previous macroblock
00226            encoded assumed this one should use 8x8 vectors. Therefore
00227            the change of quantizer is postponed until the first MB
00228            without 8x8 vectors */
00229         dquant = 0;
00230         QP_xmitted = QP_prev;
00231         QuantChangePostponed = 1;
00232       }
00233       else {
00234         QP_xmitted = QP_new;
00235         QuantChangePostponed = 0;
00236       }
00237       if (dquant > 2)  { dquant =  2; QP_xmitted = QP_prev + dquant;}
00238       if (dquant < -2) { dquant = -2; QP_xmitted = QP_prev + dquant;}
00239 
00240       pic->DQUANT = dquant;
00241       /* modify mode if dquant != 0 (e.g. MODE_INTER -> MODE_INTER_Q) */
00242       Mode = ModifyMode(MV[0][j+1][i+1]->Mode,pic->DQUANT);
00243       MV[0][j+1][i+1]->Mode = Mode;
00244 
00245       pic->MB = i + j * ((VidSt->pels)/MB_SIZE);
00246 
00247       if (Mode == MODE_INTER || Mode == MODE_INTER_Q || Mode==MODE_INTER4V) {
00248         /* Predict P-MB */
00249         diff = Predict_P(curr,prev_recon,prev_ipol,
00250          i*MB_SIZE,j*MB_SIZE,MV,pic->PB);
00251 
00252       }
00253       else {
00254         diff = (MB_Structure *)malloc(sizeof(MB_Structure));
00255         FillLumBlock(i*MB_SIZE, j*MB_SIZE, curr, diff);
00256         FillChromBlock(i*MB_SIZE, j*MB_SIZE, curr, diff);
00257       }
00258 
00259       /* P or INTRA Macroblock */
00260       qcoeff_P = MB_Encode(diff, QP_xmitted, Mode);
00261       CBP = FindCBP(qcoeff_P, Mode, 64);
00262       if (CBP == 0 && (Mode == MODE_INTER || Mode == MODE_INTER_Q)) 
00263         ZeroMBlock(diff);
00264       else
00265         MB_Decode(qcoeff_P, diff, QP_xmitted, Mode);
00266       recon_data_P = MB_Recon_P(prev_recon, prev_ipol,diff, 
00267         i*MB_SIZE,j*MB_SIZE,MV,pic->PB);
00268       Clip(recon_data_P);
00269       free(diff);
00270         
00271 
00272       /* Predict B-MB using reconstructed P-MB and prev. recon. image */
00273       if (pic->PB) {
00274         diff = Predict_B(B_image, prev_recon, prev_ipol,i*MB_SIZE, j*MB_SIZE,
00275          MV, recon_data_P, frameskip, pic->TRB);
00276         if (QP_xmitted == 0)  
00277           QP_B = 0;  /* (QP = 0 means no quantization) */
00278         else 
00279           QP_B = mmax(1,mmin(31,bquant[pic->BQUANT]*QP_xmitted/4));
00280         qcoeff_B = MB_Encode(diff, QP_B, MODE_INTER);
00281         CBPB = FindCBP(qcoeff_B, MODE_INTER, 64);
00282         if (CBPB)
00283           MB_Decode(qcoeff_B, diff, QP_B, MODE_INTER);
00284         else
00285           ZeroMBlock(diff);
00286         recon_data_B = MB_Recon_B(prev_recon, diff,prev_ipol,i*MB_SIZE,
00287           j*MB_SIZE,MV,recon_data_P,frameskip,
00288           pic->TRB);
00289         Clip(recon_data_B);
00290 
00291         /* decide MODB */ 
00292 
00293         if (CBPB) {
00294           pic->MODB = PBMODE_CBPB_MVDB;
00295         }
00296         else {
00297           if (MV[5][j+1][i+1]->x == 0 && MV[5][j+1][i+1]->y == 0)
00298             pic->MODB = PBMODE_NORMAL;
00299           else
00300             pic->MODB = PBMODE_MVDB;
00301         }
00302 
00303         free(diff);
00304 
00305       }
00306       else
00307         ZeroVec(MV[5][j+1][i+1]); /* Zero out PB deltas */
00308 
00309       if ((CBP==0) && (CBPB==0) && (EqualVec(MV[0][j+1][i+1],&ZERO)) && 
00310           (EqualVec(MV[5][j+1][i+1],&ZERO)) &&
00311           (Mode == MODE_INTER || Mode == MODE_INTER_Q)) {
00312         /* Skipped MB : both CBP and CBPB are zero, 16x16 vector is zero,
00313            PB delta vector is zero and Mode = MODE_INTER */
00314         if (Mode == MODE_INTER_Q) {
00315           /* DQUANT != 0 but not coded anyway */
00316           QP_xmitted = QP_prev;
00317           pic->DQUANT = 0;
00318           Mode = MODE_INTER;
00319         }
00320         if (!(VidSt->syntax_arith_coding))
00321           CountBitsMB(Mode,1,CBP,CBPB,pic,bits);
00322         else
00323           Count_sac_BitsMB(Mode,1,CBP,CBPB,pic,bits);
00324       }
00325       else {
00326         /* Normal MB */
00327         if (!(VidSt->syntax_arith_coding)) { /* VLC */
00328           CountBitsMB(Mode,0,CBP,CBPB,pic,bits);
00329 
00330           if (Mode == MODE_INTER  || Mode == MODE_INTER_Q) {
00331             bits->no_inter++;
00332             CountBitsVectors(MV, bits, i, j, Mode, newgob, pic);
00333           }
00334           else if (Mode == MODE_INTER4V) {
00335             bits->no_inter4v++;
00336             CountBitsVectors(MV, bits, i, j, Mode, newgob, pic);
00337           }
00338           else {
00339             /* MODE_INTRA or MODE_INTRA_Q */
00340             bits->no_intra++;
00341             if (pic->PB)
00342               CountBitsVectors(MV, bits, i, j, Mode, newgob, pic);
00343           }
00344           
00345           if (CBP || Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
00346             CountBitsCoeff(qcoeff_P, Mode, CBP, bits, 64);
00347           if (CBPB)
00348             CountBitsCoeff(qcoeff_B, MODE_INTER, CBPB, bits, 64);
00349         } /* end VLC */
00350 
00351         else { /* SAC */
00352           Count_sac_BitsMB(Mode,0,CBP,CBPB,pic,bits);
00353  
00354           if (Mode == MODE_INTER  || Mode == MODE_INTER_Q) {
00355             bits->no_inter++;
00356             Count_sac_BitsVectors(MV, bits, i, j, Mode, newgob, pic);
00357           }
00358           else if (Mode == MODE_INTER4V) {
00359             bits->no_inter4v++;
00360             Count_sac_BitsVectors(MV, bits, i, j, Mode, newgob, pic);
00361           }
00362           else {
00363             /* MODE_INTRA or MODE_INTRA_Q */
00364             bits->no_intra++;
00365             if (pic->PB)
00366               Count_sac_BitsVectors(MV, bits, i, j, Mode, newgob, pic);
00367           }
00368           
00369           if (CBP || Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
00370             Count_sac_BitsCoeff(qcoeff_P, Mode, CBP, bits, 64);
00371           if (CBPB)
00372             Count_sac_BitsCoeff(qcoeff_B, MODE_INTER, CBPB, bits, 64);
00373         } /* end SAC */
00374 
00375         QP_prev = QP_xmitted;
00376       }
00377 
00378       abs_mb_num++;
00379       QP_cumulative += QP_xmitted;     
00380 #ifdef PRINTQ 
00381       /* most useful when quantizer changes within a picture */
00382       if (QuantChangePostponed)
00383         fprintf(stdout,"@%2d",QP_xmitted);
00384       else
00385         fprintf(stdout," %2d",QP_xmitted);
00386 #endif
00387 
00388       if (pic->PB) 
00389         ReconImage(i,j,recon_data_B,B_recon);
00390 
00391       ReconImage(i,j,recon_data_P,recon);
00392       free(recon_data_P);
00393       free(qcoeff_P);
00394       if (pic->PB) {
00395         free(qcoeff_B);
00396         free(recon_data_B);
00397       }
00398     }
00399 #ifdef PRINTQ
00400     fprintf(stdout,"\n");
00401 #endif
00402   }
00403 
00404   pic->QP_mean = QP_cumulative/(float)abs_mb_num;
00405 
00406   /* Free memory */
00407   free(pi);
00408   if ((VidSt->mv_outside_frame)) {
00409     free(prev_recon);
00410     FreeImage(pr_edge);
00411   }
00412   for (j = 0; j < ((VidSt->lines)>>4)+1; j++)
00413     for (i = 0; i < ((VidSt->pels)>>4)+2; i++) 
00414       for (k = 0; k < 6; k++)
00415         free(MV[k][j][i]);
00416   return;
00417 }

int CodeTCoef int  mod_index,
int  position,
int  intra
 

Definition at line 686 of file vid_countbit.c.

References AR_Encode(), cumf_TCOEF1, cumf_TCOEF1_intra, cumf_TCOEF2, cumf_TCOEF2_intra, cumf_TCOEF3, cumf_TCOEF3_intra, cumf_TCOEFr, and cumf_TCOEFr_intra.

Referenced by Code_sac_Coeff().

00687 {
00688   int length;
00689  
00690   switch (position) {
00691     case 1:
00692     {
00693         if (intra)
00694           length = AR_Encode(mod_index, cumf_TCOEF1_intra);
00695         else
00696           length = AR_Encode(mod_index, cumf_TCOEF1);
00697         break;
00698     }
00699     case 2:
00700     {
00701         if (intra)
00702           length = AR_Encode(mod_index, cumf_TCOEF2_intra);
00703         else
00704           length = AR_Encode(mod_index, cumf_TCOEF2);
00705         break;
00706     }
00707     case 3:
00708     {
00709         if (intra)
00710           length = AR_Encode(mod_index, cumf_TCOEF3_intra);
00711         else
00712           length = AR_Encode(mod_index, cumf_TCOEF3);
00713         break;
00714     }
00715     default:
00716     {
00717         if (intra)
00718           length = AR_Encode(mod_index, cumf_TCOEFr_intra);
00719         else
00720           length = AR_Encode(mod_index, cumf_TCOEFr);
00721         break;
00722     }
00723   }
00724  
00725   return length;
00726 }

void ComputeSNR PictImage im1,
PictImage im2,
Results res,
int  write
 

Definition at line 67 of file vid_snr.c.

References pict_image::Cb, pict_image::Cr, video_codec::lines, pict_image::lum, video_codec::pels, PictImage, Results, results::SNR_Cb, results::SNR_Cr, results::SNR_l, and VidSt.

Referenced by code_video().

00068 {
00069   FILE *out = NULL;
00070   int n;
00071   register int m;
00072   int quad, quad_Cr, quad_Cb, diff;
00073   PictImage *diff_image = NULL;
00074   /* Diff. image written to diff_filename */
00075   char *diff_filename=DEF_DIFFILENAME;
00076 
00077   if (write) {
00078     out = fopen(diff_filename,"ab");
00079     diff_image = (PictImage *)malloc(sizeof(PictImage));
00080     diff_image->lum = (unsigned char *)malloc(sizeof(char)*(VidSt->pels)*(VidSt->lines));
00081     diff_image->Cr =  (unsigned char *)malloc(sizeof(char)*(VidSt->pels)*(VidSt->lines)/4);
00082     diff_image->Cb =  (unsigned char *)malloc(sizeof(char)*(VidSt->pels)*(VidSt->lines)/4);    
00083   }
00084 
00085   quad = 0;
00086   quad_Cr = quad_Cb = 0;
00087   /* Luminance */
00088   quad = 0;
00089   for (n = 0; n < (VidSt->lines); n++)
00090     for (m = 0; m < (VidSt->pels); m++) {
00091       diff = *(im1->lum + m + n*(VidSt->pels)) - *(im2->lum + m + n*(VidSt->pels));
00092       if (write)
00093         *(diff_image->lum + m + n*(VidSt->pels)) = 10*diff + 128;
00094       quad += diff * diff;
00095     }
00096 
00097   res->SNR_l = (float)quad/(float)((VidSt->pels)*(VidSt->lines));
00098   if (res->SNR_l) {
00099     res->SNR_l = (float)(255*255) / res->SNR_l;
00100     res->SNR_l = 10 * (float)log10(res->SNR_l);
00101   }
00102   else res->SNR_l = (float)99.99;
00103 
00104   /* Chrominance */
00105   for (n = 0; n < (VidSt->lines)/2; n++)
00106     for (m = 0; m < (VidSt->pels)/2; m++) {
00107       quad_Cr += (*(im1->Cr+m + n*(VidSt->pels)/2) - *(im2->Cr + m + n*(VidSt->pels)/2)) *
00108         (*(im1->Cr+m + n*(VidSt->pels)/2) - *(im2->Cr + m + n*(VidSt->pels)/2));
00109       quad_Cb += (*(im1->Cb+m + n*(VidSt->pels)/2) - *(im2->Cb + m + n*(VidSt->pels)/2)) *
00110         (*(im1->Cb+m + n*(VidSt->pels)/2) - *(im2->Cb + m + n*(VidSt->pels)/2));
00111       if (write) {
00112         *(diff_image->Cr + m + n*(VidSt->pels)/2) = 
00113           (*(im1->Cr+m + n*(VidSt->pels)/2) - *(im2->Cr + m + n*(VidSt->pels)/2))*10+128;
00114         *(diff_image->Cb + m + n*(VidSt->pels)/2) = 
00115           (*(im1->Cb+m + n*(VidSt->pels)/2) - *(im2->Cb + m + n*(VidSt->pels)/2))*10+128;
00116       }
00117     }
00118 
00119   res->SNR_Cr = (float)quad_Cr/(float)((VidSt->pels)*(VidSt->lines)/4);
00120   if (res->SNR_Cr) {
00121     res->SNR_Cr = (float)(255*255) / res->SNR_Cr;
00122     res->SNR_Cr = 10 * (float)log10(res->SNR_Cr);
00123   }
00124   else res->SNR_Cr = (float)99.99;
00125 
00126   res->SNR_Cb = (float)quad_Cb/(float)((VidSt->pels)*(VidSt->lines)/4);
00127   if (res->SNR_Cb) {
00128     res->SNR_Cb = (float)(255*255) / res->SNR_Cb;
00129     res->SNR_Cb = 10 * (float)log10(res->SNR_Cb);
00130   }
00131   else res->SNR_Cb = (float)99.99;
00132 
00133   if (write) {
00134     fwrite(diff_image->lum, sizeof(char), (VidSt->pels)*(VidSt->lines), out);
00135     fwrite(diff_image->Cr,  sizeof(char), (VidSt->pels)*(VidSt->lines)/4, out);
00136     fwrite(diff_image->Cb,  sizeof(char), (VidSt->pels)*(VidSt->lines)/4, out);
00137     free(diff_image->lum);
00138     free(diff_image->Cr);
00139     free(diff_image->Cb);
00140     free(diff_image);
00141     fclose(out);
00142   }
00143   return;
00144 }

void CopyVec MotionVector MV1,
MotionVector MV2
 

Definition at line 1127 of file vid_countbit.c.

References MotionVector, motionvector::x, motionvector::x_half, motionvector::y, and motionvector::y_half.

01128 {
01129   MV2->x = MV1->x;
01130   MV2->x_half = MV1->x_half;
01131   MV2->y = MV1->y;
01132   MV2->y_half = MV1->y_half;
01133   return;
01134 }

void Count_sac_BitsCoeff int *  qcoeff,
int  I,
int  CBP,
Bits bits,
int  ncoeffs
 

Definition at line 486 of file vid_countbit.c.

References video_codec::arith_used, Bits, bits_counted::C, Code_sac_Coeff(), MODE_INTRA, VidSt, and bits_counted::Y.

Referenced by CodeOneIntra(), and CodeOneOrTwo().

00487 {
00488  
00489   int i;
00490  
00491   (VidSt->arith_used) = 1;
00492  
00493   if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q) {
00494     for (i = 0; i < 4; i++) {
00495       bits->Y += Code_sac_Coeff(Mode, qcoeff,i,ncoeffs);
00496     }
00497     for (i = 4; i < 6; i++) {
00498       bits->C += Code_sac_Coeff(Mode, qcoeff,i,ncoeffs);
00499     }
00500   }
00501   else {
00502     for (i = 0; i < 4; i++) {
00503       if ((i==0 && CBP&32) ||
00504           (i==1 && CBP&16) ||
00505           (i==2 && CBP&8) ||
00506           (i==3 && CBP&4) ||
00507           (i==4 && CBP&2) ||
00508           (i==5 && CBP&1)) {
00509         bits->Y += Code_sac_Coeff(Mode, qcoeff, i, ncoeffs);
00510       }
00511     }
00512     for (i = 4; i < 6; i++) {
00513       if ((i==0 && CBP&32) ||
00514           (i==1 && CBP&16) ||
00515           (i==2 && CBP&8) ||
00516           (i==3 && CBP&4) ||
00517           (i==4 && CBP&2) ||
00518           (i==5 && CBP&1)) {
00519         bits->C += Code_sac_Coeff(Mode, qcoeff, i, ncoeffs);
00520       }
00521     }
00522   }
00523   return;
00524 }

void Count_sac_BitsMB int  Mode,
int  COD,
int  CBP,
int  CBPB,
Pict pic,
Bits bits
 

Definition at line 169 of file vid_countbit.c.

References AR_Encode(), video_codec::arith_used, Bits, bits_counted::CBPB, bits_counted::CBPCM, bits_counted::CBPY, cbpy_intratab, cbpytab, bits_counted::COD, cumf_CBPY, cumf_CBPY_intra, cumf_COD, cumf_DQUANT, cumf_MCBPC, cumf_MCBPC_intra, cumf_MODB, cumf_UVCBPB, cumf_YCBPB, pict::DQUANT, bits_counted::DQUANT, dquanttab, indexfn(), pict::MB, mcbpc_intratab, mcbpctab, bits_counted::MODB, pict::MODB, MODE_INTRA, pict::PB, PBMODE_CBPB_MVDB, PBMODE_MVDB, PBMODE_NORMAL, Pict, pict::picture_coding_type, video_codec::tf, video_codec::trace, and VidSt.

Referenced by CodeOneIntra(), and CodeOneOrTwo().

00170 {
00171   int cbpy, cbpcm, length, i;
00172  
00173   (VidSt->arith_used) = 1;
00174  
00175   /* COD */
00176  
00177   if ((VidSt->trace)) {
00178     fprintf((VidSt->tf),"MB-nr: %d",pic->MB);
00179     if (pic->picture_coding_type == PCT_INTER)
00180       fprintf((VidSt->tf),"  COD: %d ",COD);
00181   }
00182  
00183   if (pic->picture_coding_type == PCT_INTER)
00184     bits->COD+=AR_Encode(COD, cumf_COD);
00185  
00186   if (COD)
00187     return;    /* not coded */
00188  
00189   /* CBPCM */
00190  
00191   cbpcm = Mode | ((CBP&3)<<4);
00192   if ((VidSt->trace)) {
00193     fprintf((VidSt->tf),"CBPCM (CBP=%d) (cbpcm=%d): ",CBP,cbpcm);
00194   }
00195   if (pic->picture_coding_type == PCT_INTRA)
00196     length = AR_Encode(indexfn(cbpcm,mcbpc_intratab,9),cumf_MCBPC_intra);
00197   else
00198     length = AR_Encode(indexfn(cbpcm,mcbpctab,21),cumf_MCBPC);
00199  
00200   bits->CBPCM += length;
00201  
00202   /* MODB & CBPB */
00203    if (pic->PB) {
00204      switch (pic->MODB) {
00205      case PBMODE_NORMAL:
00206        bits->MODB += AR_Encode(0, cumf_MODB);
00207        break;
00208      case PBMODE_MVDB:
00209        bits->MODB += AR_Encode(1, cumf_MODB);
00210        break;
00211      case PBMODE_CBPB_MVDB:
00212        bits->MODB += AR_Encode(2, cumf_MODB);
00213        /* CBPB */
00214        for(i=5; i>1; i--)
00215          bits->CBPB += AR_Encode(((CBPB & 1<<i)>>i), cumf_YCBPB);
00216        for(i=1; i>-1; i--)
00217          bits->CBPB += AR_Encode(((CBPB & 1<<i)>>i), cumf_UVCBPB);
00218        break;
00219      }
00220      if ((VidSt->trace)) 
00221        fprintf((VidSt->tf),"MODB: %d, CBPB: %d\n", pic->MODB, CBPB);
00222    }
00223 
00224   /* CBPY */
00225  
00226   cbpy = CBP>>2;
00227   if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q) { /* Intra */
00228     length = AR_Encode(indexfn(cbpy,cbpy_intratab,16),cumf_CBPY_intra);
00229   } else {
00230     length = AR_Encode(indexfn(cbpy,cbpytab,16),cumf_CBPY);
00231   }
00232   if ((VidSt->trace)) {
00233     fprintf((VidSt->tf),"CBPY (CBP=%d) (cbpy=%d): ",CBP,cbpy);
00234   }
00235   bits->CBPY += length;
00236  
00237   /* DQUANT */
00238  
00239   if ((Mode == MODE_INTER_Q) || (Mode == MODE_INTRA_Q)) {
00240     if ((VidSt->trace)) {
00241       fprintf((VidSt->tf),"DQUANT: ");
00242     }
00243     bits->DQUANT += AR_Encode(indexfn(pic->DQUANT+2,dquanttab,4), cumf_DQUANT);
00244   }
00245   return;
00246 }

void Count_sac_BitsVectors MotionVector MV[5][MBR+1][MBC+2],
Bits bits,
int  i,
int  j,
int  Mode,
int  newgob,
Pict pic
 

Referenced by CodeOneOrTwo().

void CountBitsCoeff int *  qcoeff,
int  I,
int  CBP,
Bits bits,
int  ncoeffs
 

Definition at line 323 of file vid_countbit.c.

References Bits, bits_counted::C, CodeCoeff(), MODE_INTRA, and bits_counted::Y.

Referenced by CodeOneIntra(), and CodeOneOrTwo().

00324 {
00325   
00326   int i;
00327 
00328   if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q) {
00329     for (i = 0; i < 4; i++) {
00330       bits->Y += CodeCoeff(Mode, qcoeff,i,ncoeffs);
00331     }
00332     for (i = 4; i < 6; i++) {
00333       bits->C += CodeCoeff(Mode, qcoeff,i,ncoeffs);
00334     }
00335   }
00336   else {
00337     for (i = 0; i < 4; i++) {
00338       if ((i==0 && CBP&32) || 
00339           (i==1 && CBP&16) ||
00340           (i==2 && CBP&8) || 
00341           (i==3 && CBP&4) || 
00342           (i==4 && CBP&2) ||
00343           (i==5 && CBP&1)) {
00344         bits->Y += CodeCoeff(Mode, qcoeff, i, ncoeffs);
00345       }
00346     }
00347     for (i = 4; i < 6; i++) {
00348       if ((i==0 && CBP&32) || 
00349           (i==1 && CBP&16) ||
00350           (i==2 && CBP&8) || 
00351           (i==3 && CBP&4) || 
00352           (i==4 && CBP&2) ||
00353           (i==5 && CBP&1)) {
00354         bits->C += CodeCoeff(Mode, qcoeff, i, ncoeffs);
00355       }
00356     }
00357   }
00358   return;
00359 }

void CountBitsMB int  Mode,
int  COD,
int  CBP,
int  CBPB,
Pict pic,
Bits bits
 

Definition at line 65 of file vid_countbit.c.

References Bits, bits_counted::CBPB, bits_counted::CBPCM, bits_counted::CBPY, bits_counted::COD, bits_counted::DQUANT, pict::DQUANT, pict::MB, bits_counted::MODB, pict::MODB, MODE_INTRA, pict::PB, PBMODE_CBPB_MVDB, PBMODE_MVDB, PBMODE_NORMAL, Pict, pict::picture_coding_type, put_cbpcm_inter(), put_cbpcm_intra(), put_cbpy(), putbits(), video_codec::tf, video_codec::trace, and VidSt.

Referenced by CodeOneIntra(), and CodeOneOrTwo().

00066 {
00067   int cbpy, cbpcm, length;
00068 
00069   /* COD */
00070   if ((VidSt->trace)) {
00071     fprintf((VidSt->tf),"MB-nr: %d",pic->MB);
00072     if (pic->picture_coding_type == PCT_INTER)
00073       fprintf((VidSt->tf),"  COD: %d\n",COD);
00074   }
00075   if (pic->picture_coding_type == PCT_INTER) {
00076     putbits(1,COD);
00077     bits->COD++;
00078   }
00079 
00080   if (COD) 
00081     return;    /* not coded */
00082 
00083   /* CBPCM */
00084   cbpcm = Mode | ((CBP&3)<<4);
00085   if ((VidSt->trace)) {
00086     fprintf((VidSt->tf),"CBPCM (CBP=%d) (cbpcm=%d): ",CBP,cbpcm);
00087   }
00088   if (pic->picture_coding_type == PCT_INTRA)
00089     length = put_cbpcm_intra (CBP, Mode);
00090   else
00091     length = put_cbpcm_inter (CBP, Mode);
00092   bits->CBPCM += length;
00093 
00094     /* MODB & CBPB */
00095   if (pic->PB) {
00096     switch (pic->MODB) {
00097     case PBMODE_NORMAL:
00098       putbits(1,0);
00099       bits->MODB += 1;
00100       break;
00101     case PBMODE_MVDB:
00102       putbits(2,2);
00103       bits->MODB += 2;
00104       break;
00105     case PBMODE_CBPB_MVDB:
00106       putbits(2,3);
00107       bits->MODB += 2;
00108       /* CBPB */
00109       putbits(6,CBPB);
00110       bits->CBPB += 6;
00111       break;
00112     }
00113     if ((VidSt->trace)) 
00114       fprintf((VidSt->tf),"MODB: %d, CBPB: %d\n", pic->MODB, CBPB);
00115   }
00116     
00117   /* CBPY */
00118   cbpy = CBP>>2;
00119   if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q) /* Intra */
00120     cbpy = cbpy^15;
00121   if ((VidSt->trace)) {
00122     fprintf((VidSt->tf),"CBPY (CBP=%d) (cbpy=%d): ",CBP,cbpy);
00123   }
00124   length = put_cbpy (CBP, Mode);
00125 
00126   bits->CBPY += length;
00127   
00128   /* DQUANT */
00129   if ((Mode == MODE_INTER_Q) || (Mode == MODE_INTRA_Q)) {
00130     if ((VidSt->trace)) {
00131       fprintf((VidSt->tf),"DQUANT: ");
00132     }
00133     switch (pic->DQUANT) {
00134     case -1:
00135       putbits(2,0);
00136       break;
00137     case -2:
00138       putbits(2,1);
00139       break;
00140     case 1:
00141       putbits(2,2);
00142       break;
00143     case 2:
00144       putbits(2,3);
00145       break;
00146     default:
00147       fprintf(stderr,"Invalid DQUANT\n");
00148       exit(-1);
00149     }
00150     bits->DQUANT += 2;
00151   }
00152   return;
00153 }

int CountBitsPicture Pict pic  ) 
 

Definition at line 1163 of file vid_countbit.c.

References video_codec::advanced, pict::BQUANT, pict::PB, Pict, pict::picture_coding_type, PSC, PSC_LENGTH, putbits(), pict::QUANT, pict::source_format, pict::spare, video_codec::syntax_arith_coding, video_codec::tf, pict::TR, video_codec::trace, pict::TRB, pict::unrestricted_mv_mode, and VidSt.

Referenced by CodeOneIntra(), and CodeOneOrTwo().

01164 {
01165   int bits = 0;
01166 
01167   /* in case of arithmetic coding, encoder_flush() has been called before
01168      zeroflush() in vid_main.c */
01169 
01170   /* Picture start code */
01171   if ((VidSt->trace)) {
01172     fprintf((VidSt->tf),"picture_start_code: ");
01173   }
01174   putbits(PSC_LENGTH,PSC);
01175   bits += PSC_LENGTH;
01176 
01177   /* Group number */
01178   if ((VidSt->trace)) {
01179     fprintf((VidSt->tf),"Group number in picture header: ");
01180   }
01181   putbits(5,0); 
01182   bits += 5;
01183   
01184   /* Time reference */
01185   if ((VidSt->trace)) {
01186     fprintf((VidSt->tf),"Time reference: ");
01187   }
01188   putbits(8,pic->TR);
01189   bits += 8;
01190 
01191  /* bit 1 */
01192   if ((VidSt->trace)) {
01193     fprintf((VidSt->tf),"spare: ");
01194   }
01195   pic->spare = 1; /* always 1 to avoid start code emulation */
01196   putbits(1,pic->spare);
01197   bits += 1;
01198 
01199   /* bit 2 */
01200   if ((VidSt->trace)) {
01201     fprintf((VidSt->tf),"always zero for distinction with H.261\n");
01202   }
01203   putbits(1,0);
01204   bits += 1;
01205   
01206   /* bit 3 */
01207   if ((VidSt->trace)) {
01208     fprintf((VidSt->tf),"split_screen_indicator: ");
01209   }
01210   putbits(1,0);     /* no support for split-screen in this software */
01211   bits += 1;
01212 
01213   /* bit 4 */
01214   if ((VidSt->trace)) {
01215     fprintf((VidSt->tf),"document_camera_indicator: ");
01216   }
01217   putbits(1,0);
01218   bits += 1;
01219 
01220   /* bit 5 */
01221   if ((VidSt->trace)) {
01222     fprintf((VidSt->tf),"freeze_picture_release: ");
01223   }
01224   putbits(1,0);
01225   bits += 1;
01226 
01227   /* bit 6-8 */
01228   if ((VidSt->trace)) {
01229     fprintf((VidSt->tf),"source_format: ");
01230   }
01231   putbits(3,pic->source_format);
01232   bits += 3;
01233 
01234   /* bit 9 */
01235   if ((VidSt->trace)) {
01236     fprintf((VidSt->tf),"picture_coding_type: ");
01237   }
01238   putbits(1,pic->picture_coding_type);
01239   bits += 1;
01240 
01241   /* bit 10 */
01242   if ((VidSt->trace)) {
01243     fprintf((VidSt->tf),"mv_outside_frame: ");
01244   }
01245   putbits(1,pic->unrestricted_mv_mode);  /* Unrestricted Motion Vector mode */
01246   bits += 1;
01247 
01248   /* bit 11 */
01249   if ((VidSt->trace)) {
01250     fprintf((VidSt->tf),"sac_coding: ");
01251   }
01252   putbits(1,(VidSt->syntax_arith_coding)); /* Syntax-based Arithmetic Coding mode */
01253   bits += 1;
01254 
01255   /* bit 12 */
01256   if ((VidSt->trace)) {
01257     fprintf((VidSt->tf),"adv_pred_mode: ");
01258   }
01259   putbits(1,(VidSt->advanced)); /* Advanced Prediction mode */
01260   bits += 1;
01261 
01262   /* bit 13 */
01263   if ((VidSt->trace)) {
01264     fprintf((VidSt->tf),"PB-coded: "); /* PB-frames mode */
01265   }
01266   putbits(1,pic->PB);
01267   bits += 1;
01268 
01269 
01270   /* QUANT */
01271   if ((VidSt->trace)) {
01272     fprintf((VidSt->tf),"QUANT: ");
01273   }
01274   putbits(5,pic->QUANT);
01275   bits += 5;
01276 
01277   /* Continuous Presence Multipoint (CPM) */
01278   putbits(1,0); /* CPM is not supported in this software */
01279   bits += 1;
01280 
01281   /* Picture Sub Bitstream Indicator (PSBI) */
01282   /* if CPM == 1: 2 bits PSBI */
01283   /* not supported */
01284 
01285   /* extra information for PB-frames */
01286   if (pic->PB) {
01287     if ((VidSt->trace)) {
01288       fprintf((VidSt->tf),"TRB: ");
01289     }
01290     putbits(3,pic->TRB);
01291     bits += 3;
01292 
01293     if ((VidSt->trace)) {
01294       fprintf((VidSt->tf),"BQUANT: ");
01295     }
01296     putbits(2,pic->BQUANT);
01297     bits += 2;
01298     
01299   }
01300 
01301   /* PEI (extra information) */
01302   if ((VidSt->trace)) {
01303     fprintf((VidSt->tf),"PEI: ");
01304   }
01305   /* "Encoders shall not insert PSPARE until specified by the ITU" */
01306   putbits(1,0); 
01307   bits += 1;
01308 
01309   /* PSPARE */
01310   /* if PEI == 1: 8 bits PSPARE + another PEI bit */
01311   /* not supported */
01312 
01313   return bits;
01314 }

int CountBitsSlice int  slice,
int  quant
 

Definition at line 260 of file vid_countbit.c.

References video_codec::arith_used, encoder_flush(), PSC, PSC_LENGTH, putbits(), video_codec::tf, video_codec::trace, and VidSt.

Referenced by CodeOneIntra(), and CodeOneOrTwo().

00261 {
00262   int bits = 0;
00263 
00264   if ((VidSt->arith_used)) {
00265     bits+=encoder_flush(); /* Need to call before fixed length string output */
00266     (VidSt->arith_used) = 0;
00267   }
00268 
00269   /* Picture Start Code */
00270   if ((VidSt->trace))
00271     fprintf((VidSt->tf),"GOB sync (GBSC): ");
00272   putbits(PSC_LENGTH,PSC); /* PSC */
00273   bits += PSC_LENGTH;
00274 
00275   /* Group Number */
00276   if ((VidSt->trace))
00277     fprintf((VidSt->tf),"GN: ");
00278   putbits(5,slice);
00279   bits += 5;
00280 
00281   /* GOB Sub Bitstream Indicator */
00282   /* if CPM == 1: read 2 bits GSBI */
00283   /* not supported in this version */
00284 
00285   /* GOB Frame ID */
00286   if ((VidSt->trace))
00287     fprintf((VidSt->tf),"GFID: ");
00288   putbits(2, 0);  
00289   /* NB: in error-prone environments this value should change if 
00290      PTYPE in picture header changes. In this version of the encoder
00291      PTYPE only changes when PB-frames are used in the following cases:
00292      (i) after the first intra frame
00293      (ii) if the distance between two P-frames is very large 
00294      Therefore I haven't implemented this GFID change */
00295   /* GFID is not allowed to change unless PTYPE changes */
00296   bits += 2;
00297 
00298   /* Gquant */
00299   if ((VidSt->trace))
00300     fprintf((VidSt->tf),"GQUANT: ");
00301   putbits(5,quant);
00302   bits += 5;
00303 
00304   return bits;
00305 }

void CountBitsVectors MotionVector MV[5][MBR+1][MBC+2],
Bits bits,
int  i,
int  j,
int  Mode,
int  newgob,
Pict pic
 

Referenced by CodeOneOrTwo().

int Dct int *  block,
int *  coeff
 

Definition at line 89 of file vid_dct.c.

References zigzag.

Referenced by MB_Encode().

00090 {
00091   int        j1, i, j, k;
00092   float b[8];
00093   float        b1[8];
00094   float        d[8][8];
00095   float f0=(float).7071068;
00096   float f1=(float).4903926;
00097   float f2=(float).4619398;
00098   float f3=(float).4157348;
00099   float f4=(float).3535534;
00100   float f5=(float).2777851;
00101   float f6=(float).1913417;
00102   float f7=(float).0975452;
00103 
00104   for (i = 0, k = 0; i < 8; i++, k += 8) {
00105     for (j = 0; j < 8; j++) {
00106       b[j] = (float)block[k+j];
00107     }
00108     /* Horizontal transform */
00109     for (j = 0; j < 4; j++) {
00110       j1 = 7 - j;
00111       b1[j] = b[j] + b[j1];
00112       b1[j1] = b[j] - b[j1];
00113     }
00114     b[0] = b1[0] + b1[3];
00115     b[1] = b1[1] + b1[2];
00116     b[2] = b1[1] - b1[2];
00117     b[3] = b1[0] - b1[3];
00118     b[4] = b1[4];
00119     b[5] = (b1[6] - b1[5]) * f0;
00120     b[6] = (b1[6] + b1[5]) * f0;
00121     b[7] = b1[7];
00122     d[i][0] = (b[0] + b[1]) * f4;
00123     d[i][4] = (b[0] - b[1]) * f4;
00124     d[i][2] = b[2] * f6 + b[3] * f2;
00125     d[i][6] = b[3] * f6 - b[2] * f2;
00126     b1[4] = b[4] + b[5];
00127     b1[7] = b[7] + b[6];
00128     b1[5] = b[4] - b[5];
00129     b1[6] = b[7] - b[6];
00130     d[i][1] = b1[4] * f7 + b1[7] * f1;
00131     d[i][5] = b1[5] * f3 + b1[6] * f5;
00132     d[i][7] = b1[7] * f7 - b1[4] * f1;
00133     d[i][3] = b1[6] * f3 - b1[5] * f5;
00134   }
00135   /* Vertical transform */
00136   for (i = 0; i < 8; i++) {
00137     for (j = 0; j < 4; j++) {
00138       j1 = 7 - j;
00139       b1[j] = d[j][i] + d[j1][i];
00140       b1[j1] = d[j][i] - d[j1][i];
00141     }
00142     b[0] = b1[0] + b1[3];
00143     b[1] = b1[1] + b1[2];
00144     b[2] = b1[1] - b1[2];
00145     b[3] = b1[0] - b1[3];
00146     b[4] = b1[4];
00147     b[5] = (b1[6] - b1[5]) * f0;
00148     b[6] = (b1[6] + b1[5]) * f0;
00149     b[7] = b1[7];
00150     d[0][i] = (b[0] + b[1]) * f4;
00151     d[4][i] = (b[0] - b[1]) * f4;
00152     d[2][i] = b[2] * f6 + b[3] * f2;
00153     d[6][i] = b[3] * f6 - b[2] * f2;
00154     b1[4] = b[4] + b[5];
00155     b1[7] = b[7] + b[6];
00156     b1[5] = b[4] - b[5];
00157     b1[6] = b[7] - b[6];
00158     d[1][i] = b1[4] * f7 + b1[7] * f1;
00159     d[5][i] = b1[5] * f3 + b1[6] * f5;
00160     d[7][i] = b1[7] * f7 - b1[4] * f1;
00161     d[3][i] = b1[6] * f3 - b1[5] * f5;
00162   }
00163   /* Zigzag - scanning */
00164   for (i = 0; i < 8; i++) {
00165     for (j = 0; j < 8; j++) {
00166       *(coeff + zigzag[i][j]) = (int)(d[i][j]);
00167     }
00168   }
00169   return 0;
00170 }

void Dequant int *  qcoeff,
int *  rcoeff,
int  QP,
int  I
 

Definition at line 108 of file vid_quant.c.

References MODE_INTRA, and sign.

Referenced by MB_Decode().

00109 {
00110   int i;
00111   
00112   if (QP) {
00113     for (i = 0; i < 64; i++) {
00114       if (qcoeff[i]) {
00115         if ((QP % 2) == 1)
00116           rcoeff[i] = QP * (2*abs(qcoeff[i]) + 1);
00117         else
00118           rcoeff[i] = QP * (2*abs(qcoeff[i]) + 1) - 1;
00119         rcoeff[i] = sign(qcoeff[i]) * rcoeff[i];
00120       }
00121       else
00122         rcoeff[i] = 0;
00123     }
00124     if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q) { /* Intra */
00125       rcoeff[0] = qcoeff[0]*8;
00126     }
00127   }
00128   else {
00129     /* No quantizing at all */
00130     for (i = 0; i < 64; i++) {
00131       rcoeff[i] = qcoeff[i];
00132     }
00133   }
00134   return;
00135 }

void DoPredChrom_P int  x_curr,
int  y_curr,
int  dx,
int  dy,
PictImage curr,
PictImage prev,
MB_Structure pred_error
 

Definition at line 676 of file vid_pred.c.

References mb_structure::Cb, pict_image::Cb, video_codec::cpels, mb_structure::Cr, pict_image::Cr, video_codec::long_vectors, MB_Structure, video_codec::mv_outside_frame, video_codec::pels, PictImage, and VidSt.

Referenced by Predict_P().

00679 {
00680   int m,n;
00681 
00682   int x, y, ofx, ofy, pel, lx;
00683   int xint, yint;
00684   int xh, yh;
00685 
00686   lx = ((VidSt->mv_outside_frame) ? (VidSt->pels)/2 + ((VidSt->long_vectors)?32:16) : (VidSt->pels)/2);
00687 
00688   x = x_curr>>1;
00689   y = y_curr>>1;
00690 
00691   xint = dx>>1;
00692   xh = dx & 1;
00693   yint = dy>>1;
00694   yh = dy & 1;
00695 
00696   if (!xh && !yh) {
00697     for (n = 0; n < 8; n++) {
00698       for (m = 0; m < 8; m++) {
00699 
00700         ofx = x + xint + m;
00701         ofy = y + yint + n;
00702         pel=*(prev->Cr+ofx    + (ofy   )*lx);
00703         pred_error->Cr[n][m] = (int)(*(curr->Cr + x+m + (y+n)*(VidSt->cpels)) - pel);
00704 
00705         pel=*(prev->Cb+ofx    + (ofy   )*lx);
00706         pred_error->Cb[n][m] = (int)(*(curr->Cb + x+m + (y+n)*(VidSt->cpels)) - pel);
00707       }
00708     }
00709   }
00710   else if (!xh && yh) {
00711     for (n = 0; n < 8; n++) {
00712       for (m = 0; m < 8; m++) {
00713 
00714         ofx = x + xint + m;
00715         ofy = y + yint + n;
00716         pel=(*(prev->Cr+ofx    + (ofy   )*lx)+
00717              *(prev->Cr+ofx    + (ofy+yh)*lx) + 1)>>1;
00718 
00719         pred_error->Cr[n][m] = 
00720           (int)(*(curr->Cr + x+m + (y+n)*(VidSt->cpels)) - pel);
00721 
00722         pel=(*(prev->Cb+ofx    + (ofy   )*lx)+
00723              *(prev->Cb+ofx    + (ofy+yh)*lx) + 1)>>1;
00724 
00725         pred_error->Cb[n][m] = 
00726           (int)(*(curr->Cb + x+m + (y+n)*(VidSt->cpels)) - pel);
00727       
00728       }
00729     }
00730   }
00731   else if (xh && !yh) {
00732     for (n = 0; n < 8; n++) {
00733       for (m = 0; m < 8; m++) {
00734 
00735         ofx = x + xint + m;
00736         ofy = y + yint + n;
00737         pel=(*(prev->Cr+ofx    + (ofy   )*lx)+
00738              *(prev->Cr+ofx+xh + (ofy   )*lx) + 1)>>1;
00739 
00740         pred_error->Cr[n][m] = 
00741           (int)(*(curr->Cr + x+m + (y+n)*(VidSt->cpels)) - pel);
00742 
00743         pel=(*(prev->Cb+ofx    + (ofy   )*lx)+
00744              *(prev->Cb+ofx+xh + (ofy   )*lx) + 1)>>1;
00745 
00746         pred_error->Cb[n][m] = 
00747           (int)(*(curr->Cb + x+m + (y+n)*(VidSt->cpels)) - pel);
00748       
00749       }
00750     }
00751   }
00752   else { /* xh && yh */
00753     for (n = 0; n < 8; n++) {
00754       for (m = 0; m < 8; m++) {
00755         ofx = x + xint + m;
00756         ofy = y + yint + n;
00757         pel=(*(prev->Cr+ofx    + (ofy   )*lx)+
00758              *(prev->Cr+ofx+xh + (ofy   )*lx)+
00759              *(prev->Cr+ofx    + (ofy+yh)*lx)+
00760              *(prev->Cr+ofx+xh + (ofy+yh)*lx)+
00761              2)>>2;
00762 
00763         pred_error->Cr[n][m] = 
00764           (int)(*(curr->Cr + x+m + (y+n)*(VidSt->cpels)) - pel);
00765 
00766         pel=(*(prev->Cb+ofx    + (ofy   )*lx)+
00767              *(prev->Cb+ofx+xh + (ofy   )*lx)+
00768              *(prev->Cb+ofx    + (ofy+yh)*lx)+
00769              *(prev->Cb+ofx+xh + (ofy+yh)*lx)+
00770              2)>>2;
00771 
00772         pred_error->Cb[n][m] = 
00773           (int)(*(curr->Cb + x+m + (y+n)*(VidSt->cpels)) - pel);
00774       
00775       }
00776     }
00777   }
00778   return;
00779 }

int encoder_flush  ) 
 

Definition at line 150 of file vid_sac.c.

References bit_opp_bits(), bitcount(), low, video_codec::tf, video_codec::trace, and VidSt.

Referenced by code_video(), and CountBitsSlice().

00151 {
00152   int bitcount = 0;
00153 
00154   if ((VidSt->trace))
00155     fprintf((VidSt->tf), "encoder_flush:\n");
00156 
00157   opposite_bits++;
00158   if (low < q1) {
00159     bitcount+=bit_opp_bits(0);
00160   }
00161   else {
00162     bitcount+=bit_opp_bits(1);
00163   }
00164   low = 0; 
00165   high = top;
00166 
00167   zerorun=0;
00168 
00169   return bitcount;
00170 }

int EqualVec MotionVector MV2,
MotionVector MV1
 

Definition at line 1136 of file vid_countbit.c.

References MotionVector, motionvector::x, motionvector::x_half, motionvector::y, and motionvector::y_half.

Referenced by CodeOneOrTwo().

01137 {
01138   if (MV1->x != MV2->x)
01139     return 0;
01140   if (MV1->y != MV2->y)
01141     return 0;
01142   if (MV1->x_half != MV2->x_half)
01143     return 0;
01144   if (MV1->y_half != MV2->y_half)
01145     return 0;
01146   return 1;
01147 }

void FillChromBlock int  x_curr,
int  y_curr,
PictImage image,
MB_Structure data
 

Definition at line 682 of file vid_coder.c.

References pict_image::Cb, mb_structure::Cb, video_codec::cpels, pict_image::Cr, mb_structure::Cr, data, MB_Structure, PictImage, and VidSt.

Referenced by CodeOneIntra(), and CodeOneOrTwo().

00684 {
00685   int n;
00686   register int m;
00687 
00688   int x, y;
00689 
00690   x = x_curr>>1;
00691   y = y_curr>>1;
00692 
00693   for (n = 0; n < (MB_SIZE>>1); n++)
00694     for (m = 0; m < (MB_SIZE>>1); m++) {
00695       data->Cr[n][m] = 
00696         (int)(*(image->Cr +x+m + (y+n)*(VidSt->cpels)));
00697       data->Cb[n][m] = 
00698         (int)(*(image->Cb +x+m + (y+n)*(VidSt->cpels)));
00699     }
00700   return;
00701 }

PictImage* FillImage unsigned char *  in  ) 
 

Definition at line 114 of file vid_io.c.

References InitImage(), video_codec::lines, video_codec::pels, Pict, PictImage, and VidSt.

Referenced by code_video().

00115 {
00116   PictImage *Pict;
00117 
00118   Pict = InitImage((VidSt->pels)*(VidSt->lines));
00119 
00120   memcpy(Pict->lum, in, (VidSt->pels)*(VidSt->lines));
00121   memcpy(Pict->Cb, in + (VidSt->pels)*(VidSt->lines), (VidSt->pels)*(VidSt->lines)/4);
00122   memcpy(Pict->Cr, in + (VidSt->pels)*(VidSt->lines) + (VidSt->pels)*(VidSt->lines)/4, (VidSt->pels)*(VidSt->lines)/4);
00123 
00124   free(in);
00125   return(Pict);
00126 }

void FillLumBlock int  x,
int  y,
PictImage image,
MB_Structure data
 

Definition at line 656 of file vid_coder.c.

References data, pict_image::lum, mb_structure::lum, MB_Structure, video_codec::pels, PictImage, and VidSt.

Referenced by CodeOneIntra(), and CodeOneOrTwo().

00657 {
00658   int n;
00659   register int m;
00660 
00661   for (n = 0; n < MB_SIZE; n++)
00662     for (m = 0; m < MB_SIZE; m++)
00663       data->lum[n][m] = 
00664         (int)(*(image->lum + x+m + (y+n)*(VidSt->pels)));
00665   return;
00666 }

void FindBiDirChromaLimits int  vec,
int *  start,
int *  stop
 

Definition at line 596 of file vid_pred.c.

References mmax, and mmin.

Referenced by FindBiDirChrPredPB().

00597 {
00598 
00599   /* limits taken from C loop in section G5 in H.263 */
00600   *start = mmax(0,(-vec+1)/2);
00601   *stop = mmin(7,7-(vec+1)/2);
00602 
00603   return;
00604 }

void FindBiDirChrPredPB MB_Structure recon_P,
int  dx,
int  dy,
MB_Structure pred
 

Definition at line 569 of file vid_pred.c.

References BiDirPredBlock(), mb_structure::Cb, mb_structure::Cr, FindBiDirChromaLimits(), and MB_Structure.

Referenced by MB_Recon_B(), and Predict_B().

00571 {
00572   int xstart,xstop,ystart,ystop;
00573 
00574   FindBiDirChromaLimits(dx,&xstart,&xstop);
00575   FindBiDirChromaLimits(dy,&ystart,&ystop);
00576 
00577   BiDirPredBlock(xstart,xstop,ystart,ystop,dx,dy,
00578          &recon_P->Cb[0][0], &pred->Cb[0][0],8);
00579   BiDirPredBlock(xstart,xstop,ystart,ystop,dx,dy,
00580          &recon_P->Cr[0][0], &pred->Cr[0][0],8);
00581 
00582   return;
00583 }

void FindBiDirLimits int  vec,
int *  start,
int *  stop,
int  nhv
 

Definition at line 585 of file vid_pred.c.

References mmax, and mmin.

Referenced by FindBiDirLumPredPB().

00586 {
00587 
00588   /* limits taken from C loop in section G5 in H.263 */
00589   *start = mmax(0,(-vec+1)/2 - nhv*8);
00590   *stop = mmin(7,15-(vec+1)/2 - nhv*8);
00591 
00592   return;
00593 
00594 }

void FindBiDirLumPredPB int *  recon_P,
MotionVector fr,
int *  pred,
int  TRD,
int  TRB,
int  bdx,
int  bdy,
int  nh,
int  nv
 

Definition at line 534 of file vid_pred.c.

References BiDirPredBlock(), FindBiDirLimits(), MotionVector, motionvector::x, motionvector::x_half, motionvector::y, and motionvector::y_half.

Referenced by MB_Recon_B(), and Predict_B().

00536 {
00537   int xstart,xstop,ystart,ystop;
00538   int xvec,yvec, mvx, mvy;
00539 
00540   mvx = 2*fr->x + fr->x_half;
00541   mvy = 2*fr->y + fr->y_half;
00542 
00543   xvec = (bdx == 0 ? (TRB-TRD) *  mvx / TRD : TRB * mvx / TRD + bdx - mvx);
00544   yvec = (bdy == 0 ? (TRB-TRD) *  mvy / TRD : TRB * mvy / TRD + bdy - mvy);
00545 
00546   /* Luma */
00547 
00548   FindBiDirLimits(xvec,&xstart,&xstop,nh);
00549   FindBiDirLimits(yvec,&ystart,&ystop,nv);
00550 
00551   BiDirPredBlock(xstart,xstop,ystart,ystop,xvec,yvec, recon_P,pred,16);
00552 
00553   return;
00554 }

int FindCBP int *  qcoeff,
int  Mode,
int  ncoeffs
 

Definition at line 743 of file vid_countbit.c.

References MODE_INTRA.

Referenced by CodeOneIntra(), and CodeOneOrTwo().

00744 {
00745   
00746   int i,j;
00747   int CBP = 0;
00748   int intra = (Mode == MODE_INTRA || Mode == MODE_INTRA_Q);
00749 
00750   /* Set CBP for this Macroblock */
00751   for (i = 0; i < 6; i++) {
00752     for (j = i*ncoeffs + intra; j < (i+1)*ncoeffs; j++) {
00753       if (qcoeff[j]) {
00754         if (i == 0) {CBP |= 32;}
00755         else if (i == 1) {CBP |= 16;}
00756         else if (i == 2) {CBP |= 8;}
00757         else if (i == 3) {CBP |= 4;}
00758         else if (i == 4) {CBP |= 2;}
00759         else if (i == 5) {CBP |= 1;}
00760         else {
00761           fprintf(stderr,"Error in CBP assignment\n");
00762           exit(-1);
00763         }
00764         break;
00765       }
00766     }
00767   }
00768 
00769   return CBP;
00770 }

void FindChromBlock_P int  x_curr,
int  y_curr,
int  dx,
int  dy,
PictImage prev,
MB_Structure data
 

Definition at line 1395 of file vid_pred.c.

References mb_structure::Cb, pict_image::Cb, mb_structure::Cr, pict_image::Cr, data, video_codec::long_vectors, MB_Structure, video_codec::mv_outside_frame, video_codec::pels, PictImage, and VidSt.

Referenced by MB_Recon_B(), and Predict_B().

01398 {
01399   int m,n;
01400 
01401   int x, y, ofx, ofy, pel,lx;
01402   int xint, yint;
01403   int xh, yh;
01404 
01405   lx = ((VidSt->mv_outside_frame) ? (VidSt->pels)/2 + ((VidSt->long_vectors)?32:16) : (VidSt->pels)/2);
01406 
01407   x = x_curr>>1;
01408   y = y_curr>>1;
01409 
01410   xint = dx>>1;
01411   xh = dx & 1;
01412   yint = dy>>1;
01413   yh = dy & 1;
01414     
01415   if (!xh && !yh) {
01416     for (n = 0; n < 8; n++) {
01417       for (m = 0; m < 8; m++) {
01418 
01419         ofx = x + xint + m;
01420         ofy = y + yint + n;
01421         pel=*(prev->Cr+ofx    + (ofy   )*lx);
01422         data->Cr[n][m] = pel;
01423 
01424         pel=*(prev->Cb+ofx    + (ofy   )*lx);
01425         data->Cb[n][m] = pel;
01426       }
01427     }
01428   }
01429   else if (!xh && yh) {
01430     for (n = 0; n < 8; n++) {
01431       for (m = 0; m < 8; m++) {
01432 
01433         ofx = x + xint + m;
01434         ofy = y + yint + n;
01435         pel=(*(prev->Cr+ofx    + (ofy   )*lx)+
01436              *(prev->Cr+ofx    + (ofy+yh)*lx) + 1)>>1;
01437 
01438         data->Cr[n][m] = pel;
01439 
01440         pel=(*(prev->Cb+ofx    + (ofy   )*lx)+
01441              *(prev->Cb+ofx    + (ofy+yh)*lx) + 1)>>1;
01442 
01443         data->Cb[n][m] = pel;
01444       
01445       }
01446     }
01447   }
01448   else if (xh && !yh) {
01449     for (n = 0; n < 8; n++) {
01450       for (m = 0; m < 8; m++) {
01451 
01452         ofx = x + xint + m;
01453         ofy = y + yint + n;
01454         pel=(*(prev->Cr+ofx    + (ofy   )*lx)+
01455              *(prev->Cr+ofx+xh + (ofy   )*lx) + 1)>>1;
01456 
01457         data->Cr[n][m] = pel;
01458 
01459         pel=(*(prev->Cb+ofx    + (ofy   )*lx)+
01460              *(prev->Cb+ofx+xh + (ofy   )*lx) + 1)>>1;
01461 
01462         data->Cb[n][m] = pel;
01463       
01464       }
01465     }
01466   }
01467   else { /* xh && yh */
01468     for (n = 0; n < 8; n++) {
01469       for (m = 0; m < 8; m++) {
01470         ofx = x + xint + m;
01471         ofy = y + yint + n;
01472         pel=(*(prev->Cr+ofx    + (ofy   )*lx)+
01473              *(prev->Cr+ofx+xh + (ofy   )*lx)+
01474              *(prev->Cr+ofx    + (ofy+yh)*lx)+
01475              *(prev->Cr+ofx+xh + (ofy+yh)*lx)+
01476              2)>>2;
01477 
01478         data->Cr[n][m] = pel;
01479 
01480         pel=(*(prev->Cb+ofx    + (ofy   )*lx)+
01481              *(prev->Cb+ofx+xh + (ofy   )*lx)+
01482              *(prev->Cb+ofx    + (ofy+yh)*lx)+
01483              *(prev->Cb+ofx+xh + (ofy+yh)*lx)+
01484              2)>>2;
01485 
01486         data->Cb[n][m] = pel;
01487       
01488       }
01489     }
01490   }
01491   return;
01492 }

void FindForwLumPredPB unsigned char *  prev_ipol,
int  x_curr,
int  y_curr,
MotionVector fr,
int *  pred,
int  TRD,
int  TRB,
int  bdx,
int  bdy,
int  bs,
int  comp
 

Definition at line 494 of file vid_pred.c.

References video_codec::long_vectors, MotionVector, video_codec::mv_outside_frame, video_codec::pels, VidSt, motionvector::x, motionvector::x_half, motionvector::y, and motionvector::y_half.

Referenced by MB_Recon_B(), and Predict_B().

00497 {
00498   int i,j;
00499   int xvec,yvec,lx;
00500 
00501   lx = ((VidSt->mv_outside_frame) ? (VidSt->pels) + ((VidSt->long_vectors)?64:32) : (VidSt->pels));
00502 
00503   /* Luma */
00504   xvec = (TRB)*(2*fr->x + fr->x_half)/TRD + bdx;
00505   yvec = (TRB)*(2*fr->y + fr->y_half)/TRD + bdy;
00506 
00507   x_curr += ((comp&1)<<3);
00508   y_curr += ((comp&2)<<2);
00509 
00510   for (j = 0; j < bs; j++) {
00511     for (i = 0; i < bs; i++) {
00512       *(pred+i+j*16) = *(prev_ipol + (i+x_curr)*2 + xvec +
00513          ((j+y_curr)*2 + yvec)*lx*2);
00514     }
00515   }
00516 
00517   return;
00518 }

void FindHalfPel int  x,
int  y,
MotionVector MV,
unsigned char *  prev,
int *  curr,
int  bs,
int  comp
 

Definition at line 797 of file vid_pred.c.

References video_codec::lines, video_codec::long_vectors, motionvector::min_error, MotionVector, video_codec::mv_outside_frame, video_codec::pels, Point, VidSt, point::x, motionvector::x, motionvector::x_half, point::y, motionvector::y, and motionvector::y_half.

Referenced by MotionEstimatePicture().

00799 {
00800   int i, m, n;
00801   int half_pel;
00802   int start_x, start_y, stop_x, stop_y, new_x, new_y, lx;
00803   int min_pos;
00804   int AE, AE_min;
00805   Point search[9];
00806 
00807   start_x = -1;
00808   stop_x = 1;
00809   start_y = -1;
00810   stop_y = 1;
00811 
00812   new_x = x + fr->x;
00813   new_y = y + fr->y;
00814 
00815   new_x += ((comp&1)<<3);
00816   new_y += ((comp&2)<<2);
00817 
00818   lx = ((VidSt->mv_outside_frame) ? (VidSt->pels) + ((VidSt->long_vectors)?64:32) : (VidSt->pels));
00819 
00820   /* Make sure that no addressing is outside the frame */
00821   if (!(VidSt->mv_outside_frame)) {
00822     if ((new_x) <= 0) 
00823       start_x = 0;
00824     if ((new_y) <= 0) 
00825       start_y = 0;
00826     if ((new_x) >= ((VidSt->pels)-bs)) 
00827       stop_x = 0;
00828     if ((new_y) >= ((VidSt->lines)-bs)) 
00829       stop_y = 0;
00830   }
00831 
00832   search[0].x = 0;        search[0].y = 0;
00833   search[1].x = start_x;        search[1].y = start_y; /*   1 2 3   */
00834   search[2].x = 0;        search[2].y = start_y; /*   4 0 5   */
00835   search[3].x = stop_x;        search[3].y = start_y; /*   6 7 8   */
00836   search[4].x = start_x;        search[4].y = 0;
00837   search[5].x = stop_x;        search[5].y = 0;
00838   search[6].x = start_x;        search[6].y = stop_y;
00839   search[7].x = 0;        search[7].y = stop_y;
00840   search[8].x = stop_x;        search[8].y = stop_y;
00841 
00842   AE_min = INT_MAX;
00843   min_pos = 0;
00844   for (i = 0; i < 9; i++) {
00845     AE = 0;
00846     for (n = 0; n < bs; n++) {
00847       for (m = 0; m < bs; m++) {
00848         /* Find absolute error */
00849         half_pel = *(prev + 2*new_x + 2*m + search[i].x +
00850              (2*new_y + 2*n + search[i].y)*lx*2);
00851         AE += abs(half_pel - *(curr + m + n*16));
00852       }
00853     }
00854     /*
00855      * if (i == 0 && fr->x == 0 && fr->y == 0 && bs == 16) 
00856      * AE -= PREF_NULL_VEC;
00857      */
00858     if (AE < AE_min) {
00859       AE_min = AE;
00860       min_pos = i;
00861     }
00862   }
00863 
00864   /* Store optimal values */
00865   fr->min_error = AE_min;
00866   fr->x_half = search[min_pos].x;
00867   fr->y_half = search[min_pos].y;
00868         
00869   return;
00870 }

void FindMB int  x,
int  y,
unsigned char *  image,
int  MB[16][16]
 

Definition at line 490 of file vid_mot_est.c.

References video_codec::pels, and VidSt.

Referenced by MotionEstimatePicture(), Predict_B(), and Predict_P().

00492 {
00493   int n;
00494   register int m;
00495 
00496   for (n = 0; n < MB_SIZE; n++)
00497     for (m = 0; m < MB_SIZE; m++)
00498       MB[n][m] = *(image + x+m + (y+n)*(VidSt->pels));
00499 }

void FindPMV MotionVector MV[5][MBR+1][MBC+2],
int  x,
int  y,
int *  p0,
int *  p1,
int  block,
int  newgob,
int  half_pel
 

Referenced by Count_sac_BitsVectors(), CountBitsVectors(), and MotionEstimatePicture().

void FindPred int  x,
int  y,
MotionVector fr,
unsigned char *  prev,
int *  pred,
int  bs,
int  comp
 

Definition at line 888 of file vid_pred.c.

References video_codec::long_vectors, MotionVector, video_codec::mv_outside_frame, video_codec::pels, VidSt, motionvector::x, motionvector::x_half, motionvector::y, and motionvector::y_half.

Referenced by Predict_P().

00890 {
00891   int m, n;
00892   int new_x, new_y;
00893   int lx;
00894 
00895   lx = ((VidSt->mv_outside_frame) ? (VidSt->pels) + ((VidSt->long_vectors)?64:32) : (VidSt->pels));
00896 
00897   new_x = x + fr->x;
00898   new_y = y + fr->y;
00899 
00900   new_x += ((comp&1)<<3);
00901   new_y += ((comp&2)<<2);
00902 
00903 
00904   /* Fill pred. data */
00905   for (n = 0; n < bs; n++) {
00906     for (m = 0; m < bs; m++) {
00907       /* Find interpolated pixel-value */
00908       *(pred + m + n*16) = *(prev + (new_x + m)*2 + fr->x_half +
00909              ((new_y + n)*2 + fr->y_half)*lx*2);
00910     }   
00911   }
00912   return;
00913 }

void FindPredOBMC int  x,
int  y,
MotionVector MV[5][MBR+1][MBC+2],
unsigned char *  prev,
int *  pred,
int  comp,
int  PB
 

Referenced by MB_Recon_P(), and Predict_P().

void FreeImage PictImage image  ) 
 

Definition at line 232 of file vid_io.c.

References pict_image::Cb, pict_image::Cr, pict_image::lum, and PictImage.

Referenced by close_video_codec(), code_video(), and CodeOneOrTwo().

00234 {
00235   free(image->lum);
00236   free(image->Cr);
00237   free(image->Cb);
00238   free(image);
00239 }

void Help  ) 
 

int idct int *  coeff,
int *  block
 

Referenced by MB_Decode().

void idctref int *  coeff,
int *  block
 

Definition at line 313 of file vid_dct.c.

References c, and zigzag.

Referenced by MB_Decode().

00314 {
00315   int i, j, k, v;
00316   double partial_product;
00317   double tmp[64];
00318   int tmp2[64];
00319   extern int zigzag[8][8];
00320 
00321   for (i=0; i<8; i++)
00322     for (j=0; j<8; j++)
00323       tmp2[j+i*8] = *(coeff + zigzag[i][j]);
00324 
00325   for (i=0; i<8; i++)
00326     for (j=0; j<8; j++)
00327     {
00328       partial_product = 0.0;
00329 
00330       for (k=0; k<8; k++)
00331         partial_product+= c[k][j]*tmp2[8*i+k];
00332 
00333       tmp[8*i+j] = partial_product;
00334     }
00335 
00336   /* Transpose operation is integrated into address mapping by switching 
00337      loop order of i and j */
00338 
00339   for (j=0; j<8; j++)
00340     for (i=0; i<8; i++)
00341     {
00342       partial_product = 0.0;
00343 
00344       for (k=0; k<8; k++)
00345         partial_product+= c[k][i]*tmp[8*k+j];
00346 
00347       v = (int)floor(partial_product+0.5);
00348       block[8*i+j] = (v<-256) ? -256 : ((v>255) ? 255 : v);
00349     }
00350 
00351 
00352 }

int indexfn int  value,
int  table[],
int  max
 

Definition at line 233 of file vid_sac.c.

Referenced by Code_sac_Coeff(), Count_sac_BitsMB(), and Count_sac_BitsVectors().

00234 {
00235   int n=0;
00236 
00237   while(1) {
00238     if (table[n++]==value) return n-1;
00239     if (n>max) return -1;
00240   }
00241 
00242 }

void init_idctref void   ) 
 

Definition at line 298 of file vid_dct.c.

References c, and PI.

Referenced by new_video_codec().

00299 {
00300   int freq, time;
00301   double scale;
00302 
00303   for (freq=0; freq < 8; freq++)
00304   {
00305     scale = (freq == 0) ? sqrt(0.125) : 0.5;
00306     for (time=0; time<8; time++)
00307       c[freq][time] = scale*cos((PI/8.0)*freq*(time + 0.5));
00308   }
00309 }

void initbits  ) 
 

Definition at line 64 of file vid_putbits.c.

References video_codec::bytecnt, video_codec::outcnt, and VidSt.

Referenced by code_video().

00065 {
00066   (VidSt->outcnt) = 8;
00067   (VidSt->bytecnt) = 0;
00068 }

int InitializeQuantizer int  pict_type,
float  bit_rate,
float  target_frame_rate,
float  QP_mean
 

Definition at line 95 of file vid_ratectrl.c.

References video_codec::B_prev, video_codec::B_target, video_codec::global_adj, mmax, mmin, and VidSt.

Referenced by CodeOneOrTwo().

00100                                                                 :  */
00101 /* int bitcount;                                               */
00102 /* AddBitsPicture(bits);                                       */
00103 /* bitcount = bits->total;                                     */
00104 
00105 {
00106   int newQP;
00107 
00108   if (pict_type == PCT_INTER) {
00109 
00110     (VidSt->B_target) = bit_rate / target_frame_rate;
00111 
00112     /* compute picture buffer descrepency as of the previous picture */
00113 
00114     if ((VidSt->B_prev) != 0.0) {
00115       (VidSt->global_adj) = ((VidSt->B_prev) - (VidSt->B_target)) / (2*(VidSt->B_target));
00116     }
00117     else {
00118       (VidSt->global_adj) = (float)0.0;
00119     }
00120     newQP = (int)(QP_mean * (1 + (VidSt->global_adj)) + (float)0.5);
00121     newQP = mmax(1,mmin(31,newQP));  
00122   }
00123   else if (pict_type == PCT_INTRA) {
00124     fprintf(stderr,"No need to call InititializeQuantizer() for Intra picture\n");
00125     exit(-1);
00126   }
00127   else  {
00128     fprintf(stderr,"Error (InitializePictureRate): picture type unkown.\n");
00129     exit(-1);
00130   }  
00131 #if 1
00132   printf("Global adj = %.2f\n", (VidSt->global_adj));
00133   printf("meanQP = %.2f   newQP = %d\n", QP_mean, newQP);
00134 #endif
00135   fprintf(stdout,"Target no. of bits: %.2f\n", (VidSt->B_target));
00136 
00137   return newQP;
00138 }

void InitializeRateControl  ) 
 

Definition at line 85 of file vid_ratectrl.c.

References video_codec::B_prev, and VidSt.

Referenced by code_video().

00086 {
00087   (VidSt->B_prev) = (float)0.0;
00088 }

PictImage* InitImage int  size  ) 
 

Definition at line 191 of file vid_io.c.

References PictImage.

Referenced by code_video(), CodeOneIntra(), CodeOneOrTwo(), and FillImage().

00192 {
00193   PictImage *new;
00194 
00195   if ((new = (PictImage *)malloc(sizeof(PictImage))) == NULL) {
00196     fprintf(stderr,"Couldn't allocate (PictImage *)\n");
00197     exit(-1);
00198   }
00199   if ((new->lum = (unsigned char *)malloc(sizeof(char)*size)) 
00200       == NULL) {
00201     fprintf(stderr,"Couldn't allocate memory for luminance\n");
00202     exit(-1);
00203   }
00204   if ((new->Cr = (unsigned char *)malloc(sizeof(char)*size/4)) 
00205       == NULL) {
00206     fprintf(stderr,"Couldn't allocate memory for Cr\n");
00207     exit(-1);
00208   }
00209   if ((new->Cb = (unsigned char *)malloc(sizeof(char)*size/4)) 
00210       == NULL) {
00211     fprintf(stderr,"Couldn't allocate memory for Cb\n");
00212     exit(-1);
00213   }
00214 
00215   return new;
00216 }

int* InterleaveCoeff int *  qp,
int *  qb,
int  bs
 

unsigned char* InterpolateImage unsigned char *  image,
int  w,
int  h
 

Definition at line 788 of file vid_coder.c.

Referenced by CodeOneOrTwo().

00789 {
00790   unsigned char *ipol_image, *ii, *oo;
00791   int i,j;
00792 
00793   ipol_image = (unsigned char *)malloc(sizeof(char)*width*height*4);
00794   ii = ipol_image;
00795   oo = image;
00796 
00797   /* main image */
00798   for (j = 0; j < height-1; j++) {
00799     for (i = 0; i  < width-1; i++) {
00800       *(ii + (i<<1)) = *(oo + i);
00801       *(ii + (i<<1)+1) = (*(oo + i) + *(oo + i + 1) + 1)>>1;
00802       *(ii + (i<<1)+(width<<1)) = (*(oo + i) + *(oo + i + width) + 1)>>1;
00803       *(ii + (i<<1)+1+(width<<1)) = (*(oo+i) + *(oo+i+1) + 
00804          *(oo+i+width) + *(oo+i+1+width) + 2)>>2;
00805     }
00806     /* last pels on each line */
00807     *(ii+ (width<<1) - 2) = *(oo + width - 1);
00808     *(ii+ (width<<1) - 1) = *(oo + width - 1);
00809     *(ii+ (width<<1)+ (width<<1)-2) = (*(oo+width-1)+*(oo+width+width-1)+1)>>1;
00810     *(ii+ (width<<1)+ (width<<1)-1) = (*(oo+width-1)+*(oo+width+width-1)+1)>>1;
00811     ii += (width<<2);
00812     oo += width;
00813   }
00814 
00815   /* last lines */
00816   for (i = 0; i < width-1; i++) {
00817     *(ii+ (i<<1)) = *(oo + i);    
00818     *(ii+ (i<<1)+1) = (*(oo + i) + *(oo + i + 1) + 1)>>1;
00819     *(ii+ (width<<1)+ (i<<1)) = *(oo + i);    
00820     *(ii+ (width<<1)+ (i<<1)+1) = (*(oo + i) + *(oo + i + 1) + 1)>>1;
00821           
00822   }
00823 
00824   /* bottom right corner pels */
00825   *(ii + (width<<1) - 2) = *(oo + width -1);
00826   *(ii + (width<<1) - 1) = *(oo + width -1);
00827   *(ii + (width<<2) - 2) = *(oo + width -1);
00828   *(ii + (width<<2) - 1) = *(oo + width -1);
00829 
00830   return ipol_image;
00831 }

unsigned char* LoadArea unsigned char *  im,
int  x,
int  y,
int  x_size,
int  y_size,
int  lx
 

Definition at line 330 of file vid_mot_est.c.

Referenced by MotionEstimation().

00332 {
00333   unsigned char *res = (unsigned char *)malloc(sizeof(char)*x_size*y_size);
00334   unsigned char *in;
00335   unsigned char *out;
00336   int i = x_size;
00337   int j = y_size;
00338 
00339   in = im + (y*lx) + x;
00340   out = res;
00341 
00342   while (j--) {
00343     while (i--)
00344       *out++ = *in++;
00345     i = x_size;
00346     in += lx - x_size;
00347   };
00348   return res;
00349 }

void MakeEdgeImage unsigned char *  src,
unsigned char *  dst,
int  width,
int  height,
int  edge
 

Definition at line 997 of file vid_coder.c.

Referenced by CodeOneOrTwo().

00999 {
01000   int i,j;
01001   unsigned char *p1,*p2,*p3,*p4;
01002   unsigned char *o1,*o2,*o3,*o4;
01003 
01004   /* center image */
01005   p1 = dst;
01006   o1 = src;
01007   for (j = 0; j < height;j++) {
01008     memcpy(p1,o1,width);
01009     p1 += width + (edge<<1);
01010     o1 += width;
01011   }
01012 
01013   /* left and right edges */
01014   p1 = dst-1;
01015   o1 = src;
01016   for (j = 0; j < height;j++) {
01017     for (i = 0; i < edge; i++) {
01018       *(p1 - i) = *o1;
01019       *(p1 + width + i + 1) = *(o1 + width - 1);
01020     }
01021     p1 += width + (edge<<1);
01022     o1 += width;
01023   }    
01024     
01025   /* top and bottom edges */
01026   p1 = dst;
01027   p2 = dst + (width + (edge<<1))*(height-1);
01028   o1 = src;
01029   o2 = src + width*(height-1);
01030   for (j = 0; j < edge;j++) {
01031     p1 = p1 - (width + (edge<<1));
01032     p2 = p2 + (width + (edge<<1));
01033     for (i = 0; i < width; i++) {
01034       *(p1 + i) = *(o1 + i);
01035       *(p2 + i) = *(o2 + i);
01036     }
01037   }    
01038 
01039   /* corners */
01040   p1 = dst - (width+(edge<<1)) - 1;
01041   p2 = p1 + width + 1;
01042   p3 = dst + (width+(edge<<1))*(height)-1;
01043   p4 = p3 + width + 1;
01044 
01045   o1 = src;
01046   o2 = o1 + width - 1;
01047   o3 = src + width*(height-1);
01048   o4 = o3 + width - 1;
01049   for (j = 0; j < edge; j++) {
01050     for (i = 0; i < edge; i++) {
01051       *(p1 - i) = *o1;
01052       *(p2 + i) = *o2;
01053       *(p3 - i) = *o3;
01054       *(p4 + i) = *o4; 
01055     }
01056     p1 = p1 - (width + (edge<<1));
01057     p2 = p2 - (width + (edge<<1));
01058     p3 = p3 + width + (edge<<1);
01059     p4 = p4 + width + (edge<<1);
01060   }
01061 }

void MarkVec MotionVector MV  ) 
 

Definition at line 1118 of file vid_countbit.c.

References MotionVector, motionvector::x, motionvector::x_half, motionvector::y, and motionvector::y_half.

Referenced by CodeOneOrTwo().

01119 {
01120   MV->x = NO_VEC;
01121   MV->y = NO_VEC;
01122   MV->x_half = 0;
01123   MV->y_half = 0;
01124   return;
01125 }

int MB_Decode int *  qcoeff,
MB_Structure mb_recon,
int  QP,
int  I
 

Definition at line 566 of file vid_coder.c.

References mb_structure::Cb, mb_structure::Cr, Dequant(), idct(), idctref(), mb_structure::lum, and MB_Structure.

Referenced by CodeOneIntra(), and CodeOneOrTwo().

00567 {
00568   int   i, j, k, l, row, col;
00569   int   *iblock;
00570   int   *qcoeff_ind;
00571   int   *rcoeff, *rcoeff_ind;
00572 
00573   if ((iblock = (int *)malloc(sizeof(int)*64)) == NULL) {
00574     fprintf(stderr,"MB_Coder: Could not allocate space for iblock\n");
00575     exit(-1);
00576   }
00577   if ((rcoeff = (int *)malloc(sizeof(int)*384)) == NULL) {
00578     fprintf(stderr,"MB_Coder: Could not allocate space for rcoeff\n");
00579     exit(-1);
00580   }  
00581 
00582   /* For control purposes */
00583   /* Zero data */
00584   for (i = 0; i < 16; i++)
00585     for (j = 0; j < 16; j++)
00586       mb_recon->lum[j][i] = 0;
00587   for (i = 0; i < 8; i++) 
00588     for (j = 0; j < 8; j++) {
00589       mb_recon->Cb[j][i] = 0;
00590       mb_recon->Cr[j][i] = 0;
00591     }
00592 
00593   qcoeff_ind = qcoeff;
00594   rcoeff_ind = rcoeff; 
00595 
00596   for (k=0;k<16;k+=8) {
00597     for (l=0;l<16;l+=8) {
00598       Dequant(qcoeff_ind,rcoeff_ind,QP,I);
00599 #ifndef FASTIDCT
00600       idctref(rcoeff_ind,iblock); 
00601 #else
00602       idct(rcoeff_ind,iblock); 
00603 #endif
00604       qcoeff_ind += 64;
00605       rcoeff_ind += 64;
00606       for (i=k,row=0;row<64;i++,row+=8) {
00607         for (j=l,col=0;col<8;j++,col++) {
00608           mb_recon->lum[i][j] = *(iblock+row+col);
00609         }
00610       }
00611     }
00612   }
00613   Dequant(qcoeff_ind,rcoeff_ind,QP,I);
00614 #ifndef FASTIDCT
00615   idctref(rcoeff_ind,iblock); 
00616 #else
00617   idct(rcoeff_ind,iblock); 
00618 #endif
00619   qcoeff_ind += 64;
00620   rcoeff_ind += 64;
00621   for (i=0;i<8;i++) {
00622     for (j=0;j<8;j++) {
00623       mb_recon->Cb[i][j] = *(iblock+i*8+j);
00624     }
00625   }
00626   Dequant(qcoeff_ind,rcoeff_ind,QP,I);
00627 #ifndef FASTIDCT
00628   idctref(rcoeff_ind,iblock); 
00629 #else
00630   idct(rcoeff_ind,iblock); 
00631 #endif
00632   for (i=0;i<8;i++) {
00633     for (j=0;j<8;j++) {
00634       mb_recon->Cr[i][j] = *(iblock+i*8+j);
00635     }
00636   }
00637   free(iblock);
00638   free(rcoeff);
00639   return 0;
00640 }

int* MB_Encode MB_Structure mb_orig,
int  QP,
int  I
 

Definition at line 501 of file vid_coder.c.

References mb_structure::Cb, mb_structure::Cr, Dct(), mb_structure::lum, MB_Structure, and Quant().

Referenced by CodeOneIntra(), and CodeOneOrTwo().

00502 {
00503   int        i, j, k, l, row, col;
00504   int        fblock[64];
00505   int        coeff[384];
00506   int        *coeff_ind;
00507   int                *qcoeff;
00508   int        *qcoeff_ind;
00509 
00510   if ((qcoeff=(int *)malloc(sizeof(int)*384)) == 0) {
00511     fprintf(stderr,"mb_encode(): Couldn't allocate qcoeff.\n");
00512     exit(-1);
00513   }
00514 
00515   coeff_ind = coeff;
00516   qcoeff_ind = qcoeff;
00517   for (k=0;k<16;k+=8) {
00518     for (l=0;l<16;l+=8) {
00519       for (i=k,row=0;row<64;i++,row+=8) {
00520         for (j=l,col=0;col<8;j++,col++) {
00521           *(fblock+row+col) = mb_orig->lum[i][j];
00522         }
00523       }
00524       Dct(fblock,coeff_ind);
00525       Quant(coeff_ind,qcoeff_ind,QP,I);
00526       coeff_ind += 64;
00527       qcoeff_ind += 64;
00528     }
00529   }
00530   for (i=0;i<8;i++) {
00531     for (j=0;j<8;j++) {
00532       *(fblock+i*8+j) = mb_orig->Cb[i][j];
00533     }
00534   }
00535   Dct(fblock,coeff_ind);
00536   Quant(coeff_ind,qcoeff_ind,QP,I); 
00537   coeff_ind += 64;
00538   qcoeff_ind += 64;
00539 
00540   for (i=0;i<8;i++) {
00541     for (j=0;j<8;j++) {
00542       *(fblock+i*8+j) = mb_orig->Cr[i][j];
00543     }
00544   }
00545   Dct(fblock,coeff_ind);
00546   Quant(coeff_ind,qcoeff_ind,QP,I); 
00547   
00548   return qcoeff;
00549 }

MB_Structure* MB_Recon PictImage prev_recon,
MB_Structure diff,
int  x_curr,
int  y_curr,
MotionVector MV
 

MB_Structure* MB_Recon_B PictImage prev,
MB_Structure diff,
unsigned char *  prev_ipol,
int  x_curr,
int  y_curr,
MotionVector MV[5][MBR+1][MBC+2],
MB_Structure recon_P,
int  TR,
int  TRB
 

Definition at line 360 of file vid_pred.c.

References mb_structure::Cb, mb_structure::Cr, FindBiDirChrPredPB(), FindBiDirLumPredPB(), FindChromBlock_P(), FindForwLumPredPB(), mb_structure::lum, MB_SIZE, MB_Structure, motionvector::Mode, MotionVector, PictImage, roundtab, sign, motionvector::x, motionvector::x_half, motionvector::y, and motionvector::y_half.

Referenced by CodeOneOrTwo().

00364 {
00365   int i,j,k;
00366   int dx, dy, bdx, bdy, mvx, mvy, xvec, yvec;
00367   MB_Structure *recon_B = (MB_Structure *)malloc(sizeof(MB_Structure));
00368   MB_Structure *pred = (MB_Structure *)malloc(sizeof(MB_Structure));
00369   MotionVector *f[5];
00370 
00371   for (k = 0; k <= 4; k++)
00372     f[k] = MV[k][y/MB_SIZE+1][x/MB_SIZE+1];
00373 
00374   bdx = MV[5][y/MB_SIZE+1][x/MB_SIZE+1]->x;
00375   bdy = MV[5][y/MB_SIZE+1][x/MB_SIZE+1]->y;
00376 
00377   if (f[0]->Mode == MODE_INTER4V) {  /* Mode INTER4V */
00378     /* Find forward prediction */
00379 
00380     /* Luma */
00381     FindForwLumPredPB(prev_ipol,x,y,f[1],&pred->lum[0][0],TRD,TRB,bdx,bdy,8,0);
00382     FindForwLumPredPB(prev_ipol,x,y,f[2],&pred->lum[0][8],TRD,TRB,bdx,bdy,8,1);
00383     FindForwLumPredPB(prev_ipol,x,y,f[3],&pred->lum[8][0],TRD,TRB,bdx,bdy,8,2);
00384     FindForwLumPredPB(prev_ipol,x,y,f[4],&pred->lum[8][8],TRD,TRB,bdx,bdy,8,3);
00385 
00386     /* chroma vectors are sum of B luma vectors divided and rounded */
00387     xvec = yvec = 0;
00388     for (k = 1; k <= 4; k++) {
00389       xvec += TRB*(2*f[k]->x + f[k]->x_half)/TRD + bdx;
00390       yvec += TRB*(2*f[k]->y + f[k]->y_half)/TRD + bdy;
00391     }
00392 
00393     /* round values according to TABLE 16/H.263 */
00394     dx = sign(xvec)*(roundtab[abs(xvec)%16] + (abs(xvec)/16)*2);
00395     dy = sign(yvec)*(roundtab[abs(yvec)%16] + (abs(yvec)/16)*2);
00396 
00397     FindChromBlock_P(x, y, dx, dy, prev_image, pred);
00398 
00399     /* Find bidirectional prediction */
00400     FindBiDirLumPredPB(&recon_P->lum[0][0], f[1], &pred->lum[0][0], 
00401                TRD, TRB, bdx, bdy, 0, 0);
00402     FindBiDirLumPredPB(&recon_P->lum[0][8], f[2], &pred->lum[0][8], 
00403                TRD, TRB, bdx, bdy, 1, 0);
00404     FindBiDirLumPredPB(&recon_P->lum[8][0], f[3], &pred->lum[8][0], 
00405                TRD, TRB, bdx, bdy, 0, 1);
00406     FindBiDirLumPredPB(&recon_P->lum[8][8], f[4], &pred->lum[8][8], 
00407                TRD, TRB, bdx, bdy, 1, 1);
00408 
00409     /* chroma vectors are sum of B luma vectors divided and rounded */
00410     xvec = yvec = 0;
00411     for (k = 1; k <= 4; k++) {
00412       mvx = 2*f[k]->x + f[k]->x_half;
00413       mvy = 2*f[k]->y + f[k]->y_half;
00414       xvec += bdx == 0 ? (TRB-TRD) *  mvx / TRD : TRB * mvx / TRD + bdx - mvx;
00415       yvec += bdy == 0 ? (TRB-TRD) *  mvy / TRD : TRB * mvy / TRD + bdy - mvy;
00416     }
00417 
00418     /* round values according to TABLE 16/H.263 */
00419     dx = sign(xvec)*(roundtab[abs(xvec)%16] + (abs(xvec)/16)*2);
00420     dy = sign(yvec)*(roundtab[abs(yvec)%16] + (abs(yvec)/16)*2);
00421 
00422     FindBiDirChrPredPB(recon_P, dx, dy, pred); 
00423 
00424   }
00425   else {  /* Mode INTER or INTER_Q */
00426     /* Find forward prediction */
00427     
00428     FindForwLumPredPB(prev_ipol,x,y,f[0],&pred->lum[0][0],TRD,TRB,
00429               bdx,bdy,16,0);
00430 
00431     xvec = 4 * (TRB*(2*f[0]->x + f[0]->x_half) / TRD + bdx);
00432     yvec = 4 * (TRB*(2*f[0]->y + f[0]->y_half) / TRD + bdy);
00433     /* round values according to TABLE 16/H.263 */
00434     dx = sign(xvec)*(roundtab[abs(xvec)%16] + (abs(xvec)/16)*2);
00435     dy = sign(yvec)*(roundtab[abs(yvec)%16] + (abs(yvec)/16)*2);
00436 
00437     FindChromBlock_P(x, y, dx, dy, prev_image, pred);
00438 
00439     /* Find bidirectional prediction */
00440     FindBiDirLumPredPB(&recon_P->lum[0][0], f[0], &pred->lum[0][0], 
00441                TRD, TRB, bdx, bdy, 0, 0);
00442     FindBiDirLumPredPB(&recon_P->lum[0][8], f[0], &pred->lum[0][8], 
00443                TRD, TRB, bdx, bdy, 1, 0);
00444     FindBiDirLumPredPB(&recon_P->lum[8][0], f[0], &pred->lum[8][0], 
00445                TRD, TRB, bdx, bdy, 0, 1);
00446     FindBiDirLumPredPB(&recon_P->lum[8][8], f[0], &pred->lum[8][8], 
00447                TRD, TRB, bdx, bdy, 1, 1);
00448 
00449     /* chroma vectors */
00450     mvx = 2*f[0]->x + f[0]->x_half;
00451     xvec = bdx == 0 ? (TRB-TRD) * mvx / TRD : TRB * mvx / TRD + bdx - mvx;
00452     xvec *= 4;
00453 
00454     mvy = 2*f[0]->y + f[0]->y_half;
00455     yvec = bdy == 0 ? (TRB-TRD) * mvy / TRD : TRB * mvy / TRD + bdy - mvy;
00456     yvec *= 4;
00457       
00458     /* round values according to TABLE 16/H.263 */
00459     dx = sign(xvec)*(roundtab[abs(xvec)%16] + (abs(xvec)/16)*2);
00460     dy = sign(yvec)*(roundtab[abs(yvec)%16] + (abs(yvec)/16)*2);
00461 
00462     FindBiDirChrPredPB(recon_P, dx, dy, pred); 
00463 
00464   }
00465 
00466   /* Reconstruction */
00467   for (j = 0; j < MB_SIZE; j++) 
00468     for (i = 0; i < MB_SIZE; i++) 
00469       recon_B->lum[j][i] = pred->lum[j][i] + diff->lum[j][i];
00470         
00471   for (j = 0; j < MB_SIZE>>1; j++) 
00472     for (i = 0; i < MB_SIZE>>1; i++) {
00473       recon_B->Cr[j][i] = pred->Cr[j][i] + diff->Cr[j][i];
00474       recon_B->Cb[j][i] = pred->Cb[j][i] + diff->Cb[j][i];
00475     }
00476   
00477   free(pred);
00478   return recon_B;
00479 }

MB_Structure* MB_Recon_P PictImage prev_image,
unsigned char *  prev_ipol,
MB_Structure diff,
int  x_curr,
int  y_curr,
MotionVector MV[5][MBR+1][MBC+2],
int  PB
 

Referenced by CodeOneOrTwo().

int ModifyMode int  Mode,
int  dquant
 

Definition at line 1535 of file vid_pred.c.

Referenced by CodeOneOrTwo().

01536 {
01537 
01538   if (Mode == MODE_INTRA) {
01539     if(dquant!=0)
01540       return MODE_INTRA_Q;
01541     else
01542       return MODE_INTRA;
01543   }
01544   else{ 
01545     if(dquant!=0)
01546       return MODE_INTER_Q;
01547     else
01548       return Mode;
01549   }
01550 }

void MotionEstimatePicture unsigned char *  curr,
unsigned char *  prev,
unsigned char *  prev_ipol,
int  seek_dist,
MotionVector MV[5][MBR+1][MBC+2],
int  gobsync
 

Referenced by CodeOneOrTwo().

void MotionEstimation unsigned char *  curr,
unsigned char *  prev,
int  x_curr,
int  y_curr,
int  xoff,
int  yoff,
int  seek_dist,
MotionVector MV[5][MBR+1][MBC+2],
int *  sad_0
 

Referenced by MotionEstimatePicture(), and NextTwoPB().

int NextTwoPB PictImage p2,
PictImage bim,
PictImage p1,
int  bskip,
int  pskip,
int  seek_dist
 

Definition at line 75 of file vid_main.c.

References video_codec::advanced, video_codec::lines, video_codec::long_vectors, pict_image::lum, MB_SIZE, MBC, MBR, motionvector::min_error, MotionEstimation(), MotionVector, video_codec::mv_outside_frame, video_codec::pels, PictImage, SAD_MB_Bidir(), VidSt, motionvector::x, and motionvector::y.

Referenced by code_video().

00077 {
00078   int adv_is_on = 0, mof_is_on = 0, lv_is_on = 0;
00079   int psad1, psad2, bsad, psad;
00080   MotionVector *MV[6][MBR+1][MBC+2];
00081   MotionVector *mvp, *mvbf, *mvbb;
00082   int x,y;
00083   int i,j,k,tmp;
00084 
00085   /* Temporarily disable some options to simplify motion estimation */
00086   if ((VidSt->advanced)) {
00087     (VidSt->advanced) = OFF;
00088     adv_is_on = ON;
00089   }
00090   if ((VidSt->mv_outside_frame)) {
00091     (VidSt->mv_outside_frame) = OFF;
00092     mof_is_on = ON;
00093   }
00094   if ((VidSt->long_vectors)) {
00095     (VidSt->long_vectors) = OFF;
00096     lv_is_on = ON;
00097   }
00098 
00099   for (j = 1; j <= ((VidSt->lines)>>4); j++) 
00100     for (i = 1; i <= ((VidSt->pels)>>4); i++) 
00101       for (k = 0; k < 3; k++) {
00102         MV[k][j][i] = (MotionVector *)calloc(1,sizeof(MotionVector));
00103         /* calloc to avoid Checker warnings about reading of
00104            unitizalized memory in the memcpy's below */
00105       }
00106 
00107   mvbf = (MotionVector *)malloc(sizeof(MotionVector));
00108   mvbb = (MotionVector *)malloc(sizeof(MotionVector));
00109 
00110   psad = 0;
00111   psad1 = 0;
00112   psad2 = 0;
00113   bsad = 0;
00114 
00115   /* Integer motion estimation */
00116   for ( j = 1; j < (VidSt->lines)/MB_SIZE - 1; j++) {
00117     for ( i = 1; i < (VidSt->pels)/MB_SIZE - 1 ; i++) {
00118       x = i*MB_SIZE;
00119       y = j*MB_SIZE;
00120 
00121       /* picture order: prev -> next1 -> next2 */
00122       /* next1 and next2 can be coded as PB or PP */
00123       /* prev is the previous encoded picture */
00124 
00125       /* computes vectors (prev <- next2) */
00126       MotionEstimation(next2->lum,prev->lum,x,y,0,0,seek_dist,MV,&tmp);
00127       if (MV[0][j+1][i+1]->x == 0 && MV[0][j+1][i+1]->y == 0)
00128         MV[0][j+1][i+1]->min_error += PREF_NULL_VEC;
00129       /* not necessary to prefer zero vector here */
00130       memcpy(MV[2][j+1][i+1],MV[0][j+1][i+1],sizeof(MotionVector));
00131 
00132       /* computes sad(prev <- next1) */
00133       MotionEstimation(next1->lum,prev->lum,x,y,0,0,seek_dist,MV,&tmp);
00134       if (MV[0][j+1][i+1]->x == 0 && MV[0][j+1][i+1]->y == 0)
00135         MV[0][j+1][i+1]->min_error += PREF_NULL_VEC;
00136       memcpy(MV[1][j+1][i+1],MV[0][j+1][i+1],sizeof(MotionVector));
00137 
00138       /* computes vectors for (next1 <- next2) */
00139       MotionEstimation(next2->lum,next1->lum,x,y,0,0,seek_dist,MV,&tmp);
00140       if (MV[0][j+1][i+1]->x == 0 && MV[0][j+1][i+1]->y == 0)
00141         MV[0][j+1][i+1]->min_error += PREF_NULL_VEC;
00142 
00143       /* scales vectors for (prev <- next2 ) */
00144       mvp = MV[2][j+1][i+1];
00145       mvbf->x =   bskip * mvp->x / (bskip + pskip);
00146       mvbb->x = - pskip * mvp->x / (bskip + pskip);
00147       mvbf->y =   bskip * mvp->y / (bskip + pskip);
00148       mvbb->y = - pskip * mvp->y / (bskip + pskip);
00149 
00150       psad1 += MV[0][j+1][i+1]->min_error;
00151       psad2 += MV[1][j+1][i+1]->min_error;
00152       psad +=  mvp->min_error;
00153 
00154       /* computes sad(prev <- next1 -> next2) */
00155       bsad += SAD_MB_Bidir(next1->lum + x + y*(VidSt->pels),
00156            next2->lum + x + mvbb->x + (y + mvbb->y)*(VidSt->pels),
00157            prev->lum + x + mvbf->x + (y + mvbf->y)*(VidSt->pels),
00158            (VidSt->pels), INT_MAX);
00159     }
00160   }
00161 
00162   for (j = 1; j <= ((VidSt->lines)>>4); j++) 
00163     for (i = 1; i <= ((VidSt->pels)>>4); i++) 
00164       for (k = 0; k < 3; k++) 
00165         free(MV[k][j][i]);
00166   free(mvbf);
00167   free(mvbb);
00168 
00169   /* restore advanced parameters */
00170   (VidSt->advanced) = adv_is_on;
00171   (VidSt->mv_outside_frame) = mof_is_on;
00172   (VidSt->long_vectors) = lv_is_on;
00173 
00174   /* do the decision */
00175   if (bsad < (psad1+psad2)/2)
00176     fprintf(stdout,"Chose PB - bsad %d, psad %d\n", 
00177             bsad, (psad1+psad2)/2);
00178   else
00179     fprintf(stdout,"Chose PP  - bsad %d, psad %d\n", 
00180             bsad, (psad1+psad2)/2);
00181 
00182   if (bsad < (psad1 + psad2)/2)
00183     return 1;
00184   else 
00185     return 0;
00186 }

MB_Structure* Predict PictImage curr,
PictImage prev_recon,
int  x_curr,
int  y_curr,
MotionVector MV
 

MB_Structure* Predict_B PictImage curr_image,
PictImage prev_image,
unsigned char *  prev_ipol,
int  x_curr,
int  y_curr,
MotionVector fr[5][MBR+1][MBC+2],
MB_Structure recon_P,
int  TR,
int  TRB
 

Definition at line 156 of file vid_pred.c.

References pict_image::Cb, mb_structure::Cb, video_codec::cpels, pict_image::Cr, mb_structure::Cr, FindBiDirChrPredPB(), FindBiDirLumPredPB(), FindChromBlock_P(), FindForwLumPredPB(), FindMB(), video_codec::lines, mb_structure::lum, pict_image::lum, MB_SIZE, MB_Structure, motionvector::Mode, MODE_INTRA, MotionVector, video_codec::mv_outside_frame, video_codec::pels, PictImage, roundtab, SAD_MB_integer(), sign, VidSt, motionvector::x, motionvector::x_half, motionvector::y, and motionvector::y_half.

Referenced by CodeOneOrTwo().

00160 {
00161   int i,j,k;
00162   int dx, dy, sad, sad_min=INT_MAX, curr[16][16], bdx=0, bdy=0;
00163   MB_Structure *p_err = (MB_Structure *)malloc(sizeof(MB_Structure));
00164   MB_Structure *pred = (MB_Structure *)malloc(sizeof(MB_Structure));
00165   MotionVector *f[5];
00166   int xvec, yvec, mvx, mvy;
00167 
00168   for (k = 0; k <= 4; k++)
00169     f[k] = MV[k][y/MB_SIZE+1][x/MB_SIZE+1];
00170 
00171   /* Find MB in current image */
00172   FindMB(x, y, curr_image->lum, curr);
00173 
00174   if (f[0]->Mode == MODE_INTER4V) {  /* Mode INTER4V */
00175     /* Find forward prediction */
00176 
00177     /* Luma */
00178     for (j = -DEF_PBDELTA_WIN; j <= DEF_PBDELTA_WIN; j++) {
00179       for (i = -DEF_PBDELTA_WIN; i <= DEF_PBDELTA_WIN; i++) {
00180 
00181         FindForwLumPredPB(prev_ipol, x, y, f[1], &pred->lum[0][0], 
00182           TRD, TRB, i, j, 8, 0);
00183         FindForwLumPredPB(prev_ipol, x, y, f[2], &pred->lum[0][8], 
00184           TRD, TRB, i, j, 8, 1);
00185         FindForwLumPredPB(prev_ipol, x, y, f[3], &pred->lum[8][0], 
00186           TRD, TRB, i, j, 8, 2);
00187         FindForwLumPredPB(prev_ipol, x, y, f[4], &pred->lum[8][8], 
00188           TRD, TRB, i, j, 8, 3);
00189 
00190         sad = SAD_MB_integer(&curr[0][0],&pred->lum[0][0], 16,INT_MAX);
00191         if (i == 0 && j == 0)
00192           sad -= PREF_PBDELTA_NULL_VEC;
00193         if (sad < sad_min) {
00194           sad_min = sad;
00195           bdx = i;
00196           bdy = j;
00197         }
00198       }
00199     }
00200 
00201     FindForwLumPredPB(prev_ipol,x,y,f[1],&pred->lum[0][0],TRD,TRB,bdx,bdy,8,0);
00202     FindForwLumPredPB(prev_ipol,x,y,f[2],&pred->lum[0][8],TRD,TRB,bdx,bdy,8,1);
00203     FindForwLumPredPB(prev_ipol,x,y,f[3],&pred->lum[8][0],TRD,TRB,bdx,bdy,8,2);
00204     FindForwLumPredPB(prev_ipol,x,y,f[4],&pred->lum[8][8],TRD,TRB,bdx,bdy,8,3);
00205 
00206     /* chroma vectors are sum of B luma vectors divided and rounded */
00207     xvec = yvec = 0;
00208     for (k = 1; k <= 4; k++) {
00209       xvec += TRB*(2*f[k]->x + f[k]->x_half)/TRD + bdx;
00210       yvec += TRB*(2*f[k]->y + f[k]->y_half)/TRD + bdy;
00211     }
00212 
00213     /* round values according to TABLE 16/H.263 */
00214     dx = sign(xvec)*(roundtab[abs(xvec)%16] + (abs(xvec)/16)*2);
00215     dy = sign(yvec)*(roundtab[abs(yvec)%16] + (abs(yvec)/16)*2);
00216 
00217     FindChromBlock_P(x, y, dx, dy, prev_image, pred);
00218 
00219     /* Find bidirectional prediction */
00220     FindBiDirLumPredPB(&recon_P->lum[0][0], f[1], &pred->lum[0][0], 
00221                TRD, TRB, bdx, bdy, 0, 0);
00222     FindBiDirLumPredPB(&recon_P->lum[0][8], f[2], &pred->lum[0][8], 
00223                TRD, TRB, bdx, bdy, 1, 0);
00224     FindBiDirLumPredPB(&recon_P->lum[8][0], f[3], &pred->lum[8][0], 
00225                TRD, TRB, bdx, bdy, 0, 1);
00226     FindBiDirLumPredPB(&recon_P->lum[8][8], f[4], &pred->lum[8][8], 
00227                TRD, TRB, bdx, bdy, 1, 1);
00228 
00229     /* chroma vectors are sum of B luma vectors divided and rounded */
00230     xvec = yvec = 0;
00231     for (k = 1; k <= 4; k++) {
00232       mvx = 2*f[k]->x + f[k]->x_half;
00233       mvy = 2*f[k]->y + f[k]->y_half;
00234       xvec += bdx == 0 ? (TRB-TRD) *  mvx / TRD : TRB * mvx / TRD + bdx - mvx;
00235       yvec += bdy == 0 ? (TRB-TRD) *  mvy / TRD : TRB * mvy / TRD + bdy - mvy;
00236     }
00237 
00238     /* round values according to TABLE 16/H.263 */
00239     dx = sign(xvec)*(roundtab[abs(xvec)%16] + (abs(xvec)/16)*2);
00240     dy = sign(yvec)*(roundtab[abs(yvec)%16] + (abs(yvec)/16)*2);
00241 
00242     FindBiDirChrPredPB(recon_P, dx, dy, pred); 
00243   }
00244 
00245   else {  /* Mode INTER or INTER_Q */
00246     /* Find forward prediction */
00247 
00248     for (j = -DEF_PBDELTA_WIN; j <= DEF_PBDELTA_WIN; j++) {
00249       for (i = -DEF_PBDELTA_WIN; i <= DEF_PBDELTA_WIN; i++) {
00250 
00251         dx = i; dy = j;
00252         /* To keep things simple I turn off PB delta vectors at the edges */
00253         if (!(VidSt->mv_outside_frame)) {
00254           if (x == 0) dx = 0;
00255           if (x == (VidSt->pels) - MB_SIZE) dx = 0;
00256           if (y == 0) dy = 0;
00257           if (y == (VidSt->lines) - MB_SIZE) dy = 0;
00258         }
00259 
00260         if (f[0]->Mode == MODE_INTRA || f[0]->Mode == MODE_INTRA_Q) {
00261           dx = dy = 0;
00262         }
00263 
00264         if (f[0]->x == 0 && f[0]->y == 0 && 
00265             f[0]->x_half == 0 && f[0]->y_half == 0) {
00266           dx = dy = 0;
00267         }
00268 
00269         FindForwLumPredPB(prev_ipol, x, y, f[0], &pred->lum[0][0], 
00270           TRD, TRB, dx, dy, 16, 0);
00271 
00272         sad = SAD_MB_integer(&curr[0][0],&pred->lum[0][0], 16, INT_MAX);
00273         if (i == 0 && j == 0)
00274           sad -= PREF_PBDELTA_NULL_VEC;
00275         if (sad < sad_min) {
00276           sad_min = sad;
00277           bdx = dx;
00278           bdy = dy;
00279         }
00280       }
00281     }
00282     FindForwLumPredPB(prev_ipol,x,y,f[0],&pred->lum[0][0],TRD,TRB,
00283               bdx,bdy,16,0);
00284 
00285     xvec = 4 * (TRB*(2*f[0]->x + f[0]->x_half) / TRD + bdx);
00286     yvec = 4 * (TRB*(2*f[0]->y + f[0]->y_half) / TRD + bdy);
00287     /* round values according to TABLE 16/H.263 */
00288     dx = sign(xvec)*(roundtab[abs(xvec)%16] + (abs(xvec)/16)*2);
00289     dy = sign(yvec)*(roundtab[abs(yvec)%16] + (abs(yvec)/16)*2);
00290 
00291     FindChromBlock_P(x, y, dx, dy, prev_image, pred);
00292 
00293     /* Find bidirectional prediction */
00294     FindBiDirLumPredPB(&recon_P->lum[0][0], f[0], &pred->lum[0][0], 
00295                TRD, TRB, bdx, bdy, 0, 0);
00296     FindBiDirLumPredPB(&recon_P->lum[0][8], f[0], &pred->lum[0][8], 
00297                TRD, TRB, bdx, bdy, 1, 0);
00298     FindBiDirLumPredPB(&recon_P->lum[8][0], f[0], &pred->lum[8][0], 
00299                TRD, TRB, bdx, bdy, 0, 1);
00300     FindBiDirLumPredPB(&recon_P->lum[8][8], f[0], &pred->lum[8][8], 
00301                TRD, TRB, bdx, bdy, 1, 1);
00302 
00303     /* chroma vectors */
00304     mvx = 2*f[0]->x + f[0]->x_half;
00305     xvec = bdx == 0 ? (TRB-TRD) * mvx / TRD : TRB * mvx / TRD + bdx - mvx;
00306     xvec *= 4;
00307 
00308     mvy = 2*f[0]->y + f[0]->y_half;
00309     yvec = bdy == 0 ? (TRB-TRD) * mvy / TRD : TRB * mvy / TRD + bdy - mvy;
00310     yvec *= 4;
00311       
00312     /* round values according to TABLE 16/H.263 */
00313     dx = sign(xvec)*(roundtab[abs(xvec)%16] + (abs(xvec)/16)*2);
00314     dy = sign(yvec)*(roundtab[abs(yvec)%16] + (abs(yvec)/16)*2);
00315 
00316     FindBiDirChrPredPB(recon_P, dx, dy, pred); 
00317   }
00318 
00319   /* store PB-deltas */
00320   MV[5][y/MB_SIZE+1][x/MB_SIZE+1]->x = bdx; /* is in half pel format */
00321   MV[5][y/MB_SIZE+1][x/MB_SIZE+1]->y = bdy;
00322   MV[5][y/MB_SIZE+1][x/MB_SIZE+1]->x_half = 0;
00323   MV[5][y/MB_SIZE+1][x/MB_SIZE+1]->y_half = 0;
00324 
00325 
00326   /* Do the actual prediction */
00327   for (j = 0; j < MB_SIZE; j++) 
00328     for (i = 0; i < MB_SIZE; i++) 
00329       p_err->lum[j][i] = 
00330         *(curr_image->lum+x+i + (y+j)*(VidSt->pels)) - pred->lum[j][i];
00331 
00332   y >>= 1;
00333   x >>= 1;
00334   for (j = 0; j < MB_SIZE>>1; j++) 
00335     for (i = 0; i < MB_SIZE>>1; i++) {
00336       p_err->Cr[j][i] = *(curr_image->Cr+x+i + (y+j)*(VidSt->cpels)) - pred->Cr[j][i];
00337       p_err->Cb[j][i] = *(curr_image->Cb+x+i + (y+j)*(VidSt->cpels)) - pred->Cb[j][i];
00338     }
00339 
00340   free(pred);
00341   return p_err;
00342 }

MB_Structure* Predict_P PictImage curr_image,
PictImage prev_image,
unsigned char *  prev_ipol,
int  x_curr,
int  y_curr,
MotionVector fr[5][MBR+1][MBC+2],
int  PB
 

Referenced by CodeOneOrTwo().

void PrintResult Bits bits,
int  num_units,
int  num
 

Definition at line 204 of file vid_main.c.

References Bits, bits_counted::C, bits_counted::CBPB, bits_counted::CBPCM, bits_counted::CBPY, bits_counted::COD, bits_counted::DQUANT, bits_counted::header, bits_counted::MODB, bits_counted::no_inter, bits_counted::no_inter4v, bits_counted::no_intra, bits_counted::total, bits_counted::vec, and bits_counted::Y.

Referenced by close_video_codec(), and code_video().

00205 {
00206   fprintf(stdout,"# intra   : %d\n", bits->no_intra/num_units);
00207   fprintf(stdout,"# inter   : %d\n", bits->no_inter/num_units);
00208   fprintf(stdout,"# inter4v : %d\n", bits->no_inter4v/num_units);
00209   fprintf(stdout,"--------------\n");
00210   fprintf(stdout,"Coeff_Y: %d\n", bits->Y/num);
00211   fprintf(stdout,"Coeff_C: %d\n", bits->C/num);
00212   fprintf(stdout,"Vectors: %d\n", bits->vec/num);
00213   fprintf(stdout,"CBPY   : %d\n", bits->CBPY/num);
00214   fprintf(stdout,"MCBPC  : %d\n", bits->CBPCM/num);
00215   fprintf(stdout,"MODB   : %d\n", bits->MODB/num);
00216   fprintf(stdout,"CBPB   : %d\n", bits->CBPB/num);
00217   fprintf(stdout,"COD    : %d\n", bits->COD/num);
00218   fprintf(stdout,"DQUANT : %d\n", bits->DQUANT/num);
00219   fprintf(stdout,"header : %d\n", bits->header/num);
00220   fprintf(stdout,"==============\n");
00221   fprintf(stdout,"Total  : %d\n", bits->total/num);
00222   fprintf(stdout,"\n");
00223   return;
00224 }

void PrintSNR Results res,
int  num
 

Definition at line 226 of file vid_main.c.

References Results, results::SNR_Cb, results::SNR_Cr, and results::SNR_l.

Referenced by close_video_codec(), and code_video().

00227 {
00228   FILE *fp = fopen("snr.dat","a");
00229   assert(fp!=NULL);
00230   fprintf(fp,"%.2f %.2f %.2f\n",res->SNR_l/num,res->SNR_Cb/num,res->SNR_Cr/num);
00231   fclose(fp);
00232   fprintf(stdout,"SNR_Y  : %.2f\n", res->SNR_l/num);
00233   fprintf(stdout,"SNR_Cb : %.2f\n", res->SNR_Cb/num);
00234   fprintf(stdout,"SNR_Cr : %.2f\n", res->SNR_Cr/num);
00235   fprintf(stdout,"--------------\n");
00236 
00237   return;
00238 }

void putbits int  ,
int 
 

Definition at line 73 of file vid_putbits.c.

References al_send_byte(), BitPrint(), video_codec::bytecnt, video_codec::label, video_codec::outbfr, video_codec::outcnt, video_codec::output, video_codec::simbuffer, video_codec::streamfile, video_codec::tf, video_codec::trace, and VidSt.

Referenced by alignbits(), bit_in_psc_layer(), CodeCoeff(), CountBitsMB(), CountBitsPicture(), CountBitsSlice(), put_cbpcm_inter(), put_cbpcm_intra(), put_cbpy(), put_coeff(), and put_mv().

00074 {
00075   int i;
00076   unsigned int mask;
00077   char bitstring[32];
00078 
00079   if ((VidSt->trace)) {
00080     if (n > 0) {
00081       BitPrint(n,val,bitstring);
00082       fprintf((VidSt->tf),bitstring);
00083     }
00084   }
00085 
00086   mask = 1 << (n-1); /* selects first (leftmost) bit */
00087 
00088   for (i=0; i<n; i++) {
00089     (VidSt->outbfr) <<= 1;
00090 
00091     if (val & mask)
00092       (VidSt->outbfr)|= 1;
00093 
00094     mask >>= 1; /* select next bit */
00095     (VidSt->outcnt)--;
00096 
00097     if ((VidSt->outcnt)==0) /* 8 bit buffer full */ {
00098       putc((VidSt->outbfr),(VidSt->streamfile));
00099 
00100       if (al_send_byte((VidSt->output), (VidSt->outbfr)) == 0)
00101         printf("!: \"%s\" overwrote AL-buffer!\n", (VidSt->label));
00102 
00103       (VidSt->simbuffer)++;
00104 
00105       (VidSt->outcnt) = 8;
00106       (VidSt->bytecnt)++;
00107     }
00108   }
00109 }

void Quant int *  coeff,
int *  qcoeff,
int  QP,
int  I
 

Definition at line 61 of file vid_quant.c.

References mmax, mmin, MODE_INTRA, and sign.

Referenced by MB_Encode().

00062 {
00063   int i;
00064   int level;
00065   
00066   if (QP) {
00067     if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q) { /* Intra */
00068       qcoeff[0] = mmax(1,mmin(254,coeff[0]/8));
00069 
00070       for (i = 1; i < 64; i++) {
00071         level = (abs(coeff[i])) / (2*QP);
00072         qcoeff[i] =  mmin(127,mmax(-127,sign(coeff[i]) * level));
00073       }
00074     }
00075     else { /* non Intra */
00076       for (i = 0; i < 64; i++) {
00077         level = (abs(coeff[i])-QP/2)  / (2*QP);
00078         qcoeff[i] = mmin(127,mmax(-127,sign(coeff[i]) * level));
00079       }
00080     }
00081   }
00082   else {
00083     /* No quantizing.
00084        Used only for testing. Bitstream will not be decodable 
00085        whether clipping is performed or not */
00086     for (i = 0; i < 64; i++) {
00087       qcoeff[i] = coeff[i];
00088     }
00089   }
00090   return;
00091 }

unsigned char* ReadImage char *  filename,
int  frame_no,
int  headerlength
 

Definition at line 62 of file vid_io.c.

References video_codec::lines, video_codec::pels, and VidSt.

Referenced by code_video().

00064 {
00065   FILE *im_file = NULL;
00066   int im_size = (VidSt->pels)*(VidSt->lines)*3/2;
00067   unsigned char *raw;
00068   int status;
00069 
00070   if ((raw = (unsigned char *)malloc(sizeof(char)*im_size)) == NULL) {
00071     fprintf(stderr,"Couldn't allocate memory to image\n");
00072     exit(-1);
00073   }
00074   if ((im_file = fopen(filename,"rb")) == NULL) {
00075     fprintf(stderr,"Unable to open image_file: %s\n",filename);
00076     exit(-1);
00077   }
00078   rewind(im_file);
00079   /* Find the correct image */
00080   status = fseek(im_file,headerlength + (frame_no) * im_size,0);
00081   if (status != 0) {
00082     fprintf(stderr,"Error in seeking image no: %d\n",frame_no);
00083     fprintf(stderr,"From file: %s\n",filename);
00084     exit(-1);
00085   }
00086   /* Read image */
00087   fprintf(stdout,"Reading image no: %d\n",frame_no);
00088   if ((status = fread(raw, sizeof(char), 
00089               im_size, im_file)) != im_size) {
00090     fprintf(stderr,"Error in reading image no: %d\n",frame_no);
00091     fprintf(stderr,"From file: %s\n",filename);
00092     exit(-1);
00093   }
00094 
00095   fclose(im_file);
00096   return raw;
00097 }

void ReconChromBlock_P int  x_curr,
int  y_curr,
int  dx,
int  dy,
PictImage prev,
MB_Structure data
 

Definition at line 1285 of file vid_pred.c.

References mb_structure::Cb, pict_image::Cb, mb_structure::Cr, pict_image::Cr, data, video_codec::long_vectors, MB_Structure, video_codec::mv_outside_frame, video_codec::pels, PictImage, and VidSt.

Referenced by MB_Recon_P().

01288 {
01289   int m,n;
01290 
01291   int x, y, ofx, ofy, pel,lx;
01292   int xint, yint;
01293   int xh, yh;
01294 
01295   lx = ((VidSt->mv_outside_frame) ? (VidSt->pels)/2 + ((VidSt->long_vectors)?32:16) : (VidSt->pels)/2);
01296 
01297   x = x_curr>>1;
01298   y = y_curr>>1;
01299 
01300   xint = dx>>1;
01301   xh = dx & 1;
01302   yint = dy>>1;
01303   yh = dy & 1;
01304     
01305   if (!xh && !yh) {
01306     for (n = 0; n < 8; n++) {
01307       for (m = 0; m < 8; m++) {
01308 
01309         ofx = x + xint + m;
01310         ofy = y + yint + n;
01311         pel=*(prev->Cr+ofx    + (ofy   )*lx);
01312         data->Cr[n][m] += pel;
01313 
01314         pel=*(prev->Cb+ofx    + (ofy   )*lx);
01315         data->Cb[n][m] += pel;
01316       }
01317     }
01318   }
01319   else if (!xh && yh) {
01320     for (n = 0; n < 8; n++) {
01321       for (m = 0; m < 8; m++) {
01322 
01323         ofx = x + xint + m;
01324         ofy = y + yint + n;
01325         pel=(*(prev->Cr+ofx    + (ofy   )*lx)+
01326              *(prev->Cr+ofx    + (ofy+yh)*lx) + 1)>>1;
01327 
01328         data->Cr[n][m] += pel;
01329 
01330         pel=(*(prev->Cb+ofx    + (ofy   )*lx)+
01331              *(prev->Cb+ofx    + (ofy+yh)*lx) + 1)>>1;
01332 
01333         data->Cb[n][m] += pel;
01334       
01335       }
01336     }
01337   }
01338   else if (xh && !yh) {
01339     for (n = 0; n < 8; n++) {
01340       for (m = 0; m < 8; m++) {
01341 
01342         ofx = x + xint + m;
01343         ofy = y + yint + n;
01344         pel=(*(prev->Cr+ofx    + (ofy   )*lx)+
01345              *(prev->Cr+ofx+xh + (ofy   )*lx) + 1)>>1;
01346 
01347         data->Cr[n][m] += pel;
01348 
01349         pel=(*(prev->Cb+ofx    + (ofy   )*lx)+
01350              *(prev->Cb+ofx+xh + (ofy   )*lx) + 1)>>1;
01351 
01352         data->Cb[n][m] += pel;
01353       
01354       }
01355     }
01356   }
01357   else { /* xh && yh */
01358     for (n = 0; n < 8; n++) {
01359       for (m = 0; m < 8; m++) {
01360         ofx = x + xint + m;
01361         ofy = y + yint + n;
01362         pel=(*(prev->Cr+ofx    + (ofy   )*lx)+
01363              *(prev->Cr+ofx+xh + (ofy   )*lx)+
01364              *(prev->Cr+ofx    + (ofy+yh)*lx)+
01365              *(prev->Cr+ofx+xh + (ofy+yh)*lx)+
01366              2)>>2;
01367 
01368         data->Cr[n][m] += pel;
01369 
01370         pel=(*(prev->Cb+ofx    + (ofy   )*lx)+
01371              *(prev->Cb+ofx+xh + (ofy   )*lx)+
01372              *(prev->Cb+ofx    + (ofy+yh)*lx)+
01373              *(prev->Cb+ofx+xh + (ofy+yh)*lx)+
01374              2)>>2;
01375 
01376         data->Cb[n][m] += pel;
01377       
01378       }
01379     }
01380   }
01381   return;
01382 }

void ReconImage int  i,
int  j,
MB_Structure data,
PictImage recon
 

Definition at line 747 of file vid_coder.c.

References mb_structure::Cb, pict_image::Cb, video_codec::cpels, mb_structure::Cr, pict_image::Cr, data, mb_structure::lum, pict_image::lum, MB_Structure, video_codec::pels, PictImage, and VidSt.

Referenced by CodeOneIntra(), and CodeOneOrTwo().

00748 {
00749   int n;
00750   register int m;
00751 
00752   int x_curr, y_curr;
00753 
00754   x_curr = i * MB_SIZE;
00755   y_curr = j * MB_SIZE;
00756 
00757   /* Fill in luminance data */
00758   for (n = 0; n < MB_SIZE; n++)
00759     for (m= 0; m < MB_SIZE; m++) {
00760       *(recon->lum + x_curr+m + (y_curr+n)*(VidSt->pels)) = data->lum[n][m];
00761     }
00762 
00763   /* Fill in chrominance data */
00764   for (n = 0; n < MB_SIZE>>1; n++)
00765     for (m = 0; m < MB_SIZE>>1; m++) {
00766       *(recon->Cr + (x_curr>>1)+m + ((y_curr>>1)+n)*(VidSt->cpels)) = data->Cr[n][m];
00767       *(recon->Cb + (x_curr>>1)+m + ((y_curr>>1)+n)*(VidSt->cpels)) = data->Cb[n][m];
00768     }
00769   return;
00770 }

void ReconLumBlock_P int  x,
int  y,
MotionVector fr,
unsigned char *  prev,
int *  data,
int  bs,
int  comp
 

Definition at line 1249 of file vid_pred.c.

References data, video_codec::long_vectors, MotionVector, video_codec::mv_outside_frame, video_codec::pels, VidSt, motionvector::x, motionvector::x_half, motionvector::y, and motionvector::y_half.

Referenced by MB_Recon_P().

01251 {
01252   int m, n;
01253   int x1, y1, lx;
01254 
01255   lx = ((VidSt->mv_outside_frame) ? (VidSt->pels) + ((VidSt->long_vectors)?64:32) : (VidSt->pels));
01256 
01257   x1 = 2*(x + fr->x) + fr->x_half;
01258   y1 = 2*(y + fr->y) + fr->y_half;
01259   
01260   x1 += ((comp&1)<<4);
01261   y1 += ((comp&2)<<3);
01262 
01263   for (n = 0; n < bs; n++) {
01264     for (m = 0; m < bs; m++) {
01265       *(data+m+n*16) += (int)(*(prev+x1+2*m + (y1+2*n)*2*lx));
01266     }
01267   }
01268 
01269   return;
01270 }

int SAD_Block unsigned char *  ii,
unsigned char *  act_block,
int  h_length,
int  min_sofar
 

Definition at line 394 of file vid_mot_est.c.

Referenced by MotionEstimation().

00396 {
00397   int i;
00398   int sad = 0;
00399   unsigned char *kk;
00400 
00401   kk = act_block;
00402   i = 8;
00403   while (i--) {
00404     sad += (abs(*ii     - *kk     ) +abs(*(ii+1 ) - *(kk+1) )
00405             +abs(*(ii+2) - *(kk+2) ) +abs(*(ii+3 ) - *(kk+3) )
00406             +abs(*(ii+4) - *(kk+4) ) +abs(*(ii+5 ) - *(kk+5) )
00407             +abs(*(ii+6) - *(kk+6) ) +abs(*(ii+7 ) - *(kk+7) ));
00408 
00409     ii += h_length;
00410     kk += 16;
00411     if (sad > min_sofar)
00412       return INT_MAX;
00413   } 
00414   return sad;
00415 }

int SAD_Macroblock unsigned char *  ii,
unsigned char *  act_block,
int  h_length,
int  Min_FRAME
 

Definition at line 367 of file vid_mot_est.c.

Referenced by MotionEstimation().

00369 {
00370   int i;
00371   int sad = 0;
00372   unsigned char *kk;
00373 
00374   kk = act_block;
00375   i = 16;
00376   while (i--) {
00377     sad += (abs(*ii     - *kk     ) +abs(*(ii+1 ) - *(kk+1) )
00378             +abs(*(ii+2) - *(kk+2) ) +abs(*(ii+3 ) - *(kk+3) )
00379             +abs(*(ii+4) - *(kk+4) ) +abs(*(ii+5 ) - *(kk+5) )
00380             +abs(*(ii+6) - *(kk+6) ) +abs(*(ii+7 ) - *(kk+7) )
00381             +abs(*(ii+8) - *(kk+8) ) +abs(*(ii+9 ) - *(kk+9) )
00382             +abs(*(ii+10)- *(kk+10)) +abs(*(ii+11) - *(kk+11))
00383             +abs(*(ii+12)- *(kk+12)) +abs(*(ii+13) - *(kk+13))
00384             +abs(*(ii+14)- *(kk+14)) +abs(*(ii+15) - *(kk+15)) );
00385 
00386     ii += h_length;
00387     kk += 16;
00388     if (sad > Min_FRAME)
00389       return INT_MAX;
00390   } 
00391   return sad;
00392 }

int SAD_MB_Bidir unsigned char *  ii,
unsigned char *  aa,
unsigned char *  bb,
int  width,
int  min_sofar
 

Definition at line 417 of file vid_mot_est.c.

Referenced by NextTwoPB().

00419 {
00420   int i, sad = 0;
00421   unsigned char *ll, *kk;
00422   kk = aa;
00423   ll = bb;
00424   i = 16;
00425   while (i--) {
00426     sad += (abs(*ii     - ((*kk    + *ll    )>>1)) +
00427             abs(*(ii+1) - ((*(kk+1)+ *(ll+1))>>1)) +
00428             abs(*(ii+2) - ((*(kk+2)+ *(ll+2))>>1)) +
00429             abs(*(ii+3) - ((*(kk+3)+ *(ll+3))>>1)) +
00430             abs(*(ii+4) - ((*(kk+4)+ *(ll+4))>>1)) +
00431             abs(*(ii+5) - ((*(kk+5)+ *(ll+5))>>1)) +
00432             abs(*(ii+6) - ((*(kk+6)+ *(ll+6))>>1)) +
00433             abs(*(ii+7) - ((*(kk+7)+ *(ll+7))>>1)) +
00434             abs(*(ii+8) - ((*(kk+8)+ *(ll+8))>>1)) +
00435             abs(*(ii+9) - ((*(kk+9)+ *(ll+9))>>1)) +
00436             abs(*(ii+10) - ((*(kk+10)+ *(ll+10))>>1)) +
00437             abs(*(ii+11) - ((*(kk+11)+ *(ll+11))>>1)) +
00438             abs(*(ii+12) - ((*(kk+12)+ *(ll+12))>>1)) +
00439             abs(*(ii+13) - ((*(kk+13)+ *(ll+13))>>1)) +
00440             abs(*(ii+14) - ((*(kk+14)+ *(ll+14))>>1)) +
00441             abs(*(ii+15) - ((*(kk+15)+ *(ll+15))>>1)));
00442 
00443     ii += width;
00444     kk += width;
00445     ll += width;
00446     if (sad > min_sofar)
00447       return INT_MAX;
00448   } 
00449   return sad;
00450 }

int SAD_MB_integer int *  ii,
int *  act_block,
int  h_length,
int  min_sofar
 

Definition at line 452 of file vid_mot_est.c.

Referenced by Predict_B().

00453 {
00454   int i, sad = 0, *kk;
00455 
00456   kk = act_block;
00457   i = 16;
00458   while (i--) {
00459     sad += (abs(*ii     - *kk     ) +abs(*(ii+1 ) - *(kk+1) )
00460             +abs(*(ii+2) - *(kk+2) ) +abs(*(ii+3 ) - *(kk+3) )
00461             +abs(*(ii+4) - *(kk+4) ) +abs(*(ii+5 ) - *(kk+5) )
00462             +abs(*(ii+6) - *(kk+6) ) +abs(*(ii+7 ) - *(kk+7) )
00463             +abs(*(ii+8) - *(kk+8) ) +abs(*(ii+9 ) - *(kk+9) )
00464             +abs(*(ii+10)- *(kk+10)) +abs(*(ii+11) - *(kk+11))
00465             +abs(*(ii+12)- *(kk+12)) +abs(*(ii+13) - *(kk+13))
00466             +abs(*(ii+14)- *(kk+14)) +abs(*(ii+15) - *(kk+15)) );
00467 
00468     ii += h_length;
00469     kk += 16;
00470     if (sad > min_sofar)
00471       return INT_MAX;
00472   } 
00473   return sad;
00474 }

char* StripName char *  s  ) 
 

int UpdateQuantizer int  mb,
float  QP_mean,
int  pict_type,
float  bit_rate,
int  mb_width,
int  mb_height,
int  bitcount
 

Definition at line 168 of file vid_ratectrl.c.

References video_codec::B_target, bitcount(), video_codec::global_adj, mmax, mmin, and VidSt.

Referenced by CodeOneOrTwo().

00175 {
00176   int newQP=16;
00177   float local_adj, descrepency, projection;
00178   
00179   if (pict_type == PCT_INTRA) {
00180     newQP = 16;         /* Changed from newQP = 16;, BD 8/27/96 */
00181   }
00182   else if (pict_type == PCT_INTER) {
00183     /* compute expected buffer fullness */
00184     
00185     projection = mb * ((VidSt->B_target) / (mb_width*mb_height));
00186     
00187     /* measure descrepency between current fullness and projection */
00188     descrepency= (bitcount - projection);
00189     
00190     /* scale */
00191     
00192     local_adj = 12 * descrepency / bit_rate;
00193     
00194 #if(0)
00195     printf("mb = %d\n",mb);
00196     printf("bit_count = %d projection = %.2f \n",bitcount,projection);
00197     printf("B_target = %.2f local_adj = %.2f \n",(VidSt->B_target),local_adj);
00198 #endif
00199     
00200     newQP = (int)(QP_mean * (1 + (VidSt->global_adj) + local_adj) + 0.5);
00201     
00202   /* the update equation for newQP in TMN4 document section 3.7 */
00203 
00204   }
00205   else  {
00206     fprintf(stderr,"Error (UpdateQuantizer): picture type unkown.\n");
00207   }
00208   
00209 #if(0)
00210   printf("mb = %d  newQP = %d \n",mb,newQP);
00211 #endif 
00212 
00213   newQP = mmax(1,mmin(31,newQP));
00214   return newQP;
00215 }

void UpdateRateControl int  bits  ) 
 

Definition at line 90 of file vid_ratectrl.c.

References video_codec::B_prev, and VidSt.

Referenced by code_video().

00091 {
00092   (VidSt->B_prev) = (float)bits;
00093 }

void WriteImage PictImage image,
char *  filename
 

Definition at line 142 of file vid_io.c.

References pict_image::Cb, pict_image::Cr, video_codec::lines, pict_image::lum, video_codec::pels, PictImage, and VidSt.

Referenced by code_video().

00144 {
00145   int status;
00146   FILE *f_out;
00147 
00148   /* Opening file */
00149   if ((f_out = fopen(filename,"ab")) == NULL) {
00150     fprintf(stderr,"%s%s\n","Error in opening file: ",filename);
00151     exit(-1);
00152   }
00153 
00154   /* Writing lum to file */
00155   if ((status = fwrite(image->lum,sizeof(char),(VidSt->pels)*(VidSt->lines),f_out)) 
00156       != (VidSt->pels)*(VidSt->lines)) {
00157     fprintf(stderr,"%s%s\n","Error in writing to file: ",filename);
00158     exit(-1);
00159   }
00160   /* Writing Cb to file */
00161   if ((status = fwrite(image->Cb,sizeof(char),(VidSt->pels)*(VidSt->lines)/4,f_out)) 
00162       != (VidSt->pels)*(VidSt->lines)/4) {
00163     fprintf(stderr,"%s%s\n","Error in writing to file: ",filename);
00164     exit(-1);
00165   }
00166   /* Writing Cr to file */
00167   if ((status = fwrite(image->Cr,sizeof(char),(VidSt->pels)*(VidSt->lines)/4,f_out)) 
00168       != (VidSt->pels)*(VidSt->lines)/4) {
00169     fprintf(stderr,"%s%s\n","Error in writing to file: ",filename);
00170     exit(-1);
00171   }
00172 
00173   fclose(f_out);
00174   return;
00175 }

void ZeroBits Bits bits  ) 
 

Definition at line 1044 of file vid_countbit.c.

References Bits, bits_counted::C, bits_counted::CBPB, bits_counted::CBPCM, bits_counted::CBPY, bits_counted::COD, bits_counted::DQUANT, bits_counted::header, bits_counted::MODB, bits_counted::no_inter, bits_counted::no_inter4v, bits_counted::no_intra, bits_counted::total, bits_counted::vec, and bits_counted::Y.

Referenced by code_video(), CodeOneIntra(), and CodeOneOrTwo().

01045 {
01046   bits->Y = 0;
01047   bits->C = 0;
01048   bits->vec = 0;
01049   bits->CBPY = 0;
01050   bits->CBPCM = 0;
01051   bits->MODB = 0;
01052   bits->CBPB = 0;
01053   bits->COD = 0;
01054   bits->DQUANT = 0;
01055   bits->header = 0;
01056   bits->total = 0;
01057   bits->no_inter = 0;
01058   bits->no_inter4v = 0;
01059   bits->no_intra = 0;
01060   return;
01061 }

void ZeroMBlock MB_Structure data  ) 
 

Definition at line 717 of file vid_coder.c.

References mb_structure::Cb, mb_structure::Cr, data, mb_structure::lum, and MB_Structure.

Referenced by CodeOneOrTwo().

00718 {
00719   int n;
00720   register int m;
00721 
00722   for (n = 0; n < MB_SIZE; n++)
00723     for (m = 0; m < MB_SIZE; m++)
00724       data->lum[n][m] = 0;
00725   for (n = 0; n < (MB_SIZE>>1); n++)
00726     for (m = 0; m < (MB_SIZE>>1); m++) {
00727       data->Cr[n][m] = 0;
00728       data->Cb[n][m] = 0;
00729     }
00730   return;
00731 }

void ZeroRes Results res  ) 
 

Definition at line 1062 of file vid_countbit.c.

References results::QP_mean, Results, results::SNR_Cb, results::SNR_Cr, and results::SNR_l.

Referenced by code_video().

01063 {
01064   res->SNR_l = (float)0;
01065   res->SNR_Cr = (float)0;
01066   res->SNR_Cb = (float)0;
01067   res->QP_mean = (float)0;
01068 }

void ZeroVec MotionVector MV  ) 
 

Definition at line 1110 of file vid_countbit.c.

References MotionVector, motionvector::x, motionvector::x_half, motionvector::y, and motionvector::y_half.

Referenced by CodeOneOrTwo(), and MotionEstimatePicture().

01111 {
01112   MV->x = 0;
01113   MV->y = 0;
01114   MV->x_half = 0;
01115   MV->y_half = 0;
01116   return;
01117 }


Generated on Sun Jul 16 16:27:48 2006 by  doxygen 1.3.9.1