📄 main.c
字号:
fprintf(stderr,"Couldn't open outputfile: %s\n",outputfile);
exit(-1);
}else
fclose(cleared);
if (writediff) {
if ((cleared = fopen(diff_filename,"wb")) == NULL) {
fprintf(stderr,"Couldn't open diff-file: %s\n",diff_filename);
exit(-1);
}else
fclose(cleared);
}
/* Intra image */
check_image_sum = Check_image_sequence(seqfilename,start, end, headerlength);
if(check_image_sum == 0){
fprintf(stderr,"Image Sequence are not right, program abort !\n");
exit(-1);
}
fprintf(stderr,"Start Coding...\n");
if(time_flag == 1) {
times->encode_start_time = clock();
times->frame_start_time = clock();
}
image = ReadImage(seqfilename,start,headerlength);
curr_image = FillImage(image);
pic->picture_coding_type = PCT_INTRA;
pic->QUANT = QPI;
curr_recon = CodeOneIntra(curr_image, QPI, bits, pic);
if (arith_used) {
bits->header += encoder_flush();
arith_used = 0;
}
bits->header += alignbits (); /* pictures shall be byte aligned */
fprintf(stdout,"Finished INTRA\n");
if(time_flag == 1) {
times->frame_end_time = clock();
times->frame_duration =
(float)(times->frame_end_time - times->frame_start_time) / CLOCKS_PER_SEC;
}
ComputeSNR(curr_image, curr_recon, res, writediff);
AddBitsPicture(bits);
PrintSNR(res, 1);
PrintResult(bits, times, 1, 1);
memcpy(intra_bits,bits,sizeof(Bits));
ZeroBits(total_bits);
ZeroRes(total_res);
ZeroRes(b_res);
/* number of seconds to encode */
seconds = (end - start + chosen_frameskip) * orig_frameskip/ ref_frame_rate;
if (trace) {
strcpy(tracefile, "trace");
fclose(tf);
/* Open trace-file for writing */
if ((tf = fopen(tracefile,"w")) == NULL) {
fprintf(stderr,"Unable to open tracefile (non-intra)\n");
exit(-1);
}
}
/* compute first frameskip */
#ifdef OFFLINE_RATE_CONTROL
first_frameskip = chosen_frameskip;
frameskip = chosen_frameskip;
#else
CommBacklog = intra_bits->total -
(int)(DelayBetweenFramesInSeconds * pic->bit_rate);
if (pic->bit_rate == 0) {
frameskip = chosen_frameskip;
}
else { /* rate control is used */
frameskip = 1;
while ( (int)(DelayBetweenFramesInSeconds*pic->bit_rate) <= CommBacklog) {
CommBacklog -= (int) ( DelayBetweenFramesInSeconds * pic->bit_rate );
frameskip += 1;
}
}
first_frameskip = frameskip;
#endif
if (first_frameskip > 256)
fprintf(stderr,"Warning: frameskip > 256\n");
pic->picture_coding_type = PCT_INTER;
pic->QUANT = QP;
bdist = chosen_frameskip;
/* always encode the first frame after intra as P frame.
This is not necessary, but something we chose to make
the adaptive PB frames calculations a bit simpler */
if (pb_frames) {
pic->PB = 0;
pdist = 2*chosen_frameskip - bdist;
}
if (write_repeated)
icopies = chosen_frameskip;
for (i = 0; i < icopies; i++)
WriteImage(curr_recon,outputfile); /* write wcopies frames to disk */
/***** Main loop *****/
for (frame_no = start + first_frameskip; frame_no <= end;
frame_no += frameskip) {
prev_image = curr_image;
prev_recon = curr_recon;
/* Set QP to pic->QUANT from previous encoded picture */
QP = pic->QUANT;
if(time_flag == 1) times->frame_start_time = clock();
if (!PPFlag) {
if (pic->PB) {
bdist = frameskip;
pdist = 2*frameskip - bdist;
pic->TRB = bdist * orig_frameskip;
if (pic->TRB > 8)
fprintf(stdout,"distance too large for B-frame\n");
/* Read the frame to be coded as B */
image = ReadImage(seqfilename,frame_no,headerlength);
B_image = FillImage(image);
first_loop_finished = 1;
if (frame_no + pdist <= end) {
image = ReadImage(seqfilename,frame_no + pdist,headerlength);
}
else {
pic->PB = 0; /* end of sequence, encode as P */
image = ReadImage(seqfilename,frame_no,headerlength);
}
}
else {
image = ReadImage(seqfilename,frame_no,headerlength);
}
curr_image = FillImage(image);
if (pic->PB) {
if (pic->TRB > 8 || !NextTwoPB(curr_image, B_image, prev_image,
bdist, pdist, pic->seek_dist)) {
/* curr_image and B_image were not suitable to be coded
as a PB-frame - encoding as two P-frames instead */
pic->PB = 0;
#ifdef OFFLINE_RATE_CONTROL
stored_image = curr_image;
#else
FreeImage(curr_image);
#endif
frameskip = bdist;
curr_image = B_image;
PPFlag = 1;
}
else {
frame_no += pdist;
}
}
}
else {
/* PPFlag is set when the second of the two P-frames
is due to be coded */
#ifdef OFFLINE_RATE_CONTROL
curr_image = stored_image;
#else
image = ReadImage(seqfilename,frame_no,headerlength);
curr_image = FillImage(image);
#endif
pic->PB = 0;
PPFlag = 0;
}
/* Temporal Reference is the distance between encoded frames compared
the reference picture rate which is 25.0 or 30 fps */
pic->TR += (( (frameskip+(pic->PB?pdist:0)) *orig_frameskip) % 256);
if (frameskip+(pic->PB?pdist:0) > 256)
fprintf(stdout,"Warning: frameskip > 256\n");
frames += (pic->PB ? 2: 1);
bframes += (pic->PB ? 1 : 0);
pframes += 1;
if (pic->PB) { /* Code two frames as a PB-frame */
B_recon = InitImage(pels*lines);
fprintf(stdout,"Coding PB frames %d and %d... ",
frame_no - pdist, frame_no);
fflush(stdout);
}
else { /* Code the next frame as a normal P-frame */
fprintf(stdout,"Coding P frame %d... ", frame_no);
fflush(stdout);
}
curr_recon = InitImage(pels*lines);
CodeOneOrTwo(curr_image, B_image, prev_image, prev_recon,
QP, (bdist+pdist)*orig_frameskip, bits, pic, B_recon, curr_recon);
fprintf(stdout,"done\n");
if (targetrate != 0)
fprintf(stdout,"Inter QP: %d\n", QP);
fflush(stdout);
if (arith_used) {
bits->header += encoder_flush();
arith_used = 0;
}
bits->header += alignbits (); /* pictures shall be byte aligned */
AddBitsPicture(bits);
AddBits(total_bits, bits);
#ifndef OFFLINE_RATE_CONTROL
if (pic->bit_rate != 0 && pic->PB)
CommBacklog -= (int)
( DelayBetweenFramesInSeconds*pic->bit_rate ) * pdist;
if (pic->bit_rate != 0) {
UpdateRateControl(bits->total);
CommBacklog += bits->total;
frameskip = 1;
CommBacklog -= (int)
(frameskip * DelayBetweenFramesInSeconds *pic->bit_rate);
while ( (int)(DelayBetweenFramesInSeconds*pic->bit_rate) <= CommBacklog)
{
CommBacklog -= (int) ( DelayBetweenFramesInSeconds * pic->bit_rate );
frameskip += 1;
}
}
#else
/* Aim for the targetrate with a once per frame rate control scheme */
if (targetrate != 0)
if (frame_no - start > (end - start) * start_rate_control/100.0)
/* when generating the MPEG-4 anchors, rate control was started
after 70% of the sequence was finished.
Set start_rate_control with option "-R <n>" */
pic->QUANT = FrameUpdateQP(total_bits->total + intra_bits->total,
bits->total / (pic->PB?2:1),
(end-frame_no) / chosen_frameskip + PPFlag,
QP, targetrate, seconds);
frameskip = chosen_frameskip;
#endif
if(time_flag == 1){
times->frame_end_time = clock();
times->frame_duration =
(float)(times->frame_end_time - times->frame_start_time) / CLOCKS_PER_SEC;
}
if (pic->PB) {
if (write_repeated)
wcopies = pdist;
for (i = 0; i < wcopies; i++)
WriteImage(B_recon,outputfile); /* write wcopies frames to disk */
ComputeSNR(B_image, B_recon, res, writediff);
fprintf(stdout,"Results for B-frame:\n");
AddRes(b_res,res,pic);
PrintSNR(res, 1);
FreeImage(B_image);
FreeImage(B_recon);
}
if (write_repeated)
wcopies = (pb_frames ? bdist : frameskip);
for (i = 0; i < wcopies; i++)
WriteImage(curr_recon,outputfile); /* write wcopies frames to disk */
if (pb_frames)
pic->PB = 1;
ComputeSNR(curr_image, curr_recon, res, writediff);
fprintf(stdout,"Results for P-frame:\n");
AddRes(total_res,res,pic);
PrintSNR(res, 1);
PrintResult(bits, times, 1, 1);
FreeImage(prev_image);
FreeImage(prev_recon);
fflush(stdout);
}
/***** end of main loop *****/
if(time_flag == 1) times->encode_end_time = clock();
/* Count encode time */
if(time_flag == 1) times->encode_duration =
(float)(times->encode_end_time - times->encode_start_time) / CLOCKS_PER_SEC;
/* Closing files */
fclose (streamfile);
if (trace) {
fclose(tf);
}
/* Print total results */
total_frames_passed = frame_no - start - first_frameskip;
fprintf(stdout,"\n==== TOTAL ====\n");
fprintf(stdout,"for %d images of %s\n", frames, seqfilename);
if (frames != 0) {
if (write_repeated)
fprintf(stdout,"Frames saved : %d predicted + %d intra\n",
total_frames_passed,icopies);
fprintf(stdout,"--------------\n");
if (pb_frames && bframes != 0) {
fprintf(stdout,"SNR for %d B-frames:\n",bframes);
PrintSNR(b_res,bframes);
}
fprintf(stdout,"SNR for %d P-frames:\n",pframes);
PrintSNR(total_res,pframes);
PrintResult(total_bits, times, pframes, frames);
if (targetrate != 0 || pic->bit_rate != 0)
fprintf(stdout,"Original seq time: %.2f (%.2f) sec\n",
(total_frames_passed + first_frameskip) /
ref_frame_rate * orig_frameskip,
total_frames_passed /
ref_frame_rate * orig_frameskip);
fprintf(stdout,"Mean quantizer : %.2f\n", total_res->QP_mean/pframes);
#if 0
fprintf(stdout,"Total frames : %3d (%3d)\n",
total_frames_passed + first_frameskip,
total_frames_passed);
#endif
fprintf(stdout,"Encoded frames : %3d (%3d)\n",
frames + 1,
frames);
mean_frame_rate = frames / (float)total_frames_passed *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -