codec/h264/h264dec: fix CABAC context initialisation

Issue #126 on hold
Saxon Milton created an issue

The CABAC context initialisation currently looks very wrong in comparison to the reference implementation. We should fix this to follow the reference implementation. The relevant specification sections are as follows (tables not included because can’t format)

9.3.1 Initialisation process
Outputs of this process are initialised CABAC internal variables.
The processes in clauses 9.3.1.1 and 9.3.1.2 are invoked when starting the parsing of the slice data of a slice in clause 7.3.4.
The process in clause 9.3.1.2 is also invoked after decoding any pcm_alignment_zero_bit and all pcm_sample_luma and
pcm_sample_chroma data for a macroblock of type I_PCM.

9.3.1.1 Initialisation process for context variables
Outputs of this process are the initialised CABAC context variables indexed by ctxIdx.
Tables 9-12 to 9-33 contain the values of the variables n and m used in the initialisation of context variables that are
assigned to all syntax elements in clauses 7.3.4 and 7.3.5 except for the end-of-slice flag.
For each context variable, the two variables pStateIdx and valMPS are initialised.
NOTE 1 – The variable pStateIdx corresponds to a probability state index and the variable valMPS corresponds to the value of the most probable symbol as further described in clause 9.3.3.2.
The two values assigned to pStateIdx and valMPS for the initialisation are derived from SliceQP_Y , which is derived in
Equation 7-30. Given the two table entries ( m, n ), the initialisation is specified by the following pseudo-code process:

preCtxState = Clip3( 1, 126, ( ( m * Clip3( 0, 51, SliceQP Y ) ) >> 4 ) + n )
if( preCtxState <= 63 ) {
  pStateIdx = 63 − preCtxState
  valMPS = 0
} else {
  pStateIdx = preCtxState − 64
  valMPS = 1
}

In Table 9-11, the ctxIdx for which initialisation is needed for each of the slice types are listed. Also listed is the table
number that includes the values of m and n needed for the initialisation. For P, SP and B slice type, the initialisation
depends also on the value of the cabac_init_idc syntax element. Note that the syntax element names do not affect the
initialisation process.

Comments (5)

  1. Alan Noble

    We may revisit the H.264 decoder project one day, so let’s keep these (and related) issues on hold (and not resolve them).

  2. Log in to comment