GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/av1_parse.h Lines: 44 48 91.7 %
Date: 2019-11-22 03:34:36 Branches: 26 32 81.2 %

Line Branch Exec Source
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 "av1.h"
27
#include "avcodec.h"
28
#include "get_bits.h"
29
30
typedef struct AV1OBU {
31
    /** Size of payload */
32
    int size;
33
    const uint8_t *data;
34
35
    /**
36
     * Size, in bits, of just the data, excluding the trailing_one_bit and
37
     * any trailing padding.
38
     */
39
    int size_bits;
40
41
    /** Size of entire OBU, including header */
42
    int raw_size;
43
    const uint8_t *raw_data;
44
45
    /** GetBitContext initialized to the start of the payload */
46
    GetBitContext gb;
47
48
    int type;
49
50
    int temporal_id;
51
    int spatial_id;
52
} AV1OBU;
53
54
/** An input packet split into OBUs */
55
typedef struct AV1Packet {
56
    AV1OBU *obus;
57
    int nb_obus;
58
    int obus_allocated;
59
    unsigned obus_allocated_size;
60
} AV1Packet;
61
62
/**
63
 * Extract an OBU from a raw bitstream.
64
 *
65
 * @note This function does not copy or store any bitstream data. All
66
 *       the pointers in the AV1OBU structure will be valid as long
67
 *       as the input buffer also is.
68
 */
69
int ff_av1_extract_obu(AV1OBU *obu, const uint8_t *buf, int length,
70
                       void *logctx);
71
72
/**
73
 * Split an input packet into OBUs.
74
 *
75
 * @note This function does not copy or store any bitstream data. All
76
 *       the pointers in the AV1Packet structure will be valid as
77
 *       long as the input buffer also is.
78
 */
79
int ff_av1_packet_split(AV1Packet *pkt, const uint8_t *buf, int length,
80
                        void *logctx);
81
82
/**
83
 * Free all the allocated memory in the packet.
84
 */
85
void ff_av1_packet_uninit(AV1Packet *pkt);
86
87
1025
static inline int64_t leb128(GetBitContext *gb) {
88
1025
    int64_t ret = 0;
89
    int i;
90
91
1516
    for (i = 0; i < 8; i++) {
92
1510
        int byte = get_bits(gb, 8);
93
1510
        ret |= (int64_t)(byte & 0x7f) << (i * 7);
94
1510
        if (!(byte & 0x80))
95
1019
            break;
96
    }
97
1025
    return ret;
98
}
99
100
1154
static inline int parse_obu_header(const uint8_t *buf, int buf_size,
101
                                   int64_t *obu_size, int *start_pos, int *type,
102
                                   int *temporal_id, int *spatial_id)
103
{
104
    GetBitContext gb;
105
    int ret, extension_flag, has_size_flag;
106
    int64_t size;
107
108
1154
    ret = init_get_bits8(&gb, buf, FFMIN(buf_size, 2 + 8)); // OBU header fields + max leb128 length
109
1154
    if (ret < 0)
110
        return ret;
111
112
1154
    if (get_bits1(&gb) != 0) // obu_forbidden_bit
113
34
        return AVERROR_INVALIDDATA;
114
115
1120
    *type      = get_bits(&gb, 4);
116
1120
    extension_flag = get_bits1(&gb);
117
1120
    has_size_flag  = get_bits1(&gb);
118
1120
    skip_bits1(&gb); // obu_reserved_1bit
119
120
1120
    if (extension_flag) {
121
976
        *temporal_id = get_bits(&gb, 3);
122
976
        *spatial_id  = get_bits(&gb, 2);
123
976
        skip_bits(&gb, 3); // extension_header_reserved_3bits
124
    } else {
125
144
        *temporal_id = *spatial_id = 0;
126
    }
127
128
1025
    *obu_size  = has_size_flag ? leb128(&gb)
129
1120
                               : buf_size - 1 - extension_flag;
130
131
1120
    if (get_bits_left(&gb) < 0)
132
20
        return AVERROR_INVALIDDATA;
133
134
1100
    *start_pos = get_bits_count(&gb) / 8;
135
136
1100
    size = *obu_size + *start_pos;
137
138
1100
    if (size > buf_size)
139
614
        return AVERROR_INVALIDDATA;
140
141
486
    return size;
142
}
143
144
37
static inline int get_obu_bit_length(const uint8_t *buf, int size, int type)
145
{
146
    int v;
147
148
    /* There are no trailing bits on these */
149

37
    if (type == AV1_OBU_TILE_GROUP || type == AV1_OBU_FRAME) {
150
11
        if (size > INT_MAX / 8)
151
            return AVERROR(ERANGE);
152
        else
153
11
            return size * 8;
154
    }
155
156

26
    while (size > 0 && buf[size - 1] == 0)
157
        size--;
158
159
26
    if (!size)
160
11
        return 0;
161
162
15
    v = buf[size - 1];
163
164
15
    if (size > INT_MAX / 8)
165
        return AVERROR(ERANGE);
166
15
    size *= 8;
167
168
    /* Remove the trailing_one_bit and following trailing zeros */
169
15
    if (v)
170
15
        size -= ff_ctz(v) + 1;
171
172
15
    return size;
173
}
174
175
#endif /* AVCODEC_AV1_PARSE_H */