diff options
-rw-r--r-- | src/libffmpeg/libavcodec/avs.c | 158 | ||||
-rw-r--r-- | src/libffmpeg/libavcodec/cavs.c | 1500 | ||||
-rw-r--r-- | src/libffmpeg/libavcodec/cavsdata.h | 639 | ||||
-rw-r--r-- | src/libffmpeg/libavcodec/cook.c | 1311 | ||||
-rw-r--r-- | src/libffmpeg/libavcodec/cookdata.h | 557 | ||||
-rw-r--r-- | src/libffmpeg/libavcodec/cscd.c | 262 | ||||
-rw-r--r-- | src/libffmpeg/libavcodec/flashsv.c | 274 | ||||
-rw-r--r-- | src/libffmpeg/libavcodec/kmvc.c | 417 | ||||
-rw-r--r-- | src/libffmpeg/libavcodec/lzo.c | 224 | ||||
-rw-r--r-- | src/libffmpeg/libavcodec/mmvideo.c | 204 | ||||
-rw-r--r-- | src/libffmpeg/libavcodec/nuv.c | 214 | ||||
-rw-r--r-- | src/libffmpeg/libavcodec/rtjpeg.c | 162 | ||||
-rw-r--r-- | src/libffmpeg/libavcodec/smacker.c | 725 | ||||
-rw-r--r-- | src/libffmpeg/libavcodec/truespeech.c | 379 | ||||
-rw-r--r-- | src/libffmpeg/libavcodec/truespeech_data.h | 136 | ||||
-rw-r--r-- | src/libffmpeg/libavcodec/tta.c | 455 | ||||
-rw-r--r-- | src/libffmpeg/libavcodec/vc1.c | 3228 | ||||
-rw-r--r-- | src/libffmpeg/libavcodec/vc1acdata.h | 564 | ||||
-rw-r--r-- | src/libffmpeg/libavcodec/vc1data.h | 613 | ||||
-rw-r--r-- | src/libffmpeg/libavcodec/vc1dsp.c | 451 | ||||
-rw-r--r-- | src/libffmpeg/libavcodec/zmbv.c | 692 |
21 files changed, 13165 insertions, 0 deletions
diff --git a/src/libffmpeg/libavcodec/avs.c b/src/libffmpeg/libavcodec/avs.c new file mode 100644 index 000000000..557e9becb --- /dev/null +++ b/src/libffmpeg/libavcodec/avs.c @@ -0,0 +1,158 @@ +/* + * AVS video decoder. + * Copyright (c) 2006 Aurelien Jacobs <aurel@gnuage.org> + * + * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "avcodec.h" +#include "bitstream.h" + + +typedef struct { + AVFrame picture; +} avs_context_t; + +typedef enum { + AVS_VIDEO = 0x01, + AVS_AUDIO = 0x02, + AVS_PALETTE = 0x03, + AVS_GAME_DATA = 0x04, +} avs_block_type_t; + +typedef enum { + AVS_I_FRAME = 0x00, + AVS_P_FRAME_3X3 = 0x01, + AVS_P_FRAME_2X2 = 0x02, + AVS_P_FRAME_2X3 = 0x03, +} avs_video_sub_type_t; + + +static int +avs_decode_frame(AVCodecContext * avctx, + void *data, int *data_size, uint8_t * buf, int buf_size) +{ + avs_context_t *const avs = avctx->priv_data; + AVFrame *picture = data; + AVFrame *const p = (AVFrame *) & avs->picture; + uint8_t *table, *vect, *out; + int i, j, x, y, stride, vect_w = 3, vect_h = 3; + int sub_type; + avs_block_type_t type; + GetBitContext change_map; + + if (avctx->reget_buffer(avctx, p)) { + av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n"); + return -1; + } + p->reference = 1; + p->pict_type = FF_P_TYPE; + p->key_frame = 0; + + out = avs->picture.data[0]; + stride = avs->picture.linesize[0]; + + sub_type = buf[0]; + type = buf[1]; + buf += 4; + + if (type == AVS_PALETTE) { + int first, last; + uint32_t *pal = (uint32_t *) avs->picture.data[1]; + + first = LE_16(buf); + last = first + LE_16(buf + 2); + buf += 4; + for (i=first; i<last; i++, buf+=3) + pal[i] = (buf[0] << 18) | (buf[1] << 10) | (buf[2] << 2); + + sub_type = buf[0]; + type = buf[1]; + buf += 4; + } + + if (type != AVS_VIDEO) + return -1; + + switch (sub_type) { + case AVS_I_FRAME: + p->pict_type = FF_I_TYPE; + p->key_frame = 1; + case AVS_P_FRAME_3X3: + vect_w = 3; + vect_h = 3; + break; + + case AVS_P_FRAME_2X2: + vect_w = 2; + vect_h = 2; + break; + + case AVS_P_FRAME_2X3: + vect_w = 2; + vect_h = 3; + break; + + default: + return -1; + } + + table = buf + (256 * vect_w * vect_h); + if (sub_type != AVS_I_FRAME) { + int map_size = ((318 / vect_w + 7) / 8) * (198 / vect_h); + init_get_bits(&change_map, table, map_size); + table += map_size; + } + + for (y=0; y<198; y+=vect_h) { + for (x=0; x<318; x+=vect_w) { + if (sub_type == AVS_I_FRAME || get_bits1(&change_map)) { + vect = &buf[*table++ * (vect_w * vect_h)]; + for (j=0; j<vect_w; j++) { + out[(y + 0) * stride + x + j] = vect[(0 * vect_w) + j]; + out[(y + 1) * stride + x + j] = vect[(1 * vect_w) + j]; + if (vect_h == 3) + out[(y + 2) * stride + x + j] = + vect[(2 * vect_w) + j]; + } + } + } + if (sub_type != AVS_I_FRAME) + align_get_bits(&change_map); + } + + *picture = *(AVFrame *) & avs->picture; + *data_size = sizeof(AVPicture); + + return buf_size; +} + +static int avs_decode_init(AVCodecContext * avctx) +{ + avctx->pix_fmt = PIX_FMT_PAL8; + return 0; +} + +AVCodec avs_decoder = { + "avs", + CODEC_TYPE_VIDEO, + CODEC_ID_AVS, + sizeof(avs_context_t), + avs_decode_init, + NULL, + NULL, + avs_decode_frame, + CODEC_CAP_DR1, +}; diff --git a/src/libffmpeg/libavcodec/cavs.c b/src/libffmpeg/libavcodec/cavs.c new file mode 100644 index 000000000..520089268 --- /dev/null +++ b/src/libffmpeg/libavcodec/cavs.c @@ -0,0 +1,1500 @@ +/* + * Chinese AVS video (AVS1-P2, JiZhun profile) decoder. + * Copyright (c) 2006 Stefan Gehrer <stefan.gehrer@gmx.de> + * + * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file cavs.c + * Chinese AVS video (AVS1-P2, JiZhun profile) decoder + * @author Stefan Gehrer <stefan.gehrer@gmx.de> + */ + +#include "avcodec.h" +#include "bitstream.h" +#include "golomb.h" +#include "mpegvideo.h" +#include "cavsdata.h" + +typedef struct { + MpegEncContext s; + Picture picture; ///< currently decoded frame + Picture DPB[2]; ///< reference frames + int dist[2]; ///< temporal distances from current frame to ref frames + int profile, level; + int aspect_ratio; + int mb_width, mb_height; + int pic_type; + int progressive; + int pic_structure; + int skip_mode_flag; ///< select between skip_count or one skip_flag per MB + int loop_filter_disable; + int alpha_offset, beta_offset; + int ref_flag; + int mbx, mby; ///< macroblock coordinates + int flags; ///< availability flags of neighbouring macroblocks + int stc; ///< last start code + uint8_t *cy, *cu, *cv; ///< current MB sample pointers + int left_qp; + uint8_t *top_qp; + + /** mv motion vector cache + 0: D3 B2 B3 C2 + 4: A1 X0 X1 - + 8: A3 X2 X3 - + + X are the vectors in the current macroblock (5,6,9,10) + A is the macroblock to the left (4,8) + B is the macroblock to the top (1,2) + C is the macroblock to the top-right (3) + D is the macroblock to the top-left (0) + + the same is repeated for backward motion vectors */ + vector_t mv[2*4*3]; + vector_t *top_mv[2]; + vector_t *col_mv; + + /** luma pred mode cache + 0: -- B2 B3 + 3: A1 X0 X1 + 6: A3 X2 X3 */ + int pred_mode_Y[3*3]; + int *top_pred_Y; + int l_stride, c_stride; + int luma_scan[4]; + int qp; + int qp_fixed; + int cbp; + ScanTable scantable; + + /** intra prediction is done with un-deblocked samples + they are saved here before deblocking the MB */ + uint8_t *top_border_y, *top_border_u, *top_border_v; + uint8_t left_border_y[26], left_border_u[10], left_border_v[10]; + uint8_t intern_border_y[26]; + uint8_t topleft_border_y, topleft_border_u, topleft_border_v; + + void (*intra_pred_l[8])(uint8_t *d,uint8_t *top,uint8_t *left,int stride); + void (*intra_pred_c[7])(uint8_t *d,uint8_t *top,uint8_t *left,int stride); + uint8_t *col_type_base; + uint8_t *col_type; + + /* scaling factors for MV prediction */ + int sym_factor; ///< for scaling in symmetrical B block + int direct_den[2]; ///< for scaling in direct B block + int scale_den[2]; ///< for scaling neighbouring MVs + + int got_keyframe; + DCTELEM *block; +} AVSContext; + +/***************************************************************************** + * + * in-loop deblocking filter + * + ****************************************************************************/ + +static inline int get_bs(vector_t *mvP, vector_t *mvQ, int b) { + if((mvP->ref == REF_INTRA) || (mvQ->ref == REF_INTRA)) + return 2; + if( (abs(mvP->x - mvQ->x) >= 4) || (abs(mvP->y - mvQ->y) >= 4) ) + return 1; + if(b){ + mvP += MV_BWD_OFFS; + mvQ += MV_BWD_OFFS; + if( (abs(mvP->x - mvQ->x) >= 4) || (abs(mvP->y - mvQ->y) >= 4) ) + return 1; + }else{ + if(mvP->ref != mvQ->ref) + return 1; + } + return 0; +} + +#define SET_PARAMS \ + alpha = alpha_tab[clip(qp_avg + h->alpha_offset,0,63)]; \ + beta = beta_tab[clip(qp_avg + h->beta_offset, 0,63)]; \ + tc = tc_tab[clip(qp_avg + h->alpha_offset,0,63)]; + +/** + * in-loop deblocking filter for a single macroblock + * + * boundary strength (bs) mapping: + * + * --4---5-- + * 0 2 | + * | 6 | 7 | + * 1 3 | + * --------- + * + */ +static void filter_mb(AVSContext *h, enum mb_t mb_type) { + DECLARE_ALIGNED_8(uint8_t, bs[8]); + int qp_avg, alpha, beta, tc; + int i; + + /* save un-deblocked lines */ + h->topleft_border_y = h->top_border_y[h->mbx*16+15]; + h->topleft_border_u = h->top_border_u[h->mbx*10+8]; + h->topleft_border_v = h->top_border_v[h->mbx*10+8]; + memcpy(&h->top_border_y[h->mbx*16], h->cy + 15* h->l_stride,16); + memcpy(&h->top_border_u[h->mbx*10+1], h->cu + 7* h->c_stride,8); + memcpy(&h->top_border_v[h->mbx*10+1], h->cv + 7* h->c_stride,8); + for(i=0;i<8;i++) { + h->left_border_y[i*2+1] = *(h->cy + 15 + (i*2+0)*h->l_stride); + h->left_border_y[i*2+2] = *(h->cy + 15 + (i*2+1)*h->l_stride); + h->left_border_u[i+1] = *(h->cu + 7 + i*h->c_stride); + h->left_border_v[i+1] = *(h->cv + 7 + i*h->c_stride); + } + if(!h->loop_filter_disable) { + /* determine bs */ + if(mb_type == I_8X8) + *((uint64_t *)bs) = 0x0202020202020202ULL; + else{ + *((uint64_t *)bs) = 0; + if(partition_flags[mb_type] & SPLITV){ + bs[2] = get_bs(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X1], mb_type > P_8X8); + bs[3] = get_bs(&h->mv[MV_FWD_X2], &h->mv[MV_FWD_X3], mb_type > P_8X8); + } + if(partition_flags[mb_type] & SPLITH){ + bs[6] = get_bs(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X2], mb_type > P_8X8); + bs[7] = get_bs(&h->mv[MV_FWD_X1], &h->mv[MV_FWD_X3], mb_type > P_8X8); + } + bs[0] = get_bs(&h->mv[MV_FWD_A1], &h->mv[MV_FWD_X0], mb_type > P_8X8); + bs[1] = get_bs(&h->mv[MV_FWD_A3], &h->mv[MV_FWD_X2], mb_type > P_8X8); + bs[4] = get_bs(&h->mv[MV_FWD_B2], &h->mv[MV_FWD_X0], mb_type > P_8X8); + bs[5] = get_bs(&h->mv[MV_FWD_B3], &h->mv[MV_FWD_X1], mb_type > P_8X8); + } + if( *((uint64_t *)bs) ) { + if(h->flags & A_AVAIL) { + qp_avg = (h->qp + h->left_qp + 1) >> 1; + SET_PARAMS; + h->s.dsp.cavs_filter_lv(h->cy,h->l_stride,alpha,beta,tc,bs[0],bs[1]); + h->s.dsp.cavs_filter_cv(h->cu,h->c_stride,alpha,beta,tc,bs[0],bs[1]); + h->s.dsp.cavs_filter_cv(h->cv,h->c_stride,alpha,beta,tc,bs[0],bs[1]); + } + qp_avg = h->qp; + SET_PARAMS; + h->s.dsp.cavs_filter_lv(h->cy + 8,h->l_stride,alpha,beta,tc,bs[2],bs[3]); + h->s.dsp.cavs_filter_lh(h->cy + 8*h->l_stride,h->l_stride,alpha,beta,tc, + bs[6],bs[7]); + + if(h->flags & B_AVAIL) { + qp_avg = (h->qp + h->top_qp[h->mbx] + 1) >> 1; + SET_PARAMS; + h->s.dsp.cavs_filter_lh(h->cy,h->l_stride,alpha,beta,tc,bs[4],bs[5]); + h->s.dsp.cavs_filter_ch(h->cu,h->c_stride,alpha,beta,tc,bs[4],bs[5]); + h->s.dsp.cavs_filter_ch(h->cv,h->c_stride,alpha,beta,tc,bs[4],bs[5]); + } + } + } + h->left_qp = h->qp; + h->top_qp[h->mbx] = h->qp; +} + +#undef SET_PARAMS + +/***************************************************************************** + * + * spatial intra prediction + * + ****************************************************************************/ + +static inline void load_intra_pred_luma(AVSContext *h, uint8_t *top, + uint8_t **left, int block) { + int i; + + switch(block) { + case 0: + *left = h->left_border_y; + h->left_border_y[0] = h->left_border_y[1]; + memset(&h->left_border_y[17],h->left_border_y[16],9); + memcpy(&top[1],&h->top_border_y[h->mbx*16],16); + top[17] = top[16]; + top[0] = top[1]; + if((h->flags & A_AVAIL) && (h->flags & B_AVAIL)) + h->left_border_y[0] = top[0] = h->topleft_border_y; + break; + case 1: + *left = h->intern_border_y; + for(i=0;i<8;i++) + h->intern_border_y[i+1] = *(h->cy + 7 + i*h->l_stride); + memset(&h->intern_border_y[9],h->intern_border_y[8],9); + h->intern_border_y[0] = h->intern_border_y[1]; + memcpy(&top[1],&h->top_border_y[h->mbx*16+8],8); + if(h->flags & C_AVAIL) + memcpy(&top[9],&h->top_border_y[(h->mbx + 1)*16],8); + else + memset(&top[9],top[8],9); + top[17] = top[16]; + top[0] = top[1]; + if(h->flags & B_AVAIL) + h->intern_border_y[0] = top[0] = h->top_border_y[h->mbx*16+7]; + break; + case 2: + *left = &h->left_border_y[8]; + memcpy(&top[1],h->cy + 7*h->l_stride,16); + top[17] = top[16]; + top[0] = top[1]; + if(h->flags & A_AVAIL) + top[0] = h->left_border_y[8]; + break; + case 3: + *left = &h->intern_border_y[8]; + for(i=0;i<8;i++) + h->intern_border_y[i+9] = *(h->cy + 7 + (i+8)*h->l_stride); + memset(&h->intern_border_y[17],h->intern_border_y[16],9); + memcpy(&top[0],h->cy + 7 + 7*h->l_stride,9); + memset(&top[9],top[8],9); + break; + } +} + +static void intra_pred_vert(uint8_t *d,uint8_t *top,uint8_t *left,int stride) { + int y; + uint64_t a = unaligned64(&top[1]); + for(y=0;y<8;y++) { + *((uint64_t *)(d+y*stride)) = a; + } +} + +static void intra_pred_horiz(uint8_t *d,uint8_t *top,uint8_t *left,int stride) { + int y; + uint64_t a; + for(y=0;y<8;y++) { + a = left[y+1] * 0x0101010101010101ULL; + *((uint64_t *)(d+y*stride)) = a; + } +} + +static void intra_pred_dc_128(uint8_t *d,uint8_t *top,uint8_t *left,int stride) { + int y; + uint64_t a = 0x8080808080808080ULL; + for(y=0;y<8;y++) + *((uint64_t *)(d+y*stride)) = a; +} + +static void intra_pred_plane(uint8_t *d,uint8_t *top,uint8_t *left,int stride) { + int x,y,ia; + int ih = 0; + int iv = 0; + uint8_t *cm = cropTbl + MAX_NEG_CROP; + + for(x=0; x<4; x++) { + ih += (x+1)*(top[5+x]-top[3-x]); + iv += (x+1)*(left[5+x]-left[3-x]); + } + ia = (top[8]+left[8])<<4; + ih = (17*ih+16)>>5; + iv = (17*iv+16)>>5; + for(y=0; y<8; y++) + for(x=0; x<8; x++) + d[y*stride+x] = cm[(ia+(x-3)*ih+(y-3)*iv+16)>>5]; +} + +#define LOWPASS(ARRAY,INDEX) \ + (( ARRAY[(INDEX)-1] + 2*ARRAY[(INDEX)] + ARRAY[(INDEX)+1] + 2) >> 2) + +static void intra_pred_lp(uint8_t *d,uint8_t *top,uint8_t *left,int stride) { + int x,y; + for(y=0; y<8; y++) + for(x=0; x<8; x++) + d[y*stride+x] = (LOWPASS(top,x+1) + LOWPASS(left,y+1)) >> 1; +} + +static void intra_pred_down_left(uint8_t *d,uint8_t *top,uint8_t *left,int stride) { + int x,y; + for(y=0; y<8; y++) + for(x=0; x<8; x++) + d[y*stride+x] = (LOWPASS(top,x+y+2) + LOWPASS(left,x+y+2)) >> 1; +} + +static void intra_pred_down_right(uint8_t *d,uint8_t *top,uint8_t *left,int stride) { + int x,y; + for(y=0; y<8; y++) + for(x=0; x<8; x++) + if(x==y) + d[y*stride+x] = (left[1]+2*top[0]+top[1]+2)>>2; + else if(x>y) + d[y*stride+x] = LOWPASS(top,x-y); + else + d[y*stride+x] = LOWPASS(left,y-x); +} + +static void intra_pred_lp_left(uint8_t *d,uint8_t *top,uint8_t *left,int stride) { + int x,y; + for(y=0; y<8; y++) + for(x=0; x<8; x++) + d[y*stride+x] = LOWPASS(left,y+1); +} + +static void intra_pred_lp_top(uint8_t *d,uint8_t *top,uint8_t *left,int stride) { + int x,y; + for(y=0; y<8; y++) + for(x=0; x<8; x++) + d[y*stride+x] = LOWPASS(top,x+1); +} + +#undef LOWPASS + +static inline void modify_pred(const int_fast8_t *mod_table, int *mode) { + *mode = mod_table[*mode]; + if(*mode < 0) { + av_log(NULL, AV_LOG_ERROR, "Illegal intra prediction mode\n"); + *mode = 0; + } +} + +/***************************************************************************** + * + * motion compensation + * + ****************************************************************************/ + +static inline void mc_dir_part(AVSContext *h,Picture *pic,int square, + int chroma_height,int delta,int list,uint8_t *dest_y, + uint8_t *dest_cb,uint8_t *dest_cr,int src_x_offset, + int src_y_offset,qpel_mc_func *qpix_op, + h264_chroma_mc_func chroma_op,vector_t *mv){ + MpegEncContext * const s = &h->s; + const int mx= mv->x + src_x_offset*8; + const int my= mv->y + src_y_offset*8; + const int luma_xy= (mx&3) + ((my&3)<<2); + uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->l_stride; + uint8_t * src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->c_stride; + uint8_t * src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->c_stride; + int extra_width= 0; //(s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16; + int extra_height= extra_width; + int emu=0; + const int full_mx= mx>>2; + const int full_my= my>>2; + const int pic_width = 16*h->mb_width; + const int pic_height = 16*h->mb_height; + + if(!pic->data[0]) + return; + if(mx&7) extra_width -= 3; + if(my&7) extra_height -= 3; + + if( full_mx < 0-extra_width + || full_my < 0-extra_height + || full_mx + 16/*FIXME*/ > pic_width + extra_width + || full_my + 16/*FIXME*/ > pic_height + extra_height){ + ff_emulated_edge_mc(s->edge_emu_buffer, src_y - 2 - 2*h->l_stride, h->l_stride, + 16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height); + src_y= s->edge_emu_buffer + 2 + 2*h->l_stride; + emu=1; + } + + qpix_op[luma_xy](dest_y, src_y, h->l_stride); //FIXME try variable height perhaps? + if(!square){ + qpix_op[luma_xy](dest_y + delta, src_y + delta, h->l_stride); + } + + if(emu){ + ff_emulated_edge_mc(s->edge_emu_buffer, src_cb, h->c_stride, + 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1); + src_cb= s->edge_emu_buffer; + } + chroma_op(dest_cb, src_cb, h->c_stride, chroma_height, mx&7, my&7); + + if(emu){ + ff_emulated_edge_mc(s->edge_emu_buffer, src_cr, h->c_stride, + 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1); + src_cr= s->edge_emu_buffer; + } + chroma_op(dest_cr, src_cr, h->c_stride, chroma_height, mx&7, my&7); +} + +static inline void mc_part_std(AVSContext *h,int square,int chroma_height,int delta, + uint8_t *dest_y,uint8_t *dest_cb,uint8_t *dest_cr, + int x_offset, int y_offset,qpel_mc_func *qpix_put, + h264_chroma_mc_func chroma_put,qpel_mc_func *qpix_avg, + h264_chroma_mc_func chroma_avg, vector_t *mv){ + qpel_mc_func *qpix_op= qpix_put; + h264_chroma_mc_func chroma_op= chroma_put; + + dest_y += 2*x_offset + 2*y_offset*h->l_stride; + dest_cb += x_offset + y_offset*h->c_stride; + dest_cr += x_offset + y_offset*h->c_stride; + x_offset += 8*h->mbx; + y_offset += 8*h->mby; + + if(mv->ref >= 0){ + Picture *ref= &h->DPB[mv->ref]; + mc_dir_part(h, ref, square, chroma_height, delta, 0, + dest_y, dest_cb, dest_cr, x_offset, y_offset, + qpix_op, chroma_op, mv); + + qpix_op= qpix_avg; + chroma_op= chroma_avg; + } + + if((mv+MV_BWD_OFFS)->ref >= 0){ + Picture *ref= &h->DPB[0]; + mc_dir_part(h, ref, square, chroma_height, delta, 1, + dest_y, dest_cb, dest_cr, x_offset, y_offset, + qpix_op, chroma_op, mv+MV_BWD_OFFS); + } +} + +static void inter_pred(AVSContext *h, enum mb_t mb_type) { + if(partition_flags[mb_type] == 0){ // 16x16 + mc_part_std(h, 1, 8, 0, h->cy, h->cu, h->cv, 0, 0, + h->s.dsp.put_cavs_qpel_pixels_tab[0], + h->s.dsp.put_h264_chroma_pixels_tab[0], + h->s.dsp.avg_cavs_qpel_pixels_tab[0], + h->s.dsp.avg_h264_chroma_pixels_tab[0],&h->mv[MV_FWD_X0]); + }else{ + mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 0, 0, + h->s.dsp.put_cavs_qpel_pixels_tab[1], + h->s.dsp.put_h264_chroma_pixels_tab[1], + h->s.dsp.avg_cavs_qpel_pixels_tab[1], + h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X0]); + mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 4, 0, + h->s.dsp.put_cavs_qpel_pixels_tab[1], + h->s.dsp.put_h264_chroma_pixels_tab[1], + h->s.dsp.avg_cavs_qpel_pixels_tab[1], + h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X1]); + mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 0, 4, + h->s.dsp.put_cavs_qpel_pixels_tab[1], + h->s.dsp.put_h264_chroma_pixels_tab[1], + h->s.dsp.avg_cavs_qpel_pixels_tab[1], + h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X2]); + mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 4, 4, + h->s.dsp.put_cavs_qpel_pixels_tab[1], + h->s.dsp.put_h264_chroma_pixels_tab[1], + h->s.dsp.avg_cavs_qpel_pixels_tab[1], + h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X3]); + } + /* set intra prediction modes to default values */ + h->pred_mode_Y[3] = h->pred_mode_Y[6] = INTRA_L_LP; + h->top_pred_Y[h->mbx*2+0] = h->top_pred_Y[h->mbx*2+1] = INTRA_L_LP; +} + +/***************************************************************************** + * + * motion vector prediction + * + ****************************************************************************/ + +static inline void set_mvs(vector_t *mv, enum block_t size) { + switch(size) { + case BLK_16X16: + mv[MV_STRIDE ] = mv[0]; + mv[MV_STRIDE+1] = mv[0]; + case BLK_16X8: + mv[1] = mv[0]; + break; + case BLK_8X16: + mv[MV_STRIDE] = mv[0]; + break; + } +} + +static inline void store_mvs(AVSContext *h) { + h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 0] = h->mv[MV_FWD_X0]; + h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 1] = h->mv[MV_FWD_X1]; + h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 2] = h->mv[MV_FWD_X2]; + h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 3] = h->mv[MV_FWD_X3]; +} + +static inline void scale_mv(AVSContext *h, int *d_x, int *d_y, vector_t *src, int distp) { + int den = h->scale_den[src->ref]; + + *d_x = (src->x*distp*den + 256 + (src->x>>31)) >> 9; + *d_y = (src->y*distp*den + 256 + (src->y>>31)) >> 9; +} + +static inline void mv_pred_median(AVSContext *h, vector_t *mvP, vector_t *mvA, vector_t *mvB, vector_t *mvC) { + int ax, ay, bx, by, cx, cy; + int len_ab, len_bc, len_ca, len_mid; + + /* scale candidates according to their temporal span */ + scale_mv(h, &ax, &ay, mvA, mvP->dist); + scale_mv(h, &bx, &by, mvB, mvP->dist); + scale_mv(h, &cx, &cy, mvC, mvP->dist); + /* find the geometrical median of the three candidates */ + len_ab = abs(ax - bx) + abs(ay - by); + len_bc = abs(bx - cx) + abs(by - cy); + len_ca = abs(cx - ax) + abs(cy - ay); + len_mid = mid_pred(len_ab, len_bc, len_ca); + if(len_mid == len_ab) { + mvP->x = cx; + mvP->y = cy; + } else if(len_mid == len_bc) { + mvP->x = ax; + mvP->y = ay; + } else { + mvP->x = bx; + mvP->y = by; + } +} + +static inline void mv_pred_direct(AVSContext *h, vector_t *pmv_fw, + vector_t *col_mv) { + vector_t *pmv_bw = pmv_fw + MV_BWD_OFFS; + int den = h->direct_den[col_mv->ref]; + int m = col_mv->x >> 31; + + pmv_fw->dist = h->dist[1]; + pmv_bw->dist = h->dist[0]; + pmv_fw->ref = 1; + pmv_bw->ref = 0; + /* scale the co-located motion vector according to its temporal span */ + pmv_fw->x = (((den+(den*col_mv->x*pmv_fw->dist^m)-m-1)>>14)^m)-m; + pmv_bw->x = m-(((den+(den*col_mv->x*pmv_bw->dist^m)-m-1)>>14)^m); + m = col_mv->y >> 31; + pmv_fw->y = (((den+(den*col_mv->y*pmv_fw->dist^m)-m-1)>>14)^m)-m; + pmv_bw->y = m-(((den+(den*col_mv->y*pmv_bw->dist^m)-m-1)>>14)^m); +} + +static inline void mv_pred_sym(AVSContext *h, vector_t *src, enum block_t size) { + vector_t *dst = src + MV_BWD_OFFS; + + /* backward mv is the scaled and negated forward mv */ + dst->x = -((src->x * h->sym_factor + 256) >> 9); + dst->y = -((src->y * h->sym_factor + 256) >> 9); + dst->ref = 0; + dst->dist = h->dist[0]; + set_mvs(dst, size); +} + +static void mv_pred(AVSContext *h, enum mv_loc_t nP, enum mv_loc_t nC, + enum mv_pred_t mode, enum block_t size, int ref) { + vector_t *mvP = &h->mv[nP]; + vector_t *mvA = &h->mv[nP-1]; + vector_t *mvB = &h->mv[nP-4]; + vector_t *mvC = &h->mv[nC]; + const vector_t *mvP2 = NULL; + + mvP->ref = ref; + mvP->dist = h->dist[mvP->ref]; + if(mvC->ref == NOT_AVAIL) + mvC = &h->mv[nP-5]; // set to top-left (mvD) + if((mode == MV_PRED_PSKIP) && + ((mvA->ref == NOT_AVAIL) || (mvB->ref == NOT_AVAIL) || + ((mvA->x | mvA->y | mvA->ref) == 0) || + ((mvB->x | mvB->y | mvB->ref) == 0) )) { + mvP2 = &un_mv; + /* if there is only one suitable candidate, take it */ + } else if((mvA->ref >= 0) && (mvB->ref < 0) && (mvC->ref < 0)) { + mvP2= mvA; + } else if((mvA->ref < 0) && (mvB->ref >= 0) && (mvC->ref < 0)) { + mvP2= mvB; + } else if((mvA->ref < 0) && (mvB->ref < 0) && (mvC->ref >= 0)) { + mvP2= mvC; + } else if(mode == MV_PRED_LEFT && mvA->ref == ref){ + mvP2= mvA; + } else if(mode == MV_PRED_TOP && mvB->ref == ref){ + mvP2= mvB; + } else if(mode == MV_PRED_TOPRIGHT && mvC->ref == ref){ + mvP2= mvC; + } + if(mvP2){ + mvP->x = mvP2->x; + mvP->y = mvP2->y; + }else + mv_pred_median(h, mvP, mvA, mvB, mvC); + + if(mode < MV_PRED_PSKIP) { + mvP->x += get_se_golomb(&h->s.gb); + mvP->y += get_se_golomb(&h->s.gb); + } + set_mvs(mvP,size); +} + +/***************************************************************************** + * + * residual data decoding + * + ****************************************************************************/ + +/** kth-order exponential golomb code */ +static inline int get_ue_code(GetBitContext *gb, int order) { + if(order) { + int ret = get_ue_golomb(gb) << order; + return ret + get_bits(gb,order); + } + return get_ue_golomb(gb); +} + +/** + * decode coefficients from one 8x8 block, dequantize, inverse transform + * and add them to sample block + * @param r pointer to 2D VLC table + * @param esc_golomb_order escape codes are k-golomb with this order k + * @param qp quantizer + * @param dst location of sample block + * @param stride line stride in frame buffer + */ +static int decode_residual_block(AVSContext *h, GetBitContext *gb, + const residual_vlc_t *r, int esc_golomb_order, + int qp, uint8_t *dst, int stride) { + int i,pos = -1; + int level_code, esc_code, level, run, mask; + int level_buf[64]; + int run_buf[64]; + int dqm = dequant_mul[qp]; + int dqs = dequant_shift[qp]; + int dqa = 1 << (dqs - 1); + const uint8_t *scantab = h->scantable.permutated; + DCTELEM *block = h->block; + + for(i=0;i<65;i++) { + level_code = get_ue_code(gb,r->golomb_order); + if(level_code >= ESCAPE_CODE) { + run = ((level_code - ESCAPE_CODE) >> 1) + 1; + esc_code = get_ue_code(gb,esc_golomb_order); + level = esc_code + (run > r->max_run ? 1 : r->level_add[run]); + while(level > r->inc_limit) + r++; + mask = -(level_code & 1); + level = (level^mask) - mask; + } else { + level = r->rltab[level_code][0]; + if(!level) //end of block signal + break; + run = r->rltab[level_code][1]; + r += r->rltab[level_code][2]; + } + level_buf[i] = level; + run_buf[i] = run; + } + /* inverse scan and dequantization */ + while(--i >= 0){ + pos += run_buf[i]; + if(pos > 63) { + av_log(h->s.avctx, AV_LOG_ERROR, + "position out of block bounds at pic %d MB(%d,%d)\n", + h->picture.poc, h->mbx, h->mby); + return -1; + } + block[scantab[pos]] = (level_buf[i]*dqm + dqa) >> dqs; + } + h->s.dsp.cavs_idct8_add(dst,block,stride); + return 0; +} + + +static inline void decode_residual_chroma(AVSContext *h) { + if(h->cbp & (1<<4)) + decode_residual_block(h,&h->s.gb,chroma_2dvlc,0, chroma_qp[h->qp], + h->cu,h->c_stride); + if(h->cbp & (1<<5)) + decode_residual_block(h,&h->s.gb,chroma_2dvlc,0, chroma_qp[h->qp], + h->cv,h->c_stride); +} + +static inline int decode_residual_inter(AVSContext *h) { + int block; + + /* get coded block pattern */ + int cbp= get_ue_golomb(&h->s.gb); + if(cbp > 63){ + av_log(h->s.avctx, AV_LOG_ERROR, "illegal inter cbp\n"); + return -1; + } + h->cbp = cbp_tab[cbp][1]; + + /* get quantizer */ + if(h->cbp && !h->qp_fixed) + h->qp = (h->qp + get_se_golomb(&h->s.gb)) & 63; + for(block=0;block<4;block++) + if(h->cbp & (1<<block)) + decode_residual_block(h,&h->s.gb,inter_2dvlc,0,h->qp, + h->cy + h->luma_scan[block], h->l_stride); + decode_residual_chroma(h); + + return 0; +} + +/***************************************************************************** + * + * macroblock level + * + ****************************************************************************/ + +/** + * initialise predictors for motion vectors and intra prediction + */ +static inline void init_mb(AVSContext *h) { + int i; + + /* copy predictors from top line (MB B and C) into cache */ + for(i=0;i<3;i++) { + h->mv[MV_FWD_B2+i] = h->top_mv[0][h->mbx*2+i]; + h->mv[MV_BWD_B2+i] = h->top_mv[1][h->mbx*2+i]; + } + h->pred_mode_Y[1] = h->top_pred_Y[h->mbx*2+0]; + h->pred_mode_Y[2] = h->top_pred_Y[h->mbx*2+1]; + /* clear top predictors if MB B is not available */ + if(!(h->flags & B_AVAIL)) { + h->mv[MV_FWD_B2] = un_mv; + h->mv[MV_FWD_B3] = un_mv; + h->mv[MV_BWD_B2] = un_mv; + h->mv[MV_BWD_B3] = un_mv; + h->pred_mode_Y[1] = h->pred_mode_Y[2] = NOT_AVAIL; + h->flags &= ~(C_AVAIL|D_AVAIL); + } else if(h->mbx) { + h->flags |= D_AVAIL; + } + if(h->mbx == h->mb_width-1) //MB C not available + h->flags &= ~C_AVAIL; + /* clear top-right predictors if MB C is not available */ + if(!(h->flags & C_AVAIL)) { + h->mv[MV_FWD_C2] = un_mv; + h->mv[MV_BWD_C2] = un_mv; + } + /* clear top-left predictors if MB D is not available */ + if(!(h->flags & D_AVAIL)) { + h->mv[MV_FWD_D3] = un_mv; + h->mv[MV_BWD_D3] = un_mv; + } + /* set pointer for co-located macroblock type */ + h->col_type = &h->col_type_base[h->mby*h->mb_width + h->mbx]; +} + +static inline void check_for_slice(AVSContext *h); + +/** + * save predictors for later macroblocks and increase + * macroblock address + * @returns 0 if end of frame is reached, 1 otherwise + */ +static inline int next_mb(AVSContext *h) { + int i; + + h->flags |= A_AVAIL; + h->cy += 16; + h->cu += 8; + h->cv += 8; + /* copy mvs as predictors to the left */ + for(i=0;i<=20;i+=4) + h->mv[i] = h->mv[i+2]; + /* copy bottom mvs from cache to top line */ + h->top_mv[0][h->mbx*2+0] = h->mv[MV_FWD_X2]; + h->top_mv[0][h->mbx*2+1] = h->mv[MV_FWD_X3]; + h->top_mv[1][h->mbx*2+0] = h->mv[MV_BWD_X2]; + h->top_mv[1][h->mbx*2+1] = h->mv[MV_BWD_X3]; + /* next MB address */ + h->mbx++; + if(h->mbx == h->mb_width) { //new mb line + h->flags = B_AVAIL|C_AVAIL; + /* clear left pred_modes */ + h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL; + /* clear left mv predictors */ + for(i=0;i<=20;i+=4) + h->mv[i] = un_mv; + h->mbx = 0; + h->mby++; + /* re-calculate sample pointers */ + h->cy = h->picture.data[0] + h->mby*16*h->l_stride; + h->cu = h->picture.data[1] + h->mby*8*h->c_stride; + h->cv = h->picture.data[2] + h->mby*8*h->c_stride; + if(h->mby == h->mb_height) { //frame end + return 0; + } else { + //check_for_slice(h); + } + } + return 1; +} + +static int decode_mb_i(AVSContext *h, int cbp_code) { + GetBitContext *gb = &h->s.gb; + int block, pred_mode_uv; + uint8_t top[18]; + uint8_t *left = NULL; + uint8_t *d; + + init_mb(h); + + /* get intra prediction modes from stream */ + for(block=0;block<4;block++) { + int nA,nB,predpred; + int pos = scan3x3[block]; + + nA = h->pred_mode_Y[pos-1]; + nB = h->pred_mode_Y[pos-3]; + predpred = FFMIN(nA,nB); + if(predpred == NOT_AVAIL) // if either is not available + predpred = INTRA_L_LP; + if(!get_bits1(gb)){ + int rem_mode= get_bits(gb, 2); + predpred = rem_mode + (rem_mode >= predpred); + } + h->pred_mode_Y[pos] = predpred; + } + pred_mode_uv = get_ue_golomb(gb); + if(pred_mode_uv > 6) { + av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n"); + return -1; + } + + /* save pred modes before they get modified */ + h->pred_mode_Y[3] = h->pred_mode_Y[5]; + h->pred_mode_Y[6] = h->pred_mode_Y[8]; + h->top_pred_Y[h->mbx*2+0] = h->pred_mode_Y[7]; + h->top_pred_Y[h->mbx*2+1] = h->pred_mode_Y[8]; + + /* modify pred modes according to availability of neighbour samples */ + if(!(h->flags & A_AVAIL)) { + modify_pred(left_modifier_l, &h->pred_mode_Y[4] ); + modify_pred(left_modifier_l, &h->pred_mode_Y[7] ); + modify_pred(left_modifier_c, &pred_mode_uv ); + } + if(!(h->flags & B_AVAIL)) { + modify_pred(top_modifier_l, &h->pred_mode_Y[4] ); + modify_pred(top_modifier_l, &h->pred_mode_Y[5] ); + modify_pred(top_modifier_c, &pred_mode_uv ); + } + + /* get coded block pattern */ + if(h->pic_type == FF_I_TYPE) + cbp_code = get_ue_golomb(gb); + if(cbp_code > 63){ + av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra cbp\n"); + return -1; + } + h->cbp = cbp_tab[cbp_code][0]; + if(h->cbp && !h->qp_fixed) + h->qp = (h->qp + get_se_golomb(gb)) & 63; //qp_delta + + /* luma intra prediction interleaved with residual decode/transform/add */ + for(block=0;block<4;block++) { + d = h->cy + h->luma_scan[block]; + load_intra_pred_luma(h, top, &left, block); + h->intra_pred_l[h->pred_mode_Y[scan3x3[block]]] + (d, top, left, h->l_stride); + if(h->cbp & (1<<block)) + decode_residual_block(h,gb,intra_2dvlc,1,h->qp,d,h->l_stride); + } + + /* chroma intra prediction */ + /* extend borders by one pixel */ + h->left_border_u[9] = h->left_border_u[8]; + h->left_border_v[9] = h->left_border_v[8]; + h->top_border_u[h->mbx*10+9] = h->top_border_u[h->mbx*10+8]; + h->top_border_v[h->mbx*10+9] = h->top_border_v[h->mbx*10+8]; + if(h->mbx && h->mby) { + h->top_border_u[h->mbx*10] = h->left_border_u[0] = h->topleft_border_u; + h->top_border_v[h->mbx*10] = h->left_border_v[0] = h->topleft_border_v; + } else { + h->left_border_u[0] = h->left_border_u[1]; + h->left_border_v[0] = h->left_border_v[1]; + h->top_border_u[h->mbx*10] = h->top_border_u[h->mbx*10+1]; + h->top_border_v[h->mbx*10] = h->top_border_v[h->mbx*10+1]; + } + h->intra_pred_c[pred_mode_uv](h->cu, &h->top_border_u[h->mbx*10], + h->left_border_u, h->c_stride); + h->intra_pred_c[pred_mode_uv](h->cv, &h->top_border_v[h->mbx*10], + h->left_border_v, h->c_stride); + + decode_residual_chroma(h); + filter_mb(h,I_8X8); + + /* mark motion vectors as intra */ + h->mv[MV_FWD_X0] = intra_mv; + set_mvs(&h->mv[MV_FWD_X0], BLK_16X16); + h->mv[MV_BWD_X0] = intra_mv; + set_mvs(&h->mv[MV_BWD_X0], BLK_16X16); + if(h->pic_type != FF_B_TYPE) + *h->col_type = I_8X8; + + return 0; +} + +static void decode_mb_p(AVSContext *h, enum mb_t mb_type) { + GetBitContext *gb = &h->s.gb; + int ref[4]; + + init_mb(h); + switch(mb_type) { + case P_SKIP: + mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_PSKIP, BLK_16X16, 0); + break; + case P_16X16: + ref[0] = h->ref_flag ? 0 : get_bits1(gb); + mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16,ref[0]); + break; + case P_16X8: + ref[0] = h->ref_flag ? 0 : get_bits1(gb); + ref[2] = h->ref_flag ? 0 : get_bits1(gb); + mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, ref[0]); + mv_pred(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, ref[2]); + break; + case P_8X16: + ref[0] = h->ref_flag ? 0 : get_bits1(gb); + ref[1] = h->ref_flag ? 0 : get_bits1(gb); + mv_pred(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, ref[0]); + mv_pred(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, ref[1]); + break; + case P_8X8: + ref[0] = h->ref_flag ? 0 : get_bits1(gb); + ref[1] = h->ref_flag ? 0 : get_bits1(gb); + ref[2] = h->ref_flag ? 0 : get_bits1(gb); + ref[3] = h->ref_flag ? 0 : get_bits1(gb); + mv_pred(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_MEDIAN, BLK_8X8, ref[0]); + mv_pred(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_MEDIAN, BLK_8X8, ref[1]); + mv_pred(h, MV_FWD_X2, MV_FWD_X1, MV_PRED_MEDIAN, BLK_8X8, ref[2]); + mv_pred(h, MV_FWD_X3, MV_FWD_X0, MV_PRED_MEDIAN, BLK_8X8, ref[3]); + } + inter_pred(h, mb_type); + store_mvs(h); + if(mb_type != P_SKIP) + decode_residual_inter(h); + filter_mb(h,mb_type); + *h->col_type = mb_type; +} + +static void decode_mb_b(AVSContext *h, enum mb_t mb_type) { + int block; + enum sub_mb_t sub_type[4]; + int flags; + + init_mb(h); + + /* reset all MVs */ + h->mv[MV_FWD_X0] = dir_mv; + set_mvs(&h->mv[MV_FWD_X0], BLK_16X16); + h->mv[MV_BWD_X0] = dir_mv; + set_mvs(&h->mv[MV_BWD_X0], BLK_16X16); + switch(mb_type) { + case B_SKIP: + case B_DIRECT: + if(!(*h->col_type)) { + /* intra MB at co-location, do in-plane prediction */ + mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_BSKIP, BLK_16X16, 1); + mv_pred(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_BSKIP, BLK_16X16, 0); + } else + /* direct prediction from co-located P MB, block-wise */ + for(block=0;block<4;block++) + mv_pred_direct(h,&h->mv[mv_scan[block]], + &h->col_mv[(h->mby*h->mb_width+h->mbx)*4 + block]); + break; + case B_FWD_16X16: + mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1); + break; + case B_SYM_16X16: + mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1); + mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X16); + break; + case B_BWD_16X16: + mv_pred(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_MEDIAN, BLK_16X16, 0); + break; + case B_8X8: + for(block=0;block<4;block++) + sub_type[block] = get_bits(&h->s.gb,2); + for(block=0;block<4;block++) { + switch(sub_type[block]) { + case B_SUB_DIRECT: + if(!(*h->col_type)) { + /* intra MB at co-location, do in-plane prediction */ + mv_pred(h, mv_scan[block], mv_scan[block]-3, + MV_PRED_BSKIP, BLK_8X8, 1); + mv_pred(h, mv_scan[block]+MV_BWD_OFFS, + mv_scan[block]-3+MV_BWD_OFFS, + MV_PRED_BSKIP, BLK_8X8, 0); + } else + mv_pred_direct(h,&h->mv[mv_scan[block]], + &h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + block]); + break; + case B_SUB_FWD: + mv_pred(h, mv_scan[block], mv_scan[block]-3, + MV_PRED_MEDIAN, BLK_8X8, 1); + break; + case B_SUB_SYM: + mv_pred(h, mv_scan[block], mv_scan[block]-3, + MV_PRED_MEDIAN, BLK_8X8, 1); + mv_pred_sym(h, &h->mv[mv_scan[block]], BLK_8X8); + break; + } + } + for(block=0;block<4;block++) { + if(sub_type[block] == B_SUB_BWD) + mv_pred(h, mv_scan[block]+MV_BWD_OFFS, + mv_scan[block]+MV_BWD_OFFS-3, + MV_PRED_MEDIAN, BLK_8X8, 0); + } + break; + default: + assert((mb_type > B_SYM_16X16) && (mb_type < B_8X8)); + flags = partition_flags[mb_type]; + if(mb_type & 1) { /* 16x8 macroblock types */ + if(flags & FWD0) + mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, 1); + if(flags & SYM0) + mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8); + if(flags & FWD1) + mv_pred(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1); + if(flags & SYM1) + mv_pred_sym(h, &h->mv[MV_FWD_X2], BLK_16X8); + if(flags & BWD0) + mv_pred(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_TOP, BLK_16X8, 0); + if(flags & BWD1) + mv_pred(h, MV_BWD_X2, MV_BWD_A1, MV_PRED_LEFT, BLK_16X8, 0); + } else { /* 8x16 macroblock types */ + if(flags & FWD0) + mv_pred(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1); + if(flags & SYM0) + mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16); + if(flags & FWD1) + mv_pred(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, 1); + if(flags & SYM1) + mv_pred_sym(h, &h->mv[MV_FWD_X1], BLK_8X16); + if(flags & BWD0) + mv_pred(h, MV_BWD_X0, MV_BWD_B3, MV_PRED_LEFT, BLK_8X16, 0); + if(flags & BWD1) + mv_pred(h, MV_BWD_X1, MV_BWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, 0); + } + } + inter_pred(h, mb_type); + if(mb_type != B_SKIP) + decode_residual_inter(h); + filter_mb(h,mb_type); +} + +/***************************************************************************** + * + * slice level + * + ****************************************************************************/ + +static inline int decode_slice_header(AVSContext *h, GetBitContext *gb) { + if(h->stc > 0xAF) + av_log(h->s.avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc); + h->mby = h->stc; + if((h->mby == 0) && (!h->qp_fixed)){ + h->qp_fixed = get_bits1(gb); + h->qp = get_bits(gb,6); + } + /* inter frame or second slice can have weighting params */ + if((h->pic_type != FF_I_TYPE) || (!h->pic_structure && h->mby >= h->mb_width/2)) + if(get_bits1(gb)) { //slice_weighting_flag + av_log(h->s.avctx, AV_LOG_ERROR, + "weighted prediction not yet supported\n"); + } + return 0; +} + +static inline void check_for_slice(AVSContext *h) { + GetBitContext *gb = &h->s.gb; + int align; + align = (-get_bits_count(gb)) & 7; + if((show_bits_long(gb,24+align) & 0xFFFFFF) == 0x000001) { + get_bits_long(gb,24+align); + h->stc = get_bits(gb,8); + decode_slice_header(h,gb); + } +} + +/***************************************************************************** + * + * frame level + * + ****************************************************************************/ + +static void init_pic(AVSContext *h) { + int i; + + /* clear some predictors */ + for(i=0;i<=20;i+=4) + h->mv[i] = un_mv; + h->mv[MV_BWD_X0] = dir_mv; + set_mvs(&h->mv[MV_BWD_X0], BLK_16X16); + h->mv[MV_FWD_X0] = dir_mv; + set_mvs(&h->mv[MV_FWD_X0], BLK_16X16); + h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL; + h->cy = h->picture.data[0]; + h->cu = h->picture.data[1]; + h->cv = h->picture.data[2]; + h->l_stride = h->picture.linesize[0]; + h->c_stride = h->picture.linesize[1]; + h->luma_scan[2] = 8*h->l_stride; + h->luma_scan[3] = 8*h->l_stride+8; + h->mbx = h->mby = 0; + h->flags = 0; +} + +static int decode_pic(AVSContext *h) { + MpegEncContext *s = &h->s; + int skip_count; + enum mb_t mb_type; + + if (!s->context_initialized) { + s->avctx->idct_algo = FF_IDCT_CAVS; + if (MPV_common_init(s) < 0) + return -1; + ff_init_scantable(s->dsp.idct_permutation,&h->scantable,ff_zigzag_direct); + } + get_bits(&s->gb,16);//bbv_dwlay + if(h->stc == PIC_PB_START_CODE) { + h->pic_type = get_bits(&s->gb,2) + FF_I_TYPE; + if(h->pic_type > FF_B_TYPE) { + av_log(s->avctx, AV_LOG_ERROR, "illegal picture type\n"); + return -1; + } + /* make sure we have the reference frames we need */ + if(!h->DPB[0].data[0] || + (!h->DPB[1].data[0] && h->pic_type == FF_B_TYPE)) + return -1; + } else { + h->pic_type = FF_I_TYPE; + if(get_bits1(&s->gb)) + get_bits(&s->gb,16);//time_code + } + /* release last B frame */ + if(h->picture.data[0]) + s->avctx->release_buffer(s->avctx, (AVFrame *)&h->picture); + + s->avctx->get_buffer(s->avctx, (AVFrame *)&h->picture); + init_pic(h); + h->picture.poc = get_bits(&s->gb,8)*2; + + /* get temporal distances and MV scaling factors */ + if(h->pic_type != FF_B_TYPE) { + h->dist[0] = (h->picture.poc - h->DPB[0].poc + 512) % 512; + } else { + h->dist[0] = (h->DPB[0].poc - h->picture.poc + 512) % 512; + } + h->dist[1] = (h->picture.poc - h->DPB[1].poc + 512) % 512; + h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0; + h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0; + if(h->pic_type == FF_B_TYPE) { + h->sym_factor = h->dist[0]*h->scale_den[1]; + } else { + h->direct_den[0] = h->dist[0] ? 16384/h->dist[0] : 0; + h->direct_den[1] = h->dist[1] ? 16384/h->dist[1] : 0; + } + + if(s->low_delay) + get_ue_golomb(&s->gb); //bbv_check_times + h->progressive = get_bits1(&s->gb); + if(h->progressive) + h->pic_structure = 1; + else if(!(h->pic_structure = get_bits1(&s->gb) && (h->stc == PIC_PB_START_CODE)) ) + get_bits1(&s->gb); //advanced_pred_mode_disable + skip_bits1(&s->gb); //top_field_first + skip_bits1(&s->gb); //repeat_first_field + h->qp_fixed = get_bits1(&s->gb); + h->qp = get_bits(&s->gb,6); + if(h->pic_type == FF_I_TYPE) { + if(!h->progressive && !h->pic_structure) + skip_bits1(&s->gb);//what is this? + skip_bits(&s->gb,4); //reserved bits + } else { + if(!(h->pic_type == FF_B_TYPE && h->pic_structure == 1)) + h->ref_flag = get_bits1(&s->gb); + skip_bits(&s->gb,4); //reserved bits + h->skip_mode_flag = get_bits1(&s->gb); + } + h->loop_filter_disable = get_bits1(&s->gb); + if(!h->loop_filter_disable && get_bits1(&s->gb)) { + h->alpha_offset = get_se_golomb(&s->gb); + h->beta_offset = get_se_golomb(&s->gb); + } else { + h->alpha_offset = h->beta_offset = 0; + } + check_for_slice(h); + if(h->pic_type == FF_I_TYPE) { + do { + decode_mb_i(h, 0); + } while(next_mb(h)); + } else if(h->pic_type == FF_P_TYPE) { + do { + if(h->skip_mode_flag) { + skip_count = get_ue_golomb(&s->gb); + while(skip_count--) { + decode_mb_p(h,P_SKIP); + if(!next_mb(h)) + goto done; + } + mb_type = get_ue_golomb(&s->gb) + P_16X16; + } else + mb_type = get_ue_golomb(&s->gb) + P_SKIP; + if(mb_type > P_8X8) { + decode_mb_i(h, mb_type - P_8X8 - 1); + } else + decode_mb_p(h,mb_type); + } while(next_mb(h)); + } else { /* FF_B_TYPE */ + do { + if(h->skip_mode_flag) { + skip_count = get_ue_golomb(&s->gb); + while(skip_count--) { + decode_mb_b(h,B_SKIP); + if(!next_mb(h)) + goto done; + } + mb_type = get_ue_golomb(&s->gb) + B_DIRECT; + } else + mb_type = get_ue_golomb(&s->gb) + B_SKIP; + if(mb_type > B_8X8) { + decode_mb_i(h, mb_type - B_8X8 - 1); + } else + decode_mb_b(h,mb_type); + } while(next_mb(h)); + } + done: + if(h->pic_type != FF_B_TYPE) { + if(h->DPB[1].data[0]) + s->avctx->release_buffer(s->avctx, (AVFrame *)&h->DPB[1]); + memcpy(&h->DPB[1], &h->DPB[0], sizeof(Picture)); + memcpy(&h->DPB[0], &h->picture, sizeof(Picture)); + memset(&h->picture,0,sizeof(Picture)); + } + return 0; +} + +/***************************************************************************** + * + * headers and interface + * + ****************************************************************************/ + +/** + * some predictions require data from the top-neighbouring macroblock. + * this data has to be stored for one complete row of macroblocks + * and this storage space is allocated here + */ +static void init_top_lines(AVSContext *h) { + /* alloc top line of predictors */ + h->top_qp = av_malloc( h->mb_width); + h->top_mv[0] = av_malloc((h->mb_width*2+1)*sizeof(vector_t)); + h->top_mv[1] = av_malloc((h->mb_width*2+1)*sizeof(vector_t)); + h->top_pred_Y = av_malloc( h->mb_width*2*sizeof(*h->top_pred_Y)); + h->top_border_y = av_malloc((h->mb_width+1)*16); + h->top_border_u = av_malloc((h->mb_width)*10); + h->top_border_v = av_malloc((h->mb_width)*10); + + /* alloc space for co-located MVs and types */ + h->col_mv = av_malloc( h->mb_width*h->mb_height*4*sizeof(vector_t)); + h->col_type_base = av_malloc(h->mb_width*h->mb_height); + h->block = av_mallocz(64*sizeof(DCTELEM)); +} + +static int decode_seq_header(AVSContext *h) { + MpegEncContext *s = &h->s; + extern const AVRational ff_frame_rate_tab[]; + int frame_rate_code; + + h->profile = get_bits(&s->gb,8); + h->level = get_bits(&s->gb,8); + skip_bits1(&s->gb); //progressive sequence + s->width = get_bits(&s->gb,14); + s->height = get_bits(&s->gb,14); + skip_bits(&s->gb,2); //chroma format + skip_bits(&s->gb,3); //sample_precision + h->aspect_ratio = get_bits(&s->gb,4); + frame_rate_code = get_bits(&s->gb,4); + skip_bits(&s->gb,18);//bit_rate_lower + skip_bits1(&s->gb); //marker_bit + skip_bits(&s->gb,12);//bit_rate_upper + s->low_delay = get_bits1(&s->gb); + h->mb_width = (s->width + 15) >> 4; + h->mb_height = (s->height + 15) >> 4; + h->s.avctx->time_base.den = ff_frame_rate_tab[frame_rate_code].num; + h->s.avctx->time_base.num = ff_frame_rate_tab[frame_rate_code].den; + h->s.avctx->width = s->width; + h->s.avctx->height = s->height; + if(!h->top_qp) + init_top_lines(h); + return 0; +} + +/** + * finds the end of the current frame in the bitstream. + * @return the position of the first byte of the next frame, or -1 + */ +int ff_cavs_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size) { + int pic_found, i; + uint32_t state; + + pic_found= pc->frame_start_found; + state= pc->state; + + i=0; + if(!pic_found){ + for(i=0; i<buf_size; i++){ + state= (state<<8) | buf[i]; + if(state == PIC_I_START_CODE || state == PIC_PB_START_CODE){ + i++; + pic_found=1; + break; + } + } + } + + if(pic_found){ + /* EOF considered as end of frame */ + if (buf_size == 0) + return 0; + for(; i<buf_size; i++){ + state= (state<<8) | buf[i]; + if((state&0xFFFFFF00) == 0x100){ + if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){ + pc->frame_start_found=0; + pc->state=-1; + return i-3; + } + } + } + } + pc->frame_start_found= pic_found; + pc->state= state; + return END_NOT_FOUND; +} + +void ff_cavs_flush(AVCodecContext * avctx) { + AVSContext *h = avctx->priv_data; + h->got_keyframe = 0; +} + +static int cavs_decode_frame(AVCodecContext * avctx,void *data, int *data_size, + uint8_t * buf, int buf_size) { + AVSContext *h = avctx->priv_data; + MpegEncContext *s = &h->s; + int input_size; + const uint8_t *buf_end; + const uint8_t *buf_ptr; + AVFrame *picture = data; + uint32_t stc; + + s->avctx = avctx; + + if (buf_size == 0) { + if(!s->low_delay && h->DPB[0].data[0]) { + *data_size = sizeof(AVPicture); + *picture = *(AVFrame *) &h->DPB[0]; + } + return 0; + } + + buf_ptr = buf; + buf_end = buf + buf_size; + for(;;) { + buf_ptr = ff_find_start_code(buf_ptr,buf_end, &stc); + if(stc & 0xFFFFFE00) + return FFMAX(0, buf_ptr - buf - s->parse_context.last_index); + input_size = (buf_end - buf_ptr)*8; + switch(stc) { + case SEQ_START_CODE: + init_get_bits(&s->gb, buf_ptr, input_size); + decode_seq_header(h); + break; + case PIC_I_START_CODE: + if(!h->got_keyframe) { + if(h->DPB[0].data[0]) + avctx->release_buffer(avctx, (AVFrame *)&h->DPB[0]); + if(h->DPB[1].data[0]) + avctx->release_buffer(avctx, (AVFrame *)&h->DPB[1]); + h->got_keyframe = 1; + } + case PIC_PB_START_CODE: + *data_size = 0; + if(!h->got_keyframe) + break; + init_get_bits(&s->gb, buf_ptr, input_size); + h->stc = stc; + if(decode_pic(h)) + break; + *data_size = sizeof(AVPicture); + if(h->pic_type != FF_B_TYPE) { + if(h->DPB[1].data[0]) { + *picture = *(AVFrame *) &h->DPB[1]; + } else { + *data_size = 0; + } + } else + *picture = *(AVFrame *) &h->picture; + break; + case EXT_START_CODE: + //mpeg_decode_extension(avctx,buf_ptr, input_size); + break; + case USER_START_CODE: + //mpeg_decode_user_data(avctx,buf_ptr, input_size); + break; + default: + if (stc >= SLICE_MIN_START_CODE && + stc <= SLICE_MAX_START_CODE) { + init_get_bits(&s->gb, buf_ptr, input_size); + decode_slice_header(h, &s->gb); + } + break; + } + } +} + +static int cavs_decode_init(AVCodecContext * avctx) { + AVSContext *h = avctx->priv_data; + MpegEncContext * const s = &h->s; + + MPV_decode_defaults(s); + s->avctx = avctx; + + avctx->pix_fmt= PIX_FMT_YUV420P; + + h->luma_scan[0] = 0; + h->luma_scan[1] = 8; + h->intra_pred_l[ INTRA_L_VERT] = intra_pred_vert; + h->intra_pred_l[ INTRA_L_HORIZ] = intra_pred_horiz; + h->intra_pred_l[ INTRA_L_LP] = intra_pred_lp; + h->intra_pred_l[ INTRA_L_DOWN_LEFT] = intra_pred_down_left; + h->intra_pred_l[INTRA_L_DOWN_RIGHT] = intra_pred_down_right; + h->intra_pred_l[ INTRA_L_LP_LEFT] = intra_pred_lp_left; + h->intra_pred_l[ INTRA_L_LP_TOP] = intra_pred_lp_top; + h->intra_pred_l[ INTRA_L_DC_128] = intra_pred_dc_128; + h->intra_pred_c[ INTRA_C_LP] = intra_pred_lp; + h->intra_pred_c[ INTRA_C_HORIZ] = intra_pred_horiz; + h->intra_pred_c[ INTRA_C_VERT] = intra_pred_vert; + h->intra_pred_c[ INTRA_C_PLANE] = intra_pred_plane; + h->intra_pred_c[ INTRA_C_LP_LEFT] = intra_pred_lp_left; + h->intra_pred_c[ INTRA_C_LP_TOP] = intra_pred_lp_top; + h->intra_pred_c[ INTRA_C_DC_128] = intra_pred_dc_128; + h->mv[ 7] = un_mv; + h->mv[19] = un_mv; + return 0; +} + +static int cavs_decode_end(AVCodecContext * avctx) { + AVSContext *h = avctx->priv_data; + + av_free(h->top_qp); + av_free(h->top_mv[0]); + av_free(h->top_mv[1]); + av_free(h->top_pred_Y); + av_free(h->top_border_y); + av_free(h->top_border_u); + av_free(h->top_border_v); + av_free(h->col_mv); + av_free(h->col_type_base); + av_free(h->block); + return 0; +} + +AVCodec cavs_decoder = { + "cavs", + CODEC_TYPE_VIDEO, + CODEC_ID_CAVS, + sizeof(AVSContext), + cavs_decode_init, + NULL, + cavs_decode_end, + cavs_decode_frame, + CODEC_CAP_DR1 | CODEC_CAP_DELAY, + .flush= ff_cavs_flush, +}; diff --git a/src/libffmpeg/libavcodec/cavsdata.h b/src/libffmpeg/libavcodec/cavsdata.h new file mode 100644 index 000000000..d6c78e433 --- /dev/null +++ b/src/libffmpeg/libavcodec/cavsdata.h @@ -0,0 +1,639 @@ +/* + * Chinese AVS video (AVS1-P2, JiZhun profile) decoder. + * Copyright (c) 2006 Stefan Gehrer <stefan.gehrer@gmx.de> + * + * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#define SLICE_MIN_START_CODE 0x00000101 +#define SLICE_MAX_START_CODE 0x000001af +#define EXT_START_CODE 0x000001b5 +#define USER_START_CODE 0x000001b2 +#define SEQ_START_CODE 0x000001b0 +#define PIC_I_START_CODE 0x000001b3 +#define PIC_PB_START_CODE 0x000001b6 + +#define A_AVAIL 1 +#define B_AVAIL 2 +#define C_AVAIL 4 +#define D_AVAIL 8 +#define NOT_AVAIL -1 +#define REF_INTRA -2 +#define REF_DIR -3 + +#define ESCAPE_CODE 59 + +#define FWD0 0x01 +#define FWD1 0x02 +#define BWD0 0x04 +#define BWD1 0x08 +#define SYM0 0x10 +#define SYM1 0x20 +#define SPLITH 0x40 +#define SPLITV 0x80 + +#define MV_BWD_OFFS 12 +#define MV_STRIDE 4 + +enum mb_t { + I_8X8 = 0, + P_SKIP, + P_16X16, + P_16X8, + P_8X16, + P_8X8, + B_SKIP, + B_DIRECT, + B_FWD_16X16, + B_BWD_16X16, + B_SYM_16X16, + B_8X8 = 29 +}; + +enum sub_mb_t { + B_SUB_DIRECT, + B_SUB_FWD, + B_SUB_BWD, + B_SUB_SYM +}; + +enum intra_luma_t { + INTRA_L_VERT, + INTRA_L_HORIZ, + INTRA_L_LP, + INTRA_L_DOWN_LEFT, + INTRA_L_DOWN_RIGHT, + INTRA_L_LP_LEFT, + INTRA_L_LP_TOP, + INTRA_L_DC_128 +}; + +enum intra_chroma_t { + INTRA_C_LP, + INTRA_C_HORIZ, + INTRA_C_VERT, + INTRA_C_PLANE, + INTRA_C_LP_LEFT, + INTRA_C_LP_TOP, + INTRA_C_DC_128, +}; + +enum mv_pred_t { + MV_PRED_MEDIAN, + MV_PRED_LEFT, + MV_PRED_TOP, + MV_PRED_TOPRIGHT, + MV_PRED_PSKIP, + MV_PRED_BSKIP +}; + +enum block_t { + BLK_16X16, + BLK_16X8, + BLK_8X16, + BLK_8X8 +}; + +enum mv_loc_t { + MV_FWD_D3 = 0, + MV_FWD_B2, + MV_FWD_B3, + MV_FWD_C2, + MV_FWD_A1, + MV_FWD_X0, + MV_FWD_X1, + MV_FWD_A3 = 8, + MV_FWD_X2, + MV_FWD_X3, + MV_BWD_D3 = MV_BWD_OFFS, + MV_BWD_B2, + MV_BWD_B3, + MV_BWD_C2, + MV_BWD_A1, + MV_BWD_X0, + MV_BWD_X1, + MV_BWD_A3 = MV_BWD_OFFS+8, + MV_BWD_X2, + MV_BWD_X3 +}; + +static const uint8_t partition_flags[30] = { + 0, //I_8X8 + 0, //P_SKIP + 0, //P_16X16 + SPLITH, //P_16X8 + SPLITV, //P_8X16 + SPLITH|SPLITV, //P_8X8 + SPLITH|SPLITV, //B_SKIP + SPLITH|SPLITV, //B_DIRECT + 0, //B_FWD_16X16 + 0, //B_BWD_16X16 + 0, //B_SYM_16X16 + FWD0|FWD1 |SPLITH, + FWD0|FWD1 |SPLITV, + BWD0|BWD1 |SPLITH, + BWD0|BWD1 |SPLITV, + FWD0|BWD1 |SPLITH, + FWD0|BWD1 |SPLITV, + BWD0|FWD1 |SPLITH, + BWD0|FWD1 |SPLITV, + FWD0|FWD1 |SYM1|SPLITH, + FWD0|FWD1 |SYM1 |SPLITV, + BWD0|FWD1 |SYM1|SPLITH, + BWD0|FWD1 |SYM1 |SPLITV, + FWD0|FWD1|SYM0 |SPLITH, + FWD0|FWD1|SYM0 |SPLITV, + FWD0|BWD1|SYM0 |SPLITH, + FWD0|BWD1|SYM0 |SPLITV, + FWD0|FWD1|SYM0|SYM1|SPLITH, + FWD0|FWD1|SYM0|SYM1 |SPLITV, + SPLITH|SPLITV, //B_8X8 = 29 +}; + +static const uint8_t scan3x3[4] = {4,5,7,8}; + +static const uint8_t mv_scan[4] = { + MV_FWD_X0,MV_FWD_X1, + MV_FWD_X2,MV_FWD_X3 +}; + +static const uint8_t cbp_tab[64][2] = { + {63, 0},{15,15},{31,63},{47,31},{ 0,16},{14,32},{13,47},{11,13}, + { 7,14},{ 5,11},{10,12},{ 8, 5},{12,10},{61, 7},{ 4,48},{55, 3}, + { 1, 2},{ 2, 8},{59, 4},{ 3, 1},{62,61},{ 9,55},{ 6,59},{29,62}, + {45,29},{51,27},{23,23},{39,19},{27,30},{46,28},{53, 9},{30, 6}, + {43,60},{37,21},{60,44},{16,26},{21,51},{28,35},{19,18},{35,20}, + {42,24},{26,53},{44,17},{32,37},{58,39},{24,45},{20,58},{17,43}, + {18,42},{48,46},{22,36},{33,33},{25,34},{49,40},{40,52},{36,49}, + {34,50},{50,56},{52,25},{54,22},{41,54},{56,57},{38,41},{57,38} +}; + +static const uint8_t chroma_qp[64] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15, + 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31, + 32,33,34,35,36,37,38,39,40,41,42,42,43,43,44,44, + 45,45,46,46,47,47,48,48,48,49,49,49,50,50,50,51 +}; + +static const uint8_t dequant_shift[64] = { + 14,14,14,14,14,14,14,14, + 13,13,13,13,13,13,13,13, + 13,12,12,12,12,12,12,12, + 11,11,11,11,11,11,11,11, + 11,10,10,10,10,10,10,10, + 10, 9, 9, 9, 9, 9, 9, 9, + 9, 8, 8, 8, 8, 8, 8, 8, + 7, 7, 7, 7, 7, 7, 7, 7 +}; + +static const uint16_t dequant_mul[64] = { + 32768,36061,38968,42495,46341,50535,55437,60424, + 32932,35734,38968,42495,46177,50535,55109,59933, + 65535,35734,38968,42577,46341,50617,55027,60097, + 32809,35734,38968,42454,46382,50576,55109,60056, + 65535,35734,38968,42495,46320,50515,55109,60076, + 65535,35744,38968,42495,46341,50535,55099,60087, + 65535,35734,38973,42500,46341,50535,55109,60097, + 32771,35734,38965,42497,46341,50535,55109,60099 +}; + +DECLARE_ALIGNED_8(typedef, struct) { + int16_t x; + int16_t y; + int16_t dist; + int16_t ref; +} vector_t; + +/** marks block as unavailable, i.e. out of picture + or not yet decoded */ +static const vector_t un_mv = {0,0,1,NOT_AVAIL}; + +/** marks block as "no prediction from this direction" + e.g. forward motion vector in BWD partition */ +static const vector_t dir_mv = {0,0,1,REF_DIR}; + +/** marks block as using intra prediction */ +static const vector_t intra_mv = {0,0,1,REF_INTRA}; + +typedef struct residual_vlc_t { + int8_t rltab[59][3]; + int8_t level_add[27]; + int8_t golomb_order; + int inc_limit; + int8_t max_run; +} residual_vlc_t; + +#define EOB 0,0,0 + +static const residual_vlc_t intra_2dvlc[7] = { + { + { //level / run / table_inc + { 1, 1, 1},{ -1, 1, 1},{ 1, 2, 1},{ -1, 2, 1},{ 1, 3, 1},{ -1, 3, 1}, + { 1, 4, 1},{ -1, 4, 1},{ 1, 5, 1},{ -1, 5, 1},{ 1, 6, 1},{ -1, 6, 1}, + { 1, 7, 1},{ -1, 7, 1},{ 1, 8, 1},{ -1, 8, 1},{ 1, 9, 1},{ -1, 9, 1}, + { 1,10, 1},{ -1,10, 1},{ 1,11, 1},{ -1,11, 1},{ 2, 1, 2},{ -2, 1, 2}, + { 1,12, 1},{ -1,12, 1},{ 1,13, 1},{ -1,13, 1},{ 1,14, 1},{ -1,14, 1}, + { 1,15, 1},{ -1,15, 1},{ 2, 2, 2},{ -2, 2, 2},{ 1,16, 1},{ -1,16, 1}, + { 1,17, 1},{ -1,17, 1},{ 3, 1, 3},{ -3, 1, 3},{ 1,18, 1},{ -1,18, 1}, + { 1,19, 1},{ -1,19, 1},{ 2, 3, 2},{ -2, 3, 2},{ 1,20, 1},{ -1,20, 1}, + { 1,21, 1},{ -1,21, 1},{ 2, 4, 2},{ -2, 4, 2},{ 1,22, 1},{ -1,22, 1}, + { 2, 5, 2},{ -2, 5, 2},{ 1,23, 1},{ -1,23, 1},{ EOB } + }, + //level_add + { 0, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2,-1,-1,-1}, + 2, //golomb_order + 0, //inc_limit + 23, //max_run + },{ + { //level / run + { 1, 1, 0},{ -1, 1, 0},{ 1, 2, 0},{ -1, 2, 0},{ 2, 1, 1},{ -2, 1, 1}, + { 1, 3, 0},{ -1, 3, 0},{ EOB },{ 1, 4, 0},{ -1, 4, 0},{ 1, 5, 0}, + { -1, 5, 0},{ 1, 6, 0},{ -1, 6, 0},{ 3, 1, 2},{ -3, 1, 2},{ 2, 2, 1}, + { -2, 2, 1},{ 1, 7, 0},{ -1, 7, 0},{ 1, 8, 0},{ -1, 8, 0},{ 1, 9, 0}, + { -1, 9, 0},{ 2, 3, 1},{ -2, 3, 1},{ 4, 1, 2},{ -4, 1, 2},{ 1,10, 0}, + { -1,10, 0},{ 1,11, 0},{ -1,11, 0},{ 2, 4, 1},{ -2, 4, 1},{ 3, 2, 2}, + { -3, 2, 2},{ 1,12, 0},{ -1,12, 0},{ 2, 5, 1},{ -2, 5, 1},{ 5, 1, 3}, + { -5, 1, 3},{ 1,13, 0},{ -1,13, 0},{ 2, 6, 1},{ -2, 6, 1},{ 1,14, 0}, + { -1,14, 0},{ 2, 7, 1},{ -2, 7, 1},{ 2, 8, 1},{ -2, 8, 1},{ 3, 3, 2}, + { -3, 3, 2},{ 6, 1, 3},{ -6, 1, 3},{ 1,15, 0},{ -1,15, 0} + }, + //level_add + { 0, 7, 4, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, + 2, //golomb_order + 1, //inc_limit + 15, //max_run + },{ + { //level / run + { 1, 1, 0},{ -1, 1, 0},{ 2, 1, 0},{ -2, 1, 0},{ 1, 2, 0},{ -1, 2, 0}, + { 3, 1, 1},{ -3, 1, 1},{ EOB },{ 1, 3, 0},{ -1, 3, 0},{ 2, 2, 0}, + { -2, 2, 0},{ 4, 1, 1},{ -4, 1, 1},{ 1, 4, 0},{ -1, 4, 0},{ 5, 1, 2}, + { -5, 1, 2},{ 1, 5, 0},{ -1, 5, 0},{ 3, 2, 1},{ -3, 2, 1},{ 2, 3, 0}, + { -2, 3, 0},{ 1, 6, 0},{ -1, 6, 0},{ 6, 1, 2},{ -6, 1, 2},{ 2, 4, 0}, + { -2, 4, 0},{ 1, 7, 0},{ -1, 7, 0},{ 4, 2, 1},{ -4, 2, 1},{ 7, 1, 2}, + { -7, 1, 2},{ 3, 3, 1},{ -3, 3, 1},{ 2, 5, 0},{ -2, 5, 0},{ 1, 8, 0}, + { -1, 8, 0},{ 2, 6, 0},{ -2, 6, 0},{ 8, 1, 3},{ -8, 1, 3},{ 1, 9, 0}, + { -1, 9, 0},{ 5, 2, 2},{ -5, 2, 2},{ 3, 4, 1},{ -3, 4, 1},{ 2, 7, 0}, + { -2, 7, 0},{ 9, 1, 3},{ -9, 1, 3},{ 1,10, 0},{ -1,10, 0} + }, + //level_add + { 0,10, 6, 4, 4, 3, 3, 3, 2, 2, 2,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, + 2, //golomb_order + 2, //inc_limit + 10, //max_run + },{ + { //level / run + { 1, 1, 0},{ -1, 1, 0},{ 2, 1, 0},{ -2, 1, 0},{ 3, 1, 0},{ -3, 1, 0}, + { 1, 2, 0},{ -1, 2, 0},{ EOB },{ 4, 1, 0},{ -4, 1, 0},{ 5, 1, 1}, + { -5, 1, 1},{ 2, 2, 0},{ -2, 2, 0},{ 1, 3, 0},{ -1, 3, 0},{ 6, 1, 1}, + { -6, 1, 1},{ 3, 2, 0},{ -3, 2, 0},{ 7, 1, 1},{ -7, 1, 1},{ 1, 4, 0}, + { -1, 4, 0},{ 8, 1, 2},{ -8, 1, 2},{ 2, 3, 0},{ -2, 3, 0},{ 4, 2, 0}, + { -4, 2, 0},{ 1, 5, 0},{ -1, 5, 0},{ 9, 1, 2},{ -9, 1, 2},{ 5, 2, 1}, + { -5, 2, 1},{ 2, 4, 0},{ -2, 4, 0},{ 10, 1, 2},{-10, 1, 2},{ 3, 3, 0}, + { -3, 3, 0},{ 1, 6, 0},{ -1, 6, 0},{ 11, 1, 3},{-11, 1, 3},{ 6, 2, 1}, + { -6, 2, 1},{ 1, 7, 0},{ -1, 7, 0},{ 2, 5, 0},{ -2, 5, 0},{ 3, 4, 0}, + { -3, 4, 0},{ 12, 1, 3},{-12, 1, 3},{ 4, 3, 0},{ -4, 3, 0} + }, + //level_add + { 0,13, 7, 5, 4, 3, 2, 2,-1,-1,-1 -1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, + 2, //golomb_order + 4, //inc_limit + 7, //max_run + },{ + { //level / run + { 1, 1, 0},{ -1, 1, 0},{ 2, 1, 0},{ -2, 1, 0},{ 3, 1, 0},{ -3, 1, 0}, + { EOB },{ 4, 1, 0},{ -4, 1, 0},{ 5, 1, 0},{ -5, 1, 0},{ 6, 1, 0}, + { -6, 1, 0},{ 1, 2, 0},{ -1, 2, 0},{ 7, 1, 0},{ -7, 1, 0},{ 8, 1, 1}, + { -8, 1, 1},{ 2, 2, 0},{ -2, 2, 0},{ 9, 1, 1},{ -9, 1, 1},{ 10, 1, 1}, + {-10, 1, 1},{ 1, 3, 0},{ -1, 3, 0},{ 3, 2, 0},{ -3, 2, 0},{ 11, 1, 2}, + {-11, 1, 2},{ 4, 2, 0},{ -4, 2, 0},{ 12, 1, 2},{-12, 1, 2},{ 13, 1, 2}, + {-13, 1, 2},{ 5, 2, 0},{ -5, 2, 0},{ 1, 4, 0},{ -1, 4, 0},{ 2, 3, 0}, + { -2, 3, 0},{ 14, 1, 2},{-14, 1, 2},{ 6, 2, 0},{ -6, 2, 0},{ 15, 1, 2}, + {-15, 1, 2},{ 16, 1, 2},{-16, 1, 2},{ 3, 3, 0},{ -3, 3, 0},{ 1, 5, 0}, + { -1, 5, 0},{ 7, 2, 0},{ -7, 2, 0},{ 17, 1, 2},{-17, 1, 2} + }, + //level_add + { 0,18, 8, 4, 2, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, + 2, //golomb_order + 7, //inc_limit + 5, //max_run + },{ + { //level / run + { EOB },{ 1, 1, 0},{ -1, 1, 0},{ 2, 1, 0},{ -2, 1, 0},{ 3, 1, 0}, + { -3, 1, 0},{ 4, 1, 0},{ -4, 1, 0},{ 5, 1, 0},{ -5, 1, 0},{ 6, 1, 0}, + { -6, 1, 0},{ 7, 1, 0},{ -7, 1, 0},{ 8, 1, 0},{ -8, 1, 0},{ 9, 1, 0}, + { -9, 1, 0},{ 10, 1, 0},{-10, 1, 0},{ 1, 2, 0},{ -1, 2, 0},{ 11, 1, 1}, + {-11, 1, 1},{ 12, 1, 1},{-12, 1, 1},{ 13, 1, 1},{-13, 1, 1},{ 2, 2, 0}, + { -2, 2, 0},{ 14, 1, 1},{-14, 1, 1},{ 15, 1, 1},{-15, 1, 1},{ 3, 2, 0}, + { -3, 2, 0},{ 16, 1, 1},{-16, 1, 1},{ 1, 3, 0},{ -1, 3, 0},{ 17, 1, 1}, + {-17, 1, 1},{ 4, 2, 0},{ -4, 2, 0},{ 18, 1, 1},{-18, 1, 1},{ 5, 2, 0}, + { -5, 2, 0},{ 19, 1, 1},{-19, 1, 1},{ 20, 1, 1},{-20, 1, 1},{ 6, 2, 0}, + { -6, 2, 0},{ 21, 1, 1},{-21, 1, 1},{ 2, 3, 0},{ -2, 3, 0} + }, + //level_add + { 0,22, 7, 3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, + 2, //golomb_order + 10, //inc_limit + 3, //max_run + },{ + { //level / run + { EOB },{ 1, 1, 0},{ -1, 1, 0},{ 2, 1, 0},{ -2, 1, 0},{ 3, 1, 0}, + { -3, 1, 0},{ 4, 1, 0},{ -4, 1, 0},{ 5, 1, 0},{ -5, 1, 0},{ 6, 1, 0}, + { -6, 1, 0},{ 7, 1, 0},{ -7, 1, 0},{ 8, 1, 0},{ -8, 1, 0},{ 9, 1, 0}, + { -9, 1, 0},{ 10, 1, 0},{-10, 1, 0},{ 11, 1, 0},{-11, 1, 0},{ 12, 1, 0}, + {-12, 1, 0},{ 13, 1, 0},{-13, 1, 0},{ 14, 1, 0},{-14, 1, 0},{ 15, 1, 0}, + {-15, 1, 0},{ 16, 1, 0},{-16, 1, 0},{ 1, 2, 0},{ -1, 2, 0},{ 17, 1, 0}, + {-17, 1, 0},{ 18, 1, 0},{-18, 1, 0},{ 19, 1, 0},{-19, 1, 0},{ 20, 1, 0}, + {-20, 1, 0},{ 21, 1, 0},{-21, 1, 0},{ 2, 2, 0},{ -2, 2, 0},{ 22, 1, 0}, + {-22, 1, 0},{ 23, 1, 0},{-23, 1, 0},{ 24, 1, 0},{-24, 1, 0},{ 25, 1, 0}, + {-25, 1, 0},{ 3, 2, 0},{ -3, 2, 0},{ 26, 1, 0},{-26, 1, 0} + }, + //level_add + { 0,27, 4,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, + 2, //golomb_order + INT_MAX, //inc_limit + 2, //max_run + } +}; + +static const residual_vlc_t inter_2dvlc[7] = { + { + { //level / run + { 1, 1, 1},{ -1, 1, 1},{ 1, 2, 1},{ -1, 2, 1},{ 1, 3, 1},{ -1, 3, 1}, + { 1, 4, 1},{ -1, 4, 1},{ 1, 5, 1},{ -1, 5, 1},{ 1, 6, 1},{ -1, 6, 1}, + { 1, 7, 1},{ -1, 7, 1},{ 1, 8, 1},{ -1, 8, 1},{ 1, 9, 1},{ -1, 9, 1}, + { 1,10, 1},{ -1,10, 1},{ 1,11, 1},{ -1,11, 1},{ 1,12, 1},{ -1,12, 1}, + { 1,13, 1},{ -1,13, 1},{ 2, 1, 2},{ -2, 1, 2},{ 1,14, 1},{ -1,14, 1}, + { 1,15, 1},{ -1,15, 1},{ 1,16, 1},{ -1,16, 1},{ 1,17, 1},{ -1,17, 1}, + { 1,18, 1},{ -1,18, 1},{ 1,19, 1},{ -1,19, 1},{ 3, 1, 3},{ -3, 1, 3}, + { 1,20, 1},{ -1,20, 1},{ 1,21, 1},{ -1,21, 1},{ 2, 2, 2},{ -2, 2, 2}, + { 1,22, 1},{ -1,22, 1},{ 1,23, 1},{ -1,23, 1},{ 1,24, 1},{ -1,24, 1}, + { 1,25, 1},{ -1,25, 1},{ 1,26, 1},{ -1,26, 1},{ EOB } + }, + //level_add + { 0, 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}, + 3, //golomb_order + 0, //inc_limit + 26 //max_run + },{ + { //level / run + { 1, 1, 0},{ -1, 1, 0},{ EOB },{ 1, 2, 0},{ -1, 2, 0},{ 1, 3, 0}, + { -1, 3, 0},{ 1, 4, 0},{ -1, 4, 0},{ 1, 5, 0},{ -1, 5, 0},{ 1, 6, 0}, + { -1, 6, 0},{ 2, 1, 1},{ -2, 1, 1},{ 1, 7, 0},{ -1, 7, 0},{ 1, 8, 0}, + { -1, 8, 0},{ 1, 9, 0},{ -1, 9, 0},{ 1,10, 0},{ -1,10, 0},{ 2, 2, 1}, + { -2, 2, 1},{ 1,11, 0},{ -1,11, 0},{ 1,12, 0},{ -1,12, 0},{ 3, 1, 2}, + { -3, 1, 2},{ 1,13, 0},{ -1,13, 0},{ 1,14, 0},{ -1,14, 0},{ 2, 3, 1}, + { -2, 3, 1},{ 1,15, 0},{ -1,15, 0},{ 2, 4, 1},{ -2, 4, 1},{ 1,16, 0}, + { -1,16, 0},{ 2, 5, 1},{ -2, 5, 1},{ 1,17, 0},{ -1,17, 0},{ 4, 1, 3}, + { -4, 1, 3},{ 2, 6, 1},{ -2, 6, 1},{ 1,18, 0},{ -1,18, 0},{ 1,19, 0}, + { -1,19, 0},{ 2, 7, 1},{ -2, 7, 1},{ 3, 2, 2},{ -3, 2, 2} + }, + //level_add + { 0, 5, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2,-1,-1,-1,-1,-1,-1,-1}, + 2, //golomb_order + 1, //inc_limit + 19 //max_run + },{ + { //level / run + { 1, 1, 0},{ -1, 1, 0},{ EOB },{ 1, 2, 0},{ -1, 2, 0},{ 2, 1, 0}, + { -2, 1, 0},{ 1, 3, 0},{ -1, 3, 0},{ 1, 4, 0},{ -1, 4, 0},{ 3, 1, 1}, + { -3, 1, 1},{ 2, 2, 0},{ -2, 2, 0},{ 1, 5, 0},{ -1, 5, 0},{ 1, 6, 0}, + { -1, 6, 0},{ 1, 7, 0},{ -1, 7, 0},{ 2, 3, 0},{ -2, 3, 0},{ 4, 1, 2}, + { -4, 1, 2},{ 1, 8, 0},{ -1, 8, 0},{ 3, 2, 1},{ -3, 2, 1},{ 2, 4, 0}, + { -2, 4, 0},{ 1, 9, 0},{ -1, 9, 0},{ 1,10, 0},{ -1,10, 0},{ 5, 1, 2}, + { -5, 1, 2},{ 2, 5, 0},{ -2, 5, 0},{ 1,11, 0},{ -1,11, 0},{ 2, 6, 0}, + { -2, 6, 0},{ 1,12, 0},{ -1,12, 0},{ 3, 3, 1},{ -3, 3, 1},{ 6, 1, 2}, + { -6, 1, 2},{ 4, 2, 2},{ -4, 2, 2},{ 1,13, 0},{ -1,13, 0},{ 2, 7, 0}, + { -2, 7, 0},{ 3, 4, 1},{ -3, 4, 1},{ 1,14, 0},{ -1,14, 0} + }, + //level_add + { 0, 7, 5, 4, 4, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, + 2, //golomb_order + 2, //inc_limit + 14 //max_run + },{ + { //level / run + { 1, 1, 0},{ -1, 1, 0},{ EOB },{ 2, 1, 0},{ -2, 1, 0},{ 1, 2, 0}, + { -1, 2, 0},{ 3, 1, 0},{ -3, 1, 0},{ 1, 3, 0},{ -1, 3, 0},{ 2, 2, 0}, + { -2, 2, 0},{ 4, 1, 1},{ -4, 1, 1},{ 1, 4, 0},{ -1, 4, 0},{ 5, 1, 1}, + { -5, 1, 1},{ 1, 5, 0},{ -1, 5, 0},{ 3, 2, 0},{ -3, 2, 0},{ 2, 3, 0}, + { -2, 3, 0},{ 1, 6, 0},{ -1, 6, 0},{ 6, 1, 1},{ -6, 1, 1},{ 2, 4, 0}, + { -2, 4, 0},{ 1, 7, 0},{ -1, 7, 0},{ 4, 2, 1},{ -4, 2, 1},{ 7, 1, 2}, + { -7, 1, 2},{ 3, 3, 0},{ -3, 3, 0},{ 1, 8, 0},{ -1, 8, 0},{ 2, 5, 0}, + { -2, 5, 0},{ 8, 1, 2},{ -8, 1, 2},{ 1, 9, 0},{ -1, 9, 0},{ 3, 4, 0}, + { -3, 4, 0},{ 2, 6, 0},{ -2, 6, 0},{ 5, 2, 1},{ -5, 2, 1},{ 1,10, 0}, + { -1,10, 0},{ 9, 1, 2},{ -9, 1, 2},{ 4, 3, 1},{ -4, 3, 1} + }, + //level_add + { 0,10, 6, 5, 4, 3, 3, 2, 2, 2, 2,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, + 2, //golomb_order + 3, //inc_limit + 10 //max_run + },{ + { //level / run + { 1, 1, 0},{ -1, 1, 0},{ EOB },{ 2, 1, 0},{ -2, 1, 0},{ 3, 1, 0}, + { -3, 1, 0},{ 1, 2, 0},{ -1, 2, 0},{ 4, 1, 0},{ -4, 1, 0},{ 5, 1, 0}, + { -5, 1, 0},{ 2, 2, 0},{ -2, 2, 0},{ 1, 3, 0},{ -1, 3, 0},{ 6, 1, 0}, + { -6, 1, 0},{ 3, 2, 0},{ -3, 2, 0},{ 7, 1, 1},{ -7, 1, 1},{ 1, 4, 0}, + { -1, 4, 0},{ 8, 1, 1},{ -8, 1, 1},{ 2, 3, 0},{ -2, 3, 0},{ 4, 2, 0}, + { -4, 2, 0},{ 1, 5, 0},{ -1, 5, 0},{ 9, 1, 1},{ -9, 1, 1},{ 5, 2, 0}, + { -5, 2, 0},{ 2, 4, 0},{ -2, 4, 0},{ 1, 6, 0},{ -1, 6, 0},{ 10, 1, 2}, + {-10, 1, 2},{ 3, 3, 0},{ -3, 3, 0},{ 11, 1, 2},{-11, 1, 2},{ 1, 7, 0}, + { -1, 7, 0},{ 6, 2, 0},{ -6, 2, 0},{ 3, 4, 0},{ -3, 4, 0},{ 2, 5, 0}, + { -2, 5, 0},{ 12, 1, 2},{-12, 1, 2},{ 4, 3, 0},{ -4, 3, 0} + }, + //level_add + { 0,13, 7, 5, 4, 3, 2, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, + 2, //golomb_order + 6, //inc_limit + 7 //max_run + },{ + { //level / run + { EOB },{ 1, 1, 0},{ -1, 1, 0},{ 2, 1, 0},{ -2, 1, 0},{ 3, 1, 0}, + { -3, 1, 0},{ 4, 1, 0},{ -4, 1, 0},{ 5, 1, 0},{ -5, 1, 0},{ 1, 2, 0}, + { -1, 2, 0},{ 6, 1, 0},{ -6, 1, 0},{ 7, 1, 0},{ -7, 1, 0},{ 8, 1, 0}, + { -8, 1, 0},{ 2, 2, 0},{ -2, 2, 0},{ 9, 1, 0},{ -9, 1, 0},{ 1, 3, 0}, + { -1, 3, 0},{ 10, 1, 1},{-10, 1, 1},{ 3, 2, 0},{ -3, 2, 0},{ 11, 1, 1}, + {-11, 1, 1},{ 4, 2, 0},{ -4, 2, 0},{ 12, 1, 1},{-12, 1, 1},{ 1, 4, 0}, + { -1, 4, 0},{ 2, 3, 0},{ -2, 3, 0},{ 13, 1, 1},{-13, 1, 1},{ 5, 2, 0}, + { -5, 2, 0},{ 14, 1, 1},{-14, 1, 1},{ 6, 2, 0},{ -6, 2, 0},{ 1, 5, 0}, + { -1, 5, 0},{ 15, 1, 1},{-15, 1, 1},{ 3, 3, 0},{ -3, 3, 0},{ 16, 1, 1}, + {-16, 1, 1},{ 2, 4, 0},{ -2, 4, 0},{ 7, 2, 0},{ -7, 2, 0} + }, + //level_add + { 0,17, 8, 4, 3, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, + 2, //golomb_order + 9, //inc_limit + 5 //max_run + },{ + { //level / run + { EOB },{ 1, 1, 0},{ -1, 1, 0},{ 2, 1, 0},{ -2, 1, 0},{ 3, 1, 0}, + { -3, 1, 0},{ 4, 1, 0},{ -4, 1, 0},{ 5, 1, 0},{ -5, 1, 0},{ 6, 1, 0}, + { -6, 1, 0},{ 7, 1, 0},{ -7, 1, 0},{ 1, 2, 0},{ -1, 2, 0},{ 8, 1, 0}, + { -8, 1, 0},{ 9, 1, 0},{ -9, 1, 0},{ 10, 1, 0},{-10, 1, 0},{ 11, 1, 0}, + {-11, 1, 0},{ 12, 1, 0},{-12, 1, 0},{ 2, 2, 0},{ -2, 2, 0},{ 13, 1, 0}, + {-13, 1, 0},{ 1, 3, 0},{ -1, 3, 0},{ 14, 1, 0},{-14, 1, 0},{ 15, 1, 0}, + {-15, 1, 0},{ 3, 2, 0},{ -3, 2, 0},{ 16, 1, 0},{-16, 1, 0},{ 17, 1, 0}, + {-17, 1, 0},{ 18, 1, 0},{-18, 1, 0},{ 4, 2, 0},{ -4, 2, 0},{ 19, 1, 0}, + {-19, 1, 0},{ 20, 1, 0},{-20, 1, 0},{ 2, 3, 0},{ -2, 3, 0},{ 1, 4, 0}, + { -1, 4, 0},{ 5, 2, 0},{ -5, 2, 0},{ 21, 1, 0},{-21, 1, 0} + }, + //level_add + { 0,22, 6, 3, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, + 2, //golomb_order + INT_MAX, //inc_limit + 4 //max_run + } +}; + +static const residual_vlc_t chroma_2dvlc[5] = { + { + { //level / run + { 1, 1, 1},{ -1, 1, 1},{ 1, 2, 1},{ -1, 2, 1},{ 1, 3, 1},{ -1, 3, 1}, + { 1, 4, 1},{ -1, 4, 1},{ 1, 5, 1},{ -1, 5, 1},{ 1, 6, 1},{ -1, 6, 1}, + { 1, 7, 1},{ -1, 7, 1},{ 2, 1, 2},{ -2, 1, 2},{ 1, 8, 1},{ -1, 8, 1}, + { 1, 9, 1},{ -1, 9, 1},{ 1,10, 1},{ -1,10, 1},{ 1,11, 1},{ -1,11, 1}, + { 1,12, 1},{ -1,12, 1},{ 1,13, 1},{ -1,13, 1},{ 1,14, 1},{ -1,14, 1}, + { 1,15, 1},{ -1,15, 1},{ 3, 1, 3},{ -3, 1, 3},{ 1,16, 1},{ -1,16, 1}, + { 1,17, 1},{ -1,17, 1},{ 1,18, 1},{ -1,18, 1},{ 1,19, 1},{ -1,19, 1}, + { 1,20, 1},{ -1,20, 1},{ 1,21, 1},{ -1,21, 1},{ 1,22, 1},{ -1,22, 1}, + { 2, 2, 2},{ -2, 2, 2},{ 1,23, 1},{ -1,23, 1},{ 1,24, 1},{ -1,24, 1}, + { 1,25, 1},{ -1,25, 1},{ 4, 1, 3},{ -4, 1, 3},{ EOB } + }, + //level_add + { 0, 5, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2,-1}, + 2, //golomb_order + 0, //inc_limit + 25 //max_run + },{ + { //level / run + { EOB },{ 1, 1, 0},{ -1, 1, 0},{ 1, 2, 0},{ -1, 2, 0},{ 2, 1, 1}, + { -2, 1, 1},{ 1, 3, 0},{ -1, 3, 0},{ 1, 4, 0},{ -1, 4, 0},{ 1, 5, 0}, + { -1, 5, 0},{ 1, 6, 0},{ -1, 6, 0},{ 3, 1, 2},{ -3, 1, 2},{ 1, 7, 0}, + { -1, 7, 0},{ 1, 8, 0},{ -1, 8, 0},{ 2, 2, 1},{ -2, 2, 1},{ 1, 9, 0}, + { -1, 9, 0},{ 1,10, 0},{ -1,10, 0},{ 1,11, 0},{ -1,11, 0},{ 4, 1, 2}, + { -4, 1, 2},{ 1,12, 0},{ -1,12, 0},{ 1,13, 0},{ -1,13, 0},{ 1,14, 0}, + { -1,14, 0},{ 2, 3, 1},{ -2, 3, 1},{ 1,15, 0},{ -1,15, 0},{ 2, 4, 1}, + { -2, 4, 1},{ 5, 1, 3},{ -5, 1, 3},{ 3, 2, 2},{ -3, 2, 2},{ 1,16, 0}, + { -1,16, 0},{ 1,17, 0},{ -1,17, 0},{ 1,18, 0},{ -1,18, 0},{ 2, 5, 1}, + { -2, 5, 1},{ 1,19, 0},{ -1,19, 0},{ 1,20, 0},{ -1,20, 0} + }, + //level_add + { 0, 6, 4, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2,-1,-1,-1,-1,-1,-1}, + 0, //golomb_order + 1, //inc_limit + 20 //max_run + },{ + { //level / run + { 1, 1, 0},{ -1, 1, 0},{ EOB },{ 2, 1, 0},{ -2, 1, 0},{ 1, 2, 0}, + { -1, 2, 0},{ 3, 1, 1},{ -3, 1, 1},{ 1, 3, 0},{ -1, 3, 0},{ 4, 1, 1}, + { -4, 1, 1},{ 2, 2, 0},{ -2, 2, 0},{ 1, 4, 0},{ -1, 4, 0},{ 5, 1, 2}, + { -5, 1, 2},{ 1, 5, 0},{ -1, 5, 0},{ 3, 2, 1},{ -3, 2, 1},{ 2, 3, 0}, + { -2, 3, 0},{ 1, 6, 0},{ -1, 6, 0},{ 6, 1, 2},{ -6, 1, 2},{ 1, 7, 0}, + { -1, 7, 0},{ 2, 4, 0},{ -2, 4, 0},{ 7, 1, 2},{ -7, 1, 2},{ 1, 8, 0}, + { -1, 8, 0},{ 4, 2, 1},{ -4, 2, 1},{ 1, 9, 0},{ -1, 9, 0},{ 3, 3, 1}, + { -3, 3, 1},{ 2, 5, 0},{ -2, 5, 0},{ 2, 6, 0},{ -2, 6, 0},{ 8, 1, 2}, + { -8, 1, 2},{ 1,10, 0},{ -1,10, 0},{ 1,11, 0},{ -1,11, 0},{ 9, 1, 2}, + { -9, 1, 2},{ 5, 2, 2},{ -5, 2, 2},{ 3, 4, 1},{ -3, 4, 1}, + }, + //level_add + { 0,10, 6, 4, 4, 3, 3, 2, 2, 2, 2, 2,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, + 1, //golomb_order + 2, //inc_limit + 11 //max_run + },{ + { //level / run + { EOB },{ 1, 1, 0},{ -1, 1, 0},{ 2, 1, 0},{ -2, 1, 0},{ 3, 1, 0}, + { -3, 1, 0},{ 4, 1, 0},{ -4, 1, 0},{ 1, 2, 0},{ -1, 2, 0},{ 5, 1, 1}, + { -5, 1, 1},{ 2, 2, 0},{ -2, 2, 0},{ 6, 1, 1},{ -6, 1, 1},{ 1, 3, 0}, + { -1, 3, 0},{ 7, 1, 1},{ -7, 1, 1},{ 3, 2, 0},{ -3, 2, 0},{ 8, 1, 1}, + { -8, 1, 1},{ 1, 4, 0},{ -1, 4, 0},{ 2, 3, 0},{ -2, 3, 0},{ 9, 1, 1}, + { -9, 1, 1},{ 4, 2, 0},{ -4, 2, 0},{ 1, 5, 0},{ -1, 5, 0},{ 10, 1, 1}, + {-10, 1, 1},{ 3, 3, 0},{ -3, 3, 0},{ 5, 2, 1},{ -5, 2, 1},{ 2, 4, 0}, + { -2, 4, 0},{ 11, 1, 1},{-11, 1, 1},{ 1, 6, 0},{ -1, 6, 0},{ 12, 1, 1}, + {-12, 1, 1},{ 1, 7, 0},{ -1, 7, 0},{ 6, 2, 1},{ -6, 2, 1},{ 13, 1, 1}, + {-13, 1, 1},{ 2, 5, 0},{ -2, 5, 0},{ 1, 8, 0},{ -1, 8, 0}, + }, + //level_add + { 0,14, 7, 4, 3, 3, 2, 2, 2,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, + 1, //golomb_order + 4, //inc_limit + 8 //max_run + },{ + { //level / run + { EOB },{ 1, 1, 0},{ -1, 1, 0},{ 2, 1, 0},{ -2, 1, 0},{ 3, 1, 0}, + { -3, 1, 0},{ 4, 1, 0},{ -4, 1, 0},{ 5, 1, 0},{ -5, 1, 0},{ 6, 1, 0}, + { -6, 1, 0},{ 7, 1, 0},{ -7, 1, 0},{ 8, 1, 0},{ -8, 1, 0},{ 1, 2, 0}, + { -1, 2, 0},{ 9, 1, 0},{ -9, 1, 0},{ 10, 1, 0},{-10, 1, 0},{ 11, 1, 0}, + {-11, 1, 0},{ 2, 2, 0},{ -2, 2, 0},{ 12, 1, 0},{-12, 1, 0},{ 13, 1, 0}, + {-13, 1, 0},{ 3, 2, 0},{ -3, 2, 0},{ 14, 1, 0},{-14, 1, 0},{ 1, 3, 0}, + { -1, 3, 0},{ 15, 1, 0},{-15, 1, 0},{ 4, 2, 0},{ -4, 2, 0},{ 16, 1, 0}, + {-16, 1, 0},{ 17, 1, 0},{-17, 1, 0},{ 5, 2, 0},{ -5, 2, 0},{ 1, 4, 0}, + { -1, 4, 0},{ 2, 3, 0},{ -2, 3, 0},{ 18, 1, 0},{-18, 1, 0},{ 6, 2, 0}, + { -6, 2, 0},{ 19, 1, 0},{-19, 1, 0},{ 1, 5, 0},{ -1, 5, 0}, + }, + //level_add + { 0,20, 7, 3, 2, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, + 0, //golomb_order + INT_MAX, //inc_limit + 5, //max_run + } +}; + +#undef EOB + +static const uint8_t alpha_tab[64] = { + 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, + 4, 4, 5, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 18, 20, + 22, 24, 26, 28, 30, 33, 33, 35, 35, 36, 37, 37, 39, 39, 42, 44, + 46, 48, 50, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64 +}; + +static const uint8_t beta_tab[64] = { + 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, + 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, + 6, 7, 7, 7, 8, 8, 8, 9, 9, 10, 10, 11, 11, 12, 13, 14, + 15, 16, 17, 18, 19, 20, 21, 22, 23, 23, 24, 24, 25, 25, 26, 27 +}; + +static const uint8_t tc_tab[64] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, + 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, + 5, 5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9 +}; + +static const int_fast8_t left_modifier_l[8] = { 0,-1, 6,-1,-1, 7, 6, 7}; +static const int_fast8_t top_modifier_l[8] = {-1, 1, 5,-1,-1, 5, 7, 7}; +static const int_fast8_t left_modifier_c[7] = { 5,-1, 2,-1, 6, 5, 6}; +static const int_fast8_t top_modifier_c[7] = { 4, 1,-1,-1, 4, 6, 6}; diff --git a/src/libffmpeg/libavcodec/cook.c b/src/libffmpeg/libavcodec/cook.c new file mode 100644 index 000000000..fb04cf574 --- /dev/null +++ b/src/libffmpeg/libavcodec/cook.c @@ -0,0 +1,1311 @@ +/* + * COOK compatible decoder + * Copyright (c) 2003 Sascha Sommer + * Copyright (c) 2005 Benjamin Larsson + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +/** + * @file cook.c + * Cook compatible decoder. + * This decoder handles RealNetworks, RealAudio G2 data. + * Cook is identified by the codec name cook in RM files. + * + * To use this decoder, a calling application must supply the extradata + * bytes provided from the RM container; 8+ bytes for mono streams and + * 16+ for stereo streams (maybe more). + * + * Codec technicalities (all this assume a buffer length of 1024): + * Cook works with several different techniques to achieve its compression. + * In the timedomain the buffer is divided into 8 pieces and quantized. If + * two neighboring pieces have different quantization index a smooth + * quantization curve is used to get a smooth overlap between the different + * pieces. + * To get to the transformdomain Cook uses a modulated lapped transform. + * The transform domain has 50 subbands with 20 elements each. This + * means only a maximum of 50*20=1000 coefficients are used out of the 1024 + * available. + */ + +#include <math.h> +#include <stddef.h> +#include <stdio.h> + +#define ALT_BITSTREAM_READER +#include "avcodec.h" +#include "bitstream.h" +#include "dsputil.h" + +#include "cookdata.h" + +/* the different Cook versions */ +#define MONO_COOK1 0x1000001 +#define MONO_COOK2 0x1000002 +#define JOINT_STEREO 0x1000003 +#define MC_COOK 0x2000000 //multichannel Cook, not supported + +#define SUBBAND_SIZE 20 +//#define COOKDEBUG + +typedef struct { + int size; + int qidx_table1[8]; + int qidx_table2[8]; +} COOKgain; + +typedef struct __attribute__((__packed__)){ + /* codec data start */ + uint32_t cookversion; //in network order, bigendian + uint16_t samples_per_frame; //amount of samples per frame per channel, bigendian + uint16_t subbands; //amount of bands used in the frequency domain, bigendian + /* Mono extradata ends here. */ + uint32_t unused; + uint16_t js_subband_start; //bigendian + uint16_t js_vlc_bits; //bigendian + /* Stereo extradata ends here. */ +} COOKextradata; + + +typedef struct { + GetBitContext gb; + /* stream data */ + int nb_channels; + int joint_stereo; + int bit_rate; + int sample_rate; + int samples_per_channel; + int samples_per_frame; + int subbands; + int log2_numvector_size; + int numvector_size; //1 << log2_numvector_size; + int js_subband_start; + int total_subbands; + int num_vectors; + int bits_per_subpacket; + /* states */ + int random_state; + + /* transform data */ + FFTContext fft_ctx; + FFTSample mlt_tmp[1024] __attribute__((aligned(16))); /* temporary storage for imlt */ + float* mlt_window; + float* mlt_precos; + float* mlt_presin; + float* mlt_postcos; + int fft_size; + int fft_order; + int mlt_size; //modulated lapped transform size + + /* gain buffers */ + COOKgain* gain_now_ptr; + COOKgain* gain_previous_ptr; + COOKgain gain_current; + COOKgain gain_now; + COOKgain gain_previous; + COOKgain gain_channel1[2]; + COOKgain gain_channel2[2]; + + /* VLC data */ + int js_vlc_bits; + VLC envelope_quant_index[13]; + VLC sqvh[7]; //scalar quantization + VLC ccpl; //channel coupling + + /* generatable tables and related variables */ + int gain_size_factor; + float gain_table[23]; + float pow2tab[127]; + float rootpow2tab[127]; + + /* data buffers */ + + uint8_t* decoded_bytes_buffer; + float mono_mdct_output[2048] __attribute__((aligned(16))); + float* previous_buffer_ptr[2]; + float mono_previous_buffer1[1024]; + float mono_previous_buffer2[1024]; + float* decode_buf_ptr[4]; + float* decode_buf_ptr2[2]; + float decode_buffer_1[1024]; + float decode_buffer_2[1024]; + float decode_buffer_3[1024]; + float decode_buffer_4[1024]; +} COOKContext; + +/* debug functions */ + +#ifdef COOKDEBUG +static void dump_float_table(float* table, int size, int delimiter) { + int i=0; + av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i); + for (i=0 ; i<size ; i++) { + av_log(NULL, AV_LOG_ERROR, "%5.1f, ", table[i]); + if ((i+1)%delimiter == 0) av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i+1); + } +} + +static void dump_int_table(int* table, int size, int delimiter) { + int i=0; + av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i); + for (i=0 ; i<size ; i++) { + av_log(NULL, AV_LOG_ERROR, "%d, ", table[i]); + if ((i+1)%delimiter == 0) av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i+1); + } +} + +static void dump_short_table(short* table, int size, int delimiter) { + int i=0; + av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i); + for (i=0 ; i<size ; i++) { + av_log(NULL, AV_LOG_ERROR, "%d, ", table[i]); + if ((i+1)%delimiter == 0) av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i+1); + } +} + +#endif + +/*************** init functions ***************/ + +/* table generator */ +static void init_pow2table(COOKContext *q){ + int i; + q->pow2tab[63] = 1.0; + for (i=1 ; i<64 ; i++){ + q->pow2tab[63+i]=(float)((uint64_t)1<<i); + q->pow2tab[63-i]=1.0/(float)((uint64_t)1<<i); + } +} + +/* table generator */ +static void init_rootpow2table(COOKContext *q){ + int i; + q->rootpow2tab[63] = 1.0; + for (i=1 ; i<64 ; i++){ + q->rootpow2tab[63+i]=sqrt((float)((uint64_t)1<<i)); + q->rootpow2tab[63-i]=sqrt(1.0/(float)((uint64_t)1<<i)); + } +} + +/* table generator */ +static void init_gain_table(COOKContext *q) { + int i; + q->gain_size_factor = q->samples_per_channel/8; + for (i=0 ; i<23 ; i++) { + q->gain_table[i] = pow((double)q->pow2tab[i+52] , + (1.0/(double)q->gain_size_factor)); + } +} + + +static int init_cook_vlc_tables(COOKContext *q) { + int i, result; + + result = 0; + for (i=0 ; i<13 ; i++) { + result &= init_vlc (&q->envelope_quant_index[i], 9, 24, + envelope_quant_index_huffbits[i], 1, 1, + envelope_quant_index_huffcodes[i], 2, 2, 0); + } + av_log(NULL,AV_LOG_DEBUG,"sqvh VLC init\n"); + for (i=0 ; i<7 ; i++) { + result &= init_vlc (&q->sqvh[i], vhvlcsize_tab[i], vhsize_tab[i], + cvh_huffbits[i], 1, 1, + cvh_huffcodes[i], 2, 2, 0); + } + + if (q->nb_channels==2 && q->joint_stereo==1){ + result &= init_vlc (&q->ccpl, 6, (1<<q->js_vlc_bits)-1, + ccpl_huffbits[q->js_vlc_bits-2], 1, 1, + ccpl_huffcodes[q->js_vlc_bits-2], 2, 2, 0); + av_log(NULL,AV_LOG_DEBUG,"Joint-stereo VLC used.\n"); + } + + av_log(NULL,AV_LOG_DEBUG,"VLC tables initialized.\n"); + return result; +} + +static int init_cook_mlt(COOKContext *q) { + int j; + float alpha; + + /* Allocate the buffers, could be replaced with a static [512] + array if needed. */ + q->mlt_size = q->samples_per_channel; + q->mlt_window = av_malloc(sizeof(float)*q->mlt_size); + q->mlt_precos = av_malloc(sizeof(float)*q->mlt_size/2); + q->mlt_presin = av_malloc(sizeof(float)*q->mlt_size/2); + q->mlt_postcos = av_malloc(sizeof(float)*q->mlt_size/2); + + /* Initialize the MLT window: simple sine window. */ + alpha = M_PI / (2.0 * (float)q->mlt_size); + for(j=0 ; j<q->mlt_size ; j++) { + q->mlt_window[j] = sin((j + 512.0/(float)q->mlt_size) * alpha); + } + + /* pre/post twiddle factors */ + for (j=0 ; j<q->mlt_size/2 ; j++){ + q->mlt_precos[j] = cos( ((j+0.25)*M_PI)/q->mlt_size); + q->mlt_presin[j] = sin( ((j+0.25)*M_PI)/q->mlt_size); + q->mlt_postcos[j] = (float)sqrt(2.0/(float)q->mlt_size)*cos( ((float)j*M_PI) /q->mlt_size); //sqrt(2/MLT_size) = scalefactor + } + + /* Initialize the FFT. */ + ff_fft_init(&q->fft_ctx, av_log2(q->mlt_size)-1, 0); + av_log(NULL,AV_LOG_DEBUG,"FFT initialized, order = %d.\n", + av_log2(q->samples_per_channel)-1); + + return (int)(q->mlt_window && q->mlt_precos && q->mlt_presin && q->mlt_postcos); +} + +/*************** init functions end ***********/ + +/** + * Cook indata decoding, every 32 bits are XORed with 0x37c511f2. + * Why? No idea, some checksum/error detection method maybe. + * Nice way to waste CPU cycles. + * + * @param in pointer to 32bit array of indata + * @param bits amount of bits + * @param out pointer to 32bit array of outdata + */ + +static inline void decode_bytes(uint8_t* inbuffer, uint8_t* out, int bytes){ + int i; + uint32_t* buf = (uint32_t*) inbuffer; + uint32_t* obuf = (uint32_t*) out; + /* FIXME: 64 bit platforms would be able to do 64 bits at a time. + * I'm too lazy though, should be something like + * for(i=0 ; i<bitamount/64 ; i++) + * (int64_t)out[i] = 0x37c511f237c511f2^be2me_64(int64_t)in[i]); + * Buffer alignment needs to be checked. */ + + + for(i=0 ; i<bytes/4 ; i++){ +#ifdef WORDS_BIGENDIAN + obuf[i] = 0x37c511f2^buf[i]; +#else + obuf[i] = 0xf211c537^buf[i]; +#endif + } +} + +/** + * Cook uninit + */ + +static int cook_decode_close(AVCodecContext *avctx) +{ + int i; + COOKContext *q = avctx->priv_data; + av_log(NULL,AV_LOG_DEBUG, "Deallocating memory.\n"); + + /* Free allocated memory buffers. */ + av_free(q->mlt_window); + av_free(q->mlt_precos); + av_free(q->mlt_presin); + av_free(q->mlt_postcos); + av_free(q->decoded_bytes_buffer); + + /* Free the transform. */ + ff_fft_end(&q->fft_ctx); + + /* Free the VLC tables. */ + for (i=0 ; i<13 ; i++) { + free_vlc(&q->envelope_quant_index[i]); + } + for (i=0 ; i<7 ; i++) { + free_vlc(&q->sqvh[i]); + } + if(q->nb_channels==2 && q->joint_stereo==1 ){ + free_vlc(&q->ccpl); + } + + av_log(NULL,AV_LOG_DEBUG,"Memory deallocated.\n"); + + return 0; +} + +/** + * Fill the COOKgain structure for the timedomain quantization. + * + * @param q pointer to the COOKContext + * @param gaininfo pointer to the COOKgain + */ + +static void decode_gain_info(GetBitContext *gb, COOKgain* gaininfo) { + int i; + + while (get_bits1(gb)) {} + + gaininfo->size = get_bits_count(gb) - 1; //amount of elements*2 to update + + if (get_bits_count(gb) - 1 <= 0) return; + + for (i=0 ; i<gaininfo->size ; i++){ + gaininfo->qidx_table1[i] = get_bits(gb,3); + if (get_bits1(gb)) { + gaininfo->qidx_table2[i] = get_bits(gb,4) - 7; //convert to signed + } else { + gaininfo->qidx_table2[i] = -1; + } + } +} + +/** + * Create the quant index table needed for the envelope. + * + * @param q pointer to the COOKContext + * @param quant_index_table pointer to the array + */ + +static void decode_envelope(COOKContext *q, int* quant_index_table) { + int i,j, vlc_index; + int bitbias; + + bitbias = get_bits_count(&q->gb); + quant_index_table[0]= get_bits(&q->gb,6) - 6; //This is used later in categorize + + for (i=1 ; i < q->total_subbands ; i++){ + vlc_index=i; + if (i >= q->js_subband_start * 2) { + vlc_index-=q->js_subband_start; + } else { + vlc_index/=2; + if(vlc_index < 1) vlc_index = 1; + } + if (vlc_index>13) vlc_index = 13; //the VLC tables >13 are identical to No. 13 + + j = get_vlc2(&q->gb, q->envelope_quant_index[vlc_index-1].table, + q->envelope_quant_index[vlc_index-1].bits,2); + quant_index_table[i] = quant_index_table[i-1] + j - 12; //differential encoding + } +} + +/** + * Create the quant value table. + * + * @param q pointer to the COOKContext + * @param quant_value_table pointer to the array + */ + +static void inline dequant_envelope(COOKContext *q, int* quant_index_table, + float* quant_value_table){ + + int i; + for(i=0 ; i < q->total_subbands ; i++){ + quant_value_table[i] = q->rootpow2tab[quant_index_table[i]+63]; + } +} + +/** + * Calculate the category and category_index vector. + * + * @param q pointer to the COOKContext + * @param quant_index_table pointer to the array + * @param category pointer to the category array + * @param category_index pointer to the category_index array + */ + +static void categorize(COOKContext *q, int* quant_index_table, + int* category, int* category_index){ + int exp_idx, bias, tmpbias, bits_left, num_bits, index, v, i, j; + int exp_index2[102]; + int exp_index1[102]; + + int tmp_categorize_array1[128]; + int tmp_categorize_array1_idx=0; + int tmp_categorize_array2[128]; + int tmp_categorize_array2_idx=0; + int category_index_size=0; + + bits_left = q->bits_per_subpacket - get_bits_count(&q->gb); + + if(bits_left > q->samples_per_channel) { + bits_left = q->samples_per_channel + + ((bits_left - q->samples_per_channel)*5)/8; + //av_log(NULL, AV_LOG_ERROR, "bits_left = %d\n",bits_left); + } + + memset(&exp_index1,0,102*sizeof(int)); + memset(&exp_index2,0,102*sizeof(int)); + memset(&tmp_categorize_array1,0,128*sizeof(int)); + memset(&tmp_categorize_array2,0,128*sizeof(int)); + + bias=-32; + + /* Estimate bias. */ + for (i=32 ; i>0 ; i=i/2){ + num_bits = 0; + index = 0; + for (j=q->total_subbands ; j>0 ; j--){ + exp_idx = (i - quant_index_table[index] + bias) / 2; + if (exp_idx<0){ + exp_idx=0; + } else if(exp_idx >7) { + exp_idx=7; + } + index++; + num_bits+=expbits_tab[exp_idx]; + } + if(num_bits >= bits_left - 32){ + bias+=i; + } + } + + /* Calculate total number of bits. */ + num_bits=0; + for (i=0 ; i<q->total_subbands ; i++) { + exp_idx = (bias - quant_index_table[i]) / 2; + if (exp_idx<0) { + exp_idx=0; + } else if(exp_idx >7) { + exp_idx=7; + } + num_bits += expbits_tab[exp_idx]; + exp_index1[i] = exp_idx; + exp_index2[i] = exp_idx; + } + tmpbias = bias = num_bits; + + for (j = 1 ; j < q->numvector_size ; j++) { + if (tmpbias + bias > 2*bits_left) { /* ---> */ + int max = -999999; + index=-1; + for (i=0 ; i<q->total_subbands ; i++){ + if (exp_index1[i] < 7) { + v = (-2*exp_index1[i]) - quant_index_table[i] - 32; + if ( v >= max) { + max = v; + index = i; + } + } + } + if(index==-1)break; + tmp_categorize_array1[tmp_categorize_array1_idx++] = index; + tmpbias -= expbits_tab[exp_index1[index]] - + expbits_tab[exp_index1[index]+1]; + ++exp_index1[index]; + } else { /* <--- */ + int min = 999999; + index=-1; + for (i=0 ; i<q->total_subbands ; i++){ + if(exp_index2[i] > 0){ + v = (-2*exp_index2[i])-quant_index_table[i]; + if ( v < min) { + min = v; + index = i; + } + } + } + if(index == -1)break; + tmp_categorize_array2[tmp_categorize_array2_idx++] = index; + tmpbias -= expbits_tab[exp_index2[index]] - + expbits_tab[exp_index2[index]-1]; + --exp_index2[index]; + } + } + + for(i=0 ; i<q->total_subbands ; i++) + category[i] = exp_index2[i]; + + /* Concatenate the two arrays. */ + for(i=tmp_categorize_array2_idx-1 ; i >= 0; i--) + category_index[category_index_size++] = tmp_categorize_array2[i]; + + for(i=0;i<tmp_categorize_array1_idx;i++) + category_index[category_index_size++ ] = tmp_categorize_array1[i]; + + /* FIXME: mc_sich_ra8_20.rm triggers this, not sure with what we + should fill the remaining bytes. */ + for(i=category_index_size;i<q->numvector_size;i++) + category_index[i]=0; + +} + + +/** + * Expand the category vector. + * + * @param q pointer to the COOKContext + * @param category pointer to the category array + * @param category_index pointer to the category_index array + */ + +static void inline expand_category(COOKContext *q, int* category, + int* category_index){ + int i; + for(i=0 ; i<q->num_vectors ; i++){ + ++category[category_index[i]]; + } +} + +/** + * The real requantization of the mltcoefs + * + * @param q pointer to the COOKContext + * @param index index + * @param band current subband + * @param quant_value_table pointer to the array + * @param subband_coef_index array of indexes to quant_centroid_tab + * @param subband_coef_noise use random noise instead of predetermined value + * @param mlt_buffer pointer to the mlt buffer + */ + + +static void scalar_dequant(COOKContext *q, int index, int band, + float* quant_value_table, int* subband_coef_index, + int* subband_coef_noise, float* mlt_buffer){ + int i; + float f1; + + for(i=0 ; i<SUBBAND_SIZE ; i++) { + if (subband_coef_index[i]) { + if (subband_coef_noise[i]) { + f1 = -quant_centroid_tab[index][subband_coef_index[i]]; + } else { + f1 = quant_centroid_tab[index][subband_coef_index[i]]; + } + } else { + /* noise coding if subband_coef_noise[i] == 0 */ + q->random_state = q->random_state * 214013 + 2531011; //typical RNG numbers + f1 = randsign[(q->random_state/0x1000000)&1] * dither_tab[index]; //>>31 + } + mlt_buffer[band*20+ i] = f1 * quant_value_table[band]; + } +} +/** + * Unpack the subband_coef_index and subband_coef_noise vectors. + * + * @param q pointer to the COOKContext + * @param category pointer to the category array + * @param subband_coef_index array of indexes to quant_centroid_tab + * @param subband_coef_noise use random noise instead of predetermined value + */ + +static int unpack_SQVH(COOKContext *q, int category, int* subband_coef_index, + int* subband_coef_noise) { + int i,j; + int vlc, vd ,tmp, result; + int ub; + int cb; + + vd = vd_tab[category]; + result = 0; + for(i=0 ; i<vpr_tab[category] ; i++){ + ub = get_bits_count(&q->gb); + vlc = get_vlc2(&q->gb, q->sqvh[category].table, q->sqvh[category].bits, 3); + cb = get_bits_count(&q->gb); + if (q->bits_per_subpacket < get_bits_count(&q->gb)){ + vlc = 0; + result = 1; + } + for(j=vd-1 ; j>=0 ; j--){ + tmp = (vlc * invradix_tab[category])/0x100000; + subband_coef_index[vd*i+j] = vlc - tmp * (kmax_tab[category]+1); + vlc = tmp; + } + for(j=0 ; j<vd ; j++){ + if (subband_coef_index[i*vd + j]) { + if(get_bits_count(&q->gb) < q->bits_per_subpacket){ + subband_coef_noise[i*vd+j] = get_bits1(&q->gb); + } else { + result=1; + subband_coef_noise[i*vd+j]=0; + } + } else { + subband_coef_noise[i*vd+j]=0; + } + } + } + return result; +} + + +/** + * Fill the mlt_buffer with mlt coefficients. + * + * @param q pointer to the COOKContext + * @param category pointer to the category array + * @param quant_value_table pointer to the array + * @param mlt_buffer pointer to mlt coefficients + */ + + +static void decode_vectors(COOKContext* q, int* category, + float* quant_value_table, float* mlt_buffer){ + /* A zero in this table means that the subband coefficient is + random noise coded. */ + int subband_coef_noise[SUBBAND_SIZE]; + /* A zero in this table means that the subband coefficient is a + positive multiplicator. */ + int subband_coef_index[SUBBAND_SIZE]; + int band, j; + int index=0; + + for(band=0 ; band<q->total_subbands ; band++){ + index = category[band]; + if(category[band] < 7){ + if(unpack_SQVH(q, category[band], subband_coef_index, subband_coef_noise)){ + index=7; + for(j=0 ; j<q->total_subbands ; j++) category[band+j]=7; + } + } + if(index==7) { + memset(subband_coef_index, 0, sizeof(subband_coef_index)); + memset(subband_coef_noise, 0, sizeof(subband_coef_noise)); + } + scalar_dequant(q, index, band, quant_value_table, subband_coef_index, + subband_coef_noise, mlt_buffer); + } + + if(q->total_subbands*SUBBAND_SIZE >= q->samples_per_channel){ + return; + } +} + + +/** + * function for decoding mono data + * + * @param q pointer to the COOKContext + * @param mlt_buffer1 pointer to left channel mlt coefficients + * @param mlt_buffer2 pointer to right channel mlt coefficients + */ + +static void mono_decode(COOKContext *q, float* mlt_buffer) { + + int category_index[128]; + float quant_value_table[102]; + int quant_index_table[102]; + int category[128]; + + memset(&category, 0, 128*sizeof(int)); + memset(&quant_value_table, 0, 102*sizeof(int)); + memset(&category_index, 0, 128*sizeof(int)); + + decode_envelope(q, quant_index_table); + q->num_vectors = get_bits(&q->gb,q->log2_numvector_size); + dequant_envelope(q, quant_index_table, quant_value_table); + categorize(q, quant_index_table, category, category_index); + expand_category(q, category, category_index); + decode_vectors(q, category, quant_value_table, mlt_buffer); +} + + +/** + * The modulated lapped transform, this takes transform coefficients + * and transforms them into timedomain samples. This is done through + * an FFT-based algorithm with pre- and postrotation steps. + * A window and reorder step is also included. + * + * @param q pointer to the COOKContext + * @param inbuffer pointer to the mltcoefficients + * @param outbuffer pointer to the timedomain buffer + * @param mlt_tmp pointer to temporary storage space + */ + +static void cook_imlt(COOKContext *q, float* inbuffer, float* outbuffer, + float* mlt_tmp){ + int i; + + /* prerotation */ + for(i=0 ; i<q->mlt_size ; i+=2){ + outbuffer[i] = (q->mlt_presin[i/2] * inbuffer[q->mlt_size-1-i]) + + (q->mlt_precos[i/2] * inbuffer[i]); + outbuffer[i+1] = (q->mlt_precos[i/2] * inbuffer[q->mlt_size-1-i]) - + (q->mlt_presin[i/2] * inbuffer[i]); + } + + /* FFT */ + ff_fft_permute(&q->fft_ctx, (FFTComplex *) outbuffer); + ff_fft_calc (&q->fft_ctx, (FFTComplex *) outbuffer); + + /* postrotation */ + for(i=0 ; i<q->mlt_size ; i+=2){ + mlt_tmp[i] = (q->mlt_postcos[(q->mlt_size-1-i)/2] * outbuffer[i+1]) + + (q->mlt_postcos[i/2] * outbuffer[i]); + mlt_tmp[q->mlt_size-1-i] = (q->mlt_postcos[(q->mlt_size-1-i)/2] * outbuffer[i]) - + (q->mlt_postcos[i/2] * outbuffer[i+1]); + } + + /* window and reorder */ + for(i=0 ; i<q->mlt_size/2 ; i++){ + outbuffer[i] = mlt_tmp[q->mlt_size/2-1-i] * q->mlt_window[i]; + outbuffer[q->mlt_size-1-i]= mlt_tmp[q->mlt_size/2-1-i] * + q->mlt_window[q->mlt_size-1-i]; + outbuffer[q->mlt_size+i]= mlt_tmp[q->mlt_size/2+i] * + q->mlt_window[q->mlt_size-1-i]; + outbuffer[2*q->mlt_size-1-i]= -(mlt_tmp[q->mlt_size/2+i] * + q->mlt_window[i]); + } +} + + +/** + * the actual requantization of the timedomain samples + * + * @param q pointer to the COOKContext + * @param buffer pointer to the timedomain buffer + * @param gain_index index for the block multiplier + * @param gain_index_next index for the next block multiplier + */ + +static void interpolate(COOKContext *q, float* buffer, + int gain_index, int gain_index_next){ + int i; + float fc1, fc2; + fc1 = q->pow2tab[gain_index+63]; + + if(gain_index == gain_index_next){ //static gain + for(i=0 ; i<q->gain_size_factor ; i++){ + buffer[i]*=fc1; + } + return; + } else { //smooth gain + fc2 = q->gain_table[11 + (gain_index_next-gain_index)]; + for(i=0 ; i<q->gain_size_factor ; i++){ + buffer[i]*=fc1; + fc1*=fc2; + } + return; + } +} + +/** + * timedomain requantization of the timedomain samples + * + * @param q pointer to the COOKContext + * @param buffer pointer to the timedomain buffer + * @param gain_now current gain structure + * @param gain_previous previous gain structure + */ + +static void gain_window(COOKContext *q, float* buffer, COOKgain* gain_now, + COOKgain* gain_previous){ + int i, index; + int gain_index[9]; + int tmp_gain_index; + + gain_index[8]=0; + index = gain_previous->size; + for (i=7 ; i>=0 ; i--) { + if(index && gain_previous->qidx_table1[index-1]==i) { + gain_index[i] = gain_previous->qidx_table2[index-1]; + index--; + } else { + gain_index[i]=gain_index[i+1]; + } + } + /* This is applied to the to be previous data buffer. */ + for(i=0;i<8;i++){ + interpolate(q, &buffer[q->samples_per_channel+q->gain_size_factor*i], + gain_index[i], gain_index[i+1]); + } + + tmp_gain_index = gain_index[0]; + index = gain_now->size; + for (i=7 ; i>=0 ; i--) { + if(index && gain_now->qidx_table1[index-1]==i) { + gain_index[i]= gain_now->qidx_table2[index-1]; + index--; + } else { + gain_index[i]=gain_index[i+1]; + } + } + + /* This is applied to the to be current block. */ + for(i=0;i<8;i++){ + interpolate(q, &buffer[i*q->gain_size_factor], + tmp_gain_index+gain_index[i], + tmp_gain_index+gain_index[i+1]); + } +} + + +/** + * mlt overlapping and buffer management + * + * @param q pointer to the COOKContext + * @param buffer pointer to the timedomain buffer + * @param gain_now current gain structure + * @param gain_previous previous gain structure + * @param previous_buffer pointer to the previous buffer to be used for overlapping + * + */ + +static void gain_compensate(COOKContext *q, float* buffer, COOKgain* gain_now, + COOKgain* gain_previous, float* previous_buffer) { + int i; + if((gain_now->size || gain_previous->size)) { + gain_window(q, buffer, gain_now, gain_previous); + } + + /* Overlap with the previous block. */ + for(i=0 ; i<q->samples_per_channel ; i++) buffer[i]+=previous_buffer[i]; + + /* Save away the current to be previous block. */ + memcpy(previous_buffer, buffer+q->samples_per_channel, + sizeof(float)*q->samples_per_channel); +} + + +/** + * function for getting the jointstereo coupling information + * + * @param q pointer to the COOKContext + * @param decouple_tab decoupling array + * + */ + +static void decouple_info(COOKContext *q, int* decouple_tab){ + int length, i; + + if(get_bits1(&q->gb)) { + if(cplband[q->js_subband_start] > cplband[q->subbands-1]) return; + + length = cplband[q->subbands-1] - cplband[q->js_subband_start] + 1; + for (i=0 ; i<length ; i++) { + decouple_tab[cplband[q->js_subband_start] + i] = get_vlc2(&q->gb, q->ccpl.table, q->ccpl.bits, 2); + } + return; + } + + if(cplband[q->js_subband_start] > cplband[q->subbands-1]) return; + + length = cplband[q->subbands-1] - cplband[q->js_subband_start] + 1; + for (i=0 ; i<length ; i++) { + decouple_tab[cplband[q->js_subband_start] + i] = get_bits(&q->gb, q->js_vlc_bits); + } + return; +} + + +/** + * function for decoding joint stereo data + * + * @param q pointer to the COOKContext + * @param mlt_buffer1 pointer to left channel mlt coefficients + * @param mlt_buffer2 pointer to right channel mlt coefficients + */ + +static void joint_decode(COOKContext *q, float* mlt_buffer1, + float* mlt_buffer2) { + int i,j; + int decouple_tab[SUBBAND_SIZE]; + float decode_buffer[1060]; + int idx, cpl_tmp,tmp_idx; + float f1,f2; + float* cplscale; + + memset(decouple_tab, 0, sizeof(decouple_tab)); + memset(decode_buffer, 0, sizeof(decode_buffer)); + + /* Make sure the buffers are zeroed out. */ + memset(mlt_buffer1,0, 1024*sizeof(float)); + memset(mlt_buffer2,0, 1024*sizeof(float)); + decouple_info(q, decouple_tab); + mono_decode(q, decode_buffer); + + /* The two channels are stored interleaved in decode_buffer. */ + for (i=0 ; i<q->js_subband_start ; i++) { + for (j=0 ; j<SUBBAND_SIZE ; j++) { + mlt_buffer1[i*20+j] = decode_buffer[i*40+j]; + mlt_buffer2[i*20+j] = decode_buffer[i*40+20+j]; + } + } + + /* When we reach js_subband_start (the higher frequencies) + the coefficients are stored in a coupling scheme. */ + idx = (1 << q->js_vlc_bits) - 1; + for (i=q->js_subband_start ; i<q->subbands ; i++) { + cpl_tmp = cplband[i]; + idx -=decouple_tab[cpl_tmp]; + cplscale = (float*)cplscales[q->js_vlc_bits-2]; //choose decoupler table + f1 = cplscale[decouple_tab[cpl_tmp]]; + f2 = cplscale[idx-1]; + for (j=0 ; j<SUBBAND_SIZE ; j++) { + tmp_idx = ((q->js_subband_start + i)*20)+j; + mlt_buffer1[20*i + j] = f1 * decode_buffer[tmp_idx]; + mlt_buffer2[20*i + j] = f2 * decode_buffer[tmp_idx]; + } + idx = (1 << q->js_vlc_bits) - 1; + } +} + +/** + * Cook subpacket decoding. This function returns one decoded subpacket, + * usually 1024 samples per channel. + * + * @param q pointer to the COOKContext + * @param inbuffer pointer to the inbuffer + * @param sub_packet_size subpacket size + * @param outbuffer pointer to the outbuffer + */ + + +static int decode_subpacket(COOKContext *q, uint8_t *inbuffer, + int sub_packet_size, int16_t *outbuffer) { + int i,j; + int value; + float* tmp_ptr; + + /* packet dump */ +// for (i=0 ; i<sub_packet_size ; i++) { +// av_log(NULL, AV_LOG_ERROR, "%02x", inbuffer[i]); +// } +// av_log(NULL, AV_LOG_ERROR, "\n"); + + decode_bytes(inbuffer, q->decoded_bytes_buffer, sub_packet_size); + init_get_bits(&q->gb, q->decoded_bytes_buffer, sub_packet_size*8); + decode_gain_info(&q->gb, &q->gain_current); + + if(q->nb_channels==2 && q->joint_stereo==1){ + joint_decode(q, q->decode_buf_ptr[0], q->decode_buf_ptr[2]); + + /* Swap buffer pointers. */ + tmp_ptr = q->decode_buf_ptr[1]; + q->decode_buf_ptr[1] = q->decode_buf_ptr[0]; + q->decode_buf_ptr[0] = tmp_ptr; + tmp_ptr = q->decode_buf_ptr[3]; + q->decode_buf_ptr[3] = q->decode_buf_ptr[2]; + q->decode_buf_ptr[2] = tmp_ptr; + + /* FIXME: Rethink the gainbuffer handling, maybe a rename? + now/previous swap */ + q->gain_now_ptr = &q->gain_now; + q->gain_previous_ptr = &q->gain_previous; + for (i=0 ; i<q->nb_channels ; i++){ + + cook_imlt(q, q->decode_buf_ptr[i*2], q->mono_mdct_output, q->mlt_tmp); + gain_compensate(q, q->mono_mdct_output, q->gain_now_ptr, + q->gain_previous_ptr, q->previous_buffer_ptr[0]); + + /* Swap out the previous buffer. */ + tmp_ptr = q->previous_buffer_ptr[0]; + q->previous_buffer_ptr[0] = q->previous_buffer_ptr[1]; + q->previous_buffer_ptr[1] = tmp_ptr; + + /* Clip and convert the floats to 16 bits. */ + for (j=0 ; j<q->samples_per_frame ; j++){ + value = lrintf(q->mono_mdct_output[j]); + if(value < -32768) value = -32768; + else if(value > 32767) value = 32767; + outbuffer[2*j+i] = value; + } + } + + memcpy(&q->gain_now, &q->gain_previous, sizeof(COOKgain)); + memcpy(&q->gain_previous, &q->gain_current, sizeof(COOKgain)); + + } else if (q->nb_channels==2 && q->joint_stereo==0) { + /* channel 0 */ + mono_decode(q, q->decode_buf_ptr2[0]); + + tmp_ptr = q->decode_buf_ptr2[0]; + q->decode_buf_ptr2[0] = q->decode_buf_ptr2[1]; + q->decode_buf_ptr2[1] = tmp_ptr; + + memcpy(&q->gain_channel1[0], &q->gain_current ,sizeof(COOKgain)); + q->gain_now_ptr = &q->gain_channel1[0]; + q->gain_previous_ptr = &q->gain_channel1[1]; + + cook_imlt(q, q->decode_buf_ptr2[0], q->mono_mdct_output,q->mlt_tmp); + gain_compensate(q, q->mono_mdct_output, q->gain_now_ptr, + q->gain_previous_ptr, q->mono_previous_buffer1); + + memcpy(&q->gain_channel1[1], &q->gain_channel1[0],sizeof(COOKgain)); + + + for (j=0 ; j<q->samples_per_frame ; j++){ + value = lrintf(q->mono_mdct_output[j]); + if(value < -32768) value = -32768; + else if(value > 32767) value = 32767; + outbuffer[2*j+1] = value; + } + + /* channel 1 */ + //av_log(NULL,AV_LOG_ERROR,"bits = %d\n",get_bits_count(&q->gb)); + init_get_bits(&q->gb, q->decoded_bytes_buffer, sub_packet_size*8+q->bits_per_subpacket); + + q->gain_now_ptr = &q->gain_channel2[0]; + q->gain_previous_ptr = &q->gain_channel2[1]; + + decode_gain_info(&q->gb, &q->gain_channel2[0]); + mono_decode(q, q->decode_buf_ptr[0]); + + tmp_ptr = q->decode_buf_ptr[0]; + q->decode_buf_ptr[0] = q->decode_buf_ptr[1]; + q->decode_buf_ptr[1] = tmp_ptr; + + cook_imlt(q, q->decode_buf_ptr[0], q->mono_mdct_output,q->mlt_tmp); + gain_compensate(q, q->mono_mdct_output, q->gain_now_ptr, + q->gain_previous_ptr, q->mono_previous_buffer2); + + /* Swap out the previous buffer. */ + tmp_ptr = q->previous_buffer_ptr[0]; + q->previous_buffer_ptr[0] = q->previous_buffer_ptr[1]; + q->previous_buffer_ptr[1] = tmp_ptr; + + memcpy(&q->gain_channel2[1], &q->gain_channel2[0] ,sizeof(COOKgain)); + + for (j=0 ; j<q->samples_per_frame ; j++){ + value = lrintf(q->mono_mdct_output[j]); + if(value < -32768) value = -32768; + else if(value > 32767) value = 32767; + outbuffer[2*j] = value; + } + + } else { + mono_decode(q, q->decode_buf_ptr[0]); + + /* Swap buffer pointers. */ + tmp_ptr = q->decode_buf_ptr[1]; + q->decode_buf_ptr[1] = q->decode_buf_ptr[0]; + q->decode_buf_ptr[0] = tmp_ptr; + + /* FIXME: Rethink the gainbuffer handling, maybe a rename? + now/previous swap */ + q->gain_now_ptr = &q->gain_now; + q->gain_previous_ptr = &q->gain_previous; + + cook_imlt(q, q->decode_buf_ptr[0], q->mono_mdct_output,q->mlt_tmp); + gain_compensate(q, q->mono_mdct_output, q->gain_now_ptr, + q->gain_previous_ptr, q->mono_previous_buffer1); + + /* Clip and convert the floats to 16 bits */ + for (j=0 ; j<q->samples_per_frame ; j++){ + value = lrintf(q->mono_mdct_output[j]); + if(value < -32768) value = -32768; + else if(value > 32767) value = 32767; + outbuffer[j] = value; + } + memcpy(&q->gain_now, &q->gain_previous, sizeof(COOKgain)); + memcpy(&q->gain_previous, &q->gain_current, sizeof(COOKgain)); + } + return q->samples_per_frame * sizeof(int16_t); +} + + +/** + * Cook frame decoding + * + * @param avctx pointer to the AVCodecContext + */ + +static int cook_decode_frame(AVCodecContext *avctx, + void *data, int *data_size, + uint8_t *buf, int buf_size) { + COOKContext *q = avctx->priv_data; + + if (buf_size < avctx->block_align) + return buf_size; + + *data_size = decode_subpacket(q, buf, avctx->block_align, data); + + return avctx->block_align; +} + +#ifdef COOKDEBUG +static void dump_cook_context(COOKContext *q, COOKextradata *e) +{ + //int i=0; +#define PRINT(a,b) av_log(NULL,AV_LOG_ERROR," %s = %d\n", a, b); + av_log(NULL,AV_LOG_ERROR,"COOKextradata\n"); + av_log(NULL,AV_LOG_ERROR,"cookversion=%x\n",e->cookversion); + if (e->cookversion > MONO_COOK2) { + PRINT("js_subband_start",e->js_subband_start); + PRINT("js_vlc_bits",e->js_vlc_bits); + } + av_log(NULL,AV_LOG_ERROR,"COOKContext\n"); + PRINT("nb_channels",q->nb_channels); + PRINT("bit_rate",q->bit_rate); + PRINT("sample_rate",q->sample_rate); + PRINT("samples_per_channel",q->samples_per_channel); + PRINT("samples_per_frame",q->samples_per_frame); + PRINT("subbands",q->subbands); + PRINT("random_state",q->random_state); + PRINT("mlt_size",q->mlt_size); + PRINT("js_subband_start",q->js_subband_start); + PRINT("log2_numvector_size",q->log2_numvector_size); + PRINT("numvector_size",q->numvector_size); + PRINT("total_subbands",q->total_subbands); +} +#endif + +/** + * Cook initialization + * + * @param avctx pointer to the AVCodecContext + */ + +static int cook_decode_init(AVCodecContext *avctx) +{ + COOKextradata *e = avctx->extradata; + COOKContext *q = avctx->priv_data; + + /* Take care of the codec specific extradata. */ + if (avctx->extradata_size <= 0) { + av_log(NULL,AV_LOG_ERROR,"Necessary extradata missing!\n"); + return -1; + } else { + /* 8 for mono, 16 for stereo, ? for multichannel + Swap to right endianness so we don't need to care later on. */ + av_log(NULL,AV_LOG_DEBUG,"codecdata_length=%d\n",avctx->extradata_size); + if (avctx->extradata_size >= 8){ + e->cookversion = be2me_32(e->cookversion); + e->samples_per_frame = be2me_16(e->samples_per_frame); + e->subbands = be2me_16(e->subbands); + } + if (avctx->extradata_size >= 16){ + e->js_subband_start = be2me_16(e->js_subband_start); + e->js_vlc_bits = be2me_16(e->js_vlc_bits); + } + } + + /* Take data from the AVCodecContext (RM container). */ + q->sample_rate = avctx->sample_rate; + q->nb_channels = avctx->channels; + q->bit_rate = avctx->bit_rate; + + /* Initialize state. */ + q->random_state = 1; + + /* Initialize extradata related variables. */ + q->samples_per_channel = e->samples_per_frame / q->nb_channels; + q->samples_per_frame = e->samples_per_frame; + q->subbands = e->subbands; + q->bits_per_subpacket = avctx->block_align * 8; + + /* Initialize default data states. */ + q->js_subband_start = 0; + q->log2_numvector_size = 5; + q->total_subbands = q->subbands; + + /* Initialize version-dependent variables */ + av_log(NULL,AV_LOG_DEBUG,"e->cookversion=%x\n",e->cookversion); + switch (e->cookversion) { + case MONO_COOK1: + if (q->nb_channels != 1) { + av_log(NULL,AV_LOG_ERROR,"Container channels != 1, report sample!\n"); + return -1; + } + av_log(NULL,AV_LOG_DEBUG,"MONO_COOK1\n"); + break; + case MONO_COOK2: + if (q->nb_channels != 1) { + q->joint_stereo = 0; + q->bits_per_subpacket = q->bits_per_subpacket/2; + } + av_log(NULL,AV_LOG_DEBUG,"MONO_COOK2\n"); + break; + case JOINT_STEREO: + if (q->nb_channels != 2) { + av_log(NULL,AV_LOG_ERROR,"Container channels != 2, report sample!\n"); + return -1; + } + av_log(NULL,AV_LOG_DEBUG,"JOINT_STEREO\n"); + if (avctx->extradata_size >= 16){ + q->total_subbands = q->subbands + e->js_subband_start; + q->js_subband_start = e->js_subband_start; + q->joint_stereo = 1; + q->js_vlc_bits = e->js_vlc_bits; + } + if (q->samples_per_channel > 256) { + q->log2_numvector_size = 6; + } + if (q->samples_per_channel > 512) { + q->log2_numvector_size = 7; + } + break; + case MC_COOK: + av_log(NULL,AV_LOG_ERROR,"MC_COOK not supported!\n"); + return -1; + break; + default: + av_log(NULL,AV_LOG_ERROR,"Unknown Cook version, report sample!\n"); + return -1; + break; + } + + /* Initialize variable relations */ + q->mlt_size = q->samples_per_channel; + q->numvector_size = (1 << q->log2_numvector_size); + + /* Generate tables */ + init_rootpow2table(q); + init_pow2table(q); + init_gain_table(q); + + if (init_cook_vlc_tables(q) != 0) + return -1; + + + if(avctx->block_align >= UINT_MAX/2) + return -1; + + /* Pad the databuffer with FF_INPUT_BUFFER_PADDING_SIZE, + this is for the bitstreamreader. */ + if ((q->decoded_bytes_buffer = av_mallocz((avctx->block_align+(4-avctx->block_align%4) + FF_INPUT_BUFFER_PADDING_SIZE)*sizeof(uint8_t))) == NULL) + return -1; + + q->decode_buf_ptr[0] = q->decode_buffer_1; + q->decode_buf_ptr[1] = q->decode_buffer_2; + q->decode_buf_ptr[2] = q->decode_buffer_3; + q->decode_buf_ptr[3] = q->decode_buffer_4; + + q->decode_buf_ptr2[0] = q->decode_buffer_3; + q->decode_buf_ptr2[1] = q->decode_buffer_4; + + q->previous_buffer_ptr[0] = q->mono_previous_buffer1; + q->previous_buffer_ptr[1] = q->mono_previous_buffer2; + + /* Initialize transform. */ + if ( init_cook_mlt(q) == 0 ) + return -1; + + /* Try to catch some obviously faulty streams, othervise it might be exploitable */ + if (q->total_subbands > 53) { + av_log(NULL,AV_LOG_ERROR,"total_subbands > 53, report sample!\n"); + return -1; + } + if (q->subbands > 50) { + av_log(NULL,AV_LOG_ERROR,"subbands > 50, report sample!\n"); + return -1; + } + if ((q->samples_per_channel == 256) || (q->samples_per_channel == 512) || (q->samples_per_channel == 1024)) { + } else { + av_log(NULL,AV_LOG_ERROR,"unknown amount of samples_per_channel = %d, report sample!\n",q->samples_per_channel); + return -1; + } + +#ifdef COOKDEBUG + dump_cook_context(q,e); +#endif + return 0; +} + + +AVCodec cook_decoder = +{ + .name = "cook", + .type = CODEC_TYPE_AUDIO, + .id = CODEC_ID_COOK, + .priv_data_size = sizeof(COOKContext), + .init = cook_decode_init, + .close = cook_decode_close, + .decode = cook_decode_frame, +}; diff --git a/src/libffmpeg/libavcodec/cookdata.h b/src/libffmpeg/libavcodec/cookdata.h new file mode 100644 index 000000000..1247d9d91 --- /dev/null +++ b/src/libffmpeg/libavcodec/cookdata.h @@ -0,0 +1,557 @@ +/* + * COOK compatible decoder data + * Copyright (c) 2003 Sascha Sommer + * Copyright (c) 2005 Benjamin Larsson + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +/** + * @file cookdata.h + * Cook AKA RealAudio G2 compatible decoderdata + */ + +/* various data tables */ + +static const int expbits_tab[8] = { + 52,47,43,37,29,22,16,0, +}; + +static const float dither_tab[8] = { + 0.0, 0.0, 0.0, 0.0, 0.0, 0.176777, 0.25, 0.707107, +}; + +static const float randsign[2] = {1.0, -1.0}; + +static const float quant_centroid_tab[7][14] = { + { 0.000, 0.392, 0.761, 1.120, 1.477, 1.832, 2.183, 2.541, 2.893, 3.245, 3.598, 3.942, 4.288, 4.724 }, + { 0.000, 0.544, 1.060, 1.563, 2.068, 2.571, 3.072, 3.562, 4.070, 4.620, 0.000, 0.000, 0.000, 0.000 }, + { 0.000, 0.746, 1.464, 2.180, 2.882, 3.584, 4.316, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000 }, + { 0.000, 1.006, 2.000, 2.993, 3.985, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000 }, + { 0.000, 1.321, 2.703, 3.983, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000 }, + { 0.000, 1.657, 3.491, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000 }, + { 0.000, 1.964, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000 } +}; + +static const int invradix_tab[7] = { + 74899, 104858, 149797, 209716, 262144, 349526, 524288, +}; + +static const int kmax_tab[7] = { + 13, 9, 6, 4, 3, 2, 1, +}; + +static const int vd_tab[7] = { + 2, 2, 2, 4, 4, 5, 5, +}; + +static const int vpr_tab[7] = { + 10, 10, 10, 5, 5, 4, 4, +}; + + + +/* VLC data */ + +static const int vhsize_tab[7] = { + 191, 97, 48, 607, 246, 230, 32, +}; + +static const int vhvlcsize_tab[7] = { + 8, 7, 7, 10, 9, 9, 6, +}; + +static const uint8_t envelope_quant_index_huffbits[13][24] = { + { 4, 6, 5, 5, 4, 4, 4, 4, 4, 4, 3, 3, 3, 4, 5, 7, 8, 9, 11, 11, 12, 12, 12, 12 }, + { 10, 8, 6, 5, 5, 4, 3, 3, 3, 3, 3, 3, 4, 5, 7, 9, 11, 12, 13, 15, 15, 15, 16, 16 }, + { 12, 10, 8, 6, 5, 4, 4, 4, 4, 4, 4, 3, 3, 3, 4, 4, 5, 5, 7, 9, 11, 13, 14, 14 }, + { 13, 10, 9, 9, 7, 7, 5, 5, 4, 3, 3, 3, 3, 3, 4, 4, 4, 5, 7, 9, 11, 13, 13, 13 }, + { 12, 13, 10, 8, 6, 6, 5, 5, 4, 4, 3, 3, 3, 3, 3, 4, 5, 5, 6, 7, 9, 11, 14, 14 }, + { 12, 11, 9, 8, 8, 7, 5, 4, 4, 3, 3, 3, 3, 3, 4, 4, 5, 5, 7, 8, 10, 13, 14, 14 }, + { 15, 16, 15, 12, 10, 8, 6, 5, 4, 3, 3, 3, 2, 3, 4, 5, 5, 7, 9, 11, 13, 16, 16, 16 }, + { 14, 14, 11, 10, 9, 7, 7, 5, 5, 4, 3, 3, 2, 3, 3, 4, 5, 7, 9, 9, 12, 14, 15, 15 }, + { 9, 9, 9, 8, 7, 6, 5, 4, 3, 3, 3, 3, 3, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 13 }, + { 14, 12, 10, 8, 6, 6, 5, 4, 3, 3, 3, 3, 3, 3, 4, 5, 6, 8, 8, 9, 11, 14, 14, 14 }, + { 13, 10, 9, 8, 6, 6, 5, 4, 4, 4, 3, 3, 2, 3, 4, 5, 6, 8, 9, 9, 11, 12, 14, 14 }, + { 16, 13, 12, 11, 9, 6, 5, 5, 4, 4, 4, 3, 2, 3, 3, 4, 5, 7, 8, 10, 14, 16, 16, 16 }, + { 13, 14, 14, 14, 10, 8, 7, 7, 5, 4, 3, 3, 2, 3, 3, 4, 5, 5, 7, 9, 11, 14, 14, 14 }, +}; + +static const uint16_t envelope_quant_index_huffcodes[13][24] = { + {0x0006, 0x003e, 0x001c, 0x001d, 0x0007, 0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x0000, 0x0001, + 0x0002, 0x000d, 0x001e, 0x007e, 0x00fe, 0x01fe, 0x07fc, 0x07fd, 0x0ffc, 0x0ffd, 0x0ffe, 0x0fff}, + {0x03fe, 0x00fe, 0x003e, 0x001c, 0x001d, 0x000c, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, + 0x000d, 0x001e, 0x007e, 0x01fe, 0x07fe, 0x0ffe, 0x1ffe, 0x7ffc, 0x7ffd, 0x7ffe, 0xfffe, 0xffff}, + {0x0ffe, 0x03fe, 0x00fe, 0x003e, 0x001c, 0x0006, 0x0007, 0x0008, 0x0009, 0x000a, 0x000b, 0x0000, + 0x0001, 0x0002, 0x000c, 0x000d, 0x001d, 0x001e, 0x007e, 0x01fe, 0x07fe, 0x1ffe, 0x3ffe, 0x3fff}, + {0x1ffc, 0x03fe, 0x01fc, 0x01fd, 0x007c, 0x007d, 0x001c, 0x001d, 0x000a, 0x0000, 0x0001, 0x0002, + 0x0003, 0x0004, 0x000b, 0x000c, 0x000d, 0x001e, 0x007e, 0x01fe, 0x07fe, 0x1ffd, 0x1ffe, 0x1fff}, + {0x0ffe, 0x1ffe, 0x03fe, 0x00fe, 0x003c, 0x003d, 0x001a, 0x001b, 0x000a, 0x000b, 0x0000, 0x0001, + 0x0002, 0x0003, 0x0004, 0x000c, 0x001c, 0x001d, 0x003e, 0x007e, 0x01fe, 0x07fe, 0x3ffe, 0x3fff}, + {0x0ffe, 0x07fe, 0x01fe, 0x00fc, 0x00fd, 0x007c, 0x001c, 0x000a, 0x000b, 0x0000, 0x0001, 0x0002, + 0x0003, 0x0004, 0x000c, 0x000d, 0x001d, 0x001e, 0x007d, 0x00fe, 0x03fe, 0x1ffe, 0x3ffe, 0x3fff}, + {0x7ffc, 0xfffc, 0x7ffd, 0x0ffe, 0x03fe, 0x00fe, 0x003e, 0x001c, 0x000c, 0x0002, 0x0003, 0x0004, + 0x0000, 0x0005, 0x000d, 0x001d, 0x001e, 0x007e, 0x01fe, 0x07fe, 0x1ffe, 0xfffd, 0xfffe, 0xffff}, + {0x3ffc, 0x3ffd, 0x07fe, 0x03fe, 0x01fc, 0x007c, 0x007d, 0x001c, 0x001d, 0x000c, 0x0002, 0x0003, + 0x0000, 0x0004, 0x0005, 0x000d, 0x001e, 0x007e, 0x01fd, 0x01fe, 0x0ffe, 0x3ffe, 0x7ffe, 0x7fff}, + {0x01fc, 0x01fd, 0x01fe, 0x00fc, 0x007c, 0x003c, 0x001c, 0x000c, 0x0000, 0x0001, 0x0002, 0x0003, + 0x0004, 0x0005, 0x000d, 0x001d, 0x003d, 0x007d, 0x00fd, 0x03fe, 0x07fe, 0x0ffe, 0x1ffe, 0x1fff}, + {0x3ffc, 0x0ffe, 0x03fe, 0x00fc, 0x003c, 0x003d, 0x001c, 0x000c, 0x0000, 0x0001, 0x0002, 0x0003, + 0x0004, 0x0005, 0x000d, 0x001d, 0x003e, 0x00fd, 0x00fe, 0x01fe, 0x07fe, 0x3ffd, 0x3ffe, 0x3fff}, + {0x1ffe, 0x03fe, 0x01fc, 0x00fc, 0x003c, 0x003d, 0x001c, 0x000a, 0x000b, 0x000c, 0x0002, 0x0003, + 0x0000, 0x0004, 0x000d, 0x001d, 0x003e, 0x00fd, 0x01fd, 0x01fe, 0x07fe, 0x0ffe, 0x3ffe, 0x3fff}, + {0xfffc, 0x1ffe, 0x0ffe, 0x07fe, 0x01fe, 0x003e, 0x001c, 0x001d, 0x000a, 0x000b, 0x000c, 0x0002, + 0x0000, 0x0003, 0x0004, 0x000d, 0x001e, 0x007e, 0x00fe, 0x03fe, 0x3ffe, 0xfffd, 0xfffe, 0xffff}, + {0x1ffc, 0x3ffa, 0x3ffb, 0x3ffc, 0x03fe, 0x00fe, 0x007c, 0x007d, 0x001c, 0x000c, 0x0002, 0x0003, + 0x0000, 0x0004, 0x0005, 0x000d, 0x001d, 0x001e, 0x007e, 0x01fe, 0x07fe, 0x3ffd, 0x3ffe, 0x3fff}, +}; + + +static const uint8_t cvh_huffbits0[191] = { + 1, 4, 6, 6, 7, 7, 8, 8, 8, 9, 9, 10, + 11, 11, 4, 5, 6, 7, 7, 8, 8, 9, 9, 9, + 9, 10, 11, 11, 5, 6, 7, 8, 8, 9, 9, 9, + 9, 10, 10, 10, 11, 12, 6, 7, 8, 9, 9, 9, + 9, 10, 10, 10, 10, 11, 12, 13, 7, 7, 8, 9, + 9, 9, 10, 10, 10, 10, 11, 11, 12, 13, 8, 8, + 9, 9, 9, 10, 10, 10, 10, 11, 11, 12, 13, 14, + 8, 8, 9, 9, 10, 10, 11, 11, 11, 12, 12, 13, + 13, 15, 8, 8, 9, 9, 10, 10, 11, 11, 11, 12, + 12, 13, 14, 15, 9, 9, 9, 10, 10, 10, 11, 11, + 12, 13, 12, 14, 15, 16, 9, 9, 10, 10, 10, 10, + 11, 12, 12, 14, 14, 16, 16, 0, 9, 9, 10, 10, + 11, 11, 12, 13, 13, 14, 14, 15, 0, 0, 10, 10, + 10, 11, 11, 12, 12, 13, 15, 15, 16, 0, 0, 0, + 11, 11, 11, 12, 13, 13, 13, 15, 16, 16, 0, 0, + 0, 0, 11, 11, 12, 13, 13, 14, 15, 16, 16, +}; + +static const uint16_t cvh_huffcodes0[191] = { + 0x0000,0x0008,0x002c,0x002d,0x0062,0x0063,0x00d4,0x00d5,0x00d6,0x01c6,0x01c7,0x03ca, + 0x07d6,0x07d7,0x0009,0x0014,0x002e,0x0064,0x0065,0x00d7,0x00d8,0x01c8,0x01c9,0x01ca, + 0x01cb,0x03cb,0x07d8,0x07d9,0x0015,0x002f,0x0066,0x00d9,0x00da,0x01cc,0x01cd,0x01ce, + 0x01cf,0x03cc,0x03cd,0x03ce,0x07da,0x0fe4,0x0030,0x0067,0x00db,0x01d0,0x01d1,0x01d2, + 0x01d3,0x03cf,0x03d0,0x03d1,0x03d2,0x07db,0x0fe5,0x1fea,0x0068,0x0069,0x00dc,0x01d4, + 0x01d5,0x01d6,0x03d3,0x03d4,0x03d5,0x03d6,0x07dc,0x07dd,0x0fe6,0x1feb,0x00dd,0x00de, + 0x01d7,0x01d8,0x01d9,0x03d7,0x03d8,0x03d9,0x03da,0x07de,0x07df,0x0fe7,0x1fec,0x3ff2, + 0x00df,0x00e0,0x01da,0x01db,0x03db,0x03dc,0x07e0,0x07e1,0x07e2,0x0fe8,0x0fe9,0x1fed, + 0x1fee,0x7ff4,0x00e1,0x00e2,0x01dc,0x01dd,0x03dd,0x03de,0x07e3,0x07e4,0x07e5,0x0fea, + 0x0feb,0x1fef,0x3ff3,0x7ff5,0x01de,0x01df,0x01e0,0x03df,0x03e0,0x03e1,0x07e6,0x07e7, + 0x0fec,0x1ff0,0x0fed,0x3ff4,0x7ff6,0xfff8,0x01e1,0x01e2,0x03e2,0x03e3,0x03e4,0x03e5, + 0x07e8,0x0fee,0x0fef,0x3ff5,0x3ff6,0xfff9,0xfffa,0xfffa,0x01e3,0x01e4,0x03e6,0x03e7, + 0x07e9,0x07ea,0x0ff0,0x1ff1,0x1ff2,0x3ff7,0x3ff8,0x7ff7,0x7ff7,0xfffa,0x03e8,0x03e9, + 0x03ea,0x07eb,0x07ec,0x0ff1,0x0ff2,0x1ff3,0x7ff8,0x7ff9,0xfffb,0x3ff8,0x7ff7,0x7ff7, + 0x07ed,0x07ee,0x07ef,0x0ff3,0x1ff4,0x1ff5,0x1ff6,0x7ffa,0xfffc,0xfffd,0xfffb,0xfffb, + 0x3ff8,0x7ff7,0x07f0,0x07f1,0x0ff4,0x1ff7,0x1ff8,0x3ff9,0x7ffb,0xfffe,0xffff, +}; + + +static const uint8_t cvh_huffbits1[97] = { + 1, 4, 5, 6, 7, 8, 8, 9, 10, 10, 4, 5, + 6, 7, 7, 8, 8, 9, 9, 11, 5, 5, 6, 7, + 8, 8, 9, 9, 10, 11, 6, 6, 7, 8, 8, 9, + 9, 10, 11, 12, 7, 7, 8, 8, 9, 9, 10, 11, + 11, 13, 8, 8, 8, 9, 9, 10, 10, 11, 12, 14, + 8, 8, 8, 9, 10, 11, 11, 12, 13, 15, 9, 9, + 9, 10, 11, 12, 12, 14, 14, 0, 9, 9, 9, 10, + 11, 12, 14, 16, 0, 0, 10, 10, 11, 12, 13, 14, + 16, +}; + + +static const uint16_t cvh_huffcodes1[97] = { + 0x0000,0x0008,0x0014,0x0030,0x006a,0x00e2,0x00e3,0x01e4,0x03ec,0x03ed,0x0009,0x0015, + 0x0031,0x006b,0x006c,0x00e4,0x00e5,0x01e5,0x01e6,0x07f0,0x0016,0x0017,0x0032,0x006d, + 0x00e6,0x00e7,0x01e7,0x01e8,0x03ee,0x07f1,0x0033,0x0034,0x006e,0x00e8,0x00e9,0x01e9, + 0x01ea,0x03ef,0x07f2,0x0ff6,0x006f,0x0070,0x00ea,0x00eb,0x01eb,0x01ec,0x03f0,0x07f3, + 0x07f4,0x1ffa,0x00ec,0x00ed,0x00ee,0x01ed,0x01ee,0x03f1,0x03f2,0x07f5,0x0ff7,0x3ffa, + 0x00ef,0x00f0,0x00f1,0x01ef,0x03f3,0x07f6,0x07f7,0x0ff8,0x1ffb,0x7ffe,0x01f0,0x01f1, + 0x01f2,0x03f4,0x07f8,0x0ff9,0x0ffa,0x3ffb,0x3ffc,0x0000,0x01f3,0x01f4,0x01f5,0x03f5, + 0x07f9,0x0ffb,0x3ffd,0xfffe,0x0000,0x0000,0x03f6,0x03f7,0x07fa,0x0ffc,0x1ffc,0x3ffe, + 0xffff, +}; + +static const uint8_t cvh_huffbits2[48] = { + 1, 4, 5, 7, 8, 9, 10, 3, 4, 5, 7, 8, + 9, 10, 5, 5, 6, 7, 8, 10, 10, 7, 6, 7, + 8, 9, 10, 12, 8, 8, 8, 9, 10, 12, 14, 8, + 9, 9, 10, 11, 15, 16, 9, 10, 11, 12, 13, 16, +}; + +static const uint16_t cvh_huffcodes2[48] = { + 0x0000,0x000a,0x0018,0x0074,0x00f2,0x01f4,0x03f6,0x0004,0x000b,0x0019,0x0075,0x00f3, + 0x01f5,0x03f7,0x001a,0x001b,0x0038,0x0076,0x00f4,0x03f8,0x03f9,0x0077,0x0039,0x0078, + 0x00f5,0x01f6,0x03fa,0x0ffc,0x00f6,0x00f7,0x00f8,0x01f7,0x03fb,0x0ffd,0x3ffe,0x00f9, + 0x01f8,0x01f9,0x03fc,0x07fc,0x7ffe,0xfffe,0x01fa,0x03fd,0x07fd,0x0ffe,0x1ffe,0xffff, +}; + +static const uint8_t cvh_huffbits3[607] = { + 2, 4, 6, 8, 10, 5, 5, 6, 8, 10, 7, 8, + 8, 10, 12, 9, 9, 10, 12, 15, 10, 11, 13, 16, + 16, 5, 6, 8, 10, 11, 5, 6, 8, 10, 12, 7, + 7, 8, 10, 13, 9, 9, 10, 12, 15, 12, 11, 13, + 16, 16, 7, 9, 10, 12, 15, 7, 8, 10, 12, 13, + 9, 9, 11, 13, 16, 11, 11, 12, 14, 16, 12, 12, + 14, 16, 0, 9, 11, 12, 16, 16, 9, 10, 13, 15, + 16, 10, 11, 12, 16, 16, 13, 13, 16, 16, 16, 16, + 16, 15, 16, 0, 11, 13, 16, 16, 15, 11, 13, 15, + 16, 16, 13, 13, 16, 16, 0, 14, 16, 16, 16, 0, + 16, 16, 0, 0, 0, 4, 6, 8, 10, 13, 6, 6, + 8, 10, 13, 9, 8, 10, 12, 16, 10, 10, 11, 15, + 16, 13, 12, 14, 16, 16, 5, 6, 8, 11, 13, 6, + 6, 8, 10, 13, 8, 8, 9, 11, 14, 10, 10, 12, + 12, 16, 13, 12, 13, 15, 16, 7, 8, 9, 12, 16, + 7, 8, 10, 12, 14, 9, 9, 10, 13, 16, 11, 10, + 12, 15, 16, 13, 13, 16, 16, 0, 9, 11, 13, 16, + 16, 9, 10, 12, 15, 16, 10, 11, 13, 16, 16, 13, + 12, 16, 16, 16, 16, 16, 16, 16, 0, 11, 13, 16, + 16, 16, 11, 13, 16, 16, 16, 12, 13, 15, 16, 0, + 16, 16, 16, 16, 0, 16, 16, 0, 0, 0, 6, 8, + 11, 13, 16, 8, 8, 10, 12, 16, 11, 10, 11, 13, + 16, 12, 13, 13, 15, 16, 16, 16, 14, 16, 0, 6, + 8, 10, 13, 16, 8, 8, 10, 12, 16, 10, 10, 11, + 13, 16, 13, 12, 13, 16, 16, 14, 14, 14, 16, 0, + 8, 9, 11, 13, 16, 8, 9, 11, 16, 14, 10, 10, + 12, 15, 16, 12, 12, 13, 16, 16, 15, 16, 16, 16, + 0, 10, 12, 15, 16, 16, 10, 12, 12, 14, 16, 12, + 12, 13, 16, 16, 14, 15, 16, 16, 0, 16, 16, 16, + 0, 0, 12, 15, 15, 16, 0, 13, 13, 16, 16, 0, + 14, 16, 16, 16, 0, 16, 16, 16, 0, 0, 0, 0, + 0, 0, 0, 8, 10, 13, 15, 16, 10, 11, 13, 16, + 16, 13, 13, 14, 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 0, 8, 10, 11, 15, 16, 9, 10, 12, + 16, 16, 12, 12, 15, 16, 16, 16, 14, 16, 16, 16, + 16, 16, 16, 16, 0, 9, 11, 14, 16, 16, 10, 11, + 13, 16, 16, 14, 13, 14, 16, 16, 16, 15, 15, 16, + 0, 16, 16, 16, 0, 0, 11, 13, 16, 16, 16, 11, + 13, 15, 16, 16, 13, 16, 16, 16, 0, 16, 16, 16, + 16, 0, 16, 16, 0, 0, 0, 15, 16, 16, 16, 0, + 14, 16, 16, 16, 0, 16, 16, 16, 0, 0, 16, 16, + 0, 0, 0, 0, 0, 0, 0, 0, 9, 13, 16, 16, + 16, 11, 13, 16, 16, 16, 14, 15, 16, 16, 0, 15, + 16, 16, 16, 0, 16, 16, 0, 0, 0, 9, 13, 15, + 15, 16, 12, 13, 14, 16, 16, 16, 15, 16, 16, 0, + 16, 16, 16, 16, 0, 16, 16, 0, 0, 0, 11, 13, + 15, 16, 0, 12, 14, 16, 16, 0, 16, 16, 16, 16, + 0, 16, 16, 16, 0, 0, 0, 0, 0, 0, 0, 16, + 16, 16, 16, 0, 16, 16, 16, 16, 0, 16, 16, 16, + 0, 0, 16, 16, 0, 0, 0, 0, 0, 0, 0, 0, + 16, 16, 0, 0, 0, 16, 16, +}; + + +static const uint16_t cvh_huffcodes3[607] = { + 0x0000,0x0004,0x0022,0x00c6,0x03b0,0x000c,0x000d,0x0023,0x00c7,0x03b1,0x005c,0x00c8, + 0x00c9,0x03b2,0x0fa4,0x01c2,0x01c3,0x03b3,0x0fa5,0x7f72,0x03b4,0x07b2,0x1f9a,0xff24, + 0xff25,0x000e,0x0024,0x00ca,0x03b5,0x07b3,0x000f,0x0025,0x00cb,0x03b6,0x0fa6,0x005d, + 0x005e,0x00cc,0x03b7,0x1f9b,0x01c4,0x01c5,0x03b8,0x0fa7,0x7f73,0x0fa8,0x07b4,0x1f9c, + 0xff26,0xff27,0x005f,0x01c6,0x03b9,0x0fa9,0x7f74,0x0060,0x00cd,0x03ba,0x0faa,0x1f9d, + 0x01c7,0x01c8,0x07b5,0x1f9e,0xff28,0x07b6,0x07b7,0x0fab,0x3fa2,0xff29,0x0fac,0x0fad, + 0x3fa3,0xff2a,0x3fa2,0x01c9,0x07b8,0x0fae,0xff2b,0xff2c,0x01ca,0x03bb,0x1f9f,0x7f75, + 0xff2d,0x03bc,0x07b9,0x0faf,0xff2e,0xff2f,0x1fa0,0x1fa1,0xff30,0xff31,0xff32,0xff33, + 0xff34,0x7f76,0xff35,0xff31,0x07ba,0x1fa2,0xff36,0xff37,0x7f77,0x07bb,0x1fa3,0x7f78, + 0xff38,0xff39,0x1fa4,0x1fa5,0xff3a,0xff3b,0xff2e,0x3fa4,0xff3c,0xff3d,0xff3e,0xff31, + 0xff3f,0xff40,0xff30,0xff31,0xff31,0x0005,0x0026,0x00ce,0x03bd,0x1fa6,0x0027,0x0028, + 0x00cf,0x03be,0x1fa7,0x01cb,0x00d0,0x03bf,0x0fb0,0xff41,0x03c0,0x03c1,0x07bc,0x7f79, + 0xff42,0x1fa8,0x0fb1,0x3fa5,0xff43,0xff44,0x0010,0x0029,0x00d1,0x07bd,0x1fa9,0x002a, + 0x002b,0x00d2,0x03c2,0x1faa,0x00d3,0x00d4,0x01cc,0x07be,0x3fa6,0x03c3,0x03c4,0x0fb2, + 0x0fb3,0xff45,0x1fab,0x0fb4,0x1fac,0x7f7a,0xff46,0x0061,0x00d5,0x01cd,0x0fb5,0xff47, + 0x0062,0x00d6,0x03c5,0x0fb6,0x3fa7,0x01ce,0x01cf,0x03c6,0x1fad,0xff48,0x07bf,0x03c7, + 0x0fb7,0x7f7b,0xff49,0x1fae,0x1faf,0xff4a,0xff4b,0x7f7b,0x01d0,0x07c0,0x1fb0,0xff4c, + 0xff4d,0x01d1,0x03c8,0x0fb8,0x7f7c,0xff4e,0x03c9,0x07c1,0x1fb1,0xff4f,0xff50,0x1fb2, + 0x0fb9,0xff51,0xff52,0xff53,0xff54,0xff55,0xff56,0xff57,0xff52,0x07c2,0x1fb3,0xff58, + 0xff59,0xff5a,0x07c3,0x1fb4,0xff5b,0xff5c,0xff5d,0x0fba,0x1fb5,0x7f7d,0xff5e,0xff4f, + 0xff5f,0xff60,0xff61,0xff62,0xff52,0xff63,0xff64,0xff51,0xff52,0xff52,0x002c,0x00d7, + 0x07c4,0x1fb6,0xff65,0x00d8,0x00d9,0x03ca,0x0fbb,0xff66,0x07c5,0x03cb,0x07c6,0x1fb7, + 0xff67,0x0fbc,0x1fb8,0x1fb9,0x7f7e,0xff68,0xff69,0xff6a,0x3fa8,0xff6b,0x7f7e,0x002d, + 0x00da,0x03cc,0x1fba,0xff6c,0x00db,0x00dc,0x03cd,0x0fbd,0xff6d,0x03ce,0x03cf,0x07c7, + 0x1fbb,0xff6e,0x1fbc,0x0fbe,0x1fbd,0xff6f,0xff70,0x3fa9,0x3faa,0x3fab,0xff71,0xff6f, + 0x00dd,0x01d2,0x07c8,0x1fbe,0xff72,0x00de,0x01d3,0x07c9,0xff73,0x3fac,0x03d0,0x03d1, + 0x0fbf,0x7f7f,0xff74,0x0fc0,0x0fc1,0x1fbf,0xff75,0xff76,0x7f80,0xff77,0xff78,0xff79, + 0xff75,0x03d2,0x0fc2,0x7f81,0xff7a,0xff7b,0x03d3,0x0fc3,0x0fc4,0x3fad,0xff7c,0x0fc5, + 0x0fc6,0x1fc0,0xff7d,0xff7e,0x3fae,0x7f82,0xff7f,0xff80,0xff80,0xff81,0xff82,0xff83, + 0xff80,0xff80,0x0fc7,0x7f83,0x7f84,0xff84,0xff7a,0x1fc1,0x1fc2,0xff85,0xff86,0x3fad, + 0x3faf,0xff87,0xff88,0xff89,0xff7d,0xff8a,0xff8b,0xff8c,0xff80,0xff80,0x3fae,0x7f82, + 0xff7f,0xff80,0xff80,0x00df,0x03d4,0x1fc3,0x7f85,0xff8d,0x03d5,0x07ca,0x1fc4,0xff8e, + 0xff8f,0x1fc5,0x1fc6,0x3fb0,0xff90,0xff91,0xff92,0xff93,0xff94,0xff95,0xff96,0xff97, + 0xff98,0xff99,0xff9a,0xff95,0x00e0,0x03d6,0x07cb,0x7f86,0xff9b,0x01d4,0x03d7,0x0fc8, + 0xff9c,0xff9d,0x0fc9,0x0fca,0x7f87,0xff9e,0xff9f,0xffa0,0x3fb1,0xffa1,0xffa2,0xffa3, + 0xffa4,0xffa5,0xffa6,0xffa7,0xffa2,0x01d5,0x07cc,0x3fb2,0xffa8,0xffa9,0x03d8,0x07cd, + 0x1fc7,0xffaa,0xffab,0x3fb3,0x1fc8,0x3fb4,0xffac,0xffad,0xffae,0x7f88,0x7f89,0xffaf, + 0xffaf,0xffb0,0xffb1,0xffb2,0xffaf,0xffaf,0x07ce,0x1fc9,0xffb3,0xffb4,0xffb5,0x07cf, + 0x1fca,0x7f8a,0xffb6,0xffb7,0x1fcb,0xffb8,0xffb9,0xffba,0xffba,0xffbb,0xffbc,0xffbd, + 0xffbe,0xffbe,0xffbf,0xffc0,0xffbd,0xffbe,0xffbe,0x7f8b,0xffc1,0xffc2,0xffc3,0xffb4, + 0x3fb5,0xffc4,0xffc5,0xffc6,0xffb6,0xffc7,0xffc8,0xffc9,0xffba,0xffba,0xffca,0xffcb, + 0xffbd,0xffbe,0xffbe,0xffbb,0xffbc,0xffbd,0xffbe,0xffbe,0x01d6,0x1fcc,0xffcc,0xffcd, + 0xffce,0x07d0,0x1fcd,0xffcf,0xffd0,0xffd1,0x3fb6,0x7f8c,0xffd2,0xffd3,0xff90,0x7f8d, + 0xffd4,0xffd5,0xffd6,0xff95,0xffd7,0xffd8,0xff94,0xff95,0xff95,0x01d7,0x1fce,0x7f8e, + 0x7f8f,0xffd9,0x0fcb,0x1fcf,0x3fb7,0xffda,0xffdb,0xffdc,0x7f90,0xffdd,0xffde,0xff9e, + 0xffdf,0xffe0,0xffe1,0xffe2,0xffa2,0xffe3,0xffe4,0xffa1,0xffa2,0xffa2,0x07d1,0x1fd0, + 0x7f91,0xffe5,0xffa8,0x0fcc,0x3fb8,0xffe6,0xffe7,0xffaa,0xffe8,0xffe9,0xffea,0xffeb, + 0xffac,0xffec,0xffed,0xffee,0xffaf,0xffaf,0xffae,0x7f88,0x7f89,0xffaf,0xffaf,0xffef, + 0xfff0,0xfff1,0xfff2,0xffb4,0xfff3,0xfff4,0xfff5,0xfff6,0xffb6,0xfff7,0xfff8,0xfff9, + 0xffba,0xffba,0xfffa,0xfffb,0xffbd,0xffbe,0xffbe,0xffbb,0xffbc,0xffbd,0xffbe,0xffbe, + 0xfffc,0xfffd,0xffb3,0xffb4,0xffb4,0xfffe,0xffff, +}; + +static const uint8_t cvh_huffbits4[246] = { + 2, 4, 7, 10, 4, 5, 7, 10, 7, 8, 10, 14, + 11, 11, 15, 15, 4, 5, 9, 12, 5, 5, 8, 12, + 8, 7, 10, 15, 11, 11, 15, 15, 7, 9, 12, 15, + 8, 8, 12, 15, 10, 10, 13, 15, 14, 14, 15, 0, + 11, 13, 15, 15, 11, 13, 15, 15, 14, 15, 15, 0, + 15, 15, 0, 0, 4, 5, 9, 13, 5, 6, 9, 13, + 9, 9, 11, 15, 14, 13, 15, 15, 4, 6, 9, 12, + 5, 6, 9, 13, 9, 8, 11, 15, 13, 12, 15, 15, + 7, 9, 12, 15, 7, 8, 11, 15, 10, 10, 14, 15, + 14, 15, 15, 0, 10, 12, 15, 15, 11, 13, 15, 15, + 15, 15, 15, 0, 15, 15, 0, 0, 6, 9, 13, 14, + 8, 9, 12, 15, 12, 12, 15, 15, 15, 15, 15, 0, + 7, 9, 13, 15, 8, 9, 12, 15, 11, 12, 15, 15, + 15, 15, 15, 0, 9, 11, 15, 15, 9, 11, 15, 15, + 14, 14, 15, 0, 15, 15, 0, 0, 14, 15, 15, 0, + 14, 15, 15, 0, 15, 15, 0, 0, 0, 0, 0, 0, + 9, 12, 15, 15, 12, 13, 15, 15, 15, 15, 15, 0, + 15, 15, 0, 0, 10, 12, 15, 15, 12, 14, 15, 15, + 15, 15, 15, 0, 15, 15, 0, 0, 14, 15, 15, 0, + 15, 15, 15, 0, 15, 15, 0, 0, 0, 0, 0, 0, + 15, 15, 0, 0, 15, 15, +}; + + +static const uint16_t cvh_huffcodes4[246] = { + 0x0000,0x0004,0x006c,0x03e6,0x0005,0x0012,0x006d,0x03e7,0x006e,0x00e8,0x03e8,0x3fc4, + 0x07e0,0x07e1,0x7fa4,0x7fa5,0x0006,0x0013,0x01e2,0x0fda,0x0014,0x0015,0x00e9,0x0fdb, + 0x00ea,0x006f,0x03e9,0x7fa6,0x07e2,0x07e3,0x7fa7,0x7fa8,0x0070,0x01e3,0x0fdc,0x7fa9, + 0x00eb,0x00ec,0x0fdd,0x7faa,0x03ea,0x03eb,0x1fd6,0x7fab,0x3fc5,0x3fc6,0x7fac,0x1fd6, + 0x07e4,0x1fd7,0x7fad,0x7fae,0x07e5,0x1fd8,0x7faf,0x7fb0,0x3fc7,0x7fb1,0x7fb2,0x1fd6, + 0x7fb3,0x7fb4,0x1fd6,0x1fd6,0x0007,0x0016,0x01e4,0x1fd9,0x0017,0x0032,0x01e5,0x1fda, + 0x01e6,0x01e7,0x07e6,0x7fb5,0x3fc8,0x1fdb,0x7fb6,0x7fb7,0x0008,0x0033,0x01e8,0x0fde, + 0x0018,0x0034,0x01e9,0x1fdc,0x01ea,0x00ed,0x07e7,0x7fb8,0x1fdd,0x0fdf,0x7fb9,0x7fba, + 0x0071,0x01eb,0x0fe0,0x7fbb,0x0072,0x00ee,0x07e8,0x7fbc,0x03ec,0x03ed,0x3fc9,0x7fbd, + 0x3fca,0x7fbe,0x7fbf,0x3fc9,0x03ee,0x0fe1,0x7fc0,0x7fc1,0x07e9,0x1fde,0x7fc2,0x7fc3, + 0x7fc4,0x7fc5,0x7fc6,0x3fc9,0x7fc7,0x7fc8,0x3fc9,0x3fc9,0x0035,0x01ec,0x1fdf,0x3fcb, + 0x00ef,0x01ed,0x0fe2,0x7fc9,0x0fe3,0x0fe4,0x7fca,0x7fcb,0x7fcc,0x7fcd,0x7fce,0x7fca, + 0x0073,0x01ee,0x1fe0,0x7fcf,0x00f0,0x01ef,0x0fe5,0x7fd0,0x07ea,0x0fe6,0x7fd1,0x7fd2, + 0x7fd3,0x7fd4,0x7fd5,0x7fd1,0x01f0,0x07eb,0x7fd6,0x7fd7,0x01f1,0x07ec,0x7fd8,0x7fd9, + 0x3fcc,0x3fcd,0x7fda,0x7fda,0x7fdb,0x7fdc,0x7fda,0x7fda,0x3fce,0x7fdd,0x7fde,0x7fd6, + 0x3fcf,0x7fdf,0x7fe0,0x7fd8,0x7fe1,0x7fe2,0x7fda,0x7fda,0x3fcc,0x3fcd,0x7fda,0x7fda, + 0x01f2,0x0fe7,0x7fe3,0x7fe4,0x0fe8,0x1fe1,0x7fe5,0x7fe6,0x7fe7,0x7fe8,0x7fe9,0x7fca, + 0x7fea,0x7feb,0x7fca,0x7fca,0x03ef,0x0fe9,0x7fec,0x7fed,0x0fea,0x3fd0,0x7fee,0x7fef, + 0x7ff0,0x7ff1,0x7ff2,0x7fd1,0x7ff3,0x7ff4,0x7fd1,0x7fd1,0x3fd1,0x7ff5,0x7ff6,0x7fd6, + 0x7ff7,0x7ff8,0x7ff9,0x7fd8,0x7ffa,0x7ffb,0x7fda,0x7fda,0x3fcc,0x3fcd,0x7fda,0x7fda, + 0x7ffc,0x7ffd,0x7fd6,0x7fd6,0x7ffe,0x7fff, +}; + + +static const uint8_t cvh_huffbits5[230] = { + 2, 4, 8, 4, 5, 9, 9, 10, 14, 4, 6, 11, + 5, 6, 12, 10, 11, 15, 9, 11, 15, 10, 13, 15, + 14, 15, 0, 4, 6, 12, 6, 7, 12, 12, 12, 15, + 5, 7, 13, 6, 7, 13, 12, 13, 15, 10, 12, 15, + 11, 13, 15, 15, 15, 0, 8, 13, 15, 11, 12, 15, + 15, 15, 0, 10, 13, 15, 12, 15, 15, 15, 15, 0, + 15, 15, 0, 15, 15, 0, 0, 0, 0, 4, 5, 11, + 5, 7, 12, 11, 12, 15, 6, 7, 13, 7, 8, 14, + 12, 14, 15, 11, 13, 15, 12, 13, 15, 15, 15, 0, + 5, 6, 13, 7, 8, 15, 12, 14, 15, 6, 8, 14, + 7, 8, 15, 14, 15, 15, 12, 12, 15, 12, 13, 15, + 15, 15, 0, 9, 13, 15, 12, 13, 15, 15, 15, 0, + 11, 13, 15, 13, 13, 15, 15, 15, 0, 14, 15, 0, + 15, 15, 0, 0, 0, 0, 8, 10, 15, 11, 12, 15, + 15, 15, 0, 10, 12, 15, 12, 13, 15, 15, 15, 0, + 14, 15, 0, 15, 15, 0, 0, 0, 0, 8, 12, 15, + 12, 13, 15, 15, 15, 0, 11, 13, 15, 13, 15, 15, + 15, 15, 0, 15, 15, 0, 15, 15, 0, 0, 0, 0, + 14, 15, 0, 15, 15, 0, 0, 0, 0, 15, 15, 0, + 15, 15, +}; + + + +static const uint16_t cvh_huffcodes5[230] = { + 0x0000,0x0004,0x00f0,0x0005,0x0012,0x01f0,0x01f1,0x03e8,0x3fce,0x0006,0x0030,0x07de, + 0x0013,0x0031,0x0fd2,0x03e9,0x07df,0x7fb0,0x01f2,0x07e0,0x7fb1,0x03ea,0x1fd2,0x7fb2, + 0x3fcf,0x7fb3,0x0031,0x0007,0x0032,0x0fd3,0x0033,0x0070,0x0fd4,0x0fd5,0x0fd6,0x7fb4, + 0x0014,0x0071,0x1fd3,0x0034,0x0072,0x1fd4,0x0fd7,0x1fd5,0x7fb5,0x03eb,0x0fd8,0x7fb6, + 0x07e1,0x1fd6,0x7fb7,0x7fb8,0x7fb9,0x0072,0x00f1,0x1fd7,0x7fba,0x07e2,0x0fd9,0x7fbb, + 0x7fbc,0x7fbd,0x0070,0x03ec,0x1fd8,0x7fbe,0x0fda,0x7fbf,0x7fc0,0x7fc1,0x7fc2,0x0072, + 0x7fc3,0x7fc4,0x0071,0x7fc5,0x7fc6,0x0072,0x0034,0x0072,0x0072,0x0008,0x0015,0x07e3, + 0x0016,0x0073,0x0fdb,0x07e4,0x0fdc,0x7fc7,0x0035,0x0074,0x1fd9,0x0075,0x00f2,0x3fd0, + 0x0fdd,0x3fd1,0x7fc8,0x07e5,0x1fda,0x7fc9,0x0fde,0x1fdb,0x7fca,0x7fcb,0x7fcc,0x00f2, + 0x0017,0x0036,0x1fdc,0x0076,0x00f3,0x7fcd,0x0fdf,0x3fd2,0x7fce,0x0037,0x00f4,0x3fd3, + 0x0077,0x00f5,0x7fcf,0x3fd4,0x7fd0,0x7fd1,0x0fe0,0x0fe1,0x7fd2,0x0fe2,0x1fdd,0x7fd3, + 0x7fd4,0x7fd5,0x00f5,0x01f3,0x1fde,0x7fd6,0x0fe3,0x1fdf,0x7fd7,0x7fd8,0x7fd9,0x00f3, + 0x07e6,0x1fe0,0x7fda,0x1fe1,0x1fe2,0x7fdb,0x7fdc,0x7fdd,0x00f5,0x3fd5,0x7fde,0x00f4, + 0x7fdf,0x7fe0,0x00f5,0x0077,0x00f5,0x00f5,0x00f6,0x03ed,0x7fe1,0x07e7,0x0fe4,0x7fe2, + 0x7fe3,0x7fe4,0x0073,0x03ee,0x0fe5,0x7fe5,0x0fe6,0x1fe3,0x7fe6,0x7fe7,0x7fe8,0x00f2, + 0x3fd6,0x7fe9,0x0074,0x7fea,0x7feb,0x00f2,0x0075,0x00f2,0x00f2,0x00f7,0x0fe7,0x7fec, + 0x0fe8,0x1fe4,0x7fed,0x7fee,0x7fef,0x00f3,0x07e8,0x1fe5,0x7ff0,0x1fe6,0x7ff1,0x7ff2, + 0x7ff3,0x7ff4,0x00f5,0x7ff5,0x7ff6,0x00f4,0x7ff7,0x7ff8,0x00f5,0x0077,0x00f5,0x00f5, + 0x3fd7,0x7ff9,0x0036,0x7ffa,0x7ffb,0x00f3,0x0076,0x00f3,0x00f3,0x7ffc,0x7ffd,0x0000, + 0x7ffe,0x7fff, +}; + + +static const uint8_t cvh_huffbits6[32] = { + 1, 4, 4, 6, 4, 6, 6, 8, 4, 6, 6, 8, + 6, 9, 8, 10, 4, 6, 7, 8, 6, 9, 8, 11, + 6, 9, 8, 10, 8, 10, 9, 11, +}; + +static const uint16_t cvh_huffcodes6[32] = { + 0x0000,0x0008,0x0009,0x0034,0x000a,0x0035,0x0036,0x00f6,0x000b,0x0037,0x0038,0x00f7, + 0x0039,0x01fa,0x00f8,0x03fc,0x000c,0x003a,0x007a,0x00f9,0x003b,0x01fb,0x00fa,0x07fe, + 0x003c,0x01fc,0x00fb,0x03fd,0x00fc,0x03fe,0x01fd,0x07ff, +}; + +static const uint16_t* cvh_huffcodes[7] = { + cvh_huffcodes0, cvh_huffcodes1, cvh_huffcodes2, cvh_huffcodes3, + cvh_huffcodes4, cvh_huffcodes5, cvh_huffcodes6, +}; + +static const uint8_t* cvh_huffbits[7] = { + cvh_huffbits0, cvh_huffbits1, cvh_huffbits2, cvh_huffbits3, + cvh_huffbits4, cvh_huffbits5, cvh_huffbits6, +}; + + +static const uint16_t ccpl_huffcodes2[3] = { + 0x02,0x00,0x03, +}; + +static const uint16_t ccpl_huffcodes3[7] = { + 0x3e,0x1e,0x02,0x00,0x06,0x0e,0x3f, +}; + +static const uint16_t ccpl_huffcodes4[15] = { + 0xfc,0xfd,0x7c,0x3c,0x1c,0x0c,0x04,0x00,0x05,0x0d,0x1d,0x3d, + 0x7d,0xfe,0xff, +}; + +static const uint16_t ccpl_huffcodes5[31] = { + 0x03f8,0x03f9,0x03fa,0x03fb,0x01f8,0x01f9,0x00f8,0x00f9,0x0078,0x0079,0x0038,0x0039, + 0x0018,0x0019,0x0004,0x0000,0x0005,0x001a,0x001b,0x003a,0x003b,0x007a,0x007b,0x00fa, + 0x00fb,0x01fa,0x01fb,0x03fc,0x03fd,0x03fe,0x03ff, +}; + +static const uint16_t ccpl_huffcodes6[63] = { + 0x0004,0x0005,0x0005,0x0006,0x0006,0x0007,0x0007,0x0007,0x0007,0x0008,0x0008,0x0008, + 0x0008,0x0009,0x0009,0x0009,0x0009,0x000a,0x000a,0x000a,0x000a,0x000a,0x000b,0x000b, + 0x000b,0x000b,0x000c,0x000d,0x000e,0x000e,0x0010,0x0000,0x000a,0x0018,0x0019,0x0036, + 0x0037,0x0074,0x0075,0x0076,0x0077,0x00f4,0x00f5,0x00f6,0x00f7,0x01f5,0x01f6,0x01f7, + 0x01f8,0x03f6,0x03f7,0x03f8,0x03f9,0x03fa,0x07fa,0x07fb,0x07fc,0x07fd,0x0ffd,0x1ffd, + 0x3ffd,0x3ffe,0xffff, +}; + +static const uint8_t ccpl_huffbits2[3] = { + 2,1,2, +}; + +static const uint8_t ccpl_huffbits3[7] = { + 6,5,2,1,3,4,6, +}; + +static const uint8_t ccpl_huffbits4[15] = { + 8,8,7,6,5,4,3,1,3,4,5,6,7,8,8, +}; + +static const uint8_t ccpl_huffbits5[31] = { + 10,10,10,10,9,9,8,8,7,7,6,6, + 5,5,3,1,3,5,5,6,6,7,7,8, + 8,9,9,10,10,10,10, +}; + +static const uint8_t ccpl_huffbits6[63] = { + 16,15,14,13,12,11,11,11,11,10,10,10, + 10,9,9,9,9,9,8,8,8,8,7,7, + 7,7,6,6,5,5,3,1,4,5,5,6, + 6,7,7,7,7,8,8,8,8,9,9,9, + 9,10,10,10,10,10,11,11,11,11,12,13, + 14,14,16, +}; + +static const uint16_t* ccpl_huffcodes[5] = { + ccpl_huffcodes2,ccpl_huffcodes3, + ccpl_huffcodes4,ccpl_huffcodes5,ccpl_huffcodes6 +}; + +static const uint8_t* ccpl_huffbits[5] = { + ccpl_huffbits2,ccpl_huffbits3, + ccpl_huffbits4,ccpl_huffbits5,ccpl_huffbits6 +}; + + +//Coupling tables + +static const int cplband[51] = { + 0,1,2,3,4,5,6,7,8,9, + 10,11,11,12,12,13,13,14,14,14, + 15,15,15,15,16,16,16,16,16,17, + 17,17,17,17,17,18,18,18,18,18, + 18,18,19,19,19,19,19,19,19,19, + 19, +}; + +static const float cplscale2[3] = { +0.953020632266998,0.70710676908493,0.302905440330505, +}; + +static const float cplscale3[7] = { +0.981279790401459,0.936997592449188,0.875934481620789,0.70710676908493, +0.482430040836334,0.349335819482803,0.192587479948997, +}; + +static const float cplscale4[15] = { +0.991486728191376,0.973249018192291,0.953020632266998,0.930133521556854, +0.903453230857849,0.870746195316315,0.826180458068848,0.70710676908493, +0.563405573368073,0.491732746362686,0.428686618804932,0.367221474647522, +0.302905440330505,0.229752898216248,0.130207896232605, +}; + +static const float cplscale5[31] = { +0.995926380157471,0.987517595291138,0.978726446628571,0.969505727291107, +0.95979779958725,0.949531257152557,0.938616216182709,0.926936149597168, +0.914336204528809,0.900602877140045,0.885426938533783,0.868331849575043, +0.84851086139679,0.824381768703461,0.791833400726318,0.70710676908493, +0.610737144947052,0.566034197807312,0.529177963733673,0.495983630418777, +0.464778542518616,0.434642940759659,0.404955863952637,0.375219136476517, +0.344963222742081,0.313672333955765,0.280692428350449,0.245068684220314, +0.205169528722763,0.157508864998817,0.0901700109243393, +}; + +static const float cplscale6[63] = { +0.998005926609039,0.993956744670868,0.989822506904602,0.985598564147949, +0.981279790401459,0.976860702037811,0.972335040569305,0.967696130275726, +0.962936460971832,0.958047747612000,0.953020632266998,0.947844684123993, +0.942508161067963,0.936997592449188,0.931297719478607,0.925390899181366, +0.919256627559662,0.912870943546295,0.906205296516418,0.899225592613220, +0.891890347003937,0.884148240089417,0.875934481620789,0.867165684700012, +0.857730865478516,0.847477376461029,0.836184680461884,0.823513329029083, +0.808890223503113,0.791194140911102,0.767520070075989,0.707106769084930, +0.641024887561798,0.611565053462982,0.587959706783295,0.567296981811523, +0.548448026180267,0.530831515789032,0.514098942279816,0.498019754886627, +0.482430040836334,0.467206478118896,0.452251672744751,0.437485188245773, +0.422837972640991,0.408248275518417,0.393658757209778,0.379014074802399, +0.364258885383606,0.349335819482803,0.334183186292648,0.318732559680939, +0.302905440330505,0.286608695983887,0.269728302955627,0.252119421958923, +0.233590632677078,0.213876649737358,0.192587479948997,0.169101938605309, +0.142307326197624,0.109772264957428,0.0631198287010193, +}; + +static const float* cplscales[5] = { + cplscale2, cplscale3, cplscale4, cplscale5, cplscale6, +}; diff --git a/src/libffmpeg/libavcodec/cscd.c b/src/libffmpeg/libavcodec/cscd.c new file mode 100644 index 000000000..0d6e04526 --- /dev/null +++ b/src/libffmpeg/libavcodec/cscd.c @@ -0,0 +1,262 @@ +/* + * CamStudio decoder + * Copyright (c) 2006 Reimar Doeffinger + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include <stdio.h> +#include <stdlib.h> + +#include "common.h" +#include "avcodec.h" + +#ifdef CONFIG_ZLIB +#include <zlib.h> +#endif +#include "lzo.h" + +typedef struct { + AVFrame pic; + int linelen, height, bpp; + unsigned int decomp_size; + unsigned char* decomp_buf; +} CamStudioContext; + +static void copy_frame_default(AVFrame *f, uint8_t *src, + int linelen, int height) { + int i; + uint8_t *dst = f->data[0]; + dst += (height - 1) * f->linesize[0]; + for (i = height; i; i--) { + memcpy(dst, src, linelen); + src += linelen; + dst -= f->linesize[0]; + } +} + +static void add_frame_default(AVFrame *f, uint8_t *src, + int linelen, int height) { + int i, j; + uint8_t *dst = f->data[0]; + dst += (height - 1) * f->linesize[0]; + for (i = height; i; i--) { + for (j = linelen; j; j--) + *dst++ += *src++; + dst -= f->linesize[0] + linelen; + } +} + +#ifndef WORDS_BIGENDIAN +#define copy_frame_16 copy_frame_default +#define copy_frame_32 copy_frame_default +#define add_frame_16 add_frame_default +#define add_frame_32 add_frame_default +#else +static void copy_frame_16(AVFrame *f, uint8_t *src, + int linelen, int height) { + int i, j; + uint8_t *dst = f->data[0]; + dst += (height - 1) * f->linesize[0]; + for (i = height; i; i--) { + for (j = linelen / 2; j; j--) { + dst[0] = src[1]; + dst[1] = src[0]; + src += 2; + dst += 2; + } + dst -= f->linesize[0] + linelen; + } +} + +static void copy_frame_32(AVFrame *f, uint8_t *src, + int linelen, int height) { + int i, j; + uint8_t *dst = f->data[0]; + dst += (height - 1) * f->linesize[0]; + for (i = height; i; i--) { + for (j = linelen / 4; j; j--) { + dst[0] = src[3]; + dst[1] = src[2]; + dst[2] = src[1]; + dst[3] = src[0]; + src += 4; + dst += 4; + } + dst -= f->linesize[0] + linelen; + } +} + +static void add_frame_16(AVFrame *f, uint8_t *src, + int linelen, int height) { + int i, j; + uint8_t *dst = f->data[0]; + dst += (height - 1) * f->linesize[0]; + for (i = height; i; i--) { + for (j = linelen / 2; j; j--) { + dst[0] += src[1]; + dst[1] += src[0]; + src += 2; + dst += 2; + } + dst -= f->linesize[0] + linelen; + } +} + +static void add_frame_32(AVFrame *f, uint8_t *src, + int linelen, int height) { + int i, j; + uint8_t *dst = f->data[0]; + dst += (height - 1) * f->linesize[0]; + for (i = height; i; i--) { + for (j = linelen / 4; j; j--) { + dst[0] += src[3]; + dst[1] += src[2]; + dst[2] += src[1]; + dst[3] += src[0]; + src += 4; + dst += 4; + } + dst -= f->linesize[0] + linelen; + } +} +#endif + +static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, + uint8_t *buf, int buf_size) { + CamStudioContext *c = (CamStudioContext *)avctx->priv_data; + AVFrame *picture = data; + + if (buf_size < 2) { + av_log(avctx, AV_LOG_ERROR, "coded frame too small\n"); + return -1; + } + + if (c->pic.data[0]) + avctx->release_buffer(avctx, &c->pic); + c->pic.reference = 1; + c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE | + FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE; + if (avctx->get_buffer(avctx, &c->pic) < 0) { + av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); + return -1; + } + + // decompress data + switch ((buf[0] >> 1) & 7) { + case 0: { // lzo compression + int outlen = c->decomp_size, inlen = buf_size - 2; + if (lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen)) + av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n"); + break; + } + case 1: { // zlib compression +#ifdef CONFIG_ZLIB + unsigned long dlen = c->decomp_size; + if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK) + av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n"); + break; +#else + av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n"); + return -1; +#endif + } + default: + av_log(avctx, AV_LOG_ERROR, "unknown compression\n"); + return -1; + } + + // flip upside down, add difference frame + if (buf[0] & 1) { // keyframe + c->pic.pict_type = FF_I_TYPE; + c->pic.key_frame = 1; + switch (c->bpp) { + case 16: + copy_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height); + break; + case 32: + copy_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height); + break; + default: + copy_frame_default(&c->pic, c->decomp_buf, c->linelen, c->height); + } + } else { + c->pic.pict_type = FF_P_TYPE; + c->pic.key_frame = 0; + switch (c->bpp) { + case 16: + add_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height); + break; + case 32: + add_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height); + break; + default: + add_frame_default(&c->pic, c->decomp_buf, c->linelen, c->height); + } + } + + *picture = c->pic; + *data_size = sizeof(AVFrame); + return buf_size; +} + +static int decode_init(AVCodecContext *avctx) { + CamStudioContext *c = (CamStudioContext *)avctx->priv_data; + if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) { + return 1; + } + avctx->has_b_frames = 0; + switch (avctx->bits_per_sample) { + case 16: avctx->pix_fmt = PIX_FMT_RGB565; break; + case 24: avctx->pix_fmt = PIX_FMT_BGR24; break; + case 32: avctx->pix_fmt = PIX_FMT_RGBA32; break; + default: + av_log(avctx, AV_LOG_ERROR, + "CamStudio codec error: unvalid depth %i bpp\n", + avctx->bits_per_sample); + return 1; + } + c->bpp = avctx->bits_per_sample; + c->pic.data[0] = NULL; + c->linelen = avctx->width * avctx->bits_per_sample / 8; + c->height = avctx->height; + c->decomp_size = c->height * c->linelen; + c->decomp_buf = av_malloc(c->decomp_size + LZO_OUTPUT_PADDING); + if (!c->decomp_buf) { + av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n"); + return 1; + } + return 0; +} + +static int decode_end(AVCodecContext *avctx) { + CamStudioContext *c = (CamStudioContext *)avctx->priv_data; + av_freep(&c->decomp_buf); + if (c->pic.data[0]) + avctx->release_buffer(avctx, &c->pic); + return 0; +} + +AVCodec cscd_decoder = { + "camstudio", + CODEC_TYPE_VIDEO, + CODEC_ID_CSCD, + sizeof(CamStudioContext), + decode_init, + NULL, + decode_end, + decode_frame, + CODEC_CAP_DR1, +}; + diff --git a/src/libffmpeg/libavcodec/flashsv.c b/src/libffmpeg/libavcodec/flashsv.c new file mode 100644 index 000000000..3214d1860 --- /dev/null +++ b/src/libffmpeg/libavcodec/flashsv.c @@ -0,0 +1,274 @@ +/* + * Flash Screen Video decoder + * Copyright (C) 2004 Alex Beregszaszi + * Copyright (C) 2006 Benjamin Larsson + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file flashsv.c + * Flash Screen Video decoder + * @author Alex Beregszaszi + * @author Benjamin Larsson + */ + +/* Bitstream description + * The picture is divided into blocks that are zlib compressed. + * + * The decoder is fed complete frames, the frameheader contains: + * 4bits of block width + * 12bits of frame width + * 4bits of block height + * 12bits of frame height + * + * Directly after the header are the compressed blocks. The blocks + * have their compressed size represented with 16bits in the beginnig. + * If the size = 0 then the block is unchanged from the previous frame. + * All blocks are decompressed until the buffer is consumed. + * + * Encoding ideas, a basic encoder would just use a fixed block size. + * Block sizes can be multipels of 16, from 16 to 256. The blocks don't + * have to be quadratic. A brute force search with a set of diffrent + * block sizes should give a better result then to just use a fixed size. + */ + +#include <stdio.h> +#include <stdlib.h> + +#include "common.h" +#include "avcodec.h" +#include "bitstream.h" + +#ifdef CONFIG_ZLIB +#include <zlib.h> +#endif + +typedef struct FlashSVContext { + AVCodecContext *avctx; + AVFrame frame; + int image_width, image_height; + int block_width, block_height; + uint8_t* tmpblock; + int block_size; +#ifdef CONFIG_ZLIB + z_stream zstream; +#endif +} FlashSVContext; + + +static void copy_region(uint8_t *sptr, uint8_t *dptr, + int dx, int dy, int h, int w, int stride) +{ + int i; + + for (i = dx+h; i > dx; i--) + { + memcpy(dptr+(i*stride)+dy*3, sptr, w*3); + sptr += w*3; + } +} + + +static int flashsv_decode_init(AVCodecContext *avctx) +{ + FlashSVContext *s = (FlashSVContext *)avctx->priv_data; + int zret; // Zlib return code + + s->avctx = avctx; +#ifdef CONFIG_ZLIB + s->zstream.zalloc = Z_NULL; + s->zstream.zfree = Z_NULL; + s->zstream.opaque = Z_NULL; + zret = inflateInit(&(s->zstream)); + if (zret != Z_OK) { + av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret); + return 1; + } +#else + av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled. Needed for the decoder.\n"); + return 1; +#endif + avctx->pix_fmt = PIX_FMT_BGR24; + avctx->has_b_frames = 0; + s->frame.data[0] = NULL; + + return 0; +} + + +static int flashsv_decode_frame(AVCodecContext *avctx, + void *data, int *data_size, + uint8_t *buf, int buf_size) +{ + FlashSVContext *s = (FlashSVContext *)avctx->priv_data; + int h_blocks, v_blocks, h_part, v_part, i, j; + GetBitContext gb; + + /* no supplementary picture */ + if (buf_size == 0) + return 0; + + if(s->frame.data[0]) + avctx->release_buffer(avctx, &s->frame); + + init_get_bits(&gb, buf, buf_size * 8); + + /* start to parse the bitstream */ + s->block_width = 16* (get_bits(&gb, 4)+1); + s->image_width = get_bits(&gb,12); + s->block_height= 16* (get_bits(&gb, 4)+1); + s->image_height= get_bits(&gb,12); + + /* calculate amount of blocks and the size of the border blocks */ + h_blocks = s->image_width / s->block_width; + h_part = s->image_width % s->block_width; + v_blocks = s->image_height / s->block_height; + v_part = s->image_height % s->block_height; + + /* the block size could change between frames, make sure the buffer + * is large enough, if not, get a larger one */ + if(s->block_size < s->block_width*s->block_height) { + if (s->tmpblock != NULL) + av_free(s->tmpblock); + s->block_size = s->block_width*s->block_height; + if ((s->tmpblock = av_malloc(3*s->block_size)) == NULL) { + av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n"); + return -1; + } + } + + /* init the image size once */ + if((avctx->width==0) && (avctx->height==0)){ + avctx->width = s->image_width; + avctx->height = s->image_height; + } + + /* check for changes of image width and image height */ + if ((avctx->width != s->image_width) || (avctx->height != s->image_height)) { + av_log(avctx, AV_LOG_ERROR, "Frame width or height differs from first frames!\n"); + av_log(avctx, AV_LOG_ERROR, "fh = %d, fv %d vs ch = %d, cv = %d\n",avctx->height, + avctx->width,s->image_height,s->image_width); + return -1; + } + + av_log(avctx, AV_LOG_DEBUG, "image: %dx%d block: %dx%d num: %dx%d part: %dx%d\n", + s->image_width, s->image_height, s->block_width, s->block_height, + h_blocks, v_blocks, h_part, v_part); + + s->frame.reference = 1; + s->frame.buffer_hints = FF_BUFFER_HINTS_VALID; + if (avctx->get_buffer(avctx, &s->frame) < 0) { + av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n"); + return -1; + } + + /* loop over all block columns */ + for (j = 0; j < v_blocks + (v_part?1:0); j++) + { + + int hp = j*s->block_height; // horiz position in frame + int hs = (j<v_blocks)?s->block_height:v_part; // size of block + + + /* loop over all block rows */ + for (i = 0; i < h_blocks + (h_part?1:0); i++) + { + int wp = i*s->block_width; // vert position in frame + int ws = (i<h_blocks)?s->block_width:h_part; // size of block + + /* get the size of the compressed zlib chunk */ + int size = get_bits(&gb, 16); + + if (size == 0) { + /* no change, don't do anything */ + } else { + /* decompress block */ +#ifdef CONFIG_ZLIB + int ret = inflateReset(&(s->zstream)); + if (ret != Z_OK) + { + av_log(avctx, AV_LOG_ERROR, "error in decompression (reset) of block %dx%d\n", i, j); + /* return -1; */ + } + s->zstream.next_in = buf+(get_bits_count(&gb)/8); + s->zstream.avail_in = size; + s->zstream.next_out = s->tmpblock; + s->zstream.avail_out = s->block_size*3; + ret = inflate(&(s->zstream), Z_FINISH); + if (ret == Z_DATA_ERROR) + { + av_log(avctx, AV_LOG_ERROR, "Zlib resync occured\n"); + inflateSync(&(s->zstream)); + ret = inflate(&(s->zstream), Z_FINISH); + } + + if ((ret != Z_OK) && (ret != Z_STREAM_END)) + { + av_log(avctx, AV_LOG_ERROR, "error in decompression of block %dx%d: %d\n", i, j, ret); + /* return -1; */ + } +#else + av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled in.\n"); + return -1; +#endif + copy_region(s->tmpblock, s->frame.data[0], s->image_height-(hp+hs+1), wp, hs, ws, s->frame.linesize[0]); + skip_bits(&gb, 8*size); /* skip the consumed bits */ + } + } + } + + *data_size = sizeof(AVFrame); + *(AVFrame*)data = s->frame; + + if ((get_bits_count(&gb)/8) != buf_size) + av_log(avctx, AV_LOG_ERROR, "buffer not fully consumed (%d != %d)\n", + buf_size, (get_bits_count(&gb)/8)); + + /* report that the buffer was completely consumed */ + return buf_size; +} + + +static int flashsv_decode_end(AVCodecContext *avctx) +{ + FlashSVContext *s = (FlashSVContext *)avctx->priv_data; +#ifdef CONFIG_ZLIB + inflateEnd(&(s->zstream)); +#endif + /* release the frame if needed */ + if (s->frame.data[0]) + avctx->release_buffer(avctx, &s->frame); + + /* free the tmpblock */ + if (s->tmpblock != NULL) + av_free(s->tmpblock); + + return 0; +} + + +AVCodec flashsv_decoder = { + "flashsv", + CODEC_TYPE_VIDEO, + CODEC_ID_FLASHSV, + sizeof(FlashSVContext), + flashsv_decode_init, + NULL, + flashsv_decode_end, + flashsv_decode_frame, + CODEC_CAP_DR1, + .pix_fmts = (enum PixelFormat[]){PIX_FMT_BGR24, -1}, +}; diff --git a/src/libffmpeg/libavcodec/kmvc.c b/src/libffmpeg/libavcodec/kmvc.c new file mode 100644 index 000000000..036efa559 --- /dev/null +++ b/src/libffmpeg/libavcodec/kmvc.c @@ -0,0 +1,417 @@ +/* + * KMVC decoder + * Copyright (c) 2006 Konstantin Shishkov + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +/** + * @file kmvc.c + * Karl Morton's Video Codec decoder + */ + +#include <stdio.h> +#include <stdlib.h> + +#include "common.h" +#include "avcodec.h" + +#define KMVC_KEYFRAME 0x80 +#define KMVC_PALETTE 0x40 +#define KMVC_METHOD 0x0F + +/* + * Decoder context + */ +typedef struct KmvcContext { + AVCodecContext *avctx; + AVFrame pic; + + int setpal; + int palsize; + uint32_t pal[256]; + uint8_t *cur, *prev; + uint8_t *frm0, *frm1; +} KmvcContext; + +typedef struct BitBuf { + int bits; + int bitbuf; +} BitBuf; + +#define BLK(data, x, y) data[(x) + (y) * 320] + +#define kmvc_init_getbits(bb, src) bb.bits = 7; bb.bitbuf = *src++; + +#define kmvc_getbit(bb, src, res) {\ + res = 0; \ + if (bb.bitbuf & (1 << bb.bits)) res = 1; \ + bb.bits--; \ + if(bb.bits == -1) { \ + bb.bitbuf = *src++; \ + bb.bits = 7; \ + } \ +} + +static void kmvc_decode_intra_8x8(KmvcContext * ctx, uint8_t * src, int w, int h) +{ + BitBuf bb; + int res, val; + int i, j; + int bx, by; + int l0x, l1x, l0y, l1y; + int mx, my; + + kmvc_init_getbits(bb, src); + + for (by = 0; by < h; by += 8) + for (bx = 0; bx < w; bx += 8) { + kmvc_getbit(bb, src, res); + if (!res) { // fill whole 8x8 block + val = *src++; + for (i = 0; i < 64; i++) + BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = val; + } else { // handle four 4x4 subblocks + for (i = 0; i < 4; i++) { + l0x = bx + (i & 1) * 4; + l0y = by + (i & 2) * 2; + kmvc_getbit(bb, src, res); + if (!res) { + kmvc_getbit(bb, src, res); + if (!res) { // fill whole 4x4 block + val = *src++; + for (j = 0; j < 16; j++) + BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val; + } else { // copy block from already decoded place + val = *src++; + mx = val & 0xF; + my = val >> 4; + for (j = 0; j < 16; j++) + BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = + BLK(ctx->cur, l0x + (j & 3) - mx, l0y + (j >> 2) - my); + } + } else { // descend to 2x2 sub-sub-blocks + for (j = 0; j < 4; j++) { + l1x = l0x + (j & 1) * 2; + l1y = l0y + (j & 2); + kmvc_getbit(bb, src, res); + if (!res) { + kmvc_getbit(bb, src, res); + if (!res) { // fill whole 2x2 block + val = *src++; + BLK(ctx->cur, l1x, l1y) = val; + BLK(ctx->cur, l1x + 1, l1y) = val; + BLK(ctx->cur, l1x, l1y + 1) = val; + BLK(ctx->cur, l1x + 1, l1y + 1) = val; + } else { // copy block from already decoded place + val = *src++; + mx = val & 0xF; + my = val >> 4; + BLK(ctx->cur, l1x, l1y) = BLK(ctx->cur, l1x - mx, l1y - my); + BLK(ctx->cur, l1x + 1, l1y) = + BLK(ctx->cur, l1x + 1 - mx, l1y - my); + BLK(ctx->cur, l1x, l1y + 1) = + BLK(ctx->cur, l1x - mx, l1y + 1 - my); + BLK(ctx->cur, l1x + 1, l1y + 1) = + BLK(ctx->cur, l1x + 1 - mx, l1y + 1 - my); + } + } else { // read values for block + BLK(ctx->cur, l1x, l1y) = *src++; + BLK(ctx->cur, l1x + 1, l1y) = *src++; + BLK(ctx->cur, l1x, l1y + 1) = *src++; + BLK(ctx->cur, l1x + 1, l1y + 1) = *src++; + } + } + } + } + } + } +} + +static void kmvc_decode_inter_8x8(KmvcContext * ctx, uint8_t * src, int w, int h) +{ + BitBuf bb; + int res, val; + int i, j; + int bx, by; + int l0x, l1x, l0y, l1y; + int mx, my; + + kmvc_init_getbits(bb, src); + + for (by = 0; by < h; by += 8) + for (bx = 0; bx < w; bx += 8) { + kmvc_getbit(bb, src, res); + if (!res) { + kmvc_getbit(bb, src, res); + if (!res) { // fill whole 8x8 block + val = *src++; + for (i = 0; i < 64; i++) + BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = val; + } else { // copy block from previous frame + for (i = 0; i < 64; i++) + BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = + BLK(ctx->prev, bx + (i & 0x7), by + (i >> 3)); + } + } else { // handle four 4x4 subblocks + for (i = 0; i < 4; i++) { + l0x = bx + (i & 1) * 4; + l0y = by + (i & 2) * 2; + kmvc_getbit(bb, src, res); + if (!res) { + kmvc_getbit(bb, src, res); + if (!res) { // fill whole 4x4 block + val = *src++; + for (j = 0; j < 16; j++) + BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val; + } else { // copy block + val = *src++; + mx = (val & 0xF) - 8; + my = (val >> 4) - 8; + for (j = 0; j < 16; j++) + BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = + BLK(ctx->prev, l0x + (j & 3) + mx, l0y + (j >> 2) + my); + } + } else { // descend to 2x2 sub-sub-blocks + for (j = 0; j < 4; j++) { + l1x = l0x + (j & 1) * 2; + l1y = l0y + (j & 2); + kmvc_getbit(bb, src, res); + if (!res) { + kmvc_getbit(bb, src, res); + if (!res) { // fill whole 2x2 block + val = *src++; + BLK(ctx->cur, l1x, l1y) = val; + BLK(ctx->cur, l1x + 1, l1y) = val; + BLK(ctx->cur, l1x, l1y + 1) = val; + BLK(ctx->cur, l1x + 1, l1y + 1) = val; + } else { // copy block + val = *src++; + mx = (val & 0xF) - 8; + my = (val >> 4) - 8; + BLK(ctx->cur, l1x, l1y) = BLK(ctx->prev, l1x + mx, l1y + my); + BLK(ctx->cur, l1x + 1, l1y) = + BLK(ctx->prev, l1x + 1 + mx, l1y + my); + BLK(ctx->cur, l1x, l1y + 1) = + BLK(ctx->prev, l1x + mx, l1y + 1 + my); + BLK(ctx->cur, l1x + 1, l1y + 1) = + BLK(ctx->prev, l1x + 1 + mx, l1y + 1 + my); + } + } else { // read values for block + BLK(ctx->cur, l1x, l1y) = *src++; + BLK(ctx->cur, l1x + 1, l1y) = *src++; + BLK(ctx->cur, l1x, l1y + 1) = *src++; + BLK(ctx->cur, l1x + 1, l1y + 1) = *src++; + } + } + } + } + } + } +} + +static int decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t * buf, + int buf_size) +{ + KmvcContext *const ctx = (KmvcContext *) avctx->priv_data; + uint8_t *out, *src; + int i; + int header; + int blocksize; + + if (ctx->pic.data[0]) + avctx->release_buffer(avctx, &ctx->pic); + + ctx->pic.reference = 1; + ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID; + if (avctx->get_buffer(avctx, &ctx->pic) < 0) { + av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); + return -1; + } + + header = *buf++; + + /* blocksize 127 is really palette change event */ + if (buf[0] == 127) { + buf += 3; + for (i = 0; i < 127; i++) { + ctx->pal[i + (header & 0x81)] = (buf[0] << 16) | (buf[1] << 8) | buf[2]; + buf += 4; + } + buf -= 127 * 4 + 3; + } + + if (header & KMVC_KEYFRAME) { + ctx->pic.key_frame = 1; + ctx->pic.pict_type = FF_I_TYPE; + } else { + ctx->pic.key_frame = 0; + ctx->pic.pict_type = FF_P_TYPE; + } + + /* if palette has been changed, copy it from palctrl */ + if (ctx->avctx->palctrl && ctx->avctx->palctrl->palette_changed) { + memcpy(ctx->pal, ctx->avctx->palctrl->palette, AVPALETTE_SIZE); + ctx->setpal = 1; + ctx->avctx->palctrl->palette_changed = 0; + } + + if (header & KMVC_PALETTE) { + ctx->pic.palette_has_changed = 1; + // palette starts from index 1 and has 127 entries + for (i = 1; i <= ctx->palsize; i++) { + ctx->pal[i] = (buf[0] << 16) | (buf[1] << 8) | buf[2]; + buf += 3; + } + } + + if (ctx->setpal) { + ctx->setpal = 0; + ctx->pic.palette_has_changed = 1; + } + + /* make the palette available on the way out */ + memcpy(ctx->pic.data[1], ctx->pal, 1024); + + blocksize = *buf++; + + if (blocksize != 8 && blocksize != 127) { + av_log(avctx, AV_LOG_ERROR, "Block size = %i\n", blocksize); + return -1; + } + memset(ctx->cur, 0, 320 * 200); + switch (header & KMVC_METHOD) { + case 0: + case 1: // used in palette changed event + memcpy(ctx->cur, ctx->prev, 320 * 200); + break; + case 3: + kmvc_decode_intra_8x8(ctx, buf, avctx->width, avctx->height); + break; + case 4: + kmvc_decode_inter_8x8(ctx, buf, avctx->width, avctx->height); + break; + default: + av_log(avctx, AV_LOG_ERROR, "Unknown compression method %i\n", header & KMVC_METHOD); + return -1; + } + + out = ctx->pic.data[0]; + src = ctx->cur; + for (i = 0; i < avctx->height; i++) { + memcpy(out, src, avctx->width); + src += 320; + out += ctx->pic.linesize[0]; + } + + /* flip buffers */ + if (ctx->cur == ctx->frm0) { + ctx->cur = ctx->frm1; + ctx->prev = ctx->frm0; + } else { + ctx->cur = ctx->frm0; + ctx->prev = ctx->frm1; + } + + *data_size = sizeof(AVFrame); + *(AVFrame *) data = ctx->pic; + + /* always report that the buffer was completely consumed */ + return buf_size; +} + + + +/* + * Init kmvc decoder + */ +static int decode_init(AVCodecContext * avctx) +{ + KmvcContext *const c = (KmvcContext *) avctx->priv_data; + int i; + + c->avctx = avctx; + avctx->has_b_frames = 0; + + c->pic.data[0] = NULL; + + if (avctx->width > 320 || avctx->height > 200) { + av_log(avctx, AV_LOG_ERROR, "KMVC supports frames <= 320x200\n"); + return -1; + } + + c->frm0 = av_mallocz(320 * 200); + c->frm1 = av_mallocz(320 * 200); + c->cur = c->frm0; + c->prev = c->frm1; + + for (i = 0; i < 256; i++) { + c->pal[i] = i * 0x10101; + } + + if (avctx->extradata_size < 12) { + av_log(NULL, 0, "Extradata missing, decoding may not work properly...\n"); + c->palsize = 127; + } else { + c->palsize = LE_16(avctx->extradata + 10); + } + + if (avctx->extradata_size == 1036) { // palette in extradata + uint8_t *src = avctx->extradata + 12; + for (i = 0; i < 256; i++) { + c->pal[i] = LE_32(src); + src += 4; + } + c->setpal = 1; + if (c->avctx->palctrl) { + c->avctx->palctrl->palette_changed = 0; + } + } + + avctx->pix_fmt = PIX_FMT_PAL8; + + return 0; +} + + + +/* + * Uninit kmvc decoder + */ +static int decode_end(AVCodecContext * avctx) +{ + KmvcContext *const c = (KmvcContext *) avctx->priv_data; + + if (c->frm0) + av_free(c->frm0); + if (c->frm1) + av_free(c->frm1); + if (c->pic.data[0]) + avctx->release_buffer(avctx, &c->pic); + + return 0; +} + +AVCodec kmvc_decoder = { + "kmvc", + CODEC_TYPE_VIDEO, + CODEC_ID_KMVC, + sizeof(KmvcContext), + decode_init, + NULL, + decode_end, + decode_frame +}; diff --git a/src/libffmpeg/libavcodec/lzo.c b/src/libffmpeg/libavcodec/lzo.c new file mode 100644 index 000000000..d9b42f848 --- /dev/null +++ b/src/libffmpeg/libavcodec/lzo.c @@ -0,0 +1,224 @@ +/* + * LZO 1x decompression + * Copyright (c) 2006 Reimar Doeffinger + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include "common.h" +//! avoid e.g. MPlayers fast_memcpy, it slows things down here +#undef memcpy +#include <string.h> +#include "lzo.h" + +//! define if we may write up to 12 bytes beyond the output buffer +#define OUTBUF_PADDED 1 +//! define if we may read up to 4 bytes beyond the input buffer +#define INBUF_PADDED 1 +typedef struct LZOContext { + uint8_t *in, *in_end; + uint8_t *out_start, *out, *out_end; + int error; +} LZOContext; + +/** + * \brief read one byte from input buffer, avoiding overrun + * \return byte read + */ +static inline int get_byte(LZOContext *c) { + if (c->in < c->in_end) + return *c->in++; + c->error |= LZO_INPUT_DEPLETED; + return 1; +} + +/** + * \brief decode a length value in the coding used by lzo + * \param x previous byte value + * \param mask bits used from x + * \return decoded length value + */ +static inline int get_len(LZOContext *c, int x, int mask) { + int cnt = x & mask; + if (!cnt) { + while (!(x = get_byte(c))) cnt += 255; + cnt += mask + x; + } + return cnt; +} + +/** + * \brief copy bytes from input to output buffer with checking + * \param cnt number of bytes to copy, must be > 0 + */ +static inline void copy(LZOContext *c, int cnt) { + register uint8_t *src = c->in; + register uint8_t *dst = c->out; + if (src + cnt > c->in_end) { + cnt = c->in_end - src; + c->error |= LZO_INPUT_DEPLETED; + } + if (dst + cnt > c->out_end) { + cnt = c->out_end - dst; + c->error |= LZO_OUTPUT_FULL; + } +#if defined(INBUF_PADDED) && defined(OUTBUF_PADDED) + dst[0] = src[0]; + dst[1] = src[1]; + dst[2] = src[2]; + dst[3] = src[3]; + src += 4; + dst += 4; + cnt -= 4; + if (cnt > 0) +#endif + memcpy(dst, src, cnt); + c->in = src + cnt; + c->out = dst + cnt; +} + +/** + * \brief copy previously decoded bytes to current position + * \param back how many bytes back we start + * \param cnt number of bytes to copy, must be > 0 + * + * cnt > back is valid, this will copy the bytes we just copied, + * thus creating a repeating pattern with a period length of back. + */ +static inline void copy_backptr(LZOContext *c, int back, int cnt) { + register uint8_t *src = &c->out[-back]; + register uint8_t *dst = c->out; + if (src < c->out_start) { + c->error |= LZO_INVALID_BACKPTR; + return; + } + if (dst + cnt > c->out_end) { + cnt = c->out_end - dst; + c->error |= LZO_OUTPUT_FULL; + } + if (back == 1) { + memset(dst, *src, cnt); + dst += cnt; + } else { +#ifdef OUTBUF_PADDED + dst[0] = src[0]; + dst[1] = src[1]; + dst[2] = src[2]; + dst[3] = src[3]; + src += 4; + dst += 4; + cnt -= 4; + if (cnt > 0) { + dst[0] = src[0]; + dst[1] = src[1]; + dst[2] = src[2]; + dst[3] = src[3]; + dst[4] = src[4]; + dst[5] = src[5]; + dst[6] = src[6]; + dst[7] = src[7]; + src += 8; + dst += 8; + cnt -= 8; + } +#endif + if (cnt > 0) { + int blocklen = back; + while (cnt > blocklen) { + memcpy(dst, src, blocklen); + dst += blocklen; + cnt -= blocklen; + blocklen <<= 1; + } + memcpy(dst, src, cnt); + } + dst += cnt; + } + c->out = dst; +} + +/** + * \brief decode LZO 1x compressed data + * \param out output buffer + * \param outlen size of output buffer, number of bytes left are returned here + * \param in input buffer + * \param inlen size of input buffer, number of bytes left are returned here + * \return 0 on success, otherwise error flags, see lzo.h + * + * make sure all buffers are appropriately padded, in must provide + * LZO_INPUT_PADDING, out must provide LZO_OUTPUT_PADDING additional bytes + */ +int lzo1x_decode(void *out, int *outlen, void *in, int *inlen) { + enum {COPY, BACKPTR} state = COPY; + int x; + LZOContext c; + c.in = in; + c.in_end = in + *inlen; + c.out = c.out_start = out; + c.out_end = out + * outlen; + c.error = 0; + x = get_byte(&c); + if (x > 17) { + copy(&c, x - 17); + x = get_byte(&c); + if (x < 16) c.error |= LZO_ERROR; + } + while (!c.error) { + int cnt, back; + if (x >> 4) { + if (x >> 6) { + cnt = (x >> 5) - 1; + back = (get_byte(&c) << 3) + ((x >> 2) & 7) + 1; + } else if (x >> 5) { + cnt = get_len(&c, x, 31); + x = get_byte(&c); + back = (get_byte(&c) << 6) + (x >> 2) + 1; + } else { + cnt = get_len(&c, x, 7); + back = (1 << 14) + ((x & 8) << 11); + x = get_byte(&c); + back += (get_byte(&c) << 6) + (x >> 2); + if (back == (1 << 14)) { + if (cnt != 1) + c.error |= LZO_ERROR; + break; + } + } + } else + switch (state) { + case COPY: + cnt = get_len(&c, x, 15); + copy(&c, cnt + 3); + x = get_byte(&c); + if (x >> 4) + continue; + cnt = 1; + back = (1 << 11) + (get_byte(&c) << 2) + (x >> 2) + 1; + break; + case BACKPTR: + cnt = 0; + back = (get_byte(&c) << 2) + (x >> 2) + 1; + break; + } + copy_backptr(&c, back, cnt + 2); + cnt = x & 3; + state = cnt ? BACKPTR : COPY; + if (cnt) + copy(&c, cnt); + x = get_byte(&c); + } + *inlen = c.in_end - c.in; + *outlen = c.out_end - c.out; + return c.error; +} diff --git a/src/libffmpeg/libavcodec/mmvideo.c b/src/libffmpeg/libavcodec/mmvideo.c new file mode 100644 index 000000000..0cfae83de --- /dev/null +++ b/src/libffmpeg/libavcodec/mmvideo.c @@ -0,0 +1,204 @@ +/* + * American Laser Games MM Video Decoder + * Copyright (c) 2006 Peter Ross + * + * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file mm.c + * American Laser Games MM Video Decoder + * by Peter Ross (suxen_drol at hotmail dot com) + * + * The MM format was used by IBM-PC ports of ALG's "arcade shooter" games, + * including Mad Dog McCree and Crime Patrol. + * + * Technical details here: + * http://wiki.multimedia.cx/index.php?title=American_Laser_Games_MM + */ + +#include "avcodec.h" + +#define MM_PREAMBLE_SIZE 6 + +#define MM_TYPE_INTER 0x5 +#define MM_TYPE_INTRA 0x8 +#define MM_TYPE_INTRA_HH 0xc +#define MM_TYPE_INTER_HH 0xd +#define MM_TYPE_INTRA_HHV 0xe +#define MM_TYPE_INTER_HHV 0xf + +typedef struct MmContext { + AVCodecContext *avctx; + AVFrame frame; +} MmContext; + +static int mm_decode_init(AVCodecContext *avctx) +{ + MmContext *s = avctx->priv_data; + + s->avctx = avctx; + + if (s->avctx->palctrl == NULL) { + av_log(avctx, AV_LOG_ERROR, "mmvideo: palette expected.\n"); + return -1; + } + + avctx->pix_fmt = PIX_FMT_PAL8; + avctx->has_b_frames = 0; + + if (avcodec_check_dimensions(avctx, avctx->width, avctx->height)) + return -1; + + s->frame.reference = 1; + if (avctx->get_buffer(avctx, &s->frame)) { + av_log(s->avctx, AV_LOG_ERROR, "mmvideo: get_buffer() failed\n"); + return -1; + } + + return 0; +} + +static void mm_decode_intra(MmContext * s, int half_horiz, int half_vert, const uint8_t *buf, int buf_size) +{ + int i, x, y; + i=0; x=0; y=0; + + while(i<buf_size) { + int run_length, color; + + if (buf[i] & 0x80) { + run_length = 1; + color = buf[i]; + i++; + }else{ + run_length = (buf[i] & 0x7f) + 2; + color = buf[i+1]; + i+=2; + } + + if (half_horiz) + run_length *=2; + + if (color) { + memset(s->frame.data[0] + y*s->frame.linesize[0] + x, color, run_length); + if (half_vert) + memset(s->frame.data[0] + (y+1)*s->frame.linesize[0] + x, color, run_length); + } + x+= run_length; + + if (x >= s->avctx->width) { + x=0; + y += half_vert ? 2 : 1; + } + } +} + +static void mm_decode_inter(MmContext * s, int half_horiz, int half_vert, const uint8_t *buf, int buf_size) +{ + const int data_ptr = 2 + LE_16(&buf[0]); + int d, r, y; + d = data_ptr; r = 2; y = 0; + + while(r < data_ptr) { + int i, j; + int length = buf[r] & 0x7f; + int x = buf[r+1] + ((buf[r] & 0x80) << 1); + r += 2; + + if (length==0) { + y += x; + continue; + } + + for(i=0; i<length; i++) { + for(j=0; j<8; j++) { + int replace = (buf[r+i] >> (7-j)) & 1; + if (replace) { + int color = buf[d]; + s->frame.data[0][y*s->frame.linesize[0] + x] = color; + if (half_horiz) + s->frame.data[0][y*s->frame.linesize[0] + x + 1] = color; + if (half_vert) { + s->frame.data[0][(y+1)*s->frame.linesize[0] + x] = color; + if (half_horiz) + s->frame.data[0][(y+1)*s->frame.linesize[0] + x + 1] = color; + } + d++; + } + x += half_horiz ? 2 : 1; + } + } + + r += length; + y += half_vert ? 2 : 1; + } +} + +static int mm_decode_frame(AVCodecContext *avctx, + void *data, int *data_size, + uint8_t *buf, int buf_size) +{ + MmContext *s = avctx->priv_data; + AVPaletteControl *palette_control = avctx->palctrl; + int type; + + if (palette_control->palette_changed) { + memcpy(s->frame.data[1], palette_control->palette, AVPALETTE_SIZE); + palette_control->palette_changed = 0; + } + + type = LE_16(&buf[0]); + buf += MM_PREAMBLE_SIZE; + buf_size -= MM_PREAMBLE_SIZE; + + switch(type) { + case MM_TYPE_INTRA : mm_decode_intra(s, 0, 0, buf, buf_size); break; + case MM_TYPE_INTRA_HH : mm_decode_intra(s, 1, 0, buf, buf_size); break; + case MM_TYPE_INTRA_HHV : mm_decode_intra(s, 1, 1, buf, buf_size); break; + case MM_TYPE_INTER : mm_decode_inter(s, 0, 0, buf, buf_size); break; + case MM_TYPE_INTER_HH : mm_decode_inter(s, 1, 0, buf, buf_size); break; + case MM_TYPE_INTER_HHV : mm_decode_inter(s, 1, 1, buf, buf_size); break; + default : + return -1; + } + + *data_size = sizeof(AVFrame); + *(AVFrame*)data = s->frame; + + return buf_size; +} + +static int mm_decode_end(AVCodecContext *avctx) +{ + MmContext *s = avctx->priv_data; + + if(s->frame.data[0]) + avctx->release_buffer(avctx, &s->frame); + + return 0; +} + +AVCodec mmvideo_decoder = { + "mmvideo", + CODEC_TYPE_VIDEO, + CODEC_ID_MMVIDEO, + sizeof(MmContext), + mm_decode_init, + NULL, + mm_decode_end, + mm_decode_frame, + CODEC_CAP_DR1, +}; diff --git a/src/libffmpeg/libavcodec/nuv.c b/src/libffmpeg/libavcodec/nuv.c new file mode 100644 index 000000000..d31518250 --- /dev/null +++ b/src/libffmpeg/libavcodec/nuv.c @@ -0,0 +1,214 @@ +/* + * NuppelVideo decoder + * Copyright (c) 2006 Reimar Doeffinger + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include <stdio.h> +#include <stdlib.h> + +#include "common.h" +#include "avcodec.h" + +#include "bswap.h" +#include "dsputil.h" +#include "lzo.h" +#include "rtjpeg.h" + +typedef struct { + AVFrame pic; + int width, height; + unsigned int decomp_size; + unsigned char* decomp_buf; + uint32_t lq[64], cq[64]; + RTJpegContext rtj; + DSPContext dsp; +} NuvContext; + +/** + * \brief copy frame data from buffer to AVFrame, handling stride. + * \param f destination AVFrame + * \param src source buffer, does not use any line-stride + * \param width width of the video frame + * \param height height of the video frame + */ +static void copy_frame(AVFrame *f, uint8_t *src, + int width, int height) { + AVPicture pic; + avpicture_fill(&pic, src, PIX_FMT_YUV420P, width, height); + img_copy((AVPicture *)f, &pic, PIX_FMT_YUV420P, width, height); +} + +/** + * \brief extract quantization tables from codec data into our context + */ +static int get_quant(AVCodecContext *avctx, NuvContext *c, + uint8_t *buf, int size) { + int i; + if (size < 2 * 64 * 4) { + av_log(avctx, AV_LOG_ERROR, "insufficient rtjpeg quant data\n"); + return -1; + } + for (i = 0; i < 64; i++, buf += 4) + c->lq[i] = LE_32(buf); + for (i = 0; i < 64; i++, buf += 4) + c->cq[i] = LE_32(buf); + return 0; +} + +static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, + uint8_t *buf, int buf_size) { + NuvContext *c = (NuvContext *)avctx->priv_data; + AVFrame *picture = data; + int orig_size = buf_size; + enum {NUV_UNCOMPRESSED = '0', NUV_RTJPEG = '1', + NUV_RTJPEG_IN_LZO = '2', NUV_LZO = '3', + NUV_BLACK = 'N', NUV_COPY_LAST = 'L'} comptype; + + if (buf_size < 12) { + av_log(avctx, AV_LOG_ERROR, "coded frame too small\n"); + return -1; + } + + if (c->pic.data[0]) + avctx->release_buffer(avctx, &c->pic); + c->pic.reference = 1; + c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE | + FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE; + if (avctx->get_buffer(avctx, &c->pic) < 0) { + av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); + return -1; + } + + // codec data (rtjpeg quant tables) + if (buf[0] == 'D' && buf[1] == 'R') { + int ret; + // skip rest of the frameheader. + buf = &buf[12]; + buf_size -= 12; + ret = get_quant(avctx, c, buf, buf_size); + if (ret < 0) + return ret; + rtjpeg_decode_init(&c->rtj, &c->dsp, c->width, c->height, c->lq, c->cq); + return orig_size; + } + + if (buf[0] != 'V' || buf_size < 12) { + av_log(avctx, AV_LOG_ERROR, "not a nuv video frame\n"); + return -1; + } + comptype = buf[1]; + // skip rest of the frameheader. + buf = &buf[12]; + buf_size -= 12; + + c->pic.pict_type = FF_I_TYPE; + c->pic.key_frame = 1; + // decompress/copy/whatever data + switch (comptype) { + case NUV_UNCOMPRESSED: { + int height = c->height; + if (buf_size < c->width * height * 3 / 2) { + av_log(avctx, AV_LOG_ERROR, "uncompressed frame too short\n"); + height = buf_size / c->width / 3 * 2; + } + copy_frame(&c->pic, buf, c->width, height); + break; + } + case NUV_RTJPEG: { + rtjpeg_decode_frame_yuv420(&c->rtj, &c->pic, buf, buf_size); + break; + } + case NUV_RTJPEG_IN_LZO: { + int outlen = c->decomp_size, inlen = buf_size; + if (lzo1x_decode(c->decomp_buf, &outlen, buf, &inlen)) + av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n"); + rtjpeg_decode_frame_yuv420(&c->rtj, &c->pic, c->decomp_buf, c->decomp_size); + break; + } + case NUV_LZO: { + int outlen = c->decomp_size, inlen = buf_size; + if (lzo1x_decode(c->decomp_buf, &outlen, buf, &inlen)) + av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n"); + copy_frame(&c->pic, c->decomp_buf, c->width, c->height); + break; + } + case NUV_BLACK: { + memset(c->pic.data[0], 0, c->width * c->height); + memset(c->pic.data[1], 128, c->width * c->height / 4); + memset(c->pic.data[2], 128, c->width * c->height / 4); + break; + } + case NUV_COPY_LAST: { + c->pic.pict_type = FF_P_TYPE; + c->pic.key_frame = 0; + /* nothing more to do here */ + break; + } + default: + av_log(avctx, AV_LOG_ERROR, "unknown compression\n"); + return -1; + } + + *picture = c->pic; + *data_size = sizeof(AVFrame); + return orig_size; +} + +static int decode_init(AVCodecContext *avctx) { + NuvContext *c = (NuvContext *)avctx->priv_data; + avctx->width = (avctx->width + 1) & ~1; + avctx->height = (avctx->height + 1) & ~1; + if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) { + return 1; + } + avctx->has_b_frames = 0; + avctx->pix_fmt = PIX_FMT_YUV420P; + c->pic.data[0] = NULL; + c->width = avctx->width; + c->height = avctx->height; + c->decomp_size = c->height * c->width * 3 / 2; + c->decomp_buf = av_malloc(c->decomp_size + LZO_OUTPUT_PADDING); + if (!c->decomp_buf) { + av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n"); + return 1; + } + dsputil_init(&c->dsp, avctx); + if (avctx->extradata_size) + get_quant(avctx, c, avctx->extradata, avctx->extradata_size); + rtjpeg_decode_init(&c->rtj, &c->dsp, c->width, c->height, c->lq, c->cq); + return 0; +} + +static int decode_end(AVCodecContext *avctx) { + NuvContext *c = (NuvContext *)avctx->priv_data; + av_freep(&c->decomp_buf); + if (c->pic.data[0]) + avctx->release_buffer(avctx, &c->pic); + return 0; +} + +AVCodec nuv_decoder = { + "nuv", + CODEC_TYPE_VIDEO, + CODEC_ID_NUV, + sizeof(NuvContext), + decode_init, + NULL, + decode_end, + decode_frame, + CODEC_CAP_DR1, +}; + diff --git a/src/libffmpeg/libavcodec/rtjpeg.c b/src/libffmpeg/libavcodec/rtjpeg.c new file mode 100644 index 000000000..ebf10886b --- /dev/null +++ b/src/libffmpeg/libavcodec/rtjpeg.c @@ -0,0 +1,162 @@ +/* + * RTJpeg decoding functions + * Copyright (c) 2006 Reimar Doeffinger + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include "common.h" +#include "bitstream.h" +#include "dsputil.h" +#include "rtjpeg.h" + +#define PUT_COEFF(c) \ + i = scan[coeff--]; \ + block[i] = (c) * quant[i]; + +//! aligns the bitstream to the give power of two +#define ALIGN(a) \ + n = (-get_bits_count(gb)) & (a - 1); \ + if (n) {skip_bits(gb, n);} + +/** + * \brief read one block from stream + * \param gb contains stream data + * \param block where data is written to + * \param scan array containing the mapping stream address -> block position + * \param quant quantization factors + * + * Note: GetBitContext is used to make the code simpler, since all data is + * aligned this could be done faster in a different way, e.g. as it is done + * in MPlayer libmpcodecs/native/RTjpegN.c + */ +static inline int get_block(GetBitContext *gb, DCTELEM *block, uint8_t *scan, + uint32_t *quant) { + int coeff, i, n; + int8_t ac; + uint8_t dc = get_bits(gb, 8); + + // block not coded + if (dc == 255) + return 0; + + // number of non-zero coefficients + coeff = get_bits(gb, 6); + // normally we would only need to clear the (63 - coeff) last values, + // but since we do not know where they are we just clear the whole block + memset(block, 0, 64 * sizeof(DCTELEM)); + + // 2 bits per coefficient + while (coeff) { + ac = get_sbits(gb, 2); + if (ac == -2) + break; // continue with more bits + PUT_COEFF(ac); + } + + // 4 bits per coefficient + ALIGN(4); + while (coeff) { + ac = get_sbits(gb, 4); + if (ac == -8) + break; // continue with more bits + PUT_COEFF(ac); + } + + // 8 bits per coefficient + ALIGN(8); + while (coeff) { + ac = get_sbits(gb, 8); + PUT_COEFF(ac); + } + + PUT_COEFF(dc); + return 1; +} + +/** + * \brief decode one rtjpeg YUV420 frame + * \param c context, must be initialized via rtjpeg_decode_init + * \param f AVFrame to place decoded frame into. If parts of the frame + * are not coded they are left unchanged, so consider initializing it + * \param buf buffer containing input data + * \param buf_size length of input data in bytes + * \return number of bytes consumed from the input buffer + */ +int rtjpeg_decode_frame_yuv420(RTJpegContext *c, AVFrame *f, + uint8_t *buf, int buf_size) { + GetBitContext gb; + int w = c->w / 16, h = c->h / 16; + int x, y; + void *y1 = f->data[0], *y2 = f->data[0] + 8 * f->linesize[0]; + void *u = f->data[1], *v = f->data[2]; + init_get_bits(&gb, buf, buf_size * 8); + for (y = 0; y < h; y++) { + for (x = 0; x < w; x++) { + if (get_block(&gb, c->block, c->scan, c->lquant)) + c->dsp->idct_put(y1, f->linesize[0], c->block); + y1 += 8; + if (get_block(&gb, c->block, c->scan, c->lquant)) + c->dsp->idct_put(y1, f->linesize[0], c->block); + y1 += 8; + if (get_block(&gb, c->block, c->scan, c->lquant)) + c->dsp->idct_put(y2, f->linesize[0], c->block); + y2 += 8; + if (get_block(&gb, c->block, c->scan, c->lquant)) + c->dsp->idct_put(y2, f->linesize[0], c->block); + y2 += 8; + if (get_block(&gb, c->block, c->scan, c->cquant)) + c->dsp->idct_put(u, f->linesize[1], c->block); + u += 8; + if (get_block(&gb, c->block, c->scan, c->cquant)) + c->dsp->idct_put(v, f->linesize[2], c->block); + v += 8; + } + y1 += 2 * 8 * (f->linesize[0] - w); + y2 += 2 * 8 * (f->linesize[0] - w); + u += 8 * (f->linesize[1] - w); + v += 8 * (f->linesize[2] - w); + } + return get_bits_count(&gb) / 8; +} + +/** + * \brief initialize an RTJpegContext, may be called multiple times + * \param c context to initialize + * \param dsp specifies the idct to use for decoding + * \param width width of image, will be rounded down to the nearest multiple + * of 16 for decoding + * \param height height of image, will be rounded down to the nearest multiple + * of 16 for decoding + * \param lquant luma quantization table to use + * \param cquant chroma quantization table to use + */ +void rtjpeg_decode_init(RTJpegContext *c, DSPContext *dsp, + int width, int height, + uint32_t *lquant, uint32_t *cquant) { + int i; + c->dsp = dsp; + for (i = 0; i < 64; i++) { + int z = ff_zigzag_direct[i]; + int p = c->dsp->idct_permutation[i]; + z = ((z << 3) | (z >> 3)) & 63; // rtjpeg uses a transposed variant + + // permute the scan and quantization tables for the chosen idct + c->scan[i] = c->dsp->idct_permutation[z]; + c->lquant[p] = lquant[i]; + c->cquant[p] = cquant[i]; + } + c->w = width; + c->h = height; +} diff --git a/src/libffmpeg/libavcodec/smacker.c b/src/libffmpeg/libavcodec/smacker.c new file mode 100644 index 000000000..162c68ada --- /dev/null +++ b/src/libffmpeg/libavcodec/smacker.c @@ -0,0 +1,725 @@ +/* + * Smacker decoder + * Copyright (c) 2006 Konstantin Shishkov + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +/** + * @file smacker.c + * Smacker decoder + */ + +/* + * Based on http://wiki.multimedia.cx/index.php?title=Smacker + */ + +#include <stdio.h> +#include <stdlib.h> + +#include "common.h" +#include "avcodec.h" + +#define ALT_BITSTREAM_READER_LE +#include "bitstream.h" + +#define SMKTREE_BITS 9 +#define SMK_NODE 0x80000000 + +/* + * Decoder context + */ +typedef struct SmackVContext { + AVCodecContext *avctx; + AVFrame pic; + + int *mmap_tbl, *mclr_tbl, *full_tbl, *type_tbl; + int mmap_last[3], mclr_last[3], full_last[3], type_last[3]; +} SmackVContext; + +/** + * Context used for code reconstructing + */ +typedef struct HuffContext { + int length; + int maxlength; + int current; + uint32_t *bits; + int *lengths; + int *values; +} HuffContext; + +/* common parameters used for decode_bigtree */ +typedef struct DBCtx { + VLC *v1, *v2; + int *recode1, *recode2; + int escapes[3]; + int *last; + int lcur; +} DBCtx; + +/* possible runs of blocks */ +static const int block_runs[64] = { + 1, 2, 3, 4, 5, 6, 7, 8, + 9, 10, 11, 12, 13, 14, 15, 16, + 17, 18, 19, 20, 21, 22, 23, 24, + 25, 26, 27, 28, 29, 30, 31, 32, + 33, 34, 35, 36, 37, 38, 39, 40, + 41, 42, 43, 44, 45, 46, 47, 48, + 49, 50, 51, 52, 53, 54, 55, 56, + 57, 58, 59, 128, 256, 512, 1024, 2048 }; + +enum SmkBlockTypes { + SMK_BLK_MONO = 0, + SMK_BLK_FULL = 1, + SMK_BLK_SKIP = 2, + SMK_BLK_FILL = 3 }; + +/** + * Decode local frame tree + */ +static int smacker_decode_tree(GetBitContext *gb, HuffContext *hc, uint32_t prefix, int length) +{ + if(!get_bits1(gb)){ //Leaf + if(hc->current >= 256){ + av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n"); + return -1; + } + if(length){ + hc->bits[hc->current] = prefix; + hc->lengths[hc->current] = length; + } else { + hc->bits[hc->current] = 0; + hc->lengths[hc->current] = 0; + } + hc->values[hc->current] = get_bits(gb, 8); + hc->current++; + if(hc->maxlength < length) + hc->maxlength = length; + return 0; + } else { //Node + int r; + length++; + r = smacker_decode_tree(gb, hc, prefix, length); + if(r) + return r; + return smacker_decode_tree(gb, hc, prefix | (1 << (length - 1)), length); + } +} + +/** + * Decode header tree + */ +static int smacker_decode_bigtree(GetBitContext *gb, HuffContext *hc, DBCtx *ctx) +{ + if(!get_bits1(gb)){ //Leaf + int val, i1, i2, b1, b2; + if(hc->current >= hc->length){ + av_log(NULL, AV_LOG_ERROR, "Tree size exceeded!\n"); + return -1; + } + b1 = get_bits_count(gb); + i1 = get_vlc2(gb, ctx->v1->table, SMKTREE_BITS, 3); + b1 = get_bits_count(gb) - b1; + b2 = get_bits_count(gb); + i2 = get_vlc2(gb, ctx->v2->table, SMKTREE_BITS, 3); + b2 = get_bits_count(gb) - b2; + val = ctx->recode1[i1] | (ctx->recode2[i2] << 8); + if(val == ctx->escapes[0]) { + ctx->last[0] = hc->current; + val = 0; + } else if(val == ctx->escapes[1]) { + ctx->last[1] = hc->current; + val = 0; + } else if(val == ctx->escapes[2]) { + ctx->last[2] = hc->current; + val = 0; + } + + hc->values[hc->current++] = val; + return 1; + } else { //Node + int r = 0, t; + + t = hc->current++; + r = smacker_decode_bigtree(gb, hc, ctx); + if(r < 0) + return r; + hc->values[t] = SMK_NODE | r; + r++; + r += smacker_decode_bigtree(gb, hc, ctx); + return r; + } +} + +/** + * Store large tree as FFmpeg's vlc codes + */ +static int smacker_decode_header_tree(SmackVContext *smk, GetBitContext *gb, int **recodes, int *last, int size) +{ + int res; + HuffContext huff; + HuffContext tmp1, tmp2; + VLC vlc[2]; + int escapes[3]; + DBCtx ctx; + + if(size >= UINT_MAX>>4){ // (((size + 3) >> 2) + 3) << 2 must not overflow + av_log(smk->avctx, AV_LOG_ERROR, "size too large\n"); + return -1; + } + + tmp1.length = 256; + tmp1.maxlength = 0; + tmp1.current = 0; + tmp1.bits = av_mallocz(256 * 4); + tmp1.lengths = av_mallocz(256 * sizeof(int)); + tmp1.values = av_mallocz(256 * sizeof(int)); + + tmp2.length = 256; + tmp2.maxlength = 0; + tmp2.current = 0; + tmp2.bits = av_mallocz(256 * 4); + tmp2.lengths = av_mallocz(256 * sizeof(int)); + tmp2.values = av_mallocz(256 * sizeof(int)); + + memset(&vlc[0], 0, sizeof(VLC)); + memset(&vlc[1], 0, sizeof(VLC)); + + if(get_bits1(gb)) { + smacker_decode_tree(gb, &tmp1, 0, 0); + get_bits1(gb); + res = init_vlc(&vlc[0], SMKTREE_BITS, tmp1.length, + tmp1.lengths, sizeof(int), sizeof(int), + tmp1.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE); + if(res < 0) { + av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n"); + return -1; + } + } else { + av_log(smk->avctx, AV_LOG_ERROR, "Skipping low bytes tree\n"); + } + if(get_bits1(gb)){ + smacker_decode_tree(gb, &tmp2, 0, 0); + get_bits1(gb); + res = init_vlc(&vlc[1], SMKTREE_BITS, tmp2.length, + tmp2.lengths, sizeof(int), sizeof(int), + tmp2.bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE); + if(res < 0) { + av_log(smk->avctx, AV_LOG_ERROR, "Cannot build VLC table\n"); + return -1; + } + } else { + av_log(smk->avctx, AV_LOG_ERROR, "Skipping high bytes tree\n"); + } + + escapes[0] = get_bits(gb, 8); + escapes[0] |= get_bits(gb, 8) << 8; + escapes[1] = get_bits(gb, 8); + escapes[1] |= get_bits(gb, 8) << 8; + escapes[2] = get_bits(gb, 8); + escapes[2] |= get_bits(gb, 8) << 8; + + last[0] = last[1] = last[2] = -1; + + ctx.escapes[0] = escapes[0]; + ctx.escapes[1] = escapes[1]; + ctx.escapes[2] = escapes[2]; + ctx.v1 = &vlc[0]; + ctx.v2 = &vlc[1]; + ctx.recode1 = tmp1.values; + ctx.recode2 = tmp2.values; + ctx.last = last; + + huff.length = ((size + 3) >> 2) + 3; + huff.maxlength = 0; + huff.current = 0; + huff.values = av_mallocz(huff.length * sizeof(int)); + + smacker_decode_bigtree(gb, &huff, &ctx); + get_bits1(gb); + if(ctx.last[0] == -1) ctx.last[0] = huff.current++; + if(ctx.last[1] == -1) ctx.last[1] = huff.current++; + if(ctx.last[2] == -1) ctx.last[2] = huff.current++; + + *recodes = huff.values; + + if(vlc[0].table) + free_vlc(&vlc[0]); + if(vlc[1].table) + free_vlc(&vlc[1]); + av_free(tmp1.bits); + av_free(tmp1.lengths); + av_free(tmp1.values); + av_free(tmp2.bits); + av_free(tmp2.lengths); + av_free(tmp2.values); + + return 0; +} + +static int decode_header_trees(SmackVContext *smk) { + GetBitContext gb; + int mmap_size, mclr_size, full_size, type_size; + + mmap_size = LE_32(smk->avctx->extradata); + mclr_size = LE_32(smk->avctx->extradata + 4); + full_size = LE_32(smk->avctx->extradata + 8); + type_size = LE_32(smk->avctx->extradata + 12); + + init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8); + + if(!get_bits1(&gb)) { + av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n"); + smk->mmap_tbl = av_malloc(sizeof(int) * 2); + smk->mmap_tbl[0] = 0; + smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1; + } else { + smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size); + } + if(!get_bits(&gb, 1)) { + av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n"); + smk->mclr_tbl = av_malloc(sizeof(int) * 2); + smk->mclr_tbl[0] = 0; + smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1; + } else { + smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size); + } + if(!get_bits(&gb, 1)) { + av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n"); + smk->full_tbl = av_malloc(sizeof(int) * 2); + smk->full_tbl[0] = 0; + smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1; + } else { + smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size); + } + if(!get_bits(&gb, 1)) { + av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n"); + smk->type_tbl = av_malloc(sizeof(int) * 2); + smk->type_tbl[0] = 0; + smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1; + } else { + smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size); + } + + return 0; +} + +static always_inline void last_reset(int *recode, int *last) { + recode[last[0]] = recode[last[1]] = recode[last[2]] = 0; +} + +/* get code and update history */ +static always_inline int smk_get_code(GetBitContext *gb, int *recode, int *last) { + register int *table = recode; + int v, b; + + b = get_bits_count(gb); + while(*table & SMK_NODE) { + if(get_bits1(gb)) + table += (*table) & (~SMK_NODE); + table++; + } + v = *table; + b = get_bits_count(gb) - b; + + if(v != recode[last[0]]) { + recode[last[2]] = recode[last[1]]; + recode[last[1]] = recode[last[0]]; + recode[last[0]] = v; + } + return v; +} + +static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size) +{ + SmackVContext * const smk = (SmackVContext *)avctx->priv_data; + uint8_t *out; + uint32_t *pal; + GetBitContext gb; + int blocks, blk, bw, bh; + int i; + int stride; + + if(buf_size == 769) + return 0; + if(smk->pic.data[0]) + avctx->release_buffer(avctx, &smk->pic); + + smk->pic.reference = 1; + smk->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE; + if(avctx->reget_buffer(avctx, &smk->pic) < 0){ + av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); + return -1; + } + + /* make the palette available on the way out */ + out = buf + 1; + pal = (uint32_t*)smk->pic.data[1]; + smk->pic.palette_has_changed = buf[0] & 1; + smk->pic.key_frame = !!(buf[0] & 2); + if(smk->pic.key_frame) + smk->pic.pict_type = FF_I_TYPE; + else + smk->pic.pict_type = FF_P_TYPE; + + for(i = 0; i < 256; i++) { + int r, g, b; + r = *out++; + g = *out++; + b = *out++; + *pal++ = (r << 16) | (g << 8) | b; + } + + last_reset(smk->mmap_tbl, smk->mmap_last); + last_reset(smk->mclr_tbl, smk->mclr_last); + last_reset(smk->full_tbl, smk->full_last); + last_reset(smk->type_tbl, smk->type_last); + init_get_bits(&gb, buf + 769, (buf_size - 769) * 8); + + blk = 0; + bw = avctx->width >> 2; + bh = avctx->height >> 2; + blocks = bw * bh; + out = smk->pic.data[0]; + stride = smk->pic.linesize[0]; + while(blk < blocks) { + int type, run, mode; + uint16_t pix; + + type = smk_get_code(&gb, smk->type_tbl, smk->type_last); + run = block_runs[(type >> 2) & 0x3F]; + switch(type & 3){ + case SMK_BLK_MONO: + while(run-- && blk < blocks){ + int clr, map; + int hi, lo; + clr = smk_get_code(&gb, smk->mclr_tbl, smk->mclr_last); + map = smk_get_code(&gb, smk->mmap_tbl, smk->mmap_last); + out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4; + hi = clr >> 8; + lo = clr & 0xFF; + for(i = 0; i < 4; i++) { + if(map & 1) out[0] = hi; else out[0] = lo; + if(map & 2) out[1] = hi; else out[1] = lo; + if(map & 4) out[2] = hi; else out[2] = lo; + if(map & 8) out[3] = hi; else out[3] = lo; + map >>= 4; + out += stride; + } + blk++; + } + break; + case SMK_BLK_FULL: + mode = 0; + if(avctx->codec_tag == MKTAG('S', 'M', 'K', '4')) { // In case of Smacker v4 we have three modes + if(get_bits1(&gb)) mode = 1; + else if(get_bits1(&gb)) mode = 2; + } + while(run-- && blk < blocks){ + out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4; + switch(mode){ + case 0: + for(i = 0; i < 4; i++) { + pix = smk_get_code(&gb, smk->full_tbl, smk->full_last); + out[2] = pix & 0xFF; + out[3] = pix >> 8; + pix = smk_get_code(&gb, smk->full_tbl, smk->full_last); + out[0] = pix & 0xFF; + out[1] = pix >> 8; + out += stride; + } + break; + case 1: + pix = smk_get_code(&gb, smk->full_tbl, smk->full_last); + out[0] = out[1] = pix & 0xFF; + out[2] = out[3] = pix >> 8; + out += stride; + out[0] = out[1] = pix & 0xFF; + out[2] = out[3] = pix >> 8; + out += stride; + pix = smk_get_code(&gb, smk->full_tbl, smk->full_last); + out[0] = out[1] = pix & 0xFF; + out[2] = out[3] = pix >> 8; + out += stride; + out[0] = out[1] = pix & 0xFF; + out[2] = out[3] = pix >> 8; + out += stride; + break; + case 2: + for(i = 0; i < 2; i++) { + uint16_t pix1, pix2; + pix1 = smk_get_code(&gb, smk->full_tbl, smk->full_last); + pix2 = smk_get_code(&gb, smk->full_tbl, smk->full_last); + out[0] = pix1 & 0xFF; out[1] = pix1 >> 8; + out[2] = pix2 & 0xFF; out[3] = pix2 >> 8; + out += stride; + out[0] = pix1 & 0xFF; out[1] = pix1 >> 8; + out[2] = pix2 & 0xFF; out[3] = pix2 >> 8; + out += stride; + } + break; + } + blk++; + } + break; + case SMK_BLK_SKIP: + while(run-- && blk < blocks) + blk++; + break; + case SMK_BLK_FILL: + mode = type >> 8; + while(run-- && blk < blocks){ + uint32_t col; + out = smk->pic.data[0] + (blk / bw) * (stride * 4) + (blk % bw) * 4; + col = mode * 0x01010101; + for(i = 0; i < 4; i++) { + *((uint32_t*)out) = col; + out += stride; + } + blk++; + } + break; + } + + } + + *data_size = sizeof(AVFrame); + *(AVFrame*)data = smk->pic; + + /* always report that the buffer was completely consumed */ + return buf_size; +} + + + +/* + * + * Init smacker decoder + * + */ +static int decode_init(AVCodecContext *avctx) +{ + SmackVContext * const c = (SmackVContext *)avctx->priv_data; + + c->avctx = avctx; + avctx->has_b_frames = 0; + + c->pic.data[0] = NULL; + + if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) { + return 1; + } + + avctx->pix_fmt = PIX_FMT_PAL8; + + + /* decode huffman trees from extradata */ + if(avctx->extradata_size < 16){ + av_log(avctx, AV_LOG_ERROR, "Extradata missing!\n"); + return -1; + } + + decode_header_trees(c); + + + return 0; +} + + + +/* + * + * Uninit smacker decoder + * + */ +static int decode_end(AVCodecContext *avctx) +{ + SmackVContext * const smk = (SmackVContext *)avctx->priv_data; + + if(smk->mmap_tbl) + av_free(smk->mmap_tbl); + if(smk->mclr_tbl) + av_free(smk->mclr_tbl); + if(smk->full_tbl) + av_free(smk->full_tbl); + if(smk->type_tbl) + av_free(smk->type_tbl); + + if (smk->pic.data[0]) + avctx->release_buffer(avctx, &smk->pic); + + return 0; +} + + +static int smka_decode_init(AVCodecContext *avctx) +{ + return 0; +} + +/** + * Decode Smacker audio data + */ +static int smka_decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size) +{ + GetBitContext gb; + HuffContext h[4]; + VLC vlc[4]; + int16_t *samples = data; + int val; + int i, res; + int unp_size; + int bits, stereo; + int pred[2] = {0, 0}; + + unp_size = LE_32(buf); + + init_get_bits(&gb, buf + 4, (buf_size - 4) * 8); + + if(!get_bits1(&gb)){ + av_log(avctx, AV_LOG_INFO, "Sound: no data\n"); + *data_size = 0; + return 1; + } + stereo = get_bits1(&gb); + bits = get_bits1(&gb); + + memset(vlc, 0, sizeof(VLC) * 4); + memset(h, 0, sizeof(HuffContext) * 4); + // Initialize + for(i = 0; i < (1 << (bits + stereo)); i++) { + h[i].length = 256; + h[i].maxlength = 0; + h[i].current = 0; + h[i].bits = av_mallocz(256 * 4); + h[i].lengths = av_mallocz(256 * sizeof(int)); + h[i].values = av_mallocz(256 * sizeof(int)); + get_bits1(&gb); + smacker_decode_tree(&gb, &h[i], 0, 0); + get_bits1(&gb); + if(h[i].current > 1) { + res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length, + h[i].lengths, sizeof(int), sizeof(int), + h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE); + if(res < 0) { + av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n"); + return -1; + } + } + } + if(bits) { //decode 16-bit data + pred[0] = get_bits(&gb, 8); + pred[0] |= get_bits(&gb, 8); + *samples++ = pred[0]; + if(stereo) { + pred[1] = get_bits(&gb, 8); + pred[1] |= get_bits(&gb, 8); + *samples++ = pred[1]; + } + for(i = 0; i < unp_size / 2; i++) { + if(i & stereo) { + if(vlc[2].table) + res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3); + else + res = 0; + val = h[2].values[res]; + if(vlc[3].table) + res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3); + else + res = 0; + val |= h[3].values[res] << 8; + pred[1] += (int16_t)val; + *samples++ = pred[1]; + } else { + if(vlc[0].table) + res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3); + else + res = 0; + val = h[0].values[res]; + if(vlc[1].table) + res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3); + else + res = 0; + val |= h[1].values[res] << 8; + pred[0] += val; + *samples++ = pred[0]; + } + } + } else { //8-bit data + pred[0] = get_bits(&gb, 8); + *samples++ = (pred[0] - 0x80) << 8; + if(stereo) { + pred[1] = get_bits(&gb, 8); + *samples++ = (pred[1] - 0x80) << 8; + } + for(i = 0; i < unp_size; i++) { + if(i & stereo){ + if(vlc[1].table) + res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3); + else + res = 0; + pred[1] += (int8_t)h[1].values[res]; + *samples++ = (pred[1] - 0x80) << 8; + } else { + if(vlc[0].table) + res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3); + else + res = 0; + pred[0] += (int8_t)h[0].values[res]; + *samples++ = (pred[0] - 0x80) << 8; + } + } + unp_size *= 2; + } + + for(i = 0; i < 4; i++) { + if(vlc[i].table) + free_vlc(&vlc[i]); + if(h[i].bits) + av_free(h[i].bits); + if(h[i].lengths) + av_free(h[i].lengths); + if(h[i].values) + av_free(h[i].values); + } + + *data_size = unp_size; + return buf_size; +} + +AVCodec smacker_decoder = { + "smackvid", + CODEC_TYPE_VIDEO, + CODEC_ID_SMACKVIDEO, + sizeof(SmackVContext), + decode_init, + NULL, + decode_end, + decode_frame +}; + +AVCodec smackaud_decoder = { + "smackaud", + CODEC_TYPE_AUDIO, + CODEC_ID_SMACKAUDIO, + 0, + smka_decode_init, + NULL, + NULL, + smka_decode_frame +}; + diff --git a/src/libffmpeg/libavcodec/truespeech.c b/src/libffmpeg/libavcodec/truespeech.c new file mode 100644 index 000000000..dbd29b38f --- /dev/null +++ b/src/libffmpeg/libavcodec/truespeech.c @@ -0,0 +1,379 @@ +/* + * DSP Group TrueSpeech compatible decoder + * Copyright (c) 2005 Konstantin Shishkov + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include "avcodec.h" + +#include "truespeech_data.h" +/** + * @file truespeech.c + * TrueSpeech decoder. + */ + +/** + * TrueSpeech decoder context + */ +typedef struct { + /* input data */ + int16_t vector[8]; //< input vector: 5/5/4/4/4/3/3/3 + int offset1[2]; //< 8-bit value, used in one copying offset + int offset2[4]; //< 7-bit value, encodes offsets for copying and for two-point filter + int pulseoff[4]; //< 4-bit offset of pulse values block + int pulsepos[4]; //< 27-bit variable, encodes 7 pulse positions + int pulseval[4]; //< 7x2-bit pulse values + int flag; //< 1-bit flag, shows how to choose filters + /* temporary data */ + int filtbuf[146]; // some big vector used for storing filters + int prevfilt[8]; // filter from previous frame + int16_t tmp1[8]; // coefficients for adding to out + int16_t tmp2[8]; // coefficients for adding to out + int16_t tmp3[8]; // coefficients for adding to out + int16_t cvector[8]; // correlated input vector + int filtval; // gain value for one function + int16_t newvec[60]; // tmp vector + int16_t filters[32]; // filters for every subframe +} TSContext; + +static int truespeech_decode_init(AVCodecContext * avctx) +{ +// TSContext *c = avctx->priv_data; + + return 0; +} + +static void truespeech_read_frame(TSContext *dec, uint8_t *input) +{ + uint32_t t; + + /* first dword */ + t = LE_32(input); + input += 4; + + dec->flag = t & 1; + + dec->vector[0] = ts_codebook[0][(t >> 1) & 0x1F]; + dec->vector[1] = ts_codebook[1][(t >> 6) & 0x1F]; + dec->vector[2] = ts_codebook[2][(t >> 11) & 0xF]; + dec->vector[3] = ts_codebook[3][(t >> 15) & 0xF]; + dec->vector[4] = ts_codebook[4][(t >> 19) & 0xF]; + dec->vector[5] = ts_codebook[5][(t >> 23) & 0x7]; + dec->vector[6] = ts_codebook[6][(t >> 26) & 0x7]; + dec->vector[7] = ts_codebook[7][(t >> 29) & 0x7]; + + /* second dword */ + t = LE_32(input); + input += 4; + + dec->offset2[0] = (t >> 0) & 0x7F; + dec->offset2[1] = (t >> 7) & 0x7F; + dec->offset2[2] = (t >> 14) & 0x7F; + dec->offset2[3] = (t >> 21) & 0x7F; + + dec->offset1[0] = ((t >> 28) & 0xF) << 4; + + /* third dword */ + t = LE_32(input); + input += 4; + + dec->pulseval[0] = (t >> 0) & 0x3FFF; + dec->pulseval[1] = (t >> 14) & 0x3FFF; + + dec->offset1[1] = (t >> 28) & 0x0F; + + /* fourth dword */ + t = LE_32(input); + input += 4; + + dec->pulseval[2] = (t >> 0) & 0x3FFF; + dec->pulseval[3] = (t >> 14) & 0x3FFF; + + dec->offset1[1] |= ((t >> 28) & 0x0F) << 4; + + /* fifth dword */ + t = LE_32(input); + input += 4; + + dec->pulsepos[0] = (t >> 4) & 0x7FFFFFF; + + dec->pulseoff[0] = (t >> 0) & 0xF; + + dec->offset1[0] |= (t >> 31) & 1; + + /* sixth dword */ + t = LE_32(input); + input += 4; + + dec->pulsepos[1] = (t >> 4) & 0x7FFFFFF; + + dec->pulseoff[1] = (t >> 0) & 0xF; + + dec->offset1[0] |= ((t >> 31) & 1) << 1; + + /* seventh dword */ + t = LE_32(input); + input += 4; + + dec->pulsepos[2] = (t >> 4) & 0x7FFFFFF; + + dec->pulseoff[2] = (t >> 0) & 0xF; + + dec->offset1[0] |= ((t >> 31) & 1) << 2; + + /* eighth dword */ + t = LE_32(input); + input += 4; + + dec->pulsepos[3] = (t >> 4) & 0x7FFFFFF; + + dec->pulseoff[3] = (t >> 0) & 0xF; + + dec->offset1[0] |= ((t >> 31) & 1) << 3; + +} + +static void truespeech_correlate_filter(TSContext *dec) +{ + int16_t tmp[8]; + int i, j; + + for(i = 0; i < 8; i++){ + if(i > 0){ + memcpy(tmp, dec->cvector, i * 2); + for(j = 0; j < i; j++) + dec->cvector[j] = ((tmp[i - j - 1] * dec->vector[i]) + + (dec->cvector[j] << 15) + 0x4000) >> 15; + } + dec->cvector[i] = (8 - dec->vector[i]) >> 3; + } + for(i = 0; i < 8; i++) + dec->cvector[i] = (dec->cvector[i] * ts_230[i]) >> 15; + + dec->filtval = dec->vector[0]; +} + +static void truespeech_filters_merge(TSContext *dec) +{ + int i; + + if(!dec->flag){ + for(i = 0; i < 8; i++){ + dec->filters[i + 0] = dec->prevfilt[i]; + dec->filters[i + 8] = dec->prevfilt[i]; + } + }else{ + for(i = 0; i < 8; i++){ + dec->filters[i + 0]=(dec->cvector[i] * 21846 + dec->prevfilt[i] * 10923 + 16384) >> 15; + dec->filters[i + 8]=(dec->cvector[i] * 10923 + dec->prevfilt[i] * 21846 + 16384) >> 15; + } + } + for(i = 0; i < 8; i++){ + dec->filters[i + 16] = dec->cvector[i]; + dec->filters[i + 24] = dec->cvector[i]; + } +} + +static void truespeech_apply_twopoint_filter(TSContext *dec, int quart) +{ + int16_t tmp[146 + 60], *ptr0, *ptr1; + const int16_t *filter; + int i, t, off; + + t = dec->offset2[quart]; + if(t == 127){ + memset(dec->newvec, 0, 60 * 2); + return; + } + for(i = 0; i < 146; i++) + tmp[i] = dec->filtbuf[i]; + off = (t / 25) + dec->offset1[quart >> 1] + 18; + ptr0 = tmp + 145 - off; + ptr1 = tmp + 146; + filter = (const int16_t*)ts_240 + (t % 25) * 2; + for(i = 0; i < 60; i++){ + t = (ptr0[0] * filter[0] + ptr0[1] * filter[1] + 0x2000) >> 14; + ptr0++; + dec->newvec[i] = t; + ptr1[i] = t; + } +} + +static void truespeech_place_pulses(TSContext *dec, int16_t *out, int quart) +{ + int16_t tmp[7]; + int i, j, t; + const int16_t *ptr1; + int16_t *ptr2; + int coef; + + memset(out, 0, 60 * 2); + for(i = 0; i < 7; i++) { + t = dec->pulseval[quart] & 3; + dec->pulseval[quart] >>= 2; + tmp[6 - i] = ts_562[dec->pulseoff[quart] * 4 + t]; + } + + coef = dec->pulsepos[quart] >> 15; + ptr1 = (const int16_t*)ts_140 + 30; + ptr2 = tmp; + for(i = 0, j = 3; (i < 30) && (j > 0); i++){ + t = *ptr1++; + if(coef >= t) + coef -= t; + else{ + out[i] = *ptr2++; + ptr1 += 30; + j--; + } + } + coef = dec->pulsepos[quart] & 0x7FFF; + ptr1 = (const int16_t*)ts_140; + for(i = 30, j = 4; (i < 60) && (j > 0); i++){ + t = *ptr1++; + if(coef >= t) + coef -= t; + else{ + out[i] = *ptr2++; + ptr1 += 30; + j--; + } + } + +} + +static void truespeech_update_filters(TSContext *dec, int16_t *out, int quart) +{ + int i; + + for(i = 0; i < 86; i++) + dec->filtbuf[i] = dec->filtbuf[i + 60]; + for(i = 0; i < 60; i++){ + dec->filtbuf[i + 86] = out[i] + dec->newvec[i] - (dec->newvec[i] >> 3); + out[i] += dec->newvec[i]; + } +} + +static void truespeech_synth(TSContext *dec, int16_t *out, int quart) +{ + int i,k; + int t[8]; + int16_t *ptr0, *ptr1; + + ptr0 = dec->tmp1; + ptr1 = dec->filters + quart * 8; + for(i = 0; i < 60; i++){ + int sum = 0; + for(k = 0; k < 8; k++) + sum += ptr0[k] * ptr1[k]; + sum = (sum + (out[i] << 12) + 0x800) >> 12; + out[i] = clip(sum, -0x7FFE, 0x7FFE); + for(k = 7; k > 0; k--) + ptr0[k] = ptr0[k - 1]; + ptr0[0] = out[i]; + } + + for(i = 0; i < 8; i++) + t[i] = (ts_5E2[i] * ptr1[i]) >> 15; + + ptr0 = dec->tmp2; + for(i = 0; i < 60; i++){ + int sum = 0; + for(k = 0; k < 8; k++) + sum += ptr0[k] * t[k]; + for(k = 7; k > 0; k--) + ptr0[k] = ptr0[k - 1]; + ptr0[0] = out[i]; + out[i] = ((out[i] << 12) - sum) >> 12; + } + + for(i = 0; i < 8; i++) + t[i] = (ts_5F2[i] * ptr1[i]) >> 15; + + ptr0 = dec->tmp3; + for(i = 0; i < 60; i++){ + int sum = out[i] << 12; + for(k = 0; k < 8; k++) + sum += ptr0[k] * t[k]; + for(k = 7; k > 0; k--) + ptr0[k] = ptr0[k - 1]; + ptr0[0] = clip((sum + 0x800) >> 12, -0x7FFE, 0x7FFE); + + sum = ((ptr0[1] * (dec->filtval - (dec->filtval >> 2))) >> 4) + sum; + sum = sum - (sum >> 3); + out[i] = clip((sum + 0x800) >> 12, -0x7FFE, 0x7FFE); + } +} + +static void truespeech_save_prevvec(TSContext *c) +{ + int i; + + for(i = 0; i < 8; i++) + c->prevfilt[i] = c->cvector[i]; +} + +static int truespeech_decode_frame(AVCodecContext *avctx, + void *data, int *data_size, + uint8_t *buf, int buf_size) +{ + TSContext *c = avctx->priv_data; + + int i; + short *samples = data; + int consumed = 0; + int16_t out_buf[240]; + + if (!buf_size) + return 0; + + while (consumed < buf_size) { + truespeech_read_frame(c, buf + consumed); + consumed += 32; + + truespeech_correlate_filter(c); + truespeech_filters_merge(c); + + memset(out_buf, 0, 240 * 2); + for(i = 0; i < 4; i++) { + truespeech_apply_twopoint_filter(c, i); + truespeech_place_pulses(c, out_buf + i * 60, i); + truespeech_update_filters(c, out_buf + i * 60, i); + truespeech_synth(c, out_buf + i * 60, i); + } + + truespeech_save_prevvec(c); + + /* finally output decoded frame */ + for(i = 0; i < 240; i++) + *samples++ = out_buf[i]; + + } + + *data_size = consumed * 15; + + return buf_size; +} + +AVCodec truespeech_decoder = { + "truespeech", + CODEC_TYPE_AUDIO, + CODEC_ID_TRUESPEECH, + sizeof(TSContext), + truespeech_decode_init, + NULL, + NULL, + truespeech_decode_frame, +}; diff --git a/src/libffmpeg/libavcodec/truespeech_data.h b/src/libffmpeg/libavcodec/truespeech_data.h new file mode 100644 index 000000000..9a9007234 --- /dev/null +++ b/src/libffmpeg/libavcodec/truespeech_data.h @@ -0,0 +1,136 @@ +#ifndef __TRUESPEECH_DATA__ +#define __TRUESPEECH_DATA__ + +/* codebooks fo expanding input filter */ +static const int16_t ts_cb_0[32] = { + 0x8240, 0x8364, 0x84CE, 0x865D, 0x8805, 0x89DE, 0x8BD7, 0x8DF4, + 0x9051, 0x92E2, 0x95DE, 0x990F, 0x9C81, 0xA079, 0xA54C, 0xAAD2, + 0xB18A, 0xB90A, 0xC124, 0xC9CC, 0xD339, 0xDDD3, 0xE9D6, 0xF893, + 0x096F, 0x1ACA, 0x29EC, 0x381F, 0x45F9, 0x546A, 0x63C3, 0x73B5, +}; + +static const int16_t ts_cb_1[32] = { + 0x9F65, 0xB56B, 0xC583, 0xD371, 0xE018, 0xEBB4, 0xF61C, 0xFF59, + 0x085B, 0x1106, 0x1952, 0x214A, 0x28C9, 0x2FF8, 0x36E6, 0x3D92, + 0x43DF, 0x49BB, 0x4F46, 0x5467, 0x5930, 0x5DA3, 0x61EC, 0x65F9, + 0x69D4, 0x6D5A, 0x709E, 0x73AD, 0x766B, 0x78F0, 0x7B5A, 0x7DA5, +}; + +static const int16_t ts_cb_2[16] = { + 0x96F8, 0xA3B4, 0xAF45, 0xBA53, 0xC4B1, 0xCECC, 0xD86F, 0xE21E, + 0xEBF3, 0xF640, 0x00F7, 0x0C20, 0x1881, 0x269A, 0x376B, 0x4D60, +}; + +static const int16_t ts_cb_3[16] = { + 0xC654, 0xDEF2, 0xEFAA, 0xFD94, 0x096A, 0x143F, 0x1E7B, 0x282C, + 0x3176, 0x3A89, 0x439F, 0x4CA2, 0x557F, 0x5E50, 0x6718, 0x6F8D, +}; + +static const int16_t ts_cb_4[16] = { + 0xABE7, 0xBBA8, 0xC81C, 0xD326, 0xDD0E, 0xE5D4, 0xEE22, 0xF618, + 0xFE28, 0x064F, 0x0EB7, 0x17B8, 0x21AA, 0x2D8B, 0x3BA2, 0x4DF9, +}; + +static const int16_t ts_cb_5[8] = { + 0xD51B, 0xF12E, 0x042E, 0x13C7, 0x2260, 0x311B, 0x40DE, 0x5385, +}; + +static const int16_t ts_cb_6[8] = { + 0xB550, 0xC825, 0xD980, 0xE997, 0xF883, 0x0752, 0x1811, 0x2E18, +}; + +static const int16_t ts_cb_7[8] = { + 0xCEF0, 0xE4F9, 0xF6BB, 0x0646, 0x14F5, 0x23FF, 0x356F, 0x4A8D, +}; + +static const int16_t *ts_codebook[8] = { + ts_cb_0, ts_cb_1, ts_cb_2, ts_cb_3, ts_cb_4, ts_cb_5, ts_cb_6, ts_cb_7 +}; + +/* table used for decoding pulse positions */ +static const int16_t ts_140[120] = { + 0x0E46, 0x0CCC, 0x0B6D, 0x0A28, 0x08FC, 0x07E8, 0x06EB, 0x0604, + 0x0532, 0x0474, 0x03C9, 0x0330, 0x02A8, 0x0230, 0x01C7, 0x016C, + 0x011E, 0x00DC, 0x00A5, 0x0078, 0x0054, 0x0038, 0x0023, 0x0014, + 0x000A, 0x0004, 0x0001, 0x0000, 0x0000, 0x0000, + + 0x0196, 0x017A, 0x015F, 0x0145, 0x012C, 0x0114, 0x00FD, 0x00E7, + 0x00D2, 0x00BE, 0x00AB, 0x0099, 0x0088, 0x0078, 0x0069, 0x005B, + 0x004E, 0x0042, 0x0037, 0x002D, 0x0024, 0x001C, 0x0015, 0x000F, + 0x000A, 0x0006, 0x0003, 0x0001, 0x0000, 0x0000, + + 0x001D, 0x001C, 0x001B, 0x001A, 0x0019, 0x0018, 0x0017, 0x0016, + 0x0015, 0x0014, 0x0013, 0x0012, 0x0011, 0x0010, 0x000F, 0x000E, + 0x000D, 0x000C, 0x000B, 0x000A, 0x0009, 0x0008, 0x0007, 0x0006, + 0x0005, 0x0004, 0x0003, 0x0002, 0x0001, 0x0000, + + 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, + 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, + 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, + 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001 +}; + +/* filter for correlated input filter */ +static const int16_t ts_230[8] = + { 0x7F3B, 0x7E78, 0x7DB6, 0x7CF5, 0x7C35, 0x7B76, 0x7AB8, 0x79FC }; + +/* two-point filters table */ +static const int16_t ts_240[25 * 2] = { + 0xED2F, 0x5239, + 0x54F1, 0xE4A9, + 0x2620, 0xEE3E, + 0x09D6, 0x2C40, + 0xEFB5, 0x2BE0, + + 0x3FE1, 0x3339, + 0x442F, 0xE6FE, + 0x4458, 0xF9DF, + 0xF231, 0x43DB, + 0x3DB0, 0xF705, + + 0x4F7B, 0xFEFB, + 0x26AD, 0x0CDC, + 0x33C2, 0x0739, + 0x12BE, 0x43A2, + 0x1BDF, 0x1F3E, + + 0x0211, 0x0796, + 0x2AEB, 0x163F, + 0x050D, 0x3A38, + 0x0D1E, 0x0D78, + 0x150F, 0x3346, + + 0x38A4, 0x0B7D, + 0x2D5D, 0x1FDF, + 0x19B7, 0x2822, + 0x0D99, 0x1F12, + 0x194C, 0x0CE6 +}; + +/* possible pulse values */ +static const int16_t ts_562[64] = { + 0x0002, 0x0006, 0xFFFE, 0xFFFA, + 0x0004, 0x000C, 0xFFFC, 0xFFF4, + 0x0006, 0x0012, 0xFFFA, 0xFFEE, + 0x000A, 0x001E, 0xFFF6, 0xFFE2, + 0x0010, 0x0030, 0xFFF0, 0xFFD0, + 0x0019, 0x004B, 0xFFE7, 0xFFB5, + 0x0028, 0x0078, 0xFFD8, 0xFF88, + 0x0040, 0x00C0, 0xFFC0, 0xFF40, + 0x0065, 0x012F, 0xFF9B, 0xFED1, + 0x00A1, 0x01E3, 0xFF5F, 0xFE1D, + 0x0100, 0x0300, 0xFF00, 0xFD00, + 0x0196, 0x04C2, 0xFE6A, 0xFB3E, + 0x0285, 0x078F, 0xFD7B, 0xF871, + 0x0400, 0x0C00, 0xFC00, 0xF400, + 0x0659, 0x130B, 0xF9A7, 0xECF5, + 0x0A14, 0x1E3C, 0xF5EC, 0xE1C4 +}; + +/* filters used in final output calculations */ +static const int16_t ts_5E2[8] = + { 0x4666, 0x26B8, 0x154C, 0x0BB6, 0x0671, 0x038B, 0x01F3, 0x0112 }; +static const int16_t ts_5F2[8] = + { 0x6000, 0x4800, 0x3600, 0x2880, 0x1E60, 0x16C8, 0x1116, 0x0CD1 }; + +#endif diff --git a/src/libffmpeg/libavcodec/tta.c b/src/libffmpeg/libavcodec/tta.c new file mode 100644 index 000000000..979a94a74 --- /dev/null +++ b/src/libffmpeg/libavcodec/tta.c @@ -0,0 +1,455 @@ +/* + * TTA (The Lossless True Audio) decoder + * Copyright (c) 2006 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file tta.c + * TTA (The Lossless True Audio) decoder + * (www.true-audio.com or tta.corecodec.org) + * @author Alex Beregszaszi + * + */ + +#define ALT_BITSTREAM_READER_LE +//#define DEBUG +#include <limits.h> +#include "avcodec.h" +#include "bitstream.h" + +#define FORMAT_INT 1 +#define FORMAT_FLOAT 3 + +typedef struct TTAContext { + AVCodecContext *avctx; + GetBitContext gb; + + int flags, channels, bps, is_float, data_length; + int frame_length, last_frame_length, total_frames; + + int32_t *decode_buffer; +} TTAContext; + +#if 0 +static inline int shift_1(int i) +{ + if (i < 32) + return 1 << i; + else + return 0x80000000; // 16 << 31 +} + +static inline int shift_16(int i) +{ + if (i < 28) + return 16 << i; + else + return 0x80000000; // 16 << 27 +} +#else +static const uint32_t shift_1[] = { + 0x00000001, 0x00000002, 0x00000004, 0x00000008, + 0x00000010, 0x00000020, 0x00000040, 0x00000080, + 0x00000100, 0x00000200, 0x00000400, 0x00000800, + 0x00001000, 0x00002000, 0x00004000, 0x00008000, + 0x00010000, 0x00020000, 0x00040000, 0x00080000, + 0x00100000, 0x00200000, 0x00400000, 0x00800000, + 0x01000000, 0x02000000, 0x04000000, 0x08000000, + 0x10000000, 0x20000000, 0x40000000, 0x80000000, + 0x80000000, 0x80000000, 0x80000000, 0x80000000, + 0x80000000, 0x80000000, 0x80000000, 0x80000000 +}; + +static const uint32_t *shift_16 = shift_1 + 4; +#endif + +#define MAX_ORDER 16 +typedef struct TTAFilter { + int32_t shift, round, error, mode; + int32_t qm[MAX_ORDER]; + int32_t dx[MAX_ORDER]; + int32_t dl[MAX_ORDER]; +} TTAFilter; + +static int32_t ttafilter_configs[4][2] = { + {10, 1}, + {9, 1}, + {10, 1}, + {12, 0} +}; + +static void ttafilter_init(TTAFilter *c, int32_t shift, int32_t mode) { + memset(c, 0, sizeof(TTAFilter)); + c->shift = shift; + c->round = shift_1[shift-1]; +// c->round = 1 << (shift - 1); + c->mode = mode; +} + +// FIXME: copy paste from original +static inline void memshl(register int32_t *a, register int32_t *b) { + *a++ = *b++; + *a++ = *b++; + *a++ = *b++; + *a++ = *b++; + *a++ = *b++; + *a++ = *b++; + *a++ = *b++; + *a = *b; +} + +// FIXME: copy paste from original +// mode=1 encoder, mode=0 decoder +static inline void ttafilter_process(TTAFilter *c, int32_t *in, int32_t mode) { + register int32_t *dl = c->dl, *qm = c->qm, *dx = c->dx, sum = c->round; + + if (!c->error) { + sum += *dl++ * *qm, qm++; + sum += *dl++ * *qm, qm++; + sum += *dl++ * *qm, qm++; + sum += *dl++ * *qm, qm++; + sum += *dl++ * *qm, qm++; + sum += *dl++ * *qm, qm++; + sum += *dl++ * *qm, qm++; + sum += *dl++ * *qm, qm++; + dx += 8; + } else if(c->error < 0) { + sum += *dl++ * (*qm -= *dx++), qm++; + sum += *dl++ * (*qm -= *dx++), qm++; + sum += *dl++ * (*qm -= *dx++), qm++; + sum += *dl++ * (*qm -= *dx++), qm++; + sum += *dl++ * (*qm -= *dx++), qm++; + sum += *dl++ * (*qm -= *dx++), qm++; + sum += *dl++ * (*qm -= *dx++), qm++; + sum += *dl++ * (*qm -= *dx++), qm++; + } else { + sum += *dl++ * (*qm += *dx++), qm++; + sum += *dl++ * (*qm += *dx++), qm++; + sum += *dl++ * (*qm += *dx++), qm++; + sum += *dl++ * (*qm += *dx++), qm++; + sum += *dl++ * (*qm += *dx++), qm++; + sum += *dl++ * (*qm += *dx++), qm++; + sum += *dl++ * (*qm += *dx++), qm++; + sum += *dl++ * (*qm += *dx++), qm++; + } + + *(dx-0) = ((*(dl-1) >> 30) | 1) << 2; + *(dx-1) = ((*(dl-2) >> 30) | 1) << 1; + *(dx-2) = ((*(dl-3) >> 30) | 1) << 1; + *(dx-3) = ((*(dl-4) >> 30) | 1); + + // compress + if (mode) { + *dl = *in; + *in -= (sum >> c->shift); + c->error = *in; + } else { + c->error = *in; + *in += (sum >> c->shift); + *dl = *in; + } + + if (c->mode) { + *(dl-1) = *dl - *(dl-1); + *(dl-2) = *(dl-1) - *(dl-2); + *(dl-3) = *(dl-2) - *(dl-3); + } + + memshl(c->dl, c->dl + 1); + memshl(c->dx, c->dx + 1); +} + +typedef struct TTARice { + uint32_t k0, k1, sum0, sum1; +} TTARice; + +static void rice_init(TTARice *c, uint32_t k0, uint32_t k1) +{ + c->k0 = k0; + c->k1 = k1; + c->sum0 = shift_16[k0]; + c->sum1 = shift_16[k1]; +} + +static int tta_get_unary(GetBitContext *gb) +{ + int ret = 0; + + // count ones + while(get_bits1(gb)) + ret++; + return ret; +} + +// shamelessly copied from shorten.c +static int inline get_le16(GetBitContext *gb) +{ + return bswap_16(get_bits_long(gb, 16)); +} + +static int inline get_le32(GetBitContext *gb) +{ + return bswap_32(get_bits_long(gb, 32)); +} + +static int tta_decode_init(AVCodecContext * avctx) +{ + TTAContext *s = avctx->priv_data; + int i; + + s->avctx = avctx; + + // 30bytes includes a seektable with one frame + if (avctx->extradata_size < 30) + return -1; + + init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size); + if (show_bits_long(&s->gb, 32) == bswap_32(ff_get_fourcc("TTA1"))) + { + /* signature */ + skip_bits(&s->gb, 32); +// if (get_bits_long(&s->gb, 32) != bswap_32(ff_get_fourcc("TTA1"))) { +// av_log(s->avctx, AV_LOG_ERROR, "Missing magic\n"); +// return -1; +// } + + s->flags = get_le16(&s->gb); + if (s->flags != 1 && s->flags != 3) + { + av_log(s->avctx, AV_LOG_ERROR, "Invalid flags\n"); + return -1; + } + s->is_float = (s->flags == FORMAT_FLOAT); + avctx->channels = s->channels = get_le16(&s->gb); + avctx->bits_per_sample = get_le16(&s->gb); + s->bps = (avctx->bits_per_sample + 7) / 8; + avctx->sample_rate = get_le32(&s->gb); + if(avctx->sample_rate > 1000000){ //prevent FRAME_TIME * avctx->sample_rate from overflowing and sanity check + av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n"); + return -1; + } + s->data_length = get_le32(&s->gb); + skip_bits(&s->gb, 32); // CRC32 of header + + if (s->is_float) + { + avctx->sample_fmt = SAMPLE_FMT_FLT; + av_log(s->avctx, AV_LOG_ERROR, "Unsupported sample format. Please contact the developers.\n"); + return -1; + } + else switch(s->bps) { +// case 1: avctx->sample_fmt = SAMPLE_FMT_U8; break; + case 2: avctx->sample_fmt = SAMPLE_FMT_S16; break; +// case 3: avctx->sample_fmt = SAMPLE_FMT_S24; break; + case 4: avctx->sample_fmt = SAMPLE_FMT_S32; break; + default: + av_log(s->avctx, AV_LOG_ERROR, "Invalid/unsupported sample format. Please contact the developers.\n"); + return -1; + } + + // FIXME: horribly broken, but directly from reference source +#define FRAME_TIME 1.04489795918367346939 + s->frame_length = (int)(FRAME_TIME * avctx->sample_rate); + + s->last_frame_length = s->data_length % s->frame_length; + s->total_frames = s->data_length / s->frame_length + + (s->last_frame_length ? 1 : 0); + + av_log(s->avctx, AV_LOG_DEBUG, "flags: %x chans: %d bps: %d rate: %d block: %d\n", + s->flags, avctx->channels, avctx->bits_per_sample, avctx->sample_rate, + avctx->block_align); + av_log(s->avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n", + s->data_length, s->frame_length, s->last_frame_length, s->total_frames); + + // FIXME: seek table + for (i = 0; i < s->total_frames; i++) + skip_bits(&s->gb, 32); + skip_bits(&s->gb, 32); // CRC32 of seektable + + if(s->frame_length >= UINT_MAX / (s->channels * sizeof(int32_t))){ + av_log(avctx, AV_LOG_ERROR, "frame_length too large\n"); + return -1; + } + + s->decode_buffer = av_mallocz(sizeof(int32_t)*s->frame_length*s->channels); + } else { + av_log(avctx, AV_LOG_ERROR, "Wrong extradata present\n"); + return -1; + } + + return 0; +} + +static int tta_decode_frame(AVCodecContext *avctx, + void *data, int *data_size, + uint8_t *buf, int buf_size) +{ + TTAContext *s = avctx->priv_data; + int i; + + init_get_bits(&s->gb, buf, buf_size*8); + { + int32_t predictors[s->channels]; + TTAFilter filters[s->channels]; + TTARice rices[s->channels]; + int cur_chan = 0, framelen = s->frame_length; + int32_t *p; + + // FIXME: seeking + s->total_frames--; + if (!s->total_frames && s->last_frame_length) + framelen = s->last_frame_length; + + // init per channel states + for (i = 0; i < s->channels; i++) { + predictors[i] = 0; + ttafilter_init(&(filters[i]), ttafilter_configs[s->bps-1][0], ttafilter_configs[s->bps-1][1]); + rice_init(&(rices[i]), 10, 10); + } + + for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) { + int32_t *predictor = &(predictors[cur_chan]); + TTAFilter *filter = &(filters[cur_chan]); + TTARice *rice = &(rices[cur_chan]); + uint32_t unary, depth, k; + int32_t value; + + unary = tta_get_unary(&s->gb); + + if (unary == 0) { + depth = 0; + k = rice->k0; + } else { + depth = 1; + k = rice->k1; + unary--; + } + + if (k) + value = (unary << k) + get_bits(&s->gb, k); + else + value = unary; + + // FIXME: copy paste from original + switch (depth) { + case 1: + rice->sum1 += value - (rice->sum1 >> 4); + if (rice->k1 > 0 && rice->sum1 < shift_16[rice->k1]) + rice->k1--; + else if(rice->sum1 > shift_16[rice->k1 + 1]) + rice->k1++; + value += shift_1[rice->k0]; + default: + rice->sum0 += value - (rice->sum0 >> 4); + if (rice->k0 > 0 && rice->sum0 < shift_16[rice->k0]) + rice->k0--; + else if(rice->sum0 > shift_16[rice->k0 + 1]) + rice->k0++; + } + + // extract sign +#define SIGN(x) (((x)&1) ? (++(x)>>1) : (-(x)>>1)) + *p = SIGN(value); + + // run hybrid filter + ttafilter_process(filter, p, 0); + + // fixed order prediction +#define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k) + switch (s->bps) { + case 1: *p += PRED(*predictor, 4); break; + case 2: + case 3: *p += PRED(*predictor, 5); break; + case 4: *p += *predictor; break; + } + *predictor = *p; + +#if 0 + // extract 32bit float from last two int samples + if (s->is_float && ((p - data) & 1)) { + uint32_t neg = *p & 0x80000000; + uint32_t hi = *(p - 1); + uint32_t lo = abs(*p) - 1; + + hi += (hi || lo) ? 0x3f80 : 0; + // SWAP16: swap all the 16 bits + *(p - 1) = (hi << 16) | SWAP16(lo) | neg; + } +#endif + + /*if ((get_bits_count(&s->gb)+7)/8 > buf_size) + { + av_log(NULL, AV_LOG_INFO, "overread!!\n"); + break; + }*/ + + // flip channels + if (cur_chan < (s->channels-1)) + cur_chan++; + else { + // decorrelate in case of stereo integer + if (!s->is_float && (s->channels > 1)) { + int32_t *r = p - 1; + for (*p += *r / 2; r > p - s->channels; r--) + *r = *(r + 1) - *r; + } + cur_chan = 0; + } + } + + skip_bits(&s->gb, 32); // frame crc + + // convert to output buffer + switch(s->bps) { + case 2: { + uint16_t *samples = data; + for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) { +// *samples++ = (unsigned char)*p; +// *samples++ = (unsigned char)(*p >> 8); + *samples++ = *p; + } + *data_size = (uint8_t *)samples - (uint8_t *)data; + break; + } + default: + av_log(s->avctx, AV_LOG_ERROR, "Error, only 16bit samples supported!\n"); + } + } + +// return get_bits_count(&s->gb)+7)/8; + return buf_size; +} + +static int tta_decode_close(AVCodecContext *avctx) { + TTAContext *s = avctx->priv_data; + + if (s->decode_buffer) + av_free(s->decode_buffer); + + return 0; +} + +AVCodec tta_decoder = { + "tta", + CODEC_TYPE_AUDIO, + CODEC_ID_TTA, + sizeof(TTAContext), + tta_decode_init, + NULL, + tta_decode_close, + tta_decode_frame, +}; diff --git a/src/libffmpeg/libavcodec/vc1.c b/src/libffmpeg/libavcodec/vc1.c new file mode 100644 index 000000000..731baa4dc --- /dev/null +++ b/src/libffmpeg/libavcodec/vc1.c @@ -0,0 +1,3228 @@ +/* + * VC-1 and WMV3 decoder + * Copyright (c) 2006 Konstantin Shishkov + * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +/** + * @file vc1.c + * VC-1 and WMV3 decoder + * + */ +#include "common.h" +#include "dsputil.h" +#include "avcodec.h" +#include "mpegvideo.h" +#include "vc1data.h" +#include "vc1acdata.h" + +#undef NDEBUG +#include <assert.h> + +extern const uint32_t ff_table0_dc_lum[120][2], ff_table1_dc_lum[120][2]; +extern const uint32_t ff_table0_dc_chroma[120][2], ff_table1_dc_chroma[120][2]; +extern VLC ff_msmp4_dc_luma_vlc[2], ff_msmp4_dc_chroma_vlc[2]; +#define MB_INTRA_VLC_BITS 9 +extern VLC ff_msmp4_mb_i_vlc; +extern const uint16_t ff_msmp4_mb_i_table[64][2]; +#define DC_VLC_BITS 9 +#define AC_VLC_BITS 9 +static const uint16_t table_mb_intra[64][2]; + + +/** Available Profiles */ +//@{ +enum Profile { + PROFILE_SIMPLE, + PROFILE_MAIN, + PROFILE_COMPLEX, ///< TODO: WMV9 specific + PROFILE_ADVANCED +}; +//@} + +/** Sequence quantizer mode */ +//@{ +enum QuantMode { + QUANT_FRAME_IMPLICIT, ///< Implicitly specified at frame level + QUANT_FRAME_EXPLICIT, ///< Explicitly specified at frame level + QUANT_NON_UNIFORM, ///< Non-uniform quant used for all frames + QUANT_UNIFORM ///< Uniform quant used for all frames +}; +//@} + +/** Where quant can be changed */ +//@{ +enum DQProfile { + DQPROFILE_FOUR_EDGES, + DQPROFILE_DOUBLE_EDGES, + DQPROFILE_SINGLE_EDGE, + DQPROFILE_ALL_MBS +}; +//@} + +/** @name Where quant can be changed + */ +//@{ +enum DQSingleEdge { + DQSINGLE_BEDGE_LEFT, + DQSINGLE_BEDGE_TOP, + DQSINGLE_BEDGE_RIGHT, + DQSINGLE_BEDGE_BOTTOM +}; +//@} + +/** Which pair of edges is quantized with ALTPQUANT */ +//@{ +enum DQDoubleEdge { + DQDOUBLE_BEDGE_TOPLEFT, + DQDOUBLE_BEDGE_TOPRIGHT, + DQDOUBLE_BEDGE_BOTTOMRIGHT, + DQDOUBLE_BEDGE_BOTTOMLEFT +}; +//@} + +/** MV modes for P frames */ +//@{ +enum MVModes { + MV_PMODE_1MV_HPEL_BILIN, + MV_PMODE_1MV, + MV_PMODE_1MV_HPEL, + MV_PMODE_MIXED_MV, + MV_PMODE_INTENSITY_COMP +}; +//@} + +/** @name MV types for B frames */ +//@{ +enum BMVTypes { + BMV_TYPE_BACKWARD, + BMV_TYPE_FORWARD, + BMV_TYPE_INTERPOLATED = 3 //XXX: ?? +}; +//@} + +/** @name Block types for P/B frames */ +//@{ +enum TransformTypes { + TT_8X8, + TT_8X4_BOTTOM, + TT_8X4_TOP, + TT_8X4, //Both halves + TT_4X8_RIGHT, + TT_4X8_LEFT, + TT_4X8, //Both halves + TT_4X4 +}; +//@} + +/** Table for conversion between TTBLK and TTMB */ +static const int ttblk_to_tt[3][8] = { + { TT_8X4, TT_4X8, TT_8X8, TT_4X4, TT_8X4_TOP, TT_8X4_BOTTOM, TT_4X8_RIGHT, TT_4X8_LEFT }, + { TT_8X8, TT_4X8_RIGHT, TT_4X8_LEFT, TT_4X4, TT_8X4, TT_4X8, TT_8X4_BOTTOM, TT_8X4_TOP }, + { TT_8X8, TT_4X8, TT_4X4, TT_8X4_BOTTOM, TT_4X8_RIGHT, TT_4X8_LEFT, TT_8X4, TT_8X4_TOP } +}; + +static const int ttfrm_to_tt[4] = { TT_8X8, TT_8X4, TT_4X8, TT_4X4 }; + +/** MV P mode - the 5th element is only used for mode 1 */ +static const uint8_t mv_pmode_table[2][5] = { + { MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_1MV, MV_PMODE_1MV_HPEL, MV_PMODE_INTENSITY_COMP, MV_PMODE_MIXED_MV }, + { MV_PMODE_1MV, MV_PMODE_MIXED_MV, MV_PMODE_1MV_HPEL, MV_PMODE_INTENSITY_COMP, MV_PMODE_1MV_HPEL_BILIN } +}; +static const uint8_t mv_pmode_table2[2][4] = { + { MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_1MV, MV_PMODE_1MV_HPEL, MV_PMODE_MIXED_MV }, + { MV_PMODE_1MV, MV_PMODE_MIXED_MV, MV_PMODE_1MV_HPEL, MV_PMODE_1MV_HPEL_BILIN } +}; + +/** One more frame type */ +#define BI_TYPE 7 + +static const int fps_nr[5] = { 24, 25, 30, 50, 60 }, + fps_dr[2] = { 1000, 1001 }; +static const uint8_t pquant_table[3][32] = { + { /* Implicit quantizer */ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 6, 7, 8, 9, 10, 11, 12, + 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 29, 31 + }, + { /* Explicit quantizer, pquantizer uniform */ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 + }, + { /* Explicit quantizer, pquantizer non-uniform */ + 0, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, + 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 31 + } +}; + +/** @name VC-1 VLC tables and defines + * @todo TODO move this into the context + */ +//@{ +#define VC1_BFRACTION_VLC_BITS 7 +static VLC vc1_bfraction_vlc; +#define VC1_IMODE_VLC_BITS 4 +static VLC vc1_imode_vlc; +#define VC1_NORM2_VLC_BITS 3 +static VLC vc1_norm2_vlc; +#define VC1_NORM6_VLC_BITS 9 +static VLC vc1_norm6_vlc; +/* Could be optimized, one table only needs 8 bits */ +#define VC1_TTMB_VLC_BITS 9 //12 +static VLC vc1_ttmb_vlc[3]; +#define VC1_MV_DIFF_VLC_BITS 9 //15 +static VLC vc1_mv_diff_vlc[4]; +#define VC1_CBPCY_P_VLC_BITS 9 //14 +static VLC vc1_cbpcy_p_vlc[4]; +#define VC1_4MV_BLOCK_PATTERN_VLC_BITS 6 +static VLC vc1_4mv_block_pattern_vlc[4]; +#define VC1_TTBLK_VLC_BITS 5 +static VLC vc1_ttblk_vlc[3]; +#define VC1_SUBBLKPAT_VLC_BITS 6 +static VLC vc1_subblkpat_vlc[3]; + +static VLC vc1_ac_coeff_table[8]; +//@} + +enum CodingSet { + CS_HIGH_MOT_INTRA = 0, + CS_HIGH_MOT_INTER, + CS_LOW_MOT_INTRA, + CS_LOW_MOT_INTER, + CS_MID_RATE_INTRA, + CS_MID_RATE_INTER, + CS_HIGH_RATE_INTRA, + CS_HIGH_RATE_INTER +}; + +/** The VC1 Context + * @fixme Change size wherever another size is more efficient + * Many members are only used for Advanced Profile + */ +typedef struct VC1Context{ + MpegEncContext s; + + int bits; + + /** Simple/Main Profile sequence header */ + //@{ + int res_sm; ///< reserved, 2b + int res_x8; ///< reserved + int multires; ///< frame-level RESPIC syntax element present + int res_fasttx; ///< reserved, always 1 + int res_transtab; ///< reserved, always 0 + int rangered; ///< RANGEREDFRM (range reduction) syntax element present + ///< at frame level + int res_rtm_flag; ///< reserved, set to 1 + int reserved; ///< reserved + //@} + + /** Advanced Profile */ + //@{ + int level; ///< 3bits, for Advanced/Simple Profile, provided by TS layer + int chromaformat; ///< 2bits, 2=4:2:0, only defined + int postprocflag; ///< Per-frame processing suggestion flag present + int broadcast; ///< TFF/RFF present + int interlace; ///< Progressive/interlaced (RPTFTM syntax element) + int tfcntrflag; ///< TFCNTR present + int panscanflag; ///< NUMPANSCANWIN, TOPLEFT{X,Y}, BOTRIGHT{X,Y} present + int extended_dmv; ///< Additional extended dmv range at P/B frame-level + int color_prim; ///< 8bits, chroma coordinates of the color primaries + int transfer_char; ///< 8bits, Opto-electronic transfer characteristics + int matrix_coef; ///< 8bits, Color primaries->YCbCr transform matrix + int hrd_param_flag; ///< Presence of Hypothetical Reference + ///< Decoder parameters + //@} + + /** Sequence header data for all Profiles + * TODO: choose between ints, uint8_ts and monobit flags + */ + //@{ + int profile; ///< 2bits, Profile + int frmrtq_postproc; ///< 3bits, + int bitrtq_postproc; ///< 5bits, quantized framerate-based postprocessing strength + int fastuvmc; ///< Rounding of qpel vector to hpel ? (not in Simple) + int extended_mv; ///< Ext MV in P/B (not in Simple) + int dquant; ///< How qscale varies with MBs, 2bits (not in Simple) + int vstransform; ///< variable-size [48]x[48] transform type + info + int overlap; ///< overlapped transforms in use + int quantizer_mode; ///< 2bits, quantizer mode used for sequence, see QUANT_* + int finterpflag; ///< INTERPFRM present + //@} + + /** Frame decoding info for all profiles */ + //@{ + uint8_t mv_mode; ///< MV coding monde + uint8_t mv_mode2; ///< Secondary MV coding mode (B frames) + int k_x; ///< Number of bits for MVs (depends on MV range) + int k_y; ///< Number of bits for MVs (depends on MV range) + int range_x, range_y; ///< MV range + uint8_t pq, altpq; ///< Current/alternate frame quantizer scale + /** pquant parameters */ + //@{ + uint8_t dquantfrm; + uint8_t dqprofile; + uint8_t dqsbedge; + uint8_t dqbilevel; + //@} + /** AC coding set indexes + * @see 8.1.1.10, p(1)10 + */ + //@{ + int c_ac_table_index; ///< Chroma index from ACFRM element + int y_ac_table_index; ///< Luma index from AC2FRM element + //@} + int ttfrm; ///< Transform type info present at frame level + uint8_t ttmbf; ///< Transform type flag + uint8_t ttblk4x4; ///< Value of ttblk which indicates a 4x4 transform + int codingset; ///< index of current table set from 11.8 to use for luma block decoding + int codingset2; ///< index of current table set from 11.8 to use for chroma block decoding + int pqindex; ///< raw pqindex used in coding set selection + int a_avail, c_avail; + uint8_t *mb_type_base, *mb_type[3]; + + + /** Luma compensation parameters */ + //@{ + uint8_t lumscale; + uint8_t lumshift; + //@} + int16_t bfraction; ///< Relative position % anchors=> how to scale MVs + uint8_t halfpq; ///< Uniform quant over image and qp+.5 + uint8_t respic; ///< Frame-level flag for resized images + int buffer_fullness; ///< HRD info + /** Ranges: + * -# 0 -> [-64n 63.f] x [-32, 31.f] + * -# 1 -> [-128, 127.f] x [-64, 63.f] + * -# 2 -> [-512, 511.f] x [-128, 127.f] + * -# 3 -> [-1024, 1023.f] x [-256, 255.f] + */ + uint8_t mvrange; + uint8_t pquantizer; ///< Uniform (over sequence) quantizer in use + VLC *cbpcy_vlc; ///< CBPCY VLC table + int tt_index; ///< Index for Transform Type tables + uint8_t* mv_type_mb_plane; ///< bitplane for mv_type == (4MV) + uint8_t* direct_mb_plane; ///< bitplane for "direct" MBs + int mv_type_is_raw; ///< mv type mb plane is not coded + int dmb_is_raw; ///< direct mb plane is raw + int skip_is_raw; ///< skip mb plane is not coded + uint8_t luty[256], lutuv[256]; // lookup tables used for intensity compensation + int rnd; ///< rounding control + + /** Frame decoding info for S/M profiles only */ + //@{ + uint8_t rangeredfrm; ///< out_sample = CLIP((in_sample-128)*2+128) + uint8_t interpfrm; + //@} + + /** Frame decoding info for Advanced profile */ + //@{ + uint8_t fcm; ///< 0->Progressive, 2->Frame-Interlace, 3->Field-Interlace + uint8_t numpanscanwin; + uint8_t tfcntr; + uint8_t rptfrm, tff, rff; + uint16_t topleftx; + uint16_t toplefty; + uint16_t bottomrightx; + uint16_t bottomrighty; + uint8_t uvsamp; + uint8_t postproc; + int hrd_num_leaky_buckets; + uint8_t bit_rate_exponent; + uint8_t buffer_size_exponent; +// BitPlane ac_pred_plane; ///< AC prediction flags bitplane +// BitPlane over_flags_plane; ///< Overflags bitplane + uint8_t condover; + uint16_t *hrd_rate, *hrd_buffer; + uint8_t *hrd_fullness; + uint8_t range_mapy_flag; + uint8_t range_mapuv_flag; + uint8_t range_mapy; + uint8_t range_mapuv; + //@} +} VC1Context; + +/** + * Get unary code of limited length + * @fixme FIXME Slow and ugly + * @param gb GetBitContext + * @param[in] stop The bitstop value (unary code of 1's or 0's) + * @param[in] len Maximum length + * @return Unary length/index + */ +static int get_prefix(GetBitContext *gb, int stop, int len) +{ +#if 1 + int i; + + for(i = 0; i < len && get_bits1(gb) != stop; i++); + return i; +/* int i = 0, tmp = !stop; + + while (i != len && tmp != stop) + { + tmp = get_bits(gb, 1); + i++; + } + if (i == len && tmp != stop) return len+1; + return i;*/ +#else + unsigned int buf; + int log; + + OPEN_READER(re, gb); + UPDATE_CACHE(re, gb); + buf=GET_CACHE(re, gb); //Still not sure + if (stop) buf = ~buf; + + log= av_log2(-buf); //FIXME: -? + if (log < limit){ + LAST_SKIP_BITS(re, gb, log+1); + CLOSE_READER(re, gb); + return log; + } + + LAST_SKIP_BITS(re, gb, limit); + CLOSE_READER(re, gb); + return limit; +#endif +} + +static inline int decode210(GetBitContext *gb){ + int n; + n = get_bits1(gb); + if (n == 1) + return 0; + else + return 2 - get_bits1(gb); +} + +/** + * Init VC-1 specific tables and VC1Context members + * @param v The VC1Context to initialize + * @return Status + */ +static int vc1_init_common(VC1Context *v) +{ + static int done = 0; + int i = 0; + + v->hrd_rate = v->hrd_buffer = NULL; + + /* VLC tables */ + if(!done) + { + done = 1; + init_vlc(&vc1_bfraction_vlc, VC1_BFRACTION_VLC_BITS, 23, + vc1_bfraction_bits, 1, 1, + vc1_bfraction_codes, 1, 1, 1); + init_vlc(&vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4, + vc1_norm2_bits, 1, 1, + vc1_norm2_codes, 1, 1, 1); + init_vlc(&vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 64, + vc1_norm6_bits, 1, 1, + vc1_norm6_codes, 2, 2, 1); + init_vlc(&vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7, + vc1_imode_bits, 1, 1, + vc1_imode_codes, 1, 1, 1); + for (i=0; i<3; i++) + { + init_vlc(&vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 16, + vc1_ttmb_bits[i], 1, 1, + vc1_ttmb_codes[i], 2, 2, 1); + init_vlc(&vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 8, + vc1_ttblk_bits[i], 1, 1, + vc1_ttblk_codes[i], 1, 1, 1); + init_vlc(&vc1_subblkpat_vlc[i], VC1_SUBBLKPAT_VLC_BITS, 15, + vc1_subblkpat_bits[i], 1, 1, + vc1_subblkpat_codes[i], 1, 1, 1); + } + for(i=0; i<4; i++) + { + init_vlc(&vc1_4mv_block_pattern_vlc[i], VC1_4MV_BLOCK_PATTERN_VLC_BITS, 16, + vc1_4mv_block_pattern_bits[i], 1, 1, + vc1_4mv_block_pattern_codes[i], 1, 1, 1); + init_vlc(&vc1_cbpcy_p_vlc[i], VC1_CBPCY_P_VLC_BITS, 64, + vc1_cbpcy_p_bits[i], 1, 1, + vc1_cbpcy_p_codes[i], 2, 2, 1); + init_vlc(&vc1_mv_diff_vlc[i], VC1_MV_DIFF_VLC_BITS, 73, + vc1_mv_diff_bits[i], 1, 1, + vc1_mv_diff_codes[i], 2, 2, 1); + } + for(i=0; i<8; i++) + init_vlc(&vc1_ac_coeff_table[i], AC_VLC_BITS, vc1_ac_sizes[i], + &vc1_ac_tables[i][0][1], 8, 4, + &vc1_ac_tables[i][0][0], 8, 4, 1); + init_vlc(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64, + &ff_msmp4_mb_i_table[0][1], 4, 2, + &ff_msmp4_mb_i_table[0][0], 4, 2, 1); + } + + /* Other defaults */ + v->pq = -1; + v->mvrange = 0; /* 7.1.1.18, p80 */ + + return 0; +} + +/***********************************************************************/ +/** + * @defgroup bitplane VC9 Bitplane decoding + * @see 8.7, p56 + * @{ + */ + +/** @addtogroup bitplane + * Imode types + * @{ + */ +enum Imode { + IMODE_RAW, + IMODE_NORM2, + IMODE_DIFF2, + IMODE_NORM6, + IMODE_DIFF6, + IMODE_ROWSKIP, + IMODE_COLSKIP +}; +/** @} */ //imode defines + +/** Decode rows by checking if they are skipped + * @param plane Buffer to store decoded bits + * @param[in] width Width of this buffer + * @param[in] height Height of this buffer + * @param[in] stride of this buffer + */ +static void decode_rowskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){ + int x, y; + + for (y=0; y<height; y++){ + if (!get_bits(gb, 1)) //rowskip + memset(plane, 0, width); + else + for (x=0; x<width; x++) + plane[x] = get_bits(gb, 1); + plane += stride; + } +} + +/** Decode columns by checking if they are skipped + * @param plane Buffer to store decoded bits + * @param[in] width Width of this buffer + * @param[in] height Height of this buffer + * @param[in] stride of this buffer + * @fixme FIXME: Optimize + */ +static void decode_colskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){ + int x, y; + + for (x=0; x<width; x++){ + if (!get_bits(gb, 1)) //colskip + for (y=0; y<height; y++) + plane[y*stride] = 0; + else + for (y=0; y<height; y++) + plane[y*stride] = get_bits(gb, 1); + plane ++; + } +} + +/** Decode a bitplane's bits + * @param bp Bitplane where to store the decode bits + * @param v VC-1 context for bit reading and logging + * @return Status + * @fixme FIXME: Optimize + * @todo TODO: Decide if a struct is needed + */ +static int bitplane_decoding(uint8_t* data, int *raw_flag, VC1Context *v) +{ + GetBitContext *gb = &v->s.gb; + + int imode, x, y, code, offset; + uint8_t invert, *planep = data; + int width, height, stride; + + width = v->s.mb_width; + height = v->s.mb_height; + stride = v->s.mb_stride; + invert = get_bits(gb, 1); + imode = get_vlc2(gb, vc1_imode_vlc.table, VC1_IMODE_VLC_BITS, 1); + + *raw_flag = 0; + switch (imode) + { + case IMODE_RAW: + //Data is actually read in the MB layer (same for all tests == "raw") + *raw_flag = 1; //invert ignored + return invert; + case IMODE_DIFF2: + case IMODE_NORM2: + if ((height * width) & 1) + { + *planep++ = get_bits(gb, 1); + offset = 1; + } + else offset = 0; + // decode bitplane as one long line + for (y = offset; y < height * width; y += 2) { + code = get_vlc2(gb, vc1_norm2_vlc.table, VC1_NORM2_VLC_BITS, 1); + *planep++ = code & 1; + offset++; + if(offset == width) { + offset = 0; + planep += stride - width; + } + *planep++ = code >> 1; + offset++; + if(offset == width) { + offset = 0; + planep += stride - width; + } + } + break; + case IMODE_DIFF6: + case IMODE_NORM6: + if(!(height % 3) && (width % 3)) { // use 2x3 decoding + for(y = 0; y < height; y+= 3) { + for(x = width & 1; x < width; x += 2) { + code = get_vlc2(gb, vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2); + if(code < 0){ + av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n"); + return -1; + } + planep[x + 0] = (code >> 0) & 1; + planep[x + 1] = (code >> 1) & 1; + planep[x + 0 + stride] = (code >> 2) & 1; + planep[x + 1 + stride] = (code >> 3) & 1; + planep[x + 0 + stride * 2] = (code >> 4) & 1; + planep[x + 1 + stride * 2] = (code >> 5) & 1; + } + planep += stride * 3; + } + if(width & 1) decode_colskip(data, 1, height, stride, &v->s.gb); + } else { // 3x2 + planep += (height & 1) * stride; + for(y = height & 1; y < height; y += 2) { + for(x = width % 3; x < width; x += 3) { + code = get_vlc2(gb, vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2); + if(code < 0){ + av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n"); + return -1; + } + planep[x + 0] = (code >> 0) & 1; + planep[x + 1] = (code >> 1) & 1; + planep[x + 2] = (code >> 2) & 1; + planep[x + 0 + stride] = (code >> 3) & 1; + planep[x + 1 + stride] = (code >> 4) & 1; + planep[x + 2 + stride] = (code >> 5) & 1; + } + planep += stride * 2; + } + x = width % 3; + if(x) decode_colskip(data , x, height , stride, &v->s.gb); + if(height & 1) decode_rowskip(data+x, width - x, 1, stride, &v->s.gb); + } + break; + case IMODE_ROWSKIP: + decode_rowskip(data, width, height, stride, &v->s.gb); + break; + case IMODE_COLSKIP: + decode_colskip(data, width, height, stride, &v->s.gb); + break; + default: break; + } + + /* Applying diff operator */ + if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6) + { + planep = data; + planep[0] ^= invert; + for (x=1; x<width; x++) + planep[x] ^= planep[x-1]; + for (y=1; y<height; y++) + { + planep += stride; + planep[0] ^= planep[-stride]; + for (x=1; x<width; x++) + { + if (planep[x-1] != planep[x-stride]) planep[x] ^= invert; + else planep[x] ^= planep[x-1]; + } + } + } + else if (invert) + { + planep = data; + for (x=0; x<stride*height; x++) planep[x] = !planep[x]; //FIXME stride + } + return (imode<<1) + invert; +} + +/** @} */ //Bitplane group + +/***********************************************************************/ +/** VOP Dquant decoding + * @param v VC-1 Context + */ +static int vop_dquant_decoding(VC1Context *v) +{ + GetBitContext *gb = &v->s.gb; + int pqdiff; + + //variable size + if (v->dquant == 2) + { + pqdiff = get_bits(gb, 3); + if (pqdiff == 7) v->altpq = get_bits(gb, 5); + else v->altpq = v->pq + pqdiff + 1; + } + else + { + v->dquantfrm = get_bits(gb, 1); + if ( v->dquantfrm ) + { + v->dqprofile = get_bits(gb, 2); + switch (v->dqprofile) + { + case DQPROFILE_SINGLE_EDGE: + case DQPROFILE_DOUBLE_EDGES: + v->dqsbedge = get_bits(gb, 2); + break; + case DQPROFILE_ALL_MBS: + v->dqbilevel = get_bits(gb, 1); + default: break; //Forbidden ? + } + if (v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS) + { + pqdiff = get_bits(gb, 3); + if (pqdiff == 7) v->altpq = get_bits(gb, 5); + else v->altpq = v->pq + pqdiff + 1; + } + } + } + return 0; +} + +/** Put block onto picture + * @todo move to DSPContext + */ +static void vc1_put_block(VC1Context *v, DCTELEM block[6][64]) +{ + uint8_t *Y; + int ys, us, vs; + DSPContext *dsp = &v->s.dsp; + + if(v->rangeredfrm) { + int i, j, k; + for(k = 0; k < 6; k++) + for(j = 0; j < 8; j++) + for(i = 0; i < 8; i++) + block[k][i + j*8] = ((block[k][i + j*8] - 128) << 1) + 128; + + } + ys = v->s.current_picture.linesize[0]; + us = v->s.current_picture.linesize[1]; + vs = v->s.current_picture.linesize[2]; + Y = v->s.dest[0]; + + dsp->put_pixels_clamped(block[0], Y, ys); + dsp->put_pixels_clamped(block[1], Y + 8, ys); + Y += ys * 8; + dsp->put_pixels_clamped(block[2], Y, ys); + dsp->put_pixels_clamped(block[3], Y + 8, ys); + + if(!(v->s.flags & CODEC_FLAG_GRAY)) { + dsp->put_pixels_clamped(block[4], v->s.dest[1], us); + dsp->put_pixels_clamped(block[5], v->s.dest[2], vs); + } +} + +/** Do motion compensation over 1 macroblock + * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c + */ +static void vc1_mc_1mv(VC1Context *v, int dir) +{ + MpegEncContext *s = &v->s; + DSPContext *dsp = &v->s.dsp; + uint8_t *srcY, *srcU, *srcV; + int dxy, uvdxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y; + + if(!v->s.last_picture.data[0])return; + + mx = s->mv[0][0][0]; + my = s->mv[0][0][1]; + uvmx = (mx + ((mx & 3) == 3)) >> 1; + uvmy = (my + ((my & 3) == 3)) >> 1; + if(!dir) { + srcY = s->last_picture.data[0]; + srcU = s->last_picture.data[1]; + srcV = s->last_picture.data[2]; + } else { + srcY = s->next_picture.data[0]; + srcU = s->next_picture.data[1]; + srcV = s->next_picture.data[2]; + } + + src_x = s->mb_x * 16 + (mx >> 2); + src_y = s->mb_y * 16 + (my >> 2); + uvsrc_x = s->mb_x * 8 + (uvmx >> 2); + uvsrc_y = s->mb_y * 8 + (uvmy >> 2); + + src_x = clip( src_x, -16, s->mb_width * 16); + src_y = clip( src_y, -16, s->mb_height * 16); + uvsrc_x = clip(uvsrc_x, -8, s->mb_width * 8); + uvsrc_y = clip(uvsrc_y, -8, s->mb_height * 8); + + srcY += src_y * s->linesize + src_x; + srcU += uvsrc_y * s->uvlinesize + uvsrc_x; + srcV += uvsrc_y * s->uvlinesize + uvsrc_x; + + /* for grayscale we should not try to read from unknown area */ + if(s->flags & CODEC_FLAG_GRAY) { + srcU = s->edge_emu_buffer + 18 * s->linesize; + srcV = s->edge_emu_buffer + 18 * s->linesize; + } + + if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP) + || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3 + || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){ + uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize; + + srcY -= s->mspel * (1 + s->linesize); + ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2, + src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos); + srcY = s->edge_emu_buffer; + ff_emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8+1, 8+1, + uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1); + ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1, + uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1); + srcU = uvbuf; + srcV = uvbuf + 16; + /* if we deal with range reduction we need to scale source blocks */ + if(v->rangeredfrm) { + int i, j; + uint8_t *src, *src2; + + src = srcY; + for(j = 0; j < 17 + s->mspel*2; j++) { + for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128; + src += s->linesize; + } + src = srcU; src2 = srcV; + for(j = 0; j < 9; j++) { + for(i = 0; i < 9; i++) { + src[i] = ((src[i] - 128) >> 1) + 128; + src2[i] = ((src2[i] - 128) >> 1) + 128; + } + src += s->uvlinesize; + src2 += s->uvlinesize; + } + } + /* if we deal with intensity compensation we need to scale source blocks */ + if(v->mv_mode == MV_PMODE_INTENSITY_COMP) { + int i, j; + uint8_t *src, *src2; + + src = srcY; + for(j = 0; j < 17 + s->mspel*2; j++) { + for(i = 0; i < 17 + s->mspel*2; i++) src[i] = v->luty[src[i]]; + src += s->linesize; + } + src = srcU; src2 = srcV; + for(j = 0; j < 9; j++) { + for(i = 0; i < 9; i++) { + src[i] = v->lutuv[src[i]]; + src2[i] = v->lutuv[src2[i]]; + } + src += s->uvlinesize; + src2 += s->uvlinesize; + } + } + srcY += s->mspel * (1 + s->linesize); + } + + if(v->fastuvmc) { + uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1)); + uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1)); + } + + if(s->mspel) { + dxy = ((my & 3) << 2) | (mx & 3); + dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] , srcY , s->linesize, v->rnd); + dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd); + srcY += s->linesize * 8; + dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize , srcY , s->linesize, v->rnd); + dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd); + } else if(!s->quarter_sample) { // hpel mc + mx >>= 1; + my >>= 1; + dxy = ((my & 1) << 1) | (mx & 1); + + if(!v->rnd) + dsp->put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16); + else + dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16); + } else { + dxy = ((my & 3) << 2) | (mx & 3); + + if(!v->rnd) + dsp->put_qpel_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize); + else + dsp->put_no_rnd_qpel_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize); + } + + if(s->flags & CODEC_FLAG_GRAY) return; + /* Chroma MC always uses qpel blilinear */ + uvdxy = ((uvmy & 3) << 2) | (uvmx & 3); + if(!v->rnd){ + dsp->put_qpel_pixels_tab[1][uvdxy](s->dest[1], srcU, s->uvlinesize); + dsp->put_qpel_pixels_tab[1][uvdxy](s->dest[2], srcV, s->uvlinesize); + }else{ + dsp->put_no_rnd_qpel_pixels_tab[1][uvdxy](s->dest[1], srcU, s->uvlinesize); + dsp->put_no_rnd_qpel_pixels_tab[1][uvdxy](s->dest[2], srcV, s->uvlinesize); + } +} + +/** Do motion compensation for 4-MV macroblock - luminance block + */ +static void vc1_mc_4mv_luma(VC1Context *v, int n) +{ + MpegEncContext *s = &v->s; + DSPContext *dsp = &v->s.dsp; + uint8_t *srcY; + int dxy, mx, my, src_x, src_y; + int off; + + if(!v->s.last_picture.data[0])return; + mx = s->mv[0][n][0]; + my = s->mv[0][n][1]; + srcY = s->last_picture.data[0]; + + off = s->linesize * 4 * (n&2) + (n&1) * 8; + + src_x = s->mb_x * 16 + (n&1) * 8 + (mx >> 2); + src_y = s->mb_y * 16 + (n&2) * 4 + (my >> 2); + + src_x = clip( src_x, -16, s->mb_width * 16); + src_y = clip( src_y, -16, s->mb_height * 16); + + srcY += src_y * s->linesize + src_x; + + if(v->rangeredfrm || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 8 - s->mspel + || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 8 - s->mspel){ + srcY -= s->mspel * (1 + s->linesize); + ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 9+s->mspel*2, 9+s->mspel*2, + src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos); + srcY = s->edge_emu_buffer; + /* if we deal with range reduction we need to scale source blocks */ + if(v->rangeredfrm) { + int i, j; + uint8_t *src; + + src = srcY; + for(j = 0; j < 9 + s->mspel*2; j++) { + for(i = 0; i < 9 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128; + src += s->linesize; + } + } + srcY += s->mspel * (1 + s->linesize); + } + + if(s->mspel) { + dxy = ((my & 3) << 2) | (mx & 3); + dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, v->rnd); + } else if(!s->quarter_sample) { // hpel mc + mx >>= 1; + my >>= 1; + dxy = ((my & 1) << 1) | (mx & 1); + + if(!v->rnd) + dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8); + else + dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8); + } else { + dxy = ((my & 3) << 2) | (mx & 3); + + if(!v->rnd) + dsp->put_qpel_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize); + else + dsp->put_no_rnd_qpel_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize); + } +} + +static inline int median4(int a, int b, int c, int d) +{ + if(a < b) { + if(c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2; + else return (FFMIN(b, c) + FFMAX(a, d)) / 2; + } else { + if(c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2; + else return (FFMIN(a, c) + FFMAX(b, d)) / 2; + } +} + + +/** Do motion compensation for 4-MV macroblock - both chroma blocks + */ +static void vc1_mc_4mv_chroma(VC1Context *v) +{ + MpegEncContext *s = &v->s; + DSPContext *dsp = &v->s.dsp; + uint8_t *srcU, *srcV; + int uvdxy, uvmx, uvmy, uvsrc_x, uvsrc_y; + int i, idx, tx = 0, ty = 0; + int mvx[4], mvy[4], intra[4]; + static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4}; + + if(!v->s.last_picture.data[0])return; + if(s->flags & CODEC_FLAG_GRAY) return; + + for(i = 0; i < 4; i++) { + mvx[i] = s->mv[0][i][0]; + mvy[i] = s->mv[0][i][1]; + intra[i] = v->mb_type[0][s->block_index[i]]; + } + + /* calculate chroma MV vector from four luma MVs */ + idx = (intra[3] << 3) | (intra[2] << 2) | (intra[1] << 1) | intra[0]; + if(!idx) { // all blocks are inter + tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]); + ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]); + } else if(count[idx] == 1) { // 3 inter blocks + switch(idx) { + case 0x1: + tx = mid_pred(mvx[1], mvx[2], mvx[3]); + ty = mid_pred(mvy[1], mvy[2], mvy[3]); + break; + case 0x2: + tx = mid_pred(mvx[0], mvx[2], mvx[3]); + ty = mid_pred(mvy[0], mvy[2], mvy[3]); + break; + case 0x4: + tx = mid_pred(mvx[0], mvx[1], mvx[3]); + ty = mid_pred(mvy[0], mvy[1], mvy[3]); + break; + case 0x8: + tx = mid_pred(mvx[0], mvx[1], mvx[2]); + ty = mid_pred(mvy[0], mvy[1], mvy[2]); + break; + } + } else if(count[idx] == 2) { + int t1 = 0, t2 = 0; + for(i=0; i<3;i++) if(!intra[i]) {t1 = i; break;} + for(i= t1+1; i<4; i++)if(!intra[i]) {t2 = i; break;} + tx = (mvx[t1] + mvx[t2]) / 2; + ty = (mvy[t1] + mvy[t2]) / 2; + } else + return; //no need to do MC for inter blocks + + uvmx = (tx + ((tx&3) == 3)) >> 1; + uvmy = (ty + ((ty&3) == 3)) >> 1; + + uvsrc_x = s->mb_x * 8 + (uvmx >> 2); + uvsrc_y = s->mb_y * 8 + (uvmy >> 2); + + uvsrc_x = clip(uvsrc_x, -8, s->mb_width * 8); + uvsrc_y = clip(uvsrc_y, -8, s->mb_height * 8); + srcU = s->last_picture.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x; + srcV = s->last_picture.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x; + if(v->rangeredfrm || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9 + || (unsigned)uvsrc_y > (s->v_edge_pos >> 1) - 9){ + ff_emulated_edge_mc(s->edge_emu_buffer , srcU, s->uvlinesize, 8+1, 8+1, + uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1); + ff_emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize, 8+1, 8+1, + uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1); + srcU = s->edge_emu_buffer; + srcV = s->edge_emu_buffer + 16; + + /* if we deal with range reduction we need to scale source blocks */ + if(v->rangeredfrm) { + int i, j; + uint8_t *src, *src2; + + src = srcU; src2 = srcV; + for(j = 0; j < 9; j++) { + for(i = 0; i < 9; i++) { + src[i] = ((src[i] - 128) >> 1) + 128; + src2[i] = ((src2[i] - 128) >> 1) + 128; + } + src += s->uvlinesize; + src2 += s->uvlinesize; + } + } + } + + if(v->fastuvmc) { + uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1)); + uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1)); + } + + /* Chroma MC always uses qpel blilinear */ + uvdxy = ((uvmy & 3) << 2) | (uvmx & 3); + if(!v->rnd){ + dsp->put_qpel_pixels_tab[1][uvdxy](s->dest[1], srcU, s->uvlinesize); + dsp->put_qpel_pixels_tab[1][uvdxy](s->dest[2], srcV, s->uvlinesize); + }else{ + dsp->put_no_rnd_qpel_pixels_tab[1][uvdxy](s->dest[1], srcU, s->uvlinesize); + dsp->put_no_rnd_qpel_pixels_tab[1][uvdxy](s->dest[2], srcV, s->uvlinesize); + } +} + +/** + * Decode Simple/Main Profiles sequence header + * @see Figure 7-8, p16-17 + * @param avctx Codec context + * @param gb GetBit context initialized from Codec context extra_data + * @return Status + */ +static int decode_sequence_header(AVCodecContext *avctx, GetBitContext *gb) +{ + VC1Context *v = avctx->priv_data; + + av_log(avctx, AV_LOG_INFO, "Header: %0X\n", show_bits(gb, 32)); + v->profile = get_bits(gb, 2); + if (v->profile == 2) + { + av_log(avctx, AV_LOG_ERROR, "Profile value 2 is forbidden (and WMV3 Complex Profile is unsupported)\n"); + return -1; + } + + if (v->profile == PROFILE_ADVANCED) + { + v->level = get_bits(gb, 3); + if(v->level >= 5) + { + av_log(avctx, AV_LOG_ERROR, "Reserved LEVEL %i\n",v->level); + } + v->chromaformat = get_bits(gb, 2); + if (v->chromaformat != 1) + { + av_log(avctx, AV_LOG_ERROR, + "Only 4:2:0 chroma format supported\n"); + return -1; + } + } + else + { + v->res_sm = get_bits(gb, 2); //reserved + if (v->res_sm) + { + av_log(avctx, AV_LOG_ERROR, + "Reserved RES_SM=%i is forbidden\n", v->res_sm); + return -1; + } + } + + // (fps-2)/4 (->30) + v->frmrtq_postproc = get_bits(gb, 3); //common + // (bitrate-32kbps)/64kbps + v->bitrtq_postproc = get_bits(gb, 5); //common + v->s.loop_filter = get_bits(gb, 1); //common + if(v->s.loop_filter == 1 && v->profile == PROFILE_SIMPLE) + { + av_log(avctx, AV_LOG_ERROR, + "LOOPFILTER shell not be enabled in simple profile\n"); + } + + if (v->profile < PROFILE_ADVANCED) + { + v->res_x8 = get_bits(gb, 1); //reserved + if (v->res_x8) + { + av_log(avctx, AV_LOG_ERROR, + "1 for reserved RES_X8 is forbidden\n"); + //return -1; + } + v->multires = get_bits(gb, 1); + v->res_fasttx = get_bits(gb, 1); + if (!v->res_fasttx) + { + av_log(avctx, AV_LOG_ERROR, + "0 for reserved RES_FASTTX is forbidden\n"); + //return -1; + } + } + + v->fastuvmc = get_bits(gb, 1); //common + if (!v->profile && !v->fastuvmc) + { + av_log(avctx, AV_LOG_ERROR, + "FASTUVMC unavailable in Simple Profile\n"); + return -1; + } + v->extended_mv = get_bits(gb, 1); //common + if (!v->profile && v->extended_mv) + { + av_log(avctx, AV_LOG_ERROR, + "Extended MVs unavailable in Simple Profile\n"); + return -1; + } + v->dquant = get_bits(gb, 2); //common + v->vstransform = get_bits(gb, 1); //common + + if (v->profile < PROFILE_ADVANCED) + { + v->res_transtab = get_bits(gb, 1); + if (v->res_transtab) + { + av_log(avctx, AV_LOG_ERROR, + "1 for reserved RES_TRANSTAB is forbidden\n"); + return -1; + } + } + + v->overlap = get_bits(gb, 1); //common + + if (v->profile < PROFILE_ADVANCED) + { + v->s.resync_marker = get_bits(gb, 1); + v->rangered = get_bits(gb, 1); + if (v->rangered && v->profile == PROFILE_SIMPLE) + { + av_log(avctx, AV_LOG_INFO, + "RANGERED should be set to 0 in simple profile\n"); + } + } + + v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common + v->quantizer_mode = get_bits(gb, 2); //common + + if (v->profile < PROFILE_ADVANCED) + { + v->finterpflag = get_bits(gb, 1); //common + v->res_rtm_flag = get_bits(gb, 1); //reserved + if (!v->res_rtm_flag) + { + av_log(avctx, AV_LOG_ERROR, + "0 for reserved RES_RTM_FLAG is forbidden\n"); + //return -1; + } + av_log(avctx, AV_LOG_DEBUG, + "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n" + "LoopFilter=%i, MultiRes=%i, FastUVMC=%i, Extended MV=%i\n" + "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n" + "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n", + v->profile, v->frmrtq_postproc, v->bitrtq_postproc, + v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv, + v->rangered, v->vstransform, v->overlap, v->s.resync_marker, + v->dquant, v->quantizer_mode, avctx->max_b_frames + ); + return 0; + } + return -1; +} + + +static int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb) +{ + int pqindex, lowquant, status; + + if(v->finterpflag) v->interpfrm = get_bits(gb, 1); + skip_bits(gb, 2); //framecnt unused + v->rangeredfrm = 0; + if (v->rangered) v->rangeredfrm = get_bits(gb, 1); + v->s.pict_type = get_bits(gb, 1); + if (v->s.avctx->max_b_frames) { + if (!v->s.pict_type) { + if (get_bits(gb, 1)) v->s.pict_type = I_TYPE; + else v->s.pict_type = B_TYPE; + } else v->s.pict_type = P_TYPE; + } else v->s.pict_type = v->s.pict_type ? P_TYPE : I_TYPE; + + if(v->s.pict_type == I_TYPE) + get_bits(gb, 7); // skip buffer fullness + if(v->s.pict_type == B_TYPE) { + v->bfraction = get_vlc2(gb, vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1); + v->bfraction = vc1_bfraction_lut[v->bfraction]; + if(v->bfraction == -1) { + v->s.pict_type = BI_TYPE; + } + } + + /* calculate RND */ + if(v->s.pict_type == I_TYPE) + v->rnd = 1; + if(v->s.pict_type == P_TYPE) + v->rnd ^= 1; + + /* Quantizer stuff */ + pqindex = get_bits(gb, 5); + if (v->quantizer_mode == QUANT_FRAME_IMPLICIT) + v->pq = pquant_table[0][pqindex]; + else + v->pq = pquant_table[1][pqindex]; + + v->pquantizer = 1; + if (v->quantizer_mode == QUANT_FRAME_IMPLICIT) + v->pquantizer = pqindex < 9; + if (v->quantizer_mode == QUANT_NON_UNIFORM) + v->pquantizer = 0; + v->pqindex = pqindex; + if (pqindex < 9) v->halfpq = get_bits(gb, 1); + else v->halfpq = 0; + if (v->quantizer_mode == QUANT_FRAME_EXPLICIT) + v->pquantizer = get_bits(gb, 1); + v->dquantfrm = 0; + if (v->extended_mv == 1) v->mvrange = get_prefix(gb, 0, 3); + v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13 + v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11 + v->range_x = 1 << (v->k_x - 1); + v->range_y = 1 << (v->k_y - 1); + if (v->profile == PROFILE_ADVANCED) + { + if (v->postprocflag) v->postproc = get_bits(gb, 1); + } + else + if (v->multires && v->s.pict_type != B_TYPE) v->respic = get_bits(gb, 2); + +//av_log(v->s.avctx, AV_LOG_INFO, "%c Frame: QP=[%i]%i (+%i/2) %i\n", +// (v->s.pict_type == P_TYPE) ? 'P' : ((v->s.pict_type == I_TYPE) ? 'I' : 'B'), pqindex, v->pq, v->halfpq, v->rangeredfrm); + + //TODO: complete parsing for P/B/BI frames + switch(v->s.pict_type) { + case P_TYPE: + if (v->pq < 5) v->tt_index = 0; + else if(v->pq < 13) v->tt_index = 1; + else v->tt_index = 2; + + lowquant = (v->pq > 12) ? 0 : 1; + v->mv_mode = mv_pmode_table[lowquant][get_prefix(gb, 1, 4)]; + if (v->mv_mode == MV_PMODE_INTENSITY_COMP) + { + int scale, shift, i; + v->mv_mode2 = mv_pmode_table2[lowquant][get_prefix(gb, 1, 3)]; + v->lumscale = get_bits(gb, 6); + v->lumshift = get_bits(gb, 6); + /* fill lookup tables for intensity compensation */ + if(!v->lumscale) { + scale = -64; + shift = (255 - v->lumshift * 2) << 6; + if(v->lumshift > 31) + shift += 128 << 6; + } else { + scale = v->lumscale + 32; + if(v->lumshift > 31) + shift = (v->lumshift - 64) << 6; + else + shift = v->lumshift << 6; + } + for(i = 0; i < 256; i++) { + v->luty[i] = clip_uint8((scale * i + shift + 32) >> 6); + v->lutuv[i] = clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6); + } + } + if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN) + v->s.quarter_sample = 0; + else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) { + if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN) + v->s.quarter_sample = 0; + else + v->s.quarter_sample = 1; + } else + v->s.quarter_sample = 1; + v->s.mspel = !(v->mv_mode == MV_PMODE_1MV_HPEL_BILIN || (v->mv_mode == MV_PMODE_INTENSITY_COMP && v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)); + + if ((v->mv_mode == MV_PMODE_INTENSITY_COMP && + v->mv_mode2 == MV_PMODE_MIXED_MV) + || v->mv_mode == MV_PMODE_MIXED_MV) + { + status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v); + if (status < 0) return -1; + av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: " + "Imode: %i, Invert: %i\n", status>>1, status&1); + } else { + v->mv_type_is_raw = 0; + memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height); + } + status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v); + if (status < 0) return -1; + av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: " + "Imode: %i, Invert: %i\n", status>>1, status&1); + + /* Hopefully this is correct for P frames */ + v->s.mv_table_index = get_bits(gb, 2); //but using vc1_ tables + v->cbpcy_vlc = &vc1_cbpcy_p_vlc[get_bits(gb, 2)]; + + if (v->dquant) + { + av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n"); + vop_dquant_decoding(v); + } + + v->ttfrm = 0; //FIXME Is that so ? + if (v->vstransform) + { + v->ttmbf = get_bits(gb, 1); + if (v->ttmbf) + { + v->ttfrm = ttfrm_to_tt[get_bits(gb, 2)]; + } + } else { + v->ttmbf = 1; + v->ttfrm = TT_8X8; + } + break; + case B_TYPE: + if (v->pq < 5) v->tt_index = 0; + else if(v->pq < 13) v->tt_index = 1; + else v->tt_index = 2; + + lowquant = (v->pq > 12) ? 0 : 1; + v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN; + v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV); + v->s.mspel = v->s.quarter_sample; + + status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v); + if (status < 0) return -1; + av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: " + "Imode: %i, Invert: %i\n", status>>1, status&1); + status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v); + if (status < 0) return -1; + av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: " + "Imode: %i, Invert: %i\n", status>>1, status&1); + + v->s.mv_table_index = get_bits(gb, 2); + v->cbpcy_vlc = &vc1_cbpcy_p_vlc[get_bits(gb, 2)]; + + if (v->dquant) + { + av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n"); + vop_dquant_decoding(v); + } + + v->ttfrm = 0; + if (v->vstransform) + { + v->ttmbf = get_bits(gb, 1); + if (v->ttmbf) + { + v->ttfrm = ttfrm_to_tt[get_bits(gb, 2)]; + } + } else { + v->ttmbf = 1; + v->ttfrm = TT_8X8; + } + break; + } + + /* AC Syntax */ + v->c_ac_table_index = decode012(gb); + if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE) + { + v->y_ac_table_index = decode012(gb); + } + /* DC Syntax */ + v->s.dc_table_index = get_bits(gb, 1); + + return 0; +} + +/***********************************************************************/ +/** + * @defgroup block VC-1 Block-level functions + * @see 7.1.4, p91 and 8.1.1.7, p(1)04 + * @todo TODO: Integrate to MpegEncContext facilities + * @{ + */ + +/** + * @def GET_MQUANT + * @brief Get macroblock-level quantizer scale + * @warning XXX: qdiff to the frame quant, not previous quant ? + * @fixme XXX: Don't know how to initialize mquant otherwise in last case + */ +#define GET_MQUANT() \ + if (v->dquantfrm) \ + { \ + int edges = 0; \ + if (v->dqprofile == DQPROFILE_ALL_MBS) \ + { \ + if (v->dqbilevel) \ + { \ + mquant = (get_bits(gb, 1)) ? v->altpq : v->pq; \ + } \ + else \ + { \ + mqdiff = get_bits(gb, 3); \ + if (mqdiff != 7) mquant = v->pq + mqdiff; \ + else mquant = get_bits(gb, 5); \ + } \ + } \ + if(v->dqprofile == DQPROFILE_SINGLE_EDGE) \ + edges = 1 << v->dqsbedge; \ + else if(v->dqprofile == DQPROFILE_DOUBLE_EDGES) \ + edges = (3 << v->dqsbedge) % 15; \ + else if(v->dqprofile == DQPROFILE_FOUR_EDGES) \ + edges = 15; \ + if((edges&1) && !s->mb_x) \ + mquant = v->altpq; \ + if((edges&2) && s->first_slice_line) \ + mquant = v->altpq; \ + if((edges&4) && s->mb_x == (s->mb_width - 1)) \ + mquant = v->altpq; \ + if((edges&8) && s->mb_y == (s->mb_height - 1)) \ + mquant = v->altpq; \ + } + +/** + * @def GET_MVDATA(_dmv_x, _dmv_y) + * @brief Get MV differentials + * @see MVDATA decoding from 8.3.5.2, p(1)20 + * @param _dmv_x Horizontal differential for decoded MV + * @param _dmv_y Vertical differential for decoded MV + * @todo TODO: Use MpegEncContext arrays to store them + */ +#define GET_MVDATA(_dmv_x, _dmv_y) \ + index = 1 + get_vlc2(gb, vc1_mv_diff_vlc[s->mv_table_index].table,\ + VC1_MV_DIFF_VLC_BITS, 2); \ + if (index > 36) \ + { \ + mb_has_coeffs = 1; \ + index -= 37; \ + } \ + else mb_has_coeffs = 0; \ + s->mb_intra = 0; \ + if (!index) { _dmv_x = _dmv_y = 0; } \ + else if (index == 35) \ + { \ + _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \ + _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \ + } \ + else if (index == 36) \ + { \ + _dmv_x = 0; \ + _dmv_y = 0; \ + s->mb_intra = 1; \ + } \ + else \ + { \ + index1 = index%6; \ + if (!s->quarter_sample && index1 == 5) val = 1; \ + else val = 0; \ + if(size_table[index1] - val > 0) \ + val = get_bits(gb, size_table[index1] - val); \ + else val = 0; \ + sign = 0 - (val&1); \ + _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \ + \ + index1 = index/6; \ + if (!s->quarter_sample && index1 == 5) val = 1; \ + else val = 0; \ + if(size_table[index1] - val > 0) \ + val = get_bits(gb, size_table[index1] - val); \ + else val = 0; \ + sign = 0 - (val&1); \ + _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign; \ + } + +/** Predict and set motion vector + */ +static inline void vc1_pred_mv(MpegEncContext *s, int n, int dmv_x, int dmv_y, int mv1, int r_x, int r_y, uint8_t* is_intra) +{ + int xy, wrap, off = 0; + int16_t *A, *B, *C; + int px, py; + int sum; + + /* scale MV difference to be quad-pel */ + dmv_x <<= 1 - s->quarter_sample; + dmv_y <<= 1 - s->quarter_sample; + + wrap = s->b8_stride; + xy = s->block_index[n]; + + if(s->mb_intra){ + s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0; + s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0; + if(mv1) { /* duplicate motion data for 1-MV block */ + s->current_picture.motion_val[0][xy + 1][0] = 0; + s->current_picture.motion_val[0][xy + 1][1] = 0; + s->current_picture.motion_val[0][xy + wrap][0] = 0; + s->current_picture.motion_val[0][xy + wrap][1] = 0; + s->current_picture.motion_val[0][xy + wrap + 1][0] = 0; + s->current_picture.motion_val[0][xy + wrap + 1][1] = 0; + } + return; + } + + C = s->current_picture.motion_val[0][xy - 1]; + A = s->current_picture.motion_val[0][xy - wrap]; + if(mv1) + off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2; + else { + //in 4-MV mode different blocks have different B predictor position + switch(n){ + case 0: + off = (s->mb_x > 0) ? -1 : 1; + break; + case 1: + off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1; + break; + case 2: + off = 1; + break; + case 3: + off = -1; + } + } + B = s->current_picture.motion_val[0][xy - wrap + off]; + + if(!s->first_slice_line || (n==2 || n==3)) { // predictor A is not out of bounds + if(s->mb_width == 1) { + px = A[0]; + py = A[1]; + } else { + px = mid_pred(A[0], B[0], C[0]); + py = mid_pred(A[1], B[1], C[1]); + } + } else if(s->mb_x || (n==1 || n==3)) { // predictor C is not out of bounds + px = C[0]; + py = C[1]; + } else { + px = py = 0; + } + /* Pullback MV as specified in 8.3.5.3.4 */ + { + int qx, qy, X, Y; + qx = (s->mb_x << 6) + ((n==1 || n==3) ? 32 : 0); + qy = (s->mb_y << 6) + ((n==2 || n==3) ? 32 : 0); + X = (s->mb_width << 6) - 4; + Y = (s->mb_height << 6) - 4; + if(mv1) { + if(qx + px < -60) px = -60 - qx; + if(qy + py < -60) py = -60 - qy; + } else { + if(qx + px < -28) px = -28 - qx; + if(qy + py < -28) py = -28 - qy; + } + if(qx + px > X) px = X - qx; + if(qy + py > Y) py = Y - qy; + } + /* Calculate hybrid prediction as specified in 8.3.5.3.5 */ + if((!s->first_slice_line || (n==2 || n==3)) && (s->mb_x || (n==1 || n==3))) { + if(is_intra[xy - wrap]) + sum = ABS(px) + ABS(py); + else + sum = ABS(px - A[0]) + ABS(py - A[1]); + if(sum > 32) { + if(get_bits1(&s->gb)) { + px = A[0]; + py = A[1]; + } else { + px = C[0]; + py = C[1]; + } + } else { + if(is_intra[xy - 1]) + sum = ABS(px) + ABS(py); + else + sum = ABS(px - C[0]) + ABS(py - C[1]); + if(sum > 32) { + if(get_bits1(&s->gb)) { + px = A[0]; + py = A[1]; + } else { + px = C[0]; + py = C[1]; + } + } + } + } + /* store MV using signed modulus of MV range defined in 4.11 */ + s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x; + s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y; + if(mv1) { /* duplicate motion data for 1-MV block */ + s->current_picture.motion_val[0][xy + 1][0] = s->current_picture.motion_val[0][xy][0]; + s->current_picture.motion_val[0][xy + 1][1] = s->current_picture.motion_val[0][xy][1]; + s->current_picture.motion_val[0][xy + wrap][0] = s->current_picture.motion_val[0][xy][0]; + s->current_picture.motion_val[0][xy + wrap][1] = s->current_picture.motion_val[0][xy][1]; + s->current_picture.motion_val[0][xy + wrap + 1][0] = s->current_picture.motion_val[0][xy][0]; + s->current_picture.motion_val[0][xy + wrap + 1][1] = s->current_picture.motion_val[0][xy][1]; + } +} + +/** Reconstruct motion vector for B-frame and do motion compensation + */ +static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mode) +{ + MpegEncContext *s = &v->s; + int mx[4], my[4], mv_x, mv_y; + int i; + + /* scale MV difference to be quad-pel */ + dmv_x[0] <<= 1 - s->quarter_sample; + dmv_y[0] <<= 1 - s->quarter_sample; + dmv_x[1] <<= 1 - s->quarter_sample; + dmv_y[1] <<= 1 - s->quarter_sample; + + if(direct || mode == BMV_TYPE_INTERPOLATED) { + /* TODO */ + return; + } + + if(mode == BMV_TYPE_BACKWARD) { + for(i = 0; i < 4; i++) { + mx[i] = s->last_picture.motion_val[0][s->block_index[i]][0]; + my[i] = s->last_picture.motion_val[0][s->block_index[i]][1]; + } + } else { + for(i = 0; i < 4; i++) { + mx[i] = s->next_picture.motion_val[0][s->block_index[i]][0]; + my[i] = s->next_picture.motion_val[0][s->block_index[i]][1]; + } + } + + /* XXX: not right but how to determine 4-MV intra/inter in another frame? */ + mv_x = median4(mx[0], mx[1], mx[2], mx[3]); + mv_y = median4(my[0], my[1], my[2], my[3]); + s->mv[0][0][0] = mv_x; + s->mv[0][0][1] = mv_y; + + vc1_mc_1mv(v, (mode == BMV_TYPE_FORWARD)); +} + +/** Get predicted DC value for I-frames only + * prediction dir: left=0, top=1 + * @param s MpegEncContext + * @param[in] n block index in the current MB + * @param dc_val_ptr Pointer to DC predictor + * @param dir_ptr Prediction direction for use in AC prediction + */ +static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n, + int16_t **dc_val_ptr, int *dir_ptr) +{ + int a, b, c, wrap, pred, scale; + int16_t *dc_val; + static const uint16_t dcpred[32] = { + -1, 1024, 512, 341, 256, 205, 171, 146, 128, + 114, 102, 93, 85, 79, 73, 68, 64, + 60, 57, 54, 51, 49, 47, 45, 43, + 41, 39, 38, 37, 35, 34, 33 + }; + + /* find prediction - wmv3_dc_scale always used here in fact */ + if (n < 4) scale = s->y_dc_scale; + else scale = s->c_dc_scale; + + wrap = s->block_wrap[n]; + dc_val= s->dc_val[0] + s->block_index[n]; + + /* B A + * C X + */ + c = dc_val[ - 1]; + b = dc_val[ - 1 - wrap]; + a = dc_val[ - wrap]; + + if (pq < 9 || !overlap) + { + /* Set outer values */ + if (s->first_slice_line && (n!=2 && n!=3)) b=a=dcpred[scale]; + if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=dcpred[scale]; + } + else + { + /* Set outer values */ + if (s->first_slice_line && (n!=2 && n!=3)) b=a=0; + if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=0; + } + + if (abs(a - b) <= abs(b - c)) { + pred = c; + *dir_ptr = 1;//left + } else { + pred = a; + *dir_ptr = 0;//top + } + + /* update predictor */ + *dc_val_ptr = &dc_val[0]; + return pred; +} + + +/** Get predicted DC value + * prediction dir: left=0, top=1 + * @param s MpegEncContext + * @param[in] n block index in the current MB + * @param dc_val_ptr Pointer to DC predictor + * @param dir_ptr Prediction direction for use in AC prediction + */ +static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n, + int a_avail, int c_avail, + int16_t **dc_val_ptr, int *dir_ptr) +{ + int a, b, c, wrap, pred, scale; + int16_t *dc_val; + int mb_pos = s->mb_x + s->mb_y * s->mb_stride; + int q1, q2 = 0; + + /* find prediction - wmv3_dc_scale always used here in fact */ + if (n < 4) scale = s->y_dc_scale; + else scale = s->c_dc_scale; + + wrap = s->block_wrap[n]; + dc_val= s->dc_val[0] + s->block_index[n]; + + /* B A + * C X + */ + c = dc_val[ - 1]; + b = dc_val[ - 1 - wrap]; + a = dc_val[ - wrap]; + /* scale predictors if needed */ + q1 = s->current_picture.qscale_table[mb_pos]; + if(c_avail && (n!= 1 && n!=3)) { + q2 = s->current_picture.qscale_table[mb_pos - 1]; + if(q2 && q2 != q1) + c = (c * s->y_dc_scale_table[q2] * vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18; + } + if(a_avail && (n!= 2 && n!=3)) { + q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride]; + if(q2 && q2 != q1) + a = (a * s->y_dc_scale_table[q2] * vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18; + } + if(a_avail && c_avail && (n!=3)) { + int off = mb_pos; + if(n != 1) off--; + if(n != 2) off -= s->mb_stride; + q2 = s->current_picture.qscale_table[off]; + if(q2 && q2 != q1) + b = (b * s->y_dc_scale_table[q2] * vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18; + } + + if(a_avail && c_avail) { + if(abs(a - b) <= abs(b - c)) { + pred = c; + *dir_ptr = 1;//left + } else { + pred = a; + *dir_ptr = 0;//top + } + } else if(a_avail) { + pred = a; + *dir_ptr = 0;//top + } else if(c_avail) { + pred = c; + *dir_ptr = 1;//left + } else { + pred = 0; + *dir_ptr = 1;//left + } + + /* update predictor */ + *dc_val_ptr = &dc_val[0]; + return pred; +} + + +/** + * @defgroup std_mb VC1 Macroblock-level functions in Simple/Main Profiles + * @see 7.1.4, p91 and 8.1.1.7, p(1)04 + * @todo TODO: Integrate to MpegEncContext facilities + * @{ + */ + +static inline int vc1_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr) +{ + int xy, wrap, pred, a, b, c; + + xy = s->block_index[n]; + wrap = s->b8_stride; + + /* B C + * A X + */ + a = s->coded_block[xy - 1 ]; + b = s->coded_block[xy - 1 - wrap]; + c = s->coded_block[xy - wrap]; + + if (b == c) { + pred = a; + } else { + pred = c; + } + + /* store value */ + *coded_block_ptr = &s->coded_block[xy]; + + return pred; +} + +/** + * Decode one AC coefficient + * @param v The VC1 context + * @param last Last coefficient + * @param skip How much zero coefficients to skip + * @param value Decoded AC coefficient value + * @see 8.1.3.4 + */ +static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip, int *value, int codingset) +{ + GetBitContext *gb = &v->s.gb; + int index, escape, run = 0, level = 0, lst = 0; + + index = get_vlc2(gb, vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3); + if (index != vc1_ac_sizes[codingset] - 1) { + run = vc1_index_decode_table[codingset][index][0]; + level = vc1_index_decode_table[codingset][index][1]; + lst = index >= vc1_last_decode_table[codingset]; + if(get_bits(gb, 1)) + level = -level; + } else { + escape = decode210(gb); + if (escape != 2) { + index = get_vlc2(gb, vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3); + run = vc1_index_decode_table[codingset][index][0]; + level = vc1_index_decode_table[codingset][index][1]; + lst = index >= vc1_last_decode_table[codingset]; + if(escape == 0) { + if(lst) + level += vc1_last_delta_level_table[codingset][run]; + else + level += vc1_delta_level_table[codingset][run]; + } else { + if(lst) + run += vc1_last_delta_run_table[codingset][level] + 1; + else + run += vc1_delta_run_table[codingset][level] + 1; + } + if(get_bits(gb, 1)) + level = -level; + } else { + int sign; + lst = get_bits(gb, 1); + if(v->s.esc3_level_length == 0) { + if(v->pq < 8 || v->dquantfrm) { // table 59 + v->s.esc3_level_length = get_bits(gb, 3); + if(!v->s.esc3_level_length) + v->s.esc3_level_length = get_bits(gb, 2) + 8; + } else { //table 60 + v->s.esc3_level_length = get_prefix(gb, 1, 6) + 2; + } + v->s.esc3_run_length = 3 + get_bits(gb, 2); + } + run = get_bits(gb, v->s.esc3_run_length); + sign = get_bits(gb, 1); + level = get_bits(gb, v->s.esc3_level_length); + if(sign) + level = -level; + } + } + + *last = lst; + *skip = run; + *value = level; +} + +/** Decode intra block in intra frames - should be faster than decode_intra_block + * @param v VC1Context + * @param block block to decode + * @param coded are AC coeffs present or not + * @param codingset set of VLC to decode data + */ +static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset) +{ + GetBitContext *gb = &v->s.gb; + MpegEncContext *s = &v->s; + int dc_pred_dir = 0; /* Direction of the DC prediction used */ + int run_diff, i; + int16_t *dc_val; + int16_t *ac_val, *ac_val2; + int dcdiff; + + /* Get DC differential */ + if (n < 4) { + dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3); + } else { + dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3); + } + if (dcdiff < 0){ + av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n"); + return -1; + } + if (dcdiff) + { + if (dcdiff == 119 /* ESC index value */) + { + /* TODO: Optimize */ + if (v->pq == 1) dcdiff = get_bits(gb, 10); + else if (v->pq == 2) dcdiff = get_bits(gb, 9); + else dcdiff = get_bits(gb, 8); + } + else + { + if (v->pq == 1) + dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3; + else if (v->pq == 2) + dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1; + } + if (get_bits(gb, 1)) + dcdiff = -dcdiff; + } + + /* Prediction */ + dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir); + *dc_val = dcdiff; + + /* Store the quantized DC coeff, used for prediction */ + if (n < 4) { + block[0] = dcdiff * s->y_dc_scale; + } else { + block[0] = dcdiff * s->c_dc_scale; + } + /* Skip ? */ + run_diff = 0; + i = 0; + if (!coded) { + goto not_coded; + } + + //AC Decoding + i = 1; + + { + int last = 0, skip, value; + const int8_t *zz_table; + int scale; + int k; + + scale = v->pq * 2 + v->halfpq; + + if(v->s.ac_pred) { + if(!dc_pred_dir) + zz_table = vc1_horizontal_zz; + else + zz_table = vc1_vertical_zz; + } else + zz_table = vc1_normal_zz; + + ac_val = s->ac_val[0][0] + s->block_index[n] * 16; + ac_val2 = ac_val; + if(dc_pred_dir) //left + ac_val -= 16; + else //top + ac_val -= 16 * s->block_wrap[n]; + + while (!last) { + vc1_decode_ac_coeff(v, &last, &skip, &value, codingset); + i += skip; + if(i > 63) + break; + block[zz_table[i++]] = value; + } + + /* apply AC prediction if needed */ + if(s->ac_pred) { + if(dc_pred_dir) { //left + for(k = 1; k < 8; k++) + block[k << 3] += ac_val[k]; + } else { //top + for(k = 1; k < 8; k++) + block[k] += ac_val[k + 8]; + } + } + /* save AC coeffs for further prediction */ + for(k = 1; k < 8; k++) { + ac_val2[k] = block[k << 3]; + ac_val2[k + 8] = block[k]; + } + + /* scale AC coeffs */ + for(k = 1; k < 64; k++) + if(block[k]) { + block[k] *= scale; + if(!v->pquantizer) + block[k] += (block[k] < 0) ? -v->pq : v->pq; + } + + if(s->ac_pred) i = 63; + } + +not_coded: + if(!coded) { + int k, scale; + ac_val = s->ac_val[0][0] + s->block_index[n] * 16; + ac_val2 = ac_val; + + scale = v->pq * 2 + v->halfpq; + memset(ac_val2, 0, 16 * 2); + if(dc_pred_dir) {//left + ac_val -= 16; + if(s->ac_pred) + memcpy(ac_val2, ac_val, 8 * 2); + } else {//top + ac_val -= 16 * s->block_wrap[n]; + if(s->ac_pred) + memcpy(ac_val2 + 8, ac_val + 8, 8 * 2); + } + + /* apply AC prediction if needed */ + if(s->ac_pred) { + if(dc_pred_dir) { //left + for(k = 1; k < 8; k++) { + block[k << 3] = ac_val[k] * scale; + if(!v->pquantizer && block[k << 3]) + block[k << 3] += (block[k << 3] < 0) ? -v->pq : v->pq; + } + } else { //top + for(k = 1; k < 8; k++) { + block[k] = ac_val[k + 8] * scale; + if(!v->pquantizer && block[k]) + block[k] += (block[k] < 0) ? -v->pq : v->pq; + } + } + i = 63; + } + } + s->block_last_index[n] = i; + + return 0; +} + +/** Decode intra block in inter frames - more generic version than vc1_decode_i_block + * @param v VC1Context + * @param block block to decode + * @param coded are AC coeffs present or not + * @param mquant block quantizer + * @param codingset set of VLC to decode data + */ +static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n, int coded, int mquant, int codingset) +{ + GetBitContext *gb = &v->s.gb; + MpegEncContext *s = &v->s; + int dc_pred_dir = 0; /* Direction of the DC prediction used */ + int run_diff, i; + int16_t *dc_val; + int16_t *ac_val, *ac_val2; + int dcdiff; + int mb_pos = s->mb_x + s->mb_y * s->mb_stride; + int a_avail = v->a_avail, c_avail = v->c_avail; + int use_pred = s->ac_pred; + int scale; + int q1, q2 = 0; + + /* XXX: Guard against dumb values of mquant */ + mquant = (mquant < 1) ? 0 : ( (mquant>31) ? 31 : mquant ); + + /* Set DC scale - y and c use the same */ + s->y_dc_scale = s->y_dc_scale_table[mquant]; + s->c_dc_scale = s->c_dc_scale_table[mquant]; + + /* Get DC differential */ + if (n < 4) { + dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3); + } else { + dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3); + } + if (dcdiff < 0){ + av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n"); + return -1; + } + if (dcdiff) + { + if (dcdiff == 119 /* ESC index value */) + { + /* TODO: Optimize */ + if (mquant == 1) dcdiff = get_bits(gb, 10); + else if (mquant == 2) dcdiff = get_bits(gb, 9); + else dcdiff = get_bits(gb, 8); + } + else + { + if (mquant == 1) + dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3; + else if (mquant == 2) + dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1; + } + if (get_bits(gb, 1)) + dcdiff = -dcdiff; + } + + /* Prediction */ + dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir); + *dc_val = dcdiff; + + /* Store the quantized DC coeff, used for prediction */ + + if (n < 4) { + block[0] = dcdiff * s->y_dc_scale; + } else { + block[0] = dcdiff * s->c_dc_scale; + } + /* Skip ? */ + run_diff = 0; + i = 0; + + //AC Decoding + i = 1; + + /* check if AC is needed at all and adjust direction if needed */ + if(!a_avail) dc_pred_dir = 1; + if(!c_avail) dc_pred_dir = 0; + if(!a_avail && !c_avail) use_pred = 0; + ac_val = s->ac_val[0][0] + s->block_index[n] * 16; + ac_val2 = ac_val; + + scale = mquant * 2 + v->halfpq; + + if(dc_pred_dir) //left + ac_val -= 16; + else //top + ac_val -= 16 * s->block_wrap[n]; + + q1 = s->current_picture.qscale_table[mb_pos]; + if(dc_pred_dir && c_avail) q2 = s->current_picture.qscale_table[mb_pos - 1]; + if(!dc_pred_dir && a_avail) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride]; + if(n && n<4) q2 = q1; + + if(coded) { + int last = 0, skip, value; + const int8_t *zz_table; + int k; + + zz_table = vc1_simple_progressive_8x8_zz; + + while (!last) { + vc1_decode_ac_coeff(v, &last, &skip, &value, codingset); + i += skip; + if(i > 63) + break; + block[zz_table[i++]] = value; + } + + /* apply AC prediction if needed */ + if(use_pred) { + /* scale predictors if needed*/ + if(q2 && q1!=q2) { + q1 = q1 * 2 - 1; + q2 = q2 * 2 - 1; + + if(dc_pred_dir) { //left + for(k = 1; k < 8; k++) + block[k << 3] += (ac_val[k] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18; + } else { //top + for(k = 1; k < 8; k++) + block[k] += (ac_val[k + 8] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18; + } + } else { + if(dc_pred_dir) { //left + for(k = 1; k < 8; k++) + block[k << 3] += ac_val[k]; + } else { //top + for(k = 1; k < 8; k++) + block[k] += ac_val[k + 8]; + } + } + } + /* save AC coeffs for further prediction */ + for(k = 1; k < 8; k++) { + ac_val2[k] = block[k << 3]; + ac_val2[k + 8] = block[k]; + } + + /* scale AC coeffs */ + for(k = 1; k < 64; k++) + if(block[k]) { + block[k] *= scale; + if(!v->pquantizer) + block[k] += (block[k] < 0) ? -mquant : mquant; + } + + if(use_pred) i = 63; + } else { // no AC coeffs + int k; + + memset(ac_val2, 0, 16 * 2); + if(dc_pred_dir) {//left + if(use_pred) { + memcpy(ac_val2, ac_val, 8 * 2); + if(q2 && q1!=q2) { + q1 = q1 * 2 - 1; + q2 = q2 * 2 - 1; + for(k = 1; k < 8; k++) + ac_val2[k] = (ac_val2[k] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18; + } + } + } else {//top + if(use_pred) { + memcpy(ac_val2 + 8, ac_val + 8, 8 * 2); + if(q2 && q1!=q2) { + q1 = q1 * 2 - 1; + q2 = q2 * 2 - 1; + for(k = 1; k < 8; k++) + ac_val2[k + 8] = (ac_val2[k + 8] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18; + } + } + } + + /* apply AC prediction if needed */ + if(use_pred) { + if(dc_pred_dir) { //left + for(k = 1; k < 8; k++) { + block[k << 3] = ac_val2[k] * scale; + if(!v->pquantizer && block[k << 3]) + block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant; + } + } else { //top + for(k = 1; k < 8; k++) { + block[k] = ac_val2[k + 8] * scale; + if(!v->pquantizer && block[k]) + block[k] += (block[k] < 0) ? -mquant : mquant; + } + } + i = 63; + } + } + s->block_last_index[n] = i; + + return 0; +} + +/** Decode P block + */ +static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n, int mquant, int ttmb, int first_block) +{ + MpegEncContext *s = &v->s; + GetBitContext *gb = &s->gb; + int i, j; + int subblkpat = 0; + int scale, off, idx, last, skip, value; + int ttblk = ttmb & 7; + + if(ttmb == -1) { + ttblk = ttblk_to_tt[v->tt_index][get_vlc2(gb, vc1_ttblk_vlc[v->tt_index].table, VC1_TTBLK_VLC_BITS, 1)]; + } + if(ttblk == TT_4X4) { + subblkpat = ~(get_vlc2(gb, vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1); + } + if((ttblk != TT_8X8 && ttblk != TT_4X4) && (v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))) { + subblkpat = decode012(gb); + if(subblkpat) subblkpat ^= 3; //swap decoded pattern bits + if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) ttblk = TT_8X4; + if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) ttblk = TT_4X8; + } + scale = 2 * mquant + v->halfpq; + + // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT + if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) { + subblkpat = 2 - (ttblk == TT_8X4_TOP); + ttblk = TT_8X4; + } + if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) { + subblkpat = 2 - (ttblk == TT_4X8_LEFT); + ttblk = TT_4X8; + } + switch(ttblk) { + case TT_8X8: + i = 0; + last = 0; + while (!last) { + vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2); + i += skip; + if(i > 63) + break; + idx = vc1_simple_progressive_8x8_zz[i++]; + block[idx] = value * scale; + if(!v->pquantizer) + block[idx] += (block[idx] < 0) ? -mquant : mquant; + } + s->dsp.vc1_inv_trans_8x8(block); + break; + case TT_4X4: + for(j = 0; j < 4; j++) { + last = subblkpat & (1 << (3 - j)); + i = 0; + off = (j & 1) * 4 + (j & 2) * 16; + while (!last) { + vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2); + i += skip; + if(i > 15) + break; + idx = vc1_simple_progressive_4x4_zz[i++]; + block[idx + off] = value * scale; + if(!v->pquantizer) + block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant; + } + if(!(subblkpat & (1 << (3 - j)))) + s->dsp.vc1_inv_trans_4x4(block, j); + } + break; + case TT_8X4: + for(j = 0; j < 2; j++) { + last = subblkpat & (1 << (1 - j)); + i = 0; + off = j * 32; + while (!last) { + vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2); + i += skip; + if(i > 31) + break; + idx = vc1_simple_progressive_8x4_zz[i++]; + block[idx + off] = value * scale; + if(!v->pquantizer) + block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant; + } + if(!(subblkpat & (1 << (1 - j)))) + s->dsp.vc1_inv_trans_8x4(block, j); + } + break; + case TT_4X8: + for(j = 0; j < 2; j++) { + last = subblkpat & (1 << (1 - j)); + i = 0; + off = j * 4; + while (!last) { + vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2); + i += skip; + if(i > 31) + break; + idx = vc1_simple_progressive_4x8_zz[i++]; + block[idx + off] = value * scale; + if(!v->pquantizer) + block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant; + } + if(!(subblkpat & (1 << (1 - j)))) + s->dsp.vc1_inv_trans_4x8(block, j); + } + break; + } + return 0; +} + + +/** Decode one P-frame MB (in Simple/Main profile) + * @todo TODO: Extend to AP + * @fixme FIXME: DC value for inter blocks not set + */ +static int vc1_decode_p_mb(VC1Context *v) +{ + MpegEncContext *s = &v->s; + GetBitContext *gb = &s->gb; + int i, j; + int mb_pos = s->mb_x + s->mb_y * s->mb_stride; + int cbp; /* cbp decoding stuff */ + int mqdiff, mquant; /* MB quantization */ + int ttmb = v->ttfrm; /* MB Transform type */ + int status; + + static const int size_table[6] = { 0, 2, 3, 4, 5, 8 }, + offset_table[6] = { 0, 1, 3, 7, 15, 31 }; + int mb_has_coeffs = 1; /* last_flag */ + int dmv_x, dmv_y; /* Differential MV components */ + int index, index1; /* LUT indices */ + int val, sign; /* temp values */ + int first_block = 1; + int dst_idx, off; + int skipped, fourmv; + + mquant = v->pq; /* Loosy initialization */ + + if (v->mv_type_is_raw) + fourmv = get_bits1(gb); + else + fourmv = v->mv_type_mb_plane[mb_pos]; + if (v->skip_is_raw) + skipped = get_bits1(gb); + else + skipped = v->s.mbskip_table[mb_pos]; + + s->dsp.clear_blocks(s->block[0]); + + if (!fourmv) /* 1MV mode */ + { + if (!skipped) + { + GET_MVDATA(dmv_x, dmv_y); + + s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16; + vc1_pred_mv(s, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]); + + /* FIXME Set DC val for inter block ? */ + if (s->mb_intra && !mb_has_coeffs) + { + GET_MQUANT(); + s->ac_pred = get_bits(gb, 1); + cbp = 0; + } + else if (mb_has_coeffs) + { + if (s->mb_intra) s->ac_pred = get_bits(gb, 1); + cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2); + GET_MQUANT(); + } + else + { + mquant = v->pq; + cbp = 0; + } + s->current_picture.qscale_table[mb_pos] = mquant; + + if (!v->ttmbf && !s->mb_intra && mb_has_coeffs) + ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table, + VC1_TTMB_VLC_BITS, 2); + if(!s->mb_intra) vc1_mc_1mv(v, 0); + dst_idx = 0; + for (i=0; i<6; i++) + { + s->dc_val[0][s->block_index[i]] = 0; + dst_idx += i >> 2; + val = ((cbp >> (5 - i)) & 1); + off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize); + v->mb_type[0][s->block_index[i]] = s->mb_intra; + if(s->mb_intra) { + /* check if prediction blocks A and C are available */ + v->a_avail = v->c_avail = 0; + if(i == 2 || i == 3 || !s->first_slice_line) + v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]]; + if(i == 1 || i == 3 || s->mb_x) + v->c_avail = v->mb_type[0][s->block_index[i] - 1]; + + vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset); + if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue; + s->dsp.vc1_inv_trans_8x8(s->block[i]); + if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1; + for(j = 0; j < 64; j++) s->block[i][j] += 128; + s->dsp.put_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2)); + /* TODO: proper loop filtering */ + if(v->pq >= 9 && v->overlap) { + if(v->a_avail) + s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2), (i<4) ? ((i&1)>>1) : (s->mb_y&1)); + if(v->c_avail) + s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2), (i<4) ? (i&1) : (s->mb_x&1)); + } + } else if(val) { + vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block); + if(!v->ttmbf && ttmb < 8) ttmb = -1; + first_block = 0; + if((i<4) || !(s->flags & CODEC_FLAG_GRAY)) + s->dsp.add_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize); + } + } + } + else //Skipped + { + s->mb_intra = 0; + for(i = 0; i < 6; i++) { + v->mb_type[0][s->block_index[i]] = 0; + s->dc_val[0][s->block_index[i]] = 0; + } + s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP; + s->current_picture.qscale_table[mb_pos] = 0; + vc1_pred_mv(s, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]); + vc1_mc_1mv(v, 0); + return 0; + } + } //1MV mode + else //4MV mode + { + if (!skipped /* unskipped MB */) + { + int intra_count = 0, coded_inter = 0; + int is_intra[6], is_coded[6]; + /* Get CBPCY */ + cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2); + for (i=0; i<6; i++) + { + val = ((cbp >> (5 - i)) & 1); + s->dc_val[0][s->block_index[i]] = 0; + s->mb_intra = 0; + if(i < 4) { + dmv_x = dmv_y = 0; + s->mb_intra = 0; + mb_has_coeffs = 0; + if(val) { + GET_MVDATA(dmv_x, dmv_y); + } + vc1_pred_mv(s, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]); + if(!s->mb_intra) vc1_mc_4mv_luma(v, i); + intra_count += s->mb_intra; + is_intra[i] = s->mb_intra; + is_coded[i] = mb_has_coeffs; + } + if(i&4){ + is_intra[i] = (intra_count >= 3); + is_coded[i] = val; + } + if(i == 4) vc1_mc_4mv_chroma(v); + v->mb_type[0][s->block_index[i]] = is_intra[i]; + if(!coded_inter) coded_inter = !is_intra[i] & is_coded[i]; + } + // if there are no coded blocks then don't do anything more + if(!intra_count && !coded_inter) return 0; + dst_idx = 0; + GET_MQUANT(); + s->current_picture.qscale_table[mb_pos] = mquant; + /* test if block is intra and has pred */ + { + int intrapred = 0; + for(i=0; i<6; i++) + if(is_intra[i]) { + if(((!s->first_slice_line || (i==2 || i==3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]]) + || ((s->mb_x || (i==1 || i==3)) && v->mb_type[0][s->block_index[i] - 1])) { + intrapred = 1; + break; + } + } + if(intrapred)s->ac_pred = get_bits(gb, 1); + else s->ac_pred = 0; + } + if (!v->ttmbf && coded_inter) + ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2); + for (i=0; i<6; i++) + { + dst_idx += i >> 2; + off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize); + s->mb_intra = is_intra[i]; + if (is_intra[i]) { + /* check if prediction blocks A and C are available */ + v->a_avail = v->c_avail = 0; + if(i == 2 || i == 3 || !s->first_slice_line) + v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]]; + if(i == 1 || i == 3 || s->mb_x) + v->c_avail = v->mb_type[0][s->block_index[i] - 1]; + + vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant, (i&4)?v->codingset2:v->codingset); + if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue; + s->dsp.vc1_inv_trans_8x8(s->block[i]); + if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1; + for(j = 0; j < 64; j++) s->block[i][j] += 128; + s->dsp.put_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize); + /* TODO: proper loop filtering */ + if(v->pq >= 9 && v->overlap) { + if(v->a_avail) + s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2), (i<4) ? ((i&1)>>1) : (s->mb_y&1)); + if(v->c_avail) + s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2), (i<4) ? (i&1) : (s->mb_x&1)); + } + } else if(is_coded[i]) { + status = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block); + if(!v->ttmbf && ttmb < 8) ttmb = -1; + first_block = 0; + if((i<4) || !(s->flags & CODEC_FLAG_GRAY)) + s->dsp.add_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize); + } + } + return status; + } + else //Skipped MB + { + s->mb_intra = 0; + s->current_picture.qscale_table[mb_pos] = 0; + for (i=0; i<6; i++) { + v->mb_type[0][s->block_index[i]] = 0; + s->dc_val[0][s->block_index[i]] = 0; + } + for (i=0; i<4; i++) + { + vc1_pred_mv(s, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0]); + vc1_mc_4mv_luma(v, i); + } + vc1_mc_4mv_chroma(v); + s->current_picture.qscale_table[mb_pos] = 0; + return 0; + } + } + + /* Should never happen */ + return -1; +} + +/** Decode one B-frame MB (in Main profile) + */ +static void vc1_decode_b_mb(VC1Context *v) +{ + MpegEncContext *s = &v->s; + GetBitContext *gb = &s->gb; + int i, j; + int mb_pos = s->mb_x + s->mb_y * s->mb_stride; + int cbp; /* cbp decoding stuff */ + int mqdiff, mquant; /* MB quantization */ + int ttmb = v->ttfrm; /* MB Transform type */ + + static const int size_table[6] = { 0, 2, 3, 4, 5, 8 }, + offset_table[6] = { 0, 1, 3, 7, 15, 31 }; + int mb_has_coeffs = 0; /* last_flag */ + int index, index1; /* LUT indices */ + int val, sign; /* temp values */ + int first_block = 1; + int dst_idx, off; + int skipped, direct; + int dmv_x[2], dmv_y[2]; + int bmvtype = BMV_TYPE_BACKWARD; /* XXX: is it so? */ + + mquant = v->pq; /* Loosy initialization */ + s->mb_intra = 0; + + if (v->dmb_is_raw) + direct = get_bits1(gb); + else + direct = v->direct_mb_plane[mb_pos]; + if (v->skip_is_raw) + skipped = get_bits1(gb); + else + skipped = v->s.mbskip_table[mb_pos]; + + s->dsp.clear_blocks(s->block[0]); + dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0; + for(i = 0; i < 6; i++) { + v->mb_type[0][s->block_index[i]] = 0; + s->dc_val[0][s->block_index[i]] = 0; + } + s->current_picture.qscale_table[mb_pos] = 0; + + if (!direct) { + if (!skipped) { + GET_MVDATA(dmv_x[0], dmv_y[0]); + } + if(skipped || !s->mb_intra) { + bmvtype = decode012(gb); + switch(bmvtype) { + case 0: + bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD; + break; + case 1: + bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD; + break; + case 2: + bmvtype = BMV_TYPE_INTERPOLATED; + } + } + } + + if (skipped) { + vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype); + return; + } + if (direct) { + cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2); + GET_MQUANT(); + s->current_picture.qscale_table[mb_pos] = mquant; + if(!v->ttmbf && !s->mb_intra && mb_has_coeffs) + ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2); + vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype); + } else { + if(!mb_has_coeffs && !s->mb_intra) { + /* no coded blocks - effectively skipped */ + vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype); + return; + } + if(s->mb_intra && !mb_has_coeffs) { + GET_MQUANT(); + s->current_picture.qscale_table[mb_pos] = mquant; + s->ac_pred = get_bits1(gb); + cbp = 0; + } else { + if(bmvtype == BMV_TYPE_INTERPOLATED) { + GET_MVDATA(dmv_x[1], dmv_y[1]); + if(!mb_has_coeffs) { + /* interpolated skipped block */ + vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype); + return; + } + } + if(!s->mb_intra) + vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype); + if(s->mb_intra) + s->ac_pred = get_bits1(gb); + cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2); + GET_MQUANT(); + s->current_picture.qscale_table[mb_pos] = mquant; + if(!v->ttmbf && !s->mb_intra && mb_has_coeffs) + ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2); + } + } + dst_idx = 0; + for (i=0; i<6; i++) + { + s->dc_val[0][s->block_index[i]] = 0; + dst_idx += i >> 2; + val = ((cbp >> (5 - i)) & 1); + off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize); + v->mb_type[0][s->block_index[i]] = s->mb_intra; + if(s->mb_intra) { + /* check if prediction blocks A and C are available */ + v->a_avail = v->c_avail = 0; + if(i == 2 || i == 3 || !s->first_slice_line) + v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]]; + if(i == 1 || i == 3 || s->mb_x) + v->c_avail = v->mb_type[0][s->block_index[i] - 1]; + + vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset); + if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue; + s->dsp.vc1_inv_trans_8x8(s->block[i]); + if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1; + for(j = 0; j < 64; j++) s->block[i][j] += 128; + s->dsp.put_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2)); + /* TODO: proper loop filtering */ + if(v->pq >= 9 && v->overlap) { + if(v->a_avail) + s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2), (i<4) ? ((i&1)>>1) : (s->mb_y&1)); + if(v->c_avail) + s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2), (i<4) ? (i&1) : (s->mb_x&1)); + } + } else if(val) { + vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block); + if(!v->ttmbf && ttmb < 8) ttmb = -1; + first_block = 0; + if((i<4) || !(s->flags & CODEC_FLAG_GRAY)) + s->dsp.add_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize); + } + } +} + +/** Decode blocks of I-frame + */ +static void vc1_decode_i_blocks(VC1Context *v) +{ + int k, j; + MpegEncContext *s = &v->s; + int cbp, val; + uint8_t *coded_val; + int mb_pos; + + /* select codingmode used for VLC tables selection */ + switch(v->y_ac_table_index){ + case 0: + v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA; + break; + case 1: + v->codingset = CS_HIGH_MOT_INTRA; + break; + case 2: + v->codingset = CS_MID_RATE_INTRA; + break; + } + + switch(v->c_ac_table_index){ + case 0: + v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER; + break; + case 1: + v->codingset2 = CS_HIGH_MOT_INTER; + break; + case 2: + v->codingset2 = CS_MID_RATE_INTER; + break; + } + + /* Set DC scale - y and c use the same */ + s->y_dc_scale = s->y_dc_scale_table[v->pq]; + s->c_dc_scale = s->c_dc_scale_table[v->pq]; + + //do frame decode + s->mb_x = s->mb_y = 0; + s->mb_intra = 1; + s->first_slice_line = 1; + ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END)); + for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) { + for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) { + ff_init_block_index(s); + ff_update_block_index(s); + s->dsp.clear_blocks(s->block[0]); + mb_pos = s->mb_x + s->mb_y * s->mb_width; + s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA; + s->current_picture.qscale_table[mb_pos] = v->pq; + + // do actual MB decoding and displaying + cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2); + v->s.ac_pred = get_bits(&v->s.gb, 1); + + for(k = 0; k < 6; k++) { + val = ((cbp >> (5 - k)) & 1); + + if (k < 4) { + int pred = vc1_coded_block_pred(&v->s, k, &coded_val); + val = val ^ pred; + *coded_val = val; + } + cbp |= val << (5 - k); + + vc1_decode_i_block(v, s->block[k], k, val, (k<4)? v->codingset : v->codingset2); + + s->dsp.vc1_inv_trans_8x8(s->block[k]); + if(v->pq >= 9 && v->overlap) { + for(j = 0; j < 64; j++) s->block[k][j] += 128; + } + } + + vc1_put_block(v, s->block); + if(v->pq >= 9 && v->overlap) { /* XXX: do proper overlapping insted of loop filter */ + if(!s->first_slice_line) { + s->dsp.vc1_v_overlap(s->dest[0], s->linesize, 0); + s->dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize, 0); + if(!(s->flags & CODEC_FLAG_GRAY)) { + s->dsp.vc1_v_overlap(s->dest[1], s->uvlinesize, s->mb_y&1); + s->dsp.vc1_v_overlap(s->dest[2], s->uvlinesize, s->mb_y&1); + } + } + s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize, 1); + s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize, 1); + if(s->mb_x) { + s->dsp.vc1_h_overlap(s->dest[0], s->linesize, 0); + s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize, 0); + if(!(s->flags & CODEC_FLAG_GRAY)) { + s->dsp.vc1_h_overlap(s->dest[1], s->uvlinesize, s->mb_x&1); + s->dsp.vc1_h_overlap(s->dest[2], s->uvlinesize, s->mb_x&1); + } + } + s->dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize, 1); + s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize, 1); + } + + if(get_bits_count(&s->gb) > v->bits) { + av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits); + return; + } + } + ff_draw_horiz_band(s, s->mb_y * 16, 16); + s->first_slice_line = 0; + } +} + +static void vc1_decode_p_blocks(VC1Context *v) +{ + MpegEncContext *s = &v->s; + + /* select codingmode used for VLC tables selection */ + switch(v->c_ac_table_index){ + case 0: + v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA; + break; + case 1: + v->codingset = CS_HIGH_MOT_INTRA; + break; + case 2: + v->codingset = CS_MID_RATE_INTRA; + break; + } + + switch(v->c_ac_table_index){ + case 0: + v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER; + break; + case 1: + v->codingset2 = CS_HIGH_MOT_INTER; + break; + case 2: + v->codingset2 = CS_MID_RATE_INTER; + break; + } + + ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END)); + s->first_slice_line = 1; + for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) { + for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) { + ff_init_block_index(s); + ff_update_block_index(s); + s->dsp.clear_blocks(s->block[0]); + + vc1_decode_p_mb(v); + if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) { + av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n", get_bits_count(&s->gb), v->bits,s->mb_x,s->mb_y); + return; + } + } + ff_draw_horiz_band(s, s->mb_y * 16, 16); + s->first_slice_line = 0; + } +} + +static void vc1_decode_b_blocks(VC1Context *v) +{ + MpegEncContext *s = &v->s; + + /* select codingmode used for VLC tables selection */ + switch(v->c_ac_table_index){ + case 0: + v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA; + break; + case 1: + v->codingset = CS_HIGH_MOT_INTRA; + break; + case 2: + v->codingset = CS_MID_RATE_INTRA; + break; + } + + switch(v->c_ac_table_index){ + case 0: + v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER; + break; + case 1: + v->codingset2 = CS_HIGH_MOT_INTER; + break; + case 2: + v->codingset2 = CS_MID_RATE_INTER; + break; + } + + ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END)); + s->first_slice_line = 1; + for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) { + for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) { + ff_init_block_index(s); + ff_update_block_index(s); + s->dsp.clear_blocks(s->block[0]); + + vc1_decode_b_mb(v); + if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) { + av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n", get_bits_count(&s->gb), v->bits,s->mb_x,s->mb_y); + return; + } + } + ff_draw_horiz_band(s, s->mb_y * 16, 16); + s->first_slice_line = 0; + } +} + +static void vc1_decode_blocks(VC1Context *v) +{ + + v->s.esc3_level_length = 0; + + switch(v->s.pict_type) { + case I_TYPE: + vc1_decode_i_blocks(v); + break; + case P_TYPE: + vc1_decode_p_blocks(v); + break; + case B_TYPE: + vc1_decode_b_blocks(v); + break; + } +} + + +/** Initialize a VC1/WMV3 decoder + * @todo TODO: Handle VC-1 IDUs (Transport level?) + * @todo TODO: Decypher remaining bits in extra_data + */ +static int vc1_decode_init(AVCodecContext *avctx) +{ + VC1Context *v = avctx->priv_data; + MpegEncContext *s = &v->s; + GetBitContext gb; + + if (!avctx->extradata_size || !avctx->extradata) return -1; + if (!(avctx->flags & CODEC_FLAG_GRAY)) + avctx->pix_fmt = PIX_FMT_YUV420P; + else + avctx->pix_fmt = PIX_FMT_GRAY8; + v->s.avctx = avctx; + avctx->flags |= CODEC_FLAG_EMU_EDGE; + v->s.flags |= CODEC_FLAG_EMU_EDGE; + + if(ff_h263_decode_init(avctx) < 0) + return -1; + if (vc1_init_common(v) < 0) return -1; + + avctx->coded_width = avctx->width; + avctx->coded_height = avctx->height; + if (avctx->codec_id == CODEC_ID_WMV3) + { + int count = 0; + + // looks like WMV3 has a sequence header stored in the extradata + // advanced sequence header may be before the first frame + // the last byte of the extradata is a version number, 1 for the + // samples we can decode + + init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8); + + if (decode_sequence_header(avctx, &gb) < 0) + return -1; + + count = avctx->extradata_size*8 - get_bits_count(&gb); + if (count>0) + { + av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n", + count, get_bits(&gb, count)); + } + else if (count < 0) + { + av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count); + } + } + avctx->has_b_frames= !!(avctx->max_b_frames); + + s->mb_width = (avctx->coded_width+15)>>4; + s->mb_height = (avctx->coded_height+15)>>4; + + /* Allocate mb bitplanes */ + v->mv_type_mb_plane = av_malloc(s->mb_stride * s->mb_height); + v->direct_mb_plane = av_malloc(s->mb_stride * s->mb_height); + + /* allocate block type info in that way so it could be used with s->block_index[] */ + v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2); + v->mb_type[0] = v->mb_type_base + s->b8_stride + 1; + v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1; + v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1); + + /* Init coded blocks info */ + if (v->profile == PROFILE_ADVANCED) + { +// if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0) +// return -1; +// if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0) +// return -1; + } + + return 0; +} + + +/** Decode a VC1/WMV3 frame + * @todo TODO: Handle VC-1 IDUs (Transport level?) + * @warning Initial try at using MpegEncContext stuff + */ +static int vc1_decode_frame(AVCodecContext *avctx, + void *data, int *data_size, + uint8_t *buf, int buf_size) +{ + VC1Context *v = avctx->priv_data; + MpegEncContext *s = &v->s; + AVFrame *pict = data; + + /* no supplementary picture */ + if (buf_size == 0) { + /* special case for last picture */ + if (s->low_delay==0 && s->next_picture_ptr) { + *pict= *(AVFrame*)s->next_picture_ptr; + s->next_picture_ptr= NULL; + + *data_size = sizeof(AVFrame); + } + + return 0; + } + + //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there + if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){ + int i= ff_find_unused_picture(s, 0); + s->current_picture_ptr= &s->picture[i]; + } + + avctx->has_b_frames= !s->low_delay; + + init_get_bits(&s->gb, buf, buf_size*8); + // do parse frame header + if(vc1_parse_frame_header(v, &s->gb) == -1) + return -1; + +// if(s->pict_type != I_TYPE && s->pict_type != P_TYPE)return -1; + + // for hurry_up==5 + s->current_picture.pict_type= s->pict_type; + s->current_picture.key_frame= s->pict_type == I_TYPE; + + /* skip B-frames if we don't have reference frames */ + if(s->last_picture_ptr==NULL && (s->pict_type==B_TYPE || s->dropable)) return -1;//buf_size; + /* skip b frames if we are in a hurry */ + if(avctx->hurry_up && s->pict_type==B_TYPE) return -1;//buf_size; + if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==B_TYPE) + || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=I_TYPE) + || avctx->skip_frame >= AVDISCARD_ALL) + return buf_size; + /* skip everything if we are in a hurry>=5 */ + if(avctx->hurry_up>=5) return -1;//buf_size; + + if(s->next_p_frame_damaged){ + if(s->pict_type==B_TYPE) + return buf_size; + else + s->next_p_frame_damaged=0; + } + + if(MPV_frame_start(s, avctx) < 0) + return -1; + + ff_er_frame_start(s); + + v->bits = buf_size * 8; + vc1_decode_blocks(v); +//av_log(s->avctx, AV_LOG_INFO, "Consumed %i/%i bits\n", get_bits_count(&s->gb), buf_size*8); +// if(get_bits_count(&s->gb) > buf_size * 8) +// return -1; + ff_er_frame_end(s); + + MPV_frame_end(s); + +assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type); +assert(s->current_picture.pict_type == s->pict_type); + if (s->pict_type == B_TYPE || s->low_delay) { + *pict= *(AVFrame*)s->current_picture_ptr; + } else if (s->last_picture_ptr != NULL) { + *pict= *(AVFrame*)s->last_picture_ptr; + } + + if(s->last_picture_ptr || s->low_delay){ + *data_size = sizeof(AVFrame); + ff_print_debug_info(s, pict); + } + + /* Return the Picture timestamp as the frame number */ + /* we substract 1 because it is added on utils.c */ + avctx->frame_number = s->picture_number - 1; + + return buf_size; +} + + +/** Close a VC1/WMV3 decoder + * @warning Initial try at using MpegEncContext stuff + */ +static int vc1_decode_end(AVCodecContext *avctx) +{ + VC1Context *v = avctx->priv_data; + + av_freep(&v->hrd_rate); + av_freep(&v->hrd_buffer); + MPV_common_end(&v->s); + av_freep(&v->mv_type_mb_plane); + av_freep(&v->direct_mb_plane); + av_freep(&v->mb_type_base); + return 0; +} + + +AVCodec vc1_decoder = { + "vc1", + CODEC_TYPE_VIDEO, + CODEC_ID_VC1, + sizeof(VC1Context), + vc1_decode_init, + NULL, + vc1_decode_end, + vc1_decode_frame, + CODEC_CAP_DELAY, + NULL +}; + +AVCodec wmv3_decoder = { + "wmv3", + CODEC_TYPE_VIDEO, + CODEC_ID_WMV3, + sizeof(VC1Context), + vc1_decode_init, + NULL, + vc1_decode_end, + vc1_decode_frame, + CODEC_CAP_DELAY, + NULL +}; diff --git a/src/libffmpeg/libavcodec/vc1acdata.h b/src/libffmpeg/libavcodec/vc1acdata.h new file mode 100644 index 000000000..ffcc39d64 --- /dev/null +++ b/src/libffmpeg/libavcodec/vc1acdata.h @@ -0,0 +1,564 @@ +#define AC_MODES 8 + +static const int vc1_ac_sizes[AC_MODES] = { + 186, 169, 133, 149, 103, 103, 163, 175 +}; + +static const uint32_t vc1_ac_tables[AC_MODES][186][2] = { +{ +{ 0x0001, 2}, { 0x0005, 3}, { 0x000D, 4}, { 0x0012, 5}, { 0x000E, 6}, { 0x0015, 7}, +{ 0x0013, 8}, { 0x003F, 8}, { 0x004B, 9}, { 0x011F, 9}, { 0x00B8, 10}, { 0x03E3, 10}, +{ 0x0172, 11}, { 0x024D, 12}, { 0x03DA, 12}, { 0x02DD, 13}, { 0x1F55, 13}, { 0x05B9, 14}, +{ 0x3EAE, 14}, { 0x0000, 4}, { 0x0010, 5}, { 0x0008, 7}, { 0x0020, 8}, { 0x0029, 9}, +{ 0x01F4, 9}, { 0x0233, 10}, { 0x01E0, 11}, { 0x012A, 12}, { 0x03DD, 12}, { 0x050A, 13}, +{ 0x1F29, 13}, { 0x0A42, 14}, { 0x1272, 15}, { 0x1737, 15}, { 0x0003, 5}, { 0x0011, 7}, +{ 0x00C4, 8}, { 0x004B, 10}, { 0x00B4, 11}, { 0x07D4, 11}, { 0x0345, 12}, { 0x02D7, 13}, +{ 0x07BF, 13}, { 0x0938, 14}, { 0x0BBB, 14}, { 0x095E, 15}, { 0x0013, 5}, { 0x0078, 7}, +{ 0x0069, 9}, { 0x0232, 10}, { 0x0461, 11}, { 0x03EC, 12}, { 0x0520, 13}, { 0x1F2A, 13}, +{ 0x3E50, 14}, { 0x3E51, 14}, { 0x1486, 15}, { 0x000C, 6}, { 0x0024, 9}, { 0x0094, 11}, +{ 0x08C0, 12}, { 0x0F09, 14}, { 0x1EF0, 15}, { 0x003D, 6}, { 0x0053, 9}, { 0x01A0, 11}, +{ 0x02D6, 13}, { 0x0F08, 14}, { 0x0013, 7}, { 0x007C, 9}, { 0x07C1, 11}, { 0x04AC, 14}, +{ 0x001B, 7}, { 0x00A0, 10}, { 0x0344, 12}, { 0x0F79, 14}, { 0x0079, 7}, { 0x03E1, 10}, +{ 0x02D4, 13}, { 0x2306, 14}, { 0x0021, 8}, { 0x023C, 10}, { 0x0FAE, 12}, { 0x23DE, 14}, +{ 0x0035, 8}, { 0x0175, 11}, { 0x07B3, 13}, { 0x00C5, 8}, { 0x0174, 11}, { 0x0785, 13}, +{ 0x0048, 9}, { 0x01A3, 11}, { 0x049E, 13}, { 0x002C, 9}, { 0x00FA, 10}, { 0x07D6, 11}, +{ 0x0092, 10}, { 0x05CC, 13}, { 0x1EF1, 15}, { 0x00A3, 10}, { 0x03ED, 12}, { 0x093E, 14}, +{ 0x01E2, 11}, { 0x1273, 15}, { 0x07C4, 11}, { 0x1487, 15}, { 0x0291, 12}, { 0x0293, 12}, +{ 0x0F8A, 12}, { 0x0509, 13}, { 0x0508, 13}, { 0x078D, 13}, { 0x07BE, 13}, { 0x078C, 13}, +{ 0x04AE, 14}, { 0x0BBA, 14}, { 0x2307, 14}, { 0x0B9A, 14}, { 0x1736, 15}, { 0x000E, 4}, +{ 0x0045, 7}, { 0x01F3, 9}, { 0x047A, 11}, { 0x05DC, 13}, { 0x23DF, 14}, { 0x0019, 5}, +{ 0x0028, 9}, { 0x0176, 11}, { 0x049D, 13}, { 0x23DD, 14}, { 0x0030, 6}, { 0x00A2, 10}, +{ 0x02EF, 12}, { 0x05B8, 14}, { 0x003F, 6}, { 0x00A5, 10}, { 0x03DB, 12}, { 0x093F, 14}, +{ 0x0044, 7}, { 0x07CB, 11}, { 0x095F, 15}, { 0x0063, 7}, { 0x03C3, 12}, { 0x0015, 8}, +{ 0x08F6, 12}, { 0x0017, 8}, { 0x0498, 13}, { 0x002C, 8}, { 0x07B2, 13}, { 0x002F, 8}, +{ 0x1F54, 13}, { 0x008D, 8}, { 0x07BD, 13}, { 0x008E, 8}, { 0x1182, 13}, { 0x00FB, 8}, +{ 0x050B, 13}, { 0x002D, 8}, { 0x07C0, 11}, { 0x0079, 9}, { 0x1F5F, 13}, { 0x007A, 9}, +{ 0x1F56, 13}, { 0x0231, 10}, { 0x03E4, 10}, { 0x01A1, 11}, { 0x0143, 11}, { 0x01F7, 11}, +{ 0x016F, 12}, { 0x0292, 12}, { 0x02E7, 12}, { 0x016C, 12}, { 0x016D, 12}, { 0x03DC, 12}, +{ 0x0F8B, 12}, { 0x0499, 13}, { 0x03D8, 12}, { 0x078E, 13}, { 0x02D5, 13}, { 0x1F5E, 13}, +{ 0x1F2B, 13}, { 0x078F, 13}, { 0x04AD, 14}, { 0x3EAF, 14}, { 0x23DC, 14}, { 0x004A, 9} +}, +{ +{ 0x0000, 3}, { 0x0003, 4}, { 0x000B, 5}, { 0x0014, 6}, { 0x003F, 6}, { 0x005D, 7}, +{ 0x00A2, 8}, { 0x00AC, 9}, { 0x016E, 9}, { 0x020A, 10}, { 0x02E2, 10}, { 0x0432, 11}, +{ 0x05C9, 11}, { 0x0827, 12}, { 0x0B54, 12}, { 0x04E6, 13}, { 0x105F, 13}, { 0x172A, 13}, +{ 0x20B2, 14}, { 0x2D4E, 14}, { 0x39F0, 14}, { 0x4175, 15}, { 0x5A9E, 15}, { 0x0004, 4}, +{ 0x001E, 5}, { 0x0042, 7}, { 0x00B6, 8}, { 0x0173, 9}, { 0x0395, 10}, { 0x072E, 11}, +{ 0x0B94, 12}, { 0x16A4, 13}, { 0x20B3, 14}, { 0x2E45, 14}, { 0x0005, 5}, { 0x0040, 7}, +{ 0x0049, 9}, { 0x028F, 10}, { 0x05CB, 11}, { 0x048A, 13}, { 0x09DD, 14}, { 0x73E2, 15}, +{ 0x0018, 5}, { 0x0025, 8}, { 0x008A, 10}, { 0x051B, 11}, { 0x0E5F, 12}, { 0x09C9, 14}, +{ 0x139C, 15}, { 0x0029, 6}, { 0x004F, 9}, { 0x0412, 11}, { 0x048D, 13}, { 0x2E41, 14}, +{ 0x0038, 6}, { 0x010E, 9}, { 0x05A8, 11}, { 0x105C, 13}, { 0x39F2, 14}, { 0x0058, 7}, +{ 0x021F, 10}, { 0x0E7E, 12}, { 0x39FF, 14}, { 0x0023, 8}, { 0x02E3, 10}, { 0x04E5, 13}, +{ 0x2E40, 14}, { 0x00A1, 8}, { 0x05BE, 11}, { 0x09C8, 14}, { 0x0083, 8}, { 0x013A, 11}, +{ 0x1721, 13}, { 0x0044, 9}, { 0x0276, 12}, { 0x39F6, 14}, { 0x008B, 10}, { 0x04EF, 13}, +{ 0x5A9B, 15}, { 0x0208, 10}, { 0x1CFE, 13}, { 0x0399, 10}, { 0x1CB4, 13}, { 0x039E, 10}, +{ 0x39F3, 14}, { 0x05AB, 11}, { 0x73E3, 15}, { 0x0737, 11}, { 0x5A9F, 15}, { 0x082D, 12}, +{ 0x0E69, 12}, { 0x0E68, 12}, { 0x0433, 11}, { 0x0B7B, 12}, { 0x2DF8, 14}, { 0x2E56, 14}, +{ 0x2E57, 14}, { 0x39F7, 14}, { 0x51A5, 15}, { 0x0003, 3}, { 0x002A, 6}, { 0x00E4, 8}, +{ 0x028E, 10}, { 0x0735, 11}, { 0x1058, 13}, { 0x1CFA, 13}, { 0x2DF9, 14}, { 0x4174, 15}, +{ 0x0009, 4}, { 0x0054, 8}, { 0x0398, 10}, { 0x048B, 13}, { 0x139D, 15}, { 0x000D, 4}, +{ 0x00AD, 9}, { 0x0826, 12}, { 0x2D4C, 14}, { 0x0011, 5}, { 0x016B, 9}, { 0x0B7F, 12}, +{ 0x51A4, 15}, { 0x0019, 5}, { 0x021B, 10}, { 0x16FD, 13}, { 0x001D, 5}, { 0x0394, 10}, +{ 0x28D3, 14}, { 0x002B, 6}, { 0x05BC, 11}, { 0x5A9A, 15}, { 0x002F, 6}, { 0x0247, 12}, +{ 0x0010, 7}, { 0x0A35, 12}, { 0x003E, 6}, { 0x0B7A, 12}, { 0x0059, 7}, { 0x105E, 13}, +{ 0x0026, 8}, { 0x09CF, 14}, { 0x0055, 8}, { 0x1CB5, 13}, { 0x0057, 8}, { 0x0E5B, 12}, +{ 0x00A0, 8}, { 0x1468, 13}, { 0x0170, 9}, { 0x0090, 10}, { 0x01CE, 9}, { 0x021A, 10}, +{ 0x0218, 10}, { 0x0168, 9}, { 0x021E, 10}, { 0x0244, 12}, { 0x0736, 11}, { 0x0138, 11}, +{ 0x0519, 11}, { 0x0E5E, 12}, { 0x072C, 11}, { 0x0B55, 12}, { 0x09DC, 14}, { 0x20BB, 14}, +{ 0x048C, 13}, { 0x1723, 13}, { 0x2E44, 14}, { 0x16A5, 13}, { 0x0518, 11}, { 0x39FE, 14}, +{ 0x0169, 9} +}, +{ +{ 0x0001, 2}, { 0x0006, 3}, { 0x000F, 4}, { 0x0016, 5}, { 0x0020, 6}, { 0x0018, 7}, +{ 0x0008, 8}, { 0x009A, 8}, { 0x0056, 9}, { 0x013E, 9}, { 0x00F0, 10}, { 0x03A5, 10}, +{ 0x0077, 11}, { 0x01EF, 11}, { 0x009A, 12}, { 0x005D, 13}, { 0x0001, 4}, { 0x0011, 5}, +{ 0x0002, 7}, { 0x000B, 8}, { 0x0012, 9}, { 0x01D6, 9}, { 0x027E, 10}, { 0x0191, 11}, +{ 0x00EA, 12}, { 0x03DC, 12}, { 0x013B, 13}, { 0x0004, 5}, { 0x0014, 7}, { 0x009E, 8}, +{ 0x0009, 10}, { 0x01AC, 11}, { 0x01E2, 11}, { 0x03CA, 12}, { 0x005F, 13}, { 0x0017, 5}, +{ 0x004E, 7}, { 0x005E, 9}, { 0x00F3, 10}, { 0x01AD, 11}, { 0x00EC, 12}, { 0x05F0, 13}, +{ 0x000E, 6}, { 0x00E1, 8}, { 0x03A4, 10}, { 0x009C, 12}, { 0x013D, 13}, { 0x003B, 6}, +{ 0x001C, 9}, { 0x0014, 11}, { 0x09BE, 12}, { 0x0006, 7}, { 0x007A, 9}, { 0x0190, 11}, +{ 0x0137, 13}, { 0x001B, 7}, { 0x0008, 10}, { 0x075C, 11}, { 0x0071, 7}, { 0x00D7, 10}, +{ 0x09BF, 12}, { 0x0007, 8}, { 0x00AF, 10}, { 0x04CC, 11}, { 0x0034, 8}, { 0x0265, 10}, +{ 0x009F, 12}, { 0x00E0, 8}, { 0x0016, 11}, { 0x0327, 12}, { 0x0015, 9}, { 0x017D, 11}, +{ 0x0EBB, 12}, { 0x0014, 9}, { 0x00F6, 10}, { 0x01E4, 11}, { 0x00CB, 10}, { 0x099D, 12}, +{ 0x00CA, 10}, { 0x02FC, 12}, { 0x017F, 11}, { 0x04CD, 11}, { 0x02FD, 12}, { 0x04FE, 11}, +{ 0x013A, 13}, { 0x000A, 4}, { 0x0042, 7}, { 0x01D3, 9}, { 0x04DD, 11}, { 0x0012, 5}, +{ 0x00E8, 8}, { 0x004C, 11}, { 0x0136, 13}, { 0x0039, 6}, { 0x0264, 10}, { 0x0EBA, 12}, +{ 0x0000, 7}, { 0x00AE, 10}, { 0x099C, 12}, { 0x001F, 7}, { 0x04DE, 11}, { 0x0043, 7}, +{ 0x04DC, 11}, { 0x0003, 8}, { 0x03CB, 12}, { 0x0006, 8}, { 0x099E, 12}, { 0x002A, 8}, +{ 0x05F1, 13}, { 0x000F, 8}, { 0x09FE, 12}, { 0x0033, 8}, { 0x09FF, 12}, { 0x0098, 8}, +{ 0x099F, 12}, { 0x00EA, 8}, { 0x013C, 13}, { 0x002E, 8}, { 0x0192, 11}, { 0x0136, 9}, +{ 0x006A, 9}, { 0x0015, 11}, { 0x03AF, 10}, { 0x01E3, 11}, { 0x0074, 11}, { 0x00EB, 12}, +{ 0x02F9, 12}, { 0x005C, 13}, { 0x00ED, 12}, { 0x03DD, 12}, { 0x0326, 12}, { 0x005E, 13}, +{ 0x0016, 7} +}, +{ +{ 0x0004, 3}, { 0x0014, 5}, { 0x0017, 7}, { 0x007F, 8}, { 0x0154, 9}, { 0x01F2, 10}, +{ 0x00BF, 11}, { 0x0065, 12}, { 0x0AAA, 12}, { 0x0630, 13}, { 0x1597, 13}, { 0x03B7, 14}, +{ 0x2B22, 14}, { 0x0BE6, 15}, { 0x000B, 4}, { 0x0037, 7}, { 0x0062, 9}, { 0x0007, 11}, +{ 0x0166, 12}, { 0x00CE, 13}, { 0x1590, 13}, { 0x05F6, 14}, { 0x0BE7, 15}, { 0x0007, 5}, +{ 0x006D, 8}, { 0x0003, 11}, { 0x031F, 12}, { 0x05F2, 14}, { 0x0002, 6}, { 0x0061, 9}, +{ 0x0055, 12}, { 0x01DF, 14}, { 0x001A, 6}, { 0x001E, 10}, { 0x0AC9, 12}, { 0x2B23, 14}, +{ 0x001E, 6}, { 0x001F, 10}, { 0x0AC3, 12}, { 0x2B2B, 14}, { 0x0006, 7}, { 0x0004, 11}, +{ 0x02F8, 13}, { 0x0019, 7}, { 0x0006, 11}, { 0x063D, 13}, { 0x0057, 7}, { 0x0182, 11}, +{ 0x2AA2, 14}, { 0x0004, 8}, { 0x0180, 11}, { 0x059C, 14}, { 0x007D, 8}, { 0x0164, 12}, +{ 0x076D, 15}, { 0x0002, 9}, { 0x018D, 11}, { 0x1581, 13}, { 0x00AD, 8}, { 0x0060, 12}, +{ 0x0C67, 14}, { 0x001C, 9}, { 0x00EE, 13}, { 0x0003, 9}, { 0x02CF, 13}, { 0x00D9, 9}, +{ 0x1580, 13}, { 0x0002, 11}, { 0x0183, 11}, { 0x0057, 12}, { 0x0061, 12}, { 0x0031, 11}, +{ 0x0066, 12}, { 0x0631, 13}, { 0x0632, 13}, { 0x00AC, 13}, { 0x031D, 12}, { 0x0076, 12}, +{ 0x003A, 11}, { 0x0165, 12}, { 0x0C66, 14}, { 0x0003, 2}, { 0x0054, 7}, { 0x02AB, 10}, +{ 0x0016, 13}, { 0x05F7, 14}, { 0x0005, 4}, { 0x00F8, 9}, { 0x0AA9, 12}, { 0x005F, 15}, +{ 0x0004, 4}, { 0x001C, 10}, { 0x1550, 13}, { 0x0004, 5}, { 0x0077, 11}, { 0x076C, 15}, +{ 0x000E, 5}, { 0x000A, 12}, { 0x000C, 5}, { 0x0562, 11}, { 0x0004, 6}, { 0x031C, 12}, +{ 0x0006, 6}, { 0x00C8, 13}, { 0x000D, 6}, { 0x01DA, 13}, { 0x0007, 6}, { 0x00C9, 13}, +{ 0x0001, 7}, { 0x002E, 14}, { 0x0014, 7}, { 0x1596, 13}, { 0x000A, 7}, { 0x0AC2, 12}, +{ 0x0016, 7}, { 0x015B, 14}, { 0x0015, 7}, { 0x015A, 14}, { 0x000F, 8}, { 0x005E, 15}, +{ 0x007E, 8}, { 0x00AB, 8}, { 0x002D, 9}, { 0x00D8, 9}, { 0x000B, 9}, { 0x0014, 10}, +{ 0x02B3, 10}, { 0x01F3, 10}, { 0x003A, 10}, { 0x0000, 10}, { 0x0058, 10}, { 0x002E, 9}, +{ 0x005E, 10}, { 0x0563, 11}, { 0x00EC, 12}, { 0x0054, 12}, { 0x0AC1, 12}, { 0x1556, 13}, +{ 0x02FA, 13}, { 0x0181, 11}, { 0x1557, 13}, { 0x059D, 14}, { 0x2AA3, 14}, { 0x2B2A, 14}, +{ 0x01DE, 14}, { 0x063C, 13}, { 0x00CF, 13}, { 0x1594, 13}, { 0x000D, 9} +}, +{ +{ 0x0002, 2}, { 0x0006, 3}, { 0x000F, 4}, { 0x000D, 5}, { 0x000C, 5}, { 0x0015, 6}, +{ 0x0013, 6}, { 0x0012, 6}, { 0x0017, 7}, { 0x001F, 8}, { 0x001E, 8}, { 0x001D, 8}, +{ 0x0025, 9}, { 0x0024, 9}, { 0x0023, 9}, { 0x0021, 9}, { 0x0021, 10}, { 0x0020, 10}, +{ 0x000F, 10}, { 0x000E, 10}, { 0x0007, 11}, { 0x0006, 11}, { 0x0020, 11}, { 0x0021, 11}, +{ 0x0050, 12}, { 0x0051, 12}, { 0x0052, 12}, { 0x000E, 4}, { 0x0014, 6}, { 0x0016, 7}, +{ 0x001C, 8}, { 0x0020, 9}, { 0x001F, 9}, { 0x000D, 10}, { 0x0022, 11}, { 0x0053, 12}, +{ 0x0055, 12}, { 0x000B, 5}, { 0x0015, 7}, { 0x001E, 9}, { 0x000C, 10}, { 0x0056, 12}, +{ 0x0011, 6}, { 0x001B, 8}, { 0x001D, 9}, { 0x000B, 10}, { 0x0010, 6}, { 0x0022, 9}, +{ 0x000A, 10}, { 0x000D, 6}, { 0x001C, 9}, { 0x0008, 10}, { 0x0012, 7}, { 0x001B, 9}, +{ 0x0054, 12}, { 0x0014, 7}, { 0x001A, 9}, { 0x0057, 12}, { 0x0019, 8}, { 0x0009, 10}, +{ 0x0018, 8}, { 0x0023, 11}, { 0x0017, 8}, { 0x0019, 9}, { 0x0018, 9}, { 0x0007, 10}, +{ 0x0058, 12}, { 0x0007, 4}, { 0x000C, 6}, { 0x0016, 8}, { 0x0017, 9}, { 0x0006, 10}, +{ 0x0005, 11}, { 0x0004, 11}, { 0x0059, 12}, { 0x000F, 6}, { 0x0016, 9}, { 0x0005, 10}, +{ 0x000E, 6}, { 0x0004, 10}, { 0x0011, 7}, { 0x0024, 11}, { 0x0010, 7}, { 0x0025, 11}, +{ 0x0013, 7}, { 0x005A, 12}, { 0x0015, 8}, { 0x005B, 12}, { 0x0014, 8}, { 0x0013, 8}, +{ 0x001A, 8}, { 0x0015, 9}, { 0x0014, 9}, { 0x0013, 9}, { 0x0012, 9}, { 0x0011, 9}, +{ 0x0026, 11}, { 0x0027, 11}, { 0x005C, 12}, { 0x005D, 12}, { 0x005E, 12}, { 0x005F, 12}, +{ 0x0003, 7} +}, +{ +{ 0x0002, 2}, { 0x000F, 4}, { 0x0015, 6}, { 0x0017, 7}, { 0x001F, 8}, { 0x0025, 9}, +{ 0x0024, 9}, { 0x0021, 10}, { 0x0020, 10}, { 0x0007, 11}, { 0x0006, 11}, { 0x0020, 11}, +{ 0x0006, 3}, { 0x0014, 6}, { 0x001E, 8}, { 0x000F, 10}, { 0x0021, 11}, { 0x0050, 12}, +{ 0x000E, 4}, { 0x001D, 8}, { 0x000E, 10}, { 0x0051, 12}, { 0x000D, 5}, { 0x0023, 9}, +{ 0x000D, 10}, { 0x000C, 5}, { 0x0022, 9}, { 0x0052, 12}, { 0x000B, 5}, { 0x000C, 10}, +{ 0x0053, 12}, { 0x0013, 6}, { 0x000B, 10}, { 0x0054, 12}, { 0x0012, 6}, { 0x000A, 10}, +{ 0x0011, 6}, { 0x0009, 10}, { 0x0010, 6}, { 0x0008, 10}, { 0x0016, 7}, { 0x0055, 12}, +{ 0x0015, 7}, { 0x0014, 7}, { 0x001C, 8}, { 0x001B, 8}, { 0x0021, 9}, { 0x0020, 9}, +{ 0x001F, 9}, { 0x001E, 9}, { 0x001D, 9}, { 0x001C, 9}, { 0x001B, 9}, { 0x001A, 9}, +{ 0x0022, 11}, { 0x0023, 11}, { 0x0056, 12}, { 0x0057, 12}, { 0x0007, 4}, { 0x0019, 9}, +{ 0x0005, 11}, { 0x000F, 6}, { 0x0004, 11}, { 0x000E, 6}, { 0x000D, 6}, { 0x000C, 6}, +{ 0x0013, 7}, { 0x0012, 7}, { 0x0011, 7}, { 0x0010, 7}, { 0x001A, 8}, { 0x0019, 8}, +{ 0x0018, 8}, { 0x0017, 8}, { 0x0016, 8}, { 0x0015, 8}, { 0x0014, 8}, { 0x0013, 8}, +{ 0x0018, 9}, { 0x0017, 9}, { 0x0016, 9}, { 0x0015, 9}, { 0x0014, 9}, { 0x0013, 9}, +{ 0x0012, 9}, { 0x0011, 9}, { 0x0007, 10}, { 0x0006, 10}, { 0x0005, 10}, { 0x0004, 10}, +{ 0x0024, 11}, { 0x0025, 11}, { 0x0026, 11}, { 0x0027, 11}, { 0x0058, 12}, { 0x0059, 12}, +{ 0x005A, 12}, { 0x005B, 12}, { 0x005C, 12}, { 0x005D, 12}, { 0x005E, 12}, { 0x005F, 12}, +{ 0x0003, 7} +}, +{ +{ 0x0000, 2}, { 0x0003, 3}, { 0x000D, 4}, { 0x0005, 4}, { 0x001C, 5}, { 0x0016, 5}, +{ 0x003F, 6}, { 0x003A, 6}, { 0x002E, 6}, { 0x0022, 6}, { 0x007B, 7}, { 0x0067, 7}, +{ 0x005F, 7}, { 0x0047, 7}, { 0x0026, 7}, { 0x00EF, 8}, { 0x00CD, 8}, { 0x00C1, 8}, +{ 0x00A9, 8}, { 0x004F, 8}, { 0x01F2, 9}, { 0x01DD, 9}, { 0x0199, 9}, { 0x0185, 9}, +{ 0x015D, 9}, { 0x011B, 9}, { 0x03EF, 10}, { 0x03E1, 10}, { 0x03C8, 10}, { 0x0331, 10}, +{ 0x0303, 10}, { 0x02F1, 10}, { 0x02A0, 10}, { 0x0233, 10}, { 0x0126, 10}, { 0x07C0, 11}, +{ 0x076F, 11}, { 0x076C, 11}, { 0x0661, 11}, { 0x0604, 11}, { 0x0572, 11}, { 0x0551, 11}, +{ 0x046A, 11}, { 0x0274, 11}, { 0x0F27, 12}, { 0x0F24, 12}, { 0x0EDB, 12}, { 0x0C8E, 12}, +{ 0x0C0B, 12}, { 0x0C0A, 12}, { 0x0AE3, 12}, { 0x08D6, 12}, { 0x0490, 12}, { 0x0495, 12}, +{ 0x1F19, 13}, { 0x1DB5, 13}, { 0x0009, 4}, { 0x0010, 5}, { 0x0029, 6}, { 0x0062, 7}, +{ 0x00F3, 8}, { 0x00AD, 8}, { 0x01E5, 9}, { 0x0179, 9}, { 0x009C, 9}, { 0x03B1, 10}, +{ 0x02AE, 10}, { 0x0127, 10}, { 0x076E, 11}, { 0x0570, 11}, { 0x0275, 11}, { 0x0F25, 12}, +{ 0x0EC0, 12}, { 0x0AA0, 12}, { 0x08D7, 12}, { 0x1E4C, 13}, { 0x0008, 5}, { 0x0063, 7}, +{ 0x00AF, 8}, { 0x017B, 9}, { 0x03B3, 10}, { 0x07DD, 11}, { 0x0640, 11}, { 0x0F8D, 12}, +{ 0x0BC1, 12}, { 0x0491, 12}, { 0x0028, 6}, { 0x00C3, 8}, { 0x0151, 9}, { 0x02A1, 10}, +{ 0x0573, 11}, { 0x0EC3, 12}, { 0x1F35, 13}, { 0x0065, 7}, { 0x01DA, 9}, { 0x02AF, 10}, +{ 0x0277, 11}, { 0x08C9, 12}, { 0x1781, 13}, { 0x0025, 7}, { 0x0118, 9}, { 0x0646, 11}, +{ 0x0AA6, 12}, { 0x1780, 13}, { 0x00C9, 8}, { 0x0321, 10}, { 0x0F9B, 12}, { 0x191E, 13}, +{ 0x0048, 8}, { 0x07CC, 11}, { 0x0AA1, 12}, { 0x0180, 9}, { 0x0465, 11}, { 0x1905, 13}, +{ 0x03E2, 10}, { 0x0EC1, 12}, { 0x3C9B, 14}, { 0x02F4, 10}, { 0x08C8, 12}, { 0x07C1, 11}, +{ 0x0928, 13}, { 0x05E1, 11}, { 0x320D, 14}, { 0x0EC2, 12}, { 0x6418, 15}, { 0x1F34, 13}, +{ 0x0078, 7}, { 0x0155, 9}, { 0x0552, 11}, { 0x191F, 13}, { 0x00FA, 8}, { 0x07DC, 11}, +{ 0x1907, 13}, { 0x00AC, 8}, { 0x0249, 11}, { 0x13B1, 14}, { 0x01F6, 9}, { 0x0AE2, 12}, +{ 0x01DC, 9}, { 0x04ED, 12}, { 0x0184, 9}, { 0x1904, 13}, { 0x0156, 9}, { 0x09D9, 13}, +{ 0x03E7, 10}, { 0x0929, 13}, { 0x03B2, 10}, { 0x3B68, 14}, { 0x02F5, 10}, { 0x13B0, 14}, +{ 0x0322, 10}, { 0x3B69, 14}, { 0x0234, 10}, { 0x7935, 15}, { 0x07C7, 11}, { 0xC833, 16}, +{ 0x0660, 11}, { 0x7934, 15}, { 0x024B, 11}, { 0xC832, 16}, { 0x0AA7, 12}, { 0x1F18, 13}, +{ 0x007A, 7} +}, +{ +{ 0x0002, 2}, { 0x0000, 3}, { 0x001E, 5}, { 0x0004, 5}, { 0x0012, 6}, { 0x0070, 7}, +{ 0x001A, 7}, { 0x005F, 8}, { 0x0047, 8}, { 0x01D3, 9}, { 0x00B5, 9}, { 0x0057, 9}, +{ 0x03B5, 10}, { 0x016D, 10}, { 0x0162, 10}, { 0x07CE, 11}, { 0x0719, 11}, { 0x0691, 11}, +{ 0x02C6, 11}, { 0x0156, 11}, { 0x0F92, 12}, { 0x0D2E, 12}, { 0x0D20, 12}, { 0x059E, 12}, +{ 0x0468, 12}, { 0x02A6, 12}, { 0x1DA2, 13}, { 0x1C60, 13}, { 0x1A43, 13}, { 0x0B1D, 13}, +{ 0x08C0, 13}, { 0x055D, 13}, { 0x0003, 3}, { 0x000A, 5}, { 0x0077, 7}, { 0x00E5, 8}, +{ 0x01D9, 9}, { 0x03E5, 10}, { 0x0166, 10}, { 0x0694, 11}, { 0x0152, 11}, { 0x059F, 12}, +{ 0x1F3C, 13}, { 0x1A4B, 13}, { 0x055E, 13}, { 0x000C, 4}, { 0x007D, 7}, { 0x0044, 8}, +{ 0x03E0, 10}, { 0x0769, 11}, { 0x0E31, 12}, { 0x1F26, 13}, { 0x055C, 13}, { 0x001B, 5}, +{ 0x00E2, 8}, { 0x03A5, 10}, { 0x02C9, 11}, { 0x1F23, 13}, { 0x3B47, 14}, { 0x0007, 5}, +{ 0x01D8, 9}, { 0x02D8, 11}, { 0x1F27, 13}, { 0x3494, 14}, { 0x0035, 6}, { 0x03E1, 10}, +{ 0x059C, 12}, { 0x38C3, 14}, { 0x000C, 6}, { 0x0165, 10}, { 0x1D23, 13}, { 0x1638, 14}, +{ 0x0068, 7}, { 0x0693, 11}, { 0x3A45, 14}, { 0x0020, 7}, { 0x0F90, 12}, { 0x7CF6, 15}, +{ 0x00E8, 8}, { 0x058F, 12}, { 0x2CEF, 15}, { 0x0045, 8}, { 0x0B3A, 13}, { 0x01F1, 9}, +{ 0x3B46, 14}, { 0x01A7, 9}, { 0x1676, 14}, { 0x0056, 9}, { 0x692A, 15}, { 0x038D, 10}, +{ 0xE309, 16}, { 0x00AA, 10}, { 0x1C611, 17}, { 0x02DF, 11}, { 0xB3B9, 17}, { 0x02C8, 11}, +{ 0x38C20, 18}, { 0x01B0, 11}, { 0x16390, 18}, { 0x0F9F, 12}, { 0x16771, 18}, { 0x0ED0, 12}, +{ 0x71843, 19}, { 0x0D2A, 12}, { 0xF9E8C, 20}, { 0x0461, 12}, { 0xF9E8E, 20}, { 0x0B67, 13}, +{ 0x055F, 13}, { 0x003F, 6}, { 0x006D, 9}, { 0x0E90, 12}, { 0x054E, 13}, { 0x0013, 6}, +{ 0x0119, 10}, { 0x0B66, 13}, { 0x000B, 6}, { 0x0235, 11}, { 0x7CF5, 15}, { 0x0075, 7}, +{ 0x0D24, 12}, { 0xF9E9, 16}, { 0x002E, 7}, { 0x1F22, 13}, { 0x0021, 7}, { 0x054F, 13}, +{ 0x0014, 7}, { 0x3A44, 14}, { 0x00E4, 8}, { 0x7CF7, 15}, { 0x005E, 8}, { 0x7185, 15}, +{ 0x0037, 8}, { 0x2C73, 15}, { 0x01DB, 9}, { 0x59DD, 16}, { 0x01C7, 9}, { 0x692B, 15}, +{ 0x01A6, 9}, { 0x58E5, 16}, { 0x00B4, 9}, { 0x1F3D0, 17}, { 0x00B0, 9}, { 0xB1C9, 17}, +{ 0x03E6, 10}, { 0x16770, 18}, { 0x016E, 10}, { 0x3E7A2, 18}, { 0x011B, 10}, { 0xF9E8D, 20}, +{ 0x00D9, 10}, { 0xF9E8F, 20}, { 0x00A8, 10}, { 0x2C723, 19}, { 0x0749, 11}, { 0xE3084, 20}, +{ 0x0696, 11}, { 0x58E45, 20}, { 0x02DE, 11}, { 0xB1C88, 21}, { 0x0231, 11}, { 0x1C610A, 21}, +{ 0x01B1, 11}, { 0x71842D, 23}, { 0x0D2B, 12}, { 0x38C217, 22}, { 0x0D2F, 12}, { 0x163913, 22}, +{ 0x05B2, 12}, { 0x163912, 22}, { 0x0469, 12}, { 0x71842C, 23}, { 0x1A42, 13}, { 0x08C1, 13}, +{ 0x0073, 7} +} +}; + +/* which indexes point to last=1 entries in tables */ +static const int vc1_last_decode_table[AC_MODES] = { + 119, 99, 85, 81, 67, 58, 126, 109 +}; + +static const uint8_t vc1_index_decode_table[AC_MODES][185][2] = { +{ +{ 0, 1}, { 0, 2}, { 0, 3}, { 0, 4}, { 0, 5}, { 0, 6}, { 0, 7}, { 0, 8}, +{ 0, 9}, { 0, 10}, { 0, 11}, { 0, 12}, { 0, 13}, { 0, 14}, { 0, 15}, { 0, 16}, +{ 0, 17}, { 0, 18}, { 0, 19}, { 1, 1}, { 1, 2}, { 1, 3}, { 1, 4}, { 1, 5}, +{ 1, 6}, { 1, 7}, { 1, 8}, { 1, 9}, { 1, 10}, { 1, 11}, { 1, 12}, { 1, 13}, +{ 1, 14}, { 1, 15}, { 2, 1}, { 2, 2}, { 2, 3}, { 2, 4}, { 2, 5}, { 2, 6}, +{ 2, 7}, { 2, 8}, { 2, 9}, { 2, 10}, { 2, 11}, { 2, 12}, { 3, 1}, { 3, 2}, +{ 3, 3}, { 3, 4}, { 3, 5}, { 3, 6}, { 3, 7}, { 3, 8}, { 3, 9}, { 3, 10}, +{ 3, 11}, { 4, 1}, { 4, 2}, { 4, 3}, { 4, 4}, { 4, 5}, { 4, 6}, { 5, 1}, +{ 5, 2}, { 5, 3}, { 5, 4}, { 5, 5}, { 6, 1}, { 6, 2}, { 6, 3}, { 6, 4}, +{ 7, 1}, { 7, 2}, { 7, 3}, { 7, 4}, { 8, 1}, { 8, 2}, { 8, 3}, { 8, 4}, +{ 9, 1}, { 9, 2}, { 9, 3}, { 9, 4}, { 10, 1}, { 10, 2}, { 10, 3}, { 11, 1}, +{ 11, 2}, { 11, 3}, { 12, 1}, { 12, 2}, { 12, 3}, { 13, 1}, { 13, 2}, { 13, 3}, +{ 14, 1}, { 14, 2}, { 14, 3}, { 15, 1}, { 15, 2}, { 15, 3}, { 16, 1}, { 16, 2}, +{ 17, 1}, { 17, 2}, { 18, 1}, { 19, 1}, { 20, 1}, { 21, 1}, { 22, 1}, { 23, 1}, +{ 24, 1}, { 25, 1}, { 26, 1}, { 27, 1}, { 28, 1}, { 29, 1}, { 30, 1}, { 0, 1}, +{ 0, 2}, { 0, 3}, { 0, 4}, { 0, 5}, { 0, 6}, { 1, 1}, { 1, 2}, { 1, 3}, +{ 1, 4}, { 1, 5}, { 2, 1}, { 2, 2}, { 2, 3}, { 2, 4}, { 3, 1}, { 3, 2}, +{ 3, 3}, { 3, 4}, { 4, 1}, { 4, 2}, { 4, 3}, { 5, 1}, { 5, 2}, { 6, 1}, +{ 6, 2}, { 7, 1}, { 7, 2}, { 8, 1}, { 8, 2}, { 9, 1}, { 9, 2}, { 10, 1}, +{ 10, 2}, { 11, 1}, { 11, 2}, { 12, 1}, { 12, 2}, { 13, 1}, { 13, 2}, { 14, 1}, +{ 14, 2}, { 15, 1}, { 15, 2}, { 16, 1}, { 17, 1}, { 18, 1}, { 19, 1}, { 20, 1}, +{ 21, 1}, { 22, 1}, { 23, 1}, { 24, 1}, { 25, 1}, { 26, 1}, { 27, 1}, { 28, 1}, +{ 29, 1}, { 30, 1}, { 31, 1}, { 32, 1}, { 33, 1}, { 34, 1}, { 35, 1}, { 36, 1}, +{ 37, 1} +}, +{ +{ 0, 1}, { 0, 2}, { 0, 3}, { 0, 4}, { 0, 5}, { 0, 6}, { 0, 7}, { 0, 8}, +{ 0, 9}, { 0, 10}, { 0, 11}, { 0, 12}, { 0, 13}, { 0, 14}, { 0, 15}, { 0, 16}, +{ 0, 17}, { 0, 18}, { 0, 19}, { 0, 20}, { 0, 21}, { 0, 22}, { 0, 23}, { 1, 1}, +{ 1, 2}, { 1, 3}, { 1, 4}, { 1, 5}, { 1, 6}, { 1, 7}, { 1, 8}, { 1, 9}, +{ 1, 10}, { 1, 11}, { 2, 1}, { 2, 2}, { 2, 3}, { 2, 4}, { 2, 5}, { 2, 6}, +{ 2, 7}, { 2, 8}, { 3, 1}, { 3, 2}, { 3, 3}, { 3, 4}, { 3, 5}, { 3, 6}, +{ 3, 7}, { 4, 1}, { 4, 2}, { 4, 3}, { 4, 4}, { 4, 5}, { 5, 1}, { 5, 2}, +{ 5, 3}, { 5, 4}, { 5, 5}, { 6, 1}, { 6, 2}, { 6, 3}, { 6, 4}, { 7, 1}, +{ 7, 2}, { 7, 3}, { 7, 4}, { 8, 1}, { 8, 2}, { 8, 3}, { 9, 1}, { 9, 2}, +{ 9, 3}, { 10, 1}, { 10, 2}, { 10, 3}, { 11, 1}, { 11, 2}, { 11, 3}, { 12, 1}, +{ 12, 2}, { 13, 1}, { 13, 2}, { 14, 1}, { 14, 2}, { 15, 1}, { 15, 2}, { 16, 1}, +{ 16, 2}, { 17, 1}, { 18, 1}, { 19, 1}, { 20, 1}, { 21, 1}, { 22, 1}, { 23, 1}, +{ 24, 1}, { 25, 1}, { 26, 1}, { 0, 1}, { 0, 2}, { 0, 3}, { 0, 4}, { 0, 5}, +{ 0, 6}, { 0, 7}, { 0, 8}, { 0, 9}, { 1, 1}, { 1, 2}, { 1, 3}, { 1, 4}, +{ 1, 5}, { 2, 1}, { 2, 2}, { 2, 3}, { 2, 4}, { 3, 1}, { 3, 2}, { 3, 3}, +{ 3, 4}, { 4, 1}, { 4, 2}, { 4, 3}, { 5, 1}, { 5, 2}, { 5, 3}, { 6, 1}, +{ 6, 2}, { 6, 3}, { 7, 1}, { 7, 2}, { 8, 1}, { 8, 2}, { 9, 1}, { 9, 2}, +{ 10, 1}, { 10, 2}, { 11, 1}, { 11, 2}, { 12, 1}, { 12, 2}, { 13, 1}, { 13, 2}, +{ 14, 1}, { 14, 2}, { 15, 1}, { 16, 1}, { 17, 1}, { 18, 1}, { 19, 1}, { 20, 1}, +{ 21, 1}, { 22, 1}, { 23, 1}, { 24, 1}, { 25, 1}, { 26, 1}, { 27, 1}, { 28, 1}, +{ 29, 1}, { 30, 1}, { 31, 1}, { 32, 1}, { 33, 1}, { 34, 1}, { 35, 1}, { 36, 1} +}, +{ +{ 0, 1}, { 0, 2}, { 0, 3}, { 0, 4}, { 0, 5}, { 0, 6}, { 0, 7}, { 0, 8}, +{ 0, 9}, { 0, 10}, { 0, 11}, { 0, 12}, { 0, 13}, { 0, 14}, { 0, 15}, { 0, 16}, +{ 1, 1}, { 1, 2}, { 1, 3}, { 1, 4}, { 1, 5}, { 1, 6}, { 1, 7}, { 1, 8}, +{ 1, 9}, { 1, 10}, { 1, 11}, { 2, 1}, { 2, 2}, { 2, 3}, { 2, 4}, { 2, 5}, +{ 2, 6}, { 2, 7}, { 2, 8}, { 3, 1}, { 3, 2}, { 3, 3}, { 3, 4}, { 3, 5}, +{ 3, 6}, { 3, 7}, { 4, 1}, { 4, 2}, { 4, 3}, { 4, 4}, { 4, 5}, { 5, 1}, +{ 5, 2}, { 5, 3}, { 5, 4}, { 6, 1}, { 6, 2}, { 6, 3}, { 6, 4}, { 7, 1}, +{ 7, 2}, { 7, 3}, { 8, 1}, { 8, 2}, { 8, 3}, { 9, 1}, { 9, 2}, { 9, 3}, +{ 10, 1}, { 10, 2}, { 10, 3}, { 11, 1}, { 11, 2}, { 11, 3}, { 12, 1}, { 12, 2}, +{ 12, 3}, { 13, 1}, { 13, 2}, { 13, 3}, { 14, 1}, { 14, 2}, { 15, 1}, { 15, 2}, +{ 16, 1}, { 17, 1}, { 18, 1}, { 19, 1}, { 20, 1}, { 0, 1}, { 0, 2}, { 0, 3}, +{ 0, 4}, { 1, 1}, { 1, 2}, { 1, 3}, { 1, 4}, { 2, 1}, { 2, 2}, { 2, 3}, +{ 3, 1}, { 3, 2}, { 3, 3}, { 4, 1}, { 4, 2}, { 5, 1}, { 5, 2}, { 6, 1}, +{ 6, 2}, { 7, 1}, { 7, 2}, { 8, 1}, { 8, 2}, { 9, 1}, { 9, 2}, { 10, 1}, +{ 10, 2}, { 11, 1}, { 11, 2}, { 12, 1}, { 12, 2}, { 13, 1}, { 13, 2}, { 14, 1}, +{ 15, 1}, { 16, 1}, { 17, 1}, { 18, 1}, { 19, 1}, { 20, 1}, { 21, 1}, { 22, 1}, +{ 23, 1}, { 24, 1}, { 25, 1}, { 26, 1} +}, +{ +{ 0, 1}, { 0, 2}, { 0, 3}, { 0, 4}, { 0, 5}, { 0, 6}, { 0, 7}, { 0, 8}, +{ 0, 9}, { 0, 10}, { 0, 11}, { 0, 12}, { 0, 13}, { 0, 14}, { 1, 1}, { 1, 2}, +{ 1, 3}, { 1, 4}, { 1, 5}, { 1, 6}, { 1, 7}, { 1, 8}, { 1, 9}, { 2, 1}, +{ 2, 2}, { 2, 3}, { 2, 4}, { 2, 5}, { 3, 1}, { 3, 2}, { 3, 3}, { 3, 4}, +{ 4, 1}, { 4, 2}, { 4, 3}, { 4, 4}, { 5, 1}, { 5, 2}, { 5, 3}, { 5, 4}, +{ 6, 1}, { 6, 2}, { 6, 3}, { 7, 1}, { 7, 2}, { 7, 3}, { 8, 1}, { 8, 2}, +{ 8, 3}, { 9, 1}, { 9, 2}, { 9, 3}, { 10, 1}, { 10, 2}, { 10, 3}, { 11, 1}, +{ 11, 2}, { 11, 3}, { 12, 1}, { 12, 2}, { 12, 3}, { 13, 1}, { 13, 2}, { 14, 1}, +{ 14, 2}, { 15, 1}, { 15, 2}, { 16, 1}, { 17, 1}, { 18, 1}, { 19, 1}, { 20, 1}, +{ 21, 1}, { 22, 1}, { 23, 1}, { 24, 1}, { 25, 1}, { 26, 1}, { 27, 1}, { 28, 1}, +{ 29, 1}, { 0, 1}, { 0, 2}, { 0, 3}, { 0, 4}, { 0, 5}, { 1, 1}, { 1, 2}, +{ 1, 3}, { 1, 4}, { 2, 1}, { 2, 2}, { 2, 3}, { 3, 1}, { 3, 2}, { 3, 3}, +{ 4, 1}, { 4, 2}, { 5, 1}, { 5, 2}, { 6, 1}, { 6, 2}, { 7, 1}, { 7, 2}, +{ 8, 1}, { 8, 2}, { 9, 1}, { 9, 2}, { 10, 1}, { 10, 2}, { 11, 1}, { 11, 2}, +{ 12, 1}, { 12, 2}, { 13, 1}, { 13, 2}, { 14, 1}, { 14, 2}, { 15, 1}, { 15, 2}, +{ 16, 1}, { 17, 1}, { 18, 1}, { 19, 1}, { 20, 1}, { 21, 1}, { 22, 1}, { 23, 1}, +{ 24, 1}, { 25, 1}, { 26, 1}, { 27, 1}, { 28, 1}, { 29, 1}, { 30, 1}, { 31, 1}, +{ 32, 1}, { 33, 1}, { 34, 1}, { 35, 1}, { 36, 1}, { 37, 1}, { 38, 1}, { 39, 1}, +{ 40, 1}, { 41, 1}, { 42, 1}, { 43, 1} +}, +{ +{ 0, 1}, { 0, 2}, { 0, 3}, { 0, 4}, { 0, 5}, { 0, 6}, { 0, 7}, { 0, 8}, +{ 0, 9}, { 0, 10}, { 0, 11}, { 0, 12}, { 0, 13}, { 0, 14}, { 0, 15}, { 0, 16}, +{ 0, 17}, { 0, 18}, { 0, 19}, { 0, 20}, { 0, 21}, { 0, 22}, { 0, 23}, { 0, 24}, +{ 0, 25}, { 0, 26}, { 0, 27}, { 1, 1}, { 1, 2}, { 1, 3}, { 1, 4}, { 1, 5}, +{ 1, 6}, { 1, 7}, { 1, 8}, { 1, 9}, { 1, 10}, { 2, 1}, { 2, 2}, { 2, 3}, +{ 2, 4}, { 2, 5}, { 3, 1}, { 3, 2}, { 3, 3}, { 3, 4}, { 4, 1}, { 4, 2}, +{ 4, 3}, { 5, 1}, { 5, 2}, { 5, 3}, { 6, 1}, { 6, 2}, { 6, 3}, { 7, 1}, +{ 7, 2}, { 7, 3}, { 8, 1}, { 8, 2}, { 9, 1}, { 9, 2}, { 10, 1}, { 11, 1}, +{ 12, 1}, { 13, 1}, { 14, 1}, { 0, 1}, { 0, 2}, { 0, 3}, { 0, 4}, { 0, 5}, +{ 0, 6}, { 0, 7}, { 0, 8}, { 1, 1}, { 1, 2}, { 1, 3}, { 2, 1}, { 2, 2}, +{ 3, 1}, { 3, 2}, { 4, 1}, { 4, 2}, { 5, 1}, { 5, 2}, { 6, 1}, { 6, 2}, +{ 7, 1}, { 8, 1}, { 9, 1}, { 10, 1}, { 11, 1}, { 12, 1}, { 13, 1}, { 14, 1}, +{ 15, 1}, { 16, 1}, { 17, 1}, { 18, 1}, { 19, 1}, { 20, 1} +}, +{ +{ 0, 1}, { 0, 2}, { 0, 3}, { 0, 4}, { 0, 5}, { 0, 6}, { 0, 7}, { 0, 8}, +{ 0, 9}, { 0, 10}, { 0, 11}, { 0, 12}, { 1, 1}, { 1, 2}, { 1, 3}, { 1, 4}, +{ 1, 5}, { 1, 6}, { 2, 1}, { 2, 2}, { 2, 3}, { 2, 4}, { 3, 1}, { 3, 2}, +{ 3, 3}, { 4, 1}, { 4, 2}, { 4, 3}, { 5, 1}, { 5, 2}, { 5, 3}, { 6, 1}, +{ 6, 2}, { 6, 3}, { 7, 1}, { 7, 2}, { 8, 1}, { 8, 2}, { 9, 1}, { 9, 2}, +{ 10, 1}, { 10, 2}, { 11, 1}, { 12, 1}, { 13, 1}, { 14, 1}, { 15, 1}, { 16, 1}, +{ 17, 1}, { 18, 1}, { 19, 1}, { 20, 1}, { 21, 1}, { 22, 1}, { 23, 1}, { 24, 1}, +{ 25, 1}, { 26, 1}, { 0, 1}, { 0, 2}, { 0, 3}, { 1, 1}, { 1, 2}, { 2, 1}, +{ 3, 1}, { 4, 1}, { 5, 1}, { 6, 1}, { 7, 1}, { 8, 1}, { 9, 1}, { 10, 1}, +{ 11, 1}, { 12, 1}, { 13, 1}, { 14, 1}, { 15, 1}, { 16, 1}, { 17, 1}, { 18, 1}, +{ 19, 1}, { 20, 1}, { 21, 1}, { 22, 1}, { 23, 1}, { 24, 1}, { 25, 1}, { 26, 1}, +{ 27, 1}, { 28, 1}, { 29, 1}, { 30, 1}, { 31, 1}, { 32, 1}, { 33, 1}, { 34, 1}, +{ 35, 1}, { 36, 1}, { 37, 1}, { 38, 1}, { 39, 1}, { 40, 1} +}, +{ +{ 0, 1}, { 0, 2}, { 0, 3}, { 0, 4}, { 0, 5}, { 0, 6}, { 0, 7}, { 0, 8}, +{ 0, 9}, { 0, 10}, { 0, 11}, { 0, 12}, { 0, 13}, { 0, 14}, { 0, 15}, { 0, 16}, +{ 0, 17}, { 0, 18}, { 0, 19}, { 0, 20}, { 0, 21}, { 0, 22}, { 0, 23}, { 0, 24}, +{ 0, 25}, { 0, 26}, { 0, 27}, { 0, 28}, { 0, 29}, { 0, 30}, { 0, 31}, { 0, 32}, +{ 0, 33}, { 0, 34}, { 0, 35}, { 0, 36}, { 0, 37}, { 0, 38}, { 0, 39}, { 0, 40}, +{ 0, 41}, { 0, 42}, { 0, 43}, { 0, 44}, { 0, 45}, { 0, 46}, { 0, 47}, { 0, 48}, +{ 0, 49}, { 0, 50}, { 0, 51}, { 0, 52}, { 0, 53}, { 0, 54}, { 0, 55}, { 0, 56}, +{ 1, 1}, { 1, 2}, { 1, 3}, { 1, 4}, { 1, 5}, { 1, 6}, { 1, 7}, { 1, 8}, +{ 1, 9}, { 1, 10}, { 1, 11}, { 1, 12}, { 1, 13}, { 1, 14}, { 1, 15}, { 1, 16}, +{ 1, 17}, { 1, 18}, { 1, 19}, { 1, 20}, { 2, 1}, { 2, 2}, { 2, 3}, { 2, 4}, +{ 2, 5}, { 2, 6}, { 2, 7}, { 2, 8}, { 2, 9}, { 2, 10}, { 3, 1}, { 3, 2}, +{ 3, 3}, { 3, 4}, { 3, 5}, { 3, 6}, { 3, 7}, { 4, 1}, { 4, 2}, { 4, 3}, +{ 4, 4}, { 4, 5}, { 4, 6}, { 5, 1}, { 5, 2}, { 5, 3}, { 5, 4}, { 5, 5}, +{ 6, 1}, { 6, 2}, { 6, 3}, { 6, 4}, { 7, 1}, { 7, 2}, { 7, 3}, { 8, 1}, +{ 8, 2}, { 8, 3}, { 9, 1}, { 9, 2}, { 9, 3}, { 10, 1}, { 10, 2}, { 11, 1}, +{ 11, 2}, { 12, 1}, { 12, 2}, { 13, 1}, { 13, 2}, { 14, 1}, { 0, 1}, { 0, 2}, +{ 0, 3}, { 0, 4}, { 1, 1}, { 1, 2}, { 1, 3}, { 2, 1}, { 2, 2}, { 2, 3}, +{ 3, 1}, { 3, 2}, { 4, 1}, { 4, 2}, { 5, 1}, { 5, 2}, { 6, 1}, { 6, 2}, +{ 7, 1}, { 7, 2}, { 8, 1}, { 8, 2}, { 9, 1}, { 9, 2}, { 10, 1}, { 10, 2}, +{ 11, 1}, { 11, 2}, { 12, 1}, { 12, 2}, { 13, 1}, { 13, 2}, { 14, 1}, { 14, 2}, +{ 15, 1}, { 16, 1} +}, +{ +{ 0, 1}, { 0, 2}, { 0, 3}, { 0, 4}, { 0, 5}, { 0, 6}, { 0, 7}, { 0, 8}, +{ 0, 9}, { 0, 10}, { 0, 11}, { 0, 12}, { 0, 13}, { 0, 14}, { 0, 15}, { 0, 16}, +{ 0, 17}, { 0, 18}, { 0, 19}, { 0, 20}, { 0, 21}, { 0, 22}, { 0, 23}, { 0, 24}, +{ 0, 25}, { 0, 26}, { 0, 27}, { 0, 28}, { 0, 29}, { 0, 30}, { 0, 31}, { 0, 32}, +{ 1, 1}, { 1, 2}, { 1, 3}, { 1, 4}, { 1, 5}, { 1, 6}, { 1, 7}, { 1, 8}, +{ 1, 9}, { 1, 10}, { 1, 11}, { 1, 12}, { 1, 13}, { 2, 1}, { 2, 2}, { 2, 3}, +{ 2, 4}, { 2, 5}, { 2, 6}, { 2, 7}, { 2, 8}, { 3, 1}, { 3, 2}, { 3, 3}, +{ 3, 4}, { 3, 5}, { 3, 6}, { 4, 1}, { 4, 2}, { 4, 3}, { 4, 4}, { 4, 5}, +{ 5, 1}, { 5, 2}, { 5, 3}, { 5, 4}, { 6, 1}, { 6, 2}, { 6, 3}, { 6, 4}, +{ 7, 1}, { 7, 2}, { 7, 3}, { 8, 1}, { 8, 2}, { 8, 3}, { 9, 1}, { 9, 2}, +{ 9, 3}, { 10, 1}, { 10, 2}, { 11, 1}, { 11, 2}, { 12, 1}, { 12, 2}, { 13, 1}, +{ 13, 2}, { 14, 1}, { 14, 2}, { 15, 1}, { 15, 2}, { 16, 1}, { 16, 2}, { 17, 1}, +{ 17, 2}, { 18, 1}, { 18, 2}, { 19, 1}, { 19, 2}, { 20, 1}, { 20, 2}, { 21, 1}, +{ 21, 2}, { 22, 1}, { 22, 2}, { 23, 1}, { 24, 1}, { 0, 1}, { 0, 2}, { 0, 3}, +{ 0, 4}, { 1, 1}, { 1, 2}, { 1, 3}, { 2, 1}, { 2, 2}, { 2, 3}, { 3, 1}, +{ 3, 2}, { 3, 3}, { 4, 1}, { 4, 2}, { 5, 1}, { 5, 2}, { 6, 1}, { 6, 2}, +{ 7, 1}, { 7, 2}, { 8, 1}, { 8, 2}, { 9, 1}, { 9, 2}, { 10, 1}, { 10, 2}, +{ 11, 1}, { 11, 2}, { 12, 1}, { 12, 2}, { 13, 1}, { 13, 2}, { 14, 1}, { 14, 2}, +{ 15, 1}, { 15, 2}, { 16, 1}, { 16, 2}, { 17, 1}, { 17, 2}, { 18, 1}, { 18, 2}, +{ 19, 1}, { 19, 2}, { 20, 1}, { 20, 2}, { 21, 1}, { 21, 2}, { 22, 1}, { 22, 2}, +{ 23, 1}, { 23, 2}, { 24, 1}, { 24, 2}, { 25, 1}, { 25, 2}, { 26, 1}, { 26, 2}, +{ 27, 1}, { 27, 2}, { 28, 1}, { 28, 2}, { 29, 1}, { 30, 1} +} +}; + +static const uint8_t vc1_delta_level_table[AC_MODES][31] = { +{ + 19, 15, 12, 11, 6, 5, 4, 4, 4, 4, + 3, 3, 3, 3, 3, 3, 2, 2, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1 +}, +{ + 23, 11, 8, 7, 5, 5, 4, 4, 3, 3, + 3, 3, 2, 2, 2, 2, 2, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1 +}, +{ + 16, 11, 8, 7, 5, 4, 4, 3, 3, 3, + 3, 3, 3, 3, 2, 2, 1, 1, 1, 1, + 1 +}, +{ + 14, 9, 5, 4, 4, 4, 3, 3, 3, 3, + 3, 3, 3, 2, 2, 2, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1 +}, +{ + 27, 10, 5, 4, 3, 3, 3, 3, 2, 2, + 1, 1, 1, 1, 1 +}, +{ + 12, 6, 4, 3, 3, 3, 3, 2, 2, 2, + 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1 +}, +{ + 56, 20, 10, 7, 6, 5, 4, 3, 3, 3, + 2, 2, 2, 2, 1 +}, +{ + 32, 13, 8, 6, 5, 4, 4, 3, 3, 3, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 1, 1 +} +}; + +static const uint8_t vc1_last_delta_level_table[AC_MODES][44] = { +{ + 6, 5, 4, 4, 3, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1 +}, +{ + 9, 5, 4, 4, 3, 3, 3, 2, 2, 2, + 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1 +}, +{ + 4, 4, 3, 3, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1 +}, +{ + 5, 4, 3, 3, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1 +}, +{ + 8, 3, 2, 2, 2, 2, 2, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1 +}, +{ + 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1 +}, +{ + 4, 3, 3, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 1, 1 +}, +{ + 4, 3, 3, 3, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, + 1 +} +}; + +static const uint8_t vc1_delta_run_table[AC_MODES][57] = { +{ + -1, 30, 17, 15, 9, 5, 4, 3, 3, 3, + 3, 3, 2, 1, 1, 1, 0, 0, 0, + 0 +}, +{ + -1, 26, 16, 11, 7, 5, 3, 3, 2, 1, + 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0 +}, +{ + -1, 20, 15, 13, 6, 4, 3, 3, 2, 1, + 1, 1, 0, 0, 0, 0, 0 +}, +{ + -1, 29, 15, 12, 5, 2, 1, 1, 1, 1, + 0, 0, 0, 0, 0 +}, +{ + -1, 14, 9, 7, 3, 2, 1, 1, 1, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0 +}, +{ + -1, 26, 10, 6, 2, 1, 1, 0, 0, 0, + 0, 0, 0 +}, +{ + -1, 14, 13, 9, 6, 5, 4, 3, 2, 2, + 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0 +}, +{ + -1, 24, 22, 9, 6, 4, 3, 2, 2, 1, + 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0 +} +}; + +static const uint8_t vc1_last_delta_run_table[AC_MODES][10] = { +{ + -1, 37, 15, 4, 3, 1, 0 +}, +{ + -1, 36, 14, 6, 3, 1, 0, 0, 0, + 0 +}, +{ + -1, 26, 13, 3, 1 +}, +{ + -1, 43, 15, 3, 1, 0 +}, +{ + -1, 20, 6, 1, 0, 0, 0, 0, 0 +}, +{ + -1, 40, 1, 0 +}, +{ + -1, 16, 14, 2, 0 +}, +{ + -1, 30, 28, 3, 0 +} +}; diff --git a/src/libffmpeg/libavcodec/vc1data.h b/src/libffmpeg/libavcodec/vc1data.h new file mode 100644 index 000000000..9f9e21b4a --- /dev/null +++ b/src/libffmpeg/libavcodec/vc1data.h @@ -0,0 +1,613 @@ +/** + * @file vc1data.h + * VC-1 tables. + */ + +#ifndef VC1DATA_H +#define VC1DATA_H + +/* Denominator used for vc1_bfraction_lut */ +#define B_FRACTION_DEN 840 + +/* bfraction is fractional, we scale to the GCD 3*5*7*8 = 840 */ +const int16_t vc1_bfraction_lut[23] = { + 420 /*1/2*/, 280 /*1/3*/, 560 /*2/3*/, 210 /*1/4*/, + 630 /*3/4*/, 168 /*1/5*/, 336 /*2/5*/, + 504 /*3/5*/, 672 /*4/5*/, 140 /*1/6*/, 700 /*5/6*/, + 120 /*1/7*/, 240 /*2/7*/, 360 /*3/7*/, 480 /*4/7*/, + 600 /*5/7*/, 720 /*6/7*/, 105 /*1/8*/, 315 /*3/8*/, + 525 /*5/8*/, 735 /*7/8*/, + -1 /*inv.*/, 0 /*BI fm*/ +}; +const uint8_t vc1_bfraction_bits[23] = { + 3, 3, 3, 3, + 3, 3, 3, + 7, 7, 7, 7, + 7, 7, 7, 7, + 7, 7, 7, 7, + 7, 7, + 7, 7 +}; +const uint8_t vc1_bfraction_codes[23] = { + 0, 1, 2, 3, + 4, 5, 6, + 112, 113, 114, 115, + 116, 117, 118, 119, + 120, 121, 122, 123, + 124, 125, + 126, 127 +}; + +//Same as H.264 +static const AVRational vc1_pixel_aspect[16]={ + {0, 1}, + {1, 1}, + {12, 11}, + {10, 11}, + {16, 11}, + {40, 33}, + {24, 11}, + {20, 11}, + {32, 11}, + {80, 33}, + {18, 11}, + {15, 11}, + {64, 33}, + {160, 99}, + {0, 1}, + {0, 1} +}; + +/* BitPlane IMODE - such a small table... */ +static const uint8_t vc1_imode_codes[7] = { + 0, 2, 1, 3, 1, 2, 3 +}; +static const uint8_t vc1_imode_bits[7] = { + 4, 2, 3, 2, 4, 3, 3 +}; + +/* Normal-2 imode */ +static const uint8_t vc1_norm2_codes[4] = { + 0, 4, 5, 3 +}; +static const uint8_t vc1_norm2_bits[4] = { + 1, 3, 3, 2 +}; + +static const uint16_t vc1_norm6_codes[64] = { +0x001, 0x002, 0x003, 0x000, 0x004, 0x001, 0x002, 0x047, 0x005, 0x003, 0x004, 0x04B, 0x005, 0x04D, 0x04E, 0x30E, +0x006, 0x006, 0x007, 0x053, 0x008, 0x055, 0x056, 0x30D, 0x009, 0x059, 0x05A, 0x30C, 0x05C, 0x30B, 0x30A, 0x037, +0x007, 0x00A, 0x00B, 0x043, 0x00C, 0x045, 0x046, 0x309, 0x00D, 0x049, 0x04A, 0x308, 0x04C, 0x307, 0x306, 0x036, +0x00E, 0x051, 0x052, 0x305, 0x054, 0x304, 0x303, 0x035, 0x058, 0x302, 0x301, 0x034, 0x300, 0x033, 0x032, 0x007, +}; + +static const uint8_t vc1_norm6_bits[64] = { + 1, 4, 4, 8, 4, 8, 8, 10, 4, 8, 8, 10, 8, 10, 10, 13, + 4, 8, 8, 10, 8, 10, 10, 13, 8, 10, 10, 13, 10, 13, 13, 9, + 4, 8, 8, 10, 8, 10, 10, 13, 8, 10, 10, 13, 10, 13, 13, 9, + 8, 10, 10, 13, 10, 13, 13, 9, 10, 13, 13, 9, 13, 9, 9, 6, +}; +/* Normal-6 imode */ +static const uint8_t vc1_norm6_spec[64][5] = { +{ 0, 1, 1 }, +{ 1, 2, 4 }, +{ 2, 3, 4 }, +{ 3, 0, 8 }, +{ 4, 4, 4 }, +{ 5, 1, 8 }, +{ 6, 2, 8 }, +{ 7, 2, 5, 7, 5 }, +{ 8, 5, 4 }, +{ 9, 3, 8 }, +{10, 4, 8 }, +{11, 2, 5, 11, 5 }, +{12, 5, 8 }, +{13, 2, 5, 13, 5 }, +{14, 2, 5, 14, 5 }, +{15, 3, 5, 14, 8 }, +{16, 6, 4 }, +{17, 6, 8 }, +{18, 7, 8 }, +{19, 2, 5, 19, 5 }, +{20, 8, 8 }, +{21, 2, 5, 21, 5 }, +{22, 2, 5, 22, 5 }, +{23, 3, 5, 13, 8 }, +{24, 9, 8 }, +{25, 2, 5, 25, 5 }, +{26, 2, 5, 26, 5 }, +{27, 3, 5, 12, 8 }, +{28, 2, 5, 28, 5 }, +{29, 3, 5, 11, 8 }, +{30, 3, 5, 10, 8 }, +{31, 3, 5, 7, 4 }, +{32, 7, 4 }, +{33, 10, 8 }, +{34, 11, 8 }, +{35, 2, 5, 3, 5 }, +{36, 12, 8 }, +{37, 2, 5, 5, 5 }, +{38, 2, 5, 6, 5 }, +{39, 3, 5, 9, 8 }, +{40, 13, 8 }, +{41, 2, 5, 9, 5 }, +{42, 2, 5, 10, 5 }, +{43, 3, 5, 8, 8 }, +{44, 2, 5, 12, 5 }, +{45, 3, 5, 7, 8 }, +{46, 3, 5, 6, 8 }, +{47, 3, 5, 6, 4 }, +{48, 14, 8 }, +{49, 2, 5, 17, 5 }, +{50, 2, 5, 18, 5 }, +{51, 3, 5, 5, 8 }, +{52, 2, 5, 20, 5 }, +{53, 3, 5, 4, 8 }, +{54, 3, 5, 3, 8 }, +{55, 3, 5, 5, 4 }, +{56, 2, 5, 24, 5 }, +{57, 3, 5, 2, 8 }, +{58, 3, 5, 1, 8 }, +{59, 3, 5, 4, 4 }, +{60, 3, 5, 0, 8 }, +{61, 3, 5, 3, 4 }, +{62, 3, 5, 2, 4 }, +{63, 3, 5, 1, 1 }, +}; + +/* 4MV Block pattern VLC tables */ +static const uint8_t vc1_4mv_block_pattern_codes[4][16] = { + { 14, 58, 59, 25, 12, 26, 15, 15, 13, 24, 27, 0, 28, 1, 2, 2}, + { 8, 18, 19, 4, 20, 5, 30, 11, 21, 31, 6, 12, 7, 13, 14, 0}, + { 15, 6, 7, 2, 8, 3, 28, 9, 10, 29, 4, 11, 5, 12, 13, 0}, + { 0, 11, 12, 4, 13, 5, 30, 16, 14, 31, 6, 17, 7, 18, 19, 19} +}; +static const uint8_t vc1_4mv_block_pattern_bits[4][16] = { + { 5, 6, 6, 5, 5, 5, 5, 4, 5, 5, 5, 3, 5, 3, 3, 2}, + { 4, 5, 5, 4, 5, 4, 5, 4, 5, 5, 4, 4, 4, 4, 4, 2}, + { 4, 4, 4, 4, 4, 4, 5, 4, 4, 5, 4, 4, 4, 4, 4, 3}, + { 2, 4, 4, 4, 4, 4, 5, 5, 4, 5, 4, 5, 4, 5, 5, 4} +}; + +const uint8_t wmv3_dc_scale_table[32]={ + 0, 2, 4, 8, 8, 8, 9, 9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21 +}; + +/* P-Picture CBPCY VLC tables */ +#if 1 // Looks like original tables are not conforming to standard at all. Are they used for old WMV? +static const uint16_t vc1_cbpcy_p_codes[4][64] = { + { + 0, 6, 15, 13, 13, 11, 3, 13, 5, 8, 49, 10, 12, 114, 102, 119, + 1, 54, 96, 8, 10, 111, 5, 15, 12, 10, 2, 12, 13, 115, 53, 63, + 1, 7, 1, 7, 14, 12, 4, 14, 1, 9, 97, 11, 7, 58, 52, 62, + 4, 103, 1, 9, 11, 56, 101, 118, 4, 110, 100, 30, 2, 5, 4, 3 + }, + { + 0, 9, 1, 18, 5, 14, 237, 26, 3, 121, 3, 22, 13, 16, 6, 30, + 2, 10, 1, 20, 12, 241, 5, 28, 16, 12, 3, 24, 28, 124, 239, 247, + 1, 240, 1, 19, 18, 15, 4, 27, 1, 122, 2, 23, 1, 17, 7, 31, + 1, 11, 2, 21, 19, 246, 238, 29, 17, 13, 236, 25, 58, 63, 8, 125 + }, + { + 0, 201, 25, 231, 5, 221, 1, 3, 2, 414, 2, 241, 16, 225, 195, 492, + 2, 412, 1, 240, 7, 224, 98, 245, 1, 220, 96, 5, 9, 230, 101, 247, + 1, 102, 1, 415, 24, 3, 2, 244, 3, 54, 3, 484, 17, 114, 200, 493, + 3, 413, 1, 4, 13, 113, 99, 485, 4, 111, 194, 243, 5, 29, 26, 31 + }, + { + 0, 28, 12, 44, 3, 36, 20, 52, 2, 32, 16, 48, 8, 40, 24, 28, + 1, 30, 14, 46, 6, 38, 22, 54, 3, 34, 18, 50, 10, 42, 26, 30, + 1, 29, 13, 45, 5, 37, 21, 53, 2, 33, 17, 49, 9, 41, 25, 29, + 1, 31, 15, 47, 7, 39, 23, 55, 4, 35, 19, 51, 11, 43, 27, 31 + } +}; + +static const uint8_t vc1_cbpcy_p_bits[4][64] = { + { + 13, 13, 7, 13, 7, 13, 13, 12, 6, 13, 7, 12, 6, 8, 8, 8, + 5, 7, 8, 12, 6, 8, 13, 12, 7, 13, 13, 12, 6, 8, 7, 7, + 6, 13, 8, 12, 7, 13, 13, 12, 7, 13, 8, 12, 5, 7, 7, 7, + 6, 8, 13, 12, 6, 7, 8, 8, 5, 8, 8, 6, 3, 3, 3, 2 + }, + { + 14, 13, 8, 13, 3, 13, 8, 13, 3, 7, 8, 13, 4, 13, 13, 13, + 3, 13, 13, 13, 4, 8, 13, 13, 5, 13, 13, 13, 5, 7, 8, 8, + 3, 8, 14, 13, 5, 13, 13, 13, 4, 7, 13, 13, 6, 13, 13, 13, + 5, 13, 8, 13, 5, 8, 8, 13, 5, 13, 8, 13, 6, 6, 13, 7 + }, + { + 13, 8, 6, 8, 4, 8, 13, 12, 4, 9, 8, 8, 5, 8, 8, 9, + 5, 9, 10, 8, 4, 8, 7, 8, 6, 8, 7, 13, 4, 8, 7, 8, + 5, 7, 8, 9, 6, 13, 13, 8, 4, 6, 8, 9, 5, 7, 8, 9, + 5, 9, 9, 13, 5, 7, 7, 9, 4, 7, 8, 8, 3, 5, 5, 5 + }, + { + 9, 9, 9, 9, 2, 9, 9, 9, 2, 9, 9, 9, 9, 9, 9, 8, + 3, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 8, + 2, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 8, + 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 8 + } +}; +#else +static const uint16_t vc1_cbpcy_p_codes[4][64] = { + { + 0, 1, 1, 4, 5, 1, 12, 4, 13, 14, 10, 11, 12, 7, 13, 2, + 15, 1, 96, 1, 49, 97, 2, 100, 3, 4, 5, 101, 102, 52, 53, 4, + 6, 7, 54, 103, 8, 9, 10, 110, 11, 12, 111, 56, 114, 58, 115, 5, + 13, 7, 8, 9, 10, 11, 12, 30, 13, 14, 15, 118, 119, 62, 63, 3 + }, + { + 0, 1, 2, 1, 3, 1, 16, 17, 5, 18, 12, 19, 13, 1, 28, 58, + 1, 1, 1, 2, 3, 2, 3, 236, 237, 4, 5, 238, 6, 7, 239, 8, + 9, 240, 10, 11, 121, 122, 12, 13, 14, 15, 241, 246, 16, 17, 124, 63, + 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 247, 125 + }, + { + 0, 1, 2, 3, 2, 3, 1, 4, 5, 24, 7, 13, 16, 17, 9, 5, + 25, 1, 1, 1, 2, 3, 96, 194, 1, 2, 98, 99, 195, 200, 101, 26, + 201, 102, 412, 413, 414, 54, 220, 111, 221, 3, 224, 113, 225, 114, 230, 29, + 231, 415, 240, 4, 241, 484, 5, 243, 3, 244, 245, 485, 492, 493, 247, 31 + }, + { + 0, 1, 1, 1, 2, 2, 3, 4, 3, 5, 6, 7, 8, 9, 10, 11, + 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, + 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, + 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 28, 29, 30, 31 + } +}; +static const uint8_t vc1_cbpcy_p_bits[4][64] = { + { + 13, 6, 5, 6, 6, 7, 7, 5, 7, 7, 6, 6, 6, 5, 6, 3, + 7, 8, 8, 13, 7, 8, 13, 8, 13, 13, 13, 8, 8, 7, 7, 3, + 13, 13, 7, 8, 13, 13, 13, 8, 13, 13, 8, 7, 8, 7, 8, 3, + 13, 12, 12, 12, 12, 12, 12, 6, 12, 12, 12, 8, 8, 7, 7, 2 + }, + { + 14, 3, 3, 5, 3, 4, 5, 5, 3, 5, 4, 5, 4, 6, 5, 6, + 8, 14, 13, 8, 8, 13, 13, 8, 8, 13, 13, 8, 13, 13, 8, 13, + 13, 8, 13, 13, 7, 7, 13, 13, 13, 13, 8, 8, 13, 13, 7, 6, + 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 8, 7 + }, + { + 13, 5, 5, 5, 4, 4, 6, 4, 4, 6, 4, 5, 5, 5, 4, 3, + 6, 8, 10, 9, 8, 8, 7, 8, 13, 13, 7, 7, 8, 8, 7, 5, + 8, 7, 9, 9, 9, 6, 8, 7, 8, 13, 8, 7, 8, 7, 8, 5, + 8, 9, 8, 13, 8, 9, 13, 8, 12, 8, 8, 9, 9, 9, 8, 5 + }, + { + 9, 2, 3, 9, 2, 9, 9, 9, 2, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 8, 8, 8, 8 + } +}; +#endif + +/* MacroBlock Transform Type: 7.1.3.11, p89 + * 8x8:B + * 8x4:B:btm 8x4:B:top 8x4:B:both, + * 4x8:B:right 4x8:B:left 4x8:B:both + * 4x4:B 8x8:MB + * 8x4:MB:btm 8x4:MB:top 8x4,MB,both + * 4x8,MB,right 4x8,MB,left + * 4x4,MB */ +static const uint16_t vc1_ttmb_codes[3][16] = { + { + 0x0003, + 0x002E, 0x005F, 0x0000, + 0x0016, 0x0015, 0x0001, + 0x0004, 0x0014, + 0x02F1, 0x0179, 0x017B, + 0x0BC0, 0x0BC1, 0x05E1, + 0x017A + }, + { + 0x0006, + 0x0006, 0x0003, 0x0007, + 0x000F, 0x000E, 0x0000, + 0x0002, 0x0002, + 0x0014, 0x0011, 0x000B, + 0x0009, 0x0021, 0x0015, + 0x0020 + }, + { + 0x0006, + 0x0000, 0x000E, 0x0005, + 0x0002, 0x0003, 0x0003, + 0x000F, 0x0002, + 0x0081, 0x0021, 0x0009, + 0x0101, 0x0041, 0x0011, + 0x0100 + } +}; + +static const uint8_t vc1_ttmb_bits[3][16] = { + { + 2, + 6, 7, 2, + 5, 5, 2, + 3, 5, + 10, 9, 9, + 12, 12, 11, + 9 + }, + { + 3, + 4, 4, 4, + 4, 4, 3, + 3, 2, + 7, 7, 6, + 6, 8, 7, + 8 + }, + { + 3, + 3, 4, 5, + 3, 3, 4, + 4, 2, + 10, 8, 6, + 11, 9, 7, + 11 + } +}; + +/* TTBLK (Transform Type per Block) tables */ +static const uint8_t vc1_ttblk_codes[3][8] = { + { 0, 1, 3, 5, 16, 17, 18, 19}, + { 3, 0, 1, 2, 3, 5, 8, 9}, + { 1, 0, 1, 4, 6, 7, 10, 11} +}; +static const uint8_t vc1_ttblk_bits[3][8] = { + { 2, 2, 2, 3, 5, 5, 5, 5}, + { 2, 3, 3, 3, 3, 3, 4, 4}, + { 2, 3, 3, 3, 3, 3, 4, 4} +}; + +/* SUBBLKPAT tables, p93-94, reordered */ +static const uint8_t vc1_subblkpat_codes[3][15] = { + { 14, 12, 7, 11, 9, 26, 2, 10, 27, 8, 0, 6, 1, 15, 1}, + { 14, 0, 8, 15, 10, 4, 23, 13, 5, 9, 25, 3, 24, 22, 1}, + { 5, 6, 2, 2, 8, 0, 28, 3, 1, 3, 29, 1, 19, 18, 15} +}; +static const uint8_t vc1_subblkpat_bits[3][15] = { + { 5, 5, 5, 5, 5, 6, 4, 5, 6, 5, 4, 5, 4, 5, 1}, + { 4, 3, 4, 4, 4, 5, 5, 4, 5, 4, 5, 4, 5, 5, 2}, + { 3, 3, 4, 3, 4, 5, 5, 3, 5, 4, 5, 4, 5, 5, 4} +}; + +/* MV differential tables, p265 */ +static const uint16_t vc1_mv_diff_codes[4][73] = { + { + 0, 2, 3, 8, 576, 3, 2, 6, + 5, 577, 578, 7, 8, 9, 40, 19, + 37, 82, 21, 22, 23, 579, 580, 166, + 96, 167, 49, 194, 195, 581, 582, 583, + 292, 293, 294, 13, 2, 7, 24, 50, + 102, 295, 13, 7, 8, 18, 50, 103, + 38, 20, 21, 22, 39, 204, 103, 23, + 24, 25, 104, 410, 105, 106, 107, 108, + 109, 220, 411, 442, 222, 443, 446, 447, + 7 /* 73 elements */ + }, + { + 0, 4, 5, 3, 4, 3, 4, 5, + 20, 6, 21, 44, 45, 46, 3008, 95, + 112, 113, 57, 3009, 3010, 116, 117, 3011, + 118, 3012, 3013, 3014, 3015, 3016, 3017, 3018, + 3019, 3020, 3021, 3022, 1, 4, 15, 160, + 161, 41, 6, 11, 42, 162, 43, 119, + 56, 57, 58, 163, 236, 237, 3023, 119, + 120, 242, 122, 486, 1512, 487, 246, 494, + 1513, 495, 1514, 1515, 1516, 1517, 1518, 1519, + 31 /* 73 elements */ + }, + { + 0, 512, 513, 514, 515, 2, 3, 258, + 259, 260, 261, 262, 263, 264, 265, 266, + 267, 268, 269, 270, 271, 272, 273, 274, + 275, 276, 277, 278, 279, 280, 281, 282, + 283, 284, 285, 286, 1, 5, 287, 288, + 289, 290, 6, 7, 291, 292, 293, 294, + 295, 296, 297, 298, 299, 300, 301, 302, + 303, 304, 305, 306, 307, 308, 309, 310, + 311, 312, 313, 314, 315, 316, 317, 318, + 319 /* 73 elements */ + }, + { + 0, 1, 1, 2, 3, 4, 1, 5, + 4, 3, 5, 8, 6, 9, 10, 11, + 12, 7, 104, 14, 105, 4, 10, 15, + 11, 6, 14, 8, 106, 107, 108, 15, + 109, 9, 55, 10, 1, 2, 1, 2, + 3, 12, 6, 2, 6, 7, 28, 7, + 15, 8, 5, 18, 29, 152, 77, 24, + 25, 26, 39, 108, 13, 109, 55, 56, + 57, 116, 11, 153, 234, 235, 118, 119, + 15 /* 73 elements */ + } +}; +static const uint8_t vc1_mv_diff_bits[4][73] = { + { + 6, 7, 7, 8, 14, 6, 5, 6, 7, 14, 14, 6, 6, 6, 8, 9, + 10, 9, 7, 7, 7, 14, 14, 10, 9, 10, 8, 10, 10, 14, 14, 14, + 13, 13, 13, 6, 3, 5, 6, 8, 9, 13, 5, 4, 4, 5, 7, 9, + 6, 5, 5, 5, 6, 9, 8, 5, 5, 5, 7, 10, 7, 7, 7, 7, + 7, 8, 10, 9, 8, 9, 9, 9, 3 /* 73 elements */ + }, + { + 5, 7, 7, 6, 6, 5, 5, 6, 7, 5, 7, 8, 8, 8, 14, 9, + 9, 9, 8, 14, 14, 9, 9, 14, 9, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 2, 3, 6, 8, 8, 6, 3, 4, 6, 8, 6, 9, + 6, 6, 6, 8, 8, 8, 14, 7, 7, 8, 7, 9, 13, 9, 8, 9, + 13, 9, 13, 13, 13, 13, 13, 13, 5 /* 73 elements */ + + }, + { + 3, 12, 12, 12, 12, 3, 4, 11, 11, 11, 11, 11, 11, 11, 11, 11, + 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, + 11, 11, 11, 11, 1, 5, 11, 11, 11, 11, 4, 4, 11, 11, 11, 11, + 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, + 11, 11, 11, 11, 11, 11, 11, 11, 11 /* 73 elements */ + }, + { + 15, 11, 15, 15, 15, 15, 12, 15, 12, 11, 12, 12, 15, 12, 12, 12, + 12, 15, 15, 12, 15, 10, 11, 12, 11, 10, 11, 10, 15, 15, 15, 11, + 15, 10, 14, 10, 4, 4, 5, 7, 8, 9, 5, 3, 4, 5, 6, 8, + 5, 4, 3, 5, 6, 8, 7, 5, 5, 5, 6, 7, 9, 7, 6, 6, + 6, 7, 10, 8, 8, 8, 7, 7, 4 /* 73 elements */ + } +}; + +/* DC differentials low+hi-mo, p217 are the same as in msmpeg4data .h */ + +/* Scantables/ZZ scan are at 11.9 (p262) and 8.1.1.12 (p10) */ +static const int8_t vc1_normal_zz[64] = { + 0, 8, 1, 2, 9, 16, 24, 17, + 10, 3, 4, 11, 18, 25, 32, 40, + 33, 48, 26, 19, 12, 5, 6, 13, + 20, 27, 34, 41, 56, 49, 57, 42, + 35, 28, 21, 14, 7, 15, 22, 29, + 36, 43, 50, 58, 51, 59, 44, 37, + 30, 23, 31, 38, 45, 52, 60, 53, + 61, 46, 39, 47, 54, 62, 55, 63 +}; + +static const int8_t vc1_horizontal_zz [64] = /* Table 227 */ +{ + 0, 1, 8, 2, 3, 9, 16, 24, + 17, 10, 4, 5, 11, 18, 25, 32, + 40, 48, 33, 26, 19, 12, 6, 7, + 13, 20, 27, 34, 41, 56, 49, 57, + 42, 35, 28, 21, 14, 15, 22, 29, + 36, 43, 50, 58, 51, 44, 37, 30, + 23, 31, 38, 45, 52, 59, 60, 53, + 46, 39, 47, 54, 61, 62, 55, 63 +}; + +static const int8_t vc1_vertical_zz [64] = /* Table 228 */ +{ + 0, 8, 16, 1, 24, 32, 40, 9, + 2, 3, 10, 17, 25, 48, 56, 41, + 33, 26, 18, 11, 4, 5, 12, 19, + 27, 34, 49, 57, 50, 42, 35, 28, + 20, 13, 6, 7, 14, 21, 29, 36, + 43, 51, 58, 59, 52, 44, 37, 30, + 22, 15, 23, 31, 38, 45, 60, 53, + 46, 39, 47, 54, 61, 62, 55, 63 +}; + +static const int8_t vc1_simple_progressive_8x8_zz [64] = +/* Table 229 */ +{ + 0, 8, 1, 2, 9, 16, 24, 17, + 10, 3, 4, 11, 18, 25, 32, 40, + 48, 56, 41, 33, 26, 19, 12, 5, + 6, 13, 20, 27, 34, 49, 57, 58, + 50, 42, 35, 28, 21, 14, 7, 15, + 22, 29, 36, 43, 51, 59, 60, 52, + 44, 37, 30, 23, 31, 38, 45, 53, + 61, 62, 54, 46, 39, 47, 55, 63 +}; + +static const int8_t vc1_simple_progressive_8x4_zz [32] = /* Table 230 */ +{ + 0, 1, 2, 8, 3, 9, 10, 16, + 4, 11, 17, 24, 18, 12, 5, 19, + 25, 13, 20, 26, 27, 6, 21, 28, + 14, 22, 29, 7, 30, 15, 23, 31 +}; + +static const int8_t vc1_simple_progressive_4x8_zz [32] = /* Table 231 */ +{ + 0, 8, 1, 16, + 9, 24, 17, 2, + 32, 10, 25, 40, + 18, 48, 33, 26, + 56, 41, 34, 3, + 49, 57, 11, 42, + 19, 50, 27, 58, + 35, 43, 51, 59 +}; + +/* Table 232 */ +static const int8_t vc1_simple_progressive_4x4_zz [16] = +{ + 0, 8, 16, 1, + 9, 24, 17, 2, + 10, 18, 25, 3, + 11, 26, 19, 27 +}; + +static const int8_t vc1_adv_progressive_8x4_zz [32] = /* Table 233 */ +{ + 0, 8, 1, 16, 2, 9, 10, 3, + 24, 17, 4, 11, 18, 12, 5, 19, + 25, 13, 20, 26, 27, 6, 21, 28, + 14, 22, 29, 7, 30, 15, 23, 31 +}; + +static const int8_t vc1_adv_progressive_4x8_zz [32] = /* Table 234 */ +{ + 0, 1, 8, 2, + 9, 16, 17, 24, + 10, 32, 25, 18, + 40, 3, 33, 26, + 48, 11, 56, 41, + 34, 49, 57, 42, + 19, 50, 27, 58, + 35, 43, 51, 59 +}; + +static const int8_t vc1_adv_interlaced_8x8_zz [64] = /* Table 235 */ +{ + 0, 8, 1, 16, 24, 9, 2, 32, + 40, 48, 56, 17, 10, 3, 25, 18, + 11, 4, 33, 41, 49, 57, 26, 34, + 42, 50, 58, 19, 12, 5, 27, 20, + 13, 6, 35, 28, 21, 14, 7, 15, + 22, 29, 36, 43, 51, 59, 60, 52, + 44, 37, 30, 23, 31, 38, 45, 53, + 61, 62, 54, 46, 39, 47, 55, 63 +}; + +static const int8_t vc1_adv_interlaced_8x4_zz [32] = /* Table 236 */ +{ + 0, 8, 16, 24, 1, 9, 2, 17, + 25, 10, 3, 18, 26, 4, 11, 19, + 12, 5, 13, 20, 27, 6, 21, 28, + 14, 22, 29, 7, 30, 15, 23, 31 +}; + +static const int8_t vc1_adv_interlaced_4x8_zz [32] = /* Table 237 */ +{ + 0, 1, 2, 8, + 16, 9, 24, 17, + 10, 3, 32, 40, + 48, 56, 25, 18, + 33, 26, 41, 34, + 49, 57, 11, 42, + 19, 50, 27, 58, + 35, 43, 51, 59 +}; + +static const int8_t vc1_adv_interlaced_4x4_zz [16] = /* Table 238 */ +{ + 0, 8, 16, 24, + 1, 9, 17, 2, + 25, 10, 18, 3, + 26, 11, 19, 27 +}; + + +/* DQScale as specified in 8.1.3.9 - almost identical to 0x40000/i */ +static const int32_t vc1_dqscale[63] = { +0x40000, 0x20000, 0x15555, 0x10000, 0xCCCD, 0xAAAB, 0x9249, 0x8000, + 0x71C7, 0x6666, 0x5D17, 0x5555, 0x4EC5, 0x4925, 0x4444, 0x4000, + 0x3C3C, 0x38E4, 0x35E5, 0x3333, 0x30C3, 0x2E8C, 0x2C86, 0x2AAB, + 0x28F6, 0x2762, 0x25ED, 0x2492, 0x234F, 0x2222, 0x2108, 0x2000, + 0x1F08, 0x1E1E, 0x1D42, 0x1C72, 0x1BAD, 0x1AF3, 0x1A42, 0x199A, + 0x18FA, 0x1862, 0x17D0, 0x1746, 0x16C1, 0x1643, 0x15CA, 0x1555, + 0x14E6, 0x147B, 0x1414, 0x13B1, 0x1352, 0x12F7, 0x129E, 0x1249, + 0x11F7, 0x11A8, 0x115B, 0x1111, 0x10C9, 0x1084, 0x1000 +}; +#endif /* VC1DATA_H */ diff --git a/src/libffmpeg/libavcodec/vc1dsp.c b/src/libffmpeg/libavcodec/vc1dsp.c new file mode 100644 index 000000000..16fe31b90 --- /dev/null +++ b/src/libffmpeg/libavcodec/vc1dsp.c @@ -0,0 +1,451 @@ +/* + * VC-1 and WMV3 decoder - DSP functions + * Copyright (c) 2006 Konstantin Shishkov + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +/** +* @file vc1dsp.c + * VC-1 and WMV3 decoder + * + */ + +#include "dsputil.h" + + +/** Apply overlap transform to vertical edge +*/ +static void vc1_v_overlap_c(uint8_t* src, int stride, int rnd) +{ + int i; + int a, b, c, d; + for(i = 0; i < 8; i++) { + a = src[-2*stride]; + b = src[-stride]; + c = src[0]; + d = src[stride]; + + src[-2*stride] = clip_uint8((7*a + d + 4 - rnd) >> 3); + src[-stride] = clip_uint8((-a + 7*b + c + d + 3 + rnd) >> 3); + src[0] = clip_uint8((a + b + 7*c - d + 4 - rnd) >> 3); + src[stride] = clip_uint8((a + 7*d + 3 + rnd) >> 3); + src++; + } +} + +/** Apply overlap transform to horizontal edge +*/ +static void vc1_h_overlap_c(uint8_t* src, int stride, int rnd) +{ + int i; + int a, b, c, d; + for(i = 0; i < 8; i++) { + a = src[-2]; + b = src[-1]; + c = src[0]; + d = src[1]; + + src[-2] = clip_uint8((7*a + d + 4 - rnd) >> 3); + src[-1] = clip_uint8((-a + 7*b + c + d + 3 + rnd) >> 3); + src[0] = clip_uint8((a + b + 7*c - d + 4 - rnd) >> 3); + src[1] = clip_uint8((a + 7*d + 3 + rnd) >> 3); + src += stride; + } +} + + +/** Do inverse transform on 8x8 block +*/ +static void vc1_inv_trans_8x8_c(DCTELEM block[64]) +{ + int i; + register int t1,t2,t3,t4,t5,t6,t7,t8; + DCTELEM *src, *dst; + + src = block; + dst = block; + for(i = 0; i < 8; i++){ + t1 = 12 * (src[0] + src[4]); + t2 = 12 * (src[0] - src[4]); + t3 = 16 * src[2] + 6 * src[6]; + t4 = 6 * src[2] - 16 * src[6]; + + t5 = t1 + t3; + t6 = t2 + t4; + t7 = t2 - t4; + t8 = t1 - t3; + + t1 = 16 * src[1] + 15 * src[3] + 9 * src[5] + 4 * src[7]; + t2 = 15 * src[1] - 4 * src[3] - 16 * src[5] - 9 * src[7]; + t3 = 9 * src[1] - 16 * src[3] + 4 * src[5] + 15 * src[7]; + t4 = 4 * src[1] - 9 * src[3] + 15 * src[5] - 16 * src[7]; + + dst[0] = (t5 + t1 + 4) >> 3; + dst[1] = (t6 + t2 + 4) >> 3; + dst[2] = (t7 + t3 + 4) >> 3; + dst[3] = (t8 + t4 + 4) >> 3; + dst[4] = (t8 - t4 + 4) >> 3; + dst[5] = (t7 - t3 + 4) >> 3; + dst[6] = (t6 - t2 + 4) >> 3; + dst[7] = (t5 - t1 + 4) >> 3; + + src += 8; + dst += 8; + } + + src = block; + dst = block; + for(i = 0; i < 8; i++){ + t1 = 12 * (src[ 0] + src[32]); + t2 = 12 * (src[ 0] - src[32]); + t3 = 16 * src[16] + 6 * src[48]; + t4 = 6 * src[16] - 16 * src[48]; + + t5 = t1 + t3; + t6 = t2 + t4; + t7 = t2 - t4; + t8 = t1 - t3; + + t1 = 16 * src[ 8] + 15 * src[24] + 9 * src[40] + 4 * src[56]; + t2 = 15 * src[ 8] - 4 * src[24] - 16 * src[40] - 9 * src[56]; + t3 = 9 * src[ 8] - 16 * src[24] + 4 * src[40] + 15 * src[56]; + t4 = 4 * src[ 8] - 9 * src[24] + 15 * src[40] - 16 * src[56]; + + dst[ 0] = (t5 + t1 + 64) >> 7; + dst[ 8] = (t6 + t2 + 64) >> 7; + dst[16] = (t7 + t3 + 64) >> 7; + dst[24] = (t8 + t4 + 64) >> 7; + dst[32] = (t8 - t4 + 64 + 1) >> 7; + dst[40] = (t7 - t3 + 64 + 1) >> 7; + dst[48] = (t6 - t2 + 64 + 1) >> 7; + dst[56] = (t5 - t1 + 64 + 1) >> 7; + + src++; + dst++; + } +} + +/** Do inverse transform on 8x4 part of block +*/ +static void vc1_inv_trans_8x4_c(DCTELEM block[64], int n) +{ + int i; + register int t1,t2,t3,t4,t5,t6,t7,t8; + DCTELEM *src, *dst; + int off; + + off = n * 32; + src = block + off; + dst = block + off; + for(i = 0; i < 4; i++){ + t1 = 12 * (src[0] + src[4]); + t2 = 12 * (src[0] - src[4]); + t3 = 16 * src[2] + 6 * src[6]; + t4 = 6 * src[2] - 16 * src[6]; + + t5 = t1 + t3; + t6 = t2 + t4; + t7 = t2 - t4; + t8 = t1 - t3; + + t1 = 16 * src[1] + 15 * src[3] + 9 * src[5] + 4 * src[7]; + t2 = 15 * src[1] - 4 * src[3] - 16 * src[5] - 9 * src[7]; + t3 = 9 * src[1] - 16 * src[3] + 4 * src[5] + 15 * src[7]; + t4 = 4 * src[1] - 9 * src[3] + 15 * src[5] - 16 * src[7]; + + dst[0] = (t5 + t1 + 4) >> 3; + dst[1] = (t6 + t2 + 4) >> 3; + dst[2] = (t7 + t3 + 4) >> 3; + dst[3] = (t8 + t4 + 4) >> 3; + dst[4] = (t8 - t4 + 4) >> 3; + dst[5] = (t7 - t3 + 4) >> 3; + dst[6] = (t6 - t2 + 4) >> 3; + dst[7] = (t5 - t1 + 4) >> 3; + + src += 8; + dst += 8; + } + + src = block + off; + dst = block + off; + for(i = 0; i < 8; i++){ + t1 = 17 * (src[ 0] + src[16]); + t2 = 17 * (src[ 0] - src[16]); + t3 = 22 * src[ 8]; + t4 = 22 * src[24]; + t5 = 10 * src[ 8]; + t6 = 10 * src[24]; + + dst[ 0] = (t1 + t3 + t6 + 64) >> 7; + dst[ 8] = (t2 - t4 + t5 + 64) >> 7; + dst[16] = (t2 + t4 - t5 + 64) >> 7; + dst[24] = (t1 - t3 - t6 + 64) >> 7; + + src ++; + dst ++; + } +} + +/** Do inverse transform on 4x8 parts of block +*/ +static void vc1_inv_trans_4x8_c(DCTELEM block[64], int n) +{ + int i; + register int t1,t2,t3,t4,t5,t6,t7,t8; + DCTELEM *src, *dst; + int off; + + off = n * 4; + src = block + off; + dst = block + off; + for(i = 0; i < 8; i++){ + t1 = 17 * (src[0] + src[2]); + t2 = 17 * (src[0] - src[2]); + t3 = 22 * src[1]; + t4 = 22 * src[3]; + t5 = 10 * src[1]; + t6 = 10 * src[3]; + + dst[0] = (t1 + t3 + t6 + 4) >> 3; + dst[1] = (t2 - t4 + t5 + 4) >> 3; + dst[2] = (t2 + t4 - t5 + 4) >> 3; + dst[3] = (t1 - t3 - t6 + 4) >> 3; + + src += 8; + dst += 8; + } + + src = block + off; + dst = block + off; + for(i = 0; i < 4; i++){ + t1 = 12 * (src[ 0] + src[32]); + t2 = 12 * (src[ 0] - src[32]); + t3 = 16 * src[16] + 6 * src[48]; + t4 = 6 * src[16] - 16 * src[48]; + + t5 = t1 + t3; + t6 = t2 + t4; + t7 = t2 - t4; + t8 = t1 - t3; + + t1 = 16 * src[ 8] + 15 * src[24] + 9 * src[40] + 4 * src[56]; + t2 = 15 * src[ 8] - 4 * src[24] - 16 * src[40] - 9 * src[56]; + t3 = 9 * src[ 8] - 16 * src[24] + 4 * src[40] + 15 * src[56]; + t4 = 4 * src[ 8] - 9 * src[24] + 15 * src[40] - 16 * src[56]; + + dst[ 0] = (t5 + t1 + 64) >> 7; + dst[ 8] = (t6 + t2 + 64) >> 7; + dst[16] = (t7 + t3 + 64) >> 7; + dst[24] = (t8 + t4 + 64) >> 7; + dst[32] = (t8 - t4 + 64 + 1) >> 7; + dst[40] = (t7 - t3 + 64 + 1) >> 7; + dst[48] = (t6 - t2 + 64 + 1) >> 7; + dst[56] = (t5 - t1 + 64 + 1) >> 7; + + src++; + dst++; + } +} + +/** Do inverse transform on 4x4 part of block +*/ +static void vc1_inv_trans_4x4_c(DCTELEM block[64], int n) +{ + int i; + register int t1,t2,t3,t4,t5,t6; + DCTELEM *src, *dst; + int off; + + off = (n&1) * 4 + (n&2) * 16; + src = block + off; + dst = block + off; + for(i = 0; i < 4; i++){ + t1 = 17 * (src[0] + src[2]); + t2 = 17 * (src[0] - src[2]); + t3 = 22 * src[1]; + t4 = 22 * src[3]; + t5 = 10 * src[1]; + t6 = 10 * src[3]; + + dst[0] = (t1 + t3 + t6 + 4) >> 3; + dst[1] = (t2 - t4 + t5 + 4) >> 3; + dst[2] = (t2 + t4 - t5 + 4) >> 3; + dst[3] = (t1 - t3 - t6 + 4) >> 3; + + src += 8; + dst += 8; + } + + src = block + off; + dst = block + off; + for(i = 0; i < 4; i++){ + t1 = 17 * (src[ 0] + src[16]); + t2 = 17 * (src[ 0] - src[16]); + t3 = 22 * src[ 8]; + t4 = 22 * src[24]; + t5 = 10 * src[ 8]; + t6 = 10 * src[24]; + + dst[ 0] = (t1 + t3 + t6 + 64) >> 7; + dst[ 8] = (t2 - t4 + t5 + 64) >> 7; + dst[16] = (t2 + t4 - t5 + 64) >> 7; + dst[24] = (t1 - t3 - t6 + 64) >> 7; + + src ++; + dst ++; + } +} + +/* motion compensation functions */ + +/** Filter used to interpolate fractional pel values + */ +static always_inline int vc1_mspel_filter(const uint8_t *src, int stride, int mode, int r) +{ + switch(mode){ + case 0: //no shift + return src[0]; + case 1: // 1/4 shift + return (-4*src[-stride] + 53*src[0] + 18*src[stride] - 3*src[stride*2] + 32 - r) >> 6; + case 2: // 1/2 shift + return (-src[-stride] + 9*src[0] + 9*src[stride] - src[stride*2] + 8 - r) >> 4; + case 3: // 3/4 shift + return (-3*src[-stride] + 18*src[0] + 53*src[stride] - 4*src[stride*2] + 32 - r) >> 6; + } + return 0; //should not occur +} + +/** Function used to do motion compensation with bicubic interpolation + */ +static void vc1_mspel_mc(uint8_t *dst, const uint8_t *src, int stride, int mode, int rnd) +{ + int i, j; + uint8_t tmp[8*11], *tptr; + int m, r; + + m = (mode & 3); + r = rnd; + src -= stride; + tptr = tmp; + for(j = 0; j < 11; j++) { + for(i = 0; i < 8; i++) + tptr[i] = clip_uint8(vc1_mspel_filter(src + i, 1, m, r)); + src += stride; + tptr += 8; + } + r = 1 - rnd; + m = (mode >> 2) & 3; + + tptr = tmp + 8; + for(j = 0; j < 8; j++) { + for(i = 0; i < 8; i++) + dst[i] = clip_uint8(vc1_mspel_filter(tptr + i, 8, m, r)); + dst += stride; + tptr += 8; + } +} + +/* pixel functions - really are entry points to vc1_mspel_mc */ + +/* this one is defined in dsputil.c */ +void ff_put_vc1_mspel_mc00_c(uint8_t *dst, const uint8_t *src, int stride, int rnd); + +static void ff_put_vc1_mspel_mc10_c(uint8_t *dst, const uint8_t *src, int stride, int rnd) { + vc1_mspel_mc(dst, src, stride, 0x1, rnd); +} + +static void ff_put_vc1_mspel_mc20_c(uint8_t *dst, const uint8_t *src, int stride, int rnd) { + vc1_mspel_mc(dst, src, stride, 0x2, rnd); +} + +static void ff_put_vc1_mspel_mc30_c(uint8_t *dst, const uint8_t *src, int stride, int rnd) { + vc1_mspel_mc(dst, src, stride, 0x3, rnd); +} + +static void ff_put_vc1_mspel_mc01_c(uint8_t *dst, const uint8_t *src, int stride, int rnd) { + vc1_mspel_mc(dst, src, stride, 0x4, rnd); +} + +static void ff_put_vc1_mspel_mc11_c(uint8_t *dst, const uint8_t *src, int stride, int rnd) { + vc1_mspel_mc(dst, src, stride, 0x5, rnd); +} + +static void ff_put_vc1_mspel_mc21_c(uint8_t *dst, const uint8_t *src, int stride, int rnd) { + vc1_mspel_mc(dst, src, stride, 0x6, rnd); +} + +static void ff_put_vc1_mspel_mc31_c(uint8_t *dst, const uint8_t *src, int stride, int rnd) { + vc1_mspel_mc(dst, src, stride, 0x7, rnd); +} + +static void ff_put_vc1_mspel_mc02_c(uint8_t *dst, const uint8_t *src, int stride, int rnd) { + vc1_mspel_mc(dst, src, stride, 0x8, rnd); +} + +static void ff_put_vc1_mspel_mc12_c(uint8_t *dst, const uint8_t *src, int stride, int rnd) { + vc1_mspel_mc(dst, src, stride, 0x9, rnd); +} + +static void ff_put_vc1_mspel_mc22_c(uint8_t *dst, const uint8_t *src, int stride, int rnd) { + vc1_mspel_mc(dst, src, stride, 0xA, rnd); +} + +static void ff_put_vc1_mspel_mc32_c(uint8_t *dst, const uint8_t *src, int stride, int rnd) { + vc1_mspel_mc(dst, src, stride, 0xB, rnd); +} + +static void ff_put_vc1_mspel_mc03_c(uint8_t *dst, const uint8_t *src, int stride, int rnd) { + vc1_mspel_mc(dst, src, stride, 0xC, rnd); +} + +static void ff_put_vc1_mspel_mc13_c(uint8_t *dst, const uint8_t *src, int stride, int rnd) { + vc1_mspel_mc(dst, src, stride, 0xD, rnd); +} + +static void ff_put_vc1_mspel_mc23_c(uint8_t *dst, const uint8_t *src, int stride, int rnd) { + vc1_mspel_mc(dst, src, stride, 0xE, rnd); +} + +static void ff_put_vc1_mspel_mc33_c(uint8_t *dst, const uint8_t *src, int stride, int rnd) { + vc1_mspel_mc(dst, src, stride, 0xF, rnd); +} + +void ff_vc1dsp_init(DSPContext* dsp, AVCodecContext *avctx) { + dsp->vc1_inv_trans_8x8 = vc1_inv_trans_8x8_c; + dsp->vc1_inv_trans_4x8 = vc1_inv_trans_4x8_c; + dsp->vc1_inv_trans_8x4 = vc1_inv_trans_8x4_c; + dsp->vc1_inv_trans_4x4 = vc1_inv_trans_4x4_c; + dsp->vc1_h_overlap = vc1_h_overlap_c; + dsp->vc1_v_overlap = vc1_v_overlap_c; + + dsp->put_vc1_mspel_pixels_tab[ 0] = ff_put_vc1_mspel_mc00_c; + dsp->put_vc1_mspel_pixels_tab[ 1] = ff_put_vc1_mspel_mc10_c; + dsp->put_vc1_mspel_pixels_tab[ 2] = ff_put_vc1_mspel_mc20_c; + dsp->put_vc1_mspel_pixels_tab[ 3] = ff_put_vc1_mspel_mc30_c; + dsp->put_vc1_mspel_pixels_tab[ 4] = ff_put_vc1_mspel_mc01_c; + dsp->put_vc1_mspel_pixels_tab[ 5] = ff_put_vc1_mspel_mc11_c; + dsp->put_vc1_mspel_pixels_tab[ 6] = ff_put_vc1_mspel_mc21_c; + dsp->put_vc1_mspel_pixels_tab[ 7] = ff_put_vc1_mspel_mc31_c; + dsp->put_vc1_mspel_pixels_tab[ 8] = ff_put_vc1_mspel_mc02_c; + dsp->put_vc1_mspel_pixels_tab[ 9] = ff_put_vc1_mspel_mc12_c; + dsp->put_vc1_mspel_pixels_tab[10] = ff_put_vc1_mspel_mc22_c; + dsp->put_vc1_mspel_pixels_tab[11] = ff_put_vc1_mspel_mc32_c; + dsp->put_vc1_mspel_pixels_tab[12] = ff_put_vc1_mspel_mc03_c; + dsp->put_vc1_mspel_pixels_tab[13] = ff_put_vc1_mspel_mc13_c; + dsp->put_vc1_mspel_pixels_tab[14] = ff_put_vc1_mspel_mc23_c; + dsp->put_vc1_mspel_pixels_tab[15] = ff_put_vc1_mspel_mc33_c; +} diff --git a/src/libffmpeg/libavcodec/zmbv.c b/src/libffmpeg/libavcodec/zmbv.c new file mode 100644 index 000000000..fd8497dd3 --- /dev/null +++ b/src/libffmpeg/libavcodec/zmbv.c @@ -0,0 +1,692 @@ +/* + * Zip Motion Blocks Video (ZMBV) decoder + * Copyright (c) 2006 Konstantin Shishkov + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +/** + * @file zmbv.c + * Zip Motion Blocks Video decoder + */ + +#include <stdio.h> +#include <stdlib.h> + +#include "common.h" +#include "avcodec.h" + +#ifdef CONFIG_ZLIB +#include <zlib.h> +#endif + +#define ZMBV_KEYFRAME 1 +#define ZMBV_DELTAPAL 2 + +enum ZmbvFormat { + ZMBV_FMT_NONE = 0, + ZMBV_FMT_1BPP = 1, + ZMBV_FMT_2BPP = 2, + ZMBV_FMT_4BPP = 3, + ZMBV_FMT_8BPP = 4, + ZMBV_FMT_15BPP = 5, + ZMBV_FMT_16BPP = 6, + ZMBV_FMT_24BPP = 7, + ZMBV_FMT_32BPP = 8 +}; + +/* + * Decoder context + */ +typedef struct ZmbvContext { + AVCodecContext *avctx; + AVFrame pic; + + int bpp; + unsigned int decomp_size; + uint8_t* decomp_buf; + uint8_t pal[768]; + uint8_t *prev, *cur; + int width, height; + int fmt; + int comp; + int flags; + int bw, bh, bx, by; + int decomp_len; +#ifdef CONFIG_ZLIB + z_stream zstream; +#endif + int (*decode_intra)(struct ZmbvContext *c); + int (*decode_xor)(struct ZmbvContext *c); +} ZmbvContext; + +/** + * Decode XOR'ed frame - 8bpp version + */ + +static int zmbv_decode_xor_8(ZmbvContext *c) +{ + uint8_t *src = c->decomp_buf; + uint8_t *output, *prev; + int8_t *mvec; + int x, y; + int d, dx, dy, bw2, bh2; + int block; + int i, j; + int mx, my; + + output = c->cur; + prev = c->prev; + + if(c->flags & ZMBV_DELTAPAL){ + for(i = 0; i < 768; i++) + c->pal[i] ^= *src++; + } + + mvec = (int8_t*)src; + src += ((c->bx * c->by * 2 + 3) & ~3); + + block = 0; + for(y = 0; y < c->height; y += c->bh) { + bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y); + for(x = 0; x < c->width; x += c->bw) { + uint8_t *out, *tprev; + + d = mvec[block] & 1; + dx = mvec[block] >> 1; + dy = mvec[block + 1] >> 1; + block += 2; + + bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x); + + /* copy block - motion vectors out of bounds are used to zero blocks */ + out = output + x; + tprev = prev + x + dx + dy * c->width; + mx = x + dx; + my = y + dy; + for(j = 0; j < bh2; j++){ + if((my + j < 0) || (my + j >= c->height)) { + memset(out, 0, bw2); + } else { + for(i = 0; i < bw2; i++){ + if((mx + i < 0) || (mx + i >= c->width)) + out[i] = 0; + else + out[i] = tprev[i]; + } + } + out += c->width; + tprev += c->width; + } + + if(d) { /* apply XOR'ed difference */ + out = output + x; + for(j = 0; j < bh2; j++){ + for(i = 0; i < bw2; i++) + out[i] ^= *src++; + out += c->width; + } + } + } + output += c->width * c->bh; + prev += c->width * c->bh; + } + if(src - c->decomp_buf != c->decomp_len) + av_log(c->avctx, AV_LOG_ERROR, "Used %i of %i bytes\n", src-c->decomp_buf, c->decomp_len); + return 0; +} + +/** + * Decode XOR'ed frame - 15bpp and 16bpp version + */ + +static int zmbv_decode_xor_16(ZmbvContext *c) +{ + uint8_t *src = c->decomp_buf; + uint16_t *output, *prev; + int8_t *mvec; + int x, y; + int d, dx, dy, bw2, bh2; + int block; + int i, j; + int mx, my; + + output = (uint16_t*)c->cur; + prev = (uint16_t*)c->prev; + + mvec = (int8_t*)src; + src += ((c->bx * c->by * 2 + 3) & ~3); + + block = 0; + for(y = 0; y < c->height; y += c->bh) { + bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y); + for(x = 0; x < c->width; x += c->bw) { + uint16_t *out, *tprev; + + d = mvec[block] & 1; + dx = mvec[block] >> 1; + dy = mvec[block + 1] >> 1; + block += 2; + + bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x); + + /* copy block - motion vectors out of bounds are used to zero blocks */ + out = output + x; + tprev = prev + x + dx + dy * c->width; + mx = x + dx; + my = y + dy; + for(j = 0; j < bh2; j++){ + if((my + j < 0) || (my + j >= c->height)) { + memset(out, 0, bw2 * 2); + } else { + for(i = 0; i < bw2; i++){ + if((mx + i < 0) || (mx + i >= c->width)) + out[i] = 0; + else + out[i] = tprev[i]; + } + } + out += c->width; + tprev += c->width; + } + + if(d) { /* apply XOR'ed difference */ + out = output + x; + for(j = 0; j < bh2; j++){ + for(i = 0; i < bw2; i++) { + out[i] ^= *((uint16_t*)src); + src += 2; + } + out += c->width; + } + } + } + output += c->width * c->bh; + prev += c->width * c->bh; + } + if(src - c->decomp_buf != c->decomp_len) + av_log(c->avctx, AV_LOG_ERROR, "Used %i of %i bytes\n", src-c->decomp_buf, c->decomp_len); + return 0; +} + +#ifdef ZMBV_ENABLE_24BPP +/** + * Decode XOR'ed frame - 24bpp version + */ + +static int zmbv_decode_xor_24(ZmbvContext *c) +{ + uint8_t *src = c->decomp_buf; + uint8_t *output, *prev; + int8_t *mvec; + int x, y; + int d, dx, dy, bw2, bh2; + int block; + int i, j; + int mx, my; + int stride; + + output = c->cur; + prev = c->prev; + + stride = c->width * 3; + mvec = (int8_t*)src; + src += ((c->bx * c->by * 2 + 3) & ~3); + + block = 0; + for(y = 0; y < c->height; y += c->bh) { + bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y); + for(x = 0; x < c->width; x += c->bw) { + uint8_t *out, *tprev; + + d = mvec[block] & 1; + dx = mvec[block] >> 1; + dy = mvec[block + 1] >> 1; + block += 2; + + bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x); + + /* copy block - motion vectors out of bounds are used to zero blocks */ + out = output + x * 3; + tprev = prev + (x + dx) * 3 + dy * stride; + mx = x + dx; + my = y + dy; + for(j = 0; j < bh2; j++){ + if((my + j < 0) || (my + j >= c->height)) { + memset(out, 0, bw2 * 3); + } else { + for(i = 0; i < bw2; i++){ + if((mx + i < 0) || (mx + i >= c->width)) { + out[i * 3 + 0] = 0; + out[i * 3 + 1] = 0; + out[i * 3 + 2] = 0; + } else { + out[i * 3 + 0] = tprev[i * 3 + 0]; + out[i * 3 + 1] = tprev[i * 3 + 1]; + out[i * 3 + 2] = tprev[i * 3 + 2]; + } + } + } + out += stride; + tprev += stride; + } + + if(d) { /* apply XOR'ed difference */ + out = output + x * 3; + for(j = 0; j < bh2; j++){ + for(i = 0; i < bw2; i++) { + out[i * 3 + 0] ^= *src++; + out[i * 3 + 1] ^= *src++; + out[i * 3 + 2] ^= *src++; + } + out += stride; + } + } + } + output += stride * c->bh; + prev += stride * c->bh; + } + if(src - c->decomp_buf != c->decomp_len) + av_log(c->avctx, AV_LOG_ERROR, "Used %i of %i bytes\n", src-c->decomp_buf, c->decomp_len); + return 0; +} +#endif //ZMBV_ENABLE_24BPP + +/** + * Decode XOR'ed frame - 32bpp version + */ + +static int zmbv_decode_xor_32(ZmbvContext *c) +{ + uint8_t *src = c->decomp_buf; + uint32_t *output, *prev; + int8_t *mvec; + int x, y; + int d, dx, dy, bw2, bh2; + int block; + int i, j; + int mx, my; + + output = (uint32_t*)c->cur; + prev = (uint32_t*)c->prev; + + mvec = (int8_t*)src; + src += ((c->bx * c->by * 2 + 3) & ~3); + + block = 0; + for(y = 0; y < c->height; y += c->bh) { + bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y); + for(x = 0; x < c->width; x += c->bw) { + uint32_t *out, *tprev; + + d = mvec[block] & 1; + dx = mvec[block] >> 1; + dy = mvec[block + 1] >> 1; + block += 2; + + bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x); + + /* copy block - motion vectors out of bounds are used to zero blocks */ + out = output + x; + tprev = prev + x + dx + dy * c->width; + mx = x + dx; + my = y + dy; + for(j = 0; j < bh2; j++){ + if((my + j < 0) || (my + j >= c->height)) { + memset(out, 0, bw2 * 4); + } else { + for(i = 0; i < bw2; i++){ + if((mx + i < 0) || (mx + i >= c->width)) + out[i] = 0; + else + out[i] = tprev[i]; + } + } + out += c->width; + tprev += c->width; + } + + if(d) { /* apply XOR'ed difference */ + out = output + x; + for(j = 0; j < bh2; j++){ + for(i = 0; i < bw2; i++) { + out[i] ^= *((uint32_t*)src); + src += 4; + } + out += c->width; + } + } + } + output += c->width * c->bh; + prev += c->width * c->bh; + } + if(src - c->decomp_buf != c->decomp_len) + av_log(c->avctx, AV_LOG_ERROR, "Used %i of %i bytes\n", src-c->decomp_buf, c->decomp_len); + return 0; +} + +/** + * Decode intraframe + */ +static int zmbv_decode_intra(ZmbvContext *c) +{ + uint8_t *src = c->decomp_buf; + + /* make the palette available on the way out */ + if (c->fmt == ZMBV_FMT_8BPP) { + memcpy(c->pal, src, 768); + src += 768; + } + + memcpy(c->cur, src, c->width * c->height * (c->bpp / 8)); + return 0; +} + +static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size) +{ + ZmbvContext * const c = (ZmbvContext *)avctx->priv_data; + uint8_t *outptr; +#ifdef CONFIG_ZLIB + int zret = Z_OK; // Zlib return code +#endif + int len = buf_size; + int hi_ver, lo_ver; + + if(c->pic.data[0]) + avctx->release_buffer(avctx, &c->pic); + + c->pic.reference = 1; + c->pic.buffer_hints = FF_BUFFER_HINTS_VALID; + if(avctx->get_buffer(avctx, &c->pic) < 0){ + av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); + return -1; + } + + outptr = c->pic.data[0]; // Output image pointer + + /* parse header */ + c->flags = buf[0]; + buf++; len--; + if(c->flags & ZMBV_KEYFRAME) { + hi_ver = buf[0]; + lo_ver = buf[1]; + c->comp = buf[2]; + c->fmt = buf[3]; + c->bw = buf[4]; + c->bh = buf[5]; + + buf += 6; + len -= 6; + av_log(avctx, AV_LOG_DEBUG, "Flags=%X ver=%i.%i comp=%i fmt=%i blk=%ix%i\n",c->flags,hi_ver,lo_ver,c->comp,c->fmt,c->bw,c->bh); + if(hi_ver != 0 || lo_ver != 1) { + av_log(avctx, AV_LOG_ERROR, "Unsupported version %i.%i\n", hi_ver, lo_ver); + return -1; + } + if(c->bw == 0 || c->bh == 0) { + av_log(avctx, AV_LOG_ERROR, "Unsupported block size %ix%i\n", c->bw, c->bh); + } + if(c->comp != 0 && c->comp != 1) { + av_log(avctx, AV_LOG_ERROR, "Unsupported compression type %i\n", c->comp); + return -1; + } + + switch(c->fmt) { + case ZMBV_FMT_8BPP: + c->bpp = 8; + c->decode_intra = zmbv_decode_intra; + c->decode_xor = zmbv_decode_xor_8; + break; + case ZMBV_FMT_15BPP: + case ZMBV_FMT_16BPP: + c->bpp = 16; + c->decode_intra = zmbv_decode_intra; + c->decode_xor = zmbv_decode_xor_16; + break; +#ifdef ZMBV_ENABLE_24BPP + case ZMBV_FMT_24BPP: + c->bpp = 24; + c->decode_intra = zmbv_decode_intra; + c->decode_xor = zmbv_decode_xor_24; + break; +#endif //ZMBV_ENABLE_24BPP + case ZMBV_FMT_32BPP: + c->bpp = 32; + c->decode_intra = zmbv_decode_intra; + c->decode_xor = zmbv_decode_xor_32; + break; + default: + c->decode_intra = NULL; + c->decode_xor = NULL; + av_log(avctx, AV_LOG_ERROR, "Unsupported (for now) format %i\n", c->fmt); + return -1; + } +#ifdef CONFIG_ZLIB + zret = inflateReset(&c->zstream); + if (zret != Z_OK) { + av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret); + return -1; + } +#else + av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n"); + return -1; +#endif /* CONFIG_ZLIB */ + c->cur = av_realloc(c->cur, avctx->width * avctx->height * (c->bpp / 8)); + c->prev = av_realloc(c->prev, avctx->width * avctx->height * (c->bpp / 8)); + c->bx = (c->width + c->bw - 1) / c->bw; + c->by = (c->height+ c->bh - 1) / c->bh; + } + + if(c->decode_intra == NULL) { + av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n"); + return -1; + } + + if(c->comp == 0) { //Uncompressed data + memcpy(c->decomp_buf, buf, len); + c->decomp_size = 1; + } else { // ZLIB-compressed data +#ifdef CONFIG_ZLIB + c->zstream.total_in = c->zstream.total_out = 0; + c->zstream.next_in = buf; + c->zstream.avail_in = len; + c->zstream.next_out = c->decomp_buf; + c->zstream.avail_out = c->decomp_size; + inflate(&c->zstream, Z_FINISH); + c->decomp_len = c->zstream.total_out; +#else + av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n"); + return -1; +#endif + } + if(c->flags & ZMBV_KEYFRAME) { + c->pic.key_frame = 1; + c->pic.pict_type = FF_I_TYPE; + c->decode_intra(c); + } else { + c->pic.key_frame = 0; + c->pic.pict_type = FF_P_TYPE; + c->decode_xor(c); + } + + /* update frames */ + { + uint8_t *out, *src; + int i, j; + + out = c->pic.data[0]; + src = c->cur; + switch(c->fmt) { + case ZMBV_FMT_8BPP: + for(j = 0; j < c->height; j++) { + for(i = 0; i < c->width; i++) { + out[i * 3 + 0] = c->pal[(*src) * 3 + 0]; + out[i * 3 + 1] = c->pal[(*src) * 3 + 1]; + out[i * 3 + 2] = c->pal[(*src) * 3 + 2]; + *src++; + } + out += c->pic.linesize[0]; + } + break; + case ZMBV_FMT_15BPP: + for(j = 0; j < c->height; j++) { + for(i = 0; i < c->width; i++) { + uint16_t tmp = LE_16(src); + src += 2; + out[i * 3 + 0] = (tmp & 0x7C00) >> 7; + out[i * 3 + 1] = (tmp & 0x03E0) >> 2; + out[i * 3 + 2] = (tmp & 0x001F) << 3; + } + out += c->pic.linesize[0]; + } + break; + case ZMBV_FMT_16BPP: + for(j = 0; j < c->height; j++) { + for(i = 0; i < c->width; i++) { + uint16_t tmp = LE_16(src); + src += 2; + out[i * 3 + 0] = (tmp & 0xF800) >> 8; + out[i * 3 + 1] = (tmp & 0x07E0) >> 3; + out[i * 3 + 2] = (tmp & 0x001F) << 3; + } + out += c->pic.linesize[0]; + } + break; +#ifdef ZMBV_ENABLE_24BPP + case ZMBV_FMT_24BPP: + for(j = 0; j < c->height; j++) { + memcpy(out, src, c->width * 3); + src += c->width * 3; + out += c->pic.linesize[0]; + } + break; +#endif //ZMBV_ENABLE_24BPP + case ZMBV_FMT_32BPP: + for(j = 0; j < c->height; j++) { + for(i = 0; i < c->width; i++) { + uint32_t tmp = LE_32(src); + src += 4; + out[i * 3 + 0] = tmp >> 16; + out[i * 3 + 1] = tmp >> 8; + out[i * 3 + 2] = tmp >> 0; + } + out += c->pic.linesize[0]; + } + break; + default: + av_log(avctx, AV_LOG_ERROR, "Cannot handle format %i\n", c->fmt); + } + memcpy(c->prev, c->cur, c->width * c->height * (c->bpp / 8)); + } + *data_size = sizeof(AVFrame); + *(AVFrame*)data = c->pic; + + /* always report that the buffer was completely consumed */ + return buf_size; +} + + + +/* + * + * Init zmbv decoder + * + */ +static int decode_init(AVCodecContext *avctx) +{ + ZmbvContext * const c = (ZmbvContext *)avctx->priv_data; + int zret; // Zlib return code + + c->avctx = avctx; + avctx->has_b_frames = 0; + + c->pic.data[0] = NULL; + c->width = avctx->width; + c->height = avctx->height; + + if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) { + return 1; + } + c->bpp = avctx->bits_per_sample; + +#ifdef CONFIG_ZLIB + // Needed if zlib unused or init aborted before inflateInit + memset(&(c->zstream), 0, sizeof(z_stream)); +#else + av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n"); + return 1; +#endif + avctx->pix_fmt = PIX_FMT_RGB24; + c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64); + + /* Allocate decompression buffer */ + if (c->decomp_size) { + if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) { + av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n"); + return 1; + } + } + +#ifdef CONFIG_ZLIB + c->zstream.zalloc = Z_NULL; + c->zstream.zfree = Z_NULL; + c->zstream.opaque = Z_NULL; + zret = inflateInit(&(c->zstream)); + if (zret != Z_OK) { + av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret); + return 1; + } +#endif + + return 0; +} + + + +/* + * + * Uninit zmbv decoder + * + */ +static int decode_end(AVCodecContext *avctx) +{ + ZmbvContext * const c = (ZmbvContext *)avctx->priv_data; + + av_freep(&c->decomp_buf); + + if (c->pic.data[0]) + avctx->release_buffer(avctx, &c->pic); +#ifdef CONFIG_ZLIB + inflateEnd(&(c->zstream)); +#endif + if(c->cur) + av_freep(&c->cur); + if(c->prev) + av_freep(&c->prev); + + return 0; +} + +AVCodec zmbv_decoder = { + "zmbv", + CODEC_TYPE_VIDEO, + CODEC_ID_ZMBV, + sizeof(ZmbvContext), + decode_init, + NULL, + decode_end, + decode_frame +}; + |