r499: This commit was manufactured by cvs2svn to create tag 'r1_2_1-last'.
[cinelerra_cv/mob.git] / hvirtual / quicktime / mp4a.c
blob66322ec5ed8cb305e48edb755135f97ee1805ef0
1 #include <faad.h>
2 #include "funcprotos.h"
3 #include "quicktime.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))))
11 typedef struct
13 // Decoder objects
14 faacDecHandle decoder_handle;
15 faacDecFrameInfo frame_info;
16 faacDecConfigurationPtr decoder_config;
17 int initialized;
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);
34 free(codec);
37 static int decode(quicktime_t *file,
38 int16_t *output_i,
39 float *output_f,
40 long samples,
41 int track,
42 int channel)
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;
52 // Initialize decoder
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,
67 &samplerate,
68 &channels) < 0)
70 return 1;
72 codec->initialized = 1;
75 if(quicktime_align_vbr(track_map,
76 samples))
78 return 1;
80 else
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,
89 &frame_info,
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");
95 * int i;
96 * for(i = 0; i < frame_info.samples; i++)
97 * {
98 * int16_t sample = (int)(sample_buffer[i] * 32767);
99 * fwrite(&sample, 2, 1, test);
101 * fflush(test);
104 quicktime_store_vbr_float(track_map,
105 sample_buffer,
106 frame_info.samples / track_map->channels);
108 else
110 break;
114 // Transfer from buffer to output
115 if(output_i)
116 quicktime_copy_vbr_int16(vbr,
117 current_position,
118 samples,
119 output_i,
120 channel);
121 else
122 if(output_f)
123 quicktime_copy_vbr_float(vbr,
124 current_position,
125 samples,
126 output_f,
127 channel);
129 return 0;
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