Ocean
Loading...
Searching...
No Matches
MediaSerializer.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_IO_SERIALIZATION_MEDIA_SERIALIZER_H
9#define META_OCEAN_IO_SERIALIZATION_MEDIA_SERIALIZER_H
10
14
15#include "ocean/base/Frame.h"
16
19
20namespace Ocean
21{
22
23namespace IO
24{
25
26namespace Serialization
27{
28
29/**
30 * This class implements media serialization functionalities.
31 * @ingroup ioserialization
32 */
33class OCEAN_IO_SERIALIZATION_EXPORT MediaSerializer
34{
35 public:
36
37 /**
38 * This class implements a sample for Ocean::Frame objects.
39 *
40 * The class supports two primary use cases:
41 * 1. Automatic encoding/decoding:
42 * A Frame can be provided and will be automatically encoded using the Ocean Image library with a preferred encoding type (e.g., "ocn" or "jpg").
43 * The frame() function will decode the data on-demand.
44 * 2. Custom encoded data: An already encoded buffer can be provided directly (e.g., an mp4 frame with custom imageType).
45 * In this case, the user is responsible for decoding the data. The buffer() function provides direct access to the encoded data for custom decoding.
46 */
48 {
49 public:
50
51 /**
52 * Definition of a vector holding encoded frame data.
53 */
54 using Buffer = std::vector<uint8_t>;
55
56 public:
57
58 /**
59 * Default constructor creating an invalid sample.
60 */
61 DataSampleFrame() = default;
62
63 /**
64 * Creates a new DataSampleFrame object from a frame.
65 * The frame will be encoded immediately.
66 * @param frame The frame to be serialized, must be valid
67 * @param imageType The image type to be used for encoding
68 * @param camera Optional camera model to be associated with the frame
69 * @param device_T_camera Optional transformation between camera and device
70 * @param sampleCreationTimestamp The timestamp when the sample was created, this timestamp is not serialized and is only used to automatically determine the playback timestamp
71 */
72 explicit DataSampleFrame(const Frame& frame, const std::string& imageType = "ocn", SharedAnyCamera camera = SharedAnyCamera(), const HomogenousMatrixD4& device_T_camera = HomogenousMatrixD4(false), const Timestamp& sampleCreationTimestamp = Timestamp(true));
73
74 /**
75 * Creates a new DataSampleFrame object from an encoded buffer.
76 * @param buffer The encoded frame buffer, will be moved
77 * @param imageType The image type of the encoded buffer
78 * @param dataTimestamp The timestamp of the data
79 * @param camera Optional camera model to be associated with the frame
80 * @param device_T_camera Optional transformation between camera and device
81 * @param sampleCreationTimestamp The timestamp when the sample was created, this timestamp is not serialized and is only used to automatically determine the playback timestamp
82 */
83 explicit DataSampleFrame(Buffer&& buffer, const std::string& imageType, const DataTimestamp& dataTimestamp, SharedAnyCamera camera = SharedAnyCamera(), const HomogenousMatrixD4& device_T_camera = HomogenousMatrixD4(false), const Timestamp& sampleCreationTimestamp = Timestamp(true));
84
85 /**
86 * Reads the sample from an input bitstream.
87 * @param inputBitstream The input bitstream from which the sample will be read
88 * @return True, if succeeded
89 */
90 bool readSample(InputBitstream& inputBitstream) override;
91
92 /**
93 * Writes the sample to an output bitstream.
94 * @param outputBitstream The output bitstream to which the sample will be written
95 * @return True, if succeeded
96 */
97 bool writeSample(OutputBitstream& outputBitstream) const override;
98
99 /**
100 * Returns the type of the sample.
101 * @return The sample type
102 */
103 inline const std::string& type() const override;
104
105 /**
106 * Returns the frame stored in this sample.
107 * The frame will be decoded from the buffer on-demand.
108 * @param camera Optional output parameter to receive the parsed camera model, nullptr if not of interest
109 * @return The frame, invalid if the sample is invalid or decoding fails
110 */
111 Frame frame(SharedAnyCamera* camera = nullptr) const;
112
113 /**
114 * Returns the camera model associated with this sample.
115 * The camera will be parsed from the cameraModel_ JSON string on-demand.
116 * @return The camera model, nullptr if no camera model is set or parsing fails
117 */
119
120 /**
121 * Returns the encoded buffer.
122 * @return The buffer storing the encoded frame data
123 */
124 inline const Buffer& buffer() const;
125
126 /**
127 * Returns the image type used for encoding/decoding.
128 * @return The image type
129 */
130 inline const std::string& imageType() const;
131
132 /**
133 * Returns the optional camera model associated with the frame.
134 * @return The camera model (JSON string), empty if not set
135 */
136 inline const std::string& cameraModel() const;
137
138 /**
139 * Returns the device_T_camera transformation.
140 * @return The transformation between camera and device, invalid if not set
141 */
142 inline const HomogenousMatrixD4& device_T_camera() const;
143
144 /**
145 * Returns whether this sample is valid.
146 * @return True, if the buffer is not empty and the image type is not empty
147 */
148 inline bool isValid() const;
149
150 /**
151 * Returns the type of the sample.
152 * @return The sample type
153 */
154 static const std::string& sampleType();
155
156 /**
157 * Factory function for creating a DataSampleFrame.
158 * This function can be used with InputDataSerializer::registerFactoryFunction().
159 * @param sampleType The sample type (unused, but required by the factory function signature)
160 * @return A new DataSampleFrame instance
161 */
162 static UniqueDataSample createSample(const std::string& sampleType);
163
164 protected:
165
166 /// The encoded frame buffer.
168
169 /// The image type used for encoding/decoding.
170 std::string imageType_;
171
172 /// The optional camera model associated with the frame (JSON string from CameraCalibrationManager).
173 std::string cameraModel_;
174
175 /// The optional transformation between camera and device.
176 HomogenousMatrixD4 device_T_camera_ = HomogenousMatrixD4(false);
177 };
178};
179
180inline const std::string& MediaSerializer::DataSampleFrame::type() const
181{
182 return sampleType();
183}
184
189
190inline const std::string& MediaSerializer::DataSampleFrame::imageType() const
191{
192 return imageType_;
193}
194
195inline const std::string& MediaSerializer::DataSampleFrame::cameraModel() const
196{
197 return cameraModel_;
198}
199
201{
202 return device_T_camera_;
203}
204
206{
207 return !buffer_.empty() && !imageType_.empty();
208}
209
210}
211
212}
213
214}
215
216#endif // META_OCEAN_IO_SERIALIZATION_MEDIA_SERIALIZER_H
This class implements Ocean's image class.
Definition Frame.h:1879
This class implements an input bitstream.
Definition Bitstream.h:51
This class implements an output bitstream.
Definition Bitstream.h:215
This class implements a base class for data samples.
Definition DataSample.h:46
This class implements a data timestamp which can hold either a double or int64_t value.
Definition DataTimestamp.h:29
This class implements a sample for Ocean::Frame objects.
Definition MediaSerializer.h:48
std::string imageType_
The image type used for encoding/decoding.
Definition MediaSerializer.h:170
static const std::string & sampleType()
Returns the type of the sample.
DataSampleFrame()=default
Default constructor creating an invalid sample.
SharedAnyCamera camera() const
Returns the camera model associated with this sample.
bool writeSample(OutputBitstream &outputBitstream) const override
Writes the sample to an output bitstream.
Buffer buffer_
The encoded frame buffer.
Definition MediaSerializer.h:167
const Buffer & buffer() const
Returns the encoded buffer.
Definition MediaSerializer.h:185
bool readSample(InputBitstream &inputBitstream) override
Reads the sample from an input bitstream.
DataSampleFrame(const Frame &frame, const std::string &imageType="ocn", SharedAnyCamera camera=SharedAnyCamera(), const HomogenousMatrixD4 &device_T_camera=HomogenousMatrixD4(false), const Timestamp &sampleCreationTimestamp=Timestamp(true))
Creates a new DataSampleFrame object from a frame.
const HomogenousMatrixD4 & device_T_camera() const
Returns the device_T_camera transformation.
Definition MediaSerializer.h:200
const std::string & cameraModel() const
Returns the optional camera model associated with the frame.
Definition MediaSerializer.h:195
DataSampleFrame(Buffer &&buffer, const std::string &imageType, const DataTimestamp &dataTimestamp, SharedAnyCamera camera=SharedAnyCamera(), const HomogenousMatrixD4 &device_T_camera=HomogenousMatrixD4(false), const Timestamp &sampleCreationTimestamp=Timestamp(true))
Creates a new DataSampleFrame object from an encoded buffer.
Frame frame(SharedAnyCamera *camera=nullptr) const
Returns the frame stored in this sample.
std::string cameraModel_
The optional camera model associated with the frame (JSON string from CameraCalibrationManager).
Definition MediaSerializer.h:173
const std::string & type() const override
Returns the type of the sample.
Definition MediaSerializer.h:180
std::vector< uint8_t > Buffer
Definition of a vector holding encoded frame data.
Definition MediaSerializer.h:54
static UniqueDataSample createSample(const std::string &sampleType)
Factory function for creating a DataSampleFrame.
bool isValid() const
Returns whether this sample is valid.
Definition MediaSerializer.h:205
const std::string & imageType() const
Returns the image type used for encoding/decoding.
Definition MediaSerializer.h:190
This class implements media serialization functionalities.
Definition MediaSerializer.h:34
This class implements a timestamp.
Definition Timestamp.h:64
std::unique_ptr< DataSample > UniqueDataSample
Definition of a unique pointer holding a DataSample.
Definition DataSample.h:39
std::shared_ptr< AnyCamera > SharedAnyCamera
Definition of a shared pointer holding an AnyCamera object with Scalar precision.
Definition AnyCamera.h:61
The namespace covering the entire Ocean framework.
Definition Accessor.h:15