]> git.sesse.net Git - ffmpeg/blob - libavcodec/av1_parse.h
avcodec/av1_parse: return size of the parsed OBU in parse_obu_header()
[ffmpeg] / libavcodec / av1_parse.h
1 /*
2  * AV1 common parsing code
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #ifndef AVCODEC_AV1_PARSE_H
22 #define AVCODEC_AV1_PARSE_H
23
24 #include <stdint.h>
25
26 #include "avcodec.h"
27 #include "get_bits.h"
28
29 typedef struct AV1OBU {
30     /** Size of payload */
31     int size;
32     const uint8_t *data;
33
34     /** Size of entire OBU, including header */
35     int raw_size;
36     const uint8_t *raw_data;
37
38     /** GetBitContext initialized to the start of the payload */
39     GetBitContext gb;
40
41     int type;
42
43     int temporal_id;
44     int spatial_id;
45 } AV1OBU;
46
47 /** An input packet split into OBUs */
48 typedef struct AV1Packet {
49     AV1OBU *obus;
50     int nb_obus;
51     int obus_allocated;
52 } AV1Packet;
53
54 /**
55  * Extract an OBU from a raw bitstream.
56  *
57  * @note This function does not copy or store any bistream data. All
58  *       the pointers in the AV1OBU structure will be valid as long
59  *       as the input buffer also is.
60  */
61 int ff_av1_extract_obu(AV1OBU *obu, const uint8_t *buf, int length,
62                        void *logctx);
63
64 /**
65  * Split an input packet into OBUs.
66  *
67  * @note This function does not copy or store any bistream data. All
68  *       the pointers in the AV1Packet structure will be valid as
69  *       long as the input buffer also is.
70  */
71 int ff_av1_packet_split(AV1Packet *pkt, const uint8_t *buf, int length,
72                         void *logctx);
73
74 /**
75  * Free all the allocated memory in the packet.
76  */
77 void ff_av1_packet_uninit(AV1Packet *pkt);
78
79 static inline int64_t leb128(GetBitContext *gb) {
80     int64_t ret = 0;
81     int i;
82
83     for (i = 0; i < 8; i++) {
84         int byte = get_bits(gb, 8);
85         ret |= (int64_t)(byte & 0x7f) << (i * 7);
86         if (!(byte & 0x80))
87             break;
88     }
89     return ret;
90 }
91
92 static inline int parse_obu_header(const uint8_t *buf, int buf_size,
93                                    int64_t *obu_size, int *start_pos, int *type,
94                                    int *temporal_id, int *spatial_id)
95 {
96     GetBitContext gb;
97     int ret, extension_flag, has_size_flag;
98     int64_t size;
99
100     ret = init_get_bits8(&gb, buf, FFMIN(buf_size, 2 + 8)); // OBU header fields + max leb128 length
101     if (ret < 0)
102         return ret;
103
104     if (get_bits1(&gb) != 0) // obu_forbidden_bit
105         return AVERROR_INVALIDDATA;
106
107     *type      = get_bits(&gb, 4);
108     extension_flag = get_bits1(&gb);
109     has_size_flag  = get_bits1(&gb);
110     skip_bits1(&gb); // obu_reserved_1bit
111
112     if (extension_flag) {
113         *temporal_id = get_bits(&gb, 3);
114         *spatial_id  = get_bits(&gb, 2);
115         skip_bits(&gb, 3); // extension_header_reserved_3bits
116     } else {
117         *temporal_id = *spatial_id = 0;
118     }
119
120     *obu_size  = has_size_flag ? leb128(&gb)
121                                : buf_size - 1 - extension_flag;
122
123     if (get_bits_left(&gb) < 0)
124         return AVERROR_INVALIDDATA;
125
126     *start_pos = get_bits_count(&gb) / 8;
127
128     size = *obu_size + *start_pos;
129
130     if (size > INT_MAX)
131         return AVERROR(ERANGE);
132
133     return size;
134 }
135
136 #endif /* AVCODEC_AV1_PARSE_H */