Skip to content
Permalink
Browse files

avutil/hwcontext_cuda: use same alignment logic everywhere

  • Loading branch information
BtbN committed May 9, 2018
1 parent b7f04a8 commit 6a25397a86b636c98bac3dd0c4432ea916a103e7
Showing with 26 additions and 30 deletions.
  1. +26 −30 libavutil/hwcontext_cuda.c
@@ -114,10 +114,16 @@ static AVBufferRef *cuda_pool_alloc(void *opaque, int size)
return ret;
}

static int calc_linesize(int width, enum AVPixelFormat pix_fmt)
{
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
return FFALIGN(width * desc->comp[0].step, CUDA_FRAME_ALIGNMENT);
}

static int cuda_frames_init(AVHWFramesContext *ctx)
{
CUDAFramesContext *priv = ctx->internal->priv;
int aligned_width = FFALIGN(ctx->width, CUDA_FRAME_ALIGNMENT);
int linesize = calc_linesize(ctx->width, ctx->sw_format);
int i;

for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++) {
@@ -138,19 +144,17 @@ static int cuda_frames_init(AVHWFramesContext *ctx)
switch (ctx->sw_format) {
case AV_PIX_FMT_NV12:
case AV_PIX_FMT_YUV420P:
size = aligned_width * ctx->height * 3 / 2;
break;
case AV_PIX_FMT_YUV444P:
case AV_PIX_FMT_P010:
case AV_PIX_FMT_P016:
size = aligned_width * ctx->height * 3;
size = linesize * ctx->height * 3 / 2;
break;
case AV_PIX_FMT_YUV444P:
case AV_PIX_FMT_YUV444P16:
size = aligned_width * ctx->height * 6;
size = linesize * ctx->height * 3;
break;
case AV_PIX_FMT_0RGB32:
case AV_PIX_FMT_0BGR32:
size = aligned_width * ctx->height * 4;
size = linesize * ctx->height;
break;
default:
av_log(ctx, AV_LOG_ERROR, "BUG: Pixel format missing from size calculation.");
@@ -167,15 +171,7 @@ static int cuda_frames_init(AVHWFramesContext *ctx)

static int cuda_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
{
int aligned_width;
int width_in_bytes = ctx->width;

if (ctx->sw_format == AV_PIX_FMT_P010 ||
ctx->sw_format == AV_PIX_FMT_P016 ||
ctx->sw_format == AV_PIX_FMT_YUV444P16) {
width_in_bytes *= 2;
}
aligned_width = FFALIGN(width_in_bytes, CUDA_FRAME_ALIGNMENT);
int linesize = calc_linesize(ctx->width, ctx->sw_format);

frame->buf[0] = av_buffer_pool_get(ctx->pool);
if (!frame->buf[0])
@@ -186,31 +182,31 @@ static int cuda_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
case AV_PIX_FMT_P010:
case AV_PIX_FMT_P016:
frame->data[0] = frame->buf[0]->data;
frame->data[1] = frame->data[0] + aligned_width * ctx->height;
frame->linesize[0] = aligned_width;
frame->linesize[1] = aligned_width;
frame->data[1] = frame->data[0] + linesize * ctx->height;
frame->linesize[0] = linesize;
frame->linesize[1] = linesize;
break;
case AV_PIX_FMT_YUV420P:
frame->data[0] = frame->buf[0]->data;
frame->data[2] = frame->data[0] + aligned_width * ctx->height;
frame->data[1] = frame->data[2] + aligned_width * ctx->height / 4;
frame->linesize[0] = aligned_width;
frame->linesize[1] = aligned_width / 2;
frame->linesize[2] = aligned_width / 2;
frame->data[2] = frame->data[0] + linesize * ctx->height;
frame->data[1] = frame->data[2] + linesize * ctx->height / 4;
frame->linesize[0] = linesize;
frame->linesize[1] = linesize / 2;
frame->linesize[2] = linesize / 2;
break;
case AV_PIX_FMT_YUV444P:
case AV_PIX_FMT_YUV444P16:
frame->data[0] = frame->buf[0]->data;
frame->data[1] = frame->data[0] + aligned_width * ctx->height;
frame->data[2] = frame->data[1] + aligned_width * ctx->height;
frame->linesize[0] = aligned_width;
frame->linesize[1] = aligned_width;
frame->linesize[2] = aligned_width;
frame->data[1] = frame->data[0] + linesize * ctx->height;
frame->data[2] = frame->data[1] + linesize * ctx->height;
frame->linesize[0] = linesize;
frame->linesize[1] = linesize;
frame->linesize[2] = linesize;
break;
case AV_PIX_FMT_0BGR32:
case AV_PIX_FMT_0RGB32:
frame->data[0] = frame->buf[0]->data;
frame->linesize[0] = aligned_width * 4;
frame->linesize[0] = linesize;
break;
default:
av_frame_unref(frame);

0 comments on commit 6a25397

Please sign in to comment.