]> git.sesse.net Git - vlc/blob - modules/mux/mpeg/pes.c
* modules/mux/mpeg/pes.c: allow cases where we don't write a pts.
[vlc] / modules / mux / mpeg / pes.c
1 /*****************************************************************************
2  * pes.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: pes.c,v 1.6 2003/06/09 07:16:41 gbazin Exp $
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, int i_es_size, int i_stream_id, int i_private_id )
50 {
51     bits_buffer_t bits;
52
53     bits_initwrite( &bits, 30, p_hdr );
54
55     /* add start code */
56     bits_write( &bits, 24, 0x01 );
57     bits_write( &bits, 8, i_stream_id );
58     switch( i_stream_id )
59     {
60         case PES_PROGRAM_STREAM_MAP:
61         case PES_PADDING:
62         case PES_PRIVATE_STREAM_2:
63         case PES_ECM:
64         case PES_EMM:
65         case PES_PROGRAM_STREAM_DIRECTORY:
66         case PES_DSMCC_STREAM:
67         case PES_ITU_T_H222_1_TYPE_E_STREAM:
68             /* add pes data size  */
69             bits_write( &bits, 16, i_es_size );
70             bits_align( &bits );
71             return( bits.i_data );
72
73         default:
74             /* arg, a little more difficult */
75 //            if( b_mpeg2 ) // FIXME unsupported
76             {
77                 int     i_pts_dts;
78                 int     i_extra = 0;
79
80                 /* For PES_PRIVATE_STREAM_1 there is an extra header after the
81                    pes header */
82                 if( i_stream_id == PES_PRIVATE_STREAM_1 )
83                 {
84                     i_extra = 1;
85                     if( ( i_private_id&0xf0 ) == 0x80 )
86                     {
87                         i_extra += 3;
88                     }
89                 }
90
91                 if( i_pts >= 0 && i_dts >= 0 )
92                 {
93                     bits_write( &bits, 16, i_es_size + i_extra+ 13 );
94                     i_pts_dts = 0x03;
95                 }
96                 else if( i_pts >= 0 )
97                 {
98                     bits_write( &bits, 16, i_es_size  + i_extra + 8 );
99                     i_pts_dts = 0x02;
100                 }
101                 else
102                 {
103                     bits_write( &bits, 16, i_es_size  + i_extra + 3 );
104                     i_pts_dts = 0x00;
105                 }
106
107                 bits_write( &bits, 2, 0x02 ); // mpeg2 id
108                 bits_write( &bits, 2, 0x00 ); // pes scrambling control
109                 bits_write( &bits, 1, 0x00 ); // pes priority
110                 bits_write( &bits, 1, 0x00 ); // data alignement indicator
111                 bits_write( &bits, 1, 0x00 ); // copyright
112                 bits_write( &bits, 1, 0x00 ); // original or copy
113
114                 bits_write( &bits, 2, i_pts_dts ); // pts_dts flags
115                 bits_write( &bits, 1, 0x00 ); // escr flags
116                 bits_write( &bits, 1, 0x00 ); // es rate flag
117                 bits_write( &bits, 1, 0x00 ); // dsm trick mode flag
118                 bits_write( &bits, 1, 0x00 ); // additional copy info flag
119                 bits_write( &bits, 1, 0x00 ); // pes crc flag
120                 bits_write( &bits, 1, 0x00 ); // pes extention flags
121                 if( i_pts_dts == 0x03 )
122                 {
123                     bits_write( &bits, 8, 0x0a ); // header size -> pts and dts
124                 }
125                 else if( i_pts_dts == 0x02 )
126                 {
127                     bits_write( &bits, 8, 0x05 ); // header size -> pts
128                 }
129                 else
130                 {
131                     bits_write( &bits, 8, 0x00 ); // header size -> 0
132                 }
133
134                 /* write pts */
135                 if( i_pts_dts & 0x02 )
136                 {
137                     bits_write( &bits, 4, i_pts_dts ); // '0010' or '0011'
138                     bits_write( &bits, 3, i_pts >> 30 );
139                     bits_write( &bits, 1, 0x01 ); // marker
140                     bits_write( &bits, 15, i_pts >> 15 );
141                     bits_write( &bits, 1, 0x01 ); // marker
142                     bits_write( &bits, 15, i_pts );
143                     bits_write( &bits, 1, 0x01 ); // marker
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                 }
156             }
157             /* now should be stuffing */
158             /* and then pes data */
159
160             bits_align( &bits );
161             if( i_stream_id == PES_PRIVATE_STREAM_1 && i_private_id != -1 )
162             {
163                 bits_write( &bits, 8, i_private_id );
164                 if( ( i_private_id&0xf0 ) == 0x80 )
165                 {
166                     bits_write( &bits, 24, 0 ); // ac3
167                 }
168             }
169             bits_align( &bits );
170             return( bits.i_data );
171     }
172 }
173
174 int E_( EStoPES )( sout_instance_t *p_sout,
175                    sout_buffer_t **pp_pes,
176                    sout_buffer_t *p_es,
177                    int i_stream_id,
178                    int b_mpeg2 )
179 {
180     sout_buffer_t *p_es_sav, *p_pes;
181     mtime_t i_pts, i_dts;
182
183     uint8_t *p_data;
184     int     i_size;
185
186     int     i_private_id = -1;
187
188     uint8_t header[30];     // PES header + extra < 30 (more like 17)
189     int     i_pes_payload;
190     int     i_pes_header;
191
192     /* HACK for private stream 1 in ps */
193     if( ( i_stream_id >> 8 ) == PES_PRIVATE_STREAM_1 )
194     {
195         i_private_id = i_stream_id & 0xff;
196         i_stream_id  = PES_PRIVATE_STREAM_1;
197     }
198
199     i_pts = p_es->i_pts * 9 / 100; // 90000 units clock
200     i_dts = p_es->i_dts * 9 / 100; // 90000 units clock
201
202     i_size = p_es->i_size;
203     p_data = p_es->p_buffer;
204
205     *pp_pes = p_pes = NULL;
206     p_es_sav = p_es;
207
208     do
209     {
210         i_pes_payload = __MIN( i_size, PES_PAYLOAD_SIZE_MAX );
211         i_pes_header  = PESHeader( header, i_pts, i_dts, i_pes_payload, i_stream_id, i_private_id );
212         i_dts = -1; // only first PES has a dts
213
214         if( p_es  )
215         {
216             if( sout_BufferReallocFromPreHeader( p_sout, p_es, i_pes_header ) )
217             {
218                 msg_Err( p_sout, "cannot realloc prehader (should never happen)" );
219                 return( -1 );
220             }
221             /* reuse p_es for first frame */
222             *pp_pes = p_pes = p_es;
223             /* don't touch i_dts, i_pts, i_length as are already set :) */
224             p_es = NULL;
225         }
226         else
227         {
228             p_pes->p_next = sout_BufferNew( p_sout, i_pes_header + i_pes_payload );
229             p_pes = p_pes->p_next;
230
231             p_pes->i_dts    = 0;
232             p_pes->i_pts    = 0;
233             p_pes->i_length = 0;
234             if( i_pes_payload > 0 )
235             {
236                 p_sout->p_vlc->pf_memcpy( p_pes->p_buffer + i_pes_header, p_data, i_pes_payload );
237             }
238         }
239
240         /* copy header */
241         memcpy( p_pes->p_buffer, header, i_pes_header );
242
243         i_size -= i_pes_payload;
244         p_data += i_pes_payload;
245         p_pes->i_size =  i_pes_header + i_pes_payload;
246
247     } while( i_size > 0 );
248
249     /* sav some space */
250     if( p_es_sav->i_size + 10*1024 < p_es_sav->i_buffer_size )
251     {
252         sout_BufferRealloc( p_sout, p_es_sav, p_es_sav->i_size );
253     }
254
255     return( 0 );
256 }
257
258
259
260