i965_drv_video.c 72.2 KB
Newer Older
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
/*
 * Copyright  2009 Intel Corporation
 *
 * 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:
 *    Xiang Haihao <haihao.xiang@intel.com>
 *    Zou Nan hai <nanhai.zou@intel.com>
 *
 */

30
#include "config.h"
Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
31
#include <stdlib.h>
32 33 34
#include <string.h>
#include <assert.h>

35
#include "va/x11/va_dricommon.h"
36 37 38 39 40 41 42

#include "intel_driver.h"
#include "intel_memman.h"
#include "intel_batchbuffer.h"

#include "i965_media.h"
#include "i965_drv_video.h"
43
#include "i965_defines.h"
44
#include "i965_encoder.h"
45 46 47 48 49 50 51 52

#define CONFIG_ID_OFFSET                0x01000000
#define CONTEXT_ID_OFFSET               0x02000000
#define SURFACE_ID_OFFSET               0x04000000
#define BUFFER_ID_OFFSET                0x08000000
#define IMAGE_ID_OFFSET                 0x0a000000
#define SUBPIC_ID_OFFSET                0x10000000

53
#define HAS_MPEG2(ctx)  (IS_G4X((ctx)->intel.device_id) ||              \
54 55
                         IS_IRONLAKE((ctx)->intel.device_id) ||         \
                         (IS_GEN6((ctx)->intel.device_id) && (ctx)->intel.has_bsd))
56

57 58
#define HAS_H264(ctx)   ((IS_GEN6((ctx)->intel.device_id) ||            \
                          IS_IRONLAKE((ctx)->intel.device_id)) &&       \
59
                         (ctx)->intel.has_bsd)
60 61

#define HAS_VC1(ctx)    (IS_GEN6((ctx)->intel.device_id) && (ctx)->intel.has_bsd)
62

63
#define HAS_TILED_SURFACE(ctx) (IS_GEN6((ctx)->intel.device_id) &&      \
64
                                (ctx)->render_state.interleaved_uv)
65

66 67 68
#define HAS_ENCODER(ctx)        (IS_GEN6((ctx)->intel.device_id) &&     \
                                 (ctx)->intel.has_bsd)

69 70 71 72 73 74
enum {
    I965_SURFACETYPE_RGBA = 1,
    I965_SURFACETYPE_YUV,
    I965_SURFACETYPE_INDEXED
};

Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
75 76 77 78 79 80 81 82 83 84
/* List of supported image formats */
typedef struct {
    unsigned int        type;
    VAImageFormat       va_format;
} i965_image_format_map_t;

static const i965_image_format_map_t
i965_image_formats_map[I965_MAX_IMAGE_FORMATS + 1] = {
    { I965_SURFACETYPE_YUV,
      { VA_FOURCC('Y','V','1','2'), VA_LSB_FIRST, 12, } },
Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
85 86
    { I965_SURFACETYPE_YUV,
      { VA_FOURCC('I','4','2','0'), VA_LSB_FIRST, 12, } },
Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
87 88
    { I965_SURFACETYPE_YUV,
      { VA_FOURCC('N','V','1','2'), VA_LSB_FIRST, 12, } },
Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
89 90
};

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
/* List of supported subpicture formats */
typedef struct {
    unsigned int        type;
    unsigned int        format;
    VAImageFormat       va_format;
    unsigned int        va_flags;
} i965_subpic_format_map_t;

static const i965_subpic_format_map_t
i965_subpic_formats_map[I965_MAX_SUBPIC_FORMATS + 1] = {
    { I965_SURFACETYPE_INDEXED, I965_SURFACEFORMAT_P4A4_UNORM,
      { VA_FOURCC('I','A','4','4'), VA_MSB_FIRST, 8, },
      0 },
    { I965_SURFACETYPE_INDEXED, I965_SURFACEFORMAT_A4P4_UNORM,
      { VA_FOURCC('A','I','4','4'), VA_MSB_FIRST, 8, },
      0 },
Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
107 108 109 110 111 112 113 114
    { I965_SURFACETYPE_RGBA, I965_SURFACEFORMAT_B8G8R8A8_UNORM,
      { VA_FOURCC('B','G','R','A'), VA_LSB_FIRST, 32,
        32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 },
      0 },
    { I965_SURFACETYPE_RGBA, I965_SURFACEFORMAT_R8G8B8A8_UNORM,
      { VA_FOURCC('R','G','B','A'), VA_LSB_FIRST, 32,
        32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 },
      0 },
115 116 117 118 119 120
};

static const i965_subpic_format_map_t *
get_subpic_format(const VAImageFormat *va_format)
{
    unsigned int i;
121
    for (i = 0; i965_subpic_formats_map[i].type != 0; i++) {
122 123 124 125 126 127 128 129 130 131 132 133 134
        const i965_subpic_format_map_t * const m = &i965_subpic_formats_map[i];
        if (m->va_format.fourcc == va_format->fourcc &&
            (m->type == I965_SURFACETYPE_RGBA ?
             (m->va_format.byte_order == va_format->byte_order &&
              m->va_format.red_mask   == va_format->red_mask   &&
              m->va_format.green_mask == va_format->green_mask &&
              m->va_format.blue_mask  == va_format->blue_mask  &&
              m->va_format.alpha_mask == va_format->alpha_mask) : 1))
            return m;
    }
    return NULL;
}

135 136 137 138 139
VAStatus 
i965_QueryConfigProfiles(VADriverContextP ctx,
                         VAProfile *profile_list,       /* out */
                         int *num_profiles)             /* out */
{
140
    struct i965_driver_data * const i965 = i965_driver_data(ctx);
141 142
    int i = 0;

143 144 145 146
    if (HAS_MPEG2(i965)) {
        profile_list[i++] = VAProfileMPEG2Simple;
        profile_list[i++] = VAProfileMPEG2Main;
    }
147 148 149 150 151 152

    if (HAS_H264(i965)) {
        profile_list[i++] = VAProfileH264Baseline;
        profile_list[i++] = VAProfileH264Main;
        profile_list[i++] = VAProfileH264High;
    }
153

154
    if (HAS_VC1(i965)) {
155 156 157 158 159
        profile_list[i++] = VAProfileVC1Simple;
        profile_list[i++] = VAProfileVC1Main;
        profile_list[i++] = VAProfileVC1Advanced;
    }

160 161 162 163 164 165 166 167 168 169 170 171 172
    /* If the assert fails then I965_MAX_PROFILES needs to be bigger */
    assert(i <= I965_MAX_PROFILES);
    *num_profiles = i;

    return VA_STATUS_SUCCESS;
}

VAStatus 
i965_QueryConfigEntrypoints(VADriverContextP ctx,
                            VAProfile profile,
                            VAEntrypoint *entrypoint_list,      /* out */
                            int *num_entrypoints)               /* out */
{
173 174
    struct i965_driver_data * const i965 = i965_driver_data(ctx);
    int n = 0;
175

176 177 178
    switch (profile) {
    case VAProfileMPEG2Simple:
    case VAProfileMPEG2Main:
179 180
        if (HAS_MPEG2(i965))
            entrypoint_list[n++] = VAEntrypointVLD;
181 182
        break;

183 184 185
    case VAProfileH264Baseline:
    case VAProfileH264Main:
    case VAProfileH264High:
186 187
        if (HAS_H264(i965))
            entrypoint_list[n++] = VAEntrypointVLD;
188 189 190 191
        
        if (HAS_ENCODER(i965))
            entrypoint_list[n++] = VAEntrypointEncSlice;

192 193
        break;

194 195 196
    case VAProfileVC1Simple:
    case VAProfileVC1Main:
    case VAProfileVC1Advanced:
197 198
        if (HAS_VC1(i965))
            entrypoint_list[n++] = VAEntrypointVLD;
199 200
        break;

201 202 203 204 205
    default:
        break;
    }

    /* If the assert fails then I965_MAX_ENTRYPOINTS needs to be bigger */
206 207 208
    assert(n <= I965_MAX_ENTRYPOINTS);
    *num_entrypoints = n;
    return n > 0 ? VA_STATUS_SUCCESS : VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
}

VAStatus 
i965_GetConfigAttributes(VADriverContextP ctx,
                         VAProfile profile,
                         VAEntrypoint entrypoint,
                         VAConfigAttrib *attrib_list,  /* in/out */
                         int num_attribs)
{
    int i;

    /* Other attributes don't seem to be defined */
    /* What to do if we don't know the attribute? */
    for (i = 0; i < num_attribs; i++) {
        switch (attrib_list[i].type) {
        case VAConfigAttribRTFormat:
            attrib_list[i].value = VA_RT_FORMAT_YUV420;
            break;

228 229 230 231
        case VAConfigAttribRateControl:
            attrib_list[i].value = VA_RC_VBR;
            break;

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
        default:
            /* Do nothing */
            attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
            break;
        }
    }

    return VA_STATUS_SUCCESS;
}

static void 
i965_destroy_config(struct object_heap *heap, struct object_base *obj)
{
    object_heap_free(heap, obj);
}

static VAStatus 
i965_update_attribute(struct object_config *obj_config, VAConfigAttrib *attrib)
{
    int i;

    /* Check existing attrbiutes */
    for (i = 0; obj_config->num_attribs < i; i++) {
        if (obj_config->attrib_list[i].type == attrib->type) {
            /* Update existing attribute */
            obj_config->attrib_list[i].value = attrib->value;
            return VA_STATUS_SUCCESS;
        }
    }

    if (obj_config->num_attribs < I965_MAX_CONFIG_ATTRIBUTES) {
        i = obj_config->num_attribs;
        obj_config->attrib_list[i].type = attrib->type;
        obj_config->attrib_list[i].value = attrib->value;
        obj_config->num_attribs++;
        return VA_STATUS_SUCCESS;
    }

    return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
}

VAStatus 
i965_CreateConfig(VADriverContextP ctx,
                  VAProfile profile,
                  VAEntrypoint entrypoint,
                  VAConfigAttrib *attrib_list,
                  int num_attribs,
                  VAConfigID *config_id)		/* out */
{
281
    struct i965_driver_data * const i965 = i965_driver_data(ctx);
282 283 284 285 286 287 288 289 290
    struct object_config *obj_config;
    int configID;
    int i;
    VAStatus vaStatus;

    /* Validate profile & entrypoint */
    switch (profile) {
    case VAProfileMPEG2Simple:
    case VAProfileMPEG2Main:
291
        if (HAS_MPEG2(i965) && VAEntrypointVLD == entrypoint) {
292 293 294 295 296 297
            vaStatus = VA_STATUS_SUCCESS;
        } else {
            vaStatus = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
        }
        break;

298 299 300
    case VAProfileH264Baseline:
    case VAProfileH264Main:
    case VAProfileH264High:
301 302
        if ((HAS_H264(i965) && VAEntrypointVLD == entrypoint) ||
            (HAS_ENCODER(i965) && VAEntrypointEncSlice == entrypoint)) {
303 304 305 306 307 308 309
            vaStatus = VA_STATUS_SUCCESS;
        } else {
            vaStatus = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
        }

        break;

310 311 312
    case VAProfileVC1Simple:
    case VAProfileVC1Main:
    case VAProfileVC1Advanced:
313
        if (HAS_VC1(i965) && VAEntrypointVLD == entrypoint) {
314 315 316 317 318 319 320
            vaStatus = VA_STATUS_SUCCESS;
        } else {
            vaStatus = VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
        }

        break;

321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
    default:
        vaStatus = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
        break;
    }

    if (VA_STATUS_SUCCESS != vaStatus) {
        return vaStatus;
    }

    configID = NEW_CONFIG_ID();
    obj_config = CONFIG(configID);

    if (NULL == obj_config) {
        vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
        return vaStatus;
    }

    obj_config->profile = profile;
    obj_config->entrypoint = entrypoint;
    obj_config->attrib_list[0].type = VAConfigAttribRTFormat;
    obj_config->attrib_list[0].value = VA_RT_FORMAT_YUV420;
    obj_config->num_attribs = 1;

    for(i = 0; i < num_attribs; i++) {
        vaStatus = i965_update_attribute(obj_config, &(attrib_list[i]));

        if (VA_STATUS_SUCCESS != vaStatus) {
            break;
        }
    }

    /* Error recovery */
    if (VA_STATUS_SUCCESS != vaStatus) {
        i965_destroy_config(&i965->config_heap, (struct object_base *)obj_config);
    } else {
        *config_id = configID;
    }

    return vaStatus;
}

VAStatus 
i965_DestroyConfig(VADriverContextP ctx, VAConfigID config_id)
{
365
    struct intel_driver_data * const intel = intel_driver_data(ctx);
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
    struct i965_driver_data *i965 = i965_driver_data(ctx);
    struct object_config *obj_config = CONFIG(config_id);
    VAStatus vaStatus;

    if (NULL == obj_config) {
        vaStatus = VA_STATUS_ERROR_INVALID_CONFIG;
        return vaStatus;
    }

    i965_destroy_config(&i965->config_heap, (struct object_base *)obj_config);
    return VA_STATUS_SUCCESS;
}

VAStatus i965_QueryConfigAttributes(VADriverContextP ctx,
                                    VAConfigID config_id,
                                    VAProfile *profile,                 /* out */
                                    VAEntrypoint *entrypoint,           /* out */
                                    VAConfigAttrib *attrib_list,        /* out */
                                    int *num_attribs)                   /* out */
{
    struct i965_driver_data *i965 = i965_driver_data(ctx);
    struct object_config *obj_config = CONFIG(config_id);
    VAStatus vaStatus = VA_STATUS_SUCCESS;
    int i;

    assert(obj_config);
    *profile = obj_config->profile;
    *entrypoint = obj_config->entrypoint;
    *num_attribs = obj_config->num_attribs;

    for(i = 0; i < obj_config->num_attribs; i++) {
        attrib_list[i] = obj_config->attrib_list[i];
    }

    return vaStatus;
}

static void 
i965_destroy_surface(struct object_heap *heap, struct object_base *obj)
{
    struct object_surface *obj_surface = (struct object_surface *)obj;

    dri_bo_unreference(obj_surface->bo);
    obj_surface->bo = NULL;
410 411
    dri_bo_unreference(obj_surface->pp_out_bo);
    obj_surface->pp_out_bo = NULL;
412 413 414 415 416 417

    if (obj_surface->free_private_data != NULL) {
        obj_surface->free_private_data(&obj_surface->private_data);
        obj_surface->private_data = NULL;
    }

418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
    object_heap_free(heap, obj);
}

VAStatus 
i965_CreateSurfaces(VADriverContextP ctx,
                    int width,
                    int height,
                    int format,
                    int num_surfaces,
                    VASurfaceID *surfaces)      /* out */
{
    struct i965_driver_data *i965 = i965_driver_data(ctx);
    int i;
    VAStatus vaStatus = VA_STATUS_SUCCESS;

    /* We only support one format */
    if (VA_RT_FORMAT_YUV420 != format) {
        return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
    }

    for (i = 0; i < num_surfaces; i++) {
        int surfaceID = NEW_SURFACE_ID();
        struct object_surface *obj_surface = SURFACE(surfaceID);

        if (NULL == obj_surface) {
            vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
            break;
        }

        surfaces[i] = surfaceID;
        obj_surface->status = VASurfaceReady;
449
        obj_surface->subpic = VA_INVALID_ID;
450 451
        obj_surface->orig_width = width;
        obj_surface->orig_height = height;
452

453
        if (IS_GEN6(i965->intel.device_id)) {
454 455 456 457 458 459 460
            obj_surface->width = ALIGN(obj_surface->orig_width, 128);
            obj_surface->height = ALIGN(obj_surface->orig_height, 32);
        } else {
            obj_surface->width = ALIGN(obj_surface->orig_width, 16);
            obj_surface->height = ALIGN(obj_surface->orig_height, 16);
        }

461
        obj_surface->size = SIZE_YUV420(obj_surface->width, obj_surface->height);
462 463
        obj_surface->flags = SURFACE_REFERENCED;
        obj_surface->bo = NULL;
464
        obj_surface->pp_out_bo = NULL;
465 466
        obj_surface->private_data = NULL;
        obj_surface->free_private_data = NULL;
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
    }

    /* Error recovery */
    if (VA_STATUS_SUCCESS != vaStatus) {
        /* surfaces[i-1] was the last successful allocation */
        for (; i--; ) {
            struct object_surface *obj_surface = SURFACE(surfaces[i]);

            surfaces[i] = VA_INVALID_SURFACE;
            assert(obj_surface);
            i965_destroy_surface(&i965->surface_heap, (struct object_base *)obj_surface);
        }
    }

    return vaStatus;
}

VAStatus 
i965_DestroySurfaces(VADriverContextP ctx,
                     VASurfaceID *surface_list,
                     int num_surfaces)
{
    struct i965_driver_data *i965 = i965_driver_data(ctx);
    int i;

    for (i = num_surfaces; i--; ) {
        struct object_surface *obj_surface = SURFACE(surface_list[i]);

        assert(obj_surface);
        i965_destroy_surface(&i965->surface_heap, (struct object_base *)obj_surface);
    }

    return VA_STATUS_SUCCESS;
}

VAStatus 
i965_QueryImageFormats(VADriverContextP ctx,
                       VAImageFormat *format_list,      /* out */
                       int *num_formats)                /* out */
{
Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
507 508 509 510 511 512 513 514
    int n;

    for (n = 0; i965_image_formats_map[n].va_format.fourcc != 0; n++) {
        const i965_image_format_map_t * const m = &i965_image_formats_map[n];
        if (format_list)
            format_list[n] = m->va_format;
    }

515
    if (num_formats)
Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
516
        *num_formats = n;
517

518 519 520 521
    return VA_STATUS_SUCCESS;
}

VAStatus 
522
i965_PutImage(VADriverContextP ctx,
523 524 525 526 527 528 529 530 531 532
              VASurfaceID surface,
              VAImageID image,
              int src_x,
              int src_y,
              unsigned int src_width,
              unsigned int src_height,
              int dest_x,
              int dest_y,
              unsigned int dest_width,
              unsigned int dest_height)
533 534 535 536 537 538 539 540 541 542
{
    return VA_STATUS_SUCCESS;
}

VAStatus 
i965_QuerySubpictureFormats(VADriverContextP ctx,
                            VAImageFormat *format_list,         /* out */
                            unsigned int *flags,                /* out */
                            unsigned int *num_formats)          /* out */
{
543 544 545 546 547 548 549 550 551 552 553 554 555
    int n;

    for (n = 0; i965_subpic_formats_map[n].va_format.fourcc != 0; n++) {
        const i965_subpic_format_map_t * const m = &i965_subpic_formats_map[n];
        if (format_list)
            format_list[n] = m->va_format;
        if (flags)
            flags[n] = m->va_flags;
    }

    if (num_formats)
        *num_formats = n;

556 557 558 559 560 561
    return VA_STATUS_SUCCESS;
}

static void 
i965_destroy_subpic(struct object_heap *heap, struct object_base *obj)
{
562
    //    struct object_subpic *obj_subpic = (struct object_subpic *)obj;
563 564 565 566 567 568 569 570 571 572 573 574

    object_heap_free(heap, obj);
}

VAStatus 
i965_CreateSubpicture(VADriverContextP ctx,
                      VAImageID image,
                      VASubpictureID *subpicture)         /* out */
{
    struct i965_driver_data *i965 = i965_driver_data(ctx);
    VASubpictureID subpicID = NEW_SUBPIC_ID()
	
575
        struct object_subpic *obj_subpic = SUBPIC(subpicID);
576 577 578
    if (!obj_subpic)
        return VA_STATUS_ERROR_ALLOCATION_FAILED;

579
    struct object_image *obj_image = IMAGE(image);
580 581 582 583 584 585 586
    if (!obj_image)
        return VA_STATUS_ERROR_INVALID_IMAGE;

    const i965_subpic_format_map_t * const m = get_subpic_format(&obj_image->image.format);
    if (!m)
        return VA_STATUS_ERROR_UNKNOWN; /* XXX: VA_STATUS_ERROR_UNSUPPORTED_FORMAT? */

587
    *subpicture = subpicID;
588
    obj_subpic->image  = image;
589
    obj_subpic->format = m->format;
590 591
    obj_subpic->width  = obj_image->image.width;
    obj_subpic->height = obj_image->image.height;
Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
592
    obj_subpic->pitch  = obj_image->image.pitches[0];
593
    obj_subpic->bo     = obj_image->bo;
594
    return VA_STATUS_SUCCESS;
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
}

VAStatus 
i965_DestroySubpicture(VADriverContextP ctx,
                       VASubpictureID subpicture)
{
	
    struct i965_driver_data *i965 = i965_driver_data(ctx);
    struct object_subpic *obj_subpic = SUBPIC(subpicture);
    i965_destroy_subpic(&i965->subpic_heap, (struct object_base *)obj_subpic);
    return VA_STATUS_SUCCESS;
}

VAStatus 
i965_SetSubpictureImage(VADriverContextP ctx,
                        VASubpictureID subpicture,
                        VAImageID image)
{
613 614
    /* TODO */
    return VA_STATUS_ERROR_UNIMPLEMENTED;
615 616 617 618 619 620 621 622 623
}

VAStatus 
i965_SetSubpictureChromakey(VADriverContextP ctx,
                            VASubpictureID subpicture,
                            unsigned int chromakey_min,
                            unsigned int chromakey_max,
                            unsigned int chromakey_mask)
{
624 625
    /* TODO */
    return VA_STATUS_ERROR_UNIMPLEMENTED;
626 627 628 629 630 631 632
}

VAStatus 
i965_SetSubpictureGlobalAlpha(VADriverContextP ctx,
                              VASubpictureID subpicture,
                              float global_alpha)
{
633 634
    /* TODO */
    return VA_STATUS_ERROR_UNIMPLEMENTED;
635 636 637 638 639 640 641 642 643
}

VAStatus 
i965_AssociateSubpicture(VADriverContextP ctx,
                         VASubpictureID subpicture,
                         VASurfaceID *target_surfaces,
                         int num_surfaces,
                         short src_x, /* upper left offset in subpicture */
                         short src_y,
644 645
                         unsigned short src_width,
                         unsigned short src_height,
646 647
                         short dest_x, /* upper left offset in surface */
                         short dest_y,
648 649
                         unsigned short dest_width,
                         unsigned short dest_height,
650 651 652 653 654 655 656 657
                         /*
                          * whether to enable chroma-keying or global-alpha
                          * see VA_SUBPICTURE_XXX values
                          */
                         unsigned int flags)
{
    struct i965_driver_data *i965 = i965_driver_data(ctx);
    struct object_subpic *obj_subpic = SUBPIC(subpicture);
658
    int i;
659

660 661 662 663 664 665 666 667
    obj_subpic->src_rect.x      = src_x;
    obj_subpic->src_rect.y      = src_y;
    obj_subpic->src_rect.width  = src_width;
    obj_subpic->src_rect.height = src_height;
    obj_subpic->dst_rect.x      = dest_x;
    obj_subpic->dst_rect.y      = dest_y;
    obj_subpic->dst_rect.width  = dest_width;
    obj_subpic->dst_rect.height = dest_height;
668

669 670 671 672 673 674
    for (i = 0; i < num_surfaces; i++) {
        struct object_surface *obj_surface = SURFACE(target_surfaces[i]);
        if (!obj_surface)
            return VA_STATUS_ERROR_INVALID_SURFACE;
        obj_surface->subpic = subpicture;
    }
675 676 677 678 679 680 681 682 683 684
    return VA_STATUS_SUCCESS;
}


VAStatus 
i965_DeassociateSubpicture(VADriverContextP ctx,
                           VASubpictureID subpicture,
                           VASurfaceID *target_surfaces,
                           int num_surfaces)
{
685 686 687 688 689 690 691 692 693 694
    struct i965_driver_data *i965 = i965_driver_data(ctx);
    int i;

    for (i = 0; i < num_surfaces; i++) {
        struct object_surface *obj_surface = SURFACE(target_surfaces[i]);
        if (!obj_surface)
            return VA_STATUS_ERROR_INVALID_SURFACE;
        if (obj_surface->subpic == subpicture)
            obj_surface->subpic = VA_INVALID_ID;
    }
695 696 697
    return VA_STATUS_SUCCESS;
}

698
void
699 700 701 702 703 704 705 706 707 708 709
i965_reference_buffer_store(struct buffer_store **ptr, 
                            struct buffer_store *buffer_store)
{
    assert(*ptr == NULL);

    if (buffer_store) {
        buffer_store->ref_count++;
        *ptr = buffer_store;
    }
}

710
void 
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
i965_release_buffer_store(struct buffer_store **ptr)
{
    struct buffer_store *buffer_store = *ptr;

    if (buffer_store == NULL)
        return;

    assert(buffer_store->bo || buffer_store->buffer);
    assert(!(buffer_store->bo && buffer_store->buffer));
    buffer_store->ref_count--;
    
    if (buffer_store->ref_count == 0) {
        dri_bo_unreference(buffer_store->bo);
        free(buffer_store->buffer);
        buffer_store->bo = NULL;
        buffer_store->buffer = NULL;
        free(buffer_store);
    }

    *ptr = NULL;
}

static void 
i965_destroy_context(struct object_heap *heap, struct object_base *obj)
{
    struct object_context *obj_context = (struct object_context *)obj;
737 738 739 740
    int i;

    assert(obj_context->decode_state.num_slice_params <= obj_context->decode_state.max_slice_params);
    assert(obj_context->decode_state.num_slice_datas <= obj_context->decode_state.max_slice_datas);
741 742 743 744

    i965_release_buffer_store(&obj_context->decode_state.pic_param);
    i965_release_buffer_store(&obj_context->decode_state.iq_matrix);
    i965_release_buffer_store(&obj_context->decode_state.bit_plane);
745 746 747 748 749 750 751 752 753

    for (i = 0; i < obj_context->decode_state.num_slice_params; i++)
        i965_release_buffer_store(&obj_context->decode_state.slice_params[i]);

    for (i = 0; i < obj_context->decode_state.num_slice_datas; i++)
        i965_release_buffer_store(&obj_context->decode_state.slice_datas[i]);

    free(obj_context->decode_state.slice_params);
    free(obj_context->decode_state.slice_datas);
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
    free(obj_context->render_targets);
    object_heap_free(heap, obj);
}

VAStatus 
i965_CreateContext(VADriverContextP ctx,
                   VAConfigID config_id,
                   int picture_width,
                   int picture_height,
                   int flag,
                   VASurfaceID *render_targets,
                   int num_render_targets,
                   VAContextID *context)                /* out */
{
    struct i965_driver_data *i965 = i965_driver_data(ctx);
769
    struct i965_render_state *render_state = &i965->render_state;
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
    struct object_config *obj_config = CONFIG(config_id);
    struct object_context *obj_context = NULL;
    VAStatus vaStatus = VA_STATUS_SUCCESS;
    int contextID;
    int i;

    if (NULL == obj_config) {
        vaStatus = VA_STATUS_ERROR_INVALID_CONFIG;
        return vaStatus;
    }

    /* Validate flag */
    /* Validate picture dimensions */
    contextID = NEW_CONTEXT_ID();
    obj_context = CONTEXT(contextID);

    if (NULL == obj_context) {
        vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
        return vaStatus;
    }

791 792 793 794 795 796
    switch (obj_config->profile) {
    case VAProfileH264Baseline:
    case VAProfileH264Main:
    case VAProfileH264High:
        if (!HAS_H264(i965))
            return VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
797
        render_state->interleaved_uv = 1;
798 799 800 801
        break;
    default:
        render_state->interleaved_uv = !!IS_GEN6(i965->intel.device_id);
        break;
802 803
    }

804
    *context = contextID;
805 806
    obj_context->flags = flag;
    obj_context->context_id = contextID;
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
    obj_context->config_id = config_id;
    obj_context->picture_width = picture_width;
    obj_context->picture_height = picture_height;
    obj_context->num_render_targets = num_render_targets;
    obj_context->render_targets = 
        (VASurfaceID *)calloc(num_render_targets, sizeof(VASurfaceID));

    for(i = 0; i < num_render_targets; i++) {
        if (NULL == SURFACE(render_targets[i])) {
            vaStatus = VA_STATUS_ERROR_INVALID_SURFACE;
            break;
        }

        obj_context->render_targets[i] = render_targets[i];
    }

823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
    if (VA_STATUS_SUCCESS == vaStatus) {
        if (VAEntrypointEncSlice == obj_config->entrypoint ) { /*encode routin only*/
            memset(&obj_context->encode_state, 0, sizeof(obj_context->encode_state));
            vaStatus = i965_encoder_create_context(ctx, config_id, picture_width, picture_height, 
                                                   flag, render_targets, num_render_targets, obj_context);
        } else {
            memset(&obj_context->decode_state, 0, sizeof(obj_context->decode_state));
            obj_context->decode_state.current_render_target = -1;
            obj_context->decode_state.max_slice_params = NUM_SLICES;
            obj_context->decode_state.max_slice_datas = NUM_SLICES;
            obj_context->decode_state.slice_params = calloc(obj_context->decode_state.max_slice_params,
                                                            sizeof(*obj_context->decode_state.slice_params));
            obj_context->decode_state.slice_datas = calloc(obj_context->decode_state.max_slice_datas,
                                                           sizeof(*obj_context->decode_state.slice_datas));
        }
    }
839 840 841 842 843 844 845 846 847 848 849 850 851 852

    /* Error recovery */
    if (VA_STATUS_SUCCESS != vaStatus) {
        i965_destroy_context(&i965->context_heap, (struct object_base *)obj_context);
    }

    return vaStatus;
}

VAStatus 
i965_DestroyContext(VADriverContextP ctx, VAContextID context)
{
    struct i965_driver_data *i965 = i965_driver_data(ctx);
    struct object_context *obj_context = CONTEXT(context);
853 854
    struct object_config *obj_config;
    VAContextID config;
855 856

    assert(obj_context);
857 858 859 860 861 862 863 864 865
    config = obj_context->config_id;
    obj_config = CONFIG(config);
    assert(obj_config);

    if (VAEntrypointEncSlice == obj_config->entrypoint ){
        i965_encoder_destroy_context(&i965->context_heap, (struct object_base *)obj_context);
    } else {
        i965_destroy_context(&i965->context_heap, (struct object_base *)obj_context);
    }
866 867 868 869 870 871 872 873 874 875 876 877 878 879

    return VA_STATUS_SUCCESS;
}

static void 
i965_destroy_buffer(struct object_heap *heap, struct object_base *obj)
{
    struct object_buffer *obj_buffer = (struct object_buffer *)obj;

    assert(obj_buffer->buffer_store);
    i965_release_buffer_store(&obj_buffer->buffer_store);
    object_heap_free(heap, obj);
}

880 881 882 883 884 885 886 887 888
static VAStatus
i965_create_buffer_internal(VADriverContextP ctx,
                            VAContextID context,
                            VABufferType type,
                            unsigned int size,
                            unsigned int num_elements,
                            void *data,
                            dri_bo *store_bo,
                            VABufferID *buf_id)
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
{
    struct i965_driver_data *i965 = i965_driver_data(ctx);
    struct object_buffer *obj_buffer = NULL;
    struct buffer_store *buffer_store = NULL;
    int bufferID;

    /* Validate type */
    switch (type) {
    case VAPictureParameterBufferType:
    case VAIQMatrixBufferType:
    case VABitPlaneBufferType:
    case VASliceGroupMapBufferType:
    case VASliceParameterBufferType:
    case VASliceDataBufferType:
    case VAMacroblockParameterBufferType:
    case VAResidualDataBufferType:
    case VADeblockingParameterBufferType:
    case VAImageBufferType:
907 908 909 910
    case VAEncCodedBufferType:
    case VAEncSequenceParameterBufferType:
    case VAEncPictureParameterBufferType:
    case VAEncSliceParameterBufferType:
911 912 913 914 915 916 917 918 919 920 921 922 923 924
        /* Ok */
        break;

    default:
        return VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE;
    }

    bufferID = NEW_BUFFER_ID();
    obj_buffer = BUFFER(bufferID);

    if (NULL == obj_buffer) {
        return VA_STATUS_ERROR_ALLOCATION_FAILED;
    }

Xiang, Haihao's avatar
Xiang, Haihao committed
925 926 927 928
    if (type == VAEncCodedBufferType) {
        size += ALIGN(sizeof(VACodedBufferSegment), 64);
    }

929 930 931 932 933 934 935 936 937
    obj_buffer->max_num_elements = num_elements;
    obj_buffer->num_elements = num_elements;
    obj_buffer->size_element = size;
    obj_buffer->type = type;
    obj_buffer->buffer_store = NULL;
    buffer_store = calloc(1, sizeof(struct buffer_store));
    assert(buffer_store);
    buffer_store->ref_count = 1;

938 939 940 941 942 943
    if (store_bo != NULL) {
        buffer_store->bo = store_bo;
        dri_bo_reference(buffer_store->bo);
        
        if (data)
            dri_bo_subdata(buffer_store->bo, 0, size * num_elements, data);
944
    } else if (type == VASliceDataBufferType || type == VAImageBufferType || type == VAEncCodedBufferType) {
945
        buffer_store->bo = dri_bo_alloc(i965->intel.bufmgr, 
946 947
                                        "Buffer", 
                                        size * num_elements, 64);
948 949
        assert(buffer_store->bo);

Xiang, Haihao's avatar
Xiang, Haihao committed
950 951 952 953 954 955 956 957 958 959 960
        if (type == VAEncCodedBufferType) {
            VACodedBufferSegment *coded_buffer_segment;
            dri_bo_map(buffer_store->bo, 1);
            coded_buffer_segment = (VACodedBufferSegment *)buffer_store->bo->virtual;
            coded_buffer_segment->size = size - ALIGN(sizeof(VACodedBufferSegment), 64);
            coded_buffer_segment->bit_offset = 0;
            coded_buffer_segment->status = 0;
            coded_buffer_segment->buf = NULL;
            coded_buffer_segment->next = NULL;
            dri_bo_unmap(buffer_store->bo);
        } else if (data) {
961
            dri_bo_subdata(buffer_store->bo, 0, size * num_elements, data);
Xiang, Haihao's avatar
Xiang, Haihao committed
962 963
        }

964 965 966 967 968 969 970 971
    } else {
        buffer_store->buffer = malloc(size * num_elements);
        assert(buffer_store->buffer);

        if (data)
            memcpy(buffer_store->buffer, data, size * num_elements);
    }

972
    buffer_store->num_elements = obj_buffer->num_elements;
973 974 975 976 977 978 979
    i965_reference_buffer_store(&obj_buffer->buffer_store, buffer_store);
    i965_release_buffer_store(&buffer_store);
    *buf_id = bufferID;

    return VA_STATUS_SUCCESS;
}

980 981 982 983 984 985 986 987 988 989 990 991
VAStatus 
i965_CreateBuffer(VADriverContextP ctx,
                  VAContextID context,          /* in */
                  VABufferType type,            /* in */
                  unsigned int size,            /* in */
                  unsigned int num_elements,    /* in */
                  void *data,                   /* in */
                  VABufferID *buf_id)           /* out */
{
    return i965_create_buffer_internal(ctx, context, type, size, num_elements, data, NULL, buf_id);
}

992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008

VAStatus 
i965_BufferSetNumElements(VADriverContextP ctx,
                          VABufferID buf_id,           /* in */
                          unsigned int num_elements)   /* in */
{
    struct i965_driver_data *i965 = i965_driver_data(ctx);
    struct object_buffer *obj_buffer = BUFFER(buf_id);
    VAStatus vaStatus = VA_STATUS_SUCCESS;

    assert(obj_buffer);

    if ((num_elements < 0) || 
        (num_elements > obj_buffer->max_num_elements)) {
        vaStatus = VA_STATUS_ERROR_UNKNOWN;
    } else {
        obj_buffer->num_elements = num_elements;
1009
        if (obj_buffer->buffer_store != NULL) {
1010
            obj_buffer->buffer_store->num_elements = num_elements;
1011
        }
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
    }

    return vaStatus;
}

VAStatus 
i965_MapBuffer(VADriverContextP ctx,
               VABufferID buf_id,       /* in */
               void **pbuf)             /* out */
{
    struct i965_driver_data *i965 = i965_driver_data(ctx);
    struct object_buffer *obj_buffer = BUFFER(buf_id);
    VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;

    assert(obj_buffer && obj_buffer->buffer_store);
    assert(obj_buffer->buffer_store->bo || obj_buffer->buffer_store->buffer);
    assert(!(obj_buffer->buffer_store->bo && obj_buffer->buffer_store->buffer));

    if (NULL != obj_buffer->buffer_store->bo) {
1031 1032 1033 1034 1035 1036 1037 1038 1039
        unsigned int tiling, swizzle;

        dri_bo_get_tiling(obj_buffer->buffer_store->bo, &tiling, &swizzle);

        if (tiling != I915_TILING_NONE)
            drm_intel_gem_bo_map_gtt(obj_buffer->buffer_store->bo);
        else
            dri_bo_map(obj_buffer->buffer_store->bo, 1);

1040 1041
        assert(obj_buffer->buffer_store->bo->virtual);
        *pbuf = obj_buffer->buffer_store->bo->virtual;
Xiang, Haihao's avatar
Xiang, Haihao committed
1042 1043 1044 1045 1046 1047

        if (obj_buffer->type == VAEncCodedBufferType) {
            VACodedBufferSegment *coded_buffer_segment = (VACodedBufferSegment *)(obj_buffer->buffer_store->bo->virtual);
            coded_buffer_segment->buf = (unsigned char *)(obj_buffer->buffer_store->bo->virtual) + ALIGN(sizeof(VACodedBufferSegment), 64);
        }

1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
        vaStatus = VA_STATUS_SUCCESS;
    } else if (NULL != obj_buffer->buffer_store->buffer) {
        *pbuf = obj_buffer->buffer_store->buffer;
        vaStatus = VA_STATUS_SUCCESS;
    }

    return vaStatus;
}

VAStatus 
i965_UnmapBuffer(VADriverContextP ctx, VABufferID buf_id)
{
    struct i965_driver_data *i965 = i965_driver_data(ctx);
    struct object_buffer *obj_buffer = BUFFER(buf_id);
    VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;

    assert(obj_buffer && obj_buffer->buffer_store);
    assert(obj_buffer->buffer_store->bo || obj_buffer->buffer_store->buffer);
    assert(!(obj_buffer->buffer_store->bo && obj_buffer->buffer_store->buffer));

    if (NULL != obj_buffer->buffer_store->bo) {
1069 1070 1071 1072 1073 1074 1075 1076 1077
        unsigned int tiling, swizzle;

        dri_bo_get_tiling(obj_buffer->buffer_store->bo, &tiling, &swizzle);

        if (tiling != I915_TILING_NONE)
            drm_intel_gem_bo_unmap_gtt(obj_buffer->buffer_store->bo);
        else
            dri_bo_unmap(obj_buffer->buffer_store->bo);

1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
        vaStatus = VA_STATUS_SUCCESS;
    } else if (NULL != obj_buffer->buffer_store->buffer) {
        /* Do nothing */
        vaStatus = VA_STATUS_SUCCESS;
    }

    return vaStatus;    
}

VAStatus 
i965_DestroyBuffer(VADriverContextP ctx, VABufferID buffer_id)
{
    struct i965_driver_data *i965 = i965_driver_data(ctx);
    struct object_buffer *obj_buffer = BUFFER(buffer_id);

    assert(obj_buffer);
    i965_destroy_buffer(&i965->buffer_heap, (struct object_base *)obj_buffer);

    return VA_STATUS_SUCCESS;
}

VAStatus 
i965_BeginPicture(VADriverContextP ctx,
                  VAContextID context,
                  VASurfaceID render_target)
{
1104
    struct intel_driver_data * const intel = intel_driver_data(ctx);
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
    struct i965_driver_data *i965 = i965_driver_data(ctx); 
    struct object_context *obj_context = CONTEXT(context);
    struct object_surface *obj_surface = SURFACE(render_target);
    struct object_config *obj_config;
    VAContextID config;
    VAStatus vaStatus;

    assert(obj_context);
    assert(obj_surface);

    config = obj_context->config_id;
    obj_config = CONFIG(config);
    assert(obj_config);

1119 1120 1121 1122 1123
    if (VAEntrypointEncSlice == obj_config->entrypoint ){
        vaStatus = i965_encoder_begin_picture(ctx, context, render_target);
        return vaStatus;
    }

1124 1125 1126 1127 1128 1129
    switch (obj_config->profile) {
    case VAProfileMPEG2Simple:
    case VAProfileMPEG2Main:
        vaStatus = VA_STATUS_SUCCESS;
        break;

1130 1131 1132 1133 1134 1135
    case VAProfileH264Baseline:
    case VAProfileH264Main:
    case VAProfileH264High:
        vaStatus = VA_STATUS_SUCCESS;
        break;

1136 1137 1138 1139 1140 1141
    case VAProfileVC1Simple:
    case VAProfileVC1Main:
    case VAProfileVC1Advanced:
        vaStatus = VA_STATUS_SUCCESS;
        break;

1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
    default:
        assert(0);
        vaStatus = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
        break;
    }

    obj_context->decode_state.current_render_target = render_target;

    return vaStatus;
}

static VAStatus
i965_render_picture_parameter_buffer(VADriverContextP ctx,
                                     struct object_context *obj_context,
                                     struct object_buffer *obj_buffer)
{
    assert(obj_buffer->buffer_store->bo == NULL);
    assert(obj_buffer->buffer_store->buffer);
    i965_release_buffer_store(&obj_context->decode_state.pic_param);
    i965_reference_buffer_store(&obj_context->decode_state.pic_param,
                                obj_buffer->buffer_store);

    return VA_STATUS_SUCCESS;
}

static VAStatus
i965_render_iq_matrix_buffer(VADriverContextP ctx,
                             struct object_context *obj_context,
                             struct object_buffer *obj_buffer)
{
    assert(obj_buffer->buffer_store->bo == NULL);
    assert(obj_buffer->buffer_store->buffer);
    i965_release_buffer_store(&obj_context->decode_state.iq_matrix);
    i965_reference_buffer_store(&obj_context->decode_state.iq_matrix,
                                obj_buffer->buffer_store);

    return VA_STATUS_SUCCESS;
}

static VAStatus
i965_render_bit_plane_buffer(VADriverContextP ctx,
                             struct object_context *obj_context,
                             struct object_buffer *obj_buffer)
{
    assert(obj_buffer->buffer_store->bo == NULL);
    assert(obj_buffer->buffer_store->buffer);
    i965_release_buffer_store(&obj_context->decode_state.bit_plane);
    i965_reference_buffer_store(&obj_context->decode_state.bit_plane,
1190
                                obj_buffer->buffer_store);
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
    
    return VA_STATUS_SUCCESS;
}

static VAStatus
i965_render_slice_parameter_buffer(VADriverContextP ctx,
                                   struct object_context *obj_context,
                                   struct object_buffer *obj_buffer)
{
    assert(obj_buffer->buffer_store->bo == NULL);
    assert(obj_buffer->buffer_store->buffer);
1202 1203 1204
    
    if (obj_context->decode_state.num_slice_params == obj_context->decode_state.max_slice_params) {
        obj_context->decode_state.slice_params = realloc(obj_context->decode_state.slice_params,
1205
                                                         (obj_context->decode_state.max_slice_params + NUM_SLICES) * sizeof(*obj_context->decode_state.slice_params));
1206 1207 1208 1209 1210 1211
        memset(obj_context->decode_state.slice_params + obj_context->decode_state.max_slice_params, 0, NUM_SLICES * sizeof(*obj_context->decode_state.slice_params));
        obj_context->decode_state.max_slice_params += NUM_SLICES;
    }
        
    i965_release_buffer_store(&obj_context->decode_state.slice_params[obj_context->decode_state.num_slice_params]);
    i965_reference_buffer_store(&obj_context->decode_state.slice_params[obj_context->decode_state.num_slice_params],
1212
                                obj_buffer->buffer_store);
1213
    obj_context->decode_state.num_slice_params++;
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
    
    return VA_STATUS_SUCCESS;
}

static VAStatus
i965_render_slice_data_buffer(VADriverContextP ctx,
                              struct object_context *obj_context,
                              struct object_buffer *obj_buffer)
{
    assert(obj_buffer->buffer_store->buffer == NULL);
    assert(obj_buffer->buffer_store->bo);
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234

    if (obj_context->decode_state.num_slice_datas == obj_context->decode_state.max_slice_datas) {
        obj_context->decode_state.slice_datas = realloc(obj_context->decode_state.slice_datas,
                                                        (obj_context->decode_state.max_slice_datas + NUM_SLICES) * sizeof(*obj_context->decode_state.slice_datas));
        memset(obj_context->decode_state.slice_datas + obj_context->decode_state.max_slice_datas, 0, NUM_SLICES * sizeof(*obj_context->decode_state.slice_datas));
        obj_context->decode_state.max_slice_datas += NUM_SLICES;
    }
        
    i965_release_buffer_store(&obj_context->decode_state.slice_datas[obj_context->decode_state.num_slice_datas]);
    i965_reference_buffer_store(&obj_context->decode_state.slice_datas[obj_context->decode_state.num_slice_datas],
1235
                                obj_buffer->buffer_store);
1236
    obj_context->decode_state.num_slice_datas++;
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
    
    return VA_STATUS_SUCCESS;
}

VAStatus 
i965_RenderPicture(VADriverContextP ctx,
                   VAContextID context,
                   VABufferID *buffers,
                   int num_buffers)
{
    struct i965_driver_data *i965 = i965_driver_data(ctx);
    struct object_context *obj_context;
1249 1250
    struct object_config *obj_config;
    VAContextID config;
1251 1252 1253 1254 1255 1256
    int i;
    VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;

    obj_context = CONTEXT(context);
    assert(obj_context);

1257 1258 1259 1260 1261 1262 1263 1264 1265
    config = obj_context->config_id;
    obj_config = CONFIG(config);
    assert(obj_config);

    if (VAEntrypointEncSlice == obj_config->entrypoint ){
        vaStatus = i965_encoder_render_picture(ctx, context, buffers, num_buffers);
        return vaStatus;
    }

1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
    for (i = 0; i < num_buffers; i++) {
        struct object_buffer *obj_buffer = BUFFER(buffers[i]);
        assert(obj_buffer);

        switch (obj_buffer->type) {
        case VAPictureParameterBufferType:
            vaStatus = i965_render_picture_parameter_buffer(ctx, obj_context, obj_buffer);
            break;
            
        case VAIQMatrixBufferType:
            vaStatus = i965_render_iq_matrix_buffer(ctx, obj_context, obj_buffer);
            break;

        case VABitPlaneBufferType:
            vaStatus = i965_render_bit_plane_buffer(ctx, obj_context, obj_buffer);
            break;

        case VASliceParameterBufferType:
            vaStatus = i965_render_slice_parameter_buffer(ctx, obj_context, obj_buffer);
            break;

        case VASliceDataBufferType:
            vaStatus = i965_render_slice_data_buffer(ctx, obj_context, obj_buffer);
            break;

        default:
            break;
        }
    }

    return vaStatus;
}

VAStatus 
i965_EndPicture(VADriverContextP ctx, VAContextID context)
{
    struct i965_driver_data *i965 = i965_driver_data(ctx); 
    struct object_context *obj_context = CONTEXT(context);
    struct object_config *obj_config;
    VAContextID config;
1306
    int i;
1307 1308

    assert(obj_context);
1309 1310 1311 1312 1313 1314 1315 1316
    config = obj_context->config_id;
    obj_config = CONFIG(config);
    assert(obj_config);

    if (VAEntrypointEncSlice == obj_config->entrypoint ){
        return i965_encoder_end_picture(ctx, context);
    }

1317
    assert(obj_context->decode_state.pic_param);
1318 1319 1320
    assert(obj_context->decode_state.num_slice_params >= 1);
    assert(obj_context->decode_state.num_slice_datas >= 1);
    assert(obj_context->decode_state.num_slice_params == obj_context->decode_state.num_slice_datas);
1321 1322 1323

    i965_media_decode_picture(ctx, obj_config->profile, &obj_context->decode_state);
    obj_context->decode_state.current_render_target = -1;
1324 1325
    obj_context->decode_state.num_slice_params = 0;
    obj_context->decode_state.num_slice_datas = 0;
1326 1327 1328
    i965_release_buffer_store(&obj_context->decode_state.pic_param);
    i965_release_buffer_store(&obj_context->decode_state.iq_matrix);
    i965_release_buffer_store(&obj_context->decode_state.bit_plane);
1329 1330 1331 1332 1333

    for (i = 0; i < obj_context->decode_state.num_slice_params; i++) {
        i965_release_buffer_store(&obj_context->decode_state.slice_params[i]);
        i965_release_buffer_store(&obj_context->decode_state.slice_datas[i]);
    }
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358

    return VA_STATUS_SUCCESS;
}

VAStatus 
i965_SyncSurface(VADriverContextP ctx,
                 VASurfaceID render_target)
{
    struct i965_driver_data *i965 = i965_driver_data(ctx); 
    struct object_surface *obj_surface = SURFACE(render_target);

    assert(obj_surface);

    return VA_STATUS_SUCCESS;
}

VAStatus 
i965_QuerySurfaceStatus(VADriverContextP ctx,
                        VASurfaceID render_target,
                        VASurfaceStatus *status)        /* out */
{
    struct i965_driver_data *i965 = i965_driver_data(ctx); 
    struct object_surface *obj_surface = SURFACE(render_target);

    assert(obj_surface);
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370

    /* Commit pending operations to the HW */
    intel_batchbuffer_flush(ctx);

    /* Usually GEM will handle synchronization with the graphics hardware */
#if 0
    if (obj_surface->bo) {
        dri_bo_map(obj_surface->bo, 0);
        dri_bo_unmap(obj_surface->bo);
    }
#endif
    
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
    *status = obj_surface->status;

    return VA_STATUS_SUCCESS;
}


/* 
 * Query display attributes 
 * The caller must provide a "attr_list" array that can hold at
 * least vaMaxNumDisplayAttributes() entries. The actual number of attributes
 * returned in "attr_list" is returned in "num_attributes".
 */
VAStatus 
i965_QueryDisplayAttributes(VADriverContextP ctx,
                            VADisplayAttribute *attr_list,	/* out */
                            int *num_attributes)		/* out */
{
1388 1389 1390 1391
    if (num_attributes)
        *num_attributes = 0;

    return VA_STATUS_SUCCESS;
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
}

/* 
 * Get display attributes 
 * This function returns the current attribute values in "attr_list".
 * Only attributes returned with VA_DISPLAY_ATTRIB_GETTABLE set in the "flags" field
 * from vaQueryDisplayAttributes() can have their values retrieved.  
 */
VAStatus 
i965_GetDisplayAttributes(VADriverContextP ctx,
                          VADisplayAttribute *attr_list,	/* in/out */
                          int num_attributes)
{
    /* TODO */
1406
    return VA_STATUS_ERROR_UNIMPLEMENTED;
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
}

/* 
 * Set display attributes 
 * Only attributes returned with VA_DISPLAY_ATTRIB_SETTABLE set in the "flags" field
 * from vaQueryDisplayAttributes() can be set.  If the attribute is not settable or 
 * the value is out of range, the function returns VA_STATUS_ERROR_ATTR_NOT_SUPPORTED
 */
VAStatus 
i965_SetDisplayAttributes(VADriverContextP ctx,
                          VADisplayAttribute *attr_list,
                          int num_attributes)
{
    /* TODO */
1421
    return VA_STATUS_ERROR_UNIMPLEMENTED;
1422 1423 1424 1425 1426 1427 1428 1429 1430
}

VAStatus 
i965_DbgCopySurfaceToBuffer(VADriverContextP ctx,
                            VASurfaceID surface,
                            void **buffer,              /* out */
                            unsigned int *stride)       /* out */
{
    /* TODO */
1431
    return VA_STATUS_ERROR_UNIMPLEMENTED;
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
}

static VAStatus 
i965_Init(VADriverContextP ctx)
{
    struct i965_driver_data *i965 = i965_driver_data(ctx); 

    if (intel_driver_init(ctx) == False)
        return VA_STATUS_ERROR_UNKNOWN;

1442
    if (!IS_G4X(i965->intel.device_id) &&
1443 1444
        !IS_IRONLAKE(i965->intel.device_id) &&
        !IS_GEN6(i965->intel.device_id))
1445 1446 1447 1448 1449
        return VA_STATUS_ERROR_UNKNOWN;

    if (i965_media_init(ctx) == False)
        return VA_STATUS_ERROR_UNKNOWN;

1450 1451 1452
    if (i965_post_processing_init(ctx) == False)
        return VA_STATUS_ERROR_UNKNOWN;

1453 1454 1455
    if (i965_render_init(ctx) == False)
        return VA_STATUS_ERROR_UNKNOWN;

1456 1457 1458
    if (HAS_ENCODER(i965) && (i965_encoder_init(ctx) == False))
        return VA_STATUS_ERROR_UNKNOWN;

1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
    return VA_STATUS_SUCCESS;
}

static void
i965_destroy_heap(struct object_heap *heap, 
                  void (*func)(struct object_heap *heap, struct object_base *object))
{
    struct object_base *object;
    object_heap_iterator iter;    

    object = object_heap_first(heap, &iter);

    while (object) {
        if (func)
            func(heap, object);

        object = object_heap_next(heap, &iter);
    }

    object_heap_destroy(heap);
}


1482 1483
VAStatus 
i965_DestroyImage(VADriverContextP ctx, VAImageID image);
1484 1485 1486 1487 1488 1489

VAStatus 
i965_CreateImage(VADriverContextP ctx,
                 VAImageFormat *format,
                 int width,
                 int height,
1490
                 VAImage *out_image)        /* out */
1491 1492
{
    struct i965_driver_data *i965 = i965_driver_data(ctx);
1493 1494 1495 1496
    struct object_image *obj_image;
    VAStatus va_status = VA_STATUS_ERROR_OPERATION_FAILED;
    VAImageID image_id;
    unsigned int width2, height2, size2, size;
1497

1498 1499
    out_image->image_id = VA_INVALID_ID;
    out_image->buf      = VA_INVALID_ID;
1500

1501 1502 1503 1504 1505 1506 1507
    image_id = NEW_IMAGE_ID();
    if (image_id == VA_INVALID_ID)
        return VA_STATUS_ERROR_ALLOCATION_FAILED;

    obj_image = IMAGE(image_id);
    if (!obj_image)
        return VA_STATUS_ERROR_ALLOCATION_FAILED;
1508 1509
    obj_image->bo         = NULL;
    obj_image->palette    = NULL;
1510
    obj_image->derived_surface = VA_INVALID_ID;
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537

    VAImage * const image = &obj_image->image;
    image->image_id       = image_id;
    image->buf            = VA_INVALID_ID;

    size    = width * height;
    width2  = (width  + 1) / 2;
    height2 = (height + 1) / 2;
    size2   = width2 * height2;

    image->num_palette_entries = 0;
    image->entry_bytes         = 0;
    memset(image->component_order, 0, sizeof(image->component_order));

    switch (format->fourcc) {
    case VA_FOURCC('I','A','4','4'):
    case VA_FOURCC('A','I','4','4'):
        image->num_planes = 1;
        image->pitches[0] = width;
        image->offsets[0] = 0;
        image->data_size  = image->offsets[0] + image->pitches[0] * height;
        image->num_palette_entries = 16;
        image->entry_bytes         = 3;
        image->component_order[0]  = 'R';
        image->component_order[1]  = 'G';
        image->component_order[2]  = 'B';
        break;
Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
1538 1539 1540 1541 1542 1543 1544 1545 1546
    case VA_FOURCC('A','R','G','B'):
    case VA_FOURCC('A','B','G','R'):
    case VA_FOURCC('B','G','R','A'):
    case VA_FOURCC('R','G','B','A'):
        image->num_planes = 1;
        image->pitches[0] = width * 4;
        image->offsets[0] = 0;
        image->data_size  = image->offsets[0] + image->pitches[0] * height;
        break;
Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556
    case VA_FOURCC('Y','V','1','2'):
        image->num_planes = 3;
        image->pitches[0] = width;
        image->offsets[0] = 0;
        image->pitches[1] = width2;
        image->offsets[1] = size + size2;
        image->pitches[2] = width2;
        image->offsets[2] = size;
        image->data_size  = size + 2 * size2;
        break;
Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
    case VA_FOURCC('I','4','2','0'):
        image->num_planes = 3;
        image->pitches[0] = width;
        image->offsets[0] = 0;
        image->pitches[1] = width2;
        image->offsets[1] = size;
        image->pitches[2] = width2;
        image->offsets[2] = size + size2;
        image->data_size  = size + 2 * size2;
        break;
Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
1567 1568 1569 1570 1571 1572 1573 1574
    case VA_FOURCC('N','V','1','2'):
        image->num_planes = 2;
        image->pitches[0] = width;
        image->offsets[0] = 0;
        image->pitches[1] = width;
        image->offsets[1] = size;
        image->data_size  = size + 2 * size2;
        break;
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
    default:
        goto error;
    }

    va_status = i965_CreateBuffer(ctx, 0, VAImageBufferType,
                                  image->data_size, 1, NULL, &image->buf);
    if (va_status != VA_STATUS_SUCCESS)
        goto error;

    obj_image->bo = BUFFER(image->buf)->buffer_store->bo;
1585
    dri_bo_reference(obj_image->bo);
1586

1587 1588 1589 1590 1591 1592
    if (image->num_palette_entries > 0 && image->entry_bytes > 0) {
        obj_image->palette = malloc(image->num_palette_entries * sizeof(obj_image->palette));
        if (!obj_image->palette)
            goto error;
    }

1593 1594 1595 1596 1597 1598
    image->image_id             = image_id;
    image->format               = *format;
    image->width                = width;
    image->height               = height;

    *out_image                  = *image;
1599
    return VA_STATUS_SUCCESS;
1600

1601
 error:
1602 1603
    i965_DestroyImage(ctx, image_id);
    return va_status;
1604 1605 1606 1607
}

VAStatus i965_DeriveImage(VADriverContextP ctx,
                          VASurfaceID surface,
1608
                          VAImage *out_image)        /* out */
1609
{
1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
    struct i965_driver_data *i965 = i965_driver_data(ctx);
    struct i965_render_state *render_state = &i965->render_state;
    struct object_image *obj_image;
    struct object_surface *obj_surface; 
    VAImageID image_id;
    unsigned int w_pitch, h_pitch;
    unsigned int data_size;
    VAStatus va_status;

    out_image->image_id = VA_INVALID_ID;
    obj_surface = SURFACE(surface);

    if (!obj_surface)
        return VA_STATUS_ERROR_INVALID_SURFACE;

    w_pitch = obj_surface->width;
    h_pitch = obj_surface->height;
    data_size = obj_surface->orig_width * obj_surface->orig_height +
        2 * (((obj_surface->orig_width + 1) / 2) * ((obj_surface->orig_height + 1) / 2));

    image_id = NEW_IMAGE_ID();

    if (image_id == VA_INVALID_ID)
        return VA_STATUS_ERROR_ALLOCATION_FAILED;

    obj_image = IMAGE(image_id);
    
    if (!obj_image)
        return VA_STATUS_ERROR_ALLOCATION_FAILED;

    obj_image->bo = NULL;
    obj_image->palette = NULL;
    obj_image->derived_surface = VA_INVALID_ID;

    VAImage * const image = &obj_image->image;
    
    memset(image, 0, sizeof(*image));
    image->image_id = image_id;
    image->buf = VA_INVALID_ID;
    image->num_palette_entries = 0;
    image->entry_bytes = 0;
    image->width = obj_surface->orig_width;
    image->height = obj_surface->orig_height;
    image->data_size = data_size;

    if (render_state->interleaved_uv) {
        image->format.fourcc = VA_FOURCC('N','V','1','2');
        image->format.byte_order = VA_LSB_FIRST;
        image->format.bits_per_pixel = 12;
        image->num_planes = 2;
        image->pitches[0] = w_pitch;
        image->offsets[0] = 0;
        image->pitches[1] = w_pitch;
        image->offsets[1] = w_pitch * h_pitch;
    } else {
        image->format.fourcc = VA_FOURCC('Y','V','1','2');
        image->format.byte_order = VA_LSB_FIRST;
        image->format.bits_per_pixel = 12;
        image->num_planes = 3;
        image->pitches[0] = w_pitch;
        image->offsets[0] = 0;
        image->pitches[1] = w_pitch / 2;
        image->offsets[1] = w_pitch * h_pitch;
        image->pitches[2] = w_pitch / 2;
        image->offsets[2] = w_pitch * h_pitch + (w_pitch / 2) * (h_pitch / 2);
    }

    if (obj_surface->bo == NULL) {
        if (HAS_TILED_SURFACE(i965)) {
        
            uint32_t tiling_mode = I915_TILING_Y;
            unsigned long pitch;

            obj_surface->bo = drm_intel_bo_alloc_tiled(i965->intel.bufmgr, 
                                                       "vaapi surface",
                                                       obj_surface->width, 
                                                       obj_surface->height + obj_surface->height / 2,
                                                       1,
                                                       &tiling_mode,
                                                       &pitch,
                                                       0);
            assert(obj_surface->bo);
            assert(tiling_mode == I915_TILING_Y);
            assert(pitch == obj_surface->width);
        } else {
            obj_surface->bo = dri_bo_alloc(i965->intel.bufmgr,
                                           "vaapi surface",
                                           obj_surface->size,
                                           0x1000);
        }
    }

    assert(obj_surface->bo);
    va_status = i965_create_buffer_internal(ctx, 0, VAImageBufferType,
                                            obj_surface->size, 1, NULL, obj_surface->bo, &image->buf);
    if (va_status != VA_STATUS_SUCCESS)
        goto error;

    obj_image->bo = BUFFER(image->buf)->buffer_store->bo;
    dri_bo_reference(obj_image->bo);

    if (image->num_palette_entries > 0 && image->entry_bytes > 0) {
        obj_image->palette = malloc(image->num_palette_entries * sizeof(obj_image->palette));
        if (!obj_image->palette) {
            va_status = VA_STATUS_ERROR_ALLOCATION_FAILED;
            goto error;
        }
    }

    *out_image = *image;
    obj_surface->flags |= SURFACE_DERIVED;

    return VA_STATUS_SUCCESS;

1724
 error:
1725 1726
    i965_DestroyImage(ctx, image_id);
    return va_status;
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740
}

static void 
i965_destroy_image(struct object_heap *heap, struct object_base *obj)
{
    object_heap_free(heap, obj);
}


VAStatus 
i965_DestroyImage(VADriverContextP ctx, VAImageID image)
{
    struct i965_driver_data *i965 = i965_driver_data(ctx);
    struct object_image *obj_image = IMAGE(image); 
1741
    struct object_surface *obj_surface; 
1742

1743 1744 1745
    if (!obj_image)
        return VA_STATUS_SUCCESS;

1746 1747 1748
    dri_bo_unreference(obj_image->bo);
    obj_image->bo = NULL;

1749
    if (obj_image->image.buf != VA_INVALID_ID) {
1750 1751
        i965_DestroyBuffer(ctx, obj_image->image.buf);
        obj_image->image.buf = VA_INVALID_ID;
Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
1752 1753
    }

1754 1755 1756 1757 1758
    if (obj_image->palette) {
        free(obj_image->palette);
        obj_image->palette = NULL;
    }

1759 1760 1761 1762 1763 1764
    obj_surface = SURFACE(obj_image->derived_surface);

    if (obj_surface) {
        obj_surface->flags &= ~SURFACE_DERIVED;
    }

1765 1766 1767 1768 1769
    i965_destroy_image(&i965->image_heap, (struct object_base *)obj_image);
	
    return VA_STATUS_SUCCESS;
}

1770 1771 1772 1773 1774
/*
 * pointer to an array holding the palette data.  The size of the array is
 * num_palette_entries * entry_bytes in size.  The order of the components
 * in the palette is described by the component_order in VASubpicture struct
 */
1775 1776 1777 1778 1779
VAStatus 
i965_SetImagePalette(VADriverContextP ctx,
                     VAImageID image,
                     unsigned char *palette)
{
1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792
    struct i965_driver_data *i965 = i965_driver_data(ctx);
    unsigned int i;

    struct object_image *obj_image = IMAGE(image);
    if (!obj_image)
        return VA_STATUS_ERROR_INVALID_IMAGE;

    if (!obj_image->palette)
        return VA_STATUS_ERROR_ALLOCATION_FAILED; /* XXX: unpaletted/error */

    for (i = 0; i < obj_image->image.num_palette_entries; i++)
        obj_image->palette[i] = (((unsigned int)palette[3*i + 0] << 16) |
                                 ((unsigned int)palette[3*i + 1] <<  8) |
1793
                                 (unsigned int)palette[3*i + 2]);
1794 1795 1796
    return VA_STATUS_SUCCESS;
}

Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810
static inline void
memcpy_pic(uint8_t *dst, unsigned int dst_stride,
           const uint8_t *src, unsigned int src_stride,
           unsigned int len, unsigned int height)
{
    unsigned int i;

    for (i = 0; i < height; i++) {
        memcpy(dst, src, len);
        dst += dst_stride;
        src += src_stride;
    }
}

1811
static void
Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
1812
get_image_i420(struct object_image *obj_image, uint8_t *image_data,
1813 1814 1815 1816
               struct object_surface *obj_surface,
               const VARectangle *rect)
{
    uint8_t *dst[3], *src[3];
1817 1818 1819
    const int Y = 0;
    const int U = obj_image->image.format.fourcc == VA_FOURCC_YV12 ? 2 : 1;
    const int V = obj_image->image.format.fourcc == VA_FOURCC_YV12 ? 1 : 2;
1820
    unsigned int tiling, swizzle;
1821 1822 1823 1824

    if (!obj_surface->bo)
        return;

1825 1826 1827 1828 1829 1830
    dri_bo_get_tiling(obj_surface->bo, &tiling, &swizzle);

    if (tiling != I915_TILING_NONE)
        drm_intel_gem_bo_map_gtt(obj_surface->bo);
    else
        dri_bo_map(obj_surface->bo, 0);
1831 1832 1833 1834

    if (!obj_surface->bo->virtual)
        return;

Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
1835 1836
    /* Dest VA image has either I420 or YV12 format.
       Source VA surface alway has I420 format */
1837
    dst[Y] = image_data + obj_image->image.offsets[Y];
1838
    src[0] = (uint8_t *)obj_surface->bo->virtual;
1839
    dst[U] = image_data + obj_image->image.offsets[U];
1840
    src[1] = src[0] + obj_surface->width * obj_surface->height;
1841
    dst[V] = image_data + obj_image->image.offsets[V];
1842 1843
    src[2] = src[1] + (obj_surface->width / 2) * (obj_surface->height / 2);

Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
1844
    /* Y plane */
1845
    dst[Y] += rect->y * obj_image->image.pitches[Y] + rect->x;
Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
1846
    src[0] += rect->y * obj_surface->width + rect->x;
1847
    memcpy_pic(dst[Y], obj_image->image.pitches[Y],
Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
1848 1849 1850 1851
               src[0], obj_surface->width,
               rect->width, rect->height);

    /* U plane */
1852
    dst[U] += (rect->y / 2) * obj_image->image.pitches[U] + rect->x / 2;
Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
1853
    src[1] += (rect->y / 2) * obj_surface->width / 2 + rect->x / 2;
1854
    memcpy_pic(dst[U], obj_image->image.pitches[U],
Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
1855 1856 1857 1858
               src[1], obj_surface->width / 2,
               rect->width / 2, rect->height / 2);

    /* V plane */
1859
    dst[V] += (rect->y / 2) * obj_image->image.pitches[V] + rect->x / 2;
Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
1860
    src[2] += (rect->y / 2) * obj_surface->width / 2 + rect->x / 2;
1861
    memcpy_pic(dst[V], obj_image->image.pitches[V],
Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
1862 1863
               src[2], obj_surface->width / 2,
               rect->width / 2, rect->height / 2);
1864

1865 1866 1867 1868
    if (tiling != I915_TILING_NONE)
        drm_intel_gem_bo_unmap_gtt(obj_surface->bo);
    else
        dri_bo_unmap(obj_surface->bo);
1869 1870
}

Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
1871 1872 1873 1874 1875
static void
get_image_nv12(struct object_image *obj_image, uint8_t *image_data,
               struct object_surface *obj_surface,
               const VARectangle *rect)
{
Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
1876
    uint8_t *dst[2], *src[2];
1877
    unsigned int tiling, swizzle;
Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
1878 1879 1880 1881

    if (!obj_surface->bo)
        return;

1882 1883 1884 1885 1886 1887
    dri_bo_get_tiling(obj_surface->bo, &tiling, &swizzle);

    if (tiling != I915_TILING_NONE)
        drm_intel_gem_bo_map_gtt(obj_surface->bo);
    else
        dri_bo_map(obj_surface->bo, 0);
Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
1888 1889 1890 1891

    if (!obj_surface->bo->virtual)
        return;

Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
1892 1893 1894 1895 1896
    /* Both dest VA image and source surface have NV12 format */
    dst[0] = image_data + obj_image->image.offsets[0];
    src[0] = (uint8_t *)obj_surface->bo->virtual;
    dst[1] = image_data + obj_image->image.offsets[1];
    src[1] = src[0] + obj_surface->width * obj_surface->height;
Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
1897

Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
1898 1899 1900 1901 1902 1903
    /* Y plane */
    dst[0] += rect->y * obj_image->image.pitches[0] + rect->x;
    src[0] += rect->y * obj_surface->width + rect->x;
    memcpy_pic(dst[0], obj_image->image.pitches[0],
               src[0], obj_surface->width,
               rect->width, rect->height);
Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
1904

Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
1905 1906 1907 1908 1909 1910
    /* UV plane */
    dst[1] += (rect->y / 2) * obj_image->image.pitches[1] + (rect->x & -2);
    src[1] += (rect->y / 2) * obj_surface->width + (rect->x & -2);
    memcpy_pic(dst[1], obj_image->image.pitches[1],
               src[1], obj_surface->width,
               rect->width, rect->height / 2);
Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
1911

1912 1913 1914 1915
    if (tiling != I915_TILING_NONE)
        drm_intel_gem_bo_unmap_gtt(obj_surface->bo);
    else
        dri_bo_unmap(obj_surface->bo);
Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
1916 1917
}

1918 1919 1920 1921 1922 1923 1924 1925 1926
VAStatus 
i965_GetImage(VADriverContextP ctx,
              VASurfaceID surface,
              int x,   /* coordinates of the upper left source pixel */
              int y,
              unsigned int width,      /* width and height of the region */
              unsigned int height,
              VAImageID image)
{
1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939
    struct i965_driver_data *i965 = i965_driver_data(ctx);
    struct i965_render_state *render_state = &i965->render_state;

    struct object_surface *obj_surface = SURFACE(surface);
    if (!obj_surface)
        return VA_STATUS_ERROR_INVALID_SURFACE;

    struct object_image *obj_image = IMAGE(image);
    if (!obj_image)
        return VA_STATUS_ERROR_INVALID_IMAGE;

    if (x < 0 || y < 0)
        return VA_STATUS_ERROR_INVALID_PARAMETER;
1940 1941
    if (x + width > obj_surface->orig_width ||
        y + height > obj_surface->orig_height)
1942
        return VA_STATUS_ERROR_INVALID_PARAMETER;
1943 1944
    if (x + width > obj_image->image.width ||
        y + height > obj_image->image.height)
1945 1946
        return VA_STATUS_ERROR_INVALID_PARAMETER;

1947 1948 1949
    /* Commit pending operations to the HW */
    intel_batchbuffer_flush(ctx);

1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963
    VAStatus va_status;
    void *image_data = NULL;

    va_status = i965_MapBuffer(ctx, obj_image->image.buf, &image_data);
    if (va_status != VA_STATUS_SUCCESS)
        return va_status;

    VARectangle rect;
    rect.x = x;
    rect.y = y;
    rect.width = width;
    rect.height = height;

    switch (obj_image->image.format.fourcc) {
Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
1964 1965 1966
    case VA_FOURCC('Y','V','1','2'):
    case VA_FOURCC('I','4','2','0'):
        /* I420 is native format for MPEG-2 decoded surfaces */
1967 1968
        if (render_state->interleaved_uv)
            goto operation_failed;
Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
1969
        get_image_i420(obj_image, image_data, obj_surface, &rect);
1970
        break;
Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
1971 1972 1973 1974 1975 1976
    case VA_FOURCC('N','V','1','2'):
        /* NV12 is native format for H.264 decoded surfaces */
        if (!render_state->interleaved_uv)
            goto operation_failed;
        get_image_nv12(obj_image, image_data, obj_surface, &rect);
        break;
1977 1978 1979 1980 1981 1982 1983 1984
    default:
    operation_failed:
        va_status = VA_STATUS_ERROR_OPERATION_FAILED;
        break;
    }

    i965_UnmapBuffer(ctx, obj_image->image.buf);
    return va_status;
1985 1986 1987 1988 1989
}

VAStatus 
i965_PutSurface(VADriverContextP ctx,
                VASurfaceID surface,
1990
                void *draw, /* X Drawable */
1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
                short srcx,
                short srcy,
                unsigned short srcw,
                unsigned short srch,
                short destx,
                short desty,
                unsigned short destw,
                unsigned short desth,
                VARectangle *cliprects, /* client supplied clip list */
                unsigned int number_cliprects, /* number of clip rects in the clip list */
                unsigned int flags) /* de-interlacing flags */
{
    struct i965_driver_data *i965 = i965_driver_data(ctx); 
    struct dri_state *dri_state = (struct dri_state *)ctx->dri_state;
    struct i965_render_state *render_state = &i965->render_state;
    struct dri_drawable *dri_drawable;
    union dri_buffer *buffer;
    struct intel_region *dest_region;
    struct object_surface *obj_surface; 
2010
    int ret;
2011 2012
    uint32_t name;
    Bool new_region = False;
2013
    int pp_flag = 0;
2014 2015 2016 2017
    /* Currently don't support DRI1 */
    if (dri_state->driConnectedFlag != VA_DRI2)
        return VA_STATUS_ERROR_UNKNOWN;

2018 2019 2020 2021
    /* Some broken sources such as H.264 conformance case FM2_SVA_C
     * will get here
     */
    obj_surface = SURFACE(surface);
2022
    if (!obj_surface || !obj_surface->bo)
2023 2024
        return VA_STATUS_SUCCESS;

2025
    dri_drawable = dri_get_drawable(ctx, (Drawable)draw);
2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062
    assert(dri_drawable);

    buffer = dri_get_rendering_buffer(ctx, dri_drawable);
    assert(buffer);
    
    dest_region = render_state->draw_region;

    if (dest_region) {
        assert(dest_region->bo);
        dri_bo_flink(dest_region->bo, &name);
        
        if (buffer->dri2.name != name) {
            new_region = True;
            dri_bo_unreference(dest_region->bo);
        }
    } else {
        dest_region = (struct intel_region *)calloc(1, sizeof(*dest_region));
        assert(dest_region);
        render_state->draw_region = dest_region;
        new_region = True;
    }

    if (new_region) {
        dest_region->x = dri_drawable->x;
        dest_region->y = dri_drawable->y;
        dest_region->width = dri_drawable->width;
        dest_region->height = dri_drawable->height;
        dest_region->cpp = buffer->dri2.cpp;
        dest_region->pitch = buffer->dri2.pitch;

        dest_region->bo = intel_bo_gem_create_from_name(i965->intel.bufmgr, "rendering buffer", buffer->dri2.name);
        assert(dest_region->bo);

        ret = dri_bo_get_tiling(dest_region->bo, &(dest_region->tiling), &(dest_region->swizzle));
        assert(ret == 0);
    }

2063 2064 2065 2066 2067 2068
    if ((flags & VA_FILTER_SCALING_MASK) == VA_FILTER_SCALING_NL_ANAMORPHIC)
        pp_flag |= I965_PP_FLAG_AVS;

    if (flags & (VA_BOTTOM_FIELD | VA_TOP_FIELD))
        pp_flag |= I965_PP_FLAG_DEINTERLACING;

2069
    intel_render_put_surface(ctx, surface,
2070 2071 2072
                             srcx, srcy, srcw, srch,
                             destx, desty, destw, desth,
                             pp_flag);
2073

2074
    if(obj_surface->subpic != VA_INVALID_ID) {	
2075 2076 2077
	intel_render_put_subpicture(ctx, surface,
                                    srcx, srcy, srcw, srch,
                                    destx, desty, destw, desth);
2078
    } 
2079

2080
    dri_swap_buffer(ctx, dri_drawable);
2081 2082
    obj_surface->flags |= SURFACE_DISPLAYED;

2083
    if ((obj_surface->flags & SURFACE_ALL_MASK) == SURFACE_DISPLAYED) {
2084 2085
        dri_bo_unreference(obj_surface->bo);
        obj_surface->bo = NULL;
2086
        obj_surface->flags &= ~SURFACE_REF_DIS_MASK;
2087 2088 2089 2090

        if (obj_surface->free_private_data)
            obj_surface->free_private_data(&obj_surface->private_data);
    }
2091 2092 2093 2094 2095 2096 2097 2098 2099

    return VA_STATUS_SUCCESS;
}

VAStatus 
i965_Terminate(VADriverContextP ctx)
{
    struct i965_driver_data *i965 = i965_driver_data(ctx);

2100 2101 2102
    if (HAS_ENCODER(i965) && (i965_encoder_terminate(ctx) == False))
        return VA_STATUS_ERROR_UNKNOWN;

2103 2104 2105
    if (i965_render_terminate(ctx) == False)
	return VA_STATUS_ERROR_UNKNOWN;

2106 2107 2108
    if (i965_post_processing_terminate(ctx) == False)
        return VA_STATUS_ERROR_UNKNOWN;

2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128
    if (i965_media_terminate(ctx) == False)
	return VA_STATUS_ERROR_UNKNOWN;

    if (intel_driver_terminate(ctx) == False)
	return VA_STATUS_ERROR_UNKNOWN;

    i965_destroy_heap(&i965->buffer_heap, i965_destroy_buffer);
    i965_destroy_heap(&i965->image_heap, i965_destroy_image);
    i965_destroy_heap(&i965->subpic_heap, i965_destroy_subpic);
    i965_destroy_heap(&i965->surface_heap, i965_destroy_surface);
    i965_destroy_heap(&i965->context_heap, i965_destroy_context);
    i965_destroy_heap(&i965->config_heap, i965_destroy_config);

    free(ctx->pDriverData);
    ctx->pDriverData = NULL;

    return VA_STATUS_SUCCESS;
}

VAStatus 
2129
VA_DRIVER_INIT_FUNC(  VADriverContextP ctx )
2130
{
2131
    struct VADriverVTable * const vtable = ctx->vtable;
2132 2133 2134
    struct i965_driver_data *i965;
    int result;

Gwenole Beauchesne's avatar
Gwenole Beauchesne committed
2135 2136
    ctx->version_major = VA_MAJOR_VERSION;
    ctx->version_minor = VA_MINOR_VERSION;
2137 2138 2139 2140 2141 2142 2143 2144
    ctx->max_profiles = I965_MAX_PROFILES;
    ctx->max_entrypoints = I965_MAX_ENTRYPOINTS;
    ctx->max_attributes = I965_MAX_CONFIG_ATTRIBUTES;
    ctx->max_image_formats = I965_MAX_IMAGE_FORMATS;
    ctx->max_subpic_formats = I965_MAX_SUBPIC_FORMATS;
    ctx->max_display_attributes = I965_MAX_DISPLAY_ATTRIBUTES;
    ctx->str_vendor = I965_STR_VENDOR;

2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185
    vtable->vaTerminate = i965_Terminate;
    vtable->vaQueryConfigEntrypoints = i965_QueryConfigEntrypoints;
    vtable->vaQueryConfigProfiles = i965_QueryConfigProfiles;
    vtable->vaQueryConfigEntrypoints = i965_QueryConfigEntrypoints;
    vtable->vaQueryConfigAttributes = i965_QueryConfigAttributes;
    vtable->vaCreateConfig = i965_CreateConfig;
    vtable->vaDestroyConfig = i965_DestroyConfig;
    vtable->vaGetConfigAttributes = i965_GetConfigAttributes;
    vtable->vaCreateSurfaces = i965_CreateSurfaces;
    vtable->vaDestroySurfaces = i965_DestroySurfaces;
    vtable->vaCreateContext = i965_CreateContext;
    vtable->vaDestroyContext = i965_DestroyContext;
    vtable->vaCreateBuffer = i965_CreateBuffer;
    vtable->vaBufferSetNumElements = i965_BufferSetNumElements;
    vtable->vaMapBuffer = i965_MapBuffer;
    vtable->vaUnmapBuffer = i965_UnmapBuffer;
    vtable->vaDestroyBuffer = i965_DestroyBuffer;
    vtable->vaBeginPicture = i965_BeginPicture;
    vtable->vaRenderPicture = i965_RenderPicture;
    vtable->vaEndPicture = i965_EndPicture;
    vtable->vaSyncSurface = i965_SyncSurface;
    vtable->vaQuerySurfaceStatus = i965_QuerySurfaceStatus;
    vtable->vaPutSurface = i965_PutSurface;
    vtable->vaQueryImageFormats = i965_QueryImageFormats;
    vtable->vaCreateImage = i965_CreateImage;
    vtable->vaDeriveImage = i965_DeriveImage;
    vtable->vaDestroyImage = i965_DestroyImage;
    vtable->vaSetImagePalette = i965_SetImagePalette;
    vtable->vaGetImage = i965_GetImage;
    vtable->vaPutImage = i965_PutImage;
    vtable->vaQuerySubpictureFormats = i965_QuerySubpictureFormats;
    vtable->vaCreateSubpicture = i965_CreateSubpicture;
    vtable->vaDestroySubpicture = i965_DestroySubpicture;
    vtable->vaSetSubpictureImage = i965_SetSubpictureImage;
    vtable->vaSetSubpictureChromakey = i965_SetSubpictureChromakey;
    vtable->vaSetSubpictureGlobalAlpha = i965_SetSubpictureGlobalAlpha;
    vtable->vaAssociateSubpicture = i965_AssociateSubpicture;
    vtable->vaDeassociateSubpicture = i965_DeassociateSubpicture;
    vtable->vaQueryDisplayAttributes = i965_QueryDisplayAttributes;
    vtable->vaGetDisplayAttributes = i965_GetDisplayAttributes;
    vtable->vaSetDisplayAttributes = i965_SetDisplayAttributes;
2186
    //    vtable->vaDbgCopySurfaceToBuffer = i965_DbgCopySurfaceToBuffer;
2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220

    i965 = (struct i965_driver_data *)calloc(1, sizeof(*i965));
    assert(i965);
    ctx->pDriverData = (void *)i965;

    result = object_heap_init(&i965->config_heap, 
                              sizeof(struct object_config), 
                              CONFIG_ID_OFFSET);
    assert(result == 0);

    result = object_heap_init(&i965->context_heap, 
                              sizeof(struct object_context), 
                              CONTEXT_ID_OFFSET);
    assert(result == 0);

    result = object_heap_init(&i965->surface_heap, 
                              sizeof(struct object_surface), 
                              SURFACE_ID_OFFSET);
    assert(result == 0);

    result = object_heap_init(&i965->buffer_heap, 
                              sizeof(struct object_buffer), 
                              BUFFER_ID_OFFSET);
    assert(result == 0);

    result = object_heap_init(&i965->image_heap, 
                              sizeof(struct object_image), 
                              IMAGE_ID_OFFSET);
    assert(result == 0);
	
    result = object_heap_init(&i965->subpic_heap, 
                              sizeof(struct object_subpic), 
                              SUBPIC_ID_OFFSET);
    assert(result == 0);
2221
    
2222 2223
    return i965_Init(ctx);
}