1 2 3 4 5 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
/*****************************************************************************
* ingests.c: create the aux file for a transport stream file
*****************************************************************************
* Copyright (C) 2009, 2011, 2015 VideoLAN
* $Id$
*
* Authors: Christophe Massiot <massiot@via.ecp.fr>
*
* 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <syslog.h>
#include <bitstream/mpeg/ts.h>
#include "util.h"
/*****************************************************************************
* Local declarations
*****************************************************************************/
#define READ_ONCE 100
#define MAX_PCR_GAP (500ULL * 27000ULL) /* that's 500 ms */
#define POW2_33 8589934592ULL
static uint16_t i_pcr_pid = 0;
static size_t i_ts_in_payload = DEFAULT_PAYLOAD_SIZE / TS_SIZE;
static int i_fd;
static FILE *p_output_aux;
static int i_ts_read = 0;
static bool b_init = true;
static int i_ts_since_output = 0;
static uint64_t i_last_pcr = POW2_33 * 300;
static uint64_t i_last_pcr_diff = 0;
static int i_last_nb_payloads = 0;
static uint64_t i_last_stc = 0;
static void usage(void)
{
msg_Raw( NULL, "Usage: ingests [-l <syslogtag>] -p <PCR PID> [-m <payload size>] <input ts>" );
exit(EXIT_FAILURE);
}
/*****************************************************************************
* OutputAux: date payload packets
*****************************************************************************/
static void OutputAux( int i_nb_payloads, uint64_t i_duration )
{
uint8_t p_aux[i_nb_payloads*sizeof(uint64_t)];
int i;
for ( i = 0; i < i_nb_payloads; i++ )
{
uint64_t i_stc = i_last_stc + i_duration * (i + 1) / i_nb_payloads;
p_aux[8 * i + 0] = i_stc >> 56;
p_aux[8 * i + 1] = (i_stc >> 48) & 0xff;
p_aux[8 * i + 2] = (i_stc >> 40) & 0xff;
p_aux[8 * i + 3] = (i_stc >> 32) & 0xff;
p_aux[8 * i + 4] = (i_stc >> 24) & 0xff;
p_aux[8 * i + 5] = (i_stc >> 16) & 0xff;
p_aux[8 * i + 6] = (i_stc >> 8) & 0xff;
p_aux[8 * i + 7] = (i_stc >> 0) & 0xff;
}
i_last_stc += i_duration;
if ( fwrite( p_aux, 8, i_nb_payloads, p_output_aux ) != i_nb_payloads )
msg_Err( NULL, "couldn't write to auxiliary file" );
}
/*****************************************************************************
* Output: date as many payload packets as possible
*****************************************************************************/
static void Output(void)
{
int i_nb_payloads = (i_ts_since_output + i_ts_in_payload - 1)
/ i_ts_in_payload;
if ( b_init )
{
/* Emulate CBR */
OutputAux( i_last_nb_payloads,
i_last_pcr_diff * i_last_nb_payloads / i_nb_payloads );
b_init = false;
}
OutputAux( i_nb_payloads, i_last_pcr_diff );
i_ts_since_output -= i_nb_payloads * i_ts_in_payload;
i_last_nb_payloads = i_nb_payloads;
}
/*****************************************************************************
* OutputFirst: manipulate structures to emulate CBR at the beginning
*****************************************************************************/
static void OutputFirst(void)
{
i_last_nb_payloads = (i_ts_since_output + i_ts_in_payload - 1)
/ i_ts_in_payload;
i_ts_since_output -= i_last_nb_payloads * i_ts_in_payload;
}
/*****************************************************************************
* OutputLast: emulate CBR at the end
*****************************************************************************/
static void OutputLast(void)
{
int i_nb_payloads = (i_ts_since_output + i_ts_in_payload - 1)
/ i_ts_in_payload;
OutputAux( i_nb_payloads,
i_last_pcr_diff * i_nb_payloads / i_last_nb_payloads );
}
/*****************************************************************************
* TSHandle: find a PCR and stamp packets
*****************************************************************************/
static void TSHandle( uint8_t *p_ts )
{
uint16_t i_pid = ts_get_pid( p_ts );
if ( !ts_validate( p_ts ) )
{
msg_Err( NULL, "lost TS synchro, go and fix your file (pos=%llu)",
(uint64_t)i_ts_read * TS_SIZE );
exit(EXIT_FAILURE);
}
i_ts_since_output++;
int i_nb_payloads = (i_ts_since_output + i_ts_in_payload - 1)
/ i_ts_in_payload;
if ( i_nb_payloads <= 0 )
return;
if ( (i_pid == i_pcr_pid || i_pcr_pid == 8192)
&& ts_has_adaptation(p_ts) && ts_get_adaptation(p_ts)
&& tsaf_has_pcr(p_ts) )
{
uint64_t i_pcr = tsaf_get_pcr( p_ts ) * 300 + tsaf_get_pcrext( p_ts );
if ( i_last_pcr == POW2_33 * 300 ) /* init */
{
i_last_pcr = i_pcr;
OutputFirst();
return;
}
if ( (POW2_33 * 300 + i_pcr) - i_last_pcr < MAX_PCR_GAP )
/* Clock wrapped */
i_last_pcr_diff = POW2_33 * 300 + i_pcr - i_last_pcr;
else if ( (i_pcr < i_last_pcr) ||
(i_pcr - i_last_pcr > MAX_PCR_GAP) )
/* Do not change the slope - consider CBR */
msg_Warn( NULL, "PCR discontinuity (%llu->%llu, pos=%llu)",
i_last_pcr, i_pcr, (uint64_t)i_ts_read * TS_SIZE );
else
i_last_pcr_diff = i_pcr - i_last_pcr;
i_last_pcr = i_pcr;
Output();
}
}
/*****************************************************************************
* Entry point
*****************************************************************************/
int main( int i_argc, char **pp_argv )
{
const char *psz_syslog_tag = NULL;
uint8_t *p_buffer;
unsigned int i_payload_size = DEFAULT_PAYLOAD_SIZE;
mode_t i_mode;
for ( ; ; )
{
int c;
if ( (c = getopt(i_argc, pp_argv, "l:p:m:h")) == -1 )
break;
switch ( c )
{
case 'l':
psz_syslog_tag = optarg;
break;
case 'p':
i_pcr_pid = strtoul(optarg, NULL, 0);
break;
case 'm':
i_payload_size = strtoul(optarg, NULL, 0);
i_ts_in_payload = i_payload_size / TS_SIZE;
if ( i_payload_size % TS_SIZE )
{
msg_Err( NULL, "payload size must be a multiple of 188" );
exit(EXIT_FAILURE);
}
break;
case 'h':
default:
usage();
break;
}
}
if ( optind >= i_argc || !i_pcr_pid )
usage();
if ( psz_syslog_tag != NULL )
msg_Openlog( psz_syslog_tag, LOG_NDELAY, LOG_USER );
i_mode = StatFile( pp_argv[optind] );
if ( S_ISCHR( i_mode ) || S_ISFIFO( i_mode ) || S_ISDIR( i_mode ) )
usage();
i_fd = OpenFile( pp_argv[optind], true, false );
char *psz_aux_file = GetAuxFile( pp_argv[optind], i_payload_size );
p_output_aux = OpenAuxFile( psz_aux_file, false, false );
free( psz_aux_file );
p_buffer = malloc( TS_SIZE * READ_ONCE );
for ( ; ; )
{
int i;
ssize_t i_ret;
if ( (i_ret = read( i_fd, p_buffer, TS_SIZE * READ_ONCE )) < 0 )
{
msg_Err( NULL, "read error (%s)", strerror(errno) );
break;
}
if ( i_ret == 0 )
{
msg_Dbg( NULL, "end of file reached" );
break;
}
for ( i = 0; i < i_ret / TS_SIZE; i++ )
{
TSHandle( p_buffer + TS_SIZE * i );
i_ts_read++;
}
}
free( p_buffer );
if ( !i_last_pcr_diff )
msg_Err( NULL, "no PCR found" );
else
OutputLast(); /* Emulate CBR */
fclose( p_output_aux );
close( i_fd );
if ( psz_syslog_tag != NULL )
msg_Closelog();
return 0;
}
|