1 #ifdef HAVE_CIN_3RDPARTY
4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5 * Copyright (C) 2003-2016 Cinelerra CV contributors
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 // work around (centos) for __STDC_CONSTANT_MACROS
34 # define INT64_MAX 9223372036854775807LL
39 #include "libavcodec/avcodec.h"
48 #include "mainerror.h"
54 FileAC3::FileAC3(Asset *asset, File *file)
55 : FileBase(asset, file)
67 int FileAC3::reset_parameters_derived()
75 temp_raw_allocated = 0;
79 void FileAC3::get_parameters(BC_WindowBase *parent_window,
80 Asset *asset, BC_WindowBase* &format_window,
81 int audio_options, int video_options, EDL *edl)
86 AC3ConfigAudio *window = new AC3ConfigAudio(parent_window, asset);
87 format_window = window;
88 window->create_objects();
94 int FileAC3::check_sig()
96 // Libmpeg3 does this in FileMPEG.
100 int64_t FileAC3::get_channel_layout(int channels)
103 case 1: return AV_CH_LAYOUT_MONO;
104 case 2: return AV_CH_LAYOUT_STEREO;
105 case 3: return AV_CH_LAYOUT_SURROUND;
106 case 4: return AV_CH_LAYOUT_QUAD;
107 case 5: return AV_CH_LAYOUT_5POINT0;
108 case 6: return AV_CH_LAYOUT_5POINT1;
109 case 8: return AV_CH_LAYOUT_7POINT1;
114 int FileAC3::open_file(int rd, int wr)
121 mpg_file = new FileMPEG(file->asset, file);
122 result = mpg_file->open_file(1, 0);
124 eprintf(_("Error while opening \"%s\" for reading. \n%m\n"), asset->path);
130 codec = avcodec_find_encoder(AV_CODEC_ID_AC3);
133 eprintf(_("FileAC3::open_file codec not found.\n"));
136 if( !result && !(fd = fopen(asset->path, "w")))
138 eprintf(_("Error while opening \"%s\" for writing. \n%m\n"), asset->path);
142 int channels = asset->channels;
143 int sample_rate = asset->sample_rate;
144 int64_t layout = get_channel_layout(channels);
145 AVChannelLayout ch_layout;
146 av_channel_layout_default(&ch_layout, channels);
147 if(!ch_layout.nb_channels) {
148 printf ("av_ch_layut_default failed! \n"); }
149 int bitrate = asset->ac3_bitrate * 1000;
150 av_init_packet(&avpkt);
151 codec_context = avcodec_alloc_context3(codec);
152 codec_context->bit_rate = bitrate;
153 codec_context->sample_rate = sample_rate;
154 codec_context->ch_layout.nb_channels = channels;
155 codec_context->ch_layout.u.mask = layout;
156 av_channel_layout_copy(&codec_context->ch_layout, &ch_layout);
157 codec_context->sample_fmt = codec->sample_fmts[0];
158 SwrContext *tmp_resample_context = NULL;
159 int ret = swr_alloc_set_opts2(&tmp_resample_context,
160 &ch_layout, codec_context->sample_fmt, sample_rate,
161 &ch_layout, AV_SAMPLE_FMT_S16, sample_rate,
163 if(ret <0) printf("swr_alloc eror: %i \n", ret );
164 if(tmp_resample_context){
165 resample_context = tmp_resample_context;
166 if(swr_init(resample_context))
168 eprintf(_("FileAC3::open_file failed to init swr.\n"));
172 if(avcodec_open2(codec_context, codec, 0))
174 eprintf(_("FileAC3::open_file failed to open codec.\n"));
177 av_channel_layout_uninit(&ch_layout);
184 int FileAC3::close_file()
194 avcodec_close(codec_context);
195 avcodec_free_context(&codec_context);
198 if( resample_context )
199 swr_free(&resample_context);
211 FileBase::close_file();
215 int FileAC3::read_samples(double *buffer, int64_t len)
217 return !mpg_file ? 0 : mpg_file->read_samples(buffer, len);
220 int FileAC3::get_index(IndexFile *index_file, MainProgressBar *progress_bar)
222 return !mpg_file ? 1 : mpg_file->get_index(index_file, progress_bar);
225 // Channel conversion matrices because ffmpeg encodes a
226 // different channel order than liba52 decodes.
227 // Each row is an output channel.
228 // Each column is an input channel.
229 // static int channels5[] =
234 // static int channels6[] =
239 int FileAC3::write_packet()
241 AVCodecContext *&avctx = codec_context;
242 int ret = avcodec_receive_packet(avctx, &avpkt);
245 if( avpkt.data && avpkt.size > 0 ) {
246 int sz = fwrite(avpkt.data, 1, avpkt.size, fd);
247 if( sz == avpkt.size ) ret = 1;
250 eprintf(_("Error while writing samples. \n%m\n"));
251 av_packet_unref(&avpkt);
253 else if( ret == AVERROR_EOF )
258 int FileAC3::encode_frame(AVFrame *frame)
260 AVCodecContext *&avctx = codec_context;
261 int ret = 0, pkts = 0;
262 for( int retry=100; --retry>=0; ) {
263 ret = avcodec_send_frame(avctx, frame);
264 if( ret >= 0 ) return pkts;
265 if( ret != AVERROR(EAGAIN) ) break;
266 if( (ret=write_packet()) < 0 ) break;
267 if( !ret ) return pkts;
271 char errmsg[BCTEXTLEN];
272 av_strerror(ret, errmsg, sizeof(errmsg));
273 fprintf(stderr, "FileAC3::encode_frame: encode failed: %s\n", errmsg);
278 int FileAC3::encode_flush()
280 AVCodecContext *&avctx = codec_context;
281 int ret = avcodec_send_frame(avctx, 0);
282 while( (ret=write_packet()) > 0 );
284 char errmsg[BCTEXTLEN];
285 av_strerror(ret, errmsg, sizeof(errmsg));
286 fprintf(stderr, "FileAC3::encode_flush: encode failed: %s\n", errmsg);
291 int FileAC3::write_samples(double **buffer, int64_t len)
293 // Convert buffer to encoder format
294 if(temp_raw_size + len > temp_raw_allocated)
296 int new_allocated = temp_raw_size + len;
297 int16_t *new_raw = new int16_t[new_allocated * asset->channels];
302 sizeof(int16_t) * temp_raw_size * asset->channels);
306 temp_raw_allocated = new_allocated;
309 // Append buffer to temp raw
310 int16_t *out_ptr = temp_raw + temp_raw_size * asset->channels;
311 for(int i = 0; i < len; i++)
313 for(int j = 0; j < asset->channels; j++)
315 int sample = (int)(buffer[j][i] * 32767);
316 CLAMP(sample, -32768, 32767);
320 temp_raw_size += len;
322 AVCodecContext *&avctx = codec_context;
323 int frame_size = avctx->frame_size;
324 int cur_sample = 0, ret = 0;
325 for(cur_sample = 0; ret >= 0 &&
326 cur_sample + frame_size <= temp_raw_size;
327 cur_sample += frame_size)
329 AVFrame *frame = av_frame_alloc();
330 frame->nb_samples = frame_size;
331 frame->format = avctx->sample_fmt;
332 av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout);
333 frame->sample_rate = avctx->sample_rate;
335 ret = av_frame_get_buffer(frame, 0);
337 const uint8_t *samples = (uint8_t *)temp_raw +
338 cur_sample * sizeof(int16_t) * asset->channels;
339 ret = swr_convert(resample_context,
340 (uint8_t **)frame->extended_data, frame_size,
341 &samples, frame_size);
342 } else { printf("fileAC3: av_frame_get_buffer failed!\n"); }
344 frame->pts = avctx->sample_rate && avctx->time_base.num ?
345 file->get_audio_position() : AV_NOPTS_VALUE ;
346 ret = encode_frame(frame);
348 av_frame_free(&frame);
353 temp_raw + cur_sample * asset->channels,
354 (temp_raw_size - cur_sample) * sizeof(int16_t) * asset->channels);
355 temp_raw_size -= cur_sample;
361 AC3ConfigAudio::AC3ConfigAudio(BC_WindowBase *parent_window,
363 : BC_Window(_(PROGRAM_NAME ": Audio Compression"),
364 parent_window->get_abs_cursor_x(1),
365 parent_window->get_abs_cursor_y(1),
366 xS(500), BC_OKButton::calculate_h() + yS(100),
367 xS(500), BC_OKButton::calculate_h() + yS(100),
370 this->parent_window = parent_window;
372 // *** CONTEXT_HELP ***
373 context_help_set_keyword("Single File Rendering");
376 void AC3ConfigAudio::create_objects()
378 int x = xS(10), y = yS(10);
380 lock_window("AC3ConfigAudio::create_objects");
381 add_tool(new BC_Title(x, y, _("Bitrate (kbps):")));
382 AC3ConfigAudioBitrate *bitrate;
383 add_tool(bitrate = new AC3ConfigAudioBitrate(this, x1, y));
384 bitrate->create_objects();
386 add_subwindow(new BC_OKButton(this));
391 int AC3ConfigAudio::close_event()
398 AC3ConfigAudioBitrate::AC3ConfigAudioBitrate(AC3ConfigAudio *gui, int x, int y)
399 : BC_PopupMenu(x, y, xS(150),
400 AC3ConfigAudioBitrate::bitrate_to_string(gui->string, gui->asset->ac3_bitrate))
405 char* AC3ConfigAudioBitrate::bitrate_to_string(char *string, int bitrate)
407 sprintf(string, "%d", bitrate);
411 void AC3ConfigAudioBitrate::create_objects()
413 add_item(new BC_MenuItem("32"));
414 add_item(new BC_MenuItem("40"));
415 add_item(new BC_MenuItem("48"));
416 add_item(new BC_MenuItem("56"));
417 add_item(new BC_MenuItem("64"));
418 add_item(new BC_MenuItem("80"));
419 add_item(new BC_MenuItem("96"));
420 add_item(new BC_MenuItem("112"));
421 add_item(new BC_MenuItem("128"));
422 add_item(new BC_MenuItem("160"));
423 add_item(new BC_MenuItem("192"));
424 add_item(new BC_MenuItem("224"));
425 add_item(new BC_MenuItem("256"));
426 add_item(new BC_MenuItem("320"));
427 add_item(new BC_MenuItem("384"));
428 add_item(new BC_MenuItem("448"));
429 add_item(new BC_MenuItem("512"));
430 add_item(new BC_MenuItem("576"));
431 add_item(new BC_MenuItem("640"));
434 int AC3ConfigAudioBitrate::handle_event()
436 gui->asset->ac3_bitrate = atol(get_text());