-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathenc.cpp
170 lines (135 loc) · 4.31 KB
/
enc.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
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
#include "stdafx.h"
#include "cs.h"
#include <assert.h>
#include "./libenc/common/common.h"
#include "./libenc/x264.h"
#pragma comment(lib, "./lib/libenc.lib")
int frame_type[7];
struct x264_handle_wrapper{
x264_t* x264_handle;
long id;
int tps;
x264_picture_t pic;
};
long current = 0;
x264_handle_wrapper wrapper = {0, -1};
extern CRITICAL_SECTION cse;
int x264_open(int width, int height, int framerate, int bitrate, int ifrm_min_interval, int ifrm_max_interval, int tps){
long id = InterlockedExchangeAdd(¤t, 2);
InterlockedCompareExchange(&wrapper.id, id, -1);
if(wrapper.id != id){
return -1;
}
x264_param_t param;
x264_param_default(¶m);
param.rc.i_bitrate = bitrate;
param.rc.i_rc_method = X264_RC_ABR;//X264_RC_ABR;
param.rc.i_vbv_max_bitrate = bitrate * 1.5;
param.analyse.i_me_method = X264_ME_UMH;
param.analyse.i_subpel_refine = 9;
//param.rc.f_rf_constant = 23;
//param.rc.i_rc_method = X264_RC_CRF;
//param.rc.i_rc_method = X264_RC_CQP;
//param.rc.f_ip_factor = atof(value);
//param.rc.f_pb_factor = atof(value);
//param.rc.i_qp_constant = atof(value);
param.rc.f_qcompress = 0;
param.rc.i_lookahead = 0;
param.i_frame_reference = 3;
//param.analyse.b_mixed_references = 0;
//param.analyse.inter = X264_ANALYSE_I8x8|X264_ANALYSE_I4x4;
param.b_repeat_headers = 0;
param.b_annexb = 0;
param.b_dts_compress = 1;
param.i_bframe = 0;
param.b_vfr_input = 0;
param.i_fps_num = framerate;
param.i_fps_den = 1;
param.i_timebase_num = param.i_fps_den;
param.i_timebase_den = param.i_fps_num;
param.i_width = width;
param.i_height = height;
//modify by xingyanju
//{{
// param.i_keyint_min = ifrm_min_interval;
// param.i_open_gop = X264_OPEN_GOP_BLURAY;
//}}
param.i_keyint_max = ifrm_max_interval;
if(param.i_keyint_max < param.i_keyint_min){
param.i_keyint_max = param.i_keyint_min;
}
x264_t* p = x264_encoder_open(¶m);
if(p == NULL){
wrapper.id = -1;
return -1;
}
wrapper.x264_handle = p;
wrapper.tps = tps;
x264_picture_init(&wrapper.pic);
return (int) wrapper.id;
}
int x264_enc_header(int handle, x264_rawpic* out){
if(out == NULL || handle == -1 || wrapper.id != handle){
return -1;
}
x264_nal_t *headers;
int i_nal;
int n = x264_encoder_headers(wrapper.x264_handle, &headers, &i_nal) ;
if(n < 0){
return -1;
}
if(i_nal > 0){
out->o_nal = (x264_nal *) headers;
out->o_nr_nal = i_nal;
}else{
out->o_nal = NULL;
out->o_nr_nal = 0;
}
return out->o_nr_nal;
}
int x264_encode(int handle, x264_rawpic* in, x264_rawpic* out){
if(out == NULL || handle == -1 || wrapper.id != handle){
return -1;
}
x264_picture_t* pic = &wrapper.pic;
x264_param_t* param = &wrapper.x264_handle->thread[wrapper.x264_handle->i_thread_phase]->param;
memcpy(pic->img.i_stride, in->i_stride, sizeof(in->i_stride));
memcpy(pic->img.plane, in->i_plane, sizeof(in->i_plane));
pic->img.i_csp = in->i_csp;
pic->img.i_plane = in->i_planes;
pic->i_type = X264_TYPE_AUTO;
pic->i_qpplus1 = 0;
pic->i_pts = in->io_pts;
x264_picture_t pic_out;
x264_nal_t *nal;
int i_nal;
EnterCriticalSection(&cse);
if(wrapper.id != handle){
LeaveCriticalSection(&cse);
return -1;
}
int i_frame_size = x264_encoder_encode(wrapper.x264_handle, &nal, &i_nal, pic, &pic_out);
LeaveCriticalSection(&cse);
if(i_frame_size < 0){
return -1;
}
if(i_frame_size > 0){
out->io_pts = (int64_t) ((pic_out.i_pts * wrapper.tps * ((double) param->i_timebase_num / param->i_timebase_den)) + 0.5);
out->o_dts = (int64_t) ((pic_out.i_dts * wrapper.tps * ((double) param->i_timebase_num / param->i_timebase_den)) + 0.5);
out->o_type = frame_type[pic_out.i_type];
out->o_keyfrm = pic_out.b_keyframe;
out->o_nal = (x264_nal *) nal;
out->o_nr_nal = i_nal;
}
return i_frame_size;
}
int x264_close(int handle){
if(handle == -1 || wrapper.id != handle){
return -1;
}
EnterCriticalSection(&cse);
x264_encoder_close(wrapper.x264_handle);
wrapper.id = -1;
LeaveCriticalSection(&cse);
return 0;
}