From 440ba17594d17736a56cac0d89be97fb63577989 Mon Sep 17 00:00:00 2001 From: Tim Redfern Date: Mon, 16 Sep 2013 14:31:04 +0100 Subject: ffmpegsource audio loading --- rotord/src/graph.cpp | 148 ++++++++++++++++++++++++++++++++++++++++++++ rotord/src/graph.h | 5 +- rotord/src/libavwrapper.cpp | 66 +++++++++++++++----- rotord/src/libavwrapper.h | 107 +++++++++++++++++++++++++------- rotord/src/rotor.h | 16 ++--- 5 files changed, 295 insertions(+), 47 deletions(-) diff --git a/rotord/src/graph.cpp b/rotord/src/graph.cpp index 5487717..29b46be 100644 --- a/rotord/src/graph.cpp +++ b/rotord/src/graph.cpp @@ -80,6 +80,154 @@ bool Graph::preview(xmlIO &XML,string &node,string &_format,int frame,int w,int } bool Graph::video_render(const string &output_filename,const float framerate) { + //vector loaders=find_nodes("video_loader"); + //for (auto i:loaders){ + // if (!dynamic_cast(i)->isLoaded) { + // cerr<<"Rotor: all loaders must be populated before rendering"<(find_node("video_output")); + for (auto f: find_nodes("video_feedback")){ + (dynamic_cast(f))->set_feedback(&(video_output->image)); + } + // + //setup defaults + int bitRate=5000000; + AVCodecID codecId=AV_CODEC_ID_H264; //MPEG4; + std::string container ="mp4"; + + //at the moment it crashes if you render before audio is loaded and also on 2nd render + libav::exporter exporter; + + float spct=100.0f/duration; + + if (exporter.setup(outW,outH,bitRate,framerate,container)) { //codecId, + if (exporter.record(output_filename)) { + + libav::audio_decoder audioloader; + + bool usingaudio=audioloader.open(audio_filename); + float *avframe=nullptr; + + Logger& logger = Logger::get("Rotor"); + logger.information("Video_output rendering "+output_filename+": "+toString(duration)+" seconds at "+toString(framerate)+" fps, audio frame size: "+toString(exporter.get_audio_framesize())); + //25fps video and 43.06640625fps audio? hmm + //how to get the timecodes correct for the interleaved files + + struct timeval start, end; + + gettimeofday(&start, NULL); + + uint16_t *audioframe=nullptr; + uint16_t *audio=nullptr; + int samples_in_frame; + + if (usingaudio){ + samples_in_frame=(audioloader.get_sample_rate())/framerate; + string whether=usingaudio?"Loading":"Cannot load"; + logger.information(whether+" audio file: "+audio_filename+", each frame contains "+toString(samples_in_frame)+" samples at "+toString(audioloader.get_sample_rate())+" hz"); + audioframe=new uint16_t[(samples_in_frame+exporter.get_audio_framesize())*audioloader.get_number_channels()]; + audio=new uint16_t[samples_in_frame*audioloader.get_number_channels()]; + } + + float vstep=1.0f/framerate; + float v=0.0f; + float vf=0.0f; + float af=0.0f; + int aoffs=0; + int audioend=0; + Audio_frame *a; + int64_t sample_start=0; + while (vf0){ + //shift down samples + int s=0; + while ((s+aoffs)get_output(Frame_spec(vf,framerate,duration,outW,outH,a)); + } + else i=video_output->get_output(Frame_spec(vf,framerate,duration,outW,outH)); + if (i) { + exporter.encodeFrame(i->RGBdata); + } + vf+=vstep; + progress=vf/duration; + if (usingaudio) {delete a;}; + } + + exporter.finishRecord(); + + gettimeofday(&end, NULL); + + float mtime = ((end.tv_sec-start.tv_sec) + (end.tv_usec-start.tv_usec)/1000000.0) + 0.5; + + logger.information("Video_output: rendered "+output_filename+": in "+toString(mtime)+" seconds"); + + if (usingaudio) { + audioloader.cleanup(); + delete[] audioframe; + delete[] audio; + } + + + + return true; + } + } + + return false; + } + + cerr<<"Rotor: video output node not found"< loaders=find_nodes("video_loader"); //for (auto i:loaders){ // if (!dynamic_cast(i)->isLoaded) { diff --git a/rotord/src/graph.h b/rotord/src/graph.h index 6a2fc2d..9207559 100644 --- a/rotord/src/graph.h +++ b/rotord/src/graph.h @@ -42,6 +42,7 @@ namespace Rotor { vector find_nodes(const string &type); //could be a way of finding a set based on capabilities? Node* find_node(const string &type); bool signal_render(string &signal_xml,const float framerate); + bool _video_render(const string &output_filename,const float framerate); bool video_render(const string &output_filename,const float framerate); bool load(string data,string media_path); bool loadFile(string &filename,string media_path); @@ -70,7 +71,7 @@ namespace Rotor { public: bool make(const string &inputfilename,int w,int h,const string &outputfilename) { if (player.open(inputfilename)){ - if (player.fetchFrame(w,h,player.getNumberOfFrames()/4)) { + if (player.fetch_frame(w,h,player.get_number_frames()/4)) { Image i; i.setup_fromRGB(w,h,player.frame->Data[0],player.frame->Linesize[0]-(w*3)); cv::Mat cp; @@ -83,7 +84,7 @@ namespace Rotor { return false; } private: - libav::ffms2_decoder player; + libav::video_decoder player; }; } #endif diff --git a/rotord/src/libavwrapper.cpp b/rotord/src/libavwrapper.cpp index 717667c..1fe0e5a 100644 --- a/rotord/src/libavwrapper.cpp +++ b/rotord/src/libavwrapper.cpp @@ -557,45 +557,45 @@ bool libav::decoder::avtry(int result, const std::string& msg) { return true; } - void libav::ffms2_decoder::cleanup(){ + void libav::video_decoder::cleanup(){ if (loaded) { mutex.lock(); - FFMS_DestroyVideoSource(videosource); + FFMS_DestroyVideoSource(source); mutex.unlock(); loaded=false; } } -bool libav::ffms2_decoder::open(const std::string& filename){ +bool libav::video_decoder::open(const std::string& filename){ mutex.lock(); loaded=false; - FFMS_Index *index = FFMS_MakeIndex(filename.c_str(), 0, 0, NULL, NULL, FFMS_IEH_IGNORE, NULL, NULL, &errinfo); + FFMS_Index *index = FFMS_MakeIndex(filename.c_str(), 0, 0, NULL, NULL, FFMS_IEH_IGNORE, NULL, NULL, &err); if (index == NULL) { - std::cerr<<"ffmpegsource: "<EncodedWidth; h=propframe->EncodedHeight; //propframe->EncodedPixelFormat; - if (FFMS_SetOutputFormatV2(videosource, pixfmts, propframe->EncodedWidth, propframe->EncodedHeight, FFMS_RESIZER_BICUBIC, &errinfo)) { - std::cerr<<"ffmpegsource: "<EncodedWidth, propframe->EncodedHeight, FFMS_RESIZER_BICUBIC, &err)) { + std::cerr<<"ffmpegsource: "<FPSNumerator)/((float)videoprops->FPSDenominator)); + float get_framerate(){ + if (loaded) return (((float)props->FPSNumerator)/((float)props->FPSDenominator)); else return -1.0f; } - int getNumberOfFrames(){ - if (loaded) return videoprops->NumFrames; + int get_number_frames(){ + if (loaded) return props->NumFrames; else return -1; } - int getNumberOfChannels(){ + int get_number_channels(){ return 3; //this is what we convert to } - int getWidth(){ + int get_width(){ return w; } - int getHeight(){ + int get_height(){ return h; } - bool fetchFrame(int width,int height,int wanted){ - if (FFMS_SetOutputFormatV2(videosource, pixfmts, width, height, FFMS_RESIZER_BICUBIC, &errinfo)) { - std::cerr<<"ffmpegsource: "<NumFrames, &errinfo); + frame = FFMS_GetFrame(source, wanted%props->NumFrames, &err); if (frame == NULL) { - std::cerr<<"ffmpegsource: "<SampleFormat; + else return 0; + } + int get_sample_rate(){ + if (props) return props->SampleRate; + else return 0; + } + int get_bit_depth(){ + if (props) return props->BitsPerSample; + else return 0; + } + int get_number_channels(){ + if (props) return props->Channels; + else return 0; + } + int64_t get_channel_layout(){ + if (props) return props->ChannelLayout; + else return 0; + } + float get_duration(){ + if (props) return ((float)props->NumSamples)/props->SampleRate; + else return 0; + } + bool get_samples(void *buf,int64_t start, int64_t count){ + if (source) { + if (FFMS_GetAudio(source, buf, start, count, &err)) { + std::cerr<<"ffmpegsource: "<value==0.0f?player.getFrameRate():parameters["framerate"]->value); + float clipframerate=(parameters["framerate"]->value==0.0f?player.get_framerate():parameters["framerate"]->value); float clipspeed=(clipframerate/frame.framerate)*parameters["speed"]->value; - int wanted=(((int) ((frame.time*frame.framerate)+0.5))%max(1,player.getNumberOfFrames()-1)); + int wanted=(((int) ((frame.time*frame.framerate)+0.5))%max(1,player.get_number_frames()-1)); if (wanted!=lastframe){ - if (!player.fetchFrame(frame.w,frame.h,wanted)) { //seek fail + if (!player.fetch_frame(frame.w,frame.h,wanted)) { //seek fail Poco::Logger& logger = Poco::Logger::get("Rotor"); logger.error("Video_loader failed to seek frame "+toString(wanted)+" of "+attributes["filename"]->value); @@ -966,7 +966,7 @@ namespace Rotor { bool isLoaded; private: //ffmpegsource::decoder player; - libav::ffms2_decoder player; + libav::video_decoder player; int lastframe; }; class Video_output: public Image_node { -- cgit v1.2.3