Skip to content

Commit 6a25397

Browse files
committed
avutil/hwcontext_cuda: use same alignment logic everywhere
1 parent b7f04a8 commit 6a25397

File tree

1 file changed

+26
-30
lines changed

1 file changed

+26
-30
lines changed

libavutil/hwcontext_cuda.c

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,16 @@ static AVBufferRef *cuda_pool_alloc(void *opaque, int size)
114114
return ret;
115115
}
116116

117+
static int calc_linesize(int width, enum AVPixelFormat pix_fmt)
118+
{
119+
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
120+
return FFALIGN(width * desc->comp[0].step, CUDA_FRAME_ALIGNMENT);
121+
}
122+
117123
static int cuda_frames_init(AVHWFramesContext *ctx)
118124
{
119125
CUDAFramesContext *priv = ctx->internal->priv;
120-
int aligned_width = FFALIGN(ctx->width, CUDA_FRAME_ALIGNMENT);
126+
int linesize = calc_linesize(ctx->width, ctx->sw_format);
121127
int i;
122128

123129
for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++) {
@@ -138,19 +144,17 @@ static int cuda_frames_init(AVHWFramesContext *ctx)
138144
switch (ctx->sw_format) {
139145
case AV_PIX_FMT_NV12:
140146
case AV_PIX_FMT_YUV420P:
141-
size = aligned_width * ctx->height * 3 / 2;
142-
break;
143-
case AV_PIX_FMT_YUV444P:
144147
case AV_PIX_FMT_P010:
145148
case AV_PIX_FMT_P016:
146-
size = aligned_width * ctx->height * 3;
149+
size = linesize * ctx->height * 3 / 2;
147150
break;
151+
case AV_PIX_FMT_YUV444P:
148152
case AV_PIX_FMT_YUV444P16:
149-
size = aligned_width * ctx->height * 6;
153+
size = linesize * ctx->height * 3;
150154
break;
151155
case AV_PIX_FMT_0RGB32:
152156
case AV_PIX_FMT_0BGR32:
153-
size = aligned_width * ctx->height * 4;
157+
size = linesize * ctx->height;
154158
break;
155159
default:
156160
av_log(ctx, AV_LOG_ERROR, "BUG: Pixel format missing from size calculation.");
@@ -167,15 +171,7 @@ static int cuda_frames_init(AVHWFramesContext *ctx)
167171

168172
static int cuda_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
169173
{
170-
int aligned_width;
171-
int width_in_bytes = ctx->width;
172-
173-
if (ctx->sw_format == AV_PIX_FMT_P010 ||
174-
ctx->sw_format == AV_PIX_FMT_P016 ||
175-
ctx->sw_format == AV_PIX_FMT_YUV444P16) {
176-
width_in_bytes *= 2;
177-
}
178-
aligned_width = FFALIGN(width_in_bytes, CUDA_FRAME_ALIGNMENT);
174+
int linesize = calc_linesize(ctx->width, ctx->sw_format);
179175

180176
frame->buf[0] = av_buffer_pool_get(ctx->pool);
181177
if (!frame->buf[0])
@@ -186,31 +182,31 @@ static int cuda_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
186182
case AV_PIX_FMT_P010:
187183
case AV_PIX_FMT_P016:
188184
frame->data[0] = frame->buf[0]->data;
189-
frame->data[1] = frame->data[0] + aligned_width * ctx->height;
190-
frame->linesize[0] = aligned_width;
191-
frame->linesize[1] = aligned_width;
185+
frame->data[1] = frame->data[0] + linesize * ctx->height;
186+
frame->linesize[0] = linesize;
187+
frame->linesize[1] = linesize;
192188
break;
193189
case AV_PIX_FMT_YUV420P:
194190
frame->data[0] = frame->buf[0]->data;
195-
frame->data[2] = frame->data[0] + aligned_width * ctx->height;
196-
frame->data[1] = frame->data[2] + aligned_width * ctx->height / 4;
197-
frame->linesize[0] = aligned_width;
198-
frame->linesize[1] = aligned_width / 2;
199-
frame->linesize[2] = aligned_width / 2;
191+
frame->data[2] = frame->data[0] + linesize * ctx->height;
192+
frame->data[1] = frame->data[2] + linesize * ctx->height / 4;
193+
frame->linesize[0] = linesize;
194+
frame->linesize[1] = linesize / 2;
195+
frame->linesize[2] = linesize / 2;
200196
break;
201197
case AV_PIX_FMT_YUV444P:
202198
case AV_PIX_FMT_YUV444P16:
203199
frame->data[0] = frame->buf[0]->data;
204-
frame->data[1] = frame->data[0] + aligned_width * ctx->height;
205-
frame->data[2] = frame->data[1] + aligned_width * ctx->height;
206-
frame->linesize[0] = aligned_width;
207-
frame->linesize[1] = aligned_width;
208-
frame->linesize[2] = aligned_width;
200+
frame->data[1] = frame->data[0] + linesize * ctx->height;
201+
frame->data[2] = frame->data[1] + linesize * ctx->height;
202+
frame->linesize[0] = linesize;
203+
frame->linesize[1] = linesize;
204+
frame->linesize[2] = linesize;
209205
break;
210206
case AV_PIX_FMT_0BGR32:
211207
case AV_PIX_FMT_0RGB32:
212208
frame->data[0] = frame->buf[0]->data;
213-
frame->linesize[0] = aligned_width * 4;
209+
frame->linesize[0] = linesize;
214210
break;
215211
default:
216212
av_frame_unref(frame);

0 commit comments

Comments
 (0)