1 /* GStreamer
2 * Copyright (C) <2008> Wim Taymans <wim.taymans@gmail.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include <string.h>
21 #include <stdlib.h>
22
23 #include "gstrtpchannels.h"
24
25 /*
26 * RTP channel positions as discussed in RFC 3551 and also RFC 3555
27 *
28 * We can't really represent the described channel positions in GStreamer but we
29 * implement a (very rough) approximation here.
30 */
31
32 static const GstAudioChannelPosition pos_4_1[] = {
33 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
34 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
35 GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
36 GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT
37 };
38
39 static const GstAudioChannelPosition pos_4_2[] = {
40 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
41 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
42 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
43 GST_AUDIO_CHANNEL_POSITION_LFE1
44 };
45
46 static const GstAudioChannelPosition pos_4_3[] = {
47 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
48 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
49 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
50 GST_AUDIO_CHANNEL_POSITION_LFE1
51 };
52
53 static const GstAudioChannelPosition pos_5_1[] = {
54 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
55 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
56 GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
57 GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT,
58 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER
59 };
60
61 static const GstAudioChannelPosition pos_6_1[] = {
62 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
63 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
64 GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
65 GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT,
66 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
67 GST_AUDIO_CHANNEL_POSITION_LFE1
68 };
69
70 static const GstAudioChannelPosition pos_6_2[] = {
71 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
72 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
73 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
74 GST_AUDIO_CHANNEL_POSITION_LFE1,
75 GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
76 GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT
77 };
78
79 static const GstAudioChannelPosition pos_8_1[] = {
80 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
81 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
82 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
83 GST_AUDIO_CHANNEL_POSITION_LFE1,
84 GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
85 GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT,
86 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
87 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT
88 };
89
90 static const GstAudioChannelPosition pos_8_2[] = {
91 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
92 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
93 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
94 GST_AUDIO_CHANNEL_POSITION_LFE1,
95 GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
96 GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT,
97 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
98 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT
99 };
100
101 static const GstAudioChannelPosition pos_8_3[] = {
102 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
103 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
104 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
105 GST_AUDIO_CHANNEL_POSITION_LFE1,
106 GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
107 GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT,
108 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
109 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT
110 };
111
112 static const GstAudioChannelPosition pos_def_1[] = {
113 GST_AUDIO_CHANNEL_POSITION_MONO
114 };
115
116 static const GstAudioChannelPosition pos_def_2[] = {
117 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
118 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT
119 };
120
121 static const GstAudioChannelPosition pos_def_3[] = {
122 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
123 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
124 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER
125 };
126
127 static const GstAudioChannelPosition pos_def_4[] = {
128 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
129 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
130 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
131 GST_AUDIO_CHANNEL_POSITION_LFE1
132 };
133
134 static const GstAudioChannelPosition pos_def_5[] = {
135 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
136 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
137 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
138 GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
139 GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT
140 };
141
142 static const GstAudioChannelPosition pos_def_6[] = {
143 GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
144 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
145 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
146 GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT,
147 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
148 GST_AUDIO_CHANNEL_POSITION_LFE1
149 };
150
151 const GstRTPChannelOrder gst_rtp_channel_orders[] = {
152 /* 4 channels */
153 {"DV.LRLsRs", 4, pos_4_1},
154 {"DV.LRCS", 4, pos_4_2},
155 {"DV.LRCWo", 4, pos_4_3},
156 /* 5 channels */
157 {"DV.LRLsRsC", 5, pos_5_1},
158 /* 6 channels */
159 {"DV.LRLsRsCS", 6, pos_6_1},
160 {"DV.LmixRmixTWoQ1Q2", 6, pos_6_2},
161 /* 8 channels */
162 {"DV.LRCWoLsRsLmixRmix", 8, pos_8_1},
163 {"DV.LRCWoLs1Rs1Ls2Rs2", 8, pos_8_2},
164 {"DV.LRCWoLsRsLcRc", 8, pos_8_3},
165
166 /* default layouts */
167 {NULL, 1, pos_def_1},
168 {NULL, 2, pos_def_2},
169 {NULL, 3, pos_def_3},
170 {NULL, 4, pos_def_4},
171 {NULL, 5, pos_def_5},
172 {NULL, 6, pos_def_6},
173
174 /* terminator, invalid entry */
175 {NULL, 0, NULL},
176 };
177
178 static gboolean
check_channels(const GstRTPChannelOrder * order,const GstAudioChannelPosition * pos)179 check_channels (const GstRTPChannelOrder * order,
180 const GstAudioChannelPosition * pos)
181 {
182 gint i, j;
183 gboolean res = TRUE;
184
185 for (i = 0; i < order->channels; i++) {
186 for (j = 0; j < order->channels; j++) {
187 if (order->pos[j] == pos[i])
188 break;
189 }
190 if (j == order->channels)
191 return FALSE;
192 }
193 return res;
194 }
195
196 /**
197 * gst_rtp_channels_get_by_pos:
198 * @channels: the amount of channels
199 * @pos: a channel layout
200 *
201 * Return a description of the channel layout.
202 *
203 * Returns: a #GstRTPChannelOrder with the channel information or NULL when @pos
204 * is not a valid layout.
205 */
206 const GstRTPChannelOrder *
gst_rtp_channels_get_by_pos(gint channels,const GstAudioChannelPosition * pos)207 gst_rtp_channels_get_by_pos (gint channels, const GstAudioChannelPosition * pos)
208 {
209 gint i;
210 const GstRTPChannelOrder *res = NULL;
211
212 g_return_val_if_fail (pos != NULL, NULL);
213
214 for (i = 0; channel_orders[i].pos; i++) {
215 if (channel_orders[i].channels != channels)
216 continue;
217
218 if (check_channels (&channel_orders[i], pos)) {
219 res = &channel_orders[i];
220 break;
221 }
222 }
223 return res;
224 }
225
226 /**
227 * gst_rtp_channels_create_default:
228 * @channels: the amount of channels
229 * @order: a channel order
230 *
231 * Get the channel order info the @order and @channels.
232 *
233 * Returns: a #GstRTPChannelOrder with the channel information or NULL when
234 * @order is not a know layout for @channels.
235 */
236 const GstRTPChannelOrder *
gst_rtp_channels_get_by_order(gint channels,const gchar * order)237 gst_rtp_channels_get_by_order (gint channels, const gchar * order)
238 {
239 gint i;
240 const GstRTPChannelOrder *res = NULL;
241
242 for (i = 0; channel_orders[i].pos; i++) {
243 if (channel_orders[i].channels != channels)
244 continue;
245
246 /* no name but channels match, continue */
247 if (!channel_orders[i].name || !order) {
248 res = &channel_orders[i];
249 break;
250 }
251
252 /* compare names */
253 if (g_ascii_strcasecmp (channel_orders[i].name, order)) {
254 res = &channel_orders[i];
255 break;
256 }
257 }
258 return res;
259 }
260
261 /**
262 * gst_rtp_channels_get_by_index:
263 * @channels: the amount of channels
264 * @idx: the channel index to retrieve
265 *
266 * Get the allowed channel order descriptions for @channels. @idx can be used to
267 * retrieve the desired index.
268 *
269 * Returns: a #GstRTPChannelOrder at @idx, NULL when there are no valid channel
270 * orders.
271 */
272 const GstRTPChannelOrder *
gst_rtp_channels_get_by_index(gint channels,guint idx)273 gst_rtp_channels_get_by_index (gint channels, guint idx)
274 {
275 gint i;
276 const GstRTPChannelOrder *res = NULL;
277
278 for (i = 0; channel_orders[i].pos; i++) {
279 if (channel_orders[i].channels != channels)
280 continue;
281
282 if (idx == 0) {
283 res = &channel_orders[i];
284 break;
285 }
286 idx--;
287 }
288 return res;
289 }
290
291
292 /**
293 * gst_rtp_channels_create_default:
294 * @channels: the amount of channels
295 *
296 * Create a default none channel mapping for @channels.
297 *
298 * Returns: a #GstAudioChannelPosition with all the channel position info set to
299 * #GST_AUDIO_CHANNEL_POSITION_NONE.
300 */
301 void
gst_rtp_channels_create_default(gint channels,GstAudioChannelPosition * posn)302 gst_rtp_channels_create_default (gint channels, GstAudioChannelPosition * posn)
303 {
304 gint i;
305
306 g_return_if_fail (channels > 0);
307
308 for (i = 0; i < channels; i++)
309 posn[i] = GST_AUDIO_CHANNEL_POSITION_NONE;
310 }
311