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