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