Skip to content

Commit 672cb7b

Browse files
committed
feat: pmt program information CUEI
1 parent 527c0f5 commit 672cb7b

File tree

6 files changed

+46
-11
lines changed

6 files changed

+46
-11
lines changed

libmpeg/source/mpeg-pat.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ size_t pat_read(struct pat_t *pat, const uint8_t* data, size_t bytes)
105105
{
106106
pn = (data[i] << 8) | data[i+1];
107107
pid = ((data[i+2] & 0x1F) << 8) | data[i+3];
108-
// printf("PAT: pn: %0x, pid: %0x\n", (unsigned int)pn, (unsigned int)pid);
108+
// printf("PAT: pn: 0x%0x, pid: 0x%0x\n", (unsigned int)pn, (unsigned int)pid);
109109

110110
if(0 == pn)
111111
continue; // ignore NIT info

libmpeg/source/mpeg-pmt.c

+36-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,39 @@ static struct pes_t* pmt_fetch(struct pmt_t* pmt, uint16_t pid)
2828
return &pmt->streams[pmt->stream_count++];
2929
}
3030

31+
static int pmt_read_program_descriptor(struct pmt_t* pmt, const uint8_t* data, uint16_t bytes)
32+
{
33+
uint8_t tag;
34+
uint8_t len;
35+
uint8_t channels;
36+
37+
// Registration descriptor
38+
while (bytes > 2)
39+
{
40+
tag = data[0];
41+
len = data[1];
42+
if (len + 2 > bytes)
43+
return -1; // invalid len
44+
45+
// ISO/IEC 13818-1:2018 (E) Table 2-45 Program and program element descriptors (p90)
46+
switch (tag)
47+
{
48+
case 0x05: // 2.6.8 Registration descriptor(p94)
49+
if (len >= 4 && 'C' == data[2] && 'U' == data[3] && 'E' == data[4] && 'I' == data[5])
50+
{
51+
memcpy(pmt->proginfo, data+2, 4);
52+
}
53+
break;
54+
}
55+
56+
data += len + 2;
57+
bytes -= len + 2;
58+
}
59+
assert(0 == bytes);
60+
61+
return 0;
62+
}
63+
3164
static int pmt_read_descriptor(struct pes_t* stream, const uint8_t* data, uint16_t bytes)
3265
{
3366
uint8_t tag;
@@ -51,7 +84,7 @@ static int pmt_read_descriptor(struct pes_t* stream, const uint8_t* data, uint16
5184
assert(PSI_STREAM_PRIVATE_DATA == stream->codecid);
5285
stream->codecid = PSI_STREAM_AUDIO_OPUS;
5386
}
54-
if (len >= 4 && 'A' == data[2] && 'V' == data[3] && '0' == data[4] && '1' == data[5])
87+
else if (len >= 4 && 'A' == data[2] && 'V' == data[3] && '0' == data[4] && '1' == data[5])
5588
{
5689
// https://aomediacodec.github.io/av1-mpeg2-ts/
5790
// Constraints on AV1 streams in MPEG-2 TS
@@ -154,6 +187,7 @@ size_t pmt_read(struct pmt_t *pmt, const uint8_t* data, size_t bytes)
154187
if(program_info_length > 2)
155188
{
156189
// descriptor(data + 12, program_info_length)
190+
pmt_read_program_descriptor(pmt, data + 12, program_info_length);
157191
}
158192

159193
PMT_VERSION_CHANGE:
@@ -162,7 +196,7 @@ size_t pmt_read(struct pmt_t *pmt, const uint8_t* data, size_t bytes)
162196
{
163197
pid = ((data[i+1] & 0x1F) << 8) | data[i+2];
164198
len = ((data[i+3] & 0x0F) << 8) | data[i+4];
165-
// 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);
199+
//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);
166200

167201
if (i + len + 5 > section_length + 3 - 4/*CRC32*/)
168202
break; // mark error ?

libmpeg/source/mpeg-ts-dec.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,12 @@ int ts_demuxer_input(struct ts_demuxer_t* ts, const uint8_t* data, size_t bytes)
268268
}
269269
else if (!pes->have_pes_header)
270270
{
271-
continue; // don't have pes header yet
271+
return 0; // ignore, don't have pes header yet
272272
}
273273

274274
r = pes_packet(&pes->pkt, pes, data + i, bytes - i, &consume, pkhd.payload_unit_start_indicator, ts->onpacket, ts->param);
275275
pes->have_pes_header = (r || (0 == pes->pkt.size && pes->len > 0)) ? 0 : 1; // packet completed
276-
break; // find stream
276+
return r; // find stream
277277
}
278278
} // PMT handler
279279
}

libmpeg/source/mpeg-ts-internal.h

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ struct pmt_t
7171

7272
char provider[64];
7373
char name[64];
74+
char proginfo[4]; // CUEI
7475

7576
unsigned int stream_count;
7677
struct pes_t streams[4];

libmpeg/test/mpeg-ts-dec-test.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static int on_ts_packet(void* /*param*/, int program, int stream, int avtype, in
6262
{
6363
static int64_t x_pts = 0, x_dts = 0;
6464
//assert(0 == x_dts || dts >= x_dts);
65-
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 ? " [-]" : "");
65+
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 ? " [-]" : "");
6666
x_pts = pts;
6767
x_dts = dts;
6868
//assert(0);

libmpeg/test/mpeg-ts-test.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,23 @@ inline const char* ts_type(int type)
3535
}
3636
}
3737

38-
static int ts_stream(void* ts, int codecid)
38+
static int ts_stream(void* ts, int stream, int codecid)
3939
{
4040
static std::map<int, int> streams;
41-
std::map<int, int>::const_iterator it = streams.find(codecid);
41+
std::map<int, int>::const_iterator it = streams.find(stream);
4242
if (streams.end() != it)
4343
return it->second;
4444

4545
int i = mpeg_ts_add_stream(ts, codecid, NULL, 0);
46-
streams[codecid] = i;
46+
streams[stream] = i;
4747
return i;
4848
}
4949

5050
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)
5151
{
52-
printf("[%s] pts: %08lu, dts: %08lu%s\n", ts_type(avtype), (unsigned long)pts, (unsigned long)dts, flags ? " [I]":"");
52+
printf("[%d:%d][%s] pts: %08lu, dts: %08lu%s\n", program, stream, ts_type(avtype), (unsigned long)pts, (unsigned long)dts, flags ? " [I]":"");
5353

54-
return mpeg_ts_write(ts, ts_stream(ts, avtype), flags, pts, dts, data, bytes);
54+
return mpeg_ts_write(ts, ts_stream(ts, stream, avtype), flags, pts, dts, data, bytes);
5555
}
5656

5757
static void mpeg_ts_file(const char* file, void* muxer)

0 commit comments

Comments
 (0)