houxiao
2016-12-29 633e76c1d533c3d9c257b92df7ebdfd36c9fd8a0
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
#include "PL_H264Decoder.h"
#include "MaterialBuffer.h"
#include "logger.h"
 
#include <H264VideoRTPSource.hh> // for SPropRecord
#include <libbase64.h>
 
extern "C"
{
    #include <libavcodec/avcodec.h>
    #include <libavutil/frame.h>
    #include <libavformat/avformat.h>
}
 
struct H264Decoder_Internal
{
    //uint8_t buffer[1920*1080*3];
    //size_t buffSize;
    //size_t buffSizeMax;
    bool fmtp_set_to_context;
    bool payError;
 
    AVCodecContext* pAVCodecContext;
    AVFrame* pAVFrame;//#todo delete
    MB_Frame lastFrame;
    
    H264Decoder_Internal() : 
        //buffSize(0), buffSizeMax(sizeof(buffer)), 
        fmtp_set_to_context(false), 
        payError(true), 
        pAVCodecContext(nullptr), pAVFrame(nullptr), 
        lastFrame()
    {
    }
    
    ~H264Decoder_Internal()
    {
    }
    
    void reset()
    {
        //buffSize = 0;
        fmtp_set_to_context = false;
        payError = true;
        
        MB_Frame _lastFrame;
        lastFrame = _lastFrame;
    }
};
 
PipeLineElem* create_PL_H264Decoder()
{
    return new PL_H264Decoder;
}
 
PL_H264Decoder::PL_H264Decoder() : internal(new H264Decoder_Internal)
{
}
 
PL_H264Decoder::~PL_H264Decoder()
{
    delete (H264Decoder_Internal*)internal;
    internal= nullptr;
}
 
bool PL_H264Decoder::init(void* args)
{
    H264Decoder_Internal* in = (H264Decoder_Internal*)internal;
    in->reset();
    
    return true;
}
 
void PL_H264Decoder::finit()
{
    H264Decoder_Internal* in = (H264Decoder_Internal*)internal;
    
}
 
SPropRecord* parseSPropParameterSets(char const* sPropParameterSetsStr, size_t& numSPropRecords) {  
  // Make a copy of the input string, so we can replace the commas with '\0's:  
  char* inStr = strDup(sPropParameterSetsStr);  
  if (inStr == NULL) {  
    numSPropRecords = 0;  
    return NULL;  
  }  
  
  // Count the number of commas (and thus the number of parameter sets):  
  numSPropRecords = 1;  
  char* s;  
  for (s = inStr; *s != '\0'; ++s) {  
    if (*s == ',') {  
      ++numSPropRecords;  
      *s = '\0';  
    }  
  }  
  
  // Allocate and fill in the result array:  
  SPropRecord* resultArray = new SPropRecord[numSPropRecords];
  s = inStr;  
  for (unsigned i = 0; i < numSPropRecords; ++i) {  
    resultArray[i].sPropBytes = new uint8_t[256];
    
    size_t sPropLength = 0;
    base64_decode(s, strlen(s), (char*)resultArray[i].sPropBytes, &sPropLength, 0);
    resultArray[i].sPropLength = sPropLength;
    
    s += strlen(s) + 1;  
  }  
  
  delete[] inStr;  
  return resultArray;  
}
 
bool initH264DecoderEnv(H264Decoder_Internal* in, 
    uint8_t* sps, size_t spsSize, uint8_t* pps, size_t ppsSize)
{
    av_register_all();
 
    // find the video encoder
    AVCodec* avCodec = avcodec_find_decoder(AV_CODEC_ID_H264);
 
    if (!avCodec)   
    {  
        LOG(WARN) << "codec not found!";  
        return false;  
    }  
 
    in->pAVCodecContext = avcodec_alloc_context3(avCodec);
 
    in->pAVCodecContext->time_base.num = 1;
    in->pAVCodecContext->frame_number = 1;
    in->pAVCodecContext->codec_type = AVMEDIA_TYPE_VIDEO;
    in->pAVCodecContext->bit_rate = 0;
    in->pAVCodecContext->time_base.den = 25;
    in->pAVCodecContext->width = 1920;//#todo get from pm
    in->pAVCodecContext->height = 1080;
 
    if (in->pAVCodecContext->extradata == NULL)  
    {  
        int totalsize = 0;  
        unsigned char* tmp = NULL;  
        unsigned char nalu_header[4] = { 0, 0, 0, 1 };  
  
        totalsize = 8 + spsSize + ppsSize;  
  
        tmp = new unsigned char[totalsize];  
        memcpy(tmp, nalu_header, 4);  
        memcpy(tmp + 4, sps, spsSize);  
        memcpy(tmp + 4 + spsSize, nalu_header, 4);  
        memcpy(tmp + 4 + spsSize + 4, pps, ppsSize);  
  
        in->pAVCodecContext->extradata_size = totalsize;
  
        in->pAVCodecContext->extradata = tmp;  
    }
    
    if(avcodec_open2(in->pAVCodecContext, avCodec, NULL) >= 0)
        in->pAVFrame = av_frame_alloc();// Allocate video frame
    else
        return false;
    
    return true;
}
 
bool decodeH264(H264Decoder_Internal* in, uint8_t* buffer, size_t buffSize, timeval pts)  
{
    AVPacket packet = {0};
    int gotPicture = buffSize; // frameFinished
 
    if (av_packet_from_data(&packet, buffer, buffSize) != 0)
    {  
        LOG(WARN) << "av_packet_from_data error";
        return false;
    }
    
    packet.pts = packet.dts = (pts.tv_sec * 1000 * 1000 + pts.tv_usec) / 90000;
    
    // decode
    avcodec_decode_video2(in->pAVCodecContext, in->pAVFrame, &gotPicture, &packet);
    if(gotPicture)
    {
        // decode ok
        return true;
    }
    else
    {
        LOG(WARN) << "incomplete frame";
        return false;
    }
}
 
bool PL_H264Decoder::pay(const PipeMaterial& pm)
{
    H264Decoder_Internal* in = (H264Decoder_Internal*)internal;
    
    in->payError = true;
    
    if (!in->fmtp_set_to_context)
    {
        if (manager == NULL)
            return false;
        
        std::string fmtp(manager->get_global_param(PLGP_RTSP_FMTP));
        if (fmtp.empty())
            return false;
        
        size_t numSPropRecords = 0;
        SPropRecord *p_record = parseSPropParameterSets(fmtp.c_str(), numSPropRecords);
        if (numSPropRecords < 2)
            return false;//#todo log
 
        SPropRecord &sps = p_record[0];  
        SPropRecord &pps = p_record[1];
 
        bool ret = initH264DecoderEnv(in, sps.sPropBytes, sps.sPropLength, pps.sPropBytes, pps.sPropLength);
        if (!ret)
        {
            LOG(ERROR) << "PL_H264Decoder::pay initH264DecoderEnv error";
            return false; // #todo log
        }
        else
            in->fmtp_set_to_context = true;
    }
    
    if (pm.buffer == nullptr)
        return false;
    
    bool ret = false;
    if (pm.type == PipeMaterial::PMT_BYTES)
    {
        if (pm.buffSize <= 0)
            return false;
 
        timeval pts = {0};
        ret = decodeH264(in, (uint8_t*)pm.buffer, pm.buffSize, pts);
    }
    else if (pm.type == PipeMaterial::PMT_FRAME)
    {
        MB_Frame* frame = (MB_Frame*)pm.buffer;
        
        if (frame->buffSize <= 0)
            return false;
        
        ret = decodeH264(in, (uint8_t*)frame->buffer, frame->buffSize, frame->pts);
        if (ret)
        {
            in->lastFrame.type = MB_Frame::MBFT_PTR_AVFRAME;
            in->lastFrame.buffer = (uint8_t*)(in->pAVFrame);
            in->lastFrame.buffSize = sizeof(in->pAVFrame);
            in->lastFrame.width = in->pAVFrame->width;
            in->lastFrame.height = in->pAVFrame->height;
            //in->lastFrame.pts = frame->pts;//#todo
            gettimeofday(&(in->lastFrame.pts),NULL);
        }
    }
    
    in->payError = !ret;
    return ret;
}
 
bool PL_H264Decoder::gain(PipeMaterial& pm)
{
    H264Decoder_Internal* in = (H264Decoder_Internal*)internal;
 
    if (!in->payError)
    {
        pm.type = PipeMaterial::PMT_FRAME;
        pm.buffer = &(in->lastFrame);
        pm.buffSize = 0;
    }
    pm.former = this;
    return !in->payError;
}