1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: pes.c,v 1.7 2003/06/10 22:42:59 gbazin Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Eric Petit <titer@videolan.org>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
29 #include <sys/types.h>
36 #include <vlc/input.h>
47 #define PES_PAYLOAD_SIZE_MAX 65500
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 )
54 bits_initwrite( &bits, 30, p_hdr );
57 bits_write( &bits, 24, 0x01 );
58 bits_write( &bits, 8, i_stream_id );
61 case PES_PROGRAM_STREAM_MAP:
63 case PES_PRIVATE_STREAM_2:
66 case PES_PROGRAM_STREAM_DIRECTORY:
67 case PES_DSMCC_STREAM:
68 case PES_ITU_T_H222_1_TYPE_E_STREAM:
69 /* add pes data size */
70 bits_write( &bits, 16, i_es_size );
72 return( bits.i_data );
75 /* arg, a little more difficult */
76 // if( b_mpeg2 ) // FIXME unsupported
81 /* For PES_PRIVATE_STREAM_1 there is an extra header after the
83 if( i_stream_id == PES_PRIVATE_STREAM_1 )
86 if( ( i_private_id&0xf0 ) == 0x80 )
92 if( i_pts >= 0 && i_dts >= 0 )
94 bits_write( &bits, 16, i_es_size + i_extra+ 13 );
99 bits_write( &bits, 16, i_es_size + i_extra + 8 );
104 bits_write( &bits, 16, i_es_size + i_extra + 3 );
108 bits_write( &bits, 2, 0x02 ); // mpeg2 id
109 bits_write( &bits, 2, 0x00 ); // pes scrambling control
110 bits_write( &bits, 1, 0x00 ); // pes priority
111 bits_write( &bits, 1, 0x00 ); // data alignement indicator
112 bits_write( &bits, 1, 0x00 ); // copyright
113 bits_write( &bits, 1, 0x00 ); // original or copy
115 bits_write( &bits, 2, i_pts_dts ); // pts_dts flags
116 bits_write( &bits, 1, 0x00 ); // escr flags
117 bits_write( &bits, 1, 0x00 ); // es rate flag
118 bits_write( &bits, 1, 0x00 ); // dsm trick mode flag
119 bits_write( &bits, 1, 0x00 ); // additional copy info flag
120 bits_write( &bits, 1, 0x00 ); // pes crc flag
121 bits_write( &bits, 1, 0x00 ); // pes extention flags
122 if( i_pts_dts == 0x03 )
124 bits_write( &bits, 8, 0x0a ); // header size -> pts and dts
126 else if( i_pts_dts == 0x02 )
128 bits_write( &bits, 8, 0x05 ); // header size -> pts
132 bits_write( &bits, 8, 0x00 ); // header size -> 0
136 if( i_pts_dts & 0x02 )
138 bits_write( &bits, 4, i_pts_dts ); // '0010' or '0011'
139 bits_write( &bits, 3, i_pts >> 30 );
140 bits_write( &bits, 1, 0x01 ); // marker
141 bits_write( &bits, 15, i_pts >> 15 );
142 bits_write( &bits, 1, 0x01 ); // marker
143 bits_write( &bits, 15, i_pts );
144 bits_write( &bits, 1, 0x01 ); // marker
147 if( i_pts_dts & 0x01 )
149 bits_write( &bits, 4, 0x01 ); // '0001'
150 bits_write( &bits, 3, i_dts >> 30 );
151 bits_write( &bits, 1, 0x01 ); // marker
152 bits_write( &bits, 15, i_dts >> 15 );
153 bits_write( &bits, 1, 0x01 ); // marker
154 bits_write( &bits, 15, i_dts );
155 bits_write( &bits, 1, 0x01 ); // marker
158 /* now should be stuffing */
159 /* and then pes data */
162 if( i_stream_id == PES_PRIVATE_STREAM_1 && i_private_id != -1 )
164 bits_write( &bits, 8, i_private_id );
165 if( ( i_private_id&0xf0 ) == 0x80 )
167 bits_write( &bits, 24, 0 ); // ac3
171 return( bits.i_data );
175 int E_( EStoPES )( sout_instance_t *p_sout,
176 sout_buffer_t **pp_pes,
181 sout_buffer_t *p_es_sav, *p_pes;
182 mtime_t i_pts, i_dts;
187 int i_private_id = -1;
189 uint8_t header[30]; // PES header + extra < 30 (more like 17)
193 /* HACK for private stream 1 in ps */
194 if( ( i_stream_id >> 8 ) == PES_PRIVATE_STREAM_1 )
196 i_private_id = i_stream_id & 0xff;
197 i_stream_id = PES_PRIVATE_STREAM_1;
200 i_pts = p_es->i_pts < 0 ? -1 : p_es->i_pts * 9 / 100; // 90000 units clock
201 i_dts = p_es->i_dts < 0 ? -1 : p_es->i_dts * 9 / 100; // 90000 units clock
203 i_size = p_es->i_size;
204 p_data = p_es->p_buffer;
206 *pp_pes = p_pes = NULL;
211 i_pes_payload = __MIN( i_size, PES_PAYLOAD_SIZE_MAX );
212 i_pes_header = PESHeader( header, i_pts, i_dts, i_pes_payload,
213 i_stream_id, i_private_id );
214 i_dts = -1; // only first PES has a dts/pts
219 if( sout_BufferReallocFromPreHeader( p_sout, p_es, i_pes_header ) )
222 "cannot realloc preheader (should never happen)" );
225 /* reuse p_es for first frame */
226 *pp_pes = p_pes = p_es;
227 /* don't touch i_dts, i_pts, i_length as are already set :) */
232 p_pes->p_next = sout_BufferNew( p_sout,
233 i_pes_header + i_pes_payload );
234 p_pes = p_pes->p_next;
239 if( i_pes_payload > 0 )
241 p_sout->p_vlc->pf_memcpy( p_pes->p_buffer + i_pes_header,
242 p_data, i_pes_payload );
247 memcpy( p_pes->p_buffer, header, i_pes_header );
249 i_size -= i_pes_payload;
250 p_data += i_pes_payload;
251 p_pes->i_size = i_pes_header + i_pes_payload;
253 } while( i_size > 0 );
256 if( p_es_sav->i_size + 10*1024 < p_es_sav->i_buffer_size )
258 sout_BufferRealloc( p_sout, p_es_sav, p_es_sav->i_size );