Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

对x264中的,slicewrite函数的以下的每一行代码注释 int totalbits = bspos(&h->out.bs) +…

  1. 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; } }
  2. Assistant

    该函数用于在码流中写入片的数据。

    1. 首先计算总比特数以及片与之前宏块的偏移量。
    2. 如果切片的大小已经限定,且当前宏块不在帧内的MB-AFF域中的偶数行上,那么在码流中计算丢失的零(skip)数目,将切片的大小减去8个码字节,并重新编码最后一个宏块。
    3. 如果最终的总比特数超过了切片的最大大小,并且之前没有进行过编码调整,则尝试创建一个新的切片,否则说明可能存在一些特殊情况,需要在上一次检查点处回归并重新尝试编码。