|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 /* GStreamer Multichannel-Audio helper functions |
|
18 * (c) 2004 Ronald Bultje <rbultje@ronald.bitfreak.net> |
|
19 * |
|
20 * This library is free software; you can redistribute it and/or |
|
21 * modify it under the terms of the GNU Library General Public |
|
22 * License as published by the Free Software Foundation; either |
|
23 * version 2 of the License, or (at your option) any later version. |
|
24 * |
|
25 * This library is distributed in the hope that it will be useful, |
|
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
28 * Library General Public License for more details. |
|
29 * |
|
30 * You should have received a copy of the GNU Library General Public |
|
31 * License along with this library; if not, write to the |
|
32 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|
33 * Boston, MA 02111-1307, USA. |
|
34 */ |
|
35 |
|
36 #ifdef HAVE_CONFIG_H |
|
37 #include <config.h> |
|
38 #endif |
|
39 |
|
40 #include "multichannel.h" |
|
41 |
|
42 #define GST_AUDIO_CHANNEL_POSITIONS_FIELD_NAME "channel-positions" |
|
43 |
|
44 /* |
|
45 * This function checks if basic assumptions apply: |
|
46 * - does each position occur at most once? |
|
47 * - do conflicting positions occur? |
|
48 * + front_mono vs. front_left/right |
|
49 * + front_center vs. front_left/right_of_center |
|
50 * + rear_center vs. rear_left/right |
|
51 * It also adds some hacks that 0.8.x needs for compatibility: |
|
52 * - if channels == 1, are we really mono? |
|
53 * - if channels == 2, are we really stereo? |
|
54 */ |
|
55 |
|
56 static gboolean |
|
57 gst_audio_check_channel_positions (const GstAudioChannelPosition * pos, |
|
58 gint channels) |
|
59 { |
|
60 gint i, n; |
|
61 const struct |
|
62 { |
|
63 const GstAudioChannelPosition pos1[2]; |
|
64 const GstAudioChannelPosition pos2[1]; |
|
65 } conf[] = { |
|
66 /* front: mono <-> stereo */ |
|
67 { { |
|
68 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT, |
|
69 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT}, { |
|
70 GST_AUDIO_CHANNEL_POSITION_FRONT_MONO}}, |
|
71 /* front center: 2 <-> 1 */ |
|
72 { { |
|
73 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER, |
|
74 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER}, { |
|
75 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER}}, |
|
76 /* rear: 2 <-> 1 */ |
|
77 { { |
|
78 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT, |
|
79 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT}, { |
|
80 GST_AUDIO_CHANNEL_POSITION_REAR_CENTER}}, { { |
|
81 GST_AUDIO_CHANNEL_POSITION_INVALID}} |
|
82 }; |
|
83 |
|
84 g_assert (pos != NULL && channels > 0); |
|
85 |
|
86 /* check for invalid channel positions */ |
|
87 for (n = 0; n < channels; n++) { |
|
88 if (pos[n] <= GST_AUDIO_CHANNEL_POSITION_INVALID || |
|
89 pos[n] >= GST_AUDIO_CHANNEL_POSITION_NUM) { |
|
90 g_warning ("Channel position %d for channel %d is invalid", pos[n], n); |
|
91 return FALSE; |
|
92 } |
|
93 } |
|
94 |
|
95 /* either all channel positions are NONE or all are defined, |
|
96 * but having only some channel positions NONE and others not |
|
97 * is not allowed */ |
|
98 if (pos[0] == GST_AUDIO_CHANNEL_POSITION_NONE) { |
|
99 for (n = 1; n < channels; ++n) { |
|
100 if (pos[n] != GST_AUDIO_CHANNEL_POSITION_NONE) { |
|
101 g_warning ("Either all channel positions must be defined, or all " |
|
102 "be set to NONE, having only some defined is not allowed"); |
|
103 return FALSE; |
|
104 } |
|
105 } |
|
106 /* all positions are NONE, we are done here */ |
|
107 return TRUE; |
|
108 } |
|
109 |
|
110 /* check for multiple position occurrences */ |
|
111 for (i = GST_AUDIO_CHANNEL_POSITION_INVALID + 1; |
|
112 i < GST_AUDIO_CHANNEL_POSITION_NUM; i++) { |
|
113 gint count = 0; |
|
114 |
|
115 for (n = 0; n < channels; n++) { |
|
116 if (pos[n] == i) |
|
117 count++; |
|
118 } |
|
119 |
|
120 /* NONE may not occur mixed with other channel positions */ |
|
121 if (i == GST_AUDIO_CHANNEL_POSITION_NONE && count > 0) { |
|
122 g_warning ("Either all channel positions must be defined, or all " |
|
123 "be set to NONE, having only some defined is not allowed"); |
|
124 return FALSE; |
|
125 } |
|
126 |
|
127 if (count > 1) { |
|
128 g_warning ("Channel position %d occurred %d times, not allowed", |
|
129 i, count); |
|
130 return FALSE; |
|
131 } |
|
132 } |
|
133 |
|
134 /* check for position conflicts */ |
|
135 for (i = 0; conf[i].pos1[0] != GST_AUDIO_CHANNEL_POSITION_INVALID; i++) { |
|
136 gboolean found1 = FALSE, found2 = FALSE; |
|
137 |
|
138 for (n = 0; n < channels; n++) { |
|
139 if (pos[n] == conf[i].pos1[0] || pos[n] == conf[i].pos1[1]) |
|
140 found1 = TRUE; |
|
141 else if (pos[n] == conf[i].pos2[0]) |
|
142 found2 = TRUE; |
|
143 } |
|
144 |
|
145 if (found1 && found2) { |
|
146 g_warning ("Found conflicting channel positions %d/%d and %d", |
|
147 conf[i].pos1[0], conf[i].pos1[1], conf[i].pos2[0]); |
|
148 return FALSE; |
|
149 } |
|
150 } |
|
151 |
|
152 /* Throw warning if we encounter an unusual 2-channel configuration, |
|
153 * at least until someone finds a reason why we should not */ |
|
154 if (channels == 2 && (pos[0] != GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT || |
|
155 pos[1] != GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT)) { |
|
156 g_warning ("channels=2 implies stereo, but channel positions are " |
|
157 "< %d, %d>", pos[0], pos[1]); |
|
158 return FALSE; |
|
159 } |
|
160 |
|
161 return TRUE; |
|
162 } |
|
163 |
|
164 /* FIXME: these default positions may or may not be correct. In any |
|
165 * case, they are mostly just a fallback for buggy plugins, so it |
|
166 * should not really matter too much */ |
|
167 #define NUM_DEF_CHANS 8 |
|
168 static const GstAudioChannelPosition |
|
169 default_positions[NUM_DEF_CHANS][NUM_DEF_CHANS] = { |
|
170 /* 1 channel */ |
|
171 { |
|
172 GST_AUDIO_CHANNEL_POSITION_FRONT_MONO, |
|
173 }, |
|
174 /* 2 channels */ |
|
175 { |
|
176 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT, |
|
177 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT, |
|
178 }, |
|
179 /* 3 channels (2.1) */ |
|
180 { |
|
181 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT, |
|
182 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT, |
|
183 GST_AUDIO_CHANNEL_POSITION_LFE, /* or FRONT_CENTER for 3.0? */ |
|
184 }, |
|
185 /* 4 channels (4.0 or 3.1?) */ |
|
186 { |
|
187 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT, |
|
188 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT, |
|
189 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT, |
|
190 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT, |
|
191 }, |
|
192 /* 5 channels */ |
|
193 { |
|
194 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT, |
|
195 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT, |
|
196 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT, |
|
197 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT, |
|
198 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER, |
|
199 }, |
|
200 /* 6 channels */ |
|
201 { |
|
202 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT, |
|
203 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT, |
|
204 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT, |
|
205 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT, |
|
206 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER, |
|
207 GST_AUDIO_CHANNEL_POSITION_LFE, |
|
208 }, |
|
209 /* 7 channels */ |
|
210 { |
|
211 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT, |
|
212 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT, |
|
213 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT, |
|
214 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT, |
|
215 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER, |
|
216 GST_AUDIO_CHANNEL_POSITION_LFE, |
|
217 GST_AUDIO_CHANNEL_POSITION_REAR_CENTER, |
|
218 }, |
|
219 /* 8 channels */ |
|
220 { |
|
221 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT, |
|
222 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT, |
|
223 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT, |
|
224 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT, |
|
225 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER, |
|
226 GST_AUDIO_CHANNEL_POSITION_LFE, |
|
227 GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT, |
|
228 GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT, |
|
229 } |
|
230 }; |
|
231 |
|
232 /** |
|
233 * gst_audio_get_channel_positions: |
|
234 * @str: A #GstStructure to retrieve channel positions from. |
|
235 * |
|
236 * Retrieves a number of (fixed!) audio channel positions from |
|
237 * the provided #GstStructure and returns it as a newly allocated |
|
238 * array. The caller should g_free () this array. The caller |
|
239 * should also check that the members in this #GstStructure are |
|
240 * indeed "fixed" before calling this function. |
|
241 * |
|
242 * Returns: a newly allocated array containing the channel |
|
243 * positions as provided in the given #GstStructure. Returns |
|
244 * NULL on error. |
|
245 */ |
|
246 #ifdef __SYMBIAN32__ |
|
247 EXPORT_C |
|
248 #endif |
|
249 |
|
250 |
|
251 GstAudioChannelPosition * |
|
252 gst_audio_get_channel_positions (GstStructure * str) |
|
253 { |
|
254 GstAudioChannelPosition *pos; |
|
255 gint channels, n; |
|
256 const GValue *pos_val_arr, *pos_val_entry; |
|
257 gboolean res; |
|
258 GType t; |
|
259 |
|
260 /* get number of channels, general type checkups */ |
|
261 g_return_val_if_fail (str != NULL, NULL); |
|
262 res = gst_structure_get_int (str, "channels", &channels); |
|
263 g_return_val_if_fail (res, NULL); |
|
264 g_return_val_if_fail (channels > 0, NULL); |
|
265 pos_val_arr = gst_structure_get_value (str, |
|
266 GST_AUDIO_CHANNEL_POSITIONS_FIELD_NAME); |
|
267 |
|
268 /* The following checks are here to retain compatibility for plugins not |
|
269 * implementing this field. They expect that channels=1 implies mono |
|
270 * and channels=2 implies stereo, so we follow that. */ |
|
271 if (pos_val_arr == NULL) { |
|
272 /* channel layouts for 1 and 2 channels are implicit, don't warn */ |
|
273 if (channels > 2) { |
|
274 g_warning ("Failed to retrieve channel layout from caps. This usually " |
|
275 "means there is a GStreamer element that does not implement " |
|
276 "multichannel audio correctly. Please file a bug."); |
|
277 } |
|
278 |
|
279 /* just return some default channel layout if we have one */ |
|
280 if (channels >= 1 && channels <= NUM_DEF_CHANS) { |
|
281 const GstAudioChannelPosition *p; |
|
282 |
|
283 p = default_positions[channels - 1]; |
|
284 return g_memdup (p, channels * sizeof (GstAudioChannelPosition)); |
|
285 } |
|
286 |
|
287 return NULL; |
|
288 } |
|
289 |
|
290 g_return_val_if_fail (gst_value_array_get_size (pos_val_arr) == channels, |
|
291 NULL); |
|
292 for (n = 0; n < channels; n++) { |
|
293 t = G_VALUE_TYPE (gst_value_array_get_value (pos_val_arr, n)); |
|
294 g_return_val_if_fail (t == GST_TYPE_AUDIO_CHANNEL_POSITION, NULL); |
|
295 } |
|
296 |
|
297 /* ... and fill array */ |
|
298 pos = g_new (GstAudioChannelPosition, channels); |
|
299 for (n = 0; n < channels; n++) { |
|
300 pos_val_entry = gst_value_array_get_value (pos_val_arr, n); |
|
301 pos[n] = g_value_get_enum (pos_val_entry); |
|
302 } |
|
303 |
|
304 if (!gst_audio_check_channel_positions (pos, channels)) { |
|
305 g_free (pos); |
|
306 return NULL; |
|
307 } |
|
308 |
|
309 return pos; |
|
310 } |
|
311 |
|
312 /** |
|
313 * gst_audio_set_channel_positions: |
|
314 * @str: A #GstStructure to set channel positions on. |
|
315 * @pos: an array of channel positions. The number of members |
|
316 * in this array should be equal to the (fixed!) number |
|
317 * of the "channels" field in the given #GstStructure. |
|
318 * |
|
319 * Adds a "channel-positions" field to the given #GstStructure, |
|
320 * which will represent the channel positions as given in the |
|
321 * provided #GstAudioChannelPosition array. |
|
322 */ |
|
323 #ifdef __SYMBIAN32__ |
|
324 EXPORT_C |
|
325 #endif |
|
326 |
|
327 |
|
328 void |
|
329 gst_audio_set_channel_positions (GstStructure * str, |
|
330 const GstAudioChannelPosition * pos) |
|
331 { |
|
332 GValue pos_val_arr = { 0 }, pos_val_entry = { |
|
333 0}; |
|
334 gint channels, n; |
|
335 gboolean res; |
|
336 |
|
337 /* get number of channels, checkups */ |
|
338 g_return_if_fail (str != NULL); |
|
339 g_return_if_fail (pos != NULL); |
|
340 res = gst_structure_get_int (str, "channels", &channels); |
|
341 g_return_if_fail (res); |
|
342 g_return_if_fail (channels > 0); |
|
343 if (!gst_audio_check_channel_positions (pos, channels)) |
|
344 return; |
|
345 |
|
346 /* build gvaluearray from positions */ |
|
347 g_value_init (&pos_val_entry, GST_TYPE_AUDIO_CHANNEL_POSITION); |
|
348 g_value_init (&pos_val_arr, GST_TYPE_ARRAY); |
|
349 for (n = 0; n < channels; n++) { |
|
350 g_value_set_enum (&pos_val_entry, pos[n]); |
|
351 gst_value_array_append_value (&pos_val_arr, &pos_val_entry); |
|
352 } |
|
353 g_value_unset (&pos_val_entry); |
|
354 |
|
355 /* add to structure */ |
|
356 gst_structure_set_value (str, |
|
357 GST_AUDIO_CHANNEL_POSITIONS_FIELD_NAME, &pos_val_arr); |
|
358 g_value_unset (&pos_val_arr); |
|
359 } |
|
360 |
|
361 /** |
|
362 * gst_audio_set_structure_channel_positions_list: |
|
363 * @str: #GstStructure to set the list of channel positions |
|
364 * on. |
|
365 * @pos: the array containing one or more possible audio |
|
366 * channel positions that we should add in each value |
|
367 * of the array in the given structure. |
|
368 * @num_positions: the number of values in pos. |
|
369 * |
|
370 * Sets a (possibly non-fixed) list of possible audio channel |
|
371 * positions (given in pos) on the given structure. The |
|
372 * structure, after this function has been called, will contain |
|
373 * a "channel-positions" field with an array of the size of |
|
374 * the "channels" field value in the given structure (note |
|
375 * that this means that the channels field in the provided |
|
376 * structure should be fixed!). Each value in the array will |
|
377 * contain each of the values given in the pos array. |
|
378 */ |
|
379 #ifdef __SYMBIAN32__ |
|
380 EXPORT_C |
|
381 #endif |
|
382 |
|
383 |
|
384 void |
|
385 gst_audio_set_structure_channel_positions_list (GstStructure * str, |
|
386 const GstAudioChannelPosition * pos, gint num_positions) |
|
387 { |
|
388 gint channels, n, c; |
|
389 GValue pos_val_arr = { 0 }, pos_val_list = { |
|
390 0}, pos_val_entry = { |
|
391 0}; |
|
392 gboolean res; |
|
393 |
|
394 /* get number of channels, general type checkups */ |
|
395 g_return_if_fail (str != NULL); |
|
396 g_return_if_fail (num_positions > 0); |
|
397 g_return_if_fail (pos != NULL); |
|
398 res = gst_structure_get_int (str, "channels", &channels); |
|
399 g_return_if_fail (res); |
|
400 g_return_if_fail (channels > 0); |
|
401 |
|
402 /* 0.8.x: channels=1 or channels=2 is mono/stereo, no positions needed |
|
403 * there (we discard them anyway) */ |
|
404 if (channels == 1 || channels == 2) |
|
405 return; |
|
406 |
|
407 /* create the array of lists */ |
|
408 g_value_init (&pos_val_arr, GST_TYPE_ARRAY); |
|
409 g_value_init (&pos_val_entry, GST_TYPE_AUDIO_CHANNEL_POSITION); |
|
410 for (n = 0; n < channels; n++) { |
|
411 g_value_init (&pos_val_list, GST_TYPE_LIST); |
|
412 for (c = 0; c < num_positions; c++) { |
|
413 g_value_set_enum (&pos_val_entry, pos[c]); |
|
414 gst_value_list_append_value (&pos_val_list, &pos_val_entry); |
|
415 } |
|
416 gst_value_array_append_value (&pos_val_arr, &pos_val_list); |
|
417 g_value_unset (&pos_val_list); |
|
418 } |
|
419 g_value_unset (&pos_val_entry); |
|
420 gst_structure_set_value (str, GST_AUDIO_CHANNEL_POSITIONS_FIELD_NAME, |
|
421 &pos_val_arr); |
|
422 g_value_unset (&pos_val_arr); |
|
423 } |
|
424 |
|
425 /* |
|
426 * Helper function for below. The structure will be conserved, |
|
427 * but might be cut down. Any additional structures that were |
|
428 * created will be stored in the returned caps. |
|
429 */ |
|
430 |
|
431 static GstCaps * |
|
432 add_list_to_struct (GstStructure * str, |
|
433 const GstAudioChannelPosition * pos, gint num_positions) |
|
434 { |
|
435 GstCaps *caps = gst_caps_new_empty (); |
|
436 const GValue *chan_val; |
|
437 |
|
438 chan_val = gst_structure_get_value (str, "channels"); |
|
439 if (G_VALUE_TYPE (chan_val) == G_TYPE_INT) { |
|
440 gst_audio_set_structure_channel_positions_list (str, pos, num_positions); |
|
441 } else if (G_VALUE_TYPE (chan_val) == GST_TYPE_LIST) { |
|
442 gint size; |
|
443 const GValue *sub_val; |
|
444 |
|
445 size = gst_value_list_get_size (chan_val); |
|
446 sub_val = gst_value_list_get_value (chan_val, 0); |
|
447 gst_structure_set_value (str, "channels", sub_val); |
|
448 gst_caps_append (caps, add_list_to_struct (str, pos, num_positions)); |
|
449 while (--size > 0) { |
|
450 str = gst_structure_copy (str); |
|
451 sub_val = gst_value_list_get_value (chan_val, size); |
|
452 gst_structure_set_value (str, "channels", sub_val); |
|
453 gst_caps_append (caps, add_list_to_struct (str, pos, num_positions)); |
|
454 gst_caps_append_structure (caps, str); |
|
455 } |
|
456 } else if (G_VALUE_TYPE (chan_val) == GST_TYPE_INT_RANGE) { |
|
457 gint min, max; |
|
458 |
|
459 min = gst_value_get_int_range_min (chan_val); |
|
460 max = gst_value_get_int_range_max (chan_val); |
|
461 |
|
462 gst_structure_set (str, "channels", G_TYPE_INT, min, NULL); |
|
463 gst_audio_set_structure_channel_positions_list (str, pos, num_positions); |
|
464 for (++min; min < max; min++) { |
|
465 str = gst_structure_copy (str); |
|
466 gst_structure_set (str, "channels", G_TYPE_INT, min, NULL); |
|
467 gst_audio_set_structure_channel_positions_list (str, pos, num_positions); |
|
468 gst_caps_append_structure (caps, str); |
|
469 } |
|
470 } else { |
|
471 g_warning ("Unexpected value type '%s' for channels field", |
|
472 GST_STR_NULL (g_type_name (G_VALUE_TYPE (chan_val)))); |
|
473 } |
|
474 |
|
475 return caps; |
|
476 } |
|
477 |
|
478 /** |
|
479 * gst_audio_set_caps_channel_positions_list: |
|
480 * @caps: #GstCaps to set the list of channel positions on. |
|
481 * @pos: the array containing one or more possible audio |
|
482 * channel positions that we should add in each value |
|
483 * of the array in the given structure. |
|
484 * @num_positions: the number of values in pos. |
|
485 * |
|
486 * Sets a (possibly non-fixed) list of possible audio channel |
|
487 * positions (given in pos) on the given caps. Each of the |
|
488 * structures of the caps, after this function has been called, |
|
489 * will contain a "channel-positions" field with an array. |
|
490 * Each value in the array will contain each of the values given |
|
491 * in the pos array. Note that the size of the caps might be |
|
492 * increased by this, since each structure with a "channel- |
|
493 * positions" field needs to have a fixed "channels" field. |
|
494 * The input caps is not required to have this. |
|
495 */ |
|
496 #ifdef __SYMBIAN32__ |
|
497 EXPORT_C |
|
498 #endif |
|
499 |
|
500 |
|
501 void |
|
502 gst_audio_set_caps_channel_positions_list (GstCaps * caps, |
|
503 const GstAudioChannelPosition * pos, gint num_positions) |
|
504 { |
|
505 gint size, n; |
|
506 |
|
507 /* get number of channels, general type checkups */ |
|
508 g_return_if_fail (caps != NULL); |
|
509 g_return_if_fail (num_positions > 0); |
|
510 g_return_if_fail (pos != NULL); |
|
511 |
|
512 size = gst_caps_get_size (caps); |
|
513 for (n = 0; n < size; n++) { |
|
514 gst_caps_append (caps, add_list_to_struct (gst_caps_get_structure (caps, |
|
515 n), pos, num_positions)); |
|
516 } |
|
517 } |
|
518 |
|
519 /** |
|
520 * gst_audio_fixate_channel_positions: |
|
521 * @str: a #GstStructure containing a (possibly unfixed) |
|
522 * "channel-positions" field. |
|
523 * |
|
524 * Custom fixate function. Elements that implement some sort of |
|
525 * channel conversion algorithm should use this function for |
|
526 * fixating on GstAudioChannelPosition properties. It will take |
|
527 * care of equal channel positioning (left/right). Caller g_free()s |
|
528 * the return value. The input properties may be (and are supposed |
|
529 * to be) unfixed. |
|
530 * Note that this function is mostly a hack because we currently |
|
531 * have no way to add default fixation functions for new GTypes. |
|
532 * |
|
533 * Returns: fixed values that the caller could use as a fixed |
|
534 * set of #GstAudioChannelPosition values. |
|
535 */ |
|
536 #ifdef __SYMBIAN32__ |
|
537 EXPORT_C |
|
538 #endif |
|
539 |
|
540 |
|
541 GstAudioChannelPosition * |
|
542 gst_audio_fixate_channel_positions (GstStructure * str) |
|
543 { |
|
544 GstAudioChannelPosition *pos; |
|
545 gint channels, n, num_unfixed = 0, i, c; |
|
546 const GValue *pos_val_arr, *pos_val_entry, *pos_val; |
|
547 gboolean res, is_stereo = TRUE; |
|
548 GType t; |
|
549 |
|
550 /* |
|
551 * We're going to do this cluelessly. We'll make an array of values that |
|
552 * conflict with each other and, for each iteration in this array, pick |
|
553 * either one until all unknown values are filled. This might not work in |
|
554 * corner cases but should work OK for the general case. |
|
555 */ |
|
556 const struct |
|
557 { |
|
558 const GstAudioChannelPosition pos1[2]; |
|
559 const GstAudioChannelPosition pos2[1]; |
|
560 } conf[] = { |
|
561 /* front: mono <-> stereo */ |
|
562 { { |
|
563 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT, |
|
564 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT}, { |
|
565 GST_AUDIO_CHANNEL_POSITION_FRONT_MONO}}, |
|
566 /* front center: 2 <-> 1 */ |
|
567 { { |
|
568 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER, |
|
569 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER}, { |
|
570 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER}}, |
|
571 /* rear: 2 <-> 1 */ |
|
572 { { |
|
573 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT, |
|
574 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT}, { |
|
575 GST_AUDIO_CHANNEL_POSITION_REAR_CENTER}}, { { |
|
576 GST_AUDIO_CHANNEL_POSITION_INVALID, GST_AUDIO_CHANNEL_POSITION_INVALID}, { |
|
577 GST_AUDIO_CHANNEL_POSITION_LFE}}, { { |
|
578 GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT, |
|
579 GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT}, { |
|
580 GST_AUDIO_CHANNEL_POSITION_INVALID}}, { { |
|
581 GST_AUDIO_CHANNEL_POSITION_INVALID, GST_AUDIO_CHANNEL_POSITION_INVALID}, { |
|
582 GST_AUDIO_CHANNEL_POSITION_INVALID}} |
|
583 }; |
|
584 struct |
|
585 { |
|
586 gint num_opt[3]; |
|
587 guint num_opts[3]; |
|
588 gboolean is_fixed[3]; |
|
589 gint choice; /* -1 is none, 0 is the two, 1 is the one */ |
|
590 } opt; |
|
591 |
|
592 /* get number of channels, general type checkups */ |
|
593 g_return_val_if_fail (str != NULL, NULL); |
|
594 res = gst_structure_get_int (str, "channels", &channels); |
|
595 g_return_val_if_fail (res, NULL); |
|
596 g_return_val_if_fail (channels > 0, NULL); |
|
597 |
|
598 /* 0.8.x mono/stereo checks */ |
|
599 pos_val_arr = gst_structure_get_value (str, |
|
600 GST_AUDIO_CHANNEL_POSITIONS_FIELD_NAME); |
|
601 if (!pos_val_arr && (channels == 1 || channels == 2)) { |
|
602 pos = g_new (GstAudioChannelPosition, channels); |
|
603 if (channels == 1) { |
|
604 pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_MONO; |
|
605 } else { |
|
606 pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT; |
|
607 pos[1] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT; |
|
608 } |
|
609 return pos; |
|
610 } |
|
611 g_return_val_if_fail (pos_val_arr != NULL, NULL); |
|
612 g_return_val_if_fail (gst_value_array_get_size (pos_val_arr) == channels, |
|
613 NULL); |
|
614 for (n = 0; n < channels; n++) { |
|
615 t = G_VALUE_TYPE (gst_value_array_get_value (pos_val_arr, n)); |
|
616 g_return_val_if_fail (t == GST_TYPE_LIST || |
|
617 t == GST_TYPE_AUDIO_CHANNEL_POSITION, NULL); |
|
618 } |
|
619 |
|
620 /* all unknown, to start with */ |
|
621 pos = g_new (GstAudioChannelPosition, channels); |
|
622 for (n = 0; n < channels; n++) |
|
623 pos[n] = GST_AUDIO_CHANNEL_POSITION_INVALID; |
|
624 num_unfixed = channels; |
|
625 |
|
626 /* Iterate the array of conflicting values */ |
|
627 for (i = 0; conf[i].pos1[0] != GST_AUDIO_CHANNEL_POSITION_INVALID || |
|
628 conf[i].pos2[0] != GST_AUDIO_CHANNEL_POSITION_INVALID; i++) { |
|
629 /* front/center only important if not mono (obviously) */ |
|
630 if (conf[i].pos1[0] == GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER && |
|
631 !is_stereo) |
|
632 continue; |
|
633 |
|
634 /* init values */ |
|
635 for (n = 0; n < 3; n++) { |
|
636 opt.num_opt[n] = -1; |
|
637 opt.num_opts[n] = -1; |
|
638 opt.is_fixed[n] = FALSE; |
|
639 } |
|
640 |
|
641 /* Now, we'll see for each channel if it allows for any of the values in |
|
642 * the set of conflicting audio channel positions and keep scores. */ |
|
643 for (n = 0; n < channels; n++) { |
|
644 /* if the channel is already taken, don't bother */ |
|
645 if (pos[n] != GST_AUDIO_CHANNEL_POSITION_INVALID) |
|
646 continue; |
|
647 |
|
648 pos_val_entry = gst_value_array_get_value (pos_val_arr, n); |
|
649 t = G_VALUE_TYPE (pos_val_entry); |
|
650 if (t == GST_TYPE_LIST) { |
|
651 /* This algorhythm is suboptimal. */ |
|
652 for (c = 0; c < gst_value_list_get_size (pos_val_entry); c++) { |
|
653 pos_val = gst_value_list_get_value (pos_val_entry, c); |
|
654 if (g_value_get_enum (pos_val) == conf[i].pos1[0] && |
|
655 opt.num_opts[0] > gst_value_list_get_size (pos_val_entry) && |
|
656 !opt.is_fixed[0]) { |
|
657 /* Now test if the old position of num_opt[0] also allows for |
|
658 * the other channel (which was skipped previously). If so, |
|
659 * keep score. */ |
|
660 if (opt.num_opt[0] != -1) { |
|
661 gint c1; |
|
662 |
|
663 pos_val_entry = gst_value_array_get_value (pos_val_arr, |
|
664 opt.num_opt[0]); |
|
665 if (G_VALUE_TYPE (pos_val_entry) == GST_TYPE_LIST) { |
|
666 for (c1 = 0; c1 < gst_value_list_get_size (pos_val_entry); c1++) { |
|
667 pos_val = gst_value_list_get_value (pos_val_entry, c1); |
|
668 if (g_value_get_enum (pos_val) == conf[i].pos1[1] && |
|
669 opt.num_opts[1] > opt.num_opts[0] && !opt.is_fixed[1]) { |
|
670 opt.num_opts[1] = opt.num_opts[0]; |
|
671 opt.num_opt[1] = opt.num_opt[0]; |
|
672 } |
|
673 } |
|
674 pos_val = gst_value_list_get_value (pos_val_entry, c); |
|
675 } |
|
676 pos_val_entry = gst_value_array_get_value (pos_val_arr, n); |
|
677 } |
|
678 |
|
679 /* and save values */ |
|
680 opt.num_opts[0] = gst_value_list_get_size (pos_val_entry); |
|
681 opt.num_opt[0] = n; |
|
682 } else if (g_value_get_enum (pos_val) == conf[i].pos1[1] && |
|
683 opt.num_opts[1] > gst_value_list_get_size (pos_val_entry) && |
|
684 !opt.is_fixed[1] && n != opt.num_opt[0]) { |
|
685 opt.num_opts[1] = gst_value_list_get_size (pos_val_entry); |
|
686 opt.num_opt[1] = n; |
|
687 } |
|
688 |
|
689 /* 2 goes separately, because 0/1 vs. 2 are separate */ |
|
690 if (g_value_get_enum (pos_val) == conf[i].pos2[0] && |
|
691 opt.num_opts[2] > gst_value_list_get_size (pos_val_entry) && |
|
692 !opt.is_fixed[2]) { |
|
693 opt.num_opts[2] = gst_value_list_get_size (pos_val_entry); |
|
694 opt.num_opt[2] = n; |
|
695 } |
|
696 } |
|
697 } else { |
|
698 if (g_value_get_enum (pos_val_entry) == conf[i].pos1[0]) { |
|
699 opt.num_opt[0] = n; |
|
700 opt.is_fixed[0] = TRUE; |
|
701 } else if (g_value_get_enum (pos_val_entry) == conf[i].pos1[1]) { |
|
702 opt.num_opt[1] = n; |
|
703 opt.is_fixed[1] = TRUE; |
|
704 } else if (g_value_get_enum (pos_val_entry) == conf[i].pos2[0]) { |
|
705 opt.num_opt[2] = n; |
|
706 opt.is_fixed[2] = TRUE; |
|
707 } |
|
708 } |
|
709 } |
|
710 |
|
711 /* check our results and choose either one */ |
|
712 if ((opt.is_fixed[0] || opt.is_fixed[1]) && opt.is_fixed[2]) { |
|
713 g_warning ("Pre-fixated on both %d/%d and %d - conflict!", |
|
714 conf[i].pos1[0], conf[i].pos1[1], conf[i].pos2[0]); |
|
715 g_free (pos); |
|
716 return NULL; |
|
717 } else if ((opt.is_fixed[0] && opt.num_opt[1] == -1) || |
|
718 (opt.is_fixed[1] && opt.num_opt[0] == -1)) { |
|
719 g_warning ("Pre-fixated one side, but other side n/a of %d/%d", |
|
720 conf[i].pos1[0], conf[i].pos1[1]); |
|
721 g_free (pos); |
|
722 return NULL; |
|
723 } else if (opt.is_fixed[0] || opt.is_fixed[1]) { |
|
724 opt.choice = 0; |
|
725 } else if (opt.is_fixed[2]) { |
|
726 opt.choice = 1; |
|
727 } else if (opt.num_opt[0] != -1 && opt.num_opt[1] != -1) { |
|
728 opt.choice = 0; |
|
729 } else if (opt.num_opt[2] != -1) { |
|
730 opt.choice = 1; |
|
731 } else { |
|
732 opt.choice = -1; |
|
733 } |
|
734 |
|
735 /* stereo? Note that we keep is_stereo to TRUE if we didn't decide on |
|
736 * any arrangement. The mono/stereo channels might be handled elsewhere |
|
737 * which is clearly outside the scope of this element, so we cannot |
|
738 * know and expect the application to handle that then. */ |
|
739 if (conf[i].pos2[0] == GST_AUDIO_CHANNEL_POSITION_FRONT_MONO && |
|
740 opt.choice == 1) { |
|
741 is_stereo = FALSE; |
|
742 } |
|
743 |
|
744 /* now actually decide what we'll do and fixate on that */ |
|
745 if (opt.choice == 0) { |
|
746 g_assert (conf[i].pos1[0] != GST_AUDIO_CHANNEL_POSITION_INVALID && |
|
747 conf[i].pos1[1] != GST_AUDIO_CHANNEL_POSITION_INVALID); |
|
748 pos[opt.num_opt[0]] = conf[i].pos1[0]; |
|
749 pos[opt.num_opt[1]] = conf[i].pos1[1]; |
|
750 num_unfixed -= 2; |
|
751 } else if (opt.choice == 1) { |
|
752 g_assert (conf[i].pos2[0] != GST_AUDIO_CHANNEL_POSITION_INVALID); |
|
753 pos[opt.num_opt[2]] = conf[i].pos2[0]; |
|
754 num_unfixed--; |
|
755 } |
|
756 } |
|
757 |
|
758 /* safety check */ |
|
759 if (num_unfixed > 0) { |
|
760 g_warning ("%d unfixed channel positions left after fixation!", |
|
761 num_unfixed); |
|
762 g_free (pos); |
|
763 return NULL; |
|
764 } |
|
765 |
|
766 if (!gst_audio_check_channel_positions (pos, channels)) { |
|
767 g_free (pos); |
|
768 return NULL; |
|
769 } |
|
770 |
|
771 return pos; |
|
772 } |