Ticket #64: audio_detect.diff

File audio_detect.diff, 3.6 KB (added by behrisch@…, 22 years ago)

Fixes the problem

  • libmpdemux/demux_audio.

    old new  
    2121
    2222
    2323#define HDR_SIZE 4
     24#define MIN_VALID_HEADERS 6
    2425
    2526typedef struct da_priv {
    2627  int frmt;
     
    3738  stream_t *s;
    3839  sh_audio_t* sh_audio;
    3940  uint8_t hdr[HDR_SIZE];
    40   int st_pos = 0,frmt = 0, n = 0, pos = 0, step, mp3_freq,mp3_chans;
     41  int frmt = 0, n = 0, pos = 0, bytes_to_read = HDR_SIZE;
     42  int len, mp3_freq, mp3_chans;
    4143  da_priv_t* priv;
    4244#ifdef MP_DEBUG
    4345  assert(demuxer != NULL);
     
    4648 
    4749  s = demuxer->stream;
    4850
    49   while(n < 5 && ! s->eof) {
    50     st_pos = stream_tell(s);
    51     step = 1;
    52     if(pos < HDR_SIZE) {
    53       stream_read(s,&hdr[pos],HDR_SIZE-pos);
    54       pos = HDR_SIZE;
    55     }
     51  while(n < 3000 && ! s->eof) {
     52    stream_read(s,&hdr[HDR_SIZE-bytes_to_read],bytes_to_read);
     53    pos = stream_tell(s) - HDR_SIZE;
     54    bytes_to_read = 1;
    5655
    5756    if( hdr[0] == 'R' && hdr[1] == 'I' && hdr[2] == 'F' && hdr[3] == 'F' ) {
    5857      stream_skip(s,4);
    59       if(s->eof)
    60         break;
    61       stream_read(s,hdr,4);
    62       if(s->eof)
    63         break;
     58      if(s->eof) break;
     59      stream_read(s,hdr,HDR_SIZE);
     60      if(s->eof) break;
    6461      if(hdr[0] != 'W' || hdr[1] != 'A' || hdr[2] != 'V'  || hdr[3] != 'E' )
    6562        stream_skip(s,-8);
    6663      else
    6764      // We found wav header. Now we can have 'fmt ' or a mp3 header
    6865      // empty the buffer
    69         step = 4;
     66        bytes_to_read = HDR_SIZE;
    7067    } else if( hdr[0] == 'I' && hdr[1] == 'D' && hdr[2] == '3' && (hdr[3] >= 2)) {
    71       int len;
    7268      stream_skip(s,2);
    73       stream_read(s,hdr,4);
     69      if(s->eof) break;
     70      stream_read(s,hdr,HDR_SIZE);
     71      if(s->eof) break;
    7472      len = (hdr[0]<<21) | (hdr[1]<<14) | (hdr[2]<<7) | hdr[3];
    7573      stream_skip(s,len);
    76       step = 4;
     74      bytes_to_read = HDR_SIZE;
    7775    } else if( hdr[0] == 'f' && hdr[1] == 'm' && hdr[2] == 't' && hdr[3] == ' ' ) {
    7876      frmt = WAV;
    7977      break;     
    80     } else if((n = mp_get_mp3_header(hdr,&mp3_chans,&mp3_freq)) > 0) {
     78    } else if((len = mp_get_mp3_header(hdr,&mp3_chans,&mp3_freq)) > 20) {
     79      int i, mp3_freq2, mp3_chans2;
     80      for(i = 1; i < MIN_VALID_HEADERS ; i++) {
     81        stream_skip(s,len-HDR_SIZE);
     82        if(s->eof) break;
     83        stream_read(s,hdr,HDR_SIZE);
     84        if(s->eof) break;
     85        len = mp_get_mp3_header(hdr,&mp3_chans2,&mp3_freq2);
     86        if(len < 21 || mp3_chans != mp3_chans2 || mp3_freq != mp3_freq2) break;
     87      }
     88      if(i == MIN_VALID_HEADERS) {
    8189      frmt = MP3;
    8290      break;
     91      }
     92      if(s->eof || !s->end_pos) break;
     93      stream_seek(s, pos+1);
     94      bytes_to_read = HDR_SIZE;
    8395    } else if( hdr[0] == 'f' && hdr[1] == 'L' && hdr[2] == 'a' && hdr[3] == 'C' ) {
    8496      frmt = fLaC;
    8597      stream_skip(s,-4);
    8698      break;
    8799    }
    88100    // Add here some other audio format detection
    89     if(step < HDR_SIZE)
    90       memmove(hdr,&hdr[step],HDR_SIZE-step);
    91     pos -= step;
     101    if(bytes_to_read < HDR_SIZE)
     102      memmove(hdr,&hdr[bytes_to_read],HDR_SIZE-bytes_to_read);
     103    n++;
    92104  }
    93105
    94106  if(!frmt)
     
    99111  switch(frmt) {
    100112  case MP3:
    101113    sh_audio->format = 0x55;
    102     demuxer->movi_start = st_pos-HDR_SIZE+n;
     114    demuxer->movi_start = pos;
    103115    sh_audio->audio.dwSampleSize= 0;
    104116    sh_audio->audio.dwScale = 1152;
    105117    sh_audio->audio.dwRate = mp3_freq;
     
    110122    sh_audio->wf->nBlockAlign = 1152;
    111123    sh_audio->wf->wBitsPerSample = 16;
    112124    sh_audio->wf->cbSize = 0;   
     125    //following loop can be removed after testing new detection
    113126    for(n = 0; n < 5 ; n++) {
    114127      pos = mp_decode_mp3_header(hdr);
    115128      if(pos < 0)