/src/vlc/modules/mux/mpeg/tsutil.c
Line | Count | Source |
1 | | /***************************************************************************** |
2 | | * tsutil.c |
3 | | ***************************************************************************** |
4 | | * Copyright (C) 2001-2005, 2015 VLC authors and VideoLAN |
5 | | * |
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. |
10 | | * |
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. |
15 | | * |
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 |
23 | | |
24 | | #include <vlc_common.h> |
25 | | #include <vlc_block.h> |
26 | | |
27 | | #include "tsutil.h" |
28 | | |
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 ) |
31 | 737 | { |
32 | | /* get PES total size */ |
33 | 737 | uint8_t *p_data = p_pes->p_buffer; |
34 | 737 | int i_size = p_pes->i_buffer; |
35 | | |
36 | 737 | bool b_new_pes = true; |
37 | | |
38 | 737 | for (;;) |
39 | 737 | { |
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 |
49 | | */ |
50 | | |
51 | 737 | int i_copy = __MIN( i_size, 184 ); |
52 | 737 | bool b_adaptation_field = i_size < 184; |
53 | 737 | block_t *p_ts = block_Alloc( 188 ); |
54 | | |
55 | 737 | p_ts->p_buffer[0] = 0x47; |
56 | 737 | p_ts->p_buffer[1] = ( b_new_pes ? 0x40 : 0x00 )| |
57 | 737 | ( ( i_pid >> 8 )&0x1f ); |
58 | 737 | p_ts->p_buffer[2] = i_pid & 0xff; |
59 | 737 | p_ts->p_buffer[3] = ( b_adaptation_field ? 0x30 : 0x10 )| |
60 | 737 | *pi_continuity_counter; |
61 | | |
62 | 737 | b_new_pes = false; |
63 | 737 | *pi_continuity_counter = (*pi_continuity_counter+1)%16; |
64 | | |
65 | 737 | if( b_adaptation_field ) |
66 | 737 | { |
67 | 737 | int i_stuffing = 184 - i_copy; |
68 | | |
69 | 737 | p_ts->p_buffer[4] = i_stuffing - 1; |
70 | 737 | if( i_stuffing > 1 ) |
71 | 737 | { |
72 | 737 | p_ts->p_buffer[5] = 0x00; |
73 | 737 | if( *pb_discontinuity ) |
74 | 0 | { |
75 | 0 | p_ts->p_buffer[5] |= 0x80; |
76 | 0 | *pb_discontinuity = false; |
77 | 0 | } |
78 | 118k | for (int i = 6; i < 6 + i_stuffing - 2; i++ ) |
79 | 117k | { |
80 | 117k | p_ts->p_buffer[i] = 0xff; |
81 | 117k | } |
82 | 737 | } |
83 | 737 | } |
84 | | /* copy payload */ |
85 | 737 | memcpy( &p_ts->p_buffer[188 - i_copy], p_data, i_copy ); |
86 | 737 | p_data += i_copy; |
87 | 737 | i_size -= i_copy; |
88 | | |
89 | 737 | pf_callback( p_opaque, p_ts ); |
90 | | |
91 | 737 | if( i_size <= 0 ) |
92 | 737 | { |
93 | 737 | block_t *p_next = p_pes->p_next; |
94 | | |
95 | 737 | p_pes->p_next = NULL; |
96 | 737 | block_Release( p_pes ); |
97 | 737 | if( p_next == NULL ) |
98 | 737 | return; |
99 | | |
100 | 0 | b_new_pes = true; |
101 | 0 | p_pes = p_next; |
102 | 0 | i_size = p_pes->i_buffer; |
103 | 0 | p_data = p_pes->p_buffer; |
104 | 0 | } |
105 | 737 | } |
106 | 737 | } |