]> git.sesse.net Git - vlc/blob - modules/mux/mpeg/pes.c
* ps: clean up and dvd subtitles support.
[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: pes.c,v 1.9 2003/08/02 01:33:53 fenrir 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,
50                              int i_es_size, int i_stream_id, int i_private_id,
51                              vlc_bool_t b_mpeg2 )
52 {
53     bits_buffer_t bits;
54
55     bits_initwrite( &bits, 30, p_hdr );
56
57     /* add start code */
58     bits_write( &bits, 24, 0x01 );
59     bits_write( &bits, 8, i_stream_id );
60     switch( i_stream_id )
61     {
62         case PES_PROGRAM_STREAM_MAP:
63         case PES_PADDING:
64         case PES_PRIVATE_STREAM_2:
65         case PES_ECM:
66         case PES_EMM:
67         case PES_PROGRAM_STREAM_DIRECTORY:
68         case PES_DSMCC_STREAM:
69         case PES_ITU_T_H222_1_TYPE_E_STREAM:
70             /* add pes data size  */
71             bits_write( &bits, 16, i_es_size );
72             bits_align( &bits );
73             return( bits.i_data );
74
75         default:
76             /* arg, a little more difficult */
77             if( b_mpeg2 )
78             {
79                 int     i_pts_dts;
80                 int     i_extra = 0;
81
82                 /* For PES_PRIVATE_STREAM_1 there is an extra header after the
83                    pes header */
84                 if( i_stream_id == PES_PRIVATE_STREAM_1 )
85                 {
86                     i_extra = 1;
87                     if( ( i_private_id&0xf8 ) == 0x80 )
88                     {
89                         i_extra += 3;
90                     }
91                 }
92
93                 if( i_pts >= 0 && i_dts >= 0 )
94                 {
95                     bits_write( &bits, 16, i_es_size + i_extra+ 13 );
96                     i_pts_dts = 0x03;
97                 }
98                 else if( i_pts >= 0 )
99                 {
100                     bits_write( &bits, 16, i_es_size  + i_extra + 8 );
101                     i_pts_dts = 0x02;
102                 }
103                 else
104                 {
105                     bits_write( &bits, 16, i_es_size  + i_extra + 3 );
106                     i_pts_dts = 0x00;
107                 }
108
109                 bits_write( &bits, 2, 0x02 ); // mpeg2 id
110                 bits_write( &bits, 2, 0x00 ); // pes scrambling control
111                 bits_write( &bits, 1, 0x00 ); // pes priority
112                 bits_write( &bits, 1, 0x00 ); // data alignement indicator
113                 bits_write( &bits, 1, 0x00 ); // copyright
114                 bits_write( &bits, 1, 0x00 ); // original or copy
115
116                 bits_write( &bits, 2, i_pts_dts ); // pts_dts flags
117                 bits_write( &bits, 1, 0x00 ); // escr flags
118                 bits_write( &bits, 1, 0x00 ); // es rate flag
119                 bits_write( &bits, 1, 0x00 ); // dsm trick mode flag
120                 bits_write( &bits, 1, 0x00 ); // additional copy info flag
121                 bits_write( &bits, 1, 0x00 ); // pes crc flag
122                 bits_write( &bits, 1, 0x00 ); // pes extention flags
123                 if( i_pts_dts == 0x03 )
124                 {
125                     bits_write( &bits, 8, 0x0a ); // header size -> pts and dts
126                 }
127                 else if( i_pts_dts == 0x02 )
128                 {
129                     bits_write( &bits, 8, 0x05 ); // header size -> pts
130                 }
131                 else
132                 {
133                     bits_write( &bits, 8, 0x00 ); // header size -> 0
134                 }
135
136                 /* write pts */
137                 if( i_pts_dts & 0x02 )
138                 {
139                     bits_write( &bits, 4, i_pts_dts ); // '0010' or '0011'
140                     bits_write( &bits, 3, i_pts >> 30 );
141                     bits_write( &bits, 1, 0x01 ); // marker
142                     bits_write( &bits, 15, i_pts >> 15 );
143                     bits_write( &bits, 1, 0x01 ); // marker
144                     bits_write( &bits, 15, i_pts );
145                     bits_write( &bits, 1, 0x01 ); // marker
146                 }
147                 /* write i_dts */
148                 if( i_pts_dts & 0x01 )
149                 {
150                     bits_write( &bits, 4, 0x01 ); // '0001'
151                     bits_write( &bits, 3, i_dts >> 30 );
152                     bits_write( &bits, 1, 0x01 ); // marker
153                     bits_write( &bits, 15, i_dts >> 15 );
154                     bits_write( &bits, 1, 0x01 ); // marker
155                     bits_write( &bits, 15, i_dts );
156                     bits_write( &bits, 1, 0x01 ); // marker
157                 }
158             }
159             else /* MPEG1 */
160             {
161                 int i_pts_dts;
162
163                 if( i_pts >= 0 && i_dts >= 0 )
164                 {
165                     bits_write( &bits, 16, i_es_size + 10 /* + stuffing */ );
166                     i_pts_dts = 0x03;
167                 }
168                 else if( i_pts >= 0 )
169                 {
170                      bits_write( &bits, 16, i_es_size + 5 /* + stuffing */ );
171                     i_pts_dts = 0x02;
172                 }
173                 else
174                 {
175                     bits_write( &bits, 16, i_es_size + 1 /* + stuffing */);
176                     i_pts_dts = 0x00;
177                 }
178
179                 /* FIXME: Now should be stuffing */
180
181                 /* No STD_buffer_scale and STD_buffer_size */
182
183                 /* write pts */
184                 if( i_pts_dts & 0x02 )
185                 {
186                     bits_write( &bits, 4, i_pts_dts ); // '0010' or '0011'
187                     bits_write( &bits, 3, i_pts >> 30 );
188                     bits_write( &bits, 1, 0x01 ); // marker
189                     bits_write( &bits, 15, i_pts >> 15 );
190                     bits_write( &bits, 1, 0x01 ); // marker
191                     bits_write( &bits, 15, i_pts );
192                     bits_write( &bits, 1, 0x01 ); // marker
193                 }
194                 /* write i_dts */
195                 if( i_pts_dts & 0x01 )
196                 {
197                     bits_write( &bits, 4, 0x01 ); // '0001'
198                     bits_write( &bits, 3, i_dts >> 30 );
199                     bits_write( &bits, 1, 0x01 ); // marker
200                     bits_write( &bits, 15, i_dts >> 15 );
201                     bits_write( &bits, 1, 0x01 ); // marker
202                     bits_write( &bits, 15, i_dts );
203                     bits_write( &bits, 1, 0x01 ); // marker
204                 }
205                 if( !i_pts_dts )
206                 {
207                     bits_write( &bits, 8, 0x0F );
208                 }
209
210             }
211
212             /* now should be stuffing */
213             /* and then pes data */
214
215             bits_align( &bits );
216             if( i_stream_id == PES_PRIVATE_STREAM_1 && i_private_id != -1 )
217             {
218                 bits_write( &bits, 8, i_private_id );
219                 if( ( i_private_id&0xf0 ) == 0x80 )
220                 {
221                     bits_write( &bits, 24, 0 ); // ac3
222                 }
223             }
224             bits_align( &bits );
225             return( bits.i_data );
226     }
227 }
228
229 int E_( EStoPES )( sout_instance_t *p_sout,
230                    sout_buffer_t **pp_pes,
231                    sout_buffer_t *p_es,
232                    int i_stream_id,
233                    int b_mpeg2 )
234 {
235     sout_buffer_t *p_es_sav, *p_pes;
236     mtime_t i_pts, i_dts;
237
238     uint8_t *p_data;
239     int     i_size;
240
241     int     i_private_id = -1;
242
243     uint8_t header[30];     // PES header + extra < 30 (more like 17)
244     int     i_pes_payload;
245     int     i_pes_header;
246
247     /* HACK for private stream 1 in ps */
248     if( ( i_stream_id >> 8 ) == PES_PRIVATE_STREAM_1 )
249     {
250         i_private_id = i_stream_id & 0xff;
251         i_stream_id  = PES_PRIVATE_STREAM_1;
252     }
253
254     i_pts = p_es->i_pts < 0 ? -1 : p_es->i_pts * 9 / 100; // 90000 units clock
255     i_dts = p_es->i_dts < 0 ? -1 : p_es->i_dts * 9 / 100; // 90000 units clock
256
257     i_size = p_es->i_size;
258     p_data = p_es->p_buffer;
259
260     *pp_pes = p_pes = NULL;
261     p_es_sav = p_es;
262
263     do
264     {
265         i_pes_payload = __MIN( i_size, PES_PAYLOAD_SIZE_MAX );
266         i_pes_header  = PESHeader( header, i_pts, i_dts, i_pes_payload,
267                                    i_stream_id, i_private_id, b_mpeg2 );
268         i_dts = -1; // only first PES has a dts/pts
269         i_pts = -1;
270
271         if( p_es  )
272         {
273             if( sout_BufferReallocFromPreHeader( p_sout, p_es, i_pes_header ) )
274             {
275                 msg_Err( p_sout,
276                          "cannot realloc preheader (should never happen)" );
277                 return( -1 );
278             }
279             /* reuse p_es for first frame */
280             *pp_pes = p_pes = p_es;
281             /* don't touch i_dts, i_pts, i_length as are already set :) */
282             p_es = NULL;
283         }
284         else
285         {
286             p_pes->p_next = sout_BufferNew( p_sout,
287                                             i_pes_header + i_pes_payload );
288             p_pes = p_pes->p_next;
289
290             p_pes->i_dts    = 0;
291             p_pes->i_pts    = 0;
292             p_pes->i_length = 0;
293             if( i_pes_payload > 0 )
294             {
295                 p_sout->p_vlc->pf_memcpy( p_pes->p_buffer + i_pes_header,
296                                           p_data, i_pes_payload );
297             }
298         }
299
300         /* copy header */
301         memcpy( p_pes->p_buffer, header, i_pes_header );
302
303         i_size -= i_pes_payload;
304         p_data += i_pes_payload;
305         p_pes->i_size =  i_pes_header + i_pes_payload;
306
307     } while( i_size > 0 );
308
309     /* sav some space */
310     if( p_es_sav->i_size + 10*1024 < p_es_sav->i_buffer_size )
311     {
312         sout_BufferRealloc( p_sout, p_es_sav, p_es_sav->i_size );
313     }
314
315     return( 0 );
316 }