Skip to content

Commit 5d0f51f

Browse files
author
Clement Champetier
committed
Video/Audio transform: add check before converting the data
* Remove protected method inherited from ITransform. * Add the same series of test for the video and the audio transform.
1 parent 6f87435 commit 5d0f51f

File tree

5 files changed

+25
-33
lines changed

5 files changed

+25
-33
lines changed

src/AvTranscoder/transform/AudioTransform.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ extern "C" {
2626
#endif
2727
}
2828

29-
#include <stdexcept>
3029
#include <sstream>
30+
#include <cassert>
31+
#include <stdexcept>
3132

3233
namespace avtranscoder
3334
{
@@ -43,7 +44,7 @@ AudioTransform::~AudioTransform()
4344
FreeResampleContext(&_audioConvertContext);
4445
}
4546

46-
bool AudioTransform::init(const Frame& srcFrame, const Frame& dstFrame)
47+
bool AudioTransform::init(const AudioFrame& src, const AudioFrame& dst)
4748
{
4849
// Set convert context
4950
_audioConvertContext = AllocResampleContext();
@@ -52,9 +53,6 @@ bool AudioTransform::init(const Frame& srcFrame, const Frame& dstFrame)
5253
throw std::runtime_error("unable to create audio convert context");
5354
}
5455

55-
const AudioFrame& src = static_cast<const AudioFrame&>(srcFrame);
56-
const AudioFrame& dst = static_cast<const AudioFrame&>(dstFrame);
57-
5856
av_opt_set_int(_audioConvertContext, "in_channel_layout", src.getChannelLayout(), 0);
5957
av_opt_set_int(_audioConvertContext, "out_channel_layout", dst.getChannelLayout(), 0);
6058
av_opt_set_int(_audioConvertContext, "in_sample_rate", src.getSampleRate(), 0);
@@ -92,8 +90,17 @@ bool AudioTransform::init(const Frame& srcFrame, const Frame& dstFrame)
9290

9391
void AudioTransform::convert(const Frame& srcFrame, Frame& dstFrame)
9492
{
93+
const AudioFrame& src = static_cast<const AudioFrame&>(srcFrame);
94+
const AudioFrame& dst = static_cast<const AudioFrame&>(dstFrame);
95+
96+
assert(src.getSampleRate() > 0);
97+
assert(src.getNbChannels() > 0);
98+
assert(src.getNbSamplesPerChannel() > 0);
99+
assert(src.getSampleFormat() != AV_SAMPLE_FMT_NONE);
100+
assert(dst.getSize() > 0);
101+
95102
if(!_isInit)
96-
_isInit = init(srcFrame, dstFrame);
103+
_isInit = init(src, dst);
97104

98105
// if number of samples change from previous frame
99106
const size_t nbInputSamplesPerChannel = srcFrame.getAVFrame().nb_samples;
@@ -110,13 +117,9 @@ void AudioTransform::convert(const Frame& srcFrame, Frame& dstFrame)
110117
swr_convert(_audioConvertContext, dstData, nbInputSamplesPerChannel, srcData, nbInputSamplesPerChannel);
111118
#endif
112119

120+
// update the number of samples of the output frame
113121
if(nbOutputSamplesPerChannel < 0)
114-
{
115-
throw std::runtime_error("unable to convert audio samples");
116-
}
117-
else
118-
{
119-
dstFrame.getAVFrame().nb_samples = nbOutputSamplesPerChannel;
120-
}
122+
throw std::runtime_error("Unable to convert audio samples");
123+
dstFrame.getAVFrame().nb_samples = nbOutputSamplesPerChannel;
121124
}
122125
}

src/AvTranscoder/transform/AudioTransform.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44
#include "ITransform.hpp"
55

6-
#include <AvTranscoder/common.hpp>
7-
#include <AvTranscoder/data/decoded/Frame.hpp>
6+
#include <AvTranscoder/data/decoded/AudioFrame.hpp>
87

98
#ifdef AVTRANSCODER_LIBAV_DEPENDENCY
109
#define ResampleContext AVAudioResampleContext
@@ -30,7 +29,7 @@ class AvExport AudioTransform : public ITransform
3029
void convert(const Frame& srcFrame, Frame& dstFrame);
3130

3231
private:
33-
bool init(const Frame& srcFrame, const Frame& dstFrame);
32+
bool init(const AudioFrame& src, const AudioFrame& dst);
3433

3534
private:
3635
ResampleContext* _audioConvertContext;

src/AvTranscoder/transform/ITransform.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ class AvExport ITransform
1515
virtual ~ITransform() {}
1616

1717
virtual void convert(const Frame& src, Frame& dst) = 0;
18-
19-
protected:
20-
virtual bool init(const Frame& src, const Frame& dst) = 0;
2118
};
2219
}
2320

src/AvTranscoder/transform/VideoTransform.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include "VideoTransform.hpp"
22

3-
#include <AvTranscoder/data/decoded/VideoFrame.hpp>
4-
53
extern "C" {
64
#include <libavcodec/avcodec.h>
75
#include <libswscale/swscale.h>
@@ -31,12 +29,8 @@ VideoTransform::~VideoTransform()
3129
sws_freeContext(_imageConvertContext);
3230
}
3331

34-
bool VideoTransform::init(const Frame& srcFrame, const Frame& dstFrame)
32+
bool VideoTransform::init(const VideoFrame& src, const VideoFrame& dst)
3533
{
36-
// Set convert context
37-
const VideoFrame& src = static_cast<const VideoFrame&>(srcFrame);
38-
const VideoFrame& dst = static_cast<const VideoFrame&>(dstFrame);
39-
4034
const AVPixelFormat srcPixelFormat = src.getPixelFormat();
4135
const AVPixelFormat dstPixelFormat = dst.getPixelFormat();
4236

@@ -71,12 +65,13 @@ void VideoTransform::convert(const Frame& srcFrame, Frame& dstFrame)
7165
const VideoFrame& src = static_cast<const VideoFrame&>(srcFrame);
7266
VideoFrame& dst = static_cast<VideoFrame&>(dstFrame);
7367

74-
assert(src.getWidth() != 0);
75-
assert(src.getHeight() != 0);
68+
assert(src.getWidth() > 0);
69+
assert(src.getHeight() > 0);
7670
assert(src.getPixelFormat() != AV_PIX_FMT_NONE);
71+
assert(dst.getSize() > 0);
7772

7873
if(!_isInit)
79-
_isInit = init(srcFrame, dstFrame);
74+
_isInit = init(src, dst);
8075

8176
if(!_imageConvertContext)
8277
{

src/AvTranscoder/transform/VideoTransform.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
#ifndef _AV_TRANSCODER_ESSENCE_TRANSFORM_VIDEO_ESSENCE_TRANSFORM_HPP
22
#define _AV_TRANSCODER_ESSENCE_TRANSFORM_VIDEO_ESSENCE_TRANSFORM_HPP
33

4-
#include <vector>
5-
64
#include "ITransform.hpp"
75

8-
#include <AvTranscoder/data/decoded/Frame.hpp>
6+
#include <AvTranscoder/data/decoded/VideoFrame.hpp>
97

108
class SwsContext;
119

@@ -25,7 +23,7 @@ class AvExport VideoTransform : public ITransform
2523
void convert(const Frame& srcFrame, Frame& dstFrame);
2624

2725
private:
28-
bool init(const Frame& srcFrame, const Frame& dstFrame);
26+
bool init(const VideoFrame& src, const VideoFrame& dst);
2927

3028
SwsContext* _imageConvertContext;
3129
bool _isInit;

0 commit comments

Comments
 (0)