Ticket #64: audio_detect.diff
| File audio_detect.diff, 3.6 KB (added by , 22 years ago) |
|---|
-
libmpdemux/demux_audio.
old new 21 21 22 22 23 23 #define HDR_SIZE 4 24 #define MIN_VALID_HEADERS 6 24 25 25 26 typedef struct da_priv { 26 27 int frmt; … … 37 38 stream_t *s; 38 39 sh_audio_t* sh_audio; 39 40 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; 41 43 da_priv_t* priv; 42 44 #ifdef MP_DEBUG 43 45 assert(demuxer != NULL); … … 46 48 47 49 s = demuxer->stream; 48 50 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; 56 55 57 56 if( hdr[0] == 'R' && hdr[1] == 'I' && hdr[2] == 'F' && hdr[3] == 'F' ) { 58 57 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; 64 61 if(hdr[0] != 'W' || hdr[1] != 'A' || hdr[2] != 'V' || hdr[3] != 'E' ) 65 62 stream_skip(s,-8); 66 63 else 67 64 // We found wav header. Now we can have 'fmt ' or a mp3 header 68 65 // empty the buffer 69 step = 4;66 bytes_to_read = HDR_SIZE; 70 67 } else if( hdr[0] == 'I' && hdr[1] == 'D' && hdr[2] == '3' && (hdr[3] >= 2)) { 71 int len;72 68 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; 74 72 len = (hdr[0]<<21) | (hdr[1]<<14) | (hdr[2]<<7) | hdr[3]; 75 73 stream_skip(s,len); 76 step = 4;74 bytes_to_read = HDR_SIZE; 77 75 } else if( hdr[0] == 'f' && hdr[1] == 'm' && hdr[2] == 't' && hdr[3] == ' ' ) { 78 76 frmt = WAV; 79 77 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) { 81 89 frmt = MP3; 82 90 break; 91 } 92 if(s->eof || !s->end_pos) break; 93 stream_seek(s, pos+1); 94 bytes_to_read = HDR_SIZE; 83 95 } else if( hdr[0] == 'f' && hdr[1] == 'L' && hdr[2] == 'a' && hdr[3] == 'C' ) { 84 96 frmt = fLaC; 85 97 stream_skip(s,-4); 86 98 break; 87 99 } 88 100 // 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++; 92 104 } 93 105 94 106 if(!frmt) … … 99 111 switch(frmt) { 100 112 case MP3: 101 113 sh_audio->format = 0x55; 102 demuxer->movi_start = st_pos-HDR_SIZE+n;114 demuxer->movi_start = pos; 103 115 sh_audio->audio.dwSampleSize= 0; 104 116 sh_audio->audio.dwScale = 1152; 105 117 sh_audio->audio.dwRate = mp3_freq; … … 110 122 sh_audio->wf->nBlockAlign = 1152; 111 123 sh_audio->wf->wBitsPerSample = 16; 112 124 sh_audio->wf->cbSize = 0; 125 //following loop can be removed after testing new detection 113 126 for(n = 0; n < 5 ; n++) { 114 127 pos = mp_decode_mp3_header(hdr); 115 128 if(pos < 0)
