mpeg_restamp.c 6.76 KB
Newer Older
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
/*****************************************************************************
 * mpeg_restamp.c: Restamps PCR and PTS/DTS to make them appear continuous
 *****************************************************************************
 * Copyright (C) 2013 VideoLAN
 *
 * Authors: Christophe Massiot <massiot@via.ecp.fr>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject
 * to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *****************************************************************************/

/* Limitation: this supposes the PES header is not fragmented over several
 * TS packets, and that the stream is not scrambled. */

#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <inttypes.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <time.h>

#include <bitstream/mpeg/ts.h>
#include <bitstream/mpeg/pes.h>

#define UINT33_MAX UINT64_C(8589934592)
#define TS_CLOCK_MAX UINT33_MAX
#define CLOCK_FREQ UINT64_C(90000)
#define MAX_PCR_INTERVAL (CLOCK_FREQ / 10)

/*****************************************************************************
 * Local declarations
 *****************************************************************************/
static uint64_t i_last_pcr_date;
static uint64_t i_last_pcr = TS_CLOCK_MAX;
static uint64_t i_pcr_offset = 0;

/*****************************************************************************
 * get_date:
 *****************************************************************************/
static uint64_t get_date(void)
{
    struct timespec ts;

    /* Try to use POSIX monotonic clock if available */
    if (clock_gettime(CLOCK_MONOTONIC, &ts) == EINVAL)
        /* Run-time fallback to real-time clock (always available) */
        (void)clock_gettime(CLOCK_REALTIME, &ts);

    return ((uint64_t)ts.tv_sec * CLOCK_FREQ)
            + ((uint64_t)ts.tv_nsec * (CLOCK_FREQ / 10000) / 100000);
}

/*****************************************************************************
 * handle_pcr:
 *****************************************************************************/
static void handle_pcr(uint8_t *p_ts, uint64_t i_date)
{
    uint64_t i_pcr = tsaf_get_pcr(p_ts);
    bool b_discontinuity = tsaf_has_discontinuity(p_ts);

    if (i_last_pcr == TS_CLOCK_MAX)
        i_last_pcr = i_pcr;
    else {
        /* handle 2^33 wrap-arounds */
        uint64_t i_delta =
            (TS_CLOCK_MAX + i_pcr -
             (i_last_pcr % TS_CLOCK_MAX)) % TS_CLOCK_MAX;
        if (i_delta <= MAX_PCR_INTERVAL && !b_discontinuity)
            i_last_pcr = i_pcr;
        else {
            i_last_pcr += i_date - i_last_pcr_date;
            i_last_pcr %= TS_CLOCK_MAX;
            i_pcr_offset += TS_CLOCK_MAX + i_last_pcr - i_pcr;
            i_pcr_offset %= TS_CLOCK_MAX;
            i_last_pcr = i_pcr;
        }
    }
    i_last_pcr_date = i_date;
    if (!i_pcr_offset)
        return;

    i_pcr += i_pcr_offset;
    i_pcr %= TS_CLOCK_MAX;
    tsaf_set_pcr(p_ts, i_pcr);
    tsaf_clear_discontinuity(p_ts);
}

/*****************************************************************************
 * handle_ts:
 *****************************************************************************/
static uint64_t handle_ts(uint64_t i_ts)
{
    i_ts += i_pcr_offset;
    i_ts %= TS_CLOCK_MAX;
    return i_ts;
}

/*****************************************************************************
 * Main loop
 *****************************************************************************/
static void usage(const char *psz)
{
    fprintf(stderr, "usage: %s [<mtu>] < <input file> [> <output>]\n", psz);
    exit(EXIT_FAILURE);
}

int main(int i_argc, char **ppsz_argv)
{
    if (i_argc > 2 || i_argc < 1 ||
        (!strcmp(ppsz_argv[1], "-h") || !strcmp(ppsz_argv[1], "--help")))
        usage(ppsz_argv[0]);

    unsigned int i_mtu = TS_SIZE;
    if (i_argc == 2) {
        i_mtu = strtoul(ppsz_argv[1], NULL, 0);
        if (!i_mtu)
            usage(ppsz_argv[0]);
    }

    for ( ; ; ) {
        uint8_t p_buffer[i_mtu];
        ssize_t i_read = read(STDIN_FILENO, p_buffer, i_mtu);
        uint64_t date = get_date();
        if (i_read == -1) {
            if (errno == EAGAIN || errno == EWOULDBLOCK)
                continue;
            exit(EXIT_FAILURE);
        }

        if (!i_read)
            exit(EXIT_SUCCESS);

        uint8_t *p_ts = p_buffer;
        while (p_ts < p_buffer + i_mtu && ts_validate(p_ts)) {
            if (ts_has_adaptation(p_ts) && ts_get_adaptation(p_ts) &&
                tsaf_has_pcr(p_ts))
                handle_pcr(p_ts, date);

            uint16_t header_size = TS_HEADER_SIZE +
                                   (ts_has_adaptation(p_ts) ? 1 : 0) +
                                   ts_get_adaptation(p_ts);
            if (ts_get_unitstart(p_ts) && ts_has_payload(p_ts) &&
                header_size + PES_HEADER_SIZE_PTS <= TS_SIZE &&
                pes_validate(p_ts + header_size) &&
                pes_get_streamid(p_ts + header_size) !=
                    PES_STREAM_ID_PRIVATE_2 &&
                pes_validate_header(p_ts + header_size) &&
                pes_has_pts(p_ts + header_size) &&
                pes_validate_pts(p_ts + header_size)) {
                pes_set_pts(p_ts + header_size,
                            handle_ts(pes_get_pts(p_ts + header_size)));

                if (header_size + PES_HEADER_SIZE_PTSDTS <= TS_SIZE &&
                    pes_has_dts(p_ts + header_size) &&
                    pes_validate_dts(p_ts + header_size))
                    pes_set_dts(p_ts + header_size,
                                handle_ts(pes_get_dts(p_ts + header_size)));
            }

            p_ts += TS_SIZE;
        }

        ssize_t i_written = write(STDOUT_FILENO, p_buffer, i_mtu);
        if (i_written == -1) {
            if (errno == EAGAIN || errno == EWOULDBLOCK)
                continue;
            exit(EXIT_FAILURE);
        }
    }

    return EXIT_FAILURE;
}