1 /******************************************************************************
5 * Copyright (C) 2002 Edouard Gomez <ed.gomez@wanadoo.fr>
7 * The curve treatment algorithm is based on work done by Foxer <email?> and
8 * Dirk Knop <dknop@gwdg.de> for the XviD vfw dynamic library.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *****************************************************************************/
26 /* Standard Headers */
36 /******************************************************************************
37 * Build time constants
38 *****************************************************************************/
42 * Perhaps the msvc headers define Pi with another constant name
44 #define DEG2RAD (M_PI / 180.0)
46 /* Defaults settings will be computed with the help of these constants */
47 #define DEFAULT_DESIRED_SIZE 700
48 #define DEFAULT_AUDIO_BITRATE 128
49 #define DEFAULT_MOVIE_LENGTH 2
50 #define DEFAULT_TWOPASS_BOOST 1000
51 #define DEFAULT_FPS 25.0f
52 #define DEFAULT_CREDITS_SIZE 0
54 #define DEFAULT_XVID_DBG_FILE "xvid.dbg"
55 #define DEFAULT_XVID_STATS_FILE "xvid.stats"
58 /******************************************************************************
60 *****************************************************************************/
62 /* Sub vbrInit cases functions */
63 static vbr_init_function vbr_init_dummy
;
64 static vbr_init_function vbr_init_2pass1
;
65 static vbr_init_function vbr_init_2pass2
;
66 static vbr_init_function vbr_init_fixedquant
;
68 /* Sub vbrGetQuant cases functions */
69 static vbr_get_quant_function vbr_getquant_1pass
;
70 static vbr_get_quant_function vbr_getquant_2pass1
;
71 static vbr_get_quant_function vbr_getquant_2pass2
;
72 static vbr_get_quant_function vbr_getquant_fixedquant
;
74 /* Sub vbrGetIntra cases functions */
75 static vbr_get_intra_function vbr_getintra_1pass
;
76 static vbr_get_intra_function vbr_getintra_2pass1
;
77 static vbr_get_intra_function vbr_getintra_2pass2
;
78 static vbr_get_intra_function vbr_getintra_fixedquant
;
80 /* Sub vbrUpdate prototypes */
81 static vbr_update_function vbr_update_dummy
;
82 static vbr_update_function vbr_update_2pass1
;
83 static vbr_update_function vbr_update_2pass2
;
85 /* Sub vbrFinish cases functions */
86 static vbr_finish_function vbr_finish_dummy
;
87 static vbr_finish_function vbr_finish_2pass1
;
88 static vbr_finish_function vbr_finish_2pass2
;
90 /* Is the encoder in the credits */
91 #define FRAME_TYPE_NORMAL_MOVIE 0x00
92 #define FRAME_TYPE_STARTING_CREDITS 0x01
93 #define FRAME_TYPE_ENDING_CREDITS 0x02
95 /******************************************************************************
96 * Inline utility functions
97 *****************************************************************************/
99 static __inline
int util_frametype(vbr_control_t
*state
)
102 if(state
->credits_start
) {
104 if(state
->cur_frame
>= state
->credits_start_begin
&&
105 state
->cur_frame
< state
->credits_start_end
)
106 return(FRAME_TYPE_STARTING_CREDITS
);
110 if(state
->credits_end
) {
112 if(state
->cur_frame
>= state
->credits_end_begin
&&
113 state
->cur_frame
< state
->credits_end_end
)
114 return(FRAME_TYPE_ENDING_CREDITS
);
118 return(FRAME_TYPE_NORMAL_MOVIE
);
123 static __inline
int util_creditsframes(vbr_control_t
*state
)
128 if(state
->credits_start
)
129 frames
+= state
->credits_start_end
- state
->credits_start_begin
;
130 if(state
->credits_end
)
131 frames
+= state
->credits_end_end
- state
->credits_end_begin
;
137 /******************************************************************************
139 *****************************************************************************/
141 /*****************************************************************************
142 * Function description :
144 * This function initialiazes the vbr_control_t with safe defaults for all
149 ****************************************************************************/
151 int vbrSetDefaults(vbr_control_t
*state
)
154 /* Set all the structure to zero */
155 memset(state
, 0, sizeof(state
));
157 /* Default mode is CBR */
158 state
->mode
= VBR_MODE_1PASS
;
160 /* Default statistic filename */
161 state
->filename
= DEFAULT_XVID_STATS_FILE
;
164 * Default is a 2hour movie on 700Mo CD-ROM + 128kbit sound track
165 * This represents a target bitrate of 687kbit/s
167 state
->desired_size
= DEFAULT_DESIRED_SIZE
*1024*1024 -
168 DEFAULT_MOVIE_LENGTH
*3600*DEFAULT_AUDIO_BITRATE
*1000/8;
169 state
->desired_bitrate
= state
->desired_size
*8/(DEFAULT_MOVIE_LENGTH
*3600);
172 state
->credits_mode
= VBR_CREDITS_MODE_RATE
;
173 state
->credits_start
= 0;
174 state
->credits_start_begin
= 0;
175 state
->credits_start_end
= 0;
176 state
->credits_end
= 0;
177 state
->credits_end_begin
= 0;
178 state
->credits_end_end
= 0;
179 state
->credits_quant_ratio
= 20;
180 state
->credits_fixed_quant
= 20;
181 state
->credits_quant_i
= 20;
182 state
->credits_quant_p
= 20;
183 state
->credits_start_size
= DEFAULT_CREDITS_SIZE
*1024*1024;
184 state
->credits_end_size
= DEFAULT_CREDITS_SIZE
*1024*1024;
187 state
->keyframe_boost
= 0;
188 state
->kftreshold
= 10;
189 state
->kfreduction
= 30;
190 state
->min_key_interval
= 1;
191 state
->max_key_interval
= (int)DEFAULT_FPS
*10;
193 /* Normal curve treatment */
194 state
->curve_compression_high
= 25;
195 state
->curve_compression_low
= 10;
198 state
->use_alt_curve
= 1;
199 state
->alt_curve_type
= VBR_ALT_CURVE_LINEAR
;
200 state
->alt_curve_low_dist
= 90;
201 state
->alt_curve_high_dist
= 500;
202 state
->alt_curve_min_rel_qual
= 50;
203 state
->alt_curve_use_auto
= 1;
204 state
->alt_curve_auto_str
= 30;
205 state
->alt_curve_use_auto_bonus_bias
= 1;
206 state
->alt_curve_bonus_bias
= 50;
207 state
->bitrate_payback_method
= VBR_PAYBACK_BIAS
;
208 state
->bitrate_payback_delay
= 250;
209 state
->twopass_max_bitrate
= DEFAULT_TWOPASS_BOOST
*state
->desired_bitrate
;
210 state
->twopass_max_overflow_improvement
= 60;
211 state
->twopass_max_overflow_degradation
= 60;
212 state
->max_iquant
= 31;
213 state
->min_iquant
= 2;
214 state
->max_pquant
= 31;
215 state
->min_pquant
= 2;
216 state
->fixed_quant
= 3;
218 state
->max_framesize
= (1.0/(float)DEFAULT_FPS
) * state
->twopass_max_bitrate
/ 8;
220 state
->fps
= (float)DEFAULT_FPS
;
226 /*****************************************************************************
227 * Function description :
229 * This function initialiaze the vbr_control_t state passed in parameter.
231 * The initialization depends on state->mode, there are 4 modes allowed.
232 * Each mode description is done in the README file shipped with the lib.
238 *****************************************************************************/
240 int vbrInit(vbr_control_t
*state
)
243 if(state
== NULL
) return(-1);
245 /* Function pointers safe initialization */
247 state
->getquant
= NULL
;
248 state
->getintra
= NULL
;
249 state
->update
= NULL
;
250 state
->finish
= NULL
;
254 state
->debug_file
= fopen(DEFAULT_XVID_DBG_FILE
, "w+");
256 if(state
->debug_file
== NULL
)
259 fprintf(state
->debug_file
, "# XviD Debug output\n");
260 fprintf(state
->debug_file
, "# quant | intra | header bytes"
261 "| total bytes | kblocks | mblocks | ublocks"
262 "| vbr overflow | vbr kf overflow"
263 "| vbr kf partial overflow\n\n");
266 /* Function pointers sub case initialization */
267 switch(state
->mode
) {
269 state
->init
= vbr_init_dummy
;
270 state
->getquant
= vbr_getquant_1pass
;
271 state
->getintra
= vbr_getintra_1pass
;
272 state
->update
= vbr_update_dummy
;
273 state
->finish
= vbr_finish_dummy
;
275 case VBR_MODE_2PASS_1
:
276 state
->init
= vbr_init_2pass1
;
277 state
->getquant
= vbr_getquant_2pass1
;
278 state
->getintra
= vbr_getintra_2pass1
;
279 state
->update
= vbr_update_2pass1
;
280 state
->finish
= vbr_finish_2pass1
;
282 case VBR_MODE_FIXED_QUANT
:
283 state
->init
= vbr_init_fixedquant
;
284 state
->getquant
= vbr_getquant_fixedquant
;
285 state
->getintra
= vbr_getintra_fixedquant
;
286 state
->update
= vbr_update_dummy
;
287 state
->finish
= vbr_finish_dummy
;
289 case VBR_MODE_2PASS_2
:
290 state
->init
= vbr_init_2pass2
;
291 state
->getintra
= vbr_getintra_2pass2
;
292 state
->getquant
= vbr_getquant_2pass2
;
293 state
->update
= vbr_update_2pass2
;
294 state
->finish
= vbr_finish_2pass2
;
300 return(state
->init(state
));
304 /******************************************************************************
305 * Function description :
307 * This function returns an adapted quantizer according to the current vbr
311 * the quantizer value (0 <= value <= 31)
312 * (0 is a special case, means : let XviD decide)
314 *****************************************************************************/
316 int vbrGetQuant(vbr_control_t
*state
)
319 /* Returns Zero, so XviD decides alone */
320 if(state
== NULL
|| state
->getquant
== NULL
) return(0);
322 return(state
->getquant(state
));
326 /******************************************************************************
327 * Function description :
329 * This function returns the type of the frame to be encoded next (I or P/B)
332 * = -1 let the XviD encoder decide wether or not the next frame is I
336 *****************************************************************************/
338 int vbrGetIntra(vbr_control_t
*state
)
341 /* Returns -1, means let XviD decide */
342 if(state
== NULL
|| state
->getintra
== NULL
) return(-1);
344 return(state
->getintra(state
));
348 /******************************************************************************
349 * Function description :
351 * This function updates the vbr control state according to collected statistics
358 *****************************************************************************/
360 int vbrUpdate(vbr_control_t
*state
,
370 if(state
== NULL
|| state
->update
== NULL
) return(-1);
372 if(state
->debug
&& state
->debug_file
!= NULL
) {
375 fprintf(state
->debug_file
, "%d %d %d %d %d %d %d %d %d %d\n",
376 quant
, intra
, header_bytes
, total_bytes
, kblocks
,
377 mblocks
, ublocks
, state
->overflow
, state
->KFoverflow
,
378 state
->KFoverflow_partial
);
389 state
->debug_quant_count
[idx
]++;
393 return(state
->update(state
, quant
, intra
, header_bytes
, total_bytes
,
394 kblocks
, mblocks
, ublocks
));
398 /******************************************************************************
399 * Function description :
401 * This function stops the vbr controller
407 *****************************************************************************/
409 int vbrFinish(vbr_control_t
*state
)
412 if(state
== NULL
|| state
->finish
== NULL
) return(-1);
414 if(state
->debug
&& state
->debug_file
!= NULL
) {
418 fprintf(state
->debug_file
, "\n\n");
421 fprintf(state
->debug_file
, "#");
423 fprintf(state
->debug_file
, "\n# Quantizer distribution :\n\n");
427 fprintf(state
->debug_file
, "# quant %d : %d\n",
429 state
->debug_quant_count
[i
]);
433 fclose(state
->debug_file
);
437 return(state
->finish(state
));
441 /******************************************************************************
442 * Dummy functions - Used when a mode does not need such a function
443 *****************************************************************************/
445 static int vbr_init_dummy(void *sstate
)
448 vbr_control_t
*state
= sstate
;
450 state
->cur_frame
= 0;
456 static int vbr_update_dummy(void *state
,
466 ((vbr_control_t
*)state
)->cur_frame
++;
472 static int vbr_finish_dummy(void *state
)
479 /******************************************************************************
480 * 1 pass mode - XviD will do its job alone.
481 *****************************************************************************/
483 static int vbr_getquant_1pass(void *state
)
490 static int vbr_getintra_1pass(void *state
)
497 /******************************************************************************
498 * 2 pass mode - first pass functions
499 *****************************************************************************/
501 static int vbr_init_2pass1(void *sstate
)
505 vbr_control_t
*state
= sstate
;
507 /* Check the filename */
508 if(state
->filename
== NULL
|| state
->filename
[0] == '\0')
511 /* Initialize safe defaults for 2pass 1 */
512 state
->pass1_file
= NULL
;
513 state
->nb_frames
= 0;
514 state
->nb_keyframes
= 0;
515 state
->cur_frame
= 0;
517 /* Open the 1st pass file */
518 if((f
= fopen(state
->filename
, "w+")) == NULL
)
524 * The extra white spaces will be used during the vbrFinish to write
525 * the resulting number of frames and keyframes (10 spaces == maximum
526 * string length of an int on 32bit machines, i don't think anyone is
527 * encoding more than 4 billion frames :-)
529 fprintf(f
, "# ASCII XviD vbr stat file version %d\n#\n", VBR_VERSION
);
530 fprintf(f
, "# frames : \n");
531 fprintf(f
, "# keyframes : \n");
532 fprintf(f
, "#\n# quant | intra | header bytes | total bytes | kblocks |"
533 " mblocks | ublocks\n\n");
535 /* Save file pointer */
536 state
->pass1_file
= f
;
542 static int vbr_getquant_2pass1(void *state
)
549 static int vbr_getintra_2pass1(void *state
)
556 static int vbr_update_2pass1(void *sstate
,
568 vbr_control_t
*state
= sstate
;
570 if(state
->pass1_file
== NULL
)
573 /* Writes the resulting statistics */
574 fprintf(state
->pass1_file
, "%d %d %d %d %d %d %d\n",
583 /* Update vbr control state */
584 if(intra
) state
->nb_keyframes
++;
592 static int vbr_finish_2pass1(void *sstate
)
596 vbr_control_t
*state
= sstate
;
598 if(state
->pass1_file
== NULL
)
601 /* Goto to the file beginning */
602 fseek(state
->pass1_file
, 0, SEEK_SET
);
604 /* Skip the version line and the empty line */
607 c
= fgetc(state
->pass1_file
);
609 if(c
== EOF
) return(-1);
614 /* Prepare to write to the stream */
615 fseek( state
->pass1_file
, 0L, SEEK_CUR
);
617 /* Overwrite the frame field - safe as we have written extra spaces */
618 fprintf(state
->pass1_file
, "# frames : %.10d\n", state
->nb_frames
);
620 /* Overwrite the keyframe field */
621 fprintf(state
->pass1_file
, "# keyframes : %.10d\n",
622 state
->nb_keyframes
);
625 if(fclose(state
->pass1_file
) != 0)
632 /******************************************************************************
633 * 2 pass mode - 2nd pass functions (Need to be finished)
634 *****************************************************************************/
636 static int vbr_init_2pass2(void *sstate
)
640 int c
, n
, pos_firstframe
, credits_frames
;
641 long long credits1_bytes
;
642 long long credits2_bytes
;
644 long long total_bytes
;
645 long long itotal_bytes
;
646 long long start_curved
;
647 long long end_curved
;
651 vbr_control_t
*state
= sstate
;
653 /* Check the filename */
654 if(state
->filename
== NULL
|| state
->filename
[0] == '\0')
657 /* Initialize safe defaults for 2pass 2 */
658 state
->pass1_file
= NULL
;
659 state
->nb_frames
= 0;
660 state
->nb_keyframes
= 0;
662 /* Open the 1st pass file */
663 if((f
= fopen(state
->filename
, "r")) == NULL
)
666 state
->pass1_file
= f
;
668 /* Get the file version and check against current version */
669 fscanf(state
->pass1_file
, "# ASCII XviD vbr stat file version %d\n", &n
);
671 if(n
!= VBR_VERSION
) {
672 fclose(state
->pass1_file
);
673 state
->pass1_file
= NULL
;
677 /* Skip the blank commented line */
681 c
= fgetc(state
->pass1_file
);
684 fclose(state
->pass1_file
);
685 state
->pass1_file
= NULL
;
694 /* Get the number of frames */
695 fscanf(state
->pass1_file
, "# frames : %d\n", &state
->nb_frames
);
697 /* Compute the desired size */
698 state
->desired_size
= (long long)
699 (((long long)state
->nb_frames
* (long long)state
->desired_bitrate
) /
702 /* Get the number of keyframes */
703 fscanf(state
->pass1_file
, "# keyframes : %d\n", &state
->nb_keyframes
);
705 /* Allocate memory space for the keyframe_location array */
706 if((state
->keyframe_locations
707 = (int*)malloc((state
->nb_keyframes
+1)*sizeof(int))) == NULL
) {
708 fclose(state
->pass1_file
);
709 state
->pass1_file
= NULL
;
713 /* Skip the blank commented line and the colum description */
717 c
= fgetc(state
->pass1_file
);
720 fclose(state
->pass1_file
);
721 state
->pass1_file
= NULL
;
729 /* Save position for future use */
730 pos_firstframe
= ftell(state
->pass1_file
);
732 /* Read and initialize some variables */
733 credits1_bytes
= credits2_bytes
= 0;
734 total_bytes
= itotal_bytes
= 0;
735 start_curved
= end_curved
= 0;
738 for(state
->cur_frame
= c
= 0; state
->cur_frame
<state
->nb_frames
; state
->cur_frame
++) {
740 int quant
, keyframe
, frame_hbytes
, frame_bytes
;
741 int kblocks
, mblocks
, ublocks
;
743 fscanf(state
->pass1_file
, "%d %d %d %d %d %d %d\n",
744 &quant
, &keyframe
, &frame_hbytes
, &frame_bytes
,
745 &kblocks
, &mblocks
, &ublocks
);
747 /* Is the frame in the beginning credits */
748 if(util_frametype(state
) == FRAME_TYPE_STARTING_CREDITS
) {
749 credits1_bytes
+= frame_bytes
;
754 /* Is the frame in the eding credits */
755 if(util_frametype(state
) == FRAME_TYPE_ENDING_CREDITS
) {
756 credits2_bytes
+= frame_bytes
;
761 /* We only care about Keyframes when not in credits */
763 itotal_bytes
+= frame_bytes
+ frame_bytes
*
764 state
->keyframe_boost
/ 100;
765 total_bytes
+= frame_bytes
*
766 state
->keyframe_boost
/ 100;
767 state
->keyframe_locations
[c
++] = state
->cur_frame
;
770 total_bytes
+= frame_bytes
;
775 * Last frame is treated like an I Frame so we can dispatch overflow
776 * all other the last film segment
778 state
->keyframe_locations
[c
] = state
->cur_frame
;
780 desired
= state
->desired_size
;
782 switch(state
->credits_mode
) {
783 case VBR_CREDITS_MODE_QUANT
:
785 state
->movie_curve
= (double)
786 (total_bytes
- credits1_bytes
- credits2_bytes
) /
787 (desired
- credits1_bytes
- credits2_bytes
);
789 start_curved
= credits1_bytes
;
790 end_curved
= credits2_bytes
;
793 case VBR_CREDITS_MODE_SIZE
:
795 /* start curve = (start / start desired size) */
796 state
->credits_start_curve
= (double)
797 (credits1_bytes
/ state
->credits_start_size
);
799 /* end curve = (end / end desired size) */
800 state
->credits_end_curve
= (double)
801 (credits2_bytes
/ state
->credits_end_size
);
803 start_curved
= (long long)
804 (credits1_bytes
/ state
->credits_start_curve
);
806 end_curved
= (long long)
807 (credits2_bytes
/ state
->credits_end_curve
);
809 /* movie curve=(total-credits)/(desired_size-curved credits) */
810 state
->movie_curve
= (double)
811 (total_bytes
- credits1_bytes
- credits2_bytes
) /
812 (desired
- start_curved
- end_curved
);
815 case VBR_CREDITS_MODE_RATE
:
818 /* credits curve = (total/desired_size)*(100/credits_rate) */
819 state
->credits_start_curve
= state
->credits_end_curve
=
820 ((double)total_bytes
/ desired
) *
821 ((double)100 / state
->credits_quant_ratio
);
824 (long long)(credits1_bytes
/state
->credits_start_curve
);
827 (long long)(credits2_bytes
/state
->credits_end_curve
);
829 state
->movie_curve
= (double)
830 (total_bytes
- credits1_bytes
- credits2_bytes
) /
831 (desired
- start_curved
- end_curved
);
837 * average frame size = (desired - curved credits - curved keyframes) /
838 * (frames - credits frames - keyframes)
840 state
->average_frame
= (double)
841 (desired
- start_curved
- end_curved
-
842 (itotal_bytes
/ state
->movie_curve
)) /
843 (state
->nb_frames
- util_creditsframes(state
) -
844 state
->nb_keyframes
);
846 /* Initialize alt curve parameters */
847 if (state
->use_alt_curve
) {
849 state
->alt_curve_low
=
850 state
->average_frame
- state
->average_frame
*
851 (double)(state
->alt_curve_low_dist
/ 100.0);
853 state
->alt_curve_low_diff
=
854 state
->average_frame
- state
->alt_curve_low
;
856 state
->alt_curve_high
=
857 state
->average_frame
+ state
->average_frame
*
858 (double)(state
->alt_curve_high_dist
/ 100.0);
860 state
->alt_curve_high_diff
=
861 state
->alt_curve_high
- state
->average_frame
;
863 if (state
->alt_curve_use_auto
) {
865 if (state
->movie_curve
> 1.0) {
867 state
->alt_curve_min_rel_qual
=
868 (int)(100.0 - (100.0 - 100.0 / state
->movie_curve
) *
869 (double)state
->alt_curve_auto_str
/ 100.0);
871 if (state
->alt_curve_min_rel_qual
< 20)
872 state
->alt_curve_min_rel_qual
= 20;
875 state
->alt_curve_min_rel_qual
= 100;
880 state
->alt_curve_mid_qual
=
881 (1.0 + (double)state
->alt_curve_min_rel_qual
/ 100.0) / 2.0;
883 state
->alt_curve_qual_dev
= 1.0 - state
->alt_curve_mid_qual
;
885 if (state
->alt_curve_low_dist
> 100) {
887 switch(state
->alt_curve_type
) {
888 case VBR_ALT_CURVE_AGGRESIVE
:
889 /* Sine Curve (high aggressiveness) */
890 state
->alt_curve_qual_dev
*=
892 (1.0 + sin(DEG2RAD
* (state
->average_frame
* 90.0 / state
->alt_curve_low_diff
)));
894 state
->alt_curve_mid_qual
=
895 1.0 - state
->alt_curve_qual_dev
*
896 sin(DEG2RAD
* (state
->average_frame
* 90.0 / state
->alt_curve_low_diff
));
900 case VBR_ALT_CURVE_LINEAR
:
901 /* Linear (medium aggressiveness) */
902 state
->alt_curve_qual_dev
*=
904 (1.0 + state
->average_frame
/ state
->alt_curve_low_diff
);
906 state
->alt_curve_mid_qual
=
907 1.0 - state
->alt_curve_qual_dev
*
908 state
->average_frame
/ state
->alt_curve_low_diff
;
912 case VBR_ALT_CURVE_SOFT
:
913 /* Cosine Curve (low aggressiveness) */
914 state
->alt_curve_qual_dev
*=
916 (1.0 + (1.0 - cos(DEG2RAD
* (state
->average_frame
* 90.0 / state
->alt_curve_low_diff
))));
918 state
->alt_curve_mid_qual
=
919 1.0 - state
->alt_curve_qual_dev
*
920 (1.0 - cos(DEG2RAD
* (state
->average_frame
* 90.0 / state
->alt_curve_low_diff
)));
927 /* Go to the first non credits frame stats line into file */
928 fseek(state
->pass1_file
, pos_firstframe
, SEEK_SET
);
930 /* Perform prepass to compensate for over/undersizing */
931 total1
= total2
= 0.0;
932 for(state
->cur_frame
=0; state
->cur_frame
<state
->nb_frames
; state
->cur_frame
++) {
934 int quant
, keyframe
, frame_hbytes
, frame_bytes
;
935 int kblocks
, mblocks
, ublocks
;
937 fscanf(state
->pass1_file
, "%d %d %d %d %d %d %d\n",
938 &quant
, &keyframe
, &frame_hbytes
, &frame_bytes
,
939 &kblocks
, &mblocks
, &ublocks
);
941 if(util_frametype(state
) != FRAME_TYPE_NORMAL_MOVIE
)
946 double dbytes
= frame_bytes
/ state
->movie_curve
;
949 if (state
->use_alt_curve
) {
951 if (dbytes
> state
->average_frame
) {
953 if (dbytes
>= state
->alt_curve_high
) {
954 total2
+= dbytes
* (state
->alt_curve_mid_qual
- state
->alt_curve_qual_dev
);
958 switch(state
->alt_curve_type
) {
959 case VBR_ALT_CURVE_AGGRESIVE
:
963 (state
->alt_curve_mid_qual
- state
->alt_curve_qual_dev
*
964 sin(DEG2RAD
* ((dbytes
- state
->average_frame
) * 90.0 / state
->alt_curve_high_diff
)));
967 case VBR_ALT_CURVE_LINEAR
:
971 (state
->alt_curve_mid_qual
- state
->alt_curve_qual_dev
*
972 (dbytes
- state
->average_frame
) / state
->alt_curve_high_diff
);
974 case VBR_ALT_CURVE_SOFT
:
977 (state
->alt_curve_mid_qual
- state
->alt_curve_qual_dev
*
978 (1.0 - cos(DEG2RAD
* ((dbytes
- state
->average_frame
) * 90.0 / state
->alt_curve_high_diff
))));
984 if (dbytes
<= state
->alt_curve_low
) {
989 switch(state
->alt_curve_type
) {
990 case VBR_ALT_CURVE_AGGRESIVE
:
993 (state
->alt_curve_mid_qual
- state
->alt_curve_qual_dev
*
994 sin(DEG2RAD
* ((dbytes
- state
->average_frame
) * 90.0 / state
->alt_curve_low_diff
)));
997 case VBR_ALT_CURVE_LINEAR
:
1000 (state
->alt_curve_mid_qual
- state
->alt_curve_qual_dev
*
1001 (dbytes
- state
->average_frame
) / state
->alt_curve_low_diff
);
1003 case VBR_ALT_CURVE_SOFT
:
1006 (state
->alt_curve_mid_qual
+ state
->alt_curve_qual_dev
*
1007 (1.0 - cos(DEG2RAD
* ((dbytes
- state
->average_frame
) * 90.0 / state
->alt_curve_low_diff
))));
1013 if (dbytes
> state
->average_frame
) {
1016 (state
->average_frame
- dbytes
) *
1017 state
->curve_compression_high
/ 100.0);
1022 (state
->average_frame
- dbytes
) *
1023 state
->curve_compression_low
/ 100.0);
1029 state
->curve_comp_scale
= total1
/ total2
;
1031 if (state
->use_alt_curve
) {
1033 double curve_temp
, dbytes
;
1034 int newquant
, percent
;
1037 if (state
->alt_curve_use_auto_bonus_bias
)
1038 state
->alt_curve_bonus_bias
= state
->alt_curve_min_rel_qual
;
1040 state
->curve_bias_bonus
=
1041 (total1
- total2
) * (double)state
->alt_curve_bonus_bias
/
1042 (100.0 * (double)(state
->nb_frames
- util_creditsframes(state
) - state
->nb_keyframes
));
1043 state
->curve_comp_scale
=
1044 ((total1
- total2
) * (1.0 - (double)state
->alt_curve_bonus_bias
/ 100.0) + total2
) /
1048 for (n
=1; n
<= (int)(state
->alt_curve_high
*2) + 1; n
++) {
1050 if (dbytes
> state
->average_frame
)
1052 if (dbytes
>= state
->alt_curve_high
) {
1053 curve_temp
= dbytes
* (state
->alt_curve_mid_qual
- state
->alt_curve_qual_dev
);
1056 switch(state
->alt_curve_type
) {
1057 case VBR_ALT_CURVE_AGGRESIVE
:
1058 curve_temp
= dbytes
* (state
->alt_curve_mid_qual
- state
->alt_curve_qual_dev
*
1059 sin(DEG2RAD
* ((dbytes
- state
->average_frame
) * 90.0 / state
->alt_curve_high_diff
)));
1062 case VBR_ALT_CURVE_LINEAR
:
1063 curve_temp
= dbytes
* (state
->alt_curve_mid_qual
- state
->alt_curve_qual_dev
*
1064 (dbytes
- state
->average_frame
) / state
->alt_curve_high_diff
);
1066 case VBR_ALT_CURVE_SOFT
:
1067 curve_temp
= dbytes
* (state
->alt_curve_mid_qual
- state
->alt_curve_qual_dev
*
1068 (1.0 - cos(DEG2RAD
* ((dbytes
- state
->average_frame
) * 90.0 / state
->alt_curve_high_diff
))));
1073 if (dbytes
<= state
->alt_curve_low
) {
1074 curve_temp
= dbytes
;
1077 switch(state
->alt_curve_type
) {
1078 case VBR_ALT_CURVE_AGGRESIVE
:
1079 curve_temp
= dbytes
* (state
->alt_curve_mid_qual
- state
->alt_curve_qual_dev
*
1080 sin(DEG2RAD
* ((dbytes
- state
->average_frame
) * 90.0 / state
->alt_curve_low_diff
)));
1083 case VBR_ALT_CURVE_LINEAR
:
1084 curve_temp
= dbytes
* (state
->alt_curve_mid_qual
- state
->alt_curve_qual_dev
*
1085 (dbytes
- state
->average_frame
) / state
->alt_curve_low_diff
);
1087 case VBR_ALT_CURVE_SOFT
:
1088 curve_temp
= dbytes
* (state
->alt_curve_mid_qual
+ state
->alt_curve_qual_dev
*
1089 (1.0 - cos(DEG2RAD
* ((dbytes
- state
->average_frame
) * 90.0 / state
->alt_curve_low_diff
))));
1094 if (state
->movie_curve
> 1.0)
1095 dbytes
*= state
->movie_curve
;
1097 newquant
= (int)(dbytes
* 2.0 / (curve_temp
* state
->curve_comp_scale
+ state
->curve_bias_bonus
));
1100 if (newquant
!= oldquant
)
1102 oldquant
= newquant
;
1103 percent
= (int)((n
- state
->average_frame
) * 100.0 / state
->average_frame
);
1112 state
->overflow
= 0;
1113 state
->KFoverflow
= 0;
1114 state
->KFoverflow_partial
= 0;
1117 for (n
=0 ; n
< 32 ; n
++) {
1118 state
->quant_error
[n
] = 0.0;
1119 state
->quant_count
[n
] = 0;
1122 state
->curve_comp_error
= 0.0;
1123 state
->last_quant
= 0;
1126 * Above this frame size limit, normal vbr rules will not apply
1128 * 1 - Quant can de/increase more than -/+2 between 2 frames
1129 * 2 - Leads to artifacts because of 1
1131 state
->max_framesize
= state
->twopass_max_bitrate
/state
->fps
;
1133 /* Get back to the beginning of frame statistics */
1134 fseek(state
->pass1_file
, pos_firstframe
, SEEK_SET
);
1137 * Small hack : We have to get next frame stats before the
1138 * getintra/quant calls
1139 * User clients update the data when they call vbrUpdate
1140 * we are just bypassing this because we don't have to update
1141 * the overflow and so on...
1146 int next_hbytes
, next_kblocks
, next_mblocks
, next_ublocks
;
1148 fscanf(state
->pass1_file
, "%d %d %d %d %d %d %d\n",
1149 &state
->pass1_quant
, &state
->pass1_intra
, &next_hbytes
,
1150 &state
->pass1_bytes
, &next_kblocks
, &next_mblocks
,
1155 /* Initialize the frame counter */
1156 state
->cur_frame
= 0;
1157 state
->last_keyframe
= 0;
1163 static int vbr_getquant_2pass2(void *sstate
)
1170 int capped_to_max_framesize
= 0;
1171 int KFdistance
, KF_min_size
;
1172 vbr_control_t
*state
= sstate
;
1174 bytes1
= state
->pass1_bytes
;
1175 overflow
= state
->overflow
/ 8;
1176 /* To shut up gcc warning */
1180 if (state
->pass1_intra
)
1185 if (util_frametype(state
) != FRAME_TYPE_NORMAL_MOVIE
) {
1188 switch (state
->credits_mode
) {
1189 case VBR_CREDITS_MODE_QUANT
:
1190 if (state
->credits_quant_i
!= state
->credits_quant_p
) {
1191 quant
= state
->pass1_intra
?
1192 state
->credits_quant_i
:
1193 state
->credits_quant_p
;
1196 quant
= state
->credits_quant_p
;
1199 state
->bytes1
= bytes1
;
1200 state
->bytes2
= bytes1
;
1201 state
->desired_bytes2
= bytes1
;
1204 case VBR_CREDITS_MODE_RATE
:
1205 case VBR_CREDITS_MODE_SIZE
:
1206 if(util_frametype(state
) == FRAME_TYPE_STARTING_CREDITS
)
1207 bytes2
= (int)(bytes1
/ state
->credits_start_curve
);
1209 bytes2
= (int)(bytes1
/ state
->credits_end_curve
);
1214 /* Foxer: apply curve compression outside credits */
1215 double dbytes
, curve_temp
;
1219 if (state
->pass1_intra
)
1220 dbytes
= ((int)(bytes2
+ bytes2
* state
->keyframe_boost
/ 100)) /
1223 dbytes
= bytes2
/ state
->movie_curve
;
1225 /* spread the compression error accross payback_delay frames */
1226 if (state
->bitrate_payback_method
== VBR_PAYBACK_BIAS
) {
1227 bytes2
= (int)(state
->curve_comp_error
/ state
->bitrate_payback_delay
);
1230 bytes2
= (int)(state
->curve_comp_error
* dbytes
/
1231 state
->average_frame
/ state
->bitrate_payback_delay
);
1233 if (labs(bytes2
) > fabs(state
->curve_comp_error
))
1234 bytes2
= (int)state
->curve_comp_error
;
1237 state
->curve_comp_error
-= bytes2
;
1239 if (state
->use_alt_curve
) {
1241 if (!state
->pass1_intra
) {
1243 if (dbytes
> state
->average_frame
) {
1244 if (dbytes
>= state
->alt_curve_high
)
1245 curve_temp
= dbytes
* (state
->alt_curve_mid_qual
- state
->alt_curve_qual_dev
);
1247 switch(state
->alt_curve_type
) {
1248 case VBR_ALT_CURVE_AGGRESIVE
:
1249 curve_temp
= dbytes
* (state
->alt_curve_mid_qual
- state
->alt_curve_qual_dev
*
1250 sin(DEG2RAD
* ((dbytes
- state
->average_frame
) * 90.0 / state
->alt_curve_high_diff
)));
1253 case VBR_ALT_CURVE_LINEAR
:
1254 curve_temp
= dbytes
* (state
->alt_curve_mid_qual
- state
->alt_curve_qual_dev
*
1255 (dbytes
- state
->average_frame
) / state
->alt_curve_high_diff
);
1257 case VBR_ALT_CURVE_SOFT
:
1258 curve_temp
= dbytes
* (state
->alt_curve_mid_qual
- state
->alt_curve_qual_dev
*
1259 (1.0 - cos(DEG2RAD
* ((dbytes
- state
->average_frame
) * 90.0 / state
->alt_curve_high_diff
))));
1264 if (dbytes
<= state
->alt_curve_low
)
1265 curve_temp
= dbytes
;
1267 switch(state
->alt_curve_type
) {
1268 case VBR_ALT_CURVE_AGGRESIVE
:
1269 curve_temp
= dbytes
* (state
->alt_curve_mid_qual
- state
->alt_curve_qual_dev
*
1270 sin(DEG2RAD
* ((dbytes
- state
->average_frame
) * 90.0 / state
->alt_curve_low_diff
)));
1273 case VBR_ALT_CURVE_LINEAR
:
1274 curve_temp
= dbytes
* (state
->alt_curve_mid_qual
- state
->alt_curve_qual_dev
*
1275 (dbytes
- state
->average_frame
) / state
->alt_curve_low_diff
);
1277 case VBR_ALT_CURVE_SOFT
:
1278 curve_temp
= dbytes
* (state
->alt_curve_mid_qual
+ state
->alt_curve_qual_dev
*
1279 (1.0 - cos(DEG2RAD
* ((dbytes
- state
->average_frame
) * 90.0 / state
->alt_curve_low_diff
))));
1284 curve_temp
= curve_temp
* state
->curve_comp_scale
+ state
->curve_bias_bonus
;
1286 bytes2
+= ((int)curve_temp
);
1287 state
->curve_comp_error
+= curve_temp
- ((int)curve_temp
);
1291 state
->curve_comp_error
+= dbytes
- ((int)dbytes
);
1292 bytes2
+= ((int)dbytes
);
1295 else if ((state
->curve_compression_high
+ state
->curve_compression_low
) &&
1296 !state
->pass1_intra
) {
1298 if (dbytes
> state
->average_frame
) {
1299 curve_temp
= state
->curve_comp_scale
*
1300 ((double)dbytes
+ (state
->average_frame
- dbytes
) *
1301 state
->curve_compression_high
/ 100.0);
1304 curve_temp
= state
->curve_comp_scale
*
1305 ((double)dbytes
+ (state
->average_frame
- dbytes
) *
1306 state
->curve_compression_low
/ 100.0);
1309 bytes2
+= ((int)curve_temp
);
1310 state
->curve_comp_error
+= curve_temp
- ((int)curve_temp
);
1313 state
->curve_comp_error
+= dbytes
- ((int)dbytes
);
1314 bytes2
+= ((int)dbytes
);
1317 /* cap bytes2 to first pass size, lowers number of quant=1 frames */
1318 if (bytes2
> bytes1
) {
1319 state
->curve_comp_error
+= bytes2
- bytes1
;
1322 else if (bytes2
< 1) {
1323 state
->curve_comp_error
+= --bytes2
;
1328 state
->desired_bytes2
= bytes2
;
1330 /* Ugly dependance between getquant and getintra */
1331 intra
= state
->getintra(state
);
1335 KFdistance
= state
->keyframe_locations
[state
->KF_idx
] -
1336 state
->keyframe_locations
[state
->KF_idx
- 1];
1338 if (KFdistance
< state
->kftreshold
) {
1339 KFdistance
= KFdistance
- state
->min_key_interval
;
1341 if (KFdistance
>= 0) {
1343 KF_min_size
= bytes2
* (100 - state
->kfreduction
) / 100;
1344 if (KF_min_size
< 1)
1347 bytes2
= KF_min_size
+ (bytes2
- KF_min_size
) * KFdistance
/
1348 (state
->kftreshold
- state
->min_key_interval
);
1357 * Foxer: scale overflow in relation to average size, so smaller frames don't get
1358 * too much/little bitrate
1360 overflow
= (int)((double)overflow
* bytes2
/ state
->average_frame
);
1362 /* Foxer: reign in overflow with huge frames */
1363 if (labs(overflow
) > labs(state
->overflow
)) {
1364 overflow
= state
->overflow
;
1367 /* Foxer: make sure overflow doesn't run away */
1368 if(overflow
> bytes2
* state
->twopass_max_overflow_improvement
/ 100) {
1369 bytes2
+= (overflow
<= bytes2
) ? bytes2
* state
->twopass_max_overflow_improvement
/ 100 :
1370 overflow
* state
->twopass_max_overflow_improvement
/ 100;
1372 else if(overflow
< bytes2
* state
->twopass_max_overflow_degradation
/ -100) {
1373 bytes2
+= bytes2
* state
->twopass_max_overflow_degradation
/ -100;
1379 if(bytes2
> state
->max_framesize
) {
1380 capped_to_max_framesize
= 1;
1381 bytes2
= state
->max_framesize
;
1388 state
->bytes1
= bytes1
;
1389 state
->bytes2
= bytes2
;
1391 /* very 'simple' quant<->filesize relationship */
1392 quant
= state
->pass1_quant
* bytes1
/ bytes2
;
1398 else if(!state
->pass1_intra
) {
1400 /* Foxer: aid desired quantizer precision by accumulating decision error */
1401 state
->quant_error
[quant
] += ((double)(state
->pass1_quant
* bytes1
) / bytes2
) - quant
;
1403 if (state
->quant_error
[quant
] >= 1.0) {
1404 state
->quant_error
[quant
] -= 1.0;
1409 /* we're done with credits */
1410 if(util_frametype(state
) != FRAME_TYPE_NORMAL_MOVIE
) {
1416 if (quant
< state
->min_iquant
)
1417 quant
= state
->min_iquant
;
1418 if (quant
> state
->max_iquant
)
1419 quant
= state
->max_iquant
;
1423 if(quant
> state
->max_pquant
)
1424 quant
= state
->max_pquant
;
1425 if(quant
< state
->min_pquant
)
1426 quant
= state
->min_pquant
;
1428 /* subsequent frame quants can only be +- 2 */
1429 if(state
->last_quant
&& capped_to_max_framesize
== 0) {
1430 if (quant
> state
->last_quant
+ 2)
1431 quant
= state
->last_quant
+ 2;
1432 if (quant
< state
->last_quant
- 2)
1433 quant
= state
->last_quant
- 2;
1441 static int vbr_getintra_2pass2(void *sstate
)
1445 vbr_control_t
*state
= sstate
;
1448 /* Get next intra state (fetched by update) */
1449 intra
= state
->pass1_intra
;
1451 /* During credits, XviD will decide itself */
1452 if(util_frametype(state
) != FRAME_TYPE_NORMAL_MOVIE
) {
1455 switch(state
->credits_mode
) {
1457 case VBR_CREDITS_MODE_RATE
:
1458 case VBR_CREDITS_MODE_SIZE
:
1461 case VBR_CREDITS_MODE_QUANT
:
1462 /* Except in this case */
1463 if (state
->credits_quant_i
== state
->credits_quant_p
)
1470 /* Force I Frame when max_key_interval is reached */
1471 if((state
->cur_frame
- state
->last_keyframe
) > state
->max_key_interval
)
1475 * Force P or B Frames for frames whose distance is less than the
1478 if((state
->cur_frame
- state
->last_keyframe
) < state
->min_key_interval
)
1482 /* Return the given intra mode except for first frame */
1483 return((state
->cur_frame
==0)?1:intra
);
1487 static int vbr_update_2pass2(void *sstate
,
1500 int next_hbytes
, next_kblocks
, next_mblocks
, next_ublocks
;
1503 vbr_control_t
*state
= sstate
;
1506 * We do not depend on getintra/quant because we have the real results
1507 * from the xvid core
1510 if (util_frametype(state
) == FRAME_TYPE_NORMAL_MOVIE
) {
1512 state
->quant_count
[quant
]++;
1514 if (state
->pass1_intra
) {
1516 state
->overflow
+= state
->KFoverflow
;
1517 state
->KFoverflow
= state
->desired_bytes2
- total_bytes
;
1519 tempdiv
= (state
->keyframe_locations
[state
->KF_idx
] -
1520 state
->keyframe_locations
[state
->KF_idx
- 1]);
1522 /* redistribute correctly (by koepi) */
1524 /* non-consecutive keyframes */
1525 state
->KFoverflow_partial
= state
->KFoverflow
/
1529 state
->overflow
+= state
->KFoverflow
;
1530 state
->KFoverflow
= 0;
1531 state
->KFoverflow_partial
= 0;
1537 state
->overflow
+= state
->desired_bytes2
- total_bytes
+
1538 state
->KFoverflow_partial
;
1539 state
->KFoverflow
-= state
->KFoverflow_partial
;
1544 state
->overflow
+= state
->desired_bytes2
- total_bytes
;
1545 state
->overflow
+= state
->KFoverflow
;
1546 state
->KFoverflow
= 0;
1547 state
->KFoverflow_partial
= 0;
1550 /* Save old quant */
1551 state
->last_quant
= quant
;
1553 /* Update next frame data */
1554 fscanf(state
->pass1_file
, "%d %d %d %d %d %d %d\n",
1555 &state
->pass1_quant
, &state
->pass1_intra
, &next_hbytes
,
1556 &state
->pass1_bytes
, &next_kblocks
, &next_mblocks
,
1559 /* Save the last Keyframe pos */
1561 state
->last_keyframe
= state
->cur_frame
;
1570 static int vbr_finish_2pass2(void *sstate
)
1573 vbr_control_t
*state
= sstate
;
1575 if(state
->pass1_file
== NULL
)
1578 /* Close the file */
1579 if(fclose(state
->pass1_file
) != 0)
1582 /* Free the memory */
1583 if(state
->keyframe_locations
)
1584 free(state
->keyframe_locations
);
1591 /******************************************************************************
1592 * Fixed quant mode - Most of the functions will be dummy functions
1593 *****************************************************************************/
1595 static int vbr_init_fixedquant(void *sstate
)
1598 vbr_control_t
*state
= sstate
;
1600 if(state
->fixed_quant
< 1)
1601 state
->fixed_quant
= 1;
1603 if(state
->fixed_quant
> 31)
1604 state
->fixed_quant
= 31;
1606 state
->cur_frame
= 0;
1612 static int vbr_getquant_fixedquant(void *sstate
)
1615 vbr_control_t
*state
= sstate
;
1617 /* Credits' frame ? */
1618 if(util_frametype(state
) != FRAME_TYPE_NORMAL_MOVIE
) {
1622 switch(state
->credits_mode
) {
1623 case VBR_CREDITS_MODE_RATE
:
1624 quant
= state
->fixed_quant
* state
->credits_quant_ratio
;
1626 case VBR_CREDITS_MODE_QUANT
:
1627 quant
= state
->credits_fixed_quant
;
1630 quant
= state
->fixed_quant
;
1638 /* No credit frame - return fixed quant */
1639 return(state
->fixed_quant
);
1643 static int vbr_getintra_fixedquant(void *state
)