-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmain.cpp
101 lines (92 loc) · 2.71 KB
/
main.cpp
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
// Copyright (C) Trevor Sundberg, MIT License (see LICENSE.md)
#include "h264-mp4-encoder.h"
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static int pixel_of_chessboard(double x, double y)
{
int mid = (fabs(x) < 4 && fabs(y) < 4);
int i = (int)(x);
int j = (int)(y);
int black = (mid) ? 128 : i / 16;
int white = (mid) ? 128 : 255 - j / 16;
int c00 = (((i >> 4) + (j >> 4)) & 1) ? white : black;
int c01 = ((((i + 1) >> 4) + (j >> 4)) & 1) ? white : black;
int c10 = (((i >> 4) + ((j + 1) >> 4)) & 1) ? white : black;
int c11 = ((((i + 1) >> 4) + ((j + 1) >> 4)) & 1) ? white : black;
int s = (int)((c00 * (1 - (x - i)) + c01 * (x - i)) * (1 - (y - j)) +
(c10 * (1 - (x - i)) + c11 * (x - i)) * ((y - j)) + 0.5);
return s < 0 ? 0 : s > 255 ? 255 : s;
}
static void gen_chessboard_rot_yuv(unsigned char *p, int w, int h, int frm)
{
memset(p + w * h, 128, w *h / 2);
double co = cos(.01 * frm);
double si = sin(.01 * frm);
int hw = w >> 1;
int hh = h >> 1;
for (int r = 0; r < h; r++)
{
for (int c = 0; c < w; c++)
{
double x = co * (c - hw) + si * (r - hh);
double y = -si * (c - hw) + co * (r - hh);
p[r * w + c] = pixel_of_chessboard(x, y);
}
}
}
static void gen_chessboard_rot_rgba(unsigned char *p, int w, int h, int frm)
{
memset(p + w * h, 128, w *h / 2);
double co = cos(.01 * frm);
double si = sin(.01 * frm);
int hw = w >> 1;
int hh = h >> 1;
for (int r = 0; r < h; r++)
{
for (int c = 0; c < w; c++)
{
double x = co * (c - hw) + si * (r - hh);
double y = -si * (c - hw) + co * (r - hh);
int base = (r * w + c) * 4;
int color = pixel_of_chessboard(x / 2, y / 2);
p[base + 0] = color;
p[base + 1] = color / 2;
p[base + 2] = color;
p[base + 3] = 255;
}
}
}
int main(int argc, char *argv[])
{
printf("Starting encoding\n");
H264MP4Encoder encoder;
encoder.set_width(1024);
encoder.set_height(768);
encoder.set_frameRate(25);
encoder.set_debug(false);
int frame_size = 0;
bool use_rgba = true;
if (use_rgba) {
frame_size = encoder.get_width() * encoder.get_height() * 4;
} else {
frame_size = encoder.get_width() * encoder.get_height() * 3 / 2;
}
std::string buffer;
buffer.resize(frame_size);
encoder.initialize();
for (int i = 0; i < 100; ++i)
{
if (use_rgba) {
gen_chessboard_rot_rgba((uint8_t*)buffer.data(), encoder.get_width(), encoder.get_height(), i);
encoder.addFrameRgba(buffer);
} else {
gen_chessboard_rot_yuv((uint8_t*)buffer.data(), encoder.get_width(), encoder.get_height(), i);
encoder.addFrameYuv(buffer);
}
}
encoder.finalize();
printf("Done encoding\n");
return 0;
}