对x264中的,slicewrite函数的以下代码注释 int totalbits = bspos(&h->out.bs) + x264cabacpos(&h->cabac);…
- User
对x264中的,slice_write函数的以下代码注释 int total_bits = bs_pos(&h->out.bs) + x264_cabac_pos(&h->cabac); int mb_size = total_bits - mb_spos;
if( slice_max_size && (!SLICE_MBAFF || (i_mb_y&1)) ) { /* Count the skip run, just in case. / if( !h->param.b_cabac ) total_bits += bs_size_ue_big( i_skip ); / Check for escape bytes. / uint8_t end = h->param.b_cabac ? h->cabac.p : h->out.bs.p; for( ; last_emu_check < end - 2; last_emu_check++ ) if( last_emu_check[0] == 0 && last_emu_check[1] == 0 && last_emu_check[2] <= 3 ) { slice_max_size -= 8; last_emu_check++; } / We’ll just re-encode this last macroblock if we go over the max slice size. / if( total_bits - starting_bits > slice_max_size && !h->mb.b_reencode_mb ) { if( !x264_frame_new_slice( h, h->fdec ) ) { / Handle the most obnoxious slice-min-mbs edge case: we need to end the slice
- because it’s gone over the maximum size, but doing so would violate slice-min-mbs.
- If possible, roll back to the last checkpoint and try again.
- We could try raising QP, but that would break in the case where a slice spans multiple
- rows, which the re-encoding infrastructure can’t currently handle. / if( mb_xy <= thread_last_mb && (thread_last_mb+1-mb_xy) < h->param.i_slice_min_mbs ) { if( thread_last_mb-h->param.i_slice_min_mbs < h->sh.i_first_mb+h->param.i_slice_min_mbs ) { x264_log( h, X264_LOG_WARNING, “slice-max-size violated (frame %d, cause: slice-min-mbs)\n”, h->i_frame ); slice_max_size = 0; goto cont; } bitstream_restore( h, &bs_bak[BS_BAK_SLICE_MIN_MBS], &i_skip, 0 ); h->mb.b_reencode_mb = 1; h->sh.i_last_mb = thread_last_mb-h->param.i_slice_min_mbs; break; } if( mb_xy-SLICE_MBAFFh->mb.i_mb_stride != h->sh.i_first_mb ) { bitstream_restore( h, &bs_bak[BS_BAK_SLICE_MAX_SIZE], &i_skip, 0 ); h->mb.b_reencode_mb = 1; if( SLICE_MBAFF ) { // set to bottom of previous mbpair if( i_mb_x ) h->sh.i_last_mb = mb_xy-1+h->mb.i_mb_stride(!(i_mb_y&1)); else h->sh.i_last_mb = (i_mb_y-2+!(i_mb_y&1))*h->mb.i_mb_stride + h->mb.i_mb_width - 1; } else h->sh.i_last_mb = mb_xy-1; break; } else h->sh.i_last_mb = mb_xy; } else slice_max_size = 0; } }
- Assistant
这段代码是 x264 中的 slice_write 函数,主要用于控制编码每个 slice(片段)的最大码流长度 slice_max_size。如果当前 slice 的码流长度超过了 slice_max_size,则需要重新编码最后一个宏块(MB),以控制码流长度不超过 slice_max_size。
代码中首先计算了当前 slice 的总码流长度 total_bits,包括起始位置(bs_pos(&h->out.bs))、CABAC 转移状态长度(x264_cabac_pos(&h->cabac))和当前 slice 中的每个宏块的码流长度(mb_size)。
然后判断当前是否需要检查 slice 的最大码流长度。如果是 mb-aff 模式(一个 MB 由上下两个 16x16 的子块组成),则只检查奇数行的 slice;如果不是 mb-aff 模式,则检查所有行的 slice。检查过程中,如果发现了 CABAC 编码中的 escape bytes,则将 slice_max_size 减去 8 个比特,以避免在解码时出现错误。
如果 total_bits 超过了 slice_max_size,则需要重新编码最后一个宏块。但是,如果当前 slice 中的宏块个数少于参数 i_slice_min_mbs(即当前 slice 的最小宏块数),则不能重新编码最后一个宏块,因为这样会导致当前 slice 的宏块数小于最小值,破坏码流的正确性。为了避免这种情况,代码尝试将当前 slice 回滚到上一个检查点并重新编码,直到当前 slice 的宏块数不小于 i_slice_min_mbs。如果不能回滚到上一个检查点,则退出编码过程。如果不需要重新编码宏块,则直接结束当前 slice 的编码。
- User
对每一行代码做注释
- Assistant
- User
对他的以下代码注释 if( x264_ratecontrol_mb( h, mb_size ) < 0 ) { bitstream_restore( h, &bs_bak[BS_BAK_ROW_VBV], &i_skip, 1 ); h->mb.b_reencode_mb = 1; i_mb_x = 0; i_mb_y = i_mb_y - SLICE_MBAFF; h->mb.i_mb_prev_xy = i_mb_y * h->mb.i_mb_stride - 1; h->sh.i_last_mb = orig_last_mb; continue; }
- Assistant
这段代码是在判断对当前 slice 中的每个宏块进行编码后,码流长度是否符合比特率控制的要求。如果不满足要求,则需要回滚到上一个检查点并重新编码。
具体实现是调用 x264_ratecontrol_mb 函数计算当前宏块的码流长度和相应的 QP 值,并比较与预期的码流长度是否相符。如果不相符,则需要回滚到上一个检查点重新编码最后一个宏块,并将 i_mb_x 和 i_mb_y 分别设置为 0 和 i_mb_y - SLICE_MBAFF(如果当前是 mb-aff 模式)以重新计算 i_mb_x 和 i_mb_y,并将当前宏块的索引设置为上一个宏块(即 h->mb.i_mb_prev_xy = i_mb_y * h->mb.i_mb_stride - 1),以便下一轮循环中正常处理上一个宏块。同时,h->sh.i_last_mb 被设置为 orig_last_mb,表示回滚到上一个检查点处理。如果当前码流长度符合要求,则开始处理下一个宏块。
- User
对他的以下代码做注释 if( h->sh.i_last_mb == (h->i_threadslice_end * h->mb.i_mb_width - 1) ) { h->stat.frame.i_misc_bits = bs_pos( &h->out.bs ) + (h->out.i_nal*NALU_OVERHEAD * 8) - h->stat.frame.i_tex_bits - h->stat.frame.i_mv_bits; fdec_filter_row( h, h->i_threadslice_end, 0 );
if( h->param.b_sliced_threads ) { /* Tell the main thread we're done. */ x264_threadslice_cond_broadcast( h, 1 ); /* Do hpel now */ for( int mb_y = h->i_threadslice_start; mb_y <= h->i_threadslice_end; mb_y++ ) fdec_filter_row( h, mb_y, 1 ); x264_threadslice_cond_broadcast( h, 2 ); /* Do the first row of hpel, now that the previous slice is done */ if( h->i_thread_idx > 0 ) { x264_threadslice_cond_wait( h->thread[h->i_thread_idx-1], 2 ); fdec_filter_row( h, h->i_threadslice_start + (1 << SLICE_MBAFF), 2 ); } } /* Free mb info after the last thread's done using it */ if( h->fdec->mb_info_free && (!h->param.b_sliced_threads || h->i_thread_idx == (h->param.i_threads-1)) ) { h->fdec->mb_info_free( h->fdec->mb_info ); h->fdec->mb_info = NULL; h->fdec->mb_info_free = NULL; } } - Assistant
这段代码是在每个线程处理完当前 slice 的最后一个宏块时执行的,用于完成一些后续工作。
首先判断当前处理的宏块是否是当前线程的最后一个宏块(即 h->sh.i_last_mb 是否等于当前线程处理的最后一个宏块的索引)。如果是,则表示当前线程已经处理完所有分配给它的宏块,需要执行一些收尾工作。具体工作包括:
- 计算该 slice 的统计信息,包括去除 texture bits 和 motion vector bits 之后的剩余比特数,用于比特率控制;
- 执行 fdec_filter_row 函数,对当前线程处理的宏块所在的一整行进行滤波操作;
- 如果启用了 sliced_threads 参数,则通知主线程当前线程已经处理完所有分配给它的宏块,然后进行一些额外的滤波操作;
- 如果当前线程是最后一个线程(即 h->i_thread_idx == (h->param.i_threads-1)),则释放 fdec 结构体中的 mb_info 字段,以便下一轮编码时重新分配它。
在以上工作完成后,当前线程结束执行,返回主线程进行下一轮编码。