4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
27 #include "colormodels.h"
32 #include "filelist.inc"
33 #include "overlayframe.inc"
34 #include "strategies.inc"
37 #include <sys/types.h>
39 // Number of samples saved before the current read position
40 #define HISTORY_MAX 0x10000
42 // inherited by every file interpreter
46 FileBase(Asset *asset, File *file);
52 friend class FileList;
53 friend class FrameWriter;
58 int get_mode(char *mode, int rd, int wr);
59 int reset_parameters();
63 virtual void get_parameters(BC_WindowBase *parent_window,
65 BC_WindowBase **format_window,
68 int lock_compressor) {};
72 virtual int get_index(char *index_path) { return 1; }
73 virtual int check_header() { return 0; } // Test file to see if it is of this type.
74 virtual int reset_parameters_derived() { return 0; }
75 virtual int read_header() { return 1; } // WAV files for getting header
76 virtual int open_file(int rd, int wr) { return 1; }
77 virtual int close_file();
78 virtual int close_file_derived() { return 0; }
80 virtual int seek_end() { return 0; }
81 virtual int seek_start() { return 0; }
82 virtual int64_t get_video_position() { return 0; }
83 virtual int64_t get_audio_position() { return 0; }
84 virtual int set_video_position(int64_t x) { return 0; }
85 virtual int set_audio_position(int64_t x) { return 0; }
86 virtual int set_subtitle(int value) { return -1; }
87 virtual int select_video_stream(Asset *asset, int vstream) { return -1; }
88 virtual int select_audio_stream(Asset *asset, int astream) { return -1; }
89 virtual int set_program(int no) { return -1; }
90 virtual int get_cell_time(int no, double &time) { time = -1.; return 1; }
91 virtual int get_system_time(int64_t &tm) { tm = -1; return 1; }
92 virtual int get_audio_for_video(int vstream, int astream, int64_t &channel_mask) {
93 channel_mask = 0; return -1;
95 virtual int get_video_pid(int track) { return -1; }
96 virtual int get_video_info(int track, int &pid, double &framerate,
97 int &width, int &height, char *title=0) { return -1; }
99 virtual int get_thumbnail(int stream, int64_t &position,
100 unsigned char *&thumbnail, int &ww, int &hh) { return -1; }
101 virtual int set_skimming(int track, int skim, skim_fn fn, void *vp) { return -1; }
102 virtual int skim_video(int track, void *vp, skim_fn fn) { return -1; }
104 // Subclass should call this to add the base class allocation.
105 // Only used in read mode.
106 virtual int64_t base_memory_usage();
108 virtual int write_samples(double **buffer,
109 int64_t len) { return 0; }
110 virtual int write_frames(VFrame ***frames, int len) { return 0; }
111 virtual int read_compressed_frame(VFrame *buffer) { return 0; }
112 virtual int write_compressed_frame(VFrame *buffers) { return 0; }
113 virtual int64_t compressed_frame_size() { return 0; }
114 // Doubles are used to allow resampling
115 virtual int read_samples(double *buffer, int64_t len) { return 0; }
116 virtual int read_frame(VFrame *frame) { return 1; }
117 // get dvb record stream file descriptor
118 virtual int record_fd() { return -1; }
120 virtual int prefer_samples_float() {return 0;};
121 virtual int read_samples_float(float *buffer, int64_t len) { return 0; };
124 // Return either the argument or another colormodel which read_frame should
126 virtual int colormodel_supported(int colormodel) { return BC_RGB888; }
127 // This file can copy compressed frames directly from the asset
128 virtual int can_copy_from(Asset *asset, int64_t position) { return 0; }
129 virtual int get_render_strategy(ArrayList<int>* render_strategies) { return VRENDER_VPIXEL; }
131 // Manages an audio history buffer
132 void update_pcm_history(int64_t len);
133 // Returns history_start + history_size
134 int64_t get_history_sample();
136 void append_history(float **new_data, int len);
138 void append_history(short *new_data, int len);
139 void read_history(double *dst,
140 int64_t start_sample,
143 void allocate_history(int len);
145 // For static functions to access it
150 // Return 1 if the render_strategy is present on the list.
151 static int search_render_strategies(ArrayList<int>* render_strategies, int render_strategy);
153 // convert samples into file format
154 int64_t samples_to_raw(char *out_buffer,
155 float **in_buffer, // was **buffer
162 // overwrites the buffer from PCM data depending on feather.
163 int raw_to_samples(float *out_buffer, char *in_buffer,
164 int64_t samples, int bits, int channels, int channel, int feather,
165 float lfeather_len, float lfeather_gain, float lfeather_slope);
167 // Overwrite the buffer from float data using feather.
168 int overlay_float_buffer(float *out_buffer, float *in_buffer,
170 float lfeather_len, float lfeather_gain, float lfeather_slope);
172 // convert a frame to and from file format
174 int64_t frame_to_raw(unsigned char *out_buffer,
182 // allocate a buffer for translating int to float
183 int get_audio_buffer(char **buffer, int64_t len, int64_t bits, int64_t channels); // audio
185 // Allocate a buffer for feathering floats
186 int get_float_buffer(float **buffer, int64_t len);
188 // allocate a buffer for translating video to VFrame
189 int get_video_buffer(unsigned char **buffer, int depth); // video
190 int get_row_pointers(unsigned char *buffer, unsigned char ***pointers, int depth);
191 static int match4(const char *in, const char *out); // match 4 bytes for a quicktime type
193 int64_t ima4_samples_to_bytes(int64_t samples, int channels);
194 int64_t ima4_bytes_to_samples(int64_t bytes, int channels);
196 float *float_buffer; // for floating point feathering
197 unsigned char **row_pointers_in, **row_pointers_out;
198 int64_t prev_buffer_position; // for audio determines if reading raw data is necessary
199 int64_t prev_frame_position; // for video determines if reading raw video data is necessary
200 int64_t prev_bytes; // determines if new raw buffer is needed and used for getting memory usage
205 int internal_byte_order;
208 // ================================= Audio compression
209 double **pcm_history;
210 int64_t history_allocated;
211 int64_t history_size;
212 int64_t history_start;
213 int history_channels;
214 // Range to decode to fill history buffer. Maintained by FileBase.
215 int64_t decode_start;
217 // End of last decoded sample. Maintained by user for seeking.
227 float ulawtofloat(char ulaw);
228 char floattoulaw(float value);
229 int generate_ulaw_tables();
230 int delete_ulaw_tables();
231 float *ulawtofloat_table, *ulawtofloat_ptr;
232 unsigned char *floattoulaw_table, *floattoulaw_ptr;
237 int ima4_decode_block(int16_t *output, unsigned char *input);
238 int ima4_decode_sample(int *predictor, int nibble, int *index, int *step);
239 int ima4_encode_block(unsigned char *output, int16_t *input, int step, int channel);
240 int ima4_encode_sample(int *last_sample, int *last_index, int *nibble, int next_sample);
242 static int ima4_step[89];
243 static int ima4_index[16];
244 int *last_ima4_samples;
245 int *last_ima4_indexes;
247 int ima4_block_samples;
248 OverlayFrame *overlayer;