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"
|
2014-01-24 11:55:16 +01:00
|
|
|
#include "idctdsp.h"
|
2001-07-22 16:18:56 +02:00
|
|
|
#include "mpegvideo.h"
|
2007-11-08 00:41:39 +01:00
|
|
|
#include "msmpeg4.h"
|
2012-08-08 14:51:52 +02:00
|
|
|
#include "libavutil/x86/asm.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"
|
2012-11-25 03:21:06 +01:00
|
|
|
#include "libavutil/imgutils.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
|
|
|
*/
|
|
|
|
|
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;
|
|
|
|
|
2012-10-27 21:30:01 +02:00
|
|
|
if(ff_v2_dc_chroma_table[255 + 256][1])
|
|
|
|
return;
|
|
|
|
|
2009-12-30 15:15:12 +01:00
|
|
|
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
|
|
|
{
|
|
|
|
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){
|
2014-01-24 11:55:16 +01:00
|
|
|
ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_wmv1_scantable[1]);
|
|
|
|
ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_wmv1_scantable[2]);
|
|
|
|
ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_wmv1_scantable[3]);
|
|
|
|
ff_init_scantable(s->idsp.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
|
|
|
|
2012-10-27 21:30:01 +02:00
|
|
|
init_h263_dc_for_msmpeg4();
|
2002-06-18 02:49:00 +02:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-06-24 03:44:30 +02:00
|
|
|
static int get_dc(uint8_t *src, int stride, int scale, int block_size)
|
2002-07-07 10:34:46 +02:00
|
|
|
{
|
|
|
|
int y;
|
|
|
|
int sum=0;
|
2013-06-24 03:44:30 +02:00
|
|
|
for(y=0; y<block_size; y++){
|
2002-07-07 10:34:46 +02:00
|
|
|
int x;
|
2013-06-24 03:44:30 +02:00
|
|
|
for(x=0; x<block_size; x++){
|
2002-07-07 10:34:46 +02:00
|
|
|
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"
|
2013-04-09 16:25:12 +02:00
|
|
|
"imull %4 \n\t"
|
2005-12-22 02:10:11 +01:00
|
|
|
"movl %%edx, %0 \n\t"
|
|
|
|
"movl %1, %%eax \n\t"
|
2013-04-09 16:25:12 +02:00
|
|
|
"imull %4 \n\t"
|
2005-12-22 02:10:11 +01:00
|
|
|
"movl %%edx, %1 \n\t"
|
|
|
|
"movl %2, %%eax \n\t"
|
2013-04-09 16:25:12 +02:00
|
|
|
"imull %4 \n\t"
|
2005-12-22 02:10:11 +01:00
|
|
|
"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
|
2013-11-13 20:39:56 +01:00
|
|
|
/* Divisions are costly everywhere; optimize the most common case. */
|
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{
|
2013-06-24 03:44:30 +02:00
|
|
|
int bs = 8 >> s->avctx->lowres;
|
2002-07-07 10:34:46 +02:00
|
|
|
if(n<4){
|
|
|
|
wrap= s->linesize;
|
2014-04-09 14:36:47 +02:00
|
|
|
dest= s->current_picture.f->data[0] + (((n >> 1) + 2*s->mb_y) * bs* wrap ) + ((n & 1) + 2*s->mb_x) * bs;
|
2002-07-07 10:34:46 +02:00
|
|
|
}else{
|
2002-07-15 16:15:10 +02:00
|
|
|
wrap= s->uvlinesize;
|
2014-04-09 14:36:47 +02:00
|
|
|
dest= s->current_picture.f->data[n - 3] + (s->mb_y * bs * wrap) + s->mb_x * bs;
|
2002-07-07 10:34:46 +02:00
|
|
|
}
|
|
|
|
if(s->mb_x==0) a= (1024 + (scale>>1))/scale;
|
2013-06-24 03:44:30 +02:00
|
|
|
else a= get_dc(dest-bs, wrap, scale*8>>(2*s->avctx->lowres), bs);
|
2002-07-07 10:34:46 +02:00
|
|
|
if(s->mb_y==0) c= (1024 + (scale>>1))/scale;
|
2013-06-24 03:44:30 +02:00
|
|
|
else c= get_dc(dest-bs*wrap, wrap, scale*8>>(2*s->avctx->lowres), bs);
|
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;
|
|
|
|
}
|
|
|
|
|