1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 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 365 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 410 411 412 413 414 415 416 417 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 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 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 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 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 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
|
/*
* Copyright (c) 2009-2018, 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, sublicense,
* 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 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 NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS 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.
*/
/*
* Video process test case based on LibVA.
* This test covers 1:N(N>=2) output for scaling and several surface format conversion.
* also support 2nd scale, regarding the 1st scale output as input for 2nd scale
* also support UserPtr 16 alignment as NV12/YV12/YUY2 input
* support none (0, 0) top/left in render target as RGB/YV12 output
* support none (0, 0) top/left input crop
* Usage: ./vppscaling_n_out_usrptr process_scaling_n_out_usrptr.cfg
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <time.h>
#include <assert.h>
#include <va/va.h>
#include <va/va_vpp.h>
#include "va_display.h"
#if 0
#include <va/va_x11.h>
#endif
#define MAX_LEN 1024
#define CHECK_VASTATUS(va_status,func) \
if (va_status != VA_STATUS_SUCCESS) { \
fprintf(stderr,"%s:%s (%d) failed,exit\n", __func__, func, __LINE__); \
exit(1); \
}
#define VPP_FREE(p) \
if(p != NULL){ \
free(p); p = NULL; \
}
typedef struct {
char file_name[100];
FILE* file_fd;
uint32_t pic_width;
uint32_t pic_height;
uint32_t fourcc;
uint32_t rtformat;
uint32_t memtype;
uint32_t align_mode;
void *pBuf;
uint8_t *pUserBase;
uintptr_t ptrb;
} VPP_ImageInfo;
#if 0
static Display *x11_display = NULL;
#endif
static VADisplay va_dpy = NULL;
static VAContextID context_id = 0;
static VAConfigID config_id = 0;
static VASurfaceID g_in_surface_id = VA_INVALID_ID;
static VASurfaceID *g_out_surface_ids = NULL;
static FILE* g_config_file_fd = NULL;
static char g_config_file_name[MAX_LEN];
static VPP_ImageInfo g_src_info;
static VPP_ImageInfo *g_dst_info = NULL;
static uint32_t g_frame_count = 0;
static uint32_t g_dst_count = 1;
static uint32_t g_scale_again = 0;
static int8_t
read_value_string(FILE *fp, const char* field_name, char* value)
{
char strLine[MAX_LEN];
char* field = NULL;
char* str = NULL;
uint16_t i;
if (!fp || !field_name || !value) {
printf("Invalid fuction parameters\n");
return -1;
}
rewind(fp);
while (!feof(fp)) {
if (!fgets(strLine, MAX_LEN, fp))
continue;
for (i = 0; i < MAX_LEN && strLine[i]; i++)
if (strLine[i] != ' ') break;
if (i == MAX_LEN || strLine[i] == '#' || strLine[i] == '\n')
continue;
field = strtok(&strLine[i], ":");
if (strncmp(field, field_name, strlen(field_name)))
continue;
if (!(str = strtok(NULL, ":")))
continue;
/* skip blank space in string */
while (*str == ' ')
str++;
*(str + strlen(str)-1) = '\0';
strcpy(value, str);
return 0;
}
return -1;
}
static int8_t
read_value_uint32(FILE* fp, const char* field_name, uint32_t* value)
{
char str[MAX_LEN];
if (read_value_string(fp, field_name, str)) {
printf("Failed to find integer field: %s", field_name);
return -1;
}
*value = (uint32_t)atoi(str);
return 0;
}
static int8_t
read_value_int16(FILE* fp, const char* field_name, int16_t* value)
{
char str[MAX_LEN];
if (read_value_string(fp, field_name, str)) {
printf("Failed to find integer field: %s", field_name);
return -1;
}
*value = (int16_t)atoi(str);
return 0;
}
static int8_t
read_value_uint16(FILE* fp, const char* field_name, uint16_t* value)
{
char str[MAX_LEN];
if (read_value_string(fp, field_name, str)) {
printf("Failed to find integer field: %s", field_name);
return -1;
}
*value = (uint16_t)atoi(str);
return 0;
}
static VAStatus
create_surface(VPP_ImageInfo &img_info,VASurfaceID * p_surface_id)
{
VAStatus va_status = VA_STATUS_SUCCESS;
if(img_info.memtype == VA_SURFACE_ATTRIB_MEM_TYPE_VA){
VASurfaceAttrib surface_attrib;
surface_attrib.type = VASurfaceAttribPixelFormat;
surface_attrib.flags = VA_SURFACE_ATTRIB_SETTABLE;
surface_attrib.value.type = VAGenericValueTypeInteger;
surface_attrib.value.value.i = img_info.fourcc;
va_status = vaCreateSurfaces(va_dpy,
img_info.rtformat,
img_info.pic_width ,
img_info.pic_height,
p_surface_id,
1,
&surface_attrib,
1);
CHECK_VASTATUS(va_status, "vaCreateSurfaces");
}else if(img_info.memtype == VA_SURFACE_ATTRIB_MEM_TYPE_USER_PTR){
VASurfaceAttrib surfaceAttrib[3];
VASurfaceAttribExternalBuffers extBuffer;
uint32_t base_addr_align = 0x1000;
//VAAppSurfaceResInfo surfaceinfo;
//va_status = vaQuerySurfaceAllocation(va_dpy, format, fourCC,width, height, VA_SURFACE_ATTRIB_MEM_TYPE_USER_PTR, &surfaceinfo);
uint32_t size = 0;
surfaceAttrib[0].flags = VA_SURFACE_ATTRIB_SETTABLE;
surfaceAttrib[0].type = VASurfaceAttribPixelFormat;
surfaceAttrib[0].value.type = VAGenericValueTypeInteger;
surfaceAttrib[0].value.value.i = img_info.fourcc;
surfaceAttrib[1].flags = VA_SURFACE_ATTRIB_SETTABLE;
surfaceAttrib[1].type = VASurfaceAttribMemoryType;
surfaceAttrib[1].value.type = VAGenericValueTypeInteger;
surfaceAttrib[1].value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_USER_PTR;
surfaceAttrib[2].flags = VA_SURFACE_ATTRIB_SETTABLE;
surfaceAttrib[2].type = VASurfaceAttribExternalBufferDescriptor;
surfaceAttrib[2].value.type = VAGenericValueTypePointer;
surfaceAttrib[2].value.value.p = (void *)&extBuffer;
memset(&extBuffer, 0, sizeof(extBuffer));
uint32_t pitch_align = img_info.align_mode;
switch(img_info.fourcc)
{
case VA_FOURCC_NV12:
extBuffer.pitches[0] = ((img_info.pic_width + pitch_align - 1)/pitch_align) * pitch_align;
size = (extBuffer.pitches[0] *img_info.pic_height)*3/2;
size = (size+base_addr_align-1)/base_addr_align*base_addr_align;
extBuffer.offsets[0] = 0;
extBuffer.offsets[1] = extBuffer.pitches[0] * img_info.pic_height;
extBuffer.pitches[1] = extBuffer.pitches[0];
extBuffer.num_planes =2;
break;
case VA_FOURCC_YUY2:
extBuffer.pitches[0] = ((img_info.pic_width + pitch_align - 1)/pitch_align) * pitch_align;
size = (extBuffer.pitches[0] *(img_info.pic_height + 2))*2;
size = (size+base_addr_align-1)/base_addr_align*base_addr_align;
extBuffer.offsets[0] = 0;
extBuffer.pitches[0] = extBuffer.pitches[0] * 2;
extBuffer.num_planes = 1;
break;
case VA_FOURCC_YV12:
{
int y_align = 32;
int uv_align = 16;
extBuffer.pitches[0] = ((img_info.pic_width + y_align - 1)/y_align) * y_align;
extBuffer.pitches[1] = ((img_info.pic_width/2 + uv_align - 1)/uv_align) * uv_align;
extBuffer.pitches[2] = extBuffer.pitches[1];
size = extBuffer.pitches[0] *img_info.pic_height + extBuffer.pitches[1] *img_info.pic_height;
size = (size+base_addr_align-1)/base_addr_align*base_addr_align;
extBuffer.offsets[0] = 0;
extBuffer.offsets[1] = extBuffer.pitches[0] * img_info.pic_height;
extBuffer.offsets[2] = extBuffer.offsets[1] + extBuffer.pitches[1] * img_info.pic_height/2;
extBuffer.num_planes =3;
break;
}
case VA_FOURCC_ARGB:
extBuffer.pitches[0] = ((img_info.pic_width + pitch_align - 1)/pitch_align) * pitch_align;
size = (extBuffer.pitches[0] *(img_info.pic_height + 2))*4;
size = (size+base_addr_align-1)/base_addr_align*base_addr_align;
extBuffer.offsets[0] = 0;
extBuffer.pitches[0] = extBuffer.pitches[0] * 4;
extBuffer.num_planes = 1;
break;
default:
break;
}
img_info.pBuf = malloc(size + base_addr_align);
img_info.pUserBase = (uint8_t*)((((uint64_t)(img_info.pBuf) + base_addr_align-1)/base_addr_align)*base_addr_align);
extBuffer.pixel_format = img_info.fourcc;
extBuffer.width = img_info.pic_width;
extBuffer.height = img_info.pic_height;
extBuffer.data_size = size;
extBuffer.num_buffers = 1;
extBuffer.buffers = &(img_info.ptrb);
extBuffer.buffers[0] = (uintptr_t)(img_info.pUserBase);
extBuffer.flags = VA_SURFACE_ATTRIB_MEM_TYPE_USER_PTR;
va_status = vaCreateSurfaces(va_dpy, img_info.rtformat, img_info.pic_width,img_info.pic_height,p_surface_id, 1, surfaceAttrib, 3);
CHECK_VASTATUS(va_status, "vaCreateSurfaces");
}
return va_status;
}
/* Load yuv frame to NV12/YUY2/YV12/ARGB surface*/
static VAStatus
upload_yuv_frame_to_yuv_surface(FILE *fp,
VASurfaceID surface_id)
{
VAStatus va_status;
VAImage surface_image;
unsigned char *y_src = NULL;
unsigned char *u_src = NULL;
unsigned char *v_src = NULL;
unsigned char *y_dst = NULL;
unsigned char *u_dst = NULL;
unsigned char *v_dst = NULL;
void *surface_p = NULL;
uint32_t frame_size, row;
size_t n_items;
unsigned char * newImageBuffer = NULL;
va_status = vaSyncSurface (va_dpy,surface_id);
CHECK_VASTATUS(va_status, "vaSyncSurface");
va_status = vaDeriveImage(va_dpy, surface_id, &surface_image);
CHECK_VASTATUS(va_status, "vaDeriveImage");
va_status = vaMapBuffer(va_dpy, surface_image.buf, &surface_p);
CHECK_VASTATUS(va_status, "vaMapBuffer");
if (surface_image.format.fourcc == VA_FOURCC_NV12){
frame_size = surface_image.width * surface_image.height * 3 / 2;
newImageBuffer = (unsigned char*)malloc(frame_size);
assert(newImageBuffer);
do {
n_items = fread(newImageBuffer, frame_size, 1, fp);
} while (n_items != 1);
y_src = newImageBuffer;
u_src = newImageBuffer + surface_image.width * surface_image.height;
y_dst = (unsigned char *)((unsigned char*)surface_p + surface_image.offsets[0]);
u_dst = (unsigned char *)((unsigned char*)surface_p + surface_image.offsets[1]);
/* Y plane, directly copy */
for (row = 0; row < surface_image.height; row++) {
memcpy(y_dst, y_src, surface_image.width);
y_dst += surface_image.pitches[0];
y_src += surface_image.width;
}
/* UV plane */
for (row = 0; row < surface_image.height / 2; row++) {
memcpy(u_dst, u_src, surface_image.width);
u_src += surface_image.width;
u_dst += surface_image.pitches[1];
}
} else if (surface_image.format.fourcc == VA_FOURCC_YUY2) {
frame_size = surface_image.width * surface_image.height * 2;
newImageBuffer = (unsigned char*)malloc(frame_size);
assert(newImageBuffer);
do {
n_items = fread(newImageBuffer, frame_size, 1, fp);
} while (n_items != 1);
y_src = newImageBuffer;
y_dst = (unsigned char *)((unsigned char*)surface_p + surface_image.offsets[0]);
/* plane 0, directly copy */
for (row = 0; row < surface_image.height; row++) {
memcpy(y_dst, y_src, surface_image.width * 2);
y_src += surface_image.width * 2;
y_dst += surface_image.pitches[0];
}
}else if (surface_image.format.fourcc == VA_FOURCC_YV12) {
frame_size = surface_image.width * surface_image.height * 3 / 2;
newImageBuffer = (unsigned char*)malloc(frame_size);
assert(newImageBuffer);
do {
n_items = fread(newImageBuffer, frame_size, 1, fp);
} while (n_items != 1);
y_src = newImageBuffer;
v_src = newImageBuffer + surface_image.width * surface_image.height;
u_src = newImageBuffer + surface_image.width * surface_image.height * 5 / 4;
y_dst = (unsigned char *)((unsigned char*)surface_p + surface_image.offsets[0]);
v_dst = (unsigned char *)((unsigned char*)surface_p + surface_image.offsets[1]);
u_dst = (unsigned char *)((unsigned char*)surface_p + surface_image.offsets[2]);
/* Y plane, directly copy */
for (row = 0; row < surface_image.height; row++) {
memcpy(y_dst, y_src, surface_image.width);
y_dst += surface_image.pitches[0];
y_src += surface_image.width;
}
for (row = 0; row < surface_image.height /2; row ++){
memcpy(v_dst, v_src, surface_image.width/2);
memcpy(u_dst, u_src, surface_image.width/2);
v_src += surface_image.width/2;
u_src += surface_image.width/2;
v_dst += surface_image.pitches[1];
u_dst += surface_image.pitches[2];
}
}else if (surface_image.format.fourcc == VA_FOURCC_ARGB) {
frame_size = surface_image.width * surface_image.height * 4;
newImageBuffer = (unsigned char*)malloc(frame_size);
assert(newImageBuffer);
do {
n_items = fread(newImageBuffer, frame_size, 1, fp);
} while (n_items != 1);
y_src = newImageBuffer;
y_dst = (unsigned char *)((unsigned char*)surface_p + surface_image.offsets[0]);
/* plane 0, directly copy */
for (row = 0; row < surface_image.height; row++) {
memcpy(y_dst, y_src, surface_image.width * 4);
y_src += surface_image.width * 4;
y_dst += surface_image.pitches[0];
}
}
if (newImageBuffer){
free(newImageBuffer);
newImageBuffer = NULL;
}
va_status = vaUnmapBuffer(va_dpy, surface_image.buf);
CHECK_VASTATUS(va_status, "vaUnmapBuffer");
va_status = vaDestroyImage(va_dpy, surface_image.image_id);
CHECK_VASTATUS(va_status, "vaDestroyImage");
return VA_STATUS_SUCCESS;
}
static VAStatus
store_yuv_surface_to_nv12_file(FILE *fp,
VASurfaceID surface_id)
{
VAStatus va_status;
VAImage surface_image;
void *surface_p = NULL;
unsigned char *y_src = NULL;
unsigned char *u_src = NULL;
unsigned char *y_dst = NULL;
unsigned char *u_dst = NULL;
uint32_t row;
int32_t n_items;
unsigned char * newImageBuffer = NULL;
va_status = vaDeriveImage(va_dpy, surface_id, &surface_image);
CHECK_VASTATUS(va_status, "vaDeriveImage");
va_status = vaMapBuffer(va_dpy, surface_image.buf, &surface_p);
CHECK_VASTATUS(va_status, "vaMapBuffer");
/* store the surface to one nv12 file */
uint32_t y_size = surface_image.width * surface_image.height;
newImageBuffer = (unsigned char*)malloc(y_size * 3 / 2);
assert(newImageBuffer);
/* stored as YV12 format */
y_dst = newImageBuffer;
u_dst = newImageBuffer + y_size;
y_src = (unsigned char *)((unsigned char*)surface_p + surface_image.offsets[0]);
u_src = (unsigned char *)((unsigned char*)surface_p + surface_image.offsets[1]);
/* Y plane copy */
for (row = 0; row < surface_image.height; row++) {
memcpy(y_dst, y_src, surface_image.width);
y_src += surface_image.pitches[0];
y_dst += surface_image.width;
}
/* UV plane copy */
for (row = 0; row < surface_image.height / 2; row++) {
memcpy(u_dst, u_src, surface_image.width);
u_dst += surface_image.width;
u_src += surface_image.pitches[1];
}
/* write frame to file */
do {
n_items = fwrite(newImageBuffer, y_size * 3 / 2, 1, fp);
} while (n_items != 1);
if (newImageBuffer){
free(newImageBuffer);
newImageBuffer = NULL;
}
va_status = vaUnmapBuffer(va_dpy, surface_image.buf);
CHECK_VASTATUS(va_status, "vaUnmapBuffer");
va_status = vaDestroyImage(va_dpy, surface_image.image_id);
CHECK_VASTATUS(va_status, "vaDestroyImage");
return VA_STATUS_SUCCESS;
}
static VAStatus
store_packed_yuv_surface_to_yuy2_file(FILE *fp,
VASurfaceID surface_id)
{
VAStatus va_status;
VAImage surface_image;
void *surface_p = NULL;
unsigned char *y_src = NULL;
unsigned char *y_dst = NULL;
uint32_t row;
int32_t n_items;
unsigned char * newImageBuffer = NULL;
va_status = vaDeriveImage(va_dpy, surface_id, &surface_image);
CHECK_VASTATUS(va_status, "vaDeriveImage");
va_status = vaMapBuffer(va_dpy, surface_image.buf, &surface_p);
CHECK_VASTATUS(va_status, "vaMapBuffer");
/* store the surface to one YUY2 file */
uint32_t frame_size = surface_image.width * surface_image.height * 2;
newImageBuffer = (unsigned char*)malloc(frame_size);
assert(newImageBuffer);
memset(newImageBuffer, 0, frame_size);
/* stored as YUY2 or UYVY format */
y_dst = newImageBuffer;
y_src = (unsigned char *)((unsigned char*)surface_p + surface_image.offsets[0]);
/* Plane 0 copy */
for (row = 0; row < surface_image.height; row++) {
memcpy(y_dst, y_src, surface_image.width * 2);
y_src += surface_image.pitches[0];
y_dst += surface_image.width * 2;
}
/* write frame to file */
do {
n_items = fwrite(newImageBuffer, frame_size, 1, fp);
} while (n_items != 1);
if (newImageBuffer){
free(newImageBuffer);
newImageBuffer = NULL;
}
va_status = vaUnmapBuffer(va_dpy, surface_image.buf);
CHECK_VASTATUS(va_status, "vaUnmapBuffer");
va_status = vaDestroyImage(va_dpy, surface_image.image_id);
CHECK_VASTATUS(va_status, "vaDestroyImage");
return VA_STATUS_SUCCESS;
}
/* Store YV12 surface to yv12 file */
static VAStatus
store_yuv_surface_to_yv12_file(FILE *fp,
VASurfaceID surface_id)
{
VAStatus va_status;
VAImage surface_image;
void *surface_p = NULL;
unsigned char *y_src, *u_src, *v_src;
unsigned char *y_dst, *u_dst, *v_dst;
uint32_t row;
int32_t n_items;
unsigned char * newImageBuffer = NULL;
va_status = vaDeriveImage(va_dpy, surface_id, &surface_image);
CHECK_VASTATUS(va_status, "vaDeriveImage");
va_status = vaMapBuffer(va_dpy, surface_image.buf, &surface_p);
CHECK_VASTATUS(va_status, "vaMapBuffer");
uint32_t y_size = surface_image.width * surface_image.height;
uint32_t u_size = y_size/4;
newImageBuffer = (unsigned char*)malloc(y_size * 3 / 2);
assert(newImageBuffer);
/* stored as YV12 format */
y_dst = newImageBuffer;
v_dst = newImageBuffer + y_size;
u_dst = newImageBuffer + y_size + u_size;
y_src = (unsigned char *)((unsigned char*)surface_p + surface_image.offsets[0]);
v_src = (unsigned char *)((unsigned char*)surface_p + surface_image.offsets[1]);
u_src = (unsigned char *)((unsigned char*)surface_p + surface_image.offsets[2]);
/* Y plane copy */
for (row = 0; row < surface_image.height; row++) {
memcpy(y_dst, y_src, surface_image.width);
y_src += surface_image.pitches[0];
y_dst += surface_image.width;
}
/* UV plane copy */
for (row = 0; row < surface_image.height /2; row ++){
memcpy(v_dst, v_src, surface_image.width/2);
memcpy(u_dst, u_src, surface_image.width/2);
v_dst += surface_image.width/2;
u_dst += surface_image.width/2;
v_src += surface_image.pitches[1];
u_src += surface_image.pitches[2];
}
/* write frame to file */
do {
n_items = fwrite(newImageBuffer, y_size * 3 / 2, 1, fp);
} while (n_items != 1);
if (newImageBuffer){
free(newImageBuffer);
newImageBuffer = NULL;
}
vaUnmapBuffer(va_dpy, surface_image.buf);
vaDestroyImage(va_dpy, surface_image.image_id);
return VA_STATUS_SUCCESS;
}
static VAStatus
store_rgb_surface_to_rgb_file(FILE *fp, VASurfaceID surface_id)
{
VAStatus va_status;
VAImage surface_image;
void *surface_p = NULL;
unsigned char *y_src;
unsigned char *y_dst;
uint32_t frame_size, row;
int32_t n_items;
unsigned char * newImageBuffer = NULL;
va_status = vaDeriveImage(va_dpy, surface_id, &surface_image);
CHECK_VASTATUS(va_status, "vaDeriveImage");
va_status = vaMapBuffer(va_dpy, surface_image.buf, &surface_p);
CHECK_VASTATUS(va_status, "vaMapBuffer");
frame_size = surface_image.width * surface_image.height * 4;
newImageBuffer = (unsigned char*)malloc(frame_size);
assert(newImageBuffer);
y_dst = newImageBuffer;
y_src = (unsigned char *)((unsigned char*)surface_p + surface_image.offsets[0]);
for (row = 0; row < surface_image.height; row++) {
memcpy(y_dst, y_src, surface_image.width * 4);
y_src += surface_image.pitches[0];
y_dst += surface_image.width * 4;
}
/* write frame to file */
do {
n_items = fwrite(newImageBuffer, frame_size, 1, fp);
} while (n_items != 1);
if (newImageBuffer){
free(newImageBuffer);
newImageBuffer = NULL;
}
vaUnmapBuffer(va_dpy, surface_image.buf);
vaDestroyImage(va_dpy, surface_image.image_id);
return VA_STATUS_SUCCESS;
}
static VAStatus
store_yuv_surface_to_file(FILE *fp,
VASurfaceID surface_id,uint32_t out_fourcc)
{
if (out_fourcc == VA_FOURCC_NV12) {
return store_yuv_surface_to_nv12_file(fp, surface_id);
} else if (out_fourcc == VA_FOURCC_YUY2) {
return store_packed_yuv_surface_to_yuy2_file(fp, surface_id);
} else if (out_fourcc == VA_FOURCC_YV12) {
return store_yuv_surface_to_yv12_file(fp, surface_id);
} else if (out_fourcc == VA_FOURCC_ARGB) {
return store_rgb_surface_to_rgb_file(fp, surface_id);
} else {
printf("Not supported YUV fourcc for output !!!\n");
return VA_STATUS_ERROR_INVALID_SURFACE;
}
}
static VAStatus
video_frame_process( VASurfaceID in_surface_id,
VASurfaceID *out_surface_ids)
{
VAStatus va_status;
VAProcPipelineParameterBuffer pipeline_param;
VARectangle surface_region, output_region;
VABufferID pipeline_param_buf_id = VA_INVALID_ID;
/* Fill pipeline buffer */
memset(&pipeline_param, 0, sizeof(pipeline_param));
pipeline_param.surface = in_surface_id;
if(g_dst_count > 1 && g_scale_again == 0)
{
pipeline_param.additional_outputs = &out_surface_ids[1];
pipeline_param.num_additional_outputs = g_dst_count -1;
}
uint32_t input_crop = 0;
read_value_uint32(g_config_file_fd, "SRC_SURFACE_CROP", &input_crop);
if(input_crop == 0){
surface_region.x = 0;
surface_region.y = 0;
surface_region.width = g_src_info.pic_width;
surface_region.height = g_src_info.pic_height;
}else{
//do the input crop
read_value_int16(g_config_file_fd, "SRC_CROP_LEFT_X", &surface_region.x);
read_value_int16(g_config_file_fd, "SRC_CROP_TOP_Y", &surface_region.y);
read_value_uint16(g_config_file_fd, "SRC_CROP_WIDTH", &surface_region.width);
read_value_uint16(g_config_file_fd, "SRC_CROP_HEIGHT", &surface_region.height);
}
uint32_t output_crop = 0;
read_value_uint32(g_config_file_fd, "DST_SURFACE_CROP", &output_crop);
if(output_crop == 0){
output_region.x = 0;
output_region.y = 0;
output_region.width = g_dst_info[0].pic_width;
output_region.height = g_dst_info[0].pic_height;
}else{
//do the output crop
read_value_int16(g_config_file_fd, "DST_CROP_LEFT_X", &output_region.x);
read_value_int16(g_config_file_fd, "DST_CROP_TOP_Y", &output_region.y);
read_value_uint16(g_config_file_fd, "DST_CROP_WIDTH", &output_region.width);
read_value_uint16(g_config_file_fd, "DST_CROP_HEIGHT", &output_region.height);
}
pipeline_param.surface_region = &surface_region;
pipeline_param.output_region = &output_region;
pipeline_param.output_background_color = 0xff000000;
va_status = vaCreateBuffer(va_dpy,
context_id,
VAProcPipelineParameterBufferType,
sizeof(pipeline_param),
1,
&pipeline_param,
&pipeline_param_buf_id);
CHECK_VASTATUS(va_status, "vaCreateBuffer");
va_status = vaBeginPicture(va_dpy,
context_id,
out_surface_ids[0]);
CHECK_VASTATUS(va_status, "vaBeginPicture");
va_status = vaRenderPicture(va_dpy,
context_id,
&pipeline_param_buf_id,
1);
CHECK_VASTATUS(va_status, "vaRenderPicture");
va_status = vaEndPicture(va_dpy, context_id);
CHECK_VASTATUS(va_status, "vaEndPicture");
if (pipeline_param_buf_id != VA_INVALID_ID){
vaDestroyBuffer(va_dpy,pipeline_param_buf_id);
CHECK_VASTATUS(va_status, "vaDestroyBuffer");
}
if(g_scale_again == 1)
{
printf("begin to scale the 16align out as input\n");
memset(&pipeline_param, 0, sizeof(pipeline_param));
pipeline_param.surface = out_surface_ids[0];
va_status = vaCreateBuffer(va_dpy,
context_id,
VAProcPipelineParameterBufferType,
sizeof(pipeline_param),
1,
&pipeline_param,
&pipeline_param_buf_id);
CHECK_VASTATUS(va_status, "vaCreateBuffer");
va_status = vaBeginPicture(va_dpy, context_id, out_surface_ids[1]);
CHECK_VASTATUS(va_status, "vaBeginPicture");
va_status = vaRenderPicture(va_dpy, context_id,&pipeline_param_buf_id, 1);
CHECK_VASTATUS(va_status, "vaRenderPicture");
va_status = vaEndPicture(va_dpy, context_id);
CHECK_VASTATUS(va_status, "vaEndPicture");
if (pipeline_param_buf_id != VA_INVALID_ID){
vaDestroyBuffer(va_dpy,pipeline_param_buf_id);
CHECK_VASTATUS(va_status, "vaDestroyBuffer");
}
}
return va_status;
}
static VAStatus
vpp_context_create()
{
VAStatus va_status = VA_STATUS_SUCCESS;
int32_t j;
/* VA driver initialization */
#if 0
x11_display = XOpenDisplay("intel-CoffeeLake-Client-Platform:2");
if (NULL == x11_display) {
printf("Error: Can't connect X server! %s %s(line %d)\n", __FILE__, __func__, __LINE__);
return false;
}
va_dpy = vaGetDisplay(x11_display);
#endif
va_dpy = va_open_display();
int32_t major_ver, minor_ver;
va_status = vaInitialize(va_dpy, &major_ver, &minor_ver);
assert(va_status == VA_STATUS_SUCCESS);
/* Check whether VPP is supported by driver */
VAEntrypoint entrypoints[5];
int32_t num_entrypoints;
num_entrypoints = vaMaxNumEntrypoints(va_dpy);
va_status = vaQueryConfigEntrypoints(va_dpy,
VAProfileNone,
entrypoints,
&num_entrypoints);
CHECK_VASTATUS(va_status, "vaQueryConfigEntrypoints");
for (j = 0; j < num_entrypoints; j++) {
if (entrypoints[j] == VAEntrypointVideoProc)
break;
}
if (j == num_entrypoints) {
printf("VPP is not supported by driver\n");
assert(0);
}
/* Create surface/config/context for VPP pipeline */
va_status = create_surface(g_src_info,&g_in_surface_id);
CHECK_VASTATUS(va_status, "vaCreateSurfaces for input");
for(uint32_t i = 0; i< g_dst_count; i++)
{
va_status = create_surface(g_dst_info[i],&g_out_surface_ids[i]);
}
CHECK_VASTATUS(va_status, "vaCreateSurfaces for output");
VAConfigAttrib attrib;
attrib.type = VAConfigAttribRTFormat;
attrib.value = VA_RT_FORMAT_YUV420;
va_status = vaCreateConfig(va_dpy,
VAProfileNone,
VAEntrypointVideoProc,
&attrib,
1,
&config_id);
CHECK_VASTATUS(va_status, "vaCreateConfig");
va_status = vaCreateContext(va_dpy,
config_id,
g_dst_info[0].pic_width,
g_dst_info[0].pic_height,
VA_PROGRESSIVE,
g_out_surface_ids,
g_dst_count,
&context_id);
CHECK_VASTATUS(va_status, "vaCreateContext");
return va_status;
}
static void
vpp_context_destroy()
{
VAStatus va_status;
/* Release resource */
va_status = vaDestroySurfaces(va_dpy, &g_in_surface_id, 1);
CHECK_VASTATUS(va_status, "vaDestroySurfaces");
VPP_FREE(g_src_info.pBuf);
for(uint32_t index = 0; index < g_dst_count; index++){
vaDestroySurfaces(va_dpy, &g_out_surface_ids[index], 1);
CHECK_VASTATUS(va_status, "vaDestroySurfaces");
VPP_FREE(g_dst_info[index].pBuf);
}
VPP_FREE(g_dst_info);
VPP_FREE(g_out_surface_ids);
vaDestroyContext(va_dpy, context_id);
CHECK_VASTATUS(va_status, "vaDestroyContext");
vaDestroyConfig(va_dpy, config_id);
CHECK_VASTATUS(va_status, "vaDestroyConfig");
vaTerminate(va_dpy);
CHECK_VASTATUS(va_status, "vaTerminate");
#if 0
XCloseDisplay(x11_display);
x11_display = NULL;
#endif
va_close_display(va_dpy);
}
static int8_t
parse_fourcc_and_format(char *str, uint32_t *fourcc, uint32_t *format)
{
uint32_t tfourcc = VA_FOURCC('N', 'V', '1', '2');
uint32_t tformat = VA_RT_FORMAT_YUV420;
if (!strcmp(str, "NV12")){
tfourcc = VA_FOURCC('N', 'V', '1', '2');
} else if(!strcmp(str, "YUY2")) {
tfourcc = VA_FOURCC('Y', 'U', 'Y', '2');
} else if(!strcmp(str, "YV12")) {
tfourcc = VA_FOURCC('Y', 'V', '1', '2');
}else if(!strcmp(str, "ARGB")) {
tfourcc = VA_FOURCC('A', 'R', 'G', 'B');
}else{
printf("Not supported format: %s! Currently only support following format: %s for this sample\n",
str, "NV12, YUY2,YV12,ARGB");
assert(0);
}
if (fourcc)
*fourcc = tfourcc;
if (format)
*format = tformat;
return 0;
}
static int8_t
parse_memtype_format(char *str,uint32_t *dst_memtype)
{
uint32_t tmemtype = VA_SURFACE_ATTRIB_MEM_TYPE_VA;
if (!strcmp(str, "VA")){
tmemtype = VA_SURFACE_ATTRIB_MEM_TYPE_VA;
} else if(!strcmp(str, "CPU")){
tmemtype = VA_SURFACE_ATTRIB_MEM_TYPE_USER_PTR;
}else{
printf("Not supported format: %s! Currently only support following format: %s\n",
str, "VA,CPU");
assert(0);
}
if(dst_memtype)
*dst_memtype = tmemtype;
return 0;
}
static int8_t
parse_basic_parameters()
{
char str[MAX_LEN];
/* Read src frame file information */
read_value_string(g_config_file_fd, "SRC_FILE_NAME", g_src_info.file_name);
read_value_uint32(g_config_file_fd, "SRC_FRAME_WIDTH", &g_src_info.pic_width);
read_value_uint32(g_config_file_fd, "SRC_FRAME_HEIGHT", &g_src_info.pic_height);
read_value_string(g_config_file_fd, "SRC_FRAME_FORMAT", str);
parse_fourcc_and_format(str, &g_src_info.fourcc, &g_src_info.rtformat);
read_value_string(g_config_file_fd, "SRC_SURFACE_MEMORY_TYPE", str);
parse_memtype_format(str,&g_src_info.memtype);
read_value_uint32(g_config_file_fd, "SRC_SURFACE_CPU_ALIGN_MODE", &g_src_info.align_mode);
read_value_uint32(g_config_file_fd, "2ND_SCALE", &g_scale_again);
/* Read dst frame file information */
read_value_uint32(g_config_file_fd, "DST_NUMBER", &g_dst_count);
g_out_surface_ids = (VASurfaceID*)malloc(g_dst_count * sizeof(VASurfaceID));
g_dst_info = (VPP_ImageInfo *)malloc(g_dst_count * sizeof(VPP_ImageInfo));
for(uint32_t i = 0; i< g_dst_count; i++)
{
char dst_file_name[MAX_LEN];
char dst_frame_width[MAX_LEN];
char dst_frame_height[MAX_LEN];
char dst_frame_format[MAX_LEN];
char dst_memtype[MAX_LEN];
char dst_align_mode[MAX_LEN];
sprintf(dst_file_name,"DST_FILE_NAME_%d",i+1);
sprintf(dst_frame_width,"DST_FRAME_WIDTH_%d",i+1);
sprintf(dst_frame_height,"DST_FRAME_HEIGHT_%d",i+1);
sprintf(dst_frame_format,"DST_FRAME_FORMAT_%d",i+1);
sprintf(dst_memtype,"DST_SURFACE_MEMORY_TYPE_%d",i+1);
sprintf(dst_align_mode,"DST_SURFACE_CPU_ALIGN_MODE_%d",i+1);
read_value_string(g_config_file_fd, dst_file_name, g_dst_info[i].file_name);
read_value_uint32(g_config_file_fd, dst_frame_width, &g_dst_info[i].pic_width);
read_value_uint32(g_config_file_fd, dst_frame_height,&g_dst_info[i].pic_height);
read_value_string(g_config_file_fd, dst_frame_format, str);
parse_fourcc_and_format(str, &g_dst_info[i].fourcc, &g_dst_info[i].rtformat);
read_value_string(g_config_file_fd, dst_memtype, str);
parse_memtype_format(str,&g_dst_info[i].memtype);
read_value_uint32(g_config_file_fd, dst_align_mode, &g_dst_info[i].align_mode);
}
read_value_uint32(g_config_file_fd, "FRAME_SUM", &g_frame_count);
return 0;
}
static void
print_help()
{
printf("The app is used to test the usrptr and 1:N output and crop feature.\n");
printf("Cmd Usage: ./vppscaling_n_out_usrptr process_scaling_n_out_usrptr.cfg\n");
printf("The configure file process_scaling_n_out_usrptr.cfg is used to configure the para.\n");
printf("You can refer process_scaling_n_out_usrptr.cfg.template for each para meaning and create the configure file.\n");
}
int32_t main(int32_t argc, char *argv[])
{
VAStatus va_status;
uint32_t i;
if (argc != 2 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")){
print_help();
return -1;
}
/* Parse the configure file for video process*/
strncpy(g_config_file_name, argv[1], MAX_LEN);
g_config_file_name[MAX_LEN - 1] = '\0';
if (NULL == (g_config_file_fd = fopen(g_config_file_name, "r"))){
printf("Open configure file %s failed!\n",g_config_file_name);
assert(0);
}
/* Parse basic parameters */
if (parse_basic_parameters()){
printf("Parse parameters in configure file error\n");
assert(0);
}
va_status = vpp_context_create();
if (va_status != VA_STATUS_SUCCESS) {
printf("vpp context create failed \n");
assert(0);
}
/* Video frame fetch, process and store */
if (NULL == (g_src_info.file_fd = fopen(g_src_info.file_name, "r"))){
printf("Open SRC_FILE_NAME: %s failed, please specify it in config file: %s !\n",
g_src_info.file_name, g_config_file_name);
assert(0);
}
for(uint32_t index = 0; index < g_dst_count; index++){
if (NULL == (g_dst_info[index].file_fd = fopen(g_dst_info[index].file_name, "w"))){
printf("Open DST_FILE_NAME: %s failed, please specify it in config file: %s !\n",
g_dst_info[index].file_name, g_config_file_name);
assert(0);
}
}
printf("\nStart to process, ...\n");
struct timespec Pre_time;
struct timespec Cur_time;
unsigned int duration = 0;
clock_gettime(CLOCK_MONOTONIC, &Pre_time);
for (i = 0; i < g_frame_count; i ++){
upload_yuv_frame_to_yuv_surface(g_src_info.file_fd,g_in_surface_id);
video_frame_process(g_in_surface_id, g_out_surface_ids);
//first sync surface to check the process ready
va_status = vaSyncSurface (va_dpy,g_out_surface_ids[g_dst_count - 1]);
CHECK_VASTATUS(va_status, "vaSyncSurface");
for(uint32_t index = 0; index < g_dst_count; index++){
store_yuv_surface_to_file(g_dst_info[index].file_fd, g_out_surface_ids[index],g_dst_info[index].fourcc);
}
}
clock_gettime(CLOCK_MONOTONIC, &Cur_time);
duration = (Cur_time.tv_sec - Pre_time.tv_sec) * 1000;
if (Cur_time.tv_nsec > Pre_time.tv_nsec) {
duration += (Cur_time.tv_nsec - Pre_time.tv_nsec) / 1000000;
} else {
duration += (Cur_time.tv_nsec + 1000000000 - Pre_time.tv_nsec) / 1000000 - 1000;
}
printf("Finish processing, performance: \n" );
printf("%d frames processed in: %d ms, ave time = %d ms\n",g_frame_count, duration, duration/g_frame_count);
if (g_src_info.file_fd)
fclose(g_src_info.file_fd);
for(uint32_t index = 0; index < g_dst_count; index++){
if (g_dst_info[index].file_fd)
fclose(g_dst_info[index].file_fd);
}
if (g_config_file_fd)
fclose(g_config_file_fd);
vpp_context_destroy();
return 0;
}
|