comparison libaf/af.c @ 7589:443b440798a5

Redesign of buffer length calculation
author anders
date Thu, 03 Oct 2002 12:02:46 +0000
parents 31d0ea35c66b
children 0cba73469341
comparison
equal deleted inserted replaced
7588:8ee95f554262 7589:443b440798a5
364 }while(af); 364 }while(af);
365 return data; 365 return data;
366 } 366 }
367 367
368 /* Helper function used to calculate the exact buffer length needed 368 /* Helper function used to calculate the exact buffer length needed
369 when buffers are resized */ 369 when buffers are resized. The returned length is >= than what is
370 inline int af_lencalc(frac_t mul, int len){ 370 needed */
371 register int q = len*mul.n; 371 inline int af_lencalc(frac_t mul, af_data_t* d){
372 return q/mul.d + q%mul.d; 372 register int t = d->bps*d->nch;
373 return t*(((d->len/t)*mul.n + 1)/mul.d);
373 } 374 }
374 375
375 /* Calculate how long the output from the filters will be given the 376 /* Calculate how long the output from the filters will be given the
376 input length "len" */ 377 input length "len". The calculated length is >= the actual
378 length. */
377 int af_outputlen(af_stream_t* s, int len) 379 int af_outputlen(af_stream_t* s, int len)
378 { 380 {
381 int t = s->input.bps*s->input.nch;
379 af_instance_t* af=s->first; 382 af_instance_t* af=s->first;
380 frac_t mul = {1,1}; 383 register frac_t mul = {1,1};
381 // Iterate through all filters 384 // Iterate through all filters
382 do{ 385 do{
383 mul.n *= af->mul.n; 386 mul.n *= af->mul.n;
384 mul.d *= af->mul.d; 387 mul.d *= af->mul.d;
385 af=af->next; 388 af=af->next;
386 }while(af); 389 }while(af);
387 return af_lencalc(mul,len); 390 return t * (((len/t)*mul.n + 1)/mul.d);
388 } 391 }
389 392
390 /* Calculate how long the input to the filters should be to produce a 393 /* Calculate how long the input to the filters should be to produce a
391 certain output length, i.e. the return value of this function is 394 certain output length, i.e. the return value of this function is
392 the input length required to produce the output length "len". */ 395 the input length required to produce the output length "len". The
396 calculated length is <= the actual length */
393 int af_inputlen(af_stream_t* s, int len) 397 int af_inputlen(af_stream_t* s, int len)
394 { 398 {
399 int t = s->input.bps*s->input.nch;
395 af_instance_t* af=s->first; 400 af_instance_t* af=s->first;
396 frac_t mul = {1,1}; 401 register frac_t mul = {1,1};
397 // Iterate through all filters 402 // Iterate through all filters
398 do{ 403 do{
399 mul.d *= af->mul.n; 404 mul.n *= af->mul.n;
400 mul.n *= af->mul.d; 405 mul.d *= af->mul.d;
401 af=af->next; 406 af=af->next;
402 }while(af); 407 }while(af);
403 return af_lencalc(mul,len); 408 return t * (((len/t) * mul.d - 1)/mul.n);
404 } 409 }
405 410
406 /* Helper function called by the macro with the same name this 411 /* Helper function called by the macro with the same name this
407 function should not be called directly */ 412 function should not be called directly */
408 inline int af_resize_local_buffer(af_instance_t* af, af_data_t* data) 413 inline int af_resize_local_buffer(af_instance_t* af, af_data_t* data)
409 { 414 {
410 // Calculate new length 415 // Calculate new length
411 register int len = af_lencalc(af->mul,data->len/(data->nch*data->bps)) * 416 register int len = af_lencalc(af->mul,data);
412 data->nch * data->bps;
413 mp_msg(MSGT_AFILTER,MSGL_V,"Reallocating memory in module %s, old len = %i, new len = %i\n",af->info->name,af->data->len,len); 417 mp_msg(MSGT_AFILTER,MSGL_V,"Reallocating memory in module %s, old len = %i, new len = %i\n",af->info->name,af->data->len,len);
414 // If there is a buffer free it 418 // If there is a buffer free it
415 if(af->data->audio) 419 if(af->data->audio)
416 free(af->data->audio); 420 free(af->data->audio);
417 // Create new buffer and check that it is OK 421 // Create new buffer and check that it is OK