Skip to content

Commit d34ab32

Browse files
change video parameters structure
1 parent 91851c2 commit d34ab32

File tree

10 files changed

+186
-107
lines changed

10 files changed

+186
-107
lines changed

app/avTranscoder/avTranscoder.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <AvTranscoder/ColorTransform.hpp>
1313

14+
#include <AvTranscoder/DatasStructures/VideoStream.hpp>
1415
#include <AvTranscoder/DatasStructures/Image.hpp>
1516

1617
void displayMetadatas( const char* filename )
@@ -90,10 +91,6 @@ void transcodeVideo( const char* inputfilename, const char* outputFilename )
9091
exit( -1 );
9192
}
9293

93-
std::cout << "Input Video Stream Properties " << std::endl;
94-
std::cout << "size " << inputStreamVideo.getWidth() << "x" << inputStreamVideo.getHeight() << std::endl;
95-
std::cout << "components " << inputStreamVideo.getComponents() << std::endl;
96-
std::cout << "bit depth " << inputStreamVideo.getBitDepth() << std::endl;
9794
//dVideo.set( key, value );
9895

9996
// same as
@@ -106,17 +103,28 @@ void transcodeVideo( const char* inputfilename, const char* outputFilename )
106103
// init video & audio encoders
107104
OutputStreamVideo outputStreamVideo;
108105

109-
outputStreamVideo.setWidth( inputStreamVideo.getWidth() );
110-
outputStreamVideo.setHeight( inputStreamVideo.getHeight() );
111-
outputStreamVideo.setComponents( inputStreamVideo.getComponents() );
112-
outputStreamVideo.setBitDepth( inputStreamVideo.getBitDepth() );
113-
//eVideo.set( "mv_method", "me_hex" );
106+
VideoStream& videoStream = outputStreamVideo.getVideoDesc();
114107

115-
VideoStream videoStream;
108+
Pixel oPixel;
116109

117-
videoStream.setCodecFromName( "dnxhd" );
110+
oPixel.setSubsampling( eSubsampling422 );
111+
oPixel.setBitsPerPixel( 16 );
112+
//std::cout << oPixel.findPixel() << std::endl;
118113

119-
if( !outputStreamVideo.setup( videoStream ) )
114+
Image image;
115+
image.setWidth ( inputStreamVideo.getWidth() );
116+
image.setHeight( inputStreamVideo.getHeight() );
117+
image.setPixel( oPixel );
118+
119+
videoStream.setVideoCodec( "dnxhd" );
120+
videoStream.setBitrate( 120000000 );
121+
videoStream.setTimeBase( 1, 25 ); // 25 fps
122+
123+
videoStream.setParametersFromImage( image );
124+
125+
//videoStream.initCodecContext();
126+
127+
if( !outputStreamVideo.setup( ) )
120128
{
121129
std::cout << "error during initialising video output stream" << std::endl;
122130
exit( -1 );
@@ -171,6 +179,8 @@ void transcodeVideo( const char* inputfilename, const char* outputFilename )
171179

172180
Image codedImage;
173181

182+
std::cout << "start transcoding" << std::endl;
183+
174184
for( size_t count = 0; count < 10; ++count )
175185
{
176186
inputStreamVideo.readNextFrame( frameBuffer );

src/AvTranscoder/ColorTransform.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ void ColorTransform::convert( const Image& src, Image& dst )
5353
{
5454

5555
SwsContext* m_imageConvertContext = sws_getContext(
56-
m_width, m_height, m_inputPixel.get(),
57-
m_width, m_height, m_outputPixel.get(),
56+
m_width, m_height, m_inputPixel.findPixel(),
57+
m_width, m_height, m_outputPixel.findPixel(),
5858
SWS_POINT, NULL, NULL, NULL);
5959

6060
// resize dst buffer ? using :
@@ -67,8 +67,8 @@ void ColorTransform::convert( const Image& src, Image& dst )
6767

6868
//dataIn[0] = &src.getPtr();
6969
//dataOut[0] = &dst[0];
70-
lineSizeIn [0] = av_image_get_linesize( m_inputPixel.get(), src.getWidth(), 3 );
71-
lineSizeOut[0] = av_image_get_linesize( m_outputPixel.get(), src.getWidth(), 3 );
70+
lineSizeIn [0] = av_image_get_linesize( m_inputPixel.findPixel(), src.getWidth(), 3 );
71+
lineSizeOut[0] = av_image_get_linesize( m_outputPixel.findPixel(), src.getWidth(), 3 );
7272

7373
// sws_scale( m_imageConvertContext,
7474
// dataIn, lineSizeIn, 0, src.getHeight(),

src/AvTranscoder/DatasStructures/Image.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class Image
4242
DataBuffer& getBuffer() { return m_dataBuffer; }
4343

4444
unsigned char* getPtr() { return &m_dataBuffer[0]; }
45-
45+
4646
#ifndef SWIG
4747
const unsigned char* getPtr() const { return &m_dataBuffer[0]; }
4848
#endif
@@ -52,7 +52,9 @@ class Image
5252
size_t getWidth () const { return m_width; }
5353
size_t getHeight() const { return m_height; }
5454
// Ratio getDar() const { return m_displayAspectRatio; }
55-
Pixel getPixel() const { return m_pixel; }
55+
Pixel getPixelDesc() const { return m_pixel; }
56+
57+
//AVFrame getEmptyFrame() const;
5658

5759
private:
5860
DataBuffer m_dataBuffer;

src/AvTranscoder/DatasStructures/Pixel.cpp

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,43 @@ extern "C" {
44
#ifndef __STDC_CONSTANT_MACROS
55
#define __STDC_CONSTANT_MACROS
66
#endif
7+
#include <libavformat/avformat.h>
78
#include <libavutil/pixfmt.h>
89
#include <libswscale/swscale.h>
910
#include <libavutil/imgutils.h>
1011
#include <libavutil/pixdesc.h>
1112
}
1213

14+
#include <iostream>
15+
1316
namespace avtranscoder
1417
{
1518

19+
AVPixelFormat Pixel::findPixel()
20+
{
21+
//av_register_all();
22+
const AVPixFmtDescriptor *pix_desc = NULL;
23+
while( ( pix_desc = av_pix_fmt_desc_next( pix_desc ) ) )
24+
{
25+
if( m_components == (size_t) pix_desc->nb_components &&
26+
m_pixelSize == (size_t) av_get_bits_per_pixel( pix_desc ) &&
27+
m_endianess == ( pix_desc->flags & PIX_FMT_BE ) &&
28+
m_withAlpha == ( pix_desc->flags & PIX_FMT_ALPHA ) &&
29+
m_planar == ( ( pix_desc->flags & PIX_FMT_PLANAR ) != 0 ) &&
30+
asCorrectColorComponents( pix_desc, m_componentType ) &&
31+
asCorrectSubsampling( pix_desc, m_subsamplingType ) )
32+
{
33+
return av_pix_fmt_desc_get_id( pix_desc );
34+
}
35+
}
36+
return AV_PIX_FMT_NONE;
37+
}
38+
1639
bool Pixel::asCorrectColorComponents( const AVPixFmtDescriptor* pix_desc, const EComponentType componentType )
1740
{
1841
if( componentType == eComponentRgb && pix_desc->flags & PIX_FMT_RGB )
1942
return true;
20-
if( ( componentType != eComponentRgb ) && ! ( pix_desc->flags & PIX_FMT_RGB ) )
43+
if( ( componentType != eComponentRgb ) && ( ! ( pix_desc->flags & PIX_FMT_RGB ) ) )
2144
return true;
2245
return false;
2346
}
@@ -27,37 +50,22 @@ bool Pixel::asCorrectSubsampling( const AVPixFmtDescriptor* pix_desc, const ESub
2750
switch( subsamplingType )
2851
{
2952
case eSubsamplingNone :
53+
{
3054
return ( pix_desc->log2_chroma_w == false ) &&
3155
( pix_desc->log2_chroma_h == false );
56+
}
3257
case eSubsampling422 :
58+
{
3359
return ( pix_desc->log2_chroma_w == true ) &&
3460
( pix_desc->log2_chroma_h == false );
61+
}
3562
case eSubsampling420 :
63+
{
3664
return ( pix_desc->log2_chroma_w == true ) &&
3765
( pix_desc->log2_chroma_h == true );
38-
}
39-
return false;
40-
}
41-
42-
void Pixel::findMatchingPixel()
43-
{
44-
const AVPixFmtDescriptor *pix_desc = NULL;
45-
while( ( pix_desc = av_pix_fmt_desc_next( pix_desc ) ) )
46-
{
47-
//std::cout << pix_desc->name << std::endl;
48-
// enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id( pix_desc );
49-
if( m_components == (int) pix_desc->nb_components &&
50-
m_bitDepth == (size_t) av_get_bits_per_pixel( pix_desc ) &&
51-
m_endianess == ( pix_desc->flags & PIX_FMT_BE ) &&
52-
m_outWithAlpha == ( pix_desc->flags & PIX_FMT_ALPHA ) &&
53-
asCorrectColorComponents( pix_desc, m_componentType ) &&
54-
asCorrectSubsampling( pix_desc, m_subsamplingType ) )
55-
{
56-
//std::cout << "select : " << pix_desc->name << std::endl;
57-
m_pixel = av_pix_fmt_desc_get_id( pix_desc );
58-
return;
5966
}
6067
}
68+
return false;
6169
}
6270

6371
}

src/AvTranscoder/DatasStructures/Pixel.hpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,39 +33,37 @@ class Pixel
3333
{
3434
public:
3535
Pixel()
36-
: m_bitDepth ( 8 )
36+
: m_pixelSize ( 24 )
3737
, m_components ( 3 )
3838
, m_componentType ( eComponentYuv )
3939
, m_subsamplingType ( eSubsamplingNone )
40-
, m_endianess ( true )
41-
, m_outWithAlpha ( false )
40+
, m_endianess ( false )
41+
, m_withAlpha ( false )
42+
, m_planar ( true )
4243
{ }
4344

44-
void setBitsPerPixel ( const size_t bitDepth ) { m_bitDepth = bitDepth; }
45+
void setBitsPerPixel ( const size_t pixelSize ) { m_pixelSize = pixelSize; }
4546
void setBigEndian ( const bool endianess ) { m_endianess = endianess; }
4647
void setComponents ( const size_t components ) { m_components = components; }
4748
void setColorComponents( const EComponentType componentType ) { m_componentType = componentType; }
4849
void setSubsampling ( const ESubsamplingType subsamplingType = eSubsamplingNone ) { m_subsamplingType = subsamplingType; }
49-
void setAlpha ( const bool outWithAlpha = true ) { m_outWithAlpha = outWithAlpha; }
50+
void setAlpha ( const bool withAlpha = true ) { m_withAlpha = withAlpha; }
51+
void setPlanar ( const bool isPlanar ) { m_planar = isPlanar; }
5052

51-
bool asCorrectColorComponents( const AVPixFmtDescriptor* pix_desc, const EComponentType componentType );
52-
53-
bool asCorrectSubsampling( const AVPixFmtDescriptor* pix_desc, const ESubsamplingType subsamplingType );
54-
55-
void findMatchingPixel();
56-
57-
AVPixelFormat get(){ return m_pixel; }
53+
AVPixelFormat findPixel();
5854

5955
private:
60-
AVPixelFormat m_pixel;
56+
bool asCorrectColorComponents( const AVPixFmtDescriptor* pix_desc, const EComponentType componentType );
57+
bool asCorrectSubsampling( const AVPixFmtDescriptor* pix_desc, const ESubsamplingType subsamplingType );
6158

62-
size_t m_bitDepth;
59+
size_t m_pixelSize;
6360
size_t m_components;
6461
double m_gamma;
6562
EComponentType m_componentType;
6663
ESubsamplingType m_subsamplingType;
6764
bool m_endianess;
68-
bool m_outWithAlpha;
65+
bool m_withAlpha;
66+
bool m_planar;
6967

7068
//AVChromaLocation
7169
};
Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,83 @@
11
#include "VideoStream.hpp"
22

3+
extern "C" {
4+
#ifndef __STDC_CONSTANT_MACROS
5+
#define __STDC_CONSTANT_MACROS
6+
#endif
7+
#include <libavcodec/avcodec.h>
8+
#include <libavformat/avformat.h>
9+
#include <libavutil/avutil.h>
10+
#include <libavutil/pixdesc.h>
11+
#include <libavutil/imgutils.h>
12+
#include <libavutil/mathematics.h>
13+
}
14+
#include <iostream>
15+
316
namespace avtranscoder
417
{
518

6-
void VideoStream::setCodecFromName( const std::string& codecName )
19+
VideoStream::VideoStream( const std::string& codecName )
20+
: m_codec( NULL )
21+
, m_codecContext( NULL )
722
{
8-
AVCodec* codec = avcodec_find_encoder_by_name( codecName.c_str() );
9-
m_codecId = codec->id;
23+
if( codecName.size() )
24+
setVideoCodec( codecName );
25+
}
26+
27+
VideoStream::VideoStream( const AVCodecID codecId )
28+
: m_codec( NULL )
29+
, m_codecContext( NULL )
30+
{
31+
setVideoCodec( codecId );
32+
}
33+
34+
void VideoStream::setVideoCodec( const std::string& codecName )
35+
{
36+
avcodec_register_all(); // Warning: should be called only once
37+
m_codec = avcodec_find_encoder_by_name( codecName.c_str() );
38+
initCodecContext();
39+
}
40+
41+
void VideoStream::setVideoCodec( const AVCodecID codecId )
42+
{
43+
avcodec_register_all(); // Warning: should be called only once
44+
m_codec = avcodec_find_encoder( codecId );
45+
initCodecContext();
46+
}
47+
48+
void VideoStream::setParametersFromImage( const Image& image )
49+
{
50+
m_codecContext->width = image.getWidth();
51+
m_codecContext->height = image.getHeight();
52+
m_codecContext->pix_fmt = image.getPixelDesc().findPixel();
53+
std::cout << image.getPixelDesc().findPixel() << std::endl;
54+
}
55+
56+
void VideoStream::setBitrate( const size_t bitRate )
57+
{
58+
m_codecContext->bit_rate = bitRate;
59+
}
60+
61+
void VideoStream::setTimeBase( const size_t num, const size_t den )
62+
{
63+
m_codecContext->time_base.num = num;
64+
m_codecContext->time_base.den = den;
65+
}
66+
67+
void VideoStream::initCodecContext( )
68+
{
69+
if( m_codec == NULL )
70+
return;
71+
72+
if( ( m_codecContext = avcodec_alloc_context3( m_codec ) ) == NULL )
73+
return;
74+
75+
// Set default codec parameters
76+
if( avcodec_get_context_defaults3( m_codecContext, m_codec ) != 0 )
77+
{
78+
m_codecContext = NULL;
79+
return;
80+
}
1081
}
1182

1283
}

src/AvTranscoder/DatasStructures/VideoStream.hpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,26 @@ namespace avtranscoder
2121
class VideoStream
2222
{
2323
public:
24-
VideoStream(){};
24+
VideoStream( const std::string& codecName = "" );
25+
VideoStream( const AVCodecID codecId );
2526

26-
void setCodecFromName( const std::string& codecName );
27-
28-
void setCodecFromID( const AVCodecID codecId ) { m_codecId = codecId; }
27+
void setVideoCodec( const std::string& codecName );
28+
void setVideoCodec( const AVCodecID codecId );
2929

3030
void setParametersFromImage( const Image& image );
3131

32-
void setBitrate( const size_t bitRate ) { m_bitRate = bitRate; }
32+
void setBitrate( const size_t bitRate );
33+
void setTimeBase( const size_t num, const size_t den );
3334

34-
AVCodecID getCodecId() const { return m_codecId; }
35+
#ifndef SWIG
36+
AVCodec* getCodec() const { return m_codec; }
37+
AVCodecContext* getCodecContext() const { return m_codecContext; }
38+
#endif
3539

3640
private:
37-
AVCodecID m_codecId;
38-
size_t m_bitRate;
41+
void initCodecContext( );
42+
AVCodec* m_codec;
43+
AVCodecContext* m_codecContext;
3944
};
4045

4146
}

src/AvTranscoder/InputStreamVideo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ bool InputStreamVideo::setup( const std::string& filename, const size_t streamIn
3030
{
3131
av_register_all();
3232
//av_log_set_level( AV_LOG_FATAL );
33+
av_log_set_level( AV_LOG_DEBUG );
3334

3435
if( avformat_open_input( &formatContext, filename.c_str(), NULL, NULL ) < 0 )
3536
{

0 commit comments

Comments
 (0)