Tag Parser  7.0.0
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
flacstream.cpp
Go to the documentation of this file.
1 #include "./flacstream.h"
2 #include "./flacmetadata.h"
3 
4 #include "../vorbis/vorbiscomment.h"
5 
6 #include "../exceptions.h"
7 #include "../mediafileinfo.h"
8 #include "../mediaformat.h"
9 
10 #include "resources/config.h"
11 
12 #include <c++utilities/io/copy.h>
13 
14 #include <sstream>
15 
16 using namespace std;
17 using namespace IoUtilities;
18 using namespace ConversionUtilities;
19 using namespace ChronoUtilities;
20 
21 namespace TagParser {
22 
33 FlacStream::FlacStream(MediaFileInfo &mediaFileInfo, uint64 startOffset)
34  : AbstractTrack(mediaFileInfo.stream(), startOffset)
35  , m_mediaFileInfo(mediaFileInfo)
36  , m_paddingSize(0)
37  , m_streamOffset(0)
38 {
40 }
41 
47 {
48  if (!m_vorbisComment) {
49  m_vorbisComment = make_unique<VorbisComment>();
50  }
51  return m_vorbisComment.get();
52 }
53 
59 {
60  if (!m_vorbisComment) {
61  return false;
62  }
63  m_vorbisComment.reset();
64  return true;
65 }
66 
68 {
69  static const string context("parsing raw FLAC header");
70  if (!m_istream) {
71  throw NoDataFoundException();
72  }
73 
74  m_istream->seekg(m_startOffset, ios_base::beg);
75  char buffer[0x22];
76 
77  // check signature
78  if (m_reader.readUInt32BE() != 0x664C6143) {
79  diag.emplace_back(DiagLevel::Critical, "Signature (fLaC) not found.", context);
80  throw InvalidDataException();
81  }
83 
84  // parse meta data blocks
85  for (FlacMetaDataBlockHeader header; !header.isLast();) {
86  // parse block header
87  m_istream->read(buffer, 4);
88  header.parseHeader(buffer);
89 
90  // remember start offset
91  const auto startOffset = m_istream->tellg();
92 
93  // parse relevant meta data
94  switch (static_cast<FlacMetaDataBlockType>(header.type())) {
96  if (header.dataSize() >= 0x22) {
97  m_istream->read(buffer, 0x22);
98  FlacMetaDataBlockStreamInfo streamInfo;
99  streamInfo.parse(buffer);
100  m_channelCount = streamInfo.channelCount();
101  m_samplingFrequency = streamInfo.samplingFrequency();
102  m_sampleCount = streamInfo.totalSampleCount();
103  m_bitsPerSample = streamInfo.bitsPerSample();
104  m_duration = TimeSpan::fromSeconds(static_cast<double>(m_sampleCount) / m_samplingFrequency);
105  } else {
106  diag.emplace_back(DiagLevel::Critical, "\"METADATA_BLOCK_STREAMINFO\" is truncated and will be ignored.", context);
107  }
108  break;
109 
111  // parse Vorbis comment
112  // if more than one comment exist, simply thread those comments as one
113  if (!m_vorbisComment) {
114  m_vorbisComment = make_unique<VorbisComment>();
115  }
116  try {
117  m_vorbisComment->parse(*m_istream, header.dataSize(), VorbisCommentFlags::NoSignature | VorbisCommentFlags::NoFramingByte, diag);
118  } catch (const Failure &) {
119  // error is logged via notifications, just continue with the next metadata block
120  }
121  break;
122 
124  try {
125  // parse the cover
126  VorbisCommentField coverField;
127  coverField.setId(m_vorbisComment->fieldId(KnownField::Cover));
128  FlacMetaDataBlockPicture picture(coverField.value());
129  picture.parse(*m_istream, header.dataSize());
130  coverField.setTypeInfo(picture.pictureType());
131 
132  if (coverField.value().isEmpty()) {
133  diag.emplace_back(DiagLevel::Warning, "\"METADATA_BLOCK_PICTURE\" contains no picture.", context);
134  } else {
135  // add the cover to the Vorbis comment
136  if (!m_vorbisComment) {
137  // create one if none exists yet
138  m_vorbisComment = make_unique<VorbisComment>();
139  m_vorbisComment->setVendor(TagValue(APP_NAME " v" APP_VERSION, TagTextEncoding::Utf8));
140  }
141  m_vorbisComment->fields().insert(make_pair(coverField.id(), move(coverField)));
142  }
143 
144  } catch (const TruncatedDataException &) {
145  diag.emplace_back(DiagLevel::Critical, "\"METADATA_BLOCK_PICTURE\" is truncated and will be ignored.", context);
146  }
147  break;
148 
150  m_paddingSize += 4 + header.dataSize();
151  break;
152 
153  default:;
154  }
155 
156  // seek to next block
157  m_istream->seekg(startOffset + static_cast<decltype(startOffset)>(header.dataSize()));
158 
159  // TODO: check first FLAC frame
160  }
161 
162  m_streamOffset = m_istream->tellg();
163 }
164 
176 uint32 FlacStream::makeHeader(ostream &outputStream, Diagnostics &diag)
177 {
178  istream &originalStream = m_mediaFileInfo.stream();
179  originalStream.seekg(m_startOffset + 4);
180  CopyHelper<512> copy;
181 
182  // write signature
183  BE::getBytes(static_cast<uint32>(0x664C6143u), copy.buffer());
184  outputStream.write(copy.buffer(), 4);
185 
186  uint32 lastStartOffset = 0;
187 
188  // write meta data blocks which don't need to be adjusted
189  for (FlacMetaDataBlockHeader header; !header.isLast();) {
190  // parse block header
191  m_istream->read(copy.buffer(), 4);
192  header.parseHeader(copy.buffer());
193 
194  // parse relevant meta data
195  switch (static_cast<FlacMetaDataBlockType>(header.type())) {
199  m_istream->seekg(header.dataSize(), ios_base::cur);
200  break; // written separately/ignored
201  default:
202  m_istream->seekg(-4, ios_base::cur);
203  lastStartOffset = outputStream.tellp();
204  copy.copy(originalStream, outputStream, 4 + header.dataSize());
205  }
206  }
207 
208  // write Vorbis comment
209  if (!m_vorbisComment) {
210  return lastStartOffset;
211  }
212  // leave 4 bytes space for the "METADATA_BLOCK_HEADER"
213  lastStartOffset = outputStream.tellp();
214  outputStream.write(copy.buffer(), 4);
215 
216  // determine cover ID since covers must be written separately
217  const auto coverId = m_vorbisComment->fieldId(KnownField::Cover);
218 
219  // write Vorbis comment
221 
222  // write "METADATA_BLOCK_HEADER"
223  const uint32 endOffset = outputStream.tellp();
226  header.setDataSize(endOffset - lastStartOffset - 4);
227  header.setLast(!m_vorbisComment->hasField(coverId));
228  outputStream.seekp(lastStartOffset);
229  header.makeHeader(outputStream);
230  outputStream.seekp(endOffset);
231 
232  // write cover fields separately as "METADATA_BLOCK_PICTURE"
233  if (header.isLast()) {
234  return lastStartOffset;
235  }
237  const auto coverFields = m_vorbisComment->fields().equal_range(coverId);
238  for (auto i = coverFields.first; i != coverFields.second;) {
239  lastStartOffset = outputStream.tellp();
240  FlacMetaDataBlockPicture pictureBlock(i->second.value());
241  pictureBlock.setPictureType(i->second.typeInfo());
242  header.setDataSize(pictureBlock.requiredSize());
243  header.setLast(++i == coverFields.second);
244  header.makeHeader(outputStream);
245  pictureBlock.make(outputStream);
246  }
247 
248  return lastStartOffset;
249 }
250 
255 void FlacStream::makePadding(ostream &stream, uint32 size, bool isLast, Diagnostics &diag)
256 {
257  // make header
260  header.setLast(isLast);
261  header.setDataSize(size -= 4);
262  header.makeHeader(stream);
263 
264  // write zeroes
265  for (; size; --size) {
266  stream.put(0);
267  }
268 }
269 
270 } // namespace TagParser
IoUtilities::BinaryReader m_reader
void setTypeInfo(const TypeInfoType &typeInfo)
Sets the type info of the current TagField.
bool removeVorbisComment()
Removes the assigned Vorbis comment if one is assigned; does nothing otherwise.
Definition: flacstream.cpp:58
void parse(std::istream &inputStream, uint32 maxSize)
Parses the FLAC "METADATA_BLOCK_PICTURE".
byte isLast() const
Returns whether this is the last metadata block before the audio blocks.
Definition: flacmetadata.h:63
std::ostream & outputStream()
Returns the associated output stream.
STL namespace.
uint32 samplingFrequency() const
Returns the sampling frequency in Hz.
Definition: flacmetadata.h:198
void parse(const char *buffer)
Parses the FLAC "METADATA_BLOCK_STREAMINFO" which is read using the specified iterator.
void setDataSize(uint32 dataSize)
Sets the length in bytes of the meta data (excluding the size of the header itself).
Definition: flacmetadata.h:105
uint64 totalSampleCount() const
Returns the total samples in stream.
Definition: flacmetadata.h:233
bool isEmpty() const
Returns an indication whether an value is assigned.
Definition: tagvalue.h:367
void setPictureType(uint32 pictureType)
Sets the picture type according to the ID3v2 APIC frame.
Definition: flacmetadata.h:290
byte channelCount() const
Returns the number of channels.
Definition: flacmetadata.h:208
uint32 makeHeader(std::ostream &stream, Diagnostics &diag)
Writes the FLAC metadata header to the specified outputStream.
Definition: flacstream.cpp:176
Contains utility classes helping to read and write streams.
void makeHeader(std::ostream &outputStream)
Writes the header to the specified outputStream.
uint64 startOffset() const
Returns the start offset of the track in the associated stream.
ChronoUtilities::TimeSpan m_duration
void internalParseHeader(Diagnostics &diag) override
This method is internally called to parse header information.
Definition: flacstream.cpp:67
uint64 size() const
Returns the size in bytes if known; otherwise returns 0.
VorbisComment * createVorbisComment()
Creates a new Vorbis comment for the stream.
Definition: flacstream.cpp:46
std::istream * m_istream
const IdentifierType & id() const
Returns the id of the current TagField.
byte bitsPerSample() const
Returns the bits per sample.
Definition: flacmetadata.h:220
void setType(FlacMetaDataBlockType type)
Sets the block type.
Definition: flacmetadata.h:88
static void makePadding(std::ostream &stream, uint32 size, bool isLast, Diagnostics &diag)
Writes padding of the specified size to the specified stream.
Definition: flacstream.cpp:255
void setLast(byte last)
Sets whether this is the last metadata block before the audio blocks.
Definition: flacmetadata.h:71
IoUtilities::NativeFileStream & stream()
Returns the std::fstream for the current instance.
Definition: basicfileinfo.h:79
void setId(const IdentifierType &id)
Sets the id of the current Tag Field.
TagValue & value()
Returns the value of the current TagField.