1 #ifdef HAVE_CIN_3RDPARTY
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)
66 int FileAC3::reset_parameters_derived()
74 temp_raw_allocated = 0;
78 void FileAC3::get_parameters(BC_WindowBase *parent_window,
79 Asset *asset, BC_WindowBase* &format_window,
80 int audio_options, int video_options, EDL *edl)
85 AC3ConfigAudio *window = new AC3ConfigAudio(parent_window, asset);
86 format_window = window;
87 window->create_objects();
93 int FileAC3::check_sig()
95 // Libmpeg3 does this in FileMPEG.
99 int64_t FileAC3::get_channel_layout(int channels)
102 case 1: return AV_CH_LAYOUT_MONO;
103 case 2: return AV_CH_LAYOUT_STEREO;
104 case 3: return AV_CH_LAYOUT_SURROUND;
105 case 4: return AV_CH_LAYOUT_QUAD;
106 case 5: return AV_CH_LAYOUT_5POINT0;
107 case 6: return AV_CH_LAYOUT_5POINT1;
108 case 8: return AV_CH_LAYOUT_7POINT1;
113 int FileAC3::open_file(int rd, int wr)
120 mpg_file = new FileMPEG(file->asset, file);
121 result = mpg_file->open_file(1, 0);
123 eprintf(_("Error while opening \"%s\" for reading. \n%m\n"), asset->path);
129 codec = avcodec_find_encoder(AV_CODEC_ID_AC3);
132 eprintf(_("FileAC3::open_file codec not found.\n"));
135 if( !result && !(fd = fopen(asset->path, "w")))
137 eprintf(_("Error while opening \"%s\" for writing. \n%m\n"), asset->path);
141 int channels = asset->channels;
142 int sample_rate = asset->sample_rate;
143 int64_t layout = get_channel_layout(channels);
144 int bitrate = asset->ac3_bitrate * 1000;
145 av_init_packet(&avpkt);
146 codec_context = avcodec_alloc_context3(codec);
147 codec_context->bit_rate = bitrate;
148 codec_context->sample_rate = sample_rate;
149 codec_context->channels = channels;
150 codec_context->channel_layout = layout;
151 codec_context->sample_fmt = codec->sample_fmts[0];
152 resample_context = swr_alloc_set_opts(NULL,
153 layout, codec_context->sample_fmt, sample_rate,
154 layout, AV_SAMPLE_FMT_S16, sample_rate,
156 swr_init(resample_context);
157 if(avcodec_open2(codec_context, codec, 0))
159 eprintf(_("FileAC3::open_file failed to open codec.\n"));
168 int FileAC3::close_file()
178 avcodec_close(codec_context);
179 avcodec_free_context(&codec_context);
182 if( resample_context )
183 swr_free(&resample_context);
195 FileBase::close_file();
199 int FileAC3::read_samples(double *buffer, int64_t len)
201 return !mpg_file ? 0 : mpg_file->read_samples(buffer, len);
204 int FileAC3::get_index(IndexFile *index_file, MainProgressBar *progress_bar)
206 return !mpg_file ? 1 : mpg_file->get_index(index_file, progress_bar);
209 // Channel conversion matrices because ffmpeg encodes a
210 // different channel order than liba52 decodes.
211 // Each row is an output channel.
212 // Each column is an input channel.
213 // static int channels5[] =
218 // static int channels6[] =
223 int FileAC3::write_packet()
225 AVCodecContext *&avctx = codec_context;
226 int ret = avcodec_receive_packet(avctx, &avpkt);
229 if( avpkt.data && avpkt.size > 0 ) {
230 int sz = fwrite(avpkt.data, 1, avpkt.size, fd);
231 if( sz == avpkt.size ) ret = 1;
234 eprintf(_("Error while writing samples. \n%m\n"));
235 av_packet_unref(&avpkt);
237 else if( ret == AVERROR_EOF )
242 int FileAC3::encode_frame(AVFrame *frame)
244 AVCodecContext *&avctx = codec_context;
245 int ret = 0, pkts = 0;
246 for( int retry=100; --retry>=0; ) {
247 ret = avcodec_send_frame(avctx, frame);
248 if( ret >= 0 ) return pkts;
249 if( ret != AVERROR(EAGAIN) ) break;
250 if( (ret=write_packet()) < 0 ) break;
251 if( !ret ) return pkts;
255 char errmsg[BCTEXTLEN];
256 av_strerror(ret, errmsg, sizeof(errmsg));
257 fprintf(stderr, "FileAC3::encode_frame: encode failed: %s\n", errmsg);
262 int FileAC3::encode_flush()
264 AVCodecContext *&avctx = codec_context;
265 int ret = avcodec_send_frame(avctx, 0);
266 while( (ret=write_packet()) > 0 );
268 char errmsg[BCTEXTLEN];
269 av_strerror(ret, errmsg, sizeof(errmsg));
270 fprintf(stderr, "FileAC3::encode_flush: encode failed: %s\n", errmsg);
275 int FileAC3::write_samples(double **buffer, int64_t len)
277 // Convert buffer to encoder format
278 if(temp_raw_size + len > temp_raw_allocated)
280 int new_allocated = temp_raw_size + len;
281 int16_t *new_raw = new int16_t[new_allocated * asset->channels];
286 sizeof(int16_t) * temp_raw_size * asset->channels);
290 temp_raw_allocated = new_allocated;
293 // Append buffer to temp raw
294 int16_t *out_ptr = temp_raw + temp_raw_size * asset->channels;
295 for(int i = 0; i < len; i++)
297 for(int j = 0; j < asset->channels; j++)
299 int sample = (int)(buffer[j][i] * 32767);
300 CLAMP(sample, -32768, 32767);
304 temp_raw_size += len;
306 AVCodecContext *&avctx = codec_context;
307 int frame_size = avctx->frame_size;
308 int cur_sample = 0, ret = 0;
309 for(cur_sample = 0; ret >= 0 &&
310 cur_sample + frame_size <= temp_raw_size;
311 cur_sample += frame_size)
313 AVFrame *frame = av_frame_alloc();
314 frame->nb_samples = frame_size;
315 frame->format = avctx->sample_fmt;
316 frame->channel_layout = avctx->channel_layout;
317 frame->sample_rate = avctx->sample_rate;
318 ret = av_frame_get_buffer(frame, 0);
320 const uint8_t *samples = (uint8_t *)temp_raw +
321 cur_sample * sizeof(int16_t) * asset->channels;
322 ret = swr_convert(resample_context,
323 (uint8_t **)frame->extended_data, frame_size,
324 &samples, frame_size);
327 frame->pts = avctx->sample_rate && avctx->time_base.num ?
328 file->get_audio_position() : AV_NOPTS_VALUE ;
329 ret = encode_frame(frame);
331 av_frame_free(&frame);
336 temp_raw + cur_sample * asset->channels,
337 (temp_raw_size - cur_sample) * sizeof(int16_t) * asset->channels);
338 temp_raw_size -= cur_sample;
344 AC3ConfigAudio::AC3ConfigAudio(BC_WindowBase *parent_window,
346 : BC_Window(_(PROGRAM_NAME ": Audio Compression"),
347 parent_window->get_abs_cursor_x(1),
348 parent_window->get_abs_cursor_y(1),
349 xS(500), BC_OKButton::calculate_h() + yS(100),
350 xS(500), BC_OKButton::calculate_h() + yS(100),
353 this->parent_window = parent_window;
357 void AC3ConfigAudio::create_objects()
359 int x = xS(10), y = yS(10);
361 lock_window("AC3ConfigAudio::create_objects");
362 add_tool(new BC_Title(x, y, _("Bitrate (kbps):")));
363 AC3ConfigAudioBitrate *bitrate;
364 add_tool(bitrate = new AC3ConfigAudioBitrate(this, x1, y));
365 bitrate->create_objects();
367 add_subwindow(new BC_OKButton(this));
372 int AC3ConfigAudio::close_event()
379 AC3ConfigAudioBitrate::AC3ConfigAudioBitrate(AC3ConfigAudio *gui, int x, int y)
380 : BC_PopupMenu(x, y, xS(150),
381 AC3ConfigAudioBitrate::bitrate_to_string(gui->string, gui->asset->ac3_bitrate))
386 char* AC3ConfigAudioBitrate::bitrate_to_string(char *string, int bitrate)
388 sprintf(string, "%d", bitrate);
392 void AC3ConfigAudioBitrate::create_objects()
394 add_item(new BC_MenuItem("32"));
395 add_item(new BC_MenuItem("40"));
396 add_item(new BC_MenuItem("48"));
397 add_item(new BC_MenuItem("56"));
398 add_item(new BC_MenuItem("64"));
399 add_item(new BC_MenuItem("80"));
400 add_item(new BC_MenuItem("96"));
401 add_item(new BC_MenuItem("112"));
402 add_item(new BC_MenuItem("128"));
403 add_item(new BC_MenuItem("160"));
404 add_item(new BC_MenuItem("192"));
405 add_item(new BC_MenuItem("224"));
406 add_item(new BC_MenuItem("256"));
407 add_item(new BC_MenuItem("320"));
408 add_item(new BC_MenuItem("384"));
409 add_item(new BC_MenuItem("448"));
410 add_item(new BC_MenuItem("512"));
411 add_item(new BC_MenuItem("576"));
412 add_item(new BC_MenuItem("640"));
415 int AC3ConfigAudioBitrate::handle_event()
417 gui->asset->ac3_bitrate = atol(get_text());