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