]> git.sesse.net Git - vlc/blob - modules/mux/mpeg/pes.c
* all: use p_vlc->pf_memcpy instead of memcpy on big data block.
[vlc] / modules / mux / mpeg / pes.c
1 /*****************************************************************************
2  * pes.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: pes.c,v 1.4 2003/01/17 15:26:24 fenrir Exp $
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 #elif defined( _MSC_VER ) && defined( _WIN32 ) && !defined( UNDER_CE )
42 #   include <io.h>
43 #endif
44
45 #include "codecs.h"
46 #include "pes.h"
47 #include "bits.h"
48
49 #define PES_PAYLOAD_SIZE_MAX 65500
50
51 static inline int PESHeader( uint8_t *p_hdr, mtime_t i_pts, mtime_t i_dts, int i_es_size, int i_stream_id, int i_private_id )
52 {
53     bits_buffer_t bits;
54
55     bits_initwrite( &bits, 30, p_hdr );
56
57     /* add start code */
58     bits_write( &bits, 24, 0x01 );
59     bits_write( &bits, 8, i_stream_id );
60     switch( i_stream_id )
61     {
62         case PES_PROGRAM_STREAM_MAP:
63         case PES_PADDING:
64         case PES_PRIVATE_STREAM_2:
65         case PES_ECM:
66         case PES_EMM:
67         case PES_PROGRAM_STREAM_DIRECTORY:
68         case PES_DSMCC_STREAM:
69         case PES_ITU_T_H222_1_TYPE_E_STREAM:
70             /* add pes data size  */
71             bits_write( &bits, 16, i_es_size );
72             bits_align( &bits );
73             return( bits.i_data );
74
75         default:
76             /* arg, a little more difficult */
77 //            if( b_mpeg2 ) // FIXME unsupported
78             {
79                 int     i_pts_dts;
80                 int     i_extra = 0;
81
82                 /* *** for PES_PRIVATE_STREAM_1 there is an extra header after the pes hdr *** */
83                 if( i_stream_id == PES_PRIVATE_STREAM_1 )
84                 {
85                     i_extra = 1;
86                     if( ( i_private_id&0xf0 ) == 0x80 )
87                     {
88                         i_extra += 3;
89                     }
90                 }
91
92                 if( i_dts >= 0 )
93                 {
94                     bits_write( &bits, 16, i_es_size + i_extra+ 13 );
95                     i_pts_dts = 0x03;
96                 }
97                 else
98                 {
99                     bits_write( &bits, 16, i_es_size  + i_extra + 8 );
100                     i_pts_dts = 0x02;
101                 }
102
103                 bits_write( &bits, 2, 0x02 ); // mpeg2 id
104                 bits_write( &bits, 2, 0x00 ); // pes scrambling control
105                 bits_write( &bits, 1, 0x00 ); // pes priority
106                 bits_write( &bits, 1, 0x00 ); // data alignement indicator
107                 bits_write( &bits, 1, 0x00 ); // copyright
108                 bits_write( &bits, 1, 0x00 ); // original or copy
109
110                 bits_write( &bits, 2, i_pts_dts ); // pts_dts flags
111                 bits_write( &bits, 1, 0x00 ); // escr flags
112                 bits_write( &bits, 1, 0x00 ); // es rate flag
113                 bits_write( &bits, 1, 0x00 ); // dsm trick mode flag
114                 bits_write( &bits, 1, 0x00 ); // additional copy info flag
115                 bits_write( &bits, 1, 0x00 ); // pes crc flag
116                 bits_write( &bits, 1, 0x00 ); // pes extention flags
117                 if( i_pts_dts & 0x01 )
118                 {
119                     bits_write( &bits, 8, 0x0a ); // header size -> pts and dts
120                 }
121                 else
122                 {
123                     bits_write( &bits, 8, 0x05 ); // header size -> pts
124                 }
125
126                 /* write pts */
127                 bits_write( &bits, 4, i_pts_dts ); // '0010' or '0011'
128                 bits_write( &bits, 3, i_pts >> 30 );
129                 bits_write( &bits, 1, 0x01 ); // marker
130                 bits_write( &bits, 15, i_pts >> 15 );
131                 bits_write( &bits, 1, 0x01 ); // marker
132                 bits_write( &bits, 15, i_pts );
133                 bits_write( &bits, 1, 0x01 ); // marker
134
135                 /* write i_dts */
136                 if( i_pts_dts & 0x01 )
137                 {
138                     bits_write( &bits, 4, 0x01 ); // '0001'
139                     bits_write( &bits, 3, i_dts >> 30 );
140                     bits_write( &bits, 1, 0x01 ); // marker
141                     bits_write( &bits, 15, i_dts >> 15 );
142                     bits_write( &bits, 1, 0x01 ); // marker
143                     bits_write( &bits, 15, i_dts );
144                     bits_write( &bits, 1, 0x01 ); // marker
145                 }
146             }
147             /* now should be stuffing */
148             /* and then pes data */
149
150             bits_align( &bits );
151             if( i_stream_id == PES_PRIVATE_STREAM_1 && i_private_id != -1 )
152             {
153                 bits_write( &bits, 8, i_private_id );
154                 if( ( i_private_id&0xf0 ) == 0x80 )
155                 {
156                     bits_write( &bits, 24, 0 ); // ac3
157                 }
158             }
159             bits_align( &bits );
160             return( bits.i_data );
161     }
162 }
163
164 int E_( EStoPES )( sout_instance_t *p_sout,
165                    sout_buffer_t **pp_pes,
166                    sout_buffer_t *p_es,
167                    int i_stream_id,
168                    int b_mpeg2 )
169 {
170     sout_buffer_t *p_es_sav, *p_pes;
171     mtime_t i_pts, i_dts;
172
173     uint8_t *p_data;
174     int     i_size;
175
176     int     i_private_id = -1;
177
178     uint8_t header[30];     // PES header + extra < 30 (more like 17)
179     int     i_pes_payload;
180     int     i_pes_header;
181
182     /* HACK for private stream 1 in ps */
183     if( ( i_stream_id >> 8 ) == PES_PRIVATE_STREAM_1 )
184     {
185         i_private_id = i_stream_id & 0xff;
186         i_stream_id  = PES_PRIVATE_STREAM_1;
187     }
188
189     i_pts = p_es->i_pts * 9 / 100; // 90000 units clock
190     i_dts = p_es->i_dts * 9 / 100; // 90000 units clock
191
192     i_size = p_es->i_size;
193     p_data = p_es->p_buffer;
194
195     *pp_pes = p_pes = NULL;
196     p_es_sav = p_es;
197
198     do
199     {
200         i_pes_payload = __MIN( i_size, PES_PAYLOAD_SIZE_MAX );
201         i_pes_header  = PESHeader( header, i_pts, i_dts, i_pes_payload, i_stream_id, i_private_id );
202         i_dts = -1; // only first PES has a dts
203
204         if( p_es  )
205         {
206             if( sout_BufferReallocFromPreHeader( p_sout, p_es, i_pes_header ) )
207             {
208                 msg_Err( p_sout, "cannot realloc prehader (should never happen)" );
209                 return( -1 );
210             }
211             /* reuse p_es for first frame */
212             *pp_pes = p_pes = p_es;
213             /* don't touch i_dts, i_pts, i_length as are already set :) */
214             p_es = NULL;
215         }
216         else
217         {
218             p_pes->p_next = sout_BufferNew( p_sout, i_pes_header + i_pes_payload );
219             p_pes = p_pes->p_next;
220
221             p_pes->i_dts    = 0;
222             p_pes->i_pts    = 0;
223             p_pes->i_length = 0;
224             if( i_pes_payload > 0 )
225             {
226                 p_sout->p_vlc->pf_memcpy( p_pes->p_buffer + i_pes_header, p_data, i_pes_payload );
227             }
228         }
229
230         /* copy header */
231         memcpy( p_pes->p_buffer, header, i_pes_header );
232
233         i_size -= i_pes_payload;
234         p_data += i_pes_payload;
235         p_pes->i_size =  i_pes_header + i_pes_payload;
236
237     } while( i_size > 0 );
238
239     /* sav some space */
240     if( p_es_sav->i_size + 10*1024 < p_es_sav->i_buffer_size )
241     {
242         sout_BufferRealloc( p_sout, p_es_sav, p_es_sav->i_size );
243     }
244
245     return( 0 );
246 }
247
248
249
250