FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/bsf/h264_mp4toannexb.c
Date: 2024-02-29 09:57:37
Exec Total Coverage
Lines: 162 203 79.8%
Functions: 6 7 85.7%
Branches: 90 128 70.3%

Line Branch Exec Source
1 /*
2 * H.264 MP4 to Annex B byte stream format filter
3 * Copyright (c) 2007 Benoit Fouet <benoit.fouet@free.fr>
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/avassert.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/mem.h"
27
28 #include "bsf.h"
29 #include "bsf_internal.h"
30 #include "bytestream.h"
31 #include "defs.h"
32 #include "h264.h"
33
34 typedef struct H264BSFContext {
35 uint8_t *sps;
36 uint8_t *pps;
37 int sps_size;
38 int pps_size;
39 unsigned sps_buf_size;
40 unsigned pps_buf_size;
41 uint8_t length_size;
42 uint8_t new_idr;
43 uint8_t idr_sps_seen;
44 uint8_t idr_pps_seen;
45 int extradata_parsed;
46 } H264BSFContext;
47
48 enum PsSource {
49 PS_OUT_OF_BAND = -1,
50 PS_NONE = 0,
51 PS_IN_BAND = 1,
52 };
53
54 3582 static void count_or_copy(uint8_t **out, uint64_t *out_size,
55 const uint8_t *in, int in_size, enum PsSource ps, int copy)
56 {
57 uint8_t start_code_size;
58
59
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 3514 times.
3582 if (ps == PS_OUT_OF_BAND)
60 /* start code already present in out-of-band ps data, so don't need to
61 * add it manually again
62 */
63 68 start_code_size = 0;
64
4/4
✓ Branch 0 taken 3510 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 821 times.
✓ Branch 3 taken 2689 times.
3514 else if (ps == PS_IN_BAND || *out_size == 0)
65 825 start_code_size = 4;
66 else
67 2689 start_code_size = 3;
68
69
2/2
✓ Branch 0 taken 1789 times.
✓ Branch 1 taken 1793 times.
3582 if (copy) {
70 1789 memcpy(*out + start_code_size, in, in_size);
71
2/2
✓ Branch 0 taken 412 times.
✓ Branch 1 taken 1377 times.
1789 if (start_code_size == 4) {
72 412 AV_WB32(*out, 1);
73
2/2
✓ Branch 0 taken 1343 times.
✓ Branch 1 taken 34 times.
1377 } else if (start_code_size) {
74 1343 (*out)[0] =
75 1343 (*out)[1] = 0;
76 1343 (*out)[2] = 1;
77 }
78 1789 *out += start_code_size + in_size;
79 }
80 3582 *out_size += start_code_size + in_size;
81 3582 }
82
83 10 static int h264_extradata_to_annexb(AVBSFContext *ctx,
84 uint8_t *extradata, int extradata_size)
85 {
86 10 H264BSFContext *s = ctx->priv_data;
87 10 GetByteContext ogb, *gb = &ogb;
88 uint16_t unit_size;
89 10 uint32_t total_size = 0;
90 10 uint8_t *out = NULL, unit_nb, sps_done = 0;
91 static const uint8_t nalu_header[4] = { 0, 0, 0, 1 };
92 10 const int padding = AV_INPUT_BUFFER_PADDING_SIZE;
93 10 int length_size, pps_offset = 0;
94
95 10 bytestream2_init(gb, extradata, extradata_size);
96
97 10 bytestream2_skipu(gb, 4);
98
99 /* retrieve length coded size */
100 10 length_size = (bytestream2_get_byteu(gb) & 0x3) + 1;
101
102 /* retrieve sps and pps unit(s) */
103 10 unit_nb = bytestream2_get_byteu(gb) & 0x1f; /* number of sps unit(s) */
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!unit_nb) {
105 goto pps;
106 }
107
108
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 10 times.
30 while (unit_nb--) {
109 int err;
110
111 /* possible overread ok due to padding */
112 20 unit_size = bytestream2_get_be16u(gb);
113 20 total_size += unit_size + 4;
114 av_assert1(total_size <= INT_MAX - padding);
115
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
20 if (bytestream2_get_bytes_left(gb) < unit_size + !sps_done) {
116 av_log(ctx, AV_LOG_ERROR, "Global extradata truncated, "
117 "corrupted stream or invalid MP4/AVCC bitstream\n");
118 av_free(out);
119 return AVERROR_INVALIDDATA;
120 }
121
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
20 if ((err = av_reallocp(&out, total_size + padding)) < 0)
122 return err;
123 20 memcpy(out + total_size - unit_size - 4, nalu_header, 4);
124 20 bytestream2_get_bufferu(gb, out + total_size - unit_size, unit_size);
125 20 pps:
126
3/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 10 times.
20 if (!unit_nb && !sps_done++) {
127 10 unit_nb = bytestream2_get_byteu(gb); /* number of pps unit(s) */
128 10 pps_offset = total_size;
129 }
130 }
131
132
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (out)
133 10 memset(out + total_size, 0, padding);
134
135
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (pps_offset) {
136 uint8_t *sps;
137
138 10 s->sps_size = pps_offset;
139 10 sps = av_fast_realloc(s->sps, &s->sps_buf_size, s->sps_size);
140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!sps) {
141 av_free(out);
142 return AVERROR(ENOMEM);
143 }
144 10 s->sps = sps;
145 10 memcpy(s->sps, out, s->sps_size);
146 } else {
147 av_log(ctx, AV_LOG_WARNING,
148 "Warning: SPS NALU missing or invalid. "
149 "The resulting stream may not play.\n");
150 }
151
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (pps_offset < total_size) {
152 uint8_t *pps;
153
154 10 s->pps_size = total_size - pps_offset;
155 10 pps = av_fast_realloc(s->pps, &s->pps_buf_size, s->pps_size);
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!pps) {
157 av_freep(&s->sps);
158 av_free(out);
159 return AVERROR(ENOMEM);
160 }
161 10 s->pps = pps;
162 10 memcpy(s->pps, out + pps_offset, s->pps_size);
163 } else {
164 av_log(ctx, AV_LOG_WARNING,
165 "Warning: PPS NALU missing or invalid. "
166 "The resulting stream may not play.\n");
167 }
168
169 10 av_freep(&ctx->par_out->extradata);
170 10 ctx->par_out->extradata = out;
171 10 ctx->par_out->extradata_size = total_size;
172
173 10 s->length_size = length_size;
174 10 s->new_idr = 1;
175 10 s->idr_sps_seen = 0;
176 10 s->idr_pps_seen = 0;
177 10 s->extradata_parsed = 1;
178
179 10 return 0;
180 }
181
182 2 static int h264_mp4toannexb_save_ps(uint8_t **dst, int *dst_size,
183 unsigned *dst_buf_size,
184 const uint8_t *nal, uint32_t nal_size,
185 int first)
186 {
187 static const uint8_t nalu_header[4] = { 0, 0, 0, 1 };
188 2 const int start_code_size = sizeof(nalu_header);
189 uint8_t *ptr;
190 uint32_t size;
191
192
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (first)
193 2 size = 0;
194 else
195 size = *dst_size;
196
197 2 ptr = av_fast_realloc(*dst, dst_buf_size, size + nal_size + start_code_size);
198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!ptr)
199 return AVERROR(ENOMEM);
200
201 2 memcpy(ptr + size, nalu_header, start_code_size);
202 2 size += start_code_size;
203 2 memcpy(ptr + size, nal, nal_size);
204 2 size += nal_size;
205
206 2 *dst = ptr;
207 2 *dst_size = size;
208 2 return 0;
209 }
210
211 7 static int h264_mp4toannexb_init(AVBSFContext *ctx)
212 {
213 7 int extra_size = ctx->par_in->extradata_size;
214
215 /* retrieve sps and pps NAL units from extradata */
216
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
7 if (!extra_size ||
217
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
7 (extra_size >= 3 && AV_RB24(ctx->par_in->extradata) == 1) ||
218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 (extra_size >= 4 && AV_RB32(ctx->par_in->extradata) == 1)) {
219 av_log(ctx, AV_LOG_VERBOSE,
220 "The input looks like it is Annex B already\n");
221
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 } else if (extra_size >= 7) {
222 7 return h264_extradata_to_annexb(ctx,
223 7 ctx->par_in->extradata,
224 7 ctx->par_in->extradata_size);
225 } else {
226 av_log(ctx, AV_LOG_ERROR, "Invalid extradata size: %d\n", extra_size);
227 return AVERROR_INVALIDDATA;
228 }
229
230 return 0;
231 }
232
233 831 static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *opkt)
234 {
235 831 H264BSFContext *s = ctx->priv_data;
236 AVPacket *in;
237 uint8_t unit_type, new_idr, sps_seen, pps_seen;
238 const uint8_t *buf;
239 const uint8_t *buf_end;
240 uint8_t *out;
241 uint64_t out_size;
242 int ret;
243 size_t extradata_size;
244 uint8_t *extradata;
245
246 831 ret = ff_bsf_get_packet(ctx, &in);
247
2/2
✓ Branch 0 taken 419 times.
✓ Branch 1 taken 412 times.
831 if (ret < 0)
248 419 return ret;
249
250 412 extradata = av_packet_get_side_data(in, AV_PKT_DATA_NEW_EXTRADATA,
251 &extradata_size);
252
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 409 times.
412 if (extradata) {
253 3 ret = h264_extradata_to_annexb(ctx, extradata, extradata_size);
254
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
255 goto fail;
256 }
257
258 /* nothing to filter */
259
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 412 times.
412 if (!s->extradata_parsed) {
260 av_packet_move_ref(opkt, in);
261 av_packet_free(&in);
262 return 0;
263 }
264
265 412 buf_end = in->data + in->size;
266
267 #define LOG_ONCE(...) \
268 if (j) \
269 av_log(__VA_ARGS__)
270
2/2
✓ Branch 0 taken 823 times.
✓ Branch 1 taken 411 times.
1234 for (int j = 0; j < 2; j++) {
271 823 int sps_count = 0;
272 823 int pps_count = 0;
273
274 823 buf = in->data;
275 823 new_idr = s->new_idr;
276 823 sps_seen = s->idr_sps_seen;
277 823 pps_seen = s->idr_pps_seen;
278 823 out_size = 0;
279
280 do {
281 3515 uint32_t nal_size = 0;
282 enum PsSource ps;
283
284 /* possible overread ok due to padding */
285
2/2
✓ Branch 0 taken 14060 times.
✓ Branch 1 taken 3515 times.
17575 for (int i = 0; i < s->length_size; i++)
286 14060 nal_size = (nal_size << 8) | buf[i];
287
288 3515 buf += s->length_size;
289
290 /* This check requires the cast as the right side might
291 * otherwise be promoted to an unsigned value. */
292
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3514 times.
3515 if ((int64_t)nal_size > buf_end - buf) {
293 1 ret = AVERROR_INVALIDDATA;
294 1 goto fail;
295 }
296
297
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3514 times.
3514 if (!nal_size)
298 continue;
299
300 3514 unit_type = *buf & 0x1f;
301
302
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3512 times.
3514 if (unit_type == H264_NAL_SPS) {
303 2 sps_seen = new_idr = 1;
304
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (!j) {
305 1 h264_mp4toannexb_save_ps(&s->sps, &s->sps_size, &s->sps_buf_size,
306 buf, nal_size, !sps_count);
307 1 sps_count++;
308 }
309
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3510 times.
3512 } else if (unit_type == H264_NAL_PPS) {
310 2 pps_seen = new_idr = 1;
311
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (!j) {
312 1 h264_mp4toannexb_save_ps(&s->pps, &s->pps_size, &s->pps_buf_size,
313 buf, nal_size, !pps_count);
314 1 pps_count++;
315 }
316 /* if SPS has not been seen yet, prepend the AVCC one to PPS */
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!sps_seen) {
318 if (!s->sps_size) {
319 LOG_ONCE(ctx, AV_LOG_WARNING, "SPS not present in the stream, nor in AVCC, stream may be unreadable\n");
320 } else {
321 count_or_copy(&out, &out_size, s->sps, s->sps_size, PS_OUT_OF_BAND, j);
322 sps_seen = 1;
323 }
324 }
325 }
326
327 /* If this is a new IDR picture following an IDR picture, reset the idr flag.
328 * Just check first_mb_in_slice to be 0 as this is the simplest solution.
329 * This could be checking idr_pic_id instead, but would complexify the parsing. */
330
5/6
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 3336 times.
✓ Branch 2 taken 104 times.
✓ Branch 3 taken 74 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 104 times.
3514 if (!new_idr && unit_type == H264_NAL_IDR_SLICE && (buf[1] & 0x80))
331 new_idr = 1;
332
333 /* prepend only to the first type 5 NAL unit of an IDR picture, if no sps/pps are already present */
334
7/8
✓ Branch 0 taken 3336 times.
✓ Branch 1 taken 178 times.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 3300 times.
✓ Branch 4 taken 34 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 34 times.
✗ Branch 7 not taken.
3514 if (new_idr && unit_type == H264_NAL_IDR_SLICE && !sps_seen && !pps_seen) {
335
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 if (s->sps_size)
336 34 count_or_copy(&out, &out_size, s->sps, s->sps_size, PS_OUT_OF_BAND, j);
337
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 if (s->pps_size)
338 34 count_or_copy(&out, &out_size, s->pps, s->pps_size, PS_OUT_OF_BAND, j);
339 34 new_idr = 0;
340 /* if only SPS has been seen, also insert PPS */
341
6/8
✓ Branch 0 taken 3302 times.
✓ Branch 1 taken 178 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 3300 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 2 times.
3480 } else if (new_idr && unit_type == H264_NAL_IDR_SLICE && sps_seen && !pps_seen) {
342 if (!s->pps_size) {
343 LOG_ONCE(ctx, AV_LOG_WARNING, "PPS not present in the stream, nor in AVCC, stream may be unreadable\n");
344 } else {
345 count_or_copy(&out, &out_size, s->pps, s->pps_size, PS_OUT_OF_BAND, j);
346 }
347 }
348
349
4/4
✓ Branch 0 taken 3512 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 3510 times.
3514 if (unit_type == H264_NAL_SPS || unit_type == H264_NAL_PPS)
350 4 ps = PS_IN_BAND;
351 else
352 3510 ps = PS_NONE;
353 3514 count_or_copy(&out, &out_size, buf, nal_size, ps, j);
354
2/2
✓ Branch 0 taken 1762 times.
✓ Branch 1 taken 1752 times.
3514 if (unit_type == H264_NAL_SLICE) {
355 1762 new_idr = 1;
356 1762 sps_seen = 0;
357 1762 pps_seen = 0;
358 }
359
360 3514 buf += nal_size;
361
2/2
✓ Branch 0 taken 2692 times.
✓ Branch 1 taken 822 times.
3514 } while (buf < buf_end);
362
363
2/2
✓ Branch 0 taken 411 times.
✓ Branch 1 taken 411 times.
822 if (!j) {
364
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 411 times.
411 if (out_size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
365 ret = AVERROR_INVALIDDATA;
366 goto fail;
367 }
368 411 ret = av_new_packet(opkt, out_size);
369
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 411 times.
411 if (ret < 0)
370 goto fail;
371 411 out = opkt->data;
372 }
373 }
374 #undef LOG_ONCE
375
376 av_assert1(out_size == opkt->size);
377
378 411 s->new_idr = new_idr;
379 411 s->idr_sps_seen = sps_seen;
380 411 s->idr_pps_seen = pps_seen;
381
382 411 ret = av_packet_copy_props(opkt, in);
383
1/2
✓ Branch 0 taken 411 times.
✗ Branch 1 not taken.
411 if (ret < 0)
384 goto fail;
385
386 411 fail:
387
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 411 times.
412 if (ret < 0)
388 1 av_packet_unref(opkt);
389 412 av_packet_free(&in);
390
391 412 return ret;
392 }
393
394 7 static void h264_mp4toannexb_close(AVBSFContext *ctx)
395 {
396 7 H264BSFContext *s = ctx->priv_data;
397
398 7 av_freep(&s->sps);
399 7 av_freep(&s->pps);
400 7 }
401
402 static void h264_mp4toannexb_flush(AVBSFContext *ctx)
403 {
404 H264BSFContext *s = ctx->priv_data;
405
406 s->idr_sps_seen = 0;
407 s->idr_pps_seen = 0;
408 s->new_idr = s->extradata_parsed;
409 }
410
411 static const enum AVCodecID codec_ids[] = {
412 AV_CODEC_ID_H264, AV_CODEC_ID_NONE,
413 };
414
415 const FFBitStreamFilter ff_h264_mp4toannexb_bsf = {
416 .p.name = "h264_mp4toannexb",
417 .p.codec_ids = codec_ids,
418 .priv_data_size = sizeof(H264BSFContext),
419 .init = h264_mp4toannexb_init,
420 .filter = h264_mp4toannexb_filter,
421 .close = h264_mp4toannexb_close,
422 .flush = h264_mp4toannexb_flush,
423 };
424