]> git.sesse.net Git - vlc/blob - modules/mux/mpeg/pes.c
875132ee512aa1e48625dfe77268d56eb21a2a78
[vlc] / modules / mux / mpeg / pes.c
1 /*****************************************************************************
2  * pes.c: PES packetizer used by the MPEG multiplexers
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <fcntl.h>
34
35 #include <vlc/vlc.h>
36 #include <vlc/input.h>
37 #include <vlc/sout.h>
38
39 #ifdef HAVE_UNISTD_H
40 #   include <unistd.h>
41 #endif
42
43 #include "codecs.h"
44 #include "pes.h"
45 #include "bits.h"
46
47 #define PES_PAYLOAD_SIZE_MAX 65500
48
49 static inline int PESHeader( uint8_t *p_hdr, mtime_t i_pts, mtime_t i_dts,
50                              int i_es_size, int i_stream_id, int i_private_id,
51                              vlc_bool_t b_mpeg2, vlc_bool_t b_data_alignment,
52                              int i_header_size )
53 {
54     bits_buffer_t bits;
55     int     i_extra = 0;
56
57     /* For PES_PRIVATE_STREAM_1 there is an extra header after the
58        pes header */
59     /* i_private_id != -1 because TS use 0xbd without private_id */
60     if( i_stream_id == PES_PRIVATE_STREAM_1 && i_private_id != -1 )
61     {
62         i_extra = 1;
63         if( ( i_private_id & 0xf0 ) == 0x80 )
64         {
65             i_extra += 3;
66         }
67     }
68
69
70     bits_initwrite( &bits, 50, p_hdr );
71
72     /* add start code */
73     bits_write( &bits, 24, 0x01 );
74     bits_write( &bits, 8, i_stream_id );
75     switch( i_stream_id )
76     {
77         case PES_PROGRAM_STREAM_MAP:
78         case PES_PADDING:
79         case PES_PRIVATE_STREAM_2:
80         case PES_ECM:
81         case PES_EMM:
82         case PES_PROGRAM_STREAM_DIRECTORY:
83         case PES_DSMCC_STREAM:
84         case PES_ITU_T_H222_1_TYPE_E_STREAM:
85             /* add pes data size  */
86             bits_write( &bits, 16, i_es_size );
87             bits_align( &bits );
88             return( bits.i_data );
89
90         default:
91             /* arg, a little more difficult */
92             if( b_mpeg2 )
93             {
94                 int i_pts_dts;
95
96                 if( i_pts > 0 && i_dts > 0 && i_pts != i_dts )
97                 {
98                     i_pts_dts = 0x03;
99                     if ( !i_header_size ) i_header_size = 0xa;
100                 }
101                 else if( i_pts > 0 )
102                 {
103                     i_pts_dts = 0x02;
104                     if ( !i_header_size ) i_header_size = 0x5;
105                 }
106                 else
107                 {
108                     i_pts_dts = 0x00;
109                     if ( !i_header_size ) i_header_size = 0x0;
110                 }
111
112                 bits_write( &bits, 16, i_es_size + i_extra + 3
113                              + i_header_size ); // size
114                 bits_write( &bits, 2, 0x02 ); // mpeg2 id
115                 bits_write( &bits, 2, 0x00 ); // pes scrambling control
116                 bits_write( &bits, 1, 0x00 ); // pes priority
117                 bits_write( &bits, 1, b_data_alignment ); // data alignement indicator
118                 bits_write( &bits, 1, 0x00 ); // copyright
119                 bits_write( &bits, 1, 0x00 ); // original or copy
120
121                 bits_write( &bits, 2, i_pts_dts ); // pts_dts flags
122                 bits_write( &bits, 1, 0x00 ); // escr flags
123                 bits_write( &bits, 1, 0x00 ); // es rate flag
124                 bits_write( &bits, 1, 0x00 ); // dsm trick mode flag
125                 bits_write( &bits, 1, 0x00 ); // additional copy info flag
126                 bits_write( &bits, 1, 0x00 ); // pes crc flag
127                 bits_write( &bits, 1, 0x00 ); // pes extention flags
128                 bits_write( &bits, 8, i_header_size ); // header size -> pts and dts
129
130                 /* write pts */
131                 if( i_pts_dts & 0x02 )
132                 {
133                     bits_write( &bits, 4, i_pts_dts ); // '0010' or '0011'
134                     bits_write( &bits, 3, i_pts >> 30 );
135                     bits_write( &bits, 1, 0x01 ); // marker
136                     bits_write( &bits, 15, i_pts >> 15 );
137                     bits_write( &bits, 1, 0x01 ); // marker
138                     bits_write( &bits, 15, i_pts );
139                     bits_write( &bits, 1, 0x01 ); // marker
140                     i_header_size -= 0x5;
141                 }
142                 /* write i_dts */
143                 if( i_pts_dts & 0x01 )
144                 {
145                     bits_write( &bits, 4, 0x01 ); // '0001'
146                     bits_write( &bits, 3, i_dts >> 30 );
147                     bits_write( &bits, 1, 0x01 ); // marker
148                     bits_write( &bits, 15, i_dts >> 15 );
149                     bits_write( &bits, 1, 0x01 ); // marker
150                     bits_write( &bits, 15, i_dts );
151                     bits_write( &bits, 1, 0x01 ); // marker
152                     i_header_size -= 0x5;
153                 }
154                 while ( i_header_size )
155                 {
156                     bits_write( &bits, 8, 0xff );
157                     i_header_size--;
158                 }
159             }
160             else /* MPEG1 */
161             {
162                 int i_pts_dts;
163
164                 if( i_pts > 0 && i_dts > 0 && i_pts != i_dts )
165                 {
166                     bits_write( &bits, 16, i_es_size + i_extra + 10 /* + stuffing */ );
167                     i_pts_dts = 0x03;
168                 }
169                 else if( i_pts > 0 )
170                 {
171                     bits_write( &bits, 16, i_es_size + i_extra + 5 /* + stuffing */ );
172                     i_pts_dts = 0x02;
173                 }
174                 else
175                 {
176                     bits_write( &bits, 16, i_es_size + i_extra + 1 /* + stuffing */);
177                     i_pts_dts = 0x00;
178                 }
179
180                 /* FIXME: Now should be stuffing */
181
182                 /* No STD_buffer_scale and STD_buffer_size */
183
184                 /* write pts */
185                 if( i_pts_dts & 0x02 )
186                 {
187                     bits_write( &bits, 4, i_pts_dts ); // '0010' or '0011'
188                     bits_write( &bits, 3, i_pts >> 30 );
189                     bits_write( &bits, 1, 0x01 ); // marker
190                     bits_write( &bits, 15, i_pts >> 15 );
191                     bits_write( &bits, 1, 0x01 ); // marker
192                     bits_write( &bits, 15, i_pts );
193                     bits_write( &bits, 1, 0x01 ); // marker
194                 }
195                 /* write i_dts */
196                 if( i_pts_dts & 0x01 )
197                 {
198                     bits_write( &bits, 4, 0x01 ); // '0001'
199                     bits_write( &bits, 3, i_dts >> 30 );
200                     bits_write( &bits, 1, 0x01 ); // marker
201                     bits_write( &bits, 15, i_dts >> 15 );
202                     bits_write( &bits, 1, 0x01 ); // marker
203                     bits_write( &bits, 15, i_dts );
204                     bits_write( &bits, 1, 0x01 ); // marker
205                 }
206                 if( !i_pts_dts )
207                 {
208                     bits_write( &bits, 8, 0x0F );
209                 }
210
211             }
212
213             /* now should be stuffing */
214             /* and then pes data */
215
216             bits_align( &bits );
217             if( i_stream_id == PES_PRIVATE_STREAM_1 && i_private_id != -1 )
218             {
219                 bits_write( &bits, 8, i_private_id );
220                 if( ( i_private_id&0xf0 ) == 0x80 )
221                 {
222                     bits_write( &bits, 24, 0 ); // ac3
223                 }
224             }
225             bits_align( &bits );
226             return( bits.i_data );
227     }
228 }
229
230 int E_( EStoPES )( sout_instance_t *p_sout,
231                    block_t **pp_pes,
232                    block_t *p_es,
233                    int i_stream_id,
234                    int b_mpeg2, int b_data_alignment, int i_header_size )
235 {
236     block_t *p_pes;
237     mtime_t i_pts, i_dts, i_length;
238
239     uint8_t *p_data;
240     int     i_size;
241
242     int     i_private_id = -1;
243
244     uint8_t header[50];     // PES header + extra < 50 (more like 17)
245     int     i_pes_payload;
246     int     i_pes_header;
247
248     int     i_pes_count = 1;
249
250     /* HACK for private stream 1 in ps */
251     if( ( i_stream_id >> 8 ) == PES_PRIVATE_STREAM_1 )
252     {
253         i_private_id = i_stream_id & 0xff;
254         i_stream_id  = PES_PRIVATE_STREAM_1;
255     }
256
257     i_pts = p_es->i_pts <= 0 ? 0 : p_es->i_pts * 9 / 100; // 90000 units clock
258     i_dts = p_es->i_dts <= 0 ? 0 : p_es->i_dts * 9 / 100; // 90000 units clock
259
260     i_size = p_es->i_buffer;
261     p_data = p_es->p_buffer;
262
263     *pp_pes = p_pes = NULL;
264
265     do
266     {
267         i_pes_payload = __MIN( i_size, PES_PAYLOAD_SIZE_MAX );
268         i_pes_header  = PESHeader( header, i_pts, i_dts, i_pes_payload,
269                                    i_stream_id, i_private_id, b_mpeg2,
270                                    b_data_alignment, i_header_size );
271         i_dts = 0; // only first PES has a dts/pts
272         i_pts = 0;
273
274         if( p_es )
275         {
276             p_es = block_Realloc( p_es, i_pes_header, p_es->i_buffer );
277             /* reuse p_es for first frame */
278             *pp_pes = p_pes = p_es;
279             /* don't touch i_dts, i_pts, i_length as are already set :) */
280             p_es = NULL;
281         }
282         else
283         {
284             p_pes->p_next = block_New( p_sout, i_pes_header + i_pes_payload );
285             p_pes = p_pes->p_next;
286
287             p_pes->i_dts    = 0;
288             p_pes->i_pts    = 0;
289             p_pes->i_length = 0;
290             if( i_pes_payload > 0 )
291             {
292                 p_sout->p_vlc->pf_memcpy( p_pes->p_buffer + i_pes_header,
293                                           p_data, i_pes_payload );
294             }
295             i_pes_count++;
296         }
297
298         /* copy header */
299         memcpy( p_pes->p_buffer, header, i_pes_header );
300
301         i_size -= i_pes_payload;
302         p_data += i_pes_payload;
303         p_pes->i_buffer =  i_pes_header + i_pes_payload;
304
305     } while( i_size > 0 );
306
307     /* Now redate all pes */
308     i_dts    = (*pp_pes)->i_dts;
309     i_length = (*pp_pes)->i_length / i_pes_count;
310     for( p_pes = *pp_pes; p_pes != NULL; p_pes = p_pes->p_next )
311     {
312         p_pes->i_dts = i_dts;
313         p_pes->i_length = i_length;
314
315         i_dts += i_length;
316     }
317     return( 0 );
318 }
319
320