20 #include <com/avpkit/ferry/Logger.h>
21 #include <com/avpkit/ferry/Buffer.h>
22 #include <com/avpkit/core/Packet.h>
28 VS_LOG_SETUP(VS_CPP_PACKAGE);
30 namespace com {
namespace avpkit {
namespace core
37 mPacket = (AVPacket*)av_malloc(
sizeof(AVPacket));
39 throw std::bad_alloc();
44 av_init_packet(mPacket);
65 return (mPacket ? mPacket->pts : (int64_t)-1);
71 if (mPacket) mPacket->pts = aPts;
77 return (mPacket ? mPacket->dts : (int64_t)-1);
83 if (mPacket) mPacket->dts = aDts;
89 return (mPacket ? mPacket->size: (int32_t)-1);
94 return (mBuffer ? mBuffer->getBufferSize() : -1);
99 return (mPacket ? mPacket->stream_index: (int32_t)-1);
104 return (mPacket ? mPacket->flags: (int32_t)-1);
109 return (mPacket ? mPacket->flags & AV_PKT_FLAG_KEY :
false);
118 mPacket->flags |= AV_PKT_FLAG_KEY;
128 mPacket->flags = flags;
134 mIsComplete = complete;
138 mPacket->size = size;
146 mPacket->stream_index = streamIndex;
151 return (mPacket ? mPacket->duration: (int64_t)-1);
157 if (mPacket) mPacket->duration = duration;
163 return (mPacket ? mPacket->pos: (int64_t)-1);
169 if (mPacket) mPacket->pos = position;
175 return mBuffer.get();
179 Packet :: wrapAVPacket(AVPacket* pkt)
183 VS_ASSERT(mPacket,
"No packet?");
187 (void) this->allocateNewPayload(pkt->size);
190 if (pkt->data && pkt->size)
191 memcpy(mPacket->data, pkt->data, pkt->size);
195 uint8_t* data_buf = mPacket->data;
202 mPacket->data = data_buf;
204 av_copy_packet_side_data(mPacket, pkt);
206 setComplete(
true, mPacket->size);
214 av_packet_unref(mPacket);
215 av_init_packet(mPacket);
217 setComplete(
false, 0);
229 throw std::bad_alloc();
232 catch (std::bad_alloc & e)
234 VS_REF_RELEASE(retval);
244 Packet *retval= NULL;
248 retval->wrapBuffer(buffer);
256 Packet* retval= NULL;
258 IRational* timeBase = NULL;
262 throw std::runtime_error(
"need packet to copy");
266 int32_t numBytes = packet->getSize();
267 retval = make(numBytes);
268 if (!retval || !retval->mPacket || !retval->mPacket->data)
269 throw std::bad_alloc();
270 if (numBytes > 0 && packet->mPacket->data)
271 memcpy(retval->mPacket->data, packet->mPacket->data,
274 buffer=packet->getData();
275 retval = make(buffer);
277 throw std::bad_alloc();
281 uint8_t* data_buf = retval->mPacket->data;
286 *(retval->mPacket) = *(packet->mPacket);
287 retval->mPacket->buf = NULL;
288 retval->mPacket->data = data_buf;
290 av_copy_packet_side_data(retval->mPacket, packet->mPacket);
292 timeBase = packet->getTimeBase();
293 retval->setTimeBase(timeBase);
295 retval->setComplete(retval->mPacket->size > 0,
296 retval->mPacket->size);
298 catch (std::exception &e)
300 VS_REF_RELEASE(retval);
302 VS_REF_RELEASE(buffer);
303 VS_REF_RELEASE(timeBase);
313 uint8_t* payload = 0;
317 if (!mBuffer || mBuffer->getBufferSize() < payloadSize)
320 payload = (uint8_t*) av_malloc(payloadSize+AV_INPUT_BUFFER_PADDING_SIZE);
322 throw std::bad_alloc();
327 Packet::freeAVBuffer, 0);
330 throw std::bad_alloc();
333 memset(payload + payloadSize,
335 AV_INPUT_BUFFER_PADDING_SIZE);
337 payload = (uint8_t*)mBuffer->getBytes(0, payloadSize);
339 VS_ASSERT(mPacket,
"Should already have a packet");
340 VS_ASSERT(mBuffer,
"Should have allocated a buffer");
341 VS_ASSERT(payload,
"Should have allocated a payload");
342 if (mBuffer && mPacket)
344 mPacket->data = payload;
348 this->setComplete(
false, 0);
362 Packet :: wrapBuffer(
IBuffer *buffer)
364 if (buffer != mBuffer.value())
368 mBuffer.reset(buffer,
true);
373 VS_ASSERT(mPacket,
"No AVPacket");
376 mPacket->size = mBuffer->getBufferSize();
377 mPacket->data = (uint8_t*)mBuffer->getBytes(0, mPacket->size);
379 setComplete(
true, mPacket->size);
386 return mIsComplete && mPacket->data;
390 Packet :: freeAVBuffer(
void * buf,
void * closure)
static IPacket * make()
Allocate a new packet.
virtual void setFlags(int32_t flags)
Set any internal flags.
virtual int32_t allocateNewPayload(int32_t payloadSize)
Discard the current payload and allocate a new payload.
virtual int64_t getPts()
Get the Presentation Time Stamp (PTS) for this packet.
virtual void setKeyPacket(bool keyPacket)
Set if this is a key packet.
virtual void setDuration(int64_t duration)
Set the duration.
virtual com::avpkit::ferry::IBuffer * getData()
Get any underlying raw data available for this object.
virtual void setPosition(int64_t position)
Set the position.
virtual int32_t getMaxSize()
Get the maximum size (in bytes) of payload this packet can hold.
virtual bool isKeyPacket()
Does this packet contain Key data? i.e.
virtual void setStreamIndex(int32_t streamIndex)
Set the stream index for this packet.
virtual void reset()
Clear out any data in this packet, but leaves the buffer available for reuse.
virtual int32_t getSize()
Get the size in bytes of the payload currently in this packet.
virtual void setPts(int64_t pts)
Set a new Presentation Time Stamp (PTS) for this packet.
virtual void setData(com::avpkit::ferry::IBuffer *buffer)
Sets the underlying buffer used by this object.
virtual void setDts(int64_t dts)
Set a new Decompression Time Stamp (DTS) for this packet.
virtual int32_t getFlags()
Get any flags set on this packet, as a 4-byte binary-ORed bit-mask.
virtual bool isComplete()
Is this packet complete.
virtual void setComplete(bool complete, int32_t size)
Set if this packet is complete, and what the total size of the data should be assumed to be.
virtual int32_t getStreamIndex()
Get the container-specific index for the stream this packet is part of.
virtual int64_t getPosition()
Return the position (in bytes) of this packet in the stream.
virtual int64_t getDts()
Get the Decompression Time Stamp (DTS) for this packet.
virtual int64_t getDuration()
Return the duration of this packet, in units of getTimeBase().
static VS_API_FERRY Buffer * make(RefCounted *requestor, int32_t bufferSize)
Allocate a new buffer of at least bufferSize.
Allows Java code to get data from a native buffers, and optionally modify native memory directly.
This library contains routines used by AVPKit libraries for "ferry"ing Java objects to and from nativ...
WARNING: Do not use logging in this class, and do not set any static file variables to values other t...