custom_video_enc_h264.c
18.7 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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#ifdef __VIDEO_ARCHI_V2__
/* __H264_ENC_SUPPORT__ is a global option. */
#ifdef __H264_ENC_SUPPORT__
#include <stddef.h>
#include "kal_release.h"
#include "custom_video_enc_setting.h"
#include "custom_video_enc_h264.h"
#ifdef __MMI_EM_MISC_RECORDER_PARAM_AJUST__
#include "EngineerModeRecorderSetting.h"
#endif
// #include "drv_comm.h" // for drv_trace1(), drv_trace8()
/*****************************************************************************
* Static global variables
*****************************************************************************/
static CUSTOM_VENC_H264_MGR_T _rCustomVencH264Mgr;
/*****************************************************************************
* Clear the saved index of table selection. All "Set" functions need to
* call this function so that the frame rate table will be search again with
* the new set value.
*****************************************************************************/
static __inline _CustomVenc_H264_ClearTableSelection(void)
{
_rCustomVencH264Mgr.u2TableIndex = CUSTOM_VENC_H264_UNKNOWN_TABLE_INDEX;
}
/*****************************************************************************
* Memory pointed by the input parameters is allocated by caller.
*****************************************************************************/
static CUSTOM_VENC_ERROR_T _CustomVenc_H264_MakeTableSelection(kal_uint16 *pu2TableIndex)
{
CUSTOM_VENC_ERROR_T eError = CUSTOM_VENC_ERROR_NONE;
const kal_uint16 u2TableEntries = _rCustomVencH264Mgr.u2TableEntries;
kal_uint16 u2TableIndex = _rCustomVencH264Mgr.u2TableIndex;
if (!(u2TableEntries != CUSTOM_VENC_H264_UNKNOWN_TABLE_ENTRIES)) { ASSERT(0); }
if (!(_rCustomVencH264Mgr.u2Width != CUSTOM_VENC_H264_UNKNOWN_WIDTH)) { ASSERT(0); }
if (!(_rCustomVencH264Mgr.u2Height != CUSTOM_VENC_H264_UNKNOWN_HEIGHT)) { ASSERT(0); }
if (!(_rCustomVencH264Mgr.eQuality != CUSTOM_VENC_QUALITY_UNKNOWN)) { ASSERT(0); }
if (!(_rCustomVencH264Mgr.eStorage != CUSTOM_VENC_STORAGE_UNKNOWN)) { ASSERT(0); }
if (!(_rCustomVencH264Mgr.eCodec != CUSTOM_VENC_CODEC_UNKNOWN)) { ASSERT(0); }
// if (_rCustomVencH264Mgr.eResolution == CUSTOM_VENC_RESOLUTION_UNKNOWN)
{
kal_uint16 u2Width, u2Height;
CUSTOM_VENC_RESOLUTION_T eResolution;
u2Width = _rCustomVencH264Mgr.u2Width;
u2Height = _rCustomVencH264Mgr.u2Height;
eError = CustomVenc_MapWidthAndHeightToResolution(u2Width, u2Height, &eResolution);
if (!(eError == CUSTOM_VENC_ERROR_NONE)) { ASSERT(0); }
if (!(eResolution != CUSTOM_VENC_RESOLUTION_UNKNOWN)) { ASSERT(0); }
_rCustomVencH264Mgr.eResolution = eResolution;
}
if (u2TableIndex == CUSTOM_VENC_H264_UNKNOWN_TABLE_INDEX)
{
for (u2TableIndex = 0; u2TableIndex < u2TableEntries; u2TableIndex++)
{
if (_rH264FrameRateTable[u2TableIndex].rUpperTable.eResolution == _rCustomVencH264Mgr.eResolution)
{
if ((_rH264FrameRateTable[u2TableIndex].rUpperTable.eQuality == CUSTOM_VENC_QUALITY_ALL) ||
(_rH264FrameRateTable[u2TableIndex].rUpperTable.eQuality == _rCustomVencH264Mgr.eQuality))
{
if ((_rH264FrameRateTable[u2TableIndex].rUpperTable.eStorage == CUSTOM_VENC_STORAGE_ALL) ||
(_rH264FrameRateTable[u2TableIndex].rUpperTable.eStorage == _rCustomVencH264Mgr.eStorage))
{
if ((_rH264FrameRateTable[u2TableIndex].rUpperTable.eCodec == CUSTOM_VENC_CODEC_ALL) ||
(_rH264FrameRateTable[u2TableIndex].rUpperTable.eCodec == _rCustomVencH264Mgr.eCodec))
{
_rCustomVencH264Mgr.u2TableIndex = u2TableIndex;
eError = CUSTOM_VENC_ERROR_NONE;
break;
}
}
}
}
}
if (u2TableIndex == u2TableEntries)
{
eError = CUSTOM_VENC_ERROR_NO_MATCH;
}
}
else
{
eError = CUSTOM_VENC_ERROR_NONE;
}
*pu2TableIndex = u2TableIndex;
return eError;
}
/*****************************************************************************
*
*****************************************************************************/
static kal_uint16 _CustomVenc_H264_ComputeNumOfEntries(void)
{
if (!(sizeof(_rH264FrameRateTable) != 0)) { ASSERT(0); }
if (!(sizeof(_rH264FrameRateTable[0]) != 0)) { ASSERT(0); }
if (!(sizeof(_rDefaultH264FrameRateTable) != 0)) { ASSERT(0); }
if (!(sizeof(_rDefaultH264FrameRateTable[0]) != 0)) { ASSERT(0); }
return (kal_uint16)(sizeof(_rH264FrameRateTable) / sizeof(_rH264FrameRateTable[0]));
}
/*****************************************************************************
*
*****************************************************************************/
static CUSTOM_VENC_ERROR_T _CustomVenc_H264_SetUser(void *pvInput,
kal_uint32 u4InputSize, void *pvOutput, kal_uint32 u4OutputSize)
{
// Clear all internal variables.
_rCustomVencH264Mgr.u2Width = CUSTOM_VENC_H264_UNKNOWN_WIDTH;
_rCustomVencH264Mgr.u2Height = CUSTOM_VENC_H264_UNKNOWN_HEIGHT;
_rCustomVencH264Mgr.u2TableEntries = _CustomVenc_H264_ComputeNumOfEntries();
_rCustomVencH264Mgr.u2TableIndex = CUSTOM_VENC_H264_UNKNOWN_TABLE_INDEX;
_rCustomVencH264Mgr.eResolution = CUSTOM_VENC_RESOLUTION_UNKNOWN;
_rCustomVencH264Mgr.eQuality = CUSTOM_VENC_QUALITY_UNKNOWN;
_rCustomVencH264Mgr.eStorage = CUSTOM_VENC_STORAGE_UNKNOWN;
_rCustomVencH264Mgr.eCodec = CUSTOM_VENC_CODEC_UNKNOWN;
return CUSTOM_VENC_ERROR_NONE;
}
/*****************************************************************************
*
*****************************************************************************/
static CUSTOM_VENC_ERROR_T _CustomVenc_H264_GetNumOfEntries(void *pvInput,
kal_uint32 u4InputSize, void *pvOutput, kal_uint32 u4OutputSize)
{
kal_uint32 *pu4Output;
if (!(pvOutput != NULL)) { ASSERT(0); }
if (!(sizeof(*pu4Output) == u4OutputSize)) { ASSERT(0); }
_rCustomVencH264Mgr.u2TableEntries = _CustomVenc_H264_ComputeNumOfEntries();
pu4Output = (kal_uint32*)pvOutput;
*pu4Output = _rCustomVencH264Mgr.u2TableEntries;
return CUSTOM_VENC_ERROR_NONE;
}
/*****************************************************************************
*
*****************************************************************************/
static CUSTOM_VENC_ERROR_T _CustomVenc_H264_GetOneEntry(void *pvInput,
kal_uint32 u4InputSize, void *pvOutput, kal_uint32 u4OutputSize)
{
kal_uint32 *pu4Index;
CUSTOM_VENC_UPPER_TABLE_T *prUpperTable;
if (!(pvInput != NULL)) { ASSERT(0); }
if (!(sizeof(*pu4Index) == u4InputSize)) { ASSERT(0); }
pu4Index = (kal_uint32*)pvInput;
if (!(_rCustomVencH264Mgr.u2TableEntries != CUSTOM_VENC_H264_UNKNOWN_TABLE_ENTRIES)) { ASSERT(0); }
if (!(*pu4Index < _rCustomVencH264Mgr.u2TableEntries)) { ASSERT(0); }
if (!(pvOutput != NULL)) { ASSERT(0); }
if (!(sizeof(*prUpperTable) == u4OutputSize)) { ASSERT(0); }
prUpperTable = (CUSTOM_VENC_UPPER_TABLE_T*)pvOutput;
*prUpperTable = _rH264FrameRateTable[*pu4Index].rUpperTable;
return CUSTOM_VENC_ERROR_NONE;
}
//----------------------------------------------------------------------------
static CUSTOM_VENC_ERROR_T _CustomVenc_H264_SetField(void *pvInput, kal_uint32 u4InputSize, int field_offset, int size_of_field)
{
kal_uint32 *pInput = (kal_uint32 *) pvInput;
if (!(pvInput != NULL)) { ASSERT(0); }
if (!(sizeof(*pInput) == u4InputSize)) { ASSERT(0); }
switch (size_of_field)
{
case 1:
{
kal_uint8 *pField = (kal_uint8 *)((kal_uint8 *) &_rCustomVencH264Mgr + field_offset);
*pField = (kal_uint8) *pInput;
break;
}
case 2:
{
kal_uint16 *pField = (kal_uint16 *)((kal_uint8 *) &_rCustomVencH264Mgr + field_offset);
*pField = (kal_uint16) *pInput;
break;
}
case 4:
{
kal_uint32 *pField = (kal_uint32 *)((kal_uint8 *) &_rCustomVencH264Mgr + field_offset);
*pField = (kal_uint32) *pInput;
break;
}
default:
{
ASSERT(0);
}
}
_CustomVenc_H264_ClearTableSelection();
return CUSTOM_VENC_ERROR_NONE;
}
//----------------------------------------------------------------------------
static CUSTOM_VENC_ERROR_T _CustomVenc_H264_GetField(void *pvOutput, int u4OutputSize, int field_offset, int size_of_field)
{
const kal_uint16 u2TableEntries = _rCustomVencH264Mgr.u2TableEntries;
kal_uint16 u2TableIndex;
CUSTOM_VENC_ERROR_T eError;
void *pTableEntry = NULL;
kal_uint32 *pOutput = (kal_uint32 *) pvOutput;
if (!pvOutput) { ASSERT(0); }
if (sizeof(kal_uint32) != u4OutputSize) { ASSERT(0); }
eError = _CustomVenc_H264_MakeTableSelection(&u2TableIndex);
if (eError == CUSTOM_VENC_ERROR_NONE)
{
if (!(u2TableEntries != CUSTOM_VENC_H264_UNKNOWN_TABLE_ENTRIES)) { ASSERT(0); }
if (!(u2TableIndex != CUSTOM_VENC_H264_UNKNOWN_TABLE_INDEX)) { ASSERT(0); }
if (!(u2TableIndex < u2TableEntries)) { ASSERT(0); }
pTableEntry = (void *) &_rH264FrameRateTable[u2TableIndex];
}
else
{
pTableEntry = (void *) &_rDefaultH264FrameRateTable[0];
eError = CUSTOM_VENC_ERROR_USE_DEFAULT;
}
if (pTableEntry == NULL) { ASSERT(0); }
switch (size_of_field)
{
case 1:
{
*pOutput = *((kal_uint8 *) ((kal_uint8 *) pTableEntry + field_offset));
break;
}
case 2:
{
*pOutput = *((kal_uint16 *) ((kal_uint8 *) pTableEntry + field_offset));
break;
}
case 4:
{
*pOutput = *((kal_uint32 *) ((kal_uint8 *) pTableEntry + field_offset));
break;
}
default:
{
ASSERT(0);
}
}
return eError;
}
/*****************************************************************************
*
*****************************************************************************/
static CUSTOM_VENC_ERROR_T _CustomVenc_H264_Ctrl(CUSTOM_VENC_OP_T eOp,
void *pvInput, kal_uint32 u4InputSize, void *pvOutput, kal_uint32 u4OutputSize)
{
CUSTOM_VENC_ERROR_T eError;
#ifdef __MMI_EM_MISC_RECORDER_PARAM_AJUST__
if (g_mmi_em_recorder_setting.apply_em_setting == MMI_EM_RECORDER_SETTING_APPLY_YES)
{
kal_int32 *pRes = (kal_int32*) pvOutput;
eError = CUSTOM_VENC_ERROR_NOT_SUPPORTED;
switch (eOp)
{
case CUSTOM_VENC_OP_GET_INTRA_VOP_RATE:
*pRes = g_mmi_em_recorder_setting.intra_vop_rate;
eError = CUSTOM_VENC_ERROR_NONE;
break;
case CUSTOM_VENC_OP_GET_ALGORITHM:
*pRes = g_mmi_em_recorder_setting.algorithm;
eError = CUSTOM_VENC_ERROR_NONE;
break;
case CUSTOM_VENC_OP_GET_BIT_RATE:
*pRes = g_mmi_em_recorder_setting.bit_rate * 1000;
eError = CUSTOM_VENC_ERROR_NONE;
break;
}
if (eError == CUSTOM_VENC_ERROR_NONE)
return eError;
}
#endif
switch (eOp)
{
case CUSTOM_VENC_OP_SET_USER:
eError = _CustomVenc_H264_SetUser(pvInput, u4InputSize, pvOutput, u4OutputSize);
break;
case CUSTOM_VENC_OP_GET_NUM_OF_ENTRIES:
eError = _CustomVenc_H264_GetNumOfEntries(pvInput, u4InputSize, pvOutput, u4OutputSize);
break;
case CUSTOM_VENC_OP_GET_ONE_ENTRY:
eError = _CustomVenc_H264_GetOneEntry(pvInput, u4InputSize, pvOutput, u4OutputSize);
break;
case CUSTOM_VENC_OP_SET_WIDTH:
eError = _CustomVenc_H264_SetField(pvInput, u4InputSize, offsetof(CUSTOM_VENC_H264_MGR_T, u2Width), sizeof(kal_uint16));
break;
case CUSTOM_VENC_OP_SET_HEIGHT:
eError = _CustomVenc_H264_SetField(pvInput, u4InputSize, offsetof(CUSTOM_VENC_H264_MGR_T, u2Height), sizeof(kal_uint16));
break;
case CUSTOM_VENC_OP_SET_QUALITY:
eError = _CustomVenc_H264_SetField(pvInput, u4InputSize, offsetof(CUSTOM_VENC_H264_MGR_T, eQuality), sizeof(CUSTOM_VENC_QUALITY_T));
break;
case CUSTOM_VENC_OP_SET_STORAGE:
eError = _CustomVenc_H264_SetField(pvInput, u4InputSize, offsetof(CUSTOM_VENC_H264_MGR_T, eStorage), sizeof(CUSTOM_VENC_STORAGE_T));
break;
case CUSTOM_VENC_OP_SET_CODEC:
eError = _CustomVenc_H264_SetField(pvInput, u4InputSize, offsetof(CUSTOM_VENC_H264_MGR_T, eCodec), sizeof(CUSTOM_VENC_CODEC_T));
break;
case CUSTOM_VENC_OP_GET_SENSOR_FRAME_RATE:
eError = _CustomVenc_H264_GetField(pvOutput,
u4OutputSize,
offsetof(VIDEO_ENC_REC_H264_FRAME_RATE_TABLE_T, rLowerTable) +
offsetof(CUSTOM_VENC_REC_H264_LOWER_TABLE_T, u2SensorFrameRate),
sizeof(kal_uint16));
break;
case CUSTOM_VENC_OP_GET_SENSOR_NIGHT_FRAME_RATE:
eError = _CustomVenc_H264_GetField(pvOutput,
u4OutputSize,
offsetof(VIDEO_ENC_REC_H264_FRAME_RATE_TABLE_T, rLowerTable) +
offsetof(CUSTOM_VENC_REC_H264_LOWER_TABLE_T, u2SensorNightFrameRate),
sizeof(kal_uint16));
break;
case CUSTOM_VENC_OP_GET_ENCODER_FRAME_RATE:
eError = _CustomVenc_H264_GetField(pvOutput,
u4OutputSize,
offsetof(VIDEO_ENC_REC_H264_FRAME_RATE_TABLE_T, rLowerTable) +
offsetof(CUSTOM_VENC_REC_H264_LOWER_TABLE_T, u2CodecFrameRate),
sizeof(kal_uint16));
break;
case CUSTOM_VENC_OP_GET_ENCODER_NIGHT_FRAME_RATE:
eError = _CustomVenc_H264_GetField(pvOutput,
u4OutputSize,
offsetof(VIDEO_ENC_REC_H264_FRAME_RATE_TABLE_T, rLowerTable) +
offsetof(CUSTOM_VENC_REC_H264_LOWER_TABLE_T, u2CodecNightFrameRate),
sizeof(kal_uint16));
break;
case CUSTOM_VENC_OP_GET_INTRA_VOP_RATE:
eError = _CustomVenc_H264_GetField(pvOutput,
u4OutputSize,
offsetof(VIDEO_ENC_REC_H264_FRAME_RATE_TABLE_T, rLowerTable) +
offsetof(CUSTOM_VENC_REC_H264_LOWER_TABLE_T, u4KeyintMax),
sizeof(kal_uint32));
break;
case CUSTOM_VENC_OP_GET_ALGORITHM:
eError = _CustomVenc_H264_GetField(pvOutput,
u4OutputSize,
offsetof(VIDEO_ENC_REC_H264_FRAME_RATE_TABLE_T, rLowerTable) +
offsetof(CUSTOM_VENC_REC_H264_LOWER_TABLE_T, u4AlgorithmSelect),
sizeof(kal_uint32));
break;
case CUSTOM_VENC_OP_GET_BIT_RATE:
eError = _CustomVenc_H264_GetField(pvOutput,
u4OutputSize,
offsetof(VIDEO_ENC_REC_H264_FRAME_RATE_TABLE_T, rLowerTable) +
offsetof(CUSTOM_VENC_REC_H264_LOWER_TABLE_T, u4bitrate),
sizeof(kal_uint32));
break;
case CUSTOM_VENC_OP_GET_TARGET_COMPLEXITY:
eError = _CustomVenc_H264_GetField(pvOutput,
u4OutputSize,
offsetof(VIDEO_ENC_REC_H264_FRAME_RATE_TABLE_T, rLowerTable) +
offsetof(CUSTOM_VENC_REC_H264_LOWER_TABLE_T, u4TargetComplexity),
sizeof(kal_uint32));
break;
case CUSTOM_VENC_OP_GET_THRESHOLD_AVG_LOW:
eError = _CustomVenc_H264_GetField(pvOutput,
u4OutputSize,
offsetof(VIDEO_ENC_REC_H264_FRAME_RATE_TABLE_T, rLowerTable) +
offsetof(CUSTOM_VENC_REC_H264_LOWER_TABLE_T, u4AvgThLow),
sizeof(kal_uint32));
break;
case CUSTOM_VENC_OP_GET_THRESHOLD_AVG_HIGH:
eError = _CustomVenc_H264_GetField(pvOutput,
u4OutputSize,
offsetof(VIDEO_ENC_REC_H264_FRAME_RATE_TABLE_T, rLowerTable) +
offsetof(CUSTOM_VENC_REC_H264_LOWER_TABLE_T, u4AvgThHigh),
sizeof(kal_uint32));
break;
case CUSTOM_VENC_OP_GET_THRESHOLD_CUR_LOW:
eError = _CustomVenc_H264_GetField(pvOutput,
u4OutputSize,
offsetof(VIDEO_ENC_REC_H264_FRAME_RATE_TABLE_T, rLowerTable) +
offsetof(CUSTOM_VENC_REC_H264_LOWER_TABLE_T, u4CurThLow),
sizeof(kal_uint32));
break;
case CUSTOM_VENC_OP_GET_THRESHOLD_CUR_HIGH:
eError = _CustomVenc_H264_GetField(pvOutput,
u4OutputSize,
offsetof(VIDEO_ENC_REC_H264_FRAME_RATE_TABLE_T, rLowerTable) +
offsetof(CUSTOM_VENC_REC_H264_LOWER_TABLE_T, u4CurThHigh),
sizeof(kal_uint32));
break;
default:
eError = CUSTOM_VENC_ERROR_NOT_SUPPORTED;
// When the error code CUSTOM_VENC_ERROR_NOT_SUPPORTED is received by
// codec, codec will try to use a default value for the requested property.
// For table extension in the future, do not assert here.
// ASSERT(0);
break;
}
return eError;
}
/*****************************************************************************
*
*****************************************************************************/
CUSTOM_VENC_ERROR_T CustomVenc_GetH264CtrlFunction(PFN_CUSTOM_VENC_CTRL *ppfnCustomVencCtrl)
{
if (!(ppfnCustomVencCtrl != NULL)) { ASSERT(0); }
*ppfnCustomVencCtrl = _CustomVenc_H264_Ctrl;
return CUSTOM_VENC_ERROR_NONE;
}
#endif // __H264_ENC_SUPPORT__
#endif // __VIDEO_ARCHI_V2__