summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
Diffstat (limited to 'modules')
-rw-r--r--modules/avcodec/encode.c31
-rw-r--r--modules/gst_video1/encode.c16
-rw-r--r--modules/h265/encode.c16
-rw-r--r--modules/v4l2_codec/v4l2_codec.c14
-rw-r--r--modules/vidloop/vidloop.c6
-rw-r--r--modules/vp8/encode.c12
-rw-r--r--modules/vp9/encode.c21
7 files changed, 86 insertions, 30 deletions
diff --git a/modules/avcodec/encode.c b/modules/avcodec/encode.c
index 9aeea34..11d30a0 100644
--- a/modules/avcodec/encode.c
+++ b/modules/avcodec/encode.c
@@ -341,7 +341,7 @@ static void param_handler(const struct pl *name, const struct pl *val,
}
-static int general_packetize(struct mbuf *mb, size_t pktsize,
+static int general_packetize(uint32_t rtp_ts, struct mbuf *mb, size_t pktsize,
videnc_packet_h *pkth, void *arg)
{
int err = 0;
@@ -355,7 +355,8 @@ static int general_packetize(struct mbuf *mb, size_t pktsize,
sz = last ? left : pktsize;
- err = pkth(last, NULL, 0, mbuf_buf(mb), sz, arg);
+ err = pkth(last, rtp_ts, NULL, 0, mbuf_buf(mb), sz,
+ arg);
mbuf_advance(mb, sz);
}
@@ -364,7 +365,8 @@ static int general_packetize(struct mbuf *mb, size_t pktsize,
}
-static int h263_packetize(struct videnc_state *st, struct mbuf *mb,
+static int h263_packetize(struct videnc_state *st,
+ uint32_t rtp_ts, struct mbuf *mb,
videnc_packet_h *pkth, void *arg)
{
struct h263_strm h263_strm;
@@ -399,7 +401,7 @@ static int h263_packetize(struct videnc_state *st, struct mbuf *mb,
st->mb_frag->pos = 0;
- err = pkth(last, NULL, 0, mbuf_buf(st->mb_frag),
+ err = pkth(last, rtp_ts, NULL, 0, mbuf_buf(st->mb_frag),
mbuf_get_left(st->mb_frag), arg);
mbuf_advance(mb, sz);
@@ -562,6 +564,7 @@ int encode_x264(struct videnc_state *st, bool update,
int i_nal;
int i, err, ret;
int csp, pln;
+ uint32_t ts;
if (!st || !frame)
return EINVAL;
@@ -618,6 +621,8 @@ int encode_x264(struct videnc_state *st, bool update,
if (i_nal == 0)
return 0;
+ ts = video_calc_rtp_timestamp(pic_out.i_pts, st->encprm.fps);
+
err = 0;
for (i=0; i<i_nal && !err; i++) {
const uint8_t hdr = nal[i].i_ref_idc<<5 | nal[i].i_type<<0;
@@ -639,10 +644,11 @@ int encode_x264(struct videnc_state *st, bool update,
if (nal[i].i_type == H264_NAL_SEI)
continue;
- err = h264_nal_send(true, true, (i+1)==i_nal, hdr,
+ err = h264_nal_send(true, true, (i+1)==i_nal, hdr, ts,
nal[i].p_payload + offset,
nal[i].i_payload - offset,
- st->encprm.pktsize, st->pkth, st->arg);
+ st->encprm.pktsize,
+ st->pkth, st->arg);
}
return err;
@@ -654,6 +660,7 @@ int encode(struct videnc_state *st, bool update, const struct vidframe *frame)
{
int i, err, ret;
int pix_fmt;
+ uint32_t ts;
if (!st || !frame)
return EINVAL;
@@ -722,6 +729,8 @@ int encode(struct videnc_state *st, bool update, const struct vidframe *frame)
return 0;
}
+ ts = video_calc_rtp_timestamp(pkt->dts, st->encprm.fps);
+
err = mbuf_write_mem(st->mb, pkt->data, pkt->size);
st->mb->pos = 0;
@@ -749,6 +758,8 @@ int encode(struct videnc_state *st, bool update, const struct vidframe *frame)
mbuf_set_end(st->mb, avpkt.size);
+ ts = video_calc_rtp_timestamp(avpkt.dts, st->encprm.fps);
+
} while (0);
#else
ret = avcodec_encode_video(st->ctx, st->mb->buf,
@@ -764,22 +775,24 @@ int encode(struct videnc_state *st, bool update, const struct vidframe *frame)
}
mbuf_set_end(st->mb, ret);
+
+ ts = video_calc_rtp_timestamp(st->pict->pts, st->encprm.fps);
#endif
switch (st->codec_id) {
case AV_CODEC_ID_H263:
- err = h263_packetize(st, st->mb, st->pkth, st->arg);
+ err = h263_packetize(st, ts, st->mb, st->pkth, st->arg);
break;
case AV_CODEC_ID_H264:
- err = h264_packetize(st->mb->buf, st->mb->end,
+ err = h264_packetize(ts, st->mb->buf, st->mb->end,
st->encprm.pktsize,
st->pkth, st->arg);
break;
case AV_CODEC_ID_MPEG4:
- err = general_packetize(st->mb, st->encprm.pktsize,
+ err = general_packetize(ts, st->mb, st->encprm.pktsize,
st->pkth, st->arg);
break;
diff --git a/modules/gst_video1/encode.c b/modules/gst_video1/encode.c
index b563d07..d0a3d44 100644
--- a/modules/gst_video1/encode.c
+++ b/modules/gst_video1/encode.c
@@ -122,13 +122,27 @@ static GstFlowReturn appsink_new_sample_cb(GstAppSink *sink,
sample = gst_app_sink_pull_sample(sink);
if (sample) {
+ GstClockTime ts;
+ uint32_t rtp_ts;
+
buffer = gst_sample_get_buffer(sample);
gst_buffer_map( buffer, &info, (GstMapFlags)(GST_MAP_READ) );
data = info.data;
size = info.size;
- h264_packetize(data, size, st->encoder.pktsize,
+ ts = GST_BUFFER_DTS_OR_PTS(buffer);
+
+ if (ts == GST_CLOCK_TIME_NONE) {
+ warning("gst_video: timestamp is unknown\n");
+ rtp_ts = 0;
+ }
+ else {
+ /* convert from nanoseconds to RTP clock */
+ rtp_ts = (uint32_t)((90000ULL * ts) / 1000000000UL);
+ }
+
+ h264_packetize(rtp_ts, data, size, st->encoder.pktsize,
st->pkth, st->arg);
gst_buffer_unmap(buffer, &info);
diff --git a/modules/h265/encode.c b/modules/h265/encode.c
index 239abe6..f4835a3 100644
--- a/modules/h265/encode.c
+++ b/modules/h265/encode.c
@@ -140,12 +140,13 @@ static int open_encoder(struct videnc_state *st, const struct vidsz *size)
static inline int packetize(bool marker, const uint8_t *buf, size_t len,
- size_t maxlen, videnc_packet_h *pkth, void *arg)
+ size_t maxlen, uint32_t rtp_ts,
+ videnc_packet_h *pkth, void *arg)
{
int err = 0;
if (len <= maxlen) {
- err = pkth(marker, NULL, 0, buf, len, arg);
+ err = pkth(marker, rtp_ts, NULL, 0, buf, len, arg);
}
else {
struct h265_nal nal;
@@ -168,7 +169,8 @@ static inline int packetize(bool marker, const uint8_t *buf, size_t len,
len-=2;
while (len > flen) {
- err |= pkth(false, fu_hdr, 3, buf, flen, arg);
+ err |= pkth(false, rtp_ts, fu_hdr, 3, buf, flen,
+ arg);
buf += flen;
len -= flen;
@@ -177,7 +179,8 @@ static inline int packetize(bool marker, const uint8_t *buf, size_t len,
fu_hdr[2] |= 1<<6; /* set END bit */
- err |= pkth(marker, fu_hdr, 3, buf, len, arg);
+ err |= pkth(marker, rtp_ts, fu_hdr, 3, buf, len,
+ arg);
}
return err;
@@ -192,6 +195,7 @@ int h265_encode(struct videnc_state *st, bool update,
uint32_t i, nalc = 0;
int colorspace;
int n, err = 0;
+ uint32_t ts;
if (!st || !frame)
return EINVAL;
@@ -253,6 +257,8 @@ int h265_encode(struct videnc_state *st, bool update,
if (n <= 0)
goto out;
+ ts = video_calc_rtp_timestamp(pic_out.pts, st->fps);
+
for (i=0; i<nalc; i++) {
x265_nal *nal = &nalv[i];
@@ -273,7 +279,7 @@ int h265_encode(struct videnc_state *st, bool update,
marker = (i+1)==nalc; /* last NAL */
err = packetize(marker, p, len, st->pktsize,
- st->pkth, st->arg);
+ ts, st->pkth, st->arg);
if (err)
goto out;
}
diff --git a/modules/v4l2_codec/v4l2_codec.c b/modules/v4l2_codec/v4l2_codec.c
index d688dad..814a477 100644
--- a/modules/v4l2_codec/v4l2_codec.c
+++ b/modules/v4l2_codec/v4l2_codec.c
@@ -277,7 +277,7 @@ static void enc_destructor(void *arg)
}
-static void encoders_read(const uint8_t *buf, size_t sz)
+static void encoders_read(uint32_t rtp_ts, const uint8_t *buf, size_t sz)
{
struct le *le;
int err;
@@ -285,8 +285,9 @@ static void encoders_read(const uint8_t *buf, size_t sz)
for (le = v4l2.encoderl.head; le; le = le->next) {
struct videnc_state *st = le->data;
- err = h264_packetize(buf, sz,
- st->encprm.pktsize, st->pkth, st->arg);
+ err = h264_packetize(rtp_ts, buf, sz,
+ st->encprm.pktsize,
+ st->pkth, st->arg);
if (err) {
warning("h264_packetize error (%m)\n", err);
}
@@ -299,6 +300,8 @@ static void read_handler(int flags, void *arg)
struct vidsrc_st *st = arg;
struct v4l2_buffer buf;
bool keyframe = false;
+ struct timeval ts;
+ uint32_t rtp_ts;
int err;
if (flags & FD_EXCEPT) {
@@ -342,6 +345,9 @@ static void read_handler(int flags, void *arg)
}
}
+ ts = buf.timestamp;
+ rtp_ts = (90000ULL * (1000000*ts.tv_sec + ts.tv_usec)) / 1000000;
+
#if 0
debug("v4l2_codec: %s frame captured at %ldsec, %ldusec (%zu bytes)\n",
keyframe ? "KEY" : " ",
@@ -350,7 +356,7 @@ static void read_handler(int flags, void *arg)
#endif
/* pass the frame to the encoders */
- encoders_read(st->buffer, buf.bytesused);
+ encoders_read(rtp_ts, st->buffer, buf.bytesused);
err = query_buffer(st->fd);
if (err) {
diff --git a/modules/vidloop/vidloop.c b/modules/vidloop/vidloop.c
index a7cecf8..99575e6 100644
--- a/modules/vidloop/vidloop.c
+++ b/modules/vidloop/vidloop.c
@@ -122,8 +122,10 @@ static int display(struct video_loop *vl, struct vidframe *frame)
}
-static int packet_handler(bool marker, const uint8_t *hdr, size_t hdr_len,
- const uint8_t *pld, size_t pld_len, void *arg)
+static int packet_handler(bool marker, uint32_t rtp_ts,
+ const uint8_t *hdr, size_t hdr_len,
+ const uint8_t *pld, size_t pld_len,
+ void *arg)
{
struct video_loop *vl = arg;
struct vidframe frame;
diff --git a/modules/vp8/encode.c b/modules/vp8/encode.c
index 99560de..83f136b 100644
--- a/modules/vp8/encode.c
+++ b/modules/vp8/encode.c
@@ -157,7 +157,8 @@ static inline void hdr_encode(uint8_t hdr[HDR_SIZE], bool noref, bool start,
static inline int packetize(bool marker, const uint8_t *buf, size_t len,
size_t maxlen, bool noref, uint8_t partid,
- uint16_t picid, videnc_packet_h *pkth, void *arg)
+ uint16_t picid, uint32_t rtp_ts,
+ videnc_packet_h *pkth, void *arg)
{
uint8_t hdr[HDR_SIZE];
bool start = true;
@@ -169,7 +170,8 @@ static inline int packetize(bool marker, const uint8_t *buf, size_t len,
hdr_encode(hdr, noref, start, partid, picid);
- err |= pkth(false, hdr, sizeof(hdr), buf, maxlen, arg);
+ err |= pkth(false, rtp_ts, hdr, sizeof(hdr), buf, maxlen,
+ arg);
buf += maxlen;
len -= maxlen;
@@ -178,7 +180,7 @@ static inline int packetize(bool marker, const uint8_t *buf, size_t len,
hdr_encode(hdr, noref, start, partid, picid);
- err |= pkth(marker, hdr, sizeof(hdr), buf, len, arg);
+ err |= pkth(marker, rtp_ts, hdr, sizeof(hdr), buf, len, arg);
return err;
}
@@ -234,6 +236,7 @@ int vp8_encode(struct videnc_state *ves, bool update,
bool keyframe = false, marker = true;
const vpx_codec_cx_pkt_t *pkt;
uint8_t partid = 0;
+ uint32_t ts;
pkt = vpx_codec_get_cx_data(&ves->ctx, &iter);
if (!pkt)
@@ -253,10 +256,13 @@ int vp8_encode(struct videnc_state *ves, bool update,
partid = pkt->data.frame.partition_id;
#endif
+ ts = video_calc_rtp_timestamp(pkt->data.frame.pts, ves->fps);
+
err = packetize(marker,
pkt->data.frame.buf,
pkt->data.frame.sz,
ves->pktsize, !keyframe, partid, ves->picid,
+ ts,
ves->pkth, ves->arg);
if (err)
return err;
diff --git a/modules/vp9/encode.c b/modules/vp9/encode.c
index 749a5bb..9de4d73 100644
--- a/modules/vp9/encode.c
+++ b/modules/vp9/encode.c
@@ -171,17 +171,20 @@ static inline void hdr_encode(uint8_t hdr[HDR_SIZE], bool start, bool end,
static int send_packet(struct videnc_state *ves, bool marker,
const uint8_t *hdr, size_t hdr_len,
- const uint8_t *pld, size_t pld_len)
+ const uint8_t *pld, size_t pld_len,
+ uint32_t rtp_ts)
{
ves->n_bytes += (hdr_len + pld_len);
- return ves->pkth(marker, hdr, hdr_len, pld, pld_len, ves->arg);
+ return ves->pkth(marker, rtp_ts, hdr, hdr_len, pld, pld_len,
+ ves->arg);
}
static inline int packetize(struct videnc_state *ves,
bool marker, const uint8_t *buf, size_t len,
- size_t maxlen, uint16_t picid)
+ size_t maxlen, uint16_t picid,
+ uint32_t rtp_ts)
{
uint8_t hdr[HDR_SIZE];
bool start = true;
@@ -193,7 +196,8 @@ static inline int packetize(struct videnc_state *ves,
hdr_encode(hdr, start, false, picid);
- err |= send_packet(ves, false, hdr, sizeof(hdr), buf, maxlen);
+ err |= send_packet(ves, false, hdr, sizeof(hdr), buf, maxlen,
+ rtp_ts);
buf += maxlen;
len -= maxlen;
@@ -202,7 +206,8 @@ static inline int packetize(struct videnc_state *ves,
hdr_encode(hdr, start, true, picid);
- err |= send_packet(ves, marker, hdr, sizeof(hdr), buf, len);
+ err |= send_packet(ves, marker, hdr, sizeof(hdr), buf, len,
+ rtp_ts);
return err;
}
@@ -275,6 +280,7 @@ int vp9_encode(struct videnc_state *ves, bool update,
for (;;) {
bool marker = true;
const vpx_codec_cx_pkt_t *pkt;
+ uint32_t ts;
pkt = vpx_codec_get_cx_data(&ves->ctx, &iter);
if (!pkt)
@@ -291,11 +297,14 @@ int vp9_encode(struct videnc_state *ves, bool update,
if (pkt->data.frame.flags & VPX_FRAME_IS_FRAGMENT)
marker = false;
+ ts = video_calc_rtp_timestamp(pkt->data.frame.pts, ves->fps);
+
err = packetize(ves,
marker,
pkt->data.frame.buf,
pkt->data.frame.sz,
- ves->pktsize, ves->picid);
+ ves->pktsize, ves->picid,
+ ts);
if (err)
return err;
}