/* * FLAC (Free Lossless Audio Codec) decoder * Copyright (c) 2003 Alex Beregszaszi * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /** * @file flac.c * FLAC (Free Lossless Audio Codec) decoder * @author Alex Beregszaszi * * For more information on the FLAC format, visit: * http://flac.sourceforge.net/ * * This decoder can be used in 1 of 2 ways: Either raw FLAC data can be fed * through, starting from the initial 'fLaC' signature; or by passing the * 34-byte streaminfo structure through avctx->extradata[_size] followed * by data starting with the 0xFFF8 marker. */ #include #include #ifndef BUILD_STANDALONE #include "../codec.h" #endif #include "bitstream.h" #include "golomb.h" #include "decoder.h" #if defined(CPU_COLDFIRE) #include "coldfire.h" #elif defined(CPU_ARM) #include "arm.h" #endif #define FFMAX(a,b) ((a) > (b) ? (a) : (b)) #define FFMIN(a,b) ((a) > (b) ? (b) : (a)) static const int sample_rate_table[] ICONST_ATTR = { 0, 0, 0, 0, 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000, 0, 0, 0, 0 }; static const int sample_size_table[] ICONST_ATTR = { 0, 8, 12, 0, 16, 20, 24, 0 }; static const int blocksize_table[] ICONST_ATTR = { 0, 192, 576<<0, 576<<1, 576<<2, 576<<3, 0, 0, 256<<0, 256<<1, 256<<2, 256<<3, 256<<4, 256<<5, 256<<6, 256<<7 }; static const uint8_t table_crc8[256] ICONST_ATTR = { 0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15, 0x38, 0x3f, 0x36, 0x31, 0x24, 0x23, 0x2a, 0x2d, 0x70, 0x77, 0x7e, 0x79, 0x6c, 0x6b, 0x62, 0x65, 0x48, 0x4f, 0x46, 0x41, 0x54, 0x53, 0x5a, 0x5d, 0xe0, 0xe7, 0xee, 0xe9, 0xfc, 0xfb, 0xf2, 0xf5, 0xd8, 0xdf, 0xd6, 0xd1, 0xc4, 0xc3, 0xca, 0xcd, 0x90, 0x97, 0x9e, 0x99, 0x8c, 0x8b, 0x82, 0x85, 0xa8, 0xaf, 0xa6, 0xa1, 0xb4, 0xb3, 0xba, 0xbd, 0xc7, 0xc0, 0xc9, 0xce, 0xdb, 0xdc, 0xd5, 0xd2, 0xff, 0xf8, 0xf1, 0xf6, 0xe3, 0xe4, 0xed, 0xea, 0xb7, 0xb0, 0xb9, 0xbe, 0xab, 0xac, 0xa5, 0xa2, 0x8f, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9d, 0x9a, 0x27, 0x20, 0x29, 0x2e, 0x3b, 0x3c, 0x35, 0x32, 0x1f, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0d, 0x0a, 0x57, 0x50, 0x59, 0x5e, 0x4b, 0x4c, 0x45, 0x42, 0x6f, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7d, 0x7a, 0x89, 0x8e, 0x87, 0x80, 0x95, 0x92, 0x9b, 0x9c, 0xb1, 0xb6, 0xbf, 0xb8, 0xad, 0xaa, 0xa3, 0xa4, 0xf9, 0xfe, 0xf7, 0xf0, 0xe5, 0xe2, 0xeb, 0xec, 0xc1, 0xc6, 0xcf, 0xc8, 0xdd, 0xda, 0xd3, 0xd4, 0x69, 0x6e, 0x67, 0x60, 0x75, 0x72, 0x7b, 0x7c, 0x51, 0x56, 0x5f, 0x58, 0x4d, 0x4a, 0x43, 0x44, 0x19, 0x1e, 0x17, 0x10, 0x05, 0x02, 0x0b, 0x0c, 0x21, 0x26, 0x2f, 0x28, 0x3d, 0x3a, 0x33, 0x34, 0x4e, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5c, 0x5b, 0x76, 0x71, 0x78, 0x7f, 0x6a, 0x6d, 0x64, 0x63, 0x3e, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2c, 0x2b, 0x06, 0x01, 0x08, 0x0f, 0x1a, 0x1d, 0x14, 0x13, 0xae, 0xa9, 0xa0, 0xa7, 0xb2, 0xb5, 0xbc, 0xbb, 0x96, 0x91, 0x98, 0x9f, 0x8a, 0x8d, 0x84, 0x83, 0xde, 0xd9, 0xd0, 0xd7, 0xc2, 0xc5, 0xcc, 0xcb, 0xe6, 0xe1, 0xe8, 0xef, 0xfa, 0xfd, 0xf4, 0xf3 }; static int64_t get_utf8(GetBitContext *gb) ICODE_ATTR_FLAC; static int64_t get_utf8(GetBitContext *gb) { uint64_t val; int ones=0, bytes; while(get_bits1(gb)) ones++; if (ones==0) bytes=0; else if(ones==1) return -1; else bytes= ones - 1; val= get_bits(gb, 7-ones); while(bytes--){ const int tmp = get_bits(gb, 8); if((tmp>>6) != 2) return -2; val<<=6; val|= tmp&0x3F; } return val; } static int get_crc8(const uint8_t *buf, int count) ICODE_ATTR_FLAC; static int get_crc8(const uint8_t *buf, int count) { int crc=0; int i; for(i=0; igb, 2); if (method_type > 1){ //fprintf(stderr,"illegal residual coding method %d\n", method_type); return -3; } rice_order = get_bits(&s->gb, 4); samples= s->blocksize >> rice_order; sample= i= pred_order; for (partition = 0; partition < (1 << rice_order); partition++) { tmp = get_bits(&s->gb, method_type == 0 ? 4 : 5); if (tmp == (method_type == 0 ? 15 : 31)) { //fprintf(stderr,"fixed len partition\n"); tmp = get_bits(&s->gb, 5); for (; i < samples; i++, sample++) decoded[sample] = get_sbits(&s->gb, tmp); } else { for (; i < samples; i++, sample++){ decoded[sample] = get_sr_golomb_flac(&s->gb, tmp, INT_MAX, 0); } } i= 0; } return 0; } static int decode_subframe_fixed(FLACContext *s, int32_t* decoded, int pred_order) ICODE_ATTR_FLAC; static int decode_subframe_fixed(FLACContext *s, int32_t* decoded, int pred_order) { const int blocksize = s->blocksize; int a, b, c, d, i; /* warm up samples */ for (i = 0; i < pred_order; i++) { decoded[i] = get_sbits(&s->gb, s->curr_bps); } if (decode_residuals(s, decoded, pred_order) < 0) return -4; a = decoded[pred_order-1]; b = a - decoded[pred_order-2]; c = b - decoded[pred_order-2] + decoded[pred_order-3]; d = c - decoded[pred_order-2] + 2*decoded[pred_order-3] - decoded[pred_order-4]; switch(pred_order) { case 0: break; case 1: for (i = pred_order; i < blocksize; i++) decoded[i] = a += decoded[i]; break; case 2: for (i = pred_order; i < blocksize; i++) decoded[i] = a += b += decoded[i]; break; case 3: for (i = pred_order; i < blocksize; i++) decoded[i] = a += b += c += decoded[i]; break; case 4: for (i = pred_order; i < blocksize; i++) decoded[i] = a += b += c += d += decoded[i]; break; default: return -5; } return 0; } static int decode_subframe_lpc(FLACContext *s, int32_t* decoded, int pred_order) ICODE_ATTR_FLAC; static int decode_subframe_lpc(FLACContext *s, int32_t* decoded, int pred_order) { int sum, i, j; int64_t wsum; int coeff_prec, qlevel; int coeffs[pred_order]; /* warm up samples */ for (i = 0; i < pred_order; i++) { decoded[i] = get_sbits(&s->gb, s->curr_bps); } coeff_prec = get_bits(&s->gb, 4) + 1; if (coeff_prec == 16) { //fprintf(stderr,"invalid coeff precision\n"); return -6; } qlevel = get_sbits(&s->gb, 5); if (qlevel < 0) { //fprintf(stderr,"qlevel %d not supported, maybe buggy stream\n", qlevel); return -7; } for (i = 0; i < pred_order; i++) { coeffs[i] = get_sbits(&s->gb, coeff_prec); } if (decode_residuals(s, decoded, pred_order) < 0) return -8; if ((s->bps + coeff_prec + av_log2(pred_order)) <= 32) { #if defined(CPU_COLDFIRE) (void)sum; lpc_decode_emac(s->blocksize - pred_order, qlevel, pred_order, decoded + pred_order, coeffs); #elif defined(CPU_ARM) (void)sum; lpc_decode_arm(s->blocksize - pred_order, qlevel, pred_order, decoded + pred_order, coeffs); #else for (i = pred_order; i < s->blocksize; i++) { sum = 0; for (j = 0; j < pred_order; j++) sum += coeffs[j] * decoded[i-j-1]; decoded[i] += sum >> qlevel; } #endif } else { #if defined(CPU_COLDFIRE) (void)wsum; (void)j; lpc_decode_emac_wide(s->blocksize - pred_order, qlevel, pred_order, decoded + pred_order, coeffs); #else for (i = pred_order; i < s->blocksize; i++) { wsum = 0; for (j = 0; j < pred_order; j++) wsum += (int64_t)coeffs[j] * (int64_t)decoded[i-j-1]; decoded[i] += wsum >> qlevel; } #endif } return 0; } static inline int decode_subframe(FLACContext *s, int channel, int32_t* decoded) { int type, wasted = 0; int i, tmp; s->curr_bps = s->bps; if(channel == 0){ if(s->decorrelation == RIGHT_SIDE) s->curr_bps++; }else{ if(s->decorrelation == LEFT_SIDE || s->decorrelation == MID_SIDE) s->curr_bps++; } if (get_bits1(&s->gb)) { //fprintf(stderr,"invalid subframe padding\n"); return -9; } type = get_bits(&s->gb, 6); // wasted = get_bits1(&s->gb); // if (wasted) // { // while (!get_bits1(&s->gb)) // wasted++; // if (wasted) // wasted++; // s->curr_bps -= wasted; // } #if 0 wasted= 16 - av_log2(show_bits(&s->gb, 17)); skip_bits(&s->gb, wasted+1); s->curr_bps -= wasted; #else if (get_bits1(&s->gb)) { wasted = 1; while (!get_bits1(&s->gb)) wasted++; s->curr_bps -= wasted; //fprintf(stderr,"%d wasted bits\n", wasted); } #endif //FIXME use av_log2 for types if (type == 0) { //fprintf(stderr,"coding type: constant\n"); tmp = get_sbits(&s->gb, s->curr_bps); for (i = 0; i < s->blocksize; i++) decoded[i] = tmp; } else if (type == 1) { //fprintf(stderr,"coding type: verbatim\n"); for (i = 0; i < s->blocksize; i++) decoded[i] = get_sbits(&s->gb, s->curr_bps); } else if ((type >= 8) && (type <= 12)) { //fprintf(stderr,"coding type: fixed\n"); if (decode_subframe_fixed(s, decoded, type & ~0x8) < 0) return -10; } else if (type >= 32) { //fprintf(stderr,"coding type: lpc\n"); if (decode_subframe_lpc(s, decoded, (type & ~0x20)+1) < 0) return -11; } else { //fprintf(stderr,"Unknown coding type: %d\n",type); return -12; } if (wasted) { int i; for (i = 0; i < s->blocksize; i++) decoded[i] <<= wasted; } return 0; } static int decode_frame(FLACContext *s, int32_t* decoded0, int32_t* decoded1, void (*yield)(void)) ICODE_ATTR_FLAC; static int decode_frame(FLACContext *s, int32_t* decoded0, int32_t* decoded1, void (*yield)(void)) { int blocksize_code, sample_rate_code, sample_size_code, assignment, crc8; int decorrelation, bps, blocksize, samplerate; int res; blocksize_code = get_bits(&s->gb, 4); sample_rate_code = get_bits(&s->gb, 4); assignment = get_bits(&s->gb, 4); /* channel assignment */ if (assignment < 8 && s->channels == assignment+1) decorrelation = INDEPENDENT; else if (assignment >=8 && assignment < 11 && s->channels == 2) decorrelation = LEFT_SIDE + assignment - 8; else { return -13; } sample_size_code = get_bits(&s->gb, 3); if(sample_size_code == 0) bps= s->bps; else if((sample_size_code != 3) && (sample_size_code != 7)) bps = sample_size_table[sample_size_code]; else { return -14; } if (get_bits1(&s->gb)) { return -15; } /* Get the samplenumber of the first sample in this block */ s->samplenumber=get_utf8(&s->gb); /* samplenumber actually contains the frame number for streams with a constant block size - so we multiply by blocksize to get the actual sample number */ if (s->min_blocksize == s->max_blocksize) { s->samplenumber*=s->min_blocksize; } #if 0 if (/*((blocksize_code == 6) || (blocksize_code == 7)) &&*/ (s->min_blocksize != s->max_blocksize)){ }else{ } #endif if (blocksize_code == 0) blocksize = s->min_blocksize; else if (blocksize_code == 6) blocksize = get_bits(&s->gb, 8)+1; else if (blocksize_code == 7) blocksize = get_bits(&s->gb, 16)+1; else blocksize = blocksize_table[blocksize_code]; if(blocksize > s->max_blocksize){ return -16; } if (sample_rate_code == 0){ samplerate= s->samplerate; }else if ((sample_rate_code > 3) && (sample_rate_code < 12)) samplerate = sample_rate_table[sample_rate_code]; else if (sample_rate_code == 12) samplerate = get_bits(&s->gb, 8) * 1000; else if (sample_rate_code == 13) samplerate = get_bits(&s->gb, 16); else if (sample_rate_code == 14) samplerate = get_bits(&s->gb, 16) * 10; else{ return -17; } skip_bits(&s->gb, 8); crc8= get_crc8(s->gb.buffer, get_bits_count(&s->gb)/8); if(crc8){ return -18; } s->blocksize = blocksize; s->samplerate = samplerate; s->bps = bps; s->decorrelation= decorrelation; yield(); /* subframes */ if ((res=decode_subframe(s, 0, decoded0)) < 0) return res-100; yield(); if (s->channels==2) { if ((res=decode_subframe(s, 1, decoded1)) < 0) return res-200; } yield(); align_get_bits(&s->gb); /* frame footer */ skip_bits(&s->gb, 16); /* data crc */ return 0; } int flac_decode_frame(FLACContext *s, int32_t* decoded0, int32_t* decoded1, uint8_t *buf, int buf_size, void (*yield)(void)) { int tmp; int i; int framesize; int scale; init_get_bits(&s->gb, buf, buf_size*8); tmp = get_bits(&s->gb, 16); if ((tmp & 0xFFFE) != 0xFFF8){ return -41; } if ((framesize=decode_frame(s,decoded0,decoded1,yield)) < 0){ s->bitstream_size=0; s->bitstream_index=0; return framesize; } yield(); scale=FLAC_OUTPUT_DEPTH-s->bps; switch(s->decorrelation) { case INDEPENDENT: if (s->channels==1) {; for (i = 0; i < s->blocksize; i++) { decoded0[i] = decoded0[i] << scale; } } else { for (i = 0; i < s->blocksize; i++) { decoded0[i] = decoded0[i] << scale; decoded1[i] = decoded1[i] << scale; } } break; case LEFT_SIDE: //assert(s->channels == 2); for (i = 0; i < s->blocksize; i++) { decoded1[i] = (decoded0[i] - decoded1[i]) << scale; decoded0[i] = decoded0[i] << scale; } break; case RIGHT_SIDE: //assert(s->channels == 2); for (i = 0; i < s->blocksize; i++) { decoded0[i] = (decoded0[i] + decoded1[i]) << scale; decoded1[i] = decoded1[i] << scale; } break; case MID_SIDE: //assert(s->channels == 2); for (i = 0; i < s->blocksize; i++) { int mid, side; mid = decoded0[i]; side = decoded1[i]; #if 1 //needs to be checked but IMHO it should be binary identical mid -= side>>1; decoded0[i] = (mid + side) << scale; decoded1[i] = mid << scale; #else mid <<= 1; if (side & 1) mid++; decoded0[i] = ((mid + side) >> 1) << scale; decoded1[i] = ((mid - side) >> 1) << scale; #endif } break; } s->framesize = (get_bits_count(&s->gb)+7)>>3; return 0; } '#n412'>412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
/* Copyright (C) 2003 Jean-Marc Valin */
/**
   @file fixed_debug.h
   @brief Fixed-point operations with debugging
*/
/*
   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:
   
   - Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
   
   - Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
   
   - Neither the name of the Xiph.org Foundation nor the names of its
   contributors may be used to endorse or promote products derived from
   this software without specific prior written permission.
   
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef FIXED_DEBUG_H
#define FIXED_DEBUG_H

#include <stdio.h>

extern long long spx_mips;
#define MIPS_INC spx_mips++,

#define QCONST16(x,bits) ((spx_word16_t)(.5+(x)*(((spx_word32_t)1)<<(bits))))
#define QCONST32(x,bits) ((spx_word32_t)(.5+(x)*(((spx_word32_t)1)<<(bits))))


#define VERIFY_SHORT(x) ((x)<=32767&&(x)>=-32768)
#define VERIFY_INT(x) ((x)<=2147483647LL&&(x)>=-2147483648LL)

static inline short NEG16(int x)
{
   int res;
   if (!VERIFY_SHORT(x))
   {
      fprintf (stderr, "NEG16: input is not short: %d\n", (int)x);
   }
   res = -x;
   if (!VERIFY_SHORT(res))
      fprintf (stderr, "NEG16: output is not short: %d\n", (int)res);
   spx_mips++;
   return res;
}
static inline int NEG32(long long x)
{
   long long res;
   if (!VERIFY_INT(x))
   {
      fprintf (stderr, "NEG16: input is not int: %d\n", (int)x);
   }
   res = -x;
   if (!VERIFY_INT(res))
      fprintf (stderr, "NEG16: output is not int: %d\n", (int)res);
   spx_mips++;
   return res;
}

#define EXTRACT16(x) _EXTRACT16(x, __FILE__, __LINE__)
static inline short _EXTRACT16(int x, char *file, int line)
{
   int res;
   if (!VERIFY_SHORT(x))
   {
      fprintf (stderr, "EXTRACT16: input is not short: %d in %s: line %d\n", x, file, line);
   }
   res = x;
   spx_mips++;
   return res;
}

#define EXTEND32(x) _EXTEND32(x, __FILE__, __LINE__)
static inline int _EXTEND32(int x, char *file, int line)
{
   int res;
   if (!VERIFY_SHORT(x))
   {
      fprintf (stderr, "EXTEND32: input is not short: %d in %s: line %d\n", x, file, line);
   }
   res = x;
   spx_mips++;
   return res;
}

#define SHR16(a, shift) _SHR16(a, shift, __FILE__, __LINE__)
static inline short _SHR16(int a, int shift, char *file, int line) 
{
   int res;
   if (!VERIFY_SHORT(a) || !VERIFY_SHORT(shift))
   {
      fprintf (stderr, "SHR16: inputs are not short: %d >> %d in %s: line %d\n", a, shift, file, line);
   }
   res = a>>shift;
   if (!VERIFY_SHORT(res))
      fprintf (stderr, "SHR16: output is not short: %d in %s: line %d\n", res, file, line);
   spx_mips++;
   return res;
}
#define SHL16(a, shift) _SHL16(a, shift, __FILE__, __LINE__)
static inline short _SHL16(int a, int shift, char *file, int line) 
{
   int res;
   if (!VERIFY_SHORT(a) || !VERIFY_SHORT(shift))
   {
      fprintf (stderr, "SHL16: inputs are not short: %d %d in %s: line %d\n", a, shift, file, line);
   }
   res = a<<shift;
   if (!VERIFY_SHORT(res))
      fprintf (stderr, "SHL16: output is not short: %d in %s: line %d\n", res, file, line);
   spx_mips++;
   return res;
}

static inline int SHR32(long long a, int shift) 
{
   long long  res;
   if (!VERIFY_INT(a) || !VERIFY_SHORT(shift))
   {
      fprintf (stderr, "SHR32: inputs are not int: %d %d\n", (int)a, shift);
   }
   res = a>>shift;
   if (!VERIFY_INT(res))
   {
      fprintf (stderr, "SHR32: output is not int: %d\n", (int)res);
   }
   spx_mips++;
   return res;
}
static inline int SHL32(long long a, int shift) 
{
   long long  res;
   if (!VERIFY_INT(a) || !VERIFY_SHORT(shift))
   {
      fprintf (stderr, "SHL32: inputs are not int: %d %d\n", (int)a, shift);
   }
   res = a<<shift;
   if (!VERIFY_INT(res))
   {
      fprintf (stderr, "SHL32: output is not int: %d\n", (int)res);
   }
   spx_mips++;
   return res;
}

#define PSHR16(a,shift) (SHR16(ADD16((a),((1<<((shift))>>1))),shift))
#define PSHR32(a,shift) (SHR32(ADD32((a),((1<<((shift))>>1))),shift))
#define VSHR32(a, shift) (((shift)>0) ? SHR32(a, shift) : SHL32(a, -(shift)))

#define SATURATE16(x,a) (((x)>(a) ? (a) : (x)<-(a) ? -(a) : (x)))
#define SATURATE32(x,a) (((x)>(a) ? (a) : (x)<-(a) ? -(a) : (x)))

//#define SHR(a,shift) ((a) >> (shift))
//#define SHL(a,shift) ((a) << (shift))

#define ADD16(a, b) _ADD16(a, b, __FILE__, __LINE__)
static inline short _ADD16(int a, int b, char *file, int line) 
{
   int res;
   if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
   {
      fprintf (stderr, "ADD16: inputs are not short: %d %d in %s: line %d\n", a, b, file, line);
   }
   res = a+b;
   if (!VERIFY_SHORT(res))
   {
      fprintf (stderr, "ADD16: output is not short: %d+%d=%d in %s: line %d\n", a,b,res, file, line);
   }
   spx_mips++;
   return res;
}

#define SUB16(a, b) _SUB16(a, b, __FILE__, __LINE__)
static inline short _SUB16(int a, int b, char *file, int line) 
{
   int res;
   if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
   {
      fprintf (stderr, "SUB16: inputs are not short: %d %d in %s: line %d\n", a, b, file, line);
   }
   res = a-b;
   if (!VERIFY_SHORT(res))
      fprintf (stderr, "SUB16: output is not short: %d in %s: line %d\n", res, file, line);
   spx_mips++;
   return res;
}

#define ADD32(a, b) _ADD32(a, b, __FILE__, __LINE__)
static inline int _ADD32(long long a, long long b, char *file, int line) 
{
   long long res;
   if (!VERIFY_INT(a) || !VERIFY_INT(b))
   {
      fprintf (stderr, "ADD32: inputs are not int: %d %d in %s: line %d\n", (int)a, (int)b, file, line);
   }
   res = a+b;
   if (!VERIFY_INT(res))
   {
      fprintf (stderr, "ADD32: output is not int: %d in %s: line %d\n", (int)res, file, line);
   }
   spx_mips++;
   return res;
}

static inline int SUB32(long long a, long long b) 
{
   long long res;
   if (!VERIFY_INT(a) || !VERIFY_INT(b))
   {
      fprintf (stderr, "SUB32: inputs are not int: %d %d\n", (int)a, (int)b);
   }
   res = a-b;
   if (!VERIFY_INT(res))
      fprintf (stderr, "SUB32: output is not int: %d\n", (int)res);
   spx_mips++;
   return res;
}

#define ADD64(a,b) (MIPS_INC(a)+(b))

/* result fits in 16 bits */
static inline short MULT16_16_16(int a, int b) 
{
   int res;
   if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
   {
      fprintf (stderr, "MULT16_16_16: inputs are not short: %d %d\n", a, b);
   }
   res = a*b;
   if (!VERIFY_SHORT(res))
      fprintf (stderr, "MULT16_16_16: output is not short: %d\n", res);
   spx_mips++;
   return res;
}

#define MULT16_16(a, b) _MULT16_16(a, b, __FILE__, __LINE__)
static inline int _MULT16_16(int a, int b, char *file, int line) 
{
   long long res;
   if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
   {
      fprintf (stderr, "MULT16_16: inputs are not short: %d %d in %s: line %d\n", a, b, file, line);
   }
   res = ((long long)a)*b;
   if (!VERIFY_INT(res))
      fprintf (stderr, "MULT16_16: output is not int: %d in %s: line %d\n", (int)res, file, line);
   spx_mips++;
   return res;
}

#define MAC16_16(c,a,b)     (spx_mips--,ADD32((c),MULT16_16((a),(b))))
#define MAC16_16_Q11(c,a,b)     (EXTRACT16(ADD16((c),EXTRACT16(SHR32(MULT16_16((a),(b)),11)))))
#define MAC16_16_Q13(c,a,b)     (EXTRACT16(ADD16((c),EXTRACT16(SHR32(MULT16_16((a),(b)),13)))))
#define MAC16_16_P13(c,a,b)     (EXTRACT16(ADD32((c),SHR32(ADD32(4096,MULT16_16((a),(b))),13))))


#define MULT16_32_QX(a, b, Q) _MULT16_32_QX(a, b, Q, __FILE__, __LINE__)
static inline int _MULT16_32_QX(int a, long long b, int Q, char *file, int line)
{
   long long res;
   if (!VERIFY_SHORT(a) || !VERIFY_INT(b))
   {
      fprintf (stderr, "MULT16_32_Q%d: inputs are not short+int: %d %d in %s: line %d\n", Q, (int)a, (int)b, file, line);
   }
   if (ABS32(b)>=(1<<(15+Q)))
      fprintf (stderr, "MULT16_32_Q%d: second operand too large: %d %d in %s: line %d\n", Q, (int)a, (int)b, file, line);      
   res = (((long long)a)*(long long)b) >> Q;
   if (!VERIFY_INT(res))
      fprintf (stderr, "MULT16_32_Q%d: output is not int: %d*%d=%d in %s: line %d\n", Q, (int)a, (int)b,(int)res, file, line);
   spx_mips+=5;
   return res;
}

static inline int MULT16_32_PX(int a, long long b, int Q)
{
   long long res;
   if (!VERIFY_SHORT(a) || !VERIFY_INT(b))
   {
      fprintf (stderr, "MULT16_32_P%d: inputs are not short+int: %d %d\n", Q, (int)a, (int)b);
   }
   if (ABS32(b)>=(1<<(15+Q)))
      fprintf (stderr, "MULT16_32_Q%d: second operand too large: %d %d\n", Q, (int)a, (int)b);      
   res = ((((long long)a)*(long long)b) + ((1<<Q)>>1))>> Q;
   if (!VERIFY_INT(res))
      fprintf (stderr, "MULT16_32_P%d: output is not int: %d*%d=%d\n", Q, (int)a, (int)b,(int)res);
   spx_mips+=5;
   return res;
}


#define MULT16_32_Q11(a,b) MULT16_32_QX(a,b,11)
#define MAC16_32_Q11(c,a,b) ADD32((c),MULT16_32_Q11((a),(b)))
#define MULT16_32_Q12(a,b) MULT16_32_QX(a,b,12)
#define MULT16_32_Q13(a,b) MULT16_32_QX(a,b,13)
#define MULT16_32_Q14(a,b) MULT16_32_QX(a,b,14)
#define MULT16_32_Q15(a,b) MULT16_32_QX(a,b,15)
#define MULT16_32_P15(a,b) MULT16_32_PX(a,b,15)
#define MAC16_32_Q15(c,a,b) ADD32((c),MULT16_32_Q15((a),(b)))

static inline int SATURATE(int a, int b)
{
   if (a>b)
      a=b;
   if (a<-b)
      a = -b;
   return a;
}

static inline int MULT16_16_Q11_32(int a, int b) 
{
   long long res;
   if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
   {
      fprintf (stderr, "MULT16_16_Q11: inputs are not short: %d %d\n", a, b);
   }
   res = ((long long)a)*b;
   res >>= 11;
   if (!VERIFY_INT(res))
      fprintf (stderr, "MULT16_16_Q11: output is not short: %d*%d=%d\n", (int)a, (int)b, (int)res);
   spx_mips+=3;
   return res;
}
static inline short MULT16_16_Q13(int a, int b) 
{
   long long res;
   if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
   {
      fprintf (stderr, "MULT16_16_Q13: inputs are not short: %d %d\n", a, b);
   }
   res = ((long long)a)*b;
   res >>= 13;
   if (!VERIFY_SHORT(res))
      fprintf (stderr, "MULT16_16_Q13: output is not short: %d*%d=%d\n", a, b, (int)res);
   spx_mips+=3;
   return res;
}
static inline short MULT16_16_Q14(int a, int b) 
{
   long long res;
   if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
   {
      fprintf (stderr, "MULT16_16_Q14: inputs are not short: %d %d\n", a, b);
   }
   res = ((long long)a)*b;
   res >>= 14;
   if (!VERIFY_SHORT(res))
      fprintf (stderr, "MULT16_16_Q14: output is not short: %d\n", (int)res);
   spx_mips+=3;
   return res;
}
static inline short MULT16_16_Q15(int a, int b) 
{
   long long res;
   if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
   {
      fprintf (stderr, "MULT16_16_Q15: inputs are not short: %d %d\n", a, b);
   }
   res = ((long long)a)*b;
   res >>= 15;
   if (!VERIFY_SHORT(res))
   {
      fprintf (stderr, "MULT16_16_Q15: output is not short: %d\n", (int)res);
   }
   spx_mips+=3;
   return res;
}