AVPKit
IStreamCoder.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 ISTREAMCODER_H_
21 #define ISTREAMCODER_H_
22 
23 #include <com/avpkit/ferry/RefCounted.h>
24 #include <com/avpkit/core/AVPKit.h>
25 #include <com/avpkit/core/ICodec.h>
26 #include <com/avpkit/core/IRational.h>
27 #include <com/avpkit/core/IPixelFormat.h>
28 #include <com/avpkit/core/IAudioSamples.h>
29 #include <com/avpkit/core/IVideoPicture.h>
30 #include <com/avpkit/core/IPacket.h>
31 #include <com/avpkit/core/IProperty.h>
32 #include <com/avpkit/core/IMetaData.h>
33 
34 namespace com { namespace avpkit { namespace core
35 {
36 
37  class IStream;
38 
44  class VS_API_AVPKIT IStreamCoder : public com::avpkit::ferry::RefCounted
45  {
46  public:
47 
51  typedef enum Direction
52  {
53  ENCODING,
54  DECODING
55  } Direction;
56 
60  typedef enum Flags {
61  FLAG_QSCALE=0x0002,
62  FLAG_4MV =0x0004,
63  FLAG_QPEL =0x0010,
64  FLAG_GMC =0x0020,
65  FLAG_MV0 =0x0040,
71  FLAG_INPUT_PRESERVED=0x0100,
72  FLAG_PASS1 =0x0200,
73  FLAG_PASS2 =0x0400,
74  FLAG_GRAY =0x2000,
75  FLAG_EMU_EDGE =0x4000,
76  FLAG_PSNR =0x8000,
77  FLAG_TRUNCATED =0x00010000,
79  FLAG_NORMALIZE_AQP =0x00020000,
80  FLAG_INTERLACED_DCT=0x00040000,
81  FLAG_LOW_DELAY =0x00080000,
82  FLAG_GLOBAL_HEADER =0x00400000,
83  FLAG_BITEXACT =0x00800000,
84  /* Fx : Flag for h263+ extra options */
85  FLAG_AC_PRED =0x01000000,
86  FLAG_CBP_RD =0x04000000,
87  FLAG_QP_RD =0x08000000,
88  FLAG_LOOP_FILTER =0x00000800,
89  FLAG_INTERLACED_ME =0x20000000,
90  FLAG_CLOSED_GOP =0x80000000,
91  FLAG2_FAST =0x00000001,
92  FLAG2_STRICT_GOP =0x00000002,
93  FLAG2_NO_OUTPUT =0x00000004,
94  FLAG2_LOCAL_HEADER =0x00000008,
95  FLAG2_SKIP_RD =0x00004000,
96  FLAG2_CHUNKS =0x00008000,
97  FLAG2_SHOW_ALL =0x00400000,
98  } Flags;
99 
103  virtual Direction getDirection()=0;
104 
110  virtual IStream* getStream()=0;
111 
117  virtual ICodec* getCodec()=0;
118 
136 
154  virtual ICodec::ID getCodecID()=0;
155 
167  virtual void setCodec(ICodec *codec)=0;
168 
183  virtual void setCodec(ICodec::ID id)=0;
184 
190  virtual int32_t getBitRate()=0;
191 
198  virtual void setBitRate(int32_t rate)=0;
199 
205  virtual int32_t getBitRateTolerance()=0;
206 
212  virtual void setBitRateTolerance(int32_t tolerance)=0;
213 
220  virtual int32_t getHeight()=0;
221 
229  virtual void setHeight(int32_t height)=0;
230 
237  virtual int32_t getWidth()=0;
238 
246  virtual void setWidth(int32_t width)=0;
247 
256  virtual IRational* getTimeBase()=0;
257 
268  virtual void setTimeBase(IRational* newTimeBase)=0;
269 
276  virtual IRational* getFrameRate()=0;
277 
291  virtual void setFrameRate(IRational* newFrameRate)=0;
292 
300  virtual int32_t getNumPicturesInGroupOfPictures()=0;
301 
309  virtual void setNumPicturesInGroupOfPictures(int32_t gops)=0;
310 
317 
324  virtual void setPixelType(IPixelFormat::Type pixelFmt)=0;
325 
331  virtual int32_t getSampleRate()=0;
332 
339  virtual void setSampleRate(int32_t sampleRate)=0;
340 
347 
354  virtual void setSampleFormat(IAudioSamples::Format aFormat)=0;
355 
361  virtual int32_t getChannels()=0;
362 
369  virtual void setChannels(int32_t channels)=0;
370 
371 
378  virtual int32_t getAudioFrameSize()=0;
379 
386  virtual int32_t getGlobalQuality()=0;
387 
394  virtual void setGlobalQuality(int32_t newQuality)=0;
395 
401  virtual int32_t getFlags()=0;
402 
411  virtual void setFlags(int32_t newFlags) = 0;
412 
420  virtual bool getFlag(Flags flag) = 0;
421 
429  virtual void setFlag(Flags flag, bool value) = 0;
430 
431 
442  virtual int64_t getNextPredictedPts()=0;
443 
459  virtual int32_t open()=0;
465  virtual int32_t close()=0;
466 
490  virtual int32_t decodeAudio(IAudioSamples * pOutSamples,
491  IPacket *packet, int32_t byteOffset)=0;
492 
508  virtual int32_t decodeVideo(IVideoPicture * pOutFrame,
509  IPacket *packet, int32_t byteOffset)=0;
510 
532  virtual int32_t encodeVideo(IPacket * pOutPacket,
533  IVideoPicture * pFrame, int32_t suggestedBufferSize)=0;
534 
560  virtual int32_t encodeAudio(IPacket * pOutPacket,
561  IAudioSamples* pSamples, int32_t sampleToStartFrom)=0;
562 
578  static IStreamCoder* make(Direction direction);
579 
580  protected:
581  IStreamCoder();
582  virtual ~IStreamCoder();
583  public:
584  /*
585  * Added for 1.17
586  */
587 
598  virtual int32_t getCodecTag()=0;
603  virtual void setCodecTag(int32_t fourcc)=0;
604 
605  /*
606  * Added for 1.19
607  */
608 
614  virtual int32_t getNumProperties()=0;
615 
623  virtual IProperty *getPropertyMetaData(int32_t propertyNo)=0;
624 
632  virtual IProperty *getPropertyMetaData(const char *name)=0;
633 
644  virtual int32_t setProperty(const char *name, const char* value)=0;
645 
646 
656  virtual int32_t setProperty(const char* name, double value)=0;
657 
667  virtual int32_t setProperty(const char* name, int64_t value)=0;
668 
678  virtual int32_t setProperty(const char* name, bool value)=0;
679 
689  virtual int32_t setProperty(const char* name, IRational *value)=0;
690 
691 #ifdef SWIG
692  %newobject getPropertyAsString(const char*);
693  %typemap(newfree) char * "free($1);";
694 #endif
706  virtual char * getPropertyAsString(const char* name)=0;
707 
715  virtual double getPropertyAsDouble(const char* name)=0;
716 
724  virtual int64_t getPropertyAsLong(const char* name)=0;
725 
733  virtual IRational *getPropertyAsRational(const char* name)=0;
734 
742  virtual bool getPropertyAsBoolean(const char* name)=0;
743 
749  virtual bool isOpen()=0;
750 
751 
752  // Added for 1.21
753 
766  virtual int32_t getDefaultAudioFrameSize()=0;
767 
778  virtual void setDefaultAudioFrameSize(int32_t aNewSize)=0;
779 
780  /*
781  * Added for 1.22
782  */
783 
805  static IStreamCoder* make(Direction direction, IStreamCoder* copyCoder);
806 
807  /*
808  * Added for 3.1
809  */
810 
835  virtual int64_t getNumDroppedFrames()=0;
836 
837  /*
838  * Added for 3.2
839  */
840 
857  virtual void setAutomaticallyStampPacketsForStream(bool value)=0;
858 
868 
887  virtual void setCodecID(ICodec::ID id)=0;
888 
889  /*
890  * Added for 3.3
891  */
892 
926  int32_t offset,
927  int32_t length, bool allocNew)=0;
928 
941  int32_t offset,
942  int32_t maxBytesToCopy)=0;
943 
950  virtual int32_t getExtraDataSize()=0;
951 
959  typedef enum {
963  COMPLIANCE_VERY_STRICT=2,
967  COMPLIANCE_STRICT=1,
972  COMPLIANCE_NORMAL=0,
976  COMPLIANCE_UNOFFICIAL=-1,
982  COMPLIANCE_EXPERIMENTAL=-2,
983  } CodecStandardsCompliance;
984 
992 
1002  virtual int32_t setStandardsCompliance(CodecStandardsCompliance compliance)=0;
1003 
1004 #ifndef SWIG
1012  virtual int32_t setHardwareDecoding(IPixelFormat::Type type, void* surface=NULL)=0;
1013 
1014  virtual void* getHardwareSurface()=0;
1015 
1016 #endif
1017 
1032  virtual int32_t open(IMetaData *options, IMetaData* unsetOptions)=0;
1033 
1051  static IStreamCoder* make(Direction direction, ICodec* codec);
1052 
1053 
1071  static IStreamCoder* make(Direction direction, ICodec::ID id);
1072 
1076  virtual int32_t setProperty(IMetaData* valuesToSet, IMetaData* valuesNotFound)=0;
1077  };
1078 
1079 }}}
1080 
1081 #endif /*ISTREAMCODER_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
virtual int32_t setProperty(const char *name, bool value)=0
Looks up the property 'name' and sets the value of the property to 'value'.
virtual int64_t getNumDroppedFrames()=0
Get the number of frames this StreamCoder had to drop to encode at the specified getTimeBase().
virtual int32_t setStandardsCompliance(CodecStandardsCompliance compliance)=0
Set the level of standards compliance.
virtual int32_t setHardwareDecoding(IPixelFormat::Type type, void *surface=NULL)=0
Try to change current decoder to an HW decoder compatible with pixel format.
virtual void setBitRateTolerance(int32_t tolerance)=0
When ENCODING set the bit rate tolerance.
virtual int32_t getBitRateTolerance()=0
The bit rate tolerance.
virtual void setChannels(int32_t channels)=0
Set the number of channels to use when ENCODING.
virtual int32_t getBitRate()=0
The bit rate.
virtual void setPixelType(IPixelFormat::Type pixelFmt)=0
Set the pixel format to ENCODE with.
virtual int32_t getSampleRate()=0
Get the sample rate we use for this stream.
virtual int32_t close()=0
Close a Codec that was opened on this StreamCoder.
virtual void setSampleRate(int32_t sampleRate)=0
Set the sample rate to use when ENCODING.
virtual int32_t getExtraDataSize()=0
Gets the current number of bytes of data maintained in the coder extra-data area.
virtual Direction getDirection()=0
Get the direction.
CodecStandardsCompliance
An enumeration of how strictly Codecs may follow the spec.
Definition: IStreamCoder.h:959
virtual void setHeight(int32_t height)=0
Set the height, in pixels.
virtual void setTimeBase(IRational *newTimeBase)=0
Set the time base we'll use to ENCODE with.
virtual int32_t getWidth()=0
The width, in pixels.
virtual int32_t getGlobalQuality()=0
Get the Global Quality setting this codec uses for video if a VideoPicture doesn't have a quality set...
virtual int32_t getDefaultAudioFrameSize()=0
Get the default audio frame size (in samples).
virtual int32_t getFlags()=0
Get the flags associated with this codec.
virtual void setAutomaticallyStampPacketsForStream(bool value)=0
Should the IStreamCoder automatically adjust time stamps on output packets for the IStream it is ulti...
virtual int32_t setProperty(const char *name, IRational *value)=0
Looks up the property 'name' and sets the value of the property to 'value'.
virtual void setCodec(ICodec *codec)=0
Set the Codec to the passed in Codec, discarding the old Codec if set.
virtual void setGlobalQuality(int32_t newQuality)=0
Set the Global Quality to a new value.
Flags
AVPKIT Flags that can be passed to the setFlag(Flags, bool) method.
Definition: IStreamCoder.h:60
virtual IProperty * getPropertyMetaData(int32_t propertyNo)=0
Returns the name of the numbered property.
virtual void setFlags(int32_t newFlags)=0
Set the FFMPEG flags to use with this codec.
virtual IRational * getTimeBase()=0
Get the time base this stream will ENCODE in, or the time base we detect while DECODING.
virtual int32_t open(IMetaData *options, IMetaData *unsetOptions)=0
Open the codec with the given options.
virtual void setCodecTag(int32_t fourcc)=0
Set the 4-byte FOURCC tag for this coder.
virtual void setSampleFormat(IAudioSamples::Format aFormat)=0
Set the sample format when ENCODING.
virtual void setFrameRate(IRational *newFrameRate)=0
Set the frame rate we'll set in the headers of this stream while ENCODING.
virtual void setWidth(int32_t width)=0
Set the width, in pixels.
virtual CodecStandardsCompliance getStandardsCompliance()=0
Gets the current level of standards compliance.
virtual int32_t setProperty(const char *name, int64_t value)=0
Looks up the property 'name' and sets the value of the property to 'value'.
virtual int32_t setProperty(IMetaData *valuesToSet, IMetaData *valuesNotFound)=0
virtual int32_t getExtraData(com::avpkit::ferry::IBuffer *dest, int32_t offset, int32_t maxBytesToCopy)=0
Copies the current content of the extra-data buffer maintained by this codec (e.g.
virtual ICodec * getCodec()=0
The Codec this StreamCoder will use.
virtual double getPropertyAsDouble(const char *name)=0
Gets the value of this property, and returns as a double;.
virtual void setCodec(ICodec::ID id)=0
Look up a Codec based on the passed in ID, and then set it.
virtual int32_t setExtraData(com::avpkit::ferry::IBuffer *src, int32_t offset, int32_t length, bool allocNew)=0
Copies data from the given buffer into the extra-data area maintained by encoders.
virtual void setDefaultAudioFrameSize(int32_t aNewSize)=0
Set the default audio frame size.
virtual int32_t getNumPicturesInGroupOfPictures()=0
The the number of pictures in this Group of Pictures (GOP).
Direction
The Direction in which this StreamCoder will work.
Definition: IStreamCoder.h:52
virtual IPixelFormat::Type getPixelType()=0
For Video streams, get the Pixel Format in use by the stream.
virtual int32_t setProperty(const char *name, const char *value)=0
Sets a property on this Object.
virtual char * getPropertyAsString(const char *name)=0
Gets a property on this Object.
virtual void setNumPicturesInGroupOfPictures(int32_t gops)=0
Set the GOPS on this stream.
virtual int32_t getAudioFrameSize()=0
For this stream, get the number of audio samples that are represented in a packet of information.
virtual int32_t encodeAudio(IPacket *pOutPacket, IAudioSamples *pSamples, int32_t sampleToStartFrom)=0
Encode the given samples using this StreamCoder.
virtual IRational * getFrameRate()=0
Get the frame-rate the attached stream claims to be using when DECODING, or the frame-rate we'll clai...
virtual int64_t getNextPredictedPts()=0
For this stream, get the next Pts that we expect to decode.
virtual int32_t getNumProperties()=0
Returns the total number of settable properties on this object.
virtual IRational * getPropertyAsRational(const char *name)=0
Gets the value of this property, and returns as an IRational;.
virtual ICodec::ID getCodecID()=0
A short hand for getCodec().getID().
virtual void setCodecID(ICodec::ID id)=0
Look up a Codec based on the passed in ID, and then set it.
virtual IStream * getStream()=0
The associated Stream we're working on.
virtual int32_t encodeVideo(IPacket *pOutPacket, IVideoPicture *pFrame, int32_t suggestedBufferSize)=0
Encode the given frame using this StreamCoder.
virtual IProperty * getPropertyMetaData(const char *name)=0
Returns the name of the numbered property.
virtual void setBitRate(int32_t rate)=0
When ENCODING, sets the bit rate to use.
virtual bool getPropertyAsBoolean(const char *name)=0
Gets the value of this property, and returns as a boolean.
virtual bool getAutomaticallyStampPacketsForStream()=0
Does this IStreamCoder automatically adjust time stamps on output packets for the IStream it is ultim...
virtual int32_t getCodecTag()=0
Returns the 4-byte FOURCC tag (Least Significant Byte first).
virtual int32_t getChannels()=0
Get the number of channels in this audio stream.
virtual bool getFlag(Flags flag)=0
Get the setting for the specified flag.
virtual bool isOpen()=0
Returns true if this IStreamCoder is currently open.
virtual int32_t decodeAudio(IAudioSamples *pOutSamples, IPacket *packet, int32_t byteOffset)=0
Decode this packet into pOutSamples.
virtual int64_t getPropertyAsLong(const char *name)=0
Gets the value of this property, and returns as an long;.
virtual int32_t decodeVideo(IVideoPicture *pOutFrame, IPacket *packet, int32_t byteOffset)=0
Decode this packet into pOutFrame.
virtual int32_t getHeight()=0
The height, in pixels.
virtual void setFlag(Flags flag, bool value)=0
Set the flag.
virtual int32_t setProperty(const char *name, double value)=0
Looks up the property 'name' and sets the value of the property to 'value'.
virtual IAudioSamples::Format getSampleFormat()=0
Get the audio sample format.
virtual ICodec::Type getCodecType()=0
A short hand for getCodec().getType().
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
Allows Java code to get data from a native buffers, and optionally modify native memory directly.
Definition: IBuffer.h:54
Parent of all Ferry objects – it mains reference counts in native code.
Definition: RefCounted.h:85
WARNING: Do not use logging in this class, and do not set any static file variables to values other t...