Debian Codegraph @

src/cip.c

raw
1 /*
2 * libiec61883 - Linux IEEE 1394 streaming media library.
3 * Copyright (C) 2004 Kristian Hogsberg, Dan Dennedy, and Dan Maas.
4 * This file written by Kristian Hogsberg.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include "iec61883.h"
26 #include "iec61883-private.h"
27
28 #include <netinet/in.h>
29
30
31 /* Integer fractional math. When we transmit a 44k1Hz signal we must
32 * send 5 41/80 samples per isochronous cycle, as these occur 8000
33 * times a second. Of course, we must send an integral number of
34 * samples in a packet, so we use the integer math to alternate
35 * between sending 5 and 6 samples per packet.
36 */
37
38 static void
39 fraction_init(struct iec61883_fraction *f, int numerator, int denominator)
40 {
41 f->integer = numerator / denominator;
42 f->numerator = numerator % denominator;
43 f->denominator = denominator;
44 }
45
46 static __inline__ void
47 fraction_add(struct iec61883_fraction *dst,
48 struct iec61883_fraction *src1, struct iec61883_fraction *src2)
49 {
50 /* assert: src1->denominator == src2->denominator */
51
52 int sum, denom;
53
54 /* We use these two local variables to allow gcc to optimize
55 * the division and the modulo into only one division. */
56
57 sum = src1->numerator + src2->numerator;
58 denom = src1->denominator;
59 dst->integer = src1->integer + src2->integer + sum / denom;
60 dst->numerator = sum % denom;
61 dst->denominator = denom;
62 }
63
64 static __inline__ void
65 fraction_sub_int(struct iec61883_fraction *dst, struct iec61883_fraction *src, int integer)
66 {
67 dst->integer = src->integer - integer;
68 dst->numerator = src->numerator;
69 dst->denominator = src->denominator;
70 }
71
72 static __inline__ int
73 fraction_floor(struct iec61883_fraction *frac)
74 {
75 return frac->integer;
76 }
77
78 static __inline__ int
79 fraction_ceil(struct iec61883_fraction *frac)
80 {
81 return frac->integer + (frac->numerator > 0 ? 1 : 0);
82 }
83
84 void
85 iec61883_cip_init(struct iec61883_cip *ptz, int format, int fdf,
86 int rate, int dbs, int syt_interval)
87 {
88 const int transfer_delay = 9000;
89
90 ptz->rate = rate;
91 ptz->cycle_count = transfer_delay / 3072;
92 ptz->format = format;
93 ptz->fdf = fdf;
94 ptz->mode = IEC61883_MODE_BLOCKING_EMPTY;
95 ptz->dbs = dbs;
96 ptz->dbc = 0;
97 ptz->syt_interval = syt_interval;
98
99 fraction_init(&ptz->samples_per_cycle, ptz->rate, 8000);
100 fraction_init(&ptz->ready_samples, 0, 8000);
101
102 /* The ticks_per_syt_offset is initialized to the number of
103 * ticks between syt_interval events. The number of ticks per
104 * second is 24.576e6, so the number of ticks between
105 * syt_interval events is 24.576e6 * syt_interval / rate.
106 */
107 fraction_init(&ptz->ticks_per_syt_offset,
108 24576000 * ptz->syt_interval, ptz->rate);
109 fraction_init(&ptz->cycle_offset,
110 (transfer_delay % 3072) * ptz->rate, ptz->rate);
111 }
112
113 void
114 iec61883_cip_resync(struct iec61883_cip *ptz, int cycle)
115 {
116 const int transfer_delay = 9000;
117
118 ptz->cycle_count = cycle + (transfer_delay / 3072);
119 fraction_init(&ptz->cycle_offset,
120 (transfer_delay % 3072) * ptz->rate, ptz->rate);
121
122 fraction_init(&ptz->ready_samples, 0, 8000);
123 ptz->dbc = 0;
124 }
125
126 void
127 iec61883_cip_set_transmission_mode(struct iec61883_cip *ptz, int mode)
128 {
129 ptz->mode = mode;
130 }
131
132 int
133 iec61883_cip_get_max_packet_size(struct iec61883_cip *ptz)
134 {
135 int max_nevents;
136
137 if (ptz->mode == IEC61883_MODE_BLOCKING_EMPTY || ptz->mode == IEC61883_MODE_BLOCKING_NODATA)
138 max_nevents = ptz->syt_interval;
139 else
140 max_nevents = fraction_ceil(&ptz->samples_per_cycle);
141
142 return max_nevents * ptz->dbs * 4 + 8;
143 }
144
145
146 int
147 iec61883_cip_fill_header(raw1394handle_t handle, struct iec61883_cip *ptz,
148 struct iec61883_packet *packet)
149 {
150 struct iec61883_fraction next;
151 int nevents, nevents_dbc, syt_index, syt;
152
153 fraction_add(&next, &ptz->ready_samples, &ptz->samples_per_cycle);
154 if (ptz->mode == IEC61883_MODE_BLOCKING_EMPTY ||
155 ptz->mode == IEC61883_MODE_BLOCKING_NODATA) {
156 if (fraction_floor(&next) >= ptz->syt_interval)
157 nevents = ptz->syt_interval;
158 else
159 nevents = 0;
160 }
161 else
162 nevents = fraction_floor(&next);
163
164 if (ptz->mode == IEC61883_MODE_BLOCKING_NODATA) {
165 /* The DBC is incremented even with NO_DATA packets. */
166 nevents_dbc = ptz->syt_interval;
167 }
168 else {
169 nevents_dbc = nevents;
170 }
171
172 /* Now that we know how many events to put in the packet, update the
173 * fraction ready_samples. */
174 fraction_sub_int(&ptz->ready_samples, &next, nevents);
175
176 /* Calculate synchronization timestamp (syt). First we
177 * determine syt_index, that is, the index in the packet of
178 * the sample for which the timestamp is valid. */
179 syt_index = (ptz->syt_interval - ptz->dbc) & (ptz->syt_interval - 1);
180 if (syt_index < nevents) {
181 syt = ((ptz->cycle_count << 12) | fraction_floor(&ptz->cycle_offset)) & 0xffff;
182 fraction_add(&ptz->cycle_offset, &ptz->cycle_offset,
183 &ptz->ticks_per_syt_offset);
184
185 /* The cycle_count field is a 13 bits value that goes from 0 to 7999.
186 * The cycle_offset field is a 12 bits value that goes from 0 to 3071. */
187 ptz->cycle_count += ptz->cycle_offset.integer / 3072;
188 ptz->cycle_count %= 8000;
189 ptz->cycle_offset.integer %= 3072;
190 }
191 else
192 syt = 0xffff;
193
194 packet->eoh0 = 0;
195
196 /* Our node ID can change after a bus reset, so it is best to fetch
197 * our node ID for each packet. */
198 packet->sid = raw1394_get_local_id( handle ) & 0x3f;
199
200 packet->dbs = ptz->dbs;
201 packet->fn = 0;
202 packet->qpc = 0;
203 packet->sph = 0;
204 packet->reserved = 0;
205 packet->dbc = ptz->dbc;
206 packet->eoh1 = 2;
207 packet->fmt = ptz->format;
208
209 if ( nevents == 0 && ptz->mode == IEC61883_MODE_BLOCKING_NODATA ) {
210 /* FDF code for packets containing dummy data. */
211 packet->fdf = IEC61883_FDF_NODATA;
212 }
213 else {
214 /* FDF code for non-blocking mode and for blocking mode with empty packets. */
215 packet->fdf = ptz->fdf;
216 }
217
218 packet->syt = htons(syt);
219
220 ptz->dbc += nevents_dbc;
221
222 return nevents;
223 }