📄 mpegaudbits.c
字号:
/* * MPEG Audio decoder * Copyright (c) 2001, 2002 Fabrice Bellard. * * 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. * *//** * @file mpegaudiodec.c * MPEG Audio decoder. * from FFMPEG_VERSION "0.4.8" */ //#define DEBUG#include <stdio.h>#include "mpegaudiodec.h"unsigned int get_bits1(GetBitContext *s){ int index= s->index; uint8_t result= s->buffer[ index>>3 ]; result<<= (index&0x07); result>>= 8 - 1; index++; s->index= index; return result;}int get_bits_count(GetBitContext *s){ return s->index;}static inline_t unsigned int ByteSwap32(unsigned int xx){ unsigned int tt; tt = xx >> 24; tt |= (xx >> 8) & 0xff00; tt |= (xx << 8) & 0xff0000; tt |= (xx << 24) & 0xff000000; return tt;}static inline_t uint32_t unaligned32(const void *v) { return *(const uint32_t *) v;}static int unaligned32_be(const void *v){#ifdef UNDER_CE //Fix for MPEG audio not working, it would exception with data misalignment // if an unaligned v was passed to unaligned32() function unsigned char *puc=(unsigned char *)v; uint32_t dw=((uint32_t)puc[3])|((uint32_t)puc[2]<<8)|((uint32_t)puc[1]<<16)|((uint32_t)puc[0]<<24); return dw;#else return ByteSwap32( unaligned32(v)); //original#endif}/** * reads 0-17 bits. */unsigned int get_bits(GetBitContext *s, int n){ register int tmp; int re_index= (s)->index; int re_cache= 0; re_cache= unaligned32_be( ((uint8_t *)(s)->buffer)+(re_index>>3) ) << (re_index&0x07); tmp = (((uint32_t)(re_cache))>>(32-(n))); re_index += (n); (s)->index= re_index; return tmp;}void skip_bits(GetBitContext *s, int n){ int re_index= (s)->index; int re_cache= 0; re_cache= unaligned32_be( ((uint8_t *)(s)->buffer)+(re_index>>3) ) << (re_index&0x07); re_index += (n); (s)->index= re_index;}int get_vlc(GetBitContext *s, VLC *vlc){ int code; VLC_TYPE (*table)[2]= vlc->table; int re_index= (s)->index; int re_cache= 0; re_cache= unaligned32_be( ((uint8_t *)(s)->buffer)+(re_index>>3) ) << (re_index&0x07); /* * if the vlc code is invalid and max_depth=1 than no bits will be removed * if the vlc code is invalid and max_depth>1 than the number of bits removed * is undefined */ { int n, index, nb_bits; index= (((uint32_t)(re_cache))>>(32-(vlc->bits))); code = table[index][0]; n = table[index][1]; if (n < 0) { re_index += (vlc->bits); re_cache= unaligned32_be( ((uint8_t *)(s)->buffer)+(re_index>>3) ) << (re_index&0x07); nb_bits = -n; index= (((uint32_t)(re_cache))>>(32-(nb_bits))) + code; code = table[index][0]; n = table[index][1]; if (n < 0) { re_index += (nb_bits); re_cache= unaligned32_be( ((uint8_t *)(s)->buffer)+(re_index>>3) ) << (re_index&0x07); nb_bits = -n; index= (((uint32_t)(re_cache))>>(32-(nb_bits))) + code; code = table[index][0]; n = table[index][1]; } } re_cache <<= (n); re_index += (n); } (s)->index= re_index; return code;}/** * init GetBitContext. * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end * @param bit_size the size of the buffer in bits */void init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size){ const int buffer_size = (bit_size+7)>>3; dprintf("buffer_size %x %x\n", buffer_size, bit_size); s->buffer= buffer; s->size_in_bits= bit_size; s->buffer_end= buffer + buffer_size; s->index=0; { int re_index= (s)->index; int re_cache= 0; re_cache= unaligned32_be( ((uint8_t *)(s)->buffer)+(re_index>>3) ) << (re_index&0x07); re_cache= unaligned32_be( ((uint8_t *)(s)->buffer)+(re_index>>3) ) << (re_index&0x07); (s)->index= re_index; }}/* handle n = 0 too */int get_bitsz(GetBitContext *s, int n){ if (n == 0) return 0; else return get_bits(s, n);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -