yuv colorspace/range + prefs, ffmpeg colorrange probe, x11 direct force colormodel...
[goodguy/history.git] / cinelerra-5.1 / cinelerra / filebase.h
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #ifndef FILEBASE_H
23 #define FILEBASE_H
24
25 #include "asset.inc"
26 #include "assets.inc"
27 #include "bccmodels.h"
28 #include "edit.inc"
29 #include "ffmpeg.inc"
30 #include "guicast.h"
31 #include "file.inc"
32 #include "filelist.inc"
33 #include "indexfile.inc"
34 #include "mainprogress.inc"
35 #include "overlayframe.inc"
36 #include "strategies.inc"
37 #include "vframe.inc"
38
39 #include <sys/types.h>
40
41 // Number of samples saved before the current read position
42 #define HISTORY_MAX 0x10000
43
44 // inherited by every file interpreter
45 class FileBase
46 {
47 public:
48         FileBase(Asset *asset, File *file);
49         virtual ~FileBase();
50
51
52         friend class File;
53         friend class FFMPEG;
54         friend class FileList;
55         friend class FrameWriter;
56
57
58
59
60         void get_mode(char *mode, int rd, int wr);
61         void reset_parameters();
62
63
64
65         virtual void get_parameters(BC_WindowBase *parent_window,
66                         Asset *asset,
67                         BC_WindowBase **format_window,
68                         int audio_options,
69                         int video_options,
70                         int lock_compressor) {};
71
72
73
74         virtual int get_index(IndexFile *index_file, MainProgressBar *progress_bar) { return -1; }
75         virtual int check_header() { return 0; }  // Test file to see if it is of this type.
76         virtual int reset_parameters_derived() { return 0; }
77         virtual int read_header() { return 1; }   // WAV files for getting header
78         virtual int get_best_colormodel(int driver, int vstream);
79
80         virtual int open_file(int rd, int wr) { return 1; }
81         virtual int close_file();
82         virtual int close_file_derived() { return 0; }
83         int set_dither();
84         virtual int seek_end() { return 0; }
85         virtual int seek_start() { return 0; }
86         virtual int64_t get_video_position() { return 0; }
87         virtual int64_t get_audio_position() { return 0; }
88         virtual int set_video_position(int64_t x) { return 0; }
89         virtual int set_audio_position(int64_t x) { return 0; }
90         virtual int set_subtitle(int value) { return -1; }
91         virtual int select_video_stream(Asset *asset, int vstream) { return -1; }
92         virtual int select_audio_stream(Asset *asset, int astream) { return -1; }
93         virtual int set_program(int no) { return -1; }
94         virtual int get_cell_time(int no, double &time) { time = -1.; return 1; }
95         virtual int get_system_time(int64_t &tm) { tm = -1; return 1; }
96         virtual int get_audio_for_video(int vstream, int astream, int64_t &channel_mask) {
97                  channel_mask = 0;  return -1;
98         }
99         virtual int get_video_pid(int track) { return -1; }
100         virtual int get_video_info(int track, int &pid, double &framerate,
101                 int &width, int &height, char *title=0) { return -1; }
102
103         virtual int get_thumbnail(int stream, int64_t &position,
104                 unsigned char *&thumbnail, int &ww, int &hh) { return -1; }
105         virtual int set_skimming(int track, int skim, skim_fn fn, void *vp) { return -1; }
106         virtual int skim_video(int track, void *vp, skim_fn fn) { return -1; }
107
108 // Subclass should call this to add the base class allocation.
109 // Only used in read mode.
110         virtual int64_t base_memory_usage();
111
112         virtual int write_samples(double **buffer,
113                 int64_t len) { return 0; }
114         virtual int write_frames(VFrame ***frames, int len) { return 0; }
115         virtual int read_compressed_frame(VFrame *buffer) { return 0; }
116         virtual int write_compressed_frame(VFrame *buffers) { return 0; }
117         virtual int64_t compressed_frame_size() { return 0; }
118 // Doubles are used to allow resampling
119         virtual int read_samples(double *buffer, int64_t len) { return 0; }
120         virtual int read_frame(VFrame *frame) { return 1; }
121 // get dvb record stream file descriptor
122         virtual int record_fd() { return -1; }
123
124         virtual int prefer_samples_float() { return 0; }
125         virtual int read_samples_float(float *buffer, int64_t len) { return 0; }
126
127 // Return either the argument or another colormodel which read_frame should
128 // use.
129         virtual int colormodel_supported(int colormodel) { return BC_RGB888; }
130 // This file can copy compressed frames directly from the asset
131         virtual int can_copy_from(Asset *asset, int64_t position) { return 0; }
132         virtual int can_scale_input() { return 0; }
133         virtual int get_render_strategy(ArrayList<int>* render_strategies) { return VRENDER_VPIXEL; }
134
135 // Manages an audio history buffer
136         void update_pcm_history(int64_t len);
137 // Returns history_start + history_size
138         int64_t get_history_sample();
139 // contiguous float
140         void append_history(float **new_data, int len);
141 // Interleaved short
142         void append_history(short *new_data, int len);
143         void read_history(double *dst,
144                 int64_t start_sample,
145                 int channel,
146                 int64_t len);
147         void allocate_history(int len);
148
149 // For static functions to access it
150         Asset *asset;
151         int rd, wr;
152
153 protected:
154 // Return 1 if the render_strategy is present on the list.
155         static int search_render_strategies(ArrayList<int>* render_strategies, int render_strategy);
156
157 // convert samples into file format
158         int64_t samples_to_raw(char *out_buffer,
159                                                         float **in_buffer, // was **buffer
160                                                         int64_t input_len,
161                                                         int bits,
162                                                         int channels,
163                                                         int byte_order,
164                                                         int signed_);
165
166 // overwrites the buffer from PCM data depending on feather.
167         int raw_to_samples(float *out_buffer, char *in_buffer,
168                 int64_t samples, int bits, int channels, int channel, int feather,
169                 float lfeather_len, float lfeather_gain, float lfeather_slope);
170
171 // Overwrite the buffer from float data using feather.
172         int overlay_float_buffer(float *out_buffer, float *in_buffer,
173                 int64_t samples,
174                 float lfeather_len, float lfeather_gain, float lfeather_slope);
175
176 // convert a frame to and from file format
177
178         int64_t frame_to_raw(unsigned char *out_buffer,
179                                         VFrame *in_frame,
180                                         int w,
181                                         int h,
182                                         int use_alpha,
183                                         int use_float,
184                                         int color_model);
185
186 // allocate a buffer for translating int to float
187         int get_audio_buffer(char **buffer, int64_t len, int64_t bits, int64_t channels); // audio
188
189 // Allocate a buffer for feathering floats
190         int get_float_buffer(float **buffer, int64_t len);
191
192 // allocate a buffer for translating video to VFrame
193         int get_video_buffer(unsigned char **buffer, int depth); // video
194         int get_row_pointers(unsigned char *buffer, unsigned char ***pointers, int depth);
195         static int match4(const char *in, const char *out);   // match 4 bytes for a fourcc type
196
197         float *float_buffer;          // for floating point feathering
198         unsigned char **row_pointers_in, **row_pointers_out;
199         int64_t prev_buffer_position;  // for audio determines if reading raw data is necessary
200         int64_t prev_frame_position;   // for video determines if reading raw video data is necessary
201         int64_t prev_bytes; // determines if new raw buffer is needed and used for getting memory usage
202         int64_t prev_len;
203         int prev_track;
204         int prev_layer;
205         int dither;
206         int internal_byte_order;
207         File *file;
208
209 // ================================= Audio compression
210         double **pcm_history;
211         int64_t history_allocated;
212         int64_t history_size;
213         int64_t history_start;
214         int history_channels;
215 // Range to decode to fill history buffer.  Maintained by FileBase.
216         int64_t decode_start;
217         int64_t decode_len;
218 // End of last decoded sample.  Maintained by user for seeking.
219         int64_t decode_end;
220
221 private:
222 // ULAW
223         float ulawtofloat(char ulaw);
224         char floattoulaw(float value);
225         int generate_ulaw_tables();
226         int delete_ulaw_tables();
227         float *ulawtofloat_table, *ulawtofloat_ptr;
228         unsigned char *floattoulaw_table, *floattoulaw_ptr;
229 };
230
231 #endif