~ft/hj264

17578ac2a4ded4303459881acd0d149188692292 — Sigrid Solveig Haflínudóttir a month ago cad6611
exit cleanly; print average fps when debug is enabled
1 files changed, 30 insertions(+), 31 deletions(-)

M hj264.c
M hj264.c => hj264.c +30 -31
@@ 137,12 137,10 @@ hj264_encode(Hj264 *h, u8int **data, int *sz)
{
	int e;

	if((e = H264E_encode(h->persist, h->scratch, &h->rp, &h->ioyuv, data, sz)) != 0){
	if((e = H264E_encode(h->persist, h->scratch, &h->rp, &h->ioyuv, data, sz)) != 0)
		werrstr("H264E_encode: %ℏ", e);
		return -1;
	}

	return 0;
	return e;
}

static Hj264 *


@@ 205,7 203,7 @@ hj264new(int nthreads, int denoise, int kbps, int gop, int ww, int hh)
		t->id = i;
		t->job = chancreate(sizeof(void*), 0);
		t->done = chancreate(sizeof(void*), 0);
		procrfork(threadf, t, mainstacksize, RFCFDG|RFCENVG);
		proccreate(threadf, t, mainstacksize);
		recvp(t->done);
	}



@@ 213,7 211,7 @@ hj264new(int nthreads, int denoise, int kbps, int gop, int ww, int hh)
}

static void
hj264free(Hj264 *h)
hj264close(Hj264 *h)
{
	int i;



@@ 222,7 220,18 @@ hj264free(Hj264 *h)
		chanclose(h->threads[i].job);
	}

	free(h);
	Bflush(&h->out);
}

static int
done(void*, char*)
{
	Hj264 *h;

	h = *threaddata();
	Bflush(&h->out);

	return 1;
}

static void


@@ 234,12 243,15 @@ encthread(void *p)
	Hj264 *h;
	int sz;

	threadsetname("hj264/encthread");

	h = p;
	prev = nil;

	threadsetname("hj264/encthread");
	threadnotify(done, 1);
	*threaddata() = h;

	for(;;){
		if((img = recvp(h->frame)) == nil)
		if(recv(h->frame, &img) < 0)
			break;
		if(opt && prev != nil && memcmp(img->bgrx, prev->bgrx, img->w*img->h*4) == 0){
			free(img);


@@ 316,25 328,12 @@ usage(void)
	threadexitsall("usage");
}

static int
done(void *, char *)
{
	Hj264 *h;

	if((h = *procdata()) != nil){
		chanclose(h->frame);
		recvp(h->done);
	}

	return 1;
}

int
main(int argc, char **argv)
{
	int nthreads, fps, kbps, denoise, quality, qp, gop;
	char *s, tmp[61], *f[5];
	uvlong fstart, fend;
	uvlong fstart, fend, f₀;
	int ww, hh, in, fmt;
	u8int v[20];
	Img *img;


@@ 428,9 427,7 @@ main(int argc, char **argv)
		h->rp.qp_max = 50;
		h->rp.desired_frame_bytes = kbps*1000/8/fps;
	}
	*procdata() = h;
	atnotify(done, 1);
	procrfork(encthread, h, mainstacksize, RFCENVG);
	proccreate(encthread, h, mainstacksize);

	if(h->fmt == FmtIVF){
		Bwrite(&h->out, "DKIF\x00\x00\x20\x00AVC1", 12);


@@ 452,7 449,8 @@ main(int argc, char **argv)
			return -1;
	}

	tstart = nsec() - npe_nanosec();
	f₀ = npe_nanosec();
	tstart = nsec() - f₀;
	for(nframes = 0;; nframes++){
		fstart = npe_nanosec();
		if((img = imgread(in, ww, hh)) == nil)


@@ 461,13 459,14 @@ main(int argc, char **argv)
			break;
		fend = npe_nanosec();

		if(debug && nframes > 0 && (nframes % fps) == 0)
			fprint(2, "avg fps: %llud\n", nframes/((fend - f₀)/Nsec));

		if(Nsec/fps > (fend - fstart))
			npe_nsleep(Nsec/fps - (fend - fstart));
	}

	chanclose(h->frame);
	recvp(h->done);
	hj264free(h);

	return done(nil, nil);
	return 0;
}