Credit Andrew - fix vorbis audio which was scratchy and ensure aging plugin does...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / mediadb.h
1 /*
2  * CINELERRA
3  * Copyright (C) 2016-2020 William Morrow
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published
7  * by the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20
21 #if defined(__i386__) || defined(__x86_64__)
22 #if defined (__linux__)
23 #ifndef _MEDIA_DB_H_
24 #define _MEDIA_DB_H_
25
26 #include "arraylist.h"
27 #include "mediadb.inc"
28 #include "filexml.inc"
29 #include "bcwindowbase.inc"
30 #include "file.inc"
31 #include "linklist.h"
32 #include "../db/tdb.h"
33
34 #include <stdint.h>
35 #include <stdlib.h>
36 #include <math.h>
37 #include <semaphore.h>
38 #include <sys/ipc.h>
39 #include <sys/shm.h>
40
41
42 void write_pbm(uint8_t *tp, int w, int h, const char *fmt, ...);
43 void write_pgm(uint8_t *tp, int w, int h, const char *fmt, ...);
44 void pbm_diff(uint8_t *ap, uint8_t *bp, int w, int h, const char *fmt, ...);
45
46
47 class Scale {
48   int rx, ry, rw, rh, iw, ih;
49   int ix0, ix1, iy0, iy1, rsh;
50   double xf0, xf1, yf0, yf1, pw;
51   uint8_t *idat, *odat;
52   uint8_t *row(int y) { return idat + (iw<<rsh) * (y>>rsh); }
53
54   inline double scalex(uint8_t *rp, double v, double yw) {
55     int x = ix0;  rp += x;
56     double vv = xf0 * *rp++;
57     int iv = 0;  while( ++x < ix1 ) iv += *rp++;
58     vv += iv + xf1 * *rp;
59     return v += vv * yw;
60   }
61
62   inline double scalexy() {
63     double yw, v = 0;
64     int y = iy0;
65     if( (yw = pw * yf0) > 0 ) v = scalex(row(y), v, yw);
66     while( ++y < iy1 ) v = scalex(row(y), v, pw);
67     if( (yw = pw * yf1) > 0 ) v = scalex(row(y), v, yw);
68     return v;
69   }
70 public:
71   Scale(uint8_t *idata, int type, int iw, int ih, int rx, int ry, int rw, int rh) {
72     rsh = type ? 1 : 0;
73     this->idat = idata + ry*iw + rx;
74     this->iw = iw;  this->ih = ih;
75     this->rx = rx;  this->ry = ry;
76     this->rw = rw;  this->rh = rh;
77   }
78   void scale(uint8_t *odata, int ow, int oh, int sx, int sy, int sw, int sh);
79 };
80
81
82 enum {
83          DEL_START,     // always the first mark
84          DEL_MARK,      // boundary between cur/next media deletion
85          DEL_SKIP,      // skip media in current deletion region
86          DEL_OOPS,      // ignore previous mark
87 };
88
89 // definition:
90 //   dele: n. A sign indicating that something is to be removed from <media>
91
92 class Dele : public ListItem<Dele>
93 {
94 public:
95         double time;
96         int action;
97
98         Dele(double t, int a) : time(t), action(a) {}
99         ~Dele() {}
100 };
101
102 class Deletions: public List<Dele>
103 {
104         int save(FileXML &xml);
105         int load(FileXML &xml);
106 public:
107         int pid;
108         char filepath[BCTEXTLEN];
109         const char *file_path() { return filepath; }
110         bool empty() { return !first; }
111
112         int write_dels(const char *filename);
113         static Deletions *read_dels(const char *filename);
114
115         Deletions(int pid, const char *fn);
116         ~Deletions();
117 };
118
119
120 class Clip : public ListItem<Clip> {
121 public:
122         int clip_id;
123         int votes, index, groups, cycle, muted;
124         double start, end, best;
125
126         Clip(int clip_id, double start, double end, int index);
127         ~Clip();
128 };
129
130 class Clips : public List<Clip> {
131 public:
132         int pid, count, cycle;
133         Clip *current;
134         Clip *locate(int id, double start, double end);
135         int get_clip(int idx, int &id, double &start, double &end);
136         virtual Clip *new_clip(int clip_id, double start, double end, int index);
137         int check(int clip_id, double start, double end, int group, double err);
138         int save(FileXML &xml);
139         int load(FileXML &xml);
140         Clips(int pid=0);
141         ~Clips();
142 };
143
144 class Snip : public Clip {
145 public:
146         ArrayList<double> weights;
147         ArrayList<double> positions;
148
149         Snip(int clip_id, double start, double end, int index);
150         ~Snip();
151 };
152
153 class Snips : public Clips {
154 public:
155         Clip *new_clip(int clip_id, double start, double end, int index);
156         Snips();
157         ~Snips();
158 };
159
160
161 class MediaDb
162 {
163         theDb *db;
164         int opened;
165
166         uint8_t frm[SFRM_SZ];
167         double position, error_limit;
168         int lid, nid, distance, group_mask;
169         double fmean, fsd, fcx, fcy, fmoment;
170         int64_t lerr, pixlmt;
171         Db::pgRef clip_id_loc;
172
173         int get_key(Clips *clips, Db::rKey &prev_rkey, Db::rKey &next_rkey);
174         int64_t key_compare(Clips *clips, Db::ObjectLoc &loc);
175         static double mean(uint8_t *dat, int n, int stride=1);
176         static double variance(uint8_t *dat, double mn, int n, int stride=1);
177         static double std_dev(uint8_t *dat, double mn, int n, int stride=1) {
178                 return sqrt(variance(dat, mn, n, stride));
179         }
180         static double centroid(uint8_t *dat, int n, int stride=1);
181         static void centroid(uint8_t *dat, int w, int h, double &xx, double &yy);
182         static void deviation(uint8_t *a, uint8_t *b, int sz, int margin,
183                 double &dev, int &ofs, int stride=1);
184         static int64_t cmpr(uint8_t *ap, uint8_t *bp, int sz);
185         static int64_t diff(uint8_t *a, uint8_t *b, int w, int h, int dx, int dy, int bias);
186         static void fit(int *dat, int n, double &a, double &b);
187         static void eq(uint8_t *sp, uint8_t *dp, int len);
188         static void eq(uint8_t *p, int len) { eq(p, p, len); }
189 public:
190         int newDb();
191         int openDb(int rw=0);
192         int resetDb(int rw=0);
193         void closeDb();
194         void commitDb();
195         void undoDb();
196         int attachDb(int rw=0);
197         int detachDb();
198         int is_open();
199         int transaction();
200
201         static void hist_eq(uint8_t *dp, int len);
202         static void scale(uint8_t *idata,int iw,int ih,int rx,int ry,int rw,int rh,
203                           uint8_t *odata,int ow,int oh,int sx,int sy,int sw,int sh);
204         int new_clip_set(const char *title, const char *asset_path, double position,
205                 double framerate, int frames, int prefix_size, int suffix_size,
206                 int64_t creation_time, int64_t system_time);
207         int new_clip_view(int clip_id, int64_t access_time);
208         int access_clip(int clip_id, int64_t access_time=0);
209         int del_clip_set(int clip_id);
210         int new_frame(int clip_id, uint8_t *dat, int no, int group, double offset);
211         int get_timeline_by_frame_id(int fid);
212         int get_timelines(int frame_id);
213         int next_timelines();
214         int get_sequences(int clip_id, int seq_no=0);
215         int next_sequences();
216         int get_image(int id, uint8_t *dat, int &w, int &h);
217         int get_frame_key(uint8_t *dat, int &fid,
218                 Clips *clips=0, double pos=0, int mask=~0);
219         int get_media_key(uint8_t *key, int &fid, int dist, double errlmt,
220                 Clips *clips=0, double pos=0, int mask=~0);
221         int add_timelines(Clips *clips, int fid, double err);
222         // clip_set accessors
223         int clip_id(int id);
224         int clip_id();
225         int clips_first_id();
226         int clips_next_id();
227         const char *clip_title();
228         const char *clip_path();
229         double clip_position();
230         double clip_framerate();
231         double clip_average_weight();
232         void clip_average_weight(double avg_wt);
233         double clip_length();
234         int clip_frames();
235         int clip_prefix_size();
236         int clip_suffix_size();
237         int clip_size();
238         double *clip_weights();
239         int64_t clip_creation_time();
240         int64_t clip_system_time();
241
242         // timeline accessors
243         int timeline_id(int id);
244         int timeline_id();
245         int timeline_clip_id();
246         int timeline_sequence_no();
247         int timeline_frame_id();
248         int timeline_group();
249         double timeline_offset();
250
251         // clip_views accessors
252         int views_id(int id);
253         int views_id();
254         int views_clip_id(int id);
255         int views_clip_id();
256         int64_t views_access_time();
257         int views_access_count();
258
259         MediaDb();
260         ~MediaDb();
261 };
262
263 #endif
264 #endif // linux
265 #endif /* x86 */