summaryrefslogtreecommitdiff
path: root/modules/h265/encode.c
blob: f4835a3f7721acb1d67cc78a3453acad4690edc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
 * @file h265/encode.c H.265 Encode
 *
 * Copyright (C) 2010 Creytiv.com
 */

#include <string.h>
#include <re.h>
#include <rem.h>
#include <baresip.h>
#include <x265.h>
#include "h265.h"


struct videnc_state {
	struct vidsz size;
	x265_param *param;
	x265_encoder *x265;
	int64_t pts;
	unsigned fps;
	unsigned bitrate;
	unsigned pktsize;
	videnc_packet_h *pkth;
	void *arg;
};


static void destructor(void *arg)
{
	struct videnc_state *st = arg;

	if (st->x265)
		x265_encoder_close(st->x265);
	if (st->param)
		x265_param_free(st->param);
}


static int set_params(struct videnc_state *st, unsigned fps, unsigned bitrate)
{
	st->param = x265_param_alloc();
	if (!st->param) {
		warning("h265: x265_param_alloc failed\n");
		return ENOMEM;
	}

	x265_param_default(st->param);

	if (0 != x265_param_apply_profile(st->param, "main")) {
		warning("h265: x265_param_apply_profile failed\n");
		return EINVAL;
	}

	if (0 != x265_param_default_preset(st->param,
					   "ultrafast", "zerolatency")) {

		warning("h265: x265_param_default_preset error\n");
		return EINVAL;
	}

	st->param->fpsNum = fps;
	st->param->fpsDenom = 1;

	/* VPS, SPS and PPS headers should be output with each keyframe */
	st->param->bRepeatHeaders = 1;

	/* Rate Control */
	st->param->rc.rateControlMode = X265_RC_CRF;
	st->param->rc.bitrate = bitrate / 1000;
	st->param->rc.vbvMaxBitrate = bitrate / 1000;
	st->param->rc.vbvBufferSize = 2 * bitrate / fps;

	return 0;
}


int h265_encode_update(struct videnc_state **vesp, const struct vidcodec *vc,
		       struct videnc_param *prm, const char *fmtp,
		       videnc_packet_h *pkth, void *arg)
{
	struct videnc_state *ves;
	int err = 0;
	(void)fmtp;

	if (!vesp || !vc || !prm || prm->pktsize < 3 || !pkth)
		return EINVAL;

	ves = *vesp;

	if (!ves) {

		ves = mem_zalloc(sizeof(*ves), destructor);
		if (!ves)
			return ENOMEM;

		*vesp = ves;
	}
	else {
		if (ves->x265 && (ves->bitrate != prm->bitrate ||
				  ves->pktsize != prm->pktsize ||
				  ves->fps     != prm->fps)) {

			x265_encoder_close(ves->x265);
			ves->x265 = NULL;
		}
	}

	ves->bitrate = prm->bitrate;
	ves->pktsize = prm->pktsize;
	ves->fps     = prm->fps;
	ves->pkth    = pkth;
	ves->arg     = arg;

	err = set_params(ves, prm->fps, prm->bitrate);
	if (err)
		return err;

	return 0;
}


static int open_encoder(struct videnc_state *st, const struct vidsz *size)
{
	if (st->x265) {
		debug("h265: re-opening encoder\n");
		x265_encoder_close(st->x265);
	}

	st->param->sourceWidth  = size->w;
	st->param->sourceHeight = size->h;

	st->x265 = x265_encoder_open(st->param);
	if (!st->x265) {
		warning("h265: x265_encoder_open failed\n");
		return ENOMEM;
	}

	return 0;
}


static inline int packetize(bool marker, const uint8_t *buf, size_t len,
			    size_t maxlen, uint32_t rtp_ts,
			    videnc_packet_h *pkth, void *arg)
{
	int err = 0;

	if (len <= maxlen) {
		err = pkth(marker, rtp_ts, NULL, 0, buf, len, arg);
	}
	else {
		struct h265_nal nal;
		uint8_t fu_hdr[3];
		const size_t flen = maxlen - sizeof(fu_hdr);

		err = h265_nal_decode(&nal, buf);
		if (err) {
			warning("h265: encode: could not decode"
				" NAL of %zu bytes (%m)\n", len, err);
			return err;
		}

		h265_nal_encode(fu_hdr, H265_NAL_FU,
				nal.nuh_temporal_id_plus1);

		fu_hdr[2] = 1<<7 | nal.nal_unit_type;

		buf+=2;
		len-=2;

		while (len > flen) {
			err |= pkth(false, rtp_ts, fu_hdr, 3, buf, flen,
				    arg);

			buf += flen;
			len -= flen;
			fu_hdr[2] &= ~(1 << 7); /* clear Start bit */
		}

		fu_hdr[2] |= 1<<6;  /* set END bit */

		err |= pkth(marker, rtp_ts, fu_hdr, 3, buf, len,
			    arg);
	}

	return err;
}


int h265_encode(struct videnc_state *st, bool update,
		const struct vidframe *frame)
{
	x265_picture *pic_in = NULL, pic_out;
	x265_nal *nalv;
	uint32_t i, nalc = 0;
	int colorspace;
	int n, err = 0;
	uint32_t ts;

	if (!st || !frame)
		return EINVAL;

	switch (frame->fmt) {

	case VID_FMT_YUV420P:
		colorspace = X265_CSP_I420;
		break;

	case VID_FMT_YUV444P:
		colorspace = X265_CSP_I444;
		break;

	default:
		warning("h265: encode: pixel format not supported (%s)\n",
			vidfmt_name(frame->fmt));
		return EINVAL;
	}

	if (!st->x265 || !vidsz_cmp(&st->size, &frame->size) ||
	    st->param->internalCsp != colorspace) {

		debug("h265: encoder: reset %u x %u (%s)\n",
		      frame->size.w, frame->size.h, vidfmt_name(frame->fmt));

		st->param->internalCsp = colorspace;

		err = open_encoder(st, &frame->size);
		if (err)
			return err;

		st->size = frame->size;
	}

	if (update) {
		debug("h265: encode: picture update was requested\n");
	}

	pic_in = x265_picture_alloc();
	if (!pic_in) {
		warning("h265: x265_picture_alloc failed\n");
		return ENOMEM;
	}

	x265_picture_init(st->param, pic_in);

	pic_in->sliceType  = update ? X265_TYPE_IDR : X265_TYPE_AUTO;
	pic_in->pts        = ++st->pts;      /* XXX: add PTS to API */
	pic_in->colorSpace = colorspace;

	for (i=0; i<3; i++) {
		pic_in->planes[i] = frame->data[i];
		pic_in->stride[i] = frame->linesize[i];
	}

	/* NOTE: important to get the PTS of the "out" picture */
	n = x265_encoder_encode(st->x265, &nalv, &nalc, pic_in, &pic_out);
	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];
		uint8_t *p = nal->payload;
		size_t len = nal->sizeBytes;
		bool marker;

#if 0
		debug("h265: encode: %s type=%2d  %s\n",
			  h265_is_keyframe(nal->type) ? "<KEY>" : "     ",
			  nal->type, h265_nalunit_name(nal->type));
#endif

		h265_skip_startcode(&p, &len);

		/* XXX: use pic_out.pts */

		marker = (i+1)==nalc;  /* last NAL */

		err = packetize(marker, p, len, st->pktsize,
				ts, st->pkth, st->arg);
		if (err)
			goto out;
	}

 out:
	if (pic_in)
		x265_picture_free(pic_in);

	return err;
}