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"
19 #include "indexfile.h"
21 #include "preferences.h"
22 #include "videodevice.inc"
24 FileFFMPEG::FileFFMPEG(Asset *asset, File *file)
25 : FileBase(asset, file)
28 if(asset->format == FILE_UNKNOWN)
29 asset->format = FILE_FFMPEG;
32 FileFFMPEG::~FileFFMPEG()
38 FFMpegConfigNum::FFMpegConfigNum(BC_Window *window,
39 int x, int y, char *title_text, int *output)
40 : BC_TumbleTextBox(window, (int64_t)*output,
41 (int64_t)-1, (int64_t)25000000, 100, y, 100)
43 this->window = window;
44 this->x = x; this->y = y;
45 this->title_text = title_text;
46 this->output = output;
49 FFMpegConfigNum::~FFMpegConfigNum()
53 void FFMpegConfigNum::create_objects()
55 window->add_subwindow(title = new BC_Title(x, y, title_text));
56 BC_TumbleTextBox::create_objects();
59 int FFMpegConfigNum::handle_event()
61 *output = atol(get_text());
65 FFMpegAudioNum::FFMpegAudioNum(BC_Window *window,
66 int x, int y, char *title_text, int *output)
67 : FFMpegConfigNum(window, x, y, title_text, output)
71 int FFMpegAudioBitrate::handle_event()
73 int ret = FFMpegAudioNum::handle_event();
77 FFMpegVideoNum::FFMpegVideoNum(BC_Window *window,
78 int x, int y, char *title_text, int *output)
79 : FFMpegConfigNum(window, x, y, title_text, output)
83 int FFMpegVideoBitrate::handle_event()
85 int ret = FFMpegVideoNum::handle_event();
86 Asset *asset = window()->asset;
87 if( asset->ff_video_bitrate )
88 window()->quality->disable();
90 window()->quality->enable();
94 int FFMpegVideoQuality::handle_event()
96 int ret = FFMpegVideoNum::handle_event();
97 Asset *asset = window()->asset;
98 if( asset->ff_video_quality )
99 window()->bitrate->disable();
101 window()->bitrate->enable();
105 void FileFFMPEG::get_parameters(BC_WindowBase *parent_window,
106 Asset *asset, BC_WindowBase *&format_window,
107 int audio_options, int video_options)
110 FFMPEGConfigAudio *window = new FFMPEGConfigAudio(parent_window, asset);
111 format_window = window;
112 window->create_objects();
113 window->run_window();
116 else if(video_options) {
117 FFMPEGConfigVideo *window = new FFMPEGConfigVideo(parent_window, asset);
118 format_window = window;
119 window->create_objects();
120 window->run_window();
125 int FileFFMPEG::check_sig(Asset *asset)
127 char *ptr = strstr(asset->path, ".pcm");
129 ptr = strstr(asset->path, ".raw");
133 int ret = !ffmpeg.init_decoder(asset->path) &&
134 !ffmpeg.open_decoder() ? 1 : 0;
138 void FileFFMPEG::get_info(char *path, char *text, int len)
142 cp += sprintf(cp, _("file path: %s\n"), path);
145 if( stat(path, &st) < 0 ) {
146 cp += sprintf(cp, _(" err: %s\n"), strerror(errno));
150 cp += sprintf(cp, _(" %jd bytes\n"), st.st_size);
152 if( !ret ) ret = ffmpeg.init_decoder(path);
153 if( !ret ) ret = ffmpeg.open_decoder();
155 cp += sprintf(cp, _("info:\n"));
156 ffmpeg.info(cp, len-(cp-text));
159 sprintf(cp, _("== open failed\n"));
162 int FileFFMPEG::get_video_info(int track, int &pid, double &framerate,
163 int &width, int &height, char *title)
166 pid = ff->ff_video_pid(track);
167 framerate = ff->ff_frame_rate(track);
168 width = ff->ff_video_width(track);
169 height = ff->ff_video_height(track);
170 if( title ) *title = 0;
174 int FileFFMPEG::get_audio_for_video(int vstream, int astream, int64_t &channel_mask)
177 return ff->ff_audio_for_video(vstream, astream, channel_mask);
180 int FileFFMPEG::select_video_stream(Asset *asset, int vstream)
182 if( !ff || !asset->video_data ) return 1;
183 asset->width = ff->ff_video_width(vstream);
184 asset->height = ff->ff_video_height(vstream);
185 asset->video_length = ff->ff_video_frames(vstream);
186 asset->frame_rate = ff->ff_frame_rate(vstream);
190 int FileFFMPEG::select_audio_stream(Asset *asset, int astream)
192 if( !ff || !asset->audio_data ) return 1;
193 asset->sample_rate = ff->ff_sample_rate(astream);
194 asset->audio_length = ff->ff_audio_samples(astream);
198 int FileFFMPEG::open_file(int rd, int wr)
202 ff = new FFMPEG(this);
205 result = ff->init_decoder(asset->path);
206 if( !result ) result = ff->open_decoder();
208 int audio_channels = ff->ff_total_audio_channels();
209 if( audio_channels > 0 ) {
210 asset->audio_data = 1;
211 asset->channels = audio_channels;
212 asset->sample_rate = ff->ff_sample_rate(0);
213 asset->audio_length = ff->ff_audio_samples(0);
215 int video_layers = ff->ff_total_video_layers();
216 if( video_layers > 0 ) {
217 asset->video_data = 1;
218 if( !asset->layers ) asset->layers = video_layers;
219 asset->actual_width = ff->ff_video_width(0);
220 asset->actual_height = ff->ff_video_height(0);
221 if( !asset->width ) asset->width = asset->actual_width;
222 if( !asset->height ) asset->height = asset->actual_height;
223 if( !asset->video_length ) asset->video_length = ff->ff_video_frames(0);
224 if( !asset->frame_rate ) asset->frame_rate = ff->ff_frame_rate(0);
226 IndexState *index_state = asset->index_state;
227 index_state->read_markers(file->preferences->index_directory, asset->path);
231 result = ff->init_encoder(asset->path);
232 // must be in this order or dvdauthor will fail
233 if( !result && asset->video_data )
234 result = ff->open_encoder("video", asset->vcodec);
235 if( !result && asset->audio_data )
236 result = ff->open_encoder("audio", asset->acodec);
241 int FileFFMPEG::close_file()
249 int FileFFMPEG::write_samples(double **buffer, int64_t len)
251 if( !ff || len < 0 ) return -1;
253 int ret = ff->encode(stream, buffer, len);
257 int FileFFMPEG::write_frames(VFrame ***frames, int len)
260 int ret = 0, layer = 0;
261 for(int i = 0; i < 1; i++) {
262 for(int j = 0; j < len && !ret; j++) {
263 VFrame *frame = frames[i][j];
264 ret = ff->encode(layer, frame);
271 int FileFFMPEG::read_samples(double *buffer, int64_t len)
273 if( !ff || len < 0 ) return -1;
274 int ch = file->current_channel;
275 int64_t pos = file->current_sample;
276 int ret = ff->decode(ch, pos, buffer, len);
277 if( ret > 0 ) return 0;
278 memset(buffer,0,len*sizeof(*buffer));
282 int FileFFMPEG::read_frame(VFrame *frame)
285 int layer = file->current_layer;
286 int64_t pos = file->current_frame;
287 int ret = ff->decode(layer, pos, frame);
288 frame->set_status(ret);
289 if( ret >= 0 ) return 0;
290 frame->clear_frame();
295 int64_t FileFFMPEG::get_memory_usage()
300 int FileFFMPEG::colormodel_supported(int colormodel)
305 int FileFFMPEG::get_best_colormodel(Asset *asset, int driver)
308 case PLAYBACK_X11: return BC_RGB888;
309 case PLAYBACK_X11_GL: return BC_YUV888;
316 FFMPEGConfigAudio::FFMPEGConfigAudio(BC_WindowBase *parent_window, Asset *asset)
317 : BC_Window(_(PROGRAM_NAME ": Audio Preset"),
318 parent_window->get_abs_cursor_x(1),
319 parent_window->get_abs_cursor_y(1),
322 this->parent_window = parent_window;
330 FFMPEGConfigAudio::~FFMPEGConfigAudio()
332 lock_window("FFMPEGConfigAudio::~FFMPEGConfigAudio");
333 if(preset_popup) delete preset_popup;
334 presets.remove_all_objects();
338 void FFMPEGConfigAudio::create_objects()
341 lock_window("FFMPEGConfigAudio::create_objects");
344 char option_path[BCTEXTLEN];
345 FFMPEG::set_option_path(option_path, "audio");
346 fs.update(option_path);
347 int total_files = fs.total_files();
348 for(int i = 0; i < total_files; i++) {
349 const char *name = fs.get_entry(i)->get_name();
350 if( asset->fformat[0] != 0 ) {
351 const char *ext = strrchr(name,'.');
353 if( strcmp(asset->fformat, ++ext) ) continue;
355 presets.append(new BC_ListBoxItem(name));
358 if( asset->acodec[0] ) {
359 int k = presets.size();
360 while( --k >= 0 && strcmp(asset->acodec, presets[k]->get_text()) );
361 if( k < 0 ) asset->acodec[0] = 0;
364 if( !asset->acodec[0] && presets.size() > 0 )
365 strcpy(asset->acodec, presets[0]->get_text());
367 add_tool(new BC_Title(x, y, _("Preset:")));
369 preset_popup = new FFMPEGConfigAudioPopup(this, x, y);
370 preset_popup->create_objects();
373 bitrate = new FFMpegAudioBitrate(this, x, y, _("Bitrate:"), &asset->ff_audio_bitrate);
374 bitrate->create_objects();
375 bitrate->set_increment(1000);
377 y += bitrate->get_h() + 10;
378 add_subwindow(new BC_Title(x, y, _("Audio Options:")));
380 if( !asset->ff_audio_options[0] && asset->acodec[0] ) {
381 FFMPEG::set_option_path(option_path, "audio/%s", asset->acodec);
382 FFMPEG::load_options(option_path, asset->ff_audio_options,
383 sizeof(asset->ff_audio_options));
385 add_subwindow(audio_options = new FFAudioOptions(this, x, y, get_w()-x-20, 10,
386 sizeof(asset->ff_audio_options)-1, asset->ff_audio_options));
387 add_subwindow(new BC_OKButton(this));
389 bitrate->handle_event();
393 int FFMPEGConfigAudio::close_event()
400 FFAudioOptions::FFAudioOptions(FFMPEGConfigAudio *audio_popup,
401 int x, int y, int w, int rows, int size, char *text)
402 : BC_TextBox(x, y, w, rows, size, text)
404 this->audio_popup = audio_popup;
407 int FFAudioOptions::handle_event()
409 strcpy(audio_popup->asset->ff_audio_options, get_text());
414 FFMPEGConfigAudioPopup::FFMPEGConfigAudioPopup(FFMPEGConfigAudio *popup, int x, int y)
415 : BC_PopupTextBox(popup, &popup->presets, popup->asset->acodec, x, y, 300, 300)
420 int FFMPEGConfigAudioPopup::handle_event()
422 strcpy(popup->asset->acodec, get_text());
423 Asset *asset = popup->asset;
424 char option_path[BCTEXTLEN];
425 FFMPEG::set_option_path(option_path, "audio/%s", asset->acodec);
426 FFMPEG::load_options(option_path, asset->ff_audio_options,
427 sizeof(asset->ff_audio_options));
428 popup->audio_options->update(asset->ff_audio_options);
433 FFMPEGConfigAudioToggle::FFMPEGConfigAudioToggle(FFMPEGConfigAudio *popup,
434 char *title_text, int x, int y, int *output)
435 : BC_CheckBox(x, y, *output, title_text)
438 this->output = output;
440 int FFMPEGConfigAudioToggle::handle_event()
442 *output = get_value();
449 FFMPEGConfigVideo::FFMPEGConfigVideo(BC_WindowBase *parent_window, Asset *asset)
450 : BC_Window(_(PROGRAM_NAME ": Video Preset"),
451 parent_window->get_abs_cursor_x(1),
452 parent_window->get_abs_cursor_y(1),
455 this->parent_window = parent_window;
464 FFMPEGConfigVideo::~FFMPEGConfigVideo()
466 lock_window("FFMPEGConfigVideo::~FFMPEGConfigVideo");
467 if(preset_popup) delete preset_popup;
468 presets.remove_all_objects();
472 void FFMPEGConfigVideo::create_objects()
475 lock_window("FFMPEGConfigVideo::create_objects");
477 add_subwindow(new BC_Title(x, y, _("Compression:")));
481 char option_path[BCTEXTLEN];
482 FFMPEG::set_option_path(option_path, "video");
483 fs.update(option_path);
484 int total_files = fs.total_files();
485 for(int i = 0; i < total_files; i++) {
486 const char *name = fs.get_entry(i)->get_name();
487 if( asset->fformat[0] != 0 ) {
488 const char *ext = strrchr(name,'.');
490 if( strcmp(asset->fformat, ++ext) ) continue;
492 presets.append(new BC_ListBoxItem(name));
495 if( asset->vcodec[0] ) {
496 int k = presets.size();
497 while( --k >= 0 && strcmp(asset->vcodec, presets[k]->get_text()) );
498 if( k < 0 ) asset->vcodec[0] = 0;
501 if( !asset->vcodec[0] && presets.size() > 0 )
502 strcpy(asset->vcodec, presets[0]->get_text());
504 preset_popup = new FFMPEGConfigVideoPopup(this, x, y);
505 preset_popup->create_objects();
507 if( asset->ff_video_bitrate && asset->ff_video_quality ) {
508 asset->ff_video_bitrate = 0;
509 asset->ff_video_quality = 0;
513 bitrate = new FFMpegVideoBitrate(this, x, y, _("Bitrate:"), &asset->ff_video_bitrate);
514 bitrate->create_objects();
515 bitrate->set_increment(100000);
516 y += bitrate->get_h() + 5;
517 quality = new FFMpegVideoQuality(this, x, y, _("Quality:"), &asset->ff_video_quality);
518 quality->create_objects();
519 quality->set_increment(1);
520 quality->set_boundaries((int64_t)0, (int64_t)31);
522 y += quality->get_h() + 10;
523 add_subwindow(new BC_Title(x, y, _("Video Options:")));
525 if( !asset->ff_video_options[0] && asset->vcodec[0] ) {
526 FFMPEG::set_option_path(option_path, "video/%s", asset->vcodec);
527 FFMPEG::load_options(option_path, asset->ff_video_options,
528 sizeof(asset->ff_video_options));
530 add_subwindow(video_options = new FFVideoOptions(this, x, y, get_w()-x-20, 10,
531 sizeof(asset->ff_video_options)-1, asset->ff_video_options));
533 add_subwindow(new BC_OKButton(this));
535 if( asset->ff_video_bitrate )
537 if( asset->ff_video_quality )
542 int FFMPEGConfigVideo::close_event()
549 FFVideoOptions::FFVideoOptions(FFMPEGConfigVideo *video_popup,
550 int x, int y, int w, int rows, int size, char *text)
551 : BC_TextBox(x, y, w, rows, size, text)
553 this->video_popup = video_popup;
556 int FFVideoOptions::handle_event()
558 strcpy(video_popup->asset->ff_video_options, get_text());
563 FFMPEGConfigVideoPopup::FFMPEGConfigVideoPopup(FFMPEGConfigVideo *popup, int x, int y)
564 : BC_PopupTextBox(popup, &popup->presets, popup->asset->vcodec, x, y, 300, 300)
569 int FFMPEGConfigVideoPopup::handle_event()
571 strcpy(popup->asset->vcodec, get_text());
572 Asset *asset = popup->asset;
573 char option_path[BCTEXTLEN];
574 FFMPEG::set_option_path(option_path, "video/%s", asset->vcodec);
575 FFMPEG::load_options(option_path, asset->ff_video_options,
576 sizeof(asset->ff_video_options));
577 popup->video_options->update(asset->ff_video_options);
582 FFMPEGConfigVideoToggle::FFMPEGConfigVideoToggle(FFMPEGConfigVideo *popup,
583 char *title_text, int x, int y, int *output)
584 : BC_CheckBox(x, y, *output, title_text)
587 this->output = output;
589 int FFMPEGConfigVideoToggle::handle_event()
591 *output = get_value();
595 FFMPEGScanProgress::FFMPEGScanProgress(const char *title, int64_t length, int64_t *position, int *canceled)
598 strcpy(this->progress_title, title);
599 this->length = length;
600 this->position = position;
601 this->canceled = canceled;
607 FFMPEGScanProgress::~FFMPEGScanProgress()
614 void FFMPEGScanProgress::run()
616 BC_ProgressBox *progress = new BC_ProgressBox(-1, -1, progress_title, length);
619 struct timeval start_time, now, then;
620 gettimeofday(&start_time, 0);
624 if( progress->update(*position, 1) ) {
625 *canceled = done = 1;
628 gettimeofday(&now, 0);
629 if(now.tv_sec - then.tv_sec >= 1) {
630 int64_t elapsed = now.tv_sec - start_time.tv_sec;
631 int64_t byte_rate = *position / elapsed;
632 int64_t eta = !byte_rate ? 0 : (length - *position) / byte_rate;
633 char string[BCTEXTLEN];
634 sprintf(string, "%s\nETA: %jdm%jds",
635 progress_title, eta / 60, eta % 60);
636 progress->update_title(string, 1);
642 progress->stop_progress();
646 int FileFFMPEG::get_index(char *index_path)
649 if( !file->preferences->ffmpeg_marker_indexes ) return 1;
651 IndexState *index_state = asset->index_state;
652 if( index_state->index_status != INDEX_NOTTESTED ) return 0;
653 index_state->reset_index();
654 index_state->reset_markers();
655 index_state->index_status = INDEX_BUILDING;
657 for( int aidx=0; aidx<ff->ffaudio.size(); ++aidx ) {
658 FFAudioStream *aud = ff->ffaudio[aidx];
659 index_state->add_audio_stream(aud->channels, aud->length);
662 char progress_title[BCTEXTLEN];
663 sprintf(progress_title, _("Creating %s\n"), index_path);
665 int64_t file_bytes = fs.get_size(ff->fmt_ctx->filename);
666 int64_t scan_position = 0;
668 FFMPEGScanProgress scan_progress(progress_title, file_bytes, &scan_position, &canceled);
670 index_state->index_bytes = file_bytes;
671 index_state->init_scan(file->preferences->index_size);
672 if( ff->scan(index_state, &scan_position, &canceled) || canceled ) {
673 index_state->reset_index();
674 index_state->reset_markers();
677 index_state->marker_status = MARKERS_READY;
678 return index_state->create_index(index_path, asset);