a/v per track data height, boxblur power fix, add french expanders
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / amodule.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2009-2013 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 "aattachmentpoint.h"
23 #include "aedit.h"
24 #include "amodule.h"
25 #include "aplugin.h"
26 #include "arender.h"
27 #include "asset.h"
28 #include "atrack.h"
29 #include "automation.h"
30 #include "bcsignals.h"
31 #include "cache.h"
32 #include "clip.h"
33 #include "edits.h"
34 #include "edl.h"
35 #include "edlsession.h"
36 #include "file.h"
37 #include "filexml.h"
38 #include "floatautos.h"
39 #include "language.h"
40 #include "module.h"
41 #include "patch.h"
42 #include "plugin.h"
43 #include "pluginarray.h"
44 #include "preferences.h"
45 #include "renderengine.h"
46 #include "mainsession.h"
47 #include "samples.h"
48 #include "sharedlocation.h"
49 #include "theme.h"
50 #include "transition.h"
51 #include "transportque.h"
52 #include "tracks.h"
53 #include <string.h>
54
55
56
57
58
59
60
61
62 AModuleResample::AModuleResample(AModule *module)
63  : Resample()
64 {
65         this->module = module;
66         bzero(nested_output, sizeof(Samples*) * MAX_CHANNELS);
67         nested_allocation = 0;
68 }
69
70 AModuleResample::~AModuleResample()
71 {
72         for(int i = 0; i < MAX_CHANNELS; i++)
73                 delete nested_output[i];
74 }
75
76 int AModuleResample::read_samples(Samples *buffer,
77                 int64_t start, int64_t len, int direction)
78 {
79         return module->read_samples(buffer, start, len, direction);
80 }
81
82 AModule::AModule(RenderEngine *renderengine,
83         CommonRender *commonrender,
84         PluginArray *plugin_array,
85         Track *track)
86  : Module(renderengine, commonrender, plugin_array, track)
87 {
88         data_type = TRACK_AUDIO;
89         channel = 0;
90         transition_temp = 0;
91         speed_temp = 0;
92         bzero(nested_output, sizeof(Samples*) * MAX_CHANNELS);
93         meter_history = new MeterHistory();
94         nested_allocation = 0;
95         resample = 0;
96         asset = 0;
97         file = 0;
98 }
99
100
101
102
103 AModule::~AModule()
104 {
105         delete transition_temp;
106         delete speed_temp;
107         delete meter_history;
108         for(int i = 0; i < MAX_CHANNELS; i++)
109                 delete nested_output[i];
110         delete resample;
111 }
112
113 int AModule::read_samples(Samples *buffer, int64_t start, int64_t len, int direction)
114 {
115         if( len < 0 ) return 1;
116         double *buffer_data = buffer->get_data();
117 // if start < 0, zero fill prefix.  if error, zero fill buffer
118         int64_t zeros = len;
119         int result = 0;
120         if( asset ) {
121 // Files only read going forward.
122                 if( direction == PLAY_REVERSE ) start -= len;
123                 int64_t sz = start >= 0 ? len : len + start;
124                 if( start < 0 ) start = 0;
125                 if( sz > 0 ) {
126                         file->set_audio_position(start);
127                         file->set_channel(channel);
128                         result = file->read_samples(buffer, sz);
129                         if( !result && (zeros-=sz) > 0 ) {
130                                 double *top_data = buffer_data + zeros;
131                                 memmove(top_data, buffer_data, sz*sizeof(*buffer_data));
132                         }
133                 }
134                 if( !result && direction == PLAY_REVERSE )
135                         Resample::reverse_buffer(buffer_data, len);
136         }
137         else if( nested_edl ) {
138                 if( nested_allocation < len ) {
139                         nested_allocation = len;
140                         for( int i=0; i<nested_edl->session->audio_channels; ++i ) {
141                                 delete nested_output[i];
142                                 nested_output[i] = new Samples(nested_allocation);
143                         }
144                 }
145                 result = nested_renderengine->arender->
146                         process_buffer(nested_output, len, start);
147                 if( !result ) {
148                         double *sample_data = nested_output[channel]->get_data();
149                         int buffer_size = len * sizeof(*buffer_data);
150                         memcpy(buffer_data, sample_data, buffer_size);
151                         zeros = 0;
152                 }
153         }
154         if( zeros > 0 )
155                 memset(buffer_data, 0, zeros*sizeof(*buffer_data));
156         return result;
157 }
158
159 AttachmentPoint* AModule::new_attachment(Plugin *plugin)
160 {
161         return new AAttachmentPoint(renderengine, plugin);
162 }
163
164
165 void AModule::create_objects()
166 {
167         Module::create_objects();
168 // Not needed in pluginarray
169         if( commonrender ) {
170                 meter_history->init(1, ((ARender*)commonrender)->total_peaks);
171                 meter_history->reset_channel(0);
172         }
173 }
174
175 int AModule::get_buffer_size()
176 {
177         if(renderengine)
178                 return renderengine->fragment_len;
179         else
180                 return plugin_array->get_bufsize();
181 }
182
183
184 CICache* AModule::get_cache()
185 {
186         if(renderengine)
187                 return renderengine->get_acache();
188         else
189                 return cache;
190 }
191
192
193 int AModule::import_samples(AEdit *edit,
194         int64_t start_project, int64_t edit_startproject, int64_t edit_startsource,
195         int direction, int sample_rate, Samples *buffer, int64_t fragment_len)
196 {
197         int result = 0;
198         if( fragment_len <= 0 )
199                 result = 1;
200         if( nested_edl && edit->channel >= nested_edl->session->audio_channels )
201                 result = 1;
202         double *buffer_data = buffer->get_data();
203 // buffer fragment adjusted for speed curve
204         Samples *speed_buffer = buffer;
205         double *speed_data = speed_buffer->get_data();
206         int64_t speed_fragment_len = fragment_len;
207         int dir = direction == PLAY_FORWARD ? 1 : -1;
208 // normal speed source boundaries in EDL samplerate
209         int64_t start_source = start_project - edit_startproject + edit_startsource;
210         double end_source = start_source + dir*speed_fragment_len;
211         double start_position = start_source;
212 //      double end_position = end_source;
213 // normal speed playback boundaries
214         double min_source = bmin(start_source, end_source);
215         double max_source = bmax(start_source, end_source);
216
217         this->channel = edit->channel;
218         int have_speed = track->has_speed();
219
220 // apply speed curve to source position so the timeline agrees with the playback
221         if( !result && have_speed ) {
222 // get speed adjusted start position from start of edit.
223                 FloatAuto *previous = 0, *next = 0;
224                 FloatAutos *speed_autos = (FloatAutos*)track->automation->autos[AUTOMATION_SPEED];
225                 double source_position = edit_startsource +
226                         speed_autos->automation_integral(edit_startproject,
227                                 start_project-edit_startproject, PLAY_FORWARD);
228                 min_source = source_position;
229                 max_source = source_position;
230 // calculate boundaries of input fragment required for speed curve
231                 int64_t pos = start_project;
232                 start_position = source_position;
233                 for( int64_t i=fragment_len; --i>=0; pos+=dir ) {
234                         double speed = speed_autos->get_value(pos, direction, previous, next);
235                         source_position += dir*speed;
236                         if( source_position > max_source ) max_source = source_position;
237                         if( source_position < min_source ) min_source = source_position;
238                 }
239 //              end_position = source_position;
240                 speed_fragment_len = (int64_t)(max_source - min_source);
241                 start_source = direction == PLAY_FORWARD ? min_source : max_source;
242                 if( speed_fragment_len > 0 ) {
243 // swap in the temp buffer
244                         if( speed_temp && speed_temp->get_allocated() < speed_fragment_len ) {
245                                 delete speed_temp;  speed_temp = 0;
246                         }
247                         if( !speed_temp )
248                                 speed_temp = new Samples(speed_fragment_len);
249                         speed_buffer = speed_temp;
250                         speed_data = speed_buffer->get_data();
251                 }
252         }
253
254         int edit_sample_rate = 0;
255         if( speed_fragment_len <= 0 )
256                 result = 1;
257
258         if( !result && edit->asset ) {
259                 nested_edl = 0;
260                 if( nested_renderengine ) {
261                         delete nested_renderengine;  nested_renderengine = 0;
262                 }
263 // Source is an asset
264                 asset = edit->asset;
265                 edit_sample_rate = asset->sample_rate;
266                 get_cache()->age();
267                 file = get_cache()->check_out(asset, get_edl());
268                 if( !file ) {
269                         printf(_("AModule::import_samples Couldn't open %s.\n"), asset->path);
270                         result = 1;
271                 }
272         }
273         else if( !result && edit->nested_edl ) {
274                 asset = 0;
275 // Source is a nested EDL
276                 if( !nested_edl || nested_edl->id != edit->nested_edl->id ) {
277                         nested_edl = edit->nested_edl;
278                         delete nested_renderengine;
279                         nested_renderengine = 0;
280                 }
281                 edit_sample_rate = nested_edl->session->sample_rate;
282                 int command = direction == PLAY_REVERSE ?
283                         NORMAL_REWIND : NORMAL_FWD;
284                 if( !nested_command )
285                         nested_command = new TransportCommand;
286                 nested_command->command = command;
287                 nested_command->get_edl()->copy_all(nested_edl);
288                 nested_command->change_type = CHANGE_ALL;
289                 nested_command->realtime = renderengine->command->realtime;
290                 if( !nested_renderengine ) {
291                         nested_renderengine = new RenderEngine(0, get_preferences(), 0, 1);
292                         nested_renderengine->set_acache(get_cache());
293                         nested_renderengine->arm_command(nested_command);
294                 }
295                 nested_renderengine->command->command = command;
296                 result = 0;
297         }
298         if( edit_sample_rate <= 0 )
299                 result = 1;
300
301         if( !result ) {
302 // speed_buffer is (have_speed ? speed_temp : buffer)
303                 if( sample_rate != edit_sample_rate ) {
304                         if( !resample )
305                                 resample = new AModuleResample(this);
306                         result = resample->resample(speed_buffer,
307                                 speed_fragment_len, edit_sample_rate,
308                                 sample_rate, start_source, direction);
309                 }
310                 else {
311                         result = read_samples(speed_buffer,
312                                 start_source, speed_fragment_len, direction);
313                 }
314         }
315         if( asset && file ) {
316                 file = 0;
317                 get_cache()->check_in(asset);
318         }
319 // Stretch it to fit the speed curve
320 // Need overlapping buffers to get the interpolation to work, but this
321 // screws up sequential effects.
322         if( !result && have_speed ) {
323                 FloatAuto *previous = 0, *next = 0;
324                 FloatAutos *speed_autos = (FloatAutos*)track->automation->autos[AUTOMATION_SPEED];
325                 int len1 = speed_fragment_len-1;
326                 double speed_position = start_position;
327                 double pos = start_project;
328 // speed                gnuplot> plot "/tmp/x.dat" using($1) with lines
329 // speed_position       gnuplot> plot "/tmp/x.dat" using($2) with lines
330 //FILE *fp = 0;
331 //if( !channel ) { fp = fopen("/tmp/x.dat", "a"); fprintf(fp," %f %f\n",0.,0.); }
332                 for( int64_t i=0; i<fragment_len; ++i,pos+=dir ) {
333                         int64_t speed_pos = speed_position;
334                         double speed = speed_autos->get_value(pos,
335                                 direction, previous, next);
336 //if(fp) fprintf(fp," %f %f\n", speed, speed_position);
337                         double next_position = speed_position + dir*speed;
338                         int64_t next_pos = next_position;
339                         int total = abs(next_pos - speed_pos);
340                         int k = speed_pos - min_source;
341                         if( dir < 0 ) k = len1 - k; // if buffer reversed
342                         double sample = speed_data[bclip(k, 0,len1)];
343                         if( total > 1 ) {
344                                 int d = next_pos >= speed_pos ? 1 : -1;
345                                 for( int j=total; --j>0; ) {
346                                         k += d;
347                                         sample += speed_data[bclip(k, 0,len1)];
348                                 }
349                                 sample /= total;
350                         }
351 #if 0
352                         else if( total < 1 ) {
353                                 int d = next_pos >= speed_pos ? 1 : -1;
354                                 k += d;
355                                 double next_sample = speed_data[bclip(k, 0,len1)];
356                                 double v = speed_position - speed_pos;
357                                 sample = (1.-v) * sample + v * next_sample;
358                         }
359 #endif
360                         buffer_data[i] = sample;
361                         speed_position = next_position;
362                 }
363 //if(fp) fclose(fp);
364         }
365
366         if( result )
367                 bzero(buffer_data, fragment_len*sizeof(*buffer_data));
368         return result;
369 }
370
371
372 int AModule::render(Samples *buffer,
373         int64_t input_len,
374         int64_t start_position,
375         int direction,
376         int sample_rate,
377         int use_nudge)
378 {
379         int64_t edl_rate = get_edl()->session->sample_rate;
380         const int debug = 0;
381
382 if(debug) printf("AModule::render %d\n", __LINE__);
383
384         if(use_nudge)
385                 start_position += track->nudge *
386                         sample_rate /
387                         edl_rate;
388         AEdit *playable_edit;
389         int64_t end_position;
390         if(direction == PLAY_FORWARD)
391                 end_position = start_position + input_len;
392         else
393                 end_position = start_position - input_len;
394         int buffer_offset = 0;
395         int result = 0;
396
397
398 // // Flip range around so the source is always read forward.
399 //      if(direction == PLAY_REVERSE)
400 //      {
401 //              start_project -= input_len;
402 //              end_position -= input_len;
403 //      }
404
405
406 // Clear buffer
407         bzero(buffer->get_data(), input_len * sizeof(double));
408
409 // The EDL is normalized to the requested sample rate because
410 // the requested rate may be the project sample rate and a sample rate
411 // might as well be directly from the source rate to the requested rate.
412 // Get first edit containing the range
413         if(direction == PLAY_FORWARD)
414                 playable_edit = (AEdit*)track->edits->first;
415         else
416                 playable_edit = (AEdit*)track->edits->last;
417 if(debug) printf("AModule::render %d\n", __LINE__);
418
419         while(playable_edit)
420         {
421                 int64_t edit_start = playable_edit->startproject;
422                 int64_t edit_end = playable_edit->startproject + playable_edit->length;
423
424 // Normalize to requested rate
425                 edit_start = edit_start * sample_rate / edl_rate;
426                 edit_end = edit_end * sample_rate / edl_rate;
427
428                 if(direction == PLAY_FORWARD)
429                 {
430                         if(start_position < edit_end && end_position > edit_start)
431                         {
432                                 break;
433                         }
434                         playable_edit = (AEdit*)playable_edit->next;
435                 }
436                 else
437                 {
438                         if(end_position < edit_end && start_position > edit_start)
439                         {
440                                 break;
441                         }
442                         playable_edit = (AEdit*)playable_edit->previous;
443                 }
444         }
445
446
447 if(debug) printf("AModule::render %d\n", __LINE__);
448
449
450
451
452
453 // Fill output one fragment at a time
454         while(start_position != end_position)
455         {
456                 int64_t fragment_len = input_len;
457
458 if(debug) printf("AModule::render %d %jd %jd\n", __LINE__, start_position, end_position);
459 // Clamp fragment to end of input
460                 if(direction == PLAY_FORWARD &&
461                         start_position + fragment_len > end_position)
462                         fragment_len = end_position - start_position;
463                 else
464                 if(direction == PLAY_REVERSE &&
465                         start_position - fragment_len < end_position)
466                         fragment_len = start_position - end_position;
467 if(debug) printf("AModule::render %d %jd\n", __LINE__, fragment_len);
468
469 // Normalize position here since update_transition is a boolean operation.
470                 update_transition(start_position *
471                                 edl_rate /
472                                 sample_rate,
473                         PLAY_FORWARD);
474
475                 if(playable_edit)
476                 {
477                         AEdit *previous_edit = (AEdit*)playable_edit->previous;
478
479 // Normalize EDL positions to requested rate
480                         int64_t edit_startproject = playable_edit->startproject;
481                         int64_t edit_endproject = playable_edit->startproject + playable_edit->length;
482                         int64_t edit_startsource = playable_edit->startsource;
483 if(debug) printf("AModule::render %d %jd\n", __LINE__, fragment_len);
484
485                         edit_startproject = edit_startproject * sample_rate / edl_rate;
486                         edit_endproject = edit_endproject * sample_rate / edl_rate;
487                         edit_startsource = edit_startsource * sample_rate / edl_rate;
488 if(debug) printf("AModule::render %d %jd\n", __LINE__, fragment_len);
489
490
491
492 // Clamp fragment to end of edit
493                         if(direction == PLAY_FORWARD &&
494                                 start_position + fragment_len > edit_endproject)
495                                 fragment_len = edit_endproject - start_position;
496                         else
497                         if(direction == PLAY_REVERSE &&
498                                 start_position - fragment_len < edit_startproject)
499                                 fragment_len = start_position - edit_startproject;
500 if(debug) printf("AModule::render %d %jd\n", __LINE__, fragment_len);
501
502 // Clamp to end of transition
503                         int64_t transition_len = 0;
504                         Plugin *transition = get_edl()->tracks->plugin_exists(transition_id);
505                         if( transition && previous_edit ) {
506                                 transition_len = transition->length * sample_rate / edl_rate;
507                                 if(direction == PLAY_FORWARD &&
508                                         start_position < edit_startproject + transition_len &&
509                                         start_position + fragment_len > edit_startproject + transition_len)
510                                         fragment_len = edit_startproject + transition_len - start_position;
511                                 else
512                                 if(direction == PLAY_REVERSE &&
513                                         start_position > edit_startproject + transition_len &&
514                                         start_position - fragment_len < edit_startproject + transition_len)
515                                         fragment_len = start_position - edit_startproject - transition_len;
516                         }
517 if(debug) printf("AModule::render %d buffer_offset=%d fragment_len=%jd\n",
518 __LINE__,
519 buffer_offset,
520 fragment_len);
521
522                         Samples output(buffer);
523                         output.set_offset(output.get_offset() + buffer_offset);
524                         if(import_samples(playable_edit,
525                                 start_position,
526                                 edit_startproject,
527                                 edit_startsource,
528                                 direction,
529                                 sample_rate,
530                                 &output,
531                                 fragment_len)) result = 1;
532
533 if(debug) printf("AModule::render %d\n", __LINE__);
534
535
536 // Read transition into temp and render
537                         if(transition && previous_edit)
538                         {
539                                 int64_t previous_startproject = previous_edit->startproject *
540                                         sample_rate /
541                                         edl_rate;
542                                 int64_t previous_startsource = previous_edit->startsource *
543                                         sample_rate /
544                                         edl_rate;
545
546 // Allocate transition temp size
547                                 int transition_fragment_len = fragment_len;
548                                 if(direction == PLAY_FORWARD &&
549                                         fragment_len + start_position > edit_startproject + transition_len)
550                                         fragment_len = edit_startproject + transition_len - start_position;
551
552
553 // Read into temp buffers
554 // Temp + master or temp + temp ? temp + master
555                                 if(transition_temp &&
556                                         transition_temp->get_allocated() < fragment_len)
557                                 {
558                                         delete transition_temp;
559                                         transition_temp = 0;
560                                 }
561
562                                 if(!transition_temp)
563                                 {
564                                         transition_temp = new Samples(fragment_len);
565                                 }
566
567 if(debug) printf("AModule::render %d %jd\n", __LINE__, fragment_len);
568
569                                 if(transition_fragment_len > 0)
570                                 {
571 // Previous_edit is always the outgoing segment, regardless of direction
572                                         import_samples(previous_edit,
573                                                 start_position,
574                                                 previous_startproject,
575                                                 previous_startsource,
576                                                 direction,
577                                                 sample_rate,
578                                                 transition_temp,
579                                                 transition_fragment_len);
580                                         int64_t current_position;
581
582 // Reverse buffers here so transitions always render forward.
583                                         if(direction == PLAY_REVERSE)
584                                         {
585                                                 Resample::reverse_buffer(output.get_data(), transition_fragment_len);
586                                                 Resample::reverse_buffer(transition_temp->get_data(), transition_fragment_len);
587                                                 current_position = start_position -
588                                                         transition_fragment_len -
589                                                         edit_startproject;
590                                         }
591                                         else
592                                         {
593                                                 current_position = start_position - edit_startproject;
594                                         }
595
596                                         transition_server->process_transition(
597                                                 transition_temp,
598                                                 &output,
599                                                 current_position,
600                                                 transition_fragment_len,
601                                                 transition->length);
602
603 // Reverse output buffer here so transitions always render forward.
604                                         if(direction == PLAY_REVERSE)
605                                                 Resample::reverse_buffer(output.get_data(),
606                                                         transition_fragment_len);
607                                 }
608                         }
609 if(debug) printf("AModule::render %d start_position=%jd end_position=%jd fragment_len=%jd\n",
610  __LINE__, start_position, end_position, fragment_len);
611
612                         if(direction == PLAY_REVERSE)
613                         {
614                                 if(playable_edit && start_position - fragment_len <= edit_startproject)
615                                         playable_edit = (AEdit*)playable_edit->previous;
616                         }
617                         else
618                         {
619                                 if(playable_edit && start_position + fragment_len >= edit_endproject)
620                                         playable_edit = (AEdit*)playable_edit->next;
621                         }
622                 }
623
624                 if(fragment_len > 0)
625                 {
626                         buffer_offset += fragment_len;
627                         if(direction == PLAY_FORWARD)
628                                 start_position += fragment_len;
629                         else
630                                 start_position -= fragment_len;
631                 }
632         }
633
634 if(debug) printf("AModule::render %d\n", __LINE__);
635
636         return result;
637 }
638
639
640
641
642
643
644
645
646