-
Notifications
You must be signed in to change notification settings - Fork 13
/
h264_stream.c
139 lines (119 loc) · 3.3 KB
/
h264_stream.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
/*
* h264bitstream - a library for reading and writing H.264 video
* Copyright (C) 2005-2007 Auroras Entertainment, LLC
* Copyright (C) 2008-2011 Avail-TVN
* Copyright (C) 2012 Alex Izvorski
*
* Written by Alex Izvorski <[email protected]> and Alex Giladi <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include "bs.h"
#include "h264_stream.h"
#include "h264_sei.h"
FILE* h264_dbgfile = NULL;
#define printf(...) fprintf((h264_dbgfile == NULL ? stdout : h264_dbgfile), __VA_ARGS__)
/**
Calculate the log base 2 of the argument, rounded up.
Zero or negative arguments return zero
Idea from http://www.southwindsgames.com/blog/2009/01/19/fast-integer-log2-function-in-cc/
*/
int intlog2(int x)
{
int log = 0;
if (x < 0) { x = 0; }
while ((x >> log) > 0)
{
log++;
}
if (log > 0 && x == 1<<(log-1)) { log--; }
return log;
}
int is_slice_type(int slice_type, int cmp_type)
{
if (slice_type >= 5) { slice_type -= 5; }
if (cmp_type >= 5) { cmp_type -= 5; }
if (slice_type == cmp_type) { return 1; }
else { return 0; }
}
int more_rbsp_data(bs_t* bs)
{
// TODO this version handles reading only. writing version?
// no more data
if (bs_eof(bs)) { return 0; }
// no rbsp_stop_bit yet
if (bs_peek_u1(bs) == 0) { return 1; }
// next bit is 1, is it the rsbp_stop_bit? only if the rest of bits are 0
bs_t bs_tmp;
bs_clone(&bs_tmp, bs);
bs_skip_u1(&bs_tmp);
while(!bs_eof(&bs_tmp))
{
// A later bit was 1, it wasn't the rsbp_stop_bit
if (bs_read_u1(&bs_tmp) == 1) { return 1; }
}
// All following bits were 0, it was the rsbp_stop_bit
return 0;
}
int more_rbsp_trailing_data(bs_t* b) { return !bs_eof(b); }
int _read_ff_coded_number(bs_t* b)
{
int n1 = 0;
int n2;
do
{
n2 = bs_read_u8(b);
n1 += n2;
} while (n2 == 0xff);
return n1;
}
void _write_ff_coded_number(bs_t* b, int n)
{
while (1)
{
if (n > 0xff)
{
bs_write_u8(b, 0xff);
n -= 0xff;
}
else
{
bs_write_u8(b, n);
break;
}
}
}
void debug_bytes(uint8_t* buf, int len)
{
int i;
for (i = 0; i < len; i++)
{
printf("%02X ", buf[i]);
if ((i+1) % 16 == 0) { printf ("\n"); }
}
printf("\n");
}
//7.3.2.11 RBSP trailing bits syntax
void read_rbsp_trailing_bits(bs_t* b)
{
/* rbsp_stop_one_bit */ bs_skip_u(b, 1);
while( !bs_byte_aligned(b) )
{
/* rbsp_alignment_zero_bit */ bs_skip_u(b, 1);
}
}