void init_frame_buffers(InputParameters *inp, ImageParameters *img) { int i; //for (i=0;i<img->buf_cycle;i++) for (i=0;i<2;i++) { get_mem2D(&(mref_frm[i]), (img->height+2*IMG_PAD_SIZE)*4, (img->width+2*IMG_PAD_SIZE)*4); } if(!(input->InterlaceCodingOption == FRAME_CODING)) //add by wuzhongmou 0610 { //for (i=0;i<2*img->buf_cycle;i++) for (i=0;i<4;i++) { get_mem2D(&(mref_fld[i]), (img->height/2+2*IMG_PAD_SIZE)*4, (img->width+2*IMG_PAD_SIZE)*4); //X ZHENG,20080515 } } }
int get_mem_FME() { int memory_size = 0; memory_size += get_mem2Dint(&McostState, 2*input->search_range+1, 2*input->search_range+1); memory_size += get_mem_mincost (&(all_mincost)); memory_size += get_mem_bwmincost(&(all_bwmincost)); memory_size += get_mem2D(&SearchState,7,7); return memory_size; }
// same change as in get_mem2Dint int get_mem3D(byte ****array3D, int frames, int rows, int columns) { int j; if(((*array3D) = (byte***)calloc(frames,sizeof(byte**))) == NULL) no_mem_exit("get_mem3D: array3D"); for(j=0;j<frames;j++) get_mem2D( (*array3D)+j, rows, columns ) ; return frames*rows*columns; }
// same change as in get_mem2Dint int get_mem3D(byte ****array3D, int frames, int rows, int columns) { int j; if(((*array3D) = (byte***) h264_malloc(frames * sizeof(byte**))) == NULL) { printf("get_mem3D: array3D"); exit(0); } for(j=0;j<frames;j++) { get_mem2D( (*array3D)+j, rows, columns) ; } return frames*rows*columns; }
/*! ************************************************************************ * \brief * Dynamic memory allocation of frame size related global buffers * buffers are defined in global.h, allocated memory must be freed in * void free_global_buffers() * * \par Input: * Input Parameters struct inp_par *inp, Image Parameters struct img_par *img * * \par Output: * Number of allocated bytes *********************************************************************** */ int init_global_buffers() { int memory_size=0; if (global_init_done) { free_global_buffers(); } // allocate memory for reference frame in find_snr memory_size += get_mem2D(&imgY_ref, img->height, img->width); memory_size += get_mem3D(&imgUV_ref, 2, img->height_cr, img->width_cr); // allocate memory in structure img if(((img->mb_data) = (Macroblock *) calloc(img->FrameSizeInMbs, sizeof(Macroblock))) == NULL) no_mem_exit("init_global_buffers: img->mb_data"); if(((img->intra_block) = (int*)calloc(img->FrameSizeInMbs, sizeof(int))) == NULL) no_mem_exit("init_global_buffers: img->intra_block"); memory_size += get_mem2Dint(&(img->ipredmode), 4*img->PicWidthInMbs , 4*img->FrameHeightInMbs); memory_size += get_mem2Dint(&(img->field_anchor),4*img->FrameHeightInMbs, 4*img->PicWidthInMbs); memory_size += get_mem3Dint(&(img->wp_weight), 2, MAX_REFERENCE_PICTURES, 3); memory_size += get_mem3Dint(&(img->wp_offset), 6, MAX_REFERENCE_PICTURES, 3); memory_size += get_mem4Dint(&(img->wbp_weight), 6, MAX_REFERENCE_PICTURES, MAX_REFERENCE_PICTURES, 3); // CAVLC mem memory_size += get_mem3Dint(&(img->nz_coeff), img->FrameSizeInMbs, 4, 6); memory_size += get_mem2Dint(&(img->siblock),img->PicWidthInMbs , img->FrameHeightInMbs); global_init_done = 1; img->oldFrameSizeInMbs = img->FrameSizeInMbs; return (memory_size); }
/*! ************************************************************************ * \brief * Dynamic memory allocation of frame size related global buffers * buffers are defined in global.h, allocated memory must be freed in * void free_global_buffers() * * \par Input: * Input Parameters struct inp_par *inp, Image Parameters struct img_par *img * * \par Output: * Number of allocated bytes *----------------------------------------------------------------------- * Function Argument List Changed [Removing Global Variables] * Input parameters added are * - h264_decoder* dec_params * * <*****@*****.**> *********************************************************************** */ int init_global_buffers_baseline( h264_decoder* dec_params ) { ImageParameters* img = dec_params->img; int memory_size=0; int quad_range, i; if (dec_params->global_init_done) { free_global_buffers_baseline( dec_params ); } if(((img->slice_nr) = (char *) h264_malloc(img->FrameSizeInMbs * sizeof(char))) == NULL) { printf("init_global_buffers: img->slice_nr"); exit(0); } if(((img->ei_flag) = (char *) h264_malloc(img->FrameSizeInMbs * sizeof(char))) == NULL) { printf("init_global_buffers: img->ei_flag"); exit(0); } /////////////////////////////////////////////////////////////////////////////////// // Initializations for the new mb_data structure inside the ImageParam structure // /////////////////////////////////////////////////////////////////////////////////// if((img->mb_data1.cbp_blk = (short*) h264_malloc(img->FrameSizeInMbs*sizeof(short))) == NULL) { printf("init_global_buffers: img->mb_data1->cbp_blk"); exit(0); } if((img->mb_data1.LFAlphaC0Offset = (char*) h264_malloc(img->FrameSizeInMbs*sizeof(char))) == NULL) { printf("init_global_buffers: img->mb_data1.LFAlphaC0Offset"); exit(0); } if((img->mb_data1.LFBetaOffset = (char*) h264_malloc(img->FrameSizeInMbs*sizeof(char))) == NULL) { printf("init_global_buffers: img->mb_data1.LFBetaC0Offset"); exit(0); } if((img->mb_data1.LFDisableIdc = (char*) h264_malloc(img->FrameSizeInMbs*sizeof(char))) == NULL) { printf("init_global_buffers: img->mb_data1.LFDisableIdc"); exit(0); } if((img->mb_data1.mb_type = (char*) h264_malloc(img->FrameSizeInMbs*sizeof(char))) == NULL) { printf("init_global_buffers: img->mb_data1.mb_type"); exit(0); } if((img->mb_data1.partition = (char*) h264_malloc(img->FrameSizeInMbs*sizeof(char))) == NULL) { printf("init_global_buffers: img->mb_data1.partition"); exit(0); } if((img->mb_data1.mbAvailA = (char*) h264_malloc(img->FrameSizeInMbs*sizeof(char))) == NULL) { printf("init_global_buffers: img->mb_data1.mbAvailA"); exit(0); } if((img->mb_data1.mbAvailB = (char*) h264_malloc(img->FrameSizeInMbs*sizeof(char))) == NULL) { printf("init_global_buffers: img->mb_data1.mbAvailB"); exit(0); } if((img->mb_data1.NoMbPartLessThan8x8Flag = (char*) h264_malloc(img->FrameSizeInMbs*sizeof(char))) == NULL) { printf("init_global_buffers: img->mb_data1.NoMbPartLessThan8x8Flag"); exit(0); } if((img->mb_data1.qp = (char*) h264_malloc(img->FrameSizeInMbs*sizeof(char))) == NULL) { printf("init_global_buffers: img->mb_data1.qp"); exit(0); } /////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////// if(((dec_params->img->intra_block) = (int*)h264_malloc(dec_params->img->FrameSizeInMbs * sizeof(int))) == NULL) { printf("init_global_buffers: img->intra_block"); exit(0); } memory_size += get_mem2D(&(dec_params->img->ipredmode), dec_params->img->FrameWidthInMbs<<2 , dec_params->img->FrameHeightInMbs<<2); memory_size += get_mem3Dint(&(dec_params->img->wp_weight), 2, MAX_REFERENCE_PICTURES, 3); memory_size += get_mem3Dint(&(dec_params->img->wp_offset), 6, MAX_REFERENCE_PICTURES, 3); memory_size += get_mem4Dint(&(dec_params->img->wbp_weight), 6, MAX_REFERENCE_PICTURES, MAX_REFERENCE_PICTURES, 3); // dec_params->img->nz_coeff = h264_malloc ((dec_params->img->FrameSizeInMbs<<5)*sizeof(char)); dec_params->img->nz_coeff1 = (char *)h264_malloc ((dec_params->img->FrameSizeInMbs*24)*sizeof(char)); // dec_params->img->nz_coeff1[1] = dec_params->img->nz_coeff1[0] + dec_params->img->FrameSizeInMbs*16; // dec_params->img->nz_coeff1[2] = dec_params->img->nz_coeff1[1] + dec_params->img->FrameSizeInMbs*4; memory_size += (dec_params->img->FrameSizeInMbs<<5)*sizeof(char); quad_range = (MAX_IMGPEL_VALUE + 1) <<1; //if ((dec_params->img->quad = (int*)calloc (quad_range, sizeof(int))) == NULL) /* if ((dec_params->img->quad = (int*) h264_malloc (quad_range * sizeof(int))) == NULL) { printf("init_img: img->quad"); exit(0); } for (i=0; i < quad_range>>1; ++i) { dec_params->img->quad[i]=i*i; } */ // create all context models dec_params->mot_ctx = (MotionInfoContexts *)create_contexts_MotionInfo(); dec_params->tex_ctx = (TextureInfoContexts *)create_contexts_TextureInfo(); dec_params->global_init_done = 1; // dec_params->img->oldFrameSizeInMbs = dec_params->img->FrameSizeInMbs; return (memory_size); }
/*! ************************************************************************ * \brief * Interpret the spare picture SEI message * \param payload * a pointer that point to the sei payload * \param size * the size of the sei message * \param img * the image pointer * ************************************************************************ */ void interpret_spare_pic( byte* payload, int size, ImageParameters *img ) { int i,x,y; Bitstream* buf; int bit0, bit1, bitc, no_bit0; int target_frame_num; int num_spare_pics; int delta_spare_frame_num, CandidateSpareFrameNum, SpareFrameNum = 0; int ref_area_indicator; int m, n, left, right, top, bottom,directx, directy; byte ***map; #ifdef WRITE_MAP_IMAGE int j, k, i0, j0, tmp, kk; char filename[20] = "map_dec.yuv"; FILE *fp; byte** Y; static int old_pn=-1; static int first = 1; printf("Spare picture SEI message\n"); #endif UsedBits = 0; assert( payload!=NULL); assert( img!=NULL); #ifdef ESLCPP buf = (Bitstream*)malloc(sizeof(Bitstream)); #else buf = malloc(sizeof(Bitstream)); #endif buf->bitstream_length = size; buf->streamBuffer = payload; buf->frame_bitoffset = 0; target_frame_num = ue_v("SEI: target_frame_num", buf); #ifdef WRITE_MAP_IMAGE printf( "target_frame_num is %d\n", target_frame_num ); #endif num_spare_pics = 1 + ue_v("SEI: num_spare_pics_minus1", buf); #ifdef WRITE_MAP_IMAGE printf( "num_spare_pics is %d\n", num_spare_pics ); #endif get_mem3D(&map, num_spare_pics, img->height/16, img->width/16); for (i=0; i<num_spare_pics; i++) { if (i==0) { CandidateSpareFrameNum = target_frame_num - 1; if ( CandidateSpareFrameNum < 0 ) CandidateSpareFrameNum = MAX_FN - 1; } else CandidateSpareFrameNum = SpareFrameNum; delta_spare_frame_num = ue_v("SEI: delta_spare_frame_num", buf); SpareFrameNum = CandidateSpareFrameNum - delta_spare_frame_num; if( SpareFrameNum < 0 ) SpareFrameNum = MAX_FN + SpareFrameNum; ref_area_indicator = ue_v("SEI: ref_area_indicator", buf); switch ( ref_area_indicator ) { case 0: // The whole frame can serve as spare picture for (y=0; y<img->height/16; y++) for (x=0; x<img->width/16; x++) map[i][y][x] = 0; break; case 1: // The map is not compressed for (y=0; y<img->height/16; y++) for (x=0; x<img->width/16; x++) { map[i][y][x] = u_1("SEI: ref_mb_indicator", buf); } break; case 2: // The map is compressed //!KS: could not check this function, description is unclear (as stated in Ed. Note) bit0 = 0; bit1 = 1; bitc = bit0; no_bit0 = -1; x = ( img->width/16 - 1 ) / 2; y = ( img->height/16 - 1 ) / 2; left = right = x; top = bottom = y; directx = 0; directy = 1; for (m=0; m<img->height/16; m++) for (n=0; n<img->width/16; n++) { if (no_bit0<0) { no_bit0 = ue_v("SEI: zero_run_length", buf); } if (no_bit0>0) map[i][y][x] = bit0; else map[i][y][x] = bit1; no_bit0--; // go to the next mb: if ( directx == -1 && directy == 0 ) { if (x > left) x--; else if (x == 0) { y = bottom + 1; bottom++; directx = 1; directy = 0; } else if (x == left) { x--; left--; directx = 0; directy = 1; } } else if ( directx == 1 && directy == 0 ) { if (x < right) x++; else if (x == img->width/16 - 1) { y = top - 1; top--; directx = -1; directy = 0; } else if (x == right) { x++; right++; directx = 0; directy = -1; } } else if ( directx == 0 && directy == -1 ) { if ( y > top) y--; else if (y == 0) { x = left - 1; left--; directx = 0; directy = 1; } else if (y == top) { y--; top--; directx = -1; directy = 0; } } else if ( directx == 0 && directy == 1 ) { if (y < bottom) y++; else if (y == img->height/16 - 1) { x = right+1; right++; directx = 0; directy = -1; } else if (y == bottom) { y++; bottom++; directx = 1; directy = 0; } } } break; default: printf( "Wrong ref_area_indicator %d!\n", ref_area_indicator ); exit(0); break; } } // end of num_spare_pics #ifdef WRITE_MAP_IMAGE // begin to write map seq if ( old_pn != img->number ) { old_pn = img->number; get_mem2D(&Y, img->height, img->width); if (first) { fp = fopen( filename, "wb" ); first = 0; } else fp = fopen( filename, "ab" ); assert( fp != NULL ); for (kk=0; kk<num_spare_pics; kk++) { for (i=0; i < img->height/16; i++) for (j=0; j < img->width/16; j++) { tmp=map[kk][i][j]==0? 255 : 0; for (i0=0; i0<16; i0++) for (j0=0; j0<16; j0++) Y[i*16+i0][j*16+j0]=tmp; } // write the map image for (i=0; i < img->height; i++) for (j=0; j < img->width; j++) fputc(Y[i][j], fp); for (k=0; k < 2; k++) for (i=0; i < img->height/2; i++) for (j=0; j < img->width/2; j++) fputc(128, fp); } fclose( fp ); free_mem2D( Y ); } // end of writing map image #undef WRITE_MAP_IMAGE #endif free_mem3D( map, num_spare_pics ); free(buf); }
/*! ************************************************************************ * \brief * Dynamic memory allocation of frame size related global buffers * buffers are defined in global.h, allocated memory must be freed in * void free_global_buffers() * * \par Input: * Input Parameters struct inp_par *inp, Image Parameters struct img_par *img * * \par Output: * Number of allocated bytes *********************************************************************** */ int init_global_buffers(struct inp_par *inp, struct img_par *img) { int memory_size=0; if (global_init_done) { free_global_buffers(inp, img); } if (img->structure != FRAME) { img->height *= 2; // set height to frame (twice of field) for normal variables img->height_cr *= 2; // set height to frame (twice of field) for normal variables } // allocate memory for reference frame in find_snr memory_size += get_mem2D(&imgY_ref, img->height, img->width); memory_size += get_mem3D(&imgUV_ref, 2, img->height_cr, img->width_cr); // allocate memory in structure img #ifndef STATIC_ALLOC if(((img->mb_data) = (Macroblock *) calloc((img->width/MB_BLOCK_SIZE) * (img->height/MB_BLOCK_SIZE),sizeof(Macroblock))) == NULL) no_mem_exit("init_global_buffers: img->mb_data"); if(((img->intra_block) = (int*)calloc((img->width/MB_BLOCK_SIZE) * (img->height/MB_BLOCK_SIZE),sizeof(int))) == NULL) no_mem_exit("init_global_buffers: img->intra_block"); memory_size += get_mem2Dint(&(img->ipredmode),img->width/BLOCK_SIZE , img->height/BLOCK_SIZE); // CAVLC mem memory_size += get_mem3Dint(&(img->nz_coeff), img->FrameSizeInMbs, 4, 6); memory_size += get_mem2Dint(&(img->siblock),img->width/MB_BLOCK_SIZE , img->height/MB_BLOCK_SIZE); #else // ipredmode memory_size += (1920/BLOCK_SIZE) * (1088/BLOCK_SIZE) * sizeof(int); // CAVLC mem // nz_coeff memory_size += (1920/MB_BLOCK_SIZE) * (1088/MB_BLOCK_SIZE) * 4 * 6 * sizeof(int); // siblock memory_size += (176/MB_BLOCK_SIZE) * (144/MB_BLOCK_SIZE) * sizeof(int); #endif memory_size += get_mem2Dint(&(img->field_anchor),img->height/BLOCK_SIZE,img->width/BLOCK_SIZE); memory_size += get_mem3Dint(&(img->wp_weight), 2, MAX_REFERENCE_PICTURES, 3); memory_size += get_mem3Dint(&(img->wp_offset), 2, MAX_REFERENCE_PICTURES, 3); memory_size += get_mem4Dint(&(img->wbp_weight), 2, MAX_REFERENCE_PICTURES, MAX_REFERENCE_PICTURES, 3); if (img->structure != FRAME) { img->height /= 2; // reset height for normal variables img->height_cr /= 2; // reset height for normal variables } global_init_done = 1; return (memory_size); }
/*! ************************************************************************ * \brief * Writes out a storable picture * If the picture is a field, the output buffers the picture and tries * to pair it with the next field. * \param p * Picture to be written * \param p_out * Output file ************************************************************************ */ void write_picture(StorablePicture *p, FILE *p_out, int real_structure) { int i, add; if (real_structure==FRAME) { flush_pending_output(p_out); write_out_picture(p, p_out); return; } if (real_structure==pending_output_state) { flush_pending_output(p_out); write_picture(p, p_out, real_structure); return; } if (pending_output_state == FRAME) { pending_output->size_x = p->size_x; pending_output->size_y = p->size_y; pending_output->size_x_cr = p->size_x_cr; pending_output->size_y_cr = p->size_y_cr; pending_output->frame_mbs_only_flag = p->frame_mbs_only_flag; pending_output->frame_cropping_flag = p->frame_cropping_flag; if (pending_output->frame_cropping_flag) { pending_output->frame_cropping_rect_left_offset = p->frame_cropping_rect_left_offset; pending_output->frame_cropping_rect_right_offset = p->frame_cropping_rect_right_offset; pending_output->frame_cropping_rect_top_offset = p->frame_cropping_rect_top_offset; pending_output->frame_cropping_rect_bottom_offset = p->frame_cropping_rect_bottom_offset; } get_mem2D (&(pending_output->imgY), pending_output->size_y, pending_output->size_x); get_mem3D (&(pending_output->imgUV), 2, pending_output->size_y_cr, pending_output->size_x_cr ); clear_picture(pending_output); // copy first field if (real_structure == TOP_FIELD) { add = 0; } else { add = 1; } for (i=0; i<pending_output->size_y; i+=2) { memcpy(pending_output->imgY[(i+add)], p->imgY[(i+add)], p->size_x); } for (i=0; i<pending_output->size_y_cr; i+=2) { memcpy(pending_output->imgUV[0][(i+add)], p->imgUV[0][(i+add)], p->size_x_cr); memcpy(pending_output->imgUV[1][(i+add)], p->imgUV[1][(i+add)], p->size_x_cr); } pending_output_state = real_structure; } else { if ( (pending_output->size_x!=p->size_x) || (pending_output->size_y!= p->size_y) || (pending_output->frame_mbs_only_flag != p->frame_mbs_only_flag) || (pending_output->frame_cropping_flag != p->frame_cropping_flag) || ( pending_output->frame_cropping_flag && ( (pending_output->frame_cropping_rect_left_offset != p->frame_cropping_rect_left_offset) ||(pending_output->frame_cropping_rect_right_offset != p->frame_cropping_rect_right_offset) ||(pending_output->frame_cropping_rect_top_offset != p->frame_cropping_rect_top_offset) ||(pending_output->frame_cropping_rect_bottom_offset != p->frame_cropping_rect_bottom_offset) ) ) ) { flush_pending_output(p_out); write_picture (p, p_out, real_structure); return; } // copy second field if (real_structure == TOP_FIELD) { add = 0; } else { add = 1; } for (i=0; i<pending_output->size_y; i+=2) { memcpy(pending_output->imgY[(i+add)], p->imgY[(i+add)], p->size_x); } for (i=0; i<pending_output->size_y_cr; i+=2) { memcpy(pending_output->imgUV[0][(i+add)], p->imgUV[0][(i+add)], p->size_x_cr); memcpy(pending_output->imgUV[1][(i+add)], p->imgUV[1][(i+add)], p->size_x_cr); } flush_pending_output(p_out); } }