OLD | NEW |
1 /* | 1 /* |
2 * common functions for Indeo Video Interactive codecs (Indeo4 and Indeo5) | 2 * common functions for Indeo Video Interactive codecs (Indeo4 and Indeo5) |
3 * | 3 * |
4 * Copyright (c) 2009 Maxim Poliakovski | 4 * Copyright (c) 2009 Maxim Poliakovski |
5 * | 5 * |
6 * This file is part of FFmpeg. | 6 * This file is part of FFmpeg. |
7 * | 7 * |
8 * FFmpeg is free software; you can redistribute it and/or | 8 * FFmpeg is free software; you can redistribute it and/or |
9 * modify it under the terms of the GNU Lesser General Public | 9 * modify it under the terms of the GNU Lesser General Public |
10 * License as published by the Free Software Foundation; either | 10 * License as published by the Free Software Foundation; either |
(...skipping 15 matching lines...) Expand all Loading... |
26 * Indeo5 decoders. | 26 * Indeo5 decoders. |
27 */ | 27 */ |
28 | 28 |
29 #define ALT_BITSTREAM_READER_LE | 29 #define ALT_BITSTREAM_READER_LE |
30 #include "avcodec.h" | 30 #include "avcodec.h" |
31 #include "get_bits.h" | 31 #include "get_bits.h" |
32 #include "ivi_common.h" | 32 #include "ivi_common.h" |
33 #include "libavutil/common.h" | 33 #include "libavutil/common.h" |
34 #include "ivi_dsp.h" | 34 #include "ivi_dsp.h" |
35 | 35 |
| 36 extern const IVIHuffDesc ff_ivi_mb_huff_desc[8]; ///< static macroblock huffman
tables |
| 37 extern const IVIHuffDesc ff_ivi_blk_huff_desc[8]; ///< static block huffman tabl
es |
| 38 |
| 39 VLC ff_ivi_mb_vlc_tabs [8]; |
| 40 VLC ff_ivi_blk_vlc_tabs[8]; |
| 41 |
36 /** | 42 /** |
37 * Reverses "nbits" bits of the value "val" and returns the result | 43 * Reverses "nbits" bits of the value "val" and returns the result |
38 * in the least significant bits. | 44 * in the least significant bits. |
39 */ | 45 */ |
40 static uint16_t inv_bits(uint16_t val, int nbits) | 46 static uint16_t inv_bits(uint16_t val, int nbits) |
41 { | 47 { |
42 uint16_t res; | 48 uint16_t res; |
43 | 49 |
44 if (nbits <= 8) { | 50 if (nbits <= 8) { |
45 res = av_reverse[val] >> (8-nbits); | 51 res = av_reverse[val] >> (8-nbits); |
(...skipping 27 matching lines...) Expand all Loading... |
73 codewords[pos] = inv_bits((prefix | j), bits[pos]); | 79 codewords[pos] = inv_bits((prefix | j), bits[pos]); |
74 if (!bits[pos]) | 80 if (!bits[pos]) |
75 bits[pos] = 1; | 81 bits[pos] = 1; |
76 | 82 |
77 pos++; | 83 pos++; |
78 }//for j | 84 }//for j |
79 }//for i | 85 }//for i |
80 | 86 |
81 /* number of codewords = pos */ | 87 /* number of codewords = pos */ |
82 return init_vlc(vlc, IVI_VLC_BITS, pos, bits, 1, 1, codewords, 2, 2, | 88 return init_vlc(vlc, IVI_VLC_BITS, pos, bits, 1, 1, codewords, 2, 2, |
83 (flag & 1) | INIT_VLC_LE); | 89 (flag ? INIT_VLC_USE_NEW_STATIC : 0) | INIT_VLC_LE); |
84 } | 90 } |
85 | 91 |
86 int ff_ivi_dec_huff_desc(GetBitContext *gb, IVIHuffDesc *desc) | 92 void ff_ivi_init_static_vlc(void) |
87 { | 93 { |
88 int tab_sel, i; | 94 int i; |
| 95 static VLC_TYPE table_data[8192 * 16][2]; |
| 96 static int initialized_vlcs = 0; |
89 | 97 |
90 tab_sel = get_bits(gb, 3); | 98 if (initialized_vlcs) |
91 if (tab_sel == 7) { | 99 return; |
92 /* custom huffman table (explicitly encoded) */ | 100 for (i = 0; i < 8; i++) { |
93 desc->num_rows = get_bits(gb, 4); | 101 ff_ivi_mb_vlc_tabs[i].table = table_data + i * 2 * 8192; |
| 102 ff_ivi_mb_vlc_tabs[i].table_allocated = 8192; |
| 103 ff_ivi_create_huff_from_desc(&ff_ivi_mb_huff_desc[i], &ff_ivi_mb_vlc_ta
bs[i], 1); |
| 104 ff_ivi_blk_vlc_tabs[i].table = table_data + (i * 2 + 1) * 8192; |
| 105 ff_ivi_blk_vlc_tabs[i].table_allocated = 8192; |
| 106 ff_ivi_create_huff_from_desc(&ff_ivi_blk_huff_desc[i], &ff_ivi_blk_vlc_t
abs[i], 1); |
| 107 } |
| 108 initialized_vlcs = 1; |
| 109 } |
94 | 110 |
95 for (i = 0; i < desc->num_rows; i++) | 111 int ff_ivi_dec_huff_desc(GetBitContext *gb, int desc_coded, int which_tab, |
96 desc->xbits[i] = get_bits(gb, 4); | 112 IVIHuffTab *huff_tab, AVCodecContext *avctx) |
| 113 { |
| 114 int i, result; |
| 115 IVIHuffDesc new_huff; |
| 116 |
| 117 if (!desc_coded) { |
| 118 /* select default table */ |
| 119 huff_tab->tab = (which_tab) ? &ff_ivi_blk_vlc_tabs[7] |
| 120 : &ff_ivi_mb_vlc_tabs [7]; |
| 121 } else { |
| 122 huff_tab->tab_sel = get_bits(gb, 3); |
| 123 if (huff_tab->tab_sel == 7) { |
| 124 /* custom huffman table (explicitly encoded) */ |
| 125 new_huff.num_rows = get_bits(gb, 4); |
| 126 |
| 127 for (i = 0; i < new_huff.num_rows; i++) |
| 128 new_huff.xbits[i] = get_bits(gb, 4); |
| 129 |
| 130 /* Have we got the same custom table? Rebuild if not. */ |
| 131 if (ff_ivi_huff_desc_cmp(&new_huff, &huff_tab->cust_desc)) { |
| 132 ff_ivi_huff_desc_copy(&huff_tab->cust_desc, &new_huff); |
| 133 |
| 134 if (huff_tab->cust_tab.table) |
| 135 free_vlc(&huff_tab->cust_tab); |
| 136 result = ff_ivi_create_huff_from_desc(&huff_tab->cust_desc, |
| 137 &huff_tab->cust_tab, 0); |
| 138 if (result) { |
| 139 av_log(avctx, AV_LOG_ERROR, |
| 140 "Error while initializing custom vlc table!\n"); |
| 141 return -1; |
| 142 } |
| 143 } |
| 144 huff_tab->tab = &huff_tab->cust_tab; |
| 145 } else { |
| 146 /* select one of predefined tables */ |
| 147 huff_tab->tab = (which_tab) ? &ff_ivi_blk_vlc_tabs[huff_tab->tab_sel
] |
| 148 : &ff_ivi_mb_vlc_tabs [huff_tab->tab_sel]; |
| 149 } |
97 } | 150 } |
98 | 151 |
99 return tab_sel; | 152 return 0; |
100 } | 153 } |
101 | 154 |
102 int ff_ivi_huff_desc_cmp(const IVIHuffDesc *desc1, const IVIHuffDesc *desc2) | 155 int ff_ivi_huff_desc_cmp(const IVIHuffDesc *desc1, const IVIHuffDesc *desc2) |
103 { | 156 { |
104 return desc1->num_rows != desc2->num_rows | 157 return desc1->num_rows != desc2->num_rows |
105 || memcmp(desc1->xbits, desc2->xbits, desc1->num_rows); | 158 || memcmp(desc1->xbits, desc2->xbits, desc1->num_rows); |
106 } | 159 } |
107 | 160 |
108 void ff_ivi_huff_desc_copy(IVIHuffDesc *dst, const IVIHuffDesc *src) | 161 void ff_ivi_huff_desc_copy(IVIHuffDesc *dst, const IVIHuffDesc *src) |
109 { | 162 { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 if (!band->bufs[0] || !band->bufs[1]) | 212 if (!band->bufs[0] || !band->bufs[1]) |
160 return AVERROR(ENOMEM); | 213 return AVERROR(ENOMEM); |
161 | 214 |
162 /* allocate the 3rd band buffer for scalability mode */ | 215 /* allocate the 3rd band buffer for scalability mode */ |
163 if (cfg->luma_bands > 1) { | 216 if (cfg->luma_bands > 1) { |
164 band->bufs[2] = av_malloc(buf_size); | 217 band->bufs[2] = av_malloc(buf_size); |
165 if (!band->bufs[2]) | 218 if (!band->bufs[2]) |
166 return AVERROR(ENOMEM); | 219 return AVERROR(ENOMEM); |
167 } | 220 } |
168 | 221 |
169 planes[p].bands[0].huff_desc.num_rows = 0; /* reset custom vlc */ | 222 planes[p].bands[0].blk_vlc.cust_desc.num_rows = 0; /* reset custom v
lc */ |
170 } | 223 } |
171 } | 224 } |
172 | 225 |
173 return 0; | 226 return 0; |
174 } | 227 } |
175 | 228 |
176 void av_cold ff_ivi_free_buffers(IVIPlaneDesc *planes) | 229 void av_cold ff_ivi_free_buffers(IVIPlaneDesc *planes) |
177 { | 230 { |
178 int p, b, t; | 231 int p, b, t; |
179 | 232 |
180 for (p = 0; p < 3; p++) { | 233 for (p = 0; p < 3; p++) { |
181 for (b = 0; b < planes[p].num_bands; b++) { | 234 for (b = 0; b < planes[p].num_bands; b++) { |
182 av_freep(&planes[p].bands[b].bufs[0]); | 235 av_freep(&planes[p].bands[b].bufs[0]); |
183 av_freep(&planes[p].bands[b].bufs[1]); | 236 av_freep(&planes[p].bands[b].bufs[1]); |
184 av_freep(&planes[p].bands[b].bufs[2]); | 237 av_freep(&planes[p].bands[b].bufs[2]); |
185 | 238 |
| 239 if (planes[p].bands[b].blk_vlc.cust_tab.table) |
| 240 free_vlc(&planes[p].bands[b].blk_vlc.cust_tab); |
186 for (t = 0; t < planes[p].bands[b].num_tiles; t++) | 241 for (t = 0; t < planes[p].bands[b].num_tiles; t++) |
187 av_freep(&planes[p].bands[b].tiles[t].mbs); | 242 av_freep(&planes[p].bands[b].tiles[t].mbs); |
188 av_freep(&planes[p].bands[b].tiles); | 243 av_freep(&planes[p].bands[b].tiles); |
189 } | 244 } |
190 av_freep(&planes[p].bands); | 245 av_freep(&planes[p].bands); |
191 } | 246 } |
192 } | 247 } |
193 | 248 |
194 int av_cold ff_ivi_init_tiles(IVIPlaneDesc *planes, int tile_width, int tile_hei
ght) | 249 int av_cold ff_ivi_init_tiles(IVIPlaneDesc *planes, int tile_width, int tile_hei
ght) |
195 { | 250 { |
196 int p, b, x, y, x_tiles, y_tiles, t_width, t_height; | 251 int p, b, x, y, x_tiles, y_tiles, t_width, t_height; |
197 IVIBandDesc *band; | 252 IVIBandDesc *band; |
198 IVITile *tile, *ref_tile; | 253 IVITile *tile, *ref_tile; |
199 | 254 |
200 for (p = 0; p < 3; p++) { | 255 for (p = 0; p < 3; p++) { |
201 t_width = !p ? tile_width : (tile_width + 3) >> 2; | 256 t_width = !p ? tile_width : (tile_width + 3) >> 2; |
202 t_height = !p ? tile_height : (tile_height + 3) >> 2; | 257 t_height = !p ? tile_height : (tile_height + 3) >> 2; |
203 | 258 |
| 259 if (!p && planes[0].num_bands == 4) { |
| 260 t_width >>= 1; |
| 261 t_height >>= 1; |
| 262 } |
| 263 |
204 for (b = 0; b < planes[p].num_bands; b++) { | 264 for (b = 0; b < planes[p].num_bands; b++) { |
205 band = &planes[p].bands[b]; | 265 band = &planes[p].bands[b]; |
206 x_tiles = IVI_NUM_TILES(band->width, t_width); | 266 x_tiles = IVI_NUM_TILES(band->width, t_width); |
207 y_tiles = IVI_NUM_TILES(band->height, t_height); | 267 y_tiles = IVI_NUM_TILES(band->height, t_height); |
208 band->num_tiles = x_tiles * y_tiles; | 268 band->num_tiles = x_tiles * y_tiles; |
209 | 269 |
210 av_freep(&band->tiles); | 270 av_freep(&band->tiles); |
211 band->tiles = av_mallocz(band->num_tiles * sizeof(IVITile)); | 271 band->tiles = av_mallocz(band->num_tiles * sizeof(IVITile)); |
212 if (!band->tiles) | 272 if (!band->tiles) |
213 return AVERROR(ENOMEM); | 273 return AVERROR(ENOMEM); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 buf_offs -= blk_size; | 384 buf_offs -= blk_size; |
325 buf_offs += blk_size * band->pitch; | 385 buf_offs += blk_size * band->pitch; |
326 } | 386 } |
327 | 387 |
328 if (cbp & 1) { /* block coded ? */ | 388 if (cbp & 1) { /* block coded ? */ |
329 scan_pos = -1; | 389 scan_pos = -1; |
330 memset(trvec, 0, num_coeffs*sizeof(trvec[0])); /* zero transform
vector */ | 390 memset(trvec, 0, num_coeffs*sizeof(trvec[0])); /* zero transform
vector */ |
331 memset(col_flags, 0, sizeof(col_flags)); /* zero column fla
gs */ | 391 memset(col_flags, 0, sizeof(col_flags)); /* zero column fla
gs */ |
332 | 392 |
333 while (scan_pos <= num_coeffs) { | 393 while (scan_pos <= num_coeffs) { |
334 sym = get_vlc2(gb, band->blk_vlc->table, IVI_VLC_BITS, 1); | 394 sym = get_vlc2(gb, band->blk_vlc.tab->table, IVI_VLC_BITS, 1
); |
335 if (sym == rvmap->eob_sym) | 395 if (sym == rvmap->eob_sym) |
336 break; /* End of block */ | 396 break; /* End of block */ |
337 | 397 |
338 if (sym == rvmap->esc_sym) { /* Escape - run/val explicitly
coded using 3 vlc codes */ | 398 if (sym == rvmap->esc_sym) { /* Escape - run/val explicitly
coded using 3 vlc codes */ |
339 run = get_vlc2(gb, band->blk_vlc->table, IVI_VLC_BITS, 1
) + 1; | 399 run = get_vlc2(gb, band->blk_vlc.tab->table, IVI_VLC_BIT
S, 1) + 1; |
340 lo = get_vlc2(gb, band->blk_vlc->table, IVI_VLC_BITS, 1
); | 400 lo = get_vlc2(gb, band->blk_vlc.tab->table, IVI_VLC_BIT
S, 1); |
341 hi = get_vlc2(gb, band->blk_vlc->table, IVI_VLC_BITS, 1
); | 401 hi = get_vlc2(gb, band->blk_vlc.tab->table, IVI_VLC_BIT
S, 1); |
342 val = IVI_TOSIGNED((hi << 6) | lo); /* merge them and co
nvert into signed val */ | 402 val = IVI_TOSIGNED((hi << 6) | lo); /* merge them and co
nvert into signed val */ |
343 } else { | 403 } else { |
344 run = rvmap->runtab[sym]; | 404 run = rvmap->runtab[sym]; |
345 val = rvmap->valtab[sym]; | 405 val = rvmap->valtab[sym]; |
346 } | 406 } |
347 | 407 |
348 /* de-zigzag and dequantize */ | 408 /* de-zigzag and dequantize */ |
349 scan_pos += run; | 409 scan_pos += run; |
350 if (scan_pos >= num_coeffs) | 410 if (scan_pos >= num_coeffs) |
351 break; | 411 break; |
352 pos = band->scan[scan_pos]; | 412 pos = band->scan[scan_pos]; |
353 | 413 |
354 if (IVI_DEBUG && !val) | 414 if (IVI_DEBUG && !val) |
355 av_log(NULL, AV_LOG_ERROR, "Val = 0 encountered!\n"); | 415 av_log(NULL, AV_LOG_ERROR, "Val = 0 encountered!\n"); |
356 | 416 |
357 q = (base_tab[pos] * scale_tab[quant]) >> 8; | 417 q = (base_tab[pos] * scale_tab[quant]) >> 8; |
358 if (q > 1) | 418 if (q > 1) |
359 val = val * q + FFSIGN(val) * ((q >> 1) - (q & 1)); | 419 val = val * q + FFSIGN(val) * ((q >> 1) - (q & 1)); |
360 trvec[pos] = val; | 420 trvec[pos] = val; |
361 col_flags[pos & col_mask] |= val; /* track columns containin
g non-zero coeffs */ | 421 col_flags[pos & col_mask] |= !!val; /* track columns contain
ing non-zero coeffs */ |
362 }// while | 422 }// while |
363 | 423 |
364 if (scan_pos >= num_coeffs && sym != rvmap->eob_sym) | 424 if (scan_pos >= num_coeffs && sym != rvmap->eob_sym) |
365 return -1; /* corrupt block data */ | 425 return -1; /* corrupt block data */ |
366 | 426 |
367 /* undoing DC coeff prediction for intra-blocks */ | 427 /* undoing DC coeff prediction for intra-blocks */ |
368 if (is_intra && band->is_2d_trans) { | 428 if (is_intra && band->is_2d_trans) { |
369 prev_dc += trvec[0]; | 429 prev_dc += trvec[0]; |
370 trvec[0] = prev_dc; | 430 trvec[0] = prev_dc; |
371 col_flags[0] |= !!prev_dc; | 431 col_flags[0] |= !!prev_dc; |
(...skipping 559 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
931 -17, -3, 1, 1, 1, 4, -1, -1, -4, 3, -1, 5, -3, -1, -9,
9, | 991 -17, -3, 1, 1, 1, 4, -1, -1, -4, 3, -1, 5, -3, -1, -9,
9, |
932 -5, 1, 18, -18, 2, 1, -2, 1, -1, -1, 1, 19, -1, 1, -19,
-1, | 992 -5, 1, 18, -18, 2, 1, -2, 1, -1, -1, 1, 19, -1, 1, -19,
-1, |
933 4, 1, -1, 1, 7, -4, -2, 2, -7, 10, -1, -10, 1, 20, -1, -
20, | 993 4, 1, -1, 1, 7, -4, -2, 2, -7, 10, -1, -10, 1, 20, -1, -
20, |
934 1, -1, 2, 4, -2, 5, 1, -5, 6, -4, 21, 4, 2, -6, -21,
-1, | 994 1, -1, 2, 4, -2, 5, 1, -5, 6, -4, 21, 4, 2, -6, -21,
-1, |
935 -2, 1, -4, -1, -3, 22, -1, 1, 3, -22, -1, 11, -11, 1, 1,
1, | 995 -2, 1, -4, -1, -3, 22, -1, 1, 3, -22, -1, 11, -11, 1, 1,
1, |
936 8, -8, 2, 2, -1, -2, -2, -1, 1, -1, -5, 5, 2, 23, -23,
-2, | 996 8, -8, 2, 2, -1, -2, -2, -1, 1, -1, -5, 5, 2, 23, -23,
-2, |
937 1, -1, 24, -24, -1, -1, 7, 6, -7, 5, -6, 12, -3, 3, 1,
-5, | 997 1, -1, 24, -24, -1, -1, 7, 6, -7, 5, -6, 12, -3, 3, 1,
-5, |
938 1, 1, -12, 25, -1, -5, 5, -25, -1, 1, 9, 1, -1, -9, 26, -
26} | 998 1, 1, -12, 25, -1, -5, 5, -25, -1, 1, 9, 1, -1, -9, 26, -
26} |
939 } | 999 } |
940 }; | 1000 }; |
OLD | NEW |