c59c3fec0be0c4d185f71f6bb4b64c2e0c78c350
[goodguy/history.git] / cinelerra-5.1 / cinelerra / vrender.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2009 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 "bcsignals.h"
24 #include "cache.h"
25 #include "clip.h"
26 #include "condition.h"
27 #include "datatype.h"
28 #include "edits.h"
29 #include "edl.h"
30 #include "edlsession.h"
31 #include "file.h"
32 #include "localsession.h"
33 #include "mainsession.h"
34 #include "mwindow.h"
35 #include "overlayframe.h"
36 #include "playabletracks.h"
37 #include "playbackengine.h"
38 #include "preferences.h"
39 #include "preferencesthread.h"
40 #include "renderengine.h"
41 #include "strategies.inc"
42 #include "tracks.h"
43 #include "transportque.h"
44 #include "units.h"
45 #include "vedit.h"
46 #include "vframe.h"
47 #include "videoconfig.h"
48 #include "videodevice.h"
49 #include "virtualconsole.h"
50 #include "virtualvconsole.h"
51 #include "vmodule.h"
52 #include "vrender.h"
53 #include "vtrack.h"
54
55
56
57
58
59 VRender::VRender(RenderEngine *renderengine)
60  : CommonRender(renderengine)
61 {
62         data_type = TRACK_VIDEO;
63         transition_temp = 0;
64         overlayer = new OverlayFrame(renderengine->preferences->processors);
65         input_temp = 0;
66         vmodule_render_fragment = 0;
67         playback_buffer = 0;
68         session_frame = 0;
69         asynchronous = 0;     // render 1 frame at a time
70         framerate_counter = 0;
71         video_out = 0;
72         render_strategy = -1;
73 }
74
75 VRender::~VRender()
76 {
77         if(input_temp) delete input_temp;
78         if(transition_temp) delete transition_temp;
79         if(overlayer) delete overlayer;
80 }
81
82
83 VirtualConsole* VRender::new_vconsole_object()
84 {
85         return new VirtualVConsole(renderengine, this);
86 }
87
88 int VRender::get_total_tracks()
89 {
90         return renderengine->get_edl()->tracks->total_video_tracks();
91 }
92
93 Module* VRender::new_module(Track *track)
94 {
95         return new VModule(renderengine, this, 0, track);
96 }
97
98 int VRender::flash_output()
99 {
100         if(video_out)
101                 return renderengine->video->write_buffer(video_out, renderengine->get_edl());
102         else
103                 return 0;
104 }
105
106 int VRender::process_buffer(VFrame *video_out,
107         int64_t input_position,
108         int use_opengl)
109 {
110 // process buffer for non realtime
111         int64_t render_len = 1;
112         int reconfigure = 0;
113
114
115         this->video_out = video_out;
116
117         current_position = input_position;
118
119         reconfigure = vconsole->test_reconfigure(input_position,
120                 render_len);
121
122         if(reconfigure) restart_playback();
123         return process_buffer(input_position, use_opengl);
124 }
125
126
127 int VRender::process_buffer(int64_t input_position,
128         int use_opengl)
129 {
130         VEdit *playable_edit = 0;
131         int colormodel;
132         int use_vconsole = 1;
133         int use_brender = 0;
134         int result = 0;
135         int use_cache = renderengine->command->single_frame();
136         int use_asynchronous =
137                 renderengine->command->realtime &&
138                 renderengine->get_edl()->session->video_every_frame &&
139                 renderengine->get_edl()->session->video_asynchronous;
140         const int debug = 0;
141
142 // Determine the rendering strategy for this frame.
143         use_vconsole = get_use_vconsole(&playable_edit, input_position, use_brender);
144         if(debug) printf("VRender::process_buffer %d use_vconsole=%d\n", __LINE__, use_vconsole);
145
146 // Negotiate color model
147         colormodel = get_colormodel(playable_edit, use_vconsole, use_brender);
148         if(debug) printf("VRender::process_buffer %d\n", __LINE__);
149
150
151 // Get output buffer from device
152         if(renderengine->command->realtime && !renderengine->is_nested)
153         {
154                 renderengine->video->new_output_buffer(&video_out, colormodel);
155         }
156
157         if(debug) printf("VRender::process_buffer %d video_out=%p\n", __LINE__, video_out);
158
159 // printf("VRender::process_buffer use_vconsole=%d colormodel=%d video_out=%p\n",
160 // use_vconsole,
161 // colormodel,
162 // video_out);
163 // Read directly from file to video_out
164         if(!use_vconsole)
165         {
166
167                 if(use_brender)
168                 {
169                         Asset *asset = renderengine->preferences->brender_asset;
170                         File *file = renderengine->get_vcache()->check_out(asset,
171                                 renderengine->get_edl());
172
173                         if(file)
174                         {
175                                 int64_t corrected_position = current_position;
176                                 if(renderengine->command->get_direction() == PLAY_REVERSE)
177                                         corrected_position--;
178
179 // Cache single frames only
180                                 if(use_asynchronous)
181                                         file->start_video_decode_thread();
182                                 else
183                                         file->stop_video_thread();
184                                 if(use_cache) file->set_cache_frames(1);
185                                 int64_t normalized_position = (int64_t)(corrected_position *
186                                         asset->frame_rate /
187                                         renderengine->get_edl()->session->frame_rate);
188
189                                 file->set_video_position(normalized_position,
190                                         0);
191                                 file->read_frame(video_out);
192
193
194                                 if(use_cache) file->set_cache_frames(0);
195                                 renderengine->get_vcache()->check_in(asset);
196                         }
197
198                 }
199                 else
200                 if(playable_edit)
201                 {
202                         if(debug) printf("VRender::process_buffer %d\n", __LINE__);
203                         result = ((VEdit*)playable_edit)->read_frame(video_out,
204                                 current_position,
205                                 renderengine->command->get_direction(),
206                                 renderengine->get_vcache(),
207                                 1,
208                                 use_cache,
209                                 use_asynchronous);
210                         if(debug) printf("VRender::process_buffer %d\n", __LINE__);
211                 }
212
213
214
215                 video_out->set_opengl_state(VFrame::RAM);
216         }
217         else
218 // Read into virtual console
219         {
220
221 // process this buffer now in the virtual console
222                 result = ((VirtualVConsole*)vconsole)->process_buffer(input_position,
223                         use_opengl);
224         }
225
226         return result;
227 }
228
229 // Determine if virtual console is needed
230 int VRender::get_use_vconsole(VEdit **playable_edit,
231         int64_t position, int &use_brender)
232 {
233         *playable_edit = 0;
234
235 // Background rendering completed
236         if((use_brender = renderengine->brender_available(position,
237                 renderengine->command->get_direction())) != 0)
238                 return 0;
239
240 // Descend into EDL nest
241         return renderengine->get_edl()->get_use_vconsole(playable_edit,
242                 position, renderengine->command->get_direction(),
243                 vconsole->playable_tracks);
244 }
245
246
247 int VRender::get_colormodel(VEdit *playable_edit,
248         int use_vconsole, int use_brender)
249 {
250         int colormodel = renderengine->get_edl()->session->color_model;
251
252         if(!use_vconsole && !renderengine->command->single_frame())
253         {
254 // Get best colormodel supported by the file
255                 int driver = renderengine->config->vconfig->driver;
256                 File *file;
257                 Asset *asset;
258
259                 if(use_brender)
260                 {
261                         asset = renderengine->preferences->brender_asset;
262                 }
263                 else
264                 {
265                         int64_t source_position = 0;
266                         asset = playable_edit->get_nested_asset(&source_position,
267                                 current_position,
268                                 renderengine->command->get_direction());
269                 }
270 // ffmpeg files are side effected by color_model, affects colorspace,color_range
271                 if( asset && asset->format != FILE_FFMPEG )
272                 {
273                         file = renderengine->get_vcache()->check_out(asset,
274                                 renderengine->get_edl());
275
276                         if(file)
277                         {
278                                 colormodel = file->get_best_colormodel(driver);
279                                 renderengine->get_vcache()->check_in(asset);
280                         }
281                 }
282         }
283
284         return colormodel;
285 }
286
287
288
289
290
291
292
293 void VRender::run()
294 {
295         int reconfigure;
296         const int debug = 0;
297
298 // Want to know how many samples rendering each frame takes.
299 // Then use this number to predict the next frame that should be rendered.
300 // Be suspicious of frames that render late so have a countdown
301 // before we start dropping.
302         int64_t current_sample, start_sample, end_sample; // Absolute counts.
303         int64_t skip_countdown = VRENDER_THRESHOLD;    // frames remaining until drop
304         int64_t delay_countdown = 0;  // Frames remaining until delay
305 // Number of frames before next reconfigure
306         int64_t current_input_length;
307 // Number of frames to skip.
308         int64_t frame_step = 1;
309         int use_opengl = (renderengine->video &&
310                 renderengine->video->out_config->driver == PLAYBACK_X11_GL);
311
312         first_frame = 1;
313
314 // Number of frames since start of rendering
315         session_frame = 0;
316         framerate_counter = 0;
317         framerate_timer.update();
318
319         start_lock->unlock();
320         if(debug) printf("VRender::run %d\n", __LINE__);
321
322
323         while(!done && !interrupt )
324         {
325 // Perform the most time consuming part of frame decompression now.
326 // Want the condition before, since only 1 frame is rendered
327 // and the number of frames skipped after this frame varies.
328                 current_input_length = 1;
329
330                 reconfigure = vconsole->test_reconfigure(current_position,
331                         current_input_length);
332
333
334                 if(debug) printf("VRender::run %d\n", __LINE__);
335                 if(reconfigure) restart_playback();
336
337                 if(debug) printf("VRender::run %d\n", __LINE__);
338                 process_buffer(current_position, use_opengl);
339
340
341                 if(debug) printf("VRender::run %d\n", __LINE__);
342
343                 if(renderengine->command->single_frame())
344                 {
345                         if(debug) printf("VRender::run %d\n", __LINE__);
346                         flash_output();
347                         frame_step = 1;
348                         done = 1;
349                 }
350                 else
351 // Perform synchronization
352                 {
353 // Determine the delay until the frame needs to be shown.
354                         current_sample = (int64_t)(renderengine->sync_position() *
355                                 renderengine->command->get_speed());
356 // latest sample at which the frame can be shown.
357                         end_sample = Units::tosamples(session_frame + 1,
358                                 renderengine->get_edl()->session->sample_rate,
359                                 renderengine->get_edl()->session->frame_rate);
360 // earliest sample by which the frame needs to be shown.
361                         start_sample = Units::tosamples(session_frame,
362                                 renderengine->get_edl()->session->sample_rate,
363                                 renderengine->get_edl()->session->frame_rate);
364
365                         if(first_frame || end_sample < current_sample)
366                         {
367 // Frame rendered late or this is the first frame.  Flash it now.
368 //printf("VRender::run %d\n", __LINE__);
369                                 flash_output();
370
371                                 if(renderengine->get_edl()->session->video_every_frame)
372                                 {
373 // User wants every frame.
374                                         frame_step = 1;
375                                 }
376                                 else
377                                 if(skip_countdown > 0)
378                                 {
379 // Maybe just a freak.
380                                         frame_step = 1;
381                                         skip_countdown--;
382                                 }
383                                 else
384                                 {
385 // Get the frames to skip.
386                                         delay_countdown = VRENDER_THRESHOLD;
387                                         frame_step = 1;
388                                         frame_step += (int64_t)Units::toframes(current_sample,
389                                                         renderengine->get_edl()->session->sample_rate,
390                                                         renderengine->get_edl()->session->frame_rate);
391                                         frame_step -= (int64_t)Units::toframes(end_sample,
392                                                                 renderengine->get_edl()->session->sample_rate,
393                                                                 renderengine->get_edl()->session->frame_rate);
394                                 }
395                         }
396                         else
397                         {
398 // Frame rendered early or just in time.
399                                 frame_step = 1;
400
401                                 if(delay_countdown > 0)
402                                 {
403 // Maybe just a freak
404                                         delay_countdown--;
405                                 }
406                                 else
407                                 {
408                                         skip_countdown = VRENDER_THRESHOLD;
409                                         if(start_sample > current_sample)
410                                         {
411                                                 int64_t delay_time = (int64_t)((float)(start_sample - current_sample) *
412                                                         1000 / renderengine->get_edl()->session->sample_rate);
413                                                 if( delay_time > 1000 ) delay_time = 1000;
414                                                 timer.delay(delay_time);
415                                         }
416                                         else
417                                         {
418 // Came after the earliest sample so keep going
419                                         }
420                                 }
421
422 // Flash frame now.
423 //printf("VRender::run %d %jd\n", __LINE__, current_input_length);
424                                 flash_output();
425                         }
426                 }
427                 if(debug) printf("VRender::run %d\n", __LINE__);
428
429 // Trigger audio to start
430                 if(first_frame)
431                 {
432                         renderengine->first_frame_lock->unlock();
433                         first_frame = 0;
434                         renderengine->reset_sync_position();
435                 }
436                 if(debug) printf("VRender::run %d\n", __LINE__);
437
438                 session_frame += frame_step;
439
440 // advance position in project
441                 current_input_length = frame_step;
442
443
444 // Subtract frame_step in a loop to allow looped playback to drain
445 // printf("VRender::run %d %d %d %d\n",
446 // __LINE__,
447 // done,
448 // frame_step,
449 // current_input_length);
450                 while(frame_step && current_input_length)
451                 {
452 // trim current_input_length to range
453                         get_boundaries(current_input_length);
454 // advance 1 frame
455                         advance_position(current_input_length);
456                         frame_step -= current_input_length;
457                         current_input_length = frame_step;
458                         if(done) break;
459 // printf("VRender::run %d %d %d %d\n",
460 // __LINE__,
461 // done,
462 // frame_step,
463 // current_input_length);
464                 }
465
466                 if(debug) printf("VRender::run %d current_position=%jd done=%d\n",
467                         __LINE__, current_position, done);
468
469 // Update tracking.
470                 if(renderengine->command->realtime &&
471                         renderengine->playback_engine &&
472                         renderengine->command->command != CURRENT_FRAME)
473                 {
474                         renderengine->playback_engine->update_tracking(fromunits(current_position));
475                 }
476                 if(debug) printf("VRender::run %d\n", __LINE__);
477
478 // Calculate the framerate counter
479                 framerate_counter++;
480                 if(framerate_counter >= renderengine->get_edl()->session->frame_rate &&
481                         renderengine->command->realtime)
482                 {
483                         renderengine->update_framerate((float)framerate_counter /
484                                 ((float)framerate_timer.get_difference() / 1000));
485                         framerate_counter = 0;
486                         framerate_timer.update();
487                 }
488                 if(debug) printf("VRender::run %d done=%d\n", __LINE__, done);
489                 if( !interrupt )
490                         interrupt = renderengine->video->interrupt;
491         }
492
493
494 // In case we were interrupted before the first loop
495         renderengine->first_frame_lock->unlock();
496         stop_plugins();
497         if(debug) printf("VRender::run %d done=%d\n", __LINE__, done);
498 }
499
500 int VRender::start_playback()
501 {
502 // start reading input and sending to vrenderthread
503 // use a thread only if there's a video device
504         if(renderengine->command->realtime)
505         {
506                 start();
507         }
508         return 0;
509 }
510
511 int64_t VRender::tounits(double position, int round)
512 {
513         if(round)
514                 return Units::round(position * renderengine->get_edl()->session->frame_rate);
515         else
516                 return Units::to_int64(position * renderengine->get_edl()->session->frame_rate);
517 }
518
519 double VRender::fromunits(int64_t position)
520 {
521         return (double)position / renderengine->get_edl()->session->frame_rate;
522 }
523