static int callback_init_common(struct callback_data *data, const zimg_filter_graph *graph, vszimg_bool is_pack, const zimg_image_format *format, const VSFormat *vsformat, VSCore *core, const VSAPI *vsapi, char *err_msg, size_t err_msg_size) { zimg_image_buffer_u buf_initialized = { { ZIMG_API_VERSION } }; unsigned count; unsigned mask; data->cb = NULL; data->frame_tmp = NULL; data->plane_buf = buf_initialized; data->line_buf = buf_initialized; data->height = format->height; if (vsformat->colorFamily != cmCompat) return 0; if (vsformat->id != pfCompatBGR32 && vsformat->id != pfCompatYUY2) { sprintf(err_msg, "unsupported compat format: %d (%s)", vsformat->id, vsformat->name); return 1; } if (is_pack && zimg_filter_graph_get_output_buffering(graph, &count)) { format_zimg_error(err_msg, err_msg_size); return 1; } if (!is_pack && zimg_filter_graph_get_input_buffering(graph, &count)) { format_zimg_error(err_msg, err_msg_size); return 1; } mask = zimg_select_buffer_mask(count); count = (mask == ZIMG_BUFFER_MAX) ? format->height : mask + 1; data->frame_tmp = vsapi->newVideoFrame( vsapi->registerFormat(cmYUV, stInteger, 8, vsformat->subSamplingW, vsformat->subSamplingH, core), format->width, count, NULL, core); import_frame_as_write_buffer(data->frame_tmp, &data->line_buf.m, mask, vsapi); return 0; }
std::pair<zimgxx::zimage_buffer, std::shared_ptr<void>> allocate_buffer(const zimgxx::zimage_format &format, unsigned count) { zimgxx::zimage_buffer buffer; std::shared_ptr<void> handle; unsigned char *ptr; unsigned mask = zimg_select_buffer_mask(count); size_t channel_size[3] = { 0 }; size_t pixel_size; count = (mask == ZIMG_BUFFER_MAX) ? format.height : mask + 1; if (format.pixel_type == ZIMG_PIXEL_FLOAT) pixel_size = sizeof(float); else if (format.pixel_type == ZIMG_PIXEL_WORD || format.pixel_type == ZIMG_PIXEL_HALF) pixel_size = sizeof(uint16_t); else pixel_size = sizeof(uint8_t); for (unsigned p = 0; p < (format.color_family == ZIMG_COLOR_GREY ? 1U : 3U); ++p) { unsigned count_plane = p ? count >> format.subsample_h : count; unsigned mask_plane = (mask == ZIMG_BUFFER_MAX) ? mask : mask >> format.subsample_h; size_t row_size = format.width * pixel_size; ptrdiff_t stride = row_size % 32 ? row_size - row_size % 32 + 32 : row_size; buffer.mask(p) = mask_plane; buffer.stride(p) = stride; channel_size[p] = static_cast<size_t>(stride) * count_plane; } handle.reset(aligned_malloc(channel_size[0] + channel_size[1] + channel_size[2], 32), &aligned_free); ptr = static_cast<unsigned char *>(handle.get()); for (unsigned p = 0; p < (format.color_family == ZIMG_COLOR_GREY ? 1U : 3U); ++p) { buffer.data(p) = ptr; ptr += channel_size[p]; } return{ buffer, handle }; }