| 216 | } |
| 217 | |
| 218 | int av_image_alloc(uint8_t *pointers[4], int linesizes[4], |
| 219 | int w, int h, enum AVPixelFormat pix_fmt, int align) |
| 220 | { |
| 221 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt); |
| 222 | int i, ret; |
| 223 | ptrdiff_t linesizes1[4]; |
| 224 | size_t total_size, sizes[4]; |
| 225 | uint8_t *buf; |
| 226 | |
| 227 | if (!desc) |
| 228 | return AVERROR(EINVAL); |
| 229 | |
| 230 | if ((ret = av_image_check_size(w, h, 0, NULL)) < 0) |
| 231 | return ret; |
| 232 | if ((ret = av_image_fill_linesizes(linesizes, pix_fmt, align>7 ? FFALIGN(w, 8) : w)) < 0) |
| 233 | return ret; |
| 234 | |
| 235 | for (i = 0; i < 4; i++) { |
| 236 | linesizes[i] = FFALIGN(linesizes[i], align); |
| 237 | linesizes1[i] = linesizes[i]; |
| 238 | } |
| 239 | |
| 240 | if ((ret = av_image_fill_plane_sizes(sizes, pix_fmt, h, linesizes1)) < 0) |
| 241 | return ret; |
| 242 | total_size = align; |
| 243 | for (i = 0; i < 4; i++) { |
| 244 | if (total_size > SIZE_MAX - sizes[i]) |
| 245 | return AVERROR(EINVAL); |
| 246 | total_size += sizes[i]; |
| 247 | } |
| 248 | buf = av_malloc(total_size); |
| 249 | if (!buf) |
| 250 | return AVERROR(ENOMEM); |
| 251 | if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, buf, linesizes)) < 0) { |
| 252 | av_free(buf); |
| 253 | return ret; |
| 254 | } |
| 255 | if (desc->flags & AV_PIX_FMT_FLAG_PAL) { |
| 256 | avpriv_set_systematic_pal2((uint32_t*)pointers[1], pix_fmt); |
| 257 | if (align < 4) { |
| 258 | av_log(NULL, AV_LOG_ERROR, "Formats with a palette require a minimum alignment of 4\n"); |
| 259 | av_free(buf); |
| 260 | return AVERROR(EINVAL); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | if (desc->flags & AV_PIX_FMT_FLAG_PAL && pointers[1] && |
| 265 | pointers[1] - pointers[0] > linesizes[0] * h) { |
| 266 | /* zero-initialize the padding before the palette */ |
| 267 | memset(pointers[0] + linesizes[0] * h, 0, |
| 268 | pointers[1] - pointers[0] - linesizes[0] * h); |
| 269 | } |
| 270 | |
| 271 | return ret; |
| 272 | } |
| 273 | |
| 274 | typedef struct ImgUtils { |
| 275 | const AVClass *class; |