AVPKit
StreamCoder.h
1 /*******************************************************************************
2  * Copyright (c) 2024, 2026, Olivier Ayache. All rights reserved.
3  *
4  * This file is part of AVPKit.
5  *
6  * AVPKit is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * AVPKit is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with AVPKit. If not, see <http://www.gnu.org/licenses/>.
18  *******************************************************************************/
19 
20 #ifndef STREAMCODER_H_
21 #define STREAMCODER_H_
22 
23 #include <com/avpkit/ferry/RefPointer.h>
24 #include <com/avpkit/core/IStreamCoder.h>
25 #include <com/avpkit/core/FfmpegIncludes.h>
26 #include <com/avpkit/core/Stream.h>
27 #include <com/avpkit/ferry/IBuffer.h>
28 #include <com/avpkit/core/Codec.h>
29 
30 namespace com { namespace avpkit { namespace core
31 {
32  class Packet;
33 
34  class StreamCoder : public IStreamCoder
35  {
36  VS_JNIUTILS_REFCOUNTED_OBJECT_PRIVATE_MAKE(StreamCoder)
37  public:
38  // IStreamCoder Interface implementation
39 
40  /*
41  * This section has all the Getters and Setters
42  */
43  virtual Direction getDirection() { return mDirection; }
44 
45  virtual IStream* getStream();
46 
47  virtual Codec* getCodec();
48  virtual ICodec::Type getCodecType();
49  virtual ICodec::ID getCodecID();
50 
51  virtual void setCodec(ICodec *);
52  virtual void setCodec(ICodec::ID);
53  virtual void setCodecID(ICodec::ID id) { setCodec(id); }
54 
55  virtual int32_t getBitRate();
56  virtual void setBitRate(int32_t rate);
57  virtual int32_t getBitRateTolerance();
58  virtual void setBitRateTolerance(int32_t tolerance);
59 
60  // These are Video codec getters and setters
61  virtual int32_t getHeight();
62  virtual void setHeight(int32_t);
63 
64  virtual int32_t getWidth();
65  virtual void setWidth(int32_t);
66 
67  // NOTE: Caller must release() return value when done.
68  virtual IRational* getTimeBase();
69  virtual void setTimeBase(IRational* newTimeBase);
70 
71  // These just forward to the stream we're coding
72  virtual IRational* getFrameRate();
73  virtual void setFrameRate(IRational *newFrameRate);
74 
75  virtual int32_t getNumPicturesInGroupOfPictures();
76  virtual void setNumPicturesInGroupOfPictures(int32_t gops);
77 
79  virtual void setPixelType(IPixelFormat::Type pixelFmt);
80 
81  // These are Audio codec getters and setters
82  virtual int32_t getSampleRate();
83  virtual void setSampleRate(int32_t sampleRate);
84 
85  virtual int32_t getChannels();
86  virtual void setChannels(int32_t channels);
87 
89  virtual void setSampleFormat(IAudioSamples::Format aFormat);
90 
91  virtual int32_t getGlobalQuality();
92  virtual void setGlobalQuality(int32_t newQuality);
93 
94  virtual int32_t getFlags();
95  virtual void setFlags(int32_t newFlags);
96  virtual bool getFlag(Flags flag);
97  virtual void setFlag(Flags flag, bool value);
98  virtual int32_t getAudioFrameSize();
99 
100  /*
101  * This section has the operational aspects of
102  * a StreamCoder
103  */
104  virtual int32_t open();
105  virtual int32_t close();
106  virtual int32_t decodeAudio(IAudioSamples * pOutSamples,
107  IPacket *packet, int32_t byteOffset);
108  virtual int32_t decodeVideo(IVideoPicture * pOutFrame,
109  IPacket *packet, int32_t byteOffset);
110  virtual int32_t encodeVideo(IPacket *pOutPacket,
111  IVideoPicture * pFrame, int32_t suggestedBufferSize);
112  virtual int32_t encodeAudio(IPacket *pOutPacket,
113  IAudioSamples* pSamples, int32_t sampleToStartFrom);
114 
115  virtual int64_t getNextPredictedPts();
116 
117  virtual int32_t getCodecTag();
118  virtual void setCodecTag(int32_t);
119 
120  virtual int32_t getNumProperties();
121  virtual IProperty* getPropertyMetaData(int32_t propertyNo);
122  virtual IProperty* getPropertyMetaData(const char *name);
123 
124  virtual int32_t setProperty(const char* name, const char* value);
125  virtual int32_t setProperty(const char* name, double value);
126  virtual int32_t setProperty(const char* name, int64_t value);
127  virtual int32_t setProperty(const char* name, bool value);
128  virtual int32_t setProperty(const char* name, IRational *value);
129 
130  virtual char * getPropertyAsString(const char* name);
131  virtual double getPropertyAsDouble(const char* name);
132  virtual int64_t getPropertyAsLong(const char* name);
133  virtual IRational *getPropertyAsRational(const char* name);
134  virtual bool getPropertyAsBoolean(const char* name);
135 
136  virtual bool isOpen();
137 
138  virtual int32_t getDefaultAudioFrameSize();
139  virtual void setDefaultAudioFrameSize(int32_t);
140  // Not for calling from Java
141  void setCodec(int32_t);
142 
147  static StreamCoder* make(Direction direction);
148 
153  attribute_deprecated
154  static StreamCoder* make(Direction direction,
155  AVCodecContext *context, const AVCodec*, Stream* stream);
156  static StreamCoder* make(Direction direction,
157  AVCodecParameters *codecpar, const AVCodec*, Stream* stream);
158  static StreamCoder* make(Direction direction, IStreamCoder* copyCoder);
159  static StreamCoder* make(Direction direction, Codec* codec);
160  static StreamCoder* make(Direction direction, ICodec::ID id);
161 
162  int32_t setStream(Stream*, bool assumeOnlyStream);
163  int32_t streamClosed(Stream*);
164 
165  virtual int64_t getNumDroppedFrames();
166  virtual void setAutomaticallyStampPacketsForStream(bool value);
168 
169  // RefCounted interface
170  virtual int32_t acquire();
171  virtual int32_t release();
172 
173  virtual int32_t setExtraData(com::avpkit::ferry::IBuffer* src, int32_t offset, int32_t length, bool allocNew);
174  virtual int32_t getExtraData(com::avpkit::ferry::IBuffer *dest, int32_t offset, int32_t maxBytesToCopy);
175  virtual int32_t getExtraDataSize();
176 
178  virtual int32_t setStandardsCompliance(CodecStandardsCompliance compliance);
179  virtual int32_t setHardwareDecoding(IPixelFormat::Type type, void* surface=NULL);
180  virtual void* getHardwareSurface();
181  virtual int32_t open(IMetaData *options, IMetaData* unsetOptions);
182  virtual int32_t setProperty(IMetaData* valuesToSet, IMetaData* valuesNotFound);
183 
184  protected:
185  StreamCoder();
186  virtual ~StreamCoder();
187  private:
188  Direction mDirection;
189  AVCodecContext* mCodecContext;
190  SwrContext* swrContext;
191  Stream* mStream; // Must not refcount this.
193  bool mOpened;
194 
195  // Variables used for patching up PTS values
197  int64_t mLastPtsEncoded;
198  int64_t mFakeNextPts;
199  int64_t mFakeCurrPts;
200  int64_t mSamplesForEncoding;
201  int64_t mSamplesCoded;
202  int64_t mLastExternallySetTimeStamp;
203 
205  int32_t mBytesInFrameBuffer;
206  int64_t mPtsOfFrameBuffer;
207  int64_t mStartingTimestampOfBytesInFrameBuffer;
208  int32_t mDefaultAudioFrameSize;
209  int64_t mNumDroppedFrames;
210  bool mAutomaticallyStampPacketsForStream;
211  int64_t mPtsBuffer[MAX_REORDER_DELAY+1];
212 
213  void reset();
214  void setPacketParameters(Packet *packet, int32_t size,
215  int64_t dts,
216  IRational * timebase,
217  bool keyframe,
218  int64_t duration,
219  bool complete);
224  static int32_t readyAVContexts(
225  Direction aDirection,
226  StreamCoder *aCoder,
227  Stream* aStream,
228  Codec *aCodec,
229  AVCodecContext *avContext,
230  const AVCodec *avCodec);
231  static void resetOptions(AVCodecContext*);
232  };
233 
234 }}}
235 
236 #endif /*STREAMCODER_H_*/
A set of raw (decoded) samples, plus a timestamp for when to play those samples relative to other ite...
Definition: IAudioSamples.h:38
Format
The format we use to represent audio.
Definition: IAudioSamples.h:46
A "key" to an IStreamCoder that tells it how to encode or decode data.
Definition: ICodec.h:53
ID
These are the codecs this library currently supports.
Definition: ICodec.h:61
Type
The different types of Codecs that can exist in the system.
Definition: ICodec.h:582
Get MetaData about a IContainer or IStream.
Definition: IMetaData.h:51
Represents an encoded piece of data that can be placed in an IContainer for a given IStream of data.
Definition: IPacket.h:50
Represents settable properties that effect how AVPKit objects operate.
Definition: IProperty.h:37
This class wraps represents a Rational number for the AVPKit.
Definition: IRational.h:43
The work horse of the AVPKit: Takes IPacket data from an IContainer (representing an IStream) and an ...
Definition: IStreamCoder.h:45
CodecStandardsCompliance
An enumeration of how strictly Codecs may follow the spec.
Definition: IStreamCoder.h:959
Flags
AVPKIT Flags that can be passed to the setFlag(Flags, bool) method.
Definition: IStreamCoder.h:60
Direction
The Direction in which this StreamCoder will work.
Definition: IStreamCoder.h:52
Represents a stream of similar data (eg video) in a IContainer.
Definition: IStream.h:50
Represents one raw (undecoded) picture in a video stream, plus a timestamp for when to display that v...
Definition: IVideoPicture.h:40
virtual void setTimeBase(IRational *newTimeBase)
Set the time base we'll use to ENCODE with.
virtual int32_t setExtraData(com::avpkit::ferry::IBuffer *src, int32_t offset, int32_t length, bool allocNew)
Copies data from the given buffer into the extra-data area maintained by encoders.
virtual IProperty * getPropertyMetaData(int32_t propertyNo)
Returns the name of the numbered property.
virtual double getPropertyAsDouble(const char *name)
Gets the value of this property, and returns as a double;.
virtual IStream * getStream()
The associated Stream we're working on.
virtual int64_t getPropertyAsLong(const char *name)
Gets the value of this property, and returns as an long;.
virtual bool getPropertyAsBoolean(const char *name)
Gets the value of this property, and returns as a boolean.
virtual void setAutomaticallyStampPacketsForStream(bool value)
Should the IStreamCoder automatically adjust time stamps on output packets for the IStream it is ulti...
virtual int64_t getNextPredictedPts()
For this stream, get the next Pts that we expect to decode.
virtual void setFlag(Flags flag, bool value)
Set the flag.
virtual void setCodec(ICodec *)
Set the Codec to the passed in Codec, discarding the old Codec if set.
virtual void setSampleFormat(IAudioSamples::Format aFormat)
Set the sample format when ENCODING.
virtual void setCodecTag(int32_t)
Set the 4-byte FOURCC tag for this coder.
virtual void setNumPicturesInGroupOfPictures(int32_t gops)
Set the GOPS on this stream.
virtual bool getAutomaticallyStampPacketsForStream()
Does this IStreamCoder automatically adjust time stamps on output packets for the IStream it is ultim...
virtual void setPixelType(IPixelFormat::Type pixelFmt)
Set the pixel format to ENCODE with.
virtual void setSampleRate(int32_t sampleRate)
Set the sample rate to use when ENCODING.
virtual void setCodecID(ICodec::ID id)
Look up a Codec based on the passed in ID, and then set it.
Definition: StreamCoder.h:53
virtual bool getFlag(Flags flag)
Get the setting for the specified flag.
virtual int32_t release()
Internal Only.
virtual IRational * getTimeBase()
Get the time base this stream will ENCODE in, or the time base we detect while DECODING.
virtual CodecStandardsCompliance getStandardsCompliance()
Gets the current level of standards compliance.
virtual int32_t getAudioFrameSize()
For this stream, get the number of audio samples that are represented in a packet of information.
virtual void setBitRate(int32_t rate)
When ENCODING, sets the bit rate to use.
virtual int32_t getCodecTag()
Returns the 4-byte FOURCC tag (Least Significant Byte first).
virtual void setChannels(int32_t channels)
Set the number of channels to use when ENCODING.
virtual IRational * getFrameRate()
Get the frame-rate the attached stream claims to be using when DECODING, or the frame-rate we'll clai...
virtual int32_t decodeVideo(IVideoPicture *pOutFrame, IPacket *packet, int32_t byteOffset)
Decode this packet into pOutFrame.
virtual void setDefaultAudioFrameSize(int32_t)
Set the default audio frame size.
virtual int32_t getNumPicturesInGroupOfPictures()
The the number of pictures in this Group of Pictures (GOP).
virtual int32_t getChannels()
Get the number of channels in this audio stream.
virtual int32_t setStandardsCompliance(CodecStandardsCompliance compliance)
Set the level of standards compliance.
virtual Direction getDirection()
Get the direction.
Definition: StreamCoder.h:43
virtual IRational * getPropertyAsRational(const char *name)
Gets the value of this property, and returns as an IRational;.
virtual void setFlags(int32_t newFlags)
Set the FFMPEG flags to use with this codec.
virtual Codec * getCodec()
The Codec this StreamCoder will use.
virtual int32_t getExtraData(com::avpkit::ferry::IBuffer *dest, int32_t offset, int32_t maxBytesToCopy)
Copies the current content of the extra-data buffer maintained by this codec (e.g.
virtual int32_t decodeAudio(IAudioSamples *pOutSamples, IPacket *packet, int32_t byteOffset)
Decode this packet into pOutSamples.
virtual int32_t getBitRate()
The bit rate.
virtual IAudioSamples::Format getSampleFormat()
Get the audio sample format.
virtual char * getPropertyAsString(const char *name)
Gets a property on this Object.
virtual void setWidth(int32_t)
Set the width, in pixels.
virtual int32_t close()
Close a Codec that was opened on this StreamCoder.
virtual int32_t encodeVideo(IPacket *pOutPacket, IVideoPicture *pFrame, int32_t suggestedBufferSize)
Encode the given frame using this StreamCoder.
virtual ICodec::ID getCodecID()
A short hand for getCodec().getID().
virtual int32_t getExtraDataSize()
Gets the current number of bytes of data maintained in the coder extra-data area.
virtual int32_t encodeAudio(IPacket *pOutPacket, IAudioSamples *pSamples, int32_t sampleToStartFrom)
Encode the given samples using this StreamCoder.
virtual int32_t getFlags()
Get the flags associated with this codec.
virtual int32_t getHeight()
The height, in pixels.
virtual int32_t getSampleRate()
Get the sample rate we use for this stream.
virtual int32_t getNumProperties()
Returns the total number of settable properties on this object.
virtual void setHeight(int32_t)
Set the height, in pixels.
virtual void setGlobalQuality(int32_t newQuality)
Set the Global Quality to a new value.
virtual void setBitRateTolerance(int32_t tolerance)
When ENCODING set the bit rate tolerance.
virtual int32_t acquire()
Internal Only.
virtual int32_t setProperty(const char *name, const char *value)
Sets a property on this Object.
virtual int32_t getDefaultAudioFrameSize()
Get the default audio frame size (in samples).
static StreamCoder * make(Direction direction)
This method creates a StreamCoder that is not tied to any container or stream.
virtual int32_t getGlobalQuality()
Get the Global Quality setting this codec uses for video if a VideoPicture doesn't have a quality set...
virtual void setFrameRate(IRational *newFrameRate)
Set the frame rate we'll set in the headers of this stream while ENCODING.
virtual IPixelFormat::Type getPixelType()
For Video streams, get the Pixel Format in use by the stream.
virtual int32_t getBitRateTolerance()
The bit rate tolerance.
virtual bool isOpen()
Returns true if this IStreamCoder is currently open.
virtual int32_t getWidth()
The width, in pixels.
virtual ICodec::Type getCodecType()
A short hand for getCodec().getType().
virtual int32_t setHardwareDecoding(IPixelFormat::Type type, void *surface=NULL)
Try to change current decoder to an HW decoder compatible with pixel format.
virtual int64_t getNumDroppedFrames()
Get the number of frames this StreamCoder had to drop to encode at the specified getTimeBase().
Allows Java code to get data from a native buffers, and optionally modify native memory directly.
Definition: IBuffer.h:54
This class is only useful from C++.
Definition: RefPointer.h:47
WARNING: Do not use logging in this class, and do not set any static file variables to values other t...