gpt4 book ai didi

c++ - 从二进制数据恢复 MP3 文件

转载 作者:太空宇宙 更新时间:2023-11-04 11:40:53 25 4
gpt4 key购买 nike

我的任务是恢复 mp3 文件,该文件在 PNG 文件中逐位编码。我从 vector 中的 PNG RGB 数据(每像素)中获得了正确的位。我正在使用 C++。

我必须通过 png 文件并读取像素的 RGB 数据:然后我有 3 个十进制值。从十进制值的二进制表示,我需要最小的局部值。 11 个像素以 33 位显示 mp3 的长度。然后我从像素中解码所有二进制数据,并放入一个 vector 中;

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <vector>
#include <math.h>
#include <iostream>
#include <fstream>

#define PNG_DEBUG 3
#include <png.h>

void abort_(const char * s, ...)
{
va_list args;
va_start(args, s);
vfprintf(stderr, s, args);
fprintf(stderr, "\n");
va_end(args);
abort();
}

void itob(short n, std::vector<int> &bin)
{
int d = n;

if (n > 1)
{
d = n % 2;
itob(n / 2, bin);
}
bin.push_back(d);
}

void btoi(unsigned int& n, std::vector<int> bin)
{
n = 0;
int k = 32;
for(int i = 0; i < bin.size() ; i++){
if(bin[i] == 1){
long int num = pow(2,k);
n += num;
}
k--;
}
}

int x, y;

int width, height;
png_byte color_type;
png_byte bit_depth;

png_structp png_ptr;
png_infop info_ptr;
int number_of_passes;
png_bytep * row_pointers;

void read_png_file()
{
unsigned char header[8]; // 8 is the maximum size that can be checked

/* open file and test for it being a png */
FILE *fp = fopen("image.png", "rb");
if (!fp)
abort_("[read_png_file] File %s could not be opened for reading", "image.png");
fread(header, 1, 8, fp);
if (png_sig_cmp(header, 0, 8))
abort_("[read_png_file] File %s is not recognized as a PNG file", "image.png");


/* initialize stuff */
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);

if (!png_ptr)
abort_("[read_png_file] png_create_read_struct failed");

info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
abort_("[read_png_file] png_create_info_struct failed");

png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, 8);

png_read_info(png_ptr, info_ptr);

width = png_get_image_width(png_ptr, info_ptr);
height = png_get_image_height(png_ptr, info_ptr);
color_type = png_get_color_type(png_ptr, info_ptr);
bit_depth = png_get_bit_depth(png_ptr, info_ptr);

number_of_passes = png_set_interlace_handling(png_ptr);
png_read_update_info(png_ptr, info_ptr);

row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);
for (y=0; y<height; y++)
row_pointers[y] = (png_byte*) malloc(png_get_rowbytes(png_ptr,info_ptr));

png_read_image(png_ptr, row_pointers);

fclose(fp);
}


void process_file(void)
{
if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_RGBA)
abort_("[process_file] input file is PNG_COLOR_TYPE_RGB but must be PNG_COLOR_TYPE_RGB "
"(lacks the alpha channel)");

if (png_get_color_type(png_ptr, info_ptr) != PNG_COLOR_TYPE_RGB)
abort_("[process_file] color_type of input file must be PNG_COLOR_TYPE_RGB (%d) (is %d)",
PNG_COLOR_TYPE_RGBA, png_get_color_type(png_ptr, info_ptr));

printf("width: %d\nheight: %d\n", width, height);

int mHeader = 33; unsigned int mSize = 0;
std::vector<int> mSizeByBites;
for (y=0; y<height; y++) {
png_byte* row = row_pointers[y];
for (x=0; x<width; x++) {
png_byte* ptr = &(row[x*3]);
if(mHeader == 0){ break; }
mHeader-=3;

std::vector<int> b;

itob(ptr[0], b);
mSizeByBites.push_back(b[b.size()-1]);
b.clear();

itob(ptr[1], b);
mSizeByBites.push_back(b[b.size()-1]);
b.clear();

itob(ptr[2], b);
mSizeByBites.push_back(b[b.size()-1]);
b.clear();
}
if(mHeader == 0){ break; }
}


for(int i =0; i<mSizeByBites.size(); i++){
printf("%d", mSizeByBites[i]);
}
btoi(mSize, mSizeByBites);
printf(" = %i\n", mSize);

std::vector<int> mDataBaBites;

for (y=0; y<height; y++) {
png_byte* row = row_pointers[y];
for (x=0; x<width; x++) {
if(mSize <= 0){ break; }

png_byte* ptr = &(row[x*3]);
std::vector<int> b;

itob(ptr[0], b);
mDataBaBites.push_back(b[b.size()-1]);
b.clear();
mSize--;
if(mSize <= 0){ break; }
itob(ptr[1], b);
mDataBaBites.push_back(b[b.size()-1]);
b.clear();
mSize--;
if(mSize <= 0){ break; }
itob(ptr[2], b);
mDataBaBites.push_back(b[b.size()-1]);
b.clear();
mSize--;
if(mSize <= 0){ break; }
printf("%i\n", mSize);
}
if(mSize<=0){ break; }
}

std::ofstream output("result.mp3", std::ios::out | std::ios::binary);

printf("[D] Writing to file start: %li\n", mDataBaBites.size());
output.write( (char*)(&mDataBaBites[0]), mDataBaBites.size() );
output.close();

}


int main(int argc, char **argv)
{

read_png_file();
process_file();

return 0;
}

现在我不知道如何将它写入文件,我可以将其作为 mp3 播放。我尝试将位转换为六位。

mp3 文件的正确格式是什么?如何以正确的格式写入位?

最佳答案

试试这个:

#include <fstream> //For std::min

std::ofstream mp3File( "restored.mp3", std::ios::out | std::ios::binary );
//Assuming rgbData is a char* with the mp3 data,
//and rgbDataSize is its size in bytes
mp3File.write( rgbData, rgbDataSize );
mp3File.close();

更新:当我们(程序员)说“二进制表示”时,我们几乎总是指字节,而不是位。根据您对解码过程的描述,我认为您应该比较每个像素的 3 个 RGB 分量,并将最小值保留为解码字节。为此:

#include <algorithm>

//...

std::vector<char> mDataBaBites;

for (y=0; y<height; y++) {
png_byte* row = row_pointers[y];
for (x=0; x<width; x++) {
png_byte red = row[x*3];
png_byte green = row[x*3 + 1];
png_byte blue = row[x*3 + 2];
png_byte minByte = std::min( std::min(red,green), blue );
mDataBaBites.push_back( minByte );
mSize -= 3;
}
if(mSize<=0){ break; }
}

std::ofstream output("result.mp3", std::ios::out | std::ios::binary);
printf("[D] Writing to file start: %li\n", mDataBaBites.size());
output.write( (char*)(&mDataBaBites[0]), mDataBaBites.size() );
output.close();

更新 2:

    std::ofstream output("result.mp3", std::ios::out | std::ios::binary);
printf("[D] Writing to file start: %li\n", mDataBaBites.size());
for( int i=0; i<mDataBaBites.size(); i+=8 ){
char decodedByte = 0;
for( int j=0; j<8; j++ )
decodedByte |= (mDataBaBites[i+j] << j);
output.write( (char*)(&mDataBaBites[0]), 1 );
}
output.close();

如果这也不起作用,您可能想澄清解码过程定义(它的来源是什么?有一些正式的定义吗?)

关于c++ - 从二进制数据恢复 MP3 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21436449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com