pes.c 11.1 KB
Newer Older
1
/*****************************************************************************
2
 * pes.c: PES packetizer used by the MPEG multiplexers
3
4
 *****************************************************************************
 * Copyright (C) 2001, 2002 VideoLAN
5
 * $Id: pes.c,v 1.12 2003/11/15 18:57:12 fenrir Exp $
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *          Eric Petit <titer@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>

#include <vlc/vlc.h>
#include <vlc/input.h>
#include <vlc/sout.h>

#ifdef HAVE_UNISTD_H
#   include <unistd.h>
#endif

#include "codecs.h"
#include "pes.h"
#include "bits.h"

47
48
#define PES_PAYLOAD_SIZE_MAX 65500

gbazin's avatar
   
gbazin committed
49
static inline int PESHeader( uint8_t *p_hdr, mtime_t i_pts, mtime_t i_dts,
50
51
                             int i_es_size, int i_stream_id, int i_private_id,
                             vlc_bool_t b_mpeg2 )
52
53
{
    bits_buffer_t bits;
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    int     i_extra = 0;

    /* For PES_PRIVATE_STREAM_1 there is an extra header after the
       pes header */
    /* i_private_id != -1 because TS use 0xbd without private_id */
    if( i_stream_id == PES_PRIVATE_STREAM_1 && i_private_id != -1 )
    {
        i_extra = 1;
        if( ( i_private_id&0xf8 ) == 0x80 )
        {
            i_extra += 3;
        }
    }

68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

    bits_initwrite( &bits, 30, p_hdr );

    /* add start code */
    bits_write( &bits, 24, 0x01 );
    bits_write( &bits, 8, i_stream_id );
    switch( i_stream_id )
    {
        case PES_PROGRAM_STREAM_MAP:
        case PES_PADDING:
        case PES_PRIVATE_STREAM_2:
        case PES_ECM:
        case PES_EMM:
        case PES_PROGRAM_STREAM_DIRECTORY:
        case PES_DSMCC_STREAM:
        case PES_ITU_T_H222_1_TYPE_E_STREAM:
            /* add pes data size  */
            bits_write( &bits, 16, i_es_size );
            bits_align( &bits );
            return( bits.i_data );

        default:
            /* arg, a little more difficult */
91
            if( b_mpeg2 )
92
93
94
            {
                int     i_pts_dts;

gbazin's avatar
   
gbazin committed
95
                if( i_pts >= 0 && i_dts >= 0 )
96
97
98
99
                {
                    bits_write( &bits, 16, i_es_size + i_extra+ 13 );
                    i_pts_dts = 0x03;
                }
gbazin's avatar
   
gbazin committed
100
                else if( i_pts >= 0 )
101
102
103
104
                {
                    bits_write( &bits, 16, i_es_size  + i_extra + 8 );
                    i_pts_dts = 0x02;
                }
gbazin's avatar
   
gbazin committed
105
106
107
108
109
                else
                {
                    bits_write( &bits, 16, i_es_size  + i_extra + 3 );
                    i_pts_dts = 0x00;
                }
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

                bits_write( &bits, 2, 0x02 ); // mpeg2 id
                bits_write( &bits, 2, 0x00 ); // pes scrambling control
                bits_write( &bits, 1, 0x00 ); // pes priority
                bits_write( &bits, 1, 0x00 ); // data alignement indicator
                bits_write( &bits, 1, 0x00 ); // copyright
                bits_write( &bits, 1, 0x00 ); // original or copy

                bits_write( &bits, 2, i_pts_dts ); // pts_dts flags
                bits_write( &bits, 1, 0x00 ); // escr flags
                bits_write( &bits, 1, 0x00 ); // es rate flag
                bits_write( &bits, 1, 0x00 ); // dsm trick mode flag
                bits_write( &bits, 1, 0x00 ); // additional copy info flag
                bits_write( &bits, 1, 0x00 ); // pes crc flag
                bits_write( &bits, 1, 0x00 ); // pes extention flags
gbazin's avatar
   
gbazin committed
125
                if( i_pts_dts == 0x03 )
126
127
128
                {
                    bits_write( &bits, 8, 0x0a ); // header size -> pts and dts
                }
gbazin's avatar
   
gbazin committed
129
                else if( i_pts_dts == 0x02 )
130
131
132
                {
                    bits_write( &bits, 8, 0x05 ); // header size -> pts
                }
gbazin's avatar
   
gbazin committed
133
134
135
136
                else
                {
                    bits_write( &bits, 8, 0x00 ); // header size -> 0
                }
137
138

                /* write pts */
gbazin's avatar
   
gbazin committed
139
140
141
142
143
144
145
146
147
148
                if( i_pts_dts & 0x02 )
                {
                    bits_write( &bits, 4, i_pts_dts ); // '0010' or '0011'
                    bits_write( &bits, 3, i_pts >> 30 );
                    bits_write( &bits, 1, 0x01 ); // marker
                    bits_write( &bits, 15, i_pts >> 15 );
                    bits_write( &bits, 1, 0x01 ); // marker
                    bits_write( &bits, 15, i_pts );
                    bits_write( &bits, 1, 0x01 ); // marker
                }
149
150
151
152
153
154
155
156
157
158
159
160
                /* write i_dts */
                if( i_pts_dts & 0x01 )
                {
                    bits_write( &bits, 4, 0x01 ); // '0001'
                    bits_write( &bits, 3, i_dts >> 30 );
                    bits_write( &bits, 1, 0x01 ); // marker
                    bits_write( &bits, 15, i_dts >> 15 );
                    bits_write( &bits, 1, 0x01 ); // marker
                    bits_write( &bits, 15, i_dts );
                    bits_write( &bits, 1, 0x01 ); // marker
                }
            }
161
162
            else /* MPEG1 */
            {
163
164
165
166
                int i_pts_dts;

                if( i_pts >= 0 && i_dts >= 0 )
                {
167
                    bits_write( &bits, 16, i_es_size + i_extra + 10 /* + stuffing */ );
168
169
170
171
                    i_pts_dts = 0x03;
                }
                else if( i_pts >= 0 )
                {
172
                    bits_write( &bits, 16, i_es_size + i_extra + 5 /* + stuffing */ );
173
174
175
176
                    i_pts_dts = 0x02;
                }
                else
                {
177
                    bits_write( &bits, 16, i_es_size + i_extra + 1 /* + stuffing */);
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
                    i_pts_dts = 0x00;
                }

                /* FIXME: Now should be stuffing */

                /* No STD_buffer_scale and STD_buffer_size */

                /* write pts */
                if( i_pts_dts & 0x02 )
                {
                    bits_write( &bits, 4, i_pts_dts ); // '0010' or '0011'
                    bits_write( &bits, 3, i_pts >> 30 );
                    bits_write( &bits, 1, 0x01 ); // marker
                    bits_write( &bits, 15, i_pts >> 15 );
                    bits_write( &bits, 1, 0x01 ); // marker
                    bits_write( &bits, 15, i_pts );
                    bits_write( &bits, 1, 0x01 ); // marker
                }
                /* write i_dts */
                if( i_pts_dts & 0x01 )
                {
                    bits_write( &bits, 4, 0x01 ); // '0001'
                    bits_write( &bits, 3, i_dts >> 30 );
                    bits_write( &bits, 1, 0x01 ); // marker
                    bits_write( &bits, 15, i_dts >> 15 );
                    bits_write( &bits, 1, 0x01 ); // marker
                    bits_write( &bits, 15, i_dts );
                    bits_write( &bits, 1, 0x01 ); // marker
                }
207
                if( !i_pts_dts )
208
209
210
211
                {
                    bits_write( &bits, 8, 0x0F );
                }

212
            }
213

214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
            /* now should be stuffing */
            /* and then pes data */

            bits_align( &bits );
            if( i_stream_id == PES_PRIVATE_STREAM_1 && i_private_id != -1 )
            {
                bits_write( &bits, 8, i_private_id );
                if( ( i_private_id&0xf0 ) == 0x80 )
                {
                    bits_write( &bits, 24, 0 ); // ac3
                }
            }
            bits_align( &bits );
            return( bits.i_data );
    }
}

231
232
233
234
235
int E_( EStoPES )( sout_instance_t *p_sout,
                   sout_buffer_t **pp_pes,
                   sout_buffer_t *p_es,
                   int i_stream_id,
                   int b_mpeg2 )
236
{
237
    sout_buffer_t *p_es_sav, *p_pes;
238
    mtime_t i_pts, i_dts, i_length;
239

240
241
242
    uint8_t *p_data;
    int     i_size;

243
244
245
246
247
    int     i_private_id = -1;

    uint8_t header[30];     // PES header + extra < 30 (more like 17)
    int     i_pes_payload;
    int     i_pes_header;
248

249
250
    int     i_pes_count = 1;

251
    /* HACK for private stream 1 in ps */
252
    if( ( i_stream_id >> 8 ) == PES_PRIVATE_STREAM_1 )
253
254
255
256
257
    {
        i_private_id = i_stream_id & 0xff;
        i_stream_id  = PES_PRIVATE_STREAM_1;
    }

gbazin's avatar
   
gbazin committed
258
259
    i_pts = p_es->i_pts < 0 ? -1 : p_es->i_pts * 9 / 100; // 90000 units clock
    i_dts = p_es->i_dts < 0 ? -1 : p_es->i_dts * 9 / 100; // 90000 units clock
260
261
262
263
264

    i_size = p_es->i_size;
    p_data = p_es->p_buffer;

    *pp_pes = p_pes = NULL;
265
    p_es_sav = p_es;
266
267
268

    do
    {
269
        i_pes_payload = __MIN( i_size, PES_PAYLOAD_SIZE_MAX );
gbazin's avatar
   
gbazin committed
270
        i_pes_header  = PESHeader( header, i_pts, i_dts, i_pes_payload,
271
                                   i_stream_id, i_private_id, b_mpeg2 );
gbazin's avatar
   
gbazin committed
272
273
        i_dts = -1; // only first PES has a dts/pts
        i_pts = -1;
274

275
        if( p_es  )
276
        {
277
278
            if( sout_BufferReallocFromPreHeader( p_sout, p_es, i_pes_header ) )
            {
gbazin's avatar
   
gbazin committed
279
280
                msg_Err( p_sout,
                         "cannot realloc preheader (should never happen)" );
281
282
283
284
285
286
                return( -1 );
            }
            /* reuse p_es for first frame */
            *pp_pes = p_pes = p_es;
            /* don't touch i_dts, i_pts, i_length as are already set :) */
            p_es = NULL;
287
288
289
        }
        else
        {
gbazin's avatar
   
gbazin committed
290
291
            p_pes->p_next = sout_BufferNew( p_sout,
                                            i_pes_header + i_pes_payload );
292
293
294
295
296
            p_pes = p_pes->p_next;

            p_pes->i_dts    = 0;
            p_pes->i_pts    = 0;
            p_pes->i_length = 0;
297
298
            if( i_pes_payload > 0 )
            {
gbazin's avatar
   
gbazin committed
299
300
                p_sout->p_vlc->pf_memcpy( p_pes->p_buffer + i_pes_header,
                                          p_data, i_pes_payload );
301
            }
302
            i_pes_count++;
303
304
        }

305
306
        /* copy header */
        memcpy( p_pes->p_buffer, header, i_pes_header );
307

308
309
310
        i_size -= i_pes_payload;
        p_data += i_pes_payload;
        p_pes->i_size =  i_pes_header + i_pes_payload;
311

312
    } while( i_size > 0 );
313

314
315
316
317
318
    /* sav some space */
    if( p_es_sav->i_size + 10*1024 < p_es_sav->i_buffer_size )
    {
        sout_BufferRealloc( p_sout, p_es_sav, p_es_sav->i_size );
    }
319

320
321
322
323
324
325
326
327
328
329
    /* Now redate all pes */
    i_dts    = (*pp_pes)->i_dts;
    i_length = (*pp_pes)->i_length / i_pes_count;
    for( p_pes = *pp_pes; p_pes != NULL; p_pes = p_pes->p_next )
    {
        p_pes->i_dts = i_dts;
        p_pes->i_length = i_length;

        i_dts += i_length;
    }
330
331
    return( 0 );
}
332
333