-
Notifications
You must be signed in to change notification settings - Fork 1
/
encoder_soft_h264.c
137 lines (108 loc) · 4.41 KB
/
encoder_soft_h264.c
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
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <pthread.h>
#include <linux/videodev2.h>
#include <x264.h>
#include "encoder_soft_h264.h"
#define PRESET "ultrafast"
#define TUNE "zerolatency"
#define PROFILE "baseline"
static char errbuf[256];
static void set_error(const char *format, ...) {
va_list args;
va_start(args, format);
vsnprintf(errbuf, 256, format, args);
}
const char *encoder_soft_h264_get_error() {
return errbuf;
}
typedef struct {
const parameters_t *params;
encoder_soft_h264_output_cb output_cb;
x264_param_t x_params;
x264_t *x_handler;
x264_picture_t x_pic_in;
x264_picture_t x_pic_out;
uint64_t next_pts;
pthread_mutex_t mutex;
} encoder_soft_h264_priv_t;
bool encoder_soft_h264_create(const parameters_t *params, int stride, int colorspace, encoder_soft_h264_output_cb output_cb, encoder_soft_h264_t **enc) {
*enc = malloc(sizeof(encoder_soft_h264_priv_t));
encoder_soft_h264_priv_t *encp = (encoder_soft_h264_priv_t *)(*enc);
memset(encp, 0, sizeof(encoder_soft_h264_priv_t));
int res = x264_param_default_preset(&encp->x_params, PRESET, TUNE);
if (res < 0) {
set_error("x264_param_default_preset() failed");
goto failed;
}
encp->x_params.i_width = params->width;
encp->x_params.i_height = params->height;
encp->x_params.i_csp = X264_CSP_I420;
encp->x_params.i_bitdepth = 8;
encp->x_params.b_vfr_input = 0;
encp->x_params.i_fps_num = params->fps;
encp->x_params.i_fps_den = 1;
encp->x_params.b_repeat_headers = 1;
encp->x_params.b_intra_refresh = 0;
encp->x_params.b_annexb = 1;
encp->x_params.i_keyint_max = params->idr_period;
if (colorspace == V4L2_COLORSPACE_REC709) {
encp->x_params.vui.i_colmatrix = 1;
} else { // SMPTE170M
encp->x_params.vui.i_colmatrix = 6;
}
encp->x_params.rc.i_bitrate = params->bitrate / 1000;
encp->x_params.rc.i_vbv_buffer_size = params->bitrate / 1000;
encp->x_params.rc.i_vbv_max_bitrate = params->bitrate / 1000;
encp->x_params.rc.i_rc_method = X264_RC_ABR;
encp->x_params.i_bframe = 0;
res = x264_param_apply_profile(&encp->x_params, PROFILE);
if (res < 0) {
set_error("x264_param_apply_profile() failed");
goto failed;
}
encp->x_handler = x264_encoder_open(&encp->x_params);
x264_picture_init(&encp->x_pic_in);
encp->x_pic_in.img.i_csp = encp->x_params.i_csp;
encp->x_pic_in.img.i_plane = 3;
encp->x_pic_in.img.i_stride[0] = stride;
encp->x_pic_in.img.i_stride[1] = stride >> 1;
encp->x_pic_in.img.i_stride[2] = stride >> 1;
x264_picture_alloc(&encp->x_pic_out, encp->x_params.i_csp, encp->x_params.i_width, encp->x_params.i_height);
encp->params = params;
encp->output_cb = output_cb;
pthread_mutex_init(&encp->mutex, NULL);
return true;
failed:
free(*enc);
return false;
}
void encoder_soft_h264_encode(encoder_soft_h264_t *enc, uint8_t *buffer_mapped, int buffer_fd, size_t buffer_size, uint64_t timestamp) {
encoder_soft_h264_priv_t *encp = (encoder_soft_h264_priv_t *)enc;
encp->x_pic_in.img.plane[0] = buffer_mapped; // Y
encp->x_pic_in.img.plane[1] = encp->x_pic_in.img.plane[0] + encp->x_pic_in.img.i_stride[0] * encp->params->height; // U
encp->x_pic_in.img.plane[2] = encp->x_pic_in.img.plane[1] + (encp->x_pic_in.img.i_stride[0] / 2) * (encp->params->height / 2); // V
encp->x_pic_in.i_pts = encp->next_pts++;
pthread_mutex_lock(&encp->mutex);
x264_nal_t *nal;
int nal_count;
int frame_size = x264_encoder_encode(encp->x_handler, &nal, &nal_count, &encp->x_pic_in, &encp->x_pic_out);
pthread_mutex_unlock(&encp->mutex);
encp->output_cb(nal->p_payload, frame_size, timestamp);
}
void encoder_soft_h264_reload_params(encoder_soft_h264_t *enc, const parameters_t *params) {
encoder_soft_h264_priv_t *encp = (encoder_soft_h264_priv_t *)enc;
pthread_mutex_lock(&encp->mutex);
encp->x_params.i_keyint_max = params->idr_period;
encp->x_params.rc.i_bitrate = params->bitrate / 1000;
encp->x_params.rc.i_vbv_buffer_size = params->bitrate / 1000;
encp->x_params.rc.i_vbv_max_bitrate = params->bitrate / 1000;
int res = x264_encoder_reconfig(encp->x_handler, &encp->x_params);
if (res != 0) {
fprintf(stderr, "x264_encoder_reconfig() failed\n");
}
encp->params = params;
pthread_mutex_unlock(&encp->mutex);
}