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