FFMpeg C Lib - Transpose导致图像损坏

时间:2016-12-15 17:49:50

标签: 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;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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