2 #include "funcprotos.h"
6 // Attempts to read more samples than this will crash
7 #define OUTPUT_ALLOCATION 0x100000
8 #define CLAMP(x, y, z) ((x) = ((x) < (y) ? (y) : ((x) > (z) ? (z) : (x))))
14 faacDecHandle decoder_handle
;
15 faacDecFrameInfo frame_info
;
16 faacDecConfigurationPtr decoder_config
;
18 } quicktime_mp4a_codec_t
;
25 static int delete_codec(quicktime_audio_map_t
*atrack
)
27 quicktime_mp4a_codec_t
*codec
=
28 ((quicktime_codec_t
*)atrack
->codec
)->priv
;
29 if(codec
->initialized
)
31 faacDecClose(codec
->decoder_handle
);
37 static int decode(quicktime_t
*file
,
44 quicktime_audio_map_t
*track_map
= &(file
->atracks
[track
]);
45 quicktime_trak_t
*trak
= track_map
->track
;
46 quicktime_mp4a_codec_t
*codec
= ((quicktime_codec_t
*)track_map
->codec
)->priv
;
47 int64_t current_position
= track_map
->current_position
;
48 int64_t end_position
= current_position
+ samples
;
49 quicktime_vbr_t
*vbr
= &track_map
->vbr
;
53 if(!codec
->initialized
)
55 unsigned long samplerate
= trak
->mdia
.minf
.stbl
.stsd
.table
[0].sample_rate
;
56 unsigned char channels
= track_map
->channels
;
57 quicktime_init_vbr(vbr
, channels
);
58 codec
->decoder_handle
= faacDecOpen();
59 codec
->decoder_config
= faacDecGetCurrentConfiguration(codec
->decoder_handle
);
60 codec
->decoder_config
->outputFormat
= FAAD_FMT_FLOAT
;
61 codec
->decoder_config
->defSampleRate
=
62 trak
->mdia
.minf
.stbl
.stsd
.table
[0].sample_rate
;
63 faacDecSetConfiguration(codec
->decoder_handle
, codec
->decoder_config
);
64 if(faacDecInit(codec
->decoder_handle
,
72 codec
->initialized
= 1;
75 if(quicktime_align_vbr(track_map
,
82 // Decode until buffer is full
83 while(quicktime_vbr_end(vbr
) < end_position
)
85 if(!quicktime_read_vbr(file
, track_map
))
87 faacDecFrameInfo frame_info
;
88 float *sample_buffer
= faacDecDecode(codec
->decoder_handle
,
90 quicktime_vbr_input(vbr
),
91 quicktime_vbr_input_size(vbr
));
93 * static FILE *test = 0;
94 * if(!test) test = fopen("/hmov/test.pcm", "w");
96 * for(i = 0; i < frame_info.samples; i++)
98 * int16_t sample = (int)(sample_buffer[i] * 32767);
99 * fwrite(&sample, 2, 1, test);
104 quicktime_store_vbr_float(track_map
,
106 frame_info
.samples
/ track_map
->channels
);
114 // Transfer from buffer to output
116 quicktime_copy_vbr_int16(vbr
,
123 quicktime_copy_vbr_float(vbr
,
135 void quicktime_init_codec_mp4a(quicktime_audio_map_t
*atrack
)
137 quicktime_codec_t
*codec_base
= (quicktime_codec_t
*)atrack
->codec
;
138 quicktime_mp4a_codec_t
*codec
;
139 codec_base
->priv
= calloc(1, sizeof(quicktime_mp4a_codec_t
));
140 codec_base
->delete_acodec
= delete_codec
;
141 codec_base
->decode_audio
= decode
;
142 codec_base
->fourcc
= "mp4a";
143 codec_base
->title
= "Advanced Audio Codec";
144 codec_base
->desc
= "Audio section of MPEG4 standard";
146 codec
= (quicktime_mp4a_codec_t
*)codec_base
->priv
;
147 // Default encoding parameters here