drag edithandle rework, lib x264/x265 update, dvb fixes, batchrender boot_defaults...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / filedb.C
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 #include "asset.h"
23 #include "filebase.h"
24 #include "file.h"
25 #include "filedb.h"
26 #include "mediadb.h"
27 #include "mwindow.h"
28 #include "preferences.h"
29 #include "vframe.h"
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <unistd.h>
34
35
36 FileDB::FileDB(Asset *asset, File *file)
37  : FileBase(asset, file)
38 {
39         reset_parameters();
40         mdb = new MediaDb();
41         if(asset->format == FILE_UNKNOWN)
42                 asset->format = FILE_DB;
43         clip_id = -1;
44         swidth = (SWIDTH+1) & ~1;
45         sheight = (SHEIGHT+1) & ~1;
46         frame_id = clip_size = -1;
47         seq_no = 0;
48         prefix_size = suffix_offset = -1;
49         framerate = -1;
50         vframe = 0;
51 }
52
53 FileDB::~FileDB()
54 {
55         close_file();
56         delete mdb;
57 }
58
59 int FileDB::check_sig(Asset *asset)
60 {
61         return !strncmp(asset->path,"/db:",4);
62 }
63
64 int FileDB::open_file(int rd, int wr)
65 {
66         int result = 0;
67         if( mdb->openDb() ) return 1;
68         mdb->detachDb();
69
70         if(rd) {
71                 char *cp = 0;
72                 clip_id = strtol(asset->path+4,&cp,0);
73                 if( !cp || cp == asset->path+4 || *cp != 0 ) result = 1;
74                 if( !result ) result = mdb->clip_id(clip_id);
75                 if( !result ) {
76                         framerate = mdb->clip_framerate();
77                         clip_size = mdb->clip_size();
78                         prefix_size = mdb->clip_prefix_size();
79                         suffix_offset = mdb->clip_frames() - clip_size;
80                         asset->audio_data = 0;
81                         asset->video_data = 1;
82                         asset->actual_width = swidth;
83                         asset->actual_height = sheight;
84                         if( !asset->layers ) asset->layers = 1;
85                         if( !asset->width ) asset->width = asset->actual_width;
86                         if( !asset->height ) asset->height = asset->actual_height;
87                         if( !asset->video_length ) asset->video_length = clip_size;
88                         if( !asset->frame_rate ) asset->frame_rate = framerate;
89                 }
90         }
91
92         if(!result && wr ) {
93                 result = 1;
94         }
95
96         if( result ) {
97                 mdb->closeDb();
98         }
99         return result;
100 }
101
102 int FileDB::close_file()
103 {
104         reset_parameters();
105         FileBase::close_file();
106         mdb->closeDb();
107         delete vframe;
108         vframe = 0;
109         return 0;
110 }
111
112 int FileDB::get_best_colormodel(Asset *asset, int driver)
113 {
114         return BC_YUV420P;
115 }
116
117 int FileDB::colormodel_supported(int colormodel)
118 {
119         return colormodel;
120 }
121
122
123 int FileDB::set_video_position(int64_t pos)
124 {
125         if( pos < 0 || pos >= asset->video_length )
126                 return 1;
127         seq_no = pos;
128         return 0;
129 }
130
131 int64_t FileDB::get_memory_usage()
132 {
133         return 0;
134 }
135
136
137
138 int FileDB::write_frames(VFrame ***frames, int len)
139 {
140         int result = 0;
141         return result;
142 }
143
144
145 int FileDB::read_frame(VFrame *frame)
146 {
147         int sw, sh;
148         mdb->attachDb();
149         int result = seq_no < clip_size ? 0 : 1;
150         if( !result ) {
151                 int n = seq_no++;
152                 if( !n ) { frame_id = -1; }
153                 else if( n >= prefix_size ) n += suffix_offset;
154                 result = mdb->get_sequences(clip_id, n);
155                 if( !result && mdb->timeline_sequence_no() == n )
156                         frame_id = mdb->timeline_frame_id();
157         }
158         VFrame *fp = frame->get_w() == swidth && frame->get_h() == sheight &&
159                         frame->get_color_model() == BC_YUV420P ? frame :
160                 !vframe ? (vframe = new VFrame(swidth,sheight,BC_YUV420P,0)) :
161                 vframe;
162         if( !result ) {
163                 if( frame_id < 0 )
164                         memset(fp->get_y(), 0, swidth*sheight);
165                 else
166                         result = mdb->get_image(frame_id, fp->get_y(), sw,sh);
167         }
168 //printf("seq_no=%d, result=%d\n",seq_no,result);
169         mdb->detachDb();
170         if( !result ) {
171                 memset(fp->get_u(),0x80,swidth/2 * sheight/2);
172                 memset(fp->get_v(),0x80,swidth/2 * sheight/2);
173         }
174         if( !result && fp == vframe ) {
175                 BC_CModels::transfer(frame->get_rows(), fp->get_rows(),
176                         frame->get_y(), frame->get_u(), frame->get_v(),
177                         fp->get_y(), fp->get_u(), fp->get_v(),
178                         0, 0, fp->get_w(), fp->get_h(),
179                         0, 0, frame->get_w(), frame->get_h(),
180                         fp->get_color_model(), frame->get_color_model(), 0,
181                         fp->get_bytes_per_line(), swidth);
182         }
183         return result;
184 }
185
186