2001-07-22 16:18:56 +02:00
|
|
|
/*
|
2011-10-30 17:56:57 +01:00
|
|
|
* MSMPEG4 backend for encoder and decoder
|
2009-01-19 16:46:40 +01:00
|
|
|
* Copyright (c) 2001 Fabrice Bellard
|
2004-01-10 17:04:55 +01:00
|
|
|
* Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
|
2001-07-22 16:18:56 +02:00
|
|
|
*
|
2007-07-05 12:37:29 +02:00
|
|
|
* msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at>
|
|
|
|
*
|
2006-10-07 17:30:46 +02:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2002-05-26 00:45:33 +02:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 17:30:46 +02:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2001-07-22 16:18:56 +02:00
|
|
|
*
|
2006-10-07 17:30:46 +02:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2001-07-22 16:18:56 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2002-05-26 00:45:33 +02:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2001-07-22 16:18:56 +02:00
|
|
|
*
|
2002-05-26 00:45:33 +02:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2006-10-07 17:30:46 +02:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-01-12 23:43:26 +01:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2001-07-22 16:18:56 +02:00
|
|
|
*/
|
2003-03-06 12:32:04 +01:00
|
|
|
|
|
|
|
/**
|
2010-04-20 16:45:34 +02:00
|
|
|
* @file
|
2011-10-30 17:56:57 +01:00
|
|
|
* MSMPEG4 backend for encoder and decoder
|
2003-03-06 12:32:04 +01:00
|
|
|
*/
|
|
|
|
|
2002-05-19 01:03:29 +02:00
|
|
|
#include "avcodec.h"
|
2001-07-22 16:18:56 +02:00
|
|
|
#include "dsputil.h"
|
|
|
|
#include "mpegvideo.h"
|
2007-11-08 00:41:39 +01:00
|
|
|
#include "msmpeg4.h"
|
2009-08-21 11:54:28 +02:00
|
|
|
#include "libavutil/x86_cpu.h"
|
2010-01-08 00:53:49 +01:00
|
|
|
#include "h263.h"
|
|
|
|
#include "mpeg4video.h"
|
2012-02-18 14:28:43 +01:00
|
|
|
#include "msmpeg4data.h"
|
2012-02-18 13:43:59 +01:00
|
|
|
#include "vc1data.h"
|
2002-05-22 01:13:57 +02:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
/*
|
2005-12-17 19:14:38 +01:00
|
|
|
* You can also call this codec : MPEG4 with a twist !
|
2001-07-22 16:18:56 +02:00
|
|
|
*
|
2005-12-17 19:14:38 +01:00
|
|
|
* TODO:
|
2001-07-22 16:18:56 +02:00
|
|
|
* - (encoding) select best mv table (two choices)
|
2005-12-17 19:14:38 +01:00
|
|
|
* - (encoding) select best vlc/dc table
|
2001-07-22 16:18:56 +02:00
|
|
|
*/
|
|
|
|
//#define DEBUG
|
|
|
|
|
2002-07-10 22:05:42 +02:00
|
|
|
#define DC_VLC_BITS 9
|
|
|
|
#define V2_INTRA_CBPC_VLC_BITS 3
|
|
|
|
#define V2_MB_TYPE_VLC_BITS 7
|
|
|
|
#define MV_VLC_BITS 9
|
|
|
|
#define V2_MV_VLC_BITS 9
|
|
|
|
#define TEX_VLC_BITS 9
|
|
|
|
|
2002-12-28 00:51:46 +01:00
|
|
|
#define DEFAULT_INTER_INDEX 3
|
|
|
|
|
2009-12-30 15:15:12 +01:00
|
|
|
/* This table is practically identical to the one from h263
|
|
|
|
* except that it is inverted. */
|
|
|
|
static av_cold void init_h263_dc_for_msmpeg4(void)
|
|
|
|
{
|
|
|
|
int level, uni_code, uni_len;
|
|
|
|
|
|
|
|
for(level=-256; level<256; level++){
|
|
|
|
int size, v, l;
|
|
|
|
/* find number of bits */
|
|
|
|
size = 0;
|
|
|
|
v = abs(level);
|
|
|
|
while (v) {
|
|
|
|
v >>= 1;
|
|
|
|
size++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (level < 0)
|
|
|
|
l= (-level) ^ ((1 << size) - 1);
|
|
|
|
else
|
|
|
|
l= level;
|
|
|
|
|
|
|
|
/* luminance h263 */
|
2010-01-08 18:28:43 +01:00
|
|
|
uni_code= ff_mpeg4_DCtab_lum[size][0];
|
|
|
|
uni_len = ff_mpeg4_DCtab_lum[size][1];
|
2009-12-30 15:15:12 +01:00
|
|
|
uni_code ^= (1<<uni_len)-1; //M$ does not like compatibility
|
|
|
|
|
|
|
|
if (size > 0) {
|
|
|
|
uni_code<<=size; uni_code|=l;
|
|
|
|
uni_len+=size;
|
|
|
|
if (size > 8){
|
|
|
|
uni_code<<=1; uni_code|=1;
|
|
|
|
uni_len++;
|
|
|
|
}
|
|
|
|
}
|
2012-02-18 14:28:43 +01:00
|
|
|
ff_v2_dc_lum_table[level + 256][0] = uni_code;
|
|
|
|
ff_v2_dc_lum_table[level + 256][1] = uni_len;
|
2009-12-30 15:15:12 +01:00
|
|
|
|
|
|
|
/* chrominance h263 */
|
2010-01-08 18:28:43 +01:00
|
|
|
uni_code= ff_mpeg4_DCtab_chrom[size][0];
|
|
|
|
uni_len = ff_mpeg4_DCtab_chrom[size][1];
|
2009-12-30 15:15:12 +01:00
|
|
|
uni_code ^= (1<<uni_len)-1; //M$ does not like compatibility
|
|
|
|
|
|
|
|
if (size > 0) {
|
|
|
|
uni_code<<=size; uni_code|=l;
|
|
|
|
uni_len+=size;
|
|
|
|
if (size > 8){
|
|
|
|
uni_code<<=1; uni_code|=1;
|
|
|
|
uni_len++;
|
|
|
|
}
|
|
|
|
}
|
2012-02-18 14:28:43 +01:00
|
|
|
ff_v2_dc_chroma_table[level + 256][0] = uni_code;
|
|
|
|
ff_v2_dc_chroma_table[level + 256][1] = uni_len;
|
2009-12-30 15:15:12 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-18 14:28:43 +01:00
|
|
|
av_cold void ff_msmpeg4_common_init(MpegEncContext *s)
|
2002-06-18 02:49:00 +02:00
|
|
|
{
|
2008-02-13 10:26:10 +01:00
|
|
|
static int initialized=0;
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2002-06-18 02:49:00 +02:00
|
|
|
switch(s->msmpeg4_version){
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
s->y_dc_scale_table=
|
|
|
|
s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
if(s->workaround_bugs){
|
2012-02-15 11:56:41 +01:00
|
|
|
s->y_dc_scale_table= ff_old_ff_y_dc_scale_table;
|
|
|
|
s->c_dc_scale_table= ff_wmv1_c_dc_scale_table;
|
2002-06-18 02:49:00 +02:00
|
|
|
} else{
|
|
|
|
s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
|
|
|
|
s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 4:
|
2002-12-28 00:51:46 +01:00
|
|
|
case 5:
|
2012-02-15 11:56:41 +01:00
|
|
|
s->y_dc_scale_table= ff_wmv1_y_dc_scale_table;
|
|
|
|
s->c_dc_scale_table= ff_wmv1_c_dc_scale_table;
|
2002-06-18 02:49:00 +02:00
|
|
|
break;
|
2009-07-29 11:54:49 +02:00
|
|
|
#if CONFIG_VC1_DECODER
|
2005-01-30 17:34:57 +01:00
|
|
|
case 6:
|
2012-02-15 12:00:08 +01:00
|
|
|
s->y_dc_scale_table= ff_wmv3_dc_scale_table;
|
|
|
|
s->c_dc_scale_table= ff_wmv3_dc_scale_table;
|
2005-01-30 17:34:57 +01:00
|
|
|
break;
|
2005-05-03 00:14:42 +02:00
|
|
|
#endif
|
2005-01-30 17:34:57 +01:00
|
|
|
|
2002-06-18 02:49:00 +02:00
|
|
|
}
|
|
|
|
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2002-12-28 00:51:46 +01:00
|
|
|
if(s->msmpeg4_version>=4){
|
2012-02-15 11:56:41 +01:00
|
|
|
ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_wmv1_scantable[1]);
|
|
|
|
ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_wmv1_scantable[2]);
|
|
|
|
ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_wmv1_scantable[3]);
|
|
|
|
ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_wmv1_scantable[0]);
|
2002-06-18 02:49:00 +02:00
|
|
|
}
|
2002-09-30 00:44:22 +02:00
|
|
|
//Note the default tables are set in common_init in mpegvideo.c
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2008-02-13 10:26:10 +01:00
|
|
|
if(!initialized){
|
|
|
|
initialized=1;
|
2002-06-18 02:49:00 +02:00
|
|
|
|
|
|
|
init_h263_dc_for_msmpeg4();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
/* predict coded block */
|
2007-11-08 00:23:35 +01:00
|
|
|
int ff_msmpeg4_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
2002-03-29 02:53:59 +01:00
|
|
|
int xy, wrap, pred, a, b, c;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2002-03-29 02:53:59 +01:00
|
|
|
xy = s->block_index[n];
|
2004-04-16 03:01:45 +02:00
|
|
|
wrap = s->b8_stride;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
/* B C
|
2005-12-17 19:14:38 +01:00
|
|
|
* A X
|
2001-07-22 16:18:56 +02:00
|
|
|
*/
|
2002-03-29 02:53:59 +01:00
|
|
|
a = s->coded_block[xy - 1 ];
|
|
|
|
b = s->coded_block[xy - 1 - wrap];
|
|
|
|
c = s->coded_block[xy - wrap];
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
if (b == c) {
|
|
|
|
pred = a;
|
|
|
|
} else {
|
|
|
|
pred = c;
|
|
|
|
}
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
/* store value */
|
2002-03-29 02:53:59 +01:00
|
|
|
*coded_block_ptr = &s->coded_block[xy];
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
return pred;
|
|
|
|
}
|
|
|
|
|
2005-12-17 19:14:38 +01:00
|
|
|
static inline int msmpeg4v1_pred_dc(MpegEncContext * s, int n,
|
2003-02-11 17:35:48 +01:00
|
|
|
int32_t **dc_val_ptr)
|
2002-06-02 14:22:30 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (n < 4) {
|
|
|
|
i= 0;
|
|
|
|
} else {
|
|
|
|
i= n-3;
|
|
|
|
}
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2002-06-02 14:22:30 +02:00
|
|
|
*dc_val_ptr= &s->last_dc[i];
|
2005-12-17 19:14:38 +01:00
|
|
|
return s->last_dc[i];
|
2002-06-02 14:22:30 +02:00
|
|
|
}
|
|
|
|
|
2002-07-07 10:34:46 +02:00
|
|
|
static int get_dc(uint8_t *src, int stride, int scale)
|
|
|
|
{
|
|
|
|
int y;
|
|
|
|
int sum=0;
|
|
|
|
for(y=0; y<8; y++){
|
|
|
|
int x;
|
|
|
|
for(x=0; x<8; x++){
|
|
|
|
sum+=src[x + y*stride];
|
|
|
|
}
|
|
|
|
}
|
2003-05-14 17:12:13 +02:00
|
|
|
return FASTDIV((sum + (scale>>1)), scale);
|
2002-07-07 10:34:46 +02:00
|
|
|
}
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
/* dir = 0: left, dir = 1: top prediction */
|
2012-02-18 14:28:43 +01:00
|
|
|
int ff_msmpeg4_pred_dc(MpegEncContext *s, int n,
|
|
|
|
int16_t **dc_val_ptr, int *dir_ptr)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
2002-03-29 02:53:59 +01:00
|
|
|
int a, b, c, wrap, pred, scale;
|
2006-09-28 00:13:44 +02:00
|
|
|
int16_t *dc_val;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
/* find prediction */
|
|
|
|
if (n < 4) {
|
2005-12-22 02:10:11 +01:00
|
|
|
scale = s->y_dc_scale;
|
2001-07-22 16:18:56 +02:00
|
|
|
} else {
|
2005-12-22 02:10:11 +01:00
|
|
|
scale = s->c_dc_scale;
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2002-03-29 02:53:59 +01:00
|
|
|
wrap = s->block_wrap[n];
|
|
|
|
dc_val= s->dc_val[0] + s->block_index[n];
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
/* B C
|
2005-12-17 19:14:38 +01:00
|
|
|
* A X
|
2001-07-22 16:18:56 +02:00
|
|
|
*/
|
2002-03-29 02:53:59 +01:00
|
|
|
a = dc_val[ - 1];
|
|
|
|
b = dc_val[ - 1 - wrap];
|
|
|
|
c = dc_val[ - wrap];
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2003-01-09 12:37:08 +01:00
|
|
|
if(s->first_slice_line && (n&2)==0 && s->msmpeg4_version<4){
|
2002-10-13 15:16:04 +02:00
|
|
|
b=c=1024;
|
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
/* XXX: the following solution consumes divisions, but it does not
|
|
|
|
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 !) */
|
2009-08-21 11:54:28 +02:00
|
|
|
#if ARCH_X86 && HAVE_7REGS && HAVE_EBX_AVAILABLE
|
2008-10-16 15:34:09 +02:00
|
|
|
__asm__ volatile(
|
2005-12-22 02:10:11 +01:00
|
|
|
"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)
|
2006-11-12 19:49:36 +01:00
|
|
|
: "g" (scale), "S" (ff_inverse[scale])
|
2005-12-22 02:10:11 +01:00
|
|
|
: "%eax", "%edx"
|
2002-01-14 05:34:52 +01:00
|
|
|
);
|
2002-01-28 19:06:28 +01:00
|
|
|
#else
|
2009-01-14 00:44:16 +01:00
|
|
|
/* #elif ARCH_ALPHA */
|
2002-01-20 15:48:02 +01:00
|
|
|
/* Divisions are extremely costly on Alpha; optimize the most
|
2002-01-28 19:06:28 +01:00
|
|
|
common case. But they are costly everywhere...
|
|
|
|
*/
|
2002-01-20 15:48:02 +01:00
|
|
|
if (scale == 8) {
|
2005-12-22 02:10:11 +01:00
|
|
|
a = (a + (8 >> 1)) / 8;
|
|
|
|
b = (b + (8 >> 1)) / 8;
|
|
|
|
c = (c + (8 >> 1)) / 8;
|
2002-01-20 15:48:02 +01:00
|
|
|
} else {
|
2005-12-22 02:10:11 +01:00
|
|
|
a = FASTDIV((a + (scale >> 1)), scale);
|
|
|
|
b = FASTDIV((b + (scale >> 1)), scale);
|
|
|
|
c = FASTDIV((c + (scale >> 1)), scale);
|
2002-01-20 15:48:02 +01:00
|
|
|
}
|
2002-01-14 05:34:52 +01:00
|
|
|
#endif
|
2001-07-22 16:18:56 +02:00
|
|
|
/* XXX: WARNING: they did not choose the same test as MPEG4. This
|
|
|
|
is very important ! */
|
2002-06-22 17:52:25 +02:00
|
|
|
if(s->msmpeg4_version>3){
|
2002-07-07 10:34:46 +02:00
|
|
|
if(s->inter_intra_pred){
|
|
|
|
uint8_t *dest;
|
|
|
|
int wrap;
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2002-07-07 10:34:46 +02:00
|
|
|
if(n==1){
|
|
|
|
pred=a;
|
|
|
|
*dir_ptr = 0;
|
|
|
|
}else if(n==2){
|
|
|
|
pred=c;
|
|
|
|
*dir_ptr = 1;
|
|
|
|
}else if(n==3){
|
|
|
|
if (abs(a - b) < abs(b - c)) {
|
|
|
|
pred = c;
|
|
|
|
*dir_ptr = 1;
|
|
|
|
} else {
|
|
|
|
pred = a;
|
|
|
|
*dir_ptr = 0;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
if(n<4){
|
|
|
|
wrap= s->linesize;
|
2011-07-06 20:08:30 +02:00
|
|
|
dest= s->current_picture.f.data[0] + (((n >> 1) + 2*s->mb_y) * 8* wrap ) + ((n & 1) + 2*s->mb_x) * 8;
|
2002-07-07 10:34:46 +02:00
|
|
|
}else{
|
2002-07-15 16:15:10 +02:00
|
|
|
wrap= s->uvlinesize;
|
2011-07-06 20:08:30 +02:00
|
|
|
dest= s->current_picture.f.data[n - 3] + (s->mb_y * 8 * wrap) + s->mb_x * 8;
|
2002-07-07 10:34:46 +02:00
|
|
|
}
|
|
|
|
if(s->mb_x==0) a= (1024 + (scale>>1))/scale;
|
|
|
|
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);
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2002-07-07 10:34:46 +02:00
|
|
|
if (s->h263_aic_dir==0) {
|
|
|
|
pred= a;
|
|
|
|
*dir_ptr = 0;
|
|
|
|
}else if (s->h263_aic_dir==1) {
|
|
|
|
if(n==0){
|
|
|
|
pred= c;
|
|
|
|
*dir_ptr = 1;
|
|
|
|
}else{
|
|
|
|
pred= a;
|
|
|
|
*dir_ptr = 0;
|
|
|
|
}
|
|
|
|
}else if (s->h263_aic_dir==2) {
|
|
|
|
if(n==0){
|
|
|
|
pred= a;
|
|
|
|
*dir_ptr = 0;
|
|
|
|
}else{
|
|
|
|
pred= c;
|
|
|
|
*dir_ptr = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pred= c;
|
|
|
|
*dir_ptr = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
if (abs(a - b) < abs(b - c)) {
|
|
|
|
pred = c;
|
|
|
|
*dir_ptr = 1;
|
|
|
|
} else {
|
|
|
|
pred = a;
|
|
|
|
*dir_ptr = 0;
|
|
|
|
}
|
2002-06-22 17:52:25 +02:00
|
|
|
}
|
|
|
|
}else{
|
|
|
|
if (abs(a - b) <= abs(b - c)) {
|
|
|
|
pred = c;
|
|
|
|
*dir_ptr = 1;
|
|
|
|
} else {
|
|
|
|
pred = a;
|
|
|
|
*dir_ptr = 0;
|
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* update predictor */
|
2002-03-29 02:53:59 +01:00
|
|
|
*dc_val_ptr = &dc_val[0];
|
2001-07-22 16:18:56 +02:00
|
|
|
return pred;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************/
|
|
|
|
/* decoding stuff */
|
|
|
|
|
2007-11-08 00:23:35 +01:00
|
|
|
VLC ff_mb_non_intra_vlc[4];
|
2002-04-05 06:09:04 +02:00
|
|
|
static VLC v2_dc_lum_vlc;
|
|
|
|
static VLC v2_dc_chroma_vlc;
|
|
|
|
static VLC v2_intra_cbpc_vlc;
|
|
|
|
static VLC v2_mb_type_vlc;
|
|
|
|
static VLC v2_mv_vlc;
|
2007-11-08 00:23:35 +01:00
|
|
|
VLC ff_inter_intra_vlc;
|
2002-04-05 06:09:04 +02:00
|
|
|
|
2009-12-30 15:15:12 +01:00
|
|
|
/* 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)
|
2002-04-05 06:09:04 +02:00
|
|
|
{
|
2009-12-30 15:15:12 +01:00
|
|
|
int code, val, sign, shift;
|
2002-04-05 06:09:04 +02:00
|
|
|
|
2009-12-30 15:15:12 +01:00
|
|
|
code = get_vlc2(&s->gb, v2_mv_vlc.table, V2_MV_VLC_BITS, 2);
|
|
|
|
// printf("MV code %d at %d %d pred: %d\n", code, s->mb_x,s->mb_y, pred);
|
|
|
|
if (code < 0)
|
|
|
|
return 0xffff;
|
2002-04-05 06:09:04 +02:00
|
|
|
|
2009-12-30 15:15:12 +01:00
|
|
|
if (code == 0)
|
|
|
|
return pred;
|
|
|
|
sign = get_bits1(&s->gb);
|
|
|
|
shift = f_code - 1;
|
|
|
|
val = code;
|
|
|
|
if (shift) {
|
|
|
|
val = (val - 1) << shift;
|
|
|
|
val |= get_bits(&s->gb, shift);
|
|
|
|
val++;
|
|
|
|
}
|
|
|
|
if (sign)
|
|
|
|
val = -val;
|
2002-04-05 06:09:04 +02:00
|
|
|
|
2009-12-30 15:15:12 +01:00
|
|
|
val += pred;
|
|
|
|
if (val <= -64)
|
|
|
|
val += 64;
|
|
|
|
else if (val >= 64)
|
|
|
|
val -= 64;
|
2002-04-05 06:09:04 +02:00
|
|
|
|
2009-12-30 15:15:12 +01:00
|
|
|
return val;
|
|
|
|
}
|
2002-04-05 06:09:04 +02:00
|
|
|
|
2009-12-30 15:15:12 +01:00
|
|
|
static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
|
|
|
|
{
|
|
|
|
int cbp, code, i;
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2011-04-28 01:40:44 +02:00
|
|
|
if (s->pict_type == AV_PICTURE_TYPE_P) {
|
2009-12-30 15:15:12 +01:00
|
|
|
if (s->use_skip_mb_code) {
|
|
|
|
if (get_bits1(&s->gb)) {
|
|
|
|
/* skip mb */
|
|
|
|
s->mb_intra = 0;
|
|
|
|
for(i=0;i<6;i++)
|
|
|
|
s->block_last_index[i] = -1;
|
|
|
|
s->mv_dir = MV_DIR_FORWARD;
|
|
|
|
s->mv_type = MV_TYPE_16X16;
|
|
|
|
s->mv[0][0][0] = 0;
|
|
|
|
s->mv[0][0][1] = 0;
|
|
|
|
s->mb_skipped = 1;
|
|
|
|
return 0;
|
2002-04-05 06:09:04 +02:00
|
|
|
}
|
2009-12-30 15:15:12 +01:00
|
|
|
}
|
2002-04-05 06:09:04 +02:00
|
|
|
|
2009-12-30 15:15:12 +01:00
|
|
|
if(s->msmpeg4_version==2)
|
|
|
|
code = get_vlc2(&s->gb, v2_mb_type_vlc.table, V2_MB_TYPE_VLC_BITS, 1);
|
|
|
|
else
|
2010-01-08 20:10:11 +01:00
|
|
|
code = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
|
2009-12-30 15:15:12 +01:00
|
|
|
if(code<0 || code>7){
|
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", code, s->mb_x, s->mb_y);
|
|
|
|
return -1;
|
2002-04-05 06:09:04 +02:00
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2009-12-30 15:15:12 +01:00
|
|
|
s->mb_intra = code >>2;
|
|
|
|
|
|
|
|
cbp = code & 0x3;
|
|
|
|
} else {
|
|
|
|
s->mb_intra = 1;
|
|
|
|
if(s->msmpeg4_version==2)
|
|
|
|
cbp= get_vlc2(&s->gb, v2_intra_cbpc_vlc.table, V2_INTRA_CBPC_VLC_BITS, 1);
|
|
|
|
else
|
2010-01-08 20:10:11 +01:00
|
|
|
cbp= get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 1);
|
2009-12-30 15:15:12 +01:00
|
|
|
if(cbp<0 || cbp>3){
|
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!s->mb_intra) {
|
|
|
|
int mx, my, cbpy;
|
|
|
|
|
2010-01-08 18:51:48 +01:00
|
|
|
cbpy= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
|
2009-12-30 15:15:12 +01:00
|
|
|
if(cbpy<0){
|
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cbp|= cbpy<<2;
|
|
|
|
if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C;
|
|
|
|
|
2012-02-09 10:28:46 +01:00
|
|
|
ff_h263_pred_motion(s, 0, 0, &mx, &my);
|
2009-12-30 15:15:12 +01:00
|
|
|
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;
|
|
|
|
s->mv[0][0][1] = my;
|
|
|
|
} else {
|
|
|
|
if(s->msmpeg4_version==2){
|
|
|
|
s->ac_pred = get_bits1(&s->gb);
|
2010-01-08 18:51:48 +01:00
|
|
|
cbp|= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
|
2009-12-30 15:15:12 +01:00
|
|
|
} else{
|
|
|
|
s->ac_pred = 0;
|
2010-01-08 18:51:48 +01:00
|
|
|
cbp|= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
|
2011-04-28 01:40:44 +02:00
|
|
|
if(s->pict_type==AV_PICTURE_TYPE_P) cbp^=0x3C;
|
2009-12-30 15:15:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s->dsp.clear_blocks(s->block[0]);
|
|
|
|
for (i = 0; i < 6; i++) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
|
|
|
|
{
|
|
|
|
int cbp, code, i;
|
|
|
|
uint8_t *coded_val;
|
2011-07-06 20:08:30 +02:00
|
|
|
uint32_t * const mb_type_ptr = &s->current_picture.f.mb_type[s->mb_x + s->mb_y*s->mb_stride];
|
2009-12-30 15:15:12 +01:00
|
|
|
|
2011-04-28 01:40:44 +02:00
|
|
|
if (s->pict_type == AV_PICTURE_TYPE_P) {
|
2009-12-30 15:15:12 +01:00
|
|
|
if (s->use_skip_mb_code) {
|
|
|
|
if (get_bits1(&s->gb)) {
|
|
|
|
/* skip mb */
|
|
|
|
s->mb_intra = 0;
|
|
|
|
for(i=0;i<6;i++)
|
|
|
|
s->block_last_index[i] = -1;
|
|
|
|
s->mv_dir = MV_DIR_FORWARD;
|
|
|
|
s->mv_type = MV_TYPE_16X16;
|
|
|
|
s->mv[0][0][0] = 0;
|
|
|
|
s->mv[0][0][1] = 0;
|
|
|
|
s->mb_skipped = 1;
|
|
|
|
*mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
cbp = code & 0x3f;
|
|
|
|
} else {
|
|
|
|
s->mb_intra = 1;
|
|
|
|
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 */
|
|
|
|
cbp = 0;
|
|
|
|
for(i=0;i<6;i++) {
|
|
|
|
int val = ((code >> (5 - i)) & 1);
|
|
|
|
if (i < 4) {
|
|
|
|
int pred = ff_msmpeg4_coded_block_pred(s, i, &coded_val);
|
|
|
|
val = val ^ pred;
|
|
|
|
*coded_val = val;
|
|
|
|
}
|
|
|
|
cbp |= val << (5 - i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!s->mb_intra) {
|
|
|
|
int mx, my;
|
|
|
|
//printf("P at %d %d\n", s->mb_x, s->mb_y);
|
|
|
|
if(s->per_mb_rl_table && cbp){
|
|
|
|
s->rl_table_index = decode012(&s->gb);
|
|
|
|
s->rl_chroma_table_index = s->rl_table_index;
|
|
|
|
}
|
2012-02-09 10:28:46 +01:00
|
|
|
ff_h263_pred_motion(s, 0, 0, &mx, &my);
|
2009-12-30 15:15:12 +01:00
|
|
|
if (ff_msmpeg4_decode_motion(s, &mx, &my) < 0)
|
|
|
|
return -1;
|
|
|
|
s->mv_dir = MV_DIR_FORWARD;
|
|
|
|
s->mv_type = MV_TYPE_16X16;
|
|
|
|
s->mv[0][0][0] = mx;
|
|
|
|
s->mv[0][0][1] = my;
|
|
|
|
*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));
|
|
|
|
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, 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){
|
|
|
|
s->rl_table_index = decode012(&s->gb);
|
|
|
|
s->rl_chroma_table_index = s->rl_table_index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s->dsp.clear_blocks(s->block[0]);
|
|
|
|
for (i = 0; i < 6; i++) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* init all vlc decoding tables */
|
2010-01-08 20:40:10 +01:00
|
|
|
av_cold int ff_msmpeg4_decode_init(AVCodecContext *avctx)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
2010-01-08 20:40:10 +01:00
|
|
|
MpegEncContext *s = avctx->priv_data;
|
2002-06-06 16:31:18 +02:00
|
|
|
static int done = 0;
|
2001-07-22 16:18:56 +02:00
|
|
|
int i;
|
|
|
|
MVTable *mv;
|
|
|
|
|
2010-04-12 23:16:12 +02:00
|
|
|
if (ff_h263_decode_init(avctx) < 0)
|
|
|
|
return -1;
|
2010-01-08 20:40:10 +01:00
|
|
|
|
2012-02-18 14:28:43 +01:00
|
|
|
ff_msmpeg4_common_init(s);
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2002-06-06 16:31:18 +02:00
|
|
|
if (!done) {
|
|
|
|
done = 1;
|
|
|
|
|
|
|
|
for(i=0;i<NB_RL_TABLES;i++) {
|
2012-02-18 14:28:43 +01:00
|
|
|
ff_init_rl(&ff_rl_table[i], ff_static_rl_table_store[i]);
|
2002-06-06 16:31:18 +02:00
|
|
|
}
|
2012-02-15 11:56:41 +01:00
|
|
|
INIT_VLC_RL(ff_rl_table[0], 642);
|
|
|
|
INIT_VLC_RL(ff_rl_table[1], 1104);
|
|
|
|
INIT_VLC_RL(ff_rl_table[2], 554);
|
|
|
|
INIT_VLC_RL(ff_rl_table[3], 940);
|
|
|
|
INIT_VLC_RL(ff_rl_table[4], 962);
|
|
|
|
INIT_VLC_RL(ff_rl_table[5], 554);
|
|
|
|
|
|
|
|
mv = &ff_mv_tables[0];
|
2009-04-11 15:29:05 +02:00
|
|
|
INIT_VLC_STATIC(&mv->vlc, MV_VLC_BITS, mv->n + 1,
|
|
|
|
mv->table_mv_bits, 1, 1,
|
|
|
|
mv->table_mv_code, 2, 2, 3714);
|
2012-02-15 11:56:41 +01:00
|
|
|
mv = &ff_mv_tables[1];
|
2009-04-11 15:29:05 +02:00
|
|
|
INIT_VLC_STATIC(&mv->vlc, MV_VLC_BITS, mv->n + 1,
|
|
|
|
mv->table_mv_bits, 1, 1,
|
|
|
|
mv->table_mv_code, 2, 2, 2694);
|
|
|
|
|
|
|
|
INIT_VLC_STATIC(&ff_msmp4_dc_luma_vlc[0], DC_VLC_BITS, 120,
|
2005-01-26 13:01:35 +01:00
|
|
|
&ff_table0_dc_lum[0][1], 8, 4,
|
2009-04-11 15:29:05 +02:00
|
|
|
&ff_table0_dc_lum[0][0], 8, 4, 1158);
|
|
|
|
INIT_VLC_STATIC(&ff_msmp4_dc_chroma_vlc[0], DC_VLC_BITS, 120,
|
2005-01-26 13:01:35 +01:00
|
|
|
&ff_table0_dc_chroma[0][1], 8, 4,
|
2009-04-11 15:29:05 +02:00
|
|
|
&ff_table0_dc_chroma[0][0], 8, 4, 1118);
|
|
|
|
INIT_VLC_STATIC(&ff_msmp4_dc_luma_vlc[1], DC_VLC_BITS, 120,
|
2005-01-26 13:01:35 +01:00
|
|
|
&ff_table1_dc_lum[0][1], 8, 4,
|
2009-04-11 15:29:05 +02:00
|
|
|
&ff_table1_dc_lum[0][0], 8, 4, 1476);
|
|
|
|
INIT_VLC_STATIC(&ff_msmp4_dc_chroma_vlc[1], DC_VLC_BITS, 120,
|
2005-01-26 13:01:35 +01:00
|
|
|
&ff_table1_dc_chroma[0][1], 8, 4,
|
2009-04-11 15:29:05 +02:00
|
|
|
&ff_table1_dc_chroma[0][0], 8, 4, 1216);
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2009-04-11 15:29:05 +02:00
|
|
|
INIT_VLC_STATIC(&v2_dc_lum_vlc, DC_VLC_BITS, 512,
|
2012-02-18 14:28:43 +01:00
|
|
|
&ff_v2_dc_lum_table[0][1], 8, 4,
|
|
|
|
&ff_v2_dc_lum_table[0][0], 8, 4, 1472);
|
2009-04-11 15:29:05 +02:00
|
|
|
INIT_VLC_STATIC(&v2_dc_chroma_vlc, DC_VLC_BITS, 512,
|
2012-02-18 14:28:43 +01:00
|
|
|
&ff_v2_dc_chroma_table[0][1], 8, 4,
|
|
|
|
&ff_v2_dc_chroma_table[0][0], 8, 4, 1506);
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2009-04-11 15:29:05 +02:00
|
|
|
INIT_VLC_STATIC(&v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 4,
|
2012-02-15 11:56:41 +01:00
|
|
|
&ff_v2_intra_cbpc[0][1], 2, 1,
|
|
|
|
&ff_v2_intra_cbpc[0][0], 2, 1, 8);
|
2009-04-11 15:29:05 +02:00
|
|
|
INIT_VLC_STATIC(&v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 8,
|
2012-02-15 11:56:41 +01:00
|
|
|
&ff_v2_mb_type[0][1], 2, 1,
|
|
|
|
&ff_v2_mb_type[0][0], 2, 1, 128);
|
2009-04-11 15:29:05 +02:00
|
|
|
INIT_VLC_STATIC(&v2_mv_vlc, V2_MV_VLC_BITS, 33,
|
2012-02-09 10:28:46 +01:00
|
|
|
&ff_mvtab[0][1], 2, 1,
|
|
|
|
&ff_mvtab[0][0], 2, 1, 538);
|
2009-04-11 15:29:05 +02:00
|
|
|
|
|
|
|
INIT_VLC_STATIC(&ff_mb_non_intra_vlc[0], MB_NON_INTRA_VLC_BITS, 128,
|
2012-02-15 11:56:41 +01:00
|
|
|
&ff_wmv2_inter_table[0][0][1], 8, 4,
|
|
|
|
&ff_wmv2_inter_table[0][0][0], 8, 4, 1636);
|
2009-04-11 15:29:05 +02:00
|
|
|
INIT_VLC_STATIC(&ff_mb_non_intra_vlc[1], MB_NON_INTRA_VLC_BITS, 128,
|
2012-02-15 11:56:41 +01:00
|
|
|
&ff_wmv2_inter_table[1][0][1], 8, 4,
|
|
|
|
&ff_wmv2_inter_table[1][0][0], 8, 4, 2648);
|
2009-04-11 15:29:05 +02:00
|
|
|
INIT_VLC_STATIC(&ff_mb_non_intra_vlc[2], MB_NON_INTRA_VLC_BITS, 128,
|
2012-02-15 11:56:41 +01:00
|
|
|
&ff_wmv2_inter_table[2][0][1], 8, 4,
|
|
|
|
&ff_wmv2_inter_table[2][0][0], 8, 4, 1532);
|
2009-04-11 15:29:05 +02:00
|
|
|
INIT_VLC_STATIC(&ff_mb_non_intra_vlc[3], MB_NON_INTRA_VLC_BITS, 128,
|
2012-02-15 11:56:41 +01:00
|
|
|
&ff_wmv2_inter_table[3][0][1], 8, 4,
|
|
|
|
&ff_wmv2_inter_table[3][0][0], 8, 4, 2488);
|
2009-04-11 15:29:05 +02:00
|
|
|
|
|
|
|
INIT_VLC_STATIC(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64,
|
2005-01-30 17:34:57 +01:00
|
|
|
&ff_msmp4_mb_i_table[0][1], 4, 2,
|
2009-04-11 15:29:05 +02:00
|
|
|
&ff_msmp4_mb_i_table[0][0], 4, 2, 536);
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2009-04-11 15:29:05 +02:00
|
|
|
INIT_VLC_STATIC(&ff_inter_intra_vlc, INTER_INTRA_VLC_BITS, 4,
|
2012-02-15 11:56:41 +01:00
|
|
|
&ff_table_inter_intra[0][1], 2, 1,
|
|
|
|
&ff_table_inter_intra[0][0], 2, 1, 8);
|
2002-06-06 16:31:18 +02:00
|
|
|
}
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2002-10-13 15:16:04 +02:00
|
|
|
switch(s->msmpeg4_version){
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
s->decode_mb= msmpeg4v12_decode_mb;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
case 4:
|
|
|
|
s->decode_mb= msmpeg4v34_decode_mb;
|
|
|
|
break;
|
2002-12-28 00:51:46 +01:00
|
|
|
case 5:
|
2009-01-14 18:19:17 +01:00
|
|
|
if (CONFIG_WMV2_DECODER)
|
2007-11-08 00:42:24 +01:00
|
|
|
s->decode_mb= ff_wmv2_decode_mb;
|
2005-01-30 17:34:57 +01:00
|
|
|
case 6:
|
2006-06-27 04:55:54 +02:00
|
|
|
//FIXME + TODO VC1 decode mb
|
2002-12-28 00:51:46 +01:00
|
|
|
break;
|
2002-10-13 15:16:04 +02:00
|
|
|
}
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2007-06-12 11:29:25 +02:00
|
|
|
s->slice_height= s->mb_height; //to avoid 1/0 if the first frame is not a keyframe
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-15 12:05:06 +01:00
|
|
|
int ff_msmpeg4_decode_picture_header(MpegEncContext * s)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
2002-06-18 02:49:00 +02:00
|
|
|
int code;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2002-06-02 14:22:30 +02:00
|
|
|
if(s->msmpeg4_version==1){
|
2010-08-24 17:40:57 +02:00
|
|
|
int start_code = get_bits_long(&s->gb, 32);
|
2002-06-02 14:22:30 +02:00
|
|
|
if(start_code!=0x00000100){
|
2003-11-03 14:26:22 +01:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "invalid startcode\n");
|
2002-06-02 14:22:30 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-04-17 14:41:37 +02:00
|
|
|
skip_bits(&s->gb, 5); // frame number */
|
2002-06-02 14:22:30 +02:00
|
|
|
}
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
s->pict_type = get_bits(&s->gb, 2) + 1;
|
2011-04-28 01:40:44 +02:00
|
|
|
if (s->pict_type != AV_PICTURE_TYPE_I &&
|
|
|
|
s->pict_type != AV_PICTURE_TYPE_P){
|
2003-11-03 14:26:22 +01:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "invalid picture type\n");
|
2001-07-22 16:18:56 +02:00
|
|
|
return -1;
|
2002-06-02 14:22:30 +02:00
|
|
|
}
|
2002-07-07 10:34:46 +02:00
|
|
|
#if 0
|
|
|
|
{
|
|
|
|
static int had_i=0;
|
2011-04-28 01:40:44 +02:00
|
|
|
if(s->pict_type == AV_PICTURE_TYPE_I) had_i=1;
|
2002-07-07 10:34:46 +02:00
|
|
|
if(!had_i) return -1;
|
|
|
|
}
|
|
|
|
#endif
|
2003-12-03 17:07:41 +01:00
|
|
|
s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
|
2003-02-10 23:43:30 +01:00
|
|
|
if(s->qscale==0){
|
2003-11-03 14:26:22 +01:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "invalid qscale\n");
|
2003-02-10 23:43:30 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2011-04-28 01:40:44 +02:00
|
|
|
if (s->pict_type == AV_PICTURE_TYPE_I) {
|
2005-12-17 19:14:38 +01:00
|
|
|
code = get_bits(&s->gb, 5);
|
2002-06-02 14:22:30 +02:00
|
|
|
if(s->msmpeg4_version==1){
|
|
|
|
if(code==0 || code>s->mb_height){
|
2003-11-03 14:26:22 +01:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "invalid slice height %d\n", code);
|
2002-06-02 14:22:30 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->slice_height = code;
|
|
|
|
}else{
|
|
|
|
/* 0x17: one slice, 0x18: two slices, ... */
|
2002-07-07 10:34:46 +02:00
|
|
|
if (code < 0x17){
|
2003-11-03 14:26:22 +01:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "error, slice code was %X\n", code);
|
2002-06-02 14:22:30 +02:00
|
|
|
return -1;
|
2002-07-07 10:34:46 +02:00
|
|
|
}
|
2002-06-02 14:22:30 +02:00
|
|
|
|
|
|
|
s->slice_height = s->mb_height / (code - 0x16);
|
|
|
|
}
|
2002-04-07 00:29:37 +02:00
|
|
|
|
|
|
|
switch(s->msmpeg4_version){
|
2002-06-02 14:22:30 +02:00
|
|
|
case 1:
|
2002-04-07 00:29:37 +02:00
|
|
|
case 2:
|
2002-04-05 06:09:04 +02:00
|
|
|
s->rl_chroma_table_index = 2;
|
|
|
|
s->rl_table_index = 2;
|
2002-03-29 03:07:25 +01:00
|
|
|
|
2002-04-05 06:09:04 +02:00
|
|
|
s->dc_table_index = 0; //not used
|
2002-04-07 00:29:37 +02:00
|
|
|
break;
|
|
|
|
case 3:
|
2002-04-05 06:09:04 +02:00
|
|
|
s->rl_chroma_table_index = decode012(&s->gb);
|
|
|
|
s->rl_table_index = decode012(&s->gb);
|
2002-03-29 03:07:25 +01:00
|
|
|
|
2002-04-05 06:09:04 +02:00
|
|
|
s->dc_table_index = get_bits1(&s->gb);
|
2002-04-07 00:29:37 +02:00
|
|
|
break;
|
|
|
|
case 4:
|
2012-02-15 12:05:06 +01:00
|
|
|
ff_msmpeg4_decode_ext_header(s, (2+5+5+17+7)/8);
|
2002-06-18 02:49:00 +02:00
|
|
|
|
2002-07-22 10:15:27 +02:00
|
|
|
if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
|
|
|
|
else s->per_mb_rl_table= 0;
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2002-06-18 02:49:00 +02:00
|
|
|
if(!s->per_mb_rl_table){
|
|
|
|
s->rl_chroma_table_index = decode012(&s->gb);
|
|
|
|
s->rl_table_index = decode012(&s->gb);
|
2002-04-07 00:29:37 +02:00
|
|
|
}
|
|
|
|
|
2002-06-18 02:49:00 +02:00
|
|
|
s->dc_table_index = get_bits1(&s->gb);
|
2002-07-07 10:34:46 +02:00
|
|
|
s->inter_intra_pred= 0;
|
2002-04-07 00:29:37 +02:00
|
|
|
break;
|
2002-04-05 06:09:04 +02:00
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
s->no_rounding = 1;
|
2003-07-29 03:45:19 +02:00
|
|
|
if(s->avctx->debug&FF_DEBUG_PICT_INFO)
|
2005-12-22 02:10:11 +01:00
|
|
|
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,
|
2002-07-07 10:34:46 +02:00
|
|
|
s->per_mb_rl_table,
|
2003-07-29 03:45:19 +02:00
|
|
|
s->slice_height);
|
2001-07-22 16:18:56 +02:00
|
|
|
} else {
|
2002-06-02 14:22:30 +02:00
|
|
|
switch(s->msmpeg4_version){
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
if(s->msmpeg4_version==1)
|
|
|
|
s->use_skip_mb_code = 1;
|
|
|
|
else
|
|
|
|
s->use_skip_mb_code = get_bits1(&s->gb);
|
2002-04-05 06:09:04 +02:00
|
|
|
s->rl_table_index = 2;
|
|
|
|
s->rl_chroma_table_index = s->rl_table_index;
|
|
|
|
s->dc_table_index = 0; //not used
|
|
|
|
s->mv_table_index = 0;
|
2002-06-02 14:22:30 +02:00
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
s->use_skip_mb_code = get_bits1(&s->gb);
|
2002-04-05 06:09:04 +02:00
|
|
|
s->rl_table_index = decode012(&s->gb);
|
|
|
|
s->rl_chroma_table_index = s->rl_table_index;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2002-04-05 06:09:04 +02:00
|
|
|
s->dc_table_index = get_bits1(&s->gb);
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2002-06-18 02:49:00 +02:00
|
|
|
s->mv_table_index = get_bits1(&s->gb);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
s->use_skip_mb_code = get_bits1(&s->gb);
|
2002-07-07 10:34:46 +02:00
|
|
|
|
2002-07-22 10:15:27 +02:00
|
|
|
if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
|
|
|
|
else s->per_mb_rl_table= 0;
|
2002-07-07 10:34:46 +02:00
|
|
|
|
2002-06-18 02:49:00 +02:00
|
|
|
if(!s->per_mb_rl_table){
|
|
|
|
s->rl_table_index = decode012(&s->gb);
|
|
|
|
s->rl_chroma_table_index = s->rl_table_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->dc_table_index = get_bits1(&s->gb);
|
|
|
|
|
2002-04-05 06:09:04 +02:00
|
|
|
s->mv_table_index = get_bits1(&s->gb);
|
2002-07-22 10:15:27 +02:00
|
|
|
s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE);
|
2002-06-02 14:22:30 +02:00
|
|
|
break;
|
2002-04-05 06:09:04 +02:00
|
|
|
}
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2003-07-29 03:45:19 +02:00
|
|
|
if(s->avctx->debug&FF_DEBUG_PICT_INFO)
|
2005-12-22 02:10:11 +01:00
|
|
|
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,
|
2002-07-07 10:34:46 +02:00
|
|
|
s->per_mb_rl_table,
|
2003-07-29 03:45:19 +02:00
|
|
|
s->qscale);
|
|
|
|
|
2005-12-22 02:10:11 +01:00
|
|
|
if(s->flipflop_rounding){
|
|
|
|
s->no_rounding ^= 1;
|
|
|
|
}else{
|
|
|
|
s->no_rounding = 0;
|
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
2002-12-28 00:51:46 +01:00
|
|
|
//printf("%d %d %d %d %d\n", s->pict_type, s->bit_rate, s->inter_intra_pred, s->width, s->height);
|
2002-06-18 02:49:00 +02:00
|
|
|
|
|
|
|
s->esc3_level_length= 0;
|
|
|
|
s->esc3_run_length= 0;
|
2002-04-05 06:09:04 +02:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-15 12:05:06 +01:00
|
|
|
int ff_msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size)
|
2002-01-15 23:22:41 +01:00
|
|
|
{
|
2002-06-02 14:22:30 +02:00
|
|
|
int left= buf_size*8 - get_bits_count(&s->gb);
|
|
|
|
int length= s->msmpeg4_version>=3 ? 17 : 16;
|
2002-01-15 23:22:41 +01:00
|
|
|
/* the alt_bitstream reader could read over the end so we need to check it */
|
2002-06-02 14:22:30 +02:00
|
|
|
if(left>=length && left<length+8)
|
2002-01-15 23:22:41 +01:00
|
|
|
{
|
2011-06-01 18:26:27 +02:00
|
|
|
skip_bits(&s->gb, 5); /* fps */
|
2002-07-22 10:15:27 +02:00
|
|
|
s->bit_rate= get_bits(&s->gb, 11)*1024;
|
2002-06-02 14:22:30 +02:00
|
|
|
if(s->msmpeg4_version>=3)
|
|
|
|
s->flipflop_rounding= get_bits1(&s->gb);
|
|
|
|
else
|
|
|
|
s->flipflop_rounding= 0;
|
2002-02-22 20:19:01 +01:00
|
|
|
|
2002-07-22 10:15:27 +02:00
|
|
|
// printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bit_rate/1024, s->flipflop_rounding);
|
2002-01-15 23:22:41 +01:00
|
|
|
}
|
2002-06-02 14:22:30 +02:00
|
|
|
else if(left<length+8)
|
2002-01-15 23:22:41 +01:00
|
|
|
{
|
2002-02-22 20:19:01 +01:00
|
|
|
s->flipflop_rounding= 0;
|
2003-08-10 23:11:45 +02:00
|
|
|
if(s->msmpeg4_version != 2)
|
2003-11-03 14:26:22 +01:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "ext header missing, %d left\n", left);
|
2002-06-02 14:22:30 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-11-03 14:26:22 +01:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "I frame too long, ignoring ext header\n");
|
2002-01-15 23:22:41 +01:00
|
|
|
}
|
2002-02-22 20:19:01 +01:00
|
|
|
|
2002-01-15 23:22:41 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-30 15:15:12 +01:00
|
|
|
static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
|
2002-04-05 23:04:09 +02:00
|
|
|
{
|
2009-12-30 15:15:12 +01:00
|
|
|
int level, pred;
|
2002-04-05 23:04:09 +02:00
|
|
|
|
2009-12-30 15:15:12 +01:00
|
|
|
if(s->msmpeg4_version<=2){
|
|
|
|
if (n < 4) {
|
|
|
|
level = get_vlc2(&s->gb, v2_dc_lum_vlc.table, DC_VLC_BITS, 3);
|
2002-04-05 23:04:09 +02:00
|
|
|
} else {
|
2009-12-30 15:15:12 +01:00
|
|
|
level = get_vlc2(&s->gb, v2_dc_chroma_vlc.table, DC_VLC_BITS, 3);
|
2002-06-02 14:22:30 +02:00
|
|
|
}
|
2009-12-30 15:15:12 +01:00
|
|
|
if (level < 0)
|
2002-06-02 14:22:30 +02:00
|
|
|
return -1;
|
2009-12-30 15:15:12 +01:00
|
|
|
level-=256;
|
|
|
|
}else{ //FIXME optimize use unified tables & index
|
|
|
|
if (n < 4) {
|
|
|
|
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, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
|
2002-06-02 14:22:30 +02:00
|
|
|
}
|
2009-12-30 15:15:12 +01:00
|
|
|
if (level < 0){
|
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
|
2002-06-02 14:22:30 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2002-04-05 06:09:04 +02:00
|
|
|
|
2009-12-30 15:15:12 +01:00
|
|
|
if (level == DC_MAX) {
|
|
|
|
level = get_bits(&s->gb, 8);
|
|
|
|
if (get_bits1(&s->gb))
|
|
|
|
level = -level;
|
|
|
|
} else if (level != 0) {
|
|
|
|
if (get_bits1(&s->gb))
|
|
|
|
level = -level;
|
2005-12-22 02:10:11 +01:00
|
|
|
}
|
2002-04-05 06:09:04 +02:00
|
|
|
}
|
2003-04-10 15:18:38 +02:00
|
|
|
|
2009-12-30 15:15:12 +01:00
|
|
|
if(s->msmpeg4_version==1){
|
|
|
|
int32_t *dc_val;
|
|
|
|
pred = msmpeg4v1_pred_dc(s, n, &dc_val);
|
|
|
|
level += pred;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2009-12-30 15:15:12 +01:00
|
|
|
/* update predictor */
|
|
|
|
*dc_val= level;
|
|
|
|
}else{
|
|
|
|
int16_t *dc_val;
|
2012-02-18 14:28:43 +01:00
|
|
|
pred = ff_msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
|
2009-12-30 15:15:12 +01:00
|
|
|
level += pred;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2009-12-30 15:15:12 +01:00
|
|
|
/* update predictor */
|
|
|
|
if (n < 4) {
|
|
|
|
*dc_val = level * s->y_dc_scale;
|
|
|
|
} else {
|
|
|
|
*dc_val = level * s->c_dc_scale;
|
2005-12-22 02:10:11 +01:00
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2009-12-30 15:15:12 +01:00
|
|
|
return level;
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
2009-12-30 15:15:12 +01:00
|
|
|
|
2002-07-17 11:15:14 +02:00
|
|
|
//#define ERROR_DETAILS
|
2007-11-08 00:23:35 +01:00
|
|
|
int ff_msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
|
2002-12-28 00:51:46 +01:00
|
|
|
int n, int coded, const uint8_t *scan_table)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
2002-07-13 16:55:12 +02:00
|
|
|
int level, i, last, run, run_diff;
|
2009-02-09 23:04:34 +01:00
|
|
|
int av_uninit(dc_pred_dir);
|
2001-07-22 16:18:56 +02:00
|
|
|
RLTable *rl;
|
2002-07-13 16:55:12 +02:00
|
|
|
RL_VLC_ELEM *rl_vlc;
|
2002-01-13 05:59:37 +01:00
|
|
|
int qmul, qadd;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
if (s->mb_intra) {
|
2002-01-13 05:59:37 +01:00
|
|
|
qmul=1;
|
|
|
|
qadd=0;
|
|
|
|
|
2005-12-22 02:10:11 +01:00
|
|
|
/* DC coef */
|
2001-07-22 16:18:56 +02:00
|
|
|
level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2002-06-02 14:22:30 +02:00
|
|
|
if (level < 0){
|
2003-11-03 14:26:22 +01:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "dc overflow- block: %d qscale: %d//\n", n, s->qscale);
|
2002-07-07 10:34:46 +02:00
|
|
|
if(s->inter_intra_pred) level=0;
|
|
|
|
else return -1;
|
2002-06-02 14:22:30 +02:00
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
if (n < 4) {
|
2012-02-15 11:56:41 +01:00
|
|
|
rl = &ff_rl_table[s->rl_table_index];
|
2002-06-02 14:22:30 +02:00
|
|
|
if(level > 256*s->y_dc_scale){
|
2003-11-03 14:26:22 +01:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ L qscale: %d//\n", s->qscale);
|
2002-07-07 10:34:46 +02:00
|
|
|
if(!s->inter_intra_pred) return -1;
|
2002-06-02 14:22:30 +02:00
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
} else {
|
2012-02-15 11:56:41 +01:00
|
|
|
rl = &ff_rl_table[3 + s->rl_chroma_table_index];
|
2002-06-02 14:22:30 +02:00
|
|
|
if(level > 256*s->c_dc_scale){
|
2003-11-03 14:26:22 +01:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ C qscale: %d//\n", s->qscale);
|
2002-07-07 10:34:46 +02:00
|
|
|
if(!s->inter_intra_pred) return -1;
|
2002-06-02 14:22:30 +02:00
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
2002-06-02 14:22:30 +02:00
|
|
|
block[0] = level;
|
2002-01-13 05:59:37 +01:00
|
|
|
|
2007-08-05 22:03:37 +02:00
|
|
|
run_diff = s->msmpeg4_version >= 4;
|
2002-07-13 16:55:12 +02:00
|
|
|
i = 0;
|
2001-07-22 16:18:56 +02:00
|
|
|
if (!coded) {
|
|
|
|
goto not_coded;
|
|
|
|
}
|
|
|
|
if (s->ac_pred) {
|
2005-12-17 19:14:38 +01:00
|
|
|
if (dc_pred_dir == 0)
|
2002-09-30 00:44:22 +02:00
|
|
|
scan_table = s->intra_v_scantable.permutated; /* left */
|
2001-07-22 16:18:56 +02:00
|
|
|
else
|
2002-09-30 00:44:22 +02:00
|
|
|
scan_table = s->intra_h_scantable.permutated; /* top */
|
2001-07-22 16:18:56 +02:00
|
|
|
} else {
|
2002-09-30 00:44:22 +02:00
|
|
|
scan_table = s->intra_scantable.permutated;
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
2002-07-13 16:55:12 +02:00
|
|
|
rl_vlc= rl->rl_vlc[0];
|
2001-07-22 16:18:56 +02:00
|
|
|
} else {
|
2002-01-13 05:59:37 +01:00
|
|
|
qmul = s->qscale << 1;
|
|
|
|
qadd = (s->qscale - 1) | 1;
|
2002-07-13 16:55:12 +02:00
|
|
|
i = -1;
|
2012-02-15 11:56:41 +01:00
|
|
|
rl = &ff_rl_table[3 + s->rl_table_index];
|
2002-04-05 06:09:04 +02:00
|
|
|
|
|
|
|
if(s->msmpeg4_version==2)
|
|
|
|
run_diff = 0;
|
|
|
|
else
|
|
|
|
run_diff = 1;
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
if (!coded) {
|
2002-07-13 16:55:12 +02:00
|
|
|
s->block_last_index[n] = i;
|
2001-07-22 16:18:56 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2002-12-28 00:51:46 +01:00
|
|
|
if(!scan_table)
|
|
|
|
scan_table = s->inter_scantable.permutated;
|
2002-07-13 16:55:12 +02:00
|
|
|
rl_vlc= rl->rl_vlc[s->qscale];
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
2002-07-13 16:55:12 +02:00
|
|
|
{
|
|
|
|
OPEN_READER(re, &s->gb);
|
2001-07-22 16:18:56 +02:00
|
|
|
for(;;) {
|
2002-07-13 16:55:12 +02:00
|
|
|
UPDATE_CACHE(re, &s->gb);
|
2005-04-18 22:07:48 +02:00
|
|
|
GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
|
2002-07-13 16:55:12 +02:00
|
|
|
if (level==0) {
|
|
|
|
int cache;
|
|
|
|
cache= GET_CACHE(re, &s->gb);
|
2001-07-22 16:18:56 +02:00
|
|
|
/* escape */
|
2002-07-13 16:55:12 +02:00
|
|
|
if (s->msmpeg4_version==1 || (cache&0x80000000)==0) {
|
|
|
|
if (s->msmpeg4_version==1 || (cache&0x40000000)==0) {
|
2001-07-22 16:18:56 +02:00
|
|
|
/* third escape */
|
2002-07-13 16:55:12 +02:00
|
|
|
if(s->msmpeg4_version!=1) LAST_SKIP_BITS(re, &s->gb, 2);
|
|
|
|
UPDATE_CACHE(re, &s->gb);
|
2002-06-18 02:49:00 +02:00
|
|
|
if(s->msmpeg4_version<=3){
|
2002-07-13 16:55:12 +02:00
|
|
|
last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
|
|
|
|
run= SHOW_UBITS(re, &s->gb, 6); SKIP_CACHE(re, &s->gb, 6);
|
2011-12-17 20:40:16 +01:00
|
|
|
level= SHOW_SBITS(re, &s->gb, 8);
|
2002-07-13 16:55:12 +02:00
|
|
|
SKIP_COUNTER(re, &s->gb, 1+6+8);
|
2005-12-17 19:14:38 +01:00
|
|
|
}else{
|
2002-06-18 02:49:00 +02:00
|
|
|
int sign;
|
2002-07-13 16:55:12 +02:00
|
|
|
last= SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
|
2002-06-18 02:49:00 +02:00
|
|
|
if(!s->esc3_level_length){
|
|
|
|
int ll;
|
|
|
|
//printf("ESC-3 %X at %d %d\n", show_bits(&s->gb, 24), s->mb_x, s->mb_y);
|
|
|
|
if(s->qscale<8){
|
2002-07-13 16:55:12 +02:00
|
|
|
ll= SHOW_UBITS(re, &s->gb, 3); SKIP_BITS(re, &s->gb, 3);
|
2002-06-18 02:49:00 +02:00
|
|
|
if(ll==0){
|
2010-02-04 15:26:57 +01:00
|
|
|
ll= 8+SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
|
2002-06-18 02:49:00 +02:00
|
|
|
}
|
|
|
|
}else{
|
|
|
|
ll=2;
|
2002-07-13 16:55:12 +02:00
|
|
|
while(ll<8 && SHOW_UBITS(re, &s->gb, 1)==0){
|
|
|
|
ll++;
|
|
|
|
SKIP_BITS(re, &s->gb, 1);
|
|
|
|
}
|
2002-07-22 10:15:27 +02:00
|
|
|
if(ll<8) SKIP_BITS(re, &s->gb, 1);
|
2002-06-18 02:49:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
s->esc3_level_length= ll;
|
2002-07-13 16:55:12 +02:00
|
|
|
s->esc3_run_length= SHOW_UBITS(re, &s->gb, 2) + 3; SKIP_BITS(re, &s->gb, 2);
|
2002-06-18 02:49:00 +02:00
|
|
|
//printf("level length:%d, run length: %d\n", ll, s->esc3_run_length);
|
2002-07-22 10:15:27 +02:00
|
|
|
UPDATE_CACHE(re, &s->gb);
|
2002-06-18 02:49:00 +02:00
|
|
|
}
|
2005-12-17 19:14:38 +01:00
|
|
|
run= SHOW_UBITS(re, &s->gb, s->esc3_run_length);
|
2002-07-13 16:55:12 +02:00
|
|
|
SKIP_BITS(re, &s->gb, s->esc3_run_length);
|
2005-12-17 19:14:38 +01:00
|
|
|
|
|
|
|
sign= SHOW_UBITS(re, &s->gb, 1);
|
2002-07-13 16:55:12 +02:00
|
|
|
SKIP_BITS(re, &s->gb, 1);
|
2005-12-17 19:14:38 +01:00
|
|
|
|
|
|
|
level= SHOW_UBITS(re, &s->gb, s->esc3_level_length);
|
2002-07-13 16:55:12 +02:00
|
|
|
SKIP_BITS(re, &s->gb, s->esc3_level_length);
|
2002-06-18 02:49:00 +02:00
|
|
|
if(sign) level= -level;
|
|
|
|
}
|
|
|
|
//printf("level: %d, run: %d at %d %d\n", level, run, s->mb_x, s->mb_y);
|
2002-06-02 14:22:30 +02:00
|
|
|
#if 0 // waste of time / this will detect very few errors
|
|
|
|
{
|
2006-10-12 01:17:58 +02:00
|
|
|
const int abs_level= FFABS(level);
|
2002-06-02 14:22:30 +02:00
|
|
|
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]){
|
2006-03-08 12:43:10 +01:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
|
2002-06-02 14:22:30 +02:00
|
|
|
return DECODING_AC_LOST;
|
|
|
|
}
|
|
|
|
if(abs_level <= rl->max_level[last][run]*2){
|
2006-03-08 12:43:10 +01:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
|
2002-06-02 14:22:30 +02:00
|
|
|
return DECODING_AC_LOST;
|
|
|
|
}
|
2002-06-18 02:49:00 +02:00
|
|
|
if(run1>=0 && abs_level <= rl->max_level[last][run1]){
|
2006-03-08 12:43:10 +01:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
|
2002-06-02 14:22:30 +02:00
|
|
|
return DECODING_AC_LOST;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2005-12-22 02:10:11 +01:00
|
|
|
//level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
|
|
|
|
if (level>0) level= level * qmul + qadd;
|
2002-06-02 14:22:30 +02:00
|
|
|
else level= level * qmul - qadd;
|
|
|
|
#if 0 // waste of time too :(
|
|
|
|
if(level>2048 || level<-2048){
|
2006-03-08 12:43:10 +01:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "|level| overflow in 3. esc\n");
|
2002-06-02 14:22:30 +02:00
|
|
|
return DECODING_AC_LOST;
|
|
|
|
}
|
|
|
|
#endif
|
2002-07-13 16:55:12 +02:00
|
|
|
i+= run + 1;
|
|
|
|
if(last) i+=192;
|
2002-07-17 11:15:14 +02:00
|
|
|
#ifdef ERROR_DETAILS
|
|
|
|
if(run==66)
|
2005-08-26 21:05:44 +02:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC3 level=%d\n", level);
|
2002-07-17 11:15:14 +02:00
|
|
|
else if((i>62 && i<192) || i>192+63)
|
2005-08-26 21:05:44 +02:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC3 i=%d run=%d level=%d\n", i, run, level);
|
2002-07-17 11:15:14 +02:00
|
|
|
#endif
|
2001-07-22 16:18:56 +02:00
|
|
|
} else {
|
|
|
|
/* second escape */
|
2002-07-13 16:55:12 +02:00
|
|
|
SKIP_BITS(re, &s->gb, 2);
|
2005-04-18 22:07:48 +02:00
|
|
|
GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
|
2002-07-13 16:55:12 +02:00
|
|
|
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);
|
2002-07-17 11:15:14 +02:00
|
|
|
#ifdef ERROR_DETAILS
|
|
|
|
if(run==66)
|
2005-08-26 21:05:44 +02:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC2 level=%d\n", level);
|
2002-07-17 11:15:14 +02:00
|
|
|
else if((i>62 && i<192) || i>192+63)
|
2005-08-26 21:05:44 +02:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC2 i=%d run=%d level=%d\n", i, run, level);
|
2002-07-17 11:15:14 +02:00
|
|
|
#endif
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* first escape */
|
2002-07-13 16:55:12 +02:00
|
|
|
SKIP_BITS(re, &s->gb, 1);
|
2005-04-18 22:07:48 +02:00
|
|
|
GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
|
2002-07-13 16:55:12 +02:00
|
|
|
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);
|
2002-07-17 11:15:14 +02:00
|
|
|
#ifdef ERROR_DETAILS
|
|
|
|
if(run==66)
|
2005-08-26 21:05:44 +02:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC1 level=%d\n", level);
|
2002-07-17 11:15:14 +02:00
|
|
|
else if((i>62 && i<192) || i>192+63)
|
2005-08-26 21:05:44 +02:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC1 i=%d run=%d level=%d\n", i, run, level);
|
2002-07-17 11:15:14 +02:00
|
|
|
#endif
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
} else {
|
2002-07-13 16:55:12 +02:00
|
|
|
i+= run;
|
|
|
|
level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
|
|
|
|
LAST_SKIP_BITS(re, &s->gb, 1);
|
2002-07-17 11:15:14 +02:00
|
|
|
#ifdef ERROR_DETAILS
|
|
|
|
if(run==66)
|
2005-08-26 21:05:44 +02:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code level=%d\n", level);
|
2002-07-17 11:15:14 +02:00
|
|
|
else if((i>62 && i<192) || i>192+63)
|
2005-08-26 21:05:44 +02:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "run overflow i=%d run=%d level=%d\n", i, run, level);
|
2002-07-17 11:15:14 +02:00
|
|
|
#endif
|
2002-06-18 02:49:00 +02:00
|
|
|
}
|
2002-07-13 16:55:12 +02:00
|
|
|
if (i > 62){
|
|
|
|
i-= 192;
|
|
|
|
if(i&(~63)){
|
2009-11-16 18:42:43 +01:00
|
|
|
const int left= get_bits_left(&s->gb);
|
2011-12-13 01:26:28 +01:00
|
|
|
if(((i+192 == 64 && level/qmul==-1) || !(s->err_recognition&(AV_EF_BITSTREAM|AV_EF_COMPLIANT))) && left>=0){
|
2003-11-03 14:26:22 +01:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "ignoring overflow at %d %d\n", s->mb_x, s->mb_y);
|
2002-07-17 11:15:14 +02:00
|
|
|
break;
|
|
|
|
}else{
|
2003-11-03 14:26:22 +01:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
|
2002-07-17 11:15:14 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2002-07-13 16:55:12 +02:00
|
|
|
}
|
2002-06-02 14:22:30 +02:00
|
|
|
|
2002-07-13 16:55:12 +02:00
|
|
|
block[scan_table[i]] = level;
|
2001-07-22 16:18:56 +02:00
|
|
|
break;
|
2002-07-13 16:55:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
block[scan_table[i]] = level;
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
2002-07-13 16:55:12 +02:00
|
|
|
CLOSE_READER(re, &s->gb);
|
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
not_coded:
|
|
|
|
if (s->mb_intra) {
|
2012-02-15 11:36:50 +01:00
|
|
|
ff_mpeg4_pred_ac(s, block, n, dc_pred_dir);
|
2001-07-22 16:18:56 +02:00
|
|
|
if (s->ac_pred) {
|
2002-07-13 16:55:12 +02:00
|
|
|
i = 63; /* XXX: not optimal */
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
}
|
2002-12-28 00:51:46 +01:00
|
|
|
if(s->msmpeg4_version>=4 && i>0) i=63; //FIXME/XXX optimize
|
2002-07-13 16:55:12 +02:00
|
|
|
s->block_last_index[n] = i;
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-11-08 00:23:35 +01:00
|
|
|
int ff_msmpeg4_decode_motion(MpegEncContext * s,
|
2001-07-22 16:18:56 +02:00
|
|
|
int *mx_ptr, int *my_ptr)
|
|
|
|
{
|
|
|
|
MVTable *mv;
|
|
|
|
int code, mx, my;
|
|
|
|
|
2012-02-15 11:56:41 +01:00
|
|
|
mv = &ff_mv_tables[s->mv_table_index];
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2002-07-10 22:05:42 +02:00
|
|
|
code = get_vlc2(&s->gb, mv->vlc.table, MV_VLC_BITS, 2);
|
2002-06-18 02:49:00 +02:00
|
|
|
if (code < 0){
|
2003-11-03 14:26:22 +01:00
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "illegal MV code at %d %d\n", s->mb_x, s->mb_y);
|
2001-07-22 16:18:56 +02:00
|
|
|
return -1;
|
2002-06-18 02:49:00 +02:00
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
if (code == mv->n) {
|
2002-06-18 02:49:00 +02:00
|
|
|
//printf("MV ESC %X at %d %d\n", show_bits(&s->gb, 24), s->mb_x, s->mb_y);
|
2001-07-22 16:18:56 +02:00
|
|
|
mx = get_bits(&s->gb, 6);
|
|
|
|
my = get_bits(&s->gb, 6);
|
|
|
|
} else {
|
|
|
|
mx = mv->table_mvx[code];
|
|
|
|
my = mv->table_mvy[code];
|
|
|
|
}
|
|
|
|
|
|
|
|
mx += *mx_ptr - 32;
|
|
|
|
my += *my_ptr - 32;
|
|
|
|
/* WARNING : they do not do exactly modulo encoding */
|
|
|
|
if (mx <= -64)
|
|
|
|
mx += 64;
|
|
|
|
else if (mx >= 64)
|
|
|
|
mx -= 64;
|
|
|
|
|
|
|
|
if (my <= -64)
|
|
|
|
my += 64;
|
|
|
|
else if (my >= 64)
|
|
|
|
my -= 64;
|
|
|
|
*mx_ptr = mx;
|
|
|
|
*my_ptr = my;
|
|
|
|
return 0;
|
|
|
|
}
|
2010-01-08 20:40:10 +01:00
|
|
|
|
2011-01-25 22:40:11 +01:00
|
|
|
AVCodec ff_msmpeg4v1_decoder = {
|
2011-07-17 12:54:31 +02:00
|
|
|
.name = "msmpeg4v1",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.id = CODEC_ID_MSMPEG4V1,
|
|
|
|
.priv_data_size = sizeof(MpegEncContext),
|
|
|
|
.init = ff_msmpeg4_decode_init,
|
|
|
|
.close = ff_h263_decode_end,
|
|
|
|
.decode = ff_h263_decode_frame,
|
|
|
|
.capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
|
2012-04-06 18:19:39 +02:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 1"),
|
|
|
|
.pix_fmts = ff_pixfmt_list_420,
|
2010-01-08 20:40:10 +01:00
|
|
|
};
|
|
|
|
|
2011-01-25 22:40:11 +01:00
|
|
|
AVCodec ff_msmpeg4v2_decoder = {
|
2011-07-17 12:54:31 +02:00
|
|
|
.name = "msmpeg4v2",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.id = CODEC_ID_MSMPEG4V2,
|
|
|
|
.priv_data_size = sizeof(MpegEncContext),
|
|
|
|
.init = ff_msmpeg4_decode_init,
|
|
|
|
.close = ff_h263_decode_end,
|
|
|
|
.decode = ff_h263_decode_frame,
|
|
|
|
.capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
|
2012-04-06 18:19:39 +02:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
|
|
|
|
.pix_fmts = ff_pixfmt_list_420,
|
2010-01-08 20:40:10 +01:00
|
|
|
};
|
|
|
|
|
2011-01-25 22:40:11 +01:00
|
|
|
AVCodec ff_msmpeg4v3_decoder = {
|
2011-07-17 12:54:31 +02:00
|
|
|
.name = "msmpeg4",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.id = CODEC_ID_MSMPEG4V3,
|
|
|
|
.priv_data_size = sizeof(MpegEncContext),
|
|
|
|
.init = ff_msmpeg4_decode_init,
|
|
|
|
.close = ff_h263_decode_end,
|
|
|
|
.decode = ff_h263_decode_frame,
|
|
|
|
.capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
|
2012-04-06 18:19:39 +02:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
|
|
|
|
.pix_fmts = ff_pixfmt_list_420,
|
2010-01-08 20:40:10 +01:00
|
|
|
};
|
|
|
|
|
2011-01-25 22:40:11 +01:00
|
|
|
AVCodec ff_wmv1_decoder = {
|
2011-07-17 12:54:31 +02:00
|
|
|
.name = "wmv1",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.id = CODEC_ID_WMV1,
|
|
|
|
.priv_data_size = sizeof(MpegEncContext),
|
|
|
|
.init = ff_msmpeg4_decode_init,
|
|
|
|
.close = ff_h263_decode_end,
|
|
|
|
.decode = ff_h263_decode_frame,
|
|
|
|
.capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
|
2012-04-06 18:19:39 +02:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
|
|
|
|
.pix_fmts = ff_pixfmt_list_420,
|
2010-01-08 20:40:10 +01:00
|
|
|
};
|