From 2036185cc1aa4aa97fbf393c966313b95bef00eb Mon Sep 17 00:00:00 2001 From: selim mustafaev Date: Fri, 31 Mar 2017 00:36:49 +0300 Subject: [PATCH] more use of smart pointers --- .gitignore | 1 + examples/ffConv.cpp | 8 ++++---- examples/ffPreview.cpp | 6 +++--- include/ffcpp/Codec.h | 4 ++-- include/ffcpp/FifoQueue.h | 4 ++-- include/ffcpp/Frame.h | 2 +- include/ffcpp/Resampler.h | 2 +- src/Codec.cpp | 8 ++++---- src/FifoQueue.cpp | 11 ++++------- src/Resampler.cpp | 9 ++++----- 10 files changed, 26 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index 1bd8f2e..9e9eef9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .idea/ lib/ bin/ +CMakeLists.txt.user diff --git a/examples/ffConv.cpp b/examples/ffConv.cpp index 5e5eada..46ca8e7 100644 --- a/examples/ffConv.cpp +++ b/examples/ffConv.cpp @@ -65,9 +65,9 @@ int main(int argc, char** argv) { // FIXME: we're losing last samples in case when fifo queue isn't full enough for encoder while(fifo.enoughSamples()) { auto frame = aEncoder->createAudioFrame(); - fifo.readFrame(frame); - frame.setPts(aPts); - aPts += frame.samplesCount(); + fifo.readFrame(frame); + frame->setPts(aPts); + aPts += frame->samplesCount(); auto encPacket = aEncoder->encode(frame); if(!encPacket) continue; encPacket.setStreamIndex(AUDIO_STREAM_INDEX); @@ -78,7 +78,7 @@ int main(int argc, char** argv) { auto frame = vDecoder->decode(packet); if(needScaling) frame = scaler.scale(frame); - frame.setPictureType(AV_PICTURE_TYPE_NONE); + frame->setPictureType(AV_PICTURE_TYPE_NONE); auto encPacket = vEncoder->encode(frame); if(!encPacket) continue; encPacket.setStreamIndex(VIDEO_STREAM_INDEX); diff --git a/examples/ffPreview.cpp b/examples/ffPreview.cpp index 030b18d..63072eb 100644 --- a/examples/ffPreview.cpp +++ b/examples/ffPreview.cpp @@ -25,10 +25,10 @@ int main(int argc, char** argv) { AVMediaType packetType = input.packetType(packet); if(packetType == AVMEDIA_TYPE_VIDEO) { auto frame = vDecoder->decode(packet); - if(frame.isKeyFrame() && (frame.pts() > 0 || KEY_FRAME_TO_SAVE == 0)) { + if(frame->isKeyFrame() && (frame->pts() > 0 || KEY_FRAME_TO_SAVE == 0)) { if(curKeyFrame == KEY_FRAME_TO_SAVE) { frame = scaler.scale(frame); - frame.setPictureType(AV_PICTURE_TYPE_NONE); + frame->setPictureType(AV_PICTURE_TYPE_NONE); auto encPacket = vEncoder->encode(frame); if(!encPacket) continue; encPacket.setStreamIndex(0); @@ -45,4 +45,4 @@ int main(int argc, char** argv) { output.writeTrailer(); return 0; -} \ No newline at end of file +} diff --git a/include/ffcpp/Codec.h b/include/ffcpp/Codec.h index dc3797d..e5b1ef2 100644 --- a/include/ffcpp/Codec.h +++ b/include/ffcpp/Codec.h @@ -49,8 +49,8 @@ namespace ffcpp { void setPixelFormat(AVPixelFormat pixelFormat); FramePtr decode(Packet& packet); - Packet encode(AVFrame* frame); - Frame createAudioFrame() const; + Packet encode(FramePtr frame); + FramePtr createAudioFrame() const; public: Codec(Codec&& c) noexcept; diff --git a/include/ffcpp/FifoQueue.h b/include/ffcpp/FifoQueue.h index 6404f05..947a7a2 100644 --- a/include/ffcpp/FifoQueue.h +++ b/include/ffcpp/FifoQueue.h @@ -15,10 +15,10 @@ namespace ffcpp { int _frameSize; public: FifoQueue(AVSampleFormat sampleFormat, int channels, int frameSize); - void addSamples(const Frame& frame); + void addSamples(FramePtr frame); void addSamples(void** data, int samplesCount); bool enoughSamples() const; - void readFrame(Frame& frame); + void readFrame(FramePtr frame); }; } diff --git a/include/ffcpp/Frame.h b/include/ffcpp/Frame.h index c59343d..c035c48 100644 --- a/include/ffcpp/Frame.h +++ b/include/ffcpp/Frame.h @@ -34,7 +34,7 @@ namespace ffcpp { int samplesCount() const; void setPts(int pts); bool isKeyFrame() const; - int pts() const; + int pts() const; }; } diff --git a/include/ffcpp/Resampler.h b/include/ffcpp/Resampler.h index 0444d4d..2e0e3c8 100644 --- a/include/ffcpp/Resampler.h +++ b/include/ffcpp/Resampler.h @@ -23,7 +23,7 @@ namespace ffcpp { Resampler(CodecPtr decoder, CodecPtr encoder); ~Resampler(); - Frame resample(Frame& inFrame); + FramePtr resample(FramePtr inFrame); static bool needResampling(CodecPtr decoder, CodecPtr encoder); }; diff --git a/src/Codec.cpp b/src/Codec.cpp index 75e7f1e..0a42e4b 100644 --- a/src/Codec.cpp +++ b/src/Codec.cpp @@ -120,19 +120,19 @@ namespace ffcpp { return frame; } - Packet Codec::encode(AVFrame* frame) { + Packet Codec::encode(FramePtr frame) { Packet packet; int gotPacket = 0; auto encFunc = (_codecCtx->codec_type == AVMEDIA_TYPE_VIDEO ? avcodec_encode_video2 : avcodec_encode_audio2); - int res = encFunc(_codecCtx, packet, frame, &gotPacket); + int res = encFunc(_codecCtx, packet, frame->nativePtr(), &gotPacket); if(res < 0) throw std::runtime_error("cannot encode frame"); return packet; } - Frame Codec::createAudioFrame() const { - return Frame(_codecCtx->frame_size, _codecCtx->channels, _codecCtx->codec->sample_fmts[0], _codecCtx->sample_rate); + FramePtr Codec::createAudioFrame() const { + return std::make_shared(_codecCtx->frame_size, _codecCtx->channels, _codecCtx->codec->sample_fmts[0], _codecCtx->sample_rate); } } diff --git a/src/FifoQueue.cpp b/src/FifoQueue.cpp index e47d264..1ea6567 100644 --- a/src/FifoQueue.cpp +++ b/src/FifoQueue.cpp @@ -11,9 +11,8 @@ namespace ffcpp { throw std::runtime_error("cannot create audio fifo queue"); } - void FifoQueue::addSamples(const Frame &frame) { - const AVFrame* frameImpl = frame; - addSamples((void**)frameImpl->data, frameImpl->nb_samples); + void FifoQueue::addSamples(FramePtr frame) { + addSamples((void**)frame->nativePtr()->data, frame->samplesCount()); } void FifoQueue::addSamples(void **data, int samplesCount) { @@ -28,10 +27,8 @@ namespace ffcpp { return av_audio_fifo_size(_fifo) >= _frameSize; } - void FifoQueue::readFrame(Frame& frame) { - AVFrame* nativeFrame = frame; - - int res = av_audio_fifo_read(_fifo, (void**)nativeFrame->data, _frameSize); + void FifoQueue::readFrame(FramePtr frame) { + int res = av_audio_fifo_read(_fifo, (void**)frame->nativePtr()->data, _frameSize); throwIfError(res, "cannot read data from fifo queue"); } diff --git a/src/Resampler.cpp b/src/Resampler.cpp index 88f80cf..cebc0d0 100644 --- a/src/Resampler.cpp +++ b/src/Resampler.cpp @@ -43,13 +43,12 @@ namespace ffcpp { } } - Frame Resampler::resample(Frame& inFrame) { + FramePtr Resampler::resample(FramePtr inFrame) { int channelsCount = av_get_channel_layout_nb_channels(_dstChannelLayout); - AVFrame* fin = inFrame; - int outSamples = swr_get_out_samples(_swrContext, fin->nb_samples); + int outSamples = swr_get_out_samples(_swrContext, inFrame->samplesCount()); - Frame outFrame(outSamples, channelsCount, _dstSampleFormat, _dstSampleRate); - int res = swr_convert_frame(_swrContext, outFrame, inFrame); + FramePtr outFrame = std::make_shared(outSamples, channelsCount, _dstSampleFormat, _dstSampleRate); + int res = swr_convert_frame(_swrContext, outFrame->nativePtr(), inFrame->nativePtr()); throwIfError(res, "cannot convert audio frame"); return outFrame;