h264.c
上传用户:shlianrong
上传日期:2022-07-08
资源大小:309k
文件大小:325k
- if(i[1] < len){
- in[ i[1] ]->pic_id= is_long ? i[1] : in[ i[1] ]->frame_num;
- split_field_copy(&def[index++], in[ i[1]++ ], sel^3, 0);
- }
- }
- return index;
- }
- static int add_sorted(Picture **sorted, Picture **src, int len, int limit, int dir){
- int i, best_poc;
- int out_i= 0;
- for(;;){
- best_poc= dir ? INT_MIN : INT_MAX;
- for(i=0; i<len; i++){
- const int poc= src[i]->poc;
- if(((poc > limit) ^ dir) && ((poc < best_poc) ^ dir)){
- best_poc= poc;
- sorted[out_i]= src[i];
- }
- }
- if(best_poc == (dir ? INT_MIN : INT_MAX))
- break;
- limit= sorted[out_i++]->poc - dir;
- }
- return out_i;
- }
- /**
- * fills the default_ref_list.
- */
- static int fill_default_ref_list(H264Context *h){
- MpegEncContext * const s = &h->s;
- int i, len;
- if(h->slice_type_nos==FF_B_TYPE){
- Picture *sorted[32];
- int cur_poc, list;
- int lens[2];
- if(FIELD_PICTURE)
- cur_poc= s->current_picture_ptr->field_poc[ s->picture_structure == PICT_BOTTOM_FIELD ];
- else
- cur_poc= s->current_picture_ptr->poc;
- for(list= 0; list<2; list++){
- len= add_sorted(sorted , h->short_ref, h->short_ref_count, cur_poc, 1^list);
- len+=add_sorted(sorted+len, h->short_ref, h->short_ref_count, cur_poc, 0^list);
- assert(len<=32);
- len= build_def_list(h->default_ref_list[list] , sorted , len, 0, s->picture_structure);
- len+=build_def_list(h->default_ref_list[list]+len, h->long_ref, 16 , 1, s->picture_structure);
- assert(len<=32);
- if(len < h->ref_count[list])
- memset(&h->default_ref_list[list][len], 0, sizeof(Picture)*(h->ref_count[list] - len));
- lens[list]= len;
- }
- if(lens[0] == lens[1] && lens[1] > 1){
- for(i=0; h->default_ref_list[0][i].data[0] == h->default_ref_list[1][i].data[0] && i<lens[0]; i++);
- if(i == lens[0])
- FFSWAP(Picture, h->default_ref_list[1][0], h->default_ref_list[1][1]);
- }
- }else{
- len = build_def_list(h->default_ref_list[0] , h->short_ref, h->short_ref_count, 0, s->picture_structure);
- len+= build_def_list(h->default_ref_list[0]+len, h-> long_ref, 16 , 1, s->picture_structure);
- assert(len <= 32);
- if(len < h->ref_count[0])
- memset(&h->default_ref_list[0][len], 0, sizeof(Picture)*(h->ref_count[0] - len));
- }
- #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_nos==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);
- 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_31(&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((int)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->frame_num == frame_num &&
- (ref->reference & pic_structure)
- )
- 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]){
- av_log(h->s.avctx, AV_LOG_ERROR, "Missing reference picturen");
- h->ref_list[list][index]= s->current_picture; //FIXME this is not a sensible solution
- }
- }
- }
- 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[0].poc= field[0].field_poc[0];
- field[1] = field[0];
- for(j=0; j<3; j++)
- field[1].data[j] += frame->linesize[j];
- field[1].reference = PICT_BOTTOM_FIELD;
- field[1].poc= field[1].field_poc[1];
- 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;
- }
- if(CHROMA){
- 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_nos != 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 {
- 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++){
- remove_long(h, i, 0);
- }
- assert(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;
- h->prev_frame_num= 0;
- h->prev_frame_num_offset= 0;
- h->prev_poc_msb=
- h->prev_poc_lsb= 0;
- }
- /* forget old pics after a seek */
- static void flush_dpb(AVCodecContext *avctx){
- H264Context *h= avctx->priv_data;
- int i;
- for(i=0; i<MAX_DELAYED_PIC_COUNT; i++) {
- if(h->delayed_pic[i])
- h->delayed_pic[i]->reference= 0;
- h->delayed_pic[i]= NULL;
- }
- h->outputed_poc= INT_MIN;
- 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, int ref_mask){
- 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){
- if(unreference_pic(h, pic, ref_mask))
- remove_short_at_index(h, i);
- }
- return pic;
- }
- /**
- * Remove a picture from the long term reference list by its index in
- * that list.
- * @return the removed picture or NULL if an error occurs
- */
- static Picture * remove_long(H264Context *h, int i, int ref_mask){
- Picture *pic;
- pic= h->long_ref[i];
- if (pic){
- if(unreference_pic(h, pic, ref_mask)){
- assert(h->long_ref[i]->long_ref == 1);
- h->long_ref[i]->long_ref= 0;
- h->long_ref[i]= NULL;
- h->long_ref_count--;
- }
- }
- return pic;
- }
- /**
- * print short term list
- */
- static void print_short_term(H264Context *h) {
- int32_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;
- 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);
- if( mmco[i].opcode == MMCO_SHORT2UNUSED
- || mmco[i].opcode == MMCO_SHORT2LONG){
- frame_num = pic_num_extract(h, mmco[i].short_pic_num, &structure);
- pic = find_short(h, frame_num, &j);
- if(!pic){
- if(mmco[i].opcode != MMCO_SHORT2LONG || !h->long_ref[mmco[i].long_arg]
- || h->long_ref[mmco[i].long_arg]->frame_num != frame_num)
- av_log(h->s.avctx, AV_LOG_ERROR, "mmco: unref short failuren");
- continue;
- }
- }
- 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);
- remove_short(h, frame_num, structure ^ PICT_FRAME);
- break;
- case MMCO_SHORT2LONG:
- if (h->long_ref[mmco[i].long_arg] != pic)
- remove_long(h, mmco[i].long_arg, 0);
- remove_short_at_index(h, j);
- h->long_ref[ mmco[i].long_arg ]= pic;
- 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) {
- remove_long(h, j, structure ^ PICT_FRAME);
- } else if(s->avctx->debug&FF_DEBUG_MMCO)
- av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: unref long failuren");
- break;
- case MMCO_LONG:
- // Comment below left from previous code as it is an interresting note.
- /* First field in pair is in short term list or
- * at a different long term index.
- * This is not allowed; see 7.4.3.3, notes 2 and 3.
- * Report the problem and keep the pair where it is,
- * and mark this field valid.
- */
- if (h->long_ref[mmco[i].long_arg] != s->current_picture_ptr) {
- remove_long(h, mmco[i].long_arg, 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++){
- remove_long(h, j, 0);
- }
- break;
- case MMCO_RESET:
- while(h->short_ref_count){
- remove_short(h, h->short_ref[0]->frame_num, 0);
- }
- for(j = 0; j < 16; j++) {
- remove_long(h, j, 0);
- }
- s->current_picture_ptr->poc=
- s->current_picture_ptr->field_poc[0]=
- s->current_picture_ptr->field_poc[1]=
- h->poc_lsb=
- h->poc_msb=
- h->frame_num=
- s->current_picture_ptr->frame_num= 0;
- break;
- default: assert(0);
- }
- }
- if (!current_ref_assigned) {
- /* 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.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 {
- pic= remove_short(h, s->current_picture_ptr->frame_num, 0);
- if(pic){
- 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_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 && !h->short_ref_count) {
- for (i = 0; i < 16; ++i)
- if (h->long_ref[i])
- break;
- assert(i < 16);
- remove_long(h, i, 0);
- } else {
- pic = h->short_ref[h->short_ref_count - 1];
- remove_short(h, pic->frame_num, 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;
- h->mmco_index= 0;
- if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
- s->broken_link= get_bits1(gb) -1;
- if(get_bits1(gb)){
- h->mmco[0].opcode= MMCO_LONG;
- h->mmco[0].long_arg= 0;
- 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= (MMCOOpcode)get_ue_golomb_31(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_31(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;
- }
- }
- }
- }
- 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];
- Picture *cur = s->current_picture_ptr;
- h->frame_num_offset= h->prev_frame_num_offset;
- if(h->frame_num < h->prev_frame_num)
- h->frame_num_offset += max_frame_num;
- if(h->sps.poc_type==0){
- const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
- 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= 2*(h->frame_num_offset + h->frame_num);
- if(!h->nal_ref_idc)
- poc--;
- 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];
- if(s->picture_structure != PICT_TOP_FIELD)
- s->current_picture_ptr->field_poc[1]= field_poc[1];
- 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;
- unsigned int slice_type, tmp;
- int i, j;
- 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_31(&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= golomb_to_pict_type[ 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;
- h->slice_type_nos= slice_type & 3;
- s->pict_type= h->slice_type; // to make a few old functions 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);
- flush_dpb(s->avctx);
- 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};
- s->avctx->time_base.num = h->sps.num_units_in_tick * 2;
- s->avctx->time_base.den = 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;
- }
- }
- h->mb_field_decoding_flag= s->picture_structure != PICT_FRAME;
- if(h0->current_slice == 0){
- while(h->frame_num != h->prev_frame_num &&
- h->frame_num != (h->prev_frame_num+1)%(1<<h->sps.log2_max_frame_num)){
- av_log(NULL, AV_LOG_DEBUG, "Frame num gap %d %dn", h->frame_num, h->prev_frame_num);
- frame_start(h);
- h->prev_frame_num++;
- h->prev_frame_num %= 1<<h->sps.log2_max_frame_num;
- s->current_picture_ptr->frame_num= h->prev_frame_num;
- execute_ref_pic_marking(h, NULL, 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((int)first_mb_in_slice << FIELD_OR_MBAFF_PICTURE >= s->mb_num || (int)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 overridden a few lines later
- h->ref_count[0]= h->pps.ref_count[0];
- h->ref_count[1]= h->pps.ref_count[1];
- if(h->slice_type_nos != FF_I_TYPE){
- if(h->slice_type_nos == FF_B_TYPE){
- h->direct_spatial_mv_pred= get_bits1(&s->gb);
- }
- 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_nos==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_nos == 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(h->slice_type_nos!=FF_I_TYPE && decode_ref_pic_list_reordering(h) < 0)
- return -1;
- if(h->slice_type_nos!=FF_I_TYPE){
- s->last_picture_ptr= &h->ref_list[0][0];
- ff_copy_picture(&s->last_picture, s->last_picture_ptr);
- }
- if(h->slice_type_nos==FF_B_TYPE){
- s->next_picture_ptr= &h->ref_list[1][0];
- ff_copy_picture(&s->next_picture, s->next_picture_ptr);
- }
- if( (h->pps.weighted_pred && h->slice_type_nos == FF_P_TYPE )
- || (h->pps.weighted_bipred_idc==1 && h->slice_type_nos== FF_B_TYPE ) )
- pred_weight_table(h);
- else if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== 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_nos==FF_B_TYPE && !h->direct_spatial_mv_pred)
- direct_dist_scale_factor(h);
- direct_ref_list_init(h);
- if( h->slice_type_nos != FF_I_TYPE && h->pps.cabac ){
- tmp = get_ue_golomb_31(&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_31(&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_nos != FF_I_TYPE)
- ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type_nos == 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;
- if(h->slice_num >= MAX_SLICES){
- av_log(s->avctx, AV_LOG_ERROR, "Too many slices, increase MAX_SLICES and recompilen");
- }
- for(j=0; j<2; j++){
- int *ref2frm= h->ref2frm[h->slice_num&(MAX_SLICES-1)][j];
- ref2frm[0]=
- ref2frm[1]= -1;
- for(i=0; i<16; i++)
- ref2frm[i+2]= 4*h->ref_list[j][i].frame_num
- +(h->ref_list[j][i].reference&3);
- ref2frm[18+0]=
- ref2frm[18+1]= -1;
- for(i=16; i<48; i++)
- ref2frm[i+4]= 4*h->ref_list[j][i].frame_num
- +(h->ref_list[j][i].reference&3);
- }
- 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;
- s->avctx->refs= h->sps.ref_frame_count;
- if(s->avctx->debug&FF_DEBUG_PICT_INFO){
- av_log(h->s.avctx, AV_LOG_DEBUG, "slice:%d %s mb:%d %c%s%s 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), h->slice_type_fixed ? " fix" : "", h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
- 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){
- if(h->sps.direct_8x8_inference_flag)
- return !(*(uint64_t*)h->sub_mb_type & ((MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_8x8 )*0x0001000100010001ULL));
- else
- return !(*(uint64_t*)h->sub_mb_type & ((MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_8x8|MB_TYPE_DIRECT2)*0x0001000100010001ULL));
- }
- /**
- * 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 > 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);
- assert(total_coeff<=16);
- i = show_bits(gb, 3);
- skip_bits(gb, trailing_ones);
- level[0] = 1-((i&4)>>1);
- level[1] = 1-((i&2) );
- level[2] = 1-((i&1)<<1);
- if(trailing_ones<total_coeff) {
- int mask, prefix;
- int suffix_length = total_coeff > 10 && trailing_ones < 3;
- int bitsi= show_bits(gb, LEVEL_TAB_BITS);
- int level_code= cavlc_level_tab[suffix_length][bitsi][0];
- skip_bits(gb, cavlc_level_tab[suffix_length][bitsi][1]);
- if(level_code >= 100){
- prefix= level_code - 100;
- if(prefix == LEVEL_TAB_BITS)
- 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{
- level_code= (15<<suffix_length) + get_bits(gb, prefix-3); //part
- if(suffix_length==0) level_code+=15; //FIXME doesn't make (much)sense
- if(prefix>=16)
- level_code += (1<<(prefix-3))-4096;
- }
- if(trailing_ones < 3) level_code += 2;
- suffix_length = 2;
- mask= -(level_code&1);
- level[trailing_ones]= (((2+level_code)>>1) ^ mask) - mask;
- }else{
- if(trailing_ones < 3) level_code += (level_code>>31)|1;
- suffix_length = 1;
- if(level_code + 3U > 6U)
- suffix_length++;
- level[trailing_ones]= level_code;
- }
- //remaining coefficients have suffix_length > 0
- for(i=trailing_ones+1;i<total_coeff;i++) {
- static const unsigned int suffix_limit[7] = {0,3,6,12,24,48,INT_MAX };
- int bitsi= show_bits(gb, LEVEL_TAB_BITS);
- level_code= cavlc_level_tab[suffix_length][bitsi][0];
- skip_bits(gb, cavlc_level_tab[suffix_length][bitsi][1]);
- if(level_code >= 100){
- prefix= level_code - 100;
- if(prefix == LEVEL_TAB_BITS){
- prefix += get_level_prefix(gb);
- }
- if(prefix<15){
- level_code = (prefix<<suffix_length) + get_bits(gb, suffix_length);
- }else{
- level_code = (15<<suffix_length) + get_bits(gb, prefix-3);
- if(prefix>=16)
- level_code += (1<<(prefix-3))-4096;
- }
- mask= -(level_code&1);
- level_code= (((2+level_code)>>1) ^ mask) - mask;
- }
- level[i]= level_code;
- if(suffix_limit[suffix_length] + level_code > 2U*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_nos == FF_B_TYPE )
- {
- // just for fill_caches. pred_direct_motion will set the real mb_type
- mb_type|= 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;
- }
- /**
- * 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;
- 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_nos != FF_I_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);
- }
- h->prev_mb_skipped= 0;
- mb_type= get_ue_golomb(&s->gb);
- if(h->slice_type_nos == 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_nos == FF_P_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_nos == FF_I_TYPE);
- if(h->slice_type == FF_SI_TYPE && mb_type)
- mb_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)){
- int x;
- // 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(x=0; x < (CHROMA ? 384 : 256); x++){
- ((uint8_t*)h->mb)[x]= get_bits(&s->gb, 8);
- }
- // In deblocking, the quantizer is 0
- s->current_picture.qscale_table[mb_xy]= 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);
- }
- 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;
- }
- if(CHROMA){
- pred_mode= check_intra_pred_mode(h, get_ue_golomb_31(&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_nos == FF_B_TYPE){
- for(i=0; i<4; i++){
- h->sub_mb_type[i]= get_ue_golomb_31(&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_nos == FF_P_TYPE); //FIXME SP correct ?
- for(i=0; i<4; i++){
- h->sub_mb_type[i]= get_ue_golomb_31(&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;
- if(ref_count == 1){
- tmp= 0;
- }else if(ref_count == 2){
- tmp= get_bits1(&s->gb)^1;
- }else{
- tmp= get_ue_golomb_31(&s->gb);
- if((int)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)){
- if(h->ref_count[list]==1){
- val= 0;
- }else if(h->ref_count[list]==2){
- val= get_bits1(&s->gb)^1;
- }else{
- val= get_ue_golomb_31(&s->gb);
- if((int)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)){
- if(h->ref_count[list] == 1){
- val= 0;
- }else if(h->ref_count[list] == 2){
- val= get_bits1(&s->gb)^1;
- }else{
- val= get_ue_golomb_31(&s->gb);
- if((int)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
- if(h->ref_count[list]==1){
- val= 0;
- }else if(h->ref_count[list]==2){
- val= get_bits1(&s->gb)^1;
- }else{
- val= get_ue_golomb_31(&s->gb);
- if((int)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(CHROMA){
- if(IS_INTRA4x4(mb_type)) cbp= golomb_to_intra4x4_cbp[cbp];
- else cbp= golomb_to_inter_cbp [cbp];
- }else{
- if(IS_INTRA4x4(mb_type)) cbp= golomb_to_intra4x4_cbp_gray[cbp];
- else cbp= golomb_to_inter_cbp_gray[cbp];
- }
- }
- h->cbp = cbp;
- if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){
- if(get_bits1(&s->gb)){
- mb_type |= MB_TYPE_8x8DCT;
- h->cbp_table[mb_xy]= cbp;
- }
- }
- 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_b( H264Context *h ) {
- 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;
- int bits;
- assert(h->slice_type_nos == FF_B_TYPE);
- 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_* */
- }
- 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_nos == 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= h->last_qscale_diff != 0;
- int val = 0;
- while( get_cabac_noinline( &h->cabac, &h->cabac_state[60 + ctx] ) ) {
- ctx= 2+(ctx>>1);
- val++;
- if(val > 102) //prevent infinite loop
- return INT_MIN;
- }
- if( val&0x01 )
- return (val + 1)>>1 ;
- else
- return -((val + 1)>>1);
- }
- 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_nos == 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++;
- ctx = (ctx>>2)+4;
- if(ref >= 32 /*h->ref_list[list]*/){
- return -1;
- }
- }
- 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 mvd;
- int ctx = (amvd>2) + (amvd>32);
- 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 av_always_inline int get_cabac_cbf_ctx( H264Context *h, int cat, int idx, int is_dc ) {
- int nza, nzb;
- int ctx = 0;
- if( is_dc ) {
- if( cat == 0 ) {
- nza = h->left_cbp&0x100;
- nzb = h-> top_cbp&0x100;
- } else {
- nza = (h->left_cbp>>(6+idx))&0x01;
- nzb = (h-> top_cbp>>(6+idx))&0x01;
- }
- } else {
- assert(cat == 1 || cat == 2 || cat == 4);
- nza = h->non_zero_count_cache[scan8[idx] - 1];
- nzb = h->non_zero_count_cache[scan8[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 av_always_inline void decode_cabac_residual_internal( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff, int is_dc ) {
- 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 };
- /* map node ctx => cabac ctx for level>1 */
- static const uint8_t coeff_abs_levelgt1_ctx[8] = { 5, 5, 5, 5, 6, 7, 8, 9 };
- static const uint8_t coeff_abs_level_transition[2][8] = {
- /* update node ctx after decoding a level=1 */
- { 1, 2, 3, 3, 4, 5, 6, 7 },
- /* update node ctx after decoding a level>1 */
- { 4, 4, 4, 4, 5, 6, 7, 7 }
- };
- int index[64];
- int av_unused last;
- int coeff_count = 0;
- int node_ctx = 0;
- uint8_t *significant_coeff_ctx_base;
- uint8_t *last_coeff_ctx_base;
- uint8_t *abs_level_m1_ctx_base;
- #ifndef ARCH_X86
- #define CABAC_ON_STACK
- #endif
- #ifdef CABAC_ON_STACK
- #define CC &cc
- CABACContext cc;
- cc.range = h->cabac.range;
- cc.low = h->cabac.low;
- cc.bytestream= h->cabac.bytestream;
- #else
- #define CC &h->cabac
- #endif
- /* cat: 0-> DC 16x16 n = 0
- * 1-> AC 16x16 n = luma4x4idx
- * 2-> Luma4x4 n = luma4x4idx
- * 3-> DC Chroma n = iCbCr
- * 4-> AC Chroma n = 16 + 4 * iCbCr + chroma4x4idx
- * 5-> Luma8x8 n = 4 * luma8x8idx
- */
- /* read coded block flag */
- if( is_dc || cat != 5 ) {
- if( get_cabac( CC, &h->cabac_state[85 + get_cabac_cbf_ctx( h, cat, n, is_dc ) ] ) == 0 ) {
- if( !is_dc )
- h->non_zero_count_cache[scan8[n]] = 0;
- #ifdef CABAC_ON_STACK
- h->cabac.range = cc.range ;
- h->cabac.low = cc.low ;
- h->cabac.bytestream= cc.bytestream;
- #endif
- return;
- }
- }
- significant_coeff_ctx_base = h->cabac_state
- + significant_coeff_flag_offset[MB_FIELD][cat];
- last_coeff_ctx_base = h->cabac_state
- + last_coeff_flag_offset[MB_FIELD][cat];
- abs_level_m1_ctx_base = h->cabac_state
- + coeff_abs_level_m1_offset[cat];
- if( !is_dc && cat == 5 ) {
- #define DECODE_SIGNIFICANCE( coefs, sig_off, last_off )
- for(last= 0; last < coefs; last++) {
- uint8_t *sig_ctx = significant_coeff_ctx_base + sig_off;
- if( get_cabac( CC, sig_ctx )) {
- uint8_t *last_ctx = last_coeff_ctx_base + last_off;
- index[coeff_count++] = last;
- if( get_cabac( CC, last_ctx ) ) {
- last= max_coeff;
- break;
- }
- }
- }
- if( last == max_coeff -1 ) {
- index[coeff_count++] = last;
- }
- const uint8_t *sig_off = significant_coeff_flag_offset_8x8[MB_FIELD];
- #if defined(ARCH_X86) && defined(HAVE_7REGS) && defined(HAVE_EBX_AVAILABLE) && !defined(BROKEN_RELOCATIONS)
- coeff_count= decode_significance_8x8_x86(CC, significant_coeff_ctx_base, index, sig_off);
- } else {
- coeff_count= decode_significance_x86(CC, max_coeff, significant_coeff_ctx_base, index);
- #else
- DECODE_SIGNIFICANCE( 63, sig_off[last], last_coeff_flag_offset_8x8[last] );
- } else {
- DECODE_SIGNIFICANCE( max_coeff - 1, last, last );
- #endif
- }
- assert(coeff_count > 0);
- if( is_dc ) {
- if( cat == 0 )
- h->cbp_table[h->mb_xy] |= 0x100;
- else
- h->cbp_table[h->mb_xy] |= 0x40 << n;
- } else {
- if( cat == 5 )
- fill_rectangle(&h->non_zero_count_cache[scan8[n]], 2, 2, 8, coeff_count, 1);
- else {
- assert( cat == 1 || cat == 2 || cat == 4 );
- h->non_zero_count_cache[scan8[n]] = coeff_count;
- }
- }
- do {
- uint8_t *ctx = coeff_abs_level1_ctx[node_ctx] + abs_level_m1_ctx_base;
- int j= scantable[index[--coeff_count]];
- if( get_cabac( CC, ctx ) == 0 )
- {
- int32_t ret;
- node_ctx = coeff_abs_level_transition[0][node_ctx];
- if( is_dc )
- {
- block[j] = get_cabac_bypass_sign( CC, -1);
- }
- else
- {
- //block[j] = (get_cabac_bypass_sign( CC, -qmul[j]) + 32) >> 6;
- ret = qmul[j];
- block[j] = (get_cabac_bypass_sign( CC, -ret) + 32) >> 6;
- }
- }
- else
- {
- int coeff_abs = 2;
- ctx = coeff_abs_levelgt1_ctx[node_ctx] + abs_level_m1_ctx_base;
- node_ctx = coeff_abs_level_transition[1][node_ctx];
- while( coeff_abs < 15 && get_cabac( CC, ctx ) ) {
- coeff_abs++;
- }
- if( coeff_abs >= 15 ) {
- int j = 0;
- while( get_cabac_bypass( CC ) ) {
- j++;
- }
- coeff_abs=1;
- while( j-- ) {
- coeff_abs += coeff_abs + get_cabac_bypass( CC );
- }
- coeff_abs+= 14;
- }
- if( is_dc ) {
- block[j] = get_cabac_bypass_sign( CC, -coeff_abs );
- }else{
- block[j] = (get_cabac_bypass_sign( CC, -coeff_abs ) * qmul[j] + 32) >> 6;
- }
- }
- } while( coeff_count );
- #ifdef CABAC_ON_STACK
- h->cabac.range = cc.range ;
- h->cabac.low = cc.low ;
- h->cabac.bytestream= cc.bytestream;
- #endif
- }
- #ifndef CONFIG_SMALL
- static void decode_cabac_residual_dc( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff ) {
- decode_cabac_residual_internal(h, block, cat, n, scantable, qmul, max_coeff, 1);
- }
- static void decode_cabac_residual_nondc( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff ) {
- decode_cabac_residual_internal(h, block, cat, n, scantable, qmul, max_coeff, 0);
- }
- #endif
- static void decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff ) {
- #ifdef CONFIG_SMALL
- decode_cabac_residual_internal(h, block, cat, n, scantable, qmul, max_coeff, cat == 0 || cat == 3);
- #else
- if( cat == 0 || cat == 3 ) decode_cabac_residual_dc(h, block, cat, n, scantable, qmul, max_coeff);
- else decode_cabac_residual_nondc(h, block, cat, n, scantable, qmul, max_coeff);
- #endif
- }
- static inline void compute_mb_neighbors(H264Context *h)
- {
- MpegEncContext * const s = &h->s;
- const int mb_xy = h->mb_xy;
- h->top_mb_xy = mb_xy - s->mb_stride;
- h->left_mb_xy[0] = mb_xy - 1;
- if(FRAME_MBAFF){
- const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
- const int top_pair_xy = pair_xy - s->mb_stride;
- const int top_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
- const int left_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
- const int curr_mb_field_flag = MB_FIELD;
- const int bottom = (s->mb_y & 1);
- if (curr_mb_field_flag && (bottom || top_mb_field_flag)){
- h->top_mb_xy -= s->mb_stride;
- }
- if (!left_mb_field_flag == curr_mb_field_flag) {
- h->left_mb_xy[0] = pair_xy - 1;
- }
- } else if (FIELD_PICTURE) {
- h->top_mb_xy -= s->mb_stride;
- }
- return;
- }
- /**
- * decodes a macroblock
- * @returns 0 if OK, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
- */
- static int decode_mb_cabac(H264Context *h) {
- MpegEncContext * const s = &h->s;
- int mb_xy;
- int mb_type, partition_count, cbp = 0;
- int dct8x8_allowed= h->pps.transform_8x8_mode;
- mb_xy = h->mb_xy = s->mb_x + s->mb_y*s->mb_stride;
- tprintf(s->avctx, "pic:%d mb:%d/%dn", h->frame_num, s->mb_x, s->mb_y);
- if( h->slice_type_nos != FF_I_TYPE ) {
- int skip;
- /* a skipped mb needs the aff flag from the following mb */
- if( FRAME_MBAFF && s->mb_x==0 && (s->mb_y&1)==0 )
- predict_field_decoding_flag(h);
- if( FRAME_MBAFF && (s->mb_y&1)==1 && h->prev_mb_skipped )
- skip = h->next_mb_skipped;
- else
- skip = decode_cabac_mb_skip( h, s->mb_x, s->mb_y );
- /* read skip flags */
- if( skip ) {
- if( FRAME_MBAFF && (s->mb_y&1)==0 ){
- s->current_picture.mb_type[mb_xy] = MB_TYPE_SKIP;
- h->next_mb_skipped = decode_cabac_mb_skip( h, s->mb_x, s->mb_y+1 );
- if(!h->next_mb_skipped)
- h->mb_mbaff = h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
- }
- decode_mb_skip(h);
- h->cbp_table[mb_xy] = 0;
- h->chroma_pred_mode_table[mb_xy] = 0;
- h->last_qscale_diff = 0;
- return 0;
- }
- }
- if(FRAME_MBAFF){
- if( (s->mb_y&1) == 0 )
- h->mb_mbaff =
- h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
- }
- h->prev_mb_skipped = 0;
- compute_mb_neighbors(h);
- if( h->slice_type_nos == FF_B_TYPE ) {
- mb_type = decode_cabac_mb_type_b( h );
- 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_nos == 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 */
- mb_type= 3 * get_cabac_noinline( &h->cabac, &h->cabac_state[16] );
- } else {
- /* P_L0_D8x16, P_L0_D16x8 */
- mb_type= 2 - get_cabac_noinline( &h->cabac, &h->cabac_state[17] );
- }
- partition_count= p_mb_type_info[mb_type].partition_count;
- mb_type= p_mb_type_info[mb_type].type;
- } else {
- mb_type= decode_cabac_intra_mb_type(h, 17, 0);
- goto decode_intra_mb;
- }
- } else {
- mb_type= decode_cabac_intra_mb_type(h, 3, 1);
- if(h->slice_type == FF_SI_TYPE && mb_type)
- mb_type--;
- assert(h->slice_type_nos == FF_I_TYPE);