Skip to content

Commit 37aba05

Browse files
author
marnaud
committed
clean return codes and Data struture usages
1 parent f5a087b commit 37aba05

File tree

9 files changed

+54
-98
lines changed

9 files changed

+54
-98
lines changed

app/avTranscoder/avTranscoder.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,10 @@ void transcodeVideo( const char* inputfilename, const char* outputFilename )
9999
InputStreamVideo inputStreamVideo; // take the first video stream per default
100100

101101
av_log_set_level( AV_LOG_FATAL );
102-
//av_log_set_level( AV_LOG_DEBUG );
102+
av_log_set_level( AV_LOG_DEBUG );
103103

104104

105-
if( !inputStreamVideo.setup( inputfilename, 0 ) )
106-
{
107-
std::cout << "error during initialising video input reader" << std::endl;
108-
exit( -1 );
109-
}
105+
inputStreamVideo.setup( inputfilename, 0 );
110106

111107
//dVideo.set( key, value );
112108

@@ -134,8 +130,8 @@ void transcodeVideo( const char* inputfilename, const char* outputFilename )
134130

135131
Image sourceImage( imageDesc );
136132

137-
videoDesc.setVideoCodec( "dnxhd" );
138-
videoDesc.set( "b", 120000000 );
133+
videoDesc.setVideoCodec( "mpeg2video" );
134+
videoDesc.set( "b",50000000 );
139135
try
140136
{
141137
videoDesc.set( "unknownParameter", 120000000 );
@@ -157,8 +153,10 @@ void transcodeVideo( const char* inputfilename, const char* outputFilename )
157153
exit( -1 );
158154
}
159155

156+
DataStreamDesc dataStreamDesc;
157+
160158
Image imageToEncode( sourceImage );
161-
Image codedImage( sourceImage );
159+
DataStream codedImage( dataStreamDesc );
162160

163161

164162
OutputStreamAudio osAudioLeft ( ); // "AudioStreamEncoder" / "AudioOutputStream" ?
@@ -178,11 +176,7 @@ void transcodeVideo( const char* inputfilename, const char* outputFilename )
178176
InputFile inputFile;
179177
inputFile.setup( inputfilename );
180178

181-
if( ! of.addVideoStream( inputFile.getVideoDesc( 0 ) ) )
182-
{
183-
std::cout << "error during adding output video stream" << std::endl;
184-
exit( -1 );
185-
}
179+
of.addVideoStream( inputFile.getVideoDesc( 0 ) );
186180
/*of.addAudioStream();
187181
of.addAudioStream();
188182
of.addAudioStream();

src/AvTranscoder/InputFile.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ namespace avtranscoder
1919

2020
InputFile::InputFile()
2121
: m_formatContext ( NULL )
22-
, m_codec ( NULL )
23-
, m_codecContext ( NULL )
2422
, m_stream ( NULL )
2523
, m_filename ( "" )
2624
, m_packetCount ( 0 )
@@ -30,16 +28,11 @@ InputFile::InputFile()
3028

3129
InputFile::~InputFile()
3230
{
33-
if( m_formatContext )
31+
if( m_formatContext != NULL )
3432
{
3533
avformat_close_input( &m_formatContext );
3634
m_formatContext = NULL;
3735
}
38-
if( m_codecContext )
39-
{
40-
avcodec_close( m_codecContext );
41-
m_codecContext = NULL;
42-
}
4336
}
4437

4538
InputFile& InputFile::setup( const std::string& filename )

src/AvTranscoder/InputFile.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,9 @@ class InputFile
3333
protected:
3434
bool readNextPacket( AVPacket& packet, const size_t streamIndex );
3535

36-
private:
36+
protected:
3737
AVFormatContext* m_formatContext;
38-
39-
AVCodec* m_codec;
40-
AVCodecContext* m_codecContext;
38+
4139
AVStream* m_stream;
4240

4341
std::string m_filename;

src/AvTranscoder/InputStreamVideo.cpp

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,33 @@ extern "C" {
1212
}
1313

1414
#include <iostream>
15+
#include <stdexcept>
1516

1617
namespace avtranscoder
1718
{
1819

1920
InputStreamVideo::InputStreamVideo()
20-
: m_formatContext( NULL )
21-
, m_codec ( NULL )
22-
, m_codecContext ( NULL )
23-
, m_frame ( NULL )
21+
: m_codec ( NULL )
22+
, m_codecContext ( NULL )
23+
, m_frame ( NULL )
2424
, m_selectedStream( -1 )
2525
{}
2626

2727
InputStreamVideo::~InputStreamVideo()
2828
{
29-
if( m_formatContext )
30-
{
31-
avformat_close_input( &m_formatContext );
32-
m_formatContext = NULL;
33-
}
34-
if( m_codecContext )
29+
/*if( m_codecContext != NULL )
3530
{
3631
avcodec_close( m_codecContext );
3732
m_codecContext = NULL;
38-
}
33+
}*/
3934
if( m_frame != NULL )
4035
{
4136
av_frame_free( &m_frame );
4237
m_frame = NULL;
4338
}
4439
}
4540

46-
bool InputStreamVideo::setup( const std::string& filename, const size_t streamIndex )
41+
void InputStreamVideo::setup( const std::string& filename, const size_t streamIndex )
4742
{
4843
av_register_all();
4944

@@ -63,19 +58,21 @@ bool InputStreamVideo::setup( const std::string& filename, const size_t streamIn
6358
}
6459

6560
if( m_selectedStream == -1 )
66-
return false;
67-
61+
{
62+
throw std::runtime_error( "unable to find video stream" );
63+
}
64+
6865
m_codec = avcodec_find_decoder( m_formatContext->streams[m_selectedStream]->codec->codec_id );
6966
if( m_codec == NULL )
7067
{
71-
return false;
68+
throw std::runtime_error( "codec not supported" );
7269
}
7370

7471
m_codecContext = avcodec_alloc_context3( m_codec );
7572

7673
if( m_codecContext == NULL )
7774
{
78-
return false;
75+
throw std::runtime_error( "unable to find context for codec" );
7976
}
8077

8178
// if( codec->capabilities & CODEC_CAP_TRUNCATED )
@@ -85,16 +82,14 @@ bool InputStreamVideo::setup( const std::string& filename, const size_t streamIn
8582

8683
if( m_codecContext == NULL || m_codec == NULL )
8784
{
88-
return false;
85+
throw std::runtime_error( "unable open codec" );
8986
}
9087

9188
m_frame = avcodec_alloc_frame();
9289
if( m_frame == NULL )
9390
{
94-
return false;
91+
throw std::runtime_error( "unable to setup frame buffer" );
9592
}
96-
97-
return true;
9893
}
9994

10095
bool InputStreamVideo::readNextFrame( Image& frameBuffer )

src/AvTranscoder/InputStreamVideo.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@ class InputStreamVideo : public InputFile
2121
InputStreamVideo();
2222
~InputStreamVideo();
2323

24-
bool setup( const std::string& filename, const size_t streamIndex );
24+
void setup( const std::string& filename, const size_t streamIndex );
2525

2626
bool readNextFrame( Image& frameBuffer );
2727

2828
private:
29-
AVFormatContext* m_formatContext;
3029
AVCodec* m_codec;
3130
AVCodecContext* m_codecContext;
3231
AVFrame* m_frame;

src/AvTranscoder/OutputFile.cpp

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ extern "C" {
1212
}
1313

1414
#include <iostream>
15+
#include <stdexcept>
16+
#include <cassert>
1517

1618
namespace avtranscoder
1719
{
@@ -33,11 +35,13 @@ bool OutputFile::setup()
3335
outputFormat = av_guess_format( NULL, filename.c_str(), NULL);
3436

3537
if( !outputFormat )
36-
return false;
38+
{
39+
throw std::runtime_error( "unable to find format" );
40+
}
3741

3842
if( ( formatContext = avformat_alloc_context() ) == NULL )
3943
{
40-
return false;
44+
throw std::runtime_error( "unable to create format context" );
4145
}
4246

4347
formatContext->oformat = outputFormat;
@@ -46,41 +50,40 @@ bool OutputFile::setup()
4650
{
4751
if( avio_open2( &formatContext->pb, filename.c_str(), AVIO_FLAG_WRITE, NULL, NULL ) < 0 )
4852
{
49-
// @TODO free format here
50-
return false;
53+
avformat_close_input( &formatContext );
54+
formatContext = NULL;
55+
throw std::runtime_error( "error when opening output format" );
5156
}
5257
}
5358

5459
return formatContext != NULL;
5560
}
5661

57-
bool OutputFile::addVideoStream( const VideoDesc& videoDesc )
62+
void OutputFile::addVideoStream( const VideoDesc& videoDesc )
5863
{
59-
if( formatContext == NULL )
60-
return false; // throw not return ...
64+
assert( formatContext != NULL );
6165

6266
if( ( stream = avformat_new_stream( formatContext, videoDesc.getCodec() ) ) == NULL )
63-
return false;
67+
{
68+
throw std::runtime_error( "unable to add new video stream" );
69+
}
6470

6571
// need to set the time_base on the AVCodecContext and the AVStream...
66-
stream->codec->time_base = videoDesc.getCodecContext()->time_base;
67-
stream->time_base = stream->codec->time_base;
68-
6972
stream->codec->width = videoDesc.getCodecContext()->width;
7073
stream->codec->height = videoDesc.getCodecContext()->height;
71-
stream->codec->bit_rate = 50000000;
72-
stream->codec->time_base.num = 1;
73-
stream->codec->time_base.den = 25;
74-
stream->codec->pix_fmt = AV_PIX_FMT_YUV422P;
74+
stream->codec->bit_rate = videoDesc.getCodecContext()->bit_rate;
75+
stream->codec->time_base = videoDesc.getCodecContext()->time_base;
76+
stream->codec->pix_fmt = videoDesc.getCodecContext()->pix_fmt;
77+
stream->codec->profile = videoDesc.getCodecContext()->profile;
78+
stream->codec->level = videoDesc.getCodecContext()->level;
79+
80+
stream->time_base = stream->codec->time_base;
7581

7682
// to move in endSetup
7783
if( avformat_write_header( formatContext, NULL ) != 0 )
7884
{
79-
std::cout << "could not write header" << std::endl;
80-
return false;
85+
throw std::runtime_error( "could not write header" );
8186
}
82-
83-
return true;
8487
}
8588

8689
bool OutputFile::addAudioStream( )
@@ -103,30 +106,6 @@ bool OutputFile::addAudioStream( )
103106
return true;
104107
}
105108

106-
bool OutputFile::wrap( const Image& data, const size_t streamId )
107-
{
108-
AVPacket packet;
109-
av_init_packet( &packet );
110-
111-
packet.size = data.getSize();
112-
packet.data = (uint8_t*)data.getPtr();
113-
packet.stream_index = streamId;
114-
115-
packet.dts = 0;
116-
packet.pts = packetCount;
117-
118-
if( av_interleaved_write_frame( formatContext, &packet ) != 0 )
119-
{
120-
std::cout << "error when writting packet in stream" << std::endl;
121-
return false;
122-
}
123-
124-
av_free_packet( &packet );
125-
126-
packetCount++;
127-
return true;
128-
}
129-
130109
bool OutputFile::wrap( const DataStream& data, const size_t streamId )
131110
{
132111
AVPacket packet;

src/AvTranscoder/OutputFile.hpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,13 @@ class OutputFile
2424

2525
bool setup();
2626

27-
bool addVideoStream( const VideoDesc& videoDesc );
27+
void addVideoStream( const VideoDesc& videoDesc );
2828

2929

30-
bool addVideoStream( const std::string& codecName );
30+
void addVideoStream( const std::string& codecName );
3131

3232
bool addAudioStream( );
3333

34-
35-
bool wrap( const Image& data, const size_t streamId );
36-
3734
bool wrap( const DataStream& data, const size_t streamId );
3835

3936
private:

src/AvTranscoder/OutputStreamVideo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ bool OutputStreamVideo::setup( )
3939
}
4040

4141

42-
bool OutputStreamVideo::encodeFrame( const Image& sourceImage, Image& codedFrame )
42+
bool OutputStreamVideo::encodeFrame( const Image& sourceImage, DataStream& codedFrame )
4343
{
4444
AVFrame* frame = avcodec_alloc_frame();
4545

src/AvTranscoder/OutputStreamVideo.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ extern "C" {
1212

1313
#include "DatasStructures/Image.hpp"
1414
#include "DatasStructures/VideoDesc.hpp"
15+
#include "DatasStructures/DataStreamDesc.hpp"
1516

1617
#include <string>
1718
#include <vector>
@@ -34,7 +35,7 @@ class OutputStreamVideo : public OutputStream
3435
/**
3536
* @param[out] codecFrame blabla
3637
*/
37-
bool encodeFrame( const Image& sourceImage, Image& codedFrame );
38+
bool encodeFrame( const Image& sourceImage, DataStream& codedFrame );
3839

3940
VideoDesc& getVideoDesc() { return m_videoDesc; }
4041

0 commit comments

Comments
 (0)