Skip to content

Commit 5b8c88b

Browse files
committed
Merge pull request #78 from cchampet/clean_frame
Clean frame
2 parents b9b4853 + bf8f351 commit 5b8c88b

File tree

7 files changed

+155
-88
lines changed

7 files changed

+155
-88
lines changed

src/AvTranscoder/file/IOutputFile.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <AvTranscoder/ProfileLoader.hpp>
1212

1313
#include <string>
14-
#include <exception>
14+
#include <stdexcept>
1515

1616
namespace avtranscoder
1717
{

src/AvTranscoder/frame/AudioFrame.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "AudioFrame.hpp"
2+
3+
#include <stdexcept>
4+
#include <stdlib.h>
5+
6+
namespace avtranscoder
7+
{
8+
9+
AudioFrameDesc::AudioFrameDesc( const size_t sampleRate, const size_t channels, const AVSampleFormat sampleFormat )
10+
: _sampleRate( sampleRate )
11+
, _channels( channels )
12+
, _sampleFormat( sampleFormat )
13+
, _fps( 1. )
14+
{}
15+
16+
AudioFrameDesc::AudioFrameDesc( const size_t sampleRate, const size_t channels, const std::string& sampleFormat )
17+
: _sampleRate( sampleRate )
18+
, _channels( channels )
19+
, _sampleFormat( av_get_sample_fmt( sampleFormat.c_str() ) )
20+
, _fps( 1. )
21+
{}
22+
23+
std::string AudioFrameDesc::getSampleFormatName() const
24+
{
25+
const char* formatName = av_get_sample_fmt_name( _sampleFormat );
26+
return formatName ? std::string( formatName ) : "unknown sample format";
27+
}
28+
29+
size_t AudioFrameDesc::getDataSize() const
30+
{
31+
if( _sampleFormat == AV_SAMPLE_FMT_NONE )
32+
throw std::runtime_error( "incorrect sample format" );
33+
34+
size_t size = ( _sampleRate / _fps ) * _channels * av_get_bytes_per_sample( _sampleFormat );
35+
if( size == 0 )
36+
throw std::runtime_error( "unable to determine audio buffer size" );
37+
38+
return size;
39+
}
40+
41+
void AudioFrameDesc::setSampleFormat( const std::string& sampleFormatName )
42+
{
43+
_sampleFormat = av_get_sample_fmt( sampleFormatName.c_str() );
44+
}
45+
46+
void AudioFrameDesc::setParameters( const ProfileLoader::Profile& profile )
47+
{
48+
// sample rate
49+
if( profile.count( constants::avProfileSampleRate ) )
50+
setSampleRate( atoi( profile.find( constants::avProfileSampleRate )->second.c_str() ) );
51+
// channel
52+
if( profile.count( constants::avProfileChannel ) )
53+
setChannels( atoi( profile.find( constants::avProfileChannel )->second.c_str() ) );
54+
// sample format
55+
if( profile.count( constants::avProfileSampleFormat ) )
56+
setSampleFormat( profile.find( constants::avProfileSampleFormat )->second );
57+
// fps
58+
if( profile.count( constants::avProfileFrameRate ) )
59+
setFps( atof( profile.find( constants::avProfileFrameRate )->second.c_str() ) );
60+
}
61+
62+
}

src/AvTranscoder/frame/AudioFrame.hpp

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,62 +8,32 @@ extern "C" {
88
#include <libavutil/samplefmt.h>
99
}
1010

11-
#include <stdexcept>
12-
1311
namespace avtranscoder
1412
{
1513

1614
/// @brief Description of a number of samples, which corresponds to one video frame
1715
class AvExport AudioFrameDesc
1816
{
1917
public:
20-
AudioFrameDesc( const size_t sampleRate = 0, const size_t channels = 0, const AVSampleFormat sampleFormat = AV_SAMPLE_FMT_NONE )
21-
: _sampleRate( sampleRate )
22-
, _channels( channels )
23-
, _sampleFormat( sampleFormat )
24-
, _fps( 1. )
25-
{}
18+
AudioFrameDesc( const size_t sampleRate = 0, const size_t channels = 0, const AVSampleFormat sampleFormat = AV_SAMPLE_FMT_NONE );
2619

27-
AudioFrameDesc( const size_t sampleRate, const size_t channels, const std::string& sampleFormat )
28-
: _sampleRate( sampleRate )
29-
, _channels( channels )
30-
, _sampleFormat( av_get_sample_fmt( sampleFormat.c_str() ) )
31-
, _fps( 1. )
32-
{}
20+
AudioFrameDesc( const size_t sampleRate, const size_t channels, const std::string& sampleFormat );
3321

3422
size_t getSampleRate() const { return _sampleRate; }
3523
size_t getChannels() const { return _channels; }
3624
AVSampleFormat getSampleFormat() const { return _sampleFormat; }
37-
std::string getSampleFormatName() const
38-
{
39-
const char* formatName = av_get_sample_fmt_name( _sampleFormat );
40-
return formatName ? std::string( formatName ) : "unknown sample format";
41-
}
25+
std::string getSampleFormatName() const;
4226
double getFps() const { return _fps; }
4327

44-
size_t getDataSize() const
45-
{
46-
if( _sampleFormat == AV_SAMPLE_FMT_NONE )
47-
throw std::runtime_error( "incorrect sample format" );
48-
49-
size_t size = ( _sampleRate / _fps ) * _channels * av_get_bytes_per_sample( _sampleFormat );
50-
if( size == 0 )
51-
throw std::runtime_error( "unable to determine audio buffer size" );
52-
53-
return size;
54-
}
28+
size_t getDataSize() const;
5529

5630
void setSampleRate( const size_t sampleRate ) { _sampleRate = sampleRate; }
5731
void setChannels( const size_t channels ) { _channels = channels; }
58-
void setSampleFormat( const std::string& sampleFormatName ) { _sampleFormat = av_get_sample_fmt( sampleFormatName.c_str() ); }
32+
void setSampleFormat( const std::string& sampleFormatName );
5933
void setSampleFormat( const AVSampleFormat sampleFormat ) { _sampleFormat = sampleFormat; }
6034
void setFps( const double fps ) { _fps = fps; }
6135

62-
void setParameters( const ProfileLoader::Profile& profile )
63-
{
64-
if( profile.find( constants::avProfileSampleFormat ) != profile.end() )
65-
setSampleFormat( profile.find( constants::avProfileSampleFormat )->second );
66-
}
36+
void setParameters( const ProfileLoader::Profile& profile );
6737

6838
private:
6939
size_t _sampleRate;

src/AvTranscoder/frame/Frame.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ void Frame::refData( unsigned char* buffer, const size_t size )
3838
_packet.size = size;
3939
}
4040

41+
void Frame::copyData( unsigned char* buffer, const size_t size )
42+
{
43+
resize( size );
44+
if ( size != 0 )
45+
memcpy( _packet.data, buffer, _packet.size );
46+
}
47+
4148
void Frame::refData( Frame& frame )
4249
{
4350
_packet.data = frame.getData();

src/AvTranscoder/frame/Frame.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ class AvExport Frame
3434
void refData( unsigned char* buffer, const size_t size );
3535
///@}
3636

37+
/// Copy external data buffer
38+
void copyData( unsigned char* buffer, const size_t size );
39+
3740
/**
3841
* @brief Resize the buffer with the given size, and copy the given value
3942
* @note Use this function to check if we can modify the buffer

src/AvTranscoder/frame/VideoFrame.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "VideoFrame.hpp"
2+
3+
extern "C" {
4+
#include <libavcodec/avcodec.h>
5+
#include <libavutil/pixdesc.h>
6+
}
7+
8+
#include <stdexcept>
9+
#include <stdlib.h>
10+
11+
namespace avtranscoder
12+
{
13+
14+
VideoFrameDesc::VideoFrameDesc( const size_t width, const size_t height, const AVPixelFormat pixelFormat )
15+
: _width( width )
16+
, _height( height )
17+
, _pixelFormat( pixelFormat )
18+
, _fps( 1.0 )
19+
{
20+
}
21+
VideoFrameDesc::VideoFrameDesc( const size_t width, const size_t height, const std::string& pixelFormat )
22+
: _width( width )
23+
, _height( height )
24+
, _pixelFormat( av_get_pix_fmt( pixelFormat.c_str() ) )
25+
, _fps( 1.0 )
26+
{
27+
}
28+
29+
std::string VideoFrameDesc::getPixelFormatName() const
30+
{
31+
const char* formatName = av_get_pix_fmt_name( _pixelFormat );
32+
return formatName ? std::string( formatName ) : "unknown pixel format";
33+
}
34+
35+
size_t VideoFrameDesc::getDataSize() const
36+
{
37+
if( _pixelFormat == AV_PIX_FMT_NONE )
38+
throw std::runtime_error( "incorrect pixel format" );
39+
40+
size_t size = avpicture_get_size( _pixelFormat, _width, _height );
41+
if( size == 0 )
42+
throw std::runtime_error( "unable to determine image buffer size" );
43+
44+
return size;
45+
}
46+
47+
void VideoFrameDesc::setPixelFormat( const std::string& pixelFormat )
48+
{
49+
_pixelFormat = av_get_pix_fmt( pixelFormat.c_str() );
50+
}
51+
52+
void VideoFrameDesc::setParameters( const ProfileLoader::Profile& profile )
53+
{
54+
// width
55+
if( profile.count( constants::avProfileWidth ) )
56+
setWidth( atoi( profile.find( constants::avProfileWidth )->second.c_str() ) );
57+
// height
58+
if( profile.count( constants::avProfileHeight ) )
59+
setHeight( atoi( profile.find( constants::avProfileHeight )->second.c_str() ) );
60+
// pixel format
61+
if( profile.count( constants::avProfilePixelFormat ) )
62+
setPixelFormat( profile.find( constants::avProfilePixelFormat )->second );
63+
// fps
64+
if( profile.count( constants::avProfileFrameRate ) )
65+
setFps( atof( profile.find( constants::avProfileFrameRate )->second.c_str() ) );
66+
}
67+
68+
}
69+
70+

src/AvTranscoder/frame/VideoFrame.hpp

Lines changed: 6 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,78 +5,33 @@
55
#include <AvTranscoder/ProfileLoader.hpp>
66

77
extern "C" {
8-
#include <libavcodec/avcodec.h>
98
#include <libavutil/pixfmt.h>
10-
#include <libavutil/pixdesc.h>
119
}
1210

13-
#include <stdexcept>
14-
#include <stdlib.h>
15-
1611
namespace avtranscoder
1712
{
1813

1914
class AvExport VideoFrameDesc
2015
{
2116
public:
22-
VideoFrameDesc( const size_t width = 0, const size_t height = 0, const AVPixelFormat pixelFormat = AV_PIX_FMT_NONE )
23-
: _width( width )
24-
, _height( height )
25-
, _pixelFormat( pixelFormat )
26-
, _fps( 1.0 )
27-
{
28-
}
29-
VideoFrameDesc( const size_t width, const size_t height, const std::string& pixelFormat )
30-
: _width( width )
31-
, _height( height )
32-
, _pixelFormat( av_get_pix_fmt( pixelFormat.c_str() ) )
33-
, _fps( 1.0 )
34-
{
35-
}
17+
VideoFrameDesc( const size_t width = 0, const size_t height = 0, const AVPixelFormat pixelFormat = AV_PIX_FMT_NONE );
18+
VideoFrameDesc( const size_t width, const size_t height, const std::string& pixelFormat );
3619

3720
size_t getWidth () const { return _width; }
3821
size_t getHeight() const { return _height; }
3922
AVPixelFormat getPixelFormat() const { return _pixelFormat; }
40-
std::string getPixelFormatName() const
41-
{
42-
const char* formatName = av_get_pix_fmt_name( _pixelFormat );
43-
return formatName ? std::string( formatName ) : "unknown pixel format";
44-
}
23+
std::string getPixelFormatName() const;
4524
double getFps() const { return _fps; }
4625

47-
size_t getDataSize() const
48-
{
49-
if( _pixelFormat == AV_PIX_FMT_NONE )
50-
throw std::runtime_error( "incorrect pixel format" );
51-
52-
size_t size = avpicture_get_size( _pixelFormat, _width, _height );
53-
if( size == 0 )
54-
throw std::runtime_error( "unable to determine image buffer size" );
55-
56-
return size;
57-
}
26+
size_t getDataSize() const;
5827

5928
void setWidth ( const size_t width ) { _width = width; }
6029
void setHeight( const size_t height ) { _height = height; }
61-
void setPixelFormat( const std::string& pixelFormat ) { _pixelFormat = av_get_pix_fmt( pixelFormat.c_str() ); }
30+
void setPixelFormat( const std::string& pixelFormat );
6231
void setPixelFormat( const AVPixelFormat pixelFormat ) { _pixelFormat = pixelFormat; }
6332
void setFps( const double fps ) { _fps = fps; }
6433

65-
void setParameters( const ProfileLoader::Profile& profile )
66-
{
67-
// width
68-
if( profile.count( constants::avProfileWidth ) )
69-
setWidth( atoi( profile.find( constants::avProfileWidth )->second.c_str() ) );
70-
// height
71-
if( profile.count( constants::avProfileHeight ) )
72-
setHeight( atoi( profile.find( constants::avProfileHeight )->second.c_str() ) );
73-
// pixel format
74-
if( profile.count( constants::avProfilePixelFormat ) )
75-
setPixelFormat( profile.find( constants::avProfilePixelFormat )->second );
76-
// fps
77-
if( profile.count( constants::avProfileFrameRate ) )
78-
setFps( atof( profile.find( constants::avProfileFrameRate )->second.c_str() ) );
79-
}
34+
void setParameters( const ProfileLoader::Profile& profile );
8035

8136
private:
8237
size_t _width;

0 commit comments

Comments
 (0)