move aspect ratio fixex, better get_info, fixes for ffmpeg asset detection
[goodguy/history.git] / cinelerra-5.0 / 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 "ffmpeg.h"
14 #include "filebase.h"
15 #include "file.h"
16 #include "fileffmpeg.h"
17 #include "filesystem.h"
18 #include "mutex.h"
19 #include "videodevice.inc"
20
21 FileFFMPEG::FileFFMPEG(Asset *asset, File *file)
22   : FileBase(asset, file)
23 {
24         ff = 0;
25         if(asset->format == FILE_UNKNOWN)
26                 asset->format = FILE_FFMPEG;
27 }
28
29 FileFFMPEG::~FileFFMPEG()
30 {
31         delete ff;
32 }
33
34
35 FFMpegConfigNum::FFMpegConfigNum(BC_Window *window,
36                 int x, int y, char *title_text, int *output)
37  : BC_TumbleTextBox(window, (int64_t)*output,
38         (int64_t)-1, (int64_t)25000000, 100, y, 100)
39 {
40         this->window = window;
41         this->x = x;  this->y = y;
42         this->title_text = title_text;
43         this->output = output;
44 }
45
46 FFMpegConfigNum::~FFMpegConfigNum()
47 {
48 }
49
50 void FFMpegConfigNum::create_objects()
51 {
52         window->add_subwindow(title = new BC_Title(x, y, title_text));
53         BC_TumbleTextBox::create_objects();
54 }
55
56 int FFMpegConfigNum::handle_event()
57 {
58         *output = atol(get_text());
59         return 1;
60 }
61
62 FFMpegAudioNum::FFMpegAudioNum(BC_Window *window,
63                 int x, int y, char *title_text, int *output)
64  : FFMpegConfigNum(window, x, y, title_text, output)
65 {
66 }
67
68 int FFMpegAudioBitrate::handle_event()
69 {
70         int ret = FFMpegAudioNum::handle_event();
71         return ret;
72 }
73
74 FFMpegVideoNum::FFMpegVideoNum(BC_Window *window,
75                 int x, int y, char *title_text, int *output)
76  : FFMpegConfigNum(window, x, y, title_text, output)
77 {
78 }
79
80 int FFMpegVideoBitrate::handle_event()
81 {
82         int ret = FFMpegVideoNum::handle_event();
83         Asset *asset = window()->asset;
84         if( asset->ff_video_bitrate )
85                 window()->quality->disable();
86         else
87                 window()->quality->enable();
88         return ret;
89 }
90
91 int FFMpegVideoQuality::handle_event()
92 {
93         int ret = FFMpegVideoNum::handle_event();
94         Asset *asset = window()->asset;
95         if( asset->ff_video_quality )
96                 window()->bitrate->disable();
97         else
98                 window()->bitrate->enable();
99         return ret;
100 }
101
102 void FileFFMPEG::get_parameters(BC_WindowBase *parent_window,
103                 Asset *asset, BC_WindowBase *&format_window,
104                 int audio_options, int video_options)
105 {
106         if(audio_options) {
107                 FFMPEGConfigAudio *window = new FFMPEGConfigAudio(parent_window, asset);
108                 format_window = window;
109                 window->create_objects();
110                 window->run_window();
111                 delete window;
112         }
113         else if(video_options) {
114                 FFMPEGConfigVideo *window = new FFMPEGConfigVideo(parent_window, asset);
115                 format_window = window;
116                 window->create_objects();
117                 window->run_window();
118                 delete window;
119         }
120 }
121
122 int FileFFMPEG::check_sig(Asset *asset)
123 {
124         char *ptr = strstr(asset->path, ".pcm");
125         if( ptr ) return 0;
126         ptr = strstr(asset->path, ".raw");
127         if( ptr ) return 0;
128
129         FFMPEG ffmpeg(0);
130         int ret = !ffmpeg.init_decoder(asset->path) &&
131                 !ffmpeg.open_decoder() ? 1 : 0;
132         return ret;
133 }
134
135 void FileFFMPEG::get_info(char *path, char *text, int len)
136 {
137         char *cp = text;
138         FFMPEG ffmpeg(0);
139         cp += sprintf(cp, _("file path: %s\n"), path);
140         struct stat st;
141         int ret = 0;
142         if( stat(path, &st) < 0 ) {
143                 cp += sprintf(cp, _(" err: %s\n"), strerror(errno));
144                 ret = 1;
145         }
146         else {
147                 cp += sprintf(cp, _("  %jd bytes\n"), st.st_size);
148         }
149         if( !ret ) ret = ffmpeg.init_decoder(path);
150         if( !ret ) ret = ffmpeg.open_decoder();
151         if( !ret ) {
152                 cp += sprintf(cp, _("info:\n"));
153                 ffmpeg.info(cp, len-(cp-text));
154         }
155         else
156                 sprintf(cp, _("== open failed\n"));
157 }
158
159 int FileFFMPEG::get_video_info(int track, int &pid, double &framerate,
160                 int &width, int &height, char *title)
161 {
162         if( !ff ) return -1;
163         pid = ff->ff_video_pid(track);
164         framerate = ff->ff_frame_rate(track);
165         width = ff->ff_video_width(track);
166         height = ff->ff_video_height(track);
167         if( title ) *title = 0;
168         return 0;
169 }
170
171 int FileFFMPEG::get_audio_for_video(int vstream, int astream, int64_t &channel_mask)
172 {
173         if( !ff ) return 1;
174         return ff->ff_audio_for_video(vstream, astream, channel_mask);
175 }
176
177 int FileFFMPEG::select_video_stream(Asset *asset, int vstream)
178 {
179         if( !ff || !asset->video_data ) return 1;
180         asset->width = ff->ff_video_width(vstream);
181         asset->height = ff->ff_video_height(vstream);
182         asset->video_length = ff->ff_video_frames(vstream);
183         asset->frame_rate = ff->ff_frame_rate(vstream);
184         return 0;
185 }
186
187 int FileFFMPEG::select_audio_stream(Asset *asset, int astream)
188 {
189         if( !ff || !asset->audio_data ) return 1;
190         asset->sample_rate = ff->ff_sample_rate(astream);
191         asset->audio_length = ff->ff_audio_samples(astream);
192         return 0;
193 }
194
195 int FileFFMPEG::open_file(int rd, int wr)
196 {
197         int result = 0;
198         if( ff ) return 1;
199         ff = new FFMPEG(this);
200
201         if( rd ) {
202                 result = ff->init_decoder(asset->path);
203                 if( !result ) result = ff->open_decoder();
204                 if( !result ) {
205                         int audio_channels = ff->ff_total_audio_channels();
206                         if( audio_channels > 0 ) {
207                                 asset->audio_data = 1;
208                                 asset->channels = audio_channels;
209                                 asset->sample_rate = ff->ff_sample_rate(0);
210                                 asset->audio_length = ff->ff_audio_samples(0);
211                         }
212                         int video_layers = ff->ff_total_video_layers();
213                         if( video_layers > 0 ) {
214                                 asset->video_data = 1;
215                                 if( !asset->layers ) asset->layers = video_layers;
216                                 asset->actual_width = ff->ff_video_width(0);
217                                 asset->actual_height = ff->ff_video_height(0);
218                                 if( !asset->width ) asset->width = asset->actual_width;
219                                 if( !asset->height ) asset->height = asset->actual_height;
220                                 if( !asset->video_length ) asset->video_length = ff->ff_video_frames(0);
221                                 if( !asset->frame_rate ) asset->frame_rate = ff->ff_frame_rate(0);
222                         }
223                 }
224         }
225         else if( wr ) {
226                 result = ff->init_encoder(asset->path);
227                 // must be in this order or dvdauthor will fail
228                 if( !result && asset->video_data )
229                         result = ff->open_encoder("video", asset->vcodec);
230                 if( !result && asset->audio_data )
231                         result = ff->open_encoder("audio", asset->acodec);
232         }
233         return result;
234 }
235
236 int FileFFMPEG::close_file()
237 {
238         delete ff;
239         ff = 0;
240         return 0;
241 }
242
243
244 int FileFFMPEG::set_video_position(int64_t pos)
245 {
246         if( !ff || pos < 0 || pos >= asset->video_length )
247                 return 1;
248         return 0;
249 }
250
251
252 int FileFFMPEG::set_audio_position(int64_t pos)
253 {
254         if( !ff || pos < 0 || pos >= asset->audio_length )
255                 return 1;
256         return 0;
257 }
258
259
260 int FileFFMPEG::write_samples(double **buffer, int64_t len)
261 {
262         if( !ff || len < 0 ) return -1;
263         int stream = 0;
264         int ret = ff->encode(stream, buffer, len);
265         return ret;
266 }
267
268 int FileFFMPEG::write_frames(VFrame ***frames, int len)
269 {
270         if( !ff ) return -1;
271         int ret = 0, layer = 0;
272         for(int i = 0; i < 1; i++) {
273                 for(int j = 0; j < len && !ret; j++) {
274                         VFrame *frame = frames[i][j];
275                         ret = ff->encode(layer, frame);
276                 }
277         }
278         return ret;
279 }
280
281
282 int FileFFMPEG::read_samples(double *buffer, int64_t len)
283 {
284         if( !ff || len < 0 ) return -1;
285         int ch = file->current_channel;
286         int64_t pos = file->current_sample;
287         ff->decode(ch, pos, buffer, len);
288         return 0;
289 }
290
291 int FileFFMPEG::read_frame(VFrame *frame)
292 {
293         if( !ff ) return -1;
294         int layer = file->current_layer;
295         int64_t pos = file->current_frame;
296         ff->decode(layer, pos, frame);
297         return 0;
298 }
299
300
301 int64_t FileFFMPEG::get_memory_usage()
302 {
303         return 0;
304 }
305
306 int FileFFMPEG::colormodel_supported(int colormodel)
307 {
308         return colormodel;
309 }
310
311 int FileFFMPEG::get_best_colormodel(Asset *asset, int driver)
312 {
313         switch(driver) {
314         case PLAYBACK_X11:      return BC_RGB888;
315         case PLAYBACK_X11_GL:   return BC_YUV888;
316         }
317         return BC_YUV420P;
318 }
319
320 //======
321
322 FFMPEGConfigAudio::FFMPEGConfigAudio(BC_WindowBase *parent_window, Asset *asset)
323  : BC_Window(_(PROGRAM_NAME ": Audio Preset"),
324         parent_window->get_abs_cursor_x(1),
325         parent_window->get_abs_cursor_y(1),
326         420, 420)
327 {
328         this->parent_window = parent_window;
329         this->asset = asset;
330         preset_popup = 0;
331
332         bitrate = 0;
333         audio_options = 0;
334 }
335
336 FFMPEGConfigAudio::~FFMPEGConfigAudio()
337 {
338         lock_window("FFMPEGConfigAudio::~FFMPEGConfigAudio");
339         if(preset_popup) delete preset_popup;
340         presets.remove_all_objects();
341         unlock_window();
342 }
343
344 void FFMPEGConfigAudio::create_objects()
345 {
346         int x = 10, y = 10;
347         lock_window("FFMPEGConfigAudio::create_objects");
348
349         FileSystem fs;
350         char option_path[BCTEXTLEN];
351         FFMPEG::set_option_path(option_path, "audio");
352         fs.update(option_path);
353         int total_files = fs.total_files();
354         for(int i = 0; i < total_files; i++) {
355                 const char *name = fs.get_entry(i)->get_name();
356                 if( asset->fformat[0] != 0 ) {
357                         const char *ext = strrchr(name,'.');
358                         if( !ext ) continue;
359                         if( strcmp(asset->fformat, ++ext) ) continue;
360                 }
361                 presets.append(new BC_ListBoxItem(name));
362         }
363
364         if( asset->acodec[0] ) {
365                 int k = presets.size();
366                 while( --k >= 0 && strcmp(asset->acodec, presets[k]->get_text()) );
367                 if( k < 0 ) asset->acodec[0] = 0;
368         }
369
370         if( !asset->acodec[0] && presets.size() > 0 )
371                 strcpy(asset->acodec, presets[0]->get_text());
372
373         add_tool(new BC_Title(x, y, _("Preset:")));
374         y += 25;
375         preset_popup = new FFMPEGConfigAudioPopup(this, x, y);
376         preset_popup->create_objects();
377
378         y += 50;
379         bitrate = new FFMpegAudioBitrate(this, x, y, _("Bitrate:"), &asset->ff_audio_bitrate);
380         bitrate->create_objects();
381         bitrate->set_increment(1000);
382
383         y += bitrate->get_h() + 10;
384         add_subwindow(new BC_Title(x, y, _("Audio Options:")));
385         y += 25;
386         if( !asset->ff_audio_options[0] && asset->acodec[0] ) {
387                 FFMPEG::set_option_path(option_path, "audio/%s", asset->acodec);
388                 FFMPEG::load_options(option_path, asset->ff_audio_options,
389                          sizeof(asset->ff_audio_options));
390         }
391         add_subwindow(audio_options = new FFAudioOptions(this, x, y, get_w()-x-20, 10,
392                  sizeof(asset->ff_audio_options)-1, asset->ff_audio_options));
393         add_subwindow(new BC_OKButton(this));
394         show_window(1);
395         bitrate->handle_event();
396         unlock_window();
397 }
398
399 int FFMPEGConfigAudio::close_event()
400 {
401         set_done(0);
402         return 1;
403 }
404
405
406 FFAudioOptions::FFAudioOptions(FFMPEGConfigAudio *audio_popup,
407         int x, int y, int w, int rows, int size, char *text)
408  : BC_TextBox(x, y, w, rows, size, text)
409 {
410         this->audio_popup = audio_popup;
411 }
412
413 int FFAudioOptions::handle_event()
414 {
415         strcpy(audio_popup->asset->ff_audio_options, get_text());
416         return 1;
417 }
418
419
420 FFMPEGConfigAudioPopup::FFMPEGConfigAudioPopup(FFMPEGConfigAudio *popup, int x, int y)
421  : BC_PopupTextBox(popup, &popup->presets, popup->asset->acodec, x, y, 300, 300)
422 {
423         this->popup = popup;
424 }
425
426 int FFMPEGConfigAudioPopup::handle_event()
427 {
428         strcpy(popup->asset->acodec, get_text());
429         Asset *asset = popup->asset;
430         char option_path[BCTEXTLEN];
431         FFMPEG::set_option_path(option_path, "audio/%s", asset->acodec);
432         FFMPEG::load_options(option_path, asset->ff_audio_options,
433                          sizeof(asset->ff_audio_options));
434         popup->audio_options->update(asset->ff_audio_options);
435         return 1;
436 }
437
438
439 FFMPEGConfigAudioToggle::FFMPEGConfigAudioToggle(FFMPEGConfigAudio *popup,
440         char *title_text, int x, int y, int *output)
441  : BC_CheckBox(x, y, *output, title_text)
442 {
443         this->popup = popup;
444         this->output = output;
445 }
446 int FFMPEGConfigAudioToggle::handle_event()
447 {
448         *output = get_value();
449         return 1;
450 }
451
452
453 //======
454
455 FFMPEGConfigVideo::FFMPEGConfigVideo(BC_WindowBase *parent_window, Asset *asset)
456  : BC_Window(_(PROGRAM_NAME ": Video Preset"),
457         parent_window->get_abs_cursor_x(1),
458         parent_window->get_abs_cursor_y(1),
459         420, 420)
460 {
461         this->parent_window = parent_window;
462         this->asset = asset;
463         preset_popup = 0;
464
465         bitrate = 0;
466         quality = 0;
467         video_options = 0;
468 }
469
470 FFMPEGConfigVideo::~FFMPEGConfigVideo()
471 {
472         lock_window("FFMPEGConfigVideo::~FFMPEGConfigVideo");
473         if(preset_popup) delete preset_popup;
474         presets.remove_all_objects();
475         unlock_window();
476 }
477
478 void FFMPEGConfigVideo::create_objects()
479 {
480         int x = 10, y = 10;
481         lock_window("FFMPEGConfigVideo::create_objects");
482
483         add_subwindow(new BC_Title(x, y, _("Compression:")));
484         y += 25;
485
486         FileSystem fs;
487         char option_path[BCTEXTLEN];
488         FFMPEG::set_option_path(option_path, "video");
489         fs.update(option_path);
490         int total_files = fs.total_files();
491         for(int i = 0; i < total_files; i++) {
492                 const char *name = fs.get_entry(i)->get_name();
493                 if( asset->fformat[0] != 0 ) {
494                         const char *ext = strrchr(name,'.');
495                         if( !ext ) continue;
496                         if( strcmp(asset->fformat, ++ext) ) continue;
497                 }
498                 presets.append(new BC_ListBoxItem(name));
499         }
500
501         if( asset->vcodec[0] ) {
502                 int k = presets.size();
503                 while( --k >= 0 && strcmp(asset->vcodec, presets[k]->get_text()) );
504                 if( k < 0 ) asset->vcodec[0] = 0;
505         }
506
507         if( !asset->vcodec[0] && presets.size() > 0 )
508                 strcpy(asset->vcodec, presets[0]->get_text());
509
510         preset_popup = new FFMPEGConfigVideoPopup(this, x, y);
511         preset_popup->create_objects();
512
513         if( asset->ff_video_bitrate && asset->ff_video_quality ) {
514                 asset->ff_video_bitrate = 0;
515                 asset->ff_video_quality = 0;
516         }
517
518         y += 50;
519         bitrate = new FFMpegVideoBitrate(this, x, y, _("Bitrate:"), &asset->ff_video_bitrate);
520         bitrate->create_objects();
521         bitrate->set_increment(100000);
522         y += bitrate->get_h() + 5;
523         quality = new FFMpegVideoQuality(this, x, y, _("Quality:"), &asset->ff_video_quality);
524         quality->create_objects();
525         quality->set_increment(1);
526         quality->set_boundaries((int64_t)0, (int64_t)31);
527
528         y += quality->get_h() + 10;
529         add_subwindow(new BC_Title(x, y, _("Video Options:")));
530         y += 25;
531         if( !asset->ff_video_options[0] && asset->vcodec[0] ) {
532                 FFMPEG::set_option_path(option_path, "video/%s", asset->vcodec);
533                 FFMPEG::load_options(option_path, asset->ff_video_options,
534                          sizeof(asset->ff_video_options));
535         }
536         add_subwindow(video_options = new FFVideoOptions(this, x, y, get_w()-x-20, 10,
537                  sizeof(asset->ff_video_options)-1, asset->ff_video_options));
538
539         add_subwindow(new BC_OKButton(this));
540         show_window(1);
541         if( asset->ff_video_bitrate )
542                 quality->disable();
543         if( asset->ff_video_quality )
544                 bitrate->disable();
545         unlock_window();
546 }
547
548 int FFMPEGConfigVideo::close_event()
549 {
550         set_done(0);
551         return 1;
552 }
553
554
555 FFVideoOptions::FFVideoOptions(FFMPEGConfigVideo *video_popup,
556         int x, int y, int w, int rows, int size, char *text)
557  : BC_TextBox(x, y, w, rows, size, text)
558 {
559         this->video_popup = video_popup;
560 }
561
562 int FFVideoOptions::handle_event()
563 {
564         strcpy(video_popup->asset->ff_video_options, get_text());
565         return 1;
566 }
567
568
569 FFMPEGConfigVideoPopup::FFMPEGConfigVideoPopup(FFMPEGConfigVideo *popup, int x, int y)
570  : BC_PopupTextBox(popup, &popup->presets, popup->asset->vcodec, x, y, 300, 300)
571 {
572         this->popup = popup;
573 }
574
575 int FFMPEGConfigVideoPopup::handle_event()
576 {
577         strcpy(popup->asset->vcodec, get_text());
578         Asset *asset = popup->asset;
579         char option_path[BCTEXTLEN];
580         FFMPEG::set_option_path(option_path, "video/%s", asset->vcodec);
581         FFMPEG::load_options(option_path, asset->ff_video_options,
582                          sizeof(asset->ff_video_options));
583         popup->video_options->update(asset->ff_video_options);
584         return 1;
585 }
586
587
588 FFMPEGConfigVideoToggle::FFMPEGConfigVideoToggle(FFMPEGConfigVideo *popup,
589         char *title_text, int x, int y, int *output)
590  : BC_CheckBox(x, y, *output, title_text)
591 {
592         this->popup = popup;
593         this->output = output;
594 }
595 int FFMPEGConfigVideoToggle::handle_event()
596 {
597         *output = get_value();
598         return 1;
599 }
600
601