Skip to content

Commit 02d5cd0

Browse files
authored
Merge pull request #254 from cchampet/dev_analyseRawStream
Analyse raw video stream
2 parents 8ddf8e1 + 39ebb6c commit 02d5cd0

19 files changed

+351
-102
lines changed

src/AvTranscoder/file/InputFile.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,6 @@ void InputFile::analyse(IProgress& progress, const EAnalyseLevel level)
5353
_properties->extractStreamProperties(progress, level);
5454
}
5555

56-
FileProperties InputFile::analyseFile(const std::string& filename, IProgress& progress, const EAnalyseLevel level)
57-
{
58-
InputFile file(filename);
59-
file.analyse(progress, level);
60-
return file.getProperties();
61-
}
62-
6356
bool InputFile::readNextPacket(CodedData& data, const size_t streamIndex)
6457
{
6558
bool nextPacketFound = false;

src/AvTranscoder/file/InputFile.hpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,6 @@ class AvExport InputFile
8787
*/
8888
virtual void setupUnwrapping(const ProfileLoader::Profile& profile);
8989

90-
public:
91-
/**
92-
* @brief Get media file properties using static method.
93-
* @param filename input filename
94-
* @param progress callback to get analysis progression
95-
* @return structure of media metadatas
96-
**/
97-
static FileProperties analyseFile(const std::string& filename, IProgress& progress,
98-
const EAnalyseLevel level = eAnalyseLevelFirstGop);
99-
10090
private:
10191
/**
10292
* @brief Get Fps from first video stream

src/AvTranscoder/properties/AttachementProperties.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ namespace avtranscoder
99
class AvExport AttachementProperties : public StreamProperties
1010
{
1111
public:
12-
AttachementProperties(const FormatContext& formatContext, const size_t index)
13-
: StreamProperties(formatContext, index)
12+
AttachementProperties(const FileProperties& fileProperties, const size_t index)
13+
: StreamProperties(fileProperties, index)
1414
{
1515
}
1616
};

src/AvTranscoder/properties/AudioProperties.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ extern "C" {
1515
namespace avtranscoder
1616
{
1717

18-
AudioProperties::AudioProperties(const FormatContext& formatContext, const size_t index)
19-
: StreamProperties(formatContext, index)
18+
AudioProperties::AudioProperties(const FileProperties& fileProperties, const size_t index)
19+
: StreamProperties(fileProperties, index)
2020
{
2121
}
2222

@@ -104,32 +104,34 @@ std::string AudioProperties::getChannelDescription() const
104104
#endif
105105
}
106106

107-
size_t AudioProperties::getSampleRate() const
107+
size_t AudioProperties::getBitRate() const
108108
{
109109
if(!_codecContext)
110110
throw std::runtime_error("unknown codec context");
111-
return _codecContext->sample_rate;
111+
112+
// return bit rate of stream if present
113+
if(_codecContext->bit_rate)
114+
return _codecContext->bit_rate;
115+
116+
LOG_WARN("The bitrate of the stream '" << _streamIndex << "' of file '" << _formatContext->filename << "' is unknown.")
117+
LOG_INFO("Compute the audio bitrate (suppose PCM audio data).")
118+
119+
const int bitsPerSample = av_get_bits_per_sample(_codecContext->codec_id); // 0 if unknown for the given codec
120+
return getSampleRate() * getNbChannels() * bitsPerSample;
112121
}
113122

114-
size_t AudioProperties::getNbChannels() const
123+
size_t AudioProperties::getSampleRate() const
115124
{
116125
if(!_codecContext)
117126
throw std::runtime_error("unknown codec context");
118-
return _codecContext->channels;
127+
return _codecContext->sample_rate;
119128
}
120129

121-
size_t AudioProperties::getBitRate() const
130+
size_t AudioProperties::getNbChannels() const
122131
{
123132
if(!_codecContext)
124133
throw std::runtime_error("unknown codec context");
125-
126-
// return bit rate of stream
127-
if(_codecContext->bit_rate)
128-
return _codecContext->bit_rate;
129-
130-
// else get computed bit rate from our computation (warning: way to compute bit rate of PCM audio data)
131-
int bitsPerSample = av_get_bits_per_sample(_codecContext->codec_id); // 0 if unknown for the given codec
132-
return _codecContext->sample_rate * _codecContext->channels * bitsPerSample;
134+
return _codecContext->channels;
133135
}
134136

135137
size_t AudioProperties::getNbSamples() const
@@ -158,8 +160,8 @@ PropertyVector& AudioProperties::fillVector(PropertyVector& data) const
158160

159161
addProperty(data, "sampleFormatName", &AudioProperties::getSampleFormatName);
160162
addProperty(data, "sampleFormatLongName", &AudioProperties::getSampleFormatLongName);
161-
addProperty(data, "sampleRate", &AudioProperties::getSampleRate);
162163
addProperty(data, "bitRate", &AudioProperties::getBitRate);
164+
addProperty(data, "sampleRate", &AudioProperties::getSampleRate);
163165
addProperty(data, "nbSamples", &AudioProperties::getNbSamples);
164166
addProperty(data, "nbChannels", &AudioProperties::getNbChannels);
165167
addProperty(data, "channelLayout", &AudioProperties::getChannelLayout);

src/AvTranscoder/properties/AudioProperties.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ namespace avtranscoder
1111
class AvExport AudioProperties : public StreamProperties
1212
{
1313
public:
14-
AudioProperties(const FormatContext& formatContext, const size_t index);
14+
AudioProperties(const FileProperties& fileProperties, const size_t index);
1515

1616
std::string getSampleFormatName() const;
1717
std::string getSampleFormatLongName() const;
1818
std::string getChannelLayout() const;
1919
std::string getChannelName() const;
2020
std::string getChannelDescription() const;
2121

22+
size_t getBitRate() const; ///< in bits/s, 0 if unknown
2223
size_t getSampleRate() const;
2324
size_t getNbChannels() const;
24-
size_t getBitRate() const; ///< 0 if unknown
2525
size_t getNbSamples() const;
2626

2727
size_t getTicksPerFrame() const;

src/AvTranscoder/properties/DataProperties.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ namespace avtranscoder
99
class AvExport DataProperties : public StreamProperties
1010
{
1111
public:
12-
DataProperties(const FormatContext& formatContext, const size_t index)
13-
: StreamProperties(formatContext, index)
12+
DataProperties(const FileProperties& fileProperties, const size_t index)
13+
: StreamProperties(fileProperties, index)
1414
{
1515
}
1616

src/AvTranscoder/properties/FileProperties.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <stdexcept>
88
#include <sstream>
9+
#include <fstream>
910

1011
namespace avtranscoder
1112
{
@@ -29,7 +30,7 @@ FileProperties::FileProperties(const FormatContext& formatContext)
2930

3031
void FileProperties::extractStreamProperties(IProgress& progress, const EAnalyseLevel level)
3132
{
32-
// if the analysis level wiil decode some streams parts, seek at the beginning
33+
// Returns at the beginning of the stream before any deep analysis
3334
if(level > eAnalyseLevelHeader && ! isRawFormat())
3435
const_cast<FormatContext*>(_formatContext)->seek(0, AVSEEK_FLAG_BACKWARD);
3536

@@ -43,37 +44,37 @@ void FileProperties::extractStreamProperties(IProgress& progress, const EAnalyse
4344
{
4445
case AVMEDIA_TYPE_VIDEO:
4546
{
46-
VideoProperties properties(*_formatContext, streamIndex, progress, level);
47+
VideoProperties properties(*this, streamIndex, progress, level);
4748
_videoStreams.push_back(properties);
4849
break;
4950
}
5051
case AVMEDIA_TYPE_AUDIO:
5152
{
52-
AudioProperties properties(*_formatContext, streamIndex);
53+
AudioProperties properties(*this, streamIndex);
5354
_audioStreams.push_back(properties);
5455
break;
5556
}
5657
case AVMEDIA_TYPE_DATA:
5758
{
58-
DataProperties properties(*_formatContext, streamIndex);
59+
DataProperties properties(*this, streamIndex);
5960
_dataStreams.push_back(properties);
6061
break;
6162
}
6263
case AVMEDIA_TYPE_SUBTITLE:
6364
{
64-
SubtitleProperties properties(*_formatContext, streamIndex);
65+
SubtitleProperties properties(*this, streamIndex);
6566
_subtitleStreams.push_back(properties);
6667
break;
6768
}
6869
case AVMEDIA_TYPE_ATTACHMENT:
6970
{
70-
AttachementProperties properties(*_formatContext, streamIndex);
71+
AttachementProperties properties(*this, streamIndex);
7172
_attachementStreams.push_back(properties);
7273
break;
7374
}
7475
case AVMEDIA_TYPE_UNKNOWN:
7576
{
76-
UnknownProperties properties(*_formatContext, streamIndex);
77+
UnknownProperties properties(*this, streamIndex);
7778
_unknownStreams.push_back(properties);
7879
break;
7980
}
@@ -121,7 +122,7 @@ void FileProperties::extractStreamProperties(IProgress& progress, const EAnalyse
121122
_streams[unknownStreamIndex] = &_unknownStreams.at(streamIndex);
122123
}
123124

124-
// if the analysis level has decoded some streams parts, return at the beginning
125+
// Returns at the beginning of the stream after any deep analysis
125126
if(level > eAnalyseLevelHeader && ! isRawFormat())
126127
const_cast<FormatContext*>(_formatContext)->seek(0, AVSEEK_FLAG_BACKWARD);
127128
}
@@ -186,6 +187,9 @@ float FileProperties::getDuration() const
186187
{
187188
if(!_avFormatContext)
188189
throw std::runtime_error("unknown format context");
190+
const size_t duration = _avFormatContext->duration;
191+
if(duration == (size_t)AV_NOPTS_VALUE)
192+
return 0;
189193
return 1.0 * _avFormatContext->duration / AV_TIME_BASE;
190194
}
191195

@@ -196,6 +200,12 @@ size_t FileProperties::getBitRate() const
196200
return _avFormatContext->bit_rate;
197201
}
198202

203+
size_t FileProperties::getFileSize() const
204+
{
205+
std::ifstream in(getFilename().c_str(), std::ios::binary | std::ios::ate);
206+
return in.tellg();
207+
}
208+
199209
size_t FileProperties::getPacketSize() const
200210
{
201211
if(!_avFormatContext)
@@ -243,10 +253,13 @@ PropertyVector& FileProperties::fillVector(PropertyVector& data) const
243253
addProperty(data, "formatName", &FileProperties::getFormatName);
244254
addProperty(data, "formatLongName", &FileProperties::getFormatLongName);
245255
addProperty(data, "mimeType", &FileProperties::getFormatMimeType);
256+
addProperty(data, "rawFormat", &FileProperties::isRawFormat);
246257

247258
addProperty(data, "startTime", &FileProperties::getStartTime);
248259
addProperty(data, "duration", &FileProperties::getDuration);
249260
addProperty(data, "bitrate", &FileProperties::getBitRate);
261+
addProperty(data, "fileSize", &FileProperties::getFileSize);
262+
addProperty(data, "packetSize", &FileProperties::getPacketSize);
250263
addProperty(data, "numberOfStreams", &FileProperties::getNbStreams);
251264
addProperty(data, "numberOfPrograms", &FileProperties::getProgramsCount);
252265

src/AvTranscoder/properties/FileProperties.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ class AvExport FileProperties
4646

4747
size_t getProgramsCount() const;
4848
double getStartTime() const;
49-
float getDuration() const; ///< in seconds
49+
float getDuration() const; ///< in seconds, 0 if not available
5050
size_t getBitRate() const; ///< total stream bitrate in bit/s, 0 if not available (result of a computation by ffmpeg)
51+
size_t getFileSize() const; ///< in bytes
5152
size_t getPacketSize() const;
5253

5354
const PropertyVector& getMetadatas() const { return _metadatas; }
@@ -60,7 +61,7 @@ class AvExport FileProperties
6061
size_t getNbAttachementStreams() const { return _attachementStreams.size(); }
6162
size_t getNbUnknownStreams() const { return _unknownStreams.size(); }
6263

63-
const FormatContext& getFormatContext() { return *_formatContext; }
64+
const FormatContext& getFormatContext() const { return *_formatContext; }
6465

6566
//@{
6667
// @brief Get the properties at the indicated stream index
@@ -80,7 +81,7 @@ class AvExport FileProperties
8081
//@}
8182

8283
#ifndef SWIG
83-
const AVFormatContext& getAVFormatContext() { return *_avFormatContext; }
84+
const AVFormatContext& getAVFormatContext() const { return *_avFormatContext; }
8485
#endif
8586

8687
std::string allPropertiesAsJson() const; ///< Return all properties as a json format.

src/AvTranscoder/properties/StreamProperties.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
#include <AvTranscoder/properties/util.hpp>
44
#include <AvTranscoder/properties/JsonWriter.hpp>
5+
#include <AvTranscoder/properties/FileProperties.hpp>
56

67
#include <stdexcept>
78

89
namespace avtranscoder
910
{
1011

11-
StreamProperties::StreamProperties(const FormatContext& formatContext, const size_t index)
12-
: _formatContext(&formatContext.getAVFormatContext())
12+
StreamProperties::StreamProperties(const FileProperties& fileProperties, const size_t index)
13+
: _fileProperties(&fileProperties)
14+
, _formatContext(&fileProperties.getAVFormatContext())
1315
, _codecContext(NULL)
1416
, _codec(NULL)
1517
, _streamIndex(index)
@@ -66,7 +68,13 @@ Rational StreamProperties::getTimeBase() const
6668
float StreamProperties::getDuration() const
6769
{
6870
const Rational timeBase = getTimeBase();
69-
return av_q2d(timeBase) * _formatContext->streams[_streamIndex]->duration;
71+
const size_t duration = _formatContext->streams[_streamIndex]->duration;
72+
if(duration == (size_t)AV_NOPTS_VALUE)
73+
{
74+
LOG_WARN("The duration of the stream '" << _streamIndex << "' of file '" << _formatContext->filename << "' is unknown.")
75+
return 0;
76+
}
77+
return av_q2d(timeBase) * duration;
7078
}
7179

7280
AVMediaType StreamProperties::getStreamType() const

src/AvTranscoder/properties/StreamProperties.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,22 @@
99
namespace avtranscoder
1010
{
1111

12+
class FileProperties;
13+
1214
/// Virtual based class of properties for all types of stream
1315
class AvExport StreamProperties
1416
{
1517
public:
16-
StreamProperties(const FormatContext& formatContext, const size_t index);
18+
StreamProperties(const FileProperties& fileProperties, const size_t index);
1719
virtual ~StreamProperties() = 0;
1820

1921
size_t getStreamIndex() const { return _streamIndex; }
2022
size_t getStreamId() const;
2123
Rational getTimeBase() const;
22-
float getDuration() const; ///< in seconds
2324
AVMediaType getStreamType() const;
2425

26+
virtual float getDuration() const; ///< in seconds, 0 if not available
27+
2528
size_t getCodecId() const;
2629
std::string getCodecName() const;
2730
std::string getCodecLongName() const;
@@ -65,6 +68,7 @@ class AvExport StreamProperties
6568
#endif
6669

6770
protected:
71+
const FileProperties* _fileProperties; ///< Has link (no ownership)
6872
const AVFormatContext* _formatContext; ///< Has link (no ownership)
6973
AVCodecContext* _codecContext; ///< Has link (no ownership)
7074
AVCodec* _codec; ///< Has link (no ownership)

src/AvTranscoder/properties/SubtitleProperties.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ namespace avtranscoder
99
class AvExport SubtitleProperties : public StreamProperties
1010
{
1111
public:
12-
SubtitleProperties(const FormatContext& formatContext, const size_t index)
13-
: StreamProperties(formatContext, index)
12+
SubtitleProperties(const FileProperties& fileProperties, const size_t index)
13+
: StreamProperties(fileProperties, index)
1414
{
1515
}
1616
};

src/AvTranscoder/properties/UnknownProperties.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ namespace avtranscoder
99
class AvExport UnknownProperties : public StreamProperties
1010
{
1111
public:
12-
UnknownProperties(const FormatContext& formatContext, const size_t index)
13-
: StreamProperties(formatContext, index)
12+
UnknownProperties(const FileProperties& fileProperties, const size_t index)
13+
: StreamProperties(fileProperties, index)
1414
{
1515
}
1616
};

0 commit comments

Comments
 (0)