]> git.sesse.net Git - vlc/blob - modules/mux/mpeg/pes.c
fixed compilation on debian etch.
[vlc] / modules / mux / mpeg / pes.c
1 /*****************************************************************************
2  * pes.c: PES packetizer used by the MPEG multiplexers
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Eric Petit <titer@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <errno.h>
36 #include <fcntl.h>
37
38 #include <vlc_common.h>
39 #include <vlc_sout.h>
40 #include <vlc_block.h>
41
42 #ifdef HAVE_UNISTD_H
43 #   include <unistd.h>
44 #endif
45
46 #include <vlc_codecs.h>
47 #include "pes.h"
48 #include "bits.h"
49
50 static inline int PESHeader( uint8_t *p_hdr, mtime_t i_pts, mtime_t i_dts,
51                              int i_es_size, es_format_t *p_fmt,
52                              int i_stream_id, int i_private_id,
53                              bool b_mpeg2, bool b_data_alignment,
54                              int i_header_size )
55 {
56     bits_buffer_t bits;
57     int     i_extra = 0;
58
59     /* For PES_PRIVATE_STREAM_1 there is an extra header after the
60        pes header */
61     /* i_private_id != -1 because TS use 0xbd without private_id */
62     if( i_stream_id == PES_PRIVATE_STREAM_1 && i_private_id != -1 )
63     {
64         i_extra = 1;
65         if( ( i_private_id & 0xf0 ) == 0x80 )
66         {
67             i_extra += 3;
68         }
69     }
70
71     bits_initwrite( &bits, 50, p_hdr );
72
73     /* add start code */
74     bits_write( &bits, 24, 0x01 );
75     bits_write( &bits, 8, i_stream_id );
76     switch( i_stream_id )
77     {
78         case PES_PROGRAM_STREAM_MAP:
79         case PES_PADDING:
80         case PES_PRIVATE_STREAM_2:
81         case PES_ECM:
82         case PES_EMM:
83         case PES_PROGRAM_STREAM_DIRECTORY:
84         case PES_DSMCC_STREAM:
85         case PES_ITU_T_H222_1_TYPE_E_STREAM:
86             /* add pes data size  */
87             bits_write( &bits, 16, i_es_size );
88             bits_align( &bits );
89             return( bits.i_data );
90
91         default:
92             /* arg, a little more difficult */
93             if( b_mpeg2 )
94             {
95                 int i_pts_dts;
96
97                 if( i_pts > 0 && i_dts > 0 &&
98                     ( i_pts != i_dts || ( p_fmt->i_cat == VIDEO_ES &&
99                       p_fmt->i_codec != VLC_FOURCC('m','p','g','v') ) ) )
100                 {
101                     i_pts_dts = 0x03;
102                     if ( !i_header_size ) i_header_size = 0xa;
103                 }
104                 else if( i_pts > 0 )
105                 {
106                     i_pts_dts = 0x02;
107                     if ( !i_header_size ) i_header_size = 0x5;
108                 }
109                 else
110                 {
111                     i_pts_dts = 0x00;
112                     if ( !i_header_size ) i_header_size = 0x0;
113                 }
114
115                 bits_write( &bits, 16, i_es_size + i_extra + 3
116                              + i_header_size ); // size
117                 bits_write( &bits, 2, 0x02 ); // mpeg2 id
118                 bits_write( &bits, 2, 0x00 ); // pes scrambling control
119                 bits_write( &bits, 1, 0x00 ); // pes priority
120                 bits_write( &bits, 1, b_data_alignment ); // data alignement indicator
121                 bits_write( &bits, 1, 0x00 ); // copyright
122                 bits_write( &bits, 1, 0x00 ); // original or copy
123
124                 bits_write( &bits, 2, i_pts_dts ); // pts_dts flags
125                 bits_write( &bits, 1, 0x00 ); // escr flags
126                 bits_write( &bits, 1, 0x00 ); // es rate flag
127                 bits_write( &bits, 1, 0x00 ); // dsm trick mode flag
128                 bits_write( &bits, 1, 0x00 ); // additional copy info flag
129                 bits_write( &bits, 1, 0x00 ); // pes crc flag
130                 bits_write( &bits, 1, 0x00 ); // pes extension flags
131                 bits_write( &bits, 8, i_header_size ); // header size -> pts and dts
132
133                 /* write pts */
134                 if( i_pts_dts & 0x02 )
135                 {
136                     bits_write( &bits, 4, i_pts_dts ); // '0010' or '0011'
137                     bits_write( &bits, 3, i_pts >> 30 );
138                     bits_write( &bits, 1, 0x01 ); // marker
139                     bits_write( &bits, 15, i_pts >> 15 );
140                     bits_write( &bits, 1, 0x01 ); // marker
141                     bits_write( &bits, 15, i_pts );
142                     bits_write( &bits, 1, 0x01 ); // marker
143                     i_header_size -= 0x5;
144                 }
145                 /* write i_dts */
146                 if( i_pts_dts & 0x01 )
147                 {
148                     bits_write( &bits, 4, 0x01 ); // '0001'
149                     bits_write( &bits, 3, i_dts >> 30 );
150                     bits_write( &bits, 1, 0x01 ); // marker
151                     bits_write( &bits, 15, i_dts >> 15 );
152                     bits_write( &bits, 1, 0x01 ); // marker
153                     bits_write( &bits, 15, i_dts );
154                     bits_write( &bits, 1, 0x01 ); // marker
155                     i_header_size -= 0x5;
156                 }
157                 while ( i_header_size )
158                 {
159                     bits_write( &bits, 8, 0xff );
160                     i_header_size--;
161                 }
162             }
163             else /* MPEG1 */
164             {
165                 int i_pts_dts;
166
167                 if( i_pts > 0 && i_dts > 0 &&
168                     ( i_pts != i_dts || p_fmt->i_cat == VIDEO_ES ) )
169                 {
170                     bits_write( &bits, 16, i_es_size + i_extra + 10 /* + stuffing */ );
171                     i_pts_dts = 0x03;
172                 }
173                 else if( i_pts > 0 )
174                 {
175                     bits_write( &bits, 16, i_es_size + i_extra + 5 /* + stuffing */ );
176                     i_pts_dts = 0x02;
177                 }
178                 else
179                 {
180                     bits_write( &bits, 16, i_es_size + i_extra + 1 /* + stuffing */);
181                     i_pts_dts = 0x00;
182                 }
183
184                 /* FIXME: Now should be stuffing */
185
186                 /* No STD_buffer_scale and STD_buffer_size */
187
188                 /* write pts */
189                 if( i_pts_dts & 0x02 )
190                 {
191                     bits_write( &bits, 4, i_pts_dts ); // '0010' or '0011'
192                     bits_write( &bits, 3, i_pts >> 30 );
193                     bits_write( &bits, 1, 0x01 ); // marker
194                     bits_write( &bits, 15, i_pts >> 15 );
195                     bits_write( &bits, 1, 0x01 ); // marker
196                     bits_write( &bits, 15, i_pts );
197                     bits_write( &bits, 1, 0x01 ); // marker
198                 }
199                 /* write i_dts */
200                 if( i_pts_dts & 0x01 )
201                 {
202                     bits_write( &bits, 4, 0x01 ); // '0001'
203                     bits_write( &bits, 3, i_dts >> 30 );
204                     bits_write( &bits, 1, 0x01 ); // marker
205                     bits_write( &bits, 15, i_dts >> 15 );
206                     bits_write( &bits, 1, 0x01 ); // marker
207                     bits_write( &bits, 15, i_dts );
208                     bits_write( &bits, 1, 0x01 ); // marker
209                 }
210                 if( !i_pts_dts )
211                 {
212                     bits_write( &bits, 8, 0x0F );
213                 }
214
215             }
216
217             /* now should be stuffing */
218             /* and then pes data */
219
220             bits_align( &bits );
221             if( i_stream_id == PES_PRIVATE_STREAM_1 && i_private_id != -1 )
222             {
223                 bits_write( &bits, 8, i_private_id );
224                 if( ( i_private_id&0xf0 ) == 0x80 )
225                 {
226                     bits_write( &bits, 24, 0 ); // ac3
227                 }
228             }
229             bits_align( &bits );
230             return( bits.i_data );
231     }
232 }
233
234 int  EStoPES ( sout_instance_t *p_sout, block_t **pp_pes, block_t *p_es,
235                    es_format_t *p_fmt, int i_stream_id,
236                    int b_mpeg2, int b_data_alignment, int i_header_size,
237                    int i_max_pes_size )
238 {
239     block_t *p_pes;
240     mtime_t i_pts, i_dts, i_length;
241
242     uint8_t *p_data;
243     int     i_size;
244
245     int     i_private_id = -1;
246
247     uint8_t header[50];     // PES header + extra < 50 (more like 17)
248     int     i_pes_payload;
249     int     i_pes_header;
250
251     int     i_pes_count = 1;
252
253     /* HACK for private stream 1 in ps */
254     if( ( i_stream_id >> 8 ) == PES_PRIVATE_STREAM_1 )
255     {
256         i_private_id = i_stream_id & 0xff;
257         i_stream_id  = PES_PRIVATE_STREAM_1;
258     }
259
260     if( p_fmt->i_codec == VLC_FOURCC( 'm', 'p','4', 'v' ) &&
261         p_es->i_flags & BLOCK_FLAG_TYPE_I )
262     {
263         /* For MPEG4 video, add VOL before I-frames */
264         p_es = block_Realloc( p_es, p_fmt->i_extra, p_es->i_buffer );
265
266         memcpy( p_es->p_buffer, p_fmt->p_extra, p_fmt->i_extra );
267     }
268
269     i_pts = p_es->i_pts <= 0 ? 0 : p_es->i_pts * 9 / 100; // 90000 units clock
270     i_dts = p_es->i_dts <= 0 ? 0 : p_es->i_dts * 9 / 100; // 90000 units clock
271
272     i_size = p_es->i_buffer;
273     p_data = p_es->p_buffer;
274
275     *pp_pes = p_pes = NULL;
276
277 #ifndef NDEBUG
278     memset( header, 0, 50 );
279 #endif
280
281     do
282     {
283         i_pes_payload = __MIN( i_size, (i_max_pes_size ?
284                                i_max_pes_size : PES_PAYLOAD_SIZE_MAX) );
285         i_pes_header  = PESHeader( header, i_pts, i_dts, i_pes_payload,
286                                    p_fmt, i_stream_id, i_private_id, b_mpeg2,
287                                    b_data_alignment, i_header_size );
288         i_dts = 0; // only first PES has a dts/pts
289         i_pts = 0;
290
291         if( p_es )
292         {
293             p_es = block_Realloc( p_es, i_pes_header, p_es->i_buffer );
294             p_data = p_es->p_buffer+i_pes_header;
295             /* reuse p_es for first frame */
296             *pp_pes = p_pes = p_es;
297             /* don't touch i_dts, i_pts, i_length as are already set :) */
298             p_es = NULL;
299         }
300         else
301         {
302             p_pes->p_next = block_New( p_sout, i_pes_header + i_pes_payload );
303             p_pes = p_pes->p_next;
304
305             p_pes->i_dts    = 0;
306             p_pes->i_pts    = 0;
307             p_pes->i_length = 0;
308             if( i_pes_payload > 0 )
309             {
310                 vlc_memcpy( p_pes->p_buffer + i_pes_header, p_data,
311                             i_pes_payload );
312             }
313             i_pes_count++;
314         }
315
316         /* copy header */
317         memcpy( p_pes->p_buffer, header, i_pes_header );
318
319         i_size -= i_pes_payload;
320         p_data += i_pes_payload;
321         p_pes->i_buffer =  i_pes_header + i_pes_payload;
322
323     } while( i_size > 0 );
324
325     /* Now redate all pes */
326     i_dts    = (*pp_pes)->i_dts;
327     i_length = (*pp_pes)->i_length / i_pes_count;
328     for( p_pes = *pp_pes; p_pes != NULL; p_pes = p_pes->p_next )
329     {
330         p_pes->i_dts = i_dts;
331         p_pes->i_length = i_length;
332
333         i_dts += i_length;
334     }
335
336     return 0;
337 }