dlna: add PrepareForConnection action
[vlc.git] / modules / packetizer / hxxx_nal.h
blob81ef497f492981dbdbec3c9741976de86f825874
1 /*****************************************************************************
2 * hxxx_nal.h: Common helpers for AVC/HEVC NALU
3 *****************************************************************************
4 * Copyright (C) 2014-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 #ifndef HXXX_NAL_H
21 #define HXXX_NAL_H
23 #include <vlc_common.h>
24 #include <vlc_es.h>
25 #include "startcode_helper.h"
27 static const uint8_t annexb_startcode4[] = { 0x00, 0x00, 0x00, 0x01 };
28 #define annexb_startcode3 (&annexb_startcode4[1])
30 /* strips any AnnexB startcode [0] 0 0 1 */
31 static inline bool hxxx_strip_AnnexB_startcode( const uint8_t **pp_data, size_t *pi_data )
33 unsigned bitflow = 0;
34 const uint8_t *p_data = *pp_data;
35 size_t i_data = *pi_data;
37 while( i_data && p_data[0] <= 1 )
39 bitflow = (bitflow << 1) | (!p_data[0]);
40 p_data++;
41 i_data--;
42 if( !(bitflow & 0x01) )
44 if( (bitflow & 0x06) == 0x06 ) /* there was at least 2 leading zeros */
46 *pi_data = i_data;
47 *pp_data = p_data;
48 return true;
50 return false;
53 return false;
56 /* Declarations */
58 typedef struct
60 const uint8_t *p_head;
61 const uint8_t *p_tail;
62 uint8_t i_nal_length_size;
63 } hxxx_iterator_ctx_t;
65 static inline void hxxx_iterator_init( hxxx_iterator_ctx_t *p_ctx, const uint8_t *p_data, size_t i_data,
66 uint8_t i_nal_length_size )
68 p_ctx->p_head = p_data;
69 p_ctx->p_tail = p_data + i_data;
70 if( vlc_popcount(i_nal_length_size) == 1 && i_nal_length_size <= 4 )
71 p_ctx->i_nal_length_size = i_nal_length_size;
72 else
73 p_ctx->i_nal_length_size = 0;
76 static inline bool hxxx_iterate_next( hxxx_iterator_ctx_t *p_ctx, const uint8_t **pp_start, size_t *pi_size )
78 if( p_ctx->i_nal_length_size == 0 )
79 return false;
81 if( p_ctx->p_tail - p_ctx->p_head < p_ctx->i_nal_length_size )
82 return false;
84 uint32_t i_nal_size = 0;
85 for( uint8_t i=0; i < p_ctx->i_nal_length_size ; i++ )
86 i_nal_size = (i_nal_size << 8) | *p_ctx->p_head++;
88 if( i_nal_size > p_ctx->p_tail - p_ctx->p_head )
89 return false;
91 *pp_start = p_ctx->p_head;
92 *pi_size = i_nal_size;
93 p_ctx->p_head += i_nal_size;
95 return true;
98 static inline bool hxxx_annexb_iterate_next( hxxx_iterator_ctx_t *p_ctx, const uint8_t **pp_start, size_t *pi_size )
100 if( !p_ctx->p_head )
101 return false;
103 p_ctx->p_head = startcode_FindAnnexB( p_ctx->p_head, p_ctx->p_tail );
104 if( !p_ctx->p_head )
105 return false;
107 const uint8_t *p_end = startcode_FindAnnexB( p_ctx->p_head + 3, p_ctx->p_tail );
108 if( !p_end )
109 p_end = p_ctx->p_tail;
111 /* fix 3 to 4 startcode offset and strip any trailing zeros */
112 while( p_end > p_ctx->p_head && p_end[-1] == 0 )
113 p_end--;
115 *pp_start = p_ctx->p_head;
116 *pi_size = p_end - p_ctx->p_head;
117 p_ctx->p_head = p_end;
119 return hxxx_strip_AnnexB_startcode( pp_start, pi_size );
122 /* Takes any AnnexB NAL buffer and converts it to prefixed size (AVC/HEVC) */
123 block_t *hxxx_AnnexB_to_xVC( block_t *p_block, uint8_t i_nal_length_size );
125 #endif // HXXX_NAL_H