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