forked from envytools/envytools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeh264.c
225 lines (223 loc) · 6.47 KB
/
deh264.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* Copyright (C) 2011 Marcin Kościelnicki <[email protected]>
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include "h264.h"
#include "vstream.h"
#include "util.h"
#include <stdio.h>
int main() {
uint8_t *bytes = 0;
int bytesnum = 0;
int bytesmax = 0;
int c;
while ((c = getchar()) != EOF) {
ADDARRAY(bytes, c);
}
struct bitstream *str = vs_new_decode(VS_H264, bytes, bytesnum);
struct h264_seqparm *seqparms[32] = { 0 };
struct h264_seqparm *subseqparms[32] = { 0 };
struct h264_picparm *picparms[256] = { 0 };
int res;
int last_idr = 0;
while (1) {
uint32_t start_code;
if (vs_start(str, &start_code)) goto err;
if (start_code & 0x80) {
fprintf(stderr, "forbidden_zero_bit not 0\n");
goto err;
}
uint32_t nal_ref_idc = start_code >> 5;
uint32_t nal_unit_type = start_code & 0x1f;
printf("NAL unit:\n");
printf("\tnal_ref_idc = %d\n", nal_ref_idc);
printf("\tnal_unit_type = %d\n", nal_unit_type);
struct h264_seqparm *sp;
struct h264_picparm *pp;
struct h264_slice *slice;
uint32_t idx;
uint32_t additional_extension_flag = 0;
switch (nal_unit_type) {
case H264_NAL_UNIT_TYPE_SLICE_NONIDR:
case H264_NAL_UNIT_TYPE_SLICE_IDR:
case H264_NAL_UNIT_TYPE_SLICE_AUX:
slice = calloc (sizeof *slice, 1);
slice->nal_ref_idc = nal_ref_idc;
slice->nal_unit_type = nal_unit_type;
if (nal_unit_type == H264_NAL_UNIT_TYPE_SLICE_IDR)
last_idr = 1;
if (nal_unit_type == H264_NAL_UNIT_TYPE_SLICE_NONIDR)
last_idr = 0;
/* for AUX, keep IDR status of last slice */
slice->idr_pic_flag = last_idr;
if (h264_slice_header(str, seqparms, picparms, slice)) {
h264_del_slice(slice);
goto err;
}
h264_print_slice_header(slice);
slice->mbs = calloc (sizeof *slice->mbs, slice->pic_size_in_mbs);
if (h264_slice_data(str, slice)) {
h264_print_slice_data(slice);
h264_del_slice(slice);
goto err;
}
h264_print_slice_data(slice);
h264_del_slice(slice);
break;
case H264_NAL_UNIT_TYPE_SEQPARM:
sp = calloc (sizeof *sp, 1);
if (h264_seqparm(str, sp)) {
h264_del_seqparm(sp);
goto err;
}
if (vs_end(str)) {
h264_del_seqparm(sp);
goto err;
}
h264_print_seqparm(sp);
if (sp->seq_parameter_set_id > 31) {
fprintf(stderr, "seq_parameter_set_id out of bounds\n");
goto err;
}
if (seqparms[sp->seq_parameter_set_id]) {
h264_del_seqparm(seqparms[sp->seq_parameter_set_id]);
}
seqparms[sp->seq_parameter_set_id] = sp;
break;
case H264_NAL_UNIT_TYPE_PICPARM:
pp = calloc (sizeof *pp, 1);
if (h264_picparm(str, seqparms, subseqparms, pp)) {
h264_del_picparm(pp);
goto err;
}
if (vs_end(str)) {
h264_del_picparm(pp);
goto err;
}
h264_print_picparm(pp);
if (pp->pic_parameter_set_id > 255) {
fprintf(stderr, "pic_parameter_set_id out of bounds\n");
goto err;
}
if (picparms[pp->pic_parameter_set_id]) {
h264_del_picparm(picparms[pp->pic_parameter_set_id]);
}
picparms[pp->pic_parameter_set_id] = pp;
break;
case H264_NAL_UNIT_TYPE_SEQPARM_EXT:
if (h264_seqparm_ext(str, seqparms, &idx))
goto err;
if (vs_end(str))
goto err;
h264_print_seqparm_ext(seqparms[idx]);
break;
case H264_NAL_UNIT_TYPE_ACC_UNIT_DELIM: {
uint32_t primary_pic_type;
if (vs_u(str, &primary_pic_type, 3)) goto err;
if (vs_end(str)) goto err;
printf ("Access unit delimiter:\n");
static const char *const names[8] = {
"I",
"P+I",
"P+B+I",
"SI",
"SP+SI",
"I+SI",
"P+I+SP+SI",
"P+B+I+SP+SI",
};
printf ("\tprimary_pic_type = %d [%s]\n", primary_pic_type, names[primary_pic_type]);
break;
}
case H264_NAL_UNIT_TYPE_END_SEQ:
printf ("End of sequence.\n");
break;
case H264_NAL_UNIT_TYPE_END_STREAM:
printf ("End of stream.\n");
break;
case H264_NAL_UNIT_TYPE_SUBSET_SEQPARM:
sp = calloc (sizeof *sp, 1);
if (h264_seqparm(str, sp)) {
h264_del_seqparm(sp);
goto err;
}
switch (sp->profile_idc) {
case H264_PROFILE_SCALABLE_BASELINE:
case H264_PROFILE_SCALABLE_HIGH:
if (h264_seqparm_svc(str, sp)) {
h264_del_seqparm(sp);
goto err;
}
break;
case H264_PROFILE_MULTIVIEW_HIGH:
case H264_PROFILE_STEREO_HIGH:
if (h264_seqparm_mvc(str, sp)) {
h264_del_seqparm(sp);
goto err;
}
break;
default:
break;
}
if (vs_u(str, &additional_extension_flag, 1)) {
h264_del_seqparm(sp);
goto err;
}
if (additional_extension_flag) {
fprintf(stderr, "WARNING: additional data in subset seqparm extension\n");
while (vs_has_more_data(str)) {
if (vs_u(str, &additional_extension_flag, 1)) {
h264_del_seqparm(sp);
goto err;
}
}
}
if (vs_end(str)) {
h264_del_seqparm(sp);
goto err;
}
h264_print_seqparm(sp);
if (sp->seq_parameter_set_id > 31) {
fprintf(stderr, "seq_parameter_set_id out of bounds\n");
goto err;
}
if (subseqparms[sp->seq_parameter_set_id]) {
h264_del_seqparm(subseqparms[sp->seq_parameter_set_id]);
}
subseqparms[sp->seq_parameter_set_id] = sp;
break;
default:
fprintf(stderr, "Unknown NAL type\n");
goto err;
}
printf("NAL decoded successfully\n\n");
continue;
err:
res = vs_search_start(str);
if (res == -1)
return 1;
if (!res)
break;
printf("\n");
}
return 0;
}