annotate libmpdemux/mp3_hdr.c @ 8556:ecff1745d189

Document things from the start. Inspired by a patch from Thomas Jarosch <tomj@simonv.com>.
author diego
date Tue, 24 Dec 2002 18:41:20 +0000
parents 5a276890cec4
children b9fdaecc672f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
1 #include <stdio.h>
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
2
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
3 #include "config.h"
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents: 2589
diff changeset
4 #include "../mp_msg.h"
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
5
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
6 //----------------------- mp3 audio frame header parser -----------------------
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
7
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
8 static int 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
9 { {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
10 {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
11 {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
12
7910
5a276890cec4 check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents: 6763
diff changeset
13 { {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
14 {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
15 {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
16 };
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
17 static long freqs[9] = { 44100, 48000, 32000, 22050, 24000, 16000 , 11025 , 12000 , 8000 };
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
18
5806
ef85f6de947c mp_mp3_get_lsf() added - to get decomp. frame size
arpi
parents: 4694
diff changeset
19 int mp_mp3_get_lsf(unsigned char* hbuf){
ef85f6de947c mp_mp3_get_lsf() added - to get decomp. frame size
arpi
parents: 4694
diff changeset
20 unsigned long newhead =
ef85f6de947c mp_mp3_get_lsf() added - to get decomp. frame size
arpi
parents: 4694
diff changeset
21 hbuf[0] << 24 |
ef85f6de947c mp_mp3_get_lsf() added - to get decomp. frame size
arpi
parents: 4694
diff changeset
22 hbuf[1] << 16 |
ef85f6de947c mp_mp3_get_lsf() added - to get decomp. frame size
arpi
parents: 4694
diff changeset
23 hbuf[2] << 8 |
ef85f6de947c mp_mp3_get_lsf() added - to get decomp. frame size
arpi
parents: 4694
diff changeset
24 hbuf[3];
ef85f6de947c mp_mp3_get_lsf() added - to get decomp. frame size
arpi
parents: 4694
diff changeset
25 if( newhead & ((long)1<<20) ) {
ef85f6de947c mp_mp3_get_lsf() added - to get decomp. frame size
arpi
parents: 4694
diff changeset
26 return (newhead & ((long)1<<19)) ? 0x0 : 0x1;
ef85f6de947c mp_mp3_get_lsf() added - to get decomp. frame size
arpi
parents: 4694
diff changeset
27 }
ef85f6de947c mp_mp3_get_lsf() added - to get decomp. frame size
arpi
parents: 4694
diff changeset
28 return 1;
ef85f6de947c mp_mp3_get_lsf() added - to get decomp. frame size
arpi
parents: 4694
diff changeset
29 }
ef85f6de947c mp_mp3_get_lsf() added - to get decomp. frame size
arpi
parents: 4694
diff changeset
30
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
31 /*
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
32 * return frame size or -1 (bad frame)
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
33 */
6763
e29f95ed5d36 Fix vbr muxing and win32 codec crash on init
albeu
parents: 5806
diff changeset
34 int mp_get_mp3_header(unsigned char* hbuf,int* chans, int* srate){
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
35 int stereo,ssize,crc,lsf,mpeg25,framesize,padding,bitrate_index,sampling_frequency;
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
36 unsigned long newhead =
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
37 hbuf[0] << 24 |
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
38 hbuf[1] << 16 |
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
39 hbuf[2] << 8 |
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
40 hbuf[3];
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
41
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
42 // printf("head=0x%08X\n",newhead);
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
43
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
44 #if 1
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
45 // head_check:
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
46 if( (newhead & 0xffe00000) != 0xffe00000 ||
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
47 (newhead & 0x0000fc00) == 0x0000fc00){
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents: 2589
diff changeset
48 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"head_check failed\n");
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
49 return -1;
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
50 }
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
51 #endif
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
52
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents: 2589
diff changeset
53 if((4-((newhead>>17)&3))!=3){
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents: 2589
diff changeset
54 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"not layer-3\n");
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents: 2589
diff changeset
55 return -1;
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents: 2589
diff changeset
56 }
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
57
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
58 if( newhead & ((long)1<<20) ) {
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
59 lsf = (newhead & ((long)1<<19)) ? 0x0 : 0x1;
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
60 mpeg25 = 0;
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
61 } else {
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
62 lsf = 1;
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
63 mpeg25 = 1;
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
64 }
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
65
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
66 if(mpeg25)
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
67 sampling_frequency = 6 + ((newhead>>10)&0x3);
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
68 else
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
69 sampling_frequency = ((newhead>>10)&0x3) + (lsf*3);
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
70
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
71 if(sampling_frequency>8){
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents: 2589
diff changeset
72 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"invalid sampling_frequency\n");
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
73 return -1; // valid: 0..8
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
74 }
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
75
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
76 crc = ((newhead>>16)&0x1)^0x1;
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
77 bitrate_index = ((newhead>>12)&0xf);
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
78 padding = ((newhead>>9)&0x1);
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
79 // fr->extension = ((newhead>>8)&0x1);
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
80 // fr->mode = ((newhead>>6)&0x3);
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
81 // fr->mode_ext = ((newhead>>4)&0x3);
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
82 // fr->copyright = ((newhead>>3)&0x1);
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
83 // fr->original = ((newhead>>2)&0x1);
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
84 // fr->emphasis = newhead & 0x3;
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
85
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
86 stereo = ( (((newhead>>6)&0x3)) == 3) ? 1 : 2;
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
87
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
88 if(!bitrate_index){
4694
a21735031d6a Audio file demuxer. Extended version for demuxer info.
albeu
parents: 2589
diff changeset
89 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"Free format not supported.\n");
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
90 return -1;
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
91 }
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
92
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
93 if(lsf)
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
94 ssize = (stereo == 1) ? 9 : 17;
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
95 else
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
96 ssize = (stereo == 1) ? 17 : 32;
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
97 if(crc) ssize += 2;
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
98
7910
5a276890cec4 check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents: 6763
diff changeset
99 framesize = tabsel_123[lsf][2][bitrate_index] * 144000;
5a276890cec4 check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents: 6763
diff changeset
100
5a276890cec4 check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents: 6763
diff changeset
101 if(!framesize){
5a276890cec4 check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents: 6763
diff changeset
102 mp_msg(MSGT_DEMUXER,MSGL_DBG2,"invalid framesize/bitrate_index\n");
5a276890cec4 check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents: 6763
diff changeset
103 return -1; // valid: 1..14
5a276890cec4 check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents: 6763
diff changeset
104 }
5a276890cec4 check for framesize validity, return -1 (error) for zero size (bug found by pl)
arpi
parents: 6763
diff changeset
105
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
106 framesize /= freqs[sampling_frequency]<<lsf;
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
107 framesize += padding;
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
108
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
109 // if(framesize<=0 || framesize>MAXFRAMESIZE) return FALSE;
6763
e29f95ed5d36 Fix vbr muxing and win32 codec crash on init
albeu
parents: 5806
diff changeset
110 if(srate) *srate = freqs[sampling_frequency];
e29f95ed5d36 Fix vbr muxing and win32 codec crash on init
albeu
parents: 5806
diff changeset
111 if(chans) *chans = stereo;
2588
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
112
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
113 return framesize;
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
114 }
19731609ea59 mp3 header parser
arpi
parents:
diff changeset
115