summaryrefslogtreecommitdiff
path: root/vaenc/va_encode.h
blob: 6b1e2549e082ca9c6bc426b18fc55a813d4a879e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * Copyright (c) 2007-2013 Intel Corporation. All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *    Chang, Ying<ying.chang@intel.com>
 *    Lin, Edward <edward.lin@intel.com>
 *    Liu, Bolun<bolun.liu@intel.com>
 *    Sun, Jing <jing.a.sun@intel.com>
 *    Wang, Elaine<elaine.wang@intel.com>
 *    Yuan, Shengquan<shengquan.yuan@intel.com>
 *    Zhang, Zhangfei<zhangfei.zhang@intel.com>
 */ 

#include "sysdeps.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include <va/va.h>
#include "../utils/configrc.h"

#define MIN(a, b) ((a)>(b)?(b):(a))
#define MAX(a, b) ((a)>(b)?(a):(b))

#define CHECK_VASTATUS(va_status,func)                                  \
    if (va_status != VA_STATUS_SUCCESS) {                               \
        fprintf(stderr,"%s:%s (%d) failed with %s,exit\n", __func__, func, __LINE__, vaErrorStr(va_status)); \
        exit(1);                                                        \
    }

#define CHECK_VASTATUS_NOEXIT(va_status,func)                           \
    if (va_status != VA_STATUS_SUCCESS) {                               \
        fprintf(stderr,"%s:%s (%d) failed with %s,but continue\n", __func__, func, __LINE__, vaErrorStr(va_status)); \
    }

#define FRAME_P 0
#define FRAME_B 1
#define FRAME_I 2
#define FRAME_IDR 7

#define SURFACE_TYPE_VA          1
#define SURFACE_TYPE_USER_PTR    4
#define SURFACE_TYPE_GRALLOC     0x00100000
#define SURFACE_TYPE_GRALLOC_LINEAR     0x00200000

#define SURFACE_NUM 16 /* 16 surfaces for source YUV 
                        * 16 surfaces for reference
                        */
#define JPEG_SURFACE_NUM 1 /* For 8MP JPEG encoding, 16*8M*1.5B is too large to be allocated */
#define JPEG_MAX_CODED_BUFFER_SIZE(width, height) ((((((width)+15)/16)*(((height)+15)/16)*1170/4+1024)+0xf)&~0xf)

#define current_slot (current_frame_display % surface_num)

#define VAEntrypointMax 11

extern VADisplay va_dpy;
extern VAProfile va_profile;
extern char *va_profile_str;
extern char *va_level_str;
extern VAEntrypoint va_entrypoint;
extern VAConfigAttrib config_attrib[VAConfigAttribTypeMax];
extern unsigned int config_attrib_num;
extern unsigned int surface_num;
extern unsigned int ref_surface_num;
extern unsigned int carc;
extern VASurfaceID src_surface[SURFACE_NUM];
extern VABufferID  coded_buf[SURFACE_NUM];
extern VASurfaceID ref_surface[SURFACE_NUM];
extern VAConfigID config_id;
extern VAContextID context_id;

extern unsigned int frame_width;
extern unsigned int frame_height;
extern unsigned int frame_width_mbaligned;
extern unsigned int frame_height_mbaligned;
extern unsigned int frame_width_aligned;
extern unsigned int frame_height_aligned;
extern unsigned int frame_rate;
extern unsigned int frame_bitrate;
extern unsigned int frame_count;
extern unsigned int frame_coded;
extern unsigned int frame_slice_count;
extern unsigned int frame_slice_height;
extern unsigned int codedbuf_size;
extern double frame_size;
extern unsigned int initial_qp;
extern unsigned int minimal_qp;
extern unsigned int slice_qp;
extern unsigned int intra_period;
extern unsigned int intra_idr_period;
extern unsigned int ip_period;
extern unsigned int rc_mode;

extern unsigned int vp8_error_resilient;
extern unsigned int kf_auto;
extern unsigned int kf_min_dist;
extern unsigned int kf_max_dist;
extern unsigned int max_frame_size;
extern unsigned int maximum_qp;
extern unsigned int maximum_qp_vp8;
extern unsigned int bit_stuffing_dis;
extern unsigned int write_rec;
extern unsigned int first_frame_width;
extern unsigned int first_frame_height;

extern unsigned int current_frame_encoding;
extern unsigned int current_frame_display;
extern unsigned int current_IDR_display;
extern unsigned int current_frame_num;
extern unsigned int current_frame_type;

extern unsigned int misc_priv_type;
extern unsigned int misc_priv_value;

extern unsigned int surface_type;

/* h264 specific variables */
extern unsigned int h264_entropy_mode; /* cabac */
extern unsigned int h264_8x8_dct;
extern unsigned int layer_num;

extern unsigned int jpeg_quality;

extern unsigned int encode_syncmode;

extern unsigned int hrd_buffer_size;

/* For Adaptive Intra Refresh */
extern unsigned int enableAIR;
extern unsigned int num_air_mbs;
extern unsigned int airthreshold;
extern unsigned int autotune_air;

/* For Cyclic Intra Refresh */
extern unsigned int num_cir_mbs;

extern unsigned int max_slice_size;

extern FILE* frame_size_log_fp;

extern unsigned int ivf_file;
extern unsigned int riff_file;
extern unsigned int correctness_test;
extern unsigned int bitrate_layer0;
extern unsigned int bitrate_layer1;
extern unsigned int bitrate_layer2;
extern unsigned int windows_size;
extern int pm_active;
extern int buf_destroy;

extern unsigned int roi_enable;
extern int roi_value;
extern int roi_minqp;
extern int roi_maxqp;
extern unsigned int roi_rec_x;
extern unsigned int roi_rec_y;
extern unsigned int roi_rec_w;
extern unsigned int roi_rec_h;
extern unsigned int target_percentage;

int access_statitics(void *p, int encoder_order);
int print_statitics(int frame_number, int width, int height);

#ifdef ANDROID
#include <va/va_android.h>

#ifndef __cplusplus
int alloc_gralloc_buffer(int num_buffers, unsigned long *buffers,
                         int width, int height, int linear);
#endif

#else
static inline int alloc_gralloc_buffer(int num_buffers, unsigned long *buffers,
                         int width, int height, int linear)
{
    printf("Shoudnt run into here:%s\n", __func__);
    exit(1);
}
#endif

static inline const char *fourcc_to_string(int fourcc)
{
    switch (fourcc) {
    case VA_FOURCC_NV12:
        return "NV12";
    case VA_FOURCC_IYUV:
        return "IYUV";
    case VA_FOURCC_YV12:
        return "YV12";
    case VA_FOURCC_UYVY:
        return "UYVY";
    default:
        return "Unknown";
    }
}

static inline int string_to_fourcc(char *str)
{
    int fourcc;
    
    if (!strncmp(str, "NV12", 4))
        fourcc = VA_FOURCC_NV12;
    else if (!strncmp(str, "IYUV", 4))
        fourcc = VA_FOURCC_IYUV;
    else if (!strncmp(str, "YV12", 4))
        fourcc = VA_FOURCC_YV12;
    else if (!strncmp(str, "UYVY", 4))
        fourcc = VA_FOURCC_UYVY;
    else {
        printf("Unknow FOURCC\n");
        fourcc = -1;
    }
    return fourcc;
}

static inline const char *rc_to_string(int rcmode)
{
    switch (rc_mode) {
    case VA_RC_NONE:
        return "NONE";
    case VA_RC_CBR:
        return "CBR";
    case VA_RC_VBR:
        return "VBR";
    case VA_RC_VCM:
        return "VCM";
    case VA_RC_CQP:
        return "CQP";
    case VA_RC_VBR_CONSTRAINED:
        return "VBR_CONSTRAINED";
    default:
        return "Unknown";
    }
}

static inline int get_number(unsigned int numbers[], char *string)
{
    char *endptr = string;
    char *nptr = string;
    int i = 0;
    
    while (1) {
        numbers[i] = strtol(nptr, &endptr, 0);

        if (numbers[i] ==0 && endptr == nptr)
            break;
        nptr = endptr + 1;
        i++;
    }
    
    return i;
}

static inline int string_to_rc(char *str)
{
    int rc_mode;
    
    if (!strncmp(str, "NONE", 4))
        rc_mode = VA_RC_NONE;
    else if (!strncmp(str, "CBR", 3))
        rc_mode = VA_RC_CBR;
    else if (!strncmp(str, "VBR", 3))
        rc_mode = VA_RC_VBR;
    else if (!strncmp(str, "VCM", 3))
        rc_mode = VA_RC_VCM;
    else if (!strncmp(str, "CQP", 3))
        rc_mode = VA_RC_CQP;
    else if (!strncmp(str, "VBR_CONSTRAINED", 15))
        rc_mode = VA_RC_VBR_CONSTRAINED;
    else {
        printf("Unknown RC mode\n");
        rc_mode = -1;
    }
    return rc_mode;
}

int destroy_vabuffers(VADisplay dpy, int buf_num, ...);
VAStatus create_surfaces();
VAStatus destroy_surfaces();
VAStatus get_surface_attrib();


struct codec_table_t {
    VAStatus (*get_config)(void);
    VAStatus (*render_frame)(unsigned int mask, struct rc_param_t *rc_param);
    int (*save_codeddata)(FILE *coded_fp, VACodedBufferSegment *buf_list, VASurfaceID *rec_surfaceid);
};
extern struct codec_table_t h264_codec;
extern struct codec_table_t mpeg4_codec;
extern struct codec_table_t h263_codec;
extern struct codec_table_t vp8_codec;
extern struct codec_table_t jpeg_codec;
static struct codec_table_t *current_codec = &h264_codec;