ireader / media-server

RTSP/RTP/RTMP/FLV/HLS/MPEG-TS/MPEG-PS/MPEG-DASH/MP4/fMP4/MKV/WebM
MIT License
3.06k stars 1.07k forks source link

mov-writer-h264.cpp 中h264_handler 实现和mov-writer-h265.cpp的265_handler实现区别 #287

Closed menglingbo closed 1 year ago

menglingbo commented 1 year ago

h264_handler我按照265的实现方式如下,是否可行呢? static void h264_handler(void param, const uint8_t nalu, size_t bytes) { struct mov_h264_test_t ctx = (struct mov_h264_test_t)param; static uint8_t startcode[] = {0x00, 0x00, 0x00, 0x01}; static int i = 0; static int j = 0; uint8_t nalutype = nalu[0] & 0x1f; if (ctx->vcl > 0 && h264_is_new_access_unit((const uint8_t*)nalu, bytes)) { int r = h264_write(ctx, ctx->buf, ctx->bytes); if (-1 == r) return; // wait for more data if ((j++) % 25 == 0) i = (i + 1) % ctx->vcl; ctx->bytes = 0; ctx->vcl = 0; }

    if (1 <= nalutype && nalutype <= 5)
    {
    ++ctx->vcl;
    if (1 == ctx->vcl || ctx->vcl == i)
            {
                    memcpy(ctx->buf + ctx->bytes, startcode, sizeof(startcode));
                    ctx->bytes += sizeof(startcode);
                    memcpy(ctx->buf + ctx->bytes, nalu, bytes);
                    ctx->bytes += bytes;
            }
    }
    else
    {
            memcpy(ctx->buf + ctx->bytes, startcode, sizeof(startcode));
            ctx->bytes += sizeof(startcode);
            memcpy(ctx->buf + ctx->bytes, nalu, bytes);
            ctx->bytes += bytes;
    }

} h264原来实现: static void h264_handler(void param, const uint8_t nalu, size_t bytes) { struct mov_h264_test_t ctx = (struct mov_h264_test_t)param; assert(ctx->ptr < nalu);

const uint8_t* ptr = nalu - 3;

// const uint8_t end = (const uint8_t)nalu + bytes; uint8_t nalutype = nalu[0] & 0x1f; if (ctx->vcl > 0 && h264_is_new_access_unit((const uint8_t*)nalu, bytes)) { int r = h264_write(ctx, ctx->ptr, ptr - ctx->ptr); if (-1 == r) return; // wait for more data

    ctx->ptr = ptr;
    ctx->vcl = 0;
}

if (1 <= nalutype && nalutype <= 5)
    ++ctx->vcl;

} h265实现: static void h265_handler(void param, const uint8_t nalu, size_t bytes) { static int i = 0; static int j = 0; static uint8_t startcode[] = {0x00, 0x00, 0x00, 0x01};

struct mov_h265_test_t* ctx = (struct mov_h265_test_t*)param;
assert(ctx->ptr < nalu);

const uint8_t* ptr = nalu - 3;

// const uint8_t end = (const uint8_t)nalu + bytes; uint8_t nalutype = (nalu[0] >> 1) & 0x3f; if (ctx->vcl > 0 && h265_is_new_access_unit((const uint8_t*)nalu, bytes)) { //int r = h265_write(ctx, ctx->ptr, ptr - ctx->ptr); int r = h265_write(ctx, ctx->buf, ctx->bytes); if (-1 == r) return; // wait for more data

    if ((j++) % 25 == 0)
        i = (i + 1) % ctx->vcl;
    ctx->bytes = 0;
    printf("\n");

    ctx->ptr = ptr;
    ctx->vcl = 0;
}

if (nalutype <= 31)
{
    ++ctx->vcl;

    if (1 == ctx->vcl || ctx->vcl == i)
    {
        printf("ctx->vcl: %d ", ctx->vcl);
        memcpy(ctx->buf + ctx->bytes, startcode, sizeof(startcode));
        ctx->bytes += sizeof(startcode);
        memcpy(ctx->buf + ctx->bytes, nalu, bytes);
        ctx->bytes += bytes;
    }
}
else
{
    memcpy(ctx->buf + ctx->bytes, startcode, sizeof(startcode));
    ctx->bytes += sizeof(startcode);
    memcpy(ctx->buf + ctx->bytes, nalu, bytes);
    ctx->bytes += bytes;
}

}

ireader commented 1 year ago

H265实现是为了测试别的功能误提交的代码, 265的裸流参照H264实现即可。

ireader commented 1 year ago
static void h265_handler(void* param, const uint8_t* nalu, size_t bytes)
{
    static int i = 0;
    static int j = 0;
    static uint8_t startcode[] = {0x00, 0x00, 0x00, 0x01};

    struct mov_h265_test_t* ctx = (struct mov_h265_test_t*)param;
    assert(ctx->ptr < nalu);

    const uint8_t* ptr = nalu - 3;
//  const uint8_t* end = (const uint8_t*)nalu + bytes;
    uint8_t nalutype = (nalu[0] >> 1) & 0x3f;
    if (ctx->vcl > 0 && h265_is_new_access_unit((const uint8_t*)nalu, bytes))
    {
        int r = h265_write(ctx, ctx->ptr, ptr - ctx->ptr);
        if (-1 == r)
            return; // wait for more data

        ctx->ptr = ptr;
        ctx->vcl = 0;
    }

    if (nalutype <= 31)
        ++ctx->vcl;
}