23 #include <com/avpkit/ferry/Logger.h>
24 #include <com/avpkit/ferry/RefPointer.h>
26 #include <com/avpkit/core/Global.h>
27 #include <com/avpkit/core/Stream.h>
28 #include <com/avpkit/core/Rational.h>
29 #include <com/avpkit/core/StreamCoder.h>
30 #include <com/avpkit/core/Container.h>
31 #include <com/avpkit/core/MetaData.h>
32 #include <com/avpkit/core/Packet.h>
33 #include <com/avpkit/core/IIndexEntry.h>
35 #include "FfmpegIncludes.h"
37 VS_LOG_SETUP(VS_CPP_PACKAGE);
39 namespace com {
namespace avpkit {
namespace core
66 VS_REF_RELEASE(mCoder);
72 av_bsf_free(&mBsfContext);
76 Stream :: make(Container *container, AVStream * aStream, Direction direction,
const AVCodec* avCodec)
79 Stream *newStream = 0;
83 newStream->mStream = aStream;
84 newStream->mDirection = direction;
87 direction == INBOUND?IStreamCoder::DECODING : IStreamCoder::ENCODING,
88 direction == INBOUND?aStream->codecpar : NULL,
91 newStream->mContainer = container;
92 }
catch (std::bad_alloc & e) {
93 VS_REF_RELEASE(newStream);
103 return (mStream ? mStream->index : -1);
109 return (mStream ? mStream->id : -1);
119 VS_REF_ACQUIRE(retval);
130 AVRational f = av_guess_frame_rate(NULL, mStream, NULL);
156 Stream :: setFrameRate(IRational* src)
160 mStream->r_frame_rate.den = src->getDenominator();
161 mStream->r_frame_rate.num = src->getNumerator();
186 return (mStream ? mStream->nb_frames : 0);
191 return (mStream ? mStream->nb_index_entries : 0);
198 mCoder->streamClosed(
this);
208 VS_LOG_TRACE(
"Acquired %p: %d",
this, retval);
217 VS_LOG_TRACE(
"Released %p: %d",
this, retval);
228 mStream->sample_aspect_ratio.num,
229 mStream->sample_aspect_ratio.den);
237 if (aNewValue && mStream)
239 mStream->sample_aspect_ratio.num =
241 mStream->sample_aspect_ratio.den =
250 const char*retval = 0;
263 metaData->setValue(
"language", aNewValue);
272 VS_REF_ACQUIRE(mContainer);
287 if (mCoder && mCoder->
isOpen())
288 throw std::runtime_error(
"cannot call setStreamCoder when current coder is open");
291 throw std::runtime_error(
"cannot set to a null stream coder");
295 throw std::runtime_error(
"IStreamCoder is not of expected underlying C++ type");
300 mCoder->streamClosed(
this);
303 if (coder->setStream(
this, assumeOnlyStream) < 0)
304 throw std::runtime_error(
"IStreamCoder doesn't like this stream");
306 VS_REF_RELEASE(mCoder);
308 VS_REF_ACQUIRE(mCoder);
311 catch (std::exception & e)
313 VS_LOG_ERROR(
"Error: %s", e.what());
325 return IStream::PARSE_NONE;
333 mStream->need_parsing = (
enum AVStreamParseType)type;
344 const AVBitStreamFilter* filter = av_bsf_get_by_name(name);
346 if (ret = av_bsf_alloc(filter, &mBsfContext) == 0) {
347 if (ret = avcodec_parameters_copy(mBsfContext->par_in, mStream->codecpar) >= 0) {
348 mBsfContext->time_base_in = mStream->time_base;
349 if (ret = av_bsf_init(mBsfContext) < 0) {
350 VS_LOG_ERROR(
"BSF INIT FAIL");
351 av_bsf_free(&mBsfContext);
354 VS_LOG_ERROR(
"BSF OK");
360 VS_LOG_ERROR(
"Bistream filter already set on this stream");
370 if (!mMetaData && mStream)
372 if (mDirection == IStream::OUTBOUND)
379 return mMetaData.get();
409 if (!thisBase || !packetBase)
411 if (thisBase->compareTo(packetBase.
value()) == 0) {
420 int64_t dts = packet->
getDts();
421 int64_t pts = packet->
getPts();
424 duration = thisBase->rescale(duration, packetBase.
value(),
425 IRational::ROUND_DOWN);
429 pts = thisBase->rescale(pts, packetBase.
value(), IRational::ROUND_DOWN);
433 dts = thisBase->rescale(dts, packetBase.
value(), IRational::ROUND_DOWN);
477 retval = av_index_search_timestamp(
489 if (mStream->index_entries
491 && index < mStream->nb_index_entries)
493 AVIndexEntry* entry = &(mStream->index_entries[index]);
513 return av_add_index_entry(mStream,
static const int64_t NO_PTS
A value that means no time stamp is set for a given object.
A file (or network data source) that contains one or more IStream objects of audio and video data.
An index entry for a IStream.
virtual int32_t getMinDistance()=0
Minimum number of index entries between this index entry and the last keyframe in the index,...
virtual int32_t getSize()=0
The size of bytes of the frame this index entry points to.
virtual int64_t getPosition()=0
The position in bytes of the frame corresponding to this index entry in the IContainer.
virtual int64_t getTimeStamp()=0
The actual time stamp, in units of IStream#getTimeBase(), of the frame this entry points to.
virtual int32_t getFlags()=0
Flags set for this entry.
static IIndexEntry * make(int64_t position, int64_t timeStamp, int32_t flags, int32_t size, int32_t minDistance)
Create a new IIndexEntry with the specified values.
Represents an encoded piece of data that can be placed in an IContainer for a given IStream of data.
virtual void setStreamIndex(int32_t streamIndex)=0
Set the stream index for this packet.
virtual int64_t getDuration()=0
Return the duration of this packet, in units of getTimeBase().
virtual void setDts(int64_t aDts)=0
Set a new Decompression Time Stamp (DTS) for this packet.
virtual int64_t getDts()=0
Get the Decompression Time Stamp (DTS) for this packet.
virtual void setPts(int64_t aPts)=0
Set a new Presentation Time Stamp (PTS) for this packet.
virtual void setDuration(int64_t duration)=0
Set the duration.
virtual int64_t getPts()=0
Get the Presentation Time Stamp (PTS) for this packet.
This class wraps represents a Rational number for the AVPKit.
static IRational * make()
Get a new rational that will be set to 0/0.
virtual int32_t getDenominator()=0
Get the denominator for this rational.
virtual int32_t getNumerator()=0
Get the numerator for this rational.
The work horse of the AVPKit: Takes IPacket data from an IContainer (representing an IStream) and an ...
ParseType
What types of parsing can we do on a call to IContainer#readNextPacket(IPacket).
static StreamCoder * make(Direction direction)
This method creates a StreamCoder that is not tied to any container or stream.
virtual bool isOpen()
Returns true if this IStreamCoder is currently open.
virtual int32_t acquire()
Internal Only.
virtual IContainer * getContainer()
Get the underlying container for this stream, or null if AVPKit doesn't know.
virtual int64_t getDuration()
Return the duration, in getTimeBase() units, of this stream, or Global#NO_PTS if unknown.
virtual IRational * getTimeBase()
The time base in which all timestamps (e.g.
virtual int getNumIndexEntries()
Get the number of index entries in this stream.
virtual int getId()
Return a container format specific id for this stream.
virtual IRational * getFrameRate()
Get the (sometimes estimated) frame rate of this container.
virtual IMetaData * getMetaData()
Get the IMetaData for this object, or null if none.
virtual IStream::ParseType getParseType()
Get how the decoding codec should parse data from this stream.
virtual int64_t getCurrentDts()
The current Decompression Time Stamp that will be used on this stream, in getTimeBase() units.
virtual IIndexEntry * findTimeStampEntryInIndex(int64_t wantedTimeStamp, int32_t flags)
Search for the given time stamp in the key-frame index for this IStream.
virtual int32_t stampOutputPacket(IPacket *packet)
Takes a packet destined for this stream, and stamps the stream index, and converts the time stamp to ...
virtual void setLanguage(const char *language)
Set the 4-character language setting for this stream.
virtual int getIndex()
Get the relative position this stream has in the hosting IContainer object.
virtual int64_t getStartTime()
Return the start time, in getTimeBase() units, when this stream started.
virtual int setBitstreamFilter(const char *name)
Set a bitstream filter on this stream.
virtual void setMetaData(IMetaData *metaData)
Set the IMetaData on this object, overriding any previous meta data.
virtual IStreamCoder * getStreamCoder()
Get the StreamCoder than can manipulate this stream.
virtual int32_t setStreamCoder(IStreamCoder *coder)
Sets the stream coder to use for this stream.
virtual const char * getLanguage()
Get the 4-character language setting for this stream.
virtual int32_t findTimeStampPositionInIndex(int64_t wantedTimeStamp, int32_t flags)
Search for the given time stamp in the key-frame index for this IStream.
virtual int32_t addIndexEntry(IIndexEntry *entry)
Adds an index entry into the stream's sorted index list.
void setId(int32_t id)
Set the format-specific stream id.
virtual IRational * getSampleAspectRatio()
Added in 1.17.
virtual int64_t getNumFrames()
Returns the number of encoded frames if known.
virtual void setParseType(ParseType type)
Set the parse type the decoding codec should use.
virtual int32_t release()
Internal Only.
virtual IIndexEntry * getIndexEntry(int32_t position)
Get the IIndexEntry at the given position in this IStream object's index.
virtual void setSampleAspectRatio(IRational *newRatio)
Sets the sample aspect ratio.
virtual int32_t acquire()
Internal Only.
virtual int32_t release()
Internal Only.
virtual int32_t getCurrentRefCount()
Return the current reference count on this object.
This class is only useful from C++.
T * value()
Return the managed pointer without calling RefCounted::acquire() on it.
WARNING: Do not use logging in this class, and do not set any static file variables to values other t...