Tag Parser: mpegaudio/mpegaudioframestream.cpp Source File

Tag Parser 12.5.2 C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags

Loading...
Searching...
No Matches
mpegaudioframestream.cpp
Go to the documentation of this file.
1#include "./mpegaudioframestream.h"
2
3#include "../exceptions.h"
4#include "../mediaformat.h"
5
6#include <c++utilities/conversion/stringbuilder.h>
7
8#include <sstream>
9
10using namespace std;
11using namespace CppUtilities;
12
13namespace TagParser {
14
19
23 void MpegAudioFrameStream::addInfo(const MpegAudioFrame &frame, AbstractTrack &track)
24{
25 track.m_version = frame.mpegVersion();
26 track.m_format = MediaFormat(GeneralMediaFormat::Mpeg1Audio, static_cast<unsigned char>(frame.layer()));
27 track.m_channelCount = frame.channelMode() == MpegChannelMode::SingleChannel ? 1 : 2;
28 track.m_channelConfig = static_cast<std::uint8_t>(frame.channelMode());
29 track.m_samplingFrequency = frame.samplingFrequency();
30}
31
32 void MpegAudioFrameStream::internalParseHeader(Diagnostics &diag, AbortableProgressFeedback &progress)
33{
34 CPP_UTILITIES_UNUSED(progress)
35
36 static const string context("parsing MPEG audio frame header");
37 if (!m_istream) {
38 throw NoDataFoundException();
39 }
40 // parse frames until the first valid, non-empty frame is reached
41 m_istream->seekg(static_cast<std::streamoff>(m_startOffset), ios_base::beg);
42 for (size_t invalidByteskipped = 0; m_frames.size() < 200 && invalidByteskipped <= 0x600u;) {
43 MpegAudioFrame &frame = invalidByteskipped > 0 ? m_frames.back() : m_frames.emplace_back();
44 try {
45 frame.parseHeader(m_reader, diag);
46 } catch (const InvalidDataException &) {
47 if (++invalidByteskipped > 1) {
48 diag.pop_back();
49 }
50 m_istream->seekg(-3, ios_base::cur);
51 continue;
52 }
53 if (invalidByteskipped > 1) {
54 diag.emplace_back(DiagLevel::Critical, argsToString("The next ", invalidByteskipped, " bytes are junk as well."), context);
55 }
56 if (!frame.size()) {
57 continue; // likely just junk, check further frames
58 }
59 invalidByteskipped = 0;
60 if (frame.isProtectedByCrc()) {
61 m_istream->seekg(2, ios_base::cur);
62 }
63 break;
64 }
65 if (!m_frames.back().isValid()) {
66 return;
67 }
68 const MpegAudioFrame &frame = m_frames.back();
69 addInfo(frame, *this);
70 if (frame.isXingBytesfieldPresent()) {
71 const auto xingSize = frame.xingBytesfield();
72 if (!m_size) {
73 m_size = xingSize;
74 } else if (xingSize != m_size) {
75 diag.emplace_back(DiagLevel::Warning,
76 argsToString("Real size of MPEG audio frames (", m_size, " byte) is not in accordance with value provided by Xing header (", xingSize,
77 " byte). The real size will be used."),
78 context);
79 }
80 }
81 if (frame.isXingFramefieldPresent()) {
82 const auto duration = static_cast<double>(frame.xingFrameCount() * frame.sampleCount()) / static_cast<double>(frame.samplingFrequency());
83 m_bitrate = static_cast<double>(m_size) / duration / 125.0;
84 m_duration = TimeSpan::fromSeconds(duration);
85 } else {
86 m_bitrate = frame.bitrate();
87 m_duration = TimeSpan::fromSeconds(static_cast<double>(m_size) / (m_bytesPerSecond = static_cast<std::uint32_t>(m_bitrate * 125)));
88 }
89}
90
91} // namespace TagParser
TagParser::AbortableProgressFeedback
The AbortableProgressFeedback class provides feedback about an ongoing operation via callbacks.
Definition progressfeedback.h:186
TagParser::AbstractTrack::m_bytesPerSecond
std::uint32_t m_bytesPerSecond
Definition abstracttrack.h:216
TagParser::AbstractTrack::m_version
double m_version
Definition abstracttrack.h:202
TagParser::AbstractTrack::m_size
std::uint64_t m_size
Definition abstracttrack.h:203
TagParser::AbstractTrack::AbstractTrack
AbstractTrack(std::istream &inputStream, std::ostream &outputStream, std::uint64_t startOffset)
Constructs a new track.
Definition abstracttrack.cpp:33
TagParser::AbstractTrack::m_channelCount
std::uint16_t m_channelCount
Definition abstracttrack.h:217
TagParser::AbstractTrack::m_channelConfig
std::uint8_t m_channelConfig
Definition abstracttrack.h:218
TagParser::AbstractTrack::m_duration
CppUtilities::TimeSpan m_duration
Definition abstracttrack.h:207
TagParser::AbstractTrack::m_reader
CppUtilities::BinaryReader m_reader
Definition abstracttrack.h:194
TagParser::AbstractTrack::m_startOffset
std::uint64_t m_startOffset
Definition abstracttrack.h:196
TagParser::AbstractTrack::duration
const CppUtilities::TimeSpan & duration() const
Returns the duration if known; otherwise returns a TimeSpan of zero ticks.
Definition abstracttrack.h:467
TagParser::AbstractTrack::m_format
MediaFormat m_format
Definition abstracttrack.h:198
TagParser::AbstractTrack::m_bitrate
double m_bitrate
Definition abstracttrack.h:208
TagParser::AbstractTrack::m_istream
std::istream * m_istream
Definition abstracttrack.h:192
TagParser::AbstractTrack::m_samplingFrequency
std::uint32_t m_samplingFrequency
Definition abstracttrack.h:213
TagParser::Diagnostics
The Diagnostics class is a container for DiagMessage.
Definition diagnostics.h:156
TagParser::InvalidDataException
The exception that is thrown when the data to be parsed or to be made seems invalid and therefore can...
Definition exceptions.h:25
TagParser::MediaFormat
The MediaFormat class specifies the format of media data.
Definition mediaformat.h:247
TagParser::MpegAudioFrameStream::addInfo
static void addInfo(const MpegAudioFrame &frame, AbstractTrack &track)
Adds the information from the specified frame to the specified track.
Definition mpegaudioframestream.cpp:23
TagParser::MpegAudioFrameStream::internalParseHeader
void internalParseHeader(Diagnostics &diag, AbortableProgressFeedback &progress) override
This method is internally called to parse header information.
Definition mpegaudioframestream.cpp:32
TagParser::MpegAudioFrame
The MpegAudioFrame class is used to parse MPEG audio frames.
Definition mpegaudioframe.h:37
TagParser::MpegAudioFrame::size
std::uint32_t size() const
Returns the size if known; otherwise returns 0.
Definition mpegaudioframe.cpp:208
TagParser::MpegAudioFrame::samplingFrequency
std::uint32_t samplingFrequency() const
Returns the sampeling frequency of the frame if known; otherwise returns 0.
Definition mpegaudioframe.cpp:122
TagParser::MpegAudioFrame::channelMode
MpegChannelMode channelMode() const
Returns the channel mode if known; otherwise returns MpegChannelMode::Unspecifed.
Definition mpegaudioframe.cpp:164
TagParser::MpegAudioFrame::isProtectedByCrc
constexpr bool isProtectedByCrc() const
Returns an indication whether the frame is protected by CRC.
Definition mpegaudioframe.h:101
TagParser::MpegAudioFrame::sampleCount
std::uint32_t sampleCount() const
Returns the sample count if known; otherwise returns 0.
Definition mpegaudioframe.cpp:185
TagParser::MpegAudioFrame::layer
int layer() const
Returns the MPEG layer if known (1, 2, or 3); otherwise returns 0.
Definition mpegaudioframe.cpp:105
TagParser::MpegAudioFrame::bitrate
std::uint16_t bitrate() const
Returns the bitrate of the frame if known; otherwise returns 0.
Definition mpegaudioframe.h:109
TagParser::MpegAudioFrame::xingFrameCount
constexpr std::uint32_t xingFrameCount() const
Returns an indication whether the Xing frame count is present.
Definition mpegaudioframe.h:207
TagParser::MpegAudioFrame::isXingBytesfieldPresent
constexpr bool isXingBytesfieldPresent() const
Returns an indication whether the Xing bytes field is present.
Definition mpegaudioframe.h:183
TagParser::MpegAudioFrame::parseHeader
void parseHeader(CppUtilities::BinaryReader &reader, Diagnostics &diag)
Parses the header read using the specified reader.
Definition mpegaudioframe.cpp:49
TagParser::MpegAudioFrame::mpegVersion
double mpegVersion() const
Returns the MPEG version if known (1.0, 2.0 or 2.5); otherwise returns 0.
Definition mpegaudioframe.cpp:88
TagParser::MpegAudioFrame::isXingFramefieldPresent
constexpr bool isXingFramefieldPresent() const
Returns an indication whether the Xing frame field is present.
Definition mpegaudioframe.h:175
TagParser::MpegAudioFrame::xingBytesfield
constexpr std::uint32_t xingBytesfield() const
Returns the Xing bytes field if known; otherwise returns 0.
Definition mpegaudioframe.h:215
TagParser::NoDataFoundException
The exception that is thrown when the data to be parsed holds no parsable information (e....
Definition exceptions.h:18
exceptions.h
mediaformat.h
mpegaudioframestream.h
CppUtilities
Definition abstractcontainer.h:16
TagParser
Contains all classes and functions of the TagInfo library.
Definition aaccodebook.h:10
TagParser::DiagLevel::Warning
@ Warning
Definition diagnostics.h:20
TagParser::DiagLevel::Critical
@ Critical
Definition diagnostics.h:21
TagParser::GeneralMediaFormat::Mpeg1Audio
@ Mpeg1Audio
Definition mediaformat.h:65
TagParser::MpegChannelMode::SingleChannel
@ SingleChannel
Definition mpegaudioframe.h:23