mostly cosmetic changes to LTC generator
[ardour.git] / libs / ardour / session_ltc.cc
1 /*
2   Copyright (C) 2012 Paul Davis
3   Written by Robin Gareus <robin@gareus.org>
4
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 2 of the License, or
8   (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20 #include "ardour/debug.h"
21 #include "timecode/time.h"
22 #include "ardour/session.h"
23 #include "ardour/audioengine.h"
24 #include "ardour/audio_port.h"
25 #include "ardour/slave.h"
26
27 #include "i18n.h"
28
29 using namespace std;
30 using namespace ARDOUR;
31 using namespace MIDI;
32 using namespace PBD;
33 using namespace Timecode;
34
35 /* really verbose timing debug */
36 //#define LTC_GEN_FRAMEDBUG
37 //#define LTC_GEN_TXDBUG
38
39 void
40 Session::ltc_tx_initialize()
41 {
42         ltc_enc_tcformat = config.get_timecode_format();
43
44         DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX init sr: %1 fps: %2\n", nominal_frame_rate(), timecode_to_frames_per_second(ltc_enc_tcformat)));
45         ltc_encoder = ltc_encoder_create(nominal_frame_rate(),
46                         timecode_to_frames_per_second(ltc_enc_tcformat),
47                         0);
48
49         ltc_encoder_set_bufsize(ltc_encoder, nominal_frame_rate(), 23.0);
50
51         /* buffersize for 1 LTC frame: (1 + sample-rate / fps) bytes
52          * usually returned by ltc_encoder_get_buffersize(encoder)
53          *
54          * since the fps can change and A3's  min fps: 24000/1001 */
55         ltc_enc_buf = (ltcsnd_sample_t*) calloc((nominal_frame_rate() / 23), sizeof(ltcsnd_sample_t));
56         ltc_speed = 0;
57         ltc_tx_reset();
58 }
59
60 void
61 Session::ltc_tx_cleanup()
62 {
63         DEBUG_TRACE (DEBUG::LTC, "LTC TX cleanup\n");
64         if (ltc_enc_buf) free(ltc_enc_buf);
65         ltc_encoder_free(ltc_encoder);
66         ltc_encoder = NULL;
67 }
68
69 void
70 Session::ltc_tx_reset()
71 {
72         DEBUG_TRACE (DEBUG::LTC, "LTC TX reset\n");
73         ltc_enc_pos = -9999; // force re-start
74         ltc_buf_len = 0;
75         ltc_buf_off = 0;
76         ltc_enc_byte = 0;
77         ltc_enc_cnt = 0;
78 }
79
80 void
81 Session::ltc_tx_recalculate_position()
82 {
83         SMPTETimecode enctc;
84         Timecode::Time a3tc;
85         ltc_encoder_get_timecode(ltc_encoder, &enctc);
86
87         a3tc.hours   = enctc.hours;
88         a3tc.minutes = enctc.mins;
89         a3tc.seconds = enctc.secs;
90         a3tc.frames  = enctc.frame;
91         a3tc.rate = timecode_to_frames_per_second(ltc_enc_tcformat);
92         a3tc.drop = timecode_has_drop_frames(ltc_enc_tcformat);
93
94         Timecode::timecode_to_sample (a3tc, ltc_enc_pos, true, false,
95                 double(frame_rate()),
96                 config.get_subframes_per_frame(),
97                 config.get_timecode_offset_negative(), config.get_timecode_offset()
98                 );
99 }
100
101 int
102 Session::ltc_tx_send_time_code_for_cycle (framepos_t start_frame, framepos_t end_frame,
103                 double target_speed, double current_speed,
104                 pframes_t nframes)
105 {
106         assert(nframes > 0);
107
108         jack_default_audio_sample_t *out;
109         pframes_t txf = 0;
110         boost::shared_ptr<Port> ltcport = engine().ltc_output_port();
111
112         if (!ltc_encoder || !ltc_enc_buf || !ltcport || ! ltcport->jack_port()) return 0;
113
114         out = (jack_default_audio_sample_t*) jack_port_get_buffer (ltcport->jack_port(), nframes);
115         if (!out) return 0;
116
117         SyncSource sync_src = Config->get_sync_source();
118         if (engine().freewheeling()
119                         || !Config->get_send_ltc()
120                         /* TODO
121                          * decide which time-sources we can generated LTC from.
122                          * Internal, JACK or sample-synced slaves should be fine.
123                          *
124                          *
125                          || (config.get_external_sync() && sync_src == LTC)
126                          || (config.get_external_sync() && sync_src == MTC)
127                          */
128                          || (config.get_external_sync() && sync_src == MIDIClock)
129                         ) {
130                 memset(out, 0, nframes * sizeof(jack_default_audio_sample_t));
131                 return nframes;
132         }
133
134         /* range from libltc (38..218) || - 128.0  -> (-90..90) */
135         const float ltcvol = Config->get_ltc_output_volume()/(90.0); // pow(10, db/20.0)/(90.0);
136
137         jack_latency_range_t ltc_latency;
138         ltcport->get_connected_latency_range(ltc_latency, true);
139         DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX %1 to %2 / %3 | lat: %4\n", start_frame, end_frame, nframes, ltc_latency.max));
140
141         /* all systems go. Now here's the plan:
142          *
143          *  1) check if fps has changed
144          *  2) check direction of encoding, calc speed, re-sample existing buffer
145          *  3) calculate frame and byte to send aligned to jack-period size
146          *  4) check if it's the frame/byte that is already in the queue
147          *  5) if (4) mismatch, re-calculate offset of LTC frame relative to period size
148          *  6) actual LTC audio output
149          *  6a) send remaining part of already queued frame; break on nframes
150          *  6b) encode new LTC-frame byte
151          *  6c) goto 6a
152          *  7) done
153          */
154
155         // (1) check fps
156         TimecodeFormat cur_timecode = config.get_timecode_format();
157         if (cur_timecode != ltc_enc_tcformat) {
158                 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX1: TC format mismatch - reinit sr: %1 fps: %2\n", nominal_frame_rate(), timecode_to_frames_per_second(cur_timecode)));
159                 if (ltc_encoder_reinit(ltc_encoder, nominal_frame_rate(), timecode_to_frames_per_second(cur_timecode), 0)) {
160                         PBD::error << _("LTC encoder: invalid framerate - LTC encoding is disabled for the remainder of this session.") << endmsg;
161                         ltc_tx_cleanup();
162                         return 0;
163                 }
164                 ltc_enc_tcformat = cur_timecode;
165                 ltc_tx_reset();
166         }
167
168         /* LTC is max. 30 fps */
169         if (timecode_to_frames_per_second(cur_timecode) > 30) {
170                 memset(out, 0, nframes * sizeof(jack_default_audio_sample_t));
171                 return nframes;
172         }
173
174         // (2) speed & direction
175 #define SIGNUM(a) ( (a) < 0 ? -1 : 1)
176         bool speed_changed = false;
177
178         /* use port latency compensation */
179 #if 1
180         /* The generated timecode is offset by the port-latency,
181          * therefore the offset depends on the direction of transport.
182          */
183         framepos_t cycle_start_frame = (current_speed < 0) ? (start_frame + ltc_latency.max) : (start_frame - ltc_latency.max);
184 #else
185         /* This comes in handy when testing sync - record output on an A3 track
186          * see also http://tracker.ardour.org/view.php?id=5073
187          */
188         framepos_t cycle_start_frame = start_frame;
189 #endif
190
191         /* cycle-start may become negative due to latency compensation */
192         if (cycle_start_frame < 0) { cycle_start_frame = 0; }
193
194         double new_ltc_speed = double(labs(end_frame - start_frame) * SIGNUM(current_speed)) / double(nframes);
195
196         if (SIGNUM(new_ltc_speed) != SIGNUM (ltc_speed)) {
197                 DEBUG_TRACE (DEBUG::LTC, "LTC TX2: transport changed direction\n");
198                 ltc_tx_reset();
199         }
200
201         if (ltc_speed != new_ltc_speed) {
202                 /* check ./libs/ardour/interpolation.cc  CubicInterpolation::interpolate
203                  * if target_speed != current_speed we should interpolate, too.
204                  *
205                  * However, currency in A3 target_speed == current_speed for each process cycle
206                  * (except for the sign and if target_speed > 8.0).
207                  * Besides, above speed calculation uses the difference (end_frame - start_frame).
208                  * end_frame is calculated from 'frames_moved' which includes the interpolation.
209                  * so we're good.
210                  */
211                 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX2: speed change old: %1 cur: %2 tgt: %3 ctd: %4\n", ltc_speed, current_speed, target_speed, fabs(current_speed) - target_speed));
212                 speed_changed = true;
213         }
214
215         if (end_frame == start_frame || fabs(current_speed) < 0.1 ) {
216                 DEBUG_TRACE (DEBUG::LTC, "LTC TX2: transport is not rolling or absolute-speed < 0.1\n");
217                 /* keep repeating current frame
218                  *
219                  * an LTC generator must be able to continue generating LTC when Ardours transport is in stop
220                  * some machines do odd things if LTC goes away:
221                  * e.g. a tape based machine (video or audio), some think they have gone into park if LTC goes away,
222                  * so unspool the tape from the playhead. That might be inconvenient.
223                  * If LTC keeps arriving they remain in a stop position with the tape on the playhead.
224                  */
225                 new_ltc_speed = 0;
226                 if (!Config->get_ltc_send_continuously()) {
227                         ltc_speed = new_ltc_speed;
228                         memset(out, 0, nframes * sizeof(jack_default_audio_sample_t));
229                         return nframes;
230                 }
231         }
232
233         if (fabs(new_ltc_speed) > 10.0) {
234                 DEBUG_TRACE (DEBUG::LTC, "LTC TX2: speed is out of bounds.\n");
235                 ltc_tx_reset();
236                 memset(out, 0, nframes * sizeof(jack_default_audio_sample_t));
237                 return nframes;
238         }
239
240         if (ltc_speed == 0 && new_ltc_speed != 0) {
241                 DEBUG_TRACE (DEBUG::LTC, "LTC TX2: transport started rolling - reset\n");
242                 ltc_tx_reset();
243         }
244
245         /* the timecode duration corresponding to the samples that are still
246          * in the buffer. Here, the speed of previous cycle is used to calculate
247          * the alignment at the beginning of this cycle later.
248          */
249         double poff = (ltc_buf_len - ltc_buf_off) * ltc_speed;
250
251         if (speed_changed && new_ltc_speed != 0) {
252                 /* we need to re-sample the existing buffer.
253                  * "make space for the en-coder to catch up to the new speed"
254                  *
255                  * since the LTC signal is a rectangular waveform we can simply squeeze it
256                  * by removing samples or duplicating samples /here and there/.
257                  *
258                  * There may be a more elegant way to do this, in fact one could
259                  * simply re-render the buffer using ltc_encoder_encode_byte()
260                  * but that'd require some timecode offset buffer magic,
261                  * which is left for later..
262                  */
263
264                 double oldbuflen = double(ltc_buf_len - ltc_buf_off);
265                 double newbuflen = double(ltc_buf_len - ltc_buf_off) * fabs(ltc_speed / new_ltc_speed);
266
267                 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX2: bufOld %1 bufNew %2 | diff %3\n",
268                                         (ltc_buf_len - ltc_buf_off), newbuflen, newbuflen - oldbuflen
269                                         ));
270
271                 double bufrspdiff = rint(newbuflen - oldbuflen);
272
273                 if (abs(bufrspdiff) > newbuflen || abs(bufrspdiff) > oldbuflen) {
274                         DEBUG_TRACE (DEBUG::LTC, "LTC TX2: resampling buffer would destroy information.\n");
275                         ltc_tx_reset();
276                         poff = 0;
277                 } else if (bufrspdiff != 0 && newbuflen > oldbuflen) {
278                         int incnt = 0;
279                         double samples_to_insert = ceil(newbuflen - oldbuflen);
280                         double avg_distance = newbuflen / samples_to_insert;
281                         DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX2: resample buffer insert: %1\n", samples_to_insert));
282
283                         for (int rp = ltc_buf_off; rp < ltc_buf_len - 1; ++rp) {
284                                 const int ro = rp - ltc_buf_off;
285                                 if (ro < (incnt*avg_distance)) continue;
286                                 const ltcsnd_sample_t v1 = ltc_enc_buf[rp];
287                                 const ltcsnd_sample_t v2 = ltc_enc_buf[rp+1];
288                                 if (v1 != v2 && ro < ((incnt+1)*avg_distance)) continue;
289                                 memmove(&ltc_enc_buf[rp+1], &ltc_enc_buf[rp], ltc_buf_len-rp);
290                                 incnt++;
291                                 ltc_buf_len++;
292                         }
293                 } else if (bufrspdiff != 0 && newbuflen < oldbuflen) {
294                         double samples_to_remove = ceil(oldbuflen - newbuflen);
295                         DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX2: resample buffer - remove: %1\n", samples_to_remove));
296                         if (oldbuflen <= samples_to_remove) {
297                                 ltc_buf_off = ltc_buf_len= 0;
298                         } else {
299                                 double avg_distance = newbuflen / samples_to_remove;
300                                 int rmcnt = 0;
301                                 for (int rp = ltc_buf_off; rp < ltc_buf_len - 1; ++rp) {
302                                         const int ro = rp - ltc_buf_off;
303                                         if (ro < (rmcnt*avg_distance)) continue;
304                                         const ltcsnd_sample_t v1 = ltc_enc_buf[rp];
305                                         const ltcsnd_sample_t v2 = ltc_enc_buf[rp+1];
306                                         if (v1 != v2 && ro < ((rmcnt+1)*avg_distance)) continue;
307                                         memmove(&ltc_enc_buf[rp], &ltc_enc_buf[rp+1], ltc_buf_len-rp-1);
308                                         ltc_buf_len--;
309                                         rmcnt++;
310                                 }
311                         }
312                 }
313         }
314
315         ltc_speed = new_ltc_speed;
316         DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX2: transport speed %1.\n", ltc_speed));
317
318
319         // (3) bit/sample alignment
320         Timecode::Time tc_start;
321         framepos_t tc_sample_start;
322
323         /* calc timecode frame from current position - round down to nearest timecode */
324         sample_to_timecode(cycle_start_frame, tc_start, true, false);
325
326         /* convert timecode back to sample-position */
327         Timecode::timecode_to_sample (tc_start, tc_sample_start, true, false,
328                 double(frame_rate()),
329                 config.get_subframes_per_frame(),
330                 config.get_timecode_offset_negative(), config.get_timecode_offset()
331                 );
332
333         /* difference between current frame and TC frame in samples */
334         frameoffset_t soff = cycle_start_frame - tc_sample_start;
335         DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX3: A3cycle: %1 = A3tc: %2 +off: %3\n",
336                                 cycle_start_frame, tc_sample_start, soff));
337
338
339         // (4) check if alignment matches
340         const double fptcf = frames_per_timecode_frame(); // convenient, used a lot below.
341
342         /* maximum difference of bit alignment in audio-samples.
343          *
344          * if transport and LTC generator differs more than this, the LTC
345          * generator will be re-initialized
346          *
347          * due to rounding error and variations in LTC-bit duration depending
348          * on the speed, it can be off by +- ltc_speed audio-samples.
349          * When the playback speed changes, it can actually reach +- 2 * ltc_speed
350          * in the cycle _after_ the speed changed. The average delta however is 0.
351          */
352         double maxdiff;
353
354         if (config.get_external_sync() && slave()) {
355                 maxdiff = slave()->resolution();
356         } else {
357                 maxdiff = ceil(fabs(ltc_speed))*2.0;
358         }
359
360         DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX4: enc: %1 + %2 - %3 || buf-bytes: %4 enc-byte: %5\n",
361                                 ltc_enc_pos, ltc_enc_cnt, poff, (ltc_buf_len - ltc_buf_off), poff, ltc_enc_byte));
362
363         DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX4: enc-pos: %1  | d: %2\n",
364                                 ltc_enc_pos + ltc_enc_cnt - poff,
365                                 rint(ltc_enc_pos + ltc_enc_cnt - poff) - cycle_start_frame
366                                 ));
367
368         if (ltc_speed != 0 && fabs(ceil(ltc_enc_pos + ltc_enc_cnt - poff) - cycle_start_frame) > maxdiff)
369         {
370                 // (5) re-align
371                 ltc_tx_reset();
372
373                 /* set frame to encode */
374                 SMPTETimecode tc;
375                 tc.hours = tc_start.hours;
376                 tc.mins = tc_start.minutes;
377                 tc.secs = tc_start.seconds;
378                 tc.frame = tc_start.frames;
379                 ltc_encoder_set_timecode(ltc_encoder, &tc);
380
381                 /* workaround for libltc recognizing 29.97 and 30000/1001 as drop-frame TC.
382                  * In A3 30000/1001 or 30 fps can be drop-frame.
383                  */
384                 LTCFrame ltcframe;
385                 ltc_encoder_get_frame(ltc_encoder, &ltcframe);
386                 ltcframe.dfbit = timecode_has_drop_frames(cur_timecode)?1:0;
387                 ltc_encoder_set_frame(ltc_encoder, &ltcframe);
388
389
390                 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX4: now: %1 trs: %2 toff %3\n", cycle_start_frame, tc_sample_start, soff));
391
392                 uint32_t cyc_off;
393                 assert(soff >= 0 && soff < fptcf);
394
395                 if (ltc_speed < 0 ) {
396                         /* calculate the byte that starts at or after the current position */
397                         ltc_enc_byte = floor((10.0 * soff) / (fptcf));
398                         ltc_enc_cnt = double(ltc_enc_byte * fptcf / 10.0);
399
400                         /* calculate difference between the current position and the byte to send */
401                         cyc_off = soff- ceil(ltc_enc_cnt);
402
403                 } else {
404                         /* calculate the byte that starts at or after the current position */
405                         ltc_enc_byte = ceil((10.0 * soff) / fptcf);
406                         ltc_enc_cnt = double(ltc_enc_byte * fptcf / 10.0);
407
408                         /* calculate difference between the current position and the byte to send */
409                         cyc_off = ceil(ltc_enc_cnt) - soff;
410
411                         if (ltc_enc_byte == 10) {
412                                 ltc_enc_byte = 0;
413                                 ltc_encoder_inc_timecode(ltc_encoder);
414                         }
415                 }
416
417                 assert (cyc_off >= 0);
418
419                 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX5 restart encoder: soff %1 byte %2 cycoff %3\n",
420                                         soff, ltc_enc_byte, cyc_off));
421
422                 if (cyc_off > 0 && cyc_off <= nframes) {
423                         /* offset in this cycle */
424                         txf= rint(cyc_off / fabs(ltc_speed));
425                         memset(out, 0, cyc_off * sizeof(jack_default_audio_sample_t));
426                 } else {
427                         /* resync next cycle */
428                         memset(out, 0, cyc_off * sizeof(jack_default_audio_sample_t));
429                         return nframes;
430                 }
431
432                 ltc_enc_pos = tc_sample_start;
433
434                 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX5 restart @ %1 + %2 - %3 |  byte %4\n",
435                                         ltc_enc_pos, ltc_enc_cnt, cyc_off, ltc_enc_byte));
436         }
437
438
439         // (6) encode and output
440         while (1) {
441 #ifdef LTC_GEN_TXDBUG
442                 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX6.1 @%1  [ %2 / %3 ]\n", txf, ltc_buf_off, ltc_buf_len));
443 #endif
444                 // (6a) send remaining buffer
445                 while ((ltc_buf_off < ltc_buf_len) && (txf < nframes)) {
446                         const float v1 = ltc_enc_buf[ltc_buf_off++] - 128.0;
447                         const jack_default_audio_sample_t val = (jack_default_audio_sample_t) (v1*ltcvol);
448                         out[txf++] = val;
449                 }
450 #ifdef LTC_GEN_TXDBUG
451                 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX6.2 @%1  [ %2 / %3 ]\n", txf, ltc_buf_off, ltc_buf_len));
452 #endif
453
454                 if (txf >= nframes) {
455                         DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX7 enc: %1 [ %2 / %3 ] byte: %4 spd %5 fpp %6 || nf: %7\n",
456                                                 ltc_enc_pos, ltc_buf_off, ltc_buf_len, ltc_enc_byte, ltc_speed, nframes, txf));
457                         return nframes;
458                 }
459
460                 ltc_buf_len = 0;
461                 ltc_buf_off = 0;
462
463                 // (6b) encode LTC, bump timecode
464
465                 if (ltc_speed < 0) {
466                         ltc_enc_byte = (ltc_enc_byte + 9)%10;
467                         if (ltc_enc_byte == 9) {
468                                 ltc_encoder_dec_timecode(ltc_encoder);
469                                 ltc_tx_recalculate_position();
470                                 ltc_enc_cnt = fptcf;
471                         }
472                 }
473
474                 if (ltc_encoder_encode_byte(ltc_encoder, ltc_enc_byte, (ltc_speed==0)?1.0:(1.0/ltc_speed))) {
475                         DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX6.3 encoder error byte %1\n", ltc_enc_byte));
476                         ltc_encoder_buffer_flush(ltc_encoder);
477                         ltc_tx_reset();
478                         memset(out, 0, nframes * sizeof(jack_default_audio_sample_t));
479                         return -1;
480                 }
481                 int enc_frames = ltc_encoder_get_buffer(ltc_encoder, &(ltc_enc_buf[ltc_buf_len]));
482 #ifdef LTC_GEN_FRAMEDBUG
483                 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX6.3 encoded %1 bytes for LTC-byte %2 at spd %3\n", enc_frames, ltc_enc_byte, ltc_speed));
484 #endif
485                 if (enc_frames <=0) {
486                         DEBUG_TRACE (DEBUG::LTC, "LTC TX6.3 encoder empty buffer.\n");
487                         ltc_encoder_buffer_flush(ltc_encoder);
488                         ltc_tx_reset();
489                         memset(out, 0, nframes * sizeof(jack_default_audio_sample_t));
490                         return -1;
491                 }
492
493                 ltc_buf_len += enc_frames;
494                 if (ltc_speed < 0)
495                         ltc_enc_cnt -= fptcf/10.0;
496                 else
497                         ltc_enc_cnt += fptcf/10.0;
498
499                 if (ltc_speed >= 0) {
500                         ltc_enc_byte = (ltc_enc_byte + 1)%10;
501                         if (ltc_enc_byte == 0 && ltc_speed != 0) {
502                                 ltc_encoder_inc_timecode(ltc_encoder);
503                                 ltc_tx_recalculate_position();
504                                 ltc_enc_cnt = 0;
505                         } else if (ltc_enc_byte == 0) {
506                                 ltc_enc_cnt = 0;
507                         }
508                 }
509 #ifdef LTC_GEN_FRAMEDBUG
510                 DEBUG_TRACE (DEBUG::LTC, string_compose("LTC TX6.4 enc-pos: %1 + %2 [ %4 / %5 ] spd %6\n", ltc_enc_pos, ltc_enc_cnt, ltc_buf_off, ltc_buf_len, ltc_speed));
511 #endif
512         }
513
514         return nframes;
515 }