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 int bitrate = asset->ac3_bitrate * 1000;
146 av_init_packet(&avpkt);
147 codec_context = avcodec_alloc_context3(codec);
148 codec_context->bit_rate = bitrate;
149 codec_context->sample_rate = sample_rate;
150 codec_context->channels = channels;
151 codec_context->channel_layout = layout;
152 codec_context->sample_fmt = codec->sample_fmts[0];
153 resample_context = swr_alloc_set_opts(NULL,
154 layout, codec_context->sample_fmt, sample_rate,
155 layout, AV_SAMPLE_FMT_S16, sample_rate,
157 swr_init(resample_context);
158 if(avcodec_open2(codec_context, codec, 0))
160 eprintf(_("FileAC3::open_file failed to open codec.\n"));
169 int FileAC3::close_file()
179 avcodec_close(codec_context);
180 avcodec_free_context(&codec_context);
183 if( resample_context )
184 swr_free(&resample_context);
196 FileBase::close_file();
200 int FileAC3::read_samples(double *buffer, int64_t len)
202 return !mpg_file ? 0 : mpg_file->read_samples(buffer, len);
205 int FileAC3::get_index(IndexFile *index_file, MainProgressBar *progress_bar)
207 return !mpg_file ? 1 : mpg_file->get_index(index_file, progress_bar);
210 // Channel conversion matrices because ffmpeg encodes a
211 // different channel order than liba52 decodes.
212 // Each row is an output channel.
213 // Each column is an input channel.
214 // static int channels5[] =
219 // static int channels6[] =
224 int FileAC3::write_packet()
226 AVCodecContext *&avctx = codec_context;
227 int ret = avcodec_receive_packet(avctx, &avpkt);
230 if( avpkt.data && avpkt.size > 0 ) {
231 int sz = fwrite(avpkt.data, 1, avpkt.size, fd);
232 if( sz == avpkt.size ) ret = 1;
235 eprintf(_("Error while writing samples. \n%m\n"));
236 av_packet_unref(&avpkt);
238 else if( ret == AVERROR_EOF )
243 int FileAC3::encode_frame(AVFrame *frame)
245 AVCodecContext *&avctx = codec_context;
246 int ret = 0, pkts = 0;
247 for( int retry=100; --retry>=0; ) {
248 ret = avcodec_send_frame(avctx, frame);
249 if( ret >= 0 ) return pkts;
250 if( ret != AVERROR(EAGAIN) ) break;
251 if( (ret=write_packet()) < 0 ) break;
252 if( !ret ) return pkts;
256 char errmsg[BCTEXTLEN];
257 av_strerror(ret, errmsg, sizeof(errmsg));
258 fprintf(stderr, "FileAC3::encode_frame: encode failed: %s\n", errmsg);
263 int FileAC3::encode_flush()
265 AVCodecContext *&avctx = codec_context;
266 int ret = avcodec_send_frame(avctx, 0);
267 while( (ret=write_packet()) > 0 );
269 char errmsg[BCTEXTLEN];
270 av_strerror(ret, errmsg, sizeof(errmsg));
271 fprintf(stderr, "FileAC3::encode_flush: encode failed: %s\n", errmsg);
276 int FileAC3::write_samples(double **buffer, int64_t len)
278 // Convert buffer to encoder format
279 if(temp_raw_size + len > temp_raw_allocated)
281 int new_allocated = temp_raw_size + len;
282 int16_t *new_raw = new int16_t[new_allocated * asset->channels];
287 sizeof(int16_t) * temp_raw_size * asset->channels);
291 temp_raw_allocated = new_allocated;
294 // Append buffer to temp raw
295 int16_t *out_ptr = temp_raw + temp_raw_size * asset->channels;
296 for(int i = 0; i < len; i++)
298 for(int j = 0; j < asset->channels; j++)
300 int sample = (int)(buffer[j][i] * 32767);
301 CLAMP(sample, -32768, 32767);
305 temp_raw_size += len;
307 AVCodecContext *&avctx = codec_context;
308 int frame_size = avctx->frame_size;
309 int cur_sample = 0, ret = 0;
310 for(cur_sample = 0; ret >= 0 &&
311 cur_sample + frame_size <= temp_raw_size;
312 cur_sample += frame_size)
314 AVFrame *frame = av_frame_alloc();
315 frame->nb_samples = frame_size;
316 frame->format = avctx->sample_fmt;
317 frame->channel_layout = avctx->channel_layout;
318 frame->sample_rate = avctx->sample_rate;
319 ret = av_frame_get_buffer(frame, 0);
321 const uint8_t *samples = (uint8_t *)temp_raw +
322 cur_sample * sizeof(int16_t) * asset->channels;
323 ret = swr_convert(resample_context,
324 (uint8_t **)frame->extended_data, frame_size,
325 &samples, frame_size);
328 frame->pts = avctx->sample_rate && avctx->time_base.num ?
329 file->get_audio_position() : AV_NOPTS_VALUE ;
330 ret = encode_frame(frame);
332 av_frame_free(&frame);
337 temp_raw + cur_sample * asset->channels,
338 (temp_raw_size - cur_sample) * sizeof(int16_t) * asset->channels);
339 temp_raw_size -= cur_sample;
345 AC3ConfigAudio::AC3ConfigAudio(BC_WindowBase *parent_window,
347 : BC_Window(_(PROGRAM_NAME ": Audio Compression"),
348 parent_window->get_abs_cursor_x(1),
349 parent_window->get_abs_cursor_y(1),
350 xS(500), BC_OKButton::calculate_h() + yS(100),
351 xS(500), BC_OKButton::calculate_h() + yS(100),
354 this->parent_window = parent_window;
356 // *** CONTEXT_HELP ***
357 context_help_set_keyword("Single File Rendering");
360 void AC3ConfigAudio::create_objects()
362 int x = xS(10), y = yS(10);
364 lock_window("AC3ConfigAudio::create_objects");
365 add_tool(new BC_Title(x, y, _("Bitrate (kbps):")));
366 AC3ConfigAudioBitrate *bitrate;
367 add_tool(bitrate = new AC3ConfigAudioBitrate(this, x1, y));
368 bitrate->create_objects();
370 add_subwindow(new BC_OKButton(this));
375 int AC3ConfigAudio::close_event()
382 AC3ConfigAudioBitrate::AC3ConfigAudioBitrate(AC3ConfigAudio *gui, int x, int y)
383 : BC_PopupMenu(x, y, xS(150),
384 AC3ConfigAudioBitrate::bitrate_to_string(gui->string, gui->asset->ac3_bitrate))
389 char* AC3ConfigAudioBitrate::bitrate_to_string(char *string, int bitrate)
391 sprintf(string, "%d", bitrate);
395 void AC3ConfigAudioBitrate::create_objects()
397 add_item(new BC_MenuItem("32"));
398 add_item(new BC_MenuItem("40"));
399 add_item(new BC_MenuItem("48"));
400 add_item(new BC_MenuItem("56"));
401 add_item(new BC_MenuItem("64"));
402 add_item(new BC_MenuItem("80"));
403 add_item(new BC_MenuItem("96"));
404 add_item(new BC_MenuItem("112"));
405 add_item(new BC_MenuItem("128"));
406 add_item(new BC_MenuItem("160"));
407 add_item(new BC_MenuItem("192"));
408 add_item(new BC_MenuItem("224"));
409 add_item(new BC_MenuItem("256"));
410 add_item(new BC_MenuItem("320"));
411 add_item(new BC_MenuItem("384"));
412 add_item(new BC_MenuItem("448"));
413 add_item(new BC_MenuItem("512"));
414 add_item(new BC_MenuItem("576"));
415 add_item(new BC_MenuItem("640"));
418 int AC3ConfigAudioBitrate::handle_event()
420 gui->asset->ac3_bitrate = atol(get_text());