summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--rotord/Makefile4
-rw-r--r--rotord/rotor.cpp164
-rwxr-xr-xrotord/rotor.h7
3 files changed, 95 insertions, 80 deletions
diff --git a/rotord/Makefile b/rotord/Makefile
index 1ec0f56..6bf75a0 100644
--- a/rotord/Makefile
+++ b/rotord/Makefile
@@ -1,5 +1,7 @@
# The pre-processor and compiler options.
-MY_CFLAGS = -I ../ffmpeg -fpermissive -std=c++11
+MY_CFLAGS = -fpermissive -std=c++11
+
+# -I ../ffmpeg
# The linker options.
MY_LIBS = -lPocoNet -lPocoXML -lPocoUtil -lPocoFoundation -lvamp-hostsdk -lsndfile -lavcodec -lavutil -lavformat -lavfilter -lavdevice
diff --git a/rotord/rotor.cpp b/rotord/rotor.cpp
index 83fff6f..0662379 100644
--- a/rotord/rotor.cpp
+++ b/rotord/rotor.cpp
@@ -1,5 +1,5 @@
#include "rotor.h"
-#include <math.h>
+
//float equality
bool fequal(const float u,const float v){
@@ -29,8 +29,10 @@ void Render_context::runTask() {
mutex.unlock();
if (cmd==ANALYSE_AUDIO) {
state=ANALYSING_AUDIO;
- audio_analyser.process(audio_filename);
+ //audio_analyser.process(audio_filename);
//vampHost::runPlugin("","qm-vamp-plugins","qm-tempotracker", "",0, audio_filename, cerr,true);
+ load_audio(audio_filename);
+
state=AUDIO_READY;
}
sleep(100);
@@ -286,99 +288,103 @@ Command_response Render_context::session_command(const std::vector<std::string>&
//get all attributes of node and pass them as creation parameters?
//could just pass them all as strings, let the object choose what to do itself?
//YES
-bool Render_context::load_audio(string &filename){
- //load audio into memory and create thumbnail
- AVCodec *codec;
- AVCodecContext *c= NULL;
- int len;
- FILE *f;
- uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
- AVPacket avpkt;
- AVFrame *decoded_frame = NULL;
- av_init_packet(&avpkt);
+//http://blog.tomaka17.com/2012/03/libavcodeclibavformat-tutorial/
- // http://stackoverflow.com/questions/9799560/decode-audio-using-libavcodec-and-play-using-libao
+static int open_codec_context(int *stream_idx,AVFormatContext *fmt_ctx, enum AVMediaType type)
+{
+ int ret;
+ AVStream *st;
+ AVCodecContext *dec_ctx = NULL;
+ AVCodec *dec = NULL;
+ ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0);
+ if (ret < 0) {
+ fprintf(stderr, "Could not find %s stream in input file \n",
+ av_get_media_type_string(type));
+ return ret;
+ } else {
+ *stream_idx = ret;
+ st = fmt_ctx->streams[*stream_idx];
+ /* find decoder for the stream */
+ dec_ctx = st->codec;
+ dec = avcodec_find_decoder(dec_ctx->codec_id);
+ cerr << "Looking for decoder for codec "<< dec_ctx->codec_id<<endl;
+ if (!dec) {
+ fprintf(stderr, "Failed to find %s codec\n",
+ av_get_media_type_string(type));
+ return ret;
+ }
+ if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) {
+ fprintf(stderr, "Failed to open %s codec\n",
+ av_get_media_type_string(type));
+ return ret;
+ }
+ }
+ return 0;
+}
+
+
+
+bool Render_context::load_audio(string &filename){
+ //load audio into memory and create thumbnail
+
+ av_register_all();
+
+ std::shared_ptr<AVFormatContext> avFormat(avformat_alloc_context(), &avformat_free_context);
- /* find the mpeg audio decoder */
- codec = avcodec_find_decoder(AV_CODEC_ID_MP2);
- if (!codec) {
- fprintf(stderr, "Codec not found\n");
+ auto avFormatPtr = avFormat.get();
+ if (avformat_open_input(&avFormatPtr,filename.c_str(), NULL, NULL) != 0) {
+ cerr <<"Error while calling avformat_open_input (probably invalid file format)" << endl;
return false;
}
- c = avcodec_alloc_context3(codec);
- if (!c) {
- fprintf(stderr, "Could not allocate audio codec context\n");
+ if (avformat_find_stream_info(avFormat.get(), NULL) < 0) {
+ cerr << "Error while calling avformat_find_stream_info" << endl;
return false;
}
+
+ av_dump_format(avFormat.get(), 0, 0, false);
- /* open it */
- if (avcodec_open2(c, codec, NULL) < 0) {
- fprintf(stderr, "Could not open codec\n");
- return false;
+ for (unsigned int i = 0; i < avFormat->nb_streams; ++i) {
+ auto stream = avFormat->streams[i]; // pointer to a structure describing the stream
+ auto codecType = stream->codec->codec_type; // the type of data in this stream, notable values are AVMEDIA_TYPE_VIDEO and AVMEDIA_TYPE_AUDIO
+ auto codecID = stream->codec->codec_id; // identifier for the codec
+ cerr << "stream " << i << ": codec type: "<< codecType << " , id: "<<codecID <<endl;
}
-
- f = fopen(filename.c_str(), "rb");
- if (!f) {
- fprintf(stderr, "Could not open %s\n", filename.c_str());
+
+ static int audio_stream_idx = -1;
+ static AVCodecContext *audio_dec_ctx;
+ static AVStream *audio_stream = NULL;
+
+ auto fmt_ctx=avFormat.get();
+
+ if (open_codec_context(&audio_stream_idx, fmt_ctx, AVMEDIA_TYPE_AUDIO) >= 0) {
+ int nb_planes;
+ audio_stream = fmt_ctx->streams[audio_stream_idx];
+ audio_dec_ctx = audio_stream->codec;
+
+ /*
+ audio_dst_file = fopen(audio_dst_filename, "wb");
+ if (!audio_dst_file) {
+ cerr << "Could not open destination file %s\n"<< endl;
return false;
- }
-
- /* decode until eof */
- avpkt.data = inbuf;
- avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
-
- while (avpkt.size > 0) {
- int got_frame = 0;
-
- if (!decoded_frame) {
- if (!(decoded_frame = avcodec_alloc_frame())) {
- printf("Could not allocate audio frame\n");
- return false;
- }
- } else
- avcodec_get_frame_defaults(decoded_frame);
-
- len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt);
- if (len < 0) {
- fprintf(stderr, "Error while decoding\n");
- return false;
- }
- if (got_frame) {
- /* if a frame has been decoded, output it */
- int data_size = av_samples_get_buffer_size(NULL, c->channels,decoded_frame->nb_samples,c->sample_fmt, 1);
- //fwrite(decoded_frame->data[0], 1, data_size, outfile);
-
- //here is where we do something with the data
-
- }
- avpkt.size -= len;
- avpkt.data += len;
- avpkt.dts =
- avpkt.pts = AV_NOPTS_VALUE;
- if (avpkt.size < AUDIO_REFILL_THRESH) {
- /* Refill the input buffer, to avoid trying to decode
- * incomplete frames. Instead of this, one could also use
- * a parser, or use a proper container format through
- * libavformat. */
- memmove(inbuf, avpkt.data, avpkt.size);
- avpkt.data = inbuf;
- len = fread(avpkt.data + avpkt.size, 1,
- AUDIO_INBUF_SIZE - avpkt.size, f);
- if (len > 0)
- avpkt.size += len;
}
+ nb_planes = av_sample_fmt_is_planar(audio_dec_ctx->sample_fmt) ?
+ audio_dec_ctx->channels : 1;
+ audio_dst_data = av_mallocz(sizeof(uint8_t *) * nb_planes);
+ if (!audio_dst_data) {
+ cerr << "Could not allocate audio data buffers\n"<< endl;
+ return false;
+ */
+ }
+ else {
+ cerr << "Could not get codec context" << endl;
+ return false;
}
- fclose(f);
-
- avcodec_close(c);
- av_free(c);
- avcodec_free_frame(&decoded_frame);
-
return true;
}
+
bool Render_context::load_graph(string &filename){
printf("loading %s\n",filename.c_str());
if(xml.loadFile(filename) ){
diff --git a/rotord/rotor.h b/rotord/rotor.h
index 546e6f5..21161a2 100755
--- a/rotord/rotor.h
+++ b/rotord/rotor.h
@@ -41,6 +41,8 @@ next - build signal_output and make a working chain with dummy data
#include <unordered_map>
#include <deque>
+#include <math.h>
+#include <memory>
#include "Poco/Net/HTTPServer.h"
#include "Poco/Net/HTTPResponse.h"
@@ -66,12 +68,17 @@ using Poco::Net::HTTPResponse;
extern "C" {
#include "libavcodec/avcodec.h"
+ #include "libavformat/avformat.h"
#include "libavutil/opt.h"
#include "libavutil/channel_layout.h"
#include "libavutil/common.h"
#include "libavutil/imgutils.h"
#include "libavutil/mathematics.h"
#include "libavutil/samplefmt.h"
+
+ #include <libavutil/imgutils.h>
+ #include <libavutil/samplefmt.h>
+ //#include <libavutil/timestamp.h>
}
#define AUDIO_INBUF_SIZE 20480