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