Coverage Report

Created: 2025-12-31 07:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/bsf/hevc_mp4toannexb.c
Line
Count
Source
1
/*
2
 * HEVC MP4 to Annex B byte stream format filter
3
 * copyright (c) 2015 Anton Khirnov
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 "bsf.h"
28
#include "bsf_internal.h"
29
#include "bytestream.h"
30
#include "defs.h"
31
32
#include "hevc/hevc.h"
33
34
2.22k
#define MIN_HEVCC_LENGTH 23
35
36
typedef struct HEVCBSFContext {
37
    uint8_t  length_size;
38
    int      extradata_parsed;
39
} HEVCBSFContext;
40
41
static int hevc_extradata_to_annexb(AVBSFContext *ctx)
42
733
{
43
733
    GetByteContext gb;
44
733
    int length_size, num_arrays, i, j;
45
733
    int ret = 0;
46
47
733
    uint8_t *new_extradata = NULL;
48
733
    size_t   new_extradata_size = 0;
49
50
733
    bytestream2_init(&gb, ctx->par_in->extradata, ctx->par_in->extradata_size);
51
52
733
    bytestream2_skip(&gb, 21);
53
733
    length_size = (bytestream2_get_byte(&gb) & 3) + 1;
54
733
    num_arrays  = bytestream2_get_byte(&gb);
55
56
4.30k
    for (i = 0; i < num_arrays; i++) {
57
3.96k
        int type = bytestream2_get_byte(&gb) & 0x3f;
58
3.96k
        int cnt  = bytestream2_get_be16(&gb);
59
60
3.96k
        if (!(type == HEVC_NAL_VPS || type == HEVC_NAL_SPS || type == HEVC_NAL_PPS ||
61
3.08k
              type == HEVC_NAL_SEI_PREFIX || type == HEVC_NAL_SEI_SUFFIX)) {
62
165
            av_log(ctx, AV_LOG_ERROR, "Invalid NAL unit type in extradata: %d\n",
63
165
                   type);
64
165
            ret = AVERROR_INVALIDDATA;
65
165
            goto fail;
66
165
        }
67
68
6.09k
        for (j = 0; j < cnt; j++) {
69
2.53k
            const int nalu_len = bytestream2_get_be16(&gb);
70
71
2.53k
            if (!nalu_len ||
72
2.46k
                nalu_len > bytestream2_get_bytes_left(&gb) ||
73
2.30k
                4 + AV_INPUT_BUFFER_PADDING_SIZE + nalu_len > SIZE_MAX - new_extradata_size) {
74
228
                ret = AVERROR_INVALIDDATA;
75
228
                goto fail;
76
228
            }
77
2.30k
            ret = av_reallocp(&new_extradata, new_extradata_size + nalu_len + 4 + AV_INPUT_BUFFER_PADDING_SIZE);
78
2.30k
            if (ret < 0)
79
0
                goto fail;
80
81
2.30k
            AV_WB32(new_extradata + new_extradata_size, 1); // add the startcode
82
2.30k
            bytestream2_get_buffer(&gb, new_extradata + new_extradata_size + 4, nalu_len);
83
2.30k
            new_extradata_size += 4 + nalu_len;
84
2.30k
            memset(new_extradata + new_extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
85
2.30k
        }
86
3.79k
    }
87
88
340
    av_freep(&ctx->par_out->extradata);
89
340
    ctx->par_out->extradata      = new_extradata;
90
340
    ctx->par_out->extradata_size = new_extradata_size;
91
92
340
    if (!new_extradata_size)
93
296
        av_log(ctx, AV_LOG_WARNING, "No parameter sets in the extradata\n");
94
95
340
    return length_size;
96
393
fail:
97
393
    av_freep(&new_extradata);
98
393
    return ret;
99
733
}
100
101
static int hevc_mp4toannexb_init(AVBSFContext *ctx)
102
1.11k
{
103
1.11k
    HEVCBSFContext *s = ctx->priv_data;
104
1.11k
    int ret;
105
106
1.11k
    if (ctx->par_in->extradata_size < MIN_HEVCC_LENGTH ||
107
738
        AV_RB24(ctx->par_in->extradata) == 1           ||
108
736
        AV_RB32(ctx->par_in->extradata) == 1) {
109
379
        av_log(ctx, AV_LOG_VERBOSE,
110
379
               "The input looks like it is Annex B already\n");
111
733
    } else {
112
733
        ret = hevc_extradata_to_annexb(ctx);
113
733
        if (ret < 0)
114
393
            return ret;
115
340
        s->length_size      = ret;
116
340
        s->extradata_parsed = 1;
117
340
    }
118
119
719
    return 0;
120
1.11k
}
121
122
static int hevc_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out)
123
19.9k
{
124
19.9k
    HEVCBSFContext *s = ctx->priv_data;
125
19.9k
    AVPacket *in;
126
19.9k
    GetByteContext gb;
127
128
19.9k
    int got_irap = 0;
129
19.9k
    int got_ps = 0, seen_irap_ps = 0;
130
19.9k
    int i, ret = 0;
131
132
19.9k
    ret = ff_bsf_get_packet(ctx, &in);
133
19.9k
    if (ret < 0)
134
6.04k
        return ret;
135
136
13.8k
    if (!s->extradata_parsed) {
137
3.47k
        av_packet_move_ref(out, in);
138
3.47k
        av_packet_free(&in);
139
3.47k
        return 0;
140
3.47k
    }
141
142
10.3k
    bytestream2_init(&gb, in->data, in->size);
143
144
255k
    while (!got_irap && bytestream2_get_bytes_left(&gb)) {
145
251k
        uint32_t nalu_size = 0;
146
251k
        int      nalu_type;
147
148
251k
        if (bytestream2_get_bytes_left(&gb) < s->length_size) {
149
835
            ret = AVERROR_INVALIDDATA;
150
835
            goto fail;
151
835
        }
152
508k
        for (i = 0; i < s->length_size; i++)
153
257k
            nalu_size = (nalu_size << 8) | bytestream2_get_byte(&gb);
154
155
250k
        if (nalu_size < 2 || nalu_size > bytestream2_get_bytes_left(&gb)) {
156
5.45k
            ret = AVERROR_INVALIDDATA;
157
5.45k
            goto fail;
158
5.45k
        }
159
160
245k
        nalu_type = (bytestream2_peek_byte(&gb) >> 1) & 0x3f;
161
245k
        bytestream2_skip(&gb, nalu_size);
162
245k
        got_irap |= nalu_type >= HEVC_NAL_BLA_W_LP &&
163
4.93k
                    nalu_type <= HEVC_NAL_RSV_IRAP_VCL23;
164
245k
        got_ps   |= nalu_type >= HEVC_NAL_VPS && nalu_type <= HEVC_NAL_PPS;
165
245k
    }
166
4.10k
    seen_irap_ps = got_irap && got_ps;
167
4.10k
    got_irap = got_ps = 0;
168
169
4.10k
    bytestream2_init(&gb, in->data, in->size);
170
171
944k
    while (bytestream2_get_bytes_left(&gb)) {
172
942k
        uint32_t nalu_size = 0;
173
942k
        int      nalu_type;
174
942k
        int is_irap, is_ps, add_extradata, extra_size, prev_size;
175
176
942k
        if (bytestream2_get_bytes_left(&gb) < s->length_size) {
177
586
            ret = AVERROR_INVALIDDATA;
178
586
            goto fail;
179
586
        }
180
1.88M
        for (i = 0; i < s->length_size; i++)
181
943k
            nalu_size = (nalu_size << 8) | bytestream2_get_byte(&gb);
182
183
942k
        if (nalu_size < 2 || nalu_size > bytestream2_get_bytes_left(&gb)) {
184
1.44k
            ret = AVERROR_INVALIDDATA;
185
1.44k
            goto fail;
186
1.44k
        }
187
188
940k
        nalu_type = (bytestream2_peek_byte(&gb) >> 1) & 0x3f;
189
190
        /* prepend extradata to IRAP frames */
191
940k
        is_irap = nalu_type >= HEVC_NAL_BLA_W_LP &&
192
5.71k
                  nalu_type <= HEVC_NAL_RSV_IRAP_VCL23;
193
940k
        is_ps   = nalu_type >= HEVC_NAL_VPS && nalu_type <= HEVC_NAL_PPS && seen_irap_ps;
194
940k
        add_extradata = (is_ps || is_irap) && !got_ps && !got_irap;
195
940k
        extra_size    = add_extradata * ctx->par_out->extradata_size;
196
940k
        got_irap     |= is_irap;
197
940k
        got_ps       |= is_ps;
198
199
940k
        if (FFMIN(INT_MAX, SIZE_MAX) < 4ULL + nalu_size + extra_size) {
200
0
            ret = AVERROR_INVALIDDATA;
201
0
            goto fail;
202
0
        }
203
204
940k
        prev_size = out->size;
205
206
940k
        ret = av_grow_packet(out, 4 + nalu_size + extra_size);
207
940k
        if (ret < 0)
208
0
            goto fail;
209
210
940k
        if (extra_size)
211
698
            memcpy(out->data + prev_size, ctx->par_out->extradata, extra_size);
212
940k
        AV_WB32(out->data + prev_size + extra_size, 1);
213
940k
        bytestream2_get_buffer(&gb, out->data + prev_size + 4 + extra_size, nalu_size);
214
940k
    }
215
216
2.07k
    ret = av_packet_copy_props(out, in);
217
2.07k
    if (ret < 0)
218
0
        goto fail;
219
220
10.3k
fail:
221
10.3k
    if (ret < 0)
222
8.31k
        av_packet_unref(out);
223
10.3k
    av_packet_free(&in);
224
225
10.3k
    return ret;
226
2.07k
}
227
228
static const enum AVCodecID codec_ids[] = {
229
    AV_CODEC_ID_HEVC, AV_CODEC_ID_NONE,
230
};
231
232
const FFBitStreamFilter ff_hevc_mp4toannexb_bsf = {
233
    .p.name         = "hevc_mp4toannexb",
234
    .p.codec_ids    = codec_ids,
235
    .priv_data_size = sizeof(HEVCBSFContext),
236
    .init           = hevc_mp4toannexb_init,
237
    .filter         = hevc_mp4toannexb_filter,
238
};