-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathPES.as
executable file
·103 lines (83 loc) · 3.59 KB
/
PES.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//-------------------------------------------------------------------------------
// Copyright (c) 2014-2013 NoZAP B.V.
// Copyright (c) 2013 Guillaume du Pontavice (https://github.com/mangui/HLSprovider)
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//
// Authors:
// Jeroen Arnoldus
// Guillaume du Pontavice
//-------------------------------------------------------------------------------
package org.mangui.HLS.muxing {
import org.mangui.HLS.muxing.*;
import org.mangui.HLS.utils.*;
import flash.utils.ByteArray;
/** Representation of a Packetized Elementary Stream. **/
public class PES {
/** Timescale of pts/dts is 90khz. **/
public static var TIMESCALE:Number = 90;
/** Is it AAC audio or AVC video. **/
public var audio:Boolean;
/** The PES data (including headers). **/
public var data:ByteArray;
/** Start of the payload. **/
public var payload:Number;
/** Timestamp from the PTS header. **/
public var pts:Number;
/** Timestamp from the DTS header. **/
public var dts:Number;
/** Save the first chunk of PES data. **/
public function PES(dat:ByteArray,aud:Boolean) {
data = dat;
audio = aud;
};
/** Append PES data from additional TS packets. **/
public function append(dat:ByteArray):void {
data.writeBytes(dat,0,0);
};
/** When all data is appended, parse the PES headers. **/
public function parse():void {
data.position = 0;
// Start code prefix and packet ID.
var prefix:Number = data.readUnsignedInt();
/*Audio streams (0x1C0-0x1DF)
Video streams (0x1E0-0x1EF)
0x1BD is special case, could be audio or video (ffmpeg\libavformat\mpeg.c)
*/
if((audio && (prefix > 0x1df || prefix < 0x1c0 && prefix !=0x1bd)) ||
(!audio && prefix != 0x1e0 && prefix != 0x1ea && prefix != 0x1bd)) {
throw new Error("PES start code not found or not AAC/AVC: " + prefix);
}
// Ignore packet length and marker bits.
data.position += 3;
// Check for PTS
var flags:uint = (data.readUnsignedByte() & 192) >> 6;
if(flags != 2 && flags != 3) {
throw new Error("No PTS/DTS in this PES packet");
}
// Check PES header length
var length:uint = data.readUnsignedByte();
// Grab the timestamp from PTS data (spread out over 5 bytes):
// XXXX---X -------- -------X -------- -------X
var _pts:Number = ((data.readUnsignedByte() & 0x0e) << 29) +
((data.readUnsignedShort() >> 1) << 15) +
(data.readUnsignedShort() >> 1);
length -= 5;
var _dts:Number = _pts;
if(flags == 3) {
// Grab the DTS (like PTS)
_dts = ((data.readUnsignedByte() & 0x0e) << 29) +
((data.readUnsignedShort() >> 1) << 15) +
(data.readUnsignedShort() >> 1);
length -= 5;
}
pts = Math.round(_pts / PES.TIMESCALE);
dts = Math.round(_dts / PES.TIMESCALE);
// Skip other header data and parse payload.
data.position += length;
payload = data.position;
};
}
}