[svn] / branches / dev-api-3 / vfw / src / 2pass.c Repository:
ViewVC logotype

Diff of /branches/dev-api-3/vfw/src/2pass.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 102, Fri Apr 5 14:42:37 2002 UTC revision 365, Wed Aug 7 06:13:18 2002 UTC
# Line 23  Line 23 
23   *   *
24   *      History:   *      History:
25   *   *
26     *      17.04.2002      changed 1st pass quant to always be 2 (2pass_update())
27     *      07.04.2002      added max bitrate constraint, overflow controls (foxer)
28   *      31.03.2002      inital version;   *      31.03.2002      inital version;
29   *   *
30   *************************************************************************/   *************************************************************************/
# Line 161  Line 163 
163                                          {                                          {
164                                                  i_boost_total = twopass->nns2.bytes * codec->config.keyframe_boost / 100;                                                  i_boost_total = twopass->nns2.bytes * codec->config.keyframe_boost / 100;
165                                                  i_total += twopass->nns2.bytes;                                                  i_total += twopass->nns2.bytes;
166                                                    twopass->keyframe_locations[i_frames] = frames;
167                                                  ++i_frames;                                                  ++i_frames;
168                                          }                                          }
169    
# Line 172  Line 175 
175    
176                                  ++frames;                                  ++frames;
177                          }                          }
178                            twopass->keyframe_locations[i_frames] = frames;
179    
180                          twopass->movie_curve = ((double)(total_ext + i_boost_total) / total_ext);                          twopass->movie_curve = ((double)(total_ext + i_boost_total) / total_ext);
181                          twopass->average_frame = ((double)(total_ext - i_total) / (frames - credits_frames - i_frames) / twopass->movie_curve);                          twopass->average_frame = ((double)(total_ext - i_total) / (frames - credits_frames - i_frames) / twopass->movie_curve);
# Line 369  Line 373 
373                                  {                                  {
374                                          i_total += twopass->nns1.bytes + twopass->nns1.bytes * codec->config.keyframe_boost / 100;                                          i_total += twopass->nns1.bytes + twopass->nns1.bytes * codec->config.keyframe_boost / 100;
375                                          total += twopass->nns1.bytes * codec->config.keyframe_boost / 100;                                          total += twopass->nns1.bytes * codec->config.keyframe_boost / 100;
376                                            twopass->keyframe_locations[i_frames] = frames;
377                                          ++i_frames;                                          ++i_frames;
378                                  }                                  }
379    
# Line 376  Line 381 
381    
382                                  ++frames;                                  ++frames;
383                          }                          }
384                            twopass->keyframe_locations[i_frames] = frames;
385    
386                          // compensate for avi frame overhead                          // compensate for avi frame overhead
387                          desired -= frames * 24;                          desired -= frames * 24;
# Line 680  Line 686 
686                  }                  }
687    
688                  twopass->overflow = 0;                  twopass->overflow = 0;
689                    twopass->KFoverflow = 0;
690                    twopass->KFoverflow_partial = 0;
691                    twopass->KF_idx = 1;
692    
693                  break;                  break;
694          }          }
# Line 700  Line 709 
709          int bytes1, bytes2;          int bytes1, bytes2;
710          int overflow;          int overflow;
711          int credits_pos;          int credits_pos;
712            int capped_to_max_framesize = 0;
713            int KFdistance, KF_min_size;
714    
715          if (codec->framenum == 0)          if (codec->framenum == 0)
716          {          {
# Line 708  Line 719 
719                  for (i=0 ; i<32 ; ++i)                  for (i=0 ; i<32 ; ++i)
720                  {                  {
721                          quant_error[i] = 0.0;                          quant_error[i] = 0.0;
722                            twopass->quant_count[i] = 0;
723                  }                  }
724    
725                  curve_comp_error = 0.0;                  curve_comp_error = 0.0;
# Line 918  Line 930 
930    
931          twopass->desired_bytes2 = bytes2;          twopass->desired_bytes2 = bytes2;
932    
933            // if this keyframe is too close to the next,
934            // reduce it's byte allotment
935            if (frame->intra && !credits_pos)
936            {
937                    KFdistance = codec->twopass.keyframe_locations[codec->twopass.KF_idx] -
938                            codec->twopass.keyframe_locations[codec->twopass.KF_idx - 1];
939    
940                    if (KFdistance < codec->config.kftreshold)
941                    {
942                            KFdistance = KFdistance - codec->config.min_key_interval;
943    
944                            if (KFdistance >= 0)
945                            {
946                                    KF_min_size = bytes2 * (100 - codec->config.kfreduction) / 100;
947                                    if (KF_min_size < 1)
948                                            KF_min_size = 1;
949    
950                                    bytes2 = KF_min_size + (bytes2 - KF_min_size) * KFdistance /
951                                            (codec->config.kftreshold - codec->config.min_key_interval);
952    
953                                    if (bytes2 < 1)
954                                            bytes2 = 1;
955                            }
956                    }
957            }
958    
959          // Foxer: scale overflow in relation to average size, so smaller frames don't get          // Foxer: scale overflow in relation to average size, so smaller frames don't get
960          // too much/little bitrate          // too much/little bitrate
961          overflow = (int)((double)overflow * bytes2 / twopass->average_frame);          overflow = (int)((double)overflow * bytes2 / twopass->average_frame);
# Line 929  Line 967 
967          }          }
968    
969          // Foxer: make sure overflow doesn't run away          // Foxer: make sure overflow doesn't run away
970          if (overflow > bytes2 * 6 / 10)          if (overflow > bytes2 * codec->config.twopass_max_overflow_improvement / 100)
971          {          {
972                  bytes2 += (overflow <= bytes2) ? bytes2 * 6 / 10 : overflow * 6 / 10;                  bytes2 += (overflow <= bytes2) ? bytes2 * codec->config.twopass_max_overflow_improvement / 100 :
973                            overflow * codec->config.twopass_max_overflow_improvement / 100;
974          }          }
975          else if (overflow < bytes2 * -6 / 10)          else if (overflow < bytes2 * codec->config.twopass_max_overflow_degradation / -100)
976          {          {
977                  bytes2 += bytes2 * -6 / 10;                  bytes2 += bytes2 * codec->config.twopass_max_overflow_degradation / -100;
978          }          }
979          else          else
980          {          {
981                  bytes2 += overflow;                  bytes2 += overflow;
982          }          }
983    
984            if (bytes2 > twopass->max_framesize)
985            {
986                    capped_to_max_framesize = 1;
987                    bytes2 = twopass->max_framesize;
988            }
989    
990          if (bytes2 < 1)          if (bytes2 < 1)
991          {          {
992                  bytes2 = 1;                  bytes2 = 1;
# Line 1005  Line 1050 
1050                  }                  }
1051    
1052                  // subsequent frame quants can only be +- 2                  // subsequent frame quants can only be +- 2
1053                  if (last_quant)                  if (last_quant && capped_to_max_framesize == 0)
1054                  {                  {
1055                          if (frame->quant > last_quant + 2)                          if (frame->quant > last_quant + 2)
1056                          {                          {
# Line 1020  Line 1065 
1065                  }                  }
1066          }          }
1067    
1068            if (capped_to_max_framesize == 0)
1069          last_quant = frame->quant;          last_quant = frame->quant;
1070    
1071          if (codec->config.quant_type == QUANT_MODE_MOD)          if (codec->config.quant_type == QUANT_MODE_MOD)
# Line 1038  Line 1084 
1084    
1085          NNSTATS nns1;          NNSTATS nns1;
1086          DWORD wrote;          DWORD wrote;
1087            int credits_pos, tempdiv;
1088          char* quant_type;          char* quant_type;
1089    
1090          if (codec->framenum == 0)          if (codec->framenum == 0)
# Line 1060  Line 1107 
1107                  nns1.md_u = nns1.md_y = 0;                  nns1.md_u = nns1.md_y = 0;
1108                  nns1.mk_u = nns1.mk_y = 0;                  nns1.mk_u = nns1.mk_y = 0;
1109    
1110                  nns1.quant = stats->quant;  //              nns1.quant = stats->quant;
1111                    nns1.quant = 2;                         // ugly fix for lumi masking in 1st pass returning new quant
1112                  if (frame->intra)                  if (frame->intra)
1113                  {                  {
1114                          nns1.quant |= NNSTATS_KEYFRAME;                          nns1.quant |= NNSTATS_KEYFRAME;
# Line 1083  Line 1131 
1131    
1132          case DLG_MODE_2PASS_2_INT :          case DLG_MODE_2PASS_2_INT :
1133          case DLG_MODE_2PASS_2_EXT :          case DLG_MODE_2PASS_2_EXT :
1134                    credits_pos = codec_is_in_credits(&codec->config, codec->framenum);
1135                    if (!credits_pos)
1136                    {
1137                            codec->twopass.quant_count[frame->quant]++;
1138                            if ((codec->twopass.nns1.quant & NNSTATS_KEYFRAME))
1139                            {
1140                                    // calculate how much to distribute per frame in
1141                                    // order to make up for this keyframe's overflow
1142    
1143                                    codec->twopass.overflow += codec->twopass.KFoverflow;
1144                                    codec->twopass.KFoverflow = codec->twopass.desired_bytes2 - frame->length;
1145    
1146                                    tempdiv = (codec->twopass.keyframe_locations[codec->twopass.KF_idx] -
1147                                            codec->twopass.keyframe_locations[codec->twopass.KF_idx - 1]);
1148    
1149                                    if (tempdiv > 1)
1150                                    {
1151                                            // non-consecutive keyframes
1152                                            codec->twopass.KFoverflow_partial = codec->twopass.KFoverflow / (tempdiv - 1);
1153                                    }
1154                                    else
1155                                    {
1156                                            // consecutive keyframes
1157                                            codec->twopass.overflow += codec->twopass.KFoverflow;
1158                                            codec->twopass.KFoverflow = 0;
1159                                            codec->twopass.KFoverflow_partial = 0;
1160                                    }
1161                                    codec->twopass.KF_idx++;
1162                            }
1163                            else
1164                            {
1165                                    // distribute part of the keyframe overflow
1166    
1167                                    codec->twopass.overflow += codec->twopass.desired_bytes2 - frame->length +
1168                                            codec->twopass.KFoverflow_partial;
1169                                    codec->twopass.KFoverflow -= codec->twopass.KFoverflow_partial;
1170                            }
1171                    }
1172                    else
1173                    {
1174                  codec->twopass.overflow += codec->twopass.desired_bytes2 - frame->length;                  codec->twopass.overflow += codec->twopass.desired_bytes2 - frame->length;
1175                  DEBUG2ND(frame->quant, quant_type, frame->intra, codec->twopass.bytes1, codec->twopass.desired_bytes2, frame->length, codec->twopass.overflow, codec_is_in_credits(&codec->config, codec->framenum))  
1176                            // ugly fix for credits..
1177                            codec->twopass.overflow += codec->twopass.KFoverflow;
1178                            codec->twopass.KFoverflow = 0;
1179                            codec->twopass.KFoverflow_partial = 0;
1180                            // end of ugly fix.
1181                    }
1182    
1183                    DEBUG2ND(frame->quant, quant_type, frame->intra, codec->twopass.bytes1, codec->twopass.desired_bytes2, frame->length, codec->twopass.overflow, credits_pos)
1184                  break;                  break;
1185    
1186          default:          default:
# Line 1094  Line 1190 
1190          return ICERR_OK;          return ICERR_OK;
1191  }  }
1192    
1193    void codec_2pass_finish(CODEC* codec)
1194    {
1195            int i;
1196            char s[100];
1197            if (codec->config.mode == DLG_MODE_2PASS_2_EXT || codec->config.mode == DLG_MODE_2PASS_2_INT)
1198            {
1199                    // output the quantizer distribution for this encode.
1200    
1201                    OutputDebugString("Quantizer distribution for 2nd pass:");
1202                    for (i=1; i<=31; i++)
1203                    {
1204                            if (codec->twopass.quant_count[i])
1205                            {
1206                                    wsprintf(s, "Q:%i:%i", i, codec->twopass.quant_count[i]);
1207                                    OutputDebugString(s);
1208                            }
1209                    }
1210                    return;
1211            }
1212    }

Legend:
Removed from v.102  
changed lines
  Added in v.365

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4