1 /*****************************************************************************
2 * pes.c: PES packetizer used by the MPEG multiplexers
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <sys/types.h>
38 #include <vlc_common.h>
40 #include <vlc_block.h>
46 #include <vlc_codecs.h>
50 /** PESHeader, write a pes header
51 * \param i_es_size length of payload data. (Must be < PES_PAYLOAD_SIZE_MAX
52 * unless the conditions for unbounded PES packets are met)
53 * \param i_stream_id stream id as follows:
54 * - 0x00 - 0xff : normal stream_id as per Table 2-18
55 * - 0xfd00 - 0xfd7f : stream_id_extension = low 7 bits
56 * (stream_id = PES_EXTENDED_STREAM_ID)
57 * \param i_header_size length of padding data to insert into PES packet
60 static inline int PESHeader( uint8_t *p_hdr, mtime_t i_pts, mtime_t i_dts,
61 int i_es_size, es_format_t *p_fmt,
62 int i_stream_id, int i_private_id,
63 bool b_mpeg2, bool b_data_alignment,
68 int i_stream_id_extension = 0;
70 /* For PES_PRIVATE_STREAM_1 there is an extra header after the
72 /* i_private_id != -1 because TS use 0xbd without private_id */
73 if( i_stream_id == PES_PRIVATE_STREAM_1 && i_private_id != -1 )
76 if( ( i_private_id & 0xf0 ) == 0x80 )
82 if( ( i_stream_id >> 8 ) == PES_EXTENDED_STREAM_ID )
84 /* Enable support for extended_stream_id as defined in
85 * ISO/IEC 13818-1:2000/Amd.2:2003 */
86 /* NB, i_extended_stream_id is limited to 7 bits */
87 i_stream_id_extension = i_stream_id & 0x7f;
88 i_stream_id = PES_EXTENDED_STREAM_ID;
91 bits_initwrite( &bits, 50, p_hdr );
94 bits_write( &bits, 24, 0x01 );
95 bits_write( &bits, 8, i_stream_id );
98 case PES_PROGRAM_STREAM_MAP:
100 case PES_PRIVATE_STREAM_2:
103 case PES_PROGRAM_STREAM_DIRECTORY:
104 case PES_DSMCC_STREAM:
105 case PES_ITU_T_H222_1_TYPE_E_STREAM:
106 /* add pes data size */
107 bits_write( &bits, 16, i_es_size );
109 return( bits.i_data );
112 /* arg, a little more difficult */
116 bool b_pes_extension_flag = false;
118 if( i_pts > 0 && i_dts > 0 &&
119 ( i_pts != i_dts || ( p_fmt->i_cat == VIDEO_ES &&
120 p_fmt->i_codec != VLC_FOURCC('m','p','g','v') ) ) )
123 if ( !i_header_size ) i_header_size = 0xa;
128 if ( !i_header_size ) i_header_size = 0x5;
133 if ( !i_header_size ) i_header_size = 0x0;
136 if( i_stream_id == 0xfd )
138 b_pes_extension_flag = true;
139 i_header_size += 1 + 1;
142 if( b_pes_extension_flag )
147 /* Unbounded streams are only allowed in TS (not PS) and only
148 * for some ES, eg. MPEG* Video ES or Dirac ES. */
149 if( i_es_size > PES_PAYLOAD_SIZE_MAX )
150 bits_write( &bits, 16, 0 ); // size unbounded
152 bits_write( &bits, 16, i_es_size + i_extra + 3
153 + i_header_size ); // size
154 bits_write( &bits, 2, 0x02 ); // mpeg2 id
155 bits_write( &bits, 2, 0x00 ); // pes scrambling control
156 bits_write( &bits, 1, 0x00 ); // pes priority
157 bits_write( &bits, 1, b_data_alignment ); // data alignement indicator
158 bits_write( &bits, 1, 0x00 ); // copyright
159 bits_write( &bits, 1, 0x00 ); // original or copy
161 bits_write( &bits, 2, i_pts_dts ); // pts_dts flags
162 bits_write( &bits, 1, 0x00 ); // escr flags
163 bits_write( &bits, 1, 0x00 ); // es rate flag
164 bits_write( &bits, 1, 0x00 ); // dsm trick mode flag
165 bits_write( &bits, 1, 0x00 ); // additional copy info flag
166 bits_write( &bits, 1, 0x00 ); // pes crc flag
167 bits_write( &bits, 1, b_pes_extension_flag );
168 bits_write( &bits, 8, i_header_size );
171 if( i_pts_dts & 0x02 )
173 bits_write( &bits, 4, i_pts_dts ); // '0010' or '0011'
174 bits_write( &bits, 3, i_pts >> 30 );
175 bits_write( &bits, 1, 0x01 ); // marker
176 bits_write( &bits, 15, i_pts >> 15 );
177 bits_write( &bits, 1, 0x01 ); // marker
178 bits_write( &bits, 15, i_pts );
179 bits_write( &bits, 1, 0x01 ); // marker
180 i_header_size -= 0x5;
183 if( i_pts_dts & 0x01 )
185 bits_write( &bits, 4, 0x01 ); // '0001'
186 bits_write( &bits, 3, i_dts >> 30 );
187 bits_write( &bits, 1, 0x01 ); // marker
188 bits_write( &bits, 15, i_dts >> 15 );
189 bits_write( &bits, 1, 0x01 ); // marker
190 bits_write( &bits, 15, i_dts );
191 bits_write( &bits, 1, 0x01 ); // marker
192 i_header_size -= 0x5;
194 if( b_pes_extension_flag )
196 bits_write( &bits, 1, 0x00 ); // PES_private_data_flag
197 bits_write( &bits, 1, 0x00 ); // pack_header_field_flag
198 bits_write( &bits, 1, 0x00 ); // program_packet_sequence_counter_flag
199 bits_write( &bits, 1, 0x00 ); // P-STD_buffer_flag
200 bits_write( &bits, 3, 0x07 ); // reserved
201 bits_write( &bits, 1, 0x01 ); // PES_extension_flag_2
202 /* skipping unsupported parts: */
203 /* PES_private_data */
205 /* program_packet_sequence_counter */
206 /* P-STD_buffer_flag */
207 if( i_stream_id == 0xfd )
209 /* PES_extension_2 */
210 bits_write( &bits, 1, 0x01 ); // marker
211 bits_write( &bits, 7, 0x01 ); // PES_extension_field_length
212 bits_write( &bits, 1, 0x01 ); // stream_id_extension_flag
213 bits_write( &bits, 7, i_stream_id_extension );
214 i_header_size -= 0x2;
216 i_header_size -= 0x1;
218 while ( i_header_size )
220 bits_write( &bits, 8, 0xff );
228 if( i_pts > 0 && i_dts > 0 &&
229 ( i_pts != i_dts || p_fmt->i_cat == VIDEO_ES ) )
231 bits_write( &bits, 16, i_es_size + i_extra + 10 /* + stuffing */ );
236 bits_write( &bits, 16, i_es_size + i_extra + 5 /* + stuffing */ );
241 bits_write( &bits, 16, i_es_size + i_extra + 1 /* + stuffing */);
245 /* FIXME: Now should be stuffing */
247 /* No STD_buffer_scale and STD_buffer_size */
250 if( i_pts_dts & 0x02 )
252 bits_write( &bits, 4, i_pts_dts ); // '0010' or '0011'
253 bits_write( &bits, 3, i_pts >> 30 );
254 bits_write( &bits, 1, 0x01 ); // marker
255 bits_write( &bits, 15, i_pts >> 15 );
256 bits_write( &bits, 1, 0x01 ); // marker
257 bits_write( &bits, 15, i_pts );
258 bits_write( &bits, 1, 0x01 ); // marker
261 if( i_pts_dts & 0x01 )
263 bits_write( &bits, 4, 0x01 ); // '0001'
264 bits_write( &bits, 3, i_dts >> 30 );
265 bits_write( &bits, 1, 0x01 ); // marker
266 bits_write( &bits, 15, i_dts >> 15 );
267 bits_write( &bits, 1, 0x01 ); // marker
268 bits_write( &bits, 15, i_dts );
269 bits_write( &bits, 1, 0x01 ); // marker
273 bits_write( &bits, 8, 0x0F );
278 /* now should be stuffing */
279 /* and then pes data */
282 if( i_stream_id == PES_PRIVATE_STREAM_1 && i_private_id != -1 )
284 bits_write( &bits, 8, i_private_id );
285 if( ( i_private_id&0xf0 ) == 0x80 )
287 bits_write( &bits, 24, 0 ); // ac3
291 return( bits.i_data );
295 /** EStoPES, encapsulate an elementary stream block into PES packet(s)
296 * each with a maximal payload size of @i_max_pes_size@.
298 * In some circumstances, unbounded PES packets are allowed:
299 * - Transport streams only (NOT programme streams)
300 * - Only some types of elementary streams (eg MPEG2 video)
301 * It is the responsibility of the caller to enforce these constraints.
303 * EStoPES will only produce an unbounded PES packet if:
305 * - i_max_pes_size > PES_PAYLOAD_SIZE_MAX
306 * - length of p_es > PES_PAYLOAD_SIZE_MAX
307 * If the last condition is not met, a single PES packet is produced
308 * which is not unbounded in length.
310 * \param i_stream_id stream id as follows:
311 * - 0x00 - 0xff : normal stream_id as per Table 2-18
312 * - 0xfd00 - 0xfd7f : stream_id_extension = low 7 bits
313 * (stream_id = PES_EXTENDED_STREAM_ID)
314 * - 0xbd00 - 0xbdff : private_id = low 8 bits
315 * (stream_id = PES_PRIVATE_STREAM)
316 * \param i_header_size length of padding data to insert into PES packet
318 * \param i_max_pes_size maximum length of each pes packet payload.
319 * if zero, uses default maximum.
320 * To allow unbounded PES packets in transport stream
321 * VIDEO_ES, set to INT_MAX.
323 int EStoPES ( sout_instance_t *p_sout, block_t **pp_pes, block_t *p_es,
324 es_format_t *p_fmt, int i_stream_id,
325 int b_mpeg2, int b_data_alignment, int i_header_size,
329 mtime_t i_pts, i_dts, i_length;
334 int i_private_id = -1;
336 uint8_t header[50]; // PES header + extra < 50 (more like 17)
342 assert( i_max_pes_size >= 0 );
343 assert( i_header_size >= 0 );
345 /* NB, Only video ES may have unbounded length */
346 if( !i_max_pes_size ||
347 ( p_fmt->i_cat != VIDEO_ES && i_max_pes_size > PES_PAYLOAD_SIZE_MAX ) )
349 i_max_pes_size = PES_PAYLOAD_SIZE_MAX;
352 /* HACK for private stream 1 in ps */
353 if( ( i_stream_id >> 8 ) == PES_PRIVATE_STREAM_1 )
355 i_private_id = i_stream_id & 0xff;
356 i_stream_id = PES_PRIVATE_STREAM_1;
359 if( p_fmt->i_codec == VLC_FOURCC( 'm', 'p','4', 'v' ) &&
360 p_es->i_flags & BLOCK_FLAG_TYPE_I )
362 /* For MPEG4 video, add VOL before I-frames */
363 p_es = block_Realloc( p_es, p_fmt->i_extra, p_es->i_buffer );
365 memcpy( p_es->p_buffer, p_fmt->p_extra, p_fmt->i_extra );
368 i_pts = p_es->i_pts <= 0 ? 0 : p_es->i_pts * 9 / 100; // 90000 units clock
369 i_dts = p_es->i_dts <= 0 ? 0 : p_es->i_dts * 9 / 100; // 90000 units clock
371 i_size = p_es->i_buffer;
372 p_data = p_es->p_buffer;
374 *pp_pes = p_pes = NULL;
377 memset( header, 0, 50 );
382 i_pes_payload = __MIN( i_size, i_max_pes_size );
383 i_pes_header = PESHeader( header, i_pts, i_dts, i_pes_payload,
384 p_fmt, i_stream_id, i_private_id, b_mpeg2,
385 b_data_alignment, i_header_size );
386 i_dts = 0; // only first PES has a dts/pts
391 p_es = block_Realloc( p_es, i_pes_header, p_es->i_buffer );
392 p_data = p_es->p_buffer+i_pes_header;
393 /* reuse p_es for first frame */
394 *pp_pes = p_pes = p_es;
395 /* don't touch i_dts, i_pts, i_length as are already set :) */
400 p_pes->p_next = block_New( p_sout, i_pes_header + i_pes_payload );
401 p_pes = p_pes->p_next;
406 if( i_pes_payload > 0 )
408 vlc_memcpy( p_pes->p_buffer + i_pes_header, p_data,
415 memcpy( p_pes->p_buffer, header, i_pes_header );
417 i_size -= i_pes_payload;
418 p_data += i_pes_payload;
419 p_pes->i_buffer = i_pes_header + i_pes_payload;
421 } while( i_size > 0 );
423 /* Now redate all pes */
424 i_dts = (*pp_pes)->i_dts;
425 i_length = (*pp_pes)->i_length / i_pes_count;
426 for( p_pes = *pp_pes; p_pes != NULL; p_pes = p_pes->p_next )
428 p_pes->i_dts = i_dts;
429 p_pes->i_length = i_length;