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