demux: mp4: avoid audio cuts on seek
[vlc.git] / modules / mux / mpeg / tsutil.c
blob21668a067683ab4123498d0f94c4f87073d381c0
1 /*****************************************************************************
2 * tsutil.c
3 *****************************************************************************
4 * Copyright (C) 2001-2005, 2015 VLC authors and VideoLAN
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
24 #include <vlc_common.h>
25 #include <vlc_block.h>
27 #include "tsutil.h"
29 void PEStoTS( void *p_opaque, PEStoTSCallback pf_callback, block_t *p_pes,
30 uint16_t i_pid, bool *pb_discontinuity, uint8_t *pi_continuity_counter )
32 /* get PES total size */
33 uint8_t *p_data = p_pes->p_buffer;
34 int i_size = p_pes->i_buffer;
36 bool b_new_pes = true;
38 for (;;)
40 /* write header
41 * 8b 0x47 sync byte
42 * 1b transport_error_indicator
43 * 1b payload_unit_start
44 * 1b transport_priority
45 * 13b pid
46 * 2b transport_scrambling_control
47 * 2b if adaptation_field 0x03 else 0x01
48 * 4b continuity_counter
51 int i_copy = __MIN( i_size, 184 );
52 bool b_adaptation_field = i_size < 184;
53 block_t *p_ts = block_Alloc( 188 );
55 p_ts->p_buffer[0] = 0x47;
56 p_ts->p_buffer[1] = ( b_new_pes ? 0x40 : 0x00 )|
57 ( ( i_pid >> 8 )&0x1f );
58 p_ts->p_buffer[2] = i_pid & 0xff;
59 p_ts->p_buffer[3] = ( b_adaptation_field ? 0x30 : 0x10 )|
60 *pi_continuity_counter;
62 b_new_pes = false;
63 *pi_continuity_counter = (*pi_continuity_counter+1)%16;
65 if( b_adaptation_field )
67 int i_stuffing = 184 - i_copy;
69 p_ts->p_buffer[4] = i_stuffing - 1;
70 if( i_stuffing > 1 )
72 p_ts->p_buffer[5] = 0x00;
73 if( *pb_discontinuity )
75 p_ts->p_buffer[5] |= 0x80;
76 *pb_discontinuity = false;
78 for (int i = 6; i < 6 + i_stuffing - 2; i++ )
80 p_ts->p_buffer[i] = 0xff;
84 /* copy payload */
85 memcpy( &p_ts->p_buffer[188 - i_copy], p_data, i_copy );
86 p_data += i_copy;
87 i_size -= i_copy;
89 pf_callback( p_opaque, p_ts );
91 if( i_size <= 0 )
93 block_t *p_next = p_pes->p_next;
95 p_pes->p_next = NULL;
96 block_Release( p_pes );
97 if( p_next == NULL )
98 return;
100 b_new_pes = true;
101 p_pes = p_next;
102 i_size = p_pes->i_buffer;
103 p_data = p_pes->p_buffer;