diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/mace.c b/src/add-ons/media/plugins/avcodec/libavcodec/mace.c index 1beac6c40d..c789984618 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/mace.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/mace.c @@ -2,26 +2,28 @@ * MACE decoder * Copyright (c) 2002 Laszlo Torok * - * This library is free software; you can redistribute it and/or + * This file is part of FFmpeg. + * + * FFmpeg 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. + * version 2.1 of the License, or (at your option) any later version. * - * This library is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ /** * @file mace.c * MACE decoder. */ - + #include "avcodec.h" /* @@ -242,7 +244,8 @@ typedef struct MACEContext { static void chomp3(MACEContext *ctx, uint8_t val, const uint16_t tab1[], - const uint16_t tab2[][8]) + const uint16_t tab2[][8], + uint32_t numChannels) { short current; @@ -252,14 +255,15 @@ static void chomp3(MACEContext *ctx, else current+=ctx->lev; ctx->lev=current-(current >> 3); // *ctx->outPtr++=current >> 8; - *ctx->outPtr++=current; + *ctx->outPtr=current; + ctx->outPtr+=numChannels; if ( ( ctx->index += tab1[val]-(ctx->index>>5) ) < 0 ) ctx->index = 0; } /* \\\ */ /* /// "Exp1to3()" */ static void Exp1to3(MACEContext *ctx, - uint8_t *inBuffer, + const uint8_t *inBuffer, void *outBuffer, uint32_t cnt, uint32_t numChannels, @@ -281,13 +285,13 @@ static void Exp1to3(MACEContext *ctx, while (cnt>0) { pkt=inBuffer[0]; - chomp3(ctx, pkt & 7, MACEtab1, MACEtab2); - chomp3(ctx,(pkt >> 3) & 3, MACEtab3, MACEtab4); - chomp3(ctx, pkt >> 5 , MACEtab1, MACEtab2); + chomp3(ctx, pkt & 7, MACEtab1, MACEtab2, numChannels); + chomp3(ctx,(pkt >> 3) & 3, MACEtab3, MACEtab4, numChannels); + chomp3(ctx, pkt >> 5 , MACEtab1, MACEtab2, numChannels); pkt=inBuffer[1]; - chomp3(ctx, pkt & 7, MACEtab1, MACEtab2); - chomp3(ctx,(pkt >> 3) & 3, MACEtab3, MACEtab4); - chomp3(ctx, pkt >> 5 , MACEtab1, MACEtab2); + chomp3(ctx, pkt & 7, MACEtab1, MACEtab2, numChannels); + chomp3(ctx,(pkt >> 3) & 3, MACEtab3, MACEtab4, numChannels); + chomp3(ctx, pkt >> 5 , MACEtab1, MACEtab2, numChannels); inBuffer+=numChannels*2; --cnt; @@ -306,7 +310,8 @@ static void Exp1to3(MACEContext *ctx, static void chomp6(MACEContext *ctx, uint8_t val, const uint16_t tab1[], - const uint16_t tab2[][8]) + const uint16_t tab2[][8], + uint32_t numChannels) { short current; @@ -329,9 +334,10 @@ static void chomp6(MACEContext *ctx, // *ctx->outPtr++=(ctx->previous+ctx->prev2-((ctx->prev2-current) >> 2)) >> 8; // *ctx->outPtr++=(ctx->previous+current+((ctx->prev2-current) >> 2)) >> 8; - *ctx->outPtr++=(ctx->previous+ctx->prev2-((ctx->prev2-current) >> 2)); - *ctx->outPtr++=(ctx->previous+current+((ctx->prev2-current) >> 2)); - + *ctx->outPtr=(ctx->previous+ctx->prev2-((ctx->prev2-current) >> 2)); + ctx->outPtr+=numChannels; + *ctx->outPtr=(ctx->previous+current+((ctx->prev2-current) >> 2)); + ctx->outPtr+=numChannels; ctx->prev2=ctx->previous; ctx->previous=current; @@ -341,7 +347,7 @@ static void chomp6(MACEContext *ctx, /* /// "Exp1to6()" */ static void Exp1to6(MACEContext *ctx, - uint8_t *inBuffer, + const uint8_t *inBuffer, void *outBuffer, uint32_t cnt, uint32_t numChannels, @@ -366,9 +372,9 @@ static void Exp1to6(MACEContext *ctx, while (cnt>0) { pkt=*inBuffer; - chomp6(ctx, pkt >> 5 , MACEtab1, MACEtab2); - chomp6(ctx,(pkt >> 3) & 3, MACEtab3, MACEtab4); - chomp6(ctx, pkt & 7, MACEtab1, MACEtab2); + chomp6(ctx, pkt >> 5 , MACEtab1, MACEtab2, numChannels); + chomp6(ctx,(pkt >> 3) & 3, MACEtab3, MACEtab4, numChannels); + chomp6(ctx, pkt & 7, MACEtab1, MACEtab2, numChannels); inBuffer+=numChannels; --cnt; @@ -386,16 +392,17 @@ static void Exp1to6(MACEContext *ctx, } /* \\\ */ -static int mace_decode_init(AVCodecContext * avctx) +static av_cold int mace_decode_init(AVCodecContext * avctx) { if (avctx->channels > 2) return -1; + avctx->sample_fmt = SAMPLE_FMT_S16; return 0; } static int mace_decode_frame(AVCodecContext *avctx, void *data, int *data_size, - uint8_t *buf, int buf_size) + const uint8_t *buf, int buf_size) { short *samples; MACEContext *c = avctx->priv_data; @@ -403,25 +410,20 @@ static int mace_decode_frame(AVCodecContext *avctx, samples = (short *)data; switch (avctx->codec->id) { case CODEC_ID_MACE3: -#ifdef DEBUG -puts("mace_decode_frame[3]()"); -#endif - Exp1to3(c, buf, samples, buf_size / 2, avctx->channels, 1); + dprintf(avctx, "mace_decode_frame[3]()"); + Exp1to3(c, buf, samples, buf_size / 2 / avctx->channels, avctx->channels, 1); if (avctx->channels == 2) - Exp1to3(c, buf, samples+1, buf_size / 2, 2, 2); + Exp1to3(c, buf, samples+1, buf_size / 2 / 2, 2, 2); *data_size = 2 * 3 * buf_size; break; case CODEC_ID_MACE6: -#ifdef DEBUG -puts("mace_decode_frame[6]()"); -#endif - Exp1to6(c, buf, samples, buf_size, avctx->channels, 1); + dprintf(avctx, "mace_decode_frame[6]()"); + Exp1to6(c, buf, samples, buf_size / avctx->channels, avctx->channels, 1); if (avctx->channels == 2) - Exp1to6(c, buf, samples+1, buf_size, 2, 2); + Exp1to6(c, buf, samples+1, buf_size / 2, 2, 2); *data_size = 2 * 6 * buf_size; break; default: - *data_size = 0; return -1; } return buf_size; @@ -436,6 +438,7 @@ AVCodec mace3_decoder = { NULL, NULL, mace_decode_frame, + .long_name = NULL_IF_CONFIG_SMALL("MACE (Macintosh Audio Compression/Expansion) 3:1"), }; AVCodec mace6_decoder = { @@ -447,5 +450,6 @@ AVCodec mace6_decoder = { NULL, NULL, mace_decode_frame, + .long_name = NULL_IF_CONFIG_SMALL("MACE (Macintosh Audio Compression/Expansion) 6:1"), }; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/mathops.h b/src/add-ons/media/plugins/avcodec/libavcodec/mathops.h new file mode 100644 index 0000000000..b066386a20 --- /dev/null +++ b/src/add-ons/media/plugins/avcodec/libavcodec/mathops.h @@ -0,0 +1,87 @@ +/* + * simple math operations + * Copyright (c) 2001, 2002 Fabrice Bellard. + * Copyright (c) 2006 Michael Niedermayer et al + * + * This file is part of FFmpeg. + * + * FFmpeg 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.1 of the License, or (at your option) any later version. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#ifndef FFMPEG_MATHOPS_H +#define FFMPEG_MATHOPS_H + +#include "common.h" + +#ifdef ARCH_X86_32 + +#include "i386/mathops.h" + +#elif defined(ARCH_ARMV4L) + +#include "armv4l/mathops.h" + +#elif defined(ARCH_POWERPC) + +#include "ppc/mathops.h" + +#elif defined(ARCH_BFIN) + +#include "bfin/mathops.h" + +#endif + +/* generic implementation */ + +#ifndef MULL +# define MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS) +#endif + +#ifndef MULH +//gcc 3.4 creates an incredibly bloated mess out of this +//# define MULH(a,b) (((int64_t)(a) * (int64_t)(b))>>32) + +static av_always_inline int MULH(int a, int b){ + return ((int64_t)(a) * (int64_t)(b))>>32; +} +#endif + +#ifndef MUL64 +# define MUL64(a,b) ((int64_t)(a) * (int64_t)(b)) +#endif + +#ifndef MAC64 +# define MAC64(d, a, b) ((d) += MUL64(a, b)) +#endif + +#ifndef MLS64 +# define MLS64(d, a, b) ((d) -= MUL64(a, b)) +#endif + +/* signed 16x16 -> 32 multiply add accumulate */ +#ifndef MAC16 +# define MAC16(rt, ra, rb) rt += (ra) * (rb) +#endif + +/* signed 16x16 -> 32 multiply */ +#ifndef MUL16 +# define MUL16(ra, rb) ((ra) * (rb)) +#endif + +#ifndef MLS16 +# define MLS16(rt, ra, rb) ((rt) -= (ra) * (rb)) +#endif + +#endif /* FFMPEG_MATHOPS_H */ + diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/mdct.c b/src/add-ons/media/plugins/avcodec/libavcodec/mdct.c index a0f5671771..fe19f1ff3d 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/mdct.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/mdct.c @@ -2,19 +2,21 @@ * MDCT/IMDCT transforms * Copyright (c) 2002 Fabrice Bellard. * - * This library is free software; you can redistribute it and/or + * This file is part of FFmpeg. + * + * FFmpeg 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. + * version 2.1 of the License, or (at your option) any later version. * - * This library is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "dsputil.h" @@ -23,13 +25,52 @@ * MDCT/IMDCT transforms. */ +// Generate a Kaiser-Bessel Derived Window. +#define BESSEL_I0_ITER 50 // default: 50 iterations of Bessel I0 approximation +void ff_kbd_window_init(float *window, float alpha, int n) +{ + int i, j; + double sum = 0.0, bessel, tmp; + double local_window[n]; + double alpha2 = (alpha * M_PI / n) * (alpha * M_PI / n); + + for (i = 0; i < n; i++) { + tmp = i * (n - i) * alpha2; + bessel = 1.0; + for (j = BESSEL_I0_ITER; j > 0; j--) + bessel = bessel * tmp / (j * j) + 1; + sum += bessel; + local_window[i] = sum; + } + + sum++; + for (i = 0; i < n; i++) + window[i] = sqrt(local_window[i] / sum); +} + +DECLARE_ALIGNED(16, float, ff_sine_128 [ 128]); +DECLARE_ALIGNED(16, float, ff_sine_256 [ 256]); +DECLARE_ALIGNED(16, float, ff_sine_512 [ 512]); +DECLARE_ALIGNED(16, float, ff_sine_1024[1024]); +DECLARE_ALIGNED(16, float, ff_sine_2048[2048]); +float *ff_sine_windows[5] = { + ff_sine_128, ff_sine_256, ff_sine_512, ff_sine_1024, ff_sine_2048, +}; + +// Generate a sine window. +void ff_sine_window_init(float *window, int n) { + int i; + for(i = 0; i < n; i++) + window[i] = sin((i + 0.5) / (2 * n) * M_PI); +} + /** * init MDCT or IMDCT computation. */ int ff_mdct_init(MDCTContext *s, int nbits, int inverse) { int n, n4, i; - float alpha; + double alpha; memset(s, 0, sizeof(*s)); n = 1 << nbits; @@ -48,7 +89,7 @@ int ff_mdct_init(MDCTContext *s, int nbits, int inverse) s->tcos[i] = -cos(alpha); s->tsin[i] = -sin(alpha); } - if (fft_init(&s->fft, s->nbits - 2, inverse) < 0) + if (ff_fft_init(&s->fft, s->nbits - 2, inverse) < 0) goto fail; return 0; fail: @@ -60,29 +101,28 @@ int ff_mdct_init(MDCTContext *s, int nbits, int inverse) /* complex multiplication: p = a * b */ #define CMUL(pre, pim, are, aim, bre, bim) \ {\ - float _are = (are);\ - float _aim = (aim);\ - float _bre = (bre);\ - float _bim = (bim);\ + FFTSample _are = (are);\ + FFTSample _aim = (aim);\ + FFTSample _bre = (bre);\ + FFTSample _bim = (bim);\ (pre) = _are * _bre - _aim * _bim;\ (pim) = _are * _bim + _aim * _bre;\ } /** - * Compute inverse MDCT of size N = 2^nbits - * @param output N samples + * Compute the middle half of the inverse MDCT of size N = 2^nbits, + * thus excluding the parts that can be derived by symmetry + * @param output N/2 samples * @param input N/2 samples - * @param tmp N/2 samples */ -void ff_imdct_calc(MDCTContext *s, FFTSample *output, - const FFTSample *input, FFTSample *tmp) +void ff_imdct_half_c(MDCTContext *s, FFTSample *output, const FFTSample *input) { int k, n8, n4, n2, n, j; const uint16_t *revtab = s->fft.revtab; const FFTSample *tcos = s->tcos; const FFTSample *tsin = s->tsin; const FFTSample *in1, *in2; - FFTComplex *z = (FFTComplex *)tmp; + FFTComplex *z = (FFTComplex *)output; n = 1 << s->nbits; n2 = n >> 1; @@ -98,25 +138,39 @@ void ff_imdct_calc(MDCTContext *s, FFTSample *output, in1 += 2; in2 -= 2; } - fft_calc(&s->fft, z); + ff_fft_calc(&s->fft, z); /* post rotation + reordering */ - /* XXX: optimize */ - for(k = 0; k < n4; k++) { - CMUL(z[k].re, z[k].im, z[k].re, z[k].im, tcos[k], tsin[k]); - } + output += n4; for(k = 0; k < n8; k++) { - output[2*k] = -z[n8 + k].im; - output[n2-1-2*k] = z[n8 + k].im; + FFTSample r0, i0, r1, i1; + CMUL(r0, i1, z[n8-k-1].im, z[n8-k-1].re, tsin[n8-k-1], tcos[n8-k-1]); + CMUL(r1, i0, z[n8+k ].im, z[n8+k ].re, tsin[n8+k ], tcos[n8+k ]); + z[n8-k-1].re = r0; + z[n8-k-1].im = i0; + z[n8+k ].re = r1; + z[n8+k ].im = i1; + } +} - output[2*k+1] = z[n8-1-k].re; - output[n2-1-2*k-1] = -z[n8-1-k].re; +/** + * Compute inverse MDCT of size N = 2^nbits + * @param output N samples + * @param input N/2 samples + * @param tmp N/2 samples + */ +void ff_imdct_calc_c(MDCTContext *s, FFTSample *output, const FFTSample *input) +{ + int k; + int n = 1 << s->nbits; + int n2 = n >> 1; + int n4 = n >> 2; - output[n2 + 2*k]=-z[k+n8].re; - output[n-1- 2*k]=-z[k+n8].re; + ff_imdct_half_c(s, output+n4, input); - output[n2 + 2*k+1]=z[n8-k-1].im; - output[n-2 - 2 * k] = z[n8-k-1].im; + for(k = 0; k < n4; k++) { + output[k] = -output[n2-k-1]; + output[n-k-1] = output[n2+k]; } } @@ -126,15 +180,14 @@ void ff_imdct_calc(MDCTContext *s, FFTSample *output, * @param out N/2 samples * @param tmp temporary storage of N/2 samples */ -void ff_mdct_calc(MDCTContext *s, FFTSample *out, - const FFTSample *input, FFTSample *tmp) +void ff_mdct_calc(MDCTContext *s, FFTSample *out, const FFTSample *input) { int i, j, n, n8, n4, n2, n3; - FFTSample re, im, re1, im1; + FFTSample re, im; const uint16_t *revtab = s->fft.revtab; const FFTSample *tcos = s->tcos; const FFTSample *tsin = s->tsin; - FFTComplex *x = (FFTComplex *)tmp; + FFTComplex *x = (FFTComplex *)out; n = 1 << s->nbits; n2 = n >> 1; @@ -155,15 +208,17 @@ void ff_mdct_calc(MDCTContext *s, FFTSample *out, CMUL(x[j].re, x[j].im, re, im, -tcos[n8 + i], tsin[n8 + i]); } - fft_calc(&s->fft, x); - + ff_fft_calc(&s->fft, x); + /* post rotation */ - for(i=0;itcos); av_freep(&s->tsin); - fft_end(&s->fft); + ff_fft_end(&s->fft); } diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/mdec.c b/src/add-ons/media/plugins/avcodec/libavcodec/mdec.c index faf3cef316..37457676a6 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/mdec.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/mdec.c @@ -1,42 +1,41 @@ /* - * PSX MDEC codec + * Sony PlayStation MDEC (Motion DECoder) * Copyright (c) 2003 Michael Niedermayer * - * This library is free software; you can redistribute it and/or + * based upon code from Sebastian Jedruszkiewicz + * + * This file is part of FFmpeg. + * + * FFmpeg 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. + * version 2.1 of the License, or (at your option) any later version. * - * This library is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * based upon code from Sebastian Jedruszkiewicz + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ - + /** * @file mdec.c - * PSX MDEC codec. - * This is very similar to intra only MPEG1. + * Sony PlayStation MDEC (Motion DECoder) + * This is very similar to intra-only MPEG-1. */ - + #include "avcodec.h" #include "dsputil.h" #include "mpegvideo.h" - -//#undef NDEBUG -//#include +#include "mpeg12.h" typedef struct MDECContext{ AVCodecContext *avctx; DSPContext dsp; AVFrame picture; - PutBitContext pb; GetBitContext gb; ScanTable scantable; int version; @@ -45,25 +44,25 @@ typedef struct MDECContext{ int mb_width; int mb_height; int mb_x, mb_y; - DCTELEM __align8 block[6][64]; - uint16_t __align8 intra_matrix[64]; - int __align8 q_intra_matrix[64]; + DECLARE_ALIGNED_16(DCTELEM, block[6][64]); + DECLARE_ALIGNED_8(uint16_t, intra_matrix[64]); + DECLARE_ALIGNED_8(int, q_intra_matrix[64]); uint8_t *bitstream_buffer; - int bitstream_buffer_size; + unsigned int bitstream_buffer_size; int block_last_index[6]; } MDECContext; -//very similar to mpeg1 +//very similar to MPEG-1 static inline int mdec_decode_block_intra(MDECContext *a, DCTELEM *block, int n) { int level, diff, i, j, run; int component; - RLTable *rl = &rl_mpeg1; + RLTable *rl = &ff_rl_mpeg1; uint8_t * const scantable= a->scantable.permutated; const uint16_t *quant_matrix= ff_mpeg1_default_intra_matrix; const int qscale= a->qscale; - /* DC coef */ + /* DC coefficient */ if(a->version==2){ block[0]= 2*get_sbits(&a->gb, 10) + 1024; }else{ @@ -74,22 +73,21 @@ static inline int mdec_decode_block_intra(MDECContext *a, DCTELEM *block, int n) a->last_dc[component]+= diff; block[0] = a->last_dc[component]<<3; } - + i = 0; { - OPEN_READER(re, &a->gb); - /* now quantify & encode AC coefs */ + OPEN_READER(re, &a->gb); + /* now quantify & encode AC coefficients */ for(;;) { UPDATE_CACHE(re, &a->gb); - GET_RL_VLC(level, run, re, &a->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2); - + GET_RL_VLC(level, run, re, &a->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0); + if(level == 127){ break; } else if(level != 0) { i += run; j = scantable[i]; level= (level*qscale*quant_matrix[j])>>3; -// level= (level-1)|1; level = (level ^ SHOW_SBITS(re, &a->gb, 1)) - SHOW_SBITS(re, &a->gb, 1); LAST_SKIP_BITS(re, &a->gb, 1); } else { @@ -127,9 +125,9 @@ static inline int decode_mb(MDECContext *a, DCTELEM block[6][64]){ const int block_index[6]= {5,4,0,1,2,3}; a->dsp.clear_blocks(block[0]); - + for(i=0; i<6; i++){ - if( mdec_decode_block_intra(a, block[ block_index[i] ], block_index[i]) < 0) + if( mdec_decode_block_intra(a, block[ block_index[i] ], block_index[i]) < 0) return -1; } return 0; @@ -138,7 +136,7 @@ static inline int decode_mb(MDECContext *a, DCTELEM block[6][64]){ static inline void idct_put(MDECContext *a, int mb_x, int mb_y){ DCTELEM (*block)[64]= a->block; int linesize= a->picture.linesize[0]; - + uint8_t *dest_y = a->picture.data[0] + (mb_y * 16* linesize ) + mb_x * 16; uint8_t *dest_cb = a->picture.data[1] + (mb_y * 8 * a->picture.linesize[1]) + mb_x * 8; uint8_t *dest_cr = a->picture.data[2] + (mb_y * 8 * a->picture.linesize[2]) + mb_x * 8; @@ -154,22 +152,15 @@ static inline void idct_put(MDECContext *a, int mb_x, int mb_y){ } } -static int decode_frame(AVCodecContext *avctx, +static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, - uint8_t *buf, int buf_size) + const uint8_t *buf, int buf_size) { MDECContext * const a = avctx->priv_data; AVFrame *picture = data; - AVFrame * const p= (AVFrame*)&a->picture; + AVFrame * const p= &a->picture; int i; - *data_size = 0; - - /* special case for last picture */ - if (buf_size == 0) { - return 0; - } - if(p->data[0]) avctx->release_buffer(avctx, p); @@ -178,11 +169,8 @@ static int decode_frame(AVCodecContext *avctx, av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); return -1; } - p->pict_type= I_TYPE; + p->pict_type= FF_I_TYPE; p->key_frame= 1; - a->last_dc[0]= - a->last_dc[1]= - a->last_dc[2]= 0; a->bitstream_buffer= av_fast_realloc(a->bitstream_buffer, &a->bitstream_buffer_size, buf_size + FF_INPUT_BUFFER_PADDING_SIZE); for(i=0; ibitstream_buffer[i+1]= buf[i ]; } init_get_bits(&a->gb, a->bitstream_buffer, buf_size*8); - + /* skip over 4 preamble bytes in stream (typically 0xXX 0xXX 0x00 0x38) */ skip_bits(&a->gb, 32); a->qscale= get_bits(&a->gb, 16); a->version= get_bits(&a->gb, 16); - -// printf("qscale:%d (0x%X), version:%d (0x%X)\n", a->qscale, a->qscale, a->version, a->version); - + + a->last_dc[0]= + a->last_dc[1]= + a->last_dc[2]= 128; + for(a->mb_x=0; a->mb_xmb_width; a->mb_x++){ for(a->mb_y=0; a->mb_ymb_height; a->mb_y++){ if( decode_mb(a, a->block) <0) return -1; - + idct_put(a, a->mb_x, a->mb_y); } } -// p->quality= (32 + a->inv_qscale/2)/a->inv_qscale; -// memset(p->qscale_table, p->quality, p->qstride*a->mb_height); - - *picture= *(AVFrame*)&a->picture; + p->quality= a->qscale * FF_QP2LAMBDA; + memset(p->qscale_table, a->qscale, p->qstride*a->mb_height); + + *picture = a->picture; *data_size = sizeof(AVPicture); - emms_c(); - return (get_bits_count(&a->gb)+31)/32*4; } -static void mdec_common_init(AVCodecContext *avctx){ +static av_cold void mdec_common_init(AVCodecContext *avctx){ MDECContext * const a = avctx->priv_data; dsputil_init(&a->dsp, avctx); - a->mb_width = (avctx->width + 15) / 16; - a->mb_height = (avctx->height + 15) / 16; + a->mb_width = (avctx->coded_width + 15) / 16; + a->mb_height = (avctx->coded_height + 15) / 16; - avctx->coded_frame= (AVFrame*)&a->picture; + avctx->coded_frame= &a->picture; a->avctx= avctx; } -static int decode_init(AVCodecContext *avctx){ +static av_cold int decode_init(AVCodecContext *avctx){ MDECContext * const a = avctx->priv_data; - AVFrame *p= (AVFrame*)&a->picture; - + AVFrame *p= &a->picture; + mdec_common_init(avctx); - init_vlcs(); + ff_mpeg12_init_vlcs(); ff_init_scantable(a->dsp.idct_permutation, &a->scantable, ff_zigzag_direct); -/* - for(i=0; i<64; i++){ - int index= ff_zigzag_direct[i]; - a->intra_matrix[i]= 64*ff_mpeg1_default_intra_matrix[index] / a->inv_qscale; - } -*/ + p->qstride= a->mb_width; p->qscale_table= av_mallocz( p->qstride * a->mb_height); + avctx->pix_fmt= PIX_FMT_YUV420P; return 0; } -static int decode_end(AVCodecContext *avctx){ +static av_cold int decode_end(AVCodecContext *avctx){ MDECContext * const a = avctx->priv_data; av_freep(&a->bitstream_buffer); av_freep(&a->picture.qscale_table); a->bitstream_buffer_size=0; - - avcodec_default_free_buffers(avctx); return 0; } @@ -272,5 +254,6 @@ AVCodec mdec_decoder = { decode_end, decode_frame, CODEC_CAP_DR1, + .long_name= NULL_IF_CONFIG_SMALL("Sony PlayStation MDEC (Motion DECoder)"), }; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/mimic.c b/src/add-ons/media/plugins/avcodec/libavcodec/mimic.c new file mode 100644 index 0000000000..527c7bcd00 --- /dev/null +++ b/src/add-ons/media/plugins/avcodec/libavcodec/mimic.c @@ -0,0 +1,390 @@ +/* + * Copyright (C) 2005 Ole André Vadla Ravnås + * Copyright (C) 2008 Ramiro Polla + * + * This file is part of FFmpeg. + * + * FFmpeg 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.1 of the License, or (at your option) any later version. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include + +#include "avcodec.h" +#include "bitstream.h" +#include "bytestream.h" +#include "dsputil.h" + +#define MIMIC_HEADER_SIZE 20 + +typedef struct { + AVCodecContext *avctx; + + int num_vblocks[3]; + int num_hblocks[3]; + + uint8_t *swap_buf; + int swap_buf_size; + + int cur_index; + int prev_index; + + AVFrame buf_ptrs [16]; + AVPicture flipped_ptrs[16]; + + DECLARE_ALIGNED_16(DCTELEM, dct_block[64]); + + GetBitContext gb; + ScanTable scantable; + DSPContext dsp; + VLC vlc; +} MimicContext; + +static const uint32_t huffcodes[] = { + 0x0000000a, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x0000000b, + 0x0000001b, 0x00000038, 0x00000078, 0x00000079, 0x0000007a, 0x000000f9, + 0x000000fa, 0x000003fb, 0x000007f8, 0x000007f9, 0x000007fa, 0x000007fb, + 0x00000ff8, 0x00000ff9, 0x00000001, 0x00000039, 0x0000007b, 0x000000fb, + 0x000001f8, 0x000001f9, 0x00000ffa, 0x00000ffb, 0x00001ff8, 0x00001ff9, + 0x00001ffa, 0x00001ffb, 0x00003ff8, 0x00003ff9, 0x00003ffa, 0x00000000, + 0x00000004, 0x0000003a, 0x000001fa, 0x00003ffb, 0x00007ff8, 0x00007ff9, + 0x00007ffa, 0x00007ffb, 0x0000fff8, 0x0000fff9, 0x0000fffa, 0x0000fffb, + 0x0001fff8, 0x0001fff9, 0x0001fffa, 0x00000000, 0x0000000c, 0x000000f8, + 0x000001fb, 0x0001fffb, 0x0003fff8, 0x0003fff9, 0x0003fffa, 0x0003fffb, + 0x0007fff8, 0x0007fff9, 0x0007fffa, 0x0007fffb, 0x000ffff8, 0x000ffff9, + 0x000ffffa, 0x00000000, 0x0000001a, 0x000003f8, 0x000ffffb, 0x001ffff8, + 0x001ffff9, 0x001ffffa, 0x001ffffb, 0x003ffff8, 0x003ffff9, 0x003ffffa, + 0x003ffffb, 0x007ffff8, 0x007ffff9, 0x007ffffa, 0x007ffffb, 0x00000000, + 0x0000003b, 0x000003f9, 0x00fffff8, 0x00fffff9, 0x00fffffa, 0x00fffffb, + 0x01fffff8, 0x01fffff9, 0x01fffffa, 0x01fffffb, 0x03fffff8, 0x03fffff9, + 0x03fffffa, 0x03fffffb, 0x07fffff8, 0x00000000, 0x000003fa, 0x07fffff9, + 0x07fffffa, 0x07fffffb, 0x0ffffff8, 0x0ffffff9, 0x0ffffffa, 0x0ffffffb, + 0x1ffffff8, 0x1ffffff9, 0x1ffffffa, 0x1ffffffb, 0x3ffffff8, 0x3ffffff9, + 0x3ffffffa, +}; + +static const uint8_t huffbits[] = { + 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 2, 4, 5, 6, 7, 7, 7, 8, + 8, 10, 11, 11, 11, 11, 12, 12, 2, 6, 7, 8, + 9, 9, 12, 12, 13, 13, 13, 13, 14, 14, 14, 0, + 3, 6, 9, 14, 15, 15, 15, 15, 16, 16, 16, 16, + 17, 17, 17, 0, 4, 8, 9, 17, 18, 18, 18, 18, + 19, 19, 19, 19, 20, 20, 20, 0, 5, 10, 20, 21, + 21, 21, 21, 22, 22, 22, 22, 23, 23, 23, 23, 0, + 6, 10, 24, 24, 24, 24, 25, 25, 25, 25, 26, 26, + 26, 26, 27, 0, 10, 27, 27, 27, 28, 28, 28, 28, + 29, 29, 29, 29, 30, 30, 30, +}; + +static const uint8_t col_zag[64] = { + 0, 8, 1, 2, 9, 16, 24, 17, + 10, 3, 4, 11, 18, 25, 32, 40, + 33, 26, 19, 12, 5, 6, 13, 20, + 27, 34, 41, 48, 56, 49, 42, 35, + 28, 21, 14, 7, 15, 22, 29, 36, + 43, 50, 57, 58, 51, 44, 37, 30, + 23, 31, 38, 45, 52, 59, 39, 46, + 53, 60, 61, 54, 47, 55, 62, 63, +}; + +static av_cold int mimic_decode_init(AVCodecContext *avctx) +{ + MimicContext *ctx = avctx->priv_data; + + ctx->prev_index = 0; + ctx->cur_index = 15; + + if(init_vlc(&ctx->vlc, 11, sizeof(huffbits)/sizeof(huffbits[0]), + huffbits, 1, 1, huffcodes, 4, 4, 0)) { + av_log(avctx, AV_LOG_ERROR, "error initializing vlc table\n"); + return -1; + } + dsputil_init(&ctx->dsp, avctx); + ff_init_scantable(ctx->dsp.idct_permutation, &ctx->scantable, col_zag); + + return 0; +} + +const static int8_t vlcdec_lookup[9][64] = { + { 0, }, + { -1, 1, }, + { -3, 3, -2, 2, }, + { -7, 7, -6, 6, -5, 5, -4, 4, }, + { -15, 15, -14, 14, -13, 13, -12, 12, + -11, 11, -10, 10, -9, 9, -8, 8, }, + { -31, 31, -30, 30, -29, 29, -28, 28, + -27, 27, -26, 26, -25, 25, -24, 24, + -23, 23, -22, 22, -21, 21, -20, 20, + -19, 19, -18, 18, -17, 17, -16, 16, }, + { -63, 63, -62, 62, -61, 61, -60, 60, + -59, 59, -58, 58, -57, 57, -56, 56, + -55, 55, -54, 54, -53, 53, -52, 52, + -51, 51, -50, 50, -49, 49, -48, 48, + -47, 47, -46, 46, -45, 45, -44, 44, + -43, 43, -42, 42, -41, 41, -40, 40, + -39, 39, -38, 38, -37, 37, -36, 36, + -35, 35, -34, 34, -33, 33, -32, 32, }, + { -127, 127, -126, 126, -125, 125, -124, 124, + -123, 123, -122, 122, -121, 121, -120, 120, + -119, 119, -118, 118, -117, 117, -116, 116, + -115, 115, -114, 114, -113, 113, -112, 112, + -111, 111, -110, 110, -109, 109, -108, 108, + -107, 107, -106, 106, -105, 105, -104, 104, + -103, 103, -102, 102, -101, 101, -100, 100, + -99, 99, -98, 98, -97, 97, -96, 96, }, + { -95, 95, -94, 94, -93, 93, -92, 92, + -91, 91, -90, 90, -89, 89, -88, 88, + -87, 87, -86, 86, -85, 85, -84, 84, + -83, 83, -82, 82, -81, 81, -80, 80, + -79, 79, -78, 78, -77, 77, -76, 76, + -75, 75, -74, 74, -73, 73, -72, 72, + -71, 71, -70, 70, -69, 69, -68, 68, + -67, 67, -66, 66, -65, 65, -64, 64, }, +}; + +static int vlc_decode_block(MimicContext *ctx, int num_coeffs, int qscale) +{ + DCTELEM *block = ctx->dct_block; + unsigned int pos; + + memset(block, 0, 64 * sizeof(DCTELEM)); + + block[0] = get_bits(&ctx->gb, 8) << 3; + + for(pos = 1; pos < num_coeffs; pos++) { + uint32_t vlc, num_bits; + int value; + int coeff; + + vlc = get_vlc2(&ctx->gb, ctx->vlc.table, ctx->vlc.bits, 3); + if(!vlc) /* end-of-block code */ + return 1; + if(vlc == -1) + return 0; + + /* pos_add and num_bits are coded in the vlc code */ + pos += vlc&15; // pos_add + num_bits = vlc>>4; // num_bits + + if(pos >= 64) + return 0; + + value = get_bits(&ctx->gb, num_bits); + + /* FFmpeg's IDCT behaves somewhat different from the original code, so + * a factor of 4 was added to the input */ + + coeff = vlcdec_lookup[num_bits][value]; + if(pos<3) + coeff <<= 4; + else /* TODO Use >> 10 instead of / 1001 */ + coeff = (coeff * qscale) / 1001; + + block[ctx->scantable.permutated[pos]] = coeff; + } + + return 1; +} + +static int decode(MimicContext *ctx, int quality, int num_coeffs, + int is_iframe) +{ + int y, x, plane; + + for(plane = 0; plane < 3; plane++) { + const int is_chroma = !!plane; + const int qscale = av_clip(10000-quality,is_chroma?1000:2000,10000)<<2; + const int stride = ctx->flipped_ptrs[ctx->cur_index].linesize[plane]; + const uint8_t *src = ctx->flipped_ptrs[ctx->prev_index].data[plane]; + uint8_t *dst = ctx->flipped_ptrs[ctx->cur_index ].data[plane]; + + for(y = 0; y < ctx->num_vblocks[plane]; y++) { + for(x = 0; x < ctx->num_hblocks[plane]; x++) { + + /* Check for a change condition in the current block. + * - iframes always change. + * - Luma plane changes on get_bits1 == 0 + * - Chroma planes change on get_bits1 == 1 */ + if(is_iframe || get_bits1(&ctx->gb) == is_chroma) { + + /* Luma planes may use a backreference from the 15 last + * frames preceding the previous. (get_bits1 == 1) + * Chroma planes don't use backreferences. */ + if(is_chroma || is_iframe || !get_bits1(&ctx->gb)) { + + if(!vlc_decode_block(ctx, num_coeffs, qscale)) + return 0; + ctx->dsp.idct_put(dst, stride, ctx->dct_block); + } else { + unsigned int backref = get_bits(&ctx->gb, 4); + int index = (ctx->cur_index+backref)&15; + uint8_t *p = ctx->flipped_ptrs[index].data[0]; + + if(p) { + p += src - + ctx->flipped_ptrs[ctx->prev_index].data[plane]; + ctx->dsp.put_pixels_tab[1][0](dst, p, stride, 8); + } else { + av_log(ctx->avctx, AV_LOG_ERROR, + "No such backreference! Buggy sample.\n"); + } + } + } else { + ctx->dsp.put_pixels_tab[1][0](dst, src, stride, 8); + } + src += 8; + dst += 8; + } + src += (stride - ctx->num_hblocks[plane])<<3; + dst += (stride - ctx->num_hblocks[plane])<<3; + } + } + + return 1; +} + +/** + * Flip the buffer upside-down and put it in the YVU order to match the + * way Mimic encodes frames. + */ +static void prepare_avpic(MimicContext *ctx, AVPicture *dst, AVPicture *src) +{ + int i; + dst->data[0] = src->data[0]+( ctx->avctx->height -1)*src->linesize[0]; + dst->data[1] = src->data[2]+((ctx->avctx->height>>1)-1)*src->linesize[2]; + dst->data[2] = src->data[1]+((ctx->avctx->height>>1)-1)*src->linesize[1]; + for(i = 0; i < 3; i++) + dst->linesize[i] = -src->linesize[i]; +} + +static int mimic_decode_frame(AVCodecContext *avctx, void *data, + int *data_size, const uint8_t *buf, int buf_size) +{ + MimicContext *ctx = avctx->priv_data; + int is_pframe; + int width, height; + int quality, num_coeffs; + int swap_buf_size = buf_size - MIMIC_HEADER_SIZE; + + if(buf_size < MIMIC_HEADER_SIZE) { + av_log(avctx, AV_LOG_ERROR, "insufficient data\n"); + return -1; + } + + buf += 2; /* some constant (always 256) */ + quality = bytestream_get_le16(&buf); + width = bytestream_get_le16(&buf); + height = bytestream_get_le16(&buf); + buf += 4; /* some constant */ + is_pframe = bytestream_get_le32(&buf); + num_coeffs = bytestream_get_byte(&buf); + buf += 3; /* some constant */ + + if(!ctx->avctx) { + int i; + + if(!(width == 160 && height == 120) && + !(width == 320 && height == 240)) { + av_log(avctx, AV_LOG_ERROR, "invalid width/height!\n"); + return -1; + } + + ctx->avctx = avctx; + avctx->width = width; + avctx->height = height; + avctx->pix_fmt = PIX_FMT_YUV420P; + for(i = 0; i < 3; i++) { + ctx->num_vblocks[i] = -((-height) >> (3 + !!i)); + ctx->num_hblocks[i] = width >> (3 + !!i) ; + } + } else if(width != ctx->avctx->width || height != ctx->avctx->height) { + av_log(avctx, AV_LOG_ERROR, "resolution changing is not supported\n"); + return -1; + } + + if(is_pframe && !ctx->buf_ptrs[ctx->prev_index].data[0]) { + av_log(avctx, AV_LOG_ERROR, "decoding must start with keyframe\n"); + return -1; + } + + ctx->buf_ptrs[ctx->cur_index].reference = 1; + if(avctx->get_buffer(avctx, &ctx->buf_ptrs[ctx->cur_index])) { + av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); + return -1; + } + + prepare_avpic(ctx, &ctx->flipped_ptrs[ctx->cur_index], + (AVPicture*) &ctx->buf_ptrs[ctx->cur_index]); + + ctx->swap_buf = av_fast_realloc(ctx->swap_buf, &ctx->swap_buf_size, + swap_buf_size + FF_INPUT_BUFFER_PADDING_SIZE); + if(!ctx->swap_buf) + return AVERROR_NOMEM; + + ctx->dsp.bswap_buf((uint32_t*)ctx->swap_buf, + (const uint32_t*) buf, + swap_buf_size>>2); + init_get_bits(&ctx->gb, ctx->swap_buf, swap_buf_size << 3); + + if(!decode(ctx, quality, num_coeffs, !is_pframe)) { + avctx->release_buffer(avctx, &ctx->buf_ptrs[ctx->cur_index]); + return -1; + } + + ctx->buf_ptrs[ctx->cur_index].pict_type = is_pframe ? FF_P_TYPE:FF_I_TYPE; + *(AVFrame*)data = ctx->buf_ptrs[ctx->cur_index]; + *data_size = sizeof(AVFrame); + + ctx->prev_index = ctx->cur_index; + ctx->cur_index--; + ctx->cur_index &= 15; + + /* Only release frames that aren't used for backreferences anymore */ + if(ctx->buf_ptrs[ctx->cur_index].data[0]) + avctx->release_buffer(avctx, &ctx->buf_ptrs[ctx->cur_index]); + + return buf_size; +} + +static av_cold int mimic_decode_end(AVCodecContext *avctx) +{ + MimicContext *ctx = avctx->priv_data; + int i; + + av_free(ctx->swap_buf); + for(i = 0; i < 16; i++) + if(ctx->buf_ptrs[i].data[0]) + avctx->release_buffer(avctx, &ctx->buf_ptrs[i]); + free_vlc(&ctx->vlc); + + return 0; +} + +AVCodec mimic_decoder = { + "mimic", + CODEC_TYPE_VIDEO, + CODEC_ID_MIMIC, + sizeof(MimicContext), + mimic_decode_init, + NULL, + mimic_decode_end, + mimic_decode_frame, + CODEC_CAP_DR1, + .long_name = NULL_IF_CONFIG_SMALL("Mimic"), +}; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/mjpeg.c b/src/add-ons/media/plugins/avcodec/libavcodec/mjpeg.c index 1a948aa56f..11a84bafcb 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/mjpeg.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/mjpeg.c @@ -1,142 +1,44 @@ /* * MJPEG encoder and decoder * Copyright (c) 2000, 2001 Fabrice Bellard. + * Copyright (c) 2003 Alex Beregszaszi + * Copyright (c) 2003-2004 Michael Niedermayer * - * This library is free software; you can redistribute it and/or + * Support for external huffman table, various fixes (AVID workaround), + * aspecting, new decode_frame mechanism and apple mjpeg-b support + * by Alex Beregszaszi + * + * This file is part of FFmpeg. + * + * FFmpeg 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. + * version 2.1 of the License, or (at your option) any later version. * - * This library is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Support for external huffman table, various fixes (AVID workaround), - * aspecting, new decode_frame mechanism and apple mjpeg-b support - * by Alex Beregszaszi + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ /** * @file mjpeg.c * MJPEG encoder and decoder. */ - -//#define DEBUG -#include -#include "avcodec.h" -#include "dsputil.h" -#include "mpegvideo.h" +#include "mjpeg.h" -/* use two quantizer tables (one for luminance and one for chrominance) */ -/* not yet working */ -#undef TWOMATRIXES - -typedef struct MJpegContext { - uint8_t huff_size_dc_luminance[12]; //FIXME use array [3] instead of lumi / chrom, for easier addressing - uint16_t huff_code_dc_luminance[12]; - uint8_t huff_size_dc_chrominance[12]; - uint16_t huff_code_dc_chrominance[12]; - - uint8_t huff_size_ac_luminance[256]; - uint16_t huff_code_ac_luminance[256]; - uint8_t huff_size_ac_chrominance[256]; - uint16_t huff_code_ac_chrominance[256]; -} MJpegContext; - -/* JPEG marker codes */ -typedef enum { - /* start of frame */ - SOF0 = 0xc0, /* baseline */ - SOF1 = 0xc1, /* extended sequential, huffman */ - SOF2 = 0xc2, /* progressive, huffman */ - SOF3 = 0xc3, /* lossless, huffman */ - - SOF5 = 0xc5, /* differential sequential, huffman */ - SOF6 = 0xc6, /* differential progressive, huffman */ - SOF7 = 0xc7, /* differential lossless, huffman */ - JPG = 0xc8, /* reserved for JPEG extension */ - SOF9 = 0xc9, /* extended sequential, arithmetic */ - SOF10 = 0xca, /* progressive, arithmetic */ - SOF11 = 0xcb, /* lossless, arithmetic */ - - SOF13 = 0xcd, /* differential sequential, arithmetic */ - SOF14 = 0xce, /* differential progressive, arithmetic */ - SOF15 = 0xcf, /* differential lossless, arithmetic */ - - DHT = 0xc4, /* define huffman tables */ - - DAC = 0xcc, /* define arithmetic-coding conditioning */ - - /* restart with modulo 8 count "m" */ - RST0 = 0xd0, - RST1 = 0xd1, - RST2 = 0xd2, - RST3 = 0xd3, - RST4 = 0xd4, - RST5 = 0xd5, - RST6 = 0xd6, - RST7 = 0xd7, - - SOI = 0xd8, /* start of image */ - EOI = 0xd9, /* end of image */ - SOS = 0xda, /* start of scan */ - DQT = 0xdb, /* define quantization tables */ - DNL = 0xdc, /* define number of lines */ - DRI = 0xdd, /* define restart interval */ - DHP = 0xde, /* define hierarchical progression */ - EXP = 0xdf, /* expand reference components */ - - APP0 = 0xe0, - APP1 = 0xe1, - APP2 = 0xe2, - APP3 = 0xe3, - APP4 = 0xe4, - APP5 = 0xe5, - APP6 = 0xe6, - APP7 = 0xe7, - APP8 = 0xe8, - APP9 = 0xe9, - APP10 = 0xea, - APP11 = 0xeb, - APP12 = 0xec, - APP13 = 0xed, - APP14 = 0xee, - APP15 = 0xef, - - JPG0 = 0xf0, - JPG1 = 0xf1, - JPG2 = 0xf2, - JPG3 = 0xf3, - JPG4 = 0xf4, - JPG5 = 0xf5, - JPG6 = 0xf6, - JPG7 = 0xf7, - JPG8 = 0xf8, - JPG9 = 0xf9, - JPG10 = 0xfa, - JPG11 = 0xfb, - JPG12 = 0xfc, - JPG13 = 0xfd, - - COM = 0xfe, /* comment */ - - TEM = 0x01, /* temporary private use for arithmetic coding */ - - /* 0x02 -> 0xbf reserved */ -} JPEG_MARKER; #if 0 /* These are the sample quantization tables given in JPEG spec section K.1. * The spec says that the values given produce "good" quality, and * when divided by 2, "very good" quality. */ -static const unsigned char std_luminance_quant_tbl[64] = { +const unsigned char std_luminance_quant_tbl[64] = { 16, 11, 10, 16, 24, 40, 51, 61, 12, 12, 14, 19, 26, 58, 60, 55, 14, 13, 16, 24, 40, 57, 69, 56, @@ -146,7 +48,7 @@ static const unsigned char std_luminance_quant_tbl[64] = { 49, 64, 78, 87, 103, 121, 120, 101, 72, 92, 95, 98, 112, 100, 103, 99 }; -static const unsigned char std_chrominance_quant_tbl[64] = { +const unsigned char std_chrominance_quant_tbl[64] = { 17, 18, 24, 47, 99, 99, 99, 99, 18, 21, 26, 66, 99, 99, 99, 99, 24, 26, 56, 99, 99, 99, 99, 99, @@ -160,19 +62,17 @@ static const unsigned char std_chrominance_quant_tbl[64] = { /* Set up the standard Huffman tables (cf. JPEG standard section K.3) */ /* IMPORTANT: these are only valid for 8-bit data precision! */ -static const uint8_t bits_dc_luminance[17] = +const uint8_t ff_mjpeg_bits_dc_luminance[17] = { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }; -static const uint8_t val_dc_luminance[] = +const uint8_t ff_mjpeg_val_dc[12] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; -static const uint8_t bits_dc_chrominance[17] = +const uint8_t ff_mjpeg_bits_dc_chrominance[17] = { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 }; -static const uint8_t val_dc_chrominance[] = -{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; -static const uint8_t bits_ac_luminance[17] = +const uint8_t ff_mjpeg_bits_ac_luminance[17] = { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d }; -static const uint8_t val_ac_luminance[] = +const uint8_t ff_mjpeg_val_ac_luminance[] = { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, @@ -193,13 +93,13 @@ static const uint8_t val_ac_luminance[] = 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, - 0xf9, 0xfa + 0xf9, 0xfa }; -static const uint8_t bits_ac_chrominance[17] = +const uint8_t ff_mjpeg_bits_ac_chrominance[17] = { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 }; -static const uint8_t val_ac_chrominance[] = +const uint8_t ff_mjpeg_val_ac_chrominance[] = { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, @@ -220,12 +120,13 @@ static const uint8_t val_ac_chrominance[] = 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, - 0xf9, 0xfa + 0xf9, 0xfa }; /* isn't this function nicer than the one in the libjpeg ? */ -static void build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code, - const uint8_t *bits_table, const uint8_t *val_table) +void ff_mjpeg_build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code, + const uint8_t *bits_table, + const uint8_t *val_table) { int i, j, k,nb, code, sym; @@ -242,1979 +143,3 @@ static void build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code, code <<= 1; } } - -#ifdef CONFIG_ENCODERS -int mjpeg_init(MpegEncContext *s) -{ - MJpegContext *m; - - m = av_malloc(sizeof(MJpegContext)); - if (!m) - return -1; - - s->min_qcoeff=-1023; - s->max_qcoeff= 1023; - - /* build all the huffman tables */ - build_huffman_codes(m->huff_size_dc_luminance, - m->huff_code_dc_luminance, - bits_dc_luminance, - val_dc_luminance); - build_huffman_codes(m->huff_size_dc_chrominance, - m->huff_code_dc_chrominance, - bits_dc_chrominance, - val_dc_chrominance); - build_huffman_codes(m->huff_size_ac_luminance, - m->huff_code_ac_luminance, - bits_ac_luminance, - val_ac_luminance); - build_huffman_codes(m->huff_size_ac_chrominance, - m->huff_code_ac_chrominance, - bits_ac_chrominance, - val_ac_chrominance); - - s->mjpeg_ctx = m; - return 0; -} - -void mjpeg_close(MpegEncContext *s) -{ - av_free(s->mjpeg_ctx); -} -#endif //CONFIG_ENCODERS - -#define PREDICT(ret, topleft, top, left, predictor)\ - switch(predictor){\ - case 1: ret= left; break;\ - case 2: ret= top; break;\ - case 3: ret= topleft; break;\ - case 4: ret= left + top - topleft; break;\ - case 5: ret= left + ((top - topleft)>>1); break;\ - case 6: ret= top + ((left - topleft)>>1); break;\ - default:\ - case 7: ret= (left + top)>>1; break;\ - } - -#ifdef CONFIG_ENCODERS -static inline void put_marker(PutBitContext *p, int code) -{ - put_bits(p, 8, 0xff); - put_bits(p, 8, code); -} - -/* table_class: 0 = DC coef, 1 = AC coefs */ -static int put_huffman_table(MpegEncContext *s, int table_class, int table_id, - const uint8_t *bits_table, const uint8_t *value_table) -{ - PutBitContext *p = &s->pb; - int n, i; - - put_bits(p, 4, table_class); - put_bits(p, 4, table_id); - - n = 0; - for(i=1;i<=16;i++) { - n += bits_table[i]; - put_bits(p, 8, bits_table[i]); - } - - for(i=0;ipb; - int i, j, size; - uint8_t *ptr; - - /* quant matrixes */ - put_marker(p, DQT); -#ifdef TWOMATRIXES - put_bits(p, 16, 2 + 2 * (1 + 64)); -#else - put_bits(p, 16, 2 + 1 * (1 + 64)); -#endif - put_bits(p, 4, 0); /* 8 bit precision */ - put_bits(p, 4, 0); /* table 0 */ - for(i=0;i<64;i++) { - j = s->intra_scantable.permutated[i]; - put_bits(p, 8, s->intra_matrix[j]); - } -#ifdef TWOMATRIXES - put_bits(p, 4, 0); /* 8 bit precision */ - put_bits(p, 4, 1); /* table 1 */ - for(i=0;i<64;i++) { - j = s->intra_scantable.permutated[i]; - put_bits(p, 8, s->chroma_intra_matrix[j]); - } -#endif - - /* huffman table */ - put_marker(p, DHT); - flush_put_bits(p); - ptr = pbBufPtr(p); - put_bits(p, 16, 0); /* patched later */ - size = 2; - size += put_huffman_table(s, 0, 0, bits_dc_luminance, val_dc_luminance); - size += put_huffman_table(s, 0, 1, bits_dc_chrominance, val_dc_chrominance); - - size += put_huffman_table(s, 1, 0, bits_ac_luminance, val_ac_luminance); - size += put_huffman_table(s, 1, 1, bits_ac_chrominance, val_ac_chrominance); - ptr[0] = size >> 8; - ptr[1] = size; -} - -static void jpeg_put_comments(MpegEncContext *s) -{ - PutBitContext *p = &s->pb; - int size; - uint8_t *ptr; - - if (s->aspect_ratio_info /* && !lossless */) - { - /* JFIF header */ - put_marker(p, APP0); - put_bits(p, 16, 16); - put_string(p, "JFIF"); /* this puts the trailing zero-byte too */ - put_bits(p, 16, 0x0201); /* v 1.02 */ - put_bits(p, 8, 0); /* units type: 0 - aspect ratio */ - put_bits(p, 16, s->avctx->sample_aspect_ratio.num); - put_bits(p, 16, s->avctx->sample_aspect_ratio.den); - put_bits(p, 8, 0); /* thumbnail width */ - put_bits(p, 8, 0); /* thumbnail height */ - } - - /* comment */ - if(!(s->flags & CODEC_FLAG_BITEXACT)){ - put_marker(p, COM); - flush_put_bits(p); - ptr = pbBufPtr(p); - put_bits(p, 16, 0); /* patched later */ - put_string(p, LIBAVCODEC_IDENT); - size = strlen(LIBAVCODEC_IDENT)+3; - ptr[0] = size >> 8; - ptr[1] = size; - } -} - -void mjpeg_picture_header(MpegEncContext *s) -{ - const int lossless= s->avctx->codec_id == CODEC_ID_LJPEG; - - put_marker(&s->pb, SOI); - - if (!s->mjpeg_data_only_frames) - { - jpeg_put_comments(s); - - if (s->mjpeg_write_tables) jpeg_table_header(s); - - put_marker(&s->pb, lossless ? SOF3 : SOF0); - - put_bits(&s->pb, 16, 17); - if(lossless && s->avctx->pix_fmt == PIX_FMT_RGBA32) - put_bits(&s->pb, 8, 9); /* 9 bits/component RCT */ - else - put_bits(&s->pb, 8, 8); /* 8 bits/component */ - put_bits(&s->pb, 16, s->height); - put_bits(&s->pb, 16, s->width); - put_bits(&s->pb, 8, 3); /* 3 components */ - - /* Y component */ - put_bits(&s->pb, 8, 1); /* component number */ - put_bits(&s->pb, 4, s->mjpeg_hsample[0]); /* H factor */ - put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* V factor */ - put_bits(&s->pb, 8, 0); /* select matrix */ - - /* Cb component */ - put_bits(&s->pb, 8, 2); /* component number */ - put_bits(&s->pb, 4, s->mjpeg_hsample[1]); /* H factor */ - put_bits(&s->pb, 4, s->mjpeg_vsample[1]); /* V factor */ -#ifdef TWOMATRIXES - put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */ -#else - put_bits(&s->pb, 8, 0); /* select matrix */ -#endif - - /* Cr component */ - put_bits(&s->pb, 8, 3); /* component number */ - put_bits(&s->pb, 4, s->mjpeg_hsample[2]); /* H factor */ - put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* V factor */ -#ifdef TWOMATRIXES - put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */ -#else - put_bits(&s->pb, 8, 0); /* select matrix */ -#endif - } - - /* scan header */ - put_marker(&s->pb, SOS); - put_bits(&s->pb, 16, 12); /* length */ - put_bits(&s->pb, 8, 3); /* 3 components */ - - /* Y component */ - put_bits(&s->pb, 8, 1); /* index */ - put_bits(&s->pb, 4, 0); /* DC huffman table index */ - put_bits(&s->pb, 4, 0); /* AC huffman table index */ - - /* Cb component */ - put_bits(&s->pb, 8, 2); /* index */ - put_bits(&s->pb, 4, 1); /* DC huffman table index */ - put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */ - - /* Cr component */ - put_bits(&s->pb, 8, 3); /* index */ - put_bits(&s->pb, 4, 1); /* DC huffman table index */ - put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */ - - put_bits(&s->pb, 8, lossless ? s->avctx->prediction_method+1 : 0); /* Ss (not used) */ - put_bits(&s->pb, 8, lossless ? 0 : 63); /* Se (not used) */ - put_bits(&s->pb, 8, 0); /* Ah/Al (not used) */ -} - -static void escape_FF(MpegEncContext *s, int start) -{ - int size= get_bit_count(&s->pb) - start*8; - int i, ff_count; - uint8_t *buf= s->pb.buf + start; - int align= (-(size_t)(buf))&3; - - assert((size&7) == 0); - size >>= 3; - - ff_count=0; - for(i=0; i>4))&0x0F0F0F0F)+0x01010101)&0x10101010; - v= *(uint32_t*)(&buf[i+4]); - acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010; - v= *(uint32_t*)(&buf[i+8]); - acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010; - v= *(uint32_t*)(&buf[i+12]); - acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010; - - acc>>=4; - acc+= (acc>>16); - acc+= (acc>>8); - ff_count+= acc&0xFF; - } - for(; ipb, 32, 0); - put_bits(&s->pb, (ff_count-i)*8, 0); - flush_put_bits(&s->pb); - - for(i=size-1; ff_count; i--){ - int v= buf[i]; - - if(v==0xFF){ -//printf("%d %d\n", i, ff_count); - buf[i+ff_count]= 0; - ff_count--; - } - - buf[i+ff_count]= v; - } -} - -void mjpeg_picture_trailer(MpegEncContext *s) -{ - int pad= (-get_bit_count(&s->pb))&7; - - put_bits(&s->pb, pad,0xFF>>(8-pad)); - flush_put_bits(&s->pb); - - assert((s->header_bits&7)==0); - - escape_FF(s, s->header_bits>>3); - - put_marker(&s->pb, EOI); -} - -static inline void mjpeg_encode_dc(MpegEncContext *s, int val, - uint8_t *huff_size, uint16_t *huff_code) -{ - int mant, nbits; - - if (val == 0) { - put_bits(&s->pb, huff_size[0], huff_code[0]); - } else { - mant = val; - if (val < 0) { - val = -val; - mant--; - } - - nbits= av_log2_16bit(val) + 1; - - put_bits(&s->pb, huff_size[nbits], huff_code[nbits]); - - put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1)); - } -} - -static void encode_block(MpegEncContext *s, DCTELEM *block, int n) -{ - int mant, nbits, code, i, j; - int component, dc, run, last_index, val; - MJpegContext *m = s->mjpeg_ctx; - uint8_t *huff_size_ac; - uint16_t *huff_code_ac; - - /* DC coef */ - component = (n <= 3 ? 0 : n - 4 + 1); - dc = block[0]; /* overflow is impossible */ - val = dc - s->last_dc[component]; - if (n < 4) { - mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance); - huff_size_ac = m->huff_size_ac_luminance; - huff_code_ac = m->huff_code_ac_luminance; - } else { - mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance); - huff_size_ac = m->huff_size_ac_chrominance; - huff_code_ac = m->huff_code_ac_chrominance; - } - s->last_dc[component] = dc; - - /* AC coefs */ - - run = 0; - last_index = s->block_last_index[n]; - for(i=1;i<=last_index;i++) { - j = s->intra_scantable.permutated[i]; - val = block[j]; - if (val == 0) { - run++; - } else { - while (run >= 16) { - put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]); - run -= 16; - } - mant = val; - if (val < 0) { - val = -val; - mant--; - } - - nbits= av_log2(val) + 1; - code = (run << 4) | nbits; - - put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]); - - put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1)); - run = 0; - } - } - - /* output EOB only if not already 64 values */ - if (last_index < 63 || run != 0) - put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]); -} - -void mjpeg_encode_mb(MpegEncContext *s, - DCTELEM block[6][64]) -{ - int i; - for(i=0;i<6;i++) { - encode_block(s, block[i], i); - } -} - -static int encode_picture_lossless(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){ - MpegEncContext * const s = avctx->priv_data; - MJpegContext * const m = s->mjpeg_ctx; - AVFrame *pict = data; - const int width= s->width; - const int height= s->height; - AVFrame * const p= (AVFrame*)&s->current_picture; - const int predictor= avctx->prediction_method+1; - - init_put_bits(&s->pb, buf, buf_size); - - *p = *pict; - p->pict_type= FF_I_TYPE; - p->key_frame= 1; - - mjpeg_picture_header(s); - - s->header_bits= get_bit_count(&s->pb); - - if(avctx->pix_fmt == PIX_FMT_RGBA32){ - int x, y, i; - const int linesize= p->linesize[0]; - uint16_t buffer[2048][4]; - int left[3], top[3], topleft[3]; - - for(i=0; i<3; i++){ - buffer[0][i]= 1 << (9 - 1); - } - - for(y = 0; y < height; y++) { - const int modified_predictor= y ? predictor : 1; - uint8_t *ptr = p->data[0] + (linesize * y); - - for(i=0; i<3; i++){ - top[i]= left[i]= topleft[i]= buffer[0][i]; - } - for(x = 0; x < width; x++) { - buffer[x][1] = ptr[4*x+0] - ptr[4*x+1] + 0x100; - buffer[x][2] = ptr[4*x+2] - ptr[4*x+1] + 0x100; - buffer[x][0] = (ptr[4*x+0] + 2*ptr[4*x+1] + ptr[4*x+2])>>2; - - for(i=0;i<3;i++) { - int pred, diff; - - PREDICT(pred, topleft[i], top[i], left[i], modified_predictor); - - topleft[i]= top[i]; - top[i]= buffer[x+1][i]; - - left[i]= buffer[x][i]; - - diff= ((left[i] - pred + 0x100)&0x1FF) - 0x100; - - if(i==0) - mjpeg_encode_dc(s, diff, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly - else - mjpeg_encode_dc(s, diff, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance); - } - } - } - }else{ - int mb_x, mb_y, i; - const int mb_width = (width + s->mjpeg_hsample[0] - 1) / s->mjpeg_hsample[0]; - const int mb_height = (height + s->mjpeg_vsample[0] - 1) / s->mjpeg_vsample[0]; - - for(mb_y = 0; mb_y < mb_height; mb_y++) { - for(mb_x = 0; mb_x < mb_width; mb_x++) { - if(mb_x==0 || mb_y==0){ - for(i=0;i<3;i++) { - uint8_t *ptr; - int x, y, h, v, linesize; - h = s->mjpeg_hsample[i]; - v = s->mjpeg_vsample[i]; - linesize= p->linesize[i]; - - for(y=0; ydata[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap - if(y==0 && mb_y==0){ - if(x==0 && mb_x==0){ - pred= 128; - }else{ - pred= ptr[-1]; - } - }else{ - if(x==0 && mb_x==0){ - pred= ptr[-linesize]; - }else{ - PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor); - } - } - - if(i==0) - mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly - else - mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_chrominance, m->huff_code_dc_chrominance); - } - } - } - }else{ - for(i=0;i<3;i++) { - uint8_t *ptr; - int x, y, h, v, linesize; - h = s->mjpeg_hsample[i]; - v = s->mjpeg_vsample[i]; - linesize= p->linesize[i]; - - for(y=0; ydata[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap -//printf("%d %d %d %d %8X\n", mb_x, mb_y, x, y, ptr); - PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor); - - if(i==0) - mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly - else - mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_chrominance, m->huff_code_dc_chrominance); - } - } - } - } - } - } - } - - emms_c(); - - mjpeg_picture_trailer(s); - s->picture_number++; - - flush_put_bits(&s->pb); - return pbBufPtr(&s->pb) - s->pb.buf; -// return (get_bit_count(&f->pb)+7)/8; -} - -#endif //CONFIG_ENCODERS - -/******************************************/ -/* decoding */ - -#define MAX_COMPONENTS 4 - -typedef struct MJpegDecodeContext { - AVCodecContext *avctx; - GetBitContext gb; - int mpeg_enc_ctx_allocated; /* true if decoding context allocated */ - - int start_code; /* current start code */ - int buffer_size; - uint8_t *buffer; - - int16_t quant_matrixes[4][64]; - VLC vlcs[2][4]; - int qscale[4]; ///< quantizer scale calculated from quant_matrixes - - int org_height; /* size given at codec init */ - int first_picture; /* true if decoding first picture */ - int interlaced; /* true if interlaced */ - int bottom_field; /* true if bottom field */ - int lossless; - int rgb; - int rct; /* standard rct */ - int pegasus_rct; /* pegasus reversible colorspace transform */ - int bits; /* bits per component */ - - int width, height; - int mb_width, mb_height; - int nb_components; - int component_id[MAX_COMPONENTS]; - int h_count[MAX_COMPONENTS]; /* horizontal and vertical count for each component */ - int v_count[MAX_COMPONENTS]; - int comp_index[MAX_COMPONENTS]; - int dc_index[MAX_COMPONENTS]; - int ac_index[MAX_COMPONENTS]; - int nb_blocks[MAX_COMPONENTS]; - int h_scount[MAX_COMPONENTS]; - int v_scount[MAX_COMPONENTS]; - int h_max, v_max; /* maximum h and v counts */ - int quant_index[4]; /* quant table index for each component */ - int last_dc[MAX_COMPONENTS]; /* last DEQUANTIZED dc (XXX: am I right to do that ?) */ - AVFrame picture; /* picture structure */ - int linesize[MAX_COMPONENTS]; ///< linesize << interlaced - uint8_t *qscale_table; - DCTELEM block[64] __align8; - ScanTable scantable; - void (*idct_put)(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/); - - int restart_interval; - int restart_count; - - int buggy_avid; - int interlace_polarity; -} MJpegDecodeContext; - -static int mjpeg_decode_dht(MJpegDecodeContext *s); - -static int build_vlc(VLC *vlc, const uint8_t *bits_table, const uint8_t *val_table, - int nb_codes) -{ - uint8_t huff_size[256]; - uint16_t huff_code[256]; - - memset(huff_size, 0, sizeof(huff_size)); - build_huffman_codes(huff_size, huff_code, bits_table, val_table); - - return init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2); -} - -static int mjpeg_decode_init(AVCodecContext *avctx) -{ - MJpegDecodeContext *s = avctx->priv_data; - MpegEncContext s2; - - s->avctx = avctx; - - /* ugly way to get the idct & scantable FIXME */ - memset(&s2, 0, sizeof(MpegEncContext)); - s2.avctx= avctx; -// s2->out_format = FMT_MJPEG; - s2.width = 8; - s2.height = 8; - if (MPV_common_init(&s2) < 0) - return -1; - s->scantable= s2.intra_scantable; - s->idct_put= s2.dsp.idct_put; - MPV_common_end(&s2); - - s->mpeg_enc_ctx_allocated = 0; - s->buffer_size = 102400; /* smaller buffer should be enough, - but photojpg files could ahive bigger sizes */ - s->buffer = av_malloc(s->buffer_size); - if (!s->buffer) - return -1; - s->start_code = -1; - s->first_picture = 1; - s->org_height = avctx->height; - - build_vlc(&s->vlcs[0][0], bits_dc_luminance, val_dc_luminance, 12); - build_vlc(&s->vlcs[0][1], bits_dc_chrominance, val_dc_chrominance, 12); - build_vlc(&s->vlcs[1][0], bits_ac_luminance, val_ac_luminance, 251); - build_vlc(&s->vlcs[1][1], bits_ac_chrominance, val_ac_chrominance, 251); - - if (avctx->flags & CODEC_FLAG_EXTERN_HUFF) - { - av_log(avctx, AV_LOG_INFO, "mjpeg: using external huffman table\n"); - init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size*8); - mjpeg_decode_dht(s); - /* should check for error - but dunno */ - } - - return 0; -} - -/* quantize tables */ -static int mjpeg_decode_dqt(MJpegDecodeContext *s) -{ - int len, index, i, j; - - len = get_bits(&s->gb, 16) - 2; - - while (len >= 65) { - /* only 8 bit precision handled */ - if (get_bits(&s->gb, 4) != 0) - { - dprintf("dqt: 16bit precision\n"); - return -1; - } - index = get_bits(&s->gb, 4); - if (index >= 4) - return -1; - dprintf("index=%d\n", index); - /* read quant table */ - for(i=0;i<64;i++) { - j = s->scantable.permutated[i]; - s->quant_matrixes[index][j] = get_bits(&s->gb, 8); - } - - //XXX FIXME finetune, and perhaps add dc too - s->qscale[index]= FFMAX( - s->quant_matrixes[index][s->scantable.permutated[1]], - s->quant_matrixes[index][s->scantable.permutated[8]]) >> 1; - dprintf("qscale[%d]: %d\n", index, s->qscale[index]); - len -= 65; - } - - return 0; -} - -/* decode huffman tables and build VLC decoders */ -static int mjpeg_decode_dht(MJpegDecodeContext *s) -{ - int len, index, i, class, n, v, code_max; - uint8_t bits_table[17]; - uint8_t val_table[256]; - - len = get_bits(&s->gb, 16) - 2; - - while (len > 0) { - if (len < 17) - return -1; - class = get_bits(&s->gb, 4); - if (class >= 2) - return -1; - index = get_bits(&s->gb, 4); - if (index >= 4) - return -1; - n = 0; - for(i=1;i<=16;i++) { - bits_table[i] = get_bits(&s->gb, 8); - n += bits_table[i]; - } - len -= 17; - if (len < n || n > 256) - return -1; - - code_max = 0; - for(i=0;igb, 8); - if (v > code_max) - code_max = v; - val_table[i] = v; - } - len -= n; - - /* build VLC and flush previous vlc if present */ - free_vlc(&s->vlcs[class][index]); - dprintf("class=%d index=%d nb_codes=%d\n", - class, index, code_max + 1); - if(build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1) < 0){ - return -1; - } - } - return 0; -} - -static int mjpeg_decode_sof(MJpegDecodeContext *s) -{ - int len, nb_components, i, width, height; - - /* XXX: verify len field validity */ - len = get_bits(&s->gb, 16); - s->bits= get_bits(&s->gb, 8); - - if(s->pegasus_rct) s->bits=9; - if(s->bits==9 && !s->pegasus_rct) s->rct=1; //FIXME ugly - - if (s->bits != 8 && !s->lossless){ - av_log(s->avctx, AV_LOG_ERROR, "only 8 bits/component accepted\n"); - return -1; - } - height = get_bits(&s->gb, 16); - width = get_bits(&s->gb, 16); - dprintf("sof0: picture: %dx%d\n", width, height); - - nb_components = get_bits(&s->gb, 8); - if (nb_components <= 0 || - nb_components > MAX_COMPONENTS) - return -1; - s->nb_components = nb_components; - s->h_max = 1; - s->v_max = 1; - for(i=0;icomponent_id[i] = get_bits(&s->gb, 8) - 1; - s->h_count[i] = get_bits(&s->gb, 4); - s->v_count[i] = get_bits(&s->gb, 4); - /* compute hmax and vmax (only used in interleaved case) */ - if (s->h_count[i] > s->h_max) - s->h_max = s->h_count[i]; - if (s->v_count[i] > s->v_max) - s->v_max = s->v_count[i]; - s->quant_index[i] = get_bits(&s->gb, 8); - if (s->quant_index[i] >= 4) - return -1; - dprintf("component %d %d:%d id: %d quant:%d\n", i, s->h_count[i], - s->v_count[i], s->component_id[i], s->quant_index[i]); - } - - if(s->v_max==1 && s->h_max==1 && s->lossless==1) s->rgb=1; - - /* if different size, realloc/alloc picture */ - /* XXX: also check h_count and v_count */ - if (width != s->width || height != s->height) { - av_freep(&s->qscale_table); - - s->width = width; - s->height = height; - s->avctx->width = s->width; - s->avctx->height = s->height; - - /* test interlaced mode */ - if (s->first_picture && - s->org_height != 0 && - s->height < ((s->org_height * 3) / 4)) { - s->interlaced = 1; -// s->bottom_field = (s->interlace_polarity) ? 1 : 0; - s->bottom_field = 0; - s->avctx->height *= 2; - } - - s->qscale_table= av_mallocz((s->width+15)/16); - - s->first_picture = 0; - } - - if(s->interlaced && s->bottom_field) - return 0; - - /* XXX: not complete test ! */ - switch((s->h_count[0] << 4) | s->v_count[0]) { - case 0x11: - if(s->rgb){ - s->avctx->pix_fmt = PIX_FMT_RGBA32; - }else if(s->nb_components==3) - s->avctx->pix_fmt = PIX_FMT_YUV444P; - else - s->avctx->pix_fmt = PIX_FMT_GRAY8; - break; - case 0x21: - s->avctx->pix_fmt = PIX_FMT_YUV422P; - break; - default: - case 0x22: - s->avctx->pix_fmt = PIX_FMT_YUV420P; - break; - } - - if(s->picture.data[0]) - s->avctx->release_buffer(s->avctx, &s->picture); - - s->picture.reference= 0; - if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){ - av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n"); - return -1; - } - s->picture.pict_type= I_TYPE; - s->picture.key_frame= 1; - - for(i=0; i<3; i++){ - s->linesize[i]= s->picture.linesize[i] << s->interlaced; - } - -// printf("%d %d %d %d %d %d\n", s->width, s->height, s->linesize[0], s->linesize[1], s->interlaced, s->avctx->height); - - if (len != (8+(3*nb_components))) - { - dprintf("decode_sof0: error, len(%d) mismatch\n", len); - } - - return 0; -} - -static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index) -{ - int code; - code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2); - if (code < 0) - { - dprintf("mjpeg_decode_dc: bad vlc: %d:%d (%p)\n", 0, dc_index, - &s->vlcs[0][dc_index]); - return 0xffff; - } - - if(code) - return get_xbits(&s->gb, code); - else - return 0; -} - -/* decode block and dequantize */ -static int decode_block(MJpegDecodeContext *s, DCTELEM *block, - int component, int dc_index, int ac_index, int quant_index) -{ - int code, i, j, level, val; - VLC *ac_vlc; - int16_t *quant_matrix; - - /* DC coef */ - val = mjpeg_decode_dc(s, dc_index); - if (val == 0xffff) { - dprintf("error dc\n"); - return -1; - } - quant_matrix = s->quant_matrixes[quant_index]; - val = val * quant_matrix[0] + s->last_dc[component]; - s->last_dc[component] = val; - block[0] = val; - /* AC coefs */ - ac_vlc = &s->vlcs[1][ac_index]; - i = 1; - for(;;) { - code = get_vlc2(&s->gb, s->vlcs[1][ac_index].table, 9, 2); - - if (code < 0) { - dprintf("error ac\n"); - return -1; - } - /* EOB */ - if (code == 0) - break; - if (code == 0xf0) { - i += 16; - } else { - level = get_xbits(&s->gb, code & 0xf); - i += code >> 4; - if (i >= 64) { - dprintf("error count: %d\n", i); - return -1; - } - j = s->scantable.permutated[i]; - block[j] = level * quant_matrix[j]; - i++; - if (i >= 64) - break; - } - } - return 0; -} - -static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int predictor, int point_transform){ - int i, mb_x, mb_y; - uint16_t buffer[2048][4]; - int left[3], top[3], topleft[3]; - const int linesize= s->linesize[0]; - const int mask= (1<bits)-1; - - for(i=0; i<3; i++){ - buffer[0][i]= 1 << (s->bits + point_transform - 1); - } - for(mb_y = 0; mb_y < s->mb_height; mb_y++) { - const int modified_predictor= mb_y ? predictor : 1; - uint8_t *ptr = s->picture.data[0] + (linesize * mb_y); - - if (s->interlaced && s->bottom_field) - ptr += linesize >> 1; - - for(i=0; i<3; i++){ - top[i]= left[i]= topleft[i]= buffer[0][i]; - } - for(mb_x = 0; mb_x < s->mb_width; mb_x++) { - if (s->restart_interval && !s->restart_count) - s->restart_count = s->restart_interval; - - for(i=0;i<3;i++) { - int pred; - - topleft[i]= top[i]; - top[i]= buffer[mb_x][i]; - - PREDICT(pred, topleft[i], top[i], left[i], modified_predictor); - - left[i]= - buffer[mb_x][i]= mask & (pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform)); - } - - if (s->restart_interval && !--s->restart_count) { - align_get_bits(&s->gb); - skip_bits(&s->gb, 16); /* skip RSTn */ - } - } - - if(s->rct){ - for(mb_x = 0; mb_x < s->mb_width; mb_x++) { - ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200)>>2); - ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1]; - ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1]; - } - }else if(s->pegasus_rct){ - for(mb_x = 0; mb_x < s->mb_width; mb_x++) { - ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2])>>2); - ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1]; - ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1]; - } - }else{ - for(mb_x = 0; mb_x < s->mb_width; mb_x++) { - ptr[4*mb_x+0] = buffer[mb_x][0]; - ptr[4*mb_x+1] = buffer[mb_x][1]; - ptr[4*mb_x+2] = buffer[mb_x][2]; - } - } - } - return 0; -} - -static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor, int point_transform){ - int i, mb_x, mb_y; - const int nb_components=3; - - for(mb_y = 0; mb_y < s->mb_height; mb_y++) { - for(mb_x = 0; mb_x < s->mb_width; mb_x++) { - if (s->restart_interval && !s->restart_count) - s->restart_count = s->restart_interval; - - if(mb_x==0 || mb_y==0 || s->interlaced){ - for(i=0;inb_blocks[i]; - c = s->comp_index[i]; - h = s->h_scount[i]; - v = s->v_scount[i]; - x = 0; - y = 0; - linesize= s->linesize[c]; - - for(j=0; jpicture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap - if(y==0 && mb_y==0){ - if(x==0 && mb_x==0){ - pred= 128 << point_transform; - }else{ - pred= ptr[-1]; - } - }else{ - if(x==0 && mb_x==0){ - pred= ptr[-linesize]; - }else{ - PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor); - } - } - - if (s->interlaced && s->bottom_field) - ptr += linesize >> 1; - *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform); - - if (++x == h) { - x = 0; - y++; - } - } - } - }else{ - for(i=0;inb_blocks[i]; - c = s->comp_index[i]; - h = s->h_scount[i]; - v = s->v_scount[i]; - x = 0; - y = 0; - linesize= s->linesize[c]; - - for(j=0; jpicture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap - PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor); - *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform); - if (++x == h) { - x = 0; - y++; - } - } - } - } - if (s->restart_interval && !--s->restart_count) { - align_get_bits(&s->gb); - skip_bits(&s->gb, 16); /* skip RSTn */ - } - } - } - return 0; -} - -static int mjpeg_decode_scan(MJpegDecodeContext *s){ - int i, mb_x, mb_y; - const int nb_components=3; - - for(mb_y = 0; mb_y < s->mb_height; mb_y++) { - for(mb_x = 0; mb_x < s->mb_width; mb_x++) { - if (s->restart_interval && !s->restart_count) - s->restart_count = s->restart_interval; - - for(i=0;inb_blocks[i]; - c = s->comp_index[i]; - h = s->h_scount[i]; - v = s->v_scount[i]; - x = 0; - y = 0; - for(j=0;jblock, 0, sizeof(s->block)); - if (decode_block(s, s->block, i, - s->dc_index[i], s->ac_index[i], - s->quant_index[c]) < 0) { - dprintf("error y=%d x=%d\n", mb_y, mb_x); - return -1; - } -// dprintf("mb: %d %d processed\n", mb_y, mb_x); - ptr = s->picture.data[c] + - (s->linesize[c] * (v * mb_y + y) * 8) + - (h * mb_x + x) * 8; - if (s->interlaced && s->bottom_field) - ptr += s->linesize[c] >> 1; -//printf("%d %d %d %d %d %d %d %d \n", mb_x, mb_y, x, y, c, s->bottom_field, (v * mb_y + y) * 8, (h * mb_x + x) * 8); - s->idct_put(ptr, s->linesize[c], s->block); - if (++x == h) { - x = 0; - y++; - } - } - } - /* (< 1350) buggy workaround for Spectralfan.mov, should be fixed */ - if (s->restart_interval && (s->restart_interval < 1350) && - !--s->restart_count) { - align_get_bits(&s->gb); - skip_bits(&s->gb, 16); /* skip RSTn */ - for (i=0; ilast_dc[i] = 1024; - } - } - } - return 0; -} - -static int mjpeg_decode_sos(MJpegDecodeContext *s) -{ - int len, nb_components, i, h, v, predictor, point_transform; - int vmax, hmax, index, id; - const int block_size= s->lossless ? 1 : 8; - - /* XXX: verify len field validity */ - len = get_bits(&s->gb, 16); - nb_components = get_bits(&s->gb, 8); - if (len != 6+2*nb_components) - { - dprintf("decode_sos: invalid len (%d)\n", len); - return -1; - } - /* XXX: only interleaved scan accepted */ - if (nb_components != s->nb_components) - { - dprintf("decode_sos: components(%d) mismatch\n", nb_components); - return -1; - } - vmax = 0; - hmax = 0; - for(i=0;igb, 8) - 1; - dprintf("component: %d\n", id); - /* find component index */ - for(index=0;indexnb_components;index++) - if (id == s->component_id[index]) - break; - if (index == s->nb_components) - { - dprintf("decode_sos: index(%d) out of components\n", index); - return -1; - } - - s->comp_index[i] = index; - - s->nb_blocks[i] = s->h_count[index] * s->v_count[index]; - s->h_scount[i] = s->h_count[index]; - s->v_scount[i] = s->v_count[index]; - - s->dc_index[i] = get_bits(&s->gb, 4); - s->ac_index[i] = get_bits(&s->gb, 4); - - if (s->dc_index[i] < 0 || s->ac_index[i] < 0 || - s->dc_index[i] >= 4 || s->ac_index[i] >= 4) - goto out_of_range; -#if 0 //buggy - switch(s->start_code) - { - case SOF0: - if (dc_index[i] > 1 || ac_index[i] > 1) - goto out_of_range; - break; - case SOF1: - case SOF2: - if (dc_index[i] > 3 || ac_index[i] > 3) - goto out_of_range; - break; - case SOF3: - if (dc_index[i] > 3 || ac_index[i] != 0) - goto out_of_range; - break; - } -#endif - } - - predictor= get_bits(&s->gb, 8); /* lossless predictor or start of spectral (Ss) */ - skip_bits(&s->gb, 8); /* Se */ - skip_bits(&s->gb, 4); /* Ah */ - point_transform= get_bits(&s->gb, 4); /* Al */ - - for(i=0;ilast_dc[i] = 1024; - - if (nb_components > 1) { - /* interleaved stream */ - s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size); - s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size); - } else { - h = s->h_max / s->h_scount[s->comp_index[0]]; - v = s->v_max / s->v_scount[s->comp_index[0]]; - s->mb_width = (s->width + h * block_size - 1) / (h * block_size); - s->mb_height = (s->height + v * block_size - 1) / (v * block_size); - s->nb_blocks[0] = 1; - s->h_scount[0] = 1; - s->v_scount[0] = 1; - } - - if(s->avctx->debug & FF_DEBUG_PICT_INFO) - av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d\n", s->lossless ? "lossless" : "sequencial DCT", s->rgb ? "RGB" : "", predictor, point_transform); - - if(s->lossless){ - if(s->rgb){ - if(ljpeg_decode_rgb_scan(s, predictor, point_transform) < 0) - return -1; - }else{ - if(ljpeg_decode_yuv_scan(s, predictor, point_transform) < 0) - return -1; - } - }else{ - if(mjpeg_decode_scan(s) < 0) - return -1; - } - emms_c(); - return 0; - out_of_range: - dprintf("decode_sos: ac/dc index out of range\n"); - return -1; -} - -static int mjpeg_decode_dri(MJpegDecodeContext *s) -{ - if (get_bits(&s->gb, 16) != 4) - return -1; - s->restart_interval = get_bits(&s->gb, 16); - dprintf("restart interval: %d\n", s->restart_interval); - - return 0; -} - -static int mjpeg_decode_app(MJpegDecodeContext *s) -{ - int len, id; - - /* XXX: verify len field validity */ - len = get_bits(&s->gb, 16); - if (len < 5) - return -1; - - id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16); - id = be2me_32(id); - len -= 6; - - if(s->avctx->debug & FF_DEBUG_STARTCODE){ - av_log(s->avctx, AV_LOG_DEBUG, "APPx %8X\n", id); - } - - /* buggy AVID, it puts EOI only at every 10th frame */ - /* also this fourcc is used by non-avid files too, it holds some - informations, but it's always present in AVID creates files */ - if (id == ff_get_fourcc("AVI1")) - { - /* structure: - 4bytes AVI1 - 1bytes polarity - 1bytes always zero - 4bytes field_size - 4bytes field_size_less_padding - */ - s->buggy_avid = 1; -// if (s->first_picture) -// printf("mjpeg: workarounding buggy AVID\n"); - s->interlace_polarity = get_bits(&s->gb, 8); -#if 0 - skip_bits(&s->gb, 8); - skip_bits(&s->gb, 32); - skip_bits(&s->gb, 32); - len -= 10; -#endif -// if (s->interlace_polarity) -// printf("mjpeg: interlace polarity: %d\n", s->interlace_polarity); - goto out; - } - -// len -= 2; - - if (id == ff_get_fourcc("JFIF")) - { - int t_w, t_h; - skip_bits(&s->gb, 8); /* the trailing zero-byte */ - av_log(s->avctx, AV_LOG_INFO, "mjpeg: JFIF header found (version: %x.%x)\n", - get_bits(&s->gb, 8), get_bits(&s->gb, 8)); - skip_bits(&s->gb, 8); - - s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 16); - s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 16); - - t_w = get_bits(&s->gb, 8); - t_h = get_bits(&s->gb, 8); - if (t_w && t_h) - { - /* skip thumbnail */ - if (len-10-(t_w*t_h*3) > 0) - len -= t_w*t_h*3; - } - len -= 10; - goto out; - } - - if (id == ff_get_fourcc("Adob") && (get_bits(&s->gb, 8) == 'e')) - { - av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found\n"); - skip_bits(&s->gb, 16); /* version */ - skip_bits(&s->gb, 16); /* flags0 */ - skip_bits(&s->gb, 16); /* flags1 */ - skip_bits(&s->gb, 8); /* transform */ - len -= 7; - goto out; - } - - if (id == ff_get_fourcc("LJIF")){ - av_log(s->avctx, AV_LOG_INFO, "Pegasus lossless jpeg header found\n"); - skip_bits(&s->gb, 16); /* version ? */ - skip_bits(&s->gb, 16); /* unknwon always 0? */ - skip_bits(&s->gb, 16); /* unknwon always 0? */ - skip_bits(&s->gb, 16); /* unknwon always 0? */ - switch( get_bits(&s->gb, 8)){ - case 1: - s->rgb= 1; - s->pegasus_rct=0; - break; - case 2: - s->rgb= 1; - s->pegasus_rct=1; - break; - default: - av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace\n"); - } - len -= 9; - goto out; - } - - /* Apple MJPEG-A */ - if ((s->start_code == APP1) && (len > (0x28 - 8))) - { - id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16); - id = be2me_32(id); - len -= 4; - if (id == ff_get_fourcc("mjpg")) /* Apple MJPEG-A */ - { -#if 0 - skip_bits(&s->gb, 32); /* field size */ - skip_bits(&s->gb, 32); /* pad field size */ - skip_bits(&s->gb, 32); /* next off */ - skip_bits(&s->gb, 32); /* quant off */ - skip_bits(&s->gb, 32); /* huff off */ - skip_bits(&s->gb, 32); /* image off */ - skip_bits(&s->gb, 32); /* scan off */ - skip_bits(&s->gb, 32); /* data off */ -#endif - if (s->first_picture) - av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n"); - } - } - -out: - /* slow but needed for extreme adobe jpegs */ - if (len < 0) - av_log(s->avctx, AV_LOG_ERROR, "mjpeg: error, decode_app parser read over the end\n"); - while(--len > 0) - skip_bits(&s->gb, 8); - - return 0; -} - -static int mjpeg_decode_com(MJpegDecodeContext *s) -{ - /* XXX: verify len field validity */ - int len = get_bits(&s->gb, 16); - if (len >= 2 && len < 32768) { - /* XXX: any better upper bound */ - uint8_t *cbuf = av_malloc(len - 1); - if (cbuf) { - int i; - for (i = 0; i < len - 2; i++) - cbuf[i] = get_bits(&s->gb, 8); - if (i > 0 && cbuf[i-1] == '\n') - cbuf[i-1] = 0; - else - cbuf[i] = 0; - - av_log(s->avctx, AV_LOG_INFO, "mjpeg comment: '%s'\n", cbuf); - - /* buggy avid, it puts EOI only at every 10th frame */ - if (!strcmp(cbuf, "AVID")) - { - s->buggy_avid = 1; - // if (s->first_picture) - // printf("mjpeg: workarounding buggy AVID\n"); - } - - av_free(cbuf); - } - } - - return 0; -} - -#if 0 -static int valid_marker_list[] = -{ - /* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f */ -/* 0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -/* 1 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -/* 2 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -/* 3 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -/* 4 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -/* 5 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -/* 6 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -/* 7 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -/* 8 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -/* 9 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -/* a */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -/* b */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -/* c */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -/* d */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -/* e */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -/* f */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, -} -#endif - -/* return the 8 bit start code value and update the search - state. Return -1 if no start code found */ -static int find_marker(uint8_t **pbuf_ptr, uint8_t *buf_end) -{ - uint8_t *buf_ptr; - unsigned int v, v2; - int val; -#ifdef DEBUG - int skipped=0; -#endif - - buf_ptr = *pbuf_ptr; - while (buf_ptr < buf_end) { - v = *buf_ptr++; - v2 = *buf_ptr; - if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) { - val = *buf_ptr++; - goto found; - } -#ifdef DEBUG - skipped++; -#endif - } - val = -1; -found: -#ifdef DEBUG - dprintf("find_marker skipped %d bytes\n", skipped); -#endif - *pbuf_ptr = buf_ptr; - return val; -} - -static int mjpeg_decode_frame(AVCodecContext *avctx, - void *data, int *data_size, - uint8_t *buf, int buf_size) -{ - MJpegDecodeContext *s = avctx->priv_data; - uint8_t *buf_end, *buf_ptr; - int start_code; - AVFrame *picture = data; - - *data_size = 0; - - /* no supplementary picture */ - if (buf_size == 0) - return 0; - - buf_ptr = buf; - buf_end = buf + buf_size; - while (buf_ptr < buf_end) { - /* find start next marker */ - start_code = find_marker(&buf_ptr, buf_end); - { - /* EOF */ - if (start_code < 0) { - goto the_end; - } else { - dprintf("marker=%x avail_size_in_buf=%d\n", start_code, buf_end - buf_ptr); - - if ((buf_end - buf_ptr) > s->buffer_size) - { - av_free(s->buffer); - s->buffer_size = buf_end-buf_ptr; - s->buffer = av_malloc(s->buffer_size); - dprintf("buffer too small, expanding to %d bytes\n", - s->buffer_size); - } - - /* unescape buffer of SOS */ - if (start_code == SOS) - { - uint8_t *src = buf_ptr; - uint8_t *dst = s->buffer; - - while (src= 0xd0 && x <= 0xd7) - *(dst++) = x; - else if (x) - break; - } - } - init_get_bits(&s->gb, s->buffer, (dst - s->buffer)*8); - - dprintf("escaping removed %d bytes\n", - (buf_end - buf_ptr) - (dst - s->buffer)); - } - else - init_get_bits(&s->gb, buf_ptr, (buf_end - buf_ptr)*8); - - s->start_code = start_code; - if(s->avctx->debug & FF_DEBUG_STARTCODE){ - av_log(s->avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code); - } - - /* process markers */ - if (start_code >= 0xd0 && start_code <= 0xd7) { - dprintf("restart marker: %d\n", start_code&0x0f); - } else if (s->first_picture) { - /* APP fields */ - if (start_code >= 0xe0 && start_code <= 0xef) - mjpeg_decode_app(s); - /* Comment */ - else if (start_code == COM) - mjpeg_decode_com(s); - } - - switch(start_code) { - case SOI: - s->restart_interval = 0; - /* nothing to do on SOI */ - break; - case DQT: - mjpeg_decode_dqt(s); - break; - case DHT: - if(mjpeg_decode_dht(s) < 0){ - av_log(s->avctx, AV_LOG_ERROR, "huffman table decode error\n"); - return -1; - } - break; - case SOF0: - s->lossless=0; - if (mjpeg_decode_sof(s) < 0) - return -1; - break; - case SOF3: - s->lossless=1; - if (mjpeg_decode_sof(s) < 0) - return -1; - break; - case EOI: - if ((s->buggy_avid && !s->interlaced) || s->restart_interval) - break; -eoi_parser: - { - if (s->interlaced) { - s->bottom_field ^= 1; - /* if not bottom field, do not output image yet */ - if (s->bottom_field) - goto not_the_end; - } - *picture = s->picture; - *data_size = sizeof(AVFrame); - - if(!s->lossless){ - picture->quality= FFMAX(FFMAX(s->qscale[0], s->qscale[1]), s->qscale[2]); - picture->qstride= 0; - picture->qscale_table= s->qscale_table; - memset(picture->qscale_table, picture->quality, (s->width+15)/16); - if(avctx->debug & FF_DEBUG_QP) - av_log(s->avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality); - picture->quality*= FF_QP2LAMBDA; - } - - goto the_end; - } - break; - case SOS: - mjpeg_decode_sos(s); - /* buggy avid puts EOI every 10-20th frame */ - /* if restart period is over process EOI */ - if ((s->buggy_avid && !s->interlaced) || s->restart_interval) - goto eoi_parser; - break; - case DRI: - mjpeg_decode_dri(s); - break; - case SOF1: - case SOF2: - case SOF5: - case SOF6: - case SOF7: - case SOF9: - case SOF10: - case SOF11: - case SOF13: - case SOF14: - case SOF15: - case JPG: - av_log(s->avctx, AV_LOG_ERROR, "mjpeg: unsupported coding type (%x)\n", start_code); - break; -// default: -// printf("mjpeg: unsupported marker (%x)\n", start_code); -// break; - } - -not_the_end: - /* eof process start code */ - buf_ptr += (get_bits_count(&s->gb)+7)/8; - dprintf("marker parser used %d bytes (%d bits)\n", - (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb)); - } - } - } -the_end: - dprintf("mjpeg decode frame unused %d bytes\n", buf_end - buf_ptr); -// return buf_end - buf_ptr; - return buf_ptr - buf; -} - -static int mjpegb_decode_frame(AVCodecContext *avctx, - void *data, int *data_size, - uint8_t *buf, int buf_size) -{ - MJpegDecodeContext *s = avctx->priv_data; - uint8_t *buf_end, *buf_ptr; - AVFrame *picture = data; - GetBitContext hgb; /* for the header */ - uint32_t dqt_offs, dht_offs, sof_offs, sos_offs, second_field_offs; - uint32_t field_size; - - *data_size = 0; - - /* no supplementary picture */ - if (buf_size == 0) - return 0; - - buf_ptr = buf; - buf_end = buf + buf_size; - -read_header: - /* reset on every SOI */ - s->restart_interval = 0; - - init_get_bits(&hgb, buf_ptr, /*buf_size*/(buf_end - buf_ptr)*8); - - skip_bits(&hgb, 32); /* reserved zeros */ - - if (get_bits(&hgb, 32) != be2me_32(ff_get_fourcc("mjpg"))) - { - dprintf("not mjpeg-b (bad fourcc)\n"); - return 0; - } - - field_size = get_bits(&hgb, 32); /* field size */ - dprintf("field size: 0x%x\n", field_size); - skip_bits(&hgb, 32); /* padded field size */ - second_field_offs = get_bits(&hgb, 32); - dprintf("second field offs: 0x%x\n", second_field_offs); - if (second_field_offs) - s->interlaced = 1; - - dqt_offs = get_bits(&hgb, 32); - dprintf("dqt offs: 0x%x\n", dqt_offs); - if (dqt_offs) - { - init_get_bits(&s->gb, buf+dqt_offs, (buf_end - (buf+dqt_offs))*8); - s->start_code = DQT; - mjpeg_decode_dqt(s); - } - - dht_offs = get_bits(&hgb, 32); - dprintf("dht offs: 0x%x\n", dht_offs); - if (dht_offs) - { - init_get_bits(&s->gb, buf+dht_offs, (buf_end - (buf+dht_offs))*8); - s->start_code = DHT; - mjpeg_decode_dht(s); - } - - sof_offs = get_bits(&hgb, 32); - dprintf("sof offs: 0x%x\n", sof_offs); - if (sof_offs) - { - init_get_bits(&s->gb, buf+sof_offs, (buf_end - (buf+sof_offs))*8); - s->start_code = SOF0; - if (mjpeg_decode_sof(s) < 0) - return -1; - } - - sos_offs = get_bits(&hgb, 32); - dprintf("sos offs: 0x%x\n", sos_offs); - if (sos_offs) - { -// init_get_bits(&s->gb, buf+sos_offs, (buf_end - (buf+sos_offs))*8); - init_get_bits(&s->gb, buf+sos_offs, field_size*8); - s->start_code = SOS; - mjpeg_decode_sos(s); - } - - skip_bits(&hgb, 32); /* start of data offset */ - - if (s->interlaced) { - s->bottom_field ^= 1; - /* if not bottom field, do not output image yet */ - if (s->bottom_field && second_field_offs) - { - buf_ptr = buf + second_field_offs; - second_field_offs = 0; - goto read_header; - } - } - - //XXX FIXME factorize, this looks very similar to the EOI code - - *picture= s->picture; - *data_size = sizeof(AVFrame); - - if(!s->lossless){ - picture->quality= FFMAX(FFMAX(s->qscale[0], s->qscale[1]), s->qscale[2]); - picture->qstride= 0; - picture->qscale_table= s->qscale_table; - memset(picture->qscale_table, picture->quality, (s->width+15)/16); - if(avctx->debug & FF_DEBUG_QP) - av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality); - picture->quality*= FF_QP2LAMBDA; - } - - return buf_ptr - buf; -} - -#include "sp5x.h" - -static int sp5x_decode_frame(AVCodecContext *avctx, - void *data, int *data_size, - uint8_t *buf, int buf_size) -{ -#if 0 - MJpegDecodeContext *s = avctx->priv_data; -#endif - const int qscale = 5; - uint8_t *buf_ptr, *buf_end, *recoded; - int i = 0, j = 0; - - *data_size = 0; - - /* no supplementary picture */ - if (buf_size == 0) - return 0; - - if (!avctx->width || !avctx->height) - return -1; - - buf_ptr = buf; - buf_end = buf + buf_size; - -#if 1 - recoded = av_mallocz(buf_size + 1024); - if (!recoded) - return -1; - - /* SOI */ - recoded[j++] = 0xFF; - recoded[j++] = 0xD8; - - memcpy(recoded+j, &sp5x_data_dqt[0], sizeof(sp5x_data_dqt)); - memcpy(recoded+j+5, &sp5x_quant_table[qscale * 2], 64); - memcpy(recoded+j+70, &sp5x_quant_table[(qscale * 2) + 1], 64); - j += sizeof(sp5x_data_dqt); - - memcpy(recoded+j, &sp5x_data_dht[0], sizeof(sp5x_data_dht)); - j += sizeof(sp5x_data_dht); - - memcpy(recoded+j, &sp5x_data_sof[0], sizeof(sp5x_data_sof)); - recoded[j+5] = (avctx->height >> 8) & 0xFF; - recoded[j+6] = avctx->height & 0xFF; - recoded[j+7] = (avctx->width >> 8) & 0xFF; - recoded[j+8] = avctx->width & 0xFF; - j += sizeof(sp5x_data_sof); - - memcpy(recoded+j, &sp5x_data_sos[0], sizeof(sp5x_data_sos)); - j += sizeof(sp5x_data_sos); - - for (i = 14; i < buf_size && j < buf_size+1024-2; i++) - { - recoded[j++] = buf[i]; - if (buf[i] == 0xff) - recoded[j++] = 0; - } - - /* EOI */ - recoded[j++] = 0xFF; - recoded[j++] = 0xD9; - - i = mjpeg_decode_frame(avctx, data, data_size, recoded, j); - - av_free(recoded); - -#else - /* SOF */ - s->bits = 8; - s->width = avctx->width; - s->height = avctx->height; - s->nb_components = 3; - s->component_id[0] = 0; - s->h_count[0] = 2; - s->v_count[0] = 2; - s->quant_index[0] = 0; - s->component_id[1] = 1; - s->h_count[1] = 1; - s->v_count[1] = 1; - s->quant_index[1] = 1; - s->component_id[2] = 2; - s->h_count[2] = 1; - s->v_count[2] = 1; - s->quant_index[2] = 1; - s->h_max = 2; - s->v_max = 2; - - s->qscale_table = av_mallocz((s->width+15)/16); - avctx->pix_fmt = PIX_FMT_YUV420P; - s->interlaced = 0; - - s->picture.reference = 0; - if (avctx->get_buffer(avctx, &s->picture) < 0) - { - fprintf(stderr, "get_buffer() failed\n"); - return -1; - } - - s->picture.pict_type = I_TYPE; - s->picture.key_frame = 1; - - for (i = 0; i < 3; i++) - s->linesize[i] = s->picture.linesize[i] << s->interlaced; - - /* DQT */ - for (i = 0; i < 64; i++) - { - j = s->scantable.permutated[i]; - s->quant_matrixes[0][j] = sp5x_quant_table[(qscale * 2) + i]; - } - s->qscale[0] = FFMAX( - s->quant_matrixes[0][s->scantable.permutated[1]], - s->quant_matrixes[0][s->scantable.permutated[8]]) >> 1; - - for (i = 0; i < 64; i++) - { - j = s->scantable.permutated[i]; - s->quant_matrixes[1][j] = sp5x_quant_table[(qscale * 2) + 1 + i]; - } - s->qscale[1] = FFMAX( - s->quant_matrixes[1][s->scantable.permutated[1]], - s->quant_matrixes[1][s->scantable.permutated[8]]) >> 1; - - /* DHT */ - - /* SOS */ - s->comp_index[0] = 0; - s->nb_blocks[0] = s->h_count[0] * s->v_count[0]; - s->h_scount[0] = s->h_count[0]; - s->v_scount[0] = s->v_count[0]; - s->dc_index[0] = 0; - s->ac_index[0] = 0; - - s->comp_index[1] = 1; - s->nb_blocks[1] = s->h_count[1] * s->v_count[1]; - s->h_scount[1] = s->h_count[1]; - s->v_scount[1] = s->v_count[1]; - s->dc_index[1] = 1; - s->ac_index[1] = 1; - - s->comp_index[2] = 2; - s->nb_blocks[2] = s->h_count[2] * s->v_count[2]; - s->h_scount[2] = s->h_count[2]; - s->v_scount[2] = s->v_count[2]; - s->dc_index[2] = 1; - s->ac_index[2] = 1; - - for (i = 0; i < 3; i++) - s->last_dc[i] = 1024; - - s->mb_width = (s->width * s->h_max * 8 -1) / (s->h_max * 8); - s->mb_height = (s->height * s->v_max * 8 -1) / (s->v_max * 8); - - init_get_bits(&s->gb, buf+14, (buf_size-14)*8); - - return mjpeg_decode_scan(s); -#endif - - return i; -} - -static int mjpeg_decode_end(AVCodecContext *avctx) -{ - MJpegDecodeContext *s = avctx->priv_data; - int i, j; - - av_free(s->buffer); - av_free(s->qscale_table); - avcodec_default_free_buffers(avctx); - - for(i=0;i<2;i++) { - for(j=0;j<4;j++) - free_vlc(&s->vlcs[i][j]); - } - return 0; -} - -AVCodec mjpeg_decoder = { - "mjpeg", - CODEC_TYPE_VIDEO, - CODEC_ID_MJPEG, - sizeof(MJpegDecodeContext), - mjpeg_decode_init, - NULL, - mjpeg_decode_end, - mjpeg_decode_frame, - CODEC_CAP_DR1, - NULL -}; - -AVCodec mjpegb_decoder = { - "mjpegb", - CODEC_TYPE_VIDEO, - CODEC_ID_MJPEGB, - sizeof(MJpegDecodeContext), - mjpeg_decode_init, - NULL, - mjpeg_decode_end, - mjpegb_decode_frame, - CODEC_CAP_DR1, - NULL -}; - -AVCodec sp5x_decoder = { - "sp5x", - CODEC_TYPE_VIDEO, - CODEC_ID_SP5X, - sizeof(MJpegDecodeContext), - mjpeg_decode_init, - NULL, - mjpeg_decode_end, - sp5x_decode_frame, - CODEC_CAP_DR1, - NULL -}; - -#ifdef CONFIG_ENCODERS -AVCodec ljpeg_encoder = { //FIXME avoid MPV_* lossless jpeg shouldnt need them - "ljpeg", - CODEC_TYPE_VIDEO, - CODEC_ID_LJPEG, - sizeof(MpegEncContext), - MPV_encode_init, - encode_picture_lossless, - MPV_encode_end, -}; -#endif diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/mjpeg.h b/src/add-ons/media/plugins/avcodec/libavcodec/mjpeg.h new file mode 100644 index 0000000000..028f8c0d3f --- /dev/null +++ b/src/add-ons/media/plugins/avcodec/libavcodec/mjpeg.h @@ -0,0 +1,155 @@ +/* + * MJPEG encoder and decoder + * Copyright (c) 2000, 2001 Fabrice Bellard. + * Copyright (c) 2003 Alex Beregszaszi + * Copyright (c) 2003-2004 Michael Niedermayer + * + * Support for external huffman table, various fixes (AVID workaround), + * aspecting, new decode_frame mechanism and apple mjpeg-b support + * by Alex Beregszaszi + * + * This file is part of FFmpeg. + * + * FFmpeg 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.1 of the License, or (at your option) any later version. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file mjpeg.h + * MJPEG encoder and decoder. + */ + +#ifndef FFMPEG_MJPEG_H +#define FFMPEG_MJPEG_H + +#include "avcodec.h" +#include "bitstream.h" + + +/* JPEG marker codes */ +typedef enum { + /* start of frame */ + SOF0 = 0xc0, /* baseline */ + SOF1 = 0xc1, /* extended sequential, huffman */ + SOF2 = 0xc2, /* progressive, huffman */ + SOF3 = 0xc3, /* lossless, huffman */ + + SOF5 = 0xc5, /* differential sequential, huffman */ + SOF6 = 0xc6, /* differential progressive, huffman */ + SOF7 = 0xc7, /* differential lossless, huffman */ + JPG = 0xc8, /* reserved for JPEG extension */ + SOF9 = 0xc9, /* extended sequential, arithmetic */ + SOF10 = 0xca, /* progressive, arithmetic */ + SOF11 = 0xcb, /* lossless, arithmetic */ + + SOF13 = 0xcd, /* differential sequential, arithmetic */ + SOF14 = 0xce, /* differential progressive, arithmetic */ + SOF15 = 0xcf, /* differential lossless, arithmetic */ + + DHT = 0xc4, /* define huffman tables */ + + DAC = 0xcc, /* define arithmetic-coding conditioning */ + + /* restart with modulo 8 count "m" */ + RST0 = 0xd0, + RST1 = 0xd1, + RST2 = 0xd2, + RST3 = 0xd3, + RST4 = 0xd4, + RST5 = 0xd5, + RST6 = 0xd6, + RST7 = 0xd7, + + SOI = 0xd8, /* start of image */ + EOI = 0xd9, /* end of image */ + SOS = 0xda, /* start of scan */ + DQT = 0xdb, /* define quantization tables */ + DNL = 0xdc, /* define number of lines */ + DRI = 0xdd, /* define restart interval */ + DHP = 0xde, /* define hierarchical progression */ + EXP = 0xdf, /* expand reference components */ + + APP0 = 0xe0, + APP1 = 0xe1, + APP2 = 0xe2, + APP3 = 0xe3, + APP4 = 0xe4, + APP5 = 0xe5, + APP6 = 0xe6, + APP7 = 0xe7, + APP8 = 0xe8, + APP9 = 0xe9, + APP10 = 0xea, + APP11 = 0xeb, + APP12 = 0xec, + APP13 = 0xed, + APP14 = 0xee, + APP15 = 0xef, + + JPG0 = 0xf0, + JPG1 = 0xf1, + JPG2 = 0xf2, + JPG3 = 0xf3, + JPG4 = 0xf4, + JPG5 = 0xf5, + JPG6 = 0xf6, + SOF48 = 0xf7, ///< JPEG-LS + LSE = 0xf8, ///< JPEG-LS extension parameters + JPG9 = 0xf9, + JPG10 = 0xfa, + JPG11 = 0xfb, + JPG12 = 0xfc, + JPG13 = 0xfd, + + COM = 0xfe, /* comment */ + + TEM = 0x01, /* temporary private use for arithmetic coding */ + + /* 0x02 -> 0xbf reserved */ +} JPEG_MARKER; + +static inline void put_marker(PutBitContext *p, int code) +{ + put_bits(p, 8, 0xff); + put_bits(p, 8, code); +} + +#define PREDICT(ret, topleft, top, left, predictor)\ + switch(predictor){\ + case 1: ret= left; break;\ + case 2: ret= top; break;\ + case 3: ret= topleft; break;\ + case 4: ret= left + top - topleft; break;\ + case 5: ret= left + ((top - topleft)>>1); break;\ + case 6: ret= top + ((left - topleft)>>1); break;\ + default:\ + case 7: ret= (left + top)>>1; break;\ + } + +extern const uint8_t ff_mjpeg_bits_dc_luminance[]; +extern const uint8_t ff_mjpeg_val_dc[]; + +extern const uint8_t ff_mjpeg_bits_dc_chrominance[]; + +extern const uint8_t ff_mjpeg_bits_ac_luminance[]; +extern const uint8_t ff_mjpeg_val_ac_luminance[]; + +extern const uint8_t ff_mjpeg_bits_ac_chrominance[]; +extern const uint8_t ff_mjpeg_val_ac_chrominance[]; + +void ff_mjpeg_build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code, + const uint8_t *bits_table, + const uint8_t *val_table); + +#endif /* FFMPEG_MJPEG_H */ diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/mjpeg_parser.c b/src/add-ons/media/plugins/avcodec/libavcodec/mjpeg_parser.c new file mode 100644 index 0000000000..aad112eb9b --- /dev/null +++ b/src/add-ons/media/plugins/avcodec/libavcodec/mjpeg_parser.c @@ -0,0 +1,101 @@ +/* + * MJPEG parser + * Copyright (c) 2000, 2001 Fabrice Bellard. + * Copyright (c) 2003 Alex Beregszaszi + * Copyright (c) 2003-2004 Michael Niedermayer + * + * This file is part of FFmpeg. + * + * FFmpeg 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.1 of the License, or (at your option) any later version. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file mjpeg_parser.c + * MJPEG parser. + */ + +#include "parser.h" + + +/** + * finds the end of the current frame in the bitstream. + * @return the position of the first byte of the next frame, or -1 + */ +static int find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){ + int vop_found, i; + uint16_t state; + + vop_found= pc->frame_start_found; + state= pc->state; + + i=0; + if(!vop_found){ + for(i=0; iframe_start_found=0; + pc->state=0; + return i-1; + } + } + } + pc->frame_start_found= vop_found; + pc->state= state; + return END_NOT_FOUND; +} + +static int jpeg_parse(AVCodecParserContext *s, + AVCodecContext *avctx, + const uint8_t **poutbuf, int *poutbuf_size, + const uint8_t *buf, int buf_size) +{ + ParseContext *pc = s->priv_data; + int next; + + next= find_frame_end(pc, buf, buf_size); + + if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) { + *poutbuf = NULL; + *poutbuf_size = 0; + return buf_size; + } + + *poutbuf = buf; + *poutbuf_size = buf_size; + return next; +} + + +AVCodecParser mjpeg_parser = { + { CODEC_ID_MJPEG }, + sizeof(ParseContext), + NULL, + jpeg_parse, + ff_parse_close, +}; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/mjpega_dump_header_bsf.c b/src/add-ons/media/plugins/avcodec/libavcodec/mjpega_dump_header_bsf.c new file mode 100644 index 0000000000..a3f0131742 --- /dev/null +++ b/src/add-ons/media/plugins/avcodec/libavcodec/mjpega_dump_header_bsf.c @@ -0,0 +1,92 @@ +/* + * MJPEG A dump header bitstream filter + * Copyright (c) 2006 Baptiste Coudurier. + * + * This file is part of FFmpeg. + * + * FFmpeg 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.1 of the License, or (at your option) any later version. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file mjpega_dump_header_bsf.c + * MJPEG A dump header bitstream filter + * modifies bitstream to be decoded by quicktime + */ + +#include "avcodec.h" +#include "bytestream.h" +#include "mjpeg.h" + + +static int mjpega_dump_header(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args, + uint8_t **poutbuf, int *poutbuf_size, + const uint8_t *buf, int buf_size, int keyframe) +{ + uint8_t *poutbufp; + int i; + + if (avctx->codec_id != CODEC_ID_MJPEG) { + av_log(avctx, AV_LOG_ERROR, "mjpega bitstream filter only applies to mjpeg codec\n"); + return 0; + } + + *poutbuf_size = 0; + *poutbuf = av_malloc(buf_size + 44 + FF_INPUT_BUFFER_PADDING_SIZE); + poutbufp = *poutbuf; + bytestream_put_byte(&poutbufp, 0xff); + bytestream_put_byte(&poutbufp, SOI); + bytestream_put_byte(&poutbufp, 0xff); + bytestream_put_byte(&poutbufp, APP1); + bytestream_put_be16(&poutbufp, 42); /* size */ + bytestream_put_be32(&poutbufp, 0); + bytestream_put_buffer(&poutbufp, "mjpg", 4); + bytestream_put_be32(&poutbufp, buf_size + 44); /* field size */ + bytestream_put_be32(&poutbufp, buf_size + 44); /* pad field size */ + bytestream_put_be32(&poutbufp, 0); /* next ptr */ + + for (i = 0; i < buf_size - 1; i++) { + if (buf[i] == 0xff) { + switch (buf[i + 1]) { + case DQT: /* quant off */ + case DHT: /* huff off */ + case SOF0: /* image off */ + bytestream_put_be32(&poutbufp, i + 46); + break; + case SOS: + bytestream_put_be32(&poutbufp, i + 46); /* scan off */ + bytestream_put_be32(&poutbufp, i + 46 + AV_RB16(buf + i + 2)); /* data off */ + bytestream_put_buffer(&poutbufp, buf + 2, buf_size - 2); /* skip already written SOI */ + *poutbuf_size = poutbufp - *poutbuf; + return 1; + case APP1: + if (i + 8 < buf_size && AV_RL32(buf + i + 8) == ff_get_fourcc("mjpg")) { + av_log(avctx, AV_LOG_ERROR, "bitstream already formatted\n"); + memcpy(*poutbuf, buf, buf_size); + *poutbuf_size = buf_size; + return 1; + } + } + } + } + av_freep(poutbuf); + av_log(avctx, AV_LOG_ERROR, "could not find SOS marker in bitstream\n"); + return 0; +} + +AVBitStreamFilter mjpega_dump_header_bsf = { + "mjpegadump", + 0, + mjpega_dump_header, +}; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/mjpegbdec.c b/src/add-ons/media/plugins/avcodec/libavcodec/mjpegbdec.c new file mode 100644 index 0000000000..4a6c0d857e --- /dev/null +++ b/src/add-ons/media/plugins/avcodec/libavcodec/mjpegbdec.c @@ -0,0 +1,150 @@ +/* + * Apple MJPEG-B decoder + * Copyright (c) 2002 Alex Beregszaszi + * + * This file is part of FFmpeg. + * + * FFmpeg 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.1 of the License, or (at your option) any later version. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file mjpegbdec.c + * Apple MJPEG-B decoder. + */ + +#include "avcodec.h" +#include "mjpeg.h" +#include "mjpegdec.h" + + +static int mjpegb_decode_frame(AVCodecContext *avctx, + void *data, int *data_size, + const uint8_t *buf, int buf_size) +{ + MJpegDecodeContext *s = avctx->priv_data; + const uint8_t *buf_end, *buf_ptr; + AVFrame *picture = data; + GetBitContext hgb; /* for the header */ + uint32_t dqt_offs, dht_offs, sof_offs, sos_offs, second_field_offs; + uint32_t field_size, sod_offs; + + buf_ptr = buf; + buf_end = buf + buf_size; + +read_header: + /* reset on every SOI */ + s->restart_interval = 0; + s->restart_count = 0; + s->mjpb_skiptosod = 0; + + init_get_bits(&hgb, buf_ptr, /*buf_size*/(buf_end - buf_ptr)*8); + + skip_bits(&hgb, 32); /* reserved zeros */ + + if (get_bits_long(&hgb, 32) != MKBETAG('m','j','p','g')) + { + av_log(avctx, AV_LOG_WARNING, "not mjpeg-b (bad fourcc)\n"); + return 0; + } + + field_size = get_bits_long(&hgb, 32); /* field size */ + av_log(avctx, AV_LOG_DEBUG, "field size: 0x%x\n", field_size); + skip_bits(&hgb, 32); /* padded field size */ + second_field_offs = get_bits_long(&hgb, 32); + av_log(avctx, AV_LOG_DEBUG, "second field offs: 0x%x\n", second_field_offs); + + dqt_offs = get_bits_long(&hgb, 32); + av_log(avctx, AV_LOG_DEBUG, "dqt offs: 0x%x\n", dqt_offs); + if (dqt_offs) + { + init_get_bits(&s->gb, buf_ptr+dqt_offs, (buf_end - (buf_ptr+dqt_offs))*8); + s->start_code = DQT; + ff_mjpeg_decode_dqt(s); + } + + dht_offs = get_bits_long(&hgb, 32); + av_log(avctx, AV_LOG_DEBUG, "dht offs: 0x%x\n", dht_offs); + if (dht_offs) + { + init_get_bits(&s->gb, buf_ptr+dht_offs, (buf_end - (buf_ptr+dht_offs))*8); + s->start_code = DHT; + ff_mjpeg_decode_dht(s); + } + + sof_offs = get_bits_long(&hgb, 32); + av_log(avctx, AV_LOG_DEBUG, "sof offs: 0x%x\n", sof_offs); + if (sof_offs) + { + init_get_bits(&s->gb, buf_ptr+sof_offs, (buf_end - (buf_ptr+sof_offs))*8); + s->start_code = SOF0; + if (ff_mjpeg_decode_sof(s) < 0) + return -1; + } + + sos_offs = get_bits_long(&hgb, 32); + av_log(avctx, AV_LOG_DEBUG, "sos offs: 0x%x\n", sos_offs); + sod_offs = get_bits_long(&hgb, 32); + av_log(avctx, AV_LOG_DEBUG, "sod offs: 0x%x\n", sod_offs); + if (sos_offs) + { +// init_get_bits(&s->gb, buf+sos_offs, (buf_end - (buf+sos_offs))*8); + init_get_bits(&s->gb, buf_ptr+sos_offs, field_size*8); + s->mjpb_skiptosod = (sod_offs - sos_offs - show_bits(&s->gb, 16)); + s->start_code = SOS; + ff_mjpeg_decode_sos(s); + } + + if (s->interlaced) { + s->bottom_field ^= 1; + /* if not bottom field, do not output image yet */ + if (s->bottom_field != s->interlace_polarity && second_field_offs) + { + buf_ptr = buf + second_field_offs; + second_field_offs = 0; + goto read_header; + } + } + + //XXX FIXME factorize, this looks very similar to the EOI code + + *picture= s->picture; + *data_size = sizeof(AVFrame); + + if(!s->lossless){ + picture->quality= FFMAX3(s->qscale[0], s->qscale[1], s->qscale[2]); + picture->qstride= 0; + picture->qscale_table= s->qscale_table; + memset(picture->qscale_table, picture->quality, (s->width+15)/16); + if(avctx->debug & FF_DEBUG_QP) + av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality); + picture->quality*= FF_QP2LAMBDA; + } + + return buf_ptr - buf; +} + +AVCodec mjpegb_decoder = { + "mjpegb", + CODEC_TYPE_VIDEO, + CODEC_ID_MJPEGB, + sizeof(MJpegDecodeContext), + ff_mjpeg_decode_init, + NULL, + ff_mjpeg_decode_end, + mjpegb_decode_frame, + CODEC_CAP_DR1, + NULL, + .long_name = NULL_IF_CONFIG_SMALL("Apple MJPEG-B"), +}; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/mjpegdec.c b/src/add-ons/media/plugins/avcodec/libavcodec/mjpegdec.c new file mode 100644 index 0000000000..b302c8a05a --- /dev/null +++ b/src/add-ons/media/plugins/avcodec/libavcodec/mjpegdec.c @@ -0,0 +1,1383 @@ +/* + * MJPEG decoder + * Copyright (c) 2000, 2001 Fabrice Bellard. + * Copyright (c) 2003 Alex Beregszaszi + * Copyright (c) 2003-2004 Michael Niedermayer + * + * Support for external huffman table, various fixes (AVID workaround), + * aspecting, new decode_frame mechanism and apple mjpeg-b support + * by Alex Beregszaszi + * + * This file is part of FFmpeg. + * + * FFmpeg 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.1 of the License, or (at your option) any later version. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file mjpegdec.c + * MJPEG decoder. + */ + +//#define DEBUG +#include + +#include "avcodec.h" +#include "dsputil.h" +#include "mjpeg.h" +#include "mjpegdec.h" +#include "jpeglsdec.h" + + +static int build_vlc(VLC *vlc, const uint8_t *bits_table, const uint8_t *val_table, + int nb_codes, int use_static, int is_ac) +{ + uint8_t huff_size[256+16]; + uint16_t huff_code[256+16]; + + assert(nb_codes <= 256); + + memset(huff_size, 0, sizeof(huff_size)); + ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table); + + if(is_ac){ + memmove(huff_size+16, huff_size, sizeof(uint8_t)*nb_codes); + memmove(huff_code+16, huff_code, sizeof(uint16_t)*nb_codes); + memset(huff_size, 0, sizeof(uint8_t)*16); + memset(huff_code, 0, sizeof(uint16_t)*16); + nb_codes += 16; + } + + return init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2, use_static); +} + +static void build_basic_mjpeg_vlc(MJpegDecodeContext * s) { + build_vlc(&s->vlcs[0][0], ff_mjpeg_bits_dc_luminance, + ff_mjpeg_val_dc, 12, 0, 0); + build_vlc(&s->vlcs[0][1], ff_mjpeg_bits_dc_chrominance, + ff_mjpeg_val_dc, 12, 0, 0); + build_vlc(&s->vlcs[1][0], ff_mjpeg_bits_ac_luminance, + ff_mjpeg_val_ac_luminance, 251, 0, 1); + build_vlc(&s->vlcs[1][1], ff_mjpeg_bits_ac_chrominance, + ff_mjpeg_val_ac_chrominance, 251, 0, 1); +} + +av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx) +{ + MJpegDecodeContext *s = avctx->priv_data; + + s->avctx = avctx; + dsputil_init(&s->dsp, avctx); + ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct); + s->buffer_size = 0; + s->buffer = NULL; + s->start_code = -1; + s->first_picture = 1; + s->org_height = avctx->coded_height; + + build_basic_mjpeg_vlc(s); + + if (avctx->flags & CODEC_FLAG_EXTERN_HUFF) + { + av_log(avctx, AV_LOG_INFO, "mjpeg: using external huffman table\n"); + init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size*8); + if (ff_mjpeg_decode_dht(s)) { + av_log(avctx, AV_LOG_ERROR, "mjpeg: error using external huffman table, switching back to internal\n"); + build_basic_mjpeg_vlc(s); + } + } + if (avctx->extradata_size > 9 && + AV_RL32(avctx->extradata + 4) == MKTAG('f','i','e','l')) { + if (avctx->extradata[9] == 6) { /* quicktime icefloe 019 */ + s->interlace_polarity = 1; /* bottom field first */ + av_log(avctx, AV_LOG_DEBUG, "mjpeg bottom field first\n"); + } + } + + return 0; +} + + +/* quantize tables */ +int ff_mjpeg_decode_dqt(MJpegDecodeContext *s) +{ + int len, index, i, j; + + len = get_bits(&s->gb, 16) - 2; + + while (len >= 65) { + /* only 8 bit precision handled */ + if (get_bits(&s->gb, 4) != 0) + { + av_log(s->avctx, AV_LOG_ERROR, "dqt: 16bit precision\n"); + return -1; + } + index = get_bits(&s->gb, 4); + if (index >= 4) + return -1; + av_log(s->avctx, AV_LOG_DEBUG, "index=%d\n", index); + /* read quant table */ + for(i=0;i<64;i++) { + j = s->scantable.permutated[i]; + s->quant_matrixes[index][j] = get_bits(&s->gb, 8); + } + + //XXX FIXME finetune, and perhaps add dc too + s->qscale[index]= FFMAX( + s->quant_matrixes[index][s->scantable.permutated[1]], + s->quant_matrixes[index][s->scantable.permutated[8]]) >> 1; + av_log(s->avctx, AV_LOG_DEBUG, "qscale[%d]: %d\n", index, s->qscale[index]); + len -= 65; + } + + return 0; +} + +/* decode huffman tables and build VLC decoders */ +int ff_mjpeg_decode_dht(MJpegDecodeContext *s) +{ + int len, index, i, class, n, v, code_max; + uint8_t bits_table[17]; + uint8_t val_table[256]; + + len = get_bits(&s->gb, 16) - 2; + + while (len > 0) { + if (len < 17) + return -1; + class = get_bits(&s->gb, 4); + if (class >= 2) + return -1; + index = get_bits(&s->gb, 4); + if (index >= 4) + return -1; + n = 0; + for(i=1;i<=16;i++) { + bits_table[i] = get_bits(&s->gb, 8); + n += bits_table[i]; + } + len -= 17; + if (len < n || n > 256) + return -1; + + code_max = 0; + for(i=0;igb, 8); + if (v > code_max) + code_max = v; + val_table[i] = v; + } + len -= n; + + /* build VLC and flush previous vlc if present */ + free_vlc(&s->vlcs[class][index]); + av_log(s->avctx, AV_LOG_DEBUG, "class=%d index=%d nb_codes=%d\n", + class, index, code_max + 1); + if(build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1, 0, class > 0) < 0){ + return -1; + } + } + return 0; +} + +int ff_mjpeg_decode_sof(MJpegDecodeContext *s) +{ + int len, nb_components, i, width, height, pix_fmt_id; + + /* XXX: verify len field validity */ + len = get_bits(&s->gb, 16); + s->bits= get_bits(&s->gb, 8); + + if(s->pegasus_rct) s->bits=9; + if(s->bits==9 && !s->pegasus_rct) s->rct=1; //FIXME ugly + + if (s->bits != 8 && !s->lossless){ + av_log(s->avctx, AV_LOG_ERROR, "only 8 bits/component accepted\n"); + return -1; + } + + height = get_bits(&s->gb, 16); + width = get_bits(&s->gb, 16); + + //HACK for odd_height.mov + if(s->interlaced && s->width == width && s->height == height + 1) + height= s->height; + + av_log(s->avctx, AV_LOG_DEBUG, "sof0: picture: %dx%d\n", width, height); + if(avcodec_check_dimensions(s->avctx, width, height)) + return -1; + + nb_components = get_bits(&s->gb, 8); + if (nb_components <= 0 || + nb_components > MAX_COMPONENTS) + return -1; + if (s->ls && !(s->bits <= 8 || nb_components == 1)){ + av_log(s->avctx, AV_LOG_ERROR, "only <= 8 bits/component or 16-bit gray accepted for JPEG-LS\n"); + return -1; + } + s->nb_components = nb_components; + s->h_max = 1; + s->v_max = 1; + for(i=0;icomponent_id[i] = get_bits(&s->gb, 8) - 1; + s->h_count[i] = get_bits(&s->gb, 4); + s->v_count[i] = get_bits(&s->gb, 4); + /* compute hmax and vmax (only used in interleaved case) */ + if (s->h_count[i] > s->h_max) + s->h_max = s->h_count[i]; + if (s->v_count[i] > s->v_max) + s->v_max = s->v_count[i]; + s->quant_index[i] = get_bits(&s->gb, 8); + if (s->quant_index[i] >= 4) + return -1; + av_log(s->avctx, AV_LOG_DEBUG, "component %d %d:%d id: %d quant:%d\n", i, s->h_count[i], + s->v_count[i], s->component_id[i], s->quant_index[i]); + } + + if(s->ls && (s->h_max > 1 || s->v_max > 1)) { + av_log(s->avctx, AV_LOG_ERROR, "Subsampling in JPEG-LS is not supported.\n"); + return -1; + } + + if(s->v_max==1 && s->h_max==1 && s->lossless==1) s->rgb=1; + + /* if different size, realloc/alloc picture */ + /* XXX: also check h_count and v_count */ + if (width != s->width || height != s->height) { + av_freep(&s->qscale_table); + + s->width = width; + s->height = height; + s->interlaced = 0; + + /* test interlaced mode */ + if (s->first_picture && + s->org_height != 0 && + s->height < ((s->org_height * 3) / 4)) { + s->interlaced = 1; + s->bottom_field = s->interlace_polarity; + s->picture.interlaced_frame = 1; + s->picture.top_field_first = !s->interlace_polarity; + height *= 2; + } + + avcodec_set_dimensions(s->avctx, width, height); + + s->qscale_table= av_mallocz((s->width+15)/16); + + s->first_picture = 0; + } + + if(s->interlaced && (s->bottom_field == !s->interlace_polarity)) + return 0; + + /* XXX: not complete test ! */ + pix_fmt_id = (s->h_count[0] << 20) | (s->v_count[0] << 16) | + (s->h_count[1] << 12) | (s->v_count[1] << 8) | + (s->h_count[2] << 4) | s->v_count[2]; + av_log(s->avctx, AV_LOG_DEBUG, "pix fmt id %x\n", pix_fmt_id); + switch(pix_fmt_id){ + case 0x222222: + case 0x111111: + if(s->rgb){ + s->avctx->pix_fmt = PIX_FMT_RGB32; + }else if(s->nb_components==3) + s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV444P : PIX_FMT_YUVJ444P; + else + s->avctx->pix_fmt = PIX_FMT_GRAY8; + break; + case 0x110000: + s->avctx->pix_fmt = PIX_FMT_GRAY8; + break; + case 0x121111: + s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV440P : PIX_FMT_YUVJ440P; + break; + case 0x211111: + case 0x221212: + s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV422P : PIX_FMT_YUVJ422P; + break; + case 0x221111: + s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV420P : PIX_FMT_YUVJ420P; + break; + default: + av_log(s->avctx, AV_LOG_ERROR, "Unhandled pixel format 0x%x\n", pix_fmt_id); + return -1; + } + if(s->ls){ + if(s->nb_components > 1) + s->avctx->pix_fmt = PIX_FMT_RGB24; + else if(s->bits <= 8) + s->avctx->pix_fmt = PIX_FMT_GRAY8; + else + s->avctx->pix_fmt = PIX_FMT_GRAY16; + } + + if(s->picture.data[0]) + s->avctx->release_buffer(s->avctx, &s->picture); + + s->picture.reference= 0; + if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){ + av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n"); + return -1; + } + s->picture.pict_type= FF_I_TYPE; + s->picture.key_frame= 1; + + for(i=0; i<3; i++){ + s->linesize[i]= s->picture.linesize[i] << s->interlaced; + } + +// printf("%d %d %d %d %d %d\n", s->width, s->height, s->linesize[0], s->linesize[1], s->interlaced, s->avctx->height); + + if (len != (8+(3*nb_components))) + { + av_log(s->avctx, AV_LOG_DEBUG, "decode_sof0: error, len(%d) mismatch\n", len); + } + + /* totally blank picture as progressive JPEG will only add details to it */ + if(s->progressive){ + memset(s->picture.data[0], 0, s->picture.linesize[0] * s->height); + memset(s->picture.data[1], 0, s->picture.linesize[1] * s->height >> (s->v_max - s->v_count[1])); + memset(s->picture.data[2], 0, s->picture.linesize[2] * s->height >> (s->v_max - s->v_count[2])); + } + return 0; +} + +static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index) +{ + int code; + code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2); + if (code < 0) + { + av_log(s->avctx, AV_LOG_WARNING, "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n", 0, dc_index, + &s->vlcs[0][dc_index]); + return 0xffff; + } + + if(code) + return get_xbits(&s->gb, code); + else + return 0; +} + +/* decode block and dequantize */ +static int decode_block(MJpegDecodeContext *s, DCTELEM *block, + int component, int dc_index, int ac_index, int16_t *quant_matrix) +{ + int code, i, j, level, val; + + /* DC coef */ + val = mjpeg_decode_dc(s, dc_index); + if (val == 0xffff) { + av_log(s->avctx, AV_LOG_ERROR, "error dc\n"); + return -1; + } + val = val * quant_matrix[0] + s->last_dc[component]; + s->last_dc[component] = val; + block[0] = val; + /* AC coefs */ + i = 0; + {OPEN_READER(re, &s->gb) + for(;;) { + UPDATE_CACHE(re, &s->gb); + GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2) + + /* EOB */ + if (code == 0x10) + break; + i += ((unsigned)code) >> 4; + if(code != 0x100){ + code &= 0xf; + if(code > MIN_CACHE_BITS - 16){ + UPDATE_CACHE(re, &s->gb) + } + { + int cache=GET_CACHE(re,&s->gb); + int sign=(~cache)>>31; + level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign; + } + + LAST_SKIP_BITS(re, &s->gb, code) + + if (i >= 63) { + if(i == 63){ + j = s->scantable.permutated[63]; + block[j] = level * quant_matrix[j]; + break; + } + av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); + return -1; + } + j = s->scantable.permutated[i]; + block[j] = level * quant_matrix[j]; + } + } + CLOSE_READER(re, &s->gb)} + + return 0; +} + +/* decode block and dequantize - progressive JPEG version */ +static int decode_block_progressive(MJpegDecodeContext *s, DCTELEM *block, + int component, int dc_index, int ac_index, int16_t *quant_matrix, + int ss, int se, int Ah, int Al, int *EOBRUN) +{ + int code, i, j, level, val, run; + + /* DC coef */ + if(!ss){ + val = mjpeg_decode_dc(s, dc_index); + if (val == 0xffff) { + av_log(s->avctx, AV_LOG_ERROR, "error dc\n"); + return -1; + } + val = (val * quant_matrix[0] << Al) + s->last_dc[component]; + }else + val = 0; + s->last_dc[component] = val; + block[0] = val; + if(!se) return 0; + /* AC coefs */ + if(*EOBRUN){ + (*EOBRUN)--; + return 0; + } + {OPEN_READER(re, &s->gb) + for(i=ss;;i++) { + UPDATE_CACHE(re, &s->gb); + GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2) + /* Progressive JPEG use AC coeffs from zero and this decoder sets offset 16 by default */ + code -= 16; + if(code & 0xF) { + i += ((unsigned) code) >> 4; + code &= 0xf; + if(code > MIN_CACHE_BITS - 16){ + UPDATE_CACHE(re, &s->gb) + } + { + int cache=GET_CACHE(re,&s->gb); + int sign=(~cache)>>31; + level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign; + } + + LAST_SKIP_BITS(re, &s->gb, code) + + if (i >= se) { + if(i == se){ + j = s->scantable.permutated[se]; + block[j] = level * quant_matrix[j] << Al; + break; + } + av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); + return -1; + } + j = s->scantable.permutated[i]; + block[j] = level * quant_matrix[j] << Al; + }else{ + run = ((unsigned) code) >> 4; + if(run == 0xF){// ZRL - skip 15 coefficients + i += 15; + }else{ + val = run; + run = (1 << run); + UPDATE_CACHE(re, &s->gb); + run += (GET_CACHE(re, &s->gb) >> (32 - val)) & (run - 1); + if(val) + LAST_SKIP_BITS(re, &s->gb, val); + *EOBRUN = run - 1; + break; + } + } + } + CLOSE_READER(re, &s->gb)} + + return 0; +} + +static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int predictor, int point_transform){ + int i, mb_x, mb_y; + uint16_t buffer[32768][4]; + int left[3], top[3], topleft[3]; + const int linesize= s->linesize[0]; + const int mask= (1<bits)-1; + + if((unsigned)s->mb_width > 32768) //dynamic alloc + return -1; + + for(i=0; i<3; i++){ + buffer[0][i]= 1 << (s->bits + point_transform - 1); + } + for(mb_y = 0; mb_y < s->mb_height; mb_y++) { + const int modified_predictor= mb_y ? predictor : 1; + uint8_t *ptr = s->picture.data[0] + (linesize * mb_y); + + if (s->interlaced && s->bottom_field) + ptr += linesize >> 1; + + for(i=0; i<3; i++){ + top[i]= left[i]= topleft[i]= buffer[0][i]; + } + for(mb_x = 0; mb_x < s->mb_width; mb_x++) { + if (s->restart_interval && !s->restart_count) + s->restart_count = s->restart_interval; + + for(i=0;i<3;i++) { + int pred; + + topleft[i]= top[i]; + top[i]= buffer[mb_x][i]; + + PREDICT(pred, topleft[i], top[i], left[i], modified_predictor); + + left[i]= + buffer[mb_x][i]= mask & (pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform)); + } + + if (s->restart_interval && !--s->restart_count) { + align_get_bits(&s->gb); + skip_bits(&s->gb, 16); /* skip RSTn */ + } + } + + if(s->rct){ + for(mb_x = 0; mb_x < s->mb_width; mb_x++) { + ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200)>>2); + ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1]; + ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1]; + } + }else if(s->pegasus_rct){ + for(mb_x = 0; mb_x < s->mb_width; mb_x++) { + ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2])>>2); + ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1]; + ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1]; + } + }else{ + for(mb_x = 0; mb_x < s->mb_width; mb_x++) { + ptr[4*mb_x+0] = buffer[mb_x][0]; + ptr[4*mb_x+1] = buffer[mb_x][1]; + ptr[4*mb_x+2] = buffer[mb_x][2]; + } + } + } + return 0; +} + +static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor, int point_transform){ + int i, mb_x, mb_y; + const int nb_components=3; + + for(mb_y = 0; mb_y < s->mb_height; mb_y++) { + for(mb_x = 0; mb_x < s->mb_width; mb_x++) { + if (s->restart_interval && !s->restart_count) + s->restart_count = s->restart_interval; + + if(mb_x==0 || mb_y==0 || s->interlaced){ + for(i=0;inb_blocks[i]; + c = s->comp_index[i]; + h = s->h_scount[i]; + v = s->v_scount[i]; + x = 0; + y = 0; + linesize= s->linesize[c]; + + for(j=0; jpicture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap + if(y==0 && mb_y==0){ + if(x==0 && mb_x==0){ + pred= 128 << point_transform; + }else{ + pred= ptr[-1]; + } + }else{ + if(x==0 && mb_x==0){ + pred= ptr[-linesize]; + }else{ + PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor); + } + } + + if (s->interlaced && s->bottom_field) + ptr += linesize >> 1; + *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform); + + if (++x == h) { + x = 0; + y++; + } + } + } + }else{ + for(i=0;inb_blocks[i]; + c = s->comp_index[i]; + h = s->h_scount[i]; + v = s->v_scount[i]; + x = 0; + y = 0; + linesize= s->linesize[c]; + + for(j=0; jpicture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap + PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor); + *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform); + if (++x == h) { + x = 0; + y++; + } + } + } + } + if (s->restart_interval && !--s->restart_count) { + align_get_bits(&s->gb); + skip_bits(&s->gb, 16); /* skip RSTn */ + } + } + } + return 0; +} + +static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int ss, int se, int Ah, int Al){ + int i, mb_x, mb_y; + int EOBRUN = 0; + uint8_t* data[MAX_COMPONENTS]; + int linesize[MAX_COMPONENTS]; + + if(Ah) return 0; /* TODO decode refinement planes too */ + + for(i=0; i < nb_components; i++) { + int c = s->comp_index[i]; + data[c] = s->picture.data[c]; + linesize[c]=s->linesize[c]; + if(s->avctx->codec->id==CODEC_ID_AMV) { + //picture should be flipped upside-down for this codec + assert(!(s->avctx->flags & CODEC_FLAG_EMU_EDGE)); + data[c] += (linesize[c] * (s->v_scount[i] * (8 * s->mb_height -((s->height/s->v_max)&7)) - 1 )); + linesize[c] *= -1; + } + } + + for(mb_y = 0; mb_y < s->mb_height; mb_y++) { + for(mb_x = 0; mb_x < s->mb_width; mb_x++) { + if (s->restart_interval && !s->restart_count) + s->restart_count = s->restart_interval; + + for(i=0;inb_blocks[i]; + c = s->comp_index[i]; + h = s->h_scount[i]; + v = s->v_scount[i]; + x = 0; + y = 0; + for(j=0;jblock, 0, sizeof(s->block)); + if (!s->progressive && decode_block(s, s->block, i, + s->dc_index[i], s->ac_index[i], + s->quant_matrixes[ s->quant_index[c] ]) < 0) { + av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x); + return -1; + } + if (s->progressive && decode_block_progressive(s, s->block, i, + s->dc_index[i], s->ac_index[i], + s->quant_matrixes[ s->quant_index[c] ], ss, se, Ah, Al, &EOBRUN) < 0) { + av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x); + return -1; + } +// av_log(s->avctx, AV_LOG_DEBUG, "mb: %d %d processed\n", mb_y, mb_x); + ptr = data[c] + + (((linesize[c] * (v * mb_y + y) * 8) + + (h * mb_x + x) * 8) >> s->avctx->lowres); + if (s->interlaced && s->bottom_field) + ptr += linesize[c] >> 1; +//av_log(NULL, AV_LOG_DEBUG, "%d %d %d %d %d %d %d %d \n", mb_x, mb_y, x, y, c, s->bottom_field, (v * mb_y + y) * 8, (h * mb_x + x) * 8); + if(!s->progressive) + s->dsp.idct_put(ptr, linesize[c], s->block); + else + s->dsp.idct_add(ptr, linesize[c], s->block); + if (++x == h) { + x = 0; + y++; + } + } + } + /* (< 1350) buggy workaround for Spectralfan.mov, should be fixed */ + if (s->restart_interval && (s->restart_interval < 1350) && + !--s->restart_count) { + align_get_bits(&s->gb); + skip_bits(&s->gb, 16); /* skip RSTn */ + for (i=0; ilast_dc[i] = 1024; + } + } + } + return 0; +} + +int ff_mjpeg_decode_sos(MJpegDecodeContext *s) +{ + int len, nb_components, i, h, v, predictor, point_transform; + int vmax, hmax, index, id; + const int block_size= s->lossless ? 1 : 8; + int ilv, prev_shift; + + /* XXX: verify len field validity */ + len = get_bits(&s->gb, 16); + nb_components = get_bits(&s->gb, 8); + if (len != 6+2*nb_components) + { + av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\n", len); + return -1; + } + vmax = 0; + hmax = 0; + for(i=0;igb, 8) - 1; + av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id); + /* find component index */ + for(index=0;indexnb_components;index++) + if (id == s->component_id[index]) + break; + if (index == s->nb_components) + { + av_log(s->avctx, AV_LOG_ERROR, "decode_sos: index(%d) out of components\n", index); + return -1; + } + + s->comp_index[i] = index; + + s->nb_blocks[i] = s->h_count[index] * s->v_count[index]; + s->h_scount[i] = s->h_count[index]; + s->v_scount[i] = s->v_count[index]; + + s->dc_index[i] = get_bits(&s->gb, 4); + s->ac_index[i] = get_bits(&s->gb, 4); + + if (s->dc_index[i] < 0 || s->ac_index[i] < 0 || + s->dc_index[i] >= 4 || s->ac_index[i] >= 4) + goto out_of_range; +#if 0 //buggy + switch(s->start_code) + { + case SOF0: + if (dc_index[i] > 1 || ac_index[i] > 1) + goto out_of_range; + break; + case SOF1: + case SOF2: + if (dc_index[i] > 3 || ac_index[i] > 3) + goto out_of_range; + break; + case SOF3: + if (dc_index[i] > 3 || ac_index[i] != 0) + goto out_of_range; + break; + } +#endif + } + + predictor= get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */ + ilv= get_bits(&s->gb, 8); /* JPEG Se / JPEG-LS ILV */ + prev_shift = get_bits(&s->gb, 4); /* Ah */ + point_transform= get_bits(&s->gb, 4); /* Al */ + + for(i=0;ilast_dc[i] = 1024; + + if (nb_components > 1) { + /* interleaved stream */ + s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size); + s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size); + } else if(!s->ls) { /* skip this for JPEG-LS */ + h = s->h_max / s->h_scount[0]; + v = s->v_max / s->v_scount[0]; + s->mb_width = (s->width + h * block_size - 1) / (h * block_size); + s->mb_height = (s->height + v * block_size - 1) / (v * block_size); + s->nb_blocks[0] = 1; + s->h_scount[0] = 1; + s->v_scount[0] = 1; + } + + if(s->avctx->debug & FF_DEBUG_PICT_INFO) + av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d ilv:%d bits:%d %s\n", s->lossless ? "lossless" : "sequencial DCT", s->rgb ? "RGB" : "", + predictor, point_transform, ilv, s->bits, + s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : "")); + + + /* mjpeg-b can have padding bytes between sos and image data, skip them */ + for (i = s->mjpb_skiptosod; i > 0; i--) + skip_bits(&s->gb, 8); + + if(s->lossless){ + if(ENABLE_JPEGLS_DECODER && s->ls){ +// for(){ +// reset_ls_coding_parameters(s, 0); + + if(ff_jpegls_decode_picture(s, predictor, point_transform, ilv) < 0) + return -1; + }else{ + if(s->rgb){ + if(ljpeg_decode_rgb_scan(s, predictor, point_transform) < 0) + return -1; + }else{ + if(ljpeg_decode_yuv_scan(s, predictor, point_transform) < 0) + return -1; + } + } + }else{ + if(mjpeg_decode_scan(s, nb_components, predictor, ilv, prev_shift, point_transform) < 0) + return -1; + } + emms_c(); + return 0; + out_of_range: + av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n"); + return -1; +} + +static int mjpeg_decode_dri(MJpegDecodeContext *s) +{ + if (get_bits(&s->gb, 16) != 4) + return -1; + s->restart_interval = get_bits(&s->gb, 16); + s->restart_count = 0; + av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n", s->restart_interval); + + return 0; +} + +static int mjpeg_decode_app(MJpegDecodeContext *s) +{ + int len, id, i; + + len = get_bits(&s->gb, 16); + if (len < 5) + return -1; + if(8*len + get_bits_count(&s->gb) > s->gb.size_in_bits) + return -1; + + id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16); + id = be2me_32(id); + len -= 6; + + if(s->avctx->debug & FF_DEBUG_STARTCODE){ + av_log(s->avctx, AV_LOG_DEBUG, "APPx %8X\n", id); + } + + /* buggy AVID, it puts EOI only at every 10th frame */ + /* also this fourcc is used by non-avid files too, it holds some + informations, but it's always present in AVID creates files */ + if (id == ff_get_fourcc("AVI1")) + { + /* structure: + 4bytes AVI1 + 1bytes polarity + 1bytes always zero + 4bytes field_size + 4bytes field_size_less_padding + */ + s->buggy_avid = 1; +// if (s->first_picture) +// printf("mjpeg: workarounding buggy AVID\n"); + i = get_bits(&s->gb, 8); + if (i==2) s->bottom_field= 1; + else if(i==1) s->bottom_field= 0; +#if 0 + skip_bits(&s->gb, 8); + skip_bits(&s->gb, 32); + skip_bits(&s->gb, 32); + len -= 10; +#endif +// if (s->interlace_polarity) +// printf("mjpeg: interlace polarity: %d\n", s->interlace_polarity); + goto out; + } + +// len -= 2; + + if (id == ff_get_fourcc("JFIF")) + { + int t_w, t_h, v1, v2; + skip_bits(&s->gb, 8); /* the trailing zero-byte */ + v1= get_bits(&s->gb, 8); + v2= get_bits(&s->gb, 8); + skip_bits(&s->gb, 8); + + s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 16); + s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 16); + + if (s->avctx->debug & FF_DEBUG_PICT_INFO) + av_log(s->avctx, AV_LOG_INFO, "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n", + v1, v2, + s->avctx->sample_aspect_ratio.num, + s->avctx->sample_aspect_ratio.den + ); + + t_w = get_bits(&s->gb, 8); + t_h = get_bits(&s->gb, 8); + if (t_w && t_h) + { + /* skip thumbnail */ + if (len-10-(t_w*t_h*3) > 0) + len -= t_w*t_h*3; + } + len -= 10; + goto out; + } + + if (id == ff_get_fourcc("Adob") && (get_bits(&s->gb, 8) == 'e')) + { + if (s->avctx->debug & FF_DEBUG_PICT_INFO) + av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found\n"); + skip_bits(&s->gb, 16); /* version */ + skip_bits(&s->gb, 16); /* flags0 */ + skip_bits(&s->gb, 16); /* flags1 */ + skip_bits(&s->gb, 8); /* transform */ + len -= 7; + goto out; + } + + if (id == ff_get_fourcc("LJIF")){ + if (s->avctx->debug & FF_DEBUG_PICT_INFO) + av_log(s->avctx, AV_LOG_INFO, "Pegasus lossless jpeg header found\n"); + skip_bits(&s->gb, 16); /* version ? */ + skip_bits(&s->gb, 16); /* unknwon always 0? */ + skip_bits(&s->gb, 16); /* unknwon always 0? */ + skip_bits(&s->gb, 16); /* unknwon always 0? */ + switch( get_bits(&s->gb, 8)){ + case 1: + s->rgb= 1; + s->pegasus_rct=0; + break; + case 2: + s->rgb= 1; + s->pegasus_rct=1; + break; + default: + av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace\n"); + } + len -= 9; + goto out; + } + + /* Apple MJPEG-A */ + if ((s->start_code == APP1) && (len > (0x28 - 8))) + { + id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16); + id = be2me_32(id); + len -= 4; + if (id == ff_get_fourcc("mjpg")) /* Apple MJPEG-A */ + { +#if 0 + skip_bits(&s->gb, 32); /* field size */ + skip_bits(&s->gb, 32); /* pad field size */ + skip_bits(&s->gb, 32); /* next off */ + skip_bits(&s->gb, 32); /* quant off */ + skip_bits(&s->gb, 32); /* huff off */ + skip_bits(&s->gb, 32); /* image off */ + skip_bits(&s->gb, 32); /* scan off */ + skip_bits(&s->gb, 32); /* data off */ +#endif + if (s->avctx->debug & FF_DEBUG_PICT_INFO) + av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n"); + } + } + +out: + /* slow but needed for extreme adobe jpegs */ + if (len < 0) + av_log(s->avctx, AV_LOG_ERROR, "mjpeg: error, decode_app parser read over the end\n"); + while(--len > 0) + skip_bits(&s->gb, 8); + + return 0; +} + +static int mjpeg_decode_com(MJpegDecodeContext *s) +{ + int len = get_bits(&s->gb, 16); + if (len >= 2 && 8*len - 16 + get_bits_count(&s->gb) <= s->gb.size_in_bits) { + char *cbuf = av_malloc(len - 1); + if (cbuf) { + int i; + for (i = 0; i < len - 2; i++) + cbuf[i] = get_bits(&s->gb, 8); + if (i > 0 && cbuf[i-1] == '\n') + cbuf[i-1] = 0; + else + cbuf[i] = 0; + + if(s->avctx->debug & FF_DEBUG_PICT_INFO) + av_log(s->avctx, AV_LOG_INFO, "mjpeg comment: '%s'\n", cbuf); + + /* buggy avid, it puts EOI only at every 10th frame */ + if (!strcmp(cbuf, "AVID")) + { + s->buggy_avid = 1; + // if (s->first_picture) + // printf("mjpeg: workarounding buggy AVID\n"); + } + else if(!strcmp(cbuf, "CS=ITU601")){ + s->cs_itu601= 1; + } + + av_free(cbuf); + } + } + + return 0; +} + +#if 0 +static int valid_marker_list[] = +{ + /* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f */ +/* 0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 1 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 2 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 3 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 4 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 5 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 6 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 7 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 8 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 9 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* a */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* b */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* c */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, +/* d */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, +/* e */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, +/* f */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, +} +#endif + +/* return the 8 bit start code value and update the search + state. Return -1 if no start code found */ +static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end) +{ + const uint8_t *buf_ptr; + unsigned int v, v2; + int val; +#ifdef DEBUG + int skipped=0; +#endif + + buf_ptr = *pbuf_ptr; + while (buf_ptr < buf_end) { + v = *buf_ptr++; + v2 = *buf_ptr; + if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) { + val = *buf_ptr++; + goto found; + } +#ifdef DEBUG + skipped++; +#endif + } + val = -1; +found: +#ifdef DEBUG + av_log(NULL, AV_LOG_VERBOSE, "find_marker skipped %d bytes\n", skipped); +#endif + *pbuf_ptr = buf_ptr; + return val; +} + +int ff_mjpeg_decode_frame(AVCodecContext *avctx, + void *data, int *data_size, + const uint8_t *buf, int buf_size) +{ + MJpegDecodeContext *s = avctx->priv_data; + const uint8_t *buf_end, *buf_ptr; + int start_code; + AVFrame *picture = data; + + buf_ptr = buf; + buf_end = buf + buf_size; + while (buf_ptr < buf_end) { + /* find start next marker */ + start_code = find_marker(&buf_ptr, buf_end); + { + /* EOF */ + if (start_code < 0) { + goto the_end; + } else { + av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%td\n", start_code, buf_end - buf_ptr); + + if ((buf_end - buf_ptr) > s->buffer_size) + { + av_free(s->buffer); + s->buffer_size = buf_end-buf_ptr; + s->buffer = av_malloc(s->buffer_size + FF_INPUT_BUFFER_PADDING_SIZE); + av_log(avctx, AV_LOG_DEBUG, "buffer too small, expanding to %d bytes\n", + s->buffer_size); + } + + /* unescape buffer of SOS, use special treatment for JPEG-LS */ + if (start_code == SOS && !s->ls) + { + const uint8_t *src = buf_ptr; + uint8_t *dst = s->buffer; + + while (srccodec_id != CODEC_ID_THP) + { + if (x == 0xff) { + while (src < buf_end && x == 0xff) + x = *(src++); + + if (x >= 0xd0 && x <= 0xd7) + *(dst++) = x; + else if (x) + break; + } + } + } + init_get_bits(&s->gb, s->buffer, (dst - s->buffer)*8); + + av_log(avctx, AV_LOG_DEBUG, "escaping removed %td bytes\n", + (buf_end - buf_ptr) - (dst - s->buffer)); + } + else if(start_code == SOS && s->ls){ + const uint8_t *src = buf_ptr; + uint8_t *dst = s->buffer; + int bit_count = 0; + int t = 0, b = 0; + PutBitContext pb; + + s->cur_scan++; + + /* find marker */ + while (src + t < buf_end){ + uint8_t x = src[t++]; + if (x == 0xff){ + while((src + t < buf_end) && x == 0xff) + x = src[t++]; + if (x & 0x80) { + t -= 2; + break; + } + } + } + bit_count = t * 8; + + init_put_bits(&pb, dst, t); + + /* unescape bitstream */ + while(b < t){ + uint8_t x = src[b++]; + put_bits(&pb, 8, x); + if(x == 0xFF){ + x = src[b++]; + put_bits(&pb, 7, x); + bit_count--; + } + } + flush_put_bits(&pb); + + init_get_bits(&s->gb, dst, bit_count); + } + else + init_get_bits(&s->gb, buf_ptr, (buf_end - buf_ptr)*8); + + s->start_code = start_code; + if(s->avctx->debug & FF_DEBUG_STARTCODE){ + av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code); + } + + /* process markers */ + if (start_code >= 0xd0 && start_code <= 0xd7) { + av_log(avctx, AV_LOG_DEBUG, "restart marker: %d\n", start_code&0x0f); + /* APP fields */ + } else if (start_code >= APP0 && start_code <= APP15) { + mjpeg_decode_app(s); + /* Comment */ + } else if (start_code == COM){ + mjpeg_decode_com(s); + } + + switch(start_code) { + case SOI: + s->restart_interval = 0; + + s->restart_count = 0; + /* nothing to do on SOI */ + break; + case DQT: + ff_mjpeg_decode_dqt(s); + break; + case DHT: + if(ff_mjpeg_decode_dht(s) < 0){ + av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n"); + return -1; + } + break; + case SOF0: + s->lossless=0; + s->ls=0; + s->progressive=0; + if (ff_mjpeg_decode_sof(s) < 0) + return -1; + break; + case SOF2: + s->lossless=0; + s->ls=0; + s->progressive=1; + if (ff_mjpeg_decode_sof(s) < 0) + return -1; + break; + case SOF3: + s->lossless=1; + s->ls=0; + s->progressive=0; + if (ff_mjpeg_decode_sof(s) < 0) + return -1; + break; + case SOF48: + s->lossless=1; + s->ls=1; + s->progressive=0; + if (ff_mjpeg_decode_sof(s) < 0) + return -1; + break; + case LSE: + if (!ENABLE_JPEGLS_DECODER || ff_jpegls_decode_lse(s) < 0) + return -1; + break; + case EOI: + s->cur_scan = 0; + if ((s->buggy_avid && !s->interlaced) || s->restart_interval) + break; +eoi_parser: + { + if (s->interlaced) { + s->bottom_field ^= 1; + /* if not bottom field, do not output image yet */ + if (s->bottom_field == !s->interlace_polarity) + goto not_the_end; + } + *picture = s->picture; + *data_size = sizeof(AVFrame); + + if(!s->lossless){ + picture->quality= FFMAX3(s->qscale[0], s->qscale[1], s->qscale[2]); + picture->qstride= 0; + picture->qscale_table= s->qscale_table; + memset(picture->qscale_table, picture->quality, (s->width+15)/16); + if(avctx->debug & FF_DEBUG_QP) + av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality); + picture->quality*= FF_QP2LAMBDA; + } + + goto the_end; + } + break; + case SOS: + ff_mjpeg_decode_sos(s); + /* buggy avid puts EOI every 10-20th frame */ + /* if restart period is over process EOI */ + if ((s->buggy_avid && !s->interlaced) || s->restart_interval) + goto eoi_parser; + break; + case DRI: + mjpeg_decode_dri(s); + break; + case SOF1: + case SOF5: + case SOF6: + case SOF7: + case SOF9: + case SOF10: + case SOF11: + case SOF13: + case SOF14: + case SOF15: + case JPG: + av_log(avctx, AV_LOG_ERROR, "mjpeg: unsupported coding type (%x)\n", start_code); + break; +// default: +// printf("mjpeg: unsupported marker (%x)\n", start_code); +// break; + } + +not_the_end: + /* eof process start code */ + buf_ptr += (get_bits_count(&s->gb)+7)/8; + av_log(avctx, AV_LOG_DEBUG, "marker parser used %d bytes (%d bits)\n", + (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb)); + } + } + } +the_end: + av_log(avctx, AV_LOG_DEBUG, "mjpeg decode frame unused %td bytes\n", buf_end - buf_ptr); +// return buf_end - buf_ptr; + return buf_ptr - buf; +} + +av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx) +{ + MJpegDecodeContext *s = avctx->priv_data; + int i, j; + + av_free(s->buffer); + av_free(s->qscale_table); + + for(i=0;i<2;i++) { + for(j=0;j<4;j++) + free_vlc(&s->vlcs[i][j]); + } + return 0; +} + +AVCodec mjpeg_decoder = { + "mjpeg", + CODEC_TYPE_VIDEO, + CODEC_ID_MJPEG, + sizeof(MJpegDecodeContext), + ff_mjpeg_decode_init, + NULL, + ff_mjpeg_decode_end, + ff_mjpeg_decode_frame, + CODEC_CAP_DR1, + NULL, + .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"), +}; + +AVCodec thp_decoder = { + "thp", + CODEC_TYPE_VIDEO, + CODEC_ID_THP, + sizeof(MJpegDecodeContext), + ff_mjpeg_decode_init, + NULL, + ff_mjpeg_decode_end, + ff_mjpeg_decode_frame, + CODEC_CAP_DR1, + NULL, + .long_name = NULL_IF_CONFIG_SMALL("Nintendo Gamecube THP video"), +}; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/mjpegdec.h b/src/add-ons/media/plugins/avcodec/libavcodec/mjpegdec.h new file mode 100644 index 0000000000..d6022eb1e9 --- /dev/null +++ b/src/add-ons/media/plugins/avcodec/libavcodec/mjpegdec.h @@ -0,0 +1,111 @@ +/* + * MJPEG decoder + * Copyright (c) 2000, 2001 Fabrice Bellard. + * Copyright (c) 2003 Alex Beregszaszi + * Copyright (c) 2003-2004 Michael Niedermayer + * + * This file is part of FFmpeg. + * + * FFmpeg 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.1 of the License, or (at your option) any later version. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file mjpegdec.h + * MJPEG decoder. + */ + +#ifndef FFMPEG_MJPEGDEC_H +#define FFMPEG_MJPEGDEC_H + +#include "avcodec.h" +#include "bitstream.h" +#include "dsputil.h" + +#define MAX_COMPONENTS 4 + +typedef struct MJpegDecodeContext { + AVCodecContext *avctx; + GetBitContext gb; + + int start_code; /* current start code */ + int buffer_size; + uint8_t *buffer; + + int16_t quant_matrixes[4][64]; + VLC vlcs[2][4]; + int qscale[4]; ///< quantizer scale calculated from quant_matrixes + + int org_height; /* size given at codec init */ + int first_picture; /* true if decoding first picture */ + int interlaced; /* true if interlaced */ + int bottom_field; /* true if bottom field */ + int lossless; + int ls; + int progressive; + int rgb; + int rct; /* standard rct */ + int pegasus_rct; /* pegasus reversible colorspace transform */ + int bits; /* bits per component */ + + int maxval; + int near; ///< near lossless bound (si 0 for lossless) + int t1,t2,t3; + int reset; ///< context halfing intervall ?rename + + int width, height; + int mb_width, mb_height; + int nb_components; + int component_id[MAX_COMPONENTS]; + int h_count[MAX_COMPONENTS]; /* horizontal and vertical count for each component */ + int v_count[MAX_COMPONENTS]; + int comp_index[MAX_COMPONENTS]; + int dc_index[MAX_COMPONENTS]; + int ac_index[MAX_COMPONENTS]; + int nb_blocks[MAX_COMPONENTS]; + int h_scount[MAX_COMPONENTS]; + int v_scount[MAX_COMPONENTS]; + int h_max, v_max; /* maximum h and v counts */ + int quant_index[4]; /* quant table index for each component */ + int last_dc[MAX_COMPONENTS]; /* last DEQUANTIZED dc (XXX: am I right to do that ?) */ + AVFrame picture; /* picture structure */ + int linesize[MAX_COMPONENTS]; ///< linesize << interlaced + int8_t *qscale_table; + DECLARE_ALIGNED_16(DCTELEM, block[64]); + ScanTable scantable; + DSPContext dsp; + + int restart_interval; + int restart_count; + + int buggy_avid; + int cs_itu601; + int interlace_polarity; + + int mjpb_skiptosod; + + int cur_scan; /* current scan, used by JPEG-LS */ +} MJpegDecodeContext; + +int ff_mjpeg_decode_init(AVCodecContext *avctx); +int ff_mjpeg_decode_end(AVCodecContext *avctx); +int ff_mjpeg_decode_frame(AVCodecContext *avctx, + void *data, int *data_size, + const uint8_t *buf, int buf_size); +int ff_mjpeg_decode_dqt(MJpegDecodeContext *s); +int ff_mjpeg_decode_dht(MJpegDecodeContext *s); +int ff_mjpeg_decode_sof(MJpegDecodeContext *s); +int ff_mjpeg_decode_sos(MJpegDecodeContext *s); + +#endif /* FFMPEG_MJPEGDEC_H */ diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/mjpegenc.c b/src/add-ons/media/plugins/avcodec/libavcodec/mjpegenc.c new file mode 100644 index 0000000000..9268af0e1f --- /dev/null +++ b/src/add-ons/media/plugins/avcodec/libavcodec/mjpegenc.c @@ -0,0 +1,459 @@ +/* + * MJPEG encoder + * Copyright (c) 2000, 2001 Fabrice Bellard. + * Copyright (c) 2003 Alex Beregszaszi + * Copyright (c) 2003-2004 Michael Niedermayer + * + * Support for external huffman table, various fixes (AVID workaround), + * aspecting, new decode_frame mechanism and apple mjpeg-b support + * by Alex Beregszaszi + * + * This file is part of FFmpeg. + * + * FFmpeg 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.1 of the License, or (at your option) any later version. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file mjpegenc.c + * MJPEG encoder. + */ + +//#define DEBUG +#include + +#include "avcodec.h" +#include "dsputil.h" +#include "mpegvideo.h" +#include "mjpeg.h" +#include "mjpegenc.h" + +/* use two quantizer tables (one for luminance and one for chrominance) */ +/* not yet working */ +#undef TWOMATRIXES + + +av_cold int ff_mjpeg_encode_init(MpegEncContext *s) +{ + MJpegContext *m; + + m = av_malloc(sizeof(MJpegContext)); + if (!m) + return -1; + + s->min_qcoeff=-1023; + s->max_qcoeff= 1023; + + /* build all the huffman tables */ + ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance, + m->huff_code_dc_luminance, + ff_mjpeg_bits_dc_luminance, + ff_mjpeg_val_dc); + ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance, + m->huff_code_dc_chrominance, + ff_mjpeg_bits_dc_chrominance, + ff_mjpeg_val_dc); + ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance, + m->huff_code_ac_luminance, + ff_mjpeg_bits_ac_luminance, + ff_mjpeg_val_ac_luminance); + ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance, + m->huff_code_ac_chrominance, + ff_mjpeg_bits_ac_chrominance, + ff_mjpeg_val_ac_chrominance); + + s->mjpeg_ctx = m; + return 0; +} + +void ff_mjpeg_encode_close(MpegEncContext *s) +{ + av_free(s->mjpeg_ctx); +} + +/* table_class: 0 = DC coef, 1 = AC coefs */ +static int put_huffman_table(MpegEncContext *s, int table_class, int table_id, + const uint8_t *bits_table, const uint8_t *value_table) +{ + PutBitContext *p = &s->pb; + int n, i; + + put_bits(p, 4, table_class); + put_bits(p, 4, table_id); + + n = 0; + for(i=1;i<=16;i++) { + n += bits_table[i]; + put_bits(p, 8, bits_table[i]); + } + + for(i=0;ipb; + int i, j, size; + uint8_t *ptr; + + /* quant matrixes */ + put_marker(p, DQT); +#ifdef TWOMATRIXES + put_bits(p, 16, 2 + 2 * (1 + 64)); +#else + put_bits(p, 16, 2 + 1 * (1 + 64)); +#endif + put_bits(p, 4, 0); /* 8 bit precision */ + put_bits(p, 4, 0); /* table 0 */ + for(i=0;i<64;i++) { + j = s->intra_scantable.permutated[i]; + put_bits(p, 8, s->intra_matrix[j]); + } +#ifdef TWOMATRIXES + put_bits(p, 4, 0); /* 8 bit precision */ + put_bits(p, 4, 1); /* table 1 */ + for(i=0;i<64;i++) { + j = s->intra_scantable.permutated[i]; + put_bits(p, 8, s->chroma_intra_matrix[j]); + } +#endif + + /* huffman table */ + put_marker(p, DHT); + flush_put_bits(p); + ptr = pbBufPtr(p); + put_bits(p, 16, 0); /* patched later */ + size = 2; + size += put_huffman_table(s, 0, 0, ff_mjpeg_bits_dc_luminance, + ff_mjpeg_val_dc); + size += put_huffman_table(s, 0, 1, ff_mjpeg_bits_dc_chrominance, + ff_mjpeg_val_dc); + + size += put_huffman_table(s, 1, 0, ff_mjpeg_bits_ac_luminance, + ff_mjpeg_val_ac_luminance); + size += put_huffman_table(s, 1, 1, ff_mjpeg_bits_ac_chrominance, + ff_mjpeg_val_ac_chrominance); + AV_WB16(ptr, size); +} + +static void jpeg_put_comments(MpegEncContext *s) +{ + PutBitContext *p = &s->pb; + int size; + uint8_t *ptr; + + if (s->aspect_ratio_info /* && !lossless */) + { + /* JFIF header */ + put_marker(p, APP0); + put_bits(p, 16, 16); + ff_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */ + put_bits(p, 16, 0x0201); /* v 1.02 */ + put_bits(p, 8, 0); /* units type: 0 - aspect ratio */ + put_bits(p, 16, s->avctx->sample_aspect_ratio.num); + put_bits(p, 16, s->avctx->sample_aspect_ratio.den); + put_bits(p, 8, 0); /* thumbnail width */ + put_bits(p, 8, 0); /* thumbnail height */ + } + + /* comment */ + if(!(s->flags & CODEC_FLAG_BITEXACT)){ + put_marker(p, COM); + flush_put_bits(p); + ptr = pbBufPtr(p); + put_bits(p, 16, 0); /* patched later */ + ff_put_string(p, LIBAVCODEC_IDENT, 1); + size = strlen(LIBAVCODEC_IDENT)+3; + AV_WB16(ptr, size); + } + + if( s->avctx->pix_fmt == PIX_FMT_YUV420P + ||s->avctx->pix_fmt == PIX_FMT_YUV422P + ||s->avctx->pix_fmt == PIX_FMT_YUV444P){ + put_marker(p, COM); + flush_put_bits(p); + ptr = pbBufPtr(p); + put_bits(p, 16, 0); /* patched later */ + ff_put_string(p, "CS=ITU601", 1); + size = strlen("CS=ITU601")+3; + AV_WB16(ptr, size); + } +} + +void ff_mjpeg_encode_picture_header(MpegEncContext *s) +{ + const int lossless= s->avctx->codec_id != CODEC_ID_MJPEG; + + put_marker(&s->pb, SOI); + + jpeg_put_comments(s); + + jpeg_table_header(s); + + switch(s->avctx->codec_id){ + case CODEC_ID_MJPEG: put_marker(&s->pb, SOF0 ); break; + case CODEC_ID_LJPEG: put_marker(&s->pb, SOF3 ); break; + default: assert(0); + } + + put_bits(&s->pb, 16, 17); + if(lossless && s->avctx->pix_fmt == PIX_FMT_RGB32) + put_bits(&s->pb, 8, 9); /* 9 bits/component RCT */ + else + put_bits(&s->pb, 8, 8); /* 8 bits/component */ + put_bits(&s->pb, 16, s->height); + put_bits(&s->pb, 16, s->width); + put_bits(&s->pb, 8, 3); /* 3 components */ + + /* Y component */ + put_bits(&s->pb, 8, 1); /* component number */ + put_bits(&s->pb, 4, s->mjpeg_hsample[0]); /* H factor */ + put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* V factor */ + put_bits(&s->pb, 8, 0); /* select matrix */ + + /* Cb component */ + put_bits(&s->pb, 8, 2); /* component number */ + put_bits(&s->pb, 4, s->mjpeg_hsample[1]); /* H factor */ + put_bits(&s->pb, 4, s->mjpeg_vsample[1]); /* V factor */ +#ifdef TWOMATRIXES + put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */ +#else + put_bits(&s->pb, 8, 0); /* select matrix */ +#endif + + /* Cr component */ + put_bits(&s->pb, 8, 3); /* component number */ + put_bits(&s->pb, 4, s->mjpeg_hsample[2]); /* H factor */ + put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* V factor */ +#ifdef TWOMATRIXES + put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */ +#else + put_bits(&s->pb, 8, 0); /* select matrix */ +#endif + + /* scan header */ + put_marker(&s->pb, SOS); + put_bits(&s->pb, 16, 12); /* length */ + put_bits(&s->pb, 8, 3); /* 3 components */ + + /* Y component */ + put_bits(&s->pb, 8, 1); /* index */ + put_bits(&s->pb, 4, 0); /* DC huffman table index */ + put_bits(&s->pb, 4, 0); /* AC huffman table index */ + + /* Cb component */ + put_bits(&s->pb, 8, 2); /* index */ + put_bits(&s->pb, 4, 1); /* DC huffman table index */ + put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */ + + /* Cr component */ + put_bits(&s->pb, 8, 3); /* index */ + put_bits(&s->pb, 4, 1); /* DC huffman table index */ + put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */ + + put_bits(&s->pb, 8, lossless ? s->avctx->prediction_method+1 : 0); /* Ss (not used) */ + + switch(s->avctx->codec_id){ + case CODEC_ID_MJPEG: put_bits(&s->pb, 8, 63); break; /* Se (not used) */ + case CODEC_ID_LJPEG: put_bits(&s->pb, 8, 0); break; /* not used */ + default: assert(0); + } + + put_bits(&s->pb, 8, 0); /* Ah/Al (not used) */ +} + +static void escape_FF(MpegEncContext *s, int start) +{ + int size= put_bits_count(&s->pb) - start*8; + int i, ff_count; + uint8_t *buf= s->pb.buf + start; + int align= (-(size_t)(buf))&3; + + assert((size&7) == 0); + size >>= 3; + + ff_count=0; + for(i=0; i>4))&0x0F0F0F0F)+0x01010101)&0x10101010; + v= *(uint32_t*)(&buf[i+4]); + acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010; + v= *(uint32_t*)(&buf[i+8]); + acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010; + v= *(uint32_t*)(&buf[i+12]); + acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010; + + acc>>=4; + acc+= (acc>>16); + acc+= (acc>>8); + ff_count+= acc&0xFF; + } + for(; ipb, 32, 0); + put_bits(&s->pb, (ff_count-i)*8, 0); + flush_put_bits(&s->pb); + + for(i=size-1; ff_count; i--){ + int v= buf[i]; + + if(v==0xFF){ +//printf("%d %d\n", i, ff_count); + buf[i+ff_count]= 0; + ff_count--; + } + + buf[i+ff_count]= v; + } +} + +void ff_mjpeg_encode_stuffing(PutBitContext * pbc) +{ + int length; + length= (-put_bits_count(pbc))&7; + if(length) put_bits(pbc, length, (1<pb); + flush_put_bits(&s->pb); + + assert((s->header_bits&7)==0); + + escape_FF(s, s->header_bits>>3); + + put_marker(&s->pb, EOI); +} + +void ff_mjpeg_encode_dc(MpegEncContext *s, int val, + uint8_t *huff_size, uint16_t *huff_code) +{ + int mant, nbits; + + if (val == 0) { + put_bits(&s->pb, huff_size[0], huff_code[0]); + } else { + mant = val; + if (val < 0) { + val = -val; + mant--; + } + + nbits= av_log2_16bit(val) + 1; + + put_bits(&s->pb, huff_size[nbits], huff_code[nbits]); + + put_sbits(&s->pb, nbits, mant); + } +} + +static void encode_block(MpegEncContext *s, DCTELEM *block, int n) +{ + int mant, nbits, code, i, j; + int component, dc, run, last_index, val; + MJpegContext *m = s->mjpeg_ctx; + uint8_t *huff_size_ac; + uint16_t *huff_code_ac; + + /* DC coef */ + component = (n <= 3 ? 0 : (n&1) + 1); + dc = block[0]; /* overflow is impossible */ + val = dc - s->last_dc[component]; + if (n < 4) { + ff_mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance); + huff_size_ac = m->huff_size_ac_luminance; + huff_code_ac = m->huff_code_ac_luminance; + } else { + ff_mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance); + huff_size_ac = m->huff_size_ac_chrominance; + huff_code_ac = m->huff_code_ac_chrominance; + } + s->last_dc[component] = dc; + + /* AC coefs */ + + run = 0; + last_index = s->block_last_index[n]; + for(i=1;i<=last_index;i++) { + j = s->intra_scantable.permutated[i]; + val = block[j]; + if (val == 0) { + run++; + } else { + while (run >= 16) { + put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]); + run -= 16; + } + mant = val; + if (val < 0) { + val = -val; + mant--; + } + + nbits= av_log2(val) + 1; + code = (run << 4) | nbits; + + put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]); + + put_sbits(&s->pb, nbits, mant); + run = 0; + } + } + + /* output EOB only if not already 64 values */ + if (last_index < 63 || run != 0) + put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]); +} + +void ff_mjpeg_encode_mb(MpegEncContext *s, DCTELEM block[6][64]) +{ + int i; + for(i=0;i<5;i++) { + encode_block(s, block[i], i); + } + if (s->chroma_format == CHROMA_420) { + encode_block(s, block[5], 5); + } else { + encode_block(s, block[6], 6); + encode_block(s, block[5], 5); + encode_block(s, block[7], 7); + } +} + +AVCodec mjpeg_encoder = { + "mjpeg", + CODEC_TYPE_VIDEO, + CODEC_ID_MJPEG, + sizeof(MpegEncContext), + MPV_encode_init, + MPV_encode_picture, + MPV_encode_end, + .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_NONE}, + .long_name= NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"), +}; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/mjpegenc.h b/src/add-ons/media/plugins/avcodec/libavcodec/mjpegenc.h new file mode 100644 index 0000000000..02f8a714c3 --- /dev/null +++ b/src/add-ons/media/plugins/avcodec/libavcodec/mjpegenc.h @@ -0,0 +1,60 @@ +/* + * MJPEG encoder + * Copyright (c) 2000, 2001 Fabrice Bellard. + * Copyright (c) 2003 Alex Beregszaszi + * Copyright (c) 2003-2004 Michael Niedermayer + * + * Support for external huffman table, various fixes (AVID workaround), + * aspecting, new decode_frame mechanism and apple mjpeg-b support + * by Alex Beregszaszi + * + * This file is part of FFmpeg. + * + * FFmpeg 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.1 of the License, or (at your option) any later version. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file mjpegenc.h + * MJPEG encoder. + */ + +#ifndef FFMPEG_MJPEGENC_H +#define FFMPEG_MJPEGENC_H + +#include "dsputil.h" +#include "mpegvideo.h" + +typedef struct MJpegContext { + uint8_t huff_size_dc_luminance[12]; //FIXME use array [3] instead of lumi / chrom, for easier addressing + uint16_t huff_code_dc_luminance[12]; + uint8_t huff_size_dc_chrominance[12]; + uint16_t huff_code_dc_chrominance[12]; + + uint8_t huff_size_ac_luminance[256]; + uint16_t huff_code_ac_luminance[256]; + uint8_t huff_size_ac_chrominance[256]; + uint16_t huff_code_ac_chrominance[256]; +} MJpegContext; + +int ff_mjpeg_encode_init(MpegEncContext *s); +void ff_mjpeg_encode_close(MpegEncContext *s); +void ff_mjpeg_encode_picture_header(MpegEncContext *s); +void ff_mjpeg_encode_picture_trailer(MpegEncContext *s); +void ff_mjpeg_encode_stuffing(PutBitContext *pbc); +void ff_mjpeg_encode_dc(MpegEncContext *s, int val, + uint8_t *huff_size, uint16_t *huff_code); +void ff_mjpeg_encode_mb(MpegEncContext *s, DCTELEM block[6][64]); + +#endif /* FFMPEG_MJPEGENC_H */ diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/mlp.c b/src/add-ons/media/plugins/avcodec/libavcodec/mlp.c new file mode 100644 index 0000000000..2ac2e658f1 --- /dev/null +++ b/src/add-ons/media/plugins/avcodec/libavcodec/mlp.c @@ -0,0 +1,119 @@ +/* + * MLP codec common code + * Copyright (c) 2007-2008 Ian Caulfield + * + * This file is part of FFmpeg. + * + * FFmpeg 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.1 of the License, or (at your option) any later version. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include + +#include "libavutil/crc.h" +#include "mlp.h" + +const uint8_t ff_mlp_huffman_tables[3][18][2] = { + { /* Huffman table 0, -7 - +10 */ + {0x01, 9}, {0x01, 8}, {0x01, 7}, {0x01, 6}, {0x01, 5}, {0x01, 4}, {0x01, 3}, + {0x04, 3}, {0x05, 3}, {0x06, 3}, {0x07, 3}, + {0x03, 3}, {0x05, 4}, {0x09, 5}, {0x11, 6}, {0x21, 7}, {0x41, 8}, {0x81, 9}, + }, { /* Huffman table 1, -7 - +8 */ + {0x01, 9}, {0x01, 8}, {0x01, 7}, {0x01, 6}, {0x01, 5}, {0x01, 4}, {0x01, 3}, + {0x02, 2}, {0x03, 2}, + {0x03, 3}, {0x05, 4}, {0x09, 5}, {0x11, 6}, {0x21, 7}, {0x41, 8}, {0x81, 9}, + }, { /* Huffman table 2, -7 - +7 */ + {0x01, 9}, {0x01, 8}, {0x01, 7}, {0x01, 6}, {0x01, 5}, {0x01, 4}, {0x01, 3}, + {0x01, 1}, + {0x03, 3}, {0x05, 4}, {0x09, 5}, {0x11, 6}, {0x21, 7}, {0x41, 8}, {0x81, 9}, + } +}; + +static int crc_init = 0; +static AVCRC crc_63[1024]; +static AVCRC crc_1D[1024]; + + +static int crc_init_2D = 0; +static AVCRC crc_2D[1024]; + +int av_cold ff_mlp_init_crc2D(AVCodecParserContext *s) +{ + if (!crc_init_2D) { + av_crc_init(crc_2D, 0, 16, 0x002D, sizeof(crc_2D)); + crc_init_2D = 1; + } + + return 0; +} + +void av_cold ff_mlp_init_crc() +{ + if (!crc_init) { + av_crc_init(crc_63, 0, 8, 0x63, sizeof(crc_63)); + av_crc_init(crc_1D, 0, 8, 0x1D, sizeof(crc_1D)); + crc_init = 1; + } +} + +uint16_t ff_mlp_checksum16(const uint8_t *buf, unsigned int buf_size) +{ + uint16_t crc; + + crc = av_crc(crc_2D, 0, buf, buf_size - 2); + crc ^= AV_RL16(buf + buf_size - 2); + return crc; +} + +uint8_t ff_mlp_checksum8(const uint8_t *buf, unsigned int buf_size) +{ + uint8_t checksum = av_crc(crc_63, 0x3c, buf, buf_size - 1); // crc_63[0xa2] == 0x3c + checksum ^= buf[buf_size-1]; + return checksum; +} + +uint8_t ff_mlp_restart_checksum(const uint8_t *buf, unsigned int bit_size) +{ + int i; + int num_bytes = (bit_size + 2) / 8; + + int crc = crc_1D[buf[0] & 0x3f]; + crc = av_crc(crc_1D, crc, buf + 1, num_bytes - 2); + crc ^= buf[num_bytes - 1]; + + for (i = 0; i < ((bit_size + 2) & 7); i++) { + crc <<= 1; + if (crc & 0x100) + crc ^= 0x11D; + crc ^= (buf[num_bytes] >> (7 - i)) & 1; + } + + return crc; +} + +uint8_t ff_mlp_calculate_parity(const uint8_t *buf, unsigned int buf_size) +{ + uint32_t scratch = 0; + const uint8_t *buf_end = buf + buf_size; + + for (; buf < buf_end - 3; buf += 4) + scratch ^= *((const uint32_t*)buf); + + scratch = xor_32_to_8(scratch); + + for (; buf < buf_end; buf++) + scratch ^= *buf; + + return scratch; +} diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/mlp.h b/src/add-ons/media/plugins/avcodec/libavcodec/mlp.h new file mode 100644 index 0000000000..bbb09e0b5a --- /dev/null +++ b/src/add-ons/media/plugins/avcodec/libavcodec/mlp.h @@ -0,0 +1,121 @@ +/* + * MLP codec common header file + * Copyright (c) 2007-2008 Ian Caulfield + * + * This file is part of FFmpeg. + * + * FFmpeg 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.1 of the License, or (at your option) any later version. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef FFMPEG_MLP_H +#define FFMPEG_MLP_H + +#include + +#include "avcodec.h" + +/** Maximum number of channels that can be decoded. */ +#define MAX_CHANNELS 16 + +/** Maximum number of matrices used in decoding; most streams have one matrix + * per output channel, but some rematrix a channel (usually 0) more than once. + */ +#define MAX_MATRICES 15 + +/** Maximum number of substreams that can be decoded. This could also be set + * higher, but I haven't seen any examples with more than two. + */ +#define MAX_SUBSTREAMS 2 + +/** maximum sample frequency seen in files */ +#define MAX_SAMPLERATE 192000 + +/** maximum number of audio samples within one access unit */ +#define MAX_BLOCKSIZE (40 * (MAX_SAMPLERATE / 48000)) +/** next power of two greater than MAX_BLOCKSIZE */ +#define MAX_BLOCKSIZE_POW2 (64 * (MAX_SAMPLERATE / 48000)) + +/** number of allowed filters */ +#define NUM_FILTERS 2 + +/** The maximum number of taps in either the IIR or FIR filter; + * I believe MLP actually specifies the maximum order for IIR filters as four, + * and that the sum of the orders of both filters must be <= 8. +*/ +#define MAX_FILTER_ORDER 8 + +/** Code that signals end of a stream. */ +#define END_OF_STREAM 0xd234d234 + +#define FIR 0 +#define IIR 1 + +/** filter data */ +typedef struct { + uint8_t order; ///< number of taps in filter + uint8_t shift; ///< Right shift to apply to output of filter. + + int32_t coeff[MAX_FILTER_ORDER]; + int32_t state[MAX_FILTER_ORDER]; +} FilterParams; + +/** sample data coding information */ +typedef struct { + FilterParams filter_params[NUM_FILTERS]; + + int16_t huff_offset; ///< Offset to apply to residual values. + int32_t sign_huff_offset; ///< sign/rounding-corrected version of huff_offset + uint8_t codebook; ///< Which VLC codebook to use to read residuals. + uint8_t huff_lsbs; ///< Size of residual suffix not encoded using VLC. +} ChannelParams; + +/** Tables defining the Huffman codes. + * There are three entropy coding methods used in MLP (four if you count + * "none" as a method). These use the same sequences for codes starting with + * 00 or 01, but have different codes starting with 1. + */ +extern const uint8_t ff_mlp_huffman_tables[3][18][2]; + +/** MLP uses checksums that seem to be based on the standard CRC algorithm, but + * are not (in implementation terms, the table lookup and XOR are reversed). + * We can implement this behavior using a standard av_crc on all but the + * last element, then XOR that with the last element. + */ +uint8_t ff_mlp_checksum8 (const uint8_t *buf, unsigned int buf_size); +uint16_t ff_mlp_checksum16(const uint8_t *buf, unsigned int buf_size); + +/** Calculate an 8-bit checksum over a restart header -- a non-multiple-of-8 + * number of bits, starting two bits into the first byte of buf. + */ +uint8_t ff_mlp_restart_checksum(const uint8_t *buf, unsigned int bit_size); + +/** XOR together all the bytes of a buffer. + * Does this belong in dspcontext? + */ +uint8_t ff_mlp_calculate_parity(const uint8_t *buf, unsigned int buf_size); + +int ff_mlp_init_crc2D(AVCodecParserContext *s); + +void ff_mlp_init_crc(); + +/** XOR four bytes into one. */ +static inline uint8_t xor_32_to_8(uint32_t value) +{ + value ^= value >> 16; + value ^= value >> 8; + return value; +} + +#endif /* FFMPEG_MLP_H */ diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/mlp_parser.c b/src/add-ons/media/plugins/avcodec/libavcodec/mlp_parser.c new file mode 100644 index 0000000000..4400d71fc4 --- /dev/null +++ b/src/add-ons/media/plugins/avcodec/libavcodec/mlp_parser.c @@ -0,0 +1,289 @@ +/* + * MLP parser + * Copyright (c) 2007 Ian Caulfield + * + * This file is part of FFmpeg. + * + * FFmpeg 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.1 of the License, or (at your option) any later version. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file mlp_parser.c + * MLP parser + */ + +#include + +#include "libavutil/crc.h" +#include "bitstream.h" +#include "parser.h" +#include "mlp_parser.h" +#include "mlp.h" + +static const uint8_t mlp_quants[16] = { + 16, 20, 24, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, +}; + +static const uint8_t mlp_channels[32] = { + 1, 2, 3, 4, 3, 4, 5, 3, 4, 5, 4, 5, 6, 4, 5, 4, + 5, 6, 5, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +}; + +static const uint8_t thd_chancount[13] = { +// LR C LFE LRs LRvh LRc LRrs Cs Ts LRsd LRw Cvh LFE2 + 2, 1, 1, 2, 2, 2, 2, 1, 1, 2, 2, 1, 1 +}; + +static int mlp_samplerate(int in) +{ + if (in == 0xF) + return 0; + + return (in & 8 ? 44100 : 48000) << (in & 7) ; +} + +static int truehd_channels(int chanmap) +{ + int channels = 0, i; + + for (i = 0; i < 13; i++) + channels += thd_chancount[i] * ((chanmap >> i) & 1); + + return channels; +} + +/** Read a major sync info header - contains high level information about + * the stream - sample rate, channel arrangement etc. Most of this + * information is not actually necessary for decoding, only for playback. + * gb must be a freshly initialized GetBitContext with no bits read. + */ + +int ff_mlp_read_major_sync(void *log, MLPHeaderInfo *mh, GetBitContext *gb) +{ + int ratebits; + uint16_t checksum; + + assert(get_bits_count(gb) == 0); + + if (gb->size_in_bits < 28 << 3) { + av_log(log, AV_LOG_ERROR, "packet too short, unable to read major sync\n"); + return -1; + } + + checksum = ff_mlp_checksum16(gb->buffer, 26); + if (checksum != AV_RL16(gb->buffer+26)) { + av_log(log, AV_LOG_ERROR, "major sync info header checksum error\n"); + return -1; + } + + if (get_bits_long(gb, 24) != 0xf8726f) /* Sync words */ + return -1; + + mh->stream_type = get_bits(gb, 8); + + if (mh->stream_type == 0xbb) { + mh->group1_bits = mlp_quants[get_bits(gb, 4)]; + mh->group2_bits = mlp_quants[get_bits(gb, 4)]; + + ratebits = get_bits(gb, 4); + mh->group1_samplerate = mlp_samplerate(ratebits); + mh->group2_samplerate = mlp_samplerate(get_bits(gb, 4)); + + skip_bits(gb, 11); + + mh->channels_mlp = get_bits(gb, 5); + } else if (mh->stream_type == 0xba) { + mh->group1_bits = 24; // TODO: Is this information actually conveyed anywhere? + mh->group2_bits = 0; + + ratebits = get_bits(gb, 4); + mh->group1_samplerate = mlp_samplerate(ratebits); + mh->group2_samplerate = 0; + + skip_bits(gb, 8); + + mh->channels_thd_stream1 = get_bits(gb, 5); + + skip_bits(gb, 2); + + mh->channels_thd_stream2 = get_bits(gb, 13); + } else + return -1; + + mh->access_unit_size = 40 << (ratebits & 7); + mh->access_unit_size_pow2 = 64 << (ratebits & 7); + + skip_bits_long(gb, 48); + + mh->is_vbr = get_bits1(gb); + + mh->peak_bitrate = (get_bits(gb, 15) * mh->group1_samplerate + 8) >> 4; + + mh->num_substreams = get_bits(gb, 4); + + skip_bits_long(gb, 4 + 11 * 8); + + return 0; +} + +typedef struct MLPParseContext +{ + ParseContext pc; + + int bytes_left; + + int in_sync; + + int num_substreams; +} MLPParseContext; + +static int mlp_parse(AVCodecParserContext *s, + AVCodecContext *avctx, + const uint8_t **poutbuf, int *poutbuf_size, + const uint8_t *buf, int buf_size) +{ + MLPParseContext *mp = s->priv_data; + int sync_present; + uint8_t parity_bits; + int next; + int i, p = 0; + + *poutbuf_size = 0; + if (buf_size == 0) + return 0; + + if (!mp->in_sync) { + // Not in sync - find a major sync header + + for (i = 0; i < buf_size; i++) { + mp->pc.state = (mp->pc.state << 8) | buf[i]; + if ((mp->pc.state & 0xfffffffe) == 0xf8726fba) { + mp->in_sync = 1; + mp->bytes_left = 0; + break; + } + } + + if (!mp->in_sync) { + ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size); + return buf_size; + } + + ff_combine_frame(&mp->pc, i - 7, &buf, &buf_size); + + return i - 7; + } + + if (mp->bytes_left == 0) { + // Find length of this packet + + /* Copy overread bytes from last frame into buffer. */ + for(; mp->pc.overread>0; mp->pc.overread--) { + mp->pc.buffer[mp->pc.index++]= mp->pc.buffer[mp->pc.overread_index++]; + } + + if (mp->pc.index + buf_size < 2) { + ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size); + return buf_size; + } + + mp->bytes_left = ((mp->pc.index > 0 ? mp->pc.buffer[0] : buf[0]) << 8) + | (mp->pc.index > 1 ? mp->pc.buffer[1] : buf[1-mp->pc.index]); + mp->bytes_left = (mp->bytes_left & 0xfff) * 2; + mp->bytes_left -= mp->pc.index; + } + + next = (mp->bytes_left > buf_size) ? END_NOT_FOUND : mp->bytes_left; + + if (ff_combine_frame(&mp->pc, next, &buf, &buf_size) < 0) { + mp->bytes_left -= buf_size; + return buf_size; + } + + mp->bytes_left = 0; + + sync_present = (AV_RB32(buf + 4) & 0xfffffffe) == 0xf8726fba; + + if (!sync_present) { + /* The first nibble of a frame is a parity check of the 4-byte + * access unit header and all the 2- or 4-byte substream headers. */ + // Only check when this isn't a sync frame - syncs have a checksum. + + parity_bits = 0; + for (i = -1; i < mp->num_substreams; i++) { + parity_bits ^= buf[p++]; + parity_bits ^= buf[p++]; + + if (i < 0 || buf[p-2] & 0x80) { + parity_bits ^= buf[p++]; + parity_bits ^= buf[p++]; + } + } + + if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) { + av_log(avctx, AV_LOG_INFO, "mlpparse: Parity check failed.\n"); + goto lost_sync; + } + } else { + GetBitContext gb; + MLPHeaderInfo mh; + + init_get_bits(&gb, buf + 4, (buf_size - 4) << 3); + if (ff_mlp_read_major_sync(avctx, &mh, &gb) < 0) + goto lost_sync; + +#ifdef CONFIG_AUDIO_NONSHORT + avctx->bits_per_sample = mh.group1_bits; + if (avctx->bits_per_sample > 16) + avctx->sample_fmt = SAMPLE_FMT_S32; +#endif + avctx->sample_rate = mh.group1_samplerate; + avctx->frame_size = mh.access_unit_size; + + if (mh.stream_type == 0xbb) { + /* MLP stream */ + avctx->channels = mlp_channels[mh.channels_mlp]; + } else { /* mh.stream_type == 0xba */ + /* TrueHD stream */ + if (mh.channels_thd_stream2) + avctx->channels = truehd_channels(mh.channels_thd_stream2); + else + avctx->channels = truehd_channels(mh.channels_thd_stream1); + } + + if (!mh.is_vbr) /* Stream is CBR */ + avctx->bit_rate = mh.peak_bitrate; + + mp->num_substreams = mh.num_substreams; + } + + *poutbuf = buf; + *poutbuf_size = buf_size; + + return next; + +lost_sync: + mp->in_sync = 0; + return 1; +} + +AVCodecParser mlp_parser = { + { CODEC_ID_MLP }, + sizeof(MLPParseContext), + ff_mlp_init_crc2D, + mlp_parse, + NULL, +}; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/mlp_parser.h b/src/add-ons/media/plugins/avcodec/libavcodec/mlp_parser.h new file mode 100644 index 0000000000..c3d26add4f --- /dev/null +++ b/src/add-ons/media/plugins/avcodec/libavcodec/mlp_parser.h @@ -0,0 +1,59 @@ +/* + * MLP parser prototypes + * Copyright (c) 2007 Ian Caulfield + * + * This file is part of FFmpeg. + * + * FFmpeg 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.1 of the License, or (at your option) any later version. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file mlp_parser.h + * MLP parser prototypes + */ + +#ifndef FFMPEG_MLP_PARSER_H +#define FFMPEG_MLP_PARSER_H + +#include "bitstream.h" + +typedef struct MLPHeaderInfo +{ + int stream_type; ///< 0xBB for MLP, 0xBA for TrueHD + + int group1_bits; ///< The bit depth of the first substream + int group2_bits; ///< Bit depth of the second substream (MLP only) + + int group1_samplerate; ///< Sample rate of first substream + int group2_samplerate; ///< Sample rate of second substream (MLP only) + + int channels_mlp; ///< Channel arrangement for MLP streams + int channels_thd_stream1; ///< Channel arrangement for substream 1 of TrueHD streams (5.1) + int channels_thd_stream2; ///< Channel arrangement for substream 2 of TrueHD streams (7.1) + + int access_unit_size; ///< Number of samples per coded frame + int access_unit_size_pow2; ///< Next power of two above number of samples per frame + + int is_vbr; ///< Stream is VBR instead of CBR + int peak_bitrate; ///< Peak bitrate for VBR, actual bitrate (==peak) for CBR + + int num_substreams; ///< Number of substreams within stream +} MLPHeaderInfo; + + +int ff_mlp_read_major_sync(void *log, MLPHeaderInfo *mh, GetBitContext *gb); + +#endif /* FFMPEG_MLP_PARSER_H */ + diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/mlpdec.c b/src/add-ons/media/plugins/avcodec/libavcodec/mlpdec.c new file mode 100644 index 0000000000..7272458085 --- /dev/null +++ b/src/add-ons/media/plugins/avcodec/libavcodec/mlpdec.c @@ -0,0 +1,1053 @@ +/* + * MLP decoder + * Copyright (c) 2007-2008 Ian Caulfield + * + * This file is part of FFmpeg. + * + * FFmpeg 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.1 of the License, or (at your option) any later version. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file mlpdec.c + * MLP decoder + */ + +#include + +#include "avcodec.h" +#include "libavutil/intreadwrite.h" +#include "bitstream.h" +#include "libavutil/crc.h" +#include "parser.h" +#include "mlp_parser.h" +#include "mlp.h" + +/** number of bits used for VLC lookup - longest Huffman code is 9 */ +#define VLC_BITS 9 + + +static const char* sample_message = + "Please file a bug report following the instructions at " + "http://ffmpeg.mplayerhq.hu/bugreports.html and include " + "a sample of this file."; + +typedef struct SubStream { + //! Set if a valid restart header has been read. Otherwise the substream cannot be decoded. + uint8_t restart_seen; + + //@{ + /** restart header data */ + //! The type of noise to be used in the rematrix stage. + uint16_t noise_type; + + //! The index of the first channel coded in this substream. + uint8_t min_channel; + //! The index of the last channel coded in this substream. + uint8_t max_channel; + //! The number of channels input into the rematrix stage. + uint8_t max_matrix_channel; + + //! The left shift applied to random noise in 0x31ea substreams. + uint8_t noise_shift; + //! The current seed value for the pseudorandom noise generator(s). + uint32_t noisegen_seed; + + //! Set if the substream contains extra info to check the size of VLC blocks. + uint8_t data_check_present; + + //! Bitmask of which parameter sets are conveyed in a decoding parameter block. + uint8_t param_presence_flags; +#define PARAM_BLOCKSIZE (1 << 7) +#define PARAM_MATRIX (1 << 6) +#define PARAM_OUTSHIFT (1 << 5) +#define PARAM_QUANTSTEP (1 << 4) +#define PARAM_FIR (1 << 3) +#define PARAM_IIR (1 << 2) +#define PARAM_HUFFOFFSET (1 << 1) + //@} + + //@{ + /** matrix data */ + + //! Number of matrices to be applied. + uint8_t num_primitive_matrices; + + //! matrix output channel + uint8_t matrix_out_ch[MAX_MATRICES]; + + //! Whether the LSBs of the matrix output are encoded in the bitstream. + uint8_t lsb_bypass[MAX_MATRICES]; + //! Matrix coefficients, stored as 2.14 fixed point. + int32_t matrix_coeff[MAX_MATRICES][MAX_CHANNELS+2]; + //! Left shift to apply to noise values in 0x31eb substreams. + uint8_t matrix_noise_shift[MAX_MATRICES]; + //@} + + //! Left shift to apply to Huffman-decoded residuals. + uint8_t quant_step_size[MAX_CHANNELS]; + + //! number of PCM samples in current audio block + uint16_t blocksize; + //! Number of PCM samples decoded so far in this frame. + uint16_t blockpos; + + //! Left shift to apply to decoded PCM values to get final 24-bit output. + int8_t output_shift[MAX_CHANNELS]; + + //! Running XOR of all output samples. + int32_t lossless_check_data; + +} SubStream; + +typedef struct MLPDecodeContext { + AVCodecContext *avctx; + + //! Set if a valid major sync block has been read. Otherwise no decoding is possible. + uint8_t params_valid; + + //! Number of substreams contained within this stream. + uint8_t num_substreams; + + //! Index of the last substream to decode - further substreams are skipped. + uint8_t max_decoded_substream; + + //! number of PCM samples contained in each frame + int access_unit_size; + //! next power of two above the number of samples in each frame + int access_unit_size_pow2; + + SubStream substream[MAX_SUBSTREAMS]; + + ChannelParams channel_params[MAX_CHANNELS]; + + int8_t noise_buffer[MAX_BLOCKSIZE_POW2]; + int8_t bypassed_lsbs[MAX_BLOCKSIZE][MAX_CHANNELS]; + int32_t sample_buffer[MAX_BLOCKSIZE][MAX_CHANNELS+2]; +} MLPDecodeContext; + +static VLC huff_vlc[3]; + +/** Initialize static data, constant between all invocations of the codec. */ + +static av_cold void init_static() +{ + INIT_VLC_STATIC(&huff_vlc[0], VLC_BITS, 18, + &ff_mlp_huffman_tables[0][0][1], 2, 1, + &ff_mlp_huffman_tables[0][0][0], 2, 1, 512); + INIT_VLC_STATIC(&huff_vlc[1], VLC_BITS, 16, + &ff_mlp_huffman_tables[1][0][1], 2, 1, + &ff_mlp_huffman_tables[1][0][0], 2, 1, 512); + INIT_VLC_STATIC(&huff_vlc[2], VLC_BITS, 15, + &ff_mlp_huffman_tables[2][0][1], 2, 1, + &ff_mlp_huffman_tables[2][0][0], 2, 1, 512); + + ff_mlp_init_crc(); +} + +static inline int32_t calculate_sign_huff(MLPDecodeContext *m, + unsigned int substr, unsigned int ch) +{ + ChannelParams *cp = &m->channel_params[ch]; + SubStream *s = &m->substream[substr]; + int lsb_bits = cp->huff_lsbs - s->quant_step_size[ch]; + int sign_shift = lsb_bits + (cp->codebook ? 2 - cp->codebook : -1); + int32_t sign_huff_offset = cp->huff_offset; + + if (cp->codebook > 0) + sign_huff_offset -= 7 << lsb_bits; + + if (sign_shift >= 0) + sign_huff_offset -= 1 << sign_shift; + + return sign_huff_offset; +} + +/** Read a sample, consisting of either, both or neither of entropy-coded MSBs + * and plain LSBs. */ + +static inline int read_huff_channels(MLPDecodeContext *m, GetBitContext *gbp, + unsigned int substr, unsigned int pos) +{ + SubStream *s = &m->substream[substr]; + unsigned int mat, channel; + + for (mat = 0; mat < s->num_primitive_matrices; mat++) + if (s->lsb_bypass[mat]) + m->bypassed_lsbs[pos + s->blockpos][mat] = get_bits1(gbp); + + for (channel = s->min_channel; channel <= s->max_channel; channel++) { + ChannelParams *cp = &m->channel_params[channel]; + int codebook = cp->codebook; + int quant_step_size = s->quant_step_size[channel]; + int lsb_bits = cp->huff_lsbs - quant_step_size; + int result = 0; + + if (codebook > 0) + result = get_vlc2(gbp, huff_vlc[codebook-1].table, + VLC_BITS, (9 + VLC_BITS - 1) / VLC_BITS); + + if (result < 0) + return -1; + + if (lsb_bits > 0) + result = (result << lsb_bits) + get_bits(gbp, lsb_bits); + + result += cp->sign_huff_offset; + result <<= quant_step_size; + + m->sample_buffer[pos + s->blockpos][channel] = result; + } + + return 0; +} + +static av_cold int mlp_decode_init(AVCodecContext *avctx) +{ + MLPDecodeContext *m = avctx->priv_data; + int substr; + + init_static(); + m->avctx = avctx; + for (substr = 0; substr < MAX_SUBSTREAMS; substr++) + m->substream[substr].lossless_check_data = 0xffffffff; + avctx->sample_fmt = SAMPLE_FMT_S16; + return 0; +} + +/** Read a major sync info header - contains high level information about + * the stream - sample rate, channel arrangement etc. Most of this + * information is not actually necessary for decoding, only for playback. + */ + +static int read_major_sync(MLPDecodeContext *m, GetBitContext *gb) +{ + MLPHeaderInfo mh; + int substr; + + if (ff_mlp_read_major_sync(m->avctx, &mh, gb) != 0) + return -1; + + if (mh.group1_bits == 0) { + av_log(m->avctx, AV_LOG_ERROR, "invalid/unknown bits per sample\n"); + return -1; + } + if (mh.group2_bits > mh.group1_bits) { + av_log(m->avctx, AV_LOG_ERROR, + "Channel group 2 cannot have more bits per sample than group 1.\n"); + return -1; + } + + if (mh.group2_samplerate && mh.group2_samplerate != mh.group1_samplerate) { + av_log(m->avctx, AV_LOG_ERROR, + "Channel groups with differing sample rates are not currently supported.\n"); + return -1; + } + + if (mh.group1_samplerate == 0) { + av_log(m->avctx, AV_LOG_ERROR, "invalid/unknown sampling rate\n"); + return -1; + } + if (mh.group1_samplerate > MAX_SAMPLERATE) { + av_log(m->avctx, AV_LOG_ERROR, + "Sampling rate %d is greater than the supported maximum (%d).\n", + mh.group1_samplerate, MAX_SAMPLERATE); + return -1; + } + if (mh.access_unit_size > MAX_BLOCKSIZE) { + av_log(m->avctx, AV_LOG_ERROR, + "Block size %d is greater than the supported maximum (%d).\n", + mh.access_unit_size, MAX_BLOCKSIZE); + return -1; + } + if (mh.access_unit_size_pow2 > MAX_BLOCKSIZE_POW2) { + av_log(m->avctx, AV_LOG_ERROR, + "Block size pow2 %d is greater than the supported maximum (%d).\n", + mh.access_unit_size_pow2, MAX_BLOCKSIZE_POW2); + return -1; + } + + if (mh.num_substreams == 0) + return -1; + if (mh.num_substreams > MAX_SUBSTREAMS) { + av_log(m->avctx, AV_LOG_ERROR, + "Number of substreams %d is larger than the maximum supported " + "by the decoder. %s\n", mh.num_substreams, sample_message); + return -1; + } + + m->access_unit_size = mh.access_unit_size; + m->access_unit_size_pow2 = mh.access_unit_size_pow2; + + m->num_substreams = mh.num_substreams; + m->max_decoded_substream = m->num_substreams - 1; + + m->avctx->sample_rate = mh.group1_samplerate; + m->avctx->frame_size = mh.access_unit_size; + +#ifdef CONFIG_AUDIO_NONSHORT + m->avctx->bits_per_sample = mh.group1_bits; + if (mh.group1_bits > 16) { + m->avctx->sample_fmt = SAMPLE_FMT_S32; + } +#endif + + m->params_valid = 1; + for (substr = 0; substr < MAX_SUBSTREAMS; substr++) + m->substream[substr].restart_seen = 0; + + return 0; +} + +/** Read a restart header from a block in a substream. This contains parameters + * required to decode the audio that do not change very often. Generally + * (always) present only in blocks following a major sync. */ + +static int read_restart_header(MLPDecodeContext *m, GetBitContext *gbp, + const uint8_t *buf, unsigned int substr) +{ + SubStream *s = &m->substream[substr]; + unsigned int ch; + int sync_word, tmp; + uint8_t checksum; + uint8_t lossless_check; + int start_count = get_bits_count(gbp); + + sync_word = get_bits(gbp, 13); + + if (sync_word != 0x31ea >> 1) { + av_log(m->avctx, AV_LOG_ERROR, + "restart header sync incorrect (got 0x%04x)\n", sync_word); + return -1; + } + s->noise_type = get_bits1(gbp); + + skip_bits(gbp, 16); /* Output timestamp */ + + s->min_channel = get_bits(gbp, 4); + s->max_channel = get_bits(gbp, 4); + s->max_matrix_channel = get_bits(gbp, 4); + + if (s->min_channel > s->max_channel) { + av_log(m->avctx, AV_LOG_ERROR, + "Substream min channel cannot be greater than max channel.\n"); + return -1; + } + + if (m->avctx->request_channels > 0 + && s->max_channel + 1 >= m->avctx->request_channels + && substr < m->max_decoded_substream) { + av_log(m->avctx, AV_LOG_INFO, + "Extracting %d channel downmix from substream %d. " + "Further substreams will be skipped.\n", + s->max_channel + 1, substr); + m->max_decoded_substream = substr; + } + + s->noise_shift = get_bits(gbp, 4); + s->noisegen_seed = get_bits(gbp, 23); + + skip_bits(gbp, 19); + + s->data_check_present = get_bits1(gbp); + lossless_check = get_bits(gbp, 8); + if (substr == m->max_decoded_substream + && s->lossless_check_data != 0xffffffff) { + tmp = xor_32_to_8(s->lossless_check_data); + if (tmp != lossless_check) + av_log(m->avctx, AV_LOG_WARNING, + "Lossless check failed - expected %02x, calculated %02x.\n", + lossless_check, tmp); + else + dprintf(m->avctx, "Lossless check passed for substream %d (%x).\n", + substr, tmp); + } + + skip_bits(gbp, 16); + + for (ch = 0; ch <= s->max_matrix_channel; ch++) { + int ch_assign = get_bits(gbp, 6); + dprintf(m->avctx, "ch_assign[%d][%d] = %d\n", substr, ch, + ch_assign); + if (ch_assign != ch) { + av_log(m->avctx, AV_LOG_ERROR, + "Non-1:1 channel assignments are used in this stream. %s\n", + sample_message); + return -1; + } + } + + checksum = ff_mlp_restart_checksum(buf, get_bits_count(gbp) - start_count); + + if (checksum != get_bits(gbp, 8)) + av_log(m->avctx, AV_LOG_ERROR, "restart header checksum error\n"); + + /* Set default decoding parameters. */ + s->param_presence_flags = 0xff; + s->num_primitive_matrices = 0; + s->blocksize = 8; + s->lossless_check_data = 0; + + memset(s->output_shift , 0, sizeof(s->output_shift )); + memset(s->quant_step_size, 0, sizeof(s->quant_step_size)); + + for (ch = s->min_channel; ch <= s->max_channel; ch++) { + ChannelParams *cp = &m->channel_params[ch]; + cp->filter_params[FIR].order = 0; + cp->filter_params[IIR].order = 0; + cp->filter_params[FIR].shift = 0; + cp->filter_params[IIR].shift = 0; + + /* Default audio coding is 24-bit raw PCM. */ + cp->huff_offset = 0; + cp->sign_huff_offset = (-1) << 23; + cp->codebook = 0; + cp->huff_lsbs = 24; + } + + if (substr == m->max_decoded_substream) { + m->avctx->channels = s->max_channel + 1; + } + + return 0; +} + +/** Read parameters for one of the prediction filters. */ + +static int read_filter_params(MLPDecodeContext *m, GetBitContext *gbp, + unsigned int channel, unsigned int filter) +{ + FilterParams *fp = &m->channel_params[channel].filter_params[filter]; + const char fchar = filter ? 'I' : 'F'; + int i, order; + + // Filter is 0 for FIR, 1 for IIR. + assert(filter < 2); + + order = get_bits(gbp, 4); + if (order > MAX_FILTER_ORDER) { + av_log(m->avctx, AV_LOG_ERROR, + "%cIR filter order %d is greater than maximum %d.\n", + fchar, order, MAX_FILTER_ORDER); + return -1; + } + fp->order = order; + + if (order > 0) { + int coeff_bits, coeff_shift; + + fp->shift = get_bits(gbp, 4); + + coeff_bits = get_bits(gbp, 5); + coeff_shift = get_bits(gbp, 3); + if (coeff_bits < 1 || coeff_bits > 16) { + av_log(m->avctx, AV_LOG_ERROR, + "%cIR filter coeff_bits must be between 1 and 16.\n", + fchar); + return -1; + } + if (coeff_bits + coeff_shift > 16) { + av_log(m->avctx, AV_LOG_ERROR, + "Sum of coeff_bits and coeff_shift for %cIR filter must be 16 or less.\n", + fchar); + return -1; + } + + for (i = 0; i < order; i++) + fp->coeff[i] = get_sbits(gbp, coeff_bits) << coeff_shift; + + if (get_bits1(gbp)) { + int state_bits, state_shift; + + if (filter == FIR) { + av_log(m->avctx, AV_LOG_ERROR, + "FIR filter has state data specified.\n"); + return -1; + } + + state_bits = get_bits(gbp, 4); + state_shift = get_bits(gbp, 4); + + /* TODO: Check validity of state data. */ + + for (i = 0; i < order; i++) + fp->state[i] = get_sbits(gbp, state_bits) << state_shift; + } + } + + return 0; +} + +/** Read decoding parameters that change more often than those in the restart + * header. */ + +static int read_decoding_params(MLPDecodeContext *m, GetBitContext *gbp, + unsigned int substr) +{ + SubStream *s = &m->substream[substr]; + unsigned int mat, ch; + + if (get_bits1(gbp)) + s->param_presence_flags = get_bits(gbp, 8); + + if (s->param_presence_flags & PARAM_BLOCKSIZE) + if (get_bits1(gbp)) { + s->blocksize = get_bits(gbp, 9); + if (s->blocksize > MAX_BLOCKSIZE) { + av_log(m->avctx, AV_LOG_ERROR, "block size too large\n"); + s->blocksize = 0; + return -1; + } + } + + if (s->param_presence_flags & PARAM_MATRIX) + if (get_bits1(gbp)) { + s->num_primitive_matrices = get_bits(gbp, 4); + + for (mat = 0; mat < s->num_primitive_matrices; mat++) { + int frac_bits, max_chan; + s->matrix_out_ch[mat] = get_bits(gbp, 4); + frac_bits = get_bits(gbp, 4); + s->lsb_bypass [mat] = get_bits1(gbp); + + if (s->matrix_out_ch[mat] > s->max_channel) { + av_log(m->avctx, AV_LOG_ERROR, + "Invalid channel %d specified as output from matrix.\n", + s->matrix_out_ch[mat]); + return -1; + } + if (frac_bits > 14) { + av_log(m->avctx, AV_LOG_ERROR, + "Too many fractional bits specified.\n"); + return -1; + } + + max_chan = s->max_matrix_channel; + if (!s->noise_type) + max_chan+=2; + + for (ch = 0; ch <= max_chan; ch++) { + int coeff_val = 0; + if (get_bits1(gbp)) + coeff_val = get_sbits(gbp, frac_bits + 2); + + s->matrix_coeff[mat][ch] = coeff_val << (14 - frac_bits); + } + + if (s->noise_type) + s->matrix_noise_shift[mat] = get_bits(gbp, 4); + else + s->matrix_noise_shift[mat] = 0; + } + } + + if (s->param_presence_flags & PARAM_OUTSHIFT) + if (get_bits1(gbp)) + for (ch = 0; ch <= s->max_matrix_channel; ch++) { + s->output_shift[ch] = get_bits(gbp, 4); + dprintf(m->avctx, "output shift[%d] = %d\n", + ch, s->output_shift[ch]); + /* TODO: validate */ + } + + if (s->param_presence_flags & PARAM_QUANTSTEP) + if (get_bits1(gbp)) + for (ch = 0; ch <= s->max_channel; ch++) { + ChannelParams *cp = &m->channel_params[ch]; + + s->quant_step_size[ch] = get_bits(gbp, 4); + /* TODO: validate */ + + cp->sign_huff_offset = calculate_sign_huff(m, substr, ch); + } + + for (ch = s->min_channel; ch <= s->max_channel; ch++) + if (get_bits1(gbp)) { + ChannelParams *cp = &m->channel_params[ch]; + FilterParams *fir = &cp->filter_params[FIR]; + FilterParams *iir = &cp->filter_params[IIR]; + + if (s->param_presence_flags & PARAM_FIR) + if (get_bits1(gbp)) + if (read_filter_params(m, gbp, ch, FIR) < 0) + return -1; + + if (s->param_presence_flags & PARAM_IIR) + if (get_bits1(gbp)) + if (read_filter_params(m, gbp, ch, IIR) < 0) + return -1; + + if (fir->order && iir->order && + fir->shift != iir->shift) { + av_log(m->avctx, AV_LOG_ERROR, + "FIR and IIR filters must use the same precision.\n"); + return -1; + } + /* The FIR and IIR filters must have the same precision. + * To simplify the filtering code, only the precision of the + * FIR filter is considered. If only the IIR filter is employed, + * the FIR filter precision is set to that of the IIR filter, so + * that the filtering code can use it. */ + if (!fir->order && iir->order) + fir->shift = iir->shift; + + if (s->param_presence_flags & PARAM_HUFFOFFSET) + if (get_bits1(gbp)) + cp->huff_offset = get_sbits(gbp, 15); + + cp->codebook = get_bits(gbp, 2); + cp->huff_lsbs = get_bits(gbp, 5); + + cp->sign_huff_offset = calculate_sign_huff(m, substr, ch); + + /* TODO: validate */ + } + + return 0; +} + +#define MSB_MASK(bits) (-1u << bits) + +/** Generate PCM samples using the prediction filters and residual values + * read from the data stream, and update the filter state. */ + +static void filter_channel(MLPDecodeContext *m, unsigned int substr, + unsigned int channel) +{ + SubStream *s = &m->substream[substr]; + int32_t filter_state_buffer[NUM_FILTERS][MAX_BLOCKSIZE + MAX_FILTER_ORDER]; + FilterParams *fp[NUM_FILTERS] = { &m->channel_params[channel].filter_params[FIR], + &m->channel_params[channel].filter_params[IIR], }; + unsigned int filter_shift = fp[FIR]->shift; + int32_t mask = MSB_MASK(s->quant_step_size[channel]); + int index = MAX_BLOCKSIZE; + int j, i; + + for (j = 0; j < NUM_FILTERS; j++) { + memcpy(&filter_state_buffer[j][MAX_BLOCKSIZE], &fp[j]->state[0], + MAX_FILTER_ORDER * sizeof(int32_t)); + } + + for (i = 0; i < s->blocksize; i++) { + int32_t residual = m->sample_buffer[i + s->blockpos][channel]; + unsigned int order; + int64_t accum = 0; + int32_t result; + + /* TODO: Move this code to DSPContext? */ + + for (j = 0; j < NUM_FILTERS; j++) + for (order = 0; order < fp[j]->order; order++) + accum += (int64_t)filter_state_buffer[j][index + order] * + fp[j]->coeff[order]; + + accum = accum >> filter_shift; + result = (accum + residual) & mask; + + --index; + + filter_state_buffer[FIR][index] = result; + filter_state_buffer[IIR][index] = result - accum; + + m->sample_buffer[i + s->blockpos][channel] = result; + } + + for (j = 0; j < NUM_FILTERS; j++) { + memcpy(&fp[j]->state[0], &filter_state_buffer[j][index], + MAX_FILTER_ORDER * sizeof(int32_t)); + } +} + +/** Read a block of PCM residual data (or actual if no filtering active). */ + +static int read_block_data(MLPDecodeContext *m, GetBitContext *gbp, + unsigned int substr) +{ + SubStream *s = &m->substream[substr]; + unsigned int i, ch, expected_stream_pos = 0; + + if (s->data_check_present) { + expected_stream_pos = get_bits_count(gbp); + expected_stream_pos += get_bits(gbp, 16); + av_log(m->avctx, AV_LOG_WARNING, "This file contains some features " + "we have not tested yet. %s\n", sample_message); + } + + if (s->blockpos + s->blocksize > m->access_unit_size) { + av_log(m->avctx, AV_LOG_ERROR, "too many audio samples in frame\n"); + return -1; + } + + memset(&m->bypassed_lsbs[s->blockpos][0], 0, + s->blocksize * sizeof(m->bypassed_lsbs[0])); + + for (i = 0; i < s->blocksize; i++) { + if (read_huff_channels(m, gbp, substr, i) < 0) + return -1; + } + + for (ch = s->min_channel; ch <= s->max_channel; ch++) { + filter_channel(m, substr, ch); + } + + s->blockpos += s->blocksize; + + if (s->data_check_present) { + if (get_bits_count(gbp) != expected_stream_pos) + av_log(m->avctx, AV_LOG_ERROR, "block data length mismatch\n"); + skip_bits(gbp, 8); + } + + return 0; +} + +/** Data table used for TrueHD noise generation function. */ + +static const int8_t noise_table[256] = { + 30, 51, 22, 54, 3, 7, -4, 38, 14, 55, 46, 81, 22, 58, -3, 2, + 52, 31, -7, 51, 15, 44, 74, 30, 85, -17, 10, 33, 18, 80, 28, 62, + 10, 32, 23, 69, 72, 26, 35, 17, 73, 60, 8, 56, 2, 6, -2, -5, + 51, 4, 11, 50, 66, 76, 21, 44, 33, 47, 1, 26, 64, 48, 57, 40, + 38, 16, -10, -28, 92, 22, -18, 29, -10, 5, -13, 49, 19, 24, 70, 34, + 61, 48, 30, 14, -6, 25, 58, 33, 42, 60, 67, 17, 54, 17, 22, 30, + 67, 44, -9, 50, -11, 43, 40, 32, 59, 82, 13, 49, -14, 55, 60, 36, + 48, 49, 31, 47, 15, 12, 4, 65, 1, 23, 29, 39, 45, -2, 84, 69, + 0, 72, 37, 57, 27, 41, -15, -16, 35, 31, 14, 61, 24, 0, 27, 24, + 16, 41, 55, 34, 53, 9, 56, 12, 25, 29, 53, 5, 20, -20, -8, 20, + 13, 28, -3, 78, 38, 16, 11, 62, 46, 29, 21, 24, 46, 65, 43, -23, + 89, 18, 74, 21, 38, -12, 19, 12, -19, 8, 15, 33, 4, 57, 9, -8, + 36, 35, 26, 28, 7, 83, 63, 79, 75, 11, 3, 87, 37, 47, 34, 40, + 39, 19, 20, 42, 27, 34, 39, 77, 13, 42, 59, 64, 45, -1, 32, 37, + 45, -5, 53, -6, 7, 36, 50, 23, 6, 32, 9, -21, 18, 71, 27, 52, + -25, 31, 35, 42, -1, 68, 63, 52, 26, 43, 66, 37, 41, 25, 40, 70, +}; + +/** Noise generation functions. + * I'm not sure what these are for - they seem to be some kind of pseudorandom + * sequence generators, used to generate noise data which is used when the + * channels are rematrixed. I'm not sure if they provide a practical benefit + * to compression, or just obfuscate the decoder. Are they for some kind of + * dithering? */ + +/** Generate two channels of noise, used in the matrix when + * restart sync word == 0x31ea. */ + +static void generate_2_noise_channels(MLPDecodeContext *m, unsigned int substr) +{ + SubStream *s = &m->substream[substr]; + unsigned int i; + uint32_t seed = s->noisegen_seed; + unsigned int maxchan = s->max_matrix_channel; + + for (i = 0; i < s->blockpos; i++) { + uint16_t seed_shr7 = seed >> 7; + m->sample_buffer[i][maxchan+1] = ((int8_t)(seed >> 15)) << s->noise_shift; + m->sample_buffer[i][maxchan+2] = ((int8_t) seed_shr7) << s->noise_shift; + + seed = (seed << 16) ^ seed_shr7 ^ (seed_shr7 << 5); + } + + s->noisegen_seed = seed; +} + +/** Generate a block of noise, used when restart sync word == 0x31eb. */ + +static void fill_noise_buffer(MLPDecodeContext *m, unsigned int substr) +{ + SubStream *s = &m->substream[substr]; + unsigned int i; + uint32_t seed = s->noisegen_seed; + + for (i = 0; i < m->access_unit_size_pow2; i++) { + uint8_t seed_shr15 = seed >> 15; + m->noise_buffer[i] = noise_table[seed_shr15]; + seed = (seed << 8) ^ seed_shr15 ^ (seed_shr15 << 5); + } + + s->noisegen_seed = seed; +} + + +/** Apply the channel matrices in turn to reconstruct the original audio + * samples. */ + +static void rematrix_channels(MLPDecodeContext *m, unsigned int substr) +{ + SubStream *s = &m->substream[substr]; + unsigned int mat, src_ch, i; + unsigned int maxchan; + + maxchan = s->max_matrix_channel; + if (!s->noise_type) { + generate_2_noise_channels(m, substr); + maxchan += 2; + } else { + fill_noise_buffer(m, substr); + } + + for (mat = 0; mat < s->num_primitive_matrices; mat++) { + int matrix_noise_shift = s->matrix_noise_shift[mat]; + unsigned int dest_ch = s->matrix_out_ch[mat]; + int32_t mask = MSB_MASK(s->quant_step_size[dest_ch]); + + /* TODO: DSPContext? */ + + for (i = 0; i < s->blockpos; i++) { + int64_t accum = 0; + for (src_ch = 0; src_ch <= maxchan; src_ch++) { + accum += (int64_t)m->sample_buffer[i][src_ch] + * s->matrix_coeff[mat][src_ch]; + } + if (matrix_noise_shift) { + uint32_t index = s->num_primitive_matrices - mat; + index = (i * (index * 2 + 1) + index) & (m->access_unit_size_pow2 - 1); + accum += m->noise_buffer[index] << (matrix_noise_shift + 7); + } + m->sample_buffer[i][dest_ch] = ((accum >> 14) & mask) + + m->bypassed_lsbs[i][mat]; + } + } +} + +/** Write the audio data into the output buffer. */ + +static int output_data_internal(MLPDecodeContext *m, unsigned int substr, + uint8_t *data, unsigned int *data_size, int is32) +{ + SubStream *s = &m->substream[substr]; + unsigned int i, ch = 0; + int32_t *data_32 = (int32_t*) data; + int16_t *data_16 = (int16_t*) data; + + if (*data_size < (s->max_channel + 1) * s->blockpos * (is32 ? 4 : 2)) + return -1; + + for (i = 0; i < s->blockpos; i++) { + for (ch = 0; ch <= s->max_channel; ch++) { + int32_t sample = m->sample_buffer[i][ch] << s->output_shift[ch]; + s->lossless_check_data ^= (sample & 0xffffff) << ch; + if (is32) *data_32++ = sample << 8; + else *data_16++ = sample >> 8; + } + } + + *data_size = i * ch * (is32 ? 4 : 2); + + return 0; +} + +static int output_data(MLPDecodeContext *m, unsigned int substr, + uint8_t *data, unsigned int *data_size) +{ + if (m->avctx->sample_fmt == SAMPLE_FMT_S32) + return output_data_internal(m, substr, data, data_size, 1); + else + return output_data_internal(m, substr, data, data_size, 0); +} + + +/** Read an access unit from the stream. + * Returns < 0 on error, 0 if not enough data is present in the input stream + * otherwise returns the number of bytes consumed. */ + +static int read_access_unit(AVCodecContext *avctx, void* data, int *data_size, + const uint8_t *buf, int buf_size) +{ + MLPDecodeContext *m = avctx->priv_data; + GetBitContext gb; + unsigned int length, substr; + unsigned int substream_start; + unsigned int header_size = 4; + unsigned int substr_header_size = 0; + uint8_t substream_parity_present[MAX_SUBSTREAMS]; + uint16_t substream_data_len[MAX_SUBSTREAMS]; + uint8_t parity_bits; + + if (buf_size < 4) + return 0; + + length = (AV_RB16(buf) & 0xfff) * 2; + + if (length > buf_size) + return -1; + + init_get_bits(&gb, (buf + 4), (length - 4) * 8); + + if (show_bits_long(&gb, 31) == (0xf8726fba >> 1)) { + dprintf(m->avctx, "Found major sync.\n"); + if (read_major_sync(m, &gb) < 0) + goto error; + header_size += 28; + } + + if (!m->params_valid) { + av_log(m->avctx, AV_LOG_WARNING, + "Stream parameters not seen; skipping frame.\n"); + *data_size = 0; + return length; + } + + substream_start = 0; + + for (substr = 0; substr < m->num_substreams; substr++) { + int extraword_present, checkdata_present, end; + + extraword_present = get_bits1(&gb); + skip_bits1(&gb); + checkdata_present = get_bits1(&gb); + skip_bits1(&gb); + + end = get_bits(&gb, 12) * 2; + + substr_header_size += 2; + + if (extraword_present) { + skip_bits(&gb, 16); + substr_header_size += 2; + } + + if (end + header_size + substr_header_size > length) { + av_log(m->avctx, AV_LOG_ERROR, + "Indicated length of substream %d data goes off end of " + "packet.\n", substr); + end = length - header_size - substr_header_size; + } + + if (end < substream_start) { + av_log(avctx, AV_LOG_ERROR, + "Indicated end offset of substream %d data " + "is smaller than calculated start offset.\n", + substr); + goto error; + } + + if (substr > m->max_decoded_substream) + continue; + + substream_parity_present[substr] = checkdata_present; + substream_data_len[substr] = end - substream_start; + substream_start = end; + } + + parity_bits = ff_mlp_calculate_parity(buf, 4); + parity_bits ^= ff_mlp_calculate_parity(buf + header_size, substr_header_size); + + if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) { + av_log(avctx, AV_LOG_ERROR, "Parity check failed.\n"); + goto error; + } + + buf += header_size + substr_header_size; + + for (substr = 0; substr <= m->max_decoded_substream; substr++) { + SubStream *s = &m->substream[substr]; + init_get_bits(&gb, buf, substream_data_len[substr] * 8); + + s->blockpos = 0; + do { + if (get_bits1(&gb)) { + if (get_bits1(&gb)) { + /* A restart header should be present. */ + if (read_restart_header(m, &gb, buf, substr) < 0) + goto next_substr; + s->restart_seen = 1; + } + + if (!s->restart_seen) { + av_log(m->avctx, AV_LOG_ERROR, + "No restart header present in substream %d.\n", + substr); + goto next_substr; + } + + if (read_decoding_params(m, &gb, substr) < 0) + goto next_substr; + } + + if (!s->restart_seen) { + av_log(m->avctx, AV_LOG_ERROR, + "No restart header present in substream %d.\n", + substr); + goto next_substr; + } + + if (read_block_data(m, &gb, substr) < 0) + return -1; + + } while ((get_bits_count(&gb) < substream_data_len[substr] * 8) + && get_bits1(&gb) == 0); + + skip_bits(&gb, (-get_bits_count(&gb)) & 15); + if (substream_data_len[substr] * 8 - get_bits_count(&gb) >= 32 && + (show_bits_long(&gb, 32) == END_OF_STREAM || + show_bits_long(&gb, 20) == 0xd234e)) { + skip_bits(&gb, 18); + if (substr == m->max_decoded_substream) + av_log(m->avctx, AV_LOG_INFO, "End of stream indicated.\n"); + + if (get_bits1(&gb)) { + int shorten_by = get_bits(&gb, 13); + shorten_by = FFMIN(shorten_by, s->blockpos); + s->blockpos -= shorten_by; + } else + skip_bits(&gb, 13); + } + if (substream_data_len[substr] * 8 - get_bits_count(&gb) >= 16 && + substream_parity_present[substr]) { + uint8_t parity, checksum; + + parity = ff_mlp_calculate_parity(buf, substream_data_len[substr] - 2); + if ((parity ^ get_bits(&gb, 8)) != 0xa9) + av_log(m->avctx, AV_LOG_ERROR, + "Substream %d parity check failed.\n", substr); + + checksum = ff_mlp_checksum8(buf, substream_data_len[substr] - 2); + if (checksum != get_bits(&gb, 8)) + av_log(m->avctx, AV_LOG_ERROR, "Substream %d checksum failed.\n", + substr); + } + if (substream_data_len[substr] * 8 != get_bits_count(&gb)) { + av_log(m->avctx, AV_LOG_ERROR, "substream %d length mismatch\n", + substr); + return -1; + } + +next_substr: + buf += substream_data_len[substr]; + } + + rematrix_channels(m, m->max_decoded_substream); + + if (output_data(m, m->max_decoded_substream, data, data_size) < 0) + return -1; + + return length; + +error: + m->params_valid = 0; + return -1; +} + +AVCodec mlp_decoder = { + "mlp", + CODEC_TYPE_AUDIO, + CODEC_ID_MLP, + sizeof(MLPDecodeContext), + mlp_decode_init, + NULL, + NULL, + read_access_unit, + .long_name = NULL_IF_CONFIG_SMALL("Meridian Lossless Packing"), +}; + diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/mmvideo.c b/src/add-ons/media/plugins/avcodec/libavcodec/mmvideo.c new file mode 100644 index 0000000000..03d86ee9dc --- /dev/null +++ b/src/add-ons/media/plugins/avcodec/libavcodec/mmvideo.c @@ -0,0 +1,212 @@ +/* + * American Laser Games MM Video Decoder + * Copyright (c) 2006,2008 Peter Ross + * + * This file is part of FFmpeg. + * + * FFmpeg 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.1 of the License, or (at your option) any later version. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, 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 +#define MM_TYPE_PALETTE 0x31 + +typedef struct MmContext { + AVCodecContext *avctx; + AVFrame frame; + int palette[AVPALETTE_COUNT]; +} MmContext; + +static av_cold int mm_decode_init(AVCodecContext *avctx) +{ + MmContext *s = avctx->priv_data; + + s->avctx = avctx; + + avctx->pix_fmt = PIX_FMT_PAL8; + + 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_pal(MmContext *s, const uint8_t *buf, const uint8_t *buf_end) +{ + int i; + buf += 4; + for (i=0; i<128 && buf+2palette[i] = AV_RB24(buf); + s->palette[i+128] = s->palette[i]<<2; + buf += 3; + } +} + +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(iframe.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 + AV_RL16(&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> (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, + const uint8_t *buf, int buf_size) +{ + MmContext *s = avctx->priv_data; + const uint8_t *buf_end = buf+buf_size; + int type; + + type = AV_RL16(&buf[0]); + buf += MM_PREAMBLE_SIZE; + buf_size -= MM_PREAMBLE_SIZE; + + switch(type) { + case MM_TYPE_PALETTE : mm_decode_pal(s, buf, buf_end); return buf_size; + 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; + } + + memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE); + + *data_size = sizeof(AVFrame); + *(AVFrame*)data = s->frame; + + return buf_size; +} + +static av_cold 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, + .long_name = NULL_IF_CONFIG_SMALL("American Laser Games MM Video"), +}; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/motion-test.c b/src/add-ons/media/plugins/avcodec/libavcodec/motion-test.c new file mode 100644 index 0000000000..69791500b0 --- /dev/null +++ b/src/add-ons/media/plugins/avcodec/libavcodec/motion-test.c @@ -0,0 +1,164 @@ +/* + * (c) 2001 Fabrice Bellard + * + * This file is part of FFmpeg. + * + * FFmpeg 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.1 of the License, or (at your option) any later version. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file motion_test.c + * motion test. + */ + +#include +#include +#include +#include +#include + +#include "dsputil.h" + +#include "i386/mmx.h" + +#undef exit +#undef printf +#undef random + +#define WIDTH 64 +#define HEIGHT 64 + +uint8_t img1[WIDTH * HEIGHT]; +uint8_t img2[WIDTH * HEIGHT]; + +void fill_random(uint8_t *tab, int size) +{ + int i; + for(i=0;idsp_mask = FF_MM_FORCE; + dsputil_init(&cctx, ctx); + for (c = 0; c < 2; c++) { + int x; + ctx->dsp_mask = FF_MM_FORCE | flags[c]; + dsputil_init(&mmxctx, ctx); + + for (x = 0; x < 2; x++) { + printf("%s for %dx%d pixels\n", c ? "mmx2" : "mmx", + x ? 8 : 16, x ? 8 : 16); + test_motion("mmx", mmxctx.pix_abs[x][0], cctx.pix_abs[x][0]); + test_motion("mmx_x2", mmxctx.pix_abs[x][1], cctx.pix_abs[x][1]); + test_motion("mmx_y2", mmxctx.pix_abs[x][2], cctx.pix_abs[x][2]); + test_motion("mmx_xy2", mmxctx.pix_abs[x][3], cctx.pix_abs[x][3]); + } + } + av_free(ctx); + + return 0; +} diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/motion_est.c b/src/add-ons/media/plugins/avcodec/libavcodec/motion_est.c index e8641790b2..71fe90f420 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/motion_est.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/motion_est.c @@ -1,31 +1,32 @@ /* - * Motion estimation + * Motion estimation * Copyright (c) 2000,2001 Fabrice Bellard. * Copyright (c) 2002-2004 Michael Niedermayer - * * - * This library is free software; you can redistribute it and/or + * new motion estimation (X1/EPZS) by Michael Niedermayer + * + * This file is part of FFmpeg. + * + * FFmpeg 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. + * version 2.1 of the License, or (at your option) any later version. * - * This library is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * new Motion Estimation (X1/EPZS) by Michael Niedermayer + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ - + /** * @file motion_est.c * Motion estimation. */ - + #include #include #include @@ -33,8 +34,8 @@ #include "dsputil.h" #include "mpegvideo.h" -//#undef NDEBUG -//#include +#undef NDEBUG +#include #define SQ(a) ((a)*(a)) @@ -45,19 +46,18 @@ #define P_MV1 P[9] static inline int sad_hpel_motion_search(MpegEncContext * s, - int *mx_ptr, int *my_ptr, int dmin, - int pred_x, int pred_y, uint8_t *src_data[3], - uint8_t *ref_data[6], int stride, int uvstride, - int size, int h, uint8_t * const mv_penalty); + int *mx_ptr, int *my_ptr, int dmin, + int src_index, int ref_index, + int size, int h); -static inline int update_map_generation(MpegEncContext * s) +static inline int update_map_generation(MotionEstContext *c) { - s->me.map_generation+= 1<<(ME_MAP_MV_BITS*2); - if(s->me.map_generation==0){ - s->me.map_generation= 1<<(ME_MAP_MV_BITS*2); - memset(s->me.map, 0, sizeof(uint32_t)*ME_MAP_SIZE); + c->map_generation+= 1<<(ME_MAP_MV_BITS*2); + if(c->map_generation==0){ + c->map_generation= 1<<(ME_MAP_MV_BITS*2); + memset(c->map, 0, sizeof(uint32_t)*ME_MAP_SIZE); } - return s->me.map_generation; + return c->map_generation; } /* shape adaptive search stuff */ @@ -70,283 +70,243 @@ typedef struct Minima{ static int minima_cmp(const void *a, const void *b){ const Minima *da = (const Minima *) a; const Minima *db = (const Minima *) b; - + return da->height - db->height; } - -/* SIMPLE */ -#define RENAME(a) simple_ ## a -#define CMP(d, x, y, size)\ -d = cmp(s, src_y, (ref_y) + (x) + (y)*(stride), stride, h); +#define FLAG_QPEL 1 //must be 1 +#define FLAG_CHROMA 2 +#define FLAG_DIRECT 4 -#define CMP_HPEL(d, dx, dy, x, y, size)\ -{\ - const int dxy= (dx) + 2*(dy);\ - hpel_put[0][dxy](s->me.scratchpad, (ref_y) + (x) + (y)*(stride), stride, h);\ - d = cmp_sub(s, s->me.scratchpad, src_y, stride, h);\ -} - - -#define CMP_QPEL(d, dx, dy, x, y, size)\ -{\ - const int dxy= (dx) + 4*(dy);\ - qpel_put[0][dxy](s->me.scratchpad, (ref_y) + (x) + (y)*(stride), stride);\ - d = cmp_sub(s, s->me.scratchpad, src_y, stride, h);\ -} - -#include "motion_est_template.c" -#undef RENAME -#undef CMP -#undef CMP_HPEL -#undef CMP_QPEL -#undef INIT - -/* SIMPLE CHROMA */ -#define RENAME(a) simple_chroma_ ## a - -#define CMP(d, x, y, size)\ -d = cmp(s, src_y, (ref_y) + (x) + (y)*(stride), stride, h);\ -if(chroma_cmp){\ - int dxy= ((x)&1) + 2*((y)&1);\ - int c= ((x)>>1) + ((y)>>1)*uvstride;\ -\ - chroma_hpel_put[0][dxy](s->me.scratchpad, ref_u + c, uvstride, h>>1);\ - d += chroma_cmp(s, s->me.scratchpad, src_u, uvstride, h>>1);\ - chroma_hpel_put[0][dxy](s->me.scratchpad, ref_v + c, uvstride, h>>1);\ - d += chroma_cmp(s, s->me.scratchpad, src_v, uvstride, h>>1);\ -} - -#define CMP_HPEL(d, dx, dy, x, y, size)\ -{\ - const int dxy= (dx) + 2*(dy);\ - hpel_put[0][dxy](s->me.scratchpad, (ref_y) + (x) + (y)*(stride), stride, h);\ - d = cmp_sub(s, s->me.scratchpad, src_y, stride, h);\ - if(chroma_cmp_sub){\ - int cxy= (dxy) | ((x)&1) | (2*((y)&1));\ - int c= ((x)>>1) + ((y)>>1)*uvstride;\ - chroma_hpel_put[0][cxy](s->me.scratchpad, ref_u + c, uvstride, h>>1);\ - d += chroma_cmp_sub(s, s->me.scratchpad, src_u, uvstride, h>>1);\ - chroma_hpel_put[0][cxy](s->me.scratchpad, ref_v + c, uvstride, h>>1);\ - d += chroma_cmp_sub(s, s->me.scratchpad, src_v, uvstride, h>>1);\ - }\ -} - -#define CMP_QPEL(d, dx, dy, x, y, size)\ -{\ - const int dxy= (dx) + 4*(dy);\ - qpel_put[0][dxy](s->me.scratchpad, (ref_y) + (x) + (y)*(stride), stride);\ - d = cmp_sub(s, s->me.scratchpad, src_y, stride, h);\ - if(chroma_cmp_sub){\ - int cxy, c;\ - int cx= (4*(x) + (dx))/2;\ - int cy= (4*(y) + (dy))/2;\ - cx= (cx>>1)|(cx&1);\ - cy= (cy>>1)|(cy&1);\ - cxy= (cx&1) + 2*(cy&1);\ - c= ((cx)>>1) + ((cy)>>1)*uvstride;\ - chroma_hpel_put[0][cxy](s->me.scratchpad, ref_u + c, uvstride, h>>1);\ - d += chroma_cmp_sub(s, s->me.scratchpad, src_u, uvstride, h>>1);\ - chroma_hpel_put[0][cxy](s->me.scratchpad, ref_v + c, uvstride, h>>1);\ - d += chroma_cmp_sub(s, s->me.scratchpad, src_v, uvstride, h>>1);\ - }\ -} - -#include "motion_est_template.c" -#undef RENAME -#undef CMP -#undef CMP_HPEL -#undef CMP_QPEL -#undef INIT - -/* SIMPLE DIRECT HPEL */ -#define RENAME(a) simple_direct_hpel_ ## a -//FIXME precalc divisions stuff - -#define CMP_DIRECT(d, dx, dy, x, y, size, cmp_func)\ -if((x) >= xmin && 2*(x) + (dx) <= 2*xmax && (y) >= ymin && 2*(y) + (dy) <= 2*ymax){\ - const int hx= 2*(x) + (dx);\ - const int hy= 2*(y) + (dy);\ - if(s->mv_type==MV_TYPE_8X8){\ - int i;\ - for(i=0; i<4; i++){\ - int fx = s->me.direct_basis_mv[i][0] + hx;\ - int fy = s->me.direct_basis_mv[i][1] + hy;\ - int bx = hx ? fx - s->me.co_located_mv[i][0] : s->me.co_located_mv[i][0]*(time_pb - time_pp)/time_pp + (i &1)*16;\ - int by = hy ? fy - s->me.co_located_mv[i][1] : s->me.co_located_mv[i][1]*(time_pb - time_pp)/time_pp + (i>>1)*16;\ - int fxy= (fx&1) + 2*(fy&1);\ - int bxy= (bx&1) + 2*(by&1);\ -\ - uint8_t *dst= s->me.scratchpad + 8*(i&1) + 8*stride*(i>>1);\ - hpel_put[1][fxy](dst, (ref_y ) + (fx>>1) + (fy>>1)*(stride), stride, 8);\ - hpel_avg[1][bxy](dst, (ref_data[3]) + (bx>>1) + (by>>1)*(stride), stride, 8);\ - }\ - }else{\ - int fx = s->me.direct_basis_mv[0][0] + hx;\ - int fy = s->me.direct_basis_mv[0][1] + hy;\ - int bx = hx ? fx - s->me.co_located_mv[0][0] : (s->me.co_located_mv[0][0]*(time_pb - time_pp)/time_pp);\ - int by = hy ? fy - s->me.co_located_mv[0][1] : (s->me.co_located_mv[0][1]*(time_pb - time_pp)/time_pp);\ - int fxy= (fx&1) + 2*(fy&1);\ - int bxy= (bx&1) + 2*(by&1);\ - \ - assert((fx>>1) + 16*s->mb_x >= -16);\ - assert((fy>>1) + 16*s->mb_y >= -16);\ - assert((fx>>1) + 16*s->mb_x <= s->width);\ - assert((fy>>1) + 16*s->mb_y <= s->height);\ - assert((bx>>1) + 16*s->mb_x >= -16);\ - assert((by>>1) + 16*s->mb_y >= -16);\ - assert((bx>>1) + 16*s->mb_x <= s->width);\ - assert((by>>1) + 16*s->mb_y <= s->height);\ -\ - hpel_put[0][fxy](s->me.scratchpad, (ref_y ) + (fx>>1) + (fy>>1)*(stride), stride, 16);\ - hpel_avg[0][bxy](s->me.scratchpad, (ref_data[3]) + (bx>>1) + (by>>1)*(stride), stride, 16);\ - }\ - d = cmp_func(s, s->me.scratchpad, src_y, stride, 16);\ -}else\ - d= 256*256*256*32; - - -#define CMP_HPEL(d, dx, dy, x, y, size)\ - CMP_DIRECT(d, dx, dy, x, y, size, cmp_sub) - -#define CMP(d, x, y, size)\ - CMP_DIRECT(d, 0, 0, x, y, size, cmp) - -#include "motion_est_template.c" -#undef RENAME -#undef CMP -#undef CMP_HPEL -#undef CMP_QPEL -#undef INIT -#undef CMP_DIRECT - -/* SIMPLE DIRECT QPEL */ -#define RENAME(a) simple_direct_qpel_ ## a - -#define CMP_DIRECT(d, dx, dy, x, y, size, cmp_func)\ -if((x) >= xmin && 4*(x) + (dx) <= 4*xmax && (y) >= ymin && 4*(y) + (dy) <= 4*ymax){\ - const int qx= 4*(x) + (dx);\ - const int qy= 4*(y) + (dy);\ - if(s->mv_type==MV_TYPE_8X8){\ - int i;\ - for(i=0; i<4; i++){\ - int fx = s->me.direct_basis_mv[i][0] + qx;\ - int fy = s->me.direct_basis_mv[i][1] + qy;\ - int bx = qx ? fx - s->me.co_located_mv[i][0] : s->me.co_located_mv[i][0]*(time_pb - time_pp)/time_pp + (i &1)*16;\ - int by = qy ? fy - s->me.co_located_mv[i][1] : s->me.co_located_mv[i][1]*(time_pb - time_pp)/time_pp + (i>>1)*16;\ - int fxy= (fx&3) + 4*(fy&3);\ - int bxy= (bx&3) + 4*(by&3);\ -\ - uint8_t *dst= s->me.scratchpad + 8*(i&1) + 8*stride*(i>>1);\ - qpel_put[1][fxy](dst, (ref_y ) + (fx>>2) + (fy>>2)*(stride), stride);\ - qpel_avg[1][bxy](dst, (ref_data[3]) + (bx>>2) + (by>>2)*(stride), stride);\ - }\ - }else{\ - int fx = s->me.direct_basis_mv[0][0] + qx;\ - int fy = s->me.direct_basis_mv[0][1] + qy;\ - int bx = qx ? fx - s->me.co_located_mv[0][0] : s->me.co_located_mv[0][0]*(time_pb - time_pp)/time_pp;\ - int by = qy ? fy - s->me.co_located_mv[0][1] : s->me.co_located_mv[0][1]*(time_pb - time_pp)/time_pp;\ - int fxy= (fx&3) + 4*(fy&3);\ - int bxy= (bx&3) + 4*(by&3);\ -\ - qpel_put[1][fxy](s->me.scratchpad , (ref_y ) + (fx>>2) + (fy>>2)*(stride) , stride);\ - qpel_put[1][fxy](s->me.scratchpad + 8 , (ref_y ) + (fx>>2) + (fy>>2)*(stride) + 8 , stride);\ - qpel_put[1][fxy](s->me.scratchpad + 8*stride, (ref_y ) + (fx>>2) + (fy>>2)*(stride) + 8*stride, stride);\ - qpel_put[1][fxy](s->me.scratchpad + 8 + 8*stride, (ref_y ) + (fx>>2) + (fy>>2)*(stride) + 8 + 8*stride, stride);\ - qpel_avg[1][bxy](s->me.scratchpad , (ref_data[3]) + (bx>>2) + (by>>2)*(stride) , stride);\ - qpel_avg[1][bxy](s->me.scratchpad + 8 , (ref_data[3]) + (bx>>2) + (by>>2)*(stride) + 8 , stride);\ - qpel_avg[1][bxy](s->me.scratchpad + 8*stride, (ref_data[3]) + (bx>>2) + (by>>2)*(stride) + 8*stride, stride);\ - qpel_avg[1][bxy](s->me.scratchpad + 8 + 8*stride, (ref_data[3]) + (bx>>2) + (by>>2)*(stride) + 8 + 8*stride, stride);\ - }\ - d = cmp_func(s, s->me.scratchpad, src_y, stride, 16);\ -}else\ - d= 256*256*256*32; - - -#define CMP_QPEL(d, dx, dy, x, y, size)\ - CMP_DIRECT(d, dx, dy, x, y, size, cmp_sub) - -#define CMP(d, x, y, size)\ - CMP_DIRECT(d, 0, 0, x, y, size, cmp) - -#include "motion_est_template.c" -#undef RENAME -#undef CMP -#undef CMP_HPEL -#undef CMP_QPEL -#undef INIT -#undef CMP__DIRECT - -static inline int get_penalty_factor(MpegEncContext *s, int type){ - switch(type&0xFF){ - default: - case FF_CMP_SAD: - return s->qscale*2; - case FF_CMP_DCT: - return s->qscale*3; - case FF_CMP_SATD: - return s->qscale*6; - case FF_CMP_SSE: - return s->qscale*s->qscale*2; - case FF_CMP_BIT: - return 1; - case FF_CMP_RD: - case FF_CMP_PSNR: - return (s->qscale*s->qscale*185 + 64)>>7; +static inline void init_ref(MotionEstContext *c, uint8_t *src[3], uint8_t *ref[3], uint8_t *ref2[3], int x, int y, int ref_index){ + const int offset[3]= { + y*c-> stride + x, + ((y*c->uvstride + x)>>1), + ((y*c->uvstride + x)>>1), + }; + int i; + for(i=0; i<3; i++){ + c->src[0][i]= src [i] + offset[i]; + c->ref[0][i]= ref [i] + offset[i]; + } + if(ref_index){ + for(i=0; i<3; i++){ + c->ref[ref_index][i]= ref2[i] + offset[i]; + } } } -void ff_init_me(MpegEncContext *s){ - ff_set_cmp(&s->dsp, s->dsp.me_pre_cmp, s->avctx->me_pre_cmp); - ff_set_cmp(&s->dsp, s->dsp.me_cmp, s->avctx->me_cmp); - ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, s->avctx->me_sub_cmp); - ff_set_cmp(&s->dsp, s->dsp.mb_cmp, s->avctx->mb_cmp); +static int get_flags(MotionEstContext *c, int direct, int chroma){ + return ((c->avctx->flags&CODEC_FLAG_QPEL) ? FLAG_QPEL : 0) + + (direct ? FLAG_DIRECT : 0) + + (chroma ? FLAG_CHROMA : 0); +} +/*! \brief compares a block (either a full macroblock or a partition thereof) + against a proposed motion-compensated prediction of that block + */ +static av_always_inline int cmp(MpegEncContext *s, const int x, const int y, const int subx, const int suby, + const int size, const int h, int ref_index, int src_index, + me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){ + MotionEstContext * const c= &s->me; + const int stride= c->stride; + const int uvstride= c->uvstride; + const int qpel= flags&FLAG_QPEL; + const int chroma= flags&FLAG_CHROMA; + const int dxy= subx + (suby<<(1+qpel)); //FIXME log2_subpel? + const int hx= subx + (x<<(1+qpel)); + const int hy= suby + (y<<(1+qpel)); + uint8_t * const * const ref= c->ref[ref_index]; + uint8_t * const * const src= c->src[src_index]; + int d; + //FIXME check chroma 4mv, (no crashes ...) + if(flags&FLAG_DIRECT){ + assert(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1)); + if(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1)){ + const int time_pp= s->pp_time; + const int time_pb= s->pb_time; + const int mask= 2*qpel+1; + if(s->mv_type==MV_TYPE_8X8){ + int i; + for(i=0; i<4; i++){ + int fx = c->direct_basis_mv[i][0] + hx; + int fy = c->direct_basis_mv[i][1] + hy; + int bx = hx ? fx - c->co_located_mv[i][0] : c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(qpel+4)); + int by = hy ? fy - c->co_located_mv[i][1] : c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(qpel+4)); + int fxy= (fx&mask) + ((fy&mask)<<(qpel+1)); + int bxy= (bx&mask) + ((by&mask)<<(qpel+1)); + + uint8_t *dst= c->temp + 8*(i&1) + 8*stride*(i>>1); + if(qpel){ + c->qpel_put[1][fxy](dst, ref[0] + (fx>>2) + (fy>>2)*stride, stride); + c->qpel_avg[1][bxy](dst, ref[8] + (bx>>2) + (by>>2)*stride, stride); + }else{ + c->hpel_put[1][fxy](dst, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 8); + c->hpel_avg[1][bxy](dst, ref[8] + (bx>>1) + (by>>1)*stride, stride, 8); + } + } + }else{ + int fx = c->direct_basis_mv[0][0] + hx; + int fy = c->direct_basis_mv[0][1] + hy; + int bx = hx ? fx - c->co_located_mv[0][0] : (c->co_located_mv[0][0]*(time_pb - time_pp)/time_pp); + int by = hy ? fy - c->co_located_mv[0][1] : (c->co_located_mv[0][1]*(time_pb - time_pp)/time_pp); + int fxy= (fx&mask) + ((fy&mask)<<(qpel+1)); + int bxy= (bx&mask) + ((by&mask)<<(qpel+1)); + + if(qpel){ + c->qpel_put[1][fxy](c->temp , ref[0] + (fx>>2) + (fy>>2)*stride , stride); + c->qpel_put[1][fxy](c->temp + 8 , ref[0] + (fx>>2) + (fy>>2)*stride + 8 , stride); + c->qpel_put[1][fxy](c->temp + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8*stride, stride); + c->qpel_put[1][fxy](c->temp + 8 + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8 + 8*stride, stride); + c->qpel_avg[1][bxy](c->temp , ref[8] + (bx>>2) + (by>>2)*stride , stride); + c->qpel_avg[1][bxy](c->temp + 8 , ref[8] + (bx>>2) + (by>>2)*stride + 8 , stride); + c->qpel_avg[1][bxy](c->temp + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8*stride, stride); + c->qpel_avg[1][bxy](c->temp + 8 + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8 + 8*stride, stride); + }else{ + assert((fx>>1) + 16*s->mb_x >= -16); + assert((fy>>1) + 16*s->mb_y >= -16); + assert((fx>>1) + 16*s->mb_x <= s->width); + assert((fy>>1) + 16*s->mb_y <= s->height); + assert((bx>>1) + 16*s->mb_x >= -16); + assert((by>>1) + 16*s->mb_y >= -16); + assert((bx>>1) + 16*s->mb_x <= s->width); + assert((by>>1) + 16*s->mb_y <= s->height); + + c->hpel_put[0][fxy](c->temp, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 16); + c->hpel_avg[0][bxy](c->temp, ref[8] + (bx>>1) + (by>>1)*stride, stride, 16); + } + } + d = cmp_func(s, c->temp, src[0], stride, 16); + }else + d= 256*256*256*32; + }else{ + int uvdxy; /* no, it might not be used uninitialized */ + if(dxy){ + if(qpel){ + c->qpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride); //FIXME prototype (add h) + if(chroma){ + int cx= hx/2; + int cy= hy/2; + cx= (cx>>1)|(cx&1); + cy= (cy>>1)|(cy&1); + uvdxy= (cx&1) + 2*(cy&1); + //FIXME x/y wrong, but mpeg4 qpel is sick anyway, we should drop as much of it as possible in favor for h264 + } + }else{ + c->hpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride, h); + if(chroma) + uvdxy= dxy | (x&1) | (2*(y&1)); + } + d = cmp_func(s, c->temp, src[0], stride, h); + }else{ + d = cmp_func(s, src[0], ref[0] + x + y*stride, stride, h); + if(chroma) + uvdxy= (x&1) + 2*(y&1); + } + if(chroma){ + uint8_t * const uvtemp= c->temp + 16*stride; + c->hpel_put[size+1][uvdxy](uvtemp , ref[1] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1); + c->hpel_put[size+1][uvdxy](uvtemp+8, ref[2] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1); + d += chroma_cmp_func(s, uvtemp , src[1], uvstride, h>>1); + d += chroma_cmp_func(s, uvtemp+8, src[2], uvstride, h>>1); + } + } +#if 0 + if(full_pel){ + const int index= (((y)<mv_penalty[hx - c->pred_x] + c->mv_penalty[hy - c->pred_y])*c->penalty_factor; +#endif + return d; +} + +#include "motion_est_template.c" + +static int zero_cmp(void *s, uint8_t *a, uint8_t *b, int stride, int h){ + return 0; +} + +static void zero_hpel(uint8_t *a, const uint8_t *b, int stride, int h){ +} + +int ff_init_me(MpegEncContext *s){ + MotionEstContext * const c= &s->me; + int cache_size= FFMIN(ME_MAP_SIZE>>ME_MAP_SHIFT, 1<avctx->dia_size)&255, FFABS(s->avctx->pre_dia_size)&255); + + if(FFMIN(s->avctx->dia_size, s->avctx->pre_dia_size) < -ME_MAP_SIZE){ + av_log(s->avctx, AV_LOG_ERROR, "ME_MAP size is too small for SAB diamond\n"); + return -1; + } + + c->avctx= s->avctx; + + if(cache_size < 2*dia_size && !c->stride){ + av_log(s->avctx, AV_LOG_INFO, "ME_MAP size may be a little small for the selected diamond size\n"); + } + + ff_set_cmp(&s->dsp, s->dsp.me_pre_cmp, c->avctx->me_pre_cmp); + ff_set_cmp(&s->dsp, s->dsp.me_cmp, c->avctx->me_cmp); + ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, c->avctx->me_sub_cmp); + ff_set_cmp(&s->dsp, s->dsp.mb_cmp, c->avctx->mb_cmp); + + c->flags = get_flags(c, 0, c->avctx->me_cmp &FF_CMP_CHROMA); + c->sub_flags= get_flags(c, 0, c->avctx->me_sub_cmp&FF_CMP_CHROMA); + c->mb_flags = get_flags(c, 0, c->avctx->mb_cmp &FF_CMP_CHROMA); + +/*FIXME s->no_rounding b_type*/ if(s->flags&CODEC_FLAG_QPEL){ - if(s->avctx->me_sub_cmp&FF_CMP_CHROMA) - s->me.sub_motion_search= simple_chroma_qpel_motion_search; - else - s->me.sub_motion_search= simple_qpel_motion_search; + c->sub_motion_search= qpel_motion_search; + c->qpel_avg= s->dsp.avg_qpel_pixels_tab; + if(s->no_rounding) c->qpel_put= s->dsp.put_no_rnd_qpel_pixels_tab; + else c->qpel_put= s->dsp.put_qpel_pixels_tab; }else{ - if(s->avctx->me_sub_cmp&FF_CMP_CHROMA) - s->me.sub_motion_search= simple_chroma_hpel_motion_search; - else if( s->avctx->me_sub_cmp == FF_CMP_SAD - && s->avctx-> me_cmp == FF_CMP_SAD - && s->avctx-> mb_cmp == FF_CMP_SAD) - s->me.sub_motion_search= sad_hpel_motion_search; // 2050 vs. 2450 cycles + if(c->avctx->me_sub_cmp&FF_CMP_CHROMA) + c->sub_motion_search= hpel_motion_search; + else if( c->avctx->me_sub_cmp == FF_CMP_SAD + && c->avctx-> me_cmp == FF_CMP_SAD + && c->avctx-> mb_cmp == FF_CMP_SAD) + c->sub_motion_search= sad_hpel_motion_search; // 2050 vs. 2450 cycles else - s->me.sub_motion_search= simple_hpel_motion_search; + c->sub_motion_search= hpel_motion_search; + } + c->hpel_avg= s->dsp.avg_pixels_tab; + if(s->no_rounding) c->hpel_put= s->dsp.put_no_rnd_pixels_tab; + else c->hpel_put= s->dsp.put_pixels_tab; + + if(s->linesize){ + c->stride = s->linesize; + c->uvstride= s->uvlinesize; + }else{ + c->stride = 16*s->mb_width + 32; + c->uvstride= 8*s->mb_width + 16; } - if(s->avctx->me_cmp&FF_CMP_CHROMA){ - s->me.motion_search[0]= simple_chroma_epzs_motion_search; - s->me.motion_search[1]= simple_chroma_epzs_motion_search4; - s->me.motion_search[4]= simple_chroma_epzs_motion_search2; - }else{ - s->me.motion_search[0]= simple_epzs_motion_search; - s->me.motion_search[1]= simple_epzs_motion_search4; - s->me.motion_search[4]= simple_epzs_motion_search2; + /* 8x8 fullpel search would need a 4x4 chroma compare, which we do + * not have yet, and even if we had, the motion estimation code + * does not expect it. */ + if(s->codec_id != CODEC_ID_SNOW){ + if((c->avctx->me_cmp&FF_CMP_CHROMA)/* && !s->dsp.me_cmp[2]*/){ + s->dsp.me_cmp[2]= zero_cmp; + } + if((c->avctx->me_sub_cmp&FF_CMP_CHROMA) && !s->dsp.me_sub_cmp[2]){ + s->dsp.me_sub_cmp[2]= zero_cmp; + } + c->hpel_put[2][0]= c->hpel_put[2][1]= + c->hpel_put[2][2]= c->hpel_put[2][3]= zero_hpel; } - - if(s->avctx->me_pre_cmp&FF_CMP_CHROMA){ - s->me.pre_motion_search= simple_chroma_epzs_motion_search; - }else{ - s->me.pre_motion_search= simple_epzs_motion_search; - } - - if(s->flags&CODEC_FLAG_QPEL){ - if(s->avctx->mb_cmp&FF_CMP_CHROMA) - s->me.get_mb_score= simple_chroma_qpel_get_mb_score; - else - s->me.get_mb_score= simple_qpel_get_mb_score; - }else{ - if(s->avctx->mb_cmp&FF_CMP_CHROMA) - s->me.get_mb_score= simple_chroma_hpel_get_mb_score; - else - s->me.get_mb_score= simple_hpel_get_mb_score; + + if(s->codec_id == CODEC_ID_H261){ + c->sub_motion_search= no_sub_motion_search; } + + c->temp= c->scratchpad; + + return 0; } - + #if 0 static int pix_dev(uint8_t * pix, int line_size, int mean) { @@ -354,252 +314,30 @@ static int pix_dev(uint8_t * pix, int line_size, int mean) s = 0; for (i = 0; i < 16; i++) { - for (j = 0; j < 16; j += 8) { - s += ABS(pix[0]-mean); - s += ABS(pix[1]-mean); - s += ABS(pix[2]-mean); - s += ABS(pix[3]-mean); - s += ABS(pix[4]-mean); - s += ABS(pix[5]-mean); - s += ABS(pix[6]-mean); - s += ABS(pix[7]-mean); - pix += 8; - } - pix += line_size - 16; + for (j = 0; j < 16; j += 8) { + s += FFABS(pix[0]-mean); + s += FFABS(pix[1]-mean); + s += FFABS(pix[2]-mean); + s += FFABS(pix[3]-mean); + s += FFABS(pix[4]-mean); + s += FFABS(pix[5]-mean); + s += FFABS(pix[6]-mean); + s += FFABS(pix[7]-mean); + pix += 8; + } + pix += line_size - 16; } return s; } #endif static inline void no_motion_search(MpegEncContext * s, - int *mx_ptr, int *my_ptr) + int *mx_ptr, int *my_ptr) { *mx_ptr = 16 * s->mb_x; *my_ptr = 16 * s->mb_y; } -static int full_motion_search(MpegEncContext * s, - int *mx_ptr, int *my_ptr, int range, - int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture) -{ - int x1, y1, x2, y2, xx, yy, x, y; - int mx, my, dmin, d; - uint8_t *pix; - - xx = 16 * s->mb_x; - yy = 16 * s->mb_y; - x1 = xx - range + 1; /* we loose one pixel to avoid boundary pb with half pixel pred */ - if (x1 < xmin) - x1 = xmin; - x2 = xx + range - 1; - if (x2 > xmax) - x2 = xmax; - y1 = yy - range + 1; - if (y1 < ymin) - y1 = ymin; - y2 = yy + range - 1; - if (y2 > ymax) - y2 = ymax; - pix = s->new_picture.data[0] + (yy * s->linesize) + xx; - dmin = 0x7fffffff; - mx = 0; - my = 0; - for (y = y1; y <= y2; y++) { - for (x = x1; x <= x2; x++) { - d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x, - s->linesize, 16); - if (d < dmin || - (d == dmin && - (abs(x - xx) + abs(y - yy)) < - (abs(mx - xx) + abs(my - yy)))) { - dmin = d; - mx = x; - my = y; - } - } - } - - *mx_ptr = mx; - *my_ptr = my; - -#if 0 - if (*mx_ptr < -(2 * range) || *mx_ptr >= (2 * range) || - *my_ptr < -(2 * range) || *my_ptr >= (2 * range)) { - fprintf(stderr, "error %d %d\n", *mx_ptr, *my_ptr); - } -#endif - return dmin; -} - - -static int log_motion_search(MpegEncContext * s, - int *mx_ptr, int *my_ptr, int range, - int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture) -{ - int x1, y1, x2, y2, xx, yy, x, y; - int mx, my, dmin, d; - uint8_t *pix; - - xx = s->mb_x << 4; - yy = s->mb_y << 4; - - /* Left limit */ - x1 = xx - range; - if (x1 < xmin) - x1 = xmin; - - /* Right limit */ - x2 = xx + range; - if (x2 > xmax) - x2 = xmax; - - /* Upper limit */ - y1 = yy - range; - if (y1 < ymin) - y1 = ymin; - - /* Lower limit */ - y2 = yy + range; - if (y2 > ymax) - y2 = ymax; - - pix = s->new_picture.data[0] + (yy * s->linesize) + xx; - dmin = 0x7fffffff; - mx = 0; - my = 0; - - do { - for (y = y1; y <= y2; y += range) { - for (x = x1; x <= x2; x += range) { - d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x, s->linesize, 16); - if (d < dmin || (d == dmin && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) { - dmin = d; - mx = x; - my = y; - } - } - } - - range = range >> 1; - - x1 = mx - range; - if (x1 < xmin) - x1 = xmin; - - x2 = mx + range; - if (x2 > xmax) - x2 = xmax; - - y1 = my - range; - if (y1 < ymin) - y1 = ymin; - - y2 = my + range; - if (y2 > ymax) - y2 = ymax; - - } while (range >= 1); - -#ifdef DEBUG - fprintf(stderr, "log - MX: %d\tMY: %d\n", mx, my); -#endif - *mx_ptr = mx; - *my_ptr = my; - return dmin; -} - -static int phods_motion_search(MpegEncContext * s, - int *mx_ptr, int *my_ptr, int range, - int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture) -{ - int x1, y1, x2, y2, xx, yy, x, y, lastx, d; - int mx, my, dminx, dminy; - uint8_t *pix; - - xx = s->mb_x << 4; - yy = s->mb_y << 4; - - /* Left limit */ - x1 = xx - range; - if (x1 < xmin) - x1 = xmin; - - /* Right limit */ - x2 = xx + range; - if (x2 > xmax) - x2 = xmax; - - /* Upper limit */ - y1 = yy - range; - if (y1 < ymin) - y1 = ymin; - - /* Lower limit */ - y2 = yy + range; - if (y2 > ymax) - y2 = ymax; - - pix = s->new_picture.data[0] + (yy * s->linesize) + xx; - mx = 0; - my = 0; - - x = xx; - y = yy; - do { - dminx = 0x7fffffff; - dminy = 0x7fffffff; - - lastx = x; - for (x = x1; x <= x2; x += range) { - d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x, s->linesize, 16); - if (d < dminx || (d == dminx && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) { - dminx = d; - mx = x; - } - } - - x = lastx; - for (y = y1; y <= y2; y += range) { - d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x, s->linesize, 16); - if (d < dminy || (d == dminy && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) { - dminy = d; - my = y; - } - } - - range = range >> 1; - - x = mx; - y = my; - x1 = mx - range; - if (x1 < xmin) - x1 = xmin; - - x2 = mx + range; - if (x2 > xmax) - x2 = xmax; - - y1 = my - range; - if (y1 < ymin) - y1 = ymin; - - y2 = my + range; - if (y2 > ymax) - y2 = ymax; - - } while (range >= 1); - -#ifdef DEBUG - fprintf(stderr, "phods - MX: %d\tMY: %d\n", mx, my); -#endif - - /* half pixel search */ - *mx_ptr = mx; - *my_ptr = my; - return dminy; -} - - #define Z_THRESHOLD 256 #define CHECK_SAD_HALF_MV(suffix, x, y) \ @@ -610,40 +348,40 @@ static int phods_motion_search(MpegEncContext * s, } static inline int sad_hpel_motion_search(MpegEncContext * s, - int *mx_ptr, int *my_ptr, int dmin, - int pred_x, int pred_y, uint8_t *src_data[3], - uint8_t *ref_data[6], int stride, int uvstride, - int size, int h, uint8_t * const mv_penalty) + int *mx_ptr, int *my_ptr, int dmin, + int src_index, int ref_index, + int size, int h) { - uint32_t *score_map= s->me.score_map; - const int penalty_factor= s->me.sub_penalty_factor; + MotionEstContext * const c= &s->me; + const int penalty_factor= c->sub_penalty_factor; int mx, my, dminh; uint8_t *pix, *ptr; - const int xmin= s->me.xmin; - const int ymin= s->me.ymin; - const int xmax= s->me.xmax; - const int ymax= s->me.ymax; + int stride= c->stride; + const int flags= c->sub_flags; + LOAD_COMMON - if(s->me.skip){ + assert(flags == 0); + + if(c->skip){ // printf("S"); *mx_ptr = 0; *my_ptr = 0; return dmin; } // printf("N"); - - pix = src_data[0]; + + pix = c->src[src_index][0]; mx = *mx_ptr; my = *my_ptr; - ptr = ref_data[0] + (my * stride) + mx; - + ptr = c->ref[ref_index][0] + (my * stride) + mx; + dminh = dmin; - if (mx > xmin && mx < xmax && + if (mx > xmin && mx < xmax && my > ymin && my < ymax) { int dx=0, dy=0; - int d, pen_x, pen_y; + int d, pen_x, pen_y; const int index= (my<mb_x + s->mb_y*s->mb_stride; - + s->p_mv_table[xy][0] = mx; s->p_mv_table[xy][1] = my; - /* has allready been set to the 4 MV if 4MV is done */ + /* has already been set to the 4 MV if 4MV is done */ if(mv4){ int mot_xy= s->block_index[0]; @@ -733,7 +471,7 @@ static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my, int mv4) s->current_picture.motion_val[0][mot_xy+1][0]= mx; s->current_picture.motion_val[0][mot_xy+1][1]= my; - mot_xy += s->block_wrap[0]; + mot_xy += s->b8_stride; s->current_picture.motion_val[0][mot_xy ][0]= mx; s->current_picture.motion_val[0][mot_xy ][1]= my; s->current_picture.motion_val[0][mot_xy+1][0]= mx; @@ -746,116 +484,116 @@ static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my, int mv4) */ static inline void get_limits(MpegEncContext *s, int x, int y) { + MotionEstContext * const c= &s->me; + int range= c->avctx->me_range >> (1 + !!(c->flags&FLAG_QPEL)); /* - if(s->avctx->me_range) s->me.range= s->avctx->me_range >> 1; - else s->me.range= 16; + if(c->avctx->me_range) c->range= c->avctx->me_range >> 1; + else c->range= 16; */ if (s->unrestricted_mv) { - s->me.xmin = - x - 16; - s->me.ymin = - y - 16; - s->me.xmax = - x + s->mb_width *16; - s->me.ymax = - y + s->mb_height*16; + c->xmin = - x - 16; + c->ymin = - y - 16; + c->xmax = - x + s->mb_width *16; + c->ymax = - y + s->mb_height*16; + } else if (s->out_format == FMT_H261){ + // Search range of H261 is different from other codec standards + c->xmin = (x > 15) ? - 15 : 0; + c->ymin = (y > 15) ? - 15 : 0; + c->xmax = (x < s->mb_width * 16 - 16) ? 15 : 0; + c->ymax = (y < s->mb_height * 16 - 16) ? 15 : 0; } else { - s->me.xmin = - x; - s->me.ymin = - y; - s->me.xmax = - x + s->mb_width *16 - 16; - s->me.ymax = - y + s->mb_height*16 - 16; + c->xmin = - x; + c->ymin = - y; + c->xmax = - x + s->mb_width *16 - 16; + c->ymax = - y + s->mb_height*16 - 16; } + if(range){ + c->xmin = FFMAX(c->xmin,-range); + c->xmax = FFMIN(c->xmax, range); + c->ymin = FFMAX(c->ymin,-range); + c->ymax = FFMIN(c->ymax, range); + } +} + +static inline void init_mv4_ref(MotionEstContext *c){ + const int stride= c->stride; + + c->ref[1][0] = c->ref[0][0] + 8; + c->ref[2][0] = c->ref[0][0] + 8*stride; + c->ref[3][0] = c->ref[2][0] + 8; + c->src[1][0] = c->src[0][0] + 8; + c->src[2][0] = c->src[0][0] + 8*stride; + c->src[3][0] = c->src[2][0] + 8; } static inline int h263_mv4_search(MpegEncContext *s, int mx, int my, int shift) { + MotionEstContext * const c= &s->me; const int size= 1; const int h=8; int block; int P[10][2]; int dmin_sum=0, mx4_sum=0, my4_sum=0; - uint8_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV; int same=1; - const int stride= s->linesize; - const int uvstride= s->uvlinesize; - const int xmin= s->me.xmin; - const int ymin= s->me.ymin; - const int xmax= s->me.xmax; - const int ymax= s->me.ymax; + const int stride= c->stride; + uint8_t *mv_penalty= c->current_mv_penalty; + + init_mv4_ref(c); for(block=0; block<4; block++){ int mx4, my4; int pred_x4, pred_y4; int dmin4; static const int off[4]= {2, 1, 1, -1}; - const int mot_stride = s->block_wrap[0]; + const int mot_stride = s->b8_stride; const int mot_xy = s->block_index[block]; - const int block_x= (block&1); - const int block_y= (block>>1); - uint8_t *src_data[3]= { - s->new_picture.data[0] + 8*(2*s->mb_x + block_x) + stride *8*(2*s->mb_y + block_y), //FIXME chroma? - s->new_picture.data[1] + 4*(2*s->mb_x + block_x) + uvstride*4*(2*s->mb_y + block_y), - s->new_picture.data[2] + 4*(2*s->mb_x + block_x) + uvstride*4*(2*s->mb_y + block_y) - }; - uint8_t *ref_data[3]= { - s->last_picture.data[0] + 8*(2*s->mb_x + block_x) + stride *8*(2*s->mb_y + block_y), //FIXME chroma? - s->last_picture.data[1] + 4*(2*s->mb_x + block_x) + uvstride*4*(2*s->mb_y + block_y), - s->last_picture.data[2] + 4*(2*s->mb_x + block_x) + uvstride*4*(2*s->mb_y + block_y) - }; P_LEFT[0] = s->current_picture.motion_val[0][mot_xy - 1][0]; P_LEFT[1] = s->current_picture.motion_val[0][mot_xy - 1][1]; - if(P_LEFT[0] > (s->me.xmax<me.xmax< (c->xmax<xmax<mb_y == 0 && block<2) { - pred_x4= P_LEFT[0]; - pred_y4= P_LEFT[1]; + if (s->first_slice_line && block<2) { + c->pred_x= pred_x4= P_LEFT[0]; + c->pred_y= pred_y4= P_LEFT[1]; } else { P_TOP[0] = s->current_picture.motion_val[0][mot_xy - mot_stride ][0]; P_TOP[1] = s->current_picture.motion_val[0][mot_xy - mot_stride ][1]; P_TOPRIGHT[0] = s->current_picture.motion_val[0][mot_xy - mot_stride + off[block]][0]; P_TOPRIGHT[1] = s->current_picture.motion_val[0][mot_xy - mot_stride + off[block]][1]; - if(P_TOP[1] > (s->me.ymax<me.ymax<me.xmin<me.xmin< (s->me.xmax<me.xmax< (s->me.ymax<me.ymax< (c->ymax<ymax<xmin<xmin< (c->xmax<xmax< (c->ymax<ymax<out_format == FMT_H263){ - pred_x4 = P_MEDIAN[0]; - pred_y4 = P_MEDIAN[1]; -#if 0 - }else { /* mpeg1 at least */ - pred_x4= P_LEFT[0]; - pred_y4= P_LEFT[1]; - } -#endif + c->pred_x= pred_x4 = P_MEDIAN[0]; + c->pred_y= pred_y4 = P_MEDIAN[1]; } P_MV1[0]= mx; P_MV1[1]= my; - dmin4 = s->me.motion_search[1](s, &mx4, &my4, P, pred_x4, pred_y4, - src_data, ref_data, stride, uvstride, s->p_mv_table, (1<<16)>>shift, mv_penalty); + dmin4 = epzs_motion_search4(s, &mx4, &my4, P, block, block, s->p_mv_table, (1<<16)>>shift); - dmin4= s->me.sub_motion_search(s, &mx4, &my4, dmin4, - pred_x4, pred_y4, src_data, ref_data, stride, uvstride, size, h, mv_penalty); - - if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0] - && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE){ + dmin4= c->sub_motion_search(s, &mx4, &my4, dmin4, block, block, size, h); + + if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){ int dxy; const int offset= ((block&1) + (block>>1)*stride)*8; - uint8_t *dest_y = s->me.scratchpad + offset; - + uint8_t *dest_y = c->scratchpad + offset; if(s->quarter_sample){ - uint8_t *ref= ref_data[0] + (mx4>>2) + (my4>>2)*stride; + uint8_t *ref= c->ref[block][0] + (mx4>>2) + (my4>>2)*stride; dxy = ((my4 & 3) << 2) | (mx4 & 3); if(s->no_rounding) - s->dsp.put_no_rnd_qpel_pixels_tab[1][dxy](dest_y , ref , s->linesize); + s->dsp.put_no_rnd_qpel_pixels_tab[1][dxy](dest_y , ref , stride); else s->dsp.put_qpel_pixels_tab [1][dxy](dest_y , ref , stride); }else{ - uint8_t *ref= ref_data[0] + (mx4>>1) + (my4>>1)*stride; + uint8_t *ref= c->ref[block][0] + (mx4>>1) + (my4>>1)*stride; dxy = ((my4 & 1) << 1) | (mx4 & 1); if(s->no_rounding) @@ -863,7 +601,7 @@ static inline int h263_mv4_search(MpegEncContext *s, int mx, int my, int shift) else s->dsp.put_pixels_tab [1][dxy](dest_y , ref , stride, h); } - dmin_sum+= (mv_penalty[mx4-pred_x4] + mv_penalty[my4-pred_y4])*s->me.mb_penalty_factor; + dmin_sum+= (mv_penalty[mx4-pred_x4] + mv_penalty[my4-pred_y4])*c->mb_penalty_factor; }else dmin_sum+= dmin4; @@ -874,21 +612,21 @@ static inline int h263_mv4_search(MpegEncContext *s, int mx, int my, int shift) mx4_sum+= mx4; my4_sum+= my4; } - + s->current_picture.motion_val[0][ s->block_index[block] ][0]= mx4; s->current_picture.motion_val[0][ s->block_index[block] ][1]= my4; if(mx4 != mx || my4 != my) same=0; } - + if(same) return INT_MAX; - + if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){ - dmin_sum += s->dsp.mb_cmp[0](s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*16*stride, s->me.scratchpad, stride, 16); + dmin_sum += s->dsp.mb_cmp[0](s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*16*stride, c->scratchpad, stride, 16); } - - if(s->avctx->mb_cmp&FF_CMP_CHROMA){ + + if(c->avctx->mb_cmp&FF_CMP_CHROMA){ int dxy; int mx, my; int offset; @@ -896,121 +634,133 @@ static inline int h263_mv4_search(MpegEncContext *s, int mx, int my, int shift) mx= ff_h263_round_chroma(mx4_sum); my= ff_h263_round_chroma(my4_sum); dxy = ((my & 1) << 1) | (mx & 1); - + offset= (s->mb_x*8 + (mx>>1)) + (s->mb_y*8 + (my>>1))*s->uvlinesize; - + if(s->no_rounding){ - s->dsp.put_no_rnd_pixels_tab[1][dxy](s->me.scratchpad , s->last_picture.data[1] + offset, s->uvlinesize, 8); - s->dsp.put_no_rnd_pixels_tab[1][dxy](s->me.scratchpad+8 , s->last_picture.data[2] + offset, s->uvlinesize, 8); + s->dsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad , s->last_picture.data[1] + offset, s->uvlinesize, 8); + s->dsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad+8 , s->last_picture.data[2] + offset, s->uvlinesize, 8); }else{ - s->dsp.put_pixels_tab [1][dxy](s->me.scratchpad , s->last_picture.data[1] + offset, s->uvlinesize, 8); - s->dsp.put_pixels_tab [1][dxy](s->me.scratchpad+8 , s->last_picture.data[2] + offset, s->uvlinesize, 8); + s->dsp.put_pixels_tab [1][dxy](c->scratchpad , s->last_picture.data[1] + offset, s->uvlinesize, 8); + s->dsp.put_pixels_tab [1][dxy](c->scratchpad+8 , s->last_picture.data[2] + offset, s->uvlinesize, 8); } - dmin_sum += s->dsp.mb_cmp[1](s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*8*s->uvlinesize, s->me.scratchpad , s->uvlinesize, 8); - dmin_sum += s->dsp.mb_cmp[1](s, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*8*s->uvlinesize, s->me.scratchpad+8, s->uvlinesize, 8); + dmin_sum += s->dsp.mb_cmp[1](s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*8*s->uvlinesize, c->scratchpad , s->uvlinesize, 8); + dmin_sum += s->dsp.mb_cmp[1](s, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*8*s->uvlinesize, c->scratchpad+8, s->uvlinesize, 8); } - switch(s->avctx->mb_cmp&0xFF){ + c->pred_x= mx; + c->pred_y= my; + + switch(c->avctx->mb_cmp&0xFF){ /*case FF_CMP_SSE: return dmin_sum+ 32*s->qscale*s->qscale;*/ case FF_CMP_RD: return dmin_sum; default: - return dmin_sum+ 11*s->me.mb_penalty_factor; + return dmin_sum+ 11*c->mb_penalty_factor; } } -static int interlaced_search(MpegEncContext *s, uint8_t *frame_src_data[3], uint8_t *frame_ref_data[3], - int16_t (*mv_tables[2][2])[2], uint8_t *field_select_tables[2], int f_code, int mx, int my) +static inline void init_interlaced_ref(MpegEncContext *s, int ref_index){ + MotionEstContext * const c= &s->me; + + c->ref[1+ref_index][0] = c->ref[0+ref_index][0] + s->linesize; + c->src[1][0] = c->src[0][0] + s->linesize; + if(c->flags & FLAG_CHROMA){ + c->ref[1+ref_index][1] = c->ref[0+ref_index][1] + s->uvlinesize; + c->ref[1+ref_index][2] = c->ref[0+ref_index][2] + s->uvlinesize; + c->src[1][1] = c->src[0][1] + s->uvlinesize; + c->src[1][2] = c->src[0][2] + s->uvlinesize; + } +} + +static int interlaced_search(MpegEncContext *s, int ref_index, + int16_t (*mv_tables[2][2])[2], uint8_t *field_select_tables[2], int mx, int my, int user_field_select) { + MotionEstContext * const c= &s->me; const int size=0; const int h=8; int block; int P[10][2]; - uint8_t * const mv_penalty= s->me.mv_penalty[f_code] + MAX_MV; + uint8_t * const mv_penalty= c->current_mv_penalty; int same=1; const int stride= 2*s->linesize; - const int uvstride= 2*s->uvlinesize; int dmin_sum= 0; const int mot_stride= s->mb_stride; const int xy= s->mb_x + s->mb_y*mot_stride; - - s->me.ymin>>=1; - s->me.ymax>>=1; - + + c->ymin>>=1; + c->ymax>>=1; + c->stride<<=1; + c->uvstride<<=1; + init_interlaced_ref(s, ref_index); + for(block=0; block<2; block++){ int field_select; int best_dmin= INT_MAX; int best_field= -1; - uint8_t *src_data[3]= { - frame_src_data[0] + s-> linesize*block, - frame_src_data[1] + s->uvlinesize*block, - frame_src_data[2] + s->uvlinesize*block - }; - for(field_select=0; field_select<2; field_select++){ - int dmin, mx_i, my_i, pred_x, pred_y; - uint8_t *ref_data[3]= { - frame_ref_data[0] + s-> linesize*field_select, - frame_ref_data[1] + s->uvlinesize*field_select, - frame_ref_data[2] + s->uvlinesize*field_select - }; + int dmin, mx_i, my_i; int16_t (*mv_table)[2]= mv_tables[block][field_select]; - + + if(user_field_select){ + assert(field_select==0 || field_select==1); + assert(field_select_tables[block][xy]==0 || field_select_tables[block][xy]==1); + if(field_select_tables[block][xy] != field_select) + continue; + } + P_LEFT[0] = mv_table[xy - 1][0]; P_LEFT[1] = mv_table[xy - 1][1]; - if(P_LEFT[0] > (s->me.xmax<<1)) P_LEFT[0] = (s->me.xmax<<1); - - pred_x= P_LEFT[0]; - pred_y= P_LEFT[1]; - - if(s->mb_y){ + if(P_LEFT[0] > (c->xmax<<1)) P_LEFT[0] = (c->xmax<<1); + + c->pred_x= P_LEFT[0]; + c->pred_y= P_LEFT[1]; + + if(!s->first_slice_line){ P_TOP[0] = mv_table[xy - mot_stride][0]; P_TOP[1] = mv_table[xy - mot_stride][1]; P_TOPRIGHT[0] = mv_table[xy - mot_stride + 1][0]; P_TOPRIGHT[1] = mv_table[xy - mot_stride + 1][1]; - if(P_TOP[1] > (s->me.ymax<<1)) P_TOP[1] = (s->me.ymax<<1); - if(P_TOPRIGHT[0] < (s->me.xmin<<1)) P_TOPRIGHT[0]= (s->me.xmin<<1); - if(P_TOPRIGHT[0] > (s->me.xmax<<1)) P_TOPRIGHT[0]= (s->me.xmax<<1); - if(P_TOPRIGHT[1] > (s->me.ymax<<1)) P_TOPRIGHT[1]= (s->me.ymax<<1); - + if(P_TOP[1] > (c->ymax<<1)) P_TOP[1] = (c->ymax<<1); + if(P_TOPRIGHT[0] < (c->xmin<<1)) P_TOPRIGHT[0]= (c->xmin<<1); + if(P_TOPRIGHT[0] > (c->xmax<<1)) P_TOPRIGHT[0]= (c->xmax<<1); + if(P_TOPRIGHT[1] > (c->ymax<<1)) P_TOPRIGHT[1]= (c->ymax<<1); + P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]); P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]); } P_MV1[0]= mx; //FIXME not correct if block != field_select P_MV1[1]= my / 2; - - dmin = s->me.motion_search[4](s, &mx_i, &my_i, P, pred_x, pred_y, - src_data, ref_data, stride, uvstride, mv_table, (1<<16)>>1, mv_penalty); - dmin= s->me.sub_motion_search(s, &mx_i, &my_i, dmin, - pred_x, pred_y, src_data, ref_data, stride, uvstride, size, h, mv_penalty); - + dmin = epzs_motion_search2(s, &mx_i, &my_i, P, block, field_select+ref_index, mv_table, (1<<16)>>1); + + dmin= c->sub_motion_search(s, &mx_i, &my_i, dmin, block, field_select+ref_index, size, h); + mv_table[xy][0]= mx_i; mv_table[xy][1]= my_i; - - if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0] - && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE){ + + if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){ int dxy; //FIXME chroma ME - uint8_t *ref= ref_data[0] + (mx_i>>1) + (my_i>>1)*stride; + uint8_t *ref= c->ref[field_select+ref_index][0] + (mx_i>>1) + (my_i>>1)*stride; dxy = ((my_i & 1) << 1) | (mx_i & 1); if(s->no_rounding){ - s->dsp.put_no_rnd_pixels_tab[size][dxy](s->me.scratchpad, ref , stride, h); + s->dsp.put_no_rnd_pixels_tab[size][dxy](c->scratchpad, ref , stride, h); }else{ - s->dsp.put_pixels_tab [size][dxy](s->me.scratchpad, ref , stride, h); + s->dsp.put_pixels_tab [size][dxy](c->scratchpad, ref , stride, h); } - dmin= s->dsp.mb_cmp[size](s, src_data[0], s->me.scratchpad, stride, h); - dmin+= (mv_penalty[mx_i-pred_x] + mv_penalty[my_i-pred_y] + 1)*s->me.mb_penalty_factor; + dmin= s->dsp.mb_cmp[size](s, c->src[block][0], c->scratchpad, stride, h); + dmin+= (mv_penalty[mx_i-c->pred_x] + mv_penalty[my_i-c->pred_y] + 1)*c->mb_penalty_factor; }else - dmin+= s->me.mb_penalty_factor; //field_select bits - + dmin+= c->mb_penalty_factor; //field_select bits + dmin += field_select != block; //slightly prefer same field - + if(dmin < best_dmin){ best_dmin= dmin; best_field= field_select; @@ -1021,178 +771,331 @@ static int interlaced_search(MpegEncContext *s, uint8_t *frame_src_data[3], uint if(mv_table[xy][0] != mx) same=0; //FIXME check if these checks work and are any good at all if(mv_table[xy][1]&1) same=0; - if(mv_table[xy][1]*2 != my) same=0; + if(mv_table[xy][1]*2 != my) same=0; if(best_field != block) same=0; } field_select_tables[block][xy]= best_field; dmin_sum += best_dmin; } - - s->me.ymin<<=1; - s->me.ymax<<=1; + + c->ymin<<=1; + c->ymax<<=1; + c->stride>>=1; + c->uvstride>>=1; if(same) return INT_MAX; - - switch(s->avctx->mb_cmp&0xFF){ + + switch(c->avctx->mb_cmp&0xFF){ /*case FF_CMP_SSE: return dmin_sum+ 32*s->qscale*s->qscale;*/ case FF_CMP_RD: return dmin_sum; default: - return dmin_sum+ 11*s->me.mb_penalty_factor; + return dmin_sum+ 11*c->mb_penalty_factor; } } +static void clip_input_mv(MpegEncContext * s, int16_t *mv, int interlaced){ + int ymax= s->me.ymax>>interlaced; + int ymin= s->me.ymin>>interlaced; + + if(mv[0] < s->me.xmin) mv[0] = s->me.xmin; + if(mv[0] > s->me.xmax) mv[0] = s->me.xmax; + if(mv[1] < ymin) mv[1] = ymin; + if(mv[1] > ymax) mv[1] = ymax; +} + +static inline int check_input_motion(MpegEncContext * s, int mb_x, int mb_y, int p_type){ + MotionEstContext * const c= &s->me; + Picture *p= s->current_picture_ptr; + int mb_xy= mb_x + mb_y*s->mb_stride; + int xy= 2*mb_x + 2*mb_y*s->b8_stride; + int mb_type= s->current_picture.mb_type[mb_xy]; + int flags= c->flags; + int shift= (flags&FLAG_QPEL) + 1; + int mask= (1<dsp.sse[0]; + me_cmp_func chroma_cmpf= s->dsp.sse[1]; + + if(p_type && USES_LIST(mb_type, 1)){ + av_log(c->avctx, AV_LOG_ERROR, "backward motion vector in P frame\n"); + return INT_MAX/2; + } + assert(IS_INTRA(mb_type) || USES_LIST(mb_type,0) || USES_LIST(mb_type,1)); + + for(i=0; i<4; i++){ + int xy= s->block_index[i]; + clip_input_mv(s, p->motion_val[0][xy], !!IS_INTERLACED(mb_type)); + clip_input_mv(s, p->motion_val[1][xy], !!IS_INTERLACED(mb_type)); + } + + if(IS_INTERLACED(mb_type)){ + int xy2= xy + s->b8_stride; + s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTRA; + c->stride<<=1; + c->uvstride<<=1; + + if(!(s->flags & CODEC_FLAG_INTERLACED_ME)){ + av_log(c->avctx, AV_LOG_ERROR, "Interlaced macroblock selected but interlaced motion estimation disabled\n"); + return INT_MAX/2; + } + + if(USES_LIST(mb_type, 0)){ + int field_select0= p->ref_index[0][xy ]; + int field_select1= p->ref_index[0][xy2]; + assert(field_select0==0 ||field_select0==1); + assert(field_select1==0 ||field_select1==1); + init_interlaced_ref(s, 0); + + if(p_type){ + s->p_field_select_table[0][mb_xy]= field_select0; + s->p_field_select_table[1][mb_xy]= field_select1; + *(uint32_t*)s->p_field_mv_table[0][field_select0][mb_xy]= *(uint32_t*)p->motion_val[0][xy ]; + *(uint32_t*)s->p_field_mv_table[1][field_select1][mb_xy]= *(uint32_t*)p->motion_val[0][xy2]; + s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER_I; + }else{ + s->b_field_select_table[0][0][mb_xy]= field_select0; + s->b_field_select_table[0][1][mb_xy]= field_select1; + *(uint32_t*)s->b_field_mv_table[0][0][field_select0][mb_xy]= *(uint32_t*)p->motion_val[0][xy ]; + *(uint32_t*)s->b_field_mv_table[0][1][field_select1][mb_xy]= *(uint32_t*)p->motion_val[0][xy2]; + s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_FORWARD_I; + } + + x= p->motion_val[0][xy ][0]; + y= p->motion_val[0][xy ][1]; + d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select0, 0, cmpf, chroma_cmpf, flags); + x= p->motion_val[0][xy2][0]; + y= p->motion_val[0][xy2][1]; + d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select1, 1, cmpf, chroma_cmpf, flags); + } + if(USES_LIST(mb_type, 1)){ + int field_select0= p->ref_index[1][xy ]; + int field_select1= p->ref_index[1][xy2]; + assert(field_select0==0 ||field_select0==1); + assert(field_select1==0 ||field_select1==1); + init_interlaced_ref(s, 2); + + s->b_field_select_table[1][0][mb_xy]= field_select0; + s->b_field_select_table[1][1][mb_xy]= field_select1; + *(uint32_t*)s->b_field_mv_table[1][0][field_select0][mb_xy]= *(uint32_t*)p->motion_val[1][xy ]; + *(uint32_t*)s->b_field_mv_table[1][1][field_select1][mb_xy]= *(uint32_t*)p->motion_val[1][xy2]; + if(USES_LIST(mb_type, 0)){ + s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_BIDIR_I; + }else{ + s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_BACKWARD_I; + } + + x= p->motion_val[1][xy ][0]; + y= p->motion_val[1][xy ][1]; + d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select0+2, 0, cmpf, chroma_cmpf, flags); + x= p->motion_val[1][xy2][0]; + y= p->motion_val[1][xy2][1]; + d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select1+2, 1, cmpf, chroma_cmpf, flags); + //FIXME bidir scores + } + c->stride>>=1; + c->uvstride>>=1; + }else if(IS_8X8(mb_type)){ + if(!(s->flags & CODEC_FLAG_4MV)){ + av_log(c->avctx, AV_LOG_ERROR, "4MV macroblock selected but 4MV encoding disabled\n"); + return INT_MAX/2; + } + cmpf= s->dsp.sse[1]; + chroma_cmpf= s->dsp.sse[1]; + init_mv4_ref(c); + for(i=0; i<4; i++){ + xy= s->block_index[i]; + x= p->motion_val[0][xy][0]; + y= p->motion_val[0][xy][1]; + d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 1, 8, i, i, cmpf, chroma_cmpf, flags); + } + s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER4V; + }else{ + if(USES_LIST(mb_type, 0)){ + if(p_type){ + *(uint32_t*)s->p_mv_table[mb_xy]= *(uint32_t*)p->motion_val[0][xy]; + s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER; + }else if(USES_LIST(mb_type, 1)){ + *(uint32_t*)s->b_bidir_forw_mv_table[mb_xy]= *(uint32_t*)p->motion_val[0][xy]; + *(uint32_t*)s->b_bidir_back_mv_table[mb_xy]= *(uint32_t*)p->motion_val[1][xy]; + s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_BIDIR; + }else{ + *(uint32_t*)s->b_forw_mv_table[mb_xy]= *(uint32_t*)p->motion_val[0][xy]; + s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_FORWARD; + } + x= p->motion_val[0][xy][0]; + y= p->motion_val[0][xy][1]; + d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 16, 0, 0, cmpf, chroma_cmpf, flags); + }else if(USES_LIST(mb_type, 1)){ + *(uint32_t*)s->b_back_mv_table[mb_xy]= *(uint32_t*)p->motion_val[1][xy]; + s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_BACKWARD; + + x= p->motion_val[1][xy][0]; + y= p->motion_val[1][xy][1]; + d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 16, 2, 0, cmpf, chroma_cmpf, flags); + }else + s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTRA; + } + return d; +} + void ff_estimate_p_frame_motion(MpegEncContext * s, int mb_x, int mb_y) { + MotionEstContext * const c= &s->me; uint8_t *pix, *ppix; - int sum, varc, vard, mx, my, dmin, xx, yy; - int pred_x=0, pred_y=0; + int sum, mx, my, dmin; + int varc; ///< the variance of the block (sum of squared (p[y][x]-average)) + int vard; ///< sum of squared differences with the estimated motion vector int P[10][2]; const int shift= 1+s->quarter_sample; int mb_type=0; - uint8_t *ref_picture= s->last_picture.data[0]; Picture * const pic= &s->current_picture; - uint8_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV; - const int stride= s->linesize; - const int uvstride= s->uvlinesize; - uint8_t *src_data[3]= { - s->new_picture.data[0] + 16*(mb_x + stride*mb_y), - s->new_picture.data[1] + 8*(mb_x + uvstride*mb_y), - s->new_picture.data[2] + 8*(mb_x + uvstride*mb_y) - }; - uint8_t *ref_data[3]= { - s->last_picture.data[0] + 16*(mb_x + stride*mb_y), - s->last_picture.data[1] + 8*(mb_x + uvstride*mb_y), - s->last_picture.data[2] + 8*(mb_x + uvstride*mb_y) - }; + + init_ref(c, s->new_picture.data, s->last_picture.data, NULL, 16*mb_x, 16*mb_y, 0); assert(s->quarter_sample==0 || s->quarter_sample==1); + assert(s->linesize == c->stride); + assert(s->uvlinesize == c->uvstride); - s->me.penalty_factor = get_penalty_factor(s, s->avctx->me_cmp); - s->me.sub_penalty_factor= get_penalty_factor(s, s->avctx->me_sub_cmp); - s->me.mb_penalty_factor = get_penalty_factor(s, s->avctx->mb_cmp); + c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp); + c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp); + c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp); + c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV; get_limits(s, 16*mb_x, 16*mb_y); - s->me.skip=0; + c->skip=0; + + /* intra / predictive decision */ + pix = c->src[0][0]; + sum = s->dsp.pix_sum(pix, s->linesize); + varc = s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500; + + pic->mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8; + pic->mb_var [s->mb_stride * mb_y + mb_x] = (varc+128)>>8; + c->mb_var_sum_temp += (varc+128)>>8; + + if(c->avctx->me_threshold){ + vard= check_input_motion(s, mb_x, mb_y, 1); + + if((vard+128)>>8 < c->avctx->me_threshold){ + int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100); + int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20; + pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8; + c->mc_mb_var_sum_temp += (vard+128)>>8; + c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score); + return; + } + if((vard+128)>>8 < c->avctx->mb_threshold) + mb_type= s->mb_type[mb_x + mb_y*s->mb_stride]; + } switch(s->me_method) { case ME_ZERO: default: - no_motion_search(s, &mx, &my); + no_motion_search(s, &mx, &my); mx-= mb_x*16; my-= mb_y*16; dmin = 0; break; -#if 0 - case ME_FULL: - dmin = full_motion_search(s, &mx, &my, range, ref_picture); - mx-= mb_x*16; - my-= mb_y*16; - break; - case ME_LOG: - dmin = log_motion_search(s, &mx, &my, range / 2, ref_picture); - mx-= mb_x*16; - my-= mb_y*16; - break; - case ME_PHODS: - dmin = phods_motion_search(s, &mx, &my, range / 2, ref_picture); - mx-= mb_x*16; - my-= mb_y*16; - break; -#endif case ME_X1: case ME_EPZS: { - const int mot_stride = s->block_wrap[0]; + const int mot_stride = s->b8_stride; const int mot_xy = s->block_index[0]; P_LEFT[0] = s->current_picture.motion_val[0][mot_xy - 1][0]; P_LEFT[1] = s->current_picture.motion_val[0][mot_xy - 1][1]; - if(P_LEFT[0] > (s->me.xmax<me.xmax< (c->xmax<xmax<first_slice_line) { P_TOP[0] = s->current_picture.motion_val[0][mot_xy - mot_stride ][0]; P_TOP[1] = s->current_picture.motion_val[0][mot_xy - mot_stride ][1]; P_TOPRIGHT[0] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][0]; P_TOPRIGHT[1] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][1]; - if(P_TOP[1] > (s->me.ymax<me.ymax<me.xmin<me.xmin< (s->me.ymax<me.ymax< (c->ymax<ymax<xmin<xmin< (c->ymax<ymax<out_format == FMT_H263){ - pred_x = P_MEDIAN[0]; - pred_y = P_MEDIAN[1]; + c->pred_x = P_MEDIAN[0]; + c->pred_y = P_MEDIAN[1]; }else { /* mpeg1 at least */ - pred_x= P_LEFT[0]; - pred_y= P_LEFT[1]; + c->pred_x= P_LEFT[0]; + c->pred_y= P_LEFT[1]; } }else{ - pred_x= P_LEFT[0]; - pred_y= P_LEFT[1]; + c->pred_x= P_LEFT[0]; + c->pred_y= P_LEFT[1]; } } - dmin = s->me.motion_search[0](s, &mx, &my, P, pred_x, pred_y, - src_data, ref_data, stride, uvstride, s->p_mv_table, (1<<16)>>shift, mv_penalty); - + dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16); + break; } - /* intra / predictive decision */ - xx = mb_x * 16; - yy = mb_y * 16; - - pix = src_data[0]; /* At this point (mx,my) are full-pell and the relative displacement */ - ppix = ref_data[0] + (my * s->linesize) + mx; - - sum = s->dsp.pix_sum(pix, s->linesize); - - varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8; - vard = (s->dsp.sse[0](NULL, pix, ppix, s->linesize, 16)+128)>>8; + ppix = c->ref[0][0] + (my * s->linesize) + mx; + + vard = s->dsp.sse[0](NULL, pix, ppix, s->linesize, 16); + + pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8; +// pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin; + c->mc_mb_var_sum_temp += (vard+128)>>8; -//printf("%d %d %d %X %X %X\n", s->mb_width, mb_x, mb_y,(int)s, (int)s->mb_var, (int)s->mc_mb_var); fflush(stdout); - pic->mb_var [s->mb_stride * mb_y + mb_x] = varc; - pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = vard; - pic->mb_mean [s->mb_stride * mb_y + mb_x] = (sum+128)>>8; -// pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin; - pic->mb_var_sum += varc; - pic->mc_mb_var_sum += vard; -//printf("E%d %d %d %X %X %X\n", s->mb_width, mb_x, mb_y,(int)s, (int)s->mb_var, (int)s->mc_mb_var); fflush(stdout); - #if 0 printf("varc=%4d avg_var=%4d (sum=%4d) vard=%4d mx=%2d my=%2d\n", - varc, s->avg_mb_var, sum, vard, mx - xx, my - yy); + varc, s->avg_mb_var, sum, vard, mx - xx, my - yy); #endif - if(s->avctx->mb_decision > FF_MB_DECISION_SIMPLE){ - if (vard <= 64 || vard < varc) - s->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc); - else - s->scene_change_score+= s->qscale; + if(mb_type){ + int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100); + int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20; + c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score); - if (vard*2 + 200 > varc) + if(mb_type == CANDIDATE_MB_TYPE_INTER){ + c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16); + set_p_mv_tables(s, mx, my, 1); + }else{ + mx <<=shift; + my <<=shift; + } + if(mb_type == CANDIDATE_MB_TYPE_INTER4V){ + h263_mv4_search(s, mx, my, shift); + + set_p_mv_tables(s, mx, my, 0); + } + if(mb_type == CANDIDATE_MB_TYPE_INTER_I){ + interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 1); + } + }else if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){ + int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100); + int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20; + c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score); + + if (vard*2 + 200*256 > varc) mb_type|= CANDIDATE_MB_TYPE_INTRA; - if (varc*2 + 200 > vard){ + if (varc*2 + 200*256 > vard || s->qscale > 24){ +// if (varc*2 + 200*256 + 50*(s->lambda2>>FF_LAMBDA_SHIFT) > vard){ mb_type|= CANDIDATE_MB_TYPE_INTER; - s->me.sub_motion_search(s, &mx, &my, dmin, - pred_x, pred_y, src_data, ref_data, stride, uvstride, 0, 16, mv_penalty); + c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16); if(s->flags&CODEC_FLAG_MV0) if(mx || my) - mb_type |= CANDIDATE_MB_TYPE_SKIPED; //FIXME check difference + mb_type |= CANDIDATE_MB_TYPE_SKIPPED; //FIXME check difference }else{ mx <<=shift; my <<=shift; } if((s->flags&CODEC_FLAG_4MV) - && !s->me.skip && varc>50 && vard>10){ + && !c->skip && varc>50<<8 && vard>10<<8){ if(h263_mv4_search(s, mx, my, shift) < INT_MAX) mb_type|=CANDIDATE_MB_TYPE_INTER4V; @@ -1200,21 +1103,20 @@ void ff_estimate_p_frame_motion(MpegEncContext * s, }else set_p_mv_tables(s, mx, my, 1); if((s->flags&CODEC_FLAG_INTERLACED_ME) - && !s->me.skip){ //FIXME varc/d checks - if(interlaced_search(s, src_data, ref_data, s->p_field_mv_table, s->p_field_select_table, s->f_code, mx, my) < INT_MAX) + && !c->skip){ //FIXME varc/d checks + if(interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0) < INT_MAX) mb_type |= CANDIDATE_MB_TYPE_INTER_I; } }else{ int intra_score, i; mb_type= CANDIDATE_MB_TYPE_INTER; - dmin= s->me.sub_motion_search(s, &mx, &my, dmin, - pred_x, pred_y, src_data, ref_data, stride, uvstride, 0, 16, mv_penalty); - if(s->avctx->me_sub_cmp != s->avctx->mb_cmp && !s->me.skip) - dmin= s->me.get_mb_score(s, mx, my, pred_x, pred_y, src_data, ref_data, stride, uvstride, mv_penalty); + dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16); + if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip) + dmin= ff_get_mb_score(s, mx, my, 0, 0, 0, 16, 1); if((s->flags&CODEC_FLAG_4MV) - && !s->me.skip && varc>50 && vard>10){ + && !c->skip && varc>50<<8 && vard>10<<8){ int dmin4= h263_mv4_search(s, mx, my, shift); if(dmin4 < dmin){ mb_type= CANDIDATE_MB_TYPE_INTER4V; @@ -1222,69 +1124,69 @@ void ff_estimate_p_frame_motion(MpegEncContext * s, } } if((s->flags&CODEC_FLAG_INTERLACED_ME) - && !s->me.skip){ //FIXME varc/d checks - int dmin_i= interlaced_search(s, src_data, ref_data, s->p_field_mv_table, s->p_field_select_table, s->f_code, mx, my); + && !c->skip){ //FIXME varc/d checks + int dmin_i= interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0); if(dmin_i < dmin){ mb_type = CANDIDATE_MB_TYPE_INTER_I; dmin= dmin_i; } } - -// pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin; + +// pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin; set_p_mv_tables(s, mx, my, mb_type!=CANDIDATE_MB_TYPE_INTER4V); /* get intra luma score */ - if((s->avctx->mb_cmp&0xFF)==FF_CMP_SSE){ - intra_score= (varc<<8) - 500; //FIXME dont scale it down so we dont have to fix it + if((c->avctx->mb_cmp&0xFF)==FF_CMP_SSE){ + intra_score= varc - 500; }else{ int mean= (sum+128)>>8; mean*= 0x01010101; - + for(i=0; i<16; i++){ - *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 0]) = mean; - *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 4]) = mean; - *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 8]) = mean; - *(uint32_t*)(&s->me.scratchpad[i*s->linesize+12]) = mean; + *(uint32_t*)(&c->scratchpad[i*s->linesize+ 0]) = mean; + *(uint32_t*)(&c->scratchpad[i*s->linesize+ 4]) = mean; + *(uint32_t*)(&c->scratchpad[i*s->linesize+ 8]) = mean; + *(uint32_t*)(&c->scratchpad[i*s->linesize+12]) = mean; } - intra_score= s->dsp.mb_cmp[0](s, s->me.scratchpad, pix, s->linesize, 16); + intra_score= s->dsp.mb_cmp[0](s, c->scratchpad, pix, s->linesize, 16); } #if 0 //FIXME /* get chroma score */ - if(s->avctx->mb_cmp&FF_CMP_CHROMA){ + if(c->avctx->mb_cmp&FF_CMP_CHROMA){ for(i=1; i<3; i++){ uint8_t *dest_c; int mean; - + if(s->out_format == FMT_H263){ - mean= (s->dc_val[i][mb_x + (mb_y+1)*(s->mb_width+2)] + 4)>>3; //FIXME not exact but simple ;) + mean= (s->dc_val[i][mb_x + mb_y*s->b8_stride] + 4)>>3; //FIXME not exact but simple ;) }else{ mean= (s->last_dc[i] + 4)>>3; } dest_c = s->new_picture.data[i] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8; - + mean*= 0x01010101; for(i=0; i<8; i++){ - *(uint32_t*)(&s->me.scratchpad[i*s->uvlinesize+ 0]) = mean; - *(uint32_t*)(&s->me.scratchpad[i*s->uvlinesize+ 4]) = mean; + *(uint32_t*)(&c->scratchpad[i*s->uvlinesize+ 0]) = mean; + *(uint32_t*)(&c->scratchpad[i*s->uvlinesize+ 4]) = mean; } - - intra_score+= s->dsp.mb_cmp[1](s, s->me.scratchpad, dest_c, s->uvlinesize); - } + + intra_score+= s->dsp.mb_cmp[1](s, c->scratchpad, dest_c, s->uvlinesize); + } } #endif - intra_score += s->me.mb_penalty_factor*16; - + intra_score += c->mb_penalty_factor*16; + if(intra_score < dmin){ mb_type= CANDIDATE_MB_TYPE_INTRA; s->current_picture.mb_type[mb_y*s->mb_stride + mb_x]= CANDIDATE_MB_TYPE_INTRA; //FIXME cleanup }else s->current_picture.mb_type[mb_y*s->mb_stride + mb_x]= 0; - - if (vard <= 64 || vard < varc) { //FIXME - s->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc); - }else{ - s->scene_change_score+= s->qscale; + + { + int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100); + int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20; + c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score); } } @@ -1294,154 +1196,123 @@ void ff_estimate_p_frame_motion(MpegEncContext * s, int ff_pre_estimate_p_frame_motion(MpegEncContext * s, int mb_x, int mb_y) { + MotionEstContext * const c= &s->me; int mx, my, dmin; - int pred_x=0, pred_y=0; int P[10][2]; const int shift= 1+s->quarter_sample; - uint8_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV; const int xy= mb_x + mb_y*s->mb_stride; - const int stride= s->linesize; - const int uvstride= s->uvlinesize; - uint8_t *src_data[3]= { - s->new_picture.data[0] + 16*(mb_x + stride*mb_y), - s->new_picture.data[1] + 8*(mb_x + uvstride*mb_y), - s->new_picture.data[2] + 8*(mb_x + uvstride*mb_y) - }; - uint8_t *ref_data[3]= { - s->last_picture.data[0] + 16*(mb_x + stride*mb_y), - s->last_picture.data[1] + 8*(mb_x + uvstride*mb_y), - s->last_picture.data[2] + 8*(mb_x + uvstride*mb_y) - }; - + init_ref(c, s->new_picture.data, s->last_picture.data, NULL, 16*mb_x, 16*mb_y, 0); + assert(s->quarter_sample==0 || s->quarter_sample==1); - s->me.pre_penalty_factor = get_penalty_factor(s, s->avctx->me_pre_cmp); + c->pre_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_pre_cmp); + c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV; get_limits(s, 16*mb_x, 16*mb_y); - s->me.skip=0; + c->skip=0; P_LEFT[0] = s->p_mv_table[xy + 1][0]; P_LEFT[1] = s->p_mv_table[xy + 1][1]; - if(P_LEFT[0] < (s->me.xmin<me.xmin<xmin<xmin<mb_height-1) { - pred_x= P_LEFT[0]; - pred_y= P_LEFT[1]; + if (s->first_slice_line) { + c->pred_x= P_LEFT[0]; + c->pred_y= P_LEFT[1]; P_TOP[0]= P_TOPRIGHT[0]= P_MEDIAN[0]= - P_TOP[1]= P_TOPRIGHT[1]= P_MEDIAN[1]= 0; //FIXME + P_TOP[1]= P_TOPRIGHT[1]= P_MEDIAN[1]= 0; //FIXME } else { P_TOP[0] = s->p_mv_table[xy + s->mb_stride ][0]; P_TOP[1] = s->p_mv_table[xy + s->mb_stride ][1]; P_TOPRIGHT[0] = s->p_mv_table[xy + s->mb_stride - 1][0]; P_TOPRIGHT[1] = s->p_mv_table[xy + s->mb_stride - 1][1]; - if(P_TOP[1] < (s->me.ymin<me.ymin< (s->me.xmax<me.xmax<me.ymin<me.ymin<ymin<ymin< (c->xmax<xmax<ymin<ymin<pred_x = P_MEDIAN[0]; + c->pred_y = P_MEDIAN[1]; } - dmin = s->me.pre_motion_search(s, &mx, &my, P, pred_x, pred_y, - src_data, ref_data, stride, uvstride, s->p_mv_table, (1<<16)>>shift, mv_penalty); + + dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16); s->p_mv_table[xy][0] = mx<p_mv_table[xy][1] = my<me; int mx, my, dmin; - int pred_x=0, pred_y=0; int P[10][2]; const int shift= 1+s->quarter_sample; const int mot_stride = s->mb_stride; const int mot_xy = mb_y*mot_stride + mb_x; - uint8_t * const ref_picture= ref_data[0] - 16*s->mb_x - 16*s->mb_y*s->linesize; //FIXME ugly - uint8_t * const mv_penalty= s->me.mv_penalty[f_code] + MAX_MV; + uint8_t * const mv_penalty= c->mv_penalty[f_code] + MAX_MV; int mv_scale; - - s->me.penalty_factor = get_penalty_factor(s, s->avctx->me_cmp); - s->me.sub_penalty_factor= get_penalty_factor(s, s->avctx->me_sub_cmp); - s->me.mb_penalty_factor = get_penalty_factor(s, s->avctx->mb_cmp); + + c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp); + c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp); + c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp); + c->current_mv_penalty= mv_penalty; get_limits(s, 16*mb_x, 16*mb_y); switch(s->me_method) { case ME_ZERO: default: - no_motion_search(s, &mx, &my); + no_motion_search(s, &mx, &my); dmin = 0; mx-= mb_x*16; my-= mb_y*16; break; -#if 0 - case ME_FULL: - dmin = full_motion_search(s, &mx, &my, range, ref_picture); - mx-= mb_x*16; - my-= mb_y*16; - break; - case ME_LOG: - dmin = log_motion_search(s, &mx, &my, range / 2, ref_picture); - mx-= mb_x*16; - my-= mb_y*16; - break; - case ME_PHODS: - dmin = phods_motion_search(s, &mx, &my, range / 2, ref_picture); - mx-= mb_x*16; - my-= mb_y*16; - break; -#endif case ME_X1: case ME_EPZS: { P_LEFT[0] = mv_table[mot_xy - 1][0]; P_LEFT[1] = mv_table[mot_xy - 1][1]; - if(P_LEFT[0] > (s->me.xmax<me.xmax< (c->xmax<xmax<first_slice_line) { P_TOP[0] = mv_table[mot_xy - mot_stride ][0]; P_TOP[1] = mv_table[mot_xy - mot_stride ][1]; P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1 ][0]; P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1 ][1]; - if(P_TOP[1] > (s->me.ymax<me.ymax<me.xmin<me.xmin< (s->me.ymax<me.ymax< (c->ymax<ymax<xmin<xmin< (c->ymax<ymax<pred_x= P_LEFT[0]; + c->pred_y= P_LEFT[1]; } - + if(mv_table == s->b_forw_mv_table){ mv_scale= (s->pb_time<<16) / (s->pp_time<pb_time - s->pp_time)<<16) / (s->pp_time<me.motion_search[0](s, &mx, &my, P, pred_x, pred_y, - src_data, ref_data, stride, uvstride, s->p_mv_table, mv_scale, mv_penalty); - + + dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, ref_index, s->p_mv_table, mv_scale, 0, 16); + break; } - - dmin= s->me.sub_motion_search(s, &mx, &my, dmin, - pred_x, pred_y, src_data, ref_data, stride, uvstride, 0, 16, mv_penalty); - - if(s->avctx->me_sub_cmp != s->avctx->mb_cmp && !s->me.skip) - dmin= s->me.get_mb_score(s, mx, my, pred_x, pred_y, src_data, ref_data, stride, uvstride, mv_penalty); + + dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, ref_index, 0, 16); + + if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip) + dmin= ff_get_mb_score(s, mx, my, 0, ref_index, 0, 16, 1); //printf("%d %d %d %d//", s->mb_x, s->mb_y, mx, my); // s->mb_type[mb_y*s->mb_width + mb_x]= mb_type; @@ -1451,8 +1322,7 @@ static int ff_estimate_motion_b(MpegEncContext * s, return dmin; } -static inline int check_bidir_mv(MpegEncContext * s, uint8_t *src_data[3], uint8_t *ref_data[6], - int stride, int uvstride, +static inline int check_bidir_mv(MpegEncContext * s, int motion_fx, int motion_fy, int motion_bx, int motion_by, int pred_fx, int pred_fy, @@ -1460,15 +1330,20 @@ static inline int check_bidir_mv(MpegEncContext * s, uint8_t *src_data[3], uint8 int size, int h) { //FIXME optimize? - //FIXME move into template? //FIXME better f_code prediction (max mv & distance) //FIXME pointers - uint8_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame - uint8_t *dest_y = s->me.scratchpad; + MotionEstContext * const c= &s->me; + uint8_t * const mv_penalty_f= c->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame + uint8_t * const mv_penalty_b= c->mv_penalty[s->b_code] + MAX_MV; // f_code of the prev frame + int stride= c->stride; + uint8_t *dest_y = c->scratchpad; uint8_t *ptr; int dxy; int src_x, src_y; int fbmin; + uint8_t **src_data= c->src[0]; + uint8_t **ref_data= c->ref[0]; + uint8_t **ref2_data= c->ref[2]; if(s->quarter_sample){ dxy = ((motion_fy & 3) << 2) | (motion_fx & 3); @@ -1481,8 +1356,8 @@ static inline int check_bidir_mv(MpegEncContext * s, uint8_t *src_data[3], uint8 dxy = ((motion_by & 3) << 2) | (motion_bx & 3); src_x = motion_bx >> 2; src_y = motion_by >> 2; - - ptr = ref_data[3] + (src_y * stride) + src_x; + + ptr = ref2_data[0] + (src_y * stride) + src_x; s->dsp.avg_qpel_pixels_tab[size][dxy](dest_y , ptr , stride); }else{ dxy = ((motion_fy & 1) << 1) | (motion_fx & 1); @@ -1495,27 +1370,26 @@ static inline int check_bidir_mv(MpegEncContext * s, uint8_t *src_data[3], uint8 dxy = ((motion_by & 1) << 1) | (motion_bx & 1); src_x = motion_bx >> 1; src_y = motion_by >> 1; - - ptr = ref_data[3] + (src_y * stride) + src_x; + + ptr = ref2_data[0] + (src_y * stride) + src_x; s->dsp.avg_pixels_tab[size][dxy](dest_y , ptr , stride, h); } - fbmin = (mv_penalty[motion_fx-pred_fx] + mv_penalty[motion_fy-pred_fy])*s->me.mb_penalty_factor - +(mv_penalty[motion_bx-pred_bx] + mv_penalty[motion_by-pred_by])*s->me.mb_penalty_factor + fbmin = (mv_penalty_f[motion_fx-pred_fx] + mv_penalty_f[motion_fy-pred_fy])*c->mb_penalty_factor + +(mv_penalty_b[motion_bx-pred_bx] + mv_penalty_b[motion_by-pred_by])*c->mb_penalty_factor + s->dsp.mb_cmp[size](s, src_data[0], dest_y, stride, h); //FIXME new_pic - - if(s->avctx->mb_cmp&FF_CMP_CHROMA){ + + if(c->avctx->mb_cmp&FF_CMP_CHROMA){ } //FIXME CHROMA !!! - + return fbmin; } /* refine the bidir vectors in hq mode and return the score in both lq & hq mode*/ -static inline int bidir_refine(MpegEncContext * s, uint8_t *src_data[3], uint8_t *ref_data[6], - int stride, int uvstride, - int mb_x, int mb_y) +static inline int bidir_refine(MpegEncContext * s, int mb_x, int mb_y) { + MotionEstContext * const c= &s->me; const int mot_stride = s->mb_stride; const int xy = mb_y *mot_stride + mb_x; int fbmin; @@ -1527,23 +1401,92 @@ static inline int bidir_refine(MpegEncContext * s, uint8_t *src_data[3], uint8_t int motion_fy= s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1]; int motion_bx= s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0]; int motion_by= s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1]; + const int flags= c->sub_flags; + const int qpel= flags&FLAG_QPEL; + const int shift= 1+qpel; + const int xmin= c->xmin<ymin<xmax<ymax<avctx->bidir_refine){ + int score, end; +#define CHECK_BIDIR(fx,fy,bx,by)\ + if( !BIDIR_MAP(fx,fy,bx,by)\ + &&(fx<=0 || motion_fx+fx<=xmax) && (fy<=0 || motion_fy+fy<=ymax) && (bx<=0 || motion_bx+bx<=xmax) && (by<=0 || motion_by+by<=ymax)\ + &&(fx>=0 || motion_fx+fx>=xmin) && (fy>=0 || motion_fy+fy>=ymin) && (bx>=0 || motion_bx+bx>=xmin) && (by>=0 || motion_by+by>=ymin)){\ + BIDIR_MAP(fx,fy,bx,by) = 1;\ + score= check_bidir_mv(s, motion_fx+fx, motion_fy+fy, motion_bx+bx, motion_by+by, pred_fx, pred_fy, pred_bx, pred_by, 0, 16);\ + if(score < fbmin){\ + fbmin= score;\ + motion_fx+=fx;\ + motion_fy+=fy;\ + motion_bx+=bx;\ + motion_by+=by;\ + end=0;\ + }\ + } +#define CHECK_BIDIR2(a,b,c,d)\ +CHECK_BIDIR(a,b,c,d)\ +CHECK_BIDIR(-(a),-(b),-(c),-(d)) + +#define CHECK_BIDIRR(a,b,c,d)\ +CHECK_BIDIR2(a,b,c,d)\ +CHECK_BIDIR2(b,c,d,a)\ +CHECK_BIDIR2(c,d,a,b)\ +CHECK_BIDIR2(d,a,b,c) + + do{ + end=1; + + CHECK_BIDIRR( 0, 0, 0, 1) + if(s->avctx->bidir_refine > 1){ + CHECK_BIDIRR( 0, 0, 1, 1) + CHECK_BIDIR2( 0, 1, 0, 1) + CHECK_BIDIR2( 1, 0, 1, 0) + CHECK_BIDIRR( 0, 0,-1, 1) + CHECK_BIDIR2( 0,-1, 0, 1) + CHECK_BIDIR2(-1, 0, 1, 0) + if(s->avctx->bidir_refine > 2){ + CHECK_BIDIRR( 0, 1, 1, 1) + CHECK_BIDIRR( 0,-1, 1, 1) + CHECK_BIDIRR( 0, 1,-1, 1) + CHECK_BIDIRR( 0, 1, 1,-1) + if(s->avctx->bidir_refine > 3){ + CHECK_BIDIR2( 1, 1, 1, 1) + CHECK_BIDIRR( 1, 1, 1,-1) + CHECK_BIDIR2( 1, 1,-1,-1) + CHECK_BIDIR2( 1,-1,-1, 1) + CHECK_BIDIR2( 1,-1, 1,-1) + } + } + } + }while(!end); + } + + s->b_bidir_forw_mv_table[xy][0]= motion_fx; + s->b_bidir_forw_mv_table[xy][1]= motion_fy; + s->b_bidir_back_mv_table[xy][0]= motion_bx; + s->b_bidir_back_mv_table[xy][1]= motion_by; + + return fbmin; } -static inline int direct_search(MpegEncContext * s, uint8_t *src_data[3], uint8_t *ref_data[6], - int stride, int uvstride, - int mb_x, int mb_y) +static inline int direct_search(MpegEncContext * s, int mb_x, int mb_y) { + MotionEstContext * const c= &s->me; int P[10][2]; const int mot_stride = s->mb_stride; const int mot_xy = mb_y*mot_stride + mb_x; @@ -1553,8 +1496,8 @@ static inline int direct_search(MpegEncContext * s, uint8_t *src_data[3], uint8_ const int time_pb= s->pb_time; int mx, my, xmin, xmax, ymin, ymax; int16_t (*mv_table)[2]= s->b_direct_mv_table; - uint8_t * const mv_penalty= s->me.mv_penalty[1] + MAX_MV; - + + c->current_mv_penalty= c->mv_penalty[1] + MAX_MV; ymin= xmin=(-32)>>shift; ymax= xmax= 31>>shift; @@ -1567,151 +1510,199 @@ static inline int direct_search(MpegEncContext * s, uint8_t *src_data[3], uint8_ for(i=0; i<4; i++){ int index= s->block_index[i]; int min, max; - - s->me.co_located_mv[i][0]= s->next_picture.motion_val[0][index][0]; - s->me.co_located_mv[i][1]= s->next_picture.motion_val[0][index][1]; - s->me.direct_basis_mv[i][0]= s->me.co_located_mv[i][0]*time_pb/time_pp + ((i& 1)<<(shift+3)); - s->me.direct_basis_mv[i][1]= s->me.co_located_mv[i][1]*time_pb/time_pp + ((i>>1)<<(shift+3)); -// s->me.direct_basis_mv[1][i][0]= s->me.co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(shift+3); -// s->me.direct_basis_mv[1][i][1]= s->me.co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(shift+3); - max= FFMAX(s->me.direct_basis_mv[i][0], s->me.direct_basis_mv[i][0] - s->me.co_located_mv[i][0])>>shift; - min= FFMIN(s->me.direct_basis_mv[i][0], s->me.direct_basis_mv[i][0] - s->me.co_located_mv[i][0])>>shift; + c->co_located_mv[i][0]= s->next_picture.motion_val[0][index][0]; + c->co_located_mv[i][1]= s->next_picture.motion_val[0][index][1]; + c->direct_basis_mv[i][0]= c->co_located_mv[i][0]*time_pb/time_pp + ((i& 1)<<(shift+3)); + c->direct_basis_mv[i][1]= c->co_located_mv[i][1]*time_pb/time_pp + ((i>>1)<<(shift+3)); +// c->direct_basis_mv[1][i][0]= c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(shift+3); +// c->direct_basis_mv[1][i][1]= c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(shift+3); + + max= FFMAX(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift; + min= FFMIN(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift; max+= 16*mb_x + 1; // +-1 is for the simpler rounding min+= 16*mb_x - 1; xmax= FFMIN(xmax, s->width - max); xmin= FFMAX(xmin, - 16 - min); - max= FFMAX(s->me.direct_basis_mv[i][1], s->me.direct_basis_mv[i][1] - s->me.co_located_mv[i][1])>>shift; - min= FFMIN(s->me.direct_basis_mv[i][1], s->me.direct_basis_mv[i][1] - s->me.co_located_mv[i][1])>>shift; + max= FFMAX(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift; + min= FFMIN(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift; max+= 16*mb_y + 1; // +-1 is for the simpler rounding min+= 16*mb_y - 1; ymax= FFMIN(ymax, s->height - max); ymin= FFMAX(ymin, - 16 - min); - + if(s->mv_type == MV_TYPE_16X16) break; } - + assert(xmax <= 15 && ymax <= 15 && xmin >= -16 && ymin >= -16); - + if(xmax < 0 || xmin >0 || ymax < 0 || ymin > 0){ s->b_direct_mv_table[mot_xy][0]= 0; s->b_direct_mv_table[mot_xy][1]= 0; return 256*256*256*64; } - - s->me.xmin= xmin; - s->me.ymin= ymin; - s->me.xmax= xmax; - s->me.ymax= ymax; - P_LEFT[0] = clip(mv_table[mot_xy - 1][0], xmin<xmin= xmin; + c->ymin= ymin; + c->xmax= xmax; + c->ymax= ymax; + c->flags |= FLAG_DIRECT; + c->sub_flags |= FLAG_DIRECT; + c->pred_x=0; + c->pred_y=0; + + P_LEFT[0] = av_clip(mv_table[mot_xy - 1][0], xmin<first_slice_line) { //FIXME maybe allow this over thread boundary as it is clipped + P_TOP[0] = av_clip(mv_table[mot_xy - mot_stride ][0], xmin<flags&CODEC_FLAG_QPEL){ - dmin = simple_direct_qpel_epzs_motion_search(s, &mx, &my, P, 0, 0, - src_data, ref_data, stride, uvstride, mv_table, 1<<14, mv_penalty); - dmin = simple_direct_qpel_qpel_motion_search(s, &mx, &my, dmin, - 0, 0, src_data, ref_data, stride, uvstride, 0, 16, mv_penalty); - - if(s->avctx->me_sub_cmp != s->avctx->mb_cmp && !s->me.skip) - dmin= simple_direct_qpel_qpel_get_mb_score(s, mx, my, 0, 0, src_data, ref_data, stride, uvstride, mv_penalty); - }else{ - dmin = simple_direct_hpel_epzs_motion_search(s, &mx, &my, P, 0, 0, - src_data, ref_data, stride, uvstride, mv_table, 1<<15, mv_penalty); - dmin = simple_direct_hpel_hpel_motion_search(s, &mx, &my, dmin, - 0, 0, src_data, ref_data, stride, uvstride, 0, 16, mv_penalty); - if(s->avctx->me_sub_cmp != s->avctx->mb_cmp && !s->me.skip) - dmin= simple_direct_hpel_hpel_get_mb_score(s, mx, my, 0, 0, src_data, ref_data, stride, uvstride, mv_penalty); - } - - get_limits(s, 16*mb_x, 16*mb_y); //restore s->me.?min/max, maybe not needed + dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, mv_table, 1<<(16-shift), 0, 16); + if(c->sub_flags&FLAG_QPEL) + dmin = qpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16); + else + dmin = hpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16); + + if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip) + dmin= ff_get_mb_score(s, mx, my, 0, 0, 0, 16, 1); + + get_limits(s, 16*mb_x, 16*mb_y); //restore c->?min/max, maybe not needed + + mv_table[mot_xy][0]= mx; + mv_table[mot_xy][1]= my; + c->flags &= ~FLAG_DIRECT; + c->sub_flags &= ~FLAG_DIRECT; - s->b_direct_mv_table[mot_xy][0]= mx; - s->b_direct_mv_table[mot_xy][1]= my; return dmin; } void ff_estimate_b_frame_motion(MpegEncContext * s, int mb_x, int mb_y) { - const int penalty_factor= s->me.mb_penalty_factor; + MotionEstContext * const c= &s->me; + const int penalty_factor= c->mb_penalty_factor; int fmin, bmin, dmin, fbmin, bimin, fimin; int type=0; - const int stride= s->linesize; - const int uvstride= s->uvlinesize; - uint8_t *src_data[3]= { - s->new_picture.data[0] + 16*(s->mb_x + stride*s->mb_y), - s->new_picture.data[1] + 8*(s->mb_x + uvstride*s->mb_y), - s->new_picture.data[2] + 8*(s->mb_x + uvstride*s->mb_y) - }; - uint8_t *ref_data[6]= { - s->last_picture.data[0] + 16*(s->mb_x + stride*s->mb_y), - s->last_picture.data[1] + 8*(s->mb_x + uvstride*s->mb_y), - s->last_picture.data[2] + 8*(s->mb_x + uvstride*s->mb_y), - s->next_picture.data[0] + 16*(s->mb_x + stride*s->mb_y), - s->next_picture.data[1] + 8*(s->mb_x + uvstride*s->mb_y), - s->next_picture.data[2] + 8*(s->mb_x + uvstride*s->mb_y) - }; - - s->me.skip=0; + const int xy = mb_y*s->mb_stride + mb_x; + init_ref(c, s->new_picture.data, s->last_picture.data, s->next_picture.data, 16*mb_x, 16*mb_y, 2); + + get_limits(s, 16*mb_x, 16*mb_y); + + c->skip=0; + + if(s->codec_id == CODEC_ID_MPEG4 && s->next_picture.mbskip_table[xy]){ + int score= direct_search(s, mb_x, mb_y); //FIXME just check 0,0 + + score= ((unsigned)(score*score + 128*256))>>16; + c->mc_mb_var_sum_temp += score; + s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE + s->mb_type[mb_y*s->mb_stride + mb_x]= CANDIDATE_MB_TYPE_DIRECT0; + + return; + } + + if(c->avctx->me_threshold){ + int vard= check_input_motion(s, mb_x, mb_y, 0); + + if((vard+128)>>8 < c->avctx->me_threshold){ +// pix = c->src[0][0]; +// sum = s->dsp.pix_sum(pix, s->linesize); +// varc = s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500; + +// pic->mb_var [s->mb_stride * mb_y + mb_x] = (varc+128)>>8; + s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8; +/* pic->mb_mean [s->mb_stride * mb_y + mb_x] = (sum+128)>>8; + c->mb_var_sum_temp += (varc+128)>>8;*/ + c->mc_mb_var_sum_temp += (vard+128)>>8; +/* if (vard <= 64<<8 || vard < varc) { + c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc); + }else{ + c->scene_change_score+= s->qscale * s->avctx->scenechange_factor; + }*/ + return; + } + if((vard+128)>>8 < c->avctx->mb_threshold){ + type= s->mb_type[mb_y*s->mb_stride + mb_x]; + if(type == CANDIDATE_MB_TYPE_DIRECT){ + direct_search(s, mb_x, mb_y); + } + if(type == CANDIDATE_MB_TYPE_FORWARD || type == CANDIDATE_MB_TYPE_BIDIR){ + c->skip=0; + ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code); + } + if(type == CANDIDATE_MB_TYPE_BACKWARD || type == CANDIDATE_MB_TYPE_BIDIR){ + c->skip=0; + ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code); + } + if(type == CANDIDATE_MB_TYPE_FORWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){ + c->skip=0; + c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV; + interlaced_search(s, 0, + s->b_field_mv_table[0], s->b_field_select_table[0], + s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 1); + } + if(type == CANDIDATE_MB_TYPE_BACKWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){ + c->skip=0; + c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV; + interlaced_search(s, 2, + s->b_field_mv_table[1], s->b_field_select_table[1], + s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 1); + } + return; + } + } + if (s->codec_id == CODEC_ID_MPEG4) - dmin= direct_search(s, src_data, ref_data, stride, uvstride, mb_x, mb_y); + dmin= direct_search(s, mb_x, mb_y); else dmin= INT_MAX; //FIXME penalty stuff for non mpeg4 - s->me.skip=0; - fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, src_data, - ref_data, stride, uvstride, s->f_code) + 3*penalty_factor; - - s->me.skip=0; - bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, src_data, - ref_data+3, stride, uvstride, s->b_code) + 2*penalty_factor; + c->skip=0; + fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code) + 3*penalty_factor; + + c->skip=0; + bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code) + 2*penalty_factor; //printf(" %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]); - s->me.skip=0; - fbmin= bidir_refine(s, src_data, ref_data, stride, uvstride, mb_x, mb_y) + penalty_factor; + c->skip=0; + fbmin= bidir_refine(s, mb_x, mb_y) + penalty_factor; //printf("%d %d %d %d\n", dmin, fmin, bmin, fbmin); - - if(s->flags & CODEC_FLAG_INTERLACED_ME){ - const int xy = mb_y*s->mb_stride + mb_x; + if(s->flags & CODEC_FLAG_INTERLACED_ME){ //FIXME mb type penalty - s->me.skip=0; - fimin= interlaced_search(s, src_data, ref_data , - s->b_field_mv_table[0], s->b_field_select_table[0], s->f_code, - s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]); - bimin= interlaced_search(s, src_data, ref_data+3, - s->b_field_mv_table[1], s->b_field_select_table[1], s->b_code, - s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1]); + c->skip=0; + c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV; + fimin= interlaced_search(s, 0, + s->b_field_mv_table[0], s->b_field_select_table[0], + s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 0); + c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV; + bimin= interlaced_search(s, 2, + s->b_field_mv_table[1], s->b_field_select_table[1], + s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 0); }else fimin= bimin= INT_MAX; { int score= fmin; type = CANDIDATE_MB_TYPE_FORWARD; - + if (dmin <= score){ score = dmin; type = CANDIDATE_MB_TYPE_DIRECT; } if(bmin>16; - s->current_picture.mc_mb_var_sum += score; + c->mc_mb_var_sum_temp += score; s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE } - if(s->avctx->mb_decision > FF_MB_DECISION_SIMPLE){ + if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){ type= CANDIDATE_MB_TYPE_FORWARD | CANDIDATE_MB_TYPE_BACKWARD | CANDIDATE_MB_TYPE_BIDIR | CANDIDATE_MB_TYPE_DIRECT; if(fimin < INT_MAX) type |= CANDIDATE_MB_TYPE_FORWARD_I; @@ -1741,8 +1732,10 @@ void ff_estimate_b_frame_motion(MpegEncContext * s, type |= CANDIDATE_MB_TYPE_BIDIR_I; } //FIXME something smarter - if(dmin>256*256*16) type&= ~CANDIDATE_MB_TYPE_DIRECT; //dont try direct mode if its invalid for this MB -#if 0 + if(dmin>256*256*16) type&= ~CANDIDATE_MB_TYPE_DIRECT; //do not try direct mode if it is invalid for this MB + if(s->codec_id == CODEC_ID_MPEG4 && type&CANDIDATE_MB_TYPE_DIRECT && s->flags&CODEC_FLAG_MV0 && *(uint32_t*)s->b_direct_mv_table[xy]) + type |= CANDIDATE_MB_TYPE_DIRECT0; +#if 0 if(s->out_format == FMT_MPEG1) type |= CANDIDATE_MB_TYPE_INTRA; #endif @@ -1756,11 +1749,16 @@ int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type) { if(s->me_method>=ME_EPZS){ int score[8]; - int i, y; + int i, y, range= s->avctx->me_range ? s->avctx->me_range : (INT_MAX/2); uint8_t * fcode_tab= s->fcode_tab; int best_fcode=-1; int best_score=-10000000; + if(s->msmpeg4_version) + range= FFMIN(range, 16); + else if(s->codec_id == CODEC_ID_MPEG2VIDEO && s->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL) + range= FFMIN(range, 256); + for(i=0; i<8; i++) score[i]= s->mb_num*(8-i); for(y=0; ymb_height; y++){ @@ -1768,19 +1766,25 @@ int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type) int xy= y*s->mb_stride; for(x=0; xmb_width; x++){ if(s->mb_type[xy] & type){ - int fcode= FFMAX(fcode_tab[mv_table[xy][0] + MAX_MV], - fcode_tab[mv_table[xy][1] + MAX_MV]); + int mx= mv_table[xy][0]; + int my= mv_table[xy][1]; + int fcode= FFMAX(fcode_tab[mx + MAX_MV], + fcode_tab[my + MAX_MV]); int j; - + + if(mx >= range || mx < -range || + my >= range || my < -range) + continue; + for(j=0; jpict_type==B_TYPE || s->current_picture.mc_mb_var[xy] < s->current_picture.mb_var[xy]) + if(s->pict_type==FF_B_TYPE || s->current_picture.mc_mb_var[xy] < s->current_picture.mb_var[xy]) score[j]-= 170; } } xy++; } } - + for(i=1; i<8; i++){ if(score[i] > best_score){ best_score= score[i]; @@ -1802,23 +1806,25 @@ int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type) void ff_fix_long_p_mvs(MpegEncContext * s) { + MotionEstContext * const c= &s->me; const int f_code= s->f_code; int y, range; - assert(s->pict_type==P_TYPE); + assert(s->pict_type==FF_P_TYPE); + + range = (((s->out_format == FMT_MPEG1 || s->msmpeg4_version) ? 8 : 16) << f_code); + + assert(range <= 16 || !s->msmpeg4_version); + assert(range <=256 || !(s->codec_id == CODEC_ID_MPEG2VIDEO && s->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL)); + + if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range; - range = (((s->out_format == FMT_MPEG1) ? 8 : 16) << f_code); - - if(s->msmpeg4_version) range= 16; - - if(s->avctx->me_range && range > s->avctx->me_range) range= s->avctx->me_range; - //printf("%d no:%d %d//\n", clip, noclip, f_code); if(s->flags&CODEC_FLAG_4MV){ - const int wrap= 2+ s->mb_width*2; + const int wrap= s->b8_stride; /* clip / convert to intra 8x8 type MVs */ for(y=0; ymb_height; y++){ - int xy= (y*2 + 1)*wrap + 1; + int xy= y*2*wrap; int i= y*s->mb_stride; int x; @@ -1849,16 +1855,16 @@ void ff_fix_long_p_mvs(MpegEncContext * s) * * @param truncate 1 for truncation, 0 for using intra */ -void ff_fix_long_mvs(MpegEncContext * s, uint8_t *field_select_table, int field_select, +void ff_fix_long_mvs(MpegEncContext * s, uint8_t *field_select_table, int field_select, int16_t (*mv_table)[2], int f_code, int type, int truncate) { + MotionEstContext * const c= &s->me; int y, h_range, v_range; // RAL: 8 in MPEG-1, 16 in MPEG-4 - int range = (((s->out_format == FMT_MPEG1) ? 8 : 16) << f_code); + int range = (((s->out_format == FMT_MPEG1 || s->msmpeg4_version) ? 8 : 16) << f_code); - if(s->msmpeg4_version) range= 16; - if(s->avctx->me_range && range > s->avctx->me_range) range= s->avctx->me_range; + if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range; h_range= range; v_range= field_select_table ? range>>1 : range; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/motion_est_template.c b/src/add-ons/media/plugins/avcodec/libavcodec/motion_est_template.c index 18203ec065..05c276412c 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/motion_est_template.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/motion_est_template.c @@ -1,88 +1,63 @@ /* - * Motion estimation + * Motion estimation * Copyright (c) 2002-2004 Michael Niedermayer * - * This library is free software; you can redistribute it and/or + * This file is part of FFmpeg. + * + * FFmpeg 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. + * version 2.1 of the License, or (at your option) any later version. * - * This library is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ - + /** * @file motion_est_template.c * Motion estimation template. */ -//FIXME ref2_y next_pic? -//lets hope gcc will remove the unused vars ...(gcc 3.2.2 seems to do it ...) -//Note, the last line is there to kill these ugly unused var warnings + +//Let us hope gcc will remove the unused vars ...(gcc 3.2.2 seems to do it ...) #define LOAD_COMMON\ - uint32_t * const score_map= s->me.score_map;\ - const int time_pp= s->pp_time;\ - const int time_pb= s->pb_time;\ - const int xmin= s->me.xmin;\ - const int ymin= s->me.ymin;\ - const int xmax= s->me.xmax;\ - const int ymax= s->me.ymax;\ - uint8_t * const src_y= src_data[0];\ - uint8_t * const src_u= src_data[1];\ - uint8_t * const src_v= src_data[2];\ - uint8_t * const ref_y= ref_data[0];\ - uint8_t * const ref_u= ref_data[1];\ - uint8_t * const ref_v= ref_data[2];\ - op_pixels_func (*hpel_put)[4];\ - op_pixels_func (*hpel_avg)[4]= &s->dsp.avg_pixels_tab[size];\ - op_pixels_func (*chroma_hpel_put)[4];\ - qpel_mc_func (*qpel_put)[16];\ - qpel_mc_func (*qpel_avg)[16]= &s->dsp.avg_qpel_pixels_tab[size];\ - const __attribute__((unused)) int unu= time_pp + time_pb + (size_t)src_u + (size_t)src_v + (size_t)ref_u + (size_t)ref_v\ - + (size_t)hpel_avg + (size_t)qpel_avg + (size_t)score_map\ - + xmin + xmax + ymin + ymax;\ - if(s->no_rounding /*FIXME b_type*/){\ - hpel_put= &s->dsp.put_no_rnd_pixels_tab[size];\ - chroma_hpel_put= &s->dsp.put_no_rnd_pixels_tab[size+1];\ - qpel_put= &s->dsp.put_no_rnd_qpel_pixels_tab[size];\ - }else{\ - hpel_put=& s->dsp.put_pixels_tab[size];\ - chroma_hpel_put= &s->dsp.put_pixels_tab[size+1];\ - qpel_put= &s->dsp.put_qpel_pixels_tab[size];\ - } + uint32_t av_unused * const score_map= c->score_map;\ + const int av_unused xmin= c->xmin;\ + const int av_unused ymin= c->ymin;\ + const int av_unused xmax= c->xmax;\ + const int av_unused ymax= c->ymax;\ + uint8_t *mv_penalty= c->current_mv_penalty;\ + const int pred_x= c->pred_x;\ + const int pred_y= c->pred_y;\ - -#ifdef CMP_HPEL - #define CHECK_HALF_MV(dx, dy, x, y)\ {\ const int hx= 2*(x)+(dx);\ const int hy= 2*(y)+(dy);\ - CMP_HPEL(d, dx, dy, x, y, size);\ + d= cmp(s, x, y, dx, dy, size, h, ref_index, src_index, cmp_sub, chroma_cmp_sub, flags);\ d += (mv_penalty[hx - pred_x] + mv_penalty[hy - pred_y])*penalty_factor;\ COPY3_IF_LT(dmin, d, bx, hx, by, hy)\ } #if 0 -static int RENAME(hpel_motion_search)(MpegEncContext * s, - int *mx_ptr, int *my_ptr, int dmin, - int pred_x, int pred_y, uint8_t *ref_data[3], - int size, uint8_t * const mv_penalty) +static int hpel_motion_search)(MpegEncContext * s, + int *mx_ptr, int *my_ptr, int dmin, + uint8_t *ref_data[3], + int size) { const int xx = 16 * s->mb_x + 8*(n&1); const int yy = 16 * s->mb_y + 8*(n>>1); const int mx = *mx_ptr; const int my = *my_ptr; - const int penalty_factor= s->me.sub_penalty_factor; - + const int penalty_factor= c->sub_penalty_factor; + LOAD_COMMON - + // INIT; //FIXME factorize me_cmp_func cmp, chroma_cmp, cmp_sub, chroma_cmp_sub; @@ -94,35 +69,35 @@ static int RENAME(hpel_motion_search)(MpegEncContext * s, hpel_put=& s->dsp.put_pixels_tab[size]; chroma_hpel_put= &s->dsp.put_pixels_tab[size+1]; } - cmp= s->dsp.me_cmp[size]; - chroma_cmp= s->dsp.me_cmp[size+1]; + cmpf= s->dsp.me_cmp[size]; + chroma_cmpf= s->dsp.me_cmp[size+1]; cmp_sub= s->dsp.me_sub_cmp[size]; chroma_cmp_sub= s->dsp.me_sub_cmp[size+1]; - if(s->me.skip){ //FIXME somehow move up (benchmark) + if(c->skip){ //FIXME somehow move up (benchmark) *mx_ptr = 0; *my_ptr = 0; return dmin; } - - if(s->avctx->me_cmp != s->avctx->me_sub_cmp){ + + if(c->avctx->me_cmp != c->avctx->me_sub_cmp){ CMP_HPEL(dmin, 0, 0, mx, my, size); if(mx || my) dmin += (mv_penalty[2*mx - pred_x] + mv_penalty[2*my - pred_y])*penalty_factor; } - - if (mx > xmin && mx < xmax && + + if (mx > xmin && mx < xmax && my > ymin && my < ymax) { int bx=2*mx, by=2*my; int d= dmin; - + CHECK_HALF_MV(1, 1, mx-1, my-1) - CHECK_HALF_MV(0, 1, mx , my-1) + CHECK_HALF_MV(0, 1, mx , my-1) CHECK_HALF_MV(1, 1, mx , my-1) CHECK_HALF_MV(1, 0, mx-1, my ) CHECK_HALF_MV(1, 0, mx , my ) CHECK_HALF_MV(1, 1, mx-1, my ) - CHECK_HALF_MV(0, 1, mx , my ) + CHECK_HALF_MV(0, 1, mx , my ) CHECK_HALF_MV(1, 1, mx , my ) assert(bx >= xmin*2 || bx <= xmax*2 || by >= ymin*2 || by <= ymax*2); @@ -138,55 +113,56 @@ static int RENAME(hpel_motion_search)(MpegEncContext * s, } #else -static int RENAME(hpel_motion_search)(MpegEncContext * s, - int *mx_ptr, int *my_ptr, int dmin, - int pred_x, int pred_y, uint8_t *src_data[3], - uint8_t *ref_data[3], int stride, int uvstride, - int size, int h, uint8_t * const mv_penalty) +static int hpel_motion_search(MpegEncContext * s, + int *mx_ptr, int *my_ptr, int dmin, + int src_index, int ref_index, + int size, int h) { + MotionEstContext * const c= &s->me; const int mx = *mx_ptr; - const int my = *my_ptr; - const int penalty_factor= s->me.sub_penalty_factor; + const int my = *my_ptr; + const int penalty_factor= c->sub_penalty_factor; me_cmp_func cmp_sub, chroma_cmp_sub; int bx=2*mx, by=2*my; LOAD_COMMON - + int flags= c->sub_flags; + //FIXME factorize cmp_sub= s->dsp.me_sub_cmp[size]; chroma_cmp_sub= s->dsp.me_sub_cmp[size+1]; - if(s->me.skip){ //FIXME move out of hpel? + if(c->skip){ //FIXME move out of hpel? *mx_ptr = 0; *my_ptr = 0; return dmin; } - - if(s->avctx->me_cmp != s->avctx->me_sub_cmp){ - CMP_HPEL(dmin, 0, 0, mx, my, size); + + if(c->avctx->me_cmp != c->avctx->me_sub_cmp){ + dmin= cmp(s, mx, my, 0, 0, size, h, ref_index, src_index, cmp_sub, chroma_cmp_sub, flags); if(mx || my || size>0) dmin += (mv_penalty[2*mx - pred_x] + mv_penalty[2*my - pred_y])*penalty_factor; } - - if (mx > xmin && mx < xmax && + + if (mx > xmin && mx < xmax && my > ymin && my < ymax) { int d= dmin; const int index= (my<me.penalty_factor; + const int t= score_map[(index-(1<penalty_factor; const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)] - + (mv_penalty[bx-2 - pred_x] + mv_penalty[by - pred_y])*s->me.penalty_factor; + + (mv_penalty[bx-2 - pred_x] + mv_penalty[by - pred_y])*c->penalty_factor; const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)] - + (mv_penalty[bx+2 - pred_x] + mv_penalty[by - pred_y])*s->me.penalty_factor; + + (mv_penalty[bx+2 - pred_x] + mv_penalty[by - pred_y])*c->penalty_factor; const int b= score_map[(index+(1<me.penalty_factor; - + + (mv_penalty[bx - pred_x] + mv_penalty[by+2 - pred_y])*c->penalty_factor; + #if 1 int key; - int map_generation= s->me.map_generation; + int map_generation= c->map_generation; #ifndef NDEBUG - uint32_t *map= s->me.map; + uint32_t *map= c->map; #endif key= ((my-1)<dsp.me_sub_cmp != s->dsp.mb_cmp; - const int size= 0; - const int h= 16; - const int penalty_factor= s->me.mb_penalty_factor; + MotionEstContext * const c= &s->me; + const int penalty_factor= c->mb_penalty_factor; + const int flags= c->mb_flags; + const int qpel= flags & FLAG_QPEL; + const int mask= 1+2*qpel; me_cmp_func cmp_sub, chroma_cmp_sub; int d; LOAD_COMMON - + //FIXME factorize cmp_sub= s->dsp.mb_cmp[size]; chroma_cmp_sub= s->dsp.mb_cmp[size+1]; - - assert(!s->me.skip); - assert(s->avctx->me_sub_cmp != s->avctx->mb_cmp); - CMP_HPEL(d, mx&1, my&1, mx>>1, my>>1, size); +// assert(!c->skip); +// assert(c->avctx->me_sub_cmp != c->avctx->mb_cmp); + + d= cmp(s, mx>>(qpel+1), my>>(qpel+1), mx&mask, my&mask, size, h, ref_index, src_index, cmp_sub, chroma_cmp_sub, flags); //FIXME check cbp before adding penalty for (0,0) vector - if(mx || my || size>0) + if(add_rate && (mx || my || size>0)) d += (mv_penalty[mx - pred_x] + mv_penalty[my - pred_y])*penalty_factor; - + return d; } -#endif /* CMP_HPEL */ - - - -#ifdef CMP_QPEL - #define CHECK_QUARTER_MV(dx, dy, x, y)\ {\ const int hx= 4*(x)+(dx);\ const int hy= 4*(y)+(dy);\ - CMP_QPEL(d, dx, dy, x, y, size);\ + d= cmp(s, x, y, dx, dy, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\ d += (mv_penalty[hx - pred_x] + mv_penalty[hy - pred_y])*penalty_factor;\ COPY3_IF_LT(dmin, d, bx, hx, by, hy)\ } -static int RENAME(qpel_motion_search)(MpegEncContext * s, - int *mx_ptr, int *my_ptr, int dmin, - int pred_x, int pred_y, uint8_t *src_data[3], - uint8_t *ref_data[3], int stride, int uvstride, - int size, int h, uint8_t * const mv_penalty) +static int qpel_motion_search(MpegEncContext * s, + int *mx_ptr, int *my_ptr, int dmin, + int src_index, int ref_index, + int size, int h) { + MotionEstContext * const c= &s->me; const int mx = *mx_ptr; - const int my = *my_ptr; - const int penalty_factor= s->me.sub_penalty_factor; - const int map_generation= s->me.map_generation; - const int subpel_quality= s->avctx->me_subpel_quality; - uint32_t *map= s->me.map; - me_cmp_func cmp, chroma_cmp; + const int my = *my_ptr; + const int penalty_factor= c->sub_penalty_factor; + const int map_generation= c->map_generation; + const int subpel_quality= c->avctx->me_subpel_quality; + uint32_t *map= c->map; + me_cmp_func cmpf, chroma_cmpf; me_cmp_func cmp_sub, chroma_cmp_sub; LOAD_COMMON - - cmp= s->dsp.me_cmp[size]; - chroma_cmp= s->dsp.me_cmp[size+1]; //factorize FIXME + int flags= c->sub_flags; + + cmpf= s->dsp.me_cmp[size]; + chroma_cmpf= s->dsp.me_cmp[size+1]; //factorize FIXME //FIXME factorize cmp_sub= s->dsp.me_sub_cmp[size]; chroma_cmp_sub= s->dsp.me_sub_cmp[size+1]; - if(s->me.skip){ //FIXME somehow move up (benchmark) + if(c->skip){ //FIXME somehow move up (benchmark) *mx_ptr = 0; *my_ptr = 0; return dmin; } - - if(s->avctx->me_cmp != s->avctx->me_sub_cmp){ - CMP_QPEL(dmin, 0, 0, mx, my, size); + + if(c->avctx->me_cmp != c->avctx->me_sub_cmp){ + dmin= cmp(s, mx, my, 0, 0, size, h, ref_index, src_index, cmp_sub, chroma_cmp_sub, flags); if(mx || my || size>0) dmin += (mv_penalty[4*mx - pred_x] + mv_penalty[4*my - pred_y])*penalty_factor; } - - if (mx > xmin && mx < xmax && + + if (mx > xmin && mx < xmax && my > ymin && my < ymax) { int bx=4*mx, by=4*my; int d= dmin; @@ -339,10 +321,10 @@ static int RENAME(qpel_motion_search)(MpegEncContext * s, const int c= score_map[(index )&(ME_MAP_SIZE-1)]; int best[8]; int best_pos[8][2]; - + memset(best, 64, sizeof(int)*8); #if 1 - if(s->me.dia_size>=2){ + if(s->me.dia_size>=2){ const int tl= score_map[(index-(1<>10; int i; - + if((nx&3)==0 && (ny&3)==0) continue; - - score += 1024*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor; - -// if(nx&1) score-=1024*s->me.penalty_factor; -// if(ny&1) score-=1024*s->me.penalty_factor; - + + score += (mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor; + +// if(nx&1) score-=1024*c->penalty_factor; +// if(ny&1) score-=1024*c->penalty_factor; + for(i=0; i<8; i++){ if(score < best[i]){ memmove(&best[i+1], &best[i], sizeof(int)*(7-i)); @@ -377,37 +360,39 @@ static int RENAME(qpel_motion_search)(MpegEncContext * s, } }else{ int tl; + //FIXME this could overflow (unlikely though) const int cx = 4*(r - l); - const int cx2= r + l - 2*c; + const int cx2= r + l - 2*c; const int cy = 4*(b - t); const int cy2= b + t - 2*c; int cxy; - + if(map[(index-(1<me.penalty_factor; - // if(ny&1) score-=32*s->me.penalty_factor; - +// if(nx&1) score-=32*c->penalty_factor; + // if(ny&1) score-=32*c->penalty_factor; + for(i=0; i<8; i++){ if(score < best[i]){ memmove(&best[i+1], &best[i], sizeof(int)*(7-i)); @@ -419,7 +404,7 @@ static int RENAME(qpel_motion_search)(MpegEncContext * s, } } } - } + } } for(i=0; ime.mb_penalty_factor; - me_cmp_func cmp_sub, chroma_cmp_sub; - int d; - - LOAD_COMMON - - //FIXME factorize - - cmp_sub= s->dsp.mb_cmp[size]; - chroma_cmp_sub= s->dsp.mb_cmp[size+1]; - - assert(!s->me.skip); - assert(s->avctx->me_sub_cmp != s->avctx->mb_cmp); - - CMP_QPEL(d, mx&3, my&3, mx>>2, my>>2, size); - //FIXME check cbp before adding penalty for (0,0) vector - if(mx || my || size>0) - d += (mv_penalty[mx - pred_x] + mv_penalty[my - pred_y])*penalty_factor; - - return d; -} - - -#endif /* CMP_QPEL */ #define CHECK_MV(x,y)\ {\ const int key= ((y)<= xmin);\ + assert((x) <= xmax);\ + assert((y) >= ymin);\ + assert((y) <= ymax);\ /*printf("check_mv %d %d\n", x, y);*/\ if(map[index]!=key){\ - CMP(d, x, y, size);\ + d= cmp(s, x, y, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\ map[index]= key;\ score_map[index]= d;\ d += (mv_penalty[((x)<(xmax<<(S)) ) printf("%d %d %d %d %d xmax" #v, xmax, (x), (y), s->mb_x, if( (y)<(ymin<<(S)) ) printf("%d %d %d %d %d ymin" #v, ymin, (x), (y), s->mb_x, s->mb_y);\ if( (y)>(ymax<<(S)) ) printf("%d %d %d %d %d ymax" #v, ymax, (x), (y), s->mb_x, s->mb_y);\ +#define LOAD_COMMON2\ + uint32_t *map= c->map;\ + const int qpel= flags&FLAG_QPEL;\ + const int shift= 1+qpel;\ -static inline int RENAME(small_diamond_search)(MpegEncContext * s, int *best, int dmin, - uint8_t *src_data[3], - uint8_t *ref_data[3], int stride, int uvstride, - int const pred_x, int const pred_y, int const penalty_factor, - int const shift, - uint32_t *map, int map_generation, int size, int h, uint8_t * const mv_penalty - ) +static av_always_inline int small_diamond_search(MpegEncContext * s, int *best, int dmin, + int src_index, int ref_index, int const penalty_factor, + int size, int h, int flags) { - me_cmp_func cmp, chroma_cmp; + MotionEstContext * const c= &s->me; + me_cmp_func cmpf, chroma_cmpf; int next_dir=-1; LOAD_COMMON - - cmp= s->dsp.me_cmp[size]; - chroma_cmp= s->dsp.me_cmp[size+1]; + LOAD_COMMON2 + int map_generation= c->map_generation; + + cmpf= s->dsp.me_cmp[size]; + chroma_cmpf= s->dsp.me_cmp[size+1]; { /* ensure that the best point is in the MAP as h/qpel refinement needs it */ const int key= (best[1]<me; + me_cmp_func cmpf, chroma_cmpf; int dia_size; LOAD_COMMON - - cmp= s->dsp.me_cmp[size]; - chroma_cmp= s->dsp.me_cmp[size+1]; + LOAD_COMMON2 + int map_generation= c->map_generation; + + cmpf= s->dsp.me_cmp[size]; + chroma_cmpf= s->dsp.me_cmp[size+1]; for(dia_size=1; dia_size<=4; dia_size++){ int dir; const int x= best[0]; const int y= best[1]; - + if(dia_size&(dia_size-1)) continue; if( x + dia_size > xmax @@ -659,7 +622,7 @@ static inline int RENAME(funny_diamond_search)(MpegEncContext * s, int *best, in || y + dia_size > ymax || y - dia_size < ymin) continue; - + for(dir= 0; dirdx){ dx^=dy; dy^=dx; dx^=dy; } @@ -691,7 +654,161 @@ if(256*256*256*64 % (stats[0]+1)==0){ } #endif } - return dmin; + return dmin; +} + +static int hex_search(MpegEncContext * s, int *best, int dmin, + int src_index, int ref_index, int const penalty_factor, + int size, int h, int flags, int dia_size) +{ + MotionEstContext * const c= &s->me; + me_cmp_func cmpf, chroma_cmpf; + LOAD_COMMON + LOAD_COMMON2 + int map_generation= c->map_generation; + int x,y,d; + const int dec= dia_size & (dia_size-1); + + cmpf= s->dsp.me_cmp[size]; + chroma_cmpf= s->dsp.me_cmp[size+1]; + + for(;dia_size; dia_size= dec ? dia_size-1 : dia_size>>1){ + do{ + x= best[0]; + y= best[1]; + + CHECK_CLIPPED_MV(x -dia_size , y); + CHECK_CLIPPED_MV(x+ dia_size , y); + CHECK_CLIPPED_MV(x+( dia_size>>1), y+dia_size); + CHECK_CLIPPED_MV(x+( dia_size>>1), y-dia_size); + if(dia_size>1){ + CHECK_CLIPPED_MV(x+(-dia_size>>1), y+dia_size); + CHECK_CLIPPED_MV(x+(-dia_size>>1), y-dia_size); + } + }while(best[0] != x || best[1] != y); + } + + return dmin; +} + +static int l2s_dia_search(MpegEncContext * s, int *best, int dmin, + int src_index, int ref_index, int const penalty_factor, + int size, int h, int flags) +{ + MotionEstContext * const c= &s->me; + me_cmp_func cmpf, chroma_cmpf; + LOAD_COMMON + LOAD_COMMON2 + int map_generation= c->map_generation; + int x,y,i,d; + int dia_size= c->dia_size&0xFF; + const int dec= dia_size & (dia_size-1); + static const int hex[8][2]={{-2, 0}, {-1,-1}, { 0,-2}, { 1,-1}, + { 2, 0}, { 1, 1}, { 0, 2}, {-1, 1}}; + + cmpf= s->dsp.me_cmp[size]; + chroma_cmpf= s->dsp.me_cmp[size+1]; + + for(; dia_size; dia_size= dec ? dia_size-1 : dia_size>>1){ + do{ + x= best[0]; + y= best[1]; + for(i=0; i<8; i++){ + CHECK_CLIPPED_MV(x+hex[i][0]*dia_size, y+hex[i][1]*dia_size); + } + }while(best[0] != x || best[1] != y); + } + + x= best[0]; + y= best[1]; + CHECK_CLIPPED_MV(x+1, y); + CHECK_CLIPPED_MV(x, y+1); + CHECK_CLIPPED_MV(x-1, y); + CHECK_CLIPPED_MV(x, y-1); + + return dmin; +} + +static int umh_search(MpegEncContext * s, int *best, int dmin, + int src_index, int ref_index, int const penalty_factor, + int size, int h, int flags) +{ + MotionEstContext * const c= &s->me; + me_cmp_func cmpf, chroma_cmpf; + LOAD_COMMON + LOAD_COMMON2 + int map_generation= c->map_generation; + int x,y,x2,y2, i, j, d; + const int dia_size= c->dia_size&0xFE; + static const int hex[16][2]={{-4,-2}, {-4,-1}, {-4, 0}, {-4, 1}, {-4, 2}, + { 4,-2}, { 4,-1}, { 4, 0}, { 4, 1}, { 4, 2}, + {-2, 3}, { 0, 4}, { 2, 3}, + {-2,-3}, { 0,-4}, { 2,-3},}; + + cmpf= s->dsp.me_cmp[size]; + chroma_cmpf= s->dsp.me_cmp[size+1]; + + x= best[0]; + y= best[1]; + for(x2=FFMAX(x-dia_size+1, xmin); x2<=FFMIN(x+dia_size-1,xmax); x2+=2){ + CHECK_MV(x2, y); + } + for(y2=FFMAX(y-dia_size/2+1, ymin); y2<=FFMIN(y+dia_size/2-1,ymax); y2+=2){ + CHECK_MV(x, y2); + } + + x= best[0]; + y= best[1]; + for(y2=FFMAX(y-2, ymin); y2<=FFMIN(y+2,ymax); y2++){ + for(x2=FFMAX(x-2, xmin); x2<=FFMIN(x+2,xmax); x2++){ + CHECK_MV(x2, y2); + } + } + +//FIXME prevent the CLIP stuff + + for(j=1; j<=dia_size/4; j++){ + for(i=0; i<16; i++){ + CHECK_CLIPPED_MV(x+hex[i][0]*j, y+hex[i][1]*j); + } + } + + return hex_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags, 2); +} + +static int full_search(MpegEncContext * s, int *best, int dmin, + int src_index, int ref_index, int const penalty_factor, + int size, int h, int flags) +{ + MotionEstContext * const c= &s->me; + me_cmp_func cmpf, chroma_cmpf; + LOAD_COMMON + LOAD_COMMON2 + int map_generation= c->map_generation; + int x,y, d; + const int dia_size= c->dia_size&0xFF; + + cmpf= s->dsp.me_cmp[size]; + chroma_cmpf= s->dsp.me_cmp[size+1]; + + for(y=FFMAX(-dia_size, ymin); y<=FFMIN(dia_size,ymax); y++){ + for(x=FFMAX(-dia_size, xmin); x<=FFMIN(dia_size,xmax); x++){ + CHECK_MV(x, y); + } + } + + x= best[0]; + y= best[1]; + d= dmin; + CHECK_CLIPPED_MV(x , y); + CHECK_CLIPPED_MV(x+1, y); + CHECK_CLIPPED_MV(x, y+1); + CHECK_CLIPPED_MV(x-1, y); + CHECK_CLIPPED_MV(x, y-1); + best[0]= x; + best[1]= y; + + return d; } #define SAB_CHECK_MV(ax,ay)\ @@ -700,7 +817,7 @@ if(256*256*256*64 % (stats[0]+1)==0){ const int index= (((ay)<me; + me_cmp_func cmpf, chroma_cmpf; Minima minima[MAX_SAB_SIZE]; - const int minima_count= ABS(s->me.dia_size); + const int minima_count= FFABS(c->dia_size); int i, j; LOAD_COMMON - - cmp= s->dsp.me_cmp[size]; - chroma_cmp= s->dsp.me_cmp[size+1]; - - for(j=i=0; imap_generation; + + cmpf= s->dsp.me_cmp[size]; + chroma_cmpf= s->dsp.me_cmp[size+1]; + + /*Note j>=ME_MAP_MV_BITS; minima[j].y= key & ((1< xmax || minima[j].x < xmin + || minima[j].y > ymax || minima[j].y < ymin) + continue; + minima[j].checked=0; if(minima[j].x || minima[j].y) minima[j].height+= (mv_penalty[((minima[j].x)<= xmax || x <= xmin || y >= ymax || y <= ymin) continue; @@ -785,14 +908,14 @@ static inline int RENAME(sab_diamond_search)(MpegEncContext * s, int *best, int SAB_CHECK_MV(x+1, y) SAB_CHECK_MV(x , y-1) SAB_CHECK_MV(x , y+1) - + minima[i].checked= 1; } - + best[0]= minima[0].x; best[1]= minima[0].y; dmin= minima[0].height; - + if( best[0] < xmax && best[0] > xmin && best[1] < ymax && best[1] > ymin){ int d; @@ -802,25 +925,24 @@ static inline int RENAME(sab_diamond_search)(MpegEncContext * s, int *best, int CHECK_MV(best[0], best[1]-1) CHECK_MV(best[0], best[1]+1) } - return dmin; + return dmin; } -static inline int RENAME(var_diamond_search)(MpegEncContext * s, int *best, int dmin, - uint8_t *src_data[3], - uint8_t *ref_data[3], int stride, int uvstride, - int const pred_x, int const pred_y, int const penalty_factor, - int const shift, - uint32_t *map, int map_generation, int size, int h, uint8_t * const mv_penalty - ) +static int var_diamond_search(MpegEncContext * s, int *best, int dmin, + int src_index, int ref_index, int const penalty_factor, + int size, int h, int flags) { - me_cmp_func cmp, chroma_cmp; + MotionEstContext * const c= &s->me; + me_cmp_func cmpf, chroma_cmpf; int dia_size; LOAD_COMMON - - cmp= s->dsp.me_cmp[size]; - chroma_cmp= s->dsp.me_cmp[size+1]; + LOAD_COMMON2 + int map_generation= c->map_generation; - for(dia_size=1; dia_size<=s->me.dia_size; dia_size++){ + cmpf= s->dsp.me_cmp[size]; + chroma_cmpf= s->dsp.me_cmp[size+1]; + + for(dia_size=1; dia_size<=c->dia_size; dia_size++){ int dir, start, end; const int x= best[0]; const int y= best[1]; @@ -867,8 +989,8 @@ static inline int RENAME(var_diamond_search)(MpegEncContext * s, int *best, int { int dx, dy, i; static int stats[8*8]; -dx= ABS(x-best[0]); -dy= ABS(y-best[1]); +dx= FFABS(x-best[0]); +dy= FFABS(y-best[1]); stats[dy*8 + dx] ++; if(256*256*256*64 % (stats[0]+1)==0){ for(i=0; i<64; i++){ @@ -880,76 +1002,123 @@ if(256*256*256*64 % (stats[0]+1)==0){ } #endif } - return dmin; + return dmin; } -static int RENAME(epzs_motion_search)(MpegEncContext * s, - int *mx_ptr, int *my_ptr, - int P[10][2], int pred_x, int pred_y, uint8_t *src_data[3], - uint8_t *ref_data[3], int stride, int uvstride, int16_t (*last_mv)[2], - int ref_mv_scale, uint8_t * const mv_penalty) +static av_always_inline int diamond_search(MpegEncContext * s, int *best, int dmin, + int src_index, int ref_index, int const penalty_factor, + int size, int h, int flags){ + MotionEstContext * const c= &s->me; + if(c->dia_size==-1) + return funny_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags); + else if(c->dia_size<-1) + return sab_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags); + else if(c->dia_size<2) + return small_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags); + else if(c->dia_size>1024) + return full_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags); + else if(c->dia_size>768) + return umh_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags); + else if(c->dia_size>512) + return hex_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags, c->dia_size&0xFF); + else if(c->dia_size>256) + return l2s_dia_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags); + else + return var_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags); +} + +/*! + \param P[10][2] a list of candidate mvs to check before starting the + iterative search. If one of the candidates is close to the optimal mv, then + it takes fewer iterations. And it increases the chance that we find the + optimal mv. + */ +static av_always_inline int epzs_motion_search_internal(MpegEncContext * s, int *mx_ptr, int *my_ptr, + int P[10][2], int src_index, int ref_index, int16_t (*last_mv)[2], + int ref_mv_scale, int flags, int size, int h) { - int best[2]={0, 0}; - int d, dmin; - const int shift= 1+s->quarter_sample; - uint32_t *map= s->me.map; + MotionEstContext * const c= &s->me; + int best[2]={0, 0}; /*!< x and y coordinates of the best motion vector. + i.e. the difference between the position of the + block currently being encoded and the position of + the block chosen to predict it from. */ + int d; ///< the score (cmp + penalty) of any given mv + int dmin; /*!< the best value of d, i.e. the score + corresponding to the mv stored in best[]. */ int map_generation; - const int penalty_factor= s->me.penalty_factor; - const int size=0; - const int h=16; + int penalty_factor; const int ref_mv_stride= s->mb_stride; //pass as arg FIXME const int ref_mv_xy= s->mb_x + s->mb_y*ref_mv_stride; //add to last_mv beforepassing FIXME - me_cmp_func cmp, chroma_cmp; - LOAD_COMMON - - cmp= s->dsp.me_cmp[size]; - chroma_cmp= s->dsp.me_cmp[size+1]; - - map_generation= update_map_generation(s); + me_cmp_func cmpf, chroma_cmpf; - CMP(dmin, 0, 0, size); + LOAD_COMMON + LOAD_COMMON2 + + if(c->pre_pass){ + penalty_factor= c->pre_penalty_factor; + cmpf= s->dsp.me_pre_cmp[size]; + chroma_cmpf= s->dsp.me_pre_cmp[size+1]; + }else{ + penalty_factor= c->penalty_factor; + cmpf= s->dsp.me_cmp[size]; + chroma_cmpf= s->dsp.me_cmp[size+1]; + } + + map_generation= update_map_generation(c); + + assert(cmpf); + dmin= cmp(s, 0, 0, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags); map[0]= map_generation; score_map[0]= dmin; + //FIXME precalc first term below? + if((s->pict_type == FF_B_TYPE && !(c->flags & FLAG_DIRECT)) || s->flags&CODEC_FLAG_MV0) + dmin += (mv_penalty[pred_x] + mv_penalty[pred_y])*penalty_factor; + /* first line */ - if (s->mb_y == 0) { + if (s->first_slice_line) { CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift) - CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, + CHECK_CLIPPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16) }else{ - if(dmin<256 && ( P_LEFT[0] |P_LEFT[1] + if(dmin<((h*h*s->avctx->mv0_threshold)>>8) + && ( P_LEFT[0] |P_LEFT[1] |P_TOP[0] |P_TOP[1] |P_TOPRIGHT[0]|P_TOPRIGHT[1])==0){ *mx_ptr= 0; *my_ptr= 0; - s->me.skip=1; + c->skip=1; return dmin; } - CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift) - if(dmin>256*2){ - CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, - (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16) - CHECK_MV(P_LEFT[0] >>shift, P_LEFT[1] >>shift) - CHECK_MV(P_TOP[0] >>shift, P_TOP[1] >>shift) - CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift) - } + CHECK_MV( P_MEDIAN[0] >>shift , P_MEDIAN[1] >>shift) + CHECK_CLIPPED_MV((P_MEDIAN[0]>>shift) , (P_MEDIAN[1]>>shift)-1) + CHECK_CLIPPED_MV((P_MEDIAN[0]>>shift) , (P_MEDIAN[1]>>shift)+1) + CHECK_CLIPPED_MV((P_MEDIAN[0]>>shift)-1, (P_MEDIAN[1]>>shift) ) + CHECK_CLIPPED_MV((P_MEDIAN[0]>>shift)+1, (P_MEDIAN[1]>>shift) ) + CHECK_CLIPPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, + (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16) + CHECK_MV(P_LEFT[0] >>shift, P_LEFT[1] >>shift) + CHECK_MV(P_TOP[0] >>shift, P_TOP[1] >>shift) + CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift) } - if(dmin>256*4){ - if(s->me.pre_pass){ - CHECK_CLIPED_MV((last_mv[ref_mv_xy-1][0]*ref_mv_scale + (1<<15))>>16, + if(dmin>h*h*4){ + if(c->pre_pass){ + CHECK_CLIPPED_MV((last_mv[ref_mv_xy-1][0]*ref_mv_scale + (1<<15))>>16, (last_mv[ref_mv_xy-1][1]*ref_mv_scale + (1<<15))>>16) - CHECK_CLIPED_MV((last_mv[ref_mv_xy-ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16, - (last_mv[ref_mv_xy-ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16) + if(!s->first_slice_line) + CHECK_CLIPPED_MV((last_mv[ref_mv_xy-ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16, + (last_mv[ref_mv_xy-ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16) }else{ - CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16, + CHECK_CLIPPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16, (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16) - CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16, - (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16) + if(s->mb_y+1end_mb_y) //FIXME replace at least with last_slice_line + CHECK_CLIPPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16, + (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16) } } - if(s->avctx->last_predictor_count){ - const int count= s->avctx->last_predictor_count; + if(c->avctx->last_predictor_count){ + const int count= c->avctx->last_predictor_count; const int xstart= FFMAX(0, s->mb_x - count); const int ystart= FFMAX(0, s->mb_y - count); const int xend= FFMIN(s->mb_width , s->mb_x + count + 1); @@ -970,184 +1139,147 @@ static int RENAME(epzs_motion_search)(MpegEncContext * s, } //check(best[0],best[1],0, b0) - if(s->me.dia_size==-1) - dmin= RENAME(funny_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride, - pred_x, pred_y, penalty_factor, - shift, map, map_generation, size, h, mv_penalty); - else if(s->me.dia_size<-1) - dmin= RENAME(sab_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride, - pred_x, pred_y, penalty_factor, - shift, map, map_generation, size, h, mv_penalty); - else if(s->me.dia_size<2) - dmin= RENAME(small_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride, - pred_x, pred_y, penalty_factor, - shift, map, map_generation, size, h, mv_penalty); - else - dmin= RENAME(var_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride, - pred_x, pred_y, penalty_factor, - shift, map, map_generation, size, h, mv_penalty); + dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags); //check(best[0],best[1],0, b1) *mx_ptr= best[0]; - *my_ptr= best[1]; + *my_ptr= best[1]; // printf("%d %d %d \n", best[0], best[1], dmin); return dmin; } -#ifndef CMP_DIRECT /* no 4mv search needed in direct mode */ -static int RENAME(epzs_motion_search4)(MpegEncContext * s, - int *mx_ptr, int *my_ptr, - int P[10][2], int pred_x, int pred_y, - uint8_t *src_data[3], - uint8_t *ref_data[3], int stride, int uvstride, int16_t (*last_mv)[2], - int ref_mv_scale, uint8_t * const mv_penalty) +//this function is dedicated to the braindamaged gcc +inline int ff_epzs_motion_search(MpegEncContext * s, int *mx_ptr, int *my_ptr, + int P[10][2], int src_index, int ref_index, int16_t (*last_mv)[2], + int ref_mv_scale, int size, int h) { + MotionEstContext * const c= &s->me; +//FIXME convert other functions in the same way if faster + if(c->flags==0 && h==16 && size==0){ + return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, 0, 0, 16); +// case FLAG_QPEL: +// return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, FLAG_QPEL); + }else{ + return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, c->flags, size, h); + } +} + +static int epzs_motion_search4(MpegEncContext * s, + int *mx_ptr, int *my_ptr, int P[10][2], + int src_index, int ref_index, int16_t (*last_mv)[2], + int ref_mv_scale) +{ + MotionEstContext * const c= &s->me; int best[2]={0, 0}; - int d, dmin; - const int shift= 1+s->quarter_sample; - uint32_t *map= s->me.map; + int d, dmin; int map_generation; - const int penalty_factor= s->me.penalty_factor; + const int penalty_factor= c->penalty_factor; const int size=1; const int h=8; const int ref_mv_stride= s->mb_stride; const int ref_mv_xy= s->mb_x + s->mb_y *ref_mv_stride; - me_cmp_func cmp, chroma_cmp; + me_cmp_func cmpf, chroma_cmpf; LOAD_COMMON - - cmp= s->dsp.me_cmp[size]; - chroma_cmp= s->dsp.me_cmp[size+1]; + int flags= c->flags; + LOAD_COMMON2 - map_generation= update_map_generation(s); + cmpf= s->dsp.me_cmp[size]; + chroma_cmpf= s->dsp.me_cmp[size+1]; + + map_generation= update_map_generation(c); dmin = 1000000; -//printf("%d %d %d %d //",xmin, ymin, xmax, ymax); +//printf("%d %d %d %d //",xmin, ymin, xmax, ymax); /* first line */ - if (s->mb_y == 0/* && block<2*/) { - CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift) - CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, + if (s->first_slice_line) { + CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift) + CHECK_CLIPPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16) CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift) }else{ CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift) //FIXME try some early stop - if(dmin>64*2){ - CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift) - CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift) - CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift) - CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift) - CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, - (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16) - } + CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift) + CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift) + CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift) + CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift) + CHECK_CLIPPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, + (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16) } if(dmin>64*4){ - CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16, + CHECK_CLIPPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16, (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16) - CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16, - (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16) + if(s->mb_y+1end_mb_y) //FIXME replace at least with last_slice_line + CHECK_CLIPPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16, + (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16) } - if(s->me.dia_size==-1) - dmin= RENAME(funny_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride, - pred_x, pred_y, penalty_factor, - shift, map, map_generation, size, h, mv_penalty); - else if(s->me.dia_size<-1) - dmin= RENAME(sab_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride, - pred_x, pred_y, penalty_factor, - shift, map, map_generation, size, h, mv_penalty); - else if(s->me.dia_size<2) - dmin= RENAME(small_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride, - pred_x, pred_y, penalty_factor, - shift, map, map_generation, size, h, mv_penalty); - else - dmin= RENAME(var_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride, - pred_x, pred_y, penalty_factor, - shift, map, map_generation, size, h, mv_penalty); - + dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags); *mx_ptr= best[0]; - *my_ptr= best[1]; + *my_ptr= best[1]; // printf("%d %d %d \n", best[0], best[1], dmin); return dmin; } //try to merge with above FIXME (needs PSNR test) -static int RENAME(epzs_motion_search2)(MpegEncContext * s, - int *mx_ptr, int *my_ptr, - int P[10][2], int pred_x, int pred_y, - uint8_t *src_data[3], - uint8_t *ref_data[3], int stride, int uvstride, int16_t (*last_mv)[2], - int ref_mv_scale, uint8_t * const mv_penalty) +static int epzs_motion_search2(MpegEncContext * s, + int *mx_ptr, int *my_ptr, int P[10][2], + int src_index, int ref_index, int16_t (*last_mv)[2], + int ref_mv_scale) { + MotionEstContext * const c= &s->me; int best[2]={0, 0}; - int d, dmin; - const int shift= 1+s->quarter_sample; - uint32_t *map= s->me.map; + int d, dmin; int map_generation; - const int penalty_factor= s->me.penalty_factor; + const int penalty_factor= c->penalty_factor; const int size=0; //FIXME pass as arg const int h=8; const int ref_mv_stride= s->mb_stride; const int ref_mv_xy= s->mb_x + s->mb_y *ref_mv_stride; - me_cmp_func cmp, chroma_cmp; + me_cmp_func cmpf, chroma_cmpf; LOAD_COMMON - - cmp= s->dsp.me_cmp[size]; - chroma_cmp= s->dsp.me_cmp[size+1]; + int flags= c->flags; + LOAD_COMMON2 - map_generation= update_map_generation(s); + cmpf= s->dsp.me_cmp[size]; + chroma_cmpf= s->dsp.me_cmp[size+1]; + + map_generation= update_map_generation(c); dmin = 1000000; -//printf("%d %d %d %d //",xmin, ymin, xmax, ymax); +//printf("%d %d %d %d //",xmin, ymin, xmax, ymax); /* first line */ - if (s->mb_y == 0) { - CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift) - CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, + if (s->first_slice_line) { + CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift) + CHECK_CLIPPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16) CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift) }else{ CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift) //FIXME try some early stop - if(dmin>64*2){ - CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift) - CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift) - CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift) - CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift) - CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, - (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16) - } + CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift) + CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift) + CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift) + CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift) + CHECK_CLIPPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, + (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16) } if(dmin>64*4){ - CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16, + CHECK_CLIPPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16, (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16) - CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16, - (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16) + if(s->mb_y+1end_mb_y) //FIXME replace at least with last_slice_line + CHECK_CLIPPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16, + (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16) } - if(s->me.dia_size==-1) - dmin= RENAME(funny_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride, - pred_x, pred_y, penalty_factor, - shift, map, map_generation, size, h, mv_penalty); - else if(s->me.dia_size<-1) - dmin= RENAME(sab_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride, - pred_x, pred_y, penalty_factor, - shift, map, map_generation, size, h, mv_penalty); - else if(s->me.dia_size<2) - dmin= RENAME(small_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride, - pred_x, pred_y, penalty_factor, - shift, map, map_generation, size, h, mv_penalty); - else - dmin= RENAME(var_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride, - pred_x, pred_y, penalty_factor, - shift, map, map_generation, size, h, mv_penalty); - + dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags); *mx_ptr= best[0]; - *my_ptr= best[1]; + *my_ptr= best[1]; // printf("%d %d %d \n", best[0], best[1], dmin); return dmin; } -#endif /* !CMP_DIRECT */ diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/motionpixels.c b/src/add-ons/media/plugins/avcodec/libavcodec/motionpixels.c new file mode 100644 index 0000000000..01a94546d2 --- /dev/null +++ b/src/add-ons/media/plugins/avcodec/libavcodec/motionpixels.c @@ -0,0 +1,363 @@ +/* + * Motion Pixels Video Decoder + * Copyright (c) 2008 Gregory Montoir (cyx@users.sourceforge.net) + * + * This file is part of FFmpeg. + * + * FFmpeg 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.1 of the License, or (at your option) any later version. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "avcodec.h" +#include "bitstream.h" +#include "dsputil.h" + +#define MAX_HUFF_CODES 16 + +typedef struct YuvPixel { + int8_t y, v, u; +} YuvPixel; + +typedef struct HuffCode { + int code; + uint8_t size; + uint8_t delta; +} HuffCode; + +typedef struct MotionPixelsContext { + AVCodecContext *avctx; + AVFrame frame; + DSPContext dsp; + uint8_t *changes_map; + int offset_bits_len; + int codes_count, current_codes_count; + int max_codes_bits; + HuffCode codes[MAX_HUFF_CODES]; + VLC vlc; + YuvPixel *vpt, *hpt; + uint8_t gradient_scale[3]; + uint8_t *bswapbuf; + int bswapbuf_size; +} MotionPixelsContext; + +static YuvPixel mp_rgb_yuv_table[1 << 15]; + +static int mp_yuv_to_rgb(int y, int v, int u, int clip_rgb) { + static const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP; + int r, g, b; + + r = (1000 * y + 701 * v) / 1000; + g = (1000 * y - 357 * v - 172 * u) / 1000; + b = (1000 * y + 886 * u) / 1000; + if (clip_rgb) + return ((cm[r * 8] & 0xF8) << 7) | ((cm[g * 8] & 0xF8) << 2) | (cm[b * 8] >> 3); + if ((unsigned)r < 32 && (unsigned)g < 32 && (unsigned)b < 32) + return (r << 10) | (g << 5) | b; + return 1 << 15; +} + +static void mp_set_zero_yuv(YuvPixel *p) +{ + int i, j; + + for (i = 0; i < 31; ++i) { + for (j = 31; j > i; --j) + if (!(p[j].u | p[j].v | p[j].y)) + p[j] = p[j - 1]; + for (j = 0; j < 31 - i; ++j) + if (!(p[j].u | p[j].v | p[j].y)) + p[j] = p[j + 1]; + } +} + +static void mp_build_rgb_yuv_table(YuvPixel *p) +{ + int y, v, u, i; + + for (y = 0; y <= 31; ++y) + for (v = -31; v <= 31; ++v) + for (u = -31; u <= 31; ++u) { + i = mp_yuv_to_rgb(y, v, u, 0); + if (i < (1 << 15) && !(p[i].u | p[i].v | p[i].y)) { + p[i].y = y; + p[i].v = v; + p[i].u = u; + } + } + for (i = 0; i < 1024; ++i) + mp_set_zero_yuv(p + i * 32); +} + +static av_cold int mp_decode_init(AVCodecContext *avctx) +{ + MotionPixelsContext *mp = avctx->priv_data; + + if (!mp_rgb_yuv_table[0].u) { + mp_build_rgb_yuv_table(mp_rgb_yuv_table); + } + mp->avctx = avctx; + dsputil_init(&mp->dsp, avctx); + mp->changes_map = av_mallocz(avctx->width * avctx->height); + mp->offset_bits_len = av_log2(avctx->width * avctx->height) + 1; + mp->vpt = av_mallocz(avctx->height * sizeof(YuvPixel)); + mp->hpt = av_mallocz(avctx->height * avctx->width / 16 * sizeof(YuvPixel)); + return 0; +} + +static void mp_read_changes_map(MotionPixelsContext *mp, GetBitContext *gb, int count, int bits_len, int read_color) +{ + uint16_t *pixels; + int offset, w, h, color = 0, x, y, i; + + while (count--) { + offset = get_bits_long(gb, mp->offset_bits_len); + w = get_bits(gb, bits_len) + 1; + h = get_bits(gb, bits_len) + 1; + if (read_color) + color = get_bits(gb, 15); + x = offset % mp->avctx->width; + y = offset / mp->avctx->width; + if (y >= mp->avctx->height) + continue; + w = FFMIN(w, mp->avctx->width - x); + h = FFMIN(h, mp->avctx->height - y); + pixels = (uint16_t *)&mp->frame.data[0][y * mp->frame.linesize[0] + x * 2]; + while (h--) { + mp->changes_map[offset] = w; + if (read_color) + for (i = 0; i < w; ++i) + pixels[i] = color; + offset += mp->avctx->width; + pixels += mp->frame.linesize[0] / 2; + } + } +} + +static void mp_get_code(MotionPixelsContext *mp, GetBitContext *gb, int size, int code) +{ + while (get_bits1(gb)) { + ++size; + if (size > mp->max_codes_bits) { + av_log(mp->avctx, AV_LOG_ERROR, "invalid code size %d/%d\n", size, mp->max_codes_bits); + return; + } + code <<= 1; + mp_get_code(mp, gb, size, code + 1); + } + if (mp->current_codes_count >= MAX_HUFF_CODES) { + av_log(mp->avctx, AV_LOG_ERROR, "too many codes\n"); + return; + } + mp->codes[mp->current_codes_count ].code = code; + mp->codes[mp->current_codes_count++].size = size; +} + +static void mp_read_codes_table(MotionPixelsContext *mp, GetBitContext *gb) +{ + if (mp->codes_count == 1) { + mp->codes[0].delta = get_bits(gb, 4); + } else { + int i; + + mp->max_codes_bits = get_bits(gb, 4); + for (i = 0; i < mp->codes_count; ++i) + mp->codes[i].delta = get_bits(gb, 4); + mp->current_codes_count = 0; + mp_get_code(mp, gb, 0, 0); + } +} + +static int mp_gradient(MotionPixelsContext *mp, int component, int v) +{ + int delta; + + delta = (v - 7) * mp->gradient_scale[component]; + mp->gradient_scale[component] = (v == 0 || v == 14) ? 2 : 1; + return delta; +} + +static YuvPixel mp_get_yuv_from_rgb(MotionPixelsContext *mp, int x, int y) +{ + int color; + + color = *(uint16_t *)&mp->frame.data[0][y * mp->frame.linesize[0] + x * 2]; + return mp_rgb_yuv_table[color]; +} + +static void mp_set_rgb_from_yuv(MotionPixelsContext *mp, int x, int y, const YuvPixel *p) +{ + int color; + + color = mp_yuv_to_rgb(p->y, p->v, p->u, 1); + *(uint16_t *)&mp->frame.data[0][y * mp->frame.linesize[0] + x * 2] = color; +} + +static int mp_get_vlc(MotionPixelsContext *mp, GetBitContext *gb) +{ + int i; + + i = (mp->codes_count == 1) ? 0 : get_vlc2(gb, mp->vlc.table, mp->max_codes_bits, 1); + return mp->codes[i].delta; +} + +static void mp_decode_line(MotionPixelsContext *mp, GetBitContext *gb, int y) +{ + YuvPixel p; + const int y0 = y * mp->avctx->width; + int w, i, x = 0; + + p = mp->vpt[y]; + if (mp->changes_map[y0 + x] == 0) { + memset(mp->gradient_scale, 1, sizeof(mp->gradient_scale)); + ++x; + } + while (x < mp->avctx->width) { + w = mp->changes_map[y0 + x]; + if (w != 0) { + if ((y & 3) == 0) { + if (mp->changes_map[y0 + x + mp->avctx->width] < w || + mp->changes_map[y0 + x + mp->avctx->width * 2] < w || + mp->changes_map[y0 + x + mp->avctx->width * 3] < w) { + for (i = (x + 3) & ~3; i < x + w; i += 4) { + mp->hpt[((y / 4) * mp->avctx->width + i) / 4] = mp_get_yuv_from_rgb(mp, i, y); + } + } + } + x += w; + memset(mp->gradient_scale, 1, sizeof(mp->gradient_scale)); + p = mp_get_yuv_from_rgb(mp, x - 1, y); + } else { + p.y += mp_gradient(mp, 0, mp_get_vlc(mp, gb)); + if ((x & 3) == 0) { + if ((y & 3) == 0) { + p.v += mp_gradient(mp, 1, mp_get_vlc(mp, gb)); + p.u += mp_gradient(mp, 2, mp_get_vlc(mp, gb)); + mp->hpt[((y / 4) * mp->avctx->width + x) / 4] = p; + } else { + p.v = mp->hpt[((y / 4) * mp->avctx->width + x) / 4].v; + p.u = mp->hpt[((y / 4) * mp->avctx->width + x) / 4].u; + } + } + mp_set_rgb_from_yuv(mp, x, y, &p); + ++x; + } + } +} + +static void mp_decode_frame_helper(MotionPixelsContext *mp, GetBitContext *gb) +{ + YuvPixel p; + int y, y0; + + for (y = 0; y < mp->avctx->height; ++y) { + if (mp->changes_map[y * mp->avctx->width] != 0) { + memset(mp->gradient_scale, 1, sizeof(mp->gradient_scale)); + p = mp_get_yuv_from_rgb(mp, 0, y); + } else { + p.y += mp_gradient(mp, 0, mp_get_vlc(mp, gb)); + if ((y & 3) == 0) { + p.v += mp_gradient(mp, 1, mp_get_vlc(mp, gb)); + p.u += mp_gradient(mp, 2, mp_get_vlc(mp, gb)); + } + mp->vpt[y] = p; + mp_set_rgb_from_yuv(mp, 0, y, &p); + } + } + for (y0 = 0; y0 < 2; ++y0) + for (y = y0; y < mp->avctx->height; y += 2) + mp_decode_line(mp, gb, y); +} + +static int mp_decode_frame(AVCodecContext *avctx, + void *data, int *data_size, + const uint8_t *buf, int buf_size) +{ + MotionPixelsContext *mp = avctx->priv_data; + GetBitContext gb; + int i, count1, count2, sz; + + mp->frame.reference = 1; + mp->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE; + if (avctx->reget_buffer(avctx, &mp->frame)) { + av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n"); + return -1; + } + + /* le32 bitstream msb first */ + mp->bswapbuf = av_fast_realloc(mp->bswapbuf, &mp->bswapbuf_size, buf_size + FF_INPUT_BUFFER_PADDING_SIZE); + mp->dsp.bswap_buf((uint32_t *)mp->bswapbuf, (const uint32_t *)buf, buf_size / 4); + if (buf_size & 3) + memcpy(mp->bswapbuf + (buf_size & ~3), buf + (buf_size & ~3), buf_size & 3); + init_get_bits(&gb, mp->bswapbuf, buf_size * 8); + + memset(mp->changes_map, 0, avctx->width * avctx->height); + for (i = !(avctx->extradata[1] & 2); i < 2; ++i) { + count1 = get_bits(&gb, 12); + count2 = get_bits(&gb, 12); + mp_read_changes_map(mp, &gb, count1, 8, i); + mp_read_changes_map(mp, &gb, count2, 4, i); + } + + mp->codes_count = get_bits(&gb, 4); + if (mp->codes_count == 0) + goto end; + + if (mp->changes_map[0] == 0) { + *(uint16_t *)mp->frame.data[0] = get_bits(&gb, 15); + mp->changes_map[0] = 1; + } + mp_read_codes_table(mp, &gb); + + sz = get_bits(&gb, 18); + if (avctx->extradata[0] != 5) + sz += get_bits(&gb, 18); + if (sz == 0) + goto end; + + init_vlc(&mp->vlc, mp->max_codes_bits, mp->codes_count, &mp->codes[0].size, sizeof(HuffCode), 1, &mp->codes[0].code, sizeof(HuffCode), 4, 0); + mp_decode_frame_helper(mp, &gb); + free_vlc(&mp->vlc); + +end: + *data_size = sizeof(AVFrame); + *(AVFrame *)data = mp->frame; + return buf_size; +} + +static av_cold int mp_decode_end(AVCodecContext *avctx) +{ + MotionPixelsContext *mp = avctx->priv_data; + + av_freep(&mp->changes_map); + av_freep(&mp->vpt); + av_freep(&mp->hpt); + av_freep(&mp->bswapbuf); + if (mp->frame.data[0]) + avctx->release_buffer(avctx, &mp->frame); + + return 0; +} + +AVCodec motionpixels_decoder = { + "motionpixels", + CODEC_TYPE_VIDEO, + CODEC_ID_MOTIONPIXELS, + sizeof(MotionPixelsContext), + mp_decode_init, + NULL, + mp_decode_end, + mp_decode_frame, + CODEC_CAP_DR1, + .long_name = NULL_IF_CONFIG_SMALL("Motion Pixels Video"), +}; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/movsub_bsf.c b/src/add-ons/media/plugins/avcodec/libavcodec/movsub_bsf.c new file mode 100644 index 0000000000..99e8135f5e --- /dev/null +++ b/src/add-ons/media/plugins/avcodec/libavcodec/movsub_bsf.c @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2008 Reimar Döffinger + * + * This file is part of FFmpeg. + * + * FFmpeg 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.1 of the License, or (at your option) any later version. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "avcodec.h" + + +static int text2movsub(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args, + uint8_t **poutbuf, int *poutbuf_size, + const uint8_t *buf, int buf_size, int keyframe){ + if (buf_size > 0xffff) return 0; + *poutbuf_size = buf_size + 2; + *poutbuf = av_malloc(*poutbuf_size + FF_INPUT_BUFFER_PADDING_SIZE); + AV_WB16(*poutbuf, buf_size); + memcpy(*poutbuf + 2, buf, buf_size); + return 1; +} + +AVBitStreamFilter text2movsub_bsf={ + "text2movsub", + 0, + text2movsub, +}; + +static int mov2textsub(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args, + uint8_t **poutbuf, int *poutbuf_size, + const uint8_t *buf, int buf_size, int keyframe){ + if (buf_size < 2) return 0; + *poutbuf_size = FFMIN(buf_size - 2, AV_RB16(buf)); + *poutbuf = av_malloc(*poutbuf_size + FF_INPUT_BUFFER_PADDING_SIZE); + memcpy(*poutbuf, buf + 2, *poutbuf_size); + return 1; +} + +AVBitStreamFilter mov2textsub_bsf={ + "mov2textsub", + 0, + mov2textsub, +}; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/mp3_header_compress_bsf.c b/src/add-ons/media/plugins/avcodec/libavcodec/mp3_header_compress_bsf.c new file mode 100644 index 0000000000..f5c513834a --- /dev/null +++ b/src/add-ons/media/plugins/avcodec/libavcodec/mp3_header_compress_bsf.c @@ -0,0 +1,86 @@ +/* + * copyright (c) 2006 Michael Niedermayer + * + * This file is part of FFmpeg. + * + * FFmpeg 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.1 of the License, or (at your option) any later version. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "avcodec.h" +#include "mpegaudio.h" + + +static int mp3_header_compress(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args, + uint8_t **poutbuf, int *poutbuf_size, + const uint8_t *buf, int buf_size, int keyframe){ + uint32_t header, extraheader; + int mode_extension, header_size; + + if(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){ + av_log(avctx, AV_LOG_ERROR, "not standards compliant\n"); + return -1; + } + + header = AV_RB32(buf); + mode_extension= (header>>4)&3; + + if(ff_mpa_check_header(header) < 0 || (header&0x60000) != 0x20000){ +output_unchanged: + *poutbuf= (uint8_t *) buf; + *poutbuf_size= buf_size; + + av_log(avctx, AV_LOG_INFO, "cannot compress %08X\n", header); + return 0; + } + + if(avctx->extradata_size == 0){ + avctx->extradata_size=15; + avctx->extradata= av_malloc(avctx->extradata_size); + strcpy(avctx->extradata, "FFCMP3 0.0"); + memcpy(avctx->extradata+11, buf, 4); + } + if(avctx->extradata_size != 15){ + av_log(avctx, AV_LOG_ERROR, "Extradata invalid\n"); + return -1; + } + extraheader = AV_RB32(avctx->extradata+11); + if((extraheader&MP3_MASK) != (header&MP3_MASK)) + goto output_unchanged; + + header_size= (header&0x10000) ? 4 : 6; + + *poutbuf_size= buf_size - header_size; + *poutbuf= av_malloc(buf_size - header_size + FF_INPUT_BUFFER_PADDING_SIZE); + memcpy(*poutbuf, buf + header_size, buf_size - header_size + FF_INPUT_BUFFER_PADDING_SIZE); + + if(avctx->channels==2){ + if((header & (3<<19)) != 3<<19){ + (*poutbuf)[1] &= 0x3F; + (*poutbuf)[1] |= mode_extension<<6; + FFSWAP(int, (*poutbuf)[1], (*poutbuf)[2]); + }else{ + (*poutbuf)[1] &= 0x8F; + (*poutbuf)[1] |= mode_extension<<4; + } + } + + return 1; +} + +AVBitStreamFilter mp3_header_compress_bsf={ + "mp3comp", + 0, + mp3_header_compress, +}; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/mp3_header_decompress_bsf.c b/src/add-ons/media/plugins/avcodec/libavcodec/mp3_header_decompress_bsf.c new file mode 100644 index 0000000000..d897ed9852 --- /dev/null +++ b/src/add-ons/media/plugins/avcodec/libavcodec/mp3_header_decompress_bsf.c @@ -0,0 +1,96 @@ +/* + * copyright (c) 2006 Michael Niedermayer + * + * This file is part of FFmpeg. + * + * FFmpeg 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.1 of the License, or (at your option) any later version. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "avcodec.h" +#include "mpegaudio.h" +#include "mpegaudiodata.h" + + +static int mp3_header_decompress(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args, + uint8_t **poutbuf, int *poutbuf_size, + const uint8_t *buf, int buf_size, int keyframe){ + uint32_t header; + int sample_rate= avctx->sample_rate; + int sample_rate_index=0; + int lsf, mpeg25, bitrate_index, frame_size; + + header = AV_RB32(buf); + if(ff_mpa_check_header(header) >= 0){ + *poutbuf= (uint8_t *) buf; + *poutbuf_size= buf_size; + + return 0; + } + + if(avctx->extradata_size != 15 || strcmp(avctx->extradata, "FFCMP3 0.0")){ + av_log(avctx, AV_LOG_ERROR, "Extradata invalid %d\n", avctx->extradata_size); + return -1; + } + + header= AV_RB32(avctx->extradata+11) & MP3_MASK; + + lsf = sample_rate < (24000+32000)/2; + mpeg25 = sample_rate < (12000+16000)/2; + sample_rate_index= (header>>10)&3; + sample_rate= ff_mpa_freq_tab[sample_rate_index] >> (lsf + mpeg25); //in case sample rate is a little off + + for(bitrate_index=2; bitrate_index<30; bitrate_index++){ + frame_size = ff_mpa_bitrate_tab[lsf][2][bitrate_index>>1]; + frame_size = (frame_size * 144000) / (sample_rate << lsf) + (bitrate_index&1); + if(frame_size == buf_size + 4) + break; + if(frame_size == buf_size + 6) + break; + } + if(bitrate_index == 30){ + av_log(avctx, AV_LOG_ERROR, "Could not find bitrate_index.\n"); + return -1; + } + + header |= (bitrate_index&1)<<9; + header |= (bitrate_index>>1)<<12; + header |= (frame_size == buf_size + 4)<<16; //FIXME actually set a correct crc instead of 0 + + *poutbuf_size= frame_size; + *poutbuf= av_malloc(frame_size + FF_INPUT_BUFFER_PADDING_SIZE); + memcpy(*poutbuf + frame_size - buf_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE); + + if(avctx->channels==2){ + uint8_t *p= *poutbuf + frame_size - buf_size; + if(lsf){ + FFSWAP(int, p[1], p[2]); + header |= (p[1] & 0xC0)>>2; + p[1] &= 0x3F; + }else{ + header |= p[1] & 0x30; + p[1] &= 0xCF; + } + } + + AV_WB32(*poutbuf, header); + + return 1; +} + +AVBitStreamFilter mp3_header_decompress_bsf={ + "mp3decomp", + 0, + mp3_header_decompress, +}; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/msmpeg4.c b/src/add-ons/media/plugins/avcodec/libavcodec/msmpeg4.c index b7b88c38f8..16fdd8608c 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/msmpeg4.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/msmpeg4.c @@ -3,21 +3,23 @@ * Copyright (c) 2001 Fabrice Bellard. * Copyright (c) 2002-2004 Michael Niedermayer * - * This library is free software; you can redistribute it and/or + * msmpeg4v1 & v2 stuff by Michael Niedermayer + * + * This file is part of FFmpeg. + * + * FFmpeg 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. + * version 2.1 of the License, or (at your option) any later version. * - * This library is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * msmpeg4v1 & v2 stuff by Michael Niedermayer + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ /** @@ -28,19 +30,19 @@ #include "avcodec.h" #include "dsputil.h" #include "mpegvideo.h" +#include "msmpeg4.h" /* - * You can also call this codec : MPEG4 with a twist ! + * You can also call this codec : MPEG4 with a twist ! * - * TODO: + * TODO: * - (encoding) select best mv table (two choices) - * - (encoding) select best vlc/dc table + * - (encoding) select best vlc/dc table */ //#define DEBUG #define DC_VLC_BITS 9 #define CBPY_VLC_BITS 6 -#define INTER_INTRA_VLC_BITS 3 #define V1_INTRA_CBPC_VLC_BITS 6 #define V1_INTER_CBPC_VLC_BITS 6 #define V2_INTRA_CBPC_VLC_BITS 3 @@ -48,8 +50,6 @@ #define MV_VLC_BITS 9 #define V2_MV_VLC_BITS 9 #define TEX_VLC_BITS 9 -#define MB_NON_INTRA_VLC_BITS 9 -#define MB_INTRA_VLC_BITS 9 #define II_BITRATE 128*1024 #define MBAC_BITRATE 50*1024 @@ -59,22 +59,18 @@ static uint32_t v2_dc_lum_table[512][2]; static uint32_t v2_dc_chroma_table[512][2]; -static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n); -static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, - int n, int coded, const uint8_t *scantable); static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr); -static int msmpeg4_decode_motion(MpegEncContext * s, - int *mx_ptr, int *my_ptr); -static void msmpeg4v2_encode_motion(MpegEncContext * s, int val); static void init_h263_dc_for_msmpeg4(void); static inline void msmpeg4_memsetw(short *tab, int val, int n); #ifdef CONFIG_ENCODERS +static void msmpeg4v2_encode_motion(MpegEncContext * s, int val); static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra); #endif //CONFIG_ENCODERS static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64]); static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64]); -static int wmv2_decode_mb(MpegEncContext *s, DCTELEM block[6][64]); +/* vc1 externs */ +extern const uint8_t wmv3_dc_scale_table[32]; #ifdef DEBUG int intra_count = 0; @@ -83,78 +79,16 @@ int frame_count = 0; #include "msmpeg4data.h" -#ifdef CONFIG_ENCODERS //strangely gcc includes this even if its not references +#ifdef CONFIG_ENCODERS //strangely gcc includes this even if it is not references static uint8_t rl_length[NB_RL_TABLES][MAX_LEVEL+1][MAX_RUN+1][2]; #endif //CONFIG_ENCODERS -#ifdef STATS - -const char *st_names[ST_NB] = { - "unknown", - "dc", - "intra_ac", - "inter_ac", - "intra_mb", - "inter_mb", - "mv", -}; - -int st_current_index = 0; -unsigned int st_bit_counts[ST_NB]; -unsigned int st_out_bit_counts[ST_NB]; - -#define set_stat(var) st_current_index = var; - -void print_stats(void) -{ - unsigned int total; - int i; - - printf("Input:\n"); - total = 0; - for(i=0;imsmpeg4_version){ case 1: case 2: @@ -164,7 +98,7 @@ static void common_init(MpegEncContext * s) case 3: if(s->workaround_bugs){ s->y_dc_scale_table= old_ff_y_dc_scale_table; - s->c_dc_scale_table= old_ff_c_dc_scale_table; + s->c_dc_scale_table= wmv1_c_dc_scale_table; } else{ s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table; s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table; @@ -175,9 +109,16 @@ static void common_init(MpegEncContext * s) s->y_dc_scale_table= wmv1_y_dc_scale_table; s->c_dc_scale_table= wmv1_c_dc_scale_table; break; +#if defined(CONFIG_WMV3_DECODER)||defined(CONFIG_VC1_DECODER) + case 6: + s->y_dc_scale_table= wmv3_dc_scale_table; + s->c_dc_scale_table= wmv3_dc_scale_table; + break; +#endif + } - + if(s->msmpeg4_version>=4){ ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , wmv1_scantable[1]); ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, wmv1_scantable[2]); @@ -185,9 +126,9 @@ static void common_init(MpegEncContext * s) ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , wmv1_scantable[0]); } //Note the default tables are set in common_init in mpegvideo.c - - if(!inited){ - inited=1; + + if(!initialized){ + initialized=1; init_h263_dc_for_msmpeg4(); } @@ -204,7 +145,7 @@ static void init_mv_table(MVTable *tab) /* mark all entries as not used */ for(i=0;i<4096;i++) tab->table_mv_index[i] = tab->n; - + for(i=0;in;i++) { x = tab->table_mvx[i]; y = tab->table_mvy[i]; @@ -212,7 +153,7 @@ static void init_mv_table(MVTable *tab) } } -static void code012(PutBitContext *pb, int n) +void ff_msmpeg4_code012(PutBitContext *pb, int n) { if (n == 0) { put_bits(pb, 1, 0); @@ -239,7 +180,7 @@ void ff_msmpeg4_encode_init(MpegEncContext *s) init_mv_table(&mv_tables[0]); init_mv_table(&mv_tables[1]); for(i=0;itable_vlc[code][1]; if (code == rl->n) { int level1, run1; level1 = level - rl->max_level[last][run]; - if (level1 < 1) + if (level1 < 1) goto esc2; code = get_rl_index(rl, last, run, level1); if (code == rl->n) { @@ -297,7 +238,7 @@ static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, return size; } -static void find_best_tables(MpegEncContext * s) +void ff_find_best_tables(MpegEncContext * s) { int i; int best =-1, best_size =9999999; @@ -309,7 +250,7 @@ static void find_best_tables(MpegEncContext * s) int size=0; if(i>0){// ;) - size++; + size++; chroma_size++; } for(level=0; level<=MAX_LEVEL; level++){ @@ -321,15 +262,15 @@ static void find_best_tables(MpegEncContext * s) int inter_count = s->ac_stats[0][0][level][run][last] + s->ac_stats[0][1][level][run][last]; int intra_luma_count = s->ac_stats[1][0][level][run][last]; int intra_chroma_count= s->ac_stats[1][1][level][run][last]; - - if(s->pict_type==I_TYPE){ + + if(s->pict_type==FF_I_TYPE){ size += intra_luma_count *rl_length[i ][level][run][last]; chroma_size+= intra_chroma_count*rl_length[i+3][level][run][last]; }else{ size+= intra_luma_count *rl_length[i ][level][run][last] +intra_chroma_count*rl_length[i+3][level][run][last] +inter_count *rl_length[i+3][level][run][last]; - } + } } if(last_size == size+chroma_size) break; } @@ -344,19 +285,19 @@ static void find_best_tables(MpegEncContext * s) } } -// printf("type:%d, best:%d, qp:%d, var:%d, mcvar:%d, size:%d //\n", +// printf("type:%d, best:%d, qp:%d, var:%d, mcvar:%d, size:%d //\n", // s->pict_type, best, s->qscale, s->mb_var_sum, s->mc_mb_var_sum, best_size); - - if(s->pict_type==P_TYPE) chroma_best= best; + + if(s->pict_type==FF_P_TYPE) chroma_best= best; memset(s->ac_stats, 0, sizeof(int)*(MAX_LEVEL+1)*(MAX_RUN+1)*2*2*2); s->rl_table_index = best; s->rl_chroma_table_index= chroma_best; - + if(s->pict_type != s->last_non_b_pict_type){ s->rl_table_index= 2; - if(s->pict_type==I_TYPE) + if(s->pict_type==FF_I_TYPE) s->rl_chroma_table_index= 1; else s->rl_chroma_table_index= 2; @@ -367,7 +308,7 @@ static void find_best_tables(MpegEncContext * s) /* write MSMPEG4 compatible frame header */ void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number) { - find_best_tables(s); + ff_find_best_tables(s); align_put_bits(&s->pb); put_bits(&s->pb, 2, s->pict_type - 1); @@ -383,13 +324,13 @@ void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number) s->use_skip_mb_code = 1; /* only if P frame */ s->per_mb_rl_table = 0; if(s->msmpeg4_version==4) - s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE && s->pict_type==P_TYPE); + s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE && s->pict_type==FF_P_TYPE); //printf("%d %d %d %d %d\n", s->pict_type, s->bit_rate, s->inter_intra_pred, s->width, s->height); - if (s->pict_type == I_TYPE) { + if (s->pict_type == FF_I_TYPE) { s->slice_height= s->mb_height/1; put_bits(&s->pb, 5, 0x16 + s->mb_height/s->slice_height); - + if(s->msmpeg4_version==4){ msmpeg4_encode_ext_header(s); if(s->bit_rate>MBAC_BITRATE) @@ -398,21 +339,21 @@ void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number) if(s->msmpeg4_version>2){ if(!s->per_mb_rl_table){ - code012(&s->pb, s->rl_chroma_table_index); - code012(&s->pb, s->rl_table_index); + ff_msmpeg4_code012(&s->pb, s->rl_chroma_table_index); + ff_msmpeg4_code012(&s->pb, s->rl_table_index); } put_bits(&s->pb, 1, s->dc_table_index); } } else { put_bits(&s->pb, 1, s->use_skip_mb_code); - + if(s->msmpeg4_version==4 && s->bit_rate>MBAC_BITRATE) put_bits(&s->pb, 1, s->per_mb_rl_table); if(s->msmpeg4_version>2){ if(!s->per_mb_rl_table) - code012(&s->pb, s->rl_table_index); + ff_msmpeg4_code012(&s->pb, s->rl_table_index); put_bits(&s->pb, 1, s->dc_table_index); @@ -425,13 +366,13 @@ void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number) #ifdef DEBUG intra_count = 0; - printf("*****frame %d:\n", frame_count++); + av_log(s->avctx, AV_LOG_DEBUG, "*****frame %d:\n", frame_count++); #endif } void msmpeg4_encode_ext_header(MpegEncContext * s) { - put_bits(&s->pb, 5, s->avctx->frame_rate / s->avctx->frame_rate_base); //yes 29.97 -> 29 + put_bits(&s->pb, 5, s->avctx->time_base.den / s->avctx->time_base.num); //yes 29.97 -> 29 put_bits(&s->pb, 11, FFMIN(s->bit_rate/1024, 2047)); @@ -444,26 +385,26 @@ void msmpeg4_encode_ext_header(MpegEncContext * s) #endif //CONFIG_ENCODERS /* predict coded block */ -static inline int coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr) +int ff_msmpeg4_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->block_wrap[0]; + wrap = s->b8_stride; /* B C - * A X + * 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]; @@ -472,7 +413,7 @@ static inline int coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_bl #ifdef CONFIG_ENCODERS -static void msmpeg4_encode_motion(MpegEncContext * s, +void ff_msmpeg4_encode_motion(MpegEncContext * s, int mx, int my) { int code; @@ -489,29 +430,28 @@ static void msmpeg4_encode_motion(MpegEncContext * s, my += 64; else if (my >= 64) my -= 64; - + mx += 32; my += 32; #if 0 if ((unsigned)mx >= 64 || - (unsigned)my >= 64) - fprintf(stderr, "error mx=%d my=%d\n", mx, my); + (unsigned)my >= 64) + av_log(s->avctx, AV_LOG_ERROR, "error mx=%d my=%d\n", mx, my); #endif mv = &mv_tables[s->mv_table_index]; code = mv->table_mv_index[(mx << 6) | my]; - set_stat(ST_MV); - put_bits(&s->pb, - mv->table_mv_bits[code], + put_bits(&s->pb, + mv->table_mv_bits[code], mv->table_mv_code[code]); if (code == mv->n) { - /* escape : code litterally */ + /* escape : code literally */ put_bits(&s->pb, 6, mx); put_bits(&s->pb, 6, my); } } -static inline void handle_slices(MpegEncContext *s){ +void ff_msmpeg4_handle_slices(MpegEncContext *s){ if (s->mb_x == 0) { if (s->slice_height && (s->mb_y % s->slice_height) == 0) { if(s->msmpeg4_version < 4){ @@ -519,12 +459,12 @@ static inline void handle_slices(MpegEncContext *s){ } s->first_slice_line = 1; } else { - s->first_slice_line = 0; + s->first_slice_line = 0; } } } -void msmpeg4_encode_mb(MpegEncContext * s, +void msmpeg4_encode_mb(MpegEncContext * s, DCTELEM block[6][64], int motion_x, int motion_y) { @@ -532,113 +472,110 @@ void msmpeg4_encode_mb(MpegEncContext * s, int pred_x, pred_y; uint8_t *coded_block; - handle_slices(s); - + ff_msmpeg4_handle_slices(s); + if (!s->mb_intra) { - /* compute cbp */ - set_stat(ST_INTER_MB); - cbp = 0; - for (i = 0; i < 6; i++) { - if (s->block_last_index[i] >= 0) - cbp |= 1 << (5 - i); - } - if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) { - /* skip macroblock */ - put_bits(&s->pb, 1, 1); + /* compute cbp */ + cbp = 0; + for (i = 0; i < 6; i++) { + if (s->block_last_index[i] >= 0) + cbp |= 1 << (5 - i); + } + if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) { + /* skip macroblock */ + put_bits(&s->pb, 1, 1); s->last_bits++; - s->misc_bits++; + s->misc_bits++; s->skip_count++; - return; - } + return; + } if (s->use_skip_mb_code) - put_bits(&s->pb, 1, 0); /* mb coded */ - + put_bits(&s->pb, 1, 0); /* mb coded */ + if(s->msmpeg4_version<=2){ - put_bits(&s->pb, - v2_mb_type[cbp&3][1], + put_bits(&s->pb, + v2_mb_type[cbp&3][1], v2_mb_type[cbp&3][0]); if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C; else coded_cbp= cbp; - put_bits(&s->pb, - cbpy_tab[coded_cbp>>2][1], + put_bits(&s->pb, + cbpy_tab[coded_cbp>>2][1], cbpy_tab[coded_cbp>>2][0]); s->misc_bits += get_bits_diff(s); - h263_pred_motion(s, 0, &pred_x, &pred_y); + h263_pred_motion(s, 0, 0, &pred_x, &pred_y); msmpeg4v2_encode_motion(s, motion_x - pred_x); msmpeg4v2_encode_motion(s, motion_y - pred_y); }else{ - put_bits(&s->pb, - table_mb_non_intra[cbp + 64][1], + put_bits(&s->pb, + table_mb_non_intra[cbp + 64][1], table_mb_non_intra[cbp + 64][0]); s->misc_bits += get_bits_diff(s); /* motion vector */ - h263_pred_motion(s, 0, &pred_x, &pred_y); - msmpeg4_encode_motion(s, motion_x - pred_x, + h263_pred_motion(s, 0, 0, &pred_x, &pred_y); + ff_msmpeg4_encode_motion(s, motion_x - pred_x, motion_y - pred_y); } s->mv_bits += get_bits_diff(s); for (i = 0; i < 6; i++) { - msmpeg4_encode_block(s, block[i], i); + ff_msmpeg4_encode_block(s, block[i], i); } s->p_tex_bits += get_bits_diff(s); } else { - /* compute cbp */ - cbp = 0; + /* compute cbp */ + cbp = 0; coded_cbp = 0; - for (i = 0; i < 6; i++) { + for (i = 0; i < 6; i++) { int val, pred; val = (s->block_last_index[i] >= 1); cbp |= val << (5 - i); if (i < 4) { /* predict value for close blocks only for luma */ - pred = coded_block_pred(s, i, &coded_block); + pred = ff_msmpeg4_coded_block_pred(s, i, &coded_block); *coded_block = val; val = val ^ pred; } coded_cbp |= val << (5 - i); - } + } #if 0 if (coded_cbp) printf("cbp=%x %x\n", cbp, coded_cbp); #endif if(s->msmpeg4_version<=2){ - if (s->pict_type == I_TYPE) { - put_bits(&s->pb, + if (s->pict_type == FF_I_TYPE) { + put_bits(&s->pb, v2_intra_cbpc[cbp&3][1], v2_intra_cbpc[cbp&3][0]); } else { if (s->use_skip_mb_code) - put_bits(&s->pb, 1, 0); /* mb coded */ - put_bits(&s->pb, - v2_mb_type[(cbp&3) + 4][1], + put_bits(&s->pb, 1, 0); /* mb coded */ + put_bits(&s->pb, + v2_mb_type[(cbp&3) + 4][1], v2_mb_type[(cbp&3) + 4][0]); } - put_bits(&s->pb, 1, 0); /* no AC prediction yet */ - put_bits(&s->pb, - cbpy_tab[cbp>>2][1], + put_bits(&s->pb, 1, 0); /* no AC prediction yet */ + put_bits(&s->pb, + cbpy_tab[cbp>>2][1], cbpy_tab[cbp>>2][0]); }else{ - if (s->pict_type == I_TYPE) { - set_stat(ST_INTRA_MB); - put_bits(&s->pb, - table_mb_intra[coded_cbp][1], table_mb_intra[coded_cbp][0]); + if (s->pict_type == FF_I_TYPE) { + put_bits(&s->pb, + ff_msmp4_mb_i_table[coded_cbp][1], ff_msmp4_mb_i_table[coded_cbp][0]); } else { if (s->use_skip_mb_code) - put_bits(&s->pb, 1, 0); /* mb coded */ - put_bits(&s->pb, - table_mb_non_intra[cbp][1], + put_bits(&s->pb, 1, 0); /* mb coded */ + put_bits(&s->pb, + table_mb_non_intra[cbp][1], table_mb_non_intra[cbp][0]); } - set_stat(ST_INTRA_MB); - put_bits(&s->pb, 1, 0); /* no AC prediction yet */ + put_bits(&s->pb, 1, 0); /* no AC prediction yet */ if(s->inter_intra_pred){ s->h263_aic_dir=0; put_bits(&s->pb, table_inter_intra[s->h263_aic_dir][1], table_inter_intra[s->h263_aic_dir][0]); @@ -647,7 +584,7 @@ void msmpeg4_encode_mb(MpegEncContext * s, s->misc_bits += get_bits_diff(s); for (i = 0; i < 6; i++) { - msmpeg4_encode_block(s, block[i], i); + ff_msmpeg4_encode_block(s, block[i], i); } s->i_tex_bits += get_bits_diff(s); s->i_count++; @@ -656,22 +593,7 @@ void msmpeg4_encode_mb(MpegEncContext * s, #endif //CONFIG_ENCODERS -/* old ffmpeg msmpeg4v3 mode */ -static void ff_old_msmpeg4_dc_scale(MpegEncContext * s) -{ - if (s->qscale < 5){ - s->y_dc_scale = 8; - s->c_dc_scale = 8; - }else if (s->qscale < 9){ - s->y_dc_scale = 2 * s->qscale; - s->c_dc_scale = (s->qscale + 13)>>1; - }else{ - s->y_dc_scale = s->qscale + 8; - s->c_dc_scale = (s->qscale + 13)>>1; - } -} - -static inline int msmpeg4v1_pred_dc(MpegEncContext * s, int n, +static inline int msmpeg4v1_pred_dc(MpegEncContext * s, int n, int32_t **dc_val_ptr) { int i; @@ -681,9 +603,9 @@ static inline int msmpeg4v1_pred_dc(MpegEncContext * s, int n, } else { i= n-3; } - + *dc_val_ptr= &s->last_dc[i]; - return s->last_dc[i]; + return s->last_dc[i]; } static int get_dc(uint8_t *src, int stride, int scale) @@ -700,29 +622,29 @@ static int get_dc(uint8_t *src, int stride, int scale) } /* dir = 0: left, dir = 1: top prediction */ -static inline int msmpeg4_pred_dc(MpegEncContext * s, int n, - uint16_t **dc_val_ptr, int *dir_ptr) +static inline int msmpeg4_pred_dc(MpegEncContext * s, int n, + int16_t **dc_val_ptr, int *dir_ptr) { int a, b, c, wrap, pred, scale; int16_t *dc_val; /* find prediction */ if (n < 4) { - scale = s->y_dc_scale; + scale = s->y_dc_scale; } else { - scale = s->c_dc_scale; + scale = s->c_dc_scale; } - + wrap = s->block_wrap[n]; dc_val= s->dc_val[0] + s->block_index[n]; /* B C - * A X + * A X */ a = dc_val[ - 1]; b = dc_val[ - 1 - wrap]; c = dc_val[ - wrap]; - + if(s->first_slice_line && (n&2)==0 && s->msmpeg4_version<4){ b=c=1024; } @@ -731,24 +653,24 @@ static inline int msmpeg4_pred_dc(MpegEncContext * s, int n, necessitate to modify mpegvideo.c. The problem comes from the fact they decided to store the quantized DC (which would lead to problems if Q could vary !) */ -#if defined ARCH_X86 && !defined PIC +#if (defined(ARCH_X86)) && !defined PIC asm volatile( - "movl %3, %%eax \n\t" - "shrl $1, %%eax \n\t" - "addl %%eax, %2 \n\t" - "addl %%eax, %1 \n\t" - "addl %0, %%eax \n\t" - "mull %4 \n\t" - "movl %%edx, %0 \n\t" - "movl %1, %%eax \n\t" - "mull %4 \n\t" - "movl %%edx, %1 \n\t" - "movl %2, %%eax \n\t" - "mull %4 \n\t" - "movl %%edx, %2 \n\t" - : "+b" (a), "+c" (b), "+D" (c) - : "g" (scale), "S" (inverse[scale]) - : "%eax", "%edx" + "movl %3, %%eax \n\t" + "shrl $1, %%eax \n\t" + "addl %%eax, %2 \n\t" + "addl %%eax, %1 \n\t" + "addl %0, %%eax \n\t" + "mull %4 \n\t" + "movl %%edx, %0 \n\t" + "movl %1, %%eax \n\t" + "mull %4 \n\t" + "movl %%edx, %1 \n\t" + "movl %2, %%eax \n\t" + "mull %4 \n\t" + "movl %%edx, %2 \n\t" + : "+b" (a), "+c" (b), "+D" (c) + : "g" (scale), "S" (ff_inverse[scale]) + : "%eax", "%edx" ); #else /* #elif defined (ARCH_ALPHA) */ @@ -756,13 +678,13 @@ static inline int msmpeg4_pred_dc(MpegEncContext * s, int n, common case. But they are costly everywhere... */ if (scale == 8) { - a = (a + (8 >> 1)) / 8; - b = (b + (8 >> 1)) / 8; - c = (c + (8 >> 1)) / 8; + a = (a + (8 >> 1)) / 8; + b = (b + (8 >> 1)) / 8; + c = (c + (8 >> 1)) / 8; } else { - a = FASTDIV((a + (scale >> 1)), scale); - b = FASTDIV((b + (scale >> 1)), scale); - c = FASTDIV((c + (scale >> 1)), scale); + a = FASTDIV((a + (scale >> 1)), scale); + b = FASTDIV((b + (scale >> 1)), scale); + c = FASTDIV((c + (scale >> 1)), scale); } #endif /* XXX: WARNING: they did not choose the same test as MPEG4. This @@ -771,7 +693,7 @@ static inline int msmpeg4_pred_dc(MpegEncContext * s, int n, if(s->inter_intra_pred){ uint8_t *dest; int wrap; - + if(n==1){ pred=a; *dir_ptr = 0; @@ -798,7 +720,7 @@ static inline int msmpeg4_pred_dc(MpegEncContext * s, int n, else a= get_dc(dest-8, wrap, scale*8); if(s->mb_y==0) c= (1024 + (scale>>1))/scale; else c= get_dc(dest-8*wrap, wrap, scale*8); - + if (s->h263_aic_dir==0) { pred= a; *dir_ptr = 0; @@ -852,16 +774,17 @@ static inline int msmpeg4_pred_dc(MpegEncContext * s, int n, static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr) { int sign, code; - int pred; + int pred, extquant; + int extrabits = 0; if(s->msmpeg4_version==1){ int32_t *dc_val; pred = msmpeg4v1_pred_dc(s, n, &dc_val); - + /* update predictor */ *dc_val= level; }else{ - uint16_t *dc_val; + int16_t *dc_val; pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr); /* update predictor */ @@ -877,11 +800,11 @@ static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr if(s->msmpeg4_version<=2){ if (n < 4) { - put_bits(&s->pb, + put_bits(&s->pb, v2_dc_lum_table[level+256][1], v2_dc_lum_table[level+256][0]); }else{ - put_bits(&s->pb, + put_bits(&s->pb, v2_dc_chroma_table[level+256][1], v2_dc_chroma_table[level+256][0]); } @@ -892,26 +815,40 @@ static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr sign = 1; } code = level; - if (code > DC_MAX) + if (code > DC_MAX) code = DC_MAX; + else if( s->msmpeg4_version>=6 ) { + if( s->qscale == 1 ) { + extquant = (level + 3) & 0x3; + code = ((level+3)>>2); + } else if( s->qscale == 2 ) { + extquant = (level + 1) & 0x1; + code = ((level+1)>>1); + } + } if (s->dc_table_index == 0) { if (n < 4) { - put_bits(&s->pb, table0_dc_lum[code][1], table0_dc_lum[code][0]); + put_bits(&s->pb, ff_table0_dc_lum[code][1], ff_table0_dc_lum[code][0]); } else { - put_bits(&s->pb, table0_dc_chroma[code][1], table0_dc_chroma[code][0]); + put_bits(&s->pb, ff_table0_dc_chroma[code][1], ff_table0_dc_chroma[code][0]); } } else { if (n < 4) { - put_bits(&s->pb, table1_dc_lum[code][1], table1_dc_lum[code][0]); + put_bits(&s->pb, ff_table1_dc_lum[code][1], ff_table1_dc_lum[code][0]); } else { - put_bits(&s->pb, table1_dc_chroma[code][1], table1_dc_chroma[code][0]); + put_bits(&s->pb, ff_table1_dc_chroma[code][1], ff_table1_dc_chroma[code][0]); } } - + + if(s->msmpeg4_version>=6 && s->qscale<=2) + extrabits = 3 - s->qscale; + if (code == DC_MAX) - put_bits(&s->pb, 8, level); - + put_bits(&s->pb, 8 + extrabits, level); + else if(extrabits > 0)//== VC1 && s->qscale<=2 + put_bits(&s->pb, extrabits, extquant); + if (level != 0) { put_bits(&s->pb, 1, sign); } @@ -921,7 +858,7 @@ static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr /* Encoding of a block. Very similar to MPEG4 except for a different escape coding (same as H263) and more vlc tables. */ -static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n) +void ff_msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n) { int level, run, last, i, j, last_index; int last_non_zero, sign, slevel; @@ -930,7 +867,6 @@ static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int const uint8_t *scantable; if (s->mb_intra) { - set_stat(ST_DC); msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir); i = 1; if (n < 4) { @@ -938,9 +874,8 @@ static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int } else { rl = &rl_table[3 + s->rl_chroma_table_index]; } - run_diff = 0; + run_diff = s->msmpeg4_version>=4; scantable= s->intra_scantable.permutated; - set_stat(ST_INTRA_AC); } else { i = 0; rl = &rl_table[3 + s->rl_table_index]; @@ -949,11 +884,10 @@ static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int else run_diff = 1; scantable= s->inter_scantable.permutated; - set_stat(ST_INTER_AC); } /* recalculate block_last_index for M$ wmv1 */ - if(s->msmpeg4_version>=4 && s->block_last_index[n]>0){ + if(s->msmpeg4_version>=4 && s->msmpeg4_version<6 && s->block_last_index[n]>0){ for(last_index=63; last_index>=0; last_index--){ if(block[scantable[last_index]]) break; } @@ -963,17 +897,17 @@ static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int /* AC coefs */ last_non_zero = i - 1; for (; i <= last_index; i++) { - j = scantable[i]; - level = block[j]; - if (level) { - run = i - last_non_zero - 1; - last = (i == last_index); - sign = 0; - slevel = level; - if (level < 0) { - sign = 1; - level = -level; - } + j = scantable[i]; + level = block[j]; + if (level) { + run = i - last_non_zero - 1; + last = (i == last_index); + sign = 0; + slevel = level; + if (level < 0) { + sign = 1; + level = -level; + } if(level<=MAX_LEVEL && run<=MAX_RUN){ s->ac_stats[s->mb_intra][n>3][level][run][last]++; @@ -988,7 +922,7 @@ else int level1, run1; level1 = level - rl->max_level[last][run]; - if (level1 < 1) + if (level1 < 1) goto esc2; code = get_rl_index(rl, last, run, level1); if (code == rl->n) { @@ -999,6 +933,9 @@ else run1 = run - rl->max_run[last][level] - run_diff; if (run1 < 0) goto esc3; + code = get_rl_index(rl, last, run1+1, level); + if (s->msmpeg4_version == 4 && code == rl->n) + goto esc3; code = get_rl_index(rl, last, run1, level); if (code == rl->n) { esc3: @@ -1009,8 +946,9 @@ else if(s->esc3_level_length==0){ s->esc3_level_length=8; s->esc3_run_length= 6; + //ESCLVLSZ + ESCRUNSZ if(s->qscale<8) - put_bits(&s->pb, 6, 3); + put_bits(&s->pb, 6 + (s->msmpeg4_version>=6), 3); else put_bits(&s->pb, 8, 3); } @@ -1019,7 +957,7 @@ else put_bits(&s->pb, s->esc3_level_length, level); }else{ put_bits(&s->pb, 6, run); - put_bits(&s->pb, 8, slevel & 0xff); + put_sbits(&s->pb, 8, slevel); } } else { /* second escape */ @@ -1036,18 +974,15 @@ else } else { put_bits(&s->pb, 1, sign); } - last_non_zero = i; - } + last_non_zero = i; + } } } /****************************************/ /* decoding stuff */ -static VLC mb_non_intra_vlc[4]; -static VLC mb_intra_vlc; -static VLC dc_lum_vlc[2]; -static VLC dc_chroma_vlc[2]; +VLC ff_mb_non_intra_vlc[4]; static VLC v2_dc_lum_vlc; static VLC v2_dc_chroma_vlc; static VLC cbpy_vlc; @@ -1056,9 +991,10 @@ static VLC v2_mb_type_vlc; static VLC v2_mv_vlc; static VLC v1_intra_cbpc_vlc; static VLC v1_inter_cbpc_vlc; -static VLC inter_intra_vlc; +VLC ff_inter_intra_vlc; -/* this table is practically identical to the one from h263 except that its inverted */ +/* This table is practically identical to the one from h263 + * except that it is inverted. */ static void init_h263_dc_for_msmpeg4(void) { int level, uni_code, uni_len; @@ -1070,7 +1006,7 @@ static void init_h263_dc_for_msmpeg4(void) v = abs(level); while (v) { v >>= 1; - size++; + size++; } if (level < 0) @@ -1081,7 +1017,7 @@ static void init_h263_dc_for_msmpeg4(void) /* luminance h263 */ uni_code= DCtab_lum[size][0]; uni_len = DCtab_lum[size][1]; - uni_code ^= (1< 0) { uni_code<<=size; uni_code|=l; @@ -1097,8 +1033,8 @@ static void init_h263_dc_for_msmpeg4(void) /* chrominance h263 */ uni_code= DCtab_chrom[size][0]; uni_len = DCtab_chrom[size][1]; - uni_code ^= (1< 0) { uni_code<<=size; uni_code|=l; uni_len+=size; @@ -1126,71 +1062,76 @@ int ff_msmpeg4_decode_init(MpegEncContext *s) done = 1; for(i=0;ivlc, MV_VLC_BITS, mv->n + 1, + init_vlc(&mv->vlc, MV_VLC_BITS, mv->n + 1, mv->table_mv_bits, 1, 1, - mv->table_mv_code, 2, 2); + mv->table_mv_code, 2, 2, 1); } - init_vlc(&dc_lum_vlc[0], DC_VLC_BITS, 120, - &table0_dc_lum[0][1], 8, 4, - &table0_dc_lum[0][0], 8, 4); - init_vlc(&dc_chroma_vlc[0], DC_VLC_BITS, 120, - &table0_dc_chroma[0][1], 8, 4, - &table0_dc_chroma[0][0], 8, 4); - init_vlc(&dc_lum_vlc[1], DC_VLC_BITS, 120, - &table1_dc_lum[0][1], 8, 4, - &table1_dc_lum[0][0], 8, 4); - init_vlc(&dc_chroma_vlc[1], DC_VLC_BITS, 120, - &table1_dc_chroma[0][1], 8, 4, - &table1_dc_chroma[0][0], 8, 4); - - init_vlc(&v2_dc_lum_vlc, DC_VLC_BITS, 512, + init_vlc(&ff_msmp4_dc_luma_vlc[0], DC_VLC_BITS, 120, + &ff_table0_dc_lum[0][1], 8, 4, + &ff_table0_dc_lum[0][0], 8, 4, 1); + init_vlc(&ff_msmp4_dc_chroma_vlc[0], DC_VLC_BITS, 120, + &ff_table0_dc_chroma[0][1], 8, 4, + &ff_table0_dc_chroma[0][0], 8, 4, 1); + init_vlc(&ff_msmp4_dc_luma_vlc[1], DC_VLC_BITS, 120, + &ff_table1_dc_lum[0][1], 8, 4, + &ff_table1_dc_lum[0][0], 8, 4, 1); + init_vlc(&ff_msmp4_dc_chroma_vlc[1], DC_VLC_BITS, 120, + &ff_table1_dc_chroma[0][1], 8, 4, + &ff_table1_dc_chroma[0][0], 8, 4, 1); + + init_vlc(&v2_dc_lum_vlc, DC_VLC_BITS, 512, &v2_dc_lum_table[0][1], 8, 4, - &v2_dc_lum_table[0][0], 8, 4); - init_vlc(&v2_dc_chroma_vlc, DC_VLC_BITS, 512, + &v2_dc_lum_table[0][0], 8, 4, 1); + init_vlc(&v2_dc_chroma_vlc, DC_VLC_BITS, 512, &v2_dc_chroma_table[0][1], 8, 4, - &v2_dc_chroma_table[0][0], 8, 4); - + &v2_dc_chroma_table[0][0], 8, 4, 1); + init_vlc(&cbpy_vlc, CBPY_VLC_BITS, 16, &cbpy_tab[0][1], 2, 1, - &cbpy_tab[0][0], 2, 1); + &cbpy_tab[0][0], 2, 1, 1); init_vlc(&v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 4, &v2_intra_cbpc[0][1], 2, 1, - &v2_intra_cbpc[0][0], 2, 1); + &v2_intra_cbpc[0][0], 2, 1, 1); init_vlc(&v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 8, &v2_mb_type[0][1], 2, 1, - &v2_mb_type[0][0], 2, 1); + &v2_mb_type[0][0], 2, 1, 1); init_vlc(&v2_mv_vlc, V2_MV_VLC_BITS, 33, &mvtab[0][1], 2, 1, - &mvtab[0][0], 2, 1); + &mvtab[0][0], 2, 1, 1); for(i=0; i<4; i++){ - init_vlc(&mb_non_intra_vlc[i], MB_NON_INTRA_VLC_BITS, 128, + init_vlc(&ff_mb_non_intra_vlc[i], MB_NON_INTRA_VLC_BITS, 128, &wmv2_inter_table[i][0][1], 8, 4, - &wmv2_inter_table[i][0][0], 8, 4); //FIXME name? + &wmv2_inter_table[i][0][0], 8, 4, 1); //FIXME name? } - - init_vlc(&mb_intra_vlc, MB_INTRA_VLC_BITS, 64, - &table_mb_intra[0][1], 4, 2, - &table_mb_intra[0][0], 4, 2); - - init_vlc(&v1_intra_cbpc_vlc, V1_INTRA_CBPC_VLC_BITS, 8, + + 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); + + init_vlc(&v1_intra_cbpc_vlc, V1_INTRA_CBPC_VLC_BITS, 8, intra_MCBPC_bits, 1, 1, - intra_MCBPC_code, 1, 1); - init_vlc(&v1_inter_cbpc_vlc, V1_INTER_CBPC_VLC_BITS, 25, + intra_MCBPC_code, 1, 1, 1); + init_vlc(&v1_inter_cbpc_vlc, V1_INTER_CBPC_VLC_BITS, 25, inter_MCBPC_bits, 1, 1, - inter_MCBPC_code, 1, 1); - - init_vlc(&inter_intra_vlc, INTER_INTRA_VLC_BITS, 4, + inter_MCBPC_code, 1, 1, 1); + + init_vlc(&ff_inter_intra_vlc, INTER_INTRA_VLC_BITS, 4, &table_inter_intra[0][1], 2, 1, - &table_inter_intra[0][0], 2, 1); + &table_inter_intra[0][0], 2, 1, 1); } - + switch(s->msmpeg4_version){ case 1: case 2: @@ -1201,23 +1142,16 @@ int ff_msmpeg4_decode_init(MpegEncContext *s) s->decode_mb= msmpeg4v34_decode_mb; break; case 5: - s->decode_mb= wmv2_decode_mb; + if (ENABLE_WMV2_DECODER) + s->decode_mb= ff_wmv2_decode_mb; + case 6: + //FIXME + TODO VC1 decode mb break; } - - s->slice_height= s->mb_height; //to avoid 1/0 if the first frame isnt a keyframe - - return 0; -} -static int decode012(GetBitContext *gb) -{ - int n; - n = get_bits1(gb); - if (n == 0) - return 0; - else - return get_bits1(gb) + 1; + s->slice_height= s->mb_height; //to avoid 1/0 if the first frame is not a keyframe + + return 0; } int msmpeg4_decode_picture_header(MpegEncContext * s) @@ -1228,9 +1162,9 @@ int msmpeg4_decode_picture_header(MpegEncContext * s) { int i; for(i=0; igb.size_in_bits; i++) - printf("%d", get_bits1(&s->gb)); + av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb)); // get_bits1(&s->gb); -printf("END\n"); +av_log(s->avctx, AV_LOG_DEBUG, "END\n"); return -1; } #endif @@ -1247,15 +1181,15 @@ return -1; } s->pict_type = get_bits(&s->gb, 2) + 1; - if (s->pict_type != I_TYPE && - s->pict_type != P_TYPE){ + if (s->pict_type != FF_I_TYPE && + s->pict_type != FF_P_TYPE){ av_log(s->avctx, AV_LOG_ERROR, "invalid picture type\n"); return -1; } #if 0 { static int had_i=0; - if(s->pict_type == I_TYPE) had_i=1; + if(s->pict_type == FF_I_TYPE) had_i=1; if(!had_i) return -1; } #endif @@ -1265,8 +1199,8 @@ return -1; return -1; } - if (s->pict_type == I_TYPE) { - code = get_bits(&s->gb, 5); + if (s->pict_type == FF_I_TYPE) { + code = get_bits(&s->gb, 5); if(s->msmpeg4_version==1){ if(code==0 || code>s->mb_height){ av_log(s->avctx, AV_LOG_ERROR, "invalid slice height %d\n", code); @@ -1303,7 +1237,7 @@ return -1; if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb); else s->per_mb_rl_table= 0; - + if(!s->per_mb_rl_table){ s->rl_chroma_table_index = decode012(&s->gb); s->rl_table_index = decode012(&s->gb); @@ -1315,11 +1249,11 @@ return -1; } s->no_rounding = 1; if(s->avctx->debug&FF_DEBUG_PICT_INFO) - av_log(s->avctx, AV_LOG_DEBUG, "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d slice:%d \n", - s->qscale, - s->rl_chroma_table_index, - s->rl_table_index, - s->dc_table_index, + av_log(s->avctx, AV_LOG_DEBUG, "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d slice:%d \n", + s->qscale, + s->rl_chroma_table_index, + s->rl_table_index, + s->dc_table_index, s->per_mb_rl_table, s->slice_height); } else { @@ -1361,22 +1295,22 @@ return -1; s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE); break; } - + if(s->avctx->debug&FF_DEBUG_PICT_INFO) - av_log(s->avctx, AV_LOG_DEBUG, "skip:%d rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d \n", - s->use_skip_mb_code, - s->rl_table_index, - s->rl_chroma_table_index, - s->dc_table_index, - s->mv_table_index, + av_log(s->avctx, AV_LOG_DEBUG, "skip:%d rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d \n", + s->use_skip_mb_code, + s->rl_table_index, + s->rl_chroma_table_index, + s->dc_table_index, + s->mv_table_index, s->per_mb_rl_table, s->qscale); - if(s->flipflop_rounding){ - s->no_rounding ^= 1; - }else{ - s->no_rounding = 0; - } + if(s->flipflop_rounding){ + s->no_rounding ^= 1; + }else{ + s->no_rounding = 0; + } } //printf("%d %d %d %d %d\n", s->pict_type, s->bit_rate, s->inter_intra_pred, s->width, s->height); @@ -1384,7 +1318,7 @@ return -1; s->esc3_run_length= 0; #ifdef DEBUG - printf("*****frame %d:\n", frame_count++); + av_log(s->avctx, AV_LOG_DEBUG, "*****frame %d:\n", frame_count++); #endif return 0; } @@ -1428,6 +1362,7 @@ static inline void msmpeg4_memsetw(short *tab, int val, int n) tab[i] = val; } +#ifdef CONFIG_ENCODERS static void msmpeg4v2_encode_motion(MpegEncContext * s, int val) { int range, bit_size, sign, code, bits; @@ -1454,14 +1389,15 @@ static void msmpeg4v2_encode_motion(MpegEncContext * s, int val) code = (val >> bit_size) + 1; bits = val & (range - 1); - put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign); + put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign); if (bit_size > 0) { put_bits(&s->pb, bit_size, bits); } } } +#endif -/* this is identical to h263 except that its range is multiplied by 2 */ +/* This is identical to h263 except that its range is multiplied by 2. */ static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code) { int code, val, sign, shift; @@ -1496,8 +1432,8 @@ static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code) static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64]) { int cbp, code, i; - - if (s->pict_type == P_TYPE) { + + if (s->pict_type == FF_P_TYPE) { if (s->use_skip_mb_code) { if (get_bits1(&s->gb)) { /* skip mb */ @@ -1508,7 +1444,7 @@ static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64]) s->mv_type = MV_TYPE_16X16; s->mv[0][0][0] = 0; s->mv[0][0][1] = 0; - s->mb_skiped = 1; + s->mb_skipped = 1; return 0; } } @@ -1523,7 +1459,7 @@ static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64]) } s->mb_intra = code >>2; - + cbp = code & 0x3; } else { s->mb_intra = 1; @@ -1539,7 +1475,7 @@ static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64]) if (!s->mb_intra) { int mx, my, cbpy; - + cbpy= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1); if(cbpy<0){ av_log(s->avctx, AV_LOG_ERROR, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y); @@ -1548,11 +1484,11 @@ static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64]) cbp|= cbpy<<2; if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C; - - h263_pred_motion(s, 0, &mx, &my); + + h263_pred_motion(s, 0, 0, &mx, &my); mx= msmpeg4v2_decode_motion(s, mx, 1); my= msmpeg4v2_decode_motion(s, my, 1); - + s->mv_dir = MV_DIR_FORWARD; s->mv_type = MV_TYPE_16X16; s->mv[0][0][0] = mx; @@ -1564,16 +1500,17 @@ static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64]) } else{ s->ac_pred = 0; cbp|= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors - if(s->pict_type==P_TYPE) cbp^=0x3C; + if(s->pict_type==FF_P_TYPE) cbp^=0x3C; } } + s->dsp.clear_blocks(s->block[0]); for (i = 0; i < 6; i++) { - if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0) - { + if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0) + { av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); return -1; - } + } } return 0; } @@ -1584,8 +1521,7 @@ static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64]) uint8_t *coded_val; uint32_t * const mb_type_ptr= &s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]; - if (s->pict_type == P_TYPE) { - set_stat(ST_INTER_MB); + if (s->pict_type == FF_P_TYPE) { if (s->use_skip_mb_code) { if (get_bits1(&s->gb)) { /* skip mb */ @@ -1596,24 +1532,23 @@ static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64]) s->mv_type = MV_TYPE_16X16; s->mv[0][0][0] = 0; s->mv[0][0][1] = 0; - s->mb_skiped = 1; + s->mb_skipped = 1; *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16; return 0; } } - - code = get_vlc2(&s->gb, mb_non_intra_vlc[DEFAULT_INTER_INDEX].table, MB_NON_INTRA_VLC_BITS, 3); + + code = get_vlc2(&s->gb, ff_mb_non_intra_vlc[DEFAULT_INTER_INDEX].table, MB_NON_INTRA_VLC_BITS, 3); if (code < 0) return -1; - //s->mb_intra = (code & 0x40) ? 0 : 1; - s->mb_intra = (~code & 0x40) >> 6; - + //s->mb_intra = (code & 0x40) ? 0 : 1; + s->mb_intra = (~code & 0x40) >> 6; + cbp = code & 0x3f; } else { - set_stat(ST_INTRA_MB); s->mb_intra = 1; - code = get_vlc2(&s->gb, mb_intra_vlc.table, MB_INTRA_VLC_BITS, 2); + code = get_vlc2(&s->gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2); if (code < 0) return -1; /* predict coded block pattern */ @@ -1621,7 +1556,7 @@ static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64]) for(i=0;i<6;i++) { int val = ((code >> (5 - i)) & 1); if (i < 4) { - int pred = coded_block_pred(s, i, &coded_val); + int pred = ff_msmpeg4_coded_block_pred(s, i, &coded_val); val = val ^ pred; *coded_val = val; } @@ -1636,9 +1571,8 @@ static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64]) s->rl_table_index = decode012(&s->gb); s->rl_chroma_table_index = s->rl_table_index; } - set_stat(ST_MV); - h263_pred_motion(s, 0, &mx, &my); - if (msmpeg4_decode_motion(s, &mx, &my) < 0) + h263_pred_motion(s, 0, 0, &mx, &my); + if (ff_msmpeg4_decode_motion(s, &mx, &my) < 0) return -1; s->mv_dir = MV_DIR_FORWARD; s->mv_type = MV_TYPE_16X16; @@ -1647,11 +1581,10 @@ static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64]) *mb_type_ptr = MB_TYPE_L0 | MB_TYPE_16x16; } else { //printf("I at %d %d %d %06X\n", s->mb_x, s->mb_y, ((cbp&3)? 1 : 0) +((cbp&0x3C)? 2 : 0), show_bits(&s->gb, 24)); - set_stat(ST_INTRA_MB); s->ac_pred = get_bits1(&s->gb); *mb_type_ptr = MB_TYPE_INTRA; if(s->inter_intra_pred){ - s->h263_aic_dir= get_vlc2(&s->gb, inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1); + s->h263_aic_dir= get_vlc2(&s->gb, ff_inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1); // printf("%d%d %d %d/", s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y); } if(s->per_mb_rl_table && cbp){ @@ -1660,18 +1593,19 @@ static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64]) } } + s->dsp.clear_blocks(s->block[0]); for (i = 0; i < 6; i++) { - if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0) - { - av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); - return -1; - } + if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0) + { + av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); + return -1; + } } - + return 0; } //#define ERROR_DETAILS -static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, +int ff_msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, int n, int coded, const uint8_t *scan_table) { int level, i, last, run, run_diff; @@ -1684,10 +1618,9 @@ static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, qmul=1; qadd=0; - /* DC coef */ - set_stat(ST_DC); + /* DC coef */ level = msmpeg4_decode_dc(s, n, &dc_pred_dir); - + if (level < 0){ av_log(s->avctx, AV_LOG_ERROR, "dc overflow- block: %d qscale: %d//\n", n, s->qscale); if(s->inter_intra_pred) level=0; @@ -1708,20 +1641,19 @@ static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, } block[0] = level; - run_diff = 0; + run_diff = s->msmpeg4_version >= 4; i = 0; if (!coded) { goto not_coded; } if (s->ac_pred) { - if (dc_pred_dir == 0) + if (dc_pred_dir == 0) scan_table = s->intra_v_scantable.permutated; /* left */ else scan_table = s->intra_h_scantable.permutated; /* top */ } else { scan_table = s->intra_scantable.permutated; } - set_stat(ST_INTRA_AC); rl_vlc= rl->rl_vlc[0]; } else { qmul = s->qscale << 1; @@ -1740,14 +1672,13 @@ static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, } if(!scan_table) scan_table = s->inter_scantable.permutated; - set_stat(ST_INTER_AC); rl_vlc= rl->rl_vlc[s->qscale]; } { OPEN_READER(re, &s->gb); for(;;) { UPDATE_CACHE(re, &s->gb); - GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2); + GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0); if (level==0) { int cache; cache= GET_CACHE(re, &s->gb); @@ -1762,7 +1693,7 @@ static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, run= SHOW_UBITS(re, &s->gb, 6); SKIP_CACHE(re, &s->gb, 6); level= SHOW_SBITS(re, &s->gb, 8); LAST_SKIP_CACHE(re, &s->gb, 8); SKIP_COUNTER(re, &s->gb, 1+6+8); - }else{ + }else{ int sign; last= SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1); if(!s->esc3_level_length){ @@ -1789,43 +1720,43 @@ static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, //printf("level length:%d, run length: %d\n", ll, s->esc3_run_length); UPDATE_CACHE(re, &s->gb); } - run= SHOW_UBITS(re, &s->gb, s->esc3_run_length); + run= SHOW_UBITS(re, &s->gb, s->esc3_run_length); SKIP_BITS(re, &s->gb, s->esc3_run_length); - - sign= SHOW_UBITS(re, &s->gb, 1); + + sign= SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1); - - level= SHOW_UBITS(re, &s->gb, s->esc3_level_length); + + level= SHOW_UBITS(re, &s->gb, s->esc3_level_length); SKIP_BITS(re, &s->gb, s->esc3_level_length); if(sign) level= -level; } //printf("level: %d, run: %d at %d %d\n", level, run, s->mb_x, s->mb_y); #if 0 // waste of time / this will detect very few errors { - const int abs_level= ABS(level); + const int abs_level= FFABS(level); const int run1= run - rl->max_run[last][abs_level] - run_diff; if(abs_level<=MAX_LEVEL && run<=MAX_RUN){ if(abs_level <= rl->max_level[last][run]){ - fprintf(stderr, "illegal 3. esc, vlc encoding possible\n"); + av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n"); return DECODING_AC_LOST; } if(abs_level <= rl->max_level[last][run]*2){ - fprintf(stderr, "illegal 3. esc, esc 1 encoding possible\n"); + av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n"); return DECODING_AC_LOST; } if(run1>=0 && abs_level <= rl->max_level[last][run1]){ - fprintf(stderr, "illegal 3. esc, esc 2 encoding possible\n"); + av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n"); return DECODING_AC_LOST; } } } #endif - //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ; - if (level>0) level= level * qmul + qadd; + //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ; + if (level>0) level= level * qmul + qadd; else level= level * qmul - qadd; #if 0 // waste of time too :( if(level>2048 || level<-2048){ - fprintf(stderr, "|level| overflow in 3. esc\n"); + av_log(s->avctx, AV_LOG_ERROR, "|level| overflow in 3. esc\n"); return DECODING_AC_LOST; } #endif @@ -1833,9 +1764,9 @@ static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, if(last) i+=192; #ifdef ERROR_DETAILS if(run==66) - fprintf(stderr, "illegal vlc code in ESC3 level=%d\n", level); + av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC3 level=%d\n", level); else if((i>62 && i<192) || i>192+63) - fprintf(stderr, "run overflow in ESC3 i=%d run=%d level=%d\n", i, run, level); + av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC3 i=%d run=%d level=%d\n", i, run, level); #endif } else { /* second escape */ @@ -1845,15 +1776,15 @@ static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, #else SKIP_BITS(re, &s->gb, 2); #endif - GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2); + GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1); i+= run + rl->max_run[run>>7][level/qmul] + run_diff; //FIXME opt indexing level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); LAST_SKIP_BITS(re, &s->gb, 1); #ifdef ERROR_DETAILS if(run==66) - fprintf(stderr, "illegal vlc code in ESC2 level=%d\n", level); + av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC2 level=%d\n", level); else if((i>62 && i<192) || i>192+63) - fprintf(stderr, "run overflow in ESC2 i=%d run=%d level=%d\n", i, run, level); + av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC2 i=%d run=%d level=%d\n", i, run, level); #endif } } else { @@ -1864,16 +1795,16 @@ static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, #else SKIP_BITS(re, &s->gb, 1); #endif - GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2); + GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1); i+= run; level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); LAST_SKIP_BITS(re, &s->gb, 1); #ifdef ERROR_DETAILS if(run==66) - fprintf(stderr, "illegal vlc code in ESC1 level=%d\n", level); + av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC1 level=%d\n", level); else if((i>62 && i<192) || i>192+63) - fprintf(stderr, "run overflow in ESC1 i=%d run=%d level=%d\n", i, run, level); + av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC1 i=%d run=%d level=%d\n", i, run, level); #endif } } else { @@ -1882,9 +1813,9 @@ static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, LAST_SKIP_BITS(re, &s->gb, 1); #ifdef ERROR_DETAILS if(run==66) - fprintf(stderr, "illegal vlc code level=%d\n", level); + av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code level=%d\n", level); else if((i>62 && i<192) || i>192+63) - fprintf(stderr, "run overflow i=%d run=%d level=%d\n", i, run, level); + av_log(s->avctx, AV_LOG_ERROR, "run overflow i=%d run=%d level=%d\n", i, run, level); #endif } if (i > 62){ @@ -1917,7 +1848,7 @@ static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, } if(s->msmpeg4_version>=4 && i>0) i=63; //FIXME/XXX optimize s->block_last_index[n] = i; - + return 0; } @@ -1931,14 +1862,14 @@ static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr) } else { level = get_vlc2(&s->gb, v2_dc_chroma_vlc.table, DC_VLC_BITS, 3); } - if (level < 0) + if (level < 0) return -1; level-=256; }else{ //FIXME optimize use unified tables & index if (n < 4) { - level = get_vlc2(&s->gb, dc_lum_vlc[s->dc_table_index].table, DC_VLC_BITS, 3); + level = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3); } else { - level = get_vlc2(&s->gb, dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3); + level = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3); } if (level < 0){ av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n"); @@ -1959,11 +1890,11 @@ static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr) int32_t *dc_val; pred = msmpeg4v1_pred_dc(s, n, &dc_val); level += pred; - + /* update predictor */ *dc_val= level; }else{ - uint16_t *dc_val; + int16_t *dc_val; pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr); level += pred; @@ -1978,7 +1909,7 @@ static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr) return level; } -static int msmpeg4_decode_motion(MpegEncContext * s, +int ff_msmpeg4_decode_motion(MpegEncContext * s, int *mx_ptr, int *my_ptr) { MVTable *mv; @@ -2016,9 +1947,3 @@ static int msmpeg4_decode_motion(MpegEncContext * s, *my_ptr = my; return 0; } - -/* cleanest way to support it - * there is too much shared between versions so that we cant have 1 file per version & 1 common - * as allmost everything would be in the common file - */ -#include "wmv2.c" diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/msmpeg4.h b/src/add-ons/media/plugins/avcodec/libavcodec/msmpeg4.h new file mode 100644 index 0000000000..17288b8e28 --- /dev/null +++ b/src/add-ons/media/plugins/avcodec/libavcodec/msmpeg4.h @@ -0,0 +1,65 @@ +/* + * MSMPEG4 backend for ffmpeg encoder and decoder + * copyright (c) 2007 Aurelien Jacobs + * + * This file is part of FFmpeg. + * + * FFmpeg 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.1 of the License, or (at your option) any later version. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file msmpeg4.h + */ + +#ifndef FFMPEG_MSMPEG4_H +#define FFMPEG_MSMPEG4_H + +#include "config.h" +#include "avcodec.h" +#include "dsputil.h" +#include "mpegvideo.h" + +#define INTER_INTRA_VLC_BITS 3 +#define MB_NON_INTRA_VLC_BITS 9 +#define MB_INTRA_VLC_BITS 9 + +extern VLC ff_mb_non_intra_vlc[4]; +extern VLC ff_inter_intra_vlc; + +void ff_msmpeg4_code012(PutBitContext *pb, int n); +void ff_msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n); +void ff_msmpeg4_handle_slices(MpegEncContext *s); +void ff_msmpeg4_encode_motion(MpegEncContext * s, int mx, int my); +int ff_msmpeg4_coded_block_pred(MpegEncContext * s, int n, + uint8_t **coded_block_ptr); +int ff_msmpeg4_decode_motion(MpegEncContext * s, int *mx_ptr, int *my_ptr); +int ff_msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, + int n, int coded, const uint8_t *scan_table); +int ff_wmv2_decode_mb(MpegEncContext *s, DCTELEM block[6][64]); + +#define ENABLE_MSMPEG4_DECODER (ENABLE_MSMPEG4V1_DECODER || \ + ENABLE_MSMPEG4V2_DECODER || \ + ENABLE_MSMPEG4V3_DECODER || \ + ENABLE_WMV2_DECODER) +#define ENABLE_MSMPEG4_ENCODER (ENABLE_MSMPEG4V1_ENCODER || \ + ENABLE_MSMPEG4V2_ENCODER || \ + ENABLE_MSMPEG4V3_ENCODER || \ + ENABLE_WMV2_ENCODER) +#define ENABLE_MSMPEG4 (ENABLE_MSMPEG4_DECODER || ENABLE_MSMPEG4_ENCODER) +#define ENABLE_WMV2 (ENABLE_WMV2_DECODER || ENABLE_WMV2_ENCODER) +#define ENABLE_WMV_DECODER (ENABLE_WMV1_DECODER || ENABLE_WMV2_DECODER) +#define ENABLE_WMV_ENCODER (ENABLE_WMV1_ENCODER || ENABLE_WMV2_ENCODER) + +#endif /* FFMPEG_MSMPEG4_H */ diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/msmpeg4data.c b/src/add-ons/media/plugins/avcodec/libavcodec/msmpeg4data.c new file mode 100644 index 0000000000..c0bb953bc5 --- /dev/null +++ b/src/add-ons/media/plugins/avcodec/libavcodec/msmpeg4data.c @@ -0,0 +1,2001 @@ +/* + * MSMPEG4 backend for ffmpeg encoder and decoder + * copyright (c) 2001 Fabrice Bellard + * copyright (c) 2002-2004 Michael Niedermayer + * + * msmpeg4v1 & v2 stuff by Michael Niedermayer + * + * This file is part of FFmpeg. + * + * FFmpeg 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.1 of the License, or (at your option) any later version. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file msmpeg4data.c + * MSMPEG4 data tables. + */ + +#include "msmpeg4data.h" + +VLC ff_msmp4_mb_i_vlc; +VLC ff_msmp4_dc_luma_vlc[2]; +VLC ff_msmp4_dc_chroma_vlc[2]; + +/* intra picture macro block coded block pattern */ +const uint16_t ff_msmp4_mb_i_table[64][2] = { +{ 0x1, 1 },{ 0x17, 6 },{ 0x9, 5 },{ 0x5, 5 }, +{ 0x6, 5 },{ 0x47, 9 },{ 0x20, 7 },{ 0x10, 7 }, +{ 0x2, 5 },{ 0x7c, 9 },{ 0x3a, 7 },{ 0x1d, 7 }, +{ 0x2, 6 },{ 0xec, 9 },{ 0x77, 8 },{ 0x0, 8 }, +{ 0x3, 5 },{ 0xb7, 9 },{ 0x2c, 7 },{ 0x13, 7 }, +{ 0x1, 6 },{ 0x168, 10 },{ 0x46, 8 },{ 0x3f, 8 }, +{ 0x1e, 6 },{ 0x712, 13 },{ 0xb5, 9 },{ 0x42, 8 }, +{ 0x22, 7 },{ 0x1c5, 11 },{ 0x11e, 10 },{ 0x87, 9 }, +{ 0x6, 4 },{ 0x3, 9 },{ 0x1e, 7 },{ 0x1c, 6 }, +{ 0x12, 7 },{ 0x388, 12 },{ 0x44, 9 },{ 0x70, 9 }, +{ 0x1f, 6 },{ 0x23e, 11 },{ 0x39, 8 },{ 0x8e, 9 }, +{ 0x1, 7 },{ 0x1c6, 11 },{ 0xb6, 9 },{ 0x45, 9 }, +{ 0x14, 6 },{ 0x23f, 11 },{ 0x7d, 9 },{ 0x18, 9 }, +{ 0x7, 7 },{ 0x1c7, 11 },{ 0x86, 9 },{ 0x19, 9 }, +{ 0x15, 6 },{ 0x1db, 10 },{ 0x2, 9 },{ 0x46, 9 }, +{ 0xd, 8 },{ 0x713, 13 },{ 0x1da, 10 },{ 0x169, 10 }, +}; + +/* non intra picture macro block coded block pattern + mb type */ +const uint32_t table_mb_non_intra[128][2] = { +{ 0x40, 7 },{ 0x13c9, 13 },{ 0x9fd, 12 },{ 0x1fc, 15 }, +{ 0x9fc, 12 },{ 0xa83, 18 },{ 0x12d34, 17 },{ 0x83bc, 16 }, +{ 0x83a, 12 },{ 0x7f8, 17 },{ 0x3fd, 16 },{ 0x3ff, 16 }, +{ 0x79, 13 },{ 0xa82, 18 },{ 0x969d, 16 },{ 0x2a4, 16 }, +{ 0x978, 12 },{ 0x543, 17 },{ 0x41df, 15 },{ 0x7f9, 17 }, +{ 0x12f3, 13 },{ 0x25a6b, 18 },{ 0x25ef9, 18 },{ 0x3fa, 16 }, +{ 0x20ee, 14 },{ 0x969ab, 20 },{ 0x969c, 16 },{ 0x25ef8, 18 }, +{ 0x12d2, 13 },{ 0xa85, 18 },{ 0x969e, 16 },{ 0x4bc8, 15 }, +{ 0x3d, 12 },{ 0x12f7f, 17 },{ 0x2a2, 16 },{ 0x969f, 16 }, +{ 0x25ee, 14 },{ 0x12d355, 21 },{ 0x12f7d, 17 },{ 0x12f7e, 17 }, +{ 0x9e5, 12 },{ 0xa81, 18 },{ 0x4b4d4, 19 },{ 0x83bd, 16 }, +{ 0x78, 13 },{ 0x969b, 16 },{ 0x3fe, 16 },{ 0x2a5, 16 }, +{ 0x7e, 13 },{ 0xa80, 18 },{ 0x2a3, 16 },{ 0x3fb, 16 }, +{ 0x1076, 13 },{ 0xa84, 18 },{ 0x153, 15 },{ 0x4bc9, 15 }, +{ 0x55, 13 },{ 0x12d354, 21 },{ 0x4bde, 15 },{ 0x25e5, 14 }, +{ 0x25b, 10 },{ 0x4b4c, 15 },{ 0x96b, 12 },{ 0x96a, 12 }, +{ 0x1, 2 },{ 0x0, 7 },{ 0x26, 6 },{ 0x12b, 9 }, +{ 0x7, 3 },{ 0x20f, 10 },{ 0x4, 9 },{ 0x28, 12 }, +{ 0x6, 3 },{ 0x20a, 10 },{ 0x128, 9 },{ 0x2b, 12 }, +{ 0x11, 5 },{ 0x1b, 11 },{ 0x13a, 9 },{ 0x4ff, 11 }, +{ 0x3, 4 },{ 0x277, 10 },{ 0x106, 9 },{ 0x839, 12 }, +{ 0xb, 4 },{ 0x27b, 10 },{ 0x12c, 9 },{ 0x4bf, 11 }, +{ 0x9, 6 },{ 0x35, 12 },{ 0x27e, 10 },{ 0x13c8, 13 }, +{ 0x1, 6 },{ 0x4aa, 11 },{ 0x208, 10 },{ 0x29, 12 }, +{ 0x1, 4 },{ 0x254, 10 },{ 0x12e, 9 },{ 0x838, 12 }, +{ 0x24, 6 },{ 0x4f3, 11 },{ 0x276, 10 },{ 0x12f6, 13 }, +{ 0x1, 5 },{ 0x27a, 10 },{ 0x13e, 9 },{ 0x3e, 12 }, +{ 0x8, 6 },{ 0x413, 11 },{ 0xc, 10 },{ 0x4be, 11 }, +{ 0x14, 5 },{ 0x412, 11 },{ 0x253, 10 },{ 0x97a, 12 }, +{ 0x21, 6 },{ 0x4ab, 11 },{ 0x20b, 10 },{ 0x34, 12 }, +{ 0x15, 5 },{ 0x278, 10 },{ 0x252, 10 },{ 0x968, 12 }, +{ 0x5, 5 },{ 0xb, 10 },{ 0x9c, 8 },{ 0xe, 10 }, +}; + +/* dc table 0 */ + +const uint32_t ff_table0_dc_lum[120][2] = { +{ 0x1, 1 },{ 0x1, 2 },{ 0x1, 4 },{ 0x1, 5 }, +{ 0x5, 5 },{ 0x7, 5 },{ 0x8, 6 },{ 0xc, 6 }, +{ 0x0, 7 },{ 0x2, 7 },{ 0x12, 7 },{ 0x1a, 7 }, +{ 0x3, 8 },{ 0x7, 8 },{ 0x27, 8 },{ 0x37, 8 }, +{ 0x5, 9 },{ 0x4c, 9 },{ 0x6c, 9 },{ 0x6d, 9 }, +{ 0x8, 10 },{ 0x19, 10 },{ 0x9b, 10 },{ 0x1b, 10 }, +{ 0x9a, 10 },{ 0x13, 11 },{ 0x34, 11 },{ 0x35, 11 }, +{ 0x61, 12 },{ 0x48, 13 },{ 0xc4, 13 },{ 0x4a, 13 }, +{ 0xc6, 13 },{ 0xc7, 13 },{ 0x92, 14 },{ 0x18b, 14 }, +{ 0x93, 14 },{ 0x183, 14 },{ 0x182, 14 },{ 0x96, 14 }, +{ 0x97, 14 },{ 0x180, 14 },{ 0x314, 15 },{ 0x315, 15 }, +{ 0x605, 16 },{ 0x604, 16 },{ 0x606, 16 },{ 0xc0e, 17 }, +{ 0x303cd, 23 },{ 0x303c9, 23 },{ 0x303c8, 23 },{ 0x303ca, 23 }, +{ 0x303cb, 23 },{ 0x303cc, 23 },{ 0x303ce, 23 },{ 0x303cf, 23 }, +{ 0x303d0, 23 },{ 0x303d1, 23 },{ 0x303d2, 23 },{ 0x303d3, 23 }, +{ 0x303d4, 23 },{ 0x303d5, 23 },{ 0x303d6, 23 },{ 0x303d7, 23 }, +{ 0x303d8, 23 },{ 0x303d9, 23 },{ 0x303da, 23 },{ 0x303db, 23 }, +{ 0x303dc, 23 },{ 0x303dd, 23 },{ 0x303de, 23 },{ 0x303df, 23 }, +{ 0x303e0, 23 },{ 0x303e1, 23 },{ 0x303e2, 23 },{ 0x303e3, 23 }, +{ 0x303e4, 23 },{ 0x303e5, 23 },{ 0x303e6, 23 },{ 0x303e7, 23 }, +{ 0x303e8, 23 },{ 0x303e9, 23 },{ 0x303ea, 23 },{ 0x303eb, 23 }, +{ 0x303ec, 23 },{ 0x303ed, 23 },{ 0x303ee, 23 },{ 0x303ef, 23 }, +{ 0x303f0, 23 },{ 0x303f1, 23 },{ 0x303f2, 23 },{ 0x303f3, 23 }, +{ 0x303f4, 23 },{ 0x303f5, 23 },{ 0x303f6, 23 },{ 0x303f7, 23 }, +{ 0x303f8, 23 },{ 0x303f9, 23 },{ 0x303fa, 23 },{ 0x303fb, 23 }, +{ 0x303fc, 23 },{ 0x303fd, 23 },{ 0x303fe, 23 },{ 0x303ff, 23 }, +{ 0x60780, 24 },{ 0x60781, 24 },{ 0x60782, 24 },{ 0x60783, 24 }, +{ 0x60784, 24 },{ 0x60785, 24 },{ 0x60786, 24 },{ 0x60787, 24 }, +{ 0x60788, 24 },{ 0x60789, 24 },{ 0x6078a, 24 },{ 0x6078b, 24 }, +{ 0x6078c, 24 },{ 0x6078d, 24 },{ 0x6078e, 24 },{ 0x6078f, 24 }, +}; + +const uint32_t ff_table0_dc_chroma[120][2] = { +{ 0x0, 2 },{ 0x1, 2 },{ 0x5, 3 },{ 0x9, 4 }, +{ 0xd, 4 },{ 0x11, 5 },{ 0x1d, 5 },{ 0x1f, 5 }, +{ 0x21, 6 },{ 0x31, 6 },{ 0x38, 6 },{ 0x33, 6 }, +{ 0x39, 6 },{ 0x3d, 6 },{ 0x61, 7 },{ 0x79, 7 }, +{ 0x80, 8 },{ 0xc8, 8 },{ 0xca, 8 },{ 0xf0, 8 }, +{ 0x81, 8 },{ 0xc0, 8 },{ 0xc9, 8 },{ 0x107, 9 }, +{ 0x106, 9 },{ 0x196, 9 },{ 0x183, 9 },{ 0x1e3, 9 }, +{ 0x1e2, 9 },{ 0x20a, 10 },{ 0x20b, 10 },{ 0x609, 11 }, +{ 0x412, 11 },{ 0x413, 11 },{ 0x60b, 11 },{ 0x411, 11 }, +{ 0x60a, 11 },{ 0x65f, 11 },{ 0x410, 11 },{ 0x65d, 11 }, +{ 0x65e, 11 },{ 0xcb8, 12 },{ 0xc10, 12 },{ 0xcb9, 12 }, +{ 0x1823, 13 },{ 0x3045, 14 },{ 0x6089, 15 },{ 0xc110, 16 }, +{ 0x304448, 22 },{ 0x304449, 22 },{ 0x30444a, 22 },{ 0x30444b, 22 }, +{ 0x30444c, 22 },{ 0x30444d, 22 },{ 0x30444e, 22 },{ 0x30444f, 22 }, +{ 0x304450, 22 },{ 0x304451, 22 },{ 0x304452, 22 },{ 0x304453, 22 }, +{ 0x304454, 22 },{ 0x304455, 22 },{ 0x304456, 22 },{ 0x304457, 22 }, +{ 0x304458, 22 },{ 0x304459, 22 },{ 0x30445a, 22 },{ 0x30445b, 22 }, +{ 0x30445c, 22 },{ 0x30445d, 22 },{ 0x30445e, 22 },{ 0x30445f, 22 }, +{ 0x304460, 22 },{ 0x304461, 22 },{ 0x304462, 22 },{ 0x304463, 22 }, +{ 0x304464, 22 },{ 0x304465, 22 },{ 0x304466, 22 },{ 0x304467, 22 }, +{ 0x304468, 22 },{ 0x304469, 22 },{ 0x30446a, 22 },{ 0x30446b, 22 }, +{ 0x30446c, 22 },{ 0x30446d, 22 },{ 0x30446e, 22 },{ 0x30446f, 22 }, +{ 0x304470, 22 },{ 0x304471, 22 },{ 0x304472, 22 },{ 0x304473, 22 }, +{ 0x304474, 22 },{ 0x304475, 22 },{ 0x304476, 22 },{ 0x304477, 22 }, +{ 0x304478, 22 },{ 0x304479, 22 },{ 0x30447a, 22 },{ 0x30447b, 22 }, +{ 0x30447c, 22 },{ 0x30447d, 22 },{ 0x30447e, 22 },{ 0x30447f, 22 }, +{ 0x608880, 23 },{ 0x608881, 23 },{ 0x608882, 23 },{ 0x608883, 23 }, +{ 0x608884, 23 },{ 0x608885, 23 },{ 0x608886, 23 },{ 0x608887, 23 }, +{ 0x608888, 23 },{ 0x608889, 23 },{ 0x60888a, 23 },{ 0x60888b, 23 }, +{ 0x60888c, 23 },{ 0x60888d, 23 },{ 0x60888e, 23 },{ 0x60888f, 23 }, +}; + +/* dc table 1 */ + +const uint32_t ff_table1_dc_lum[120][2] = { +{ 0x2, 2 },{ 0x3, 2 },{ 0x3, 3 },{ 0x2, 4 }, +{ 0x5, 4 },{ 0x1, 5 },{ 0x3, 5 },{ 0x8, 5 }, +{ 0x0, 6 },{ 0x5, 6 },{ 0xd, 6 },{ 0xf, 6 }, +{ 0x13, 6 },{ 0x8, 7 },{ 0x18, 7 },{ 0x1c, 7 }, +{ 0x24, 7 },{ 0x4, 8 },{ 0x6, 8 },{ 0x12, 8 }, +{ 0x32, 8 },{ 0x3b, 8 },{ 0x4a, 8 },{ 0x4b, 8 }, +{ 0xb, 9 },{ 0x26, 9 },{ 0x27, 9 },{ 0x66, 9 }, +{ 0x74, 9 },{ 0x75, 9 },{ 0x14, 10 },{ 0x1c, 10 }, +{ 0x1f, 10 },{ 0x1d, 10 },{ 0x2b, 11 },{ 0x3d, 11 }, +{ 0x19d, 11 },{ 0x19f, 11 },{ 0x54, 12 },{ 0x339, 12 }, +{ 0x338, 12 },{ 0x33d, 12 },{ 0xab, 13 },{ 0xf1, 13 }, +{ 0x678, 13 },{ 0xf2, 13 },{ 0x1e0, 14 },{ 0x1e1, 14 }, +{ 0x154, 14 },{ 0xcf2, 14 },{ 0x3cc, 15 },{ 0x2ab, 15 }, +{ 0x19e7, 15 },{ 0x3ce, 15 },{ 0x19e6, 15 },{ 0x554, 16 }, +{ 0x79f, 16 },{ 0x555, 16 },{ 0xf3d, 17 },{ 0xf37, 17 }, +{ 0xf3c, 17 },{ 0xf35, 17 },{ 0x1e6d, 18 },{ 0x1e68, 18 }, +{ 0x3cd8, 19 },{ 0x3cd3, 19 },{ 0x3cd9, 19 },{ 0x79a4, 20 }, +{ 0xf34ba, 25 },{ 0xf34b4, 25 },{ 0xf34b5, 25 },{ 0xf34b6, 25 }, +{ 0xf34b7, 25 },{ 0xf34b8, 25 },{ 0xf34b9, 25 },{ 0xf34bb, 25 }, +{ 0xf34bc, 25 },{ 0xf34bd, 25 },{ 0xf34be, 25 },{ 0xf34bf, 25 }, +{ 0x1e6940, 26 },{ 0x1e6941, 26 },{ 0x1e6942, 26 },{ 0x1e6943, 26 }, +{ 0x1e6944, 26 },{ 0x1e6945, 26 },{ 0x1e6946, 26 },{ 0x1e6947, 26 }, +{ 0x1e6948, 26 },{ 0x1e6949, 26 },{ 0x1e694a, 26 },{ 0x1e694b, 26 }, +{ 0x1e694c, 26 },{ 0x1e694d, 26 },{ 0x1e694e, 26 },{ 0x1e694f, 26 }, +{ 0x1e6950, 26 },{ 0x1e6951, 26 },{ 0x1e6952, 26 },{ 0x1e6953, 26 }, +{ 0x1e6954, 26 },{ 0x1e6955, 26 },{ 0x1e6956, 26 },{ 0x1e6957, 26 }, +{ 0x1e6958, 26 },{ 0x1e6959, 26 },{ 0x1e695a, 26 },{ 0x1e695b, 26 }, +{ 0x1e695c, 26 },{ 0x1e695d, 26 },{ 0x1e695e, 26 },{ 0x1e695f, 26 }, +{ 0x1e6960, 26 },{ 0x1e6961, 26 },{ 0x1e6962, 26 },{ 0x1e6963, 26 }, +{ 0x1e6964, 26 },{ 0x1e6965, 26 },{ 0x1e6966, 26 },{ 0x1e6967, 26 }, +}; + +const uint32_t ff_table1_dc_chroma[120][2] = { +{ 0x0, 2 },{ 0x1, 2 },{ 0x4, 3 },{ 0x7, 3 }, +{ 0xb, 4 },{ 0xd, 4 },{ 0x15, 5 },{ 0x28, 6 }, +{ 0x30, 6 },{ 0x32, 6 },{ 0x52, 7 },{ 0x62, 7 }, +{ 0x66, 7 },{ 0xa6, 8 },{ 0xc6, 8 },{ 0xcf, 8 }, +{ 0x14f, 9 },{ 0x18e, 9 },{ 0x19c, 9 },{ 0x29d, 10 }, +{ 0x33a, 10 },{ 0x538, 11 },{ 0x63c, 11 },{ 0x63e, 11 }, +{ 0x63f, 11 },{ 0x676, 11 },{ 0xa73, 12 },{ 0xc7a, 12 }, +{ 0xcef, 12 },{ 0x14e5, 13 },{ 0x19dd, 13 },{ 0x29c8, 14 }, +{ 0x29c9, 14 },{ 0x63dd, 15 },{ 0x33b8, 14 },{ 0x33b9, 14 }, +{ 0xc7b6, 16 },{ 0x63d8, 15 },{ 0x63df, 15 },{ 0xc7b3, 16 }, +{ 0xc7b4, 16 },{ 0xc7b5, 16 },{ 0x63de, 15 },{ 0xc7b7, 16 }, +{ 0xc7b8, 16 },{ 0xc7b9, 16 },{ 0x18f65, 17 },{ 0x31ec8, 18 }, +{ 0xc7b248, 24 },{ 0xc7b249, 24 },{ 0xc7b24a, 24 },{ 0xc7b24b, 24 }, +{ 0xc7b24c, 24 },{ 0xc7b24d, 24 },{ 0xc7b24e, 24 },{ 0xc7b24f, 24 }, +{ 0xc7b250, 24 },{ 0xc7b251, 24 },{ 0xc7b252, 24 },{ 0xc7b253, 24 }, +{ 0xc7b254, 24 },{ 0xc7b255, 24 },{ 0xc7b256, 24 },{ 0xc7b257, 24 }, +{ 0xc7b258, 24 },{ 0xc7b259, 24 },{ 0xc7b25a, 24 },{ 0xc7b25b, 24 }, +{ 0xc7b25c, 24 },{ 0xc7b25d, 24 },{ 0xc7b25e, 24 },{ 0xc7b25f, 24 }, +{ 0xc7b260, 24 },{ 0xc7b261, 24 },{ 0xc7b262, 24 },{ 0xc7b263, 24 }, +{ 0xc7b264, 24 },{ 0xc7b265, 24 },{ 0xc7b266, 24 },{ 0xc7b267, 24 }, +{ 0xc7b268, 24 },{ 0xc7b269, 24 },{ 0xc7b26a, 24 },{ 0xc7b26b, 24 }, +{ 0xc7b26c, 24 },{ 0xc7b26d, 24 },{ 0xc7b26e, 24 },{ 0xc7b26f, 24 }, +{ 0xc7b270, 24 },{ 0xc7b271, 24 },{ 0xc7b272, 24 },{ 0xc7b273, 24 }, +{ 0xc7b274, 24 },{ 0xc7b275, 24 },{ 0xc7b276, 24 },{ 0xc7b277, 24 }, +{ 0xc7b278, 24 },{ 0xc7b279, 24 },{ 0xc7b27a, 24 },{ 0xc7b27b, 24 }, +{ 0xc7b27c, 24 },{ 0xc7b27d, 24 },{ 0xc7b27e, 24 },{ 0xc7b27f, 24 }, +{ 0x18f6480, 25 },{ 0x18f6481, 25 },{ 0x18f6482, 25 },{ 0x18f6483, 25 }, +{ 0x18f6484, 25 },{ 0x18f6485, 25 },{ 0x18f6486, 25 },{ 0x18f6487, 25 }, +{ 0x18f6488, 25 },{ 0x18f6489, 25 },{ 0x18f648a, 25 },{ 0x18f648b, 25 }, +{ 0x18f648c, 25 },{ 0x18f648d, 25 },{ 0x18f648e, 25 },{ 0x18f648f, 25 }, +}; + +/* vlc table 0, for intra luma */ + +static const uint16_t table0_vlc[133][2] = { +{ 0x1, 2 },{ 0x6, 3 },{ 0xf, 4 },{ 0x16, 5 }, +{ 0x20, 6 },{ 0x18, 7 },{ 0x8, 8 },{ 0x9a, 8 }, +{ 0x56, 9 },{ 0x13e, 9 },{ 0xf0, 10 },{ 0x3a5, 10 }, +{ 0x77, 11 },{ 0x1ef, 11 },{ 0x9a, 12 },{ 0x5d, 13 }, +{ 0x1, 4 },{ 0x11, 5 },{ 0x2, 7 },{ 0xb, 8 }, +{ 0x12, 9 },{ 0x1d6, 9 },{ 0x27e, 10 },{ 0x191, 11 }, +{ 0xea, 12 },{ 0x3dc, 12 },{ 0x13b, 13 },{ 0x4, 5 }, +{ 0x14, 7 },{ 0x9e, 8 },{ 0x9, 10 },{ 0x1ac, 11 }, +{ 0x1e2, 11 },{ 0x3ca, 12 },{ 0x5f, 13 },{ 0x17, 5 }, +{ 0x4e, 7 },{ 0x5e, 9 },{ 0xf3, 10 },{ 0x1ad, 11 }, +{ 0xec, 12 },{ 0x5f0, 13 },{ 0xe, 6 },{ 0xe1, 8 }, +{ 0x3a4, 10 },{ 0x9c, 12 },{ 0x13d, 13 },{ 0x3b, 6 }, +{ 0x1c, 9 },{ 0x14, 11 },{ 0x9be, 12 },{ 0x6, 7 }, +{ 0x7a, 9 },{ 0x190, 11 },{ 0x137, 13 },{ 0x1b, 7 }, +{ 0x8, 10 },{ 0x75c, 11 },{ 0x71, 7 },{ 0xd7, 10 }, +{ 0x9bf, 12 },{ 0x7, 8 },{ 0xaf, 10 },{ 0x4cc, 11 }, +{ 0x34, 8 },{ 0x265, 10 },{ 0x9f, 12 },{ 0xe0, 8 }, +{ 0x16, 11 },{ 0x327, 12 },{ 0x15, 9 },{ 0x17d, 11 }, +{ 0xebb, 12 },{ 0x14, 9 },{ 0xf6, 10 },{ 0x1e4, 11 }, +{ 0xcb, 10 },{ 0x99d, 12 },{ 0xca, 10 },{ 0x2fc, 12 }, +{ 0x17f, 11 },{ 0x4cd, 11 },{ 0x2fd, 12 },{ 0x4fe, 11 }, +{ 0x13a, 13 },{ 0xa, 4 },{ 0x42, 7 },{ 0x1d3, 9 }, +{ 0x4dd, 11 },{ 0x12, 5 },{ 0xe8, 8 },{ 0x4c, 11 }, +{ 0x136, 13 },{ 0x39, 6 },{ 0x264, 10 },{ 0xeba, 12 }, +{ 0x0, 7 },{ 0xae, 10 },{ 0x99c, 12 },{ 0x1f, 7 }, +{ 0x4de, 11 },{ 0x43, 7 },{ 0x4dc, 11 },{ 0x3, 8 }, +{ 0x3cb, 12 },{ 0x6, 8 },{ 0x99e, 12 },{ 0x2a, 8 }, +{ 0x5f1, 13 },{ 0xf, 8 },{ 0x9fe, 12 },{ 0x33, 8 }, +{ 0x9ff, 12 },{ 0x98, 8 },{ 0x99f, 12 },{ 0xea, 8 }, +{ 0x13c, 13 },{ 0x2e, 8 },{ 0x192, 11 },{ 0x136, 9 }, +{ 0x6a, 9 },{ 0x15, 11 },{ 0x3af, 10 },{ 0x1e3, 11 }, +{ 0x74, 11 },{ 0xeb, 12 },{ 0x2f9, 12 },{ 0x5c, 13 }, +{ 0xed, 12 },{ 0x3dd, 12 },{ 0x326, 12 },{ 0x5e, 13 }, +{ 0x16, 7 }, +}; + +static const int8_t table0_level[132] = { + 1, 2, 3, 4, 5, 6, 7, 8, + 9, 10, 11, 12, 13, 14, 15, 16, + 1, 2, 3, 4, 5, 6, 7, 8, + 9, 10, 11, 1, 2, 3, 4, 5, + 6, 7, 8, 1, 2, 3, 4, 5, + 6, 7, 1, 2, 3, 4, 5, 1, + 2, 3, 4, 1, 2, 3, 4, 1, + 2, 3, 1, 2, 3, 1, 2, 3, + 1, 2, 3, 1, 2, 3, 1, 2, + 3, 1, 2, 3, 1, 2, 1, 2, + 1, 1, 1, 1, 1, 1, 2, 3, + 4, 1, 2, 3, 4, 1, 2, 3, + 1, 2, 3, 1, 2, 1, 2, 1, + 2, 1, 2, 1, 2, 1, 2, 1, + 2, 1, 2, 1, 2, 1, 2, 1, + 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, +}; + +static const int8_t table0_run[132] = { + 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, 2, 2, 2, 2, 2, + 2, 2, 2, 3, 3, 3, 3, 3, + 3, 3, 4, 4, 4, 4, 4, 5, + 5, 5, 5, 6, 6, 6, 6, 7, + 7, 7, 8, 8, 8, 9, 9, 9, + 10, 10, 10, 11, 11, 11, 12, 12, + 12, 13, 13, 13, 14, 14, 15, 15, + 16, 17, 18, 19, 20, 0, 0, 0, + 0, 1, 1, 1, 1, 2, 2, 2, + 3, 3, 3, 4, 4, 5, 5, 6, + 6, 7, 7, 8, 8, 9, 9, 10, + 10, 11, 11, 12, 12, 13, 13, 14, + 15, 16, 17, 18, 19, 20, 21, 22, + 23, 24, 25, 26, +}; + +/* vlc table 1, for intra chroma and P macro blocks */ + +static const uint16_t table1_vlc[149][2] = { +{ 0x4, 3 },{ 0x14, 5 },{ 0x17, 7 },{ 0x7f, 8 }, +{ 0x154, 9 },{ 0x1f2, 10 },{ 0xbf, 11 },{ 0x65, 12 }, +{ 0xaaa, 12 },{ 0x630, 13 },{ 0x1597, 13 },{ 0x3b7, 14 }, +{ 0x2b22, 14 },{ 0xbe6, 15 },{ 0xb, 4 },{ 0x37, 7 }, +{ 0x62, 9 },{ 0x7, 11 },{ 0x166, 12 },{ 0xce, 13 }, +{ 0x1590, 13 },{ 0x5f6, 14 },{ 0xbe7, 15 },{ 0x7, 5 }, +{ 0x6d, 8 },{ 0x3, 11 },{ 0x31f, 12 },{ 0x5f2, 14 }, +{ 0x2, 6 },{ 0x61, 9 },{ 0x55, 12 },{ 0x1df, 14 }, +{ 0x1a, 6 },{ 0x1e, 10 },{ 0xac9, 12 },{ 0x2b23, 14 }, +{ 0x1e, 6 },{ 0x1f, 10 },{ 0xac3, 12 },{ 0x2b2b, 14 }, +{ 0x6, 7 },{ 0x4, 11 },{ 0x2f8, 13 },{ 0x19, 7 }, +{ 0x6, 11 },{ 0x63d, 13 },{ 0x57, 7 },{ 0x182, 11 }, +{ 0x2aa2, 14 },{ 0x4, 8 },{ 0x180, 11 },{ 0x59c, 14 }, +{ 0x7d, 8 },{ 0x164, 12 },{ 0x76d, 15 },{ 0x2, 9 }, +{ 0x18d, 11 },{ 0x1581, 13 },{ 0xad, 8 },{ 0x60, 12 }, +{ 0xc67, 14 },{ 0x1c, 9 },{ 0xee, 13 },{ 0x3, 9 }, +{ 0x2cf, 13 },{ 0xd9, 9 },{ 0x1580, 13 },{ 0x2, 11 }, +{ 0x183, 11 },{ 0x57, 12 },{ 0x61, 12 },{ 0x31, 11 }, +{ 0x66, 12 },{ 0x631, 13 },{ 0x632, 13 },{ 0xac, 13 }, +{ 0x31d, 12 },{ 0x76, 12 },{ 0x3a, 11 },{ 0x165, 12 }, +{ 0xc66, 14 },{ 0x3, 2 },{ 0x54, 7 },{ 0x2ab, 10 }, +{ 0x16, 13 },{ 0x5f7, 14 },{ 0x5, 4 },{ 0xf8, 9 }, +{ 0xaa9, 12 },{ 0x5f, 15 },{ 0x4, 4 },{ 0x1c, 10 }, +{ 0x1550, 13 },{ 0x4, 5 },{ 0x77, 11 },{ 0x76c, 15 }, +{ 0xe, 5 },{ 0xa, 12 },{ 0xc, 5 },{ 0x562, 11 }, +{ 0x4, 6 },{ 0x31c, 12 },{ 0x6, 6 },{ 0xc8, 13 }, +{ 0xd, 6 },{ 0x1da, 13 },{ 0x7, 6 },{ 0xc9, 13 }, +{ 0x1, 7 },{ 0x2e, 14 },{ 0x14, 7 },{ 0x1596, 13 }, +{ 0xa, 7 },{ 0xac2, 12 },{ 0x16, 7 },{ 0x15b, 14 }, +{ 0x15, 7 },{ 0x15a, 14 },{ 0xf, 8 },{ 0x5e, 15 }, +{ 0x7e, 8 },{ 0xab, 8 },{ 0x2d, 9 },{ 0xd8, 9 }, +{ 0xb, 9 },{ 0x14, 10 },{ 0x2b3, 10 },{ 0x1f3, 10 }, +{ 0x3a, 10 },{ 0x0, 10 },{ 0x58, 10 },{ 0x2e, 9 }, +{ 0x5e, 10 },{ 0x563, 11 },{ 0xec, 12 },{ 0x54, 12 }, +{ 0xac1, 12 },{ 0x1556, 13 },{ 0x2fa, 13 },{ 0x181, 11 }, +{ 0x1557, 13 },{ 0x59d, 14 },{ 0x2aa3, 14 },{ 0x2b2a, 14 }, +{ 0x1de, 14 },{ 0x63c, 13 },{ 0xcf, 13 },{ 0x1594, 13 }, +{ 0xd, 9 }, +}; + +static const int8_t table1_level[148] = { + 1, 2, 3, 4, 5, 6, 7, 8, + 9, 10, 11, 12, 13, 14, 1, 2, + 3, 4, 5, 6, 7, 8, 9, 1, + 2, 3, 4, 5, 1, 2, 3, 4, + 1, 2, 3, 4, 1, 2, 3, 4, + 1, 2, 3, 1, 2, 3, 1, 2, + 3, 1, 2, 3, 1, 2, 3, 1, + 2, 3, 1, 2, 3, 1, 2, 1, + 2, 1, 2, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 2, 3, 4, 5, 1, 2, + 3, 4, 1, 2, 3, 1, 2, 3, + 1, 2, 1, 2, 1, 2, 1, 2, + 1, 2, 1, 2, 1, 2, 1, 2, + 1, 2, 1, 2, 1, 2, 1, 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, +}; + +static const int8_t table1_run[148] = { + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 1, 1, + 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, 9, 10, 10, 10, 11, + 11, 11, 12, 12, 12, 13, 13, 14, + 14, 15, 15, 16, 17, 18, 19, 20, + 21, 22, 23, 24, 25, 26, 27, 28, + 29, 0, 0, 0, 0, 0, 1, 1, + 1, 1, 2, 2, 2, 3, 3, 3, + 4, 4, 5, 5, 6, 6, 7, 7, + 8, 8, 9, 9, 10, 10, 11, 11, + 12, 12, 13, 13, 14, 14, 15, 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, +}; + +/* third vlc table */ + +static const uint16_t table2_vlc[186][2] = { +{ 0x1, 2 },{ 0x5, 3 },{ 0xd, 4 },{ 0x12, 5 }, +{ 0xe, 6 },{ 0x15, 7 },{ 0x13, 8 },{ 0x3f, 8 }, +{ 0x4b, 9 },{ 0x11f, 9 },{ 0xb8, 10 },{ 0x3e3, 10 }, +{ 0x172, 11 },{ 0x24d, 12 },{ 0x3da, 12 },{ 0x2dd, 13 }, +{ 0x1f55, 13 },{ 0x5b9, 14 },{ 0x3eae, 14 },{ 0x0, 4 }, +{ 0x10, 5 },{ 0x8, 7 },{ 0x20, 8 },{ 0x29, 9 }, +{ 0x1f4, 9 },{ 0x233, 10 },{ 0x1e0, 11 },{ 0x12a, 12 }, +{ 0x3dd, 12 },{ 0x50a, 13 },{ 0x1f29, 13 },{ 0xa42, 14 }, +{ 0x1272, 15 },{ 0x1737, 15 },{ 0x3, 5 },{ 0x11, 7 }, +{ 0xc4, 8 },{ 0x4b, 10 },{ 0xb4, 11 },{ 0x7d4, 11 }, +{ 0x345, 12 },{ 0x2d7, 13 },{ 0x7bf, 13 },{ 0x938, 14 }, +{ 0xbbb, 14 },{ 0x95e, 15 },{ 0x13, 5 },{ 0x78, 7 }, +{ 0x69, 9 },{ 0x232, 10 },{ 0x461, 11 },{ 0x3ec, 12 }, +{ 0x520, 13 },{ 0x1f2a, 13 },{ 0x3e50, 14 },{ 0x3e51, 14 }, +{ 0x1486, 15 },{ 0xc, 6 },{ 0x24, 9 },{ 0x94, 11 }, +{ 0x8c0, 12 },{ 0xf09, 14 },{ 0x1ef0, 15 },{ 0x3d, 6 }, +{ 0x53, 9 },{ 0x1a0, 11 },{ 0x2d6, 13 },{ 0xf08, 14 }, +{ 0x13, 7 },{ 0x7c, 9 },{ 0x7c1, 11 },{ 0x4ac, 14 }, +{ 0x1b, 7 },{ 0xa0, 10 },{ 0x344, 12 },{ 0xf79, 14 }, +{ 0x79, 7 },{ 0x3e1, 10 },{ 0x2d4, 13 },{ 0x2306, 14 }, +{ 0x21, 8 },{ 0x23c, 10 },{ 0xfae, 12 },{ 0x23de, 14 }, +{ 0x35, 8 },{ 0x175, 11 },{ 0x7b3, 13 },{ 0xc5, 8 }, +{ 0x174, 11 },{ 0x785, 13 },{ 0x48, 9 },{ 0x1a3, 11 }, +{ 0x49e, 13 },{ 0x2c, 9 },{ 0xfa, 10 },{ 0x7d6, 11 }, +{ 0x92, 10 },{ 0x5cc, 13 },{ 0x1ef1, 15 },{ 0xa3, 10 }, +{ 0x3ed, 12 },{ 0x93e, 14 },{ 0x1e2, 11 },{ 0x1273, 15 }, +{ 0x7c4, 11 },{ 0x1487, 15 },{ 0x291, 12 },{ 0x293, 12 }, +{ 0xf8a, 12 },{ 0x509, 13 },{ 0x508, 13 },{ 0x78d, 13 }, +{ 0x7be, 13 },{ 0x78c, 13 },{ 0x4ae, 14 },{ 0xbba, 14 }, +{ 0x2307, 14 },{ 0xb9a, 14 },{ 0x1736, 15 },{ 0xe, 4 }, +{ 0x45, 7 },{ 0x1f3, 9 },{ 0x47a, 11 },{ 0x5dc, 13 }, +{ 0x23df, 14 },{ 0x19, 5 },{ 0x28, 9 },{ 0x176, 11 }, +{ 0x49d, 13 },{ 0x23dd, 14 },{ 0x30, 6 },{ 0xa2, 10 }, +{ 0x2ef, 12 },{ 0x5b8, 14 },{ 0x3f, 6 },{ 0xa5, 10 }, +{ 0x3db, 12 },{ 0x93f, 14 },{ 0x44, 7 },{ 0x7cb, 11 }, +{ 0x95f, 15 },{ 0x63, 7 },{ 0x3c3, 12 },{ 0x15, 8 }, +{ 0x8f6, 12 },{ 0x17, 8 },{ 0x498, 13 },{ 0x2c, 8 }, +{ 0x7b2, 13 },{ 0x2f, 8 },{ 0x1f54, 13 },{ 0x8d, 8 }, +{ 0x7bd, 13 },{ 0x8e, 8 },{ 0x1182, 13 },{ 0xfb, 8 }, +{ 0x50b, 13 },{ 0x2d, 8 },{ 0x7c0, 11 },{ 0x79, 9 }, +{ 0x1f5f, 13 },{ 0x7a, 9 },{ 0x1f56, 13 },{ 0x231, 10 }, +{ 0x3e4, 10 },{ 0x1a1, 11 },{ 0x143, 11 },{ 0x1f7, 11 }, +{ 0x16f, 12 },{ 0x292, 12 },{ 0x2e7, 12 },{ 0x16c, 12 }, +{ 0x16d, 12 },{ 0x3dc, 12 },{ 0xf8b, 12 },{ 0x499, 13 }, +{ 0x3d8, 12 },{ 0x78e, 13 },{ 0x2d5, 13 },{ 0x1f5e, 13 }, +{ 0x1f2b, 13 },{ 0x78f, 13 },{ 0x4ad, 14 },{ 0x3eaf, 14 }, +{ 0x23dc, 14 },{ 0x4a, 9 }, +}; + +static const int8_t table2_level[185] = { + 1, 2, 3, 4, 5, 6, 7, 8, + 9, 10, 11, 12, 13, 14, 15, 16, + 17, 18, 19, 1, 2, 3, 4, 5, + 6, 7, 8, 9, 10, 11, 12, 13, + 14, 15, 1, 2, 3, 4, 5, 6, + 7, 8, 9, 10, 11, 12, 1, 2, + 3, 4, 5, 6, 7, 8, 9, 10, + 11, 1, 2, 3, 4, 5, 6, 1, + 2, 3, 4, 5, 1, 2, 3, 4, + 1, 2, 3, 4, 1, 2, 3, 4, + 1, 2, 3, 4, 1, 2, 3, 1, + 2, 3, 1, 2, 3, 1, 2, 3, + 1, 2, 3, 1, 2, 3, 1, 2, + 1, 2, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, + 2, 3, 4, 5, 6, 1, 2, 3, + 4, 5, 1, 2, 3, 4, 1, 2, + 3, 4, 1, 2, 3, 1, 2, 1, + 2, 1, 2, 1, 2, 1, 2, 1, + 2, 1, 2, 1, 2, 1, 2, 1, + 2, 1, 2, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, + 1, +}; + +static const int8_t table2_run[185] = { + 0, 0, 0, 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, 1, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, + 3, 4, 4, 4, 4, 4, 4, 5, + 5, 5, 5, 5, 6, 6, 6, 6, + 7, 7, 7, 7, 8, 8, 8, 8, + 9, 9, 9, 9, 10, 10, 10, 11, + 11, 11, 12, 12, 12, 13, 13, 13, + 14, 14, 14, 15, 15, 15, 16, 16, + 17, 17, 18, 19, 20, 21, 22, 23, + 24, 25, 26, 27, 28, 29, 30, 0, + 0, 0, 0, 0, 0, 1, 1, 1, + 1, 1, 2, 2, 2, 2, 3, 3, + 3, 3, 4, 4, 4, 5, 5, 6, + 6, 7, 7, 8, 8, 9, 9, 10, + 10, 11, 11, 12, 12, 13, 13, 14, + 14, 15, 15, 16, 17, 18, 19, 20, + 21, 22, 23, 24, 25, 26, 27, 28, + 29, 30, 31, 32, 33, 34, 35, 36, + 37, +}; + +/* second non intra vlc table */ +static const uint16_t table4_vlc[169][2] = { +{ 0x0, 3 },{ 0x3, 4 },{ 0xb, 5 },{ 0x14, 6 }, +{ 0x3f, 6 },{ 0x5d, 7 },{ 0xa2, 8 },{ 0xac, 9 }, +{ 0x16e, 9 },{ 0x20a, 10 },{ 0x2e2, 10 },{ 0x432, 11 }, +{ 0x5c9, 11 },{ 0x827, 12 },{ 0xb54, 12 },{ 0x4e6, 13 }, +{ 0x105f, 13 },{ 0x172a, 13 },{ 0x20b2, 14 },{ 0x2d4e, 14 }, +{ 0x39f0, 14 },{ 0x4175, 15 },{ 0x5a9e, 15 },{ 0x4, 4 }, +{ 0x1e, 5 },{ 0x42, 7 },{ 0xb6, 8 },{ 0x173, 9 }, +{ 0x395, 10 },{ 0x72e, 11 },{ 0xb94, 12 },{ 0x16a4, 13 }, +{ 0x20b3, 14 },{ 0x2e45, 14 },{ 0x5, 5 },{ 0x40, 7 }, +{ 0x49, 9 },{ 0x28f, 10 },{ 0x5cb, 11 },{ 0x48a, 13 }, +{ 0x9dd, 14 },{ 0x73e2, 15 },{ 0x18, 5 },{ 0x25, 8 }, +{ 0x8a, 10 },{ 0x51b, 11 },{ 0xe5f, 12 },{ 0x9c9, 14 }, +{ 0x139c, 15 },{ 0x29, 6 },{ 0x4f, 9 },{ 0x412, 11 }, +{ 0x48d, 13 },{ 0x2e41, 14 },{ 0x38, 6 },{ 0x10e, 9 }, +{ 0x5a8, 11 },{ 0x105c, 13 },{ 0x39f2, 14 },{ 0x58, 7 }, +{ 0x21f, 10 },{ 0xe7e, 12 },{ 0x39ff, 14 },{ 0x23, 8 }, +{ 0x2e3, 10 },{ 0x4e5, 13 },{ 0x2e40, 14 },{ 0xa1, 8 }, +{ 0x5be, 11 },{ 0x9c8, 14 },{ 0x83, 8 },{ 0x13a, 11 }, +{ 0x1721, 13 },{ 0x44, 9 },{ 0x276, 12 },{ 0x39f6, 14 }, +{ 0x8b, 10 },{ 0x4ef, 13 },{ 0x5a9b, 15 },{ 0x208, 10 }, +{ 0x1cfe, 13 },{ 0x399, 10 },{ 0x1cb4, 13 },{ 0x39e, 10 }, +{ 0x39f3, 14 },{ 0x5ab, 11 },{ 0x73e3, 15 },{ 0x737, 11 }, +{ 0x5a9f, 15 },{ 0x82d, 12 },{ 0xe69, 12 },{ 0xe68, 12 }, +{ 0x433, 11 },{ 0xb7b, 12 },{ 0x2df8, 14 },{ 0x2e56, 14 }, +{ 0x2e57, 14 },{ 0x39f7, 14 },{ 0x51a5, 15 },{ 0x3, 3 }, +{ 0x2a, 6 },{ 0xe4, 8 },{ 0x28e, 10 },{ 0x735, 11 }, +{ 0x1058, 13 },{ 0x1cfa, 13 },{ 0x2df9, 14 },{ 0x4174, 15 }, +{ 0x9, 4 },{ 0x54, 8 },{ 0x398, 10 },{ 0x48b, 13 }, +{ 0x139d, 15 },{ 0xd, 4 },{ 0xad, 9 },{ 0x826, 12 }, +{ 0x2d4c, 14 },{ 0x11, 5 },{ 0x16b, 9 },{ 0xb7f, 12 }, +{ 0x51a4, 15 },{ 0x19, 5 },{ 0x21b, 10 },{ 0x16fd, 13 }, +{ 0x1d, 5 },{ 0x394, 10 },{ 0x28d3, 14 },{ 0x2b, 6 }, +{ 0x5bc, 11 },{ 0x5a9a, 15 },{ 0x2f, 6 },{ 0x247, 12 }, +{ 0x10, 7 },{ 0xa35, 12 },{ 0x3e, 6 },{ 0xb7a, 12 }, +{ 0x59, 7 },{ 0x105e, 13 },{ 0x26, 8 },{ 0x9cf, 14 }, +{ 0x55, 8 },{ 0x1cb5, 13 },{ 0x57, 8 },{ 0xe5b, 12 }, +{ 0xa0, 8 },{ 0x1468, 13 },{ 0x170, 9 },{ 0x90, 10 }, +{ 0x1ce, 9 },{ 0x21a, 10 },{ 0x218, 10 },{ 0x168, 9 }, +{ 0x21e, 10 },{ 0x244, 12 },{ 0x736, 11 },{ 0x138, 11 }, +{ 0x519, 11 },{ 0xe5e, 12 },{ 0x72c, 11 },{ 0xb55, 12 }, +{ 0x9dc, 14 },{ 0x20bb, 14 },{ 0x48c, 13 },{ 0x1723, 13 }, +{ 0x2e44, 14 },{ 0x16a5, 13 },{ 0x518, 11 },{ 0x39fe, 14 }, +{ 0x169, 9 }, +}; + +static const int8_t table4_level[168] = { + 1, 2, 3, 4, 5, 6, 7, 8, + 9, 10, 11, 12, 13, 14, 15, 16, + 17, 18, 19, 20, 21, 22, 23, 1, + 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 1, 2, 3, 4, 5, 6, + 7, 8, 1, 2, 3, 4, 5, 6, + 7, 1, 2, 3, 4, 5, 1, 2, + 3, 4, 5, 1, 2, 3, 4, 1, + 2, 3, 4, 1, 2, 3, 1, 2, + 3, 1, 2, 3, 1, 2, 3, 1, + 2, 1, 2, 1, 2, 1, 2, 1, + 2, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 2, 3, 4, 5, + 6, 7, 8, 9, 1, 2, 3, 4, + 5, 1, 2, 3, 4, 1, 2, 3, + 4, 1, 2, 3, 1, 2, 3, 1, + 2, 3, 1, 2, 1, 2, 1, 2, + 1, 2, 1, 2, 1, 2, 1, 2, + 1, 2, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, +}; + +static const int8_t table4_run[168] = { + 0, 0, 0, 0, 0, 0, 0, 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, 2, 2, 2, 2, 2, 2, + 2, 2, 3, 3, 3, 3, 3, 3, + 3, 4, 4, 4, 4, 4, 5, 5, + 5, 5, 5, 6, 6, 6, 6, 7, + 7, 7, 7, 8, 8, 8, 9, 9, + 9, 10, 10, 10, 11, 11, 11, 12, + 12, 13, 13, 14, 14, 15, 15, 16, + 16, 17, 18, 19, 20, 21, 22, 23, + 24, 25, 26, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 1, 1, 1, + 1, 2, 2, 2, 2, 3, 3, 3, + 3, 4, 4, 4, 5, 5, 5, 6, + 6, 6, 7, 7, 8, 8, 9, 9, + 10, 10, 11, 11, 12, 12, 13, 13, + 14, 14, 15, 16, 17, 18, 19, 20, + 21, 22, 23, 24, 25, 26, 27, 28, + 29, 30, 31, 32, 33, 34, 35, 36, +}; + +extern const uint16_t inter_vlc[103][2]; +extern const int8_t inter_level[102]; +extern const int8_t inter_run[102]; + +extern const uint16_t intra_vlc[103][2]; +extern const int8_t intra_level[102]; +extern const int8_t intra_run[102]; + +RLTable rl_table[NB_RL_TABLES] = { + /* intra luminance tables */ + /* low motion */ + { + 132, + 85, + table0_vlc, + table0_run, + table0_level, + }, + /* high motion */ + { + 185, + 119, + table2_vlc, + table2_run, + table2_level, + }, + /* mid-rate */ + { + 102, + 67, + intra_vlc, + intra_run, + intra_level, + }, + /* intra chrominance / non intra tables */ + /* low motion inter */ + { + 148, + 81, + table1_vlc, + table1_run, + table1_level, + }, + /* high motion inter */ + { + 168, + 99, + table4_vlc, + table4_run, + table4_level, + }, + /* mid rate inter */ + { + 102, + 58, + inter_vlc, + inter_run, + inter_level, + }, +}; + +/* motion vector table 0 */ + +static const uint16_t table0_mv_code[1100] = { + 0x0001, 0x0003, 0x0005, 0x0007, 0x0003, 0x0008, 0x000c, 0x0001, + 0x0002, 0x001b, 0x0006, 0x000b, 0x0015, 0x0002, 0x000e, 0x000f, + 0x0014, 0x0020, 0x0022, 0x0025, 0x0027, 0x0029, 0x002d, 0x004b, + 0x004d, 0x0003, 0x0022, 0x0023, 0x0025, 0x0027, 0x0042, 0x0048, + 0x0049, 0x0050, 0x005c, 0x0091, 0x009f, 0x000e, 0x0043, 0x004c, + 0x0054, 0x0056, 0x008c, 0x0098, 0x009a, 0x009b, 0x00b1, 0x00b2, + 0x0120, 0x0121, 0x0126, 0x0133, 0x0139, 0x01a1, 0x01a4, 0x01a5, + 0x01a6, 0x01a7, 0x01ae, 0x01af, 0x000b, 0x0019, 0x0085, 0x0090, + 0x009b, 0x00aa, 0x00af, 0x010c, 0x010e, 0x011c, 0x011e, 0x0133, + 0x0144, 0x0160, 0x0174, 0x0175, 0x0177, 0x0178, 0x0249, 0x024b, + 0x0252, 0x0261, 0x0265, 0x0270, 0x0352, 0x0353, 0x0355, 0x0359, + 0x0010, 0x0011, 0x0013, 0x0034, 0x0035, 0x0036, 0x0037, 0x003d, + 0x003e, 0x0109, 0x0126, 0x0156, 0x021a, 0x021e, 0x023a, 0x023e, + 0x028e, 0x028f, 0x02cf, 0x0491, 0x0494, 0x049f, 0x04a0, 0x04a3, + 0x04a6, 0x04a7, 0x04ad, 0x04ae, 0x04c0, 0x04c4, 0x04c6, 0x04c8, + 0x04c9, 0x04f5, 0x04f6, 0x04f7, 0x0680, 0x0682, 0x0683, 0x0688, + 0x0689, 0x068d, 0x068e, 0x068f, 0x06a2, 0x06a3, 0x06a9, 0x06b0, + 0x06b1, 0x06b4, 0x06b5, 0x0024, 0x0060, 0x0063, 0x0078, 0x0079, + 0x0211, 0x0244, 0x0245, 0x0247, 0x0248, 0x0249, 0x024a, 0x024b, + 0x026b, 0x02af, 0x02b8, 0x02bb, 0x0436, 0x0476, 0x0477, 0x047e, + 0x04c8, 0x04c9, 0x04ca, 0x0514, 0x0586, 0x0587, 0x0598, 0x059d, + 0x05d9, 0x05da, 0x0920, 0x0921, 0x093b, 0x093c, 0x093d, 0x0942, + 0x0943, 0x0944, 0x0945, 0x0959, 0x095e, 0x095f, 0x0982, 0x0983, + 0x098e, 0x098f, 0x09c4, 0x09e7, 0x09e8, 0x09e9, 0x0d02, 0x0d17, + 0x0d18, 0x0d19, 0x0d41, 0x0d42, 0x0d43, 0x0d50, 0x0d5f, 0x0d6d, + 0x0d6e, 0x0d6f, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x041e, 0x041f, 0x0420, 0x0421, + 0x048c, 0x048d, 0x04d3, 0x04d4, 0x04d5, 0x055c, 0x055d, 0x0572, + 0x0573, 0x0574, 0x0575, 0x08de, 0x08df, 0x08fe, 0x08ff, 0x0996, + 0x0a36, 0x0a37, 0x0b08, 0x0b09, 0x0b0a, 0x0b0b, 0x0b32, 0x0b33, + 0x0b34, 0x0b35, 0x0b36, 0x0b37, 0x0b38, 0x0b39, 0x0bb0, 0x0bf7, + 0x0bf8, 0x0bf9, 0x0bfa, 0x0bfb, 0x0bfc, 0x0bfd, 0x0bfe, 0x0bff, + 0x1254, 0x1255, 0x1256, 0x1257, 0x1270, 0x1271, 0x1272, 0x1273, + 0x1274, 0x1275, 0x12ab, 0x12ac, 0x12ad, 0x12ae, 0x12af, 0x12b0, + 0x12b1, 0x1315, 0x1316, 0x1317, 0x13bf, 0x13c0, 0x13c1, 0x13c2, + 0x13c3, 0x13c4, 0x13c5, 0x13c6, 0x13c7, 0x13c8, 0x13c9, 0x13ca, + 0x13cb, 0x13cc, 0x13cd, 0x1a06, 0x1a07, 0x1a28, 0x1a29, 0x1a2a, + 0x1a2b, 0x1a2c, 0x1a2d, 0x1a80, 0x1abb, 0x1abc, 0x1abd, 0x1ad8, + 0x1ad9, 0x0094, 0x0095, 0x0096, 0x0097, 0x00a0, 0x00a1, 0x00a2, + 0x00a3, 0x0831, 0x0832, 0x0833, 0x0834, 0x0835, 0x0836, 0x0837, + 0x0838, 0x0839, 0x083a, 0x083b, 0x0939, 0x093a, 0x093b, 0x093c, + 0x093d, 0x093e, 0x093f, 0x09a0, 0x09a1, 0x09a2, 0x09a3, 0x09a4, + 0x09a5, 0x11ac, 0x11ad, 0x11ae, 0x11af, 0x11b0, 0x11b1, 0x11b2, + 0x11b3, 0x11b4, 0x11b5, 0x11b6, 0x11b7, 0x11b8, 0x11b9, 0x11ba, + 0x11bb, 0x132f, 0x1454, 0x1455, 0x1456, 0x1457, 0x1458, 0x1459, + 0x145a, 0x145b, 0x145c, 0x145d, 0x145e, 0x145f, 0x1460, 0x1461, + 0x1462, 0x1463, 0x1464, 0x1465, 0x1466, 0x1467, 0x1468, 0x1469, + 0x146a, 0x146b, 0x17de, 0x17df, 0x17e0, 0x17e1, 0x17e2, 0x17e3, + 0x17e4, 0x17e5, 0x17e6, 0x17e7, 0x17e8, 0x17e9, 0x17ea, 0x17eb, + 0x17ec, 0x17ed, 0x2540, 0x2541, 0x2542, 0x2543, 0x2544, 0x2545, + 0x2546, 0x2547, 0x2548, 0x2549, 0x254a, 0x254b, 0x254c, 0x254d, + 0x254e, 0x254f, 0x2550, 0x2551, 0x2552, 0x2553, 0x2554, 0x2555, + 0x2628, 0x2766, 0x2767, 0x2768, 0x2769, 0x276a, 0x276b, 0x276c, + 0x276d, 0x276e, 0x276f, 0x2770, 0x2771, 0x2772, 0x2773, 0x2774, + 0x2775, 0x2776, 0x2777, 0x2778, 0x2779, 0x277a, 0x277b, 0x277c, + 0x277d, 0x3503, 0x3544, 0x3545, 0x3546, 0x3547, 0x3560, 0x3561, + 0x3562, 0x3563, 0x3564, 0x3565, 0x3566, 0x3567, 0x3568, 0x3569, + 0x356a, 0x356b, 0x356c, 0x356d, 0x356e, 0x356f, 0x3570, 0x3571, + 0x3572, 0x3573, 0x3574, 0x3575, 0x03f0, 0x103d, 0x103e, 0x103f, + 0x1040, 0x1041, 0x1042, 0x1043, 0x1044, 0x1045, 0x1046, 0x1047, + 0x1048, 0x1049, 0x104a, 0x104b, 0x104c, 0x104d, 0x104e, 0x104f, + 0x1050, 0x1051, 0x1052, 0x1053, 0x1054, 0x1055, 0x1056, 0x1057, + 0x1058, 0x1059, 0x105a, 0x105b, 0x105c, 0x105d, 0x105e, 0x105f, + 0x1060, 0x1061, 0x1270, 0x1271, 0x21b8, 0x21b9, 0x21ba, 0x21bb, + 0x21bc, 0x21bd, 0x21be, 0x21bf, 0x21f0, 0x21f1, 0x21f2, 0x21f3, + 0x21f4, 0x21f5, 0x21f6, 0x21f7, 0x21f8, 0x21f9, 0x21fa, 0x21fb, + 0x21fc, 0x21fd, 0x21fe, 0x21ff, 0x2340, 0x2341, 0x2342, 0x2343, + 0x2344, 0x2345, 0x2346, 0x2347, 0x2348, 0x2349, 0x234a, 0x234b, + 0x234c, 0x234d, 0x234e, 0x234f, 0x2350, 0x2351, 0x2352, 0x2353, + 0x2354, 0x2355, 0x2356, 0x2357, 0x265c, 0x2f88, 0x2f89, 0x2f8a, + 0x2f8b, 0x2f8c, 0x2f8d, 0x2f8e, 0x2f8f, 0x2f90, 0x2f91, 0x2f92, + 0x2f93, 0x2f94, 0x2f95, 0x2f96, 0x2f97, 0x2f98, 0x2f99, 0x2f9a, + 0x2f9b, 0x2f9c, 0x2f9d, 0x2f9e, 0x2f9f, 0x2fa0, 0x2fa1, 0x2fa2, + 0x2fa3, 0x2fa4, 0x2fa5, 0x2fa6, 0x2fa7, 0x2fa8, 0x2fa9, 0x2faa, + 0x2fab, 0x2fac, 0x2fad, 0x2fae, 0x2faf, 0x2fb0, 0x2fb1, 0x2fb2, + 0x2fb3, 0x2fb4, 0x2fb5, 0x2fb6, 0x2fb7, 0x2fb8, 0x2fb9, 0x2fba, + 0x2fbb, 0x4c52, 0x4c53, 0x4e28, 0x4e29, 0x4e2a, 0x4e2b, 0x4e2c, + 0x4e2d, 0x4e2e, 0x4e2f, 0x4e30, 0x4e31, 0x4e32, 0x4e33, 0x4e34, + 0x4e35, 0x4e36, 0x4e37, 0x4e38, 0x4e39, 0x4e3a, 0x4e3b, 0x4e3c, + 0x4e3d, 0x4e3e, 0x4e3f, 0x4e80, 0x4e81, 0x4e82, 0x4e83, 0x4e84, + 0x4e85, 0x4e86, 0x4e87, 0x4e88, 0x4e89, 0x4e8a, 0x4e8b, 0x4e8c, + 0x4e8d, 0x4e8e, 0x4e8f, 0x4e90, 0x4e91, 0x4e92, 0x4e93, 0x4e94, + 0x4e95, 0x4e96, 0x4e97, 0x4e98, 0x4e99, 0x4e9a, 0x4e9b, 0x4e9c, + 0x4e9d, 0x4e9e, 0x4e9f, 0x4ea0, 0x4ea1, 0x4ea2, 0x4ea3, 0x4ea4, + 0x4ea5, 0x4ea6, 0x4ea7, 0x4ea8, 0x4ea9, 0x4eaa, 0x4eab, 0x4eac, + 0x4ead, 0x4eae, 0x4eaf, 0x4eb0, 0x4eb1, 0x4eb2, 0x4eb3, 0x4eb4, + 0x4eb5, 0x4eb6, 0x4eb7, 0x4eb8, 0x4eb9, 0x4eba, 0x4ebb, 0x4ebc, + 0x4ebd, 0x4ebe, 0x4ebf, 0x4ec0, 0x4ec1, 0x4ec2, 0x4ec3, 0x4ec4, + 0x4ec5, 0x4ec6, 0x4ec7, 0x4ec8, 0x4ec9, 0x4eca, 0x4ecb, 0x6a04, + 0x6a05, 0x07e2, 0x07e3, 0x07e4, 0x07e5, 0x07e6, 0x07e7, 0x07e8, + 0x07e9, 0x07ea, 0x07eb, 0x07ec, 0x07ed, 0x07ee, 0x07ef, 0x07f0, + 0x07f1, 0x07f2, 0x07f3, 0x07f4, 0x07f5, 0x07f6, 0x07f7, 0x07f8, + 0x07f9, 0x07fa, 0x07fb, 0x07fc, 0x07fd, 0x07fe, 0x07ff, 0x2000, + 0x2001, 0x2002, 0x2003, 0x2004, 0x2005, 0x2006, 0x2007, 0x2008, + 0x2009, 0x200a, 0x200b, 0x200c, 0x200d, 0x200e, 0x200f, 0x2010, + 0x2011, 0x2012, 0x2013, 0x2014, 0x2015, 0x2016, 0x2017, 0x2018, + 0x2019, 0x201a, 0x201b, 0x201c, 0x201d, 0x201e, 0x201f, 0x2020, + 0x2021, 0x2022, 0x2023, 0x2024, 0x2025, 0x2026, 0x2027, 0x2028, + 0x2029, 0x202a, 0x202b, 0x202c, 0x202d, 0x202e, 0x202f, 0x2030, + 0x2031, 0x2032, 0x2033, 0x2034, 0x2035, 0x2036, 0x2037, 0x2038, + 0x2039, 0x203a, 0x203b, 0x203c, 0x203d, 0x203e, 0x203f, 0x2040, + 0x2041, 0x2042, 0x2043, 0x2044, 0x2045, 0x2046, 0x2047, 0x2048, + 0x2049, 0x204a, 0x204b, 0x204c, 0x204d, 0x204e, 0x204f, 0x2050, + 0x2051, 0x2052, 0x2053, 0x2054, 0x2055, 0x2056, 0x2057, 0x2058, + 0x2059, 0x205a, 0x205b, 0x205c, 0x205d, 0x205e, 0x205f, 0x2060, + 0x2061, 0x2062, 0x2063, 0x2064, 0x2065, 0x2066, 0x2067, 0x2068, + 0x2069, 0x206a, 0x206b, 0x206c, 0x206d, 0x206e, 0x206f, 0x2070, + 0x2071, 0x2072, 0x2073, 0x2074, 0x2075, 0x2076, 0x2077, 0x2078, + 0x2079, 0x4cba, 0x4cbb, 0x5d88, 0x5d89, 0x5d8a, 0x5d8b, 0x5d8c, + 0x5d8d, 0x5d8e, 0x5d8f, 0x5db0, 0x5db1, 0x5db2, 0x5db3, 0x5db4, + 0x5db5, 0x5db6, 0x5db7, 0x5db8, 0x5db9, 0x5dba, 0x5dbb, 0x5dbc, + 0x5dbd, 0x5dbe, 0x5dbf, 0x5e40, 0x5e41, 0x5e42, 0x5e43, 0x5e44, + 0x5e45, 0x5e46, 0x5e47, 0x5e48, 0x5e49, 0x5e4a, 0x5e4b, 0x5e4c, + 0x5e4d, 0x5e4e, 0x5e4f, 0x5e50, 0x5e51, 0x5e52, 0x5e53, 0x5e54, + 0x5e55, 0x5e56, 0x5e57, 0x5e58, 0x5e59, 0x5e5a, 0x5e5b, 0x5e5c, + 0x5e5d, 0x5e5e, 0x5e5f, 0x5e60, 0x5e61, 0x5e62, 0x5e63, 0x5e64, + 0x5e65, 0x5e66, 0x5e67, 0x5e68, 0x5e69, 0x5e6a, 0x5e6b, 0x5e6c, + 0x5e6d, 0x5e6e, 0x5e6f, 0x5e70, 0x5e71, 0x5e72, 0x5e73, 0x5e74, + 0x5e75, 0x5e76, 0x5e77, 0x5e78, 0x5e79, 0x5e7a, 0x5e7b, 0x5e7c, + 0x5e7d, 0x5e7e, 0x5e7f, 0x5e80, 0x5e81, 0x5e82, 0x5e83, 0x5e84, + 0x5e85, 0x5e86, 0x5e87, 0x5e88, 0x5e89, 0x5e8a, 0x5e8b, 0x5e8c, + 0x5e8d, 0x5e8e, 0x5e8f, 0x5e90, 0x5e91, 0x5e92, 0x5e93, 0x5e94, + 0x5e95, 0x5e96, 0x5e97, 0x5e98, 0x5e99, 0x5e9a, 0x5e9b, 0x5e9c, + 0x5e9d, 0x5e9e, 0x5e9f, 0x5ea0, 0x5ea1, 0x5ea2, 0x5ea3, 0x5ea4, + 0x5ea5, 0x5ea6, 0x5ea7, 0x5ea8, 0x5ea9, 0x5eaa, 0x5eab, 0x5eac, + 0x5ead, 0x5eae, 0x5eaf, 0x5eb0, 0x5eb1, 0x5eb2, 0x5eb3, 0x5eb4, + 0x5eb5, 0x5eb6, 0x5eb7, 0x5eb8, 0x5eb9, 0x5eba, 0x5ebb, 0x5ebc, + 0x5ebd, 0x5ebe, 0x5ebf, 0x5ec0, 0x5ec1, 0x5ec2, 0x5ec3, 0x5ec4, + 0x5ec5, 0x5ec6, 0x5ec7, 0x5ec8, 0x5ec9, 0x5eca, 0x5ecb, 0x5ecc, + 0x5ecd, 0x5ece, 0x5ecf, 0x5ed0, 0x5ed1, 0x5ed2, 0x5ed3, 0x5ed4, + 0x5ed5, 0x5ed6, 0x5ed7, 0x5ed8, 0x5ed9, 0x5eda, 0x5edb, 0x5edc, + 0x5edd, 0x5ede, 0x5edf, 0x5ee0, 0x5ee1, 0x5ee2, 0x5ee3, 0x5ee4, + 0x5ee5, 0x5ee6, 0x5ee7, 0x5ee8, 0x5ee9, 0x5eea, 0x5eeb, 0x5eec, + 0x5eed, 0x5eee, 0x5eef, 0x5ef0, 0x5ef1, 0x5ef2, 0x5ef3, 0x5ef4, + 0x5ef5, 0x5ef6, 0x5ef7, 0x5ef8, 0x5ef9, 0x5efa, 0x5efb, 0x5efc, + 0x5efd, 0x5efe, 0x5eff, 0x5f00, 0x5f01, 0x5f02, 0x5f03, 0x5f04, + 0x5f05, 0x5f06, 0x5f07, 0x5f08, 0x5f09, 0x5f0a, 0x5f0b, 0x5f0c, + 0x5f0d, 0x5f0e, 0x5f0f, 0x0000, +}; + +static const uint8_t table0_mv_bits[1100] = { + 1, 4, 4, 4, 5, 5, 5, 6, + 6, 6, 7, 7, 7, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, + 8, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 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, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 16, + 16, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 8, +}; + +static const uint8_t table0_mvx[1099] = { + 32, 32, 31, 32, 33, 31, 33, 31, + 33, 32, 34, 32, 30, 32, 31, 34, + 35, 32, 34, 33, 29, 33, 30, 30, + 31, 31, 35, 29, 33, 35, 33, 34, + 31, 29, 30, 34, 30, 36, 28, 32, + 34, 37, 30, 27, 32, 25, 39, 32, + 34, 32, 35, 35, 35, 31, 35, 29, + 32, 29, 30, 29, 37, 27, 36, 38, + 37, 33, 32, 31, 29, 31, 28, 36, + 33, 30, 34, 33, 33, 28, 27, 25, + 31, 26, 39, 32, 32, 31, 33, 39, + 31, 38, 28, 36, 21, 23, 43, 36, + 34, 41, 30, 25, 28, 31, 30, 34, + 38, 35, 61, 34, 28, 30, 37, 37, + 35, 27, 36, 3, 59, 38, 37, 32, + 31, 29, 26, 33, 37, 33, 27, 27, + 35, 34, 34, 40, 42, 33, 32, 29, + 4, 5, 28, 24, 25, 35, 39, 38, + 32, 23, 27, 32, 30, 35, 26, 34, + 60, 36, 29, 22, 26, 41, 7, 30, + 38, 30, 36, 29, 30, 41, 26, 25, + 32, 34, 24, 39, 1, 25, 39, 32, + 28, 29, 32, 38, 26, 36, 28, 63, + 28, 39, 23, 21, 26, 35, 31, 35, + 57, 31, 29, 29, 28, 30, 27, 35, + 2, 38, 40, 34, 37, 29, 38, 43, + 26, 32, 33, 42, 24, 40, 28, 32, + 32, 32, 36, 32, 43, 25, 21, 31, + 30, 31, 41, 29, 33, 37, 26, 37, + 27, 59, 23, 33, 35, 31, 31, 37, + 38, 39, 32, 23, 32, 27, 37, 36, + 31, 40, 25, 27, 38, 31, 36, 28, + 31, 36, 25, 45, 3, 34, 38, 39, + 40, 38, 30, 32, 19, 24, 25, 26, + 45, 20, 24, 33, 33, 31, 41, 34, + 39, 47, 40, 58, 59, 41, 33, 3, + 17, 61, 42, 30, 26, 29, 36, 61, + 33, 37, 62, 28, 25, 38, 25, 38, + 17, 23, 34, 33, 21, 33, 49, 27, + 32, 23, 27, 22, 24, 22, 39, 43, + 27, 37, 6, 42, 47, 26, 30, 31, + 41, 39, 33, 22, 45, 36, 32, 45, + 19, 22, 30, 5, 5, 17, 29, 22, + 31, 31, 43, 37, 27, 32, 32, 32, + 33, 34, 43, 35, 29, 26, 22, 32, + 19, 32, 25, 31, 41, 49, 28, 34, + 28, 39, 34, 19, 37, 38, 29, 21, + 36, 42, 24, 48, 16, 28, 49, 22, + 34, 31, 38, 39, 44, 11, 35, 30, + 33, 33, 23, 28, 33, 46, 15, 13, + 24, 41, 24, 34, 34, 30, 26, 24, + 14, 60, 21, 29, 39, 23, 35, 37, + 63, 45, 33, 34, 47, 41, 22, 42, + 35, 35, 23, 32, 35, 43, 32, 7, + 31, 41, 20, 31, 16, 13, 63, 25, + 30, 32, 35, 30, 30, 31, 42, 47, + 39, 38, 40, 40, 51, 55, 56, 18, + 21, 39, 39, 33, 17, 41, 23, 24, + 43, 25, 31, 20, 19, 45, 1, 34, + 31, 22, 35, 15, 46, 46, 35, 31, + 28, 29, 29, 23, 41, 27, 14, 53, + 53, 27, 24, 32, 57, 32, 17, 42, + 37, 29, 33, 1, 25, 32, 32, 63, + 26, 40, 44, 36, 31, 39, 20, 20, + 44, 23, 33, 34, 35, 33, 33, 28, + 41, 23, 41, 41, 29, 25, 26, 49, + 29, 24, 37, 49, 50, 51, 51, 26, + 39, 25, 26, 15, 39, 18, 42, 17, + 4, 31, 32, 32, 60, 1, 42, 32, + 0, 12, 19, 35, 21, 41, 17, 26, + 20, 45, 46, 32, 37, 22, 47, 29, + 31, 27, 29, 30, 21, 33, 35, 18, + 25, 33, 50, 51, 42, 2, 15, 51, + 53, 33, 25, 29, 55, 37, 38, 33, + 38, 59, 38, 33, 39, 13, 32, 40, + 61, 61, 32, 9, 44, 3, 31, 29, + 25, 31, 27, 23, 9, 25, 9, 29, + 20, 30, 30, 42, 18, 28, 25, 28, + 28, 21, 29, 43, 29, 43, 26, 44, + 44, 21, 38, 21, 24, 45, 45, 35, + 39, 22, 35, 36, 34, 34, 45, 34, + 29, 31, 46, 25, 46, 16, 17, 31, + 20, 32, 47, 47, 47, 32, 49, 49, + 49, 31, 1, 27, 28, 39, 39, 21, + 36, 23, 51, 2, 40, 51, 32, 53, + 24, 30, 24, 30, 21, 40, 57, 57, + 31, 41, 58, 32, 12, 4, 32, 34, + 59, 31, 32, 13, 9, 35, 26, 35, + 37, 61, 37, 63, 26, 29, 41, 38, + 23, 20, 41, 26, 41, 42, 42, 42, + 26, 26, 26, 26, 1, 26, 37, 37, + 37, 23, 34, 42, 27, 43, 34, 27, + 31, 24, 33, 16, 3, 31, 24, 33, + 24, 4, 44, 44, 11, 44, 31, 13, + 13, 44, 45, 13, 25, 22, 38, 26, + 38, 38, 39, 32, 30, 39, 30, 22, + 32, 26, 30, 47, 47, 47, 19, 47, + 30, 31, 35, 8, 23, 47, 47, 27, + 35, 47, 31, 48, 35, 19, 36, 49, + 49, 33, 31, 39, 27, 39, 49, 49, + 50, 50, 50, 39, 31, 51, 51, 39, + 28, 33, 33, 21, 40, 31, 52, 53, + 40, 53, 9, 33, 31, 53, 54, 54, + 54, 55, 55, 34, 15, 56, 25, 56, + 21, 21, 40, 40, 25, 40, 58, 36, + 5, 41, 41, 12, 60, 41, 41, 37, + 22, 61, 18, 29, 29, 30, 61, 30, + 61, 62, 62, 30, 30, 63, 18, 13, + 30, 23, 19, 20, 20, 41, 13, 2, + 5, 5, 1, 5, 32, 6, 32, 35, + 20, 35, 27, 35, 35, 36, 36, 13, + 36, 41, 41, 41, 3, 30, 42, 27, + 20, 30, 27, 28, 30, 21, 33, 33, + 14, 24, 30, 42, 24, 33, 25, 42, + 43, 14, 43, 43, 14, 43, 7, 36, + 37, 37, 37, 37, 7, 14, 25, 43, + 43, 44, 15, 37, 7, 7, 3, 1, + 8, 15, 15, 8, 44, 44, 44, 45, + 45, 45, 45, 8, 8, 45, 21, 45, + 28, 28, 28, 21, 28, 28, 22, 37, + 46, 46, 37, 8, 29, 37, 29, 22, + 46, 37, 22, 29, 47, 47, 38, 38, + 16, 38, 38, 33, 38, 22, 47, 47, + 29, 25, 16, 0, 48, 1, 34, 48, + 48, 34, 25, 26, 26, 49, 49, 26, + 1, 49, 4, 26, 4, 49, 1, 9, + 49, 49, 49, 10, 49, 17, 38, 17, + 17, 50, 38, 50, 50, 22, 38, 51, + 38, 38, 51, 39, 39, 18, 22, 39, + 51, 22, 52, 52, 52, 39, 53, 53, + 10, 23, 18, 29, 10, 53, 29, 54, + 11, 54, 11, 11, 55, 1, 18, 55, + 55, 55, 55, 55, 55, 29, 34, 18, + 29, 56, 56, 34, 57, 34, 34, 29, + 29, 57, 57, 35, 35, 35, 35, 35, + 39, 35, 59, 59, 18, 59, 39, 30, + 18, 40, 60, 60, 61, 30, 18, 61, + 61, 19, 19, +}; + +static const uint8_t table0_mvy[1099] = { + 32, 31, 32, 33, 32, 31, 31, 33, + 33, 34, 32, 30, 32, 35, 34, 31, + 32, 29, 33, 30, 32, 34, 33, 31, + 30, 35, 31, 31, 29, 33, 35, 30, + 29, 33, 34, 34, 30, 32, 32, 36, + 29, 32, 35, 32, 28, 32, 32, 27, + 35, 37, 34, 29, 30, 36, 35, 34, + 25, 30, 29, 35, 33, 31, 31, 32, + 31, 28, 39, 28, 29, 37, 31, 33, + 27, 36, 28, 36, 37, 33, 33, 31, + 27, 32, 31, 38, 26, 25, 25, 33, + 39, 31, 34, 30, 32, 32, 32, 34, + 36, 32, 28, 33, 30, 38, 37, 27, + 33, 28, 32, 37, 35, 38, 29, 34, + 27, 29, 29, 32, 32, 34, 35, 3, + 26, 36, 31, 38, 30, 26, 35, 34, + 37, 26, 25, 32, 32, 39, 23, 37, + 32, 32, 29, 32, 29, 36, 29, 30, + 41, 31, 30, 21, 39, 25, 34, 38, + 32, 35, 39, 32, 33, 33, 32, 27, + 29, 25, 28, 27, 26, 31, 30, 35, + 24, 24, 31, 34, 32, 30, 35, 40, + 28, 38, 5, 35, 29, 36, 36, 32, + 38, 30, 33, 31, 35, 26, 23, 38, + 32, 41, 28, 25, 37, 40, 37, 39, + 32, 36, 33, 39, 25, 26, 28, 31, + 28, 42, 23, 31, 33, 31, 39, 1, + 59, 22, 27, 4, 33, 34, 33, 24, + 41, 3, 35, 41, 41, 28, 36, 36, + 28, 33, 35, 21, 23, 21, 22, 37, + 27, 27, 43, 29, 60, 39, 27, 25, + 59, 34, 27, 27, 26, 40, 37, 27, + 61, 26, 39, 33, 31, 22, 37, 25, + 30, 25, 24, 61, 31, 34, 25, 38, + 32, 32, 30, 3, 61, 43, 29, 23, + 28, 32, 28, 32, 31, 34, 5, 33, + 32, 33, 33, 42, 37, 23, 38, 31, + 40, 26, 32, 26, 37, 38, 36, 24, + 29, 30, 20, 22, 29, 24, 32, 41, + 2, 34, 25, 33, 29, 31, 39, 35, + 36, 24, 32, 30, 33, 27, 44, 60, + 30, 36, 19, 34, 31, 24, 16, 35, + 32, 38, 21, 33, 31, 31, 21, 35, + 5, 17, 29, 38, 38, 18, 58, 19, + 43, 41, 30, 41, 43, 39, 29, 7, + 29, 17, 28, 19, 28, 31, 25, 19, + 40, 26, 21, 33, 39, 23, 40, 30, + 39, 34, 35, 32, 32, 24, 33, 30, + 40, 47, 39, 37, 32, 33, 24, 23, + 45, 47, 27, 23, 42, 32, 32, 33, + 36, 37, 37, 17, 18, 22, 40, 38, + 32, 31, 35, 24, 17, 25, 17, 23, + 33, 34, 51, 42, 31, 36, 36, 29, + 21, 22, 37, 44, 43, 25, 47, 33, + 45, 27, 31, 58, 31, 32, 31, 38, + 43, 20, 47, 45, 54, 1, 26, 34, + 38, 14, 22, 24, 33, 34, 32, 32, + 37, 21, 23, 49, 35, 23, 28, 39, + 39, 23, 55, 33, 30, 30, 63, 16, + 42, 28, 13, 33, 33, 35, 19, 46, + 43, 17, 19, 36, 39, 24, 31, 32, + 33, 26, 28, 62, 33, 63, 33, 39, + 19, 49, 17, 31, 43, 13, 15, 29, + 25, 35, 33, 23, 49, 41, 28, 29, + 34, 38, 7, 61, 11, 50, 13, 41, + 19, 47, 25, 26, 15, 42, 41, 29, + 45, 27, 17, 35, 32, 29, 32, 24, + 13, 26, 26, 31, 24, 33, 28, 30, + 31, 11, 45, 46, 33, 33, 35, 57, + 32, 32, 35, 45, 34, 11, 37, 42, + 39, 37, 31, 49, 21, 27, 29, 47, + 53, 40, 51, 16, 26, 1, 40, 30, + 41, 44, 34, 25, 27, 31, 35, 35, + 31, 15, 49, 1, 35, 40, 5, 58, + 21, 29, 22, 59, 45, 31, 9, 26, + 9, 29, 11, 32, 30, 3, 13, 20, + 18, 20, 11, 3, 29, 40, 31, 53, + 30, 17, 20, 37, 31, 42, 47, 47, + 54, 38, 9, 34, 13, 37, 21, 25, + 27, 43, 42, 45, 40, 25, 27, 46, + 22, 25, 53, 20, 2, 14, 39, 15, + 22, 44, 34, 21, 38, 33, 27, 48, + 34, 52, 35, 47, 49, 54, 2, 13, + 23, 52, 29, 45, 22, 49, 54, 21, + 40, 42, 31, 30, 29, 34, 0, 25, + 23, 51, 24, 59, 28, 38, 29, 31, + 2, 13, 31, 8, 31, 33, 12, 45, + 41, 7, 14, 30, 25, 18, 43, 20, + 43, 35, 44, 1, 49, 42, 42, 18, + 41, 38, 41, 44, 53, 11, 20, 25, + 45, 46, 47, 48, 39, 52, 46, 49, + 63, 55, 44, 38, 13, 13, 57, 22, + 51, 16, 12, 28, 35, 57, 25, 20, + 26, 28, 28, 29, 32, 31, 62, 34, + 35, 35, 19, 49, 48, 39, 40, 18, + 43, 46, 11, 6, 48, 19, 49, 41, + 10, 23, 58, 17, 21, 23, 34, 30, + 60, 0, 44, 34, 26, 37, 46, 43, + 49, 59, 4, 34, 59, 37, 22, 25, + 28, 46, 6, 40, 59, 42, 36, 61, + 28, 30, 31, 43, 10, 22, 23, 47, + 20, 52, 55, 36, 25, 16, 1, 11, + 27, 29, 5, 63, 18, 41, 31, 34, + 38, 1, 5, 13, 28, 31, 17, 38, + 39, 41, 36, 37, 22, 39, 33, 43, + 43, 15, 17, 49, 30, 21, 22, 20, + 10, 17, 25, 54, 57, 3, 34, 8, + 36, 25, 31, 14, 15, 19, 29, 25, + 18, 39, 53, 22, 27, 20, 29, 33, + 41, 42, 35, 62, 50, 29, 53, 50, + 35, 55, 42, 61, 63, 4, 7, 42, + 21, 46, 47, 49, 27, 46, 17, 55, + 41, 50, 63, 4, 56, 18, 8, 10, + 18, 51, 63, 36, 55, 18, 5, 55, + 9, 29, 17, 21, 30, 27, 1, 59, + 7, 11, 12, 15, 5, 42, 24, 41, + 43, 7, 27, 22, 25, 31, 30, 37, + 22, 39, 53, 29, 36, 37, 48, 0, + 5, 13, 17, 31, 32, 26, 46, 28, + 44, 45, 46, 53, 49, 51, 3, 41, + 3, 22, 42, 33, 5, 45, 7, 22, + 40, 53, 24, 14, 25, 27, 10, 12, + 34, 16, 17, 53, 20, 26, 39, 45, + 18, 45, 35, 33, 31, 49, 4, 39, + 42, 11, 51, 5, 13, 26, 27, 17, + 52, 30, 0, 22, 12, 34, 62, 36, + 38, 41, 47, 30, 63, 38, 41, 43, + 59, 33, 45, 37, 38, 40, 47, 24, + 48, 49, 30, 1, 10, 22, 49, 15, + 39, 59, 31, 32, 33, 18, 13, 15, + 31, 21, 27, 44, 42, 39, 46, 17, + 26, 32, 30, 31, 0, 30, 34, 9, + 12, 13, 25, 31, 32, 55, 43, 35, + 61, 33, 35, 46, 25, 47, 48, 62, + 63, 38, 61, 1, 2, 5, 7, 9, + 46, 10, 34, 35, 36, 55, 51, 7, + 40, 23, 34, 37, 5, 13, 42, 18, + 25, 27, 28, +}; + +/* motion vector table 1 */ +static const uint16_t table1_mv_code[1100] = { + 0x0000, 0x0007, 0x0009, 0x000f, 0x000a, 0x0011, 0x001a, 0x001c, + 0x0011, 0x0031, 0x0025, 0x002d, 0x002f, 0x006f, 0x0075, 0x0041, + 0x004c, 0x004e, 0x005c, 0x0060, 0x0062, 0x0066, 0x0068, 0x0069, + 0x006b, 0x00a6, 0x00c1, 0x00cb, 0x00cc, 0x00ce, 0x00da, 0x00e8, + 0x00ee, 0x0087, 0x0090, 0x009e, 0x009f, 0x00ba, 0x00ca, 0x00d8, + 0x00db, 0x00df, 0x0104, 0x0109, 0x010c, 0x0143, 0x0145, 0x014a, + 0x0156, 0x015c, 0x01b3, 0x01d3, 0x01da, 0x0103, 0x0109, 0x010b, + 0x0122, 0x0127, 0x0134, 0x0161, 0x0164, 0x0176, 0x0184, 0x018d, + 0x018e, 0x018f, 0x0190, 0x0193, 0x0196, 0x019d, 0x019e, 0x019f, + 0x01a9, 0x01b2, 0x01b4, 0x01ba, 0x01bb, 0x01bc, 0x0201, 0x0202, + 0x0205, 0x0207, 0x020d, 0x0210, 0x0211, 0x0215, 0x021b, 0x021f, + 0x0281, 0x0285, 0x0290, 0x029c, 0x029d, 0x02a2, 0x02a7, 0x02a8, + 0x02aa, 0x02b0, 0x02b1, 0x02b4, 0x02bc, 0x02bf, 0x0320, 0x0326, + 0x0327, 0x0329, 0x032a, 0x0336, 0x0360, 0x0362, 0x0363, 0x0372, + 0x03b2, 0x03bc, 0x03bd, 0x0203, 0x0205, 0x021a, 0x0249, 0x024a, + 0x024c, 0x02c7, 0x02ca, 0x02ce, 0x02ef, 0x030d, 0x0322, 0x0325, + 0x0338, 0x0373, 0x037a, 0x0409, 0x0415, 0x0416, 0x0418, 0x0428, + 0x042d, 0x042f, 0x0434, 0x0508, 0x0509, 0x0510, 0x0511, 0x051c, + 0x051e, 0x0524, 0x0541, 0x0543, 0x0546, 0x0547, 0x054d, 0x0557, + 0x055f, 0x056a, 0x056c, 0x056d, 0x056f, 0x0576, 0x0577, 0x057a, + 0x057b, 0x057c, 0x057d, 0x0600, 0x0601, 0x0603, 0x0614, 0x0616, + 0x0617, 0x061c, 0x061f, 0x0642, 0x0648, 0x0649, 0x064a, 0x064b, + 0x0657, 0x0668, 0x0669, 0x066b, 0x066e, 0x067f, 0x06c2, 0x06c8, + 0x06cb, 0x06de, 0x06df, 0x06e2, 0x06e3, 0x06ef, 0x0748, 0x074b, + 0x076e, 0x076f, 0x077c, 0x0409, 0x0423, 0x0428, 0x0429, 0x042a, + 0x042b, 0x0432, 0x0433, 0x0496, 0x049a, 0x04d5, 0x04db, 0x0581, + 0x0582, 0x058b, 0x058c, 0x058d, 0x0598, 0x0599, 0x059a, 0x059e, + 0x05dd, 0x0619, 0x0632, 0x0633, 0x0648, 0x0672, 0x06a1, 0x06a2, + 0x06a3, 0x06af, 0x06e2, 0x06e3, 0x06e4, 0x0800, 0x0801, 0x0802, + 0x0803, 0x081a, 0x081b, 0x0829, 0x082f, 0x0832, 0x083e, 0x083f, + 0x0852, 0x0853, 0x0858, 0x086b, 0x0877, 0x0878, 0x0879, 0x087a, + 0x087b, 0x0a00, 0x0a01, 0x0a0d, 0x0a0e, 0x0a0f, 0x0a24, 0x0a37, + 0x0a3a, 0x0a3b, 0x0a3e, 0x0a46, 0x0a47, 0x0a4a, 0x0a4b, 0x0a5f, + 0x0a79, 0x0a7a, 0x0a7b, 0x0a80, 0x0a81, 0x0a84, 0x0a85, 0x0a99, + 0x0aa5, 0x0aa6, 0x0ab8, 0x0aba, 0x0abb, 0x0abc, 0x0abd, 0x0ac8, + 0x0ace, 0x0acf, 0x0ad7, 0x0adc, 0x0aeb, 0x0c04, 0x0c25, 0x0c26, + 0x0c27, 0x0c2a, 0x0c2b, 0x0c3a, 0x0c3b, 0x0c3c, 0x0c3d, 0x0ca0, + 0x0cad, 0x0cd4, 0x0cd5, 0x0cfc, 0x0cfd, 0x0d86, 0x0d92, 0x0d93, + 0x0d94, 0x0d95, 0x0db0, 0x0db8, 0x0db9, 0x0dba, 0x0dbb, 0x0dc0, + 0x0dc2, 0x0dc3, 0x0dda, 0x0ddb, 0x0ddc, 0x0ddd, 0x0e92, 0x0e93, + 0x0e94, 0x0e95, 0x0ec7, 0x0ecc, 0x0ece, 0x0ecf, 0x0ed8, 0x0ed9, + 0x0eda, 0x0edb, 0x0808, 0x0809, 0x080a, 0x0810, 0x0811, 0x0844, + 0x0845, 0x0861, 0x0862, 0x0863, 0x086c, 0x0922, 0x0923, 0x092e, + 0x092f, 0x0936, 0x0937, 0x09b1, 0x09b2, 0x09b3, 0x09b4, 0x09b5, + 0x09b8, 0x09b9, 0x09ba, 0x09bb, 0x09bc, 0x09bd, 0x09be, 0x09bf, + 0x0b00, 0x0b15, 0x0b2c, 0x0b2d, 0x0b2e, 0x0b2f, 0x0b36, 0x0bb9, + 0x0c28, 0x0c2a, 0x0c2b, 0x0c2c, 0x0c2d, 0x0c2e, 0x0c2f, 0x0c30, + 0x0c31, 0x0c38, 0x0c60, 0x0c61, 0x0c62, 0x0c63, 0x0c8d, 0x0c8e, + 0x0c8f, 0x0c92, 0x0cbe, 0x0cbf, 0x0ce6, 0x0ce7, 0x0d40, 0x0d41, + 0x0d57, 0x0d58, 0x0d59, 0x0d5a, 0x0d5b, 0x0d5c, 0x0d5d, 0x0d98, + 0x0d99, 0x0d9a, 0x0d9b, 0x0d9c, 0x0d9d, 0x0dad, 0x0dae, 0x0daf, + 0x0dc0, 0x0dc1, 0x0dc2, 0x0dc3, 0x0dca, 0x0dcb, 0x0dec, 0x0ded, + 0x0dee, 0x0def, 0x1018, 0x1022, 0x1023, 0x1030, 0x1031, 0x1032, + 0x1033, 0x1050, 0x1051, 0x105c, 0x1074, 0x1075, 0x1076, 0x1077, + 0x1078, 0x1079, 0x107a, 0x107b, 0x10b2, 0x10b3, 0x10b8, 0x10b9, + 0x10ba, 0x10bb, 0x10d4, 0x10ea, 0x10eb, 0x10ec, 0x10ed, 0x1404, + 0x1405, 0x1406, 0x1407, 0x1410, 0x1411, 0x1412, 0x1413, 0x1414, + 0x1415, 0x1416, 0x1417, 0x1418, 0x1419, 0x1466, 0x1467, 0x1468, + 0x1469, 0x146a, 0x146b, 0x146c, 0x146d, 0x147e, 0x147f, 0x1488, + 0x1489, 0x148a, 0x148b, 0x14b6, 0x14b7, 0x14b8, 0x14b9, 0x14ba, + 0x14bb, 0x14bc, 0x14bd, 0x14f0, 0x14f1, 0x14f8, 0x14f9, 0x14fa, + 0x14fb, 0x14fc, 0x14fd, 0x14fe, 0x14ff, 0x152a, 0x152b, 0x152c, + 0x152d, 0x152e, 0x152f, 0x1530, 0x1531, 0x1548, 0x1549, 0x154e, + 0x154f, 0x1558, 0x1559, 0x155a, 0x155b, 0x1572, 0x159a, 0x159b, + 0x15ac, 0x15ba, 0x15bb, 0x15d0, 0x15d1, 0x15d2, 0x15d3, 0x15d4, + 0x15d5, 0x181d, 0x181e, 0x181f, 0x1840, 0x1841, 0x1842, 0x1843, + 0x1844, 0x1845, 0x1846, 0x1847, 0x1848, 0x1849, 0x1861, 0x1862, + 0x1863, 0x1864, 0x1865, 0x1866, 0x1867, 0x1868, 0x1869, 0x186a, + 0x186b, 0x186c, 0x186d, 0x186e, 0x191b, 0x191c, 0x191d, 0x191e, + 0x191f, 0x1942, 0x1943, 0x1944, 0x1945, 0x1946, 0x1947, 0x1958, + 0x1959, 0x19ed, 0x19ee, 0x19ef, 0x19f0, 0x19f1, 0x19f2, 0x19f3, + 0x19f4, 0x19f5, 0x19f6, 0x19f7, 0x1b0e, 0x1b0f, 0x1b62, 0x1b63, + 0x1b64, 0x1b65, 0x1b66, 0x1b67, 0x1b68, 0x1b69, 0x1b6a, 0x1b6b, + 0x1b6c, 0x1b6d, 0x1b6e, 0x1b6f, 0x1b82, 0x1ba8, 0x1ba9, 0x1baa, + 0x1bab, 0x1bac, 0x1bad, 0x1bae, 0x1baf, 0x1bb0, 0x1bb1, 0x1bb2, + 0x1bb3, 0x1d80, 0x1d81, 0x1d82, 0x1d83, 0x1d84, 0x1d85, 0x1d86, + 0x1d87, 0x1d88, 0x1d89, 0x1d8a, 0x1d8b, 0x1d8c, 0x1d8d, 0x1007, + 0x1008, 0x1009, 0x100a, 0x100b, 0x100c, 0x100d, 0x100e, 0x100f, + 0x1016, 0x1080, 0x1081, 0x1082, 0x1083, 0x1084, 0x1085, 0x1086, + 0x1087, 0x10c0, 0x123a, 0x123b, 0x123c, 0x123d, 0x123e, 0x123f, + 0x1240, 0x1241, 0x1242, 0x1243, 0x1350, 0x1352, 0x1353, 0x1358, + 0x1359, 0x135a, 0x135b, 0x135c, 0x135d, 0x135e, 0x135f, 0x1360, + 0x1361, 0x1602, 0x1603, 0x160c, 0x160d, 0x160e, 0x160f, 0x1620, + 0x1621, 0x1622, 0x1623, 0x1624, 0x1625, 0x1626, 0x1627, 0x1628, + 0x1629, 0x166e, 0x166f, 0x167c, 0x167d, 0x167e, 0x167f, 0x1770, + 0x1771, 0x1852, 0x1853, 0x1872, 0x1873, 0x1874, 0x1875, 0x1876, + 0x1877, 0x1878, 0x1879, 0x187a, 0x187b, 0x187c, 0x187d, 0x187e, + 0x187f, 0x1918, 0x1919, 0x1926, 0x1927, 0x1970, 0x1971, 0x1972, + 0x1973, 0x1974, 0x1975, 0x1976, 0x1977, 0x1978, 0x1979, 0x197a, + 0x197b, 0x1aa0, 0x1aa1, 0x1aa2, 0x1aa3, 0x1aa4, 0x1aa5, 0x1aa6, + 0x1aa7, 0x1aa8, 0x1aa9, 0x1aaa, 0x1aab, 0x1aac, 0x1aad, 0x1b3c, + 0x1b3d, 0x1b3e, 0x1b3f, 0x1b50, 0x1b51, 0x1b52, 0x1b53, 0x1b54, + 0x1b55, 0x1b56, 0x1b57, 0x1b58, 0x1b59, 0x2032, 0x2033, 0x2034, + 0x2035, 0x2036, 0x2037, 0x2038, 0x2039, 0x203a, 0x203b, 0x203c, + 0x203d, 0x203e, 0x203f, 0x2040, 0x2041, 0x2042, 0x2043, 0x20ba, + 0x20bb, 0x20cc, 0x20cd, 0x20ce, 0x20cf, 0x20e0, 0x20e1, 0x20e2, + 0x20e3, 0x20e4, 0x20e5, 0x20e6, 0x20e7, 0x21aa, 0x21ab, 0x21c0, + 0x21c1, 0x21c2, 0x21c3, 0x21c4, 0x21c5, 0x21c6, 0x21c7, 0x21c8, + 0x21c9, 0x21ca, 0x21cb, 0x21cc, 0x21cd, 0x21ce, 0x21cf, 0x21d0, + 0x21d1, 0x21d2, 0x21d3, 0x2894, 0x2895, 0x2896, 0x2897, 0x2898, + 0x2899, 0x289a, 0x289b, 0x289c, 0x289d, 0x289e, 0x289f, 0x28c0, + 0x28c1, 0x28c2, 0x28c3, 0x28c4, 0x28c5, 0x28c6, 0x28c7, 0x28c8, + 0x28c9, 0x28ca, 0x28cb, 0x2930, 0x2931, 0x2932, 0x2933, 0x2934, + 0x2935, 0x2936, 0x2937, 0x2938, 0x2939, 0x293a, 0x293b, 0x293c, + 0x293d, 0x293e, 0x293f, 0x2960, 0x2961, 0x2962, 0x2963, 0x2964, + 0x2965, 0x2966, 0x2967, 0x2968, 0x2969, 0x296a, 0x296b, 0x2a40, + 0x2a41, 0x2a42, 0x2a43, 0x2a44, 0x2a45, 0x2a46, 0x2a47, 0x2a48, + 0x2a49, 0x2a4a, 0x2a4b, 0x2a4c, 0x2a4d, 0x2a4e, 0x2a4f, 0x2a50, + 0x2a51, 0x2a52, 0x2a53, 0x2ae6, 0x2ae7, 0x2b24, 0x2b25, 0x2b26, + 0x2b27, 0x2b28, 0x2b29, 0x2b2a, 0x2b2b, 0x2b2c, 0x2b2d, 0x2b2e, + 0x2b2f, 0x2b30, 0x2b31, 0x2b32, 0x2b33, 0x2b5a, 0x2b5b, 0x3014, + 0x3015, 0x3016, 0x3017, 0x3020, 0x3021, 0x3022, 0x3023, 0x3024, + 0x3025, 0x3026, 0x3027, 0x3028, 0x3029, 0x302a, 0x302b, 0x302c, + 0x302d, 0x302e, 0x302f, 0x3030, 0x3031, 0x3032, 0x3033, 0x3034, + 0x3035, 0x3036, 0x3037, 0x3038, 0x3039, 0x30c0, 0x30c1, 0x30de, + 0x30df, 0x3218, 0x3219, 0x321a, 0x321b, 0x321c, 0x321d, 0x321e, + 0x321f, 0x3220, 0x3221, 0x3222, 0x3223, 0x3224, 0x3225, 0x3226, + 0x3227, 0x3228, 0x3229, 0x322a, 0x322b, 0x322c, 0x322d, 0x322e, + 0x322f, 0x3230, 0x3231, 0x3232, 0x3233, 0x3234, 0x3235, 0x3378, + 0x3379, 0x337a, 0x337b, 0x337c, 0x337d, 0x337e, 0x337f, 0x33c0, + 0x33c1, 0x33c2, 0x33c3, 0x33c4, 0x33c5, 0x33c6, 0x33c7, 0x33c8, + 0x33c9, 0x33ca, 0x33cb, 0x33cc, 0x33cd, 0x33ce, 0x33cf, 0x33d0, + 0x33d1, 0x33d2, 0x33d3, 0x33d4, 0x33d5, 0x33d6, 0x33d7, 0x33d8, + 0x33d9, 0x3706, 0x3707, 0x3730, 0x3731, 0x3732, 0x3733, 0x3734, + 0x3735, 0x3736, 0x3737, 0x3738, 0x3739, 0x373a, 0x373b, 0x373c, + 0x373d, 0x373e, 0x373f, 0x3740, 0x3741, 0x3742, 0x3743, 0x3744, + 0x3745, 0x3746, 0x3747, 0x3748, 0x3749, 0x374a, 0x374b, 0x374c, + 0x374d, 0x374e, 0x374f, 0x3b34, 0x3b35, 0x3b36, 0x3b37, 0x3be8, + 0x3be9, 0x3bea, 0x3beb, 0x3bec, 0x3bed, 0x3bee, 0x3bef, 0x3bf0, + 0x3bf1, 0x3bf2, 0x3bf3, 0x3bf4, 0x3bf5, 0x3bf6, 0x3bf7, 0x3bf8, + 0x3bf9, 0x3bfa, 0x3bfb, 0x3bfc, 0x3bfd, 0x3bfe, 0x3bff, 0x2000, + 0x2001, 0x2002, 0x2003, 0x2004, 0x2005, 0x2006, 0x2007, 0x2008, + 0x2009, 0x200a, 0x200b, 0x200c, 0x200d, 0x202e, 0x202f, 0x2182, + 0x2183, 0x21b4, 0x21b5, 0x21b6, 0x21b7, 0x21b8, 0x21b9, 0x21ba, + 0x21bb, 0x21bc, 0x21bd, 0x21be, 0x21bf, 0x2460, 0x2461, 0x2462, + 0x2463, 0x2464, 0x2465, 0x2466, 0x2467, 0x2468, 0x2469, 0x246a, + 0x246b, 0x246c, 0x246d, 0x246e, 0x246f, 0x2470, 0x2471, 0x2472, + 0x2473, 0x26a2, 0x26a3, 0x000b, +}; + +static const uint8_t table1_mv_bits[1100] = { + 2, 4, 4, 4, 5, 5, 5, 5, + 6, 6, 7, 7, 7, 7, 7, 8, + 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, + 8, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 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, + 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, 11, 11, 11, + 11, 11, 11, 11, 11, 11, 11, 11, + 11, 11, 11, 11, 11, 11, 11, 11, + 11, 11, 11, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 15, + 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 4, +}; + +static const uint8_t table1_mvx[1099] = { + 32, 31, 32, 31, 33, 32, 33, 33, + 31, 34, 30, 32, 32, 34, 35, 32, + 34, 33, 29, 30, 30, 32, 31, 31, + 33, 35, 35, 33, 31, 29, 29, 33, + 34, 30, 31, 28, 36, 30, 34, 32, + 32, 37, 32, 32, 25, 27, 39, 32, + 32, 32, 38, 35, 36, 32, 37, 61, + 26, 32, 34, 35, 3, 35, 27, 28, + 29, 34, 28, 37, 31, 36, 32, 27, + 31, 30, 29, 39, 33, 29, 33, 35, + 25, 25, 29, 33, 31, 31, 31, 33, + 32, 30, 32, 32, 41, 39, 33, 36, + 32, 28, 34, 36, 38, 24, 60, 31, + 23, 28, 32, 33, 59, 32, 40, 30, + 5, 34, 32, 38, 32, 30, 43, 4, + 32, 32, 42, 31, 31, 32, 26, 38, + 26, 22, 21, 37, 61, 63, 37, 31, + 32, 33, 2, 1, 23, 33, 41, 27, + 35, 30, 38, 23, 33, 3, 28, 34, + 34, 27, 41, 29, 39, 35, 36, 29, + 32, 27, 30, 32, 24, 61, 37, 26, + 59, 25, 35, 27, 36, 37, 30, 31, + 34, 40, 3, 28, 34, 39, 32, 31, + 32, 30, 24, 28, 35, 36, 26, 32, + 31, 33, 29, 33, 39, 25, 30, 24, + 35, 59, 29, 34, 25, 30, 21, 35, + 43, 40, 32, 29, 5, 28, 31, 62, + 33, 33, 25, 31, 21, 31, 43, 31, + 34, 33, 20, 40, 39, 31, 31, 57, + 38, 32, 42, 33, 32, 31, 32, 29, + 30, 44, 5, 31, 22, 34, 36, 17, + 38, 58, 38, 35, 32, 60, 35, 24, + 32, 38, 16, 45, 42, 32, 31, 29, + 4, 30, 17, 40, 46, 48, 63, 32, + 42, 19, 41, 22, 28, 36, 45, 33, + 33, 32, 29, 7, 41, 42, 18, 33, + 33, 32, 22, 37, 1, 26, 22, 23, + 49, 28, 26, 27, 32, 33, 27, 23, + 28, 36, 15, 6, 34, 27, 31, 26, + 23, 2, 33, 32, 34, 41, 28, 32, + 41, 0, 36, 38, 34, 31, 47, 32, + 17, 31, 39, 33, 37, 51, 30, 47, + 32, 50, 32, 19, 63, 30, 25, 27, + 33, 62, 24, 31, 27, 30, 37, 31, + 45, 32, 39, 20, 46, 47, 35, 19, + 34, 1, 49, 21, 21, 14, 51, 26, + 23, 31, 36, 35, 58, 29, 29, 21, + 20, 42, 13, 28, 12, 40, 31, 33, + 39, 60, 32, 44, 33, 31, 28, 37, + 29, 32, 30, 49, 43, 28, 39, 25, + 32, 48, 2, 15, 20, 25, 31, 28, + 21, 24, 25, 15, 31, 17, 37, 43, + 18, 32, 33, 24, 33, 36, 13, 33, + 31, 39, 11, 31, 33, 32, 39, 37, + 32, 32, 29, 17, 44, 46, 36, 35, + 26, 37, 58, 32, 34, 38, 8, 38, + 38, 22, 29, 25, 16, 35, 32, 35, + 33, 43, 18, 46, 38, 50, 33, 18, + 53, 60, 13, 32, 36, 33, 51, 36, + 43, 45, 27, 42, 29, 24, 30, 25, + 31, 52, 31, 35, 38, 9, 22, 34, + 4, 17, 28, 55, 42, 25, 17, 20, + 47, 34, 33, 16, 40, 25, 16, 30, + 53, 29, 10, 11, 14, 26, 33, 4, + 35, 44, 26, 16, 31, 26, 34, 38, + 29, 31, 30, 24, 22, 61, 32, 9, + 45, 34, 31, 19, 9, 31, 46, 31, + 35, 54, 29, 57, 30, 50, 3, 31, + 63, 34, 47, 41, 51, 18, 31, 14, + 37, 38, 31, 24, 32, 31, 50, 33, + 31, 54, 27, 9, 33, 23, 19, 32, + 29, 29, 33, 28, 47, 49, 30, 47, + 33, 27, 25, 54, 44, 45, 50, 58, + 51, 48, 33, 59, 33, 34, 57, 13, + 26, 33, 13, 48, 30, 11, 7, 56, + 34, 55, 26, 0, 26, 35, 1, 51, + 33, 53, 31, 45, 12, 29, 29, 51, + 31, 48, 2, 6, 34, 30, 28, 33, + 60, 40, 27, 46, 31, 9, 35, 29, + 31, 39, 55, 46, 19, 37, 62, 34, + 30, 16, 19, 49, 41, 41, 39, 37, + 14, 5, 13, 35, 55, 30, 40, 40, + 42, 8, 20, 25, 45, 35, 33, 36, + 54, 38, 27, 37, 62, 40, 15, 59, + 49, 31, 29, 34, 34, 39, 24, 29, + 25, 29, 21, 29, 10, 61, 33, 49, + 35, 34, 3, 38, 39, 29, 7, 41, + 1, 35, 4, 23, 15, 23, 11, 37, + 28, 35, 30, 30, 24, 1, 43, 56, + 8, 34, 42, 24, 45, 30, 20, 23, + 8, 38, 22, 33, 17, 52, 34, 22, + 53, 43, 44, 1, 27, 31, 41, 43, + 41, 30, 31, 36, 30, 5, 55, 31, + 33, 30, 40, 23, 15, 29, 34, 34, + 59, 34, 30, 11, 13, 38, 5, 0, + 30, 42, 5, 30, 29, 34, 10, 44, + 30, 63, 35, 12, 3, 26, 15, 17, + 25, 34, 43, 39, 34, 56, 29, 23, + 30, 12, 30, 10, 35, 9, 24, 58, + 10, 12, 54, 33, 37, 20, 41, 35, + 29, 18, 61, 30, 40, 24, 39, 53, + 62, 26, 29, 33, 34, 53, 49, 21, + 27, 11, 63, 20, 26, 23, 7, 13, + 6, 47, 29, 30, 9, 51, 22, 34, + 21, 25, 33, 56, 57, 30, 38, 51, + 51, 38, 63, 28, 40, 35, 33, 18, + 33, 33, 24, 58, 58, 34, 49, 29, + 43, 4, 1, 4, 42, 35, 35, 30, + 17, 5, 56, 61, 25, 37, 36, 55, + 28, 35, 29, 50, 48, 52, 2, 42, + 34, 40, 46, 46, 43, 35, 29, 48, + 20, 29, 31, 41, 7, 30, 35, 19, + 14, 21, 8, 39, 39, 40, 46, 55, + 34, 6, 30, 34, 37, 25, 37, 33, + 22, 44, 52, 17, 35, 29, 36, 35, + 40, 37, 28, 30, 50, 14, 28, 55, + 6, 23, 19, 14, 30, 3, 30, 28, + 28, 61, 61, 47, 45, 48, 40, 40, + 34, 34, 25, 30, 29, 35, 4, 26, + 53, 50, 26, 41, 27, 59, 27, 38, + 39, 3, 50, 43, 47, 23, 33, 55, + 35, 21, 23, 35, 61, 33, 46, 52, + 35, 34, 24, 30, 43, 16, 37, 21, + 2, 24, 45, 34, 30, 55, 55, 1, + 29, 29, 26, 28, 25, 31, 36, 22, + 17, 30, 52, 2, 44, 44, 57, 26, + 62, 41, 39, 57, 26, 46, 49, 11, + 16, 19, 5, 59, 38, 39, 58, 38, + 25, 49, 50, 22, 28, 59, 9, 59, + 7, 28, 55, 17, 4, 35, 50, 21, + 29, 44, 47, 18, 24, 19, 25, 42, + 35, 3, 51, 35, 16, 35, 30, 63, + 57, 39, 39, 25, 35, 38, 9, 16, + 36, 45, 31, 60, 14, 34, 42, 24, + 0, 37, 18, 61, 57, 37, 28, 53, + 20, 46, 14, 47, 38, 38, 38, 9, + 34, 39, 43, 17, 39, 59, 5, 27, + 0, 12, 27, +}; + +static const uint8_t table1_mvy[1099] = { + 32, 32, 31, 31, 32, 33, 31, 33, + 33, 32, 32, 30, 34, 31, 32, 29, + 33, 30, 32, 33, 31, 35, 34, 30, + 34, 31, 33, 29, 29, 31, 33, 35, + 30, 30, 35, 32, 32, 34, 34, 28, + 25, 32, 36, 27, 32, 32, 32, 37, + 39, 3, 32, 30, 31, 26, 31, 32, + 32, 38, 29, 29, 32, 34, 31, 31, + 34, 35, 33, 33, 28, 33, 1, 33, + 27, 29, 30, 31, 28, 29, 37, 35, + 31, 33, 35, 27, 36, 37, 25, 25, + 61, 35, 4, 5, 32, 33, 36, 30, + 23, 30, 28, 34, 31, 32, 32, 39, + 32, 34, 21, 39, 32, 59, 32, 28, + 32, 36, 60, 33, 24, 36, 32, 32, + 41, 2, 32, 38, 26, 22, 33, 30, + 31, 32, 32, 30, 31, 32, 29, 3, + 40, 38, 32, 32, 33, 26, 31, 34, + 28, 38, 34, 31, 3, 31, 35, 38, + 27, 35, 33, 28, 29, 27, 29, 27, + 43, 29, 37, 63, 31, 33, 34, 30, + 31, 30, 37, 30, 35, 35, 26, 41, + 37, 31, 33, 28, 26, 30, 42, 24, + 7, 27, 33, 29, 36, 28, 34, 57, + 23, 41, 36, 23, 35, 34, 25, 30, + 25, 33, 25, 25, 29, 24, 33, 39, + 33, 33, 0, 37, 31, 36, 21, 32, + 61, 24, 35, 61, 31, 5, 31, 59, + 39, 21, 32, 30, 34, 22, 40, 32, + 29, 16, 31, 5, 62, 2, 20, 39, + 39, 32, 33, 1, 31, 24, 36, 32, + 36, 32, 28, 26, 6, 31, 38, 34, + 58, 35, 32, 33, 33, 17, 43, 26, + 31, 40, 31, 34, 32, 32, 31, 19, + 30, 32, 29, 33, 38, 38, 32, 59, + 40, 18, 38, 32, 35, 34, 32, 17, + 1, 15, 30, 28, 31, 28, 34, 29, + 32, 27, 35, 27, 49, 22, 37, 34, + 37, 26, 32, 32, 22, 28, 45, 29, + 30, 31, 43, 46, 41, 30, 26, 13, + 34, 32, 27, 38, 42, 42, 33, 47, + 33, 60, 27, 42, 25, 32, 22, 32, + 48, 32, 45, 33, 33, 41, 27, 25, + 19, 31, 35, 19, 36, 42, 27, 17, + 31, 44, 28, 33, 33, 31, 23, 31, + 40, 33, 31, 34, 30, 32, 33, 36, + 35, 47, 37, 41, 31, 23, 41, 29, + 30, 35, 32, 25, 32, 28, 58, 2, + 37, 33, 14, 33, 49, 20, 39, 36, + 21, 9, 23, 33, 35, 24, 39, 37, + 11, 33, 30, 31, 31, 28, 51, 40, + 35, 29, 25, 33, 46, 35, 37, 30, + 30, 8, 63, 28, 15, 40, 33, 45, + 49, 25, 32, 4, 47, 51, 36, 39, + 53, 10, 24, 29, 30, 31, 25, 40, + 38, 38, 33, 56, 23, 27, 32, 37, + 26, 29, 43, 36, 33, 24, 55, 43, + 9, 29, 34, 34, 24, 33, 18, 33, + 33, 30, 31, 50, 24, 60, 30, 39, + 34, 30, 39, 28, 22, 38, 2, 26, + 63, 32, 57, 21, 39, 33, 28, 18, + 30, 34, 22, 33, 29, 41, 30, 34, + 35, 21, 13, 34, 35, 39, 30, 46, + 32, 42, 32, 31, 33, 26, 11, 33, + 22, 31, 25, 31, 53, 27, 43, 25, + 40, 50, 21, 36, 38, 30, 12, 31, + 34, 20, 15, 29, 32, 62, 30, 13, + 17, 32, 19, 31, 20, 31, 30, 7, + 1, 17, 34, 37, 31, 31, 44, 34, + 26, 40, 16, 37, 52, 48, 30, 20, + 18, 33, 38, 29, 7, 25, 30, 54, + 45, 47, 46, 41, 29, 29, 16, 30, + 14, 26, 38, 34, 34, 29, 34, 30, + 29, 30, 57, 30, 4, 46, 33, 29, + 39, 44, 30, 31, 50, 33, 31, 32, + 19, 32, 40, 31, 37, 47, 1, 35, + 16, 31, 0, 35, 33, 1, 17, 34, + 9, 34, 33, 31, 49, 43, 42, 51, + 34, 29, 23, 29, 14, 30, 45, 49, + 11, 24, 31, 28, 35, 41, 30, 44, + 18, 29, 34, 35, 36, 25, 26, 21, + 31, 30, 34, 19, 34, 44, 36, 38, + 25, 31, 28, 23, 37, 3, 55, 41, + 30, 22, 41, 24, 33, 26, 35, 35, + 30, 55, 51, 47, 48, 38, 24, 15, + 21, 50, 25, 46, 30, 29, 10, 34, + 42, 45, 29, 42, 22, 3, 33, 27, + 34, 1, 34, 28, 34, 36, 35, 23, + 23, 13, 58, 3, 26, 63, 25, 31, + 34, 61, 38, 39, 25, 61, 29, 37, + 30, 41, 26, 48, 28, 33, 50, 35, + 30, 37, 29, 29, 40, 6, 39, 28, + 28, 19, 8, 22, 45, 34, 35, 10, + 58, 17, 37, 39, 30, 18, 54, 14, + 29, 16, 59, 30, 35, 23, 35, 30, + 47, 36, 29, 55, 20, 12, 31, 35, + 14, 29, 18, 34, 34, 24, 29, 26, + 22, 2, 27, 23, 8, 30, 55, 38, + 60, 31, 4, 34, 49, 34, 27, 34, + 33, 30, 31, 54, 42, 35, 38, 46, + 44, 26, 27, 9, 39, 25, 21, 29, + 28, 42, 13, 0, 5, 34, 37, 28, + 24, 29, 63, 26, 22, 27, 29, 25, + 33, 25, 61, 0, 35, 25, 36, 15, + 27, 40, 53, 33, 3, 10, 16, 37, + 38, 18, 30, 46, 27, 9, 6, 29, + 62, 8, 42, 28, 29, 3, 25, 16, + 26, 29, 35, 28, 27, 51, 61, 48, + 37, 9, 34, 7, 49, 45, 20, 29, + 21, 5, 5, 29, 28, 34, 29, 24, + 10, 24, 35, 36, 38, 55, 11, 36, + 38, 53, 54, 26, 30, 49, 20, 27, + 30, 39, 33, 41, 49, 22, 38, 38, + 4, 30, 8, 9, 3, 24, 22, 50, + 37, 36, 31, 27, 2, 9, 42, 63, + 25, 19, 44, 1, 28, 28, 48, 30, + 34, 41, 41, 38, 12, 27, 15, 0, + 16, 34, 35, 38, 28, 29, 40, 42, + 51, 52, 45, 54, 59, 59, 42, 44, + 37, 26, 46, 24, 15, 39, 22, 46, + 19, 35, 38, 17, 37, 23, 52, 55, + 50, 37, 26, 11, 37, 12, 24, 30, + 16, 13, 22, 13, 36, 35, 40, 41, + 34, 41, 26, 53, 51, 5, 21, 30, + 2, 63, 41, 20, 1, 56, 21, 24, + 25, 5, 28, 35, 26, 28, 30, 18, + 29, 23, 40, 34, 20, 42, 39, 34, + 28, 61, 38, 27, 62, 9, 36, 17, + 9, 49, 24, 25, 54, 34, 39, 37, + 3, 1, 25, 38, 38, 44, 35, 36, + 12, 60, 36, 38, 40, 25, 43, 39, + 53, 28, 39, 57, 46, 10, 52, 27, + 35, 42, 45, 59, 15, 60, 38, 24, + 23, 39, 12, 29, 24, 0, 20, 16, + 28, 43, 35, 28, 1, 49, 4, 21, + 42, 39, 29, 3, 44, 21, 53, 55, + 11, 5, 3, 39, 53, 28, 25, 19, + 34, 28, 21, +}; + +MVTable mv_tables[2] = { + { + 1099, + table0_mv_code, + table0_mv_bits, + table0_mvx, + table0_mvy, + }, + { + 1099, + table1_mv_code, + table1_mv_bits, + table1_mvx, + table1_mvy, + } +}; + +const uint8_t v2_mb_type[8][2] = { + {1, 1}, {0 , 2}, {3 , 3}, {9 , 5}, + {5, 4}, {0x21, 7}, {0x20, 7}, {0x11, 6}, +}; + +const uint8_t v2_intra_cbpc[4][2] = { + {1, 1}, {0, 3}, {1, 3}, {1, 2}, +}; + +const uint8_t wmv1_y_dc_scale_table[32]={ +// 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 + 0, 8, 8, 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 +}; +const uint8_t wmv1_c_dc_scale_table[32]={ +// 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 + 0, 8, 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,22 +}; + +const uint8_t old_ff_y_dc_scale_table[32]={ +// 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 + 0, 8, 8, 8, 8,10,12,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39 +}; + +const uint8_t wmv1_scantable[WMV1_SCANTABLE_COUNT][64]={ + { + 0x00, 0x08, 0x01, 0x02, 0x09, 0x10, 0x18, 0x11, + 0x0A, 0x03, 0x04, 0x0B, 0x12, 0x19, 0x20, 0x28, + 0x30, 0x38, 0x29, 0x21, 0x1A, 0x13, 0x0C, 0x05, + 0x06, 0x0D, 0x14, 0x1B, 0x22, 0x31, 0x39, 0x3A, + 0x32, 0x2A, 0x23, 0x1C, 0x15, 0x0E, 0x07, 0x0F, + 0x16, 0x1D, 0x24, 0x2B, 0x33, 0x3B, 0x3C, 0x34, + 0x2C, 0x25, 0x1E, 0x17, 0x1F, 0x26, 0x2D, 0x35, + 0x3D, 0x3E, 0x36, 0x2E, 0x27, 0x2F, 0x37, 0x3F, + }, + { + 0x00, 0x08, 0x01, 0x02, 0x09, 0x10, 0x18, 0x11, + 0x0A, 0x03, 0x04, 0x0B, 0x12, 0x19, 0x20, 0x28, + 0x21, 0x30, 0x1A, 0x13, 0x0C, 0x05, 0x06, 0x0D, + 0x14, 0x1B, 0x22, 0x29, 0x38, 0x31, 0x39, 0x2A, + 0x23, 0x1C, 0x15, 0x0E, 0x07, 0x0F, 0x16, 0x1D, + 0x24, 0x2B, 0x32, 0x3A, 0x33, 0x3B, 0x2C, 0x25, + 0x1E, 0x17, 0x1F, 0x26, 0x2D, 0x34, 0x3C, 0x35, + 0x3D, 0x2E, 0x27, 0x2F, 0x36, 0x3E, 0x37, 0x3F, + }, + { + 0x00, 0x01, 0x08, 0x02, 0x03, 0x09, 0x10, 0x18, + 0x11, 0x0A, 0x04, 0x05, 0x0B, 0x12, 0x19, 0x20, + 0x28, 0x30, 0x21, 0x1A, 0x13, 0x0C, 0x06, 0x07, + 0x0D, 0x14, 0x1B, 0x22, 0x29, 0x38, 0x31, 0x39, + 0x2A, 0x23, 0x1C, 0x15, 0x0E, 0x0F, 0x16, 0x1D, + 0x24, 0x2B, 0x32, 0x3A, 0x33, 0x2C, 0x25, 0x1E, + 0x17, 0x1F, 0x26, 0x2D, 0x34, 0x3B, 0x3C, 0x35, + 0x2E, 0x27, 0x2F, 0x36, 0x3D, 0x3E, 0x37, 0x3F, + }, + { + 0x00, 0x08, 0x10, 0x01, 0x18, 0x20, 0x28, 0x09, + 0x02, 0x03, 0x0A, 0x11, 0x19, 0x30, 0x38, 0x29, + 0x21, 0x1A, 0x12, 0x0B, 0x04, 0x05, 0x0C, 0x13, + 0x1B, 0x22, 0x31, 0x39, 0x32, 0x2A, 0x23, 0x1C, + 0x14, 0x0D, 0x06, 0x07, 0x0E, 0x15, 0x1D, 0x24, + 0x2B, 0x33, 0x3A, 0x3B, 0x34, 0x2C, 0x25, 0x1E, + 0x16, 0x0F, 0x17, 0x1F, 0x26, 0x2D, 0x3C, 0x35, + 0x2E, 0x27, 0x2F, 0x36, 0x3D, 0x3E, 0x37, 0x3F, + } +}; + +const uint8_t table_inter_intra[4][2]={ + {0,1} /*Luma-Left Chroma-Left*/, + {2,2} /*Luma-Top Chroma-Left*/, + {6,3} /*luma-Left Chroma-Top */, + {7,3} /*luma-Top Chroma-Top */ +}; + +static const uint32_t table_mb_non_intra2[128][2] = { +{0x0000A7, 14}, {0x01B2B8, 18}, {0x01B28E, 18}, {0x036575, 19}, +{0x006CAC, 16}, {0x000A69, 18}, {0x002934, 20}, {0x00526B, 21}, +{0x006CA1, 16}, {0x01B2B9, 18}, {0x0029AD, 20}, {0x029353, 24}, +{0x006CA7, 16}, {0x006CAB, 16}, {0x01B2BB, 18}, {0x00029B, 16}, +{0x00D944, 17}, {0x000A6A, 18}, {0x0149A8, 23}, {0x03651F, 19}, +{0x006CAF, 16}, {0x000A4C, 18}, {0x03651E, 19}, {0x000A48, 18}, +{0x00299C, 20}, {0x00299F, 20}, {0x029352, 24}, {0x0029AC, 20}, +{0x000296, 16}, {0x00D946, 17}, {0x000A68, 18}, {0x000298, 16}, +{0x000527, 17}, {0x00D94D, 17}, {0x0014D7, 19}, {0x036574, 19}, +{0x000A5C, 18}, {0x01B299, 18}, {0x00299D, 20}, {0x00299E, 20}, +{0x000525, 17}, {0x000A66, 18}, {0x00A4D5, 22}, {0x00149B, 19}, +{0x000295, 16}, {0x006CAD, 16}, {0x000A49, 18}, {0x000521, 17}, +{0x006CAA, 16}, {0x00D945, 17}, {0x01B298, 18}, {0x00052F, 17}, +{0x003654, 15}, {0x006CA0, 16}, {0x000532, 17}, {0x000291, 16}, +{0x003652, 15}, {0x000520, 17}, {0x000A5D, 18}, {0x000294, 16}, +{0x00009B, 11}, {0x0006E2, 12}, {0x000028, 12}, {0x0001B0, 10}, +{0x000001, 3}, {0x000010, 8}, {0x00002F, 6}, {0x00004C, 10}, +{0x00000D, 4}, {0x000000, 10}, {0x000006, 9}, {0x000134, 12}, +{0x00000C, 4}, {0x000007, 10}, {0x000007, 9}, {0x0006E1, 12}, +{0x00000E, 5}, {0x0000DA, 9}, {0x000022, 9}, {0x000364, 11}, +{0x00000F, 4}, {0x000006, 10}, {0x00000F, 9}, {0x000135, 12}, +{0x000014, 5}, {0x0000DD, 9}, {0x000004, 9}, {0x000015, 11}, +{0x00001A, 6}, {0x0001B3, 10}, {0x000005, 10}, {0x0006E3, 12}, +{0x00000C, 5}, {0x0000B9, 8}, {0x000004, 8}, {0x0000DB, 9}, +{0x00000E, 4}, {0x00000B, 10}, {0x000023, 9}, {0x0006CB, 12}, +{0x000005, 6}, {0x0001B1, 10}, {0x000001, 10}, {0x0006E0, 12}, +{0x000011, 5}, {0x0000DF, 9}, {0x00000E, 9}, {0x000373, 11}, +{0x000003, 5}, {0x0000B8, 8}, {0x000006, 8}, {0x000175, 9}, +{0x000015, 5}, {0x000174, 9}, {0x000027, 9}, {0x000372, 11}, +{0x000010, 5}, {0x0000BB, 8}, {0x000005, 8}, {0x0000DE, 9}, +{0x00000F, 5}, {0x000001, 9}, {0x000012, 8}, {0x000004, 10}, +{0x000002, 3}, {0x000016, 5}, {0x000009, 4}, {0x000001, 5}, +}; + +static const uint32_t table_mb_non_intra3[128][2] = { +{0x0002A1, 10}, {0x005740, 15}, {0x01A0BF, 18}, {0x015D19, 17}, +{0x001514, 13}, {0x00461E, 15}, {0x015176, 17}, {0x015177, 17}, +{0x0011AD, 13}, {0x00682E, 16}, {0x0682F9, 20}, {0x03417D, 19}, +{0x001A36, 14}, {0x002A2D, 14}, {0x00D05E, 17}, {0x006824, 16}, +{0x001515, 13}, {0x00545C, 15}, {0x0230E9, 18}, {0x011AFA, 17}, +{0x0015D7, 13}, {0x005747, 15}, {0x008D79, 16}, {0x006825, 16}, +{0x002BA2, 14}, {0x00A8BA, 16}, {0x0235F6, 18}, {0x015D18, 17}, +{0x0011AE, 13}, {0x00346F, 15}, {0x008C3B, 16}, {0x00346E, 15}, +{0x000D1A, 13}, {0x00461F, 15}, {0x0682F8, 20}, {0x011875, 17}, +{0x002BA1, 14}, {0x008D61, 16}, {0x0235F7, 18}, {0x0230E8, 18}, +{0x001513, 13}, {0x008D7B, 16}, {0x011AF4, 17}, {0x011AF5, 17}, +{0x001185, 13}, {0x0046BF, 15}, {0x008D60, 16}, {0x008D7C, 16}, +{0x001512, 13}, {0x00461C, 15}, {0x00AE8D, 16}, {0x008D78, 16}, +{0x000D0E, 13}, {0x003413, 15}, {0x0046B1, 15}, {0x003416, 15}, +{0x000AEA, 12}, {0x002A2C, 14}, {0x005741, 15}, {0x002A2F, 14}, +{0x000158, 9}, {0x0008D2, 12}, {0x00054C, 11}, {0x000686, 12}, +{0x000000, 2}, {0x000069, 8}, {0x00006B, 8}, {0x00068C, 12}, +{0x000007, 3}, {0x00015E, 9}, {0x0002A3, 10}, {0x000AE9, 12}, +{0x000006, 3}, {0x000231, 10}, {0x0002B8, 10}, {0x001A08, 14}, +{0x000010, 5}, {0x0001A9, 10}, {0x000342, 11}, {0x000A88, 12}, +{0x000004, 4}, {0x0001A2, 10}, {0x0002A4, 10}, {0x001184, 13}, +{0x000012, 5}, {0x000232, 10}, {0x0002B2, 10}, {0x000680, 12}, +{0x00001B, 6}, {0x00046A, 11}, {0x00068E, 12}, {0x002359, 14}, +{0x000016, 5}, {0x00015F, 9}, {0x0002A0, 10}, {0x00054D, 11}, +{0x000005, 4}, {0x000233, 10}, {0x0002B9, 10}, {0x0015D6, 13}, +{0x000022, 6}, {0x000468, 11}, {0x000683, 12}, {0x001A0A, 14}, +{0x000013, 5}, {0x000236, 10}, {0x0002BB, 10}, {0x001186, 13}, +{0x000017, 5}, {0x0001AB, 10}, {0x0002A7, 10}, {0x0008D3, 12}, +{0x000014, 5}, {0x000237, 10}, {0x000460, 11}, {0x000D0F, 13}, +{0x000019, 6}, {0x0001AA, 10}, {0x0002B3, 10}, {0x000681, 12}, +{0x000018, 6}, {0x0001A8, 10}, {0x0002A5, 10}, {0x00068F, 12}, +{0x000007, 4}, {0x000055, 7}, {0x000047, 7}, {0x0000AD, 8}, +}; + +static const uint32_t table_mb_non_intra4[128][2] = { +{0x0000D4, 8}, {0x0021C5, 14}, {0x00F18A, 16}, {0x00D5BC, 16}, +{0x000879, 12}, {0x00354D, 14}, {0x010E3F, 17}, {0x010F54, 17}, +{0x000866, 12}, {0x00356E, 14}, {0x010F55, 17}, {0x010E3E, 17}, +{0x0010CE, 13}, {0x003C84, 14}, {0x00D5BD, 16}, {0x00F18B, 16}, +{0x000868, 12}, {0x00438C, 15}, {0x0087AB, 16}, {0x00790B, 15}, +{0x000F10, 12}, {0x00433D, 15}, {0x006AD3, 15}, {0x00790A, 15}, +{0x001AA7, 13}, {0x0043D4, 15}, {0x00871E, 16}, {0x006ADF, 15}, +{0x000D7C, 12}, {0x003C94, 14}, {0x00438D, 15}, {0x006AD2, 15}, +{0x0006BC, 11}, {0x0021E9, 14}, {0x006ADA, 15}, {0x006A99, 15}, +{0x0010F7, 13}, {0x004389, 15}, {0x006ADB, 15}, {0x0078C4, 15}, +{0x000D56, 12}, {0x0035F7, 14}, {0x00438E, 15}, {0x006A98, 15}, +{0x000D52, 12}, {0x003C95, 14}, {0x004388, 15}, {0x00433C, 15}, +{0x000D54, 12}, {0x001E4B, 13}, {0x003C63, 14}, {0x003C83, 14}, +{0x000861, 12}, {0x0021EB, 14}, {0x00356C, 14}, {0x0035F6, 14}, +{0x000863, 12}, {0x00219F, 14}, {0x003568, 14}, {0x003C82, 14}, +{0x0001AE, 9}, {0x0010C0, 13}, {0x000F11, 12}, {0x001AFA, 13}, +{0x000000, 1}, {0x0000F0, 8}, {0x0001AD, 9}, {0x0010C1, 13}, +{0x00000A, 4}, {0x0003C5, 10}, {0x000789, 11}, {0x001AB5, 13}, +{0x000009, 4}, {0x000435, 11}, {0x000793, 11}, {0x001E40, 13}, +{0x00001D, 5}, {0x0003CB, 10}, {0x000878, 12}, {0x001AAF, 13}, +{0x00000B, 4}, {0x0003C7, 10}, {0x000791, 11}, {0x001AAB, 13}, +{0x00001F, 5}, {0x000436, 11}, {0x0006BF, 11}, {0x000F19, 12}, +{0x00003D, 6}, {0x000D51, 12}, {0x0010C4, 13}, {0x0021E8, 14}, +{0x000036, 6}, {0x000437, 11}, {0x0006AF, 11}, {0x0010C5, 13}, +{0x00000C, 4}, {0x000432, 11}, {0x000794, 11}, {0x001E30, 13}, +{0x000042, 7}, {0x000870, 12}, {0x000F24, 12}, {0x001E43, 13}, +{0x000020, 6}, {0x00043E, 11}, {0x000795, 11}, {0x001AAA, 13}, +{0x000037, 6}, {0x0006AC, 11}, {0x0006AE, 11}, {0x0010F6, 13}, +{0x000034, 6}, {0x00043A, 11}, {0x000D50, 12}, {0x001AAE, 13}, +{0x000039, 6}, {0x00043F, 11}, {0x00078D, 11}, {0x0010D2, 13}, +{0x000038, 6}, {0x00043B, 11}, {0x0006BD, 11}, {0x0010D3, 13}, +{0x000011, 5}, {0x0001AC, 9}, {0x0000F3, 8}, {0x000439, 11}, +}; + +const uint32_t (*wmv2_inter_table[WMV2_INTER_CBP_TABLE_COUNT])[2]={ + table_mb_non_intra2, + table_mb_non_intra3, + table_mb_non_intra4, + table_mb_non_intra, +}; + +const uint8_t wmv2_scantableA[64]={ +0x00, 0x01, 0x02, 0x08, 0x03, 0x09, 0x0A, 0x10, +0x04, 0x0B, 0x11, 0x18, 0x12, 0x0C, 0x05, 0x13, +0x19, 0x0D, 0x14, 0x1A, 0x1B, 0x06, 0x15, 0x1C, +0x0E, 0x16, 0x1D, 0x07, 0x1E, 0x0F, 0x17, 0x1F, +}; + +const uint8_t wmv2_scantableB[64]={ +0x00, 0x08, 0x01, 0x10, 0x09, 0x18, 0x11, 0x02, +0x20, 0x0A, 0x19, 0x28, 0x12, 0x30, 0x21, 0x1A, +0x38, 0x29, 0x22, 0x03, 0x31, 0x39, 0x0B, 0x2A, +0x13, 0x32, 0x1B, 0x3A, 0x23, 0x2B, 0x33, 0x3B, +}; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/msmpeg4data.h b/src/add-ons/media/plugins/avcodec/libavcodec/msmpeg4data.h index 69568cbf63..0a8ecd437a 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/msmpeg4data.h +++ b/src/add-ons/media/plugins/avcodec/libavcodec/msmpeg4data.h @@ -1,1766 +1,38 @@ -/** - * @file msmpeg4data.h - * MSMPEG4 data tables. +/* + * MSMPEG4 backend for ffmpeg encoder and decoder + * copyright (c) 2001 Fabrice Bellard + * copyright (c) 2002-2004 Michael Niedermayer + * + * msmpeg4v1 & v2 stuff by Michael Niedermayer + * + * This file is part of FFmpeg. + * + * FFmpeg 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.1 of the License, or (at your option) any later version. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -/* intra picture macro block coded block pattern */ -static const uint16_t table_mb_intra[64][2] = { -{ 0x1, 1 },{ 0x17, 6 },{ 0x9, 5 },{ 0x5, 5 }, -{ 0x6, 5 },{ 0x47, 9 },{ 0x20, 7 },{ 0x10, 7 }, -{ 0x2, 5 },{ 0x7c, 9 },{ 0x3a, 7 },{ 0x1d, 7 }, -{ 0x2, 6 },{ 0xec, 9 },{ 0x77, 8 },{ 0x0, 8 }, -{ 0x3, 5 },{ 0xb7, 9 },{ 0x2c, 7 },{ 0x13, 7 }, -{ 0x1, 6 },{ 0x168, 10 },{ 0x46, 8 },{ 0x3f, 8 }, -{ 0x1e, 6 },{ 0x712, 13 },{ 0xb5, 9 },{ 0x42, 8 }, -{ 0x22, 7 },{ 0x1c5, 11 },{ 0x11e, 10 },{ 0x87, 9 }, -{ 0x6, 4 },{ 0x3, 9 },{ 0x1e, 7 },{ 0x1c, 6 }, -{ 0x12, 7 },{ 0x388, 12 },{ 0x44, 9 },{ 0x70, 9 }, -{ 0x1f, 6 },{ 0x23e, 11 },{ 0x39, 8 },{ 0x8e, 9 }, -{ 0x1, 7 },{ 0x1c6, 11 },{ 0xb6, 9 },{ 0x45, 9 }, -{ 0x14, 6 },{ 0x23f, 11 },{ 0x7d, 9 },{ 0x18, 9 }, -{ 0x7, 7 },{ 0x1c7, 11 },{ 0x86, 9 },{ 0x19, 9 }, -{ 0x15, 6 },{ 0x1db, 10 },{ 0x2, 9 },{ 0x46, 9 }, -{ 0xd, 8 },{ 0x713, 13 },{ 0x1da, 10 },{ 0x169, 10 }, -}; +/** + * @file msmpeg4data.h + * MSMPEG4 data tables. + */ -/* non intra picture macro block coded block pattern + mb type */ -static const uint32_t table_mb_non_intra[128][2] = { -{ 0x40, 7 },{ 0x13c9, 13 },{ 0x9fd, 12 },{ 0x1fc, 15 }, -{ 0x9fc, 12 },{ 0xa83, 18 },{ 0x12d34, 17 },{ 0x83bc, 16 }, -{ 0x83a, 12 },{ 0x7f8, 17 },{ 0x3fd, 16 },{ 0x3ff, 16 }, -{ 0x79, 13 },{ 0xa82, 18 },{ 0x969d, 16 },{ 0x2a4, 16 }, -{ 0x978, 12 },{ 0x543, 17 },{ 0x41df, 15 },{ 0x7f9, 17 }, -{ 0x12f3, 13 },{ 0x25a6b, 18 },{ 0x25ef9, 18 },{ 0x3fa, 16 }, -{ 0x20ee, 14 },{ 0x969ab, 20 },{ 0x969c, 16 },{ 0x25ef8, 18 }, -{ 0x12d2, 13 },{ 0xa85, 18 },{ 0x969e, 16 },{ 0x4bc8, 15 }, -{ 0x3d, 12 },{ 0x12f7f, 17 },{ 0x2a2, 16 },{ 0x969f, 16 }, -{ 0x25ee, 14 },{ 0x12d355, 21 },{ 0x12f7d, 17 },{ 0x12f7e, 17 }, -{ 0x9e5, 12 },{ 0xa81, 18 },{ 0x4b4d4, 19 },{ 0x83bd, 16 }, -{ 0x78, 13 },{ 0x969b, 16 },{ 0x3fe, 16 },{ 0x2a5, 16 }, -{ 0x7e, 13 },{ 0xa80, 18 },{ 0x2a3, 16 },{ 0x3fb, 16 }, -{ 0x1076, 13 },{ 0xa84, 18 },{ 0x153, 15 },{ 0x4bc9, 15 }, -{ 0x55, 13 },{ 0x12d354, 21 },{ 0x4bde, 15 },{ 0x25e5, 14 }, -{ 0x25b, 10 },{ 0x4b4c, 15 },{ 0x96b, 12 },{ 0x96a, 12 }, -{ 0x1, 2 },{ 0x0, 7 },{ 0x26, 6 },{ 0x12b, 9 }, -{ 0x7, 3 },{ 0x20f, 10 },{ 0x4, 9 },{ 0x28, 12 }, -{ 0x6, 3 },{ 0x20a, 10 },{ 0x128, 9 },{ 0x2b, 12 }, -{ 0x11, 5 },{ 0x1b, 11 },{ 0x13a, 9 },{ 0x4ff, 11 }, -{ 0x3, 4 },{ 0x277, 10 },{ 0x106, 9 },{ 0x839, 12 }, -{ 0xb, 4 },{ 0x27b, 10 },{ 0x12c, 9 },{ 0x4bf, 11 }, -{ 0x9, 6 },{ 0x35, 12 },{ 0x27e, 10 },{ 0x13c8, 13 }, -{ 0x1, 6 },{ 0x4aa, 11 },{ 0x208, 10 },{ 0x29, 12 }, -{ 0x1, 4 },{ 0x254, 10 },{ 0x12e, 9 },{ 0x838, 12 }, -{ 0x24, 6 },{ 0x4f3, 11 },{ 0x276, 10 },{ 0x12f6, 13 }, -{ 0x1, 5 },{ 0x27a, 10 },{ 0x13e, 9 },{ 0x3e, 12 }, -{ 0x8, 6 },{ 0x413, 11 },{ 0xc, 10 },{ 0x4be, 11 }, -{ 0x14, 5 },{ 0x412, 11 },{ 0x253, 10 },{ 0x97a, 12 }, -{ 0x21, 6 },{ 0x4ab, 11 },{ 0x20b, 10 },{ 0x34, 12 }, -{ 0x15, 5 },{ 0x278, 10 },{ 0x252, 10 },{ 0x968, 12 }, -{ 0x5, 5 },{ 0xb, 10 },{ 0x9c, 8 },{ 0xe, 10 }, -}; +#ifndef FFMPEG_MSMPEG4DATA_H +#define FFMPEG_MSMPEG4DATA_H -/* dc table 0 */ - -static const uint32_t table0_dc_lum[120][2] = { -{ 0x1, 1 },{ 0x1, 2 },{ 0x1, 4 },{ 0x1, 5 }, -{ 0x5, 5 },{ 0x7, 5 },{ 0x8, 6 },{ 0xc, 6 }, -{ 0x0, 7 },{ 0x2, 7 },{ 0x12, 7 },{ 0x1a, 7 }, -{ 0x3, 8 },{ 0x7, 8 },{ 0x27, 8 },{ 0x37, 8 }, -{ 0x5, 9 },{ 0x4c, 9 },{ 0x6c, 9 },{ 0x6d, 9 }, -{ 0x8, 10 },{ 0x19, 10 },{ 0x9b, 10 },{ 0x1b, 10 }, -{ 0x9a, 10 },{ 0x13, 11 },{ 0x34, 11 },{ 0x35, 11 }, -{ 0x61, 12 },{ 0x48, 13 },{ 0xc4, 13 },{ 0x4a, 13 }, -{ 0xc6, 13 },{ 0xc7, 13 },{ 0x92, 14 },{ 0x18b, 14 }, -{ 0x93, 14 },{ 0x183, 14 },{ 0x182, 14 },{ 0x96, 14 }, -{ 0x97, 14 },{ 0x180, 14 },{ 0x314, 15 },{ 0x315, 15 }, -{ 0x605, 16 },{ 0x604, 16 },{ 0x606, 16 },{ 0xc0e, 17 }, -{ 0x303cd, 23 },{ 0x303c9, 23 },{ 0x303c8, 23 },{ 0x303ca, 23 }, -{ 0x303cb, 23 },{ 0x303cc, 23 },{ 0x303ce, 23 },{ 0x303cf, 23 }, -{ 0x303d0, 23 },{ 0x303d1, 23 },{ 0x303d2, 23 },{ 0x303d3, 23 }, -{ 0x303d4, 23 },{ 0x303d5, 23 },{ 0x303d6, 23 },{ 0x303d7, 23 }, -{ 0x303d8, 23 },{ 0x303d9, 23 },{ 0x303da, 23 },{ 0x303db, 23 }, -{ 0x303dc, 23 },{ 0x303dd, 23 },{ 0x303de, 23 },{ 0x303df, 23 }, -{ 0x303e0, 23 },{ 0x303e1, 23 },{ 0x303e2, 23 },{ 0x303e3, 23 }, -{ 0x303e4, 23 },{ 0x303e5, 23 },{ 0x303e6, 23 },{ 0x303e7, 23 }, -{ 0x303e8, 23 },{ 0x303e9, 23 },{ 0x303ea, 23 },{ 0x303eb, 23 }, -{ 0x303ec, 23 },{ 0x303ed, 23 },{ 0x303ee, 23 },{ 0x303ef, 23 }, -{ 0x303f0, 23 },{ 0x303f1, 23 },{ 0x303f2, 23 },{ 0x303f3, 23 }, -{ 0x303f4, 23 },{ 0x303f5, 23 },{ 0x303f6, 23 },{ 0x303f7, 23 }, -{ 0x303f8, 23 },{ 0x303f9, 23 },{ 0x303fa, 23 },{ 0x303fb, 23 }, -{ 0x303fc, 23 },{ 0x303fd, 23 },{ 0x303fe, 23 },{ 0x303ff, 23 }, -{ 0x60780, 24 },{ 0x60781, 24 },{ 0x60782, 24 },{ 0x60783, 24 }, -{ 0x60784, 24 },{ 0x60785, 24 },{ 0x60786, 24 },{ 0x60787, 24 }, -{ 0x60788, 24 },{ 0x60789, 24 },{ 0x6078a, 24 },{ 0x6078b, 24 }, -{ 0x6078c, 24 },{ 0x6078d, 24 },{ 0x6078e, 24 },{ 0x6078f, 24 }, -}; - -static const uint32_t table0_dc_chroma[120][2] = { -{ 0x0, 2 },{ 0x1, 2 },{ 0x5, 3 },{ 0x9, 4 }, -{ 0xd, 4 },{ 0x11, 5 },{ 0x1d, 5 },{ 0x1f, 5 }, -{ 0x21, 6 },{ 0x31, 6 },{ 0x38, 6 },{ 0x33, 6 }, -{ 0x39, 6 },{ 0x3d, 6 },{ 0x61, 7 },{ 0x79, 7 }, -{ 0x80, 8 },{ 0xc8, 8 },{ 0xca, 8 },{ 0xf0, 8 }, -{ 0x81, 8 },{ 0xc0, 8 },{ 0xc9, 8 },{ 0x107, 9 }, -{ 0x106, 9 },{ 0x196, 9 },{ 0x183, 9 },{ 0x1e3, 9 }, -{ 0x1e2, 9 },{ 0x20a, 10 },{ 0x20b, 10 },{ 0x609, 11 }, -{ 0x412, 11 },{ 0x413, 11 },{ 0x60b, 11 },{ 0x411, 11 }, -{ 0x60a, 11 },{ 0x65f, 11 },{ 0x410, 11 },{ 0x65d, 11 }, -{ 0x65e, 11 },{ 0xcb8, 12 },{ 0xc10, 12 },{ 0xcb9, 12 }, -{ 0x1823, 13 },{ 0x3045, 14 },{ 0x6089, 15 },{ 0xc110, 16 }, -{ 0x304448, 22 },{ 0x304449, 22 },{ 0x30444a, 22 },{ 0x30444b, 22 }, -{ 0x30444c, 22 },{ 0x30444d, 22 },{ 0x30444e, 22 },{ 0x30444f, 22 }, -{ 0x304450, 22 },{ 0x304451, 22 },{ 0x304452, 22 },{ 0x304453, 22 }, -{ 0x304454, 22 },{ 0x304455, 22 },{ 0x304456, 22 },{ 0x304457, 22 }, -{ 0x304458, 22 },{ 0x304459, 22 },{ 0x30445a, 22 },{ 0x30445b, 22 }, -{ 0x30445c, 22 },{ 0x30445d, 22 },{ 0x30445e, 22 },{ 0x30445f, 22 }, -{ 0x304460, 22 },{ 0x304461, 22 },{ 0x304462, 22 },{ 0x304463, 22 }, -{ 0x304464, 22 },{ 0x304465, 22 },{ 0x304466, 22 },{ 0x304467, 22 }, -{ 0x304468, 22 },{ 0x304469, 22 },{ 0x30446a, 22 },{ 0x30446b, 22 }, -{ 0x30446c, 22 },{ 0x30446d, 22 },{ 0x30446e, 22 },{ 0x30446f, 22 }, -{ 0x304470, 22 },{ 0x304471, 22 },{ 0x304472, 22 },{ 0x304473, 22 }, -{ 0x304474, 22 },{ 0x304475, 22 },{ 0x304476, 22 },{ 0x304477, 22 }, -{ 0x304478, 22 },{ 0x304479, 22 },{ 0x30447a, 22 },{ 0x30447b, 22 }, -{ 0x30447c, 22 },{ 0x30447d, 22 },{ 0x30447e, 22 },{ 0x30447f, 22 }, -{ 0x608880, 23 },{ 0x608881, 23 },{ 0x608882, 23 },{ 0x608883, 23 }, -{ 0x608884, 23 },{ 0x608885, 23 },{ 0x608886, 23 },{ 0x608887, 23 }, -{ 0x608888, 23 },{ 0x608889, 23 },{ 0x60888a, 23 },{ 0x60888b, 23 }, -{ 0x60888c, 23 },{ 0x60888d, 23 },{ 0x60888e, 23 },{ 0x60888f, 23 }, -}; - -/* dc table 1 */ - -static const uint32_t table1_dc_lum[120][2] = { -{ 0x2, 2 },{ 0x3, 2 },{ 0x3, 3 },{ 0x2, 4 }, -{ 0x5, 4 },{ 0x1, 5 },{ 0x3, 5 },{ 0x8, 5 }, -{ 0x0, 6 },{ 0x5, 6 },{ 0xd, 6 },{ 0xf, 6 }, -{ 0x13, 6 },{ 0x8, 7 },{ 0x18, 7 },{ 0x1c, 7 }, -{ 0x24, 7 },{ 0x4, 8 },{ 0x6, 8 },{ 0x12, 8 }, -{ 0x32, 8 },{ 0x3b, 8 },{ 0x4a, 8 },{ 0x4b, 8 }, -{ 0xb, 9 },{ 0x26, 9 },{ 0x27, 9 },{ 0x66, 9 }, -{ 0x74, 9 },{ 0x75, 9 },{ 0x14, 10 },{ 0x1c, 10 }, -{ 0x1f, 10 },{ 0x1d, 10 },{ 0x2b, 11 },{ 0x3d, 11 }, -{ 0x19d, 11 },{ 0x19f, 11 },{ 0x54, 12 },{ 0x339, 12 }, -{ 0x338, 12 },{ 0x33d, 12 },{ 0xab, 13 },{ 0xf1, 13 }, -{ 0x678, 13 },{ 0xf2, 13 },{ 0x1e0, 14 },{ 0x1e1, 14 }, -{ 0x154, 14 },{ 0xcf2, 14 },{ 0x3cc, 15 },{ 0x2ab, 15 }, -{ 0x19e7, 15 },{ 0x3ce, 15 },{ 0x19e6, 15 },{ 0x554, 16 }, -{ 0x79f, 16 },{ 0x555, 16 },{ 0xf3d, 17 },{ 0xf37, 17 }, -{ 0xf3c, 17 },{ 0xf35, 17 },{ 0x1e6d, 18 },{ 0x1e68, 18 }, -{ 0x3cd8, 19 },{ 0x3cd3, 19 },{ 0x3cd9, 19 },{ 0x79a4, 20 }, -{ 0xf34ba, 25 },{ 0xf34b4, 25 },{ 0xf34b5, 25 },{ 0xf34b6, 25 }, -{ 0xf34b7, 25 },{ 0xf34b8, 25 },{ 0xf34b9, 25 },{ 0xf34bb, 25 }, -{ 0xf34bc, 25 },{ 0xf34bd, 25 },{ 0xf34be, 25 },{ 0xf34bf, 25 }, -{ 0x1e6940, 26 },{ 0x1e6941, 26 },{ 0x1e6942, 26 },{ 0x1e6943, 26 }, -{ 0x1e6944, 26 },{ 0x1e6945, 26 },{ 0x1e6946, 26 },{ 0x1e6947, 26 }, -{ 0x1e6948, 26 },{ 0x1e6949, 26 },{ 0x1e694a, 26 },{ 0x1e694b, 26 }, -{ 0x1e694c, 26 },{ 0x1e694d, 26 },{ 0x1e694e, 26 },{ 0x1e694f, 26 }, -{ 0x1e6950, 26 },{ 0x1e6951, 26 },{ 0x1e6952, 26 },{ 0x1e6953, 26 }, -{ 0x1e6954, 26 },{ 0x1e6955, 26 },{ 0x1e6956, 26 },{ 0x1e6957, 26 }, -{ 0x1e6958, 26 },{ 0x1e6959, 26 },{ 0x1e695a, 26 },{ 0x1e695b, 26 }, -{ 0x1e695c, 26 },{ 0x1e695d, 26 },{ 0x1e695e, 26 },{ 0x1e695f, 26 }, -{ 0x1e6960, 26 },{ 0x1e6961, 26 },{ 0x1e6962, 26 },{ 0x1e6963, 26 }, -{ 0x1e6964, 26 },{ 0x1e6965, 26 },{ 0x1e6966, 26 },{ 0x1e6967, 26 }, -}; - -static const uint32_t table1_dc_chroma[120][2] = { -{ 0x0, 2 },{ 0x1, 2 },{ 0x4, 3 },{ 0x7, 3 }, -{ 0xb, 4 },{ 0xd, 4 },{ 0x15, 5 },{ 0x28, 6 }, -{ 0x30, 6 },{ 0x32, 6 },{ 0x52, 7 },{ 0x62, 7 }, -{ 0x66, 7 },{ 0xa6, 8 },{ 0xc6, 8 },{ 0xcf, 8 }, -{ 0x14f, 9 },{ 0x18e, 9 },{ 0x19c, 9 },{ 0x29d, 10 }, -{ 0x33a, 10 },{ 0x538, 11 },{ 0x63c, 11 },{ 0x63e, 11 }, -{ 0x63f, 11 },{ 0x676, 11 },{ 0xa73, 12 },{ 0xc7a, 12 }, -{ 0xcef, 12 },{ 0x14e5, 13 },{ 0x19dd, 13 },{ 0x29c8, 14 }, -{ 0x29c9, 14 },{ 0x63dd, 15 },{ 0x33b8, 14 },{ 0x33b9, 14 }, -{ 0xc7b6, 16 },{ 0x63d8, 15 },{ 0x63df, 15 },{ 0xc7b3, 16 }, -{ 0xc7b4, 16 },{ 0xc7b5, 16 },{ 0x63de, 15 },{ 0xc7b7, 16 }, -{ 0xc7b8, 16 },{ 0xc7b9, 16 },{ 0x18f65, 17 },{ 0x31ec8, 18 }, -{ 0xc7b248, 24 },{ 0xc7b249, 24 },{ 0xc7b24a, 24 },{ 0xc7b24b, 24 }, -{ 0xc7b24c, 24 },{ 0xc7b24d, 24 },{ 0xc7b24e, 24 },{ 0xc7b24f, 24 }, -{ 0xc7b250, 24 },{ 0xc7b251, 24 },{ 0xc7b252, 24 },{ 0xc7b253, 24 }, -{ 0xc7b254, 24 },{ 0xc7b255, 24 },{ 0xc7b256, 24 },{ 0xc7b257, 24 }, -{ 0xc7b258, 24 },{ 0xc7b259, 24 },{ 0xc7b25a, 24 },{ 0xc7b25b, 24 }, -{ 0xc7b25c, 24 },{ 0xc7b25d, 24 },{ 0xc7b25e, 24 },{ 0xc7b25f, 24 }, -{ 0xc7b260, 24 },{ 0xc7b261, 24 },{ 0xc7b262, 24 },{ 0xc7b263, 24 }, -{ 0xc7b264, 24 },{ 0xc7b265, 24 },{ 0xc7b266, 24 },{ 0xc7b267, 24 }, -{ 0xc7b268, 24 },{ 0xc7b269, 24 },{ 0xc7b26a, 24 },{ 0xc7b26b, 24 }, -{ 0xc7b26c, 24 },{ 0xc7b26d, 24 },{ 0xc7b26e, 24 },{ 0xc7b26f, 24 }, -{ 0xc7b270, 24 },{ 0xc7b271, 24 },{ 0xc7b272, 24 },{ 0xc7b273, 24 }, -{ 0xc7b274, 24 },{ 0xc7b275, 24 },{ 0xc7b276, 24 },{ 0xc7b277, 24 }, -{ 0xc7b278, 24 },{ 0xc7b279, 24 },{ 0xc7b27a, 24 },{ 0xc7b27b, 24 }, -{ 0xc7b27c, 24 },{ 0xc7b27d, 24 },{ 0xc7b27e, 24 },{ 0xc7b27f, 24 }, -{ 0x18f6480, 25 },{ 0x18f6481, 25 },{ 0x18f6482, 25 },{ 0x18f6483, 25 }, -{ 0x18f6484, 25 },{ 0x18f6485, 25 },{ 0x18f6486, 25 },{ 0x18f6487, 25 }, -{ 0x18f6488, 25 },{ 0x18f6489, 25 },{ 0x18f648a, 25 },{ 0x18f648b, 25 }, -{ 0x18f648c, 25 },{ 0x18f648d, 25 },{ 0x18f648e, 25 },{ 0x18f648f, 25 }, -}; - -/* vlc table 0, for intra luma */ - -static const uint16_t table0_vlc[133][2] = { -{ 0x1, 2 },{ 0x6, 3 },{ 0xf, 4 },{ 0x16, 5 }, -{ 0x20, 6 },{ 0x18, 7 },{ 0x8, 8 },{ 0x9a, 8 }, -{ 0x56, 9 },{ 0x13e, 9 },{ 0xf0, 10 },{ 0x3a5, 10 }, -{ 0x77, 11 },{ 0x1ef, 11 },{ 0x9a, 12 },{ 0x5d, 13 }, -{ 0x1, 4 },{ 0x11, 5 },{ 0x2, 7 },{ 0xb, 8 }, -{ 0x12, 9 },{ 0x1d6, 9 },{ 0x27e, 10 },{ 0x191, 11 }, -{ 0xea, 12 },{ 0x3dc, 12 },{ 0x13b, 13 },{ 0x4, 5 }, -{ 0x14, 7 },{ 0x9e, 8 },{ 0x9, 10 },{ 0x1ac, 11 }, -{ 0x1e2, 11 },{ 0x3ca, 12 },{ 0x5f, 13 },{ 0x17, 5 }, -{ 0x4e, 7 },{ 0x5e, 9 },{ 0xf3, 10 },{ 0x1ad, 11 }, -{ 0xec, 12 },{ 0x5f0, 13 },{ 0xe, 6 },{ 0xe1, 8 }, -{ 0x3a4, 10 },{ 0x9c, 12 },{ 0x13d, 13 },{ 0x3b, 6 }, -{ 0x1c, 9 },{ 0x14, 11 },{ 0x9be, 12 },{ 0x6, 7 }, -{ 0x7a, 9 },{ 0x190, 11 },{ 0x137, 13 },{ 0x1b, 7 }, -{ 0x8, 10 },{ 0x75c, 11 },{ 0x71, 7 },{ 0xd7, 10 }, -{ 0x9bf, 12 },{ 0x7, 8 },{ 0xaf, 10 },{ 0x4cc, 11 }, -{ 0x34, 8 },{ 0x265, 10 },{ 0x9f, 12 },{ 0xe0, 8 }, -{ 0x16, 11 },{ 0x327, 12 },{ 0x15, 9 },{ 0x17d, 11 }, -{ 0xebb, 12 },{ 0x14, 9 },{ 0xf6, 10 },{ 0x1e4, 11 }, -{ 0xcb, 10 },{ 0x99d, 12 },{ 0xca, 10 },{ 0x2fc, 12 }, -{ 0x17f, 11 },{ 0x4cd, 11 },{ 0x2fd, 12 },{ 0x4fe, 11 }, -{ 0x13a, 13 },{ 0xa, 4 },{ 0x42, 7 },{ 0x1d3, 9 }, -{ 0x4dd, 11 },{ 0x12, 5 },{ 0xe8, 8 },{ 0x4c, 11 }, -{ 0x136, 13 },{ 0x39, 6 },{ 0x264, 10 },{ 0xeba, 12 }, -{ 0x0, 7 },{ 0xae, 10 },{ 0x99c, 12 },{ 0x1f, 7 }, -{ 0x4de, 11 },{ 0x43, 7 },{ 0x4dc, 11 },{ 0x3, 8 }, -{ 0x3cb, 12 },{ 0x6, 8 },{ 0x99e, 12 },{ 0x2a, 8 }, -{ 0x5f1, 13 },{ 0xf, 8 },{ 0x9fe, 12 },{ 0x33, 8 }, -{ 0x9ff, 12 },{ 0x98, 8 },{ 0x99f, 12 },{ 0xea, 8 }, -{ 0x13c, 13 },{ 0x2e, 8 },{ 0x192, 11 },{ 0x136, 9 }, -{ 0x6a, 9 },{ 0x15, 11 },{ 0x3af, 10 },{ 0x1e3, 11 }, -{ 0x74, 11 },{ 0xeb, 12 },{ 0x2f9, 12 },{ 0x5c, 13 }, -{ 0xed, 12 },{ 0x3dd, 12 },{ 0x326, 12 },{ 0x5e, 13 }, -{ 0x16, 7 }, -}; - -static const int8_t table0_level[132] = { - 1, 2, 3, 4, 5, 6, 7, 8, - 9, 10, 11, 12, 13, 14, 15, 16, - 1, 2, 3, 4, 5, 6, 7, 8, - 9, 10, 11, 1, 2, 3, 4, 5, - 6, 7, 8, 1, 2, 3, 4, 5, - 6, 7, 1, 2, 3, 4, 5, 1, - 2, 3, 4, 1, 2, 3, 4, 1, - 2, 3, 1, 2, 3, 1, 2, 3, - 1, 2, 3, 1, 2, 3, 1, 2, - 3, 1, 2, 3, 1, 2, 1, 2, - 1, 1, 1, 1, 1, 1, 2, 3, - 4, 1, 2, 3, 4, 1, 2, 3, - 1, 2, 3, 1, 2, 1, 2, 1, - 2, 1, 2, 1, 2, 1, 2, 1, - 2, 1, 2, 1, 2, 1, 2, 1, - 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, -}; - -static const int8_t table0_run[132] = { - 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, 2, 2, 2, 2, 2, - 2, 2, 2, 3, 3, 3, 3, 3, - 3, 3, 4, 4, 4, 4, 4, 5, - 5, 5, 5, 6, 6, 6, 6, 7, - 7, 7, 8, 8, 8, 9, 9, 9, - 10, 10, 10, 11, 11, 11, 12, 12, - 12, 13, 13, 13, 14, 14, 15, 15, - 16, 17, 18, 19, 20, 0, 0, 0, - 0, 1, 1, 1, 1, 2, 2, 2, - 3, 3, 3, 4, 4, 5, 5, 6, - 6, 7, 7, 8, 8, 9, 9, 10, - 10, 11, 11, 12, 12, 13, 13, 14, - 15, 16, 17, 18, 19, 20, 21, 22, - 23, 24, 25, 26, -}; - -/* vlc table 1, for intra chroma and P macro blocks */ - -static const uint16_t table1_vlc[149][2] = { -{ 0x4, 3 },{ 0x14, 5 },{ 0x17, 7 },{ 0x7f, 8 }, -{ 0x154, 9 },{ 0x1f2, 10 },{ 0xbf, 11 },{ 0x65, 12 }, -{ 0xaaa, 12 },{ 0x630, 13 },{ 0x1597, 13 },{ 0x3b7, 14 }, -{ 0x2b22, 14 },{ 0xbe6, 15 },{ 0xb, 4 },{ 0x37, 7 }, -{ 0x62, 9 },{ 0x7, 11 },{ 0x166, 12 },{ 0xce, 13 }, -{ 0x1590, 13 },{ 0x5f6, 14 },{ 0xbe7, 15 },{ 0x7, 5 }, -{ 0x6d, 8 },{ 0x3, 11 },{ 0x31f, 12 },{ 0x5f2, 14 }, -{ 0x2, 6 },{ 0x61, 9 },{ 0x55, 12 },{ 0x1df, 14 }, -{ 0x1a, 6 },{ 0x1e, 10 },{ 0xac9, 12 },{ 0x2b23, 14 }, -{ 0x1e, 6 },{ 0x1f, 10 },{ 0xac3, 12 },{ 0x2b2b, 14 }, -{ 0x6, 7 },{ 0x4, 11 },{ 0x2f8, 13 },{ 0x19, 7 }, -{ 0x6, 11 },{ 0x63d, 13 },{ 0x57, 7 },{ 0x182, 11 }, -{ 0x2aa2, 14 },{ 0x4, 8 },{ 0x180, 11 },{ 0x59c, 14 }, -{ 0x7d, 8 },{ 0x164, 12 },{ 0x76d, 15 },{ 0x2, 9 }, -{ 0x18d, 11 },{ 0x1581, 13 },{ 0xad, 8 },{ 0x60, 12 }, -{ 0xc67, 14 },{ 0x1c, 9 },{ 0xee, 13 },{ 0x3, 9 }, -{ 0x2cf, 13 },{ 0xd9, 9 },{ 0x1580, 13 },{ 0x2, 11 }, -{ 0x183, 11 },{ 0x57, 12 },{ 0x61, 12 },{ 0x31, 11 }, -{ 0x66, 12 },{ 0x631, 13 },{ 0x632, 13 },{ 0xac, 13 }, -{ 0x31d, 12 },{ 0x76, 12 },{ 0x3a, 11 },{ 0x165, 12 }, -{ 0xc66, 14 },{ 0x3, 2 },{ 0x54, 7 },{ 0x2ab, 10 }, -{ 0x16, 13 },{ 0x5f7, 14 },{ 0x5, 4 },{ 0xf8, 9 }, -{ 0xaa9, 12 },{ 0x5f, 15 },{ 0x4, 4 },{ 0x1c, 10 }, -{ 0x1550, 13 },{ 0x4, 5 },{ 0x77, 11 },{ 0x76c, 15 }, -{ 0xe, 5 },{ 0xa, 12 },{ 0xc, 5 },{ 0x562, 11 }, -{ 0x4, 6 },{ 0x31c, 12 },{ 0x6, 6 },{ 0xc8, 13 }, -{ 0xd, 6 },{ 0x1da, 13 },{ 0x7, 6 },{ 0xc9, 13 }, -{ 0x1, 7 },{ 0x2e, 14 },{ 0x14, 7 },{ 0x1596, 13 }, -{ 0xa, 7 },{ 0xac2, 12 },{ 0x16, 7 },{ 0x15b, 14 }, -{ 0x15, 7 },{ 0x15a, 14 },{ 0xf, 8 },{ 0x5e, 15 }, -{ 0x7e, 8 },{ 0xab, 8 },{ 0x2d, 9 },{ 0xd8, 9 }, -{ 0xb, 9 },{ 0x14, 10 },{ 0x2b3, 10 },{ 0x1f3, 10 }, -{ 0x3a, 10 },{ 0x0, 10 },{ 0x58, 10 },{ 0x2e, 9 }, -{ 0x5e, 10 },{ 0x563, 11 },{ 0xec, 12 },{ 0x54, 12 }, -{ 0xac1, 12 },{ 0x1556, 13 },{ 0x2fa, 13 },{ 0x181, 11 }, -{ 0x1557, 13 },{ 0x59d, 14 },{ 0x2aa3, 14 },{ 0x2b2a, 14 }, -{ 0x1de, 14 },{ 0x63c, 13 },{ 0xcf, 13 },{ 0x1594, 13 }, -{ 0xd, 9 }, -}; - -static const int8_t table1_level[148] = { - 1, 2, 3, 4, 5, 6, 7, 8, - 9, 10, 11, 12, 13, 14, 1, 2, - 3, 4, 5, 6, 7, 8, 9, 1, - 2, 3, 4, 5, 1, 2, 3, 4, - 1, 2, 3, 4, 1, 2, 3, 4, - 1, 2, 3, 1, 2, 3, 1, 2, - 3, 1, 2, 3, 1, 2, 3, 1, - 2, 3, 1, 2, 3, 1, 2, 1, - 2, 1, 2, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 2, 3, 4, 5, 1, 2, - 3, 4, 1, 2, 3, 1, 2, 3, - 1, 2, 1, 2, 1, 2, 1, 2, - 1, 2, 1, 2, 1, 2, 1, 2, - 1, 2, 1, 2, 1, 2, 1, 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, -}; - -static const int8_t table1_run[148] = { - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 1, 1, - 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, 9, 10, 10, 10, 11, - 11, 11, 12, 12, 12, 13, 13, 14, - 14, 15, 15, 16, 17, 18, 19, 20, - 21, 22, 23, 24, 25, 26, 27, 28, - 29, 0, 0, 0, 0, 0, 1, 1, - 1, 1, 2, 2, 2, 3, 3, 3, - 4, 4, 5, 5, 6, 6, 7, 7, - 8, 8, 9, 9, 10, 10, 11, 11, - 12, 12, 13, 13, 14, 14, 15, 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, -}; - -/* third vlc table */ - -static const uint16_t table2_vlc[186][2] = { -{ 0x1, 2 },{ 0x5, 3 },{ 0xd, 4 },{ 0x12, 5 }, -{ 0xe, 6 },{ 0x15, 7 },{ 0x13, 8 },{ 0x3f, 8 }, -{ 0x4b, 9 },{ 0x11f, 9 },{ 0xb8, 10 },{ 0x3e3, 10 }, -{ 0x172, 11 },{ 0x24d, 12 },{ 0x3da, 12 },{ 0x2dd, 13 }, -{ 0x1f55, 13 },{ 0x5b9, 14 },{ 0x3eae, 14 },{ 0x0, 4 }, -{ 0x10, 5 },{ 0x8, 7 },{ 0x20, 8 },{ 0x29, 9 }, -{ 0x1f4, 9 },{ 0x233, 10 },{ 0x1e0, 11 },{ 0x12a, 12 }, -{ 0x3dd, 12 },{ 0x50a, 13 },{ 0x1f29, 13 },{ 0xa42, 14 }, -{ 0x1272, 15 },{ 0x1737, 15 },{ 0x3, 5 },{ 0x11, 7 }, -{ 0xc4, 8 },{ 0x4b, 10 },{ 0xb4, 11 },{ 0x7d4, 11 }, -{ 0x345, 12 },{ 0x2d7, 13 },{ 0x7bf, 13 },{ 0x938, 14 }, -{ 0xbbb, 14 },{ 0x95e, 15 },{ 0x13, 5 },{ 0x78, 7 }, -{ 0x69, 9 },{ 0x232, 10 },{ 0x461, 11 },{ 0x3ec, 12 }, -{ 0x520, 13 },{ 0x1f2a, 13 },{ 0x3e50, 14 },{ 0x3e51, 14 }, -{ 0x1486, 15 },{ 0xc, 6 },{ 0x24, 9 },{ 0x94, 11 }, -{ 0x8c0, 12 },{ 0xf09, 14 },{ 0x1ef0, 15 },{ 0x3d, 6 }, -{ 0x53, 9 },{ 0x1a0, 11 },{ 0x2d6, 13 },{ 0xf08, 14 }, -{ 0x13, 7 },{ 0x7c, 9 },{ 0x7c1, 11 },{ 0x4ac, 14 }, -{ 0x1b, 7 },{ 0xa0, 10 },{ 0x344, 12 },{ 0xf79, 14 }, -{ 0x79, 7 },{ 0x3e1, 10 },{ 0x2d4, 13 },{ 0x2306, 14 }, -{ 0x21, 8 },{ 0x23c, 10 },{ 0xfae, 12 },{ 0x23de, 14 }, -{ 0x35, 8 },{ 0x175, 11 },{ 0x7b3, 13 },{ 0xc5, 8 }, -{ 0x174, 11 },{ 0x785, 13 },{ 0x48, 9 },{ 0x1a3, 11 }, -{ 0x49e, 13 },{ 0x2c, 9 },{ 0xfa, 10 },{ 0x7d6, 11 }, -{ 0x92, 10 },{ 0x5cc, 13 },{ 0x1ef1, 15 },{ 0xa3, 10 }, -{ 0x3ed, 12 },{ 0x93e, 14 },{ 0x1e2, 11 },{ 0x1273, 15 }, -{ 0x7c4, 11 },{ 0x1487, 15 },{ 0x291, 12 },{ 0x293, 12 }, -{ 0xf8a, 12 },{ 0x509, 13 },{ 0x508, 13 },{ 0x78d, 13 }, -{ 0x7be, 13 },{ 0x78c, 13 },{ 0x4ae, 14 },{ 0xbba, 14 }, -{ 0x2307, 14 },{ 0xb9a, 14 },{ 0x1736, 15 },{ 0xe, 4 }, -{ 0x45, 7 },{ 0x1f3, 9 },{ 0x47a, 11 },{ 0x5dc, 13 }, -{ 0x23df, 14 },{ 0x19, 5 },{ 0x28, 9 },{ 0x176, 11 }, -{ 0x49d, 13 },{ 0x23dd, 14 },{ 0x30, 6 },{ 0xa2, 10 }, -{ 0x2ef, 12 },{ 0x5b8, 14 },{ 0x3f, 6 },{ 0xa5, 10 }, -{ 0x3db, 12 },{ 0x93f, 14 },{ 0x44, 7 },{ 0x7cb, 11 }, -{ 0x95f, 15 },{ 0x63, 7 },{ 0x3c3, 12 },{ 0x15, 8 }, -{ 0x8f6, 12 },{ 0x17, 8 },{ 0x498, 13 },{ 0x2c, 8 }, -{ 0x7b2, 13 },{ 0x2f, 8 },{ 0x1f54, 13 },{ 0x8d, 8 }, -{ 0x7bd, 13 },{ 0x8e, 8 },{ 0x1182, 13 },{ 0xfb, 8 }, -{ 0x50b, 13 },{ 0x2d, 8 },{ 0x7c0, 11 },{ 0x79, 9 }, -{ 0x1f5f, 13 },{ 0x7a, 9 },{ 0x1f56, 13 },{ 0x231, 10 }, -{ 0x3e4, 10 },{ 0x1a1, 11 },{ 0x143, 11 },{ 0x1f7, 11 }, -{ 0x16f, 12 },{ 0x292, 12 },{ 0x2e7, 12 },{ 0x16c, 12 }, -{ 0x16d, 12 },{ 0x3dc, 12 },{ 0xf8b, 12 },{ 0x499, 13 }, -{ 0x3d8, 12 },{ 0x78e, 13 },{ 0x2d5, 13 },{ 0x1f5e, 13 }, -{ 0x1f2b, 13 },{ 0x78f, 13 },{ 0x4ad, 14 },{ 0x3eaf, 14 }, -{ 0x23dc, 14 },{ 0x4a, 9 }, -}; - -static const int8_t table2_level[185] = { - 1, 2, 3, 4, 5, 6, 7, 8, - 9, 10, 11, 12, 13, 14, 15, 16, - 17, 18, 19, 1, 2, 3, 4, 5, - 6, 7, 8, 9, 10, 11, 12, 13, - 14, 15, 1, 2, 3, 4, 5, 6, - 7, 8, 9, 10, 11, 12, 1, 2, - 3, 4, 5, 6, 7, 8, 9, 10, - 11, 1, 2, 3, 4, 5, 6, 1, - 2, 3, 4, 5, 1, 2, 3, 4, - 1, 2, 3, 4, 1, 2, 3, 4, - 1, 2, 3, 4, 1, 2, 3, 1, - 2, 3, 1, 2, 3, 1, 2, 3, - 1, 2, 3, 1, 2, 3, 1, 2, - 1, 2, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, - 2, 3, 4, 5, 6, 1, 2, 3, - 4, 5, 1, 2, 3, 4, 1, 2, - 3, 4, 1, 2, 3, 1, 2, 1, - 2, 1, 2, 1, 2, 1, 2, 1, - 2, 1, 2, 1, 2, 1, 2, 1, - 2, 1, 2, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, - 1, -}; - -static const int8_t table2_run[185] = { - 0, 0, 0, 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, 1, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, - 3, 4, 4, 4, 4, 4, 4, 5, - 5, 5, 5, 5, 6, 6, 6, 6, - 7, 7, 7, 7, 8, 8, 8, 8, - 9, 9, 9, 9, 10, 10, 10, 11, - 11, 11, 12, 12, 12, 13, 13, 13, - 14, 14, 14, 15, 15, 15, 16, 16, - 17, 17, 18, 19, 20, 21, 22, 23, - 24, 25, 26, 27, 28, 29, 30, 0, - 0, 0, 0, 0, 0, 1, 1, 1, - 1, 1, 2, 2, 2, 2, 3, 3, - 3, 3, 4, 4, 4, 5, 5, 6, - 6, 7, 7, 8, 8, 9, 9, 10, - 10, 11, 11, 12, 12, 13, 13, 14, - 14, 15, 15, 16, 17, 18, 19, 20, - 21, 22, 23, 24, 25, 26, 27, 28, - 29, 30, 31, 32, 33, 34, 35, 36, - 37, -}; - -/* second non intra vlc table */ -static const uint16_t table4_vlc[169][2] = { -{ 0x0, 3 },{ 0x3, 4 },{ 0xb, 5 },{ 0x14, 6 }, -{ 0x3f, 6 },{ 0x5d, 7 },{ 0xa2, 8 },{ 0xac, 9 }, -{ 0x16e, 9 },{ 0x20a, 10 },{ 0x2e2, 10 },{ 0x432, 11 }, -{ 0x5c9, 11 },{ 0x827, 12 },{ 0xb54, 12 },{ 0x4e6, 13 }, -{ 0x105f, 13 },{ 0x172a, 13 },{ 0x20b2, 14 },{ 0x2d4e, 14 }, -{ 0x39f0, 14 },{ 0x4175, 15 },{ 0x5a9e, 15 },{ 0x4, 4 }, -{ 0x1e, 5 },{ 0x42, 7 },{ 0xb6, 8 },{ 0x173, 9 }, -{ 0x395, 10 },{ 0x72e, 11 },{ 0xb94, 12 },{ 0x16a4, 13 }, -{ 0x20b3, 14 },{ 0x2e45, 14 },{ 0x5, 5 },{ 0x40, 7 }, -{ 0x49, 9 },{ 0x28f, 10 },{ 0x5cb, 11 },{ 0x48a, 13 }, -{ 0x9dd, 14 },{ 0x73e2, 15 },{ 0x18, 5 },{ 0x25, 8 }, -{ 0x8a, 10 },{ 0x51b, 11 },{ 0xe5f, 12 },{ 0x9c9, 14 }, -{ 0x139c, 15 },{ 0x29, 6 },{ 0x4f, 9 },{ 0x412, 11 }, -{ 0x48d, 13 },{ 0x2e41, 14 },{ 0x38, 6 },{ 0x10e, 9 }, -{ 0x5a8, 11 },{ 0x105c, 13 },{ 0x39f2, 14 },{ 0x58, 7 }, -{ 0x21f, 10 },{ 0xe7e, 12 },{ 0x39ff, 14 },{ 0x23, 8 }, -{ 0x2e3, 10 },{ 0x4e5, 13 },{ 0x2e40, 14 },{ 0xa1, 8 }, -{ 0x5be, 11 },{ 0x9c8, 14 },{ 0x83, 8 },{ 0x13a, 11 }, -{ 0x1721, 13 },{ 0x44, 9 },{ 0x276, 12 },{ 0x39f6, 14 }, -{ 0x8b, 10 },{ 0x4ef, 13 },{ 0x5a9b, 15 },{ 0x208, 10 }, -{ 0x1cfe, 13 },{ 0x399, 10 },{ 0x1cb4, 13 },{ 0x39e, 10 }, -{ 0x39f3, 14 },{ 0x5ab, 11 },{ 0x73e3, 15 },{ 0x737, 11 }, -{ 0x5a9f, 15 },{ 0x82d, 12 },{ 0xe69, 12 },{ 0xe68, 12 }, -{ 0x433, 11 },{ 0xb7b, 12 },{ 0x2df8, 14 },{ 0x2e56, 14 }, -{ 0x2e57, 14 },{ 0x39f7, 14 },{ 0x51a5, 15 },{ 0x3, 3 }, -{ 0x2a, 6 },{ 0xe4, 8 },{ 0x28e, 10 },{ 0x735, 11 }, -{ 0x1058, 13 },{ 0x1cfa, 13 },{ 0x2df9, 14 },{ 0x4174, 15 }, -{ 0x9, 4 },{ 0x54, 8 },{ 0x398, 10 },{ 0x48b, 13 }, -{ 0x139d, 15 },{ 0xd, 4 },{ 0xad, 9 },{ 0x826, 12 }, -{ 0x2d4c, 14 },{ 0x11, 5 },{ 0x16b, 9 },{ 0xb7f, 12 }, -{ 0x51a4, 15 },{ 0x19, 5 },{ 0x21b, 10 },{ 0x16fd, 13 }, -{ 0x1d, 5 },{ 0x394, 10 },{ 0x28d3, 14 },{ 0x2b, 6 }, -{ 0x5bc, 11 },{ 0x5a9a, 15 },{ 0x2f, 6 },{ 0x247, 12 }, -{ 0x10, 7 },{ 0xa35, 12 },{ 0x3e, 6 },{ 0xb7a, 12 }, -{ 0x59, 7 },{ 0x105e, 13 },{ 0x26, 8 },{ 0x9cf, 14 }, -{ 0x55, 8 },{ 0x1cb5, 13 },{ 0x57, 8 },{ 0xe5b, 12 }, -{ 0xa0, 8 },{ 0x1468, 13 },{ 0x170, 9 },{ 0x90, 10 }, -{ 0x1ce, 9 },{ 0x21a, 10 },{ 0x218, 10 },{ 0x168, 9 }, -{ 0x21e, 10 },{ 0x244, 12 },{ 0x736, 11 },{ 0x138, 11 }, -{ 0x519, 11 },{ 0xe5e, 12 },{ 0x72c, 11 },{ 0xb55, 12 }, -{ 0x9dc, 14 },{ 0x20bb, 14 },{ 0x48c, 13 },{ 0x1723, 13 }, -{ 0x2e44, 14 },{ 0x16a5, 13 },{ 0x518, 11 },{ 0x39fe, 14 }, -{ 0x169, 9 }, -}; - -static const int8_t table4_level[168] = { - 1, 2, 3, 4, 5, 6, 7, 8, - 9, 10, 11, 12, 13, 14, 15, 16, - 17, 18, 19, 20, 21, 22, 23, 1, - 2, 3, 4, 5, 6, 7, 8, 9, - 10, 11, 1, 2, 3, 4, 5, 6, - 7, 8, 1, 2, 3, 4, 5, 6, - 7, 1, 2, 3, 4, 5, 1, 2, - 3, 4, 5, 1, 2, 3, 4, 1, - 2, 3, 4, 1, 2, 3, 1, 2, - 3, 1, 2, 3, 1, 2, 3, 1, - 2, 1, 2, 1, 2, 1, 2, 1, - 2, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 2, 3, 4, 5, - 6, 7, 8, 9, 1, 2, 3, 4, - 5, 1, 2, 3, 4, 1, 2, 3, - 4, 1, 2, 3, 1, 2, 3, 1, - 2, 3, 1, 2, 1, 2, 1, 2, - 1, 2, 1, 2, 1, 2, 1, 2, - 1, 2, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, -}; - -static const int8_t table4_run[168] = { - 0, 0, 0, 0, 0, 0, 0, 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, 2, 2, 2, 2, 2, 2, - 2, 2, 3, 3, 3, 3, 3, 3, - 3, 4, 4, 4, 4, 4, 5, 5, - 5, 5, 5, 6, 6, 6, 6, 7, - 7, 7, 7, 8, 8, 8, 9, 9, - 9, 10, 10, 10, 11, 11, 11, 12, - 12, 13, 13, 14, 14, 15, 15, 16, - 16, 17, 18, 19, 20, 21, 22, 23, - 24, 25, 26, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 1, 1, 1, 1, - 1, 2, 2, 2, 2, 3, 3, 3, - 3, 4, 4, 4, 5, 5, 5, 6, - 6, 6, 7, 7, 8, 8, 9, 9, - 10, 10, 11, 11, 12, 12, 13, 13, - 14, 14, 15, 16, 17, 18, 19, 20, - 21, 22, 23, 24, 25, 26, 27, 28, - 29, 30, 31, 32, 33, 34, 35, 36, -}; - -extern const uint16_t inter_vlc[103][2]; -extern const int8_t inter_level[102]; -extern const int8_t inter_run[102]; - -extern const uint16_t intra_vlc[103][2]; -extern const int8_t intra_level[102]; -extern const int8_t intra_run[102]; - -extern const uint8_t DCtab_lum[13][2]; -extern const uint8_t DCtab_chrom[13][2]; - -extern const uint8_t cbpy_tab[16][2]; -extern const uint8_t mvtab[33][2]; - -extern const uint8_t intra_MCBPC_code[8]; -extern const uint8_t intra_MCBPC_bits[8]; - -extern const uint8_t inter_MCBPC_code[25]; -extern const uint8_t inter_MCBPC_bits[25]; - -#define NB_RL_TABLES 6 - -static RLTable rl_table[NB_RL_TABLES] = { - /* intra luminance tables */ - { - 132, - 85, - table0_vlc, - table0_run, - table0_level, - }, - { - 185, - 119, - table2_vlc, - table2_run, - table2_level, - }, - { - 102, - 67, - intra_vlc, - intra_run, - intra_level, - }, - /* intra chrominance / non intra tables */ - { - 148, - 81, - table1_vlc, - table1_run, - table1_level, - }, - { - 168, - 99, - table4_vlc, - table4_run, - table4_level, - }, - { - 102, - 58, - inter_vlc, - inter_run, - inter_level, - }, -}; - -/* motion vector table 0 */ - -static const uint16_t table0_mv_code[1100] = { - 0x0001, 0x0003, 0x0005, 0x0007, 0x0003, 0x0008, 0x000c, 0x0001, - 0x0002, 0x001b, 0x0006, 0x000b, 0x0015, 0x0002, 0x000e, 0x000f, - 0x0014, 0x0020, 0x0022, 0x0025, 0x0027, 0x0029, 0x002d, 0x004b, - 0x004d, 0x0003, 0x0022, 0x0023, 0x0025, 0x0027, 0x0042, 0x0048, - 0x0049, 0x0050, 0x005c, 0x0091, 0x009f, 0x000e, 0x0043, 0x004c, - 0x0054, 0x0056, 0x008c, 0x0098, 0x009a, 0x009b, 0x00b1, 0x00b2, - 0x0120, 0x0121, 0x0126, 0x0133, 0x0139, 0x01a1, 0x01a4, 0x01a5, - 0x01a6, 0x01a7, 0x01ae, 0x01af, 0x000b, 0x0019, 0x0085, 0x0090, - 0x009b, 0x00aa, 0x00af, 0x010c, 0x010e, 0x011c, 0x011e, 0x0133, - 0x0144, 0x0160, 0x0174, 0x0175, 0x0177, 0x0178, 0x0249, 0x024b, - 0x0252, 0x0261, 0x0265, 0x0270, 0x0352, 0x0353, 0x0355, 0x0359, - 0x0010, 0x0011, 0x0013, 0x0034, 0x0035, 0x0036, 0x0037, 0x003d, - 0x003e, 0x0109, 0x0126, 0x0156, 0x021a, 0x021e, 0x023a, 0x023e, - 0x028e, 0x028f, 0x02cf, 0x0491, 0x0494, 0x049f, 0x04a0, 0x04a3, - 0x04a6, 0x04a7, 0x04ad, 0x04ae, 0x04c0, 0x04c4, 0x04c6, 0x04c8, - 0x04c9, 0x04f5, 0x04f6, 0x04f7, 0x0680, 0x0682, 0x0683, 0x0688, - 0x0689, 0x068d, 0x068e, 0x068f, 0x06a2, 0x06a3, 0x06a9, 0x06b0, - 0x06b1, 0x06b4, 0x06b5, 0x0024, 0x0060, 0x0063, 0x0078, 0x0079, - 0x0211, 0x0244, 0x0245, 0x0247, 0x0248, 0x0249, 0x024a, 0x024b, - 0x026b, 0x02af, 0x02b8, 0x02bb, 0x0436, 0x0476, 0x0477, 0x047e, - 0x04c8, 0x04c9, 0x04ca, 0x0514, 0x0586, 0x0587, 0x0598, 0x059d, - 0x05d9, 0x05da, 0x0920, 0x0921, 0x093b, 0x093c, 0x093d, 0x0942, - 0x0943, 0x0944, 0x0945, 0x0959, 0x095e, 0x095f, 0x0982, 0x0983, - 0x098e, 0x098f, 0x09c4, 0x09e7, 0x09e8, 0x09e9, 0x0d02, 0x0d17, - 0x0d18, 0x0d19, 0x0d41, 0x0d42, 0x0d43, 0x0d50, 0x0d5f, 0x0d6d, - 0x0d6e, 0x0d6f, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, - 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x041e, 0x041f, 0x0420, 0x0421, - 0x048c, 0x048d, 0x04d3, 0x04d4, 0x04d5, 0x055c, 0x055d, 0x0572, - 0x0573, 0x0574, 0x0575, 0x08de, 0x08df, 0x08fe, 0x08ff, 0x0996, - 0x0a36, 0x0a37, 0x0b08, 0x0b09, 0x0b0a, 0x0b0b, 0x0b32, 0x0b33, - 0x0b34, 0x0b35, 0x0b36, 0x0b37, 0x0b38, 0x0b39, 0x0bb0, 0x0bf7, - 0x0bf8, 0x0bf9, 0x0bfa, 0x0bfb, 0x0bfc, 0x0bfd, 0x0bfe, 0x0bff, - 0x1254, 0x1255, 0x1256, 0x1257, 0x1270, 0x1271, 0x1272, 0x1273, - 0x1274, 0x1275, 0x12ab, 0x12ac, 0x12ad, 0x12ae, 0x12af, 0x12b0, - 0x12b1, 0x1315, 0x1316, 0x1317, 0x13bf, 0x13c0, 0x13c1, 0x13c2, - 0x13c3, 0x13c4, 0x13c5, 0x13c6, 0x13c7, 0x13c8, 0x13c9, 0x13ca, - 0x13cb, 0x13cc, 0x13cd, 0x1a06, 0x1a07, 0x1a28, 0x1a29, 0x1a2a, - 0x1a2b, 0x1a2c, 0x1a2d, 0x1a80, 0x1abb, 0x1abc, 0x1abd, 0x1ad8, - 0x1ad9, 0x0094, 0x0095, 0x0096, 0x0097, 0x00a0, 0x00a1, 0x00a2, - 0x00a3, 0x0831, 0x0832, 0x0833, 0x0834, 0x0835, 0x0836, 0x0837, - 0x0838, 0x0839, 0x083a, 0x083b, 0x0939, 0x093a, 0x093b, 0x093c, - 0x093d, 0x093e, 0x093f, 0x09a0, 0x09a1, 0x09a2, 0x09a3, 0x09a4, - 0x09a5, 0x11ac, 0x11ad, 0x11ae, 0x11af, 0x11b0, 0x11b1, 0x11b2, - 0x11b3, 0x11b4, 0x11b5, 0x11b6, 0x11b7, 0x11b8, 0x11b9, 0x11ba, - 0x11bb, 0x132f, 0x1454, 0x1455, 0x1456, 0x1457, 0x1458, 0x1459, - 0x145a, 0x145b, 0x145c, 0x145d, 0x145e, 0x145f, 0x1460, 0x1461, - 0x1462, 0x1463, 0x1464, 0x1465, 0x1466, 0x1467, 0x1468, 0x1469, - 0x146a, 0x146b, 0x17de, 0x17df, 0x17e0, 0x17e1, 0x17e2, 0x17e3, - 0x17e4, 0x17e5, 0x17e6, 0x17e7, 0x17e8, 0x17e9, 0x17ea, 0x17eb, - 0x17ec, 0x17ed, 0x2540, 0x2541, 0x2542, 0x2543, 0x2544, 0x2545, - 0x2546, 0x2547, 0x2548, 0x2549, 0x254a, 0x254b, 0x254c, 0x254d, - 0x254e, 0x254f, 0x2550, 0x2551, 0x2552, 0x2553, 0x2554, 0x2555, - 0x2628, 0x2766, 0x2767, 0x2768, 0x2769, 0x276a, 0x276b, 0x276c, - 0x276d, 0x276e, 0x276f, 0x2770, 0x2771, 0x2772, 0x2773, 0x2774, - 0x2775, 0x2776, 0x2777, 0x2778, 0x2779, 0x277a, 0x277b, 0x277c, - 0x277d, 0x3503, 0x3544, 0x3545, 0x3546, 0x3547, 0x3560, 0x3561, - 0x3562, 0x3563, 0x3564, 0x3565, 0x3566, 0x3567, 0x3568, 0x3569, - 0x356a, 0x356b, 0x356c, 0x356d, 0x356e, 0x356f, 0x3570, 0x3571, - 0x3572, 0x3573, 0x3574, 0x3575, 0x03f0, 0x103d, 0x103e, 0x103f, - 0x1040, 0x1041, 0x1042, 0x1043, 0x1044, 0x1045, 0x1046, 0x1047, - 0x1048, 0x1049, 0x104a, 0x104b, 0x104c, 0x104d, 0x104e, 0x104f, - 0x1050, 0x1051, 0x1052, 0x1053, 0x1054, 0x1055, 0x1056, 0x1057, - 0x1058, 0x1059, 0x105a, 0x105b, 0x105c, 0x105d, 0x105e, 0x105f, - 0x1060, 0x1061, 0x1270, 0x1271, 0x21b8, 0x21b9, 0x21ba, 0x21bb, - 0x21bc, 0x21bd, 0x21be, 0x21bf, 0x21f0, 0x21f1, 0x21f2, 0x21f3, - 0x21f4, 0x21f5, 0x21f6, 0x21f7, 0x21f8, 0x21f9, 0x21fa, 0x21fb, - 0x21fc, 0x21fd, 0x21fe, 0x21ff, 0x2340, 0x2341, 0x2342, 0x2343, - 0x2344, 0x2345, 0x2346, 0x2347, 0x2348, 0x2349, 0x234a, 0x234b, - 0x234c, 0x234d, 0x234e, 0x234f, 0x2350, 0x2351, 0x2352, 0x2353, - 0x2354, 0x2355, 0x2356, 0x2357, 0x265c, 0x2f88, 0x2f89, 0x2f8a, - 0x2f8b, 0x2f8c, 0x2f8d, 0x2f8e, 0x2f8f, 0x2f90, 0x2f91, 0x2f92, - 0x2f93, 0x2f94, 0x2f95, 0x2f96, 0x2f97, 0x2f98, 0x2f99, 0x2f9a, - 0x2f9b, 0x2f9c, 0x2f9d, 0x2f9e, 0x2f9f, 0x2fa0, 0x2fa1, 0x2fa2, - 0x2fa3, 0x2fa4, 0x2fa5, 0x2fa6, 0x2fa7, 0x2fa8, 0x2fa9, 0x2faa, - 0x2fab, 0x2fac, 0x2fad, 0x2fae, 0x2faf, 0x2fb0, 0x2fb1, 0x2fb2, - 0x2fb3, 0x2fb4, 0x2fb5, 0x2fb6, 0x2fb7, 0x2fb8, 0x2fb9, 0x2fba, - 0x2fbb, 0x4c52, 0x4c53, 0x4e28, 0x4e29, 0x4e2a, 0x4e2b, 0x4e2c, - 0x4e2d, 0x4e2e, 0x4e2f, 0x4e30, 0x4e31, 0x4e32, 0x4e33, 0x4e34, - 0x4e35, 0x4e36, 0x4e37, 0x4e38, 0x4e39, 0x4e3a, 0x4e3b, 0x4e3c, - 0x4e3d, 0x4e3e, 0x4e3f, 0x4e80, 0x4e81, 0x4e82, 0x4e83, 0x4e84, - 0x4e85, 0x4e86, 0x4e87, 0x4e88, 0x4e89, 0x4e8a, 0x4e8b, 0x4e8c, - 0x4e8d, 0x4e8e, 0x4e8f, 0x4e90, 0x4e91, 0x4e92, 0x4e93, 0x4e94, - 0x4e95, 0x4e96, 0x4e97, 0x4e98, 0x4e99, 0x4e9a, 0x4e9b, 0x4e9c, - 0x4e9d, 0x4e9e, 0x4e9f, 0x4ea0, 0x4ea1, 0x4ea2, 0x4ea3, 0x4ea4, - 0x4ea5, 0x4ea6, 0x4ea7, 0x4ea8, 0x4ea9, 0x4eaa, 0x4eab, 0x4eac, - 0x4ead, 0x4eae, 0x4eaf, 0x4eb0, 0x4eb1, 0x4eb2, 0x4eb3, 0x4eb4, - 0x4eb5, 0x4eb6, 0x4eb7, 0x4eb8, 0x4eb9, 0x4eba, 0x4ebb, 0x4ebc, - 0x4ebd, 0x4ebe, 0x4ebf, 0x4ec0, 0x4ec1, 0x4ec2, 0x4ec3, 0x4ec4, - 0x4ec5, 0x4ec6, 0x4ec7, 0x4ec8, 0x4ec9, 0x4eca, 0x4ecb, 0x6a04, - 0x6a05, 0x07e2, 0x07e3, 0x07e4, 0x07e5, 0x07e6, 0x07e7, 0x07e8, - 0x07e9, 0x07ea, 0x07eb, 0x07ec, 0x07ed, 0x07ee, 0x07ef, 0x07f0, - 0x07f1, 0x07f2, 0x07f3, 0x07f4, 0x07f5, 0x07f6, 0x07f7, 0x07f8, - 0x07f9, 0x07fa, 0x07fb, 0x07fc, 0x07fd, 0x07fe, 0x07ff, 0x2000, - 0x2001, 0x2002, 0x2003, 0x2004, 0x2005, 0x2006, 0x2007, 0x2008, - 0x2009, 0x200a, 0x200b, 0x200c, 0x200d, 0x200e, 0x200f, 0x2010, - 0x2011, 0x2012, 0x2013, 0x2014, 0x2015, 0x2016, 0x2017, 0x2018, - 0x2019, 0x201a, 0x201b, 0x201c, 0x201d, 0x201e, 0x201f, 0x2020, - 0x2021, 0x2022, 0x2023, 0x2024, 0x2025, 0x2026, 0x2027, 0x2028, - 0x2029, 0x202a, 0x202b, 0x202c, 0x202d, 0x202e, 0x202f, 0x2030, - 0x2031, 0x2032, 0x2033, 0x2034, 0x2035, 0x2036, 0x2037, 0x2038, - 0x2039, 0x203a, 0x203b, 0x203c, 0x203d, 0x203e, 0x203f, 0x2040, - 0x2041, 0x2042, 0x2043, 0x2044, 0x2045, 0x2046, 0x2047, 0x2048, - 0x2049, 0x204a, 0x204b, 0x204c, 0x204d, 0x204e, 0x204f, 0x2050, - 0x2051, 0x2052, 0x2053, 0x2054, 0x2055, 0x2056, 0x2057, 0x2058, - 0x2059, 0x205a, 0x205b, 0x205c, 0x205d, 0x205e, 0x205f, 0x2060, - 0x2061, 0x2062, 0x2063, 0x2064, 0x2065, 0x2066, 0x2067, 0x2068, - 0x2069, 0x206a, 0x206b, 0x206c, 0x206d, 0x206e, 0x206f, 0x2070, - 0x2071, 0x2072, 0x2073, 0x2074, 0x2075, 0x2076, 0x2077, 0x2078, - 0x2079, 0x4cba, 0x4cbb, 0x5d88, 0x5d89, 0x5d8a, 0x5d8b, 0x5d8c, - 0x5d8d, 0x5d8e, 0x5d8f, 0x5db0, 0x5db1, 0x5db2, 0x5db3, 0x5db4, - 0x5db5, 0x5db6, 0x5db7, 0x5db8, 0x5db9, 0x5dba, 0x5dbb, 0x5dbc, - 0x5dbd, 0x5dbe, 0x5dbf, 0x5e40, 0x5e41, 0x5e42, 0x5e43, 0x5e44, - 0x5e45, 0x5e46, 0x5e47, 0x5e48, 0x5e49, 0x5e4a, 0x5e4b, 0x5e4c, - 0x5e4d, 0x5e4e, 0x5e4f, 0x5e50, 0x5e51, 0x5e52, 0x5e53, 0x5e54, - 0x5e55, 0x5e56, 0x5e57, 0x5e58, 0x5e59, 0x5e5a, 0x5e5b, 0x5e5c, - 0x5e5d, 0x5e5e, 0x5e5f, 0x5e60, 0x5e61, 0x5e62, 0x5e63, 0x5e64, - 0x5e65, 0x5e66, 0x5e67, 0x5e68, 0x5e69, 0x5e6a, 0x5e6b, 0x5e6c, - 0x5e6d, 0x5e6e, 0x5e6f, 0x5e70, 0x5e71, 0x5e72, 0x5e73, 0x5e74, - 0x5e75, 0x5e76, 0x5e77, 0x5e78, 0x5e79, 0x5e7a, 0x5e7b, 0x5e7c, - 0x5e7d, 0x5e7e, 0x5e7f, 0x5e80, 0x5e81, 0x5e82, 0x5e83, 0x5e84, - 0x5e85, 0x5e86, 0x5e87, 0x5e88, 0x5e89, 0x5e8a, 0x5e8b, 0x5e8c, - 0x5e8d, 0x5e8e, 0x5e8f, 0x5e90, 0x5e91, 0x5e92, 0x5e93, 0x5e94, - 0x5e95, 0x5e96, 0x5e97, 0x5e98, 0x5e99, 0x5e9a, 0x5e9b, 0x5e9c, - 0x5e9d, 0x5e9e, 0x5e9f, 0x5ea0, 0x5ea1, 0x5ea2, 0x5ea3, 0x5ea4, - 0x5ea5, 0x5ea6, 0x5ea7, 0x5ea8, 0x5ea9, 0x5eaa, 0x5eab, 0x5eac, - 0x5ead, 0x5eae, 0x5eaf, 0x5eb0, 0x5eb1, 0x5eb2, 0x5eb3, 0x5eb4, - 0x5eb5, 0x5eb6, 0x5eb7, 0x5eb8, 0x5eb9, 0x5eba, 0x5ebb, 0x5ebc, - 0x5ebd, 0x5ebe, 0x5ebf, 0x5ec0, 0x5ec1, 0x5ec2, 0x5ec3, 0x5ec4, - 0x5ec5, 0x5ec6, 0x5ec7, 0x5ec8, 0x5ec9, 0x5eca, 0x5ecb, 0x5ecc, - 0x5ecd, 0x5ece, 0x5ecf, 0x5ed0, 0x5ed1, 0x5ed2, 0x5ed3, 0x5ed4, - 0x5ed5, 0x5ed6, 0x5ed7, 0x5ed8, 0x5ed9, 0x5eda, 0x5edb, 0x5edc, - 0x5edd, 0x5ede, 0x5edf, 0x5ee0, 0x5ee1, 0x5ee2, 0x5ee3, 0x5ee4, - 0x5ee5, 0x5ee6, 0x5ee7, 0x5ee8, 0x5ee9, 0x5eea, 0x5eeb, 0x5eec, - 0x5eed, 0x5eee, 0x5eef, 0x5ef0, 0x5ef1, 0x5ef2, 0x5ef3, 0x5ef4, - 0x5ef5, 0x5ef6, 0x5ef7, 0x5ef8, 0x5ef9, 0x5efa, 0x5efb, 0x5efc, - 0x5efd, 0x5efe, 0x5eff, 0x5f00, 0x5f01, 0x5f02, 0x5f03, 0x5f04, - 0x5f05, 0x5f06, 0x5f07, 0x5f08, 0x5f09, 0x5f0a, 0x5f0b, 0x5f0c, - 0x5f0d, 0x5f0e, 0x5f0f, 0x0000, -}; - -static const uint8_t table0_mv_bits[1100] = { - 1, 4, 4, 4, 5, 5, 5, 6, - 6, 6, 7, 7, 7, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, - 8, 9, 9, 9, 9, 9, 9, 9, - 9, 9, 9, 9, 9, 10, 10, 10, - 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 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, - 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, - 16, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 17, 17, 17, 17, 17, - 17, 17, 17, 8, -}; - -static const uint8_t table0_mvx[1099] = { - 32, 32, 31, 32, 33, 31, 33, 31, - 33, 32, 34, 32, 30, 32, 31, 34, - 35, 32, 34, 33, 29, 33, 30, 30, - 31, 31, 35, 29, 33, 35, 33, 34, - 31, 29, 30, 34, 30, 36, 28, 32, - 34, 37, 30, 27, 32, 25, 39, 32, - 34, 32, 35, 35, 35, 31, 35, 29, - 32, 29, 30, 29, 37, 27, 36, 38, - 37, 33, 32, 31, 29, 31, 28, 36, - 33, 30, 34, 33, 33, 28, 27, 25, - 31, 26, 39, 32, 32, 31, 33, 39, - 31, 38, 28, 36, 21, 23, 43, 36, - 34, 41, 30, 25, 28, 31, 30, 34, - 38, 35, 61, 34, 28, 30, 37, 37, - 35, 27, 36, 3, 59, 38, 37, 32, - 31, 29, 26, 33, 37, 33, 27, 27, - 35, 34, 34, 40, 42, 33, 32, 29, - 4, 5, 28, 24, 25, 35, 39, 38, - 32, 23, 27, 32, 30, 35, 26, 34, - 60, 36, 29, 22, 26, 41, 7, 30, - 38, 30, 36, 29, 30, 41, 26, 25, - 32, 34, 24, 39, 1, 25, 39, 32, - 28, 29, 32, 38, 26, 36, 28, 63, - 28, 39, 23, 21, 26, 35, 31, 35, - 57, 31, 29, 29, 28, 30, 27, 35, - 2, 38, 40, 34, 37, 29, 38, 43, - 26, 32, 33, 42, 24, 40, 28, 32, - 32, 32, 36, 32, 43, 25, 21, 31, - 30, 31, 41, 29, 33, 37, 26, 37, - 27, 59, 23, 33, 35, 31, 31, 37, - 38, 39, 32, 23, 32, 27, 37, 36, - 31, 40, 25, 27, 38, 31, 36, 28, - 31, 36, 25, 45, 3, 34, 38, 39, - 40, 38, 30, 32, 19, 24, 25, 26, - 45, 20, 24, 33, 33, 31, 41, 34, - 39, 47, 40, 58, 59, 41, 33, 3, - 17, 61, 42, 30, 26, 29, 36, 61, - 33, 37, 62, 28, 25, 38, 25, 38, - 17, 23, 34, 33, 21, 33, 49, 27, - 32, 23, 27, 22, 24, 22, 39, 43, - 27, 37, 6, 42, 47, 26, 30, 31, - 41, 39, 33, 22, 45, 36, 32, 45, - 19, 22, 30, 5, 5, 17, 29, 22, - 31, 31, 43, 37, 27, 32, 32, 32, - 33, 34, 43, 35, 29, 26, 22, 32, - 19, 32, 25, 31, 41, 49, 28, 34, - 28, 39, 34, 19, 37, 38, 29, 21, - 36, 42, 24, 48, 16, 28, 49, 22, - 34, 31, 38, 39, 44, 11, 35, 30, - 33, 33, 23, 28, 33, 46, 15, 13, - 24, 41, 24, 34, 34, 30, 26, 24, - 14, 60, 21, 29, 39, 23, 35, 37, - 63, 45, 33, 34, 47, 41, 22, 42, - 35, 35, 23, 32, 35, 43, 32, 7, - 31, 41, 20, 31, 16, 13, 63, 25, - 30, 32, 35, 30, 30, 31, 42, 47, - 39, 38, 40, 40, 51, 55, 56, 18, - 21, 39, 39, 33, 17, 41, 23, 24, - 43, 25, 31, 20, 19, 45, 1, 34, - 31, 22, 35, 15, 46, 46, 35, 31, - 28, 29, 29, 23, 41, 27, 14, 53, - 53, 27, 24, 32, 57, 32, 17, 42, - 37, 29, 33, 1, 25, 32, 32, 63, - 26, 40, 44, 36, 31, 39, 20, 20, - 44, 23, 33, 34, 35, 33, 33, 28, - 41, 23, 41, 41, 29, 25, 26, 49, - 29, 24, 37, 49, 50, 51, 51, 26, - 39, 25, 26, 15, 39, 18, 42, 17, - 4, 31, 32, 32, 60, 1, 42, 32, - 0, 12, 19, 35, 21, 41, 17, 26, - 20, 45, 46, 32, 37, 22, 47, 29, - 31, 27, 29, 30, 21, 33, 35, 18, - 25, 33, 50, 51, 42, 2, 15, 51, - 53, 33, 25, 29, 55, 37, 38, 33, - 38, 59, 38, 33, 39, 13, 32, 40, - 61, 61, 32, 9, 44, 3, 31, 29, - 25, 31, 27, 23, 9, 25, 9, 29, - 20, 30, 30, 42, 18, 28, 25, 28, - 28, 21, 29, 43, 29, 43, 26, 44, - 44, 21, 38, 21, 24, 45, 45, 35, - 39, 22, 35, 36, 34, 34, 45, 34, - 29, 31, 46, 25, 46, 16, 17, 31, - 20, 32, 47, 47, 47, 32, 49, 49, - 49, 31, 1, 27, 28, 39, 39, 21, - 36, 23, 51, 2, 40, 51, 32, 53, - 24, 30, 24, 30, 21, 40, 57, 57, - 31, 41, 58, 32, 12, 4, 32, 34, - 59, 31, 32, 13, 9, 35, 26, 35, - 37, 61, 37, 63, 26, 29, 41, 38, - 23, 20, 41, 26, 41, 42, 42, 42, - 26, 26, 26, 26, 1, 26, 37, 37, - 37, 23, 34, 42, 27, 43, 34, 27, - 31, 24, 33, 16, 3, 31, 24, 33, - 24, 4, 44, 44, 11, 44, 31, 13, - 13, 44, 45, 13, 25, 22, 38, 26, - 38, 38, 39, 32, 30, 39, 30, 22, - 32, 26, 30, 47, 47, 47, 19, 47, - 30, 31, 35, 8, 23, 47, 47, 27, - 35, 47, 31, 48, 35, 19, 36, 49, - 49, 33, 31, 39, 27, 39, 49, 49, - 50, 50, 50, 39, 31, 51, 51, 39, - 28, 33, 33, 21, 40, 31, 52, 53, - 40, 53, 9, 33, 31, 53, 54, 54, - 54, 55, 55, 34, 15, 56, 25, 56, - 21, 21, 40, 40, 25, 40, 58, 36, - 5, 41, 41, 12, 60, 41, 41, 37, - 22, 61, 18, 29, 29, 30, 61, 30, - 61, 62, 62, 30, 30, 63, 18, 13, - 30, 23, 19, 20, 20, 41, 13, 2, - 5, 5, 1, 5, 32, 6, 32, 35, - 20, 35, 27, 35, 35, 36, 36, 13, - 36, 41, 41, 41, 3, 30, 42, 27, - 20, 30, 27, 28, 30, 21, 33, 33, - 14, 24, 30, 42, 24, 33, 25, 42, - 43, 14, 43, 43, 14, 43, 7, 36, - 37, 37, 37, 37, 7, 14, 25, 43, - 43, 44, 15, 37, 7, 7, 3, 1, - 8, 15, 15, 8, 44, 44, 44, 45, - 45, 45, 45, 8, 8, 45, 21, 45, - 28, 28, 28, 21, 28, 28, 22, 37, - 46, 46, 37, 8, 29, 37, 29, 22, - 46, 37, 22, 29, 47, 47, 38, 38, - 16, 38, 38, 33, 38, 22, 47, 47, - 29, 25, 16, 0, 48, 1, 34, 48, - 48, 34, 25, 26, 26, 49, 49, 26, - 1, 49, 4, 26, 4, 49, 1, 9, - 49, 49, 49, 10, 49, 17, 38, 17, - 17, 50, 38, 50, 50, 22, 38, 51, - 38, 38, 51, 39, 39, 18, 22, 39, - 51, 22, 52, 52, 52, 39, 53, 53, - 10, 23, 18, 29, 10, 53, 29, 54, - 11, 54, 11, 11, 55, 1, 18, 55, - 55, 55, 55, 55, 55, 29, 34, 18, - 29, 56, 56, 34, 57, 34, 34, 29, - 29, 57, 57, 35, 35, 35, 35, 35, - 39, 35, 59, 59, 18, 59, 39, 30, - 18, 40, 60, 60, 61, 30, 18, 61, - 61, 19, 19, -}; - -static const uint8_t table0_mvy[1099] = { - 32, 31, 32, 33, 32, 31, 31, 33, - 33, 34, 32, 30, 32, 35, 34, 31, - 32, 29, 33, 30, 32, 34, 33, 31, - 30, 35, 31, 31, 29, 33, 35, 30, - 29, 33, 34, 34, 30, 32, 32, 36, - 29, 32, 35, 32, 28, 32, 32, 27, - 35, 37, 34, 29, 30, 36, 35, 34, - 25, 30, 29, 35, 33, 31, 31, 32, - 31, 28, 39, 28, 29, 37, 31, 33, - 27, 36, 28, 36, 37, 33, 33, 31, - 27, 32, 31, 38, 26, 25, 25, 33, - 39, 31, 34, 30, 32, 32, 32, 34, - 36, 32, 28, 33, 30, 38, 37, 27, - 33, 28, 32, 37, 35, 38, 29, 34, - 27, 29, 29, 32, 32, 34, 35, 3, - 26, 36, 31, 38, 30, 26, 35, 34, - 37, 26, 25, 32, 32, 39, 23, 37, - 32, 32, 29, 32, 29, 36, 29, 30, - 41, 31, 30, 21, 39, 25, 34, 38, - 32, 35, 39, 32, 33, 33, 32, 27, - 29, 25, 28, 27, 26, 31, 30, 35, - 24, 24, 31, 34, 32, 30, 35, 40, - 28, 38, 5, 35, 29, 36, 36, 32, - 38, 30, 33, 31, 35, 26, 23, 38, - 32, 41, 28, 25, 37, 40, 37, 39, - 32, 36, 33, 39, 25, 26, 28, 31, - 28, 42, 23, 31, 33, 31, 39, 1, - 59, 22, 27, 4, 33, 34, 33, 24, - 41, 3, 35, 41, 41, 28, 36, 36, - 28, 33, 35, 21, 23, 21, 22, 37, - 27, 27, 43, 29, 60, 39, 27, 25, - 59, 34, 27, 27, 26, 40, 37, 27, - 61, 26, 39, 33, 31, 22, 37, 25, - 30, 25, 24, 61, 31, 34, 25, 38, - 32, 32, 30, 3, 61, 43, 29, 23, - 28, 32, 28, 32, 31, 34, 5, 33, - 32, 33, 33, 42, 37, 23, 38, 31, - 40, 26, 32, 26, 37, 38, 36, 24, - 29, 30, 20, 22, 29, 24, 32, 41, - 2, 34, 25, 33, 29, 31, 39, 35, - 36, 24, 32, 30, 33, 27, 44, 60, - 30, 36, 19, 34, 31, 24, 16, 35, - 32, 38, 21, 33, 31, 31, 21, 35, - 5, 17, 29, 38, 38, 18, 58, 19, - 43, 41, 30, 41, 43, 39, 29, 7, - 29, 17, 28, 19, 28, 31, 25, 19, - 40, 26, 21, 33, 39, 23, 40, 30, - 39, 34, 35, 32, 32, 24, 33, 30, - 40, 47, 39, 37, 32, 33, 24, 23, - 45, 47, 27, 23, 42, 32, 32, 33, - 36, 37, 37, 17, 18, 22, 40, 38, - 32, 31, 35, 24, 17, 25, 17, 23, - 33, 34, 51, 42, 31, 36, 36, 29, - 21, 22, 37, 44, 43, 25, 47, 33, - 45, 27, 31, 58, 31, 32, 31, 38, - 43, 20, 47, 45, 54, 1, 26, 34, - 38, 14, 22, 24, 33, 34, 32, 32, - 37, 21, 23, 49, 35, 23, 28, 39, - 39, 23, 55, 33, 30, 30, 63, 16, - 42, 28, 13, 33, 33, 35, 19, 46, - 43, 17, 19, 36, 39, 24, 31, 32, - 33, 26, 28, 62, 33, 63, 33, 39, - 19, 49, 17, 31, 43, 13, 15, 29, - 25, 35, 33, 23, 49, 41, 28, 29, - 34, 38, 7, 61, 11, 50, 13, 41, - 19, 47, 25, 26, 15, 42, 41, 29, - 45, 27, 17, 35, 32, 29, 32, 24, - 13, 26, 26, 31, 24, 33, 28, 30, - 31, 11, 45, 46, 33, 33, 35, 57, - 32, 32, 35, 45, 34, 11, 37, 42, - 39, 37, 31, 49, 21, 27, 29, 47, - 53, 40, 51, 16, 26, 1, 40, 30, - 41, 44, 34, 25, 27, 31, 35, 35, - 31, 15, 49, 1, 35, 40, 5, 58, - 21, 29, 22, 59, 45, 31, 9, 26, - 9, 29, 11, 32, 30, 3, 13, 20, - 18, 20, 11, 3, 29, 40, 31, 53, - 30, 17, 20, 37, 31, 42, 47, 47, - 54, 38, 9, 34, 13, 37, 21, 25, - 27, 43, 42, 45, 40, 25, 27, 46, - 22, 25, 53, 20, 2, 14, 39, 15, - 22, 44, 34, 21, 38, 33, 27, 48, - 34, 52, 35, 47, 49, 54, 2, 13, - 23, 52, 29, 45, 22, 49, 54, 21, - 40, 42, 31, 30, 29, 34, 0, 25, - 23, 51, 24, 59, 28, 38, 29, 31, - 2, 13, 31, 8, 31, 33, 12, 45, - 41, 7, 14, 30, 25, 18, 43, 20, - 43, 35, 44, 1, 49, 42, 42, 18, - 41, 38, 41, 44, 53, 11, 20, 25, - 45, 46, 47, 48, 39, 52, 46, 49, - 63, 55, 44, 38, 13, 13, 57, 22, - 51, 16, 12, 28, 35, 57, 25, 20, - 26, 28, 28, 29, 32, 31, 62, 34, - 35, 35, 19, 49, 48, 39, 40, 18, - 43, 46, 11, 6, 48, 19, 49, 41, - 10, 23, 58, 17, 21, 23, 34, 30, - 60, 0, 44, 34, 26, 37, 46, 43, - 49, 59, 4, 34, 59, 37, 22, 25, - 28, 46, 6, 40, 59, 42, 36, 61, - 28, 30, 31, 43, 10, 22, 23, 47, - 20, 52, 55, 36, 25, 16, 1, 11, - 27, 29, 5, 63, 18, 41, 31, 34, - 38, 1, 5, 13, 28, 31, 17, 38, - 39, 41, 36, 37, 22, 39, 33, 43, - 43, 15, 17, 49, 30, 21, 22, 20, - 10, 17, 25, 54, 57, 3, 34, 8, - 36, 25, 31, 14, 15, 19, 29, 25, - 18, 39, 53, 22, 27, 20, 29, 33, - 41, 42, 35, 62, 50, 29, 53, 50, - 35, 55, 42, 61, 63, 4, 7, 42, - 21, 46, 47, 49, 27, 46, 17, 55, - 41, 50, 63, 4, 56, 18, 8, 10, - 18, 51, 63, 36, 55, 18, 5, 55, - 9, 29, 17, 21, 30, 27, 1, 59, - 7, 11, 12, 15, 5, 42, 24, 41, - 43, 7, 27, 22, 25, 31, 30, 37, - 22, 39, 53, 29, 36, 37, 48, 0, - 5, 13, 17, 31, 32, 26, 46, 28, - 44, 45, 46, 53, 49, 51, 3, 41, - 3, 22, 42, 33, 5, 45, 7, 22, - 40, 53, 24, 14, 25, 27, 10, 12, - 34, 16, 17, 53, 20, 26, 39, 45, - 18, 45, 35, 33, 31, 49, 4, 39, - 42, 11, 51, 5, 13, 26, 27, 17, - 52, 30, 0, 22, 12, 34, 62, 36, - 38, 41, 47, 30, 63, 38, 41, 43, - 59, 33, 45, 37, 38, 40, 47, 24, - 48, 49, 30, 1, 10, 22, 49, 15, - 39, 59, 31, 32, 33, 18, 13, 15, - 31, 21, 27, 44, 42, 39, 46, 17, - 26, 32, 30, 31, 0, 30, 34, 9, - 12, 13, 25, 31, 32, 55, 43, 35, - 61, 33, 35, 46, 25, 47, 48, 62, - 63, 38, 61, 1, 2, 5, 7, 9, - 46, 10, 34, 35, 36, 55, 51, 7, - 40, 23, 34, 37, 5, 13, 42, 18, - 25, 27, 28, -}; - -/* motion vector table 1 */ -static const uint16_t table1_mv_code[1100] = { - 0x0000, 0x0007, 0x0009, 0x000f, 0x000a, 0x0011, 0x001a, 0x001c, - 0x0011, 0x0031, 0x0025, 0x002d, 0x002f, 0x006f, 0x0075, 0x0041, - 0x004c, 0x004e, 0x005c, 0x0060, 0x0062, 0x0066, 0x0068, 0x0069, - 0x006b, 0x00a6, 0x00c1, 0x00cb, 0x00cc, 0x00ce, 0x00da, 0x00e8, - 0x00ee, 0x0087, 0x0090, 0x009e, 0x009f, 0x00ba, 0x00ca, 0x00d8, - 0x00db, 0x00df, 0x0104, 0x0109, 0x010c, 0x0143, 0x0145, 0x014a, - 0x0156, 0x015c, 0x01b3, 0x01d3, 0x01da, 0x0103, 0x0109, 0x010b, - 0x0122, 0x0127, 0x0134, 0x0161, 0x0164, 0x0176, 0x0184, 0x018d, - 0x018e, 0x018f, 0x0190, 0x0193, 0x0196, 0x019d, 0x019e, 0x019f, - 0x01a9, 0x01b2, 0x01b4, 0x01ba, 0x01bb, 0x01bc, 0x0201, 0x0202, - 0x0205, 0x0207, 0x020d, 0x0210, 0x0211, 0x0215, 0x021b, 0x021f, - 0x0281, 0x0285, 0x0290, 0x029c, 0x029d, 0x02a2, 0x02a7, 0x02a8, - 0x02aa, 0x02b0, 0x02b1, 0x02b4, 0x02bc, 0x02bf, 0x0320, 0x0326, - 0x0327, 0x0329, 0x032a, 0x0336, 0x0360, 0x0362, 0x0363, 0x0372, - 0x03b2, 0x03bc, 0x03bd, 0x0203, 0x0205, 0x021a, 0x0249, 0x024a, - 0x024c, 0x02c7, 0x02ca, 0x02ce, 0x02ef, 0x030d, 0x0322, 0x0325, - 0x0338, 0x0373, 0x037a, 0x0409, 0x0415, 0x0416, 0x0418, 0x0428, - 0x042d, 0x042f, 0x0434, 0x0508, 0x0509, 0x0510, 0x0511, 0x051c, - 0x051e, 0x0524, 0x0541, 0x0543, 0x0546, 0x0547, 0x054d, 0x0557, - 0x055f, 0x056a, 0x056c, 0x056d, 0x056f, 0x0576, 0x0577, 0x057a, - 0x057b, 0x057c, 0x057d, 0x0600, 0x0601, 0x0603, 0x0614, 0x0616, - 0x0617, 0x061c, 0x061f, 0x0642, 0x0648, 0x0649, 0x064a, 0x064b, - 0x0657, 0x0668, 0x0669, 0x066b, 0x066e, 0x067f, 0x06c2, 0x06c8, - 0x06cb, 0x06de, 0x06df, 0x06e2, 0x06e3, 0x06ef, 0x0748, 0x074b, - 0x076e, 0x076f, 0x077c, 0x0409, 0x0423, 0x0428, 0x0429, 0x042a, - 0x042b, 0x0432, 0x0433, 0x0496, 0x049a, 0x04d5, 0x04db, 0x0581, - 0x0582, 0x058b, 0x058c, 0x058d, 0x0598, 0x0599, 0x059a, 0x059e, - 0x05dd, 0x0619, 0x0632, 0x0633, 0x0648, 0x0672, 0x06a1, 0x06a2, - 0x06a3, 0x06af, 0x06e2, 0x06e3, 0x06e4, 0x0800, 0x0801, 0x0802, - 0x0803, 0x081a, 0x081b, 0x0829, 0x082f, 0x0832, 0x083e, 0x083f, - 0x0852, 0x0853, 0x0858, 0x086b, 0x0877, 0x0878, 0x0879, 0x087a, - 0x087b, 0x0a00, 0x0a01, 0x0a0d, 0x0a0e, 0x0a0f, 0x0a24, 0x0a37, - 0x0a3a, 0x0a3b, 0x0a3e, 0x0a46, 0x0a47, 0x0a4a, 0x0a4b, 0x0a5f, - 0x0a79, 0x0a7a, 0x0a7b, 0x0a80, 0x0a81, 0x0a84, 0x0a85, 0x0a99, - 0x0aa5, 0x0aa6, 0x0ab8, 0x0aba, 0x0abb, 0x0abc, 0x0abd, 0x0ac8, - 0x0ace, 0x0acf, 0x0ad7, 0x0adc, 0x0aeb, 0x0c04, 0x0c25, 0x0c26, - 0x0c27, 0x0c2a, 0x0c2b, 0x0c3a, 0x0c3b, 0x0c3c, 0x0c3d, 0x0ca0, - 0x0cad, 0x0cd4, 0x0cd5, 0x0cfc, 0x0cfd, 0x0d86, 0x0d92, 0x0d93, - 0x0d94, 0x0d95, 0x0db0, 0x0db8, 0x0db9, 0x0dba, 0x0dbb, 0x0dc0, - 0x0dc2, 0x0dc3, 0x0dda, 0x0ddb, 0x0ddc, 0x0ddd, 0x0e92, 0x0e93, - 0x0e94, 0x0e95, 0x0ec7, 0x0ecc, 0x0ece, 0x0ecf, 0x0ed8, 0x0ed9, - 0x0eda, 0x0edb, 0x0808, 0x0809, 0x080a, 0x0810, 0x0811, 0x0844, - 0x0845, 0x0861, 0x0862, 0x0863, 0x086c, 0x0922, 0x0923, 0x092e, - 0x092f, 0x0936, 0x0937, 0x09b1, 0x09b2, 0x09b3, 0x09b4, 0x09b5, - 0x09b8, 0x09b9, 0x09ba, 0x09bb, 0x09bc, 0x09bd, 0x09be, 0x09bf, - 0x0b00, 0x0b15, 0x0b2c, 0x0b2d, 0x0b2e, 0x0b2f, 0x0b36, 0x0bb9, - 0x0c28, 0x0c2a, 0x0c2b, 0x0c2c, 0x0c2d, 0x0c2e, 0x0c2f, 0x0c30, - 0x0c31, 0x0c38, 0x0c60, 0x0c61, 0x0c62, 0x0c63, 0x0c8d, 0x0c8e, - 0x0c8f, 0x0c92, 0x0cbe, 0x0cbf, 0x0ce6, 0x0ce7, 0x0d40, 0x0d41, - 0x0d57, 0x0d58, 0x0d59, 0x0d5a, 0x0d5b, 0x0d5c, 0x0d5d, 0x0d98, - 0x0d99, 0x0d9a, 0x0d9b, 0x0d9c, 0x0d9d, 0x0dad, 0x0dae, 0x0daf, - 0x0dc0, 0x0dc1, 0x0dc2, 0x0dc3, 0x0dca, 0x0dcb, 0x0dec, 0x0ded, - 0x0dee, 0x0def, 0x1018, 0x1022, 0x1023, 0x1030, 0x1031, 0x1032, - 0x1033, 0x1050, 0x1051, 0x105c, 0x1074, 0x1075, 0x1076, 0x1077, - 0x1078, 0x1079, 0x107a, 0x107b, 0x10b2, 0x10b3, 0x10b8, 0x10b9, - 0x10ba, 0x10bb, 0x10d4, 0x10ea, 0x10eb, 0x10ec, 0x10ed, 0x1404, - 0x1405, 0x1406, 0x1407, 0x1410, 0x1411, 0x1412, 0x1413, 0x1414, - 0x1415, 0x1416, 0x1417, 0x1418, 0x1419, 0x1466, 0x1467, 0x1468, - 0x1469, 0x146a, 0x146b, 0x146c, 0x146d, 0x147e, 0x147f, 0x1488, - 0x1489, 0x148a, 0x148b, 0x14b6, 0x14b7, 0x14b8, 0x14b9, 0x14ba, - 0x14bb, 0x14bc, 0x14bd, 0x14f0, 0x14f1, 0x14f8, 0x14f9, 0x14fa, - 0x14fb, 0x14fc, 0x14fd, 0x14fe, 0x14ff, 0x152a, 0x152b, 0x152c, - 0x152d, 0x152e, 0x152f, 0x1530, 0x1531, 0x1548, 0x1549, 0x154e, - 0x154f, 0x1558, 0x1559, 0x155a, 0x155b, 0x1572, 0x159a, 0x159b, - 0x15ac, 0x15ba, 0x15bb, 0x15d0, 0x15d1, 0x15d2, 0x15d3, 0x15d4, - 0x15d5, 0x181d, 0x181e, 0x181f, 0x1840, 0x1841, 0x1842, 0x1843, - 0x1844, 0x1845, 0x1846, 0x1847, 0x1848, 0x1849, 0x1861, 0x1862, - 0x1863, 0x1864, 0x1865, 0x1866, 0x1867, 0x1868, 0x1869, 0x186a, - 0x186b, 0x186c, 0x186d, 0x186e, 0x191b, 0x191c, 0x191d, 0x191e, - 0x191f, 0x1942, 0x1943, 0x1944, 0x1945, 0x1946, 0x1947, 0x1958, - 0x1959, 0x19ed, 0x19ee, 0x19ef, 0x19f0, 0x19f1, 0x19f2, 0x19f3, - 0x19f4, 0x19f5, 0x19f6, 0x19f7, 0x1b0e, 0x1b0f, 0x1b62, 0x1b63, - 0x1b64, 0x1b65, 0x1b66, 0x1b67, 0x1b68, 0x1b69, 0x1b6a, 0x1b6b, - 0x1b6c, 0x1b6d, 0x1b6e, 0x1b6f, 0x1b82, 0x1ba8, 0x1ba9, 0x1baa, - 0x1bab, 0x1bac, 0x1bad, 0x1bae, 0x1baf, 0x1bb0, 0x1bb1, 0x1bb2, - 0x1bb3, 0x1d80, 0x1d81, 0x1d82, 0x1d83, 0x1d84, 0x1d85, 0x1d86, - 0x1d87, 0x1d88, 0x1d89, 0x1d8a, 0x1d8b, 0x1d8c, 0x1d8d, 0x1007, - 0x1008, 0x1009, 0x100a, 0x100b, 0x100c, 0x100d, 0x100e, 0x100f, - 0x1016, 0x1080, 0x1081, 0x1082, 0x1083, 0x1084, 0x1085, 0x1086, - 0x1087, 0x10c0, 0x123a, 0x123b, 0x123c, 0x123d, 0x123e, 0x123f, - 0x1240, 0x1241, 0x1242, 0x1243, 0x1350, 0x1352, 0x1353, 0x1358, - 0x1359, 0x135a, 0x135b, 0x135c, 0x135d, 0x135e, 0x135f, 0x1360, - 0x1361, 0x1602, 0x1603, 0x160c, 0x160d, 0x160e, 0x160f, 0x1620, - 0x1621, 0x1622, 0x1623, 0x1624, 0x1625, 0x1626, 0x1627, 0x1628, - 0x1629, 0x166e, 0x166f, 0x167c, 0x167d, 0x167e, 0x167f, 0x1770, - 0x1771, 0x1852, 0x1853, 0x1872, 0x1873, 0x1874, 0x1875, 0x1876, - 0x1877, 0x1878, 0x1879, 0x187a, 0x187b, 0x187c, 0x187d, 0x187e, - 0x187f, 0x1918, 0x1919, 0x1926, 0x1927, 0x1970, 0x1971, 0x1972, - 0x1973, 0x1974, 0x1975, 0x1976, 0x1977, 0x1978, 0x1979, 0x197a, - 0x197b, 0x1aa0, 0x1aa1, 0x1aa2, 0x1aa3, 0x1aa4, 0x1aa5, 0x1aa6, - 0x1aa7, 0x1aa8, 0x1aa9, 0x1aaa, 0x1aab, 0x1aac, 0x1aad, 0x1b3c, - 0x1b3d, 0x1b3e, 0x1b3f, 0x1b50, 0x1b51, 0x1b52, 0x1b53, 0x1b54, - 0x1b55, 0x1b56, 0x1b57, 0x1b58, 0x1b59, 0x2032, 0x2033, 0x2034, - 0x2035, 0x2036, 0x2037, 0x2038, 0x2039, 0x203a, 0x203b, 0x203c, - 0x203d, 0x203e, 0x203f, 0x2040, 0x2041, 0x2042, 0x2043, 0x20ba, - 0x20bb, 0x20cc, 0x20cd, 0x20ce, 0x20cf, 0x20e0, 0x20e1, 0x20e2, - 0x20e3, 0x20e4, 0x20e5, 0x20e6, 0x20e7, 0x21aa, 0x21ab, 0x21c0, - 0x21c1, 0x21c2, 0x21c3, 0x21c4, 0x21c5, 0x21c6, 0x21c7, 0x21c8, - 0x21c9, 0x21ca, 0x21cb, 0x21cc, 0x21cd, 0x21ce, 0x21cf, 0x21d0, - 0x21d1, 0x21d2, 0x21d3, 0x2894, 0x2895, 0x2896, 0x2897, 0x2898, - 0x2899, 0x289a, 0x289b, 0x289c, 0x289d, 0x289e, 0x289f, 0x28c0, - 0x28c1, 0x28c2, 0x28c3, 0x28c4, 0x28c5, 0x28c6, 0x28c7, 0x28c8, - 0x28c9, 0x28ca, 0x28cb, 0x2930, 0x2931, 0x2932, 0x2933, 0x2934, - 0x2935, 0x2936, 0x2937, 0x2938, 0x2939, 0x293a, 0x293b, 0x293c, - 0x293d, 0x293e, 0x293f, 0x2960, 0x2961, 0x2962, 0x2963, 0x2964, - 0x2965, 0x2966, 0x2967, 0x2968, 0x2969, 0x296a, 0x296b, 0x2a40, - 0x2a41, 0x2a42, 0x2a43, 0x2a44, 0x2a45, 0x2a46, 0x2a47, 0x2a48, - 0x2a49, 0x2a4a, 0x2a4b, 0x2a4c, 0x2a4d, 0x2a4e, 0x2a4f, 0x2a50, - 0x2a51, 0x2a52, 0x2a53, 0x2ae6, 0x2ae7, 0x2b24, 0x2b25, 0x2b26, - 0x2b27, 0x2b28, 0x2b29, 0x2b2a, 0x2b2b, 0x2b2c, 0x2b2d, 0x2b2e, - 0x2b2f, 0x2b30, 0x2b31, 0x2b32, 0x2b33, 0x2b5a, 0x2b5b, 0x3014, - 0x3015, 0x3016, 0x3017, 0x3020, 0x3021, 0x3022, 0x3023, 0x3024, - 0x3025, 0x3026, 0x3027, 0x3028, 0x3029, 0x302a, 0x302b, 0x302c, - 0x302d, 0x302e, 0x302f, 0x3030, 0x3031, 0x3032, 0x3033, 0x3034, - 0x3035, 0x3036, 0x3037, 0x3038, 0x3039, 0x30c0, 0x30c1, 0x30de, - 0x30df, 0x3218, 0x3219, 0x321a, 0x321b, 0x321c, 0x321d, 0x321e, - 0x321f, 0x3220, 0x3221, 0x3222, 0x3223, 0x3224, 0x3225, 0x3226, - 0x3227, 0x3228, 0x3229, 0x322a, 0x322b, 0x322c, 0x322d, 0x322e, - 0x322f, 0x3230, 0x3231, 0x3232, 0x3233, 0x3234, 0x3235, 0x3378, - 0x3379, 0x337a, 0x337b, 0x337c, 0x337d, 0x337e, 0x337f, 0x33c0, - 0x33c1, 0x33c2, 0x33c3, 0x33c4, 0x33c5, 0x33c6, 0x33c7, 0x33c8, - 0x33c9, 0x33ca, 0x33cb, 0x33cc, 0x33cd, 0x33ce, 0x33cf, 0x33d0, - 0x33d1, 0x33d2, 0x33d3, 0x33d4, 0x33d5, 0x33d6, 0x33d7, 0x33d8, - 0x33d9, 0x3706, 0x3707, 0x3730, 0x3731, 0x3732, 0x3733, 0x3734, - 0x3735, 0x3736, 0x3737, 0x3738, 0x3739, 0x373a, 0x373b, 0x373c, - 0x373d, 0x373e, 0x373f, 0x3740, 0x3741, 0x3742, 0x3743, 0x3744, - 0x3745, 0x3746, 0x3747, 0x3748, 0x3749, 0x374a, 0x374b, 0x374c, - 0x374d, 0x374e, 0x374f, 0x3b34, 0x3b35, 0x3b36, 0x3b37, 0x3be8, - 0x3be9, 0x3bea, 0x3beb, 0x3bec, 0x3bed, 0x3bee, 0x3bef, 0x3bf0, - 0x3bf1, 0x3bf2, 0x3bf3, 0x3bf4, 0x3bf5, 0x3bf6, 0x3bf7, 0x3bf8, - 0x3bf9, 0x3bfa, 0x3bfb, 0x3bfc, 0x3bfd, 0x3bfe, 0x3bff, 0x2000, - 0x2001, 0x2002, 0x2003, 0x2004, 0x2005, 0x2006, 0x2007, 0x2008, - 0x2009, 0x200a, 0x200b, 0x200c, 0x200d, 0x202e, 0x202f, 0x2182, - 0x2183, 0x21b4, 0x21b5, 0x21b6, 0x21b7, 0x21b8, 0x21b9, 0x21ba, - 0x21bb, 0x21bc, 0x21bd, 0x21be, 0x21bf, 0x2460, 0x2461, 0x2462, - 0x2463, 0x2464, 0x2465, 0x2466, 0x2467, 0x2468, 0x2469, 0x246a, - 0x246b, 0x246c, 0x246d, 0x246e, 0x246f, 0x2470, 0x2471, 0x2472, - 0x2473, 0x26a2, 0x26a3, 0x000b, -}; - -static const uint8_t table1_mv_bits[1100] = { - 2, 4, 4, 4, 5, 5, 5, 5, - 6, 6, 7, 7, 7, 7, 7, 8, - 8, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, - 8, 9, 9, 9, 9, 9, 9, 9, - 9, 9, 9, 9, 9, 9, 9, 9, - 9, 9, 9, 9, 9, 10, 10, 10, - 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 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, - 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, 11, 11, 11, - 11, 11, 11, 11, 11, 11, 11, 11, - 11, 11, 11, 11, 11, 11, 11, 11, - 11, 11, 11, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 15, - 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 4, -}; - -static const uint8_t table1_mvx[1099] = { - 32, 31, 32, 31, 33, 32, 33, 33, - 31, 34, 30, 32, 32, 34, 35, 32, - 34, 33, 29, 30, 30, 32, 31, 31, - 33, 35, 35, 33, 31, 29, 29, 33, - 34, 30, 31, 28, 36, 30, 34, 32, - 32, 37, 32, 32, 25, 27, 39, 32, - 32, 32, 38, 35, 36, 32, 37, 61, - 26, 32, 34, 35, 3, 35, 27, 28, - 29, 34, 28, 37, 31, 36, 32, 27, - 31, 30, 29, 39, 33, 29, 33, 35, - 25, 25, 29, 33, 31, 31, 31, 33, - 32, 30, 32, 32, 41, 39, 33, 36, - 32, 28, 34, 36, 38, 24, 60, 31, - 23, 28, 32, 33, 59, 32, 40, 30, - 5, 34, 32, 38, 32, 30, 43, 4, - 32, 32, 42, 31, 31, 32, 26, 38, - 26, 22, 21, 37, 61, 63, 37, 31, - 32, 33, 2, 1, 23, 33, 41, 27, - 35, 30, 38, 23, 33, 3, 28, 34, - 34, 27, 41, 29, 39, 35, 36, 29, - 32, 27, 30, 32, 24, 61, 37, 26, - 59, 25, 35, 27, 36, 37, 30, 31, - 34, 40, 3, 28, 34, 39, 32, 31, - 32, 30, 24, 28, 35, 36, 26, 32, - 31, 33, 29, 33, 39, 25, 30, 24, - 35, 59, 29, 34, 25, 30, 21, 35, - 43, 40, 32, 29, 5, 28, 31, 62, - 33, 33, 25, 31, 21, 31, 43, 31, - 34, 33, 20, 40, 39, 31, 31, 57, - 38, 32, 42, 33, 32, 31, 32, 29, - 30, 44, 5, 31, 22, 34, 36, 17, - 38, 58, 38, 35, 32, 60, 35, 24, - 32, 38, 16, 45, 42, 32, 31, 29, - 4, 30, 17, 40, 46, 48, 63, 32, - 42, 19, 41, 22, 28, 36, 45, 33, - 33, 32, 29, 7, 41, 42, 18, 33, - 33, 32, 22, 37, 1, 26, 22, 23, - 49, 28, 26, 27, 32, 33, 27, 23, - 28, 36, 15, 6, 34, 27, 31, 26, - 23, 2, 33, 32, 34, 41, 28, 32, - 41, 0, 36, 38, 34, 31, 47, 32, - 17, 31, 39, 33, 37, 51, 30, 47, - 32, 50, 32, 19, 63, 30, 25, 27, - 33, 62, 24, 31, 27, 30, 37, 31, - 45, 32, 39, 20, 46, 47, 35, 19, - 34, 1, 49, 21, 21, 14, 51, 26, - 23, 31, 36, 35, 58, 29, 29, 21, - 20, 42, 13, 28, 12, 40, 31, 33, - 39, 60, 32, 44, 33, 31, 28, 37, - 29, 32, 30, 49, 43, 28, 39, 25, - 32, 48, 2, 15, 20, 25, 31, 28, - 21, 24, 25, 15, 31, 17, 37, 43, - 18, 32, 33, 24, 33, 36, 13, 33, - 31, 39, 11, 31, 33, 32, 39, 37, - 32, 32, 29, 17, 44, 46, 36, 35, - 26, 37, 58, 32, 34, 38, 8, 38, - 38, 22, 29, 25, 16, 35, 32, 35, - 33, 43, 18, 46, 38, 50, 33, 18, - 53, 60, 13, 32, 36, 33, 51, 36, - 43, 45, 27, 42, 29, 24, 30, 25, - 31, 52, 31, 35, 38, 9, 22, 34, - 4, 17, 28, 55, 42, 25, 17, 20, - 47, 34, 33, 16, 40, 25, 16, 30, - 53, 29, 10, 11, 14, 26, 33, 4, - 35, 44, 26, 16, 31, 26, 34, 38, - 29, 31, 30, 24, 22, 61, 32, 9, - 45, 34, 31, 19, 9, 31, 46, 31, - 35, 54, 29, 57, 30, 50, 3, 31, - 63, 34, 47, 41, 51, 18, 31, 14, - 37, 38, 31, 24, 32, 31, 50, 33, - 31, 54, 27, 9, 33, 23, 19, 32, - 29, 29, 33, 28, 47, 49, 30, 47, - 33, 27, 25, 54, 44, 45, 50, 58, - 51, 48, 33, 59, 33, 34, 57, 13, - 26, 33, 13, 48, 30, 11, 7, 56, - 34, 55, 26, 0, 26, 35, 1, 51, - 33, 53, 31, 45, 12, 29, 29, 51, - 31, 48, 2, 6, 34, 30, 28, 33, - 60, 40, 27, 46, 31, 9, 35, 29, - 31, 39, 55, 46, 19, 37, 62, 34, - 30, 16, 19, 49, 41, 41, 39, 37, - 14, 5, 13, 35, 55, 30, 40, 40, - 42, 8, 20, 25, 45, 35, 33, 36, - 54, 38, 27, 37, 62, 40, 15, 59, - 49, 31, 29, 34, 34, 39, 24, 29, - 25, 29, 21, 29, 10, 61, 33, 49, - 35, 34, 3, 38, 39, 29, 7, 41, - 1, 35, 4, 23, 15, 23, 11, 37, - 28, 35, 30, 30, 24, 1, 43, 56, - 8, 34, 42, 24, 45, 30, 20, 23, - 8, 38, 22, 33, 17, 52, 34, 22, - 53, 43, 44, 1, 27, 31, 41, 43, - 41, 30, 31, 36, 30, 5, 55, 31, - 33, 30, 40, 23, 15, 29, 34, 34, - 59, 34, 30, 11, 13, 38, 5, 0, - 30, 42, 5, 30, 29, 34, 10, 44, - 30, 63, 35, 12, 3, 26, 15, 17, - 25, 34, 43, 39, 34, 56, 29, 23, - 30, 12, 30, 10, 35, 9, 24, 58, - 10, 12, 54, 33, 37, 20, 41, 35, - 29, 18, 61, 30, 40, 24, 39, 53, - 62, 26, 29, 33, 34, 53, 49, 21, - 27, 11, 63, 20, 26, 23, 7, 13, - 6, 47, 29, 30, 9, 51, 22, 34, - 21, 25, 33, 56, 57, 30, 38, 51, - 51, 38, 63, 28, 40, 35, 33, 18, - 33, 33, 24, 58, 58, 34, 49, 29, - 43, 4, 1, 4, 42, 35, 35, 30, - 17, 5, 56, 61, 25, 37, 36, 55, - 28, 35, 29, 50, 48, 52, 2, 42, - 34, 40, 46, 46, 43, 35, 29, 48, - 20, 29, 31, 41, 7, 30, 35, 19, - 14, 21, 8, 39, 39, 40, 46, 55, - 34, 6, 30, 34, 37, 25, 37, 33, - 22, 44, 52, 17, 35, 29, 36, 35, - 40, 37, 28, 30, 50, 14, 28, 55, - 6, 23, 19, 14, 30, 3, 30, 28, - 28, 61, 61, 47, 45, 48, 40, 40, - 34, 34, 25, 30, 29, 35, 4, 26, - 53, 50, 26, 41, 27, 59, 27, 38, - 39, 3, 50, 43, 47, 23, 33, 55, - 35, 21, 23, 35, 61, 33, 46, 52, - 35, 34, 24, 30, 43, 16, 37, 21, - 2, 24, 45, 34, 30, 55, 55, 1, - 29, 29, 26, 28, 25, 31, 36, 22, - 17, 30, 52, 2, 44, 44, 57, 26, - 62, 41, 39, 57, 26, 46, 49, 11, - 16, 19, 5, 59, 38, 39, 58, 38, - 25, 49, 50, 22, 28, 59, 9, 59, - 7, 28, 55, 17, 4, 35, 50, 21, - 29, 44, 47, 18, 24, 19, 25, 42, - 35, 3, 51, 35, 16, 35, 30, 63, - 57, 39, 39, 25, 35, 38, 9, 16, - 36, 45, 31, 60, 14, 34, 42, 24, - 0, 37, 18, 61, 57, 37, 28, 53, - 20, 46, 14, 47, 38, 38, 38, 9, - 34, 39, 43, 17, 39, 59, 5, 27, - 0, 12, 27, -}; - -static const uint8_t table1_mvy[1099] = { - 32, 32, 31, 31, 32, 33, 31, 33, - 33, 32, 32, 30, 34, 31, 32, 29, - 33, 30, 32, 33, 31, 35, 34, 30, - 34, 31, 33, 29, 29, 31, 33, 35, - 30, 30, 35, 32, 32, 34, 34, 28, - 25, 32, 36, 27, 32, 32, 32, 37, - 39, 3, 32, 30, 31, 26, 31, 32, - 32, 38, 29, 29, 32, 34, 31, 31, - 34, 35, 33, 33, 28, 33, 1, 33, - 27, 29, 30, 31, 28, 29, 37, 35, - 31, 33, 35, 27, 36, 37, 25, 25, - 61, 35, 4, 5, 32, 33, 36, 30, - 23, 30, 28, 34, 31, 32, 32, 39, - 32, 34, 21, 39, 32, 59, 32, 28, - 32, 36, 60, 33, 24, 36, 32, 32, - 41, 2, 32, 38, 26, 22, 33, 30, - 31, 32, 32, 30, 31, 32, 29, 3, - 40, 38, 32, 32, 33, 26, 31, 34, - 28, 38, 34, 31, 3, 31, 35, 38, - 27, 35, 33, 28, 29, 27, 29, 27, - 43, 29, 37, 63, 31, 33, 34, 30, - 31, 30, 37, 30, 35, 35, 26, 41, - 37, 31, 33, 28, 26, 30, 42, 24, - 7, 27, 33, 29, 36, 28, 34, 57, - 23, 41, 36, 23, 35, 34, 25, 30, - 25, 33, 25, 25, 29, 24, 33, 39, - 33, 33, 0, 37, 31, 36, 21, 32, - 61, 24, 35, 61, 31, 5, 31, 59, - 39, 21, 32, 30, 34, 22, 40, 32, - 29, 16, 31, 5, 62, 2, 20, 39, - 39, 32, 33, 1, 31, 24, 36, 32, - 36, 32, 28, 26, 6, 31, 38, 34, - 58, 35, 32, 33, 33, 17, 43, 26, - 31, 40, 31, 34, 32, 32, 31, 19, - 30, 32, 29, 33, 38, 38, 32, 59, - 40, 18, 38, 32, 35, 34, 32, 17, - 1, 15, 30, 28, 31, 28, 34, 29, - 32, 27, 35, 27, 49, 22, 37, 34, - 37, 26, 32, 32, 22, 28, 45, 29, - 30, 31, 43, 46, 41, 30, 26, 13, - 34, 32, 27, 38, 42, 42, 33, 47, - 33, 60, 27, 42, 25, 32, 22, 32, - 48, 32, 45, 33, 33, 41, 27, 25, - 19, 31, 35, 19, 36, 42, 27, 17, - 31, 44, 28, 33, 33, 31, 23, 31, - 40, 33, 31, 34, 30, 32, 33, 36, - 35, 47, 37, 41, 31, 23, 41, 29, - 30, 35, 32, 25, 32, 28, 58, 2, - 37, 33, 14, 33, 49, 20, 39, 36, - 21, 9, 23, 33, 35, 24, 39, 37, - 11, 33, 30, 31, 31, 28, 51, 40, - 35, 29, 25, 33, 46, 35, 37, 30, - 30, 8, 63, 28, 15, 40, 33, 45, - 49, 25, 32, 4, 47, 51, 36, 39, - 53, 10, 24, 29, 30, 31, 25, 40, - 38, 38, 33, 56, 23, 27, 32, 37, - 26, 29, 43, 36, 33, 24, 55, 43, - 9, 29, 34, 34, 24, 33, 18, 33, - 33, 30, 31, 50, 24, 60, 30, 39, - 34, 30, 39, 28, 22, 38, 2, 26, - 63, 32, 57, 21, 39, 33, 28, 18, - 30, 34, 22, 33, 29, 41, 30, 34, - 35, 21, 13, 34, 35, 39, 30, 46, - 32, 42, 32, 31, 33, 26, 11, 33, - 22, 31, 25, 31, 53, 27, 43, 25, - 40, 50, 21, 36, 38, 30, 12, 31, - 34, 20, 15, 29, 32, 62, 30, 13, - 17, 32, 19, 31, 20, 31, 30, 7, - 1, 17, 34, 37, 31, 31, 44, 34, - 26, 40, 16, 37, 52, 48, 30, 20, - 18, 33, 38, 29, 7, 25, 30, 54, - 45, 47, 46, 41, 29, 29, 16, 30, - 14, 26, 38, 34, 34, 29, 34, 30, - 29, 30, 57, 30, 4, 46, 33, 29, - 39, 44, 30, 31, 50, 33, 31, 32, - 19, 32, 40, 31, 37, 47, 1, 35, - 16, 31, 0, 35, 33, 1, 17, 34, - 9, 34, 33, 31, 49, 43, 42, 51, - 34, 29, 23, 29, 14, 30, 45, 49, - 11, 24, 31, 28, 35, 41, 30, 44, - 18, 29, 34, 35, 36, 25, 26, 21, - 31, 30, 34, 19, 34, 44, 36, 38, - 25, 31, 28, 23, 37, 3, 55, 41, - 30, 22, 41, 24, 33, 26, 35, 35, - 30, 55, 51, 47, 48, 38, 24, 15, - 21, 50, 25, 46, 30, 29, 10, 34, - 42, 45, 29, 42, 22, 3, 33, 27, - 34, 1, 34, 28, 34, 36, 35, 23, - 23, 13, 58, 3, 26, 63, 25, 31, - 34, 61, 38, 39, 25, 61, 29, 37, - 30, 41, 26, 48, 28, 33, 50, 35, - 30, 37, 29, 29, 40, 6, 39, 28, - 28, 19, 8, 22, 45, 34, 35, 10, - 58, 17, 37, 39, 30, 18, 54, 14, - 29, 16, 59, 30, 35, 23, 35, 30, - 47, 36, 29, 55, 20, 12, 31, 35, - 14, 29, 18, 34, 34, 24, 29, 26, - 22, 2, 27, 23, 8, 30, 55, 38, - 60, 31, 4, 34, 49, 34, 27, 34, - 33, 30, 31, 54, 42, 35, 38, 46, - 44, 26, 27, 9, 39, 25, 21, 29, - 28, 42, 13, 0, 5, 34, 37, 28, - 24, 29, 63, 26, 22, 27, 29, 25, - 33, 25, 61, 0, 35, 25, 36, 15, - 27, 40, 53, 33, 3, 10, 16, 37, - 38, 18, 30, 46, 27, 9, 6, 29, - 62, 8, 42, 28, 29, 3, 25, 16, - 26, 29, 35, 28, 27, 51, 61, 48, - 37, 9, 34, 7, 49, 45, 20, 29, - 21, 5, 5, 29, 28, 34, 29, 24, - 10, 24, 35, 36, 38, 55, 11, 36, - 38, 53, 54, 26, 30, 49, 20, 27, - 30, 39, 33, 41, 49, 22, 38, 38, - 4, 30, 8, 9, 3, 24, 22, 50, - 37, 36, 31, 27, 2, 9, 42, 63, - 25, 19, 44, 1, 28, 28, 48, 30, - 34, 41, 41, 38, 12, 27, 15, 0, - 16, 34, 35, 38, 28, 29, 40, 42, - 51, 52, 45, 54, 59, 59, 42, 44, - 37, 26, 46, 24, 15, 39, 22, 46, - 19, 35, 38, 17, 37, 23, 52, 55, - 50, 37, 26, 11, 37, 12, 24, 30, - 16, 13, 22, 13, 36, 35, 40, 41, - 34, 41, 26, 53, 51, 5, 21, 30, - 2, 63, 41, 20, 1, 56, 21, 24, - 25, 5, 28, 35, 26, 28, 30, 18, - 29, 23, 40, 34, 20, 42, 39, 34, - 28, 61, 38, 27, 62, 9, 36, 17, - 9, 49, 24, 25, 54, 34, 39, 37, - 3, 1, 25, 38, 38, 44, 35, 36, - 12, 60, 36, 38, 40, 25, 43, 39, - 53, 28, 39, 57, 46, 10, 52, 27, - 35, 42, 45, 59, 15, 60, 38, 24, - 23, 39, 12, 29, 24, 0, 20, 16, - 28, 43, 35, 28, 1, 49, 4, 21, - 42, 39, 29, 3, 44, 21, 53, 55, - 11, 5, 3, 39, 53, 28, 25, 19, - 34, 28, 21, -}; +#include "common.h" +#include "bitstream.h" +#include "rl.h" /* motion vector table */ typedef struct MVTable { @@ -1773,232 +45,55 @@ typedef struct MVTable { VLC vlc; /* decoding: vlc */ } MVTable; -static MVTable mv_tables[2] = { - { - 1099, - table0_mv_code, - table0_mv_bits, - table0_mvx, - table0_mvy, - }, - { - 1099, - table1_mv_code, - table1_mv_bits, - table1_mvx, - table1_mvy, - } -}; +extern VLC ff_msmp4_mb_i_vlc; +extern VLC ff_msmp4_dc_luma_vlc[2]; +extern VLC ff_msmp4_dc_chroma_vlc[2]; -static const uint8_t v2_mb_type[8][2] = { - {1, 1}, {0 , 2}, {3 , 3}, {9 , 5}, - {5, 4}, {0x21, 7}, {0x20, 7}, {0x11, 6}, -}; +/* intra picture macro block coded block pattern */ +extern const uint16_t ff_msmp4_mb_i_table[64][2]; -static const uint8_t v2_intra_cbpc[4][2] = { - {1, 1}, {0, 3}, {1, 3}, {1, 2}, -}; +extern const uint8_t cbpy_tab[16][2]; -static uint8_t wmv1_y_dc_scale_table[32]={ -// 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 - 0, 8, 8, 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 -}; -static uint8_t wmv1_c_dc_scale_table[32]={ -// 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 - 0, 8, 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,22 -}; +extern const uint8_t DCtab_lum[13][2]; +extern const uint8_t DCtab_chrom[13][2]; -static uint8_t old_ff_y_dc_scale_table[32]={ -// 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 - 0, 8, 8, 8, 8,10,12,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39 -}; -static uint8_t old_ff_c_dc_scale_table[32]={ -// 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 - 0, 8, 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,22 -}; +extern const uint8_t mvtab[33][2]; +extern const uint8_t intra_MCBPC_code[9]; +extern const uint8_t intra_MCBPC_bits[9]; + +extern const uint8_t inter_MCBPC_code[28]; +extern const uint8_t inter_MCBPC_bits[28]; #define WMV1_SCANTABLE_COUNT 4 -static const uint8_t wmv1_scantable00[64]= { -0x00, 0x08, 0x01, 0x02, 0x09, 0x10, 0x18, 0x11, -0x0A, 0x03, 0x04, 0x0B, 0x12, 0x19, 0x20, 0x28, -0x30, 0x38, 0x29, 0x21, 0x1A, 0x13, 0x0C, 0x05, -0x06, 0x0D, 0x14, 0x1B, 0x22, 0x31, 0x39, 0x3A, -0x32, 0x2A, 0x23, 0x1C, 0x15, 0x0E, 0x07, 0x0F, -0x16, 0x1D, 0x24, 0x2B, 0x33, 0x3B, 0x3C, 0x34, -0x2C, 0x25, 0x1E, 0x17, 0x1F, 0x26, 0x2D, 0x35, -0x3D, 0x3E, 0x36, 0x2E, 0x27, 0x2F, 0x37, 0x3F, -}; -static const uint8_t wmv1_scantable01[64]= { -0x00, 0x08, 0x01, 0x02, 0x09, 0x10, 0x18, 0x11, -0x0A, 0x03, 0x04, 0x0B, 0x12, 0x19, 0x20, 0x28, -0x21, 0x30, 0x1A, 0x13, 0x0C, 0x05, 0x06, 0x0D, -0x14, 0x1B, 0x22, 0x29, 0x38, 0x31, 0x39, 0x2A, -0x23, 0x1C, 0x15, 0x0E, 0x07, 0x0F, 0x16, 0x1D, -0x24, 0x2B, 0x32, 0x3A, 0x33, 0x3B, 0x2C, 0x25, -0x1E, 0x17, 0x1F, 0x26, 0x2D, 0x34, 0x3C, 0x35, -0x3D, 0x2E, 0x27, 0x2F, 0x36, 0x3E, 0x37, 0x3F, -}; -static const uint8_t wmv1_scantable02[64]= { -0x00, 0x01, 0x08, 0x02, 0x03, 0x09, 0x10, 0x18, -0x11, 0x0A, 0x04, 0x05, 0x0B, 0x12, 0x19, 0x20, -0x28, 0x30, 0x21, 0x1A, 0x13, 0x0C, 0x06, 0x07, -0x0D, 0x14, 0x1B, 0x22, 0x29, 0x38, 0x31, 0x39, -0x2A, 0x23, 0x1C, 0x15, 0x0E, 0x0F, 0x16, 0x1D, -0x24, 0x2B, 0x32, 0x3A, 0x33, 0x2C, 0x25, 0x1E, -0x17, 0x1F, 0x26, 0x2D, 0x34, 0x3B, 0x3C, 0x35, -0x2E, 0x27, 0x2F, 0x36, 0x3D, 0x3E, 0x37, 0x3F, -}; -static const uint8_t wmv1_scantable03[64]= { -0x00, 0x08, 0x10, 0x01, 0x18, 0x20, 0x28, 0x09, -0x02, 0x03, 0x0A, 0x11, 0x19, 0x30, 0x38, 0x29, -0x21, 0x1A, 0x12, 0x0B, 0x04, 0x05, 0x0C, 0x13, -0x1B, 0x22, 0x31, 0x39, 0x32, 0x2A, 0x23, 0x1C, -0x14, 0x0D, 0x06, 0x07, 0x0E, 0x15, 0x1D, 0x24, -0x2B, 0x33, 0x3A, 0x3B, 0x34, 0x2C, 0x25, 0x1E, -0x16, 0x0F, 0x17, 0x1F, 0x26, 0x2D, 0x3C, 0x35, -0x2E, 0x27, 0x2F, 0x36, 0x3D, 0x3E, 0x37, 0x3F, -}; +extern const uint8_t wmv1_scantable[WMV1_SCANTABLE_COUNT][64]; -static const uint8_t *wmv1_scantable[WMV1_SCANTABLE_COUNT+1]={ - wmv1_scantable00, - wmv1_scantable01, - wmv1_scantable02, - wmv1_scantable03, -}; +#define NB_RL_TABLES 6 -static const uint8_t table_inter_intra[4][2]={ - {0,1} /*Luma-Left Chroma-Left*/, - {2,2} /*Luma-Top Chroma-Left*/, - {6,3} /*luma-Left Chroma-Top */, - {7,3} /*luma-Top Chroma-Top */ -}; +extern RLTable rl_table[NB_RL_TABLES]; + +extern const uint8_t wmv1_y_dc_scale_table[32]; +extern const uint8_t wmv1_c_dc_scale_table[32]; +extern const uint8_t old_ff_y_dc_scale_table[32]; + +extern MVTable mv_tables[2]; + +extern const uint8_t v2_mb_type[8][2]; +extern const uint8_t v2_intra_cbpc[4][2]; + +extern const uint32_t table_mb_non_intra[128][2]; +extern const uint8_t table_inter_intra[4][2]; + +extern const uint32_t ff_table0_dc_lum[120][2]; +extern const uint32_t ff_table1_dc_lum[120][2]; +extern const uint32_t ff_table0_dc_chroma[120][2]; +extern const uint32_t ff_table1_dc_chroma[120][2]; #define WMV2_INTER_CBP_TABLE_COUNT 4 +extern const uint32_t (*wmv2_inter_table[WMV2_INTER_CBP_TABLE_COUNT])[2]; -static const uint32_t table_mb_non_intra2[128][2] = { -{0x0000A7, 14}, {0x01B2B8, 18}, {0x01B28E, 18}, {0x036575, 19}, -{0x006CAC, 16}, {0x000A69, 18}, {0x002934, 20}, {0x00526B, 21}, -{0x006CA1, 16}, {0x01B2B9, 18}, {0x0029AD, 20}, {0x029353, 24}, -{0x006CA7, 16}, {0x006CAB, 16}, {0x01B2BB, 18}, {0x00029B, 16}, -{0x00D944, 17}, {0x000A6A, 18}, {0x0149A8, 23}, {0x03651F, 19}, -{0x006CAF, 16}, {0x000A4C, 18}, {0x03651E, 19}, {0x000A48, 18}, -{0x00299C, 20}, {0x00299F, 20}, {0x029352, 24}, {0x0029AC, 20}, -{0x000296, 16}, {0x00D946, 17}, {0x000A68, 18}, {0x000298, 16}, -{0x000527, 17}, {0x00D94D, 17}, {0x0014D7, 19}, {0x036574, 19}, -{0x000A5C, 18}, {0x01B299, 18}, {0x00299D, 20}, {0x00299E, 20}, -{0x000525, 17}, {0x000A66, 18}, {0x00A4D5, 22}, {0x00149B, 19}, -{0x000295, 16}, {0x006CAD, 16}, {0x000A49, 18}, {0x000521, 17}, -{0x006CAA, 16}, {0x00D945, 17}, {0x01B298, 18}, {0x00052F, 17}, -{0x003654, 15}, {0x006CA0, 16}, {0x000532, 17}, {0x000291, 16}, -{0x003652, 15}, {0x000520, 17}, {0x000A5D, 18}, {0x000294, 16}, -{0x00009B, 11}, {0x0006E2, 12}, {0x000028, 12}, {0x0001B0, 10}, -{0x000001, 3}, {0x000010, 8}, {0x00002F, 6}, {0x00004C, 10}, -{0x00000D, 4}, {0x000000, 10}, {0x000006, 9}, {0x000134, 12}, -{0x00000C, 4}, {0x000007, 10}, {0x000007, 9}, {0x0006E1, 12}, -{0x00000E, 5}, {0x0000DA, 9}, {0x000022, 9}, {0x000364, 11}, -{0x00000F, 4}, {0x000006, 10}, {0x00000F, 9}, {0x000135, 12}, -{0x000014, 5}, {0x0000DD, 9}, {0x000004, 9}, {0x000015, 11}, -{0x00001A, 6}, {0x0001B3, 10}, {0x000005, 10}, {0x0006E3, 12}, -{0x00000C, 5}, {0x0000B9, 8}, {0x000004, 8}, {0x0000DB, 9}, -{0x00000E, 4}, {0x00000B, 10}, {0x000023, 9}, {0x0006CB, 12}, -{0x000005, 6}, {0x0001B1, 10}, {0x000001, 10}, {0x0006E0, 12}, -{0x000011, 5}, {0x0000DF, 9}, {0x00000E, 9}, {0x000373, 11}, -{0x000003, 5}, {0x0000B8, 8}, {0x000006, 8}, {0x000175, 9}, -{0x000015, 5}, {0x000174, 9}, {0x000027, 9}, {0x000372, 11}, -{0x000010, 5}, {0x0000BB, 8}, {0x000005, 8}, {0x0000DE, 9}, -{0x00000F, 5}, {0x000001, 9}, {0x000012, 8}, {0x000004, 10}, -{0x000002, 3}, {0x000016, 5}, {0x000009, 4}, {0x000001, 5}, -}; +extern const uint8_t wmv2_scantableA[64]; +extern const uint8_t wmv2_scantableB[64]; -static const uint32_t table_mb_non_intra3[128][2] = { -{0x0002A1, 10}, {0x005740, 15}, {0x01A0BF, 18}, {0x015D19, 17}, -{0x001514, 13}, {0x00461E, 15}, {0x015176, 17}, {0x015177, 17}, -{0x0011AD, 13}, {0x00682E, 16}, {0x0682F9, 20}, {0x03417D, 19}, -{0x001A36, 14}, {0x002A2D, 14}, {0x00D05E, 17}, {0x006824, 16}, -{0x001515, 13}, {0x00545C, 15}, {0x0230E9, 18}, {0x011AFA, 17}, -{0x0015D7, 13}, {0x005747, 15}, {0x008D79, 16}, {0x006825, 16}, -{0x002BA2, 14}, {0x00A8BA, 16}, {0x0235F6, 18}, {0x015D18, 17}, -{0x0011AE, 13}, {0x00346F, 15}, {0x008C3B, 16}, {0x00346E, 15}, -{0x000D1A, 13}, {0x00461F, 15}, {0x0682F8, 20}, {0x011875, 17}, -{0x002BA1, 14}, {0x008D61, 16}, {0x0235F7, 18}, {0x0230E8, 18}, -{0x001513, 13}, {0x008D7B, 16}, {0x011AF4, 17}, {0x011AF5, 17}, -{0x001185, 13}, {0x0046BF, 15}, {0x008D60, 16}, {0x008D7C, 16}, -{0x001512, 13}, {0x00461C, 15}, {0x00AE8D, 16}, {0x008D78, 16}, -{0x000D0E, 13}, {0x003413, 15}, {0x0046B1, 15}, {0x003416, 15}, -{0x000AEA, 12}, {0x002A2C, 14}, {0x005741, 15}, {0x002A2F, 14}, -{0x000158, 9}, {0x0008D2, 12}, {0x00054C, 11}, {0x000686, 12}, -{0x000000, 2}, {0x000069, 8}, {0x00006B, 8}, {0x00068C, 12}, -{0x000007, 3}, {0x00015E, 9}, {0x0002A3, 10}, {0x000AE9, 12}, -{0x000006, 3}, {0x000231, 10}, {0x0002B8, 10}, {0x001A08, 14}, -{0x000010, 5}, {0x0001A9, 10}, {0x000342, 11}, {0x000A88, 12}, -{0x000004, 4}, {0x0001A2, 10}, {0x0002A4, 10}, {0x001184, 13}, -{0x000012, 5}, {0x000232, 10}, {0x0002B2, 10}, {0x000680, 12}, -{0x00001B, 6}, {0x00046A, 11}, {0x00068E, 12}, {0x002359, 14}, -{0x000016, 5}, {0x00015F, 9}, {0x0002A0, 10}, {0x00054D, 11}, -{0x000005, 4}, {0x000233, 10}, {0x0002B9, 10}, {0x0015D6, 13}, -{0x000022, 6}, {0x000468, 11}, {0x000683, 12}, {0x001A0A, 14}, -{0x000013, 5}, {0x000236, 10}, {0x0002BB, 10}, {0x001186, 13}, -{0x000017, 5}, {0x0001AB, 10}, {0x0002A7, 10}, {0x0008D3, 12}, -{0x000014, 5}, {0x000237, 10}, {0x000460, 11}, {0x000D0F, 13}, -{0x000019, 6}, {0x0001AA, 10}, {0x0002B3, 10}, {0x000681, 12}, -{0x000018, 6}, {0x0001A8, 10}, {0x0002A5, 10}, {0x00068F, 12}, -{0x000007, 4}, {0x000055, 7}, {0x000047, 7}, {0x0000AD, 8}, -}; - -static const uint32_t table_mb_non_intra4[128][2] = { -{0x0000D4, 8}, {0x0021C5, 14}, {0x00F18A, 16}, {0x00D5BC, 16}, -{0x000879, 12}, {0x00354D, 14}, {0x010E3F, 17}, {0x010F54, 17}, -{0x000866, 12}, {0x00356E, 14}, {0x010F55, 17}, {0x010E3E, 17}, -{0x0010CE, 13}, {0x003C84, 14}, {0x00D5BD, 16}, {0x00F18B, 16}, -{0x000868, 12}, {0x00438C, 15}, {0x0087AB, 16}, {0x00790B, 15}, -{0x000F10, 12}, {0x00433D, 15}, {0x006AD3, 15}, {0x00790A, 15}, -{0x001AA7, 13}, {0x0043D4, 15}, {0x00871E, 16}, {0x006ADF, 15}, -{0x000D7C, 12}, {0x003C94, 14}, {0x00438D, 15}, {0x006AD2, 15}, -{0x0006BC, 11}, {0x0021E9, 14}, {0x006ADA, 15}, {0x006A99, 15}, -{0x0010F7, 13}, {0x004389, 15}, {0x006ADB, 15}, {0x0078C4, 15}, -{0x000D56, 12}, {0x0035F7, 14}, {0x00438E, 15}, {0x006A98, 15}, -{0x000D52, 12}, {0x003C95, 14}, {0x004388, 15}, {0x00433C, 15}, -{0x000D54, 12}, {0x001E4B, 13}, {0x003C63, 14}, {0x003C83, 14}, -{0x000861, 12}, {0x0021EB, 14}, {0x00356C, 14}, {0x0035F6, 14}, -{0x000863, 12}, {0x00219F, 14}, {0x003568, 14}, {0x003C82, 14}, -{0x0001AE, 9}, {0x0010C0, 13}, {0x000F11, 12}, {0x001AFA, 13}, -{0x000000, 1}, {0x0000F0, 8}, {0x0001AD, 9}, {0x0010C1, 13}, -{0x00000A, 4}, {0x0003C5, 10}, {0x000789, 11}, {0x001AB5, 13}, -{0x000009, 4}, {0x000435, 11}, {0x000793, 11}, {0x001E40, 13}, -{0x00001D, 5}, {0x0003CB, 10}, {0x000878, 12}, {0x001AAF, 13}, -{0x00000B, 4}, {0x0003C7, 10}, {0x000791, 11}, {0x001AAB, 13}, -{0x00001F, 5}, {0x000436, 11}, {0x0006BF, 11}, {0x000F19, 12}, -{0x00003D, 6}, {0x000D51, 12}, {0x0010C4, 13}, {0x0021E8, 14}, -{0x000036, 6}, {0x000437, 11}, {0x0006AF, 11}, {0x0010C5, 13}, -{0x00000C, 4}, {0x000432, 11}, {0x000794, 11}, {0x001E30, 13}, -{0x000042, 7}, {0x000870, 12}, {0x000F24, 12}, {0x001E43, 13}, -{0x000020, 6}, {0x00043E, 11}, {0x000795, 11}, {0x001AAA, 13}, -{0x000037, 6}, {0x0006AC, 11}, {0x0006AE, 11}, {0x0010F6, 13}, -{0x000034, 6}, {0x00043A, 11}, {0x000D50, 12}, {0x001AAE, 13}, -{0x000039, 6}, {0x00043F, 11}, {0x00078D, 11}, {0x0010D2, 13}, -{0x000038, 6}, {0x00043B, 11}, {0x0006BD, 11}, {0x0010D3, 13}, -{0x000011, 5}, {0x0001AC, 9}, {0x0000F3, 8}, {0x000439, 11}, -}; - -static const uint32_t (*wmv2_inter_table[WMV2_INTER_CBP_TABLE_COUNT])[2]={ - table_mb_non_intra2, - table_mb_non_intra3, - table_mb_non_intra4, - table_mb_non_intra, -}; - -static const uint8_t wmv2_scantableA[64]={ -0x00, 0x01, 0x02, 0x08, 0x03, 0x09, 0x0A, 0x10, -0x04, 0x0B, 0x11, 0x18, 0x12, 0x0C, 0x05, 0x13, -0x19, 0x0D, 0x14, 0x1A, 0x1B, 0x06, 0x15, 0x1C, -0x0E, 0x16, 0x1D, 0x07, 0x1E, 0x0F, 0x17, 0x1F, -}; - -static const uint8_t wmv2_scantableB[64]={ -0x00, 0x08, 0x01, 0x10, 0x09, 0x18, 0x11, 0x02, -0x20, 0x0A, 0x19, 0x28, 0x12, 0x30, 0x21, 0x1A, -0x38, 0x29, 0x22, 0x03, 0x31, 0x39, 0x0B, 0x2A, -0x13, 0x32, 0x1B, 0x3A, 0x23, 0x2B, 0x33, 0x3B, -}; +#endif /* FFMPEG_MSMPEG4DATA_H */ diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/msrle.c b/src/add-ons/media/plugins/avcodec/libavcodec/msrle.c index b318faa77f..ceffae8bb0 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/msrle.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/msrle.c @@ -2,19 +2,21 @@ * Micrsoft RLE Video Decoder * Copyright (C) 2003 the ffmpeg project * - * This library is free software; you can redistribute it and/or + * This file is part of FFmpeg. + * + * FFmpeg 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. + * version 2.1 of the License, or (at your option) any later version. * - * This library is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ /** @@ -34,7 +36,6 @@ #include #include -#include "common.h" #include "avcodec.h" #include "dsputil.h" @@ -42,7 +43,7 @@ typedef struct MsrleContext { AVCodecContext *avctx; AVFrame frame; - unsigned char *buf; + const unsigned char *buf; int size; } MsrleContext; @@ -235,14 +236,13 @@ static void msrle_decode_pal8(MsrleContext *s) stream_ptr, s->size); } -static int msrle_decode_init(AVCodecContext *avctx) +static av_cold int msrle_decode_init(AVCodecContext *avctx) { - MsrleContext *s = (MsrleContext *)avctx->priv_data; + MsrleContext *s = avctx->priv_data; s->avctx = avctx; avctx->pix_fmt = PIX_FMT_PAL8; - avctx->has_b_frames = 0; s->frame.data[0] = NULL; return 0; @@ -250,13 +250,9 @@ static int msrle_decode_init(AVCodecContext *avctx) static int msrle_decode_frame(AVCodecContext *avctx, void *data, int *data_size, - uint8_t *buf, int buf_size) + const uint8_t *buf, int buf_size) { - MsrleContext *s = (MsrleContext *)avctx->priv_data; - - /* no supplementary picture */ - if (buf_size == 0) - return 0; + MsrleContext *s = avctx->priv_data; s->buf = buf; s->size = buf_size; @@ -287,9 +283,9 @@ static int msrle_decode_frame(AVCodecContext *avctx, return buf_size; } -static int msrle_decode_end(AVCodecContext *avctx) +static av_cold int msrle_decode_end(AVCodecContext *avctx) { - MsrleContext *s = (MsrleContext *)avctx->priv_data; + MsrleContext *s = avctx->priv_data; /* release the last frame */ if (s->frame.data[0]) @@ -308,4 +304,5 @@ AVCodec msrle_decoder = { msrle_decode_end, msrle_decode_frame, CODEC_CAP_DR1, + .long_name= NULL_IF_CONFIG_SMALL("Microsoft RLE"), }; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/msvideo1.c b/src/add-ons/media/plugins/avcodec/libavcodec/msvideo1.c index 3190efb9ef..8621d661df 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/msvideo1.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/msvideo1.c @@ -2,20 +2,21 @@ * Microsoft Video-1 Decoder * Copyright (C) 2003 the ffmpeg project * - * This library is free software; you can redistribute it and/or + * This file is part of FFmpeg. + * + * FFmpeg 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. + * version 2.1 of the License, or (at your option) any later version. * - * This library is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ /** @@ -34,12 +35,9 @@ #include #include -#include "common.h" #include "avcodec.h" -#include "dsputil.h" #define PALETTE_COUNT 256 -#define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0]) #define CHECK_STREAM_PTR(n) \ if ((stream_ptr + n) > s->size ) { \ av_log(s->avctx, AV_LOG_ERROR, " MS Video-1 warning: stream_ptr out of bounds (%d >= %d)\n", \ @@ -50,19 +48,18 @@ typedef struct Msvideo1Context { AVCodecContext *avctx; - DSPContext dsp; AVFrame frame; - unsigned char *buf; + const unsigned char *buf; int size; int mode_8bit; /* if it's not 8-bit, it's 16-bit */ } Msvideo1Context; -static int msvideo1_decode_init(AVCodecContext *avctx) +static av_cold int msvideo1_decode_init(AVCodecContext *avctx) { - Msvideo1Context *s = (Msvideo1Context *)avctx->priv_data; + Msvideo1Context *s = avctx->priv_data; s->avctx = avctx; @@ -75,9 +72,6 @@ static int msvideo1_decode_init(AVCodecContext *avctx) avctx->pix_fmt = PIX_FMT_RGB555; } - avctx->has_b_frames = 0; - dsputil_init(&s->dsp, avctx); - s->frame.data[0] = NULL; return 0; @@ -157,8 +151,8 @@ static void msvideo1_decode_8bit(Msvideo1Context *s) for (pixel_y = 0; pixel_y < 4; pixel_y++) { for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1) - pixels[pixel_ptr++] = - colors[((pixel_y & 0x2) << 1) + + pixels[pixel_ptr++] = + colors[((pixel_y & 0x2) << 1) + (pixel_x & 0x2) + ((flags & 0x1) ^ 1)]; pixel_ptr -= row_dec; } @@ -244,31 +238,31 @@ static void msvideo1_decode_16bit(Msvideo1Context *s) flags = (byte_b << 8) | byte_a; CHECK_STREAM_PTR(4); - colors[0] = LE_16(&s->buf[stream_ptr]); + colors[0] = AV_RL16(&s->buf[stream_ptr]); stream_ptr += 2; - colors[1] = LE_16(&s->buf[stream_ptr]); + colors[1] = AV_RL16(&s->buf[stream_ptr]); stream_ptr += 2; if (colors[0] & 0x8000) { /* 8-color encoding */ CHECK_STREAM_PTR(12); - colors[2] = LE_16(&s->buf[stream_ptr]); + colors[2] = AV_RL16(&s->buf[stream_ptr]); stream_ptr += 2; - colors[3] = LE_16(&s->buf[stream_ptr]); + colors[3] = AV_RL16(&s->buf[stream_ptr]); stream_ptr += 2; - colors[4] = LE_16(&s->buf[stream_ptr]); + colors[4] = AV_RL16(&s->buf[stream_ptr]); stream_ptr += 2; - colors[5] = LE_16(&s->buf[stream_ptr]); + colors[5] = AV_RL16(&s->buf[stream_ptr]); stream_ptr += 2; - colors[6] = LE_16(&s->buf[stream_ptr]); + colors[6] = AV_RL16(&s->buf[stream_ptr]); stream_ptr += 2; - colors[7] = LE_16(&s->buf[stream_ptr]); + colors[7] = AV_RL16(&s->buf[stream_ptr]); stream_ptr += 2; for (pixel_y = 0; pixel_y < 4; pixel_y++) { for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1) - pixels[pixel_ptr++] = - colors[((pixel_y & 0x2) << 1) + + pixels[pixel_ptr++] = + colors[((pixel_y & 0x2) << 1) + (pixel_x & 0x2) + ((flags & 0x1) ^ 1)]; pixel_ptr -= row_dec; } @@ -299,13 +293,9 @@ static void msvideo1_decode_16bit(Msvideo1Context *s) static int msvideo1_decode_frame(AVCodecContext *avctx, void *data, int *data_size, - uint8_t *buf, int buf_size) + const uint8_t *buf, int buf_size) { - Msvideo1Context *s = (Msvideo1Context *)avctx->priv_data; - - /* no supplementary picture */ - if (buf_size == 0) - return 0; + Msvideo1Context *s = avctx->priv_data; s->buf = buf; s->size = buf_size; @@ -329,9 +319,9 @@ static int msvideo1_decode_frame(AVCodecContext *avctx, return buf_size; } -static int msvideo1_decode_end(AVCodecContext *avctx) +static av_cold int msvideo1_decode_end(AVCodecContext *avctx) { - Msvideo1Context *s = (Msvideo1Context *)avctx->priv_data; + Msvideo1Context *s = avctx->priv_data; if (s->frame.data[0]) avctx->release_buffer(avctx, &s->frame); @@ -349,4 +339,5 @@ AVCodec msvideo1_decoder = { msvideo1_decode_end, msvideo1_decode_frame, CODEC_CAP_DR1, + .long_name= NULL_IF_CONFIG_SMALL("Microsoft Video 1"), };