Credit Andrew - fix vorbis audio which was scratchy and ensure aging plugin does...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / fileffmpeg.h
1 /*
2  * CINELERRA
3  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
4  * Copyright (C) 2010 Monty Montgomery
5  * Copyright (C) 2012-2014 Paolo Rampino
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published
9  * by the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public
18  * License along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20  * USA
21  */
22
23 #ifndef __FILEFFMPEG_H__
24 #define __FILEFFMPEG_H__
25
26 #include "asset.inc"
27 #include "bcdialog.h"
28 #include "bcwindowbase.inc"
29 #include "bitspopup.inc"
30 #include "edl.inc"
31 #include "ffmpeg.h"
32 #include "filebase.h"
33 #include "fileffmpeg.inc"
34 #include "indexfile.inc"
35 #include "mainprogress.inc"
36 #include "mutex.h"
37 #include "thread.h"
38 #include "vframe.inc"
39
40 #include <stdio.h>
41 #include <stdint.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <string.h>
45
46
47 class FileFFMPEG : public FileBase
48 {
49 public:
50         FFMPEG *ff;
51
52         FileFFMPEG(Asset *asset, File *file);
53         ~FileFFMPEG();
54         static void ff_lock(const char *cp=0);
55         static void ff_unlock();
56
57         static void set_options(char *cp, int len, const char *bp);
58         static void get_parameters(BC_WindowBase *parent_window,
59                 Asset *asset, BC_WindowBase *&format_window,
60                 int audio_options,int video_options, EDL *edl);
61         static int check_sig(Asset *asset);
62         int get_best_colormodel(int driver, int vstream);
63         int get_video_info(int track, int &pid, double &framerate,
64                 int &width, int &height, char *title=0);
65         int get_audio_for_video(int vstream, int astream, int64_t &channel_mask);
66         static void get_info(char *path,char *text,int len);
67         int open_file(int rd,int wr);
68         int get_index(IndexFile *index_file, MainProgressBar *progress_bar);
69         int close_file(void);
70         int write_samples(double **buffer,int64_t len);
71         int write_frames(VFrame ***frames,int len);
72         int read_samples(double *buffer,int64_t len);
73         int read_frame(VFrame *frame);
74         int can_scale_input() { return 1; }
75         int64_t get_memory_usage(void);
76         int colormodel_supported(int colormodel);
77         static int get_best_colormodel(Asset *asset, int driver);
78         int select_video_stream(Asset *asset, int vstream);
79         int select_audio_stream(Asset *asset, int astream);
80 };
81
82 class FFMpegConfigNum : public BC_TumbleTextBox
83 {
84 public:
85         FFMpegConfigNum(BC_Window *window, int x, int y,
86                 char *title_text, int *output);
87         ~FFMpegConfigNum();
88
89         void create_objects();
90         int update_param(const char *param, const char *opts);
91         int handle_event();
92         int *output;
93         BC_Window *window;
94         BC_Title *title;
95         char *title_text;
96         int x, y;
97 };
98
99 class FFMpegAudioNum : public FFMpegConfigNum
100 {
101 public:
102         FFMpegAudioNum(BC_Window *window, int x, int y, char *title_text, int *output);
103         ~FFMpegAudioNum() {}
104
105         FFMPEGConfigAudio *window() { return (FFMPEGConfigAudio *)FFMpegConfigNum::window; }
106 };
107
108 class FFMpegAudioBitrate : public FFMpegAudioNum
109 {
110 public:
111         FFMpegAudioBitrate(BC_Window *window, int x, int y, char *title_text, int *output)
112           : FFMpegAudioNum(window, x, y, title_text, output) {}
113         int handle_event();
114 };
115
116 class FFMpegAudioQuality : public FFMpegAudioNum
117 {
118 public:
119         FFMpegAudioQuality(BC_Window *window, int x, int y, char *title_text, int *output)
120           : FFMpegAudioNum(window, x, y, title_text, output) {}
121         int handle_event();
122 };
123
124 class FFMpegVideoNum : public FFMpegConfigNum
125 {
126 public:
127         FFMpegVideoNum(BC_Window *window, int x, int y, char *title_text, int *output);
128         ~FFMpegVideoNum() {}
129
130         FFMPEGConfigVideo *window() { return (FFMPEGConfigVideo *)FFMpegConfigNum::window; }
131 };
132
133 class FFMpegVideoBitrate : public FFMpegVideoNum
134 {
135 public:
136         FFMpegVideoBitrate(BC_Window *window, int x, int y, char *title_text, int *output)
137           : FFMpegVideoNum(window, x, y, title_text, output) {}
138         int handle_event();
139 };
140
141 class FFMpegVideoQuality : public FFMpegVideoNum
142 {
143 public:
144         FFMpegVideoQuality(BC_Window *window, int x, int y, char *title_text, int *output)
145           : FFMpegVideoNum(window, x, y, title_text, output) {}
146         int handle_event();
147 };
148
149 class FFMpegPixFmtItems : public ArrayList<BC_ListBoxItem*>
150 {
151 public:
152         FFMpegPixFmtItems() {}
153         ~FFMpegPixFmtItems() { remove_all_objects(); }
154 };
155
156 class FFMpegPixelFormat : public BC_PopupTextBox
157 {
158 public:
159         FFMpegPixelFormat(FFMPEGConfigVideo *vid_config, int x, int y, int w, int list_h);
160
161         FFMPEGConfigVideo *vid_config;
162         FFMpegPixFmtItems pixfmts;
163
164         int handle_event();
165         void update_formats();
166 };
167
168 class FFMpegSampleFormat : public BC_PopupTextBox
169 {
170 public:
171         FFMpegSampleFormat(FFMPEGConfigAudio *aud_config, int x, int y, int w, int list_h);
172
173         FFMPEGConfigAudio *aud_config;
174         ArrayList<BC_ListBoxItem*> samplefmts;
175
176         int handle_event();
177         void update_formats();
178 };
179
180 class FFMPEGConfigWindow : public BC_Window
181 {
182 public:
183         FFMPEGConfigWindow(const char *title, BC_WindowBase *parent_window,
184                 int x, int y, int w, int h, Asset *asset, EDL *edl);
185         ~FFMPEGConfigWindow();
186         virtual void read_options() = 0;
187         virtual void save_options() = 0;
188         void start(AVCodecContext *avctx);
189         void start(AVFormatContext *fmt_ctx);
190
191         BC_WindowBase *parent_window;
192         FFOptionsDialog *ff_options_dialog;
193         Asset *asset;
194         EDL *edl;
195         AVCodecContext *avctx;
196         AVFormatContext *fmt_ctx;
197         char *format_name;
198         char *codec_name;
199 };
200
201 class FFMPEGConfigAudio : public FFMPEGConfigWindow
202 {
203 public:
204         FFMPEGConfigAudio(BC_WindowBase *parent_window,
205                 int x, int y, Asset *asset, EDL *edl);
206         ~FFMPEGConfigAudio();
207
208         void create_objects();
209         int close_event();
210         void load_options();
211         void read_options();
212         void save_options();
213
214         FFMpegSampleFormat *sample_format;
215         ArrayList<BC_ListBoxItem*> presets;
216         FFMPEGConfigAudioPopup *preset_popup;
217         FFMpegAudioBitrate *bitrate;
218         FFMpegAudioQuality *quality;
219         FFAudioOptions *audio_options;
220         BC_WindowBase *parent_window;
221         FFOptionsViewAudio *view_audio;
222         FFOptionsViewFormat *view_format;
223 };
224
225 class FFAudioOptions : public BC_ScrollTextBox
226 {
227 public:
228         FFAudioOptions(FFMPEGConfigAudio *audio_popup,
229                 int x, int y, int w, int rows, int size, char *text);
230
231         FFMPEGConfigAudio *audio_popup;
232 };
233
234
235 class FFMPEGConfigAudioPopup : public BC_PopupTextBox
236 {
237 public:
238         FFMPEGConfigAudioPopup(FFMPEGConfigAudio *popup, int x, int y);
239         int handle_event();
240         FFMPEGConfigAudio *popup;
241 };
242
243 class FFMPEGConfigVideo : public FFMPEGConfigWindow
244 {
245 public:
246         FFMPEGConfigVideo(BC_WindowBase *parent_window,
247                 int x, int y, Asset *asset, EDL *edl);
248         ~FFMPEGConfigVideo();
249         void read_options();
250         void save_options();
251
252         void create_objects();
253         int close_event();
254         void load_options();
255
256         FFMpegPixelFormat *pixel_format;
257         ArrayList<BC_ListBoxItem*> presets;
258         FFMPEGConfigVideoPopup *preset_popup;
259         BC_WindowBase *parent_window;
260         FFMpegVideoBitrate *bitrate;
261         FFMpegVideoQuality *quality;
262         FFVideoOptions *video_options;
263         FFOptionsViewVideo *view_video;
264         FFOptionsViewFormat *view_format;
265 };
266
267 class FFVideoOptions : public BC_ScrollTextBox
268 {
269 public:
270         FFVideoOptions(FFMPEGConfigVideo *video_popup,
271                 int x, int y, int w, int rows, int size, char *text);
272
273         FFMPEGConfigVideo *video_popup;
274 };
275
276 class FFMPEGConfigVideoPopup : public BC_PopupTextBox
277 {
278 public:
279         FFMPEGConfigVideoPopup(FFMPEGConfigVideo *popup, int x, int y);
280         int handle_event();
281         FFMPEGConfigVideo *popup;
282 };
283
284 class FFMPEGConfigFormat : public FFMPEGConfigWindow
285 {
286 public:
287         FFMPEGConfigFormat(FFOptionsFormatViewDialog *view_dialog,
288                 int x, int y, Asset *asset, EDL *edl);
289         ~FFMPEGConfigFormat();
290         void read_options();
291         void save_options();
292         void save_changes();
293
294         void create_objects();
295         int close_event();
296         void load_options();
297
298         FFOptionsFormatViewDialog *view_dialog;
299         FFFormatOptions *format_options;
300 };
301
302 class FFFormatOptions : public BC_ScrollTextBox
303 {
304 public: 
305         FFFormatOptions(FFMPEGConfigFormat *format_popup,
306                 int x, int y, int w, int rows, int size, char *text);
307
308         FFMPEGConfigFormat *format_popup;
309 };
310
311
312 class FFMPEGScanProgress : public Thread
313 {
314 public:
315         IndexFile *index_file;
316         MainProgressBar *progress_bar;
317         char progress_title[BCTEXTLEN];
318         int64_t length, *position;
319         int done, *canceled;
320
321         FFMPEGScanProgress(IndexFile *index_file, MainProgressBar *progress_bar,
322                 const char *title, int64_t length, int64_t *position, int *canceled);
323         ~FFMPEGScanProgress();
324         void run();
325 };
326
327
328 class FFOptions_OptName : public BC_ListBoxItem {
329 public:
330         FFOptions_Opt *opt;
331
332         FFOptions_OptName(FFOptions_Opt *opt, const char *nm);
333         ~FFOptions_OptName();
334
335 };
336
337 class FFOptions_OptValue : public BC_ListBoxItem {
338 public:
339         FFOptions_Opt *opt;
340
341         void update();
342         void update(const char *v);
343         FFOptions_OptValue(FFOptions_Opt *opt);
344 };
345
346 class FFOptions_Opt {
347 public:
348         FFOptions *options;
349         const AVOption *opt;
350         FFOptions_OptName *item_name;
351         FFOptions_OptValue *item_value;
352
353         char *get(char *vp, int sz=-1);
354         void set(const char *val);
355         int types(char *rp);
356         int scalar(double d, char *rp);
357         int ranges(char *rp);
358         int units(ArrayList<const char *> &opts);
359         const char *unit_name(int id);
360         int units(char *rp);
361         const char *tip();
362
363         FFOptions_Opt(FFOptions *options, const AVOption *opt, const char *nm);
364         ~FFOptions_Opt();
365 };
366
367 class FFOptions : public ArrayList<FFOptions_Opt *>
368 {
369 public:
370         FFOptions();
371         ~FFOptions();
372         FFOptionsWindow *win;
373         AVCodecContext *avctx;
374         const void *obj;
375
376         void initialize(FFOptionsWindow *win, int kind);
377         static int cmpr(const void *a, const void *b);
378         int update();
379         void dump(FILE *fp);
380 };
381
382 class FFOptions_OptPanel : public BC_ListBox {
383 public:
384         FFOptions_OptPanel(FFOptionsWindow *fwin, int x, int y, int w, int h);
385         ~FFOptions_OptPanel();
386         void create_objects();
387         int cursor_leave_event();
388
389         FFOptionsWindow *fwin;
390         ArrayList<BC_ListBoxItem*> items[2];
391         ArrayList<BC_ListBoxItem*> &opts;
392         ArrayList<BC_ListBoxItem*> &vals;
393         char tip_text[BCTEXTLEN];
394
395         int selection_changed();
396         int update();
397         void show_tip(const char *tip);
398 };
399
400 class FFOptionsKindItem : public BC_MenuItem
401 {
402 public:
403         FFOptionsKind *kind;
404         int idx;
405         int handle_event();
406         void show_label();
407
408         FFOptionsKindItem(FFOptionsKind *kind, const char *name, int idx);
409         ~FFOptionsKindItem();
410 };
411
412 class FFOptionsKind : public BC_PopupMenu
413 {
414         static const char *kinds[];
415 public:
416         FFOptionsWindow *fwin;
417         int kind;
418
419         void create_objects();
420         int handle_event();
421         void set(int k);
422
423         FFOptionsKind(FFOptionsWindow *fwin, int x, int y, int w);
424         ~FFOptionsKind();
425 };
426
427 class FFOptionsUnits : public BC_PopupMenu {
428 public:
429         FFOptionsWindow *fwin;
430
431         FFOptionsUnits(FFOptionsWindow *fwin, int x, int y, int w);
432         ~FFOptionsUnits();
433         int handle_event();
434 };
435
436 class FFOptionsText : public BC_TextBox {
437 public:
438         FFOptionsWindow *fwin;
439
440         FFOptionsText(FFOptionsWindow *fwin, int x, int y, int w);
441         ~FFOptionsText();
442         int handle_event();
443 };
444
445 class FFOptionsApply : public BC_GenericButton {
446 public:
447         FFOptionsWindow *fwin;
448
449         FFOptionsApply(FFOptionsWindow *fwin, int x, int y);
450         ~FFOptionsApply();
451         int handle_event();
452 };
453
454 class FFOptionsWindow : public BC_Window
455 {
456 public:
457         FFOptionsWindow(FFOptionsDialog *dialog, int x, int y);
458         ~FFOptionsWindow();
459
460         void create_objects();
461         void update(FFOptions_Opt *oip);
462         void draw();
463         int resize_event(int w, int h);
464
465         FFOptionsDialog *dialog;
466         FFOptions options;
467
468         FFOptions_OptPanel *panel;
469         int panel_x, panel_y, panel_w, panel_h;
470         BC_Title *type, *range, *kind_title;
471         FFOptions_Opt *selected;
472
473         FFOptionsKind *kind;
474         FFOptionsUnits *units;
475         FFOptionsText *text;
476         FFOptionsApply *apply;
477 };
478
479 class FFOptionsDialog : public BC_DialogThread
480 {
481 public:
482         FFOptionsDialog(FFMPEGConfigWindow *cfg_window);
483         ~FFOptionsDialog();
484         virtual void update_options(const char *options) = 0;
485
486         void load_options(const char *bp, int len);
487         void store_options(char *cp, int len);
488         void start();
489         BC_Window* new_gui();
490         void handle_done_event(int result);
491
492         FFMPEGConfigWindow *cfg_window;
493         FFOptionsWindow *options_window;
494         AVDictionary *ff_opts;
495         int wx, wy;
496 };
497
498 class FFOptionsAudioDialog : public FFOptionsDialog
499 {
500 public:
501         FFMPEGConfigAudio *aud_config;
502         void update_options(const char *options);
503
504         FFOptionsAudioDialog(FFMPEGConfigAudio *aud_config);
505         ~FFOptionsAudioDialog();
506 };
507
508 class FFOptionsVideoDialog : public FFOptionsDialog
509 {
510 public:
511         FFMPEGConfigVideo *vid_config;
512         void update_options(const char *options);
513
514         FFOptionsVideoDialog(FFMPEGConfigVideo *vid_config);
515         ~FFOptionsVideoDialog();
516 };
517
518 class FFOptionsFormatDialog : public FFOptionsDialog
519 {
520 public:
521         FFMPEGConfigFormat *fmt_config;
522         void update_options(const char *options);
523
524         FFOptionsFormatDialog(FFMPEGConfigFormat *fmt_config);
525         ~FFOptionsFormatDialog();
526 };
527
528 class FFOptionsViewAudio : public BC_GenericButton
529 {
530 public:
531         FFOptionsViewAudio(FFMPEGConfigAudio *aud_config, int x, int y, const char *text);
532         ~FFOptionsViewAudio();
533
534         int handle_event();
535         FFMPEGConfigAudio *aud_config;
536         AVCodecContext *avctx;
537 };
538
539 class FFOptionsViewVideo : public BC_GenericButton
540 {
541 public:
542         FFOptionsViewVideo(FFMPEGConfigVideo *vid_config, int x, int y, const char *text);
543         ~FFOptionsViewVideo();
544
545         int handle_event();
546         FFMPEGConfigVideo *vid_config;
547         AVCodecContext *avctx;
548 };
549
550 class FFOptionsViewFormat : public BC_GenericButton
551 {
552 public:
553         FFOptionsViewFormat(FFMPEGConfigWindow *cfg_window,
554                 EDL *edl, Asset *asset, int x, int y, const char *text);
555         ~FFOptionsViewFormat();
556
557         int handle_event();
558         FFMPEGConfigWindow *cfg_window;
559         EDL *edl;
560         Asset *asset;
561         FFOptionsFormatViewDialog *format_dialog;
562 };
563
564 class FFOptionsFormatView : public BC_GenericButton
565 {
566 public:
567         FFOptionsFormatView(FFMPEGConfigFormat *fmt_config, int x, int y, const char *text);
568         ~FFOptionsFormatView();
569         int handle_event();
570
571         FFMPEGConfigFormat *fmt_config;
572         AVFormatContext *fmt_ctx;
573 };
574
575 class FFOptionsFormatViewDialog : public BC_DialogThread
576 {
577 public:
578         FFOptionsFormatViewDialog(FFOptionsViewFormat *view_format, int wx, int wy);
579         ~FFOptionsFormatViewDialog();
580         BC_Window* new_gui();
581         void handle_done_event(int result);
582
583         FFOptionsViewFormat *view_format;
584         FFMPEGConfigFormat *cfg_window;
585         int wx, wy;
586 };
587
588 #endif