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