1 /*
2  *  audiostrm_in.c: MPEG Audio strem class members handling scanning
3  *  and buffering raw input stream.
4  *
5  *  Copyright (C) 2001 Andrew Stevens <andrew.stevens@philips.com>
6  *
7  *
8  *  This program is free software; you can redistribute it and/or
9  *  modify it under the terms of version 2 of the GNU General Public License
10  *  as published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  */
21 
22 #include <config.h>
23 #include <math.h>
24 #include <stdlib.h>
25 
26 #include "audiostrm.hpp"
27 #include "interact.hpp"
28 #include "multiplexor.hpp"
29 
30 
31 static const char *mpa_audio_version[4] =
32 {
33 	"2.5",
34 	"2.0",
35 	"reserved",
36 	"1.0"
37 };
38 
39 static const unsigned int mpa_bitrates_kbps [4][3][16] =
40 {
41 	{ /* MPEG audio V2.5 */
42 		{0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,0},
43 		{0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0},
44 		{0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0}
45 	},
46 	{ /*RESERVED*/
47 		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
48 		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
49 		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
50 	},
51 	{ /* MPEG audio V2 */
52 		{0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,0},
53 		{0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0},
54 		{0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0}
55 	},
56 	{ /* MPEG audio V1 */
57 		{0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,0},
58 		{0,32,48,56,64,80,96,112,128,160,192,224,256,320,384,0},
59 		{0,32,40,48,56,64,80,96,112,128,160,192,224,256,320,0}
60 	}
61 
62 };
63 
64 
65 static const int mpa_freq_table [4][4] =
66 {
67 	/* MPEG audio V2.5 */
68 	{11025,12000,8000,0},
69 	/* RESERVED */
70 	{ 0, 0, 0, 0 },
71 	/* MPEG audio V2 */
72 	{22050,24000, 16000,0},
73 	/* MPEG audio V1 */
74 	{44100, 48000, 32000, 0}
75 };
76 
77 static const char mpa_stereo_mode [4][15] =
78 { "stereo", "joint stereo", "dual channel", "single channel" };
79 static const char mpa_copyright_status [2][20] =
80 { "no copyright","copyright protected" };
81 static const char mpa_original_bit [2][10] =
82 { "copy","original" };
83 static const char mpa_emphasis_mode [4][20] =
84 { "none", "50/15 microseconds", "reserved", "CCITT J.17" };
85 static const unsigned int mpa_slots [4] = {12, 144, 144, 0};
86 static const unsigned int mpa_samples [4] = {384, 1152, 1152, 0};
87 
88 
89 
MPAStream(IBitStream & ibs,Multiplexor & into)90 MPAStream::MPAStream(IBitStream &ibs, Multiplexor &into) :
91 	AudioStream( ibs, into )
92 {
93 	for( int i = 0; i <2 ; ++i )
94 		num_frames[i] = size_frames[i] = 0;
95 }
96 
Probe(IBitStream & bs)97 bool MPAStream::Probe(IBitStream &bs )
98 {
99     return bs.GetBits(11) == AUDIO_SYNCWORD;
100 }
101 
102 
103 /*************************************************************************
104  *
105  * Reads initial stream parameters and displays feedback banner to users
106  *
107  *************************************************************************/
108 
109 
Init(const int stream_num)110 void MPAStream::Init ( const int stream_num )
111 
112 {
113 	int padding_bit;
114 
115 	MuxStream::Init( AUDIO_STR_0 + stream_num,
116 					 0,  // Buffer scale
117 					 muxinto.audio_buffer_size,
118 					 muxinto.vcd_zero_stuffing,
119 					 muxinto.buffers_in_audio,
120 					 muxinto.always_buffers_in_audio
121 		);
122     mjpeg_info ("Scanning for header info: Audio stream %02x (%s)",
123                 AUDIO_STR_0 + stream_num,
124                 bs.StreamName()
125                 );
126 
127 	/* A.Stevens 2000 - update to be compatible up to  MPEG2.5
128 	 */
129     AU_start = bs.bitcount();
130     if (bs.GetBits(11)==AUDIO_SYNCWORD)
131     {
132 		num_syncword++;
133 		version_id = bs.GetBits( 2);
134 		layer 		= 3-bs.GetBits( 2); /* 0..2 not 1..3!! */
135 		protection 		= bs.Get1Bit();
136 		bit_rate_code	= bs.GetBits( 4);
137 		frequency 		= bs.GetBits( 2);
138 		padding_bit     = bs.Get1Bit();
139 		bs.Get1Bit();
140 		mode 		= bs.GetBits( 2);
141 		mode_extension 	= bs.GetBits( 2);
142 		copyright 		= bs.Get1Bit();
143 		original_copy 	= bs.Get1Bit ();
144 		emphasis		= bs.GetBits( 2);
145 
146 		framesize =
147 			mpa_bitrates_kbps[version_id][layer][bit_rate_code]  *
148 			mpa_slots[layer] *1000 /
149 			mpa_freq_table[version_id][frequency];
150 
151 		size_frames[0] = framesize * ( layer == 0 ? 4 : 1);
152 		size_frames[1] = (framesize+1) * ( layer == 0 ? 4 : 1);
153 		num_frames[padding_bit]++;
154         access_unit.start  = AU_start;
155 		access_unit.length = size_frames[padding_bit];
156 
157 		samples_per_second = mpa_freq_table[version_id][frequency];
158 
159 		/* Presentation time-stamping  */
160 		access_unit.PTS = static_cast<clockticks>(decoding_order) *
161 			static_cast<clockticks>(mpa_samples [layer]) *
162 			static_cast<clockticks>(CLOCKS)	/ samples_per_second;
163 		access_unit.DTS = access_unit.PTS;
164 		access_unit.dorder = decoding_order;
165 		++decoding_order;
166 		aunits.Append( access_unit );
167 
168     } else
169     {
170 		mjpeg_error ( "Invalid MPEG Audio stream header.");
171 		exit (1);
172     }
173 
174 
175 	OutputHdrInfo();
176 }
177 
NominalBitRate()178 unsigned int MPAStream::NominalBitRate()
179 {
180 	return mpa_bitrates_kbps[version_id][layer][bit_rate_code]*1024;
181 }
182 
183 
SizeFrame(int rate_code,int padding)184 unsigned int MPAStream::SizeFrame( int rate_code, int padding )
185 {
186 	return ( mpa_bitrates_kbps[version_id][layer][rate_code]  *
187 		mpa_slots [layer] *1000 /
188 		mpa_freq_table[version_id][frequency] + padding ) * ( layer == 0 ? 4 : 1);
189 }
190 
FillAUbuffer(unsigned int frames_to_buffer)191 void MPAStream::FillAUbuffer(unsigned int frames_to_buffer )
192 {
193 	unsigned int padding_bit;
194 	last_buffered_AU += frames_to_buffer;
195 
196     if( eoscan )
197         return;
198 
199     mjpeg_debug( "Scanning %d MPA frames to frame %d",
200                 frames_to_buffer,
201                 last_buffered_AU );
202 	while( !bs.eos()
203            && decoding_order < last_buffered_AU
204            && !muxinto.AfterMaxPTS(access_unit.PTS) )
205 	{
206 
207 		int skip=access_unit.length-4;
208         bs.SeekFwdBits( skip );
209 		prev_offset = AU_start;
210 		AU_start = bs.bitcount();
211         if( AU_start - prev_offset != access_unit.length*8 )
212         {
213             mjpeg_warn("Discarding incomplete final frame MPEG audio stream %02x!",
214                        stream_id
215                        );
216             aunits.DropLast();
217             --decoding_order;
218             break;
219         }
220 		/* Check we have reached the end of have  another catenated
221 		   stream to process before finishing ... */
222 		if ( (syncword = bs.GetBits( 11))!=AUDIO_SYNCWORD )
223 		{
224             //
225             // Handle a broken last frame...
226 			if( !bs.eos()   )
227 			{
228                 mjpeg_warn( "Data follows end of last recogniseable MPEG audio frame - bad stream?");
229                 eoscan = true;
230                 return;
231 			}
232             break;
233 		}
234 		// Skip version_id:2, layer:2, protection:1
235 		(void) bs.GetBits( 5);
236 		int rate_code	= bs.GetBits( 4);
237 		// Skip frequency
238 		(void) bs.GetBits( 2);
239 
240 		padding_bit=bs.Get1Bit();
241 		access_unit.start = AU_start;
242 		access_unit.length = SizeFrame( rate_code, padding_bit );
243 		access_unit.PTS = static_cast<clockticks>(decoding_order) * static_cast<clockticks>(mpa_samples[layer]) * static_cast<clockticks>(CLOCKS)
244 			/ samples_per_second;
245 		access_unit.DTS = access_unit.PTS;
246 		access_unit.dorder = decoding_order;
247 		decoding_order++;
248 		aunits.Append( access_unit );
249 		num_frames[padding_bit]++;
250 
251 		bs.GetBits( 9);
252 
253 		num_syncword++;
254 
255 		if (num_syncword >= old_frames+10 )
256 		{
257 			mjpeg_debug ("Got %d frame headers.", num_syncword);
258 			old_frames=num_syncword;
259 
260 		}
261 
262 
263 
264     }
265 	last_buffered_AU = decoding_order;
266 	eoscan = bs.eos() || muxinto.AfterMaxPTS(access_unit.PTS);
267 }
268 
269 
270 
Close()271 void MPAStream::Close()
272 {
273     stream_length = AU_start >> 3;
274 	mjpeg_info ("AUDIO_STATISTICS: %02x", stream_id);
275     mjpeg_info ("Audio stream length %lld bytes.", stream_length);
276     mjpeg_info   ("Syncwords      : %8u",num_syncword);
277     mjpeg_info   ("Frames         : %8u padded",  num_frames[0]);
278     mjpeg_info   ("Frames         : %8u unpadded", num_frames[1]);
279 
280 }
281 
282 /*************************************************************************
283 	OutputAudioInfo
284 	gibt gesammelte Informationen zu den Audio Access Units aus.
285 
286 	Prints information on audio access units
287 *************************************************************************/
288 
OutputHdrInfo()289 void MPAStream::OutputHdrInfo ()
290 {
291     unsigned int bitrate;
292     bitrate = mpa_bitrates_kbps[version_id][layer][bit_rate_code];
293 
294 
295 	mjpeg_info("MPEG AUDIO STREAM: %02x", stream_id);
296 	mjpeg_info("Audio version  : %s", mpa_audio_version[version_id]);
297     mjpeg_info("Layer          : %8u",layer+1);
298 
299     if (protection == 0) mjpeg_info ("CRC checksums  :      yes");
300     else  mjpeg_info ("CRC checksums  :       no");
301 
302     if (bit_rate_code == 0)
303 		mjpeg_info ("Bit rate       :     free");
304     else if (bit_rate_code == 0xf)
305 		mjpeg_info ("Bit rate       : reserved");
306     else
307 		mjpeg_info ("Bit rate       : %8u bytes/sec (%3u kbit/sec)",
308 				bitrate*128, bitrate);
309 
310     if (frequency == 3)
311 		mjpeg_info ("Frequency      : reserved");
312     else
313 		mjpeg_info ("Frequency      :     %d Hz",
314 				mpa_freq_table[version_id][frequency]);
315 
316     mjpeg_info   ("Mode           : %8u %s",
317 			  mode,mpa_stereo_mode[mode]);
318     mjpeg_info   ("Mode extension : %8u",mode_extension);
319     mjpeg_info   ("Copyright bit  : %8u %s",
320 			  copyright,mpa_copyright_status[copyright]);
321     mjpeg_info   ("Original/Copy  : %8u %s",
322 			  original_copy,mpa_original_bit[original_copy]);
323     mjpeg_info   ("Emphasis       : %8u %s",
324 			  emphasis,mpa_emphasis_mode[emphasis]);
325 }
326 
327 
328 
329 /*
330  * Local variables:
331  *  c-file-style: "stroustrup"
332  *  tab-width: 4
333  *  indent-tabs-mode: nil
334  * End:
335  */
336