27 #define BITSTREAM_READER_LE
30 #include "config_components.h"
46 #define MAX_INDEX (64 - 1)
52 #define ALPHA_VLC_BITS 5
68 {0x0001, 2}, {0x0003, 3}, {0x000E, 4}, {0x0007, 5},
69 {0x0017, 5}, {0x0028, 6}, {0x0008, 6}, {0x006F, 7},
70 {0x001F, 7}, {0x00C4, 8}, {0x0044, 8}, {0x005F, 8},
71 {0x00DF, 8}, {0x007F, 8}, {0x00FF, 8}, {0x3E00, 14},
72 {0x1E00, 14}, {0x2E00, 14}, {0x0E00, 14}, {0x3600, 14},
73 {0x1600, 14}, {0x2600, 14}, {0x0600, 14}, {0x3A00, 14},
74 {0x1A00, 14}, {0x2A00, 14}, {0x0A00, 14}, {0x3200, 14},
75 {0x1200, 14}, {0x2200, 14}, {0x0200, 14}, {0x0C00, 15},
76 {0x7400, 15}, {0x3400, 15}, {0x5400, 15}, {0x1400, 15},
77 {0x6400, 15}, {0x2400, 15}, {0x4400, 15}, {0x0400, 15},
78 {0x0002, 3}, {0x000C, 5}, {0x004F, 7}, {0x00E4, 8},
79 {0x0004, 8}, {0x0D00, 13}, {0x1500, 13}, {0x7C00, 15},
80 {0x3C00, 15}, {0x5C00, 15}, {0x1C00, 15}, {0x6C00, 15},
81 {0x2C00, 15}, {0x4C00, 15}, {0xC800, 16}, {0x4800, 16},
82 {0x8800, 16}, {0x0800, 16}, {0x0300, 13}, {0x1D00, 13},
83 {0x0014, 5}, {0x0070, 7}, {0x003F, 8}, {0x00C0, 10},
84 {0x0500, 13}, {0x0180, 12}, {0x0280, 12}, {0x0C80, 12},
85 {0x0080, 12}, {0x0B00, 13}, {0x1300, 13}, {0x001C, 5},
86 {0x0064, 8}, {0x0380, 12}, {0x1900, 13}, {0x0D80, 12},
87 {0x0018, 6}, {0x00BF, 8}, {0x0480, 12}, {0x0B80, 12},
88 {0x0038, 6}, {0x0040, 9}, {0x0900, 13}, {0x0030, 7},
89 {0x0780, 12}, {0x2800, 16}, {0x0010, 7}, {0x0A80, 12},
90 {0x0050, 7}, {0x0880, 12}, {0x000F, 7}, {0x1100, 13},
91 {0x002F, 7}, {0x0100, 13}, {0x0084, 8}, {0x5800, 16},
92 {0x00A4, 8}, {0x9800, 16}, {0x0024, 8}, {0x1800, 16},
93 {0x0140, 9}, {0xE800, 16}, {0x01C0, 9}, {0x6800, 16},
94 {0x02C0, 10}, {0xA800, 16}, {0x0F80, 12}, {0x0580, 12},
95 {0x0980, 12}, {0x0E80, 12}, {0x0680, 12}, {0x1F00, 13},
96 {0x0F00, 13}, {0x1700, 13}, {0x0700, 13}, {0x1B00, 13},
97 {0xF800, 16}, {0x7800, 16}, {0xB800, 16}, {0x3800, 16},
104 1, 2, 3, 4, 5, 6, 7, 8,
105 9, 10, 11, 12, 13, 14, 15, 16,
106 17, 18, 19, 20, 21, 22, 23, 24,
107 25, 26, 27, 28, 29, 30, 31, 32,
108 33, 34, 35, 36, 37, 38, 39, 40,
109 1, 2, 3, 4, 5, 6, 7, 8,
110 9, 10, 11, 12, 13, 14, 15, 16,
111 17, 18, 19, 20, 1, 2, 3, 4,
112 5, 6, 7, 8, 9, 10, 11, 1,
113 2, 3, 4, 5, 1, 2, 3, 4,
114 1, 2, 3, 1, 2, 3, 1, 2,
115 1, 2, 1, 2, 1, 2, 1, 2,
116 1, 2, 1, 2, 1, 2, 1, 2,
117 1, 2, 1, 1, 1, 1, 1, 1,
118 1, 1, 1, 1, 1, 1, 1, 1,
123 0, 0, 0, 0, 0, 0, 0, 0,
124 0, 0, 0, 0, 0, 0, 0, 0,
125 0, 0, 0, 0, 0, 0, 0, 0,
126 0, 0, 0, 0, 0, 0, 0, 0,
127 0, 0, 0, 0, 0, 0, 0, 0,
128 1, 1, 1, 1, 1, 1, 1, 1,
129 1, 1, 1, 1, 1, 1, 1, 1,
130 1, 1, 1, 1, 2, 2, 2, 2,
131 2, 2, 2, 2, 2, 2, 2, 3,
132 3, 3, 3, 3, 4, 4, 4, 4,
133 5, 5, 5, 6, 6, 6, 7, 7,
134 8, 8, 9, 9, 10, 10, 11, 11,
135 12, 12, 13, 13, 14, 14, 15, 15,
136 16, 16, 17, 18, 19, 20, 21, 22,
137 23, 24, 25, 26, 27, 28, 29, 30,
149 #if CONFIG_SPEEDHQ_DECODER
151 static const uint8_t unscaled_quant_matrix[64] = {
152 16, 16, 19, 22, 26, 27, 29, 34,
153 16, 16, 22, 24, 27, 29, 34, 37,
154 19, 22, 26, 27, 29, 34, 34, 38,
155 22, 22, 26, 27, 29, 34, 37, 40,
156 22, 26, 27, 29, 32, 35, 40, 48,
157 26, 27, 29, 32, 35, 40, 48, 58,
158 26, 27, 29, 34, 38, 46, 56, 69,
159 27, 29, 35, 38, 46, 56, 69, 83
162 static VLC dc_lum_vlc_le;
163 static VLC dc_chroma_vlc_le;
164 static VLC dc_alpha_run_vlc_le;
165 static VLC dc_alpha_level_vlc_le;
167 static inline int decode_dc_le(
GetBitContext *gb,
int component)
171 if (component == 0 || component == 3) {
184 static inline int decode_alpha_block(
const SHQContext *
s,
GetBitContext *gb, uint8_t last_alpha[16], uint8_t *dest,
int linesize)
213 for (y = 0; y < 8; y++) {
214 for (x = 0; x < 16; x++) {
215 last_alpha[x] -=
block[y * 16 + x];
217 memcpy(dest, last_alpha, 16);
226 const int *quant_matrix =
s->quant_matrix;
227 const uint8_t *scantable =
s->intra_scantable.permutated;
231 s->bdsp.clear_block(
block);
233 dc_offset = decode_dc_le(gb, component);
234 last_dc[component] -= dc_offset;
235 block[scantable[0]] = last_dc[component];
258 #if MIN_CACHE_BITS < 6 + 6 + 12
259 #error MIN_CACHE_BITS is too small for the escape code, add UPDATE_CACHE
276 s->idsp.idct_put(dest, linesize,
block);
283 int linesize_y =
frame->linesize[0] * line_stride;
284 int linesize_cb =
frame->linesize[1] * line_stride;
285 int linesize_cr =
frame->linesize[2] * line_stride;
289 if (
s->alpha_type != SHQ_NO_ALPHA)
290 linesize_a =
frame->linesize[3] * line_stride;
292 for (
int y = 0; y <
frame->height; y += 16 * line_stride) {
293 int last_dc[4] = { 1024, 1024, 1024, 1024 };
294 uint8_t *dest_y, *dest_cb, *dest_cr, *dest_a;
295 uint8_t last_alpha[16];
296 int x =
frame->width - 8;
298 dest_y =
frame->data[0] +
frame->linesize[0] * (y + field_number) + x;
299 if (
s->subsampling == SHQ_SUBSAMPLING_420) {
300 dest_cb =
frame->data[1] +
frame->linesize[1] * (y/2 + field_number) + x / 2;
301 dest_cr =
frame->data[2] +
frame->linesize[2] * (y/2 + field_number) + x / 2;
304 dest_cb =
frame->data[1] +
frame->linesize[1] * (y + field_number) + x / 2;
305 dest_cr =
frame->data[2] +
frame->linesize[2] * (y + field_number) + x / 2;
307 if (
s->alpha_type != SHQ_NO_ALPHA) {
308 memset(last_alpha, 255,
sizeof(last_alpha));
309 dest_a =
frame->data[3] +
frame->linesize[3] * (y + field_number) + x;
325 if (
s->subsampling != SHQ_SUBSAMPLING_420) {
332 if (
s->alpha_type == SHQ_RLE_ALPHA) {
334 if ((
ret = decode_alpha_block(
s, gb, last_alpha, dest_a, linesize_a)) < 0)
336 if ((
ret = decode_alpha_block(
s, gb, last_alpha, dest_a + 8 * linesize_a, linesize_a)) < 0)
338 }
else if (
s->alpha_type == SHQ_DCT_ALPHA) {
354 static int decode_speedhq_field(
const SHQContext *
s,
const uint8_t *buf,
int buf_size,
AVFrame *
frame,
int field_number,
int start,
int end,
int line_stride)
356 int ret, slice_number, slice_offsets[5];
357 int linesize_y =
frame->linesize[0] * line_stride;
358 int linesize_cb =
frame->linesize[1] * line_stride;
359 int linesize_cr =
frame->linesize[2] * line_stride;
363 if (
s->alpha_type != SHQ_NO_ALPHA)
364 linesize_a =
frame->linesize[3] * line_stride;
366 if (end < start || end - start < 3 || end > buf_size)
369 slice_offsets[0] = start;
370 slice_offsets[4] = end;
371 for (slice_number = 1; slice_number < 4; slice_number++) {
372 uint32_t last_offset, slice_len;
374 last_offset = slice_offsets[slice_number - 1];
375 slice_len =
AV_RL24(buf + last_offset);
376 slice_offsets[slice_number] = last_offset + slice_len;
378 if (slice_len < 3 || slice_offsets[slice_number] > end - 3)
382 for (slice_number = 0; slice_number < 4; slice_number++) {
386 slice_begin = slice_offsets[slice_number];
387 slice_end = slice_offsets[slice_number + 1];
392 for (y = slice_number * 16 * line_stride; y <
frame->height; y += line_stride * 64) {
393 uint8_t *dest_y, *dest_cb, *dest_cr, *dest_a;
394 int last_dc[4] = { 1024, 1024, 1024, 1024 };
395 uint8_t last_alpha[16];
397 memset(last_alpha, 255,
sizeof(last_alpha));
399 dest_y =
frame->data[0] +
frame->linesize[0] * (y + field_number);
400 if (
s->subsampling == SHQ_SUBSAMPLING_420) {
401 dest_cb =
frame->data[1] +
frame->linesize[1] * (y/2 + field_number);
402 dest_cr =
frame->data[2] +
frame->linesize[2] * (y/2 + field_number);
404 dest_cb =
frame->data[1] +
frame->linesize[1] * (y + field_number);
405 dest_cr =
frame->data[2] +
frame->linesize[2] * (y + field_number);
407 if (
s->alpha_type != SHQ_NO_ALPHA) {
408 dest_a =
frame->data[3] +
frame->linesize[3] * (y + field_number);
411 for (x = 0; x <
frame->width - 8 * (
s->subsampling != SHQ_SUBSAMPLING_444); x += 16) {
431 if (
s->subsampling != SHQ_SUBSAMPLING_420) {
438 if (
s->subsampling == SHQ_SUBSAMPLING_444) {
444 if ((
ret =
decode_dct_block(
s, &gb, last_dc, 1, dest_cb + 8 * linesize_cb + 8, linesize_cb)) < 0)
446 if ((
ret =
decode_dct_block(
s, &gb, last_dc, 2, dest_cr + 8 * linesize_cr + 8, linesize_cr)) < 0)
457 if (
s->alpha_type == SHQ_RLE_ALPHA) {
459 if ((
ret = decode_alpha_block(
s, &gb, last_alpha, dest_a, linesize_a)) < 0)
461 if ((
ret = decode_alpha_block(
s, &gb, last_alpha, dest_a + 8 * linesize_a, linesize_a)) < 0)
464 }
else if (
s->alpha_type == SHQ_DCT_ALPHA) {
480 if (
s->subsampling != SHQ_SUBSAMPLING_444 && (
frame->width & 15))
481 return decode_speedhq_border(
s, &gb,
frame, field_number, line_stride);
496 const uint8_t *buf = avpkt->
data;
497 int buf_size = avpkt->
size;
499 uint32_t second_field_offset;
502 if (buf_size < 4 || avctx->
width < 8)
512 second_field_offset =
AV_RL24(buf + 1);
513 if (second_field_offset >= buf_size - 3) {
523 frame->key_frame = 1;
525 if (second_field_offset == 4 || second_field_offset == (buf_size-4)) {
535 if ((
ret = decode_speedhq_field(
s, buf, buf_size,
frame, 0, 4, buf_size, 1)) < 0)
538 if ((
ret = decode_speedhq_field(
s, buf, buf_size,
frame, 0, 4, second_field_offset, 2)) < 0)
540 if ((
ret = decode_speedhq_field(
s, buf, buf_size,
frame, 1, second_field_offset, buf_size, 2)) < 0)
553 static av_cold void compute_alpha_vlcs(
void)
555 uint16_t run_code[134], level_code[266];
556 uint8_t
run_bits[134], level_bits[266];
557 int16_t run_symbols[134], level_symbols[266];
566 run_symbols[entry] = 0;
570 for (
i = 0;
i < 4; ++
i) {
571 run_code[entry] = (
i << 2) | 1;
573 run_symbols[entry] =
i + 1;
578 for (
i = 0;
i < 128; ++
i) {
579 run_code[entry] = (
i << 3) | 7;
581 run_symbols[entry] =
i;
588 run_symbols[entry] = -1;
597 run_symbols, 2, 2, 160);
602 for (sign = 0; sign <= 1; ++sign) {
604 level_code[entry] = (sign << 1) | 1;
605 level_bits[entry] = 2;
606 level_symbols[entry] = sign ? -1 : 1;
610 for (
i = 0;
i < 4; ++
i) {
611 level_code[entry] = (
i << 3) | (sign << 2) | 2;
612 level_bits[entry] = 5;
613 level_symbols[entry] = sign ? -(
i + 2) : (
i + 2);
624 for (
i = 0;
i < 256; ++
i) {
625 level_code[entry] =
i << 2;
626 level_bits[entry] = 10;
627 level_symbols[entry] =
i;
637 level_symbols, 2, 2, 288);
640 static av_cold void speedhq_static_init(
void)
654 compute_alpha_vlcs();
674 case MKTAG(
'S',
'H',
'Q',
'0'):
675 s->subsampling = SHQ_SUBSAMPLING_420;
676 s->alpha_type = SHQ_NO_ALPHA;
679 case MKTAG(
'S',
'H',
'Q',
'1'):
680 s->subsampling = SHQ_SUBSAMPLING_420;
681 s->alpha_type = SHQ_RLE_ALPHA;
684 case MKTAG(
'S',
'H',
'Q',
'2'):
685 s->subsampling = SHQ_SUBSAMPLING_422;
686 s->alpha_type = SHQ_NO_ALPHA;
689 case MKTAG(
'S',
'H',
'Q',
'3'):
690 s->subsampling = SHQ_SUBSAMPLING_422;
691 s->alpha_type = SHQ_RLE_ALPHA;
694 case MKTAG(
'S',
'H',
'Q',
'4'):
695 s->subsampling = SHQ_SUBSAMPLING_444;
696 s->alpha_type = SHQ_NO_ALPHA;
699 case MKTAG(
'S',
'H',
'Q',
'5'):
700 s->subsampling = SHQ_SUBSAMPLING_444;
701 s->alpha_type = SHQ_RLE_ALPHA;
704 case MKTAG(
'S',
'H',
'Q',
'7'):
705 s->subsampling = SHQ_SUBSAMPLING_422;
706 s->alpha_type = SHQ_DCT_ALPHA;
709 case MKTAG(
'S',
'H',
'Q',
'9'):
710 s->subsampling = SHQ_SUBSAMPLING_444;
711 s->alpha_type = SHQ_DCT_ALPHA;
733 .
init = speedhq_decode_init,