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