From becf9abfca17ec567cfdc1757ff8fb0ae6d67dea Mon Sep 17 00:00:00 2001 From: Comment Date: Tue, 12 Mar 2013 13:34:46 +0000 Subject: avcodec compiling --- rotord/rotor.cpp | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 1 deletion(-) (limited to 'rotord/rotor.cpp') diff --git a/rotord/rotor.cpp b/rotord/rotor.cpp index 28dab1e..83fff6f 100644 --- a/rotord/rotor.cpp +++ b/rotord/rotor.cpp @@ -286,8 +286,100 @@ Command_response Render_context::session_command(const std::vector& //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; -bool Render_context::load_graph(string filename){ + av_init_packet(&avpkt); + + // http://stackoverflow.com/questions/9799560/decode-audio-using-libavcodec-and-play-using-libao + + /* find the mpeg audio decoder */ + codec = avcodec_find_decoder(AV_CODEC_ID_MP2); + if (!codec) { + fprintf(stderr, "Codec not found\n"); + return false; + } + + c = avcodec_alloc_context3(codec); + if (!c) { + fprintf(stderr, "Could not allocate audio codec context\n"); + return false; + } + + /* open it */ + if (avcodec_open2(c, codec, NULL) < 0) { + fprintf(stderr, "Could not open codec\n"); + return false; + } + + f = fopen(filename.c_str(), "rb"); + if (!f) { + fprintf(stderr, "Could not open %s\n", filename.c_str()); + 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; + } + } + + 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) ){ graph=Graph(xml.getAttribute("patchbay","ID","",0),xml.getValue("patchbay","",0)); -- cgit v1.2.3