FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/bsf/h264_mp4toannexb.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 183 227 80.6%
Functions: 7 8 87.5%
Branches: 106 150 70.7%

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