FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/bsf/hevc_mp4toannexb.c
Date: 2024-05-03 15:42:48
Exec Total Coverage
Lines: 72 96 75.0%
Functions: 3 3 100.0%
Branches: 41 64 64.1%

Line Branch Exec 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 #include "hevc.h"
32
33 #define MIN_HEVCC_LENGTH 23
34
35 typedef struct HEVCBSFContext {
36 uint8_t length_size;
37 int extradata_parsed;
38 } HEVCBSFContext;
39
40 1 static int hevc_extradata_to_annexb(AVBSFContext *ctx)
41 {
42 GetByteContext gb;
43 int length_size, num_arrays, i, j;
44 1 int ret = 0;
45
46 1 uint8_t *new_extradata = NULL;
47 1 size_t new_extradata_size = 0;
48
49 1 bytestream2_init(&gb, ctx->par_in->extradata, ctx->par_in->extradata_size);
50
51 1 bytestream2_skip(&gb, 21);
52 1 length_size = (bytestream2_get_byte(&gb) & 3) + 1;
53 1 num_arrays = bytestream2_get_byte(&gb);
54
55
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (i = 0; i < num_arrays; i++) {
56 3 int type = bytestream2_get_byte(&gb) & 0x3f;
57 3 int cnt = bytestream2_get_be16(&gb);
58
59
5/10
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
3 if (!(type == HEVC_NAL_VPS || type == HEVC_NAL_SPS || type == HEVC_NAL_PPS ||
60 type == HEVC_NAL_SEI_PREFIX || type == HEVC_NAL_SEI_SUFFIX)) {
61 av_log(ctx, AV_LOG_ERROR, "Invalid NAL unit type in extradata: %d\n",
62 type);
63 ret = AVERROR_INVALIDDATA;
64 goto fail;
65 }
66
67
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 for (j = 0; j < cnt; j++) {
68 3 const int nalu_len = bytestream2_get_be16(&gb);
69
70
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
6 if (!nalu_len ||
71 3 nalu_len > bytestream2_get_bytes_left(&gb) ||
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 4 + AV_INPUT_BUFFER_PADDING_SIZE + nalu_len > SIZE_MAX - new_extradata_size) {
73 ret = AVERROR_INVALIDDATA;
74 goto fail;
75 }
76 3 ret = av_reallocp(&new_extradata, new_extradata_size + nalu_len + 4 + AV_INPUT_BUFFER_PADDING_SIZE);
77
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
78 goto fail;
79
80 3 AV_WB32(new_extradata + new_extradata_size, 1); // add the startcode
81 3 bytestream2_get_buffer(&gb, new_extradata + new_extradata_size + 4, nalu_len);
82 3 new_extradata_size += 4 + nalu_len;
83 3 memset(new_extradata + new_extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
84 }
85 }
86
87 1 av_freep(&ctx->par_out->extradata);
88 1 ctx->par_out->extradata = new_extradata;
89 1 ctx->par_out->extradata_size = new_extradata_size;
90
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!new_extradata_size)
92 av_log(ctx, AV_LOG_WARNING, "No parameter sets in the extradata\n");
93
94 1 return length_size;
95 fail:
96 av_freep(&new_extradata);
97 return ret;
98 }
99
100 1 static int hevc_mp4toannexb_init(AVBSFContext *ctx)
101 {
102 1 HEVCBSFContext *s = ctx->priv_data;
103 int ret;
104
105
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (ctx->par_in->extradata_size < MIN_HEVCC_LENGTH ||
106
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 AV_RB24(ctx->par_in->extradata) == 1 ||
107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 AV_RB32(ctx->par_in->extradata) == 1) {
108 av_log(ctx, AV_LOG_VERBOSE,
109 "The input looks like it is Annex B already\n");
110 } else {
111 1 ret = hevc_extradata_to_annexb(ctx);
112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
113 return ret;
114 1 s->length_size = ret;
115 1 s->extradata_parsed = 1;
116 }
117
118 1 return 0;
119 }
120
121 97 static int hevc_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out)
122 {
123 97 HEVCBSFContext *s = ctx->priv_data;
124 AVPacket *in;
125 GetByteContext gb;
126
127 97 int got_irap = 0;
128 97 int i, ret = 0;
129
130 97 ret = ff_bsf_get_packet(ctx, &in);
131
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 48 times.
97 if (ret < 0)
132 49 return ret;
133
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (!s->extradata_parsed) {
135 av_packet_move_ref(out, in);
136 av_packet_free(&in);
137 return 0;
138 }
139
140 48 bytestream2_init(&gb, in->data, in->size);
141
142
2/2
✓ Branch 1 taken 382 times.
✓ Branch 2 taken 48 times.
430 while (bytestream2_get_bytes_left(&gb)) {
143 382 uint32_t nalu_size = 0;
144 int nalu_type;
145 int is_irap, add_extradata, extra_size, prev_size;
146
147
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 382 times.
382 if (bytestream2_get_bytes_left(&gb) < s->length_size) {
148 ret = AVERROR_INVALIDDATA;
149 goto fail;
150 }
151
2/2
✓ Branch 0 taken 1528 times.
✓ Branch 1 taken 382 times.
1910 for (i = 0; i < s->length_size; i++)
152 1528 nalu_size = (nalu_size << 8) | bytestream2_get_byte(&gb);
153
154
2/4
✓ Branch 0 taken 382 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 382 times.
382 if (nalu_size < 2 || nalu_size > bytestream2_get_bytes_left(&gb)) {
155 ret = AVERROR_INVALIDDATA;
156 goto fail;
157 }
158
159 382 nalu_type = (bytestream2_peek_byte(&gb) >> 1) & 0x3f;
160
161 /* prepend extradata to IRAP frames */
162
4/4
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 330 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 51 times.
382 is_irap = nalu_type >= HEVC_NAL_BLA_W_LP &&
163 nalu_type <= HEVC_NAL_RSV_IRAP_VCL23;
164
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 381 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
382 add_extradata = is_irap && !got_irap;
165 382 extra_size = add_extradata * ctx->par_out->extradata_size;
166 382 got_irap |= is_irap;
167
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 382 times.
382 if (FFMIN(INT_MAX, SIZE_MAX) < 4ULL + nalu_size + extra_size) {
169 ret = AVERROR_INVALIDDATA;
170 goto fail;
171 }
172
173 382 prev_size = out->size;
174
175 382 ret = av_grow_packet(out, 4 + nalu_size + extra_size);
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 382 times.
382 if (ret < 0)
177 goto fail;
178
179
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 381 times.
382 if (extra_size)
180 1 memcpy(out->data + prev_size, ctx->par_out->extradata, extra_size);
181 382 AV_WB32(out->data + prev_size + extra_size, 1);
182 382 bytestream2_get_buffer(&gb, out->data + prev_size + 4 + extra_size, nalu_size);
183 }
184
185 48 ret = av_packet_copy_props(out, in);
186
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 if (ret < 0)
187 goto fail;
188
189 48 fail:
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (ret < 0)
191 av_packet_unref(out);
192 48 av_packet_free(&in);
193
194 48 return ret;
195 }
196
197 static const enum AVCodecID codec_ids[] = {
198 AV_CODEC_ID_HEVC, AV_CODEC_ID_NONE,
199 };
200
201 const FFBitStreamFilter ff_hevc_mp4toannexb_bsf = {
202 .p.name = "hevc_mp4toannexb",
203 .p.codec_ids = codec_ids,
204 .priv_data_size = sizeof(HEVCBSFContext),
205 .init = hevc_mp4toannexb_init,
206 .filter = hevc_mp4toannexb_filter,
207 };
208