1 /*****************************************************************
2 |
3 |    AP4 - AC-4 Utilities
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_UTILS_H_
30 #define _AP4_AC4_UTILS_H_
31 
32 /*----------------------------------------------------------------------
33 |   includes
34 +---------------------------------------------------------------------*/
35 #include "Ap4Utils.h"
36 
37 /*----------------------------------------------------------------------
38 |   constants
39 +---------------------------------------------------------------------*/
40 const unsigned int AP4_CH_MODE_LENGTH           = 16;   /* AC-4 ch_mode length  */
41 const unsigned int AP4_AC4_FRAME_RATE_NUM       = 14;   /* AC-4 frame rate number  */
42 const unsigned int AP4_AC4_SUBSTREAM_GROUP_NUM  = 3;    /* AC-4 Maximum substream group if presentation_config < 5 */
43 
44 const unsigned char
45 SUPER_SET_CH_MODE[AP4_CH_MODE_LENGTH][AP4_CH_MODE_LENGTH] =
46 {
47     {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15},
48     {1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15},
49     {2, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15},
50     {3, 3, 3, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15},
51     {4, 4, 4, 4, 4, 6, 6, 8, 8, 10,10,12,12,14,14,15},
52     {5, 5, 5, 5, 6, 5, 6, 7, 8, 9, 10,11,12,13,14,15},
53     {6, 6, 6, 6, 6, 6, 6, 6, 8, 6, 10,12,12,14,14,15},
54     {7, 7, 7, 7, 8, 7, 6, 7, 8, 9, 10,12,12,13,14,15},
55     {8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 10,11,12,14,14,15},
56     {9, 9, 9, 9, 10,9, 10,9, 9, 9, 10,11,12,13,14,15},
57     {10,10,10,10,10,10,10,10,10,10,10,10,12,13,14,15},
58     {11,11,11,11,12,11,12,11,12,11,12,11,13,13,14,15},
59     {12,12,12,12,12,12,12,12,12,12,12,12,12,13,14,15},
60     {13,13,13,13,14,13,14,13,14,13,14,13,14,13,14,15},
61     {14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,15},
62     {15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15}
63 };
64 
65 // ch_mode - TS 103 190-2 table 78
66 const int CH_MODE_MONO      = 0;
67 const int CH_MODE_STEREO    = 1;
68 const int CH_MODE_3_0       = 2;
69 const int CH_MODE_5_0       = 3;
70 const int CH_MODE_5_1       = 4;
71 const int CH_MODE_70_34     = 5;
72 const int CH_MODE_71_34     = 6;
73 const int CH_MODE_70_52     = 7;
74 const int CH_MODE_71_52     = 8;
75 const int CH_MODE_70_322    = 9;
76 const int CH_MODE_71_322    = 10;
77 const int CH_MODE_7_0_4     = 11;
78 const int CH_MODE_7_1_4     = 12;
79 const int CH_MODE_9_0_4     = 13;
80 const int CH_MODE_9_1_4     = 14;
81 const int CH_MODE_22_2      = 15;
82 const int CH_MODE_RESERVED  = 16;
83 
84 // speaker group index mask, indexed by ch_mode - TS 103 190-2 A.27
85 const int
86 AC4_SPEAKER_GROUP_INDEX_MASK_BY_CH_MODE[] =
87 {
88     2,        // 0b10 - 1.0
89     1,        // 0b01 - 2.0
90     3,        // 0b11 - 3.0
91     7,        // 0b0000111 - 5.0
92     71,       // 0b1000111 - 5.1
93     15,       // 0b0001111 - 7.0: 3/4/0
94     79,       // 0b1001111 - 7.1: 3/4/0.1
95     131079,   // 0b100000000000000111 - 7.0: 5/2/0
96     131143,   // 0b100000000001000111 - 7.1: 5/2/0.1
97     262151,   // 0b1000000000000000111 - 7.0: 3/2/2
98     262215,   // 0b1000000000001000111 - 7.1: 3/2/2.1
99     63,       // 0b0111111 - 7.0.4
100     127,      // 0b1111111 - 7.1.4
101     65599,    // 0b10000000000111111 - 9.0.4
102     65663,    // 0b10000000001111111 - 9.1.4
103     196479,   // 0b101111111101111111 - 22.2
104     0         // reserved
105 };
106 
107 const AP4_UI32
108 AP4_Ac4SamplingFrequencyTable[] =
109 {
110     44100,  // 44.1 kHz
111     48000   // 48 kHz
112 };
113 
114 const AP4_UI32
115 AP4_Ac4SampleDeltaTable[AP4_AC4_FRAME_RATE_NUM] =
116 {
117     2002,
118     2000,
119     1920,
120     8008,  // 29.97 fps, using 240 000 media time scale
121     1600,
122     1001,
123     1000,
124     960,
125     4004,  // 59.97 fps
126     800,
127     480,
128     2002,  // 119.88 fps
129     400,
130     2048  // 23.44 fps, AC-4 native frame rate
131 };
132 
133 const AP4_UI32
134 AP4_Ac4MediaTimeScaleTable[AP4_AC4_FRAME_RATE_NUM] =
135 {
136     48000,
137     48000,
138     48000,
139     240000,  //29.97 fps
140     48000,
141     48000,
142     48000,
143     48000,
144     240000,  // 59.97 fps
145     48000,
146     48000,
147     240000,  // 119.88 fps
148     48000,
149     48000,
150 };
151 
152 /*----------------------------------------------------------------------
153 |   struct
154 +---------------------------------------------------------------------*/
155 struct AP4_Ac4EmdfInfo{
156     AP4_UI08 emdf_version;
157     AP4_UI16 key_id;
158     AP4_UI08 b_emdf_payloads_substream_info;
159     AP4_UI16 substream_index;
160     AP4_UI08 protectionLengthPrimary;
161     AP4_UI08 protectionLengthSecondary;
162     AP4_UI08 protection_bits_primary[16];
163     AP4_UI08 protection_bits_Secondary[16];
164 };
165 
166 /*----------------------------------------------------------------------
167 |   non-inline functions
168 +---------------------------------------------------------------------*/
169 AP4_UI32
170 AP4_Ac4VariableBits(AP4_BitReader &data, int nBits);
171 
172 AP4_Result
173 AP4_Ac4ChannelCountFromSpeakerGroupIndexMask(unsigned int speakerGroupIndexMask);
174 
175 /*----------------------------------------------------------------------
176 |   inline functions
177 +---------------------------------------------------------------------*/
178 inline AP4_Result
AP4_Ac4SuperSet(int lvalue,int rvalue)179 AP4_Ac4SuperSet(int lvalue, int rvalue)
180 {
181     if ((lvalue == -1) || (lvalue > 15)) return rvalue;
182     if ((rvalue == -1) || (rvalue > 15)) return lvalue;
183     return SUPER_SET_CH_MODE[lvalue][rvalue];
184 }
185 
186 inline AP4_Result
Ap4_Ac4SubstreamGroupPartOfDefaultPresentation(unsigned int substreamGroupIndex,unsigned int presSubstreamGroupIndexes[],unsigned int presNSubstreamGroups)187 Ap4_Ac4SubstreamGroupPartOfDefaultPresentation(unsigned int substreamGroupIndex, unsigned int presSubstreamGroupIndexes[], unsigned int presNSubstreamGroups)
188 {
189     int partOf = false;
190     for (unsigned int idx = 0; idx < presNSubstreamGroups; idx ++) {
191         if (presSubstreamGroupIndexes[idx] == substreamGroupIndex) { partOf = true; }
192     }
193     return partOf;
194 }
195 
196 inline AP4_UI32
AP4_SetMaxGroupIndex(unsigned int groupIndex,unsigned int maxGroupIndex)197 AP4_SetMaxGroupIndex(unsigned int groupIndex, unsigned int maxGroupIndex)
198 {
199     if (groupIndex > maxGroupIndex) { maxGroupIndex = groupIndex; }
200     return maxGroupIndex;
201 }
202 
203 inline void
AP4_ByteAlign(AP4_BitWriter & bits)204 AP4_ByteAlign(AP4_BitWriter &bits)
205 {
206     unsigned int byte_align_nums = 8 - (bits.GetBitCount() % 8 == 0 ? 8: bits.GetBitCount() % 8);
207     if (byte_align_nums){ bits.Write(0, byte_align_nums); }
208 }
209 
210 inline void
Ap4_Ac4UpdatePresBytes(const unsigned char * buffer,unsigned int idx,unsigned char value)211 Ap4_Ac4UpdatePresBytes(const unsigned char* buffer, unsigned int idx, unsigned char value)
212 {    unsigned char *data = const_cast<unsigned char*>(buffer);
213     *(data + idx) = value;
214 }
215 
216 #endif // _AP4_AC4_UTILS_H_
217