Coverage Report

Created: 2026-10-28 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/bsf/vvc_mp4toannexb.c
Line
Count
Source
1
/*
2
 * H.266/VVC MP4 to Annex B byte stream format filter
3
 * Copyright (c) 2022, Thomas Siedel
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
22
#include <string.h>
23
24
#include "libavutil/intreadwrite.h"
25
#include "libavutil/mem.h"
26
27
#include "libavcodec/bsf.h"
28
#include "libavcodec/bsf_internal.h"
29
#include "libavcodec/bytestream.h"
30
#include "libavcodec/defs.h"
31
#include "libavcodec/vvc.h"
32
33
1.49k
#define MIN_VVCC_LENGTH 23
34
35
typedef struct VVCBSFContext {
36
    uint8_t length_size;
37
    int extradata_parsed;
38
} VVCBSFContext;
39
40
static int vvc_extradata_to_annexb(AVBSFContext *ctx)
41
568
{
42
568
    GetByteContext gb;
43
568
    int length_size, num_arrays, i, j;
44
568
    int ret = 0;
45
568
    int temp = 0;
46
568
    int ptl_present;
47
48
568
    uint8_t *new_extradata = NULL;
49
568
    size_t new_extradata_size = 0;
50
51
568
    int max_picture_width = 0;
52
568
    int max_picture_height = 0;
53
568
    int avg_frame_rate = 0;
54
55
568
    bytestream2_init(&gb, ctx->par_in->extradata, ctx->par_in->extradata_size);
56
568
    temp = bytestream2_get_byte(&gb);
57
568
    length_size = ((temp & 6) >> 1) + 1;
58
568
    ptl_present = temp & 1;
59
568
    if (ptl_present) {
60
314
        int num_bytes_constraint_info;
61
314
        int general_profile_idc;
62
314
        int general_tier_flag;
63
314
        int general_level_idc;
64
314
        int ptl_frame_only_constraint_flag;
65
314
        int ptl_multi_layer_enabled_flag;
66
314
        int ptl_num_sub_profiles;
67
314
        int temp3, temp4, temp5;
68
314
        int temp2 = bytestream2_get_be16(&gb);
69
314
        int ols_idx = (temp2 >> 7) & 0x1ff;
70
314
        int num_sublayers = (temp2 >> 4) & 0x7;
71
314
        int constant_frame_rate = (temp2 >> 2) & 0x3;
72
314
        int chroma_format_idc = temp2 & 0x3;
73
314
        int bit_depth_minus8 = (bytestream2_get_byte(&gb) >> 5) & 0x7;
74
314
        av_log(ctx, AV_LOG_DEBUG,
75
314
               "bit_depth_minus8 %d chroma_format_idc %d\n", bit_depth_minus8,
76
314
               chroma_format_idc);
77
314
        av_log(ctx, AV_LOG_DEBUG, "constant_frame_rate %d, ols_idx %d\n",
78
314
               constant_frame_rate, ols_idx);
79
        // VvcPTLRecord(num_sublayers) native_ptl
80
314
        temp3 = bytestream2_get_byte(&gb);
81
314
        num_bytes_constraint_info = (temp3) & 0x3f;
82
314
        temp4 = bytestream2_get_byte(&gb);
83
314
        general_profile_idc = (temp4 >> 1) & 0x7f;
84
314
        general_tier_flag = (temp4) & 1;
85
314
        general_level_idc = bytestream2_get_byte(&gb);
86
314
        av_log(ctx, AV_LOG_DEBUG,
87
314
               "general_profile_idc %d, general_tier_flag %d, general_level_idc %d, num_sublayers %d num_bytes_constraint_info %d\n",
88
314
               general_profile_idc, general_tier_flag, general_level_idc,
89
314
               num_sublayers, num_bytes_constraint_info);
90
91
314
        temp5 = bytestream2_get_byte(&gb);
92
314
        ptl_frame_only_constraint_flag = (temp5 >> 7) & 0x1;
93
314
        ptl_multi_layer_enabled_flag   = (temp5 >> 6) & 0x1;
94
8.02k
        for (i = 0; i < num_bytes_constraint_info - 1; i++) {
95
            // unsigned int(8*num_bytes_constraint_info - 2) general_constraint_info;
96
7.71k
            bytestream2_get_byte(&gb);
97
7.71k
        }
98
99
314
        av_log(ctx, AV_LOG_DEBUG,
100
314
               "ptl_multi_layer_enabled_flag %d, ptl_frame_only_constraint_flag %d\n",
101
314
               ptl_multi_layer_enabled_flag, ptl_frame_only_constraint_flag);
102
103
314
        if (num_sublayers > 1) {
104
197
            int temp6 = bytestream2_get_byte(&gb);
105
197
            uint8_t ptl_sublayer_level_present_flag[8] = { 0 };
106
            //uint8_t sublayer_level_idc[8] = {0};
107
972
            for (i = num_sublayers - 2; i >= 0; i--) {
108
775
                ptl_sublayer_level_present_flag[i] =
109
775
                    (temp6 >> (7 - (num_sublayers - 2 - i))) & 0x01;
110
775
            }
111
            // for (j=num_sublayers; j<=8 && num_sublayers > 1; j++)
112
            //     bit(1) ptl_reserved_zero_bit = 0;
113
972
            for (i = num_sublayers - 2; i >= 0; i--) {
114
775
                if (ptl_sublayer_level_present_flag[i]) {
115
                    //sublayer_level_idc[i] = bytestream2_get_byte(&gb);
116
235
                }
117
775
            }
118
197
        }
119
120
314
        ptl_num_sub_profiles = bytestream2_get_byte(&gb);
121
21.7k
        for (j = 0; j < ptl_num_sub_profiles; j++) {
122
            // unsigned int(32) general_sub_profile_idc[j];
123
21.4k
            bytestream2_get_be16(&gb);
124
21.4k
            bytestream2_get_be16(&gb);
125
21.4k
        }
126
127
314
        max_picture_width = bytestream2_get_be16(&gb);  // unsigned_int(16) max_picture_width;
128
314
        max_picture_height = bytestream2_get_be16(&gb); // unsigned_int(16) max_picture_height;
129
314
        avg_frame_rate = bytestream2_get_be16(&gb);     // unsigned int(16) avg_frame_rate; }
130
314
        av_log(ctx, AV_LOG_DEBUG,
131
314
               "max_picture_width %d, max_picture_height %d, avg_frame_rate %d\n",
132
314
               max_picture_width, max_picture_height, avg_frame_rate);
133
314
    }
134
135
568
    num_arrays = bytestream2_get_byte(&gb);
136
137
2.29k
    for (i = 0; i < num_arrays; i++) {
138
1.92k
        int cnt;
139
1.92k
        int type = bytestream2_get_byte(&gb) & 0x1f;
140
141
1.92k
        if (type == VVC_OPI_NUT || type == VVC_DCI_NUT)
142
1.09k
            cnt = 1;
143
832
        else
144
832
            cnt = bytestream2_get_be16(&gb);
145
146
1.92k
        av_log(ctx, AV_LOG_DEBUG, "nalu_type %d cnt %d\n", type, cnt);
147
148
1.92k
        if (!(type == VVC_OPI_NUT || type == VVC_DCI_NUT ||
149
832
              type == VVC_VPS_NUT || type == VVC_SPS_NUT || type == VVC_PPS_NUT
150
362
              || type == VVC_PREFIX_SEI_NUT || type == VVC_SUFFIX_SEI_NUT)) {
151
102
            av_log(ctx, AV_LOG_ERROR,
152
102
                   "Invalid NAL unit type in extradata: %d\n", type);
153
102
            ret = AVERROR_INVALIDDATA;
154
102
            goto fail;
155
102
        }
156
157
3.89k
        for (j = 0; j < cnt; j++) {
158
2.16k
            const int nalu_len = bytestream2_get_be16(&gb);
159
160
2.16k
            if (!nalu_len ||
161
2.14k
                nalu_len > bytestream2_get_bytes_left(&gb) ||
162
2.07k
                4 + AV_INPUT_BUFFER_PADDING_SIZE + nalu_len > SIZE_MAX - new_extradata_size) {
163
94
                ret = AVERROR_INVALIDDATA;
164
94
                goto fail;
165
94
            }
166
2.07k
            ret = av_reallocp(&new_extradata, new_extradata_size + nalu_len + 4
167
2.07k
                              + AV_INPUT_BUFFER_PADDING_SIZE);
168
2.07k
            if (ret < 0)
169
0
                goto fail;
170
171
2.07k
            AV_WB32(new_extradata + new_extradata_size, 1); // add the startcode
172
2.07k
            bytestream2_get_buffer(&gb, new_extradata + new_extradata_size + 4,
173
2.07k
                                   nalu_len);
174
2.07k
            new_extradata_size += 4 + nalu_len;
175
2.07k
            memset(new_extradata + new_extradata_size, 0,
176
2.07k
                   AV_INPUT_BUFFER_PADDING_SIZE);
177
2.07k
        }
178
1.82k
    }
179
180
372
    av_freep(&ctx->par_out->extradata);
181
372
    ctx->par_out->extradata = new_extradata;
182
372
    ctx->par_out->extradata_size = new_extradata_size;
183
184
372
    if (!new_extradata_size)
185
321
        av_log(ctx, AV_LOG_WARNING, "No parameter sets in the extradata\n");
186
187
372
    return length_size;
188
196
  fail:
189
196
    av_freep(&new_extradata);
190
196
    return ret;
191
568
}
192
193
static int vvc_mp4toannexb_init(AVBSFContext *ctx)
194
749
{
195
749
    VVCBSFContext *s = ctx->priv_data;
196
749
    int ret;
197
198
749
    if (ctx->par_in->extradata_size < MIN_VVCC_LENGTH ||
199
570
        AV_RB24(ctx->par_in->extradata) == 1 ||
200
569
        AV_RB32(ctx->par_in->extradata) == 1) {
201
181
        av_log(ctx, AV_LOG_VERBOSE,
202
181
               "The input looks like it is Annex B already\n");
203
568
    } else {
204
568
        ret = vvc_extradata_to_annexb(ctx);
205
568
        if (ret < 0)
206
196
            return ret;
207
372
        s->length_size = ret;
208
372
        s->extradata_parsed = 1;
209
372
    }
210
211
553
    return 0;
212
749
}
213
214
static int vvc_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out)
215
24.2k
{
216
24.2k
    VVCBSFContext *s = ctx->priv_data;
217
24.2k
    AVPacket *in;
218
24.2k
    GetByteContext gb;
219
220
24.2k
    int is_irap = 0;
221
24.2k
    int added_extra = 0;
222
24.2k
    int i, ret = 0;
223
224
24.2k
    ret = ff_bsf_get_packet(ctx, &in);
225
24.2k
    if (ret < 0)
226
7.40k
        return ret;
227
228
16.8k
    if (!s->extradata_parsed) {
229
3.49k
        av_packet_move_ref(out, in);
230
3.49k
        av_packet_free(&in);
231
3.49k
        return 0;
232
3.49k
    }
233
234
13.3k
    bytestream2_init(&gb, in->data, in->size);
235
236
    /* check if this packet contains an IRAP. The extradata will need to be added before any potential PH_NUT */
237
144k
    while (bytestream2_get_bytes_left(&gb)) {
238
141k
        uint32_t nalu_size = 0;
239
141k
        int nalu_type;
240
241
141k
        if (bytestream2_get_bytes_left(&gb) < s->length_size) {
242
1.26k
            ret = AVERROR_INVALIDDATA;
243
1.26k
            goto fail;
244
1.26k
        }
245
246
288k
        for (i = 0; i < s->length_size; i++)
247
148k
            nalu_size = (nalu_size << 8) | bytestream2_get_byte(&gb);
248
249
140k
        if (nalu_size < 2 || nalu_size > bytestream2_get_bytes_left(&gb)) {
250
6.45k
            ret = AVERROR_INVALIDDATA;
251
6.45k
            goto fail;
252
6.45k
        }
253
254
134k
        nalu_type = (bytestream2_peek_be16(&gb) >> 3) & 0x1f;
255
134k
        is_irap = nalu_type >= VVC_IDR_W_RADL && nalu_type <= VVC_RSV_IRAP_11;
256
134k
        if (is_irap) {
257
2.71k
            break;
258
2.71k
        }
259
131k
        bytestream2_seek(&gb, nalu_size, SEEK_CUR);
260
131k
    }
261
262
5.66k
    bytestream2_seek(&gb, 0, SEEK_SET);
263
644k
    while (bytestream2_get_bytes_left(&gb)) {
264
641k
        uint32_t nalu_size = 0;
265
641k
        int nalu_type;
266
641k
        int add_extradata, extra_size, prev_size;
267
268
641k
        if (bytestream2_get_bytes_left(&gb) < s->length_size) {
269
326
            ret = AVERROR_INVALIDDATA;
270
326
            goto fail;
271
326
        }
272
273
1.28M
        for (i = 0; i < s->length_size; i++)
274
644k
            nalu_size = (nalu_size << 8) | bytestream2_get_byte(&gb);
275
276
641k
        if (nalu_size < 2 || nalu_size > bytestream2_get_bytes_left(&gb)) {
277
1.98k
            ret = AVERROR_INVALIDDATA;
278
1.98k
            goto fail;
279
1.98k
        }
280
281
639k
        nalu_type = (bytestream2_peek_be16(&gb) >> 3) & 0x1f;
282
283
        /* prepend extradata to IRAP frames */
284
639k
        add_extradata = is_irap && nalu_type != VVC_AUD_NUT && !added_extra;
285
639k
        extra_size = add_extradata * ctx->par_out->extradata_size;
286
639k
        added_extra |= add_extradata;
287
288
639k
        if (FFMIN(INT_MAX, SIZE_MAX) < 4ULL + nalu_size + extra_size) {
289
0
            ret = AVERROR_INVALIDDATA;
290
0
            goto fail;
291
0
        }
292
293
639k
        prev_size = out->size;
294
295
639k
        ret = av_grow_packet(out, 4 + nalu_size + extra_size);
296
639k
        if (ret < 0)
297
0
            goto fail;
298
299
639k
        if (extra_size)
300
915
            memcpy(out->data + prev_size, ctx->par_out->extradata, extra_size);
301
639k
        AV_WB32(out->data + prev_size + extra_size, 1);
302
639k
        bytestream2_get_buffer(&gb, out->data + prev_size + 4 + extra_size,
303
639k
                               nalu_size);
304
639k
    }
305
306
3.35k
    ret = av_packet_copy_props(out, in);
307
3.35k
    if (ret < 0)
308
0
        goto fail;
309
310
13.3k
  fail:
311
13.3k
    if (ret < 0)
312
10.0k
        av_packet_unref(out);
313
13.3k
    av_packet_free(&in);
314
315
13.3k
    return ret;
316
3.35k
}
317
318
static const enum AVCodecID codec_ids[] = {
319
    AV_CODEC_ID_VVC, AV_CODEC_ID_NONE,
320
};
321
322
const FFBitStreamFilter ff_vvc_mp4toannexb_bsf = {
323
    .p.name         = "vvc_mp4toannexb",
324
    .p.codec_ids    = codec_ids,
325
    .priv_data_size = sizeof(VVCBSFContext),
326
    .init           = vvc_mp4toannexb_init,
327
    .filter         = vvc_mp4toannexb_filter,
328
};