Tag Parser  6.5.1
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
flacmetadata.cpp
Go to the documentation of this file.
1 #include "./flacmetadata.h"
2 
3 #include "../exceptions.h"
4 #include "../tagvalue.h"
5 
6 #include <c++utilities/conversion/binaryconversion.h>
7 #include <c++utilities/io/bitreader.h>
8 #include <c++utilities/io/binaryreader.h>
9 #include <c++utilities/io/binarywriter.h>
10 
11 #include <cstring>
12 #include <memory>
13 
14 using namespace std;
15 using namespace ConversionUtilities;
16 using namespace IoUtilities;
17 
18 namespace Media {
19 
30 void FlacMetaDataBlockHeader::parseHeader(const char *buffer)
31 {
32  m_last = *buffer & 0x80;
33  m_type = *buffer & (0x80 - 1);
34  m_dataSize = BE::toUInt24(buffer + 1);
35 }
36 
41 void FlacMetaDataBlockHeader::makeHeader(std::ostream &outputStream)
42 {
43  byte buff[4];
44  *buff = (m_last ? (0x80 | m_type) : m_type);
45  BE::getBytes24(m_dataSize, reinterpret_cast<char *>(buff) + 1);
46  outputStream.write(reinterpret_cast<char *>(buff), sizeof(buff));
47 }
48 
59 void FlacMetaDataBlockStreamInfo::parse(const char *buffer)
60 {
61  BitReader reader(buffer, 0x22);
62  m_minBlockSize = reader.readBits<uint16>(16);
63  m_maxBlockSize = reader.readBits<uint16>(16);
64  m_minFrameSize = reader.readBits<uint32>(24);
65  m_maxFrameSize = reader.readBits<uint32>(24);
66  m_samplingFrequency = reader.readBits<uint32>(20);
67  m_channelCount = reader.readBits<byte>(3) + 1;
68  m_bitsPerSample = reader.readBits<byte>(5) + 1;
69  m_totalSampleCount = reader.readBits<uint64>(36);
70  memcpy(m_md5Sum, buffer + 0x22 - sizeof(m_md5Sum), sizeof(m_md5Sum));
71 }
72 
84 void FlacMetaDataBlockPicture::parse(istream &inputStream, uint32 maxSize)
85 {
86  CHECK_MAX_SIZE(32);
87  BinaryReader reader(&inputStream);
88  m_pictureType = reader.readUInt32BE();
89  uint32 size = reader.readUInt32BE();
90  CHECK_MAX_SIZE(size);
91  m_value.setMimeType(reader.readString(size));
92  size = reader.readUInt32BE();
93  CHECK_MAX_SIZE(size);
94  m_value.setDescription(reader.readString(size));
95  // skip width, height, color depth, number of colors used
96  inputStream.seekg(4 * 4, ios_base::cur);
97  size = reader.readUInt32BE();
98  CHECK_MAX_SIZE(size);
99  if(size) {
100  auto data = make_unique<char[]>(size);
101  inputStream.read(data.get(), size);
102  m_value.assignData(move(data), size, TagDataType::Picture);
103  } else {
104  m_value.clearData();
105  }
106 }
107 
112 uint32 FlacMetaDataBlockPicture::requiredSize() const
113 {
114  return 32 + m_value.mimeType().size() + m_value.description().size() + m_value.dataSize();
115 }
116 
120 void FlacMetaDataBlockPicture::make(ostream &outputStream)
121 {
122  BinaryWriter writer(&outputStream);
123  writer.writeUInt32BE(pictureType());
124  writer.writeUInt32BE(m_value.mimeType().size());
125  writer.writeString(m_value.mimeType());
126  writer.writeUInt32BE(m_value.description().size());
127  writer.writeString(m_value.description());
128  writer.writeUInt32BE(0); // skip width
129  writer.writeUInt32BE(0); // skip height
130  writer.writeUInt32BE(0); // skip color depth
131  writer.writeUInt32BE(0); // skip number of colors used
132  writer.writeUInt32BE(m_value.dataSize());
133  writer.write(value().dataPointer(), m_value.dataSize());
134 }
135 
136 
137 }
STL namespace.
Contains utility classes helping to read and write streams.
Contains all classes and functions of the TagInfo library.
Definition: exceptions.h:9
#define CHECK_MAX_SIZE(sizeDenotation)
Throws TruncatedDataException() if the specified sizeDenotation exceeds maxSize; otherwise maxSize is...
Definition: exceptions.h:70