c++ - FFMpeg C Lib - 转置导致图像损坏

标签 c++ video ffmpeg

我正在尝试使用 ffmpeg C lib 设置转码管道,但如果我对其进行转码,则视频已损坏,如下所示。

如果我不转置,则视频很好,即管道的其余部分已正确设置。

我需要将 AVFrame 转换为另一种数据类型才能与其他软件一起使用。我相信腐败发生在拷贝上,但我不知道为什么。可能与旋转 YUV420P 像素有关?

video is rotated but corrupted

构造函数(代码采用 from here )

MyFilter::MyFilter(const std::string filter_desc, AVCodecContext *data_ctx){
  avfilter_register_all();
  buffersrc_ctx = NULL;
  buffersink_ctx = NULL;

    filter_graph = avfilter_graph_alloc();

  AVFilter *buffersink = avfilter_get_by_name("buffersink");
  if (!buffersink) {
    throw error("filtering sink element not found\n");
  }

  if (avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out", NULL, NULL, filter_graph) < 0) {
    throw error("Cannot create buffer sink\n");
  }


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

    AVFilter *buffersrc  = avfilter_get_by_name("buffer");
    if (!buffersrc) {
        throw error("filtering source element not found\n");
    }

    char args[512];
    snprintf(args, sizeof(args), "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
                    data_ctx->width, data_ctx->height, data_ctx->pix_fmt,
                    data_ctx->time_base.num, data_ctx->time_base.den,
                    data_ctx->sample_aspect_ratio.num, data_ctx->sample_aspect_ratio.den);

    log(Info, "Setting filter input with %s", args);


    if (avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in", args, NULL, filter_graph) < 0) {
         throw error("Cannot create buffer source\n");
    }

    filterOutputs = avfilter_inout_alloc();
    filterOutputs->name       = av_strdup("in");
    filterOutputs->filter_ctx = buffersrc_ctx;
    filterOutputs->pad_idx    = 0;
    filterOutputs->next       = NULL;

    if ((avfilter_graph_parse(filter_graph, filter_desc.c_str(), filterInputs, filterOutputs, NULL)) < 0)
            log(Warning,"Could not parse input filters");

    if ((avfilter_graph_config(filter_graph, NULL)) < 0)
        log(Warning,"Could not configure filter graph");

}

以及过程
AVFrame * MyFilter::process(AVFrame *inFrame){

    if (av_buffersrc_add_frame_flags(buffersrc_ctx, inFrame->get(), AV_BUFFERSRC_FLAG_PUSH | AV_BUFFERSRC_FLAG_KEEP_REF ) < 0) {
         throw error("Error while feeding the filtergraph\n");
     }

    int i = 0;
    AVFrame* outFrame =  av_frame_alloc();
    if( av_buffersink_get_frame(buffersink_ctx, outFrame) < 0 ){
     throw error("Couldnt find a frame\n");
    }
  return outFrame;
}

我使用的过滤器是:
 std::string filter_desc = "transpose=cclock"

作为一个额外的说明,似乎顶部栏(在上面的屏幕截图中可见)实际上是由正确旋转的像素组成的,这适用于整个视频。它只是降低了剩余 99% 的像素。

使用这个作品:std::string filter_desc = "rotate=PI/2" ,但是分辨率没有正确移动。如果我尝试std::string filter_desc = "rotate='PI/2:ow=ih:oh=iw'"与以前相同的问题再次出现。它似乎与分辨率的变化有关。

我认为损坏可能来自之后制作的拷贝(为了与我正在使用的其他东西兼容):
void copyToPicture(AVFrame const* avFrame, DataPicture* pic) {
    for (size_t comp=0; comp<pic->getNumPlanes(); ++comp) {
        auto const subsampling = comp == 0 ? 1 : 2;
        auto const bytePerPixel = pic->getFormat().format == YUYV422 ? 2 : 1;
        // std::cout<<"Pixel format is "<<pic->getFormat().format<<std::endl;
        auto src = avFrame->data[comp];
        auto const srcPitch = avFrame->linesize[comp];

        auto dst = pic->getPlane(comp);
        auto const dstPitch = pic->getPitch(comp);

        auto const w = avFrame->width * bytePerPixel / subsampling;
        auto const h = avFrame->height / subsampling;

        for (int y=0; y<h; ++y) {
            memcpy(dst, src, w);
            src += srcPitch;
            dst += dstPitch;
        }
    }
}

最佳答案

实际上这是一个完全不相关的问题,这段代码有效!

关于c++ - FFMpeg C Lib - 转置导致图像损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41170372/

相关文章:

c++ - 给定以弧度为单位的距离和角度,获取绕点旋转的物体的 X、Y 位置的函数?

javascript - 如何从 HTML5 视频播放器帧访问原始像素数据?

video - 使用视频标签进行自动加载的 Angular 指令

ffmpeg - 如何通过卡住最后一帧而不重新编码整个流来扩展视频?

php - ffmpeg-php 和 shell_exec

c++ - 在 C++ 中使用 POCO 和 cereal 反序列化多个对象

c++ - 设置指向对象的指针,让对象相互指向

java - 使用 ffmpeg 为 JavaFX MediaPlayer 生成视频

node.js - FFMpeg 错误执行调用 NodeJS 中的两个不同路径

c++ - 我应该使用哪个数据库来存储记录,我应该如何使用它?