예제 #1
0
 /* Init audio filters */
int init_audio_filters(sh_audio_t *sh_audio, 
	int in_samplerate, int in_channels, int in_format,
	int *out_samplerate, int *out_channels, int *out_format,
	int out_minsize, int out_maxsize){
  af_stream_t* afs=sh_audio->afilter;
  if(!afs){
    afs = malloc(sizeof(af_stream_t));
    memset(afs,0,sizeof(af_stream_t));
  }

  // input format: same as codec's output format:
  afs->input.rate   = in_samplerate;
  afs->input.nch    = in_channels;
  afs->input.format = in_format;
  af_fix_parameters(&(afs->input));

  // output format: same as ao driver's input format (if missing, fallback to input)
  afs->output.rate   = *out_samplerate;
  afs->output.nch    = *out_channels;
  afs->output.format = *out_format;
  af_fix_parameters(&(afs->output));

  // filter config:  
  memcpy(&afs->cfg,&af_cfg,sizeof(af_cfg_t));
  
  mp_msg(MSGT_DECAUDIO, MSGL_V, MSGTR_BuildingAudioFilterChain,
      afs->input.rate,afs->input.nch,af_fmt2str_short(afs->input.format),
      afs->output.rate,afs->output.nch,af_fmt2str_short(afs->output.format));
  
  // let's autoprobe it!
  if(0 != af_init(afs)){
    sh_audio->afilter=NULL;
    free(afs);
    return 0; // failed :(
  }

  *out_samplerate=afs->output.rate;
  *out_channels=afs->output.nch;
  *out_format=afs->output.format;
  
  if (out_maxsize || out_minsize) {
  // allocate the a_out_* buffers:
  if(out_maxsize<out_minsize) out_maxsize=out_minsize;
  if(out_maxsize<8192) out_maxsize=MAX_OUTBURST; // not sure this is ok

  sh_audio->a_out_buffer_size=out_maxsize;
  if (sh_audio->a_out_buffer != sh_audio->a_buffer)
      free(sh_audio->a_out_buffer);
  sh_audio->a_out_buffer=memalign(16,sh_audio->a_out_buffer_size);
  memset(sh_audio->a_out_buffer,0,sh_audio->a_out_buffer_size);
  sh_audio->a_out_buffer_len=0;
  }
  
  // ok!
  sh_audio->afilter=(void*)afs;
  return 1;
}
예제 #2
0
int init_audio_filters(sh_audio_t *sh_audio, int in_samplerate,
		       int *out_samplerate, int *out_channels, int *out_format)
{
    af_stream_t *afs = sh_audio->afilter;
    if (!afs) {
	afs = malloc(sizeof(af_stream_t));
	memset(afs, 0, sizeof(af_stream_t));
    }
    // input format: same as codec's output format:
    afs->input.rate   = in_samplerate;
    afs->input.nch    = sh_audio->channels;
    afs->input.format = sh_audio->sample_format;
    af_fix_parameters(&(afs->input));

    // output format: same as ao driver's input format (if missing, fallback to input)
    afs->output.rate   = *out_samplerate;
    afs->output.nch    = *out_channels;
    afs->output.format = *out_format;
    af_fix_parameters(&(afs->output));

    // filter config:
    memcpy(&afs->cfg, &af_cfg, sizeof(af_cfg_t));

    mp_msg(MSGT_DECAUDIO, MSGL_V,
           "Building audio filter chain for %dHz/%dch/%s -> %dHz/%dch/%s...\n",
	   afs->input.rate, afs->input.nch,
	   af_fmt2str_short(afs->input.format), afs->output.rate,
	   afs->output.nch, af_fmt2str_short(afs->output.format));

    // let's autoprobe it!
    if (0 != af_init(afs)) {
	sh_audio->afilter = NULL;
	free(afs);
	return 0;   // failed :(
    }

    *out_samplerate = afs->output.rate;
    *out_channels = afs->output.nch;
    *out_format = afs->output.format;

    // Do not reset a_out_buffer_len. This may cause some
    // glitches/slow adaption of changes but it is better than
    // losing audio even for minor adjustments and avoids sync issues.

    // ok!
    sh_audio->afilter = (void *) afs;
    return 1;
}
예제 #3
0
int init_audio_filters(sh_audio_t *sh_audio, int in_samplerate,
		       int *out_samplerate, int *out_channels, int *out_format)
{
    af_stream_t *afs = sh_audio->afilter;
    if (!afs) {
	afs = malloc(sizeof(af_stream_t));
	memset(afs, 0, sizeof(af_stream_t));
    }
    // input format: same as codec's output format:
    afs->input.rate   = in_samplerate;
    afs->input.nch    = sh_audio->channels;
    afs->input.format = sh_audio->sample_format;
    af_fix_parameters(&(afs->input));

    // output format: same as ao driver's input format (if missing, fallback to input)
    afs->output.rate   = *out_samplerate;
    afs->output.nch    = *out_channels;
    afs->output.format = *out_format;
    af_fix_parameters(&(afs->output));

    // filter config:
    memcpy(&afs->cfg, &af_cfg, sizeof(af_cfg_t));

    mp_msg(MSGT_DECAUDIO, MSGL_V, MSGTR_BuildingAudioFilterChain,
	   afs->input.rate, afs->input.nch,
	   af_fmt2str_short(afs->input.format), afs->output.rate,
	   afs->output.nch, af_fmt2str_short(afs->output.format));

    // let's autoprobe it!
    if (0 != af_init(afs)) {
	sh_audio->afilter = NULL;
	free(afs);
	return 0;   // failed :(
    }

    *out_samplerate = afs->output.rate;
    *out_channels = afs->output.nch;
    *out_format = afs->output.format;

    sh_audio->a_out_buffer_len = 0;

    // ok!
    sh_audio->afilter = (void *) afs;
    return 1;
}
예제 #4
0
파일: dec_audio.c 프로젝트: HermiG/mplayer2
int init_audio_filters(sh_audio_t *sh_audio, int in_samplerate,
		       int *out_samplerate, int *out_channels, int *out_format)
{
    af_stream_t *afs = sh_audio->afilter;
    if (!afs) {
	afs = calloc(1, sizeof(struct af_stream));
	afs->opts = sh_audio->opts;
    }
    // input format: same as codec's output format:
    afs->input.rate   = in_samplerate;
    afs->input.nch    = sh_audio->channels;
    afs->input.format = sh_audio->sample_format;
    af_fix_parameters(&(afs->input));

    // output format: same as ao driver's input format (if missing, fallback to input)
    afs->output.rate   = *out_samplerate;
    afs->output.nch    = *out_channels;
    afs->output.format = *out_format;
    af_fix_parameters(&(afs->output));

    // filter config:
    memcpy(&afs->cfg, &af_cfg, sizeof(af_cfg_t));

    mp_tmsg(MSGT_DECAUDIO, MSGL_V, "Building audio filter chain for %dHz/%dch/%s -> %dHz/%dch/%s...\n",
	   afs->input.rate, afs->input.nch,
	   af_fmt2str_short(afs->input.format), afs->output.rate,
	   afs->output.nch, af_fmt2str_short(afs->output.format));

    // let's autoprobe it!
    if (0 != af_init(afs)) {
	sh_audio->afilter = NULL;
	free(afs);
	return 0;   // failed :(
    }

    *out_samplerate = afs->output.rate;
    *out_channels = afs->output.nch;
    *out_format = afs->output.format;

    // ok!
    sh_audio->afilter = (void *) afs;
    return 1;
}
예제 #5
0
파일: dec_audio.c 프로젝트: HermiG/mplayer2
static int filter_n_bytes(sh_audio_t *sh, struct bstr *outbuf, int len)
{
    assert(len-1 + sh->audio_out_minsize <= sh->a_buffer_size);

    int error = 0;

    // Decode more bytes if needed
    int old_samplerate = sh->samplerate;
    int old_channels = sh->channels;
    int old_sample_format = sh->sample_format;
    while (sh->a_buffer_len < len) {
	unsigned char *buf = sh->a_buffer + sh->a_buffer_len;
	int minlen = len - sh->a_buffer_len;
	int maxlen = sh->a_buffer_size - sh->a_buffer_len;
	int ret = sh->ad_driver->decode_audio(sh, buf, minlen, maxlen);
	int format_change = sh->samplerate != old_samplerate
                            || sh->channels != old_channels
                            || sh->sample_format != old_sample_format;
	if (ret <= 0 || format_change) {
	    error = format_change ? -2 : -1;
            // samples from format-changing call get discarded too
	    len = sh->a_buffer_len;
	    break;
	}
	sh->a_buffer_len += ret;
    }

    // Filter
    af_data_t filter_input = {
	.audio = sh->a_buffer,
	.len = len,
	.rate = sh->samplerate,
	.nch = sh->channels,
	.format = sh->sample_format
    };
    af_fix_parameters(&filter_input);
    af_data_t *filter_output = af_play(sh->afilter, &filter_input);
    if (!filter_output)
	return -1;
    set_min_out_buffer_size(outbuf, outbuf->len + filter_output->len);
    memcpy(outbuf->start + outbuf->len, filter_output->audio,
           filter_output->len);
    outbuf->len += filter_output->len;

    // remove processed data from decoder buffer:
    sh->a_buffer_len -= len;
    memmove(sh->a_buffer, sh->a_buffer + len, sh->a_buffer_len);

    return error;
}

/* Try to get at least minlen decoded+filtered bytes in outbuf
 * (total length including possible existing data).
 * Return 0 on success, -1 on error/EOF (not distinguished).
 * In the former case outbuf->len is always >= minlen on return.
 * In case of EOF/error it might or might not be.
 * Outbuf.start must be talloc-allocated, and will be reallocated
 * if needed to fit all filter output. */
int decode_audio(sh_audio_t *sh_audio, struct bstr *outbuf, int minlen)
{
    // Indicates that a filter seems to be buffering large amounts of data
    int huge_filter_buffer = 0;
    // Decoded audio must be cut at boundaries of this many bytes
    int unitsize = sh_audio->channels * sh_audio->samplesize * 16;

    /* Filter output size will be about filter_multiplier times input size.
     * If some filter buffers audio in big blocks this might only hold
     * as average over time. */
    double filter_multiplier = af_calc_filter_multiplier(sh_audio->afilter);

    /* If the decoder set audio_out_minsize then it can do the equivalent of
     * "while (output_len < target_len) output_len += audio_out_minsize;",
     * so we must guarantee there is at least audio_out_minsize-1 bytes
     * more space in the output buffer than the minimum length we try to
     * decode. */
    int max_decode_len = sh_audio->a_buffer_size - sh_audio->audio_out_minsize;
    max_decode_len -= max_decode_len % unitsize;

    while (outbuf->len < minlen) {
	int declen = (minlen - outbuf->len) / filter_multiplier
	    + (unitsize << 5); // some extra for possible filter buffering
	if (huge_filter_buffer)
	/* Some filter must be doing significant buffering if the estimated
	 * input length didn't produce enough output from filters.
	 * Feed the filters 2k bytes at a time until we have enough output.
	 * Very small amounts could make filtering inefficient while large
	 * amounts can make MPlayer demux the file unnecessarily far ahead
	 * to get audio data and buffer video frames in memory while doing
	 * so. However the performance impact of either is probably not too
	 * significant as long as the value is not completely insane. */
	    declen = 2000;
	declen -= declen % unitsize;
	if (declen > max_decode_len)
	    declen = max_decode_len;
	else
	    /* if this iteration does not fill buffer, we must have lots
	     * of buffering in filters */
	    huge_filter_buffer = 1;
	int res = filter_n_bytes(sh_audio, outbuf, declen);
	if (res < 0)
	    return res;
    }
    return 0;
}
예제 #6
0
int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
{
  int declen;
  af_data_t  afd;  // filter input
  af_data_t* pafd; // filter output
  ad_functions_t* mpadec = sh_audio->ad_driver;

  if(!sh_audio->inited) return -1; // no codec
  if(!sh_audio->afilter){
      // no filter, just decode:
      // FIXME: don't drop initial decoded data in a_buffer!
      return mpadec->decode_audio(sh_audio,buf,minlen,maxlen);
  }
  
//  declen=af_inputlen(sh_audio->afilter,minlen);
  declen=af_calc_insize_constrained(sh_audio->afilter,minlen,maxlen,
      sh_audio->a_buffer_size-sh_audio->audio_out_minsize);

  mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"\ndecaudio: minlen=%d maxlen=%d declen=%d (max=%d)\n",
      minlen, maxlen, declen, sh_audio->a_buffer_size);

  if(declen<=0) return -1; // error!

  // limit declen to buffer size: - DONE by af_calc_insize_constrained
//  if(declen>sh_audio->a_buffer_size) declen=sh_audio->a_buffer_size;

  // decode if needed:
  while(declen>sh_audio->a_buffer_len){
      int len=declen-sh_audio->a_buffer_len;
      int maxlen=sh_audio->a_buffer_size-sh_audio->a_buffer_len;

      mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"decaudio: decoding %d bytes, max: %d (%d)\n",
        len, maxlen, sh_audio->audio_out_minsize);

      // When a decoder sets audio_out_minsize that should guarantee it can
      // write up to audio_out_minsize bytes at a time until total >= minlen
      // without checking maxlen. Thus maxlen must be at least minlen +
      // audio_out_minsize. Check that to guard against buffer overflows.
      if (maxlen < len + sh_audio->audio_out_minsize)
	  break;
      // not enough decoded data waiting, decode 'len' bytes more:
      len=mpadec->decode_audio(sh_audio,
          sh_audio->a_buffer+sh_audio->a_buffer_len, len, maxlen);
      if(len<=0) break; // EOF?
      sh_audio->a_buffer_len+=len;
  }
  if(declen>sh_audio->a_buffer_len)
    declen=sh_audio->a_buffer_len; // still no enough data (EOF) :(

  // round to whole samples:
//  declen/=sh_audio->samplesize*sh_audio->channels;
//  declen*=sh_audio->samplesize*sh_audio->channels;

  // run the filters:
  afd.audio=sh_audio->a_buffer;
  afd.len=declen;
  afd.rate=sh_audio->samplerate;
  afd.nch=sh_audio->channels;
  afd.format=sh_audio->sample_format;
  af_fix_parameters(&afd);
  //pafd=&afd;
//  printf("\nAF: %d --> ",declen);
  pafd=af_play(sh_audio->afilter,&afd);
//  printf("%d  \n",pafd->len);

  if(!pafd) return -1; // error

  mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"decaudio: declen=%d out=%d (max %d)\n",
      declen, pafd->len, maxlen);
  
  // copy filter==>out:
  if(maxlen < pafd->len) {
    af_stream_t *afs = sh_audio->afilter;
    maxlen -= maxlen % (afs->output.nch * afs->output.bps);
    mp_msg(MSGT_DECAUDIO,MSGL_WARN,"%i bytes of audio data lost due to buffer overflow, len = %i\n", pafd->len - maxlen,pafd->len);
  }
  else
    maxlen=pafd->len;
  memmove(buf, pafd->audio, maxlen);
  
  // remove processed data from decoder buffer:
  sh_audio->a_buffer_len-=declen;
  if(sh_audio->a_buffer_len>0)
    memmove(sh_audio->a_buffer, sh_audio->a_buffer+declen, sh_audio->a_buffer_len);
  
  return maxlen;
}