7 // work around for __STDC_CONSTANT_MACROS
11 #include "bcwindowbase.h"
12 #include "bitspopup.h"
18 #include "fileffmpeg.h"
19 #include "filesystem.h"
20 #include "indexfile.h"
21 #include "interlacemodes.h"
23 #include "mainerror.h"
24 #include "mainprogress.h"
26 #include "preferences.h"
27 #include "videodevice.inc"
33 FileFFMPEG::FileFFMPEG(Asset *asset, File *file)
34 : FileBase(asset, file)
37 if(asset->format == FILE_UNKNOWN)
38 asset->format = FILE_FFMPEG;
41 FileFFMPEG::~FileFFMPEG()
47 FFMpegConfigNum::FFMpegConfigNum(BC_Window *window,
48 int x, int y, char *title_text, int *output)
49 : BC_TumbleTextBox(window, *output, -1, INT_MAX, xS(100), y, xS(100))
51 this->window = window;
52 this->x = x; this->y = y;
53 this->title_text = title_text;
54 this->output = output;
57 FFMpegConfigNum::~FFMpegConfigNum()
61 void FFMpegConfigNum::create_objects()
63 window->add_subwindow(title = new BC_Title(x, y, title_text));
64 BC_TumbleTextBox::create_objects();
67 int FFMpegConfigNum::update_param(const char *param, const char *opts)
70 if( !FFMPEG::get_ff_option(param, opts, value) ) {
71 if( (*output = atoi(value)) < 0 ) {
76 BC_TumbleTextBox::update((int64_t)*output);
81 int FFMpegConfigNum::handle_event()
83 *output = atoi(get_text());
87 FFMpegAudioNum::FFMpegAudioNum(BC_Window *window,
88 int x, int y, char *title_text, int *output)
89 : FFMpegConfigNum(window, x, y, title_text, output)
93 int FFMpegAudioBitrate::handle_event()
95 int ret = FFMpegAudioNum::handle_event();
96 Asset *asset = window()->asset;
97 if( asset->ff_audio_bitrate > 0 )
98 window()->quality->disable();
99 else if( !window()->quality->get_textbox()->is_hidden() )
100 window()->quality->enable();
104 int FFMpegAudioQuality::handle_event()
106 int ret = FFMpegAudioNum::handle_event();
107 Asset *asset = window()->asset;
108 if( asset->ff_audio_quality >= 0 )
109 window()->bitrate->disable();
110 else if( !window()->bitrate->get_textbox()->is_hidden() )
111 window()->bitrate->enable();
115 FFMpegVideoNum::FFMpegVideoNum(BC_Window *window,
116 int x, int y, char *title_text, int *output)
117 : FFMpegConfigNum(window, x, y, title_text, output)
121 int FFMpegVideoBitrate::handle_event()
123 int ret = FFMpegVideoNum::handle_event();
124 Asset *asset = window()->asset;
125 if( asset->ff_video_bitrate > 0 )
126 window()->quality->disable();
127 else if( !window()->quality->get_textbox()->is_hidden() )
128 window()->quality->enable();
132 int FFMpegVideoQuality::handle_event()
134 int ret = FFMpegVideoNum::handle_event();
135 Asset *asset = window()->asset;
136 if( asset->ff_video_quality >= 0 )
137 window()->bitrate->disable();
138 else if( !window()->bitrate->get_textbox()->is_hidden() )
139 window()->bitrate->enable();
143 FFMpegPixelFormat::FFMpegPixelFormat(FFMPEGConfigVideo *vid_config,
144 int x, int y, int w, int list_h)
145 : BC_PopupTextBox(vid_config, 0, 0, x, y, w, list_h)
147 this->vid_config = vid_config;
150 int FFMpegPixelFormat::handle_event()
152 strncpy(vid_config->asset->ff_pixel_format, get_text(),
153 sizeof(vid_config->asset->ff_pixel_format));
157 void FFMpegPixelFormat::update_formats()
159 pixfmts.remove_all_objects();
160 char video_codec[BCSTRLEN]; video_codec[0] = 0;
161 const char *vcodec = vid_config->asset->vcodec;
162 AVCodec *av_codec = !FFMPEG::get_codec(video_codec, "video", vcodec) ?
163 avcodec_find_encoder_by_name(video_codec) : 0;
164 const AVPixelFormat *pix_fmts = av_codec ? av_codec->pix_fmts : 0;
166 for( int i=0; pix_fmts[i]>=0; ++i ) {
167 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmts[i]);
168 if( desc ) pixfmts.append(new BC_ListBoxItem(desc->name));
171 update_list(&pixfmts);
174 FFMpegSampleFormat::FFMpegSampleFormat(FFMPEGConfigAudio *aud_config,
175 int x, int y, int w, int list_h)
176 : BC_PopupTextBox(aud_config, 0, 0, x, y, w, list_h)
178 this->aud_config = aud_config;
181 int FFMpegSampleFormat::handle_event()
183 strncpy(aud_config->asset->ff_sample_format, get_text(),
184 sizeof(aud_config->asset->ff_sample_format));
188 void FFMpegSampleFormat::update_formats()
190 samplefmts.remove_all_objects();
191 char audio_codec[BCSTRLEN]; audio_codec[0] = 0;
192 const char *acodec = aud_config->asset->acodec;
193 AVCodec *av_codec = !FFMPEG::get_codec(audio_codec, "audio", acodec) ?
194 avcodec_find_encoder_by_name(audio_codec) : 0;
195 const AVSampleFormat *sample_fmts = av_codec ? av_codec->sample_fmts : 0;
197 for( int i=0; sample_fmts[i]>=0; ++i ) {
198 const char *name = av_get_sample_fmt_name(sample_fmts[i]);
199 if( name ) samplefmts.append(new BC_ListBoxItem(name));
202 update_list(&samplefmts);
205 void FileFFMPEG::set_options(char *cp, int len, const char *bp)
207 char *ep = cp + len-2, ch = 0;
208 while( cp < ep && *bp != 0 ) { ch = *bp++; *cp++ = ch; }
209 if( ch != '\n' ) *cp++ = '\n';
213 void FileFFMPEG::get_parameters(BC_WindowBase *parent_window,
214 Asset *asset, BC_WindowBase *&format_window,
215 int audio_options, int video_options, EDL *edl)
217 Asset *ff_asset = new Asset();
218 ff_asset->copy_from(asset, 0);
220 parent_window->get_pop_cursor(wx, wy);
221 if( audio_options ) {
222 FFMPEGConfigAudio *window = new FFMPEGConfigAudio(parent_window,
223 wx, wy, ff_asset, edl);
224 format_window = window;
225 window->create_objects();
226 if( !window->run_window() ) {
227 asset->copy_from(ff_asset,0);
228 set_options(asset->ff_audio_options,
229 sizeof(asset->ff_audio_options),
230 window->audio_options->get_text());
234 else if( video_options ) {
235 FFMPEGConfigVideo *window = new FFMPEGConfigVideo(parent_window,
236 wx, wy, ff_asset, edl);
237 format_window = window;
238 window->create_objects();
239 if( !window->run_window() ) {
240 asset->copy_from(ff_asset,0);
241 set_options(asset->ff_video_options,
242 sizeof(asset->ff_video_options),
243 window->video_options->get_text());
247 ff_asset->remove_user();
250 int FileFFMPEG::check_sig(Asset *asset)
252 char *ptr = strstr(asset->path, ".pcm");
254 ptr = strstr(asset->path, ".raw");
258 int ret = !ffmpeg.init_decoder(asset->path) &&
259 !ffmpeg.open_decoder() ? 1 : 0;
263 void FileFFMPEG::get_info(char *path, char *text, int len)
267 cp += sprintf(cp, _("file path: %s\n"), path);
270 if( stat(path, &st) < 0 ) {
271 cp += sprintf(cp, _(" err: %s\n"), strerror(errno));
275 cp += sprintf(cp, _(" %jd bytes\n"), st.st_size);
277 if( !ret ) ret = ffmpeg.init_decoder(path);
278 if( !ret ) ret = ffmpeg.open_decoder();
280 cp += sprintf(cp, _("info:\n"));
281 ffmpeg.info(cp, len-(cp-text));
284 sprintf(cp, _("== open failed\n"));
287 int FileFFMPEG::get_video_info(int track, int &pid, double &framerate,
288 int &width, int &height, char *title)
291 pid = ff->ff_video_pid(track);
292 framerate = ff->ff_frame_rate(track);
293 width = ff->ff_video_width(track);
294 height = ff->ff_video_height(track);
295 if( title ) *title = 0;
299 int FileFFMPEG::get_audio_for_video(int vstream, int astream, int64_t &channel_mask)
302 return ff->ff_audio_for_video(vstream, astream, channel_mask);
305 int FileFFMPEG::select_video_stream(Asset *asset, int vstream)
307 if( !ff || !asset->video_data ) return 1;
308 asset->width = ff->ff_video_width(vstream);
309 asset->height = ff->ff_video_height(vstream);
310 if( (asset->video_length = ff->ff_video_frames(vstream)) < 2 )
311 asset->video_length = asset->video_length < 0 ? 0 : -1;
312 asset->frame_rate = ff->ff_frame_rate(vstream);
316 int FileFFMPEG::select_audio_stream(Asset *asset, int astream)
318 if( !ff || !asset->audio_data ) return 1;
319 asset->sample_rate = ff->ff_sample_rate(astream);
320 asset->audio_length = ff->ff_audio_samples(astream);
324 int FileFFMPEG::open_file(int rd, int wr)
328 ff = new FFMPEG(this);
331 result = ff->init_decoder(asset->path);
332 if( !result ) result = ff->open_decoder();
334 int audio_channels = ff->ff_total_audio_channels();
335 if( audio_channels > 0 ) {
336 asset->audio_data = 1;
337 asset->channels = audio_channels;
338 asset->sample_rate = ff->ff_sample_rate(0);
339 asset->audio_length = ff->ff_audio_samples(0);
340 strcpy(asset->acodec, ff->ff_audio_format(0));
342 int video_layers = ff->ff_total_video_layers();
343 if( video_layers > 0 ) {
344 asset->video_data = 1;
345 asset->aspect_ratio = ff->ff_aspect_ratio(0);
346 if (!asset->interlace_mode) asset->interlace_mode = ff->ff_interlace(0);
347 if ( ff->ff_video_frames(0) > 1 ) {
349 if (!asset->interlace_mode && (ff->interlace_from_codec) ) asset->interlace_mode = ff->video_probe(1);
351 if( !asset->layers ) asset->layers = video_layers;
352 asset->actual_width = ff->ff_video_width(0);
353 asset->actual_height = ff->ff_video_height(0);
354 if( !asset->width ) asset->width = asset->actual_width;
355 if( !asset->height ) asset->height = asset->actual_height;
356 if( !asset->video_length &&
357 (asset->video_length = ff->ff_video_frames(0)) < 2 )
358 asset->video_length = asset->video_length < 0 ? 0 : -1;
359 if( !asset->frame_rate ) asset->frame_rate = ff->ff_frame_rate(0);
360 if( asset->ff_color_range < 0 )
361 asset->ff_color_range = ff->ff_color_range(0);
362 if( asset->ff_color_space < 0 )
363 asset->ff_color_space = ff->ff_color_space(0);
364 strcpy(asset->vcodec, ff->ff_video_codec(0));
366 IndexState *index_state = asset->index_state;
367 index_state->read_markers(file->preferences->index_directory, asset->path);
371 result = ff->init_encoder(asset->path);
372 // must be in this order or dvdauthor will fail
373 if( !result && asset->video_data )
374 result = ff->open_encoder("video", asset->vcodec);
375 if( !result && asset->audio_data )
376 result = ff->open_encoder("audio", asset->acodec);
381 int FileFFMPEG::close_file()
389 int FileFFMPEG::write_samples(double **buffer, int64_t len)
391 if( !ff || len < 0 ) return -1;
393 int ret = ff->encode(stream, buffer, len);
397 int FileFFMPEG::write_frames(VFrame ***frames, int len)
400 int ret = 0, layer = 0;
401 for(int i = 0; i < 1; i++) {
402 for(int j = 0; j < len && !ret; j++) {
403 VFrame *frame = frames[i][j];
404 ret = ff->encode(layer, frame);
411 int FileFFMPEG::read_samples(double *buffer, int64_t len)
413 if( !ff || len < 0 ) return -1;
414 int ch = file->current_channel;
415 int64_t pos = file->current_sample;
416 int ret = ff->decode(ch, pos, buffer, len);
417 if( ret > 0 ) return 0;
418 memset(buffer,0,len*sizeof(*buffer));
422 int FileFFMPEG::read_frame(VFrame *frame)
425 int layer = file->current_layer;
426 int64_t pos = asset->video_length >= 0 ? file->current_frame : 0;
427 int ret = ff->decode(layer, pos, frame);
428 frame->set_status(ret);
429 if( ret >= 0 ) return 0;
430 frame->clear_frame();
435 int64_t FileFFMPEG::get_memory_usage()
440 int FileFFMPEG::colormodel_supported(int colormodel)
446 int FileFFMPEG::get_best_colormodel(int driver, int vstream)
448 if( vstream < 0 ) vstream = 0;
449 int is_mpeg = !ff ? 0 : ff->ff_video_mpeg_color_range(vstream);
453 case PLAYBACK_X11_GL: return is_mpeg ? BC_YUV888 : BC_RGB888;
454 case PLAYBACK_X11_XV: return BC_YUV420P;
460 int FileFFMPEG::get_best_colormodel(Asset *asset, int driver)
463 // the direct X11 color model requires scaling in the codec
466 case PLAYBACK_X11_GL: return BC_RGB888;
467 case PLAYBACK_X11_XV: return BC_YUV420P;
474 FFMPEGConfigWindow::FFMPEGConfigWindow(const char *title,
475 BC_WindowBase *parent_window,
476 int x, int y, int w, int h,
477 Asset *asset, EDL *edl)
478 : BC_Window(title, x, y, w, h)
480 this->parent_window = parent_window;
485 ff_options_dialog = 0;
490 FFMPEGConfigWindow::~FFMPEGConfigWindow()
492 delete ff_options_dialog;
495 void FFMPEGConfigWindow::start(AVCodecContext *avctx)
498 ff_options_dialog->start();
501 void FFMPEGConfigWindow::start(AVFormatContext *fmt_ctx)
503 this->fmt_ctx = fmt_ctx;
504 ff_options_dialog->start();
509 FFMPEGConfigAudio::FFMPEGConfigAudio(BC_WindowBase *parent_window,
510 int x, int y, Asset *asset, EDL *edl)
511 : FFMPEGConfigWindow(_(PROGRAM_NAME ": Audio Preset"), parent_window, x, y,
512 xS(420), yS(420), asset, edl)
517 format_name = asset->fformat;
518 codec_name = asset->acodec;
521 FFMPEGConfigAudio::~FFMPEGConfigAudio()
523 lock_window("FFMPEGConfigAudio::~FFMPEGConfigAudio");
525 presets.remove_all_objects();
526 delete audio_options;
530 void FFMPEGConfigAudio::read_options()
532 const char *options = audio_options->get_text();
533 int options_len = strlen(options);
534 ff_options_dialog->load_options(options, options_len);
536 void FFMPEGConfigAudio::save_options()
538 char options[BCTEXTLEN];
539 ff_options_dialog->store_options(options, sizeof(options)-1);
540 audio_options->update(options);
543 void FFMPEGConfigAudio::load_options()
545 FFMPEG::load_audio_options(asset, edl);
548 void FFMPEGConfigAudio::create_objects()
550 int x = xS(10), y = yS(10);
551 lock_window("FFMPEGConfigAudio::create_objects");
554 char option_path[BCTEXTLEN];
555 FFMPEG::set_option_path(option_path, "audio");
556 fs.update(option_path);
557 int total_files = fs.total_files();
558 for(int i = 0; i < total_files; i++) {
559 const char *name = fs.get_entry(i)->get_name();
560 if( asset->fformat[0] != 0 ) {
561 const char *ext = strrchr(name,'.');
563 if( strcmp(asset->fformat, ++ext) ) continue;
565 presets.append(new BC_ListBoxItem(name));
568 if( asset->acodec[0] ) {
569 int k = presets.size();
570 while( --k >= 0 && strcmp(asset->acodec, presets[k]->get_text()) );
571 if( k < 0 ) asset->acodec[0] = 0;
574 if( !asset->acodec[0] && presets.size() > 0 )
575 strcpy(asset->acodec, presets[0]->get_text());
577 add_tool(new BC_Title(x, y, _("Preset:")));
579 preset_popup = new FFMPEGConfigAudioPopup(this, x, y);
580 preset_popup->create_objects();
583 bitrate = new FFMpegAudioBitrate(this, x, y, _("Bitrate:"), &asset->ff_audio_bitrate);
584 bitrate->create_objects();
585 bitrate->set_increment(1000);
586 bitrate->set_boundaries((int64_t)0, (int64_t)INT_MAX);
587 y += bitrate->get_h() + 5;
588 quality = new FFMpegAudioQuality(this, x, y, _("Quality:"), &asset->ff_audio_quality);
589 quality->create_objects();
590 quality->set_increment(1);
591 quality->set_boundaries((int64_t)-1, (int64_t)51);
592 y += quality->get_h() + yS(10);
594 add_subwindow(new BC_Title(x, y, _("Samples:")));
595 sample_format = new FFMpegSampleFormat(this, x+xS(90), y, xS(100), yS(120));
596 sample_format->create_objects();
597 if( asset->acodec[0] ) {
598 sample_format->update_formats();
599 if( !asset->ff_audio_options[0] )
602 if( !asset->ff_sample_format[0] ) strcpy(asset->ff_sample_format, _("None"));
603 sample_format->update(asset->ff_sample_format);
604 y += sample_format->get_h() + yS(10);
606 BC_Title *title = new BC_Title(x, y, _("Audio Options:"));
607 add_subwindow(title);
609 ff_options_dialog = new FFOptionsAudioDialog(this);
610 int x1 = x + title->get_w() + xS(8);
611 add_subwindow(view_audio = new FFOptionsViewAudio(this, x1, y, _("view")));
612 x1 += x + view_audio->get_w() + xS(20);
613 view_format = new FFOptionsViewFormat(this, edl, asset, x1, y, _("format"));
614 add_subwindow(view_format);
617 audio_options = new FFAudioOptions(this, x, y, get_w()-x-xS(20), 8,
618 sizeof(asset->ff_audio_options)-1, asset->ff_audio_options);
619 audio_options->create_objects();
620 add_subwindow(new BC_OKButton(this));
621 add_subwindow(new BC_CancelButton(this));
624 bitrate->update_param("cin_bitrate", asset->ff_audio_options);
625 quality->update_param("cin_quality", asset->ff_audio_options);
627 if( asset->ff_audio_bitrate > 0 ) quality->disable();
628 else if( asset->ff_audio_quality >= 0 ) bitrate->disable();
633 int FFMPEGConfigAudio::close_event()
639 FFAudioOptions::FFAudioOptions(FFMPEGConfigAudio *audio_popup,
640 int x, int y, int w, int rows, int size, char *text)
641 : BC_ScrollTextBox(audio_popup, x, y, w, rows, text, size)
643 this->audio_popup = audio_popup;
647 FFMPEGConfigAudioPopup::FFMPEGConfigAudioPopup(FFMPEGConfigAudio *popup, int x, int y)
648 : BC_PopupTextBox(popup, &popup->presets, popup->asset->acodec, x, y, xS(300), yS(300))
653 int FFMPEGConfigAudioPopup::handle_event()
655 strcpy(popup->asset->acodec, get_text());
656 popup->sample_format->update_formats();
657 Asset *asset = popup->asset;
658 asset->ff_audio_bitrate = 0; asset->ff_audio_quality = -1;
659 popup->load_options();
660 popup->audio_options->update(asset->ff_audio_options);
661 popup->audio_options->set_text_row(0);
663 popup->bitrate->update_param("cin_bitrate", asset->ff_audio_options);
664 popup->quality->update_param("cin_quality", asset->ff_audio_options);
665 popup->sample_format->update(asset->ff_sample_format);
671 FFMPEGConfigVideo::FFMPEGConfigVideo(BC_WindowBase *parent_window,
672 int x, int y, Asset *asset, EDL *edl)
673 : FFMPEGConfigWindow(_(PROGRAM_NAME ": Video Preset"), parent_window, x, y,
674 xS(420), yS(420), asset, edl)
678 format_name = asset->fformat;
679 codec_name = asset->vcodec;
686 FFMPEGConfigVideo::~FFMPEGConfigVideo()
688 lock_window("FFMPEGConfigVideo::~FFMPEGConfigVideo");
691 delete video_options;
692 presets.remove_all_objects();
696 void FFMPEGConfigVideo::read_options()
698 const char *options = video_options->get_text();
699 int options_len = strlen(options);
700 ff_options_dialog->load_options(options, options_len);
702 void FFMPEGConfigVideo::save_options()
704 char options[BCTEXTLEN];
705 ff_options_dialog->store_options(options, sizeof(options)-1);
706 video_options->update(options);
709 void FFMPEGConfigVideo::load_options()
711 FFMPEG::load_video_options(asset, edl);
714 void FFMPEGConfigVideo::create_objects()
716 int x = xS(10), y = yS(10);
717 lock_window("FFMPEGConfigVideo::create_objects");
719 add_subwindow(new BC_Title(x, y, _("Compression:")));
723 char option_path[BCTEXTLEN];
724 FFMPEG::set_option_path(option_path, "video");
725 fs.update(option_path);
726 int total_files = fs.total_files();
727 for(int i = 0; i < total_files; i++) {
728 const char *name = fs.get_entry(i)->get_name();
729 if( asset->fformat[0] != 0 ) {
730 const char *ext = strrchr(name,'.');
732 if( strcmp(asset->fformat, ++ext) ) continue;
734 presets.append(new BC_ListBoxItem(name));
737 if( asset->vcodec[0] ) {
738 int k = presets.size();
739 while( --k >= 0 && strcmp(asset->vcodec, presets[k]->get_text()) );
740 if( k < 0 ) asset->vcodec[0] = 0;
743 if( !asset->vcodec[0] && presets.size() > 0 )
744 strcpy(asset->vcodec, presets[0]->get_text());
746 preset_popup = new FFMPEGConfigVideoPopup(this, x, y);
747 preset_popup->create_objects();
749 if( asset->ff_video_bitrate > 0 && asset->ff_video_quality >= 0 ) {
750 asset->ff_video_bitrate = 0; asset->ff_video_quality = -1;
754 bitrate = new FFMpegVideoBitrate(this, x, y, _("Bitrate:"), &asset->ff_video_bitrate);
755 bitrate->create_objects();
756 bitrate->set_increment(100000);
757 bitrate->set_boundaries((int64_t)0, (int64_t)INT_MAX);
758 y += bitrate->get_h() + 5;
759 quality = new FFMpegVideoQuality(this, x, y, _("Quality:"), &asset->ff_video_quality);
760 quality->create_objects();
761 quality->set_increment(1);
762 quality->set_boundaries((int64_t)-1, (int64_t)51);
763 y += quality->get_h() + yS(10);
765 add_subwindow(new BC_Title(x, y, _("Pixels:")));
766 pixel_format = new FFMpegPixelFormat(this, x+xS(90), y, xS(100), yS(120));
767 pixel_format->create_objects();
768 if( asset->vcodec[0] ) {
769 pixel_format->update_formats();
770 if( !asset->ff_video_options[0] )
773 if( !asset->ff_pixel_format[0] ) strcpy(asset->ff_pixel_format, _("None"));
774 pixel_format->update(asset->ff_pixel_format);
775 y += pixel_format->get_h() + yS(10);
777 BC_Title *title = new BC_Title(x, y, _("Video Options:"));
778 add_subwindow(title);
780 ff_options_dialog = new FFOptionsVideoDialog(this);
781 int x1 = x + title->get_w() + 8;
782 add_subwindow(view_video = new FFOptionsViewVideo(this, x1, y, _("view")));
783 x1 += x + view_video->get_w() + xS(20);
784 view_format = new FFOptionsViewFormat(this, edl, asset, x1, y, _("format"));
785 add_subwindow(view_format);
788 video_options = new FFVideoOptions(this, x, y, get_w()-x-xS(20), 8,
789 sizeof(asset->ff_video_options)-1, asset->ff_video_options);
790 video_options->create_objects();
791 add_subwindow(new BC_OKButton(this));
792 add_subwindow(new BC_CancelButton(this));
795 bitrate->update_param("cin_bitrate", asset->ff_video_options);
796 quality->update_param("cin_quality", asset->ff_video_options);
798 if( asset->ff_video_bitrate > 0 ) quality->disable();
799 else if( asset->ff_video_quality >= 0 ) bitrate->disable();
803 int FFMPEGConfigVideo::close_event()
809 FFVideoOptions::FFVideoOptions(FFMPEGConfigVideo *video_popup,
810 int x, int y, int w, int rows, int size, char *text)
811 : BC_ScrollTextBox(video_popup, x, y, w, rows, text, size)
813 this->video_popup = video_popup;
817 FFMPEGConfigVideoPopup::FFMPEGConfigVideoPopup(FFMPEGConfigVideo *popup, int x, int y)
818 : BC_PopupTextBox(popup, &popup->presets, popup->asset->vcodec, x, y, xS(300), yS(300))
823 int FFMPEGConfigVideoPopup::handle_event()
825 strcpy(popup->asset->vcodec, get_text());
826 popup->pixel_format->update_formats();
827 Asset *asset = popup->asset;
828 asset->ff_video_bitrate = 0; asset->ff_video_quality = -1;
829 popup->load_options();
830 popup->video_options->update(asset->ff_video_options);
831 popup->video_options->set_text_row(0);
833 popup->bitrate->update_param("cin_bitrate", asset->ff_video_options);
834 popup->quality->update_param("cin_quality", asset->ff_video_options);
835 popup->pixel_format->update(asset->ff_pixel_format);
841 FFMPEGConfigFormat::FFMPEGConfigFormat(FFOptionsFormatViewDialog *view_dialog,
842 int x, int y, Asset *asset, EDL *edl)
843 : FFMPEGConfigWindow(_(PROGRAM_NAME ": Format Preset"),
844 view_dialog->view_format, x, y, xS(420), yS(300), asset, edl)
846 this->view_dialog = view_dialog;
848 format_name = asset->fformat;
852 FFMPEGConfigFormat::~FFMPEGConfigFormat()
854 lock_window("FFMPEGConfigFormat::~FFMPEGConfigFormat");
855 delete format_options;
859 void FFMPEGConfigFormat::read_options()
861 const char *options = format_options->get_text();
862 int options_len = strlen(options);
863 ff_options_dialog->load_options(options, options_len);
865 void FFMPEGConfigFormat::save_options()
867 char options[BCTEXTLEN];
868 ff_options_dialog->store_options(options, sizeof(options)-1);
869 format_options->update(options);
871 void FFMPEGConfigFormat::save_changes()
874 char *options = asset->ff_format_options;
875 int options_len = sizeof(asset->ff_format_options)-1;
876 ff_options_dialog->store_options(options, options_len);
879 void FFMPEGConfigFormat::load_options()
881 Asset *asset = view_dialog->view_format->asset;
882 EDL *edl = view_dialog->view_format->edl;
883 FFMPEG::load_format_options(asset, edl);
886 void FFMPEGConfigFormat::create_objects()
888 int x = xS(10), y = yS(10);
889 lock_window("FFMPEGConfigFormat::create_objects");
890 Asset *asset = view_dialog->view_format->asset;
892 add_subwindow(title = new BC_Title(x, y, _("Format:")));
893 int x1 = x + title->get_w() + 8;
894 add_subwindow(new BC_Title(x1, y, asset->fformat));
897 add_subwindow(title = new BC_Title(x, y, _("Format Options:")));
899 ff_options_dialog = new FFOptionsFormatDialog(this);
900 x1 = x + title->get_w() + 8;
901 add_subwindow(new FFOptionsFormatView(this, x1, y, _("view")));
904 format_options = new FFFormatOptions(this, x, y, get_w()-x-xS(20), 8,
905 sizeof(asset->ff_format_options)-1, asset->ff_format_options);
906 format_options->create_objects();
907 add_subwindow(new BC_OKButton(this));
908 add_subwindow(new BC_CancelButton(this));
913 int FFMPEGConfigFormat::close_event()
919 FFFormatOptions::FFFormatOptions(FFMPEGConfigFormat *format_popup,
920 int x, int y, int w, int rows, int size, char *text)
921 : BC_ScrollTextBox(format_popup, x, y, w, rows, text, size)
923 this->format_popup = format_popup;
928 FFMPEGScanProgress::FFMPEGScanProgress(IndexFile *index_file, MainProgressBar *progress_bar,
929 const char *title, int64_t length, int64_t *position, int *canceled)
932 this->index_file = index_file;
933 this->progress_bar = progress_bar;
934 strcpy(this->progress_title, title);
935 this->length = length;
936 this->position = position;
937 this->canceled = canceled;
942 FFMPEGScanProgress::~FFMPEGScanProgress()
949 void FFMPEGScanProgress::run()
952 if( progress_bar->update(*position) ) {
953 *canceled = done = 1;
956 index_file->redraw_edits(0);
961 int FileFFMPEG::get_index(IndexFile *index_file, MainProgressBar *progress_bar)
964 if( !file->preferences->ffmpeg_marker_indexes ) return 1;
966 IndexState *index_state = index_file->get_state();
967 if( index_state->index_status != INDEX_NOTTESTED ) return 0;
968 index_state->reset_index();
969 index_state->reset_markers();
970 index_state->index_status = INDEX_BUILDING;
972 for( int aidx=0; aidx<ff->ffaudio.size(); ++aidx ) {
973 FFAudioStream *aud = ff->ffaudio[aidx];
974 index_state->add_audio_stream(aud->channels, aud->length);
978 int64_t file_bytes = fs.get_size(ff->fmt_ctx->url);
979 char *index_path = index_file->index_filename;
982 int64_t scan_position = 0;
983 FFMPEGScanProgress *scan_progress = 0;
985 char progress_title[BCTEXTLEN];
986 sprintf(progress_title, _("Creating %s\n"), index_path);
987 progress_bar->update_title(progress_title, 1);
988 progress_bar->update_length(file_bytes);
989 scan_progress = new FFMPEGScanProgress(index_file,
990 progress_bar, progress_title, file_bytes,
991 &scan_position, &canceled);
994 index_state->index_bytes = file_bytes;
995 index_state->init_scan(file->preferences->index_size);
997 if( ff->scan(index_state, &scan_position, &canceled) || canceled ) {
998 index_state->reset_index();
999 index_state->reset_markers();
1003 delete scan_progress;
1004 if( canceled ) return 1;
1006 index_state->marker_status = MARKERS_READY;
1007 return index_state->create_index(index_path, asset);
1011 FFOptions_OptPanel::
1012 FFOptions_OptPanel(FFOptionsWindow *fwin, int x, int y, int w, int h)
1013 : BC_ListBox(x, y, w, h, LISTBOX_TEXT), opts(items[0]), vals(items[1])
1016 update(); // init col/wid/columns
1019 FFOptions_OptPanel::
1020 ~FFOptions_OptPanel()
1024 void FFOptions_OptPanel::create_objects()
1026 const char *cols[] = { _("option"), _("value"), };
1027 const int col1_w = xS(150);
1028 int wids[] = { col1_w, get_w()-col1_w };
1029 BC_ListBox::update(&items[0], &cols[0], &wids[0], sizeof(items)/sizeof(items[0]));
1032 int FFOptions_OptPanel::update()
1036 FFOptions &options = fwin->options;
1037 for( int i=0; i<options.size(); ++i ) {
1038 FFOptions_Opt *opt = options[i];
1039 opts.append(opt->item_name);
1040 vals.append(opt->item_value);
1046 int FFOptions_OptPanel::cursor_leave_event()
1053 FFOptions_OptName::FFOptions_OptName(FFOptions_Opt *opt, const char *nm)
1059 FFOptions_OptName::~FFOptions_OptName()
1063 FFOptions_OptValue::FFOptions_OptValue(FFOptions_Opt *opt)
1069 void FFOptions_OptValue::update()
1072 char val[BCTEXTLEN]; val[0] = 0;
1073 opt->get(val, sizeof(val));
1077 void FFOptions_OptValue::update(const char *v)
1082 FFOptions_Opt::FFOptions_Opt(FFOptions *options, const AVOption *opt, const char *nm)
1084 this->options = options;
1086 item_name = new FFOptions_OptName(this, nm);
1087 item_value = new FFOptions_OptValue(this);
1090 FFOptions_Opt::~FFOptions_Opt()
1096 char *FFOptions_Opt::get(char *vp, int sz)
1100 if( !opt ) return cp;
1102 void *obj = (void *)options->obj;
1104 if( av_opt_get(obj, opt->name, 0, &bp) >= 0 && bp != 0 ) {
1105 const char *val = (const char *)bp;
1106 if( opt->unit && *val ) {
1108 const char *uid = unit_name(id);
1109 if( uid ) val = uid;
1111 cp = sz >= 0 ? strncpy(vp,val,sz) : strcpy(vp, val);
1112 if( sz > 0 ) vp[sz-1] = 0;
1119 void FFOptions_Opt::set(const char *val)
1121 void *obj = (void *)options->obj;
1122 if( !obj || !opt ) return;
1123 av_opt_set(obj, opt->name, val, 0);
1127 FFOptionsKindItem::FFOptionsKindItem(FFOptionsKind *kind, const char *text, int idx)
1134 FFOptionsKindItem::~FFOptionsKindItem()
1138 int FFOptionsKindItem::handle_event()
1140 FFOptionsWindow *fwin = kind->fwin;
1141 FFOptions &options = fwin->options;
1142 options.initialize(fwin, idx);
1143 kind->set_text(get_text());
1148 const char *FFOptionsKind::kinds[] = {
1149 N_("codec"), // FF_KIND_CODEC
1150 N_("format"), // FF_KIND_FORMAT
1151 N_("ffmpeg"), // FF_KIND_FFMPEG
1155 FFOptionsKind(FFOptionsWindow *fwin, int x, int y, int w)
1156 : BC_PopupMenu(x, y, w, "")
1166 void FFOptionsKind::create_objects()
1168 add_item(new FFOptionsKindItem(this, _(kinds[FF_KIND_CODEC]), FF_KIND_CODEC));
1169 add_item(new FFOptionsKindItem(this, _(kinds[FF_KIND_FFMPEG]), FF_KIND_FFMPEG));
1172 int FFOptionsKind::handle_event()
1177 void FFOptionsKind::set(int k)
1180 set_text(_(kinds[k]));
1184 FFOptionsText(FFOptionsWindow *fwin, int x, int y, int w)
1185 : BC_TextBox(x, y, w, 1, (char *)"")
1195 int FFOptionsText::handle_event()
1201 FFOptionsUnits(FFOptionsWindow *fwin, int x, int y, int w)
1202 : BC_PopupMenu(x, y, w, "")
1212 int FFOptionsUnits::handle_event()
1214 const char *text = get_text();
1215 if( text && fwin->selected ) {
1216 fwin->selected->set(text);
1217 fwin->selected->item_value->update();
1218 av_dict_set(&fwin->dialog->ff_opts,
1219 fwin->selected->item_name->get_text(),
1220 fwin->selected->item_value->get_text(), 0);
1227 FFOptionsApply(FFOptionsWindow *fwin, int x, int y)
1228 : BC_GenericButton(x, y, _("Apply"))
1238 int FFOptionsApply::handle_event()
1240 const char *text = fwin->text->get_text();
1241 if( text && fwin->selected ) {
1242 fwin->selected->set(text);
1243 fwin->selected->item_value->update();
1244 av_dict_set(&fwin->dialog->ff_opts,
1245 fwin->selected->item_name->get_text(),
1246 fwin->selected->item_value->get_text(), 0);
1252 FFOptions::FFOptions()
1257 FFOptions::~FFOptions()
1259 remove_all_objects();
1262 int FFOptions::cmpr(const void *a, const void *b)
1264 FFOptions_Opt *ap = *(FFOptions_Opt **)a;
1265 FFOptions_Opt *bp = *(FFOptions_Opt **)b;
1266 const char *vap = ap->item_name->get_text();
1267 const char *vbp = bp->item_name->get_text();
1268 return strcmp(vap, vbp);
1271 void FFOptions::initialize(FFOptionsWindow *win, int kind)
1273 remove_all_objects();
1275 const void *obj = 0;
1278 obj = (const void *)win->dialog->cfg_window->avctx->priv_data;
1280 case FF_KIND_FFMPEG:
1281 obj = (const void *)win->dialog->cfg_window->avctx;
1283 case FF_KIND_FORMAT:
1284 obj = (const void *)win->dialog->cfg_window->fmt_ctx->priv_data;
1290 FFOptions &conf = *this;
1291 const AVOption *opt = 0;
1292 while( (opt=av_opt_next(obj, opt)) != 0 ) {
1293 if( opt->type == AV_OPT_TYPE_CONST ) continue;
1295 for( int i=0; !dupl && i<size(); ++i ) {
1296 FFOptions_Opt *fopt = conf[i];
1297 const AVOption *op = fopt->opt;
1298 if( op->offset != opt->offset ) continue;
1299 if( op->type != opt->type ) continue;
1301 if( strlen(op->name) < strlen(opt->name) )
1304 if( dupl ) continue;
1305 FFOptions_Opt *fopt = new FFOptions_Opt(this, opt, opt->name);
1307 AVDictionaryEntry *elem = av_dict_get(win->dialog->ff_opts,
1308 opt->name, 0, AV_DICT_IGNORE_SUFFIX);
1309 if( elem && elem->value ) fopt->set(elem->value);
1310 char val[BCTEXTLEN], *vp = fopt->get(val, sizeof(val));
1311 fopt->item_value->update(vp);
1313 qsort(&values[0],size(),sizeof(values[0]),cmpr);
1315 win->panel->update();
1316 win->panel->set_yposition(0);
1319 int FFOptions::update()
1322 FFOptions &conf = *this;
1324 for( int i=0; i<size(); ++i ) {
1325 FFOptions_Opt *fopt = conf[i];
1326 char val[BCTEXTLEN], *vp = fopt->get(val, sizeof(val));
1327 if( !vp || !strcmp(val, fopt->item_value->get_text()) ) continue;
1328 fopt->item_value->update(val);
1334 void FFOptions::dump(FILE *fp)
1337 const AVOption *opt = 0;
1338 FFOptions &conf = *this;
1340 while( (opt=av_opt_next(obj, opt)) != 0 ) {
1341 if( opt->type == AV_OPT_TYPE_CONST ) continue;
1343 while( --k >= 0 && strcmp(opt->name, conf[k]->opt->name) );
1344 if( k < 0 ) continue;
1345 FFOptions_Opt *fopt = conf[k];
1346 char val[BCTEXTLEN], *vp = fopt->get(val,sizeof(val));
1347 fprintf(fp, " %s:=%s", opt->name, vp);
1349 char unt[BCTEXTLEN], *up = unt;
1351 fprintf(fp, "%s", unt);
1358 void FFOptionsWindow::update(FFOptions_Opt *opt)
1360 if( selected != opt ) {
1361 if( selected ) selected->item_name->set_selected(0);
1363 if( selected ) selected->item_name->set_selected(1);
1365 clear_box(0,0, 0,panel->get_y());
1366 char str[BCTEXTLEN], *sp;
1368 if( opt ) opt->types(sp);
1371 if( opt ) opt->ranges(sp);
1373 while( units->total_items() ) units->del_item(0);
1374 char unit[BCSTRLEN]; strcpy(unit, "()");
1375 if( opt && opt->opt ) {
1376 ArrayList<const char *> names;
1378 if( opt->opt->unit ) {
1379 n = opt->units(names);
1380 if( n > 0 ) strcpy(unit,opt->opt->unit);
1382 for( int i=0; i<n; ++i )
1383 units->add_item(new BC_MenuItem(names[i], 0));
1385 units->set_text(unit);
1386 char val[BCTEXTLEN]; val[0] = 0;
1387 if( opt ) opt->get(val, sizeof(val));
1393 void FFOptions_OptPanel::show_tip(const char *tip)
1396 int len = strlen(tip);
1397 if( len > (int)sizeof(tip_text)-1 ) len = sizeof(tip_text)-1;
1398 strncpy(tip_text,tip,len);
1400 int line_limit = 60;
1401 int limit2 = line_limit/2;
1402 int limit4 = line_limit/4-2;
1403 char *cp = tip_text, *dp = cp+len;
1404 int n; char *bp, *ep, *pp, *sp;
1406 for( ep=cp; ep<dp && *ep!='\n'; ++ep );
1407 // target about half remaining line, constrain line_limit
1408 if( (n=(ep-1-cp)/2) < limit2 || n > line_limit )
1410 // search for last punct, last space before line_limit
1411 for( bp=cp, pp=sp=0; --n>=0 && cp<ep; ++cp ) {
1412 if( ispunct(*cp) && isspace(*(cp+1)) ) pp = cp;
1413 else if( isspace(*cp) ) sp = cp;
1417 // first, after punctuation
1418 if( pp && pp-bp >= limit4 )
1424 // last, on next space
1426 while( cp<dp && !isspace(*cp) ) ++cp;
1433 fwin->panel->set_tooltip(tip_text);
1434 fwin->panel->show_tooltip();
1437 int FFOptions_OptPanel::selection_changed()
1439 FFOptions_Opt *opt = 0;
1440 BC_ListBoxItem *item = get_selection(0, 0);
1442 FFOptions_OptName *opt_name = (FFOptions_OptName *)item;
1443 opt = opt_name->opt;
1446 if( opt ) show_tip(opt->tip());
1451 int FFOptions_Opt::types(char *rp)
1453 const char *cp = "";
1454 if( opt ) switch (opt->type) {
1455 case AV_OPT_TYPE_FLAGS: cp = N_("<flags>"); break;
1456 case AV_OPT_TYPE_INT: cp = N_("<int>"); break;
1457 case AV_OPT_TYPE_INT64: cp = N_("<int64>"); break;
1458 case AV_OPT_TYPE_DOUBLE: cp = N_("<double>"); break;
1459 case AV_OPT_TYPE_FLOAT: cp = N_("<float>"); break;
1460 case AV_OPT_TYPE_STRING: cp = N_("<string>"); break;
1461 case AV_OPT_TYPE_RATIONAL: cp = N_("<rational>"); break;
1462 case AV_OPT_TYPE_BINARY: cp = N_("<binary>"); break;
1463 case AV_OPT_TYPE_IMAGE_SIZE: cp = N_("<image_size>"); break;
1464 case AV_OPT_TYPE_VIDEO_RATE: cp = N_("<video_rate>"); break;
1465 case AV_OPT_TYPE_PIXEL_FMT: cp = N_("<pix_fmt>"); break;
1466 case AV_OPT_TYPE_SAMPLE_FMT: cp = N_("<sample_fmt>"); break;
1467 case AV_OPT_TYPE_DURATION: cp = N_("<duration>"); break;
1468 case AV_OPT_TYPE_COLOR: cp = N_("<color>"); break;
1469 case AV_OPT_TYPE_CHANNEL_LAYOUT: cp = N_("<channel_layout>"); break;
1470 case AV_OPT_TYPE_BOOL: cp = N_("<bool>"); break;
1471 default: cp = N_("<undef>"); break;
1473 return sprintf(rp, "%s", _(cp));
1475 int FFOptions_Opt::scalar(double d, char *rp)
1478 if( d == INT_MAX ) cp = "INT_MAX";
1479 else if( d == INT_MIN ) cp = "INT_MIN";
1480 else if( d == UINT32_MAX ) cp = "UINT32_MAX";
1481 else if( d == (double)INT64_MAX ) cp = "I64_MAX";
1482 else if( d == INT64_MIN ) cp = "I64_MIN";
1483 else if( d == FLT_MAX ) cp = "FLT_MAX";
1484 else if( d == FLT_MIN ) cp = "FLT_MIN";
1485 else if( d == -FLT_MAX ) cp = "-FLT_MAX";
1486 else if( d == -FLT_MIN ) cp = "-FLT_MIN";
1487 else if( d == DBL_MAX ) cp = "DBL_MAX";
1488 else if( d == DBL_MIN ) cp = "DBL_MIN";
1489 else if( d == -DBL_MAX ) cp = "-DBL_MAX";
1490 else if( d == -DBL_MIN ) cp = "-DBL_MIN";
1491 else if( d == 0 ) cp = signbit(d) ? "-0" : "0";
1492 else if( isnan(d) ) cp = signbit(d) ? "-NAN" : "NAN";
1493 else if( isinf(d) ) cp = signbit(d) ? "-INF" : "INF";
1494 else return sprintf(rp, "%g", d);
1495 return sprintf(rp, "%s", cp);
1498 int FFOptions_Opt::ranges(char *rp)
1500 if( !opt ) return 0;
1501 void *obj = (void *)options->obj;
1502 if( !obj ) return 0;
1504 switch (opt->type) {
1505 case AV_OPT_TYPE_INT:
1506 case AV_OPT_TYPE_INT64:
1507 case AV_OPT_TYPE_DOUBLE:
1508 case AV_OPT_TYPE_FLOAT: break;
1511 AVOptionRanges *r = 0;
1513 if( av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) < 0 ) return 0;
1514 for( int i=0; i<r->nb_ranges; ++i ) {
1515 cp += sprintf(cp, " ("); cp += scalar(r->range[i]->value_min, cp);
1516 cp += sprintf(cp, ".."); cp += scalar(r->range[i]->value_max, cp);
1517 cp += sprintf(cp, ")");
1519 av_opt_freep_ranges(&r);
1523 int FFOptions_Opt::units(ArrayList<const char *> &names)
1525 if( !opt || !opt->unit ) return 0;
1526 const void *obj = options->obj;
1527 if( !obj ) return 0;
1529 ArrayList<const AVOption *> opts;
1530 const AVOption *opt = NULL;
1531 while( (opt=av_opt_next(obj, opt)) != 0 ) {
1532 if( !opt->unit ) continue;
1533 if( opt->type != AV_OPT_TYPE_CONST ) continue;
1534 if( strcmp(this->opt->unit, opt->unit) ) continue;
1535 int i = opts.size();
1537 if( opts[i]->default_val.i64 != opt->default_val.i64 ) continue;
1538 if( strlen(opts[i]->name) < strlen(opt->name) ) opts[i] = opt;
1541 if( i >= 0 ) continue;
1545 for( int i=0; i<opts.size(); ++i )
1546 names.append(opts[i]->name);
1548 return names.size();
1551 int FFOptions_Opt::units(char *rp)
1553 ArrayList<const char *> names;
1554 int n = units(names);
1557 cp += sprintf(cp, " [%s:", this->opt->unit);
1558 for( int i=0; i<n; ++i )
1559 cp += sprintf(cp, " %s", names[i]);
1560 cp += sprintf(cp, "]:");
1564 const char *FFOptions_Opt::unit_name(int id)
1566 if( !opt || !opt->unit ) return 0;
1567 const void *obj = options->obj;
1568 if( !obj ) return 0;
1570 const char *ret = 0;
1571 const AVOption *opt = NULL;
1572 while( (opt=av_opt_next(obj, opt)) != 0 ) {
1573 if( !opt->unit ) continue;
1574 if( opt->type != AV_OPT_TYPE_CONST ) continue;
1575 if( strcmp(this->opt->unit, opt->unit) ) continue;
1576 if( opt->default_val.i64 != id ) continue;
1577 if( !ret ) { ret = opt->name; continue; }
1578 if( strlen(ret) < strlen(opt->name) ) ret = opt->name;
1584 const char *FFOptions_Opt::tip()
1586 return !opt ? 0 : opt->help;
1590 FFOptionsWindow::FFOptionsWindow(FFOptionsDialog *dialog, int x, int y)
1591 : BC_Window(_(PROGRAM_NAME ": Options"), x, y, xS(640), yS(400))
1593 this->dialog = dialog;
1598 FFOptionsWindow::~FFOptionsWindow()
1602 void FFOptionsWindow::create_objects()
1604 int xs8 = xS(8), xs10 = xS(10);
1606 lock_window("FFOptionsWindow::create_objects");
1608 const char *format_name = dialog->cfg_window->format_name;
1609 const char *codec_name = dialog->cfg_window->codec_name;
1610 int x0 = xs10, y0 = ys10;
1612 add_subwindow(title = new BC_Title(x, y, _("Format: ")));
1613 x += title->get_w();
1614 add_subwindow(new BC_Title(x, y, format_name));
1617 add_subwindow(title = new BC_Title(x, y, _("Codec: ")));
1618 x += title->get_w();
1619 add_subwindow(new BC_Title(x, y, codec_name));
1621 x = x0; y += title->get_h() + ys10; y0 = y;
1622 add_subwindow(title = new BC_Title(x, y, _("Type: ")));
1623 x += title->get_w() + xs8;
1624 add_subwindow(type = new BC_Title(x, y, (char *)""));
1626 add_subwindow(title = new BC_Title(x, y, _("Range: ")));
1627 x += title->get_w() + xs8;
1628 add_subwindow(range = new BC_Title(x, y, (char *)""));
1630 x = x0; y += title->get_h() + ys10;
1631 add_subwindow(units = new FFOptionsUnits(this, x, y, xS(120)));
1632 x += units->get_w() + xs8;
1633 int x1 = get_w() - BC_GenericButton::calculate_w(this, _("Apply")) - xs8;
1634 add_subwindow(text = new FFOptionsText(this, x, y, x1-x - xs8));
1635 add_subwindow(apply = new FFOptionsApply(this, x1, y));
1636 y += units->get_h() + ys10;
1638 add_subwindow(kind = new FFOptionsKind(this, x1, y0, apply->get_w()));
1639 kind->create_objects();
1640 const char *kind_text = _("Kind:");
1641 x1 -= BC_Title::calculate_w(this, kind_text) + xs8;
1642 add_subwindow(kind_title = new BC_Title(x1, y0, kind_text));
1645 panel_x = x0; panel_y = y0;
1646 panel_w = get_w()-xs10 - panel_x;
1647 panel_h = get_h()-ys10 - panel_y - BC_OKButton::calculate_h();
1648 panel = new FFOptions_OptPanel(this, panel_x, panel_y, panel_w, panel_h);
1649 add_subwindow(panel);
1650 add_subwindow(new BC_OKButton(this));
1651 add_subwindow(new BC_CancelButton(this));
1652 panel->create_objects();
1653 int k = codec_name ? FF_KIND_CODEC : FF_KIND_FORMAT;
1654 options.initialize(this, k);
1662 void FFOptionsWindow::draw()
1667 int FFOptionsWindow::resize_event(int w, int h)
1669 int xs8 = xS(8), xs10 = xS(10);
1672 int x0 = w - xs8 - kind->get_w();
1673 int y0 = kind->get_y();
1674 kind->reposition_window(x0, y0);
1675 x0 -= kind_title->get_w() + xs8;
1676 kind_title->reposition_window(x0, y0);
1678 int x1 = get_w() - apply->get_w() - xs8;
1679 int y1 = units->get_y();
1680 apply->reposition_window(x1, y1);
1681 int x0 = units->get_x() + units->get_w() + xs8;
1682 int y0 = units->get_y();
1683 text->reposition_window(x0,y0, x1-x0-xs8);
1684 panel_w = get_w()-xs10 - panel_x;
1685 panel_h = get_h()-ys10 - panel_y - BC_OKButton::calculate_h();
1686 panel->reposition_window(panel_x,panel_y, panel_w, panel_h);
1690 FFOptionsDialog::FFOptionsDialog(FFMPEGConfigWindow *cfg_window)
1693 this->cfg_window = cfg_window;
1698 FFOptionsDialog::~FFOptionsDialog()
1701 av_dict_free(&ff_opts);
1704 void FFOptionsDialog::load_options(const char *bp, int len)
1706 av_dict_free(&ff_opts);
1707 char line[BCTEXTLEN];
1708 char key[BCSTRLEN], val[BCTEXTLEN];
1709 const char *dp = bp + len-1;
1711 while( bp < dp && *bp != 0 ) {
1713 char *cp = line, *ep = cp+sizeof(line)-1;
1714 while( *bp && cp<ep && (*cp=*bp++)!='\n' ) ++cp;
1716 if( line[0] == '#' ) {
1717 sprintf(key,"#%d", no);
1718 av_dict_set(&ff_opts, key, line, 0);
1721 if( line[0] == '\n' ) continue;
1722 if( FFMPEG::scan_option_line(line, key, val) ) continue;
1723 av_dict_set(&ff_opts, key, val, 0);
1727 void FFOptionsDialog::store_options(char *cp, int len)
1729 char *ep = cp + len-1;
1730 AVDictionaryEntry *elem = 0;
1731 while( (elem=av_dict_get(ff_opts, "", elem, AV_DICT_IGNORE_SUFFIX)) != 0 ) {
1732 if( elem->key[0] == '#' ) {
1733 cp += snprintf(cp,ep-cp, "%s\n", elem->value);
1736 cp += snprintf(cp,ep-cp, "%s=%s\n", elem->key, elem->value);
1742 void FFOptionsDialog::start()
1744 if( options_window ) {
1745 options_window->lock_window("FFOptionsDialog::start");
1746 options_window->raise_window();
1747 options_window->unlock_window();
1750 cfg_window->get_pop_cursor(wx, wy);
1751 cfg_window->read_options();
1752 BC_DialogThread::start();
1755 BC_Window* FFOptionsDialog::new_gui()
1757 options_window = new FFOptionsWindow(this, wx, wy);
1758 options_window->create_objects();
1759 return options_window;
1762 void FFOptionsDialog::handle_done_event(int result)
1765 cfg_window->lock_window("FFMPEGConfigFormat::save_options");
1766 cfg_window->save_options();
1767 cfg_window->unlock_window();
1772 FFOptionsAudioDialog::FFOptionsAudioDialog(FFMPEGConfigAudio *aud_config)
1773 : FFOptionsDialog(aud_config)
1775 this->aud_config = aud_config;
1778 FFOptionsAudioDialog::~FFOptionsAudioDialog()
1783 void FFOptionsAudioDialog::update_options(const char *options)
1785 aud_config->lock_window("FFOptionsAudioDialog::update_options");
1786 aud_config->audio_options->update(options);
1787 aud_config->unlock_window();
1790 FFOptionsVideoDialog::FFOptionsVideoDialog(FFMPEGConfigVideo *vid_config)
1791 : FFOptionsDialog(vid_config)
1793 this->vid_config = vid_config;
1796 FFOptionsVideoDialog::~FFOptionsVideoDialog()
1801 void FFOptionsVideoDialog::update_options(const char *options)
1803 vid_config->lock_window("FFOptionsVideoDialog::update_options");
1804 vid_config->video_options->update(options);
1805 vid_config->unlock_window();
1808 FFOptionsFormatDialog::FFOptionsFormatDialog(FFMPEGConfigFormat *fmt_config)
1809 : FFOptionsDialog(fmt_config)
1811 this->fmt_config = fmt_config;
1814 FFOptionsFormatDialog::~FFOptionsFormatDialog()
1819 void FFOptionsFormatDialog::update_options(const char *options)
1821 fmt_config->lock_window("FFOptionsFormatDialog::update_options");
1822 fmt_config->format_options->update(options);
1823 fmt_config->unlock_window();
1827 FFOptionsViewAudio::FFOptionsViewAudio(FFMPEGConfigAudio *aud_config,
1828 int x, int y, const char *text)
1829 : BC_GenericButton(x, y, text)
1831 this->aud_config = aud_config;
1835 FFOptionsViewAudio::~FFOptionsViewAudio()
1837 avcodec_free_context(&avctx);
1840 int FFOptionsViewAudio::handle_event()
1843 Asset *asset = aud_config->asset;
1844 const char *name = asset->acodec;
1845 char audio_format[BCSTRLEN]; audio_format[0] = 0;
1846 char audio_codec[BCSTRLEN]; audio_codec[0] = 0;
1847 AVCodec *codec = !ret &&
1848 !FFMPEG::get_format(audio_format, "audio", name) &&
1849 !FFMPEG::get_codec(audio_codec, "audio", name) ?
1850 avcodec_find_encoder_by_name(audio_codec) : 0;
1851 if( !ret && !codec ) {
1852 eprintf(_("no codec named: %s: %s"), name, audio_codec);
1855 avcodec_free_context(&avctx);
1856 if( !ret && !(avctx = avcodec_alloc_context3(codec)) ) {
1857 eprintf(_("no codec context: %s: %s"), name, audio_codec);
1861 aud_config->start(avctx);
1865 FFOptionsViewVideo::FFOptionsViewVideo(FFMPEGConfigVideo *vid_config,
1866 int x, int y, const char *text)
1867 : BC_GenericButton(x, y, text)
1869 this->vid_config = vid_config;
1873 FFOptionsViewVideo::~FFOptionsViewVideo()
1875 avcodec_free_context(&avctx);
1878 int FFOptionsViewVideo::handle_event()
1881 Asset *asset = vid_config->asset;
1882 const char *name = asset->vcodec;
1883 char video_format[BCSTRLEN]; video_format[0] = 0;
1884 char video_codec[BCSTRLEN]; video_codec[0] = 0;
1885 AVCodec *codec = !ret &&
1886 !FFMPEG::get_format(video_format, "video", name) &&
1887 !FFMPEG::get_codec(video_codec, "video", name) ?
1888 avcodec_find_encoder_by_name(video_codec) : 0;
1889 if( !ret && !codec ) {
1890 eprintf(_("no codec named: %s: %s"), name, video_codec);
1893 avcodec_free_context(&avctx);
1894 if( !ret && !(avctx = avcodec_alloc_context3(codec)) ) {
1895 eprintf(_("no codec context: %s: %s"), name, video_codec);
1900 vid_config->start(avctx);
1904 FFOptionsViewFormat::FFOptionsViewFormat(FFMPEGConfigWindow *cfg_window,
1905 EDL *edl, Asset *asset, int x, int y, const char *text)
1906 : BC_GenericButton(x, y, text)
1908 this->cfg_window = cfg_window;
1910 this->asset = asset;
1914 FFOptionsViewFormat::~FFOptionsViewFormat()
1916 delete format_dialog;
1919 int FFOptionsViewFormat::handle_event()
1921 delete format_dialog;
1923 get_pop_cursor(wx, wy);
1924 format_dialog = new FFOptionsFormatViewDialog(this, wx, wy);
1925 format_dialog->start();
1930 FFOptionsFormatView::FFOptionsFormatView(FFMPEGConfigFormat *fmt_config,
1931 int x, int y, const char *text)
1932 : BC_GenericButton(x, y, text)
1934 this->fmt_config = fmt_config;
1938 FFOptionsFormatView::~FFOptionsFormatView()
1940 avformat_free_context(fmt_ctx);
1943 int FFOptionsFormatView::handle_event()
1945 Asset *asset = fmt_config->asset;
1946 char *format_name = asset->fformat;
1947 char replace_name0[] = "mov";
1948 char replace_name1[] = "mpegts";
1949 char replace_name2[] = "matroska";
1950 if (!strcmp(format_name, "qt"))
1951 format_name = replace_name0; // fixup
1952 if (!strcmp(format_name, "m2ts"))
1953 format_name = replace_name1; // fixup
1954 if (!strcmp(format_name, "mkv"))
1955 format_name = replace_name2; // fixup
1956 avformat_free_context(fmt_ctx); fmt_ctx = 0;
1957 int ret = avformat_alloc_output_context2(&fmt_ctx, 0, format_name, 0);
1958 if( ret || !fmt_ctx ) {
1959 eprintf(_("no format named: %s"), format_name);
1963 fmt_config->start(fmt_ctx);
1967 FFOptionsFormatViewDialog::FFOptionsFormatViewDialog(FFOptionsViewFormat *view_format,
1970 this->view_format = view_format;
1976 FFOptionsFormatViewDialog::~FFOptionsFormatViewDialog()
1981 BC_Window *FFOptionsFormatViewDialog::new_gui()
1983 cfg_window = new FFMPEGConfigFormat(this, wx, wy,
1984 view_format->asset, view_format->edl);
1985 cfg_window->create_objects();
1986 cfg_window->read_options();
1990 void FFOptionsFormatViewDialog::handle_done_event(int result)
1993 cfg_window->save_changes();