static const VSFrameRef *_vszimg_get_frame(struct vszimg_data *data, const VSFrameRef *src_frame, VSCore *core, const VSAPI *vsapi, char *err_msg, size_t err_msg_size) { struct vszimg_graph_data *graph_data = NULL; VSFrameRef *dst_frame = NULL; VSFrameRef *ret = NULL; void *tmp = NULL; struct callback_data unpack_cb_data = { 0 }; struct callback_data pack_cb_data = { 0 }; zimg_image_format src_format; zimg_image_format dst_format; const VSFormat *src_vsformat; const VSFormat *dst_vsformat; const VSMap *src_props; VSMap *dst_props; size_t tmp_size; zimg_image_format_default(&src_format, ZIMG_API_VERSION); zimg_image_format_default(&dst_format, ZIMG_API_VERSION); src_props = vsapi->getFramePropsRO(src_frame); src_vsformat = vsapi->getFrameFormat(src_frame); dst_vsformat = data->vi.format ? data->vi.format : src_vsformat; src_format.width = vsapi->getFrameWidth(src_frame, 0); src_format.height = vsapi->getFrameHeight(src_frame, 0); dst_format.width = data->vi.width ? (unsigned)data->vi.width : src_format.width; dst_format.height = data->vi.height ? (unsigned)data->vi.height : src_format.height; if (translate_vsformat(src_vsformat, &src_format, err_msg)) goto fail; if (translate_vsformat(dst_vsformat, &dst_format, err_msg)) goto fail; _vszimg_set_src_colorspace(data, &src_format); if (import_frame_props(vsapi, src_props, &src_format, err_msg)) goto fail; _vszimg_set_dst_colorspace(data, &src_format, &dst_format); if (!(graph_data = _vszimg_get_graph_data(data, &src_format, &dst_format, err_msg, err_msg_size))) goto fail; dst_frame = vsapi->newVideoFrame(dst_vsformat, dst_format.width, dst_format.height, src_frame, core); dst_props = vsapi->getFramePropsRW(dst_frame); if (callback_init_unpack(&unpack_cb_data, graph_data->graph, src_frame, &src_format, src_vsformat, core, vsapi, err_msg, err_msg_size)) goto fail; if (callback_init_pack(&pack_cb_data, graph_data->graph, dst_frame, &dst_format, dst_vsformat, core, vsapi, err_msg, err_msg_size)) goto fail; if (zimg_filter_graph_get_tmp_size(graph_data->graph, &tmp_size)) { format_zimg_error(err_msg, err_msg_size); goto fail; } VS_ALIGNED_MALLOC(&tmp, tmp_size, 64); if (!tmp) { sprintf(err_msg, "error allocating temporary buffer"); goto fail; } if (zimg_filter_graph_process(graph_data->graph, &unpack_cb_data.line_buf.c, &pack_cb_data.line_buf.m, tmp, unpack_cb_data.cb, &unpack_cb_data, pack_cb_data.cb, &pack_cb_data)) { format_zimg_error(err_msg, err_msg_size); goto fail; } propagate_sar(vsapi, src_props, dst_props, &src_format, &dst_format); export_frame_props(vsapi, &dst_format, dst_props); ret = dst_frame; dst_frame = NULL; fail: _vszimg_release_graph_data(data, graph_data); vsapi->freeFrame(dst_frame); VS_ALIGNED_FREE(tmp); callback_destroy(&unpack_cb_data, vsapi); callback_destroy(&pack_cb_data, vsapi); return ret; }
static const VSFrameRef * VS_CC vszimg_get_frame(int n, int activationReason, void **instanceData, void **frameData, VSFrameContext *frameCtx, VSCore *core, const VSAPI *vsapi) { struct vszimg_data *data = *instanceData; struct vszimg_graph_data *graph_data = NULL; const VSFrameRef *src_frame = NULL; VSFrameRef *dst_frame = NULL; VSFrameRef *ret = NULL; void *tmp = NULL; char err_msg[1024]; int err_flag = 1; if (activationReason == arInitial) { vsapi->requestFrameFilter(n, data->node, frameCtx); } else if (activationReason == arAllFramesReady) { zimg_image_buffer_const src_buf = { ZIMG_API_VERSION }; zimg_image_buffer dst_buf = { { ZIMG_API_VERSION } }; zimg_image_format src_format; zimg_image_format dst_format; const VSFormat *src_vsformat; const VSFormat *dst_vsformat; const VSMap *src_props; VSMap *dst_props; size_t tmp_size; int p; zimg2_image_format_default(&src_format, ZIMG_API_VERSION); zimg2_image_format_default(&dst_format, ZIMG_API_VERSION); src_frame = vsapi->getFrameFilter(n, data->node, frameCtx); src_props = vsapi->getFramePropsRO(src_frame); src_vsformat = vsapi->getFrameFormat(src_frame); dst_vsformat = data->vi.format ? data->vi.format : src_vsformat; src_format.width = vsapi->getFrameWidth(src_frame, 0); src_format.height = vsapi->getFrameHeight(src_frame, 0); dst_format.width = data->vi.width ? (unsigned)data->vi.width : src_format.width; dst_format.height = data->vi.height ? (unsigned)data->vi.height : src_format.height; if (translate_vsformat(src_vsformat, &src_format, err_msg)) goto fail; if (translate_vsformat(dst_vsformat, &dst_format, err_msg)) goto fail; _vszimg_set_src_colorspace(data, &src_format); if (import_frame_props(vsapi, src_props, &src_format, err_msg)) goto fail; _vszimg_set_dst_colorspace(data, &src_format, &dst_format); if (!(graph_data = _vszimg_get_graph_data(data, &src_format, &dst_format, err_msg, sizeof(err_msg)))) goto fail; dst_frame = vsapi->newVideoFrame(dst_vsformat, dst_format.width, dst_format.height, src_frame, core); dst_props = vsapi->getFramePropsRW(dst_frame); for (p = 0; p < src_vsformat->numPlanes; ++p) { src_buf.data[p] = vsapi->getReadPtr(src_frame, p); src_buf.stride[p] = vsapi->getStride(src_frame, p); src_buf.mask[p] = -1; } for (p = 0; p < dst_vsformat->numPlanes; ++p) { dst_buf.m.data[p] = vsapi->getWritePtr(dst_frame, p); dst_buf.m.stride[p] = vsapi->getStride(dst_frame, p); dst_buf.m.mask[p] = -1; } if (zimg2_filter_graph_get_tmp_size(graph_data->graph, &tmp_size)) { format_zimg_error(err_msg, sizeof(err_msg)); goto fail; } VS_ALIGNED_MALLOC(&tmp, tmp_size, 64); if (!tmp) { sprintf(err_msg, "error allocating temporary buffer"); goto fail; } if (zimg2_filter_graph_process(graph_data->graph, &src_buf, &dst_buf, tmp, NULL, NULL, NULL, NULL)) { format_zimg_error(err_msg, sizeof(err_msg)); goto fail; } propagate_sar(vsapi, src_props, dst_props, &src_format, &dst_format); export_frame_props(vsapi, &dst_format, dst_props); ret = dst_frame; dst_frame = NULL; } err_flag = 0; fail: if (err_flag) vsapi->setFilterError(err_msg, frameCtx); _vszimg_release_graph_data(data, graph_data); vsapi->freeFrame(src_frame); vsapi->freeFrame(dst_frame); return ret; }