]> git.sesse.net Git - vlc/blob - modules/mux/mpeg/pes.c
Plugins: include vlc_common.h directly instead of vlc/vlc.h
[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 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <errno.h>
31 #include <fcntl.h>
32
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <vlc_common.h>
38 #include <vlc_sout.h>
39 #include <vlc_block.h>
40
41 #ifdef HAVE_UNISTD_H
42 #   include <unistd.h>
43 #endif
44
45 #include <vlc_codecs.h>
46 #include "pes.h"
47 #include "bits.h"
48
49 static inline int PESHeader( uint8_t *p_hdr, mtime_t i_pts, mtime_t i_dts,
50                              int i_es_size, es_format_t *p_fmt,
51                              int i_stream_id, int i_private_id,
52                              bool b_mpeg2, bool b_data_alignment,
53                              int i_header_size )
54 {
55     bits_buffer_t bits;
56     int     i_extra = 0;
57
58     /* For PES_PRIVATE_STREAM_1 there is an extra header after the
59        pes header */
60     /* i_private_id != -1 because TS use 0xbd without private_id */
61     if( i_stream_id == PES_PRIVATE_STREAM_1 && i_private_id != -1 )
62     {
63         i_extra = 1;
64         if( ( i_private_id & 0xf0 ) == 0x80 )
65         {
66             i_extra += 3;
67         }
68     }
69
70     bits_initwrite( &bits, 50, p_hdr );
71
72     /* add start code */
73     bits_write( &bits, 24, 0x01 );
74     bits_write( &bits, 8, i_stream_id );
75     switch( i_stream_id )
76     {
77         case PES_PROGRAM_STREAM_MAP:
78         case PES_PADDING:
79         case PES_PRIVATE_STREAM_2:
80         case PES_ECM:
81         case PES_EMM:
82         case PES_PROGRAM_STREAM_DIRECTORY:
83         case PES_DSMCC_STREAM:
84         case PES_ITU_T_H222_1_TYPE_E_STREAM:
85             /* add pes data size  */
86             bits_write( &bits, 16, i_es_size );
87             bits_align( &bits );
88             return( bits.i_data );
89
90         default:
91             /* arg, a little more difficult */
92             if( b_mpeg2 )
93             {
94                 int i_pts_dts;
95
96                 if( i_pts > 0 && i_dts > 0 &&
97                     ( i_pts != i_dts || ( p_fmt->i_cat == VIDEO_ES &&
98                       p_fmt->i_codec != VLC_FOURCC('m','p','g','v') ) ) )
99                 {
100                     i_pts_dts = 0x03;
101                     if ( !i_header_size ) i_header_size = 0xa;
102                 }
103                 else if( i_pts > 0 )
104                 {
105                     i_pts_dts = 0x02;
106                     if ( !i_header_size ) i_header_size = 0x5;
107                 }
108                 else
109                 {
110                     i_pts_dts = 0x00;
111                     if ( !i_header_size ) i_header_size = 0x0;
112                 }
113
114                 bits_write( &bits, 16, i_es_size + i_extra + 3
115                              + i_header_size ); // size
116                 bits_write( &bits, 2, 0x02 ); // mpeg2 id
117                 bits_write( &bits, 2, 0x00 ); // pes scrambling control
118                 bits_write( &bits, 1, 0x00 ); // pes priority
119                 bits_write( &bits, 1, b_data_alignment ); // data alignement indicator
120                 bits_write( &bits, 1, 0x00 ); // copyright
121                 bits_write( &bits, 1, 0x00 ); // original or copy
122
123                 bits_write( &bits, 2, i_pts_dts ); // pts_dts flags
124                 bits_write( &bits, 1, 0x00 ); // escr flags
125                 bits_write( &bits, 1, 0x00 ); // es rate flag
126                 bits_write( &bits, 1, 0x00 ); // dsm trick mode flag
127                 bits_write( &bits, 1, 0x00 ); // additional copy info flag
128                 bits_write( &bits, 1, 0x00 ); // pes crc flag
129                 bits_write( &bits, 1, 0x00 ); // pes extension flags
130                 bits_write( &bits, 8, i_header_size ); // header size -> pts and dts
131
132                 /* write pts */
133                 if( i_pts_dts & 0x02 )
134                 {
135                     bits_write( &bits, 4, i_pts_dts ); // '0010' or '0011'
136                     bits_write( &bits, 3, i_pts >> 30 );
137                     bits_write( &bits, 1, 0x01 ); // marker
138                     bits_write( &bits, 15, i_pts >> 15 );
139                     bits_write( &bits, 1, 0x01 ); // marker
140                     bits_write( &bits, 15, i_pts );
141                     bits_write( &bits, 1, 0x01 ); // marker
142                     i_header_size -= 0x5;
143                 }
144                 /* write i_dts */
145                 if( i_pts_dts & 0x01 )
146                 {
147                     bits_write( &bits, 4, 0x01 ); // '0001'
148                     bits_write( &bits, 3, i_dts >> 30 );
149                     bits_write( &bits, 1, 0x01 ); // marker
150                     bits_write( &bits, 15, i_dts >> 15 );
151                     bits_write( &bits, 1, 0x01 ); // marker
152                     bits_write( &bits, 15, i_dts );
153                     bits_write( &bits, 1, 0x01 ); // marker
154                     i_header_size -= 0x5;
155                 }
156                 while ( i_header_size )
157                 {
158                     bits_write( &bits, 8, 0xff );
159                     i_header_size--;
160                 }
161             }
162             else /* MPEG1 */
163             {
164                 int i_pts_dts;
165
166                 if( i_pts > 0 && i_dts > 0 &&
167                     ( i_pts != i_dts || p_fmt->i_cat == VIDEO_ES ) )
168                 {
169                     bits_write( &bits, 16, i_es_size + i_extra + 10 /* + stuffing */ );
170                     i_pts_dts = 0x03;
171                 }
172                 else if( i_pts > 0 )
173                 {
174                     bits_write( &bits, 16, i_es_size + i_extra + 5 /* + stuffing */ );
175                     i_pts_dts = 0x02;
176                 }
177                 else
178                 {
179                     bits_write( &bits, 16, i_es_size + i_extra + 1 /* + stuffing */);
180                     i_pts_dts = 0x00;
181                 }
182
183                 /* FIXME: Now should be stuffing */
184
185                 /* No STD_buffer_scale and STD_buffer_size */
186
187                 /* write pts */
188                 if( i_pts_dts & 0x02 )
189                 {
190                     bits_write( &bits, 4, i_pts_dts ); // '0010' or '0011'
191                     bits_write( &bits, 3, i_pts >> 30 );
192                     bits_write( &bits, 1, 0x01 ); // marker
193                     bits_write( &bits, 15, i_pts >> 15 );
194                     bits_write( &bits, 1, 0x01 ); // marker
195                     bits_write( &bits, 15, i_pts );
196                     bits_write( &bits, 1, 0x01 ); // marker
197                 }
198                 /* write i_dts */
199                 if( i_pts_dts & 0x01 )
200                 {
201                     bits_write( &bits, 4, 0x01 ); // '0001'
202                     bits_write( &bits, 3, i_dts >> 30 );
203                     bits_write( &bits, 1, 0x01 ); // marker
204                     bits_write( &bits, 15, i_dts >> 15 );
205                     bits_write( &bits, 1, 0x01 ); // marker
206                     bits_write( &bits, 15, i_dts );
207                     bits_write( &bits, 1, 0x01 ); // marker
208                 }
209                 if( !i_pts_dts )
210                 {
211                     bits_write( &bits, 8, 0x0F );
212                 }
213
214             }
215
216             /* now should be stuffing */
217             /* and then pes data */
218
219             bits_align( &bits );
220             if( i_stream_id == PES_PRIVATE_STREAM_1 && i_private_id != -1 )
221             {
222                 bits_write( &bits, 8, i_private_id );
223                 if( ( i_private_id&0xf0 ) == 0x80 )
224                 {
225                     bits_write( &bits, 24, 0 ); // ac3
226                 }
227             }
228             bits_align( &bits );
229             return( bits.i_data );
230     }
231 }
232
233 int  EStoPES ( sout_instance_t *p_sout, block_t **pp_pes, block_t *p_es,
234                    es_format_t *p_fmt, int i_stream_id,
235                    int b_mpeg2, int b_data_alignment, int i_header_size,
236                    int i_max_pes_size )
237 {
238     block_t *p_pes;
239     mtime_t i_pts, i_dts, i_length;
240
241     uint8_t *p_data;
242     int     i_size;
243
244     int     i_private_id = -1;
245
246     uint8_t header[50];     // PES header + extra < 50 (more like 17)
247     int     i_pes_payload;
248     int     i_pes_header;
249
250     int     i_pes_count = 1;
251
252     /* HACK for private stream 1 in ps */
253     if( ( i_stream_id >> 8 ) == PES_PRIVATE_STREAM_1 )
254     {
255         i_private_id = i_stream_id & 0xff;
256         i_stream_id  = PES_PRIVATE_STREAM_1;
257     }
258
259     if( p_fmt->i_codec == VLC_FOURCC( 'm', 'p','4', 'v' ) &&
260         p_es->i_flags & BLOCK_FLAG_TYPE_I )
261     {
262         /* For MPEG4 video, add VOL before I-frames */
263         p_es = block_Realloc( p_es, p_fmt->i_extra, p_es->i_buffer );
264
265         memcpy( p_es->p_buffer, p_fmt->p_extra, p_fmt->i_extra );
266     }
267
268     i_pts = p_es->i_pts <= 0 ? 0 : p_es->i_pts * 9 / 100; // 90000 units clock
269     i_dts = p_es->i_dts <= 0 ? 0 : p_es->i_dts * 9 / 100; // 90000 units clock
270
271     i_size = p_es->i_buffer;
272     p_data = p_es->p_buffer;
273
274     *pp_pes = p_pes = NULL;
275
276 #ifndef NDEBUG
277     memset( header, 0, 50 );
278 #endif
279
280     do
281     {
282         i_pes_payload = __MIN( i_size, (i_max_pes_size ?
283                                i_max_pes_size : PES_PAYLOAD_SIZE_MAX) );
284         i_pes_header  = PESHeader( header, i_pts, i_dts, i_pes_payload,
285                                    p_fmt, i_stream_id, i_private_id, b_mpeg2,
286                                    b_data_alignment, i_header_size );
287         i_dts = 0; // only first PES has a dts/pts
288         i_pts = 0;
289
290         if( p_es )
291         {
292             p_es = block_Realloc( p_es, i_pes_header, p_es->i_buffer );
293             p_data = p_es->p_buffer+i_pes_header;
294             /* reuse p_es for first frame */
295             *pp_pes = p_pes = p_es;
296             /* don't touch i_dts, i_pts, i_length as are already set :) */
297             p_es = NULL;
298         }
299         else
300         {
301             p_pes->p_next = block_New( p_sout, i_pes_header + i_pes_payload );
302             p_pes = p_pes->p_next;
303
304             p_pes->i_dts    = 0;
305             p_pes->i_pts    = 0;
306             p_pes->i_length = 0;
307             if( i_pes_payload > 0 )
308             {
309                 vlc_memcpy( p_pes->p_buffer + i_pes_header, p_data,
310                             i_pes_payload );
311             }
312             i_pes_count++;
313         }
314
315         /* copy header */
316         memcpy( p_pes->p_buffer, header, i_pes_header );
317
318         i_size -= i_pes_payload;
319         p_data += i_pes_payload;
320         p_pes->i_buffer =  i_pes_header + i_pes_payload;
321
322     } while( i_size > 0 );
323
324     /* Now redate all pes */
325     i_dts    = (*pp_pes)->i_dts;
326     i_length = (*pp_pes)->i_length / i_pes_count;
327     for( p_pes = *pp_pes; p_pes != NULL; p_pes = p_pes->p_next )
328     {
329         p_pes->i_dts = i_dts;
330         p_pes->i_length = i_length;
331
332         i_dts += i_length;
333     }
334
335     return 0;
336 }