Skip to content

Commit

Permalink
Merge pull request #254 from cchampet/dev_analyseRawStream
Browse files Browse the repository at this point in the history
Analyse raw video stream
  • Loading branch information
valnoel committed Jun 23, 2016
2 parents 8ddf8e1 + 39ebb6c commit 02d5cd0
Show file tree
Hide file tree
Showing 19 changed files with 351 additions and 102 deletions.
7 changes: 0 additions & 7 deletions src/AvTranscoder/file/InputFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,6 @@ void InputFile::analyse(IProgress& progress, const EAnalyseLevel level)
_properties->extractStreamProperties(progress, level);
}

FileProperties InputFile::analyseFile(const std::string& filename, IProgress& progress, const EAnalyseLevel level)
{
InputFile file(filename);
file.analyse(progress, level);
return file.getProperties();
}

bool InputFile::readNextPacket(CodedData& data, const size_t streamIndex)
{
bool nextPacketFound = false;
Expand Down
10 changes: 0 additions & 10 deletions src/AvTranscoder/file/InputFile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,6 @@ class AvExport InputFile
*/
virtual void setupUnwrapping(const ProfileLoader::Profile& profile);

public:
/**
* @brief Get media file properties using static method.
* @param filename input filename
* @param progress callback to get analysis progression
* @return structure of media metadatas
**/
static FileProperties analyseFile(const std::string& filename, IProgress& progress,
const EAnalyseLevel level = eAnalyseLevelFirstGop);

private:
/**
* @brief Get Fps from first video stream
Expand Down
4 changes: 2 additions & 2 deletions src/AvTranscoder/properties/AttachementProperties.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace avtranscoder
class AvExport AttachementProperties : public StreamProperties
{
public:
AttachementProperties(const FormatContext& formatContext, const size_t index)
: StreamProperties(formatContext, index)
AttachementProperties(const FileProperties& fileProperties, const size_t index)
: StreamProperties(fileProperties, index)
{
}
};
Expand Down
34 changes: 18 additions & 16 deletions src/AvTranscoder/properties/AudioProperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ extern "C" {
namespace avtranscoder
{

AudioProperties::AudioProperties(const FormatContext& formatContext, const size_t index)
: StreamProperties(formatContext, index)
AudioProperties::AudioProperties(const FileProperties& fileProperties, const size_t index)
: StreamProperties(fileProperties, index)
{
}

Expand Down Expand Up @@ -104,32 +104,34 @@ std::string AudioProperties::getChannelDescription() const
#endif
}

size_t AudioProperties::getSampleRate() const
size_t AudioProperties::getBitRate() const
{
if(!_codecContext)
throw std::runtime_error("unknown codec context");
return _codecContext->sample_rate;

// return bit rate of stream if present
if(_codecContext->bit_rate)
return _codecContext->bit_rate;

LOG_WARN("The bitrate of the stream '" << _streamIndex << "' of file '" << _formatContext->filename << "' is unknown.")
LOG_INFO("Compute the audio bitrate (suppose PCM audio data).")

const int bitsPerSample = av_get_bits_per_sample(_codecContext->codec_id); // 0 if unknown for the given codec
return getSampleRate() * getNbChannels() * bitsPerSample;
}

size_t AudioProperties::getNbChannels() const
size_t AudioProperties::getSampleRate() const
{
if(!_codecContext)
throw std::runtime_error("unknown codec context");
return _codecContext->channels;
return _codecContext->sample_rate;
}

size_t AudioProperties::getBitRate() const
size_t AudioProperties::getNbChannels() const
{
if(!_codecContext)
throw std::runtime_error("unknown codec context");

// return bit rate of stream
if(_codecContext->bit_rate)
return _codecContext->bit_rate;

// else get computed bit rate from our computation (warning: way to compute bit rate of PCM audio data)
int bitsPerSample = av_get_bits_per_sample(_codecContext->codec_id); // 0 if unknown for the given codec
return _codecContext->sample_rate * _codecContext->channels * bitsPerSample;
return _codecContext->channels;
}

size_t AudioProperties::getNbSamples() const
Expand Down Expand Up @@ -158,8 +160,8 @@ PropertyVector& AudioProperties::fillVector(PropertyVector& data) const

addProperty(data, "sampleFormatName", &AudioProperties::getSampleFormatName);
addProperty(data, "sampleFormatLongName", &AudioProperties::getSampleFormatLongName);
addProperty(data, "sampleRate", &AudioProperties::getSampleRate);
addProperty(data, "bitRate", &AudioProperties::getBitRate);
addProperty(data, "sampleRate", &AudioProperties::getSampleRate);
addProperty(data, "nbSamples", &AudioProperties::getNbSamples);
addProperty(data, "nbChannels", &AudioProperties::getNbChannels);
addProperty(data, "channelLayout", &AudioProperties::getChannelLayout);
Expand Down
4 changes: 2 additions & 2 deletions src/AvTranscoder/properties/AudioProperties.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ namespace avtranscoder
class AvExport AudioProperties : public StreamProperties
{
public:
AudioProperties(const FormatContext& formatContext, const size_t index);
AudioProperties(const FileProperties& fileProperties, const size_t index);

std::string getSampleFormatName() const;
std::string getSampleFormatLongName() const;
std::string getChannelLayout() const;
std::string getChannelName() const;
std::string getChannelDescription() const;

size_t getBitRate() const; ///< in bits/s, 0 if unknown
size_t getSampleRate() const;
size_t getNbChannels() const;
size_t getBitRate() const; ///< 0 if unknown
size_t getNbSamples() const;

size_t getTicksPerFrame() const;
Expand Down
4 changes: 2 additions & 2 deletions src/AvTranscoder/properties/DataProperties.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace avtranscoder
class AvExport DataProperties : public StreamProperties
{
public:
DataProperties(const FormatContext& formatContext, const size_t index)
: StreamProperties(formatContext, index)
DataProperties(const FileProperties& fileProperties, const size_t index)
: StreamProperties(fileProperties, index)
{
}

Expand Down
29 changes: 21 additions & 8 deletions src/AvTranscoder/properties/FileProperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <stdexcept>
#include <sstream>
#include <fstream>

namespace avtranscoder
{
Expand All @@ -29,7 +30,7 @@ FileProperties::FileProperties(const FormatContext& formatContext)

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

Expand All @@ -43,37 +44,37 @@ void FileProperties::extractStreamProperties(IProgress& progress, const EAnalyse
{
case AVMEDIA_TYPE_VIDEO:
{
VideoProperties properties(*_formatContext, streamIndex, progress, level);
VideoProperties properties(*this, streamIndex, progress, level);
_videoStreams.push_back(properties);
break;
}
case AVMEDIA_TYPE_AUDIO:
{
AudioProperties properties(*_formatContext, streamIndex);
AudioProperties properties(*this, streamIndex);
_audioStreams.push_back(properties);
break;
}
case AVMEDIA_TYPE_DATA:
{
DataProperties properties(*_formatContext, streamIndex);
DataProperties properties(*this, streamIndex);
_dataStreams.push_back(properties);
break;
}
case AVMEDIA_TYPE_SUBTITLE:
{
SubtitleProperties properties(*_formatContext, streamIndex);
SubtitleProperties properties(*this, streamIndex);
_subtitleStreams.push_back(properties);
break;
}
case AVMEDIA_TYPE_ATTACHMENT:
{
AttachementProperties properties(*_formatContext, streamIndex);
AttachementProperties properties(*this, streamIndex);
_attachementStreams.push_back(properties);
break;
}
case AVMEDIA_TYPE_UNKNOWN:
{
UnknownProperties properties(*_formatContext, streamIndex);
UnknownProperties properties(*this, streamIndex);
_unknownStreams.push_back(properties);
break;
}
Expand Down Expand Up @@ -121,7 +122,7 @@ void FileProperties::extractStreamProperties(IProgress& progress, const EAnalyse
_streams[unknownStreamIndex] = &_unknownStreams.at(streamIndex);
}

// if the analysis level has decoded some streams parts, return at the beginning
// Returns at the beginning of the stream after any deep analysis
if(level > eAnalyseLevelHeader && ! isRawFormat())
const_cast<FormatContext*>(_formatContext)->seek(0, AVSEEK_FLAG_BACKWARD);
}
Expand Down Expand Up @@ -186,6 +187,9 @@ float FileProperties::getDuration() const
{
if(!_avFormatContext)
throw std::runtime_error("unknown format context");
const size_t duration = _avFormatContext->duration;
if(duration == (size_t)AV_NOPTS_VALUE)
return 0;
return 1.0 * _avFormatContext->duration / AV_TIME_BASE;
}

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

size_t FileProperties::getFileSize() const
{
std::ifstream in(getFilename().c_str(), std::ios::binary | std::ios::ate);
return in.tellg();
}

size_t FileProperties::getPacketSize() const
{
if(!_avFormatContext)
Expand Down Expand Up @@ -243,10 +253,13 @@ PropertyVector& FileProperties::fillVector(PropertyVector& data) const
addProperty(data, "formatName", &FileProperties::getFormatName);
addProperty(data, "formatLongName", &FileProperties::getFormatLongName);
addProperty(data, "mimeType", &FileProperties::getFormatMimeType);
addProperty(data, "rawFormat", &FileProperties::isRawFormat);

addProperty(data, "startTime", &FileProperties::getStartTime);
addProperty(data, "duration", &FileProperties::getDuration);
addProperty(data, "bitrate", &FileProperties::getBitRate);
addProperty(data, "fileSize", &FileProperties::getFileSize);
addProperty(data, "packetSize", &FileProperties::getPacketSize);
addProperty(data, "numberOfStreams", &FileProperties::getNbStreams);
addProperty(data, "numberOfPrograms", &FileProperties::getProgramsCount);

Expand Down
7 changes: 4 additions & 3 deletions src/AvTranscoder/properties/FileProperties.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ class AvExport FileProperties

size_t getProgramsCount() const;
double getStartTime() const;
float getDuration() const; ///< in seconds
float getDuration() const; ///< in seconds, 0 if not available
size_t getBitRate() const; ///< total stream bitrate in bit/s, 0 if not available (result of a computation by ffmpeg)
size_t getFileSize() const; ///< in bytes
size_t getPacketSize() const;

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

const FormatContext& getFormatContext() { return *_formatContext; }
const FormatContext& getFormatContext() const { return *_formatContext; }

//@{
// @brief Get the properties at the indicated stream index
Expand All @@ -80,7 +81,7 @@ class AvExport FileProperties
//@}

#ifndef SWIG
const AVFormatContext& getAVFormatContext() { return *_avFormatContext; }
const AVFormatContext& getAVFormatContext() const { return *_avFormatContext; }
#endif

std::string allPropertiesAsJson() const; ///< Return all properties as a json format.
Expand Down
14 changes: 11 additions & 3 deletions src/AvTranscoder/properties/StreamProperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

#include <AvTranscoder/properties/util.hpp>
#include <AvTranscoder/properties/JsonWriter.hpp>
#include <AvTranscoder/properties/FileProperties.hpp>

#include <stdexcept>

namespace avtranscoder
{

StreamProperties::StreamProperties(const FormatContext& formatContext, const size_t index)
: _formatContext(&formatContext.getAVFormatContext())
StreamProperties::StreamProperties(const FileProperties& fileProperties, const size_t index)
: _fileProperties(&fileProperties)
, _formatContext(&fileProperties.getAVFormatContext())
, _codecContext(NULL)
, _codec(NULL)
, _streamIndex(index)
Expand Down Expand Up @@ -66,7 +68,13 @@ Rational StreamProperties::getTimeBase() const
float StreamProperties::getDuration() const
{
const Rational timeBase = getTimeBase();
return av_q2d(timeBase) * _formatContext->streams[_streamIndex]->duration;
const size_t duration = _formatContext->streams[_streamIndex]->duration;
if(duration == (size_t)AV_NOPTS_VALUE)
{
LOG_WARN("The duration of the stream '" << _streamIndex << "' of file '" << _formatContext->filename << "' is unknown.")
return 0;
}
return av_q2d(timeBase) * duration;
}

AVMediaType StreamProperties::getStreamType() const
Expand Down
8 changes: 6 additions & 2 deletions src/AvTranscoder/properties/StreamProperties.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@
namespace avtranscoder
{

class FileProperties;

/// Virtual based class of properties for all types of stream
class AvExport StreamProperties
{
public:
StreamProperties(const FormatContext& formatContext, const size_t index);
StreamProperties(const FileProperties& fileProperties, const size_t index);
virtual ~StreamProperties() = 0;

size_t getStreamIndex() const { return _streamIndex; }
size_t getStreamId() const;
Rational getTimeBase() const;
float getDuration() const; ///< in seconds
AVMediaType getStreamType() const;

virtual float getDuration() const; ///< in seconds, 0 if not available

size_t getCodecId() const;
std::string getCodecName() const;
std::string getCodecLongName() const;
Expand Down Expand Up @@ -65,6 +68,7 @@ class AvExport StreamProperties
#endif

protected:
const FileProperties* _fileProperties; ///< Has link (no ownership)
const AVFormatContext* _formatContext; ///< Has link (no ownership)
AVCodecContext* _codecContext; ///< Has link (no ownership)
AVCodec* _codec; ///< Has link (no ownership)
Expand Down
4 changes: 2 additions & 2 deletions src/AvTranscoder/properties/SubtitleProperties.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace avtranscoder
class AvExport SubtitleProperties : public StreamProperties
{
public:
SubtitleProperties(const FormatContext& formatContext, const size_t index)
: StreamProperties(formatContext, index)
SubtitleProperties(const FileProperties& fileProperties, const size_t index)
: StreamProperties(fileProperties, index)
{
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/AvTranscoder/properties/UnknownProperties.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace avtranscoder
class AvExport UnknownProperties : public StreamProperties
{
public:
UnknownProperties(const FormatContext& formatContext, const size_t index)
: StreamProperties(formatContext, index)
UnknownProperties(const FileProperties& fileProperties, const size_t index)
: StreamProperties(fileProperties, index)
{
}
};
Expand Down

0 comments on commit 02d5cd0

Please sign in to comment.