c++ - 使用 ffmpeg 将图像写入 RTP

标签 c++ ffmpeg libav

我实际上是在尝试通过网络有效地发送实时图像。为此,我认为视频流中的 RTP 协议(protocol)是实现这一目标的好方法。

我真的试过这个:

    extern "C"
{
    #include <libavcodec/avcodec.h>
    #include <libavformat/avformat.h>
    #include <libswscale/swscale.h>

    #include <libavutil/opt.h>
    #include <libavutil/channel_layout.h>
    #include <libavutil/common.h>
    #include <libavutil/imgutils.h>
    #include <libavutil/mathematics.h>
    #include <libavutil/samplefmt.h>
}
#include <iostream>
#include <unistd.h>
#include <stdio.h>

//Mainly based on https://stackoverflow.com/questions/40825300/ffmpeg-create-rtp-stream
int main()
{
    //Init ffmpeg
    avcodec_register_all();
    av_register_all();
    avformat_network_init();

    //Init the codec used to encode our given image
    AVCodecID codecID = AV_CODEC_ID_MPEG4;
    AVCodec* codec;
    AVCodecContext* codecCtx;

    codec    = avcodec_find_encoder(codecID);
    codecCtx = avcodec_alloc_context3(codec);

    //codecCtx->bit_rate      = 400000;
    codecCtx->width         = 352;
    codecCtx->height        = 288;

    codecCtx->time_base.num = 1;
    codecCtx->time_base.den = 25;
    codecCtx->gop_size      = 25;
    codecCtx->max_b_frames  = 1;
    codecCtx->pix_fmt       = AV_PIX_FMT_YUV420P;
    codecCtx->codec_type    = AVMEDIA_TYPE_VIDEO;

    if (codecID == AV_CODEC_ID_H264) 
    {
        av_opt_set(codecCtx->priv_data, "preset", "ultrafast", 0);
        av_opt_set(codecCtx->priv_data, "tune", "zerolatency", 0);
    }

    avcodec_open2(codecCtx, codec, NULL);

    //Init the Frame containing our raw data
    AVFrame* frame;

    frame         = av_frame_alloc();
    frame->format = codecCtx->pix_fmt;
    frame->width  = codecCtx->width;
    frame->height = codecCtx->height;
    av_image_alloc(frame->data, frame->linesize, frame->width, frame->height, codecCtx->pix_fmt, 32);

    //Init the format context
    AVFormatContext* fmtCtx  = avformat_alloc_context();
    AVOutputFormat*  format  = av_guess_format("rtp", NULL, NULL);
    avformat_alloc_output_context2(&fmtCtx, format, format->name, "rtp://127.0.0.1:49990");

    avio_open(&fmtCtx->pb, fmtCtx->filename, AVIO_FLAG_WRITE);

    //Configure the AVStream for the output format context
    struct AVStream* stream      = avformat_new_stream(fmtCtx, codec);

    avcodec_parameters_from_context(stream->codecpar, codecCtx);
    stream->time_base.num        = 1;
    stream->time_base.den        = 25;

    /* Rewrite the header */
    avformat_write_header(fmtCtx, NULL);

    /* Write a file for VLC */
    char buf[200000];
    AVFormatContext *ac[] = { fmtCtx };
    av_sdp_create(ac, 1, buf, 20000);
    printf("sdp:\n%s\n", buf);
    FILE* fsdp = fopen("test.sdp", "w");
    fprintf(fsdp, "%s", buf);
    fclose(fsdp);

    AVPacket pkt;
    int j = 0;
    for(int i = 0; i < 10000; i++) 
    {
        fflush(stdout);
        av_init_packet(&pkt);
        pkt.data = NULL;    // packet data will be allocated by the encoder
        pkt.size = 0;

        int R, G, B;
        R = G = B = i % 255;

        int Y =  0.257 * R + 0.504 * G + 0.098 * B +  16;
        int U = -0.148 * R - 0.291 * G + 0.439 * B + 128;
        int V =  0.439 * R - 0.368 * G - 0.071 * B + 128;

        /* prepare a dummy image */
        /* Y */
        for (int y = 0; y < codecCtx->height; y++) 
            for (int x = 0; x < codecCtx->width; x++) 
                frame->data[0][y * codecCtx->width + x] = Y;

        for (int y = 0; y < codecCtx->height/2; y++)
            for (int x=0; x < codecCtx->width / 2; x++)
            {
                frame->data[1][y * frame->linesize[1] + x] = U;
                frame->data[2][y * frame->linesize[2] + x] = V;
            }

        /* Which frame is it ? */
        frame->pts = i;

        /* Send the frame to the codec */
        avcodec_send_frame(codecCtx, frame);

        /* Use the data in the codec to the AVPacket */
        switch(avcodec_receive_packet(codecCtx, &pkt))
        {
            case AVERROR_EOF:
                printf("Stream EOF\n");
                break;

            case AVERROR(EAGAIN): 
                printf("Stream EAGAIN\n");
                break;

            default:
                printf("Write frame %3d (size=%5d)\n", j++, pkt.size);

                /* Write the data on the packet to the output format  */
                av_interleaved_write_frame(fmtCtx, &pkt);

                /* Reset the packet */
                av_packet_unref(&pkt);
                break;
        }

        usleep(1e6/25);
    }

    // end
    avcodec_send_frame(codecCtx, NULL);

    //Free everything
    av_free(codecCtx);
    av_free(fmtCtx);

    return 0;
}

我可以用 VLC 看到一张图片,但看不到视频(我必须重新加载它才能看到另一张灰度图像)。

有人知道为什么 vlc 不能很好地播放我的视频吗?谢谢!

最佳答案

经过大量研究,我发现了错误:我需要调用

av_packet_rescale_ts(&pkt, codecCtx->time_base, stream->time_base);

在交错之前正确放置数据包。真可惜...

关于c++ - 使用 ffmpeg 将图像写入 RTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46352604/

相关文章:

c++ - 查找表示为链表的 2 个数字之和的最佳方法

C++主循环定时器

ffmpeg - 检测部分帧的场景变化

C++ h264 ffmpeg/libav 编码/解码(无损)问题

c++ - GtkImage 不会更新并且 opencv 不会释放图像

c++ - 如何优化从半精度 float16 到单精度 float32 的转换?

python - 希望在 shell 中使用 ffmpeg 来获取元数据

audio - mplayer 输出 4 个音频 channel 到插孔

c - FFmpeg:av_parser_parse2 做什么?

c - av_find_best_stream 与 avcodec_find_decoder 的解码器返回