]> git.sesse.net Git - vlc/blob - modules/mux/mpeg/pes.c
mpeg mux: missing const
[vlc] / modules / mux / mpeg / pes.c
1 /*****************************************************************************
2  * pes.c: PES packetizer used by the MPEG multiplexers
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VLC authors and VideoLAN
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 it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * 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, const 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                       p_fmt->i_codec != VLC_CODEC_MP2V &&
115                       p_fmt->i_codec != VLC_CODEC_MP1V 
116                       ) ) )
117                 {
118                     i_pts_dts = 0x03;
119                     if ( !i_header_size ) i_header_size = 0xa;
120                 }
121                 else if( i_pts > 0 )
122                 {
123                     i_pts_dts = 0x02;
124                     if ( !i_header_size ) i_header_size = 0x5;
125                 }
126                 else
127                 {
128                     i_pts_dts = 0x00;
129                     if ( !i_header_size ) i_header_size = 0x0;
130                 }
131
132                 if( i_stream_id == PES_EXTENDED_STREAM_ID )
133                 {
134                     b_pes_extension_flag = true;
135                     i_header_size += 1 + 1;
136                 }
137
138                 if( b_pes_extension_flag )
139                 {
140                     i_header_size += 1;
141                 }
142
143                 /* Unbounded streams are only allowed in TS (not PS) and only
144                  * for some ES, eg. MPEG* Video ES or Dirac ES. */
145                 if( i_es_size > PES_PAYLOAD_SIZE_MAX )
146                     bits_write( &bits, 16, 0 ); // size unbounded
147                 else
148                     bits_write( &bits, 16, i_es_size + i_extra + 3
149                                  + i_header_size ); // size
150                 bits_write( &bits, 2, 0x02 ); // mpeg2 id
151                 bits_write( &bits, 2, 0x00 ); // pes scrambling control
152                 bits_write( &bits, 1, 0x00 ); // pes priority
153                 bits_write( &bits, 1, b_data_alignment ); // data alignement indicator
154                 bits_write( &bits, 1, 0x00 ); // copyright
155                 bits_write( &bits, 1, 0x00 ); // original or copy
156
157                 bits_write( &bits, 2, i_pts_dts ); // pts_dts flags
158                 bits_write( &bits, 1, 0x00 ); // escr flags
159                 bits_write( &bits, 1, 0x00 ); // es rate flag
160                 bits_write( &bits, 1, 0x00 ); // dsm trick mode flag
161                 bits_write( &bits, 1, 0x00 ); // additional copy info flag
162                 bits_write( &bits, 1, 0x00 ); // pes crc flag
163                 bits_write( &bits, 1, b_pes_extension_flag );
164                 bits_write( &bits, 8, i_header_size );
165
166                 /* write pts */
167                 if( i_pts_dts & 0x02 )
168                 {
169                     bits_write( &bits, 4, i_pts_dts ); // '0010' or '0011'
170                     bits_write( &bits, 3, i_pts >> 30 );
171                     bits_write( &bits, 1, 0x01 ); // marker
172                     bits_write( &bits, 15, i_pts >> 15 );
173                     bits_write( &bits, 1, 0x01 ); // marker
174                     bits_write( &bits, 15, i_pts );
175                     bits_write( &bits, 1, 0x01 ); // marker
176                     i_header_size -= 0x5;
177                 }
178                 /* write i_dts */
179                 if( i_pts_dts & 0x01 )
180                 {
181                     bits_write( &bits, 4, 0x01 ); // '0001'
182                     bits_write( &bits, 3, i_dts >> 30 );
183                     bits_write( &bits, 1, 0x01 ); // marker
184                     bits_write( &bits, 15, i_dts >> 15 );
185                     bits_write( &bits, 1, 0x01 ); // marker
186                     bits_write( &bits, 15, i_dts );
187                     bits_write( &bits, 1, 0x01 ); // marker
188                     i_header_size -= 0x5;
189                 }
190                 if( b_pes_extension_flag )
191                 {
192                     bits_write( &bits, 1, 0x00 ); // PES_private_data_flag
193                     bits_write( &bits, 1, 0x00 ); // pack_header_field_flag
194                     bits_write( &bits, 1, 0x00 ); // program_packet_sequence_counter_flag
195                     bits_write( &bits, 1, 0x00 ); // P-STD_buffer_flag
196                     bits_write( &bits, 3, 0x07 ); // reserved
197                     bits_write( &bits, 1, 0x01 ); // PES_extension_flag_2
198                     /* skipping unsupported parts: */
199                     /*   PES_private_data */
200                     /*   pack_header */
201                     /*   program_packet_sequence_counter */
202                     /*   P-STD_buffer_flag */
203                     if( i_stream_id == PES_EXTENDED_STREAM_ID )
204                     {
205                         /* PES_extension_2 */
206                         bits_write( &bits, 1, 0x01 ); // marker
207                         bits_write( &bits, 7, 0x01 ); // PES_extension_field_length
208                         bits_write( &bits, 1, 0x01 ); // stream_id_extension_flag
209                         bits_write( &bits, 7, i_stream_id_extension );
210                         i_header_size -= 0x2;
211                     }
212                     i_header_size -= 0x1;
213                 }
214                 while ( i_header_size )
215                 {
216                     bits_write( &bits, 8, 0xff );
217                     i_header_size--;
218                 }
219             }
220             else /* MPEG1 */
221             {
222                 int i_pts_dts;
223
224                 if( i_pts > 0 && i_dts > 0 &&
225                     ( i_pts != i_dts || p_fmt->i_cat == VIDEO_ES ) )
226                 {
227                     bits_write( &bits, 16, i_es_size + i_extra + 10 /* + stuffing */ );
228                     i_pts_dts = 0x03;
229                 }
230                 else if( i_pts > 0 )
231                 {
232                     bits_write( &bits, 16, i_es_size + i_extra + 5 /* + stuffing */ );
233                     i_pts_dts = 0x02;
234                 }
235                 else
236                 {
237                     bits_write( &bits, 16, i_es_size + i_extra + 1 /* + stuffing */);
238                     i_pts_dts = 0x00;
239                 }
240
241                 /* FIXME: Now should be stuffing */
242
243                 /* No STD_buffer_scale and STD_buffer_size */
244
245                 /* write pts */
246                 if( i_pts_dts & 0x02 )
247                 {
248                     bits_write( &bits, 4, i_pts_dts ); // '0010' or '0011'
249                     bits_write( &bits, 3, i_pts >> 30 );
250                     bits_write( &bits, 1, 0x01 ); // marker
251                     bits_write( &bits, 15, i_pts >> 15 );
252                     bits_write( &bits, 1, 0x01 ); // marker
253                     bits_write( &bits, 15, i_pts );
254                     bits_write( &bits, 1, 0x01 ); // marker
255                 }
256                 /* write i_dts */
257                 if( i_pts_dts & 0x01 )
258                 {
259                     bits_write( &bits, 4, 0x01 ); // '0001'
260                     bits_write( &bits, 3, i_dts >> 30 );
261                     bits_write( &bits, 1, 0x01 ); // marker
262                     bits_write( &bits, 15, i_dts >> 15 );
263                     bits_write( &bits, 1, 0x01 ); // marker
264                     bits_write( &bits, 15, i_dts );
265                     bits_write( &bits, 1, 0x01 ); // marker
266                 }
267                 if( !i_pts_dts )
268                 {
269                     bits_write( &bits, 8, 0x0F );
270                 }
271
272             }
273
274             /* now should be stuffing */
275             /* and then pes data */
276
277             bits_align( &bits );
278             if( i_stream_id == PES_PRIVATE_STREAM_1 && i_private_id != -1 )
279             {
280                 bits_write( &bits, 8, i_private_id );
281                 if( ( i_private_id&0xf0 ) == 0x80 )
282                 {
283                     bits_write( &bits, 24, 0 ); // ac3
284                 }
285             }
286             bits_align( &bits );
287             return( bits.i_data );
288     }
289 }
290
291 /** EStoPES, encapsulate an elementary stream block into PES packet(s)
292  * each with a maximal payload size of @i_max_pes_size@.
293  *
294  * In some circumstances, unbounded PES packets are allowed:
295  *  - Transport streams only (NOT programme streams)
296  *  - Only some types of elementary streams (eg MPEG2 video)
297  * It is the responsibility of the caller to enforce these constraints.
298  *
299  * EStoPES will only produce an unbounded PES packet if:
300  *  - ES is VIDEO_ES
301  *  - i_max_pes_size > PES_PAYLOAD_SIZE_MAX
302  *  - length of p_es > PES_PAYLOAD_SIZE_MAX
303  * If the last condition is not met, a single PES packet is produced
304  * which is not unbounded in length.
305  *
306  * \param i_stream_id stream id as follows:
307  *                     - 0x00   - 0xff   : normal stream_id as per Table 2-18
308  *                     - 0xfd00 - 0xfd7f : stream_id_extension = low 7 bits
309  *                                         (stream_id = PES_EXTENDED_STREAM_ID)
310  *                     - 0xbd00 - 0xbdff : private_id = low 8 bits
311  *                                         (stream_id = PES_PRIVATE_STREAM)
312  * \param i_header_size length of padding data to insert into PES packet
313  *                      header in bytes.
314  * \param i_max_pes_size maximum length of each pes packet payload.
315  *                       if zero, uses default maximum.
316  *                       To allow unbounded PES packets in transport stream
317  *                       VIDEO_ES, set to INT_MAX.
318  */
319 void EStoPES ( block_t **pp_pes,
320                    const es_format_t *p_fmt, int i_stream_id,
321                    int b_mpeg2, int b_data_alignment, int i_header_size,
322                    int i_max_pes_size, mtime_t ts_offset )
323 {
324     block_t *p_es = *pp_pes;
325     block_t *p_pes = NULL;
326
327     uint8_t *p_data;
328     int     i_size;
329
330     uint8_t header[50];     // PES header + extra < 50 (more like 17)
331     int     i_pes_payload;
332     int     i_pes_header;
333
334     int     i_pes_count = 1;
335
336     assert( i_max_pes_size >= 0 );
337     assert( i_header_size >= 0 );
338
339     /* NB, Only video ES may have unbounded length */
340     if( !i_max_pes_size ||
341         ( p_fmt->i_cat != VIDEO_ES && i_max_pes_size > PES_PAYLOAD_SIZE_MAX ) )
342     {
343         i_max_pes_size = PES_PAYLOAD_SIZE_MAX;
344     }
345
346     if( ( p_fmt->i_codec == VLC_CODEC_MP4V ||
347           p_fmt->i_codec == VLC_CODEC_H264 ||
348           p_fmt->i_codec == VLC_CODEC_HEVC) &&
349         p_es->i_flags & BLOCK_FLAG_TYPE_I )
350     {
351         /* For MPEG4 video, add VOL before I-frames,
352            for H264 add SPS/PPS before keyframes*/
353         p_es = block_Realloc( p_es, p_fmt->i_extra, p_es->i_buffer );
354
355         memcpy( p_es->p_buffer, p_fmt->p_extra, p_fmt->i_extra );
356     }
357
358     if( p_fmt->i_codec == VLC_CODEC_H264 )
359     {
360         unsigned offset=2;
361         while(offset < p_es->i_buffer )
362         {
363             if( p_es->p_buffer[offset-2] == 0 &&
364                 p_es->p_buffer[offset-1] == 0 &&
365                 p_es->p_buffer[offset] == 1 )
366                 break;
367             offset++;
368         }
369         offset++;
370         if( offset <= p_es->i_buffer-4 &&
371             ((p_es->p_buffer[offset] & 0x1f) != 9) ) /* Not AUD */
372         {
373             /* Make similar AUD as libavformat does */
374             p_es = block_Realloc( p_es, 6, p_es->i_buffer );
375             p_es->p_buffer[0] = 0x00;
376             p_es->p_buffer[1] = 0x00;
377             p_es->p_buffer[2] = 0x00;
378             p_es->p_buffer[3] = 0x01;
379             p_es->p_buffer[4] = 0x09;
380             p_es->p_buffer[5] = 0xe0;
381         }
382
383     }
384
385     mtime_t i_dts = 0;
386     mtime_t i_pts = 0;
387     if (p_es->i_pts > VLC_TS_INVALID)
388         i_pts = (p_es->i_pts - ts_offset) * 9 / 100;
389     if (p_es->i_dts > VLC_TS_INVALID)
390         i_dts = (p_es->i_dts - ts_offset) * 9 / 100;
391
392     i_size = p_es->i_buffer;
393     p_data = p_es->p_buffer;
394
395     do
396     {
397         i_pes_payload = __MIN( i_size, i_max_pes_size );
398         i_pes_header  = PESHeader( header, i_pts, i_dts, i_pes_payload,
399                                    p_fmt, i_stream_id, b_mpeg2,
400                                    b_data_alignment, i_header_size );
401         i_dts = 0; // only first PES has a dts/pts
402         i_pts = 0;
403
404         if( p_es )
405         {
406             p_es = block_Realloc( p_es, i_pes_header, p_es->i_buffer );
407             p_data = p_es->p_buffer+i_pes_header;
408             /* reuse p_es for first frame */
409             *pp_pes = p_pes = p_es;
410             /* don't touch i_dts, i_pts, i_length as are already set :) */
411             p_es = NULL;
412         }
413         else
414         {
415             p_pes->p_next = block_Alloc( i_pes_header + i_pes_payload );
416             p_pes = p_pes->p_next;
417
418             p_pes->i_dts    = 0;
419             p_pes->i_pts    = 0;
420             p_pes->i_length = 0;
421             if( i_pes_payload > 0 )
422             {
423                 memcpy( p_pes->p_buffer + i_pes_header, p_data,
424                             i_pes_payload );
425             }
426             i_pes_count++;
427         }
428
429         /* copy header */
430         memcpy( p_pes->p_buffer, header, i_pes_header );
431
432         i_size -= i_pes_payload;
433         p_data += i_pes_payload;
434         p_pes->i_buffer =  i_pes_header + i_pes_payload;
435
436     } while( i_size > 0 );
437
438     /* Now redate all pes */
439     p_pes = *pp_pes;
440     i_dts    = p_pes->i_dts;
441     mtime_t i_length = p_pes->i_length / i_pes_count;
442     while( p_pes )
443     {
444         p_pes->i_dts = i_dts;
445         p_pes->i_length = i_length;
446
447         i_dts += i_length;
448         p_pes = p_pes->p_next;
449     }
450 }