Player.cpp 4.98 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * MIT License
xzl committed
3
 *
xiongziliang committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * 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
25 26
 */

xiongzilaing committed
27
#include <cstring>
xzl committed
28 29 30 31
#include "Player.h"
#include "H264/SPSParser.h"
#include "Util/logger.h"

xiongzilaing committed
32
using namespace ZL::Util;
xzl committed
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


void writeAdtsHeader(const AdtsFrame &hed, uint8_t *pcAdts) {
	pcAdts[0] = (hed.syncword >> 4 & 0xFF); //8bit
	pcAdts[1] = (hed.syncword << 4 & 0xF0); //4 bit
	pcAdts[1] |= (hed.id << 3 & 0x08); //1 bit
	pcAdts[1] |= (hed.layer << 1 & 0x06); //2bit
	pcAdts[1] |= (hed.protection_absent & 0x01); //1 bit

	pcAdts[2] = (hed.profile << 6 & 0xC0); // 2 bit
	pcAdts[2] |= (hed.sf_index << 2 & 0x3C); //4bit
	pcAdts[2] |= (hed.private_bit << 1 & 0x02); //1 bit
	pcAdts[2] |= (hed.channel_configuration >> 2 & 0x03); //1 bit

	pcAdts[3] = (hed.channel_configuration << 6 & 0xC0);  // 2 bit
	pcAdts[3] |= (hed.original << 5 & 0x20);				//1 bit
	pcAdts[3] |= (hed.home << 4 & 0x10);				//1 bit
	pcAdts[3] |= (hed.copyright_identification_bit << 3 & 0x08);			//1 bit
	pcAdts[3] |= (hed.copyright_identification_start << 2 & 0x04);		//1 bit
	pcAdts[3] |= (hed.aac_frame_length >> 11 & 0x03);				//2 bit

	pcAdts[4] = (hed.aac_frame_length >> 3 & 0xFF);				//8 bit

	pcAdts[5] = (hed.aac_frame_length << 5 & 0xE0);				//3 bit
	pcAdts[5] |= (hed.adts_buffer_fullness >> 6 & 0x1F);				//5 bit

	pcAdts[6] = (hed.adts_buffer_fullness << 2 & 0xFC);				//6 bit
	pcAdts[6] |= (hed.no_raw_data_blocks_in_frame & 0x03);				//2 bit
}
string 	makeAdtsConfig(const uint8_t *pcAdts){
	if (!(pcAdts[0] == 0xFF && (pcAdts[1] & 0xF0) == 0xF0)) {
		return "";
	}
	// Get and check the 'profile':
	unsigned char profile = (pcAdts[2] & 0xC0) >> 6; // 2 bits
	if (profile == 3) {
		return "";
	}

	// Get and check the 'sampling_frequency_index':
	unsigned char sampling_frequency_index = (pcAdts[2] & 0x3C) >> 2; // 4 bits
	if (samplingFrequencyTable[sampling_frequency_index] == 0) {
		return "";
	}

	// Get and check the 'channel_configuration':
	unsigned char channel_configuration = ((pcAdts[2] & 0x01) << 2)
			| ((pcAdts[3] & 0xC0) >> 6); // 3 bits

	unsigned char audioSpecificConfig[2];
	unsigned char const audioObjectType = profile + 1;
	audioSpecificConfig[0] = (audioObjectType << 3) | (sampling_frequency_index >> 1);
	audioSpecificConfig[1] = (sampling_frequency_index << 7) | (channel_configuration << 3);
	return string((char *)audioSpecificConfig,2);
}
void makeAdtsHeader(const string &strAudioCfg,AdtsFrame &adts) {
	uint8_t cfg1 = strAudioCfg[0];
	uint8_t cfg2 = strAudioCfg[1];

	int audioObjectType;
	int sampling_frequency_index;
	int channel_configuration;

	audioObjectType = cfg1 >> 3;
	sampling_frequency_index = ((cfg1 & 0x07) << 1) | (cfg2 >> 7);
	channel_configuration = (cfg2 & 0x7F) >> 3;

	adts.syncword = 0x0FFF;
	adts.id = 0;
	adts.layer = 0;
	adts.protection_absent = 1;
	adts.profile = audioObjectType - 1;
	adts.sf_index = sampling_frequency_index;
	adts.private_bit = 0;
	adts.channel_configuration = channel_configuration;
	adts.original = 0;
	adts.home = 0;
	adts.copyright_identification_bit = 0;
	adts.copyright_identification_start = 0;
	adts.aac_frame_length = 7;
	adts.adts_buffer_fullness = 2047;
	adts.no_raw_data_blocks_in_frame = 0;
}
void getAACInfo(const AdtsFrame &adts,int &iSampleRate,int &iChannel){
	iSampleRate = samplingFrequencyTable[adts.sf_index];
	iChannel = adts.channel_configuration;
}
bool getAVCInfo(const string& strSps,int &iVideoWidth, int &iVideoHeight, float  &iVideoFps) {
	T_GetBitContext tGetBitBuf;
	T_SPS tH264SpsInfo;
	memset(&tGetBitBuf,0,sizeof(tGetBitBuf));
	memset(&tH264SpsInfo,0,sizeof(tH264SpsInfo));
	tGetBitBuf.pu8Buf = (uint8_t *)strSps.data() + 1;
	tGetBitBuf.iBufSize = strSps.size() - 1;
	if(0 != h264DecSeqParameterSet((void *) &tGetBitBuf, &tH264SpsInfo)){
		return false;
	}
	h264GetWidthHeight(&tH264SpsInfo, &iVideoWidth, &iVideoHeight);
	h264GeFramerate(&tH264SpsInfo, &iVideoFps);
xiongziliang committed
132
	//ErrorL << iVideoWidth << " " << iVideoHeight << " " << iVideoFps;
xzl committed
133 134
	return true;
}