/******************************************************************** * * * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE. * * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * * * * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 * * by the Xiph.Org Foundation and contributors http://www.xiph.org/ * * * ******************************************************************** function: last mod: $Id$ ********************************************************************/ #include <stdlib.h> #include <string.h> #include "state.h" #if defined(OC_DUMP_IMAGES) # include <stdio.h> # include "png.h" # include "zlib.h" #endif /*The function used to fill in the chroma plane motion vectors for a macro block when 4 different motion vectors are specified in the luma plane. This version is for use with chroma decimated in the X and Y directions (4:2:0). _cbmvs: The chroma block-level motion vectors to fill in. _lbmvs: The luma block-level motion vectors.*/ static void oc_set_chroma_mvs00(oc_mv _cbmvs[4],const oc_mv _lbmvs[4]){ … } /*The function used to fill in the chroma plane motion vectors for a macro block when 4 different motion vectors are specified in the luma plane. This version is for use with chroma decimated in the Y direction. _cbmvs: The chroma block-level motion vectors to fill in. _lbmvs: The luma block-level motion vectors.*/ static void oc_set_chroma_mvs01(oc_mv _cbmvs[4],const oc_mv _lbmvs[4]){ … } /*The function used to fill in the chroma plane motion vectors for a macro block when 4 different motion vectors are specified in the luma plane. This version is for use with chroma decimated in the X direction (4:2:2). _cbmvs: The chroma block-level motion vectors to fill in. _lbmvs: The luma block-level motion vectors.*/ static void oc_set_chroma_mvs10(oc_mv _cbmvs[4],const oc_mv _lbmvs[4]){ … } /*The function used to fill in the chroma plane motion vectors for a macro block when 4 different motion vectors are specified in the luma plane. This version is for use with no chroma decimation (4:4:4). _cbmvs: The chroma block-level motion vectors to fill in. _lmbmv: The luma macro-block level motion vector to fill in for use in prediction. _lbmvs: The luma block-level motion vectors.*/ static void oc_set_chroma_mvs11(oc_mv _cbmvs[4],const oc_mv _lbmvs[4]){ … } /*A table of functions used to fill in the chroma plane motion vectors for a macro block when 4 different motion vectors are specified in the luma plane.*/ const oc_set_chroma_mvs_func OC_SET_CHROMA_MVS_TABLE[TH_PF_NFORMATS]= …; /*Returns the fragment index of the top-left block in a macro block. This can be used to test whether or not the whole macro block is valid. _sb_map: The super block map. _quadi: The quadrant number. Return: The index of the fragment of the upper left block in the macro block, or -1 if the block lies outside the coded frame.*/ static ptrdiff_t oc_sb_quad_top_left_frag(oc_sb_map_quad _sb_map[4],int _quadi){ … } /*Fills in the mapping from block positions to fragment numbers for a single color plane. This function also fills in the "valid" flag of each quadrant in the super block flags. _sb_maps: The array of super block maps for the color plane. _sb_flags: The array of super block flags for the color plane. _frag0: The index of the first fragment in the plane. _hfrags: The number of horizontal fragments in a coded frame. _vfrags: The number of vertical fragments in a coded frame.*/ static void oc_sb_create_plane_mapping(oc_sb_map _sb_maps[], oc_sb_flags _sb_flags[],ptrdiff_t _frag0,int _hfrags,int _vfrags){ … } /*Fills in the Y plane fragment map for a macro block given the fragment coordinates of its upper-left hand corner. _mb_map: The macro block map to fill. _fplane: The description of the Y plane. _xfrag0: The X location of the upper-left hand fragment in the luma plane. _yfrag0: The Y location of the upper-left hand fragment in the luma plane.*/ static void oc_mb_fill_ymapping(oc_mb_map_plane _mb_map[3], const oc_fragment_plane *_fplane,int _xfrag0,int _yfrag0){ … } /*Fills in the chroma plane fragment maps for a macro block. This version is for use with chroma decimated in the X and Y directions (4:2:0). _mb_map: The macro block map to fill. _fplanes: The descriptions of the fragment planes. _xfrag0: The X location of the upper-left hand fragment in the luma plane. _yfrag0: The Y location of the upper-left hand fragment in the luma plane.*/ static void oc_mb_fill_cmapping00(oc_mb_map_plane _mb_map[3], const oc_fragment_plane _fplanes[3],int _xfrag0,int _yfrag0){ … } /*Fills in the chroma plane fragment maps for a macro block. This version is for use with chroma decimated in the Y direction. _mb_map: The macro block map to fill. _fplanes: The descriptions of the fragment planes. _xfrag0: The X location of the upper-left hand fragment in the luma plane. _yfrag0: The Y location of the upper-left hand fragment in the luma plane.*/ static void oc_mb_fill_cmapping01(oc_mb_map_plane _mb_map[3], const oc_fragment_plane _fplanes[3],int _xfrag0,int _yfrag0){ … } /*Fills in the chroma plane fragment maps for a macro block. This version is for use with chroma decimated in the X direction (4:2:2). _mb_map: The macro block map to fill. _fplanes: The descriptions of the fragment planes. _xfrag0: The X location of the upper-left hand fragment in the luma plane. _yfrag0: The Y location of the upper-left hand fragment in the luma plane.*/ static void oc_mb_fill_cmapping10(oc_mb_map_plane _mb_map[3], const oc_fragment_plane _fplanes[3],int _xfrag0,int _yfrag0){ … } /*Fills in the chroma plane fragment maps for a macro block. This version is for use with no chroma decimation (4:4:4). This uses the already filled-in luma plane values. _mb_map: The macro block map to fill. _fplanes: The descriptions of the fragment planes. _xfrag0: The X location of the upper-left hand fragment in the luma plane. _yfrag0: The Y location of the upper-left hand fragment in the luma plane.*/ static void oc_mb_fill_cmapping11(oc_mb_map_plane _mb_map[3], const oc_fragment_plane _fplanes[3],int _xfrag0,int _yfrag0){ … } /*The function type used to fill in the chroma plane fragment maps for a macro block. _mb_map: The macro block map to fill. _fplanes: The descriptions of the fragment planes. _xfrag0: The X location of the upper-left hand fragment in the luma plane. _yfrag0: The Y location of the upper-left hand fragment in the luma plane.*/ oc_mb_fill_cmapping_func; /*A table of functions used to fill in the chroma plane fragment maps for a macro block for each type of chrominance decimation.*/ static const oc_mb_fill_cmapping_func OC_MB_FILL_CMAPPING_TABLE[4]= …; /*Fills in the mapping from macro blocks to their corresponding fragment numbers in each plane. _mb_maps: The list of macro block maps. _mb_modes: The list of macro block modes; macro blocks completely outside the coded region are marked invalid. _fplanes: The descriptions of the fragment planes. _pixel_fmt: The chroma decimation type.*/ static void oc_mb_create_mapping(oc_mb_map _mb_maps[], signed char _mb_modes[],const oc_fragment_plane _fplanes[3],int _pixel_fmt){ … } /*Marks the fragments which fall all or partially outside the displayable region of the frame. _state: The Theora state containing the fragments to be marked.*/ static void oc_state_border_init(oc_theora_state *_state){ … } static int oc_state_frarray_init(oc_theora_state *_state){ … } static void oc_state_frarray_clear(oc_theora_state *_state){ … } /*Initializes the buffers used for reconstructed frames. These buffers are padded with 16 extra pixels on each side, to allow unrestricted motion vectors without special casing the boundary. If chroma is decimated in either direction, the padding is reduced by a factor of 2 on the appropriate sides. _nrefs: The number of reference buffers to init; must be in the range 3...6.*/ static int oc_state_ref_bufs_init(oc_theora_state *_state,int _nrefs){ … } static void oc_state_ref_bufs_clear(oc_theora_state *_state){ … } void oc_state_accel_init_c(oc_theora_state *_state){ … } int oc_state_init(oc_theora_state *_state,const th_info *_info,int _nrefs){ … } void oc_state_clear(oc_theora_state *_state){ … } /*Duplicates the pixels on the border of the image plane out into the surrounding padding for use by unrestricted motion vectors. This function only adds the left and right borders, and only for the fragment rows specified. _refi: The index of the reference buffer to pad. _pli: The color plane. _y0: The Y coordinate of the first row to pad. _yend: The Y coordinate of the row to stop padding at.*/ void oc_state_borders_fill_rows(oc_theora_state *_state,int _refi,int _pli, int _y0,int _yend){ … } /*Duplicates the pixels on the border of the image plane out into the surrounding padding for use by unrestricted motion vectors. This function only adds the top and bottom borders, and must be called after the left and right borders are added. _refi: The index of the reference buffer to pad. _pli: The color plane.*/ void oc_state_borders_fill_caps(oc_theora_state *_state,int _refi,int _pli){ … } /*Duplicates the pixels on the border of the given reference image out into the surrounding padding for use by unrestricted motion vectors. _state: The context containing the reference buffers. _refi: The index of the reference buffer to pad.*/ void oc_state_borders_fill(oc_theora_state *_state,int _refi){ … } /*Determines the offsets in an image buffer to use for motion compensation. _state: The Theora state the offsets are to be computed with. _offsets: Returns the offset for the buffer(s). _offsets[0] is always set. _offsets[1] is set if the motion vector has non-zero fractional components. _pli: The color plane index. _mv: The motion vector. Return: The number of offsets returned: 1 or 2.*/ int oc_state_get_mv_offsets(const oc_theora_state *_state,int _offsets[2], int _pli,oc_mv _mv){ … } void oc_state_frag_recon_c(const oc_theora_state *_state,ptrdiff_t _fragi, int _pli,ogg_int16_t _dct_coeffs[128],int _last_zzi,ogg_uint16_t _dc_quant){ … } static void loop_filter_h(unsigned char *_pix,int _ystride,signed char *_bv){ … } static void loop_filter_v(unsigned char *_pix,int _ystride,signed char *_bv){ … } /*Initialize the bounding values array used by the loop filter. _bv: Storage for the array. _flimit: The filter limit as defined in Section 7.10 of the spec.*/ void oc_loop_filter_init_c(signed char _bv[256],int _flimit){ … } /*Apply the loop filter to a given set of fragment rows in the given plane. The filter may be run on the bottom edge, affecting pixels in the next row of fragments, so this row also needs to be available. _bv: The bounding values array. _refi: The index of the frame buffer to filter. _pli: The color plane to filter. _fragy0: The Y coordinate of the first fragment row to filter. _fragy_end: The Y coordinate of the fragment row to stop filtering at.*/ void oc_state_loop_filter_frag_rows_c(const oc_theora_state *_state, signed char *_bv,int _refi,int _pli,int _fragy0,int _fragy_end){ … } #if defined(OC_DUMP_IMAGES) int oc_state_dump_frame(const oc_theora_state *_state,int _frame, const char *_suf){ /*Dump a PNG of the reconstructed image.*/ png_structp png; png_infop info; png_bytep *image; FILE *fp; char fname[16]; unsigned char *y_row; unsigned char *u_row; unsigned char *v_row; unsigned char *y; unsigned char *u; unsigned char *v; ogg_int64_t iframe; ogg_int64_t pframe; int y_stride; int u_stride; int v_stride; int framei; int width; int height; int imgi; int imgj; width=_state->info.frame_width; height=_state->info.frame_height; iframe=_state->granpos>>_state->info.keyframe_granule_shift; pframe=_state->granpos-(iframe<<_state->info.keyframe_granule_shift); sprintf(fname,"%08i%s.png",(int)(iframe+pframe),_suf); fp=fopen(fname,"wb"); if(fp==NULL)return TH_EFAULT; image=(png_bytep *)oc_malloc_2d(height,6*width,sizeof(**image)); if(image==NULL){ fclose(fp); return TH_EFAULT; } png=png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL); if(png==NULL){ oc_free_2d(image); fclose(fp); return TH_EFAULT; } info=png_create_info_struct(png); if(info==NULL){ png_destroy_write_struct(&png,NULL); oc_free_2d(image); fclose(fp); return TH_EFAULT; } if(setjmp(png_jmpbuf(png))){ png_destroy_write_struct(&png,&info); oc_free_2d(image); fclose(fp); return TH_EFAULT; } framei=_state->ref_frame_idx[_frame]; y_row=_state->ref_frame_bufs[framei][0].data; u_row=_state->ref_frame_bufs[framei][1].data; v_row=_state->ref_frame_bufs[framei][2].data; y_stride=_state->ref_frame_bufs[framei][0].stride; u_stride=_state->ref_frame_bufs[framei][1].stride; v_stride=_state->ref_frame_bufs[framei][2].stride; /*Chroma up-sampling is just done with a box filter. This is very likely what will actually be used in practice on a real display, and also removes one more layer to search in for the source of artifacts. As an added bonus, it's dead simple.*/ for(imgi=height;imgi-->0;){ int dc; y=y_row; u=u_row; v=v_row; for(imgj=0;imgj<6*width;){ float yval; float uval; float vval; unsigned rval; unsigned gval; unsigned bval; /*This is intentionally slow and very accurate.*/ yval=(*y-16)*(1.0F/219); uval=(*u-128)*(2*(1-0.114F)/224); vval=(*v-128)*(2*(1-0.299F)/224); rval=OC_CLAMPI(0,(int)(65535*(yval+vval)+0.5F),65535); gval=OC_CLAMPI(0,(int)(65535*( yval-uval*(0.114F/0.587F)-vval*(0.299F/0.587F))+0.5F),65535); bval=OC_CLAMPI(0,(int)(65535*(yval+uval)+0.5F),65535); image[imgi][imgj++]=(unsigned char)(rval>>8); image[imgi][imgj++]=(unsigned char)(rval&0xFF); image[imgi][imgj++]=(unsigned char)(gval>>8); image[imgi][imgj++]=(unsigned char)(gval&0xFF); image[imgi][imgj++]=(unsigned char)(bval>>8); image[imgi][imgj++]=(unsigned char)(bval&0xFF); dc=(y-y_row&1)|(_state->info.pixel_fmt&1); y++; u+=dc; v+=dc; } dc=-((height-1-imgi&1)|_state->info.pixel_fmt>>1); y_row+=y_stride; u_row+=dc&u_stride; v_row+=dc&v_stride; } png_init_io(png,fp); png_set_compression_level(png,Z_BEST_COMPRESSION); png_set_IHDR(png,info,width,height,16,PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,PNG_COMPRESSION_TYPE_DEFAULT,PNG_FILTER_TYPE_DEFAULT); switch(_state->info.colorspace){ case TH_CS_ITU_REC_470M:{ png_set_gAMA(png,info,2.2); png_set_cHRM_fixed(png,info,31006,31616, 67000,32000,21000,71000,14000,8000); }break; case TH_CS_ITU_REC_470BG:{ png_set_gAMA(png,info,2.67); png_set_cHRM_fixed(png,info,31271,32902, 64000,33000,29000,60000,15000,6000); }break; default:break; } png_set_pHYs(png,info,_state->info.aspect_numerator, _state->info.aspect_denominator,0); png_set_rows(png,info,image); png_write_png(png,info,PNG_TRANSFORM_IDENTITY,NULL); png_write_end(png,info); png_destroy_write_struct(&png,&info); oc_free_2d(image); fclose(fp); return 0; } #endif ogg_int64_t th_granule_frame(void *_encdec,ogg_int64_t _granpos){ … } double th_granule_time(void *_encdec,ogg_int64_t _granpos){ … }