diff options
Diffstat (limited to 'rotord/libavwrapper.cpp')
| -rwxr-xr-x | rotord/libavwrapper.cpp | 70 |
1 files changed, 58 insertions, 12 deletions
diff --git a/rotord/libavwrapper.cpp b/rotord/libavwrapper.cpp index 5e767e0..a5f2111 100755 --- a/rotord/libavwrapper.cpp +++ b/rotord/libavwrapper.cpp @@ -215,6 +215,33 @@ bool libav::decoder::openUsingInitializedContainer(enum PixelFormat formatParam) /* duration is in microsecs */ numFrames = (int)(( container->duration / (double)AV_TIME_BASE ) * pCtx->time_base.den + 0.5); + init_buffers_and_scaler(); + + /* Give some info on stderr about the file & stream */ + //dump_format(container, 0, fname, 0); + + previousFrameIndex = -1; + return true; +} +bool libav::decoder::reinit_buffers_and_scaler(){ + mutex.lock(); + if (NULL != Sctx) { + sws_freeContext(Sctx); + Sctx = NULL; + } + if (NULL != pRaw) { + av_free(pRaw); + pRaw = NULL; + } + if (NULL != pFrameRGB) { + av_free(pFrameRGB); + pFrameRGB = NULL; + } + mutex.unlock(); + init_buffers_and_scaler(); +} + +bool libav::decoder::init_buffers_and_scaler(){ /* Get framebuffers */ if (! (pRaw = avcodec_alloc_frame()) ) throw std::runtime_error(""); @@ -230,7 +257,7 @@ bool libav::decoder::openUsingInitializedContainer(enum PixelFormat formatParam) Sctx = NULL; } else { - numBytes = avpicture_get_size( format, pCtx->width, pCtx->height ); // RGB24 format + numBytes = avpicture_get_size( format, width, height ); // RGB24 format if (! (buffer = (uint8_t*)av_malloc(numBytes + FF_INPUT_BUFFER_PADDING_SIZE)) ) // RGB24 format throw std::runtime_error(""); if (! (blank = (uint8_t*)av_mallocz(avpicture_get_size(pCtx->pix_fmt,width,height))) ) // native codec format @@ -238,7 +265,7 @@ bool libav::decoder::openUsingInitializedContainer(enum PixelFormat formatParam) /* Init buffers */ avpicture_fill( (AVPicture * ) pFrameRGB, buffer, format, - pCtx->width, pCtx->height ); + width, height ); /* Init scale & convert */ if (! (Sctx=sws_getContext( @@ -252,12 +279,18 @@ bool libav::decoder::openUsingInitializedContainer(enum PixelFormat formatParam) NULL,NULL,NULL)) ) throw std::runtime_error(""); } +} - /* Give some info on stderr about the file & stream */ - //dump_format(container, 0, fname, 0); +bool libav::decoder::fetchFrame(int w, int h,int targetFrameIndex) +{ + if (w!=width||h!=height){ + width=w; + height=h; + cerr<<"libav::decoder reiniting to "<<width<<"x"<<height<<endl; //does not seem to be aware of wrong frame + reinit_buffers_and_scaler(); + } - previousFrameIndex = -1; - return true; + fetchFrame(targetFrameIndex); } bool libav::decoder::fetchFrame(int targetFrameIndex) @@ -1434,7 +1467,10 @@ bool libav::audioloader::setup(const std::string &filename){ std::cout << "This stream has " << codecContext->channels << " channels, a sample rate of " << codecContext->sample_rate << "Hz and "<<samples <<" samples" << std::endl; std::cout << "The data is in format " <<codecContext->sample_fmt<< " (aka "<< av_get_sample_fmt_name(codecContext->sample_fmt) << ") "<<std::endl; - if(av_sample_fmt_is_planar(codecContext->sample_fmt)) cerr<<"found planar audio"<<endl; + isPlanar=(av_sample_fmt_is_planar(codecContext->sample_fmt)==1); + + if(isPlanar) { cerr<<"found planar audio"<<endl; } + av_init_packet(&packet); //sample_processed=0; @@ -1522,20 +1558,30 @@ bool libav::audioloader::setup(const std::string &filename){ for (int j=0;j<channels;j++) { //int frame->format //format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames, enum AVSampleFormat for audio) - int ff=frame->format; + //int ff=frame->format; //uint64_t frame->channel_layout //Channel layout of the audio data. - uint64_t fcl=frame->channel_layout; + //uint64_t fcl=frame->channel_layout; //int frame->nb_extended_buf //Number of elements in extended_buf. - int fnb=frame->nb_extended_buf; + //int fnb=frame->nb_extended_buf; //int frame->decode_error_flags //decode error flags of the frame, set to a combination of FF_DECODE_ERROR_xxx flags if the decoder produced a frame, but there were errors during the decoding. - int fde=frame->decode_error_flags; + //int fde=frame->decode_error_flags; //uint16_t s=((uint16_t*) frame->buf[j]->data)[i]; - uint16_t s=((uint16_t*) frame->buf[0]->data)[j*channels+i]; + uint16_t s; + if (isPlanar) { + s=((uint16_t*) frame->buf[j]->data)[i]; + }else { + s=((uint16_t*) frame->buf[0]->data)[j*channels+i]; + } + + //where is audio grunge coming from? signed/ unsigned? doesn't seem to be byte order.. + // add +1 to data subscript with no effect + + //which? must be determined by format or layout of the channels //ALSO some kind of HEINOUS memory leak?? buffer[((sample_end+i)*frame->channels)+j]=s; |
