FFMpeg分析2:AVInputFormat和AVOutputFormat

  我们先看一下音视频播放器的大概结构(个人想法,不保证正确):1、数据源输入(Input)->2、文件格式解析器(Demux)->3、音视频解码(Decoder)->4、颜色空间转换(仅视频)->5、渲染输出(Render Output)。前一篇介绍的几个struct是数据源输入模块里的内容,哪么这一帖所讲的就是第二个模块即文件格式解析器里用到的内容。

      AVInputFormat、AVOutputFormat与URLProtocol类似,每一种文件类型都有一个AVInputFormat和AVOutputFormat静态对像并通过av_register_input_format和av_register_output_format函数链成一个表,其表头在utils.c:

 

/** head of registered input format linked list */

AVInputFormat *first_iformat = NULL;

/** head of registered output format linked list */

AVOutputFormat *first_oformat = NULL;

 

AVInputFormat和AVOutputFormat的定义分别在avformat.h,代码很长,不贴出来浪费空间了。

当程序运行时,AVInputFormat对像的

 

int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,

                       AVInputFormat *fmt,

                       int buf_size,

                       AVFormatParameters *ap)

{

 

     fmt = av_probe_input_format(pd, 0);//返回该文件的AVInputFormat类型

}

至于AVOutputFormat嘛,下次再说吧,晚安!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现一个 RTSP 服务器需要考虑以下几个方面: 1. RTSP 协议的实现; 2. RTP/RTCP 协议的实现; 3. 视频编码和解码的实现; 4. 网络传输的实现。 其中,RTSP 协议和 RTP/RTCP 协议是 RTSP 服务器的核心,视频编码和解码是 RTSP 服务器的重要功能,而网络传输则是 RTSP 服务器的基础。 下面是一个使用 C++ 和 FFmpeg 实现 RTSP 服务器的示例代码: ``` #include <iostream> #include <string> #include <chrono> #include <thread> #include <cstdlib> #include <cstdio> #include <cstring> #include <cmath> extern "C" { #include <libavformat/avformat.h> #include <libavdevice/avdevice.h> #include <libavcodec/avcodec.h> #include <libswscale/swscale.h> #include <libavutil/imgutils.h> #include <libavutil/time.h> } using namespace std; #define WIDTH 640 #define HEIGHT 480 #define FPS 25 int main(int argc, char* argv[]) { AVFormatContext* pFormatCtx = NULL; AVOutputFormat* fmt = NULL; AVStream* video_st = NULL; AVCodecContext* pCodecCtx = NULL; AVCodec* pCodec = NULL; uint8_t* picture_buf = NULL; AVFrame* picture = NULL; int size; int framecnt = 0; AVPacket pkt; int got_picture = 0; int ret = 0; int i = 0; int err = 0; AVDictionary* options = NULL; AVIOContext* pb = NULL; AVFormatContext* ofmt_ctx = NULL; AVStream* video_out = NULL; AVCodecContext* codec_out = NULL; av_register_all(); avdevice_register_all(); // 1. 打开摄像头 AVInputFormat* ifmt = av_find_input_format("v4l2"); if (avformat_open_input(&pFormatCtx, "/dev/video0", ifmt, NULL) != 0) { cout << "Couldn't open input stream." << endl; return -1; } if (avformat_find_stream_info(pFormatCtx, NULL) < 0) { cout << "Couldn't find stream information." << endl; return -1; } av_dump_format(pFormatCtx, 0, "/dev/video0", 0); // 2. 初始化编码器 fmt = av_guess_format("rtsp", NULL, NULL); if (!fmt) { cout << "Couldn't guess format." << endl; return -1; } pCodec = avcodec_find_encoder(fmt->video_codec); if (!pCodec) { cout << "Couldn't find encoder." << endl; return -1; } video_st = avformat_new_stream(NULL, pCodec); if (!video_st) { cout << "Couldn't create video stream." << endl; return -1; } pCodecCtx = video_st->codec; pCodecCtx->codec_id = fmt->video_codec; pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO; pCodecCtx->bit_rate = 400000; pCodecCtx->width = WIDTH; pCodecCtx->height = HEIGHT; pCodecCtx->time_base.num = 1; pCodecCtx->time_base.den = FPS; pCodecCtx->gop_size = 10; pCodecCtx->max_b_frames = 1; pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P; if (pCodec->id == AV_CODEC_ID_H264) { av_opt_set(pCodecCtx->priv_data, "preset", "slow", 0); } if (avcodec_open2(pCodecCtx, pCodec, &options) < 0) { cout << "Couldn't open codec." << endl; return -1; } av_dump_format(ofmt_ctx, 0, "rtsp://localhost:8554/live.sdp", 1); // 3. 初始化 AVIOContext avformat_alloc_output_context2(&ofmt_ctx, NULL, "rtsp", "rtsp://localhost:8554/live.sdp"); if (!ofmt_ctx) { cout << "Couldn't create output context." << endl; return -1; } pb = avio_alloc_context(NULL, 0, AVIO_FLAG_WRITE, NULL, NULL, NULL, NULL); if (!pb) { cout << "Couldn't create AVIOContext." << endl; return -1; } ofmt_ctx->pb = pb; // 4. 添加输出流 video_out = avformat_new_stream(ofmt_ctx, NULL); if (!video_out) { cout << "Couldn't create output stream." << endl; return -1; } codec_out = video_out->codec; codec_out->codec_id = pCodec->id; codec_out->codec_type = AVMEDIA_TYPE_VIDEO; codec_out->bit_rate = 400000; codec_out->width = WIDTH; codec_out->height = HEIGHT; codec_out->time_base.den = FPS; codec_out->time_base.num = 1; codec_out->pix_fmt = AV_PIX_FMT_YUV420P; if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER) { codec_out->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; } // 5. 打开输出流 err = avio_open(&ofmt_ctx->pb, ofmt_ctx->url, AVIO_FLAG_WRITE); if (err < 0) { av_log(NULL, AV_LOG_ERROR, "Could not open output URL '%s'\n", ofmt_ctx->url); return -1; } err = avformat_write_header(ofmt_ctx, &options); if (err < 0) { av_log(NULL, AV_LOG_ERROR, "Error occurred when opening output URL\n"); return -1; } // 6. 开始编码和发送数据 picture = av_frame_alloc(); size = avpicture_get_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height); picture_buf = (uint8_t*)av_malloc(size); avpicture_fill((AVPicture*)picture, picture_buf, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height); av_new_packet(&pkt, size); while (true) { if (av_read_frame(pFormatCtx, &pkt) >= 0) { if (pkt.stream_index == 0) { // 解码 avcodec_decode_video2(pCodecCtx, picture, &got_picture, &pkt); if (got_picture) { // 编码 picture->pts = av_rescale_q(framecnt, pCodecCtx->time_base, video_st->time_base); ret = avcodec_encode_video2(pCodecCtx, &pkt, picture, &got_picture); if (ret >= 0 && got_picture) { pkt.stream_index = video_st->index; av_packet_rescale_ts(&pkt, pCodecCtx->time_base, video_st->time_base); pkt.pos = -1; err = av_interleaved_write_frame(ofmt_ctx, &pkt); if (err < 0) { av_log(NULL, AV_LOG_ERROR, "Error muxing packet\n"); break; } cout << "Write frame " << framecnt << endl; framecnt++; } av_packet_unref(&pkt); } } } else { av_seek_frame(pFormatCtx, 0, 0, AVSEEK_FLAG_FRAME); } this_thread::sleep_for(chrono::milliseconds(40)); } av_write_trailer(ofmt_ctx); avcodec_close(pCodecCtx); av_free(pCodecCtx); av_free(picture_buf); av_free(picture); avio_close(ofmt_ctx->pb); avformat_free_context(ofmt_ctx); avformat_close_input(&pFormatCtx); return 0; } ``` 在上面的代码中,我们使用 FFmpeg 库打开摄像头,并初始化了一个 RTSP 服务器,将摄像头采集到的视频编码后发送给客户端。 在代码中,我们首先打开摄像头,然后初始化编码器。接下来,我们初始化 AVIOContext,添加输出流并打开输出流。最后,我们开始编码和发送数据。 编译代码时需要链接 FFmpeg 库: ``` g++ -o rtsp_server rtsp_server.cpp -lavdevice -lavformat -lavcodec -lavutil -lswscale -lpthread ``` 启动代码后,可以使用 VLC 等 RTSP 客户端访问 RTSP 服务器: ``` vlc rtsp://localhost:8554/live.sdp ``` 这样就可以在客户端上观看摄像头采集到的视频了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值