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