Debian Codegraph @

src/amdtp.c

raw
1 /*
2 * libiec61883 - Linux IEEE 1394 streaming media library.
3 * Copyright (C) 2004 Kristian Hogsberg, Dan Dennedy, and Dan Maas.
4 *
5 * This file written by Kristian Hogsberg.
6 * Update receive to use new raw1394 API by Dan Dennedy.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 #include <stdio.h>
27 #include <errno.h>
28 #include <stdlib.h>
29 #include <netinet/in.h>
30 #include <assert.h>
31 #include <string.h>
32
33 #include "iec61883.h"
34 #include "iec61883-private.h"
35
36 #define AMDTP_MAX_PACKET_SIZE 2048
37
38 iec61883_amdtp_t
39 iec61883_amdtp_xmit_init (raw1394handle_t handle,
40 int rate,
41 int format,
42 int sample_format,
43 int mode,
44 int dimension,
45 iec61883_amdtp_xmit_t get_data, void *callback_data)
46 {
47 int fdf, syt_interval;
48 struct iec61883_amdtp *amdtp;
49
50 amdtp = malloc (sizeof (struct iec61883_amdtp));
51 if (!amdtp) {
52 errno = ENOMEM;
53 return NULL;
54 }
55 amdtp->channel = -1;
56
57 if (format <= IEC61883_AMDTP_FORMAT_IEC958_AC3)
58 amdtp->format = format;
59 else {
60 free (amdtp);
61 return NULL;
62 }
63
64 switch (rate) {
65 case 32000:
66 syt_interval = 8;
67 fdf = IEC61883_FDF_SFC_32KHZ;
68 amdtp->iec958_rate_code = 0x0c;
69 break;
70 case 44100:
71 syt_interval = 8;
72 fdf = IEC61883_FDF_SFC_44K1HZ;
73 amdtp->iec958_rate_code = 0x00;
74 break;
75 case 48000:
76 syt_interval = 8;
77 fdf = IEC61883_FDF_SFC_48KHZ;
78 amdtp->iec958_rate_code = 0x04;
79 break;
80 case 88200:
81 syt_interval = 16;
82 fdf = IEC61883_FDF_SFC_88K2HZ;
83 amdtp->iec958_rate_code = 0x00;
84 break;
85 case 96000:
86 syt_interval = 16;
87 fdf = IEC61883_FDF_SFC_96KHZ;
88 amdtp->iec958_rate_code = 0x00;
89 break;
90 case 176400:
91 syt_interval = 32;
92 fdf = IEC61883_FDF_SFC_176K4HZ;
93 amdtp->iec958_rate_code = 0x00;
94 break;
95 case 192000:
96 syt_interval = 32;
97 fdf = IEC61883_FDF_SFC_192KHZ;
98 amdtp->iec958_rate_code = 0x00;
99 break;
100
101 default:
102 free (amdtp);
103 return NULL;
104 }
105
106 /* When using the AM824 raw subformat we can stream signals of
107 * any dimension. The IEC958 subformat, however, only
108 * supports 2 channels.
109 */
110 if (amdtp->format == IEC61883_AMDTP_FORMAT_IEC958_PCM && dimension > 2) {
111 free (amdtp);
112 return NULL;
113 }
114 else {
115 amdtp->dimension = dimension;
116 }
117
118 /* reset framecounter for IEC958 mode */
119 amdtp->iec958_frame_count = 0;
120
121 amdtp->sample_format = sample_format;
122 amdtp->get_data = get_data;
123 amdtp->callback_data = callback_data;
124 amdtp->handle = handle;
125 amdtp->dimension = dimension;
126 amdtp->buffer_packets = 1000;
127 amdtp->prebuffer_packets = 1000;
128 amdtp->irq_interval = 250;
129 amdtp->synch = 0;
130 amdtp->speed = RAW1394_ISO_SPEED_100;
131
132 iec61883_cip_init (&amdtp->cip, IEC61883_FMT_AMDTP, fdf,
133 rate, dimension, syt_interval);
134 iec61883_cip_set_transmission_mode (&amdtp->cip, mode);
135
136 raw1394_set_userdata (handle, amdtp);
137
138 return amdtp;
139 }
140
141 static enum raw1394_iso_disposition
142 amdtp_xmit_handler (raw1394handle_t handle,
143 unsigned char *data, unsigned int *len,
144 unsigned char *tag, unsigned char *sy,
145 int cycle, unsigned int dropped)
146 {
147 struct iec61883_amdtp *amdtp = raw1394_get_userdata (handle);
148 struct iec61883_packet *packet = (struct iec61883_packet *) data;
149 int nevents;
150 quadlet_t *event = (quadlet_t *) packet->data;
151 enum raw1394_iso_disposition result = RAW1394_ISO_OK;
152 int nsamples;
153 int diff_sync;
154
155 assert (amdtp != NULL);
156 amdtp->total_dropped += dropped;
157
158 /* If packets got dropped, we have to resynchronize the generation
159 of SYT timestamps. Otherwise the SYT timestamps would differ
160 too much from current cycle time.
161 Unfortunately, dropped does not tell us how many packets got
162 got dropped. It just tells us that we lost packets.
163 So the only thing we can do is restarting buffering and
164 taking the current cycle count as reference. */
165 if (dropped) {
166 DEBUG ("dropped packets detected.");
167 iec61883_cip_resync(&amdtp->cip, cycle);
168 }
169
170 /* The following is a workaround for a possible bug in the kernel
171 module. It seems that after a dropped packet event, the cycle
172 number passed to this handler is incorrect (does not match the
173 transmission cycle) for some time. Therefore, we check whether
174 the SYT timestamp is in sync with the current cycle count. If
175 we ran out of sync, we resynchronize. */
176 diff_sync = (amdtp->cip.cycle_count - cycle + 8000) % 8000;
177 if (diff_sync > 5) {
178 DEBUG ("lost SYT sync, resynchronizing.");
179 iec61883_cip_resync(&amdtp->cip, cycle);
180 }
181
182 nevents = iec61883_cip_fill_header (handle, &amdtp->cip, packet);
183
184 if (nevents > 0) {
185 nsamples = nevents;
186 }
187 else {
188 if (amdtp->cip.mode == IEC61883_MODE_BLOCKING_EMPTY) {
189 nsamples = 0;
190 }
191 else {
192 nsamples = amdtp->cip.syt_interval;
193 }
194 }
195
196 memset (packet->data, '\0', nsamples * amdtp->dimension * sizeof (quadlet_t));
197
198 if (nevents > 0) {
199 if( amdtp->get_data (amdtp, packet->data, nevents, packet->dbc, dropped,
200 amdtp->callback_data) < 0 ) {
201 result = RAW1394_ISO_ERROR;
202 }
203 }
204
205 if (result == RAW1394_ISO_OK ) {
206 quadlet_t label = 0;
207 int i;
208
209 if (amdtp->format == IEC61883_AMDTP_FORMAT_RAW) {
210 label = IEC61883_AM824_LABEL;
211 switch (amdtp->sample_format) {
212 case IEC61883_AMDTP_INPUT_LE24:
213 label |= IEC61883_AM824_VBL_24BITS;
214 break;
215 case IEC61883_AMDTP_INPUT_LE20:
216 label |= IEC61883_AM824_VBL_20BITS;
217 break;
218 case IEC61883_AMDTP_INPUT_LE16:
219 label |= IEC61883_AM824_VBL_16BITS;
220 break;
221 default:
222 break;
223 }
224 label = label << 24;
225
226 for (i = 0; i < nsamples * amdtp->dimension; i++) {
227 event[i] = htonl (event[i] | label);
228 }
229 }
230 else if (amdtp->format == IEC61883_AMDTP_FORMAT_IEC958_PCM) {
231 struct iec60958_data *sample;
232
233 for (i = 0; i < nsamples * amdtp->dimension; i++) {
234 sample = (struct iec60958_data *) &event[i];
235 sample->label = IEC60958_LABEL;
236 if (nevents == 0) {
237 sample->validity = IEC60958_DATA_INVALID;
238 /* Using reseved value for old
239 * SoftAcoustik SA2.0 speakers. */
240 sample->pac = IEC60958_PAC_RSV;
241 }
242 else {
243 sample->validity = IEC60958_DATA_VALID;
244 if( i % 2 == 0 || amdtp->dimension == 1 ) {
245 /* even --> CHANNEL 1 */
246 if( amdtp->iec958_frame_count == 0 )
247 {
248 sample->pac = IEC60958_PAC_B;
249 }
250 else
251 {
252 sample->pac = IEC60958_PAC_M;
253 }
254 }
255 else {
256 /* Odd --> CHANNEL 2 */
257 sample->pac = IEC60958_PAC_W;
258 }
259 }
260
261 sample->ch_status = 0;
262 /* The parity bit is the xor of the sample bits and the channel
263 status info bit. */
264 int shift;
265 u_int32_t p;
266 for (shift = 16, p = sample->data ^ sample->ch_status; shift > 0; shift >>= 1)
267 {
268 p ^= (p >> shift);
269 }
270 sample->parity = p & 1;
271
272 event[i] = htonl (event[i]);
273
274 /* increase the IEC958 framecounter */
275 if ((i % 2 == 1) || (amdtp->dimension == 1))
276 {
277 amdtp->iec958_frame_count++;
278 if (amdtp->iec958_frame_count >= 192) {
279 amdtp->iec958_frame_count = 0;
280 }
281 }
282 }
283 }
284 else {
285 /* Unsupported format. */
286 result = RAW1394_ISO_ERROR;
287 }
288
289 *len = nsamples * amdtp->dimension * sizeof (quadlet_t) + 8;
290 *tag = IEC61883_TAG_WITH_CIP;
291 *sy = 0;
292 }
293
294 return result;
295 }
296
297 int
298 iec61883_amdtp_xmit_start (struct iec61883_amdtp *amdtp, int channel)
299 {
300 int result = 0;
301 unsigned int max_packet_size;
302
303 assert (amdtp != NULL);
304 max_packet_size = iec61883_cip_get_max_packet_size (&amdtp->cip);
305
306 result = raw1394_iso_xmit_init (amdtp->handle, amdtp_xmit_handler,
307 amdtp->buffer_packets,
308 max_packet_size, channel,
309 amdtp->speed, amdtp->irq_interval);
310 if (result == 0) {
311 amdtp->total_dropped = 0;
312 amdtp->channel = channel;
313 result = raw1394_iso_xmit_start (amdtp->handle, 0,
314 amdtp->prebuffer_packets);
315 }
316
317 return result;
318 }
319
320 iec61883_amdtp_t
321 iec61883_amdtp_recv_init (raw1394handle_t handle,
322 iec61883_amdtp_recv_t put_data, void *callback_data)
323 {
324 struct iec61883_amdtp *amdtp;
325
326 amdtp = malloc (sizeof (struct iec61883_amdtp));
327 if (!amdtp) {
328 errno = ENOMEM;
329 return NULL;
330 }
331
332 amdtp->channel = -1;
333 amdtp->handle = handle;
334 amdtp->put_data = put_data;
335 amdtp->callback_data = callback_data;
336 amdtp->buffer_packets = 1000;
337 amdtp->irq_interval = 250;
338 amdtp->synch = 0;
339
340 raw1394_set_userdata (handle, amdtp);
341
342 return amdtp;
343 }
344
345 static enum raw1394_iso_disposition
346 amdtp_recv_handler (raw1394handle_t handle,
347 unsigned char *data,
348 unsigned int len,
349 unsigned char channel,
350 unsigned char tag,
351 unsigned char sy,
352 unsigned int cycle,
353 unsigned int dropped)
354 {
355 struct iec61883_amdtp *amdtp = raw1394_get_userdata (handle);
356 enum raw1394_iso_disposition result = RAW1394_ISO_OK;
357 struct iec61883_packet *packet = (struct iec61883_packet *) data;
358 int label;
359
360 assert (amdtp != NULL);
361 amdtp->total_dropped += dropped;
362
363 /* We only support AM824 data for the moment. */
364 /* We should check the DBC value to make sure we don't miss any packet. */
365 if (tag == IEC61883_TAG_WITH_CIP &&
366 packet->fmt == IEC61883_FMT_AMDTP && (
367 ((packet->fdf & ~IEC61883_FDF_SFC_MASK) == IEC61883_FDF_AM824) ||
368 ((packet->fdf & ~IEC61883_FDF_SFC_MASK) == IEC61883_FDF_AM824_CONTROLLED))) {
369
370 /* We fill amdtp structure fields upon reception of first packet. */
371 if ((amdtp->dimension < 0) && (packet->syt != 0xFFFF)) {
372 amdtp->dimension = packet->dbs; /* Number of audio channels. */
373
374 DEBUG ("FDF code = %d.", packet->fdf);
375
376 /* Obtaining sampling rate from SFC code. */
377 switch (packet->fdf & IEC61883_FDF_SFC_MASK)
378 {
379 case IEC61883_FDF_SFC_32KHZ:
380 amdtp->rate = 32000;
381 break;
382 case IEC61883_FDF_SFC_44K1HZ:
383 amdtp->rate = 44100;
384 break;
385 case IEC61883_FDF_SFC_48KHZ:
386 amdtp->rate = 48000;
387 break;
388 case IEC61883_FDF_SFC_88K2HZ:
389 amdtp->rate = 88200;
390 break;
391 case IEC61883_FDF_SFC_96KHZ:
392 amdtp->rate = 96000;
393 break;
394 case IEC61883_FDF_SFC_176K4HZ:
395 amdtp->rate = 176400;
396 break;
397 case IEC61883_FDF_SFC_192KHZ:
398 amdtp->rate = 192000;
399 default:
400 WARN ("Unsupported SFC code (%d).",
401 packet->fdf & IEC61883_FDF_SFC_MASK);
402 return RAW1394_ISO_ERROR;
403 }
404
405 label = htonl (*((quadlet_t *) packet->data)) >> 24;
406
407 /* Checking label. */
408 if ((label & ~0x03) == IEC61883_AM824_LABEL) {
409 DEBUG ("Multi-bit Linear Audio (MBLA) samples.");
410 /* Multi-bit Linear Audio (MBLA). */
411 amdtp->format = IEC61883_AMDTP_FORMAT_RAW;
412
413 /* Checking Valid Bit Length code. */
414 switch (label & 0x03) {
415 case IEC61883_AM824_VBL_24BITS:
416 DEBUG ("24-bits samples.");
417 amdtp->sample_format =
418 IEC61883_AMDTP_INPUT_LE24;
419 break;
420 case IEC61883_AM824_VBL_20BITS:
421 DEBUG ("20-bits samples.");
422 amdtp->sample_format =
423 IEC61883_AMDTP_INPUT_LE20;
424 break;
425 case IEC61883_AM824_VBL_16BITS:
426 DEBUG ("16-bits samples.");
427 amdtp->sample_format =
428 IEC61883_AMDTP_INPUT_LE16;
429 break;
430 default:
431 WARN ("Unsupported valid bit length code (%d).", label & 0x03);
432 result = RAW1394_ISO_ERROR;
433 }
434 } else if ((label >= 0) && (label <= 0x3F)) {
435 /* IEC60958 Conformant Data. */
436 /* TODO: how to determine AC3 */
437 amdtp->format = IEC61883_AMDTP_FORMAT_IEC958_PCM;
438 amdtp->sample_format = IEC61883_AMDTP_INPUT_NA;
439 } else {
440 WARN ("Unsupported data format label (%d).",
441 label);
442 result = RAW1394_ISO_ERROR;
443 }
444 }
445
446 if ((result == RAW1394_ISO_OK) &&
447 (amdtp->dimension > 0) && (packet->syt != 0xFFFF)) {
448 int nsamples = (len / sizeof (quadlet_t)) - 2; /* Substracting CIP headers. */
449 quadlet_t *event = (quadlet_t *) packet->data;
450 int i;
451
452 for (i = 0; i < nsamples; i++)
453 event[i] = ntohl (event[i]);
454
455 if (amdtp->put_data (amdtp, packet->data, nsamples, packet->dbc, dropped,
456 amdtp->callback_data) < 0)
457 result = RAW1394_ISO_ERROR;
458 }
459 }
460 if (result == RAW1394_ISO_OK && dropped)
461 result = RAW1394_ISO_DEFER;
462
463 return result;
464 }
465
466 int
467 iec61883_amdtp_recv_start (struct iec61883_amdtp *amdtp, int channel)
468 {
469 int result = 0;
470
471 assert (amdtp != NULL);
472 result = raw1394_iso_recv_init (amdtp->handle,
473 amdtp_recv_handler,
474 amdtp->buffer_packets,
475 AMDTP_MAX_PACKET_SIZE,
476 channel,
477 RAW1394_DMA_PACKET_PER_BUFFER,
478 amdtp->irq_interval);
479
480 if (result == 0) {
481 amdtp->total_dropped = 0;
482 amdtp->channel = channel;
483 amdtp->dimension = -1; /* The audio informations in amdtp structure will be
484 * filled-in upon reception of the first isochronous
485 * packet. */
486
487 result = raw1394_iso_recv_start (amdtp->handle, -1, -1, 0);
488 }
489 return result;
490 }
491
492 void
493 iec61883_amdtp_recv_stop (struct iec61883_amdtp *amdtp)
494 {
495 assert (amdtp != NULL);
496 if (amdtp->synch)
497 raw1394_iso_recv_flush (amdtp->handle);
498 raw1394_iso_shutdown (amdtp->handle);
499 }
500
501 void
502 iec61883_amdtp_xmit_stop (struct iec61883_amdtp *amdtp)
503 {
504 assert (amdtp != NULL);
505 if (amdtp->synch)
506 raw1394_iso_xmit_sync (amdtp->handle);
507 raw1394_iso_shutdown (amdtp->handle);
508 }
509
510 void
511 iec61883_amdtp_close (struct iec61883_amdtp *amdtp)
512 {
513 assert (amdtp != NULL);
514 if (amdtp->put_data)
515 iec61883_amdtp_recv_stop (amdtp);
516 if (amdtp->get_data)
517 iec61883_amdtp_xmit_stop (amdtp);
518 free (amdtp);
519 }
520
521 unsigned int
522 iec61883_amdtp_get_buffers (iec61883_amdtp_t amdtp)
523 {
524 assert (amdtp != NULL);
525 return amdtp->buffer_packets;
526 }
527
528 void
529 iec61883_amdtp_set_buffers (iec61883_amdtp_t amdtp, unsigned int packets)
530 {
531 assert (amdtp != NULL);
532 amdtp->buffer_packets = packets;
533 }
534
535 unsigned int
536 iec61883_amdtp_get_prebuffers (iec61883_amdtp_t amdtp)
537 {
538 assert (amdtp != NULL);
539 return amdtp->prebuffer_packets;
540 }
541
542 void
543 iec61883_amdtp_set_prebuffers (iec61883_amdtp_t amdtp, unsigned int packets)
544 {
545 assert (amdtp != NULL);
546 amdtp->prebuffer_packets = packets;
547 }
548
549 unsigned int
550 iec61883_amdtp_get_irq_interval (iec61883_amdtp_t amdtp)
551 {
552 assert (amdtp != NULL);
553 return amdtp->irq_interval;
554 }
555
556 void
557 iec61883_amdtp_set_irq_interval (iec61883_amdtp_t amdtp, unsigned int packets)
558 {
559 assert (amdtp != NULL);
560 amdtp->irq_interval = packets;
561 }
562
563 int
564 iec61883_amdtp_get_synch (iec61883_amdtp_t amdtp)
565 {
566 assert (amdtp != NULL);
567 return amdtp->synch;
568 }
569
570 void
571 iec61883_amdtp_set_synch (iec61883_amdtp_t amdtp, int synch)
572 {
573 assert (amdtp != NULL);
574 amdtp->synch = synch;
575 }
576
577 int
578 iec61883_amdtp_get_speed (iec61883_amdtp_t amdtp)
579 {
580 assert (amdtp != NULL);
581 return amdtp->speed;
582 }
583
584 void
585 iec61883_amdtp_set_speed (iec61883_amdtp_t amdtp, int speed)
586 {
587 assert (amdtp != NULL);
588 amdtp->speed = speed;
589 }
590
591 unsigned int
592 iec61883_amdtp_get_dropped(iec61883_amdtp_t amdtp)
593 {
594 assert (amdtp != NULL);
595 return amdtp->total_dropped;
596 }
597
598 void *
599 iec61883_amdtp_get_callback_data (iec61883_amdtp_t amdtp)
600 {
601 assert (amdtp != NULL);
602 return amdtp->callback_data;
603 }
604
605 int
606 iec61883_amdtp_get_dimension(iec61883_amdtp_t amdtp)
607 {
608 assert (amdtp != NULL);
609 return amdtp->dimension;
610 }
611
612 int
613 iec61883_amdtp_get_rate(iec61883_amdtp_t amdtp)
614 {
615 assert (amdtp != NULL);
616 return amdtp->rate;
617 }
618
619 enum iec61883_amdtp_format
620 iec61883_amdtp_get_format(iec61883_amdtp_t amdtp)
621 {
622 assert (amdtp != NULL);
623 return amdtp->format;
624 }
625
626 enum iec61883_amdtp_sample_format
627 iec61883_amdtp_get_sample_format(iec61883_amdtp_t amdtp)
628 {
629 assert (amdtp != NULL);
630 return amdtp->sample_format;
631 }