| 12
 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
 
 | 
 
 
 
 
 
 
 
 
 
 
 
 
 
 __global__ void ReSizeKernel_Nearest_YUV420P(unsigned char* pInYData, unsigned char* pInUData, unsigned char* pInVData, int pInWidth, int pInHeight,
 unsigned char* pOutYData, unsigned char* pOutUData, unsigned char* pOutVData, int pOutWidth, int pOutHeight)
 {
 int tidx = threadIdx.x + blockDim.x * blockIdx.x;
 int tidy = threadIdx.y + blockDim.y * blockIdx.y;
 
 if (tidx < pOutWidth && tidy < pOutHeight)
 {
 int srcX = tidx * ((float)(pInWidth - 1) / (pOutWidth - 1));
 int srcY = tidy * ((float)(pInHeight - 1) / (pOutHeight - 1));
 
 int idx_in_y = srcY * pInWidth + srcX;
 int idx_in_uv = srcY / 2 * pInWidth / 2 + srcX / 2;
 
 int idx_out_y = tidy * pOutWidth + tidx;
 int idx_out_uv = tidy / 2 * pOutWidth / 2 + tidx / 2;
 
 
 pOutYData[idx_out_y] = pInYData[idx_in_y];
 
 pOutUData[idx_out_uv] = pInUData[idx_in_uv];
 
 pOutVData[idx_out_uv] = pInVData[idx_in_uv];
 }
 }
 
 
 
 
 
 
 
 
 AVFrame* ReSize_Nearest_YUV420P(AVFrame* frame, int width, int height)
 {
 auto img_size_y = width * height * sizeof(unsigned char);
 auto img_size_uv = (width / 2) * (height / 2) * sizeof(unsigned char);
 
 AVFrame* dstImg;
 unsigned char* outputY = nullptr;
 unsigned char* outputU = nullptr;
 unsigned char* outputV = nullptr;
 
 dstImg = av_frame_alloc();
 av_image_alloc(dstImg->data, dstImg->linesize, width, height, (AVPixelFormat)frame->format, 1);
 dstImg->width = width;
 dstImg->height = height;
 dstImg->format = (AVPixelFormat)frame->format;
 
 cudaMalloc(&outputY, img_size_y);
 cudaMalloc(&outputU, img_size_uv);
 cudaMalloc(&outputV, img_size_uv);
 
 dim3 block(32, 32);
 dim3 grid((width + block.x - 1) / block.x, (height + block.y - 1) / block.y);
 ReSizeKernel_Nearest_YUV420P << <grid, block >> > (frame->data[0], frame->data[1], frame->data[2], frame->width, frame->height, outputY, outputU, outputV, width, height);
 cudaThreadSynchronize();
 
 
 cudaMemcpy(dstImg->data[0], outputY, img_size_y, cudaMemcpyDeviceToHost);
 cudaMemcpy(dstImg->data[1], outputU, img_size_uv, cudaMemcpyDeviceToHost);
 cudaMemcpy(dstImg->data[2], outputV, img_size_uv, cudaMemcpyDeviceToHost);
 return dstImg;
 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 __global__ void ReSizeKernel_Nearest_NV12(unsigned char* pInYData, unsigned char* pInUVData, int pInWidth, int pInHeight,
 unsigned char* pOutYData, unsigned char* pOutUVData, int pOutWidth, int pOutHeight)
 {
 int tidx = threadIdx.x + blockDim.x * blockIdx.x;
 int tidy = threadIdx.y + blockDim.y * blockIdx.y;
 
 if (tidx < pOutWidth && tidy < pOutHeight)
 {
 int srcX = tidx * ((float)(pInWidth - 1) / (pOutWidth - 1));
 int srcY = tidy * ((float)(pInHeight - 1) / (pOutHeight - 1));
 
 int idx_in_y = srcY * pInWidth + srcX;
 int idx_in_uv = srcY / 2 * pInWidth + srcX;
 
 int idx_out_y = tidy * pOutWidth + tidx;
 int idx_out_uv = tidy / 2 * pOutWidth + tidx;
 
 
 pOutYData[idx_out_y] = pInYData[idx_in_y];
 
 pOutUVData[tidx % 2 == 0 ? idx_out_uv : idx_out_uv - 1] = pInUVData[srcX % 2 == 0 ? idx_in_uv : idx_in_uv - 1];
 
 pOutUVData[tidx % 2 == 0 ? idx_out_uv + 1 : idx_out_uv] = pInUVData[srcX % 2 == 0 ? idx_in_uv + 1 : idx_in_uv];
 }
 }
 
 
 
 
 
 
 
 
 AVFrame* ReSize_Nearest_NV12(AVFrame* frame, int width, int height)
 {
 auto img_size_y = width * height * sizeof(unsigned char);
 auto img_size_uv = width * (height / 2) * sizeof(unsigned char);
 
 AVFrame* dstImg;
 unsigned char* outputY = nullptr;
 unsigned char* outputUV = nullptr;
 
 dstImg = av_frame_alloc();
 av_image_alloc(dstImg->data, dstImg->linesize, width, height, (AVPixelFormat)frame->format, 1);
 dstImg->width = width;
 dstImg->height = height;
 dstImg->format = (AVPixelFormat)frame->format;
 
 cudaMalloc(&outputY, img_size_y);
 cudaMalloc(&outputUV, img_size_uv);
 
 dim3 block(32, 32);
 dim3 grid((width + block.x - 1) / block.x, (height + block.y - 1) / block.y);
 ReSizeKernel_Nearest_NV12 << <grid, block >> > (frame->data[0], frame->data[1], frame->width, frame->height, outputY, outputUV, width, height);
 cudaThreadSynchronize();
 
 
 cudaMemcpy(dstImg->data[0], outputY, img_size_y, cudaMemcpyDeviceToHost);
 cudaMemcpy(dstImg->data[1], outputUV, img_size_uv, cudaMemcpyDeviceToHost);
 return dstImg;
 }
 
 |