]> git.sesse.net Git - vlc/blob - modules/packetizer/packetizer_helper.h
mlp packetizer: use VLC_TS_INVALID
[vlc] / modules / packetizer / packetizer_helper.h
1 /*****************************************************************************
2  * packetizer.h: Packetizer helpers
3  *****************************************************************************
4  * Copyright (C) 2009 Laurent Aimar
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifndef _PACKETIZER_H
25 #define _PACKETIZER_H 1
26
27 #include <vlc_block.h>
28
29 enum
30 {
31     STATE_NOSYNC,
32     STATE_NEXT_SYNC
33 };
34
35 typedef void (*packetizer_reset_t)( void *p_private, bool b_broken );
36 typedef block_t *(*packetizer_parse_t)( void *p_private, bool *pb_ts_used, block_t * );
37 typedef int (*packetizer_validate_t)( void *p_private, block_t * );
38
39 typedef struct
40 {
41     int i_state;
42     block_bytestream_t bytestream;
43     size_t i_offset;
44     bool   b_flushing;
45
46     int i_startcode;
47     const uint8_t *p_startcode;
48
49     int i_au_prepend;
50     const uint8_t *p_au_prepend;
51
52     void *p_private;
53     packetizer_reset_t    pf_reset;
54     packetizer_parse_t    pf_parse;
55     packetizer_validate_t pf_validate;
56
57 } packetizer_t;
58
59 static inline void packetizer_Init( packetizer_t *p_pack,
60                                     const uint8_t *p_startcode, int i_startcode,
61                                     const uint8_t *p_au_prepend, int i_au_prepend,
62                                     packetizer_reset_t pf_reset,
63                                     packetizer_parse_t pf_parse,
64                                     packetizer_validate_t pf_validate,
65                                     void *p_private )
66 {
67     p_pack->i_state = STATE_NOSYNC;
68     p_pack->bytestream = block_BytestreamInit();
69     p_pack->i_offset = 0;
70     p_pack->b_flushing = false;
71
72     p_pack->i_au_prepend = i_au_prepend;
73     p_pack->p_au_prepend = p_au_prepend;
74
75     p_pack->i_startcode = i_startcode;
76     p_pack->p_startcode = p_startcode;
77     p_pack->pf_reset = pf_reset;
78     p_pack->pf_parse = pf_parse;
79     p_pack->pf_validate = pf_validate;
80     p_pack->p_private = p_private;
81 }
82
83 static inline void packetizer_Clean( packetizer_t *p_pack )
84 {
85     block_BytestreamRelease( &p_pack->bytestream );
86 }
87
88 static inline block_t *packetizer_Packetize( packetizer_t *p_pack, block_t **pp_block )
89 {
90     if( !pp_block || !*pp_block )
91         return NULL;
92
93     if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
94     {
95         const bool b_broken = ( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED ) != 0;
96         if( b_broken )
97         {
98             p_pack->i_state = STATE_NOSYNC;
99             block_BytestreamEmpty( &p_pack->bytestream );
100             p_pack->i_offset = 0;
101         }
102         p_pack->pf_reset( p_pack->p_private, b_broken );
103
104         block_Release( *pp_block );
105         return NULL;
106     }
107
108     block_BytestreamPush( &p_pack->bytestream, *pp_block );
109
110     for( ;; )
111     {
112         bool b_used_ts;
113         block_t *p_pic;
114
115         switch( p_pack->i_state )
116         {
117         case STATE_NOSYNC:
118             /* Find a startcode */
119             if( !block_FindStartcodeFromOffset( &p_pack->bytestream, &p_pack->i_offset,
120                                                 p_pack->p_startcode, p_pack->i_startcode ) )
121                 p_pack->i_state = STATE_NEXT_SYNC;
122
123             if( p_pack->i_offset )
124             {
125                 block_SkipBytes( &p_pack->bytestream, p_pack->i_offset );
126                 p_pack->i_offset = 0;
127                 block_BytestreamFlush( &p_pack->bytestream );
128             }
129
130             if( p_pack->i_state != STATE_NEXT_SYNC )
131                 return NULL; /* Need more data */
132
133             p_pack->i_offset = 1; /* To find next startcode */
134
135         case STATE_NEXT_SYNC:
136             /* Find the next startcode */
137             if( block_FindStartcodeFromOffset( &p_pack->bytestream, &p_pack->i_offset,
138                                                p_pack->p_startcode, p_pack->i_startcode ) )
139             {
140                 if( !p_pack->b_flushing || !p_pack->bytestream.p_chain )
141                     return NULL; /* Need more data */
142
143                 /* When flusing and we don't find a startcode, suppose that
144                  * the data extend up to the end */
145                 block_ChainProperties( p_pack->bytestream.p_chain,
146                                        NULL, &p_pack->i_offset, NULL );
147                 p_pack->i_offset -= p_pack->bytestream .i_offset;
148
149                 if( p_pack->i_offset <= (size_t)p_pack->i_startcode )
150                     return NULL;
151             }
152
153             block_BytestreamFlush( &p_pack->bytestream );
154
155             /* Get the new fragment and set the pts/dts */
156             block_t *p_block_bytestream = p_pack->bytestream.p_block;
157
158             p_pic = block_New( p_dec, p_pack->i_offset + p_pack->i_au_prepend );
159             p_pic->i_pts = p_block_bytestream->i_pts;
160             p_pic->i_dts = p_block_bytestream->i_dts;
161
162             block_GetBytes( &p_pack->bytestream, &p_pic->p_buffer[p_pack->i_au_prepend],
163                             p_pic->i_buffer - p_pack->i_au_prepend );
164             if( p_pack->i_au_prepend > 0 )
165                 memcpy( p_pic->p_buffer, p_pack->p_au_prepend, p_pack->i_au_prepend );
166
167             p_pack->i_offset = 0;
168
169             /* Parse the NAL */
170             p_pic = p_pack->pf_parse( p_pack->p_private, &b_used_ts, p_pic );
171             if( b_used_ts )
172             {
173                 p_block_bytestream->i_dts = -1;
174                 p_block_bytestream->i_pts = -1;
175             }
176
177             if( !p_pic )
178             {
179                 p_pack->i_state = STATE_NOSYNC;
180                 break;
181             }
182             if( p_pack->pf_validate( p_pack->p_private, p_pic ) )
183             {
184                 p_pack->i_state = STATE_NOSYNC;
185                 block_Release( p_pic );
186                 break;
187             }
188
189             /* So p_block doesn't get re-added several times */
190             *pp_block = block_BytestreamPop( &p_pack->bytestream );
191
192             p_pack->i_state = STATE_NOSYNC;
193
194             return p_pic;
195         }
196     }
197 }
198
199 static inline void packetizer_Header( packetizer_t *p_pack,
200                                       const uint8_t *p_header, int i_header )
201 {
202     block_t *p_init = block_Alloc( i_header );
203     if( !p_init )
204         return;
205
206     memcpy( p_init->p_buffer, p_header, i_header );
207
208     p_pack->b_flushing = true;
209
210     block_t *p_pic;
211     while( ( p_pic = packetizer_Packetize( p_pack, &p_init ) ) )
212         block_Release( p_pic ); /* Should not happen (only sequence header) */
213
214     p_pack->i_state = STATE_NOSYNC;
215     block_BytestreamEmpty( &p_pack->bytestream );
216     p_pack->i_offset = 0;
217     p_pack->b_flushing = false;
218 }
219
220 #endif
221