Credit ffmpeg team with upgrade to 6.1
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / playbackengine.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 "bcsignals.h"
24 #include "cache.h"
25 #include "canvas.h"
26 #include "condition.h"
27 #include "edl.h"
28 #include "edlsession.h"
29 #include "localsession.h"
30 #include "mbuttons.h"
31 #include "mutex.h"
32 #include "mwindow.h"
33 #include "mwindowgui.h"
34 #include "patchbay.h"
35 #include "tracking.h"
36 #include "tracks.h"
37 #include "playbackengine.h"
38 #include "playtransport.h"
39 #include "preferences.h"
40 #include "renderengine.h"
41 #include "mainsession.h"
42 #include "trackcanvas.h"
43 #include "transportque.h"
44 #include "videodevice.h"
45 #include "vdevicex11.h"
46 #include "vrender.h"
47
48
49 PlaybackEngine::PlaybackEngine(MWindow *mwindow, Canvas *output)
50  : Thread(1, 0, 0)
51 {
52         this->mwindow = mwindow;
53         this->output = output;
54         is_playing_back = 0;
55         tracking_position = 0;
56         tracking_active = 0;
57         audio_cache = 0;
58         video_cache = 0;
59         command = new TransportCommand(mwindow->preferences);
60         command->command = STOP;
61         next_command = new TransportCommand(mwindow->preferences);
62         next_command->change_type = CHANGE_ALL;
63         stop_command = new TransportCommand(mwindow->preferences);
64         stop_command->command = STOP;
65         stop_command->realtime = 1;
66         sent_command = new TransportCommand(mwindow->preferences);
67         sent_command->command = -1;
68         send_active = 0;
69         tracking_lock = new Mutex("PlaybackEngine::tracking_lock");
70         renderengine_lock = new Mutex("PlaybackEngine::renderengine_lock");
71         tracking_done = new Condition(1, "PlaybackEngine::tracking_done");
72         pause_lock = new Condition(0, "PlaybackEngine::pause_lock");
73         start_lock = new Condition(0, "PlaybackEngine::start_lock");
74         cache_lock = new Mutex("PlaybackEngine::cache_lock");
75         input_lock = new Condition(1, "PlaybackEngine::input_lock");
76         output_lock = new Condition(0, "PlaybackEngine::output_lock", 1);
77
78         render_engine = 0;
79         debug = 0;
80 }
81
82 PlaybackEngine::~PlaybackEngine()
83 {
84         done = 1;
85         output_lock->unlock();
86         Thread::join();
87         delete_render_engine();
88         delete preferences;
89         if( audio_cache )
90                 audio_cache->remove_user();
91         if( video_cache )
92                 video_cache->remove_user();
93         delete tracking_lock;
94         delete tracking_done;
95         delete pause_lock;
96         delete start_lock;
97         delete cache_lock;
98         delete renderengine_lock;
99         delete command;
100         delete next_command;
101         delete stop_command;
102         delete sent_command;
103         delete input_lock;
104         delete output_lock;
105 }
106
107 void PlaybackEngine::create_objects()
108 {
109         preferences = new Preferences;
110         preferences->copy_from(mwindow->preferences);
111
112         done = 0;
113         Thread::start();
114         start_lock->lock("PlaybackEngine::create_objects");
115 }
116
117 ChannelDB* PlaybackEngine::get_channeldb()
118 {
119         PlaybackConfig *config = command->get_edl()->session->playback_config;
120         switch(config->vconfig->driver)
121         {
122                 case VIDEO4LINUX2JPEG:
123                         return mwindow->channeldb_v4l2jpeg;
124         }
125         return 0;
126 }
127
128 int PlaybackEngine::create_render_engine()
129 {
130 // Fix playback configurations
131         delete_render_engine();
132         render_engine = new RenderEngine(this, preferences, output, 0);
133 //printf("PlaybackEngine::create_render_engine %d\n", __LINE__);
134         return 0;
135 }
136
137 void PlaybackEngine::delete_render_engine()
138 {
139         renderengine_lock->lock("PlaybackEngine::delete_render_engine");
140         if( render_engine ) {
141                 render_engine->interrupt_playback();
142                 render_engine->wait_done();
143                 delete render_engine;  render_engine = 0;
144         }
145         renderengine_lock->unlock();
146 }
147
148 void PlaybackEngine::arm_render_engine()
149 {
150         renderengine_lock->lock("PlaybackEngine::arm_render_engine");
151         if( render_engine )
152                 render_engine->arm_command(command);
153         renderengine_lock->unlock();
154 }
155
156 void PlaybackEngine::start_render_engine()
157 {
158         renderengine_lock->lock("PlaybackEngine::start_render_engine");
159         if( render_engine )
160                 render_engine->start_command();
161         renderengine_lock->unlock();
162 }
163
164 void PlaybackEngine::wait_render_engine()
165 {
166         if( command->realtime && render_engine ) {
167                 render_engine->join();
168         }
169 }
170
171 void PlaybackEngine::create_cache()
172 {
173         cache_lock->lock("PlaybackEngine::create_cache");
174         if( audio_cache )
175                 audio_cache->remove_user();
176         if( video_cache )
177                 video_cache->remove_user();
178         audio_cache = new CICache(preferences);
179         video_cache = new CICache(preferences);
180         cache_lock->unlock();
181 }
182
183
184 void PlaybackEngine::perform_change()
185 {
186         switch( command->change_type ) {
187         case CHANGE_ALL:
188                 create_cache();
189         case CHANGE_EDL:
190                 create_render_engine();
191                 break;
192         case CHANGE_PARAMS: {
193                 renderengine_lock->lock("PlaybackEngine::perform_change");
194                 EDL *edl = render_engine ? render_engine->get_edl() : 0;
195                 if( edl ) edl->add_user();
196                 renderengine_lock->unlock();
197                 if( !edl ) break;
198                 edl->synchronize_params(command->get_edl());
199                 edl->remove_user();
200                 }
201         case CHANGE_NONE:
202                         break;
203         }
204 }
205
206 void PlaybackEngine::sync_parameters(EDL *edl)
207 {
208 // TODO: lock out render engine from keyframe deletions
209         command->get_edl()->synchronize_params(edl);
210         if( render_engine )
211                 render_engine->get_edl()->synchronize_params(edl);
212 }
213
214 void PlaybackEngine::interrupt_playback(int wait_tracking)
215 {
216         renderengine_lock->lock("PlaybackEngine::interrupt_playback");
217         if( render_engine )
218                 render_engine->interrupt_playback();
219         renderengine_lock->unlock();
220
221 // Stop pausing
222         pause_lock->unlock();
223
224 // Wait for tracking to finish if it is running
225         if( wait_tracking ) {
226                 tracking_done->lock("PlaybackEngine::interrupt_playback");
227                 tracking_done->unlock();
228         }
229 }
230
231 // Return 1 if levels exist
232 int PlaybackEngine::get_output_levels(double *levels, long position)
233 {
234         int result = 0;
235         if( render_engine && render_engine->do_audio ) {
236                 render_engine->get_output_levels(levels, position);
237                 result = 1;
238         }
239         return result;
240 }
241
242
243 int PlaybackEngine::get_module_levels(ArrayList<double> *module_levels, long position)
244 {
245         int result = 0;
246         if( render_engine && render_engine->do_audio ) {
247                 render_engine->get_module_levels(module_levels, position);
248                 result = 1;
249         }
250         return result;
251 }
252
253 int PlaybackEngine::brender_available(long position)
254 {
255         return 0;
256 }
257
258 void PlaybackEngine::init_cursor(int active)
259 {
260 }
261
262 void PlaybackEngine::init_meters()
263 {
264 }
265
266 void PlaybackEngine::stop_cursor()
267 {
268 }
269
270
271 void PlaybackEngine::init_tracking()
272 {
273         tracking_active = !command->single_frame() ? 1 : 0;
274         tracking_position = command->playbackstart;
275         tracking_done->lock("PlaybackEngine::init_tracking");
276         init_cursor(tracking_active);
277         init_meters();
278 }
279
280 void PlaybackEngine::stop_tracking(double position)
281 {
282         tracking_position = position;
283         tracking_active = 0;
284         stop_cursor();
285         tracking_done->unlock();
286 }
287
288 void PlaybackEngine::update_tracking(double position)
289 {
290         tracking_lock->lock("PlaybackEngine::update_tracking");
291         tracking_position = position;
292 // Signal that the timer is accurate.
293         if(tracking_active) tracking_active = 2;
294         tracking_timer.update();
295         tracking_lock->unlock();
296 }
297
298 double PlaybackEngine::get_tracking_position()
299 {
300         double result = 0;
301
302         tracking_lock->lock("PlaybackEngine::get_tracking_position");
303
304
305 // Adjust for elapsed time since last update_tracking.
306 // But tracking timer isn't accurate until the first update_tracking
307 // so wait.
308         if(tracking_active == 2)
309         {
310 //printf("PlaybackEngine::get_tracking_position %d %d %d\n", command->get_direction(), tracking_position, tracking_timer.get_scaled_difference(command->get_edl()->session->sample_rate));
311
312
313 // Don't interpolate when every frame is played.
314                 if( command->get_edl()->session->video_every_frame &&
315                     render_engine && render_engine->do_video ) {
316                         result = tracking_position;
317                 }
318                 else
319 // Interpolate
320                 {
321                         double loop_start, loop_end;
322                         int play_loop = command->loop_play ? 1 : 0;
323                         EDL *edl = command->get_edl();
324                         int loop_playback = edl->local_session->loop_playback ? 1 : 0;
325                         if( play_loop || !loop_playback ) {
326                                 loop_start = command->start_position;
327                                 loop_end = command->end_position;
328                         }
329                         else {
330                                 loop_start = edl->local_session->loop_start;
331                                 loop_end = edl->local_session->loop_end;
332                                 play_loop = 1;
333                         }
334                         double loop_size = loop_end - loop_start;
335
336                         if( command->get_direction() == PLAY_FORWARD ) {
337 // Interpolate
338                                 result = tracking_position +
339                                         command->get_speed() *
340                                         tracking_timer.get_difference() /
341                                         1000.0;
342
343 // Compensate for loop
344 //printf("PlaybackEngine::get_tracking_position 1 %d\n", command->get_edl()->local_session->loop_playback);
345                                 if( play_loop && loop_size > 0 ) {
346                                         while( result > loop_end ) result -= loop_size;
347                                 }
348                         }
349                         else {
350 // Interpolate
351                                 result = tracking_position -
352                                         command->get_speed() *
353                                         tracking_timer.get_difference() /
354                                         1000.0;
355
356 // Compensate for loop
357                                 if( play_loop && loop_size > 0 ) {
358                                         while( result < loop_start ) result += loop_size;
359                                 }
360                         }
361
362                 }
363         }
364         else
365                 result = tracking_position;
366
367         tracking_lock->unlock();
368 //printf("PlaybackEngine::get_tracking_position %f %f %d\n", result, tracking_position, tracking_active);
369
370 // Adjust for loop
371
372         return result;
373 }
374
375 void PlaybackEngine::update_transport(int command, int paused)
376 {
377 //      mwindow->gui->lock_window();
378 //      mwindow->gui->mbuttons->transport->update_gui_state(command, paused);
379 //      mwindow->gui->unlock_window();
380 }
381
382 void PlaybackEngine::run()
383 {
384         start_lock->unlock();
385
386         while( !done ) {
387 // Wait for current command to finish
388                 output_lock->lock("PlaybackEngine::run");
389                 if( done ) break;
390 // Read the new command
391                 input_lock->lock("PlaybackEngine::run");
392                 command->copy_from(sent_command);
393 //printf("sent command=%d\n", sent_command->command);
394                 int active = this->send_active;
395                 this->send_active = 0;
396                 input_lock->unlock();
397                 if( !active ) continue;
398
399                 interrupt_playback(0);
400                 wait_render_engine();
401
402                 switch( command->command ) {
403 // Parameter change only
404                 case COMMAND_NONE:
405                         perform_change();
406                         break;
407
408                 case PAUSE:
409                         init_cursor(0);
410                         pause_lock->lock("PlaybackEngine::run");
411                         stop_cursor();
412                         break;
413
414                 case STOP:
415 // No changing
416                         break;
417
418                 case CURRENT_FRAME:
419                 case LAST_FRAME:
420                         perform_change();
421                         arm_render_engine();
422 // Dispatch the command
423                         start_render_engine();
424                         break;
425 // fall through
426                 default:
427                         is_playing_back = 1;
428                 case REWIND:
429                 case GOTO_END:
430                         perform_change();
431                         arm_render_engine();
432
433 // Start tracking after arming so the tracking position doesn't change.
434 // The tracking for a single frame command occurs during PAUSE
435                         init_tracking();
436                         clear_borders();
437 // Dispatch the command
438                         start_render_engine();
439                         break;
440                 }
441 //printf("PlaybackEngine::run 100\n");
442         }
443 }
444
445 void PlaybackEngine::clear_borders()
446 {
447         EDL *edl = command->get_edl();
448         PlaybackConfig *config = edl->session->playback_config;
449         if( config->vconfig->driver == PLAYBACK_X11_GL ) {
450                 if( render_engine && render_engine->video ) {
451                         VDeviceBase *vdriver = render_engine->video->get_output_base();
452                         ((VDeviceX11*)vdriver)->clear_output();
453                         return;
454                 }
455         }
456         BC_WindowBase *window = output->get_canvas();
457         if( !window ) return;
458         window->lock_window("PlaybackEngine::clear_output");
459         output->clear_borders(edl);
460         window->unlock_window();
461 }
462
463 void PlaybackEngine::stop_playback(int wait_tracking)
464 {
465         transport_stop(wait_tracking);
466         renderengine_lock->lock("PlaybackEngine::stop_playback");
467         if( render_engine ) {
468                 render_engine->interrupt_playback();
469                 render_engine->wait_done();
470         }
471         renderengine_lock->unlock();
472 }
473
474 int PlaybackEngine::get_direction()
475 {
476         int curr_command = is_playing_back ? this->command->command : STOP;
477         return TransportCommand::get_direction(curr_command);
478 }
479
480 void PlaybackEngine::update_preferences(Preferences *prefs)
481 {
482         preferences->copy_from(prefs);
483         create_render_engine();
484 }
485
486 void PlaybackEngine::send_command(int command, EDL *edl, int wait_tracking, int use_inout)
487 {
488 //printf("PlaybackEngine::send_command 1 %d\n", command);
489 // Stop requires transferring the output buffer to a refresh buffer.
490         int curr_command = is_playing_back ? this->command->command : STOP;
491         int curr_single_frame = TransportCommand::single_frame(curr_command);
492         int curr_audio = this->command->toggle_audio ?
493                 !curr_single_frame : curr_single_frame;
494         int single_frame = TransportCommand::single_frame(command);
495         int next_audio = next_command->toggle_audio ? !single_frame : single_frame;
496         float next_speed = next_command->speed;
497 // Dispatch command
498         switch( command ) {
499         case STOP:
500                 transport_stop(wait_tracking);
501                 break;
502         case FAST_REWIND:       // Commands that play back
503         case NORMAL_REWIND:
504         case SLOW_REWIND:
505         case SINGLE_FRAME_REWIND:
506         case SINGLE_FRAME_FWD:
507         case SLOW_FWD:
508         case NORMAL_FWD:
509         case FAST_FWD:
510         case CURRENT_FRAME:
511         case LAST_FRAME:
512 // run shuttle as no prev command
513                 if( next_speed ) curr_command = COMMAND_NONE;
514 // Same direction pressed twice, not shuttle, and no change in audio state,  Stop
515                 if( curr_command == command && !curr_single_frame &&
516                     curr_audio == next_audio ) {
517                         transport_stop(wait_tracking);
518                         break;
519                 }
520 // Resume or change direction
521                 switch( curr_command ) {
522                 case REWIND:
523                 case GOTO_END:
524                 case STOP:
525                 case COMMAND_NONE:
526                 case SINGLE_FRAME_FWD:
527                 case SINGLE_FRAME_REWIND:
528                 case CURRENT_FRAME:
529                 case LAST_FRAME:
530 // already stopped
531                         break;
532                 default:
533                         transport_stop(0);
534                         next_command->resume = 1;
535                         break;
536                 }
537                 next_command->realtime = 1;
538                 transport_command(command, CHANGE_NONE, edl, use_inout);
539                 break;
540         case REWIND:
541         case GOTO_END:
542                 transport_stop(1);
543                 next_command->realtime = 1;
544                 transport_command(command, CHANGE_NONE, edl, use_inout);
545                 stop_tracking(this->command->playbackstart);
546                 break;
547         }
548 }
549
550 int PlaybackEngine::put_command(TransportCommand *command, int reset)
551 {
552         input_lock->lock("PlaybackEngine::put_command");
553         int prev_change_type = sent_command->change_type;
554         sent_command->copy_from(command);
555 // run only last command, sum change type
556         if( send_active )
557                 sent_command->change_type |= prev_change_type;
558         send_active = 1;
559         if( reset ) command->reset();
560         output_lock->unlock();
561         input_lock->unlock();
562         return 0;
563 }
564
565 int PlaybackEngine::transport_stop(int wait_tracking)
566 {
567         put_command(stop_command, 0);
568         if( wait_tracking ) {
569                 tracking_done->lock("PlaybackEngine::transport_stop");
570                 tracking_done->unlock();
571         }
572 //printf("send: %d (STOP) 0\n", STOP);
573         return 0;
574 }
575
576 int PlaybackEngine::transport_command(int command, int change_type, EDL *new_edl, int use_inout)
577 {
578         next_command->command = command;
579         next_command->change_type |= change_type;
580         if( new_edl ) {
581 // Just change the EDL if the change requires it because renderengine
582 // structures won't point to the new EDL otherwise and because copying the
583 // EDL for every cursor movement is slow.
584                 if( change_type == CHANGE_EDL || change_type == CHANGE_ALL )
585                         next_command->get_edl()->copy_all(new_edl);
586                 else if( change_type == CHANGE_PARAMS )
587                         next_command->get_edl()->synchronize_params(new_edl);
588                 next_command->set_playback_range(new_edl, use_inout,
589                                 preferences->forward_render_displacement);
590         }
591         put_command(next_command, 1);
592 //static const char *types[] = { "NONE",
593 // "FRAME_FWD", "NORMAL_FWD", "FAST_FWD", "FRAME_REV", "NORMAL_REV", "FAST_REV",
594 // "STOP",  "PAUSE", "SLOW_FWD", "SLOW_REV", "REWIND", "GOTO_END", "CURRENT_FRAME",
595 // "LAST_FRAME" };
596 //printf("send= %d (%s) %d\n", sent_command->command,
597 // types[sent_command->command], sent_command->locked);
598         return 0;
599 }
600
601 void PlaybackEngine::refresh_frame(int change_type, EDL *edl, int dir)
602 {
603         int command = dir >= 0 ? CURRENT_FRAME : LAST_FRAME;
604         next_command->realtime = 1;
605         transport_command(command, change_type, edl);
606 }
607