ffmpeg多线程本地mp4 rtmp推流,h264+aac编码

程序说明:使用了c++11的std线程,锁,信号量等东西,替换了pthread多线程。主要之前用windows下移植的linux发现多线程始终有问题,所以决定用原生的试试。不过现在想来,应该问题还是我队列的设计问题。主要这里有个坑,就是c语言for循环内部的局部变量的内存地址是不变的,所以如果在for循环里面给指针赋值,一定要特别注意。 
这次的程序编写过程中遇到很多坑。malloc生产的avframe,不要用av_frame_free销毁。av_frame_free要和av_frame_alloc对应使用。否则会报错。这挺奇葩的。所以avframe的释放,还有avpacket的释放,最好先不要写,先不要理会内存泄漏,先保证程序运行起来。否则有可能你某个地方写了free,你的encode方法或者decode方法就莫名其妙的崩溃。说莫名其妙,是因为我暂时还不清楚为什么会这样。而且avframe,avpacket,buffer 能够搞全局变量的,尽量不要搞局部变量。 
程序实现了mp4重新编码h264+aac,然后推流。主要要控制推流速度,然后av_read_frame不能过快,之前用双链表实现的队列,弊端就是读的速度要自己再控制。不然内存会很快爆满。所以现在用的循环队列实现,可以保证后期生产者和消费者的消耗速度是1:1。 
还有一些奇怪的问题,现在音频转码用的AVFilter,本来视频也用的AVFilter,但是发现视频的不好使,测试发现某些MP4,运行过程中会av_buffersrc_add_frame_flags会失败,这个暂时不清楚什么原因造成的。如果有知道原因的欢迎私信我。所以视频转码换成了SwsContext实现,其实这个更方便,代码量更小。 
程序现在还有一些问题,就是视频播放初期,音视频会有几帧丢帧。也希望大家能解惑。 
上代码
-

//loopq.h
#include <stdio.h>
#include <malloc.h>
#include <iostream> 
#include <thread>
#include <mutex>
#include <condition_variable>
#include<Windows.h>
using namespace std;

extern "C"{
#include <libavcodec/avcodec.h>

};
typedef struct _Queue Queue;



struct _Queue{

    int size;
    //存放AVFrame*的二维指针
    AVFrame** tab;
    int type;//1表示视频。2表示音频
    int read_index;
    int write_index;
    mutex mtx;
    condition_variable cond_not_full;// 指示产品缓冲区不为满
    condition_variable cond_not_empty;// 指示产品缓冲区不为空

};

void queue_init(Queue *queue, int size,int type);
void queue_push(Queue *queue, AVFrame* frame);
AVFrame* queue_pop(Queue *queue, int finished);

 

//dbt_rtmp.cpp
#include <dbt_rtmp.h>

void init_packet(AVPacket *packet){
    av_init_packet(packet);
    packet->data = NULL;
    packet->size = 0;
}
void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
{
    if (pkt->pts != AV_NOPTS_VALUE)
        pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
    if (pkt->dts != AV_NOPTS_VALUE)
        pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
    if (pkt->duration > 0)
        pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
}


int initAudioFilters(FilteringContext *filter_ctx, AVCodecContext * icodecContext, AVCodecContext *ocodecContext)
{
    char args[512];
    int ret;
    AVFilter *abuffersrc = avfilter_get_by_name("abuffer");
    AVFilter *abuffersink = avfilter_get_by_name("abuffersink");
    AVFilterInOut *outputs = avfilter_inout_alloc();
    AVFilterInOut *inputs = avfilter_inout_alloc();
    AVFilterContext *buffersrc_ctx = NULL;
    AVFilterContext *buffersink_ctx = NULL;

    if (!icodecContext->channel_layout)
        icodecContext->channel_layout = av_get_default_channel_layout(icodecContext->channels);
    //AV_SAMPLE_FMT_FLTP
    static const enum AVSampleFormat out_sample_fmts[] = { ocodecContext->sample_fmt, AV_SAMPLE_FMT_NONE };
    static const int64_t out_channel_layouts[] = { ocodecContext->channel_layout, -1 };
    static const int out_sample_rates[] = { ocodecContext->sample_rate, -1 };


    AVFilterGraph* filter_graph = avfilter_graph_alloc();
    filter_graph->nb_threads = 1;

    sprintf_s(args, sizeof(args),
        "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%I64x",
        icodecContext->time_base.num, icodecContext->time_base.den, icodecContext->sample_rate,
        av_get_sample_fmt_name(icodecContext->sample_fmt), icodecContext->channel_layout);

    ret = avfilter_graph_create_filter(&buffersrc_ctx, abuffersrc, "in",
        args, NULL, filter_graph);
    if (ret < 0) {
        av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer source\n");
        return ret;
    }

    /* buffer audio sink: to terminate the filter chain. */
    ret = avfilter_graph_create_filter(&buffersink_ctx, abuffersink, "out",
        NULL, NULL, filter_graph);
    if (ret < 0) {
        av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer sink\n");
        return ret;
    }

    ret = av_opt_set_int_list(buffersink_ctx, "sample_fmts", out_sample_fmts, -1,
        AV_OPT_SEARCH_CHILDREN);
    if (ret < 0) {
        av_log(NULL, AV_LOG_ERROR, "Cannot set output sample format\n");
        return ret;
    }

    ret = av_opt_set_int_list(buffersink_ctx, "channel_layouts", out_channel_layouts, -1,
        AV_OPT_SEARCH_CHILDREN);
    if (ret < 0) {
        av_log(NULL, AV_LOG_ERROR, "Cannot set output channel layout\n");
        return ret;
    }

    ret = av_opt_set_int_list(buffersink_ctx, "sample_rates", out_sample_rates, -1,
        AV_OPT_SEARCH_CHILDREN);
    if (ret < 0) {
        av_log(NULL, AV_LOG_ERROR, "Cannot set output sample rate\n");
        return ret;
    }

    /* Endpoints for the filter graph. */
    outputs->name = av_strdup("in");
    outputs->filter_ctx = buffersrc_ctx;;
    outputs->pad_idx = 0;
    outputs->next = NULL;

    inputs->name = av_strdup("out");
    inputs->filter_ctx =buffersink_ctx;
    inputs->pad_idx = 0;
    inputs->next = NULL;

    if ((ret = avfilter_graph_parse_ptr(filter_graph, "anull",
        &inputs, &outputs, nullptr)) < 0)
        return ret;

    if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
        return ret;

    av_buffersink_set_frame_size(buffersink_ctx, 1024);
    /* Fill FilteringContext */
    filter_ctx->buffersrc_ctx = buffersrc_ctx;
    filter_ctx->buffersink_ctx = buffersink_ctx;
    filter_ctx->filter_graph = filter_graph;
    return 0;
}


void add_stream(AVFormatContext *out_format_context, AVStream** st, AVCodecContext **out_codec_context, AVCodec** codec, AVCodecID codec_id)
{
    *codec = avcodec_find_encoder(codec_id);
    if (!codec) {
        fprintf(stderr, "Could not find encoder for '%s'\n",
            avcodec_get_name(codec_id));
        getchar();exit(1);
    }

    *st = avformat_new_stream(out_format_context, *codec);

    if (!*st) {
        fprintf(stderr, "Could not alloc stream");
        getchar();exit(1);
    }

    *out_codec_context = (*st)->codec;
    (*st)->id = out_format_context->nb_streams - 1;
    (*out_codec_context)->codec_id = codec_id;
    AVRational time_base;
    switch ((*codec)->type) {
    case AVMEDIA_TYPE_AUDIO:

        (*out_codec_context)->codec_type = AVMEDIA_TYPE_AUDIO;

        (*out_codec_context)->channels = dst_channels;
        (*out_codec_context)->channel_layout = av_get_default_channel_layout(dst_channels);

        (*out_codec_context)->sample_rate = ist_a->codec->sample_rate;
        (*out_codec_context)->frame_size = audio_frame_size;
        (*out_codec_context)->sample_fmt = (*codec)->sample_fmts[0];

        time_base = { 1, (*out_codec_context)->sample_rate };
        (*out_codec_context)->time_base = time_base;

        break;
    case AVMEDIA_TYPE_VIDEO:
        (*out_codec_context)->codec_type = AVMEDIA_TYPE_VIDEO;

        (*out_codec_context)->time_base = ist_v->codec->time_base;

        (*out_codec_context)->pix_fmt = (*codec)->pix_fmts[0];
        (*out_codec_context)->width = width;
        (*out_codec_context)->height = height;

        (*out_codec_context)->me_range = 0;
        (*out_codec_context)->max_qdiff = 4;
        (*out_codec_context)->qmin = ist_v->codec->qmin;
        (*out_codec_context)->qmax = ist_v->codec->qmax;
        (*out_codec_context)->qcompress = 0.6;

        break;
    default:
        break;
    }
    (*out_codec_context)->codec_tag = 0;

    // some formats want stream headers to be separate
    if (out_format_context->oformat->flags & AVFMT_GLOBALHEADER)
        (*out_codec_context)->flags |= CODEC_FLAG_GLOBAL_HEADER;


}


void open_video(AVCodecContext* codec_context, AVCodec* codec){


    AVDictionary *param = NULL;

    av_dict_set(&param, "preset", "veryfast", 0);
    av_dict_set(&param, "tune", "zerolatency", 0);

    if (avcodec_open2(video_st->codec, codec, &param) < 0) {
        fprintf(stderr, "could not open codec\n");
        getchar(); exit(1);
    }

}

void open_audio(AVCodecContext* audio_codec_context, AVCodec * codec){

    if (avcodec_open2(audio_codec_context, codec, NULL) < 0) {
        printf("Could not open audio codec \n");
        getchar();exit(1);
    }
}


int encode_video_frame(AVFrame *frame,
    AVFormatContext *out_format_context,
    AVStream *video_st){

    AVCodecContext* out_codec_context = video_st->codec;
    int got_packet;



    if (frame){
        //decodec层时间基到codec的时间基转换
        frame->pts = av_rescale_q(frame->pts, ist_v->codec->time_base, video_st->codec->time_base);
        frame->pict_type = AV_PICTURE_TYPE_NONE;
    }

    int ret = avcodec_encode_video2(out_codec_context, &venc_pkt,
        frame, &got_packet);

    if (ret < 0 || !got_packet){ //在flush的时候,如果失败 ,说明丢失帧(缓存帧)已经空了

        return 1;
    }

    //codec层时间基转mux层
    av_packet_rescale_ts(&venc_pkt, video_st->codec->time_base, video_st->time_base);

    venc_pkt.stream_index = video_st->index;

    printf("video info--- enc_pkt:pts:%lld\t dts:%lld\t duration:%d\n", venc_pkt.pts, venc_pkt.dts, venc_pkt.duration);

    last_video_pts = venc_pkt.pts;
    ret = av_interleaved_write_frame(out_format_context, &venc_pkt);



    if (ret < 0){
        printf("write video frame failed!\n");
        return 1;
    }
    else{

        printf("write video frame success\t%d\n", videoframecnt);

        videoframecnt++;
    }

    return 0;
}


int encode_audio_frame(AVFrame *frame, int nbsamples,
    AVFormatContext *output_format_context, AVStream* st){
    int got_packet;

    if (frame){
        //decodec层时间基到codec的时间基转换
        frame->pts = av_rescale_q(frame->pts, ist_a->codec->time_base, audio_st->codec->time_base);

    }

    int ret = avcodec_encode_audio2(st->codec, &aenc_pkt,
        frame, &got_packet);

    if (ret < 0 || !got_packet){

        return 1;
    }

    av_packet_rescale_ts(&aenc_pkt, audio_st->codec->time_base, audio_st->time_base);

    aenc_pkt.stream_index = audio_st->index;

    last_audio_pts = aenc_pkt.pts;



    //printf("audio info--- enc_pkt:pts:%lld\t dts:%lld\t duration:%d\n", aenc_pkt.pts, aenc_pkt.dts, aenc_pkt.duration);

    av_bitstream_filter_filter(m_aac_adtstoasc, audio_st->codec, NULL, &aenc_pkt.data, &aenc_pkt.size, aenc_pkt.data, aenc_pkt.size, 0);


    ret = av_interleaved_write_frame(output_format_context, &aenc_pkt);



    if (ret < 0){
        printf("write audio frame failed!\n");
        return 1;
    }
    else{
        audioframecnt++;
    //  printf("write audio frame success!\t%d\n", audioframecnt);

    }

    return 0;
}



void decode_video_frame(AVPacket *pkt){


    av_packet_rescale_ts(pkt, ist_v->time_base, ist_v->codec->time_base);

    int got_picture;
    avcodec_decode_video2(ist_v->codec, videosrcFrame, &got_picture, pkt);
    printf("解析视频called,got_picture:%d\n", got_picture);
    if (got_picture){

        videosrcFrame->pts = av_frame_get_best_effort_timestamp(videosrcFrame);

        avpicture_fill((AVPicture *)video_filt_frame, video_data_buffer, video_codec_context->pix_fmt, video_codec_context->width, video_codec_context->height);

        sws_scale(sws_ctx, videosrcFrame->data, videosrcFrame->linesize, 0, height, video_filt_frame->data, video_filt_frame->linesize);
        video_filt_frame->pts = videosrcFrame->pts;
        queue_push(&videoq, video_filt_frame);
        printf("放入了视频\n");
    }

}


void decode_audio_frame(AVPacket *pkt){


    //demux层转decode层时间基
    av_packet_rescale_ts(pkt, ist_a->time_base, ist_a->codec->time_base);

    int got_frame;


    avcodec_decode_audio4(ist_a->codec, audiosrcFrame, &got_frame, pkt);
    printf("解析音频called,got_frame:%d\n", got_frame);
    if (got_frame){
        audiosrcFrame->pts = av_frame_get_best_effort_timestamp(audiosrcFrame);

        if (av_buffersrc_add_frame_flags(filter_ctx[ia_index].buffersrc_ctx,
            audiosrcFrame, AV_BUFFERSRC_FLAG_PUSH) >= 0){

            while (1) {

                int ret = av_buffersink_get_frame_flags(filter_ctx[ia_index].buffersink_ctx,
                    audio_filt_frame, AV_BUFFERSINK_FLAG_NO_REQUEST);

                if (ret < 0) {
                    if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
                        printf("audio Error in av_buffersink_get_frame_flags\n");
                    }
                    break;
                }
                queue_push(&audioq, audio_filt_frame);

            }

        }
        else{
            printf("audio av_buffersrc_add_frame_flags failed");

        }

    }

}


void read_decode_thread(int arg){

    int ret;
    AVPacket pkt;
    init_packet(&pkt);
    for (;;){

        ret = av_read_frame(ifmt_ctx, &pkt);
        if (ret < 0){
            printf("到文件尾了\n");
            finished = 1;
            break;
        }

        if (pkt.stream_index == iv_index){
            printf("开始解析视频\n");
            decode_video_frame(&pkt);

        }
        else if (pkt.stream_index == ia_index){
            printf("开始解析音频\n");
            decode_audio_frame(&pkt);
        }

    }
    av_free_packet(&pkt);
}

void encode_thread(int arg){


    AVFrame *pframe = av_frame_alloc();
    AVFrame* frame = NULL;
    for (;;){

        if (av_compare_ts(last_audio_pts, audio_st->time_base, last_video_pts, video_st->time_base) <= 0){
            printf("现在去取音频\n");
            frame = queue_pop(&audioq, finished);

            if (frame == NULL){
                printf("audio queue_pop exit\n");
                break;
            }

            printf("audio frame----%x,pts:%d\n", &frame, frame->pts);
            double delay = (double)out_framesize / audio_st->codec->sample_rate;

            audio_clock += delay; 
            double audiodiff = audio_clock - (double)av_gettime()/1000000;
            if (audiodiff > 0){
                Sleep(audiodiff*1000);
            }
            *pframe = *frame;
            encode_audio_frame(frame, out_framesize, ofmt_ctx, audio_st);

        }
        else{
            printf("现在去取视频\n");
            frame = queue_pop(&videoq, finished);
            if (frame == NULL){
                printf("video queue_pop exit\n");
                break;
            }

            printf("video frame----%x,pts:%d\n", &frame, frame->pts);
            double ptstime = last_video_pts*av_q2d(video_st->time_base);
            double videodiff = start_time+ptstime - audio_clock;
            if (videodiff > 0){
                Sleep(videodiff * 1000);
            }
            *pframe = *frame;
            encode_video_frame(pframe, ofmt_ctx, video_st);

        }

    }
    av_frame_free(&pframe);

}



int main(int argc, char **argv){

    av_register_all();
    avformat_network_init();
    avfilter_register_all();
    int ret;
    AVDictionary* in_options = NULL;
    //av_dict_set(&in_options, "re", "1", 0);
    if ((ret = avformat_open_input(&ifmt_ctx, INPUTURL, 0, &in_options)) < 0) {
        printf("Could not open input file.");
    }

    avformat_find_stream_info(ifmt_ctx, 0);

    for (int i = 0; i < ifmt_ctx->nb_streams; i++) {

        if (ifmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO){
            iv_index = i;

            ist_v = ifmt_ctx->streams[i];
            width = ist_v->codec->width;
            height = ist_v->codec->height;

            AVCodec *codec = avcodec_find_decoder(ist_v->codec->codec_id);

            /* open the codec */
            if (avcodec_open2(ist_v->codec, codec,NULL) < 0) {
                fprintf(stderr, "could not open input video decoder codec\n");
                getchar();exit(1);
            }

        }
        else if (ifmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO){
            ia_index = i;
            ist_a = ifmt_ctx->streams[i];

            AVCodec *codec = avcodec_find_decoder(ist_a->codec->codec_id);

            /* open the codec */
            if (avcodec_open2(ist_a->codec, codec, NULL) < 0) {
                fprintf(stderr, "could not open input audio decoder codec\n");
                getchar(); exit(1);
            }
        }

    }
    av_dump_format(ifmt_ctx, 0, INPUTURL, 0);


    ofmt_ctx = avformat_alloc_context();
    if (strstr(OUTPUTURL, "rtmp")){
        avformat_alloc_output_context2(&ofmt_ctx, NULL, "flv", OUTPUTURL); //RTMP
    }
    else{
        avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, OUTPUTURL);
    }

    AVOutputFormat* fmt = ofmt_ctx->oformat;
    if (!fmt) {
        fprintf(stderr, "Could not find suitable output format");
        getchar();exit(1);
    }


    add_stream(ofmt_ctx, &video_st, &video_codec_context, &video_codec, video_codecID);
    add_stream(ofmt_ctx, &audio_st, &audio_codec_context, &audio_codec, audio_codecID);

    open_video(video_codec_context, video_codec);
    open_audio(audio_codec_context, audio_codec);

    av_dump_format(ofmt_ctx, 0, OUTPUTURL, 1);

    filter_ctx = (FilteringContext *)av_malloc_array(ifmt_ctx->nb_streams, sizeof(*filter_ctx));

    initAudioFilters(&filter_ctx[ia_index], ist_a->codec, audio_st->codec);
    sws_ctx = sws_getContext(
        width, height, ist_v->codec->pix_fmt,
        width, height, video_codec_context->pix_fmt,
        SWS_BICUBIC, NULL, NULL, NULL);
    video_data_buffer = new uint8_t[avpicture_get_size(video_codec_context->pix_fmt, video_codec_context->width, video_codec_context->height)];

    /* open the output file, if needed */
    if (!(fmt->flags & AVFMT_NOFILE)) {
        ret = avio_open(&ofmt_ctx->pb, OUTPUTURL, AVIO_FLAG_WRITE);
        if (ret < 0) {
            fprintf(stderr, "Could not open '%s': %s\n", OUTPUTURL,
                "");
            return 1;
        }
    }
    init_packet(&venc_pkt);
    video_filt_frame = av_frame_alloc();
    videosrcFrame = av_frame_alloc();

    init_packet(&aenc_pkt);
    audio_filt_frame = av_frame_alloc();
    audiosrcFrame = av_frame_alloc();

    queue_init(&audioq,50,2);
    queue_init(&videoq,50,1);
    m_aac_adtstoasc = av_bitstream_filter_init("aac_adtstoasc");
    /*AVDictionary *out_options = NULL;
    av_dict_set(&out_options, "rtmp_buffer", "1024000", 0);
    av_dict_set(&out_options, "max_delay", "500000", 0);
    av_dict_set(&out_options, "timeout", "6", 0);*/
    ret = avformat_write_header(ofmt_ctx, NULL);
    if (ret < 0) {
        fprintf(stderr, "Error occurred when opening output file: %s\n", "");
        return 1;
    }

    out_framesize = audio_codec_context->frame_size;
    audio_clock = start_time=(double)av_gettime() / 1000000;
    thread t1(read_decode_thread, NULL);
    thread t2(encode_thread, NULL);

    t1.join();
    t2.join();


    if (video_codec_context->codec->capabilities &CODEC_CAP_DELAY){
        while (!encode_video_frame(NULL, ofmt_ctx, video_st)){ 
            printf("encode_video_frame while");
            ;
        }
    }
    if (audio_codec_context->codec->capabilities &CODEC_CAP_DELAY){
        while (!encode_audio_frame(NULL, out_framesize, ofmt_ctx,audio_st)){ 
            printf("encode_audio_frame while");
            ;
        }
    }

    av_write_trailer(ofmt_ctx);

    av_bitstream_filter_close(m_aac_adtstoasc);
    avformat_close_input(&ifmt_ctx);
    av_free_packet(&venc_pkt);
    av_frame_free(&video_filt_frame);
    av_frame_free(&videosrcFrame);
    sws_freeContext(sws_ctx);
    if (ofmt_ctx) {
        avio_closep(&ofmt_ctx->pb);
        avformat_free_context(ofmt_ctx);
    }

    printf("程序运行end");
    return getchar();
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值