Skip to content

Commit 0d6e145

Browse files
Pengfei Quxhaihao
authored andcommitted
ENC: add AUD NAL unit at the beginning of pic
v0:fix the buffer release. Fixes #32 Signed-off-by: Pengfei Qu <[email protected]> (cherry picked from commit 30143fa)
1 parent 5f5137b commit 0d6e145

File tree

1 file changed

+74
-1
lines changed

1 file changed

+74
-1
lines changed

encode/avcenc.c

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@
5757
#define NAL_IDR 5
5858
#define NAL_SPS 7
5959
#define NAL_PPS 8
60-
#define NAL_SEI 6
60+
#define NAL_SEI 6
61+
#define NAL_DELIMITER 9
62+
6163

6264
#define SLICE_TYPE_P 0
6365
#define SLICE_TYPE_B 1
@@ -92,6 +94,7 @@ static int frame_bit_rate = -1;
9294
static int frame_rate = 30;
9395
static int ip_period = 1;
9496
static int roi_test_enable = 0;
97+
static int aud_nal_enable = 1;
9598

9699
static VAEntrypoint select_entrypoint = VAEntrypointEncSlice;
97100

@@ -117,6 +120,9 @@ build_packed_pic_buffer(unsigned char **header_buffer);
117120
static int
118121
build_packed_seq_buffer(unsigned char **header_buffer);
119122

123+
static int
124+
build_nal_delimiter(unsigned char **header_buffer);
125+
120126
static int
121127
build_packed_sei_pic_timing(unsigned int cpb_removal_length,
122128
unsigned int dpb_output_length,
@@ -157,6 +163,8 @@ static struct {
157163
VABufferID packed_sei_buf_id;
158164
VABufferID misc_parameter_hrd_buf_id;
159165
VABufferID misc_parameter_roi_buf_id;
166+
VABufferID packed_aud_header_param_buf_id;
167+
VABufferID packed_aud_buf_id;
160168

161169
int num_slices;
162170
int codedbuf_i_size;
@@ -754,6 +762,32 @@ static int begin_picture(FILE *yuv_fp, int frame_num, int display_num, int slice
754762
else
755763
avcenc_context.current_input_surface = SID_INPUT_PICTURE_0;
756764

765+
if(aud_nal_enable) {
766+
VAEncPackedHeaderParameterBuffer packed_header_param_buffer;
767+
unsigned int length_in_bits;
768+
unsigned char *packed_buffer = NULL;
769+
770+
length_in_bits = build_nal_delimiter(&packed_buffer);
771+
packed_header_param_buffer.type = VAEncPackedHeaderRawData;
772+
packed_header_param_buffer.bit_length = length_in_bits;
773+
packed_header_param_buffer.has_emulation_bytes = 1;
774+
va_status = vaCreateBuffer(va_dpy,
775+
avcenc_context.context_id,
776+
VAEncPackedHeaderParameterBufferType,
777+
sizeof(packed_header_param_buffer), 1, &packed_header_param_buffer,
778+
&avcenc_context.packed_aud_header_param_buf_id);
779+
CHECK_VASTATUS(va_status,"vaCreateBuffer");
780+
781+
va_status = vaCreateBuffer(va_dpy,
782+
avcenc_context.context_id,
783+
VAEncPackedHeaderDataBufferType,
784+
(length_in_bits + 7) / 8, 1, packed_buffer,
785+
&avcenc_context.packed_aud_buf_id);
786+
CHECK_VASTATUS(va_status,"vaCreateBuffer");
787+
788+
free(packed_buffer);
789+
}
790+
757791
if (is_idr) {
758792
VAEncPackedHeaderParameterBuffer packed_header_param_buffer;
759793
unsigned int length_in_bits;
@@ -901,6 +935,12 @@ int avcenc_render_picture()
901935
unsigned int num_va_buffers = 0;
902936
int i;
903937

938+
if (avcenc_context.packed_aud_header_param_buf_id != VA_INVALID_ID)
939+
va_buffers[num_va_buffers++] = avcenc_context.packed_aud_header_param_buf_id;
940+
941+
if (avcenc_context.packed_aud_buf_id != VA_INVALID_ID)
942+
va_buffers[num_va_buffers++] = avcenc_context.packed_aud_buf_id;
943+
904944
va_buffers[num_va_buffers++] = avcenc_context.seq_param_buf_id;
905945
va_buffers[num_va_buffers++] = avcenc_context.pic_param_buf_id;
906946

@@ -928,6 +968,7 @@ int avcenc_render_picture()
928968
if (avcenc_context.misc_parameter_roi_buf_id != VA_INVALID_ID)
929969
va_buffers[num_va_buffers++] = avcenc_context.misc_parameter_roi_buf_id;
930970

971+
931972
va_status = vaBeginPicture(va_dpy,
932973
avcenc_context.context_id,
933974
surface_ids[avcenc_context.current_input_surface]);
@@ -985,6 +1026,8 @@ static void end_picture()
9851026
avcenc_destroy_buffers(&avcenc_context.codedbuf_buf_id, 1);
9861027
avcenc_destroy_buffers(&avcenc_context.misc_parameter_hrd_buf_id, 1);
9871028
avcenc_destroy_buffers(&avcenc_context.misc_parameter_roi_buf_id, 1);
1029+
avcenc_destroy_buffers(&avcenc_context.packed_aud_header_param_buf_id, 1);
1030+
avcenc_destroy_buffers(&avcenc_context.packed_aud_buf_id, 1);
9881031

9891032
memset(avcenc_context.slice_param, 0, sizeof(avcenc_context.slice_param));
9901033
avcenc_context.num_slices = 0;
@@ -1320,6 +1363,34 @@ build_header(FILE *avc_fp)
13201363
}
13211364
#endif
13221365

1366+
static void nal_delimiter(bitstream *bs,int slice_type)
1367+
{
1368+
if(slice_type == SLICE_TYPE_I || slice_type == FRAME_IDR)
1369+
bitstream_put_ui(bs, 0, 3);
1370+
else if(slice_type == SLICE_TYPE_P)
1371+
bitstream_put_ui(bs, 1, 3);
1372+
else if(slice_type == SLICE_TYPE_B)
1373+
bitstream_put_ui(bs, 2, 3);
1374+
else
1375+
assert(0);
1376+
bitstream_put_ui(bs, 1, 1);
1377+
bitstream_put_ui(bs, 0, 4);
1378+
}
1379+
1380+
static int build_nal_delimiter(unsigned char **header_buffer)
1381+
{
1382+
bitstream bs;
1383+
1384+
bitstream_start(&bs);
1385+
nal_start_code_prefix(&bs);
1386+
nal_header(&bs, NAL_REF_IDC_NONE, NAL_DELIMITER);
1387+
nal_delimiter(&bs,current_frame_type);
1388+
bitstream_end(&bs);
1389+
*header_buffer = (unsigned char *)bs.buffer;
1390+
return bs.bit_offset;
1391+
}
1392+
1393+
13231394
static int
13241395
build_packed_pic_buffer(unsigned char **header_buffer)
13251396
{
@@ -1915,6 +1986,8 @@ static void avcenc_context_init(int width, int height)
19151986
avcenc_context.packed_sei_header_param_buf_id = VA_INVALID_ID;
19161987
avcenc_context.packed_sei_buf_id = VA_INVALID_ID;
19171988
avcenc_context.misc_parameter_roi_buf_id = VA_INVALID_ID;
1989+
avcenc_context.packed_aud_header_param_buf_id = VA_INVALID_ID;
1990+
avcenc_context.packed_aud_buf_id = VA_INVALID_ID;
19181991

19191992
if (qp_value == -1)
19201993
avcenc_context.rate_control_method = VA_RC_CBR;

0 commit comments

Comments
 (0)