Skip to content

Fix videoTransform #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Dec 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/avTranscoder/avTranscoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void transcodeVideo( const char* inputfilename, const char* outputFilename )
InputFile input( inputfilename );
input.analyse( p );

input.readStream( input.getProperties().getVideoProperties().at( 0 ).getStreamId() );
input.activateStream( input.getProperties().getVideoProperties().at( 0 ).getStreamId() );

// init video decoders
AvInputVideo inputVideo( input.getStream( 0 ) );
Expand Down
2 changes: 1 addition & 1 deletion app/avplay/AvReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class AvReader : public Reader
_inputFile.analyse( p );
_videoStream = _inputFile.getProperties().getVideoProperties().at(0).getStreamId();

_inputFile.readStream( _videoStream );
_inputFile.activateStream( _videoStream );

_inputVideo = new avtranscoder::AvInputVideo( _inputFile.getStream( _videoStream ) );

Expand Down
11 changes: 1 addition & 10 deletions src/AvTranscoder/avTranscoder.i
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@
%{
#include <AvTranscoder/ProfileLoader.hpp>

#include <AvTranscoder/frame/Pixel.hpp>
#include <AvTranscoder/frame/Frame.hpp>
#include <AvTranscoder/frame/VideoFrame.hpp>
#include <AvTranscoder/frame/AudioFrame.hpp>

#include <AvTranscoder/codec/ICodec.hpp>
#include <AvTranscoder/codec/VideoCodec.hpp>
#include <AvTranscoder/codec/AudioCodec.hpp>
Expand Down Expand Up @@ -54,14 +49,10 @@ namespace std {

%include "AvTranscoder/progress/progress.i"
%include "AvTranscoder/mediaProperty/mediaProperty.i"
%include "AvTranscoder/frame/frame.i"

%include <AvTranscoder/ProfileLoader.hpp>

%include <AvTranscoder/frame/Pixel.hpp>
%include <AvTranscoder/frame/Frame.hpp>
%include <AvTranscoder/frame/VideoFrame.hpp>
%include <AvTranscoder/frame/AudioFrame.hpp>

%include <AvTranscoder/codec/ICodec.hpp>
%include <AvTranscoder/codec/VideoCodec.hpp>
%include <AvTranscoder/codec/AudioCodec.hpp>
Expand Down
16 changes: 8 additions & 8 deletions src/AvTranscoder/codedStream/AvInputStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ AvInputStream::AvInputStream( InputFile& inputFile, const size_t streamIndex )
: IInputStream( )
, _inputFile( &inputFile )
, _codec( NULL )
, _streamCache()
, _streamIndex( streamIndex )
, _bufferized( false )
, _isActivated( false )
{
AVCodecContext* context = _inputFile->getFormatContext().streams[_streamIndex]->codec;

Expand Down Expand Up @@ -57,8 +58,9 @@ AvInputStream::AvInputStream( const AvInputStream& inputStream )
: IInputStream( )
, _inputFile( inputStream._inputFile )
, _codec( inputStream._codec )
, _streamCache()
, _streamIndex( inputStream._streamIndex )
, _bufferized( inputStream._bufferized )
, _isActivated( inputStream._isActivated )
{
}

Expand All @@ -69,8 +71,8 @@ AvInputStream::~AvInputStream( )

bool AvInputStream::readNextPacket( CodedData& data )
{
if( ! _bufferized )
throw std::runtime_error( "Can't read packet on non-bufferized input stream." );
if( ! _isActivated )
throw std::runtime_error( "Can't read packet on non-activated input stream." );

// if packet is already cached
if( ! _streamCache.empty() )
Expand All @@ -90,14 +92,12 @@ bool AvInputStream::readNextPacket( CodedData& data )
void AvInputStream::addPacket( AVPacket& packet )
{
// Do not cache data if the stream is declared as unused in process
if( ! _bufferized )
if( ! _isActivated )
return;

CodedData data;
_streamCache.push( data );
_streamCache.back().getBuffer().resize( packet.size );
if( packet.size != 0 )
memcpy( _streamCache.back().getPtr(), packet.data, packet.size );
_streamCache.back().copyData( packet.data, packet.size );
}

VideoCodec& AvInputStream::getVideoCodec()
Expand Down
8 changes: 4 additions & 4 deletions src/AvTranscoder/codedStream/AvInputStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class AvExport AvInputStream : public IInputStream

void addPacket( AVPacket& packet );

void setBufferred( const bool bufferized ){ _bufferized = bufferized; };
bool getBufferred() const { return _bufferized; };
void activate( const bool activate = true ){ _isActivated = activate; };
bool isActivated() const { return _isActivated; };

void clearBuffering();

Expand All @@ -47,10 +47,10 @@ class AvExport AvInputStream : public IInputStream
InputFile* _inputFile; ///< Has link (no ownership)
ICodec* _codec; ///< Has ownership

std::queue<CodedData> _streamCache;
std::queue<CodedData> _streamCache; ///< Cache of packet data already read and corresponding to this stream

size_t _streamIndex; ///< Index of the stream in the input file
bool _bufferized; ///< If the stream is bufferized
bool _isActivated; ///< If the stream is activated, data read from it will be buffered
};

}
Expand Down
3 changes: 2 additions & 1 deletion src/AvTranscoder/codedStream/IInputStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class IInputStream

virtual double getDuration() const = 0;

virtual void setBufferred( const bool bufferized ) = 0;
virtual void activate( const bool activate = true ) = 0;
virtual bool isActivated() const = 0;

virtual void clearBuffering() = 0;

Expand Down
10 changes: 2 additions & 8 deletions src/AvTranscoder/essenceStream/AvInputAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,7 @@ bool AvInputAudio::readNextFrame( Frame& frameBuffer )

AVCodecContext* avCodecContext = _codec->getAVCodecContext();

size_t decodedSize = av_samples_get_buffer_size(
NULL, avCodecContext->channels,
_frame->nb_samples,
avCodecContext->sample_fmt, 1 );
size_t decodedSize = av_samples_get_buffer_size( NULL, avCodecContext->channels, _frame->nb_samples, avCodecContext->sample_fmt, 1 );

AudioFrame& audioBuffer = static_cast<AudioFrame&>( frameBuffer );

Expand All @@ -105,10 +102,7 @@ bool AvInputAudio::readNextFrame( Frame& frameBuffer )
unsigned char* const src = _frame->data[0];
unsigned char* dst = audioBuffer.getPtr();

av_samples_copy(
&dst, &src, 0,
0, _frame->nb_samples, avCodecContext->channels,
avCodecContext->sample_fmt );
av_samples_copy( &dst, &src, 0, 0, _frame->nb_samples, avCodecContext->channels, avCodecContext->sample_fmt );
}

return true;
Expand Down
12 changes: 4 additions & 8 deletions src/AvTranscoder/essenceStream/AvOutputAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,13 @@ bool AvOutputAudio::encodeFrame( const Frame& sourceFrame, Frame& codedFrame )
int ret = avcodec_encode_audio2( codecContext, &packet, frame, &gotPacket );
if( ret == 0 && gotPacket == 1 )
{
codedFrame.getBuffer().resize( packet.size );
memcpy( codedFrame.getPtr(), packet.data, packet.size );
codedFrame.copyData( packet.data, packet.size );
}
#else
int ret = avcodec_encode_audio( codecContext, packet.data, packet.size, frame );
if( ret > 0 )
{
codedFrame.getBuffer().resize( packet.size );
memcpy( codedFrame.getPtr(), packet.data, packet.size );
codedFrame.copyData( packet.data, packet.size );
}
#endif

Expand Down Expand Up @@ -150,8 +148,7 @@ bool AvOutputAudio::encodeFrame( Frame& codedFrame )
int ret = avcodec_encode_audio2( codecContext, &packet, NULL, &gotPacket );
if( ret == 0 && gotPacket == 1 )
{
codedFrame.getBuffer().resize( packet.size );
memcpy( codedFrame.getPtr(), packet.data, packet.size );
codedFrame.copyData( packet.data, packet.size );
}
av_free_packet( &packet );
return ret == 0 && gotPacket == 1;
Expand All @@ -160,8 +157,7 @@ bool AvOutputAudio::encodeFrame( Frame& codedFrame )
int ret = avcodec_encode_audio( codecContext, packet.data, packet.size, NULL );
if( ret > 0 )
{
codedFrame.getBuffer().resize( packet.size );
memcpy( codedFrame.getPtr(), packet.data, packet.size );
codedFrame.copyData( packet.data, packet.size );
}
av_free_packet( &packet );
return ret == 0;
Expand Down
12 changes: 4 additions & 8 deletions src/AvTranscoder/essenceStream/AvOutputVideo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,13 @@ bool AvOutputVideo::encodeFrame( const Frame& sourceFrame, Frame& codedFrame )
int ret = avcodec_encode_video2( codecContext, &packet, frame, &gotPacket );
if( ret == 0 && gotPacket == 1 )
{
codedFrame.getBuffer().resize( packet.size );
memcpy( codedFrame.getPtr(), packet.data, packet.size );
codedFrame.copyData( packet.data, packet.size );
}
#else
int ret = avcodec_encode_video( codecContext, packet.data, packet.size, frame );
if( ret > 0 )
{
codedFrame.getBuffer().resize( packet.size );
memcpy( codedFrame.getPtr(), packet.data, packet.size );
codedFrame.copyData( packet.data, packet.size );
}
#endif

Expand Down Expand Up @@ -135,8 +133,7 @@ bool AvOutputVideo::encodeFrame( Frame& codedFrame )
int ret = avcodec_encode_video2( codecContext, &packet, NULL, &gotPacket );
if( ret == 0 && gotPacket == 1 )
{
codedFrame.getBuffer().resize( packet.size );
memcpy( codedFrame.getPtr(), packet.data, packet.size );
codedFrame.copyData( packet.data, packet.size );
}
av_free_packet( &packet );
return ret == 0 && gotPacket == 1;
Expand All @@ -145,8 +142,7 @@ bool AvOutputVideo::encodeFrame( Frame& codedFrame )
int ret = avcodec_encode_video( codecContext, packet.data, packet.size, NULL );
if( ret > 0 )
{
codedFrame.getBuffer().resize( packet.size );
memcpy( codedFrame.getPtr(), packet.data, packet.size );
codedFrame.copyData( packet.data, packet.size );
}
av_free_packet( &packet );
return ret == 0;
Expand Down
13 changes: 6 additions & 7 deletions src/AvTranscoder/essenceStream/GeneratorAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ void GeneratorAudio::setFrame( Frame& inputFrame )

bool GeneratorAudio::readNextFrame( Frame& frameBuffer )
{
frameBuffer.getBuffer().resize( _frameDesc.getDataSize() );

if( ! _inputFrame )
{
AudioFrame& audioFrameBuffer = static_cast<AudioFrame&>( frameBuffer );
Expand All @@ -36,13 +34,14 @@ bool GeneratorAudio::readNextFrame( Frame& frameBuffer )
_frameDesc.getAVSampleFormat() == AV_SAMPLE_FMT_U8P
) ? 0x80 : 0x00;

if( frameBuffer.getSize() != _frameDesc.getDataSize() )
frameBuffer.getBuffer().resize( _frameDesc.getDataSize() );
memset( frameBuffer.getPtr(), fill_char, frameBuffer.getSize() );
return true;
}

if( frameBuffer.getSize() != _inputFrame->getSize() )
frameBuffer.getBuffer().resize( _inputFrame->getSize() );
std::memcpy( frameBuffer.getPtr(), _inputFrame->getPtr(), _inputFrame->getSize() );
else
{
frameBuffer.copyData( _inputFrame->getPtr(), _inputFrame->getSize() );
}
return true;
}

Expand Down
15 changes: 5 additions & 10 deletions src/AvTranscoder/essenceStream/GeneratorVideo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ bool GeneratorVideo::readNextFrame( Frame& frameBuffer )
if( ! _inputFrame )
{
// @todo support PAL (0 to 255) and NTFS (16 to 235)
int fillChar = 0;

if( frameBuffer.getSize() != _frameDesc.getDataSize() )
frameBuffer.getBuffer().resize( _frameDesc.getDataSize() );
char fillChar = 0;

VideoFrameDesc desc( _frameDesc );
Pixel rgbPixel;
Expand All @@ -45,14 +42,12 @@ bool GeneratorVideo::readNextFrame( Frame& frameBuffer )

VideoTransform videoEssenceTransform;
videoEssenceTransform.convert( intermediateBuffer, frameBuffer );

return true;
}

// Take image from _inputFrame
if( frameBuffer.getSize() != _inputFrame->getSize() )
frameBuffer.getBuffer().resize( _inputFrame->getSize() );
std::memcpy( frameBuffer.getPtr(), _inputFrame->getPtr(), _inputFrame->getSize() );
else
{
frameBuffer.copyData( _inputFrame->getPtr(), _inputFrame->getSize() );
}
return true;
}

Expand Down
25 changes: 10 additions & 15 deletions src/AvTranscoder/file/InputFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,10 @@ AvInputStream& InputFile::getStream( size_t index )
bool InputFile::readNextPacket( CodedData& data, const size_t streamIndex )
{
AVPacket packet;
av_init_packet( &packet );
while( 1 )
bool nextPacketFound = false;
while( ! nextPacketFound )
{
av_init_packet( &packet );
int ret = av_read_frame( _formatContext, &packet );
if( ret < 0 ) // error or end of file
{
Expand All @@ -161,23 +162,17 @@ bool InputFile::readNextPacket( CodedData& data, const size_t streamIndex )
// copy and return the packet data
if( packet.stream_index == (int)streamIndex )
{
data.getBuffer().resize( packet.size );
if( packet.size != 0 )
memcpy( data.getPtr(), packet.data, packet.size );
av_free_packet( &packet );
return true;
data.copyData( packet.data, packet.size );
nextPacketFound = true;
}
// else add the packet data to the stream cache
else
{
_inputStreams.at( packet.stream_index )->addPacket( packet );
}

// do not delete these 2 lines
// need to skip packet, delete this one and re-init for reading the next one
av_free_packet( &packet );
av_init_packet( &packet );
}
return true;
}

void InputFile::seekAtFrame( const size_t frame )
Expand All @@ -198,14 +193,14 @@ void InputFile::seekAtFrame( const size_t frame )
}
}

void InputFile::readStream( const size_t streamIndex, bool readStream )
void InputFile::activateStream( const size_t streamIndex, bool activate )
{
_inputStreams.at( streamIndex )->setBufferred( readStream );
_inputStreams.at( streamIndex )->activate( activate );
}

bool InputFile::getReadStream( const size_t streamIndex )
bool InputFile::isStreamActivated( const size_t streamIndex )
{
return _inputStreams.at( streamIndex )->getBufferred();
return _inputStreams.at( streamIndex )->isActivated();
}

void InputFile::setProfile( const ProfileLoader::Profile& profile )
Expand Down
Loading