es: pass no cc reorder in es fmt
[vlc.git] / modules / packetizer / hxxx_common.c
blob745921966be0696f201218a3acba057ce2eb1b7a
1 /*****************************************************************************
2 * hxxx_common.c: AVC/HEVC packetizers shared code
3 *****************************************************************************
4 * Copyright (C) 2001-2015 VLC authors and VideoLAN
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
24 #include "hxxx_common.h"
26 #include <vlc_block.h>
27 #include <vlc_codec.h>
28 #include "../codec/cc.h"
30 /****************************************************************************
31 * Closed captions handling
32 ****************************************************************************/
33 struct cc_storage_t
35 uint32_t i_flags;
36 mtime_t i_dts;
37 mtime_t i_pts;
38 cc_data_t current;
39 cc_data_t next;
42 cc_storage_t * cc_storage_new( void )
44 cc_storage_t *p_ccs = malloc( sizeof(*p_ccs) );
45 if( likely(p_ccs) )
47 p_ccs->i_pts = VLC_TS_INVALID;
48 p_ccs->i_dts = VLC_TS_INVALID;
49 p_ccs->i_flags = 0;
50 cc_Init( &p_ccs->current );
51 cc_Init( &p_ccs->next );
53 return p_ccs;
56 void cc_storage_delete( cc_storage_t *p_ccs )
58 cc_Exit( &p_ccs->current );
59 cc_Exit( &p_ccs->next );
60 free( p_ccs );
63 void cc_storage_reset( cc_storage_t *p_ccs )
65 cc_Flush( &p_ccs->next );
68 void cc_storage_append( cc_storage_t *p_ccs, bool b_top_field_first,
69 const uint8_t *p_buf, size_t i_buf )
71 cc_Extract( &p_ccs->next, CC_PAYLOAD_GA94, b_top_field_first, p_buf, i_buf );
74 void cc_storage_commit( cc_storage_t *p_ccs, block_t *p_pic )
76 p_ccs->i_pts = p_pic->i_pts;
77 p_ccs->i_dts = p_pic->i_dts;
78 p_ccs->i_flags = p_pic->i_flags;
79 p_ccs->current = p_ccs->next;
80 cc_Flush( &p_ccs->next );
83 block_t * cc_storage_get_current( cc_storage_t *p_ccs, bool pb_present[4],
84 int *pi_reorder_depth )
86 block_t *p_block;
88 *pi_reorder_depth = p_ccs->current.b_reorder ? 4 : -1;
90 for( int i = 0; i < 4; i++ )
91 pb_present[i] = p_ccs->current.pb_present[i];
93 if( p_ccs->current.i_data <= 0 )
94 return NULL;
96 p_block = block_Alloc( p_ccs->current.i_data);
97 if( p_block )
99 memcpy( p_block->p_buffer, p_ccs->current.p_data, p_ccs->current.i_data );
100 p_block->i_dts =
101 p_block->i_pts = p_ccs->current.b_reorder ? p_ccs->i_pts : p_ccs->i_dts;
102 p_block->i_flags = p_ccs->i_flags & BLOCK_FLAG_TYPE_MASK;
104 cc_Flush( &p_ccs->current );
106 return p_block;
109 /****************************************************************************
110 * PacketizeXXC1: Takes VCL blocks of data and creates annexe B type NAL stream
111 * Will always use 4 byte 0 0 0 1 startcodes
112 * Will prepend a SPS and PPS before each keyframe
113 ****************************************************************************/
114 block_t *PacketizeXXC1( decoder_t *p_dec, uint8_t i_nal_length_size,
115 block_t **pp_block, pf_annexb_nal_packetizer pf_nal_parser )
117 block_t *p_block;
118 block_t *p_ret = NULL;
119 uint8_t *p;
121 if( !pp_block || !*pp_block )
122 return NULL;
123 if( (*pp_block)->i_flags&(BLOCK_FLAG_CORRUPTED) )
125 block_Release( *pp_block );
126 return NULL;
129 p_block = *pp_block;
130 *pp_block = NULL;
132 for( p = p_block->p_buffer; p < &p_block->p_buffer[p_block->i_buffer]; )
134 bool b_dummy;
135 int i_size = 0;
136 int i;
138 if( &p_block->p_buffer[p_block->i_buffer] - p < i_nal_length_size )
139 break;
141 for( i = 0; i < i_nal_length_size; i++ )
143 i_size = (i_size << 8) | (*p++);
146 if( i_size <= 0 ||
147 i_size > ( p_block->p_buffer + p_block->i_buffer - p ) )
149 msg_Err( p_dec, "Broken frame : size %d is too big", i_size );
150 break;
153 /* Convert AVC to AnnexB */
154 block_t *p_nal;
155 /* If data exactly match remaining bytes (1 NAL only or trailing one) */
156 if( i_size == p_block->p_buffer + p_block->i_buffer - p )
158 p_block->i_buffer = i_size;
159 p_block->p_buffer = p;
160 p_nal = block_Realloc( p_block, 4, i_size );
161 if( p_nal )
162 p_block = NULL;
164 else
166 p_nal = block_Alloc( 4 + i_size );
167 if( p_nal )
169 p_nal->i_dts = p_block->i_dts;
170 p_nal->i_pts = p_block->i_pts;
171 /* Copy nalu */
172 memcpy( &p_nal->p_buffer[4], p, i_size );
174 p += i_size;
177 if( !p_nal )
178 break;
180 /* Add start code */
181 p_nal->p_buffer[0] = 0x00;
182 p_nal->p_buffer[1] = 0x00;
183 p_nal->p_buffer[2] = 0x00;
184 p_nal->p_buffer[3] = 0x01;
186 /* Parse the NAL */
187 block_t *p_pic;
188 if( ( p_pic = pf_nal_parser( p_dec, &b_dummy, p_nal ) ) )
190 block_ChainAppend( &p_ret, p_pic );
193 if( !p_block )
194 break;
197 if( p_block )
198 block_Release( p_block );
200 return p_ret;