一、CIF文件格式
- Y分量每帧288行,每行352个,即352*288;
- U分量、V分量每帧144行,每行176个,即176*144;
在存储时为了便于处理,先存Y分量的一帧,然后u分量一帧,最后V分量一帧。接着开始下一帧的Y,U,V。一直到视频的最后。
二、实现代码
代码功能:读入cif视频第一帧,并将第一帧图像上下倒置输出。
// cif_read.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <fstream>
using namespace std;
#define Width_Y 352
#define Height_Y 288
#define Width_U_V 176
#define Height_U_V 144
void Read_File(ifstream& File,unsigned char Y[][Width_Y],unsigned char U[][Width_U_V],unsigned char V[][Width_U_V]) {
// 读入Y分量
for (int i = 0; i < Height_Y; i++){
for (int j = 0; j < Width_Y; j++){
File.read((char*)(*(Y + i) + j), sizeof(unsigned char));
}
}
// 读入U分量
for (int i = 0; i < Height_U_V; i++){
for (int j = 0; j < Width_U_V; j++){
File.read((char*)(*(U + i) + j), sizeof(unsigned char));
}
}
// 读入V分量
for (int i = 0; i < Height_U_V; i++){
for (int j = 0; j < Width_U_V; j++){
File.read((char*)(*(V + i) + j), sizeof(unsigned char));
}
}
}
// Y分量倒序写入cif文件
void Out_Put(unsigned char Y[][Width_Y], unsigned char U[][Width_U_V], unsigned char V[][Width_U_V]) {
ofstream picture;
picture.open("frame.yuv", ios::binary, ios::trunc);
if (!picture) {
cout << "Open failed picture.cif" << endl;
}
else {
// 写入Y分量
for (int i = Height_Y-1; i >= 0; i--){
for (int j = 0; j < Width_Y; j++){
picture.write((char*)(*(Y + i) + j), sizeof(unsigned char));
}
}
// 写入U分量
for (int i = Height_U_V-1; i >= 0; i--){
for (int j = 0; j < Width_U_V; j++){
picture.write((char*)(*(U + i) + j), sizeof(unsigned char));
}
}
// 写入V分量
for (int i = Height_U_V-1; i >= 0; i--){
for (int j = 0; j < Width_U_V; j++){
picture.write((char*)(*(V + i) + j), sizeof(unsigned char));
}
}
cout << "输出至 frame.yuv" << endl;
picture.close();
}
}
int main()
{
ifstream cif_video;
unsigned char frame_Y[Height_Y][Width_Y], frame_U[Height_U_V][Width_U_V], frame_V[Height_U_V][Width_U_V];
// 打开cif文件
cif_video.open("miss.cif", ios::in, ios::binary);
if (!cif_video) {
cout << "Open failed miss.cif" << endl;
return 0;
}
// 读取cif文件
Read_File(cif_video, frame_Y, frame_U, frame_V);
// 倒序写入cif文件
Out_Put(frame_Y, frame_U, frame_V);
// 关闭文件
cif_video.close();
}
三、结果验证
原始cif文件:
处理后第一帧图像: