mv_3d: bug fix
[platform/core/api/mediavision.git] / mv_common / src / MediaSource.cpp
1 /**
2  * Copyright (c) 2015 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 #include "MediaSource.h"
18
19 #include <mv_private.h>
20
21 #include <cstring>
22 #include <new>
23 #include <stdexcept>
24
25 namespace MediaVision
26 {
27 namespace Common
28 {
29 MediaSource::~MediaSource()
30 {
31         clear();
32 }
33
34 bool MediaSource::alloc(unsigned int bufferSize, unsigned int width, unsigned int height, mv_colorspace_e colorspace)
35 {
36         if (bufferSize == 0)
37                 return false;
38
39         LOGD("Call clear() first for media source %p", this);
40         clear();
41
42         Plane plane;
43         auto &pBuffer = plane.buffer;
44         auto &pImageSize = plane.imageSize;
45
46         pBuffer = new (std::nothrow) unsigned char[bufferSize];
47         if (pBuffer == NULL) {
48                 LOGE("Memory allocating for buffer in media source %p failed!", this);
49                 return false;
50         }
51
52         LOGD("Assign new size of the internal buffer of media source %p. "
53                  "New size is %ui.",
54                  this, bufferSize);
55         pImageSize = bufferSize;
56
57         LOGD("Assign new size (%ui x %ui) of the internal buffer image for "
58                  "the media source %p",
59                  width, height, this);
60         _width = width;
61         _height = height;
62
63         LOGD("Assign new colorspace (%i) of the internal buffer image for "
64                  "the media source %p",
65                  colorspace, this);
66         _colorspace = colorspace;
67
68         _plane.push_back(plane);
69
70         return true;
71 }
72
73 void MediaSource::clear(void)
74 {
75         if (_packet) {
76                 media_packet_unref(_packet);
77         } else {
78                 for (const auto &p : _plane) {
79                         delete[] p.buffer;
80                 }
81         }
82         _plane.clear();
83         _width = 0;
84         _height = 0;
85         _colorspace = MEDIA_VISION_COLORSPACE_INVALID;
86         _packet = nullptr;
87 }
88
89 bool MediaSource::fill(const unsigned char *buffer, unsigned int bufferSize, unsigned int width, unsigned int height,
90                                            mv_colorspace_e colorspace)
91 {
92         if (bufferSize == 0 || buffer == NULL)
93                 return false;
94
95         LOGD("Call clear() first for media source %p", this);
96         clear();
97
98         Plane plane;
99         auto &pBuffer = plane.buffer;
100         auto &pImageSize = plane.imageSize;
101
102         LOGD("Allocate memory [%i] for buffer in media source %p", bufferSize, this);
103         LOGD("Assign new size (%ui x %ui) of the internal buffer image for "
104                  "the media source %p",
105                  width, height, this);
106         LOGD("Assign new colorspace (%i) of the internal buffer image for "
107                  "the media source %p",
108                  colorspace, this);
109         pBuffer = new (std::nothrow) unsigned char[bufferSize];
110         if (pBuffer == NULL) {
111                 LOGE("Memory allocating for buffer in media source %p failed!", this);
112                 return false;
113         }
114
115         LOGD("Copy data from external buffer (%p) to the internal buffer (%p) of "
116                  "media source %p",
117                  buffer, pBuffer, this);
118         std::memcpy(pBuffer, buffer, bufferSize);
119
120         LOGD("Assign new size of the internal buffer of media source %p. "
121                  "New size is %ui.",
122                  this, bufferSize);
123         pImageSize = bufferSize;
124
125         LOGD("Assign new size (%ui x %ui) of the internal buffer image for "
126                  "the media source %p",
127                  width, height, this);
128         _width = width;
129         _height = height;
130         plane.bytePerLine = _width;
131
132         LOGD("Assign new colorspace (%i) of the internal buffer image for "
133                  "the media source %p",
134                  colorspace, this);
135         _colorspace = colorspace;
136         _plane.push_back(plane);
137
138         return true;
139 }
140
141 bool MediaSource::fill(const unsigned char *buffer, unsigned int bufferSize, unsigned int width, unsigned int height,
142                                            size_t offset)
143 {
144         if (bufferSize == 0 || buffer == NULL) {
145                 LOGE("bufferSize is %d and buffer[%p]", bufferSize, buffer);
146                 return false;
147         }
148
149         if (_plane.empty()) {
150                 LOGE("_plane is empty");
151                 return false;
152         }
153
154         auto &pBuffer = _plane.back().buffer;
155
156         LOGD("Allocate memory [%i] for buffer in media source %p", bufferSize, this);
157         LOGD("Assign new size (%ui x %ui) of the internal buffer image for "
158                  "the media source %p",
159                  width, height, this);
160
161         LOGD("Copy data from external buffer (%p) to the internal buffer (%p + %zd) of "
162                  "media source %p",
163                  buffer, pBuffer, offset, this);
164         std::memcpy(pBuffer + offset, buffer, bufferSize);
165
166         LOGD("size is %ui x %ui [%ui] on buffer(%p).", width, height, bufferSize, this);
167
168         return true;
169 }
170
171 unsigned char *MediaSource::getBuffer(void) const
172 {
173         if (_plane.empty())
174                 return nullptr;
175         return _plane[0].buffer;
176 }
177
178 unsigned int MediaSource::getBufferSize(void) const
179 {
180         if (_plane.empty())
181                 return 0;
182         return _plane[0].imageSize;
183 }
184
185 unsigned int MediaSource::getWidth(void) const
186 {
187         return _width;
188 }
189
190 unsigned int MediaSource::getHeight(void) const
191 {
192         return _height;
193 }
194
195 mv_colorspace_e MediaSource::getColorspace(void) const
196 {
197         return _colorspace;
198 }
199
200 void MediaSource::addPlane(Plane &plane)
201 {
202         _plane.push_back(plane);
203 }
204
205 void MediaSource::setFormat(unsigned int width, unsigned int height, mv_colorspace_e colorspace,
206                                                         media_packet_h media_packet)
207 {
208         _width = width;
209         _height = height;
210         _colorspace = colorspace;
211         _packet = media_packet;
212         if (media_packet_ref(media_packet) != MEDIA_PACKET_ERROR_NONE) {
213                 LOGE("media_packet_ref failed");
214                 throw std::runtime_error("media_packet_ref failed");
215         }
216 }
217
218 unsigned int MediaSource::getWidthStride()
219 {
220         if (_plane.empty())
221                 return _width;
222         return _plane[0].bytePerLine;
223 }
224
225 } /* Common */
226 } /* MediaVision */