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