~ubuntu-branches/debian/jessie/libsndfile/jessie

« back to all changes in this revision

Viewing changes to src/broadcast.c

  • Committer: Bazaar Package Importer
  • Author(s): Samuel Mimram
  • Date: 2009-02-17 19:21:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20090217192103-7rhu1n8p79jnbzy3
Tags: 1.0.18-2
Add missing build-dependencies on pkg-config and libvorbis-dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
** Copyright (C) 2006 Paul Davis <paul@linuxaudiosystems.com>
3
 
** Copyright (C) 2006 Erik de Castro Lopo <erikd@mega-nerd.com>
 
3
** Copyright (C) 2006-2009 Erik de Castro Lopo <erikd@mega-nerd.com>
4
4
**
5
5
** This program is free software; you can redistribute it and/or modify
6
6
** it under the terms of the GNU Lesser General Public License as published by
18
18
*/
19
19
 
20
20
#include <stdio.h>
 
21
#include <stddef.h>
21
22
#include <string.h>
22
23
 
23
24
#include "common.h"
24
25
 
25
 
/*
26
 
** Allocate and initialize a broadcast info structure.
27
 
*/
28
 
 
29
 
SF_BROADCAST_INFO*
30
 
broadcast_info_alloc (void)
31
 
{       SF_BROADCAST_INFO* bext ;
32
 
 
33
 
        if ((bext = calloc (1, sizeof (SF_BROADCAST_INFO))) == NULL)
34
 
                return NULL ;
35
 
 
36
 
        return bext ;
37
 
} /* broadcast_info_alloc */
 
26
static void strncpy_crlf (char *dest, const char *src, size_t destmax, size_t srcmax) ;
 
27
static int gen_coding_history (char * added_history, int added_history_max, const SF_INFO * psfinfo) ;
 
28
 
 
29
static inline size_t
 
30
bc_min_size (const SF_BROADCAST_INFO* info)
 
31
{       if (info == NULL)
 
32
                return 0 ;
 
33
 
 
34
        return offsetof (SF_BROADCAST_INFO, coding_history) + info->coding_history_size ;
 
35
} /* broadcast_size */
 
36
 
 
37
 
 
38
static inline size_t
 
39
bc_var_coding_hist_size (const SF_BROADCAST_VAR* var)
 
40
{       return var->size - offsetof (SF_BROADCAST_VAR, binfo.coding_history) ;
 
41
} /* broadcast_size */
 
42
 
 
43
SF_BROADCAST_VAR*
 
44
broadcast_var_alloc (size_t datasize)
 
45
{       SF_BROADCAST_VAR * data ;
 
46
 
 
47
        if ((data = calloc (1, datasize)) != NULL)
 
48
                data->size = datasize ;
 
49
 
 
50
        return data ;
 
51
} /* broadcast_var_alloc */
 
52
 
38
53
 
39
54
int
40
 
broadcast_info_copy (SF_BROADCAST_INFO* dst, SF_BROADCAST_INFO* src)
41
 
{       memcpy (dst, src, sizeof (SF_BROADCAST_INFO)) ;
 
55
broadcast_var_set (SF_PRIVATE *psf, const SF_BROADCAST_INFO * info, size_t datasize)
 
56
{       char added_history [256] ;
 
57
        int added_history_len, len ;
 
58
 
 
59
        if (info == NULL)
 
60
                return SF_FALSE ;
 
61
 
 
62
        if (bc_min_size (info) > datasize)
 
63
        {       psf->error = SFE_BAD_BROADCAST_INFO_SIZE ;
 
64
                return SF_FALSE ;
 
65
                } ;
 
66
 
 
67
        added_history_len = gen_coding_history (added_history, sizeof (added_history), &(psf->sf)) ;
 
68
 
 
69
        if (psf->broadcast_var != NULL)
 
70
        {       size_t coding_hist_offset = offsetof (SF_BROADCAST_INFO, coding_history) ;
 
71
 
 
72
                if (psf->broadcast_var->binfo.coding_history_size + added_history_len < datasize - coding_hist_offset)
 
73
                {       free (psf->broadcast_var) ;
 
74
                        psf->broadcast_var = NULL ;
 
75
                        } ;
 
76
                } ;
 
77
 
 
78
        if (psf->broadcast_var == NULL)
 
79
        {       int size = datasize + added_history_len + 512 ;
 
80
 
 
81
                psf->broadcast_var = calloc (1, size) ;
 
82
                psf->broadcast_var->size = size ;
 
83
                } ;
 
84
 
 
85
        memcpy (&(psf->broadcast_var->binfo), info, offsetof (SF_BROADCAST_INFO, coding_history)) ;
 
86
 
 
87
        strncpy_crlf (psf->broadcast_var->binfo.coding_history, info->coding_history, bc_var_coding_hist_size (psf->broadcast_var), info->coding_history_size) ;
 
88
        len = strlen (psf->broadcast_var->binfo.coding_history) ;
 
89
 
 
90
        if (len > 0 && psf->broadcast_var->binfo.coding_history [len] != '\n')
 
91
                strncat (psf->broadcast_var->binfo.coding_history, "\r\n", 2) ;
 
92
 
 
93
        if (psf->mode == SFM_WRITE)
 
94
                strncat (psf->broadcast_var->binfo.coding_history, added_history, strlen (added_history)) ;
 
95
 
 
96
        psf->broadcast_var->binfo.coding_history_size = strlen (psf->broadcast_var->binfo.coding_history) ;
 
97
 
 
98
        /* Fore coding_history_size to be even. */
 
99
        psf->broadcast_var->binfo.coding_history_size += (psf->broadcast_var->binfo.coding_history_size & 1) ? 1 : 0 ;
42
100
 
43
101
        /* Currently writing this version. */
44
 
        dst->version = 1 ;
 
102
        psf->broadcast_var->binfo.version = 1 ;
45
103
 
46
104
        return SF_TRUE ;
47
 
} /* broadcast_info_copy */
 
105
} /* broadcast_var_set */
 
106
 
48
107
 
49
108
int
50
 
broadcast_add_coding_history (SF_BROADCAST_INFO* bext, unsigned int channels, unsigned int samplerate)
 
109
broadcast_var_get (SF_PRIVATE *psf, SF_BROADCAST_INFO * data, size_t datasize)
 
110
{       size_t size ;
 
111
 
 
112
        if (psf->broadcast_var == NULL)
 
113
                return SF_FALSE ;
 
114
 
 
115
        size = SF_MIN (datasize, bc_min_size (&(psf->broadcast_var->binfo))) ;
 
116
 
 
117
        memcpy (data, &(psf->broadcast_var->binfo), size) ;
 
118
 
 
119
        return SF_TRUE ;
 
120
} /* broadcast_var_set */
 
121
 
 
122
/*------------------------------------------------------------------------------
 
123
**      Strncpy which converts all line endings to CR/LF.
 
124
*/
 
125
 
 
126
static void
 
127
strncpy_crlf (char *dest, const char *src, size_t destmax, size_t srcmax)
 
128
{       char * destend = dest + destmax - 1 ;
 
129
        const char * srcend = src + srcmax ;
 
130
 
 
131
        while (dest < destend && src < srcend)
 
132
        {       if ((src [0] == '\r' && src [1] == '\n') || (src [0] == '\n' && src [1] == '\r'))
 
133
                {       *dest++ = '\r' ;
 
134
                        *dest++ = '\n' ;
 
135
                        src += 2 ;
 
136
                        continue ;
 
137
                        } ;
 
138
 
 
139
                if (src [0] == '\r')
 
140
                {       *dest++ = '\r' ;
 
141
                        *dest++ = '\n' ;
 
142
                        src += 1 ;
 
143
                        continue ;
 
144
                        } ;
 
145
 
 
146
                if (src [0] == '\n')
 
147
                {       *dest++ = '\r' ;
 
148
                        *dest++ = '\n' ;
 
149
                        src += 1 ;
 
150
                        continue ;
 
151
                        } ;
 
152
 
 
153
                *dest++ = *src++ ;
 
154
                } ;
 
155
 
 
156
        /* Make sure dest is terminated. */
 
157
        *dest = 0 ;
 
158
} /* strncpy_crlf */
 
159
 
 
160
static int
 
161
gen_coding_history (char * added_history, int added_history_max, const SF_INFO * psfinfo)
51
162
{       char chnstr [16] ;
52
 
        int count ;
53
 
 
54
 
        switch (channels)
 
163
        int count, width ;
 
164
 
 
165
        /*
 
166
        **      From : http://www.sr.se/utveckling/tu/bwf/docs/codhist2.htm
 
167
        **
 
168
        **      Parameter            Variable string <allowed option>                 Unit
 
169
        **      ==========================================================================================
 
170
        **      Coding Algorithm     A=<ANALOGUE, PCM, MPEG1L1, MPEG1L2, MPEG1L3,
 
171
        **                           MPEG2L1, MPEG2L2, MPEG2L3>
 
172
        **      Sampling frequency   F=<11000,22050,24000,32000,44100,48000>          [Hz]
 
173
        **      Bit-rate             B=<any bit-rate allowed in MPEG 2 (ISO/IEC       [kbit/s per channel]
 
174
        **                           13818-3)>
 
175
        **      Word Length          W=<8, 12, 14, 16, 18, 20, 22, 24>                [bits]
 
176
        **      Mode                 M=<mono, stereo, dual-mono, joint-stereo>
 
177
        **      Text, free string    T=<a free ASCII-text string for in house use.
 
178
        **                           This string should contain no commas (ASCII
 
179
        **                           2Chex). Examples of the contents: ID-No; codec
 
180
        **                           type; A/D type>
 
181
        */
 
182
 
 
183
        switch (psfinfo->channels)
55
184
        {       case 0 :
56
185
                        return SF_FALSE ;
57
186
 
63
192
                        strncpy (chnstr, "stereo", sizeof (chnstr)) ;
64
193
                        break ;
65
194
 
66
 
        default :
67
 
                LSF_SNPRINTF (chnstr, sizeof (chnstr), "%uchn", channels) ;
68
 
                break ;
69
 
        }
70
 
 
71
 
        count = LSF_SNPRINTF (bext->coding_history, sizeof (bext->coding_history), "F=%u,A=PCM,M=%s,W=24,T=%s-%s", samplerate, chnstr, PACKAGE, VERSION) ;
72
 
 
73
 
        if (count >= SIGNED_SIZEOF (bext->coding_history))
74
 
                bext->coding_history_size = sizeof (bext->coding_history) ;
75
 
        else
76
 
        {       count += count & 1 ;
77
 
                bext->coding_history_size = count ;
78
 
                } ;
79
 
 
80
 
        return SF_TRUE ;
81
 
} /* broadcast_add_coding_history */
82
 
 
83
 
/*
84
 
** Do not edit or modify anything in this comment block.
85
 
** The following line is a file identity tag for the GNU Arch
86
 
** revision control system.
87
 
**
88
 
** arch-tag: 4b3b69c7-d710-4424-9da0-5048534a0beb
89
 
*/
 
195
                default :
 
196
                        LSF_SNPRINTF (chnstr, sizeof (chnstr), "%uchn", psfinfo->channels) ;
 
197
                        break ;
 
198
                } ;
 
199
 
 
200
        switch (SF_CODEC (psfinfo->format))
 
201
        {       case SF_FORMAT_PCM_U8 :
 
202
                case SF_FORMAT_PCM_S8 :
 
203
                        width = 8 ;
 
204
                        break ;
 
205
                case SF_FORMAT_PCM_16 :
 
206
                        width = 16 ;
 
207
                        break ;
 
208
                case SF_FORMAT_PCM_24 :
 
209
                        width = 24 ;
 
210
                        break ;
 
211
                case SF_FORMAT_PCM_32 :
 
212
                        width = 32 ;
 
213
                        break ;
 
214
                case SF_FORMAT_FLOAT :
 
215
                        width = 24 ; /* Bits in the mantissa + 1 */
 
216
                        break ;
 
217
                case SF_FORMAT_DOUBLE :
 
218
                        width = 53 ; /* Bits in the mantissa + 1 */
 
219
                        break ;
 
220
                case SF_FORMAT_ULAW :
 
221
                case SF_FORMAT_ALAW :
 
222
                        width = 12 ;
 
223
                        break ;
 
224
                default :
 
225
                        width = 42 ;
 
226
                        break ;
 
227
                } ;
 
228
 
 
229
        count = LSF_SNPRINTF (added_history, added_history_max,
 
230
                                                        "A=PCM,F=%u,W=%hu,M=%s,T=%s-%s\r\n",
 
231
                                                        psfinfo->samplerate, width, chnstr, PACKAGE, VERSION) ;
 
232
 
 
233
        if (count >= added_history_max)
 
234
                return 0 ;
 
235
 
 
236
        return count ;
 
237
} /* gen_coding_history */
 
238