-
Notifications
You must be signed in to change notification settings - Fork 4
/
unit.c
131 lines (98 loc) · 2.79 KB
/
unit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Copyright (C) 2020 Bootlin
*/
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <errno.h>
#include <bitstream.h>
#include <unit.h>
uint8_t start_code_prefix[] = { 0x00, 0x00, 0x00, 0x01 };
unsigned int start_code_prefix_size = sizeof(start_code_prefix);
uint8_t forbidden_pattern_mask[] = { 0xff, 0xff, 0xfc };
uint8_t forbidden_pattern_match[] = { 0x00, 0x00, 0x00 };
unsigned int forbidden_pattern_size = sizeof(forbidden_pattern_mask);
struct unit *unit_pack(struct bitstream *bitstream)
{
struct unit *unit = NULL;
unsigned int unit_length;
unsigned int i, j;
unsigned int bitstream_length;
uint8_t *bitstream_values;
uint8_t *unit_values;
if (!bitstream)
return NULL;
bitstream_align(bitstream);
bitstream_length = bitstream->offset_bytes;
bitstream_values = bitstream->buffer;
unit_length = start_code_prefix_size + bitstream_length;
for (i = 0; i < (bitstream_length - forbidden_pattern_size + 1); i++) {
bool forbidden_pattern_found = true;
for (j = 0; j < forbidden_pattern_size; j++) {
if ((bitstream_values[j] & forbidden_pattern_mask[j]) !=
forbidden_pattern_match[j]) {
forbidden_pattern_found = false;
break;
}
}
if (forbidden_pattern_found) {
unit_length++;
/* Skip the forbidden pattern. */
i += forbidden_pattern_size - 1;
bitstream_values += forbidden_pattern_size - 1;
}
bitstream_values++;
}
bitstream_values = bitstream->buffer + bitstream_length - 1;
if (!*bitstream_values)
unit_length++;
unit = calloc(1, sizeof(*unit));
if (!unit)
goto error;
unit->buffer = calloc(1, unit_length);
unit->length = unit_length;
bitstream_values = bitstream->buffer;
unit_values = unit->buffer;
/* Add the start code prefix. */
for (i = 0; i < start_code_prefix_size; i++)
*unit_values++ = start_code_prefix[i];
/* Escape any forbidden pattern. */
for (i = 0; i < (bitstream_length - forbidden_pattern_size + 1); i++) {
bool forbidden_pattern_found = true;
for (j = 0; j < forbidden_pattern_size; j++) {
if ((bitstream_values[j] & forbidden_pattern_mask[j]) !=
forbidden_pattern_match[j]) {
forbidden_pattern_found = false;
break;
}
}
if (forbidden_pattern_found) {
for (j = 0; j < (forbidden_pattern_size - 1); j++) {
*unit_values++ = *bitstream_values++;
i++;
}
*unit_values++ = 0x03;
}
*unit_values++ = *bitstream_values++;
}
/* Copy leftover data */
for (; i < bitstream_length; i++)
*unit_values++ = *bitstream_values++;
bitstream_values = bitstream->buffer + bitstream_length - 1;
/* Escape a forbidden final zero byte. */
if (!*bitstream_values)
*unit_values++ = 0x03;
return unit;
error:
if (unit)
free(unit);
return NULL;
}
void unit_destroy(struct unit *unit)
{
if (!unit)
return;
if (unit->buffer)
free(unit->buffer);
free(unit);
}