forked from OtherCrashOverride/c2_vpcodec
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAML_HWEncoder.cpp
More file actions
executable file
·409 lines (363 loc) · 16.1 KB
/
AML_HWEncoder.cpp
File metadata and controls
executable file
·409 lines (363 loc) · 16.1 KB
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//#define LOG_NDEBUG 0
#define LOG_TAG "AMLVENC"
//#include <utils/Log.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include "AML_HWEncoder.h"
#include "enc_api.h"
AMVEnc_Status DetermineFrameNum(AMVEncHandle *Handle, amvenc_info_t* info, uint32 modTime, uint *frameNum, uint32 new_frame_rate, bool force_IDR)
{
uint32 modTimeRef = info->modTimeRef;
int32 currFrameNum ;
int frameInc;
int border = 200;
bool no_skip = false;
/* for now, the default is to encode every frame, To Be Changed */
if (info->first_frame){
info->modTimeRef = modTime;
info->wrapModTime = 0;
info->prevProcFrameNum = 0;
info->prevProcFrameNumOffset = 0;
info->lastTimeRef = 0;
*frameNum = 0;
/* set frame type to IDR-frame */
info->nal_unit_type = AVC_NALTYPE_IDR;
info->slice_type = AVC_I_SLICE;
info->late_frame_count = 0;
info->frame_rate = new_frame_rate;
return AMVENC_SUCCESS;
}else{
if((info->freerun == true)||(force_IDR == true))
no_skip = true;
if(info->frame_rate != new_frame_rate){
info->prevProcFrameNumOffset += info->prevProcFrameNum;
info->late_frame_count = 0;
info->prevProcFrameNum = 0;
info->modTimeRef = info->lastTimeRef;
modTimeRef = info->modTimeRef;
info->wrapModTime = 0;
info->frame_rate = new_frame_rate;
no_skip = true;
}
if (modTime < modTimeRef){
/* modTime wrapped around */
info->wrapModTime += ((uint32)0xFFFFFFFF - modTimeRef) + 1;
info->modTimeRef = modTimeRef = 0;
}
modTime += info->wrapModTime; /* wrapModTime is non zero after wrap-around */
currFrameNum = (int32)(((modTime - modTimeRef) * (float)info->frame_rate + border) / 1000); /* add small roundings */
if((currFrameNum <=(int32)(info->prevProcFrameNum - info->late_frame_count))&&(no_skip == false)){
return AMVENC_FAIL; /* this is a late frame do not encode it */
}
frameInc = currFrameNum - info->prevProcFrameNum+info->late_frame_count;
if((currFrameNum - info->prevProcFrameNum)>1)
info->late_frame_count += (int)(currFrameNum - info->prevProcFrameNum -1);
else if((info->late_frame_count>0)&&((currFrameNum - info->prevProcFrameNum) == 0))
info->late_frame_count--;
if ((frameInc < info->skip_next_frame + 1)&&(no_skip == false)){
return AMVENC_FAIL; /* frame skip required to maintain the target bit rate. */
}
//AMPreRateControl(&info->hw_info, frameInc, &IDR);
AMPreRateControl(&info->hw_info, modTime, force_IDR);//frameInc is useless in m8 ratectrl,so use it to pass timecode;
*frameNum = currFrameNum+info->prevProcFrameNumOffset;
info->lastTimeRef = modTime;
/* This part would be similar to DetermineVopType of m4venc */
if (*frameNum >= (uint)info->idrPeriod && info->idrPeriod > 0){ /* first frame or IDR*/
//info->modTimeRef += (uint32)(info->idrPeriod * 1000 / info->frame_rate);
info->modTimeRef = modTime;//add this to avoid next modTime too small
*frameNum -= info->idrPeriod;
info->nal_unit_type = AVC_NALTYPE_IDR;
info->slice_type = AVC_I_SLICE;
info->prevProcFrameNum = *frameNum;
info->prevProcFrameNumOffset = 0;
}else{
if(force_IDR){ // todo: force_IDR only need one IDR or reset gop.
//ALOGV("force next frame to idr :%d, handle: %p.",force_IDR, Handle);
info->nal_unit_type = AVC_NALTYPE_IDR;
info->slice_type = AVC_I_SLICE;
*frameNum = 0;
info->prevProcFrameNum = 0;
info->modTimeRef = modTime;
info->prevProcFrameNumOffset = 0;
//ALOGV("refresh parameter, handle: %p.", Handle);
}else{
info->nal_unit_type = AVC_NALTYPE_SLICE;
info->slice_type = AVC_P_SLICE;
info->prevProcFrameNum = currFrameNum;
}
}
}
//ALOGV("Get Nal Type: %s, handle: %p.", (info->nal_unit_type ==AVC_NALTYPE_IDR)?"IDR":"SLICE", Handle);
return AMVENC_SUCCESS;
}
AMVEnc_Status AML_HWEncInitialize(AMVEncHandle *Handle, AMVEncParams *encParam, bool* has_mix, int force_mode)
{
AMVEnc_Status status = AMVENC_FAIL;
amvenc_info_t* info = (amvenc_info_t*)calloc(1,sizeof(amvenc_info_t));
if((!info)||(!Handle)||(!encParam)){
status = AMVENC_MEMORY_FAIL;
goto exit;
}
if (encParam->frame_rate == 0){
status = AMVENC_INVALID_FRAMERATE;
goto exit;
}
memset(info,0,sizeof(amvenc_info_t));
info->hw_info.dev_fd = -1;
info->hw_info.dev_id = NO_DEFINE;
info->enc_width = encParam->width;
info->enc_height = encParam->height;
info->outOfBandParamSet = encParam->out_of_band_param_set;
info->fullsearch_enable = encParam->fullsearch;
/* now the rate control and performance related parameters */
info->scdEnable = encParam->auto_scd;
info->idrPeriod = encParam->idr_period + 1;
info->search_range = encParam->search_range;
info->frame_rate = (float)(encParam->frame_rate * 1.0 / 1000);
info->rcEnable = (encParam->rate_control == AVC_ON)?true:false;
info->bitrate = encParam->bitrate;
info->cpbSize = encParam->CPB_size;
info->initDelayOffset = (info->bitrate * encParam->init_CBP_removal_delay / 1000);
info->initQP = encParam->initQP;
info->freerun = (encParam->FreeRun== AVC_ON)?true:false;
info->nSliceHeaderSpacing = encParam->nSliceHeaderSpacing;
info->MBsIntraRefresh = encParam->MBsIntraRefresh;
info->MBsIntraOverlap = encParam->MBsIntraOverlap;
if(info->initQP == 0){
#if 0
double L1, L2, L3, bpp;
bpp = 1.0 * info->bitrate /(info->frame_rate * (info->enc_width *info->enc_height));
if (info->enc_width <= 176){
L1 = 0.1;
L2 = 0.3;
L3 = 0.6;
}else if (info->enc_width <= 352){
L1 = 0.2;
L2 = 0.6;
L3 = 1.2;
}else{
L1 = 0.6;
L2 = 1.4;
L3 = 2.4;
}
if (bpp <= L1)
info->initQP = 30;
else if (bpp <= L2)
info->initQP = 25;
else if (bpp <= L3)
info->initQP = 20;
else
info->initQP = 15;
#else
info->initQP = 26;
#endif
}
info->hw_info.init_para.enc_width = info->enc_width;
info->hw_info.init_para.enc_height = info->enc_height;
info->hw_info.init_para.initQP = info->initQP;
info->hw_info.init_para.nSliceHeaderSpacing = info->nSliceHeaderSpacing;
info->hw_info.init_para.MBsIntraRefresh = info->MBsIntraRefresh;
info->hw_info.init_para.MBsIntraOverlap = info->MBsIntraOverlap;
info->hw_info.init_para.rcEnable = info->rcEnable;
info->hw_info.init_para.bitrate= info->bitrate;
info->hw_info.init_para.frame_rate= info->frame_rate;
info->hw_info.init_para.cpbSize = info->cpbSize;
info->hw_info.init_para.bitrate_scale = (encParam->BitrateScale== AVC_ON)?true:false;
info->hw_info.init_para.encode_once = encParam->encode_once;
status = InitAMVEncode(&info->hw_info,force_mode);
if(status != AMVENC_SUCCESS){
goto exit;
}
encParam->dev_id = info->hw_info.dev_id;
if(AMInitRateControlModule(&info->hw_info)!=AMVENC_SUCCESS){
status = AMVENC_MEMORY_FAIL;
goto exit;
}
info->first_frame = true; /* set this flag for the first time */
info->modTimeRef = 0; /* ALWAYS ASSUME THAT TIMESTAMP START FROM 0 !!!*/
info->prevCodedFrameNum = 0;
info->late_frame_count = 0;
if (info->outOfBandParamSet)
info->state= AMVEnc_Encoding_SPS;
else
info->state = AMVEnc_Analyzing_Frame;
Handle->object = (void *)info;
*has_mix = (info->hw_info.dev_id == M8)?true:false;
//ALOGD("AML_HWEncInitialize success, handle: %p, fd:%d",Handle, info->hw_info.dev_fd);
return AMVENC_SUCCESS;
exit:
if(info){
AMCleanupRateControlModule(&info->hw_info);
UnInitAMVEncode(&info->hw_info);
free(info);
}
//ALOGE("AML_HWEncInitialize Fail, error=%d. handle: %p",status,Handle);
return status;
}
AMVEnc_Status AML_HWSetInput(AMVEncHandle *Handle, AMVEncFrameIO *input)
{
uint frameNum;
amvenc_info_t* info = (amvenc_info_t*)Handle->object;
AMVEnc_Status status = AMVENC_FAIL;
ulong yuv[13];
if (info == NULL){
//ALOGE("AML_HWSetInput Fail: UNINITIALIZED. handle: %p", Handle);
return AMVENC_UNINITIALIZED;
}
if (info->state== AMVEnc_WaitingForBuffer){
goto RECALL_INITFRAME;
}else if (info->state != AMVEnc_Analyzing_Frame){
//ALOGE("AML_HWSetInput Wrong state: %d. handle: %p",info->state, Handle);
return AMVENC_FAIL;
}
if (input->pitch > 0xFFFF){
//ALOGE("AML_HWSetInput Fail: NOT_SUPPORTED. handle: %p", Handle);
return AMVENC_NOT_SUPPORTED; // we use 2-bytes for pitch
}
if (AMVENC_SUCCESS != DetermineFrameNum(Handle, info, input->coding_timestamp, &frameNum, (uint32)input->frame_rate, (input->op_flag & AMVEncFrameIO_FORCE_IDR_FLAG))){
//ALOGD("AML_HWSetInput SKIPPED_PICTURE, handle: %p", Handle);
return AMVENC_SKIPPED_PICTURE; /* not time to encode, thus skipping */
}
yuv[0]= input->YCbCr[0];
yuv[1]= input->YCbCr[1];
yuv[2]= input->YCbCr[2];
yuv[3]= input->canvas;
yuv[4]= (input->op_flag & AMVEncFrameIO_FORCE_SKIP_FLAG)?1:0;
yuv[5]= input->crop_top;
yuv[6]= input->crop_bottom;
yuv[7]= input->crop_left;
yuv[8]= input->crop_right;
yuv[9]= input->pitch;
yuv[10]= input->height;
yuv[11]= input->scale_width;
yuv[12]= input->scale_height;
info->coding_order = frameNum;
RECALL_INITFRAME:
/* initialize and analyze the frame */
status = AMVEncodeInitFrame(&info->hw_info, &yuv[0], input->type, input->fmt,(info->nal_unit_type == AVC_NALTYPE_IDR));
if((status == AMVENC_NEW_IDR)&&(info->nal_unit_type != AVC_NALTYPE_IDR)){
info->modTimeRef = input->coding_timestamp;
info->coding_order = 0;
info->nal_unit_type = AVC_NALTYPE_IDR;
info->slice_type = AVC_I_SLICE;
info->prevProcFrameNum = 0;
}
AMRCInitFrameQP(&info->hw_info,(info->nal_unit_type == AVC_NALTYPE_IDR), input->bitrate, input->frame_rate);
if (status == AMVENC_SUCCESS){
info->state = AMVEnc_Encoding_Frame;
}else if (status == AMVENC_NEW_IDR){
if (info->outOfBandParamSet)
info->state = AMVEnc_Encoding_Frame;
else // assuming that in-band paramset keeps sending new SPS and PPS.
info->state = AMVEnc_Encoding_SPS;
}else if (status == AMVENC_PICTURE_READY){
info->state = AMVEnc_WaitingForBuffer; // Input accepted but can't continue
}
//ALOGV("AML_HWSetInput status: %d. handle: %p",status,Handle);
return status;
}
AMVEnc_Status AML_HWEncNAL(AMVEncHandle *Handle, unsigned char *buffer, unsigned int *buf_nal_size, int *nal_type)
{
AMVEnc_Status ret = AMVENC_FAIL;
amvenc_info_t* info = (amvenc_info_t*)Handle->object;
int datalen = 0;
if (info == NULL){
//ALOGE("AML_HWEncNAL Fail: UNINITIALIZED. handle: %p", Handle);
return AMVENC_UNINITIALIZED;
}
//ALOGV("AML_HWEncNAL state: %d. handle: %p",info->state, Handle);
switch (info->state){
case AMVEnc_Initializing:
return AMVENC_ALREADY_INITIALIZED;
case AMVEnc_Encoding_SPS:
/* encode SPS */
ret = AMVEncodeSPS_PPS(&info->hw_info, buffer,&datalen);
if(ret!=AMVENC_SUCCESS){
//ALOGV("AML_HWEncNAL state: %d, err=%d, handle: %p",info->state,ret, Handle);
return ret;
}
if (info->outOfBandParamSet)
info->state = AMVEnc_Analyzing_Frame;
else // SetInput has been called before SPS and PPS.
info->state = AMVEnc_Encoding_Frame;
*nal_type = AVC_NALTYPE_PPS;
*buf_nal_size = datalen;
break;
case AMVEnc_Encoding_Frame:
ret = AMVEncodeSlice(&info->hw_info,buffer,&datalen, false);
if (ret == AMVENC_TIMEOUT){
//ALOGD("AML_HWEncNAL state: %d, err=%d. handle: %p",info->state,ret, Handle);
//ret = AMVENC_SKIPPED_PICTURE; //need debug
info->state = AMVEnc_Analyzing_Frame; // walk around to fix the poll timeout
return ret;
}
if ((ret != AMVENC_PICTURE_READY)&&(ret != AMVENC_SUCCESS)&&(ret != AMVENC_NEW_IDR)){
//ALOGV("AML_HWEncNAL state: %d, err=%d. handle: %p",info->state,ret, Handle);
return ret;
}
info->nal_unit_type = (ret == AMVENC_NEW_IDR)?AVC_NALTYPE_IDR:info->nal_unit_type;
*nal_type = info->nal_unit_type;
*buf_nal_size = datalen;
if((ret == AMVENC_PICTURE_READY)||(ret == AMVENC_NEW_IDR)){
info->first_frame = false;
ret = AMPostRateControl(&info->hw_info,(info->nal_unit_type == AVC_NALTYPE_IDR),&info->skip_next_frame,(datalen<<3));
if((ret == AMVENC_SKIPPED_PICTURE)&&(info->freerun ==false)){
info->state = AMVEnc_Analyzing_Frame;
return ret;
}
/*we need to reencode the frame**/
if (ret == AMVENC_REENCODE_PICTURE) {
//ALOGE("AML_HWEncNAL re-encode");
ret = AMVEncodeSlice(&info->hw_info,buffer,&datalen, true);
if ((ret != AMVENC_PICTURE_READY)&&(ret != AMVENC_SUCCESS)&&(ret != AMVENC_NEW_IDR)){
//ALOGE("AML_HWEncNAL state: %d, err=%d. handle: %p",info->state,ret, Handle);
return ret;
}
info->nal_unit_type = (ret == AMVENC_NEW_IDR)?AVC_NALTYPE_IDR:info->nal_unit_type;
*nal_type = info->nal_unit_type;
*buf_nal_size = datalen;
if(ret == AMVENC_NEW_IDR){
info->first_frame = false;
ret = AMPostRateControl(&info->hw_info,(info->nal_unit_type == AVC_NALTYPE_IDR)/*true*/,&info->skip_next_frame,(datalen<<3));
if ((ret == AMVENC_SKIPPED_PICTURE)&&(info->freerun ==false)){
info->state = AMVEnc_Analyzing_Frame;
return ret;
}
}else{
//ALOGE("re-encode failed:%d. handle: %p",ret, Handle);
}
}
if(AMVEncodeCommit(&info->hw_info,(info->nal_unit_type == AVC_NALTYPE_IDR))!=AMVENC_SUCCESS){
//ALOGE("Encode Commit failed:%d. handle: %p",ret, Handle);
}
info->prevCodedFrameNum = info->coding_order;
info->state = AMVEnc_Analyzing_Frame;
if(ret != AMVENC_FORCE_IDR_NEXT)
ret = AMVENC_PICTURE_READY;
}else{
ret = AMVENC_SUCCESS;
}
break;
default:
ret = AMVENC_WRONG_STATE;
}
//ALOGV("AML_HWEncNAL next state: %d, ret=%d. handle: %p",info->state, ret, Handle);
return ret;
}
AMVEnc_Status AML_HWEncRelease(AMVEncHandle *Handle)
{
amvenc_info_t* info = (amvenc_info_t*)Handle->object;
if(info){
AMCleanupRateControlModule(&info->hw_info);
UnInitAMVEncode(&info->hw_info);
free(info);
}
Handle->object = NULL;
//ALOGV("AML_HWEncRelease Done, handle: %p", Handle);
return AMVENC_SUCCESS;
}