Credit Andrew - fix vorbis audio which was scratchy and ensure aging plugin does...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / videowindow.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 "bchash.h"
23 #include "levelwindow.h"
24 #include "mainmenu.h"
25 #include "mwindow.h"
26 #include "mwindowgui.h"
27 #include "mainsession.h"
28 #include "vframe.h"
29 #include "videowindow.h"
30 #include "videowindowgui.h"
31
32
33 VideoWindow::VideoWindow(MWindow *mwindow)
34  : Thread(1, 0, 0)
35 {
36         this->mwindow = mwindow;
37         vbuffer = 0;
38         gui = 0;
39         video_window_w = 320;
40         video_visible = 0;
41         video_cropping = 0;
42 }
43
44 VideoWindow::~VideoWindow()
45 {
46         if(gui && running()) {
47                 gui->set_done(0);
48         }
49         join();
50         delete gui;  gui = 0;
51 }
52
53 int VideoWindow::load_defaults(BC_Hash *defaults)
54 {
55         video_visible = defaults->get("VIDEOVISIBLE", 1);
56         video_window_w = defaults->get("PLAYVIDEOW", video_window_w);
57         return 0;
58 }
59
60 int VideoWindow::update_defaults(BC_Hash *defaults)
61 {
62         defaults->update("VIDEOVISIBLE", video_visible);
63         defaults->update("PLAYVIDEOW", video_window_w);
64         return 0;
65 }
66
67 void VideoWindow::create_objects()
68 {
69         set_synchronous(1);
70         if(mwindow->gui)
71         {
72                 init_window();
73         }
74 }
75
76 int VideoWindow::init_window()
77 {
78 //      int w = mwindow->session->output_w;
79 //      int h = mwindow->session->output_h;
80 //
81 //      fix_size(w, h, video_window_w, mwindow->get_aspect_ratio());
82 //      gui = new VideoWindowGUI(this, w, h);
83 //      gui->create_objects();
84 }
85
86 int VideoWindow::show_window()
87 {
88         if(gui)
89         {
90                 video_visible = 1;
91                 gui->show_window();
92                 mwindow->gui->mainmenu->set_show_video(1);
93         }
94         return 0;
95 }
96
97 int VideoWindow::hide_window()
98 {
99         if(gui)
100         {
101                 video_visible = 0;
102                 gui->hide_window();
103                 mwindow->gui->mainmenu->set_show_video(0);
104         }
105         return 0;
106 }
107
108 int VideoWindow::resize_window()
109 {
110         int proper_w = mwindow->session->output_w;
111         int proper_h = mwindow->session->output_h;
112
113 //      fix_size(proper_w, proper_h, video_window_w, mwindow->get_aspect_ratio());
114
115         gui->update_title();
116         if(gui &&
117                 (gui->get_w() != proper_w ||
118                         gui->get_h() != proper_h))
119         {
120                 gui->resize_window(proper_w, proper_h);
121                 gui->canvas->reposition_window(0, 0, proper_w, proper_h);
122                 if(video_cropping) gui->canvas->draw_crop_box();
123                 gui->flash();
124         }
125         return 0;
126 }
127
128 int VideoWindow::fix_size(int &w, int &h, int width_given, float aspect_ratio)
129 {
130         w = width_given;
131         h = (int)((float)width_given / aspect_ratio);
132         return 0;
133 }
134
135 int VideoWindow::original_size()
136 {
137         int w, h;
138         get_full_sizes(w, h);
139         video_window_w = w;
140         resize_window();
141         return 0;
142 }
143
144 int VideoWindow::get_full_sizes(int &w, int &h)
145 {
146 //      if(mwindow->get_aspect_ratio() > (float)mwindow->session->output_w / mwindow->session->output_h)
147 //      {
148 //              w = (int)((float)mwindow->session->output_h * mwindow->get_aspect_ratio() + 0.5);
149 //              h = mwindow->session->output_h;
150 //      }
151 //      else
152 //      {
153 //              w = mwindow->session->output_w;
154 //              h = (int)((float)mwindow->session->output_w / mwindow->get_aspect_ratio() + 0.5);
155 //      }
156 }
157
158
159 void VideoWindow::run()
160 {
161         if(gui) gui->run_window();
162 }
163
164 int VideoWindow::init_video()
165 {
166         if(gui)
167         {
168                 gui->canvas->start_video();
169         }
170         return 0;
171 }
172
173 int VideoWindow::stop_video()
174 {
175         if(gui)
176         {
177                 gui->canvas->stop_video();
178         }
179         return 0;
180 }
181
182 int VideoWindow::update(BC_Bitmap *frame)
183 {
184         if(gui)
185         {
186                 gui->lock_window();
187 //              gui->canvas->draw_bitmap(frame, 1);
188                 gui->unlock_window();
189         }
190         return 0;
191 }
192
193 int VideoWindow::get_w()
194 {
195         if(gui) return gui->get_w();
196         else return 0;
197 }
198
199 int VideoWindow::get_h()
200 {
201         if(gui) return gui->get_h();
202         else return 0;
203 }
204
205 BC_Bitmap* VideoWindow::get_bitmap()
206 {
207         return gui->canvas->new_bitmap(gui->get_w(), gui->get_h());
208 }
209
210 int VideoWindow::reset()
211 {
212         return 0;
213 }
214
215 int VideoWindow::start_cropping()
216 {
217         video_cropping = 1;
218         gui->x1 = 0;
219         gui->y1 = 0;
220         gui->x2 = gui->get_w();
221         gui->y2 = gui->get_h();
222         gui->canvas->draw_crop_box();
223         gui->canvas->flash();
224         return 0;
225 }
226
227 int VideoWindow::get_aspect_ratio(float &aspect_w, float &aspect_h)
228 {
229         int new_w, new_h;
230         float zoom_factor = (float)video_window_w / mwindow->session->output_w;
231
232 // For new aspect ratio
233         new_w = (int)((float)(gui->x2 - gui->x1) / zoom_factor);
234         new_h = (int)((float)(gui->y2 - gui->y1) / zoom_factor);
235
236         mwindow->create_aspect_ratio(aspect_w, aspect_h, new_w, new_h);
237         return 0;
238 }
239
240 int VideoWindow::stop_cropping()
241 {
242 //      float x_zoom, y_zoom;
243 //      int new_w, new_h;
244 //      float zoom_factor;
245 //
246 //      if(mwindow->get_aspect_ratio() < (float)mwindow->session->output_w / mwindow->session->output_h)
247 //      {
248 //              x_zoom = 1;
249 //              y_zoom = (float)mwindow->session->output_w / mwindow->get_aspect_ratio() / mwindow->session->output_h;
250 //              zoom_factor = (float)video_window_w / mwindow->session->output_w;
251 //      }
252 //      else
253 //      {
254 //              x_zoom = (float)mwindow->session->output_h * mwindow->get_aspect_ratio() / mwindow->session->output_w;
255 //              y_zoom = 1;
256 //              zoom_factor = (float)video_window_w / (mwindow->session->output_h * mwindow->get_aspect_ratio());
257 //      }
258 //
259 //
260 //      gui->canvas->draw_crop_box();
261 //      gui->canvas->flash();
262 //      video_window_w = gui->x2 - gui->x1;
263 //
264 //      gui->x1 = (int)(gui->x1 / zoom_factor / x_zoom);
265 //      gui->y1 = (int)(gui->y1 / zoom_factor / y_zoom);
266 //      gui->x2 = (int)(gui->x2 / zoom_factor / x_zoom);
267 //      gui->y2 = (int)(gui->y2 / zoom_factor / y_zoom);
268 //
269 //      video_cropping = 0;
270 }