2 * Shared definitions and helper functions for
3 * AV1 (de)packetization.
4 * Copyright (c) 2024 Axis Communications
6 * This file is part of FFmpeg.
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.
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.
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
25 * @brief shared defines and functions for AV1 RTP dec/enc
26 * @author Chris Hodges <chris.hodges@axis.com>
29 #ifndef AVFORMAT_RTP_AV1_H
30 #define AVFORMAT_RTP_AV1_H
34 #include "libavutil/log.h"
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)
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)
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;
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;
76 *lebptr
++ = length
| 0x80; // no need to mask out
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;
88 *lebptr
++ = length
| 0x80; // no need to mask out
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
) {
98 unsigned int num_lebs
= 0;
103 av_log(logctx
, AV_LOG_ERROR
, "AV1: Out of data in OBU size field AV1 RTP packet\n");
107 leb7
= leb128
& 0x7f;
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");
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");
123 // shifts >= 32 are undefined in C!
125 *obu_size
|= leb7
<< (7 * num_lebs
);
128 } while (leb128
>= 0x80);
132 #endif /* AVFORMAT_RTP_AV1_H */