gstrtpchannels.c 8.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* GStreamer
 * Copyright (C) <2008> Wim Taymans <wim.taymans@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
16 17
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
18 19 20 21 22 23 24 25 26 27 28 29 30 31
 */

#include <string.h>
#include <stdlib.h>

#include "gstrtpchannels.h"

/* 
 * RTP channel positions as discussed in RFC 3551 and also RFC 3555
 *
 * We can't really represent the described channel positions in GStreamer but we
 * implement a (very rough) approximation here.
 */

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
static const GstAudioChannelPosition pos_4_1[] = {
  GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
  GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
  GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
  GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT
};

static const GstAudioChannelPosition pos_4_2[] = {
  GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
  GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
  GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
  GST_AUDIO_CHANNEL_POSITION_LFE1
};

static const GstAudioChannelPosition pos_4_3[] = {
  GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
  GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
  GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
  GST_AUDIO_CHANNEL_POSITION_LFE1
};

static const GstAudioChannelPosition pos_5_1[] = {
  GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
  GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
  GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
  GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT,
  GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER
};

static const GstAudioChannelPosition pos_6_1[] = {
  GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
  GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
  GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
  GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT,
  GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
  GST_AUDIO_CHANNEL_POSITION_LFE1
};

static const GstAudioChannelPosition pos_6_2[] = {
  GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
  GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
  GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
  GST_AUDIO_CHANNEL_POSITION_LFE1,
  GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
  GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT
};

static const GstAudioChannelPosition pos_8_1[] = {
  GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
  GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
  GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
  GST_AUDIO_CHANNEL_POSITION_LFE1,
  GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
  GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT,
  GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
  GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT
};

static const GstAudioChannelPosition pos_8_2[] = {
  GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
  GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
  GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
  GST_AUDIO_CHANNEL_POSITION_LFE1,
  GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
  GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT,
  GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
  GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT
};

static const GstAudioChannelPosition pos_8_3[] = {
  GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
  GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
  GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
  GST_AUDIO_CHANNEL_POSITION_LFE1,
  GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
  GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT,
  GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
  GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT
};

static const GstAudioChannelPosition pos_def_1[] = {
  GST_AUDIO_CHANNEL_POSITION_MONO
};

static const GstAudioChannelPosition pos_def_2[] = {
  GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
  GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT
};

static const GstAudioChannelPosition pos_def_3[] = {
  GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
  GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
  GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER
};

static const GstAudioChannelPosition pos_def_4[] = {
  GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
  GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
  GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
  GST_AUDIO_CHANNEL_POSITION_LFE1
};

static const GstAudioChannelPosition pos_def_5[] = {
  GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
  GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
  GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
  GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
  GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT
};

static const GstAudioChannelPosition pos_def_6[] = {
  GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
  GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
  GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
  GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT,
  GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
  GST_AUDIO_CHANNEL_POSITION_LFE1
};

const GstRTPChannelOrder gst_rtp_channel_orders[] = {
  /* 4 channels */
  {"DV.LRLsRs", 4, pos_4_1},
  {"DV.LRCS", 4, pos_4_2},
  {"DV.LRCWo", 4, pos_4_3},
  /* 5 channels */
  {"DV.LRLsRsC", 5, pos_5_1},
  /* 6 channels */
  {"DV.LRLsRsCS", 6, pos_6_1},
  {"DV.LmixRmixTWoQ1Q2", 6, pos_6_2},
  /* 8 channels */
  {"DV.LRCWoLsRsLmixRmix", 8, pos_8_1},
  {"DV.LRCWoLs1Rs1Ls2Rs2", 8, pos_8_2},
  {"DV.LRCWoLsRsLcRc", 8, pos_8_3},

  /* default layouts */
  {NULL, 1, pos_def_1},
  {NULL, 2, pos_def_2},
  {NULL, 3, pos_def_3},
  {NULL, 4, pos_def_4},
  {NULL, 5, pos_def_5},
  {NULL, 6, pos_def_6},

  /* terminator, invalid entry */
  {NULL, 0, NULL},
};

178 179 180 181
static gboolean
check_channels (const GstRTPChannelOrder * order,
    const GstAudioChannelPosition * pos)
{
182
  gint i, j;
183 184 185
  gboolean res = TRUE;

  for (i = 0; i < order->channels; i++) {
186 187 188
    for (j = 0; j < order->channels; j++) {
      if (order->pos[j] == pos[i])
        break;
189
    }
190 191
    if (j == order->channels)
      return FALSE;
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
  }
  return res;
}

/**
 * gst_rtp_channels_get_by_pos:
 * @channels: the amount of channels
 * @pos: a channel layout
 *
 * Return a description of the channel layout.
 *
 * Returns: a #GstRTPChannelOrder with the channel information or NULL when @pos
 * is not a valid layout.
 */
const GstRTPChannelOrder *
gst_rtp_channels_get_by_pos (gint channels, const GstAudioChannelPosition * pos)
{
  gint i;
  const GstRTPChannelOrder *res = NULL;

  g_return_val_if_fail (pos != NULL, NULL);

  for (i = 0; channel_orders[i].pos; i++) {
    if (channel_orders[i].channels != channels)
      continue;

    if (check_channels (&channel_orders[i], pos)) {
      res = &channel_orders[i];
      break;
    }
  }
  return res;
}

/**
 * gst_rtp_channels_create_default:
 * @channels: the amount of channels
 * @order: a channel order
 *
 * Get the channel order info the @order and @channels.
 *
 * Returns: a #GstRTPChannelOrder with the channel information or NULL when
 * @order is not a know layout for @channels.
 */
const GstRTPChannelOrder *
gst_rtp_channels_get_by_order (gint channels, const gchar * order)
{
  gint i;
  const GstRTPChannelOrder *res = NULL;

  for (i = 0; channel_orders[i].pos; i++) {
    if (channel_orders[i].channels != channels)
      continue;

    /* no name but channels match, continue */
    if (!channel_orders[i].name || !order) {
      res = &channel_orders[i];
      break;
    }

    /* compare names */
    if (g_ascii_strcasecmp (channel_orders[i].name, order)) {
      res = &channel_orders[i];
      break;
    }
  }
  return res;
}

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
/**
 * gst_rtp_channels_get_by_index:
 * @channels: the amount of channels
 * @idx: the channel index to retrieve
 *
 * Get the allowed channel order descriptions for @channels. @idx can be used to
 * retrieve the desired index.
 *
 * Returns: a #GstRTPChannelOrder at @idx, NULL when there are no valid channel
 * orders.
 */
const GstRTPChannelOrder *
gst_rtp_channels_get_by_index (gint channels, guint idx)
{
  gint i;
  const GstRTPChannelOrder *res = NULL;

  for (i = 0; channel_orders[i].pos; i++) {
    if (channel_orders[i].channels != channels)
      continue;

    if (idx == 0) {
      res = &channel_orders[i];
      break;
    }
    idx--;
  }
  return res;
}


292 293 294 295 296 297 298 299 300
/**
 * gst_rtp_channels_create_default:
 * @channels: the amount of channels
 *
 * Create a default none channel mapping for @channels.
 *
 * Returns: a #GstAudioChannelPosition with all the channel position info set to
 * #GST_AUDIO_CHANNEL_POSITION_NONE.
 */
301 302
void
gst_rtp_channels_create_default (gint channels, GstAudioChannelPosition * posn)
303 304 305
{
  gint i;

306
  g_return_if_fail (channels > 0);
307 308 309 310

  for (i = 0; i < channels; i++)
    posn[i] = GST_AUDIO_CHANNEL_POSITION_NONE;
}