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