initial commit
[goodguy/history.git] / cinelerra-5.0 / libzmpeg3 / a52dec-0.7.3 / libao / audio_out_aif.c
1 /*
2  * audio_out_aif.c
3  * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
4  * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
5  *
6  * This file is part of a52dec, a free ATSC A-52 stream decoder.
7  * See http://liba52.sourceforge.net/ for updates.
8  *
9  * a52dec 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 2 of the License, or
12  * (at your option) any later version.
13  *
14  * a52dec 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, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include "config.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <inttypes.h>
30
31 #include "a52.h"
32 #include "audio_out.h"
33 #include "audio_out_internal.h"
34
35 typedef struct aif_instance_s {
36     ao_instance_t ao;
37     int sample_rate;
38     int set_params;
39     int flags;
40     int size;
41 } aif_instance_t;
42
43 static uint8_t aif_header[] = {
44     'F', 'O', 'R', 'M', 0xff, 0xff, 0xff, 0xfe, 'A', 'I', 'F', 'F',
45     'C', 'O', 'M', 'M', 0, 0, 0, 18,
46     0, 2, 0x3f, 0xff, 0xff, 0xf4, 0, 16, 0x40, 0x0e, -1, -1, 0, 0, 0, 0, 0, 0,
47     'S', 'S', 'N', 'D', 0xff, 0xff, 0xff, 0xd8, 0, 0, 0, 0, 0, 0, 0, 0
48 };
49
50 int aif_setup (ao_instance_t * _instance, int sample_rate, int * flags,
51                sample_t * level, sample_t * bias)
52 {
53     aif_instance_t * instance = (aif_instance_t *) _instance;
54
55     if ((instance->set_params == 0) && (instance->sample_rate != sample_rate))
56         return 1;
57     instance->sample_rate = sample_rate;
58
59     *flags = instance->flags;
60     *level = 1;
61     *bias = 384;
62
63     return 0;
64 }
65
66 static void store4 (uint8_t * buf, int value)
67 {
68     buf[0] = value >> 24;
69     buf[1] = value >> 16;
70     buf[2] = value >> 8;
71     buf[3] = value;
72 }
73
74 static void store2 (uint8_t * buf, int16_t value)
75 {
76     buf[0] = value >> 8;
77     buf[1] = value;
78 }
79
80 int aif_play (ao_instance_t * _instance, int flags, sample_t * _samples)
81 {
82     aif_instance_t * instance = (aif_instance_t *) _instance;
83     int16_t int16_samples[256*2];
84
85 #ifdef LIBA52_DOUBLE
86     float samples[256 * 2];
87     int i;
88
89     for (i = 0; i < 256 * 2; i++)
90         samples[i] = _samples[i];
91 #else
92     float * samples = _samples;
93 #endif
94
95     if (instance->set_params) {
96         instance->set_params = 0;
97         store2 (aif_header + 30, instance->sample_rate);
98         fwrite (aif_header, sizeof (aif_header), 1, stdout);
99     }
100
101     float2s16_2 (samples, int16_samples);
102     s16_BE (int16_samples, 2);
103     fwrite (int16_samples, 256 * sizeof (int16_t) * 2, 1, stdout);
104
105     instance->size += 256 * sizeof (int16_t) * 2;
106
107     return 0;
108 }
109
110 void aif_close (ao_instance_t * _instance)
111 {
112     aif_instance_t * instance = (aif_instance_t *) _instance;
113
114     if (fseek (stdout, 0, SEEK_SET) < 0)
115         return;
116
117     store4 (aif_header + 4, instance->size + 46);
118     store4 (aif_header + 22, instance->size / 4);
119     store4 (aif_header + 42, instance->size + 8);
120     fwrite (aif_header, sizeof (aif_header), 1, stdout);
121 }
122
123 ao_instance_t * aif_open (int flags)
124 {
125     aif_instance_t * instance;
126
127     instance = malloc (sizeof (aif_instance_t));
128     if (instance == NULL)
129         return NULL;
130
131     instance->ao.setup = aif_setup;
132     instance->ao.play = aif_play;
133     instance->ao.close = aif_close;
134
135     instance->sample_rate = 0;
136     instance->set_params = 1;
137     instance->flags = flags;
138     instance->size = 0;
139
140     return (ao_instance_t *) instance;
141 }
142
143 ao_instance_t * ao_aif_open (void)
144 {
145     return aif_open (A52_STEREO);
146 }
147
148 ao_instance_t * ao_aifdolby_open (void)
149 {
150     return aif_open (A52_DOLBY);
151 }