<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>奋斗 &#187; fec</title>
	<atom:link href="http://fangfangtu.com/blog/tag/fec/feed/" rel="self" type="application/rss+xml" />
	<link>http://fangfangtu.com/blog</link>
	<description>生命在于折腾</description>
	<lastBuildDate>Sat, 19 May 2012 21:44:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>给数据增加冗余 前向纠错编码</title>
		<link>http://fangfangtu.com/blog/2008/08/fec/</link>
		<comments>http://fangfangtu.com/blog/2008/08/fec/#comments</comments>
		<pubDate>Mon, 11 Aug 2008 14:35:59 +0000</pubDate>
		<dc:creator>bborn</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[fec]]></category>
		<category><![CDATA[forward error correction]]></category>

		<guid isPermaLink="false">http://bborn.cn/blog/?p=728</guid>
		<description><![CDATA[在不可靠的链路传送数据 比如udp 如果需要增加数据传输的可靠性 我们可以在传输原始数据的时候 增加冗余数据 这样 只要我们收到了一定数量的数据包（不需要所有的） 就可以还原出原始的数据包 比如原始数据是“10” 分成两个包，分别为“1”和“0” 通过异或计算出一个冗余包“0” 我们收到任意两个就能组装出原始的包（知道次序的情况下） 这有些类似磁盘冗余阵列的原理 这些纠错编码的理论不少 实际上可以直接拿来用的代码很少见 好不容易找到了这个 zfec C代码 GPL 提供了Python接口 作者看起来是个水果迷 在他的Mac中测试效率不错 FEC是Forward Error Correction的简称，意为前向纠错 虽然给了代码 实际要用起来还是很麻烦的 所以这里记录一下方法 1 fec_t* fec_new(unsigned k, unsigned m); k 把原始数据包分成k块 m 编码后生成m块数据块 块的大小都是一致的，所以很显然m&#62;k，其中编码后的m块中，前k块就是原始的数据 2 void fec_encode(const fec_t* code, const gf*restrict const*restrict const src, gf*restrict const*restrict const fecs, const unsigned*restrict [...]]]></description>
			<content:encoded><![CDATA[<p>在不可靠的链路传送数据 比如udp<br />
如果需要增加数据传输的可靠性<br />
我们可以在传输原始数据的时候 增加冗余数据<br />
这样 只要我们收到了一定数量的数据包（不需要所有的）<br />
就可以还原出原始的数据包<br />
比如原始数据是“10”<br />
分成两个包，分别为“1”和“0”<br />
通过异或计算出一个冗余包“0”<br />
我们收到任意两个就能组装出原始的包（知道次序的情况下）<br />
这有些类似磁盘冗余阵列的原理</p>
<p>这些纠错编码的理论不少<br />
实际上可以直接拿来用的代码很少见<br />
好不容易找到了这个 <a href="http://pypi.python.org/pypi/zfec">zfec</a><br />
C代码 GPL 提供了Python接口<br />
作者看起来是个水果迷<br />
在他的Mac中测试效率不错<br />
FEC是Forward Error Correction的简称，意为前向纠错</p>
<p>虽然给了代码 实际要用起来还是很麻烦的<br />
所以这里记录一下方法</p>
<p><span id="more-402"></span><br />
1 fec_t* fec_new(unsigned k, unsigned m);<br />
k 把原始数据包分成k块<br />
m 编码后生成m块数据块<br />
块的大小都是一致的，所以很显然m&gt;k，其中编码后的m块中，前k块就是原始的数据</p>
<p>2 void fec_encode(const fec_t* code, const gf*restrict const*restrict const src, gf*restrict const*restrict const fecs, const unsigned*restrict const block_nums, size_t num_block_nums, size_t sz);<br />
编码，参数比较难看，src是包含原始数据指针的一个数组(原始数据分成k块)，fecs 是返回的包含冗余数据指针的数组，block_nums为冗余数组的序号，由我们设置传入的，k&lt;=序号</p>
<p>3 void fec_decode(const fec_t* code, const gf*restrict const*restrict const inpkts, gf*restrict const*restrict const outpkts, const unsigned*restrict const index, size_t sz);<br />
基本同上，我们只需要任意k个包就可以还原。inpkts是将收到的数据包按序传入，如果丢包则拿一个冗余包补入。outpkts是返回我们缺失的数据包，index是我们传入的数据包的序号，sz是每个包的大小</p>
<p>4 void fec_free(fec_t* p);<br />
不过 即使这样，还是有内存泄露，自己改改吧<br />
//<br />
说不清楚 看代码吧</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">fec_t<span style="color: #000040;">*</span> p <span style="color: #000080;">=</span> fec_new<span style="color: #008000;">&#40;</span> <span style="color: #0000dd;">4</span>,<span style="color: #0000dd;">6</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">char</span> buf<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1024</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span> <span style="color: #0000dd;">1024</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
buf<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span> i <span style="color: #000040;">%</span> <span style="color: #0000dd;">255</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> src<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span>buf, buf <span style="color: #000040;">+</span> <span style="color: #0000dd;">256</span>, buf <span style="color: #000040;">+</span> <span style="color: #0000dd;">512</span>, buf <span style="color: #000040;">+</span> <span style="color: #0000dd;">768</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">char</span> dest<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">512</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> <span style="color: #0000ff;">const</span> fecs<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span> dest, dest <span style="color: #000040;">+</span> <span style="color: #0000dd;">256</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
UINT block_nums<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #0000dd;">4</span>,<span style="color: #0000dd;">5</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><span style="color: #666666;">//原始数据编号是0，1，2，3</span>
UINT num_block_nums <span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span> <span style="color: #666666;">//block_nums的大小</span>
UINT sz <span style="color: #000080;">=</span> <span style="color: #0000dd;">1024</span> <span style="color: #000040;">/</span> <span style="color: #0000dd;">4</span><span style="color: #008080;">;</span>
&nbsp;
fec_encode<span style="color: #008000;">&#40;</span>p, <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> gf<span style="color: #000040;">*</span><span style="color: #0000ff;">const</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>src, <span style="color: #008000;">&#40;</span>gf<span style="color: #000040;">*</span><span style="color: #0000ff;">const</span><span style="color: #000040;">*</span><span style="color: #0000ff;">const</span><span style="color: #008000;">&#41;</span>fecs, block_nums, num_block_nums, sz<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// 解码，假设我们丢失了原始的数据包 0，3</span>
&nbsp;
<span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> inpkts<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span>fecs<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span>, buf <span style="color: #000040;">+</span> <span style="color: #0000dd;">256</span>, buf <span style="color: #000040;">+</span> <span style="color: #0000dd;">512</span>, fecs<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><span style="color: #666666;">//必须按顺序，丢失的包由冗余包补齐</span>
<span style="color: #0000ff;">char</span> output<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">512</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> <span style="color: #0000ff;">const</span> outpkts<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span> output, output <span style="color: #000040;">+</span> <span style="color: #0000dd;">256</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
UINT index<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #0000dd;">4</span>, <span style="color: #0000dd;">1</span>, <span style="color: #0000dd;">2</span>, <span style="color: #0000dd;">5</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span> <span style="color: #666666;">//对应inpkts的顺序</span>
&nbsp;
fec_decode<span style="color: #008000;">&#40;</span>p, <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> gf<span style="color: #000040;">*</span> <span style="color: #0000ff;">const</span><span style="color: #000040;">*</span><span style="color: #0000ff;">const</span><span style="color: #008000;">&#41;</span>inpkts, <span style="color: #008000;">&#40;</span>gf<span style="color: #000040;">*</span> <span style="color: #0000ff;">const</span><span style="color: #000040;">*</span> <span style="color: #0000ff;">const</span><span style="color: #008000;">&#41;</span>outpkts, <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">unsigned</span><span style="color: #000040;">*</span> <span style="color: #0000ff;">const</span><span style="color: #008000;">&#41;</span>index, sz<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> <span style="color: #0000dd;">0</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">memcmp</span><span style="color: #008000;">&#40;</span> src<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span>, outpkts<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000dd;">256</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
TRACE<span style="color: #008000;">&#40;</span>L<span style="color: #FF0000;">&quot;ok&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> <span style="color: #0000dd;">0</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">memcmp</span><span style="color: #008000;">&#40;</span> src<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#93;</span>, outpkts<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000dd;">256</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
TRACE<span style="color: #008000;">&#40;</span>L<span style="color: #FF0000;">&quot;ok&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
fec_free<span style="color: #008000;">&#40;</span>p<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<h3  class="related_post_title">随便看看</h3><ul class="related_post"><li>2007-09-02 -- <a href="http://fangfangtu.com/blog/2007/09/little-white-marry-bighea/" title="小白,嫁给大头吧">小白,嫁给大头吧</a></li><li>2006-07-13 -- <a href="http://fangfangtu.com/blog/2006/07/office-worker/" title="上班族">上班族</a></li><li>2012-02-01 -- <a href="http://fangfangtu.com/blog/2012/02/new-year-2012/" title="新的一年">新的一年</a></li><li>2007-07-26 -- <a href="http://fangfangtu.com/blog/2007/07/ten-years-dream/" title="十年如一梦,我恍惚中过">十年如一梦,我恍惚中过</a></li><li>2010-05-23 -- <a href="http://fangfangtu.com/blog/2010/05/0910champion/" title="写在09-10赛季的欧冠决赛前">写在09-10赛季的欧冠决赛前</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://fangfangtu.com/blog/2008/08/fec/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

