forked from videolan/vlc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhxxx_common.c
200 lines (173 loc) · 5.84 KB
/
hxxx_common.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
/*****************************************************************************
* hxxx_common.c: AVC/HEVC packetizers shared code
*****************************************************************************
* Copyright (C) 2001-2015 VLC authors and VideoLAN
*
* This program 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 program 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 program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <vlc_common.h>
#include <vlc_block.h>
#include <vlc_codec.h>
#include "hxxx_common.h"
#include "../codec/cc.h"
/****************************************************************************
* Closed captions handling
****************************************************************************/
struct cc_storage_t
{
uint32_t i_flags;
mtime_t i_dts;
mtime_t i_pts;
cc_data_t current;
cc_data_t next;
};
cc_storage_t * cc_storage_new( void )
{
cc_storage_t *p_ccs = malloc( sizeof(*p_ccs) );
if( likely(p_ccs) )
{
p_ccs->i_pts = VLC_TS_INVALID;
p_ccs->i_dts = VLC_TS_INVALID;
p_ccs->i_flags = 0;
cc_Init( &p_ccs->current );
cc_Init( &p_ccs->next );
}
return p_ccs;
}
void cc_storage_delete( cc_storage_t *p_ccs )
{
cc_Exit( &p_ccs->current );
cc_Exit( &p_ccs->next );
free( p_ccs );
}
void cc_storage_reset( cc_storage_t *p_ccs )
{
cc_Flush( &p_ccs->next );
}
void cc_storage_append( cc_storage_t *p_ccs, bool b_top_field_first,
const uint8_t *p_buf, size_t i_buf )
{
cc_Extract( &p_ccs->next, CC_PAYLOAD_GA94, b_top_field_first, p_buf, i_buf );
}
void cc_storage_commit( cc_storage_t *p_ccs, block_t *p_pic )
{
p_ccs->i_pts = p_pic->i_pts;
p_ccs->i_dts = p_pic->i_dts;
p_ccs->i_flags = p_pic->i_flags;
p_ccs->current = p_ccs->next;
cc_Flush( &p_ccs->next );
}
block_t * cc_storage_get_current( cc_storage_t *p_ccs, decoder_cc_desc_t *p_desc )
{
block_t *p_block;
if( !p_ccs->current.b_reorder && p_ccs->current.i_data <= 0 )
return NULL;
p_block = block_Alloc( p_ccs->current.i_data);
if( p_block )
{
memcpy( p_block->p_buffer, p_ccs->current.p_data, p_ccs->current.i_data );
p_block->i_dts =
p_block->i_pts = p_ccs->current.b_reorder ? p_ccs->i_pts : p_ccs->i_dts;
p_block->i_flags = p_ccs->i_flags & BLOCK_FLAG_TYPE_MASK;
p_desc->i_608_channels = p_ccs->current.i_608channels;
p_desc->i_708_channels = p_ccs->current.i_708channels;
p_desc->i_reorder_depth = p_ccs->current.b_reorder ? 4 : -1;
}
cc_Flush( &p_ccs->current );
return p_block;
}
/****************************************************************************
* PacketizeXXC1: Takes VCL blocks of data and creates annexe B type NAL stream
* Will always use 4 byte 0 0 0 1 startcodes
* Will prepend a SPS and PPS before each keyframe
****************************************************************************/
block_t *PacketizeXXC1( decoder_t *p_dec, uint8_t i_nal_length_size,
block_t **pp_block, pf_annexb_nal_packetizer pf_nal_parser )
{
block_t *p_block;
block_t *p_ret = NULL;
uint8_t *p;
if( !pp_block || !*pp_block )
return NULL;
if( (*pp_block)->i_flags&(BLOCK_FLAG_CORRUPTED) )
{
block_Release( *pp_block );
return NULL;
}
p_block = *pp_block;
*pp_block = NULL;
for( p = p_block->p_buffer; p < &p_block->p_buffer[p_block->i_buffer]; )
{
bool b_dummy;
int i_size = 0;
int i;
if( &p_block->p_buffer[p_block->i_buffer] - p < i_nal_length_size )
break;
for( i = 0; i < i_nal_length_size; i++ )
{
i_size = (i_size << 8) | (*p++);
}
if( i_size <= 0 ||
i_size > ( p_block->p_buffer + p_block->i_buffer - p ) )
{
msg_Err( p_dec, "Broken frame : size %d is too big", i_size );
break;
}
/* Convert AVC to AnnexB */
block_t *p_nal;
/* If data exactly match remaining bytes (1 NAL only or trailing one) */
if( i_size == p_block->p_buffer + p_block->i_buffer - p )
{
p_block->i_buffer = i_size;
p_block->p_buffer = p;
p_nal = block_Realloc( p_block, 4, i_size );
if( p_nal )
p_block = NULL;
}
else
{
p_nal = block_Alloc( 4 + i_size );
if( p_nal )
{
p_nal->i_dts = p_block->i_dts;
p_nal->i_pts = p_block->i_pts;
/* Copy nalu */
memcpy( &p_nal->p_buffer[4], p, i_size );
}
p += i_size;
}
if( !p_nal )
break;
/* Add start code */
p_nal->p_buffer[0] = 0x00;
p_nal->p_buffer[1] = 0x00;
p_nal->p_buffer[2] = 0x00;
p_nal->p_buffer[3] = 0x01;
/* Parse the NAL */
block_t *p_pic;
if( ( p_pic = pf_nal_parser( p_dec, &b_dummy, p_nal ) ) )
{
block_ChainAppend( &p_ret, p_pic );
}
if( !p_block )
break;
}
if( p_block )
block_Release( p_block );
return p_ret;
}