10bit YUV数据在内存中的存储格式

我们知道为了获得更好的动态范围,除了常见的8bit yuv外,还有10bit,16bit这样的yuv数据。8bit的yuv数据还好理解,每一个像素8bit,在内存中自然也就是一个字节一个字节的存储咯,16bit的也类似,每一个像素对应两个字节,在内存中存起来也非常方便,那么10bit呢?

在不做任何调查的情况下,我们可以凭直觉猜想有两种存储方式:
1.每个像素依然占用16bit两个字节,但是其中6个bit是padding,补0
2.每个像素实打实地占用10bit,各个像素交织在一起,没法整齐地分布在字节中

第一种方式便于运算处理,但是有存储冗余,第二种方式则没有冗余,但是计算起来就很麻烦了。事实上,10bit是采用第一种方式存储在内存中的,也就是说,为了高动态范围,牺牲了一点压缩效率,但也获得了运算性能的加成,大概就是多媒体技术里无处不在的trade off了。

下面来详细说说是如何存储的,并且以实例进行验证。

参考这里的文档:
10-bit and 16-bit YUV Video Formats
可以得知10bit yuv数据在内存中的存储格式如下图所示

  • 6
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
要将内存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数据

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

zhanghui_cuc

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

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

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

打赏作者

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

抵扣说明:

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

余额充值