annotate libmpdemux/mp3_hdr.c @ 35218:03021b062140

Fix more places that incorrectly assume that 0 can never be a valid file descriptor.
author reimar
date Wed, 31 Oct 2012 19:50:11 +0000
parents d59e91d7e4b4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
29238
d643e4643313 Add standard license header to all files in libmpdemux.
diego
parents: 17012
diff changeset
1 /*
d643e4643313 Add standard license header to all files in libmpdemux.
diego
parents: 17012
diff changeset
2 * This file is part of MPlayer.
d643e4643313 Add standard license header to all files in libmpdemux.
diego
parents: 17012
diff changeset
3 *
d643e4643313 Add standard license header to all files in libmpdemux.
diego
parents: 17012
diff changeset
4 * MPlayer is free software; you can redistribute it and/or modify
d643e4643313 Add standard license header to all files in libmpdemux.
diego
parents: 17012
diff changeset
5 * it under the terms of the GNU General Public License as published by
d643e4643313 Add standard license header to all files in libmpdemux.
diego
parents: 17012
diff changeset
6 * the Free Software Foundation; either version 2 of the License, or
d643e4643313 Add standard license header to all files in libmpdemux.
diego
parents: 17012
diff changeset
7 * (at your option) any later version.
d643e4643313 Add standard license header to all files in libmpdemux.
diego
parents: 17012
diff changeset
8 *
d643e4643313 Add standard license header to all files in libmpdemux.
diego
parents: 17012
diff changeset
9 * MPlayer is distributed in the hope that it will be useful,
d643e4643313 Add standard license header to all files in libmpdemux.
diego
parents: 17012
diff changeset
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
d643e4643313 Add standard license header to all files in libmpdemux.
diego
parents: 17012
diff changeset
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d643e4643313 Add standard license header to all files in libmpdemux.
diego
parents: 17012
diff changeset
12 * GNU General Public License for more details.
d643e4643313 Add standard license header to all files in libmpdemux.
diego
parents: 17012
diff changeset
13 *
d643e4643313 Add standard license header to all files in libmpdemux.
diego
parents: 17012
diff changeset
14 * You should have received a copy of the GNU General Public License along
d643e4643313 Add standard license header to all files in libmpdemux.
diego
parents: 17012
diff changeset
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
d643e4643313 Add standard license header to all files in libmpdemux.
diego
parents: 17012
diff changeset
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
d643e4643313 Add standard license header to all files in libmpdemux.
diego
parents: 17012
diff changeset
17 */
d643e4643313 Add standard license header to all files in libmpdemux.
diego
parents: 17012
diff changeset
18
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
19 #include <stdio.h>
34829
77fa83061e2a Make some tables const.
reimar
parents: 34828
diff changeset
20 #include <stdint.h>
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
21
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
22 #include "config.h"
17012
6ff3379a0862 Unify include path handling, -I.. is in CFLAGS.
diego
parents: 16162
diff changeset
23 #include "mp_msg.h"
30554
321e9ea69b9f #include corresponding .h files in .c files.
diego
parents: 29263
diff changeset
24 #include "mp3_hdr.h"
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
25
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
26 //----------------------- mp3 audio frame header parser -----------------------
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
27
34829
77fa83061e2a Make some tables const.
reimar
parents: 34828
diff changeset
28 static const uint16_t tabsel_123[2][3][16] = {
7910
5a276890cec4 check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents: 6763
diff changeset
29 { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,0},
5a276890cec4 check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents: 6763
diff changeset
30 {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,0},
5a276890cec4 check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents: 6763
diff changeset
31 {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,0} },
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
32
7910
5a276890cec4 check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents: 6763
diff changeset
33 { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,0},
5a276890cec4 check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents: 6763
diff changeset
34 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0},
5a276890cec4 check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents: 6763
diff changeset
35 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,0} }
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
36 };
10369
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
37
34832
010b217fb8cd Cosmetics: fix up indentations, get rid of a few lost tabs.
reimar
parents: 34831
diff changeset
38 static const int freqs[9] = { 44100, 48000, 32000, // MPEG 1.0
010b217fb8cd Cosmetics: fix up indentations, get rid of a few lost tabs.
reimar
parents: 34831
diff changeset
39 22050, 24000, 16000, // MPEG 2.0
010b217fb8cd Cosmetics: fix up indentations, get rid of a few lost tabs.
reimar
parents: 34831
diff changeset
40 11025, 12000, 8000}; // MPEG 2.5
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
41
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
42 /*
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
43 * return frame size or -1 (bad frame)
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
44 */
16162
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15251
diff changeset
45 int mp_get_mp3_header(unsigned char* hbuf,int* chans, int* srate, int* spf, int* mpa_layer, int* br){
34835
d59e91d7e4b4 Remove unused code from mp_get_mp3_header.
reimar
parents: 34832
diff changeset
46 int stereo,lsf,framesize,padding,bitrate_index,sampling_frequency, divisor;
16162
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15251
diff changeset
47 int bitrate;
34829
77fa83061e2a Make some tables const.
reimar
parents: 34828
diff changeset
48 int layer;
77fa83061e2a Make some tables const.
reimar
parents: 34828
diff changeset
49 static const int mult[3] = { 12000, 144000, 144000 };
34830
6e2c850035fd Use more appropriate types.
reimar
parents: 34829
diff changeset
50 uint32_t newhead =
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
51 hbuf[0] << 24 |
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
52 hbuf[1] << 16 |
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
53 hbuf[2] << 8 |
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
54 hbuf[3];
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
55
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
56 // head_check:
10369
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
57 if( (newhead & 0xffe00000) != 0xffe00000 ){
34832
010b217fb8cd Cosmetics: fix up indentations, get rid of a few lost tabs.
reimar
parents: 34831
diff changeset
58 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"head_check failed\n");
010b217fb8cd Cosmetics: fix up indentations, get rid of a few lost tabs.
reimar
parents: 34831
diff changeset
59 return -1;
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
60 }
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
61
15039
c9e2d75a9013 added support for mpa layers 1 and 2
nicodvb
parents: 10370
diff changeset
62 layer = 4-((newhead>>17)&3);
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 29238
diff changeset
63 if(layer==4){
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 29238
diff changeset
64 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"not layer-1/2/3\n");
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents: 2589
diff changeset
65 return -1;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents: 2589
diff changeset
66 }
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
67
34831
1ec773d51090 Some minor simplifications.
reimar
parents: 34830
diff changeset
68 sampling_frequency = (newhead>>10)&0x3; // valid: 0..2
10369
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
69 if(sampling_frequency==3){
34832
010b217fb8cd Cosmetics: fix up indentations, get rid of a few lost tabs.
reimar
parents: 34831
diff changeset
70 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"invalid sampling_frequency\n");
010b217fb8cd Cosmetics: fix up indentations, get rid of a few lost tabs.
reimar
parents: 34831
diff changeset
71 return -1;
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
72 }
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
73
34830
6e2c850035fd Use more appropriate types.
reimar
parents: 34829
diff changeset
74 if( newhead & (1<<20) ) {
10369
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
75 // MPEG 1.0 (lsf==0) or MPEG 2.0 (lsf==1)
34831
1ec773d51090 Some minor simplifications.
reimar
parents: 34830
diff changeset
76 lsf = !(newhead & (1<<19));
1ec773d51090 Some minor simplifications.
reimar
parents: 34830
diff changeset
77 sampling_frequency += lsf*3;
10369
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
78 } else {
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
79 // MPEG 2.5
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
80 lsf = 1;
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
81 sampling_frequency += 6;
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
82 }
b9fdaecc672f better checks
arpi
parents: 7910
diff changeset
83
34831
1ec773d51090 Some minor simplifications.
reimar
parents: 34830
diff changeset
84 bitrate_index = (newhead>>12)&0xf; // valid: 1..14
1ec773d51090 Some minor simplifications.
reimar
parents: 34830
diff changeset
85 padding = (newhead>>9)&0x1;
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
86
34831
1ec773d51090 Some minor simplifications.
reimar
parents: 34830
diff changeset
87 stereo = ( ((newhead>>6)&0x3) == 3) ? 1 : 2;
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
88
16162
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15251
diff changeset
89 bitrate = tabsel_123[lsf][layer-1][bitrate_index];
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15251
diff changeset
90 framesize = bitrate * mult[layer-1];
7910
5a276890cec4 check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents: 6763
diff changeset
91
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 29238
diff changeset
92 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"FRAMESIZE: %d, layer: %d, bitrate: %d, mult: %d\n",
34832
010b217fb8cd Cosmetics: fix up indentations, get rid of a few lost tabs.
reimar
parents: 34831
diff changeset
93 framesize, layer, tabsel_123[lsf][layer-1][bitrate_index], mult[layer-1]);
7910
5a276890cec4 check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents: 6763
diff changeset
94 if(!framesize){
34832
010b217fb8cd Cosmetics: fix up indentations, get rid of a few lost tabs.
reimar
parents: 34831
diff changeset
95 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"invalid framesize/bitrate_index\n");
010b217fb8cd Cosmetics: fix up indentations, get rid of a few lost tabs.
reimar
parents: 34831
diff changeset
96 return -1;
7910
5a276890cec4 check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents: 6763
diff changeset
97 }
5a276890cec4 check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents: 6763
diff changeset
98
34831
1ec773d51090 Some minor simplifications.
reimar
parents: 34830
diff changeset
99 divisor = layer == 3 ? (freqs[sampling_frequency] << lsf) : freqs[sampling_frequency];
15251
c39173a67cbb wrong framesize calculation for layers 1 and 2 with lsf set
nicodvb
parents: 15199
diff changeset
100 framesize /= divisor;
34831
1ec773d51090 Some minor simplifications.
reimar
parents: 34830
diff changeset
101 framesize += padding;
15039
c9e2d75a9013 added support for mpa layers 1 and 2
nicodvb
parents: 10370
diff changeset
102 if(layer==1)
34831
1ec773d51090 Some minor simplifications.
reimar
parents: 34830
diff changeset
103 framesize *= 4;
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
104
34828
becaa4804918 Return a spf value even when srate is NULL.
reimar
parents: 30732
diff changeset
105 if(srate)
15199
980910eb6f0c assign correct tag, dwScale and dwBlockAlign to mpeg audio; optionally assign layer and samples_per_frame when parsing mpa header
nicodvb
parents: 15039
diff changeset
106 *srate = freqs[sampling_frequency];
34832
010b217fb8cd Cosmetics: fix up indentations, get rid of a few lost tabs.
reimar
parents: 34831
diff changeset
107 if(spf) {
010b217fb8cd Cosmetics: fix up indentations, get rid of a few lost tabs.
reimar
parents: 34831
diff changeset
108 if(layer == 1)
010b217fb8cd Cosmetics: fix up indentations, get rid of a few lost tabs.
reimar
parents: 34831
diff changeset
109 *spf = 384;
010b217fb8cd Cosmetics: fix up indentations, get rid of a few lost tabs.
reimar
parents: 34831
diff changeset
110 else if(layer == 2)
010b217fb8cd Cosmetics: fix up indentations, get rid of a few lost tabs.
reimar
parents: 34831
diff changeset
111 *spf = 1152;
010b217fb8cd Cosmetics: fix up indentations, get rid of a few lost tabs.
reimar
parents: 34831
diff changeset
112 else if(sampling_frequency > 2) // not 1.0
010b217fb8cd Cosmetics: fix up indentations, get rid of a few lost tabs.
reimar
parents: 34831
diff changeset
113 *spf = 576;
010b217fb8cd Cosmetics: fix up indentations, get rid of a few lost tabs.
reimar
parents: 34831
diff changeset
114 else
010b217fb8cd Cosmetics: fix up indentations, get rid of a few lost tabs.
reimar
parents: 34831
diff changeset
115 *spf = 1152;
010b217fb8cd Cosmetics: fix up indentations, get rid of a few lost tabs.
reimar
parents: 34831
diff changeset
116 }
15199
980910eb6f0c assign correct tag, dwScale and dwBlockAlign to mpeg audio; optionally assign layer and samples_per_frame when parsing mpa header
nicodvb
parents: 15039
diff changeset
117 if(mpa_layer) *mpa_layer = layer;
6763
e29f95ed5d36 Fix vbr muxing and win32 codec crash on init
albeu
parents: 5806
diff changeset
118 if(chans) *chans = stereo;
16162
b5c2254d13f8 set i_bps in demux_audio for WAV and MP3 to avoid division by zero before
reimar
parents: 15251
diff changeset
119 if(br) *br = bitrate;
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
120
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
121 return framesize;
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
122 }