-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMPAFile.cpp
More file actions
149 lines (118 loc) · 4.01 KB
/
MPAFile.cpp
File metadata and controls
149 lines (118 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// GNU LESSER GENERAL PUBLIC LICENSE
// Version 3, 29 June 2007
//
// Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
//
// Everyone is permitted to copy and distribute verbatim copies of this license
// document, but changing it is not allowed.
//
// This version of the GNU Lesser General Public License incorporates the terms
// and conditions of version 3 of the GNU General Public License, supplemented
// by the additional permissions listed below.
#include "stdafx.h"
#include "MPAFile.h"
#include "MPAException.h"
#include "Platform.h"
CMPAFile::CMPAFile(LPCTSTR file, IMPAFileSystem *file_system) {
m_pStream = new CMPAFileStream(file, file_system);
m_pTags = new CTags(m_pStream);
unsigned offset{0};
// find first valid MPEG frame
m_pFirstFrame = new CMPAFrame(m_pStream, offset, true, false, false, nullptr);
// check for VBR header
m_pVBRHeader = m_pFirstFrame->FindVBRHeader();
CalcBytesPerSec();
}
// destructor
CMPAFile::~CMPAFile() {
delete m_pVBRHeader;
delete m_pFirstFrame;
delete m_pTags;
delete m_pStream;
}
constexpr unsigned MAX_EMPTY_FRAMES{25U};
// try to guess the bitrate of the whole file as good as possible
void CMPAFile::CalcBytesPerSec() {
// in case of a VBR header we know the bitrate (if at least the number of
// frames is known)
if (m_pVBRHeader && m_pVBRHeader->m_dwFrames) {
const unsigned bytes{m_pVBRHeader->m_dwBytes};
// number of bytes can be guessed
if (!bytes) m_pVBRHeader->m_dwBytes = GetFileSize();
m_dwBytesPerSec = m_pFirstFrame->m_pHeader->GetBytesPerSecond(
m_pVBRHeader->m_dwFrames, bytes);
return;
}
// otherwise we have to guess it
// go through all frames that have a lower bitrate than 48kbit
CMPAFrame* frame{m_pFirstFrame};
unsigned frames_num{0};
bool should_delete_frame{false};
while (frame && frame->m_pHeader->m_dwBitrate <= 48000U) {
frame = GetFrame(GetType::Next, frame, should_delete_frame);
if (frames_num++ > MAX_EMPTY_FRAMES) break;
should_delete_frame = true;
};
if (!frame) {
frame = m_pFirstFrame;
should_delete_frame = false;
}
m_dwBytesPerSec = frame->m_pHeader->GetBytesPerSecond();
if (should_delete_frame) delete frame;
}
// MPEG2, LayerIII, 8kbps, 24kHz => Framesize = 24 Bytes
constexpr unsigned MIN_FRAME_SIZE{24U};
CMPAFrame* CMPAFile::GetFrame(CMPAFile::GetType Type, CMPAFrame* frame,
bool should_delete_old_frame, unsigned offset) {
CMPAFrame* new_frame;
CMPAHeader* comparison_header{nullptr};
bool is_subsequent_frame{true};
bool should_reverse{false};
bool is_exact_offset{true};
switch (Type) {
case GetType::First:
offset = GetBegin();
is_subsequent_frame = true;
is_exact_offset = false;
break;
case GetType::Last:
offset = GetEnd() - MIN_FRAME_SIZE;
should_reverse = true;
is_exact_offset = false;
comparison_header = m_pFirstFrame->m_pHeader;
break;
case GetType::Next:
if (!frame) return nullptr;
comparison_header = m_pFirstFrame->m_pHeader;
offset = frame->GetSubsequentHeaderOffset();
break;
case GetType::Prev:
if (!frame) return nullptr;
offset = frame->m_dwOffset - MIN_FRAME_SIZE;
should_reverse = true;
is_exact_offset = false;
comparison_header = m_pFirstFrame->m_pHeader;
break;
case GetType::Resync:
is_subsequent_frame = true;
is_exact_offset = false;
// pCompHeader = m_pFrame->m_pHeader;
break;
default:
return nullptr;
}
try {
new_frame =
new CMPAFrame(m_pStream, offset, is_subsequent_frame, is_exact_offset,
should_reverse, comparison_header);
} catch (CMPAException& e) {
// try a complete resync from position offset
if (Type == GetType::Next) {
return GetFrame(GetType::Resync, frame, should_delete_old_frame, offset);
}
DumpSystemError(e.GetErrorDescription());
new_frame = nullptr;
}
if (frame && should_delete_old_frame) delete frame;
return new_frame;
}