added class wrapper for SwrContext

This commit is contained in:
Selim Mustafaev 2016-11-03 08:11:21 +03:00
parent 1bb9d908da
commit 2c1673538d
5 changed files with 67 additions and 13 deletions

View File

@ -11,6 +11,6 @@ link_directories(${FFMPEG_LIBRARY_DIRS})
#message(FATAL_ERROR ${FFMPEG_LIBRARIES}) #message(FATAL_ERROR ${FFMPEG_LIBRARIES})
set(SOURCE_FILES main.cpp ffcpp/MediaFile.cpp ffcpp/MediaFile.h ffcpp/ffcpp.cpp ffcpp/ffcpp.h ffcpp/Stream.cpp ffcpp/Stream.h ffcpp/Codec.cpp ffcpp/Codec.h ffcpp/Packet.cpp ffcpp/Packet.h ffcpp/Frame.cpp ffcpp/Frame.h ffcpp/FifoQueue.cpp ffcpp/FifoQueue.h ffcpp/Scaler.cpp ffcpp/Scaler.h) set(SOURCE_FILES main.cpp ffcpp/MediaFile.cpp ffcpp/MediaFile.h ffcpp/ffcpp.cpp ffcpp/ffcpp.h ffcpp/Stream.cpp ffcpp/Stream.h ffcpp/Codec.cpp ffcpp/Codec.h ffcpp/Packet.cpp ffcpp/Packet.h ffcpp/Frame.cpp ffcpp/Frame.h ffcpp/FifoQueue.cpp ffcpp/FifoQueue.h ffcpp/Scaler.cpp ffcpp/Scaler.h ffcpp/Resampler.cpp ffcpp/Resampler.h)
add_executable(ffConv ${SOURCE_FILES}) add_executable(ffConv ${SOURCE_FILES})
target_link_libraries(ffConv ${FFMPEG_LIBRARIES}) target_link_libraries(ffConv ${FFMPEG_LIBRARIES})

View File

@ -20,16 +20,6 @@ namespace ffcpp {
} }
Frame::Frame(int width, int height, AVPixelFormat pixelFormat): Frame() { Frame::Frame(int width, int height, AVPixelFormat pixelFormat): Frame() {
/*
_frame->width = width;
_frame->height = height;
_frame->format = pixelFormat;
int res = av_frame_get_buffer(_frame, 0);
throwIfError(res, "cannot initialize buffer in video frame");
*/
_frame->width = width; _frame->width = width;
_frame->height = height; _frame->height = height;
_frame->format = pixelFormat; _frame->format = pixelFormat;

39
ffcpp/Resampler.cpp Normal file
View File

@ -0,0 +1,39 @@
#include "Resampler.h"
#include "ffcpp.h"
#include <stdexcept>
extern "C" {
#include <libavutil/opt.h>
}
namespace ffcpp {
Resampler::Resampler(int inChannelLayout, int inSampleRate, AVSampleFormat inSampleFormat, int outChannelLayout,
int outSampleRate, AVSampleFormat outSampleFormat) {
_swrContext = swr_alloc();
if(!_swrContext) {
throw new std::runtime_error("cannot create resampler");
}
av_opt_set_int(_swrContext, "in_channel_layout", inChannelLayout, 0);
av_opt_set_int(_swrContext, "in_sample_rate", inSampleRate, 0);
av_opt_set_sample_fmt(_swrContext, "in_sample_fmt", inSampleFormat, 0);
av_opt_set_int(_swrContext, "out_channel_layout", outChannelLayout, 0);
av_opt_set_int(_swrContext, "out_sample_rate", outSampleRate, 0);
av_opt_set_sample_fmt(_swrContext, "out_sample_fmt", outSampleFormat, 0);
int res = swr_init(_swrContext);
throwIfError(res, "cannot init resampler");
}
Resampler::~Resampler() {
if(_swrContext) {
swr_free(&_swrContext);
}
}
Frame Resampler::Resample(Frame& inFrame) {
return Frame();
}
}

26
ffcpp/Resampler.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef FFCONV_RESAMPLER_H
#define FFCONV_RESAMPLER_H
#include "Frame.h"
extern "C" {
#include <libswresample/swresample.h>
}
namespace ffcpp {
class Resampler {
private:
SwrContext* _swrContext;
public:
Resampler(int inChannelLayout, int inSampleRate, AVSampleFormat inSampleFormat,
int outChannelLayout, int outSampleRate, AVSampleFormat outSampleFormat);
~Resampler();
Frame Resample(Frame& inFrame);
};
}
#endif //FFCONV_RESAMPLER_H

View File

@ -59,7 +59,6 @@ int main(int argc, char** argv) {
while(auto packet = input.readPacket()) { while(auto packet = input.readPacket()) {
AVMediaType packetType = input.packetType(packet); AVMediaType packetType = input.packetType(packet);
if(packetType == AVMEDIA_TYPE_AUDIO) { if(packetType == AVMEDIA_TYPE_AUDIO) {
continue;
auto frame = aDecoder.decode(packet); auto frame = aDecoder.decode(packet);
fifo.addSamples(frame); fifo.addSamples(frame);
if(!fifo.enoughSamples()) continue; if(!fifo.enoughSamples()) continue;
@ -88,7 +87,7 @@ int main(int argc, char** argv) {
} }
flushEncoder(output, vEncoder, vStream, outVStream, VIDEO_STREAM_INDEX); flushEncoder(output, vEncoder, vStream, outVStream, VIDEO_STREAM_INDEX);
//flushEncoder(output, aEncoder, aStream, outAStream, AUDIO_STREAM_INDEX); flushEncoder(output, aEncoder, aStream, outAStream, AUDIO_STREAM_INDEX);
output.writeTrailer(); output.writeTrailer();
return 0; return 0;