summaryrefslogtreecommitdiff
path: root/web/kbn_assets/h265webjs-dist/raw-parser.js
blob: edc91a3f0c2a6cc1251f2b89e86a3058eb9eb493 (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/********************************************************* 
 * LICENSE: LICENSE-Free_CN.MD
 * 
 * Author: Numberwolf - ChangYanlong
 * QQ: 531365872
 * QQ Group:925466059
 * Wechat: numberwolf11
 * Discord: numberwolf#8694
 * E-Mail: porschegt23@foxmail.com
 * Github: https://github.com/numberwolf/h265web.js
 * 
 * 作者: 小老虎(Numberwolf)(常炎隆)
 * QQ: 531365872
 * QQ群: 531365872
 * 微信: numberwolf11
 * Discord: numberwolf#8694
 * 邮箱: porschegt23@foxmail.com
 * 博客: https://www.jianshu.com/u/9c09c1e00fd1
 * Github: https://github.com/numberwolf/h265web.js
 * 
 **********************************************************/
/**
 * codecImp Obj
 * Video Raw 265 264 Parser
 */
const AfterGetNalThenMvLen  = 3;

export default class RawParserModule {
    constructor() {
        this.frameList = [];
        this.stream = null;
    }

    /*
     *****************************************************
     *                                                   *
     *                                                   *
     *                     HEVC Frames                   *
     *                                                   *
     *                                                   *
     *****************************************************
     */
    pushFrameRet(streamPushInput) {
        if (!streamPushInput || streamPushInput == undefined || streamPushInput == null) {
            return false;
        }

        if (!this.frameList || this.frameList == undefined || this.frameList == null) {
            this.frameList = [];
            this.frameList.push(streamPushInput);
            
        } else {
            this.frameList.push(streamPushInput);
        }

        return true;
    }

    nextFrame() {
        if (!this.frameList && this.frameList == undefined || this.frameList == null && this.frameList.length < 1) {
            return null;
        }
        return this.frameList.shift();
    }

    clearFrameRet() {
        this.frameList = null;
    }

    /*
     *****************************************************
     *                                                   *
     *                                                   *
     *                     HEVC stream                   *
     *                                                   *
     *                                                   *
     *****************************************************
     */
    setStreamRet(streamBufInput) {
        this.stream = streamBufInput;
    }

    getStreamRet() {
        return this.stream;
    }

    /**
     * push stream nalu, for live, not vod
     * @param Uint8Array
     * @return bool
     */
    appendStreamRet(input) {
        if (!input || input === undefined || input == null) {
            return false;
        }

        if (!this.stream || this.stream === undefined || this.stream == null) {
            this.stream = input;
            return true;
        }

        let lenOld  = this.stream.length;
        let lenPush = input.length;

        let mergeStream = new Uint8Array(lenOld + lenPush);
        mergeStream.set(this.stream, 0);
        mergeStream.set(input, lenOld);

        this.stream = mergeStream;

        // let retList = this.nextNaluList(9000);
        // if (retList !== false && retList.length > 0) {
        //     this.frameList.push(...retList);
        // }

        for (let i = 0; i < 9999; i++) {
            let nalBuf = this.nextNalu();
            if (nalBuf !== false && nalBuf !== null && nalBuf !== undefined) {
                this.frameList.push(nalBuf);
            } else {
                break;
            }
        }

        return true;
    }

    /**
     * sub nalu stream, and get Nalu unit
     */
    subBuf(startOpen, endOpen) { // sub block [m,n]
        // nal
        let returnBuf = new Uint8Array(
            this.stream.subarray(startOpen, endOpen + 1)
        );

        // streamBuf sub
        this.stream = new Uint8Array(
            this.stream.subarray(endOpen + 1)
        );

        return returnBuf;
    }

    /**
     * @param onceGetNalCount: once use get nal count, defult 1
     * @return uint8array OR false
     */
    nextNalu(onceGetNalCount=1) {

        // check params
        if (this.stream == null || this.stream.length <= 4) {
            return false;
        }

        // start nal pos
        let startTag = -1;
        // return nalBuf
        let returnNalBuf = null;

        for (let i = 0;i < this.stream.length; i++) {
            if (i + 5 >= this.stream.length) {
                return false;
                // if (startTag == -1) {
                //     return false;
                // } else {
                //     // 如果结尾不到判断的字节位置 就直接全量输出最后一个nal
                //     returnNalBuf = this.subBuf(startTag, this.stream.length-1);
                //     return returnNalBuf;
                // }
            }

            // find nal
            if (
                (   // 0x00 00 01
                    this.stream[i]        == 0
                    && this.stream[i+1]   == 0
                    && this.stream[i+2]   == 1
                ) || 
                (   // 0x00 00 00 01
                    this.stream[i]        == 0
                    && this.stream[i+1]   == 0
                    && this.stream[i+2]   == 0
                    && this.stream[i+3]   == 1
                )
            ) {
                // console.log(
                //     "enter find nal , now startTag:" + startTag 
                //     + ", now pos:" + i
                // );
                let nowPos = i;
                i += AfterGetNalThenMvLen; // 移出去
                // begin pos
                if (startTag == -1) {
                    startTag = nowPos;
                } else {
                    if (onceGetNalCount <= 1) {
                        // startCode - End
                        // [startTag,nowPos)
                        // console.log("[===>] last code hex is :" + this.stream[nowPos-1].toString(16))
                        returnNalBuf = this.subBuf(startTag,nowPos-1);
                        return returnNalBuf;
                    } else {
                        onceGetNalCount -= 1;
                    }
                }
            }

        } // end for

        return false;
    }

    nextNalu2(onceGetNalCount=1) {
        // check params
        if (this.stream == null || this.stream.length <= 4) {
            return false;
        }

        // start nal pos
        let startTag = -1;
        // return nalBuf
        let returnNalBuf = null;

        for (let i = 0;i < this.stream.length; i++) {
            if (i + 5 >= this.stream.length) {
                if (startTag == -1) {
                    return false;
                } else {
                    // 如果结尾不到判断的字节位置 就直接全量输出最后一个nal
                    returnNalBuf = this.subBuf(startTag,this.stream.length-1);
                    return returnNalBuf;
                }
            }

            // find nal
            let is3BitHeader = this.stream.slice(i, i+3).join(' ') == '0 0 1';
            let is4BitHeader = this.stream.slice(i, i+4).join(' ') == '0 0 0 1';
            if (
                is3BitHeader || 
                is4BitHeader
            ) {
                let nowPos = i;
                i += AfterGetNalThenMvLen; // 移出去
                // begin pos
                if (startTag == -1) {
                    startTag = nowPos;
                } else {
                    if (onceGetNalCount <= 1) {
                        // startCode - End
                        // [startTag,nowPos)
                        // console.log("[===>] last code hex is :" + this.stream[nowPos-1].toString(16))
                        returnNalBuf = this.subBuf(startTag, nowPos-1);
                        return returnNalBuf;
                    } else {
                        onceGetNalCount -= 1;
                    }
                }
            }

        } // end for
        return false;
    }


    /**
     * @brief sub nalu stream, and get Nalu unit
     *          to parse: 
     *           typedef struct {
     *               uint32_t width;
     *               uint32_t height;
     *               uint8_t *dataY;
     *               uint8_t *dataChromaB;
     *               uint8_t *dataChromaR;
     *           } ImageData;
     * @params struct_ptr: Module.cwrap('getFrame', 'number', [])
     * @return Dict
     */
    parseYUVFrameStruct(struct_ptr = null) { // sub block [m,n]
        if (struct_ptr == null || !struct_ptr || struct_ptr == undefined) {
            return null;
        }

        let width           = Module.HEAPU32[struct_ptr / 4];
        let height          = Module.HEAPU32[struct_ptr / 4 + 1];
        // let imgBufferPtr    = Module.HEAPU32[ptr / 4 + 2];
        // let imageBuffer     = Module.HEAPU8.subarray(imgBufferPtr, imgBufferPtr + width * height * 3);
        // console.log("width:",width," height:",height);

        let sizeWH          = width * height;
        // let imgBufferYPtr   = Module.HEAPU32[ptr / 4 + 2];
        // let imageBufferY    = Module.HEAPU8.subarray(imgBufferYPtr, imgBufferYPtr + sizeWH);

        // let imgBufferBPtr   = Module.HEAPU32[ptr/4+ 2 + sizeWH/4 + 1];
        // let imageBufferB    = Module.HEAPU8.subarray(
        //     imgBufferBPtr, 
        //     imgBufferBPtr + sizeWH/4
        // );
        // console.log(imageBufferB);

        // let imgBufferRPtr   = Module.HEAPU32[imgBufferBPtr + sizeWH/16 + 1];
        // let imageBufferR    = Module.HEAPU8.subarray(
        //     imgBufferRPtr, 
        //     imgBufferRPtr + sizeWH/4
        // );

        let imgBufferPtr = Module.HEAPU32[struct_ptr / 4 + 1 + 1];

        let imageBufferY = Module.HEAPU8.subarray(imgBufferPtr, imgBufferPtr + sizeWH);

        let imageBufferB = Module.HEAPU8.subarray(
            imgBufferPtr + sizeWH + 8, 
            imgBufferPtr + sizeWH + 8 + sizeWH/4
        );

        let imageBufferR = Module.HEAPU8.subarray(
            imgBufferPtr + sizeWH + 8 + sizeWH/4 + 8,
            imgBufferPtr + sizeWH + 8 + sizeWH/2 + 8
        );

        return {
            width           : width,
            height          : height,
            sizeWH          : sizeWH,
            imageBufferY    : imageBufferY,
            imageBufferB    : imageBufferB,
            imageBufferR    : imageBufferR
        };
    }

}