Skip to content

Commit f36bc37

Browse files
using first version of Datas Structures
1 parent 89bce24 commit f36bc37

File tree

11 files changed

+50
-97
lines changed

11 files changed

+50
-97
lines changed

src/AvTranscoder/DatasStructures/Image.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,14 @@ class Image
4040
void setPixel ( const Pixel& pixel ) { m_pixel = pixel; }
4141

4242
DataBuffer& getBuffer() { return m_dataBuffer; }
43-
unsigned char* getPtr() { return &m_dataBuffer[0]; }
43+
44+
unsigned char* getPtr() { return &m_dataBuffer[0]; }
45+
46+
#ifndef SWIG
4447
const unsigned char* getPtr() const { return &m_dataBuffer[0]; }
48+
#endif
49+
50+
size_t getSize() const { return m_dataBuffer.size(); }
4551

4652
size_t getWidth () const { return m_width; }
4753
size_t getHeight() const { return m_height; }

src/AvTranscoder/DatasStructures/Pixel.hpp

Lines changed: 9 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ extern "C" {
88
#define __STDC_CONSTANT_MACROS
99
#endif
1010
#include <libavutil/pixfmt.h>
11-
#include <libswscale/swscale.h>
12-
#include <libavutil/imgutils.h>
13-
#include <libavutil/pixdesc.h>
1411
}
1512

13+
class AVPixFmtDescriptor;
14+
1615
namespace avtranscoder
1716
{
1817

@@ -49,54 +48,13 @@ class Pixel
4948
void setSubsampling ( const ESubsamplingType subsamplingType = eSubsamplingNone ) { m_subsamplingType = subsamplingType; }
5049
void setAlpha ( const bool outWithAlpha = true ) { m_outWithAlpha = outWithAlpha; }
5150

52-
bool asCorrectColorComponents( const AVPixFmtDescriptor* pix_desc, const EComponentType componentType )
53-
{
54-
if( componentType == eComponentRgb && pix_desc->flags & PIX_FMT_RGB )
55-
return true;
56-
if( ( componentType != eComponentRgb ) && ! ( pix_desc->flags & PIX_FMT_RGB ) )
57-
return true;
58-
return false;
59-
}
60-
61-
bool asCorrectSubsampling( const AVPixFmtDescriptor* pix_desc, const ESubsamplingType subsamplingType )
62-
{
63-
switch( subsamplingType )
64-
{
65-
case eSubsamplingNone :
66-
return ( pix_desc->log2_chroma_w == false ) &&
67-
( pix_desc->log2_chroma_h == false );
68-
case eSubsampling422 :
69-
return ( pix_desc->log2_chroma_w == true ) &&
70-
( pix_desc->log2_chroma_h == false );
71-
case eSubsampling420 :
72-
return ( pix_desc->log2_chroma_w == true ) &&
73-
( pix_desc->log2_chroma_h == true );
74-
}
75-
return false;
76-
}
77-
78-
void findMatchingPixel()
79-
{
80-
const AVPixFmtDescriptor *pix_desc = NULL;
81-
while( ( pix_desc = av_pix_fmt_desc_next( pix_desc ) ) )
82-
{
83-
//std::cout << pix_desc->name << std::endl;
84-
// enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id( pix_desc );
85-
if( m_components == (int) pix_desc->nb_components &&
86-
m_bitDepth == (size_t) av_get_bits_per_pixel( pix_desc ) &&
87-
m_endianess == ( pix_desc->flags & PIX_FMT_BE ) &&
88-
m_outWithAlpha == ( pix_desc->flags & PIX_FMT_ALPHA ) &&
89-
asCorrectColorComponents( pix_desc, m_componentType ) &&
90-
asCorrectSubsampling( pix_desc, m_subsamplingType ) )
91-
{
92-
//std::cout << "select : " << pix_desc->name << std::endl;
93-
m_pixel = av_pix_fmt_desc_get_id( pix_desc );
94-
return;
95-
}
96-
}
97-
}
98-
99-
AVPixelFormat operator()(){ return m_pixel; }
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; }
10058

10159
private:
10260
AVPixelFormat m_pixel;

src/AvTranscoder/DatasStructures/Stream.hpp

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/AvTranscoder/InputStreamVideo.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ bool InputStreamVideo::setup( const std::string& filename, const size_t streamIn
9393
return true;
9494
}
9595

96-
std::vector<unsigned char>& InputStreamVideo::readNextFrame( std::vector<unsigned char>& frameBuffer )
96+
Image& InputStreamVideo::readNextFrame( Image& frameBuffer )
9797
{
9898
AVPacket pkt;
9999
AVFrame* frame = av_frame_alloc();
@@ -112,15 +112,13 @@ std::vector<unsigned char>& InputStreamVideo::readNextFrame( std::vector<unsigne
112112
av_free_packet( &pkt );
113113
}
114114

115-
//std::cout << "read frame " << frame->width << "x"<< frame->height << " disp time " << frame->display_picture_number << "/" << frame->coded_picture_number << std::endl;
116-
117-
frameBuffer.resize( avpicture_get_size( (AVPixelFormat)frame->format, frame->width, frame->height ) );
115+
frameBuffer.getBuffer().resize( avpicture_get_size( (AVPixelFormat)frame->format, frame->width, frame->height ) );
118116
//memcpy( &frameBuffer[0], frame->data, frameBuffer.size() );
119117

120118
// Copy pixel data from an AVPicture into one contiguous buffer.
121-
avpicture_layout( (AVPicture*)frame, (AVPixelFormat)frame->format, frame->width, frame->height, &frameBuffer[0], frameBuffer.size() );
119+
avpicture_layout( (AVPicture*)frame, (AVPixelFormat)frame->format, frame->width, frame->height, &frameBuffer.getBuffer()[0], frameBuffer.getBuffer().size() );
122120

123-
av_frame_free( &frame );
121+
//av_frame_free( &frame );
124122

125123
return frameBuffer;
126124
}

src/AvTranscoder/InputStreamVideo.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define _AV_TRANSCODER_INPUT_STREAM_VIDEO_HPP_
33

44
#include "InputStream.hpp"
5+
#include "DatasStructures/Image.hpp"
56

67
#include <vector>
78

@@ -24,7 +25,7 @@ class InputStreamVideo : public InputStream
2425
size_t getComponents() const { return components; }
2526
size_t getBitDepth() const { return bitDepth; }
2627

27-
std::vector<unsigned char>& readNextFrame( std::vector<unsigned char>& frameBuffer );
28+
Image& readNextFrame( Image& frameBuffer );
2829

2930
private:
3031
AVFormatContext* formatContext;

src/AvTranscoder/OutputFile.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ bool OutputFile::addAudioStream( )
109109
return true;
110110
}
111111

112-
bool OutputFile::wrap( const std::vector<unsigned char>& data, const size_t streamId )
112+
bool OutputFile::wrap( const Image& data, const size_t streamId )
113113
{
114114
AVPacket packet;
115115
av_init_packet( &packet );
116116

117-
packet.size = data.size();
118-
packet.data = (uint8_t*)&data[0];
117+
packet.size = data.getSize();
118+
packet.data = (uint8_t*)data.getPtr();
119119
packet.stream_index = streamId;
120120

121121
packet.dts = 0;

src/AvTranscoder/OutputFile.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef _AV_TRANSCODER_OUTPUT_FILE_HPP_
22
#define _AV_TRANSCODER_OUTPUT_FILE_HPP_
33

4+
#include "DatasStructures/Image.hpp"
5+
46
#include <string>
57
#include <vector>
68

@@ -24,7 +26,7 @@ class OutputFile
2426
bool addAudioStream( );
2527

2628

27-
bool wrap( const std::vector<unsigned char>& data, const size_t streamId );
29+
bool wrap( const Image& data, const size_t streamId );
2830

2931
private:
3032
AVOutputFormat* outputFormat;

src/AvTranscoder/OutputStreamVideo.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,20 @@ namespace avtranscoder
1818
{
1919

2020
OutputStreamVideo::OutputStreamVideo()
21-
: codec ( NULL )
22-
, codecContext( NULL )
21+
: codecContext( NULL )
2322
, width ( 0 )
2423
, height( 0 )
2524
, pixelFormat( AV_PIX_FMT_YUV422P )
2625
{
2726
}
2827

29-
bool OutputStreamVideo::setup()
28+
bool OutputStreamVideo::setup( const VideoStream& videoStream )
3029
{
3130
av_register_all(); // Warning: should be called only once
3231

3332
// av_log_set_level( AV_LOG_DEBUG );
34-
35-
if( ( codec = avcodec_find_encoder_by_name( "dnxhd" ) ) == NULL )
33+
AVCodec* codec;
34+
if( ( codec = avcodec_find_encoder( videoStream.getCodecId() ) ) == NULL )
3635
return false;
3736

3837
if( ( codecContext = avcodec_alloc_context3( codec ) ) == NULL )
@@ -59,7 +58,7 @@ bool OutputStreamVideo::setup()
5958
}
6059

6160

62-
bool OutputStreamVideo::encodeFrame( const std::vector<unsigned char>& sourceImage, std::vector<unsigned char>& codedFrame )
61+
bool OutputStreamVideo::encodeFrame( const Image& sourceImage, Image& codedFrame )
6362
{
6463
AVFrame* frame = avcodec_alloc_frame();
6564

@@ -69,7 +68,7 @@ bool OutputStreamVideo::encodeFrame( const std::vector<unsigned char>& sourceIma
6968
frame->width = width;
7069
frame->height = height;
7170
frame->format = pixelFormat;
72-
avpicture_fill( (AVPicture*)frame, const_cast< unsigned char * >( &sourceImage[0] ), pixelFormat, width, height );
71+
avpicture_fill( (AVPicture*)frame, const_cast< unsigned char * >( sourceImage.getPtr() ), pixelFormat, width, height );
7372

7473
AVPacket packet;
7574
av_init_packet( &packet );
@@ -100,8 +99,8 @@ bool OutputStreamVideo::encodeFrame( const std::vector<unsigned char>& sourceIma
10099

101100
if( ret == 0 && gotPacket == 1 )
102101
{
103-
codedFrame.resize( packet.size );
104-
memcpy( &codedFrame[0], packet.data, packet.size );
102+
codedFrame.getBuffer().resize( packet.size );
103+
memcpy( codedFrame.getPtr(), packet.data, packet.size );
105104
}
106105

107106
av_frame_free( &frame );

src/AvTranscoder/OutputStreamVideo.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ extern "C" {
1010

1111
#include "OutputStream.hpp"
1212

13+
#include "DatasStructures/Image.hpp"
14+
#include "DatasStructures/VideoStream.hpp"
15+
1316
#include <string>
1417
#include <vector>
1518

1619
class AVFormatContext;
17-
class AVCodec;
1820
class AVCodecContext;
1921

2022
namespace avtranscoder
@@ -25,7 +27,7 @@ class OutputStreamVideo : public OutputStream
2527
public:
2628
OutputStreamVideo();
2729

28-
bool setup();
30+
bool setup( const VideoStream& videoStream );
2931

3032
void setWidth ( const size_t w ) { width = w; }
3133
void setHeight ( const size_t h ) { height = h; }
@@ -35,10 +37,9 @@ class OutputStreamVideo : public OutputStream
3537
/**
3638
* @param[out] codecFrame blabla
3739
*/
38-
bool encodeFrame( const std::vector<unsigned char>& sourceImage, std::vector<unsigned char>& codedFrame );
40+
bool encodeFrame( const Image& sourceImage, Image& codedFrame );
3941

4042
private:
41-
AVCodec* codec;
4243
AVCodecContext* codecContext;
4344

4445
size_t width;

src/AvTranscoder/avTranscoder.i

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
%include "std_map.i"
77

88
%{
9+
#include <AvTranscoder/DatasStructures/Pixel.hpp>
10+
#include <AvTranscoder/DatasStructures/Image.hpp>
11+
#include <AvTranscoder/DatasStructures/VideoStream.hpp>
12+
913
#include <AvTranscoder/Media.hpp>
1014

1115
#include <AvTranscoder/OutputFile.hpp>
@@ -29,6 +33,10 @@ namespace std {
2933
%template(ChannelVector) vector< avtranscoder::Channel >;
3034
}
3135

36+
%include <AvTranscoder/DatasStructures/Pixel.hpp>
37+
%include <AvTranscoder/DatasStructures/Image.hpp>
38+
%include <AvTranscoder/DatasStructures/VideoStream.hpp>
39+
3240
%include <AvTranscoder/Media.hpp>
3341

3442
%include <AvTranscoder/OutputFile.hpp>

src/SConscript

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Import( "envPy" )
77

88
AvTranscoder = env.SharedLibrary(
99
'cppAvTranscoder',
10-
Glob( 'AvTranscoder/*.cpp' ),
10+
Glob( 'AvTranscoder/*.cpp' ) + Glob( 'AvTranscoder/DatasStructures/*.cpp' ) ,
1111
LIBS = [
1212
'libavcodec'
1313
]

0 commit comments

Comments
 (0)