2 This file is part of PulseAudio.
4 PulseAudio is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published
6 by the Free Software Foundation; either version 2.1 of the License,
7 or (at your option) any later version.
9 PulseAudio is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with PulseAudio; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 #include "core-format.h"
26 #include <pulse/def.h>
27 #include <pulse/xmalloc.h>
29 #include <pulsecore/macro.h>
31 int pa_format_info_get_sample_format(pa_format_info
*f
, pa_sample_format_t
*sf
) {
34 pa_sample_format_t sf_local
;
39 r
= pa_format_info_get_prop_string(f
, PA_PROP_FORMAT_SAMPLE_FORMAT
, &sf_str
);
43 sf_local
= pa_parse_sample_format(sf_str
);
46 if (!pa_sample_format_valid(sf_local
)) {
47 pa_log_debug("Invalid sample format.");
48 return -PA_ERR_INVALID
;
56 int pa_format_info_get_rate(pa_format_info
*f
, uint32_t *rate
) {
63 r
= pa_format_info_get_prop_int(f
, PA_PROP_FORMAT_RATE
, &rate_local
);
67 if (!pa_sample_rate_valid(rate_local
)) {
68 pa_log_debug("Invalid sample rate: %i", rate_local
);
69 return -PA_ERR_INVALID
;
77 int pa_format_info_get_channels(pa_format_info
*f
, uint8_t *channels
) {
84 r
= pa_format_info_get_prop_int(f
, PA_PROP_FORMAT_CHANNELS
, &channels_local
);
88 if (!pa_channels_valid(channels_local
)) {
89 pa_log_debug("Invalid channel count: %i", channels_local
);
90 return -PA_ERR_INVALID
;
93 *channels
= channels_local
;
98 int pa_format_info_get_channel_map(pa_format_info
*f
, pa_channel_map
*map
) {
105 r
= pa_format_info_get_prop_string(f
, PA_PROP_FORMAT_CHANNEL_MAP
, &map_str
);
109 map
= pa_channel_map_parse(map
, map_str
);
113 pa_log_debug("Failed to parse channel map.");
114 return -PA_ERR_INVALID
;
120 int pa_format_info_to_sample_spec2(pa_format_info
*f
, pa_sample_spec
*ss
, pa_channel_map
*map
, pa_sample_spec
*fallback_ss
,
121 pa_channel_map
*fallback_map
) {
123 pa_sample_spec ss_local
;
124 pa_channel_map map_local
;
129 pa_assert(fallback_ss
);
130 pa_assert(fallback_map
);
132 if (!pa_format_info_is_pcm(f
))
133 return pa_format_info_to_sample_spec_fake(f
, ss
, map
);
135 r
= pa_format_info_get_sample_format(f
, &ss_local
.format
);
136 if (r
== -PA_ERR_NOENTITY
)
137 ss_local
.format
= fallback_ss
->format
;
141 pa_assert(pa_sample_format_valid(ss_local
.format
));
143 r
= pa_format_info_get_rate(f
, &ss_local
.rate
);
144 if (r
== -PA_ERR_NOENTITY
)
145 ss_local
.rate
= fallback_ss
->rate
;
149 pa_assert(pa_sample_rate_valid(ss_local
.rate
));
151 r
= pa_format_info_get_channels(f
, &ss_local
.channels
);
152 r2
= pa_format_info_get_channel_map(f
, &map_local
);
153 if (r
== -PA_ERR_NOENTITY
&& r2
>= 0)
154 ss_local
.channels
= map_local
.channels
;
155 else if (r
== -PA_ERR_NOENTITY
)
156 ss_local
.channels
= fallback_ss
->channels
;
160 pa_assert(pa_channels_valid(ss_local
.channels
));
162 if (r2
>= 0 && map_local
.channels
!= ss_local
.channels
) {
163 pa_log_debug("Channel map is not compatible with the sample spec.");
164 return -PA_ERR_INVALID
;
167 if (r2
== -PA_ERR_NOENTITY
) {
168 if (fallback_map
->channels
== ss_local
.channels
)
169 map_local
= *fallback_map
;
171 pa_channel_map_init_extend(&map_local
, ss_local
.channels
, PA_CHANNEL_MAP_DEFAULT
);
175 pa_assert(pa_channel_map_valid(&map_local
));
176 pa_assert(ss_local
.channels
== map_local
.channels
);
184 int pa_format_info_to_sample_spec_fake(pa_format_info
*f
, pa_sample_spec
*ss
, pa_channel_map
*map
) {
190 /* Note: When we add support for non-IEC61937 encapsulated compressed
191 * formats, this function should return a non-zero values for these. */
193 ss
->format
= PA_SAMPLE_S16LE
;
197 pa_channel_map_init_stereo(map
);
199 pa_return_val_if_fail(pa_format_info_get_prop_int(f
, PA_PROP_FORMAT_RATE
, &rate
) == 0, -PA_ERR_INVALID
);
200 ss
->rate
= (uint32_t) rate
;
202 if (f
->encoding
== PA_ENCODING_EAC3_IEC61937
)