英文:
Formation of CRC-32 for SDT to ts. file
问题
我的任务是从服务器捕获视频流并将其写入ts文件。当我尝试将SDT包写入ts文件时,遇到了一个问题。我通过字节手动形成数据包,但是我无法计算SDT末尾的4个字节的CRC-32校验和。(使用golang)
我尝试根据已写入SDT包的数据的校验和来获取CRC,但是它不起作用。在逐字节解析的SDT中,CRC与我计算的不同。
英文:
My task is to capture the videostream from the server and write it to ts. files. I ran into a problem when trying to write a SDT package to ts. I form the packet manually by bytes and I can’t figure out how to calculate 4 bytes of CRC-32 for the end of SDT. (used golang)
I tried to get the CRC based on the checksum of the data already written to the SDT packet, but it doesn't work. In the byte-by-byte parsed SDT, CRC was different from what I calculated.
答案1
得分: 2
如果你在谈论MPEG-2,那么CRC的定义如下:
width=32 poly=0x04c11db7 init=0xffffffff refin=false refout=false xorout=0x00000000 check=0x0376e6e7 residue=0x00000000 name="CRC-32/MPEG-2"
在RFC 4326中有一个例子:
00 3f 86 dd 00 01 02 03 04 05 60 00 00 00 00 0d
3a 40 20 01 0d b8 30 08 19 65 00 00 00 00 00 00
00 01 20 01 0d b8 25 09 19 62 00 00 00 00 00 00
00 02 80 00 9d 8c 06 38 00 04 00 00 00 00 00 7c
17 17 63
其中最后四个字节是它之前所有字节的CRC-32/MPEG-2(按大端顺序)。
以下是计算该CRC的示例C代码:
#include <stddef.h>
#include <stdint.h>
uint32_t crc32mpeg2(uint32_t crc, void const *mem, size_t len) {
    unsigned char const *data = mem;
    if (data == NULL)
        return 0xffffffff;
    for (size_t i = 0; i < len; i++) {
        crc ^= (uint32_t)data[i] << 24;
        for (unsigned k = 0; k < 8; k++) {
            crc = crc & 0x80000000 ? (crc << 1) ^ 0x4c11db7 : crc << 1;
        }
    }
    return crc;
}
(当mem等于NULL时,返回初始CRC值。)
英文:
If you're talking about MPEG-2, then the CRC definition is:
width=32 poly=0x04c11db7 init=0xffffffff refin=false refout=false xorout=0x00000000 check=0x0376e6e7 residue=0x00000000 name="CRC-32/MPEG-2"
There is an example in RFC 4326:
00 3f 86 dd 00 01 02 03 04 05 60 00 00 00 00 0d
3a 40 20 01 0d b8 30 08 19 65 00 00 00 00 00 00
00 01 20 01 0d b8 25 09 19 62 00 00 00 00 00 00
00 02 80 00 9d 8c 06 38 00 04 00 00 00 00 00 7c
17 17 63
where the last four bytes of that is the CRC-32/MPEG-2 (in big-endian order) of all of the bytes that precede it.
Here is example C code to calculate that CRC:
#include <stddef.h>
#include <stdint.h>
uint32_t crc32mpeg2(uint32_t crc, void const *mem, size_t len) {
    unsigned char const *data = mem;
    if (data == NULL)
        return 0xffffffff;
    for (size_t i = 0; i < len; i++) {
        crc ^= (uint32_t)data[i] << 24;
        for (unsigned k = 0; k < 8; k++) {
            crc = crc & 0x80000000 ? (crc << 1) ^ 0x4c11db7 : crc << 1;
        }
    }
    return crc;
}
(When called with mem equal to NULL, the initial CRC value is returned.)
答案2
得分: 2
以下是Mark Adler提供的相同(Golang)代码(如果有人需要):
func CalcCRC32(data []byte) uint32 {
	var crc uint32 = 0xffffffff
	if data == nil {
		return crc
	}
	for _, v := range data {
		crc ^= uint32(v) << 24
		for j := 0; j < 8; j++ {
			if (crc & 0x80000000) != 0 {
				crc = (crc << 1) ^ 0x04C11DB7
			} else {
				crc <<= 1
			}
		}
	}
	return crc
}
英文:
Here is the same (Golang) code as provided by Mark Adler (in case anyone needs it):
func CalcCRC32(data []byte) uint32 {
	var crc uint32 = 0xffffffff
	if data == nil {
		return crc
	}
	for _, v := range data {
		crc ^= uint32(v) << 24
		for j := 0; j < 8; j++ {
			if (crc & 0x80000000) != 0 {
				crc = (crc << 1) ^ 0x04C11DB7
			} else {
				crc <<= 1
			}
		}
	}
	return crc
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论