+static int realign_frame(const AVPixFmtDescriptor *desc, AVFrame **frame)
+{
+ AVFrame *aligned = NULL;
+ int ret = 0, plane;
+
+ /* Realign any unaligned input frame. */
+ for (plane = 0; plane < 3; plane++) {
+ int p = desc->comp[plane].plane;
+ if ((uintptr_t)(*frame)->data[p] % ZIMG_ALIGNMENT || (*frame)->linesize[p] % ZIMG_ALIGNMENT) {
+ if (!(aligned = av_frame_alloc())) {
+ ret = AVERROR(ENOMEM);
+ goto fail;
+ }
+
+ aligned->format = (*frame)->format;
+ aligned->width = (*frame)->width;
+ aligned->height = (*frame)->height;
+
+ if ((ret = av_frame_get_buffer(aligned, ZIMG_ALIGNMENT)) < 0)
+ goto fail;
+
+ if ((ret = av_frame_copy(aligned, *frame)) < 0)
+ goto fail;
+
+ if ((ret = av_frame_copy_props(aligned, *frame)) < 0)
+ goto fail;
+
+ av_frame_free(frame);
+ *frame = aligned;
+ return 0;
+ }
+ }
+
+fail:
+ av_frame_free(&aligned);
+ return ret;
+}
+