Cogs.Core
H264Parser.cpp
1#include "H264Data.h"
2#include "Parser.h"
3
4#include <algorithm>
5#include <assert.h>
6
7//#define PRINT_NALU
8
9using namespace Cogs::Core;
10
11enum {
12 Unknown_,
13 Intra_4x4,
14 Intra_8x8,
15 Intra_16x16,
16 Pred_L0,
17 Pred_L1,
18 BiPred,
19 Direct,
20};
21
22enum{
23 I_NxN,
24 I_16x16_0_0_0,
25 I_16x16_1_0_0,
26 I_16x16_2_0_0,
27 I_16x16_3_0_0,
28 I_16x16_0_1_0,
29 I_16x16_1_1_0,
30 I_16x16_2_1_0,
31 I_16x16_3_1_0,
32 I_16x16_0_2_0,
33 I_16x16_1_2_0,
34 I_16x16_2_2_0,
35 I_16x16_3_2_0,
36 I_16x16_0_0_1,
37 I_16x16_1_0_1,
38 I_16x16_2_0_1,
39 I_16x16_3_0_1,
40 I_16x16_0_1_1,
41 I_16x16_1_1_1,
42 I_16x16_2_1_1,
43 I_16x16_3_1_1,
44 I_16x16_0_2_1,
45 I_16x16_1_2_1,
46 I_16x16_2_2_1,
47 I_16x16_3_2_1,
48 I_PCM,
49};
50
51enum{
52 SI
53};
54
55enum{
56 B_Direct_16x16,
57 B_L0_16x16,
58 B_L1_16x16,
59 B_Bi_16x16,
60 B_l0_L0_16x8,
61 B_l0_L0_8x16,
62 B_L1_L1_16x8,
63 B_L1_L1_8x16,
64 B_L0_L1_16x8,
65 B_L0_L1_8x16,
66 B_L1_L0_16x8,
67 B_L1_L0_8x16,
68 B_L0_Bi_16x8,
69 B_L0_Bi_8x16,
70 B_L1_Bi_16x8,
71 B_L1_Bi_8x16,
72 B_Bi_L0_16x8,
73 B_Bi_L0_8x16,
74 B_Bi_L1_16x8,
75 B_Bi_L1_8x16,
76 B_Bi_Bi_16x8,
77 B_Bi_Bi_8x16,
78 B_8x8,
79 B_Skip,
80};
81
82enum{
83 P_L0_16x16,
84 P_L0_L0_16x8,
85 P_L0_L0_8x16,
86 P_8x8,
87 //P_8x8ref(),
88 //P_skip,
89};
90
91
92#if 0 // unreferenced
93static uint32_t parse_me(uint32_t ChromaArrayType, uint32_t pred_mode, const uint8_t *data, size_t &bit)
94{
95 uint32_t codeNum = parse_ue(data, bit);
96
97 if(ChromaArrayType == 1 || ChromaArrayType == 2){
98 if(pred_mode == Intra_4x4 || pred_mode == Intra_8x8){
99 uint8_t a[] = {47, 31, 15, 0, 23, 27, 29, 30, 7, 11, 13, 14, 39, 43, 45, 46, 16, 3, 5, 10, 12, 19, 21, 26, 28, 35, 37, 42, 44, 1, 2, 4, 8, 17, 18, 20, 24, 6, 9, 22, 25, 32, 33, 34, 36, 40, 38, 41};
100 return a[codeNum];
101 }
102 else{
103 assert(pred_mode != Intra_4x4 && pred_mode != Intra_8x8 && pred_mode != Intra_16x16);
104 uint8_t b[] = {0, 16, 1, 2, 4, 8, 32, 3, 5, 10, 12, 15, 47, 7, 11, 13, 14, 6, 9, 31, 35, 37, 42, 44, 33, 34, 36, 40, 39, 43, 45, 46, 17, 18, 20, 24, 19, 21, 26, 28, 23, 27, 29, 30, 22, 25, 38, 41};
105 return b[codeNum];
106 }
107 }
108 else if(ChromaArrayType == 0 || ChromaArrayType == 3){
109 if(pred_mode == Intra_4x4 || pred_mode == Intra_8x8){
110 uint8_t a[] = {15, 0, 7, 11, 13, 14, 3, 5, 10, 12, 1, 2, 4, 8, 6, 9};
111 return a[codeNum];
112 }
113 else{
114 assert(pred_mode != Intra_4x4 && pred_mode != Intra_8x8 && pred_mode != Intra_16x16);
115 uint8_t b[] = {0, 1, 2, 4, 8, 3, 5, 10, 12, 15, 7, 11, 13, 14, 6, 9};
116 return b[codeNum];
117 }
118 }
119
120 return 0;
121}
122#endif
123// static uint32_t parse_te(uint8_t *data, size_t &bit)
124// {
125// uint32_t codeNum = parse_ue(data, bit);
126// assert(false);
127// return 0;
128// }
129// static uint32_t parse_ae(uint8_t *data, size_t &bit)
130// {
131// assert(false);
132// return 0;
133// }
134// static uint32_t parse_ce(uint8_t *data, size_t &bit)
135// {
136// assert(false);
137// return 0;
138// }
139
140#if 0 // unreferenced
141static uint32_t NextMbAddress(uint32_t CurrMbAddr, const SeqParameterSet &seq_set, const PicParameterSet &pic_set, const SliceLayer &layer)
142{
143 uint32_t PicWidthInMbs = seq_set.pic_width_in_mbs_minus1 + 1;
144 uint32_t PicHeightInMapUnits = seq_set.pic_height_in_map_units_minus1 + 1;
145 uint32_t FrameHeightInMbs = ( 2 - seq_set.frame_mbs_only_flag ) * PicHeightInMapUnits;
146 uint32_t PicHeightInMbs = FrameHeightInMbs / ( 1 + layer.field_pic_flag );
147 uint32_t PicSizeInMbs = PicWidthInMbs * PicHeightInMbs;
148 uint32_t PicSizeInMapUnits = PicWidthInMbs * PicHeightInMapUnits;
149
150 uint32_t MbaffFrameFlag = (seq_set.mb_adaptive_frame_field_flag && !layer.field_pic_flag);
151
152 uint32_t *mapUnitToSliceGroupMap = new uint32_t[PicSizeInMbs]; // (uint32_t*)malloc(sizeof(uint32_t)*PicSizeInMbs);
153 if(pic_set.slice_group_map_type == 0){
154 uint32_t i = 0;
155 do{
156 for(uint32_t iGroup=0; iGroup<=pic_set.num_slice_groups_minus1 && i<PicSizeInMapUnits; i += pic_set.run_length_minus1[iGroup++]+1){
157 for(uint32_t j=0; j<=pic_set.run_length_minus1[iGroup] && i+j<PicSizeInMapUnits; j++){
158 mapUnitToSliceGroupMap[i+j] = iGroup;
159 }
160 }
161 } while( i < PicSizeInMapUnits );
162 }
163 else{
164 assert(false);
165 }
166
167 uint32_t *MbToSliceGroupMap = new uint32_t[PicSizeInMbs]; // (uint32_t*)malloc(sizeof(uint32_t)*PicSizeInMbs);
168
169 for(uint32_t i=0; i<PicSizeInMbs-1; i++){
170 if(seq_set.frame_mbs_only_flag == 1 || layer.field_pic_flag == 1)
171 MbToSliceGroupMap[i] = mapUnitToSliceGroupMap[i];
172 else if(MbaffFrameFlag == 1)
173 MbToSliceGroupMap[i] = mapUnitToSliceGroupMap[i/2];
174 else
175 MbToSliceGroupMap[i] = mapUnitToSliceGroupMap[(i/(2*PicWidthInMbs)) * PicWidthInMbs + (i%PicWidthInMbs)];
176 }
177
178 uint32_t n = CurrMbAddr;
179 uint32_t i = n + 1;
180 while(i < PicSizeInMbs && MbToSliceGroupMap[i] != MbToSliceGroupMap[n]) i++;
181 delete [] mapUnitToSliceGroupMap; // free(mapUnitToSliceGroupMap);
182 delete [] MbToSliceGroupMap; // free(MbToSliceGroupMap);
183 return i;
184}
185
186static uint32_t CodedBlockPatternLumaTab(uint32_t coded_block_pattern, uint32_t slice_type, uint32_t mb_type)
187{
188 if(slice_type == 2 || slice_type == 7){ // I Slice
189 if(mb_type == I_NxN) return coded_block_pattern % 16;
190 if(mb_type >= I_16x16_0_0_0 && mb_type <= I_16x16_3_2_0) return 0;
191 if(mb_type >= I_16x16_0_0_1 && mb_type <= I_16x16_3_2_1) return 15;
192 }
193 else if(slice_type == 4 || slice_type == 9){ // SI Slice
194 if(mb_type == SI) return coded_block_pattern % 16;
195 mb_type = mb_type-1;
196 if(mb_type == I_NxN) return coded_block_pattern % 16;
197 if(mb_type >= I_16x16_0_0_0 && mb_type <= I_16x16_3_2_0) return 0;
198 if(mb_type >= I_16x16_0_0_1 && mb_type <= I_16x16_3_2_1) return 15;
199 }
200 else if(slice_type == 0 || slice_type == 5){ // P / SP Slice
201 assert(mb_type >= 5);
202 mb_type = mb_type-5;
203 if(mb_type == I_NxN) return coded_block_pattern % 16;
204 if(mb_type >= I_16x16_0_0_0 && mb_type <= I_16x16_3_2_0) return 0;
205 if(mb_type >= I_16x16_0_0_1 && mb_type <= I_16x16_3_2_1) return 15;
206 }
207 else if(slice_type == 1 || slice_type == 6){ // B Slice
208 assert(mb_type >= 23);
209 mb_type = mb_type-23;
210 if(mb_type == I_NxN) return coded_block_pattern % 16;
211 if(mb_type >= I_16x16_0_0_0 && mb_type <= I_16x16_3_2_0) return 0;
212 if(mb_type >= I_16x16_0_0_1 && mb_type <= I_16x16_3_2_1) return 15;
213 }
214 assert(false);
215 return (uint32_t)-1;
216}
217static uint32_t CodedBlockPatternChromaTab(uint32_t coded_block_pattern, uint32_t slice_type, uint32_t mb_type)
218{
219 if(slice_type == 2 || slice_type == 7){ // I Slice
220 if(mb_type == I_NxN) return coded_block_pattern / 16;
221 if(mb_type >= I_16x16_0_0_0 && mb_type <= I_16x16_3_0_0) return 0;
222 if(mb_type >= I_16x16_0_1_0 && mb_type <= I_16x16_3_1_0) return 1;
223 if(mb_type >= I_16x16_0_2_0 && mb_type <= I_16x16_3_2_0) return 2;
224 if(mb_type >= I_16x16_0_0_1 && mb_type <= I_16x16_3_0_1) return 0;
225 if(mb_type >= I_16x16_0_1_1 && mb_type <= I_16x16_3_1_1) return 1;
226 if(mb_type >= I_16x16_0_2_1 && mb_type <= I_16x16_3_2_1) return 2;
227 }
228 else if(slice_type == 4 || slice_type == 9){ // SI Slice
229 if(mb_type == SI) return coded_block_pattern / 16;
230 mb_type = mb_type-1;
231 if(mb_type == I_NxN) return coded_block_pattern / 16;
232 if(mb_type >= I_16x16_0_0_0 && mb_type <= I_16x16_3_0_0) return 0;
233 if(mb_type >= I_16x16_0_1_0 && mb_type <= I_16x16_3_1_0) return 1;
234 if(mb_type >= I_16x16_0_2_0 && mb_type <= I_16x16_3_2_0) return 2;
235 if(mb_type >= I_16x16_0_0_1 && mb_type <= I_16x16_3_0_1) return 0;
236 if(mb_type >= I_16x16_0_1_1 && mb_type <= I_16x16_3_1_1) return 1;
237 if(mb_type >= I_16x16_0_2_1 && mb_type <= I_16x16_3_2_1) return 2;
238 }
239 else if(slice_type == 0 || slice_type == 5){ // P / SP Slice
240 assert(mb_type >= 5);
241 mb_type = mb_type-5;
242 if(mb_type == I_NxN) return coded_block_pattern / 16;
243 if(mb_type >= I_16x16_0_0_0 && mb_type <= I_16x16_3_0_0) return 0;
244 if(mb_type >= I_16x16_0_1_0 && mb_type <= I_16x16_3_1_0) return 1;
245 if(mb_type >= I_16x16_0_2_0 && mb_type <= I_16x16_3_2_0) return 2;
246 if(mb_type >= I_16x16_0_0_1 && mb_type <= I_16x16_3_0_1) return 0;
247 if(mb_type >= I_16x16_0_1_1 && mb_type <= I_16x16_3_1_1) return 1;
248 if(mb_type >= I_16x16_0_2_1 && mb_type <= I_16x16_3_2_1) return 2;
249 }
250 else if(slice_type == 1 || slice_type == 6){ // B Slice
251 assert(mb_type >= 23);
252 mb_type = mb_type-23;
253 if(mb_type == I_NxN) return coded_block_pattern / 16;
254 if(mb_type >= I_16x16_0_0_0 && mb_type <= I_16x16_3_0_0) return 0;
255 if(mb_type >= I_16x16_0_1_0 && mb_type <= I_16x16_3_1_0) return 1;
256 if(mb_type >= I_16x16_0_2_0 && mb_type <= I_16x16_3_2_0) return 2;
257 if(mb_type >= I_16x16_0_0_1 && mb_type <= I_16x16_3_0_1) return 0;
258 if(mb_type >= I_16x16_0_1_1 && mb_type <= I_16x16_3_1_1) return 1;
259 if(mb_type >= I_16x16_0_2_1 && mb_type <= I_16x16_3_2_1) return 2;
260 }
261 assert(false);
262 return (uint32_t)-1;
263}
264
265static uint32_t MbPartPredMode(uint32_t slice_type, uint32_t transform_size_8x8_flag, uint32_t mb_type, uint32_t n)
266{
267 if(slice_type == 2 || slice_type == 7){ // I Slice
268 if(mb_type == I_NxN && transform_size_8x8_flag == 0) return Intra_4x4;
269 if(mb_type == I_NxN && transform_size_8x8_flag == 1) return Intra_8x8;
270 if(mb_type >= I_16x16_0_0_0 && mb_type <= I_16x16_3_2_1) return Intra_16x16;
271 }
272 else if(slice_type == 4 || slice_type == 9){ // SI Slice
273 if(mb_type == SI) return Intra_4x4;
274 mb_type = mb_type-1;
275 if(mb_type == I_NxN && transform_size_8x8_flag == 0) return Intra_4x4;
276 if(mb_type == I_NxN && transform_size_8x8_flag == 1) return Intra_8x8;
277 if(mb_type >= I_16x16_0_0_0 && mb_type <= I_16x16_3_2_1) return Intra_16x16;
278 }
279 else if(slice_type == 0 || slice_type == 5){ // P / SP Slice
280 if(n == 0)
281 if(mb_type == P_L0_16x16) return Pred_L0;
282 if(mb_type == P_L0_L0_16x8) return Pred_L0;
283 if(mb_type == P_L0_L0_8x16) return Pred_L0;
284 mb_type = mb_type-5;
285 if(mb_type == I_NxN && transform_size_8x8_flag == 0) return Intra_4x4;
286 if(mb_type == I_NxN && transform_size_8x8_flag == 1) return Intra_8x8;
287 if(mb_type >= I_16x16_0_0_0 && mb_type <= I_16x16_3_2_1) return Intra_16x16;
288 }
289 else if(slice_type == 1 || slice_type == 6){ // B Slice
290 if(n == 0){
291 if(mb_type == B_Direct_16x16) return Direct;
292 if(mb_type == B_L0_16x16) return Pred_L0;
293 if(mb_type == B_L1_16x16) return Pred_L1;
294 if(mb_type == B_Bi_16x16) return BiPred;
295 if(mb_type == B_l0_L0_16x8) return Pred_L0;
296 if(mb_type == B_l0_L0_8x16) return Pred_L0;
297 if(mb_type == B_L1_L1_16x8) return Pred_L1;
298 if(mb_type == B_L1_L1_8x16) return Pred_L1;
299 if(mb_type == B_L0_L1_16x8) return Pred_L0;
300 if(mb_type == B_L0_L1_8x16) return Pred_L0;
301 if(mb_type == B_L1_L0_16x8) return Pred_L1;
302 if(mb_type == B_L1_L0_8x16) return Pred_L1;
303 if(mb_type == B_L0_Bi_16x8) return Pred_L0;
304 if(mb_type == B_L0_Bi_8x16) return Pred_L0;
305 if(mb_type == B_L1_Bi_16x8) return Pred_L1;
306 if(mb_type == B_L1_Bi_8x16) return Pred_L1;
307 if(mb_type == B_Bi_L0_16x8) return BiPred;
308 if(mb_type == B_Bi_L0_8x16) return BiPred;
309 if(mb_type == B_Bi_L1_16x8) return BiPred;
310 if(mb_type == B_Bi_L1_8x16) return BiPred;
311 if(mb_type == B_Bi_Bi_16x8) return BiPred;
312 if(mb_type == B_Bi_Bi_8x16) return BiPred;
313 }
314 else{
315 assert(n == 1);
316 if(mb_type == B_l0_L0_16x8) return Pred_L0;
317 if(mb_type == B_l0_L0_8x16) return Pred_L0;
318 if(mb_type == B_L1_L1_16x8) return Pred_L1;
319 if(mb_type == B_L1_L1_8x16) return Pred_L1;
320 if(mb_type == B_L0_L1_16x8) return Pred_L1;
321 if(mb_type == B_L0_L1_8x16) return Pred_L1;
322 if(mb_type == B_L1_L0_16x8) return Pred_L0;
323 if(mb_type == B_L1_L0_8x16) return Pred_L0;
324 if(mb_type == B_L0_Bi_16x8) return BiPred;
325 if(mb_type == B_L0_Bi_8x16) return BiPred;
326 if(mb_type == B_L1_Bi_16x8) return BiPred;
327 if(mb_type == B_L1_Bi_8x16) return BiPred;
328 if(mb_type == B_Bi_L0_16x8) return Pred_L0;
329 if(mb_type == B_Bi_L0_8x16) return Pred_L0;
330 if(mb_type == B_Bi_L1_16x8) return Pred_L1;
331 if(mb_type == B_Bi_L1_8x16) return Pred_L1;
332 if(mb_type == B_Bi_Bi_16x8) return BiPred;
333 if(mb_type == B_Bi_Bi_8x16) return BiPred;
334 }
335 mb_type = mb_type-23;
336 if(mb_type == I_NxN && transform_size_8x8_flag == 0) return Intra_4x4;
337 if(mb_type == I_NxN && transform_size_8x8_flag == 1) return Intra_8x8;
338 if(mb_type >= I_16x16_0_0_0 && mb_type <= I_16x16_3_2_1) return Intra_16x16;
339 }
340 assert(false);
341 return Unknown_;
342}
343
344static uint32_t NumMbPart(uint32_t /*mb_type*/)
345{
346 assert(false);
347 return 0;
348}
349#endif
350
351static void rbsp_trailing_bits(const uint8_t *data, size_t &bit)
352{
353 uint32_t rbsp_stop_one_bit = parse_u(1, data, bit);
354 assert(rbsp_stop_one_bit == 0x1);
355 while(bit%8 != 0){
356 uint32_t rbsp_alignment_zero_bit = parse_u(1, data, bit);
357 assert(rbsp_alignment_zero_bit == 0x0);
358 }
359}
360#if 0 // unreferenced
361static void rbsp_slice_trailing_bits(const uint8_t *data, size_t &bit)
362{
363 rbsp_trailing_bits(data, bit);
364 // if(entropy_coding_mode_flag){
365 // while(more_rbsp_trailing_data()){
366 // uint32_t cabac_zero_word = parse_f(16, data, bit);
367 // assert(cabac_zero_word == 0x0000);
368 // }
369 // }
370}
371#endif
372
373#if 0 // unreferenced
374static void residual_block(uint32_t *coeffLevel, uint32_t startIdx, uint32_t endIdx, uint32_t maxNumCoeff, const uint8_t *data, size_t &bit) //residual_block_cavlc
375{
376 // assert(entropy_coding_mode_flag == 0);
377 for(uint32_t i=0; i<maxNumCoeff; i++)
378 coeffLevel[i] = 0;
379 //uint32_t coeff_token = parse_ce(data, bit);
380 // int32_t nC;
381 // if(false){ //ChromaDCLevel){
382 // // if(ChromaArrayType == 1)
383 // // nC = -1;
384 // // else{
385 // // assert(ChromaArrayType == 2);
386 // // nC = -1;
387 // // }
388 // }
389 // else{
390 // if(Intra16x16DCLevel){
391 // luma4x4BlkIdx = 0;
392 // }
393 // else if(CbIntra16x16DCLevel){
394 // cb4x4BlkIdx = 0;
395 // }
396 // else if (CrIntra16x16DCLevel){
397 // cr4x4BlkIdx = 0;
398 // }
399 // uint32_t blkA, blkB;
400 // if(Intra16x16DCLevel, Intra16x16ACLevel, or LumaLevel4x4){
401 // assert(false);
402 // }
403 // else if(CbIntra16x16DCLevel, CbIntra16x16ACLevel, or CbLevel4x4){
404 // assert(false);
405 // }
406 // else if(CrIntra16x16DCLevel, CrIntra16x16ACLevel, or CrLevel4x4){
407 // assert(false);
408 // }
409 // else if(ChromaACLevel){
410 // assert(false);
411 // }
412 // else{ assert(false); }
413 // uint32_t availableFlagA =;
414 // if(! || lkasjalsdjkasdlksd)
415 // availableFlagA = 0;
416 // else
417 // availableFlagA = 1;
418 // uint32_t availableFlagB =;
419 // if(unavailable(mbAddrB) || lkasjalsdjkasdlksd)
420 // availableFlagB = 0;
421 // else
422 // availableFlagB = 1;
423 // uint32_t nA = ;
424 // uint32_t nB = ;
425 // uint32_t nC;
426 // }
427
428 // uint32_t coeff_token = ;
429 uint32_t TotalCoeff = 0; // TODO
430 assert(TotalCoeff <= maxNumCoeff);
431 uint32_t TrailingOnes = 0; // TODO
432
433 if(TotalCoeff > 0){
434 int32_t levelVal[16];
435 uint32_t suffixLength;
436 if(TotalCoeff > 10 && TrailingOnes<3)
437 suffixLength = 1;
438 else
439 suffixLength = 0;
440 for(uint32_t i=0; i<TotalCoeff; i++){
441 if(i < TrailingOnes){
442 uint32_t trailing_ones_sign_flag = parse_u(1, data, bit);
443 levelVal[i] = 1-2*trailing_ones_sign_flag;
444 }
445 else{
446 int32_t level_prefix = 0; // parse_ce(data, bit);
447 // assert(level_prefix <= 15); // Baseline
448 assert(false);
449 int32_t levelCode = (std::min(15, level_prefix)<<suffixLength);
450 if(suffixLength > 0 || level_prefix >= 14){
451 uint32_t levelSuffixSize;
452 if(level_prefix == 14 && suffixLength == 0)
453 levelSuffixSize = 4;
454 else if(level_prefix >= 15)
455 levelSuffixSize = level_prefix - 3;
456 else
457 levelSuffixSize = suffixLength;
458 uint32_t level_suffix;
459 if(levelSuffixSize)
460 level_suffix= parse_u(levelSuffixSize, data, bit);
461 else
462 level_suffix = 0;
463 levelCode += level_suffix;
464 }
465 if(level_prefix >= 15 && suffixLength == 0)
466 levelCode += 15;
467 if(level_prefix >= 16)
468 levelCode += (1<<(level_prefix-3))-4096;
469 if(i == TrailingOnes && TrailingOnes < 3)
470 levelCode += 2;
471 if(levelCode%2 == 0)
472 levelVal[i] = (levelCode +2)>>1;
473 else
474 levelVal[i] = (-levelCode-1)>>1;
475 if(suffixLength == 0)
476 suffixLength = 1;
477 if(std::abs(levelVal[i]) > (3<<(suffixLength-1)) && suffixLength < 6)
478 suffixLength++;
479 }
480 }
481 uint32_t zerosLeft;
482 if(TotalCoeff<endIdx-startIdx+1){
483 uint32_t total_zeros = 0; // parse_ce(data, bit);
484 assert(false);
485 zerosLeft = total_zeros;
486 }
487 else
488 zerosLeft = 0;
489 uint32_t runVal[16];
490 for(uint32_t i=0; i<TotalCoeff-1; i++){
491 if(zerosLeft>0){
492 uint32_t run_before = 0; // parse_ce(data, bit);
493 assert(false);
494 runVal[i] = run_before;
495 }
496 else{
497 runVal[i] = 0;
498 }
499 zerosLeft = zerosLeft - runVal[i];
500 }
501 runVal[TotalCoeff-1] = zerosLeft;
502 int32_t coeffNum = -1;
503 for(int32_t i=TotalCoeff-1; i>=0; i--){
504 coeffNum += runVal[i] + 1;
505 coeffLevel[startIdx + coeffNum] = levelVal[i];
506 }
507 }
508}
509#endif
510
511#if 0
512static void slice_layer_without_partitioning_rbsp(H264ParseData &parse, uint8_t *data, size_t, bool IdrPicFlag)
513{
514 const NALheader &nal_header(parse.nal_header);
515 const SeqParameterSet &seq_set(parse.seq_set);
516 const PicParameterSet &pic_set(parse.pic_set);
517 SliceLayer &layer(parse.layer);
518 layer = {};
519
520 size_t bit = 0;
521
522 layer.IdrPicFlag = IdrPicFlag;
523
524 // slice_header:
525 layer.first_mb_in_slice = parse_ue(data, bit);
526 layer.slice_type = parse_ue(data, bit);
527 layer.pic_parameter_set_id = parse_ue(data, bit);
528 assert(layer.pic_parameter_set_id == 0);
529
530 if(seq_set.separate_colour_plane_flag == 1)
531 layer.colour_plane_id = parse_u(2, data, bit);
532 layer.frame_num = parse_u(seq_set.log2_max_frame_num_minus4+4, data, bit);
533 if(!seq_set.frame_mbs_only_flag){
534 layer.field_pic_flag = parse_u(1, data, bit);
535 if(layer.field_pic_flag)
536 layer.bottom_field_flag = parse_u(1, data, bit);
537 }
538 if(layer.IdrPicFlag)
539 layer.idr_pic_id = parse_ue(data, bit);
540 if(seq_set.pic_order_cnt_type == 0){
541 layer.pic_order_cnt_lsb = parse_u(seq_set.log2_max_pic_order_cnt_lsb_minus4+4, data, bit);
542 if(pic_set.bottom_field_pic_order_in_frame_present_flag && !layer.field_pic_flag)
543 layer.delta_pic_order_cnt_bottom = parse_se(data, bit);
544 }
545 if(seq_set.pic_order_cnt_type == 1 && !seq_set.delta_pic_order_always_zero_flag){
546 layer.delta_pic_order_cnt[0] = parse_se(data, bit);
547 if(pic_set.bottom_field_pic_order_in_frame_present_flag && !layer.field_pic_flag)
548 layer.delta_pic_order_cnt[1] = parse_se(data, bit);
549 }
550 if(pic_set.redundant_pic_cnt_present_flag)
551 layer.redundant_pic_cnt = parse_ue(data, bit);
552 if(layer.slice_type == 1 || layer.slice_type == 6) // B Slice
553 layer.direct_spatial_mv_pred_flag = parse_u(1, data, bit);
554 if(layer.slice_type == 0 || layer.slice_type == 3 || layer.slice_type == 1 ||
555 layer.slice_type == 5 || layer.slice_type == 8 || layer.slice_type == 6){ // P / SP / B Slice
556 layer.num_ref_idx_active_override_flag = parse_u(1, data, bit);
557 if(layer.num_ref_idx_active_override_flag){
558 layer.num_ref_idx_l0_active_minus1 = parse_ue(data, bit);
559 if(layer.slice_type == 1 || layer.slice_type == 6) // B Slice
560 layer.num_ref_idx_l1_active_minus1 = parse_ue(data, bit);
561 }
562 }
563 if(nal_header.nal_unit_type == 20 || nal_header.nal_unit_type == 21)
564 assert(false); // ref_pic_list_mvc_modification()
565 else{
566 // ref_pic_list_modification()
567 if(layer.slice_type%5 != 2 && layer.slice_type%5 != 4){
568 uint32_t ref_pic_list_modification_flagl0 = parse_u(1, data, bit);
569 if(ref_pic_list_modification_flagl0){
570 assert(false);
571 }
572 }
573 if(layer.slice_type%5 == 1){
574 uint32_t ref_pic_list_modification_flag_l1 = parse_u(1, data, bit);
575 if(ref_pic_list_modification_flag_l1){
576 assert(false);
577 }
578 }
579 }
580 if((pic_set.weighted_pred_flag && (layer.slice_type == 0 || layer.slice_type == 3 || layer.slice_type == 5 || layer.slice_type == 8)) || // P / SP SLice
581 (pic_set.weighted_bipred_idc == 1 && (layer.slice_type == 1 || layer.slice_type == 6))){ // B Slice
582 // pred_weight_table():
583 layer.luma_log2_weight_denom = parse_ue(data, bit);
584
585 uint32_t ChromaArrayType;
586 if(seq_set.separate_colour_plane_flag == 0)
587 ChromaArrayType = seq_set.chroma_format_idc;
588 else
589 ChromaArrayType = 0;
590
591 if(ChromaArrayType != 0)
592 layer.chroma_log2_weight_denom = parse_ue(data, bit);
593 for(uint32_t i=0; i<layer.num_ref_idx_l0_active_minus1; i++){
594 assert(false);
595 }
596 if(layer.slice_type%5 == 1){
597 assert(false);
598 }
599 }
600 if(nal_header.nal_ref_idc != 0){
601 // dec_ref_pic_marking()
602 if(layer.IdrPicFlag){
603 //uint32_t no_output_of_prior_pics_flag =
604 parse_u(1, data, bit);
605 //uint32_t long_term_reference_flag =
606 parse_u(1, data, bit);
607 }
608 else{
609 uint32_t adaptive_ref_pic_marking_mode_flag = parse_u(1, data, bit);
610 if(adaptive_ref_pic_marking_mode_flag){
611 assert(false);
612 }
613 }
614 }
615 if(pic_set.entropy_coding_mode_flag && !(layer.slice_type == 2 || layer.slice_type == 7) && !(layer.slice_type == 4 || layer.slice_type == 9)) // !I / !SI Slice
616 layer.cabac_init_idc = parse_ue(data, bit);
617 layer.slice_qp_delta = parse_se(data, bit);
618 if(layer.slice_type == 3 || layer.slice_type == 4 ||
619 layer.slice_type == 8 || layer.slice_type == 9){ // SP SI Slice
620 if(layer.slice_type == 3 || layer.slice_type == 8) // SP Slice
621 layer.sp_for_switch_flag = parse_u(1, data, bit);
622 layer.slice_qs_delta = parse_se(data, bit);
623 }
624 if(pic_set.deblocking_filter_control_present_flag){
625 layer.disable_deblocking_filter_idc = parse_ue(data, bit);
626 if(layer.disable_deblocking_filter_idc != 1){
627 layer.slice_alpha_c0_offset_div2 = parse_se(data, bit);
628 layer.slice_beta_offset_div2 = parse_se(data, bit);
629 }
630 }
631 if(pic_set.num_slice_groups_minus1 > 0 &&
632 pic_set.slice_group_map_type >= 3 && pic_set.slice_group_map_type <= 5){
633 assert(false);
634 // uint32_t PicWidthInMbs = seq_set.pic_width_in_mbs_minus1 + 1;
635 // uint32_t PicHeightInMapUnits = seq_set.pic_height_in_map_units_minus1 + 1;
636 // uint32_t PicSizeInMapUnits = PicWidthInMbs * PicHeightInMapUnits;
637 // layer.slice_group_change_cycle = parse_u(ceil(log2(PicSizeInMapUnits/SliceGroupChangeRate+1)), data, bit);
638 }
639
640 // slice_data:
641 // uint32_t MbaffFrameFlag = (seq_set.mb_adaptive_frame_field_flag && !layer.field_pic_flag);
642 // uint32_t ChromaArrayType;
643 // if(seq_set.separate_colour_plane_flag == 0)
644 // ChromaArrayType = seq_set.chroma_format_idc;
645 // else
646 // ChromaArrayType = 0;
647
648 // if(pic_set.entropy_coding_mode_flag){
649 // while(bit%8 != 0){
650 // uint32_t cabac_alignment_one_bit = parse_f(1, data, bit);
651 // assert(cabac_alignment_one_bit == 0x1);
652 // }
653 // }
654 // uint32_t CurrMbAddr = layer.first_mb_in_slice*(1+MbaffFrameFlag);
655 // uint32_t moreDataFlag = 1;
656 // uint32_t prevMbSkipped = 0;
657 // do{
658 // if(!(layer.slice_type == 2 || layer.slice_type == 7) && !(layer.slice_type == 4 || layer.slice_type == 9)){ // !I / !SI Slice
659 // if(!pic_set.entropy_coding_mode_flag){
660 // uint32_t mb_skip_run = parse_ue(data, bit);
661 // prevMbSkipped = (mb_skip_run>0);
662 // for(uint32_t i=0; i<mb_skip_run; i++)
663 // CurrMbAddr = NextMbAddress(CurrMbAddr, seq_set, pic_set, layer);
664 // if(mb_skip_run>0)
665 // moreDataFlag = more_rbsp_data(data, size, bit);
666 // }
667 // else{
668 // assert(false);
669 // //uint32_t mb_skip_flag = parse_ae(data, bit);
670 // //moreDataFlag = !mb_skip_flag;
671 // }
672 // }
673 // if(moreDataFlag){
674 // if(MbaffFrameFlag && (CurrMbAddr%2 == 0 || (CurrMbAddr%2 == 1 && prevMbSkipped))){
675 // if(pic_set.entropy_coding_mode_flag == 0)
676 // uint32_t mb_field_decoding_flag = parse_u(1, data, bit);
677 // else
678 // assert(false); //uint32_t mb_field_decoding_flag = parse_ae(data, bit);
679 // }
680 // //macroblock_layer:
681 // {
682 // uint32_t mb_type = 0; // Macroblock type
683 // uint32_t transform_size_8x8_flag = 0;
684 // if(pic_set.entropy_coding_mode_flag == 0)
685 // mb_type = parse_ue(data, bit);
686 // else
687 // assert(false); // mb_type = pares_ae(data, bit);
688 // assert(layer.slice_type == 2 || layer.slice_type == 7);
689 // if(mb_type == I_PCM){
690 // while(bit%8 == 0){ // (!byte_aligned())
691 // uint32_t pcm_alignment_zero_bit = parse_f(1, data, bit);
692 // assert(pcm_alignment_zero_bit == 0x0);
693 // }
694 // assert(false);
695 // // for(uint32_t i=0; i<256; i++)
696 // // uint32_t pcm_sample_luma[i] = parse_u(?, data, bit);
697 // // assert(pcm_sample_luma[i] != 0); // Baseline
698 // // for(uint32_t i=0; i<2*MbWidthC*MbHeightC; i++)
699 // // uint32_t pcm_sample_chroma[i] = parse_u(?, data, bit);
700 // // assert(pcm_sample_chroma[i] != 0); // Baseline
701 // }
702 // else{
703 // uint32_t noSubMbPartSizeLessThan8x8Flag = 1;
704 // assert(layer.slice_type == 2 || layer.slice_type == 7);
705 // if(mb_type != I_NxN &&
706 // MbPartPredMode(layer.slice_type, 0, mb_type, 0) != Intra_16x16 &&
707 // NumMbPart(mb_type) == 4){
708 // assert(false);
709 // // sub_mb_pred(mb_type);
710 // // for(uint32_t mbPartIdx=0; mbPartIdx<4; mbPartIdx++){
711 // // if(sub_mb_type[mbPartIdx] != B_Direct_8x8){
712 // // if(NumSubMbPart(sub_mb_type[mbPartIdx]) > 1)
713 // // noSubMbPartSizeLessThan8x8Flag = 0;
714 // // }
715 // // else if(!seq_set.direct_8x8_inference_flag)
716 // // noSubMbPartSizeLessThan8x8Flag = 0;
717 // // }
718 // }
719 // else{
720 // if(pic_set.transform_8x8_mode_flag && mb_type == I_NxN){
721 // if(pic_set.entropy_coding_mode_flag == 0)
722 // transform_size_8x8_flag = parse_u(1, data, bit);
723 // else
724 // assert(false); // uint32_t transform_size_8x8_flag = parse_ae(1, data, bit);
725 // }
726 // // mb_pred(mb_type):
727 // {
728 // if(MbPartPredMode(layer.slice_type, transform_size_8x8_flag, mb_type, 0) == Intra_4x4 ||
729 // MbPartPredMode(layer.slice_type, transform_size_8x8_flag, mb_type, 0) == Intra_8x8 ||
730 // MbPartPredMode(layer.slice_type, transform_size_8x8_flag, mb_type, 0) == Intra_16x16){
731 // if(MbPartPredMode(layer.slice_type, transform_size_8x8_flag, mb_type, 0) == Intra_4x4){
732 // uint32_t prev_intra4x4_pred_mode_flag[16];
733 // uint32_t rem_intra8x8_pred_mode[16];
734 // for(uint32_t luma4x4BlkIdx=0; luma4x4BlkIdx<16; luma4x4BlkIdx++){
735 // if(pic_set.entropy_coding_mode_flag == 0)
736 // prev_intra4x4_pred_mode_flag[luma4x4BlkIdx] = parse_u(1, data, bit);
737 // else
738 // assert(false); // prev_intra4x4_pred_mode_flag[luma4x4BlkIdx] = parse_ae(1, data, bit);
739 // if(!prev_intra4x4_pred_mode_flag[luma4x4BlkIdx]){
740 // if(pic_set.entropy_coding_mode_flag == 0)
741 // rem_intra8x8_pred_mode[luma4x4BlkIdx] = parse_u(3, data, bit);
742 // else
743 // assert(false); // rem_intra8x8_pred_mode[] = parse_ae(data, bit);
744 // }
745 // }
746 // }
747 // if(MbPartPredMode(layer.slice_type, transform_size_8x8_flag, mb_type, 0) == Intra_8x8){
748 // assert(false);
749 // }
750 // if(ChromaArrayType == 1 || ChromaArrayType == 2){
751 // assert(false);
752 // if(pic_set.entropy_coding_mode_flag == 0)
753 // uint32_t intra_chroma_pred_mode = parse_ue(data, bit);
754 // else
755 // assert(false); //uint32_t intra_chroma_pred_mode = parse_ae(data, bit);
756 // }
757 // }
758 // else if(MbPartPredMode(layer.slice_type, transform_size_8x8_flag, mb_type, 0) != Direct){
759 // assert(false);
760 // assert(false);
761 // }
762 // }
763 // }
764 // uint32_t coded_block_pattern = 0;
765 // uint32_t CodedBlockPatternLuma = 0;
766 // uint32_t CodedBlockPatternChroma = 0;
767 // if(MbPartPredMode(layer.slice_type, 0, mb_type, 0) != Intra_16x16){
768 // if(pic_set.entropy_coding_mode_flag == 0){
769 // coded_block_pattern = parse_me(ChromaArrayType, MbPartPredMode(layer.slice_type, transform_size_8x8_flag, mb_type, 0), data, bit);
770 // CodedBlockPatternLuma = CodedBlockPatternLumaTab(coded_block_pattern, layer.slice_type, mb_type);
771 // CodedBlockPatternChroma = CodedBlockPatternChromaTab(coded_block_pattern, layer.slice_type, mb_type);
772 // }
773 // else
774 // assert(false); // uint32_t coded_block_pattern = parse_ae(data, bit);
775 // if(CodedBlockPatternLuma > 0 &&
776 // pic_set.transform_8x8_mode_flag && mb_type != I_NxN &&
777 // noSubMbPartSizeLessThan8x8Flag &&
778 // (mb_type != B_Direct_16x16 || seq_set.direct_8x8_inference_flag)){
779 // if(pic_set.entropy_coding_mode_flag == 0)
780 // uint32_t transform_size_8x8_flag = parse_u(1, data, bit);
781 // else
782 // assert(false); // uint32_t transform_size_8x8_flag = parse_ae(data, bit);
783 // }
784 // }
785 // if(CodedBlockPatternLuma>0 || CodedBlockPatternChroma>0 ||
786 // MbPartPredMode(layer.slice_type, transform_size_8x8_flag, mb_type, 0) == Intra_16x16){
787 // if(pic_set.entropy_coding_mode_flag == 0)
788 // uint32_t mb_qp_delta = parse_se(data, bit);
789 // else
790 // assert(false); // uint32_t mb_qp_delta = parse_ae(data, bit);
791 // // residual(0, 15);
792 // {
793 // uint32_t startIdx = 0;
794 // uint32_t endIdx = 0;
795 // if(!pic_set.entropy_coding_mode_flag)
796 // ;// TODO residual_block = residual_block_cavlc;
797 // else
798 // assert(false); // residual_block = residual_block_cabac;
799 // uint32_t i16x16DClevel[16];
800 // uint32_t i16x16AClevel[16][16];
801 // uint32_t level4x4[16][16];
802 // uint32_t level8x8[4][64];
803 // //residual_luma(i16x16DClevel, i16x16AClevel, level4x4, level8x8, startIdx, endIdx);
804 // {
805 // if(startIdx == 0 && MbPartPredMode(layer.slice_type, transform_size_8x8_flag, mb_type, 0) == Intra_16x16)
806 // assert(false); //residual_block(i16x16DClevel, 0, 15, 16);
807 // for(uint32_t i8x8=0; i8x8<4; i8x8++){
808 // if(!transform_size_8x8_flag || !pic_set.entropy_coding_mode_flag){
809 // for(uint32_t i4x4=0; i4x4<4; i4x4++){
810 // if(CodedBlockPatternLuma & (1<<i8x8)){
811 // if(MbPartPredMode(layer.slice_type, transform_size_8x8_flag, mb_type, 0) == Intra_16x16){
812 // assert(false); // residual_block(i16x16AClevel[i8x8*4+i4x4], std:max(0, stdIdx-1), 15);
813 // }
814 // else{
815 // residual_block(level4x4[i8x8*4+i4x4], startIdx, endIdx, 16, data, bit);
816 // }
817 // }
818 // else if(MbPartPredMode(layer.slice_type, transform_size_8x8_flag, mb_type, 0) == Intra_16x16){
819 // for(uint32_t i=0; i<15; i++)
820 // i16x16AClevel[i8x8*4+i4x4][i] = 0;
821 // }
822 // else{
823 // for(uint32_t i=0; i<16; i++)
824 // level4x4[i8x8*4+i4x4][i] = 0;
825 // }
826 // if(!pic_set.entropy_coding_mode_flag && transform_size_8x8_flag){
827 // for(uint32_t i=0; i<16; i++)
828 // level8x8[i8x8][4*i+i4x4] = level4x4[i8x8*4+i4x4][i];
829 // }
830 // }
831 // }
832 // else if(CodedBlockPatternLuma & (1<<i8x8))
833 // assert(false); // residual_block(level8x8[i8x8], 4*startIdx, 4*endIdx+3, 64);
834 // else{
835 // for(uint32_t i=0; i<64; i++)
836 // level8x8[i8x8][i] = 0;
837 // }
838 // }
839 // }
840 // uint32_t *Intra16x16DCLevel = i16x16DClevel;
841 // // uint32_t (*Intra16x16ACLevel)[16] = i16x16AClevel; // BAD
842 // // uint32_t (*LumaLevel4x4)[16] = level4x4;
843 // // uint32_t (*LumaLevel8x8)[64] = level8x8;
844 // if(ChromaArrayType == 1 || ChromaArrayType == 2){
845 // assert(false);
846 // }
847 // else if(ChromaArrayType == 3){
848 // assert(false);
849 // }
850 // }
851 // }
852 // }
853 // }
854 // }
855 // if(!pic_set.entropy_coding_mode_flag)
856 // moreDataFlag = more_rbsp_data(data, size, bit);
857 // else{
858 // assert(false);
859 // }
860 // CurrMbAddr = NextMbAddress(CurrMbAddr, seq_set, pic_set, layer);
861 // } while(moreDataFlag);
862
863 // rbsp_slice_trailing_bits(data, bit);
864
865 // assert(size == bit/8);
866
867 layer.print();
868}
869#endif
870
871static void hrd_parameters(H264ParseData&, HrdParameters &param, const uint8_t *data, size_t &bit)
872{
873 param.cpb_cnt_minus1 = parse_ue(data, bit);
874 assert(param.cpb_cnt_minus1+1 < 32);
875 param.bit_rate_scale = parse_u(4, data, bit);
876 param.cpb_size_scale = parse_u(4, data, bit);
877 for(size_t SchedSelIdx=0; SchedSelIdx<=param.cpb_cnt_minus1; SchedSelIdx++){
878 param.bit_rate_calue_minus1[SchedSelIdx] = parse_ue(data, bit);
879 param.cpb_size_value_minus1[SchedSelIdx] = parse_ue(data, bit);
880 param.cbr_flag[SchedSelIdx] = parse_u(1, data, bit);
881 }
882 param.initial_cpb_removal_delay_length_minus1 = parse_u(5, data, bit);
883 param.cpb_removal_delay_length_minus1 = parse_u(5, data, bit);
884 param.cpb_output_delay_length_minus1 = parse_u(5, data, bit);
885 param.time_offset_length = parse_u(5, data, bit);
886}
887
888static void seq_parameter_set_rbsp(H264ParseData &parse, const uint8_t *data, size_t size)
889{
890#ifdef PRINT_NALU
891 printf("seq_data:");
892 for(uint32_t i=0; i<size; i++){
893 if(i%8 == 0) printf("\n");
894 printf("0x%02x ", data[i]);
895 }
896 printf("\n");
897#endif
898
899 SeqParameterSet &set(parse.seq_set);
900 set = {};
901 size_t bit = 0;
902 set.profile_idc = parse_u(8, data, bit);
903 set.constraint_set0_flag = parse_u(1, data, bit);
904 set.constraint_set1_flag = parse_u(1, data, bit);
905 set.constraint_set2_flag = parse_u(1, data, bit);
906 set.constraint_set3_flag = parse_u(1, data, bit);
907 set.constraint_set3_flag; // TODO: baseline
908 set.constraint_set4_flag = parse_u(1, data, bit);
909 set.constraint_set5_flag = parse_u(1, data, bit);
910 uint32_t reserved_zero_2bits = parse_u(2, data, bit);
911 assert(reserved_zero_2bits == 0);
912 set.level_idc = parse_u(8, data, bit);
913 set.level_idc; // TODO: Baseline
914 set.seq_parameter_set_id = parse_ue(data, bit);
915 if(set.profile_idc == 100 || set.profile_idc == 110 ||
916 set.profile_idc == 122 || set.profile_idc == 244 || set.profile_idc == 44 ||
917 set.profile_idc == 83 || set.profile_idc == 86 || set.profile_idc == 118 ||
918 set.profile_idc == 128 || set.profile_idc == 138 || set.profile_idc == 139 ||
919 set.profile_idc == 134 || set.profile_idc == 135){
920 set.chroma_format_idc = parse_ue(data, bit);
921 if(set.chroma_format_idc == 3)
922 set.separate_colour_plane_flag = parse_u(1, data, bit);
923 set.bit_depth_luma_minus8 = parse_ue(data, bit);
924 set.bit_depth_chroma_minus8 = parse_ue(data, bit);
925 set.qpprime_y_zero_transform_bypass_flag = parse_u(1, data, bit);
926 set.seq_scaling_matrix_present_flag = parse_u(1, data, bit);
927 if(set.seq_scaling_matrix_present_flag){
928 assert(false);
929 // uint32_t seq_scaling_list_present_flag[12];
930 // for(uint32_t i=0; i<((chroma_format_idc != 4u)? 8u: 12u); i++){
931 // seq_scaling_list_present_flag[i] = parse_u(1, data, bit);
932 // if(seq_scaling_list_present_flag[i]){
933 // if(i<6)
934 // scaling_list(Scaling_list4x4[i], 16, UseDefaultScalingMatrix4x4Flag[i]);
935 // else
936 // scaling_list(ScalingList8x8[i-6], 64, UseDefaultScalingMatrix8x8Flag[i-6]);
937 // }
938 // }
939 }
940 }
941 else{
942 set.chroma_format_idc = 1;
943 }
944 set.log2_max_frame_num_minus4 = parse_ue(data, bit);
945 set.pic_order_cnt_type = parse_ue(data, bit);
946 if(set.pic_order_cnt_type == 0)
947 set.log2_max_pic_order_cnt_lsb_minus4 = parse_ue(data, bit);
948 else if(set.pic_order_cnt_type == 1){
949 set.delta_pic_order_always_zero_flag = parse_u(1, data, bit);
950 set.offset_for_non_ref_pic = parse_se(data, bit);
951 set.offset_for_top_to_bottom_field = parse_se(data, bit);
952 set.num_ref_frames_in_pic_order_cnt_cycle = parse_ue(data, bit);
953 assert(set.num_ref_frames_in_pic_order_cnt_cycle <= sizeof(set.offset_for_ref_frame)/sizeof(set.offset_for_ref_frame[0]));
954 for(uint32_t i=0; i<set.num_ref_frames_in_pic_order_cnt_cycle; i++)
955 set.offset_for_ref_frame[i] = parse_se(data, bit);
956 }
957 set.num_ref_frames = parse_ue(data, bit);
958 set.gaps_in_frame_num_value_allowed_flag = parse_u(1, data, bit);
959 set.pic_width_in_mbs_minus1 = parse_ue(data, bit);
960 set.pic_height_in_map_units_minus1 = parse_ue(data, bit);
961 set.frame_mbs_only_flag = parse_u(1, data, bit);
962 // assert(set.frame_mbs_only_flag == 1); // Baseline
963 if(!set.frame_mbs_only_flag)
964 set.mb_adaptive_frame_field_flag = parse_u(1, data, bit);
965 set.direct_8x8_inference_flag = parse_u(1, data, bit);
966 set.frame_cropping_flag = parse_u(1, data, bit);
967 if(set.frame_cropping_flag){
968 set.frame_crop_left_offset = parse_ue(data, bit);
969 set.frame_crop_right_offset = parse_ue(data, bit);
970 set.frame_crop_top_offset = parse_ue(data, bit);
971 set.frame_crop_bottom_offset = parse_ue(data, bit);
972 }
973
974 // Video usability information
975 set.vui_parameters_present_flag = parse_u(1, data, bit);
976 if(set.vui_parameters_present_flag){
977 VuiParameters &vui(parse.vui);
978 // Sample Aspect Ratio (SAR)
979 vui.aspect_ratio_info_present = parse_u(1, data, bit);
980 if(vui.aspect_ratio_info_present){
981 uint32_t aspect_ratio_idc = parse_u(8, data, bit);
982 uint32_t aspect_ratio_width[255] = {
983 1, 12, 10, 16, 40, 24, 20, 32, 80, 18, 15, 64, 160, 4, 3, 2
984 };
985 uint32_t aspect_ratio_height[255] = {
986 1, 11, 11, 11, 33, 11, 11, 11, 33, 11, 11, 33, 99, 3, 2, 1
987 };
988 if(aspect_ratio_idc == 255){ // Extended_SAR
989 vui.sar_width = parse_u(16, data, bit);
990 vui.sar_height = parse_u(16, data, bit);
991 }
992 else{
993 assert(aspect_ratio_idc<=16);
994 vui.sar_width = aspect_ratio_width[aspect_ratio_idc];
995 vui.sar_height = aspect_ratio_height[aspect_ratio_idc];
996 }
997 }
998 // Overscan Info
999 vui.overscan_info_present_flag = parse_u(1, data, bit);
1000 if(vui.overscan_info_present_flag){
1001 vui.overscan_appropriate_flag = parse_u(1, data, bit);
1002 }
1003 // Video Signal Type
1004 vui.video_signal_type_present_flag = parse_u(1, data, bit);
1005 if(vui.video_signal_type_present_flag){
1006 vui.video_format = parse_u(3, data, bit);
1007 vui.video_full_range_flag = parse_u(1, data, bit);
1008 vui.colour_description_present_flag = parse_u(1, data, bit);
1009 if(vui.colour_description_present_flag){
1010 vui.colour_primaries = parse_u(8, data, bit);
1011 vui.transfer_characteristics = parse_u(8, data, bit);
1012 vui.matrix_coefficients = parse_u(8, data, bit);
1013 }
1014 }
1015 // Chroma Location Info
1016 vui.chroma_loc_info_present_flag = parse_u(1, data, bit);
1017 if(vui.chroma_loc_info_present_flag){
1018 vui.chroma_sample_loc_type_top_field = parse_ue(data, bit);
1019 vui.chroma_sample_loc_type_bottom_field = parse_ue(data, bit);
1020 }
1021 // Timing Information
1022 vui.timing_info_present_flag = parse_u(1, data, bit);
1023 if(vui.timing_info_present_flag){
1024 vui.num_units_in_tick = parse_u(32, data, bit);
1025 vui.time_scale = parse_u(32, data, bit);
1026 vui.fixed_frame_rate_flag = parse_u(1, data, bit);
1027 }
1028 // Network Abstraction Layer - Hypotetical Reference Decoder - Parameters
1029 vui.nal_hrd_parameters_present_flag = parse_u(1, data, bit);
1030 if(vui.nal_hrd_parameters_present_flag){
1031 hrd_parameters(parse, parse.vui.nal_hrd_parameters, data, bit);
1032 }
1033 // Video Coding Layer - Hypotetical Reference Decoder - Parameters
1034 vui.vcl_hrd_parameters_present_flag = parse_u(1, data, bit);
1035 if(vui.vcl_hrd_parameters_present_flag){
1036 hrd_parameters(parse, parse.vui.vcl_hrd_parameters, data, bit);
1037 }
1038 // Low Delay - Hypotetical Reference Decoder
1039 if(vui.nal_hrd_parameters_present_flag || vui.vcl_hrd_parameters_present_flag){
1040 vui.low_delay_hrd_flag = parse_u(1, data, bit);
1041 }
1042 // Picture timing SEI messages are preset
1043 vui.pic_struct_present_flag = parse_u(1, data, bit);
1044 // Video sequence bitstream restriction parameters
1045 vui.bitstream_restriction_flag = parse_u(1, data, bit);
1046 if(vui.bitstream_restriction_flag){
1047 vui.motion_vectors_over_pic_bondaries_flag = parse_u(1, data, bit);
1048 vui.max_bytes_per_pic_denom = parse_ue(data, bit);
1049 vui.max_bits_per_pic_denom = parse_ue(data, bit);
1050 if(bit/8 >= size) return; // Faulty NALU
1051 vui.log2_max_mv_length_horizontal = parse_ue(data, bit, size);
1052 if(bit/8 >= size) return; // Faulty NALU
1053 vui.log2_max_mv_length_vertical = parse_ue(data, bit, size);
1054 if(bit/8 >= size) return; // Faulty NALU
1055 vui.max_num_reorder_frames = parse_ue(data, bit, size);
1056 if(bit/8 >= size) return; // Faulty NALU
1057 vui.max_dec_frame_buffering = parse_ue(data, bit, size);
1058 }
1059 }
1060 if(bit/8 == size) return; // Faulty NALU
1061 rbsp_trailing_bits(data, bit);
1062}
1063
1064static void pic_parameter_set_rbsp(H264ParseData &parse, const uint8_t *data, size_t size)
1065{
1066 PicParameterSet &set(parse.pic_set);
1067 set = {};
1068 size_t bit = 0;
1069
1070 set.pic_parameter_set_id = parse_ue(data, bit);
1071 set.seq_parameter_set_id = parse_ue(data, bit);
1072 set.entropy_coding_mode_flag = parse_u(1, data, bit);
1073 // assert(set.entropy_coding_mode_flag == 0); // Baseline
1074 set.bottom_field_pic_order_in_frame_present_flag = parse_u(1, data, bit);
1075 set.num_slice_groups_minus1 = parse_ue(data, bit);
1076 // assert(set.num_slice_groups_minus1 >= 0 && set.num_slice_groups_minus1 <= 7); // Baseline
1077 if(set.num_slice_groups_minus1 > 0){
1078 set.slice_group_map_type = parse_ue(data, bit);
1079 if(set.slice_group_map_type == 0){
1080 for(uint32_t iGroup = 0; iGroup <= set.num_slice_groups_minus1; iGroup++){
1081 parse_ue(data, bit);
1082 //run_length_minus1[iGroup] = parse_ue(data, bit); // TODO
1083 }
1084 }
1085 else if(set.slice_group_map_type == 2){
1086 for(uint32_t iGroup = 0; iGroup <= set.num_slice_groups_minus1; iGroup++){
1087 parse_ue(data, bit);
1088 parse_ue(data, bit);
1089 //top_left[iGroup] = parse_ue(data, bit); // TODO
1090 //bottom_right[iGroup] = parse_ue(data, bit); // TODO
1091 }
1092 }
1093 else if(set.slice_group_map_type == 3 ||
1094 set.slice_group_map_type == 4 ||
1095 set.slice_group_map_type == 5){
1096 //uint32_t slice_group_change_direction_flag =
1097 parse_u(1, data, bit);
1098 //uint32_t slice_group_change_rate_minus1 =
1099 parse_ue(data, bit);
1100 }
1101 else if(set.slice_group_map_type == 6){
1102 uint32_t pic_size_in_map_units_minus1 = parse_ue(data, bit);
1103 for(uint32_t i=0; i<=pic_size_in_map_units_minus1; i++){
1104 assert(false);
1105 //slice_group_id[i] = parse_u(, data, bit); // u(v)
1106 }
1107 }
1108 }
1109 set.num_ref_idx_l0_default_active_minus1 = parse_ue(data, bit);
1110 set.num_ref_idx_l1_default_active_minus1 = parse_ue(data, bit);
1111 set.weighted_pred_flag = parse_u(1, data, bit);
1112 // assert(set.weighted_pred_flag == 0); // Baseline
1113 set.weighted_bipred_idc = parse_u(2, data, bit);
1114 // assert(set.weighted_bipred_idc == 0); // Baseline
1115 set.pic_init_qp_minus26 = parse_se(data, bit);
1116 set.pic_init_sp_minus26 = parse_se(data, bit);
1117 set.chroma_qp_index_offset = parse_se(data, bit);
1118 set.deblocking_filter_control_present_flag = parse_u(1, data, bit);
1119 set.constrained_intra_pred_flag = parse_u(1, data, bit);
1120 set.redundant_pic_cnt_present_flag = parse_u(1, data, bit);
1121
1122 // assert(!more_rbsp_data(data, size, bit)); // Baseline
1123 if(more_rbsp_data(data, size, bit)){
1124 set.transform_8x8_mode_flag = parse_u(1, data, bit);
1125 set.pic_scaling_matrix_present_flag = parse_u(1, data, bit);
1126 if(set.pic_scaling_matrix_present_flag){
1127 assert(false);
1128 // for(uint32_t i=0; i<6 + ((chroma_format_idc != 3) ? 2 : 6)*transform_8x8_mode_flag; i++){
1129 // pic_scaling_list_present_flag[i] = parse_u(1, data, bit);
1130 // if(pic_scaling_list_present_flag[i])
1131 // if(i<6)
1132 // scaling_list(ScalingList4x4[i], 16, UseDefaultScalingMatrix4x4Flag[i]);
1133 // else
1134 // scaling_list(ScalingList8x8[i-6], 64 UseDefaultScalingMatrix8x8Flag[i-6]);
1135 // }
1136 }
1137 set.second_chroma_qp_index_offset = parse_se(data, bit);
1138 }
1139 rbsp_trailing_bits(data, bit);
1140}
1141
1142static void buffering_period(H264ParseData &parse, const uint8_t *data, size_t &bit, uint32_t)
1143{
1144 uint32_t NalHrdBpPresentFlag = parse.vui.nal_hrd_parameters_present_flag; // TODO: or app determined
1145 uint32_t VclHrdBpPresentFlag = parse.vui.vcl_hrd_parameters_present_flag; // TODO: or app determined
1146
1147 uint32_t seq_parameter_set_id =
1148 parse_ue(data, bit);
1149 printf(" - seq_parameter_set_id %u\n", seq_parameter_set_id);
1150 if(NalHrdBpPresentFlag){
1151 uint32_t initial_cpb_removal_delay[32];
1152 uint32_t initial_cpb_removal_delay_offset[32];
1153 for(size_t SchedSelIdx=0; SchedSelIdx<=parse.vui.nal_hrd_parameters.cpb_cnt_minus1; SchedSelIdx++){
1154 uint32_t v = parse.vui.nal_hrd_parameters.initial_cpb_removal_delay_length_minus1+1;
1155 initial_cpb_removal_delay[SchedSelIdx] = parse_u(v, data, bit);
1156 initial_cpb_removal_delay_offset[SchedSelIdx] = parse_u(v, data, bit);
1157 printf(" - initial_cpb_removal_delay[%zu] %u\n", SchedSelIdx, initial_cpb_removal_delay[SchedSelIdx]);
1158 printf(" - initial_cpb_removal_delay_offset[%zu] %u\n", SchedSelIdx, initial_cpb_removal_delay_offset[SchedSelIdx]);
1159 }
1160 }
1161
1162 if(VclHrdBpPresentFlag){
1163 uint32_t initial_cpb_removal_delay[32];
1164 uint32_t initial_cpb_removal_delay_offset[32];
1165 for(size_t SchedSelIdx=0; SchedSelIdx<=parse.vui.vcl_hrd_parameters.cpb_cnt_minus1; SchedSelIdx++){
1166 uint32_t v = parse.vui.vcl_hrd_parameters.initial_cpb_removal_delay_length_minus1;
1167 initial_cpb_removal_delay[SchedSelIdx] = parse_u(v, data, bit);
1168 initial_cpb_removal_delay_offset[SchedSelIdx] = parse_u(v, data, bit);
1169 printf(" - initial_cpb_removal_delay[%zu] %u\n", SchedSelIdx, initial_cpb_removal_delay[SchedSelIdx]);
1170 printf(" - initial_cpb_removal_delay_offset[%zu] %u\n", SchedSelIdx, initial_cpb_removal_delay_offset[SchedSelIdx]);
1171 }
1172 }
1173}
1174
1175static void pic_timing(H264ParseData &parse, const uint8_t *data, size_t &bit, uint32_t)
1176{
1177 uint32_t CpbDpbDelaysPresentFlag = parse.vui.nal_hrd_parameters_present_flag | parse.vui.vcl_hrd_parameters_present_flag;
1178
1179 HrdParameters &hrd(parse.vui.nal_hrd_parameters_present_flag ? parse.vui.nal_hrd_parameters : parse.vui.vcl_hrd_parameters);
1180
1181 if(CpbDpbDelaysPresentFlag){
1182 uint32_t cpb_removal_delay = parse_u(hrd.cpb_removal_delay_length_minus1+1, data, bit);
1183 uint32_t cpb_output_delay = parse_u(hrd.cpb_output_delay_length_minus1+1, data, bit);
1184 printf(" - cpb_removal_delay %u\n", cpb_removal_delay);
1185 printf(" - cpb_output_delay %u\n", cpb_output_delay);
1186 }
1187 if(parse.vui.pic_struct_present_flag){
1188 uint32_t pic_struct = parse_u(4, data, bit);
1189 printf(" - pic_struct %u\n", pic_struct);
1190
1191 uint32_t NumClockTS = 1;
1192 assert(pic_struct <= 8);
1193 switch(pic_struct){
1194 case 0:
1195 case 1:
1196 case 2:
1197 NumClockTS = 1;
1198 break;
1199 case 3:
1200 case 4:
1201 NumClockTS = 2;
1202 break;
1203 case 5:
1204 case 6:
1205 NumClockTS = 3;
1206 break;
1207 case 7:
1208 NumClockTS = 2;
1209 break;
1210 case 8:
1211 NumClockTS = 3;
1212 break;
1213 }
1214 printf(" - NumClockTS %u\n", NumClockTS);
1215
1216 uint32_t clock_timestamp_flag[3];
1217 for(size_t i=0; i<NumClockTS; i++){
1218 clock_timestamp_flag[i] = parse_u(1, data, bit);
1219 printf(" - clock_timestamp_flag[%zu] %u\n", i, clock_timestamp_flag[i]);
1220 if(clock_timestamp_flag[i]){
1221 uint32_t ct_type = parse_u(2, data, bit);
1222 printf(" - ct_type %u\n", ct_type);
1223 uint32_t nuit_field_based_flag = parse_u(1, data, bit);
1224 printf(" - nuit_field_based_flag %u\n", nuit_field_based_flag);
1225 uint32_t counting_type = parse_u(5, data, bit);
1226 printf(" - counting_type %u\n", counting_type);
1227 uint32_t full_timestamp_flag = parse_u(1, data, bit);
1228 printf(" - full_timestamp_flag %u\n", full_timestamp_flag);
1229 uint32_t discontinuity_flag = parse_u(1, data, bit);
1230 printf(" - discontinuity_flag %u\n", discontinuity_flag);
1231 uint32_t cnt_dropped_flag = parse_u(1, data, bit);
1232 printf(" - cnt_dropped_flag %u\n", cnt_dropped_flag);
1233 uint32_t n_frames = parse_u(5, data, bit);
1234 printf(" - n_frames %u\n", n_frames);
1235 if(full_timestamp_flag){
1236 uint32_t seconds_value = parse_u(6, data, bit);
1237 uint32_t minutes_value = parse_u(6, data, bit);
1238 uint32_t hours_value = parse_u(5, data, bit);
1239 printf(" - seconds_value %u\n", seconds_value);
1240 printf(" - minutes_value %u\n", minutes_value);
1241 printf(" - hours_value %u\n", hours_value);
1242 }
1243 else{
1244 uint32_t seconds_flag = parse_u(1, data, bit);
1245 printf(" - seconds_flag %u\n", seconds_flag);
1246 if(seconds_flag){
1247 uint32_t seconds_value = parse_u(6, data, bit);
1248 uint32_t minutes_flag = parse_u(1, data, bit);
1249 printf(" - seconds_value %u\n", seconds_value);
1250 printf(" - minutes_flag %u\n", minutes_flag);
1251 if(minutes_flag){
1252 uint32_t minutes_value = parse_u(6, data, bit);
1253 uint32_t hours_flag = parse_u(1, data, bit);
1254 printf(" - minutes_value %u\n", minutes_value);
1255 printf(" - hours_flag %u\n", hours_flag);
1256 if(hours_flag){
1257 uint32_t hours_value = parse_u(5, data, bit);
1258 printf(" - hours_value %u\n", hours_value);
1259 }
1260 }
1261 }
1262 }
1263 uint32_t v = hrd.time_offset_length;
1264 if(v>0){
1265 int32_t time_offset = parse_i(v, data, bit);
1266 printf(" - time_offset %i\n", time_offset);
1267 }
1268 }
1269 }
1270 }
1271}
1272
1273static void filler_payload(H264ParseData&, const uint8_t *data, size_t &bit, uint32_t payloadSize)
1274{
1275 for(size_t k=0; k<payloadSize; k++){
1276 uint32_t ff_byte = parse_f(8, data, bit);
1277 assert(ff_byte == 0xff);
1278 }
1279}
1280
1281static void recovery_point(H264ParseData&, const uint8_t *data, size_t &bit, uint32_t)
1282{
1283 uint32_t recovery_frame_cnt =
1284 parse_ue(data, bit);
1285 uint32_t exact_match_flag =
1286 parse_u(1, data, bit);
1287 uint32_t broken_link_flag =
1288 parse_u(1, data, bit);
1289 uint32_t changing_slice_group_idc =
1290 parse_u(2, data, bit);
1291
1292 printf("recovery_frame_cnt %u\n", recovery_frame_cnt);
1293 printf("exact_match_flag %u\n", exact_match_flag);
1294 printf("broken_link_flag %u\n", broken_link_flag);
1295 printf("changing_slice_group_idc %u\n", changing_slice_group_idc);
1296}
1297// static void frame_packing_arrangement(H264ParseData&, uint8_t *data, size_t &bit, uint32_t payloadSize)
1298// {
1299// assert(false);
1300// }
1301
1302static void reserverd_sei_message(H264ParseData&, const uint8_t* /*data*/, size_t &bit, uint32_t payloadSize)
1303{
1304 bit += 8*payloadSize;
1305 // for(size_t k=0; k<payloadSize; k++){
1306 // uint32_t reserved_sei_message_payload_byte = parse_b(8, data, bit);
1307 // }
1308}
1309static bool byte_aligned(size_t &bit)
1310{
1311 return (bit%8)==0;
1312}
1313
1314static void sei_payload(H264ParseData &parase, const uint8_t *data, size_t &bit, uint32_t payloadType, uint32_t payloadSize)
1315{
1316 printf("SEI payloadType %u\n", payloadType);
1317 if(payloadType == 0){
1318 buffering_period(parase, data, bit, payloadSize);
1319 }
1320 else if(payloadType == 1){
1321 pic_timing(parase, data, bit, payloadSize);
1322 }
1323 else if(payloadType == 3){
1324 filler_payload(parase, data, bit, payloadSize);
1325 }
1326 else if(payloadType == 6){
1327 recovery_point(parase, data, bit, payloadSize);
1328 }
1329 // else if(payloadType == 45){
1330 // frame_packing_arrangement(parase, data, bit, payloadSize);
1331 // }
1332 else{
1333 reserverd_sei_message(parase, data, bit, payloadSize);
1334 }
1335 if(!byte_aligned(bit)){
1336 uint32_t bit_equal_to_one = parse_u(1, data, bit);
1337 assert(bit_equal_to_one == 1);
1338 while(!byte_aligned(bit)){
1339 uint32_t bit_equal_to_zero = parse_u(1, data, bit);
1340 assert(bit_equal_to_zero == 0);
1341 }
1342 }
1343}
1344
1345static void sei_message(H264ParseData &parse, const uint8_t *data, size_t, size_t &bit)
1346{
1347 uint32_t payloadType = 0;
1348 while(next_bits(8, data, bit) == 0xff){
1349 uint32_t ff_byte = parse_f(8, data, bit);
1350 assert(ff_byte == 0xff);
1351 payloadType += 255;
1352 }
1353 uint32_t last_payload_type_byte = parse_u(8, data, bit);
1354 payloadType += last_payload_type_byte;
1355 uint32_t payloadSize = 0;
1356 while(next_bits(8, data, bit) == 0xff){
1357 uint32_t ff_byte = parse_f(8, data, bit);
1358 assert(ff_byte == 0xff);
1359 payloadType += 255;
1360 }
1361 uint32_t last_payload_size_byte = parse_u(8, data, bit);
1362 payloadSize += last_payload_size_byte;
1363 sei_payload(parse, data, bit, payloadType, payloadSize);
1364}
1365
1366static void sei_rbsp(H264ParseData &parse, const uint8_t *data, size_t size)
1367{
1368 size_t bit = 0;
1369 do{
1370 sei_message(parse, data, size, bit);
1371 } while(more_rbsp_data(data, size, bit));
1372 rbsp_trailing_bits(data, bit);
1373}
1374
1375static void access_unit_delimiter_rbsp(H264ParseData&, const uint8_t *data, size_t)
1376{
1377 size_t bit = 0;
1378 //uint32_t primary_pic_type =
1379 parse_u(3, data, bit);
1380 rbsp_trailing_bits(data, bit);
1381}
1382
1383static void end_of_seq_rbsp(H264ParseData&, const uint8_t* /*data*/, size_t)
1384{
1385 // NOP
1386}
1387
1388static void end_of_stream_rbsp(H264ParseData&, const uint8_t* /*data*/, size_t)
1389{
1390 // NOP
1391}
1392
1393static void filler_data_rbsp(H264ParseData&, const uint8_t *data, size_t size)
1394{
1395 size_t i=0;
1396 for(; data[i]==0xff && i<size; i++)
1397 assert(data[i] == 0xff);
1398 size_t bit = i*8;
1399 rbsp_trailing_bits(data, bit);
1400}
1401
1402static void prefix_nal_unit_rbsp(H264ParseData&, const uint8_t* /*data*/, size_t)
1403{
1404 assert(false);
1405 // if(svc_extension_flag)
1406 // prefix_nal_unit_svc(parse, data, size);
1407}
1408
1409static void seq_parameter_set_extension_rbsp(H264ParseData&, const uint8_t* /*data*/, size_t)
1410{
1411 assert(false);
1412}
1413
1414void Cogs::Core::seq_parameter_set_nalu(H264ParseData &parse, const uint8_t *data, size_t size)
1415{
1416 size_t i=0;
1417 size_t rbsp_size = size;
1418 uint32_t NumBytesInRBSP = 0;
1419 uint8_t *rbsp = new uint8_t[rbsp_size];
1420
1421 // NAL header
1422 size_t bit = i*8;
1423 i = i+1;
1424 NALheader header = {};
1425 header.forbidden_zero_bit = parse_f(1, data, bit);
1426 assert(header.forbidden_zero_bit == 0);
1427 header.nal_ref_idc = parse_u(2, data, bit);
1428 header.nal_unit_type = parse_u(5, data, bit);
1429 assert(header.nal_unit_type == 7);
1430
1431 size_t j=size;
1432 while(i<j){
1433 if(i+2 < j && data[i+0] == 0x00 && data[i+1] == 0x00 && data[i+2] == 0x03){
1434 rbsp[NumBytesInRBSP++] = data[i++];
1435 rbsp[NumBytesInRBSP++] = data[i++];
1436 i++;
1437 }
1438 else{
1439 rbsp[NumBytesInRBSP++] = data[i++];
1440 }
1441 assert(NumBytesInRBSP <= rbsp_size);
1442 }
1443
1444 seq_parameter_set_rbsp(parse, rbsp, NumBytesInRBSP);
1445
1446 delete[] rbsp;
1447}
1448
1449void Cogs::Core::pic_parameter_set_nalu(H264ParseData &parse, const uint8_t *data, size_t size)
1450{
1451 size_t i=0;
1452 size_t rbsp_size = size;
1453 uint32_t NumBytesInRBSP = 0;
1454 uint8_t *rbsp = new uint8_t[rbsp_size];
1455
1456 // NAL header
1457 size_t bit = i*8;
1458 i = i+1;
1459 NALheader header = {};
1460 header.forbidden_zero_bit = parse_f(1, data, bit);
1461 assert(header.forbidden_zero_bit == 0);
1462 header.nal_ref_idc = parse_u(2, data, bit);
1463 header.nal_unit_type = parse_u(5, data, bit);
1464 assert(header.nal_unit_type == 8);
1465
1466 size_t j=size;
1467 while(i<j){
1468 if(i+2 < j && data[i+0] == 0x00 && data[i+1] == 0x00 && data[i+2] == 0x03){
1469 rbsp[NumBytesInRBSP++] = data[i++];
1470 rbsp[NumBytesInRBSP++] = data[i++];
1471 i++;
1472 }
1473 else{
1474 rbsp[NumBytesInRBSP++] = data[i++];
1475 }
1476 assert(NumBytesInRBSP <= rbsp_size);
1477 }
1478
1479 pic_parameter_set_rbsp(parse, rbsp, NumBytesInRBSP);
1480
1481 delete[] rbsp;
1482}
1483
1484void Cogs::Core::byte_stream_nal_unit(H264ParseData &parse, const uint8_t *data, size_t size)
1485{
1486#ifdef PRINT_NALU
1487 printf("nalu:");
1488 for(uint32_t i=0; i<size; i++){
1489 if(i%8 == 0) printf("\n");
1490 printf("0x%02x ", data[i]);
1491 }
1492 printf("\n");
1493#endif
1494
1495 size_t i=0;
1496
1497 // Remove leading zero
1498 while(!(data[i+0] == 0x00 && data[i+1] == 0x00 && data[i+2] == 0x00 && data[i+3] == 0x01) &&
1499 !(data[i+0] == 0x00 && data[i+1] == 0x00 && data[i+2] == 0x01)){
1500 assert(data[i] == 0x00);
1501 i++;
1502 }
1503
1504 while(i <= size){
1505 // NAL Start prefix
1506 if(data[i+0] == 0x00 && data[i+1] == 0x00 && data[i+2] == 0x00 && data[i+3] == 0x01){
1507 i += 4;
1508 }
1509 else{
1510 assert(data[i+0] == 0x00 && data[i+1] == 0x00 && data[i+2] == 0x01);
1511 i += 3;
1512 }
1513
1514 // NAL header
1515 size_t bit = (i++)*8;
1516 NALheader &header(parse.nal_header);
1517 header.forbidden_zero_bit = parse_f(1, data, bit);
1518 assert(header.forbidden_zero_bit == 0);
1519 header.nal_ref_idc = parse_u(2, data, bit);
1520 header.nal_unit_type = parse_u(5, data, bit);
1521
1522 // uint32_t nalUnitHeaderBytes = 1;
1523 if(header.nal_unit_type == 14 || header.nal_unit_type == 20 || header.nal_unit_type == 21){
1524 assert(false);
1525 }
1526
1527 // Find next NALU
1528 size_t j=i;
1529 while(!(data[j+0] == 0x00 && data[j+1] == 0x00 && data[j+2] == 0x00 && data[j+3] == 0x01) &&
1530 !(data[j+0] == 0x00 && data[j+1] == 0x00 && data[j+2] == 0x01) &&
1531 j < size){
1532 j++;
1533 }
1534 size_t rbsp_size = j-i;
1535
1536 uint32_t NumBytesInRBSP = 0;
1537 uint8_t *rbsp = new uint8_t[rbsp_size]; // (uint8_t*)malloc(rbsp_size);
1538 while(i<j){
1539 if(i+2 < j && data[i+0] == 0x00 && data[i+1] == 0x00 && data[i+2] == 0x03){
1540 rbsp[NumBytesInRBSP++] = data[i++];
1541 rbsp[NumBytesInRBSP++] = data[i++];
1542 i++;
1543 }
1544 else{
1545 rbsp[NumBytesInRBSP++] = data[i++];
1546 }
1547 assert(NumBytesInRBSP <= rbsp_size);
1548 }
1549
1550 //bool IdrPicFlag; // Instantaneous decoding refresh
1551 switch(header.nal_unit_type){
1552 case 1:
1553 //IdrPicFlag = false;
1554 //slice_layer_without_partitioning_rbsp(parse, rbsp, NumBytesInRBSP, IdrPicFlag);
1555 break;
1556 case 5:
1557 //IdrPicFlag = true;
1558 //slice_layer_without_partitioning_rbsp(parse, rbsp, NumBytesInRBSP, IdrPicFlag);
1559 break;
1560 case 6:
1561 sei_rbsp(parse, rbsp, NumBytesInRBSP);
1562 break;
1563 case 7:
1564 seq_parameter_set_rbsp(parse, rbsp, NumBytesInRBSP);
1565 break;
1566 case 8:
1567 pic_parameter_set_rbsp(parse, rbsp, NumBytesInRBSP);
1568 break;
1569 case 9:
1570 access_unit_delimiter_rbsp(parse, rbsp, NumBytesInRBSP);
1571 break;
1572 case 10:
1573 end_of_seq_rbsp(parse, rbsp, NumBytesInRBSP);
1574 break;
1575 case 11:
1576 end_of_stream_rbsp(parse, rbsp, NumBytesInRBSP);
1577 break;
1578 case 12:
1579 filler_data_rbsp(parse, rbsp, NumBytesInRBSP);
1580 break;
1581 case 13:
1582 seq_parameter_set_extension_rbsp(parse, rbsp, NumBytesInRBSP);
1583 break;
1584 case 14:
1585 prefix_nal_unit_rbsp(parse, rbsp, NumBytesInRBSP);
1586 break;
1587 default:
1588 assert(false);
1589 break;
1590 }
1591
1592 delete [] rbsp; // free(rbsp);
1593 i=j;
1594 if(i == size) break;
1595 }
1596 assert(i == size);
1597}
1598
1599void Cogs::Core::extract_spspps(const uint8_t *data, size_t size, std::string &sps, std::string &pps)
1600{
1601 size_t i=0;
1602 // Remove leading zero
1603 while(!(data[i+0] == 0x00 && data[i+1] == 0x00 && data[i+2] == 0x00 && data[i+3] == 0x01) &&
1604 !(data[i+0] == 0x00 && data[i+1] == 0x00 && data[i+2] == 0x01)){
1605 assert(data[i] == 0x00);
1606 i++;
1607 }
1608
1609 while(i < size){
1610 // NAL Start prefix
1611 if(data[i+0] == 0x00 && data[i+1] == 0x00 && data[i+2] == 0x00 && data[i+3] == 0x01){
1612 i += 4;
1613 }
1614 else{
1615 assert(data[i+0] == 0x00 && data[i+1] == 0x00 && data[i+2] == 0x01);
1616 i += 3;
1617 }
1618
1619 // NAL header
1620 size_t bit = i*8;
1621 // i = i+1;
1622 NALheader header = {};
1623 header.forbidden_zero_bit = parse_f(1, data, bit);
1624 assert(header.forbidden_zero_bit == 0);
1625 header.nal_ref_idc = parse_u(2, data, bit);
1626 header.nal_unit_type = parse_u(5, data, bit);
1627
1628 // Find next NALU
1629 size_t j=i;
1630 while(!(data[j+0] == 0x00 && data[j+1] == 0x00 && data[j+2] == 0x00 && data[j+3] == 0x01) &&
1631 !(data[j+0] == 0x00 && data[j+1] == 0x00 && data[j+2] == 0x01) &&
1632 j < size){
1633 j++;
1634 }
1635 size_t rbsp_size = j-i;
1636
1637 if(header.nal_unit_type == 7 || header.nal_unit_type == 8){
1638 switch(header.nal_unit_type){
1639 case 7:
1640 sps = std::string((char*)&data[i], rbsp_size);
1641 if (!pps.empty()) {
1642 return;
1643 }
1644 break;
1645 case 8:
1646 pps = std::string((char*)&data[i], rbsp_size);
1647 if (!sps.empty()) {
1648 return;
1649 }
1650 break;
1651 }
1652 }
1653
1654 i=j;
1655 }
1656}
1657
1658bool Cogs::Core::extract_nalu_header(NALheader &header, const uint8_t *data, size_t size)
1659{
1660 size_t i=0;
1661 if(i+3 >= size) return false;
1662 // Remove leading zero
1663 while(!(data[i+0] == 0x00 && data[i+1] == 0x00 && data[i+2] == 0x00 && data[i+3] == 0x01) &&
1664 !(data[i+0] == 0x00 && data[i+1] == 0x00 && data[i+2] == 0x01)){
1665 if(data[i] != 0x00) return false;
1666 i++;
1667 if(i+3 >= size) return false;
1668 }
1669
1670 // NAL Start prefix
1671 if(data[i+0] == 0x00 && data[i+1] == 0x00 && data[i+2] == 0x00 && data[i+3] == 0x01){
1672 i += 4;
1673 }
1674 else if(data[i+0] == 0x00 && data[i+1] == 0x00 && data[i+2] == 0x01){
1675 i += 3;
1676 }
1677 else{
1678 return false;
1679 }
1680
1681 // NAL header
1682 size_t bit = i*8;
1683 header.forbidden_zero_bit = parse_f(1, data, bit);
1684 if(header.forbidden_zero_bit != 0) return false;
1685 header.nal_ref_idc = parse_u(2, data, bit);
1686 header.nal_unit_type = parse_u(5, data, bit);
1687 return true;
1688}
1689
1690size_t Cogs::Core::find_next_frame(const uint8_t *data, size_t size)
1691{
1692 if(size < 4) return size;
1693 size_t i=0;
1694 // Remove leading zero
1695 while(!(data[i+0] == 0x00 && data[i+1] == 0x00 && data[i+2] == 0x00 && data[i+3] == 0x01) &&
1696 !(data[i+0] == 0x00 && data[i+1] == 0x00 && data[i+2] == 0x01)){
1697 assert(data[i] == 0x00);
1698 i++;
1699 }
1700
1701 // NAL Start prefix
1702 if(data[i+0] == 0x00 && data[i+1] == 0x00 && data[i+2] == 0x00 && data[i+3] == 0x01){
1703 i += 4;
1704 }
1705 else{
1706 assert(data[i+0] == 0x00 && data[i+1] == 0x00 && data[i+2] == 0x01);
1707 i += 3;
1708 }
1709
1710 // Find next NALU
1711 {
1712 size_t j=i;
1713 while(!(data[j+0] == 0x00 && data[j+1] == 0x00 && data[j+2] == 0x00 && data[j+3] == 0x01) &&
1714 !(data[j+0] == 0x00 && data[j+1] == 0x00 && data[j+2] == 0x01) &&
1715 j < size){
1716 j++;
1717 }
1718 i=j;
1719 }
1720
1721 while(i+4 <= size){
1722 size_t end = i;
1723 // NAL Start prefix
1724 if(data[i+0] == 0x00 && data[i+1] == 0x00 && data[i+2] == 0x00 && data[i+3] == 0x01){
1725 i += 4;
1726 }
1727 else{
1728 assert(data[i+0] == 0x00 && data[i+1] == 0x00 && data[i+2] == 0x01);
1729 i += 3;
1730 }
1731
1732 // NAL header
1733 size_t bit = i*8;
1734 // i = i+1;
1735 NALheader header = {};
1736 header.forbidden_zero_bit = parse_f(1, data, bit);
1737 assert(header.forbidden_zero_bit == 0);
1738 header.nal_ref_idc = parse_u(2, data, bit);
1739 header.nal_unit_type = parse_u(5, data, bit);
1740
1741 if(header.nal_unit_type <= 5 || header.nal_unit_type >= 19)
1742 return end;
1743
1744 // Find next NALU
1745 size_t j=i;
1746 while(!(data[j+0] == 0x00 && data[j+1] == 0x00 && data[j+2] == 0x00 && data[j+3] == 0x01) &&
1747 !(data[j+0] == 0x00 && data[j+1] == 0x00 && data[j+2] == 0x01) &&
1748 j < size){
1749 j++;
1750 }
1751
1752 i=j;
1753 if(i == size) break;
1754 }
1755
1756 return size;
1757}
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....