FFmpeg视频编码 YUV420P编码H264

FFmpeg视频编码 YUV420P编码H264,第1张

//第一步:注册组件->编码、解码器等等…

    av_register_all()

    //第二步:初始化封装格式上下文->视频编码->处理为视频压缩数据格式

    AVFormatContext *avformat_context = avformat_alloc_context()

    //注意事项:FFmepg程序推测输出文件类型->视频压缩数据格式类型

    const char *coutFilePath = [outFilePath UTF8String]

    //得到视频压缩数据格式类型(h264、h265、mpeg2等等...)

    AVOutputFormat *avoutput_format = av_guess_format(NULL, coutFilePath, NULL)

    //指定类型

    avformat_context->oformat = avoutput_format

    //第三步:打开输出文件

    //参数一:输出流

    //参数二:输出文件

    //参数三:权限->输出到文件中

    if (avio_open(&avformat_context->pb, coutFilePath, AVIO_FLAG_WRITE) <0) {

        NSLog(@"打开输出文件失败")

        return

    }

    //第四步:创建输出码流->创建了一块内存空间->并不知道他是什么类型流->希望他是视频流

    AVStream *av_video_stream = avformat_new_stream(avformat_context, NULL)

    //第五步:查找视频编码

    //1、获取编码器上下文

    AVCodecContext *avcodec_context = av_video_stream->codec

    //2、设置编解码器上下文参数->必需设置->不可少

    //目标:设置为是一个视频编码器上下文->指定的是视频编码器

    //上下文种类:视频解码器、视频编码器、音频解码器、音频编码器

    //2.1 设置视频编码器ID

    avcodec_context->codec_id = avoutput_format->video_codec

    //2.2 设置编码器类型->视频编码器

    //视频编码器->AVMEDIA_TYPE_VIDEO

    //音频编码器->AVMEDIA_TYPE_AUDIO

    avcodec_context->codec_type = AVMEDIA_TYPE_VIDEO

    //2.3 设置读取像素数据格式->编码的是像素数据格式->视频像素数据格式->YUV420P(YUV422P、YUV444P等等...)

    //注意:这个类型是根据你解码的时候指定的解码的视频像素数据格式类型

    avcodec_context->pix_fmt = AV_PIX_FMT_YUV420P

    //2.4 设置视频宽高->视频尺寸

    avcodec_context->width = 640

    avcodec_context->height = 352

    //2.5 设置帧率->表示每秒25帧

    //视频信息->帧率 : 25.000 fps

    //f表示:帧数

    //ps表示:时间(单位:每秒)

    avcodec_context->time_base.num = 1

    avcodec_context->time_base.den = 25

    //2.6 设置码率

    //2.6.1 什么是码率?

    //含义:每秒传送的比特(bit)数单位为 bps(Bit Per Second),比特率越高,传送数据速度越快。

    //单位:bps,"b"表示数据量,"ps"表示每秒

    //目的:视频处理->视频码率

    //2.6.2 什么是视频码率?

    //含义:视频码率就是数据传输时单位时间传送的数据位数,一般我们用的单位是kbps即千位每秒

    //视频码率计算如下?

    //基本的算法是:【码率】(kbps)=【视频大小 - 音频大小】(bit位) /【时间】(秒)

    //例如:Test.mov时间 = 24,文件大小(视频+音频) = 1.73MB

    //视频大小 = 1.34MB(文件占比:77%) = 1.34MB * 1024 * 1024 * 8 = 字节大小 = 468365字节 = 468Kbps

    //音频大小 = 376KB(文件占比:21%)

    //计算出来值->码率 : 468Kbps->表示1000,b表示位(bit->位)

    //总结:码率越大,视频越大

    avcodec_context->bit_rate = 468000

    //2.7 设置GOP->影响到视频质量问题->画面组->一组连续画面

    //MPEG格式画面类型:3种类型->分为->I帧、P帧、B帧

    //I帧->内部编码帧->原始帧(原始视频数据)

    //    完整画面->关键帧(必需的有,如果没有I,那么你无法进行编码,解码)

    //    视频第1帧->视频序列中的第一个帧始终都是I帧,因为它是关键帧

    //P帧->向前预测帧->预测前面的一帧类型,处理数据(前面->I帧、B帧)

    //    P帧数据->根据前面的一帧数据->进行处理->得到了P帧

    //B帧->前后预测帧(双向预测帧)->前面一帧和后面一帧

    //    B帧压缩率高,但是对解码性能要求较高。

    //总结:I只需要考虑自己 = 1帧,P帧考虑自己+前面一帧 = 2帧,B帧考虑自己+前后帧 = 3帧

    //    说白了->P帧和B帧是对I帧压缩

    //每250帧,插入1个I帧,I帧越少,视频越小->默认值->视频不一样

    avcodec_context->gop_size = 250

    //2.8 设置量化参数->数学算法(高级算法)->不讲解了

    //总结:量化系数越小,视频越是清晰

    //一般情况下都是默认值,最小量化系数默认值是10,最大量化系数默认值是51

    avcodec_context->qmin = 10

    avcodec_context->qmax = 51

    //2.9 设置b帧最大值->设置不需要B帧

    avcodec_context->max_b_frames = 0

    //第二点:查找编码器->h264

    //找不到编码器->h264

    //重要原因是因为:编译库没有依赖x264库(默认情况下FFmpeg没有编译进行h264库)

    //第一步:编译h264库

    AVCodec *avcodec = avcodec_find_encoder(avcodec_context->codec_id)

    if (avcodec == NULL) {

        NSLog(@"找不到编码器")

        return

    }

    NSLog(@"编码器名称为:%s", avcodec->name)

    //第六步:打开h264编码器

    //缺少优化步骤?

    //编码延时问题

    //编码选项->编码设置

    AVDictionary *param = 0

    if (avcodec_context->codec_id == AV_CODEC_ID_H264) {

        //需要查看x264源码->x264.c文件

        //第一个值:预备参数

        //key: preset

        //value: slow->慢

        //value: superfast->超快

        av_dict_set(¶m, "preset", "slow", 0)

        //第二个值:调优

        //key: tune->调优

        //value: zerolatency->零延迟

        av_dict_set(¶m, "tune", "zerolatency", 0)

    }

    if (avcodec_open2(avcodec_context, avcodec, ¶m) <0) {

        NSLog(@"打开编码器失败")

        return

    }

    //第七步:写入文件头信息

    avformat_write_header(avformat_context, NULL)

    //第8步:循环编码yuv文件->视频像素数据(yuv格式)->编码->视频压缩数据(h264格式)

    //8.1 定义一个缓冲区

    //作用:缓存一帧视频像素数据

    //8.1.1 获取缓冲区大小

    int buffer_size = av_image_get_buffer_size(avcodec_context->pix_fmt,

                                               avcodec_context->width,

                                               avcodec_context->height,

                                               1)

    //8.1.2 创建一个缓冲区

    int y_size = avcodec_context->width * avcodec_context->height

    uint8_t *out_buffer = (uint8_t *) av_malloc(buffer_size)

    //8.1.3 打开输入文件

    const char *cinFilePath = [inFilePath UTF8String]

    FILE *in_file = fopen(cinFilePath, "rb")

    if (in_file == NULL) {

        NSLog(@"文件不存在")

        return

    }

    //8.2.1 开辟一块内存空间->av_frame_alloc

    //开辟了一块内存空间

    AVFrame *av_frame = av_frame_alloc()

    //8.2.2 设置缓冲区和AVFrame类型保持一直->填充数据

    av_image_fill_arrays(av_frame->data,

                         av_frame->linesize,

                         out_buffer,

                         avcodec_context->pix_fmt,

                         avcodec_context->width,

                         avcodec_context->height,

                         1)

    int i = 0

    //9.2 接收一帧视频像素数据->编码为->视频压缩数据格式

    AVPacket *av_packet = (AVPacket *) av_malloc(buffer_size)

    int result = 0

    int current_frame_index = 1

    while (true) {

        //8.1 从yuv文件里面读取缓冲区

        //读取大小:y_size * 3 / 2

        if (fread(out_buffer, 1, y_size * 3 / 2, in_file) <= 0) {

            NSLog(@"读取完毕...")

            break

        }else if (feof(in_file)) {

            break

        }

        //8.2 将缓冲区数据->转成AVFrame类型

        //给AVFrame填充数据

        //8.2.3 void * restrict->->转成->AVFrame->ffmpeg数据类型

        //Y值

        av_frame->data[0] = out_buffer

        //U值

        av_frame->data[1] = out_buffer + y_size

        //V值

        av_frame->data[2] = out_buffer + y_size * 5 / 4

        av_frame->pts = i

        //注意时间戳

        i++

        //总结:这样一来我们的AVFrame就有数据了

        //第9步:视频编码处理

        //9.1 发送一帧视频像素数据

        avcodec_send_frame(avcodec_context, av_frame)

        //9.2 接收一帧视频像素数据->编码为->视频压缩数据格式

        result =avcodec_receive_packet(avcodec_context, av_packet)

        //9.3 判定是否编码成功

        if (result == 0) {

            //编码成功

            //第10步:将视频压缩数据->写入到输出文件中->outFilePath

            av_packet->stream_index = av_video_stream->index

            result =av_write_frame(avformat_context, av_packet)

            NSLog(@"当前是第%d帧", current_frame_index)

            current_frame_index++

            //是否输出成功

            if (result <0) {

                NSLog(@"输出一帧数据失败")

                return

            }

        }

    }

    //第11步:写入剩余帧数据->可能没有

    flush_encoder(avformat_context, 0)

    //第12步:写入文件尾部信息

    av_write_trailer(avformat_context)

    //第13步:释放内存

    avcodec_close(avcodec_context)

    av_free(av_frame)

    av_free(out_buffer)

    av_packet_free(&av_packet)

    avio_close(avformat_context->pb)

    avformat_free_context(avformat_context)

    fclose(in_file)

开发环境:

WINDOWS7 32bit

MINGW

eclipse juno cdt

1、首先你要编译好FFMPEG,

a) 方法一:可以去官网下载源码,用MINGW编译(编译时记得支持H264,当然,事先得下载并编译好libx264,视频技术论坛里有很多介绍)

b) 方法二:更加省心省力的方法是,下载别人已经编译好的资源,如ZeranoeFFmpeg的,下载他的dev版本,包含了头文件,链接库等必须的东西,当然,这东西已经是支持H264的了。

2、以下的就是代码部分了:

a) 先声明必要的变量:

AVFormatContext *fmtctx

AVStream *video_st

AVCodec *video_codec

const int FPS = 25/* 25 images/s */

const char *RDIP = “127.0.0.1”

unsigned int RDPORT = 5678

const unsigned int OUTWIDTH = 720

const unsigned int OUTHEIGHT = 480;

av_register_all()

avformat_network_init()

b) 初始化AV容器

fmtctx = avformat_alloc_context()

c) 获得输出格式,这里是RTP网络流

fmtctx->oformat = av_guess_format("rtp", NULL, NULL)

d)打开网络流

snprintf(fmtctx->filename, sizeof(fmtctx->filename),"rtp://%s:%d",RDIP,RDPORT)

avio_open(&fmtctx->pb,fmtctx->filename, AVIO_FLAG_WRITE)

e) 开始添加H264视频流

video_st = NULLvideo_st = add_video_stream(fmtctx, &video_codec, AV_CODEC_ID_H264)

其中,add_video_stream函数为:

add_video_stream(AVFormatContext *oc,AVCodec **codec, enum AVCodecID codec_id)

{

AVCodecContext *c

AVStream *st

/* find the video encoder */

*codec = avcodec_find_encoder(codec_id)

st = avformat_new_stream(oc, *codec)

c = st->codec

avcodec_get_context_defaults3(c, *codec)

c->codec_id = codec_id

c->width = OUTWIDTH

c->height = OUTHEIGHT

c->time_base.den = FPS

c->time_base.num = 1

c->pix_fmt = PIX_FMT_YUV420P

if(oc->oformat->flags &AVFMT_GLOBALHEADER)

c->flags|= CODEC_FLAG_GLOBAL_HEADER

av_opt_set(c->priv_data, "preset", "ultrafast", 0)

av_opt_set(c->priv_data, "tune","stillimage,fastdecode,zerolatency",0)

av_opt_set(c->priv_data, "x264opts","crf=26:vbv-maxrate=728:vbv-bufsize=364:keyint=25",0)return st}

// OPEN THE CODE

avcodec_open2(video_st->codec, video_codec, NULL)

/* Write the stream header, if any. */

avformat_write_header(fmtctx, NULL)

f) 现在,就可以不断的编码数据,并发生数据了

AVFrame* m_pYUVFrame = avcodec_alloc_frame()

while(1) //这里设置成无限循环,你可以设置成250,或其他数进行测试,观看结果

{

fill_yuv_image(m_pYUVFrame, video_st->codec->frame_number,OUTWIDTH, OUTHEIGHT)

/* encode the image */

AVPacket pkt

int got_output = 0

av_init_packet(&pkt)

pkt.data = NULL // packet data will be allocated by the encoder

pkt.size = 0

pkt.pts = AV_NOPTS_VALUE

pkt.dts =AV_NOPTS_VALUE

m_pYUVFrame->pts = video_st->codec->frame_number

ret = avcodec_encode_video2(c, &pkt,frame, &got_output)

if (ret <0) {fprintf(stderr, "Error encoding video frame: %s\n", av_err2str(ret))

exit(1)

}

/* If size is zero, it means the image was buffered. */

if (got_output)

{

if (c->coded_frame->key_frame)pkt.flags |= AV_PKT_FLAG_KEY

pkt.stream_index = st->index

if (pkt.pts != AV_NOPTS_VALUE )

{

pkt.pts = av_rescale_q(pkt.pts,video_st->codec->time_base, video_st->time_base)

}

if(pkt.dts !=AV_NOPTS_VALUE )

{

pkt.dts = av_rescale_q(pkt.dts,video_st->codec->time_base, video_st->time_base)

}

/* Write the compressed frame to the media file. */

ret = av_interleaved_write_frame(oc,&pkt)

}

else {

ret = 0

}

}

g) Fill_yuv_image函数:

/* Prepare a dummy image. */

static void fill_yuv_image(AVPicture *pict,int frame_index,int width, int height)

{

int x, y, i

i = frame_index

/* Y */

for (y = 0y <heighty++)

for (x = 0x <widthx++)

pict->data[0][y * pict->linesize[0] +x] = x + y + i * 3

/* Cb and Cr */

for (y = 0y <height / 2y++)

{

for (x = 0x <width / 2x++)

{

pict->data[1][y * pict->linesize[1] +x] = 128 + y + i * 2

pict->data[2][y * pict->linesize[2] +x] = 64 + x + i * 5

}

}

}

h) 打印sdp信息,仅需一次,打印的sdp信息,用在VLC播放器结束网络视频流时用到

//打印sdp信息

char sdp[2048]

av_sdp_create(&fmtctx,1, sdp, sizeof(sdp))

printf("%s\n",sdp)

fflush(stdout)

i)最后,做一些清理工作

avcodec_free_frame(&m_pYUVFrame)

av_write_trailer(fmtctx)

/* Free the streams. */

for (unsigned int i = 0i<fmtctx->nb_streamsi++)

{

av_freep(&fmtctx->streams->codec)

av_freep(&fmtctx->streams)

}

if(!(fmtctx->oformat->flags&AVFMT_NOFILE))

/* Close the output file. */

avio_close(fmtctx->pb)

/*free the stream */

av_free(fmtctx)

3、编译代码,记得添加库文件,运行一次代码,不用死循环,设置不用循环,因为是要让他打印出sdp文件的信息。得到sdp信息,比如我精简成如下:

c=IN IP4 127.0.0.1

m=video 56782 RTP/AVP 96

a=rtpmap:96 H264/90000

a=framerate:25

a=fmtp:96 packetization-mode=1

把这些信息保存到一个文本文件,并改名为sdp后缀,如mySDP.sdp。

4、从官网下载VLC播放器,重新运行上述的代码,这一次要循环,具体循环多久,你自己决定,这一次是正式测试了。代码跑起来后,把刚刚的sdp文件用VLC打开,直接把sdp文件拖到VLC播放器中就行了。等待缓冲,就可以看到效果了。

5、代码中剩掉了出错检查部分,请自行添加。

6、关于IP地址,这里是127.0.0.1,是供本机测试,可以改成制定的接受数据的电脑IP地址,或者广播地址IP地址。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11791263.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-18
下一篇2023-05-18

发表评论

登录后才能评论

评论列表(0条)

    保存