从ffmpeg中取出YUV数据

有时需要从ffmpeg中提取出YUV数据用作预览,另存什么的。
ffmpeg是先解码成YUV, 再以这个YUV作为输入进行编码,
所以YUV数据有两种:
  解码后的YUV数据, 以及
  编码重建的YUV数据。
下面分别讲两个YUV数据从哪儿?以及如何取?

1. 解码后的YUV数据
在ffmpeg/libavcodec/utils_codec.c的
avcodec_decode_video2() 函数中:

avcodec_decode_video2(...)
{
  ... 
  ret = avctx->codec->decode(avctx, picture, got_picture_ptr,avpkt);
  /* 插入如下代码代码 */
  // picture.data 就包含了解码后的YUV数据
  {
    int i, j;
    int shift;
    char *yuv = NULL;
    FILE *fp = fopen("dec_output.yuv", "ab+");  // 一定要用'b'打开,不然会数据错位
    
    for (i = 0; i < 3; i++)
    {
      shift = i>0 ? 1 : 0;
      yuv = picture.data[i];
      for (j = 0; j < picture.height>>shift; j++)
      {
        fwrite(yuv, sizeof(char), picture.width>>shift, fp); // 每次写一行YUV帧数据
        yuv += picture.linesize[i]; // ffmpeg将解码数据进行了扩边,需要以扩边为步长,找下行数据。  
      }
    }
    
    fflush(fp);
    fclose(fp);
  }
  /* 插入代码结束 */  
  picture->pkt_dts= avpkt->dts;
  ...
}


2. x264编码重建后的YUV数据
现在视频编码格式通常是H.264,
x264已有重建YUV写成文件的接口,所以可以直接使用。
对应的参数为:

ffmpeg -i INPUT ... -vcodec libx264 -x264opts dump-yuv=recon.yuv -f flv -y OUTPUT 

输出文件在:
x264/encoder/encoder.c的
x264_encoder_frame_end()函数中:
x264_encoder_frame_end()
{
  ...
  if( h->param.psz_dump_yuv )
     x264_frame_dump( h );    //YUV数据参照这个函数就能获得
  ... 
}

要将内存YUV数据混合成一个视频,可以使用FFmpeg库进行处理。以下是一个基本的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdint.h> #include <math.h> #include <libavcodec/avcodec.h> #include <libavutil/imgutils.h> #include <libavutil/opt.h> #include <libavutil/mathematics.h> #define WIDTH 640 #define HEIGHT 480 #define FPS 25 int main(int argc, char *argv[]) { int ret; AVCodec *codec; AVCodecContext *codec_context; AVFrame *frame; AVPacket packet; uint8_t *buffer; int buffer_size; int frame_count = 0; /* Allocate frame */ frame = av_frame_alloc(); if (!frame) { fprintf(stderr, "Error allocating frame\n"); exit(1); } /* Set frame properties */ frame->format = AV_PIX_FMT_YUV420P; frame->width = WIDTH; frame->height = HEIGHT; /* Allocate buffer for frame */ buffer_size = av_image_get_buffer_size(frame->format, frame->width, frame->height, 1); buffer = av_malloc(buffer_size); av_image_fill_arrays(frame->data, frame->linesize, buffer, frame->format, frame->width, frame->height, 1); /* Open codec */ codec = avcodec_find_encoder(AV_CODEC_ID_H264); if (!codec) { fprintf(stderr, "Codec not found\n"); exit(1); } codec_context = avcodec_alloc_context3(codec); if (!codec_context) { fprintf(stderr, "Error allocating codec context\n"); exit(1); } /* Set codec properties */ codec_context->width = WIDTH; codec_context->height = HEIGHT; codec_context->pix_fmt = AV_PIX_FMT_YUV420P; codec_context->time_base = (AVRational){1, FPS}; codec_context->bit_rate = 400000; /* Open codec */ ret = avcodec_open2(codec_context, codec, NULL); if (ret < 0) { fprintf(stderr, "Error opening codec: %s\n", av_err2str(ret)); exit(1); } /* Encode frames */ while (frame_count < FPS * 10) { /* Generate YUV data */ uint8_t *y_data = malloc(WIDTH * HEIGHT); uint8_t *u_data = malloc(WIDTH * HEIGHT / 4); uint8_t *v_data = malloc(WIDTH * HEIGHT / 4); // fill y_data, u_data, v_data with your desired YUV data /* Convert YUV data to frame */ int y_size = WIDTH * HEIGHT; int u_size = y_size / 4; int v_size = y_size / 4; memcpy(frame->data[0], y_data, y_size); memcpy(frame->data[1], u_data, u_size); memcpy(frame->data[2], v_data, v_size); /* Set frame properties */ frame->pts = frame_count++; /* Encode frame */ ret = avcodec_send_frame(codec_context, frame); if (ret < 0) { fprintf(stderr, "Error sending frame to codec: %s\n", av_err2str(ret)); exit(1); } while (ret >= 0) { ret = avcodec_receive_packet(codec_context, &packet); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) break; else if (ret < 0) { fprintf(stderr, "Error receiving packet from codec: %s\n", av_err2str(ret)); exit(1); } /* Write packet to file or stream */ // fwrite(packet.data, 1, packet.size, outfile); av_packet_unref(&packet); } free(y_data); free(u_data); free(v_data); } /* Flush encoder */ ret = avcodec_send_frame(codec_context, NULL); if (ret < 0) { fprintf(stderr, "Error sending flush frame to codec: %s\n", av_err2str(ret)); exit(1); } while (ret >= 0) { ret = avcodec_receive_packet(codec_context, &packet); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) break; else if (ret < 0) { fprintf(stderr, "Error receiving packet from codec: %s\n", av_err2str(ret)); exit(1); } /* Write packet to file or stream */ // fwrite(packet.data, 1, packet.size, outfile); av_packet_unref(&packet); } /* Free resources */ avcodec_free_context(&codec_context); av_frame_free(&frame); av_free(buffer); return 0; } ``` 在此示例,我们使用`av_image_fill_arrays()`函数分配了一个YUV420P格式的AVFrame,并将其用作编码器的输入。我们使用`avcodec_send_frame()`函数将帧发送到编码器进行编码,然后使用`avcodec_receive_packet()`函数从编码器接收编码数据包。最后,我们使用`av_packet_unref()`函数释放数据包并清除任何剩余的缓存数据。 要生成要混合的YUV数据,您可以从文件读取数据,也可以在内存生成数据。无论哪种方法,您都需要将其复制到AVFrame的data数组。请注意,AVFrameYUV数据顺序是YUV420P,即先是所有的Y分量,然后是U和V分量交错。因此,在将YUV数据复制到AVFrame之前,请确保按正确的顺序复制它们。 希望这可以帮助您开始使用FFmpeg处理内存YUV数据
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北雨南萍

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值