+ if( job->pass == 2 )
+ {
+ for(;;)
+ {
+ int bytes, size, ret;
+ /*Ask the encoder how many bytes it would like.*/
+ bytes = th_encode_ctl( pv->ctx, TH_ENCCTL_2PASS_IN, NULL, 0 );
+ if( bytes < 0 )
+ {
+ hb_error("Error requesting stats size in second pass.");
+ *job->die = 1;
+ return HB_WORK_DONE;
+ }
+
+ /*If it's got enough, stop.*/
+ if( bytes == 0 ) break;
+
+ /*Read in some more bytes, if necessary.*/
+ if( bytes > pv->stat_fill - pv->stat_read )
+ size = bytes - (pv->stat_fill - pv->stat_read);
+ else
+ size = 0;
+ if( size > 80 - pv->stat_fill )
+ size = 80 - pv->stat_fill;
+ if( size > 0 &&
+ fread( pv->stat_buf+pv->stat_fill, 1, size, pv->file ) < size )
+ {
+ hb_error("Could not read frame data from two-pass data file!");
+ *job->die = 1;
+ return HB_WORK_DONE;
+ }
+ pv->stat_fill += size;
+
+ /*And pass them off.*/
+ if( bytes > pv->stat_fill - pv->stat_read )
+ bytes = pv->stat_fill - pv->stat_read;
+ ret = th_encode_ctl( pv->ctx, TH_ENCCTL_2PASS_IN,
+ pv->stat_buf+pv->stat_read, bytes);
+ if( ret < 0 )
+ {
+ hb_error("Error submitting pass data in second pass.");
+ *job->die = 1;
+ return HB_WORK_DONE;
+ }
+ /*If the encoder consumed the whole buffer, reset it.*/
+ if( ret >= pv->stat_fill - pv->stat_read )
+ pv->stat_read = pv->stat_fill = 0;
+ /*Otherwise remember how much it used.*/
+ else
+ pv->stat_read += ret;
+ }
+ }