annotate Plugins/Input/aac/src/aac_utils.c @ 184:740c072f8e23 trunk

[svn] List sndfile extensions in the plugin configuration table.
author chainsaw
date Sun, 13 Nov 2005 14:27:07 -0800
parents fa848bd484d8
children 0a2ad94e8607
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
61
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
1 /*
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
2 *
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
3 * utils for AAC informations
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
4 */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
5 #include <glib.h>
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
6 #include <stdio.h>
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
7 #include <stdlib.h>
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
8
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
9
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
10 #define ADTS_HEADER_SIZE 8
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
11 #define SEEK_TABLE_CHUNK 60
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
12 #define MPEG4_TYPE 0
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
13 #define MPEG2_TYPE 1
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
14
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
15 // Read ADTS header, the file descriptor must be at
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
16 // the begining of the aac frame not at the id3tag
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
17
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
18 int getAacInfo(FILE *fd)
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
19 {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
20 unsigned char header[ADTS_HEADER_SIZE];
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
21 unsigned int id;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
22 unsigned long originPosition;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
23
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
24 originPosition = ftell(fd);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
25 if(fread(header, 1, ADTS_HEADER_SIZE, fd) != ADTS_HEADER_SIZE){
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
26 fseek(fd, originPosition, SEEK_SET);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
27 return(-1);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
28 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
29 if(!((header[0]==0xFF)&&((header[1]& 0xF6)==0xF0))){
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
30 printf("Bad header\n");
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
31 return(-1);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
32 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
33 id = header[1]&0x08;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
34 if(id==0){//MPEG-4 AAC
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
35 fseek(fd, originPosition, SEEK_SET);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
36 return(MPEG4_TYPE);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
37 }else{
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
38 fseek(fd, originPosition, SEEK_SET);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
39 return(MPEG2_TYPE);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
40 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
41 fseek(fd, originPosition, SEEK_SET);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
42 return(-1);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
43 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
44
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
45 // as AAC is VBR we need to check all ADTS header
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
46 // to enable seeking...
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
47 // there is no other solution
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
48 void checkADTSForSeeking(FILE *fd,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
49 unsigned long **seekTable,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
50 unsigned long *seekTableLength)
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
51 {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
52 unsigned long originPosition;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
53 unsigned long position;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
54 unsigned char header[ADTS_HEADER_SIZE];
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
55 unsigned int frameCount, frameLength, frameInsec;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
56 unsigned int id=0, seconds=0;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
57
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
58 originPosition = ftell(fd);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
59
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
60 for(frameCount=0,frameInsec=0;; frameCount++,frameInsec++){
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
61 position = ftell(fd);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
62 if(fread(header, 1, ADTS_HEADER_SIZE, fd)!=ADTS_HEADER_SIZE){
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
63 break;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
64 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
65 if(!strncmp(header, "ID3", 3)){
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
66 break;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
67 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
68 if(!((header[0]==0xFF)&&((header[1]& 0xF6)==0xF0))){
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
69 printf("error : Bad 1st header, file may be corrupt !\n");
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
70 break;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
71 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
72 if(!frameCount){
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
73 id=header[1]&0x08;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
74 if(((*seekTable) = malloc(SEEK_TABLE_CHUNK * sizeof(unsigned long)))==0){
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
75 printf("malloc error\n");
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
76 return;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
77 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
78 (*seekTableLength) = SEEK_TABLE_CHUNK;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
79 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
80
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
81 //if(id==0){//MPEG-4
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
82 //frameLength = ((unsigned int)header[4]<<5)|((unsigned int)header[5]>>3);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
83 //}else{//MPEG-2
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
84 frameLength = (((unsigned int)header[3]&0x3)<<11)|((unsigned int)header[4]<<3)|(header[5]>>5);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
85 //}
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
86 if(frameInsec==43){//???
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
87 frameInsec=0;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
88 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
89 if(frameInsec==0){
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
90 if(seconds == (*seekTableLength)){
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
91 (*seekTable) = realloc((*seekTable), (seconds+SEEK_TABLE_CHUNK)*sizeof(unsigned long));
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
92 (*seekTableLength) = seconds+SEEK_TABLE_CHUNK;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
93 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
94 (*seekTable)[seconds] = position;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
95 seconds++;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
96 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
97 if(fseek(fd, frameLength-ADTS_HEADER_SIZE, SEEK_CUR)==-1){
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
98 break;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
99 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
100 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
101 (*seekTableLength) = seconds;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
102 fseek(fd, originPosition, SEEK_SET);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
103 }