12 FileAC3::FileAC3(Asset *asset, File *file)
13 : FileBase(asset, file)
23 int FileAC3::reset_parameters_derived()
30 temp_raw_allocated = 0;
32 compressed_allocated = 0;
35 void FileAC3::get_parameters(BC_WindowBase *parent_window,
37 BC_WindowBase* &format_window,
44 AC3ConfigAudio *window = new AC3ConfigAudio(parent_window, asset);
45 format_window = window;
46 window->create_objects();
52 int FileAC3::check_sig()
54 // Libmpeg3 does this in FileMPEG.
58 int FileAC3::open_file(int rd, int wr)
67 avcodec_register_all();
68 codec = avcodec_find_encoder(CODEC_ID_AC3);
72 "FileAC3::open_file codec not found.\n");
75 codec_context = avcodec_alloc_context();
76 codec_context->bit_rate = asset->ac3_bitrate * 1000;
77 codec_context->sample_rate = asset->sample_rate;
78 codec_context->channels = asset->channels;
79 if(avcodec_open(codec_context, codec))
82 "FileAC3::open_file failed to open codec.\n");
86 if(!(fd = fopen(asset->path, "w")))
88 perror("FileAC3::open_file");
94 if(!(fd = fopen(asset->path, "r")))
96 perror("FileAC3::open_file");
107 int FileAC3::close_file()
111 avcodec_close(codec_context);
128 delete [] temp_compressed;
132 FileBase::close_file();
135 // Channel conversion matrices because ffmpeg encodes a
136 // different channel order than liba52 decodes.
137 // Each row is an output channel.
138 // Each column is an input channel.
139 // static int channels5[] =
144 // static int channels6[] =
150 int FileAC3::write_samples(double **buffer, int64_t len)
152 // Convert buffer to encoder format
153 if(temp_raw_size + len > temp_raw_allocated)
155 int new_allocated = temp_raw_size + len;
156 int16_t *new_raw = new int16_t[new_allocated * asset->channels];
161 sizeof(int16_t) * temp_raw_size * asset->channels);
165 temp_raw_allocated = new_allocated;
168 // Allocate compressed data buffer
169 if(temp_raw_allocated * asset->channels * 2 > compressed_allocated)
171 compressed_allocated = temp_raw_allocated * asset->channels * 2;
172 delete [] temp_compressed;
173 temp_compressed = new unsigned char[compressed_allocated];
176 // Append buffer to temp raw
177 int16_t *out_ptr = temp_raw + temp_raw_size * asset->channels;
178 for(int i = 0; i < len; i++)
180 for(int j = 0; j < asset->channels; j++)
182 int sample = (int)(buffer[j][i] * 32767);
183 CLAMP(sample, -32768, 32767);
187 temp_raw_size += len;
189 int frame_size = codec_context->frame_size;
191 int current_sample = 0;
192 for(current_sample = 0;
193 current_sample + frame_size <= temp_raw_size;
194 current_sample += frame_size)
196 int compressed_size = avcodec_encode_audio(
198 temp_compressed + output_size,
199 compressed_allocated - output_size,
200 temp_raw + current_sample * asset->channels);
201 output_size += compressed_size;
206 temp_raw + current_sample * asset->channels,
207 (temp_raw_size - current_sample) * sizeof(int16_t) * asset->channels);
208 temp_raw_size -= current_sample;
210 int bytes_written = fwrite(temp_compressed, 1, output_size, fd);
211 if(bytes_written < output_size)
213 perror("FileAC3::write_samples");
225 AC3ConfigAudio::AC3ConfigAudio(BC_WindowBase *parent_window,
227 : BC_Window(PROGRAM_NAME ": Audio Compression",
228 parent_window->get_abs_cursor_x(1),
229 parent_window->get_abs_cursor_y(1),
231 BC_OKButton::calculate_h() + 100,
233 BC_OKButton::calculate_h() + 100,
238 this->parent_window = parent_window;
242 void AC3ConfigAudio::create_objects()
246 add_tool(new BC_Title(x, y, "Bitrate (kbps):"));
247 AC3ConfigAudioBitrate *bitrate;
249 new AC3ConfigAudioBitrate(this,
252 bitrate->create_objects();
254 add_subwindow(new BC_OKButton(this));
259 int AC3ConfigAudio::close_event()
270 AC3ConfigAudioBitrate::AC3ConfigAudioBitrate(AC3ConfigAudio *gui,
276 AC3ConfigAudioBitrate::bitrate_to_string(gui->string, gui->asset->ac3_bitrate))
281 char* AC3ConfigAudioBitrate::bitrate_to_string(char *string, int bitrate)
283 sprintf(string, "%d", bitrate);
287 void AC3ConfigAudioBitrate::create_objects()
289 add_item(new BC_MenuItem("32"));
290 add_item(new BC_MenuItem("40"));
291 add_item(new BC_MenuItem("48"));
292 add_item(new BC_MenuItem("56"));
293 add_item(new BC_MenuItem("64"));
294 add_item(new BC_MenuItem("80"));
295 add_item(new BC_MenuItem("96"));
296 add_item(new BC_MenuItem("112"));
297 add_item(new BC_MenuItem("128"));
298 add_item(new BC_MenuItem("160"));
299 add_item(new BC_MenuItem("192"));
300 add_item(new BC_MenuItem("224"));
301 add_item(new BC_MenuItem("256"));
302 add_item(new BC_MenuItem("320"));
303 add_item(new BC_MenuItem("384"));
304 add_item(new BC_MenuItem("448"));
305 add_item(new BC_MenuItem("512"));
306 add_item(new BC_MenuItem("576"));
307 add_item(new BC_MenuItem("640"));
310 int AC3ConfigAudioBitrate::handle_event()
312 gui->asset->ac3_bitrate = atol(get_text());