File: a52_decoder.c

package info (click to toggle)
ogle 0.8.2-11
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,636 kB
  • ctags: 2,827
  • sloc: ansic: 27,297; sh: 7,305; makefile: 234; asm: 71
file content (792 lines) | stat: -rw-r--r-- 18,961 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
/* Ogle - A video player
 * Copyright (C) 2000, 2001 Bjrn Englund, Hkan Hjort
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/shm.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

#ifndef SHM_SHARE_MMU
#define SHM_SHARE_MMU 0
#endif

#include <ogle/msgevents.h>

#include "debug_print.h"
#include "common.h"
#include "queue.h"
#include "timemath.h"
#include "sync.h"

#include <a52dec/a52.h>
#include <libao/audio_out.h>
#include <a52dec/mm_accel.h>

#include "parse_config.h"

/* A/52 */
static ao_instance_t * output = NULL;
static sample_t * samples;
static int disable_dynrng = 0;
static clocktime_t a52_decode_data(uint8_t *start, uint8_t *end);



static int get_q();
static int attach_ctrl_shm(int shmid);
static int attach_stream_buffer(uint8_t stream_id, uint8_t subtype, int shmid);

static void handle_events(MsgEventQ_t *q, MsgEvent_t *ev);

static int ctrl_data_shmid;
static ctrl_data_t *ctrl_data;
static ctrl_time_t *ctrl_time;

static int stream_shmid;
static char *stream_shmaddr;

static int data_buf_shmid;
static char *data_buf_shmaddr;

static int msgqid = -1;
static MsgEventQ_t *msgq;

static int flush_to_scrid = -1;
static int prev_scr_nr = 0;

static int speaker_flags = -1;

char *program_name;

void usage()
{
  fprintf(stderr, "Usage: %s  [-m <msgid>]\n", program_name);
}

static int get_speaker_flags(void)
{
  int front, rear, sub;
  int result = 0;
  
  front = get_num_front_speakers();
  rear = get_num_rear_speakers();
  sub = get_num_sub_speakers();

  if(front == 2 && rear == 0) {
    result = A52_STEREO;
    /* if(dolby) result = A52_DOLBY; */
  } else if(front == 3 && rear == 0) {
    result = A52_3F;
  } else if(front == 2 && rear == 2) {
    result = A52_2F2R;
  } else if(front == 3 && rear == 2) {
    result = A52_3F2R;
  } else if(front == 2 && rear == 1) {
    result = A52_2F1R;
  } else if(front == 3 && rear == 1) {
    result = A52_3F1R;
  }
  if(sub == 1) {
    result |= A52_LFE;
  }
  return result;
}


static void open_output(int flags)
{
  int i;
  int driver_num = 0;
  ao_driver_t * drivers;
  int ch_avail;

  /* Ideally, we would just pass `flags' through to liba52, since that
   * seems to be how it works internally, however until it offers
   * such an interface, we figure out which "driver" it needs.
   */

  switch(flags & A52_CHANNEL_MASK) {
  case A52_CHANNEL:
  case A52_STEREO:
  case A52_DOLBY:
    ch_avail = 2;
    break;
  case A52_3F:
  case A52_2F1R:
    ch_avail = 3;
    break;
  case A52_3F1R:
  case A52_2F2R:
    ch_avail = 4;
    break;
  case A52_3F2R:
    ch_avail = 5;
    break;
  default:
    ch_avail = 0;
  }

  if(flags & A52_LFE) {
    ch_avail++;
  }

  drivers = ao_drivers ();
  for(i = 0; drivers[i].name; i++) {
    if(!strcmp("oss", drivers[i].name)
      || !strcmp("solaris", drivers[i].name)) {
      if(ch_avail == 2) {
        printf("opening a 2 channel audio driver\n");
        driver_num = i;
      }
    } else if(!strcmp("oss4", drivers[i].name)) {
      if(ch_avail == 4) {
        printf("opening a 4 channel audio driver\n");
        driver_num = i;
      }
    } else if(!strcmp("oss6", drivers[i].name)) {
      if(ch_avail == 6) {
        printf("opening a 6 channel audio driver\n");
        driver_num = i;
      }
    }
  }

  if(output) {
    ao_close(output);
    free(output);
  }
  output = ao_open(drivers[driver_num].open, get_audio_device());
  if(output == NULL) {
    FATAL("Can not open audio output\n");
    exit(1);
  }
}

int main(int argc, char *argv[])
{
  MsgEvent_t ev;
  int c;
  
  program_name = argv[0];
  
  /* Parse command line options */
  while ((c = getopt(argc, argv, "m:h?")) != EOF) {
    switch (c) {
    case 'm':
      msgqid = atoi(optarg);
      break;
    case 'h':
    case '?':
      usage();
      return 1;
    }
  }

  if(msgqid == -1) {
    if(argc - optind != 1){
      usage();
      return 1;
    }
  }
  
  if(parse_config() == -2) {
    FATAL("Couldn't read config files\n");
    exit(1);
  }

  {
    uint32_t accel;
    accel = MM_ACCEL_MLIB;

    open_output(get_speaker_flags());

    samples = a52_init(accel);
    if(samples == NULL) {
        FATAL("A/52 init failed\n");
        exit(1);
    }
  }

  if(msgqid != -1) {
    if((msgq = MsgOpen(msgqid)) == NULL) {
      FATAL("couldn't get message q\n");
      exit(1);
    }
    
    ev.type = MsgEventQRegister;
    ev.registercaps.capabilities = DECODE_AC3_AUDIO;
    if(MsgSendEvent(msgq, CLIENT_RESOURCE_MANAGER, &ev, 0) == -1) {
      DPRINTF(1, "a52: register capabilities\n");
    }
    
    while(ev.type != MsgEventQDecodeStreamBuf) {
      MsgNextEvent(msgq, &ev);
      handle_events(msgq, &ev);
    }

  } else {
    FATAL("what? need a msgid\n");
  }

  while(1) {
    get_q();
  }

  return 0;
}


static void handle_events(MsgEventQ_t *q, MsgEvent_t *ev)
{
  
  switch(ev->type) {
  case MsgEventQNotify:
    DPRINTF(1, "a52: got notify\n");
    break;
  case MsgEventQFlushData:
    DPRINTF(1, "a52: got flush\n");
    flush_to_scrid = ev->flushdata.to_scrid;
    break;
  case MsgEventQDecodeStreamBuf:
    DPRINTF(1, "a52: got stream %x, %x buffer \n",
	    ev->decodestreambuf.stream_id,
	    ev->decodestreambuf.subtype);
    attach_stream_buffer(ev->decodestreambuf.stream_id,
			  ev->decodestreambuf.subtype,
			  ev->decodestreambuf.q_shmid);
    
    break;
  case MsgEventQCtrlData:
    attach_ctrl_shm(ev->ctrldata.shmid);
    break;
  case MsgEventQSpeed:
    if(ev->speed.speed == 1.0) {
      set_speed(&ctrl_time[prev_scr_nr].sync_point, ev->speed.speed);
    } else {
      if(ctrl_time[prev_scr_nr].sync_point.speed == 1.0) {
	set_speed(&ctrl_time[prev_scr_nr].sync_point, ev->speed.speed);
	if((ctrl_time[prev_scr_nr].sync_master == SYNC_AUDIO)) {
	  ctrl_time[prev_scr_nr].sync_master = SYNC_NONE;
	}
      }
    }
    break;
  default:
    DNOTE("unrecognized event type: %d\n", ev->type);
    break;
  }
}




int attach_ctrl_shm(int shmid)
{
  char *shmaddr;
  
  if(shmid >= 0) {
    if((shmaddr = shmat(shmid, NULL, SHM_SHARE_MMU)) == (void *)-1) {
      perror("a52: attach_ctrl_data(), shmat()");
      return -1;
    }
    
    ctrl_data_shmid = shmid;
    ctrl_data = (ctrl_data_t*)shmaddr;
    ctrl_time = (ctrl_time_t *)(shmaddr+sizeof(ctrl_data_t));
  }    
  
  return 0;
}

int attach_stream_buffer(uint8_t stream_id, uint8_t subtype, int shmid)
{
  char *shmaddr;
  q_head_t *q_head;

  //DNOTE("a52_decoder: shmid: %d\n", shmid);
  
  if(shmid >= 0) {
    if((shmaddr = shmat(shmid, NULL, SHM_SHARE_MMU)) == (void *)-1) {
      perror("ac52_decoder: attach_decoder_buffer(), shmat()");
      return -1;
    }
    
    stream_shmid = shmid;
    stream_shmaddr = shmaddr;
  }    

  q_head = (q_head_t *)stream_shmaddr;
  shmid = q_head->data_buf_shmid;
  
  if(shmid >= 0) {
    if((shmaddr = shmat(shmid, NULL, SHM_SHARE_MMU)) == (void *)-1) {
      perror("a52_decoder: attach_data_buffer(), shmat()");
      return -1;
    }
    
    data_buf_shmid = shmid;
    data_buf_shmaddr = shmaddr;
  }    

  return 0;
}




int get_q()
{
  q_head_t *q_head;
  q_elem_t *q_elems;
  data_buf_head_t *data_head;
  data_elem_t *data_elems;
  data_elem_t *data_elem;
  int elem;
  uint8_t *data_buffer;
  uint8_t PTS_DTS_flags;
  uint64_t PTS;
  uint64_t DTS;
  int scr_nr;
  int off;
  int len;
  static clocktime_t time_offset = { 0, 0 };
  static clocktime_t last_rt = { -1, 0 };
  static clocktime_t in_outputbuf = { 0, 0 };
  static clocktime_t prev_scr_time = { 0, 0 };
  MsgEvent_t ev;
  
  q_head = (q_head_t *)stream_shmaddr;
  q_elems = (q_elem_t *)(stream_shmaddr+sizeof(q_head_t));
  elem = q_head->read_nr;

  while(MsgCheckEvent(msgq, &ev) != -1) {
    handle_events(msgq, &ev);
  }
  
  if(!q_elems[elem].in_use) {
    q_head->reader_requests_notification = 1;
    
    while(!q_elems[elem].in_use) {
      DPRINTF(1, "a52: waiting for notification1\n");
      MsgNextEvent(msgq, &ev);
      handle_events(msgq, &ev);
    }
  }

  data_head = (data_buf_head_t *)data_buf_shmaddr;
  data_buffer = data_buf_shmaddr + data_head->buffer_start_offset;
  data_elems = (data_elem_t *)(data_buf_shmaddr+sizeof(data_buf_head_t));
  
  data_elem = &data_elems[q_elems[elem].data_elem_index];
  
  PTS_DTS_flags = data_elem->PTS_DTS_flags;
  PTS = data_elem->PTS;
  DTS = data_elem->DTS;
  scr_nr = data_elem->scr_nr;
  off = data_elem->off;
  len = data_elem->len;

  if(flush_to_scrid != -1) {
    if(ctrl_time[scr_nr].scr_id < flush_to_scrid) {

      q_head->read_nr = (q_head->read_nr+1)%q_head->nr_of_qelems;
      
      // release elem
      data_elem->in_use = 0;
      q_elems[elem].in_use = 0;
      
      if(q_head->writer_requests_notification) {
	q_head->writer_requests_notification = 0;
	ev.type = MsgEventQNotify;
	if(MsgSendEvent(msgq, q_head->writer, &ev, 0) == -1) {
	  WARNING("couldn't send notification\n");
	}
      }
      return 0;
    } else {
      flush_to_scrid = -1;
    }
  }
  if(ctrl_data->speed == 1.0) {
    clocktime_t real_time, scr_time;
    
    clocktime_get(&real_time);
    
    if(PTS_DTS_flags & 0x2) {
      PTS_TO_CLOCKTIME(scr_time, PTS);
    }
    
    if(ctrl_time[scr_nr].sync_master <= SYNC_AUDIO) {
      clocktime_t tmptime;
      ctrl_time[scr_nr].sync_master = SYNC_AUDIO;
      
      if(ctrl_time[scr_nr].offset_valid == OFFSET_NOT_VALID) {
	if(PTS_DTS_flags & 0x2) {
	  
	  // time_offset is our guess to how much is in the output q

	  if(TIME_S(last_rt) != -1) {
	    tmptime = last_rt;
	  } else {
	    tmptime = real_time;
	  }
	  
	  timeadd(&tmptime, &tmptime, &in_outputbuf);
	  {
	    clocktime_t t1, t2;
	    timesub(&t1, &scr_time, &prev_scr_time);
	    timesub(&t2, &t1, &in_outputbuf);
	    /*
	    if((TIME_SS(t2) < -CT_FRACTION/10000) ||
	       (TIME_SS(t2) > CT_FRACTION/10000)) {
	      fprintf(stderr, "*** diff: %ld.%09ld\n",
		      TIME_S(t2), TIME_SS(t2));
	    }
	    */
	  }
	  
	  fprintf(stderr, "*rt: %ld.%09ld, last_rt: %ld.%09ld\n "
		  "bt: %ld.%09ld,  tmptime: %ld.%09ld\n scr: %ld.%09ld\n",
		  (long)TIME_S(real_time), (long)TIME_SS(real_time),
		  (long)TIME_S(last_rt), (long)TIME_SS(last_rt),
		  (long)TIME_S(in_outputbuf), (long)TIME_SS(in_outputbuf),
		  (long)TIME_S(tmptime), (long)TIME_SS(tmptime),
		  (long)TIME_S(scr_time), (long)TIME_SS(scr_time));
	    
      	  set_sync_point(&ctrl_time[scr_nr],
			 &tmptime,
			 &scr_time,
			 ctrl_data->speed);
	  prev_scr_time = scr_time;

	}
	
      } else {
	/* offset valid */
	if(PTS_DTS_flags & 0x2) {
	  clocktime_t t1, t2;
	  /* clocktime_t calc_scr_time; */
	  

	  if(TIME_S(last_rt) != -1) {
	    tmptime = last_rt;
	  } else {
	    tmptime = real_time;
	  }
	  
	  timeadd(&tmptime, &tmptime, &in_outputbuf);
	  
	  timesub(&t1, &scr_time, &prev_scr_time);
	  timesub(&t2, &t1, &in_outputbuf);
	    
	  if((TIME_SS(t2) < -CT_FRACTION/10000) ||
	     (TIME_SS(t2) > CT_FRACTION/10000)) {
	    /* diff */
	    /*
	    fprintf(stderr, "** diff: %ld.%09ld\n",
		    TIME_S(t2), TIME_SS(t2));
	    */
	    /*
	    timeadd(&calc_scr_time, &scr_time, &in_outputbuf);
	    
	    set_sync_point(&ctrl_time[scr_nr],
			   &tmptime,
			   &calc_scr_time,
			   ctrl_data->speed);
	    prev_scr_time = scr_time;
	    
	    timeadd(&last_rt, &last_rt, &in_outputbuf);
	    */
	  }
	  /*
	    else {
	    // no diff
	  */
	    
	  /*  
	      fprintf(stderr, "rt: %ld.%09ld, last_rt: %ld.%09ld\n bt: %ld.%09ld,  tmptime: %ld.%09ld\n scr: %ld.%09ld\n",
	      TIME_S(real_time), TIME_SS(real_time),
	      TIME_S(last_rt), TIME_SS(last_rt),
	      TIME_S(in_outputbuf), TIME_SS(in_outputbuf),
	      TIME_S(tmptime), TIME_SS(tmptime),
	      TIME_S(scr_time), TIME_SS(scr_time));
	      
	    set_sync_point(&ctrl_time[scr_nr],
			   &tmptime,
			   &scr_time,
			   ctrl_data->speed);
	  */
	    prev_scr_time = scr_time;
	  
	    /*
	  }
	    */
	} else {
	  /* not pts set */
	  clocktime_t calc_scr_time;
	  
	  timeadd(&calc_scr_time, &scr_time, &in_outputbuf);
	  if(TIME_S(last_rt) != -1) {
	    tmptime = last_rt;
	  } else {
	    tmptime = real_time;
	  }
	  
	  timeadd(&tmptime, &tmptime, &in_outputbuf);
	  {
	    clocktime_t t1, t2;
	    timesub(&t1, &calc_scr_time, &prev_scr_time);
	    timesub(&t2, &t1, &in_outputbuf);
	    
	    if((TIME_SS(t2) < -1000) || (TIME_SS(t2) > 1000)) {
	      fprintf(stderr, "**** diff: %ld.%09ld\n",
		      (long)TIME_S(t2), (long)TIME_SS(t2));
	    }
	    
	  }
	  
	  fprintf(stderr, "rt: %ld.%09ld, last_rt: %ld.%09ld\n "
		  "bt: %ld.%09ld,  tmptime: %ld.%09ld\n calc_scr: %ld.%09ld\n",
		  (long)TIME_S(real_time), (long)TIME_SS(real_time),
		  (long)TIME_S(last_rt), (long)TIME_SS(last_rt),
		  (long)TIME_S(in_outputbuf), (long)TIME_SS(in_outputbuf),
		  (long)TIME_S(tmptime), (long)TIME_SS(tmptime),
		  (long)TIME_S(calc_scr_time), (long)TIME_SS(calc_scr_time));
	  
	  set_sync_point(&ctrl_time[scr_nr],
			 &tmptime,
			 &calc_scr_time,
			 ctrl_data->speed);
	  prev_scr_time = scr_time;

	  timeadd(&last_rt, &last_rt, &in_outputbuf);
	  
	}
      }
      if(PTS_DTS_flags & 0x2) {
	calc_realtime_left_to_scrtime(&time_offset, &real_time,
				      &scr_time,
				      &(ctrl_time[scr_nr].sync_point));
      }
      
      /*
       * primitive resync in case output buffer is emptied 
       */
      
      if(TIME_SS(time_offset) < 0 || TIME_S(time_offset) < 0) {
	TIME_S(time_offset) = 0;
	TIME_SS(time_offset) = 0;
	NOTE("resetting offset\n");
	set_sync_point(&ctrl_time[scr_nr],
		       &real_time,
		       &scr_time,
		       ctrl_data->speed);
	//DNOTE("offset reset\n");
      }
    }

    if(PTS_DTS_flags & 0x2) {
      calc_realtime_from_scrtime(&last_rt,
				 &scr_time,
				 &(ctrl_time[scr_nr].sync_point));

      calc_realtime_left_to_scrtime(&time_offset, &real_time,
				    &scr_time,
				    &(ctrl_time[scr_nr].sync_point));

      if(TIME_S(time_offset) > 10) {
	TIME_S(time_offset) = 0;
	TIME_SS(time_offset) = 0;
	//DNOTE("more than 10 secs in audio output buffer, somethings wrong?\n");
      }
    }

    if(ctrl_data->speed == 1.0) {
      
      /** TODO this is just so we don't buffer alot in the pipe **/
      
      if(PTS_DTS_flags & 0x2) {
#ifndef HAVE_CLOCK_GETTIME
	struct timespec bepa;
	clocktime_t apa = {0, 100000};
	timesub(&apa, &time_offset, &apa);
	bepa.tv_sec = apa.tv_sec;
	bepa.tv_nsec = apa.tv_usec*1000;
	
	if(bepa.tv_nsec > 10000 || bepa.tv_sec > 0) {
	  nanosleep(&bepa, NULL);
	}
#else
	
	clocktime_t apa = {0, 100000000};
	timesub(&apa, &time_offset, &apa);
	
	if(TIME_SS(apa) > 10000000 || TIME_S(apa) > 0) {
	  nanosleep(&apa, NULL);
	}
	
#endif 
      }
    }
  }
  prev_scr_nr = scr_nr;
  q_head->read_nr = (q_head->read_nr+1)%q_head->nr_of_qelems;
  
  if(ctrl_data->speed == 1.0) {
    in_outputbuf = a52_decode_data(data_buffer+off, data_buffer+off+len);  
  } else {
    TIME_S(in_outputbuf) = 0;
    TIME_SS(in_outputbuf) = 0;
  }

  // release elem
  data_elem->in_use = 0;
  q_elems[elem].in_use = 0;
  
  if(q_head->writer_requests_notification) {
    q_head->writer_requests_notification = 0;
    ev.type = MsgEventQNotify;
    if(MsgSendEvent(msgq, q_head->writer, &ev, 0) == -1) {
      WARNING("couldn't send notification\n");
    }
  }

  return 0;
}

static clocktime_t a52_decode_data(uint8_t *start, uint8_t *end) {
  static a52_state_t state;
  
  static uint8_t buf[3840];
  static uint8_t *bufptr = buf;
  static uint8_t *bufpos = buf + 7;
  clocktime_t buf_time = {0, 0};
  int blocks = 0;
  /*
   * sample_rate and flags are static because this routine could
   * exit between the a52_syncinfo() and the ao_setup(), and we want
   * to have the same values when we get back !
   */
  
  static int sample_rate;
  static int flags;
  int print_skip = 0, print_error = 0;
  
  while(start < end) {
    *bufptr++ = *start++;
    if(bufptr == bufpos) {
      if(bufpos == buf + 7) {
	int coded_flags, bit_rate, length;
	
	if(print_error) {
	  DNOTE("A/52 error while decoding, restarting\n");
	  print_error = 0;
	}

	length = a52_syncinfo(buf, &coded_flags, &sample_rate, &bit_rate);
	if(!length) {
	  print_skip = 1;
	  for(bufptr = buf; bufptr < buf + 6; bufptr++)
	    bufptr[0] = bufptr[1];
	  continue;
	}
	bufpos = buf + length;
      } else {
	sample_t level, bias;
	int i;	
	if(print_skip) {
	  print_skip = 0;
	  DNOTE("skipped data to find a valid frame\n");
	}
 	
	/* Verify or set the sample rate and retrive flags, level and bias */
	if(ao_setup(output, sample_rate, &flags, &level, &bias)) {
	  DNOTE("ao_setup() error\n");
          open_output(get_speaker_flags());
	  if(ao_setup(output, sample_rate, &flags, &level, &bias)) {
	    goto error;
	    //exit(1);
	  }
	  speaker_flags = flags;
	  DNOTE("ao_setup() averted\n");
	}
	
	if(speaker_flags == -1)
	  speaker_flags = flags;
	
	flags = speaker_flags;
	flags |= A52_ADJUST_LEVEL;
	memset(&state, 0, sizeof(a52_state_t));
	/* flags (speaker) [in/out] level [in/out] bias [in] */
	if(a52_frame(&state, buf, &flags, &level, bias)) {
	  DNOTE("a52_frame() error\n");
	  goto error;
	}

	if(disable_dynrng)
	  a52_dynrng(&state, NULL, NULL);
	for(i = 0; i < 6; i++) {
	  if(a52_block(&state, samples)) {
	    DNOTE("a52_block() error\n");
	    goto error;
	  }
	  /* flags (output from decoder) verified by ao_play */
	  if(ao_play(output, flags, samples)) {
	    int flags2;
	    sample_t level2, bias2; // Dummy parameters
	    DNOTE("ao_play() error\n");
	    /* re-open... */
	    open_output(get_speaker_flags());
	    /* re-setup (set sample rate) */
	    if(ao_setup(output, sample_rate, &flags2, &level2, &bias2))
	      goto error;
	    speaker_flags = flags;
	    /* set the output mode to what ever flags is (hopefully) */
	    if(ao_play(output, flags, samples))
	      goto error;
	    DNOTE("ao_play() averted\n");
	  }
	  blocks++;
	}
	bufptr = buf;
	bufpos = buf + 7;
	//print_fps(0);
	continue;
      error:
	print_error = 1;
	bufptr = buf;
	bufpos = buf + 7;
      }
    }
  }

  if(sample_rate != 0) {
    TIME_S(buf_time) = (int32_t)(((int64_t)(256*blocks)*(int64_t)CT_FRACTION/sample_rate) / CT_FRACTION);
    TIME_SS(buf_time) = (int32_t)(((int64_t)(256*blocks)*(int64_t)CT_FRACTION/sample_rate) % CT_FRACTION);
  } else {
    TIME_S(buf_time) = 0;
    TIME_SS(buf_time) = 0;
  }

  return buf_time;
}