+/* setting this to 1 results in a faster codec but
+ * somewhat lower image quality */
+#define DV100_SACRIFICE_QUALITY_FOR_SPEED 1
+#define DV100_ENABLE_FINER 1
+
+/* pack combination of QNO and CNO into a single 8-bit value */
+#define DV100_MAKE_QLEVEL(qno,cno) ((qno<<2) | (cno))
+#define DV100_QLEVEL_QNO(qlevel) (qlevel>>2)
+#define DV100_QLEVEL_CNO(qlevel) (qlevel&0x3)
+
+#define DV100_NUM_QLEVELS 31
+
+/* The quantization step is determined by a combination of QNO and
+ CNO. We refer to these combinations as "qlevels" (this term is our
+ own, it's not mentioned in the spec). We use CNO, a multiplier on
+ the quantization step, to "fill in the gaps" between quantization
+ steps associated with successive values of QNO. e.g. there is no
+ QNO for a quantization step of 10, but we can use QNO=5 CNO=1 to
+ get the same result. The table below encodes combinations of QNO
+ and CNO in order of increasing quantization coarseness. */
+static const uint8_t dv100_qlevels[DV100_NUM_QLEVELS] = {
+ DV100_MAKE_QLEVEL( 1,0), // 1*1= 1
+ DV100_MAKE_QLEVEL( 1,0), // 1*1= 1
+ DV100_MAKE_QLEVEL( 2,0), // 2*1= 2
+ DV100_MAKE_QLEVEL( 3,0), // 3*1= 3
+ DV100_MAKE_QLEVEL( 4,0), // 4*1= 4
+ DV100_MAKE_QLEVEL( 5,0), // 5*1= 5
+ DV100_MAKE_QLEVEL( 6,0), // 6*1= 6
+ DV100_MAKE_QLEVEL( 7,0), // 7*1= 7
+ DV100_MAKE_QLEVEL( 8,0), // 8*1= 8
+ DV100_MAKE_QLEVEL( 5,1), // 5*2=10
+ DV100_MAKE_QLEVEL( 6,1), // 6*2=12
+ DV100_MAKE_QLEVEL( 7,1), // 7*2=14
+ DV100_MAKE_QLEVEL( 9,0), // 16*1=16
+ DV100_MAKE_QLEVEL(10,0), // 18*1=18
+ DV100_MAKE_QLEVEL(11,0), // 20*1=20
+ DV100_MAKE_QLEVEL(12,0), // 22*1=22
+ DV100_MAKE_QLEVEL(13,0), // 24*1=24
+ DV100_MAKE_QLEVEL(14,0), // 28*1=28
+ DV100_MAKE_QLEVEL( 9,1), // 16*2=32
+ DV100_MAKE_QLEVEL(10,1), // 18*2=36
+ DV100_MAKE_QLEVEL(11,1), // 20*2=40
+ DV100_MAKE_QLEVEL(12,1), // 22*2=44
+ DV100_MAKE_QLEVEL(13,1), // 24*2=48
+ DV100_MAKE_QLEVEL(15,0), // 52*1=52
+ DV100_MAKE_QLEVEL(14,1), // 28*2=56
+ DV100_MAKE_QLEVEL( 9,2), // 16*4=64
+ DV100_MAKE_QLEVEL(10,2), // 18*4=72
+ DV100_MAKE_QLEVEL(11,2), // 20*4=80
+ DV100_MAKE_QLEVEL(12,2), // 22*4=88
+ DV100_MAKE_QLEVEL(13,2), // 24*4=96
+ // ...
+ DV100_MAKE_QLEVEL(15,3), // 52*8=416
+};
+
+static const int dv100_min_bias = 0;
+static const int dv100_chroma_bias = 0;
+static const int dv100_starting_qno = 1;
+
+#if DV100_SACRIFICE_QUALITY_FOR_SPEED
+static const int dv100_qlevel_inc = 4;
+#else
+static const int dv100_qlevel_inc = 1;
+#endif
+
+// 1/qstep, shifted up by 16 bits
+static const int dv100_qstep_bits = 16;
+static const int dv100_qstep_inv[16] = {
+ 65536, 65536, 32768, 21845, 16384, 13107, 10923, 9362, 8192, 4096, 3641, 3277, 2979, 2731, 2341, 1260,
+};
+
+/* DV100 weights are pre-zigzagged, inverted and multiplied by 2^(dv100_weight_shift)
+ (in DV100 the AC components are divided by the spec weights) */
+static const int dv100_weight_shift = 16;
+static const int dv_weight_1080[2][64] = {
+ { 8192, 65536, 65536, 61681, 61681, 61681, 58254, 58254,
+ 58254, 58254, 58254, 58254, 55188, 58254, 58254, 55188,
+ 55188, 55188, 55188, 55188, 55188, 24966, 27594, 26214,
+ 26214, 26214, 27594, 24966, 23831, 24385, 25575, 25575,
+ 25575, 25575, 24385, 23831, 23302, 23302, 24966, 24966,
+ 24966, 23302, 23302, 21845, 22795, 24385, 24385, 22795,
+ 21845, 21400, 21845, 23831, 21845, 21400, 10382, 10700,
+ 10700, 10382, 10082, 9620, 10082, 9039, 9039, 8525, },
+ { 8192, 65536, 65536, 61681, 61681, 61681, 41943, 41943,
+ 41943, 41943, 40330, 41943, 40330, 41943, 40330, 40330,
+ 40330, 38836, 38836, 40330, 40330, 24966, 27594, 26214,
+ 26214, 26214, 27594, 24966, 23831, 24385, 25575, 25575,
+ 25575, 25575, 24385, 23831, 11523, 11523, 12483, 12483,
+ 12483, 11523, 11523, 10923, 11275, 12193, 12193, 11275,
+ 10923, 5323, 5490, 5924, 5490, 5323, 5165, 5323,
+ 5323, 5165, 5017, 4788, 5017, 4520, 4520, 4263, }
+};
+
+static const int dv_weight_720[2][64] = {
+ { 8192, 65536, 65536, 61681, 61681, 61681, 58254, 58254,
+ 58254, 58254, 58254, 58254, 55188, 58254, 58254, 55188,
+ 55188, 55188, 55188, 55188, 55188, 24966, 27594, 26214,
+ 26214, 26214, 27594, 24966, 23831, 24385, 25575, 25575,
+ 25575, 25575, 24385, 23831, 15420, 15420, 16644, 16644,
+ 16644, 15420, 15420, 10923, 11398, 12193, 12193, 11398,
+ 10923, 10700, 10923, 11916, 10923, 10700, 5191, 5350,
+ 5350, 5191, 5041, 4810, 5041, 4520, 4520, 4263, },
+ { 8192, 43691, 43691, 40330, 40330, 40330, 29127, 29127,
+ 29127, 29127, 29127, 29127, 27594, 29127, 29127, 27594,
+ 27594, 27594, 27594, 27594, 27594, 12483, 13797, 13107,
+ 13107, 13107, 13797, 12483, 11916, 12193, 12788, 12788,
+ 12788, 12788, 12193, 11916, 5761, 5761, 6242, 6242,
+ 6242, 5761, 5761, 5461, 5638, 5461, 6096, 5638,
+ 5461, 2661, 2745, 2962, 2745, 2661, 2583, 2661,
+ 2661, 2583, 2509, 2394, 2509, 2260, 2260, 2131, }
+};
+
+static av_always_inline int dv_set_class_number_sd(DVVideoContext *s,
+ int16_t *blk, EncBlockInfo *bi,
+ const uint8_t *zigzag_scan,
+ const int *weight, int bias)