File: mpastrm_in.cpp

package info (click to toggle)
mjpegtools 1%3A2.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,460 kB
  • sloc: ansic: 60,386; cpp: 32,062; sh: 5,978; makefile: 751; asm: 103
file content (339 lines) | stat: -rw-r--r-- 9,577 bytes parent folder | download | duplicates (6)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 *  audiostrm_in.c: MPEG Audio strem class members handling scanning
 *  and buffering raw input stream.
 *
 *  Copyright (C) 2001 Andrew Stevens <andrew.stevens@philips.com>
 *
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of version 2 of the GNU General Public License
 *  as published by the Free Software Foundation.
 *
 *  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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include <config.h>
#include <math.h>
#include <stdlib.h>

#include "audiostrm.hpp"
#include "interact.hpp"
#include "multiplexor.hpp"


static const char *mpa_audio_version[4] =
{
	"2.5",
	"2.0",
	"reserved",
	"1.0"
};

static const unsigned int mpa_bitrates_kbps [4][3][16] =
{
	{ /* MPEG audio V2.5 */
		{0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,0},
		{0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0},
		{0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0}
	},
	{ /*RESERVED*/
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
	},
	{ /* MPEG audio V2 */
		{0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,0},
		{0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0},
		{0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0}
	},
	{ /* MPEG audio V1 */
		{0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,0},
		{0,32,48,56,64,80,96,112,128,160,192,224,256,320,384,0},
		{0,32,40,48,56,64,80,96,112,128,160,192,224,256,320,0}
	}

};


static const int mpa_freq_table [4][4] = 
{
	/* MPEG audio V2.5 */
	{11025,12000,8000,0},
	/* RESERVED */
	{ 0, 0, 0, 0 }, 
	/* MPEG audio V2 */
	{22050,24000, 16000,0},
	/* MPEG audio V1 */
	{44100, 48000, 32000, 0}
};

static const char mpa_stereo_mode [4][15] =
{ "stereo", "joint stereo", "dual channel", "single channel" };
static const char mpa_copyright_status [2][20] =
{ "no copyright","copyright protected" };
static const char mpa_original_bit [2][10] =
{ "copy","original" };
static const char mpa_emphasis_mode [4][20] =
{ "none", "50/15 microseconds", "reserved", "CCITT J.17" };
static const unsigned int mpa_slots [4] = {12, 144, 144, 0};
static const unsigned int mpa_samples [4] = {384, 1152, 1152, 0};



MPAStream::MPAStream(IBitStream &ibs, Multiplexor &into) : 
	AudioStream( ibs, into )
{
	for( int i = 0; i <2 ; ++i )
		num_frames[i] = size_frames[i] = 0;
}

bool MPAStream::Probe(IBitStream &bs )
{
    return bs.GetBits(11) == AUDIO_SYNCWORD;
}


/*************************************************************************
 *
 * Reads initial stream parameters and displays feedback banner to users
 *
 *************************************************************************/


void MPAStream::Init ( const int stream_num )

{
	int padding_bit;

	MuxStream::Init( AUDIO_STR_0 + stream_num, 
					 0,  // Buffer scale
					 muxinto.audio_buffer_size,
					 muxinto.vcd_zero_stuffing,
					 muxinto.buffers_in_audio,
					 muxinto.always_buffers_in_audio
		);
    mjpeg_info ("Scanning for header info: Audio stream %02x (%s)",
                AUDIO_STR_0 + stream_num,
                bs.StreamName()
                );

	/* A.Stevens 2000 - update to be compatible up to  MPEG2.5
	 */
    AU_start = bs.bitcount();
    if (bs.GetBits(11)==AUDIO_SYNCWORD)
    {
		num_syncword++;
		version_id = bs.GetBits( 2);
		layer 		= 3-bs.GetBits( 2); /* 0..2 not 1..3!! */
		protection 		= bs.Get1Bit();
		bit_rate_code	= bs.GetBits( 4);
		frequency 		= bs.GetBits( 2);
		padding_bit     = bs.Get1Bit();
		bs.Get1Bit();
		mode 		= bs.GetBits( 2);
		mode_extension 	= bs.GetBits( 2);
		copyright 		= bs.Get1Bit();
		original_copy 	= bs.Get1Bit ();
		emphasis		= bs.GetBits( 2);

		framesize =
			mpa_bitrates_kbps[version_id][layer][bit_rate_code]  * 
			mpa_slots[layer] *1000 /
			mpa_freq_table[version_id][frequency];

		size_frames[0] = framesize * ( layer == 0 ? 4 : 1);
		size_frames[1] = (framesize+1) * ( layer == 0 ? 4 : 1);
		num_frames[padding_bit]++;
        access_unit.start  = AU_start;
		access_unit.length = size_frames[padding_bit];
	  
		samples_per_second = mpa_freq_table[version_id][frequency];
		if (!samples_per_second) {
			mjpeg_error ( "Invalid frequency in MPEG Audio stream header.");
			exit(1);
		}

		/* Presentation time-stamping  */
		access_unit.PTS = static_cast<clockticks>(decoding_order) * 
			static_cast<clockticks>(mpa_samples [layer]) * 
			static_cast<clockticks>(CLOCKS)	/ samples_per_second;
		access_unit.DTS = access_unit.PTS;
		access_unit.dorder = decoding_order;
		++decoding_order;
		aunits.Append( access_unit );

    } else
    {
		mjpeg_error ( "Invalid MPEG Audio stream header.");
		exit (1);
    }


	OutputHdrInfo();
}

unsigned int MPAStream::NominalBitRate()
{ 
	return mpa_bitrates_kbps[version_id][layer][bit_rate_code]*1024;
}


unsigned int MPAStream::SizeFrame( int rate_code, int padding )
{
	return ( mpa_bitrates_kbps[version_id][layer][rate_code]  * 
		mpa_slots [layer] *1000 /
		mpa_freq_table[version_id][frequency] + padding ) * ( layer == 0 ? 4 : 1);
}

void MPAStream::FillAUbuffer(unsigned int frames_to_buffer )
{
	unsigned int padding_bit;
	last_buffered_AU += frames_to_buffer;

    if( eoscan )
        return;

    mjpeg_debug( "Scanning %d MPA frames to frame %d", 
                frames_to_buffer,
                last_buffered_AU );
	while( !bs.eos() 
           && decoding_order < last_buffered_AU 
           && !muxinto.AfterMaxPTS(access_unit.PTS) )
	{

		int skip=access_unit.length-4;
        bs.SeekFwdBits( skip );
		prev_offset = AU_start;
		AU_start = bs.bitcount();
        if( AU_start - prev_offset != access_unit.length*8 )
        {
            mjpeg_warn("Discarding incomplete final frame MPEG audio stream %02x!",
                       stream_id
                       );
            aunits.DropLast();
            --decoding_order;
            break;
        }
		/* Check we have reached the end of have  another catenated 
		   stream to process before finishing ... */
		if ( (syncword = bs.GetBits( 11))!=AUDIO_SYNCWORD )
		{
            //
            // Handle a broken last frame...
			if( !bs.eos()   )
			{
                mjpeg_warn( "Data follows end of last recogniseable MPEG audio frame - bad stream?");
                eoscan = true;
                return;
			}
            break;
		}
		// Skip version_id:2, layer:2, protection:1
		(void) bs.GetBits( 5);
		int rate_code	= bs.GetBits( 4);
		// Skip frequency
		(void) bs.GetBits( 2);

		padding_bit=bs.Get1Bit();
		access_unit.start = AU_start;
		access_unit.length = SizeFrame( rate_code, padding_bit );
		access_unit.PTS = static_cast<clockticks>(decoding_order) * static_cast<clockticks>(mpa_samples[layer]) * static_cast<clockticks>(CLOCKS)
			/ samples_per_second;
		access_unit.DTS = access_unit.PTS;
		access_unit.dorder = decoding_order;
		decoding_order++;
		aunits.Append( access_unit );
		num_frames[padding_bit]++;

		bs.GetBits( 9);
		
		num_syncword++;

		if (num_syncword >= old_frames+10 )
		{
			mjpeg_debug ("Got %d frame headers.", num_syncword);
			old_frames=num_syncword;
		
		}
	


    }
	last_buffered_AU = decoding_order;
	eoscan = bs.eos() || muxinto.AfterMaxPTS(access_unit.PTS);
}



void MPAStream::Close()
{
    stream_length = AU_start >> 3;
	mjpeg_info ("AUDIO_STATISTICS: %02x", stream_id); 
    mjpeg_info ("Audio stream length %lld bytes.", stream_length);
    mjpeg_info   ("Syncwords      : %8u",num_syncword);
    mjpeg_info   ("Frames         : %8u padded",  num_frames[0]);
    mjpeg_info   ("Frames         : %8u unpadded", num_frames[1]);
	
}

/*************************************************************************
	OutputAudioInfo
	gibt gesammelte Informationen zu den Audio Access Units aus.

	Prints information on audio access units
*************************************************************************/

void MPAStream::OutputHdrInfo ()
{
    unsigned int bitrate;
    bitrate = mpa_bitrates_kbps[version_id][layer][bit_rate_code];


	mjpeg_info("MPEG AUDIO STREAM: %02x", stream_id);
	mjpeg_info("Audio version  : %s", mpa_audio_version[version_id]);
    mjpeg_info("Layer          : %8u",layer+1);

    if (protection == 0) mjpeg_info ("CRC checksums  :      yes");
    else  mjpeg_info ("CRC checksums  :       no");

    if (bit_rate_code == 0)
		mjpeg_info ("Bit rate       :     free");
    else if (bit_rate_code == 0xf)
		mjpeg_info ("Bit rate       : reserved");
    else
		mjpeg_info ("Bit rate       : %8u bytes/sec (%3u kbit/sec)",
				bitrate*128, bitrate);

    if (frequency == 3)
		mjpeg_info ("Frequency      : reserved");
    else
		mjpeg_info ("Frequency      :     %d Hz",
				mpa_freq_table[version_id][frequency]);

    mjpeg_info   ("Mode           : %8u %s",
			  mode,mpa_stereo_mode[mode]);
    mjpeg_info   ("Mode extension : %8u",mode_extension);
    mjpeg_info   ("Copyright bit  : %8u %s",
			  copyright,mpa_copyright_status[copyright]);
    mjpeg_info   ("Original/Copy  : %8u %s",
			  original_copy,mpa_original_bit[original_copy]);
    mjpeg_info   ("Emphasis       : %8u %s",
			  emphasis,mpa_emphasis_mode[emphasis]);
}



/* 
 * Local variables:
 *  c-file-style: "stroustrup"
 *  tab-width: 4
 *  indent-tabs-mode: nil
 * End:
 */