Skip to content

Commit

Permalink
feat: pmt program information CUEI
Browse files Browse the repository at this point in the history
  • Loading branch information
ireader committed Mar 31, 2024
1 parent 527c0f5 commit 672cb7b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 11 deletions.
2 changes: 1 addition & 1 deletion libmpeg/source/mpeg-pat.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ size_t pat_read(struct pat_t *pat, const uint8_t* data, size_t bytes)
{
pn = (data[i] << 8) | data[i+1];
pid = ((data[i+2] & 0x1F) << 8) | data[i+3];
// printf("PAT: pn: %0x, pid: %0x\n", (unsigned int)pn, (unsigned int)pid);
// printf("PAT: pn: 0x%0x, pid: 0x%0x\n", (unsigned int)pn, (unsigned int)pid);

if(0 == pn)
continue; // ignore NIT info
Expand Down
38 changes: 36 additions & 2 deletions libmpeg/source/mpeg-pmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,39 @@ static struct pes_t* pmt_fetch(struct pmt_t* pmt, uint16_t pid)
return &pmt->streams[pmt->stream_count++];
}

static int pmt_read_program_descriptor(struct pmt_t* pmt, const uint8_t* data, uint16_t bytes)
{
uint8_t tag;
uint8_t len;
uint8_t channels;

// Registration descriptor
while (bytes > 2)
{
tag = data[0];
len = data[1];
if (len + 2 > bytes)
return -1; // invalid len

// ISO/IEC 13818-1:2018 (E) Table 2-45 Program and program element descriptors (p90)
switch (tag)
{
case 0x05: // 2.6.8 Registration descriptor(p94)
if (len >= 4 && 'C' == data[2] && 'U' == data[3] && 'E' == data[4] && 'I' == data[5])
{
memcpy(pmt->proginfo, data+2, 4);
}
break;
}

data += len + 2;
bytes -= len + 2;
}
assert(0 == bytes);

return 0;
}

static int pmt_read_descriptor(struct pes_t* stream, const uint8_t* data, uint16_t bytes)
{
uint8_t tag;
Expand All @@ -51,7 +84,7 @@ static int pmt_read_descriptor(struct pes_t* stream, const uint8_t* data, uint16
assert(PSI_STREAM_PRIVATE_DATA == stream->codecid);
stream->codecid = PSI_STREAM_AUDIO_OPUS;
}
if (len >= 4 && 'A' == data[2] && 'V' == data[3] && '0' == data[4] && '1' == data[5])
else if (len >= 4 && 'A' == data[2] && 'V' == data[3] && '0' == data[4] && '1' == data[5])
{
// https://aomediacodec.github.io/av1-mpeg2-ts/
// Constraints on AV1 streams in MPEG-2 TS
Expand Down Expand Up @@ -154,6 +187,7 @@ size_t pmt_read(struct pmt_t *pmt, const uint8_t* data, size_t bytes)
if(program_info_length > 2)
{
// descriptor(data + 12, program_info_length)
pmt_read_program_descriptor(pmt, data + 12, program_info_length);
}

PMT_VERSION_CHANGE:
Expand All @@ -162,7 +196,7 @@ size_t pmt_read(struct pmt_t *pmt, const uint8_t* data, size_t bytes)
{
pid = ((data[i+1] & 0x1F) << 8) | data[i+2];
len = ((data[i+3] & 0x0F) << 8) | data[i+4];
// printf("PMT: pn: %0x, pid: %0x, codec: %0x, eslen: %d\n", (unsigned int)pmt->pn, (unsigned int)pid, (unsigned int)data[i], (unsigned int)len);
//printf("PMT: pn: 0x%0x, pid: 0x%0x, codec: 0x%0x, eslen: %d\n", (unsigned int)pmt->pn, (unsigned int)pid, (unsigned int)data[i], (unsigned int)len);

if (i + len + 5 > section_length + 3 - 4/*CRC32*/)
break; // mark error ?
Expand Down
4 changes: 2 additions & 2 deletions libmpeg/source/mpeg-ts-dec.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,12 @@ int ts_demuxer_input(struct ts_demuxer_t* ts, const uint8_t* data, size_t bytes)
}
else if (!pes->have_pes_header)
{
continue; // don't have pes header yet
return 0; // ignore, don't have pes header yet
}

r = pes_packet(&pes->pkt, pes, data + i, bytes - i, &consume, pkhd.payload_unit_start_indicator, ts->onpacket, ts->param);
pes->have_pes_header = (r || (0 == pes->pkt.size && pes->len > 0)) ? 0 : 1; // packet completed
break; // find stream
return r; // find stream
}
} // PMT handler
}
Expand Down
1 change: 1 addition & 0 deletions libmpeg/source/mpeg-ts-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ struct pmt_t

char provider[64];
char name[64];
char proginfo[4]; // CUEI

unsigned int stream_count;
struct pes_t streams[4];
Expand Down
2 changes: 1 addition & 1 deletion libmpeg/test/mpeg-ts-dec-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static int on_ts_packet(void* /*param*/, int program, int stream, int avtype, in
{
static int64_t x_pts = 0, x_dts = 0;
//assert(0 == x_dts || dts >= x_dts);
printf("[%d][%d:%d] pts: %s(%lld), dts: %s(%lld), diff: %03d/%03d%s%s%s\n", avtype, program, stream, ftimestamp(pts, s_pts), pts, ftimestamp(dts, s_dts), dts, (int)(pts - x_pts) / 90, (int)(dts - x_dts) / 90, flags & MPEG_FLAG_IDR_FRAME ? " [I]" : "", flags & MPEG_FLAG_PACKET_CORRUPT ? " [X]" : "", flags & MPEG_FLAG_PACKET_LOST ? " [-]" : "");
printf("[%d][%d:%d] pts: %s(%lld), dts: %s(%lld), diff: %03d/%03d, bytes: %u%s%s%s\n", avtype, program, stream, ftimestamp(pts, s_pts), pts, ftimestamp(dts, s_dts), dts, (int)(pts - x_pts) / 90, (int)(dts - x_dts) / 90, (unsigned int)bytes, flags & MPEG_FLAG_IDR_FRAME ? " [I]" : "", flags & MPEG_FLAG_PACKET_CORRUPT ? " [X]" : "", flags & MPEG_FLAG_PACKET_LOST ? " [-]" : "");
x_pts = pts;
x_dts = dts;
//assert(0);
Expand Down
10 changes: 5 additions & 5 deletions libmpeg/test/mpeg-ts-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,23 @@ inline const char* ts_type(int type)
}
}

static int ts_stream(void* ts, int codecid)
static int ts_stream(void* ts, int stream, int codecid)
{
static std::map<int, int> streams;
std::map<int, int>::const_iterator it = streams.find(codecid);
std::map<int, int>::const_iterator it = streams.find(stream);
if (streams.end() != it)
return it->second;

int i = mpeg_ts_add_stream(ts, codecid, NULL, 0);
streams[codecid] = i;
streams[stream] = i;
return i;
}

static int on_ts_packet(void* ts, int program, int stream, int avtype, int flags, int64_t pts, int64_t dts, const void* data, size_t bytes)
{
printf("[%s] pts: %08lu, dts: %08lu%s\n", ts_type(avtype), (unsigned long)pts, (unsigned long)dts, flags ? " [I]":"");
printf("[%d:%d][%s] pts: %08lu, dts: %08lu%s\n", program, stream, ts_type(avtype), (unsigned long)pts, (unsigned long)dts, flags ? " [I]":"");

return mpeg_ts_write(ts, ts_stream(ts, avtype), flags, pts, dts, data, bytes);
return mpeg_ts_write(ts, ts_stream(ts, stream, avtype), flags, pts, dts, data, bytes);
}

static void mpeg_ts_file(const char* file, void* muxer)
Expand Down

0 comments on commit 672cb7b

Please sign in to comment.