-
Notifications
You must be signed in to change notification settings - Fork 3
/
h264enc.c
443 lines (348 loc) · 12.5 KB
/
h264enc.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
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
/*
* Copyright (c) 2014-2015 Jens Kuske <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "h264enc.h"
#include "ve.h"
#define MSG(x) fprintf(stderr, "h264enc: " x "\n")
#define ALIGN(x, a) (((x) + ((typeof(x))(a) - 1)) & ~((typeof(x))(a) - 1))
#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
struct h264enc_internal {
unsigned int mb_width, mb_height, mb_stride;
unsigned int crop_right, crop_bottom;
uint8_t *luma_buffer, *chroma_buffer;
unsigned int input_buffer_size;
enum color_format input_color_format;
uint8_t *bytestream_buffer;
unsigned int bytestream_buffer_size;
unsigned int bytestream_length;
struct h264enc_ref_pic {
void *luma_buffer, *chroma_buffer;
void *extra_buffer; /* unknown purpose, looks like smaller luma */
} ref_picture[2];
void *extra_buffer_line, *extra_buffer_frame; /* unknown purpose */
void *regs;
unsigned int write_sps_pps;
unsigned int profile_idc, level_idc, constraints;
unsigned int entropy_coding_mode_flag;
unsigned int pic_init_qp;
unsigned int keyframe_interval;
unsigned int current_frame_num;
enum slice_type { SLICE_P = 0, SLICE_I = 2 } current_slice_type;
unsigned int streaming_mode;
};
static void put_bits(void* regs, uint32_t x, int num)
{
writel(x, regs + VE_AVC_BASIC_BITS);
writel(0x1 | ((num & 0x1f) << 8), regs + VE_AVC_TRIGGER);
/* again the problem, how to check for finish? */
}
static void put_ue(void* regs, uint32_t x)
{
x++;
put_bits(regs, x, (32 - __builtin_clz(x)) * 2 - 1);
}
static void put_se(void* regs, int x)
{
x = 2 * x - 1;
x ^= (x >> 31);
put_ue(regs, x);
}
static void put_start_code(void* regs, unsigned int nal_ref_idc, unsigned int nal_unit_type)
{
uint32_t tmp = readl(regs + VE_AVC_PARAM);
/* disable emulation_prevention_three_byte */
writel(tmp | (0x1 << 31), regs + VE_AVC_PARAM);
put_bits(regs, 0, 24);
put_bits(regs, 0x100 | (nal_ref_idc << 5) | (nal_unit_type << 0), 16);
writel(tmp, regs + VE_AVC_PARAM);
}
static void put_rbsp_trailing_bits(void* regs)
{
unsigned int cur_bs_len = readl(regs + VE_AVC_VLE_LENGTH);
int num_zero_bits = 8 - ((cur_bs_len + 1) & 0x7);
put_bits(regs, 1 << num_zero_bits, num_zero_bits + 1);
}
static void put_seq_parameter_set(h264enc *c)
{
put_start_code(c->regs, 3, 7);
put_bits(c->regs, c->profile_idc, 8);
put_bits(c->regs, c->constraints, 8);
put_bits(c->regs, c->level_idc, 8);
put_ue(c->regs, /* seq_parameter_set_id = */ 0);
put_ue(c->regs, /* log2_max_frame_num_minus4 = */ 0);
put_ue(c->regs, /* pic_order_cnt_type = */ 2);
put_ue(c->regs, /* max_num_ref_frames = */ 1);
put_bits(c->regs, /* gaps_in_frame_num_value_allowed_flag = */ 0, 1);
put_ue(c->regs, c->mb_width - 1);
put_ue(c->regs, c->mb_height - 1);
put_bits(c->regs, /* frame_mbs_only_flag = */ 1, 1);
put_bits(c->regs, /* direct_8x8_inference_flag = */ 0, 1);
unsigned int frame_cropping_flag = c->crop_right || c->crop_bottom;
put_bits(c->regs, frame_cropping_flag, 1);
if (frame_cropping_flag)
{
put_ue(c->regs, 0);
put_ue(c->regs, c->crop_right);
put_ue(c->regs, 0);
put_ue(c->regs, c->crop_bottom);
}
put_bits(c->regs, /* vui_parameters_present_flag = */ 0, 1);
put_rbsp_trailing_bits(c->regs);
}
static void put_pic_parameter_set(h264enc *c)
{
put_start_code(c->regs, 3, 8);
put_ue(c->regs, /* pic_parameter_set_id = */ 0);
put_ue(c->regs, /* seq_parameter_set_id = */ 0);
put_bits(c->regs, c->entropy_coding_mode_flag, 1);
put_bits(c->regs, /* bottom_field_pic_order_in_frame_present_flag = */ 0, 1);
put_ue(c->regs, /* num_slice_groups_minus1 = */ 0);
put_ue(c->regs, /* num_ref_idx_l0_default_active_minus1 = */ 0);
put_ue(c->regs, /* num_ref_idx_l1_default_active_minus1 = */ 0);
put_bits(c->regs, /* weighted_pred_flag = */ 0, 1);
put_bits(c->regs, /* weighted_bipred_idc = */ 0, 2);
put_se(c->regs, (int)c->pic_init_qp - 26);
put_se(c->regs, (int)c->pic_init_qp - 26);
put_se(c->regs, /* chroma_qp_index_offset = */ 4);
put_bits(c->regs, /* deblocking_filter_control_present_flag = */ 1, 1);
put_bits(c->regs, /* constrained_intra_pred_flag = */ 0, 1);
put_bits(c->regs, /* redundant_pic_cnt_present_flag = */ 0, 1);
put_rbsp_trailing_bits(c->regs);
}
static void put_slice_header(h264enc *c)
{
if (c->current_slice_type == SLICE_I)
put_start_code(c->regs, 3, 5);
else
put_start_code(c->regs, 2, 1);
put_ue(c->regs, /* first_mb_in_slice = */ 0);
put_ue(c->regs, c->current_slice_type);
put_ue(c->regs, /* pic_parameter_set_id = */ 0);
put_bits(c->regs, c->current_frame_num & 0xf, 4);
if (c->current_slice_type == SLICE_I)
put_ue(c->regs, /* idr_pic_id = */ 0);
if (c->current_slice_type == SLICE_P)
{
put_bits(c->regs, /* num_ref_idx_active_override_flag = */ 0, 1);
put_bits(c->regs, /* ref_pic_list_modification_flag_l0 = */ 0, 1);
put_bits(c->regs, /* adaptive_ref_pic_marking_mode_flag = */ 0, 1);
if (c->entropy_coding_mode_flag)
put_ue(c->regs, /* cabac_init_idc = */ 0);
}
if (c->current_slice_type == SLICE_I)
{
put_bits(c->regs, /* no_output_of_prior_pics_flag = */ 0, 1);
put_bits(c->regs, /* long_term_reference_flag = */ 0, 1);
}
put_se(c->regs, /* slice_qp_delta = */ 0);
put_ue(c->regs, /* disable_deblocking_filter_idc = */ 0);
put_se(c->regs, /* slice_alpha_c0_offset_div2 = */ 0);
put_se(c->regs, /* slice_beta_offset_div2 = */ 0);
}
void h264enc_free(h264enc *c)
{
int i;
ve_free(c->extra_buffer_line);
ve_free(c->extra_buffer_frame);
for (i = 0; i < 2; i++)
{
ve_free(c->ref_picture[i].luma_buffer);
ve_free(c->ref_picture[i].extra_buffer);
}
ve_free(c->bytestream_buffer);
ve_free(c->luma_buffer);
free(c);
}
h264enc *h264enc_new(const struct h264enc_params *p)
{
h264enc *c;
int i;
/* check parameter validity */
if (!IS_ALIGNED(p->src_width, 16) || !IS_ALIGNED(p->src_height, 16) ||
!IS_ALIGNED(p->width, 2) || !IS_ALIGNED(p->height, 2) ||
p->width > p->src_width || p->height > p->src_height)
{
MSG("invalid picture size");
return NULL;
}
if (p->qp == 0 || p->qp > 47)
{
MSG("invalid QP");
return NULL;
}
if (p->src_format != H264_FMT_NV12 && p->src_format != H264_FMT_NV16)
{
MSG("invalid color format");
return NULL;
}
/* allocate memory for h264enc structure */
c = calloc(1, sizeof(*c));
if (c == NULL)
{
MSG("can't allocate h264enc data");
return NULL;
}
/* copy parameters */
c->mb_width = DIV_ROUND_UP(p->width, 16);
c->mb_height = DIV_ROUND_UP(p->height, 16);
c->mb_stride = p->src_width / 16;
c->crop_right = (c->mb_width * 16 - p->width) / 2;
c->crop_bottom = (c->mb_height * 16 - p->height) / 2;
c->profile_idc = p->profile_idc;
c->level_idc = p->level_idc;
c->entropy_coding_mode_flag = p->entropy_coding_mode ? 1 : 0;
c->pic_init_qp = p->qp;
c->keyframe_interval = p->keyframe_interval;
c->write_sps_pps = 1;
c->current_frame_num = 0;
c->streaming_mode = (p->work_mode == ENC_MODE_STREAMING);
/* allocate input buffer */
c->input_color_format = p->src_format;
switch (c->input_color_format)
{
case H264_FMT_NV12:
c->input_buffer_size = p->src_width * (p->src_height + p->src_height / 2);
break;
case H264_FMT_NV16:
c->input_buffer_size = p->src_width * p->src_height * 2;
break;
}
c->luma_buffer = ve_malloc(c->input_buffer_size);
if (c->luma_buffer == NULL)
goto nomem;
c->chroma_buffer = c->luma_buffer + p->src_width * p->src_height;
/* allocate bytestream output buffer */
c->bytestream_buffer_size = 1 * 1024 * 1024;
c->bytestream_buffer = ve_malloc(c->bytestream_buffer_size);
if (c->bytestream_buffer == NULL)
goto nomem;
/* allocate reference picture memory */
unsigned int luma_size = ALIGN(c->mb_width * 16, 32) * ALIGN(c->mb_height * 16, 32);
unsigned int chroma_size = ALIGN(c->mb_width * 16, 32) * ALIGN(c->mb_height * 8, 32);
for (i = 0; i < 2; i++)
{
c->ref_picture[i].luma_buffer = ve_malloc(luma_size + chroma_size);
c->ref_picture[i].chroma_buffer = c->ref_picture[i].luma_buffer + luma_size;
c->ref_picture[i].extra_buffer = ve_malloc(luma_size / 4);
if (c->ref_picture[i].luma_buffer == NULL || c->ref_picture[i].extra_buffer == NULL)
goto nomem;
}
/* allocate unknown purpose buffers */
c->extra_buffer_frame = ve_malloc(ALIGN(c->mb_width, 4) * c->mb_height * 8);
c->extra_buffer_line = ve_malloc(c->mb_width * 32);
if (c->extra_buffer_frame == NULL || c->extra_buffer_line == NULL)
goto nomem;
return c;
nomem:
MSG("can't allocate VE memory");
h264enc_free(c);
return NULL;
}
void *h264enc_get_input_buffer(const h264enc *c)
{
return c->luma_buffer;
}
void *h264enc_get_bytestream_buffer(const h264enc *c)
{
return c->bytestream_buffer;
}
unsigned int h264enc_get_bytestream_length(const h264enc *c)
{
return c->bytestream_length;
}
int h264enc_encode_picture(h264enc *c)
{
c->current_slice_type = c->current_frame_num ? SLICE_P : SLICE_I;
c->regs = ve_get(VE_ENGINE_AVC, 0);
/* flush buffers (output because otherwise we might read old data later) */
ve_flush_cache(c->bytestream_buffer, c->bytestream_buffer_size);
ve_flush_cache(c->luma_buffer, c->input_buffer_size);
/* set output buffer */
writel(0x0, c->regs + VE_AVC_VLE_OFFSET);
writel(ve_virt2phys(c->bytestream_buffer), c->regs + VE_AVC_VLE_ADDR);
writel(ve_virt2phys(c->bytestream_buffer) + c->bytestream_buffer_size - 1, c->regs + VE_AVC_VLE_END);
writel(c->bytestream_buffer_size * 8, c->regs + VE_AVC_VLE_MAX);
/* write headers */
if (c->write_sps_pps)
{
put_seq_parameter_set(c);
put_pic_parameter_set(c);
c->write_sps_pps = 0;
}
put_slice_header(c);
/* set input size */
writel(c->mb_stride << 16, c->regs + VE_ISP_INPUT_STRIDE);
writel((c->mb_width << 16) | (c->mb_height << 0), c->regs + VE_ISP_INPUT_SIZE);
/* set input format */
writel(c->input_color_format << 29, c->regs + VE_ISP_CTRL);
/* set input buffer */
writel(ve_virt2phys(c->luma_buffer), c->regs + VE_ISP_INPUT_LUMA);
writel(ve_virt2phys(c->chroma_buffer), c->regs + VE_ISP_INPUT_CHROMA);
/* set reconstruction buffers */
struct h264enc_ref_pic *ref_pic = &c->ref_picture[c->current_frame_num % 2];
writel(ve_virt2phys(ref_pic->luma_buffer), c->regs + VE_AVC_REC_LUMA);
writel(ve_virt2phys(ref_pic->chroma_buffer), c->regs + VE_AVC_REC_CHROMA);
writel(ve_virt2phys(ref_pic->extra_buffer), c->regs + VE_AVC_REC_SLUMA);
/* set reference buffers */
if (c->current_slice_type != SLICE_I)
{
ref_pic = &c->ref_picture[(c->current_frame_num + 1) % 2];
writel(ve_virt2phys(ref_pic->luma_buffer), c->regs + VE_AVC_REF_LUMA);
writel(ve_virt2phys(ref_pic->chroma_buffer), c->regs + VE_AVC_REF_CHROMA);
writel(ve_virt2phys(ref_pic->extra_buffer), c->regs + VE_AVC_REF_SLUMA);
}
/* set unknown purpose buffers */
writel(ve_virt2phys(c->extra_buffer_line), c->regs + VE_AVC_MB_INFO);
writel(ve_virt2phys(c->extra_buffer_frame), c->regs + VE_AVC_UNK_BUF);
/* enable interrupt and clear status flags */
writel(readl(c->regs + VE_AVC_CTRL) | 0xf, c->regs + VE_AVC_CTRL);
writel(readl(c->regs + VE_AVC_STATUS) | 0x7, c->regs + VE_AVC_STATUS);
/* set encoding parameters */
uint32_t params = 0x0;
if (c->entropy_coding_mode_flag)
params |= 0x100;
if (c->current_slice_type == SLICE_P)
params |= 0x10;
writel(params, c->regs + VE_AVC_PARAM);
writel((4 << 16) | (c->pic_init_qp << 8) | c->pic_init_qp, c->regs + VE_AVC_QP);
writel(0x00000104, c->regs + VE_AVC_MOTION_EST);
/* trigger encoding */
writel(0x8, c->regs + VE_AVC_TRIGGER);
ve_wait(1);
/* check result */
uint32_t status = readl(c->regs + VE_AVC_STATUS);
writel(status, c->regs + VE_AVC_STATUS);
/* save bytestream length */
c->bytestream_length = readl(c->regs + VE_AVC_VLE_LENGTH) / 8;
/* next frame */
c->current_frame_num++;
if (c->current_frame_num >= c->keyframe_interval) {
c->current_frame_num = 0;
// insert each I frmae SPS/PPS if streaming
if (c->streaming_mode)
c->write_sps_pps = 1;
}
ve_put();
//printf("VE status: %08X\n", status);
return (status & 0x3) == 0x1;
}