4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 // work around (centos) for __STDC_CONSTANT_MACROS
33 # define INT64_MAX 9223372036854775807LL
38 #include "libavcodec/avcodec.h"
47 #include "mainerror.h"
53 FileAC3::FileAC3(Asset *asset, File *file)
54 : FileBase(asset, file)
62 if( mpg_file ) delete mpg_file;
66 int FileAC3::reset_parameters_derived()
74 temp_raw_allocated = 0;
76 compressed_allocated = 0;
80 void FileAC3::get_parameters(BC_WindowBase *parent_window,
82 BC_WindowBase* &format_window,
89 AC3ConfigAudio *window = new AC3ConfigAudio(parent_window, asset);
90 format_window = window;
91 window->create_objects();
97 int FileAC3::check_sig()
99 // Libmpeg3 does this in FileMPEG.
103 int64_t FileAC3::get_channel_layout(int channels)
106 case 1: return AV_CH_LAYOUT_MONO;
107 case 2: return AV_CH_LAYOUT_STEREO;
108 case 3: return AV_CH_LAYOUT_SURROUND;
109 case 4: return AV_CH_LAYOUT_QUAD;
110 case 5: return AV_CH_LAYOUT_5POINT0;
111 case 6: return AV_CH_LAYOUT_5POINT1;
112 case 8: return AV_CH_LAYOUT_7POINT1;
117 int FileAC3::open_file(int rd, int wr)
124 mpg_file = new FileMPEG(file->asset, file);
125 result = mpg_file->open_file(1, 0);
131 avcodec_register_all();
132 codec = avcodec_find_encoder(CODEC_ID_AC3);
135 eprintf("FileAC3::open_file codec not found.\n");
138 if( !result && !(fd = fopen(asset->path, "w")))
140 perror("FileAC3::open_file");
144 int channels = asset->channels;
145 int sample_rate = asset->sample_rate;
146 int64_t layout = get_channel_layout(channels);
147 int bitrate = asset->ac3_bitrate * 1000;
148 codec_context = avcodec_alloc_context3(codec);
149 codec_context->bit_rate = bitrate;
150 codec_context->sample_rate = sample_rate;
151 codec_context->channels = channels;
152 codec_context->channel_layout = layout;
153 codec_context->sample_fmt = codec->sample_fmts[0];
154 resample_context = swr_alloc_set_opts(NULL,
155 layout, codec_context->sample_fmt, sample_rate,
156 layout, AV_SAMPLE_FMT_S16, sample_rate,
158 swr_init(resample_context);
159 if(avcodec_open2(codec_context, codec, 0))
161 eprintf("FileAC3::open_file failed to open codec.\n");
170 int FileAC3::close_file()
179 avcodec_close(codec_context);
184 if( resample_context )
185 swr_free(&resample_context);
198 delete [] temp_compressed;
202 FileBase::close_file();
206 int FileAC3::read_samples(double *buffer, int64_t len)
208 return !mpg_file ? 0 : mpg_file->read_samples(buffer, len);
211 int FileAC3::get_index(char *index_path)
213 return !mpg_file ? 1 : mpg_file->get_index(index_path);
216 // Channel conversion matrices because ffmpeg encodes a
217 // different channel order than liba52 decodes.
218 // Each row is an output channel.
219 // Each column is an input channel.
220 // static int channels5[] =
225 // static int channels6[] =
231 int FileAC3::write_samples(double **buffer, int64_t len)
233 // Convert buffer to encoder format
234 if(temp_raw_size + len > temp_raw_allocated)
236 int new_allocated = temp_raw_size + len;
237 int16_t *new_raw = new int16_t[new_allocated * asset->channels];
242 sizeof(int16_t) * temp_raw_size * asset->channels);
246 temp_raw_allocated = new_allocated;
249 // Allocate compressed data buffer
250 if(temp_raw_allocated * asset->channels * 2 > compressed_allocated)
252 compressed_allocated = temp_raw_allocated * asset->channels * 2;
253 delete [] temp_compressed;
254 temp_compressed = new unsigned char[compressed_allocated];
257 // Append buffer to temp raw
258 int16_t *out_ptr = temp_raw + temp_raw_size * asset->channels;
259 for(int i = 0; i < len; i++)
261 for(int j = 0; j < asset->channels; j++)
263 int sample = (int)(buffer[j][i] * 32767);
264 CLAMP(sample, -32768, 32767);
268 temp_raw_size += len;
270 AVCodecContext *&avctx = codec_context;
271 int frame_size = avctx->frame_size;
272 int output_size = 0, cur_sample = 0, ret = 0;
273 for(cur_sample = 0; !ret &&
274 cur_sample + frame_size <= temp_raw_size;
275 cur_sample += frame_size)
278 av_init_packet(&avpkt);
279 avpkt.data = temp_compressed + output_size;
280 avpkt.size = compressed_allocated - output_size;
281 AVFrame *frame = av_frame_alloc();
282 frame->nb_samples = frame_size;
283 frame->format = avctx->sample_fmt;
284 frame->channel_layout = avctx->channel_layout;
285 frame->sample_rate = avctx->sample_rate;
286 ret = av_frame_get_buffer(frame, 0);
288 const uint8_t *samples = (uint8_t *)temp_raw +
289 cur_sample * sizeof(int16_t) * asset->channels;
290 ret = swr_convert(resample_context,
291 (uint8_t **)frame->extended_data, frame_size,
292 &samples, frame_size);
295 frame->pts = avctx->sample_rate && avctx->time_base.num ?
296 file->get_audio_position() : AV_NOPTS_VALUE ;
298 ret = avcodec_encode_audio2(avctx, &avpkt, frame, &got_packet);
300 char errmsg[BCSTRLEN];
301 av_strerror(ret, errmsg, sizeof(errmsg));
302 fprintf(stderr, "avcodec_encode_audio2 failed. \n%s\n", errmsg);
305 av_packet_free_side_data(&avpkt);
306 output_size += avpkt.size;
307 if(frame->extended_data != frame->data)
308 av_freep(&frame->extended_data);
309 av_frame_free(&frame);
314 temp_raw + cur_sample * asset->channels,
315 (temp_raw_size - cur_sample) * sizeof(int16_t) * asset->channels);
316 temp_raw_size -= cur_sample;
318 int bytes_written = fwrite(temp_compressed, 1, output_size, fd);
319 if(bytes_written < output_size)
321 eprintf("Error while writing samples. \n%m\n");
333 AC3ConfigAudio::AC3ConfigAudio(BC_WindowBase *parent_window,
335 : BC_Window(PROGRAM_NAME ": Audio Compression",
336 parent_window->get_abs_cursor_x(1),
337 parent_window->get_abs_cursor_y(1),
339 BC_OKButton::calculate_h() + 100,
341 BC_OKButton::calculate_h() + 100,
346 this->parent_window = parent_window;
350 void AC3ConfigAudio::create_objects()
354 lock_window("AC3ConfigAudio::create_objects");
355 add_tool(new BC_Title(x, y, "Bitrate (kbps):"));
356 AC3ConfigAudioBitrate *bitrate;
358 new AC3ConfigAudioBitrate(this,
361 bitrate->create_objects();
363 add_subwindow(new BC_OKButton(this));
368 int AC3ConfigAudio::close_event()
379 AC3ConfigAudioBitrate::AC3ConfigAudioBitrate(AC3ConfigAudio *gui,
385 AC3ConfigAudioBitrate::bitrate_to_string(gui->string, gui->asset->ac3_bitrate))
390 char* AC3ConfigAudioBitrate::bitrate_to_string(char *string, int bitrate)
392 sprintf(string, "%d", bitrate);
396 void AC3ConfigAudioBitrate::create_objects()
398 add_item(new BC_MenuItem("32"));
399 add_item(new BC_MenuItem("40"));
400 add_item(new BC_MenuItem("48"));
401 add_item(new BC_MenuItem("56"));
402 add_item(new BC_MenuItem("64"));
403 add_item(new BC_MenuItem("80"));
404 add_item(new BC_MenuItem("96"));
405 add_item(new BC_MenuItem("112"));
406 add_item(new BC_MenuItem("128"));
407 add_item(new BC_MenuItem("160"));
408 add_item(new BC_MenuItem("192"));
409 add_item(new BC_MenuItem("224"));
410 add_item(new BC_MenuItem("256"));
411 add_item(new BC_MenuItem("320"));
412 add_item(new BC_MenuItem("384"));
413 add_item(new BC_MenuItem("448"));
414 add_item(new BC_MenuItem("512"));
415 add_item(new BC_MenuItem("576"));
416 add_item(new BC_MenuItem("640"));
419 int AC3ConfigAudioBitrate::handle_event()
421 gui->asset->ac3_bitrate = atol(get_text());