Debian Codegraph @

src/tsbuffer.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 orginially written in C++ by Dan Maas; ported to C by Dan Dennedy.
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 <stdlib.h>
26 #include <stdio.h>
27 #include <netinet/in.h>
28 #include <string.h>
29
30 #include "deque.h"
31 #include "tsbuffer.h"
32
33 // max # of packets to look ahead for PCRs
34 // reasonable values: 1000 - 10000
35 #define MAX_PCR_LOOKAHEAD 20000
36
37 // # of PCRs to average over when estimating bitrate
38 // reasonable values: 1-100
39 #define PCR_SMOOTH_INTERVAL 5
40
41 // approximate # of ISO cycles transmission delay
42 // valid range is 0-10; good values are 5-15
43 #define SYT_OFFSET 7
44
45 // leave this off for now; I don't think it's needed with the new algorithm
46 #define ENABLE_PCR_DRIFT_CORRECTION 0
47
48 // # of seconds between PCR bitrate drift checks
49 // reasonable values: 1-5
50 #define PCR_DRIFT_INTERVAL 1
51
52 typedef unsigned char u8;
53 typedef unsigned int u32;
54 typedef unsigned long long u64;
55 typedef signed long long s64;
56
57 /* 8-byte CIP (Common Isochronous Packet) header */
58 struct CIP_header
59 {
60 u8 b[ 8 ];
61 };
62
63 static void
64 fill_mpeg_cip_header( struct CIP_header *cip, u8 src_node_id, u8 counter )
65 {
66 cip->b[ 0 ] = src_node_id;
67 cip->b[ 1 ] = 0x06;
68 cip->b[ 2 ] = 0xC4;
69 cip->b[ 3 ] = counter;
70
71 cip->b[ 4 ] = 0xA0;
72 cip->b[ 5 ] = 0x80; // "time-shifted" flag; also try 0x00
73 cip->b[ 6 ] = 0x00;
74 cip->b[ 7 ] = 0x00;
75 }
76
77 /* MPEG-2 TS timestamp */
78 typedef u32 sph_t;
79
80 static inline u32
81 mpeg_ts_offset( sph_t t )
82 {
83 return t & 0xfff;
84 }
85
86 static inline u32
87 mpeg_ts_count( sph_t t )
88 {
89 return ( t >> 12 ) & 0x1fff;
90 }
91
92 static inline sph_t
93 make_sph( u32 count, u32 offset )
94 {
95 return ( ( count & 0x1fff ) << 12 ) | ( offset & 0xfff );
96 }
97
98
99 /* MPEG-2 TSP packet (timestamp plus TS packet) */
100 struct TSP_packet
101 {
102 sph_t sph;
103 unsigned char data[ 188 ];
104 };
105
106 // one ISO cycle
107 struct buf_cycle
108 {
109 struct CIP_header header;
110 struct TSP_packet packet[ 3 ]; // up to three TSP packets
111 };
112
113 // 188-byte MPEG-2 TS packet
114 struct mpeg2_ts
115 {
116 u8 ts_header[ 4 ];
117 u8 adapt_len;
118 u8 adapt_flags;
119 u8 pcr[ 6 ];
120 u8 data[ 176 ];
121 };
122
123 static int
124 ts_get_pid( struct mpeg2_ts *ts )
125 {
126 return ( ( ts->ts_header[ 1 ] << 8 ) + ts->ts_header[ 2 ] ) & 0x1fff;
127 }
128
129 // returns the PCR clock, in units of 1 / 27MHz
130 static u64
131 ts_get_pcr( struct mpeg2_ts *ts )
132 {
133 u64 pcr;
134
135 pcr = ts->pcr[ 0 ] << 25;
136 pcr += ts->pcr[ 1 ] << 17;
137 pcr += ts->pcr[ 2 ] << 9;
138 pcr += ts->pcr[ 3 ] << 1;
139 pcr += ( ts->pcr[ 4 ] & 0x80 ) >> 7;
140 pcr *= 300;
141 pcr += ( ts->pcr[ 4 ] & 0x1 ) << 8;
142 pcr += ts->pcr[ 5 ];
143
144 return pcr;
145 }
146
147 static int
148 ts_has_pcr( struct mpeg2_ts *ts, int pid )
149 {
150 if ( ( pid == -1 ) ||
151 ( pid == ts_get_pid( ts ) ) )
152 {
153
154
155 if ( ( ts->ts_header[ 3 ] & 0x20 ) // adaptation field present
156 && ( ts->adapt_len > 0 )
157 && ( ts->adapt_flags & 0x10 ) )
158 {
159 return 1;
160 }
161 }
162
163 return 0;
164 }
165
166
167
168 /** tsbuffer ******************************************************************/
169
170 struct tsbuffer
171 {
172 // queue of TS packets waiting to be sent
173 iec61883_deque_t ts_queue;
174 iec61883_mpeg2_xmit_t read_packet;
175 void *callback_data;
176 unsigned int dropped;
177
178 // PCR state machine
179 u64 last_pcr; // last PCR seen
180 u64 pcr_drift_ref; // PCR from which to base drift calculations
181 u32 pcr_drift_cycles; // how many cycles have passed since pcr_drift_ref was set
182 u32 packets_since_last_pcr;
183 u64 delta_pcr_per_packet;
184
185 // packetization state machine
186 // num/denom algorithm for determining # of TS packets to send out in each ISO cycle
187 u64 tsp_accum;
188 u64 tsp_whole;
189 u64 tsp_num;
190 u64 tsp_denom;
191
192 int selected_pid;
193
194 // ISO continuity counter
195 u32 iso_counter;
196 };
197
198 tsbuffer_t
199 tsbuffer_init (iec61883_mpeg2_xmit_t read_cb, void *callback_data, int pid)
200 {
201 tsbuffer_t this = (tsbuffer_t) calloc (1, sizeof (struct tsbuffer));
202 if (this) {
203 // initialize members
204 this->last_pcr = 0;
205 this->pcr_drift_ref = 0;
206 this->pcr_drift_cycles = 0;
207 this->tsp_accum = 0;
208 this->iso_counter = 0;
209 this->selected_pid = pid;
210 this->ts_queue = iec61883_deque_init();
211 this->read_packet = read_cb;
212 this->callback_data = callback_data;
213 this->dropped = 0;
214
215 // skip ahead to the first PCR
216 tsbuffer_read_to_next_pcr (this);
217 this->last_pcr = ts_get_pcr (iec61883_deque_back (this->ts_queue));
218
219 // dump the useless packets that precede the first PCR
220 while (iec61883_deque_size (this->ts_queue) > 0)
221 free (iec61883_deque_pop_front (this->ts_queue));
222
223 tsbuffer_refill (this);
224 }
225 return this;
226 }
227
228
229 void
230 tsbuffer_set_pid (tsbuffer_t this, int pid)
231 {
232 this->selected_pid = pid;
233 }
234
235 int
236 tsbuffer_read_ts (tsbuffer_t this)
237 {
238 struct mpeg2_ts* new_ts = calloc (1, sizeof (struct mpeg2_ts));
239 unsigned char *ts = (unsigned char*) new_ts;
240
241 if (this->read_packet (ts, 1, this->dropped, this->callback_data) < 0)
242 return 0;
243 /* Do not necessarily indicate dropped packet on next call; rawiso handler
244 will set again when needed. */
245 this->dropped = 0;
246
247 iec61883_deque_push_back (this->ts_queue, new_ts);
248
249 return 1;
250 }
251
252 int
253 tsbuffer_read_to_next_pcr (tsbuffer_t this)
254 {
255 do {
256 if (iec61883_deque_size (this->ts_queue) > MAX_PCR_LOOKAHEAD) {
257 fprintf (stderr, "couldn't find a PCR within %d packets; giving up\n", MAX_PCR_LOOKAHEAD);
258 fprintf (stderr, "(try reducing PCR_SMOOTH_INTERVAL or increase MAX_PCR_LOOKAHEAD\n");
259 return 0;
260 }
261
262 if (tsbuffer_read_ts (this) == 0)
263 return 0;
264 if (this->selected_pid == -1 && ts_has_pcr(iec61883_deque_back (this->ts_queue), -1))
265 this->selected_pid = ts_get_pid (iec61883_deque_back (this->ts_queue));
266
267 } while (ts_has_pcr (iec61883_deque_back (this->ts_queue), this->selected_pid) == 0);
268
269 return 1;
270 }
271
272 int
273 tsbuffer_refill (tsbuffer_t this)
274 {
275 unsigned int i;
276 u32 n_packets;
277 u64 pcr;
278 u64 delta_pcr;
279 u64 num;
280 u64 denom;
281
282 for (i = 0; i < PCR_SMOOTH_INTERVAL; i++)
283 if (tsbuffer_read_to_next_pcr (this) == 0)
284 return 0;
285
286 n_packets = iec61883_deque_size (this->ts_queue);
287
288 pcr = ts_get_pcr (iec61883_deque_back (this->ts_queue));
289
290 if (this->pcr_drift_ref == 0) {
291 // set up a PCR drift calculation
292 this->pcr_drift_ref = pcr;
293 this->pcr_drift_cycles = 0;
294 }
295
296 delta_pcr = pcr - this->last_pcr;
297
298 this->delta_pcr_per_packet = delta_pcr / n_packets;
299
300 // Calculate the TSP packetization rate
301
302 /*
303 This is an *exact* calculation! We want to send n_packets packets
304 in the period of time covered by delta_pcr. The packet transmission
305 rate should therefore be:
306
307 (n_packets / delta_pcr) * (27,000,000 PCRs/sec)
308
309 But what we really want is the packet transmission rate *per ISO cycle*,
310 not per second. So, divide by 8000 ISO cycles/sec:
311
312 (n_packets / delta_pcr) * (27,000,000 / sec) / (8000 cycles /sec)
313
314 And then something magic happens: 27,000,000 / 8,000 = 3,375 exactly!
315
316 So the *exact* transmission rate is:
317
318 (n_packets / delta_pcr) * (3,375) (per ISO cycle)
319
320 We use a standard numerator/denominator algorithm to achieve this.
321
322 */
323
324 num = n_packets * 3375;
325 denom = delta_pcr;
326
327 this->tsp_whole = num / denom;
328 this->tsp_num = num % denom;
329 this->tsp_denom = denom;
330
331 /* note: We don't reset tsp_accum to zero. This should improve our accuracy,
332 as long as the transmission rate stays fairly constant. */
333
334 #if 0
335 // for my SD streams: 1 90/2981 is best
336 fprintf( stderr, "PCR (PID %d) after %d packets %llu delta = %llu TSP wh %llu num %llu den %llu\n",
337 ts_get_pid (iec61883_deque_back (this->ts_queue)), n_packets, pcr, delta_pcr,
338 this->tsp_whole, this->tsp_num, this->tsp_denom );
339 #endif
340
341 this->last_pcr = pcr;
342 this->packets_since_last_pcr = 0;
343
344 return 1;
345 }
346
347 u32
348 tsbuffer_send_iso_cycle (tsbuffer_t this, void *data,
349 u32 iso_cycle, u8 src_node_id, unsigned int dropped)
350 {
351 // choose # of tsps by num/denom algorithm
352 unsigned int n_tsps = this->tsp_whole;
353 unsigned int i;
354 struct buf_cycle *cycle = (struct buf_cycle*) data;
355
356 this->dropped = dropped;
357
358 top:
359 if (this->tsp_accum > (this->tsp_denom - this->tsp_num)) {
360 n_tsps += 1;
361 this->tsp_accum -= (this->tsp_denom - this->tsp_num);
362 } else {
363 this->tsp_accum += this->tsp_num;
364 }
365
366 if (this->pcr_drift_ref != 0) {
367 this->pcr_drift_cycles++;
368 if (this->pcr_drift_cycles % (PCR_DRIFT_INTERVAL * 8000) == 0) {
369
370 // time to check for PCR drift
371
372 // estimate the PCR that would correspond to *this* packet, if it had a PCR
373 u64 cur_pcr = this->last_pcr + ((u64) this->packets_since_last_pcr)
374 * this->delta_pcr_per_packet;
375
376 // compare cur_pcr to pcr_drift_ref + (1/27MHz) * seconds_elapsed_since_pcr_drift_ref
377 s64 drift = (cur_pcr - this->pcr_drift_ref) - (((s64) 27000000) *
378 (this->pcr_drift_cycles / 8000));
379
380 #if 0
381 fprintf (stderr, "PCR drift: %lld after %u seconds\n", drift, this->pcr_drift_cycles/8000);
382 #endif
383
384 if (ENABLE_PCR_DRIFT_CORRECTION) {
385 //if(pcr_drift_cycles % (5*PCR_DRIFT_INTERVAL*8000) == 0)
386 if ( ( drift > (s64) 27000000 ) | ( drift < (s64) -27000000) ) {
387 fprintf (stderr, "applying PCR correction of %lld\n", -drift);
388
389 // apply the drift correction
390 //this->last_pcr -= drift;
391 this->pcr_drift_ref = 0;
392 this->pcr_drift_cycles = 0;
393 while (iec61883_deque_size (this->ts_queue) > 0)
394 free (iec61883_deque_pop_front (this->ts_queue));
395 if (tsbuffer_refill (this) == 0)
396 return 0;
397 goto top;
398 }
399 }
400 }
401 }
402
403 while (n_tsps > iec61883_deque_size (this->ts_queue))
404 if (tsbuffer_read_ts (this) == 0)
405 return 0;
406
407 // write the CIP header
408 fill_mpeg_cip_header (&cycle->header, src_node_id, this->iso_counter);
409
410 for (i = 0; i < n_tsps; i++) {
411 u32 cycle_count ;
412
413 memcpy ((char*) &cycle->packet[i].data[0],
414 (char*) iec61883_deque_front (this->ts_queue),
415 sizeof (struct mpeg2_ts));
416
417 free (iec61883_deque_pop_front (this->ts_queue));
418
419 // set timestamp to iso_cycle + SYT_OFFSET + 1000 offsets per TSP
420 // (since at most 3 TSP per packet; this will ensure monotonically
421 // increasing timestamps, spaced out semi-regularly)
422 cycle_count = (iso_cycle + SYT_OFFSET) % 8000;
423
424 cycle->packet[i].sph = htonl( make_sph( cycle_count, 1000 * i));
425
426 this->packets_since_last_pcr += 1;
427 }
428
429 if (iec61883_deque_size (this->ts_queue) == 0)
430 if (tsbuffer_refill (this) == 0)
431 return 0;
432
433 // advance continuity counter by 8 per TSP in this cycle
434 this->iso_counter += 8 * n_tsps;
435
436 return sizeof (struct CIP_header) + n_tsps * sizeof (struct TSP_packet);
437 }
438
439 void
440 tsbuffer_close (tsbuffer_t this)
441 {
442 iec61883_deque_close (this->ts_queue);
443 free (this);
444 }