Files
lotherk 91dfe20658 dtchat: camera capture, VP8 on the call and a real video picture (T7.1)
media_capture.c: V4L2 (YUYV, 4 mmap buffers, poll, QBUF again after use) and a
test pattern; a camera with another size is scaled to the call's 640x360, and a
build without HAVE_V4L2 gets the pattern. On this T14 the integrated camera is
/dev/video0 and gives frames (test_capture: v4l2:/dev/video0, non-uniform
frame, 320x180 through the scaler; pattern frames differ and have Y and
chroma). configure defines HAVE_V4L2 for linux/videodev2.h.

media_video.c gains dc_vpx_enc_keyframe (VPX_EFLAG_FORCE_KF on the next
frame), media_packet a DC_MEDIA_KFREQ control, media_call
dc_call_request_keyframe/take and DC_MEDIA_KFREQ in the sealed type range.
dc_call_video_out now decodes the reassembled VP8 frame into I420 (it returned
the compressed frame, which the UI drew as a green picture);
dc_call_video_packet keeps the compressed frame for tests and recordings.
Video timestamps are wall clock, not frame counts, so a pump slower than 15 fps
does not drift behind the audio clock.

localui starts camera+encoder on entering the call, sends 15 fps, requests a
keyframe after a loss, forces one when asked, decodes incoming frames into the
call window, drains up to four 20 ms capture frames per 40 ms pump (one per
pump halved the audio clock), and logs video in/out under
DTCHAT_CALL_DEBUG; DTCHAT_TEST_VIDEO switches to the pattern.

Two UIs under Xvfb: A pattern, B camera, 90 frames each way in about 6 s,
audio 300 frames (real time), the window shows the peer's picture
(build/mcde-shots/dtchat-t71-video-call.png). test_video_packet uses the
packet interface; its 50 KB/42-fragment loopback check still passes. New
harness entry DtChat/test_capture (43). In-tree build pending: a foreign
make release holds the lock.
2026-11-17 09:19:48 +00:00

295 lines
6.8 KiB
C

/*
* media_video.c -- dtchat's VP8 video through libvpx (T7.1)
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* One encoder and one decoder per call, I420 frames (Y, then U, then V,
* 4:2:0), realtime deadline, CBR, error resilience on, a keyframe every two
* seconds. 640x360 at 15 fps and 500 kbit/s by default; the caller owns the
* I420 buffers and pumps one frame at a time. A packet holds exactly one
* encoded frame, so the media packet of T6.3 carries it unchanged.
*/
#include "dc_internal.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_VPX
#include <vpx/vp8cx.h>
#include <vpx/vp8dx.h>
#include <vpx/vpx_decoder.h>
#include <vpx/vpx_encoder.h>
#endif
#ifndef HAVE_VPX
/* a build without libvpx: the video calls say so and audio still works */
DcVpxEnc *
dc_vpx_enc_new(int width, int height, int fps, int bitrate, char *err,
size_t errlen)
{
(void)width;
(void)height;
(void)fps;
(void)bitrate;
if (err != NULL && errlen > 0)
snprintf(err, errlen, "This build has no libvpx.");
return NULL;
}
int
dc_vpx_enc_frame(DcVpxEnc *enc, const uint8_t *i420, unsigned char *out,
size_t outlen, size_t *outlen_out)
{
(void)enc;
(void)i420;
(void)out;
(void)outlen;
if (outlen_out != NULL)
*outlen_out = 0;
return -1;
}
void
dc_vpx_enc_keyframe(DcVpxEnc *enc)
{
(void)enc;
}
void
dc_vpx_enc_free(DcVpxEnc *enc)
{
(void)enc;
}
DcVpxDec *
dc_vpx_dec_new(int width, int height, char *err, size_t errlen)
{
(void)width;
(void)height;
if (err != NULL && errlen > 0)
snprintf(err, errlen, "This build has no libvpx.");
return NULL;
}
int
dc_vpx_dec_frame(DcVpxDec *dec, const unsigned char *data, size_t len,
uint8_t *i420)
{
(void)dec;
(void)data;
(void)len;
(void)i420;
return -1;
}
void
dc_vpx_dec_free(DcVpxDec *dec)
{
(void)dec;
}
#else /* HAVE_VPX */
struct DcVpxEnc {
vpx_codec_ctx_t ctx;
vpx_image_t image;
int width;
int height;
long frame;
int force_keyframe;
};
struct DcVpxDec {
vpx_codec_ctx_t ctx;
int width;
int height;
};
static void
fill_image(vpx_image_t *img, int width, int height, const uint8_t *i420)
{
int y;
vpx_img_wrap(img, VPX_IMG_FMT_I420, (unsigned)width,
(unsigned)height, 1, (unsigned char *)i420);
(void)y;
}
DcVpxEnc *
dc_vpx_enc_new(int width, int height, int fps, int bitrate, char *err,
size_t errlen)
{
DcVpxEnc *enc;
vpx_codec_enc_cfg_t cfg;
vpx_codec_iface_t *iface = vpx_codec_vp8_cx();
if (width < 16 || height < 16 || width > 1920 || height > 1080 ||
(width & 1) != 0 || (height & 1) != 0) {
if (err != NULL && errlen > 0)
snprintf(err, errlen, "bad video size");
return NULL;
}
enc = calloc(1, sizeof(*enc));
if (enc == NULL)
return NULL;
if (vpx_codec_enc_config_default(iface, &cfg, 0) != VPX_CODEC_OK) {
if (err != NULL && errlen > 0)
snprintf(err, errlen, "libvpx has no default config");
free(enc);
return NULL;
}
cfg.g_w = (unsigned)width;
cfg.g_h = (unsigned)height;
cfg.g_timebase.num = 1;
cfg.g_timebase.den = fps > 0 ? (unsigned)fps : 15;
cfg.rc_end_usage = VPX_CBR;
cfg.rc_target_bitrate = (unsigned)(bitrate > 0 ? bitrate : 300);
cfg.g_error_resilient = 1;
cfg.g_lag_in_frames = 0;
cfg.g_threads = 1;
cfg.kf_mode = VPX_KF_AUTO;
cfg.kf_max_dist = (unsigned)(fps > 0 ? fps * 2 : 30);
enc->width = width;
enc->height = height;
enc->frame = 0;
if (vpx_codec_enc_init(&enc->ctx, iface, &cfg, 0) != VPX_CODEC_OK) {
if (err != NULL && errlen > 0)
snprintf(err, errlen, "cannot start the VP8 encoder");
free(enc);
return NULL;
}
(void)vpx_codec_control(&enc->ctx, VP8E_SET_CPUUSED, 8);
(void)vpx_codec_control(&enc->ctx, VP8E_SET_STATIC_THRESHOLD, 1);
return enc;
}
/*
* Encode one I420 frame (width * height * 3 / 2 bytes). OUT receives the
* packet; 0 is returned when libvpx has no packet for this frame, -1 on an
* error. The packet length comes back in *OUTLEN.
*/
int
dc_vpx_enc_frame(DcVpxEnc *enc, const uint8_t *i420, unsigned char *out,
size_t outlen, size_t *outlen_out)
{
const vpx_codec_cx_pkt_t *pkt;
vpx_codec_iter_t iter = NULL;
if (enc == NULL || i420 == NULL || out == NULL || outlen_out == NULL)
return -1;
*outlen_out = 0;
fill_image(&enc->image, enc->width, enc->height, i420);
if (vpx_codec_encode(&enc->ctx, &enc->image, enc->frame, 1,
enc->force_keyframe ? VPX_EFLAG_FORCE_KF : 0,
VPX_DL_REALTIME) != VPX_CODEC_OK)
return -1;
enc->force_keyframe = 0;
enc->frame++;
while ((pkt = vpx_codec_get_cx_data(&enc->ctx, &iter)) != NULL) {
if (pkt->kind != VPX_CODEC_CX_FRAME_PKT)
continue;
if (pkt->data.frame.sz > outlen)
return -1;
memcpy(out, pkt->data.frame.buf, pkt->data.frame.sz);
*outlen_out = pkt->data.frame.sz;
return 1;
}
return 0;
}
void
dc_vpx_enc_keyframe(DcVpxEnc *enc)
{
if (enc != NULL)
enc->force_keyframe = 1;
}
void
dc_vpx_enc_free(DcVpxEnc *enc)
{
if (enc == NULL)
return;
(void)vpx_codec_destroy(&enc->ctx);
free(enc);
}
DcVpxDec *
dc_vpx_dec_new(int width, int height, char *err, size_t errlen)
{
DcVpxDec *dec;
dec = calloc(1, sizeof(*dec));
if (dec == NULL)
return NULL;
dec->width = width;
dec->height = height;
if (vpx_codec_dec_init(&dec->ctx, vpx_codec_vp8_dx(), NULL, 0) !=
VPX_CODEC_OK) {
if (err != NULL && errlen > 0)
snprintf(err, errlen, "cannot start the VP8 decoder");
free(dec);
return NULL;
}
return dec;
}
/*
* Decode one packet into I420. Returns 1 when a frame came out, 0 when the
* packet produced none (a damaged one is dropped by libvpx), -1 on an error.
* A frame that does not match the configured size sets the error and is
* dropped.
*/
int
dc_vpx_dec_frame(DcVpxDec *dec, const unsigned char *data, size_t len,
uint8_t *i420)
{
vpx_image_t *img;
vpx_codec_iter_t iter = NULL;
if (dec == NULL || data == NULL || i420 == NULL)
return -1;
if (vpx_codec_decode(&dec->ctx, data, (unsigned)len, NULL, 0) !=
VPX_CODEC_OK)
return 0;
img = vpx_codec_get_frame(&dec->ctx, &iter);
if (img == NULL)
return 0;
if ((int)img->d_w != dec->width || (int)img->d_h != dec->height)
return -1;
{
int y;
for (y = 0; y < dec->height; y++)
memcpy(i420 + (size_t)y * dec->width,
img->planes[VPX_PLANE_Y] +
(size_t)y * img->stride[VPX_PLANE_Y],
(size_t)dec->width);
for (y = 0; y < dec->height / 2; y++) {
memcpy(i420 + (size_t)dec->width * dec->height +
(size_t)y * (dec->width / 2),
img->planes[VPX_PLANE_U] +
(size_t)y * img->stride[VPX_PLANE_U],
(size_t)dec->width / 2);
memcpy(i420 + (size_t)dec->width * dec->height +
(size_t)dec->width * dec->height / 4 +
(size_t)y * (dec->width / 2),
img->planes[VPX_PLANE_V] +
(size_t)y * img->stride[VPX_PLANE_V],
(size_t)dec->width / 2);
}
}
return 1;
}
void
dc_vpx_dec_free(DcVpxDec *dec)
{
if (dec == NULL)
return;
(void)vpx_codec_destroy(&dec->ctx);
free(dec);
}
#endif /* HAVE_VPX */