f3f535681234e3bbefe1a17d841eae4443458c55
[goodguy/history.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 "ffmpeg.h"
15 #include "filebase.h"
16 #include "file.h"
17 #include "fileffmpeg.h"
18 #include "filesystem.h"
19 #include "indexfile.h"
20 #include "language.h"
21 #include "mainerror.h"
22 #include "mainprogress.h"
23 #include "mutex.h"
24 #include "preferences.h"
25 #include "videodevice.inc"
26
27 FileFFMPEG::FileFFMPEG(Asset *asset, File *file)
28   : FileBase(asset, file)
29 {
30         ff = 0;
31         if(asset->format == FILE_UNKNOWN)
32                 asset->format = FILE_FFMPEG;
33 }
34
35 FileFFMPEG::~FileFFMPEG()
36 {
37         delete ff;
38 }
39
40
41 FFMpegConfigNum::FFMpegConfigNum(BC_Window *window,
42                 int x, int y, char *title_text, int *output)
43  : BC_TumbleTextBox(window, *output, -1, INT_MAX, 100, y, 100)
44 {
45         this->window = window;
46         this->x = x;  this->y = y;
47         this->title_text = title_text;
48         this->output = output;
49 }
50
51 FFMpegConfigNum::~FFMpegConfigNum()
52 {
53 }
54
55 void FFMpegConfigNum::create_objects()
56 {
57         window->add_subwindow(title = new BC_Title(x, y, title_text));
58         BC_TumbleTextBox::create_objects();
59 }
60
61 int FFMpegConfigNum::update_param(const char *param, const char *opts)
62 {
63         char value[BCTEXTLEN];
64         if( !FileFFMPEG::get_ff_option(param, opts, value) ) {
65                 if( (*output = atoi(value)) < 0 ) {
66                         disable(1);
67                         return 0;
68                 }
69         }
70         BC_TumbleTextBox::update((int64_t)*output);
71         enable();
72         return 1;
73 }
74
75 int FFMpegConfigNum::handle_event()
76 {
77         *output = atoi(get_text());
78         return 1;
79 }
80
81 FFMpegAudioNum::FFMpegAudioNum(BC_Window *window,
82                 int x, int y, char *title_text, int *output)
83  : FFMpegConfigNum(window, x, y, title_text, output)
84 {
85 }
86
87 int FFMpegAudioBitrate::handle_event()
88 {
89         int ret = FFMpegAudioNum::handle_event();
90         return ret;
91 }
92
93 FFMpegVideoNum::FFMpegVideoNum(BC_Window *window,
94                 int x, int y, char *title_text, int *output)
95  : FFMpegConfigNum(window, x, y, title_text, output)
96 {
97 }
98
99 int FFMpegVideoBitrate::handle_event()
100 {
101         int ret = FFMpegVideoNum::handle_event();
102         Asset *asset = window()->asset;
103         if( asset->ff_video_bitrate > 0 )
104                 window()->quality->disable();
105         else if( !window()->quality->get_textbox()->is_hidden() )
106                 window()->quality->enable();
107         return ret;
108 }
109
110 int FFMpegVideoQuality::handle_event()
111 {
112         int ret = FFMpegVideoNum::handle_event();
113         Asset *asset = window()->asset;
114         if( asset->ff_video_quality >= 0 )
115                 window()->bitrate->disable();
116         else if( !window()->bitrate->get_textbox()->is_hidden() )
117                 window()->bitrate->enable();
118         return ret;
119 }
120
121 void FileFFMPEG::set_parameters(char *cp, int len, const char *bp)
122 {
123         char *ep = cp + len-2, ch = 0;
124         while( cp < ep && *bp != 0 ) { ch = *bp++; *cp++ = ch; }
125         if( ch != '\n' ) *cp++ = '\n';
126         *cp = 0;
127 }
128
129 void FileFFMPEG::get_parameters(BC_WindowBase *parent_window,
130                 Asset *asset, BC_WindowBase *&format_window,
131                 int audio_options, int video_options)
132 {
133         if(audio_options) {
134                 FFMPEGConfigAudio *window = new FFMPEGConfigAudio(parent_window, asset);
135                 format_window = window;
136                 window->create_objects();
137                 if( !window->run_window() )
138                         set_parameters(asset->ff_audio_options, sizeof(asset->ff_audio_options),
139                                  window->audio_options->get_text());
140                 delete window;
141         }
142         else if(video_options) {
143                 FFMPEGConfigVideo *window = new FFMPEGConfigVideo(parent_window, asset);
144                 format_window = window;
145                 window->create_objects();
146                 if( !window->run_window() )
147                         set_parameters(asset->ff_video_options, sizeof(asset->ff_video_options),
148                                  window->video_options->get_text());
149                 delete window;
150         }
151 }
152
153 int FileFFMPEG::check_sig(Asset *asset)
154 {
155         char *ptr = strstr(asset->path, ".pcm");
156         if( ptr ) return 0;
157         ptr = strstr(asset->path, ".raw");
158         if( ptr ) return 0;
159
160         FFMPEG ffmpeg(0);
161         int ret = !ffmpeg.init_decoder(asset->path) &&
162                 !ffmpeg.open_decoder() ? 1 : 0;
163         return ret;
164 }
165
166 void FileFFMPEG::get_info(char *path, char *text, int len)
167 {
168         char *cp = text;
169         FFMPEG ffmpeg(0);
170         cp += sprintf(cp, _("file path: %s\n"), path);
171         struct stat st;
172         int ret = 0;
173         if( stat(path, &st) < 0 ) {
174                 cp += sprintf(cp, _(" err: %s\n"), strerror(errno));
175                 ret = 1;
176         }
177         else {
178                 cp += sprintf(cp, _("  %jd bytes\n"), st.st_size);
179         }
180         if( !ret ) ret = ffmpeg.init_decoder(path);
181         if( !ret ) ret = ffmpeg.open_decoder();
182         if( !ret ) {
183                 cp += sprintf(cp, _("info:\n"));
184                 ffmpeg.info(cp, len-(cp-text));
185         }
186         else
187                 sprintf(cp, _("== open failed\n"));
188 }
189
190 int FileFFMPEG::get_video_info(int track, int &pid, double &framerate,
191                 int &width, int &height, char *title)
192 {
193         if( !ff ) return -1;
194         pid = ff->ff_video_pid(track);
195         framerate = ff->ff_frame_rate(track);
196         width = ff->ff_video_width(track);
197         height = ff->ff_video_height(track);
198         if( title ) *title = 0;
199         return 0;
200 }
201
202 int FileFFMPEG::get_audio_for_video(int vstream, int astream, int64_t &channel_mask)
203 {
204         if( !ff ) return 1;
205         return ff->ff_audio_for_video(vstream, astream, channel_mask);
206 }
207
208 int FileFFMPEG::select_video_stream(Asset *asset, int vstream)
209 {
210         if( !ff || !asset->video_data ) return 1;
211         asset->width = ff->ff_video_width(vstream);
212         asset->height = ff->ff_video_height(vstream);
213         if( (asset->video_length = ff->ff_video_frames(vstream)) < 2 )
214                 asset->video_length = asset->video_length < 0 ? 0 : -1;
215         asset->frame_rate = ff->ff_frame_rate(vstream);
216         return 0;
217 }
218
219 int FileFFMPEG::select_audio_stream(Asset *asset, int astream)
220 {
221         if( !ff || !asset->audio_data ) return 1;
222         asset->sample_rate = ff->ff_sample_rate(astream);
223         asset->audio_length = ff->ff_audio_samples(astream);
224         return 0;
225 }
226
227 int FileFFMPEG::open_file(int rd, int wr)
228 {
229         int result = 0;
230         if( ff ) return 1;
231         ff = new FFMPEG(this);
232
233         if( rd ) {
234                 result = ff->init_decoder(asset->path);
235                 if( !result ) result = ff->open_decoder();
236                 if( !result ) {
237                         int audio_channels = ff->ff_total_audio_channels();
238                         if( audio_channels > 0 ) {
239                                 asset->audio_data = 1;
240                                 asset->channels = audio_channels;
241                                 asset->sample_rate = ff->ff_sample_rate(0);
242                                 asset->audio_length = ff->ff_audio_samples(0);
243                                 strcpy(asset->acodec, ff->ff_audio_format(0));
244                         }
245                         int video_layers = ff->ff_total_video_layers();
246                         if( video_layers > 0 ) {
247                                 asset->video_data = 1;
248                                 if( !asset->layers ) asset->layers = video_layers;
249                                 asset->actual_width = ff->ff_video_width(0);
250                                 asset->actual_height = ff->ff_video_height(0);
251                                 if( !asset->width ) asset->width = asset->actual_width;
252                                 if( !asset->height ) asset->height = asset->actual_height;
253                                 if( !asset->video_length &&
254                                     (asset->video_length = ff->ff_video_frames(0)) < 2 )
255                                         asset->video_length = asset->video_length < 0 ? 0 : -1;
256                                 if( !asset->frame_rate ) asset->frame_rate = ff->ff_frame_rate(0);
257                                 strcpy(asset->vcodec, ff->ff_video_format(0));
258                         }
259                         IndexState *index_state = asset->index_state;
260                         index_state->read_markers(file->preferences->index_directory, asset->path);
261                 }
262         }
263         else if( wr ) {
264                 result = ff->init_encoder(asset->path);
265                 // must be in this order or dvdauthor will fail
266                 if( !result && asset->video_data )
267                         result = ff->open_encoder("video", asset->vcodec);
268                 if( !result && asset->audio_data )
269                         result = ff->open_encoder("audio", asset->acodec);
270         }
271         return result;
272 }
273
274 int FileFFMPEG::close_file()
275 {
276         delete ff;
277         ff = 0;
278         return 0;
279 }
280
281
282 int FileFFMPEG::write_samples(double **buffer, int64_t len)
283 {
284         if( !ff || len < 0 ) return -1;
285         int stream = 0;
286         int ret = ff->encode(stream, buffer, len);
287         return ret;
288 }
289
290 int FileFFMPEG::write_frames(VFrame ***frames, int len)
291 {
292         if( !ff ) return -1;
293         int ret = 0, layer = 0;
294         for(int i = 0; i < 1; i++) {
295                 for(int j = 0; j < len && !ret; j++) {
296                         VFrame *frame = frames[i][j];
297                         ret = ff->encode(layer, frame);
298                 }
299         }
300         return ret;
301 }
302
303
304 int FileFFMPEG::read_samples(double *buffer, int64_t len)
305 {
306         if( !ff || len < 0 ) return -1;
307         int ch = file->current_channel;
308         int64_t pos = file->current_sample;
309         int ret = ff->decode(ch, pos, buffer, len);
310         if( ret > 0 ) return 0;
311         memset(buffer,0,len*sizeof(*buffer));
312         return -1;
313 }
314
315 int FileFFMPEG::read_frame(VFrame *frame)
316 {
317         if( !ff ) return -1;
318         int layer = file->current_layer;
319         int64_t pos = asset->video_length >= 0 ? file->current_frame : 0;
320         int ret = ff->decode(layer, pos, frame);
321         frame->set_status(ret);
322         if( ret >= 0 ) return 0;
323         frame->clear_frame();
324         return -1;
325 }
326
327
328 int64_t FileFFMPEG::get_memory_usage()
329 {
330         return 0;
331 }
332
333 int FileFFMPEG::colormodel_supported(int colormodel)
334 {
335         return colormodel;
336 }
337
338 int FileFFMPEG::get_best_colormodel(Asset *asset, int driver)
339 {
340         switch(driver) {
341 // the direct X11 color model requires scaling in the codec
342         case PLAYBACK_X11: return BC_BGR8888;
343 //      case PLAYBACK_X11: return BC_RGB888;
344         case PLAYBACK_X11_GL: return BC_RGB888;
345         }
346
347         return BC_YUV420P;
348 }
349
350 int FileFFMPEG::can_render(const char *fformat, const char *type)
351 {
352         FileSystem fs;
353         char option_path[BCTEXTLEN];
354         FFMPEG::set_option_path(option_path, type);
355         fs.update(option_path);
356         int total_files = fs.total_files();
357         for( int i=0; i<total_files; ++i ) {
358                 const char *name = fs.get_entry(i)->get_name();
359                 const char *ext = strrchr(name,'.');
360                 if( !ext ) continue;
361                 if( !strcmp(fformat, ++ext) ) return 1;
362         }
363         return 0;
364 }
365
366 int FileFFMPEG::get_ff_option(const char *nm, const char *options, char *value)
367 {
368         for( const char *cp=options; *cp!=0; ) {
369                 char line[BCTEXTLEN], *bp = line, *ep = bp+sizeof(line)-1;
370                 while( bp < ep && *cp && *cp!='\n' ) *bp++ = *cp++;
371                 if( *cp ) ++cp;
372                 *bp = 0;
373                 if( !line[0] || line[0] == '#' || line[0] == ';' ) continue;
374                 char key[BCSTRLEN], val[BCTEXTLEN];
375                 if( FFMPEG::scan_option_line(line, key, val) ) continue;
376                 if( !strcmp(key, nm) ) {
377                         strcpy(value, val);
378                         return 0;
379                 }
380         }
381         return 1;
382 }
383
384 //======
385
386 FFMPEGConfigAudio::FFMPEGConfigAudio(BC_WindowBase *parent_window, Asset *asset)
387  : BC_Window(_(PROGRAM_NAME ": Audio Preset"),
388         parent_window->get_abs_cursor_x(1),
389         parent_window->get_abs_cursor_y(1),
390         420, 420)
391 {
392         this->parent_window = parent_window;
393         this->asset = asset;
394         preset_popup = 0;
395
396         bitrate = 0;
397         audio_options = 0;
398         ff_options_dialog = 0;
399 }
400
401 FFMPEGConfigAudio::~FFMPEGConfigAudio()
402 {
403         delete ff_options_dialog;
404         lock_window("FFMPEGConfigAudio::~FFMPEGConfigAudio");
405         delete preset_popup;
406         presets.remove_all_objects();
407         unlock_window();
408 }
409
410 void FFMPEGConfigAudio::create_objects()
411 {
412         int x = 10, y = 10;
413         lock_window("FFMPEGConfigAudio::create_objects");
414
415         FileSystem fs;
416         char option_path[BCTEXTLEN];
417         FFMPEG::set_option_path(option_path, "audio");
418         fs.update(option_path);
419         int total_files = fs.total_files();
420         for(int i = 0; i < total_files; i++) {
421                 const char *name = fs.get_entry(i)->get_name();
422                 if( asset->fformat[0] != 0 ) {
423                         const char *ext = strrchr(name,'.');
424                         if( !ext ) continue;
425                         if( strcmp(asset->fformat, ++ext) ) continue;
426                 }
427                 presets.append(new BC_ListBoxItem(name));
428         }
429
430         if( asset->acodec[0] ) {
431                 int k = presets.size();
432                 while( --k >= 0 && strcmp(asset->acodec, presets[k]->get_text()) );
433                 if( k < 0 ) asset->acodec[0] = 0;
434         }
435
436         if( !asset->acodec[0] && presets.size() > 0 )
437                 strcpy(asset->acodec, presets[0]->get_text());
438
439         add_tool(new BC_Title(x, y, _("Preset:")));
440         y += 25;
441         preset_popup = new FFMPEGConfigAudioPopup(this, x, y);
442         preset_popup->create_objects();
443
444         y += 50;
445         bitrate = new FFMpegAudioBitrate(this, x, y, _("Bitrate:"), &asset->ff_audio_bitrate);
446         bitrate->create_objects();
447         bitrate->set_increment(1000);
448         bitrate->set_boundaries((int64_t)0, (int64_t)INT_MAX);
449
450         y += bitrate->get_h() + 10;
451         BC_Title *title = new BC_Title(x, y, _("Audio Options:"));
452         add_subwindow(title);
453
454         ff_options_dialog = new FFOptionsAudioDialog(this);
455         int x1 = x + title->get_w() + 8;
456         add_subwindow(new FFOptionsViewAudio(this, x1, y, _("view")));
457
458         y += 25;
459         if( !asset->ff_audio_options[0] && asset->acodec[0] ) {
460                 FFMPEG::set_option_path(option_path, "audio/%s", asset->acodec);
461                 FFMPEG::load_options(option_path, asset->ff_audio_options,
462                          sizeof(asset->ff_audio_options));
463         }
464
465         audio_options = new FFAudioOptions(this, x, y, get_w()-x-20, 10,
466                  sizeof(asset->ff_audio_options)-1, asset->ff_audio_options);
467         audio_options->create_objects();
468         add_subwindow(new BC_OKButton(this));
469         add_subwindow(new BC_CancelButton(this));
470         show_window(1);
471
472         bitrate->update_param("cin_bitrate", asset->ff_audio_options);
473
474         unlock_window();
475 }
476
477 int FFMPEGConfigAudio::close_event()
478 {
479         set_done(1);
480         return 1;
481 }
482
483 FFAudioOptions::FFAudioOptions(FFMPEGConfigAudio *audio_popup,
484         int x, int y, int w, int rows, int size, char *text)
485  : BC_ScrollTextBox(audio_popup, x, y, w, rows, text, size)
486 {
487         this->audio_popup = audio_popup;
488 }
489
490
491 FFMPEGConfigAudioPopup::FFMPEGConfigAudioPopup(FFMPEGConfigAudio *popup, int x, int y)
492  : BC_PopupTextBox(popup, &popup->presets, popup->asset->acodec, x, y, 300, 300)
493 {
494         this->popup = popup;
495 }
496
497 int FFMPEGConfigAudioPopup::handle_event()
498 {
499         strcpy(popup->asset->acodec, get_text());
500         Asset *asset = popup->asset;
501         asset->ff_audio_bitrate = 0;
502         char option_path[BCTEXTLEN];
503         FFMPEG::set_option_path(option_path, "audio/%s", asset->acodec);
504         FFMPEG::load_options(option_path, asset->ff_audio_options,
505                          sizeof(asset->ff_audio_options));
506         popup->audio_options->update(asset->ff_audio_options);
507         popup->audio_options->set_text_row(0);
508
509         popup->bitrate->update_param("cin_bitrate", asset->ff_audio_options);
510         return 1;
511 }
512
513
514 FFMPEGConfigAudioToggle::FFMPEGConfigAudioToggle(FFMPEGConfigAudio *popup,
515         char *title_text, int x, int y, int *output)
516  : BC_CheckBox(x, y, *output, title_text)
517 {
518         this->popup = popup;
519         this->output = output;
520 }
521 int FFMPEGConfigAudioToggle::handle_event()
522 {
523         *output = get_value();
524         return 1;
525 }
526
527 //======
528
529 FFMPEGConfigVideo::FFMPEGConfigVideo(BC_WindowBase *parent_window, Asset *asset)
530  : BC_Window(_(PROGRAM_NAME ": Video Preset"),
531         parent_window->get_abs_cursor_x(1),
532         parent_window->get_abs_cursor_y(1),
533         420, 420)
534 {
535         this->parent_window = parent_window;
536         this->asset = asset;
537         preset_popup = 0;
538
539         bitrate = 0;
540         quality = 0;
541         video_options = 0;
542 }
543
544 FFMPEGConfigVideo::~FFMPEGConfigVideo()
545 {
546         delete ff_options_dialog;
547         lock_window("FFMPEGConfigVideo::~FFMPEGConfigVideo");
548         if(preset_popup) delete preset_popup;
549         presets.remove_all_objects();
550         unlock_window();
551 }
552
553 void FFMPEGConfigVideo::create_objects()
554 {
555         int x = 10, y = 10;
556         lock_window("FFMPEGConfigVideo::create_objects");
557
558         add_subwindow(new BC_Title(x, y, _("Compression:")));
559         y += 25;
560
561         FileSystem fs;
562         char option_path[BCTEXTLEN];
563         FFMPEG::set_option_path(option_path, "video");
564         fs.update(option_path);
565         int total_files = fs.total_files();
566         for(int i = 0; i < total_files; i++) {
567                 const char *name = fs.get_entry(i)->get_name();
568                 if( asset->fformat[0] != 0 ) {
569                         const char *ext = strrchr(name,'.');
570                         if( !ext ) continue;
571                         if( strcmp(asset->fformat, ++ext) ) continue;
572                 }
573                 presets.append(new BC_ListBoxItem(name));
574         }
575
576         if( asset->vcodec[0] ) {
577                 int k = presets.size();
578                 while( --k >= 0 && strcmp(asset->vcodec, presets[k]->get_text()) );
579                 if( k < 0 ) asset->vcodec[0] = 0;
580         }
581
582         if( !asset->vcodec[0] && presets.size() > 0 )
583                 strcpy(asset->vcodec, presets[0]->get_text());
584
585         preset_popup = new FFMPEGConfigVideoPopup(this, x, y);
586         preset_popup->create_objects();
587
588         if( asset->ff_video_bitrate > 0 && asset->ff_video_quality >= 0 ) {
589                 asset->ff_video_bitrate = 0;  asset->ff_video_quality = -1;
590         }
591
592         y += 50;
593         bitrate = new FFMpegVideoBitrate(this, x, y, _("Bitrate:"), &asset->ff_video_bitrate);
594         bitrate->create_objects();
595         bitrate->set_increment(100000);
596         bitrate->set_boundaries((int64_t)0, (int64_t)INT_MAX);
597         y += bitrate->get_h() + 5;
598         quality = new FFMpegVideoQuality(this, x, y, _("Quality:"), &asset->ff_video_quality);
599         quality->create_objects();
600         quality->set_increment(1);
601         quality->set_boundaries((int64_t)-1, (int64_t)51);
602
603         y += quality->get_h() + 10;
604         BC_Title *title = new BC_Title(x, y, _("Video Options:"));
605         add_subwindow(title);
606
607         ff_options_dialog = new FFOptionsVideoDialog(this);
608         int x1 = x + title->get_w() + 8;
609         add_subwindow(new FFOptionsViewVideo(this, x1, y, _("view")));
610
611         y += 25;
612         if( !asset->ff_video_options[0] && asset->vcodec[0] ) {
613                 FFMPEG::set_option_path(option_path, "video/%s", asset->vcodec);
614                 FFMPEG::load_options(option_path, asset->ff_video_options,
615                          sizeof(asset->ff_video_options));
616         }
617
618         video_options = new FFVideoOptions(this, x, y, get_w()-x-20, 10,
619                  sizeof(asset->ff_video_options)-1, asset->ff_video_options);
620         video_options->create_objects();
621         add_subwindow(new BC_OKButton(this));
622         add_subwindow(new BC_CancelButton(this));
623         show_window(1);
624
625         bitrate->update_param("cin_bitrate", asset->ff_video_options);
626         quality->update_param("cin_quality", asset->ff_video_options);
627
628         if( asset->ff_video_bitrate > 0 ) quality->disable();
629         else if( asset->ff_video_quality >= 0 ) bitrate->disable();
630         unlock_window();
631 }
632
633 int FFMPEGConfigVideo::close_event()
634 {
635         set_done(1);
636         return 1;
637 }
638
639 FFVideoOptions::FFVideoOptions(FFMPEGConfigVideo *video_popup,
640         int x, int y, int w, int rows, int size, char *text)
641  : BC_ScrollTextBox(video_popup, x, y, w, rows, text, size)
642 {
643         this->video_popup = video_popup;
644 }
645
646
647 FFMPEGConfigVideoPopup::FFMPEGConfigVideoPopup(FFMPEGConfigVideo *popup, int x, int y)
648  : BC_PopupTextBox(popup, &popup->presets, popup->asset->vcodec, x, y, 300, 300)
649 {
650         this->popup = popup;
651 }
652
653 int FFMPEGConfigVideoPopup::handle_event()
654 {
655         strcpy(popup->asset->vcodec, get_text());
656         Asset *asset = popup->asset;
657         char option_path[BCTEXTLEN];
658         asset->ff_video_bitrate = 0;  asset->ff_video_quality = -1;
659         FFMPEG::set_option_path(option_path, "video/%s", asset->vcodec);
660         FFMPEG::load_options(option_path, asset->ff_video_options,
661                          sizeof(asset->ff_video_options));
662         popup->video_options->update(asset->ff_video_options);
663         popup->video_options->set_text_row(0);
664
665         popup->bitrate->update_param("cin_bitrate", asset->ff_video_options);
666         popup->quality->update_param("cin_quality", asset->ff_video_options);
667         return 1;
668 }
669
670
671 FFMPEGConfigVideoToggle::FFMPEGConfigVideoToggle(FFMPEGConfigVideo *popup,
672         char *title_text, int x, int y, int *output)
673  : BC_CheckBox(x, y, *output, title_text)
674 {
675         this->popup = popup;
676         this->output = output;
677 }
678 int FFMPEGConfigVideoToggle::handle_event()
679 {
680         *output = get_value();
681         return 1;
682 }
683
684 FFMPEGScanProgress::FFMPEGScanProgress(IndexFile *index_file, MainProgressBar *progress_bar,
685                 const char *title, int64_t length, int64_t *position, int *canceled)
686  : Thread(1, 0, 0)
687 {
688         this->index_file = index_file;
689         this->progress_bar = progress_bar;
690         strcpy(this->progress_title, title);
691         this->length = length;
692         this->position = position;
693         this->canceled = canceled;
694         done = 0;
695         start();
696 }
697
698 FFMPEGScanProgress::~FFMPEGScanProgress()
699 {
700         done = 1;
701         cancel();
702         join();
703 }
704
705 void FFMPEGScanProgress::run()
706 {
707         while( !done ) {
708                 if( progress_bar->update(*position) ) {
709                         *canceled = done = 1;
710                         break;
711                 }
712                 index_file->redraw_edits(0);
713                 usleep(500000);
714         }
715 }
716
717 int FileFFMPEG::get_index(IndexFile *index_file, MainProgressBar *progress_bar)
718 {
719         if( !ff ) return -1;
720         if( !file->preferences->ffmpeg_marker_indexes ) return 1;
721
722         IndexState *index_state = index_file->get_state();
723         if( index_state->index_status != INDEX_NOTTESTED ) return 0;
724         index_state->reset_index();
725         index_state->reset_markers();
726         index_state->index_status = INDEX_BUILDING;
727
728         for( int aidx=0; aidx<ff->ffaudio.size(); ++aidx ) {
729                 FFAudioStream *aud = ff->ffaudio[aidx];
730                 index_state->add_audio_stream(aud->channels, aud->length);
731         }
732
733         FileSystem fs;
734         int64_t file_bytes = fs.get_size(ff->fmt_ctx->filename);
735         char *index_path = index_file->index_filename;
736
737         int canceled = 0;
738         int64_t scan_position = 0;
739         FFMPEGScanProgress *scan_progress = 0;
740         if( progress_bar ) {
741                 char progress_title[BCTEXTLEN];
742                 sprintf(progress_title, _("Creating %s\n"), index_path);
743                 progress_bar->update_title(progress_title, 1);
744                 progress_bar->update_length(file_bytes);
745                 scan_progress = new FFMPEGScanProgress(index_file,
746                                 progress_bar, progress_title, file_bytes,
747                                 &scan_position, &canceled);
748         }
749
750         index_state->index_bytes = file_bytes;
751         index_state->init_scan(file->preferences->index_size);
752
753         if( ff->scan(index_state, &scan_position, &canceled) || canceled ) {
754                 index_state->reset_index();
755                 index_state->reset_markers();
756                 canceled = 1;
757         }
758
759         delete scan_progress;
760         if( canceled ) return 1;
761
762         index_state->marker_status = MARKERS_READY;
763         return index_state->create_index(index_path, asset);
764 }
765
766
767 FFOptions_OptPanel::
768 FFOptions_OptPanel(FFOptionsWindow *fwin, int x, int y, int w, int h)
769  : BC_ListBox(x, y, w, h, LISTBOX_TEXT), opts(items[0]), vals(items[1])
770 {
771         this->fwin = fwin;
772         update();  // init col/wid/columns
773 }
774
775 FFOptions_OptPanel::
776 ~FFOptions_OptPanel()
777 {
778 }
779
780 void FFOptions_OptPanel::create_objects()
781 {
782         const char *cols[] = { _("option"), _("value"), };
783         const int col1_w = 150;
784         int wids[] = { col1_w, get_w()-col1_w };
785         BC_ListBox::update(&items[0], &cols[0], &wids[0], sizeof(items)/sizeof(items[0]));
786 }
787
788 int FFOptions_OptPanel::update()
789 {
790         opts.remove_all();
791         vals.remove_all();
792         FFOptions &options = fwin->options;
793         for( int i=0; i<options.size(); ++i ) {
794                 FFOptions_Opt *opt = options[i];
795                 opts.append(opt->item_name);
796                 vals.append(opt->item_value);
797         }
798         draw_items(1);
799         return 0;
800 }
801
802 int FFOptions_OptPanel::cursor_leave_event()
803 {
804         hide_tooltip();
805         return 0;
806 }
807
808
809 FFOptions_OptName::FFOptions_OptName(FFOptions_Opt *opt, const char *nm)
810 {
811         this->opt = opt;
812         set_text(nm);
813 }
814
815 FFOptions_OptName::~FFOptions_OptName()
816 {
817 }
818
819 FFOptions_OptValue::FFOptions_OptValue(FFOptions_Opt *opt)
820 {
821         this->opt = opt;
822 }
823
824
825 void FFOptions_OptValue::update()
826 {
827         if( !opt ) return;
828         char val[BCTEXTLEN];  val[0] = 0;
829         opt->get(val, sizeof(val));
830         update(val);
831 }
832
833 void FFOptions_OptValue::update(const char *v)
834 {
835         set_text(v);
836 }
837
838 FFOptions_Opt::FFOptions_Opt(FFOptions *options, const AVOption *opt, const char *nm)
839 {
840         this->options = options;
841         this->opt = opt;
842         item_name = new FFOptions_OptName(this, nm);
843         item_value = new FFOptions_OptValue(this);
844 }
845
846 FFOptions_Opt::~FFOptions_Opt()
847 {
848         delete item_name;
849         delete item_value;
850 }
851
852 char *FFOptions_Opt::get(char *vp, int sz)
853 {
854         char *cp = vp;
855         *cp = 0;
856         if( !opt ) return cp;
857
858         void *obj = (void *)options->obj;
859         uint8_t *bp = 0;
860         if( av_opt_get(obj, opt->name, 0, &bp) >= 0 && bp != 0 ) {
861                 const char *val = (const char *)bp;
862                 if( opt->unit && *val ) {
863                         int id = atoi(val);
864                         const char *uid = unit_name(id);
865                         if( uid ) val = uid;
866                 }
867                 cp = sz >= 0 ? strncpy(vp,val,sz) : strcpy(vp, val);
868                 if( sz > 0 ) vp[sz-1] = 0;
869                 av_freep(&bp);
870         }
871
872         return cp;
873 }
874
875 void FFOptions_Opt::set(const char *val)
876 {
877         void *obj = (void *)options->obj;
878         if( !obj || !opt ) return;
879         av_opt_set(obj, opt->name, val, 0);
880 }
881
882
883 FFOptionsKindItem::FFOptionsKindItem(FFOptionsKind *kind, const char *text, int idx)
884  : BC_MenuItem(text)
885 {
886         this->kind = kind;
887         this->idx = idx;
888 }
889
890 FFOptionsKindItem::~FFOptionsKindItem()
891 {
892 }
893
894 int FFOptionsKindItem::handle_event()
895 {
896         FFOptionsWindow *fwin = kind->fwin;
897         FFOptions &options = fwin->options;
898         options.initialize(fwin, idx);
899         fwin->draw();
900         return 1;
901 }
902
903 const char *FFOptionsKind::kinds[] = {
904         N_("codec"),    // FF_KIND_CODEC
905         N_("ffmpeg"),   // FF_KIND_FFMPEG
906 };
907
908 FFOptionsKind::
909 FFOptionsKind(FFOptionsWindow *fwin, int x, int y, int w)
910  : BC_PopupMenu(x, y, w-calculate_w(0), "")
911 {
912         this->fwin = fwin;
913 }
914
915 FFOptionsKind::
916 ~FFOptionsKind()
917 {
918 }
919
920 void FFOptionsKind::create_objects()
921 {
922         for( int i=0; i<(int)(sizeof(kinds)/sizeof(kinds[0])); ++i )
923                 add_item(new FFOptionsKindItem(this, _(kinds[i]), i));
924 }
925
926 int FFOptionsKind::handle_event()
927 {
928         return 1;
929 }
930
931 void FFOptionsKind::set(int k)
932 {
933         this->kind = k;
934         set_text(_(kinds[k]));
935 }
936
937 FFOptionsText::
938 FFOptionsText(FFOptionsWindow *fwin, int x, int y, int w)
939  : BC_TextBox(x, y, w, 1, (char *)"")
940 {
941         this->fwin = fwin;
942 }
943
944 FFOptionsText::
945 ~FFOptionsText()
946 {
947 }
948
949 int FFOptionsText::handle_event()
950 {
951         return 0;
952 }
953
954 FFOptionsUnits::
955 FFOptionsUnits(FFOptionsWindow *fwin, int x, int y, int w)
956  : BC_PopupMenu(x, y, w, "")
957 {
958         this->fwin = fwin;
959 }
960
961 FFOptionsUnits::
962 ~FFOptionsUnits()
963 {
964 }
965
966 int FFOptionsUnits::handle_event()
967 {
968         const char *text = get_text();
969         if( text && fwin->selected ) {
970                 fwin->selected->set(text);
971                 fwin->selected->item_value->update();
972                 av_dict_set(&fwin->dialog->ff_opts,
973                         fwin->selected->item_name->get_text(),
974                         fwin->selected->item_value->get_text(), 0);
975                 fwin->draw();
976         }
977         return 1;
978 }
979
980 FFOptionsApply::
981 FFOptionsApply(FFOptionsWindow *fwin, int x, int y)
982  : BC_GenericButton(x, y, _("Apply"))
983 {
984         this->fwin = fwin;
985 }
986
987 FFOptionsApply::
988 ~FFOptionsApply()
989 {
990 }
991
992 int FFOptionsApply::handle_event()
993 {
994         const char *text = fwin->text->get_text();
995         if( text && fwin->selected ) {
996                 fwin->selected->set(text);
997                 fwin->selected->item_value->update();
998                 av_dict_set(&fwin->dialog->ff_opts,
999                         fwin->selected->item_name->get_text(),
1000                         fwin->selected->item_value->get_text(), 0);
1001                 fwin->draw();
1002         }
1003         return 1;
1004 }
1005
1006 FFOptions::FFOptions()
1007 {
1008         avctx = 0;
1009         obj = 0;
1010 }
1011
1012 FFOptions::~FFOptions()
1013 {
1014         remove_all_objects();
1015         avcodec_free_context(&avctx);
1016 }
1017
1018 int FFOptions::cmpr(const void *a, const void *b)
1019 {
1020         FFOptions_Opt *ap = *(FFOptions_Opt **)a;
1021         FFOptions_Opt *bp = *(FFOptions_Opt **)b;
1022         const char *vap = ap->item_name->get_text();
1023         const char *vbp = bp->item_name->get_text();
1024         return strcmp(vap, vbp);
1025 }
1026
1027 void FFOptions::initialize(FFOptionsWindow *win, int kind)
1028 {
1029         remove_all_objects();
1030         this->win = win;
1031         win->selected = 0;
1032         obj = 0;
1033         if( !avctx )
1034                 avctx = avcodec_alloc_context3(win->dialog->codec);
1035
1036         switch( kind ) {
1037         case FF_KIND_CODEC:
1038                 obj = (const void *)avctx->priv_data;
1039                 break;
1040         case FF_KIND_FFMPEG:
1041                 obj = (const void *)avctx;
1042                 break;
1043         }
1044
1045         if( obj ) {
1046                 FFOptions &conf = *this;
1047                 const AVOption *opt = 0;
1048                 while( (opt=av_opt_next(obj, opt)) != 0 ) {
1049                         if( opt->type == AV_OPT_TYPE_CONST ) continue;
1050                         int dupl = 0;
1051                         for( int i=0; !dupl && i<size(); ++i ) {
1052                                 FFOptions_Opt *fopt = conf[i];
1053                                 const AVOption *op = fopt->opt;
1054                                 if( op->offset != opt->offset ) continue;
1055                                 if( op->type != opt->type ) continue;
1056                                 dupl = 1;
1057                                 if( strlen(op->name) < strlen(opt->name) )
1058                                         fopt->opt = opt;
1059                         }
1060                         if( dupl ) continue;
1061                         FFOptions_Opt *fopt = new FFOptions_Opt(this, opt, opt->name);
1062                         append(fopt);
1063                         AVDictionaryEntry *elem = av_dict_get(win->dialog->ff_opts,
1064                                         opt->name, 0, AV_DICT_IGNORE_SUFFIX);
1065                         if( elem && elem->value ) fopt->set(elem->value);
1066                         char val[BCTEXTLEN], *vp = fopt->get(val, sizeof(val));
1067                         fopt->item_value->update(vp);
1068                 }
1069         }
1070
1071         qsort(&values[0],size(),sizeof(values[0]),cmpr);
1072         win->kind->set(kind);
1073         win->panel->update();
1074         win->panel->set_yposition(0);
1075 }
1076
1077 int FFOptions::update()
1078 {
1079         int ret = 0;
1080         FFOptions &conf = *this;
1081
1082         for( int i=0; i<size(); ++i ) {
1083                 FFOptions_Opt *fopt = conf[i];
1084                 char val[BCTEXTLEN], *vp = fopt->get(val, sizeof(val));
1085                 if( !vp || !strcmp(val, fopt->item_value->get_text()) ) continue;
1086                 fopt->item_value->update(val);
1087                 ++ret;
1088         }
1089         return ret;
1090 }
1091
1092 void FFOptions::dump(FILE *fp)
1093 {
1094         if( !obj ) return;
1095         const AVOption *opt = 0;
1096         FFOptions &conf = *this;
1097
1098         while( (opt=av_opt_next(obj, opt)) != 0 ) {
1099                 if( opt->type == AV_OPT_TYPE_CONST ) continue;
1100                 int k = size();
1101                 while( --k >= 0 && strcmp(opt->name, conf[k]->opt->name) );
1102                 if( k < 0 ) continue;
1103                 FFOptions_Opt *fopt = conf[k];
1104                 char val[BCTEXTLEN], *vp = fopt->get(val,sizeof(val));
1105                 fprintf(fp, "  %s:=%s", opt->name, vp);
1106                 if( opt->unit ) {
1107                         char unt[BCTEXTLEN], *up = unt;
1108                         fopt->units(up);
1109                         fprintf(fp, "%s", unt);
1110                 }
1111                 fprintf(fp, "\n");
1112         }
1113 }
1114
1115
1116 void FFOptionsWindow::update(FFOptions_Opt *opt)
1117 {
1118         if( selected != opt ) {
1119                 if( selected ) selected->item_name->set_selected(0);
1120                 selected = opt;
1121                 if( selected ) selected->item_name->set_selected(1);
1122         }
1123         clear_box(0,0, 0,panel->get_y());
1124         char str[BCTEXTLEN], *sp;
1125         *(sp=str) = 0;
1126         if( opt ) opt->types(sp);
1127         type->update(str);
1128         *(sp=str) = 0;
1129         if( opt ) opt->ranges(sp);
1130         range->update(str);
1131         while( units->total_items() ) units->del_item(0);
1132         char unit[BCSTRLEN];  strcpy(unit, "()");
1133         if( opt && opt->opt ) {
1134                 ArrayList<const char *> names;
1135                 int n = 0;
1136                 if( opt->opt->unit ) {
1137                         n = opt->units(names);
1138                         if( n > 0 ) strcpy(unit,opt->opt->unit);
1139                 }
1140                 for( int i=0; i<n; ++i )
1141                         units->add_item(new BC_MenuItem(names[i], 0));
1142         }
1143         units->set_text(unit);
1144         char val[BCTEXTLEN];  val[0] = 0;
1145         if( opt ) opt->get(val, sizeof(val));
1146         text->update(val);
1147
1148         panel->update();
1149 }
1150
1151 void FFOptions_OptPanel::show_tip(const char *tip)
1152 {
1153         if( !tip ) return;
1154         int len = strlen(tip);
1155         if( len > (int)sizeof(tip_text)-1 ) len = sizeof(tip_text)-1;
1156         strncpy(tip_text,tip,len);
1157         tip_text[len] = 0;
1158         int line_limit = 60;
1159         int limit2 = line_limit/2;
1160         int limit4 = line_limit/4-2;
1161         char *cp = tip_text, *dp = cp+len;
1162         int n;  char *bp, *ep, *pp, *sp;
1163         while( cp < dp ) {
1164                 for( ep=cp; ep<dp && *ep!='\n'; ++ep );
1165                 // target about half remaining line, constrain line_limit
1166                 if( (n=(ep-1-cp)/2) < limit2 || n > line_limit )
1167                         n = line_limit;
1168                 // search for last punct, last space before line_limit
1169                 for( bp=cp, pp=sp=0; --n>=0 && cp<ep; ++cp ) {
1170                         if( ispunct(*cp) && isspace(*(cp+1)) ) pp = cp;
1171                         else if( isspace(*cp) ) sp = cp;
1172                 }
1173                 // line not empty
1174                 if( cp < ep ) {
1175                         // first, after punctuation
1176                         if( pp && pp-bp >= limit4 )
1177                                 cp = pp+1;
1178                         // then, on spaces
1179                         else if( sp ) {
1180                                 cp = sp;
1181                         }
1182                         // last, on next space
1183                         else {
1184                                 while( cp<dp && !isspace(*cp) ) ++cp;
1185                         }
1186                         // add new line
1187                         if( !*cp ) break;
1188                         *cp++ = '\n';
1189                 }
1190         }
1191         fwin->panel->set_tooltip(tip_text);
1192         fwin->panel->show_tooltip();
1193 }
1194
1195 int FFOptions_OptPanel::selection_changed()
1196 {
1197         FFOptions_Opt *opt = 0;
1198         BC_ListBoxItem *item = get_selection(0, 0);
1199         if( item ) {
1200                 FFOptions_OptName *opt_name = (FFOptions_OptName *)item;
1201                 opt = opt_name->opt;
1202         }
1203         fwin->update(opt);
1204         if( opt ) show_tip(opt->tip());
1205         return 1;
1206 }
1207
1208
1209 int FFOptions_Opt::types(char *rp)
1210 {
1211         const char *cp = "";
1212         if( opt ) switch (opt->type) {
1213         case AV_OPT_TYPE_FLAGS: cp = N_("<flags>");  break;
1214         case AV_OPT_TYPE_INT: cp = N_("<int>"); break;
1215         case AV_OPT_TYPE_INT64: cp = N_("<int64>"); break;
1216         case AV_OPT_TYPE_DOUBLE: cp = N_("<double>"); break;
1217         case AV_OPT_TYPE_FLOAT: cp = N_("<float>"); break;
1218         case AV_OPT_TYPE_STRING: cp = N_("<string>"); break;
1219         case AV_OPT_TYPE_RATIONAL: cp = N_("<rational>"); break;
1220         case AV_OPT_TYPE_BINARY: cp = N_("<binary>"); break;
1221         case AV_OPT_TYPE_IMAGE_SIZE: cp = N_("<image_size>"); break;
1222         case AV_OPT_TYPE_VIDEO_RATE: cp = N_("<video_rate>"); break;
1223         case AV_OPT_TYPE_PIXEL_FMT: cp = N_("<pix_fmt>"); break;
1224         case AV_OPT_TYPE_SAMPLE_FMT: cp = N_("<sample_fmt>"); break;
1225         case AV_OPT_TYPE_DURATION: cp = N_("<duration>"); break;
1226         case AV_OPT_TYPE_COLOR: cp = N_("<color>"); break;
1227         case AV_OPT_TYPE_CHANNEL_LAYOUT: cp = N_("<channel_layout>");  break;
1228         case AV_OPT_TYPE_BOOL: cp = N_("<bool>");  break;
1229         default: cp = N_("<undef>");  break;
1230         }
1231         return sprintf(rp, "%s", _(cp));
1232 }
1233 int FFOptions_Opt::scalar(double d, char *rp)
1234 {
1235         const char *cp = 0;
1236              if( d == INT_MAX ) cp = "INT_MAX";
1237         else if( d == INT_MIN ) cp = "INT_MIN";
1238         else if( d == UINT32_MAX ) cp = "UINT32_MAX";
1239         else if( d == (double)INT64_MAX ) cp = "I64_MAX";
1240         else if( d == INT64_MIN ) cp = "I64_MIN";
1241         else if( d == FLT_MAX ) cp = "FLT_MAX";
1242         else if( d == FLT_MIN ) cp = "FLT_MIN";
1243         else if( d == -FLT_MAX ) cp = "-FLT_MAX";
1244         else if( d == -FLT_MIN ) cp = "-FLT_MIN";
1245         else if( d == DBL_MAX ) cp = "DBL_MAX";
1246         else if( d == DBL_MIN ) cp = "DBL_MIN";
1247         else if( d == -DBL_MAX ) cp = "-DBL_MAX";
1248         else if( d == -DBL_MIN ) cp = "-DBL_MIN";
1249         else if( d == 0 ) cp = signbit(d) ? "-0" : "0";
1250         else if( isnan(d) ) cp = signbit(d) ? "-NAN" : "NAN";
1251         else if( isinf(d) ) cp = signbit(d) ? "-INF" : "INF";
1252         else return sprintf(rp, "%g", d);
1253         return sprintf(rp, "%s", cp);
1254 }
1255
1256 int FFOptions_Opt::ranges(char *rp)
1257 {
1258         if( !opt ) return 0;
1259         void *obj = (void *)options->obj;
1260         if( !obj ) return 0;
1261
1262         switch (opt->type) {
1263         case AV_OPT_TYPE_INT:
1264         case AV_OPT_TYPE_INT64:
1265         case AV_OPT_TYPE_DOUBLE:
1266         case AV_OPT_TYPE_FLOAT: break;
1267         default: return 0;;
1268         }
1269         AVOptionRanges *r = 0;
1270         char *cp = rp;
1271         if( av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) < 0 ) return 0;
1272         for( int i=0; i<r->nb_ranges; ++i ) {
1273                 cp += sprintf(cp, " (");  cp += scalar(r->range[i]->value_min, cp);
1274                 cp += sprintf(cp, "..");  cp += scalar(r->range[i]->value_max, cp);
1275                 cp += sprintf(cp, ")");
1276         }
1277         av_opt_freep_ranges(&r);
1278         return cp - rp;
1279 }
1280
1281 int FFOptions_Opt::units(ArrayList<const char *> &names)
1282 {
1283         if( !opt || !opt->unit ) return 0;
1284         const void *obj = options->obj;
1285         if( !obj ) return 0;
1286
1287         ArrayList<const AVOption *> opts;
1288         const AVOption *opt = NULL;
1289         while( (opt=av_opt_next(obj, opt)) != 0 ) {
1290                 if( !opt->unit ) continue;
1291                 if( opt->type != AV_OPT_TYPE_CONST ) continue;
1292                 if( strcmp(this->opt->unit, opt->unit) ) continue;
1293                 int i = opts.size();
1294                 while( --i >= 0 ) {
1295                         if( opts[i]->default_val.i64 != opt->default_val.i64 ) continue;
1296                         if( strlen(opts[i]->name) < strlen(opt->name) ) opts[i] = opt;
1297                         break;
1298                 }
1299                 if( i >= 0 ) continue;
1300                 opts.append(opt);
1301         }
1302
1303         for( int i=0; i<opts.size(); ++i )
1304                 names.append(opts[i]->name);
1305
1306         return names.size();
1307 }
1308
1309 int FFOptions_Opt::units(char *rp)
1310 {
1311         ArrayList<const char *> names;
1312         int n = units(names);
1313         if( !n ) return 0;
1314         char *cp = rp;
1315         cp += sprintf(cp, " [%s:", this->opt->unit);
1316         for( int i=0; i<n; ++i )
1317                 cp += sprintf(cp, " %s", names[i]);
1318         cp += sprintf(cp, "]:");
1319         return cp - rp;
1320 }
1321
1322 const char *FFOptions_Opt::unit_name(int id)
1323 {
1324         if( !opt || !opt->unit ) return 0;
1325         const void *obj = options->obj;
1326         if( !obj ) return 0;
1327
1328         const char *ret = 0;
1329         const AVOption *opt = NULL;
1330         while( (opt=av_opt_next(obj, opt)) != 0 ) {
1331                 if( !opt->unit ) continue;
1332                 if( opt->type != AV_OPT_TYPE_CONST ) continue;
1333                 if( strcmp(this->opt->unit, opt->unit) ) continue;
1334                 if( opt->default_val.i64 != id ) continue;
1335                 if( !ret ) { ret = opt->name;  continue; }
1336                 if( strlen(ret) < strlen(opt->name) ) ret = opt->name;
1337         }
1338
1339         return ret;
1340 }
1341
1342 const char *FFOptions_Opt::tip()
1343 {
1344         return !opt ? 0 : opt->help;
1345 }
1346
1347
1348 FFOptionsWindow::FFOptionsWindow(FFOptionsDialog *dialog)
1349  : BC_Window(_(PROGRAM_NAME ": Options"), 60, 30, 640, 400)
1350 {
1351         this->dialog = dialog;
1352         this->selected = 0;
1353 }
1354
1355 FFOptionsWindow::~FFOptionsWindow()
1356 {
1357 }
1358
1359 void FFOptionsWindow::create_objects()
1360 {
1361         BC_Title *title;
1362         int x0 = 10, y0 = 10;
1363         int x = x0, y = y0;
1364         add_subwindow(title = new BC_Title(x, y, _("Format: ")));
1365         x += title->get_w();
1366         add_subwindow(new BC_Title(x, y, dialog->format_name));
1367         x = x0 + 150;
1368         add_subwindow(title = new BC_Title(x, y, _("Codec: ")));
1369         x += title->get_w();
1370         add_subwindow(new BC_Title(x, y, dialog->codec_name));
1371
1372         x = x0;  y += title->get_h() + 10;  y0 = y;
1373         add_subwindow(title = new BC_Title(x, y, _("Type: ")));
1374         x += title->get_w() + 8;
1375         add_subwindow(type = new BC_Title(x, y, (char *)""));
1376         x = x0 + 150;
1377         add_subwindow(title = new BC_Title(x, y, _("Range: ")));
1378         x += title->get_w() + 8;
1379         add_subwindow(range = new BC_Title(x, y, (char *)""));
1380
1381         x = x0;  y += title->get_h() + 10;
1382         add_subwindow(units = new FFOptionsUnits(this, x, y, 120));
1383         x += units->get_w() + 8;
1384         int x1 = get_w() - BC_GenericButton::calculate_w(this, _("Apply")) - 8;
1385         add_subwindow(text = new FFOptionsText(this, x, y, x1-x - 8));
1386         add_subwindow(apply = new FFOptionsApply(this, x1, y));
1387         y += units->get_h() + 10;
1388         add_subwindow(kind = new FFOptionsKind(this, x1, y0, apply->get_w()));
1389         kind->create_objects();
1390         const char *kind_text = _("Kind:");
1391         x1 -= BC_Title::calculate_w(this, kind_text) + 8;
1392         add_subwindow(kind_title = new BC_Title(x1, y0, kind_text));
1393         y0 = y;
1394
1395         panel_x = x0;  panel_y = y0;
1396         panel_w = get_w()-10 - panel_x;
1397         panel_h = get_h()-10 - panel_y - BC_OKButton::calculate_h();
1398         panel = new FFOptions_OptPanel(this, panel_x, panel_y, panel_w, panel_h);
1399         add_subwindow(panel);
1400         add_subwindow(new BC_OKButton(this));
1401         add_subwindow(new BC_CancelButton(this));
1402         panel->create_objects();
1403         options.initialize(this, FF_KIND_CODEC);
1404         draw();
1405         show_window(1);
1406 }
1407
1408 void FFOptionsWindow::draw()
1409 {
1410         update(selected);
1411 }
1412
1413 int FFOptionsWindow::resize_event(int w, int h)
1414 {
1415         int x1 = w - 8 - kind->get_w();
1416         int y = kind->get_y();
1417         kind->reposition_window(x1, y);
1418         x1 -= kind_title->get_w() + 8;
1419         kind_title->reposition_window(x1,y);
1420         x1 = get_w() - apply->get_w() - 8;
1421         int y1 = units->get_y();
1422         apply->reposition_window(x1, y1);
1423         int x0 = units->get_x() + units->get_w() + 8;
1424         int y0 = units->get_y();
1425         text->reposition_window(x0,y0, x1-x0-8);
1426         panel_w = get_w()-10 - panel_x;
1427         panel_h = get_h()-10 - panel_y;
1428         panel->reposition_window(panel_x,panel_y, panel_w, panel_h);
1429         return 1;
1430 }
1431
1432 FFOptionsDialog::FFOptionsDialog()
1433  : BC_DialogThread()
1434 {
1435         this->options_window = 0;
1436         this->codec_name = 0;
1437         this->codec = 0;
1438         this->ff_opts = 0;
1439         this->ff_len = 0;
1440 }
1441
1442 FFOptionsDialog::~FFOptionsDialog()
1443 {
1444         close_window();
1445         delete [] codec_name;
1446 }
1447
1448 void FFOptionsDialog::load_options(const char *bp, int len)
1449 {
1450         char line[BCTEXTLEN];
1451         char key[BCSTRLEN], val[BCTEXTLEN];
1452         const char *dp = bp + len-1;
1453         int no = 0;
1454         while( bp < dp && *bp != 0 ) {
1455                 ++no;
1456                 char *cp = line, *ep = cp+sizeof(line)-1;
1457                 while( *bp && cp<ep && (*cp=*bp++)!='\n' ) ++cp;
1458                 *cp = 0;
1459                 if( line[0] == '#' ) {
1460                         sprintf(key,"#%d", no);
1461                         av_dict_set(&ff_opts, key, line, 0);
1462                         continue;
1463                 }
1464                 if( line[0] == '\n' ) continue;
1465                 if( FFMPEG::scan_option_line(line, key, val) ) continue;
1466                 av_dict_set(&ff_opts, key, val, 0);
1467         }
1468 }
1469
1470 void FFOptionsDialog::store_options(char *cp, int len)
1471 {
1472         char *ep = cp + len-1;
1473         AVDictionaryEntry *elem = 0;
1474         while( (elem=av_dict_get(ff_opts, "", elem, AV_DICT_IGNORE_SUFFIX)) != 0 ) {
1475                 if( elem->key[0] == '#' ) {
1476                         cp += snprintf(cp,ep-cp, "%s\n", elem->value);
1477                         continue;
1478                 }
1479                 cp += snprintf(cp,ep-cp, "%s=%s\n", elem->key, elem->value);
1480         }
1481         *cp = 0;
1482 }
1483
1484 void FFOptionsDialog::start(const char *format_name, const char *codec_name,
1485         AVCodec *codec, const char *options, int len)
1486 {
1487         if( options_window ) {
1488                 options_window->lock_window("FFOptionsDialog::start");
1489                 options_window->raise_window();
1490                 options_window->unlock_window();
1491                 return;
1492         }
1493
1494         this->format_name = cstrdup(format_name);
1495         this->codec_name = cstrdup(codec_name);
1496         this->codec = codec;
1497         this->ff_opts = 0;
1498         this->ff_len = len;
1499         load_options(options, len);
1500
1501         BC_DialogThread::start();
1502 }
1503
1504 BC_Window* FFOptionsDialog::new_gui()
1505 {
1506         options_window = new FFOptionsWindow(this);
1507         options_window->create_objects();
1508         return options_window;
1509 }
1510
1511 void FFOptionsDialog::handle_done_event(int result)
1512 {
1513         if( !result ) {
1514                 char options[ff_len];
1515                 store_options(options, ff_len);
1516                 update_options(options);
1517         }
1518         options_window = 0;
1519         delete [] format_name; format_name = 0;
1520         delete [] codec_name;  codec_name = 0;
1521         av_dict_free(&ff_opts);
1522 }
1523
1524 FFOptionsAudioDialog::FFOptionsAudioDialog(FFMPEGConfigAudio *aud_config)
1525 {
1526         this->aud_config = aud_config;
1527 }
1528
1529 FFOptionsAudioDialog::~FFOptionsAudioDialog()
1530 {
1531         close_window();
1532 }
1533
1534 void FFOptionsAudioDialog::update_options(const char *options)
1535 {
1536         aud_config->audio_options->update(options);
1537 }
1538
1539 FFOptionsVideoDialog::FFOptionsVideoDialog(FFMPEGConfigVideo *vid_config)
1540 {
1541         this->vid_config = vid_config;
1542 }
1543
1544 FFOptionsVideoDialog::~FFOptionsVideoDialog()
1545 {
1546         close_window();
1547 }
1548
1549 void FFOptionsVideoDialog::update_options(const char *options)
1550 {
1551         vid_config->video_options->update(options);
1552 }
1553
1554
1555 FFOptionsViewAudio::FFOptionsViewAudio(FFMPEGConfigAudio *aud_config, int x, int y, const char *text)
1556  : BC_GenericButton(x, y, text)
1557 {
1558         this->aud_config = aud_config;
1559 }
1560
1561 FFOptionsViewAudio::~FFOptionsViewAudio()
1562 {
1563 }
1564
1565 int FFOptionsViewAudio::handle_event()
1566 {
1567         char audio_format[BCSTRLEN]; audio_format[0] = 0;
1568         char audio_codec[BCSTRLEN]; audio_codec[0] = 0;
1569         AVCodec *codec = 0;
1570         Asset *asset = aud_config->asset;
1571         const char *name = asset->acodec;
1572         if( !FFMPEG::get_format(audio_format, "audio", name) &&
1573             !FFMPEG::get_codec(audio_codec, "audio", name) )
1574                 codec = avcodec_find_encoder_by_name(audio_codec);
1575         if( !codec ) {
1576                 eprintf(_("no codec named: %s: %s"), name, audio_codec);
1577                 return 1;
1578         }
1579         aud_config->ff_options_dialog->start(audio_format, audio_codec, codec,
1580                 asset->ff_audio_options, sizeof(asset->ff_audio_options));
1581         return 1;
1582 }
1583
1584 FFOptionsViewVideo::FFOptionsViewVideo(FFMPEGConfigVideo *vid_config, int x, int y, const char *text)
1585  : BC_GenericButton(x, y, text)
1586 {
1587         this->vid_config = vid_config;
1588 }
1589
1590 FFOptionsViewVideo::~FFOptionsViewVideo()
1591 {
1592 }
1593
1594 int FFOptionsViewVideo::handle_event()
1595 {
1596         char video_format[BCSTRLEN]; video_format[0] = 0;
1597         char video_codec[BCSTRLEN]; video_codec[0] = 0;
1598         AVCodec *codec = 0;
1599         Asset *asset = vid_config->asset;
1600         const char *name = asset->vcodec;
1601         if( !FFMPEG::get_format(video_format, "video", name) &&
1602             !FFMPEG::get_codec(video_codec, "video", name) )
1603                 codec = avcodec_find_encoder_by_name(video_codec);
1604         if( !codec ) {
1605                 eprintf(_("no codec named: %s: %s"), name, video_codec);
1606                 return 1;
1607         }
1608         vid_config->ff_options_dialog->start(video_format, video_codec, codec,
1609                 asset->ff_video_options, sizeof(asset->ff_video_options));
1610         return 1;
1611 }
1612