FFmpeg
Loading...
Searching...
No Matches
intrax8.c
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19/**
20 * @file
21 * @brief IntraX8 (J-Frame) subdecoder, used by WMV2 and VC-1
22 */
23
24#include "libavutil/avassert.h"
25#include "libavutil/mem.h"
26#include "libavutil/thread.h"
27#include "avcodec.h"
28#include "get_bits.h"
29#include "idctdsp.h"
30#include "msmpeg4_vc1_data.h"
31#include "intrax8huf.h"
32#include "intrax8.h"
33#include "intrax8dsp.h"
34#include "mpegutils.h"
35
36#define VLC_BUFFER_SIZE 28150
37
38#define MAX_TABLE_DEPTH(table_bits, max_bits) \
39 ((max_bits + table_bits - 1) / table_bits)
40
41#define DC_VLC_BITS 9
42#define AC_VLC_BITS 9
43#define OR_VLC_BITS 7
44
45#define DC_VLC_MTD MAX_TABLE_DEPTH(DC_VLC_BITS, MAX_DC_VLC_BITS)
46#define AC_VLC_MTD MAX_TABLE_DEPTH(AC_VLC_BITS, MAX_AC_VLC_BITS)
47#define OR_VLC_MTD MAX_TABLE_DEPTH(OR_VLC_BITS, MAX_OR_VLC_BITS)
48
49static const VLCElem *j_ac_vlc[2][2][8]; // [quant < 13], [intra / inter], [select]
50static const VLCElem *j_dc_vlc[2][8]; // [quant], [select]
51static const VLCElem *j_orient_vlc[2][4]; // [quant], [select]
52
53static av_cold const VLCElem *x8_init_vlc(VLCInitState *state, int nb_bits,
54 int nb_codes, const uint8_t table[][2])
55{
56 return ff_vlc_init_tables_from_lengths(state, nb_bits, nb_codes, &table[0][1], 2,
57 &table[0][0], 2, 1, 0, 0);
58}
59
60static av_cold void x8_vlc_init(void)
61{
62 static VLCElem vlc_buf[VLC_BUFFER_SIZE];
64 int i;
65
66// set ac tables
67 for (int i = 0; i < 2; i++)
68 for (int j = 0; j < 2; j++)
69 for (int k = 0; k < 8; k++)
70 j_ac_vlc[i][j][k] = x8_init_vlc(&state, AC_VLC_BITS, 77,
71 x8_ac_quant_table[i][j][k]);
72
73// set dc tables
74 for (int i = 0; i < 2; i++)
75 for (int j = 0; j < 8; j++)
78
79// set orient tables
80 for (i = 0; i < 2; i++)
83 for (i = 0; i < 4; i++)
86}
87
89{
90 memset(w->j_dc_vlc_table, 0, sizeof(w->j_dc_vlc_table));
91 memset(w->j_ac_vlc_table, 0, sizeof(w->j_ac_vlc_table));
92 w->j_orient_vlc_table = NULL;
93}
94
95static inline void x8_select_ac_table(IntraX8Context *const w, int mode)
96{
97 int table_index;
98
99 av_assert2(mode < 4);
100
101 if (w->j_ac_vlc_table[mode])
102 return;
103
104 table_index = get_bits(w->gb, 3);
105 // 2 modes use same tables
106 w->j_ac_vlc_table[mode] = j_ac_vlc[w->quant < 13][mode >> 1][table_index];
107 av_assert2(w->j_ac_vlc_table[mode]);
108}
109
111{
112 if (!w->j_orient_vlc_table) {
113 int table_index = get_bits(w->gb, 1 + (w->quant < 13));
114 w->j_orient_vlc_table = j_orient_vlc[w->quant < 13][table_index];
115 }
116
117 return get_vlc2(w->gb, w->j_orient_vlc_table, OR_VLC_BITS, OR_VLC_MTD);
118}
119
120#define extra_bits(eb) (eb) // 3 bits
121#define extra_run (0xFF << 8) // 1 bit
122#define extra_level (0x00 << 8) // 1 bit
123#define run_offset(r) ((r) << 16) // 6 bits
124#define level_offset(l) ((l) << 24) // 5 bits
125static const uint32_t ac_decode_table[] = {
126 /* 46 */ extra_bits(3) | extra_run | run_offset(16) | level_offset(0),
127 /* 47 */ extra_bits(3) | extra_run | run_offset(24) | level_offset(0),
128 /* 48 */ extra_bits(2) | extra_run | run_offset(4) | level_offset(1),
129 /* 49 */ extra_bits(3) | extra_run | run_offset(8) | level_offset(1),
130
131 /* 50 */ extra_bits(5) | extra_run | run_offset(32) | level_offset(0),
132 /* 51 */ extra_bits(4) | extra_run | run_offset(16) | level_offset(1),
133
134 /* 52 */ extra_bits(2) | extra_level | run_offset(0) | level_offset(4),
135 /* 53 */ extra_bits(2) | extra_level | run_offset(0) | level_offset(8),
136 /* 54 */ extra_bits(2) | extra_level | run_offset(0) | level_offset(12),
137 /* 55 */ extra_bits(3) | extra_level | run_offset(0) | level_offset(16),
138 /* 56 */ extra_bits(3) | extra_level | run_offset(0) | level_offset(24),
139
140 /* 57 */ extra_bits(2) | extra_level | run_offset(1) | level_offset(3),
141 /* 58 */ extra_bits(3) | extra_level | run_offset(1) | level_offset(7),
142
143 /* 59 */ extra_bits(2) | extra_run | run_offset(16) | level_offset(0),
144 /* 60 */ extra_bits(2) | extra_run | run_offset(20) | level_offset(0),
145 /* 61 */ extra_bits(2) | extra_run | run_offset(24) | level_offset(0),
146 /* 62 */ extra_bits(2) | extra_run | run_offset(28) | level_offset(0),
147 /* 63 */ extra_bits(4) | extra_run | run_offset(32) | level_offset(0),
148 /* 64 */ extra_bits(4) | extra_run | run_offset(48) | level_offset(0),
149
150 /* 65 */ extra_bits(2) | extra_run | run_offset(4) | level_offset(1),
151 /* 66 */ extra_bits(3) | extra_run | run_offset(8) | level_offset(1),
152 /* 67 */ extra_bits(4) | extra_run | run_offset(16) | level_offset(1),
153
154 /* 68 */ extra_bits(2) | extra_level | run_offset(0) | level_offset(4),
155 /* 69 */ extra_bits(3) | extra_level | run_offset(0) | level_offset(8),
156 /* 70 */ extra_bits(4) | extra_level | run_offset(0) | level_offset(16),
157
158 /* 71 */ extra_bits(2) | extra_level | run_offset(1) | level_offset(3),
159 /* 72 */ extra_bits(3) | extra_level | run_offset(1) | level_offset(7),
160};
161#undef extra_bits
162#undef extra_run
163#undef extra_level
164#undef run_offset
165#undef level_offset
166
167static void x8_get_ac_rlf(IntraX8Context *const w, const int mode,
168 int *const run, int *const level, int *const final)
169{
170 int i, e;
171
172// x8_select_ac_table(w, mode);
173 i = get_vlc2(w->gb, w->j_ac_vlc_table[mode], AC_VLC_BITS, AC_VLC_MTD);
174
175 if (i < 46) { // [0-45]
176 int t, l;
177 if (i < 0) {
178 *level =
179 *final = // prevent 'may be used uninitialized'
180 *run = 64; // this would cause error exit in the ac loop
181 return;
182 }
183
184 /*
185 * i == 0-15 r = 0-15 l = 0; r = i & %01111
186 * i == 16-19 r = 0-3 l = 1; r = i & %00011
187 * i == 20-21 r = 0-1 l = 2; r = i & %00001
188 * i == 22 r = 0 l = 3; r = i & %00000
189 */
190
191 *final =
192 t = i > 22;
193 i -= 23 * t;
194
195 /* l = lut_l[i / 2] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3 }[i >> 1];
196 * 11 10'01 01'00 00'00 00'00 00'00 00 => 0xE50000 */
197 l = (0xE50000 >> (i & 0x1E)) & 3; // 0x1E or ~1 or (i >> 1 << 1)
198
199 /* t = lut_mask[l] = { 0x0f, 0x03, 0x01, 0x00 }[l];
200 * as i < 256 the higher bits do not matter */
201 t = 0x01030F >> (l << 3);
202
203 *run = i & t;
204 *level = l;
205 } else if (i < 73) { // [46-72]
206 uint32_t sm;
207 uint32_t mask;
208
209 i -= 46;
210 sm = ac_decode_table[i];
211
212 e = get_bits(w->gb, sm & 0xF);
213 sm >>= 8; // 3 bits
214 mask = sm & 0xff;
215 sm >>= 8; // 1 bit
216
217 *run = (sm & 0xff) + (e & mask); // 6 bits
218 *level = (sm >> 8) + (e & ~mask); // 5 bits
219 *final = i > (58 - 46);
220 } else if (i < 75) { // [73-74]
221 static const uint8_t crazy_mix_runlevel[32] = {
222 0x22, 0x32, 0x33, 0x53, 0x23, 0x42, 0x43, 0x63,
223 0x24, 0x52, 0x34, 0x73, 0x25, 0x62, 0x44, 0x83,
224 0x26, 0x72, 0x35, 0x54, 0x27, 0x82, 0x45, 0x64,
225 0x28, 0x92, 0x36, 0x74, 0x29, 0xa2, 0x46, 0x84,
226 };
227
228 *final = !(i & 1);
229 e = get_bits(w->gb, 5); // get the extra bits
230 *run = crazy_mix_runlevel[e] >> 4;
231 *level = crazy_mix_runlevel[e] & 0x0F;
232 } else {
233 *level = get_bits(w->gb, 7 - 3 * (i & 1));
234 *run = get_bits(w->gb, 6);
235 *final = get_bits1(w->gb);
236 }
237 return;
238}
239
240/* static const uint8_t dc_extra_sbits[] = {
241 * 0, 1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7,
242 * }; */
243static const uint8_t dc_index_offset[] = {
244 0, 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
245};
246
247static int x8_get_dc_rlf(IntraX8Context *const w, const int mode,
248 int *const level, int *const final)
249{
250 int i, e, c;
251
252 av_assert2(mode < 3);
253 if (!w->j_dc_vlc_table[mode]) {
254 int table_index = get_bits(w->gb, 3);
255 // 4 modes, same table
256 w->j_dc_vlc_table[mode] = j_dc_vlc[w->quant < 13][table_index];
257 }
258
259 i = get_vlc2(w->gb, w->j_dc_vlc_table[mode], DC_VLC_BITS, DC_VLC_MTD);
260
261 /* (i >= 17) { i -= 17; final =1; } */
262 c = i > 16;
263 *final = c;
264 i -= 17 * c;
265
266 if (i <= 0) {
267 *level = 0;
268 return -i;
269 }
270 c = (i + 1) >> 1; // hackish way to calculate dc_extra_sbits[]
271 c -= c > 1;
272
273 e = get_bits(w->gb, c); // get the extra bits
274 i = dc_index_offset[i] + (e >> 1);
275
276 e = -(e & 1); // 0, 0xffffff
277 *level = (i ^ e) - e; // (i ^ 0) - 0, (i ^ 0xff) - (-1)
278 return 0;
279}
280
281// end of huffman
282
284{
285 int range;
286 int sum;
287 int quant;
288
289 w->dsp.setup_spatial_compensation(w->dest[chroma], w->scratchpad,
290 w->frame->linesize[chroma > 0],
291 &range, &sum, w->edges);
292 if (chroma) {
293 w->orient = w->chroma_orient;
294 quant = w->quant_dc_chroma;
295 } else {
296 quant = w->quant;
297 }
298
299 w->flat_dc = 0;
300 if (range < quant || range < 3) {
301 w->orient = 0;
302
303 // yep you read right, a +-1 idct error may break decoding!
304 if (range < 3) {
305 w->flat_dc = 1;
306 sum += 9;
307 // ((1 << 17) + 9) / (8 + 8 + 1 + 2) = 6899
308 w->predicted_dc = sum * 6899 >> 17;
309 }
310 }
311 if (chroma)
312 return 0;
313
314 av_assert2(w->orient < 3);
316 if ((w->edges & 3) == 0) {
317 if (w->orient == 1)
318 w->orient = 11;
319 if (w->orient == 2)
320 w->orient = 10;
321 } else {
322 w->orient = 0;
323 }
324 w->raw_orient = 0;
325 } else {
326 static const uint8_t prediction_table[3][12] = {
327 { 0, 8, 4, 10, 11, 2, 6, 9, 1, 3, 5, 7 },
328 { 4, 0, 8, 11, 10, 3, 5, 2, 6, 9, 1, 7 },
329 { 8, 0, 4, 10, 11, 1, 7, 2, 6, 9, 3, 5 },
330 };
331 w->raw_orient = x8_get_orient_vlc(w);
332 if (w->raw_orient < 0)
333 return -1;
334 av_assert2(w->raw_orient < 12);
335 av_assert2(w->orient < 3);
336 w->orient=prediction_table[w->orient][w->raw_orient];
337 }
338 return 0;
339}
340
341static void x8_update_predictions(IntraX8Context *const w, const int orient,
342 const int est_run)
343{
344 w->prediction_table[w->mb_x * 2 + (w->mb_y & 1)] = (est_run << 2) + 1 * (orient == 4) + 2 * (orient == 8);
345/*
346 * y = 2n + 0 -> // 0 2 4
347 * y = 2n + 1 -> // 1 3 5
348 */
349}
350
352{
353 w->edges = 1 * !(w->mb_x >> 1);
354 w->edges |= 2 * !(w->mb_y >> 1);
355 w->edges |= 4 * (w->mb_x >= (2 * w->mb_width - 1)); // mb_x for chroma would always be odd
356
357 w->raw_orient = 0;
358 // lut_co[8] = {inv,4,8,8, inv,4,8,8} <- => {1,1,0,0;1,1,0,0} => 0xCC
359 if (w->edges & 3) {
360 w->chroma_orient = 4 << ((0xCC >> w->edges) & 1);
361 return;
362 }
363 // block[x - 1][y | 1 - 1)]
364 w->chroma_orient = (w->prediction_table[2 * w->mb_x - 2] & 0x03) << 2;
365}
366
368{
369 int a, b, c, i;
370
371 w->edges = 1 * !w->mb_x;
372 w->edges |= 2 * !w->mb_y;
373 w->edges |= 4 * (w->mb_x >= (2 * w->mb_width - 1));
374
375 switch (w->edges & 3) {
376 case 0:
377 break;
378 case 1:
379 // take the one from the above block[0][y - 1]
380 w->est_run = w->prediction_table[!(w->mb_y & 1)] >> 2;
381 w->orient = 1;
382 return;
383 case 2:
384 // take the one from the previous block[x - 1][0]
385 w->est_run = w->prediction_table[2 * w->mb_x - 2] >> 2;
386 w->orient = 2;
387 return;
388 case 3:
389 w->est_run = 16;
390 w->orient = 0;
391 return;
392 }
393 // no edge cases
394 b = w->prediction_table[2 * w->mb_x + !(w->mb_y & 1)]; // block[x ][y - 1]
395 a = w->prediction_table[2 * w->mb_x - 2 + (w->mb_y & 1)]; // block[x - 1][y ]
396 c = w->prediction_table[2 * w->mb_x - 2 + !(w->mb_y & 1)]; // block[x - 1][y - 1]
397
398 w->est_run = FFMIN(b, a);
399 /* This condition has nothing to do with w->edges, even if it looks
400 * similar it would trigger if e.g. x = 3; y = 2;
401 * I guess somebody wrote something wrong and it became standard. */
402 if ((w->mb_x & w->mb_y) != 0)
403 w->est_run = FFMIN(c, w->est_run);
404 w->est_run >>= 2;
405
406 a &= 3;
407 b &= 3;
408 c &= 3;
409
410 i = (0xFFEAF4C4 >> (2 * b + 8 * a)) & 3;
411 if (i != 3)
412 w->orient = i;
413 else
414 w->orient = (0xFFEAD8 >> (2 * c + 8 * (w->quant > 12))) & 3;
415/*
416 * lut1[b][a] = {
417 * ->{ 0, 1, 0, pad },
418 * { 0, 1, X, pad },
419 * { 2, 2, 2, pad }
420 * }
421 * pad 2 2 2;
422 * pad X 1 0;
423 * pad 0 1 0 <-
424 * -> 11 10 '10 10 '11 11'01 00 '11 00'01 00 => 0xEAF4C4
425 *
426 * lut2[q>12][c] = {
427 * ->{ 0, 2, 1, pad},
428 * { 2, 2, 2, pad}
429 * }
430 * pad 2 2 2;
431 * pad 1 2 0 <-
432 * -> 11 10'10 10 '11 01'10 00 => 0xEAD8
433 */
434}
435
436static void x8_ac_compensation(IntraX8Context *const w, const int direction,
437 const int dc_level)
438{
439 int t;
440#define B(x,y) w->block[w->idsp.idct_permutation[(x) + (y) * 8]]
441#define T(x) ((x) * dc_level + 0x8000) >> 16;
442 switch (direction) {
443 case 0:
444 t = T(3811); // h
445 B(1, 0) -= t;
446 B(0, 1) -= t;
447
448 t = T(487); // e
449 B(2, 0) -= t;
450 B(0, 2) -= t;
451
452 t = T(506); // f
453 B(3, 0) -= t;
454 B(0, 3) -= t;
455
456 t = T(135); // c
457 B(4, 0) -= t;
458 B(0, 4) -= t;
459 B(2, 1) += t;
460 B(1, 2) += t;
461 B(3, 1) += t;
462 B(1, 3) += t;
463
464 t = T(173); // d
465 B(5, 0) -= t;
466 B(0, 5) -= t;
467
468 t = T(61); // b
469 B(6, 0) -= t;
470 B(0, 6) -= t;
471 B(5, 1) += t;
472 B(1, 5) += t;
473
474 t = T(42); // a
475 B(7, 0) -= t;
476 B(0, 7) -= t;
477 B(4, 1) += t;
478 B(1, 4) += t;
479 B(4, 4) += t;
480
481 t = T(1084); // g
482 B(1, 1) += t;
483 break;
484 case 1:
485 B(0, 1) -= T(6269);
486 B(0, 3) -= T(708);
487 B(0, 5) -= T(172);
488 B(0, 7) -= T(73);
489 break;
490 case 2:
491 B(1, 0) -= T(6269);
492 B(3, 0) -= T(708);
493 B(5, 0) -= T(172);
494 B(7, 0) -= T(73);
495 break;
496 }
497#undef B
498#undef T
499}
500
501static void dsp_x8_put_solidcolor(const uint8_t pix, uint8_t *dst,
502 const ptrdiff_t linesize)
503{
504 int k;
505 for (k = 0; k < 8; k++) {
506 memset(dst, pix, 8);
507 dst += linesize;
508 }
509}
510
511static const int16_t quant_table[64] = {
512 256, 256, 256, 256, 256, 256, 259, 262,
513 265, 269, 272, 275, 278, 282, 285, 288,
514 292, 295, 299, 303, 306, 310, 314, 317,
515 321, 325, 329, 333, 337, 341, 345, 349,
516 353, 358, 362, 366, 371, 375, 379, 384,
517 389, 393, 398, 403, 408, 413, 417, 422,
518 428, 433, 438, 443, 448, 454, 459, 465,
519 470, 476, 482, 488, 493, 499, 505, 511,
520};
521
522static int x8_decode_intra_mb(IntraX8Context *const w, const int chroma)
523{
524 uint8_t *scantable;
525 int final, run, level;
526 int ac_mode, dc_mode, est_run, dc_level;
527 int pos, n;
528 int zeros_only;
529 int use_quant_matrix;
530 int sign;
531
532 av_assert2(w->orient < 12);
533 w->bdsp.clear_block(w->block);
534
535 if (chroma)
536 dc_mode = 2;
537 else
538 dc_mode = !!w->est_run; // 0, 1
539
540 if (x8_get_dc_rlf(w, dc_mode, &dc_level, &final))
541 return -1;
542 n = 0;
543 zeros_only = 0;
544 if (!final) { // decode ac
545 use_quant_matrix = w->use_quant_matrix;
546 if (chroma) {
547 ac_mode = 1;
548 est_run = 64; // not used
549 } else {
550 if (w->raw_orient < 3)
551 use_quant_matrix = 0;
552
553 if (w->raw_orient > 4) {
554 ac_mode = 0;
555 est_run = 64;
556 } else {
557 if (w->est_run > 1) {
558 ac_mode = 2;
559 est_run = w->est_run;
560 } else {
561 ac_mode = 3;
562 est_run = 64;
563 }
564 }
565 }
566 x8_select_ac_table(w, ac_mode);
567 /* scantable_selector[12] = { 0, 2, 0, 1, 1, 1, 0, 2, 2, 0, 1, 2 }; <-
568 * -> 10'01' 00'10' 10'00' 01'01' 01'00' 10'00 => 0x928548 */
569 scantable = w->permutated_scantable[(0x928548 >> (2 * w->orient)) & 3];
570 pos = 0;
571 do {
572 n++;
573 if (n >= est_run) {
574 ac_mode = 3;
576 }
577
578 x8_get_ac_rlf(w, ac_mode, &run, &level, &final);
579
580 pos += run + 1;
581 if (pos > 63) {
582 // this also handles vlc error in x8_get_ac_rlf
583 return -1;
584 }
585 level = (level + 1) * w->dquant;
586 level += w->qsum;
587
588 sign = -get_bits1(w->gb);
589 level = (level ^ sign) - sign;
590
591 if (use_quant_matrix)
592 level = (level * quant_table[pos]) >> 8;
593
594 w->block[scantable[pos]] = level;
595 } while (!final);
596 } else { // DC only
597 if (w->flat_dc && ((unsigned) (dc_level + 1)) < 3) { // [-1; 1]
598 int32_t divide_quant = !chroma ? w->divide_quant_dc_luma
599 : w->divide_quant_dc_chroma;
600 int32_t dc_quant = !chroma ? w->quant
601 : w->quant_dc_chroma;
602
603 // original intent dc_level += predicted_dc/quant;
604 // but it got lost somewhere in the rounding
605 dc_level += (w->predicted_dc * divide_quant + (1 << 12)) >> 13;
606
607 dsp_x8_put_solidcolor(av_clip_uint8((dc_level * dc_quant + 4) >> 3),
608 w->dest[chroma],
609 w->frame->linesize[!!chroma]);
610
611 goto block_placed;
612 }
613 zeros_only = dc_level == 0;
614 }
615 if (!chroma)
616 w->block[0] = dc_level * w->quant;
617 else
618 w->block[0] = dc_level * w->quant_dc_chroma;
619
620 // there is !zero_only check in the original, but dc_level check is enough
621 if ((unsigned int) (dc_level + 1) >= 3 && (w->edges & 3) != 3) {
622 int direction;
623 /* ac_comp_direction[orient] = { 0, 3, 3, 1, 1, 0, 0, 0, 2, 2, 2, 1 }; <-
624 * -> 01'10' 10'10' 00'00' 00'01' 01'11' 11'00 => 0x6A017C */
625 direction = (0x6A017C >> (w->orient * 2)) & 3;
626 if (direction != 3) {
627 x8_ac_compensation(w, direction, w->block[0]);
628 }
629 }
630
631 if (w->flat_dc) {
632 dsp_x8_put_solidcolor(w->predicted_dc, w->dest[chroma],
633 w->frame->linesize[!!chroma]);
634 } else {
635 w->dsp.spatial_compensation[w->orient](w->scratchpad,
636 w->dest[chroma],
637 w->frame->linesize[!!chroma]);
638 }
639 if (!zeros_only)
640 w->idsp.idct_add(w->dest[chroma],
641 w->frame->linesize[!!chroma],
642 w->block);
643
644block_placed:
645 if (!chroma)
646 x8_update_predictions(w, w->orient, n);
647
648 if (w->loopfilter) {
649 uint8_t *ptr = w->dest[chroma];
650 ptrdiff_t linesize = w->frame->linesize[!!chroma];
651
652 if (!((w->edges & 2) || (zeros_only && (w->orient | 4) == 4)))
653 w->dsp.h_loop_filter(ptr, linesize, w->quant);
654
655 if (!((w->edges & 1) || (zeros_only && (w->orient | 8) == 8)))
656 w->dsp.v_loop_filter(ptr, linesize, w->quant);
657 }
658 return 0;
659}
660
661// FIXME maybe merge with ff_*
663{
664 // not parent codec linesize as this would be wrong for field pics
665 // not that IntraX8 has interlacing support ;)
666 const ptrdiff_t linesize = frame->linesize[0];
667 const ptrdiff_t uvlinesize = frame->linesize[1];
668
669 w->dest[0] = frame->data[0];
670 w->dest[1] = frame->data[1];
671 w->dest[2] = frame->data[2];
672
673 w->dest[0] += w->mb_y * linesize << 3;
674 // chroma blocks are on add rows
675 w->dest[1] += (w->mb_y & ~1) * uvlinesize << 2;
676 w->dest[2] += (w->mb_y & ~1) * uvlinesize << 2;
677}
678
681 int16_t block[64],
682 int mb_width, int mb_height)
683{
684 static AVOnce init_static_once = AV_ONCE_INIT;
685
686 w->avctx = avctx;
687 w->mb_width = mb_width;
688 w->mb_height = mb_height;
689 w->block = block;
690
691 // two rows, 2 blocks per cannon mb
692 w->prediction_table = av_mallocz(w->mb_width * 2 * 2);
693 if (!w->prediction_table)
694 return AVERROR(ENOMEM);
695
696 ff_wmv2dsp_init(&w->idsp);
697
698 ff_permute_scantable(w->permutated_scantable[0], ff_wmv1_scantable[0],
699 w->idsp.idct_permutation);
700 ff_permute_scantable(w->permutated_scantable[1], ff_wmv1_scantable[2],
701 w->idsp.idct_permutation);
702 ff_permute_scantable(w->permutated_scantable[2], ff_wmv1_scantable[3],
703 w->idsp.idct_permutation);
704
705 ff_intrax8dsp_init(&w->dsp);
706 ff_blockdsp_init(&w->bdsp);
707
708 ff_thread_once(&init_static_once, x8_vlc_init);
709
710 return 0;
711}
712
714{
715 av_freep(&w->prediction_table);
716}
717
719 GetBitContext *gb, int *mb_x, int *mb_y,
720 int dquant, int quant_offset,
721 int loopfilter, int lowdelay)
722{
723 int mb_xy;
724
725 w->gb = gb;
726 w->dquant = dquant;
727 w->quant = dquant >> 1;
728 w->qsum = quant_offset;
729 w->frame = pict->f;
730 w->loopfilter = loopfilter;
731 w->use_quant_matrix = get_bits1(w->gb);
732
733 w->mb_x = *mb_x;
734 w->mb_y = *mb_y;
735
736 w->divide_quant_dc_luma = ((1 << 16) + (w->quant >> 1)) / w->quant;
737 if (w->quant < 5) {
738 w->quant_dc_chroma = w->quant;
739 w->divide_quant_dc_chroma = w->divide_quant_dc_luma;
740 } else {
741 w->quant_dc_chroma = w->quant + ((w->quant + 3) >> 3);
742 w->divide_quant_dc_chroma = ((1 << 16) + (w->quant_dc_chroma >> 1)) / w->quant_dc_chroma;
743 }
745
746 for (w->mb_y = 0; w->mb_y < w->mb_height * 2; w->mb_y++) {
747 x8_init_block_index(w, w->frame);
748 mb_xy = (w->mb_y >> 1) * (w->mb_width + 1);
749 if (get_bits_left(gb) < 1)
750 goto error;
751 for (w->mb_x = 0; w->mb_x < w->mb_width * 2; w->mb_x++) {
754 goto error;
755 if (x8_decode_intra_mb(w, 0))
756 goto error;
757
758 if (w->mb_x & w->mb_y & 1) {
760
761 /* when setting up chroma, no vlc is read,
762 * so no error condition can be reached */
764 if (x8_decode_intra_mb(w, 1))
765 goto error;
766
768 if (x8_decode_intra_mb(w, 2))
769 goto error;
770
771 w->dest[1] += 8;
772 w->dest[2] += 8;
773
774 pict->qscale_table[mb_xy] = w->quant;
775 mb_xy++;
776 }
777 w->dest[0] += 8;
778 }
779 if (w->mb_y & 1)
780 ff_draw_horiz_band(w->avctx, w->frame, w->frame,
781 (w->mb_y - 1) * 8, 16,
782 PICT_FRAME, 0, lowdelay);
783 }
784
785error:
786 *mb_x = w->mb_x;
787 *mb_y = w->mb_y;
788
789 return 0;
790}
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
int32_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition avassert.h:68
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define av_clip_uint8
Definition common.h:106
#define NULL
Definition coverity.c:32
static int16_t block[64]
Definition dct.c:125
static AVFrame * frame
static struct @346255127015250356166251341105367306144006377143 state
bitstream reader API header.
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
Definition get_bits.h:645
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
#define AVERROR(e)
Definition error.h:45
int a
#define b
Definition input.c:43
#define DC_VLC_MTD
Definition intrax8.c:45
static void x8_update_predictions(IntraX8Context *const w, const int orient, const int est_run)
Definition intrax8.c:341
#define T(x)
av_cold int ff_intrax8_common_init(AVCodecContext *avctx, IntraX8Context *w, int16_t block[64], int mb_width, int mb_height)
Initialize IntraX8 frame decoder.
Definition intrax8.c:679
#define OR_VLC_MTD
Definition intrax8.c:47
static void x8_reset_vlc_tables(IntraX8Context *w)
Definition intrax8.c:88
#define extra_run
Definition intrax8.c:121
static const VLCElem * j_dc_vlc[2][8]
Definition intrax8.c:50
#define AC_VLC_BITS
Definition intrax8.c:42
static av_cold void x8_vlc_init(void)
Definition intrax8.c:60
#define VLC_BUFFER_SIZE
Definition intrax8.c:36
static const int16_t quant_table[64]
Definition intrax8.c:511
av_cold void ff_intrax8_common_end(IntraX8Context *w)
Destroy IntraX8 frame structure.
Definition intrax8.c:713
int ff_intrax8_decode_picture(IntraX8Context *w, MPVPicture *pict, GetBitContext *gb, int *mb_x, int *mb_y, int dquant, int quant_offset, int loopfilter, int lowdelay)
Decode single IntraX8 frame.
Definition intrax8.c:718
static void x8_get_prediction_chroma(IntraX8Context *const w)
Definition intrax8.c:351
#define OR_VLC_BITS
Definition intrax8.c:43
static const uint32_t ac_decode_table[]
Definition intrax8.c:125
static void dsp_x8_put_solidcolor(const uint8_t pix, uint8_t *dst, const ptrdiff_t linesize)
Definition intrax8.c:501
static int x8_get_orient_vlc(IntraX8Context *w)
Definition intrax8.c:110
#define run_offset(r)
Definition intrax8.c:123
static const VLCElem * j_ac_vlc[2][2][8]
Definition intrax8.c:49
#define level_offset(l)
Definition intrax8.c:124
#define AC_VLC_MTD
Definition intrax8.c:46
static void x8_ac_compensation(IntraX8Context *const w, const int direction, const int dc_level)
Definition intrax8.c:436
static int x8_get_dc_rlf(IntraX8Context *const w, const int mode, int *const level, int *const final)
Definition intrax8.c:247
static const uint8_t dc_index_offset[]
Definition intrax8.c:243
#define B(x, y)
#define extra_level
Definition intrax8.c:122
static void x8_get_prediction(IntraX8Context *const w)
Definition intrax8.c:367
#define DC_VLC_BITS
Definition intrax8.c:41
#define extra_bits(eb)
Definition intrax8.c:120
static void x8_select_ac_table(IntraX8Context *const w, int mode)
Definition intrax8.c:95
static const VLCElem * j_orient_vlc[2][4]
Definition intrax8.c:51
static int x8_setup_spatial_predictor(IntraX8Context *const w, const int chroma)
Definition intrax8.c:283
static int x8_decode_intra_mb(IntraX8Context *const w, const int chroma)
Definition intrax8.c:522
static void x8_init_block_index(IntraX8Context *w, AVFrame *frame)
Definition intrax8.c:662
static void x8_get_ac_rlf(IntraX8Context *const w, const int mode, int *const run, int *const level, int *const final)
Definition intrax8.c:167
static av_cold const VLCElem * x8_init_vlc(VLCInitState *state, int nb_bits, int nb_codes, const uint8_t table[][2])
Definition intrax8.c:53
av_cold void ff_intrax8dsp_init(IntraX8DSPContext *dsp)
Definition intrax8dsp.c:441
static const uint8_t x8_dc_quant_table[2][8][34][2]
Definition intrax8huf.h:55
static const uint8_t x8_orient_lowquant_table[4][12][2]
Definition intrax8huf.h:24
static const uint8_t x8_orient_highquant_table[2][12][2]
Definition intrax8huf.h:43
static const uint8_t x8_ac_quant_table[2][2][8][77][2]
Definition intrax8huf.h:207
av_cold void ff_blockdsp_init(BlockDSPContext *c)
Definition blockdsp.c:58
av_cold void ff_permute_scantable(uint8_t dst[64], const uint8_t src[64], const uint8_t permutation[64])
Definition idctdsp.c:30
#define av_cold
Definition attributes.h:117
#define AVOnce
Definition thread.h:202
static int ff_thread_once(char *control, void(*routine)(void))
Definition thread.h:205
#define AV_ONCE_INIT
Definition thread.h:203
uint8_t w
Definition llvidencdsp.c:39
static const uint16_t mask[17]
Definition lzw.c:38
#define FFMIN(a, b)
Definition macros.h:49
enum AVColorRange range
Memory handling functions.
void ff_draw_horiz_band(AVCodecContext *avctx, const AVFrame *cur, const AVFrame *last, int y, int h, int picture_structure, int first_field, int low_delay)
Draw a horizontal band if supported.
Definition mpegutils.c:54
#define PICT_FRAME
Definition mpegutils.h:33
const uint8_t ff_wmv1_scantable[WMV1_SCANTABLE_COUNT][64]
enum AVPixelFormat pix
Definition ohcodec.c:55
static const uint16_t table[]
Definition prosumer.c:203
unsigned int pos
Definition spdifenc.c:431
main external API structure.
Definition avcodec.h:443
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
MPVPicture.
Definition mpegpicture.h:58
struct AVFrame * f
Definition mpegpicture.h:59
int8_t * qscale_table
Definition mpegpicture.h:62
Definition vlc.h:32
For static VLCs, the number of bits can often be hardcoded at each get_vlc2() callsite.
Definition vlc.h:220
Definition swscale.c:71
uint8_t run
Definition svq3.c:207
uint8_t level
Definition svq3.c:208
#define av_mallocz(s)
#define av_freep(p)
static void error(const char *err)
static av_always_inline void chroma(WaveformContext *s, AVFrame *in, AVFrame *out, int component, int intensity, int offset_y, int offset_x, int column, int mirror, int jobnr, int nb_jobs)
av_cold const VLCElem * ff_vlc_init_tables_from_lengths(VLCInitState *state, int nb_bits, int nb_codes, const int8_t *lens, int lens_wrap, const void *symbols, int symbols_wrap, int symbols_size, int offset, int flags)
Definition vlc.c:366
#define VLC_INIT_STATE(_table)
Definition vlc.h:225
static const uint8_t quant[64]
Definition vmixdec.c:71
static double c[64]
av_cold void ff_wmv2dsp_init(IDCTDSPContext *c)
Definition wmv2dsp.c:142