[Sensor] Add orientation sensor type
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Camera / Camera / PreviewFrame.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18 using System.Diagnostics;
19 using System.Runtime.InteropServices;
20 using static Interop.Camera;
21
22 namespace Tizen.Multimedia
23 {
24     /// <summary>
25     /// The class containing the preview image data.
26     /// </summary>
27     /// <since_tizen> 4 </since_tizen>
28     public class PreviewFrame
29     {
30         internal PreviewFrame(IntPtr ptr, ref PinnedPreviewBuffer<byte> buffers)
31         {
32             var unmanagedStruct = Marshal.PtrToStructure<CameraPreviewDataStruct>(ptr);
33
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);
39         }
40
41         private static PlaneType GetPlaneType(CameraPreviewDataStruct unmanagedStruct)
42         {
43             if (unmanagedStruct.NumOfPlanes == 1)
44             {
45                 switch (unmanagedStruct.Format)
46                 {
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;
58                     default:
59                         return PlaneType.SinglePlane;
60                 }
61             }
62             else if (unmanagedStruct.NumOfPlanes == 2)
63             {
64                 return PlaneType.DoublePlane;
65             }
66
67             return PlaneType.TriplePlane;
68         }
69
70         internal void CreatePlane(CameraPreviewDataStruct unmanagedStruct, ref PinnedPreviewBuffer<byte> buffers)
71         {
72             switch (PlaneType)
73             {
74                 case PlaneType.SinglePlane:
75                     var singlePlane = unmanagedStruct.Plane.SinglePlane;
76
77                     if (buffers == null)
78                     {
79                         buffers = new PinnedPreviewBuffer<byte>(singlePlane.DataLength);
80                     }
81
82                     Marshal.Copy(singlePlane.Data, buffers[0], 0, (int)singlePlane.DataLength);
83                     Plane = new SinglePlane(buffers[0]);
84
85                     break;
86                 case PlaneType.DoublePlane:
87                     var doublePlane = unmanagedStruct.Plane.DoublePlane;
88
89                     doublePlane.YLength = (uint)(Resolution.Width * Resolution.Height);
90                     doublePlane.UVLength = (uint)(Resolution.Width * Resolution.Height) / 2;
91
92                     if (buffers == null)
93                     {
94                         buffers = new PinnedPreviewBuffer<byte>(doublePlane.YLength, doublePlane.UVLength);
95                     }
96
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]);
100
101                     break;
102                 case PlaneType.TriplePlane:
103                     var triplePlane = unmanagedStruct.Plane.TriplePlane;
104
105                     if (buffers == null)
106                     {
107                         buffers = new PinnedPreviewBuffer<byte>(triplePlane.YLength, triplePlane.ULength, triplePlane.VLength);
108                     }
109
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]);
114
115                     break;
116                 case PlaneType.EncodedPlane:
117                     var encodedPlane = unmanagedStruct.Plane.EncodedPlane;
118
119                     if (buffers == null)
120                     {
121                         buffers = new PinnedPreviewBuffer<byte>(encodedPlane.DataLength * 2);
122                     }
123
124                     Marshal.Copy(encodedPlane.Data, buffers[0], 0, (int)encodedPlane.DataLength);
125                     Plane = new EncodedPlane(buffers[0], encodedPlane.IsDeltaFrame);
126
127                     break;
128                 case PlaneType.DepthPlane:
129                     var depthPlane = unmanagedStruct.Plane.DepthPlane;
130
131                     if (buffers == null)
132                     {
133                         buffers = new PinnedPreviewBuffer<byte>(depthPlane.DataLength);
134                     }
135
136                     Marshal.Copy(depthPlane.Data, buffers[0], 0, (int)depthPlane.DataLength);
137                     Plane = new DepthPlane(buffers[0]);
138
139                     break;
140                 case PlaneType.RgbPlane:
141                     var rgbPlane = unmanagedStruct.Plane.RgbPlane;
142
143                     if (buffers == null)
144                     {
145                         buffers = new PinnedPreviewBuffer<byte>(rgbPlane.DataLength);
146                     }
147                     Marshal.Copy(rgbPlane.Data, buffers[0], 0, (int)rgbPlane.DataLength);
148
149                     Plane = new RgbPlane(buffers[0]);
150                     break;
151                 default:
152                     Debug.Fail("Unknown preview data!");
153                     break;
154             }
155         }
156
157         internal static uint GetMaxPreviewPlaneSize(IntPtr ptr)
158         {
159             uint size = 0;
160             var unmanagedStruct = Marshal.PtrToStructure<CameraPreviewDataStruct>(ptr);
161
162             switch (GetPlaneType(unmanagedStruct))
163             {
164                 case PlaneType.SinglePlane:
165                     size = unmanagedStruct.Plane.SinglePlane.DataLength;
166                     break;
167                 case PlaneType.DoublePlane:
168                     size = unmanagedStruct.Plane.DoublePlane.UVLength;
169                     break;
170                 case PlaneType.TriplePlane:
171                     size = unmanagedStruct.Plane.TriplePlane.YLength;
172                     break;
173                 case PlaneType.EncodedPlane:
174                     size = unmanagedStruct.Plane.EncodedPlane.DataLength;
175                     break;
176                 case PlaneType.DepthPlane:
177                     size = unmanagedStruct.Plane.DepthPlane.DataLength;
178                     break;
179                 case PlaneType.RgbPlane:
180                     size = unmanagedStruct.Plane.RgbPlane.DataLength;
181                     break;
182                 default:
183                     Debug.Fail("Unknown preview data!");
184                     break;
185             }
186
187             return size;
188         }
189
190         /// <summary>
191         /// The pixel format of the image.
192         /// </summary>
193         /// <since_tizen> 4 </since_tizen>
194         public CameraPixelFormat Format { get; }
195
196         /// <summary>
197         /// The resolution of the preview image.
198         /// </summary>
199         /// <since_tizen> 4 </since_tizen>
200         public Size Resolution { get; }
201
202         /// <summary>
203         /// The time stamp of the preview frame.
204         /// </summary>
205         /// <since_tizen> 4 </since_tizen>
206         public uint TimeStamp { get; }
207
208         /// <summary>
209         /// The type of the preview plane. <see cref="Tizen.Multimedia.PlaneType"/>
210         /// </summary>
211         /// <since_tizen> 4 </since_tizen>
212         public PlaneType PlaneType { get; }
213
214         /// <summary>
215         /// The buffer including the preview frame.
216         /// </summary>
217         /// <since_tizen> 4 </since_tizen>
218         public IPreviewPlane Plane { get; private set;}
219     }
220 }