框架
一个封装文件(mp4)如何播放?大体流程如下:

案例
本案例实现在windows环境下,调用ffmpeg4.4.5动态库实现上述从解封装、视频解码、音频解码的全部过程,案例测试通过。由于ffmpeg接口功能网上资料较多,这里就不在赘述,代码如下:
1、DeCoder.h
#pragma once
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
#include <libswresample/swresample.h>
}
#include <iostream>
#include <fstream>
#include <vector>
#define ADTS_HERDER_LEN 7
const int sampling_frequencies[] = {
96000, //0x0
88200, //0x1
64000, //0x2
48000, //0x3
44100, //0x4
32000, //0x5
24000, //0x6
22050, //0x7
16000, //0x8
12000, //0x9
11025, //0xa
8000 //0xb // 0xc d e f是保留的
};
int test_deMuxer();
int test_deCoderH264();
int test_deCoderAac();
2、DeMuxer.cpp
#include "DeCoder.h"
using namespace std;
int adts_header(uint8_t* const p_adts_header, const int data_length, const int profile, const int samplerate, const int channels)
{
int sampling_frequency_index = 3; //48000
int adtsLen = data_length + ADTS_HERDER_LEN; // ADTS的头部信息总是占7个字节
int frequencies_size = sizeof(sampling_frequencies) / sizeof(sampling_frequencies[0]); //求内存的分配大小
int i = 0;
for (i = 0; i < frequencies_size; i++)
{
if (sampling_frequencies[i] == samplerate)
{
sampling_frequency_index = i;
break;
}
}
if (i > frequencies_size)
{
printf("unsupport samplerate:%d \n", samplerate);
return -1;
}
p_adts_header[0] = 0xff; //syncword:0xfff 高8bits 1111 1111 位数不足则用0补齐
p_adts_header[1] = 0xf0; //syncword:0xfff 低4bits 前面的4bit是属于syncword,后面的才是重新赋值 1111 0000
p_adts_header[1] |= (0 << 3); //MPEG Version:0 for MPEG-4,1 for MPEG-2 1bit |= 按位或后赋值 1111 0000 和 0000 = 1111 0000
p_adts_header[1] |= (0 << 1); //Layer:0 2bits 1111 0000 和 00
p_adts_header[1] |= 1; // protection absent:1 不校验 1bit 1111 00000 和 1 = 1111 0000 0000 0001= 1111 0001
p_adts_header[2] = (profile << 6); //profile:profile 2bits profile :1110 0011 左移6位 1100 0000
p_adts_header[2] |= (sampling_frequency_index & 0x0f) << 2; //sampling frequency index:sampling_frequency_index 4bits
p_adts_header[2] |= (0 << 1); //private bit:0 1bit
p_adts_header[2] |= (channels & 0x04) >> 2; //channel configuration:channels 高1bit
p_adts_header[3] = (channels & 0x03) << 6; //channel configuration:channels 低2bits
p_adts_header[3] |= (0 << 5); //original:0 1bit
p_adts_header[3] |= (0 << 4); //home:0 1bit
p_adts_header[3] |= (0 << 3); //copyright id bit:0 1bit
p_adts_header[3] |= (0 << 2); //copyright id start:0 1bit
p_adts_header[3] |= ((adtsLen & 0x1800) >> 11);

最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



