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