summaryrefslogtreecommitdiffstats
blob: c25e1537e4f5a4e0fcf6c1a1cd834aa423547ed5 (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
/*
 * Copyright (c) 2012 Yoann Blein
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

#include <Swiften/ScreenSharing/VP8Encoder.h>
#include <Swiften/ScreenSharing/Image.h>
#include <Swiften/ScreenSharing/VP8RTPPacketizer.h>
#include <Swiften/Base/Log.h>
#include "vpx/vp8cx.h"

namespace Swift {

VP8Encoder::VP8Encoder(VP8RTPPacketizer* packetizer, unsigned int  width, unsigned int  height)
	: VideoEncoder(),
	  packetizer(packetizer), codecInterface(vpx_codec_vp8_cx()), imageBuffer(0), codecFlags(0), frameFlags(0), frameNumber(0), pictureID(0), rpsiReceived(true)
{
	SWIFT_LOG(debug) << "VP8 Encoder:" << vpx_codec_iface_name(codecInterface) << std::endl;

	// Populate encoder configuration
	vpx_codec_err_t err = vpx_codec_enc_config_default(codecInterface, &codecConfig, 0);
	if (err) {
		SWIFT_LOG(error) << "VP8 Encoder: Failed to get config, " << vpx_codec_err_to_string(err) << std::endl;
		// TODO: exception
	}

	// Update the default configuration with our settings
	codecConfig.rc_target_bitrate = width * height * codecConfig.rc_target_bitrate / codecConfig.g_w / codecConfig.g_h; // Sample config on VP8 doc
	//codecConfig.rc_target_bitrate = 90000; // ?
	codecConfig.g_w = width;
	codecConfig.g_h = height;
	//codecConfig.g_threads = ?;

	// codecFlags = VPX_CODEC_USE_OUTPUT_PARTITION; // TODO: adapt packetization and decoder

	updateCodecConfig(); // TODO: Handle and re-throw exceptions
}

VP8Encoder::~VP8Encoder()
{
	vpx_codec_destroy(&codecContext);
	vpx_img_free(imageBuffer);
}

void VP8Encoder::updateCodecConfig()
{
	if (imageBuffer)
		vpx_img_free(imageBuffer);

	vpx_codec_err_t err = vpx_codec_enc_init(&codecContext, codecInterface, &codecConfig, codecFlags);
	if (err) {
		SWIFT_LOG(error) << "VP8 Encoder: Failed to initialize encoder, " << vpx_codec_err_to_string(err) << std::endl;
		// TODO: exception
		return;
	}

	imageBuffer = vpx_img_alloc(0, VPX_IMG_FMT_YV12, codecConfig.g_w, codecConfig.g_h, 1);
	if (!imageBuffer) {
		SWIFT_LOG(error) << "VP8 Encoder: Failed to allocate image" << std::endl;
		// TODO: exception
	}

	frameNumber = 0;
	pictureID = rand() % 128;
	rpsiReceived = true;
}

void VP8Encoder::encodeImage(const Image& frame)
{
	if (!convertRGB24toYV12inBuffer(frame)) {
		SWIFT_LOG(error) << "VP8 Encoder: Failed to convert frame: Image buffer not initialized" << std::endl;
		// TODO: exception ?
		return;
	}

	vpx_enc_frame_flags_t localFrameFlags = frameFlags;
	if (!rpsiReceived && frameNumber > 1) {
		localFrameFlags |= VP8_EFLAG_NO_REF_LAST; // VP8_EFLAG_FORCE_GF; // TODO: VP8_EFLAG_NO_REF_LAST;
	}

	vpx_codec_err_t err = vpx_codec_encode(&codecContext, imageBuffer, frameNumber, 1, localFrameFlags, VPX_DL_REALTIME);
	if (err) {
		SWIFT_LOG(error) << "VP8 Encoder: Failed to encode frame, " << vpx_codec_err_to_string(err) << std::endl;
		// TODO: exception ?
		return;
	}

	const vpx_codec_cx_pkt_t* pkt;
	vpx_codec_iter_t iter = NULL;
	while ((pkt = vpx_codec_get_cx_data(&codecContext, &iter))) {
		switch (pkt->kind) {
			case VPX_CODEC_CX_FRAME_PKT:
				packetizer->packetizeFrame(pkt, pictureID);
				break;
			default:
				break;
		}
	}

	++frameNumber;
	pictureID = (pictureID + 1) % 128;
	rpsiReceived = false;
}

void VP8Encoder::handleRPSIFeedback(int /*pictureID*/)
{
	rpsiReceived = true;
}

bool VP8Encoder::convertRGB24toYV12inBuffer(const Image& frame)
{
	if (!imageBuffer)
		return false;

	uint8_t* yPlane = imageBuffer->planes[VPX_PLANE_Y];
	uint8_t* uPlane = imageBuffer->planes[VPX_PLANE_U];
	uint8_t* vPlane = imageBuffer->planes[VPX_PLANE_V];

	unsigned int width = frame.width;
	unsigned int height = frame.height;
	unsigned int len = width * height;
	const std::vector<uint8_t>& data = frame.data;

	for (unsigned int  i = 0; i < len; ++i) {
		const uint8_t* p = &data[3 * i];
		yPlane[i] = ((66 * p[0] + 129 * p[1] + 25 * p[2] + 128) >> 8) + 16;
	}

	for (unsigned int line = 0; line < height / 2; ++line) {
		for (unsigned int col = 0; col < width / 2; ++col) {
			int pos = 2 * (line * width + col);
			const uint8_t* p1 = &data[3 * (pos)];
			const uint8_t* p2 = &data[3 * (pos + 1)];
			const uint8_t* p3 = &data[3 * (pos + width)];
			const uint8_t* p4 = &data[3 * (pos + width + 1)];

			int r = (p1[0] + p2[0] + p3[0] + p4[0]) * 0.25;
			int g = (p1[1] + p2[1] + p3[1] + p4[1]) * 0.25;
			int b = (p1[2] + p2[2] + p3[2] + p4[2]) * 0.25;

			uPlane[line * imageBuffer->stride[VPX_PLANE_U] + col] = ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
			vPlane[line * imageBuffer->stride[VPX_PLANE_V] + col] = ((112 * r - 94 * g - 18  * b + 128) >> 8) + 128;
		}
	}
	return true;
}

}