use adk_core::{UsageCall, UsageUnit, UsageVector};
use super::{
Backend, CallOptions, Inputs, Preflight, Prepared, VisionOutput, VisionRuntime, mismatch,
planned_size,
};
use crate::backend::{BackendDescriptor, Locality};
use crate::cx::VisionCx;
use crate::egress::{ClipReleaseRequest, DataClass, ReleasedClip, ReleasedImage};
use crate::error::{Result, VisionError};
use crate::geometry::{RegionNormalizer, TransformChain};
use crate::manifest::VisionTask;
use crate::media::{FrameMeta, SourceImage};
use crate::sent::SentImage;
use crate::traits::{
Sampling, SegmentPrompt, VideoAnalysis, VideoFrame, VideoInput, VideoPlan, VideoRequest,
};
use crate::video::{SentClip, SourceClip};
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub enum VideoSource<'a> {
Clip {
clip: &'a SourceClip,
sampling: Sampling,
},
Frames {
frames: &'a [(FrameMeta, SourceImage)],
sampling: Sampling,
},
}
impl<'a> VideoSource<'a> {
pub fn clip(clip: &'a SourceClip, sampling: Sampling) -> Self {
Self::Clip { clip, sampling }
}
pub fn frames(frames: &'a [(FrameMeta, SourceImage)], sampling: Sampling) -> Self {
Self::Frames { frames, sampling }
}
}
impl VisionRuntime {
pub async fn analyze_video(
&self,
cx: &VisionCx,
video: VideoSource<'_>,
request: &VideoRequest,
) -> Result<VisionOutput<VideoAnalysis>> {
self.analyze_video_with(cx, video, request, &CallOptions::default()).await
}
pub async fn analyze_video_with(
&self,
cx: &VisionCx,
video: VideoSource<'_>,
request: &VideoRequest,
options: &CallOptions,
) -> Result<VisionOutput<VideoAnalysis>> {
let route = self.route_for(VisionTask::AnalyzeVideo, options, self.videos.default_id())?;
let (descriptor, backend) = self.videos.get(&route.primary)?;
let (clip, frames, sampling) = match video {
VideoSource::Clip { clip, sampling } => (Some(clip), &[][..], sampling),
VideoSource::Frames { frames, sampling } => (None, frames, sampling),
};
if clip.is_none() && frames.is_empty() {
return Err(VisionError::InvalidInput(
"a video call needs a clip or at least one frame".into(),
));
}
let duration_ms = match clip {
Some(clip) => clip.info().duration_ms,
None => {
let first = frames.first().map_or(0, |(f, _)| f.pts_ms);
let last = frames.last().map_or(0, |(f, _)| f.pts_ms);
let step = if sampling.fps > 0.0 { (1_000.0 / sampling.fps) as u64 } else { 0 };
last.saturating_sub(first) + step
}
};
let plan = VideoPlan {
clip: clip.map(|c| c.info().clone()),
frame_sizes: frames
.iter()
.map(|(_, image)| planned_size(image.size(), options.region, &descriptor.limits))
.collect(),
duration_ms,
sampling,
};
let (billed, bound) = match backend {
Backend::Local(b) => (b.frames(&plan), b.max_cost_micro_usd(&plan, request)),
Backend::Remote(b) => (b.frames(&plan), b.max_cost_micro_usd(&plan, request)),
};
let requests = match request {
VideoRequest::Track { prompts } => {
let concepts =
prompts.iter().filter(|p| matches!(p, SegmentPrompt::Concept { .. })).count();
descriptor.requests_for(concepts, concepts < prompts.len())
}
_ => 1,
};
let hold = self.preflight_frames(cx, descriptor, billed, requests, bound).await?;
let estimate = hold.cost.or(bound);
let mut prepared = Vec::with_capacity(frames.len());
for (_, image) in frames {
prepared.push(
self.prepare(cx, descriptor, backend.is_remote(), image, options.region, options)
.await?,
);
}
let normalizers: Vec<(&FrameMeta, RegionNormalizer)> = frames
.iter()
.zip(&prepared)
.map(|((meta, _), p)| (meta, Self::normalizer(descriptor, p)))
.collect();
let mut inputs = Inputs::of(&prepared);
let metas: Vec<FrameMeta> = frames.iter().map(|(meta, _)| meta.clone()).collect();
let task = VisionTask::AnalyzeVideo;
let (raw, mut record) = match backend {
Backend::Local(b) => {
let images = prepared
.into_iter()
.map(|p| match p {
Prepared::Local(image) => Ok(image),
Prepared::Remote(_) => Err(mismatch(descriptor)),
})
.collect::<Result<Vec<SentImage>>>()?;
if let Some(clip) = clip {
inputs.sources.push(clip.digest());
}
let input = VideoInput::new(
metas.into_iter().zip(images).collect(),
clip.map(|c| SentClip::new(c.clone())),
sampling,
);
self.metered(cx, task, descriptor, inputs, hold, b.analyze(cx, &input, request))
.await?
}
Backend::Remote(b) => {
let images = prepared
.into_iter()
.map(|p| match p {
Prepared::Remote(released) => Ok(*released),
Prepared::Local(_) => Err(mismatch(descriptor)),
})
.collect::<Result<Vec<ReleasedImage>>>()?;
let released = match clip {
Some(clip) => {
let released =
self.release_clip(cx, descriptor, clip, estimate, options).await?;
inputs.sources.push(clip.digest());
inputs.egress.push(released.receipt().egress);
Some(released)
}
None => None,
};
let input =
VideoInput::new(metas.into_iter().zip(images).collect(), released, sampling);
self.metered(cx, task, descriptor, inputs, hold, b.analyze(cx, &input, request))
.await?
}
};
let clip_normalizer = clip.map(|c| {
let (w, h) = c.size();
RegionNormalizer::new(
c.digest(),
TransformChain::new(w, h),
descriptor.frame,
descriptor.id.clone(),
)
.with_manifest(descriptor.manifest.clone())
});
let mut found = Vec::with_capacity(raw.frames.len());
let mut regions = 0usize;
for frame in raw.frames {
let (normalizer, pts) = match &clip_normalizer {
Some(normalizer) => (normalizer, frame.pts_ms),
None => {
let (meta, normalizer) = normalizers
.iter()
.find(|(meta, _)| meta.index == frame.index)
.ok_or_else(|| VisionError::Parse {
backend: descriptor.id.to_string(),
message: format!(
"regions name frame {}, which the call did not send",
frame.index
),
})?;
(normalizer, frame.pts_ms.or(Some(meta.pts_ms)))
}
};
let mapped = normalizer.regions(frame.regions)?;
regions += mapped.len();
found.push(VideoFrame { index: frame.index, pts_ms: pts, regions: mapped });
}
record.regions = regions;
let mut usage = raw.usage;
if usage.frames == 0 {
usage.frames = billed.saturating_mul(requests);
}
if usage.cost_micro_usd.is_none() {
usage.cost_micro_usd = record.cost_micro_usd;
}
let value = VideoAnalysis { answer: raw.answer, events: raw.events, frames: found, usage };
Ok(VisionOutput { value, calls: vec![record] })
}
async fn release_clip(
&self,
cx: &VisionCx,
descriptor: &BackendDescriptor,
clip: &SourceClip,
estimate: Option<u64>,
options: &CallOptions,
) -> Result<ReleasedClip> {
let gate = self
.gate
.as_ref()
.ok_or_else(|| VisionError::NotReleased { backend: descriptor.id.to_string() })?;
let class = options.data_class.clone().unwrap_or_else(|| DataClass::new("video"));
let mut request =
ClipReleaseRequest::new(clip, descriptor, class).with_estimated_cost(estimate);
for digest in &options.approved {
request = request.approved(*digest);
}
gate.release_clip(cx, request).await
}
async fn preflight_frames(
&self,
cx: &VisionCx,
descriptor: &BackendDescriptor,
frames: u64,
requests: u64,
estimate: Option<u64>,
) -> Result<Preflight> {
cx.check()?;
let units = UsageVector::new().with(UsageUnit::Frames, frames.saturating_mul(requests));
let (vendor, model, cost, bound) = match &descriptor.price {
None => {
let vendor = match &descriptor.locality {
Locality::Remote { hosts } => hosts.first().cloned(),
Locality::InProcess | Locality::Loopback => None,
};
(vendor.unwrap_or_else(|| "local".into()), descriptor.id.to_string(), Some(0), 0)
}
Some(price) => match price.unit() {
Some(unit) => {
let amount = if unit == UsageUnit::Requests { 1 } else { frames };
let cost = price.request_cost(amount).map(|c| c.saturating_mul(requests));
(price.vendor().to_string(), price.model().to_string(), cost, cost.unwrap_or(0))
}
None => (
price.vendor().to_string(),
price.model().to_string(),
None,
estimate.unwrap_or(0),
),
},
};
let scope = cx.scope();
if scope.is_unmetered() {
return Ok(Preflight { hold: None, units, cost });
}
let call =
UsageCall::new(vendor, model).with_max_cost_micro_usd(bound).with_units(units.clone());
let hold = scope.begin_usage(&call).await?;
Ok(Preflight { hold: Some(hold), units, cost })
}
}