more use of smart pointers
This commit is contained in:
parent
223b5fab18
commit
2036185cc1
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,5 @@
|
|||||||
.idea/
|
.idea/
|
||||||
lib/
|
lib/
|
||||||
bin/
|
bin/
|
||||||
|
CMakeLists.txt.user
|
||||||
|
|
||||||
|
|||||||
@ -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
|
// FIXME: we're losing last samples in case when fifo queue isn't full enough for encoder
|
||||||
while(fifo.enoughSamples()) {
|
while(fifo.enoughSamples()) {
|
||||||
auto frame = aEncoder->createAudioFrame();
|
auto frame = aEncoder->createAudioFrame();
|
||||||
fifo.readFrame(frame);
|
fifo.readFrame(frame);
|
||||||
frame.setPts(aPts);
|
frame->setPts(aPts);
|
||||||
aPts += frame.samplesCount();
|
aPts += frame->samplesCount();
|
||||||
auto encPacket = aEncoder->encode(frame);
|
auto encPacket = aEncoder->encode(frame);
|
||||||
if(!encPacket) continue;
|
if(!encPacket) continue;
|
||||||
encPacket.setStreamIndex(AUDIO_STREAM_INDEX);
|
encPacket.setStreamIndex(AUDIO_STREAM_INDEX);
|
||||||
@ -78,7 +78,7 @@ int main(int argc, char** argv) {
|
|||||||
auto frame = vDecoder->decode(packet);
|
auto frame = vDecoder->decode(packet);
|
||||||
if(needScaling)
|
if(needScaling)
|
||||||
frame = scaler.scale(frame);
|
frame = scaler.scale(frame);
|
||||||
frame.setPictureType(AV_PICTURE_TYPE_NONE);
|
frame->setPictureType(AV_PICTURE_TYPE_NONE);
|
||||||
auto encPacket = vEncoder->encode(frame);
|
auto encPacket = vEncoder->encode(frame);
|
||||||
if(!encPacket) continue;
|
if(!encPacket) continue;
|
||||||
encPacket.setStreamIndex(VIDEO_STREAM_INDEX);
|
encPacket.setStreamIndex(VIDEO_STREAM_INDEX);
|
||||||
|
|||||||
@ -25,10 +25,10 @@ int main(int argc, char** argv) {
|
|||||||
AVMediaType packetType = input.packetType(packet);
|
AVMediaType packetType = input.packetType(packet);
|
||||||
if(packetType == AVMEDIA_TYPE_VIDEO) {
|
if(packetType == AVMEDIA_TYPE_VIDEO) {
|
||||||
auto frame = vDecoder->decode(packet);
|
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) {
|
if(curKeyFrame == KEY_FRAME_TO_SAVE) {
|
||||||
frame = scaler.scale(frame);
|
frame = scaler.scale(frame);
|
||||||
frame.setPictureType(AV_PICTURE_TYPE_NONE);
|
frame->setPictureType(AV_PICTURE_TYPE_NONE);
|
||||||
auto encPacket = vEncoder->encode(frame);
|
auto encPacket = vEncoder->encode(frame);
|
||||||
if(!encPacket) continue;
|
if(!encPacket) continue;
|
||||||
encPacket.setStreamIndex(0);
|
encPacket.setStreamIndex(0);
|
||||||
|
|||||||
@ -49,8 +49,8 @@ namespace ffcpp {
|
|||||||
void setPixelFormat(AVPixelFormat pixelFormat);
|
void setPixelFormat(AVPixelFormat pixelFormat);
|
||||||
|
|
||||||
FramePtr decode(Packet& packet);
|
FramePtr decode(Packet& packet);
|
||||||
Packet encode(AVFrame* frame);
|
Packet encode(FramePtr frame);
|
||||||
Frame createAudioFrame() const;
|
FramePtr createAudioFrame() const;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Codec(Codec&& c) noexcept;
|
Codec(Codec&& c) noexcept;
|
||||||
|
|||||||
@ -15,10 +15,10 @@ namespace ffcpp {
|
|||||||
int _frameSize;
|
int _frameSize;
|
||||||
public:
|
public:
|
||||||
FifoQueue(AVSampleFormat sampleFormat, int channels, int frameSize);
|
FifoQueue(AVSampleFormat sampleFormat, int channels, int frameSize);
|
||||||
void addSamples(const Frame& frame);
|
void addSamples(FramePtr frame);
|
||||||
void addSamples(void** data, int samplesCount);
|
void addSamples(void** data, int samplesCount);
|
||||||
bool enoughSamples() const;
|
bool enoughSamples() const;
|
||||||
void readFrame(Frame& frame);
|
void readFrame(FramePtr frame);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -34,7 +34,7 @@ namespace ffcpp {
|
|||||||
int samplesCount() const;
|
int samplesCount() const;
|
||||||
void setPts(int pts);
|
void setPts(int pts);
|
||||||
bool isKeyFrame() const;
|
bool isKeyFrame() const;
|
||||||
int pts() const;
|
int pts() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -23,7 +23,7 @@ namespace ffcpp {
|
|||||||
Resampler(CodecPtr decoder, CodecPtr encoder);
|
Resampler(CodecPtr decoder, CodecPtr encoder);
|
||||||
~Resampler();
|
~Resampler();
|
||||||
|
|
||||||
Frame resample(Frame& inFrame);
|
FramePtr resample(FramePtr inFrame);
|
||||||
static bool needResampling(CodecPtr decoder, CodecPtr encoder);
|
static bool needResampling(CodecPtr decoder, CodecPtr encoder);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -120,19 +120,19 @@ namespace ffcpp {
|
|||||||
return frame;
|
return frame;
|
||||||
}
|
}
|
||||||
|
|
||||||
Packet Codec::encode(AVFrame* frame) {
|
Packet Codec::encode(FramePtr frame) {
|
||||||
Packet packet;
|
Packet packet;
|
||||||
int gotPacket = 0;
|
int gotPacket = 0;
|
||||||
auto encFunc = (_codecCtx->codec_type == AVMEDIA_TYPE_VIDEO ? avcodec_encode_video2 : avcodec_encode_audio2);
|
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");
|
if(res < 0) throw std::runtime_error("cannot encode frame");
|
||||||
|
|
||||||
return packet;
|
return packet;
|
||||||
}
|
}
|
||||||
|
|
||||||
Frame Codec::createAudioFrame() const {
|
FramePtr Codec::createAudioFrame() const {
|
||||||
return Frame(_codecCtx->frame_size, _codecCtx->channels, _codecCtx->codec->sample_fmts[0], _codecCtx->sample_rate);
|
return std::make_shared<Frame>(_codecCtx->frame_size, _codecCtx->channels, _codecCtx->codec->sample_fmts[0], _codecCtx->sample_rate);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,9 +11,8 @@ namespace ffcpp {
|
|||||||
throw std::runtime_error("cannot create audio fifo queue");
|
throw std::runtime_error("cannot create audio fifo queue");
|
||||||
}
|
}
|
||||||
|
|
||||||
void FifoQueue::addSamples(const Frame &frame) {
|
void FifoQueue::addSamples(FramePtr frame) {
|
||||||
const AVFrame* frameImpl = frame;
|
addSamples((void**)frame->nativePtr()->data, frame->samplesCount());
|
||||||
addSamples((void**)frameImpl->data, frameImpl->nb_samples);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void FifoQueue::addSamples(void **data, int samplesCount) {
|
void FifoQueue::addSamples(void **data, int samplesCount) {
|
||||||
@ -28,10 +27,8 @@ namespace ffcpp {
|
|||||||
return av_audio_fifo_size(_fifo) >= _frameSize;
|
return av_audio_fifo_size(_fifo) >= _frameSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
void FifoQueue::readFrame(Frame& frame) {
|
void FifoQueue::readFrame(FramePtr frame) {
|
||||||
AVFrame* nativeFrame = frame;
|
int res = av_audio_fifo_read(_fifo, (void**)frame->nativePtr()->data, _frameSize);
|
||||||
|
|
||||||
int res = av_audio_fifo_read(_fifo, (void**)nativeFrame->data, _frameSize);
|
|
||||||
throwIfError(res, "cannot read data from fifo queue");
|
throwIfError(res, "cannot read data from fifo queue");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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);
|
int channelsCount = av_get_channel_layout_nb_channels(_dstChannelLayout);
|
||||||
AVFrame* fin = inFrame;
|
int outSamples = swr_get_out_samples(_swrContext, inFrame->samplesCount());
|
||||||
int outSamples = swr_get_out_samples(_swrContext, fin->nb_samples);
|
|
||||||
|
|
||||||
Frame outFrame(outSamples, channelsCount, _dstSampleFormat, _dstSampleRate);
|
FramePtr outFrame = std::make_shared<Frame>(outSamples, channelsCount, _dstSampleFormat, _dstSampleRate);
|
||||||
int res = swr_convert_frame(_swrContext, outFrame, inFrame);
|
int res = swr_convert_frame(_swrContext, outFrame->nativePtr(), inFrame->nativePtr());
|
||||||
throwIfError(res, "cannot convert audio frame");
|
throwIfError(res, "cannot convert audio frame");
|
||||||
|
|
||||||
return outFrame;
|
return outFrame;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user