| 1 |
/* |
| 2 |
* Copyright (C) 2013-2021 Jean-Luc Barrière |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 5 |
* See LICENSE.md for more information. |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef ELEMENTARYSTREAM_H |
| 9 |
#define ELEMENTARYSTREAM_H |
| 10 |
|
| 11 |
#include <cstdint> |
| 12 |
#include <cstddef> // for size_t |
| 13 |
|
| 14 |
#define ES_INIT_BUFFER_SIZE 64000 |
| 15 |
#define ES_MAX_BUFFER_SIZE 1048576 |
| 16 |
#define PTS_MASK 0x1ffffffffLL |
| 17 |
#define PTS_UNSET 0x1ffffffffLL |
| 18 |
#define PTS_TIME_BASE 90000LL |
| 19 |
#define RESCALE_TIME_BASE 1000000LL |
| 20 |
|
| 21 |
namespace TSDemux |
| 22 |
{ |
| 23 |
enum STREAM_TYPE |
| 24 |
{ |
| 25 |
STREAM_TYPE_UNKNOWN = 0, |
| 26 |
STREAM_TYPE_VIDEO_MPEG1, |
| 27 |
STREAM_TYPE_VIDEO_MPEG2, |
| 28 |
STREAM_TYPE_AUDIO_MPEG1, |
| 29 |
STREAM_TYPE_AUDIO_MPEG2, |
| 30 |
STREAM_TYPE_AUDIO_AAC, |
| 31 |
STREAM_TYPE_AUDIO_AAC_ADTS, |
| 32 |
STREAM_TYPE_AUDIO_AAC_LATM, |
| 33 |
STREAM_TYPE_VIDEO_H264, |
| 34 |
STREAM_TYPE_VIDEO_HEVC, |
| 35 |
STREAM_TYPE_AUDIO_AC3, |
| 36 |
STREAM_TYPE_AUDIO_EAC3, |
| 37 |
STREAM_TYPE_DVB_TELETEXT, |
| 38 |
STREAM_TYPE_DVB_SUBTITLE, |
| 39 |
STREAM_TYPE_VIDEO_MPEG4, |
| 40 |
STREAM_TYPE_VIDEO_VC1, |
| 41 |
STREAM_TYPE_AUDIO_LPCM, |
| 42 |
STREAM_TYPE_AUDIO_DTS, |
| 43 |
STREAM_TYPE_PRIVATE_DATA |
| 44 |
}; |
| 45 |
|
| 46 |
struct STREAM_INFO |
| 47 |
{ |
| 48 |
char language[4]; |
| 49 |
int composition_id; |
| 50 |
int ancillary_id; |
| 51 |
int fps_scale; |
| 52 |
int fps_rate; |
| 53 |
int height; |
| 54 |
int width; |
| 55 |
float aspect; |
| 56 |
int channels; |
| 57 |
int sample_rate; |
| 58 |
int block_align; |
| 59 |
int bit_rate; |
| 60 |
int bits_per_sample; |
| 61 |
bool interlaced; |
| 62 |
}; |
| 63 |
|
| 64 |
struct STREAM_PKT |
| 65 |
{ |
| 66 |
uint16_t pid; |
| 67 |
size_t size; |
| 68 |
const unsigned char* data; |
| 69 |
uint64_t dts; |
| 70 |
uint64_t pts; |
| 71 |
uint64_t duration; |
| 72 |
bool streamChange; |
| 73 |
}; |
| 74 |
|
| 75 |
class ElementaryStream |
| 76 |
{ |
| 77 |
public: |
| 78 |
ElementaryStream(uint16_t pes_pid); |
| 79 |
virtual ~ElementaryStream(); |
| 80 |
virtual void Reset(); |
| 81 |
void ClearBuffer(); |
| 82 |
int Append(const unsigned char* buf, size_t len, bool new_pts = false); |
| 83 |
const char* GetStreamCodecName() const; |
| 84 |
static const char* GetStreamCodecName(STREAM_TYPE stream_type); |
| 85 |
|
| 86 |
uint16_t pid; |
| 87 |
STREAM_TYPE stream_type; |
| 88 |
uint64_t c_dts; ///< current MPEG stream DTS (decode time for video) |
| 89 |
uint64_t c_pts; ///< current MPEG stream PTS (presentation time for audio and video) |
| 90 |
uint64_t p_dts; ///< previous MPEG stream DTS (decode time for video) |
| 91 |
uint64_t p_pts; ///< previous MPEG stream PTS (presentation time for audio and video) |
| 92 |
|
| 93 |
bool has_stream_info; ///< true if stream info is completed else it requires parsing of iframe |
| 94 |
|
| 95 |
STREAM_INFO stream_info; |
| 96 |
|
| 97 |
bool GetStreamPacket(STREAM_PKT* pkt); |
| 98 |
virtual void Parse(STREAM_PKT* pkt); |
| 99 |
|
| 100 |
protected: |
| 101 |
void ResetStreamPacket(STREAM_PKT* pkt); |
| 102 |
uint64_t Rescale(uint64_t a, uint64_t b, uint64_t c); |
| 103 |
bool SetVideoInformation(int FpsScale, int FpsRate, int Height, int Width, float Aspect, bool Interlaced); |
| 104 |
bool SetAudioInformation(int Channels, int SampleRate, int BitRate, int BitsPerSample, int BlockAlign); |
| 105 |
|
| 106 |
size_t es_alloc_init; ///< Initial allocation of memory for buffer |
| 107 |
unsigned char* es_buf; ///< The Pointer to buffer |
| 108 |
size_t es_alloc; ///< Allocated size of memory for buffer |
| 109 |
size_t es_len; ///< Size of data in buffer |
| 110 |
size_t es_consumed; ///< Consumed payload. Will be erased on next append |
| 111 |
size_t es_pts_pointer; ///< Position in buffer where current PTS becomes applicable |
| 112 |
size_t es_parsed; ///< Parser: Last processed position in buffer |
| 113 |
bool es_found_frame; ///< Parser: Found frame |
| 114 |
bool es_frame_valid; |
| 115 |
}; |
| 116 |
} |
| 117 |
|
| 118 |
#endif /* ELEMENTARYSTREAM_H */ |