OSDN Git Service

Define i965_CreateSurfaces in header file explicitly to avoid multiple declaration
[android-x86/hardware-intel-common-vaapi.git] / src / i965_encoder.c
1 /*
2  * Copyright © 2010 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sub license, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial portions
14  * of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
19  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
20  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *    Zhou Chang <chang.zhou@intel.com>
26  *
27  */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <assert.h>
33
34 #include "intel_batchbuffer.h"
35 #include "intel_driver.h"
36
37 #include "i965_defines.h"
38 #include "i965_drv_video.h"
39 #include "i965_encoder.h"
40 #include "gen6_vme.h"
41 #include "gen6_mfc.h"
42
43 extern Bool gen6_mfc_context_init(VADriverContextP ctx, struct intel_encoder_context *encoder_context);
44 extern Bool gen6_vme_context_init(VADriverContextP ctx, struct intel_encoder_context *encoder_context);
45 extern Bool gen7_mfc_context_init(VADriverContextP ctx, struct intel_encoder_context *encoder_context);
46
47 static VAStatus
48 intel_encoder_check_yuv_surface(VADriverContextP ctx,
49                                 VAProfile profile,
50                                 struct encode_state *encode_state,
51                                 struct intel_encoder_context *encoder_context)
52 {
53     struct i965_driver_data *i965 = i965_driver_data(ctx);
54     struct i965_surface src_surface, dst_surface;
55     struct object_surface *obj_surface;
56     VAStatus status;
57     VARectangle rect;
58
59     /* releae the temporary surface */
60     if (encoder_context->is_tmp_id) {
61         i965_DestroySurfaces(ctx, &encoder_context->input_yuv_surface, 1);
62         encode_state->input_yuv_object = NULL;
63     }
64
65     encoder_context->is_tmp_id = 0;
66     obj_surface = SURFACE(encode_state->current_render_target);
67     assert(obj_surface && obj_surface->bo);
68
69     if (!obj_surface || !obj_surface->bo)
70         return VA_STATUS_ERROR_INVALID_PARAMETER;
71
72     if (obj_surface->fourcc == VA_FOURCC_NV12) {
73         unsigned int tiling = 0, swizzle = 0;
74
75         dri_bo_get_tiling(obj_surface->bo, &tiling, &swizzle);
76
77         if (tiling == I915_TILING_Y) {
78             encoder_context->input_yuv_surface = encode_state->current_render_target;
79             encode_state->input_yuv_object = obj_surface;
80             return VA_STATUS_SUCCESS;
81         }
82     }
83
84     rect.x = 0;
85     rect.y = 0;
86     rect.width = obj_surface->orig_width;
87     rect.height = obj_surface->orig_height;
88     
89     src_surface.base = (struct object_base *)obj_surface;
90     src_surface.type = I965_SURFACE_TYPE_SURFACE;
91     src_surface.flags = I965_SURFACE_FLAG_FRAME;
92     
93     status = i965_CreateSurfaces(ctx,
94                                  obj_surface->orig_width,
95                                  obj_surface->orig_height,
96                                  VA_RT_FORMAT_YUV420,
97                                  1,
98                                  &encoder_context->input_yuv_surface);
99     assert(status == VA_STATUS_SUCCESS);
100
101     if (status != VA_STATUS_SUCCESS)
102         return status;
103
104     obj_surface = SURFACE(encoder_context->input_yuv_surface);
105     encode_state->input_yuv_object = obj_surface;
106     assert(obj_surface);
107     i965_check_alloc_surface_bo(ctx, obj_surface, 1, VA_FOURCC_NV12, SUBSAMPLE_YUV420);
108     
109     dst_surface.base = (struct object_base *)obj_surface;
110     dst_surface.type = I965_SURFACE_TYPE_SURFACE;
111     dst_surface.flags = I965_SURFACE_FLAG_FRAME;
112
113     status = i965_image_processing(ctx,
114                                    &src_surface,
115                                    &rect,
116                                    &dst_surface,
117                                    &rect);
118     assert(status == VA_STATUS_SUCCESS);
119
120     encoder_context->is_tmp_id = 1;
121
122     return VA_STATUS_SUCCESS;
123 }
124
125 static VAStatus
126 intel_encoder_check_avc_parameter(VADriverContextP ctx,
127                                   struct encode_state *encode_state,
128                                   struct intel_encoder_context *encoder_context)
129 {
130     struct i965_driver_data *i965 = i965_driver_data(ctx);
131     struct object_surface *obj_surface; 
132     struct object_buffer *obj_buffer;
133     VAEncPictureParameterBufferH264 *pic_param = (VAEncPictureParameterBufferH264 *)encode_state->pic_param_ext->buffer;
134     int i;
135
136     assert(!(pic_param->CurrPic.flags & VA_PICTURE_H264_INVALID));
137
138     if (pic_param->CurrPic.flags & VA_PICTURE_H264_INVALID)
139         goto error;
140
141     obj_surface = SURFACE(pic_param->CurrPic.picture_id);
142     assert(obj_surface); /* It is possible the store buffer isn't allocated yet */
143     
144     if (!obj_surface)
145         goto error;
146
147     encode_state->reconstructed_object = obj_surface;
148     obj_buffer = BUFFER(pic_param->coded_buf);
149     assert(obj_buffer && obj_buffer->buffer_store && obj_buffer->buffer_store->bo);
150
151     if (!obj_buffer || !obj_buffer->buffer_store || !obj_buffer->buffer_store->bo)
152         goto error;
153
154     encode_state->coded_buf_object = obj_buffer;
155
156     for (i = 0; i < 16; i++) {
157         if (pic_param->ReferenceFrames[i].flags & VA_PICTURE_H264_INVALID ||
158             pic_param->ReferenceFrames[i].picture_id == VA_INVALID_SURFACE)
159             break;
160         else {
161             obj_surface = SURFACE(pic_param->ReferenceFrames[i].picture_id);
162             assert(obj_surface);
163
164             if (!obj_surface)
165                 goto error;
166
167             if (obj_surface->bo)
168                 encode_state->reference_objects[i] = obj_surface;
169             else
170                 encode_state->reference_objects[i] = NULL; /* FIXME: Warning or Error ??? */
171         }
172     }
173
174     for ( ; i < 16; i++)
175         encode_state->reference_objects[i] = NULL;
176     
177     return VA_STATUS_SUCCESS;
178
179 error:
180     return VA_STATUS_ERROR_INVALID_PARAMETER;
181 }
182
183 static VAStatus
184 intel_encoder_check_mpeg2_parameter(VADriverContextP ctx,
185                                     struct encode_state *encode_state,
186                                     struct intel_encoder_context *encoder_context)
187 {
188     struct i965_driver_data *i965 = i965_driver_data(ctx);
189     VAEncPictureParameterBufferMPEG2 *pic_param = (VAEncPictureParameterBufferMPEG2 *)encode_state->pic_param_ext->buffer;
190     struct object_surface *obj_surface; 
191     struct object_buffer *obj_buffer;
192     int i = 0;
193     
194     obj_surface = SURFACE(pic_param->reconstructed_picture);
195     assert(obj_surface); /* It is possible the store buffer isn't allocated yet */
196     
197     if (!obj_surface)
198         goto error;
199     
200     encode_state->reconstructed_object = obj_surface;    
201     obj_buffer = BUFFER(pic_param->coded_buf);
202     assert(obj_buffer && obj_buffer->buffer_store && obj_buffer->buffer_store->bo);
203
204     if (!obj_buffer || !obj_buffer->buffer_store || !obj_buffer->buffer_store->bo)
205         goto error;
206
207     encode_state->coded_buf_object = obj_buffer;
208
209     if (pic_param->picture_type == VAEncPictureTypeIntra) {
210     } else if (pic_param->picture_type == VAEncPictureTypePredictive) {
211         assert(pic_param->forward_reference_picture != VA_INVALID_SURFACE);
212         obj_surface = SURFACE(pic_param->forward_reference_picture);
213         assert(obj_surface && obj_surface->bo);
214
215         if (!obj_surface || !obj_surface->bo)
216             goto error;
217
218         encode_state->reference_objects[i++] = obj_surface;
219     } else if (pic_param->picture_type == VAEncPictureTypeBidirectional) {
220         assert(pic_param->forward_reference_picture != VA_INVALID_SURFACE);
221         obj_surface = SURFACE(pic_param->forward_reference_picture);
222         assert(obj_surface && obj_surface->bo);
223
224         if (!obj_surface || !obj_surface->bo)
225             goto error;
226
227         encode_state->reference_objects[i++] = obj_surface;
228
229         assert(pic_param->backward_reference_picture != VA_INVALID_SURFACE);
230         obj_surface = SURFACE(pic_param->backward_reference_picture);
231         assert(obj_surface && obj_surface->bo);
232
233         if (!obj_surface || !obj_surface->bo)
234             goto error;
235
236         encode_state->reference_objects[i++] = obj_surface;
237     } else 
238         goto error;
239
240     for ( ; i < 16; i++)
241         encode_state->reference_objects[i] = NULL;
242
243     return VA_STATUS_SUCCESS;
244
245 error:
246     return VA_STATUS_ERROR_INVALID_PARAMETER;
247 }
248
249 static VAStatus
250 intel_encoder_sanity_check_input(VADriverContextP ctx,
251                                  VAProfile profile,
252                                  struct encode_state *encode_state,
253                                  struct intel_encoder_context *encoder_context)
254 {
255     VAStatus vaStatus;
256
257     switch (profile) {
258     case VAProfileH264ConstrainedBaseline:
259     case VAProfileH264Main:
260     case VAProfileH264High:
261         vaStatus = intel_encoder_check_avc_parameter(ctx, encode_state, encoder_context);
262         break;
263
264     case VAProfileMPEG2Simple:
265     case VAProfileMPEG2Main:
266         vaStatus = intel_encoder_check_mpeg2_parameter(ctx, encode_state, encoder_context);
267         break;
268
269     default:
270         vaStatus = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
271         break;
272     }
273
274     if (vaStatus != VA_STATUS_SUCCESS)
275         goto out;
276
277     vaStatus = intel_encoder_check_yuv_surface(ctx, profile, encode_state, encoder_context);
278
279 out:    
280     return vaStatus;
281 }
282  
283 static VAStatus
284 intel_encoder_end_picture(VADriverContextP ctx, 
285                           VAProfile profile, 
286                           union codec_state *codec_state,
287                           struct hw_context *hw_context)
288 {
289     struct intel_encoder_context *encoder_context = (struct intel_encoder_context *)hw_context;
290     struct encode_state *encode_state = &codec_state->encode;
291     VAStatus vaStatus;
292
293     vaStatus = intel_encoder_sanity_check_input(ctx, profile, encode_state, encoder_context);
294
295     if (vaStatus != VA_STATUS_SUCCESS)
296         return vaStatus;
297
298     encoder_context->mfc_brc_prepare(encode_state, encoder_context);
299
300     vaStatus = encoder_context->vme_pipeline(ctx, profile, encode_state, encoder_context);
301
302     if (vaStatus == VA_STATUS_SUCCESS)
303         encoder_context->mfc_pipeline(ctx, profile, encode_state, encoder_context);
304     return VA_STATUS_SUCCESS;
305 }
306
307 static void
308 intel_encoder_context_destroy(void *hw_context)
309 {
310     struct intel_encoder_context *encoder_context = (struct intel_encoder_context *)hw_context;
311
312     encoder_context->mfc_context_destroy(encoder_context->mfc_context);
313     encoder_context->vme_context_destroy(encoder_context->vme_context);
314     intel_batchbuffer_free(encoder_context->base.batch);
315     free(encoder_context);
316 }
317
318 typedef Bool (* hw_init_func)(VADriverContextP, struct intel_encoder_context *);
319
320 static struct hw_context *
321 intel_enc_hw_context_init(VADriverContextP ctx,
322                           struct object_config *obj_config,
323                           hw_init_func vme_context_init,
324                           hw_init_func mfc_context_init)
325 {
326     struct intel_driver_data *intel = intel_driver_data(ctx);
327     struct intel_encoder_context *encoder_context = calloc(1, sizeof(struct intel_encoder_context));
328     int i;
329
330     encoder_context->base.destroy = intel_encoder_context_destroy;
331     encoder_context->base.run = intel_encoder_end_picture;
332     encoder_context->base.batch = intel_batchbuffer_new(intel, I915_EXEC_RENDER, 0);
333     encoder_context->input_yuv_surface = VA_INVALID_SURFACE;
334     encoder_context->is_tmp_id = 0;
335     encoder_context->rate_control_mode = VA_RC_NONE;
336
337     switch (obj_config->profile) {
338     case VAProfileMPEG2Simple:
339     case VAProfileMPEG2Main:
340         encoder_context->codec = CODEC_MPEG2;
341         break;
342         
343     case VAProfileH264ConstrainedBaseline:
344     case VAProfileH264Main:
345     case VAProfileH264High:
346         encoder_context->codec = CODEC_H264;
347         break;
348
349     default:
350         /* Never get here */
351         assert(0);
352         break;
353     }
354
355     for (i = 0; i < obj_config->num_attribs; i++) {
356         if (obj_config->attrib_list[i].type == VAConfigAttribRateControl) {
357             encoder_context->rate_control_mode = obj_config->attrib_list[i].value;
358
359             if (encoder_context->codec == CODEC_MPEG2 &&
360                 encoder_context->rate_control_mode & VA_RC_CBR) {
361                 WARN_ONCE("Don't support CBR for MPEG-2 encoding\n");
362                 encoder_context->rate_control_mode &= ~VA_RC_CBR;
363             }
364
365             break;
366         }
367     }
368
369     vme_context_init(ctx, encoder_context);
370     assert(encoder_context->vme_context);
371     assert(encoder_context->vme_context_destroy);
372     assert(encoder_context->vme_pipeline);
373
374     mfc_context_init(ctx, encoder_context);
375     assert(encoder_context->mfc_context);
376     assert(encoder_context->mfc_context_destroy);
377     assert(encoder_context->mfc_pipeline);
378
379     return (struct hw_context *)encoder_context;
380 }
381
382 struct hw_context *
383 gen6_enc_hw_context_init(VADriverContextP ctx, struct object_config *obj_config)
384 {
385     return intel_enc_hw_context_init(ctx, obj_config, gen6_vme_context_init, gen6_mfc_context_init);
386 }
387
388 struct hw_context *
389 gen7_enc_hw_context_init(VADriverContextP ctx, struct object_config *obj_config)
390 {
391     return intel_enc_hw_context_init(ctx, obj_config, gen7_vme_context_init, gen7_mfc_context_init);
392 }
393
394 struct hw_context *
395 gen75_enc_hw_context_init(VADriverContextP ctx, struct object_config *obj_config)
396 {
397     return intel_enc_hw_context_init(ctx, obj_config, gen75_vme_context_init, gen75_mfc_context_init);
398 }
399
400 struct hw_context *
401 gen8_enc_hw_context_init(VADriverContextP ctx, struct object_config *obj_config)
402 {
403     return intel_enc_hw_context_init(ctx, obj_config, gen8_vme_context_init, gen8_mfc_context_init);
404 }
405