Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
* Shared definitions and helper functions for |
3 |
|
|
* AV1 (de)packetization. |
4 |
|
|
* Copyright (c) 2024 Axis Communications |
5 |
|
|
* |
6 |
|
|
* This file is part of FFmpeg. |
7 |
|
|
* |
8 |
|
|
* FFmpeg is free software; you can redistribute it and/or |
9 |
|
|
* modify it under the terms of the GNU Lesser General Public |
10 |
|
|
* License as published by the Free Software Foundation; either |
11 |
|
|
* version 2.1 of the License, or (at your option) any later version. |
12 |
|
|
* |
13 |
|
|
* FFmpeg is distributed in the hope that it will be useful, |
14 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 |
|
|
* Lesser General Public License for more details. |
17 |
|
|
* |
18 |
|
|
* You should have received a copy of the GNU Lesser General Public |
19 |
|
|
* License along with FFmpeg; if not, write to the Free Software |
20 |
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21 |
|
|
*/ |
22 |
|
|
|
23 |
|
|
/** |
24 |
|
|
* @file |
25 |
|
|
* @brief shared defines and functions for AV1 RTP dec/enc |
26 |
|
|
* @author Chris Hodges <chris.hodges@axis.com> |
27 |
|
|
*/ |
28 |
|
|
|
29 |
|
|
#ifndef AVFORMAT_RTP_AV1_H |
30 |
|
|
#define AVFORMAT_RTP_AV1_H |
31 |
|
|
|
32 |
|
|
#include <stdint.h> |
33 |
|
|
|
34 |
|
|
#include "libavutil/log.h" |
35 |
|
|
|
36 |
|
|
// define a couple of flags and bit fields |
37 |
|
|
#define AV1B_OBU_FORBIDDEN 7 |
38 |
|
|
#define AV1F_OBU_FORBIDDEN (1u << AV1B_OBU_FORBIDDEN) |
39 |
|
|
#define AV1S_OBU_TYPE 3 |
40 |
|
|
#define AV1M_OBU_TYPE 15 |
41 |
|
|
#define AV1B_OBU_EXTENSION_FLAG 2 |
42 |
|
|
#define AV1F_OBU_EXTENSION_FLAG (1u << AV1B_OBU_EXTENSION_FLAG) |
43 |
|
|
#define AV1B_OBU_HAS_SIZE_FIELD 1 |
44 |
|
|
#define AV1F_OBU_HAS_SIZE_FIELD (1u << AV1B_OBU_HAS_SIZE_FIELD) |
45 |
|
|
#define AV1B_OBU_RESERVED_1BIT 0 |
46 |
|
|
#define AV1F_OBU_RESERVED_1BIT (1u << AV1B_OBU_RESERVED_1BIT) |
47 |
|
|
|
48 |
|
|
#define AV1B_AGGR_HDR_FRAG_CONT 7 |
49 |
|
|
#define AV1F_AGGR_HDR_FRAG_CONT (1u << AV1B_AGGR_HDR_FRAG_CONT) |
50 |
|
|
#define AV1B_AGGR_HDR_LAST_FRAG 6 |
51 |
|
|
#define AV1F_AGGR_HDR_LAST_FRAG (1u << AV1B_AGGR_HDR_LAST_FRAG) |
52 |
|
|
#define AV1S_AGGR_HDR_NUM_OBUS 4 |
53 |
|
|
#define AV1M_AGGR_HDR_NUM_OBUS 3 |
54 |
|
|
#define AV1B_AGGR_HDR_FIRST_PKT 3 |
55 |
|
|
#define AV1F_AGGR_HDR_FIRST_PKT (1u << AV1B_AGGR_HDR_FIRST_PKT) |
56 |
|
|
|
57 |
|
|
/// calculate number of required LEB bytes for the given length |
58 |
|
✗ |
static inline unsigned int calc_leb_size(uint32_t length) { |
59 |
|
✗ |
unsigned int num_lebs = 0; |
60 |
|
|
do { |
61 |
|
✗ |
num_lebs++; |
62 |
|
✗ |
length >>= 7; |
63 |
|
✗ |
} while (length); |
64 |
|
✗ |
return num_lebs; |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
/// write out variable number of LEB bytes for the given length |
68 |
|
✗ |
static inline unsigned int write_leb(uint8_t *lebptr, uint32_t length) { |
69 |
|
✗ |
unsigned int num_lebs = 0; |
70 |
|
|
do { |
71 |
|
✗ |
num_lebs++; |
72 |
|
✗ |
if (length < 0x80) { |
73 |
|
✗ |
*lebptr = length; |
74 |
|
✗ |
break; |
75 |
|
|
} |
76 |
|
✗ |
*lebptr++ = length | 0x80; // no need to mask out |
77 |
|
✗ |
length >>= 7; |
78 |
|
|
} while (1); |
79 |
|
✗ |
return num_lebs; |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
/// write out fixed number of LEB bytes (may have "unused" bytes) |
83 |
|
✗ |
static inline void write_leb_n(uint8_t *lebptr, uint32_t length, unsigned int num_lebs) { |
84 |
|
✗ |
for (int i = 0; i < num_lebs; i++) { |
85 |
|
✗ |
if (i == num_lebs - 1) { |
86 |
|
✗ |
*lebptr = length & 0x7f; |
87 |
|
|
} else { |
88 |
|
✗ |
*lebptr++ = length | 0x80; // no need to mask out |
89 |
|
|
} |
90 |
|
✗ |
length >>= 7; |
91 |
|
|
} |
92 |
|
✗ |
} |
93 |
|
|
|
94 |
|
|
/// securely parse LEB bytes and return the resulting encoded length |
95 |
|
✗ |
static inline unsigned int parse_leb(void *logctx, const uint8_t *buf_ptr, |
96 |
|
|
uint32_t buffer_size, uint32_t *obu_size) { |
97 |
|
|
uint8_t leb128; |
98 |
|
✗ |
unsigned int num_lebs = 0; |
99 |
|
✗ |
*obu_size = 0; |
100 |
|
|
do { |
101 |
|
|
uint32_t leb7; |
102 |
|
✗ |
if (!buffer_size) { |
103 |
|
✗ |
av_log(logctx, AV_LOG_ERROR, "AV1: Out of data in OBU size field AV1 RTP packet\n"); |
104 |
|
✗ |
return 0; |
105 |
|
|
} |
106 |
|
✗ |
leb128 = *buf_ptr++; |
107 |
|
✗ |
leb7 = leb128 & 0x7f; |
108 |
|
✗ |
buffer_size--; |
109 |
|
|
/* AV1 spec says that the maximum value returned from leb128 must fit in |
110 |
|
|
* 32 bits, so if the next byte will shift data out, we have some kind |
111 |
|
|
* of violation here. It is legal, though, to have the most significant |
112 |
|
|
* bytes with all zero bits (in the lower 7 bits). */ |
113 |
|
✗ |
if (((num_lebs == 4) && (leb7 >= 0x10)) || ((num_lebs > 4) && leb7)) { |
114 |
|
✗ |
av_log(logctx, AV_LOG_ERROR, "AV1: OBU size field exceeds 32 bit in AV1 RTP packet\n"); |
115 |
|
✗ |
return 0; |
116 |
|
|
} |
117 |
|
✗ |
if ((num_lebs == 7) && (leb128 >= 0x80)) { |
118 |
|
|
/* leb128 is defined to be up to 8 bytes (why???), 8th byte MUST NOT |
119 |
|
|
* indicate continuation */ |
120 |
|
✗ |
av_log(logctx, AV_LOG_ERROR, "AV1: OBU size field consists of too many bytes in AV1 RTP packet\n"); |
121 |
|
✗ |
return 0; |
122 |
|
|
} |
123 |
|
|
// shifts >= 32 are undefined in C! |
124 |
|
✗ |
if (num_lebs <= 4) { |
125 |
|
✗ |
*obu_size |= leb7 << (7 * num_lebs); |
126 |
|
|
} |
127 |
|
✗ |
num_lebs++; |
128 |
|
✗ |
} while (leb128 >= 0x80); |
129 |
|
✗ |
return num_lebs; |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
#endif /* AVFORMAT_RTP_AV1_H */ |
133 |
|
|
|