root/trunk/libffado/src/libstreaming/amdtp/AmdtpTransmitStreamProcessor.cpp

Revision 797, 27.3 kB (checked in by ppalmers, 15 years ago)

parameters for better latency performance

Line 
1 /*
2  * Copyright (C) 2005-2007 by Pieter Palmers
3  *
4  * This file is part of FFADO
5  * FFADO = Free Firewire (pro-)audio drivers for linux
6  *
7  * FFADO is based upon FreeBoB.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 #include "config.h"
25 #include "AmdtpTransmitStreamProcessor.h"
26 #include "AmdtpPort.h"
27 #include "../StreamProcessorManager.h"
28 #include "devicemanager.h"
29
30 #include "libutil/Time.h"
31
32 #include "libieee1394/ieee1394service.h"
33 #include "libieee1394/IsoHandlerManager.h"
34 #include "libieee1394/cycletimer.h"
35
36 #include <netinet/in.h>
37 #include <assert.h>
38
39 namespace Streaming
40 {
41
42 /* transmit */
43 AmdtpTransmitStreamProcessor::AmdtpTransmitStreamProcessor(FFADODevice &parent, int dimension)
44         : StreamProcessor(parent, ePT_Transmit)
45         , m_dimension( dimension )
46         , m_dbc( 0 )
47 {}
48
49 enum StreamProcessor::eChildReturnValue
50 AmdtpTransmitStreamProcessor::generatePacketHeader (
51     unsigned char *data, unsigned int *length,
52     unsigned char *tag, unsigned char *sy,
53     int cycle, unsigned int dropped, unsigned int max_length )
54 {
55     struct iec61883_packet *packet = ( struct iec61883_packet * ) data;
56     /* Our node ID can change after a bus reset, so it is best to fetch
57     * our node ID for each packet. */
58     packet->sid = m_1394service.getLocalNodeId() & 0x3f;
59
60     packet->dbs = m_dimension;
61     packet->fn = 0;
62     packet->qpc = 0;
63     packet->sph = 0;
64     packet->reserved = 0;
65     packet->dbc = m_dbc;
66     packet->eoh1 = 2;
67     packet->fmt = IEC61883_FMT_AMDTP;
68
69     *tag = IEC61883_TAG_WITH_CIP;
70     *sy = 0;
71
72     signed int fc;
73     uint64_t presentation_time;
74     unsigned int presentation_cycle;
75     int cycles_until_presentation;
76
77     uint64_t transmit_at_time;
78     unsigned int transmit_at_cycle;
79     int cycles_until_transmit;
80
81     // FIXME: should become a define
82     // the absolute minimum number of cycles we want to transmit
83     // a packet ahead of the presentation time. The nominal time
84     // the packet is transmitted ahead of the presentation time is
85     // given by AMDTP_TRANSMIT_TRANSFER_DELAY (in ticks), but in case we
86     // are too late for that, this constant defines how late we can
87     // be.
88     const int min_cycles_before_presentation = 1;
89     // FIXME: should become a define
90     // the absolute maximum number of cycles we want to transmit
91     // a packet ahead of the ideal transmit time. The nominal time
92     // the packet is transmitted ahead of the presentation time is
93     // given by AMDTP_TRANSMIT_TRANSFER_DELAY (in ticks), but we can send
94     // packets early if we want to. (not completely according to spec)
95     const int max_cycles_to_transmit_early = 2;
96
97     debugOutput ( DEBUG_LEVEL_ULTRA_VERBOSE, "Try for cycle %d\n", cycle );
98     // check whether the packet buffer has packets for us to send.
99     // the base timestamp is the one of the next sample in the buffer
100     ffado_timestamp_t ts_head_tmp;
101     m_data_buffer->getBufferHeadTimestamp ( &ts_head_tmp, &fc ); // thread safe
102
103     // the timestamp gives us the time at which we want the sample block
104     // to be output by the device
105     presentation_time = ( uint64_t ) ts_head_tmp;
106     m_last_timestamp = presentation_time;
107
108     // now we calculate the time when we have to transmit the sample block
109     transmit_at_time = substractTicks ( presentation_time, AMDTP_TRANSMIT_TRANSFER_DELAY );
110
111     // calculate the cycle this block should be presented in
112     // (this is just a virtual calculation since at that time it should
113     //  already be in the device's buffer)
114     presentation_cycle = ( unsigned int ) ( TICKS_TO_CYCLES ( presentation_time ) );
115
116     // calculate the cycle this block should be transmitted in
117     transmit_at_cycle = ( unsigned int ) ( TICKS_TO_CYCLES ( transmit_at_time ) );
118
119     // we can check whether this cycle is within the 'window' we have
120     // to send this packet.
121     // first calculate the number of cycles left before presentation time
122     cycles_until_presentation = diffCycles ( presentation_cycle, cycle );
123
124     // we can check whether this cycle is within the 'window' we have
125     // to send this packet.
126     // first calculate the number of cycles left before presentation time
127     cycles_until_transmit = diffCycles ( transmit_at_cycle, cycle );
128
129     if (dropped) {
130         debugOutput ( DEBUG_LEVEL_VERBOSE,
131                     "Gen HDR: CY=%04u, TC=%04u, CUT=%04d, TST=%011llu (%04u), TSP=%011llu (%04u)\n",
132                     cycle,
133                     transmit_at_cycle, cycles_until_transmit,
134                     transmit_at_time, ( unsigned int ) TICKS_TO_CYCLES ( transmit_at_time ),
135                     presentation_time, ( unsigned int ) TICKS_TO_CYCLES ( presentation_time ) );
136     }
137     // two different options:
138     // 1) there are not enough frames for one packet
139     //      => determine wether this is a problem, since we might still
140     //         have some time to send it
141     // 2) there are enough packets
142     //      => determine whether we have to send them in this packet
143     if ( fc < ( signed int ) m_syt_interval )
144     {
145         // not enough frames in the buffer,
146
147         // we can still postpone the queueing of the packets
148         // if we are far enough ahead of the presentation time
149         if ( cycles_until_presentation <= min_cycles_before_presentation )
150         {
151             debugOutput ( DEBUG_LEVEL_VERBOSE,
152                         "Insufficient frames (P): N=%02d, CY=%04u, TC=%04u, CUT=%04d\n",
153                         fc, cycle, transmit_at_cycle, cycles_until_transmit );
154             // we are too late
155             return eCRV_XRun;
156         }
157         else
158         {
159             unsigned int now_cycle = ( unsigned int ) ( TICKS_TO_CYCLES ( m_1394service.getCycleTimerTicks() ) );
160
161             debugOutput ( DEBUG_LEVEL_VERBOSE,
162                         "Insufficient frames (NP): N=%02d, CY=%04u, TC=%04u, CUT=%04d, NOW=%04d\n",
163                         fc, cycle, transmit_at_cycle, cycles_until_transmit, now_cycle );
164             debugWarning("Insufficient frames (NP): N=%02d, CY=%04u, TC=%04u, CUT=%04d, NOW=%04d\n",
165                          fc, cycle, transmit_at_cycle, cycles_until_transmit, now_cycle );
166
167             // there is still time left to send the packet
168             // we want the system to give this packet another go at a later time instant
169             return eCRV_Again; // note that the raw1394 again system doesn't work as expected
170
171             // we could wait here for a certain time before trying again. However, this
172             // is not going to work since we then block the iterator thread, hence also
173             // the receiving code, meaning that we are not processing received packets,
174             // and hence there is no progression in the number of frames available.
175
176             // for example:
177             // SleepRelativeUsec(125); // one cycle
178             // goto try_block_of_frames;
179
180             // or more advanced, calculate how many cycles we are ahead of 'now' and
181             // base the sleep on that.
182
183             // note that this requires that there is one thread for each IsoHandler,
184             // otherwise we're in the deadlock described above.
185         }
186     }
187     else
188     {
189         // there are enough frames, so check the time they are intended for
190         // all frames have a certain 'time window' in which they can be sent
191         // this corresponds to the range of the timestamp mechanism:
192         // we can send a packet 15 cycles in advance of the 'presentation time'
193         // in theory we can send the packet up till one cycle before the presentation time,
194         // however this is not very smart.
195
196         // There are 3 options:
197         // 1) the frame block is too early
198         //      => send an empty packet
199         // 2) the frame block is within the window
200         //      => send it
201         // 3) the frame block is too late
202         //      => discard (and raise xrun?)
203         //         get next block of frames and repeat
204
205         if(cycles_until_transmit < 0)
206         {
207             // we are too late
208             debugOutput(DEBUG_LEVEL_VERBOSE,
209                         "Too late: CY=%04u, TC=%04u, CUT=%04d, TSP=%011llu (%04u)\n",
210                         cycle,
211                         transmit_at_cycle, cycles_until_transmit,
212                         presentation_time, (unsigned int)TICKS_TO_CYCLES(presentation_time) );
213             //debugShowBackLogLines(200);
214 //             // however, if we can send this sufficiently before the presentation
215 //             // time, it could be harmless.
216 //             // NOTE: dangerous since the device has no way of reporting that it didn't get
217 //             //       this packet on time.
218 //             if(cycles_until_presentation >= min_cycles_before_presentation)
219 //             {
220 //                 // we are not that late and can still try to transmit the packet
221 //                 m_dbc += fillDataPacketHeader(packet, length, m_last_timestamp);
222 //                 return (fc < (signed)(2*m_syt_interval) ? eCRV_Defer : eCRV_Packet);
223 //             }
224 //             else   // definitely too late
225 //             {
226                 return eCRV_XRun;
227 //             }
228         }
229         else if(cycles_until_transmit <= max_cycles_to_transmit_early)
230         {
231             // it's time send the packet
232             m_dbc += fillDataPacketHeader(packet, length, m_last_timestamp);
233             return (fc < (signed)(2*m_syt_interval) ? eCRV_Defer : eCRV_Packet);
234         }
235         else
236         {
237             debugOutput ( DEBUG_LEVEL_VERY_VERBOSE,
238                         "Too early: CY=%04u, TC=%04u, CUT=%04d, TST=%011llu (%04u), TSP=%011llu (%04u)\n",
239                         cycle,
240                         transmit_at_cycle, cycles_until_transmit,
241                         transmit_at_time, ( unsigned int ) TICKS_TO_CYCLES ( transmit_at_time ),
242                         presentation_time, ( unsigned int ) TICKS_TO_CYCLES ( presentation_time ) );
243 #ifdef DEBUG
244             if ( cycles_until_transmit > max_cycles_to_transmit_early + 1 )
245             {
246                 debugOutput ( DEBUG_LEVEL_VERY_VERBOSE,
247                             "Way too early: CY=%04u, TC=%04u, CUT=%04d, TST=%011llu (%04u), TSP=%011llu (%04u)\n",
248                             cycle,
249                             transmit_at_cycle, cycles_until_transmit,
250                             transmit_at_time, ( unsigned int ) TICKS_TO_CYCLES ( transmit_at_time ),
251                             presentation_time, ( unsigned int ) TICKS_TO_CYCLES ( presentation_time ) );
252             }
253 #endif
254             // we are too early, send only an empty packet
255             return eCRV_EmptyPacket;
256         }
257     }
258     return eCRV_Invalid;
259 }
260
261 enum StreamProcessor::eChildReturnValue
262 AmdtpTransmitStreamProcessor::generatePacketData (
263     unsigned char *data, unsigned int *length,
264     unsigned char *tag, unsigned char *sy,
265     int cycle, unsigned int dropped, unsigned int max_length )
266 {
267     struct iec61883_packet *packet = ( struct iec61883_packet * ) data;
268     if ( m_data_buffer->readFrames ( m_syt_interval, ( char * ) ( data + 8 ) ) )
269     {
270         // process all ports that should be handled on a per-packet base
271         // this is MIDI for AMDTP (due to the need of DBC)
272         if ( !encodePacketPorts ( ( quadlet_t * ) ( data+8 ), m_syt_interval, packet->dbc ) )
273         {
274             debugWarning ( "Problem encoding Packet Ports\n" );
275         }
276         debugOutput ( DEBUG_LEVEL_ULTRA_VERBOSE, "XMIT DATA (cy %04d): TSP=%011llu (%04u)\n",
277                     cycle, m_last_timestamp, ( unsigned int ) TICKS_TO_CYCLES ( m_last_timestamp ) );
278         return eCRV_OK;
279     }
280     else return eCRV_XRun;
281
282 }
283
284 enum StreamProcessor::eChildReturnValue
285 AmdtpTransmitStreamProcessor::generateSilentPacketHeader (
286     unsigned char *data, unsigned int *length,
287     unsigned char *tag, unsigned char *sy,
288     int cycle, unsigned int dropped, unsigned int max_length )
289 {
290     struct iec61883_packet *packet = ( struct iec61883_packet * ) data;
291     debugOutput ( DEBUG_LEVEL_ULTRA_VERBOSE, "XMIT NONE (cy %04d): CY=%04u, TSP=%011llu (%04u)\n",
292                 cycle, m_last_timestamp, ( unsigned int ) TICKS_TO_CYCLES ( m_last_timestamp ) );
293
294     /* Our node ID can change after a bus reset, so it is best to fetch
295     * our node ID for each packet. */
296     packet->sid = m_1394service.getLocalNodeId() & 0x3f;
297
298     packet->dbs = m_dimension;
299     packet->fn = 0;
300     packet->qpc = 0;
301     packet->sph = 0;
302     packet->reserved = 0;
303     packet->dbc = m_dbc;
304     packet->eoh1 = 2;
305     packet->fmt = IEC61883_FMT_AMDTP;
306
307     *tag = IEC61883_TAG_WITH_CIP;
308     *sy = 0;
309
310     m_dbc += fillNoDataPacketHeader ( packet, length );
311     return eCRV_OK;
312 }
313
314 enum StreamProcessor::eChildReturnValue
315 AmdtpTransmitStreamProcessor::generateSilentPacketData (
316     unsigned char *data, unsigned int *length,
317     unsigned char *tag, unsigned char *sy,
318     int cycle, unsigned int dropped, unsigned int max_length )
319 {
320     return eCRV_OK; // no need to do anything
321 }
322
323 unsigned int AmdtpTransmitStreamProcessor::fillDataPacketHeader (
324     struct iec61883_packet *packet, unsigned int* length,
325     uint32_t ts )
326 {
327
328     packet->fdf = m_fdf;
329
330     // convert the timestamp to SYT format
331     uint16_t timestamp_SYT = TICKS_TO_SYT ( ts );
332     packet->syt = ntohs ( timestamp_SYT );
333
334     *length = m_syt_interval*sizeof ( quadlet_t ) *m_dimension + 8;
335
336     return m_syt_interval;
337 }
338
339 unsigned int AmdtpTransmitStreamProcessor::fillNoDataPacketHeader (
340     struct iec61883_packet *packet, unsigned int* length )
341 {
342
343     // no-data packets have syt=0xFFFF
344     // and have the usual amount of events as dummy data (?)
345     packet->fdf = IEC61883_FDF_NODATA;
346     packet->syt = 0xffff;
347
348     // FIXME: either make this a setting or choose
349     bool send_payload=true;
350     if ( send_payload )
351     {
352         // this means no-data packets with payload (DICE doesn't like that)
353         *length = 2*sizeof ( quadlet_t ) + m_syt_interval * m_dimension * sizeof ( quadlet_t );
354         return m_syt_interval;
355     }
356     else
357     {
358         // dbc is not incremented
359         // this means no-data packets without payload
360         *length = 2*sizeof ( quadlet_t );
361         return 0;
362     }
363 }
364
365 unsigned int
366 AmdtpTransmitStreamProcessor::getSytInterval() {
367     switch (m_StreamProcessorManager.getNominalRate()) {
368         case 32000:
369         case 44100:
370         case 48000:
371             return 8;
372         case 88200:
373         case 96000:
374             return 16;
375         case 176400:
376         case 192000:
377             return 32;
378         default:
379             debugError("Unsupported rate: %d\n", m_StreamProcessorManager.getNominalRate());
380             return 0;
381     }
382 }
383 unsigned int
384 AmdtpTransmitStreamProcessor::getFDF() {
385     switch (m_StreamProcessorManager.getNominalRate()) {
386         case 32000: return IEC61883_FDF_SFC_32KHZ;
387         case 44100: return IEC61883_FDF_SFC_44K1HZ;
388         case 48000: return IEC61883_FDF_SFC_48KHZ;
389         case 88200: return IEC61883_FDF_SFC_88K2HZ;
390         case 96000: return IEC61883_FDF_SFC_96KHZ;
391         case 176400: return IEC61883_FDF_SFC_176K4HZ;
392         case 192000: return IEC61883_FDF_SFC_192KHZ;
393         default:
394             debugError("Unsupported rate: %d\n", m_StreamProcessorManager.getNominalRate());
395             return 0;
396     }
397 }
398
399 bool AmdtpTransmitStreamProcessor::prepareChild()
400 {
401     debugOutput ( DEBUG_LEVEL_VERBOSE, "Preparing (%p)...\n", this );
402     m_syt_interval = getSytInterval();
403     m_fdf = getFDF();
404
405     iec61883_cip_init (
406         &m_cip_status,
407         IEC61883_FMT_AMDTP,
408         m_fdf,
409         m_StreamProcessorManager.getNominalRate(),
410         m_dimension,
411         m_syt_interval );
412
413     for ( PortVectorIterator it = m_Ports.begin();
414             it != m_Ports.end();
415             ++it )
416     {
417         if ( ( *it )->getPortType() == Port::E_Midi )
418         {
419             // we use a timing unit of 10ns
420             // this makes sure that for the max syt interval
421             // we don't have rounding, and keeps the numbers low
422             // we have 1 slot every 8 events
423             // we have syt_interval events per packet
424             // => syt_interval/8 slots per packet
425             // packet rate is 8000pkt/sec => interval=125us
426             // so the slot interval is (1/8000)/(syt_interval/8)
427             // or: 1/(1000 * syt_interval) sec
428             // which is 1e9/(1000*syt_interval) nsec
429             // or 100000/syt_interval 'units'
430             // the event interval is fixed to 320us = 32000 'units'
431             if ( ! ( *it )->useRateControl ( true, ( 100000/m_syt_interval ),32000, false ) )
432             {
433                 debugFatal ( "Could not set signal type to PeriodSignalling" );
434                 return false;
435             }
436             break;
437         }
438     }
439     return true;
440 }
441
442 /*
443 * compose the event streams for the packets from the port buffers
444 */
445 bool AmdtpTransmitStreamProcessor::processWriteBlock ( char *data,
446         unsigned int nevents, unsigned int offset )
447 {
448     bool no_problem = true;
449
450     for ( PortVectorIterator it = m_PeriodPorts.begin();
451           it != m_PeriodPorts.end();
452           ++it )
453     {
454         if ( (*it)->isDisabled() ) { continue; };
455
456         //FIXME: make this into a static_cast when not DEBUG?
457         AmdtpPortInfo *pinfo = dynamic_cast<AmdtpPortInfo *> ( *it );
458         assert ( pinfo ); // this should not fail!!
459
460         switch( pinfo->getFormat() )
461         {
462             case AmdtpPortInfo::E_MBLA:
463                 if( encodePortToMBLAEvents(static_cast<AmdtpAudioPort *>(*it), (quadlet_t *)data, offset, nevents) )
464                 {
465                     debugWarning ( "Could not encode port %s to MBLA events", (*it)->getName().c_str() );
466                     no_problem = false;
467                 }
468                 break;
469             case AmdtpPortInfo::E_SPDIF: // still unimplemented
470                 break;
471             default: // ignore
472                 break;
473         }
474     }
475     return no_problem;
476 }
477
478 bool
479 AmdtpTransmitStreamProcessor::transmitSilenceBlock(
480     char *data, unsigned int nevents, unsigned int offset)
481 {
482     bool no_problem = true;
483     for(PortVectorIterator it = m_PeriodPorts.begin();
484         it != m_PeriodPorts.end();
485         ++it )
486     {
487         //FIXME: make this into a static_cast when not DEBUG?
488         AmdtpPortInfo *pinfo=dynamic_cast<AmdtpPortInfo *>(*it);
489         assert(pinfo); // this should not fail!!
490
491         switch( pinfo->getFormat() )
492         {
493             case AmdtpPortInfo::E_MBLA:
494                 if ( encodeSilencePortToMBLAEvents(static_cast<AmdtpAudioPort *>(*it), (quadlet_t *)data, offset, nevents) )
495                 {
496                     debugWarning("Could not encode port %s to MBLA events", (*it)->getName().c_str());
497                     no_problem = false;
498                 }
499                 break;
500             case AmdtpPortInfo::E_SPDIF: // still unimplemented
501                 break;
502             default: // ignore
503                 break;
504         }
505     }
506     return no_problem;
507 }
508
509 /**
510 * @brief decode a packet for the packet-based ports
511 *
512 * @param data Packet data
513 * @param nevents number of events in data (including events of other ports & port types)
514 * @param dbc DataBlockCount value for this packet
515 * @return true if all successfull
516 */
517 bool AmdtpTransmitStreamProcessor::encodePacketPorts ( quadlet_t *data, unsigned int nevents, unsigned int dbc )
518 {
519     bool ok=true;
520     quadlet_t byte;
521
522     quadlet_t *target_event=NULL;
523     unsigned int j;
524
525     for ( PortVectorIterator it = m_PacketPorts.begin();
526             it != m_PacketPorts.end();
527             ++it )
528     {
529
530 #ifdef DEBUG
531         AmdtpPortInfo *pinfo=dynamic_cast<AmdtpPortInfo *> ( *it );
532         assert ( pinfo ); // this should not fail!!
533
534         // the only packet type of events for AMDTP is MIDI in mbla
535         assert ( pinfo->getFormat() ==AmdtpPortInfo::E_Midi );
536 #endif
537
538         AmdtpMidiPort *mp=static_cast<AmdtpMidiPort *> ( *it );
539
540         // we encode this directly (no function call) due to the high frequency
541         /* idea:
542         spec says: current_midi_port=(dbc+j)%8;
543         => if we start at (dbc+stream->location-1)%8,
544         we'll start at the right event for the midi port.
545         => if we increment j with 8, we stay at the right event.
546         */
547         // FIXME: as we know in advance how big a packet is (syt_interval) we can
548         //        predict how much loops will be present here
549         // first prefill the buffer with NO_DATA's on all time muxed channels
550
551         for ( j = ( dbc & 0x07 ) +mp->getLocation(); j < nevents; j += 8 )
552         {
553
554             quadlet_t tmpval;
555
556             target_event= ( quadlet_t * ) ( data + ( ( j * m_dimension ) + mp->getPosition() ) );
557
558             if ( mp->canRead() )   // we can send a byte
559             {
560                 mp->readEvent ( &byte );
561                 byte &= 0xFF;
562                 tmpval=htonl (
563                         IEC61883_AM824_SET_LABEL ( ( byte ) <<16,
564                                                     IEC61883_AM824_LABEL_MIDI_1X ) );
565
566                 debugOutput ( DEBUG_LEVEL_ULTRA_VERBOSE, "MIDI port %s, pos=%d, loc=%d, dbc=%d, nevents=%d, dim=%d\n",
567                             mp->getName().c_str(), mp->getPosition(), mp->getLocation(), dbc, nevents, m_dimension );
568                 debugOutput ( DEBUG_LEVEL_ULTRA_VERBOSE, "base=%p, target=%p, value=%08X\n",
569                             data, target_event, tmpval );
570
571             }
572             else
573             {
574                 // can't send a byte, either because there is no byte,
575                 // or because this would exceed the maximum rate
576                 tmpval=htonl (
577                         IEC61883_AM824_SET_LABEL ( 0,IEC61883_AM824_LABEL_MIDI_NO_DATA ) );
578             }
579
580             *target_event=tmpval;
581         }
582
583     }
584     return ok;
585 }
586
587 #if USE_SSE
588 typedef float v4sf __attribute__ ((vector_size (16)));
589 typedef int v4si __attribute__ ((vector_size (16)));
590 typedef int v2si __attribute__ ((vector_size (8)));
591
592 int AmdtpTransmitStreamProcessor::encodePortToMBLAEvents ( AmdtpAudioPort *p, quadlet_t *data,
593         unsigned int offset, unsigned int nevents )
594 {
595     static const float sse_multiplier[4] __attribute__((aligned(16))) = {
596         (float)(0x7FFFFF00),
597         (float)(0x7FFFFF00),
598         (float)(0x7FFFFF00),
599         (float)(0x7FFFFF00)
600     };
601
602     static const int sse_mask[4] __attribute__((aligned(16))) = {
603         0x40000000,  0x40000000,  0x40000000,  0x40000000
604     };
605
606     unsigned int out[4];
607
608     unsigned int j=0;
609     unsigned int read=0;
610
611     quadlet_t *target_event;
612
613     target_event= ( quadlet_t * ) ( data + p->getPosition() );
614
615     switch ( p->getDataType() )
616     {
617         default:
618         case Port::E_Int24:
619         {
620             quadlet_t *buffer= ( quadlet_t * ) ( p->getBufferAddress() );
621
622             assert ( nevents + offset <= p->getBufferSize() );
623
624             buffer+=offset;
625
626             for ( j = 0; j < nevents; j += 1 )   // decode max nsamples
627             {
628                 *target_event = htonl ( ( * ( buffer ) & 0x00FFFFFF ) | 0x40000000 );
629                 buffer++;
630                 target_event += m_dimension;
631             }
632         }
633         break;
634         case Port::E_Float:
635         {
636             const float multiplier = ( float ) ( 0x7FFFFF00 );
637             float *buffer= ( float * ) ( p->getBufferAddress() );
638
639             assert ( nevents + offset <= p->getBufferSize() );
640
641             buffer+=offset;
642
643             j=0;
644             if(read>3) {
645                 for (j = 0; j < read-3; j += 4) {
646                     asm("movups %[floatbuff], %%xmm0\n\t"
647                             "mulps %[ssemult], %%xmm0\n\t"
648                             "cvttps2pi %%xmm0, %[out1]\n\t"
649                             "movhlps %%xmm0, %%xmm0\n\t"
650                             "psrld $8, %[out1]\n\t"
651                             "cvttps2pi %%xmm0, %[out2]\n\t"
652                             "por %[mmxmask], %[out1]\n\t"
653                             "psrld $8, %[out2]\n\t"
654                             "por %[mmxmask], %[out2]\n\t"
655                         : [out1] "=&y" (*(v2si*)&out[0]),
656                     [out2] "=&y" (*(v2si*)&out[2])
657                         : [floatbuff] "m" (*(v4sf*)buffer),
658                     [ssemult] "x" (*(v4sf*)sse_multiplier),
659                     [mmxmask] "y" (*(v2si*)sse_mask)
660                         : "xmm0");
661                     buffer += 4;
662                     *target_event = htonl(out[0]);
663                     target_event += m_dimension;
664                     *target_event = htonl(out[1]);
665                     target_event += m_dimension;
666                     *target_event = htonl(out[2]);
667                     target_event += m_dimension;
668                     *target_event = htonl(out[3]);
669                     target_event += m_dimension;
670                 }
671             }
672             for(; j < read; ++j) {
673             // don't care for overflow
674                 float v = *buffer * multiplier;  // v: -231 .. 231
675                 unsigned int tmp = (int)v;
676                 *target_event = htonl((tmp >> 8) | 0x40000000);
677    
678                 buffer++;
679                 target_event += m_dimension;
680             }
681
682             asm volatile("emms");
683             break;
684         }
685         break;
686     }
687
688     return 0;
689 }
690
691 #else
692
693 int AmdtpTransmitStreamProcessor::encodePortToMBLAEvents ( AmdtpAudioPort *p, quadlet_t *data,
694         unsigned int offset, unsigned int nevents )
695 {
696     unsigned int j=0;
697
698     quadlet_t *target_event;
699
700     target_event= ( quadlet_t * ) ( data + p->getPosition() );
701
702     switch ( p->getDataType() )
703     {
704         default:
705         case Port::E_Int24:
706         {
707             quadlet_t *buffer= ( quadlet_t * ) ( p->getBufferAddress() );
708
709             assert ( nevents + offset <= p->getBufferSize() );
710
711             buffer+=offset;
712
713             for ( j = 0; j < nevents; j += 1 )   // decode max nsamples
714             {
715                 *target_event = htonl ( ( * ( buffer ) & 0x00FFFFFF ) | 0x40000000 );
716                 buffer++;
717                 target_event += m_dimension;
718             }
719         }
720         break;
721         case Port::E_Float:
722         {
723             const float multiplier = ( float ) ( 0x7FFFFF00 );
724             float *buffer= ( float * ) ( p->getBufferAddress() );
725
726             assert ( nevents + offset <= p->getBufferSize() );
727
728             buffer+=offset;
729
730             for ( j = 0; j < nevents; j += 1 )   // decode max nsamples
731             {
732
733                 // don't care for overflow
734                 float v = *buffer * multiplier;  // v: -231 .. 231
735                 unsigned int tmp = ( ( int ) v );
736                 *target_event = htonl ( ( tmp >> 8 ) | 0x40000000 );
737
738                 buffer++;
739                 target_event += m_dimension;
740             }
741         }
742         break;
743     }
744
745     return 0;
746 }
747 #endif
748
749 int AmdtpTransmitStreamProcessor::encodeSilencePortToMBLAEvents ( AmdtpAudioPort *p, quadlet_t *data,
750         unsigned int offset, unsigned int nevents )
751 {
752     unsigned int j=0;
753
754     quadlet_t *target_event;
755
756     target_event= ( quadlet_t * ) ( data + p->getPosition() );
757
758     switch ( p->getDataType() )
759     {
760         default:
761         case Port::E_Int24:
762         case Port::E_Float:
763         {
764             for ( j = 0; j < nevents; j += 1 )   // decode max nsamples
765             {
766                 *target_event = htonl ( 0x40000000 );
767                 target_event += m_dimension;
768             }
769         }
770         break;
771     }
772
773     return 0;
774 }
775
776 } // end of namespace Streaming
Note: See TracBrowser for help on using the browser.