~ubuntu-branches/ubuntu/hardy/avidemux/hardy

« back to all changes in this revision

Viewing changes to avidemux/ADM_mplex/mpastrm_in.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Matvey Kozhev
  • Date: 2007-12-18 13:53:04 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20071218135304-cdqec2lg2bglyz15
Tags: 1:2.4~preview3-0.0ubuntu1
* Upload to Ubuntu. (LP: #163287, LP: #126572)
* debian/changelog: re-added Ubuntu releases.
* debian/control:
  - Require debhelper >= 5.0.51 (for dh_icons) and imagemagick.
  - Build-depend on libsdl1.2-dev instead of libsdl-dev.
  - Build against newer libx264-dev. (LP: #138854)
  - Removed libamrnb-dev, not in Ubuntu yet.
* debian/rules:
  - Install all icon sizes, using convert (upstream installs none).
  - Added missing calls to dh_installmenu, dh_installman, dh_icons and
    dh_desktop.
* debian/menu, debian/avidemux-qt.menu:
  - Corrected package and executable names.
* debian/avidemux-common.install: Install icons.
* debian/avidemux.common.manpages: Install man/avidemux.1.
* debian/links, debian/avidemux-cli.links, debian/avidemux-gtk.links:
  - Link manpages to avidemux.1.gz.
* debian/install, debian/avidemux-qt.install, debian/avidemux-gtk.desktop,
  debian/avidemux-qt.desktop: Install desktop files.

Show diffs side-by-side

added added

removed removed

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