2024-10-06 06:27:16 +02:00
|
|
|
/*
|
|
|
|
|
* FFv1 codec
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2024 Lynne <dev@lynne.ee>
|
|
|
|
|
*
|
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
|
*
|
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
struct SliceContext {
|
|
|
|
|
RangeCoder c;
|
2025-03-10 03:04:39 +00:00
|
|
|
|
|
|
|
|
#if !defined(DECODE)
|
2024-10-06 06:27:16 +02:00
|
|
|
PutBitContext pb; /* 8*8 bytes */
|
2025-03-10 03:04:39 +00:00
|
|
|
#else
|
|
|
|
|
GetBitContext gb;
|
|
|
|
|
#endif
|
2024-10-06 06:27:16 +02:00
|
|
|
|
|
|
|
|
ivec2 slice_dim;
|
|
|
|
|
ivec2 slice_pos;
|
|
|
|
|
ivec2 slice_rct_coef;
|
2024-12-24 15:57:22 +09:00
|
|
|
u8vec4 quant_table_idx;
|
|
|
|
|
uint context_count;
|
2024-10-06 06:27:16 +02:00
|
|
|
|
|
|
|
|
uint hdr_len; // only used for golomb
|
2024-12-24 15:57:22 +09:00
|
|
|
|
|
|
|
|
uint slice_coding_mode;
|
|
|
|
|
bool slice_reset_contexts;
|
2024-10-06 06:27:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* -1, { -1, 0 } */
|
|
|
|
|
int predict(int L, ivec2 top)
|
|
|
|
|
{
|
|
|
|
|
return mid_pred(L, L + top[1] - top[0], top[1]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* { -2, -1 }, { -1, 0, 1 }, 0 */
|
|
|
|
|
int get_context(VTYPE2 cur_l, VTYPE3 top_l, TYPE top2, uint8_t quant_table_idx)
|
|
|
|
|
{
|
|
|
|
|
const int LT = top_l[0]; /* -1 */
|
|
|
|
|
const int T = top_l[1]; /* 0 */
|
|
|
|
|
const int RT = top_l[2]; /* 1 */
|
|
|
|
|
const int L = cur_l[1]; /* -1 */
|
|
|
|
|
|
|
|
|
|
int base = quant_table[quant_table_idx][0][(L - LT) & MAX_QUANT_TABLE_MASK] +
|
|
|
|
|
quant_table[quant_table_idx][1][(LT - T) & MAX_QUANT_TABLE_MASK] +
|
|
|
|
|
quant_table[quant_table_idx][2][(T - RT) & MAX_QUANT_TABLE_MASK];
|
|
|
|
|
|
|
|
|
|
if ((quant_table[quant_table_idx][3][127] == 0) &&
|
|
|
|
|
(quant_table[quant_table_idx][4][127] == 0))
|
|
|
|
|
return base;
|
|
|
|
|
|
|
|
|
|
const int TT = top2; /* -2 */
|
|
|
|
|
const int LL = cur_l[0]; /* -2 */
|
|
|
|
|
return base +
|
|
|
|
|
quant_table[quant_table_idx][3][(LL - L) & MAX_QUANT_TABLE_MASK] +
|
|
|
|
|
quant_table[quant_table_idx][4][(TT - T) & MAX_QUANT_TABLE_MASK];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const uint32_t log2_run[41] = {
|
|
|
|
|
0, 0, 0, 0, 1, 1, 1, 1,
|
|
|
|
|
2, 2, 2, 2, 3, 3, 3, 3,
|
|
|
|
|
4, 4, 5, 5, 6, 6, 7, 7,
|
|
|
|
|
8, 9, 10, 11, 12, 13, 14, 15,
|
|
|
|
|
16, 17, 18, 19, 20, 21, 22, 23,
|
|
|
|
|
24,
|
|
|
|
|
};
|
2024-12-24 15:57:22 +09:00
|
|
|
|
|
|
|
|
uint slice_coord(uint width, uint sx, uint num_h_slices, uint chroma_shift)
|
|
|
|
|
{
|
|
|
|
|
uint mpw = 1 << chroma_shift;
|
|
|
|
|
uint awidth = align(width, mpw);
|
|
|
|
|
|
|
|
|
|
if ((version < 4) || ((version == 4) && (micro_version < 3)))
|
|
|
|
|
return width * sx / num_h_slices;
|
|
|
|
|
|
|
|
|
|
sx = (2 * awidth * sx + num_h_slices * mpw) / (2 * num_h_slices * mpw) * mpw;
|
|
|
|
|
if (sx == awidth)
|
|
|
|
|
sx = width;
|
|
|
|
|
|
|
|
|
|
return sx;
|
|
|
|
|
}
|