#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "third_party/blink/renderer/modules/webcodecs/video_frame.h"
#include <limits>
#include <utility>
#include "base/containers/span.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/scoped_refptr.h"
#include "base/numerics/checked_math.h"
#include "base/task/bind_post_task.h"
#include "base/time/time.h"
#include "components/viz/common/gpu/raster_context_provider.h"
#include "media/base/limits.h"
#include "media/base/timestamp_constants.h"
#include "media/base/video_frame.h"
#include "media/base/video_frame_metadata.h"
#include "media/base/video_frame_pool.h"
#include "media/base/video_types.h"
#include "media/base/video_util.h"
#include "media/renderers/paint_canvas_video_renderer.h"
#include "third_party/blink/public/mojom/frame/lifecycle.mojom-blink.h"
#include "third_party/blink/public/mojom/use_counter/metrics/web_feature.mojom-blink.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/web_graphics_context_3d_provider.h"
#include "third_party/blink/renderer/bindings/core/v8/to_v8_traits.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_plane_layout.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_union_cssimagevalue_htmlcanvaselement_htmlimageelement_htmlvideoelement_imagebitmap_offscreencanvas_svgimageelement_videoframe.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_video_color_space_init.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_video_frame_buffer_init.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_video_frame_copy_to_options.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_video_frame_init.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_video_pixel_format.h"
#include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_state_observer.h"
#include "third_party/blink/renderer/core/frame/deprecation/deprecation.h"
#include "third_party/blink/renderer/core/geometry/dom_rect_read_only.h"
#include "third_party/blink/renderer/core/html/canvas/canvas_image_source.h"
#include "third_party/blink/renderer/core/html/canvas/predefined_color_space.h"
#include "third_party/blink/renderer/core/html/media/html_video_element.h"
#include "third_party/blink/renderer/core/imagebitmap/image_bitmap.h"
#include "third_party/blink/renderer/core/inspector/console_message.h"
#include "third_party/blink/renderer/core/typed_arrays/dom_array_buffer.h"
#include "third_party/blink/renderer/modules/canvas/imagebitmap/image_bitmap_factories.h"
#include "third_party/blink/renderer/modules/webcodecs/array_buffer_util.h"
#include "third_party/blink/renderer/modules/webcodecs/background_readback.h"
#include "third_party/blink/renderer/modules/webcodecs/video_color_space.h"
#include "third_party/blink/renderer/modules/webcodecs/video_frame_init_util.h"
#include "third_party/blink/renderer/modules/webcodecs/video_frame_layout.h"
#include "third_party/blink/renderer/modules/webcodecs/video_frame_rect_util.h"
#include "third_party/blink/renderer/platform/geometry/geometry_hash_traits.h"
#include "third_party/blink/renderer/platform/graphics/canvas_color_params.h"
#include "third_party/blink/renderer/platform/graphics/canvas_resource_provider.h"
#include "third_party/blink/renderer/platform/graphics/gpu/shared_gpu_context.h"
#include "third_party/blink/renderer/platform/graphics/graphics_types.h"
#include "third_party/blink/renderer/platform/graphics/image.h"
#include "third_party/blink/renderer/platform/graphics/skia/sk_image_info_hash.h"
#include "third_party/blink/renderer/platform/graphics/skia/skia_utils.h"
#include "third_party/blink/renderer/platform/graphics/unaccelerated_static_bitmap_image.h"
#include "third_party/blink/renderer/platform/graphics/video_frame_image_util.h"
#include "third_party/blink/renderer/platform/heap/cross_thread_handle.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_copier_base.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_functional.h"
#include "third_party/blink/renderer/platform/wtf/hash_map.h"
#include "third_party/libyuv/include/libyuv/planar_functions.h"
#include "third_party/skia/include/gpu/GrDirectContext.h"
#include "ui/gfx/geometry/skia_conversions.h"
#include "v8/include/v8.h"
namespace WTF {
template <>
struct CrossThreadCopier<blink::VideoFrameLayout>
: public CrossThreadCopierPassThrough<blink::VideoFrameLayout> { … };
}
namespace blink {
namespace {
BASE_FEATURE(…);
media::VideoPixelFormat ToMediaPixelFormat(V8VideoPixelFormat::Enum fmt) { … }
media::VideoPixelFormat ToOpaqueMediaPixelFormat(media::VideoPixelFormat fmt) { … }
std::optional<V8VideoPixelFormat> ToV8VideoPixelFormat(
media::VideoPixelFormat fmt) { … }
bool IsFormatEnabled(media::VideoPixelFormat fmt) { … }
class CachedVideoFramePool : public GarbageCollected<CachedVideoFramePool>,
public Supplement<ExecutionContext>,
public ExecutionContextLifecycleStateObserver { … };
const char CachedVideoFramePool::kSupplementName[] = …;
const base::TimeDelta CachedVideoFramePool::kIdleTimeout = …;
class CanvasResourceProviderCache
: public GarbageCollected<CanvasResourceProviderCache>,
public Supplement<ExecutionContext>,
public ExecutionContextLifecycleStateObserver { … };
const char CanvasResourceProviderCache::kSupplementName[] = …;
const base::TimeDelta CanvasResourceProviderCache::kIdleTimeout = …;
std::optional<media::VideoPixelFormat> CopyToFormat(
const media::VideoFrame& frame) { … }
void CopyMappablePlanes(const media::VideoFrame& src_frame,
const gfx::Rect& src_rect,
const VideoFrameLayout& dest_layout,
base::span<uint8_t> dest_buffer) { … }
bool CopyTexturablePlanes(media::VideoFrame& src_frame,
const gfx::Rect& src_rect,
const VideoFrameLayout& dest_layout,
base::span<uint8_t> dest_buffer) { … }
bool ParseCopyToOptions(const media::VideoFrame& frame,
VideoFrameCopyToOptions* options,
ExceptionState& exception_state,
VideoFrameLayout* dest_layout_out,
gfx::Rect* src_rect_out = nullptr) { … }
HeapVector<Member<PlaneLayout>> ConvertLayout(
const VideoFrameLayout& dest_layout) { … }
}
VideoFrame::VideoFrame(scoped_refptr<media::VideoFrame> frame,
ExecutionContext* context,
std::string monitoring_source_id,
sk_sp<SkImage> sk_image,
bool use_capture_timestamp) { … }
VideoFrame::VideoFrame(scoped_refptr<VideoFrameHandle> handle)
: … { … }
VideoFrame::~VideoFrame() { … }
VideoFrame* VideoFrame::Create(ScriptState* script_state,
const V8CanvasImageSource* source,
const VideoFrameInit* init,
ExceptionState& exception_state) { … }
VideoFrame* VideoFrame::Create(ScriptState* script_state,
const AllowSharedBufferSource* data,
const VideoFrameBufferInit* init,
ExceptionState& exception_state) { … }
std::optional<V8VideoPixelFormat> VideoFrame::format() const { … }
uint32_t VideoFrame::codedWidth() const { … }
uint32_t VideoFrame::codedHeight() const { … }
DOMRectReadOnly* VideoFrame::codedRect() { … }
DOMRectReadOnly* VideoFrame::visibleRect() { … }
uint32_t VideoFrame::displayWidth() const { … }
uint32_t VideoFrame::displayHeight() const { … }
int64_t VideoFrame::timestamp() const { … }
std::optional<uint64_t> VideoFrame::duration() const { … }
VideoColorSpace* VideoFrame::colorSpace() { … }
uint32_t VideoFrame::allocationSize(VideoFrameCopyToOptions* options,
ExceptionState& exception_state) { … }
void VideoFrame::ConvertAndCopyToRGB(scoped_refptr<media::VideoFrame> frame,
const gfx::Rect& src_rect,
const VideoFrameLayout& dest_layout,
base::span<uint8_t> buffer,
PredefinedColorSpace target_color_space) { … }
bool VideoFrame::CopyToAsync(
ScriptPromiseResolver<IDLSequence<PlaneLayout>>* resolver,
scoped_refptr<media::VideoFrame> frame,
gfx::Rect src_rect,
const AllowSharedBufferSource* destination,
const VideoFrameLayout& dest_layout) { … }
ScriptPromise<IDLSequence<PlaneLayout>> VideoFrame::copyTo(
ScriptState* script_state,
const AllowSharedBufferSource* destination,
VideoFrameCopyToOptions* options,
ExceptionState& exception_state) { … }
void VideoFrame::close() { … }
VideoFrame* VideoFrame::clone(ExceptionState& exception_state) { … }
scoped_refptr<Image> VideoFrame::GetSourceImageForCanvas(
FlushReason,
SourceImageStatus* status,
const gfx::SizeF&,
const AlphaDisposition alpha_disposition) { … }
bool VideoFrame::WouldTaintOrigin() const { … }
gfx::SizeF VideoFrame::ElementSize(
const gfx::SizeF& default_object_size,
const RespectImageOrientationEnum respect_orientation) const { … }
bool VideoFrame::IsVideoFrame() const { … }
bool VideoFrame::IsOpaque() const { … }
bool VideoFrame::IsAccelerated() const { … }
void VideoFrame::ResetExternalMemory() { … }
gfx::Size VideoFrame::BitmapSourceSize() const { … }
ScriptPromise<ImageBitmap> VideoFrame::CreateImageBitmap(
ScriptState* script_state,
std::optional<gfx::Rect> crop_rect,
const ImageBitmapOptions* options,
ExceptionState& exception_state) { … }
void VideoFrame::Trace(Visitor* visitor) const { … }
}