ffmpeg 音频编码器

#pragma once
#include "FfmpegHeader.h"

//原始数据格式
typedef struct SourceParams {
	int channels;					//通道
	int sample_rate;				//采样率
	enum AVSampleFormat sample_fmt;	//数据位数
}SourceParams;

class AudioEncode
{
public:
	AudioEncode();

	int Open(const char * file, const SourceParams ¶ms);

	int Begin();
	int Encode(AVFrame * frame);
	int Flush();
	int End();
	void Close();
	//获取样本大小
	int GetSampleSize();
private:
	int Init(const SourceParams ¶ms);
	int InitSWR(const SourceParams &srcParams);
private:
	AVFormatContext* FormatCtx;
	AVStream* AudioStream;
	AVCodecContext*  CodecCtx;
	SwrContext * Swr_ctx;
	AVFrame* DesFrame;
	AVPacket *Packet;
	int DTS;
};


AVCodecContext *CreateCodecContent(AVCodecParameters *codecpar);

//SourceParams srcParams = { 2,44100,AV_SAMPLE_FMT_S16 };
//获取样本大小
int GetAVFrameSize(const SourceParams &srcParams, int sampleSize);


/**
*最简单的基于FFmpeg的音频编码器
*Simplest FFmpeg Audio Encoder
*
*雷霄骅 Lei Xiaohua
*leixiaohua1020@126.com
*中国传媒大学/数字电视技术
*Communication University of China / Digital TV Technology
*http://blog.csdn.net/leixiaohua1020
*
*本程序实现了音频PCM采样数据编码为压缩码流(MP3,WMA,AAC等)。
*是最简单的FFmpeg音频编码方面的教程。
*通过学习本例子可以了解FFmpeg的编码流程。
*This software encode PCM data to AAC bitstream.
*It's the simplest audio encoding software based on FFmpeg.
*Suitable for beginner of FFmpeg
*/
#include "AudioEncode.h" 
#include <stdio.h>

#define __STDC_CONSTANT_MACROS

AVCodecContext *CreateCodecContent(AVCodecParameters *codecpar)
{
	AVCodecContext *codecContext = avcodec_alloc_context3(NULL);
	avcodec_parameters_to_context(codecContext, codecpar);
	return codecContext;
}


static int Index = 0;
//写入单帧AAC数据至文件,每一帧包含AAC的7字节头
int write_buffer(void *opaque, uint8_t *buf, int buf_size) {
	printf("write_buffer:%d\n",buf_size);

	char file[1024];
	sprintf(file, "./tmp/%d.aac", Index++);
	FILE * fp = fopen(file, "wb");
	if (fp == NULL)
	{
		return 0;
	}
	int ret = fwrite(buf, 1, buf_size, fp);
	fclose(fp);

	return buf_size;
}

AudioEncode::AudioEncode()
	:FormatCtx(NULL)
	, CodecCtx(NULL)
	, Swr_ctx(NULL)
	, DesFrame(NULL)
	, Packet(NULL)
	, DTS(0)
{
}

int AudioEncode::Open(const char * file, const SourceParams ¶ms)
{
	av_register_all();

	AVOutputFormat * fmt = NULL;
	//通过文件名找文件格式
	if (file != NULL)
	{
		fmt = av_guess_format(NULL, file, NULL);
	}
	//如果找不到则搜索AAC格式
	if(fmt == NULL)
	{ 
		//AV_CODEC_ID_AAC
		//AV_CODEC_ID_H264
		//AV_CODEC_ID_H265
		//AV_CODEC_ID_RAWVIDEO
		//搜索AAC输出器
		while(true)
		{
			fmt = av_oformat_next(fmt);
			if (fmt == NULL) break;
			if (fmt->audio_codec == AV_CODEC_ID_AAC
				&& fmt->video_codec == AV_CODEC_ID_NONE
				&& fmt->subtitle_codec == AV_CODEC_ID_NONE)
			{
				break;
			}
		}
	}
	if (fmt == NULL)
	{
		return -1;
	}

	int ret = avformat_alloc_output_context2(&FormatCtx, fmt, NULL, NULL);
	if (ret < 0)
	{
		return -1;
	}

	//如果不保存为文件,则将每一帧数据通过自定义IO流输出
	if(file == NULL)
	{
		int bufferSize = 1024;
		unsigned char* outbuffer = (unsigned char*)av_malloc(bufferSize);
		AVIOContext *avio_out = avio_alloc_context(outbuffer, bufferSize, AVIO_FLAG_WRITE, NULL,
			NULL, write_buffer, NULL);
		FormatCtx->pb = avio_out;
		//开启单帧刷新模式
		FormatCtx->flags = AVFMT_FLAG_CUSTOM_IO | AVFMT_FLAG_FLUSH_PACKETS;
	}
	else
	{
		//写文件IO流
		if (avio_open(&FormatCtx->pb, file, AVIO_FLAG_WRITE) < 0) {
			printf("Failed to open output file!\n");
			return -1;
		}

		//显示格式信息
		av_dump_format(FormatCtx, 0, file, 1);
	}

	ret = Init(params);
	if (ret < 0) return ret;
	return InitSWR(params);
}

int AudioEncode::Init(const SourceParams ¶ms)
{
	AudioStream = avformat_new_stream(FormatCtx, 0);
	if (AudioStream == NULL) {
		return -1;
	}

	if (AudioStream->codec == NULL)
	{
		CodecCtx = CreateCodecContent(AudioStream->codecpar);
		AudioStream->codec = CodecCtx;
	}
	else
	{
		CodecCtx = AudioStream->codec;
	}

	CodecCtx->codec_type = AVMEDIA_TYPE_AUDIO;
	CodecCtx->codec_id = FormatCtx->oformat->audio_codec;

	CodecCtx->bit_rate =  128000;
	CodecCtx->time_base = { 1,params.sample_rate };

	//最重要的4个参数
	CodecCtx->sample_rate = params.sample_rate;	// 44100;
	CodecCtx->sample_fmt = AV_SAMPLE_FMT_FLTP;
	CodecCtx->channels = params.channels;
	CodecCtx->channel_layout = av_get_default_channel_layout(CodecCtx->channels);

	AVCodec* codec = avcodec_find_encoder(CodecCtx->codec_id);
	if (!codec) {
		printf("Can not find encoder!\n");
		return -1;
	}

	AVDictionary *param = NULL;
	//H264, 设置为编码延迟为立即编码
	if (CodecCtx->codec_id == AV_CODEC_ID_H264)
	{
		av_dict_set(¶m, "preset", "superfast", 0);
		av_dict_set(¶m, "tune", "zerolatency", 0);
	}
	//H.265  
	else if (CodecCtx->codec_id == AV_CODEC_ID_H265)
	{
		av_dict_set(¶m, "x265-params", "qp=20", 0);
		av_dict_set(¶m, "preset", "ultrafast", 0);
		av_dict_set(¶m, "tune", "zero-latency", 0);
	}

	int ret = avcodec_open2(CodecCtx, codec, ¶m);
	if (ret < 0) {
		printf("Failed to open encoder!\n");
		return -1;
	}

	int samples_size = CodecCtx->frame_size;//样本数

	//转换后的数据存储区域
	DesFrame = av_frame_alloc();
	DesFrame->nb_samples = samples_size;
	DesFrame->format = CodecCtx->sample_fmt;

	int32_t frameSize = av_samples_get_buffer_size(NULL, CodecCtx->channels, samples_size, CodecCtx->sample_fmt, 1);
	uint8_t* frame_buf = (uint8_t *)av_malloc(frameSize);

	avcodec_fill_audio_frame(DesFrame, CodecCtx->channels,
		CodecCtx->sample_fmt, (const uint8_t*)frame_buf, frameSize, 1);

	Packet = av_packet_alloc();

	DTS = 0;

	return 0;
}

int AudioEncode::GetSampleSize()
{
	return CodecCtx->frame_size;
}

int AudioEncode::InitSWR(const SourceParams &srcParams)
{
	AVCodecContext*  codecCtx = CodecCtx;
	//转换器
	/* 由AV_SAMPLE_FMT_FLT转为AV_SAMPLE_FMT_FLTP */
	Swr_ctx = swr_alloc_set_opts(
		NULL,
		av_get_default_channel_layout(codecCtx->channels),
		codecCtx->sample_fmt,                   //在编码前,我希望的采样格式  
		codecCtx->sample_rate,
		av_get_default_channel_layout(srcParams.channels),
		srcParams.sample_fmt,                      //PCM源文件的采样格式  
		srcParams.sample_rate,
		0, NULL);

	swr_init(Swr_ctx);
	/* 分配空间 */
	uint8_t ** convert_data = (uint8_t**)calloc(codecCtx->channels,
		sizeof(*convert_data));

	av_samples_alloc(convert_data, NULL,
		codecCtx->channels, codecCtx->frame_size,
		codecCtx->sample_fmt, 0);

	return 0;
}

int AudioEncode::Begin()
{
	//Write Header
	return avformat_write_header(FormatCtx, NULL);
}

int AudioEncode::Encode(AVFrame * frame)
{
	swr_convert(Swr_ctx, DesFrame->data, DesFrame->nb_samples, (const uint8_t**)frame->data, frame->nb_samples);

	DesFrame->pts = DTS * 100;
	DTS++;

	int got_frame = 0;
	int ret = avcodec_encode_audio2(CodecCtx, Packet,DesFrame, &got_frame);
	if (ret < 0) {
		char errbuf[1024] = { 0 };
		av_strerror(ret, errbuf, 1024);
		printf("AudioEncode::Encode:avcodec_send_packet:%d(%s).\n", ret, errbuf);

		if (ret == AVERROR(EAGAIN)) return 0;
		if (ret == AVERROR_INVALIDDATA) return 0;
		if (ret == AVERROR_EOF) return -1;
		if (ret == AVERROR(EINVAL)) return -1;
		if (AVERROR(ENOMEM)) return -1;

		printf("Failed to encode!\n");
		return -1;
	}
	if (got_frame == 1) {
		printf("Succeed to encode 1 frame! \tsize:%5d pts:%Id dts:%Id\n", Packet->size,Packet->pts,Packet->dts);
		Packet->stream_index = AudioStream->index;
		ret = av_write_frame(FormatCtx, Packet);
		av_packet_unref(Packet);

		return 1;
	}
	return 0;
}

int AudioEncode::Flush()
{
	if (!(CodecCtx->codec->capabilities &
		CODEC_CAP_DELAY))
	{
		return 0;
	}

	int ret;
	int got_frame;
	AVPacket packet;
	while (1) {
		packet.data = NULL;
		packet.size = 0;
		av_init_packet(&packet);

		ret = avcodec_encode_audio2(CodecCtx, &packet,
			NULL, &got_frame);

		if (ret < 0)
		{
			break;
		}
		if (!got_frame) {
			ret = 0;
			break;
		}else if(got_frame == 1)
		{
			printf("Flush Encoder: Succeed to encode 1 frame!\tsize:%5d\n", packet.size);

			/* mux encoded frame */
			packet.stream_index = AudioStream->index;
			ret = av_write_frame(FormatCtx, &packet);
			if (ret < 0)
				break;
		}
		av_packet_unref(&packet);	
	}
	return ret;
}

int AudioEncode::End()
{
	//Write Trailer
	return av_write_trailer(FormatCtx);
}

void AudioEncode::Close()
{
	if (DesFrame != NULL)
	{
		av_free(DesFrame->data[0]);
		av_frame_free(&DesFrame);
	}

	if (Packet != NULL)
	{
		av_packet_free(&Packet);
	}
	if(Swr_ctx != NULL)
	{
		swr_free(&Swr_ctx);
	}

	if (AudioStream != NULL) {
		avcodec_close(AudioStream->codec);
		AudioStream = NULL;
	}

	if (CodecCtx = NULL)
	{
		//avcodec_close(CodecCtx);
		avcodec_free_context(&CodecCtx);
	}

	if(FormatCtx != NULL)
	{
		//avio_close(FormatCtx->pb);
		//avformat_free_context(FormatCtx);
		avformat_close_input(&FormatCtx);
	}
}

int GetAVFrameSize(const SourceParams &srcParams, int sampleSize)
{
	int size = av_samples_get_buffer_size(NULL, srcParams.channels, sampleSize, srcParams.sample_fmt, 1);
	return size;
}
此模块用于将pcm音频数据转码为AAC及其他格式,如果Open指定文件名则输出至文件,如果未指定文件名则输出至IO回调中,IO回调直接将单帧打包好的数据写入tmp目录中.
AAC音频输入至文件中的帧数据是要比编码之后的AVPacket *Packet数据多7个字节的头信息.

在write_buffer回调中的数据是已经写入头信息的音频数据.


AAC音频头7字节格式处理代码片段为:

			AVCodecContext* codecCtx = ifmt_ctx->streams[audioindex]->codec;
			if (codecCtx->codec_id == AV_CODEC_ID_AAC)
			{
				//生成AAC数据7字节头
				char bits[7] = { 0 };
				int sample_index = 0, channel = 0;
				char temp = 0;
				int length = 7 + pkt.size;
				sample_index = (codecCtx->extradata[0] & 0x07) << 1;
				temp = (codecCtx->extradata[1] & 0x80);
				switch (codecCtx->sample_rate)
				{
				case 44100:
					sample_index = 0x4;
					break;
				default:
					sample_index = sample_index + (temp >> 7);
					break;
				}
				channel = ((codecCtx->extradata[1] - temp) & 0xff) >> 3;
				bits[0] = 0xff;
				bits[1] = 0xf1;
				bits[2] = 0x40 | (sample_index << 2) | (channel >> 2);
				bits[3] = ((channel & 0x3) << 6 | (length >> 11));
				bits[4] = (length >> 3) & 0xff;
				bits[5] = ((length << 5) & 0xff) | 0x1f;
				bits[6] = 0xfc;
				fwrite(bits, 1, 7, file);
			}

主要需要的参数为:channels 通道数,sample_rate 采样率, AVPacket 包数据大小.



直接内存转码模块:


#include "FfmpegHeader.h"
#include "AudioEncode.h"

class AudioTranscode
{
public:
	AudioTranscode();
	~AudioTranscode();
	int Init(const SourceParams &srcParams);
	
	int Transcode(AVFrame *frame, AVPacket *packet);
	int GetSampleSize();
	void Close();
private:
	int InitSWR(const SourceParams &srcParams);
private:
	AVCodecContext*  CodecCtx;
	SwrContext * Swr_ctx;
	AVFrame* DesFrame;
	int DTS;
};

#include "AudioTranscode.h"

AudioTranscode::AudioTranscode()
{
}

AudioTranscode::~AudioTranscode()
{
}

int AudioTranscode::Init(const SourceParams ¶ms)
{
	av_register_all();

	CodecCtx = avcodec_alloc_context3(NULL); 

	CodecCtx->codec_type = AVMEDIA_TYPE_AUDIO;
	CodecCtx->codec_id = AV_CODEC_ID_AAC;

	CodecCtx->bit_rate = 128000;
	CodecCtx->time_base = { 1,params.sample_rate };

	//最重要的4个参数
	CodecCtx->sample_rate = params.sample_rate;	// 44100;
	CodecCtx->sample_fmt = AV_SAMPLE_FMT_FLTP;
	CodecCtx->channels = params.channels;
	CodecCtx->channel_layout = av_get_default_channel_layout(CodecCtx->channels);

	AVCodec* codec = avcodec_find_encoder(CodecCtx->codec_id);
	if (!codec) {
		printf("Can not find encoder!\n");
		return -1;
	}

	AVDictionary *param = NULL;
	//H264, 设置为编码延迟为立即编码
	if (CodecCtx->codec_id == AV_CODEC_ID_H264)
	{
		av_dict_set(¶m, "preset", "superfast", 0);
		av_dict_set(¶m, "tune", "zerolatency", 0);
	}
	//H.265  
	else if (CodecCtx->codec_id == AV_CODEC_ID_H265)
	{
		av_dict_set(¶m, "x265-params", "qp=20", 0);
		av_dict_set(¶m, "preset", "ultrafast", 0);
		av_dict_set(¶m, "tune", "zero-latency", 0);
	}

	int ret = avcodec_open2(CodecCtx, codec, ¶m);
	if (ret < 0) {
		printf("Failed to open encoder!\n");
		return -1;
	}

	/*CodecCtx->frame_number = 1;
	CodecCtx->flags &= ~AV_CODEC_FLAG_BITEXACT;*/

	int samples_size = CodecCtx->frame_size;//样本数

	//转换后的数据存储区域
	DesFrame = av_frame_alloc();
	DesFrame->nb_samples = samples_size;
	DesFrame->format = CodecCtx->sample_fmt;

	int32_t frameSize = av_samples_get_buffer_size(NULL, CodecCtx->channels, samples_size, CodecCtx->sample_fmt, 1);
	uint8_t* frame_buf = (uint8_t *)av_malloc(frameSize);

	avcodec_fill_audio_frame(DesFrame, CodecCtx->channels,
		CodecCtx->sample_fmt, (const uint8_t*)frame_buf, frameSize, 1);

	DTS = 0;

	return InitSWR(params);
	return 0;
}

int AudioTranscode::InitSWR(const SourceParams &srcParams)
{
	AVCodecContext*  codecCtx = CodecCtx;
	//转换器
	/* 由AV_SAMPLE_FMT_FLT转为AV_SAMPLE_FMT_FLTP */
	Swr_ctx = swr_alloc_set_opts(
		NULL,
		av_get_default_channel_layout(codecCtx->channels),
		codecCtx->sample_fmt,                   //在编码前,我希望的采样格式  
		codecCtx->sample_rate,
		av_get_default_channel_layout(srcParams.channels),
		srcParams.sample_fmt,                      //PCM源文件的采样格式  
		srcParams.sample_rate,
		0, NULL);

	swr_init(Swr_ctx);
	/* 分配空间 */
	uint8_t ** convert_data = (uint8_t**)calloc(codecCtx->channels,
		sizeof(*convert_data));

	av_samples_alloc(convert_data, NULL,
		codecCtx->channels, codecCtx->frame_size,
		codecCtx->sample_fmt, 0);

	return 0;
}

int AudioTranscode::Transcode(AVFrame *frame, AVPacket *packet)
{
	if (packet == NULL)
	{
		return -1;
	}

	int got_frame = 0;
	int ret = 0;
	if (frame != NULL)
	{
		swr_convert(Swr_ctx, DesFrame->data, DesFrame->nb_samples, (const uint8_t**)frame->data, frame->nb_samples);

		DesFrame->pts = DTS * 100;
		DTS++;

		ret = avcodec_encode_audio2(CodecCtx, packet, DesFrame, &got_frame);
	}
	else
	{
		ret = avcodec_encode_audio2(CodecCtx, packet, NULL, &got_frame);
	}

	if (ret < 0) {
		char errbuf[1024] = { 0 };
		av_strerror(ret, errbuf, 1024);
		printf("AudioEncode::Encode:avcodec_send_packet:%d(%s).\n", ret, errbuf);

		if (ret == AVERROR(EAGAIN)) return 0;
		if (ret == AVERROR_INVALIDDATA) return 0;
		if (ret == AVERROR_EOF) return -1;
		if (ret == AVERROR(EINVAL)) return -1;
		if (AVERROR(ENOMEM)) return -1;

		printf("Failed to encode!\n");
		return -1;
	}
	if (got_frame == 1) {
		printf("Succeed to encode 1 frame! \tsize:%5d pts:%Id dts:%Id\n", packet->size, packet->pts, packet->dts);
		return 1;
	}

	return 0;
}

int AudioTranscode::GetSampleSize()
{
	return CodecCtx->frame_size;
}

void AudioTranscode::Close()
{
	if (DesFrame != NULL)
	{
		av_free(DesFrame->data[0]);
		av_frame_free(&DesFrame);
	}

	if (Swr_ctx != NULL)
	{
		swr_free(&Swr_ctx);
	}

	if (CodecCtx = NULL)
	{
		avcodec_close(CodecCtx);
		avcodec_free_context(&CodecCtx);
	}
}


测试代码:

int ae_main()
{
	const char* file = ".\\tdjm.yuv";          //Output URL
	SourceParams srcParams = { 2,44100,AV_SAMPLE_FMT_S16 };
	const char *src_file = "tdjm.pcm";

	AudioEncode encode;
	int ret = encode.Open(NULL, srcParams);
	if (ret < 0)
	{
		return -1;
	}

	FILE *in_file = fopen(src_file, "rb");

	AVFrame * frame = av_frame_alloc();
	frame->nb_samples = encode.GetSampleSize();
	frame->format = srcParams.sample_fmt;

	int size = GetAVFrameSize(srcParams, encode.GetSampleSize());
	uint8_t* src_buf = (uint8_t*)av_malloc(size);

	avcodec_fill_audio_frame(frame, srcParams.channels,
		srcParams.sample_fmt, (const uint8_t*)src_buf, size, 1);

	encode.Begin();

	int packetCount = 0;
	int encodeCount = 0;
	for (int i = 0; ; i++) {
		//Read PCM
		ret = fread(src_buf, 1, size, in_file);
		if (ret < 0) {
			printf("Failed to read raw data! \n");
			break;
		}
		else if (feof(in_file)) {
			break;
		}
		packetCount++;
		ret = encode.Encode(frame);
		if (ret < 0)
		{
			break;
		}
		else if(ret == 1)
		{
			encodeCount++;
		}
	}
	encode.Flush();
	encode.End();
	encode.Close();

	return 0;
}

int at_main()
{

	const char* file = ".\\tdjm.yuv";          //Output URL
	SourceParams srcParams = { 2,44100,AV_SAMPLE_FMT_S16 };
	const char *src_file = "tdjm.pcm";

	AudioTranscode trans;

	FILE *in_file = fopen(src_file, "rb");

	int ret = trans.Init(srcParams);
	if (ret < 0)
	{
		return -1;
	}

	AVFrame * frame = av_frame_alloc();
	frame->nb_samples = trans.GetSampleSize();
	frame->format = srcParams.sample_fmt;

	int size = GetAVFrameSize(srcParams, trans.GetSampleSize());
	uint8_t* src_buf = (uint8_t*)av_malloc(size);

	avcodec_fill_audio_frame(frame, srcParams.channels,
		srcParams.sample_fmt, (const uint8_t*)src_buf, size, 1);

	int packetCount = 0;
	int encodeCount = 0;
	AVPacket packet;

	packet.data = NULL;
	packet.size = 0;
	av_init_packet(&packet);
	bool readEnd = false;

	for (int i = 0; ; i++) {
		if(!readEnd)
		{
			//Read PCM
			int ret = fread(src_buf, 1, size, in_file);
			if (ret < 0) {
				printf("Failed to read raw data! \n");
				readEnd = true;
				continue;
			}
			else if (feof(in_file)) {
				readEnd = true;
				continue;
			}
			packetCount++;
		}
		
		ret = trans.Transcode(readEnd?NULL:frame, &packet);
		if (ret < 0)
		{
			break;
		}
		else if (ret == 1)
		{
			char name[1024];
			sprintf(name, "./tmp/%d.aac", encodeCount);
			DumpMemory((char*)packet.data, packet.size, name);

			av_packet_unref(&packet);
			encodeCount++;
		}
		else
		{
			if (readEnd) break;
		}
	}
	trans.Close();

	return 0;
}


资料参考:

http://blog.csdn.net/leixiaohua1020/article/details/39767055

/**
 * 最简单的基于FFmpeg的视音频分离器(简化版)
 * Simplest FFmpeg Demuxer Simple
 *
 * 雷霄骅 Lei Xiaohua
 * leixiaohua1020@126.com
 * 中国传媒大学/数字电视技术
 * Communication University of China / Digital TV Technology
 * http://blog.csdn.net/leixiaohua1020
 *
 * 本程序可以将封装格式中的视频码流数据和音频码流数据分离出来。
 * 在该例子中, 将FLV的文件分离得到H.264视频码流文件和MP3
 * 音频码流文件。
 *
 * 注意:
 * 这个是简化版的视音频分离器。与原版的不同在于,没有初始化输出
 * 视频流和音频流的AVFormatContext。而是直接将解码后的得到的
 * AVPacket中的的数据通过fwrite()写入文件。这样做的好处是流程比
 * 较简单。坏处是对一些格式的视音频码流是不适用的,比如说
 * FLV/MP4/MKV等格式中的AAC码流(上述封装格式中的AAC的AVPacket中
 * 的数据缺失了7字节的ADTS文件头)。
 * 
 *
 * This software split a media file (in Container such as 
 * MKV, FLV, AVI...) to video and audio bitstream.
 * In this example, it demux a FLV file to H.264 bitstream
 * and MP3 bitstream.
 * Note:
 * This is a simple version of "Simplest FFmpeg Demuxer". It is 
 * more simple because it doesn't init Output Video/Audio stream's
 * AVFormatContext. It write AVPacket's data to files directly.
 * The advantages of this method is simple. The disadvantages of
 * this method is it's not suitable for some kind of bitstreams. For
 * example, AAC bitstream in FLV/MP4/MKV Container Format(data in
 * AVPacket lack of 7 bytes of ADTS header).
 *
 */

#include <stdio.h>

#define __STDC_CONSTANT_MACROS

#ifdef _WIN32
//Windows
extern "C"
{
#include "libavformat/avformat.h"
};
#else
//Linux...
#ifdef __cplusplus
extern "C"
{
#endif
#include <libavformat/avformat.h>
#ifdef __cplusplus
};
#endif
#endif

//'1': Use H.264 Bitstream Filter 
#define USE_H264BSF 1
#define USE_AACFILTER 1

int main(int argc, char* argv[])
{
	AVFormatContext *ifmt_ctx = NULL;
	AVPacket pkt;
	int ret, i;
	int videoindex=-1,audioindex=-1;
	const char *in_filename  = "F:/test source/480p.mp4";//Input file URL
	const char *out_filename_v = "F:/test source/demuxing-480P.mp4/video/";//Output file URL
	const char *out_filename_a = "F:/test source/demuxing-480P.mp4/audio/";

	av_register_all();
	//Input
	if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) {
		printf( "Could not open input file.");
		return -1;
	}
	if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {
		printf( "Failed to retrieve input stream information");
		return -1;
	}

	videoindex=-1;
	for(i=0; i<ifmt_ctx->nb_streams; i++) {
		if(ifmt_ctx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){
			videoindex=i;
		}else if(ifmt_ctx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO){
			audioindex=i;
		}
	}
	//Dump Format------------------
	printf("\nInput Video===========================\n");
	av_dump_format(ifmt_ctx, 0, in_filename, 0);
	printf("\n======================================\n");

// 	FILE *fp_audio=fopen(out_filename_a,"wb+");  
// 	FILE *fp_video=fopen(out_filename_v,"wb+");  

	/*
	FIX: H.264 in some container format (FLV, MP4, MKV etc.) need 
	"h264_mp4toannexb" bitstream filter (BSF)
	  *Add SPS,PPS in front of IDR frame
	  *Add start code ("0,0,0,1") in front of NALU
	H.264 in some container (MPEG2TS) don't need this BSF.
	*/
#if USE_H264BSF
	AVBitStreamFilterContext* h264bsfc =  av_bitstream_filter_init("h264_mp4toannexb"); 
#endif
#if USE_AACFILTER
	AVBitStreamFilterContext* aacFilter = av_bitstream_filter_init("aac_adtstoasc");
#endif

	char path[1024] = { 0 };
	int index_v = 0;
	sprintf(path, "%s%d.264", out_filename_v, index_v);
	FILE* file = fopen(path, "ab+");
	fwrite(ifmt_ctx->streams[videoindex]->codec->extradata, ifmt_ctx->streams[videoindex]->codec->extradata_size, 1, file);
	char nal_start[] = { 0,0,0,1 };
	while(av_read_frame(ifmt_ctx, &pkt)>=0){
		if(pkt.stream_index==videoindex){
#if USE_H264BSF
			av_bitstream_filter_filter(h264bsfc, ifmt_ctx->streams[videoindex]->codec, NULL, &pkt.data, &pkt.size, pkt.data, pkt.size, 0);
#endif
			printf("Write Video Packet. size:%d\tpts:%lld\n",pkt.size,pkt.pts);
 			static int index_v = 0;
 			sprintf(path, "%s%d.264", out_filename_v, index_v++);
			//FILE* file = fopen(path, "ab+");
			fwrite(nal_start, 4, 1, file);
			fwrite(pkt.data + 4,1,pkt.size - 4,file);
			//fclose(file);
		}else if(pkt.stream_index==audioindex){
			/*
			AAC in some container format (FLV, MP4, MKV etc.) need to add 7 Bytes
			ADTS Header in front of AVPacket data manually.
			Other Audio Codec (MP3...) works well.
			*/
			printf("Write Audio Packet. size:%d\tpts:%lld\n", pkt.size, pkt.pts);

// #if USE_AACFILTER
// 			av_bitstream_filter_filter(aacFilter, ifmt_ctx->streams[audioindex]->codec, NULL, &pkt.data, &pkt.size, pkt.data, pkt.size, 0);
// #endif

			static int index_a = 0;
			sprintf(path, "%s%d.aac", out_filename_a, ++index_a);
			FILE* file = fopen(path, "ab+");

			AVCodecContext* codecCtx = ifmt_ctx->streams[audioindex]->codec;
			if (codecCtx->codec_id == AV_CODEC_ID_AAC)
			{
				//生成AAC数据7字节头
				char bits[7] = { 0 };
				int sample_index = 0, channel = 0;
				char temp = 0;
				int length = 7 + pkt.size;
				sample_index = (codecCtx->extradata[0] & 0x07) << 1;
				temp = (codecCtx->extradata[1] & 0x80);
				switch (codecCtx->sample_rate)
				{
				case 44100:
					sample_index = 0x4;
					break;
				default:
					sample_index = sample_index + (temp >> 7);
					break;
				}
				channel = ((codecCtx->extradata[1] - temp) & 0xff) >> 3;
				bits[0] = 0xff;
				bits[1] = 0xf1;
				bits[2] = 0x40 | (sample_index << 2) | (channel >> 2);
				bits[3] = ((channel & 0x3) << 6 | (length >> 11));
				bits[4] = (length >> 3) & 0xff;
				bits[5] = ((length << 5) & 0xff) | 0x1f;
				bits[6] = 0xfc;
				fwrite(bits, 1, 7, file);
			}

			fwrite(pkt.data, 1, pkt.size, file);
			fclose(file);
		}
		av_free_packet(&pkt);
	}

#if USE_H264BSF
	av_bitstream_filter_close(h264bsfc);  
#endif

 	fclose(file);
// 	fclose(fp_audio);

	avformat_close_input(&ifmt_ctx);

	if (ret < 0 && ret != AVERROR_EOF) {
		printf( "Error occurred.\n");
		return -1;
	}
	return 0;
}




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值