root/trunk/libffado/src/libstreaming/rme/RmeReceiveStreamProcessor.cpp

Revision 1965, 16.6 kB (checked in by jwoithe, 13 years ago)

- RME: further tweaks to the streaming system to make it more reliable (finally some progress is being made).
- Generic: reset timestamps in StreamProcessor::doDryRunning() in case the stream processor has previously been running (eg: before an xrun). Timestamps acquired during a previous run won't apply once the processor starts again. This change makes sense to me and helps xrun recovery on RME devices but there may be subtleties I don't see - so please double check the reasoning and behaviour with other interfaces.

Line 
1 /*
2  * Copyright (C) 2005-2009 by Jonathan Woithe
3  * Copyright (C) 2005-2008 by Pieter Palmers
4  *
5  * This file is part of FFADO
6  * FFADO = Free Firewire (pro-)audio drivers for linux
7  *
8  * FFADO is based upon FreeBoB.
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 2 of the License, or
13  * (at your option) version 3 of the License.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 /* CAUTION: this module is under active development.  It has been templated   
26  * from the MOTU driver and many MOTU-specific details remain.  Do not use
27  * this file as a reference for RME devices until initial development has     
28  * been completed.
29   */
30
31 #include "libutil/float_cast.h"
32
33 #include "RmeReceiveStreamProcessor.h"
34 #include "RmePort.h"
35 #include "../StreamProcessorManager.h"
36 #include "devicemanager.h"
37
38 #include "libieee1394/ieee1394service.h"
39 #include "libieee1394/IsoHandlerManager.h"
40 #include "libieee1394/cycletimer.h"
41
42 #include "libutil/ByteSwap.h"
43
44 // This is to pick up the RME_MODEL_* constants.  There's probably a better
45 // way ...
46 #include "../../rme/rme_avdevice.h"
47
48 #include <cstring>
49 #include <math.h>
50 #include <assert.h>
51
52 /* Provide more intuitive access to GCC's branch predition built-ins */
53 #define likely(x)   __builtin_expect((x),1)
54 #define unlikely(x) __builtin_expect((x),0)
55
56
57 namespace Streaming {
58
59 // A macro to extract specific bits from a native endian quadlet
60 #define get_bits(_d,_start,_len) (((_d)>>((_start)-(_len)+1)) & ((1<<(_len))-1))
61
62 RmeReceiveStreamProcessor::RmeReceiveStreamProcessor(FFADODevice &parent,
63   unsigned int model, unsigned int event_size)
64     : StreamProcessor(parent, ePT_Receive)
65     , n_hw_tx_buffer_samples ( -1 )
66     , m_rme_model( model )
67     , m_event_size( event_size )
68     , rxdll_t1( 0 )
69     , rxdll_e2( 0 )
70     , mb_head ( 0 )
71     , mb_tail ( 0 )
72 {
73 }
74
75 unsigned int
76 RmeReceiveStreamProcessor::getMaxPacketSize() {
77     int framerate = m_Parent.getDeviceManager().getStreamProcessorManager().getNominalRate();
78     // FIXME: the additional 8 bytes is not needed.
79     // FIXME: the upper bounds of the 1x and 2x rates need to account for the
80     //   DDS capability to run fast by 4%.
81     if (m_rme_model == Rme::RME_MODEL_FIREFACE800)
82         return 8 + (framerate<=48000?784:(framerate<=96000?1200:1200));
83     else
84         return 8 + (framerate<=48000?504:(framerate<=96000?840:1000));
85 }
86
87 unsigned int
88 RmeReceiveStreamProcessor::getNominalFramesPerPacket() {
89     int framerate = m_Parent.getDeviceManager().getStreamProcessorManager().getNominalRate();
90     return framerate<=48000?7:(framerate<=96000?15:25);
91 }
92
93 #define RXDLL_BANDWIDTH (0.003)
94
95 bool
96 RmeReceiveStreamProcessor::prepareChild() {
97     double w;
98     debugOutput( DEBUG_LEVEL_VERBOSE, "Preparing (%p)...\n", this);
99
100     // prepare the framerate estimate
101     // FIXME: not needed anymore?
102     //m_ticks_per_frame = (TICKS_PER_SECOND*1.0) / ((float)m_Parent.getDeviceManager().getStreamProcessorManager().getNominalRate());
103
104     // Initialise the "smoothing" DLL.  rxdll_e2 is set to the expected
105     // period (in ticks) which is then used to set suitable coefficients
106     // based on a normalised bandwidth.
107     rxdll_t1 = -1.0;
108     rxdll_e2 = (TICKS_PER_SECOND*1.0) / ((float)m_Parent.getDeviceManager().getStreamProcessorManager().getNominalRate());
109 //    w = (2*M_PI*RXDLL_BANDWIDTH*rxdll_e2);
110 //w = (2*M_PI*0.004);
111 //w = (2*M_PI*0.00225);
112 w = (2*M_PI*0.001);
113     rxdll_B = (sqrt(2.0)*w);
114     rxdll_C = (w*w);
115
116 debugOutput( DEBUG_LEVEL_VERBOSE, "init: e2=%g, w=%g, B=%g, C=%g\n",
117   rxdll_e2, w, rxdll_B, rxdll_C);
118
119     // Request that the iso streaming be started as soon as possible by the
120     // kernel.
121     m_IsoHandlerManager.setIsoStartCycleForStream(this, -1);
122
123     return true;
124 }
125
126 /*
127  * Processes packet header to extract timestamps and check if the packet is
128  * valid.
129  */
130 enum StreamProcessor::eChildReturnValue
131 RmeReceiveStreamProcessor::processPacketHeader(unsigned char *data, unsigned int length,
132                                                 unsigned char tag, unsigned char sy,
133                                                 uint32_t pkt_ctr)
134 {
135 // For testing
136 static signed int rep = 0;
137 static unsigned long long int prevts = 0;
138 quadlet_t *adata = (quadlet_t *)data;
139 if (rep == 0) {
140 //  debugOutput(DEBUG_LEVEL_VERBOSE, "data packet header, len=%d\n", length);
141   fprintf(stderr, "first data packet header, len=%d\n", length);
142 }
143 //fprintf(stderr, "recv len=%d\n", length);
144
145     if (length > 0) {
146         // The iso data blocks from the RMEs comprise 24-bit audio
147         // data encoded in 32-bit integers.  The LSB of the 32-bit integers
148         // of certain channels are used for house-keeping information.
149         // The number of samples for each channel present in a packet
150         // varies: 7 for 1x rates, 15 for 2x rates and 25 for 4x rates.
151         // quadlet_t *quadlet = (quadlet_t *)data;
152
153         // Don't even attempt to process a packet if it isn't what we expect
154         // from an RME.  For now the only condition seems to be a tag of 0.
155         // This will be fleshed out in due course.
156 //        if (tag!=1) {
157 //            return eCRV_Invalid;
158 //        }
159
160         // Timestamps are not transmitted explicitly by the RME interfaces
161         // so we'll have to fake it somehow in order to fit in with the rest
162         // of the FFADO infrastructure.  For now just take the packet
163         // arrival time as the "last timestamp" and feed it into a local
164         // DLL to "smooth over" the abrupt jumps which would otherwise be
165         // associated with a skipped iso cycle.  In practice there is a
166         // fixed offset that we'll have to include eventually.
167         uint64_t pkt_ctr_ticks = CYCLE_TIMER_TO_TICKS(pkt_ctr);
168         double e = pkt_ctr_ticks - rxdll_t1;
169         if (e < -64LL*TICKS_PER_SECOND)
170           e += 128LL*TICKS_PER_SECOND;
171 //        if (e < 0)
172 //          e += 128LL*TICKS_PER_SECOND;
173
174 // Very large e values indicate a discontinuity in processing, possibly due
175 // to an xrun.  In this case, reset the DLL to avoid long delays as it
176 // resynchronises.
177 if (e > 10000) {
178   rxdll_t1 = -1.0;
179   rxdll_e2 = (TICKS_PER_SECOND*1.0) / ((float)m_Parent.getDeviceManager().getStreamProcessorManager().getNominalRate());
180 }
181
182 int64_t newts=0;
183 #if 0
184 double p = m_last_timestamp;
185 debugOutput(DEBUG_LEVEL_VERBOSE, "ts read: %lld, prev=%lld, diff=%lld\n",
186   pkt_ctr_ticks, prevts, pkt_ctr_ticks-prevts);
187 debugOutput(DEBUG_LEVEL_VERBOSE, "  rxdll_t1=%g\n", rxdll_t1);
188 #endif
189
190         if (rxdll_t1 < 0.0) {
191             signed int n_frames = length / m_event_size;
192             rxdll_e2 *= n_frames;
193             rxdll_t1 = pkt_ctr_ticks + rxdll_e2;
194 newts = pkt_ctr_ticks - rxdll_e2 - (0*3072);
195 if (newts < 0)
196   newts += 128LL*TICKS_PER_SECOND;
197 m_data_buffer->setBufferTailTimestamp(newts);
198             newts = pkt_ctr_ticks;
199 //debugOutput(DEBUG_LEVEL_VERBOSE, "  INIT\n");
200         } else {
201             newts = rxdll_t1;
202             rxdll_t1 += rxdll_B*e + rxdll_e2;
203             rxdll_e2 += rxdll_C*e;
204         }
205         if (rxdll_t1 >= 128LL*TICKS_PER_SECOND)
206             rxdll_t1 -= 128LL*TICKS_PER_SECOND;
207
208 //newts += (6.0/7.00)*rxdll_e2;
209 newts -= (0*3072);  // Make there be some sort of latency
210 if (newts < 0)
211   newts += 128LL*TICKS_PER_SECOND;
212 else
213 if (newts >= 128LL*TICKS_PER_SECOND)
214   newts -= 128LL*TICKS_PER_SECOND;
215 #if 1
216 // 3584
217 if (newts-m_last_timestamp > 4000) {
218   debugOutput(DEBUG_LEVEL_VERBOSE, " **** \n");
219 }
220 debugOutput(DEBUG_LEVEL_VERBOSE, "  returned: %lld (e=%g) T=%g, f=%g\n",
221   newts, e, rxdll_e2, 7.0/rxdll_e2*24576000);
222 debugOutput(DEBUG_LEVEL_VERBOSE, "    diff=%lld, f=%g\n",
223   newts-m_last_timestamp, 24576000/((newts-m_last_timestamp)/7.0));
224 debugOutput(DEBUG_LEVEL_VERBOSE, "    ts read: %lld, prev=%lld, diff=%lld\n",
225   pkt_ctr_ticks, prevts, pkt_ctr_ticks-prevts);
226 #endif
227 m_last_timestamp = newts;
228 prevts = pkt_ctr_ticks;
229
230 if (rep == 0) {
231   debugOutput(DEBUG_LEVEL_VERBOSE, "  timestamp: %lld, ct=%08x (%03ld,%04ld,%04ld)\n", m_last_timestamp, pkt_ctr,
232     CYCLE_TIMER_GET_SECS(pkt_ctr), CYCLE_TIMER_GET_CYCLES(pkt_ctr), CYCLE_TIMER_GET_OFFSET(pkt_ctr));
233   debugOutput(DEBUG_LEVEL_VERBOSE, "  %02x %02x %02x %02x %02x %02x %02x %02x\n",
234     adata[0] & 0xff, adata[1] & 0xff, adata[2] & 0xff, adata[3] & 0xff,
235     adata[4] & 0xff, adata[5] & 0xff, adata[6] & 0xff, adata[7] & 0xff);
236   debugOutput(DEBUG_LEVEL_VERBOSE, "  tx size=%d, rxcount=%d\n",
237     ((adata[5] & 0xff) << 8) | (adata[0] & 0xff),
238     ((adata[4] & 0xff) << 8) | (adata[1] & 0xff));
239   n_hw_tx_buffer_samples = adata[7] & 0xff;
240   debugOutput(DEBUG_LEVEL_VERBOSE, "  hw tx: 0x%02x\n", n_hw_tx_buffer_samples);
241 }
242 rep=1;
243         return eCRV_OK;
244     } else {
245         return eCRV_Invalid;
246     }
247 }
248
249 /**
250  * extract the data from the packet
251  * @pre the IEC61883 packet is valid according to isValidPacket
252  * @param data
253  * @param length
254  * @param channel
255  * @param tag
256  * @param sy
257  * @param pkt_ctr
258  * @return
259  */
260 enum StreamProcessor::eChildReturnValue
261 RmeReceiveStreamProcessor::processPacketData(unsigned char *data, unsigned int length) {
262     // m_event_size should never be zero
263     unsigned int n_events = length / m_event_size;
264
265 // for testing
266 static signed int rep = 0;
267
268     // we have to keep in mind that there are also
269     // some packets buffered by the ISO layer,
270     // at most x=m_handler->getWakeupInterval()
271     // these contain at most x*syt_interval
272     // frames, meaning that we might receive
273     // this packet x*syt_interval*ticks_per_frame
274     // later than expected (the real receive time)
275     #ifdef DEBUG
276     if(isRunning()) {
277         debugOutput(DEBUG_LEVEL_VERY_VERBOSE,"STMP: %lluticks | tpf=%f\n",
278             m_last_timestamp, getTicksPerFrame());
279     }
280     #endif
281
282 // For testing
283 if (rep == 0) {
284   debugOutput(DEBUG_LEVEL_VERBOSE, "data packet data, length=%d, ev_size=%d, n_events=%d\n", length, m_event_size, n_events);
285   rep = 1;
286 }
287     if(m_data_buffer->writeFrames(n_events, (char *)data, m_last_timestamp)) {
288         return eCRV_OK;
289     } else {
290         return eCRV_XRun;
291     }
292 }
293
294 /***********************************************
295  * Encoding/Decoding API                       *
296  ***********************************************/
297 /**
298  * \brief write received events to the port ringbuffers.
299  */
300 bool RmeReceiveStreamProcessor::processReadBlock(char *data,
301                        unsigned int nevents, unsigned int offset)
302 {
303     bool no_problem=true;
304
305     for ( PortVectorIterator it = m_Ports.begin();
306           it != m_Ports.end();
307           ++it ) {
308         if((*it)->isDisabled()) {continue;};
309
310         Port *port=(*it);
311
312         switch(port->getPortType()) {
313
314         case Port::E_Audio:
315             if(decodeRmeEventsToPort(static_cast<RmeAudioPort *>(*it), (quadlet_t *)data, offset, nevents)) {
316                 debugWarning("Could not decode packet data to port %s\n",(*it)->getName().c_str());
317                 no_problem=false;
318             }
319             break;
320         case Port::E_Midi:
321              if(decodeRmeMidiEventsToPort(static_cast<RmeMidiPort *>(*it), (quadlet_t *)data, offset, nevents)) {
322                  debugWarning("Could not decode packet midi data to port %s\n",(*it)->getName().c_str());
323                  no_problem=false;
324              }
325             break;
326
327         default: // ignore
328             break;
329         }
330     }
331     return no_problem;
332 }
333
334 signed int RmeReceiveStreamProcessor::decodeRmeEventsToPort(RmeAudioPort *p,
335         quadlet_t *data, unsigned int offset, unsigned int nevents)
336 {
337     unsigned int j=0;
338
339     // For RME interfaces the audio data is contained in the most significant
340     // 24 bits of a 32-bit field.  Thus it makes sense to treat the source
341     // data as 32 bit and simply mask/shift as necessary to isolate the
342     // audio data.
343     quadlet_t *src_data;
344     src_data = data + p->getPosition()/4;
345
346     switch(m_StreamProcessorManager.getAudioDataType()) {
347         default:
348         case StreamProcessorManager::eADT_Int24:
349             {
350                 quadlet_t *buffer=(quadlet_t *)(p->getBufferAddress());
351
352                 assert(nevents + offset <= p->getBufferSize());
353
354                 // Offset is in frames, but each port is only a single
355                 // channel, so the number of frames is the same as the
356                 // number of quadlets to offset (assuming the port buffer
357                 // uses one quadlet per sample, which is the case currently).
358                 buffer+=offset;
359
360                 for(j = 0; j < nevents; j += 1) { // Decode nsamples
361                     *buffer = (*src_data >> 8) & 0x00ffffff;
362                     // Sign-extend highest bit of 24-bit int.  This isn't
363                     // strictly needed since E_Int24 is a 24-bit, but doing
364                     // so shouldn't break anything and makes the data easier
365                     // to deal with during debugging.
366                     if (*src_data & 0x80000000)
367                         *buffer |= 0xff000000;
368
369                     buffer++;
370                     src_data+=m_event_size/4;
371                 }
372             }
373             break;
374         case StreamProcessorManager::eADT_Float:
375             {
376                 const float multiplier = 1.0f / (float)(0x7FFFFF);
377                 float *buffer=(float *)(p->getBufferAddress());
378
379                 assert(nevents + offset <= p->getBufferSize());
380
381                 buffer+=offset;
382
383                 for(j = 0; j < nevents; j += 1) { // decode max nsamples
384                     signed int v = (*src_data >> 8) & 0x00ffffff;
385                     /* Sign-extend highest bit of incoming 24-bit integer */
386                     if (*src_data & 0x80000000)
387                       v |= 0xff000000;
388                     *buffer = v * multiplier;
389                     buffer++;
390                     src_data+=m_event_size/4;
391                 }
392             }
393             break;
394     }
395
396     return 0;
397 }
398
399 int
400 RmeReceiveStreamProcessor::decodeRmeMidiEventsToPort(
401                       RmeMidiPort *p, quadlet_t *data,
402                       unsigned int offset, unsigned int nevents)
403 {
404     unsigned int j = 0;
405     unsigned char *src = NULL;
406
407     quadlet_t *buffer = (quadlet_t *)(p->getBufferAddress());
408     assert(nevents + offset <= p->getBufferSize());
409     buffer += offset;
410
411     // Zero the buffer
412     memset(buffer, 0, nevents*sizeof(*buffer));
413
414     // Get MIDI bytes if present in any frames within the packet.  RME MIDI
415     // data is sent as part of a 3-byte sequence starting at the port's
416     // position.  Some RMEs (eg: the 828MkII) send more than one MIDI byte
417     // in some packets.  Since the FFADO MIDI layer requires a MIDI byte in
418     // only every 8th buffer position we allow for this by buffering the
419     // incoming data.  The buffer is small since it only has to cover for
420     // short-term excursions in the data rate.  Since the MIDI data
421     // originates on a physical MIDI bus the overall data rate is limited by
422     // the baud rate of that bus (31250), which is no more than one byte in
423     // 8 even for 1x sample rates.
424     src = (unsigned char *)data + p->getPosition();
425     // We assume that the buffer has been set up in such a way that the first
426     // element is correctly aligned for FFADOs MIDI layer.  The requirement
427     // is that actual MIDI bytes must be aligned to multiples of 8 samples. 
428
429     while (j < nevents) {
430         /* Most events don't have MIDI data bytes */
431 //        if (unlikely((*src & RME_KEY_MASK_MIDI) == RME_KEY_MASK_MIDI)) {
432         if (0) {
433             // A MIDI byte is in *(src+2).  Bit 24 is used to flag MIDI data
434             // as present once the data makes it to the output buffer.
435             midibuffer[mb_head++] = 0x01000000 | *(src+2);
436             mb_head &= RX_MIDIBUFFER_SIZE-1;
437             if (unlikely(mb_head == mb_tail)) {
438                 debugWarning("RME rx MIDI buffer overflow\n");
439                 /* Dump oldest byte.  This overflow can only happen if the
440                  * rate coming in from the hardware MIDI port grossly
441                  * exceeds the official MIDI baud rate of 31250 bps, so it
442                  * should never occur in practice.
443                  */
444                 mb_tail = (mb_tail + 1) & (RX_MIDIBUFFER_SIZE-1);
445             }
446         }
447         /* Write to the buffer if we're at an 8-sample boundary */
448         if (unlikely(!(j & 0x07))) {
449             if (mb_head != mb_tail) {
450                 *buffer = midibuffer[mb_tail++];
451                 mb_tail &= RX_MIDIBUFFER_SIZE-1;
452             }
453             buffer += 8;
454         }
455         j++;
456         src += m_event_size;
457     }
458
459     return 0;   
460 }
461
462 } // end of namespace Streaming
Note: See TracBrowser for help on using the browser.