H264Parser.cpp 3.17 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * MIT License
 *
 * Copyright (c) 2016 xiongziliang <771730766@qq.com>
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
xzl committed
26 27 28

#include "H264Parser.h"
#include "Util/logger.h"
xiongziliang committed
29
using namespace toolkit;
xiongzilaing committed
30

xzl committed
31 32 33 34 35 36 37 38 39

H264Parser::H264Parser(){
    
}
H264Parser::~H264Parser(){
    
}
void H264Parser::inputH264(const string &h264,uint32_t dts){
    
40
    _parser.SetStream((const uint8_t *)h264.data(), h264.size());
xzl committed
41
    while (true) {
42
        if(media::H264Parser::kOk != _parser.AdvanceToNextNALU(&_nalu)){
xzl committed
43 44 45
            break;
        }
        
46
        switch (_nalu.nal_unit_type) {
xzl committed
47 48
            case media::H264NALU::kNonIDRSlice:
            case media::H264NALU::kIDRSlice:{
49 50
                if(media::H264Parser::kOk == _parser.ParseSliceHeader(_nalu, &_shdr)){
                    const media::H264SPS *pPps = _parser.GetSPS(_shdr.pic_parameter_set_id);
xzl committed
51
                    if (pPps) {
52
                        _poc.ComputePicOrderCnt(pPps, _shdr, &_iNowPOC);
xzl committed
53 54 55 56 57 58 59
                        computePts(dts);
                    }
                }
            }
                break;
            case media::H264NALU::kSPS:{
                int sps_id;
60
                _parser.ParseSPS(&sps_id);
xzl committed
61 62 63 64
            }
                break;
            case media::H264NALU::kPPS:{
                 int pps_id;
65
                _parser.ParsePPS(&pps_id);
xzl committed
66 67 68 69 70 71 72 73 74
            }
                break;
            default:
                break;
        }
    }
}

void H264Parser::computePts(uint32_t iNowDTS) {
75 76
	auto iPOCInc = _iNowPOC - _iLastPOC;
	if (_shdr.slice_type % 5 == 1) {
xzl committed
77
		//这是B帧
78
		_iNowPTS = _iLastPTS + _iMsPerPOC * (iPOCInc);
xzl committed
79 80
	} else {
		//这是I帧或者P帧
81
		_iNowPTS = iNowDTS;
xzl committed
82 83
		//计算每一POC的时间
		if(iPOCInc == 0){
84
			WarnL << "iPOCInc = 0," << _iNowPOC << " " << _iLastPOC;
xzl committed
85
		}else{
86
			_iMsPerPOC = (_iNowPTS - _iLastPTS) / iPOCInc;
xzl committed
87
		}
88 89
		_iLastPTS = _iNowPTS;
		_iLastPOC = _iNowPOC;
xzl committed
90 91 92
	}

    
93
//	DebugL << _shdr.slice_type
xzl committed
94
//			<<"\r\nNOW:"
95 96
//			<< _iNowPOC << " "
//			<< _iNowPTS << " "
xzl committed
97 98
//			<< iNowDTS << " "
//			<< "\r\nLST:"
99 100 101
//			<< _iLastPOC << " "
//			<< _iLastPTS << " "
//			<< _iMsPerPOC << endl;
xzl committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

}