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