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/
|
||||
lib/
|
||||
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
|
||||
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);
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@ -34,7 +34,7 @@ namespace ffcpp {
|
||||
int samplesCount() const;
|
||||
void setPts(int pts);
|
||||
bool isKeyFrame() const;
|
||||
int pts() const;
|
||||
int pts() const;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@ -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);
|
||||
};
|
||||
|
||||
|
||||
@ -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<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");
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
|
||||
@ -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<Frame>(outSamples, channelsCount, _dstSampleFormat, _dstSampleRate);
|
||||
int res = swr_convert_frame(_swrContext, outFrame->nativePtr(), inFrame->nativePtr());
|
||||
throwIfError(res, "cannot convert audio frame");
|
||||
|
||||
return outFrame;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user