C++调用ffmpeg解复用、解码案例

框架

        一个封装文件(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);                    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值