📄 main.c
字号:
ref_frame_rate / (float)orig_frameskip;
fprintf(stdout,"Mean frame rate : %.2f Hz\n", mean_frame_rate);
if (targetrate != 0)
fprintf(stdout,"Target bit rate : %.2f kbit/sec\n",
targetrate/1000.0);
fprintf(stdout,"Obtained bit rate: %.2f (%.2f) kbit/sec\n",
(total_bits->total + intra_bits->total) /
((total_frames_passed + first_frameskip) /
ref_frame_rate * orig_frameskip)/1000.0,
(total_bits->total / (float)frames) * mean_frame_rate/1000.0);
fprintf(stdout,"Total encode time: %15.2f sec\n",times->encode_duration);
fprintf(stdout,"============================================\n");
}
#if 0
fprintf(stdout,"Total number of bits: %d (%d)\n",
total_bits->total + intra_bits->total,
(total_bits->total + intra_bits->total) / 8);
#endif
if(global_file_save_flag == 1)
fclose(message_in);
/* Free memory */
FreeImage(curr_recon);
FreeImage(curr_image);
free(streamname);
free(seqfilename);
free(outputfile);
free(tracefile);
free(bits);
free(total_bits);
free(intra_bits);
free(res);
free(total_res);
free(b_res);
free(pic);
exit(0);
}
/**********************************************************************
*
* Name: NextTwoPB
* Description: Decides whether or not to code the next
* two images as PB
* Speed: This is not a very smart solution considering
* the encoding speed, since motion vectors
* have to be calculation several times. It
* can be done together with the normal
* motion vector search, or a tree search
* instead of a full search can be used.
*
* Input: pointers to previous image, potential B-
* and P-image, frame distances
* Returns: 1 for yes, 0 otherwise
* Side effects:
*
***********************************************************************/
int NextTwoPB(PictImage *next2, PictImage *next1, PictImage *prev,
int bskip, int pskip, int seek_dist)
{
int adv_is_on = 0, mof_is_on = 0, lv_is_on = 0;
int psad1, psad2, bsad, psad;
MotionVector *MV[6][MBR+1][MBC+2];
MotionVector *mvp, *mvbf, *mvbb;
int x,y;
int i,j,k,tmp;
/* Temporarily disable some options to simplify motion estimation */
if (advanced) {
advanced = OFF;
adv_is_on = ON;
}
if (mv_outside_frame) {
mv_outside_frame = OFF;
mof_is_on = ON;
}
if (long_vectors) {
long_vectors = OFF;
lv_is_on = ON;
}
for (j = 1; j <= (lines>>4); j++)
for (i = 1; i <= (pels>>4); i++)
for (k = 0; k < 3; k++) {
MV[k][j][i] = (MotionVector *)calloc(1,sizeof(MotionVector));
/* calloc to avoid Checker warnings about reading of
unitizalized memory in the memcpy's below */
}
mvbf = (MotionVector *)malloc(sizeof(MotionVector));
mvbb = (MotionVector *)malloc(sizeof(MotionVector));
psad = 0;
psad1 = 0;
psad2 = 0;
bsad = 0;
/* Integer motion estimation */
for ( j = 1; j < lines/MB_SIZE - 1; j++) {
for ( i = 1; i < pels/MB_SIZE - 1 ; i++) {
x = i*MB_SIZE;
y = j*MB_SIZE;
/* picture order: prev -> next1 -> next2 */
/* next1 and next2 can be coded as PB or PP */
/* prev is the previous encoded picture */
/* computes vectors (prev <- next2) */
MotionEstimation(next2->lum,prev->lum,x,y,0,0,seek_dist,MV,&tmp);
if (MV[0][j+1][i+1]->x == 0 && MV[0][j+1][i+1]->y == 0)
MV[0][j+1][i+1]->min_error += PREF_NULL_VEC;
/* not necessary to prefer zero vector here */
memcpy(MV[2][j+1][i+1],MV[0][j+1][i+1],sizeof(MotionVector));
/* computes sad(prev <- next1) */
MotionEstimation(next1->lum,prev->lum,x,y,0,0,seek_dist,MV,&tmp);
if (MV[0][j+1][i+1]->x == 0 && MV[0][j+1][i+1]->y == 0)
MV[0][j+1][i+1]->min_error += PREF_NULL_VEC;
memcpy(MV[1][j+1][i+1],MV[0][j+1][i+1],sizeof(MotionVector));
/* computes vectors for (next1 <- next2) */
MotionEstimation(next2->lum,next1->lum,x,y,0,0,seek_dist,MV,&tmp);
if (MV[0][j+1][i+1]->x == 0 && MV[0][j+1][i+1]->y == 0)
MV[0][j+1][i+1]->min_error += PREF_NULL_VEC;
/* scales vectors for (prev <- next2 ) */
mvp = MV[2][j+1][i+1];
mvbf->x = bskip * mvp->x / (bskip + pskip);
mvbb->x = - pskip * mvp->x / (bskip + pskip);
mvbf->y = bskip * mvp->y / (bskip + pskip);
mvbb->y = - pskip * mvp->y / (bskip + pskip);
psad1 += MV[0][j+1][i+1]->min_error;
psad2 += MV[1][j+1][i+1]->min_error;
psad += mvp->min_error;
/* computes sad(prev <- next1 -> next2) */
bsad += SAD_MB_Bidir(next1->lum + x + y*pels,
next2->lum + x + mvbb->x + (y + mvbb->y)*pels,
prev->lum + x + mvbf->x + (y + mvbf->y)*pels,
pels, INT_MAX);
}
}
for (j = 1; j <= (lines>>4); j++)
for (i = 1; i <= (pels>>4); i++)
for (k = 0; k < 3; k++)
free(MV[k][j][i]);
free(mvbf);
free(mvbb);
/* restore advanced parameters */
advanced = adv_is_on;
mv_outside_frame = mof_is_on;
long_vectors = lv_is_on;
/* do the decision */
if (bsad < (psad1+psad2)/2)
fprintf(stdout,"Chose PB - bsad %d, psad %d\n",
bsad, (psad1+psad2)/2);
else
fprintf(stdout,"Chose PP - bsad %d, psad %d\n",
bsad, (psad1+psad2)/2);
if (bsad < (psad1 + psad2)/2)
return 1;
else
return 0;
}
/**********************************************************************
*
* Name: Help
* Description: Prints usage
*
***********************************************************************/
void Help()
{
fprintf(stdout,"Usage:\ttmn [options] -i <filename> [more options]\n");
fprintf(stdout,"Options:\n");
fprintf(stdout,"\t-i <filename> original sequence [required parameter]\n");
fprintf(stdout,"\t-o <filename> reconstructed frames [%s]\n",
DEF_OUTFILENAME);
fprintf(stdout,"\t-B <filename> filename for bitstream [%s]\n",
DEF_STREAMNAME);
fprintf(stdout,"\t-a <n> image to start at [%d]\n",
DEF_START_FRAME);
fprintf(stdout,"\t-b <n> image to stop at [%d]\n",
DEF_STOP_FRAME);
fprintf(stdout,"\t-x <n> coding format [%d]\n",DEF_CODING_FORMAT);
fprintf(stdout,"\t-q <n> (1..31) quantization parameter QP [%d]\n",
DEF_INTER_QUANT);
fprintf(stdout,"\t-I <n> (1..31) QP for first frame [%d]\n",
DEF_INTRA_QUANT);
fprintf(stdout,"\t-r <n> target bitrate in bits/s, default is variable bitrate\n");
fprintf(stdout,"\t-S <n> frames to skip between each encoded frame [%d]\n",
DEF_FRAMESKIP);
fprintf(stdout,"\t-T display encoder needed time [%s]\n",
DEF_TIME_MODE ? "ON":"OFF");
fprintf(stdout,"\t-D use unrestricted motion vector mode (annex D) [%s]\n",
DEF_UMV_MODE ? "ON" : "OFF");
fprintf(stdout,"\t-E use syntax-based arithmetic coding (annex E) [%s]\n",
DEF_SAC_MODE ? "ON" : "OFF");
fprintf(stdout,"\t-F use advanced prediction mode (annex F) [%s]\n",
DEF_ADV_MODE ? "ON" : "OFF");
fprintf(stdout,"\t-G use PB-frames (annex G) [%s]\n",
DEF_PBF_MODE ? "ON" : "OFF");
fprintf(stdout,"\t-h Prints simple help\n");
fprintf(stdout,"\t-H Prints advanced help\n");
fprintf(stdout,"\n\tDefault filenames and other options in square brackets \n\tare chosen in config.h\n");
return;
}
void AdvancedHelp()
{
fprintf(stdout,"Usage:\ttmn [options] -i <filename> [more options]\n");
fprintf(stdout,"Options:\n");
fprintf(stdout,"\t-i <filename> original sequence [required parameter]\n");
fprintf(stdout,"\t-o <filename> reconstructed frames [%s]\n",
DEF_OUTFILENAME);
fprintf(stdout,"\t-B <filename> filename for bitstream [%s]\n",
DEF_STREAMNAME);
fprintf(stdout,"\t-a <n> image to start at [%d]\n",
DEF_START_FRAME);
fprintf(stdout,"\t-b <n> image to stop at [%d]\n",
DEF_STOP_FRAME);
fprintf(stdout,"\t-x <n> coding format [%d]\n",DEF_CODING_FORMAT);
fprintf(stdout,"\t n=1: SQCIF n=2: QCIF n=3: CIF1 n=4: CIF2 n=5: 4CIF n=6: 16CIF\n");
fprintf(stdout,"\t 128x96 176x144 352x288 352x240 704x576 1408x1152\n");
fprintf(stdout,"\t-s <n> (0..15) integer pel search window [%d]\n",
DEF_SEEK_DIST);
fprintf(stdout,"\t-q <n> (1..31) quantization parameter QP [%d]\n",
DEF_INTER_QUANT);
fprintf(stdout,"\t-I <n> (1..31) QP for first frame [%d]\n",
DEF_INTRA_QUANT);
fprintf(stdout,"\t-r <n> target bitrate in bits/s, default is variable bitrate\n");
#ifdef OFFLINE_RATE_CONTROL
fprintf(stdout,"\t-R <n> start rate control after n%% of sequence [%d]\n",
DEF_START_RATE_CONTROL);
#else
fprintf(stdout,"\t -R <f> target frame rate [%.2f]\n",
DEF_TARGET_FRAME_RATE);
#endif
fprintf(stdout,"\t-S <n> frames to skip between each encoded frame [%d]\n",
DEF_FRAMESKIP);
fprintf(stdout,"\t-Z <n> reference frame rate (25 or 30 fps) [%.1f]\n",
DEF_REF_FRAME_RATE);
fprintf(stdout,"\t-O <n> frames skipped in original compared to reference frame rate [%d]\n", DEF_ORIG_SKIP);
fprintf(stdout,"\t-e <n> original sequence has n bytes header [%d]\n",
DEF_HEADERLENGTH);
fprintf(stdout,"\t-g <n> insert sync after each n GOB (slice) [%d]\n",
DEF_INSERT_SYNC);
fprintf(stdout,"\t zero above means no extra syncs inserted\n");
fprintf(stdout,"\t-w write difference image to file \"%s\" [%s]\n",
DEF_DIFFILENAME,
DEF_WRITE_DIFF ? "ON" : "OFF");
fprintf(stdout,"\t-m write repeated reconstructed frames to disk [%s]\n",
DEF_WRITE_REPEATED ? "ON" : "OFF");
fprintf(stdout,"\t-t write trace to tracefile trace.intra/trace [%s]\n",
DEF_WRITE_TRACE ? "ON" : "OFF");
fprintf(stdout,"\t-T display encoder needed time [%s]\n",
DEF_TIME_MODE ? "ON":"OFF");
fprintf(stdout,"\t-D use unrestricted motion vector mode (annex D) [%s]\n",
DEF_UMV_MODE ? "ON" : "OFF");
fprintf(stdout,"\t-E use syntax-based arithmetic coding (annex E) [%s]\n",
DEF_SAC_MODE ? "ON" : "OFF");
fprintf(stdout,"\t-F use advanced prediction mode (annex F) [%s]\n",
DEF_ADV_MODE ? "ON" : "OFF");
fprintf(stdout,"\t-G use PB-frames (annex G) [%s]\n",
DEF_PBF_MODE ? "ON" : "OFF");
fprintf(stdout,"\t-Q <n> (0..3) BQUANT parameter [%d]\n",DEF_BQUANT);
fprintf(stdout,"\t-h Prints simple help\n");
fprintf(stdout,"\t-H Prints advanced help\n");
fprintf(stdout,"\n\tDefault filenames and other options in square brackets \n\tare chosen in config.h\n");
return;
}
/**********************************************************************
*
* Name: PrintResult
* Description: add bits and prints results
*
* Input: Bits struct
*
* Returns:
* Side effects:
*
***********************************************************************/
void PrintResult(Bits *bits,Times *times,int num_units, int num)
{
fprintf(stdout,"# intra : %d\n", bits->no_intra/num_units);
fprintf(stdout,"# inter : %d\n", bits->no_inter/num_units);
fprintf(stdout,"# inter4v : %d\n", bits->no_inter4v/num_units);
fprintf(stdout,"--------------\n");
fprintf(stdout,"Coeff_Y: %d\n", bits->Y/num);
fprintf(stdout,"Coeff_C: %d\n", bits->C/num);
fprintf(stdout,"Vectors: %d\n", bits->vec/num);
fprintf(stdout,"CBPY : %d\n", bits->CBPY/num);
fprintf(stdout,"MCBPC : %d\n", bits->CBPCM/num);
fprintf(stdout,"MODB : %d\n", bits->MODB/num);
fprintf(stdout,"CBPB : %d\n", bits->CBPB/num);
fprintf(stdout,"COD : %d\n", bits->COD/num);
fprintf(stdout,"DQUANT : %d\n", bits->DQUANT/num);
fprintf(stdout,"header : %d\n", bits->header/num);
fprintf(stdout,"==============\n");
fprintf(stdout,"Total : %d\n", bits->total/num);
if(global_file_save_flag == 1){
fprintf(message_in,"%d,%d,%d",bits->total/num,bits->header/num,bits->vec/num);
if(time_flag == 1)
fprintf(message_in,",%5.2f",times->frame_duration);
fprintf(message_in,"\n");
}
if(time_flag == 1)
fprintf(stdout,"Encode time: %5.2f sec\n", times->frame_duration);
fprintf(stdout,"\n");
return;
}
void PrintSNR(Results *res, int num)
{
fprintf(stdout,"SNR_Y : %.2f\n", res->SNR_l/num);
fprintf(stdout,"SNR_Cb : %.2f\n", res->SNR_Cb/num);
fprintf(stdout,"SNR_Cr : %.2f\n", res->SNR_Cr/num);
fprintf(stdout,"--------------\n");
if(global_file_save_flag == 1)
fprintf(message_in,"%.2f,%.2f,%.2f,",res->SNR_l/num,res->SNR_Cb/num,res->SNR_Cr/num);
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -