ffmpeg里yuv格式以及各种format,笔记

ffmpeg里yuv格式以及各种8~10bits的定义在pixfmt.h里。


转换大概方法,
拿format索引
av_pix_fmt_desc_get(dstFormat)


设定输入输出格式
dstContext = sws_getContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat,
                                flags, NULL, NULL, NULL);
然后用转换
sws_scale(dstContext, (const uint8_t * const*)src, srcStride, 0, srcH, dst, dstStride);


细节没仔细看,有兴趣可以细看swscale-test.c

要保存 YUV 格式数据,可以使用 FFmpeg 库提供的 `AVFrame` 结构体和 `av_write_frame` 函数。下面是保存 YUV 格式数据的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <libavformat/avformat.h> int main(int argc, char *argv[]) { int ret; AVFormatContext *fmt_ctx = NULL; AVOutputFormat *ofmt = NULL; AVStream *video_st = NULL; AVCodecContext *codec_ctx = NULL; AVFrame *frame = NULL; uint8_t *frame_data = NULL; int frame_size; int width = 640, height = 480; // 打开输出文件 if ((ret = avformat_alloc_output_context2(&fmt_ctx, NULL, NULL, "output.yuv")) < 0) { fprintf(stderr, "Error allocating output context: %s\n", av_err2str(ret)); return 1; } ofmt = fmt_ctx->oformat; // 添加视频流 video_st = avformat_new_stream(fmt_ctx, NULL); if (!video_st) { fprintf(stderr, "Error creating video stream\n"); return 1; } codec_ctx = video_st->codec; codec_ctx->codec_id = AV_CODEC_ID_RAWVIDEO; codec_ctx->codec_type = AVMEDIA_TYPE_VIDEO; codec_ctx->pix_fmt = AV_PIX_FMT_YUV420P; codec_ctx->width = width; codec_ctx->height = height; codec_ctx->time_base = (AVRational){1, 25}; if ((ret = avcodec_parameters_to_context(codec_ctx, video_st->codecpar)) < 0) { fprintf(stderr, "Error copying codec parameters to context: %s\n", av_err2str(ret)); return 1; } // 打开视频编码器 if ((ret = avcodec_open2(codec_ctx, NULL, NULL)) < 0) { fprintf(stderr, "Error opening video encoder: %s\n", av_err2str(ret)); return 1; } // 创建帧缓冲区 frame = av_frame_alloc(); if (!frame) { fprintf(stderr, "Error allocating frame\n"); return 1; } frame->width = width; frame->height = height; frame->format = codec_ctx->pix_fmt; if ((ret = av_frame_get_buffer(frame, 0)) < 0) { fprintf(stderr, "Error allocating frame buffer: %s\n", av_err2str(ret)); return 1; } frame_data = frame->data[0]; frame_size = av_image_get_buffer_size(codec_ctx->pix_fmt, width, height, 1); // 打开输出文件 if (!(ofmt->flags & AVFMT_NOFILE)) { if ((ret = avio_open(&fmt_ctx->pb, "output.yuv", AVIO_FLAG_WRITE)) < 0) { fprintf(stderr, "Error opening output file: %s\n", av_err2str(ret)); return 1; } } // 写入视频帧 for (int i = 0; i < 25; i++) { // 生成测试图像 for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int r = rand() % 256; int g = rand() % 256; int b = rand() % 256; uint8_t *yuv = frame_data + y * frame->linesize[0] + x * 3 / 2; yuv[0] = 0.299 * r + 0.587 * g + 0.114 * b; yuv[1] = -0.14713 * r - 0.28886 * g + 0.436 * b; yuv[2] = 0.615 * r - 0.51498 * g - 0.10001 * b; } } // 编码并写入帧 frame->pts = i; AVPacket pkt = {0}; av_init_packet(&pkt); pkt.data = NULL; pkt.size = 0; if ((ret = avcodec_send_frame(codec_ctx, frame)) < 0) { fprintf(stderr, "Error sending frame to encoder: %s\n", av_err2str(ret)); return 1; } while (ret >= 0) { ret = avcodec_receive_packet(codec_ctx, &pkt); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { break; } else if (ret < 0) { fprintf(stderr, "Error encoding frame: %s\n", av_err2str(ret)); return 1; } pkt.stream_index = video_st->index; av_write_frame(fmt_ctx, &pkt); av_packet_unref(&pkt); } } // 清理工作 av_write_trailer(fmt_ctx); if (codec_ctx) avcodec_close(codec_ctx); if (frame) av_frame_free(&frame); if (fmt_ctx && !(ofmt->flags & AVFMT_NOFILE)) avio_closep(&fmt_ctx->pb); avformat_free_context(fmt_ctx); return 0; } ``` 这段代码首先创建一个 YUV 格式的视频流,然后生成一些随机数据作为测试图像,并将每帧图像编码成 YUV 格式的数据,最终将所有帧数据写入到文件中。注意,这使用了随机数据生成测试图像,实际应用中需要根据实际情况生成真实的图像数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值