7 // work around for __STDC_CONSTANT_MACROS
11 #include "bcwindowbase.h"
12 #include "bcprogressbox.h"
13 #include "bitspopup.h"
17 #include "fileffmpeg.h"
18 #include "filesystem.h"
20 #include "indexfile.h"
22 #include "preferences.h"
23 #include "videodevice.inc"
25 FileFFMPEG::FileFFMPEG(Asset *asset, File *file)
26 : FileBase(asset, file)
29 if(asset->format == FILE_UNKNOWN)
30 asset->format = FILE_FFMPEG;
33 FileFFMPEG::~FileFFMPEG()
39 FFMpegConfigNum::FFMpegConfigNum(BC_Window *window,
40 int x, int y, char *title_text, int *output)
41 : BC_TumbleTextBox(window, (int64_t)*output,
42 (int64_t)-1, (int64_t)25000000, 100, y, 100)
44 this->window = window;
45 this->x = x; this->y = y;
46 this->title_text = title_text;
47 this->output = output;
50 FFMpegConfigNum::~FFMpegConfigNum()
54 void FFMpegConfigNum::create_objects()
56 window->add_subwindow(title = new BC_Title(x, y, title_text));
57 BC_TumbleTextBox::create_objects();
60 int FFMpegConfigNum::handle_event()
62 *output = atol(get_text());
66 FFMpegAudioNum::FFMpegAudioNum(BC_Window *window,
67 int x, int y, char *title_text, int *output)
68 : FFMpegConfigNum(window, x, y, title_text, output)
72 int FFMpegAudioBitrate::handle_event()
74 int ret = FFMpegAudioNum::handle_event();
78 FFMpegVideoNum::FFMpegVideoNum(BC_Window *window,
79 int x, int y, char *title_text, int *output)
80 : FFMpegConfigNum(window, x, y, title_text, output)
84 int FFMpegVideoBitrate::handle_event()
86 int ret = FFMpegVideoNum::handle_event();
87 Asset *asset = window()->asset;
88 if( asset->ff_video_bitrate )
89 window()->quality->disable();
91 window()->quality->enable();
95 int FFMpegVideoQuality::handle_event()
97 int ret = FFMpegVideoNum::handle_event();
98 Asset *asset = window()->asset;
99 if( asset->ff_video_quality )
100 window()->bitrate->disable();
102 window()->bitrate->enable();
106 void FileFFMPEG::get_parameters(BC_WindowBase *parent_window,
107 Asset *asset, BC_WindowBase *&format_window,
108 int audio_options, int video_options)
111 FFMPEGConfigAudio *window = new FFMPEGConfigAudio(parent_window, asset);
112 format_window = window;
113 window->create_objects();
114 window->run_window();
117 else if(video_options) {
118 FFMPEGConfigVideo *window = new FFMPEGConfigVideo(parent_window, asset);
119 format_window = window;
120 window->create_objects();
121 window->run_window();
126 int FileFFMPEG::check_sig(Asset *asset)
128 char *ptr = strstr(asset->path, ".pcm");
130 ptr = strstr(asset->path, ".raw");
134 int ret = !ffmpeg.init_decoder(asset->path) &&
135 !ffmpeg.open_decoder() ? 1 : 0;
139 void FileFFMPEG::get_info(char *path, char *text, int len)
143 cp += sprintf(cp, _("file path: %s\n"), path);
146 if( stat(path, &st) < 0 ) {
147 cp += sprintf(cp, _(" err: %s\n"), strerror(errno));
151 cp += sprintf(cp, _(" %jd bytes\n"), st.st_size);
153 if( !ret ) ret = ffmpeg.init_decoder(path);
154 if( !ret ) ret = ffmpeg.open_decoder();
156 cp += sprintf(cp, _("info:\n"));
157 ffmpeg.info(cp, len-(cp-text));
160 sprintf(cp, _("== open failed\n"));
163 int FileFFMPEG::get_video_info(int track, int &pid, double &framerate,
164 int &width, int &height, char *title)
167 pid = ff->ff_video_pid(track);
168 framerate = ff->ff_frame_rate(track);
169 width = ff->ff_video_width(track);
170 height = ff->ff_video_height(track);
171 if( title ) *title = 0;
175 int FileFFMPEG::get_audio_for_video(int vstream, int astream, int64_t &channel_mask)
178 return ff->ff_audio_for_video(vstream, astream, channel_mask);
181 int FileFFMPEG::select_video_stream(Asset *asset, int vstream)
183 if( !ff || !asset->video_data ) return 1;
184 asset->width = ff->ff_video_width(vstream);
185 asset->height = ff->ff_video_height(vstream);
186 asset->video_length = ff->ff_video_frames(vstream);
187 asset->frame_rate = ff->ff_frame_rate(vstream);
191 int FileFFMPEG::select_audio_stream(Asset *asset, int astream)
193 if( !ff || !asset->audio_data ) return 1;
194 asset->sample_rate = ff->ff_sample_rate(astream);
195 asset->audio_length = ff->ff_audio_samples(astream);
199 int FileFFMPEG::open_file(int rd, int wr)
203 ff = new FFMPEG(this);
206 result = ff->init_decoder(asset->path);
207 if( !result ) result = ff->open_decoder();
209 int audio_channels = ff->ff_total_audio_channels();
210 if( audio_channels > 0 ) {
211 asset->audio_data = 1;
212 asset->channels = audio_channels;
213 asset->sample_rate = ff->ff_sample_rate(0);
214 asset->audio_length = ff->ff_audio_samples(0);
216 int video_layers = ff->ff_total_video_layers();
217 if( video_layers > 0 ) {
218 asset->video_data = 1;
219 if( !asset->layers ) asset->layers = video_layers;
220 asset->actual_width = ff->ff_video_width(0);
221 asset->actual_height = ff->ff_video_height(0);
222 if( !asset->width ) asset->width = asset->actual_width;
223 if( !asset->height ) asset->height = asset->actual_height;
224 if( !asset->video_length ) asset->video_length = ff->ff_video_frames(0);
225 if( !asset->frame_rate ) asset->frame_rate = ff->ff_frame_rate(0);
227 IndexState *index_state = asset->index_state;
228 index_state->read_markers(file->preferences->index_directory, asset->path);
232 result = ff->init_encoder(asset->path);
233 // must be in this order or dvdauthor will fail
234 if( !result && asset->video_data )
235 result = ff->open_encoder("video", asset->vcodec);
236 if( !result && asset->audio_data )
237 result = ff->open_encoder("audio", asset->acodec);
242 int FileFFMPEG::close_file()
250 int FileFFMPEG::set_video_position(int64_t pos)
252 if( !ff || pos < 0 || pos >= asset->video_length )
258 int FileFFMPEG::set_audio_position(int64_t pos)
260 if( !ff || pos < 0 || pos >= asset->audio_length )
266 int FileFFMPEG::write_samples(double **buffer, int64_t len)
268 if( !ff || len < 0 ) return -1;
270 int ret = ff->encode(stream, buffer, len);
274 int FileFFMPEG::write_frames(VFrame ***frames, int len)
277 int ret = 0, layer = 0;
278 for(int i = 0; i < 1; i++) {
279 for(int j = 0; j < len && !ret; j++) {
280 VFrame *frame = frames[i][j];
281 ret = ff->encode(layer, frame);
288 int FileFFMPEG::read_samples(double *buffer, int64_t len)
290 if( !ff || len < 0 ) return -1;
291 int ch = file->current_channel;
292 int64_t pos = file->current_sample;
293 ff->decode(ch, pos, buffer, len);
297 int FileFFMPEG::read_frame(VFrame *frame)
300 int layer = file->current_layer;
301 int64_t pos = file->current_frame;
302 ff->decode(layer, pos, frame);
307 int64_t FileFFMPEG::get_memory_usage()
312 int FileFFMPEG::colormodel_supported(int colormodel)
317 int FileFFMPEG::get_best_colormodel(Asset *asset, int driver)
320 case PLAYBACK_X11: return BC_RGB888;
321 case PLAYBACK_X11_GL: return BC_YUV888;
328 FFMPEGConfigAudio::FFMPEGConfigAudio(BC_WindowBase *parent_window, Asset *asset)
329 : BC_Window(_(PROGRAM_NAME ": Audio Preset"),
330 parent_window->get_abs_cursor_x(1),
331 parent_window->get_abs_cursor_y(1),
334 this->parent_window = parent_window;
342 FFMPEGConfigAudio::~FFMPEGConfigAudio()
344 lock_window("FFMPEGConfigAudio::~FFMPEGConfigAudio");
345 if(preset_popup) delete preset_popup;
346 presets.remove_all_objects();
350 void FFMPEGConfigAudio::create_objects()
353 lock_window("FFMPEGConfigAudio::create_objects");
356 char option_path[BCTEXTLEN];
357 FFMPEG::set_option_path(option_path, "audio");
358 fs.update(option_path);
359 int total_files = fs.total_files();
360 for(int i = 0; i < total_files; i++) {
361 const char *name = fs.get_entry(i)->get_name();
362 if( asset->fformat[0] != 0 ) {
363 const char *ext = strrchr(name,'.');
365 if( strcmp(asset->fformat, ++ext) ) continue;
367 presets.append(new BC_ListBoxItem(name));
370 if( asset->acodec[0] ) {
371 int k = presets.size();
372 while( --k >= 0 && strcmp(asset->acodec, presets[k]->get_text()) );
373 if( k < 0 ) asset->acodec[0] = 0;
376 if( !asset->acodec[0] && presets.size() > 0 )
377 strcpy(asset->acodec, presets[0]->get_text());
379 add_tool(new BC_Title(x, y, _("Preset:")));
381 preset_popup = new FFMPEGConfigAudioPopup(this, x, y);
382 preset_popup->create_objects();
385 bitrate = new FFMpegAudioBitrate(this, x, y, _("Bitrate:"), &asset->ff_audio_bitrate);
386 bitrate->create_objects();
387 bitrate->set_increment(1000);
389 y += bitrate->get_h() + 10;
390 add_subwindow(new BC_Title(x, y, _("Audio Options:")));
392 if( !asset->ff_audio_options[0] && asset->acodec[0] ) {
393 FFMPEG::set_option_path(option_path, "audio/%s", asset->acodec);
394 FFMPEG::load_options(option_path, asset->ff_audio_options,
395 sizeof(asset->ff_audio_options));
397 add_subwindow(audio_options = new FFAudioOptions(this, x, y, get_w()-x-20, 10,
398 sizeof(asset->ff_audio_options)-1, asset->ff_audio_options));
399 add_subwindow(new BC_OKButton(this));
401 bitrate->handle_event();
405 int FFMPEGConfigAudio::close_event()
412 FFAudioOptions::FFAudioOptions(FFMPEGConfigAudio *audio_popup,
413 int x, int y, int w, int rows, int size, char *text)
414 : BC_TextBox(x, y, w, rows, size, text)
416 this->audio_popup = audio_popup;
419 int FFAudioOptions::handle_event()
421 strcpy(audio_popup->asset->ff_audio_options, get_text());
426 FFMPEGConfigAudioPopup::FFMPEGConfigAudioPopup(FFMPEGConfigAudio *popup, int x, int y)
427 : BC_PopupTextBox(popup, &popup->presets, popup->asset->acodec, x, y, 300, 300)
432 int FFMPEGConfigAudioPopup::handle_event()
434 strcpy(popup->asset->acodec, get_text());
435 Asset *asset = popup->asset;
436 char option_path[BCTEXTLEN];
437 FFMPEG::set_option_path(option_path, "audio/%s", asset->acodec);
438 FFMPEG::load_options(option_path, asset->ff_audio_options,
439 sizeof(asset->ff_audio_options));
440 popup->audio_options->update(asset->ff_audio_options);
445 FFMPEGConfigAudioToggle::FFMPEGConfigAudioToggle(FFMPEGConfigAudio *popup,
446 char *title_text, int x, int y, int *output)
447 : BC_CheckBox(x, y, *output, title_text)
450 this->output = output;
452 int FFMPEGConfigAudioToggle::handle_event()
454 *output = get_value();
461 FFMPEGConfigVideo::FFMPEGConfigVideo(BC_WindowBase *parent_window, Asset *asset)
462 : BC_Window(_(PROGRAM_NAME ": Video Preset"),
463 parent_window->get_abs_cursor_x(1),
464 parent_window->get_abs_cursor_y(1),
467 this->parent_window = parent_window;
476 FFMPEGConfigVideo::~FFMPEGConfigVideo()
478 lock_window("FFMPEGConfigVideo::~FFMPEGConfigVideo");
479 if(preset_popup) delete preset_popup;
480 presets.remove_all_objects();
484 void FFMPEGConfigVideo::create_objects()
487 lock_window("FFMPEGConfigVideo::create_objects");
489 add_subwindow(new BC_Title(x, y, _("Compression:")));
493 char option_path[BCTEXTLEN];
494 FFMPEG::set_option_path(option_path, "video");
495 fs.update(option_path);
496 int total_files = fs.total_files();
497 for(int i = 0; i < total_files; i++) {
498 const char *name = fs.get_entry(i)->get_name();
499 if( asset->fformat[0] != 0 ) {
500 const char *ext = strrchr(name,'.');
502 if( strcmp(asset->fformat, ++ext) ) continue;
504 presets.append(new BC_ListBoxItem(name));
507 if( asset->vcodec[0] ) {
508 int k = presets.size();
509 while( --k >= 0 && strcmp(asset->vcodec, presets[k]->get_text()) );
510 if( k < 0 ) asset->vcodec[0] = 0;
513 if( !asset->vcodec[0] && presets.size() > 0 )
514 strcpy(asset->vcodec, presets[0]->get_text());
516 preset_popup = new FFMPEGConfigVideoPopup(this, x, y);
517 preset_popup->create_objects();
519 if( asset->ff_video_bitrate && asset->ff_video_quality ) {
520 asset->ff_video_bitrate = 0;
521 asset->ff_video_quality = 0;
525 bitrate = new FFMpegVideoBitrate(this, x, y, _("Bitrate:"), &asset->ff_video_bitrate);
526 bitrate->create_objects();
527 bitrate->set_increment(100000);
528 y += bitrate->get_h() + 5;
529 quality = new FFMpegVideoQuality(this, x, y, _("Quality:"), &asset->ff_video_quality);
530 quality->create_objects();
531 quality->set_increment(1);
532 quality->set_boundaries((int64_t)0, (int64_t)31);
534 y += quality->get_h() + 10;
535 add_subwindow(new BC_Title(x, y, _("Video Options:")));
537 if( !asset->ff_video_options[0] && asset->vcodec[0] ) {
538 FFMPEG::set_option_path(option_path, "video/%s", asset->vcodec);
539 FFMPEG::load_options(option_path, asset->ff_video_options,
540 sizeof(asset->ff_video_options));
542 add_subwindow(video_options = new FFVideoOptions(this, x, y, get_w()-x-20, 10,
543 sizeof(asset->ff_video_options)-1, asset->ff_video_options));
545 add_subwindow(new BC_OKButton(this));
547 if( asset->ff_video_bitrate )
549 if( asset->ff_video_quality )
554 int FFMPEGConfigVideo::close_event()
561 FFVideoOptions::FFVideoOptions(FFMPEGConfigVideo *video_popup,
562 int x, int y, int w, int rows, int size, char *text)
563 : BC_TextBox(x, y, w, rows, size, text)
565 this->video_popup = video_popup;
568 int FFVideoOptions::handle_event()
570 strcpy(video_popup->asset->ff_video_options, get_text());
575 FFMPEGConfigVideoPopup::FFMPEGConfigVideoPopup(FFMPEGConfigVideo *popup, int x, int y)
576 : BC_PopupTextBox(popup, &popup->presets, popup->asset->vcodec, x, y, 300, 300)
581 int FFMPEGConfigVideoPopup::handle_event()
583 strcpy(popup->asset->vcodec, get_text());
584 Asset *asset = popup->asset;
585 char option_path[BCTEXTLEN];
586 FFMPEG::set_option_path(option_path, "video/%s", asset->vcodec);
587 FFMPEG::load_options(option_path, asset->ff_video_options,
588 sizeof(asset->ff_video_options));
589 popup->video_options->update(asset->ff_video_options);
594 FFMPEGConfigVideoToggle::FFMPEGConfigVideoToggle(FFMPEGConfigVideo *popup,
595 char *title_text, int x, int y, int *output)
596 : BC_CheckBox(x, y, *output, title_text)
599 this->output = output;
601 int FFMPEGConfigVideoToggle::handle_event()
603 *output = get_value();
607 FFMPEGScanProgress::FFMPEGScanProgress(const char *title, int64_t length, int64_t *position, int *canceled)
610 strcpy(this->progress_title, title);
611 this->length = length;
612 this->position = position;
613 this->canceled = canceled;
619 FFMPEGScanProgress::~FFMPEGScanProgress()
626 void FFMPEGScanProgress::run()
628 BC_ProgressBox *progress = new BC_ProgressBox(-1, -1, progress_title, length);
631 struct timeval start_time, now, then;
632 gettimeofday(&start_time, 0);
636 if( progress->update(*position, 1) ) {
637 *canceled = done = 1;
640 gettimeofday(&now, 0);
641 if(now.tv_sec - then.tv_sec >= 1) {
642 int64_t elapsed = now.tv_sec - start_time.tv_sec;
643 int64_t byte_rate = *position / elapsed;
644 int64_t eta = !byte_rate ? 0 : (length - *position) / byte_rate;
645 char string[BCTEXTLEN];
646 sprintf(string, "%s\nETA: " _LD "m" _LD "s",
647 progress_title, eta / 60, eta % 60);
648 progress->update_title(string, 1);
654 progress->stop_progress();
658 int FileFFMPEG::get_index(char *index_path)
661 if( !file->preferences->ffmpeg_marker_indecies ) return 1;
663 IndexState *index_state = asset->index_state;
664 if( index_state->index_status != INDEX_NOTTESTED ) return 0;
665 index_state->index_status = INDEX_BUILDING;
667 for( int aidx=0; aidx<ff->ffaudio.size(); ++aidx ) {
668 FFAudioStream *aud = ff->ffaudio[aidx];
669 index_state->add_audio_stream(aud->channels, aud->length);
672 char progress_title[BCTEXTLEN];
673 sprintf(progress_title, _("Creating %s\n"), index_path);
675 int64_t file_bytes = fs.get_size(ff->fmt_ctx->filename);
676 int64_t scan_position = 0;
678 FFMPEGScanProgress scan_progress(progress_title, file_bytes, &scan_position, &canceled);
680 index_state->index_bytes = file_bytes;
681 index_state->init_scan(file->preferences->index_size);
682 if( ff->scan(index_state, &scan_position, &canceled) || canceled ) {
683 index_state->reset_index();
684 index_state->reset_markers();
687 index_state->marker_status = MARKERS_READY;
688 return index_state->create_index(index_path, asset);