/********************************************************************1* *2* THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE. *3* USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *4* GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *5* IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *6* *7* THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009,2025 *8* by the Xiph.Org Foundation and contributors *9* https://www.xiph.org/ *10* *11********************************************************************1213function:1415********************************************************************/16#if !defined(_state_H)17# define _state_H (1)18# include "internal.h"19# include "huffman.h"20# include "quant.h"21222324/*A single quadrant of the map from a super block to fragment numbers.*/25typedef ptrdiff_t oc_sb_map_quad[4];26/*A map from a super block to fragment numbers.*/27typedef oc_sb_map_quad oc_sb_map[4];28/*A single plane of the map from a macro block to fragment numbers.*/29typedef ptrdiff_t oc_mb_map_plane[4];30/*A map from a macro block to fragment numbers.*/31typedef oc_mb_map_plane oc_mb_map[3];32/*A motion vector.*/33typedef ogg_int16_t oc_mv;3435typedef struct oc_sb_flags oc_sb_flags;36typedef struct oc_border_info oc_border_info;37typedef struct oc_fragment oc_fragment;38typedef struct oc_fragment_plane oc_fragment_plane;39typedef struct oc_base_opt_vtable oc_base_opt_vtable;40typedef struct oc_base_opt_data oc_base_opt_data;41typedef struct oc_state_dispatch_vtable oc_state_dispatch_vtable;42typedef struct oc_theora_state oc_theora_state;43444546/*Shared accelerated functions.*/47# if defined(OC_X86_ASM)48# if defined(_MSC_VER)49# include "x86_vc/x86int.h"50# else51# include "x86/x86int.h"52# endif53# endif54# if defined(OC_ARM_ASM)55# include "arm/armint.h"56# endif57# if defined(OC_C64X_ASM)58# include "c64x/c64xint.h"59# endif6061# if !defined(oc_state_accel_init)62# define oc_state_accel_init oc_state_accel_init_c63# endif64# if defined(OC_STATE_USE_VTABLE)65# if !defined(oc_frag_copy)66# define oc_frag_copy(_state,_dst,_src,_ystride) \67((*(_state)->opt_vtable.frag_copy)(_dst,_src,_ystride))68# endif69# if !defined(oc_frag_copy_list)70# define oc_frag_copy_list(_state,_dst_frame,_src_frame,_ystride, \71_fragis,_nfragis,_frag_buf_offs) \72((*(_state)->opt_vtable.frag_copy_list)(_dst_frame,_src_frame,_ystride, \73_fragis,_nfragis,_frag_buf_offs))74# endif75# if !defined(oc_frag_recon_intra)76# define oc_frag_recon_intra(_state,_dst,_dst_ystride,_residue) \77((*(_state)->opt_vtable.frag_recon_intra)(_dst,_dst_ystride,_residue))78# endif79# if !defined(oc_frag_recon_inter)80# define oc_frag_recon_inter(_state,_dst,_src,_ystride,_residue) \81((*(_state)->opt_vtable.frag_recon_inter)(_dst,_src,_ystride,_residue))82# endif83# if !defined(oc_frag_recon_inter2)84# define oc_frag_recon_inter2(_state,_dst,_src1,_src2,_ystride,_residue) \85((*(_state)->opt_vtable.frag_recon_inter2)(_dst, \86_src1,_src2,_ystride,_residue))87# endif88# if !defined(oc_idct8x8)89# define oc_idct8x8(_state,_y,_x,_last_zzi) \90((*(_state)->opt_vtable.idct8x8)(_y,_x,_last_zzi))91# endif92# if !defined(oc_state_frag_recon)93# define oc_state_frag_recon(_state,_fragi, \94_pli,_dct_coeffs,_last_zzi,_dc_quant) \95((*(_state)->opt_vtable.state_frag_recon)(_state,_fragi, \96_pli,_dct_coeffs,_last_zzi,_dc_quant))97# endif98# if !defined(oc_loop_filter_init)99# define oc_loop_filter_init(_state,_bv,_flimit) \100((*(_state)->opt_vtable.loop_filter_init)(_bv,_flimit))101# endif102# if !defined(oc_state_loop_filter_frag_rows)103# define oc_state_loop_filter_frag_rows(_state, \104_bv,_refi,_pli,_fragy0,_fragy_end) \105((*(_state)->opt_vtable.state_loop_filter_frag_rows)(_state, \106_bv,_refi,_pli,_fragy0,_fragy_end))107# endif108# if !defined(oc_restore_fpu)109# define oc_restore_fpu(_state) \110((*(_state)->opt_vtable.restore_fpu)())111# endif112# else113# if !defined(oc_frag_copy)114# define oc_frag_copy(_state,_dst,_src,_ystride) \115oc_frag_copy_c(_dst,_src,_ystride)116# endif117# if !defined(oc_frag_copy_list)118# define oc_frag_copy_list(_state,_dst_frame,_src_frame,_ystride, \119_fragis,_nfragis,_frag_buf_offs) \120oc_frag_copy_list_c(_dst_frame,_src_frame,_ystride, \121_fragis,_nfragis,_frag_buf_offs)122# endif123# if !defined(oc_frag_recon_intra)124# define oc_frag_recon_intra(_state,_dst,_dst_ystride,_residue) \125oc_frag_recon_intra_c(_dst,_dst_ystride,_residue)126# endif127# if !defined(oc_frag_recon_inter)128# define oc_frag_recon_inter(_state,_dst,_src,_ystride,_residue) \129oc_frag_recon_inter_c(_dst,_src,_ystride,_residue)130# endif131# if !defined(oc_frag_recon_inter2)132# define oc_frag_recon_inter2(_state,_dst,_src1,_src2,_ystride,_residue) \133oc_frag_recon_inter2_c(_dst,_src1,_src2,_ystride,_residue)134# endif135# if !defined(oc_idct8x8)136# define oc_idct8x8(_state,_y,_x,_last_zzi) oc_idct8x8_c(_y,_x,_last_zzi)137# endif138# if !defined(oc_state_frag_recon)139# define oc_state_frag_recon oc_state_frag_recon_c140# endif141# if !defined(oc_loop_filter_init)142# define oc_loop_filter_init(_state,_bv,_flimit) \143oc_loop_filter_init_c(_bv,_flimit)144# endif145# if !defined(oc_state_loop_filter_frag_rows)146# define oc_state_loop_filter_frag_rows oc_state_loop_filter_frag_rows_c147# endif148# if !defined(oc_restore_fpu)149# define oc_restore_fpu(_state) do{}while(0)150# endif151# endif152153154155/*A keyframe.*/156# define OC_INTRA_FRAME (0)157/*A predicted frame.*/158# define OC_INTER_FRAME (1)159/*A frame of unknown type (frame type decision has not yet been made).*/160# define OC_UNKWN_FRAME (-1)161162/*The amount of padding to add to the reconstructed frame buffers on all163sides.164This is used to allow unrestricted motion vectors without special casing.165This must be a multiple of 2.*/166# define OC_UMV_PADDING (16)167168/*Frame classification indices.*/169/*The previous golden frame.*/170# define OC_FRAME_GOLD (0)171/*The previous frame.*/172# define OC_FRAME_PREV (1)173/*The current frame.*/174# define OC_FRAME_SELF (2)175/*Used to mark uncoded fragments (for DC prediction).*/176# define OC_FRAME_NONE (3)177178/*The input or output buffer.*/179# define OC_FRAME_IO (3)180/*Uncompressed prev golden frame.*/181# define OC_FRAME_GOLD_ORIG (4)182/*Uncompressed previous frame. */183# define OC_FRAME_PREV_ORIG (5)184185/*Macroblock modes.*/186/*Macro block is invalid: It is never coded.*/187# define OC_MODE_INVALID (-1)188/*Encoded difference from the same macro block in the previous frame.*/189# define OC_MODE_INTER_NOMV (0)190/*Encoded with no motion compensated prediction.*/191# define OC_MODE_INTRA (1)192/*Encoded difference from the previous frame offset by the given motion193vector.*/194# define OC_MODE_INTER_MV (2)195/*Encoded difference from the previous frame offset by the last coded motion196vector.*/197# define OC_MODE_INTER_MV_LAST (3)198/*Encoded difference from the previous frame offset by the second to last199coded motion vector.*/200# define OC_MODE_INTER_MV_LAST2 (4)201/*Encoded difference from the same macro block in the previous golden202frame.*/203# define OC_MODE_GOLDEN_NOMV (5)204/*Encoded difference from the previous golden frame offset by the given motion205vector.*/206# define OC_MODE_GOLDEN_MV (6)207/*Encoded difference from the previous frame offset by the individual motion208vectors given for each block.*/209# define OC_MODE_INTER_MV_FOUR (7)210/*The number of (coded) modes.*/211# define OC_NMODES (8)212213/*Determines the reference frame used for a given MB mode.*/214# define OC_FRAME_FOR_MODE(_x) \215OC_UNIBBLE_TABLE32(OC_FRAME_PREV,OC_FRAME_SELF,OC_FRAME_PREV,OC_FRAME_PREV, \216OC_FRAME_PREV,OC_FRAME_GOLD,OC_FRAME_GOLD,OC_FRAME_PREV,(_x))217218/*Constants for the packet state machine common between encoder and decoder.*/219220/*Next packet to emit/read: Codec info header.*/221# define OC_PACKET_INFO_HDR (-3)222/*Next packet to emit/read: Comment header.*/223# define OC_PACKET_COMMENT_HDR (-2)224/*Next packet to emit/read: Codec setup header.*/225# define OC_PACKET_SETUP_HDR (-1)226/*No more packets to emit/read.*/227# define OC_PACKET_DONE (INT_MAX)228229230231#define OC_MV(_x,_y) ((oc_mv)((_x)&0xFF|(_y)*256))232#define OC_MV_X(_mv) ((signed char)(_mv))233#define OC_MV_Y(_mv) ((_mv)>>8)234#define OC_MV_ADD(_mv1,_mv2) \235OC_MV(OC_MV_X(_mv1)+OC_MV_X(_mv2), \236OC_MV_Y(_mv1)+OC_MV_Y(_mv2))237#define OC_MV_SUB(_mv1,_mv2) \238OC_MV(OC_MV_X(_mv1)-OC_MV_X(_mv2), \239OC_MV_Y(_mv1)-OC_MV_Y(_mv2))240241242243/*Super blocks are 32x32 segments of pixels in a single color plane indexed244in image order.245Internally, super blocks are broken up into four quadrants, each of which246contains a 2x2 pattern of blocks, each of which is an 8x8 block of pixels.247Quadrants, and the blocks within them, are indexed in a special order called248a "Hilbert curve" within the super block.249250In order to differentiate between the Hilbert-curve indexing strategy and251the regular image order indexing strategy, blocks indexed in image order252are called "fragments".253Fragments are indexed in image order, left to right, then bottom to top,254from Y' plane to Cb plane to Cr plane.255256The co-located fragments in all image planes corresponding to the location257of a single quadrant of a luma plane super block form a macro block.258Thus there is only a single set of macro blocks for all planes, each of which259contains between 6 and 12 fragments, depending on the pixel format.260Therefore macro block information is kept in a separate set of arrays from261super blocks to avoid unused space in the other planes.262The lists are indexed in super block order.263That is, the macro block corresponding to the macro block mbi in (luma plane)264super block sbi is at index (sbi<<2|mbi).265Thus the number of macro blocks in each dimension is always twice the number266of super blocks, even when only an odd number fall inside the coded frame.267These "extra" macro blocks are just an artifact of our internal data layout,268and not part of the coded stream; they are flagged with a negative MB mode.*/269270271272/*Super block information.*/273struct oc_sb_flags{274unsigned char coded_fully:1;275unsigned char coded_partially:1;276unsigned char quad_valid:4;277};278279280281/*Information about a fragment which intersects the border of the displayable282region.283This marks which pixels belong to the displayable region.*/284struct oc_border_info{285/*A bit mask marking which pixels are in the displayable region.286Pixel (x,y) corresponds to bit (y<<3|x).*/287ogg_int64_t mask;288/*The number of pixels in the displayable region.289This is always positive, and always less than 64.*/290int npixels;291};292293294295/*Fragment information.*/296struct oc_fragment{297/*A flag indicating whether or not this fragment is coded.*/298unsigned coded:1;299/*A flag indicating that this entire fragment lies outside the displayable300region of the frame.301Note the contrast with an invalid macro block, which is outside the coded302frame, not just the displayable one.303There are no fragments outside the coded frame by construction.*/304unsigned invalid:1;305/*The index of the quality index used for this fragment's AC coefficients.*/306unsigned qii:4;307/*The index of the reference frame this fragment is predicted from.*/308unsigned refi:2;309/*The mode of the macroblock this fragment belongs to.*/310unsigned mb_mode:3;311/*The index of the associated border information for fragments which lie312partially outside the displayable region.313For fragments completely inside or outside this region, this is -1.314Note that the C standard requires an explicit signed keyword for bitfield315types, since some compilers may treat them as unsigned without it.*/316signed int borderi:5;317/*The prediction-corrected DC component.318Note that the C standard requires an explicit signed keyword for bitfield319types, since some compilers may treat them as unsigned without it.*/320signed int dc:16;321};322323324325/*A description of each fragment plane.*/326struct oc_fragment_plane{327/*The number of fragments in the horizontal direction.*/328int nhfrags;329/*The number of fragments in the vertical direction.*/330int nvfrags;331/*The offset of the first fragment in the plane.*/332ptrdiff_t froffset;333/*The total number of fragments in the plane.*/334ptrdiff_t nfrags;335/*The number of super blocks in the horizontal direction.*/336unsigned nhsbs;337/*The number of super blocks in the vertical direction.*/338unsigned nvsbs;339/*The offset of the first super block in the plane.*/340unsigned sboffset;341/*The total number of super blocks in the plane.*/342unsigned nsbs;343};344345346typedef void (*oc_state_loop_filter_frag_rows_func)(347const oc_theora_state *_state,signed char _bv[256],int _refi,int _pli,348int _fragy0,int _fragy_end);349350/*The shared (encoder and decoder) functions that have accelerated variants.*/351struct oc_base_opt_vtable{352void (*frag_copy)(unsigned char *_dst,353const unsigned char *_src,int _ystride);354void (*frag_copy_list)(unsigned char *_dst_frame,355const unsigned char *_src_frame,int _ystride,356const ptrdiff_t *_fragis,ptrdiff_t _nfragis,const ptrdiff_t *_frag_buf_offs);357void (*frag_recon_intra)(unsigned char *_dst,int _ystride,358const ogg_int16_t _residue[64]);359void (*frag_recon_inter)(unsigned char *_dst,360const unsigned char *_src,int _ystride,const ogg_int16_t _residue[64]);361void (*frag_recon_inter2)(unsigned char *_dst,const unsigned char *_src1,362const unsigned char *_src2,int _ystride,const ogg_int16_t _residue[64]);363void (*idct8x8)(ogg_int16_t _y[64],ogg_int16_t _x[64],int _last_zzi);364void (*state_frag_recon)(const oc_theora_state *_state,ptrdiff_t _fragi,365int _pli,ogg_int16_t _dct_coeffs[128],int _last_zzi,ogg_uint16_t _dc_quant);366void (*loop_filter_init)(signed char _bv[256],int _flimit);367oc_state_loop_filter_frag_rows_func state_loop_filter_frag_rows;368void (*restore_fpu)(void);369};370371/*The shared (encoder and decoder) tables that vary according to which variants372of the above functions are used.*/373struct oc_base_opt_data{374const unsigned char *dct_fzig_zag;375};376377378/*State information common to both the encoder and decoder.*/379struct oc_theora_state{380/*The stream information.*/381th_info info;382# if defined(OC_STATE_USE_VTABLE)383/*Table for shared accelerated functions.*/384oc_base_opt_vtable opt_vtable;385# endif386/*Table for shared data used by accelerated functions.*/387oc_base_opt_data opt_data;388/*CPU flags to detect the presence of extended instruction sets.*/389ogg_uint32_t cpu_flags;390/*The fragment plane descriptions.*/391oc_fragment_plane fplanes[3];392/*The list of fragments, indexed in image order.*/393oc_fragment *frags;394/*The the offset into the reference frame buffer to the upper-left pixel of395each fragment.*/396ptrdiff_t *frag_buf_offs;397/*The motion vector for each fragment.*/398oc_mv *frag_mvs;399/*The total number of fragments in a single frame.*/400ptrdiff_t nfrags;401/*The list of super block maps, indexed in image order.*/402oc_sb_map *sb_maps;403/*The list of super block flags, indexed in image order.*/404oc_sb_flags *sb_flags;405/*The total number of super blocks in a single frame.*/406unsigned nsbs;407/*The fragments from each color plane that belong to each macro block.408Fragments are stored in image order (left to right then top to bottom).409When chroma components are decimated, the extra fragments have an index of410-1.*/411oc_mb_map *mb_maps;412/*The list of macro block modes.413A negative number indicates the macro block lies entirely outside the414coded frame.*/415signed char *mb_modes;416/*The number of macro blocks in the X direction.*/417unsigned nhmbs;418/*The number of macro blocks in the Y direction.*/419unsigned nvmbs;420/*The total number of macro blocks.*/421size_t nmbs;422/*The list of coded fragments, in coded order.423Uncoded fragments are stored in reverse order from the end of the list.*/424ptrdiff_t *coded_fragis;425/*The number of coded fragments in each plane.*/426ptrdiff_t ncoded_fragis[3];427/*The total number of coded fragments.*/428ptrdiff_t ntotal_coded_fragis;429/*The actual buffers used for the reference frames.*/430th_ycbcr_buffer ref_frame_bufs[6];431/*The index of the buffers being used for each OC_FRAME_* reference frame.*/432int ref_frame_idx[6];433/*The storage for the reference frame buffers.434This is just ref_frame_bufs[ref_frame_idx[i]][0].data, but is cached here435for faster look-up.*/436unsigned char *ref_frame_data[6];437/*The handle used to allocate the reference frame buffers.*/438unsigned char *ref_frame_handle;439/*The strides for each plane in the reference frames.*/440int ref_ystride[3];441/*The number of unique border patterns.*/442int nborders;443/*The unique border patterns for all border fragments.444The borderi field of fragments which straddle the border indexes this445list.*/446oc_border_info borders[16];447/*The frame number of the last keyframe.*/448ogg_int64_t keyframe_num;449/*The frame number of the current frame.*/450ogg_int64_t curframe_num;451/*The granpos of the current frame.*/452ogg_int64_t granpos;453/*The type of the current frame.*/454signed char frame_type;455/*The bias to add to the frame count when computing granule positions.*/456unsigned char granpos_bias;457/*The number of quality indices used in the current frame.*/458unsigned char nqis;459/*The quality indices of the current frame.*/460unsigned char qis[3];461/*The dequantization tables, stored in zig-zag order, and indexed by462qi, pli, qti, and zzi.*/463ogg_uint16_t *dequant_tables[64][3][2];464OC_ALIGN16(oc_quant_table dequant_table_data[64][3][2]);465/*Loop filter strength parameters.*/466unsigned char loop_filter_limits[64];467};468469470471/*The function type used to fill in the chroma plane motion vectors for a472macro block when 4 different motion vectors are specified in the luma473plane.474_cbmvs: The chroma block-level motion vectors to fill in.475_lmbmv: The luma macro-block level motion vector to fill in for use in476prediction.477_lbmvs: The luma block-level motion vectors.*/478typedef void (*oc_set_chroma_mvs_func)(oc_mv _cbmvs[4],const oc_mv _lbmvs[4]);479480481482/*A table of functions used to fill in the Cb,Cr plane motion vectors for a483macro block when 4 different motion vectors are specified in the luma484plane.*/485extern const oc_set_chroma_mvs_func OC_SET_CHROMA_MVS_TABLE[TH_PF_NFORMATS];486487488489int oc_state_init(oc_theora_state *_state,const th_info *_info,int _nrefs);490void oc_state_clear(oc_theora_state *_state);491void oc_state_accel_init_c(oc_theora_state *_state);492void oc_state_borders_fill_rows(oc_theora_state *_state,int _refi,int _pli,493int _y0,int _yend);494void oc_state_borders_fill_caps(oc_theora_state *_state,int _refi,int _pli);495void oc_state_borders_fill(oc_theora_state *_state,int _refi);496void oc_state_fill_buffer_ptrs(oc_theora_state *_state,int _buf_idx,497th_ycbcr_buffer _img);498int oc_state_mbi_for_pos(oc_theora_state *_state,int _mbx,int _mby);499int oc_state_get_mv_offsets(const oc_theora_state *_state,int _offsets[2],500int _pli,oc_mv _mv);501502void oc_loop_filter_init_c(signed char _bv[256],int _flimit);503void oc_state_loop_filter(oc_theora_state *_state,int _frame);504# if defined(OC_DUMP_IMAGES)505int oc_state_dump_frame(const oc_theora_state *_state,int _frame,506const char *_suf);507# endif508509/*Default pure-C implementations of shared accelerated functions.*/510void oc_frag_copy_c(unsigned char *_dst,511const unsigned char *_src,int _src_ystride);512void oc_frag_copy_list_c(unsigned char *_dst_frame,513const unsigned char *_src_frame,int _ystride,514const ptrdiff_t *_fragis,ptrdiff_t _nfragis,const ptrdiff_t *_frag_buf_offs);515void oc_frag_recon_intra_c(unsigned char *_dst,int _dst_ystride,516const ogg_int16_t _residue[64]);517void oc_frag_recon_inter_c(unsigned char *_dst,518const unsigned char *_src,int _ystride,const ogg_int16_t _residue[64]);519void oc_frag_recon_inter2_c(unsigned char *_dst,const unsigned char *_src1,520const unsigned char *_src2,int _ystride,const ogg_int16_t _residue[64]);521void oc_idct8x8_c(ogg_int16_t _y[64],ogg_int16_t _x[64],int _last_zzi);522void oc_state_frag_recon_c(const oc_theora_state *_state,ptrdiff_t _fragi,523int _pli,ogg_int16_t _dct_coeffs[128],int _last_zzi,ogg_uint16_t _dc_quant);524void oc_state_loop_filter_frag_rows_c(const oc_theora_state *_state,525signed char _bvarray[256],int _refi,int _pli,int _fragy0,int _fragy_end);526void oc_restore_fpu_c(void);527528/*We need a way to call a few encoder functions without introducing a link-time529dependency into the decoder, while still allowing the old alpha API which530does not distinguish between encoder and decoder objects to be used.531We do this by placing a function table at the start of the encoder object532which can dispatch into the encoder library.533We do a similar thing for the decoder in case we ever decide to split off a534common base library.*/535typedef void (*oc_state_clear_func)(theora_state *_th);536typedef int (*oc_state_control_func)(theora_state *th,int _req,537void *_buf,size_t _buf_sz);538typedef ogg_int64_t (*oc_state_granule_frame_func)(theora_state *_th,539ogg_int64_t _granulepos);540typedef double (*oc_state_granule_time_func)(theora_state *_th,541ogg_int64_t _granulepos);542543544struct oc_state_dispatch_vtable{545oc_state_clear_func clear;546oc_state_control_func control;547oc_state_granule_frame_func granule_frame;548oc_state_granule_time_func granule_time;549};550551#endif552553554