]> git.sesse.net Git - vlc/blob - modules/mux/mpeg/pes.c
mux/mpeg/pes: move PES_PRIVATE_STREAM_1 id handling into PESHeader()
[vlc] / modules / mux / mpeg / pes.c
1 /*****************************************************************************
2  * pes.c: PES packetizer used by the MPEG multiplexers
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 the VideoLAN team
5  * $Id$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <errno.h>
36 #include <fcntl.h>
37
38 #include <vlc_common.h>
39 #include <vlc_sout.h>
40 #include <vlc_block.h>
41
42 #ifdef HAVE_UNISTD_H
43 #   include <unistd.h>
44 #endif
45
46 #include <vlc_codecs.h>
47 #include "pes.h"
48 #include "bits.h"
49
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  *                     - 0xbd00 - 0xbdff : private_id = low 8 bits
58  *                                         (stream_id = PES_PRIVATE_STREAM)
59  * \param i_header_size length of padding data to insert into PES packet
60  *                      header in bytes.
61  */
62 static inline int PESHeader( uint8_t *p_hdr, mtime_t i_pts, mtime_t i_dts,
63                              int i_es_size, es_format_t *p_fmt,
64                              int i_stream_id, bool b_mpeg2,
65                              bool b_data_alignment, int i_header_size )
66 {
67     bits_buffer_t bits;
68     int     i_extra = 0;
69     int i_private_id = -1;
70     int i_stream_id_extension = 0;
71
72     /* HACK for private stream 1 in ps */
73     if( ( i_stream_id >> 8 ) == PES_PRIVATE_STREAM_1 )
74     {
75         i_private_id = i_stream_id & 0xff;
76         i_stream_id = PES_PRIVATE_STREAM_1;
77         /* For PES_PRIVATE_STREAM_1 there is an extra header after the
78            pes header */
79         /* i_private_id != -1 because TS use 0xbd without private_id */
80         if( i_private_id != -1 )
81             i_extra = 1;
82         if( ( i_private_id & 0xf0 ) == 0x80 ) /* implies i_private_id != -1 */
83             i_extra += 3;
84     }
85     else if( ( i_stream_id >> 8 ) == PES_EXTENDED_STREAM_ID )
86     {
87         /* Enable support for extended_stream_id as defined in
88          * ISO/IEC 13818-1:2000/Amd.2:2003 */
89         /* NB, i_extended_stream_id is limited to 7 bits */
90         i_stream_id_extension = i_stream_id & 0x7f;
91         i_stream_id = PES_EXTENDED_STREAM_ID;
92     }
93
94     bits_initwrite( &bits, 50, p_hdr );
95
96     /* add start code */
97     bits_write( &bits, 24, 0x01 );
98     bits_write( &bits, 8, i_stream_id );
99     switch( i_stream_id )
100     {
101         case PES_PROGRAM_STREAM_MAP:
102         case PES_PADDING:
103         case PES_PRIVATE_STREAM_2:
104         case PES_ECM:
105         case PES_EMM:
106         case PES_PROGRAM_STREAM_DIRECTORY:
107         case PES_DSMCC_STREAM:
108         case PES_ITU_T_H222_1_TYPE_E_STREAM:
109             /* add pes data size  */
110             bits_write( &bits, 16, i_es_size );
111             bits_align( &bits );
112             return( bits.i_data );
113
114         default:
115             /* arg, a little more difficult */
116             if( b_mpeg2 )
117             {
118                 int i_pts_dts;
119                 bool b_pes_extension_flag = false;
120
121                 if( i_pts > 0 && i_dts > 0 &&
122                     ( i_pts != i_dts || ( p_fmt->i_cat == VIDEO_ES &&
123                       p_fmt->i_codec != VLC_FOURCC('m','p','g','v') ) ) )
124                 {
125                     i_pts_dts = 0x03;
126                     if ( !i_header_size ) i_header_size = 0xa;
127                 }
128                 else if( i_pts > 0 )
129                 {
130                     i_pts_dts = 0x02;
131                     if ( !i_header_size ) i_header_size = 0x5;
132                 }
133                 else
134                 {
135                     i_pts_dts = 0x00;
136                     if ( !i_header_size ) i_header_size = 0x0;
137                 }
138
139                 if( i_stream_id == 0xfd )
140                 {
141                     b_pes_extension_flag = true;
142                     i_header_size += 1 + 1;
143                 }
144
145                 if( b_pes_extension_flag )
146                 {
147                     i_header_size += 1;
148                 }
149
150                 /* Unbounded streams are only allowed in TS (not PS) and only
151                  * for some ES, eg. MPEG* Video ES or Dirac ES. */
152                 if( i_es_size > PES_PAYLOAD_SIZE_MAX )
153                     bits_write( &bits, 16, 0 ); // size unbounded
154                 else
155                     bits_write( &bits, 16, i_es_size + i_extra + 3
156                                  + i_header_size ); // size
157                 bits_write( &bits, 2, 0x02 ); // mpeg2 id
158                 bits_write( &bits, 2, 0x00 ); // pes scrambling control
159                 bits_write( &bits, 1, 0x00 ); // pes priority
160                 bits_write( &bits, 1, b_data_alignment ); // data alignement indicator
161                 bits_write( &bits, 1, 0x00 ); // copyright
162                 bits_write( &bits, 1, 0x00 ); // original or copy
163
164                 bits_write( &bits, 2, i_pts_dts ); // pts_dts flags
165                 bits_write( &bits, 1, 0x00 ); // escr flags
166                 bits_write( &bits, 1, 0x00 ); // es rate flag
167                 bits_write( &bits, 1, 0x00 ); // dsm trick mode flag
168                 bits_write( &bits, 1, 0x00 ); // additional copy info flag
169                 bits_write( &bits, 1, 0x00 ); // pes crc flag
170                 bits_write( &bits, 1, b_pes_extension_flag );
171                 bits_write( &bits, 8, i_header_size );
172
173                 /* write pts */
174                 if( i_pts_dts & 0x02 )
175                 {
176                     bits_write( &bits, 4, i_pts_dts ); // '0010' or '0011'
177                     bits_write( &bits, 3, i_pts >> 30 );
178                     bits_write( &bits, 1, 0x01 ); // marker
179                     bits_write( &bits, 15, i_pts >> 15 );
180                     bits_write( &bits, 1, 0x01 ); // marker
181                     bits_write( &bits, 15, i_pts );
182                     bits_write( &bits, 1, 0x01 ); // marker
183                     i_header_size -= 0x5;
184                 }
185                 /* write i_dts */
186                 if( i_pts_dts & 0x01 )
187                 {
188                     bits_write( &bits, 4, 0x01 ); // '0001'
189                     bits_write( &bits, 3, i_dts >> 30 );
190                     bits_write( &bits, 1, 0x01 ); // marker
191                     bits_write( &bits, 15, i_dts >> 15 );
192                     bits_write( &bits, 1, 0x01 ); // marker
193                     bits_write( &bits, 15, i_dts );
194                     bits_write( &bits, 1, 0x01 ); // marker
195                     i_header_size -= 0x5;
196                 }
197                 if( b_pes_extension_flag )
198                 {
199                     bits_write( &bits, 1, 0x00 ); // PES_private_data_flag
200                     bits_write( &bits, 1, 0x00 ); // pack_header_field_flag
201                     bits_write( &bits, 1, 0x00 ); // program_packet_sequence_counter_flag
202                     bits_write( &bits, 1, 0x00 ); // P-STD_buffer_flag
203                     bits_write( &bits, 3, 0x07 ); // reserved
204                     bits_write( &bits, 1, 0x01 ); // PES_extension_flag_2
205                     /* skipping unsupported parts: */
206                     /*   PES_private_data */
207                     /*   pack_header */
208                     /*   program_packet_sequence_counter */
209                     /*   P-STD_buffer_flag */
210                     if( i_stream_id == 0xfd )
211                     {
212                         /* PES_extension_2 */
213                         bits_write( &bits, 1, 0x01 ); // marker
214                         bits_write( &bits, 7, 0x01 ); // PES_extension_field_length
215                         bits_write( &bits, 1, 0x01 ); // stream_id_extension_flag
216                         bits_write( &bits, 7, i_stream_id_extension );
217                         i_header_size -= 0x2;
218                     }
219                     i_header_size -= 0x1;
220                 }
221                 while ( i_header_size )
222                 {
223                     bits_write( &bits, 8, 0xff );
224                     i_header_size--;
225                 }
226             }
227             else /* MPEG1 */
228             {
229                 int i_pts_dts;
230
231                 if( i_pts > 0 && i_dts > 0 &&
232                     ( i_pts != i_dts || p_fmt->i_cat == VIDEO_ES ) )
233                 {
234                     bits_write( &bits, 16, i_es_size + i_extra + 10 /* + stuffing */ );
235                     i_pts_dts = 0x03;
236                 }
237                 else if( i_pts > 0 )
238                 {
239                     bits_write( &bits, 16, i_es_size + i_extra + 5 /* + stuffing */ );
240                     i_pts_dts = 0x02;
241                 }
242                 else
243                 {
244                     bits_write( &bits, 16, i_es_size + i_extra + 1 /* + stuffing */);
245                     i_pts_dts = 0x00;
246                 }
247
248                 /* FIXME: Now should be stuffing */
249
250                 /* No STD_buffer_scale and STD_buffer_size */
251
252                 /* write pts */
253                 if( i_pts_dts & 0x02 )
254                 {
255                     bits_write( &bits, 4, i_pts_dts ); // '0010' or '0011'
256                     bits_write( &bits, 3, i_pts >> 30 );
257                     bits_write( &bits, 1, 0x01 ); // marker
258                     bits_write( &bits, 15, i_pts >> 15 );
259                     bits_write( &bits, 1, 0x01 ); // marker
260                     bits_write( &bits, 15, i_pts );
261                     bits_write( &bits, 1, 0x01 ); // marker
262                 }
263                 /* write i_dts */
264                 if( i_pts_dts & 0x01 )
265                 {
266                     bits_write( &bits, 4, 0x01 ); // '0001'
267                     bits_write( &bits, 3, i_dts >> 30 );
268                     bits_write( &bits, 1, 0x01 ); // marker
269                     bits_write( &bits, 15, i_dts >> 15 );
270                     bits_write( &bits, 1, 0x01 ); // marker
271                     bits_write( &bits, 15, i_dts );
272                     bits_write( &bits, 1, 0x01 ); // marker
273                 }
274                 if( !i_pts_dts )
275                 {
276                     bits_write( &bits, 8, 0x0F );
277                 }
278
279             }
280
281             /* now should be stuffing */
282             /* and then pes data */
283
284             bits_align( &bits );
285             if( i_stream_id == PES_PRIVATE_STREAM_1 && i_private_id != -1 )
286             {
287                 bits_write( &bits, 8, i_private_id );
288                 if( ( i_private_id&0xf0 ) == 0x80 )
289                 {
290                     bits_write( &bits, 24, 0 ); // ac3
291                 }
292             }
293             bits_align( &bits );
294             return( bits.i_data );
295     }
296 }
297
298 /** EStoPES, encapsulate an elementary stream block into PES packet(s)
299  * each with a maximal payload size of @i_max_pes_size@.
300  *
301  * In some circumstances, unbounded PES packets are allowed:
302  *  - Transport streams only (NOT programme streams)
303  *  - Only some types of elementary streams (eg MPEG2 video)
304  * It is the responsibility of the caller to enforce these constraints.
305  *
306  * EStoPES will only produce an unbounded PES packet if:
307  *  - ES is VIDEO_ES
308  *  - i_max_pes_size > PES_PAYLOAD_SIZE_MAX
309  *  - length of p_es > PES_PAYLOAD_SIZE_MAX
310  * If the last condition is not met, a single PES packet is produced
311  * which is not unbounded in length.
312  *
313  * \param i_stream_id stream id as follows:
314  *                     - 0x00   - 0xff   : normal stream_id as per Table 2-18
315  *                     - 0xfd00 - 0xfd7f : stream_id_extension = low 7 bits
316  *                                         (stream_id = PES_EXTENDED_STREAM_ID)
317  *                     - 0xbd00 - 0xbdff : private_id = low 8 bits
318  *                                         (stream_id = PES_PRIVATE_STREAM)
319  * \param i_header_size length of padding data to insert into PES packet
320  *                      header in bytes.
321  * \param i_max_pes_size maximum length of each pes packet payload.
322  *                       if zero, uses default maximum.
323  *                       To allow unbounded PES packets in transport stream
324  *                       VIDEO_ES, set to INT_MAX.
325  */
326 int  EStoPES ( sout_instance_t *p_sout, block_t **pp_pes, block_t *p_es,
327                    es_format_t *p_fmt, int i_stream_id,
328                    int b_mpeg2, int b_data_alignment, int i_header_size,
329                    int i_max_pes_size )
330 {
331     block_t *p_pes;
332     mtime_t i_pts, i_dts, i_length;
333
334     uint8_t *p_data;
335     int     i_size;
336
337     uint8_t header[50];     // PES header + extra < 50 (more like 17)
338     int     i_pes_payload;
339     int     i_pes_header;
340
341     int     i_pes_count = 1;
342
343     assert( i_max_pes_size >= 0 );
344     assert( i_header_size >= 0 );
345
346     /* NB, Only video ES may have unbounded length */
347     if( !i_max_pes_size ||
348         ( p_fmt->i_cat != VIDEO_ES && i_max_pes_size > PES_PAYLOAD_SIZE_MAX ) )
349     {
350         i_max_pes_size = PES_PAYLOAD_SIZE_MAX;
351     }
352
353     if( p_fmt->i_codec == VLC_FOURCC( 'm', 'p','4', 'v' ) &&
354         p_es->i_flags & BLOCK_FLAG_TYPE_I )
355     {
356         /* For MPEG4 video, add VOL before I-frames */
357         p_es = block_Realloc( p_es, p_fmt->i_extra, p_es->i_buffer );
358
359         memcpy( p_es->p_buffer, p_fmt->p_extra, p_fmt->i_extra );
360     }
361
362     i_pts = p_es->i_pts <= 0 ? 0 : p_es->i_pts * 9 / 100; // 90000 units clock
363     i_dts = p_es->i_dts <= 0 ? 0 : p_es->i_dts * 9 / 100; // 90000 units clock
364
365     i_size = p_es->i_buffer;
366     p_data = p_es->p_buffer;
367
368     *pp_pes = p_pes = NULL;
369
370 #ifndef NDEBUG
371     memset( header, 0, 50 );
372 #endif
373
374     do
375     {
376         i_pes_payload = __MIN( i_size, i_max_pes_size );
377         i_pes_header  = PESHeader( header, i_pts, i_dts, i_pes_payload,
378                                    p_fmt, i_stream_id, b_mpeg2,
379                                    b_data_alignment, i_header_size );
380         i_dts = 0; // only first PES has a dts/pts
381         i_pts = 0;
382
383         if( p_es )
384         {
385             p_es = block_Realloc( p_es, i_pes_header, p_es->i_buffer );
386             p_data = p_es->p_buffer+i_pes_header;
387             /* reuse p_es for first frame */
388             *pp_pes = p_pes = p_es;
389             /* don't touch i_dts, i_pts, i_length as are already set :) */
390             p_es = NULL;
391         }
392         else
393         {
394             p_pes->p_next = block_New( p_sout, i_pes_header + i_pes_payload );
395             p_pes = p_pes->p_next;
396
397             p_pes->i_dts    = 0;
398             p_pes->i_pts    = 0;
399             p_pes->i_length = 0;
400             if( i_pes_payload > 0 )
401             {
402                 vlc_memcpy( p_pes->p_buffer + i_pes_header, p_data,
403                             i_pes_payload );
404             }
405             i_pes_count++;
406         }
407
408         /* copy header */
409         memcpy( p_pes->p_buffer, header, i_pes_header );
410
411         i_size -= i_pes_payload;
412         p_data += i_pes_payload;
413         p_pes->i_buffer =  i_pes_header + i_pes_payload;
414
415     } while( i_size > 0 );
416
417     /* Now redate all pes */
418     i_dts    = (*pp_pes)->i_dts;
419     i_length = (*pp_pes)->i_length / i_pes_count;
420     for( p_pes = *pp_pes; p_pes != NULL; p_pes = p_pes->p_next )
421     {
422         p_pes->i_dts = i_dts;
423         p_pes->i_length = i_length;
424
425         i_dts += i_length;
426     }
427
428     return 0;
429 }