add new boxblur plugin, mods to videoscope, fix segv for menu btns kfrm-tweak/kfrm...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / fileffmpeg.C
1
2 #include <stdio.h>
3 #include <stdint.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <string.h>
7 // work around for __STDC_CONSTANT_MACROS
8 #include <lzma.h>
9
10 #include "asset.h"
11 #include "bcwindowbase.h"
12 #include "bitspopup.h"
13 #include "ctype.h"
14 #include "edl.h"
15 #include "ffmpeg.h"
16 #include "filebase.h"
17 #include "file.h"
18 #include "fileffmpeg.h"
19 #include "filesystem.h"
20 #include "indexfile.h"
21 #include "interlacemodes.h"
22 #include "language.h"
23 #include "mainerror.h"
24 #include "mainprogress.h"
25 #include "mutex.h"
26 #include "preferences.h"
27 #include "videodevice.inc"
28
29 #ifdef FFMPEG3
30 #define url filename
31 #endif
32
33 FileFFMPEG::FileFFMPEG(Asset *asset, File *file)
34  : FileBase(asset, file)
35 {
36         ff = 0;
37         if(asset->format == FILE_UNKNOWN)
38                 asset->format = FILE_FFMPEG;
39 }
40
41 FileFFMPEG::~FileFFMPEG()
42 {
43         delete ff;
44 }
45
46
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))
50 {
51         this->window = window;
52         this->x = x;  this->y = y;
53         this->title_text = title_text;
54         this->output = output;
55 }
56
57 FFMpegConfigNum::~FFMpegConfigNum()
58 {
59 }
60
61 void FFMpegConfigNum::create_objects()
62 {
63         window->add_subwindow(title = new BC_Title(x, y, title_text));
64         BC_TumbleTextBox::create_objects();
65 }
66
67 int FFMpegConfigNum::update_param(const char *param, const char *opts)
68 {
69         char value[BCSTRLEN];
70         if( !FFMPEG::get_ff_option(param, opts, value) ) {
71                 if( (*output = atoi(value)) < 0 ) {
72                         disable(1);
73                         return 0;
74                 }
75         }
76         BC_TumbleTextBox::update((int64_t)*output);
77         enable();
78         return 1;
79 }
80
81 int FFMpegConfigNum::handle_event()
82 {
83         *output = atoi(get_text());
84         return 1;
85 }
86
87 FFMpegAudioNum::FFMpegAudioNum(BC_Window *window,
88                 int x, int y, char *title_text, int *output)
89  : FFMpegConfigNum(window, x, y, title_text, output)
90 {
91 }
92
93 int FFMpegAudioBitrate::handle_event()
94 {
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();
101         return ret;
102 }
103
104 int FFMpegAudioQuality::handle_event()
105 {
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();
112         return ret;
113 }
114
115 FFMpegVideoNum::FFMpegVideoNum(BC_Window *window,
116                 int x, int y, char *title_text, int *output)
117  : FFMpegConfigNum(window, x, y, title_text, output)
118 {
119 }
120
121 int FFMpegVideoBitrate::handle_event()
122 {
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();
129         return ret;
130 }
131
132 int FFMpegVideoQuality::handle_event()
133 {
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();
140         return ret;
141 }
142
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)
146 {
147         this->vid_config = vid_config;
148 }
149
150 int FFMpegPixelFormat::handle_event()
151 {
152         strncpy(vid_config->asset->ff_pixel_format, get_text(),
153                 sizeof(vid_config->asset->ff_pixel_format));
154         return 1;
155 }
156
157 void FFMpegPixelFormat::update_formats()
158 {
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;
165         if( pix_fmts ) {
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));
169                 }
170         }
171         update_list(&pixfmts);
172 }
173
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)
177 {
178         this->aud_config = aud_config;
179 }
180
181 int FFMpegSampleFormat::handle_event()
182 {
183         strncpy(aud_config->asset->ff_sample_format, get_text(),
184                 sizeof(aud_config->asset->ff_sample_format));
185         return 1;
186 }
187
188 void FFMpegSampleFormat::update_formats()
189 {
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;
196         if( sample_fmts ) {
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));
200                 }
201         }
202         update_list(&samplefmts);
203 }
204
205 void FileFFMPEG::set_options(char *cp, int len, const char *bp)
206 {
207         char *ep = cp + len-2, ch = 0;
208         while( cp < ep && *bp != 0 ) { ch = *bp++; *cp++ = ch; }
209         if( ch != '\n' ) *cp++ = '\n';
210         *cp = 0;
211 }
212
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)
216 {
217         Asset *ff_asset = new Asset();
218         ff_asset->copy_from(asset, 0);
219         int wx, wy;
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());
231                 }
232                 delete window;
233         }
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());
244                 }
245                 delete window;
246         }
247         ff_asset->remove_user();
248 }
249
250 int FileFFMPEG::check_sig(Asset *asset)
251 {
252         char *ptr = strstr(asset->path, ".pcm");
253         if( ptr ) return 0;
254         ptr = strstr(asset->path, ".raw");
255         if( ptr ) return 0;
256
257         FFMPEG ffmpeg(0);
258         int ret = !ffmpeg.init_decoder(asset->path) &&
259                 !ffmpeg.open_decoder() ? 1 : 0;
260         return ret;
261 }
262
263 void FileFFMPEG::get_info(char *path, char *text, int len)
264 {
265         char *cp = text;
266         FFMPEG ffmpeg(0);
267         cp += sprintf(cp, _("file path: %s\n"), path);
268         struct stat st;
269         int ret = 0;
270         if( stat(path, &st) < 0 ) {
271                 cp += sprintf(cp, _(" err: %s\n"), strerror(errno));
272                 ret = 1;
273         }
274         else {
275                 cp += sprintf(cp, _("  %jd bytes\n"), st.st_size);
276         }
277         if( !ret ) ret = ffmpeg.init_decoder(path);
278         if( !ret ) ret = ffmpeg.open_decoder();
279         if( !ret ) {
280                 cp += sprintf(cp, _("info:\n"));
281                 ffmpeg.info(cp, len-(cp-text));
282         }
283         else
284                 sprintf(cp, _("== open failed\n"));
285 }
286
287 int FileFFMPEG::get_video_info(int track, int &pid, double &framerate,
288                 int &width, int &height, char *title)
289 {
290         if( !ff ) return -1;
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;
296         return 0;
297 }
298
299 int FileFFMPEG::get_audio_for_video(int vstream, int astream, int64_t &channel_mask)
300 {
301         if( !ff ) return 1;
302         return ff->ff_audio_for_video(vstream, astream, channel_mask);
303 }
304
305 int FileFFMPEG::select_video_stream(Asset *asset, int vstream)
306 {
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);
313         return 0;
314 }
315
316 int FileFFMPEG::select_audio_stream(Asset *asset, int astream)
317 {
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);
321         return 0;
322 }
323
324 int FileFFMPEG::open_file(int rd, int wr)
325 {
326         int result = 0;
327         if( ff ) return 1;
328         ff = new FFMPEG(this);
329
330         if( rd ) {
331                 result = ff->init_decoder(asset->path);
332                 if( !result ) result = ff->open_decoder();
333                 if( !result ) {
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));
341                         }
342                         int video_layers = ff->ff_total_video_layers();
343                         if( video_layers > 0 ) {
344                                 asset->video_data = 1;
345                                 if( !asset->layers ) asset->layers = video_layers;
346                                 asset->actual_width = ff->ff_video_width(0);
347                                 asset->actual_height = ff->ff_video_height(0);
348                                 if( !asset->width ) asset->width = asset->actual_width;
349                                 if( !asset->height ) asset->height = asset->actual_height;
350                                 if( !asset->video_length &&
351                                     (asset->video_length = ff->ff_video_frames(0)) < 2 )
352                                         asset->video_length = asset->video_length < 0 ? 0 : -1;
353                                 if( !asset->frame_rate ) asset->frame_rate = ff->ff_frame_rate(0);
354                                 if( asset->ff_color_range < 0 )
355                                         asset->ff_color_range = ff->ff_color_range(0);
356                                 if( asset->ff_color_space < 0 )
357                                         asset->ff_color_space = ff->ff_color_space(0);
358                                 strcpy(asset->vcodec, ff->ff_video_codec(0));
359                         }
360                         IndexState *index_state = asset->index_state;
361                         index_state->read_markers(file->preferences->index_directory, asset->path);
362                 }
363         }
364         else if( wr ) {
365                 result = ff->init_encoder(asset->path);
366                 // must be in this order or dvdauthor will fail
367                 if( !result && asset->video_data )
368                         result = ff->open_encoder("video", asset->vcodec);
369                 if( !result && asset->audio_data )
370                         result = ff->open_encoder("audio", asset->acodec);
371         }
372         return result;
373 }
374
375 int FileFFMPEG::close_file()
376 {
377         delete ff;
378         ff = 0;
379         return 0;
380 }
381
382
383 int FileFFMPEG::write_samples(double **buffer, int64_t len)
384 {
385         if( !ff || len < 0 ) return -1;
386         int stream = 0;
387         int ret = ff->encode(stream, buffer, len);
388         return ret;
389 }
390
391 int FileFFMPEG::write_frames(VFrame ***frames, int len)
392 {
393         if( !ff ) return -1;
394         int ret = 0, layer = 0;
395         for(int i = 0; i < 1; i++) {
396                 for(int j = 0; j < len && !ret; j++) {
397                         VFrame *frame = frames[i][j];
398                         ret = ff->encode(layer, frame);
399                 }
400         }
401         return ret;
402 }
403
404
405 int FileFFMPEG::read_samples(double *buffer, int64_t len)
406 {
407         if( !ff || len < 0 ) return -1;
408         int ch = file->current_channel;
409         int64_t pos = file->current_sample;
410         int ret = ff->decode(ch, pos, buffer, len);
411         if( ret > 0 ) return 0;
412         memset(buffer,0,len*sizeof(*buffer));
413         return -1;
414 }
415
416 int FileFFMPEG::read_frame(VFrame *frame)
417 {
418         if( !ff ) return -1;
419         int layer = file->current_layer;
420         int64_t pos = asset->video_length >= 0 ? file->current_frame : 0;
421         int ret = ff->decode(layer, pos, frame);
422         frame->set_status(ret);
423         if( ret >= 0 ) return 0;
424         frame->clear_frame();
425         return -1;
426 }
427
428
429 int64_t FileFFMPEG::get_memory_usage()
430 {
431         return 0;
432 }
433
434 int FileFFMPEG::colormodel_supported(int colormodel)
435 {
436         return colormodel;
437 }
438
439
440 int FileFFMPEG::get_best_colormodel(int driver, int vstream)
441 {
442         if( vstream < 0 ) vstream = 0;
443         int is_mpeg = !ff ? 0 : ff->ff_video_mpeg_color_range(vstream);
444
445         switch(driver) {
446         case PLAYBACK_X11:
447         case PLAYBACK_X11_GL: return is_mpeg ? BC_YUV888 : BC_RGB888;
448         case PLAYBACK_X11_XV: return BC_YUV420P;
449         }
450
451         return BC_RGB888;
452 }
453
454 int FileFFMPEG::get_best_colormodel(Asset *asset, int driver)
455 {
456         switch(driver) {
457 // the direct X11 color model requires scaling in the codec
458         case SCREENCAPTURE:
459         case PLAYBACK_X11:
460         case PLAYBACK_X11_GL: return BC_RGB888;
461         case PLAYBACK_X11_XV: return BC_YUV420P;
462         }
463
464         return BC_YUV420P;
465 }
466
467
468 FFMPEGConfigWindow::FFMPEGConfigWindow(const char *title,
469                 BC_WindowBase *parent_window,
470                 int x, int y, int w, int h,
471                 Asset *asset, EDL *edl)
472  : BC_Window(title, x, y, w, h)
473 {
474         this->parent_window = parent_window;
475         this->asset = asset;
476         this->edl = edl;
477         avctx = 0;
478         fmt_ctx = 0;
479         ff_options_dialog = 0;
480         format_name = 0;
481         codec_name = 0;
482 }
483
484 FFMPEGConfigWindow::~FFMPEGConfigWindow()
485 {
486         delete ff_options_dialog;
487 }
488
489 void FFMPEGConfigWindow::start(AVCodecContext *avctx)
490 {
491         this->avctx = avctx;
492         ff_options_dialog->start();
493 }
494
495 void FFMPEGConfigWindow::start(AVFormatContext *fmt_ctx)
496 {
497         this->fmt_ctx = fmt_ctx;
498         ff_options_dialog->start();
499 }
500
501 //======
502
503 FFMPEGConfigAudio::FFMPEGConfigAudio(BC_WindowBase *parent_window,
504                 int x, int y, Asset *asset, EDL *edl)
505  : FFMPEGConfigWindow(_(PROGRAM_NAME ": Audio Preset"), parent_window, x, y,
506                 xS(420), yS(420), asset, edl)
507 {
508         preset_popup = 0;
509         bitrate = 0;
510         audio_options = 0;
511         format_name = asset->fformat;
512         codec_name = asset->acodec;
513 }
514
515 FFMPEGConfigAudio::~FFMPEGConfigAudio()
516 {
517         lock_window("FFMPEGConfigAudio::~FFMPEGConfigAudio");
518         delete preset_popup;
519         presets.remove_all_objects();
520         delete audio_options;
521         unlock_window();
522 }
523
524 void FFMPEGConfigAudio::read_options()
525 {
526         const char *options = audio_options->get_text();
527         int options_len = strlen(options);
528         ff_options_dialog->load_options(options, options_len);
529 }
530 void FFMPEGConfigAudio::save_options()
531 {
532         char options[BCTEXTLEN];
533         ff_options_dialog->store_options(options, sizeof(options)-1);
534         audio_options->update(options);
535 }
536
537 void FFMPEGConfigAudio::load_options()
538 {
539         FFMPEG::load_audio_options(asset, edl);
540 }
541
542 void FFMPEGConfigAudio::create_objects()
543 {
544         int x = xS(10), y = yS(10);
545         lock_window("FFMPEGConfigAudio::create_objects");
546
547         FileSystem fs;
548         char option_path[BCTEXTLEN];
549         FFMPEG::set_option_path(option_path, "audio");
550         fs.update(option_path);
551         int total_files = fs.total_files();
552         for(int i = 0; i < total_files; i++) {
553                 const char *name = fs.get_entry(i)->get_name();
554                 if( asset->fformat[0] != 0 ) {
555                         const char *ext = strrchr(name,'.');
556                         if( !ext ) continue;
557                         if( strcmp(asset->fformat, ++ext) ) continue;
558                 }
559                 presets.append(new BC_ListBoxItem(name));
560         }
561
562         if( asset->acodec[0] ) {
563                 int k = presets.size();
564                 while( --k >= 0 && strcmp(asset->acodec, presets[k]->get_text()) );
565                 if( k < 0 ) asset->acodec[0] = 0;
566         }
567
568         if( !asset->acodec[0] && presets.size() > 0 )
569                 strcpy(asset->acodec, presets[0]->get_text());
570
571         add_tool(new BC_Title(x, y, _("Preset:")));
572         y += yS(25);
573         preset_popup = new FFMPEGConfigAudioPopup(this, x, y);
574         preset_popup->create_objects();
575
576         y += yS(50);
577         bitrate = new FFMpegAudioBitrate(this, x, y, _("Bitrate:"), &asset->ff_audio_bitrate);
578         bitrate->create_objects();
579         bitrate->set_increment(1000);
580         bitrate->set_boundaries((int64_t)0, (int64_t)INT_MAX);
581         y += bitrate->get_h() + 5;
582         quality = new FFMpegAudioQuality(this, x, y, _("Quality:"), &asset->ff_audio_quality);
583         quality->create_objects();
584         quality->set_increment(1);
585         quality->set_boundaries((int64_t)-1, (int64_t)51);
586         y += quality->get_h() + yS(10);
587
588         add_subwindow(new BC_Title(x, y, _("Samples:")));
589         sample_format = new FFMpegSampleFormat(this, x+xS(90), y, xS(100), yS(120));
590         sample_format->create_objects();
591         if( asset->acodec[0] ) {
592                 sample_format->update_formats();
593                 if( !asset->ff_audio_options[0] )
594                         load_options();
595         }
596         if( !asset->ff_sample_format[0] ) strcpy(asset->ff_sample_format, _("None"));
597         sample_format->update(asset->ff_sample_format);
598         y += sample_format->get_h() + yS(10);
599
600         BC_Title *title = new BC_Title(x, y, _("Audio Options:"));
601         add_subwindow(title);
602
603         ff_options_dialog = new FFOptionsAudioDialog(this);
604         int x1 = x + title->get_w() + xS(8);
605         add_subwindow(view_audio = new FFOptionsViewAudio(this, x1, y, _("view")));
606         x1 += x + view_audio->get_w() + xS(20);
607         view_format = new FFOptionsViewFormat(this, edl, asset, x1, y, _("format"));
608         add_subwindow(view_format);
609
610         y += yS(25);
611         audio_options = new FFAudioOptions(this, x, y, get_w()-x-xS(20), 8,
612                  sizeof(asset->ff_audio_options)-1, asset->ff_audio_options);
613         audio_options->create_objects();
614         add_subwindow(new BC_OKButton(this));
615         add_subwindow(new BC_CancelButton(this));
616         show_window(1);
617
618         bitrate->update_param("cin_bitrate", asset->ff_audio_options);
619         quality->update_param("cin_quality", asset->ff_audio_options);
620
621         if( asset->ff_audio_bitrate > 0 ) quality->disable();
622         else if( asset->ff_audio_quality >= 0 ) bitrate->disable();
623
624         unlock_window();
625 }
626
627 int FFMPEGConfigAudio::close_event()
628 {
629         set_done(1);
630         return 1;
631 }
632
633 FFAudioOptions::FFAudioOptions(FFMPEGConfigAudio *audio_popup,
634         int x, int y, int w, int rows, int size, char *text)
635  : BC_ScrollTextBox(audio_popup, x, y, w, rows, text, size)
636 {
637         this->audio_popup = audio_popup;
638 }
639
640
641 FFMPEGConfigAudioPopup::FFMPEGConfigAudioPopup(FFMPEGConfigAudio *popup, int x, int y)
642  : BC_PopupTextBox(popup, &popup->presets, popup->asset->acodec, x, y, xS(300), yS(300))
643 {
644         this->popup = popup;
645 }
646
647 int FFMPEGConfigAudioPopup::handle_event()
648 {
649         strcpy(popup->asset->acodec, get_text());
650         popup->sample_format->update_formats();
651         Asset *asset = popup->asset;
652         asset->ff_audio_bitrate = 0;  asset->ff_audio_quality = -1;
653         popup->load_options();
654         popup->audio_options->update(asset->ff_audio_options);
655         popup->audio_options->set_text_row(0);
656
657         popup->bitrate->update_param("cin_bitrate", asset->ff_audio_options);
658         popup->quality->update_param("cin_quality", asset->ff_audio_options);
659         popup->sample_format->update(asset->ff_sample_format);
660         return 1;
661 }
662
663 //======
664
665 FFMPEGConfigVideo::FFMPEGConfigVideo(BC_WindowBase *parent_window,
666                 int x, int y, Asset *asset, EDL *edl)
667  : FFMPEGConfigWindow(_(PROGRAM_NAME ": Video Preset"), parent_window, x, y,
668                 xS(420), yS(420), asset, edl)
669 {
670         preset_popup = 0;
671         pixel_format = 0;
672         format_name = asset->fformat;
673         codec_name = asset->vcodec;
674
675         bitrate = 0;
676         quality = 0;
677         video_options = 0;
678 }
679
680 FFMPEGConfigVideo::~FFMPEGConfigVideo()
681 {
682         lock_window("FFMPEGConfigVideo::~FFMPEGConfigVideo");
683         delete preset_popup;
684         delete pixel_format;
685         delete video_options;
686         presets.remove_all_objects();
687         unlock_window();
688 }
689
690 void FFMPEGConfigVideo::read_options()
691 {
692         const char *options = video_options->get_text();
693         int options_len = strlen(options);
694         ff_options_dialog->load_options(options, options_len);
695 }
696 void FFMPEGConfigVideo::save_options()
697 {
698         char options[BCTEXTLEN];
699         ff_options_dialog->store_options(options, sizeof(options)-1);
700         video_options->update(options);
701 }
702
703 void FFMPEGConfigVideo::load_options()
704 {
705         FFMPEG::load_video_options(asset, edl);
706 }
707
708 void FFMPEGConfigVideo::create_objects()
709 {
710         int x = xS(10), y = yS(10);
711         lock_window("FFMPEGConfigVideo::create_objects");
712
713         add_subwindow(new BC_Title(x, y, _("Compression:")));
714         y += yS(25);
715
716         FileSystem fs;
717         char option_path[BCTEXTLEN];
718         FFMPEG::set_option_path(option_path, "video");
719         fs.update(option_path);
720         int total_files = fs.total_files();
721         for(int i = 0; i < total_files; i++) {
722                 const char *name = fs.get_entry(i)->get_name();
723                 if( asset->fformat[0] != 0 ) {
724                         const char *ext = strrchr(name,'.');
725                         if( !ext ) continue;
726                         if( strcmp(asset->fformat, ++ext) ) continue;
727                 }
728                 presets.append(new BC_ListBoxItem(name));
729         }
730
731         if( asset->vcodec[0] ) {
732                 int k = presets.size();
733                 while( --k >= 0 && strcmp(asset->vcodec, presets[k]->get_text()) );
734                 if( k < 0 ) asset->vcodec[0] = 0;
735         }
736
737         if( !asset->vcodec[0] && presets.size() > 0 )
738                 strcpy(asset->vcodec, presets[0]->get_text());
739
740         preset_popup = new FFMPEGConfigVideoPopup(this, x, y);
741         preset_popup->create_objects();
742
743         if( asset->ff_video_bitrate > 0 && asset->ff_video_quality >= 0 ) {
744                 asset->ff_video_bitrate = 0;  asset->ff_video_quality = -1;
745         }
746
747         y += yS(50);
748         bitrate = new FFMpegVideoBitrate(this, x, y, _("Bitrate:"), &asset->ff_video_bitrate);
749         bitrate->create_objects();
750         bitrate->set_increment(100000);
751         bitrate->set_boundaries((int64_t)0, (int64_t)INT_MAX);
752         y += bitrate->get_h() + 5;
753         quality = new FFMpegVideoQuality(this, x, y, _("Quality:"), &asset->ff_video_quality);
754         quality->create_objects();
755         quality->set_increment(1);
756         quality->set_boundaries((int64_t)-1, (int64_t)51);
757         y += quality->get_h() + yS(10);
758
759         add_subwindow(new BC_Title(x, y, _("Pixels:")));
760         pixel_format = new FFMpegPixelFormat(this, x+xS(90), y, xS(100), yS(120));
761         pixel_format->create_objects();
762         if( asset->vcodec[0] ) {
763                 pixel_format->update_formats();
764                 if( !asset->ff_video_options[0] )
765                         load_options();
766         }
767         if( !asset->ff_pixel_format[0] ) strcpy(asset->ff_pixel_format, _("None"));
768         pixel_format->update(asset->ff_pixel_format);
769         y += pixel_format->get_h() + yS(10);
770
771         BC_Title *title = new BC_Title(x, y, _("Video Options:"));
772         add_subwindow(title);
773
774         ff_options_dialog = new FFOptionsVideoDialog(this);
775         int x1 = x + title->get_w() + 8;
776         add_subwindow(view_video = new FFOptionsViewVideo(this, x1, y, _("view")));
777         x1 += x + view_video->get_w() + xS(20);
778         view_format = new FFOptionsViewFormat(this, edl, asset, x1, y, _("format"));
779         add_subwindow(view_format);
780
781         y += yS(25);
782         video_options = new FFVideoOptions(this, x, y, get_w()-x-xS(20), 8,
783                  sizeof(asset->ff_video_options)-1, asset->ff_video_options);
784         video_options->create_objects();
785         add_subwindow(new BC_OKButton(this));
786         add_subwindow(new BC_CancelButton(this));
787         show_window(1);
788
789         bitrate->update_param("cin_bitrate", asset->ff_video_options);
790         quality->update_param("cin_quality", asset->ff_video_options);
791
792         if( asset->ff_video_bitrate > 0 ) quality->disable();
793         else if( asset->ff_video_quality >= 0 ) bitrate->disable();
794         unlock_window();
795 }
796
797 int FFMPEGConfigVideo::close_event()
798 {
799         set_done(1);
800         return 1;
801 }
802
803 FFVideoOptions::FFVideoOptions(FFMPEGConfigVideo *video_popup,
804         int x, int y, int w, int rows, int size, char *text)
805  : BC_ScrollTextBox(video_popup, x, y, w, rows, text, size)
806 {
807         this->video_popup = video_popup;
808 }
809
810
811 FFMPEGConfigVideoPopup::FFMPEGConfigVideoPopup(FFMPEGConfigVideo *popup, int x, int y)
812  : BC_PopupTextBox(popup, &popup->presets, popup->asset->vcodec, x, y, xS(300), yS(300))
813 {
814         this->popup = popup;
815 }
816
817 int FFMPEGConfigVideoPopup::handle_event()
818 {
819         strcpy(popup->asset->vcodec, get_text());
820         popup->pixel_format->update_formats();
821         Asset *asset = popup->asset;
822         asset->ff_video_bitrate = 0;  asset->ff_video_quality = -1;
823         popup->load_options();
824         popup->video_options->update(asset->ff_video_options);
825         popup->video_options->set_text_row(0);
826
827         popup->bitrate->update_param("cin_bitrate", asset->ff_video_options);
828         popup->quality->update_param("cin_quality", asset->ff_video_options);
829         popup->pixel_format->update(asset->ff_pixel_format);
830         return 1;
831 }
832
833 //======
834
835 FFMPEGConfigFormat::FFMPEGConfigFormat(FFOptionsFormatViewDialog *view_dialog,
836                 int x, int y, Asset *asset, EDL *edl)
837  : FFMPEGConfigWindow(_(PROGRAM_NAME ": Format Preset"),
838                 view_dialog->view_format, x, y, xS(420), yS(300), asset, edl)
839 {
840         this->view_dialog = view_dialog;
841         format_options = 0;
842         format_name = asset->fformat;
843         codec_name = 0;
844 }
845
846 FFMPEGConfigFormat::~FFMPEGConfigFormat()
847 {
848         lock_window("FFMPEGConfigFormat::~FFMPEGConfigFormat");
849         delete format_options;
850         unlock_window();
851 }
852
853 void FFMPEGConfigFormat::read_options()
854 {
855         const char *options = format_options->get_text();
856         int options_len = strlen(options);
857         ff_options_dialog->load_options(options, options_len);
858 }
859 void FFMPEGConfigFormat::save_options()
860 {
861         char options[BCTEXTLEN];
862         ff_options_dialog->store_options(options, sizeof(options)-1);
863         format_options->update(options);
864 }
865 void FFMPEGConfigFormat::save_changes()
866 {
867         read_options();
868         char *options = asset->ff_format_options;
869         int options_len = sizeof(asset->ff_format_options)-1;
870         ff_options_dialog->store_options(options, options_len);
871 }
872
873 void FFMPEGConfigFormat::load_options()
874 {
875         Asset *asset = view_dialog->view_format->asset;
876         EDL *edl = view_dialog->view_format->edl;
877         FFMPEG::load_format_options(asset, edl);
878 }
879
880 void FFMPEGConfigFormat::create_objects()
881 {
882         int x = xS(10), y = yS(10);
883         lock_window("FFMPEGConfigFormat::create_objects");
884         Asset *asset = view_dialog->view_format->asset;
885         BC_Title *title;
886         add_subwindow(title = new BC_Title(x, y, _("Format:")));
887         int x1 = x + title->get_w() + 8;
888         add_subwindow(new BC_Title(x1, y, asset->fformat));
889         y += yS(25);
890
891         add_subwindow(title = new BC_Title(x, y, _("Format Options:")));
892
893         ff_options_dialog = new FFOptionsFormatDialog(this);
894         x1 = x + title->get_w() + 8;
895         add_subwindow(new FFOptionsFormatView(this, x1, y, _("view")));
896
897         y += yS(25);
898         format_options = new FFFormatOptions(this, x, y, get_w()-x-xS(20), 8,
899                  sizeof(asset->ff_format_options)-1, asset->ff_format_options);
900         format_options->create_objects();
901         add_subwindow(new BC_OKButton(this));
902         add_subwindow(new BC_CancelButton(this));
903         show_window(1);
904         unlock_window();
905 }
906
907 int FFMPEGConfigFormat::close_event()
908 {
909         set_done(1);
910         return 1;
911 }
912
913 FFFormatOptions::FFFormatOptions(FFMPEGConfigFormat *format_popup,
914         int x, int y, int w, int rows, int size, char *text)
915  : BC_ScrollTextBox(format_popup, x, y, w, rows, text, size)
916 {
917         this->format_popup = format_popup;
918 }
919
920 //======
921
922 FFMPEGScanProgress::FFMPEGScanProgress(IndexFile *index_file, MainProgressBar *progress_bar,
923                 const char *title, int64_t length, int64_t *position, int *canceled)
924  : Thread(1, 0, 0)
925 {
926         this->index_file = index_file;
927         this->progress_bar = progress_bar;
928         strcpy(this->progress_title, title);
929         this->length = length;
930         this->position = position;
931         this->canceled = canceled;
932         done = 0;
933         start();
934 }
935
936 FFMPEGScanProgress::~FFMPEGScanProgress()
937 {
938         done = 1;
939         cancel();
940         join();
941 }
942
943 void FFMPEGScanProgress::run()
944 {
945         while( !done ) {
946                 if( progress_bar->update(*position) ) {
947                         *canceled = done = 1;
948                         break;
949                 }
950                 index_file->redraw_edits(0);
951                 usleep(500000);
952         }
953 }
954
955 int FileFFMPEG::get_index(IndexFile *index_file, MainProgressBar *progress_bar)
956 {
957         if( !ff ) return -1;
958         if( !file->preferences->ffmpeg_marker_indexes ) return 1;
959
960         IndexState *index_state = index_file->get_state();
961         if( index_state->index_status != INDEX_NOTTESTED ) return 0;
962         index_state->reset_index();
963         index_state->reset_markers();
964         index_state->index_status = INDEX_BUILDING;
965
966         for( int aidx=0; aidx<ff->ffaudio.size(); ++aidx ) {
967                 FFAudioStream *aud = ff->ffaudio[aidx];
968                 index_state->add_audio_stream(aud->channels, aud->length);
969         }
970
971         FileSystem fs;
972         int64_t file_bytes = fs.get_size(ff->fmt_ctx->url);
973         char *index_path = index_file->index_filename;
974
975         int canceled = 0;
976         int64_t scan_position = 0;
977         FFMPEGScanProgress *scan_progress = 0;
978         if( progress_bar ) {
979                 char progress_title[BCTEXTLEN];
980                 sprintf(progress_title, _("Creating %s\n"), index_path);
981                 progress_bar->update_title(progress_title, 1);
982                 progress_bar->update_length(file_bytes);
983                 scan_progress = new FFMPEGScanProgress(index_file,
984                                 progress_bar, progress_title, file_bytes,
985                                 &scan_position, &canceled);
986         }
987
988         index_state->index_bytes = file_bytes;
989         index_state->init_scan(file->preferences->index_size);
990
991         if( ff->scan(index_state, &scan_position, &canceled) || canceled ) {
992                 index_state->reset_index();
993                 index_state->reset_markers();
994                 canceled = 1;
995         }
996
997         delete scan_progress;
998         if( canceled ) return 1;
999
1000         index_state->marker_status = MARKERS_READY;
1001         return index_state->create_index(index_path, asset);
1002 }
1003
1004
1005 FFOptions_OptPanel::
1006 FFOptions_OptPanel(FFOptionsWindow *fwin, int x, int y, int w, int h)
1007  : BC_ListBox(x, y, w, h, LISTBOX_TEXT), opts(items[0]), vals(items[1])
1008 {
1009         this->fwin = fwin;
1010         update();  // init col/wid/columns
1011 }
1012
1013 FFOptions_OptPanel::
1014 ~FFOptions_OptPanel()
1015 {
1016 }
1017
1018 void FFOptions_OptPanel::create_objects()
1019 {
1020         const char *cols[] = { _("option"), _("value"), };
1021         const int col1_w = xS(150);
1022         int wids[] = { col1_w, get_w()-col1_w };
1023         BC_ListBox::update(&items[0], &cols[0], &wids[0], sizeof(items)/sizeof(items[0]));
1024 }
1025
1026 int FFOptions_OptPanel::update()
1027 {
1028         opts.remove_all();
1029         vals.remove_all();
1030         FFOptions &options = fwin->options;
1031         for( int i=0; i<options.size(); ++i ) {
1032                 FFOptions_Opt *opt = options[i];
1033                 opts.append(opt->item_name);
1034                 vals.append(opt->item_value);
1035         }
1036         draw_items(1);
1037         return 0;
1038 }
1039
1040 int FFOptions_OptPanel::cursor_leave_event()
1041 {
1042         hide_tooltip();
1043         return 0;
1044 }
1045
1046
1047 FFOptions_OptName::FFOptions_OptName(FFOptions_Opt *opt, const char *nm)
1048 {
1049         this->opt = opt;
1050         set_text(nm);
1051 }
1052
1053 FFOptions_OptName::~FFOptions_OptName()
1054 {
1055 }
1056
1057 FFOptions_OptValue::FFOptions_OptValue(FFOptions_Opt *opt)
1058 {
1059         this->opt = opt;
1060 }
1061
1062
1063 void FFOptions_OptValue::update()
1064 {
1065         if( !opt ) return;
1066         char val[BCTEXTLEN];  val[0] = 0;
1067         opt->get(val, sizeof(val));
1068         update(val);
1069 }
1070
1071 void FFOptions_OptValue::update(const char *v)
1072 {
1073         set_text(v);
1074 }
1075
1076 FFOptions_Opt::FFOptions_Opt(FFOptions *options, const AVOption *opt, const char *nm)
1077 {
1078         this->options = options;
1079         this->opt = opt;
1080         item_name = new FFOptions_OptName(this, nm);
1081         item_value = new FFOptions_OptValue(this);
1082 }
1083
1084 FFOptions_Opt::~FFOptions_Opt()
1085 {
1086         delete item_name;
1087         delete item_value;
1088 }
1089
1090 char *FFOptions_Opt::get(char *vp, int sz)
1091 {
1092         char *cp = vp;
1093         *cp = 0;
1094         if( !opt ) return cp;
1095
1096         void *obj = (void *)options->obj;
1097         uint8_t *bp = 0;
1098         if( av_opt_get(obj, opt->name, 0, &bp) >= 0 && bp != 0 ) {
1099                 const char *val = (const char *)bp;
1100                 if( opt->unit && *val ) {
1101                         int id = atoi(val);
1102                         const char *uid = unit_name(id);
1103                         if( uid ) val = uid;
1104                 }
1105                 cp = sz >= 0 ? strncpy(vp,val,sz) : strcpy(vp, val);
1106                 if( sz > 0 ) vp[sz-1] = 0;
1107                 av_freep(&bp);
1108         }
1109
1110         return cp;
1111 }
1112
1113 void FFOptions_Opt::set(const char *val)
1114 {
1115         void *obj = (void *)options->obj;
1116         if( !obj || !opt ) return;
1117         av_opt_set(obj, opt->name, val, 0);
1118 }
1119
1120
1121 FFOptionsKindItem::FFOptionsKindItem(FFOptionsKind *kind, const char *text, int idx)
1122  : BC_MenuItem(text)
1123 {
1124         this->kind = kind;
1125         this->idx = idx;
1126 }
1127
1128 FFOptionsKindItem::~FFOptionsKindItem()
1129 {
1130 }
1131
1132 int FFOptionsKindItem::handle_event()
1133 {
1134         FFOptionsWindow *fwin = kind->fwin;
1135         FFOptions &options = fwin->options;
1136         options.initialize(fwin, idx);
1137         kind->set_text(get_text());
1138         fwin->draw();
1139         return 1;
1140 }
1141
1142 const char *FFOptionsKind::kinds[] = {
1143         N_("codec"),    // FF_KIND_CODEC
1144         N_("format"),   // FF_KIND_FORMAT
1145         N_("ffmpeg"),   // FF_KIND_FFMPEG
1146 };
1147
1148 FFOptionsKind::
1149 FFOptionsKind(FFOptionsWindow *fwin, int x, int y, int w)
1150  : BC_PopupMenu(x, y, w, "")
1151 {
1152         this->fwin = fwin;
1153 }
1154
1155 FFOptionsKind::
1156 ~FFOptionsKind()
1157 {
1158 }
1159
1160 void FFOptionsKind::create_objects()
1161 {
1162         add_item(new FFOptionsKindItem(this, _(kinds[FF_KIND_CODEC]), FF_KIND_CODEC));
1163         add_item(new FFOptionsKindItem(this, _(kinds[FF_KIND_FFMPEG]), FF_KIND_FFMPEG));
1164 }
1165
1166 int FFOptionsKind::handle_event()
1167 {
1168         return 1;
1169 }
1170
1171 void FFOptionsKind::set(int k)
1172 {
1173         this->kind = k;
1174         set_text(_(kinds[k]));
1175 }
1176
1177 FFOptionsText::
1178 FFOptionsText(FFOptionsWindow *fwin, int x, int y, int w)
1179  : BC_TextBox(x, y, w, 1, (char *)"")
1180 {
1181         this->fwin = fwin;
1182 }
1183
1184 FFOptionsText::
1185 ~FFOptionsText()
1186 {
1187 }
1188
1189 int FFOptionsText::handle_event()
1190 {
1191         return 0;
1192 }
1193
1194 FFOptionsUnits::
1195 FFOptionsUnits(FFOptionsWindow *fwin, int x, int y, int w)
1196  : BC_PopupMenu(x, y, w, "")
1197 {
1198         this->fwin = fwin;
1199 }
1200
1201 FFOptionsUnits::
1202 ~FFOptionsUnits()
1203 {
1204 }
1205
1206 int FFOptionsUnits::handle_event()
1207 {
1208         const char *text = get_text();
1209         if( text && fwin->selected ) {
1210                 fwin->selected->set(text);
1211                 fwin->selected->item_value->update();
1212                 av_dict_set(&fwin->dialog->ff_opts,
1213                         fwin->selected->item_name->get_text(),
1214                         fwin->selected->item_value->get_text(), 0);
1215                 fwin->draw();
1216         }
1217         return 1;
1218 }
1219
1220 FFOptionsApply::
1221 FFOptionsApply(FFOptionsWindow *fwin, int x, int y)
1222  : BC_GenericButton(x, y, _("Apply"))
1223 {
1224         this->fwin = fwin;
1225 }
1226
1227 FFOptionsApply::
1228 ~FFOptionsApply()
1229 {
1230 }
1231
1232 int FFOptionsApply::handle_event()
1233 {
1234         const char *text = fwin->text->get_text();
1235         if( text && fwin->selected ) {
1236                 fwin->selected->set(text);
1237                 fwin->selected->item_value->update();
1238                 av_dict_set(&fwin->dialog->ff_opts,
1239                         fwin->selected->item_name->get_text(),
1240                         fwin->selected->item_value->get_text(), 0);
1241                 fwin->draw();
1242         }
1243         return 1;
1244 }
1245
1246 FFOptions::FFOptions()
1247 {
1248         obj = 0;
1249 }
1250
1251 FFOptions::~FFOptions()
1252 {
1253         remove_all_objects();
1254 }
1255
1256 int FFOptions::cmpr(const void *a, const void *b)
1257 {
1258         FFOptions_Opt *ap = *(FFOptions_Opt **)a;
1259         FFOptions_Opt *bp = *(FFOptions_Opt **)b;
1260         const char *vap = ap->item_name->get_text();
1261         const char *vbp = bp->item_name->get_text();
1262         return strcmp(vap, vbp);
1263 }
1264
1265 void FFOptions::initialize(FFOptionsWindow *win, int kind)
1266 {
1267         remove_all_objects();
1268         win->selected = 0;
1269         const void *obj = 0;
1270         switch( kind ) {
1271         case FF_KIND_CODEC:
1272                 obj = (const void *)win->dialog->cfg_window->avctx->priv_data;
1273                 break;
1274         case FF_KIND_FFMPEG:
1275                 obj = (const void *)win->dialog->cfg_window->avctx;
1276                 break;
1277         case FF_KIND_FORMAT:
1278                 obj = (const void *)win->dialog->cfg_window->fmt_ctx->priv_data;
1279                 break;
1280         }
1281         this->win = win;
1282         this->obj = obj;
1283         if( obj ) {
1284                 FFOptions &conf = *this;
1285                 const AVOption *opt = 0;
1286                 while( (opt=av_opt_next(obj, opt)) != 0 ) {
1287                         if( opt->type == AV_OPT_TYPE_CONST ) continue;
1288                         int dupl = 0;
1289                         for( int i=0; !dupl && i<size(); ++i ) {
1290                                 FFOptions_Opt *fopt = conf[i];
1291                                 const AVOption *op = fopt->opt;
1292                                 if( op->offset != opt->offset ) continue;
1293                                 if( op->type != opt->type ) continue;
1294                                 dupl = 1;
1295                                 if( strlen(op->name) < strlen(opt->name) )
1296                                         fopt->opt = opt;
1297                         }
1298                         if( dupl ) continue;
1299                         FFOptions_Opt *fopt = new FFOptions_Opt(this, opt, opt->name);
1300                         append(fopt);
1301                         AVDictionaryEntry *elem = av_dict_get(win->dialog->ff_opts,
1302                                         opt->name, 0, AV_DICT_IGNORE_SUFFIX);
1303                         if( elem && elem->value ) fopt->set(elem->value);
1304                         char val[BCTEXTLEN], *vp = fopt->get(val, sizeof(val));
1305                         fopt->item_value->update(vp);
1306                 }
1307                 qsort(&values[0],size(),sizeof(values[0]),cmpr);
1308         }
1309         win->panel->update();
1310         win->panel->set_yposition(0);
1311 }
1312
1313 int FFOptions::update()
1314 {
1315         int ret = 0;
1316         FFOptions &conf = *this;
1317
1318         for( int i=0; i<size(); ++i ) {
1319                 FFOptions_Opt *fopt = conf[i];
1320                 char val[BCTEXTLEN], *vp = fopt->get(val, sizeof(val));
1321                 if( !vp || !strcmp(val, fopt->item_value->get_text()) ) continue;
1322                 fopt->item_value->update(val);
1323                 ++ret;
1324         }
1325         return ret;
1326 }
1327
1328 void FFOptions::dump(FILE *fp)
1329 {
1330         if( !obj ) return;
1331         const AVOption *opt = 0;
1332         FFOptions &conf = *this;
1333
1334         while( (opt=av_opt_next(obj, opt)) != 0 ) {
1335                 if( opt->type == AV_OPT_TYPE_CONST ) continue;
1336                 int k = size();
1337                 while( --k >= 0 && strcmp(opt->name, conf[k]->opt->name) );
1338                 if( k < 0 ) continue;
1339                 FFOptions_Opt *fopt = conf[k];
1340                 char val[BCTEXTLEN], *vp = fopt->get(val,sizeof(val));
1341                 fprintf(fp, "  %s:=%s", opt->name, vp);
1342                 if( opt->unit ) {
1343                         char unt[BCTEXTLEN], *up = unt;
1344                         fopt->units(up);
1345                         fprintf(fp, "%s", unt);
1346                 }
1347                 fprintf(fp, "\n");
1348         }
1349 }
1350
1351
1352 void FFOptionsWindow::update(FFOptions_Opt *opt)
1353 {
1354         if( selected != opt ) {
1355                 if( selected ) selected->item_name->set_selected(0);
1356                 selected = opt;
1357                 if( selected ) selected->item_name->set_selected(1);
1358         }
1359         clear_box(0,0, 0,panel->get_y());
1360         char str[BCTEXTLEN], *sp;
1361         *(sp=str) = 0;
1362         if( opt ) opt->types(sp);
1363         type->update(str);
1364         *(sp=str) = 0;
1365         if( opt ) opt->ranges(sp);
1366         range->update(str);
1367         while( units->total_items() ) units->del_item(0);
1368         char unit[BCSTRLEN];  strcpy(unit, "()");
1369         if( opt && opt->opt ) {
1370                 ArrayList<const char *> names;
1371                 int n = 0;
1372                 if( opt->opt->unit ) {
1373                         n = opt->units(names);
1374                         if( n > 0 ) strcpy(unit,opt->opt->unit);
1375                 }
1376                 for( int i=0; i<n; ++i )
1377                         units->add_item(new BC_MenuItem(names[i], 0));
1378         }
1379         units->set_text(unit);
1380         char val[BCTEXTLEN];  val[0] = 0;
1381         if( opt ) opt->get(val, sizeof(val));
1382         text->update(val);
1383
1384         panel->update();
1385 }
1386
1387 void FFOptions_OptPanel::show_tip(const char *tip)
1388 {
1389         if( !tip ) return;
1390         int len = strlen(tip);
1391         if( len > (int)sizeof(tip_text)-1 ) len = sizeof(tip_text)-1;
1392         strncpy(tip_text,tip,len);
1393         tip_text[len] = 0;
1394         int line_limit = 60;
1395         int limit2 = line_limit/2;
1396         int limit4 = line_limit/4-2;
1397         char *cp = tip_text, *dp = cp+len;
1398         int n;  char *bp, *ep, *pp, *sp;
1399         while( cp < dp ) {
1400                 for( ep=cp; ep<dp && *ep!='\n'; ++ep );
1401                 // target about half remaining line, constrain line_limit
1402                 if( (n=(ep-1-cp)/2) < limit2 || n > line_limit )
1403                         n = line_limit;
1404                 // search for last punct, last space before line_limit
1405                 for( bp=cp, pp=sp=0; --n>=0 && cp<ep; ++cp ) {
1406                         if( ispunct(*cp) && isspace(*(cp+1)) ) pp = cp;
1407                         else if( isspace(*cp) ) sp = cp;
1408                 }
1409                 // line not empty
1410                 if( cp < ep ) {
1411                         // first, after punctuation
1412                         if( pp && pp-bp >= limit4 )
1413                                 cp = pp+1;
1414                         // then, on spaces
1415                         else if( sp ) {
1416                                 cp = sp;
1417                         }
1418                         // last, on next space
1419                         else {
1420                                 while( cp<dp && !isspace(*cp) ) ++cp;
1421                         }
1422                         // add new line
1423                         if( !*cp ) break;
1424                         *cp++ = '\n';
1425                 }
1426         }
1427         fwin->panel->set_tooltip(tip_text);
1428         fwin->panel->show_tooltip();
1429 }
1430
1431 int FFOptions_OptPanel::selection_changed()
1432 {
1433         FFOptions_Opt *opt = 0;
1434         BC_ListBoxItem *item = get_selection(0, 0);
1435         if( item ) {
1436                 FFOptions_OptName *opt_name = (FFOptions_OptName *)item;
1437                 opt = opt_name->opt;
1438         }
1439         fwin->update(opt);
1440         if( opt ) show_tip(opt->tip());
1441         return 1;
1442 }
1443
1444
1445 int FFOptions_Opt::types(char *rp)
1446 {
1447         const char *cp = "";
1448         if( opt ) switch (opt->type) {
1449         case AV_OPT_TYPE_FLAGS: cp = N_("<flags>");  break;
1450         case AV_OPT_TYPE_INT: cp = N_("<int>"); break;
1451         case AV_OPT_TYPE_INT64: cp = N_("<int64>"); break;
1452         case AV_OPT_TYPE_DOUBLE: cp = N_("<double>"); break;
1453         case AV_OPT_TYPE_FLOAT: cp = N_("<float>"); break;
1454         case AV_OPT_TYPE_STRING: cp = N_("<string>"); break;
1455         case AV_OPT_TYPE_RATIONAL: cp = N_("<rational>"); break;
1456         case AV_OPT_TYPE_BINARY: cp = N_("<binary>"); break;
1457         case AV_OPT_TYPE_IMAGE_SIZE: cp = N_("<image_size>"); break;
1458         case AV_OPT_TYPE_VIDEO_RATE: cp = N_("<video_rate>"); break;
1459         case AV_OPT_TYPE_PIXEL_FMT: cp = N_("<pix_fmt>"); break;
1460         case AV_OPT_TYPE_SAMPLE_FMT: cp = N_("<sample_fmt>"); break;
1461         case AV_OPT_TYPE_DURATION: cp = N_("<duration>"); break;
1462         case AV_OPT_TYPE_COLOR: cp = N_("<color>"); break;
1463         case AV_OPT_TYPE_CHANNEL_LAYOUT: cp = N_("<channel_layout>");  break;
1464         case AV_OPT_TYPE_BOOL: cp = N_("<bool>");  break;
1465         default: cp = N_("<undef>");  break;
1466         }
1467         return sprintf(rp, "%s", _(cp));
1468 }
1469 int FFOptions_Opt::scalar(double d, char *rp)
1470 {
1471         const char *cp = 0;
1472              if( d == INT_MAX ) cp = "INT_MAX";
1473         else if( d == INT_MIN ) cp = "INT_MIN";
1474         else if( d == UINT32_MAX ) cp = "UINT32_MAX";
1475         else if( d == (double)INT64_MAX ) cp = "I64_MAX";
1476         else if( d == INT64_MIN ) cp = "I64_MIN";
1477         else if( d == FLT_MAX ) cp = "FLT_MAX";
1478         else if( d == FLT_MIN ) cp = "FLT_MIN";
1479         else if( d == -FLT_MAX ) cp = "-FLT_MAX";
1480         else if( d == -FLT_MIN ) cp = "-FLT_MIN";
1481         else if( d == DBL_MAX ) cp = "DBL_MAX";
1482         else if( d == DBL_MIN ) cp = "DBL_MIN";
1483         else if( d == -DBL_MAX ) cp = "-DBL_MAX";
1484         else if( d == -DBL_MIN ) cp = "-DBL_MIN";
1485         else if( d == 0 ) cp = signbit(d) ? "-0" : "0";
1486         else if( isnan(d) ) cp = signbit(d) ? "-NAN" : "NAN";
1487         else if( isinf(d) ) cp = signbit(d) ? "-INF" : "INF";
1488         else return sprintf(rp, "%g", d);
1489         return sprintf(rp, "%s", cp);
1490 }
1491
1492 int FFOptions_Opt::ranges(char *rp)
1493 {
1494         if( !opt ) return 0;
1495         void *obj = (void *)options->obj;
1496         if( !obj ) return 0;
1497
1498         switch (opt->type) {
1499         case AV_OPT_TYPE_INT:
1500         case AV_OPT_TYPE_INT64:
1501         case AV_OPT_TYPE_DOUBLE:
1502         case AV_OPT_TYPE_FLOAT: break;
1503         default: return 0;;
1504         }
1505         AVOptionRanges *r = 0;
1506         char *cp = rp;
1507         if( av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) < 0 ) return 0;
1508         for( int i=0; i<r->nb_ranges; ++i ) {
1509                 cp += sprintf(cp, " (");  cp += scalar(r->range[i]->value_min, cp);
1510                 cp += sprintf(cp, "..");  cp += scalar(r->range[i]->value_max, cp);
1511                 cp += sprintf(cp, ")");
1512         }
1513         av_opt_freep_ranges(&r);
1514         return cp - rp;
1515 }
1516
1517 int FFOptions_Opt::units(ArrayList<const char *> &names)
1518 {
1519         if( !opt || !opt->unit ) return 0;
1520         const void *obj = options->obj;
1521         if( !obj ) return 0;
1522
1523         ArrayList<const AVOption *> opts;
1524         const AVOption *opt = NULL;
1525         while( (opt=av_opt_next(obj, opt)) != 0 ) {
1526                 if( !opt->unit ) continue;
1527                 if( opt->type != AV_OPT_TYPE_CONST ) continue;
1528                 if( strcmp(this->opt->unit, opt->unit) ) continue;
1529                 int i = opts.size();
1530                 while( --i >= 0 ) {
1531                         if( opts[i]->default_val.i64 != opt->default_val.i64 ) continue;
1532                         if( strlen(opts[i]->name) < strlen(opt->name) ) opts[i] = opt;
1533                         break;
1534                 }
1535                 if( i >= 0 ) continue;
1536                 opts.append(opt);
1537         }
1538
1539         for( int i=0; i<opts.size(); ++i )
1540                 names.append(opts[i]->name);
1541
1542         return names.size();
1543 }
1544
1545 int FFOptions_Opt::units(char *rp)
1546 {
1547         ArrayList<const char *> names;
1548         int n = units(names);
1549         if( !n ) return 0;
1550         char *cp = rp;
1551         cp += sprintf(cp, " [%s:", this->opt->unit);
1552         for( int i=0; i<n; ++i )
1553                 cp += sprintf(cp, " %s", names[i]);
1554         cp += sprintf(cp, "]:");
1555         return cp - rp;
1556 }
1557
1558 const char *FFOptions_Opt::unit_name(int id)
1559 {
1560         if( !opt || !opt->unit ) return 0;
1561         const void *obj = options->obj;
1562         if( !obj ) return 0;
1563
1564         const char *ret = 0;
1565         const AVOption *opt = NULL;
1566         while( (opt=av_opt_next(obj, opt)) != 0 ) {
1567                 if( !opt->unit ) continue;
1568                 if( opt->type != AV_OPT_TYPE_CONST ) continue;
1569                 if( strcmp(this->opt->unit, opt->unit) ) continue;
1570                 if( opt->default_val.i64 != id ) continue;
1571                 if( !ret ) { ret = opt->name;  continue; }
1572                 if( strlen(ret) < strlen(opt->name) ) ret = opt->name;
1573         }
1574
1575         return ret;
1576 }
1577
1578 const char *FFOptions_Opt::tip()
1579 {
1580         return !opt ? 0 : opt->help;
1581 }
1582
1583
1584 FFOptionsWindow::FFOptionsWindow(FFOptionsDialog *dialog, int x, int y)
1585  : BC_Window(_(PROGRAM_NAME ": Options"), x, y, xS(640), yS(400))
1586 {
1587         this->dialog = dialog;
1588         this->selected = 0;
1589         this->kind = 0;
1590 }
1591
1592 FFOptionsWindow::~FFOptionsWindow()
1593 {
1594 }
1595
1596 void FFOptionsWindow::create_objects()
1597 {
1598         int xs8 = xS(8), xs10 = xS(10);
1599         int ys10 = yS(10);
1600         lock_window("FFOptionsWindow::create_objects");
1601         BC_Title *title;
1602         const char *format_name = dialog->cfg_window->format_name;
1603         const char *codec_name = dialog->cfg_window->codec_name;
1604         int x0 = xs10, y0 = ys10;
1605         int x = x0, y = y0;
1606         add_subwindow(title = new BC_Title(x, y, _("Format: ")));
1607         x += title->get_w();
1608         add_subwindow(new BC_Title(x, y, format_name));
1609         if( codec_name ) {
1610                 x = x0 + xS(150);
1611                 add_subwindow(title = new BC_Title(x, y, _("Codec: ")));
1612                 x += title->get_w();
1613                 add_subwindow(new BC_Title(x, y, codec_name));
1614         }
1615         x = x0;  y += title->get_h() + ys10;  y0 = y;
1616         add_subwindow(title = new BC_Title(x, y, _("Type: ")));
1617         x += title->get_w() + xs8;
1618         add_subwindow(type = new BC_Title(x, y, (char *)""));
1619         x = x0 + xS(150);
1620         add_subwindow(title = new BC_Title(x, y, _("Range: ")));
1621         x += title->get_w() + xs8;
1622         add_subwindow(range = new BC_Title(x, y, (char *)""));
1623
1624         x = x0;  y += title->get_h() + ys10;
1625         add_subwindow(units = new FFOptionsUnits(this, x, y, xS(120)));
1626         x += units->get_w() + xs8;
1627         int x1 = get_w() - BC_GenericButton::calculate_w(this, _("Apply")) - xs8;
1628         add_subwindow(text = new FFOptionsText(this, x, y, x1-x - xs8));
1629         add_subwindow(apply = new FFOptionsApply(this, x1, y));
1630         y += units->get_h() + ys10;
1631         if( codec_name ) {
1632                 add_subwindow(kind = new FFOptionsKind(this, x1, y0, apply->get_w()));
1633                 kind->create_objects();
1634                 const char *kind_text = _("Kind:");
1635                 x1 -= BC_Title::calculate_w(this, kind_text) + xs8;
1636                 add_subwindow(kind_title = new BC_Title(x1, y0, kind_text));
1637         }
1638         y0 = y;
1639         panel_x = x0;  panel_y = y0;
1640         panel_w = get_w()-xs10 - panel_x;
1641         panel_h = get_h()-ys10 - panel_y - BC_OKButton::calculate_h();
1642         panel = new FFOptions_OptPanel(this, panel_x, panel_y, panel_w, panel_h);
1643         add_subwindow(panel);
1644         add_subwindow(new BC_OKButton(this));
1645         add_subwindow(new BC_CancelButton(this));
1646         panel->create_objects();
1647         int k = codec_name ? FF_KIND_CODEC : FF_KIND_FORMAT;
1648         options.initialize(this, k);
1649         if( kind )
1650                 kind->set(k);
1651         draw();
1652         show_window(1);
1653         unlock_window();
1654 }
1655
1656 void FFOptionsWindow::draw()
1657 {
1658         update(selected);
1659 }
1660
1661 int FFOptionsWindow::resize_event(int w, int h)
1662 {
1663         int xs8 = xS(8), xs10 = xS(10);
1664         int ys10 = yS(10);
1665         if( kind ) {
1666                 int x0 = w - xs8 - kind->get_w();
1667                 int y0 = kind->get_y();
1668                 kind->reposition_window(x0, y0);
1669                 x0 -= kind_title->get_w() + xs8;
1670                 kind_title->reposition_window(x0, y0);
1671         }
1672         int x1 = get_w() - apply->get_w() - xs8;
1673         int y1 = units->get_y();
1674         apply->reposition_window(x1, y1);
1675         int x0 = units->get_x() + units->get_w() + xs8;
1676         int y0 = units->get_y();
1677         text->reposition_window(x0,y0, x1-x0-xs8);
1678         panel_w = get_w()-xs10 - panel_x;
1679         panel_h = get_h()-ys10 - panel_y - BC_OKButton::calculate_h();
1680         panel->reposition_window(panel_x,panel_y, panel_w, panel_h);
1681         return 1;
1682 }
1683
1684 FFOptionsDialog::FFOptionsDialog(FFMPEGConfigWindow *cfg_window)
1685  : BC_DialogThread()
1686 {
1687         this->cfg_window = cfg_window;
1688         options_window = 0;
1689         ff_opts = 0;
1690 }
1691
1692 FFOptionsDialog::~FFOptionsDialog()
1693 {
1694         close_window();
1695         av_dict_free(&ff_opts);
1696 }
1697
1698 void FFOptionsDialog::load_options(const char *bp, int len)
1699 {
1700         av_dict_free(&ff_opts);
1701         char line[BCTEXTLEN];
1702         char key[BCSTRLEN], val[BCTEXTLEN];
1703         const char *dp = bp + len-1;
1704         int no = 0;
1705         while( bp < dp && *bp != 0 ) {
1706                 ++no;
1707                 char *cp = line, *ep = cp+sizeof(line)-1;
1708                 while( *bp && cp<ep && (*cp=*bp++)!='\n' ) ++cp;
1709                 *cp = 0;
1710                 if( line[0] == '#' ) {
1711                         sprintf(key,"#%d", no);
1712                         av_dict_set(&ff_opts, key, line, 0);
1713                         continue;
1714                 }
1715                 if( line[0] == '\n' ) continue;
1716                 if( FFMPEG::scan_option_line(line, key, val) ) continue;
1717                 av_dict_set(&ff_opts, key, val, 0);
1718         }
1719 }
1720
1721 void FFOptionsDialog::store_options(char *cp, int len)
1722 {
1723         char *ep = cp + len-1;
1724         AVDictionaryEntry *elem = 0;
1725         while( (elem=av_dict_get(ff_opts, "", elem, AV_DICT_IGNORE_SUFFIX)) != 0 ) {
1726                 if( elem->key[0] == '#' ) {
1727                         cp += snprintf(cp,ep-cp, "%s\n", elem->value);
1728                         continue;
1729                 }
1730                 cp += snprintf(cp,ep-cp, "%s=%s\n", elem->key, elem->value);
1731         }
1732         *cp = 0;
1733 }
1734
1735
1736 void FFOptionsDialog::start()
1737 {
1738         if( options_window ) {
1739                 options_window->lock_window("FFOptionsDialog::start");
1740                 options_window->raise_window();
1741                 options_window->unlock_window();
1742                 return;
1743         }
1744         cfg_window->get_pop_cursor(wx, wy);
1745         cfg_window->read_options();
1746         BC_DialogThread::start();
1747 }
1748
1749 BC_Window* FFOptionsDialog::new_gui()
1750 {
1751         options_window = new FFOptionsWindow(this, wx, wy);
1752         options_window->create_objects();
1753         return options_window;
1754 }
1755
1756 void FFOptionsDialog::handle_done_event(int result)
1757 {
1758         if( !result ) {
1759                 cfg_window->lock_window("FFMPEGConfigFormat::save_options");
1760                 cfg_window->save_options();
1761                 cfg_window->unlock_window();
1762         }
1763         options_window = 0;
1764 }
1765
1766 FFOptionsAudioDialog::FFOptionsAudioDialog(FFMPEGConfigAudio *aud_config)
1767  : FFOptionsDialog(aud_config)
1768 {
1769         this->aud_config = aud_config;
1770 }
1771
1772 FFOptionsAudioDialog::~FFOptionsAudioDialog()
1773 {
1774         close_window();
1775 }
1776
1777 void FFOptionsAudioDialog::update_options(const char *options)
1778 {
1779         aud_config->lock_window("FFOptionsAudioDialog::update_options");
1780         aud_config->audio_options->update(options);
1781         aud_config->unlock_window();
1782 }
1783
1784 FFOptionsVideoDialog::FFOptionsVideoDialog(FFMPEGConfigVideo *vid_config)
1785  : FFOptionsDialog(vid_config)
1786 {
1787         this->vid_config = vid_config;
1788 }
1789
1790 FFOptionsVideoDialog::~FFOptionsVideoDialog()
1791 {
1792         close_window();
1793 }
1794
1795 void FFOptionsVideoDialog::update_options(const char *options)
1796 {
1797         vid_config->lock_window("FFOptionsVideoDialog::update_options");
1798         vid_config->video_options->update(options);
1799         vid_config->unlock_window();
1800 }
1801
1802 FFOptionsFormatDialog::FFOptionsFormatDialog(FFMPEGConfigFormat *fmt_config)
1803  : FFOptionsDialog(fmt_config)
1804 {
1805         this->fmt_config = fmt_config;
1806 }
1807
1808 FFOptionsFormatDialog::~FFOptionsFormatDialog()
1809 {
1810         close_window();
1811 }
1812
1813 void FFOptionsFormatDialog::update_options(const char *options)
1814 {
1815         fmt_config->lock_window("FFOptionsFormatDialog::update_options");
1816         fmt_config->format_options->update(options);
1817         fmt_config->unlock_window();
1818 }
1819
1820
1821 FFOptionsViewAudio::FFOptionsViewAudio(FFMPEGConfigAudio *aud_config,
1822                 int x, int y, const char *text)
1823  : BC_GenericButton(x, y, text)
1824 {
1825         this->aud_config = aud_config;
1826         avctx = 0;
1827 }
1828
1829 FFOptionsViewAudio::~FFOptionsViewAudio()
1830 {
1831         avcodec_free_context(&avctx);
1832 }
1833
1834 int FFOptionsViewAudio::handle_event()
1835 {
1836         int ret = 0;
1837         Asset *asset = aud_config->asset;
1838         const char *name = asset->acodec;
1839         char audio_format[BCSTRLEN];  audio_format[0] = 0;
1840         char audio_codec[BCSTRLEN];   audio_codec[0] = 0;
1841         AVCodec *codec = !ret &&
1842             !FFMPEG::get_format(audio_format, "audio", name) &&
1843             !FFMPEG::get_codec(audio_codec, "audio", name) ?
1844                 avcodec_find_encoder_by_name(audio_codec) : 0;
1845         if( !ret && !codec ) {
1846                 eprintf(_("no codec named: %s: %s"), name, audio_codec);
1847                 ret = 1;
1848         }
1849         avcodec_free_context(&avctx);
1850         if( !ret && !(avctx = avcodec_alloc_context3(codec)) ) {
1851                 eprintf(_("no codec context: %s: %s"), name, audio_codec);
1852                 ret = 1;
1853         }
1854         if( !ret )
1855                 aud_config->start(avctx);
1856         return 1;
1857 }
1858
1859 FFOptionsViewVideo::FFOptionsViewVideo(FFMPEGConfigVideo *vid_config,
1860                 int x, int y, const char *text)
1861  : BC_GenericButton(x, y, text)
1862 {
1863         this->vid_config = vid_config;
1864         avctx = 0;
1865 }
1866
1867 FFOptionsViewVideo::~FFOptionsViewVideo()
1868 {
1869         avcodec_free_context(&avctx);
1870 }
1871
1872 int FFOptionsViewVideo::handle_event()
1873 {
1874         int ret = 0;
1875         Asset *asset = vid_config->asset;
1876         const char *name = asset->vcodec;
1877         char video_format[BCSTRLEN];  video_format[0] = 0;
1878         char video_codec[BCSTRLEN];   video_codec[0] = 0;
1879         AVCodec *codec = !ret &&
1880             !FFMPEG::get_format(video_format, "video", name) &&
1881             !FFMPEG::get_codec(video_codec, "video", name) ?
1882                 avcodec_find_encoder_by_name(video_codec) : 0;
1883         if( !ret && !codec ) {
1884                 eprintf(_("no codec named: %s: %s"), name, video_codec);
1885                 ret = 1;
1886         }
1887         avcodec_free_context(&avctx);
1888         if( !ret && !(avctx = avcodec_alloc_context3(codec)) ) {
1889                 eprintf(_("no codec context: %s: %s"), name, video_codec);
1890                 ret = 1;
1891         }
1892
1893         if( !ret )
1894                 vid_config->start(avctx);
1895         return 1;
1896 }
1897
1898 FFOptionsViewFormat::FFOptionsViewFormat(FFMPEGConfigWindow *cfg_window,
1899                 EDL *edl, Asset *asset, int x, int y, const char *text)
1900  : BC_GenericButton(x, y, text)
1901 {
1902         this->cfg_window = cfg_window;
1903         this->edl = edl;
1904         this->asset = asset;
1905         format_dialog = 0;
1906 }
1907
1908 FFOptionsViewFormat::~FFOptionsViewFormat()
1909 {
1910         delete format_dialog;
1911 }
1912
1913 int FFOptionsViewFormat::handle_event()
1914 {
1915         delete format_dialog;
1916         int wx, wy;
1917         get_pop_cursor(wx, wy);
1918         format_dialog = new FFOptionsFormatViewDialog(this, wx, wy);
1919         format_dialog->start();
1920         return 1;
1921 }
1922
1923
1924 FFOptionsFormatView::FFOptionsFormatView(FFMPEGConfigFormat *fmt_config,
1925                 int x, int y, const char *text)
1926  : BC_GenericButton(x, y, text)
1927 {
1928         this->fmt_config = fmt_config;
1929         fmt_ctx = 0;
1930 }
1931
1932 FFOptionsFormatView::~FFOptionsFormatView()
1933 {
1934         avformat_free_context(fmt_ctx);
1935 }
1936
1937 int FFOptionsFormatView::handle_event()
1938 {
1939         Asset *asset = fmt_config->asset;
1940         char *format_name = asset->fformat;
1941         avformat_free_context(fmt_ctx);  fmt_ctx = 0;
1942         int ret = avformat_alloc_output_context2(&fmt_ctx, 0, format_name, 0);
1943         if( ret || !fmt_ctx ) {
1944                 eprintf(_("no format named: %s"), format_name);
1945                 ret = 1;
1946         }
1947         if( !ret )
1948                 fmt_config->start(fmt_ctx);
1949         return 1;
1950 }
1951
1952 FFOptionsFormatViewDialog::FFOptionsFormatViewDialog(FFOptionsViewFormat *view_format,
1953                 int wx, int wy)
1954 {
1955         this->view_format = view_format;
1956         this->wx = wx;
1957         this->wy = wy;
1958         cfg_window = 0;
1959 }
1960
1961 FFOptionsFormatViewDialog::~FFOptionsFormatViewDialog()
1962 {
1963         close_window();
1964 }
1965
1966 BC_Window *FFOptionsFormatViewDialog::new_gui()
1967 {
1968         cfg_window = new FFMPEGConfigFormat(this, wx, wy,
1969                 view_format->asset, view_format->edl);
1970         cfg_window->create_objects();
1971         cfg_window->read_options();
1972         return cfg_window;
1973 }
1974
1975 void FFOptionsFormatViewDialog::handle_done_event(int result)
1976 {
1977         if( !result )
1978                 cfg_window->save_changes();
1979         cfg_window = 0;
1980 }
1981