long audio_read_func (cdrom_drive *drive, float **buffer, int prev_sector, int sectors) { int curr_sector; curr_sector = cdda_read (drive, buffer, prev_sector, sectors); prev_sector += curr_sector; return curr_sector; }
static int encode_ogg (cdrom_drive *drive, rip_opts_s *rip_opts, text_tag_s *text_tag, int track, int tracktot, char *filename, char **filenames) { ogg_stream_state os; ogg_page og; ogg_packet op; vorbis_dsp_state vd; vorbis_block vb; vorbis_info vi; long samplesdone = 0; int sector = 0, last_sector = 0; long bytes_written = 0, packetsdone = 0; double time_elapsed = 0.0; int ret = 0; time_t *timer; double time; int serialno = rand (); vorbis_comment vc; long total_samples_per_channel = 0; int channels = 2; int eos = 0; long rate = 44100; FILE *out = fopen (filename, "w+"); timer = timer_start (); if (!rip_opts->managed && (rip_opts->min_bitrate > 0 || rip_opts->max_bitrate > 0)) { log_msg ("Min or max bitrate requires managed", FL, FN, LN); return -1; } if (rip_opts->bitrate < 0 && rip_opts->min_bitrate < 0 && rip_opts->max_bitrate < 0) { rip_opts->quality_set = 1; } start_func (filename, rip_opts->bitrate, rip_opts->quality, rip_opts->quality_set, rip_opts->managed, rip_opts->min_bitrate, rip_opts->max_bitrate); vorbis_info_init (&vi); if (rip_opts->quality_set > 0) { if (vorbis_encode_setup_vbr (&vi, channels, rate, rip_opts->quality)) { log_msg ("Couldn't initialize vorbis_info", FL, FN, LN); vorbis_info_clear (&vi); return -1; } /* two options here, max or min bitrate */ if (rip_opts->max_bitrate > 0 || rip_opts->min_bitrate > 0) { struct ovectl_ratemanage_arg ai; vorbis_encode_ctl (&vi, OV_ECTL_RATEMANAGE_GET, &ai); ai.bitrate_hard_min = rip_opts->min_bitrate; ai.bitrate_hard_max = rip_opts->max_bitrate; ai.management_active = 1; vorbis_encode_ctl (&vi, OV_ECTL_RATEMANAGE_SET, &ai); } } else { if (vorbis_encode_setup_managed (&vi, channels, rate, rip_opts->max_bitrate > 0 ? rip_opts->max_bitrate * 1000 : -1, rip_opts->bitrate * 1000, rip_opts->min_bitrate > 0 ? rip_opts->min_bitrate * 1000 : -1)) { log_msg ("Mode init failed, encode setup managed", FL, FN, LN); vorbis_info_clear (&vi); return -1; } } if (rip_opts->managed && rip_opts->bitrate < 0) { vorbis_encode_ctl (&vi, OV_ECTL_RATEMANAGE_AVG, NULL); } else if (!rip_opts->managed) { vorbis_encode_ctl (&vi, OV_ECTL_RATEMANAGE_SET, NULL); } /* set advanced encoder options */ vorbis_encode_setup_init (&vi); vorbis_analysis_init (&vd, &vi); vorbis_block_init (&vd, &vb); ogg_stream_init (&os, serialno); { ogg_packet header_main; ogg_packet header_comments; ogg_packet header_codebooks; int result; char buf[32]; vorbis_comment_init (&vc); vorbis_comment_add_tag (&vc, "title", text_tag->songname); vorbis_comment_add_tag (&vc, "artist", text_tag->artistname); vorbis_comment_add_tag (&vc, "album", text_tag->albumname); vorbis_comment_add_tag (&vc, "genre", text_tag->genre); snprintf (buf, 32, "%d", text_tag->year); vorbis_comment_add_tag (&vc, "date", buf); snprintf (buf, 32, "%02d", text_tag->track); vorbis_comment_add_tag (&vc, "tracknumber", buf); vorbis_analysis_headerout (&vd, &vc, &header_main, &header_comments, &header_codebooks); ogg_stream_packetin (&os, &header_main); ogg_stream_packetin (&os, &header_comments); ogg_stream_packetin (&os, &header_codebooks); while ((result = ogg_stream_flush (&os, &og))) { if (result == 0) break; ret = write_page (&og, out); if (ret != og.header_len + og.body_len) { log_msg ("Failed writing data to output stream", FL, FN, LN); ret = -1; } } sector = cdda_track_firstsector (drive, track); last_sector = cdda_track_lastsector (drive, track); total_samples_per_channel = (last_sector - sector) * (CD_FRAMESAMPLES / 2); int eos = 0; while (!eos) { signed char *buffer = (signed char *)malloc (CD_FRAMESIZE_RAW * READ_SECTORS); //use this variable as a s**t long sectors_read = last_sector - sector; if (sectors_read > READ_SECTORS) sectors_read = READ_SECTORS; sectors_read = cdda_read (drive, (signed char *)buffer, sector, sectors_read); int i; if (sectors_read == 0) { vorbis_analysis_wrote (&vd, 0); } else { float **vorbbuf = vorbis_analysis_buffer (&vd, CD_FRAMESIZE_RAW * sectors_read); for (i = 0; i < (CD_FRAMESIZE_RAW * sectors_read) / 4; i++) { vorbbuf[0][i] = ((buffer[i * 4 + 1] << 8) | (0x00ff&(int)buffer[i * 4])) / 32768.f; vorbbuf[1][i] = ((buffer[i * 4 + 3] << 8) | (0x00ff&(int)buffer[i * 4 + 2])) / 32768.f; } int samples_read = sectors_read * (CD_FRAMESAMPLES / 2); samplesdone += samples_read; // progress every 60 pages if (packetsdone >= 60) { packetsdone = 0; time = timer_time (timer); update_statistics (total_samples_per_channel, samplesdone, time, track, tracktot, 0, filenames); } vorbis_analysis_wrote (&vd, i); } free (buffer); sector += sectors_read; while (vorbis_analysis_blockout (&vd, &vb) == 1) { vorbis_analysis (&vb, &op); vorbis_bitrate_addblock (&vb); while (vorbis_bitrate_flushpacket (&vd, &op)) { ogg_stream_packetin (&os, &op); packetsdone++; while (!eos) { int result = ogg_stream_pageout (&os, &og); if (result == 0) { break; } ret = write_page (&og, out); if (ret != og.header_len + og.body_len) { log_msg ("Failed writing data to output stream", FL, FN, LN); ret = -1; } else bytes_written += ret; if (ogg_page_eos (&og)) { eos = 1; } } } } } } ret = 0; update_statistics (total_samples_per_channel, samplesdone, time, track, tracktot, 0, filenames); ogg_stream_clear (&os); vorbis_block_clear (&vb); vorbis_dsp_clear (&vd); vorbis_comment_clear (&vc); vorbis_info_clear (&vi); vorbis_comment_clear (&vc); time_elapsed = timer_time (timer); end_func (time_elapsed, rate, samplesdone, bytes_written); timer_clear (timer); fclose (out); return ret; }
int analyze_cache(cdrom_drive *d, FILE *progress, FILE *log, int speed){ /* Some assumptions about timing: We can't perform cache determination timing based on looking at average transfer times; on slow setups, the speed of a drive reading sectors via PIO will not be reliably distinguishable from the same drive returning data from the cache via pio. We need something even more noticable and reliable: the seek time. It is unlikely we'd ever see a seek latency of under ~10ms given the synchronization requirements of a CD and the maximum possible rotational velocity. A cache hit would always be faster, even with PIO. Further complicating things, we have to watch the data collection carefully as we're not always going to be on an unloaded system, and we even have to guard against other apps accessing the drive (something that should never happen on purpose, but could happen by accident). As we know in our testing when seeks should never occur, a sudden seek-sized latency popping up in the middle of a collection is an indication that collection is possibly invalid. A second cause of 'spurious latency' would be media damage; if we're consistently hitting latency on the same sector during initial collection, may need to move past it. */ int i,j,ret=0,x; int firstsector=-1; int lastsector=-1; int firsttest=-1; int lasttest=-1; int offset; int warn=0; int current=1000; int hi=15000; int cachesize=0; int readahead=0; int rollbehind=0; int cachegran=0; float mspersector=0; if(speed<=0)speed=-1; reportC("\n=================== Checking drive cache/timing behavior ===================\n"); d->error_retry=0; /* verify the lib and cache analysis match */ if(strcmp(VERSIONNUM,paranoia_version())){ reportC("\nWARNING: cdparanoia application (and thus the cache tests) does not match the" "\ninstalled (or in use) libcdda_paranoia.so library. The final verdict of this" "\ntesting may or may not be accurate for the actual version of the paranoia" "library. Continuing anyway...\n\n"); } /* find the longest stretch of available audio data */ for(i=0;i<d->tracks;i++){ if(cdda_track_audiop(d,i+1)==1){ if(firsttest == -1) firsttest=cdda_track_firstsector(d,i+1); lasttest=cdda_track_lastsector(d,i+1); if(lasttest-firsttest > lastsector-firstsector){ firstsector=firsttest; lastsector=lasttest; } }else{ firsttest=-1; lasttest=-1; } } if(firstsector==-1){ reportC("\n\tNo audio on disc; Cannot determine timing behavior..."); return -1; } /* Dump some initial timing data to give a little context for human eyes. Take readings ten minutes apart (45000 sectors) and at end of disk. */ { int best=0; int bestcount=0; int iterating=0; offset = lastsector-firstsector-current-1; reportC("\nSeek/read timing:\n"); while(offset>=firstsector){ int m = offset/4500; int s = (offset-m*4500)/75; int f = offset-m*4500-s*75; int sofar; if(iterating){ reportC("\n"); }else{ printC("\r"); logC("\n"); } reportC("\t[%02d:%02d.%02d]: ",m,s,f); /* initial seek to put at at a small offset past end of upcoming reads */ if((ret=cdda_read(d,NULL,offset+current+1,1))<0){ /* media error! grr! retry elsewhere */ if(ret==-404)return -1; reportC("\n\tWARNING: media error during read; continuing at next offset..."); offset = (offset-firstsector+44999)/45000*45000+firstsector; offset-=45000; continue; } sofar=time_drive(d,progress, log, offset, current, 1); if(offset==firstsector)mspersector = sofar/(float)current; if(sofar==-404) return -1; else if(sofar<0){ reportC("\n\tWARNING: media error during read; continuing at next offset..."); offset = (offset-firstsector+44999)/45000*45000+firstsector; offset-=45000; continue; }else{ if(!iterating){ if(best==0 || sofar*1.01<best){ best= sofar; bestcount=0; }else{ bestcount+=sofar; if(bestcount>sofar && bestcount>4000) iterating=1; } } } if(iterating){ offset = (offset-firstsector+44999)/45000*45000+firstsector; offset-=45000; printC(" "); }else{ offset--; printC(" spinning up... "); } } } reportC("\n\nAnalyzing cache behavior...\n"); /* search on cache size; cache hits are fast, seeks are not, so a linear search through cache hits up to a miss are faster than a bisection */ { int under=1; int onex=0; current=0; offset = firstsector+10; while(current <= hi && under){ int i,j; under=0; current++; if(onex){ if(speed==-1){ logC("\tAttempting to reset read speed to full... "); }else{ logC("\tAttempting to reset read speed to %dx... ",speed); } if(cdda_speed_set(d,speed)){ logC("failed.\n"); }else{ logC("drive said OK\n"); } onex=0; } printC("\r"); reportC("\tFast search for approximate cache size... %d sectors ",current-1); logC("\n"); for(i=0;i<25 && !under;i++){ for(j=0;;j++){ int ret1=0,ret2=0; if(i>=15){ int sofar=0; if(i==15){ printC("\r"); reportC("\tSlow verify for approximate cache size... %d sectors",current-1); logC("\n"); logC("\tAttempting to reduce read speed to 1x... "); if(cdda_speed_set(d,1)){ logC("failed.\n"); }else{ logC("drive said OK\n"); } onex=1; } printC("."); logC("\t\t>>> "); while(sofar<current){ ret1 = cdda_read_timed(d,NULL,offset+sofar,current-sofar,&x); logC("slow_read=%d:%d:%d ",offset+sofar,ret1,x); if(ret1<=0)break; sofar+=ret1; } }else{ ret1 = cdda_read_timed(d,NULL,offset+current-1,1,&x); logC("\t\t>>> fast_read=%d:%d:%d ",offset+current-1,ret1,x); /* Some drives are 'easily distracted' when readahead is running ahead of the read cursor, causing accesses of the earliest sectors in the cache to show bursty latency. If there's no seek here after a borderline long read of the earliest sector in the cache, then the cache must not have been dumped yet. */ if(ret==1 && i && x<MIN_SEEK_MS){ under=1; logC("\n"); break; } } ret2 = cdda_read_timed(d,NULL,offset,1,&x); logC("seek_read=%d:%d:%d\n",offset,ret2,x); if(ret1<=0 || ret2<=0){ offset+=current+100; if(j==10 || offset+current>lastsector){ reportC("\n\tToo many read errors while performing drive cache checks;" "\n\t aborting test.\n\n"); return(-1); } reportC("\n\tRead error while performing drive cache checks;" "\n\t choosing new offset and trying again.\n"); }else{ if(x==-1){ reportC("\n\tTiming error while performing drive cache checks; aborting test.\n"); return(-1); }else{ if(x<MIN_SEEK_MS){ under=1; } break; } } } } } } cachesize=current-1; printC("\r"); if(cachesize==hi){ reportC("\tWARNING: Cannot determine drive cache size or behavior! \n"); return 1; }else if(cachesize){ reportC("\tApproximate random access cache size: %d sector(s) \n",cachesize); }else{ reportC("\tDrive does not cache nonlinear access \n"); return 0; } /* does the readahead cache exceed the maximum Paranoia currently expects? */ { cdrom_paranoia *p=paranoia_init(d); if(cachesize > paranoia_cachemodel_size(p,-1)){ reportC("\nWARNING: This drive appears to be caching more sectors of\n" " readahead than Paranoia can currently handle!\n"); warn=1; } paranoia_free(p); } if(speed==-1){ logC("\tAttempting to reset read speed to full... "); }else{ logC("\tAttempting to reset read speed to %d... ",speed); } if(cdda_speed_set(d,speed)){ logC("failed.\n"); }else{ logC("drive said OK\n"); } /* This is similar to the Fast search above, but just in case the cache is being tracked as multiple areas that are treated differently if non-contiguous.... */ { int seekoff = cachesize*3; int under=0; reportC("\tVerifying that cache is contiguous..."); for(i=0;i<20 && !under;i++){ printC("."); for(j=0;;j++){ int ret1,ret2; if(offset+seekoff>lastsector){ reportC("\n\tOut of readable space on CDROM while performing drive checks;" "\n\t aborting test.\n\n"); return(-1); } ret1 = cdda_read_timed(d,NULL,offset+seekoff,1,&x); logC("\t\t>>> %d:%d:%d ",offset+seekoff,ret1,x); ret2 = cdda_read_timed(d,NULL,offset,1,&x); logC("seek_read:%d:%d:%d\n",offset,ret2,x); if(ret1<=0 || ret2<=0){ offset+=cachesize+100; if(j==10){ reportC("\n\tToo many read errors while performing drive cache checks;" "\n\t aborting test.\n\n"); return(-1); } reportC("\n\tRead error while performing drive cache checks;" "\n\t choosing new offset and trying again.\n"); }else{ if(x==-1){ reportC("\n\tTiming error while performing drive cache checks; aborting test.\n"); return(-1); }else{ if(x<MIN_SEEK_MS)under=1; break; } } } } printC("\r"); if(under){ reportC("\nWARNING: Drive cache does not appear to be contiguous!\n"); warn=1; }else{ reportC("\tDrive cache tests as contiguous \n"); } } /* The readahead cache size ascertained above is likely qualified by background 'rollahead'; that is, the drive's readahead process is often working ahead of our actual linear reads, and if reads stop or are interrupted, readahead continues and overflows the cache. It is also the case that the cache size we determined above is slightly too low because readahead is probably always working ahead of reads. Determine the rollahead size a few ways (which may disagree: 1) Read number of sectors equal to cache size; pause; read backward until seek 2) Read sectors equal to cache-rollahead; verify reading back to beginning does not seek 3) Read sectors equal to cache; pause; read ahead until seek delay */ { int lower=0; int gran=64; int it=3; int tests=0; int under=1; readahead=0; while(gran>1 || under){ tests++; if(tests>8 && gran<64){ gran<<=3; tests=0; it=3; } if(gran && !under){ gran>>=3; tests=0; if(gran==1)it=10; } under=0; readahead=lower+gran; printC("\r"); logC("\n"); reportC("\tTesting background readahead past read cursor... %d",readahead); printC(" \b\b\b\b\b\b\b\b\b\b\b"); for(i=0;i<it;i++){ int sofar=0,ret; logC("\n\t\t%d >>> ",i); while(sofar<cachesize){ ret = cdda_read_timed(d,NULL,offset+sofar,cachesize-sofar,&x); if(ret<=0)goto error; logC("%d:%d:%d ",offset+sofar,ret,x); /* some drives can lose sync and perform an internal resync, which can also cause readahead to restart. If we see seek-like delays during the initial cahe load, retry the preload */ sofar+=ret; } printC("."); /* what we'd predict is needed to let the readahead process work. */ { int usec=mspersector*(readahead)*(6+i)*200; int max= 13000*2*readahead; /* corresponds to .5x */ if(usec>max)usec=max; logC("sleep=%dus ",usec); usleep(usec); } /* seek to offset+cachesize+readahead */ ret = cdda_read_timed(d,NULL,offset+cachesize+readahead-1,1,&x); if(ret<=0)break; logC("seek=%d:%d:%d",offset+cachesize+readahead-1,ret,x); if(x<MIN_SEEK_MS){ under=1; break; }else if(i%3==1){ /* retime the drive just to be conservative */ mspersector=retime_drive(d, progress, log, offset, readahead, mspersector); } } if(under) lower=readahead; } readahead=lower; }