ref: 4968057b69842a4e3a73149fb26c01a25a69b935
parent: c3333e66aa6e6d122e79cccd9d6f5d81cca51ec0
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Mon Aug 2 10:44:56 EDT 2021
encoder thread: compare each frame to the previous one and discard if the contents are the same
--- a/hj264.c
+++ b/hj264.c
@@ -6,11 +6,9 @@
#include <bio.h>
#include <draw.h>
#include <tos.h>
+#include <npe.h>
#include "yuv.h"
-void npe_nsleep(uvlong ns);
-uvlong npe_nanosec(void);
-
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define clp(v,a,b) min((b), max((v),(a)))
@@ -231,18 +229,25 @@
encthread(void *p)
{
u8int *data, v[12];
+ Img *img, *prev;
uvlong ts;
- Img *img;
Hj264 *h;
int sz;
h = p;
+ prev = nil;
for(;;){
if((img = recvp(h->frame)) == nil)
break;
+ if(prev != nil && memcmp(img->bgrx, prev->bgrx, img->w*img->h*4) == 0){
+ free(img);
+ continue;
+ }
+
xrgb2yuv420(img->bgrx, img->w, img->h, &h->yuv);
- ts = img->ns / 1000000ULL;
- free(img);
+ ts = img->ns / Nmsec;
+ free(prev);
+ prev = img;
if(hj264_encode(h, &data, &sz) != 0)
sysfatal("hj264_encode: %r");
@@ -271,6 +276,8 @@
if(h->done != nil)
sendp(h->done, nil);
+ free(prev);
+
threadexits(nil);
}
@@ -312,7 +319,7 @@
if(debug){
s = npe_nanosec() - tstart;
- s /= 1000000000ULL;
+ s /= Nsec;
if(s != 0)
fprint(2, "%llud fps\n", nframes / s);
}
@@ -408,7 +415,7 @@
sysfatal("hj264new: %r");
if(Binit(&h->out, 1, OWRITE) < 0)
sysfatal("Binit failed: %r");
- h->frame = chancreate(sizeof(void*), 1); /* FIXME this is wrong as the encoder might be too late */
+ h->frame = chancreate(sizeof(void*), nthreads);
h->done = chancreate(sizeof(void*), 0);
h->fmt = fmt;
@@ -452,8 +459,8 @@
break;
fend = npe_nanosec();
- if(1000000000ULL/fps > (fend - fstart))
- npe_nsleep(1000000000ULL/fps - (fend - fstart));
+ if(Nsec/fps > (fend - fstart))
+ npe_nsleep(Nsec/fps - (fend - fstart));
}
chanclose(h->frame);