2 * Copyright 2015, Dario Casalinuovo
3 * Copyright 2010, Oleg Krysenkov, beos344@mail.ru.
4 * Copyright 2012, Fredrik Modéen, [firstname]@[lastname].se.
5 * Copyright 2004-2007, Marcus Overhagen. All rights reserved.
6 * Distributed under the terms of the MIT License.
10 #include <MediaEncoder.h>
12 #include <EncoderPlugin.h>
13 #include <PluginManager.h>
19 /*************************************************************
20 * public BMediaEncoder
21 *************************************************************/
23 BMediaEncoder::BMediaEncoder()
26 fInitStatus(B_NO_INIT
)
32 BMediaEncoder::BMediaEncoder(const media_format
* outputFormat
)
35 fInitStatus(B_NO_INIT
)
42 BMediaEncoder::BMediaEncoder(const media_codec_info
* mci
)
45 fInitStatus(B_NO_INIT
)
53 BMediaEncoder::~BMediaEncoder()
61 BMediaEncoder::InitCheck() const
68 BMediaEncoder::SetTo(const media_format
* outputFormat
)
72 status_t err
= B_ERROR
;
75 if (outputFormat
== NULL
)
78 media_format format
= *outputFormat
;
79 err
= gPluginManager
.CreateEncoder(&fEncoder
, format
);
80 if (fEncoder
!= NULL
&& err
== B_OK
) {
81 err
= _AttachToEncoder();
92 BMediaEncoder::SetTo(const media_codec_info
* mci
)
97 status_t err
= gPluginManager
.CreateEncoder(&fEncoder
, mci
, 0);
98 if (fEncoder
!= NULL
&& err
== B_OK
) {
99 err
= _AttachToEncoder();
113 BMediaEncoder::SetFormat(media_format
* inputFormat
,
114 media_format
* outputFormat
, media_file_format
* mfi
)
117 TRACE("BMediaEncoder::SetFormat. Input = %d, Output = %d\n",
118 inputFormat
->type
, outputFormat
->type
);
123 if (outputFormat
!= NULL
)
126 //TODO: How we support mfi?
127 return fEncoder
->SetUp(inputFormat
);
132 BMediaEncoder::Encode(const void* buffer
,
133 int64 frameCount
, media_encode_info
* info
)
140 return fEncoder
->Encode(buffer
, frameCount
, info
);
145 BMediaEncoder::GetEncodeParameters(encode_parameters
* parameters
) const
149 if (fEncoder
== NULL
)
152 return fEncoder
->GetEncodeParameters(parameters
);
157 BMediaEncoder::SetEncodeParameters(encode_parameters
* parameters
)
161 if (fEncoder
== NULL
)
164 return fEncoder
->SetEncodeParameters(parameters
);
168 /*************************************************************
169 * protected BMediaEncoder
170 *************************************************************/
172 /* virtual */ status_t
173 BMediaEncoder::AddTrackInfo(uint32 code
, const char* data
, size_t size
)
177 if (fEncoder
== NULL
)
180 return fEncoder
->AddTrackInfo(code
, data
, size
);
184 /*************************************************************
185 * private BMediaEncoder
186 *************************************************************/
190 BMediaEncoder::BMediaEncoder(const BMediaEncoder &);
191 BMediaEncoder::BMediaEncoder & operator=(const BMediaEncoder &);
194 /* static */ status_t
195 BMediaEncoder::write_chunk(void* classptr
, const void* chunk_data
,
196 size_t chunk_len
, media_encode_info
* info
)
200 BMediaEncoder
* encoder
= static_cast<BMediaEncoder
*>(classptr
);
203 return encoder
->WriteChunk(chunk_data
, chunk_len
, info
);
208 BMediaEncoder::Init()
215 BMediaEncoder::ReleaseEncoder()
218 if (fEncoder
!= NULL
) {
219 gPluginManager
.DestroyEncoder(fEncoder
);
222 fInitStatus
= B_NO_INIT
;
227 BMediaEncoder::_AttachToEncoder()
229 class MediaEncoderChunkWriter
: public ChunkWriter
{
231 MediaEncoderChunkWriter(BMediaEncoder
* encoder
)
235 virtual status_t
WriteChunk(const void* chunkBuffer
,
236 size_t chunkSize
, media_encode_info
* encodeInfo
)
238 return fEncoder
->WriteChunk(chunkBuffer
, chunkSize
, encodeInfo
);
241 BMediaEncoder
* fEncoder
;
242 } *writer
= new(std::nothrow
) MediaEncoderChunkWriter(this);
247 fEncoder
->SetChunkWriter(writer
);
252 status_t
BMediaEncoder::_Reserved_BMediaEncoder_0(int32 arg
, ...) { return B_ERROR
; }
253 status_t
BMediaEncoder::_Reserved_BMediaEncoder_1(int32 arg
, ...) { return B_ERROR
; }
254 status_t
BMediaEncoder::_Reserved_BMediaEncoder_2(int32 arg
, ...) { return B_ERROR
; }
255 status_t
BMediaEncoder::_Reserved_BMediaEncoder_3(int32 arg
, ...) { return B_ERROR
; }
256 status_t
BMediaEncoder::_Reserved_BMediaEncoder_4(int32 arg
, ...) { return B_ERROR
; }
257 status_t
BMediaEncoder::_Reserved_BMediaEncoder_5(int32 arg
, ...) { return B_ERROR
; }
258 status_t
BMediaEncoder::_Reserved_BMediaEncoder_6(int32 arg
, ...) { return B_ERROR
; }
259 status_t
BMediaEncoder::_Reserved_BMediaEncoder_7(int32 arg
, ...) { return B_ERROR
; }
260 status_t
BMediaEncoder::_Reserved_BMediaEncoder_8(int32 arg
, ...) { return B_ERROR
; }
261 status_t
BMediaEncoder::_Reserved_BMediaEncoder_9(int32 arg
, ...) { return B_ERROR
; }
262 status_t
BMediaEncoder::_Reserved_BMediaEncoder_10(int32 arg
, ...) { return B_ERROR
; }
263 status_t
BMediaEncoder::_Reserved_BMediaEncoder_11(int32 arg
, ...) { return B_ERROR
; }
264 status_t
BMediaEncoder::_Reserved_BMediaEncoder_12(int32 arg
, ...) { return B_ERROR
; }
265 status_t
BMediaEncoder::_Reserved_BMediaEncoder_13(int32 arg
, ...) { return B_ERROR
; }
266 status_t
BMediaEncoder::_Reserved_BMediaEncoder_14(int32 arg
, ...) { return B_ERROR
; }
267 status_t
BMediaEncoder::_Reserved_BMediaEncoder_15(int32 arg
, ...) { return B_ERROR
; }
269 /*************************************************************
270 * public BMediaBufferEncoder
271 *************************************************************/
273 BMediaBufferEncoder::BMediaBufferEncoder()
282 BMediaBufferEncoder::BMediaBufferEncoder(const media_format
* outputFormat
)
284 BMediaEncoder(outputFormat
),
291 BMediaBufferEncoder::BMediaBufferEncoder(const media_codec_info
* mci
)
301 BMediaBufferEncoder::EncodeToBuffer(void* outputBuffer
,
302 size_t* outputSize
, const void* inputBuffer
,
303 int64 frameCount
, media_encode_info
* info
)
308 fBuffer
= outputBuffer
;
309 fBufferSize
= *outputSize
;
310 error
= Encode(inputBuffer
, frameCount
, info
);
315 *outputSize
= fBufferSize
;
321 /*************************************************************
322 * public BMediaBufferEncoder
323 *************************************************************/
326 BMediaBufferEncoder::WriteChunk(const void* chunkData
,
327 size_t chunkLen
, media_encode_info
* info
)
332 return B_ENTRY_NOT_FOUND
;
334 if (chunkLen
> (size_t)fBufferSize
) {
335 memcpy(fBuffer
, chunkData
, fBufferSize
);
337 return B_DEVICE_FULL
;
340 memcpy(fBuffer
, chunkData
, chunkLen
);
341 fBufferSize
= chunkLen
;