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