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 * The class provides specialized data sample types for common media content, particularly image frames.<br>
32 * It contains the DataSampleFrame nested class for serializing Ocean::Frame objects with optional camera models and device-to-camera transformations.
33 * @ingroup ioserialization
34 */
35class OCEAN_IO_SERIALIZATION_EXPORT MediaSerializer
36{
37 public:
38
39 /**
40 * This class implements a sample for Ocean::Frame objects.
41 * The class supports encoding/decoding of image frames along with optional camera models and device-to-camera transformations.
42 *
43 * The class supports two primary use cases:<br>
44 * 1. Automatic encoding/decoding:<br>
45 * 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").<br>
46 * The frame() function will decode the data on-demand.<br>
47 * 2. Custom encoded data: An already encoded buffer can be provided directly (e.g., an mp4 frame with custom imageType).<br>
48 * In this case, the user is responsible for decoding the data.<br>
49 * The buffer() function provides direct access to the encoded data for custom decoding.
50 */
51 class OCEAN_IO_SERIALIZATION_EXPORT DataSampleFrame : public DataSample
52 {
53 public:
54
55 /**
56 * Definition of a vector holding encoded frame data.
57 */
58 using Buffer = std::vector<uint8_t>;
59
60 public:
61
62 /**
63 * Default constructor creating an invalid sample.
64 */
65 DataSampleFrame() = default;
66
67 /**
68 * Creates a new DataSampleFrame object from a frame.
69 * The frame will be encoded immediately.
70 * @param frame The frame to be serialized, must be valid
71 * @param imageType The image type to be used for encoding
72 * @param camera Optional camera model to be associated with the frame
73 * @param device_T_camera Optional transformation between camera and device
74 * @param sampleCreationTimestamp The timestamp when the sample was created, this timestamp is not serialized and is only used to automatically determine the playback timestamp
75 */
76 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));
77
78 /**
79 * Creates a new DataSampleFrame object from an encoded buffer.
80 * @param buffer The encoded frame buffer, will be moved
81 * @param imageType The image type of the encoded buffer
82 * @param dataTimestamp The timestamp of the data
83 * @param camera Optional camera model to be associated with the frame
84 * @param device_T_camera Optional transformation between camera and device
85 * @param sampleCreationTimestamp The timestamp when the sample was created, this timestamp is not serialized and is only used to automatically determine the playback timestamp
86 */
87 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));
88
89 /**
90 * Reads the sample from an input bitstream.
91 * @param inputBitstream The input bitstream from which the sample will be read
92 * @return True, if succeeded
93 */
94 bool readSample(InputBitstream& inputBitstream) override;
95
96 /**
97 * Writes the sample to an output bitstream.
98 * @param outputBitstream The output bitstream to which the sample will be written
99 * @return True, if succeeded
100 */
101 bool writeSample(OutputBitstream& outputBitstream) const override;
102
103 /**
104 * Returns the type of the sample.
105 * @return The sample type
106 */
107 inline const std::string& type() const override;
108
109 /**
110 * Returns the frame stored in this sample.
111 * The frame will be decoded from the buffer on-demand.
112 * @param camera Optional output parameter to receive the parsed camera model, nullptr if not of interest
113 * @return The frame, invalid if the sample is invalid or decoding fails
114 */
115 Frame frame(SharedAnyCamera* camera = nullptr) const;
116
117 /**
118 * Returns the camera model associated with this sample.
119 * The camera will be parsed from the cameraModel_ JSON string on-demand.
120 * @return The camera model, nullptr if no camera model is set or parsing fails
121 */
123
124 /**
125 * Returns the encoded buffer.
126 * @return The buffer storing the encoded frame data
127 */
128 inline const Buffer& buffer() const;
129
130 /**
131 * Returns the image type used for encoding/decoding.
132 * @return The image type
133 */
134 inline const std::string& imageType() const;
135
136 /**
137 * Returns the optional camera model associated with the frame.
138 * @return The camera model (JSON string), empty if not set
139 */
140 inline const std::string& cameraModel() const;
141
142 /**
143 * Returns the device_T_camera transformation.
144 * @return The transformation between camera and device, invalid if not set
145 */
146 inline const HomogenousMatrixD4& device_T_camera() const;
147
148 /**
149 * Returns whether this sample is valid.
150 * @return True, if the buffer is not empty and the image type is not empty
151 */
152 inline bool isValid() const;
153
154 /**
155 * Returns the type of the sample.
156 * @return The sample type
157 */
158 static const std::string& sampleType();
159
160 /**
161 * Factory function for creating a DataSampleFrame.
162 * This function can be used with InputDataSerializer::registerFactoryFunction().
163 * @param sampleType The sample type (unused, but required by the factory function signature)
164 * @return A new DataSampleFrame instance
165 */
166 static UniqueDataSample createSample(const std::string& sampleType);
167
168 protected:
169
170 /// The encoded frame buffer.
172
173 /// The image type used for encoding/decoding.
174 std::string imageType_;
175
176 /// The optional camera model associated with the frame (JSON string from CameraCalibrationManager).
177 std::string cameraModel_;
178
179 /// The optional transformation between camera and device.
180 HomogenousMatrixD4 device_T_camera_ = HomogenousMatrixD4(false);
181 };
182};
183
184inline const std::string& MediaSerializer::DataSampleFrame::type() const
185{
186 return sampleType();
187}
188
193
194inline const std::string& MediaSerializer::DataSampleFrame::imageType() const
195{
196 return imageType_;
197}
198
199inline const std::string& MediaSerializer::DataSampleFrame::cameraModel() const
200{
201 return cameraModel_;
202}
203
205{
206 return device_T_camera_;
207}
208
210{
211 return !buffer_.empty() && !imageType_.empty();
212}
213
214}
215
216}
217
218}
219
220#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:50
This class implements a data timestamp which can hold either a double or int64_t value.
Definition DataTimestamp.h:32
This class implements a sample for Ocean::Frame objects.
Definition MediaSerializer.h:52
std::string imageType_
The image type used for encoding/decoding.
Definition MediaSerializer.h:174
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:171
const Buffer & buffer() const
Returns the encoded buffer.
Definition MediaSerializer.h:189
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:204
const std::string & cameraModel() const
Returns the optional camera model associated with the frame.
Definition MediaSerializer.h:199
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:177
const std::string & type() const override
Returns the type of the sample.
Definition MediaSerializer.h:184
std::vector< uint8_t > Buffer
Definition of a vector holding encoded frame data.
Definition MediaSerializer.h:58
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:209
const std::string & imageType() const
Returns the image type used for encoding/decoding.
Definition MediaSerializer.h:194
This class implements media serialization functionalities.
Definition MediaSerializer.h:36
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