2 * Copyright (c) 2015 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.
17 #include "MediaSource.h"
19 #include <mv_private.h>
29 MediaSource::~MediaSource()
34 bool MediaSource::alloc(unsigned int bufferSize, unsigned int width, unsigned int height, mv_colorspace_e colorspace)
39 LOGD("Call clear() first for media source %p", this);
43 auto &pBuffer = plane.buffer;
44 auto &pImageSize = plane.imageSize;
46 pBuffer = new (std::nothrow) unsigned char[bufferSize];
47 if (pBuffer == NULL) {
48 LOGE("Memory allocating for buffer in media source %p failed!", this);
52 LOGD("Assign new size of the internal buffer of media source %p. "
55 pImageSize = bufferSize;
57 LOGD("Assign new size (%ui x %ui) of the internal buffer image for "
58 "the media source %p",
63 LOGD("Assign new colorspace (%i) of the internal buffer image for "
64 "the media source %p",
66 _colorspace = colorspace;
68 _plane.push_back(plane);
73 void MediaSource::clear(void)
76 media_packet_unref(_packet);
78 for (const auto &p : _plane) {
85 _colorspace = MEDIA_VISION_COLORSPACE_INVALID;
89 bool MediaSource::fill(const unsigned char *buffer, unsigned int bufferSize, unsigned int width, unsigned int height,
90 mv_colorspace_e colorspace)
92 if (bufferSize == 0 || buffer == NULL)
95 LOGD("Call clear() first for media source %p", this);
99 auto &pBuffer = plane.buffer;
100 auto &pImageSize = plane.imageSize;
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",
109 pBuffer = new (std::nothrow) unsigned char[bufferSize];
110 if (pBuffer == NULL) {
111 LOGE("Memory allocating for buffer in media source %p failed!", this);
115 LOGD("Copy data from external buffer (%p) to the internal buffer (%p) of "
117 buffer, pBuffer, this);
118 std::memcpy(pBuffer, buffer, bufferSize);
120 LOGD("Assign new size of the internal buffer of media source %p. "
123 pImageSize = bufferSize;
125 LOGD("Assign new size (%ui x %ui) of the internal buffer image for "
126 "the media source %p",
127 width, height, this);
130 plane.bytePerLine = _width;
132 LOGD("Assign new colorspace (%i) of the internal buffer image for "
133 "the media source %p",
135 _colorspace = colorspace;
136 _plane.push_back(plane);
141 bool MediaSource::fill(const unsigned char *buffer, unsigned int bufferSize, unsigned int width, unsigned int height,
144 if (bufferSize == 0 || buffer == NULL) {
145 LOGE("bufferSize is %d and buffer[%p]", bufferSize, buffer);
149 if (_plane.empty()) {
150 LOGE("_plane is empty");
154 auto &pBuffer = _plane.back().buffer;
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);
161 LOGD("Copy data from external buffer (%p) to the internal buffer (%p + %zd) of "
163 buffer, pBuffer, offset, this);
164 std::memcpy(pBuffer + offset, buffer, bufferSize);
166 LOGD("size is %ui x %ui [%ui] on buffer(%p).", width, height, bufferSize, this);
171 unsigned char *MediaSource::getBuffer(void) const
175 return _plane[0].buffer;
178 unsigned int MediaSource::getBufferSize(void) const
182 return _plane[0].imageSize;
185 unsigned int MediaSource::getWidth(void) const
190 unsigned int MediaSource::getHeight(void) const
195 mv_colorspace_e MediaSource::getColorspace(void) const
200 void MediaSource::addPlane(Plane &plane)
202 _plane.push_back(plane);
205 void MediaSource::setFormat(unsigned int width, unsigned int height, mv_colorspace_e colorspace,
206 media_packet_h media_packet)
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");
218 unsigned int MediaSource::getWidthStride()
222 return _plane[0].bytePerLine;