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