[svn] / branches / dev-api-4 / xvidcore / src / plugins / plugin_2pass2.c Repository:
ViewVC logotype

Diff of /branches/dev-api-4/xvidcore/src/plugins/plugin_2pass2.c

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

revision 1014, Mon May 12 12:33:16 2003 UTC revision 1030, Fri May 16 17:19:51 2003 UTC
# Line 22  Line 22 
22   * along with this program; if not, write to the Free Software   * along with this program; if not, write to the Free Software
23   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24   *   *
25   * $Id: plugin_2pass2.c,v 1.1.2.3 2003-05-12 12:33:16 suxen_drol Exp $   * $Id: plugin_2pass2.c,v 1.1.2.4 2003-05-16 17:19:51 suxen_drol Exp $
26   *   *
27   *****************************************************************************/   *****************************************************************************/
28    
# Line 41  Line 41 
41          int blks[3];                    /* k,m,y blks */          int blks[3];                    /* k,m,y blks */
42      int length;             /* first pass length */      int length;             /* first pass length */
43      int scaled_length;     /* scaled length */      int scaled_length;     /* scaled length */
44      int desired_length;      int desired_length;     /* desired length; calcuated during encoding */
45    
46        int zone_mode;   /* XVID_ZONE_xxx */
47        double weight;
48  } stat_t;  } stat_t;
49    
50    
# Line 55  Line 58 
58      /* constant statistical data */      /* constant statistical data */
59          int num_frames;          int num_frames;
60      int num_keyframes;      int num_keyframes;
61      uint64_t target;    /* target bitrate */      uint64_t target;    /* target filesize */
62    
63      int count[3];   /* count of each frame types */      int count[3];   /* count of each frame types */
64      uint64_t tot_length[3];  /* total length of each frame types */      uint64_t tot_length[3];  /* total length of each frame types */
# Line 64  Line 67 
67      uint64_t tot_scaled_length[3];  /* total scaled length of each frame type */      uint64_t tot_scaled_length[3];  /* total scaled length of each frame type */
68      int max_length;     /* max frame size */      int max_length;     /* max frame size */
69    
70        /* zone statistical data */
71        double avg_weight;  /* average weight */
72        int64_t tot_quant;   /* total length used by XVID_ZONE_QUANT zones */
73    
74    
75      double curve_comp_scale;      double curve_comp_scale;
76      double movie_curve;      double movie_curve;
77    
# Line 90  Line 98 
98      int KFoverflow;      int KFoverflow;
99      int KFoverflow_partial;      int KFoverflow_partial;
100      int KF_idx;      int KF_idx;
101    
102        double fq_error;
103  } rc_2pass2_t;  } rc_2pass2_t;
104    
105    
# Line 128  Line 138 
138  }  }
139    
140    
 /* scale the curve */  
   
 static void internal_scale(rc_2pass2_t *rc)  
 {  
         int64_t target  = rc->target;  
         int64_t tot_length = rc->tot_length[0] + rc->tot_length[1] + rc->tot_length[2];  
         int min_size[3];  
         double scaler;  
         int i;  
   
         if (target <= 0 || target >= tot_length) {  
                 printf("undersize warning\n");  
         }  
   
         /* perform an initial scale pass.  
            if a frame size is scaled underneath our hardcoded minimums, then we force the  
            frame size to the minimum, and deduct the original & scaled frmae length from the  
            original and target total lengths */  
   
         min_size[0] = ((rc->stats[0].blks[0]*22) + 240) / 8;  
         min_size[1] = (rc->stats[0].blks[0] + 88) / 8;  
         min_size[2] = 8;  
   
   
         scaler = (double)target / (double)tot_length;  
         //printf("target=%i, tot_length=%i, scaler=%f\n", (int)target, (int)tot_length, scaler);  
   
         for (i=0; i<rc->num_frames; i++) {  
                 stat_t * s = &rc->stats[i];  
                 int len;  
   
                 len = (int)((double)s->length * scaler);  
                 if (len < min_size[s->type]) {          /* force frame size */  
                         s->scaled_length = min_size[s->type];  
                         target -= s->scaled_length;  
                         tot_length -= s->length;  
                 }else{  
                         s->scaled_length = 0;  
                 }  
         }  
   
         if (target <= 0 || target >= tot_length) {  
                 printf("undersize warning\n");  
                 return;  
         }  
   
         scaler = (double)target / (double)tot_length;  
         //printf("target=%i, tot_length=%i, scaler=%f\n", (int)target, (int)tot_length, scaler);  
   
         for (i=0; i<rc->num_frames; i++) {  
                 stat_t * s = &rc->stats[i];  
   
                 if (s->scaled_length==0) {      /* ignore frame with forced frame sizes */  
                         s->scaled_length = (int)((double)s->length * scaler);  
                 }  
         }  
   
 }  
   
   
   
   
   
 /* static void internal_scale(rc_2pass2_t *rc)  
 {  
     const double avg_pvop = rc->avg_length[XVID_TYPE_PVOP-1];  
     const double avg_bvop = rc->avg_length[XVID_TYPE_BVOP-1];  
     const uint64_t tot_pvop = rc->tot_length[XVID_TYPE_PVOP-1];  
     const uint64_t tot_bvop = rc->tot_length[XVID_TYPE_BVOP-1];  
     uint64_t i_total = 0;  
         double total1,total2;  
     int i;  
   
     for (i=0; i<rc->num_frames; i++) {  
         stat_t * s = &rc->stats[i];  
   
                 if (s->type == XVID_TYPE_IVOP) {  
                         i_total += s->length + s->length * rc->param.keyframe_boost / 100;  
         }  
         }  
   
         // compensate for avi frame overhead  
         rc->target_size -= rc->num_frames * 24;  
   
         // perform prepass to compensate for over/undersizing  
   
         if (rc->param.use_alt_curve) {  
   
         rc->alt_curve_low = avg_pvop - avg_pvop * (double)rc->param.alt_curve_low_dist / 100.0;  
                 rc->alt_curve_low_diff = avg_pvop - rc->alt_curve_low;  
                 rc->alt_curve_high = avg_pvop + avg_pvop * (double)rc->param.alt_curve_high_dist / 100.0;  
                 rc->alt_curve_high_diff = rc->alt_curve_high - avg_pvop;  
                 if (rc->alt_curve_use_auto) {  
                         if (rc->movie_curve > 1.0) {  
                                 rc->param.alt_curve_min_rel_qual = (int)(100.0 - (100.0 - 100.0 / rc->movie_curve) * (double)rc->param.alt_curve_auto_str / 100.0);  
                                 if (rc->param.alt_curve_min_rel_qual < 20)  
                                         rc->param.alt_curve_min_rel_qual = 20;  
             }else{  
                                 rc->param.alt_curve_min_rel_qual = 100;  
             }  
                 }  
                 rc->alt_curve_mid_qual = (1.0 + (double)rc->param.alt_curve_min_rel_qual / 100.0) / 2.0;  
                 rc->alt_curve_qual_dev = 1.0 - rc->alt_curve_mid_qual;  
   
                 if (rc->param.alt_curve_low_dist > 100) {  
                         switch(rc->param.alt_curve_type) {  
                         case XVID_CURVE_SINE : // Sine Curve (high aggressiveness)  
                                 rc->alt_curve_qual_dev *= 2.0 / (1.0 + sin(DEG2RAD * (avg_pvop * 90.0 / rc->alt_curve_low_diff)));  
                                 rc->alt_curve_mid_qual = 1.0 - rc->alt_curve_qual_dev * sin(DEG2RAD * (avg_pvop * 90.0 / rc->alt_curve_low_diff));  
                                 break;  
                         case XVID_CURVE_LINEAR : // Linear (medium aggressiveness)  
                                 rc->alt_curve_qual_dev *= 2.0 / (1.0 + avg_pvop / rc->alt_curve_low_diff);  
                                 rc->alt_curve_mid_qual = 1.0 - rc->alt_curve_qual_dev * avg_pvop / rc->alt_curve_low_diff;  
                                 break;  
                         case XVID_CURVE_COSINE : // Cosine Curve (low aggressiveness)  
                                 rc->alt_curve_qual_dev *= 2.0 / (1.0 +  (1.0 - cos(DEG2RAD * (avg_pvop * 90.0 / rc->alt_curve_low_diff))));  
                                 rc->alt_curve_mid_qual = 1.0 - rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * (avg_pvop * 90.0 / rc->alt_curve_low_diff)));  
                         }  
                 }  
         }  
   
         total1 = 0;  
         total2 = 0;  
   
     for (i=0; i<rc->num_frames; i++) {  
         stat_t * s = &rc->stats[i];  
   
                 if (s->type != XVID_TYPE_IVOP) {  
   
             double dbytes = s->length / rc->movie_curve;  
             double dbytes2;  
                         total1 += dbytes;  
   
                         if (s->type == XVID_TYPE_BVOP)  
                                 dbytes *= avg_pvop / avg_bvop;  
   
                         if (rc->param.use_alt_curve) {  
                                 if (dbytes > avg_pvop) {  
                     if (dbytes >= rc->alt_curve_high) {  
                                                 dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev);  
                     }else{  
                                                 switch(rc->param.alt_curve_type){  
                                                 case XVID_CURVE_SINE :  
                                                     dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * sin(DEG2RAD * ((dbytes - avg_pvop) * 90.0 / rc->alt_curve_high_diff)));  
                                                         break;  
                                                 case XVID_CURVE_LINEAR :  
                                                     dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (dbytes - avg_pvop) / rc->alt_curve_high_diff);  
                                                         break;  
                                                 case XVID_CURVE_COSINE :  
                                                     dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * ((dbytes - avg_pvop) * 90.0 / rc->alt_curve_high_diff))));  
                                                 }  
                                         }  
                                 }else{  
                     if (dbytes <= rc->alt_curve_low){  
                                                 dbytes2 = dbytes;  
                     }else{  
                                                 switch(rc->param.alt_curve_type){  
                                                 case XVID_CURVE_SINE :  
                                                     dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * sin(DEG2RAD * ((dbytes - avg_pvop) * 90.0 / rc->alt_curve_low_diff)));  
                                                         break;  
                                                 case XVID_CURVE_LINEAR :  
                                                     dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (dbytes - avg_pvop) / rc->alt_curve_low_diff);  
                                                         break;  
                                                 case XVID_CURVE_COSINE :  
                                                     dbytes2 = dbytes * (rc->alt_curve_mid_qual + rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * ((dbytes - avg_pvop) * 90.0 / rc->alt_curve_low_diff))));  
                                                 }  
                                         }  
                                 }  
                         }else{  
                 if (dbytes > avg_pvop) {  
                                         dbytes2 = ((double)dbytes + (avg_pvop - dbytes) *  
                                                 rc->param.curve_compression_high / 100.0);  
                                 }else{  
                                         dbytes2 = ((double)dbytes + (avg_pvop - dbytes) *  
                                                 rc->param.curve_compression_low / 100.0);  
                                 }  
                         }  
   
                         if (s->type == XVID_TYPE_BVOP) {  
                                 dbytes2 *= avg_bvop / avg_pvop;  
             }  
   
             if (dbytes2 < rc->min_length[s->type-1]) {  
                 dbytes = rc->min_length[s->type-1];  
             }  
   
             total2 += dbytes2;  
                 }  
         }  
   
         rc->curve_comp_scale = total1 / total2;  
   
         if (!rc->param.use_alt_curve) {  
                 printf("middle frame size for asymmetric curve compression: %i",  
             (int)(avg_pvop * rc->curve_comp_scale));  
         }  
 }*/  
   
   
141    
142  /* open stats file(s) and read into rc->stats array */  /* open stats file(s) and read into rc->stats array */
143    
# Line 375  Line 186 
186    
187    
188    
   
   
189  static void print_stats(rc_2pass2_t * rc)  static void print_stats(rc_2pass2_t * rc)
190  {  {
191      int i;      int i;
# Line 389  Line 198 
198    
199    
200  /* pre-process the statistics data  /* pre-process the statistics data
201      this is a clone of vfw/src/2pass.c:codec_2pass_init minus file reading, alt_curve, internal scale      - for each type, count, tot_length, min_length, max_length
202        - set keyframes_locations
203  */  */
204    
205  void pre_process0(rc_2pass2_t * rc)  void pre_process0(rc_2pass2_t * rc)
# Line 402  Line 212 
212          rc->last_quant[i] = 0;          rc->last_quant[i] = 0;
213      }      }
214    
     for (i=0; i<32;i++) {  
         rc->pquant_error[i] = 0;  
         rc->bquant_error[i] = 0;  
         rc->quant_count[i] = 0;  
     }  
   
215      for (i=j=0; i<rc->num_frames; i++) {      for (i=j=0; i<rc->num_frames; i++) {
216          stat_t * s = &rc->stats[i];          stat_t * s = &rc->stats[i];
217    
# Line 431  Line 235 
235  }  }
236    
237    
238    /* calculate zone weight "center" */
239    
240    static void zone_process(rc_2pass2_t *rc, const xvid_plg_create_t * create)
241    {
242        int i,j;
243        int n = 0;
244    
245        rc->avg_weight = 0.0;
246        rc->tot_quant = 0;
247    
248    
249        if (create->num_zones == 0) {
250            for (j = 0; j < rc->num_frames; j++) {
251                rc->stats[j].zone_mode = XVID_ZONE_WEIGHT;
252                rc->stats[j].weight = 1.0;
253            }
254            rc->avg_weight += rc->num_frames * 1.0;
255            n += rc->num_frames;
256        }
257    
258    
259        for(i=0; i < create->num_zones; i++) {
260    
261            int next = (i+1<create->num_zones) ? create->zones[i+1].frame : rc->num_frames;
262    
263            if (i==0 && create->zones[i].frame > 0) {
264                for (j = 0; j < create->zones[i].frame && j < rc->num_frames; j++) {
265                    rc->stats[j].zone_mode = XVID_ZONE_WEIGHT;
266                    rc->stats[j].weight = 1.0;
267                }
268                rc->avg_weight += create->zones[i].frame * 1.0;
269                n += create->zones[i].frame;
270            }
271    
272            if (create->zones[i].mode == XVID_ZONE_WEIGHT) {
273                for (j = create->zones[i].frame; j < next && j < rc->num_frames; j++ ) {
274                    rc->stats[j].zone_mode = XVID_ZONE_WEIGHT;
275                    rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base;
276                }
277                next -= create->zones[i].frame;
278                rc->avg_weight += (double)(next * create->zones[i].increment) / (double)create->zones[i].base;
279                n += next;
280            }else{  // XVID_ZONE_QUANT
281                for (j = create->zones[i].frame; j < next && j < rc->num_frames; j++ ) {
282                    rc->stats[j].zone_mode = XVID_ZONE_QUANT;
283                    rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base;
284                    rc->tot_quant += rc->stats[j].length;
285                }
286            }
287        }
288        rc->avg_weight = n>0 ? rc->avg_weight/n : 1.0;
289    
290        /*for (i=0; i < rc->num_frames; i++) {
291            printf("[%i] mode=%i weight=%f\n", i, rc->stats[i].zone_mode, rc->stats[i].weight);
292        }*/
293    
294        printf("center_weight: %f (for %i frames);   fixed_bytes: %i\n", rc->avg_weight, n, rc->tot_quant);
295    }
296    
297    
298    /* scale the curve */
299    
300    static void internal_scale(rc_2pass2_t *rc)
301    {
302            int64_t target  = rc->target - rc->tot_quant;
303            int64_t pass1_length = rc->tot_length[0] + rc->tot_length[1] + rc->tot_length[2] - rc->tot_quant;
304            int min_size[3];
305            double scaler;
306            int i;
307    
308    
309            if (target <= 0 || target >= pass1_length) {
310                    printf("undersize warning\n");
311            }
312    
313            /* perform an initial scale pass.
314               if a frame size is scaled underneath our hardcoded minimums, then we force the
315               frame size to the minimum, and deduct the original & scaled frmae length from the
316               original and target total lengths */
317    
318            min_size[0] = ((rc->stats[0].blks[0]*22) + 240) / 8;
319            min_size[1] = (rc->stats[0].blks[0] + 88) / 8;
320            min_size[2] = 8;
321    
322    
323            scaler = (double)target / (double)pass1_length;
324            //printf("target=%i, tot_length=%i, scaler=%f\n", (int)target, (int)pass1_length, scaler);
325    
326            for (i=0; i<rc->num_frames; i++) {
327                    stat_t * s = &rc->stats[i];
328                    int len;
329    
330            if (s->zone_mode == XVID_ZONE_QUANT) {
331                s->scaled_length = s->length;
332            }else {
333                        len = (int)((double)s->length * scaler * s->weight / rc->avg_weight);
334                        if (len < min_size[s->type]) {              /* force frame size */
335                                s->scaled_length = min_size[s->type];
336                                target -= s->scaled_length;
337                                pass1_length -= s->length;
338                        }else{
339                                s->scaled_length = 0;
340                        }
341            }
342            }
343    
344            if (target <= 0 || target >= pass1_length) {
345                    printf("undersize warning\n");
346                    return;
347            }
348    
349            scaler = (double)target / (double)pass1_length;
350            //printf("target=%i, tot_length=%i, scaler=%f\n", (int)target, (int)tot_length, scaler);
351    
352            for (i=0; i<rc->num_frames; i++) {
353                    stat_t * s = &rc->stats[i];
354    
355                    if (s->scaled_length==0) {      /* ignore frame with forced frame sizes */
356                            s->scaled_length = (int)((double)s->length * scaler * s->weight / rc->avg_weight);
357                    }
358            }
359    
360    }
361    
362    
363    
364    
365  void pre_process1(rc_2pass2_t * rc)  void pre_process1(rc_2pass2_t * rc)
366  {  {
# Line 672  Line 602 
602  {  {
603      xvid_plugin_2pass2_t * param = (xvid_plugin_2pass2_t *)create->param;      xvid_plugin_2pass2_t * param = (xvid_plugin_2pass2_t *)create->param;
604      rc_2pass2_t * rc;      rc_2pass2_t * rc;
605        int i;
606    
607      rc = malloc(sizeof(rc_2pass2_t));      rc = malloc(sizeof(rc_2pass2_t));
608      if (rc == NULL)      if (rc == NULL)
# Line 729  Line 660 
660    
661      /* pre-process our stats */      /* pre-process our stats */
662    
         {  
663                  if (rc->num_frames  < create->fbase/create->fincr) {                  if (rc->num_frames  < create->fbase/create->fincr) {
664                          rc->target = rc->param.bitrate / 8;     /* one second */                          rc->target = rc->param.bitrate / 8;     /* one second */
665                  }else{                  }else{
666                          rc->target = (rc->param.bitrate * rc->num_frames * create->fincr) / (create->fbase * 8);                          rc->target = (rc->param.bitrate * rc->num_frames * create->fincr) / (create->fbase * 8);
667                  }                  }
668    
   
669                  rc->target -= rc->num_frames*24;        /* avi file header */                  rc->target -= rc->num_frames*24;        /* avi file header */
670    
         }  
   
671    
672          pre_process0(rc);          pre_process0(rc);
673          if (rc->param.bitrate) {          if (rc->param.bitrate) {
674            zone_process(rc, create);
675                  internal_scale(rc);                  internal_scale(rc);
676        }else{
677            /* external scaler: ignore zone */
678            for (i=0;i<rc->num_frames;i++) {
679                rc->stats[i].zone_mode = XVID_ZONE_WEIGHT;
680                rc->stats[i].weight = 1.0;
681            }
682            rc->avg_weight = 1.0;
683            rc->tot_quant = 0;
684          }          }
685          pre_process1(rc);pre_process1(rc);pre_process1(rc);          pre_process1(rc);
686    
687        for (i=0; i<32;i++) {
688            rc->pquant_error[i] = 0;
689            rc->bquant_error[i] = 0;
690            rc->quant_count[i] = 0;
691        }
692    
693        rc->fq_error = 0;
694    
695      *handle = rc;      *handle = rc;
696          return(0);          return(0);
# Line 772  Line 716 
716      double curve_temp;      double curve_temp;
717      int capped_to_max_framesize = 0;      int capped_to_max_framesize = 0;
718    
719        if (data->quant <= 0) {
720    
721            if (s->zone_mode == XVID_ZONE_QUANT) {
722    
723                rc->fq_error += s->weight;
724                data->quant = (int)rc->fq_error;
725                rc->fq_error -= data->quant;
726    
727            }else { /* XVID_ZONE_WEIGHT */
728    
729      if (data->frame_num >= rc->num_frames) {      if (data->frame_num >= rc->num_frames) {
730          /* insufficent stats data */          /* insufficent stats data */
731          return 0;          return 0;
# Line 1009  Line 963 
963          rc->last_quant[s->type-1] = data->quant;          rc->last_quant[s->type-1] = data->quant;
964          }          }
965    
966    
967            }   /* if */
968    
969        }
970    
971          return 0;          return 0;
972  }  }
973    

Legend:
Removed from v.1014  
changed lines
  Added in v.1030

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