h264.c
资源名称:X264CODEC.rar [点击查看]
上传用户:lctgjx
上传日期:2022-06-04
资源大小:8887k
文件大小:321k
源码类别:
流媒体/Mpeg4/MP4
开发平台:
Visual C++
- h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy+s->mb_stride]);
- filter_mb(h, mb_x, mb_y+1, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
- } else {
- tprintf(h->s.avctx, "call filter_mbn");
- backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, simple);
- fill_caches(h, mb_type, 1); //FIXME don't fill stuff which isn't used by filter_mb
- filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
- }
- }
- }
- /**
- * Process a macroblock; this case avoids checks for expensive uncommon cases.
- */
- static void hl_decode_mb_simple(H264Context *h){
- hl_decode_mb_internal(h, 1);
- }
- /**
- * Process a macroblock; this handles edge cases, such as interlacing.
- */
- static void av_noinline hl_decode_mb_complex(H264Context *h){
- hl_decode_mb_internal(h, 0);
- }
- static void hl_decode_mb(H264Context *h){
- MpegEncContext * const s = &h->s;
- const int mb_xy= h->mb_xy;
- const int mb_type= s->current_picture.mb_type[mb_xy];
- int is_complex = FRAME_MBAFF || MB_FIELD || IS_INTRA_PCM(mb_type) || s->codec_id != CODEC_ID_H264 || (ENABLE_GRAY && (s->flags&CODEC_FLAG_GRAY)) || s->encoding;
- if(!s->decode)
- return;
- if (is_complex)
- hl_decode_mb_complex(h);
- else hl_decode_mb_simple(h);
- }
- static void pic_as_field(Picture *pic, const int parity){
- int i;
- for (i = 0; i < 4; ++i) {
- if (parity == PICT_BOTTOM_FIELD)
- pic->data[i] += pic->linesize[i];
- pic->reference = parity;
- pic->linesize[i] *= 2;
- }
- }
- static int split_field_copy(Picture *dest, Picture *src,
- int parity, int id_add){
- int match = !!(src->reference & parity);
- if (match) {
- *dest = *src;
- pic_as_field(dest, parity);
- dest->pic_id *= 2;
- dest->pic_id += id_add;
- }
- return match;
- }
- /**
- * Split one reference list into field parts, interleaving by parity
- * as per H.264 spec section 8.2.4.2.5. Output fields have their data pointers
- * set to look at the actual start of data for that field.
- *
- * @param dest output list
- * @param dest_len maximum number of fields to put in dest
- * @param src the source reference list containing fields and/or field pairs
- * (aka short_ref/long_ref, or
- * refFrameListXShortTerm/refFrameListLongTerm in spec-speak)
- * @param src_len number of Picture's in source (pairs and unmatched fields)
- * @param parity the parity of the picture being decoded/needing
- * these ref pics (PICT_{TOP,BOTTOM}_FIELD)
- * @return number of fields placed in dest
- */
- static int split_field_half_ref_list(Picture *dest, int dest_len,
- Picture *src, int src_len, int parity){
- int same_parity = 1;
- int same_i = 0;
- int opp_i = 0;
- int out_i;
- int field_output;
- for (out_i = 0; out_i < dest_len; out_i += field_output) {
- if (same_parity && same_i < src_len) {
- field_output = split_field_copy(dest + out_i, src + same_i,
- parity, 1);
- same_parity = !field_output;
- same_i++;
- } else if (opp_i < src_len) {
- field_output = split_field_copy(dest + out_i, src + opp_i,
- PICT_FRAME - parity, 0);
- same_parity = field_output;
- opp_i++;
- } else {
- break;
- }
- }
- return out_i;
- }
- /**
- * Split the reference frame list into a reference field list.
- * This implements H.264 spec 8.2.4.2.5 for a combined input list.
- * The input list contains both reference field pairs and
- * unmatched reference fields; it is ordered as spec describes
- * RefPicListX for frames in 8.2.4.2.1 and 8.2.4.2.3, except that
- * unmatched field pairs are also present. Conceptually this is equivalent
- * to concatenation of refFrameListXShortTerm with refFrameListLongTerm.
- *
- * @param dest output reference list where ordered fields are to be placed
- * @param dest_len max number of fields to place at dest
- * @param src source reference list, as described above
- * @param src_len number of pictures (pairs and unmatched fields) in src
- * @param parity parity of field being currently decoded
- * (one of PICT_{TOP,BOTTOM}_FIELD)
- * @param long_i index into src array that holds first long reference picture,
- * or src_len if no long refs present.
- */
- static int split_field_ref_list(Picture *dest, int dest_len,
- Picture *src, int src_len,
- int parity, int long_i){
- int i = split_field_half_ref_list(dest, dest_len, src, long_i, parity);
- dest += i;
- dest_len -= i;
- i += split_field_half_ref_list(dest, dest_len, src + long_i,
- src_len - long_i, parity);
- return i;
- }
- /**
- * fills the default_ref_list.
- */
- static int fill_default_ref_list(H264Context *h){
- MpegEncContext * const s = &h->s;
- int i;
- int smallest_poc_greater_than_current = -1;
- int structure_sel;
- Picture sorted_short_ref[32];
- Picture field_entry_list[2][32];
- Picture *frame_list[2];
- if (FIELD_PICTURE) {
- structure_sel = PICT_FRAME;
- frame_list[0] = field_entry_list[0];
- frame_list[1] = field_entry_list[1];
- } else {
- structure_sel = 0;
- frame_list[0] = h->default_ref_list[0];
- frame_list[1] = h->default_ref_list[1];
- }
- if(h->slice_type==FF_B_TYPE){
- int list;
- int len[2];
- int short_len[2];
- int out_i;
- int limit= INT_MIN;
- /* sort frame according to poc in B slice */
- for(out_i=0; out_i<h->short_ref_count; out_i++){
- int best_i=INT_MIN;
- int best_poc=INT_MAX;
- for(i=0; i<h->short_ref_count; i++){
- const int poc= h->short_ref[i]->poc;
- if(poc > limit && poc < best_poc){
- best_poc= poc;
- best_i= i;
- }
- }
- assert(best_i != INT_MIN);
- limit= best_poc;
- sorted_short_ref[out_i]= *h->short_ref[best_i];
- tprintf(h->s.avctx, "sorted poc: %d->%d poc:%d fn:%dn", best_i, out_i, sorted_short_ref[out_i].poc, sorted_short_ref[out_i].frame_num);
- if (-1 == smallest_poc_greater_than_current) {
- if (h->short_ref[best_i]->poc >= s->current_picture_ptr->poc) {
- smallest_poc_greater_than_current = out_i;
- }
- }
- }
- tprintf(h->s.avctx, "current poc: %d, smallest_poc_greater_than_current: %dn", s->current_picture_ptr->poc, smallest_poc_greater_than_current);
- // find the largest poc
- for(list=0; list<2; list++){
- int index = 0;
- int j= -99;
- int step= list ? -1 : 1;
- for(i=0; i<h->short_ref_count && index < h->ref_count[list]; i++, j+=step) {
- int sel;
- while(j<0 || j>= h->short_ref_count){
- if(j != -99 && step == (list ? -1 : 1))
- return -1;
- step = -step;
- j= smallest_poc_greater_than_current + (step>>1);
- }
- sel = sorted_short_ref[j].reference | structure_sel;
- if(sel != PICT_FRAME) continue;
- frame_list[list][index ]= sorted_short_ref[j];
- frame_list[list][index++].pic_id= sorted_short_ref[j].frame_num;
- }
- short_len[list] = index;
- for(i = 0; i < 16 && index < h->ref_count[ list ]; i++){
- int sel;
- if(h->long_ref[i] == NULL) continue;
- sel = h->long_ref[i]->reference | structure_sel;
- if(sel != PICT_FRAME) continue;
- frame_list[ list ][index ]= *h->long_ref[i];
- frame_list[ list ][index++].pic_id= i;
- }
- len[list] = index;
- }
- for(list=0; list<2; list++){
- if (FIELD_PICTURE)
- len[list] = split_field_ref_list(h->default_ref_list[list],
- h->ref_count[list],
- frame_list[list],
- len[list],
- s->picture_structure,
- short_len[list]);
- // swap the two first elements of L1 when L0 and L1 are identical
- if(list && len[0] > 1 && len[0] == len[1])
- for(i=0; h->default_ref_list[0][i].data[0] == h->default_ref_list[1][i].data[0]; i++)
- if(i == len[0]){
- FFSWAP(Picture, h->default_ref_list[1][0], h->default_ref_list[1][1]);
- break;
- }
- if(len[list] < h->ref_count[ list ])
- memset(&h->default_ref_list[list][len[list]], 0, sizeof(Picture)*(h->ref_count[ list ] - len[list]));
- }
- }else{
- int index=0;
- int short_len;
- for(i=0; i<h->short_ref_count; i++){
- int sel;
- sel = h->short_ref[i]->reference | structure_sel;
- if(sel != PICT_FRAME) continue;
- frame_list[0][index ]= *h->short_ref[i];
- frame_list[0][index++].pic_id= h->short_ref[i]->frame_num;
- }
- short_len = index;
- for(i = 0; i < 16; i++){
- int sel;
- if(h->long_ref[i] == NULL) continue;
- sel = h->long_ref[i]->reference | structure_sel;
- if(sel != PICT_FRAME) continue;
- frame_list[0][index ]= *h->long_ref[i];
- frame_list[0][index++].pic_id= i;
- }
- if (FIELD_PICTURE)
- index = split_field_ref_list(h->default_ref_list[0],
- h->ref_count[0], frame_list[0],
- index, s->picture_structure,
- short_len);
- if(index < h->ref_count[0])
- memset(&h->default_ref_list[0][index], 0, sizeof(Picture)*(h->ref_count[0] - index));
- }
- #ifdef TRACE
- for (i=0; i<h->ref_count[0]; i++) {
- tprintf(h->s.avctx, "List0: %s fn:%d 0x%pn", (h->default_ref_list[0][i].long_ref ? "LT" : "ST"), h->default_ref_list[0][i].pic_id, h->default_ref_list[0][i].data[0]);
- }
- if(h->slice_type==FF_B_TYPE){
- for (i=0; i<h->ref_count[1]; i++) {
- tprintf(h->s.avctx, "List1: %s fn:%d 0x%pn", (h->default_ref_list[1][i].long_ref ? "LT" : "ST"), h->default_ref_list[1][i].pic_id, h->default_ref_list[1][i].data[0]);
- }
- }
- #endif
- return 0;
- }
- static void print_short_term(H264Context *h);
- static void print_long_term(H264Context *h);
- /**
- * Extract structure information about the picture described by pic_num in
- * the current decoding context (frame or field). Note that pic_num is
- * picture number without wrapping (so, 0<=pic_num<max_pic_num).
- * @param pic_num picture number for which to extract structure information
- * @param structure one of PICT_XXX describing structure of picture
- * with pic_num
- * @return frame number (short term) or long term index of picture
- * described by pic_num
- */
- static int pic_num_extract(H264Context *h, int pic_num, int *structure){
- MpegEncContext * const s = &h->s;
- *structure = s->picture_structure;
- if(FIELD_PICTURE){
- if (!(pic_num & 1))
- /* opposite field */
- *structure ^= PICT_FRAME;
- pic_num >>= 1;
- }
- return pic_num;
- }
- static int decode_ref_pic_list_reordering(H264Context *h){
- MpegEncContext * const s = &h->s;
- int list, index, pic_structure;
- print_short_term(h);
- print_long_term(h);
- if(h->slice_type==FF_I_TYPE || h->slice_type==FF_SI_TYPE) return 0; //FIXME move before func
- for(list=0; list<h->list_count; list++){
- memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
- if(get_bits1(&s->gb)){
- int pred= h->curr_pic_num;
- for(index=0; ; index++){
- unsigned int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);
- unsigned int pic_id;
- int i;
- Picture *ref = NULL;
- if(reordering_of_pic_nums_idc==3)
- break;
- if(index >= h->ref_count[list]){
- av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflown");
- return -1;
- }
- if(reordering_of_pic_nums_idc<3){
- if(reordering_of_pic_nums_idc<2){
- const unsigned int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
- int frame_num;
- if(abs_diff_pic_num > h->max_pic_num){
- av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflown");
- return -1;
- }
- if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
- else pred+= abs_diff_pic_num;
- pred &= h->max_pic_num - 1;
- frame_num = pic_num_extract(h, pred, &pic_structure);
- for(i= h->short_ref_count-1; i>=0; i--){
- ref = h->short_ref[i];
- assert(ref->reference);
- assert(!ref->long_ref);
- if(ref->data[0] != NULL &&
- ref->frame_num == frame_num &&
- (ref->reference & pic_structure) &&
- ref->long_ref == 0) // ignore non existing pictures by testing data[0] pointer
- break;
- }
- if(i>=0)
- ref->pic_id= pred;
- }else{
- int long_idx;
- pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
- long_idx= pic_num_extract(h, pic_id, &pic_structure);
- if(long_idx>31){
- av_log(h->s.avctx, AV_LOG_ERROR, "long_term_pic_idx overflown");
- return -1;
- }
- ref = h->long_ref[long_idx];
- assert(!(ref && !ref->reference));
- if(ref && (ref->reference & pic_structure)){
- ref->pic_id= pic_id;
- assert(ref->long_ref);
- i=0;
- }else{
- i=-1;
- }
- }
- if (i < 0) {
- av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reordern");
- memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
- } else {
- for(i=index; i+1<h->ref_count[list]; i++){
- if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
- break;
- }
- for(; i > index; i--){
- h->ref_list[list][i]= h->ref_list[list][i-1];
- }
- h->ref_list[list][index]= *ref;
- if (FIELD_PICTURE){
- pic_as_field(&h->ref_list[list][index], pic_structure);
- }
- }
- }else{
- av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idcn");
- return -1;
- }
- }
- }
- }
- for(list=0; list<h->list_count; list++){
- for(index= 0; index < h->ref_count[list]; index++){
- if(!h->ref_list[list][index].data[0])
- h->ref_list[list][index]= s->current_picture;
- }
- }
- if(h->slice_type==FF_B_TYPE && !h->direct_spatial_mv_pred)
- direct_dist_scale_factor(h);
- direct_ref_list_init(h);
- return 0;
- }
- static void fill_mbaff_ref_list(H264Context *h){
- int list, i, j;
- for(list=0; list<2; list++){ //FIXME try list_count
- for(i=0; i<h->ref_count[list]; i++){
- Picture *frame = &h->ref_list[list][i];
- Picture *field = &h->ref_list[list][16+2*i];
- field[0] = *frame;
- for(j=0; j<3; j++)
- field[0].linesize[j] <<= 1;
- field[0].reference = PICT_TOP_FIELD;
- field[1] = field[0];
- for(j=0; j<3; j++)
- field[1].data[j] += frame->linesize[j];
- field[1].reference = PICT_BOTTOM_FIELD;
- h->luma_weight[list][16+2*i] = h->luma_weight[list][16+2*i+1] = h->luma_weight[list][i];
- h->luma_offset[list][16+2*i] = h->luma_offset[list][16+2*i+1] = h->luma_offset[list][i];
- for(j=0; j<2; j++){
- h->chroma_weight[list][16+2*i][j] = h->chroma_weight[list][16+2*i+1][j] = h->chroma_weight[list][i][j];
- h->chroma_offset[list][16+2*i][j] = h->chroma_offset[list][16+2*i+1][j] = h->chroma_offset[list][i][j];
- }
- }
- }
- for(j=0; j<h->ref_count[1]; j++){
- for(i=0; i<h->ref_count[0]; i++)
- h->implicit_weight[j][16+2*i] = h->implicit_weight[j][16+2*i+1] = h->implicit_weight[j][i];
- memcpy(h->implicit_weight[16+2*j], h->implicit_weight[j], sizeof(*h->implicit_weight));
- memcpy(h->implicit_weight[16+2*j+1], h->implicit_weight[j], sizeof(*h->implicit_weight));
- }
- }
- static int pred_weight_table(H264Context *h){
- MpegEncContext * const s = &h->s;
- int list, i;
- int luma_def, chroma_def;
- h->use_weight= 0;
- h->use_weight_chroma= 0;
- h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
- h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
- luma_def = 1<<h->luma_log2_weight_denom;
- chroma_def = 1<<h->chroma_log2_weight_denom;
- for(list=0; list<2; list++){
- for(i=0; i<h->ref_count[list]; i++){
- int luma_weight_flag, chroma_weight_flag;
- luma_weight_flag= get_bits1(&s->gb);
- if(luma_weight_flag){
- h->luma_weight[list][i]= get_se_golomb(&s->gb);
- h->luma_offset[list][i]= get_se_golomb(&s->gb);
- if( h->luma_weight[list][i] != luma_def
- || h->luma_offset[list][i] != 0)
- h->use_weight= 1;
- }else{
- h->luma_weight[list][i]= luma_def;
- h->luma_offset[list][i]= 0;
- }
- chroma_weight_flag= get_bits1(&s->gb);
- if(chroma_weight_flag){
- int j;
- for(j=0; j<2; j++){
- h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
- h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
- if( h->chroma_weight[list][i][j] != chroma_def
- || h->chroma_offset[list][i][j] != 0)
- h->use_weight_chroma= 1;
- }
- }else{
- int j;
- for(j=0; j<2; j++){
- h->chroma_weight[list][i][j]= chroma_def;
- h->chroma_offset[list][i][j]= 0;
- }
- }
- }
- if(h->slice_type != FF_B_TYPE) break;
- }
- h->use_weight= h->use_weight || h->use_weight_chroma;
- return 0;
- }
- static void implicit_weight_table(H264Context *h){
- MpegEncContext * const s = &h->s;
- int ref0, ref1;
- int cur_poc = s->current_picture_ptr->poc;
- if( h->ref_count[0] == 1 && h->ref_count[1] == 1
- && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
- h->use_weight= 0;
- h->use_weight_chroma= 0;
- return;
- }
- h->use_weight= 2;
- h->use_weight_chroma= 2;
- h->luma_log2_weight_denom= 5;
- h->chroma_log2_weight_denom= 5;
- for(ref0=0; ref0 < h->ref_count[0]; ref0++){
- int poc0 = h->ref_list[0][ref0].poc;
- for(ref1=0; ref1 < h->ref_count[1]; ref1++){
- int poc1 = h->ref_list[1][ref1].poc;
- int td = av_clip(poc1 - poc0, -128, 127);
- if(td){
- int tb = av_clip(cur_poc - poc0, -128, 127);
- int tx = (16384 + (FFABS(td) >> 1)) / td;
- int dist_scale_factor = av_clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
- if(dist_scale_factor < -64 || dist_scale_factor > 128)
- h->implicit_weight[ref0][ref1] = 32;
- else
- h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor;
- }else
- h->implicit_weight[ref0][ref1] = 32;
- }
- }
- }
- /**
- * Mark a picture as no longer needed for reference. The refmask
- * argument allows unreferencing of individual fields or the whole frame.
- * If the picture becomes entirely unreferenced, but is being held for
- * display purposes, it is marked as such.
- * @param refmask mask of fields to unreference; the mask is bitwise
- * anded with the reference marking of pic
- * @return non-zero if pic becomes entirely unreferenced (except possibly
- * for display purposes) zero if one of the fields remains in
- * reference
- */
- static inline int unreference_pic(H264Context *h, Picture *pic, int refmask){
- int i;
- if (pic->reference &= refmask) {
- return 0;
- } else {
- if(pic == h->delayed_output_pic)
- pic->reference=DELAYED_PIC_REF;
- else{
- for(i = 0; h->delayed_pic[i]; i++)
- if(pic == h->delayed_pic[i]){
- pic->reference=DELAYED_PIC_REF;
- break;
- }
- }
- return 1;
- }
- }
- /**
- * instantaneous decoder refresh.
- */
- static void idr(H264Context *h){
- int i;
- for(i=0; i<16; i++){
- if (h->long_ref[i] != NULL) {
- unreference_pic(h, h->long_ref[i], 0);
- h->long_ref[i]= NULL;
- }
- }
- h->long_ref_count=0;
- for(i=0; i<h->short_ref_count; i++){
- unreference_pic(h, h->short_ref[i], 0);
- h->short_ref[i]= NULL;
- }
- h->short_ref_count=0;
- }
- /* forget old pics after a seek */
- static void flush_dpb(AVCodecContext *avctx){
- H264Context *h= avctx->priv_data;
- int i;
- for(i=0; i<16; i++) {
- if(h->delayed_pic[i])
- h->delayed_pic[i]->reference= 0;
- h->delayed_pic[i]= NULL;
- }
- if(h->delayed_output_pic)
- h->delayed_output_pic->reference= 0;
- h->delayed_output_pic= NULL;
- idr(h);
- if(h->s.current_picture_ptr)
- h->s.current_picture_ptr->reference= 0;
- h->s.first_field= 0;
- ff_mpeg_flush(avctx);
- }
- /**
- * Find a Picture in the short term reference list by frame number.
- * @param frame_num frame number to search for
- * @param idx the index into h->short_ref where returned picture is found
- * undefined if no picture found.
- * @return pointer to the found picture, or NULL if no pic with the provided
- * frame number is found
- */
- static Picture * find_short(H264Context *h, int frame_num, int *idx){
- MpegEncContext * const s = &h->s;
- int i;
- for(i=0; i<h->short_ref_count; i++){
- Picture *pic= h->short_ref[i];
- if(s->avctx->debug&FF_DEBUG_MMCO)
- av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %pn", i, pic->frame_num, pic);
- if(pic->frame_num == frame_num) {
- *idx = i;
- return pic;
- }
- }
- return NULL;
- }
- /**
- * Remove a picture from the short term reference list by its index in
- * that list. This does no checking on the provided index; it is assumed
- * to be valid. Other list entries are shifted down.
- * @param i index into h->short_ref of picture to remove.
- */
- static void remove_short_at_index(H264Context *h, int i){
- assert(i > 0 && i < h->short_ref_count);
- h->short_ref[i]= NULL;
- if (--h->short_ref_count)
- memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i)*sizeof(Picture*));
- }
- /**
- *
- * @return the removed picture or NULL if an error occurs
- */
- static Picture * remove_short(H264Context *h, int frame_num){
- MpegEncContext * const s = &h->s;
- Picture *pic;
- int i;
- if(s->avctx->debug&FF_DEBUG_MMCO)
- av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %dn", frame_num, h->short_ref_count);
- pic = find_short(h, frame_num, &i);
- if (pic)
- remove_short_at_index(h, i);
- return pic;
- }
- /**
- * Remove a picture from the long term reference list by its index in
- * that list. This does no checking on the provided index; it is assumed
- * to be valid. The removed entry is set to NULL. Other entries are unaffected.
- * @param i index into h->long_ref of picture to remove.
- */
- static void remove_long_at_index(H264Context *h, int i){
- h->long_ref[i]= NULL;
- h->long_ref_count--;
- }
- /**
- *
- * @return the removed picture or NULL if an error occurs
- */
- static Picture * remove_long(H264Context *h, int i){
- Picture *pic;
- pic= h->long_ref[i];
- if (pic)
- remove_long_at_index(h, i);
- return pic;
- }
- /**
- * print short term list
- */
- static void print_short_term(H264Context *h) {
- uint32_t i;
- if(h->s.avctx->debug&FF_DEBUG_MMCO) {
- av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:n");
- for(i=0; i<h->short_ref_count; i++){
- Picture *pic= h->short_ref[i];
- av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %pn", i, pic->frame_num, pic->poc, pic->data[0]);
- }
- }
- }
- /**
- * print long term list
- */
- static void print_long_term(H264Context *h) {
- uint32_t i;
- if(h->s.avctx->debug&FF_DEBUG_MMCO) {
- av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:n");
- for(i = 0; i < 16; i++){
- Picture *pic= h->long_ref[i];
- if (pic) {
- av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %pn", i, pic->frame_num, pic->poc, pic->data[0]);
- }
- }
- }
- }
- /**
- * Executes the reference picture marking (memory management control operations).
- */
- static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
- MpegEncContext * const s = &h->s;
- int i, j;
- int current_ref_assigned=0;
- Picture *pic;
- if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
- av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco heren");
- for(i=0; i<mmco_count; i++){
- int structure, frame_num, unref_pic;
- if(s->avctx->debug&FF_DEBUG_MMCO)
- av_log(h->s.avctx, AV_LOG_DEBUG, "mmco:%d %d %dn", h->mmco[i].opcode, h->mmco[i].short_pic_num, h->mmco[i].long_arg);
- switch(mmco[i].opcode){
- case MMCO_SHORT2UNUSED:
- if(s->avctx->debug&FF_DEBUG_MMCO)
- av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: unref short %d count %dn", h->mmco[i].short_pic_num, h->short_ref_count);
- frame_num = pic_num_extract(h, mmco[i].short_pic_num, &structure);
- pic = find_short(h, frame_num, &j);
- if (pic) {
- if (unreference_pic(h, pic, structure ^ PICT_FRAME))
- remove_short_at_index(h, j);
- } else if(s->avctx->debug&FF_DEBUG_MMCO)
- av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: unref short failuren");
- break;
- case MMCO_SHORT2LONG:
- if (FIELD_PICTURE && mmco[i].long_arg < h->long_ref_count &&
- h->long_ref[mmco[i].long_arg]->frame_num ==
- mmco[i].short_pic_num / 2) {
- /* do nothing, we've already moved this field pair. */
- } else {
- int frame_num = mmco[i].short_pic_num >> FIELD_PICTURE;
- pic= remove_long(h, mmco[i].long_arg);
- if(pic) unreference_pic(h, pic, 0);
- h->long_ref[ mmco[i].long_arg ]= remove_short(h, frame_num);
- if (h->long_ref[ mmco[i].long_arg ]){
- h->long_ref[ mmco[i].long_arg ]->long_ref=1;
- h->long_ref_count++;
- }
- }
- break;
- case MMCO_LONG2UNUSED:
- j = pic_num_extract(h, mmco[i].long_arg, &structure);
- pic = h->long_ref[j];
- if (pic) {
- if (unreference_pic(h, pic, structure ^ PICT_FRAME))
- remove_long_at_index(h, j);
- } else if(s->avctx->debug&FF_DEBUG_MMCO)
- av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: unref long failuren");
- break;
- case MMCO_LONG:
- unref_pic = 1;
- if (FIELD_PICTURE && !s->first_field) {
- if (h->long_ref[mmco[i].long_arg] == s->current_picture_ptr) {
- /* Just mark second field as referenced */
- unref_pic = 0;
- } else if (s->current_picture_ptr->reference) {
- /* First field in pair is in short term list or
- * at a different long term index.
- * This is not allowed; see 7.4.3, notes 2 and 3.
- * Report the problem and keep the pair where it is,
- * and mark this field valid.
- */
- av_log(h->s.avctx, AV_LOG_ERROR,
- "illegal long term reference assignment for second "
- "field in complementary field pair (first field is "
- "short term or has non-matching long index)n");
- unref_pic = 0;
- }
- }
- if (unref_pic) {
- pic= remove_long(h, mmco[i].long_arg);
- if(pic) unreference_pic(h, pic, 0);
- h->long_ref[ mmco[i].long_arg ]= s->current_picture_ptr;
- h->long_ref[ mmco[i].long_arg ]->long_ref=1;
- h->long_ref_count++;
- }
- s->current_picture_ptr->reference |= s->picture_structure;
- current_ref_assigned=1;
- break;
- case MMCO_SET_MAX_LONG:
- assert(mmco[i].long_arg <= 16);
- // just remove the long term which index is greater than new max
- for(j = mmco[i].long_arg; j<16; j++){
- pic = remove_long(h, j);
- if (pic) unreference_pic(h, pic, 0);
- }
- break;
- case MMCO_RESET:
- while(h->short_ref_count){
- pic= remove_short(h, h->short_ref[0]->frame_num);
- if(pic) unreference_pic(h, pic, 0);
- }
- for(j = 0; j < 16; j++) {
- pic= remove_long(h, j);
- if(pic) unreference_pic(h, pic, 0);
- }
- break;
- default: assert(0);
- }
- }
- if (!current_ref_assigned && FIELD_PICTURE &&
- !s->first_field && s->current_picture_ptr->reference) {
- /* Second field of complementary field pair; the first field of
- * which is already referenced. If short referenced, it
- * should be first entry in short_ref. If not, it must exist
- * in long_ref; trying to put it on the short list here is an
- * error in the encoded bit stream (ref: 7.4.3, NOTE 2 and 3).
- */
- if (h->short_ref_count && h->short_ref[0] == s->current_picture_ptr) {
- /* Just mark the second field valid */
- s->current_picture_ptr->reference = PICT_FRAME;
- } else if (s->current_picture_ptr->long_ref) {
- av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term reference "
- "assignment for second field "
- "in complementary field pair "
- "(first field is long term)n");
- } else {
- /*
- * First field in reference, but not in any sensible place on our
- * reference lists. This shouldn't happen unless reference
- * handling somewhere else is wrong.
- */
- assert(0);
- }
- current_ref_assigned = 1;
- }
- if(!current_ref_assigned){
- pic= remove_short(h, s->current_picture_ptr->frame_num);
- if(pic){
- unreference_pic(h, pic, 0);
- av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detectedn");
- }
- if(h->short_ref_count)
- memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
- h->short_ref[0]= s->current_picture_ptr;
- h->short_ref[0]->long_ref=0;
- h->short_ref_count++;
- s->current_picture_ptr->reference |= s->picture_structure;
- }
- if (h->long_ref_count + h->short_ref_count > h->sps.ref_frame_count){
- /* We have too many reference frames, probably due to corrupted
- * stream. Need to discard one frame. Prevents overrun of the
- * short_ref and long_ref buffers.
- */
- av_log(h->s.avctx, AV_LOG_ERROR,
- "number of reference frames exceeds max (probably "
- "corrupt input), discarding onen");
- if (h->long_ref_count) {
- for (i = 0; i < 16; ++i)
- if (h->long_ref[i])
- break;
- assert(i < 16);
- pic = h->long_ref[i];
- remove_long_at_index(h, i);
- } else {
- pic = h->short_ref[h->short_ref_count - 1];
- remove_short_at_index(h, h->short_ref_count - 1);
- }
- unreference_pic(h, pic, 0);
- }
- print_short_term(h);
- print_long_term(h);
- return 0;
- }
- static int decode_ref_pic_marking(H264Context *h, GetBitContext *gb){
- MpegEncContext * const s = &h->s;
- int i;
- if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
- s->broken_link= get_bits1(gb) -1;
- h->mmco[0].long_arg= get_bits1(gb) - 1; // current_long_term_idx
- if(h->mmco[0].long_arg == -1)
- h->mmco_index= 0;
- else{
- h->mmco[0].opcode= MMCO_LONG;
- h->mmco_index= 1;
- }
- }else{
- if(get_bits1(gb)){ // adaptive_ref_pic_marking_mode_flag
- for(i= 0; i<MAX_MMCO_COUNT; i++) {
- MMCOOpcode opcode= get_ue_golomb(gb);
- h->mmco[i].opcode= opcode;
- if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
- h->mmco[i].short_pic_num= (h->curr_pic_num - get_ue_golomb(gb) - 1) & (h->max_pic_num - 1);
- /* if(h->mmco[i].short_pic_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_pic_num ] == NULL){
- av_log(s->avctx, AV_LOG_ERROR, "illegal short ref in memory management control operation %dn", mmco);
- return -1;
- }*/
- }
- if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
- unsigned int long_arg= get_ue_golomb(gb);
- if(long_arg >= 32 || (long_arg >= 16 && !(opcode == MMCO_LONG2UNUSED && FIELD_PICTURE))){
- av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %dn", opcode);
- return -1;
- }
- h->mmco[i].long_arg= long_arg;
- }
- if(opcode > (unsigned)MMCO_LONG){
- av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %dn", opcode);
- return -1;
- }
- if(opcode == MMCO_END)
- break;
- }
- h->mmco_index= i;
- }else{
- assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
- if(h->short_ref_count && h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count &&
- !(FIELD_PICTURE && !s->first_field && s->current_picture_ptr->reference)) {
- h->mmco[0].opcode= MMCO_SHORT2UNUSED;
- h->mmco[0].short_pic_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
- h->mmco_index= 1;
- if (FIELD_PICTURE) {
- h->mmco[0].short_pic_num *= 2;
- h->mmco[1].opcode= MMCO_SHORT2UNUSED;
- h->mmco[1].short_pic_num= h->mmco[0].short_pic_num + 1;
- h->mmco_index= 2;
- }
- }else
- h->mmco_index= 0;
- }
- }
- return 0;
- }
- static int init_poc(H264Context *h){
- MpegEncContext * const s = &h->s;
- const int max_frame_num= 1<<h->sps.log2_max_frame_num;
- int field_poc[2];
- if(h->nal_unit_type == NAL_IDR_SLICE){
- h->frame_num_offset= 0;
- }else{
- if(h->frame_num < h->prev_frame_num)
- h->frame_num_offset= h->prev_frame_num_offset + max_frame_num;
- else
- h->frame_num_offset= h->prev_frame_num_offset;
- }
- if(h->sps.poc_type==0){
- const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
- if(h->nal_unit_type == NAL_IDR_SLICE){
- h->prev_poc_msb=
- h->prev_poc_lsb= 0;
- }
- if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
- h->poc_msb = h->prev_poc_msb + max_poc_lsb;
- else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
- h->poc_msb = h->prev_poc_msb - max_poc_lsb;
- else
- h->poc_msb = h->prev_poc_msb;
- //printf("poc: %d %dn", h->poc_msb, h->poc_lsb);
- field_poc[0] =
- field_poc[1] = h->poc_msb + h->poc_lsb;
- if(s->picture_structure == PICT_FRAME)
- field_poc[1] += h->delta_poc_bottom;
- }else if(h->sps.poc_type==1){
- int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
- int i;
- if(h->sps.poc_cycle_length != 0)
- abs_frame_num = h->frame_num_offset + h->frame_num;
- else
- abs_frame_num = 0;
- if(h->nal_ref_idc==0 && abs_frame_num > 0)
- abs_frame_num--;
- expected_delta_per_poc_cycle = 0;
- for(i=0; i < h->sps.poc_cycle_length; i++)
- expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
- if(abs_frame_num > 0){
- int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
- int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
- expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
- for(i = 0; i <= frame_num_in_poc_cycle; i++)
- expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
- } else
- expectedpoc = 0;
- if(h->nal_ref_idc == 0)
- expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
- field_poc[0] = expectedpoc + h->delta_poc[0];
- field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
- if(s->picture_structure == PICT_FRAME)
- field_poc[1] += h->delta_poc[1];
- }else{
- int poc;
- if(h->nal_unit_type == NAL_IDR_SLICE){
- poc= 0;
- }else{
- if(h->nal_ref_idc) poc= 2*(h->frame_num_offset + h->frame_num);
- else poc= 2*(h->frame_num_offset + h->frame_num) - 1;
- }
- field_poc[0]= poc;
- field_poc[1]= poc;
- }
- if(s->picture_structure != PICT_BOTTOM_FIELD) {
- s->current_picture_ptr->field_poc[0]= field_poc[0];
- s->current_picture_ptr->poc = field_poc[0];
- }
- if(s->picture_structure != PICT_TOP_FIELD) {
- s->current_picture_ptr->field_poc[1]= field_poc[1];
- s->current_picture_ptr->poc = field_poc[1];
- }
- if(!FIELD_PICTURE || !s->first_field) {
- Picture *cur = s->current_picture_ptr;
- cur->poc= FFMIN(cur->field_poc[0], cur->field_poc[1]);
- }
- return 0;
- }
- /**
- * initialize scan tables
- */
- static void init_scan_tables(H264Context *h){
- MpegEncContext * const s = &h->s;
- int i;
- if(s->dsp.h264_idct_add == ff_h264_idct_add_c){ //FIXME little ugly
- memcpy(h->zigzag_scan, zigzag_scan, 16*sizeof(uint8_t));
- memcpy(h-> field_scan, field_scan, 16*sizeof(uint8_t));
- }else{
- for(i=0; i<16; i++){
- #define T(x) (x>>2) | ((x<<2) & 0xF)
- h->zigzag_scan[i] = T(zigzag_scan[i]);
- h-> field_scan[i] = T( field_scan[i]);
- #undef T
- }
- }
- if(s->dsp.h264_idct8_add == ff_h264_idct8_add_c){
- memcpy(h->zigzag_scan8x8, zigzag_scan8x8, 64*sizeof(uint8_t));
- memcpy(h->zigzag_scan8x8_cavlc, zigzag_scan8x8_cavlc, 64*sizeof(uint8_t));
- memcpy(h->field_scan8x8, field_scan8x8, 64*sizeof(uint8_t));
- memcpy(h->field_scan8x8_cavlc, field_scan8x8_cavlc, 64*sizeof(uint8_t));
- }else{
- for(i=0; i<64; i++){
- #define T(x) (x>>3) | ((x&7)<<3)
- h->zigzag_scan8x8[i] = T(zigzag_scan8x8[i]);
- h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
- h->field_scan8x8[i] = T(field_scan8x8[i]);
- h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
- #undef T
- }
- }
- if(h->sps.transform_bypass){ //FIXME same ugly
- h->zigzag_scan_q0 = zigzag_scan;
- h->zigzag_scan8x8_q0 = zigzag_scan8x8;
- h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
- h->field_scan_q0 = field_scan;
- h->field_scan8x8_q0 = field_scan8x8;
- h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
- }else{
- h->zigzag_scan_q0 = h->zigzag_scan;
- h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
- h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
- h->field_scan_q0 = h->field_scan;
- h->field_scan8x8_q0 = h->field_scan8x8;
- h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
- }
- }
- /**
- * Replicates H264 "master" context to thread contexts.
- */
- static void clone_slice(H264Context *dst, H264Context *src)
- {
- memcpy(dst->block_offset, src->block_offset, sizeof(dst->block_offset));
- dst->s.current_picture_ptr = src->s.current_picture_ptr;
- dst->s.current_picture = src->s.current_picture;
- dst->s.linesize = src->s.linesize;
- dst->s.uvlinesize = src->s.uvlinesize;
- dst->s.first_field = src->s.first_field;
- dst->prev_poc_msb = src->prev_poc_msb;
- dst->prev_poc_lsb = src->prev_poc_lsb;
- dst->prev_frame_num_offset = src->prev_frame_num_offset;
- dst->prev_frame_num = src->prev_frame_num;
- dst->short_ref_count = src->short_ref_count;
- memcpy(dst->short_ref, src->short_ref, sizeof(dst->short_ref));
- memcpy(dst->long_ref, src->long_ref, sizeof(dst->long_ref));
- memcpy(dst->default_ref_list, src->default_ref_list, sizeof(dst->default_ref_list));
- memcpy(dst->ref_list, src->ref_list, sizeof(dst->ref_list));
- memcpy(dst->dequant4_coeff, src->dequant4_coeff, sizeof(src->dequant4_coeff));
- memcpy(dst->dequant8_coeff, src->dequant8_coeff, sizeof(src->dequant8_coeff));
- }
- /**
- * decodes a slice header.
- * This will also call MPV_common_init() and frame_start() as needed.
- *
- * @param h h264context
- * @param h0 h264 master context (differs from 'h' when doing sliced based parallel decoding)
- *
- * @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded
- */
- static int decode_slice_header(H264Context *h, H264Context *h0){
- MpegEncContext * const s = &h->s;
- MpegEncContext * const s0 = &h0->s;
- unsigned int first_mb_in_slice;
- unsigned int pps_id;
- int num_ref_idx_active_override_flag;
- static const uint8_t slice_type_map[5]= {FF_P_TYPE, FF_B_TYPE, FF_I_TYPE, FF_SP_TYPE, FF_SI_TYPE};
- unsigned int slice_type, tmp, i;
- int default_ref_list_done = 0;
- int last_pic_structure;
- s->dropable= h->nal_ref_idc == 0;
- if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !h->nal_ref_idc){
- s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
- s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
- }else{
- s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
- s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
- }
- first_mb_in_slice= get_ue_golomb(&s->gb);
- if((s->flags2 & CODEC_FLAG2_CHUNKS) && first_mb_in_slice == 0){
- h0->current_slice = 0;
- if (!s0->first_field)
- s->current_picture_ptr= NULL;
- }
- slice_type= get_ue_golomb(&s->gb);
- if(slice_type > 9){
- av_log(h->s.avctx, AV_LOG_ERROR, "slice type too large (%d) at %d %dn", h->slice_type, s->mb_x, s->mb_y);
- return -1;
- }
- if(slice_type > 4){
- slice_type -= 5;
- h->slice_type_fixed=1;
- }else
- h->slice_type_fixed=0;
- slice_type= slice_type_map[ slice_type ];
- if (slice_type == FF_I_TYPE
- || (h0->current_slice != 0 && slice_type == h0->last_slice_type) ) {
- default_ref_list_done = 1;
- }
- h->slice_type= slice_type;
- s->pict_type= h->slice_type; // to make a few old func happy, it's wrong though
- if (s->pict_type == FF_B_TYPE && s0->last_picture_ptr == NULL) {
- av_log(h->s.avctx, AV_LOG_ERROR,
- "B picture before any references, skippingn");
- return -1;
- }
- pps_id= get_ue_golomb(&s->gb);
- if(pps_id>=MAX_PPS_COUNT){
- av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of rangen");
- return -1;
- }
- if(!h0->pps_buffers[pps_id]) {
- av_log(h->s.avctx, AV_LOG_ERROR, "non existing PPS referencedn");
- return -1;
- }
- h->pps= *h0->pps_buffers[pps_id];
- if(!h0->sps_buffers[h->pps.sps_id]) {
- av_log(h->s.avctx, AV_LOG_ERROR, "non existing SPS referencedn");
- return -1;
- }
- h->sps = *h0->sps_buffers[h->pps.sps_id];
- if(h == h0 && h->dequant_coeff_pps != pps_id){
- h->dequant_coeff_pps = pps_id;
- init_dequant_tables(h);
- }
- s->mb_width= h->sps.mb_width;
- s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
- h->b_stride= s->mb_width*4;
- h->b8_stride= s->mb_width*2;
- s->width = 16*s->mb_width - 2*FFMIN(h->sps.crop_right, 7);
- if(h->sps.frame_mbs_only_flag)
- s->height= 16*s->mb_height - 2*FFMIN(h->sps.crop_bottom, 7);
- else
- s->height= 16*s->mb_height - 4*FFMIN(h->sps.crop_bottom, 3);
- if (s->context_initialized
- && ( s->width != s->avctx->width || s->height != s->avctx->height)) {
- if(h != h0)
- return -1; // width / height changed during parallelized decoding
- free_tables(h);
- MPV_common_end(s);
- }
- if (!s->context_initialized) {
- if(h != h0)
- return -1; // we cant (re-)initialize context during parallel decoding
- if (MPV_common_init(s) < 0)
- return -1;
- s->first_field = 0;
- init_scan_tables(h);
- alloc_tables(h);
- for(i = 1; i < s->avctx->thread_count; i++) {
- H264Context *c;
- c = h->thread_context[i] = av_malloc(sizeof(H264Context));
- memcpy(c, h->s.thread_context[i], sizeof(MpegEncContext));
- memset(&c->s + 1, 0, sizeof(H264Context) - sizeof(MpegEncContext));
- c->sps = h->sps;
- c->pps = h->pps;
- init_scan_tables(c);
- clone_tables(c, h);
- }
- for(i = 0; i < s->avctx->thread_count; i++)
- if(context_init(h->thread_context[i]) < 0)
- return -1;
- s->avctx->width = s->width;
- s->avctx->height = s->height;
- s->avctx->sample_aspect_ratio= h->sps.sar;
- if(!s->avctx->sample_aspect_ratio.den)
- s->avctx->sample_aspect_ratio.den = 1;
- if(h->sps.timing_info_present_flag){
- // s->avctx->time_base= (AVRational){h->sps.num_units_in_tick * 2, h->sps.time_scale};
- if(h->x264_build > 0 && h->x264_build < 44)
- s->avctx->time_base.den *= 2;
- av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
- s->avctx->time_base.num, s->avctx->time_base.den, 1<<30);
- }
- }
- h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
- h->mb_mbaff = 0;
- h->mb_aff_frame = 0;
- last_pic_structure = s0->picture_structure;
- if(h->sps.frame_mbs_only_flag){
- s->picture_structure= PICT_FRAME;
- }else{
- if(get_bits1(&s->gb)) { //field_pic_flag
- s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
- } else {
- s->picture_structure= PICT_FRAME;
- h->mb_aff_frame = h->sps.mb_aff;
- }
- }
- if(h0->current_slice == 0){
- /* See if we have a decoded first field looking for a pair... */
- if (s0->first_field) {
- assert(s0->current_picture_ptr);
- assert(s0->current_picture_ptr->data[0]);
- assert(s0->current_picture_ptr->reference != DELAYED_PIC_REF);
- /* figure out if we have a complementary field pair */
- if (!FIELD_PICTURE || s->picture_structure == last_pic_structure) {
- /*
- * Previous field is unmatched. Don't display it, but let it
- * remain for reference if marked as such.
- */
- s0->current_picture_ptr = NULL;
- s0->first_field = FIELD_PICTURE;
- } else {
- if (h->nal_ref_idc &&
- s0->current_picture_ptr->reference &&
- s0->current_picture_ptr->frame_num != h->frame_num) {
- /*
- * This and previous field were reference, but had
- * different frame_nums. Consider this field first in
- * pair. Throw away previous field except for reference
- * purposes.
- */
- s0->first_field = 1;
- s0->current_picture_ptr = NULL;
- } else {
- /* Second field in complementary pair */
- s0->first_field = 0;
- }
- }
- } else {
- /* Frame or first field in a potentially complementary pair */
- assert(!s0->current_picture_ptr);
- s0->first_field = FIELD_PICTURE;
- }
- if((!FIELD_PICTURE || s0->first_field) && frame_start(h) < 0) {
- s0->first_field = 0;
- return -1;
- }
- }
- if(h != h0)
- clone_slice(h, h0);
- s->current_picture_ptr->frame_num= h->frame_num; //FIXME frame_num cleanup
- assert(s->mb_num == s->mb_width * s->mb_height);
- if(first_mb_in_slice << FIELD_OR_MBAFF_PICTURE >= s->mb_num ||
- first_mb_in_slice >= s->mb_num){
- av_log(h->s.avctx, AV_LOG_ERROR, "first_mb_in_slice overflown");
- return -1;
- }
- s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
- s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << FIELD_OR_MBAFF_PICTURE;
- if (s->picture_structure == PICT_BOTTOM_FIELD)
- s->resync_mb_y = s->mb_y = s->mb_y + 1;
- assert(s->mb_y < s->mb_height);
- if(s->picture_structure==PICT_FRAME){
- h->curr_pic_num= h->frame_num;
- h->max_pic_num= 1<< h->sps.log2_max_frame_num;
- }else{
- h->curr_pic_num= 2*h->frame_num + 1;
- h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
- }
- if(h->nal_unit_type == NAL_IDR_SLICE){
- get_ue_golomb(&s->gb); /* idr_pic_id */
- }
- if(h->sps.poc_type==0){
- h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
- if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
- h->delta_poc_bottom= get_se_golomb(&s->gb);
- }
- }
- if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
- h->delta_poc[0]= get_se_golomb(&s->gb);
- if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
- h->delta_poc[1]= get_se_golomb(&s->gb);
- }
- init_poc(h);
- if(h->pps.redundant_pic_cnt_present){
- h->redundant_pic_count= get_ue_golomb(&s->gb);
- }
- //set defaults, might be overriden a few line later
- h->ref_count[0]= h->pps.ref_count[0];
- h->ref_count[1]= h->pps.ref_count[1];
- if(h->slice_type == FF_P_TYPE || h->slice_type == FF_SP_TYPE || h->slice_type == FF_B_TYPE){
- if(h->slice_type == FF_B_TYPE){
- h->direct_spatial_mv_pred= get_bits1(&s->gb);
- if(FIELD_PICTURE && h->direct_spatial_mv_pred)
- av_log(h->s.avctx, AV_LOG_ERROR, "PAFF + spatial direct mode is not implementedn");
- }
- num_ref_idx_active_override_flag= get_bits1(&s->gb);
- if(num_ref_idx_active_override_flag){
- h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
- if(h->slice_type==FF_B_TYPE)
- h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
- if(h->ref_count[0]-1 > 32-1 || h->ref_count[1]-1 > 32-1){
- av_log(h->s.avctx, AV_LOG_ERROR, "reference overflown");
- h->ref_count[0]= h->ref_count[1]= 1;
- return -1;
- }
- }
- if(h->slice_type == FF_B_TYPE)
- h->list_count= 2;
- else
- h->list_count= 1;
- }else
- h->list_count= 0;
- if(!default_ref_list_done){
- fill_default_ref_list(h);
- }
- if(decode_ref_pic_list_reordering(h) < 0)
- return -1;
- if( (h->pps.weighted_pred && (h->slice_type == FF_P_TYPE || h->slice_type == FF_SP_TYPE ))
- || (h->pps.weighted_bipred_idc==1 && h->slice_type==FF_B_TYPE ) )
- pred_weight_table(h);
- else if(h->pps.weighted_bipred_idc==2 && h->slice_type==FF_B_TYPE)
- implicit_weight_table(h);
- else
- h->use_weight = 0;
- if(h->nal_ref_idc)
- decode_ref_pic_marking(h0, &s->gb);
- if(FRAME_MBAFF)
- fill_mbaff_ref_list(h);
- if( h->slice_type != FF_I_TYPE && h->slice_type != FF_SI_TYPE && h->pps.cabac ){
- tmp = get_ue_golomb(&s->gb);
- if(tmp > 2){
- av_log(s->avctx, AV_LOG_ERROR, "cabac_init_idc overflown");
- return -1;
- }
- h->cabac_init_idc= tmp;
- }
- h->last_qscale_diff = 0;
- tmp = h->pps.init_qp + get_se_golomb(&s->gb);
- if(tmp>51){
- av_log(s->avctx, AV_LOG_ERROR, "QP %u out of rangen", tmp);
- return -1;
- }
- s->qscale= tmp;
- h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
- h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
- //FIXME qscale / qp ... stuff
- if(h->slice_type == FF_SP_TYPE){
- get_bits1(&s->gb); /* sp_for_switch_flag */
- }
- if(h->slice_type==FF_SP_TYPE || h->slice_type == FF_SI_TYPE){
- get_se_golomb(&s->gb); /* slice_qs_delta */
- }
- h->deblocking_filter = 1;
- h->slice_alpha_c0_offset = 0;
- h->slice_beta_offset = 0;
- if( h->pps.deblocking_filter_parameters_present ) {
- tmp= get_ue_golomb(&s->gb);
- if(tmp > 2){
- av_log(s->avctx, AV_LOG_ERROR, "deblocking_filter_idc %u out of rangen", tmp);
- return -1;
- }
- h->deblocking_filter= tmp;
- if(h->deblocking_filter < 2)
- h->deblocking_filter^= 1; // 1<->0
- if( h->deblocking_filter ) {
- h->slice_alpha_c0_offset = get_se_golomb(&s->gb) << 1;
- h->slice_beta_offset = get_se_golomb(&s->gb) << 1;
- }
- }
- if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
- ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type != FF_I_TYPE)
- ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type == FF_B_TYPE)
- ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
- h->deblocking_filter= 0;
- if(h->deblocking_filter == 1 && h0->max_contexts > 1) {
- if(s->avctx->flags2 & CODEC_FLAG2_FAST) {
- /* Cheat slightly for speed:
- Do not bother to deblock across slices. */
- h->deblocking_filter = 2;
- } else {
- h0->max_contexts = 1;
- if(!h0->single_decode_warning) {
- av_log(s->avctx, AV_LOG_INFO, "Cannot parallelize deblocking type 1, decoding such frames in sequential ordern");
- h0->single_decode_warning = 1;
- }
- if(h != h0)
- return 1; // deblocking switched inside frame
- }
- }
- #if 0 //FMO
- if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
- slice_group_change_cycle= get_bits(&s->gb, ?);
- #endif
- h0->last_slice_type = slice_type;
- h->slice_num = ++h0->current_slice;
- h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
- h->emu_edge_height= (FRAME_MBAFF || FIELD_PICTURE) ? 0 : h->emu_edge_width;
- if(s->avctx->debug&FF_DEBUG_PICT_INFO){
- av_log(h->s.avctx, AV_LOG_DEBUG, "slice:%d %s mb:%d %c pps:%u frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s %sn",
- h->slice_num,
- (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
- first_mb_in_slice,
- av_get_pict_type_char(h->slice_type),
- pps_id, h->frame_num,
- s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
- h->ref_count[0], h->ref_count[1],
- s->qscale,
- h->deblocking_filter, h->slice_alpha_c0_offset/2, h->slice_beta_offset/2,
- h->use_weight,
- h->use_weight==1 && h->use_weight_chroma ? "c" : "",
- h->slice_type == FF_B_TYPE ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : ""
- );
- }
- return 0;
- }
- /**
- *
- */
- static inline int get_level_prefix(GetBitContext *gb){
- unsigned int buf;
- int log;
- OPEN_READER(re, gb);
- UPDATE_CACHE(re, gb);
- buf=GET_CACHE(re, gb);
- log= 32 - av_log2(buf);
- #ifdef TRACE
- print_bin(buf>>(32-log), log);
- av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d lpr @%5d in %s get_level_prefixn", buf>>(32-log), log, log-1, get_bits_count(gb), __FILE__);
- #endif
- LAST_SKIP_BITS(re, gb, log);
- CLOSE_READER(re, gb);
- return log-1;
- }
- static inline int get_dct8x8_allowed(H264Context *h){
- int i;
- for(i=0; i<4; i++){
- if(!IS_SUB_8X8(h->sub_mb_type[i])
- || (!h->sps.direct_8x8_inference_flag && IS_DIRECT(h->sub_mb_type[i])))
- return 0;
- }
- return 1;
- }
- /**
- * decodes a residual block.
- * @param n block index
- * @param scantable scantable
- * @param max_coeff number of coefficients in the block
- * @return <0 if an error occurred
- */
- static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff){
- MpegEncContext * const s = &h->s;
- static const int coeff_token_table_index[17]= {0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3};
- int level[16];
- int zeros_left, coeff_num, coeff_token, total_coeff, i, j, trailing_ones, run_before;
- //FIXME put trailing_onex into the context
- if(n == CHROMA_DC_BLOCK_INDEX){
- coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);
- total_coeff= coeff_token>>2;
- }else{
- if(n == LUMA_DC_BLOCK_INDEX){
- total_coeff= pred_non_zero_count(h, 0);
- coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
- total_coeff= coeff_token>>2;
- }else{
- total_coeff= pred_non_zero_count(h, n);
- coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
- total_coeff= coeff_token>>2;
- h->non_zero_count_cache[ scan8[n] ]= total_coeff;
- }
- }
- //FIXME set last_non_zero?
- if(total_coeff==0)
- return 0;
- if(total_coeff > (unsigned)max_coeff) {
- av_log(h->s.avctx, AV_LOG_ERROR, "corrupted macroblock %d %d (total_coeff=%d)n", s->mb_x, s->mb_y, total_coeff);
- return -1;
- }
- trailing_ones= coeff_token&3;
- tprintf(h->s.avctx, "trailing:%d, total:%dn", trailing_ones, total_coeff);
- // av_log(h->s.avctx, AV_LOG_DEBUG, "trailing:%d, total:%dn", trailing_ones, total_coeff);
- assert(total_coeff<=16);
- for(i=0; i<trailing_ones; i++){
- level[i]= 1 - 2*get_bits1(gb);
- }
- if(i<total_coeff) {
- int level_code, mask;
- int suffix_length = total_coeff > 10 && trailing_ones < 3;
- int prefix= get_level_prefix(gb);
- //first coefficient has suffix_length equal to 0 or 1
- if(prefix<14){ //FIXME try to build a large unified VLC table for all this
- if(suffix_length)
- level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
- else
- level_code= (prefix<<suffix_length); //part
- }else if(prefix==14){
- if(suffix_length)
- level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
- else
- level_code= prefix + get_bits(gb, 4); //part
- }else if(prefix==15){
- level_code= (prefix<<suffix_length) + get_bits(gb, 12); //part
- if(suffix_length==0) level_code+=15; //FIXME doesn't make (much)sense
- }else{
- av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %dn", s->mb_x, s->mb_y);
- return -1;
- }
- if(trailing_ones < 3) level_code += 2;
- suffix_length = 1;
- if(level_code > 5)
- suffix_length++;
- mask= -(level_code&1);
- level[i]= (((2+level_code)>>1) ^ mask) - mask;
- i++;
- //remaining coefficients have suffix_length > 0
- for(;i<total_coeff;i++) {
- static const int suffix_limit[7] = {0,5,11,23,47,95,INT_MAX };
- prefix = get_level_prefix(gb);
- if(prefix<15){
- level_code = (prefix<<suffix_length) + get_bits(gb, suffix_length);
- }else if(prefix==15){
- level_code = (prefix<<suffix_length) + get_bits(gb, 12);
- }else{
- av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %dn", s->mb_x, s->mb_y);
- return -1;
- }
- mask= -(level_code&1);
- level[i]= (((2+level_code)>>1) ^ mask) - mask;
- if(level_code > suffix_limit[suffix_length])
- suffix_length++;
- }
- }
- if(total_coeff == max_coeff)
- zeros_left=0;
- else{
- if(n == CHROMA_DC_BLOCK_INDEX)
- zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
- else
- zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);
- }
- coeff_num = zeros_left + total_coeff - 1;
- j = scantable[coeff_num];
- if(n > 24){
- block[j] = level[0];
- for(i=1;i<total_coeff;i++) {
- if(zeros_left <= 0)
- run_before = 0;
- else if(zeros_left < 7){
- run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
- }else{
- run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
- }
- zeros_left -= run_before;
- coeff_num -= 1 + run_before;
- j= scantable[ coeff_num ];
- block[j]= level[i];
- }
- }else{
- block[j] = (level[0] * qmul[j] + 32)>>6;
- for(i=1;i<total_coeff;i++) {
- if(zeros_left <= 0)
- run_before = 0;
- else if(zeros_left < 7){
- run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
- }else{
- run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
- }
- zeros_left -= run_before;
- coeff_num -= 1 + run_before;
- j= scantable[ coeff_num ];
- block[j]= (level[i] * qmul[j] + 32)>>6;
- }
- }
- if(zeros_left<0){
- av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %dn", s->mb_x, s->mb_y);
- return -1;
- }
- return 0;
- }
- static void predict_field_decoding_flag(H264Context *h){
- MpegEncContext * const s = &h->s;
- const int mb_xy= h->mb_xy;
- int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
- ? s->current_picture.mb_type[mb_xy-1]
- : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
- ? s->current_picture.mb_type[mb_xy-s->mb_stride]
- : 0;
- h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
- }
- /**
- * decodes a P_SKIP or B_SKIP macroblock
- */
- static void decode_mb_skip(H264Context *h){
- MpegEncContext * const s = &h->s;
- const int mb_xy= h->mb_xy;
- int mb_type=0;
- memset(h->non_zero_count[mb_xy], 0, 16);
- memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
- if(MB_FIELD)
- mb_type|= MB_TYPE_INTERLACED;
- if( h->slice_type == FF_B_TYPE )
- {
- // just for fill_caches. pred_direct_motion will set the real mb_type
- mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_SKIP;
- fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
- pred_direct_motion(h, &mb_type);
- mb_type|= MB_TYPE_SKIP;
- }
- else
- {
- int mx, my;
- mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
- fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
- pred_pskip_motion(h, &mx, &my);
- fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
- fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
- }
- write_back_motion(h, mb_type);
- s->current_picture.mb_type[mb_xy]= mb_type;
- s->current_picture.qscale_table[mb_xy]= s->qscale;
- h->slice_table[ mb_xy ]= h->slice_num;
- h->prev_mb_skipped= 1;
- }
- static void clear_blocks_c(DCTELEM *blocks)
- {
- memset(blocks, 0, sizeof(DCTELEM)*6*64);
- }
- /**
- * decodes a macroblock
- * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
- */
- static int decode_mb_cavlc(H264Context *h){
- MpegEncContext * const s = &h->s;
- int mb_xy;
- int partition_count;
- unsigned int mb_type, cbp;
- int dct8x8_allowed= h->pps.transform_8x8_mode;
- mb_xy = h->mb_xy = s->mb_x + s->mb_y*s->mb_stride;
- s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?
- // clear_blocks_c(h->mb);
- tprintf(s->avctx, "pic:%d mb:%d/%dn", h->frame_num, s->mb_x, s->mb_y);
- cbp = 0; /* avoid warning. FIXME: find a solution without slowing
- down the code */
- if(h->slice_type != FF_I_TYPE && h->slice_type != FF_SI_TYPE){
- if(s->mb_skip_run==-1)
- s->mb_skip_run= get_ue_golomb(&s->gb);
- if (s->mb_skip_run--) {
- if(FRAME_MBAFF && (s->mb_y&1) == 0){
- if(s->mb_skip_run==0)
- h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
- else
- predict_field_decoding_flag(h);
- }
- decode_mb_skip(h);
- return 0;
- }
- }
- if(FRAME_MBAFF){
- if( (s->mb_y&1) == 0 )
- h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
- }else
- h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
- h->prev_mb_skipped= 0;
- mb_type= get_ue_golomb(&s->gb);
- if(h->slice_type == FF_B_TYPE){
- if(mb_type < 23){
- partition_count= b_mb_type_info[mb_type].partition_count;
- mb_type= b_mb_type_info[mb_type].type;
- }else{
- mb_type -= 23;
- goto decode_intra_mb;
- }
- }else if(h->slice_type == FF_P_TYPE /*|| h->slice_type == FF_SP_TYPE */){
- if(mb_type < 5){
- partition_count= p_mb_type_info[mb_type].partition_count;
- mb_type= p_mb_type_info[mb_type].type;
- }else{
- mb_type -= 5;
- goto decode_intra_mb;
- }
- }else{
- assert(h->slice_type == FF_I_TYPE);
- decode_intra_mb:
- if(mb_type > 25){
- av_log(h->s.avctx, AV_LOG_ERROR, "mb_type %d in %c slice too large at %d %dn", mb_type, av_get_pict_type_char(h->slice_type), s->mb_x, s->mb_y);
- return -1;
- }
- partition_count=0;
- cbp= i_mb_type_info[mb_type].cbp;
- h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
- mb_type= i_mb_type_info[mb_type].type;
- }
- if(MB_FIELD)
- mb_type |= MB_TYPE_INTERLACED;
- h->slice_table[ mb_xy ]= h->slice_num;
- if(IS_INTRA_PCM(mb_type)){
- unsigned int x, y;
- // We assume these blocks are very rare so we do not optimize it.
- align_get_bits(&s->gb);
- // The pixels are stored in the same order as levels in h->mb array.
- for(y=0; y<16; y++){
- const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
- for(x=0; x<16; x++){
- tprintf(s->avctx, "LUMA ICPM LEVEL (%3d)n", show_bits(&s->gb, 8));
- h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= get_bits(&s->gb, 8);
- }
- }
- for(y=0; y<8; y++){
- const int index= 256 + 4*(y&3) + 32*(y>>2);
- for(x=0; x<8; x++){
- tprintf(s->avctx, "CHROMA U ICPM LEVEL (%3d)n", show_bits(&s->gb, 8));
- h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
- }
- }
- for(y=0; y<8; y++){
- const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
- for(x=0; x<8; x++){
- tprintf(s->avctx, "CHROMA V ICPM LEVEL (%3d)n", show_bits(&s->gb, 8));
- h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
- }
- }
- // In deblocking, the quantizer is 0
- s->current_picture.qscale_table[mb_xy]= 0;
- h->chroma_qp[0] = get_chroma_qp(h, 0, 0);
- h->chroma_qp[1] = get_chroma_qp(h, 1, 0);
- // All coeffs are present
- memset(h->non_zero_count[mb_xy], 16, 16);
- s->current_picture.mb_type[mb_xy]= mb_type;
- return 0;
- }
- if(MB_MBAFF){
- h->ref_count[0] <<= 1;
- h->ref_count[1] <<= 1;
- }
- fill_caches(h, mb_type, 0);
- //mb_pred
- if(IS_INTRA(mb_type)){
- int pred_mode;
- // init_top_left_availability(h);
- if(IS_INTRA4x4(mb_type)){
- int i;
- int di = 1;
- if(dct8x8_allowed && get_bits1(&s->gb)){
- mb_type |= MB_TYPE_8x8DCT;
- di = 4;
- }
- // fill_intra4x4_pred_table(h);
- for(i=0; i<16; i+=di){
- int mode= pred_intra_mode(h, i);
- if(!get_bits1(&s->gb)){
- const int rem_mode= get_bits(&s->gb, 3);
- mode = rem_mode + (rem_mode >= mode);
- // av_log(h->s.avctx, AV_LOG_ERROR, "intra =%dn",rem_mode);
- }
- // else
- // av_log(h->s.avctx, AV_LOG_ERROR, "intra =-1n");
- if(di==4)
- fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
- else
- h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
- }
- write_back_intra_pred_mode(h);
- if( check_intra4x4_pred_mode(h) < 0)
- return -1;
- }else{
- h->intra16x16_pred_mode= check_intra_pred_mode(h, h->intra16x16_pred_mode);
- if(h->intra16x16_pred_mode < 0)
- return -1;
- }
- pred_mode= check_intra_pred_mode(h, get_ue_golomb(&s->gb));
- if(pred_mode < 0)
- return -1;
- h->chroma_pred_mode= pred_mode;
- }else if(partition_count==4){
- int i, j, sub_partition_count[4], list, ref[2][4];
- if(h->slice_type == FF_B_TYPE){
- for(i=0; i<4; i++){
- h->sub_mb_type[i]= get_ue_golomb(&s->gb);
- if(h->sub_mb_type[i] >=13){
- av_log(h->s.avctx, AV_LOG_ERROR, "B sub_mb_type %u out of range at %d %dn", h->sub_mb_type[i], s->mb_x, s->mb_y);
- return -1;
- }
- sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
- h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
- }
- if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
- || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3])) {
- pred_direct_motion(h, &mb_type);
- h->ref_cache[0][scan8[4]] =
- h->ref_cache[1][scan8[4]] =
- h->ref_cache[0][scan8[12]] =
- h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
- }
- }else{
- assert(h->slice_type == FF_P_TYPE || h->slice_type == FF_SP_TYPE); //FIXME SP correct ?
- for(i=0; i<4; i++){
- h->sub_mb_type[i]= get_ue_golomb(&s->gb);
- if(h->sub_mb_type[i] >=4){
- av_log(h->s.avctx, AV_LOG_ERROR, "P sub_mb_type %u out of range at %d %dn", h->sub_mb_type[i], s->mb_x, s->mb_y);
- return -1;
- }
- sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
- h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
- }
- }
- for(list=0; list<h->list_count; list++){
- int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
- for(i=0; i<4; i++){
- if(IS_DIRECT(h->sub_mb_type[i])) continue;
- if(IS_DIR(h->sub_mb_type[i], 0, list)){
- unsigned int tmp = get_te0_golomb(&s->gb, ref_count); //FIXME init to 0 before and skip?
- if(tmp>=ref_count){
- av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflown", tmp);
- return -1;
- }
- ref[list][i]= tmp;
- }else{
- //FIXME
- ref[list][i] = -1;
- }
- }
- }
- if(dct8x8_allowed)
- dct8x8_allowed = get_dct8x8_allowed(h);
- for(list=0; list<h->list_count; list++){
- for(i=0; i<4; i++){
- if(IS_DIRECT(h->sub_mb_type[i])) {
- h->ref_cache[list][ scan8[4*i] ] = h->ref_cache[list][ scan8[4*i]+1 ];
- continue;
- }
- h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
- h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
- if(IS_DIR(h->sub_mb_type[i], 0, list)){
- const int sub_mb_type= h->sub_mb_type[i];
- const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
- for(j=0; j<sub_partition_count[i]; j++){
- int mx, my;
- const int index= 4*i + block_width*j;
- int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
- pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
- mx += get_se_golomb(&s->gb);
- my += get_se_golomb(&s->gb);
- tprintf(s->avctx, "final mv:%d %dn", mx, my);
- if(IS_SUB_8X8(sub_mb_type)){
- mv_cache[ 1 ][0]=
- mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
- mv_cache[ 1 ][1]=
- mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
- }else if(IS_SUB_8X4(sub_mb_type)){
- mv_cache[ 1 ][0]= mx;
- mv_cache[ 1 ][1]= my;
- }else if(IS_SUB_4X8(sub_mb_type)){
- mv_cache[ 8 ][0]= mx;
- mv_cache[ 8 ][1]= my;
- }
- mv_cache[ 0 ][0]= mx;
- mv_cache[ 0 ][1]= my;
- }
- }else{
- uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
- p[0] = p[1]=
- p[8] = p[9]= 0;
- }
- }
- }
- }else if(IS_DIRECT(mb_type)){
- pred_direct_motion(h, &mb_type);
- dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
- }else{
- int list, mx, my, i;
- //FIXME we should set ref_idx_l? to 0 if we use that later ...
- if(IS_16X16(mb_type)){
- for(list=0; list<h->list_count; list++){
- unsigned int val;
- if(IS_DIR(mb_type, 0, list)){
- val= get_te0_golomb(&s->gb, h->ref_count[list]);
- if(val >= h->ref_count[list]){
- av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflown", val);
- return -1;
- }
- }else
- val= LIST_NOT_USED&0xFF;
- fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
- }
- for(list=0; list<h->list_count; list++){
- unsigned int val;
- if(IS_DIR(mb_type, 0, list)){
- pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
- mx += get_se_golomb(&s->gb);
- my += get_se_golomb(&s->gb);
- tprintf(s->avctx, "final mv:%d %dn", mx, my);
- val= pack16to32(mx,my);
- }else
- val=0;
- fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, val, 4);
- }
- }
- else if(IS_16X8(mb_type)){
- for(list=0; list<h->list_count; list++){
- for(i=0; i<2; i++){
- unsigned int val;
- if(IS_DIR(mb_type, i, list)){
- val= get_te0_golomb(&s->gb, h->ref_count[list]);
- if(val >= h->ref_count[list]){
- av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflown", val);
- return -1;
- }
- }else
- val= LIST_NOT_USED&0xFF;
- fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);
- }
- }
- for(list=0; list<h->list_count; list++){
- for(i=0; i<2; i++){
- unsigned int val;
- if(IS_DIR(mb_type, i, list)){
- pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
- mx += get_se_golomb(&s->gb);
- my += get_se_golomb(&s->gb);
- tprintf(s->avctx, "final mv:%d %dn", mx, my);
- val= pack16to32(mx,my);
- }else
- val=0;
- fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 4);
- }
- }
- }else{
- assert(IS_8X16(mb_type));
- for(list=0; list<h->list_count; list++){
- for(i=0; i<2; i++){
- unsigned int val;
- if(IS_DIR(mb_type, i, list)){ //FIXME optimize
- val= get_te0_golomb(&s->gb, h->ref_count[list]);
- if(val >= h->ref_count[list]){
- av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflown", val);
- return -1;
- }
- }else
- val= LIST_NOT_USED&0xFF;
- fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
- }
- }
- for(list=0; list<h->list_count; list++){
- for(i=0; i<2; i++){
- unsigned int val;
- if(IS_DIR(mb_type, i, list)){
- pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
- mx += get_se_golomb(&s->gb);
- my += get_se_golomb(&s->gb);
- tprintf(s->avctx, "final mv:%d %dn", mx, my);
- val= pack16to32(mx,my);
- }else
- val=0;
- fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 4);
- }
- }
- }
- }
- if(IS_INTER(mb_type))
- write_back_motion(h, mb_type);
- if(!IS_INTRA16x16(mb_type)){
- cbp= get_ue_golomb(&s->gb);
- if(cbp > 47){
- av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%u) at %d %dn", cbp, s->mb_x, s->mb_y);
- return -1;
- }
- if(IS_INTRA4x4(mb_type))
- cbp= golomb_to_intra4x4_cbp[cbp];
- else
- cbp= golomb_to_inter_cbp[cbp];
- }
- h->cbp = cbp;
- if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){
- if(get_bits1(&s->gb))
- mb_type |= MB_TYPE_8x8DCT;
- }
- s->current_picture.mb_type[mb_xy]= mb_type;
- if(cbp || IS_INTRA16x16(mb_type)){
- int i8x8, i4x4, chroma_idx;
- int dquant;
- GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
- const uint8_t *scan, *scan8x8, *dc_scan;
- // fill_non_zero_count_cache(h);
- if(IS_INTERLACED(mb_type)){
- scan8x8= s->qscale ? h->field_scan8x8_cavlc : h->field_scan8x8_cavlc_q0;
- scan= s->qscale ? h->field_scan : h->field_scan_q0;
- dc_scan= luma_dc_field_scan;
- }else{
- scan8x8= s->qscale ? h->zigzag_scan8x8_cavlc : h->zigzag_scan8x8_cavlc_q0;
- scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
- dc_scan= luma_dc_zigzag_scan;
- }
- dquant= get_se_golomb(&s->gb);
- if( dquant > 25 || dquant < -26 ){
- av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %dn", dquant, s->mb_x, s->mb_y);
- return -1;
- }
- s->qscale += dquant;
- if(((unsigned)s->qscale) > 51){
- if(s->qscale<0) s->qscale+= 52;
- else s->qscale-= 52;
- }
- h->chroma_qp[0]= get_chroma_qp(h, 0, s->qscale);
- h->chroma_qp[1]= get_chroma_qp(h, 1, s->qscale);
- if(IS_INTRA16x16(mb_type)){
- if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, h->dequant4_coeff[0][s->qscale], 16) < 0){
- return -1; //FIXME continue if partitioned and other return -1 too
- }
- assert((cbp&15) == 0 || (cbp&15) == 15);
- if(cbp&15){
- for(i8x8=0; i8x8<4; i8x8++){
- for(i4x4=0; i4x4<4; i4x4++){
- const int index= i4x4 + 4*i8x8;
- if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 ){
- return -1;
- }
- }
- }
- }else{
- fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
- }
- }else{
- for(i8x8=0; i8x8<4; i8x8++){
- if(cbp & (1<<i8x8)){
- if(IS_8x8DCT(mb_type)){
- DCTELEM *buf = &h->mb[64*i8x8];
- uint8_t *nnz;
- for(i4x4=0; i4x4<4; i4x4++){
- if( decode_residual(h, gb, buf, i4x4+4*i8x8, scan8x8+16*i4x4,
- h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 16) <0 )
- return -1;
- }
- nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
- nnz[0] += nnz[1] + nnz[8] + nnz[9];
- }else{
- for(i4x4=0; i4x4<4; i4x4++){
- const int index= i4x4 + 4*i8x8;
- if( decode_residual(h, gb, h->mb + 16*index, index, scan, h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale], 16) <0 ){
- return -1;
- }
- }
- }
- }else{
- uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
- nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
- }
- }
- }
- if(cbp&0x30){
- for(chroma_idx=0; chroma_idx<2; chroma_idx++)
- if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, NULL, 4) < 0){
- return -1;
- }
- }
- if(cbp&0x20){
- for(chroma_idx=0; chroma_idx<2; chroma_idx++){
- const uint32_t *qmul = h->dequant4_coeff[chroma_idx+1+(IS_INTRA( mb_type ) ? 0:3)][h->chroma_qp[chroma_idx]];
- for(i4x4=0; i4x4<4; i4x4++){
- const int index= 16 + 4*chroma_idx + i4x4;
- if( decode_residual(h, gb, h->mb + 16*index, index, scan + 1, qmul, 15) < 0){
- return -1;
- }
- }
- }
- }else{
- uint8_t * const nnz= &h->non_zero_count_cache[0];
- nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
- nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
- }
- }else{
- uint8_t * const nnz= &h->non_zero_count_cache[0];
- fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
- nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
- nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
- }
- s->current_picture.qscale_table[mb_xy]= s->qscale;
- write_back_non_zero_count(h);
- if(MB_MBAFF){
- h->ref_count[0] >>= 1;
- h->ref_count[1] >>= 1;
- }
- return 0;
- }
- static int decode_cabac_field_decoding_flag(H264Context *h) {
- MpegEncContext * const s = &h->s;
- const int mb_x = s->mb_x;
- const int mb_y = s->mb_y & ~1;
- const int mba_xy = mb_x - 1 + mb_y *s->mb_stride;
- const int mbb_xy = mb_x + (mb_y-2)*s->mb_stride;
- unsigned int ctx = 0;
- if( h->slice_table[mba_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) ) {
- ctx += 1;
- }
- if( h->slice_table[mbb_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) ) {
- ctx += 1;
- }
- return get_cabac_noinline( &h->cabac, &h->cabac_state[70 + ctx] );
- }
- static int decode_cabac_intra_mb_type(H264Context *h, int ctx_base, int intra_slice) {
- uint8_t *state= &h->cabac_state[ctx_base];
- int mb_type;
- if(intra_slice){
- MpegEncContext * const s = &h->s;
- const int mba_xy = h->left_mb_xy[0];
- const int mbb_xy = h->top_mb_xy;
- int ctx=0;
- if( h->slice_table[mba_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mba_xy] ) )
- ctx++;
- if( h->slice_table[mbb_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mbb_xy] ) )
- ctx++;
- if( get_cabac_noinline( &h->cabac, &state[ctx] ) == 0 )
- return 0; /* I4x4 */
- state += 2;
- }else{
- if( get_cabac_noinline( &h->cabac, &state[0] ) == 0 )
- return 0; /* I4x4 */
- }
- if( get_cabac_terminate( &h->cabac ) )
- return 25; /* PCM */
- mb_type = 1; /* I16x16 */
- mb_type += 12 * get_cabac_noinline( &h->cabac, &state[1] ); /* cbp_luma != 0 */
- if( get_cabac_noinline( &h->cabac, &state[2] ) ) /* cbp_chroma */
- mb_type += 4 + 4 * get_cabac_noinline( &h->cabac, &state[2+intra_slice] );
- mb_type += 2 * get_cabac_noinline( &h->cabac, &state[3+intra_slice] );
- mb_type += 1 * get_cabac_noinline( &h->cabac, &state[3+2*intra_slice] );
- return mb_type;
- }
- static int decode_cabac_mb_type( H264Context *h ) {
- MpegEncContext * const s = &h->s;
- if( h->slice_type == FF_I_TYPE ) {
- return decode_cabac_intra_mb_type(h, 3, 1);
- } else if( h->slice_type == FF_P_TYPE ) {
- if( get_cabac_noinline( &h->cabac, &h->cabac_state[14] ) == 0 ) {
- /* P-type */
- if( get_cabac_noinline( &h->cabac, &h->cabac_state[15] ) == 0 ) {
- /* P_L0_D16x16, P_8x8 */
- return 3 * get_cabac_noinline( &h->cabac, &h->cabac_state[16] );
- } else {
- /* P_L0_D8x16, P_L0_D16x8 */
- return 2 - get_cabac_noinline( &h->cabac, &h->cabac_state[17] );
- }
- } else {
- return decode_cabac_intra_mb_type(h, 17, 0) + 5;
- }
- } else if( h->slice_type == FF_B_TYPE ) {
- const int mba_xy = h->left_mb_xy[0];
- const int mbb_xy = h->top_mb_xy;
- int ctx = 0;
- int bits;
- if( h->slice_table[mba_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mba_xy] ) )
- ctx++;
- if( h->slice_table[mbb_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mbb_xy] ) )
- ctx++;
- if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+ctx] ) )
- return 0; /* B_Direct_16x16 */
- if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+3] ) ) {
- return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ); /* B_L[01]_16x16 */
- }
- bits = get_cabac_noinline( &h->cabac, &h->cabac_state[27+4] ) << 3;
- bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 2;
- bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 1;
- bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
- if( bits < 8 )
- return bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */
- else if( bits == 13 ) {
- return decode_cabac_intra_mb_type(h, 32, 0) + 23;
- } else if( bits == 14 )
- return 11; /* B_L1_L0_8x16 */
- else if( bits == 15 )
- return 22; /* B_8x8 */
- bits= ( bits<<1 ) | get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
- return bits - 4; /* B_L0_Bi_* through B_Bi_Bi_* */
- } else {
- /* TODO SI/SP frames? */
- return -1;
- }
- }
- static int decode_cabac_mb_skip( H264Context *h, int mb_x, int mb_y ) {
- MpegEncContext * const s = &h->s;
- int mba_xy, mbb_xy;
- int ctx = 0;
- if(FRAME_MBAFF){ //FIXME merge with the stuff in fill_caches?
- int mb_xy = mb_x + (mb_y&~1)*s->mb_stride;
- mba_xy = mb_xy - 1;
- if( (mb_y&1)
- && h->slice_table[mba_xy] == h->slice_num
- && MB_FIELD == !!IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) )
- mba_xy += s->mb_stride;
- if( MB_FIELD ){
- mbb_xy = mb_xy - s->mb_stride;
- if( !(mb_y&1)
- && h->slice_table[mbb_xy] == h->slice_num
- && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) )
- mbb_xy -= s->mb_stride;
- }else
- mbb_xy = mb_x + (mb_y-1)*s->mb_stride;
- }else{
- int mb_xy = h->mb_xy;
- mba_xy = mb_xy - 1;
- mbb_xy = mb_xy - (s->mb_stride << FIELD_PICTURE);
- }
- if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] ))
- ctx++;
- if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] ))
- ctx++;
- if( h->slice_type == FF_B_TYPE )
- ctx += 13;
- return get_cabac_noinline( &h->cabac, &h->cabac_state[11+ctx] );
- }
- static int decode_cabac_mb_intra4x4_pred_mode( H264Context *h, int pred_mode ) {
- int mode = 0;
- if( get_cabac( &h->cabac, &h->cabac_state[68] ) )
- return pred_mode;
- mode += 1 * get_cabac( &h->cabac, &h->cabac_state[69] );
- mode += 2 * get_cabac( &h->cabac, &h->cabac_state[69] );
- mode += 4 * get_cabac( &h->cabac, &h->cabac_state[69] );
- if( mode >= pred_mode )
- return mode + 1;
- else
- return mode;
- }
- static int decode_cabac_mb_chroma_pre_mode( H264Context *h) {
- const int mba_xy = h->left_mb_xy[0];
- const int mbb_xy = h->top_mb_xy;
- int ctx = 0;
- /* No need to test for IS_INTRA4x4 and IS_INTRA16x16, as we set chroma_pred_mode_table to 0 */
- if( h->slice_table[mba_xy] == h->slice_num && h->chroma_pred_mode_table[mba_xy] != 0 )
- ctx++;
- if( h->slice_table[mbb_xy] == h->slice_num && h->chroma_pred_mode_table[mbb_xy] != 0 )
- ctx++;
- if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+ctx] ) == 0 )
- return 0;
- if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
- return 1;
- if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
- return 2;
- else
- return 3;
- }
- static int decode_cabac_mb_cbp_luma( H264Context *h) {
- int cbp_b, cbp_a, ctx, cbp = 0;
- cbp_a = h->slice_table[h->left_mb_xy[0]] == h->slice_num ? h->left_cbp : -1;
- cbp_b = h->slice_table[h->top_mb_xy] == h->slice_num ? h->top_cbp : -1;
- ctx = !(cbp_a & 0x02) + 2 * !(cbp_b & 0x04);
- cbp |= get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]);
- ctx = !(cbp & 0x01) + 2 * !(cbp_b & 0x08);
- cbp |= get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]) << 1;
- ctx = !(cbp_a & 0x08) + 2 * !(cbp & 0x01);
- cbp |= get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]) << 2;
- ctx = !(cbp & 0x04) + 2 * !(cbp & 0x02);
- cbp |= get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]) << 3;
- return cbp;
- }
- static int decode_cabac_mb_cbp_chroma( H264Context *h) {
- int ctx;
- int cbp_a, cbp_b;
- cbp_a = (h->left_cbp>>4)&0x03;
- cbp_b = (h-> top_cbp>>4)&0x03;
- ctx = 0;
- if( cbp_a > 0 ) ctx++;
- if( cbp_b > 0 ) ctx += 2;
- if( get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] ) == 0 )
- return 0;
- ctx = 4;
- if( cbp_a == 2 ) ctx++;
- if( cbp_b == 2 ) ctx += 2;
- return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] );
- }
- static int decode_cabac_mb_dqp( H264Context *h) {
- int ctx = 0;
- int val = 0;
- if( h->last_qscale_diff != 0 )
- ctx++;
- while( get_cabac_noinline( &h->cabac, &h->cabac_state[60 + ctx] ) ) {
- if( ctx < 2 )
- ctx = 2;
- else
- ctx = 3;
- val++;
- if(val > 102) //prevent infinite loop
- return INT_MIN;
- }
- if( val&0x01 )
- return (val + 1)/2;
- else
- return -(val + 1)/2;
- }
- static int decode_cabac_p_mb_sub_type( H264Context *h ) {
- if( get_cabac( &h->cabac, &h->cabac_state[21] ) )
- return 0; /* 8x8 */
- if( !get_cabac( &h->cabac, &h->cabac_state[22] ) )
- return 1; /* 8x4 */
- if( get_cabac( &h->cabac, &h->cabac_state[23] ) )
- return 2; /* 4x8 */
- return 3; /* 4x4 */
- }
- static int decode_cabac_b_mb_sub_type( H264Context *h ) {
- int type;
- if( !get_cabac( &h->cabac, &h->cabac_state[36] ) )
- return 0; /* B_Direct_8x8 */
- if( !get_cabac( &h->cabac, &h->cabac_state[37] ) )
- return 1 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L0_8x8, B_L1_8x8 */
- type = 3;
- if( get_cabac( &h->cabac, &h->cabac_state[38] ) ) {
- if( get_cabac( &h->cabac, &h->cabac_state[39] ) )
- return 11 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L1_4x4, B_Bi_4x4 */
- type += 4;
- }
- type += 2*get_cabac( &h->cabac, &h->cabac_state[39] );
- type += get_cabac( &h->cabac, &h->cabac_state[39] );
- return type;
- }
- static inline int decode_cabac_mb_transform_size( H264Context *h ) {
- return get_cabac_noinline( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] );
- }
- static int decode_cabac_mb_ref( H264Context *h, int list, int n ) {
- int refa = h->ref_cache[list][scan8[n] - 1];
- int refb = h->ref_cache[list][scan8[n] - 8];
- int ref = 0;
- int ctx = 0;
- if( h->slice_type == FF_B_TYPE) {
- if( refa > 0 && !h->direct_cache[scan8[n] - 1] )
- ctx++;
- if( refb > 0 && !h->direct_cache[scan8[n] - 8] )
- ctx += 2;
- } else {
- if( refa > 0 )
- ctx++;
- if( refb > 0 )
- ctx += 2;
- }
- while( get_cabac( &h->cabac, &h->cabac_state[54+ctx] ) ) {
- ref++;
- if( ctx < 4 )
- ctx = 4;
- else
- ctx = 5;
- if(ref >= 32 /*h->ref_list[list]*/){
- av_log(h->s.avctx, AV_LOG_ERROR, "overflow in decode_cabac_mb_refn");
- return 0; //FIXME we should return -1 and check the return everywhere
- }
- }
- return ref;
- }
- static int decode_cabac_mb_mvd( H264Context *h, int list, int n, int l ) {
- int amvd = abs( h->mvd_cache[list][scan8[n] - 1][l] ) +
- abs( h->mvd_cache[list][scan8[n] - 8][l] );
- int ctxbase = (l == 0) ? 40 : 47;
- int ctx, mvd;
- if( amvd < 3 )
- ctx = 0;
- else if( amvd > 32 )
- ctx = 2;
- else
- ctx = 1;
- if(!get_cabac(&h->cabac, &h->cabac_state[ctxbase+ctx]))
- return 0;
- mvd= 1;
- ctx= 3;
- while( mvd < 9 && get_cabac( &h->cabac, &h->cabac_state[ctxbase+ctx] ) ) {
- mvd++;
- if( ctx < 6 )
- ctx++;
- }
- if( mvd >= 9 ) {
- int k = 3;
- while( get_cabac_bypass( &h->cabac ) ) {
- mvd += 1 << k;
- k++;
- if(k>24){
- av_log(h->s.avctx, AV_LOG_ERROR, "overflow in decode_cabac_mb_mvdn");
- return INT_MIN;
- }
- }
- while( k-- ) {
- if( get_cabac_bypass( &h->cabac ) )
- mvd += 1 << k;
- }
- }
- return get_cabac_bypass_sign( &h->cabac, -mvd );
- }
- static inline int get_cabac_cbf_ctx( H264Context *h, int cat, int idx ) {
- int nza, nzb;
- int ctx = 0;
- if( cat == 0 ) {
- nza = h->left_cbp&0x100;
- nzb = h-> top_cbp&0x100;
- } else if( cat == 1 || cat == 2 ) {
- nza = h->non_zero_count_cache[scan8[idx] - 1];
- nzb = h->non_zero_count_cache[scan8[idx] - 8];
- } else if( cat == 3 ) {
- nza = (h->left_cbp>>(6+idx))&0x01;
- nzb = (h-> top_cbp>>(6+idx))&0x01;
- } else {
- assert(cat == 4);
- nza = h->non_zero_count_cache[scan8[16+idx] - 1];
- nzb = h->non_zero_count_cache[scan8[16+idx] - 8];
- }
- if( nza > 0 )
- ctx++;
- if( nzb > 0 )
- ctx += 2;
- return ctx + 4 * cat;
- }
- DECLARE_ASM_CONST(1, uint8_t, last_coeff_flag_offset_8x8[63]) = {
- 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
- 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
- 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
- };
- static void decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff) {
- static const int significant_coeff_flag_offset[2][6] = {
- { 105+0, 105+15, 105+29, 105+44, 105+47, 402 },
- { 277+0, 277+15, 277+29, 277+44, 277+47, 436 }
- };
- static const int last_coeff_flag_offset[2][6] = {
- { 166+0, 166+15, 166+29, 166+44, 166+47, 417 },
- { 338+0, 338+15, 338+29, 338+44, 338+47, 451 }
- };
- static const int coeff_abs_level_m1_offset[6] = {
- 227+0, 227+10, 227+20, 227+30, 227+39, 426
- };
- static const uint8_t significant_coeff_flag_offset_8x8[2][63] = {
- { 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
- 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
- 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
- 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12 },
- { 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
- 6, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11,
- 9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,
- 9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14 }
- };
- /* node ctx: 0..3: abslevel1 (with abslevelgt1 == 0).
- * 4..7: abslevelgt1 + 3 (and abslevel1 doesn't matter).
- * map node ctx => cabac ctx for level=1 */
- static const uint8_t coeff_abs_level1_ctx[8] = { 1, 2, 3, 4, 0, 0, 0, 0 };