blob: d806f9b52b354fdf55070f3d0acc44c79948df94
1 | /* |
2 | * This file is part of FFmpeg. |
3 | * |
4 | * FFmpeg is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU Lesser General Public |
6 | * License as published by the Free Software Foundation; either |
7 | * version 2.1 of the License, or (at your option) any later version. |
8 | * |
9 | * FFmpeg is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | * Lesser General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU Lesser General Public |
15 | * License along with FFmpeg; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
17 | */ |
18 | |
19 | #include <stdint.h> |
20 | |
21 | #include "vaapi_encode_h26x.h" |
22 | |
23 | int ff_vaapi_encode_h26x_nal_unit_to_byte_stream(uint8_t *dst, size_t *dst_bit_len, |
24 | uint8_t *src, size_t src_bit_len) |
25 | { |
26 | size_t dp, sp; |
27 | int zero_run = 0; |
28 | size_t dst_len = *dst_bit_len / 8; |
29 | size_t src_len = (src_bit_len + 7) / 8; |
30 | int trailing_zeroes = src_len * 8 - src_bit_len; |
31 | |
32 | if (dst_len < src_len + 4) { |
33 | // Definitely doesn't fit. |
34 | goto fail; |
35 | } |
36 | |
37 | // Start code. |
38 | dst[0] = dst[1] = dst[2] = 0; |
39 | dst[3] = 1; |
40 | dp = 4; |
41 | |
42 | for (sp = 0; sp < src_len; sp++) { |
43 | if (dp >= dst_len) |
44 | goto fail; |
45 | if (zero_run < 2) { |
46 | if (src[sp] == 0) |
47 | ++zero_run; |
48 | else |
49 | zero_run = 0; |
50 | } else { |
51 | if ((src[sp] & ~3) == 0) { |
52 | // emulation_prevention_three_byte |
53 | dst[dp++] = 3; |
54 | if (dp >= dst_len) |
55 | goto fail; |
56 | } |
57 | zero_run = src[sp] == 0; |
58 | } |
59 | dst[dp++] = src[sp]; |
60 | } |
61 | |
62 | *dst_bit_len = 8 * dp - trailing_zeroes; |
63 | return 0; |
64 | |
65 | fail: |
66 | *dst_bit_len = 0; |
67 | return AVERROR(ENOSPC); |
68 | } |
69 |