ingests.c 8.22 KB
Newer Older
Christophe Massiot's avatar
Christophe Massiot committed
1 2 3
/*****************************************************************************
 * ingests.c: create the aux file for a transport stream file
 *****************************************************************************
4
 * Copyright (C) 2009, 2011 VideoLAN
5
 * $Id$
Christophe Massiot's avatar
Christophe Massiot committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * 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>
26 27
#include <sys/types.h>
#include <sys/stat.h>
Christophe Massiot's avatar
Christophe Massiot committed
28 29 30 31 32 33
#include <unistd.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>

34 35
#include <bitstream/mpeg/ts.h>

Christophe Massiot's avatar
Christophe Massiot committed
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
#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 -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 )
{
Rémi Duraffort's avatar
Rémi Duraffort committed
71
    uint8_t p_aux[i_nb_payloads*sizeof(uint64_t)];
Christophe Massiot's avatar
Christophe Massiot committed
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
    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 ( i_ts_since_output <= 0 )
        return;

    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;
124
    i_ts_since_output -= i_last_nb_payloads * i_ts_in_payload;
Christophe Massiot's avatar
Christophe Massiot committed
125 126 127
}

/*****************************************************************************
128
 * OutputLast: emulate CBR at the end
Christophe Massiot's avatar
Christophe Massiot committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142
 *****************************************************************************/
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 )
{
143
    uint16_t i_pid = ts_get_pid( p_ts );
Christophe Massiot's avatar
Christophe Massiot committed
144

145
    if ( !ts_validate( p_ts ) )
Christophe Massiot's avatar
Christophe Massiot committed
146 147 148 149 150 151 152 153
    {
        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++;

154 155 156
    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) )
Christophe Massiot's avatar
Christophe Massiot committed
157
    {
158
        uint64_t i_pcr = tsaf_get_pcr( p_ts ) * 300 + tsaf_get_pcrext( p_ts );
Christophe Massiot's avatar
Christophe Massiot committed
159 160 161 162 163 164 165 166 167 168

        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;
169
        else if ( (i_pcr < i_last_pcr) ||
Christophe Massiot's avatar
Christophe Massiot committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
                  (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 )
{
    uint8_t *p_buffer;
188 189
    unsigned int i_payload_size = DEFAULT_PAYLOAD_SIZE;
    mode_t i_mode;
Christophe Massiot's avatar
Christophe Massiot committed
190 191 192

    for ( ; ; )
    {
193
        int c;
Christophe Massiot's avatar
Christophe Massiot committed
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

        if ( (c = getopt(i_argc, pp_argv, "p:m:h")) == -1 )
            break;

        switch ( c )
        {
        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();

223 224
    i_mode = StatFile( pp_argv[optind] );
    if ( S_ISCHR( i_mode ) || S_ISFIFO( i_mode ) || S_ISDIR( i_mode ) )
Christophe Massiot's avatar
Christophe Massiot committed
225
        usage();
226 227 228 229 230
    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 );
Christophe Massiot's avatar
Christophe Massiot committed
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

    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++;
        }
    }

Rémi Duraffort's avatar
Rémi Duraffort committed
257
    free( p_buffer );
Christophe Massiot's avatar
Christophe Massiot committed
258 259 260 261 262 263 264 265 266 267
    if ( !i_last_pcr_diff )
        msg_Err( NULL, "no PCR found" );
    else
        OutputLast(); /* Emulate CBR */
    fclose( p_output_aux );
    close( i_fd );

    return 0;
}