|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 Open Broadcast Systems Ltd |
| 3 | + * |
| 4 | + * Authors: James Darnley |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining |
| 7 | + * a copy of this software and associated documentation files (the |
| 8 | + * "Software"), to deal in the Software without restriction, including |
| 9 | + * without limitation the rights to use, copy, modify, merge, publish, |
| 10 | + * distribute, sublicense, and/or sell copies of the Software, and to |
| 11 | + * permit persons to whom the Software is furnished to do so, subject |
| 12 | + * to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be |
| 15 | + * included in all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 20 | + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 21 | + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 22 | + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 23 | + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 | + */ |
| 25 | + |
| 26 | +/** @file |
| 27 | + * @short Upipe uref s12m attributes handling |
| 28 | + * |
| 29 | + * Helper functions to handle the s12m timecode attributes. See the SMPTE |
| 30 | + * standards 12M-1 and 314M for more details. |
| 31 | + */ |
| 32 | + |
| 33 | +#ifndef _UPIPE_UREF_ATTR_S12M_H_ |
| 34 | +/** @hidden */ |
| 35 | +#define _UPIPE_UREF_ATTR_S12M_H_ |
| 36 | +#ifdef __cplusplus |
| 37 | +extern "C" { |
| 38 | +#endif |
| 39 | + |
| 40 | +#include "upipe/uref_pic.h" |
| 41 | + |
| 42 | +#include <stdbool.h> |
| 43 | +#include <stdint.h> |
| 44 | + |
| 45 | +/** @this returns a uint32_t read from possibly unaligned data |
| 46 | + * |
| 47 | + * Timecode packs and the count are native endian values. |
| 48 | + */ |
| 49 | +static inline uint32_t uref_attr_s12m_read(const uint8_t *data) |
| 50 | +{ |
| 51 | + /* TODO: big endian */ |
| 52 | + return (uint32_t)data[0] | data[1] << 8 | data[2] << 16 | data[3] <<24; |
| 53 | +} |
| 54 | + |
| 55 | +/** @This returns true if the data and size given repesents a valid s12m |
| 56 | + * attribute. |
| 57 | + */ |
| 58 | +static inline bool uref_attr_s12m_check(const uint8_t *data, const size_t size) |
| 59 | +{ |
| 60 | + /* Size must be at least 4 for a single uint32_t. */ |
| 61 | + if (size < 4) |
| 62 | + return false; |
| 63 | + const uint32_t count = uref_attr_s12m_read(data); |
| 64 | + /* The count should be followed by that number of uint32_t. */ |
| 65 | + if (size < sizeof(uint32_t) * (count+1)) |
| 66 | + return false; |
| 67 | + return true; |
| 68 | +} |
| 69 | + |
| 70 | +/** @This returns a new timecode pack from the integer components and the drop |
| 71 | + * frame flag. Does not validate values. |
| 72 | + * |
| 73 | + * @param hours 0-23 |
| 74 | + * @param minutes 0-59 |
| 75 | + * @param seconds 0-59 |
| 76 | + * @param frames 0 to framerate |
| 77 | + * @param drop true or false |
| 78 | + * @return timecode pack |
| 79 | + */ |
| 80 | + |
| 81 | +static inline uint32_t uref_attr_s12m_from_integers(const uint32_t hours, |
| 82 | + const uint32_t minutes, const uint32_t seconds, const uint32_t frames, |
| 83 | + const uint32_t drop) |
| 84 | +{ |
| 85 | + return drop << 30 |
| 86 | + | hours % 10 |
| 87 | + | hours / 10 << 4 |
| 88 | + | minutes % 10 << 8 |
| 89 | + | minutes / 10 << 12 |
| 90 | + | seconds % 10 << 16 |
| 91 | + | seconds / 10 << 20 |
| 92 | + | frames % 10 << 24 |
| 93 | + | frames / 10 << 28; |
| 94 | +} |
| 95 | + |
| 96 | +/** @This splits a timecode pack into the integer components and the drop frame |
| 97 | + * flag. Does not validate. |
| 98 | + * values. |
| 99 | + * |
| 100 | + * @param timecode timecode pack |
| 101 | + * @param hours pointer |
| 102 | + * @param minutes pointer |
| 103 | + * @param seconds pointer |
| 104 | + * @param frames pointer |
| 105 | + * @param drop pointer |
| 106 | + */ |
| 107 | + |
| 108 | +static inline void uref_attr_s12m_to_integers(const uint32_t timecode, |
| 109 | + uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint8_t *frames, |
| 110 | + bool *drop) |
| 111 | +{ |
| 112 | + *hours = 10 * (timecode >> 4 & 0x3) + (timecode & 0xf); |
| 113 | + *minutes = 10 * (timecode >> 12 & 0x7) + (timecode >> 8 & 0xf); |
| 114 | + *seconds = 10 * (timecode >> 20 & 0x7) + (timecode >> 16 & 0xf); |
| 115 | + *frames = 10 * (timecode >> 28 & 0x3) + (timecode >> 24 & 0xf); |
| 116 | + *drop = (timecode & 1<<30) == 1<<30; |
| 117 | +} |
| 118 | + |
| 119 | +/** @This returns a new timecode pack from the decimal components and the drop |
| 120 | + * frame flag. Does not validate values. |
| 121 | + * |
| 122 | + * @param hours_10s 0-2 |
| 123 | + * @param hours_1s 0-9 |
| 124 | + * @param minutes_10s 0-5 |
| 125 | + * @param minutes_1s 0-9 |
| 126 | + * @param seconds_10s 0-5 |
| 127 | + * @param seconds_1s 0-9 |
| 128 | + * @param frames_10s 0 to tens of framerate |
| 129 | + * @param frames_1s 0-9 |
| 130 | + * @param drop true or false |
| 131 | + * @return timecode pack |
| 132 | + */ |
| 133 | + |
| 134 | +static inline uint32_t uref_attr_s12m_from_decimals(const uint32_t hours_10s, |
| 135 | + const uint32_t hours_1s, const uint32_t minutes_10s, const uint32_t minutes_1s, |
| 136 | + const uint32_t seconds_10s, const uint32_t seconds_1s, const uint32_t frames_10s, |
| 137 | + const uint32_t frames_1s, const uint32_t drop) |
| 138 | +{ |
| 139 | + return drop << 30 |
| 140 | + | hours_1s |
| 141 | + | hours_10s << 4 |
| 142 | + | minutes_1s << 8 |
| 143 | + | minutes_10s << 12 |
| 144 | + | seconds_1s << 16 |
| 145 | + | seconds_10s << 20 |
| 146 | + | frames_1s << 24 |
| 147 | + | frames_10s << 28; |
| 148 | +} |
| 149 | + |
| 150 | +/** @This splits a timecode pack into the decimal components and the drop frame |
| 151 | + * flag. Does not validate values. |
| 152 | + * |
| 153 | + * @pack timecode timecode pack |
| 154 | + * @param hours_10s pointer |
| 155 | + * @param hours_1s pointer |
| 156 | + * @param minutes_10s pointer |
| 157 | + * @param minutes_1s pointer |
| 158 | + * @param seconds_10s pointer |
| 159 | + * @param seconds_1s pointer |
| 160 | + * @param frames_10s pointer |
| 161 | + * @param frames_1s pointer |
| 162 | + * @param drop pointer |
| 163 | + */ |
| 164 | + |
| 165 | +static inline void uref_attr_s12m_to_decimals(const uint32_t timecode, |
| 166 | + uint8_t *hours_10s, uint8_t *hours_1s, uint8_t *minutes_10s, uint8_t *minutes_1s, |
| 167 | + uint8_t *seconds_10s, uint8_t *seconds_1s, uint8_t *frames_10s, uint8_t *frames_1s, |
| 168 | + bool *drop) |
| 169 | +{ |
| 170 | + *hours_10s = 10 * (timecode >> 4 & 0x3); |
| 171 | + *hours_1s = (timecode & 0xf); |
| 172 | + *minutes_10s = 10 * (timecode >> 12 & 0x7); |
| 173 | + *minutes_1s = (timecode >> 8 & 0xf); |
| 174 | + *seconds_10s = 10 * (timecode >> 20 & 0x7); |
| 175 | + *seconds_1s = (timecode >> 16 & 0xf); |
| 176 | + *frames_10s = 10 * (timecode >> 28 & 0x3); |
| 177 | + *frames_1s = (timecode >> 24 & 0xf); |
| 178 | + *drop = (timecode & 1<<30) == 1<<30; |
| 179 | +} |
| 180 | + |
| 181 | +#ifdef __cplusplus |
| 182 | +} |
| 183 | +#endif |
| 184 | +#endif |
0 commit comments