forked from Nevcairiel/LAVFilters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitstreamParser.cpp
103 lines (83 loc) · 2.77 KB
/
BitstreamParser.cpp
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
/*
* Copyright (C) 2010-2019 Hendrik Leppkes
* http://www.1f0.de
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "stdafx.h"
#include "BitstreamParser.h"
#include "libavcodec/aac_ac3_parser.h"
#include "parser/dts.h"
CBitstreamParser::CBitstreamParser()
{
init_dts_parser((DTSParserContext **)&m_pParserContext);
Reset();
}
CBitstreamParser::~CBitstreamParser()
{
close_dts_parser((DTSParserContext **)&m_pParserContext);
}
void CBitstreamParser::Reset()
{
m_dwBlocks = 0;
m_dwFrameSize = 0;
m_dwSampleRate = 0;
m_dwSamples = 0;
m_bDTSHD = FALSE;
memset(&m_DTSHeader, 0, sizeof(m_DTSHeader));
}
HRESULT CBitstreamParser::Parse(AVCodecID codec, BYTE *pBuffer, DWORD dwSize, void *pParserContext)
{
switch (codec) {
case AV_CODEC_ID_DTS:
return ParseDTS(pBuffer, dwSize);
case AV_CODEC_ID_AC3:
case AV_CODEC_ID_EAC3:
return ParseAC3(pBuffer, dwSize, pParserContext);
case AV_CODEC_ID_TRUEHD:
return ParseTrueHD(pBuffer, dwSize);
}
return S_OK;
}
HRESULT CBitstreamParser::ParseDTS(BYTE *pBuffer, DWORD dwSize)
{
int ret = parse_dts_header((DTSParserContext *)m_pParserContext, &m_DTSHeader, pBuffer, (unsigned)dwSize);
if (ret < 0)
return E_FAIL;
m_bDTSHD = m_bDTSHD || m_DTSHeader.IsHD;
m_dwBlocks = m_DTSHeader.Blocks;
m_dwSampleRate = m_DTSHeader.SampleRate;
m_dwFrameSize = m_DTSHeader.FrameSize;
m_dwSamples = m_DTSHeader.SamplesPerBlock * m_dwBlocks;
return S_OK;
}
HRESULT CBitstreamParser::ParseAC3(BYTE *pBuffer, DWORD dwSize, void *pParserContext)
{
AACAC3ParseContext *ctx = (AACAC3ParseContext *)pParserContext;
m_dwSampleRate = ctx->sample_rate;
// E-AC3 always combines 6 blocks, resulting in 1536 samples
m_dwSamples = (ctx->codec_id == AV_CODEC_ID_EAC3) ? (6 * 256) : ctx->samples;
return S_OK;
}
HRESULT CBitstreamParser::ParseTrueHD(BYTE *pBuffer, DWORD dwSize)
{
if (AV_RB32(pBuffer + 4) == 0xf8726fba)
{
int nRatebits = pBuffer[8] >> 4;
m_dwSampleRate = (nRatebits & 8 ? 44100 : 48000) << (nRatebits & 7);
m_dwSamples = 24 * (40 << (nRatebits & 7));
}
return S_OK;
}