summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndre Guedes <andre.guedes@intel.com>2018-01-19 10:39:01 -0800
committerAndre Guedes <andre.guedes@intel.com>2018-01-26 18:00:09 -0800
commit145680dada729565c3ee82d30bfeaa861f1f1a1f (patch)
tree3e80c2f572eaa8083c4439e2425db6d03f12c33d
parente6bd6c11453005069d1cc59a8a9d522ce65d830a (diff)
downloadOpen-AVB-145680dada729565c3ee82d30bfeaa861f1f1a1f.tar.gz
Add AAF support
This patch adds support for AVTP Audio Format (AAF) specified in chapter 7 from the IEEE 1722-2016 spec. AAF provides two data encapsulation: PCM and AES3. This patch implements only the PCM encapsulation. AAF is based on AVTP Stream PDU so this patch introduces the 'struct avtp_stream_pdu'. Since that PDU is also used by other AVTP Formats (e.g. IEC 61883/IIDC Format, Compressed Video Format, Time-Synchronous Control Format), the 'struct avtp_stream_pdu' is defined in avtp.h header. The public header avtp_aaf.h defines the AAF fields, their possible values, and get/set APIs to manipulate these fields.
-rw-r--r--lib/libavtp/README.md7
-rw-r--r--lib/libavtp/include/avtp.h9
-rw-r--r--lib/libavtp/include/avtp_aaf.h108
-rw-r--r--lib/libavtp/meson.build10
-rw-r--r--lib/libavtp/src/avtp_aaf.c312
5 files changed, 444 insertions, 2 deletions
diff --git a/lib/libavtp/README.md b/lib/libavtp/README.md
index 70eb2134..5113a02e 100644
--- a/lib/libavtp/README.md
+++ b/lib/libavtp/README.md
@@ -30,3 +30,10 @@ To install libavtp on your system run:
```
$ sudo ninja -C build install
```
+
+# AVTP Formats Support
+
+AVTP protocol defines several AVTPDU type formats (see Table 6 from IEEE
+1722-2016 spec). Libavtp doesn't support all of them yet. The list of supported
+formarts is:
+* AAF (PCM encapsulation only)
diff --git a/lib/libavtp/include/avtp.h b/lib/libavtp/include/avtp.h
index 5322f772..c999c1eb 100644
--- a/lib/libavtp/include/avtp.h
+++ b/lib/libavtp/include/avtp.h
@@ -66,6 +66,15 @@ struct avtp_common_pdu {
uint8_t pdu_specific[0];
} __attribute__ ((__packed__));
+struct avtp_stream_pdu {
+ uint32_t subtype_data;
+ uint64_t stream_id;
+ uint32_t avtp_time;
+ uint32_t format_specific;
+ uint32_t packet_info;
+ uint8_t avtp_payload[0];
+} __attribute__ ((__packed__));
+
enum avtp_field {
AVTP_FIELD_SUBTYPE,
AVTP_FIELD_VERSION,
diff --git a/lib/libavtp/include/avtp_aaf.h b/lib/libavtp/include/avtp_aaf.h
new file mode 100644
index 00000000..80b7be14
--- /dev/null
+++ b/lib/libavtp/include/avtp_aaf.h
@@ -0,0 +1,108 @@
+/*
+ * Copyright (c) 2017, Intel Corporation
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * Neither the name of Intel Corporation nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include <errno.h>
+#include <stdint.h>
+
+/* AAF PCM 'format' field values. */
+#define AVTP_AAF_FORMAT_USER 0x00
+#define AVTP_AAF_FORMAT_FLOAT_32BIT 0x01
+#define AVTP_AAF_FORMAT_INT_32BIT 0x02
+#define AVTP_AAF_FORMAT_INT_24BIT 0x03
+#define AVTP_AAF_FORMAT_INT_16BIT 0x04
+#define AVTP_AAF_FORMAT_AES3_32BIT 0x05
+
+/* AAF PCM 'nsr' (nominal sample rate) field values. */
+#define AVTP_AAF_PCM_NSR_USER 0x00
+#define AVTP_AAF_PCM_NSR_8KHZ 0x01
+#define AVTP_AAF_PCM_NSR_16KHZ 0x02
+#define AVTP_AAF_PCM_NSR_32KHZ 0x03
+#define AVTP_AAF_PCM_NSR_44_1KHZ 0x04
+#define AVTP_AAF_PCM_NSR_48KHZ 0x05
+#define AVTP_AAF_PCM_NSR_88_2KHZ 0x06
+#define AVTP_AAF_PCM_NSR_96KHZ 0x07
+#define AVTP_AAF_PCM_NSR_176_4KHZ 0x08
+#define AVTP_AAF_PCM_NSR_192KHZ 0x09
+#define AVTP_AAF_PCM_NSR_24KHZ 0x0A
+
+/* AAF PCM 'sp' (sparse timestamp) field values. */
+#define AVTP_AAF_PCM_SP_NORMAL 0x00
+#define AVTP_AAF_PCM_SP_SPARSE 0x01
+
+enum avtp_aaf_field {
+ AVTP_AAF_FIELD_SV,
+ AVTP_AAF_FIELD_MR,
+ AVTP_AAF_FIELD_TV,
+ AVTP_AAF_FIELD_SEQ_NUM,
+ AVTP_AAF_FIELD_TU,
+ AVTP_AAF_FIELD_STREAM_ID,
+ AVTP_AAF_FIELD_TIMESTAMP,
+ AVTP_AAF_FIELD_FORMAT,
+ AVTP_AAF_FIELD_NSR,
+ AVTP_AAF_FIELD_CHAN_PER_FRAME,
+ AVTP_AAF_FIELD_BIT_DEPTH,
+ AVTP_AAF_FIELD_STREAM_DATA_LEN,
+ AVTP_AAF_FIELD_SP,
+ AVTP_AAF_FIELD_EVT,
+ AVTP_AAF_FIELD_MAX,
+};
+
+/* Get value from AAF AVTPDU field.
+ * @pdu: Pointer to PDU struct.
+ * @field: PDU field to be retrieved.
+ * @val: Pointer to variable which the retrieved value should be saved.
+ *
+ * Returns:
+ * 0: Success.
+ * -EINVAL: If any argument is invalid.
+ */
+int avtp_aaf_pdu_get(const struct avtp_stream_pdu *pdu,
+ enum avtp_aaf_field field, uint64_t *val);
+
+/* Set value from AAF AVTPDU field.
+ * @pdu: Pointer to PDU struct.
+ * @field: PDU field to be set.
+ * @val: Value to be set.
+ *
+ * Returns:
+ * 0: Success.
+ * -EINVAL: If any argument is invalid.
+ */
+int avtp_aaf_pdu_set(struct avtp_stream_pdu *pdu, enum avtp_aaf_field field,
+ uint64_t val);
+
+/* Initialize AAF AVTPDU. All AVTPDU fields are initialized with zero except
+ * 'subtype' (which is set to AVTP_SUBTYPE_AAF) and 'sv' (which is set to 1).
+ * @pdu: Pointer to PDU struct.
+ *
+ * Return values:
+ * 0: Success.
+ * -EINVAL: If any argument is invalid.
+ */
+int avtp_aaf_pdu_init(struct avtp_stream_pdu *pdu);
diff --git a/lib/libavtp/meson.build b/lib/libavtp/meson.build
index f5d29815..bdccb442 100644
--- a/lib/libavtp/meson.build
+++ b/lib/libavtp/meson.build
@@ -7,9 +7,15 @@ project(
library(
'avtp',
- 'src/avtp.c',
+ [
+ 'src/avtp.c',
+ 'src/avtp_aaf.c',
+ ],
include_directories: include_directories('include'),
install: true,
)
-install_headers('include/avtp.h')
+install_headers(
+ 'include/avtp.h',
+ 'include/avtp_aaf.h',
+)
diff --git a/lib/libavtp/src/avtp_aaf.c b/lib/libavtp/src/avtp_aaf.c
new file mode 100644
index 00000000..ce84a2c7
--- /dev/null
+++ b/lib/libavtp/src/avtp_aaf.c
@@ -0,0 +1,312 @@
+/*
+ * Copyright (c) 2017, Intel Corporation
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * Neither the name of Intel Corporation nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <arpa/inet.h>
+#include <endian.h>
+#include <string.h>
+
+#include "avtp.h"
+#include "avtp_aaf.h"
+#include "util.h"
+
+#define SHIFT_SV (31 - 8)
+#define SHIFT_MR (31 - 12)
+#define SHIFT_TV (31 - 15)
+#define SHIFT_SEQ_NUM (31 - 23)
+#define SHIFT_FORMAT (31 - 7)
+#define SHIFT_NSR (31 - 11)
+#define SHIFT_CHAN_PER_FRAME (31 - 23)
+#define SHIFT_STREAM_DATA_LEN (31 - 15)
+#define SHIFT_SP (31 - 19)
+#define SHIFT_EVT (31 - 23)
+
+#define MASK_SV (BITMASK(1) << SHIFT_SV)
+#define MASK_MR (BITMASK(1) << SHIFT_MR)
+#define MASK_TV (BITMASK(1) << SHIFT_TV)
+#define MASK_SEQ_NUM (BITMASK(8) << SHIFT_SEQ_NUM)
+#define MASK_TU (BITMASK(1))
+#define MASK_FORMAT (BITMASK(8) << SHIFT_FORMAT)
+#define MASK_NSR (BITMASK(4) << SHIFT_NSR)
+#define MASK_CHAN_PER_FRAME (BITMASK(10) << SHIFT_CHAN_PER_FRAME)
+#define MASK_BIT_DEPTH (BITMASK(8))
+#define MASK_STREAM_DATA_LEN (BITMASK(16) << SHIFT_STREAM_DATA_LEN)
+#define MASK_SP (BITMASK(1) << SHIFT_SP)
+#define MASK_EVT (BITMASK(4) << SHIFT_EVT)
+
+static int get_field_value(const struct avtp_stream_pdu *pdu,
+ enum avtp_aaf_field field, uint64_t *val)
+{
+ uint32_t bitmap, mask;
+ uint8_t shift;
+
+ switch (field) {
+ case AVTP_AAF_FIELD_SV:
+ mask = MASK_SV;
+ shift = SHIFT_SV;
+ bitmap = ntohl(pdu->subtype_data);
+ break;
+ case AVTP_AAF_FIELD_MR:
+ mask = MASK_MR;
+ shift = SHIFT_MR;
+ bitmap = ntohl(pdu->subtype_data);
+ break;
+ case AVTP_AAF_FIELD_TV:
+ mask = MASK_TV;
+ shift = SHIFT_TV;
+ bitmap = ntohl(pdu->subtype_data);
+ break;
+ case AVTP_AAF_FIELD_SEQ_NUM:
+ mask = MASK_SEQ_NUM;
+ shift = SHIFT_SEQ_NUM;
+ bitmap = ntohl(pdu->subtype_data);
+ break;
+ case AVTP_AAF_FIELD_TU:
+ mask = MASK_TU;
+ shift = 0;
+ bitmap = ntohl(pdu->subtype_data);
+ break;
+ case AVTP_AAF_FIELD_FORMAT:
+ mask = MASK_FORMAT;
+ shift = SHIFT_FORMAT;
+ bitmap = ntohl(pdu->format_specific);
+ break;
+ case AVTP_AAF_FIELD_NSR:
+ mask = MASK_NSR;
+ shift = SHIFT_NSR;
+ bitmap = ntohl(pdu->format_specific);
+ break;
+ case AVTP_AAF_FIELD_CHAN_PER_FRAME:
+ mask = MASK_CHAN_PER_FRAME;
+ shift = SHIFT_CHAN_PER_FRAME;
+ bitmap = ntohl(pdu->format_specific);
+ break;
+ case AVTP_AAF_FIELD_BIT_DEPTH:
+ mask = MASK_BIT_DEPTH;
+ shift = 0;
+ bitmap = ntohl(pdu->format_specific);
+ break;
+ case AVTP_AAF_FIELD_STREAM_DATA_LEN:
+ mask = MASK_STREAM_DATA_LEN;
+ shift = SHIFT_STREAM_DATA_LEN;
+ bitmap = ntohl(pdu->packet_info);
+ break;
+ case AVTP_AAF_FIELD_SP:
+ mask = MASK_SP;
+ shift = SHIFT_SP;
+ bitmap = ntohl(pdu->packet_info);
+ break;
+ case AVTP_AAF_FIELD_EVT:
+ mask = MASK_EVT;
+ shift = SHIFT_EVT;
+ bitmap = ntohl(pdu->packet_info);
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ *val = BITMAP_GET_VALUE(bitmap, mask, shift);
+
+ return 0;
+}
+
+int avtp_aaf_pdu_get(const struct avtp_stream_pdu *pdu,
+ enum avtp_aaf_field field, uint64_t *val)
+{
+ int res;
+
+ if (!pdu || !val)
+ return -EINVAL;
+
+ switch (field) {
+ case AVTP_AAF_FIELD_SV:
+ case AVTP_AAF_FIELD_MR:
+ case AVTP_AAF_FIELD_TV:
+ case AVTP_AAF_FIELD_SEQ_NUM:
+ case AVTP_AAF_FIELD_TU:
+ case AVTP_AAF_FIELD_FORMAT:
+ case AVTP_AAF_FIELD_NSR:
+ case AVTP_AAF_FIELD_CHAN_PER_FRAME:
+ case AVTP_AAF_FIELD_BIT_DEPTH:
+ case AVTP_AAF_FIELD_STREAM_DATA_LEN:
+ case AVTP_AAF_FIELD_SP:
+ case AVTP_AAF_FIELD_EVT:
+ res = get_field_value(pdu, field, val);
+ break;
+ case AVTP_AAF_FIELD_TIMESTAMP:
+ *val = ntohl(pdu->avtp_time);
+ res = 0;
+ break;
+ case AVTP_AAF_FIELD_STREAM_ID:
+ *val = be64toh(pdu->stream_id);
+ res = 0;
+ break;
+ default:
+ res = -EINVAL;
+ break;
+ }
+
+ return res;
+}
+
+static int set_field_value(struct avtp_stream_pdu *pdu,
+ enum avtp_aaf_field field, uint32_t val)
+{
+ uint32_t *ptr, bitmap, mask;
+ uint8_t shift;
+
+ switch (field) {
+ case AVTP_AAF_FIELD_SV:
+ mask = MASK_SV;
+ shift = SHIFT_SV;
+ ptr = &pdu->subtype_data;
+ break;
+ case AVTP_AAF_FIELD_MR:
+ mask = MASK_MR;
+ shift = SHIFT_MR;
+ ptr = &pdu->subtype_data;
+ break;
+ case AVTP_AAF_FIELD_TV:
+ mask = MASK_TV;
+ shift = SHIFT_TV;
+ ptr = &pdu->subtype_data;
+ break;
+ case AVTP_AAF_FIELD_SEQ_NUM:
+ mask = MASK_SEQ_NUM;
+ shift = SHIFT_SEQ_NUM;
+ ptr = &pdu->subtype_data;
+ break;
+ case AVTP_AAF_FIELD_TU:
+ mask = MASK_TU;
+ shift = 0;
+ ptr = &pdu->subtype_data;
+ break;
+ case AVTP_AAF_FIELD_FORMAT:
+ mask = MASK_FORMAT;
+ shift = SHIFT_FORMAT;
+ ptr = &pdu->format_specific;
+ break;
+ case AVTP_AAF_FIELD_NSR:
+ mask = MASK_NSR;
+ shift = SHIFT_NSR;
+ ptr = &pdu->format_specific;
+ break;
+ case AVTP_AAF_FIELD_CHAN_PER_FRAME:
+ mask = MASK_CHAN_PER_FRAME;
+ shift = SHIFT_CHAN_PER_FRAME;
+ ptr = &pdu->format_specific;
+ break;
+ case AVTP_AAF_FIELD_BIT_DEPTH:
+ mask = MASK_BIT_DEPTH;
+ shift = 0;
+ ptr = &pdu->format_specific;
+ break;
+ case AVTP_AAF_FIELD_STREAM_DATA_LEN:
+ mask = MASK_STREAM_DATA_LEN;
+ shift = SHIFT_STREAM_DATA_LEN;
+ ptr = &pdu->packet_info;
+ break;
+ case AVTP_AAF_FIELD_SP:
+ mask = MASK_SP;
+ shift = SHIFT_SP;
+ ptr = &pdu->packet_info;
+ break;
+ case AVTP_AAF_FIELD_EVT:
+ mask = MASK_EVT;
+ shift = SHIFT_EVT;
+ ptr = &pdu->packet_info;
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ bitmap = ntohl(*ptr);
+
+ BITMAP_SET_VALUE(bitmap, val, mask, shift);
+
+ *ptr = htonl(bitmap);
+
+ return 0;
+}
+
+int avtp_aaf_pdu_set(struct avtp_stream_pdu *pdu, enum avtp_aaf_field field,
+ uint64_t val)
+{
+ int res;
+
+ if (!pdu)
+ return -EINVAL;
+
+ switch (field) {
+ case AVTP_AAF_FIELD_SV:
+ case AVTP_AAF_FIELD_MR:
+ case AVTP_AAF_FIELD_TV:
+ case AVTP_AAF_FIELD_SEQ_NUM:
+ case AVTP_AAF_FIELD_TU:
+ case AVTP_AAF_FIELD_FORMAT:
+ case AVTP_AAF_FIELD_NSR:
+ case AVTP_AAF_FIELD_CHAN_PER_FRAME:
+ case AVTP_AAF_FIELD_BIT_DEPTH:
+ case AVTP_AAF_FIELD_STREAM_DATA_LEN:
+ case AVTP_AAF_FIELD_SP:
+ case AVTP_AAF_FIELD_EVT:
+ res = set_field_value(pdu, field, val);
+ break;
+ case AVTP_AAF_FIELD_TIMESTAMP:
+ pdu->avtp_time = htonl(val);
+ res = 0;
+ break;
+ case AVTP_AAF_FIELD_STREAM_ID:
+ pdu->stream_id = htobe64(val);
+ res = 0;
+ break;
+ default:
+ res = -EINVAL;
+ }
+
+ return res;
+}
+
+int avtp_aaf_pdu_init(struct avtp_stream_pdu *pdu)
+{
+ int res;
+
+ if (!pdu)
+ return -EINVAL;
+
+ memset(pdu, 0, sizeof(struct avtp_stream_pdu));
+
+ res = avtp_pdu_set((struct avtp_common_pdu *) pdu, AVTP_FIELD_SUBTYPE,
+ AVTP_SUBTYPE_AAF);
+ if (res < 0)
+ return res;
+
+ res = avtp_aaf_pdu_set(pdu, AVTP_AAF_FIELD_SV, 1);
+ if (res < 0)
+ return res;
+
+ return 0;
+};