您的位置:首页 > 其它

笔记:ffmpeg使用实例(bgr->yuv->h264, pcm->aac,加入音频重采样)

2017-03-27 12:07 501 查看
郑重声明:程序不完善,代码仅供参考。

#include "stdafx.h"

extern "C"

{

#include "libavcodec/avcodec.h"

#include "libavformat/avformat.h"

#include "libswscale/swscale.h"

#include "libswresample/swresample.h"

#include "SDL2/SDL.h"

};

#pragma comment(lib, "avcodec.lib")

#pragma comment(lib, "avformat.lib")

#pragma comment(lib, "avutil.lib")

#pragma comment(lib, "swscale.lib")

#pragma comment(lib, "swresample.lib")

#pragma comment(lib, "SDL2.lib")

#pragma comment(lib, "SDL2main.lib")

#define MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48KHZ 32bit audio -> 32bit / 8 * 48000 * 1 = 192000 Byte

int main(int argc, char* argv[])

{
AVFormatContext* ofmt_ctx_h264 = NULL;
const char* output_filepath_h264 = NULL;

AVFormatContext* ofmt_ctx_aac = NULL;
const char* output_filepath_aac = NULL;

AVStream* ostream_video = NULL;
AVStream* ostream_audio = NULL;

AVCodecContext*
codec_ctx_2h264 = NULL;
AVCodec* codec_2h264 = NULL;

AVCodecContext*
codec_ctx_2aac = NULL;
AVCodec* codec_2aac = NULL;

uint8_t* video_buffer_in = NULL;
int video_buffer_size_in = -1;
uint8_t* video_buffer_out = NULL;
int video_buffer_size_out = -1;
struct SwsContext* video_convert_ctx = NULL;

uint8_t* audio_buffer_in = NULL;
int audio_buffer_size_in = -1;
uint8_t* audio_buffer_out = NULL;
int audio_buffer_size_out = -1;
struct SwrContext* audio_convert_ctx = NULL;

AVFrame  frame_video = { 0 };
AVFrame  frame_audio = { 0 };
AVPacket pkt_video = { 0 };
AVPacket pkt_audio = { 0 };

av_register_all();
avformat_network_init();

output_filepath_h264 = "output.h264";
output_filepath_aac = "output.aac";

if (avformat_alloc_output_context2(&ofmt_ctx_h264, NULL, "h264", output_filepath_h264) < 0)
{
return -1;
}

if (avio_open(&ofmt_ctx_h264->pb, output_filepath_h264, AVIO_FLAG_READ_WRITE) < 0)
{
return -1;
}

if (avformat_alloc_output_context2(&ofmt_ctx_aac, NULL, "adts", output_filepath_aac) < 0)
{
return -1;
}

if (avio_open(&ofmt_ctx_aac->pb, output_filepath_aac, AVIO_FLAG_READ_WRITE) < 0)
{
return - 1;
}

ostream_video = avformat_new_stream(ofmt_ctx_h264, NULL);
ostream_audio = avformat_new_stream(ofmt_ctx_aac, NULL);

ostream_audio->codec->codec_tag = 0;
if (ofmt_ctx_aac->oformat->flags & AVFMT_GLOBALHEADER)
ostream_audio->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;

ostream_video->codec->codec_tag = 0;
if (ofmt_ctx_h264->oformat->flags & AVFMT_GLOBALHEADER)
ostream_video->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;

codec_ctx_2h264 = ostream_video->codec;
codec_ctx_2h264->codec_id = ofmt_ctx_h264->oformat->video_codec;
codec_ctx_2h264->codec_type = AVMEDIA_TYPE_VIDEO;
codec_ctx_2h264->pix_fmt = AV_PIX_FMT_YUV420P;
codec_ctx_2h264->width = 1920;
codec_ctx_2h264->height = 1080;
codec_ctx_2h264->bit_rate = 400000;
codec_ctx_2h264->gop_size = 25;
codec_ctx_2h264->time_base.num = 1;
codec_ctx_2h264->time_base.den = 25;
codec_ctx_2h264->qmin = 10;
codec_ctx_2h264->qmax = 51;
codec_ctx_2h264->max_b_frames = 3;

AVDictionary* param = NULL;
av_dict_set(¶m, "preset", "slow", 0);
av_dict_set(¶m, "tu
bed6
ne", "zerolatency", 0);
//av_dict_set(¶m, "profile", "main", 0);

codec_2h264 = avcodec_find_encoder(codec_ctx_2h264->codec_id);
if (avcodec_open2(codec_ctx_2h264, codec_2h264, ¶m) < 0)
{
return -1;
}

codec_ctx_2aac = ostream_audio->codec;
codec_ctx_2aac->codec_id = ofmt_ctx_aac->oformat->audio_codec;
codec_ctx_2aac->codec_type = AVMEDIA_TYPE_AUDIO;
codec_ctx_2aac->sample_fmt = AV_SAMPLE_FMT_FLTP;
// 音频编解码只能使用此格式?
codec_ctx_2aac->sample_rate = 44100;
codec_ctx_2aac->bit_rate = 64000;
codec_ctx_2aac->channel_layout = AV_CH_LAYOUT_MONO;
// 
codec_ctx_2aac->channels = av_get_channel_layout_nb_channels(codec_ctx_2aac->channel_layout);

codec_2aac = avcodec_find_encoder(codec_ctx_2aac->codec_id);
if (avcodec_open2(codec_ctx_2aac, codec_2aac, NULL) < 0)
{
return -1;
}

if (avformat_write_header(ofmt_ctx_h264, NULL) < 0)
{
return -1;
}

if (avformat_write_header(ofmt_ctx_aac, NULL) < 0)
{
return -1;
}

// video
int video_width_in = 1920;
int video_height_in = 1080;
AVPixelFormat video_format_in = AV_PIX_FMT_BGR24;

video_buffer_size_in = avpicture_get_size(video_format_in, video_width_in, video_height_in);
video_buffer_in = (uint8_t*)av_malloc(video_buffer_size_in);

frame_video.width = codec_ctx_2h264->width;
frame_video.height = codec_ctx_2h264->height;
frame_video.format = codec_ctx_2h264->pix_fmt;

video_buffer_size_out = avpicture_get_size((AVPixelFormat)frame_video.format, frame_video.width, frame_video.height);
video_buffer_out = (uint8_t*)av_malloc(video_buffer_size_out);
avpicture_fill((AVPicture*)&frame_video, video_buffer_out, (AVPixelFormat)frame_video.format, frame_video.width, frame_video.height);

video_convert_ctx = sws_getContext(video_width_in, video_height_in, video_format_in,
frame_video.width, frame_video.height, (AVPixelFormat)frame_video.format, SWS_BICUBIC, NULL, NULL, NULL);

// audio
int64_t audio_channel_layout_in = AV_CH_LAYOUT_STEREO;
int audio_sample_rate_in = 44100;
int audio_nb_samples_in = 1024;
AVSampleFormat audio_sample_fmt_in = AV_SAMPLE_FMT_S16;
int audio_channels_in = av_get_channel_layout_nb_channels(audio_channel_layout_in);

audio_buffer_size_in = av_samples_get_buffer_size(NULL, audio_channels_in, audio_nb_samples_in, audio_sample_fmt_in, 1);
audio_buffer_in = (uint8_t*)av_malloc(audio_buffer_size_in);

frame_audio.channel_layout = codec_ctx_2aac->channel_layout;
frame_audio.nb_samples = codec_ctx_2aac->frame_size;
frame_audio.channels = codec_ctx_2aac->channels;
frame_audio.format = codec_ctx_2aac->sample_fmt;
frame_audio.sample_rate = codec_ctx_2aac->sample_rate;

audio_buffer_size_out = av_samples_get_buffer_size(NULL, frame_audio.channels, frame_audio.nb_samples, (AVSampleFormat)frame_audio.format, 1);
audio_buffer_out = (uint8_t*)av_malloc(audio_buffer_size_out);
avcodec_fill_audio_frame(&frame_audio, frame_audio.channels, (AVSampleFormat)frame_audio.format, (const uint8_t*)audio_buffer_out, audio_buffer_size_out, 1);

audio_convert_ctx = swr_alloc_set_opts(audio_convert_ctx, frame_audio.channel_layout, (AVSampleFormat)frame_audio.format, frame_audio.sample_rate,
audio_channel_layout_in, audio_sample_fmt_in, audio_sample_rate_in, 0, NULL);
swr_init(audio_convert_ctx);

av_init_packet(&pkt_audio);
pkt_audio.data = NULL;
pkt_audio.size = 0;

av_init_packet(&pkt_video);
pkt_video.data = NULL;
pkt_video.size = 0;

FILE* input_pcm_after = fopen("input_audio_s16_stereo.pcm", "rb");
FILE* input_bgr = fopen("input_video.bgr", "rb");

while (1)
{
if (fread(video_buffer_in, video_buffer_size_in, 1, input_bgr) > 0)
{
static int videocnt = 0;
printf("videocnt %d\n", videocnt++);

int video_stride_in = video_width_in * 3;
sws_scale(video_convert_ctx, (const uint8_t* const*)&video_buffer_in, &video_stride_in, 0, video_height_in, frame_video.data, frame_video.linesize);

int got_picture = 0;
//frame_yuv.pts = videocnt;
if (avcodec_encode_video2(codec_ctx_2h264, &pkt_video, &frame_video, &got_picture) < 0)
{
return -1;
}
if (got_picture == 1)
{
pkt_video.stream_index = 0;
av_write_frame(ofmt_ctx_h264, &pkt_video);
av_free_packet(&pkt_video);
}
}

if (fread(audio_buffer_in, audio_buffer_size_in, 1, input_pcm_after) > 0)
{
static int audiocnt = 0;
printf("audiocnt %d\n", audiocnt++);

swr_convert(audio_convert_ctx, &audio_buffer_out, MAX_AUDIO_FRAME_SIZE, (const uint8_t**)&audio_buffer_in, audio_nb_samples_in);

frame_audio.data[0] = audio_buffer_out;
frame_audio.pts = audiocnt;

int got_frame = 0; 
if (avcodec_encode_audio2(codec_ctx_2aac, &pkt_audio, &frame_audio, &got_frame) < 0)
{
return -1;
}
if (got_frame == 1)
{
pkt_audio.stream_index = 0;
av_write_frame(ofmt_ctx_aac, &pkt_audio);
av_free_packet(&pkt_audio);
}
}
}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: