1 /*****************************************************************
2  |
3  |    AP4 - AC-3 Sync Frame Parser
4  |
5  |    Copyright 2002-2020 Axiomatic Systems, LLC
6  |
7  |
8  |    This file is part of Bento4/AP4 (MP4 Atom Processing Library).
9  |
10  |    Unless you have obtained Bento4 under a difference license,
11  |    this version of Bento4 is Bento4|GPL.
12  |    Bento4|GPL is free software; you can redistribute it and/or modify
13  |    it under the terms of the GNU General Public License as published by
14  |    the Free Software Foundation; either version 2, or (at your option)
15  |    any later version.
16  |
17  |    Bento4|GPL is distributed in the hope that it will be useful,
18  |    but WITHOUT ANY WARRANTY; without even the implied warranty of
19  |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  |    GNU General Public License for more details.
21  |
22  |    You should have received a copy of the GNU General Public License
23  |    along with Bento4|GPL; see the file COPYING.  If not, write to the
24  |    Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25  |    02111-1307, USA.
26  |
27  ****************************************************************/
28 
29 #ifndef _AP4_AC3_PARSER_H_
30 #define _AP4_AC3_PARSER_H_
31 
32 /*----------------------------------------------------------------------
33  |   includes
34  +---------------------------------------------------------------------*/
35 #include "Ap4Types.h"
36 #include "Ap4BitStream.h"
37 #include "Ap4Dac3Atom.h"
38 #include "Ap4Eac3Parser.h"
39 
40 /*----------------------------------------------------------------------
41  |   constants
42  +---------------------------------------------------------------------*/
43 #define AP4_AC3_HEADER_SIZE                32      /* The header size for AC-3 parser, syncinfo() + bsi() */
44 #define AP4_AC3_SYNC_WORD_BIG_ENDIAN       0x0B77  /* Big endian, 16 sync bits */
45 #define AP4_AC3_SYNC_WORD_LITTLE_ENDIAN    0x770B  /* Little endian, 16 sync bits */
46 
47 const AP4_UI32 FRAME_SIZE_CODE_ARY_AC3[3][38] = {
48     {64,64,80,80,96,96,112,112,128,128,160,160,192,192,224,224,256,256,320,320,384,384,448,448,512,512,640,640,768,768,896,896,1024,1024,1152,1152,1280,1280},
49     {69,70,87,88,104,105,121,122,139,140,174,175,208,209,243,244,278,279,348,349,417,418,487,488,557,558696,697,835,836,976,977,1114,1115,1253,1254,1393,1394},
50     {96,96,120,120,144,144,168,168,192,192,240,240,288,288,336,336,384,384,480,480,576,576,672,672,768,768,960,960,1152,1152,1344,1344,1536,1536,1728,1728,1920,1920}};
51 const AP4_UI32 FSCOD_AC3[4] = {48000, 44100, 32000, 0};
52 
53 /*----------------------------------------------------------------------
54  |   types
55  +---------------------------------------------------------------------*/
56 class AP4_Ac3Header {
57 public:
58     // constructor
59     AP4_Ac3Header(const AP4_UI08* bytes);
60 
61     // methods
62     AP4_Result Check();
63 
64     AP4_UI32 m_HeadSize;
65     AP4_UI32 m_FrameSize;
66     AP4_UI32 m_ChannelCount;
67 
68     // AC-3 sysninfo()
69     AP4_UI32 m_Fscod;
70     AP4_UI32 m_Frmsizecod;
71 
72     // AC-3 bsi()
73     AP4_UI32 m_Bsid;
74     AP4_UI32 m_Bsmod;
75     AP4_UI32 m_Acmod;
76     AP4_UI32 m_Lfeon;
77     AP4_UI32 m_Addbsie;
78     AP4_UI32 m_Addbsil;         // 6 bits
79     AP4_UI08 m_addbsi[65];      // (addbsil+1)×8
80 
81     // class methods
82     static bool MatchFixed(AP4_Ac3Header& frame, AP4_Ac3Header& next_frame);
83 };
84 
85 typedef struct {
86     AP4_UI32  m_ChannelCount;
87     AP4_UI32  m_FrameSize;
88     AP4_UI32  m_SampleRate;
89     AP4_Dac3Atom::StreamInfo m_Ac3StreamInfo;
90 } AP4_Ac3FrameInfo;
91 
92 typedef struct {
93     AP4_BitStream*   m_Source;
94     AP4_Ac3FrameInfo m_Info;
95     AP4_Flags m_LittleEndian;
96 } AP4_Ac3Frame;
97 
98 class AP4_Ac3Parser {
99 public:
100     // constructor and destructor
101     AP4_Ac3Parser();
102     virtual ~AP4_Ac3Parser();
103 
104     // methods
105     AP4_Result Reset();
106     AP4_Result Feed(const AP4_UI08* buffer,
107                     AP4_Size*       buffer_size,
108                     AP4_Flags       flags = 0);
109     AP4_Result FindFrame(AP4_Ac3Frame& frame);
110     AP4_Result Skip(AP4_Size size);
111     AP4_Size   GetBytesFree();
112     AP4_Size   GetBytesAvailable();
113 
114 private:
115     // methods
116     AP4_Result FindHeader(AP4_UI08* header);
117 
118     // members
119     AP4_BitStream m_Bits;
120     AP4_Cardinal  m_FrameCount;
121     AP4_Flags     m_LittleEndian;
122 };
123 
124 #endif // _AP4_AC3_PARSER_H_
125