#include <cstring>
#include <new>
+#include <stdexcept>
namespace MediaVision
{
void MediaSource::clear(void)
{
- if (!_isRef) {
+ if (_packet) {
+ media_packet_unref(_packet);
+ } else {
for (const auto &p : _plane) {
delete[] p.buffer;
}
_width = 0;
_height = 0;
_colorspace = MEDIA_VISION_COLORSPACE_INVALID;
- _isRef = false;
+ _packet = nullptr;
}
bool MediaSource::fill(const unsigned char *buffer, unsigned int bufferSize, unsigned int width, unsigned int height,
return _colorspace;
}
+void MediaSource::addPlane(Plane &plane)
+{
+ _plane.push_back(plane);
+}
+
+void MediaSource::setFormat(unsigned int width, unsigned int height, mv_colorspace_e colorspace,
+ media_packet_h media_packet)
+{
+ _width = width;
+ _height = height;
+ _colorspace = colorspace;
+ _packet = media_packet;
+ if (media_packet_ref(media_packet) != MEDIA_PACKET_ERROR_NONE) {
+ LOGE("media_packet_ref failed");
+ throw std::runtime_error("media_packet_ref failed");
+ }
+}
+
} /* Common */
} /* MediaVision */
int image_width = 0;
int image_height = 0;
- int plane_width[4] = {
- 0,
- };
- int plane_height[4] = {
- 0,
- };
- uint64_t plane_size[4] = {
- 0,
- };
- size_t offset = 0;
media_format_h format = NULL;
media_format_mimetype_e mimetype = MEDIA_FORMAT_I420;
unsigned char *data_buffer = NULL;
- uint64_t buffer_size = 0;
mv_colorspace_e image_colorspace = MEDIA_VISION_COLORSPACE_INVALID;
int ret = media_packet_is_video(media_packet, &is_video);
}
ret = media_packet_get_number_of_video_planes(media_packet, &plane_num);
- if (plane_num <= 0 || ret != MEDIA_VISION_ERROR_NONE) {
+ if (plane_num <= 0 || ret != MEDIA_PACKET_ERROR_NONE) {
LOGE("invalid plane_num [%d] is returned", plane_num);
return MEDIA_VISION_ERROR_INVALID_PARAMETER;
}
LOGI("%d planes with color_space [%d]", plane_num, image_colorspace);
+ MediaVision::Common::MediaSource *mediaSource = static_cast<MediaVision::Common::MediaSource *>(source);
+ try {
+ mediaSource->setFormat((unsigned int) (image_width), (unsigned int) (image_height), image_colorspace,
+ media_packet);
+ } catch (const std::exception &e) {
+ return MEDIA_VISION_ERROR_INTERNAL;
+ }
+
for (ind = 0; ind < plane_num; ++ind) {
ret = media_packet_get_video_stride_width(media_packet, ind, &image_width);
- if (image_width <= 0 || ret != MEDIA_VISION_ERROR_NONE) {
+ if (image_width <= 0 || ret != MEDIA_PACKET_ERROR_NONE) {
LOGE("invalid plane width [%d]", image_width);
return MEDIA_VISION_ERROR_INVALID_PARAMETER;
}
ret = media_packet_get_video_stride_height(media_packet, ind, &image_height);
- if (image_height <= 0 || ret != MEDIA_VISION_ERROR_NONE) {
+ if (image_height <= 0 || ret != MEDIA_PACKET_ERROR_NONE) {
LOGE("invalid plane width [%d]", image_height);
return MEDIA_VISION_ERROR_INVALID_PARAMETER;
}
- plane_width[ind] = image_width;
- plane_height[ind] = image_height;
- plane_size[ind] = static_cast<uint64_t>(image_width) * static_cast<uint64_t>(image_height);
- buffer_size += plane_size[ind];
- }
-
- if (!(static_cast<MediaVision::Common::MediaSource *>(source))
- ->alloc(buffer_size, static_cast<unsigned int>(plane_width[0]),
- static_cast<unsigned int>(plane_height[0]), image_colorspace)) {
- LOGE("mv_source_h alloc from media_packet_h failed");
- return MEDIA_VISION_ERROR_OUT_OF_MEMORY;
- }
-
- for (ind = 0; ind < plane_num; ++ind) {
ret = media_packet_get_video_plane_data_ptr(media_packet, ind, (void **) &data_buffer);
if (ret != MEDIA_PACKET_ERROR_NONE) {
LOGE("media_packet_get_video_plane_data_ptr() plane[%d] failed, mv_source_h fill skipped", ind);
return MEDIA_VISION_ERROR_INVALID_PARAMETER;
}
-
- offset = (ind == 0) ? 0 : plane_size[ind - 1] * sizeof(char);
- if (!(static_cast<MediaVision::Common::MediaSource *>(source))
- ->fill(data_buffer, plane_size[ind], static_cast<unsigned int>(plane_width[ind]),
- static_cast<unsigned int>(plane_height[ind]), offset)) {
- LOGE("mv_source_h filling from media_packet_h failed");
- return MEDIA_VISION_ERROR_OUT_OF_MEMORY;
- }
- data_buffer = NULL;
+ MediaVision::Common::Plane plane { data_buffer, (unsigned int) (image_width * image_height),
+ (unsigned int) (image_width) };
+ mediaSource->addPlane(plane);
}
LOGD("Media source has been filled from media packet");