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