Create values for packets with AV_NOPTS_VALUE. Closes #197

This commit is contained in:
Mr-Dave
2025-02-01 23:46:16 -07:00
parent 735cb8feb2
commit 1b9c6870f0
3 changed files with 71 additions and 21 deletions

View File

@@ -770,28 +770,41 @@ int cls_movie::passthru_pktpts()
if (pkt->pts != AV_NOPTS_VALUE) {
if (pkt->pts < base_pdts) {
ts_interval = 0;
MOTPLS_LOG(ERR, TYPE_ENCODER, NO_ERRNO, "Invalid pkt->pts");
return -1;
} else {
ts_interval = pkt->pts - base_pdts;
}
pkt->pts = av_rescale_q(ts_interval
, netcam_data->transfer_format->streams[indx]->time_base, tmpbase);
} else {
MOTPLS_LOG(ERR, TYPE_ENCODER, NO_ERRNO, "pkt->pts is AV_NOPTS_VALUE");
return -1;
}
if (pkt->dts != AV_NOPTS_VALUE) {
if (pkt->dts < base_pdts) {
ts_interval = 0;
MOTPLS_LOG(ERR, TYPE_ENCODER, NO_ERRNO, "pkt->dts is invalid");
return -1;
} else {
ts_interval = pkt->dts - base_pdts;
}
pkt->dts = av_rescale_q(ts_interval
, netcam_data->transfer_format->streams[indx]->time_base, tmpbase);
} else {
MOTPLS_LOG(ERR, TYPE_ENCODER, NO_ERRNO, "pkt->dts is AV_NOPTS_VALUE");
return -1;
}
ts_interval = pkt->duration;
pkt->duration = av_rescale_q(ts_interval
, netcam_data->transfer_format->streams[indx]->time_base, tmpbase);
if ((pkt->pts == AV_NOPTS_VALUE) || (pkt->dts == AV_NOPTS_VALUE)) {
MOTPLS_LOG(ERR, TYPE_ENCODER, NO_ERRNO, "pkt->dts or pkt->pts is AV_NOPTS_VALUE");
return -1;
}
/*
MOTPLS_LOG(INF, TYPE_ENCODER, NO_ERRNO
,_("base PTS %" PRId64 " new PTS %" PRId64 " srcbase %d-%d newbase %d-%d")

View File

@@ -1322,6 +1322,50 @@ int cls_netcam::resize()
return 0;
}
void cls_netcam::pkt_ts()
{
int64_t usec_ltncy;
AVRational tbase;
struct timespec tmp_tm;
if (connection_pts == -1) {
if (packet_recv->pts == AV_NOPTS_VALUE) {
connection_pts = 0;
} else {
connection_pts = packet_recv->pts;
}
clock_gettime(CLOCK_MONOTONIC, &connection_tm);
}
if (packet_recv->pts == AV_NOPTS_VALUE) {
clock_gettime(CLOCK_MONOTONIC, &tmp_tm);
tbase = format_context->streams[packet_recv->stream_index]->time_base;
if (tbase.num == 0) {
tbase.num = 1;
}
usec_ltncy = (((tmp_tm.tv_sec - connection_tm.tv_sec) * 1000000) +
((tmp_tm.tv_nsec - connection_tm.tv_nsec) / 1000));
if (usec_ltncy < 0) {
MOTPLS_LOG(ERR, TYPE_NETCAM, NO_ERRNO
,_("%s:Latency calculation error ")
, cameratype.c_str());
usec_ltncy = 0;
}
packet_recv->pts = connection_pts
+ av_rescale(usec_ltncy, tbase.den/tbase.num, 1000000);
}
if (packet_recv->dts == AV_NOPTS_VALUE) {
packet_recv->dts = packet_recv->pts;
}
last_pts = packet_recv->pts;
}
int cls_netcam::read_image()
{
int size_decoded, retcd, errcnt, nodata;
@@ -1403,7 +1447,8 @@ int cls_netcam::read_image()
clock_gettime(CLOCK_MONOTONIC, &ist_tm);
clock_gettime(CLOCK_MONOTONIC, &img_recv->image_time);
last_stream_index = packet_recv->stream_index;
last_pts = packet_recv->pts;
pkt_ts();
if (!first_image) {
status = NETCAM_CONNECTED;
@@ -1672,7 +1717,7 @@ void cls_netcam::set_parms ()
swsframe_size = 0;
hw_type = AV_HWDEVICE_TYPE_NONE;
hw_pix_fmt = AV_PIX_FMT_NONE;
connection_pts = 0;
connection_pts = -1;
last_pts = 0;
filenbr = 0;
filelist.clear();
@@ -1901,8 +1946,6 @@ int cls_netcam::open_context()
return -1;
}
connection_pts = AV_NOPTS_VALUE;
return 0;
}
@@ -1996,18 +2039,11 @@ void cls_netcam::handler_wait()
/* Adjust to clock and pts timer */
if (pts_adj == true) {
if (connection_pts == AV_NOPTS_VALUE) {
connection_pts = last_pts;
clock_gettime(CLOCK_MONOTONIC, &connection_tm);
return;
}
if (last_pts != AV_NOPTS_VALUE) {
usec_ltncy +=
+ (av_rescale(last_pts, 1000000, tbase.den/tbase.num)
- av_rescale(connection_pts, 1000000, tbase.den/tbase.num))
-(((tmp_tm.tv_sec - connection_tm.tv_sec) * 1000000) +
((tmp_tm.tv_nsec - connection_tm.tv_nsec) / 1000));
}
usec_ltncy +=
+ (av_rescale(last_pts, 1000000, tbase.den/tbase.num)
- av_rescale(connection_pts, 1000000, tbase.den/tbase.num))
-(((tmp_tm.tv_sec - connection_tm.tv_sec) * 1000000) +
((tmp_tm.tv_nsec - connection_tm.tv_nsec) / 1000));
}
if ((usec_ltncy > 0) && (usec_ltncy < 1000000L)) {

View File

@@ -91,8 +91,8 @@ class cls_netcam {
AVFormatContext *transfer_format; /* Format context just for transferring to pass-through */
ctx_packet_item *pktarray; /* Pointer to array of packets for passthru processing */
int pktarray_size; /* The number of packets in array. 1 based */
int video_stream_index; /* Stream index associated with video from camera */
int audio_stream_index; /* Stream index associated with video from camera */
int video_stream_index; /* Stream index associated with video from camera */
int audio_stream_index; /* Stream index associated with audio from camera */
bool handler_stop;
bool handler_running;
@@ -118,7 +118,6 @@ class cls_netcam {
int64_t idnbr; /* A ID number to track the packet vs image */
AVDictionary *opts; /* AVOptions when opening the format context */
int swsframe_size; /* The size of the image after resizing */
int last_stream_index; /* Index of the last packet read */
enum AVHWDeviceType hw_type;
enum AVPixelFormat hw_pix_fmt;
@@ -145,6 +144,7 @@ class cls_netcam {
struct timespec connection_tm; /* Time when camera was connected*/
int64_t connection_pts; /* PTS from the connection */
int64_t last_pts; /* PTS from the last packet read */
int last_stream_index; /* Stream index for last packet */
bool pts_adj; /* Bool for whether to use pts for timing */
struct timespec frame_prev_tm; /* The time set before calling the av functions */
@@ -191,6 +191,7 @@ class cls_netcam {
int open_codec();
int open_sws();
int resize();
void pkt_ts();
int read_image();
int ntc();
void set_options();