]> git.sesse.net Git - nageru/blob - h264encode.cpp
Refcount the input frames directly instead of trying to free them after-the-fact...
[nageru] / h264encode.cpp
1 //#include "sysdeps.h"
2 #include "h264encode.h"
3
4 #include <EGL/eglplatform.h>
5 #include <X11/X.h>
6 #include <X11/Xlib.h>
7 #include <assert.h>
8 #include <epoxy/egl.h>
9 #include <libavcodec/avcodec.h>
10 #include <libavformat/avio.h>
11 #include <libavutil/mathematics.h>
12 #include <libavutil/rational.h>
13 #include <libdrm/drm_fourcc.h>
14 #include <stdint.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <va/va.h>
19 #include <va/va_drmcommon.h>
20 #include <va/va_enc_h264.h>
21 #include <va/va_x11.h>
22 #include <condition_variable>
23 #include <mutex>
24 #include <queue>
25 #include <string>
26 #include <thread>
27
28 #include "context.h"
29
30 class QOpenGLContext;
31 class QSurface;
32
33 #define CHECK_VASTATUS(va_status, func)                                 \
34     if (va_status != VA_STATUS_SUCCESS) {                               \
35         fprintf(stderr, "%s:%d (%s) failed with %d\n", __func__, __LINE__, func, va_status); \
36         exit(1);                                                        \
37     }
38
39 //#include "loadsurface.h"
40
41 #define NAL_REF_IDC_NONE        0
42 #define NAL_REF_IDC_LOW         1
43 #define NAL_REF_IDC_MEDIUM      2
44 #define NAL_REF_IDC_HIGH        3
45
46 #define NAL_NON_IDR             1
47 #define NAL_IDR                 5
48 #define NAL_SPS                 7
49 #define NAL_PPS                 8
50 #define NAL_SEI                 6
51
52 #define SLICE_TYPE_P            0
53 #define SLICE_TYPE_B            1
54 #define SLICE_TYPE_I            2
55 #define IS_P_SLICE(type) (SLICE_TYPE_P == (type))
56 #define IS_B_SLICE(type) (SLICE_TYPE_B == (type))
57 #define IS_I_SLICE(type) (SLICE_TYPE_I == (type))
58
59
60 #define ENTROPY_MODE_CAVLC      0
61 #define ENTROPY_MODE_CABAC      1
62
63 #define PROFILE_IDC_BASELINE    66
64 #define PROFILE_IDC_MAIN        77
65 #define PROFILE_IDC_HIGH        100
66    
67 #define BITSTREAM_ALLOCATE_STEPPING     4096
68
69 #define SURFACE_NUM 16 /* 16 surfaces for source YUV */
70 static  VADisplay va_dpy;
71 static  VAProfile h264_profile = (VAProfile)~0;
72 static  VAConfigAttrib config_attrib[VAConfigAttribTypeMax];
73 static  int config_attrib_num = 0, enc_packed_header_idx;
74
75 struct GLSurface {
76         VASurfaceID src_surface, ref_surface;
77         VABufferID coded_buf;
78
79         VAImage surface_image;
80         GLuint y_tex, cbcr_tex;
81         EGLImage y_egl_image, cbcr_egl_image;
82 };
83 GLSurface gl_surfaces[SURFACE_NUM];
84
85 static  VAConfigID config_id;
86 static  VAContextID context_id;
87 static  VAEncSequenceParameterBufferH264 seq_param;
88 static  VAEncPictureParameterBufferH264 pic_param;
89 static  VAEncSliceParameterBufferH264 slice_param;
90 static  VAPictureH264 CurrentCurrPic;
91 static  VAPictureH264 ReferenceFrames[16], RefPicList0_P[32], RefPicList0_B[32], RefPicList1_B[32];
92
93 static  unsigned int MaxFrameNum = (2<<16);
94 static  unsigned int MaxPicOrderCntLsb = (2<<8);
95 static  unsigned int Log2MaxFrameNum = 16;
96 static  unsigned int Log2MaxPicOrderCntLsb = 8;
97
98 static  unsigned int num_ref_frames = 2;
99 static  unsigned int numShortTerm = 0;
100 static  int constraint_set_flag = 0;
101 static  int h264_packedheader = 0; /* support pack header? */
102 static  int h264_maxref = (1<<16|1);
103 static  int h264_entropy_mode = 1; /* cabac */
104
105 static  char *coded_fn = NULL;
106
107 static  int frame_width = 176;
108 static  int frame_height = 144;
109 static  int frame_width_mbaligned;
110 static  int frame_height_mbaligned;
111 static  int frame_rate = 60;
112 static  unsigned int frame_bitrate = 0;
113 static  unsigned int frame_slices = 1;
114 static  double frame_size = 0;
115 static  int initial_qp = 15;
116 //static  int initial_qp = 28;
117 static  int minimal_qp = 0;
118 static  int intra_period = 30;
119 static  int intra_idr_period = 60;
120 static  int ip_period = 1;
121 static  int rc_mode = -1;
122 static  int rc_default_modes[] = {
123     VA_RC_VBR,
124     VA_RC_CQP,
125     VA_RC_VBR_CONSTRAINED,
126     VA_RC_CBR,
127     VA_RC_VCM,
128     VA_RC_NONE,
129 };
130 static  unsigned long long current_frame_encoding = 0;
131 static  unsigned long long current_frame_display = 0;
132 static  unsigned long long current_IDR_display = 0;
133 static  unsigned int current_frame_num = 0;
134 static  int current_frame_type;
135
136 static  int misc_priv_type = 0;
137 static  int misc_priv_value = 0;
138
139 /* thread to save coded data */
140 #define SRC_SURFACE_FREE        0
141 #define SRC_SURFACE_IN_ENCODING 1
142     
143 struct __bitstream {
144     unsigned int *buffer;
145     int bit_offset;
146     int max_size_in_dword;
147 };
148 typedef struct __bitstream bitstream;
149
150 using namespace std;
151
152 static unsigned int 
153 va_swap32(unsigned int val)
154 {
155     unsigned char *pval = (unsigned char *)&val;
156
157     return ((pval[0] << 24)     |
158             (pval[1] << 16)     |
159             (pval[2] << 8)      |
160             (pval[3] << 0));
161 }
162
163 static void
164 bitstream_start(bitstream *bs)
165 {
166     bs->max_size_in_dword = BITSTREAM_ALLOCATE_STEPPING;
167     bs->buffer = (unsigned int *)calloc(bs->max_size_in_dword * sizeof(int), 1);
168     bs->bit_offset = 0;
169 }
170
171 static void
172 bitstream_end(bitstream *bs)
173 {
174     int pos = (bs->bit_offset >> 5);
175     int bit_offset = (bs->bit_offset & 0x1f);
176     int bit_left = 32 - bit_offset;
177
178     if (bit_offset) {
179         bs->buffer[pos] = va_swap32((bs->buffer[pos] << bit_left));
180     }
181 }
182  
183 static void
184 bitstream_put_ui(bitstream *bs, unsigned int val, int size_in_bits)
185 {
186     int pos = (bs->bit_offset >> 5);
187     int bit_offset = (bs->bit_offset & 0x1f);
188     int bit_left = 32 - bit_offset;
189
190     if (!size_in_bits)
191         return;
192
193     bs->bit_offset += size_in_bits;
194
195     if (bit_left > size_in_bits) {
196         bs->buffer[pos] = (bs->buffer[pos] << size_in_bits | val);
197     } else {
198         size_in_bits -= bit_left;
199         bs->buffer[pos] = (bs->buffer[pos] << bit_left) | (val >> size_in_bits);
200         bs->buffer[pos] = va_swap32(bs->buffer[pos]);
201
202         if (pos + 1 == bs->max_size_in_dword) {
203             bs->max_size_in_dword += BITSTREAM_ALLOCATE_STEPPING;
204             bs->buffer = (unsigned int *)realloc(bs->buffer, bs->max_size_in_dword * sizeof(unsigned int));
205         }
206
207         bs->buffer[pos + 1] = val;
208     }
209 }
210
211 static void
212 bitstream_put_ue(bitstream *bs, unsigned int val)
213 {
214     int size_in_bits = 0;
215     int tmp_val = ++val;
216
217     while (tmp_val) {
218         tmp_val >>= 1;
219         size_in_bits++;
220     }
221
222     bitstream_put_ui(bs, 0, size_in_bits - 1); // leading zero
223     bitstream_put_ui(bs, val, size_in_bits);
224 }
225
226 static void
227 bitstream_put_se(bitstream *bs, int val)
228 {
229     unsigned int new_val;
230
231     if (val <= 0)
232         new_val = -2 * val;
233     else
234         new_val = 2 * val - 1;
235
236     bitstream_put_ue(bs, new_val);
237 }
238
239 static void
240 bitstream_byte_aligning(bitstream *bs, int bit)
241 {
242     int bit_offset = (bs->bit_offset & 0x7);
243     int bit_left = 8 - bit_offset;
244     int new_val;
245
246     if (!bit_offset)
247         return;
248
249     assert(bit == 0 || bit == 1);
250
251     if (bit)
252         new_val = (1 << bit_left) - 1;
253     else
254         new_val = 0;
255
256     bitstream_put_ui(bs, new_val, bit_left);
257 }
258
259 static void 
260 rbsp_trailing_bits(bitstream *bs)
261 {
262     bitstream_put_ui(bs, 1, 1);
263     bitstream_byte_aligning(bs, 0);
264 }
265
266 static void nal_start_code_prefix(bitstream *bs)
267 {
268     bitstream_put_ui(bs, 0x00000001, 32);
269 }
270
271 static void nal_header(bitstream *bs, int nal_ref_idc, int nal_unit_type)
272 {
273     bitstream_put_ui(bs, 0, 1);                /* forbidden_zero_bit: 0 */
274     bitstream_put_ui(bs, nal_ref_idc, 2);
275     bitstream_put_ui(bs, nal_unit_type, 5);
276 }
277
278 static void sps_rbsp(bitstream *bs)
279 {
280     int profile_idc = PROFILE_IDC_BASELINE;
281
282     if (h264_profile  == VAProfileH264High)
283         profile_idc = PROFILE_IDC_HIGH;
284     else if (h264_profile  == VAProfileH264Main)
285         profile_idc = PROFILE_IDC_MAIN;
286
287     bitstream_put_ui(bs, profile_idc, 8);               /* profile_idc */
288     bitstream_put_ui(bs, !!(constraint_set_flag & 1), 1);                         /* constraint_set0_flag */
289     bitstream_put_ui(bs, !!(constraint_set_flag & 2), 1);                         /* constraint_set1_flag */
290     bitstream_put_ui(bs, !!(constraint_set_flag & 4), 1);                         /* constraint_set2_flag */
291     bitstream_put_ui(bs, !!(constraint_set_flag & 8), 1);                         /* constraint_set3_flag */
292     bitstream_put_ui(bs, 0, 4);                         /* reserved_zero_4bits */
293     bitstream_put_ui(bs, seq_param.level_idc, 8);      /* level_idc */
294     bitstream_put_ue(bs, seq_param.seq_parameter_set_id);      /* seq_parameter_set_id */
295
296     if ( profile_idc == PROFILE_IDC_HIGH) {
297         bitstream_put_ue(bs, 1);        /* chroma_format_idc = 1, 4:2:0 */ 
298         bitstream_put_ue(bs, 0);        /* bit_depth_luma_minus8 */
299         bitstream_put_ue(bs, 0);        /* bit_depth_chroma_minus8 */
300         bitstream_put_ui(bs, 0, 1);     /* qpprime_y_zero_transform_bypass_flag */
301         bitstream_put_ui(bs, 0, 1);     /* seq_scaling_matrix_present_flag */
302     }
303
304     bitstream_put_ue(bs, seq_param.seq_fields.bits.log2_max_frame_num_minus4); /* log2_max_frame_num_minus4 */
305     bitstream_put_ue(bs, seq_param.seq_fields.bits.pic_order_cnt_type);        /* pic_order_cnt_type */
306
307     if (seq_param.seq_fields.bits.pic_order_cnt_type == 0)
308         bitstream_put_ue(bs, seq_param.seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4);     /* log2_max_pic_order_cnt_lsb_minus4 */
309     else {
310         assert(0);
311     }
312
313     bitstream_put_ue(bs, seq_param.max_num_ref_frames);        /* num_ref_frames */
314     bitstream_put_ui(bs, 0, 1);                                 /* gaps_in_frame_num_value_allowed_flag */
315
316     bitstream_put_ue(bs, seq_param.picture_width_in_mbs - 1);  /* pic_width_in_mbs_minus1 */
317     bitstream_put_ue(bs, seq_param.picture_height_in_mbs - 1); /* pic_height_in_map_units_minus1 */
318     bitstream_put_ui(bs, seq_param.seq_fields.bits.frame_mbs_only_flag, 1);    /* frame_mbs_only_flag */
319
320     if (!seq_param.seq_fields.bits.frame_mbs_only_flag) {
321         assert(0);
322     }
323
324     bitstream_put_ui(bs, seq_param.seq_fields.bits.direct_8x8_inference_flag, 1);      /* direct_8x8_inference_flag */
325     bitstream_put_ui(bs, seq_param.frame_cropping_flag, 1);            /* frame_cropping_flag */
326
327     if (seq_param.frame_cropping_flag) {
328         bitstream_put_ue(bs, seq_param.frame_crop_left_offset);        /* frame_crop_left_offset */
329         bitstream_put_ue(bs, seq_param.frame_crop_right_offset);       /* frame_crop_right_offset */
330         bitstream_put_ue(bs, seq_param.frame_crop_top_offset);         /* frame_crop_top_offset */
331         bitstream_put_ue(bs, seq_param.frame_crop_bottom_offset);      /* frame_crop_bottom_offset */
332     }
333     
334     //if ( frame_bit_rate < 0 ) { //TODO EW: the vui header isn't correct
335     if ( false ) {
336         bitstream_put_ui(bs, 0, 1); /* vui_parameters_present_flag */
337     } else {
338         bitstream_put_ui(bs, 1, 1); /* vui_parameters_present_flag */
339         bitstream_put_ui(bs, 0, 1); /* aspect_ratio_info_present_flag */
340         bitstream_put_ui(bs, 0, 1); /* overscan_info_present_flag */
341         bitstream_put_ui(bs, 0, 1); /* video_signal_type_present_flag */
342         bitstream_put_ui(bs, 0, 1); /* chroma_loc_info_present_flag */
343         bitstream_put_ui(bs, 1, 1); /* timing_info_present_flag */
344         {
345             bitstream_put_ui(bs, 1, 32);  // FPS
346             bitstream_put_ui(bs, frame_rate * 2, 32);  // FPS
347             bitstream_put_ui(bs, 1, 1);
348         }
349         bitstream_put_ui(bs, 1, 1); /* nal_hrd_parameters_present_flag */
350         {
351             // hrd_parameters 
352             bitstream_put_ue(bs, 0);    /* cpb_cnt_minus1 */
353             bitstream_put_ui(bs, 4, 4); /* bit_rate_scale */
354             bitstream_put_ui(bs, 6, 4); /* cpb_size_scale */
355            
356             bitstream_put_ue(bs, frame_bitrate - 1); /* bit_rate_value_minus1[0] */
357             bitstream_put_ue(bs, frame_bitrate*8 - 1); /* cpb_size_value_minus1[0] */
358             bitstream_put_ui(bs, 1, 1);  /* cbr_flag[0] */
359
360             bitstream_put_ui(bs, 23, 5);   /* initial_cpb_removal_delay_length_minus1 */
361             bitstream_put_ui(bs, 23, 5);   /* cpb_removal_delay_length_minus1 */
362             bitstream_put_ui(bs, 23, 5);   /* dpb_output_delay_length_minus1 */
363             bitstream_put_ui(bs, 23, 5);   /* time_offset_length  */
364         }
365         bitstream_put_ui(bs, 0, 1);   /* vcl_hrd_parameters_present_flag */
366         bitstream_put_ui(bs, 0, 1);   /* low_delay_hrd_flag */ 
367
368         bitstream_put_ui(bs, 0, 1); /* pic_struct_present_flag */
369         bitstream_put_ui(bs, 0, 1); /* bitstream_restriction_flag */
370     }
371
372     rbsp_trailing_bits(bs);     /* rbsp_trailing_bits */
373 }
374
375
376 static void pps_rbsp(bitstream *bs)
377 {
378     bitstream_put_ue(bs, pic_param.pic_parameter_set_id);      /* pic_parameter_set_id */
379     bitstream_put_ue(bs, pic_param.seq_parameter_set_id);      /* seq_parameter_set_id */
380
381     bitstream_put_ui(bs, pic_param.pic_fields.bits.entropy_coding_mode_flag, 1);  /* entropy_coding_mode_flag */
382
383     bitstream_put_ui(bs, 0, 1);                         /* pic_order_present_flag: 0 */
384
385     bitstream_put_ue(bs, 0);                            /* num_slice_groups_minus1 */
386
387     bitstream_put_ue(bs, pic_param.num_ref_idx_l0_active_minus1);      /* num_ref_idx_l0_active_minus1 */
388     bitstream_put_ue(bs, pic_param.num_ref_idx_l1_active_minus1);      /* num_ref_idx_l1_active_minus1 1 */
389
390     bitstream_put_ui(bs, pic_param.pic_fields.bits.weighted_pred_flag, 1);     /* weighted_pred_flag: 0 */
391     bitstream_put_ui(bs, pic_param.pic_fields.bits.weighted_bipred_idc, 2);     /* weighted_bipred_idc: 0 */
392
393     bitstream_put_se(bs, pic_param.pic_init_qp - 26);  /* pic_init_qp_minus26 */
394     bitstream_put_se(bs, 0);                            /* pic_init_qs_minus26 */
395     bitstream_put_se(bs, 0);                            /* chroma_qp_index_offset */
396
397     bitstream_put_ui(bs, pic_param.pic_fields.bits.deblocking_filter_control_present_flag, 1); /* deblocking_filter_control_present_flag */
398     bitstream_put_ui(bs, 0, 1);                         /* constrained_intra_pred_flag */
399     bitstream_put_ui(bs, 0, 1);                         /* redundant_pic_cnt_present_flag */
400     
401     /* more_rbsp_data */
402     bitstream_put_ui(bs, pic_param.pic_fields.bits.transform_8x8_mode_flag, 1);    /*transform_8x8_mode_flag */
403     bitstream_put_ui(bs, 0, 1);                         /* pic_scaling_matrix_present_flag */
404     bitstream_put_se(bs, pic_param.second_chroma_qp_index_offset );    /*second_chroma_qp_index_offset */
405
406     rbsp_trailing_bits(bs);
407 }
408
409 static void slice_header(bitstream *bs)
410 {
411     int first_mb_in_slice = slice_param.macroblock_address;
412
413     bitstream_put_ue(bs, first_mb_in_slice);        /* first_mb_in_slice: 0 */
414     bitstream_put_ue(bs, slice_param.slice_type);   /* slice_type */
415     bitstream_put_ue(bs, slice_param.pic_parameter_set_id);        /* pic_parameter_set_id: 0 */
416     bitstream_put_ui(bs, pic_param.frame_num, seq_param.seq_fields.bits.log2_max_frame_num_minus4 + 4); /* frame_num */
417
418     /* frame_mbs_only_flag == 1 */
419     if (!seq_param.seq_fields.bits.frame_mbs_only_flag) {
420         /* FIXME: */
421         assert(0);
422     }
423
424     if (pic_param.pic_fields.bits.idr_pic_flag)
425         bitstream_put_ue(bs, slice_param.idr_pic_id);           /* idr_pic_id: 0 */
426
427     if (seq_param.seq_fields.bits.pic_order_cnt_type == 0) {
428         bitstream_put_ui(bs, pic_param.CurrPic.TopFieldOrderCnt, seq_param.seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4 + 4);
429         /* pic_order_present_flag == 0 */
430     } else {
431         /* FIXME: */
432         assert(0);
433     }
434
435     /* redundant_pic_cnt_present_flag == 0 */
436     /* slice type */
437     if (IS_P_SLICE(slice_param.slice_type)) {
438         bitstream_put_ui(bs, slice_param.num_ref_idx_active_override_flag, 1);            /* num_ref_idx_active_override_flag: */
439
440         if (slice_param.num_ref_idx_active_override_flag)
441             bitstream_put_ue(bs, slice_param.num_ref_idx_l0_active_minus1);
442
443         /* ref_pic_list_reordering */
444         bitstream_put_ui(bs, 0, 1);            /* ref_pic_list_reordering_flag_l0: 0 */
445     } else if (IS_B_SLICE(slice_param.slice_type)) {
446         bitstream_put_ui(bs, slice_param.direct_spatial_mv_pred_flag, 1);            /* direct_spatial_mv_pred: 1 */
447
448         bitstream_put_ui(bs, slice_param.num_ref_idx_active_override_flag, 1);       /* num_ref_idx_active_override_flag: */
449
450         if (slice_param.num_ref_idx_active_override_flag) {
451             bitstream_put_ue(bs, slice_param.num_ref_idx_l0_active_minus1);
452             bitstream_put_ue(bs, slice_param.num_ref_idx_l1_active_minus1);
453         }
454
455         /* ref_pic_list_reordering */
456         bitstream_put_ui(bs, 0, 1);            /* ref_pic_list_reordering_flag_l0: 0 */
457         bitstream_put_ui(bs, 0, 1);            /* ref_pic_list_reordering_flag_l1: 0 */
458     }
459
460     if ((pic_param.pic_fields.bits.weighted_pred_flag &&
461          IS_P_SLICE(slice_param.slice_type)) ||
462         ((pic_param.pic_fields.bits.weighted_bipred_idc == 1) &&
463          IS_B_SLICE(slice_param.slice_type))) {
464         /* FIXME: fill weight/offset table */
465         assert(0);
466     }
467
468     /* dec_ref_pic_marking */
469     if (pic_param.pic_fields.bits.reference_pic_flag) {     /* nal_ref_idc != 0 */
470         unsigned char no_output_of_prior_pics_flag = 0;
471         unsigned char long_term_reference_flag = 0;
472         unsigned char adaptive_ref_pic_marking_mode_flag = 0;
473
474         if (pic_param.pic_fields.bits.idr_pic_flag) {
475             bitstream_put_ui(bs, no_output_of_prior_pics_flag, 1);            /* no_output_of_prior_pics_flag: 0 */
476             bitstream_put_ui(bs, long_term_reference_flag, 1);            /* long_term_reference_flag: 0 */
477         } else {
478             bitstream_put_ui(bs, adaptive_ref_pic_marking_mode_flag, 1);            /* adaptive_ref_pic_marking_mode_flag: 0 */
479         }
480     }
481
482     if (pic_param.pic_fields.bits.entropy_coding_mode_flag &&
483         !IS_I_SLICE(slice_param.slice_type))
484         bitstream_put_ue(bs, slice_param.cabac_init_idc);               /* cabac_init_idc: 0 */
485
486     bitstream_put_se(bs, slice_param.slice_qp_delta);                   /* slice_qp_delta: 0 */
487
488     /* ignore for SP/SI */
489
490     if (pic_param.pic_fields.bits.deblocking_filter_control_present_flag) {
491         bitstream_put_ue(bs, slice_param.disable_deblocking_filter_idc);           /* disable_deblocking_filter_idc: 0 */
492
493         if (slice_param.disable_deblocking_filter_idc != 1) {
494             bitstream_put_se(bs, slice_param.slice_alpha_c0_offset_div2);          /* slice_alpha_c0_offset_div2: 2 */
495             bitstream_put_se(bs, slice_param.slice_beta_offset_div2);              /* slice_beta_offset_div2: 2 */
496         }
497     }
498
499     if (pic_param.pic_fields.bits.entropy_coding_mode_flag) {
500         bitstream_byte_aligning(bs, 1);
501     }
502 }
503
504 static int
505 build_packed_pic_buffer(unsigned char **header_buffer)
506 {
507     bitstream bs;
508
509     bitstream_start(&bs);
510     nal_start_code_prefix(&bs);
511     nal_header(&bs, NAL_REF_IDC_HIGH, NAL_PPS);
512     pps_rbsp(&bs);
513     bitstream_end(&bs);
514
515     *header_buffer = (unsigned char *)bs.buffer;
516     return bs.bit_offset;
517 }
518
519 static int
520 build_packed_seq_buffer(unsigned char **header_buffer)
521 {
522     bitstream bs;
523
524     bitstream_start(&bs);
525     nal_start_code_prefix(&bs);
526     nal_header(&bs, NAL_REF_IDC_HIGH, NAL_SPS);
527     sps_rbsp(&bs);
528     bitstream_end(&bs);
529
530     *header_buffer = (unsigned char *)bs.buffer;
531     return bs.bit_offset;
532 }
533
534 static int build_packed_slice_buffer(unsigned char **header_buffer)
535 {
536     bitstream bs;
537     int is_idr = !!pic_param.pic_fields.bits.idr_pic_flag;
538     int is_ref = !!pic_param.pic_fields.bits.reference_pic_flag;
539
540     bitstream_start(&bs);
541     nal_start_code_prefix(&bs);
542
543     if (IS_I_SLICE(slice_param.slice_type)) {
544         nal_header(&bs, NAL_REF_IDC_HIGH, is_idr ? NAL_IDR : NAL_NON_IDR);
545     } else if (IS_P_SLICE(slice_param.slice_type)) {
546         nal_header(&bs, NAL_REF_IDC_MEDIUM, NAL_NON_IDR);
547     } else {
548         assert(IS_B_SLICE(slice_param.slice_type));
549         nal_header(&bs, is_ref ? NAL_REF_IDC_LOW : NAL_REF_IDC_NONE, NAL_NON_IDR);
550     }
551
552     slice_header(&bs);
553     bitstream_end(&bs);
554
555     *header_buffer = (unsigned char *)bs.buffer;
556     return bs.bit_offset;
557 }
558
559
560 /*
561   Assume frame sequence is: Frame#0, #1, #2, ..., #M, ..., #X, ... (encoding order)
562   1) period between Frame #X and Frame #N = #X - #N
563   2) 0 means infinite for intra_period/intra_idr_period, and 0 is invalid for ip_period
564   3) intra_idr_period % intra_period (intra_period > 0) and intra_period % ip_period must be 0
565   4) intra_period and intra_idr_period take precedence over ip_period
566   5) if ip_period > 1, intra_period and intra_idr_period are not  the strict periods 
567      of I/IDR frames, see bellow examples
568   -------------------------------------------------------------------
569   intra_period intra_idr_period ip_period frame sequence (intra_period/intra_idr_period/ip_period)
570   0            ignored          1          IDRPPPPPPP ...     (No IDR/I any more)
571   0            ignored        >=2          IDR(PBB)(PBB)...   (No IDR/I any more)
572   1            0                ignored    IDRIIIIIII...      (No IDR any more)
573   1            1                ignored    IDR IDR IDR IDR...
574   1            >=2              ignored    IDRII IDRII IDR... (1/3/ignore)
575   >=2          0                1          IDRPPP IPPP I...   (3/0/1)
576   >=2          0              >=2          IDR(PBB)(PBB)(IBB) (6/0/3)
577                                               (PBB)(IBB)(PBB)(IBB)... 
578   >=2          >=2              1          IDRPPPPP IPPPPP IPPPPP (6/18/1)
579                                            IDRPPPPP IPPPPP IPPPPP...
580   >=2          >=2              >=2        {IDR(PBB)(PBB)(IBB)(PBB)(IBB)(PBB)} (6/18/3)
581                                            {IDR(PBB)(PBB)(IBB)(PBB)(IBB)(PBB)}...
582                                            {IDR(PBB)(PBB)(IBB)(PBB)}           (6/12/3)
583                                            {IDR(PBB)(PBB)(IBB)(PBB)}...
584                                            {IDR(PBB)(PBB)}                     (6/6/3)
585                                            {IDR(PBB)(PBB)}.
586 */
587
588 /*
589  * Return displaying order with specified periods and encoding order
590  * displaying_order: displaying order
591  * frame_type: frame type 
592  */
593 #define FRAME_P 0
594 #define FRAME_B 1
595 #define FRAME_I 2
596 #define FRAME_IDR 7
597 void encoding2display_order(
598     unsigned long long encoding_order, int intra_period,
599     int intra_idr_period, int ip_period,
600     unsigned long long *displaying_order,
601     int *frame_type)
602 {
603     int encoding_order_gop = 0;
604
605     if (intra_period == 1) { /* all are I/IDR frames */
606         *displaying_order = encoding_order;
607         if (intra_idr_period == 0)
608             *frame_type = (encoding_order == 0)?FRAME_IDR:FRAME_I;
609         else
610             *frame_type = (encoding_order % intra_idr_period == 0)?FRAME_IDR:FRAME_I;
611         return;
612     }
613
614     if (intra_period == 0)
615         intra_idr_period = 0;
616
617     /* new sequence like
618      * IDR PPPPP IPPPPP
619      * IDR (PBB)(PBB)(IBB)(PBB)
620      */
621     encoding_order_gop = (intra_idr_period == 0)? encoding_order:
622         (encoding_order % (intra_idr_period + ((ip_period == 1)?0:1)));
623          
624     if (encoding_order_gop == 0) { /* the first frame */
625         *frame_type = FRAME_IDR;
626         *displaying_order = encoding_order;
627     } else if (((encoding_order_gop - 1) % ip_period) != 0) { /* B frames */
628         *frame_type = FRAME_B;
629         *displaying_order = encoding_order - 1;
630     } else if ((intra_period != 0) && /* have I frames */
631                (encoding_order_gop >= 2) &&
632                ((ip_period == 1 && encoding_order_gop % intra_period == 0) || /* for IDR PPPPP IPPPP */
633                 /* for IDR (PBB)(PBB)(IBB) */
634                 (ip_period >= 2 && ((encoding_order_gop - 1) / ip_period % (intra_period / ip_period)) == 0))) {
635         *frame_type = FRAME_I;
636         *displaying_order = encoding_order + ip_period - 1;
637     } else {
638         *frame_type = FRAME_P;
639         *displaying_order = encoding_order + ip_period - 1;
640     }
641 }
642
643
644 static const char *rc_to_string(int rcmode)
645 {
646     switch (rc_mode) {
647     case VA_RC_NONE:
648         return "NONE";
649     case VA_RC_CBR:
650         return "CBR";
651     case VA_RC_VBR:
652         return "VBR";
653     case VA_RC_VCM:
654         return "VCM";
655     case VA_RC_CQP:
656         return "CQP";
657     case VA_RC_VBR_CONSTRAINED:
658         return "VBR_CONSTRAINED";
659     default:
660         return "Unknown";
661     }
662 }
663
664 #if 0
665 static int process_cmdline(int argc, char *argv[])
666 {
667     char c;
668     const struct option long_opts[] = {
669         {"help", no_argument, NULL, 0 },
670         {"bitrate", required_argument, NULL, 1 },
671         {"minqp", required_argument, NULL, 2 },
672         {"initialqp", required_argument, NULL, 3 },
673         {"intra_period", required_argument, NULL, 4 },
674         {"idr_period", required_argument, NULL, 5 },
675         {"ip_period", required_argument, NULL, 6 },
676         {"rcmode", required_argument, NULL, 7 },
677         {"srcyuv", required_argument, NULL, 9 },
678         {"recyuv", required_argument, NULL, 10 },
679         {"fourcc", required_argument, NULL, 11 },
680         {"syncmode", no_argument, NULL, 12 },
681         {"enablePSNR", no_argument, NULL, 13 },
682         {"prit", required_argument, NULL, 14 },
683         {"priv", required_argument, NULL, 15 },
684         {"framecount", required_argument, NULL, 16 },
685         {"entropy", required_argument, NULL, 17 },
686         {"profile", required_argument, NULL, 18 },
687         {NULL, no_argument, NULL, 0 }};
688     int long_index;
689     
690     while ((c =getopt_long_only(argc, argv, "w:h:n:f:o:?", long_opts, &long_index)) != EOF) {
691         switch (c) {
692         case 'w':
693             frame_width = atoi(optarg);
694             break;
695         case 'h':
696             frame_height = atoi(optarg);
697             break;
698         case 'n':
699         case 'f':
700             frame_rate = atoi(optarg);
701             break;
702         case 'o':
703             coded_fn = strdup(optarg);
704             break;
705         case 0:
706             print_help();
707             exit(0);
708         case 1:
709             frame_bitrate = atoi(optarg);
710             break;
711         case 2:
712             minimal_qp = atoi(optarg);
713             break;
714         case 3:
715             initial_qp = atoi(optarg);
716             break;
717         case 4:
718             intra_period = atoi(optarg);
719             break;
720         case 5:
721             intra_idr_period = atoi(optarg);
722             break;
723         case 6:
724             ip_period = atoi(optarg);
725             break;
726         case 7:
727             rc_mode = string_to_rc(optarg);
728             if (rc_mode < 0) {
729                 print_help();
730                 exit(1);
731             }
732             break;
733         case 9:
734             srcyuv_fn = strdup(optarg);
735             break;
736         case 11:
737             srcyuv_fourcc = string_to_fourcc(optarg);
738             if (srcyuv_fourcc <= 0) {
739                 print_help();
740                 exit(1);
741             }
742             break;
743         case 13:
744             calc_psnr = 1;
745             break;
746         case 14:
747             misc_priv_type = strtol(optarg, NULL, 0);
748             break;
749         case 15:
750             misc_priv_value = strtol(optarg, NULL, 0);
751             break;
752         case 17:
753             h264_entropy_mode = atoi(optarg) ? 1: 0;
754             break;
755         case 18:
756             if (strncmp(optarg, "BP", 2) == 0)
757                 h264_profile = VAProfileH264Baseline;
758             else if (strncmp(optarg, "MP", 2) == 0)
759                 h264_profile = VAProfileH264Main;
760             else if (strncmp(optarg, "HP", 2) == 0)
761                 h264_profile = VAProfileH264High;
762             else
763                 h264_profile = (VAProfile)0;
764             break;
765         case ':':
766         case '?':
767             print_help();
768             exit(0);
769         }
770     }
771
772     if (ip_period < 1) {
773         printf(" ip_period must be greater than 0\n");
774         exit(0);
775     }
776     if (intra_period != 1 && intra_period % ip_period != 0) {
777         printf(" intra_period must be a multiplier of ip_period\n");
778         exit(0);        
779     }
780     if (intra_period != 0 && intra_idr_period % intra_period != 0) {
781         printf(" intra_idr_period must be a multiplier of intra_period\n");
782         exit(0);        
783     }
784
785     if (frame_bitrate == 0)
786         frame_bitrate = frame_width * frame_height * 12 * frame_rate / 50;
787         
788     if (coded_fn == NULL) {
789         struct stat buf;
790         if (stat("/tmp", &buf) == 0)
791             coded_fn = strdup("/tmp/test.264");
792         else if (stat("/sdcard", &buf) == 0)
793             coded_fn = strdup("/sdcard/test.264");
794         else
795             coded_fn = strdup("./test.264");
796     }
797     
798
799     frame_width_mbaligned = (frame_width + 15) & (~15);
800     frame_height_mbaligned = (frame_height + 15) & (~15);
801     if (frame_width != frame_width_mbaligned ||
802         frame_height != frame_height_mbaligned) {
803         printf("Source frame is %dx%d and will code clip to %dx%d with crop\n",
804                frame_width, frame_height,
805                frame_width_mbaligned, frame_height_mbaligned
806                );
807     }
808     
809     return 0;
810 }
811 #endif
812
813 static Display *x11_display;
814 static Window   x11_window;
815
816 VADisplay
817 va_open_display(void)
818 {
819     x11_display = XOpenDisplay(NULL);
820     if (!x11_display) {
821         fprintf(stderr, "error: can't connect to X server!\n");
822         return NULL;
823     }
824     return vaGetDisplay(x11_display);
825 }
826
827 void
828 va_close_display(VADisplay va_dpy)
829 {
830     if (!x11_display)
831         return;
832
833     if (x11_window) {
834         XUnmapWindow(x11_display, x11_window);
835         XDestroyWindow(x11_display, x11_window);
836         x11_window = None;
837     }
838     XCloseDisplay(x11_display);
839     x11_display = NULL;
840 }
841
842 static int init_va(void)
843 {
844     VAProfile profile_list[]={VAProfileH264High, VAProfileH264Main, VAProfileH264Baseline, VAProfileH264ConstrainedBaseline};
845     VAEntrypoint *entrypoints;
846     int num_entrypoints, slice_entrypoint;
847     int support_encode = 0;    
848     int major_ver, minor_ver;
849     VAStatus va_status;
850     unsigned int i;
851
852     va_dpy = va_open_display();
853     va_status = vaInitialize(va_dpy, &major_ver, &minor_ver);
854     CHECK_VASTATUS(va_status, "vaInitialize");
855
856     num_entrypoints = vaMaxNumEntrypoints(va_dpy);
857     entrypoints = (VAEntrypoint *)malloc(num_entrypoints * sizeof(*entrypoints));
858     if (!entrypoints) {
859         fprintf(stderr, "error: failed to initialize VA entrypoints array\n");
860         exit(1);
861     }
862
863     /* use the highest profile */
864     for (i = 0; i < sizeof(profile_list)/sizeof(profile_list[0]); i++) {
865         if ((h264_profile != ~0) && h264_profile != profile_list[i])
866             continue;
867         
868         h264_profile = profile_list[i];
869         vaQueryConfigEntrypoints(va_dpy, h264_profile, entrypoints, &num_entrypoints);
870         for (slice_entrypoint = 0; slice_entrypoint < num_entrypoints; slice_entrypoint++) {
871             if (entrypoints[slice_entrypoint] == VAEntrypointEncSlice) {
872                 support_encode = 1;
873                 break;
874             }
875         }
876         if (support_encode == 1)
877             break;
878     }
879     
880     if (support_encode == 0) {
881         printf("Can't find VAEntrypointEncSlice for H264 profiles\n");
882         exit(1);
883     } else {
884         switch (h264_profile) {
885             case VAProfileH264Baseline:
886                 printf("Use profile VAProfileH264Baseline\n");
887                 ip_period = 1;
888                 constraint_set_flag |= (1 << 0); /* Annex A.2.1 */
889                 h264_entropy_mode = 0;
890                 break;
891             case VAProfileH264ConstrainedBaseline:
892                 printf("Use profile VAProfileH264ConstrainedBaseline\n");
893                 constraint_set_flag |= (1 << 0 | 1 << 1); /* Annex A.2.2 */
894                 ip_period = 1;
895                 break;
896
897             case VAProfileH264Main:
898                 printf("Use profile VAProfileH264Main\n");
899                 constraint_set_flag |= (1 << 1); /* Annex A.2.2 */
900                 break;
901
902             case VAProfileH264High:
903                 constraint_set_flag |= (1 << 3); /* Annex A.2.4 */
904                 printf("Use profile VAProfileH264High\n");
905                 break;
906             default:
907                 printf("unknow profile. Set to Baseline");
908                 h264_profile = VAProfileH264Baseline;
909                 ip_period = 1;
910                 constraint_set_flag |= (1 << 0); /* Annex A.2.1 */
911                 break;
912         }
913     }
914
915     VAConfigAttrib attrib[VAConfigAttribTypeMax];
916
917     /* find out the format for the render target, and rate control mode */
918     for (i = 0; i < VAConfigAttribTypeMax; i++)
919         attrib[i].type = (VAConfigAttribType)i;
920
921     va_status = vaGetConfigAttributes(va_dpy, h264_profile, VAEntrypointEncSlice,
922                                       &attrib[0], VAConfigAttribTypeMax);
923     CHECK_VASTATUS(va_status, "vaGetConfigAttributes");
924     /* check the interested configattrib */
925     if ((attrib[VAConfigAttribRTFormat].value & VA_RT_FORMAT_YUV420) == 0) {
926         printf("Not find desired YUV420 RT format\n");
927         exit(1);
928     } else {
929         config_attrib[config_attrib_num].type = VAConfigAttribRTFormat;
930         config_attrib[config_attrib_num].value = VA_RT_FORMAT_YUV420;
931         config_attrib_num++;
932     }
933     
934     if (attrib[VAConfigAttribRateControl].value != VA_ATTRIB_NOT_SUPPORTED) {
935         int tmp = attrib[VAConfigAttribRateControl].value;
936
937         printf("Support rate control mode (0x%x):", tmp);
938         
939         if (tmp & VA_RC_NONE)
940             printf("NONE ");
941         if (tmp & VA_RC_CBR)
942             printf("CBR ");
943         if (tmp & VA_RC_VBR)
944             printf("VBR ");
945         if (tmp & VA_RC_VCM)
946             printf("VCM ");
947         if (tmp & VA_RC_CQP)
948             printf("CQP ");
949         if (tmp & VA_RC_VBR_CONSTRAINED)
950             printf("VBR_CONSTRAINED ");
951
952         printf("\n");
953
954         if (rc_mode == -1 || !(rc_mode & tmp))  {
955             if (rc_mode != -1) {
956                 printf("Warning: Don't support the specified RateControl mode: %s!!!, switch to ", rc_to_string(rc_mode));
957             }
958
959             for (i = 0; i < sizeof(rc_default_modes) / sizeof(rc_default_modes[0]); i++) {
960                 if (rc_default_modes[i] & tmp) {
961                     rc_mode = rc_default_modes[i];
962                     break;
963                 }
964             }
965
966             printf("RateControl mode: %s\n", rc_to_string(rc_mode));
967         }
968
969         config_attrib[config_attrib_num].type = VAConfigAttribRateControl;
970         config_attrib[config_attrib_num].value = rc_mode;
971         config_attrib_num++;
972     }
973     
974
975     if (attrib[VAConfigAttribEncPackedHeaders].value != VA_ATTRIB_NOT_SUPPORTED) {
976         int tmp = attrib[VAConfigAttribEncPackedHeaders].value;
977
978         printf("Support VAConfigAttribEncPackedHeaders\n");
979         
980         h264_packedheader = 1;
981         config_attrib[config_attrib_num].type = VAConfigAttribEncPackedHeaders;
982         config_attrib[config_attrib_num].value = VA_ENC_PACKED_HEADER_NONE;
983         
984         if (tmp & VA_ENC_PACKED_HEADER_SEQUENCE) {
985             printf("Support packed sequence headers\n");
986             config_attrib[config_attrib_num].value |= VA_ENC_PACKED_HEADER_SEQUENCE;
987         }
988         
989         if (tmp & VA_ENC_PACKED_HEADER_PICTURE) {
990             printf("Support packed picture headers\n");
991             config_attrib[config_attrib_num].value |= VA_ENC_PACKED_HEADER_PICTURE;
992         }
993         
994         if (tmp & VA_ENC_PACKED_HEADER_SLICE) {
995             printf("Support packed slice headers\n");
996             config_attrib[config_attrib_num].value |= VA_ENC_PACKED_HEADER_SLICE;
997         }
998         
999         if (tmp & VA_ENC_PACKED_HEADER_MISC) {
1000             printf("Support packed misc headers\n");
1001             config_attrib[config_attrib_num].value |= VA_ENC_PACKED_HEADER_MISC;
1002         }
1003         
1004         enc_packed_header_idx = config_attrib_num;
1005         config_attrib_num++;
1006     }
1007
1008     if (attrib[VAConfigAttribEncInterlaced].value != VA_ATTRIB_NOT_SUPPORTED) {
1009         int tmp = attrib[VAConfigAttribEncInterlaced].value;
1010         
1011         printf("Support VAConfigAttribEncInterlaced\n");
1012
1013         if (tmp & VA_ENC_INTERLACED_FRAME)
1014             printf("support VA_ENC_INTERLACED_FRAME\n");
1015         if (tmp & VA_ENC_INTERLACED_FIELD)
1016             printf("Support VA_ENC_INTERLACED_FIELD\n");
1017         if (tmp & VA_ENC_INTERLACED_MBAFF)
1018             printf("Support VA_ENC_INTERLACED_MBAFF\n");
1019         if (tmp & VA_ENC_INTERLACED_PAFF)
1020             printf("Support VA_ENC_INTERLACED_PAFF\n");
1021         
1022         config_attrib[config_attrib_num].type = VAConfigAttribEncInterlaced;
1023         config_attrib[config_attrib_num].value = VA_ENC_PACKED_HEADER_NONE;
1024         config_attrib_num++;
1025     }
1026     
1027     if (attrib[VAConfigAttribEncMaxRefFrames].value != VA_ATTRIB_NOT_SUPPORTED) {
1028         h264_maxref = attrib[VAConfigAttribEncMaxRefFrames].value;
1029         
1030         printf("Support %d RefPicList0 and %d RefPicList1\n",
1031                h264_maxref & 0xffff, (h264_maxref >> 16) & 0xffff );
1032     }
1033
1034     if (attrib[VAConfigAttribEncMaxSlices].value != VA_ATTRIB_NOT_SUPPORTED)
1035         printf("Support %d slices\n", attrib[VAConfigAttribEncMaxSlices].value);
1036
1037     if (attrib[VAConfigAttribEncSliceStructure].value != VA_ATTRIB_NOT_SUPPORTED) {
1038         int tmp = attrib[VAConfigAttribEncSliceStructure].value;
1039         
1040         printf("Support VAConfigAttribEncSliceStructure\n");
1041
1042         if (tmp & VA_ENC_SLICE_STRUCTURE_ARBITRARY_ROWS)
1043             printf("Support VA_ENC_SLICE_STRUCTURE_ARBITRARY_ROWS\n");
1044         if (tmp & VA_ENC_SLICE_STRUCTURE_POWER_OF_TWO_ROWS)
1045             printf("Support VA_ENC_SLICE_STRUCTURE_POWER_OF_TWO_ROWS\n");
1046         if (tmp & VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS)
1047             printf("Support VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS\n");
1048     }
1049     if (attrib[VAConfigAttribEncMacroblockInfo].value != VA_ATTRIB_NOT_SUPPORTED) {
1050         printf("Support VAConfigAttribEncMacroblockInfo\n");
1051     }
1052
1053     free(entrypoints);
1054     return 0;
1055 }
1056
1057 static int setup_encode()
1058 {
1059     VAStatus va_status;
1060     VASurfaceID *tmp_surfaceid;
1061     int codedbuf_size, i;
1062     static VASurfaceID src_surface[SURFACE_NUM];
1063     static VASurfaceID ref_surface[SURFACE_NUM];
1064     
1065     va_status = vaCreateConfig(va_dpy, h264_profile, VAEntrypointEncSlice,
1066             &config_attrib[0], config_attrib_num, &config_id);
1067     CHECK_VASTATUS(va_status, "vaCreateConfig");
1068
1069     /* create source surfaces */
1070     va_status = vaCreateSurfaces(va_dpy,
1071                                  VA_RT_FORMAT_YUV420, frame_width_mbaligned, frame_height_mbaligned,
1072                                  &src_surface[0], SURFACE_NUM,
1073                                  NULL, 0);
1074     CHECK_VASTATUS(va_status, "vaCreateSurfaces");
1075
1076     /* create reference surfaces */
1077     va_status = vaCreateSurfaces(va_dpy,
1078                                  VA_RT_FORMAT_YUV420, frame_width_mbaligned, frame_height_mbaligned,
1079                                  &ref_surface[0], SURFACE_NUM,
1080                                  NULL, 0);
1081     CHECK_VASTATUS(va_status, "vaCreateSurfaces");
1082
1083     tmp_surfaceid = (VASurfaceID *)calloc(2 * SURFACE_NUM, sizeof(VASurfaceID));
1084     memcpy(tmp_surfaceid, src_surface, SURFACE_NUM * sizeof(VASurfaceID));
1085     memcpy(tmp_surfaceid + SURFACE_NUM, ref_surface, SURFACE_NUM * sizeof(VASurfaceID));
1086     
1087     /* Create a context for this encode pipe */
1088     va_status = vaCreateContext(va_dpy, config_id,
1089                                 frame_width_mbaligned, frame_height_mbaligned,
1090                                 VA_PROGRESSIVE,
1091                                 tmp_surfaceid, 2 * SURFACE_NUM,
1092                                 &context_id);
1093     CHECK_VASTATUS(va_status, "vaCreateContext");
1094     free(tmp_surfaceid);
1095
1096     codedbuf_size = (frame_width_mbaligned * frame_height_mbaligned * 400) / (16*16);
1097
1098     for (i = 0; i < SURFACE_NUM; i++) {
1099         /* create coded buffer once for all
1100          * other VA buffers which won't be used again after vaRenderPicture.
1101          * so APP can always vaCreateBuffer for every frame
1102          * but coded buffer need to be mapped and accessed after vaRenderPicture/vaEndPicture
1103          * so VA won't maintain the coded buffer
1104          */
1105         va_status = vaCreateBuffer(va_dpy, context_id, VAEncCodedBufferType,
1106                 codedbuf_size, 1, NULL, &gl_surfaces[i].coded_buf);
1107         CHECK_VASTATUS(va_status, "vaCreateBuffer");
1108     }
1109
1110     /* create OpenGL objects */
1111     //glGenFramebuffers(SURFACE_NUM, fbos);
1112     
1113     for (i = 0; i < SURFACE_NUM; i++) {
1114         glGenTextures(1, &gl_surfaces[i].y_tex);
1115         glGenTextures(1, &gl_surfaces[i].cbcr_tex);
1116     }
1117
1118     for (i = 0; i < SURFACE_NUM; i++) {
1119         gl_surfaces[i].src_surface = src_surface[i];
1120         gl_surfaces[i].ref_surface = ref_surface[i];
1121     }
1122     
1123     return 0;
1124 }
1125
1126
1127
1128 #define partition(ref, field, key, ascending)   \
1129     while (i <= j) {                            \
1130         if (ascending) {                        \
1131             while (ref[i].field < key)          \
1132                 i++;                            \
1133             while (ref[j].field > key)          \
1134                 j--;                            \
1135         } else {                                \
1136             while (ref[i].field > key)          \
1137                 i++;                            \
1138             while (ref[j].field < key)          \
1139                 j--;                            \
1140         }                                       \
1141         if (i <= j) {                           \
1142             tmp = ref[i];                       \
1143             ref[i] = ref[j];                    \
1144             ref[j] = tmp;                       \
1145             i++;                                \
1146             j--;                                \
1147         }                                       \
1148     }                                           \
1149
1150 static void sort_one(VAPictureH264 ref[], int left, int right,
1151                      int ascending, int frame_idx)
1152 {
1153     int i = left, j = right;
1154     unsigned int key;
1155     VAPictureH264 tmp;
1156
1157     if (frame_idx) {
1158         key = ref[(left + right) / 2].frame_idx;
1159         partition(ref, frame_idx, key, ascending);
1160     } else {
1161         key = ref[(left + right) / 2].TopFieldOrderCnt;
1162         partition(ref, TopFieldOrderCnt, (signed int)key, ascending);
1163     }
1164     
1165     /* recursion */
1166     if (left < j)
1167         sort_one(ref, left, j, ascending, frame_idx);
1168     
1169     if (i < right)
1170         sort_one(ref, i, right, ascending, frame_idx);
1171 }
1172
1173 static void sort_two(VAPictureH264 ref[], int left, int right, unsigned int key, unsigned int frame_idx,
1174                      int partition_ascending, int list0_ascending, int list1_ascending)
1175 {
1176     int i = left, j = right;
1177     VAPictureH264 tmp;
1178
1179     if (frame_idx) {
1180         partition(ref, frame_idx, key, partition_ascending);
1181     } else {
1182         partition(ref, TopFieldOrderCnt, (signed int)key, partition_ascending);
1183     }
1184     
1185
1186     sort_one(ref, left, i-1, list0_ascending, frame_idx);
1187     sort_one(ref, j+1, right, list1_ascending, frame_idx);
1188 }
1189
1190 static int update_ReferenceFrames(void)
1191 {
1192     int i;
1193     
1194     if (current_frame_type == FRAME_B)
1195         return 0;
1196
1197     CurrentCurrPic.flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE;
1198     numShortTerm++;
1199     if (numShortTerm > num_ref_frames)
1200         numShortTerm = num_ref_frames;
1201     for (i=numShortTerm-1; i>0; i--)
1202         ReferenceFrames[i] = ReferenceFrames[i-1];
1203     ReferenceFrames[0] = CurrentCurrPic;
1204     
1205     if (current_frame_type != FRAME_B)
1206         current_frame_num++;
1207     if (current_frame_num > MaxFrameNum)
1208         current_frame_num = 0;
1209     
1210     return 0;
1211 }
1212
1213
1214 static int update_RefPicList(void)
1215 {
1216     unsigned int current_poc = CurrentCurrPic.TopFieldOrderCnt;
1217     
1218     if (current_frame_type == FRAME_P) {
1219         memcpy(RefPicList0_P, ReferenceFrames, numShortTerm * sizeof(VAPictureH264));
1220         sort_one(RefPicList0_P, 0, numShortTerm-1, 0, 1);
1221     }
1222     
1223     if (current_frame_type == FRAME_B) {
1224         memcpy(RefPicList0_B, ReferenceFrames, numShortTerm * sizeof(VAPictureH264));
1225         sort_two(RefPicList0_B, 0, numShortTerm-1, current_poc, 0,
1226                  1, 0, 1);
1227
1228         memcpy(RefPicList1_B, ReferenceFrames, numShortTerm * sizeof(VAPictureH264));
1229         sort_two(RefPicList1_B, 0, numShortTerm-1, current_poc, 0,
1230                  0, 1, 0);
1231     }
1232     
1233     return 0;
1234 }
1235
1236
1237 static int render_sequence(void)
1238 {
1239     VABufferID seq_param_buf, rc_param_buf, misc_param_tmpbuf, render_id[2];
1240     VAStatus va_status;
1241     VAEncMiscParameterBuffer *misc_param, *misc_param_tmp;
1242     VAEncMiscParameterRateControl *misc_rate_ctrl;
1243     
1244     seq_param.level_idc = 41 /*SH_LEVEL_3*/;
1245     seq_param.picture_width_in_mbs = frame_width_mbaligned / 16;
1246     seq_param.picture_height_in_mbs = frame_height_mbaligned / 16;
1247     seq_param.bits_per_second = frame_bitrate;
1248
1249     seq_param.intra_period = intra_period;
1250     seq_param.intra_idr_period = intra_idr_period;
1251     seq_param.ip_period = ip_period;
1252
1253     seq_param.max_num_ref_frames = num_ref_frames;
1254     seq_param.seq_fields.bits.frame_mbs_only_flag = 1;
1255     seq_param.time_scale = frame_rate * 2;
1256     seq_param.num_units_in_tick = 1; /* Tc = num_units_in_tick / scale */
1257     seq_param.seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4 = Log2MaxPicOrderCntLsb - 4;
1258     seq_param.seq_fields.bits.log2_max_frame_num_minus4 = Log2MaxFrameNum - 4;;
1259     seq_param.seq_fields.bits.frame_mbs_only_flag = 1;
1260     seq_param.seq_fields.bits.chroma_format_idc = 1;
1261     seq_param.seq_fields.bits.direct_8x8_inference_flag = 1;
1262     
1263     if (frame_width != frame_width_mbaligned ||
1264         frame_height != frame_height_mbaligned) {
1265         seq_param.frame_cropping_flag = 1;
1266         seq_param.frame_crop_left_offset = 0;
1267         seq_param.frame_crop_right_offset = (frame_width_mbaligned - frame_width)/2;
1268         seq_param.frame_crop_top_offset = 0;
1269         seq_param.frame_crop_bottom_offset = (frame_height_mbaligned - frame_height)/2;
1270     }
1271     
1272     va_status = vaCreateBuffer(va_dpy, context_id,
1273                                VAEncSequenceParameterBufferType,
1274                                sizeof(seq_param), 1, &seq_param, &seq_param_buf);
1275     CHECK_VASTATUS(va_status, "vaCreateBuffer");
1276     
1277     va_status = vaCreateBuffer(va_dpy, context_id,
1278                                VAEncMiscParameterBufferType,
1279                                sizeof(VAEncMiscParameterBuffer) + sizeof(VAEncMiscParameterRateControl),
1280                                1, NULL, &rc_param_buf);
1281     CHECK_VASTATUS(va_status, "vaCreateBuffer");
1282     
1283     vaMapBuffer(va_dpy, rc_param_buf, (void **)&misc_param);
1284     misc_param->type = VAEncMiscParameterTypeRateControl;
1285     misc_rate_ctrl = (VAEncMiscParameterRateControl *)misc_param->data;
1286     memset(misc_rate_ctrl, 0, sizeof(*misc_rate_ctrl));
1287     misc_rate_ctrl->bits_per_second = frame_bitrate;
1288     misc_rate_ctrl->target_percentage = 66;
1289     misc_rate_ctrl->window_size = 1000;
1290     misc_rate_ctrl->initial_qp = initial_qp;
1291     misc_rate_ctrl->min_qp = minimal_qp;
1292     misc_rate_ctrl->basic_unit_size = 0;
1293     vaUnmapBuffer(va_dpy, rc_param_buf);
1294
1295     render_id[0] = seq_param_buf;
1296     render_id[1] = rc_param_buf;
1297     
1298     va_status = vaRenderPicture(va_dpy, context_id, &render_id[0], 2);
1299     CHECK_VASTATUS(va_status, "vaRenderPicture");;
1300
1301     if (misc_priv_type != 0) {
1302         va_status = vaCreateBuffer(va_dpy, context_id,
1303                                    VAEncMiscParameterBufferType,
1304                                    sizeof(VAEncMiscParameterBuffer),
1305                                    1, NULL, &misc_param_tmpbuf);
1306         CHECK_VASTATUS(va_status, "vaCreateBuffer");
1307         vaMapBuffer(va_dpy, misc_param_tmpbuf, (void **)&misc_param_tmp);
1308         misc_param_tmp->type = (VAEncMiscParameterType)misc_priv_type;
1309         misc_param_tmp->data[0] = misc_priv_value;
1310         vaUnmapBuffer(va_dpy, misc_param_tmpbuf);
1311     
1312         va_status = vaRenderPicture(va_dpy, context_id, &misc_param_tmpbuf, 1);
1313     }
1314     
1315     return 0;
1316 }
1317
1318 static int calc_poc(int pic_order_cnt_lsb)
1319 {
1320     static int PicOrderCntMsb_ref = 0, pic_order_cnt_lsb_ref = 0;
1321     int prevPicOrderCntMsb, prevPicOrderCntLsb;
1322     int PicOrderCntMsb, TopFieldOrderCnt;
1323     
1324     if (current_frame_type == FRAME_IDR)
1325         prevPicOrderCntMsb = prevPicOrderCntLsb = 0;
1326     else {
1327         prevPicOrderCntMsb = PicOrderCntMsb_ref;
1328         prevPicOrderCntLsb = pic_order_cnt_lsb_ref;
1329     }
1330     
1331     if ((pic_order_cnt_lsb < prevPicOrderCntLsb) &&
1332         ((prevPicOrderCntLsb - pic_order_cnt_lsb) >= (int)(MaxPicOrderCntLsb / 2)))
1333         PicOrderCntMsb = prevPicOrderCntMsb + MaxPicOrderCntLsb;
1334     else if ((pic_order_cnt_lsb > prevPicOrderCntLsb) &&
1335              ((pic_order_cnt_lsb - prevPicOrderCntLsb) > (int)(MaxPicOrderCntLsb / 2)))
1336         PicOrderCntMsb = prevPicOrderCntMsb - MaxPicOrderCntLsb;
1337     else
1338         PicOrderCntMsb = prevPicOrderCntMsb;
1339     
1340     TopFieldOrderCnt = PicOrderCntMsb + pic_order_cnt_lsb;
1341
1342     if (current_frame_type != FRAME_B) {
1343         PicOrderCntMsb_ref = PicOrderCntMsb;
1344         pic_order_cnt_lsb_ref = pic_order_cnt_lsb;
1345     }
1346     
1347     return TopFieldOrderCnt;
1348 }
1349
1350 static int render_picture(void)
1351 {
1352     VABufferID pic_param_buf;
1353     VAStatus va_status;
1354     int i = 0;
1355
1356     pic_param.CurrPic.picture_id = gl_surfaces[current_frame_display % SURFACE_NUM].ref_surface;
1357     pic_param.CurrPic.frame_idx = current_frame_num;
1358     pic_param.CurrPic.flags = 0;
1359     pic_param.CurrPic.TopFieldOrderCnt = calc_poc((current_frame_display - current_IDR_display) % MaxPicOrderCntLsb);
1360     pic_param.CurrPic.BottomFieldOrderCnt = pic_param.CurrPic.TopFieldOrderCnt;
1361     CurrentCurrPic = pic_param.CurrPic;
1362
1363     if (getenv("TO_DEL")) { /* set RefPicList into ReferenceFrames */
1364         update_RefPicList(); /* calc RefPicList */
1365         memset(pic_param.ReferenceFrames, 0xff, 16 * sizeof(VAPictureH264)); /* invalid all */
1366         if (current_frame_type == FRAME_P) {
1367             pic_param.ReferenceFrames[0] = RefPicList0_P[0];
1368         } else if (current_frame_type == FRAME_B) {
1369             pic_param.ReferenceFrames[0] = RefPicList0_B[0];
1370             pic_param.ReferenceFrames[1] = RefPicList1_B[0];
1371         }
1372     } else {
1373         memcpy(pic_param.ReferenceFrames, ReferenceFrames, numShortTerm*sizeof(VAPictureH264));
1374         for (i = numShortTerm; i < SURFACE_NUM; i++) {
1375             pic_param.ReferenceFrames[i].picture_id = VA_INVALID_SURFACE;
1376             pic_param.ReferenceFrames[i].flags = VA_PICTURE_H264_INVALID;
1377         }
1378     }
1379     
1380     pic_param.pic_fields.bits.idr_pic_flag = (current_frame_type == FRAME_IDR);
1381     pic_param.pic_fields.bits.reference_pic_flag = (current_frame_type != FRAME_B);
1382     pic_param.pic_fields.bits.entropy_coding_mode_flag = h264_entropy_mode;
1383     pic_param.pic_fields.bits.deblocking_filter_control_present_flag = 1;
1384     pic_param.frame_num = current_frame_num;
1385     pic_param.coded_buf = gl_surfaces[current_frame_display % SURFACE_NUM].coded_buf;
1386     pic_param.last_picture = false;  // FIXME
1387     pic_param.pic_init_qp = initial_qp;
1388
1389     va_status = vaCreateBuffer(va_dpy, context_id, VAEncPictureParameterBufferType,
1390                                sizeof(pic_param), 1, &pic_param, &pic_param_buf);
1391     CHECK_VASTATUS(va_status, "vaCreateBuffer");;
1392
1393     va_status = vaRenderPicture(va_dpy, context_id, &pic_param_buf, 1);
1394     CHECK_VASTATUS(va_status, "vaRenderPicture");
1395
1396     return 0;
1397 }
1398
1399 static int render_packedsequence(void)
1400 {
1401     VAEncPackedHeaderParameterBuffer packedheader_param_buffer;
1402     VABufferID packedseq_para_bufid, packedseq_data_bufid, render_id[2];
1403     unsigned int length_in_bits;
1404     unsigned char *packedseq_buffer = NULL;
1405     VAStatus va_status;
1406
1407     length_in_bits = build_packed_seq_buffer(&packedseq_buffer); 
1408     
1409     packedheader_param_buffer.type = VAEncPackedHeaderSequence;
1410     
1411     packedheader_param_buffer.bit_length = length_in_bits; /*length_in_bits*/
1412     packedheader_param_buffer.has_emulation_bytes = 0;
1413     va_status = vaCreateBuffer(va_dpy,
1414                                context_id,
1415                                VAEncPackedHeaderParameterBufferType,
1416                                sizeof(packedheader_param_buffer), 1, &packedheader_param_buffer,
1417                                &packedseq_para_bufid);
1418     CHECK_VASTATUS(va_status, "vaCreateBuffer");
1419
1420     va_status = vaCreateBuffer(va_dpy,
1421                                context_id,
1422                                VAEncPackedHeaderDataBufferType,
1423                                (length_in_bits + 7) / 8, 1, packedseq_buffer,
1424                                &packedseq_data_bufid);
1425     CHECK_VASTATUS(va_status, "vaCreateBuffer");
1426
1427     render_id[0] = packedseq_para_bufid;
1428     render_id[1] = packedseq_data_bufid;
1429     va_status = vaRenderPicture(va_dpy, context_id, render_id, 2);
1430     CHECK_VASTATUS(va_status, "vaRenderPicture");
1431
1432     free(packedseq_buffer);
1433     
1434     return 0;
1435 }
1436
1437
1438 static int render_packedpicture(void)
1439 {
1440     VAEncPackedHeaderParameterBuffer packedheader_param_buffer;
1441     VABufferID packedpic_para_bufid, packedpic_data_bufid, render_id[2];
1442     unsigned int length_in_bits;
1443     unsigned char *packedpic_buffer = NULL;
1444     VAStatus va_status;
1445
1446     length_in_bits = build_packed_pic_buffer(&packedpic_buffer); 
1447     packedheader_param_buffer.type = VAEncPackedHeaderPicture;
1448     packedheader_param_buffer.bit_length = length_in_bits;
1449     packedheader_param_buffer.has_emulation_bytes = 0;
1450
1451     va_status = vaCreateBuffer(va_dpy,
1452                                context_id,
1453                                VAEncPackedHeaderParameterBufferType,
1454                                sizeof(packedheader_param_buffer), 1, &packedheader_param_buffer,
1455                                &packedpic_para_bufid);
1456     CHECK_VASTATUS(va_status, "vaCreateBuffer");
1457
1458     va_status = vaCreateBuffer(va_dpy,
1459                                context_id,
1460                                VAEncPackedHeaderDataBufferType,
1461                                (length_in_bits + 7) / 8, 1, packedpic_buffer,
1462                                &packedpic_data_bufid);
1463     CHECK_VASTATUS(va_status, "vaCreateBuffer");
1464
1465     render_id[0] = packedpic_para_bufid;
1466     render_id[1] = packedpic_data_bufid;
1467     va_status = vaRenderPicture(va_dpy, context_id, render_id, 2);
1468     CHECK_VASTATUS(va_status, "vaRenderPicture");
1469
1470     free(packedpic_buffer);
1471     
1472     return 0;
1473 }
1474
1475 static void render_packedslice()
1476 {
1477     VAEncPackedHeaderParameterBuffer packedheader_param_buffer;
1478     VABufferID packedslice_para_bufid, packedslice_data_bufid, render_id[2];
1479     unsigned int length_in_bits;
1480     unsigned char *packedslice_buffer = NULL;
1481     VAStatus va_status;
1482
1483     length_in_bits = build_packed_slice_buffer(&packedslice_buffer);
1484     packedheader_param_buffer.type = VAEncPackedHeaderSlice;
1485     packedheader_param_buffer.bit_length = length_in_bits;
1486     packedheader_param_buffer.has_emulation_bytes = 0;
1487
1488     va_status = vaCreateBuffer(va_dpy,
1489                                context_id,
1490                                VAEncPackedHeaderParameterBufferType,
1491                                sizeof(packedheader_param_buffer), 1, &packedheader_param_buffer,
1492                                &packedslice_para_bufid);
1493     CHECK_VASTATUS(va_status, "vaCreateBuffer");
1494
1495     va_status = vaCreateBuffer(va_dpy,
1496                                context_id,
1497                                VAEncPackedHeaderDataBufferType,
1498                                (length_in_bits + 7) / 8, 1, packedslice_buffer,
1499                                &packedslice_data_bufid);
1500     CHECK_VASTATUS(va_status, "vaCreateBuffer");
1501
1502     render_id[0] = packedslice_para_bufid;
1503     render_id[1] = packedslice_data_bufid;
1504     va_status = vaRenderPicture(va_dpy, context_id, render_id, 2);
1505     CHECK_VASTATUS(va_status, "vaRenderPicture");
1506
1507     free(packedslice_buffer);
1508 }
1509
1510 static int render_slice(void)
1511 {
1512     VABufferID slice_param_buf;
1513     VAStatus va_status;
1514     int i;
1515
1516     update_RefPicList();
1517     
1518     /* one frame, one slice */
1519     slice_param.macroblock_address = 0;
1520     slice_param.num_macroblocks = frame_width_mbaligned * frame_height_mbaligned/(16*16); /* Measured by MB */
1521     slice_param.slice_type = (current_frame_type == FRAME_IDR)?2:current_frame_type;
1522     if (current_frame_type == FRAME_IDR) {
1523         if (current_frame_encoding != 0)
1524             ++slice_param.idr_pic_id;
1525     } else if (current_frame_type == FRAME_P) {
1526         int refpiclist0_max = h264_maxref & 0xffff;
1527         memcpy(slice_param.RefPicList0, RefPicList0_P, refpiclist0_max*sizeof(VAPictureH264));
1528
1529         for (i = refpiclist0_max; i < 32; i++) {
1530             slice_param.RefPicList0[i].picture_id = VA_INVALID_SURFACE;
1531             slice_param.RefPicList0[i].flags = VA_PICTURE_H264_INVALID;
1532         }
1533     } else if (current_frame_type == FRAME_B) {
1534         int refpiclist0_max = h264_maxref & 0xffff;
1535         int refpiclist1_max = (h264_maxref >> 16) & 0xffff;
1536
1537         memcpy(slice_param.RefPicList0, RefPicList0_B, refpiclist0_max*sizeof(VAPictureH264));
1538         for (i = refpiclist0_max; i < 32; i++) {
1539             slice_param.RefPicList0[i].picture_id = VA_INVALID_SURFACE;
1540             slice_param.RefPicList0[i].flags = VA_PICTURE_H264_INVALID;
1541         }
1542
1543         memcpy(slice_param.RefPicList1, RefPicList1_B, refpiclist1_max*sizeof(VAPictureH264));
1544         for (i = refpiclist1_max; i < 32; i++) {
1545             slice_param.RefPicList1[i].picture_id = VA_INVALID_SURFACE;
1546             slice_param.RefPicList1[i].flags = VA_PICTURE_H264_INVALID;
1547         }
1548     }
1549
1550     slice_param.slice_alpha_c0_offset_div2 = 0;
1551     slice_param.slice_beta_offset_div2 = 0;
1552     slice_param.direct_spatial_mv_pred_flag = 1;
1553     slice_param.pic_order_cnt_lsb = (current_frame_display - current_IDR_display) % MaxPicOrderCntLsb;
1554     
1555
1556     if (h264_packedheader &&
1557         config_attrib[enc_packed_header_idx].value & VA_ENC_PACKED_HEADER_SLICE)
1558         render_packedslice();
1559
1560     va_status = vaCreateBuffer(va_dpy, context_id, VAEncSliceParameterBufferType,
1561                                sizeof(slice_param), 1, &slice_param, &slice_param_buf);
1562     CHECK_VASTATUS(va_status, "vaCreateBuffer");;
1563
1564     va_status = vaRenderPicture(va_dpy, context_id, &slice_param_buf, 1);
1565     CHECK_VASTATUS(va_status, "vaRenderPicture");
1566     
1567     return 0;
1568 }
1569
1570
1571
1572 int H264Encoder::save_codeddata(unsigned long long display_order, unsigned long long encode_order, int frame_type)
1573 {    
1574     VACodedBufferSegment *buf_list = NULL;
1575     VAStatus va_status;
1576     unsigned int coded_size = 0;
1577
1578     string data;
1579
1580     va_status = vaMapBuffer(va_dpy, gl_surfaces[display_order % SURFACE_NUM].coded_buf, (void **)(&buf_list));
1581     CHECK_VASTATUS(va_status, "vaMapBuffer");
1582     while (buf_list != NULL) {
1583         data.append(reinterpret_cast<const char *>(buf_list->buf), buf_list->size);
1584         buf_list = (VACodedBufferSegment *) buf_list->next;
1585
1586         frame_size += coded_size;
1587     }
1588     vaUnmapBuffer(va_dpy, gl_surfaces[display_order % SURFACE_NUM].coded_buf);
1589
1590     AVPacket pkt;
1591     memset(&pkt, 0, sizeof(pkt));
1592     pkt.buf = nullptr;
1593     pkt.pts = av_rescale_q(display_order, AVRational{1, frame_rate}, avstream->time_base);
1594     pkt.dts = av_rescale_q(encode_order, AVRational{1, frame_rate}, avstream->time_base);
1595     pkt.data = reinterpret_cast<uint8_t *>(&data[0]);
1596     pkt.size = data.size();
1597     pkt.stream_index = 0;
1598     if (frame_type == FRAME_IDR || frame_type == FRAME_I) {
1599         pkt.flags = AV_PKT_FLAG_KEY;
1600     } else {
1601         pkt.flags = 0;
1602     }
1603     pkt.duration = 1;
1604     av_interleaved_write_frame(avctx, &pkt);
1605
1606 #if 0
1607     printf("\r      "); /* return back to startpoint */
1608     switch (encode_order % 4) {
1609         case 0:
1610             printf("|");
1611             break;
1612         case 1:
1613             printf("/");
1614             break;
1615         case 2:
1616             printf("-");
1617             break;
1618         case 3:
1619             printf("\\");
1620             break;
1621     }
1622     printf("%08lld", encode_order);
1623     printf("(%06d bytes coded)", coded_size);
1624 #endif
1625
1626     return 0;
1627 }
1628
1629
1630 // this is weird. but it seems to put a new frame onto the queue
1631 void H264Encoder::storage_task_enqueue(unsigned long long display_order, unsigned long long encode_order, int frame_type)
1632 {
1633         std::unique_lock<std::mutex> lock(storage_task_queue_mutex);
1634
1635         storage_task tmp;
1636         tmp.display_order = display_order;
1637         tmp.encode_order = encode_order;
1638         tmp.frame_type = frame_type;
1639         storage_task_queue.push(tmp);
1640         srcsurface_status[display_order % SURFACE_NUM] = SRC_SURFACE_IN_ENCODING;
1641
1642         storage_task_queue_changed.notify_all();
1643 }
1644
1645 void H264Encoder::storage_task_thread()
1646 {
1647         for ( ;; ) {
1648                 storage_task current;
1649                 {
1650                         // wait until there's an encoded frame  
1651                         std::unique_lock<std::mutex> lock(storage_task_queue_mutex);
1652                         storage_task_queue_changed.wait(lock, [this]{ return storage_thread_should_quit || !storage_task_queue.empty(); });
1653                         if (storage_thread_should_quit) return;
1654                         current = storage_task_queue.front();
1655                         storage_task_queue.pop();
1656                 }
1657
1658                 VAStatus va_status;
1659            
1660                 // waits for data, then saves it to disk.
1661                 va_status = vaSyncSurface(va_dpy, gl_surfaces[current.display_order % SURFACE_NUM].src_surface);
1662                 CHECK_VASTATUS(va_status, "vaSyncSurface");
1663                 save_codeddata(current.display_order, current.encode_order, current.frame_type);
1664
1665                 {
1666                         std::unique_lock<std::mutex> lock(storage_task_queue_mutex);
1667                         srcsurface_status[current.display_order % SURFACE_NUM] = SRC_SURFACE_FREE;
1668                         storage_task_queue_changed.notify_all();
1669                 }
1670         }
1671 }
1672
1673 static int release_encode()
1674 {
1675     int i;
1676     
1677     for (i = 0; i < SURFACE_NUM; i++) {
1678         vaDestroyBuffer(va_dpy, gl_surfaces[i].coded_buf);
1679         vaDestroySurfaces(va_dpy, &gl_surfaces[i].src_surface, 1);
1680         vaDestroySurfaces(va_dpy, &gl_surfaces[i].ref_surface, 1);
1681     }
1682     
1683     vaDestroyContext(va_dpy, context_id);
1684     vaDestroyConfig(va_dpy, config_id);
1685
1686     return 0;
1687 }
1688
1689 static int deinit_va()
1690
1691     vaTerminate(va_dpy);
1692
1693     va_close_display(va_dpy);
1694
1695     return 0;
1696 }
1697
1698
1699 static int print_input()
1700 {
1701     printf("\n\nINPUT:Try to encode H264...\n");
1702     if (rc_mode != -1)
1703         printf("INPUT: RateControl  : %s\n", rc_to_string(rc_mode));
1704     printf("INPUT: Resolution   : %dx%dframes\n", frame_width, frame_height);
1705     printf("INPUT: FrameRate    : %d\n", frame_rate);
1706     printf("INPUT: Bitrate      : %d\n", frame_bitrate);
1707     printf("INPUT: Slieces      : %d\n", frame_slices);
1708     printf("INPUT: IntraPeriod  : %d\n", intra_period);
1709     printf("INPUT: IDRPeriod    : %d\n", intra_idr_period);
1710     printf("INPUT: IpPeriod     : %d\n", ip_period);
1711     printf("INPUT: Initial QP   : %d\n", initial_qp);
1712     printf("INPUT: Min QP       : %d\n", minimal_qp);
1713     printf("INPUT: Coded Clip   : %s\n", coded_fn);
1714     
1715     printf("\n\n"); /* return back to startpoint */
1716     
1717     return 0;
1718 }
1719
1720
1721 //H264Encoder::H264Encoder(SDL_Window *window, SDL_GLContext context, int width, int height, const char *output_filename) 
1722 H264Encoder::H264Encoder(QSurface *surface, int width, int height, const char *output_filename)
1723         : current_storage_frame(0), surface(surface)
1724         //: width(width), height(height), current_encoding_frame(0)
1725 {
1726         av_register_all();
1727         avctx = avformat_alloc_context();
1728         avctx->oformat = av_guess_format(NULL, output_filename, NULL);
1729         strcpy(avctx->filename, output_filename);
1730         if (avio_open2(&avctx->pb, output_filename, AVIO_FLAG_WRITE, &avctx->interrupt_callback, NULL) < 0) {
1731                 fprintf(stderr, "%s: avio_open2() failed\n", output_filename);
1732                 exit(1);
1733         }
1734         AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_H264);
1735         avstream = avformat_new_stream(avctx, codec);
1736         if (avstream == nullptr) {
1737                 fprintf(stderr, "%s: avformat_new_stream() failed\n", output_filename);
1738                 exit(1);
1739         }
1740         avstream->time_base = AVRational{1, frame_rate};
1741         avstream->codec->width = width;
1742         avstream->codec->height = height;
1743         avstream->codec->time_base = AVRational{1, frame_rate};
1744         avstream->codec->ticks_per_frame = 1;  // or 2?
1745
1746         if (avformat_write_header(avctx, NULL) < 0) {
1747                 fprintf(stderr, "%s: avformat_write_header() failed\n", output_filename);
1748                 exit(1);
1749         }
1750
1751         frame_width = width;
1752         frame_height = height;
1753         frame_width_mbaligned = (frame_width + 15) & (~15);
1754         frame_height_mbaligned = (frame_height + 15) & (~15);
1755         frame_bitrate = 15000000;  // / 60;
1756         current_frame_encoding = 0;
1757
1758         print_input();
1759
1760         init_va();
1761         setup_encode();
1762
1763         // No frames are ready yet.
1764         memset(srcsurface_status, SRC_SURFACE_FREE, sizeof(srcsurface_status));
1765             
1766         memset(&seq_param, 0, sizeof(seq_param));
1767         memset(&pic_param, 0, sizeof(pic_param));
1768         memset(&slice_param, 0, sizeof(slice_param));
1769
1770         storage_thread = std::thread(&H264Encoder::storage_task_thread, this);
1771
1772         copy_thread = std::thread([this]{
1773                 //SDL_GL_MakeCurrent(window, context);
1774                 QOpenGLContext *context = create_context();
1775                 eglBindAPI(EGL_OPENGL_API);
1776                 if (!make_current(context, this->surface)) {
1777                         printf("display=%p surface=%p context=%p curr=%p err=%d\n", eglGetCurrentDisplay(), this->surface, context, eglGetCurrentContext(),
1778                                 eglGetError());
1779                         exit(1);
1780                 }
1781                 copy_thread_func();
1782         });
1783 }
1784
1785 H264Encoder::~H264Encoder()
1786 {
1787         {
1788                 unique_lock<mutex> lock(storage_task_queue_mutex);
1789                 storage_thread_should_quit = true;
1790                 storage_task_queue_changed.notify_all();
1791         }
1792         {
1793                 unique_lock<mutex> lock(frame_queue_mutex);
1794                 copy_thread_should_quit = true;
1795                 frame_queue_nonempty.notify_one();
1796         }
1797         storage_thread.join();
1798         copy_thread.join();
1799
1800         release_encode();
1801         deinit_va();
1802
1803         av_write_trailer(avctx);
1804         avformat_free_context(avctx);
1805 }
1806
1807 bool H264Encoder::begin_frame(GLuint *y_tex, GLuint *cbcr_tex)
1808 {
1809         {
1810                 // Wait until this frame slot is done encoding.
1811                 std::unique_lock<std::mutex> lock(storage_task_queue_mutex);
1812                 storage_task_queue_changed.wait(lock, [this]{ return storage_thread_should_quit || (srcsurface_status[current_storage_frame % SURFACE_NUM] == SRC_SURFACE_FREE); });
1813                 if (storage_thread_should_quit) return false;
1814         }
1815
1816         //*fbo = fbos[current_storage_frame % SURFACE_NUM];
1817         GLSurface *surf = &gl_surfaces[current_storage_frame % SURFACE_NUM];
1818         *y_tex = surf->y_tex;
1819         *cbcr_tex = surf->cbcr_tex;
1820
1821         VASurfaceID surface = surf->src_surface;
1822         VAStatus va_status = vaDeriveImage(va_dpy, surface, &surf->surface_image);
1823         CHECK_VASTATUS(va_status, "vaDeriveImage");
1824
1825         VABufferInfo buf_info;
1826         buf_info.mem_type = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME;  // or VA_SURFACE_ATTRIB_MEM_TYPE_KERNEL_DRM?
1827         va_status = vaAcquireBufferHandle(va_dpy, surf->surface_image.buf, &buf_info);
1828         CHECK_VASTATUS(va_status, "vaAcquireBufferHandle");
1829
1830         // Create Y image.
1831         surf->y_egl_image = EGL_NO_IMAGE_KHR;
1832         EGLint y_attribs[] = {
1833                 EGL_WIDTH, frame_width,
1834                 EGL_HEIGHT, frame_height,
1835                 EGL_LINUX_DRM_FOURCC_EXT, fourcc_code('R', '8', ' ', ' '),
1836                 EGL_DMA_BUF_PLANE0_FD_EXT, EGLint(buf_info.handle),
1837                 EGL_DMA_BUF_PLANE0_OFFSET_EXT, EGLint(surf->surface_image.offsets[0]),
1838                 EGL_DMA_BUF_PLANE0_PITCH_EXT, EGLint(surf->surface_image.pitches[0]),
1839                 EGL_NONE
1840         };
1841
1842         surf->y_egl_image = eglCreateImageKHR(eglGetCurrentDisplay(), EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, NULL, y_attribs);
1843         assert(surf->y_egl_image != EGL_NO_IMAGE_KHR);
1844
1845         // Associate Y image to a texture.
1846         glBindTexture(GL_TEXTURE_2D, *y_tex);
1847         glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, surf->y_egl_image);
1848
1849         // Create CbCr image.
1850         surf->cbcr_egl_image = EGL_NO_IMAGE_KHR;
1851         EGLint cbcr_attribs[] = {
1852                 EGL_WIDTH, frame_width,
1853                 EGL_HEIGHT, frame_height,
1854                 EGL_LINUX_DRM_FOURCC_EXT, fourcc_code('G', 'R', '8', '8'),
1855                 EGL_DMA_BUF_PLANE0_FD_EXT, EGLint(buf_info.handle),
1856                 EGL_DMA_BUF_PLANE0_OFFSET_EXT, EGLint(surf->surface_image.offsets[1]),
1857                 EGL_DMA_BUF_PLANE0_PITCH_EXT, EGLint(surf->surface_image.pitches[1]),
1858                 EGL_NONE
1859         };
1860
1861         surf->cbcr_egl_image = eglCreateImageKHR(eglGetCurrentDisplay(), EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, NULL, cbcr_attribs);
1862         assert(surf->cbcr_egl_image != EGL_NO_IMAGE_KHR);
1863
1864         // Associate CbCr image to a texture.
1865         glBindTexture(GL_TEXTURE_2D, *cbcr_tex);
1866         glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, surf->cbcr_egl_image);
1867
1868         return true;
1869 }
1870
1871 void H264Encoder::end_frame(RefCountedGLsync fence, const std::vector<RefCountedFrame> &input_frames)
1872 {
1873         {
1874                 unique_lock<mutex> lock(frame_queue_mutex);
1875                 pending_frames[current_storage_frame++] = PendingFrame{ fence, input_frames };
1876         }
1877         frame_queue_nonempty.notify_one();
1878 }
1879
1880 void H264Encoder::copy_thread_func()
1881 {
1882         for ( ;; ) {
1883                 PendingFrame frame;
1884                 encoding2display_order(current_frame_encoding, intra_period, intra_idr_period, ip_period,
1885                                        &current_frame_display, &current_frame_type);
1886                 if (current_frame_type == FRAME_IDR) {
1887                         numShortTerm = 0;
1888                         current_frame_num = 0;
1889                         current_IDR_display = current_frame_display;
1890                 }
1891
1892                 {
1893                         unique_lock<mutex> lock(frame_queue_mutex);
1894                         frame_queue_nonempty.wait(lock, [this]{ return copy_thread_should_quit || pending_frames.count(current_frame_display) != 0; });
1895                         if (copy_thread_should_quit) return;
1896                         frame = pending_frames[current_frame_display];
1897                         pending_frames.erase(current_frame_display);
1898                 }
1899
1900                 // Wait for the GPU to be done with the frame.
1901                 glClientWaitSync(frame.fence.get(), 0, 0);
1902
1903                 // Release back any input frames we needed to render this frame.
1904                 frame.input_frames.clear();
1905
1906                 // Unmap the image.
1907                 GLSurface *surf = &gl_surfaces[current_frame_display % SURFACE_NUM];
1908                 eglDestroyImageKHR(eglGetCurrentDisplay(), surf->y_egl_image);
1909                 eglDestroyImageKHR(eglGetCurrentDisplay(), surf->cbcr_egl_image);
1910                 VAStatus va_status = vaReleaseBufferHandle(va_dpy, surf->surface_image.buf);
1911                 CHECK_VASTATUS(va_status, "vaReleaseBufferHandle");
1912                 va_status = vaDestroyImage(va_dpy, surf->surface_image.image_id);
1913                 CHECK_VASTATUS(va_status, "vaDestroyImage");
1914
1915                 VASurfaceID surface = surf->src_surface;
1916
1917                 // Schedule the frame for encoding.
1918                 va_status = vaBeginPicture(va_dpy, context_id, surface);
1919                 CHECK_VASTATUS(va_status, "vaBeginPicture");
1920
1921                 if (current_frame_type == FRAME_IDR) {
1922                         render_sequence();
1923                         render_picture();            
1924                         if (h264_packedheader) {
1925                                 render_packedsequence();
1926                                 render_packedpicture();
1927                         }
1928                 } else {
1929                         //render_sequence();
1930                         render_picture();
1931                 }
1932                 render_slice();
1933                 
1934                 va_status = vaEndPicture(va_dpy, context_id);
1935                 CHECK_VASTATUS(va_status, "vaEndPicture");
1936
1937                 // so now the data is done encoding (well, async job kicked off)...
1938                 // we send that to the storage thread
1939                 storage_task_enqueue(current_frame_display, current_frame_encoding, current_frame_type);
1940                 
1941                 update_ReferenceFrames();
1942                 ++current_frame_encoding;
1943         }
1944 }