video_frame.h revision f2477e01787aa58f445919b809d89e252beef54f
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef MEDIA_BASE_VIDEO_FRAME_H_ 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define MEDIA_BASE_VIDEO_FRAME_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/callback.h" 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/md5.h" 109ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/memory/shared_memory.h" 11eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "gpu/command_buffer/common/mailbox.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "media/base/buffers.h" 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/rect.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/size.h" 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 16c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class SkBitmap; 17c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace media { 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MEDIA_EXPORT VideoFrame : public base::RefCountedThreadSafe<VideoFrame> { 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) enum { 232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) kFrameSizeAlignment = 16, 242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) kFrameSizePadding = 16, 252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) kFrameAddressAlignment = 32 262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) }; 272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) enum { 29c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) kMaxPlanes = 4, 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) kYPlane = 0, 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) kUPlane = 1, 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) kVPlane = 2, 34c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) kAPlane = 3, 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) }; 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Surface formats roughly based on FOURCC labels, see: 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // http://www.fourcc.org/rgb.php 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // http://www.fourcc.org/yuv.php 40f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // Logged to UMA, so never reuse values. 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) enum Format { 420f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) UNKNOWN = 0, // Unknown format value. 43f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) YV12 = 1, // 12bpp YVU planar 1x1 Y, 2x2 VU samples 44f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) YV16 = 2, // 16bpp YVU planar 1x1 Y, 2x1 VU samples 45f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) I420 = 3, // 12bpp YVU planar 1x1 Y, 2x2 UV samples. 46f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) YV12A = 4, // 20bpp YUVA planar 1x1 Y, 2x2 VU, 1x1 A samples. 472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if defined(GOOGLE_TV) 48f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) HOLE = 5, // Hole frame. 492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif 50f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) NATIVE_TEXTURE = 6, // Native texture. Pixel-format agnostic. 51f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) HISTOGRAM_MAX, // Must always be greatest. 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) }; 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 543240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch // Returns the name of a Format as a string. 553240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch static std::string FormatToString(Format format); 563240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch 57eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // This class calls the TextureNoLongerNeededCallback when the last reference 58eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // on the class is destroyed. The VideoFrame holds a reference to the mailbox 59eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // but anyone else who queries the mailbox should also hold a reference while 60eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // it is uses the mailbox, to ensure it remains valid. When finished with the 61eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // mailbox, call Return() with a new sync point, to ensure the mailbox remains 62eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // valid for the issued commands. 63eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch class MEDIA_EXPORT MailboxHolder 64eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch : public base::RefCountedThreadSafe<MailboxHolder> { 65eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch public: 66eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch typedef base::Callback<void(uint32 sync_point)> 67eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch TextureNoLongerNeededCallback; 68eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 69eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch MailboxHolder(const gpu::Mailbox& mailbox, 70eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch unsigned sync_point, 71eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const TextureNoLongerNeededCallback& release_callback); 72eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 73eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const gpu::Mailbox& mailbox() const { return mailbox_; } 74eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch unsigned sync_point() const { return sync_point_; } 75eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 76eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch void Return(unsigned sync_point) { sync_point_ = sync_point; } 77eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 78eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch private: 79eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch friend class base::RefCountedThreadSafe<MailboxHolder>; 80eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch ~MailboxHolder(); 81eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 82eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch gpu::Mailbox mailbox_; 83eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch unsigned sync_point_; 84eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch TextureNoLongerNeededCallback release_callback_; 85eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch }; 86eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 87eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Creates a new frame in system memory with given parameters. Buffers for 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // the frame are allocated but not initialized. 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |coded_size| is the width and height of the frame data in pixels. 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |visible_rect| is the visible portion of |coded_size|, after cropping (if 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // any) is applied. 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |natural_size| is the width and height of the frame when the frame's aspect 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // ratio is applied to |visible_rect|. 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static scoped_refptr<VideoFrame> CreateFrame( 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Format format, 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const gfx::Size& coded_size, 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const gfx::Rect& visible_rect, 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const gfx::Size& natural_size, 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::TimeDelta timestamp); 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Call prior to CreateFrame to ensure validity of frame configuration. Called 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // automatically by VideoDecoderConfig::IsValidConfig(). 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // TODO(scherkus): VideoDecoderConfig shouldn't call this method 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static bool IsValidConfig(Format format, const gfx::Size& coded_size, 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const gfx::Rect& visible_rect, 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const gfx::Size& natural_size); 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // CB to write pixels from the texture backing this frame into the 110c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // |const SkBitmap&| parameter. 111c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) typedef base::Callback<void(const SkBitmap&)> ReadPixelsCB; 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Wraps a native texture of the given parameters with a VideoFrame. When the 1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // frame is destroyed |no_longer_needed_cb.Run()| will be called. 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |coded_size| is the width and height of the frame data in pixels. 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |visible_rect| is the visible portion of |coded_size|, after cropping (if 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // any) is applied. 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |natural_size| is the width and height of the frame when the frame's aspect 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // ratio is applied to |visible_rect|. 120c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |read_pixels_cb| may be used to do (slow!) readbacks from the 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // texture to main memory. 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static scoped_refptr<VideoFrame> WrapNativeTexture( 124eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const scoped_refptr<MailboxHolder>& mailbox_holder, 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) uint32 texture_target, 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const gfx::Size& coded_size, 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const gfx::Rect& visible_rect, 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const gfx::Size& natural_size, 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::TimeDelta timestamp, 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const ReadPixelsCB& read_pixels_cb, 1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const base::Closure& no_longer_needed_cb); 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Read pixels from the native texture backing |*this| and write 134c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // them to |pixels| as BGRA. |pixels| must point to a buffer at 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // least as large as 4*visible_rect().width()*visible_rect().height(). 136c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void ReadPixelsFromNativeTexture(const SkBitmap& pixels); 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 138f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // Wraps packed image data residing in a memory buffer with a VideoFrame. 139f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // The image data resides in |data| and is assumed to be packed tightly in a 140f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // buffer of logical dimensions |coded_size| with the appropriate bit depth 141f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // and plane count as given by |format|. The shared memory handle of the 142f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // backing allocation, if present, can be passed in with |handle|. When the 143f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // frame is destroyed, |no_longer_needed_cb.Run()| will be called. 144f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) static scoped_refptr<VideoFrame> WrapExternalPackedMemory( 145ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch Format format, 146ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch const gfx::Size& coded_size, 147ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch const gfx::Rect& visible_rect, 148ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch const gfx::Size& natural_size, 149ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch uint8* data, 150424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) size_t data_size, 151ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch base::SharedMemoryHandle handle, 152ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch base::TimeDelta timestamp, 153ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch const base::Closure& no_longer_needed_cb); 154ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch 1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Wraps external YUV data of the given parameters with a VideoFrame. 1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // The returned VideoFrame does not own the data passed in. When the frame 1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // is destroyed |no_longer_needed_cb.Run()| will be called. 158ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch // TODO(sheu): merge this into WrapExternalSharedMemory(). 159ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch // http://crbug.com/270217 1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) static scoped_refptr<VideoFrame> WrapExternalYuvData( 1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) Format format, 1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const gfx::Size& coded_size, 1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const gfx::Rect& visible_rect, 1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const gfx::Size& natural_size, 1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int32 y_stride, 1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int32 u_stride, 1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int32 v_stride, 1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) uint8* y_data, 1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) uint8* u_data, 1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) uint8* v_data, 1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::TimeDelta timestamp, 1722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const base::Closure& no_longer_needed_cb); 1732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 174f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // Creates a frame which indicates end-of-stream. 175f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) static scoped_refptr<VideoFrame> CreateEOSFrame(); 1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Allocates YV12 frame based on |size|, and sets its data to the YUV(y,u,v). 1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static scoped_refptr<VideoFrame> CreateColorFrame( 1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const gfx::Size& size, 1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) uint8 y, uint8 u, uint8 v, 1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::TimeDelta timestamp); 1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Allocates YV12 frame based on |size|, and sets its data to the YUV 1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // equivalent of RGB(0,0,0). 1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static scoped_refptr<VideoFrame> CreateBlackFrame(const gfx::Size& size); 1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if defined(GOOGLE_TV) 1882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Allocates a hole frame. 1892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) static scoped_refptr<VideoFrame> CreateHoleFrame(const gfx::Size& size); 1902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif 1912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) static size_t NumPlanes(Format format); 1932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 194424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // Returns the required allocation size for a (tightly packed) frame of the 195424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // given coded size and format. 196424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) static size_t AllocationSize(Format format, const gfx::Size& coded_size); 197424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 198f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // Returns the required allocation size for a (tightly packed) plane of the 199f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // given coded size and format. 200f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) static size_t PlaneAllocationSize(Format format, 201f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) size_t plane, 202f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const gfx::Size& coded_size); 203f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Format format() const { return format_; } 2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const gfx::Size& coded_size() const { return coded_size_; } 2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const gfx::Rect& visible_rect() const { return visible_rect_; } 2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const gfx::Size& natural_size() const { return natural_size_; } 2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int stride(size_t plane) const; 2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns the number of bytes per row and number of rows for a given plane. 2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // As opposed to stride(), row_bytes() refers to the bytes representing 2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // frame data scanlines (coded_size.width() pixels, without stride padding). 2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int row_bytes(size_t plane) const; 2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int rows(size_t plane) const; 2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns pointer to the buffer for a given plane. The memory is owned by 2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // VideoFrame object and must not be freed by the caller. 2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) uint8* data(size_t plane) const; 2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 223eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // Returns the mailbox of the native texture wrapped by this frame. Only 224eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // valid to call if this is a NATIVE_TEXTURE frame. Before using the 225eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // mailbox, the caller must wait for the included sync point. 226eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const scoped_refptr<MailboxHolder>& texture_mailbox() const; 2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns the texture target. Only valid for NATIVE_TEXTURE frames. 2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) uint32 texture_target() const; 2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 231eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // Returns the shared-memory handle, if present 232eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::SharedMemoryHandle shared_memory_handle() const; 233eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns true if this VideoFrame represents the end of the stream. 235f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) bool end_of_stream() const { return end_of_stream_; } 2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::TimeDelta GetTimestamp() const { 2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return timestamp_; 2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void SetTimestamp(const base::TimeDelta& timestamp) { 2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) timestamp_ = timestamp; 2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Used to keep a running hash of seen frames. Expects an initialized MD5 2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // context. Calls MD5Update with the context and the contents of the frame. 2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void HashFrameForTesting(base::MD5Context* context); 2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) friend class base::RefCountedThreadSafe<VideoFrame>; 2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Clients must use the static CreateFrame() method to create a new frame. 2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) VideoFrame(Format format, 2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const gfx::Size& coded_size, 2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const gfx::Rect& visible_rect, 2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const gfx::Size& natural_size, 255f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) base::TimeDelta timestamp, 256f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) bool end_of_stream); 2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~VideoFrame(); 2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void AllocateYUV(); 2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Used to DCHECK() plane parameters. 2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool IsValidPlane(size_t plane) const; 2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Frame format. 2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Format format_; 2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Width and height of the video frame. 2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) gfx::Size coded_size_; 2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Width, height, and offsets of the visible portion of the video frame. 2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) gfx::Rect visible_rect_; 2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Width and height of the visible portion of the video frame with aspect 2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // ratio taken into account. 2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) gfx::Size natural_size_; 2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Array of strides for each plane, typically greater or equal to the width 2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // of the surface divided by the horizontal sampling period. Note that 2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // strides can be negative. 2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int32 strides_[kMaxPlanes]; 2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Array of data pointers to each plane. 2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) uint8* data_[kMaxPlanes]; 2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 285eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // Native texture mailbox, if this is a NATIVE_TEXTURE frame. 286eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch scoped_refptr<MailboxHolder> texture_mailbox_holder_; 2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) uint32 texture_target_; 2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ReadPixelsCB read_pixels_cb_; 2892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 290eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // Shared memory handle, if this frame was allocated from shared memory. 291eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::SharedMemoryHandle shared_memory_handle_; 292eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 2932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::Closure no_longer_needed_cb_; 2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::TimeDelta timestamp_; 2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 297f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const bool end_of_stream_; 298f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DISALLOW_IMPLICIT_CONSTRUCTORS(VideoFrame); 3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace media 3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // MEDIA_BASE_VIDEO_FRAME_H_ 305