【问题标题】:Restore MP3 file from binary data从二进制数据恢复 MP3 文件
【发布时间】:2014-02-21 13:44:58
【问题描述】:

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

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

#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 文件的正确格式是什么?如何以正确的格式写入位?

【问题讨论】:

  • 如果mp3文件真的只是png的部分,你应该可以只保存数据。只需确保您使用的是二进制模式即可。
  • “二进制模式”是什么意思?
  • 他的意思是,您必须使用 std::ios::binary 标志创建 ofstream。

标签: c++ audio binary mp3 encode


【解决方案1】:

试试这个:

#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();

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

【讨论】:

  • 我有一个向量 存储位:1010101000111... 数据在位中。如果我按照你说的做,我只会得到一个充满这些 0 和 1 的文件。 :\
  • 如果v[0]=1,也就是数字1,这样就不行了。该数字的二进制表示将被写入(根据目标平台,它将由 32 位或 64 位组成),而不是 1。您需要将 mp3 数据读取为vector&lt;char&gt; 或char*(纯二进制数据) .发布您的代码会很有帮助。
  • 有了更新的描述,我现在可以编写一个好的二进制文件了。但仍然不是有效的 mp3。
  • 那也不好。当我查看 R、B 或 G 值时,我必须将其转换为二进制。例如:r=10 -> 1010。那么我必须选择最小的局部值:101[0]。这些数字来自每个像素的每个 RGB 值,串联起来,代表一个 mp3 文件数据。这就是我使用 vector. 的原因
  • 如果真的是这样,那么按 8 组抓取整数并从中创建一个字节怎么样?这就是更新 2 所做的。
猜你喜欢
  • 2017-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-26
  • 1970-01-01
  • 2013-06-14
  • 2016-01-15
  • 2020-05-13
相关资源
最近更新 更多