#include "mpeg-ps.h"
#include "mpeg-types.h"
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>

static FILE* vfp;
static FILE* afp;

inline const char* ftimestamp(int64_t t, char* buf)
{
    if (PTS_NO_VALUE == t)
    {
        sprintf(buf, "(null)");
    }
    else
    {
        t /= 90;
        sprintf(buf, "%d:%02d:%02d.%03d", (int)(t / 3600000), (int)((t / 60000) % 60), (int)((t / 1000) % 60), (int)(t % 1000));
    }
    return buf;
}

static int onpacket(void* /*param*/, int /*stream*/, int avtype, int flags, int64_t pts, int64_t dts, const void* data, size_t bytes)
{
    static char s_pts[64], s_dts[64];

    if (PSI_STREAM_AAC == avtype || PSI_STREAM_AUDIO_G711A == avtype || PSI_STREAM_AUDIO_G711U == avtype)
    {
        static int64_t a_pts = 0, a_dts = 0;
        if (PTS_NO_VALUE == dts)
            dts = pts;
        //assert(0 == a_dts || dts >= a_dts);
        printf("[A] pts: %s(%lld), dts: %s(%lld), diff: %03d/%03d, size: %u\n", ftimestamp(pts, s_pts), pts, ftimestamp(dts, s_dts), dts, (int)(pts - a_pts) / 90, (int)(dts - a_dts) / 90, (unsigned int)bytes);
        a_pts = pts;
        a_dts = dts;

        fwrite(data, 1, bytes, afp);
    }
    else if (PSI_STREAM_H264 == avtype || PSI_STREAM_H265 == avtype || PSI_STREAM_VIDEO_SVAC == avtype)
    {
        static int64_t v_pts = 0, v_dts = 0;
        assert(0 == v_dts || dts >= v_dts);
        printf("[V] pts: %s(%lld), dts: %s(%lld), diff: %03d/%03d, size: %u%s\n", ftimestamp(pts, s_pts), pts, ftimestamp(dts, s_dts), dts, (int)(pts - v_pts) / 90, (int)(dts - v_dts) / 90, (unsigned int)bytes, (flags & MPEG_FLAG_IDR_FRAME) ? " [I]": "");
        v_pts = pts;
        v_dts = dts;

        fwrite(data, 1, bytes, vfp);
    }
    else
    {
        //assert(0);

        static int64_t x_pts = 0, x_dts = 0;
        if (PTS_NO_VALUE == dts)
            dts = pts;
        //assert(0 == x_dts || dts >= x_dts);
        printf("[X] pts: %s(%lld), dts: %s(%lld), diff: %03d/%03d\n", ftimestamp(pts, s_pts), pts, ftimestamp(dts, s_dts), dts, (int)(pts - x_pts), (int)(dts - x_dts));
        x_pts = pts;
        x_dts = dts;
    }

    return 0;
}

static void mpeg_ps_dec_testonstream(void* param, int stream, int codecid, const void* extra, int bytes, int finish)
{
    printf("stream %d, codecid: %d, finish: %s\n", stream, codecid, finish ? "true" : "false");
}

static uint8_t s_packet[2 * 1024 * 1024];

void mpeg_ps_dec_test(const char* file)
{
    FILE* fp = fopen(file, "rb");
    vfp = fopen("v.h264", "wb");
    afp = fopen("a.aac", "wb");

    struct ps_demuxer_notify_t notify = {
        mpeg_ps_dec_testonstream,
    };
    ps_demuxer_t* ps = ps_demuxer_create(onpacket, NULL);
    ps_demuxer_set_notify(ps, &notify, NULL);

    size_t n, i = 0, r = 0;
    while ((n = fread(s_packet + i, 1, sizeof(s_packet) - i, fp)) > 0)
    {
        r = ps_demuxer_input(ps, s_packet, n + i);
        memmove(s_packet, s_packet + r, n + i - r);
        i = n + i - r;
    }
    while (i > 0 && r > 0)
    {
        r = ps_demuxer_input(ps, s_packet, i);
        memmove(s_packet, s_packet + r, i - r);
        i -= r;
    }
    ps_demuxer_destroy(ps);

    fclose(fp);
    fclose(vfp);
    fclose(afp);
}
