如何使用ffmpeg复制其他帧缓冲区

如何使用ffmpeg复制其他帧缓冲区,ffmpeg,frame,Ffmpeg,Frame,我有一个关于其他位置的框架复制的问题 我从h264/mp4中得到了一个1280640解码帧(YUV420),我想将4帧合并到25601280帧缓冲区。如下 代码是这样的,但不起作用 在这里输入代码 /*用于解码帧信息的pCodecCtx和pFrame*/ 整数编码2JPEG(AVCODEContext*PCODECTX,AVFrame*pFrame,整数帧号,整数质量) { int-gotFrame; /*JPEG编码的新上下文*/ AVCodec*jpeg\u codec=NULL; AVC

我有一个关于其他位置的框架复制的问题

我从h264/mp4中得到了一个1280640解码帧(YUV420),我想将4帧合并到25601280帧缓冲区。如下

代码是这样的,但不起作用

在这里输入代码

/*用于解码帧信息的pCodecCtx和pFrame*/ 整数编码2JPEG(AVCODEContext*PCODECTX,AVFrame*pFrame,整数帧号,整数质量) {

int-gotFrame;
/*JPEG编码的新上下文*/
AVCodec*jpeg\u codec=NULL;
AVCodecContext*jpeg_ctx=NULL;
int ret;
AVPacket*数据包;
packet=av_packet_alloc();
如果(!数据包)
{
printf(“alloc fail!\n”);
返回-1;
}
如果(帧)
printf(“用于编码%3”PRId64“\n”的接收帧,帧->pts);
pFrame_gather=av_frame_alloc();
如果(!pFrame\u聚集)
{
printf(“无法分配网关帧!\n”);
返回-1;
}
pFrame\u gather->format=pCodecCtx->pix\u fmt;
pFrame_gather->宽度=pCodecCtx->宽度*2;
pFrame_gather->高度=pCodecCtx->高度*2;
if(av帧获取缓冲区(pFrame帧聚集,0)<0)
printf(“无法分配采集帧数据!\n”);
/*缓冲区清除*/
ptrdiff_t linesize[4]={frame->linesize[0],0,0,0};
如果(av图像填充)为黑色(pFrame采集->数据、线条尺寸、PCODECTX->像素fmt、\
AVCOL\u范围\u JPEG,pFrame\u聚集->宽度,pFrame\u聚集->高度)!=0)
printf(“图像清除错误!\n”);
ret=av\U帧\U使\U可写(pFrame\U聚集);
如果(重新pix_fmt);
内部y_移位;
int x_移位;
int max_步长[4];
如果(pix|u fmt<0 | pix|u fmt>=AV|u pix|u fmt|NB)
返回-1;
y_shift=desc->log2_色度;
x_shift=desc->log2_色度w;
av_图像_填充_最大_像素步数(最大步数,空,描述);
/*Y数据*/
memcpy(pFrame\u聚集->数据[0]+(上带*pFrame->线宽[0])+左带\
帧->数据[0],pCodecCtx->宽度*pCodecCtx->高度);
/*U数据*/
memcpy(pFrame\u聚集->数据[1]+((顶部频带>>y\u移位)*pFrame->linesize[1])+\
(左波段>>x\U移位),帧->数据[1],pCodecCtx->宽度/2*pCodecCtx->高度/2);
/*V数据*/
memcpy(pFrame\u聚集->数据[2]+((顶部频带>>y\u移位)*pFrame->linesize[2])\
+(左波段>>x\U移位),帧->数据[2],pCodecCtx->宽度/2*pCodecCtx->高度/2);
/*unref变量*/
....
}

上面的源代码很难编写。它不起作用。 如何用位置记忆复制

    int gotFrame;

    /* New Context for JPEG encoding */
    AVCodec *jpeg_codec = NULL;
    AVCodecContext *jpeg_ctx =NULL;
    int ret;
    AVPacket *packet;
    packet = av_packet_alloc();
    if(!packet)
    {
            printf("alloc fail!\n");
            return -1;
    }

    if(frame)
            printf("received frame for encoding %3"PRId64"\n", frame->pts);

    pFrame_gather = av_frame_alloc();
    if(!pFrame_gather)
    {
            printf("Could not allocate gater frame!\n");
            return -1;
    }

    pFrame_gather->format = pCodecCtx->pix_fmt;
    pFrame_gather->width = pCodecCtx->width*2;
    pFrame_gather->height = pCodecCtx->height*2;

    if ( av_frame_get_buffer(pFrame_gather, 0) < 0)
            printf("Could not allocate the gather frame data!\n");

    /* Buffer Clear */
    ptrdiff_t linesize[4] = { frame->linesize[0], 0, 0, 0 };
    if( av_image_fill_black ( pFrame_gather->data, linesize, pCodecCtx->pix_fmt, \
                    AVCOL_RANGE_JPEG , pFrame_gather->width, pFrame_gather->height) != 0 )
            printf("image clear error!\n");

    ret = av_frame_make_writable(pFrame_gather);
    if( ret <0)
                    printf("writeable set fail!\n");

     /* set position of destination */
    int top_band =0;
    int left_band=200;

     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pCodecCtx->pix_fmt);
     int y_shift;
     int x_shift;
     int max_step[4];

     if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)
      return -1;

 y_shift = desc->log2_chroma_h;
 x_shift = desc->log2_chroma_w;
 av_image_fill_max_pixsteps(max_step, NULL, desc);
 
 /* Y Data */
 memcpy( pFrame_gather->data[0] + (top_band * pFrame->linesize[0]) + left_band,\
         frame->data[0] , pCodecCtx->width*pCodecCtx->height);
 /* U Data */
 memcpy ( pFrame_gather->data[1] + ((top_band >> y_shift) * pFrame->linesize[1]) + \
          (left_band >> x_shift), frame->data[1], pCodecCtx->width/2*pCodecCtx->height/2);
 /* V DAta */
 memcpy ( pFrame_gather->data[2] + ((top_band >> y_shift) * pFrame->linesize[2]) \
          + (left_band >> x_shift), frame->data[2], pCodecCtx->width/2*pCodecCtx->height/2);

 /* unref variable .... */
 ....