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