从二进制数据还原mp3文件

Restore MP3 file from binary data

本文关键字:mp3 文件 还原 数据 二进制      更新时间:2023-10-16

我的taks是还原mp3文件,wich是在png文件中进行的。我从矢量中的PNG RGB数据(每个像素)中获得了正确的位。我正在使用C 。

我必须浏览PNG文件并读取像素的RGB数据:然后我有3个小数点值。从十进制值的二进制表示,我需要最小的局部值。11个像素显示了MP3长度的33位。然后,我从像素中解码所有二进制数据,然后放入向量;

#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: %dnheight: %dn", 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(" = %in", 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("%in", mSize);
            }
            if(mSize<=0){ break; }
        }
        std::ofstream output("result.mp3", std::ios::out | std::ios::binary);
        printf("[D] Writing to file start:    %lin", 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();

update :当我们(程序员)说"二进制表示"时,我们几乎总是表示字节,而不是位。从您对解码过程的描述中,我收集了您应该比较每个像素的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:    %lin", 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:    %lin", 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();

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