2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Diagnostics;
19 using System.Runtime.InteropServices;
20 using static Interop.Camera;
22 namespace Tizen.Multimedia
25 /// The class containing the preview image data.
27 /// <since_tizen> 4 </since_tizen>
28 public class PreviewFrame
30 internal PreviewFrame(IntPtr ptr, ref PinnedPreviewBuffer<byte> buffers)
32 var unmanagedStruct = Marshal.PtrToStructure<CameraPreviewDataStruct>(ptr);
34 Format = unmanagedStruct.Format;
35 Resolution = new Size(unmanagedStruct.Width, unmanagedStruct.Height);
36 TimeStamp = unmanagedStruct.TimeStamp;
37 PlaneType = GetPlaneType(unmanagedStruct);
38 CreatePlane(unmanagedStruct, ref buffers);
41 private static PlaneType GetPlaneType(CameraPreviewDataStruct unmanagedStruct)
43 if (unmanagedStruct.NumOfPlanes == 1)
45 switch (unmanagedStruct.Format)
47 case CameraPixelFormat.H264:
48 case CameraPixelFormat.Jpeg:
49 case CameraPixelFormat.Mjpeg:
50 case CameraPixelFormat.Vp8:
51 case CameraPixelFormat.Vp9:
52 return PlaneType.EncodedPlane;
53 case CameraPixelFormat.Invz:
54 return PlaneType.DepthPlane;
55 case CameraPixelFormat.Rgba:
56 case CameraPixelFormat.Argb:
57 return PlaneType.RgbPlane;
59 return PlaneType.SinglePlane;
62 else if (unmanagedStruct.NumOfPlanes == 2)
64 return PlaneType.DoublePlane;
67 return PlaneType.TriplePlane;
70 internal void CreatePlane(CameraPreviewDataStruct unmanagedStruct, ref PinnedPreviewBuffer<byte> buffers)
74 case PlaneType.SinglePlane:
75 var singlePlane = unmanagedStruct.Plane.SinglePlane;
79 buffers = new PinnedPreviewBuffer<byte>(singlePlane.DataLength);
82 Marshal.Copy(singlePlane.Data, buffers[0], 0, (int)singlePlane.DataLength);
83 Plane = new SinglePlane(buffers[0]);
86 case PlaneType.DoublePlane:
87 var doublePlane = unmanagedStruct.Plane.DoublePlane;
89 doublePlane.YLength = (uint)(Resolution.Width * Resolution.Height);
90 doublePlane.UVLength = (uint)(Resolution.Width * Resolution.Height) / 2;
94 buffers = new PinnedPreviewBuffer<byte>(doublePlane.YLength, doublePlane.UVLength);
97 Marshal.Copy(doublePlane.Y, buffers[0], 0, (int)doublePlane.YLength);
98 Marshal.Copy(doublePlane.UV, buffers[1], 0, (int)doublePlane.UVLength);
99 Plane = new DoublePlane(buffers[0], buffers[1]);
102 case PlaneType.TriplePlane:
103 var triplePlane = unmanagedStruct.Plane.TriplePlane;
107 buffers = new PinnedPreviewBuffer<byte>(triplePlane.YLength, triplePlane.ULength, triplePlane.VLength);
110 Marshal.Copy(triplePlane.Y, buffers[0], 0, (int)triplePlane.YLength);
111 Marshal.Copy(triplePlane.U, buffers[1], 0, (int)triplePlane.ULength);
112 Marshal.Copy(triplePlane.V, buffers[2], 0, (int)triplePlane.VLength);
113 Plane = new TriplePlane(buffers[0], buffers[1], buffers[2]);
116 case PlaneType.EncodedPlane:
117 var encodedPlane = unmanagedStruct.Plane.EncodedPlane;
121 buffers = new PinnedPreviewBuffer<byte>(encodedPlane.DataLength * 2);
124 Marshal.Copy(encodedPlane.Data, buffers[0], 0, (int)encodedPlane.DataLength);
125 Plane = new EncodedPlane(buffers[0], encodedPlane.IsDeltaFrame);
128 case PlaneType.DepthPlane:
129 var depthPlane = unmanagedStruct.Plane.DepthPlane;
133 buffers = new PinnedPreviewBuffer<byte>(depthPlane.DataLength);
136 Marshal.Copy(depthPlane.Data, buffers[0], 0, (int)depthPlane.DataLength);
137 Plane = new DepthPlane(buffers[0]);
140 case PlaneType.RgbPlane:
141 var rgbPlane = unmanagedStruct.Plane.RgbPlane;
145 buffers = new PinnedPreviewBuffer<byte>(rgbPlane.DataLength);
147 Marshal.Copy(rgbPlane.Data, buffers[0], 0, (int)rgbPlane.DataLength);
149 Plane = new RgbPlane(buffers[0]);
152 Debug.Fail("Unknown preview data!");
157 internal static uint GetMaxPreviewPlaneSize(IntPtr ptr)
160 var unmanagedStruct = Marshal.PtrToStructure<CameraPreviewDataStruct>(ptr);
162 switch (GetPlaneType(unmanagedStruct))
164 case PlaneType.SinglePlane:
165 size = unmanagedStruct.Plane.SinglePlane.DataLength;
167 case PlaneType.DoublePlane:
168 size = unmanagedStruct.Plane.DoublePlane.UVLength;
170 case PlaneType.TriplePlane:
171 size = unmanagedStruct.Plane.TriplePlane.YLength;
173 case PlaneType.EncodedPlane:
174 size = unmanagedStruct.Plane.EncodedPlane.DataLength;
176 case PlaneType.DepthPlane:
177 size = unmanagedStruct.Plane.DepthPlane.DataLength;
179 case PlaneType.RgbPlane:
180 size = unmanagedStruct.Plane.RgbPlane.DataLength;
183 Debug.Fail("Unknown preview data!");
191 /// The pixel format of the image.
193 /// <since_tizen> 4 </since_tizen>
194 public CameraPixelFormat Format { get; }
197 /// The resolution of the preview image.
199 /// <since_tizen> 4 </since_tizen>
200 public Size Resolution { get; }
203 /// The time stamp of the preview frame.
205 /// <since_tizen> 4 </since_tizen>
206 public uint TimeStamp { get; }
209 /// The type of the preview plane. <see cref="Tizen.Multimedia.PlaneType"/>
211 /// <since_tizen> 4 </since_tizen>
212 public PlaneType PlaneType { get; }
215 /// The buffer including the preview frame.
217 /// <since_tizen> 4 </since_tizen>
218 public IPreviewPlane Plane { get; private set;}