1 /*****************************************************************
2 |
3 |    AP4 - AC4 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_AC4_PARSER_H_
30 #define _AP4_AC4_PARSER_H_
31 
32 /*----------------------------------------------------------------------
33 |   includes
34 +---------------------------------------------------------------------*/
35 #include "Ap4Types.h"
36 #include "Ap4BitStream.h"
37 #include "Ap4Dac4Atom.h"
38 
39 /*----------------------------------------------------------------------
40 |   constants
41 +---------------------------------------------------------------------*/
42 #define AP4_AC4_HEADER_SIZE     7       /* The header size for AC-4 parser, only include (sync word + frame size) */
43 #define AP4_AC4_MAX_TOC_SIZE    512     /* Ths assumption of max toc size */
44 #define AP4_AC4_SYNC_WORD       0xAC40  /* 16 sync bits without CRC */
45 #define AP4_AC4_SYNC_WORD_CRC   0xAC41  /* 16 sync bits with    CRC */
46 
47 /*----------------------------------------------------------------------
48 |   types
49 +---------------------------------------------------------------------*/
50 class AP4_Ac4Header {
51 public:
52     // constructor
53     AP4_Ac4Header(const AP4_UI08* bytes, unsigned int size);
54 
55     // methods
56     AP4_Result Check();
57 
58     // AC-4 Sync Frame Inforamtion
59     AP4_UI32 m_SyncWord;
60     AP4_UI32 m_HeaderSize;
61     AP4_UI32 m_FrameSize;
62     AP4_UI32 m_CrcSize;
63 
64     // AC-4 Channel Count
65     AP4_UI32 m_ChannelCount;
66 
67     // Ac-4 General/High Level TOC Information
68     AP4_UI32 m_BitstreamVersion;
69     AP4_UI32 m_SequenceCounter;
70     AP4_UI32 m_BWaitFrames;
71     AP4_UI32 m_WaitFrames;
72     AP4_UI32 m_BrCode;
73     AP4_UI32 m_FsIndex;
74     AP4_UI32 m_FrameRateIndex;
75     AP4_UI32 m_BIframeGlobal;
76     AP4_UI32 m_BSinglePresentation;
77     AP4_UI32 m_BMorePresentations;
78     AP4_UI32 m_NPresentations;
79     AP4_UI32 m_BPayloadBase;
80     AP4_UI32 m_PayloadBase;
81     AP4_UI32 m_BProgramId;
82     AP4_UI32 m_ShortProgramId;
83     AP4_UI32 m_BProgramUuidPresent;
84     AP4_Byte m_ProgramUuid[16];
85 
86     static bool m_DeprecatedV0;
87 
88     // AC-4 Presentation Information
89     AP4_Dac4Atom::Ac4Dsi::PresentationV1 *m_PresentationV1;
90     // class methods
91     static bool MatchFixed(AP4_Ac4Header& frame, AP4_Ac4Header& next_frame);
92 
93 private:
94     AP4_Result GetPresentationVersionBySGIndex(unsigned int substream_group_index);
95     AP4_Result GetPresentationIndexBySGIndex  (unsigned int substream_group_index);
96 };
97 
98 typedef struct {
99     AP4_UI32  m_HeaderSize;
100     AP4_UI32  m_FrameSize;
101     AP4_UI32  m_CRCSize;
102     AP4_UI32  m_ChannelCount;
103     AP4_UI32  m_SampleDuration;
104     AP4_UI32  m_MediaTimeScale;
105     AP4_UI32  m_Iframe;
106     AP4_Dac4Atom::Ac4Dsi m_Ac4Dsi;
107 } AP4_Ac4FrameInfo;
108 
109 typedef struct {
110     AP4_BitStream*   m_Source;
111     AP4_Ac4FrameInfo m_Info;
112 } AP4_Ac4Frame;
113 
114 class AP4_Ac4Parser {
115 public:
116     // constructor and destructor
117     AP4_Ac4Parser();
118     virtual ~AP4_Ac4Parser();
119 
120     // methods
121     AP4_Result Reset();
122     AP4_Result Feed(const AP4_UI08* buffer,
123                     AP4_Size*       buffer_size,
124                     AP4_Flags       flags = 0);
125     AP4_Result FindFrame(AP4_Ac4Frame& frame);
126     AP4_Result Skip(AP4_Size size);
127     AP4_Size   GetBytesFree();
128     AP4_Size   GetBytesAvailable();
129 
130 private:
131     // methods
132     AP4_Result FindHeader(AP4_UI08* header);
133     AP4_UI32   GetSyncFrameSize(AP4_BitReader &bits);
134 
135     // members
136     AP4_BitStream m_Bits;
137     AP4_Cardinal  m_FrameCount;
138 };
139 
140 #endif // _AP4_AC4_PARSER_H_
141