godot/thirdparty/libtheora/decode.c

/********************************************************************
 *                                                                  *
 * 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 <ogg/ogg.h>
#include "decint.h"
#if defined(OC_DUMP_IMAGES)
# include <stdio.h>
# include "png.h"
#endif
#if defined(HAVE_CAIRO)
# include <cairo.h>
#endif


/*No post-processing.*/
#define OC_PP_LEVEL_DISABLED
/*Keep track of DC qi for each block only.*/
#define OC_PP_LEVEL_TRACKDCQI
/*Deblock the luma plane.*/
#define OC_PP_LEVEL_DEBLOCKY
/*Dering the luma plane.*/
#define OC_PP_LEVEL_DERINGY
/*Stronger luma plane deringing.*/
#define OC_PP_LEVEL_SDERINGY
/*Deblock the chroma planes.*/
#define OC_PP_LEVEL_DEBLOCKC
/*Dering the chroma planes.*/
#define OC_PP_LEVEL_DERINGC
/*Stronger chroma plane deringing.*/
#define OC_PP_LEVEL_SDERINGC
/*Maximum valid post-processing level.*/
#define OC_PP_LEVEL_MAX



/*The mode alphabets for the various mode coding schemes.
  Scheme 0 uses a custom alphabet, which is not stored in this table.*/
static const unsigned char OC_MODE_ALPHABETS[7][OC_NMODES]=;


/*The original DCT tokens are extended and reordered during the construction of
   the Huffman tables.
  The extension means more bits can be read with fewer calls to the bitpacker
   during the Huffman decoding process (at the cost of larger Huffman tables),
   and fewer tokens require additional extra bits (reducing the average storage
   per decoded token).
  The revised ordering reveals essential information in the token value
   itself; specifically, whether or not there are additional extra bits to read
   and the parameter to which those extra bits are applied.
  The token is used to fetch a code word from the OC_DCT_CODE_WORD table below.
  The extra bits are added into code word at the bit position inferred from the
   token value, giving the final code word from which all required parameters
   are derived.
  The number of EOBs and the leading zero run length can be extracted directly.
  The coefficient magnitude is optionally negated before extraction, according
   to a 'flip' bit.*/

/*The number of additional extra bits that are decoded with each of the
   internal DCT tokens.*/
static const unsigned char OC_INTERNAL_DCT_TOKEN_EXTRA_BITS[15]=;

/*Whether or not an internal token needs any additional extra bits.*/
#define OC_DCT_TOKEN_NEEDS_MORE(token)

/*This token (OC_DCT_REPEAT_RUN3_TOKEN) requires more than 8 extra bits.*/
#define OC_DCT_TOKEN_FAT_EOB

/*The number of EOBs to use for an end-of-frame token.
  Note: We want to set eobs to PTRDIFF_MAX here, but that requires C99, which
   is not yet available everywhere; this should be equivalent.*/
#define OC_DCT_EOB_FINISH

/*The location of the (6) run length bits in the code word.
  These are placed at index 0 and given 8 bits (even though 6 would suffice)
   because it may be faster to extract the lower byte on some platforms.*/
#define OC_DCT_CW_RLEN_SHIFT
/*The location of the (12) EOB bits in the code word.*/
#define OC_DCT_CW_EOB_SHIFT
/*The location of the (1) flip bit in the code word.
  This must be right under the magnitude bits.*/
#define OC_DCT_CW_FLIP_BIT
/*The location of the (11) token magnitude bits in the code word.
  These must be last, and rely on a sign-extending right shift.*/
#define OC_DCT_CW_MAG_SHIFT

/*Pack the given fields into a code word.*/
#define OC_DCT_CW_PACK(_eobs,_rlen,_mag,_flip)

/*A special code word value that signals the end of the frame (a long EOB run
   of zero).*/
#define OC_DCT_CW_FINISH

/*The position at which to insert the extra bits in the code word.
  We use this formulation because Intel has no useful cmov.
  A real architecture would probably do better with two of those.
  This translates to 11 instructions(!), and is _still_ faster than either a
   table lookup (just barely) or the naive double-ternary implementation (which
   gcc translates to a jump and a cmov).
  This assumes OC_DCT_CW_RLEN_SHIFT is zero, but could easily be reworked if
   you want to make one of the other shifts zero.*/
#define OC_DCT_TOKEN_EB_POS(_token)

/*The code words for each internal token.
  See the notes at OC_DCT_TOKEN_MAP for the reasons why things are out of
   order.*/
static const ogg_int32_t OC_DCT_CODE_WORD[92]=;



static int oc_sb_run_unpack(oc_pack_buf *_opb){}

static int oc_block_run_unpack(oc_pack_buf *_opb){}



void oc_dec_accel_init_c(oc_dec_ctx *_dec){}

static int oc_dec_init(oc_dec_ctx *_dec,const th_info *_info,
 const th_setup_info *_setup){}

static void oc_dec_clear(oc_dec_ctx *_dec){}


static int oc_dec_frame_header_unpack(oc_dec_ctx *_dec){}

/*Mark all fragments as coded and in OC_MODE_INTRA.
  This also builds up the coded fragment list (in coded order), and clears the
   uncoded fragment list.
  It does not update the coded macro block list nor the super block flags, as
   those are not used when decoding INTRA frames.*/
static void oc_dec_mark_all_intra(oc_dec_ctx *_dec){}

/*Decodes the bit flags indicating whether each super block is partially coded
   or not.
  Return: The number of partially coded super blocks.*/
static unsigned oc_dec_partial_sb_flags_unpack(oc_dec_ctx *_dec){}

/*Decodes the bit flags for whether or not each non-partially-coded super
   block is fully coded or not.
  This function should only be called if there is at least one
   non-partially-coded super block.
  Return: The number of partially coded super blocks.*/
static void oc_dec_coded_sb_flags_unpack(oc_dec_ctx *_dec){}

static void oc_dec_coded_flags_unpack(oc_dec_ctx *_dec){}


/*Coding scheme:
   Codeword            Mode Index
   0                       0
   10                      1
   110                     2
   1110                    3
   11110                   4
   111110                  5
   1111110                 6
   1111111                 7*/
static const ogg_int16_t OC_VLC_MODE_TREE[26]=;

static const ogg_int16_t OC_CLC_MODE_TREE[9]=;

/*Unpacks the list of macro block modes for INTER frames.*/
static void oc_dec_mb_modes_unpack(oc_dec_ctx *_dec){}



static const ogg_int16_t OC_VLC_MV_COMP_TREE[101]=;

static const ogg_int16_t OC_CLC_MV_COMP_TREE[65]=;


static oc_mv oc_mv_unpack(oc_pack_buf *_opb,const ogg_int16_t *_tree){}

/*Unpacks the list of motion vectors for INTER frames, and propagtes the macro
   block modes and motion vectors to the individual fragments.*/
static void oc_dec_mv_unpack_and_frag_modes_fill(oc_dec_ctx *_dec){}

static void oc_dec_block_qis_unpack(oc_dec_ctx *_dec){}



/*Unpacks the DC coefficient tokens.
  Unlike when unpacking the AC coefficient tokens, we actually need to decode
   the DC coefficient values now so that we can do DC prediction.
  _huff_idx:   The index of the Huffman table to use for each color plane.
  _ntoks_left: The number of tokens left to be decoded in each color plane for
                each coefficient.
               This is updated as EOB tokens and zero run tokens are decoded.
  Return: The length of any outstanding EOB run.*/
static ptrdiff_t oc_dec_dc_coeff_unpack(oc_dec_ctx *_dec,int _huff_idxs[2],
 ptrdiff_t _ntoks_left[3][64]){}

/*Unpacks the AC coefficient tokens.
  This can completely discard coefficient values while unpacking, and so is
   somewhat simpler than unpacking the DC coefficient tokens.
  _huff_idx:   The index of the Huffman table to use for each color plane.
  _ntoks_left: The number of tokens left to be decoded in each color plane for
                each coefficient.
               This is updated as EOB tokens and zero run tokens are decoded.
  _eobs:       The length of any outstanding EOB run from previous
                coefficients.
  Return: The length of any outstanding EOB run.*/
static int oc_dec_ac_coeff_unpack(oc_dec_ctx *_dec,int _zzi,int _huff_idxs[2],
 ptrdiff_t _ntoks_left[3][64],ptrdiff_t _eobs){}

/*Tokens describing the DCT coefficients that belong to each fragment are
   stored in the bitstream grouped by coefficient, not by fragment.

  This means that we either decode all the tokens in order, building up a
   separate coefficient list for each fragment as we go, and then go back and
   do the iDCT on each fragment, or we have to create separate lists of tokens
   for each coefficient, so that we can pull the next token required off the
   head of the appropriate list when decoding a specific fragment.

  The former was VP3's choice, and it meant 2*w*h extra storage for all the
   decoded coefficient values.

  We take the second option, which lets us store just one to three bytes per
   token (generally far fewer than the number of coefficients, due to EOB
   tokens and zero runs), and which requires us to only maintain a counter for
   each of the 64 coefficients, instead of a counter for every fragment to
   determine where the next token goes.

  We actually use 3 counters per coefficient, one for each color plane, so we
   can decode all color planes simultaneously.
  This lets color conversion, etc., be done as soon as a full MCU (one or
   two super block rows) is decoded, while the image data is still in cache.*/

static void oc_dec_residual_tokens_unpack(oc_dec_ctx *_dec){}


static int oc_dec_postprocess_init(oc_dec_ctx *_dec){}


/*Initialize the main decoding pipeline.*/
static void oc_dec_pipeline_init(oc_dec_ctx *_dec,
 oc_dec_pipeline_state *_pipe){}

/*Undo the DC prediction in a single plane of an MCU (one or two super block
   rows).
  As a side effect, the number of coded and uncoded fragments in this plane of
   the MCU is also computed.*/
void oc_dec_dc_unpredict_mcu_plane_c(oc_dec_ctx *_dec,
 oc_dec_pipeline_state *_pipe,int _pli){}

/*Reconstructs all coded fragments in a single MCU (one or two super block
   rows).
  This requires that each coded fragment have a proper macro block mode and
   motion vector (if not in INTRA mode), and have its DC value decoded, with
   the DC prediction process reversed, and the number of coded and uncoded
   fragments in this plane of the MCU be counted.
  The token lists for each color plane and coefficient should also be filled
   in, along with initial token offsets, extra bits offsets, and EOB run
   counts.*/
static void oc_dec_frags_recon_mcu_plane(oc_dec_ctx *_dec,
 oc_dec_pipeline_state *_pipe,int _pli){}

/*Filter a horizontal block edge.*/
static void oc_filter_hedge(unsigned char *_dst,int _dst_ystride,
 const unsigned char *_src,int _src_ystride,int _qstep,int _flimit,
 int *_variance0,int *_variance1){}

/*Filter a vertical block edge.*/
static void oc_filter_vedge(unsigned char *_dst,int _dst_ystride,
 int _qstep,int _flimit,int *_variances){}

static void oc_dec_deblock_frag_rows(oc_dec_ctx *_dec,
 th_img_plane *_dst,th_img_plane *_src,int _pli,int _fragy0,
 int _fragy_end){}

static void oc_dering_block(unsigned char *_idata,int _ystride,int _b,
 int _dc_scale,int _sharp_mod,int _strong){}

#define OC_DERING_THRESH1
#define OC_DERING_THRESH2
#define OC_DERING_THRESH3
#define OC_DERING_THRESH4

static void oc_dec_dering_frag_rows(oc_dec_ctx *_dec,th_img_plane *_img,
 int _pli,int _fragy0,int _fragy_end){}



th_dec_ctx *th_decode_alloc(const th_info *_info,const th_setup_info *_setup){}

void th_decode_free(th_dec_ctx *_dec){}

int th_decode_ctl(th_dec_ctx *_dec,int _req,void *_buf,
 size_t _buf_sz){}

/*We're decoding an INTER frame, but have no initialized reference
   buffers (i.e., decoding did not start on a key frame).
  We initialize them to a solid gray here.*/
static void oc_dec_init_dummy_frame(th_dec_ctx *_dec){}

#if defined(HAVE_CAIRO)
static void oc_render_telemetry(th_dec_ctx *_dec,th_ycbcr_buffer _ycbcr,
 int _telemetry){
  /*Stuff the plane into cairo.*/
  cairo_surface_t *cs;
  unsigned char   *data;
  unsigned char   *y_row;
  unsigned char   *u_row;
  unsigned char   *v_row;
  unsigned char   *rgb_row;
  int              cstride;
  int              w;
  int              h;
  int              x;
  int              y;
  int              hdec;
  int              vdec;
  w=_ycbcr[0].width;
  h=_ycbcr[0].height;
  hdec=!(_dec->state.info.pixel_fmt&1);
  vdec=!(_dec->state.info.pixel_fmt&2);
  /*Lazy data buffer init.
    We could try to re-use the post-processing buffer, which would save
     memory, but complicate the allocation logic there.
    I don't think anyone cares about memory usage when using telemetry; it is
     not meant for embedded devices.*/
  if(_dec->telemetry_frame_data==NULL){
    _dec->telemetry_frame_data=_ogg_malloc(
     (w*h+2*(w>>hdec)*(h>>vdec))*sizeof(*_dec->telemetry_frame_data));
    if(_dec->telemetry_frame_data==NULL)return;
  }
  cs=cairo_image_surface_create(CAIRO_FORMAT_RGB24,w,h);
  /*Sadly, no YUV support in Cairo (yet); convert into the RGB buffer.*/
  data=cairo_image_surface_get_data(cs);
  if(data==NULL){
    cairo_surface_destroy(cs);
    return;
  }
  cstride=cairo_image_surface_get_stride(cs);
  y_row=_ycbcr[0].data;
  u_row=_ycbcr[1].data;
  v_row=_ycbcr[2].data;
  rgb_row=data;
  for(y=0;y<h;y++){
    for(x=0;x<w;x++){
      int r;
      int g;
      int b;
      r=(1904000*y_row[x]+2609823*v_row[x>>hdec]-363703744)/1635200;
      g=(3827562*y_row[x]-1287801*u_row[x>>hdec]
       -2672387*v_row[x>>hdec]+447306710)/3287200;
      b=(952000*y_row[x]+1649289*u_row[x>>hdec]-225932192)/817600;
      rgb_row[4*x+0]=OC_CLAMP255(b);
      rgb_row[4*x+1]=OC_CLAMP255(g);
      rgb_row[4*x+2]=OC_CLAMP255(r);
    }
    y_row+=_ycbcr[0].stride;
    u_row+=_ycbcr[1].stride&-((y&1)|!vdec);
    v_row+=_ycbcr[2].stride&-((y&1)|!vdec);
    rgb_row+=cstride;
  }
  /*Draw coded identifier for each macroblock (stored in Hilbert order).*/
  {
    cairo_t           *c;
    const oc_fragment *frags;
    oc_mv             *frag_mvs;
    const signed char *mb_modes;
    oc_mb_map         *mb_maps;
    size_t             nmbs;
    size_t             mbi;
    int                row2;
    int                col2;
    int                qim[3]={0,0,0};
    if(_dec->state.nqis==2){
      int bqi;
      bqi=_dec->state.qis[0];
      if(_dec->state.qis[1]>bqi)qim[1]=1;
      if(_dec->state.qis[1]<bqi)qim[1]=-1;
    }
    if(_dec->state.nqis==3){
      int bqi;
      int cqi;
      int dqi;
      bqi=_dec->state.qis[0];
      cqi=_dec->state.qis[1];
      dqi=_dec->state.qis[2];
      if(cqi>bqi&&dqi>bqi){
        if(dqi>cqi){
          qim[1]=1;
          qim[2]=2;
        }
        else{
          qim[1]=2;
          qim[2]=1;
        }
      }
      else if(cqi<bqi&&dqi<bqi){
        if(dqi<cqi){
          qim[1]=-1;
          qim[2]=-2;
        }
        else{
          qim[1]=-2;
          qim[2]=-1;
        }
      }
      else{
        if(cqi<bqi)qim[1]=-1;
        else qim[1]=1;
        if(dqi<bqi)qim[2]=-1;
        else qim[2]=1;
      }
    }
    c=cairo_create(cs);
    frags=_dec->state.frags;
    frag_mvs=_dec->state.frag_mvs;
    mb_modes=_dec->state.mb_modes;
    mb_maps=_dec->state.mb_maps;
    nmbs=_dec->state.nmbs;
    row2=0;
    col2=0;
    for(mbi=0;mbi<nmbs;mbi++){
      float x;
      float y;
      int   bi;
      y=h-(row2+((col2+1>>1)&1))*16-16;
      x=(col2>>1)*16;
      cairo_set_line_width(c,1.);
      /*Keyframe (all intra) red box.*/
      if(_dec->state.frame_type==OC_INTRA_FRAME){
        if(_dec->telemetry_mbmode&0x02){
          cairo_set_source_rgba(c,1.,0,0,.5);
          cairo_rectangle(c,x+2.5,y+2.5,11,11);
          cairo_stroke_preserve(c);
          cairo_set_source_rgba(c,1.,0,0,.25);
          cairo_fill(c);
        }
      }
      else{
        ptrdiff_t fragi;
        int       frag_mvx;
        int       frag_mvy;
        for(bi=0;bi<4;bi++){
          fragi=mb_maps[mbi][0][bi];
          if(fragi>=0&&frags[fragi].coded){
            frag_mvx=OC_MV_X(frag_mvs[fragi]);
            frag_mvy=OC_MV_Y(frag_mvs[fragi]);
            break;
          }
        }
        if(bi<4){
          switch(mb_modes[mbi]){
            case OC_MODE_INTRA:{
              if(_dec->telemetry_mbmode&0x02){
                cairo_set_source_rgba(c,1.,0,0,.5);
                cairo_rectangle(c,x+2.5,y+2.5,11,11);
                cairo_stroke_preserve(c);
                cairo_set_source_rgba(c,1.,0,0,.25);
                cairo_fill(c);
              }
            }break;
            case OC_MODE_INTER_NOMV:{
              if(_dec->telemetry_mbmode&0x01){
                cairo_set_source_rgba(c,0,0,1.,.5);
                cairo_rectangle(c,x+2.5,y+2.5,11,11);
                cairo_stroke_preserve(c);
                cairo_set_source_rgba(c,0,0,1.,.25);
                cairo_fill(c);
              }
            }break;
            case OC_MODE_INTER_MV:{
              if(_dec->telemetry_mbmode&0x04){
                cairo_rectangle(c,x+2.5,y+2.5,11,11);
                cairo_set_source_rgba(c,0,1.,0,.5);
                cairo_stroke(c);
              }
              if(_dec->telemetry_mv&0x04){
                cairo_move_to(c,x+8+frag_mvx,y+8-frag_mvy);
                cairo_set_source_rgba(c,1.,1.,1.,.9);
                cairo_set_line_width(c,3.);
                cairo_line_to(c,x+8+frag_mvx*.66,y+8-frag_mvy*.66);
                cairo_stroke_preserve(c);
                cairo_set_line_width(c,2.);
                cairo_line_to(c,x+8+frag_mvx*.33,y+8-frag_mvy*.33);
                cairo_stroke_preserve(c);
                cairo_set_line_width(c,1.);
                cairo_line_to(c,x+8,y+8);
                cairo_stroke(c);
              }
            }break;
            case OC_MODE_INTER_MV_LAST:{
              if(_dec->telemetry_mbmode&0x08){
                cairo_rectangle(c,x+2.5,y+2.5,11,11);
                cairo_set_source_rgba(c,0,1.,0,.5);
                cairo_move_to(c,x+13.5,y+2.5);
                cairo_line_to(c,x+2.5,y+8);
                cairo_line_to(c,x+13.5,y+13.5);
                cairo_stroke(c);
              }
              if(_dec->telemetry_mv&0x08){
                cairo_move_to(c,x+8+frag_mvx,y+8-frag_mvy);
                cairo_set_source_rgba(c,1.,1.,1.,.9);
                cairo_set_line_width(c,3.);
                cairo_line_to(c,x+8+frag_mvx*.66,y+8-frag_mvy*.66);
                cairo_stroke_preserve(c);
                cairo_set_line_width(c,2.);
                cairo_line_to(c,x+8+frag_mvx*.33,y+8-frag_mvy*.33);
                cairo_stroke_preserve(c);
                cairo_set_line_width(c,1.);
                cairo_line_to(c,x+8,y+8);
                cairo_stroke(c);
              }
            }break;
            case OC_MODE_INTER_MV_LAST2:{
              if(_dec->telemetry_mbmode&0x10){
                cairo_rectangle(c,x+2.5,y+2.5,11,11);
                cairo_set_source_rgba(c,0,1.,0,.5);
                cairo_move_to(c,x+8,y+2.5);
                cairo_line_to(c,x+2.5,y+8);
                cairo_line_to(c,x+8,y+13.5);
                cairo_move_to(c,x+13.5,y+2.5);
                cairo_line_to(c,x+8,y+8);
                cairo_line_to(c,x+13.5,y+13.5);
                cairo_stroke(c);
              }
              if(_dec->telemetry_mv&0x10){
                cairo_move_to(c,x+8+frag_mvx,y+8-frag_mvy);
                cairo_set_source_rgba(c,1.,1.,1.,.9);
                cairo_set_line_width(c,3.);
                cairo_line_to(c,x+8+frag_mvx*.66,y+8-frag_mvy*.66);
                cairo_stroke_preserve(c);
                cairo_set_line_width(c,2.);
                cairo_line_to(c,x+8+frag_mvx*.33,y+8-frag_mvy*.33);
                cairo_stroke_preserve(c);
                cairo_set_line_width(c,1.);
                cairo_line_to(c,x+8,y+8);
                cairo_stroke(c);
              }
            }break;
            case OC_MODE_GOLDEN_NOMV:{
              if(_dec->telemetry_mbmode&0x20){
                cairo_set_source_rgba(c,1.,1.,0,.5);
                cairo_rectangle(c,x+2.5,y+2.5,11,11);
                cairo_stroke_preserve(c);
                cairo_set_source_rgba(c,1.,1.,0,.25);
                cairo_fill(c);
              }
            }break;
            case OC_MODE_GOLDEN_MV:{
              if(_dec->telemetry_mbmode&0x40){
                cairo_rectangle(c,x+2.5,y+2.5,11,11);
                cairo_set_source_rgba(c,1.,1.,0,.5);
                cairo_stroke(c);
              }
              if(_dec->telemetry_mv&0x40){
                cairo_move_to(c,x+8+frag_mvx,y+8-frag_mvy);
                cairo_set_source_rgba(c,1.,1.,1.,.9);
                cairo_set_line_width(c,3.);
                cairo_line_to(c,x+8+frag_mvx*.66,y+8-frag_mvy*.66);
                cairo_stroke_preserve(c);
                cairo_set_line_width(c,2.);
                cairo_line_to(c,x+8+frag_mvx*.33,y+8-frag_mvy*.33);
                cairo_stroke_preserve(c);
                cairo_set_line_width(c,1.);
                cairo_line_to(c,x+8,y+8);
                cairo_stroke(c);
              }
            }break;
            case OC_MODE_INTER_MV_FOUR:{
              if(_dec->telemetry_mbmode&0x80){
                cairo_rectangle(c,x+2.5,y+2.5,4,4);
                cairo_rectangle(c,x+9.5,y+2.5,4,4);
                cairo_rectangle(c,x+2.5,y+9.5,4,4);
                cairo_rectangle(c,x+9.5,y+9.5,4,4);
                cairo_set_source_rgba(c,0,1.,0,.5);
                cairo_stroke(c);
              }
              /*4mv is odd, coded in raster order.*/
              fragi=mb_maps[mbi][0][0];
              if(frags[fragi].coded&&_dec->telemetry_mv&0x80){
                frag_mvx=OC_MV_X(frag_mvs[fragi]);
                frag_mvx=OC_MV_Y(frag_mvs[fragi]);
                cairo_move_to(c,x+4+frag_mvx,y+12-frag_mvy);
                cairo_set_source_rgba(c,1.,1.,1.,.9);
                cairo_set_line_width(c,3.);
                cairo_line_to(c,x+4+frag_mvx*.66,y+12-frag_mvy*.66);
                cairo_stroke_preserve(c);
                cairo_set_line_width(c,2.);
                cairo_line_to(c,x+4+frag_mvx*.33,y+12-frag_mvy*.33);
                cairo_stroke_preserve(c);
                cairo_set_line_width(c,1.);
                cairo_line_to(c,x+4,y+12);
                cairo_stroke(c);
              }
              fragi=mb_maps[mbi][0][1];
              if(frags[fragi].coded&&_dec->telemetry_mv&0x80){
                frag_mvx=OC_MV_X(frag_mvs[fragi]);
                frag_mvx=OC_MV_Y(frag_mvs[fragi]);
                cairo_move_to(c,x+12+frag_mvx,y+12-frag_mvy);
                cairo_set_source_rgba(c,1.,1.,1.,.9);
                cairo_set_line_width(c,3.);
                cairo_line_to(c,x+12+frag_mvx*.66,y+12-frag_mvy*.66);
                cairo_stroke_preserve(c);
                cairo_set_line_width(c,2.);
                cairo_line_to(c,x+12+frag_mvx*.33,y+12-frag_mvy*.33);
                cairo_stroke_preserve(c);
                cairo_set_line_width(c,1.);
                cairo_line_to(c,x+12,y+12);
                cairo_stroke(c);
              }
              fragi=mb_maps[mbi][0][2];
              if(frags[fragi].coded&&_dec->telemetry_mv&0x80){
                frag_mvx=OC_MV_X(frag_mvs[fragi]);
                frag_mvx=OC_MV_Y(frag_mvs[fragi]);
                cairo_move_to(c,x+4+frag_mvx,y+4-frag_mvy);
                cairo_set_source_rgba(c,1.,1.,1.,.9);
                cairo_set_line_width(c,3.);
                cairo_line_to(c,x+4+frag_mvx*.66,y+4-frag_mvy*.66);
                cairo_stroke_preserve(c);
                cairo_set_line_width(c,2.);
                cairo_line_to(c,x+4+frag_mvx*.33,y+4-frag_mvy*.33);
                cairo_stroke_preserve(c);
                cairo_set_line_width(c,1.);
                cairo_line_to(c,x+4,y+4);
                cairo_stroke(c);
              }
              fragi=mb_maps[mbi][0][3];
              if(frags[fragi].coded&&_dec->telemetry_mv&0x80){
                frag_mvx=OC_MV_X(frag_mvs[fragi]);
                frag_mvx=OC_MV_Y(frag_mvs[fragi]);
                cairo_move_to(c,x+12+frag_mvx,y+4-frag_mvy);
                cairo_set_source_rgba(c,1.,1.,1.,.9);
                cairo_set_line_width(c,3.);
                cairo_line_to(c,x+12+frag_mvx*.66,y+4-frag_mvy*.66);
                cairo_stroke_preserve(c);
                cairo_set_line_width(c,2.);
                cairo_line_to(c,x+12+frag_mvx*.33,y+4-frag_mvy*.33);
                cairo_stroke_preserve(c);
                cairo_set_line_width(c,1.);
                cairo_line_to(c,x+12,y+4);
                cairo_stroke(c);
              }
            }break;
          }
        }
      }
      /*qii illustration.*/
      if(_dec->telemetry_qi&0x2){
        cairo_set_line_cap(c,CAIRO_LINE_CAP_SQUARE);
        for(bi=0;bi<4;bi++){
          ptrdiff_t fragi;
          int       qiv;
          int       xp;
          int       yp;
          xp=x+(bi&1)*8;
          yp=y+8-(bi&2)*4;
          fragi=mb_maps[mbi][0][bi];
          if(fragi>=0&&frags[fragi].coded){
            qiv=qim[frags[fragi].qii];
            cairo_set_line_width(c,3.);
            cairo_set_source_rgba(c,0.,0.,0.,.5);
            switch(qiv){
              /*Double plus:*/
              case 2:{
                if((bi&1)^((bi&2)>>1)){
                  cairo_move_to(c,xp+2.5,yp+1.5);
                  cairo_line_to(c,xp+2.5,yp+3.5);
                  cairo_move_to(c,xp+1.5,yp+2.5);
                  cairo_line_to(c,xp+3.5,yp+2.5);
                  cairo_move_to(c,xp+5.5,yp+4.5);
                  cairo_line_to(c,xp+5.5,yp+6.5);
                  cairo_move_to(c,xp+4.5,yp+5.5);
                  cairo_line_to(c,xp+6.5,yp+5.5);
                  cairo_stroke_preserve(c);
                  cairo_set_source_rgba(c,0.,1.,1.,1.);
                }
                else{
                  cairo_move_to(c,xp+5.5,yp+1.5);
                  cairo_line_to(c,xp+5.5,yp+3.5);
                  cairo_move_to(c,xp+4.5,yp+2.5);
                  cairo_line_to(c,xp+6.5,yp+2.5);
                  cairo_move_to(c,xp+2.5,yp+4.5);
                  cairo_line_to(c,xp+2.5,yp+6.5);
                  cairo_move_to(c,xp+1.5,yp+5.5);
                  cairo_line_to(c,xp+3.5,yp+5.5);
                  cairo_stroke_preserve(c);
                  cairo_set_source_rgba(c,0.,1.,1.,1.);
                }
              }break;
              /*Double minus:*/
              case -2:{
                cairo_move_to(c,xp+2.5,yp+2.5);
                cairo_line_to(c,xp+5.5,yp+2.5);
                cairo_move_to(c,xp+2.5,yp+5.5);
                cairo_line_to(c,xp+5.5,yp+5.5);
                cairo_stroke_preserve(c);
                cairo_set_source_rgba(c,1.,1.,1.,1.);
              }break;
              /*Plus:*/
              case 1:{
                if((bi&2)==0)yp-=2;
                if((bi&1)==0)xp-=2;
                cairo_move_to(c,xp+4.5,yp+2.5);
                cairo_line_to(c,xp+4.5,yp+6.5);
                cairo_move_to(c,xp+2.5,yp+4.5);
                cairo_line_to(c,xp+6.5,yp+4.5);
                cairo_stroke_preserve(c);
                cairo_set_source_rgba(c,.1,1.,.3,1.);
                break;
              }
              /*Fall through.*/
              /*Minus:*/
              case -1:{
                cairo_move_to(c,xp+2.5,yp+4.5);
                cairo_line_to(c,xp+6.5,yp+4.5);
                cairo_stroke_preserve(c);
                cairo_set_source_rgba(c,1.,.3,.1,1.);
              }break;
              default:continue;
            }
            cairo_set_line_width(c,1.);
            cairo_stroke(c);
          }
        }
      }
      col2++;
      if((col2>>1)>=_dec->state.nhmbs){
        col2=0;
        row2+=2;
      }
    }
    /*Bit usage indicator[s]:*/
    if(_dec->telemetry_bits){
      int widths[6];
      int fpsn;
      int fpsd;
      int mult;
      int fullw;
      int padw;
      int i;
      fpsn=_dec->state.info.fps_numerator;
      fpsd=_dec->state.info.fps_denominator;
      mult=(_dec->telemetry_bits>=0xFF?1:_dec->telemetry_bits);
      fullw=250.f*h*fpsd*mult/fpsn;
      padw=w-24;
      /*Header and coded block bits.*/
      if(_dec->telemetry_frame_bytes<0||
       _dec->telemetry_frame_bytes==OC_LOTS_OF_BITS){
        _dec->telemetry_frame_bytes=0;
      }
      if(_dec->telemetry_coding_bytes<0||
       _dec->telemetry_coding_bytes>_dec->telemetry_frame_bytes){
        _dec->telemetry_coding_bytes=0;
      }
      if(_dec->telemetry_mode_bytes<0||
       _dec->telemetry_mode_bytes>_dec->telemetry_frame_bytes){
        _dec->telemetry_mode_bytes=0;
      }
      if(_dec->telemetry_mv_bytes<0||
       _dec->telemetry_mv_bytes>_dec->telemetry_frame_bytes){
        _dec->telemetry_mv_bytes=0;
      }
      if(_dec->telemetry_qi_bytes<0||
       _dec->telemetry_qi_bytes>_dec->telemetry_frame_bytes){
        _dec->telemetry_qi_bytes=0;
      }
      if(_dec->telemetry_dc_bytes<0||
       _dec->telemetry_dc_bytes>_dec->telemetry_frame_bytes){
        _dec->telemetry_dc_bytes=0;
      }
      widths[0]=padw*
       (_dec->telemetry_frame_bytes-_dec->telemetry_coding_bytes)/fullw;
      widths[1]=padw*
       (_dec->telemetry_coding_bytes-_dec->telemetry_mode_bytes)/fullw;
      widths[2]=padw*
       (_dec->telemetry_mode_bytes-_dec->telemetry_mv_bytes)/fullw;
      widths[3]=padw*(_dec->telemetry_mv_bytes-_dec->telemetry_qi_bytes)/fullw;
      widths[4]=padw*(_dec->telemetry_qi_bytes-_dec->telemetry_dc_bytes)/fullw;
      widths[5]=padw*(_dec->telemetry_dc_bytes)/fullw;
      for(i=0;i<6;i++)if(widths[i]>w)widths[i]=w;
      cairo_set_source_rgba(c,.0,.0,.0,.6);
      cairo_rectangle(c,10,h-33,widths[0]+1,5);
      cairo_rectangle(c,10,h-29,widths[1]+1,5);
      cairo_rectangle(c,10,h-25,widths[2]+1,5);
      cairo_rectangle(c,10,h-21,widths[3]+1,5);
      cairo_rectangle(c,10,h-17,widths[4]+1,5);
      cairo_rectangle(c,10,h-13,widths[5]+1,5);
      cairo_fill(c);
      cairo_set_source_rgb(c,1,0,0);
      cairo_rectangle(c,10.5,h-32.5,widths[0],4);
      cairo_fill(c);
      cairo_set_source_rgb(c,0,1,0);
      cairo_rectangle(c,10.5,h-28.5,widths[1],4);
      cairo_fill(c);
      cairo_set_source_rgb(c,0,0,1);
      cairo_rectangle(c,10.5,h-24.5,widths[2],4);
      cairo_fill(c);
      cairo_set_source_rgb(c,.6,.4,.0);
      cairo_rectangle(c,10.5,h-20.5,widths[3],4);
      cairo_fill(c);
      cairo_set_source_rgb(c,.3,.3,.3);
      cairo_rectangle(c,10.5,h-16.5,widths[4],4);
      cairo_fill(c);
      cairo_set_source_rgb(c,.5,.5,.8);
      cairo_rectangle(c,10.5,h-12.5,widths[5],4);
      cairo_fill(c);
    }
    /*Master qi indicator[s]:*/
    if(_dec->telemetry_qi&0x1){
      cairo_text_extents_t extents;
      char                 buffer[10];
      int                  p;
      int                  y;
      p=0;
      y=h-7.5;
      if(_dec->state.qis[0]>=10)buffer[p++]=48+_dec->state.qis[0]/10;
      buffer[p++]=48+_dec->state.qis[0]%10;
      if(_dec->state.nqis>=2){
        buffer[p++]=' ';
        if(_dec->state.qis[1]>=10)buffer[p++]=48+_dec->state.qis[1]/10;
        buffer[p++]=48+_dec->state.qis[1]%10;
      }
      if(_dec->state.nqis==3){
        buffer[p++]=' ';
        if(_dec->state.qis[2]>=10)buffer[p++]=48+_dec->state.qis[2]/10;
        buffer[p++]=48+_dec->state.qis[2]%10;
      }
      buffer[p++]='\0';
      cairo_select_font_face(c,"sans",
       CAIRO_FONT_SLANT_NORMAL,CAIRO_FONT_WEIGHT_BOLD);
      cairo_set_font_size(c,18);
      cairo_text_extents(c,buffer,&extents);
      cairo_set_source_rgb(c,1,1,1);
      cairo_move_to(c,w-extents.x_advance-10,y);
      cairo_show_text(c,buffer);
      cairo_set_source_rgb(c,0,0,0);
      cairo_move_to(c,w-extents.x_advance-10,y);
      cairo_text_path(c,buffer);
      cairo_set_line_width(c,.8);
      cairo_set_line_join(c,CAIRO_LINE_JOIN_ROUND);
      cairo_stroke(c);
    }
    cairo_destroy(c);
  }
  /*Out of the Cairo plane into the telemetry YUV buffer.*/
  _ycbcr[0].data=_dec->telemetry_frame_data;
  _ycbcr[0].stride=_ycbcr[0].width;
  _ycbcr[1].data=_ycbcr[0].data+h*_ycbcr[0].stride;
  _ycbcr[1].stride=_ycbcr[1].width;
  _ycbcr[2].data=_ycbcr[1].data+(h>>vdec)*_ycbcr[1].stride;
  _ycbcr[2].stride=_ycbcr[2].width;
  y_row=_ycbcr[0].data;
  u_row=_ycbcr[1].data;
  v_row=_ycbcr[2].data;
  rgb_row=data;
  /*This is one of the few places it's worth handling chroma on a
     case-by-case basis.*/
  switch(_dec->state.info.pixel_fmt){
    case TH_PF_420:{
      for(y=0;y<h;y+=2){
        unsigned char *y_row2;
        unsigned char *rgb_row2;
        y_row2=y_row+_ycbcr[0].stride;
        rgb_row2=rgb_row+cstride;
        for(x=0;x<w;x+=2){
          int y;
          int u;
          int v;
          y=(65481*rgb_row[4*x+2]+128553*rgb_row[4*x+1]
           +24966*rgb_row[4*x+0]+4207500)/255000;
          y_row[x]=OC_CLAMP255(y);
          y=(65481*rgb_row[4*x+6]+128553*rgb_row[4*x+5]
           +24966*rgb_row[4*x+4]+4207500)/255000;
          y_row[x+1]=OC_CLAMP255(y);
          y=(65481*rgb_row2[4*x+2]+128553*rgb_row2[4*x+1]
           +24966*rgb_row2[4*x+0]+4207500)/255000;
          y_row2[x]=OC_CLAMP255(y);
          y=(65481*rgb_row2[4*x+6]+128553*rgb_row2[4*x+5]
           +24966*rgb_row2[4*x+4]+4207500)/255000;
          y_row2[x+1]=OC_CLAMP255(y);
          u=(-8372*(rgb_row[4*x+2]+rgb_row[4*x+6]
           +rgb_row2[4*x+2]+rgb_row2[4*x+6])
           -16436*(rgb_row[4*x+1]+rgb_row[4*x+5]
           +rgb_row2[4*x+1]+rgb_row2[4*x+5])
           +24808*(rgb_row[4*x+0]+rgb_row[4*x+4]
           +rgb_row2[4*x+0]+rgb_row2[4*x+4])+29032005)/225930;
          v=(39256*(rgb_row[4*x+2]+rgb_row[4*x+6]
           +rgb_row2[4*x+2]+rgb_row2[4*x+6])
           -32872*(rgb_row[4*x+1]+rgb_row[4*x+5]
            +rgb_row2[4*x+1]+rgb_row2[4*x+5])
           -6384*(rgb_row[4*x+0]+rgb_row[4*x+4]
            +rgb_row2[4*x+0]+rgb_row2[4*x+4])+45940035)/357510;
          u_row[x>>1]=OC_CLAMP255(u);
          v_row[x>>1]=OC_CLAMP255(v);
        }
        y_row+=_ycbcr[0].stride<<1;
        u_row+=_ycbcr[1].stride;
        v_row+=_ycbcr[2].stride;
        rgb_row+=cstride<<1;
      }
    }break;
    case TH_PF_422:{
      for(y=0;y<h;y++){
        for(x=0;x<w;x+=2){
          int y;
          int u;
          int v;
          y=(65481*rgb_row[4*x+2]+128553*rgb_row[4*x+1]
           +24966*rgb_row[4*x+0]+4207500)/255000;
          y_row[x]=OC_CLAMP255(y);
          y=(65481*rgb_row[4*x+6]+128553*rgb_row[4*x+5]
           +24966*rgb_row[4*x+4]+4207500)/255000;
          y_row[x+1]=OC_CLAMP255(y);
          u=(-16744*(rgb_row[4*x+2]+rgb_row[4*x+6])
           -32872*(rgb_row[4*x+1]+rgb_row[4*x+5])
           +49616*(rgb_row[4*x+0]+rgb_row[4*x+4])+29032005)/225930;
          v=(78512*(rgb_row[4*x+2]+rgb_row[4*x+6])
           -65744*(rgb_row[4*x+1]+rgb_row[4*x+5])
           -12768*(rgb_row[4*x+0]+rgb_row[4*x+4])+45940035)/357510;
          u_row[x>>1]=OC_CLAMP255(u);
          v_row[x>>1]=OC_CLAMP255(v);
        }
        y_row+=_ycbcr[0].stride;
        u_row+=_ycbcr[1].stride;
        v_row+=_ycbcr[2].stride;
        rgb_row+=cstride;
      }
    }break;
    /*case TH_PF_444:*/
    default:{
      for(y=0;y<h;y++){
        for(x=0;x<w;x++){
          int y;
          int u;
          int v;
          y=(65481*rgb_row[4*x+2]+128553*rgb_row[4*x+1]
           +24966*rgb_row[4*x+0]+4207500)/255000;
          u=(-33488*rgb_row[4*x+2]-65744*rgb_row[4*x+1]
           +99232*rgb_row[4*x+0]+29032005)/225930;
          v=(157024*rgb_row[4*x+2]-131488*rgb_row[4*x+1]
           -25536*rgb_row[4*x+0]+45940035)/357510;
          y_row[x]=OC_CLAMP255(y);
          u_row[x]=OC_CLAMP255(u);
          v_row[x]=OC_CLAMP255(v);
        }
        y_row+=_ycbcr[0].stride;
        u_row+=_ycbcr[1].stride;
        v_row+=_ycbcr[2].stride;
        rgb_row+=cstride;
      }
    }break;
  }
  /*Finished.
    Destroy the surface.*/
  cairo_surface_destroy(cs);
}
#endif

int th_decode_packetin(th_dec_ctx *_dec,const ogg_packet *_op,
 ogg_int64_t *_granpos){}

int th_decode_ycbcr_out(th_dec_ctx *_dec,th_ycbcr_buffer _ycbcr){}