2 * Copyright © 2010 Intel Corporation
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:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
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.
25 * Zhou Chang <chang.zhou@intel.com>
34 #include "intel_batchbuffer.h"
35 #include "intel_driver.h"
37 #include "i965_defines.h"
38 #include "i965_drv_video.h"
39 #include "i965_encoder.h"
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);
48 intel_encoder_check_yuv_surface(VADriverContextP ctx,
50 struct encode_state *encode_state,
51 struct intel_encoder_context *encoder_context)
53 struct i965_driver_data *i965 = i965_driver_data(ctx);
54 struct i965_surface src_surface, dst_surface;
55 struct object_surface *obj_surface;
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;
65 encoder_context->is_tmp_id = 0;
66 obj_surface = SURFACE(encode_state->current_render_target);
67 assert(obj_surface && obj_surface->bo);
69 if (!obj_surface || !obj_surface->bo)
70 return VA_STATUS_ERROR_INVALID_PARAMETER;
72 if (obj_surface->fourcc == VA_FOURCC_NV12) {
73 unsigned int tiling = 0, swizzle = 0;
75 dri_bo_get_tiling(obj_surface->bo, &tiling, &swizzle);
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;
86 rect.width = obj_surface->orig_width;
87 rect.height = obj_surface->orig_height;
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;
93 status = i965_CreateSurfaces(ctx,
94 obj_surface->orig_width,
95 obj_surface->orig_height,
98 &encoder_context->input_yuv_surface);
99 assert(status == VA_STATUS_SUCCESS);
101 if (status != VA_STATUS_SUCCESS)
104 obj_surface = SURFACE(encoder_context->input_yuv_surface);
105 encode_state->input_yuv_object = obj_surface;
107 i965_check_alloc_surface_bo(ctx, obj_surface, 1, VA_FOURCC_NV12, SUBSAMPLE_YUV420);
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;
113 status = i965_image_processing(ctx,
118 assert(status == VA_STATUS_SUCCESS);
120 encoder_context->is_tmp_id = 1;
122 return VA_STATUS_SUCCESS;
126 intel_encoder_check_avc_parameter(VADriverContextP ctx,
127 struct encode_state *encode_state,
128 struct intel_encoder_context *encoder_context)
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;
136 assert(!(pic_param->CurrPic.flags & VA_PICTURE_H264_INVALID));
138 if (pic_param->CurrPic.flags & VA_PICTURE_H264_INVALID)
141 obj_surface = SURFACE(pic_param->CurrPic.picture_id);
142 assert(obj_surface); /* It is possible the store buffer isn't allocated yet */
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);
151 if (!obj_buffer || !obj_buffer->buffer_store || !obj_buffer->buffer_store->bo)
154 encode_state->coded_buf_object = obj_buffer;
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)
161 obj_surface = SURFACE(pic_param->ReferenceFrames[i].picture_id);
168 encode_state->reference_objects[i] = obj_surface;
170 encode_state->reference_objects[i] = NULL; /* FIXME: Warning or Error ??? */
175 encode_state->reference_objects[i] = NULL;
177 return VA_STATUS_SUCCESS;
180 return VA_STATUS_ERROR_INVALID_PARAMETER;
184 intel_encoder_check_mpeg2_parameter(VADriverContextP ctx,
185 struct encode_state *encode_state,
186 struct intel_encoder_context *encoder_context)
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;
194 obj_surface = SURFACE(pic_param->reconstructed_picture);
195 assert(obj_surface); /* It is possible the store buffer isn't allocated yet */
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);
204 if (!obj_buffer || !obj_buffer->buffer_store || !obj_buffer->buffer_store->bo)
207 encode_state->coded_buf_object = obj_buffer;
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);
215 if (!obj_surface || !obj_surface->bo)
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);
224 if (!obj_surface || !obj_surface->bo)
227 encode_state->reference_objects[i++] = obj_surface;
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);
233 if (!obj_surface || !obj_surface->bo)
236 encode_state->reference_objects[i++] = obj_surface;
241 encode_state->reference_objects[i] = NULL;
243 return VA_STATUS_SUCCESS;
246 return VA_STATUS_ERROR_INVALID_PARAMETER;
250 intel_encoder_sanity_check_input(VADriverContextP ctx,
252 struct encode_state *encode_state,
253 struct intel_encoder_context *encoder_context)
258 case VAProfileH264ConstrainedBaseline:
259 case VAProfileH264Main:
260 case VAProfileH264High:
261 vaStatus = intel_encoder_check_avc_parameter(ctx, encode_state, encoder_context);
264 case VAProfileMPEG2Simple:
265 case VAProfileMPEG2Main:
266 vaStatus = intel_encoder_check_mpeg2_parameter(ctx, encode_state, encoder_context);
270 vaStatus = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
274 if (vaStatus != VA_STATUS_SUCCESS)
277 vaStatus = intel_encoder_check_yuv_surface(ctx, profile, encode_state, encoder_context);
284 intel_encoder_end_picture(VADriverContextP ctx,
286 union codec_state *codec_state,
287 struct hw_context *hw_context)
289 struct intel_encoder_context *encoder_context = (struct intel_encoder_context *)hw_context;
290 struct encode_state *encode_state = &codec_state->encode;
293 vaStatus = intel_encoder_sanity_check_input(ctx, profile, encode_state, encoder_context);
295 if (vaStatus != VA_STATUS_SUCCESS)
298 encoder_context->mfc_brc_prepare(encode_state, encoder_context);
300 vaStatus = encoder_context->vme_pipeline(ctx, profile, encode_state, encoder_context);
302 if (vaStatus == VA_STATUS_SUCCESS)
303 encoder_context->mfc_pipeline(ctx, profile, encode_state, encoder_context);
304 return VA_STATUS_SUCCESS;
308 intel_encoder_context_destroy(void *hw_context)
310 struct intel_encoder_context *encoder_context = (struct intel_encoder_context *)hw_context;
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);
318 typedef Bool (* hw_init_func)(VADriverContextP, struct intel_encoder_context *);
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)
326 struct intel_driver_data *intel = intel_driver_data(ctx);
327 struct intel_encoder_context *encoder_context = calloc(1, sizeof(struct intel_encoder_context));
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;
337 switch (obj_config->profile) {
338 case VAProfileMPEG2Simple:
339 case VAProfileMPEG2Main:
340 encoder_context->codec = CODEC_MPEG2;
343 case VAProfileH264ConstrainedBaseline:
344 case VAProfileH264Main:
345 case VAProfileH264High:
346 encoder_context->codec = CODEC_H264;
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;
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;
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);
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);
379 return (struct hw_context *)encoder_context;
383 gen6_enc_hw_context_init(VADriverContextP ctx, struct object_config *obj_config)
385 return intel_enc_hw_context_init(ctx, obj_config, gen6_vme_context_init, gen6_mfc_context_init);
389 gen7_enc_hw_context_init(VADriverContextP ctx, struct object_config *obj_config)
391 return intel_enc_hw_context_init(ctx, obj_config, gen7_vme_context_init, gen7_mfc_context_init);
395 gen75_enc_hw_context_init(VADriverContextP ctx, struct object_config *obj_config)
397 return intel_enc_hw_context_init(ctx, obj_config, gen75_vme_context_init, gen75_mfc_context_init);
401 gen8_enc_hw_context_init(VADriverContextP ctx, struct object_config *obj_config)
403 return intel_enc_hw_context_init(ctx, obj_config, gen8_vme_context_init, gen8_mfc_context_init);