static void sfsamples_bang(t_sfsamples *x) { DEBUG(post("sfsamples_bang")); const gavl_audio_format_t* open_audio_format; gavl_time_t gavl_time; if(x->filename == &s_) { pd_error(x,"no file set"); return; } if(!bgav_open(x->decoder, x->filename->s_name)) { pd_error(x, "Could not open file %s", x->filename->s_name); } // only concerned with the first audio stream open_audio_format = bgav_get_audio_format(x->decoder, 0); // we can get audio formats that are unkown if (open_audio_format->sample_format == GAVL_SAMPLE_NONE) { pd_error(x, "sorry, this file has unsupported audio."); } gavl_time = bgav_get_duration(x->decoder, 0); outlet_float(x->x_obj.ob_outlet, (float)(gavl_time_to_samples(open_audio_format->samplerate, gavl_time) / (float)open_audio_format->samplerate)); }
int bg_avdec_init(avdec_priv * avdec) { int i; const bgav_metadata_t * m; const gavl_chapter_list_t * cl; gavl_time_t duration; avdec->num_tracks = bgav_num_tracks(avdec->dec); avdec->track_info = calloc(avdec->num_tracks, sizeof(*(avdec->track_info))); for(i = 0; i < avdec->num_tracks; i++) { avdec->track_info[i].num_audio_streams = bgav_num_audio_streams(avdec->dec, i); avdec->track_info[i].num_video_streams = bgav_num_video_streams(avdec->dec, i); avdec->track_info[i].num_text_streams = bgav_num_text_streams(avdec->dec, i); avdec->track_info[i].num_overlay_streams = bgav_num_overlay_streams(avdec->dec, i); if(bgav_can_seek(avdec->dec)) avdec->track_info[i].flags |= BG_TRACK_SEEKABLE; if(bgav_can_pause(avdec->dec)) avdec->track_info[i].flags |= BG_TRACK_PAUSABLE; if(avdec->track_info[i].num_audio_streams) { avdec->track_info[i].audio_streams = calloc(avdec->track_info[i].num_audio_streams, sizeof(*avdec->track_info[i].audio_streams)); } if(avdec->track_info[i].num_video_streams) { avdec->track_info[i].video_streams = calloc(avdec->track_info[i].num_video_streams, sizeof(*avdec->track_info[i].video_streams)); } if(avdec->track_info[i].num_text_streams) { avdec->track_info[i].text_streams = calloc(avdec->track_info[i].num_text_streams, sizeof(*avdec->track_info[i].text_streams)); } if(avdec->track_info[i].num_overlay_streams) { avdec->track_info[i].overlay_streams = calloc(avdec->track_info[i].num_overlay_streams, sizeof(*avdec->track_info[i].overlay_streams)); } duration = bgav_get_duration(avdec->dec, i); if(duration != GAVL_TIME_UNDEFINED) gavl_metadata_set_long(&avdec->track_info[i].metadata, GAVL_META_APPROX_DURATION, duration); /* Get metadata */ m = bgav_get_metadata(avdec->dec, i); gavl_metadata_copy(&avdec->track_info[i].metadata, m); /* Get chapters */ cl = bgav_get_chapter_list(avdec->dec, i); if(cl) { avdec->track_info[i].chapter_list = gavl_chapter_list_copy(cl); bg_chapter_list_set_default_names(avdec->track_info[i].chapter_list); } } return 1; }
bool ReadMedia::initFormat() { const gavl_audio_format_t * open_audio_format; const gavl_video_format_t * open_video_format; // we use the m_vfifosize to see if the user app wants video or not // then, we set m_video_stream_count to 0 if he doesn't want video if (m_video_stream_count > 0 && m_vfifosize > 0) { open_video_format = bgav_get_video_format(m_file, 0); if (open_video_format->pixelformat == GAVL_PIXELFORMAT_NONE) { printf("!!!sorry, pixelformat is not recognized.\n"); return false; } // let's check to see if the formats are the same, if they are the same // there is no reason to recreate the fifo or frames if ( gavl_video_formats_equal( &m_video_format, open_video_format) == 0 ) { // the formats are different gavl_video_format_copy (&m_video_format, open_video_format); if (m_video_frame != NULL) gavl_video_frame_destroy(m_video_frame); m_video_frame = gavl_video_frame_create(&m_video_format); gavl_video_frame_clear( m_video_frame, &m_video_format); if (m_fifovideo != NULL) delete m_fifovideo; m_fifovideo= new FifoVideoFrames( m_vfifosize , &m_video_format); } } else { m_video_stream_count = 0; m_veof = true; } // we use the m_afifosize to see if the user app wants audio or not // then, we set m_audio_stream_count to 0 if he doesn't want audio if (m_audio_stream_count > 0 && m_afifosize > 0) { open_audio_format = bgav_get_audio_format(m_file, 0); // we can get audio formats that are unkown if ( open_audio_format->sample_format == GAVL_SAMPLE_NONE) { printf("sorry, this file has unsupported audio.\n"); return false; } if ( gavl_audio_formats_equal(&m_audio_format, open_audio_format) == 0 ) { // audio formats are different // save the old spf int spf = m_audio_format.samples_per_frame; gavl_audio_format_copy(&m_audio_format, open_audio_format); if (m_audio_frame != NULL) { gavl_audio_frame_destroy(m_audio_frame); } // set it back to original m_audio_format.samples_per_frame = spf ; m_audio_frame = gavl_audio_frame_create(&m_audio_format); gavl_audio_frame_mute( m_audio_frame, &m_audio_format); if( m_fifoaudio != NULL ) delete m_fifoaudio; m_fifoaudio = new FifoAudioFrames( m_afifosize , &m_audio_format); } } else { // user doesn't want audio m_audio_stream_count = 0; m_aeof=true; } m_length_in_gavltime = bgav_get_duration ( m_file, 0);; m_length_in_seconds = gavl_time_to_seconds( m_length_in_gavltime ); m_num_samples = 0; m_num_frames = 0; if (m_audio_stream_count) { if ( bgav_can_seek_sample(m_file) == 1 ) { m_num_samples= bgav_audio_duration ( m_file, 0) ; } else { m_num_samples= gavl_time_to_samples( m_audio_format.samplerate , bgav_get_duration ( m_file, 0) ); } } // set frames WE NEED TO take care here for non-constant frame-rates if(m_video_stream_count) { if ( bgav_can_seek_sample(m_file) == 1 && m_video_format.framerate_mode == GAVL_FRAMERATE_CONSTANT) { m_num_frames = bgav_video_duration ( m_file, 0)/ m_video_format.frame_duration; } else if ( bgav_can_seek_sample(m_file) == 1 && m_video_format.framerate_mode == GAVL_FRAMERATE_VARIABLE ) { // FIXME what to do with non constant frame rates? m_num_frames=0; } else { m_num_frames = gavl_time_to_frames( m_video_format.timescale, m_video_format.frame_duration , bgav_get_duration ( m_file, 0) ); } } // printf("m_num_frames =%lld, duration = %lld , vid_duration=%lld\n", // m_num_frames, bgav_get_duration ( m_file, 0), bgav_video_duration ( m_file, 0) ); // set seconds if ( bgav_can_seek_sample(m_file) == 1) { gavl_time_t atime=0,vtime=0; if ( m_audio_stream_count ) atime = gavl_samples_to_time( m_audio_format.samplerate, m_num_samples ); if (m_video_stream_count && m_video_format.frame_duration > 0) { vtime = gavl_frames_to_time( m_video_format.timescale, m_video_format.frame_duration, m_num_frames ); } else if ( m_video_stream_count ) { // non constant framerate vtime = bgav_video_duration( m_file, 0); } // else rely on audio time m_length_in_gavltime = atime > vtime ? atime :vtime; m_length_in_seconds = gavl_time_to_seconds( m_length_in_gavltime ); //printf("atime=%ld, vtime=%ld, l_in_sec=%f\n", atime, vtime, m_length_in_seconds); } m_pcm_seek = SEEK_NOTHING; m_frame_seek = SEEK_NOTHING; return true; }