2 * Copyright (C) 2006-2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 * based in parts on libfaad, Copyright (C) 2003-2005 M. Bakker,
11 /** \file aac_afh.c para_server's aac audio format handler */
20 static int aac_find_stsz(unsigned char *buf
, size_t buflen
, off_t
*skip
)
24 for (i
= 0; i
+ 16 < buflen
; i
++) {
25 unsigned char *p
= buf
+ i
;
26 unsigned sample_count
, sample_size
;
28 if (p
[0] != 's' || p
[1] != 't' || p
[2] != 's' || p
[3] != 'z')
30 PARA_DEBUG_LOG("found stsz@%d\n", i
);
32 sample_size
= aac_read_int32(buf
+ i
);
33 PARA_DEBUG_LOG("sample size: %d\n", sample_size
);
35 sample_count
= aac_read_int32(buf
+ i
);
37 PARA_DEBUG_LOG("sample count: %d\n", sample_count
);
44 static ssize_t
aac_compute_chunk_table(struct audio_format_info
*afi
,
45 unsigned char *map
, size_t numbytes
)
51 ret
= aac_find_stsz(map
, numbytes
, &skip
);
54 afi
->chunks_total
= ret
;
55 PARA_DEBUG_LOG("sz table has %lu entries\n", afi
->chunks_total
);
56 afi
->chunk_table
= para_malloc((afi
->chunks_total
+ 1) * sizeof(size_t));
57 for (i
= 1; i
<= afi
->chunks_total
; i
++) {
58 if (skip
+ 4 > numbytes
)
60 sum
+= aac_read_int32(map
+ skip
);
61 afi
->chunk_table
[i
] = sum
;
63 // if (i < 10 || i + 10 > afi->chunks_total)
64 // PARA_DEBUG_LOG("offset #%d: %zu\n", i, afi->chunk_table[i]);
69 static int aac_set_chunk_tv(struct audio_format_info
*afi
,
70 mp4AudioSpecificConfig
*mp4ASC
, long unsigned *seconds
)
72 float tmp
= mp4ASC
->sbr_present_flag
== 1? 2047 : 1023;
74 long unsigned ms
= 1000.0 * afi
->chunks_total
* tmp
75 / mp4ASC
->samplingFrequency
;
77 if (!mp4ASC
->samplingFrequency
)
79 ms
= 1000.0 * afi
->chunks_total
* tmp
/ mp4ASC
->samplingFrequency
;
81 tv_divide(afi
->chunks_total
, &total
, &afi
->chunk_tv
);
82 PARA_INFO_LOG("%luHz, %lus (%lu x %lums)\n",
83 mp4ASC
->samplingFrequency
, ms
/ 1000,
84 afi
->chunks_total
, tv2ms(&afi
->chunk_tv
));
92 * Init m4a file and write some tech data to given pointers.
94 static int aac_get_file_info(char *map
, size_t numbytes
,
95 struct audio_format_info
*afi
)
100 unsigned long rate
= 0, decoder_len
;
101 unsigned char channels
= 0;
102 mp4AudioSpecificConfig mp4ASC
;
103 NeAACDecHandle handle
= NULL
;
104 unsigned char *umap
= (unsigned char *) map
;
106 ret
= aac_find_esds(umap
, numbytes
, &skip
, &decoder_len
);
110 ret
= -E_AAC_AFH_INIT
;
111 if (NeAACDecInit(handle
, umap
+ skip
, decoder_len
, &rate
, &channels
))
115 PARA_DEBUG_LOG("rate: %lu, channels: %d\n", rate
, channels
);
117 if (NeAACDecAudioSpecificConfig(umap
+ skip
, numbytes
- skip
, &mp4ASC
))
119 if (!mp4ASC
.samplingFrequency
)
121 ret
= aac_compute_chunk_table(afi
, umap
, numbytes
);
125 ret
= aac_set_chunk_tv(afi
, &mp4ASC
, &afi
->seconds_total
);
128 ret
= aac_find_entry_point(umap
+ skip
, numbytes
- skip
, &skip
);
131 afi
->chunk_table
[0] = ret
;
132 for (i
= 1; i
<= afi
->chunks_total
; i
++)
133 afi
->chunk_table
[i
] += ret
;
134 afi
->channels
= channels
;
135 afi
->frequency
= rate
;
136 ret
= (afi
->chunk_table
[afi
->chunks_total
] - afi
->chunk_table
[0]) * 8; /* bits */
137 ret
+= (channels
* afi
->seconds_total
* 500); /* avoid rounding error */
138 afi
->bitrate
= ret
/ (channels
* afi
->seconds_total
* 1000);
139 sprintf(afi
->info_string
, "audio_file_info1:%lu x %lums, "
140 "%uHz, %d channel%s, %ukb/s\n"
141 "audio_file_info2:\n"
142 "audio_file_info3:\n",
143 afi
->chunks_total
, tv2ms(&afi
->chunk_tv
),
144 afi
->frequency
, channels
, channels
== 1? "" : "s", afi
->bitrate
146 tv_scale(20, &afi
->chunk_tv
, &afi
->eof_tv
);
150 NeAACDecClose(handle
);
154 static const char* aac_suffixes
[] = {"m4a", "mp4", NULL
};
156 * the init function of the aac audio format handler
158 * \param afh pointer to the struct to initialize
160 void aac_afh_init(struct audio_format_handler
*afh
)
162 afh
->get_file_info
= aac_get_file_info
,
163 afh
->suffixes
= aac_suffixes
;