如何读取 ADTS 编码的 aac 文件的 header ?我需要它来获取每个帧的缓冲区长度以读出整个 aac 文件。但我无法获得正确的值。这是我的代码,用于在假设大端序时读取 header 并获取每个帧的缓冲区长度(位 30 - 43):
main(){
ifstream file("audio_adts.m4a", ios::binary);
char header[7],buf[1024];
int framesize;
while(file.read(header,7)) {
memset(buf ,0 , 1024);
/* Get header bit 30 - 42 */
framesize = (header[3]&240|header[4]|header[5]&1);
cout << "Framesize including header: "<<framesize<<endl;
file.read(buf,framesize);
/*Do something with buffer*/
}
return 0;
}
我用这段代码得到的帧大小是 65, 45 ,45, 45, -17,然后由于负值而停止。实际帧大小约为 200。
第一个 header 的十六进制转储:
0x000000: ff f9 50 40 01 3f fc
最佳答案
您提取的framesize
似乎有转变 <<
丢失,需要将提取的位放入正确的位置
位掩码看起来与 /*bit 30-42*/
不匹配评论。
此外,更改 char
至unsigned char
否则,当您进行这种类型的位操作时,您会遇到各种符号扩展问题(这是负值错误的原因)
关于c++ - 如何在 C++ 中从文件中读取 ADTS header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24219760/