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