blob: 2f641ffabe5eaccb6de1b9b4fe90b67ea0c028cb [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef TALK_MEDIA_BASE_VIDEOFRAME_H_
29#define TALK_MEDIA_BASE_VIDEOFRAME_H_
30
31#include "talk/base/basictypes.h"
32#include "talk/base/stream.h"
33
34namespace cricket {
35
36// Simple rotation constants.
37enum {
38 ROTATION_0 = 0,
39 ROTATION_90 = 90,
40 ROTATION_180 = 180,
41 ROTATION_270 = 270
42};
43
44// Represents a YUV420 (a.k.a. I420) video frame.
45class VideoFrame {
46 public:
47 VideoFrame() {}
48 virtual ~VideoFrame() {}
49
50 virtual bool InitToBlack(int w, int h, size_t pixel_width,
51 size_t pixel_height, int64 elapsed_time,
52 int64 time_stamp) = 0;
53 // Creates a frame from a raw sample with FourCC |format| and size |w| x |h|.
54 // |h| can be negative indicating a vertically flipped image.
55 // |dw| is destination width; can be less than |w| if cropping is desired.
56 // |dh| is destination height, like |dw|, but must be a positive number.
57 // Returns whether the function succeeded or failed.
58 virtual bool Reset(uint32 fourcc, int w, int h, int dw, int dh, uint8 *sample,
59 size_t sample_size, size_t pixel_width,
60 size_t pixel_height, int64 elapsed_time, int64 time_stamp,
61 int rotation) = 0;
62
63 // Basic accessors.
64 virtual size_t GetWidth() const = 0;
65 virtual size_t GetHeight() const = 0;
66 size_t GetChromaWidth() const { return (GetWidth() + 1) / 2; }
67 size_t GetChromaHeight() const { return (GetHeight() + 1) / 2; }
68 size_t GetChromaSize() const { return GetUPitch() * GetChromaHeight(); }
69 virtual const uint8 *GetYPlane() const = 0;
70 virtual const uint8 *GetUPlane() const = 0;
71 virtual const uint8 *GetVPlane() const = 0;
72 virtual uint8 *GetYPlane() = 0;
73 virtual uint8 *GetUPlane() = 0;
74 virtual uint8 *GetVPlane() = 0;
75 virtual int32 GetYPitch() const = 0;
76 virtual int32 GetUPitch() const = 0;
77 virtual int32 GetVPitch() const = 0;
78
79 // For retrieving the aspect ratio of each pixel. Usually this is 1x1, but
80 // the aspect_ratio_idc parameter of H.264 can specify non-square pixels.
81 virtual size_t GetPixelWidth() const = 0;
82 virtual size_t GetPixelHeight() const = 0;
83
84 virtual int64 GetElapsedTime() const = 0;
85 virtual int64 GetTimeStamp() const = 0;
86 virtual void SetElapsedTime(int64 elapsed_time) = 0;
87 virtual void SetTimeStamp(int64 time_stamp) = 0;
88
89 // Indicates the rotation angle in degrees.
90 virtual int GetRotation() const = 0;
91
92 // Make a shallow copy of the frame. The frame buffer itself is not copied.
93 // Both the current and new VideoFrame will share a single reference-counted
94 // frame buffer.
95 virtual VideoFrame *Copy() const = 0;
96
97 // Since VideoFrame supports shallow copy and the internal frame buffer might
98 // be shared, in case VideoFrame needs exclusive access of the frame buffer,
99 // user can call MakeExclusive() to make sure the frame buffer is exclusive
100 // accessable to the current object. This might mean a deep copy of the frame
101 // buffer if it is currently shared by other objects.
102 virtual bool MakeExclusive() = 0;
103
104 // Writes the frame into the given frame buffer, provided that it is of
105 // sufficient size. Returns the frame's actual size, regardless of whether
106 // it was written or not (like snprintf). If there is insufficient space,
107 // nothing is written.
108 virtual size_t CopyToBuffer(uint8 *buffer, size_t size) const = 0;
109
110 // Writes the frame into the given planes, stretched to the given width and
111 // height. The parameter "interpolate" controls whether to interpolate or just
112 // take the nearest-point. The parameter "crop" controls whether to crop this
113 // frame to the aspect ratio of the given dimensions before stretching.
114 virtual bool CopyToPlanes(
115 uint8* dst_y, uint8* dst_u, uint8* dst_v,
116 int32 dst_pitch_y, int32 dst_pitch_u, int32 dst_pitch_v) const;
117
118 // Writes the frame into the target VideoFrame.
119 virtual void CopyToFrame(VideoFrame* target) const;
120
121 // Writes the frame into the given stream and returns the StreamResult.
122 // See talk/base/stream.h for a description of StreamResult and error.
123 // Error may be NULL. If a non-success value is returned from
124 // StreamInterface::Write(), we immediately return with that value.
125 virtual talk_base::StreamResult Write(talk_base::StreamInterface *stream,
126 int *error);
127
128 // Converts the I420 data to RGB of a certain type such as ARGB and ABGR.
129 // Returns the frame's actual size, regardless of whether it was written or
130 // not (like snprintf). Parameters size and stride_rgb are in units of bytes.
131 // If there is insufficient space, nothing is written.
132 virtual size_t ConvertToRgbBuffer(uint32 to_fourcc, uint8 *buffer,
133 size_t size, int stride_rgb) const = 0;
134
135 // Writes the frame into the given planes, stretched to the given width and
136 // height. The parameter "interpolate" controls whether to interpolate or just
137 // take the nearest-point. The parameter "crop" controls whether to crop this
138 // frame to the aspect ratio of the given dimensions before stretching.
139 virtual void StretchToPlanes(
140 uint8 *y, uint8 *u, uint8 *v, int32 pitchY, int32 pitchU, int32 pitchV,
141 size_t width, size_t height, bool interpolate, bool crop) const;
142
143 // Writes the frame into the given frame buffer, stretched to the given width
144 // and height, provided that it is of sufficient size. Returns the frame's
145 // actual size, regardless of whether it was written or not (like snprintf).
146 // If there is insufficient space, nothing is written. The parameter
147 // "interpolate" controls whether to interpolate or just take the
148 // nearest-point. The parameter "crop" controls whether to crop this frame to
149 // the aspect ratio of the given dimensions before stretching.
150 virtual size_t StretchToBuffer(size_t w, size_t h, uint8 *buffer, size_t size,
151 bool interpolate, bool crop) const;
152
153 // Writes the frame into the target VideoFrame, stretched to the size of that
154 // frame. The parameter "interpolate" controls whether to interpolate or just
155 // take the nearest-point. The parameter "crop" controls whether to crop this
156 // frame to the aspect ratio of the target frame before stretching.
157 virtual void StretchToFrame(VideoFrame *target, bool interpolate,
158 bool crop) const;
159
160 // Stretches the frame to the given size, creating a new VideoFrame object to
161 // hold it. The parameter "interpolate" controls whether to interpolate or
162 // just take the nearest-point. The parameter "crop" controls whether to crop
163 // this frame to the aspect ratio of the given dimensions before stretching.
164 virtual VideoFrame *Stretch(size_t w, size_t h, bool interpolate,
165 bool crop) const;
166
167 // Sets the video frame to black.
168 bool SetToBlack();
169
170 // Tests if sample is valid. Returns true if valid.
171 static bool Validate(uint32 fourcc, int w, int h, const uint8 *sample,
172 size_t sample_size);
173
174 // Size of an I420 image of given dimensions when stored as a frame buffer.
175 static size_t SizeOf(size_t w, size_t h) {
176 return w * h + ((w + 1) / 2) * ((h + 1) / 2) * 2;
177 }
178
179 protected:
180 // Creates an empty frame.
181 virtual VideoFrame *CreateEmptyFrame(int w, int h, size_t pixel_width,
182 size_t pixel_height, int64 elapsed_time,
183 int64 time_stamp) const = 0;
184};
185
186} // namespace cricket
187
188#endif // TALK_MEDIA_BASE_VIDEOFRAME_H_