| 1 |
/* |
| 2 |
* Copyright (c) 2019, Intel Corporation |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions are met: |
| 6 |
* |
| 7 |
* * Redistributions of source code must retain the above copyright notice, |
| 8 |
* this list of conditions and the following disclaimer. |
| 9 |
* * Redistributions in binary form must reproduce the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer in the |
| 11 |
* documentation and/or other materials provided with the distribution. |
| 12 |
* * Neither the name of Intel Corporation nor the names of its contributors |
| 13 |
* may be used to endorse or promote products derived from this software |
| 14 |
* without specific prior written permission. |
| 15 |
* |
| 16 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 |
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
| 20 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 21 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 22 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 |
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 |
*/ |
| 27 |
|
| 28 |
#include <arpa/inet.h> |
| 29 |
#include <string.h> |
| 30 |
|
| 31 |
#include "avtp.h" |
| 32 |
#include "avtp_cvf.h" |
| 33 |
#include "avtp_stream.h" |
| 34 |
#include "util.h" |
| 35 |
|
| 36 |
#define SHIFT_FORMAT (31 - 7) |
| 37 |
#define SHIFT_FORMAT_SUBTYPE (31 - 15) |
| 38 |
#define SHIFT_M (31 - 19) |
| 39 |
#define SHIFT_EVT (31 - 23) |
| 40 |
#define SHIFT_PTV (31 - 18) |
| 41 |
|
| 42 |
#define MASK_FORMAT (BITMASK(8) << SHIFT_FORMAT) |
| 43 |
#define MASK_FORMAT_SUBTYPE (BITMASK(8) << SHIFT_FORMAT_SUBTYPE) |
| 44 |
#define MASK_M (BITMASK(1) << SHIFT_M) |
| 45 |
#define MASK_EVT (BITMASK(4) << SHIFT_EVT) |
| 46 |
#define MASK_PTV (BITMASK(1) << SHIFT_PTV) |
| 47 |
|
| 48 |
static int get_field_value(const struct avtp_stream_pdu *pdu, |
| 49 |
enum avtp_cvf_field field, uint64_t *val) |
| 50 |
{ |
| 51 |
uint32_t bitmap, mask; |
| 52 |
uint8_t shift; |
| 53 |
|
| 54 |
switch (field) { |
| 55 |
case AVTP_CVF_FIELD_FORMAT: |
| 56 |
mask = MASK_FORMAT; |
| 57 |
shift = SHIFT_FORMAT; |
| 58 |
bitmap = ntohl(pdu->format_specific); |
| 59 |
break; |
| 60 |
case AVTP_CVF_FIELD_FORMAT_SUBTYPE: |
| 61 |
mask = MASK_FORMAT_SUBTYPE; |
| 62 |
shift = SHIFT_FORMAT_SUBTYPE; |
| 63 |
bitmap = ntohl(pdu->format_specific); |
| 64 |
break; |
| 65 |
case AVTP_CVF_FIELD_M: |
| 66 |
mask = MASK_M; |
| 67 |
shift = SHIFT_M; |
| 68 |
bitmap = ntohl(pdu->packet_info); |
| 69 |
break; |
| 70 |
case AVTP_CVF_FIELD_EVT: |
| 71 |
mask = MASK_EVT; |
| 72 |
shift = SHIFT_EVT; |
| 73 |
bitmap = ntohl(pdu->packet_info); |
| 74 |
break; |
| 75 |
case AVTP_CVF_FIELD_H264_PTV: |
| 76 |
mask = MASK_PTV; |
| 77 |
shift = SHIFT_PTV; |
| 78 |
bitmap = ntohl(pdu->packet_info); |
| 79 |
break; |
| 80 |
default: |
| 81 |
return -EINVAL; |
| 82 |
} |
| 83 |
|
| 84 |
*val = BITMAP_GET_VALUE(bitmap, mask, shift); |
| 85 |
|
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
int avtp_cvf_pdu_get(const struct avtp_stream_pdu *pdu, |
| 90 |
enum avtp_cvf_field field, uint64_t *val) |
| 91 |
{ |
| 92 |
int res; |
| 93 |
|
| 94 |
if (!pdu || !val) |
| 95 |
return -EINVAL; |
| 96 |
|
| 97 |
switch (field) { |
| 98 |
case AVTP_CVF_FIELD_SV: |
| 99 |
case AVTP_CVF_FIELD_MR: |
| 100 |
case AVTP_CVF_FIELD_TV: |
| 101 |
case AVTP_CVF_FIELD_SEQ_NUM: |
| 102 |
case AVTP_CVF_FIELD_TU: |
| 103 |
case AVTP_CVF_FIELD_STREAM_DATA_LEN: |
| 104 |
case AVTP_CVF_FIELD_TIMESTAMP: |
| 105 |
case AVTP_CVF_FIELD_STREAM_ID: |
| 106 |
res = avtp_stream_pdu_get(pdu, (enum avtp_stream_field) field, |
| 107 |
val); |
| 108 |
break; |
| 109 |
case AVTP_CVF_FIELD_FORMAT: |
| 110 |
case AVTP_CVF_FIELD_FORMAT_SUBTYPE: |
| 111 |
case AVTP_CVF_FIELD_M: |
| 112 |
case AVTP_CVF_FIELD_EVT: |
| 113 |
case AVTP_CVF_FIELD_H264_PTV: |
| 114 |
res = get_field_value(pdu, field, val); |
| 115 |
break; |
| 116 |
case AVTP_CVF_FIELD_H264_TIMESTAMP: |
| 117 |
{ |
| 118 |
/* This field lives on H.264 header, inside avtp_payload */ |
| 119 |
struct avtp_cvf_h264_payload *pay = |
| 120 |
(struct avtp_cvf_h264_payload *)pdu->avtp_payload; |
| 121 |
*val = ntohl(pay->h264_header); |
| 122 |
res = 0; |
| 123 |
break; |
| 124 |
} |
| 125 |
default: |
| 126 |
res = -EINVAL; |
| 127 |
break; |
| 128 |
} |
| 129 |
|
| 130 |
return res; |
| 131 |
} |
| 132 |
|
| 133 |
static int set_field_value(struct avtp_stream_pdu *pdu, |
| 134 |
enum avtp_cvf_field field, uint32_t val) |
| 135 |
{ |
| 136 |
uint32_t bitmap, mask; |
| 137 |
uint8_t shift; |
| 138 |
void *ptr; |
| 139 |
|
| 140 |
switch (field) { |
| 141 |
case AVTP_CVF_FIELD_FORMAT: |
| 142 |
mask = MASK_FORMAT; |
| 143 |
shift = SHIFT_FORMAT; |
| 144 |
ptr = &pdu->format_specific; |
| 145 |
break; |
| 146 |
case AVTP_CVF_FIELD_FORMAT_SUBTYPE: |
| 147 |
mask = MASK_FORMAT_SUBTYPE; |
| 148 |
shift = SHIFT_FORMAT_SUBTYPE; |
| 149 |
ptr = &pdu->format_specific; |
| 150 |
break; |
| 151 |
case AVTP_CVF_FIELD_M: |
| 152 |
mask = MASK_M; |
| 153 |
shift = SHIFT_M; |
| 154 |
ptr = &pdu->packet_info; |
| 155 |
break; |
| 156 |
case AVTP_CVF_FIELD_EVT: |
| 157 |
mask = MASK_EVT; |
| 158 |
shift = SHIFT_EVT; |
| 159 |
ptr = &pdu->packet_info; |
| 160 |
break; |
| 161 |
case AVTP_CVF_FIELD_H264_PTV: |
| 162 |
mask = MASK_PTV; |
| 163 |
shift = SHIFT_PTV; |
| 164 |
ptr = &pdu->packet_info; |
| 165 |
break; |
| 166 |
default: |
| 167 |
return -EINVAL; |
| 168 |
} |
| 169 |
|
| 170 |
bitmap = get_unaligned_be32(ptr); |
| 171 |
|
| 172 |
BITMAP_SET_VALUE(bitmap, val, mask, shift); |
| 173 |
|
| 174 |
put_unaligned_be32(bitmap, ptr); |
| 175 |
|
| 176 |
return 0; |
| 177 |
} |
| 178 |
|
| 179 |
int avtp_cvf_pdu_set(struct avtp_stream_pdu *pdu, enum avtp_cvf_field field, |
| 180 |
uint64_t val) |
| 181 |
{ |
| 182 |
int res; |
| 183 |
|
| 184 |
if (!pdu) |
| 185 |
return -EINVAL; |
| 186 |
|
| 187 |
switch (field) { |
| 188 |
case AVTP_CVF_FIELD_SV: |
| 189 |
case AVTP_CVF_FIELD_MR: |
| 190 |
case AVTP_CVF_FIELD_TV: |
| 191 |
case AVTP_CVF_FIELD_SEQ_NUM: |
| 192 |
case AVTP_CVF_FIELD_TU: |
| 193 |
case AVTP_CVF_FIELD_STREAM_DATA_LEN: |
| 194 |
case AVTP_CVF_FIELD_TIMESTAMP: |
| 195 |
case AVTP_CVF_FIELD_STREAM_ID: |
| 196 |
res = avtp_stream_pdu_set(pdu, (enum avtp_stream_field) field, |
| 197 |
val); |
| 198 |
break; |
| 199 |
case AVTP_CVF_FIELD_FORMAT: |
| 200 |
case AVTP_CVF_FIELD_FORMAT_SUBTYPE: |
| 201 |
case AVTP_CVF_FIELD_M: |
| 202 |
case AVTP_CVF_FIELD_EVT: |
| 203 |
case AVTP_CVF_FIELD_H264_PTV: |
| 204 |
res = set_field_value(pdu, field, val); |
| 205 |
break; |
| 206 |
case AVTP_CVF_FIELD_H264_TIMESTAMP: |
| 207 |
{ |
| 208 |
/* This field lives on H.264 header, inside avtp_payload */ |
| 209 |
struct avtp_cvf_h264_payload *pay = |
| 210 |
(struct avtp_cvf_h264_payload *)pdu->avtp_payload; |
| 211 |
pay->h264_header = htonl(val); |
| 212 |
res = 0; |
| 213 |
break; |
| 214 |
} |
| 215 |
default: |
| 216 |
res = -EINVAL; |
| 217 |
break; |
| 218 |
} |
| 219 |
|
| 220 |
return res; |
| 221 |
} |
| 222 |
|
| 223 |
int avtp_cvf_pdu_init(struct avtp_stream_pdu *pdu, uint8_t subtype) |
| 224 |
{ |
| 225 |
int res; |
| 226 |
|
| 227 |
if (!pdu) |
| 228 |
return -EINVAL; |
| 229 |
|
| 230 |
if (subtype > AVTP_CVF_FORMAT_SUBTYPE_JPEG2000) |
| 231 |
return -EINVAL; |
| 232 |
|
| 233 |
memset(pdu, 0, sizeof(struct avtp_stream_pdu)); |
| 234 |
|
| 235 |
res = avtp_pdu_set((struct avtp_common_pdu *) pdu, AVTP_FIELD_SUBTYPE, |
| 236 |
AVTP_SUBTYPE_CVF); |
| 237 |
if (res < 0) |
| 238 |
return res; |
| 239 |
|
| 240 |
res = avtp_cvf_pdu_set(pdu, AVTP_CVF_FIELD_SV, 1); |
| 241 |
if (res < 0) |
| 242 |
return res; |
| 243 |
|
| 244 |
res = avtp_cvf_pdu_set(pdu, AVTP_CVF_FIELD_FORMAT, AVTP_CVF_FORMAT_RFC); |
| 245 |
if (res < 0) |
| 246 |
return res; |
| 247 |
|
| 248 |
res = avtp_cvf_pdu_set(pdu, AVTP_CVF_FIELD_FORMAT_SUBTYPE, subtype); |
| 249 |
if (res < 0) |
| 250 |
return res; |
| 251 |
|
| 252 |
return 0; |
| 253 |
} |