forked from langhuihui/jessibuca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroadway.h
115 lines (106 loc) · 3.02 KB
/
broadway.h
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
#pragma once
#include "H264SwDecApi.h"
class Broadway : public VideoDecoder
{
public:
H264SwDecInst decInst;
H264SwDecInput decInput;
H264SwDecOutput decOutput;
H264SwDecPicture decPicture;
H264SwDecInfo decInfo;
u32 picDecodeNumber;
u32 picDisplayNumber;
Broadway()
{
H264SwDecRet ret;
u32 disableOutputReordering = 0;
ret = H264SwDecInit(&decInst, disableOutputReordering);
if (ret != H264SWDEC_OK)
{
emscripten_log(0, "DECODER INITIALIZATION FAILED\n");
return;
}
picDecodeNumber = picDisplayNumber = 1;
emscripten_log(0, "H264 init");
}
~Broadway()
{
if (decInst)
{
H264SwDecRelease(decInst);
decInst = nullptr;
}
}
void _decode(IOBuffer data) override
{
u8 naluType = data[0] & 0x1f;
// if (!decInst || (naluType != 1 && naluType != 5))
// return;
decInput.pStream = (u8 *)data;
decInput.dataLen = data.length;
u32 i = 0;
do
{
u8 *start = decInput.pStream;
u32 ret = broadwayDecode();
//emscripten_log(0, "Decoded Unit #%d, Size: %x, Result: %i\n", i++, (decInput.pStream - start), ret);
} while (decInput.dataLen > 0);
}
u32 broadwayDecode()
{
H264SwDecRet ret = H264SwDecDecode(decInst, &decInput, &decOutput);
switch (ret)
{
case H264SWDEC_HDRS_RDY_BUFF_NOT_EMPTY:
/* Stream headers were successfully decoded, thus stream information is available for query now. */
ret = H264SwDecGetInfo(decInst, &decInfo);
if (ret != H264SWDEC_OK)
{
return -1;
}
decodeVideoSize(decInfo.picWidth, decInfo.picHeight);
/*picSize = decInfo.picWidth * decInfo.picHeight;
picSize = (3 * picSize) / 2;
printf("picSize:%d", picSize);*/
//broadwayOnHeadersDecoded();
decInput.dataLen -= decOutput.pStrmCurrPos - decInput.pStream;
decInput.pStream = decOutput.pStrmCurrPos;
break;
case H264SWDEC_PIC_RDY_BUFF_NOT_EMPTY:
/* Picture is ready and more data remains in the input buffer,
* update input structure.
*/
decInput.dataLen -= decOutput.pStrmCurrPos - decInput.pStream;
decInput.pStream = decOutput.pStrmCurrPos;
/* fall through */
case H264SWDEC_PIC_RDY:
if (ret == H264SWDEC_PIC_RDY)
{
decInput.dataLen = 0;
}
/* Increment decoding number for every decoded picture */
picDecodeNumber++;
while (H264SwDecNextPicture(decInst, &decPicture, 0) == H264SWDEC_PIC_RDY)
{
//printf(" Decoded Picture Decode: %d, Display: %d, Type: %s\n", picDecodeNumber, picDisplayNumber, decPicture.isIdrPicture ? "IDR" : "NON-IDR");
/* Increment display number for every displayed picture */
picDisplayNumber++;
p_yuv[0] = (u32)decPicture.pOutputPicture;
p_yuv[1] = p_yuv[0] + decInfo.picWidth * decInfo.picHeight;
p_yuv[2] = p_yuv[1] + (decInfo.picWidth * decInfo.picHeight >> 2);
decodeYUV420();
}
break;
case H264SWDEC_STRM_PROCESSED:
case H264SWDEC_STRM_ERR:
/* Input stream was decoded but no picture is ready, thus get more data. */
decInput.dataLen = 0;
break;
default:
emscripten_log(0, "video decode %d", -ret);
decInput.dataLen = 0;
break;
}
return ret;
}
};