【问题标题】:Parsing FFMpeg AVPackets into h264 nal units将 FFMpeg AVPackets 解析为 h264 最终单元
【发布时间】:2014-01-22 00:26:50
【问题描述】:

我正在使用 FFMpeg 解码实时视频并使用 Live555 进行流式传输。我能够解码视频并获取输出 AVPackets。
1.使用FFMpeg的SWScale将BGR Image转为YUV422P格式

// initilize a BGR To RGB converter using FFMpeg
ctx = sws_getContext(codecContext->width, codecContext->height, AV_PIX_FMT_BGR24, codecContext->width, codecContext->height, AV_PIX_FMT_YUV422P, SWS_BICUBIC, 0, 0, 0);
tempFrame = av_frame_alloc();
int num_bytes = avpicture_get_size(PIX_FMT_BGR24, codecContext->width, codecContext->height);
uint8_t* frame2_buffer = (uint8_t*)av_malloc(num_bytes*sizeof(uint8_t));
avpicture_fill((AVPicture*)tempFrame, frame2_buffer, PIX_FMT_BGR24, codecContext->width, codecContext->height);

// inside the loop of where frames are being encoded where rawFrame is a BGR image
tempFrame->data[0] = reinterpret_cast<uint8_t*>(rawFrame->_data);
sws_scale(ctx, tempFrame->data, tempFrame->linesize, 0, frame->height, frame->data, frame->linesize);

用于解码每一帧

ret = avcodec_encode_video2(codecContext, &packet, frame, &got_output);
if(ret < 0)
{
    fprintf(stderr, "Error in encoding frame\n");
    exit(1);
}

if(got_output)
{
    //printf("Received frame! pushing to queue\n");
    OutputFrame *outFrame = new OutputFrame();
    outFrame->_data = packet.buf->data;
    outFrame->_bufferSize = packet.buf->size;
    outputQueue.push_back(outFrame);
}

到这里它工作正常。我能够将这些帧写入文件并使用 VLC 播放。在此之后,我必须将输出帧传递给 Live555。我认为我到达这里的 AVPackets 不需要是 Live555 所需的单个 H264 Nal 单元。

如何将 AVPacket 分解为 Nal 单元,然后传递给 Live555?

【问题讨论】:

    标签: ffmpeg h.264 rtsp


    【解决方案1】:

    H264VideoStreamDiscreateFramer 需要没有起始代码“\x00\x00\x00\x01”的数据。 需要删除 LiveDeviceSource 中的前 4 个字节或插入 FramedFilter 来完成这项工作。

    也许您可以尝试使用 H264VideoStreamFramer,例如 testH264VideoStreamer 测试程序。

    如果有帮助,您可以找到我的一项尝试,使用 live555 从 V4L2 捕获 https://github.com/mpromonet/h264_v4l2_rtspserver 实现 RTSP 服务器提要

    【讨论】:

    • 不幸的是,我的 V4L2 Capture 不提供 h264 数据,而是提供原始 YUV 数据,因此我必须使用编码器。我认为我的 Live555 设置很好,但问题出在 ffmpeg 编码数据中,它在 AVPacket 中提供比特流而不是 h264 Nal 单位。如果我将 h264 最终单元传递给 Live555,我认为它会起作用。我可以配置 ffmpeg (AVCodecContext) 给我 H264 Nal 单位吗?如果你做过这样的事情,请告诉我..
    • 考虑到 testH264VideoStreamer 流 H264 基本流(包含起始代码),我认为 H264VideoStreamFramer 可以在您的情况下工作。否则,使用 H264VideoStreamDiscreateFramer 您需要删除 LiveDeviceSource 中的起始码
      阅读 ffmpeg libavcodec/libx264.c 和 X264 encoder/encoder.c 和 common/bitstream.c 显示编码帧以起始码或 4 字节大小开头,具体取决于b_annexb 标志。
    猜你喜欢
    • 2011-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-24
    • 2011-08-29
    • 1970-01-01
    • 2015-09-03
    • 2011-08-26
    相关资源
    最近更新 更多