comparison fraps.c @ 4142:79ddfdee291d libavcodec

Correct support for Fraps v4 (and Huffman tree for < 256 symbols)
author kostya
date Sun, 05 Nov 2006 08:18:52 +0000
parents b03f19bc1698
children 228252c41929
comparison
equal deleted inserted replaced
4141:b03f19bc1698 4142:79ddfdee291d
91 */ 91 */
92 static int huff_cmp(const Node *a, const Node *b){ 92 static int huff_cmp(const Node *a, const Node *b){
93 return (a->count - b->count)*256 + a->sym - b->sym; 93 return (a->count - b->count)*256 + a->sym - b->sym;
94 } 94 }
95 95
96 static void get_tree_codes(uint32_t *bits, int16_t *lens, Node *nodes, int node, uint32_t pfx, int pl) 96 static void get_tree_codes(uint32_t *bits, int16_t *lens, uint8_t *xlat, Node *nodes, int node, uint32_t pfx, int pl, int *pos)
97 { 97 {
98 int s; 98 int s;
99 99
100 s = nodes[node].sym; 100 s = nodes[node].sym;
101 if(s != HNODE){ 101 if(s != HNODE || !nodes[node].count){
102 bits[s] = pfx; 102 bits[*pos] = pfx;
103 lens[s] = pl; 103 lens[*pos] = pl;
104 xlat[*pos] = s;
105 (*pos)++;
104 }else{ 106 }else{
105 pfx <<= 1; 107 pfx <<= 1;
106 pl++; 108 pl++;
107 get_tree_codes(bits, lens, nodes, nodes[node].n0, pfx, pl); 109 get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0, pfx, pl, pos);
108 pfx |= 1; 110 pfx |= 1;
109 get_tree_codes(bits, lens, nodes, nodes[node].n0+1, pfx, pl); 111 get_tree_codes(bits, lens, xlat, nodes, nodes[node].n0+1, pfx, pl, pos);
110 } 112 }
111 } 113 }
112 114
113 static int build_huff_tree(VLC *vlc, Node *nodes) 115 static int build_huff_tree(VLC *vlc, Node *nodes, uint8_t *xlat)
114 { 116 {
115 uint32_t bits[256]; 117 uint32_t bits[256];
116 int16_t lens[256]; 118 int16_t lens[256];
117 119 int pos = 0;
118 get_tree_codes(bits, lens, nodes, 510, 0, 0); 120
119 return init_vlc(vlc, 9, 256, lens, 2, 2, bits, 4, 4, 0); 121 get_tree_codes(bits, lens, xlat, nodes, 510, 0, 0, &pos);
122 return init_vlc(vlc, 9, pos, lens, 2, 2, bits, 4, 4, 0);
120 } 123 }
121 124
122 125
123 /** 126 /**
124 * decode Fraps v2 packed plane 127 * decode Fraps v2 packed plane
129 int i, j; 132 int i, j;
130 int cur_node; 133 int cur_node;
131 GetBitContext gb; 134 GetBitContext gb;
132 VLC vlc; 135 VLC vlc;
133 int64_t sum = 0; 136 int64_t sum = 0;
137 uint8_t recode[256];
134 138
135 for(i = 0; i < 256; i++){ 139 for(i = 0; i < 256; i++){
136 s->nodes[i].sym = i; 140 s->nodes[i].sym = i;
137 s->nodes[i].count = LE_32(src); 141 s->nodes[i].count = LE_32(src);
138 s->nodes[i].n0 = -2; 142 s->nodes[i].n0 = -2;
145 av_log(s->avctx, AV_LOG_ERROR, "Too high symbol frequencies. Tree construction is not possible\n"); 149 av_log(s->avctx, AV_LOG_ERROR, "Too high symbol frequencies. Tree construction is not possible\n");
146 return -1; 150 return -1;
147 } 151 }
148 qsort(s->nodes, 256, sizeof(Node), huff_cmp); 152 qsort(s->nodes, 256, sizeof(Node), huff_cmp);
149 cur_node = 256; 153 cur_node = 256;
150 // FIXME how it will handle nodes with zero count?
151 for(i = 0; i < 511; i += 2){ 154 for(i = 0; i < 511; i += 2){
152 s->nodes[cur_node].sym = HNODE; 155 s->nodes[cur_node].sym = HNODE;
153 s->nodes[cur_node].count = s->nodes[i].count + s->nodes[i+1].count; 156 s->nodes[cur_node].count = s->nodes[i].count + s->nodes[i+1].count;
154 s->nodes[cur_node].n0 = i; 157 s->nodes[cur_node].n0 = i;
155 for(j = cur_node; j > 0; j--){ 158 for(j = cur_node; j > 0; j--){
156 if(s->nodes[j].count >= s->nodes[j - 1].count) break; 159 if(s->nodes[j].count >= s->nodes[j - 1].count) break;
157 FFSWAP(Node, s->nodes[j], s->nodes[j - 1]); 160 FFSWAP(Node, s->nodes[j], s->nodes[j - 1]);
158 } 161 }
159 cur_node++; 162 cur_node++;
160 } 163 }
161 if(build_huff_tree(&vlc, s->nodes) < 0){ 164 if(build_huff_tree(&vlc, s->nodes, recode) < 0){
162 av_log(s->avctx, AV_LOG_ERROR, "Error building tree\n"); 165 av_log(s->avctx, AV_LOG_ERROR, "Error building tree\n");
163 return -1; 166 return -1;
164 } 167 }
165 /* we have built Huffman table and are ready to decode plane */ 168 /* we have built Huffman table and are ready to decode plane */
166 169
168 s->dsp.bswap_buf(s->tmpbuf, src, size >> 2); 171 s->dsp.bswap_buf(s->tmpbuf, src, size >> 2);
169 172
170 init_get_bits(&gb, s->tmpbuf, size * 8); 173 init_get_bits(&gb, s->tmpbuf, size * 8);
171 for(j = 0; j < h; j++){ 174 for(j = 0; j < h; j++){
172 for(i = 0; i < w; i++){ 175 for(i = 0; i < w; i++){
173 dst[i] = get_vlc2(&gb, vlc.table, 9, 3); 176 dst[i] = recode[get_vlc2(&gb, vlc.table, 9, 3)];
174 /* lines are stored as deltas between previous lines 177 /* lines are stored as deltas between previous lines
175 * and we need to add 0x80 to the first lines of chroma planes 178 * and we need to add 0x80 to the first lines of chroma planes
176 */ 179 */
177 if(j) dst[i] += dst[i - stride]; 180 if(j) dst[i] += dst[i - stride];
178 else if(Uoff) dst[i] += 0x80; 181 else if(Uoff) dst[i] += 0x80;
308 311
309 case 2: 312 case 2:
310 case 4: 313 case 4:
311 /** 314 /**
312 * Fraps v2 is Huffman-coded YUV420 planes 315 * Fraps v2 is Huffman-coded YUV420 planes
313 * Fraps v4 is the same except it works in grayscale 316 * Fraps v4 is virtually the same
314 */ 317 */
315 avctx->pix_fmt = (version == 2) ? PIX_FMT_YUV420P : PIX_FMT_GRAY8; 318 avctx->pix_fmt = PIX_FMT_YUV420P;
316 planes = (version == 2) ? 3 : 1; 319 planes = 3;
317 f->reference = 1; 320 f->reference = 1;
318 f->buffer_hints = FF_BUFFER_HINTS_VALID | 321 f->buffer_hints = FF_BUFFER_HINTS_VALID |
319 FF_BUFFER_HINTS_PRESERVE | 322 FF_BUFFER_HINTS_PRESERVE |
320 FF_BUFFER_HINTS_REUSABLE; 323 FF_BUFFER_HINTS_REUSABLE;
321 if (avctx->reget_buffer(avctx, f)) { 324 if (avctx->reget_buffer(avctx, f)) {
342 } 345 }
343 } 346 }
344 offs[planes] = buf_size; 347 offs[planes] = buf_size;
345 for(i = 0; i < planes; i++){ 348 for(i = 0; i < planes; i++){
346 is_chroma = !!i; 349 is_chroma = !!i;
347 s->tmpbuf = av_realloc(s->tmpbuf, offs[i + 1] - offs[i] - 1024); 350 s->tmpbuf = av_realloc(s->tmpbuf, offs[i + 1] - offs[i] - 1024 + FF_INPUT_BUFFER_PADDING_SIZE);
348 if(fraps2_decode_plane(s, f->data[i], f->linesize[i], avctx->width >> is_chroma, 351 if(fraps2_decode_plane(s, f->data[i], f->linesize[i], avctx->width >> is_chroma,
349 avctx->height >> is_chroma, buf + offs[i], offs[i + 1] - offs[i], is_chroma) < 0) { 352 avctx->height >> is_chroma, buf + offs[i], offs[i + 1] - offs[i], is_chroma) < 0) {
350 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i); 353 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
351 return -1; 354 return -1;
352 } 355 }