2001-07-22 16:18:56 +02:00
|
|
|
/*
|
|
|
|
* The simplest mpeg encoder (well, it was the simplest!)
|
2002-05-26 00:45:33 +02:00
|
|
|
* Copyright (c) 2000,2001 Fabrice Bellard.
|
2001-07-22 16:18:56 +02:00
|
|
|
*
|
2002-05-26 00:45:33 +02:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
2001-07-22 16:18:56 +02:00
|
|
|
*
|
2002-05-26 00:45:33 +02:00
|
|
|
* This library 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
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2002-03-28 05:25:35 +01:00
|
|
|
*
|
2002-04-17 18:30:31 +02:00
|
|
|
* 4MV & hq & b-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
|
2001-07-22 16:18:56 +02:00
|
|
|
*/
|
2002-09-27 12:27:57 +02:00
|
|
|
|
|
|
|
#include <ctype.h>
|
2001-07-22 16:18:56 +02:00
|
|
|
#include "avcodec.h"
|
|
|
|
#include "dsputil.h"
|
|
|
|
#include "mpegvideo.h"
|
2002-09-30 00:44:22 +02:00
|
|
|
#include "simple_idct.h"
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2001-08-02 10:29:38 +02:00
|
|
|
#ifdef USE_FASTMEMCPY
|
|
|
|
#include "fastmemcpy.h"
|
|
|
|
#endif
|
|
|
|
|
2002-09-30 00:44:22 +02:00
|
|
|
//#undef NDEBUG
|
|
|
|
//#include <assert.h>
|
|
|
|
|
2001-07-31 01:26:26 +02:00
|
|
|
static void encode_picture(MpegEncContext *s, int picture_number);
|
|
|
|
static void dct_unquantize_mpeg1_c(MpegEncContext *s,
|
|
|
|
DCTELEM *block, int n, int qscale);
|
2002-04-17 18:30:31 +02:00
|
|
|
static void dct_unquantize_mpeg2_c(MpegEncContext *s,
|
|
|
|
DCTELEM *block, int n, int qscale);
|
2001-07-31 01:26:26 +02:00
|
|
|
static void dct_unquantize_h263_c(MpegEncContext *s,
|
|
|
|
DCTELEM *block, int n, int qscale);
|
2002-01-15 05:07:29 +01:00
|
|
|
static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w);
|
2002-04-27 14:30:26 +02:00
|
|
|
static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
|
2002-01-15 05:07:29 +01:00
|
|
|
|
|
|
|
void (*draw_edges)(UINT8 *buf, int wrap, int width, int height, int w)= draw_edges_c;
|
2002-07-17 14:42:50 +02:00
|
|
|
static void emulated_edge_mc(MpegEncContext *s, UINT8 *src, int linesize, int block_w, int block_h,
|
|
|
|
int src_x, int src_y, int w, int h);
|
2002-01-15 05:07:29 +01:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
#define EDGE_WIDTH 16
|
|
|
|
|
|
|
|
/* enable all paranoid tests for rounding, overflows, etc... */
|
|
|
|
//#define PARANOID
|
|
|
|
|
|
|
|
//#define DEBUG
|
|
|
|
|
2002-04-15 00:17:40 +02:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
/* for jpeg fast DCT */
|
|
|
|
#define CONST_BITS 14
|
|
|
|
|
|
|
|
static const unsigned short aanscales[64] = {
|
|
|
|
/* precomputed values scaled up by 14 bits */
|
|
|
|
16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
|
|
|
|
22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
|
|
|
|
21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
|
|
|
|
19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
|
|
|
|
16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
|
|
|
|
12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
|
|
|
|
8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
|
|
|
|
4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
|
|
|
|
};
|
|
|
|
|
|
|
|
static UINT8 h263_chroma_roundtab[16] = {
|
|
|
|
0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
|
|
|
|
};
|
|
|
|
|
2002-03-25 00:45:13 +01:00
|
|
|
static UINT16 default_mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
|
2002-03-22 03:21:17 +01:00
|
|
|
static UINT8 default_fcode_tab[MAX_MV*2+1];
|
|
|
|
|
2002-04-15 00:17:40 +02:00
|
|
|
/* default motion estimation */
|
|
|
|
int motion_estimation_method = ME_EPZS;
|
|
|
|
|
2002-08-30 01:55:32 +02:00
|
|
|
static void convert_matrix(MpegEncContext *s, int (*qmat)[64], uint16_t (*qmat16)[64], uint16_t (*qmat16_bias)[64],
|
2002-09-30 11:19:54 +02:00
|
|
|
const UINT16 *quant_matrix, int bias, int qmin, int qmax)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
2002-04-27 14:30:26 +02:00
|
|
|
int qscale;
|
|
|
|
|
2002-09-30 11:19:54 +02:00
|
|
|
for(qscale=qmin; qscale<=qmax; qscale++){
|
2002-04-27 14:30:26 +02:00
|
|
|
int i;
|
2002-08-30 01:55:32 +02:00
|
|
|
if (s->fdct == ff_jpeg_fdct_islow) {
|
|
|
|
for(i=0;i<64;i++) {
|
2002-09-30 00:44:22 +02:00
|
|
|
const int j= s->idct_permutation[i];
|
2002-08-30 01:55:32 +02:00
|
|
|
/* 16 <= qscale * quant_matrix[i] <= 7905 */
|
|
|
|
/* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
|
|
|
|
/* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
|
|
|
|
/* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
|
|
|
|
|
2002-09-01 18:52:33 +02:00
|
|
|
qmat[qscale][j] = (int)((UINT64_C(1) << QMAT_SHIFT) /
|
2002-08-30 01:55:32 +02:00
|
|
|
(qscale * quant_matrix[j]));
|
|
|
|
}
|
|
|
|
} else if (s->fdct == fdct_ifast) {
|
2002-04-27 14:30:26 +02:00
|
|
|
for(i=0;i<64;i++) {
|
2002-09-30 00:44:22 +02:00
|
|
|
const int j= s->idct_permutation[i];
|
2002-04-27 14:30:26 +02:00
|
|
|
/* 16 <= qscale * quant_matrix[i] <= 7905 */
|
|
|
|
/* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
|
|
|
|
/* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
|
|
|
|
/* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
|
|
|
|
|
2002-09-01 18:52:33 +02:00
|
|
|
qmat[qscale][j] = (int)((UINT64_C(1) << (QMAT_SHIFT + 14)) /
|
2002-04-27 14:30:26 +02:00
|
|
|
(aanscales[i] * qscale * quant_matrix[j]));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for(i=0;i<64;i++) {
|
2002-09-30 00:44:22 +02:00
|
|
|
const int j= s->idct_permutation[i];
|
2002-04-27 14:30:26 +02:00
|
|
|
/* We can safely suppose that 16 <= quant_matrix[i] <= 255
|
|
|
|
So 16 <= qscale * quant_matrix[i] <= 7905
|
|
|
|
so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
|
|
|
|
so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67
|
|
|
|
*/
|
|
|
|
qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
|
2002-09-30 00:44:22 +02:00
|
|
|
qmat16[qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[j]);
|
2002-04-27 14:30:26 +02:00
|
|
|
|
|
|
|
if(qmat16[qscale][i]==0 || qmat16[qscale][i]==128*256) qmat16[qscale][i]=128*256-1;
|
|
|
|
qmat16_bias[qscale][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][i]);
|
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-06-02 14:20:39 +02:00
|
|
|
// move into common.c perhaps
|
|
|
|
#define CHECKED_ALLOCZ(p, size)\
|
|
|
|
{\
|
|
|
|
p= av_mallocz(size);\
|
|
|
|
if(p==NULL){\
|
|
|
|
perror("malloc");\
|
|
|
|
goto fail;\
|
|
|
|
}\
|
|
|
|
}
|
2002-09-30 00:44:22 +02:00
|
|
|
/*
|
|
|
|
static void build_end(void)
|
|
|
|
{
|
|
|
|
int lastIndex;
|
|
|
|
int lastIndexAfterPerm=0;
|
|
|
|
for(lastIndex=0; lastIndex<64; lastIndex++)
|
|
|
|
{
|
|
|
|
if(ff_zigzag_direct[lastIndex] > lastIndexAfterPerm)
|
|
|
|
lastIndexAfterPerm= ff_zigzag_direct[lastIndex];
|
|
|
|
zigzag_end[lastIndex]= lastIndexAfterPerm + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
void ff_init_scantable(MpegEncContext *s, ScanTable *st, const UINT8 *src_scantable){
|
|
|
|
int i;
|
|
|
|
int end;
|
|
|
|
|
|
|
|
for(i=0; i<64; i++){
|
|
|
|
int j;
|
|
|
|
j = src_scantable[i];
|
|
|
|
st->permutated[i] = s->idct_permutation[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
end=-1;
|
|
|
|
for(i=0; i<64; i++){
|
|
|
|
int j;
|
|
|
|
j = st->permutated[i];
|
|
|
|
if(j>end) end=j;
|
|
|
|
st->raster_end[i]= end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX: those functions should be suppressed ASAP when all IDCTs are
|
|
|
|
converted */
|
|
|
|
static void ff_jref_idct_put(UINT8 *dest, int line_size, DCTELEM *block)
|
|
|
|
{
|
|
|
|
j_rev_dct (block);
|
|
|
|
put_pixels_clamped(block, dest, line_size);
|
|
|
|
}
|
|
|
|
static void ff_jref_idct_add(UINT8 *dest, int line_size, DCTELEM *block)
|
|
|
|
{
|
|
|
|
j_rev_dct (block);
|
|
|
|
add_pixels_clamped(block, dest, line_size);
|
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
/* init common structure for both encoder and decoder */
|
|
|
|
int MPV_common_init(MpegEncContext *s)
|
|
|
|
{
|
|
|
|
int c_size, i;
|
|
|
|
UINT8 *pict;
|
|
|
|
|
2002-04-07 04:03:32 +02:00
|
|
|
s->dct_unquantize_h263 = dct_unquantize_h263_c;
|
2002-04-17 18:30:31 +02:00
|
|
|
s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_c;
|
|
|
|
s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_c;
|
2002-08-30 01:55:32 +02:00
|
|
|
s->dct_quantize= dct_quantize_c;
|
|
|
|
|
|
|
|
if(s->avctx->dct_algo==FF_DCT_FASTINT)
|
|
|
|
s->fdct = fdct_ifast;
|
|
|
|
else
|
2002-09-30 00:44:22 +02:00
|
|
|
s->fdct = ff_jpeg_fdct_islow; //slow/accurate/default
|
|
|
|
|
|
|
|
if(s->avctx->idct_algo==FF_IDCT_INT){
|
|
|
|
s->idct_put= ff_jref_idct_put;
|
|
|
|
s->idct_add= ff_jref_idct_add;
|
|
|
|
for(i=0; i<64; i++)
|
|
|
|
s->idct_permutation[i]= (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2);
|
|
|
|
}else{ //accurate/default
|
|
|
|
s->idct_put= simple_idct_put;
|
|
|
|
s->idct_add= simple_idct_add;
|
|
|
|
for(i=0; i<64; i++)
|
|
|
|
s->idct_permutation[i]= i;
|
|
|
|
}
|
2001-07-31 01:26:26 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_MMX
|
|
|
|
MPV_common_init_mmx(s);
|
2002-07-03 05:01:06 +02:00
|
|
|
#endif
|
|
|
|
#ifdef ARCH_ALPHA
|
|
|
|
MPV_common_init_axp(s);
|
2001-07-30 11:04:34 +02:00
|
|
|
#endif
|
2002-08-30 20:17:20 +02:00
|
|
|
#ifdef HAVE_MLIB
|
|
|
|
MPV_common_init_mlib(s);
|
|
|
|
#endif
|
2002-09-30 00:44:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* load & permutate scantables
|
|
|
|
note: only wmv uses differnt ones
|
|
|
|
*/
|
|
|
|
ff_init_scantable(s, &s->inter_scantable , ff_zigzag_direct);
|
|
|
|
ff_init_scantable(s, &s->intra_scantable , ff_zigzag_direct);
|
|
|
|
ff_init_scantable(s, &s->intra_h_scantable, ff_alternate_horizontal_scan);
|
|
|
|
ff_init_scantable(s, &s->intra_v_scantable, ff_alternate_vertical_scan);
|
2002-08-02 22:27:46 +02:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
s->mb_width = (s->width + 15) / 16;
|
|
|
|
s->mb_height = (s->height + 15) / 16;
|
2002-07-25 22:22:36 +02:00
|
|
|
|
|
|
|
/* set default edge pos, will be overriden in decode_header if needed */
|
|
|
|
s->h_edge_pos= s->mb_width*16;
|
|
|
|
s->v_edge_pos= s->mb_height*16;
|
2002-09-27 12:27:57 +02:00
|
|
|
|
|
|
|
/* convert fourcc to upper case */
|
|
|
|
s->avctx->fourcc= toupper( s->avctx->fourcc &0xFF)
|
|
|
|
+ (toupper((s->avctx->fourcc>>8 )&0xFF)<<8 )
|
|
|
|
+ (toupper((s->avctx->fourcc>>16)&0xFF)<<16)
|
|
|
|
+ (toupper((s->avctx->fourcc>>24)&0xFF)<<24);
|
2002-07-25 22:22:36 +02:00
|
|
|
|
2002-02-10 07:10:50 +01:00
|
|
|
s->mb_num = s->mb_width * s->mb_height;
|
2002-07-14 20:37:35 +02:00
|
|
|
if(!(s->flags&CODEC_FLAG_DR1)){
|
2002-07-15 16:15:10 +02:00
|
|
|
s->linesize = s->mb_width * 16 + 2 * EDGE_WIDTH;
|
|
|
|
s->uvlinesize = s->mb_width * 8 + EDGE_WIDTH;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2002-07-14 20:37:35 +02:00
|
|
|
for(i=0;i<3;i++) {
|
2001-07-22 16:18:56 +02:00
|
|
|
int w, h, shift, pict_start;
|
|
|
|
|
|
|
|
w = s->linesize;
|
|
|
|
h = s->mb_height * 16 + 2 * EDGE_WIDTH;
|
|
|
|
shift = (i == 0) ? 0 : 1;
|
2002-07-15 16:15:10 +02:00
|
|
|
c_size = (s->linesize>>shift) * (h >> shift);
|
|
|
|
pict_start = (s->linesize>>shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift);
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2002-06-02 14:20:39 +02:00
|
|
|
CHECKED_ALLOCZ(pict, c_size)
|
2001-07-22 16:18:56 +02:00
|
|
|
s->last_picture_base[i] = pict;
|
|
|
|
s->last_picture[i] = pict + pict_start;
|
2002-06-09 15:35:56 +02:00
|
|
|
if(i>0) memset(s->last_picture_base[i], 128, c_size);
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2002-06-02 14:20:39 +02:00
|
|
|
CHECKED_ALLOCZ(pict, c_size)
|
2001-07-22 16:18:56 +02:00
|
|
|
s->next_picture_base[i] = pict;
|
|
|
|
s->next_picture[i] = pict + pict_start;
|
2002-06-09 15:35:56 +02:00
|
|
|
if(i>0) memset(s->next_picture_base[i], 128, c_size);
|
2002-04-17 06:32:12 +02:00
|
|
|
|
2002-05-06 15:15:05 +02:00
|
|
|
if (s->has_b_frames || s->codec_id==CODEC_ID_MPEG4) {
|
|
|
|
/* Note the MPEG4 stuff is here cuz of buggy encoders which dont set the low_delay flag but
|
|
|
|
do low-delay encoding, so we cant allways distinguish b-frame containing streams from low_delay streams */
|
2002-06-02 14:20:39 +02:00
|
|
|
CHECKED_ALLOCZ(pict, c_size)
|
2001-07-22 16:18:56 +02:00
|
|
|
s->aux_picture_base[i] = pict;
|
|
|
|
s->aux_picture[i] = pict + pict_start;
|
2002-06-09 15:35:56 +02:00
|
|
|
if(i>0) memset(s->aux_picture_base[i], 128, c_size);
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
2002-07-14 20:37:35 +02:00
|
|
|
}
|
2002-07-15 16:15:10 +02:00
|
|
|
s->ip_buffer_count= 2;
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
2002-02-10 02:56:50 +01:00
|
|
|
|
2002-07-17 15:25:01 +02:00
|
|
|
CHECKED_ALLOCZ(s->edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance
|
2002-07-14 20:37:35 +02:00
|
|
|
|
2002-02-10 02:56:50 +01:00
|
|
|
if (s->encoding) {
|
2002-04-17 06:32:12 +02:00
|
|
|
int j;
|
|
|
|
int mv_table_size= (s->mb_width+2)*(s->mb_height+2);
|
2002-02-15 21:46:37 +01:00
|
|
|
|
2002-06-02 14:20:39 +02:00
|
|
|
CHECKED_ALLOCZ(s->mb_var , s->mb_num * sizeof(INT16))
|
|
|
|
CHECKED_ALLOCZ(s->mc_mb_var, s->mb_num * sizeof(INT16))
|
2002-09-23 16:56:11 +02:00
|
|
|
CHECKED_ALLOCZ(s->mb_mean , s->mb_num * sizeof(INT8))
|
2002-04-17 06:32:12 +02:00
|
|
|
|
|
|
|
/* Allocate MV tables */
|
2002-06-02 14:20:39 +02:00
|
|
|
CHECKED_ALLOCZ(s->p_mv_table , mv_table_size * 2 * sizeof(INT16))
|
|
|
|
CHECKED_ALLOCZ(s->b_forw_mv_table , mv_table_size * 2 * sizeof(INT16))
|
|
|
|
CHECKED_ALLOCZ(s->b_back_mv_table , mv_table_size * 2 * sizeof(INT16))
|
|
|
|
CHECKED_ALLOCZ(s->b_bidir_forw_mv_table , mv_table_size * 2 * sizeof(INT16))
|
|
|
|
CHECKED_ALLOCZ(s->b_bidir_back_mv_table , mv_table_size * 2 * sizeof(INT16))
|
|
|
|
CHECKED_ALLOCZ(s->b_direct_forw_mv_table, mv_table_size * 2 * sizeof(INT16))
|
|
|
|
CHECKED_ALLOCZ(s->b_direct_back_mv_table, mv_table_size * 2 * sizeof(INT16))
|
|
|
|
CHECKED_ALLOCZ(s->b_direct_mv_table , mv_table_size * 2 * sizeof(INT16))
|
|
|
|
|
|
|
|
CHECKED_ALLOCZ(s->me_scratchpad, s->linesize*16*3*sizeof(uint8_t))
|
|
|
|
|
|
|
|
CHECKED_ALLOCZ(s->me_map , ME_MAP_SIZE*sizeof(uint32_t))
|
|
|
|
CHECKED_ALLOCZ(s->me_score_map, ME_MAP_SIZE*sizeof(uint16_t))
|
2002-04-19 05:25:20 +02:00
|
|
|
|
2002-04-17 06:32:12 +02:00
|
|
|
if(s->max_b_frames){
|
|
|
|
for(j=0; j<REORDER_BUFFER_SIZE; j++){
|
|
|
|
int i;
|
|
|
|
for(i=0;i<3;i++) {
|
|
|
|
int w, h, shift;
|
|
|
|
|
|
|
|
w = s->linesize;
|
|
|
|
h = s->mb_height * 16;
|
|
|
|
shift = (i == 0) ? 0 : 1;
|
|
|
|
c_size = (w >> shift) * (h >> shift);
|
|
|
|
|
2002-06-02 14:20:39 +02:00
|
|
|
CHECKED_ALLOCZ(pict, c_size);
|
2002-04-17 06:32:12 +02:00
|
|
|
s->picture_buffer[j][i] = pict;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-06-02 14:20:39 +02:00
|
|
|
|
|
|
|
if(s->codec_id==CODEC_ID_MPEG4){
|
|
|
|
CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE);
|
|
|
|
CHECKED_ALLOCZ( s->pb2_buffer, PB_BUFFER_SIZE);
|
|
|
|
}
|
2002-08-25 23:19:50 +02:00
|
|
|
|
2002-09-11 09:26:43 +02:00
|
|
|
if(s->msmpeg4_version){
|
|
|
|
CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));
|
|
|
|
}
|
2002-08-25 23:19:50 +02:00
|
|
|
CHECKED_ALLOCZ(s->avctx->stats_out, 256);
|
2002-02-10 02:56:50 +01:00
|
|
|
}
|
|
|
|
|
2002-03-24 05:16:51 +01:00
|
|
|
if (s->out_format == FMT_H263 || s->encoding) {
|
2001-07-22 16:18:56 +02:00
|
|
|
int size;
|
2002-06-02 14:20:39 +02:00
|
|
|
/* Allocate MB type table */
|
|
|
|
CHECKED_ALLOCZ(s->mb_type , s->mb_num * sizeof(UINT8))
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
/* MV prediction */
|
|
|
|
size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
|
2002-06-02 14:20:39 +02:00
|
|
|
CHECKED_ALLOCZ(s->motion_val, size * 2 * sizeof(INT16));
|
2002-09-13 11:45:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(s->codec_id==CODEC_ID_MPEG4){
|
|
|
|
/* 4mv and interlaced direct mode decoding tables */
|
|
|
|
CHECKED_ALLOCZ(s->co_located_type_table, s->mb_num * sizeof(UINT8))
|
|
|
|
CHECKED_ALLOCZ(s->field_mv_table, s->mb_num*2*2 * sizeof(INT16))
|
|
|
|
CHECKED_ALLOCZ(s->field_select_table, s->mb_num*2* sizeof(INT8))
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
|
2002-02-18 20:33:27 +01:00
|
|
|
if (s->h263_pred || s->h263_plus) {
|
2001-07-22 16:18:56 +02:00
|
|
|
int y_size, c_size, i, size;
|
|
|
|
|
|
|
|
/* dc values */
|
|
|
|
|
|
|
|
y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
|
|
|
|
c_size = (s->mb_width + 2) * (s->mb_height + 2);
|
|
|
|
size = y_size + 2 * c_size;
|
2002-06-02 14:20:39 +02:00
|
|
|
CHECKED_ALLOCZ(s->dc_val[0], size * sizeof(INT16));
|
2001-07-22 16:18:56 +02:00
|
|
|
s->dc_val[1] = s->dc_val[0] + y_size;
|
|
|
|
s->dc_val[2] = s->dc_val[1] + c_size;
|
|
|
|
for(i=0;i<size;i++)
|
|
|
|
s->dc_val[0][i] = 1024;
|
|
|
|
|
|
|
|
/* ac values */
|
2002-06-02 14:20:39 +02:00
|
|
|
CHECKED_ALLOCZ(s->ac_val[0], size * sizeof(INT16) * 16);
|
2001-07-22 16:18:56 +02:00
|
|
|
s->ac_val[1] = s->ac_val[0] + y_size;
|
|
|
|
s->ac_val[2] = s->ac_val[1] + c_size;
|
|
|
|
|
|
|
|
/* cbp values */
|
2002-06-02 14:20:39 +02:00
|
|
|
CHECKED_ALLOCZ(s->coded_block, y_size);
|
2002-04-22 14:45:22 +02:00
|
|
|
|
|
|
|
/* divx501 bitstream reorder buffer */
|
2002-06-02 14:20:39 +02:00
|
|
|
CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE);
|
|
|
|
|
|
|
|
/* cbp, ac_pred, pred_dir */
|
|
|
|
CHECKED_ALLOCZ(s->cbp_table , s->mb_num * sizeof(UINT8))
|
|
|
|
CHECKED_ALLOCZ(s->pred_dir_table, s->mb_num * sizeof(UINT8))
|
2002-01-11 23:07:12 +01:00
|
|
|
}
|
2002-07-21 03:34:05 +02:00
|
|
|
CHECKED_ALLOCZ(s->qscale_table , s->mb_num * sizeof(UINT8))
|
|
|
|
|
2002-08-24 15:24:43 +02:00
|
|
|
/* which mb is a intra block */
|
|
|
|
CHECKED_ALLOCZ(s->mbintra_table, s->mb_num);
|
|
|
|
memset(s->mbintra_table, 1, s->mb_num);
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
/* default structure is frame */
|
|
|
|
s->picture_structure = PICT_FRAME;
|
2002-07-14 20:37:35 +02:00
|
|
|
|
2001-07-24 22:43:41 +02:00
|
|
|
/* init macroblock skip table */
|
2002-06-02 14:20:39 +02:00
|
|
|
CHECKED_ALLOCZ(s->mbskip_table, s->mb_num);
|
2002-03-27 22:25:22 +01:00
|
|
|
|
2002-04-19 05:25:20 +02:00
|
|
|
s->block= s->blocks[0];
|
2001-07-24 22:43:41 +02:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
s->context_initialized = 1;
|
|
|
|
return 0;
|
|
|
|
fail:
|
2002-02-18 10:31:37 +01:00
|
|
|
MPV_common_end(s);
|
2001-07-22 16:18:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-06-02 14:20:39 +02:00
|
|
|
|
|
|
|
//extern int sads;
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
/* init common structure for both encoder and decoder */
|
|
|
|
void MPV_common_end(MpegEncContext *s)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2002-05-19 01:03:29 +02:00
|
|
|
av_freep(&s->mb_type);
|
|
|
|
av_freep(&s->mb_var);
|
2002-06-02 14:20:39 +02:00
|
|
|
av_freep(&s->mc_mb_var);
|
2002-09-23 16:56:11 +02:00
|
|
|
av_freep(&s->mb_mean);
|
2002-05-19 01:03:29 +02:00
|
|
|
av_freep(&s->p_mv_table);
|
|
|
|
av_freep(&s->b_forw_mv_table);
|
|
|
|
av_freep(&s->b_back_mv_table);
|
|
|
|
av_freep(&s->b_bidir_forw_mv_table);
|
|
|
|
av_freep(&s->b_bidir_back_mv_table);
|
|
|
|
av_freep(&s->b_direct_forw_mv_table);
|
|
|
|
av_freep(&s->b_direct_back_mv_table);
|
|
|
|
av_freep(&s->b_direct_mv_table);
|
|
|
|
av_freep(&s->motion_val);
|
|
|
|
av_freep(&s->dc_val[0]);
|
|
|
|
av_freep(&s->ac_val[0]);
|
|
|
|
av_freep(&s->coded_block);
|
|
|
|
av_freep(&s->mbintra_table);
|
2002-06-02 14:20:39 +02:00
|
|
|
av_freep(&s->cbp_table);
|
|
|
|
av_freep(&s->pred_dir_table);
|
|
|
|
av_freep(&s->qscale_table);
|
2002-05-19 01:03:29 +02:00
|
|
|
av_freep(&s->me_scratchpad);
|
2002-06-02 14:20:39 +02:00
|
|
|
av_freep(&s->me_map);
|
|
|
|
av_freep(&s->me_score_map);
|
|
|
|
|
2002-05-19 01:03:29 +02:00
|
|
|
av_freep(&s->mbskip_table);
|
|
|
|
av_freep(&s->bitstream_buffer);
|
2002-06-02 14:20:39 +02:00
|
|
|
av_freep(&s->tex_pb_buffer);
|
|
|
|
av_freep(&s->pb2_buffer);
|
2002-07-14 20:37:35 +02:00
|
|
|
av_freep(&s->edge_emu_buffer);
|
2002-09-13 11:45:32 +02:00
|
|
|
av_freep(&s->co_located_type_table);
|
|
|
|
av_freep(&s->field_mv_table);
|
|
|
|
av_freep(&s->field_select_table);
|
2002-08-25 23:19:50 +02:00
|
|
|
av_freep(&s->avctx->stats_out);
|
2002-09-11 09:26:43 +02:00
|
|
|
av_freep(&s->ac_stats);
|
2002-08-25 23:19:50 +02:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
for(i=0;i<3;i++) {
|
2002-04-17 06:32:12 +02:00
|
|
|
int j;
|
2002-07-14 20:37:35 +02:00
|
|
|
if(!(s->flags&CODEC_FLAG_DR1)){
|
|
|
|
av_freep(&s->last_picture_base[i]);
|
|
|
|
av_freep(&s->next_picture_base[i]);
|
|
|
|
av_freep(&s->aux_picture_base[i]);
|
|
|
|
}
|
|
|
|
s->last_picture_base[i]=
|
|
|
|
s->next_picture_base[i]=
|
|
|
|
s->aux_picture_base [i] = NULL;
|
|
|
|
s->last_picture[i]=
|
|
|
|
s->next_picture[i]=
|
|
|
|
s->aux_picture [i] = NULL;
|
|
|
|
|
2002-04-17 06:32:12 +02:00
|
|
|
for(j=0; j<REORDER_BUFFER_SIZE; j++){
|
2002-05-19 01:03:29 +02:00
|
|
|
av_freep(&s->picture_buffer[j][i]);
|
2002-04-17 06:32:12 +02:00
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
s->context_initialized = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* init video encoder */
|
|
|
|
int MPV_encode_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
MpegEncContext *s = avctx->priv_data;
|
2001-08-12 02:52:37 +02:00
|
|
|
int i;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2002-04-08 23:03:35 +02:00
|
|
|
avctx->pix_fmt = PIX_FMT_YUV420P;
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
s->bit_rate = avctx->bit_rate;
|
2002-03-17 17:31:38 +01:00
|
|
|
s->bit_rate_tolerance = avctx->bit_rate_tolerance;
|
2001-07-22 16:18:56 +02:00
|
|
|
s->frame_rate = avctx->frame_rate;
|
|
|
|
s->width = avctx->width;
|
|
|
|
s->height = avctx->height;
|
2002-06-02 14:20:39 +02:00
|
|
|
if(avctx->gop_size > 600){
|
2002-06-02 17:31:30 +02:00
|
|
|
fprintf(stderr, "Warning keyframe interval too large! reducing it ...\n");
|
2002-06-02 14:20:39 +02:00
|
|
|
avctx->gop_size=600;
|
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
s->gop_size = avctx->gop_size;
|
2001-11-19 03:13:14 +01:00
|
|
|
s->rtp_mode = avctx->rtp_mode;
|
|
|
|
s->rtp_payload_size = avctx->rtp_payload_size;
|
2002-02-09 17:59:56 +01:00
|
|
|
if (avctx->rtp_callback)
|
|
|
|
s->rtp_callback = avctx->rtp_callback;
|
2002-03-17 17:31:38 +01:00
|
|
|
s->qmin= avctx->qmin;
|
|
|
|
s->qmax= avctx->qmax;
|
|
|
|
s->max_qdiff= avctx->max_qdiff;
|
|
|
|
s->qcompress= avctx->qcompress;
|
|
|
|
s->qblur= avctx->qblur;
|
2002-01-11 22:23:48 +01:00
|
|
|
s->avctx = avctx;
|
2002-03-23 00:22:08 +01:00
|
|
|
s->aspect_ratio_info= avctx->aspect_ratio_info;
|
2002-08-26 22:45:01 +02:00
|
|
|
if (avctx->aspect_ratio_info == FF_ASPECT_EXTENDED)
|
|
|
|
{
|
|
|
|
s->aspected_width = avctx->aspected_width;
|
|
|
|
s->aspected_height = avctx->aspected_height;
|
|
|
|
}
|
2002-03-27 22:25:22 +01:00
|
|
|
s->flags= avctx->flags;
|
2002-04-17 06:32:12 +02:00
|
|
|
s->max_b_frames= avctx->max_b_frames;
|
2002-04-21 23:18:17 +02:00
|
|
|
s->b_frame_strategy= avctx->b_frame_strategy;
|
2002-04-27 14:30:26 +02:00
|
|
|
s->codec_id= avctx->codec->id;
|
2002-06-02 14:20:39 +02:00
|
|
|
s->luma_elim_threshold = avctx->luma_elim_threshold;
|
|
|
|
s->chroma_elim_threshold= avctx->chroma_elim_threshold;
|
|
|
|
s->strict_std_compliance= avctx->strict_std_compliance;
|
|
|
|
s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
|
2002-08-07 01:51:15 +02:00
|
|
|
s->mpeg_quant= avctx->mpeg_quant;
|
2002-04-27 14:30:26 +02:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
if (s->gop_size <= 1) {
|
|
|
|
s->intra_only = 1;
|
|
|
|
s->gop_size = 12;
|
|
|
|
} else {
|
|
|
|
s->intra_only = 0;
|
|
|
|
}
|
2002-09-24 12:53:59 +02:00
|
|
|
|
2002-04-14 20:57:51 +02:00
|
|
|
/* ME algorithm */
|
2002-04-15 00:17:40 +02:00
|
|
|
if (avctx->me_method == 0)
|
|
|
|
/* For compatibility */
|
|
|
|
s->me_method = motion_estimation_method;
|
|
|
|
else
|
|
|
|
s->me_method = avctx->me_method;
|
2002-09-24 12:53:59 +02:00
|
|
|
|
2002-04-14 20:57:51 +02:00
|
|
|
/* Fixed QSCALE */
|
2001-07-22 16:18:56 +02:00
|
|
|
s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
|
2002-02-10 02:56:50 +01:00
|
|
|
|
2002-09-23 16:56:11 +02:00
|
|
|
s->adaptive_quant= ( s->avctx->lumi_masking
|
2002-09-24 12:53:59 +02:00
|
|
|
|| s->avctx->dark_masking
|
2002-09-23 16:56:11 +02:00
|
|
|
|| s->avctx->temporal_cplx_masking
|
|
|
|
|| s->avctx->spatial_cplx_masking
|
|
|
|
|| s->avctx->p_masking)
|
|
|
|
&& !s->fixed_qscale;
|
2002-09-26 02:22:25 +02:00
|
|
|
|
|
|
|
s->progressive_sequence= !(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
|
2002-09-24 12:53:59 +02:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
switch(avctx->codec->id) {
|
|
|
|
case CODEC_ID_MPEG1VIDEO:
|
|
|
|
s->out_format = FMT_MPEG1;
|
2002-04-22 21:04:28 +02:00
|
|
|
avctx->delay=0; //FIXME not sure, should check the spec
|
2001-07-22 16:18:56 +02:00
|
|
|
break;
|
|
|
|
case CODEC_ID_MJPEG:
|
|
|
|
s->out_format = FMT_MJPEG;
|
|
|
|
s->intra_only = 1; /* force intra only for jpeg */
|
2002-02-10 02:56:50 +01:00
|
|
|
s->mjpeg_write_tables = 1; /* write all tables */
|
2002-05-07 19:55:02 +02:00
|
|
|
s->mjpeg_data_only_frames = 0; /* write all the needed headers */
|
2002-02-10 02:56:50 +01:00
|
|
|
s->mjpeg_vsample[0] = 2; /* set up default sampling factors */
|
|
|
|
s->mjpeg_vsample[1] = 1; /* the only currently supported values */
|
|
|
|
s->mjpeg_vsample[2] = 1;
|
2002-05-07 19:55:02 +02:00
|
|
|
s->mjpeg_hsample[0] = 2;
|
2002-02-10 02:56:50 +01:00
|
|
|
s->mjpeg_hsample[1] = 1;
|
|
|
|
s->mjpeg_hsample[2] = 1;
|
2001-07-22 16:18:56 +02:00
|
|
|
if (mjpeg_init(s) < 0)
|
|
|
|
return -1;
|
2002-04-22 21:04:28 +02:00
|
|
|
avctx->delay=0;
|
2001-07-22 16:18:56 +02:00
|
|
|
break;
|
|
|
|
case CODEC_ID_H263:
|
2002-02-10 02:56:50 +01:00
|
|
|
if (h263_get_picture_format(s->width, s->height) == 7) {
|
|
|
|
printf("Input picture size isn't suitable for h263 codec! try h263+\n");
|
2001-07-22 16:18:56 +02:00
|
|
|
return -1;
|
2002-02-10 02:56:50 +01:00
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
s->out_format = FMT_H263;
|
2002-04-22 21:04:28 +02:00
|
|
|
avctx->delay=0;
|
2001-07-22 16:18:56 +02:00
|
|
|
break;
|
|
|
|
case CODEC_ID_H263P:
|
|
|
|
s->out_format = FMT_H263;
|
2001-11-19 03:13:14 +01:00
|
|
|
s->rtp_mode = 1;
|
|
|
|
s->rtp_payload_size = 1200;
|
2001-07-22 16:18:56 +02:00
|
|
|
s->h263_plus = 1;
|
2001-08-29 01:32:34 +02:00
|
|
|
s->unrestricted_mv = 1;
|
2002-05-02 06:39:45 +02:00
|
|
|
s->h263_aic = 1;
|
2001-08-31 08:21:27 +02:00
|
|
|
|
|
|
|
/* These are just to be sure */
|
|
|
|
s->umvplus = 0;
|
|
|
|
s->umvplus_dec = 0;
|
2002-04-22 21:04:28 +02:00
|
|
|
avctx->delay=0;
|
2001-07-22 16:18:56 +02:00
|
|
|
break;
|
|
|
|
case CODEC_ID_RV10:
|
|
|
|
s->out_format = FMT_H263;
|
|
|
|
s->h263_rv10 = 1;
|
2002-04-22 21:04:28 +02:00
|
|
|
avctx->delay=0;
|
2001-07-22 16:18:56 +02:00
|
|
|
break;
|
2001-08-15 15:12:23 +02:00
|
|
|
case CODEC_ID_MPEG4:
|
2001-07-22 16:18:56 +02:00
|
|
|
s->out_format = FMT_H263;
|
|
|
|
s->h263_pred = 1;
|
|
|
|
s->unrestricted_mv = 1;
|
2002-04-17 06:32:12 +02:00
|
|
|
s->has_b_frames= s->max_b_frames ? 1 : 0;
|
2002-04-22 21:04:28 +02:00
|
|
|
s->low_delay=0;
|
|
|
|
avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
|
2001-07-22 16:18:56 +02:00
|
|
|
break;
|
2002-04-05 06:09:04 +02:00
|
|
|
case CODEC_ID_MSMPEG4V1:
|
2001-07-22 16:18:56 +02:00
|
|
|
s->out_format = FMT_H263;
|
|
|
|
s->h263_msmpeg4 = 1;
|
|
|
|
s->h263_pred = 1;
|
|
|
|
s->unrestricted_mv = 1;
|
2002-04-05 06:09:04 +02:00
|
|
|
s->msmpeg4_version= 1;
|
2002-04-22 21:04:28 +02:00
|
|
|
avctx->delay=0;
|
2002-04-05 06:09:04 +02:00
|
|
|
break;
|
|
|
|
case CODEC_ID_MSMPEG4V2:
|
|
|
|
s->out_format = FMT_H263;
|
|
|
|
s->h263_msmpeg4 = 1;
|
|
|
|
s->h263_pred = 1;
|
|
|
|
s->unrestricted_mv = 1;
|
|
|
|
s->msmpeg4_version= 2;
|
2002-04-22 21:04:28 +02:00
|
|
|
avctx->delay=0;
|
2002-04-05 06:09:04 +02:00
|
|
|
break;
|
|
|
|
case CODEC_ID_MSMPEG4V3:
|
|
|
|
s->out_format = FMT_H263;
|
|
|
|
s->h263_msmpeg4 = 1;
|
|
|
|
s->h263_pred = 1;
|
|
|
|
s->unrestricted_mv = 1;
|
|
|
|
s->msmpeg4_version= 3;
|
2002-04-22 21:04:28 +02:00
|
|
|
avctx->delay=0;
|
2001-07-22 16:18:56 +02:00
|
|
|
break;
|
2002-06-18 02:49:00 +02:00
|
|
|
case CODEC_ID_WMV1:
|
|
|
|
s->out_format = FMT_H263;
|
|
|
|
s->h263_msmpeg4 = 1;
|
|
|
|
s->h263_pred = 1;
|
|
|
|
s->unrestricted_mv = 1;
|
|
|
|
s->msmpeg4_version= 4;
|
|
|
|
avctx->delay=0;
|
|
|
|
break;
|
|
|
|
case CODEC_ID_WMV2:
|
|
|
|
s->out_format = FMT_H263;
|
|
|
|
s->h263_msmpeg4 = 1;
|
|
|
|
s->h263_pred = 1;
|
|
|
|
s->unrestricted_mv = 1;
|
|
|
|
s->msmpeg4_version= 5;
|
|
|
|
avctx->delay=0;
|
|
|
|
break;
|
2001-07-22 16:18:56 +02:00
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
2002-03-28 05:25:35 +01:00
|
|
|
|
2002-03-22 03:21:17 +01:00
|
|
|
{ /* set up some save defaults, some codecs might override them later */
|
|
|
|
static int done=0;
|
|
|
|
if(!done){
|
|
|
|
int i;
|
|
|
|
done=1;
|
2002-03-25 00:45:13 +01:00
|
|
|
memset(default_mv_penalty, 0, sizeof(UINT16)*(MAX_FCODE+1)*(2*MAX_MV+1));
|
2002-03-22 03:21:17 +01:00
|
|
|
memset(default_fcode_tab , 0, sizeof(UINT8)*(2*MAX_MV+1));
|
|
|
|
|
|
|
|
for(i=-16; i<16; i++){
|
|
|
|
default_fcode_tab[i + MAX_MV]= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s->mv_penalty= default_mv_penalty;
|
|
|
|
s->fcode_tab= default_fcode_tab;
|
2002-06-25 12:15:42 +02:00
|
|
|
s->y_dc_scale_table=
|
|
|
|
s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
|
|
|
|
|
2002-03-23 21:21:33 +01:00
|
|
|
/* dont use mv_penalty table for crap MV as it would be confused */
|
2002-04-17 06:32:12 +02:00
|
|
|
if (s->me_method < ME_EPZS) s->mv_penalty = default_mv_penalty;
|
2002-03-23 21:21:33 +01:00
|
|
|
|
2001-07-24 22:43:41 +02:00
|
|
|
s->encoding = 1;
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
/* init */
|
|
|
|
if (MPV_common_init(s) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2002-09-30 00:44:22 +02:00
|
|
|
if (s->out_format == FMT_H263)
|
|
|
|
h263_encode_init(s);
|
|
|
|
else if (s->out_format == FMT_MPEG1)
|
|
|
|
ff_mpeg1_encode_init(s);
|
|
|
|
if(s->msmpeg4_version)
|
|
|
|
ff_msmpeg4_encode_init(s);
|
|
|
|
|
2001-08-12 02:52:37 +02:00
|
|
|
/* init default q matrix */
|
|
|
|
for(i=0;i<64;i++) {
|
2002-09-30 00:44:22 +02:00
|
|
|
int j= s->idct_permutation[i];
|
2002-08-07 01:51:15 +02:00
|
|
|
if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
|
2002-09-30 00:44:22 +02:00
|
|
|
s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
|
|
|
|
s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
|
2002-08-07 01:51:15 +02:00
|
|
|
}else if(s->out_format == FMT_H263){
|
2002-09-30 00:44:22 +02:00
|
|
|
s->intra_matrix[j] =
|
|
|
|
s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
|
2002-08-07 01:51:15 +02:00
|
|
|
}else{ /* mpeg1 */
|
2002-09-30 00:44:22 +02:00
|
|
|
s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
|
|
|
|
s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
|
2002-08-07 01:51:15 +02:00
|
|
|
}
|
2002-04-27 14:30:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* precompute matrix */
|
2002-05-02 06:39:45 +02:00
|
|
|
/* for mjpeg, we do include qscale in the matrix */
|
2002-04-27 14:30:26 +02:00
|
|
|
if (s->out_format != FMT_MJPEG) {
|
2002-08-30 01:55:32 +02:00
|
|
|
convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16, s->q_intra_matrix16_bias,
|
2002-09-30 11:19:54 +02:00
|
|
|
s->intra_matrix, s->intra_quant_bias, 1, 31);
|
2002-08-30 01:55:32 +02:00
|
|
|
convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16, s->q_inter_matrix16_bias,
|
2002-09-30 11:19:54 +02:00
|
|
|
s->inter_matrix, s->inter_quant_bias, 1, 31);
|
2001-08-12 02:52:37 +02:00
|
|
|
}
|
|
|
|
|
2002-04-21 23:18:17 +02:00
|
|
|
if(ff_rate_control_init(s) < 0)
|
|
|
|
return -1;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
s->picture_number = 0;
|
2002-03-22 03:21:17 +01:00
|
|
|
s->picture_in_gop_number = 0;
|
2001-07-22 16:18:56 +02:00
|
|
|
s->fake_picture_number = 0;
|
|
|
|
/* motion detector init */
|
|
|
|
s->f_code = 1;
|
2002-04-17 06:32:12 +02:00
|
|
|
s->b_code = 1;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int MPV_encode_end(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
MpegEncContext *s = avctx->priv_data;
|
|
|
|
|
|
|
|
#ifdef STATS
|
|
|
|
print_stats();
|
|
|
|
#endif
|
2002-04-21 23:18:17 +02:00
|
|
|
|
|
|
|
ff_rate_control_uninit(s);
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
MPV_common_end(s);
|
|
|
|
if (s->out_format == FMT_MJPEG)
|
|
|
|
mjpeg_close(s);
|
2002-02-10 02:56:50 +01:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* draw the edges of width 'w' of an image of size width, height */
|
2002-08-27 18:28:19 +02:00
|
|
|
//FIXME check that this is ok for mpeg4 interlaced
|
2002-01-15 05:07:29 +01:00
|
|
|
static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
|
|
|
UINT8 *ptr, *last_line;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
last_line = buf + (height - 1) * wrap;
|
|
|
|
for(i=0;i<w;i++) {
|
|
|
|
/* top and bottom */
|
|
|
|
memcpy(buf - (i + 1) * wrap, buf, width);
|
|
|
|
memcpy(last_line + (i + 1) * wrap, last_line, width);
|
|
|
|
}
|
|
|
|
/* left and right */
|
|
|
|
ptr = buf;
|
|
|
|
for(i=0;i<height;i++) {
|
|
|
|
memset(ptr - w, ptr[0], w);
|
|
|
|
memset(ptr + width, ptr[width-1], w);
|
|
|
|
ptr += wrap;
|
|
|
|
}
|
|
|
|
/* corners */
|
|
|
|
for(i=0;i<w;i++) {
|
|
|
|
memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
|
|
|
|
memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
|
|
|
|
memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
|
|
|
|
memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generic function for encode/decode called before a frame is coded/decoded */
|
2002-07-14 20:37:35 +02:00
|
|
|
void MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
UINT8 *tmp;
|
|
|
|
|
2001-08-08 23:57:07 +02:00
|
|
|
s->mb_skiped = 0;
|
2002-06-02 14:20:39 +02:00
|
|
|
s->decoding_error=0;
|
2002-07-16 13:45:14 +02:00
|
|
|
avctx->mbskip_table= s->mbskip_table;
|
2002-06-02 14:20:39 +02:00
|
|
|
|
2002-07-14 20:37:35 +02:00
|
|
|
if(avctx->flags&CODEC_FLAG_DR1){
|
|
|
|
avctx->get_buffer_callback(avctx, s->width, s->height, s->pict_type);
|
|
|
|
|
2002-07-15 16:15:10 +02:00
|
|
|
s->linesize = avctx->dr_stride;
|
|
|
|
s->uvlinesize= avctx->dr_uvstride;
|
|
|
|
s->ip_buffer_count= avctx->dr_ip_buffer_count;
|
2002-07-14 20:37:35 +02:00
|
|
|
}
|
2002-07-16 13:45:14 +02:00
|
|
|
avctx->dr_ip_buffer_count= s->ip_buffer_count;
|
2002-07-14 20:37:35 +02:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
if (s->pict_type == B_TYPE) {
|
|
|
|
for(i=0;i<3;i++) {
|
2002-07-14 20:37:35 +02:00
|
|
|
if(avctx->flags&CODEC_FLAG_DR1)
|
|
|
|
s->aux_picture[i]= avctx->dr_buffer[i];
|
2002-07-17 19:50:42 +02:00
|
|
|
|
|
|
|
//FIXME the following should never be needed, the decoder should drop b frames if no reference is available
|
|
|
|
if(s->next_picture[i]==NULL)
|
|
|
|
s->next_picture[i]= s->aux_picture[i];
|
|
|
|
if(s->last_picture[i]==NULL)
|
|
|
|
s->last_picture[i]= s->next_picture[i];
|
2002-07-14 20:37:35 +02:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
s->current_picture[i] = s->aux_picture[i];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for(i=0;i<3;i++) {
|
|
|
|
/* swap next and last */
|
2002-07-14 20:37:35 +02:00
|
|
|
if(avctx->flags&CODEC_FLAG_DR1)
|
|
|
|
tmp= avctx->dr_buffer[i];
|
|
|
|
else
|
|
|
|
tmp = s->last_picture[i];
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
s->last_picture[i] = s->next_picture[i];
|
|
|
|
s->next_picture[i] = tmp;
|
|
|
|
s->current_picture[i] = tmp;
|
2002-07-14 20:37:35 +02:00
|
|
|
|
2002-08-04 18:05:05 +02:00
|
|
|
if(s->last_picture[i]==NULL)
|
|
|
|
s->last_picture[i]= s->next_picture[i];
|
|
|
|
|
2002-07-14 20:37:35 +02:00
|
|
|
s->last_dr_opaque= s->next_dr_opaque;
|
|
|
|
s->next_dr_opaque= avctx->dr_opaque_frame;
|
|
|
|
|
2002-07-15 23:19:37 +02:00
|
|
|
if(s->has_b_frames && s->last_dr_opaque && s->codec_id!=CODEC_ID_SVQ1)
|
2002-07-14 20:37:35 +02:00
|
|
|
avctx->dr_opaque_frame= s->last_dr_opaque;
|
|
|
|
else
|
|
|
|
avctx->dr_opaque_frame= s->next_dr_opaque;
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
}
|
2002-08-02 22:27:46 +02:00
|
|
|
/* set dequantizer, we cant do it during init as it might change for mpeg4
|
|
|
|
and we cant do it in the header decode as init isnt called for mpeg4 there yet */
|
|
|
|
if(s->out_format == FMT_H263){
|
|
|
|
if(s->mpeg_quant)
|
|
|
|
s->dct_unquantize = s->dct_unquantize_mpeg2;
|
|
|
|
else
|
|
|
|
s->dct_unquantize = s->dct_unquantize_h263;
|
|
|
|
}else
|
|
|
|
s->dct_unquantize = s->dct_unquantize_mpeg1;
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
2001-07-31 01:26:26 +02:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
/* generic function for encode/decode called after a frame has been coded/decoded */
|
|
|
|
void MPV_frame_end(MpegEncContext *s)
|
|
|
|
{
|
2002-09-12 02:27:08 +02:00
|
|
|
s->avctx->key_frame = (s->pict_type == I_TYPE);
|
|
|
|
s->avctx->pict_type = s->pict_type;
|
2002-06-02 14:20:39 +02:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
/* draw edge for correct motion prediction if outside */
|
2002-07-14 20:37:35 +02:00
|
|
|
if (s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
|
2002-07-25 22:22:36 +02:00
|
|
|
draw_edges(s->current_picture[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH );
|
|
|
|
draw_edges(s->current_picture[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
|
|
|
|
draw_edges(s->current_picture[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
2002-01-15 06:02:26 +01:00
|
|
|
emms_c();
|
2002-04-21 23:18:17 +02:00
|
|
|
|
2002-08-25 23:19:50 +02:00
|
|
|
s->last_pict_type = s->pict_type;
|
2002-04-21 23:18:17 +02:00
|
|
|
if(s->pict_type!=B_TYPE){
|
|
|
|
s->last_non_b_pict_type= s->pict_type;
|
2002-04-24 03:24:06 +02:00
|
|
|
s->num_available_buffers++;
|
|
|
|
if(s->num_available_buffers>2) s->num_available_buffers= 2;
|
2002-04-21 23:18:17 +02:00
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
|
2002-04-17 06:32:12 +02:00
|
|
|
/* reorder input for encoding */
|
|
|
|
void reorder_input(MpegEncContext *s, AVPicture *pict)
|
|
|
|
{
|
|
|
|
int i, j, index;
|
|
|
|
|
|
|
|
if(s->max_b_frames > FF_MAX_B_FRAMES) s->max_b_frames= FF_MAX_B_FRAMES;
|
|
|
|
|
|
|
|
// delay= s->max_b_frames+1; (or 0 if no b frames cuz decoder diff)
|
|
|
|
|
|
|
|
for(j=0; j<REORDER_BUFFER_SIZE-1; j++){
|
|
|
|
s->coded_order[j]= s->coded_order[j+1];
|
|
|
|
}
|
|
|
|
s->coded_order[j].picture[0]= s->coded_order[j].picture[1]= s->coded_order[j].picture[2]= NULL; //catch uninitalized buffers
|
2002-04-22 23:02:18 +02:00
|
|
|
s->coded_order[j].pict_type=0;
|
2002-04-17 06:32:12 +02:00
|
|
|
|
|
|
|
switch(s->input_pict_type){
|
|
|
|
default:
|
|
|
|
case I_TYPE:
|
|
|
|
case S_TYPE:
|
|
|
|
case P_TYPE:
|
|
|
|
index= s->max_b_frames - s->b_frames_since_non_b;
|
|
|
|
s->b_frames_since_non_b=0;
|
|
|
|
break;
|
|
|
|
case B_TYPE:
|
|
|
|
index= s->max_b_frames + 1;
|
|
|
|
s->b_frames_since_non_b++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
//printf("index:%d type:%d strides: %d %d\n", index, s->input_pict_type, pict->linesize[0], s->linesize);
|
|
|
|
if( (index==0 || (s->flags&CODEC_FLAG_INPUT_PRESERVED))
|
|
|
|
&& pict->linesize[0] == s->linesize
|
2002-07-15 16:15:10 +02:00
|
|
|
&& pict->linesize[1] == s->uvlinesize
|
|
|
|
&& pict->linesize[2] == s->uvlinesize){
|
2002-04-17 06:32:12 +02:00
|
|
|
//printf("ptr\n");
|
|
|
|
for(i=0; i<3; i++){
|
|
|
|
s->coded_order[index].picture[i]= pict->data[i];
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
//printf("copy\n");
|
|
|
|
for(i=0; i<3; i++){
|
|
|
|
uint8_t *src = pict->data[i];
|
|
|
|
uint8_t *dest;
|
|
|
|
int src_wrap = pict->linesize[i];
|
|
|
|
int dest_wrap = s->linesize;
|
|
|
|
int w = s->width;
|
|
|
|
int h = s->height;
|
|
|
|
|
|
|
|
if(index==0) dest= s->last_picture[i]+16; //is current_picture indeed but the switch hapens after reordering
|
|
|
|
else dest= s->picture_buffer[s->picture_buffer_index][i];
|
|
|
|
|
|
|
|
if (i >= 1) {
|
|
|
|
dest_wrap >>= 1;
|
|
|
|
w >>= 1;
|
|
|
|
h >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->coded_order[index].picture[i]= dest;
|
|
|
|
for(j=0;j<h;j++) {
|
|
|
|
memcpy(dest, src, w);
|
|
|
|
dest += dest_wrap;
|
|
|
|
src += src_wrap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(index!=0){
|
|
|
|
s->picture_buffer_index++;
|
2002-08-01 13:01:07 +02:00
|
|
|
if(s->picture_buffer_index >= REORDER_BUFFER_SIZE) s->picture_buffer_index=0;
|
2002-04-17 06:32:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
s->coded_order[index].pict_type = s->input_pict_type;
|
|
|
|
s->coded_order[index].qscale = s->input_qscale;
|
|
|
|
s->coded_order[index].force_type= s->force_input_type;
|
|
|
|
s->coded_order[index].picture_in_gop_number= s->input_picture_in_gop_number;
|
|
|
|
s->coded_order[index].picture_number= s->input_picture_number;
|
|
|
|
|
|
|
|
for(i=0; i<3; i++){
|
|
|
|
s->new_picture[i]= s->coded_order[0].picture[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
int MPV_encode_picture(AVCodecContext *avctx,
|
|
|
|
unsigned char *buf, int buf_size, void *data)
|
|
|
|
{
|
|
|
|
MpegEncContext *s = avctx->priv_data;
|
|
|
|
AVPicture *pict = data;
|
|
|
|
|
2002-04-17 06:32:12 +02:00
|
|
|
s->input_qscale = avctx->quality;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
|
|
|
|
|
2002-04-21 23:18:17 +02:00
|
|
|
if(avctx->flags&CODEC_FLAG_TYPE){
|
|
|
|
s->input_pict_type=
|
|
|
|
s->force_input_type= avctx->key_frame ? I_TYPE : P_TYPE;
|
|
|
|
}else if(s->flags&CODEC_FLAG_PASS2){
|
|
|
|
s->input_pict_type=
|
|
|
|
s->force_input_type= s->rc_context.entry[s->input_picture_number].new_pict_type;
|
|
|
|
}else{
|
|
|
|
s->force_input_type=0;
|
|
|
|
if (!s->intra_only) {
|
|
|
|
/* first picture of GOP is intra */
|
|
|
|
if (s->input_picture_in_gop_number % s->gop_size==0){
|
|
|
|
s->input_pict_type = I_TYPE;
|
|
|
|
}else if(s->max_b_frames==0){
|
2002-04-17 06:32:12 +02:00
|
|
|
s->input_pict_type = P_TYPE;
|
2002-04-21 23:18:17 +02:00
|
|
|
}else{
|
|
|
|
if(s->b_frames_since_non_b < s->max_b_frames) //FIXME more IQ
|
|
|
|
s->input_pict_type = B_TYPE;
|
|
|
|
else
|
|
|
|
s->input_pict_type = P_TYPE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s->input_pict_type = I_TYPE;
|
2002-04-17 06:32:12 +02:00
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
|
2002-04-21 23:18:17 +02:00
|
|
|
if(s->input_pict_type==I_TYPE)
|
|
|
|
s->input_picture_in_gop_number=0;
|
|
|
|
|
2002-04-17 06:32:12 +02:00
|
|
|
reorder_input(s, pict);
|
|
|
|
|
|
|
|
/* output? */
|
|
|
|
if(s->coded_order[0].picture[0]){
|
|
|
|
|
|
|
|
s->pict_type= s->coded_order[0].pict_type;
|
|
|
|
if (s->fixed_qscale) /* the ratecontrol needs the last qscale so we dont touch it for CBR */
|
|
|
|
s->qscale= s->coded_order[0].qscale;
|
|
|
|
s->force_type= s->coded_order[0].force_type;
|
|
|
|
s->picture_in_gop_number= s->coded_order[0].picture_in_gop_number;
|
|
|
|
s->picture_number= s->coded_order[0].picture_number;
|
|
|
|
|
2002-07-14 20:37:35 +02:00
|
|
|
MPV_frame_start(s, avctx);
|
2002-04-17 06:32:12 +02:00
|
|
|
|
|
|
|
encode_picture(s, s->picture_number);
|
2002-09-12 02:27:08 +02:00
|
|
|
|
2002-05-12 01:42:16 +02:00
|
|
|
avctx->real_pict_num = s->picture_number;
|
2002-04-17 06:32:12 +02:00
|
|
|
avctx->header_bits = s->header_bits;
|
|
|
|
avctx->mv_bits = s->mv_bits;
|
|
|
|
avctx->misc_bits = s->misc_bits;
|
|
|
|
avctx->i_tex_bits = s->i_tex_bits;
|
|
|
|
avctx->p_tex_bits = s->p_tex_bits;
|
|
|
|
avctx->i_count = s->i_count;
|
2002-09-12 14:26:09 +02:00
|
|
|
avctx->p_count = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx
|
2002-04-17 06:32:12 +02:00
|
|
|
avctx->skip_count = s->skip_count;
|
|
|
|
|
|
|
|
MPV_frame_end(s);
|
|
|
|
|
|
|
|
if (s->out_format == FMT_MJPEG)
|
|
|
|
mjpeg_picture_trailer(s);
|
|
|
|
|
2002-09-23 16:56:11 +02:00
|
|
|
if(!s->fixed_qscale)
|
|
|
|
avctx->quality = s->qscale;
|
2002-04-21 23:18:17 +02:00
|
|
|
|
|
|
|
if(s->flags&CODEC_FLAG_PASS1)
|
|
|
|
ff_write_pass1_stats(s);
|
2002-06-02 14:20:39 +02:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
|
2002-04-17 06:32:12 +02:00
|
|
|
s->input_picture_number++;
|
|
|
|
s->input_picture_in_gop_number++;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
flush_put_bits(&s->pb);
|
2002-03-17 17:31:38 +01:00
|
|
|
s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
|
2002-08-25 23:19:50 +02:00
|
|
|
|
2002-03-17 17:31:38 +01:00
|
|
|
s->total_bits += s->frame_bits;
|
2002-03-23 18:43:30 +01:00
|
|
|
avctx->frame_bits = s->frame_bits;
|
|
|
|
//printf("fcode: %d, type: %d, head: %d, mv: %d, misc: %d, frame: %d, itex: %d, ptex: %d\n",
|
|
|
|
//s->f_code, avctx->key_frame, s->header_bits, s->mv_bits, s->misc_bits, s->frame_bits, s->i_tex_bits, s->p_tex_bits);
|
2002-08-25 23:19:50 +02:00
|
|
|
#if 0 //dump some stats to stats.txt for testing/debuging
|
|
|
|
if(s->max_b_frames==0)
|
|
|
|
{
|
|
|
|
static FILE *f=NULL;
|
|
|
|
if(!f) f= fopen("stats.txt", "wb");
|
|
|
|
get_psnr(pict->data, s->current_picture,
|
|
|
|
pict->linesize, s->linesize, avctx);
|
|
|
|
fprintf(f, "%7d, %7d, %2.4f\n", pbBufPtr(&s->pb) - s->pb.buf, s->qscale, avctx->psnr_y);
|
|
|
|
}
|
|
|
|
#endif
|
2002-02-12 16:43:16 +01:00
|
|
|
|
2002-02-26 23:14:27 +01:00
|
|
|
if (avctx->get_psnr) {
|
|
|
|
/* At this point pict->data should have the original frame */
|
|
|
|
/* an s->current_picture should have the coded/decoded frame */
|
|
|
|
get_psnr(pict->data, s->current_picture,
|
|
|
|
pict->linesize, s->linesize, avctx);
|
2002-04-17 06:32:12 +02:00
|
|
|
// printf("%f\n", avctx->psnr_y);
|
2002-02-26 23:14:27 +01:00
|
|
|
}
|
2002-02-12 16:43:16 +01:00
|
|
|
return pbBufPtr(&s->pb) - s->pb.buf;
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
|
2002-03-09 14:01:16 +01:00
|
|
|
static inline void gmc1_motion(MpegEncContext *s,
|
|
|
|
UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
|
|
|
|
int dest_offset,
|
|
|
|
UINT8 **ref_picture, int src_offset,
|
|
|
|
int h)
|
|
|
|
{
|
|
|
|
UINT8 *ptr;
|
2002-07-15 16:15:10 +02:00
|
|
|
int offset, src_x, src_y, linesize, uvlinesize;
|
2002-03-09 14:01:16 +01:00
|
|
|
int motion_x, motion_y;
|
2002-07-17 14:42:50 +02:00
|
|
|
int emu=0;
|
2002-03-09 14:01:16 +01:00
|
|
|
|
2002-03-15 05:10:53 +01:00
|
|
|
if(s->real_sprite_warping_points>1) printf("more than 1 warp point isnt supported\n");
|
2002-03-09 14:01:16 +01:00
|
|
|
motion_x= s->sprite_offset[0][0];
|
|
|
|
motion_y= s->sprite_offset[0][1];
|
|
|
|
src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
|
|
|
|
src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
|
|
|
|
motion_x<<=(3-s->sprite_warping_accuracy);
|
|
|
|
motion_y<<=(3-s->sprite_warping_accuracy);
|
|
|
|
src_x = clip(src_x, -16, s->width);
|
|
|
|
if (src_x == s->width)
|
|
|
|
motion_x =0;
|
|
|
|
src_y = clip(src_y, -16, s->height);
|
|
|
|
if (src_y == s->height)
|
|
|
|
motion_y =0;
|
|
|
|
|
|
|
|
linesize = s->linesize;
|
2002-07-15 16:15:10 +02:00
|
|
|
uvlinesize = s->uvlinesize;
|
2002-03-09 14:01:16 +01:00
|
|
|
ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
|
|
|
|
|
|
|
|
dest_y+=dest_offset;
|
2002-07-17 14:42:50 +02:00
|
|
|
if(s->flags&CODEC_FLAG_EMU_EDGE){
|
2002-07-25 22:22:36 +02:00
|
|
|
if(src_x<0 || src_y<0 || src_x + (motion_x&15) + 16 > s->h_edge_pos
|
|
|
|
|| src_y + (motion_y&15) + h > s->v_edge_pos){
|
|
|
|
emulated_edge_mc(s, ptr, linesize, 17, h+1, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
|
2002-07-17 14:42:50 +02:00
|
|
|
ptr= s->edge_emu_buffer;
|
|
|
|
emu=1;
|
|
|
|
}
|
|
|
|
}
|
2002-03-09 14:01:16 +01:00
|
|
|
gmc1(dest_y , ptr , linesize, h, motion_x&15, motion_y&15, s->no_rounding);
|
|
|
|
gmc1(dest_y+8, ptr+8, linesize, h, motion_x&15, motion_y&15, s->no_rounding);
|
|
|
|
|
|
|
|
motion_x= s->sprite_offset[1][0];
|
|
|
|
motion_y= s->sprite_offset[1][1];
|
|
|
|
src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
|
|
|
|
src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
|
|
|
|
motion_x<<=(3-s->sprite_warping_accuracy);
|
|
|
|
motion_y<<=(3-s->sprite_warping_accuracy);
|
|
|
|
src_x = clip(src_x, -8, s->width>>1);
|
|
|
|
if (src_x == s->width>>1)
|
|
|
|
motion_x =0;
|
|
|
|
src_y = clip(src_y, -8, s->height>>1);
|
|
|
|
if (src_y == s->height>>1)
|
|
|
|
motion_y =0;
|
|
|
|
|
2002-07-15 16:15:10 +02:00
|
|
|
offset = (src_y * uvlinesize) + src_x + (src_offset>>1);
|
2002-03-09 14:01:16 +01:00
|
|
|
ptr = ref_picture[1] + offset;
|
2002-07-17 14:42:50 +02:00
|
|
|
if(emu){
|
2002-07-25 22:22:36 +02:00
|
|
|
emulated_edge_mc(s, ptr, uvlinesize, 9, (h>>1)+1, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
|
2002-07-17 14:42:50 +02:00
|
|
|
ptr= s->edge_emu_buffer;
|
|
|
|
}
|
2002-07-15 16:15:10 +02:00
|
|
|
gmc1(dest_cb + (dest_offset>>1), ptr, uvlinesize, h>>1, motion_x&15, motion_y&15, s->no_rounding);
|
2002-07-17 14:42:50 +02:00
|
|
|
|
2002-03-09 14:01:16 +01:00
|
|
|
ptr = ref_picture[2] + offset;
|
2002-07-17 14:42:50 +02:00
|
|
|
if(emu){
|
2002-07-25 22:22:36 +02:00
|
|
|
emulated_edge_mc(s, ptr, uvlinesize, 9, (h>>1)+1, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
|
2002-07-17 14:42:50 +02:00
|
|
|
ptr= s->edge_emu_buffer;
|
|
|
|
}
|
2002-07-15 16:15:10 +02:00
|
|
|
gmc1(dest_cr + (dest_offset>>1), ptr, uvlinesize, h>>1, motion_x&15, motion_y&15, s->no_rounding);
|
2002-03-09 14:01:16 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-07-17 14:42:50 +02:00
|
|
|
static void emulated_edge_mc(MpegEncContext *s, UINT8 *src, int linesize, int block_w, int block_h,
|
2002-07-14 20:37:35 +02:00
|
|
|
int src_x, int src_y, int w, int h){
|
|
|
|
int x, y;
|
|
|
|
int start_y, start_x, end_y, end_x;
|
2002-07-17 14:42:50 +02:00
|
|
|
UINT8 *buf= s->edge_emu_buffer;
|
2002-07-25 22:22:36 +02:00
|
|
|
|
2002-07-14 20:37:35 +02:00
|
|
|
if(src_y>= h){
|
|
|
|
src+= (h-1-src_y)*linesize;
|
|
|
|
src_y=h-1;
|
2002-07-15 02:25:53 +02:00
|
|
|
}else if(src_y<=-block_h){
|
|
|
|
src+= (1-block_h-src_y)*linesize;
|
|
|
|
src_y=1-block_h;
|
2002-07-14 20:37:35 +02:00
|
|
|
}
|
|
|
|
if(src_x>= w){
|
|
|
|
src+= (w-1-src_x);
|
|
|
|
src_x=w-1;
|
2002-07-15 02:25:53 +02:00
|
|
|
}else if(src_x<=-block_w){
|
|
|
|
src+= (1-block_w-src_x);
|
|
|
|
src_x=1-block_w;
|
2002-07-14 20:37:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
start_y= MAX(0, -src_y);
|
|
|
|
start_x= MAX(0, -src_x);
|
|
|
|
end_y= MIN(block_h, h-src_y);
|
|
|
|
end_x= MIN(block_w, w-src_x);
|
2002-07-17 14:42:50 +02:00
|
|
|
|
2002-07-14 20:37:35 +02:00
|
|
|
// copy existing part
|
|
|
|
for(y=start_y; y<end_y; y++){
|
|
|
|
for(x=start_x; x<end_x; x++){
|
|
|
|
buf[x + y*linesize]= src[x + y*linesize];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//top
|
|
|
|
for(y=0; y<start_y; y++){
|
|
|
|
for(x=start_x; x<end_x; x++){
|
|
|
|
buf[x + y*linesize]= buf[x + start_y*linesize];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//bottom
|
|
|
|
for(y=end_y; y<block_h; y++){
|
|
|
|
for(x=start_x; x<end_x; x++){
|
|
|
|
buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(y=0; y<block_h; y++){
|
|
|
|
//left
|
|
|
|
for(x=0; x<start_x; x++){
|
|
|
|
buf[x + y*linesize]= buf[start_x + y*linesize];
|
|
|
|
}
|
|
|
|
|
|
|
|
//right
|
|
|
|
for(x=end_x; x<block_w; x++){
|
|
|
|
buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
/* apply one mpeg motion vector to the three components */
|
|
|
|
static inline void mpeg_motion(MpegEncContext *s,
|
|
|
|
UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
|
|
|
|
int dest_offset,
|
|
|
|
UINT8 **ref_picture, int src_offset,
|
2002-09-11 14:39:53 +02:00
|
|
|
int field_based, op_pixels_func (*pix_op)[4],
|
2001-07-22 16:18:56 +02:00
|
|
|
int motion_x, int motion_y, int h)
|
|
|
|
{
|
|
|
|
UINT8 *ptr;
|
2002-07-25 22:22:36 +02:00
|
|
|
int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
|
2002-07-14 20:37:35 +02:00
|
|
|
int emu=0;
|
2002-09-11 14:39:53 +02:00
|
|
|
#if 0
|
2002-03-09 14:01:16 +01:00
|
|
|
if(s->quarter_sample)
|
|
|
|
{
|
|
|
|
motion_x>>=1;
|
|
|
|
motion_y>>=1;
|
|
|
|
}
|
2002-09-11 14:39:53 +02:00
|
|
|
#endif
|
2001-07-22 16:18:56 +02:00
|
|
|
dxy = ((motion_y & 1) << 1) | (motion_x & 1);
|
|
|
|
src_x = s->mb_x * 16 + (motion_x >> 1);
|
|
|
|
src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
|
|
|
|
|
|
|
|
/* WARNING: do no forget half pels */
|
|
|
|
height = s->height >> field_based;
|
2002-07-25 22:22:36 +02:00
|
|
|
v_edge_pos = s->v_edge_pos >> field_based;
|
2001-07-22 16:18:56 +02:00
|
|
|
src_x = clip(src_x, -16, s->width);
|
|
|
|
if (src_x == s->width)
|
|
|
|
dxy &= ~1;
|
|
|
|
src_y = clip(src_y, -16, height);
|
|
|
|
if (src_y == height)
|
|
|
|
dxy &= ~2;
|
2002-07-17 13:04:26 +02:00
|
|
|
linesize = s->linesize << field_based;
|
|
|
|
uvlinesize = s->uvlinesize << field_based;
|
2001-07-22 16:18:56 +02:00
|
|
|
ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
|
|
|
|
dest_y += dest_offset;
|
2002-07-14 20:37:35 +02:00
|
|
|
|
|
|
|
if(s->flags&CODEC_FLAG_EMU_EDGE){
|
2002-07-25 22:22:36 +02:00
|
|
|
if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 16 > s->h_edge_pos
|
|
|
|
|| src_y + (motion_y&1) + h > v_edge_pos){
|
|
|
|
emulated_edge_mc(s, ptr, linesize, 17, h+1, src_x, src_y, s->h_edge_pos, v_edge_pos);
|
2002-07-14 20:37:35 +02:00
|
|
|
ptr= s->edge_emu_buffer;
|
|
|
|
emu=1;
|
|
|
|
}
|
|
|
|
}
|
2002-09-11 14:39:53 +02:00
|
|
|
pix_op[0][dxy](dest_y, ptr, linesize, h);
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2002-06-09 15:35:56 +02:00
|
|
|
if(s->flags&CODEC_FLAG_GRAY) return;
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
if (s->out_format == FMT_H263) {
|
|
|
|
dxy = 0;
|
|
|
|
if ((motion_x & 3) != 0)
|
|
|
|
dxy |= 1;
|
|
|
|
if ((motion_y & 3) != 0)
|
|
|
|
dxy |= 2;
|
|
|
|
mx = motion_x >> 2;
|
|
|
|
my = motion_y >> 2;
|
|
|
|
} else {
|
|
|
|
mx = motion_x / 2;
|
|
|
|
my = motion_y / 2;
|
|
|
|
dxy = ((my & 1) << 1) | (mx & 1);
|
|
|
|
mx >>= 1;
|
|
|
|
my >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
src_x = s->mb_x * 8 + mx;
|
|
|
|
src_y = s->mb_y * (8 >> field_based) + my;
|
|
|
|
src_x = clip(src_x, -8, s->width >> 1);
|
|
|
|
if (src_x == (s->width >> 1))
|
|
|
|
dxy &= ~1;
|
|
|
|
src_y = clip(src_y, -8, height >> 1);
|
|
|
|
if (src_y == (height >> 1))
|
|
|
|
dxy &= ~2;
|
2002-07-17 13:04:26 +02:00
|
|
|
offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
|
2001-07-22 16:18:56 +02:00
|
|
|
ptr = ref_picture[1] + offset;
|
2002-07-14 20:37:35 +02:00
|
|
|
if(emu){
|
2002-07-25 22:22:36 +02:00
|
|
|
emulated_edge_mc(s, ptr, uvlinesize, 9, (h>>1)+1, src_x, src_y, s->h_edge_pos>>1, v_edge_pos>>1);
|
2002-07-14 20:37:35 +02:00
|
|
|
ptr= s->edge_emu_buffer;
|
|
|
|
}
|
2002-09-11 14:39:53 +02:00
|
|
|
pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
|
2002-07-14 20:37:35 +02:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
ptr = ref_picture[2] + offset;
|
2002-07-14 20:37:35 +02:00
|
|
|
if(emu){
|
2002-07-25 22:22:36 +02:00
|
|
|
emulated_edge_mc(s, ptr, uvlinesize, 9, (h>>1)+1, src_x, src_y, s->h_edge_pos>>1, v_edge_pos>>1);
|
2002-07-14 20:37:35 +02:00
|
|
|
ptr= s->edge_emu_buffer;
|
|
|
|
}
|
2002-09-11 14:39:53 +02:00
|
|
|
pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
|
2002-03-09 14:01:16 +01:00
|
|
|
static inline void qpel_motion(MpegEncContext *s,
|
|
|
|
UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
|
|
|
|
int dest_offset,
|
|
|
|
UINT8 **ref_picture, int src_offset,
|
2002-09-11 14:39:53 +02:00
|
|
|
int field_based, op_pixels_func (*pix_op)[4],
|
|
|
|
qpel_mc_func (*qpix_op)[16],
|
2002-03-09 14:01:16 +01:00
|
|
|
int motion_x, int motion_y, int h)
|
|
|
|
{
|
|
|
|
UINT8 *ptr;
|
2002-09-14 12:11:05 +02:00
|
|
|
int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
|
2002-07-15 02:25:53 +02:00
|
|
|
int emu=0;
|
2002-03-09 14:01:16 +01:00
|
|
|
|
|
|
|
dxy = ((motion_y & 3) << 2) | (motion_x & 3);
|
|
|
|
src_x = s->mb_x * 16 + (motion_x >> 2);
|
|
|
|
src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
|
|
|
|
|
|
|
|
height = s->height >> field_based;
|
2002-07-25 22:22:36 +02:00
|
|
|
v_edge_pos = s->v_edge_pos >> field_based;
|
2002-03-09 14:01:16 +01:00
|
|
|
src_x = clip(src_x, -16, s->width);
|
|
|
|
if (src_x == s->width)
|
|
|
|
dxy &= ~3;
|
|
|
|
src_y = clip(src_y, -16, height);
|
|
|
|
if (src_y == height)
|
|
|
|
dxy &= ~12;
|
|
|
|
linesize = s->linesize << field_based;
|
2002-09-14 12:11:05 +02:00
|
|
|
uvlinesize = s->uvlinesize << field_based;
|
2002-03-09 14:01:16 +01:00
|
|
|
ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
|
|
|
|
dest_y += dest_offset;
|
|
|
|
//printf("%d %d %d\n", src_x, src_y, dxy);
|
2002-07-15 02:25:53 +02:00
|
|
|
|
|
|
|
if(s->flags&CODEC_FLAG_EMU_EDGE){
|
2002-07-25 22:22:36 +02:00
|
|
|
if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 16 > s->h_edge_pos
|
|
|
|
|| src_y + (motion_y&3) + h > v_edge_pos){
|
|
|
|
emulated_edge_mc(s, ptr, linesize, 17, h+1, src_x, src_y, s->h_edge_pos, v_edge_pos);
|
2002-07-15 02:25:53 +02:00
|
|
|
ptr= s->edge_emu_buffer;
|
|
|
|
emu=1;
|
|
|
|
}
|
|
|
|
}
|
2002-09-14 12:11:05 +02:00
|
|
|
if(!field_based)
|
|
|
|
qpix_op[0][dxy](dest_y, ptr, linesize);
|
|
|
|
else{
|
|
|
|
//damn interlaced mode
|
|
|
|
//FIXME boundary mirroring is not exactly correct here
|
|
|
|
qpix_op[1][dxy](dest_y , ptr , linesize);
|
|
|
|
qpix_op[1][dxy](dest_y+8, ptr+8, linesize);
|
|
|
|
}
|
2002-09-11 14:39:53 +02:00
|
|
|
|
2002-06-09 15:35:56 +02:00
|
|
|
if(s->flags&CODEC_FLAG_GRAY) return;
|
|
|
|
|
2002-09-14 12:11:05 +02:00
|
|
|
if(field_based){
|
|
|
|
mx= motion_x/2;
|
|
|
|
my= motion_y>>1;
|
|
|
|
}else if(s->divx_version){
|
|
|
|
mx= (motion_x>>1)|(motion_x&1);
|
|
|
|
my= (motion_y>>1)|(motion_y&1);
|
|
|
|
}else{
|
|
|
|
mx= motion_x/2;
|
|
|
|
my= motion_y/2;
|
|
|
|
}
|
|
|
|
mx= (mx>>1)|(mx&1);
|
|
|
|
my= (my>>1)|(my&1);
|
|
|
|
dxy= (mx&1) | ((my&1)<<1);
|
|
|
|
mx>>=1;
|
|
|
|
my>>=1;
|
2002-03-09 14:01:16 +01:00
|
|
|
|
|
|
|
src_x = s->mb_x * 8 + mx;
|
|
|
|
src_y = s->mb_y * (8 >> field_based) + my;
|
|
|
|
src_x = clip(src_x, -8, s->width >> 1);
|
|
|
|
if (src_x == (s->width >> 1))
|
|
|
|
dxy &= ~1;
|
|
|
|
src_y = clip(src_y, -8, height >> 1);
|
|
|
|
if (src_y == (height >> 1))
|
|
|
|
dxy &= ~2;
|
|
|
|
|
2002-09-14 12:11:05 +02:00
|
|
|
offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
|
2002-03-09 14:01:16 +01:00
|
|
|
ptr = ref_picture[1] + offset;
|
2002-07-15 02:25:53 +02:00
|
|
|
if(emu){
|
2002-09-14 12:11:05 +02:00
|
|
|
emulated_edge_mc(s, ptr, uvlinesize, 9, (h>>1)+1, src_x, src_y, s->h_edge_pos>>1, v_edge_pos>>1);
|
2002-07-15 02:25:53 +02:00
|
|
|
ptr= s->edge_emu_buffer;
|
|
|
|
}
|
2002-09-14 12:11:05 +02:00
|
|
|
pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
|
2002-07-15 02:25:53 +02:00
|
|
|
|
2002-03-09 14:01:16 +01:00
|
|
|
ptr = ref_picture[2] + offset;
|
2002-07-15 02:25:53 +02:00
|
|
|
if(emu){
|
2002-09-14 12:11:05 +02:00
|
|
|
emulated_edge_mc(s, ptr, uvlinesize, 9, (h>>1)+1, src_x, src_y, s->h_edge_pos>>1, v_edge_pos>>1);
|
2002-07-15 02:25:53 +02:00
|
|
|
ptr= s->edge_emu_buffer;
|
|
|
|
}
|
2002-09-14 12:11:05 +02:00
|
|
|
pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
|
2002-03-09 14:01:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
static inline void MPV_motion(MpegEncContext *s,
|
|
|
|
UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
|
|
|
|
int dir, UINT8 **ref_picture,
|
2002-09-11 14:39:53 +02:00
|
|
|
op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16])
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
|
|
|
int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
|
|
|
|
int mb_x, mb_y, i;
|
|
|
|
UINT8 *ptr, *dest;
|
2002-07-15 02:25:53 +02:00
|
|
|
int emu=0;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
mb_x = s->mb_x;
|
|
|
|
mb_y = s->mb_y;
|
|
|
|
|
|
|
|
switch(s->mv_type) {
|
|
|
|
case MV_TYPE_16X16:
|
2002-03-09 14:01:16 +01:00
|
|
|
if(s->mcsel){
|
|
|
|
gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
|
|
|
|
ref_picture, 0,
|
|
|
|
16);
|
2002-09-11 14:39:53 +02:00
|
|
|
}else if(s->quarter_sample){
|
2002-03-09 14:01:16 +01:00
|
|
|
qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
|
|
|
|
ref_picture, 0,
|
|
|
|
0, pix_op, qpix_op,
|
|
|
|
s->mv[dir][0][0], s->mv[dir][0][1], 16);
|
|
|
|
}else{
|
|
|
|
mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
|
|
|
|
ref_picture, 0,
|
|
|
|
0, pix_op,
|
|
|
|
s->mv[dir][0][0], s->mv[dir][0][1], 16);
|
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
break;
|
|
|
|
case MV_TYPE_8X8:
|
2002-09-14 14:36:54 +02:00
|
|
|
mx = 0;
|
|
|
|
my = 0;
|
|
|
|
if(s->quarter_sample){
|
|
|
|
for(i=0;i<4;i++) {
|
|
|
|
motion_x = s->mv[dir][i][0];
|
|
|
|
motion_y = s->mv[dir][i][1];
|
|
|
|
|
|
|
|
dxy = ((motion_y & 3) << 2) | (motion_x & 3);
|
|
|
|
src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
|
|
|
|
src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
|
|
|
|
|
|
|
|
/* WARNING: do no forget half pels */
|
|
|
|
src_x = clip(src_x, -16, s->width);
|
|
|
|
if (src_x == s->width)
|
|
|
|
dxy &= ~3;
|
|
|
|
src_y = clip(src_y, -16, s->height);
|
|
|
|
if (src_y == s->height)
|
|
|
|
dxy &= ~12;
|
|
|
|
|
|
|
|
ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
|
|
|
|
if(s->flags&CODEC_FLAG_EMU_EDGE){
|
|
|
|
if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 8 > s->h_edge_pos
|
|
|
|
|| src_y + (motion_y&3) + 8 > s->v_edge_pos){
|
|
|
|
emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
|
|
|
|
ptr= s->edge_emu_buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
|
|
|
|
qpix_op[1][dxy](dest, ptr, s->linesize);
|
|
|
|
|
|
|
|
mx += s->mv[dir][i][0]/2;
|
|
|
|
my += s->mv[dir][i][1]/2;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
for(i=0;i<4;i++) {
|
|
|
|
motion_x = s->mv[dir][i][0];
|
|
|
|
motion_y = s->mv[dir][i][1];
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2002-09-14 14:36:54 +02:00
|
|
|
dxy = ((motion_y & 1) << 1) | (motion_x & 1);
|
|
|
|
src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
|
|
|
|
src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2002-09-14 14:36:54 +02:00
|
|
|
/* WARNING: do no forget half pels */
|
|
|
|
src_x = clip(src_x, -16, s->width);
|
|
|
|
if (src_x == s->width)
|
|
|
|
dxy &= ~1;
|
|
|
|
src_y = clip(src_y, -16, s->height);
|
|
|
|
if (src_y == s->height)
|
|
|
|
dxy &= ~2;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2002-09-14 14:36:54 +02:00
|
|
|
ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
|
|
|
|
if(s->flags&CODEC_FLAG_EMU_EDGE){
|
|
|
|
if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 8 > s->h_edge_pos
|
|
|
|
|| src_y + (motion_y&1) + 8 > s->v_edge_pos){
|
|
|
|
emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
|
|
|
|
ptr= s->edge_emu_buffer;
|
|
|
|
}
|
2002-07-15 02:25:53 +02:00
|
|
|
}
|
2002-09-14 14:36:54 +02:00
|
|
|
dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
|
|
|
|
pix_op[1][dxy](dest, ptr, s->linesize, 8);
|
|
|
|
|
|
|
|
mx += s->mv[dir][i][0];
|
|
|
|
my += s->mv[dir][i][1];
|
2002-07-15 02:25:53 +02:00
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
2002-09-14 14:36:54 +02:00
|
|
|
|
2002-06-09 15:35:56 +02:00
|
|
|
if(s->flags&CODEC_FLAG_GRAY) break;
|
2001-07-22 16:18:56 +02:00
|
|
|
/* In case of 8X8, we construct a single chroma motion vector
|
|
|
|
with a special rounding */
|
|
|
|
for(i=0;i<4;i++) {
|
|
|
|
}
|
|
|
|
if (mx >= 0)
|
|
|
|
mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
|
|
|
|
else {
|
|
|
|
mx = -mx;
|
|
|
|
mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
|
|
|
|
}
|
|
|
|
if (my >= 0)
|
|
|
|
my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
|
|
|
|
else {
|
|
|
|
my = -my;
|
|
|
|
my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
|
|
|
|
}
|
|
|
|
dxy = ((my & 1) << 1) | (mx & 1);
|
|
|
|
mx >>= 1;
|
|
|
|
my >>= 1;
|
|
|
|
|
|
|
|
src_x = mb_x * 8 + mx;
|
|
|
|
src_y = mb_y * 8 + my;
|
|
|
|
src_x = clip(src_x, -8, s->width/2);
|
|
|
|
if (src_x == s->width/2)
|
|
|
|
dxy &= ~1;
|
|
|
|
src_y = clip(src_y, -8, s->height/2);
|
|
|
|
if (src_y == s->height/2)
|
|
|
|
dxy &= ~2;
|
|
|
|
|
2002-07-15 16:15:10 +02:00
|
|
|
offset = (src_y * (s->uvlinesize)) + src_x;
|
2001-07-22 16:18:56 +02:00
|
|
|
ptr = ref_picture[1] + offset;
|
2002-07-15 02:25:53 +02:00
|
|
|
if(s->flags&CODEC_FLAG_EMU_EDGE){
|
2002-07-25 22:22:36 +02:00
|
|
|
if(src_x<0 || src_y<0 || src_x + (dxy &1) + 8 > s->h_edge_pos>>1
|
|
|
|
|| src_y + (dxy>>1) + 8 > s->v_edge_pos>>1){
|
|
|
|
emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
|
2002-07-15 02:25:53 +02:00
|
|
|
ptr= s->edge_emu_buffer;
|
|
|
|
emu=1;
|
|
|
|
}
|
|
|
|
}
|
2002-09-11 14:39:53 +02:00
|
|
|
pix_op[1][dxy](dest_cb, ptr, s->uvlinesize, 8);
|
2002-07-15 02:25:53 +02:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
ptr = ref_picture[2] + offset;
|
2002-07-15 02:25:53 +02:00
|
|
|
if(emu){
|
2002-07-25 22:22:36 +02:00
|
|
|
emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
|
2002-07-15 02:25:53 +02:00
|
|
|
ptr= s->edge_emu_buffer;
|
|
|
|
}
|
2002-09-11 14:39:53 +02:00
|
|
|
pix_op[1][dxy](dest_cr, ptr, s->uvlinesize, 8);
|
2001-07-22 16:18:56 +02:00
|
|
|
break;
|
|
|
|
case MV_TYPE_FIELD:
|
|
|
|
if (s->picture_structure == PICT_FRAME) {
|
2002-09-14 12:11:05 +02:00
|
|
|
if(s->quarter_sample){
|
|
|
|
/* top field */
|
|
|
|
qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
|
|
|
|
ref_picture, s->field_select[dir][0] ? s->linesize : 0,
|
|
|
|
1, pix_op, qpix_op,
|
|
|
|
s->mv[dir][0][0], s->mv[dir][0][1], 8);
|
|
|
|
/* bottom field */
|
|
|
|
qpel_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
|
|
|
|
ref_picture, s->field_select[dir][1] ? s->linesize : 0,
|
|
|
|
1, pix_op, qpix_op,
|
|
|
|
s->mv[dir][1][0], s->mv[dir][1][1], 8);
|
|
|
|
}else{
|
|
|
|
/* top field */
|
|
|
|
mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
|
|
|
|
ref_picture, s->field_select[dir][0] ? s->linesize : 0,
|
|
|
|
1, pix_op,
|
|
|
|
s->mv[dir][0][0], s->mv[dir][0][1], 8);
|
|
|
|
/* bottom field */
|
|
|
|
mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
|
|
|
|
ref_picture, s->field_select[dir][1] ? s->linesize : 0,
|
|
|
|
1, pix_op,
|
|
|
|
s->mv[dir][1][0], s->mv[dir][1][1], 8);
|
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* put block[] to dest[] */
|
|
|
|
static inline void put_dct(MpegEncContext *s,
|
|
|
|
DCTELEM *block, int i, UINT8 *dest, int line_size)
|
|
|
|
{
|
2002-09-30 18:14:14 +02:00
|
|
|
s->dct_unquantize(s, block, i, s->qscale);
|
2002-09-30 00:44:22 +02:00
|
|
|
s->idct_put (dest, line_size, block);
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add block[] to dest[] */
|
|
|
|
static inline void add_dct(MpegEncContext *s,
|
|
|
|
DCTELEM *block, int i, UINT8 *dest, int line_size)
|
|
|
|
{
|
2002-06-06 01:43:56 +02:00
|
|
|
if (s->block_last_index[i] >= 0) {
|
2002-09-30 00:44:22 +02:00
|
|
|
s->idct_add (dest, line_size, block);
|
2002-06-06 01:43:56 +02:00
|
|
|
}
|
|
|
|
}
|
2002-04-27 15:12:57 +02:00
|
|
|
|
2002-06-06 01:43:56 +02:00
|
|
|
static inline void add_dequant_dct(MpegEncContext *s,
|
|
|
|
DCTELEM *block, int i, UINT8 *dest, int line_size)
|
|
|
|
{
|
2001-07-22 16:18:56 +02:00
|
|
|
if (s->block_last_index[i] >= 0) {
|
2002-06-06 01:43:56 +02:00
|
|
|
s->dct_unquantize(s, block, i, s->qscale);
|
2002-04-17 06:32:12 +02:00
|
|
|
|
2002-09-30 00:44:22 +02:00
|
|
|
s->idct_add (dest, line_size, block);
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-02 14:20:39 +02:00
|
|
|
/**
|
|
|
|
* cleans dc, ac, coded_block for the current non intra MB
|
|
|
|
*/
|
|
|
|
void ff_clean_intra_table_entries(MpegEncContext *s)
|
|
|
|
{
|
|
|
|
int wrap = s->block_wrap[0];
|
|
|
|
int xy = s->block_index[0];
|
|
|
|
|
|
|
|
s->dc_val[0][xy ] =
|
|
|
|
s->dc_val[0][xy + 1 ] =
|
|
|
|
s->dc_val[0][xy + wrap] =
|
|
|
|
s->dc_val[0][xy + 1 + wrap] = 1024;
|
|
|
|
/* ac pred */
|
|
|
|
memset(s->ac_val[0][xy ], 0, 32 * sizeof(INT16));
|
|
|
|
memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(INT16));
|
|
|
|
if (s->msmpeg4_version>=3) {
|
|
|
|
s->coded_block[xy ] =
|
|
|
|
s->coded_block[xy + 1 ] =
|
|
|
|
s->coded_block[xy + wrap] =
|
|
|
|
s->coded_block[xy + 1 + wrap] = 0;
|
|
|
|
}
|
|
|
|
/* chroma */
|
|
|
|
wrap = s->block_wrap[4];
|
|
|
|
xy = s->mb_x + 1 + (s->mb_y + 1) * wrap;
|
|
|
|
s->dc_val[1][xy] =
|
|
|
|
s->dc_val[2][xy] = 1024;
|
|
|
|
/* ac pred */
|
|
|
|
memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16));
|
|
|
|
memset(s->ac_val[2][xy], 0, 16 * sizeof(INT16));
|
|
|
|
|
|
|
|
s->mbintra_table[s->mb_x + s->mb_y*s->mb_width]= 0;
|
|
|
|
}
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
/* generic function called after a macroblock has been parsed by the
|
|
|
|
decoder or after it has been encoded by the encoder.
|
|
|
|
|
|
|
|
Important variables used:
|
|
|
|
s->mb_intra : true if intra macroblock
|
|
|
|
s->mv_dir : motion vector direction
|
|
|
|
s->mv_type : motion vector type
|
|
|
|
s->mv : motion vector
|
|
|
|
s->interlaced_dct : true if interlaced dct used (mpeg2)
|
|
|
|
*/
|
|
|
|
void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
|
|
|
|
{
|
2002-02-18 10:31:37 +01:00
|
|
|
int mb_x, mb_y;
|
2002-06-06 01:43:56 +02:00
|
|
|
const int mb_xy = s->mb_y * s->mb_width + s->mb_x;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
mb_x = s->mb_x;
|
|
|
|
mb_y = s->mb_y;
|
|
|
|
|
2001-10-17 01:10:38 +02:00
|
|
|
#ifdef FF_POSTPROCESS
|
2002-06-03 09:01:42 +02:00
|
|
|
/* Obsolete. Exists for compatibility with mplayer only. */
|
2001-10-17 01:10:38 +02:00
|
|
|
quant_store[mb_y][mb_x]=s->qscale;
|
|
|
|
//printf("[%02d][%02d] %d\n",mb_x,mb_y,s->qscale);
|
2002-06-03 09:01:42 +02:00
|
|
|
#else
|
2002-07-21 03:34:05 +02:00
|
|
|
/* even more obsolete, exists for mplayer xp only */
|
2002-06-03 09:58:35 +02:00
|
|
|
if(s->avctx->quant_store) s->avctx->quant_store[mb_y*s->avctx->qstride+mb_x] = s->qscale;
|
2001-10-17 01:10:38 +02:00
|
|
|
#endif
|
2002-07-21 03:34:05 +02:00
|
|
|
s->qscale_table[mb_xy]= s->qscale;
|
2001-10-17 01:10:38 +02:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
/* update DC predictors for P macroblocks */
|
|
|
|
if (!s->mb_intra) {
|
2002-02-19 20:23:34 +01:00
|
|
|
if (s->h263_pred || s->h263_aic) {
|
2002-06-06 01:43:56 +02:00
|
|
|
if(s->mbintra_table[mb_xy])
|
2002-06-02 14:20:39 +02:00
|
|
|
ff_clean_intra_table_entries(s);
|
2001-07-22 16:18:56 +02:00
|
|
|
} else {
|
2002-06-02 14:20:39 +02:00
|
|
|
s->last_dc[0] =
|
|
|
|
s->last_dc[1] =
|
2001-07-22 16:18:56 +02:00
|
|
|
s->last_dc[2] = 128 << s->intra_dc_precision;
|
|
|
|
}
|
|
|
|
}
|
2002-02-19 20:23:34 +01:00
|
|
|
else if (s->h263_pred || s->h263_aic)
|
2002-06-06 01:43:56 +02:00
|
|
|
s->mbintra_table[mb_xy]=1;
|
2002-01-10 01:53:21 +01:00
|
|
|
|
2002-03-22 17:51:44 +01:00
|
|
|
/* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
|
2002-06-06 01:43:56 +02:00
|
|
|
if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2002-06-06 01:43:56 +02:00
|
|
|
const int wrap = s->block_wrap[0];
|
|
|
|
const int xy = s->block_index[0];
|
2002-09-13 11:45:32 +02:00
|
|
|
const int mb_index= s->mb_x + s->mb_y*s->mb_width;
|
2002-08-27 18:28:19 +02:00
|
|
|
if(s->mv_type == MV_TYPE_8X8){
|
2002-09-13 11:45:32 +02:00
|
|
|
s->co_located_type_table[mb_index]= CO_LOCATED_TYPE_4MV;
|
2002-08-27 18:28:19 +02:00
|
|
|
} else {
|
|
|
|
int motion_x, motion_y;
|
|
|
|
if (s->mb_intra) {
|
|
|
|
motion_x = 0;
|
|
|
|
motion_y = 0;
|
2002-09-13 11:45:32 +02:00
|
|
|
if(s->co_located_type_table)
|
|
|
|
s->co_located_type_table[mb_index]= 0;
|
2002-08-27 18:28:19 +02:00
|
|
|
} else if (s->mv_type == MV_TYPE_16X16) {
|
|
|
|
motion_x = s->mv[0][0][0];
|
|
|
|
motion_y = s->mv[0][0][1];
|
2002-09-13 11:45:32 +02:00
|
|
|
if(s->co_located_type_table)
|
|
|
|
s->co_located_type_table[mb_index]= 0;
|
2002-08-27 18:28:19 +02:00
|
|
|
} else /*if (s->mv_type == MV_TYPE_FIELD)*/ {
|
2002-09-13 11:45:32 +02:00
|
|
|
int i;
|
2002-08-27 18:28:19 +02:00
|
|
|
motion_x = s->mv[0][0][0] + s->mv[0][1][0];
|
|
|
|
motion_y = s->mv[0][0][1] + s->mv[0][1][1];
|
|
|
|
motion_x = (motion_x>>1) | (motion_x&1);
|
2002-09-13 11:45:32 +02:00
|
|
|
for(i=0; i<2; i++){
|
|
|
|
s->field_mv_table[mb_index][i][0]= s->mv[0][i][0];
|
|
|
|
s->field_mv_table[mb_index][i][1]= s->mv[0][i][1];
|
|
|
|
s->field_select_table[mb_index][i]= s->field_select[0][i];
|
|
|
|
}
|
|
|
|
s->co_located_type_table[mb_index]= CO_LOCATED_TYPE_FIELDMV;
|
2002-08-27 18:28:19 +02:00
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
/* no update if 8X8 because it has been done during parsing */
|
2002-02-18 10:31:37 +01:00
|
|
|
s->motion_val[xy][0] = motion_x;
|
|
|
|
s->motion_val[xy][1] = motion_y;
|
|
|
|
s->motion_val[xy + 1][0] = motion_x;
|
|
|
|
s->motion_val[xy + 1][1] = motion_y;
|
|
|
|
s->motion_val[xy + wrap][0] = motion_x;
|
|
|
|
s->motion_val[xy + wrap][1] = motion_y;
|
|
|
|
s->motion_val[xy + 1 + wrap][0] = motion_x;
|
|
|
|
s->motion_val[xy + 1 + wrap][1] = motion_y;
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-04-17 06:32:12 +02:00
|
|
|
if (!(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) {
|
2001-07-22 16:18:56 +02:00
|
|
|
UINT8 *dest_y, *dest_cb, *dest_cr;
|
2002-06-06 01:43:56 +02:00
|
|
|
int dct_linesize, dct_offset;
|
2002-09-11 14:39:53 +02:00
|
|
|
op_pixels_func (*op_pix)[4];
|
|
|
|
qpel_mc_func (*op_qpix)[16];
|
2001-07-24 22:43:41 +02:00
|
|
|
|
2002-04-17 06:32:12 +02:00
|
|
|
/* avoid copy if macroblock skipped in last frame too
|
|
|
|
dont touch it for B-frames as they need the skip info from the next p-frame */
|
|
|
|
if (s->pict_type != B_TYPE) {
|
2002-06-06 01:43:56 +02:00
|
|
|
UINT8 *mbskip_ptr = &s->mbskip_table[mb_xy];
|
2001-07-24 22:43:41 +02:00
|
|
|
if (s->mb_skiped) {
|
|
|
|
s->mb_skiped = 0;
|
2002-07-15 16:15:10 +02:00
|
|
|
|
|
|
|
(*mbskip_ptr) ++; /* indicate that this time we skiped it */
|
|
|
|
if(*mbskip_ptr >99) *mbskip_ptr= 99;
|
|
|
|
|
2002-04-17 06:32:12 +02:00
|
|
|
/* if previous was skipped too, then nothing to do !
|
|
|
|
skip only during decoding as we might trash the buffers during encoding a bit */
|
2002-07-15 16:15:10 +02:00
|
|
|
if (*mbskip_ptr >= s->ip_buffer_count && !s->encoding)
|
2001-07-24 22:43:41 +02:00
|
|
|
goto the_end;
|
|
|
|
} else {
|
|
|
|
*mbskip_ptr = 0; /* not skipped */
|
|
|
|
}
|
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2002-10-02 19:07:39 +02:00
|
|
|
if(s->pict_type==B_TYPE && s->avctx->draw_horiz_band){
|
|
|
|
dest_y = s->current_picture [0] + mb_x * 16;
|
|
|
|
dest_cb = s->current_picture[1] + mb_x * 8;
|
|
|
|
dest_cr = s->current_picture[2] + mb_x * 8;
|
|
|
|
}else{
|
|
|
|
dest_y = s->current_picture [0] + (mb_y * 16* s->linesize ) + mb_x * 16;
|
|
|
|
dest_cb = s->current_picture[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
|
|
|
|
dest_cr = s->current_picture[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
|
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
if (s->interlaced_dct) {
|
|
|
|
dct_linesize = s->linesize * 2;
|
|
|
|
dct_offset = s->linesize;
|
|
|
|
} else {
|
|
|
|
dct_linesize = s->linesize;
|
|
|
|
dct_offset = s->linesize * 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!s->mb_intra) {
|
|
|
|
/* motion handling */
|
2002-06-02 14:20:39 +02:00
|
|
|
/* decoding or more than one mb_type (MC was allready done otherwise) */
|
2002-06-06 01:43:56 +02:00
|
|
|
if((!s->encoding) || (s->mb_type[mb_xy]&(s->mb_type[mb_xy]-1))){
|
2002-04-19 05:25:20 +02:00
|
|
|
if ((!s->no_rounding) || s->pict_type==B_TYPE){
|
2002-04-17 06:32:12 +02:00
|
|
|
op_pix = put_pixels_tab;
|
2002-09-11 14:39:53 +02:00
|
|
|
op_qpix= put_qpel_pixels_tab;
|
2002-04-17 06:32:12 +02:00
|
|
|
}else{
|
|
|
|
op_pix = put_no_rnd_pixels_tab;
|
2002-09-11 14:39:53 +02:00
|
|
|
op_qpix= put_no_rnd_qpel_pixels_tab;
|
2002-04-17 06:32:12 +02:00
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2002-04-17 06:32:12 +02:00
|
|
|
if (s->mv_dir & MV_DIR_FORWARD) {
|
|
|
|
MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
|
2002-09-11 14:39:53 +02:00
|
|
|
op_pix = avg_pixels_tab;
|
|
|
|
op_qpix= avg_qpel_pixels_tab;
|
2002-04-17 06:32:12 +02:00
|
|
|
}
|
|
|
|
if (s->mv_dir & MV_DIR_BACKWARD) {
|
|
|
|
MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
|
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
|
2002-06-06 01:43:56 +02:00
|
|
|
/* skip dequant / idct if we are really late ;) */
|
|
|
|
if(s->hurry_up>1) goto the_end;
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
/* add dct residue */
|
2002-09-30 18:14:14 +02:00
|
|
|
if(s->encoding || !( s->mpeg2 || s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO
|
|
|
|
|| (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
|
2002-06-06 01:43:56 +02:00
|
|
|
add_dequant_dct(s, block[0], 0, dest_y, dct_linesize);
|
|
|
|
add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize);
|
|
|
|
add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
|
|
|
|
add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
|
|
|
|
|
2002-06-09 15:35:56 +02:00
|
|
|
if(!(s->flags&CODEC_FLAG_GRAY)){
|
2002-07-15 16:15:10 +02:00
|
|
|
add_dequant_dct(s, block[4], 4, dest_cb, s->uvlinesize);
|
|
|
|
add_dequant_dct(s, block[5], 5, dest_cr, s->uvlinesize);
|
2002-06-09 15:35:56 +02:00
|
|
|
}
|
2002-06-06 01:43:56 +02:00
|
|
|
} else {
|
|
|
|
add_dct(s, block[0], 0, dest_y, dct_linesize);
|
|
|
|
add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
|
|
|
|
add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
|
|
|
|
add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2002-06-09 15:35:56 +02:00
|
|
|
if(!(s->flags&CODEC_FLAG_GRAY)){
|
2002-07-15 16:15:10 +02:00
|
|
|
add_dct(s, block[4], 4, dest_cb, s->uvlinesize);
|
|
|
|
add_dct(s, block[5], 5, dest_cr, s->uvlinesize);
|
2002-06-09 15:35:56 +02:00
|
|
|
}
|
2002-06-06 01:43:56 +02:00
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
} else {
|
|
|
|
/* dct only in intra block */
|
2002-09-30 18:14:14 +02:00
|
|
|
if(s->encoding || !(s->mpeg2 || s->codec_id==CODEC_ID_MPEG1VIDEO)){
|
|
|
|
put_dct(s, block[0], 0, dest_y, dct_linesize);
|
|
|
|
put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
|
|
|
|
put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
|
|
|
|
put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
|
|
|
|
|
|
|
|
if(!(s->flags&CODEC_FLAG_GRAY)){
|
|
|
|
put_dct(s, block[4], 4, dest_cb, s->uvlinesize);
|
|
|
|
put_dct(s, block[5], 5, dest_cr, s->uvlinesize);
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
s->idct_put(dest_y , dct_linesize, block[0]);
|
|
|
|
s->idct_put(dest_y + 8, dct_linesize, block[1]);
|
|
|
|
s->idct_put(dest_y + dct_offset , dct_linesize, block[2]);
|
|
|
|
s->idct_put(dest_y + dct_offset + 8, dct_linesize, block[3]);
|
|
|
|
|
|
|
|
if(!(s->flags&CODEC_FLAG_GRAY)){
|
|
|
|
s->idct_put(dest_cb, s->uvlinesize, block[4]);
|
|
|
|
s->idct_put(dest_cr, s->uvlinesize, block[5]);
|
|
|
|
}
|
2002-06-09 15:35:56 +02:00
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
}
|
2001-07-24 22:43:41 +02:00
|
|
|
the_end:
|
2002-03-27 22:25:22 +01:00
|
|
|
emms_c(); //FIXME remove
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
|
2002-08-18 02:42:58 +02:00
|
|
|
static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
|
2002-06-02 14:20:39 +02:00
|
|
|
{
|
|
|
|
static const char tab[64]=
|
|
|
|
{3,2,2,1,1,1,1,1,
|
|
|
|
1,1,1,1,1,1,1,1,
|
|
|
|
1,1,1,1,1,1,1,1,
|
|
|
|
0,0,0,0,0,0,0,0,
|
|
|
|
0,0,0,0,0,0,0,0,
|
|
|
|
0,0,0,0,0,0,0,0,
|
|
|
|
0,0,0,0,0,0,0,0,
|
|
|
|
0,0,0,0,0,0,0,0};
|
|
|
|
int score=0;
|
|
|
|
int run=0;
|
|
|
|
int i;
|
|
|
|
DCTELEM *block= s->block[n];
|
|
|
|
const int last_index= s->block_last_index[n];
|
2002-08-18 02:42:58 +02:00
|
|
|
int skip_dc;
|
2002-06-02 14:20:39 +02:00
|
|
|
|
2002-08-17 22:10:27 +02:00
|
|
|
if(threshold<0){
|
|
|
|
skip_dc=0;
|
|
|
|
threshold= -threshold;
|
2002-08-18 02:42:58 +02:00
|
|
|
}else
|
|
|
|
skip_dc=1;
|
2002-08-17 22:10:27 +02:00
|
|
|
|
2002-06-02 14:20:39 +02:00
|
|
|
/* are all which we could set to zero are allready zero? */
|
|
|
|
if(last_index<=skip_dc - 1) return;
|
|
|
|
|
|
|
|
for(i=0; i<=last_index; i++){
|
2002-09-30 00:44:22 +02:00
|
|
|
const int j = s->intra_scantable.permutated[i];
|
2002-06-02 14:20:39 +02:00
|
|
|
const int level = ABS(block[j]);
|
|
|
|
if(level==1){
|
|
|
|
if(skip_dc && i==0) continue;
|
|
|
|
score+= tab[run];
|
|
|
|
run=0;
|
|
|
|
}else if(level>1){
|
|
|
|
return;
|
|
|
|
}else{
|
|
|
|
run++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(score >= threshold) return;
|
|
|
|
for(i=skip_dc; i<=last_index; i++){
|
2002-09-30 00:44:22 +02:00
|
|
|
const int j = s->intra_scantable.permutated[i];
|
2002-06-02 14:20:39 +02:00
|
|
|
block[j]=0;
|
|
|
|
}
|
|
|
|
if(block[0]) s->block_last_index[n]= 0;
|
|
|
|
else s->block_last_index[n]= -1;
|
|
|
|
}
|
|
|
|
|
2002-04-27 14:30:26 +02:00
|
|
|
static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const int maxlevel= s->max_qcoeff;
|
|
|
|
const int minlevel= s->min_qcoeff;
|
2002-09-30 00:44:22 +02:00
|
|
|
|
|
|
|
if(s->mb_intra){
|
|
|
|
i=1; //skip clipping of intra dc
|
|
|
|
}else
|
|
|
|
i=0;
|
|
|
|
|
|
|
|
for(;i<=last_index; i++){
|
|
|
|
const int j= s->intra_scantable.permutated[i];
|
2002-04-27 14:30:26 +02:00
|
|
|
int level = block[j];
|
|
|
|
|
|
|
|
if (level>maxlevel) level=maxlevel;
|
|
|
|
else if(level<minlevel) level=minlevel;
|
|
|
|
block[j]= level;
|
|
|
|
}
|
|
|
|
}
|
2002-04-17 06:32:12 +02:00
|
|
|
|
2002-09-26 02:22:25 +02:00
|
|
|
static inline void requantize_coeffs(MpegEncContext *s, DCTELEM block[64], int oldq, int newq, int n)
|
|
|
|
{
|
|
|
|
int i;
|
2002-09-30 00:44:22 +02:00
|
|
|
|
|
|
|
if(s->mb_intra){
|
|
|
|
i=1; //skip clipping of intra dc
|
|
|
|
//FIXME requantize, note (mpeg1/h263/h263p-aic dont need it,...)
|
2002-09-26 02:22:25 +02:00
|
|
|
}else
|
|
|
|
i=0;
|
|
|
|
|
|
|
|
for(;i<=s->block_last_index[n]; i++){
|
2002-09-30 00:44:22 +02:00
|
|
|
const int j = s->intra_scantable.permutated[i];
|
2002-09-26 02:22:25 +02:00
|
|
|
int level = block[j];
|
|
|
|
|
|
|
|
block[j]= ROUNDED_DIV(level*oldq, newq);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i=s->block_last_index[n]; i>=0; i--){
|
2002-09-30 00:44:22 +02:00
|
|
|
const int j = s->intra_scantable.permutated[i];
|
2002-09-26 02:22:25 +02:00
|
|
|
if(block[j]) break;
|
|
|
|
}
|
|
|
|
s->block_last_index[n]= i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void auto_requantize_coeffs(MpegEncContext *s, DCTELEM block[6][64])
|
|
|
|
{
|
|
|
|
int i,n, newq;
|
|
|
|
const int maxlevel= s->max_qcoeff;
|
|
|
|
const int minlevel= s->min_qcoeff;
|
|
|
|
int largest=0, smallest=0;
|
|
|
|
|
|
|
|
assert(s->adaptive_quant);
|
|
|
|
|
|
|
|
for(n=0; n<6; n++){
|
2002-09-30 00:44:22 +02:00
|
|
|
if(s->mb_intra){
|
|
|
|
i=1; //skip clipping of intra dc
|
|
|
|
//FIXME requantize, note (mpeg1/h263/h263p-aic dont need it,...)
|
|
|
|
}else
|
|
|
|
i=0;
|
2002-09-26 02:22:25 +02:00
|
|
|
|
|
|
|
for(;i<=s->block_last_index[n]; i++){
|
2002-09-30 00:44:22 +02:00
|
|
|
const int j = s->intra_scantable.permutated[i];
|
2002-09-26 02:22:25 +02:00
|
|
|
int level = block[n][j];
|
|
|
|
if(largest < level) largest = level;
|
|
|
|
if(smallest > level) smallest= level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(newq=s->qscale+1; newq<32; newq++){
|
|
|
|
if( ROUNDED_DIV(smallest*s->qscale, newq) >= minlevel
|
|
|
|
&& ROUNDED_DIV(largest *s->qscale, newq) <= maxlevel)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(s->out_format==FMT_H263){
|
|
|
|
/* h263 like formats cannot change qscale by more than 2 easiely */
|
|
|
|
if(s->avctx->qmin + 2 < newq)
|
|
|
|
newq= s->avctx->qmin + 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(n=0; n<6; n++){
|
|
|
|
requantize_coeffs(s, block[n], s->qscale, newq, n);
|
|
|
|
clip_coeffs(s, block[n], s->block_last_index[n]);
|
|
|
|
}
|
|
|
|
|
|
|
|
s->dquant+= newq - s->qscale;
|
|
|
|
s->qscale= newq;
|
|
|
|
}
|
|
|
|
#if 0
|
|
|
|
static int pix_vcmp16x8(UINT8 *s, int stride){ //FIXME move to dsputil & optimize
|
|
|
|
int score=0;
|
|
|
|
int x,y;
|
|
|
|
|
|
|
|
for(y=0; y<7; y++){
|
|
|
|
for(x=0; x<16; x+=4){
|
|
|
|
score+= ABS(s[x ] - s[x +stride]) + ABS(s[x+1] - s[x+1+stride])
|
|
|
|
+ABS(s[x+2] - s[x+2+stride]) + ABS(s[x+3] - s[x+3+stride]);
|
|
|
|
}
|
|
|
|
s+= stride;
|
|
|
|
}
|
|
|
|
|
|
|
|
return score;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pix_diff_vcmp16x8(UINT8 *s1, UINT8*s2, int stride){ //FIXME move to dsputil & optimize
|
|
|
|
int score=0;
|
|
|
|
int x,y;
|
|
|
|
|
|
|
|
for(y=0; y<7; y++){
|
|
|
|
for(x=0; x<16; x++){
|
|
|
|
score+= ABS(s1[x ] - s2[x ] - s1[x +stride] + s2[x +stride]);
|
|
|
|
}
|
|
|
|
s1+= stride;
|
|
|
|
s2+= stride;
|
|
|
|
}
|
|
|
|
|
|
|
|
return score;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define SQ(a) ((a)*(a))
|
|
|
|
|
|
|
|
static int pix_vcmp16x8(UINT8 *s, int stride){ //FIXME move to dsputil & optimize
|
|
|
|
int score=0;
|
|
|
|
int x,y;
|
|
|
|
|
|
|
|
for(y=0; y<7; y++){
|
|
|
|
for(x=0; x<16; x+=4){
|
|
|
|
score+= SQ(s[x ] - s[x +stride]) + SQ(s[x+1] - s[x+1+stride])
|
|
|
|
+SQ(s[x+2] - s[x+2+stride]) + SQ(s[x+3] - s[x+3+stride]);
|
|
|
|
}
|
|
|
|
s+= stride;
|
|
|
|
}
|
|
|
|
|
|
|
|
return score;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pix_diff_vcmp16x8(UINT8 *s1, UINT8*s2, int stride){ //FIXME move to dsputil & optimize
|
|
|
|
int score=0;
|
|
|
|
int x,y;
|
|
|
|
|
|
|
|
for(y=0; y<7; y++){
|
|
|
|
for(x=0; x<16; x++){
|
|
|
|
score+= SQ(s1[x ] - s2[x ] - s1[x +stride] + s2[x +stride]);
|
|
|
|
}
|
|
|
|
s1+= stride;
|
|
|
|
s2+= stride;
|
|
|
|
}
|
|
|
|
|
|
|
|
return score;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2002-04-17 06:32:12 +02:00
|
|
|
static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
2002-03-27 22:25:22 +01:00
|
|
|
const int mb_x= s->mb_x;
|
|
|
|
const int mb_y= s->mb_y;
|
|
|
|
int i;
|
2002-06-02 14:20:39 +02:00
|
|
|
int skip_dct[6];
|
2002-09-26 02:22:25 +02:00
|
|
|
int dct_offset = s->linesize*8; //default for progressive frames
|
|
|
|
|
2002-06-02 14:20:39 +02:00
|
|
|
for(i=0; i<6; i++) skip_dct[i]=0;
|
2002-09-23 16:56:11 +02:00
|
|
|
|
|
|
|
if(s->adaptive_quant){
|
|
|
|
s->dquant= s->qscale_table[mb_x + mb_y*s->mb_width] - s->qscale;
|
2002-09-26 02:22:25 +02:00
|
|
|
|
|
|
|
if(s->out_format==FMT_H263){
|
2002-09-23 16:56:11 +02:00
|
|
|
if (s->dquant> 2) s->dquant= 2;
|
|
|
|
else if(s->dquant<-2) s->dquant=-2;
|
2002-09-26 02:22:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(s->codec_id==CODEC_ID_MPEG4){
|
2002-09-23 16:56:11 +02:00
|
|
|
if(!s->mb_intra){
|
|
|
|
assert(s->dquant==0 || s->mv_type!=MV_TYPE_8X8);
|
|
|
|
|
|
|
|
if(s->mv_dir&MV_DIRECT)
|
|
|
|
s->dquant=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s->qscale+= s->dquant;
|
|
|
|
s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
|
|
|
|
s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
|
|
|
|
}
|
2002-03-27 22:25:22 +01:00
|
|
|
|
2002-04-17 06:32:12 +02:00
|
|
|
if (s->mb_intra) {
|
|
|
|
UINT8 *ptr;
|
2002-09-26 02:22:25 +02:00
|
|
|
int wrap_y;
|
2002-07-18 01:32:31 +02:00
|
|
|
int emu=0;
|
2002-04-17 06:32:12 +02:00
|
|
|
|
2002-09-26 02:22:25 +02:00
|
|
|
wrap_y = s->linesize;
|
|
|
|
ptr = s->new_picture[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
|
|
|
|
|
2002-07-18 01:32:31 +02:00
|
|
|
if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
|
2002-09-26 02:22:25 +02:00
|
|
|
emulated_edge_mc(s, ptr, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
|
2002-07-18 01:32:31 +02:00
|
|
|
ptr= s->edge_emu_buffer;
|
|
|
|
emu=1;
|
|
|
|
}
|
2002-09-26 02:22:25 +02:00
|
|
|
|
|
|
|
if(s->flags&CODEC_FLAG_INTERLACED_DCT){
|
|
|
|
int progressive_score, interlaced_score;
|
|
|
|
|
|
|
|
progressive_score= pix_vcmp16x8(ptr, wrap_y ) + pix_vcmp16x8(ptr + wrap_y*8, wrap_y );
|
|
|
|
interlaced_score = pix_vcmp16x8(ptr, wrap_y*2) + pix_vcmp16x8(ptr + wrap_y , wrap_y*2);
|
|
|
|
|
|
|
|
if(progressive_score > interlaced_score + 100){
|
|
|
|
s->interlaced_dct=1;
|
|
|
|
|
|
|
|
dct_offset= wrap_y;
|
|
|
|
wrap_y<<=1;
|
|
|
|
}else
|
|
|
|
s->interlaced_dct=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
get_pixels(s->block[0], ptr , wrap_y);
|
|
|
|
get_pixels(s->block[1], ptr + 8, wrap_y);
|
|
|
|
get_pixels(s->block[2], ptr + dct_offset , wrap_y);
|
|
|
|
get_pixels(s->block[3], ptr + dct_offset + 8, wrap_y);
|
2002-04-17 06:32:12 +02:00
|
|
|
|
2002-06-09 16:04:36 +02:00
|
|
|
if(s->flags&CODEC_FLAG_GRAY){
|
|
|
|
skip_dct[4]= 1;
|
|
|
|
skip_dct[5]= 1;
|
|
|
|
}else{
|
2002-09-26 02:22:25 +02:00
|
|
|
int wrap_c = s->uvlinesize;
|
|
|
|
ptr = s->new_picture[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
|
2002-07-18 01:32:31 +02:00
|
|
|
if(emu){
|
2002-09-26 02:22:25 +02:00
|
|
|
emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
|
2002-07-18 01:32:31 +02:00
|
|
|
ptr= s->edge_emu_buffer;
|
|
|
|
}
|
2002-09-26 02:22:25 +02:00
|
|
|
get_pixels(s->block[4], ptr, wrap_c);
|
2002-04-17 06:32:12 +02:00
|
|
|
|
2002-09-26 02:22:25 +02:00
|
|
|
ptr = s->new_picture[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
|
2002-07-18 01:32:31 +02:00
|
|
|
if(emu){
|
2002-09-26 02:22:25 +02:00
|
|
|
emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
|
2002-07-18 01:32:31 +02:00
|
|
|
ptr= s->edge_emu_buffer;
|
|
|
|
}
|
2002-09-26 02:22:25 +02:00
|
|
|
get_pixels(s->block[5], ptr, wrap_c);
|
2002-06-09 16:04:36 +02:00
|
|
|
}
|
2002-04-17 06:32:12 +02:00
|
|
|
}else{
|
2002-09-11 14:39:53 +02:00
|
|
|
op_pixels_func (*op_pix)[4];
|
|
|
|
qpel_mc_func (*op_qpix)[16];
|
2002-04-17 06:32:12 +02:00
|
|
|
UINT8 *dest_y, *dest_cb, *dest_cr;
|
2002-06-02 14:20:39 +02:00
|
|
|
UINT8 *ptr_y, *ptr_cb, *ptr_cr;
|
|
|
|
int wrap_y, wrap_c;
|
2002-07-18 01:32:31 +02:00
|
|
|
int emu=0;
|
2002-04-17 06:32:12 +02:00
|
|
|
|
|
|
|
dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize ) + mb_x * 16;
|
2002-07-15 16:15:10 +02:00
|
|
|
dest_cb = s->current_picture[1] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
|
|
|
|
dest_cr = s->current_picture[2] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
|
2002-06-02 14:20:39 +02:00
|
|
|
wrap_y = s->linesize;
|
2002-09-26 02:22:25 +02:00
|
|
|
wrap_c = s->uvlinesize;
|
2002-06-02 14:20:39 +02:00
|
|
|
ptr_y = s->new_picture[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
|
|
|
|
ptr_cb = s->new_picture[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
|
|
|
|
ptr_cr = s->new_picture[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
|
2002-04-17 06:32:12 +02:00
|
|
|
|
2002-04-19 05:25:20 +02:00
|
|
|
if ((!s->no_rounding) || s->pict_type==B_TYPE){
|
2002-04-17 06:32:12 +02:00
|
|
|
op_pix = put_pixels_tab;
|
2002-09-11 14:39:53 +02:00
|
|
|
op_qpix= put_qpel_pixels_tab;
|
2002-03-28 05:25:35 +01:00
|
|
|
}else{
|
2002-04-17 06:32:12 +02:00
|
|
|
op_pix = put_no_rnd_pixels_tab;
|
2002-09-11 14:39:53 +02:00
|
|
|
op_qpix= put_no_rnd_qpel_pixels_tab;
|
2002-04-17 06:32:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (s->mv_dir & MV_DIR_FORWARD) {
|
|
|
|
MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
|
2002-09-11 14:39:53 +02:00
|
|
|
op_pix = avg_pixels_tab;
|
|
|
|
op_qpix= avg_qpel_pixels_tab;
|
2002-04-17 06:32:12 +02:00
|
|
|
}
|
|
|
|
if (s->mv_dir & MV_DIR_BACKWARD) {
|
|
|
|
MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
|
2002-03-27 22:25:22 +01:00
|
|
|
}
|
2002-04-17 06:32:12 +02:00
|
|
|
|
2002-07-18 01:32:31 +02:00
|
|
|
if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
|
|
|
|
emulated_edge_mc(s, ptr_y, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
|
|
|
|
ptr_y= s->edge_emu_buffer;
|
|
|
|
emu=1;
|
|
|
|
}
|
2002-09-26 02:22:25 +02:00
|
|
|
|
|
|
|
if(s->flags&CODEC_FLAG_INTERLACED_DCT){
|
|
|
|
int progressive_score, interlaced_score;
|
|
|
|
|
|
|
|
progressive_score= pix_diff_vcmp16x8(ptr_y , dest_y , wrap_y )
|
|
|
|
+ pix_diff_vcmp16x8(ptr_y + wrap_y*8, dest_y + wrap_y*8, wrap_y );
|
|
|
|
interlaced_score = pix_diff_vcmp16x8(ptr_y , dest_y , wrap_y*2)
|
|
|
|
+ pix_diff_vcmp16x8(ptr_y + wrap_y , dest_y + wrap_y , wrap_y*2);
|
|
|
|
|
|
|
|
if(progressive_score > interlaced_score + 600){
|
|
|
|
s->interlaced_dct=1;
|
|
|
|
|
|
|
|
dct_offset= wrap_y;
|
|
|
|
wrap_y<<=1;
|
|
|
|
}else
|
|
|
|
s->interlaced_dct=0;
|
|
|
|
}
|
|
|
|
|
2002-06-02 14:20:39 +02:00
|
|
|
diff_pixels(s->block[0], ptr_y , dest_y , wrap_y);
|
|
|
|
diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
|
2002-09-26 02:22:25 +02:00
|
|
|
diff_pixels(s->block[2], ptr_y + dct_offset , dest_y + dct_offset , wrap_y);
|
|
|
|
diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
|
2002-06-09 16:04:36 +02:00
|
|
|
|
|
|
|
if(s->flags&CODEC_FLAG_GRAY){
|
|
|
|
skip_dct[4]= 1;
|
|
|
|
skip_dct[5]= 1;
|
|
|
|
}else{
|
2002-07-18 01:32:31 +02:00
|
|
|
if(emu){
|
|
|
|
emulated_edge_mc(s, ptr_cb, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
|
|
|
|
ptr_cb= s->edge_emu_buffer;
|
|
|
|
}
|
2002-06-09 16:04:36 +02:00
|
|
|
diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
|
2002-07-18 01:32:31 +02:00
|
|
|
if(emu){
|
|
|
|
emulated_edge_mc(s, ptr_cr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
|
|
|
|
ptr_cr= s->edge_emu_buffer;
|
|
|
|
}
|
2002-06-09 16:04:36 +02:00
|
|
|
diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
|
|
|
|
}
|
|
|
|
|
2002-06-02 14:20:39 +02:00
|
|
|
/* pre quantization */
|
|
|
|
if(s->mc_mb_var[s->mb_width*mb_y+ mb_x]<2*s->qscale*s->qscale){
|
2002-09-26 02:22:25 +02:00
|
|
|
//FIXME optimize
|
2002-06-02 14:20:39 +02:00
|
|
|
if(pix_abs8x8(ptr_y , dest_y , wrap_y) < 20*s->qscale) skip_dct[0]= 1;
|
|
|
|
if(pix_abs8x8(ptr_y + 8, dest_y + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1;
|
2002-09-26 02:22:25 +02:00
|
|
|
if(pix_abs8x8(ptr_y +dct_offset , dest_y +dct_offset , wrap_y) < 20*s->qscale) skip_dct[2]= 1;
|
|
|
|
if(pix_abs8x8(ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1;
|
2002-06-02 14:20:39 +02:00
|
|
|
if(pix_abs8x8(ptr_cb , dest_cb , wrap_y) < 20*s->qscale) skip_dct[4]= 1;
|
|
|
|
if(pix_abs8x8(ptr_cr , dest_cr , wrap_y) < 20*s->qscale) skip_dct[5]= 1;
|
|
|
|
#if 0
|
|
|
|
{
|
|
|
|
static int stat[7];
|
|
|
|
int num=0;
|
|
|
|
for(i=0; i<6; i++)
|
|
|
|
if(skip_dct[i]) num++;
|
|
|
|
stat[num]++;
|
|
|
|
|
|
|
|
if(s->mb_x==0 && s->mb_y==0){
|
|
|
|
for(i=0; i<7; i++){
|
|
|
|
printf("%6d %1d\n", stat[i], i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2002-04-17 06:32:12 +02:00
|
|
|
|
2002-03-27 22:25:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
{
|
|
|
|
float adap_parm;
|
|
|
|
|
|
|
|
adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) /
|
|
|
|
((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
|
|
|
|
|
|
|
|
printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d",
|
|
|
|
(s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P',
|
|
|
|
s->qscale, adap_parm, s->qscale*adap_parm,
|
|
|
|
s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* DCT & quantize */
|
2002-04-27 14:30:26 +02:00
|
|
|
if(s->out_format==FMT_MJPEG){
|
|
|
|
for(i=0;i<6;i++) {
|
|
|
|
int overflow;
|
2002-08-30 01:55:32 +02:00
|
|
|
s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, 8, &overflow);
|
2002-05-02 06:39:45 +02:00
|
|
|
if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
|
2002-04-27 14:30:26 +02:00
|
|
|
}
|
|
|
|
}else{
|
|
|
|
for(i=0;i<6;i++) {
|
2002-06-02 14:20:39 +02:00
|
|
|
if(!skip_dct[i]){
|
|
|
|
int overflow;
|
2002-08-30 01:55:32 +02:00
|
|
|
s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
|
2002-04-27 14:30:26 +02:00
|
|
|
// FIXME we could decide to change to quantizer instead of clipping
|
2002-05-02 06:39:45 +02:00
|
|
|
// JS: I don't think that would be a good idea it could lower quality instead
|
|
|
|
// of improve it. Just INTRADC clipping deserves changes in quantizer
|
2002-06-02 14:20:39 +02:00
|
|
|
if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
|
|
|
|
}else
|
|
|
|
s->block_last_index[i]= -1;
|
2002-04-27 14:30:26 +02:00
|
|
|
}
|
2002-06-02 14:20:39 +02:00
|
|
|
if(s->luma_elim_threshold && !s->mb_intra)
|
|
|
|
for(i=0; i<4; i++)
|
2002-08-18 02:42:58 +02:00
|
|
|
dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
|
2002-06-02 14:20:39 +02:00
|
|
|
if(s->chroma_elim_threshold && !s->mb_intra)
|
|
|
|
for(i=4; i<6; i++)
|
2002-08-18 02:42:58 +02:00
|
|
|
dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
|
2002-03-27 22:25:22 +01:00
|
|
|
}
|
|
|
|
|
2002-06-09 16:04:36 +02:00
|
|
|
if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
|
|
|
|
s->block_last_index[4]=
|
|
|
|
s->block_last_index[5]= 0;
|
|
|
|
s->block[4][0]=
|
|
|
|
s->block[5][0]= 128;
|
|
|
|
}
|
|
|
|
|
2002-03-27 22:25:22 +01:00
|
|
|
/* huffman encode */
|
|
|
|
switch(s->out_format) {
|
|
|
|
case FMT_MPEG1:
|
|
|
|
mpeg1_encode_mb(s, s->block, motion_x, motion_y);
|
|
|
|
break;
|
|
|
|
case FMT_H263:
|
|
|
|
if (s->h263_msmpeg4)
|
|
|
|
msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
|
|
|
|
else if(s->h263_pred)
|
|
|
|
mpeg4_encode_mb(s, s->block, motion_x, motion_y);
|
|
|
|
else
|
|
|
|
h263_encode_mb(s, s->block, motion_x, motion_y);
|
|
|
|
break;
|
|
|
|
case FMT_MJPEG:
|
|
|
|
mjpeg_encode_mb(s, s->block);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-02 14:20:39 +02:00
|
|
|
void ff_copy_bits(PutBitContext *pb, UINT8 *src, int length)
|
2002-03-27 22:25:22 +01:00
|
|
|
{
|
2002-04-17 20:19:50 +02:00
|
|
|
int bytes= length>>4;
|
|
|
|
int bits= length&15;
|
|
|
|
int i;
|
|
|
|
|
2002-06-02 14:20:39 +02:00
|
|
|
if(length==0) return;
|
|
|
|
|
2002-04-17 20:19:50 +02:00
|
|
|
for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
|
|
|
|
put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
|
2002-03-27 22:25:22 +01:00
|
|
|
}
|
|
|
|
|
2002-06-02 14:20:39 +02:00
|
|
|
static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
|
2002-04-17 20:19:50 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
|
|
|
|
|
|
|
|
/* mpeg1 */
|
|
|
|
d->mb_incr= s->mb_incr;
|
|
|
|
for(i=0; i<3; i++)
|
|
|
|
d->last_dc[i]= s->last_dc[i];
|
|
|
|
|
|
|
|
/* statistics */
|
|
|
|
d->mv_bits= s->mv_bits;
|
|
|
|
d->i_tex_bits= s->i_tex_bits;
|
|
|
|
d->p_tex_bits= s->p_tex_bits;
|
|
|
|
d->i_count= s->i_count;
|
2002-09-12 14:26:09 +02:00
|
|
|
d->f_count= s->f_count;
|
|
|
|
d->b_count= s->b_count;
|
2002-04-17 20:19:50 +02:00
|
|
|
d->skip_count= s->skip_count;
|
|
|
|
d->misc_bits= s->misc_bits;
|
2002-04-21 23:18:17 +02:00
|
|
|
d->last_bits= 0;
|
2002-04-19 05:25:20 +02:00
|
|
|
|
|
|
|
d->mb_skiped= s->mb_skiped;
|
2002-04-17 20:19:50 +02:00
|
|
|
}
|
|
|
|
|
2002-06-02 14:20:39 +02:00
|
|
|
static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
|
2002-04-17 20:19:50 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
|
|
|
|
memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
|
|
|
|
|
|
|
|
/* mpeg1 */
|
|
|
|
d->mb_incr= s->mb_incr;
|
|
|
|
for(i=0; i<3; i++)
|
|
|
|
d->last_dc[i]= s->last_dc[i];
|
|
|
|
|
|
|
|
/* statistics */
|
|
|
|
d->mv_bits= s->mv_bits;
|
|
|
|
d->i_tex_bits= s->i_tex_bits;
|
|
|
|
d->p_tex_bits= s->p_tex_bits;
|
|
|
|
d->i_count= s->i_count;
|
2002-09-12 14:26:09 +02:00
|
|
|
d->f_count= s->f_count;
|
|
|
|
d->b_count= s->b_count;
|
2002-04-17 20:19:50 +02:00
|
|
|
d->skip_count= s->skip_count;
|
|
|
|
d->misc_bits= s->misc_bits;
|
|
|
|
|
|
|
|
d->mb_intra= s->mb_intra;
|
2002-04-19 05:25:20 +02:00
|
|
|
d->mb_skiped= s->mb_skiped;
|
2002-04-17 20:19:50 +02:00
|
|
|
d->mv_type= s->mv_type;
|
|
|
|
d->mv_dir= s->mv_dir;
|
|
|
|
d->pb= s->pb;
|
2002-06-02 14:20:39 +02:00
|
|
|
if(s->data_partitioning){
|
|
|
|
d->pb2= s->pb2;
|
|
|
|
d->tex_pb= s->tex_pb;
|
|
|
|
}
|
2002-04-17 20:19:50 +02:00
|
|
|
d->block= s->block;
|
|
|
|
for(i=0; i<6; i++)
|
|
|
|
d->block_last_index[i]= s->block_last_index[i];
|
|
|
|
}
|
|
|
|
|
2002-06-02 14:20:39 +02:00
|
|
|
static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
|
|
|
|
PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
|
|
|
|
int *dmin, int *next_block, int motion_x, int motion_y)
|
|
|
|
{
|
|
|
|
int bits_count;
|
|
|
|
|
|
|
|
copy_context_before_encode(s, backup, type);
|
|
|
|
|
|
|
|
s->block= s->blocks[*next_block];
|
|
|
|
s->pb= pb[*next_block];
|
|
|
|
if(s->data_partitioning){
|
|
|
|
s->pb2 = pb2 [*next_block];
|
|
|
|
s->tex_pb= tex_pb[*next_block];
|
|
|
|
}
|
|
|
|
|
|
|
|
encode_mb(s, motion_x, motion_y);
|
|
|
|
|
|
|
|
bits_count= get_bit_count(&s->pb);
|
|
|
|
if(s->data_partitioning){
|
|
|
|
bits_count+= get_bit_count(&s->pb2);
|
|
|
|
bits_count+= get_bit_count(&s->tex_pb);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(bits_count<*dmin){
|
|
|
|
*dmin= bits_count;
|
|
|
|
*next_block^=1;
|
|
|
|
|
|
|
|
copy_context_after_encode(best, s, type);
|
|
|
|
}
|
|
|
|
}
|
2002-04-17 20:19:50 +02:00
|
|
|
|
2002-03-27 22:25:22 +01:00
|
|
|
static void encode_picture(MpegEncContext *s, int picture_number)
|
|
|
|
{
|
|
|
|
int mb_x, mb_y, last_gob, pdif = 0;
|
|
|
|
int i;
|
2002-03-23 18:43:30 +01:00
|
|
|
int bits;
|
2002-04-17 20:19:50 +02:00
|
|
|
MpegEncContext best_s, backup_s;
|
2002-06-02 14:20:39 +02:00
|
|
|
UINT8 bit_buf[2][3000];
|
|
|
|
UINT8 bit_buf2[2][3000];
|
|
|
|
UINT8 bit_buf_tex[2][3000];
|
|
|
|
PutBitContext pb[2], pb2[2], tex_pb[2];
|
|
|
|
|
|
|
|
for(i=0; i<2; i++){
|
|
|
|
init_put_bits(&pb [i], bit_buf [i], 3000, NULL, NULL);
|
|
|
|
init_put_bits(&pb2 [i], bit_buf2 [i], 3000, NULL, NULL);
|
|
|
|
init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000, NULL, NULL);
|
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
s->picture_number = picture_number;
|
2002-03-17 17:31:38 +01:00
|
|
|
|
2002-03-27 22:25:22 +01:00
|
|
|
s->block_wrap[0]=
|
|
|
|
s->block_wrap[1]=
|
|
|
|
s->block_wrap[2]=
|
|
|
|
s->block_wrap[3]= s->mb_width*2 + 2;
|
|
|
|
s->block_wrap[4]=
|
|
|
|
s->block_wrap[5]= s->mb_width + 2;
|
|
|
|
|
2002-03-17 17:31:38 +01:00
|
|
|
/* Reset the average MB variance */
|
2002-06-02 14:20:39 +02:00
|
|
|
s->mb_var_sum = 0;
|
|
|
|
s->mc_mb_var_sum = 0;
|
2002-04-19 05:25:20 +02:00
|
|
|
|
|
|
|
/* we need to initialize some time vars before we can encode b-frames */
|
|
|
|
if (s->h263_pred && !s->h263_msmpeg4)
|
|
|
|
ff_set_mpeg4_time(s, s->picture_number);
|
|
|
|
|
2002-08-22 21:41:06 +02:00
|
|
|
s->scene_change_score=0;
|
2002-09-23 16:56:11 +02:00
|
|
|
|
|
|
|
s->qscale= (int)(s->frame_qscale + 0.5); //FIXME qscale / ... stuff for ME ratedistoration
|
2002-08-22 21:41:06 +02:00
|
|
|
|
2002-03-17 17:31:38 +01:00
|
|
|
/* Estimate motion for every MB */
|
2002-04-17 06:32:12 +02:00
|
|
|
if(s->pict_type != I_TYPE){
|
2002-03-27 22:25:22 +01:00
|
|
|
for(mb_y=0; mb_y < s->mb_height; mb_y++) {
|
|
|
|
s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
|
|
|
|
s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
|
|
|
|
s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
|
|
|
|
s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
|
|
|
|
for(mb_x=0; mb_x < s->mb_width; mb_x++) {
|
|
|
|
s->mb_x = mb_x;
|
|
|
|
s->mb_y = mb_y;
|
|
|
|
s->block_index[0]+=2;
|
|
|
|
s->block_index[1]+=2;
|
|
|
|
s->block_index[2]+=2;
|
|
|
|
s->block_index[3]+=2;
|
|
|
|
|
|
|
|
/* compute motion vector & mb_type and store in context */
|
2002-04-17 06:32:12 +02:00
|
|
|
if(s->pict_type==B_TYPE)
|
|
|
|
ff_estimate_b_frame_motion(s, mb_x, mb_y);
|
|
|
|
else
|
|
|
|
ff_estimate_p_frame_motion(s, mb_x, mb_y);
|
2002-03-27 22:25:22 +01:00
|
|
|
// s->mb_type[mb_y*s->mb_width + mb_x]=MB_TYPE_INTER;
|
2002-03-17 17:31:38 +01:00
|
|
|
}
|
|
|
|
}
|
2002-03-27 22:25:22 +01:00
|
|
|
emms_c();
|
2002-06-02 14:20:39 +02:00
|
|
|
}else /* if(s->pict_type == I_TYPE) */{
|
2002-03-27 22:25:22 +01:00
|
|
|
/* I-Frame */
|
|
|
|
//FIXME do we need to zero them?
|
|
|
|
memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
|
2002-04-17 06:32:12 +02:00
|
|
|
memset(s->p_mv_table , 0, sizeof(INT16)*(s->mb_width+2)*(s->mb_height+2)*2);
|
2002-03-27 22:25:22 +01:00
|
|
|
memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
|
2002-08-25 23:19:50 +02:00
|
|
|
|
|
|
|
if(!s->fixed_qscale){
|
|
|
|
/* finding spatial complexity for I-frame rate control */
|
|
|
|
for(mb_y=0; mb_y < s->mb_height; mb_y++) {
|
|
|
|
for(mb_x=0; mb_x < s->mb_width; mb_x++) {
|
|
|
|
int xx = mb_x * 16;
|
|
|
|
int yy = mb_y * 16;
|
|
|
|
uint8_t *pix = s->new_picture[0] + (yy * s->linesize) + xx;
|
|
|
|
int varc;
|
|
|
|
int sum = pix_sum(pix, s->linesize);
|
|
|
|
|
|
|
|
sum= (sum+8)>>4;
|
|
|
|
varc = (pix_norm1(pix, s->linesize) - sum*sum + 500 + 128)>>8;
|
|
|
|
|
2002-09-23 16:56:11 +02:00
|
|
|
s->mb_var [s->mb_width * mb_y + mb_x] = varc;
|
|
|
|
s->mb_mean[s->mb_width * mb_y + mb_x] = (sum+7)>>4;
|
2002-08-25 23:19:50 +02:00
|
|
|
s->mb_var_sum += varc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-03-17 17:31:38 +01:00
|
|
|
}
|
2002-08-22 21:41:06 +02:00
|
|
|
if(s->scene_change_score > 0 && s->pict_type == P_TYPE){
|
2002-03-17 20:45:33 +01:00
|
|
|
s->pict_type= I_TYPE;
|
2002-03-27 22:25:22 +01:00
|
|
|
memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
|
2002-04-21 23:18:17 +02:00
|
|
|
if(s->max_b_frames==0){
|
|
|
|
s->input_pict_type= I_TYPE;
|
|
|
|
s->input_picture_in_gop_number=0;
|
|
|
|
}
|
2002-08-25 23:19:50 +02:00
|
|
|
//printf("Scene change detected, encoding as I Frame %d %d\n", s->mb_var_sum, s->mc_mb_var_sum);
|
2002-03-17 20:45:33 +01:00
|
|
|
}
|
2002-04-17 06:32:12 +02:00
|
|
|
|
|
|
|
if(s->pict_type==P_TYPE || s->pict_type==S_TYPE)
|
|
|
|
s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
|
|
|
|
ff_fix_long_p_mvs(s);
|
|
|
|
if(s->pict_type==B_TYPE){
|
|
|
|
s->f_code= ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
|
|
|
|
s->b_code= ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
|
2002-04-19 05:25:20 +02:00
|
|
|
|
|
|
|
ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);
|
|
|
|
ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);
|
|
|
|
ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);
|
|
|
|
ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);
|
2002-03-22 03:21:17 +01:00
|
|
|
}
|
2002-04-17 06:32:12 +02:00
|
|
|
|
2002-09-23 16:56:11 +02:00
|
|
|
if (s->fixed_qscale)
|
|
|
|
s->frame_qscale = s->avctx->quality;
|
|
|
|
else
|
|
|
|
s->frame_qscale = ff_rate_estimate_qscale(s);
|
2002-04-21 23:18:17 +02:00
|
|
|
|
2002-09-24 17:07:44 +02:00
|
|
|
if(s->adaptive_quant){
|
|
|
|
switch(s->codec_id){
|
|
|
|
case CODEC_ID_MPEG4:
|
|
|
|
ff_clean_mpeg4_qscales(s);
|
|
|
|
break;
|
|
|
|
case CODEC_ID_H263:
|
|
|
|
case CODEC_ID_H263P:
|
|
|
|
ff_clean_h263_qscales(s);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-09-23 16:56:11 +02:00
|
|
|
s->qscale= s->qscale_table[0];
|
2002-09-24 17:07:44 +02:00
|
|
|
}else
|
2002-09-23 16:56:11 +02:00
|
|
|
s->qscale= (int)(s->frame_qscale + 0.5);
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
if (s->out_format == FMT_MJPEG) {
|
|
|
|
/* for mjpeg, we do include qscale in the matrix */
|
2002-07-10 23:01:45 +02:00
|
|
|
s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
|
2002-09-30 00:44:22 +02:00
|
|
|
for(i=1;i<64;i++){
|
|
|
|
int j= s->idct_permutation[i];
|
|
|
|
|
|
|
|
s->intra_matrix[j] = CLAMP_TO_8BIT((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
|
|
|
|
}
|
2002-08-30 01:55:32 +02:00
|
|
|
convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
|
2002-09-30 11:19:54 +02:00
|
|
|
s->q_intra_matrix16_bias, s->intra_matrix, s->intra_quant_bias, 8, 8);
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
|
2002-03-23 18:43:30 +01:00
|
|
|
s->last_bits= get_bit_count(&s->pb);
|
2001-07-22 16:18:56 +02:00
|
|
|
switch(s->out_format) {
|
|
|
|
case FMT_MJPEG:
|
|
|
|
mjpeg_picture_header(s);
|
|
|
|
break;
|
|
|
|
case FMT_H263:
|
|
|
|
if (s->h263_msmpeg4)
|
|
|
|
msmpeg4_encode_picture_header(s, picture_number);
|
|
|
|
else if (s->h263_pred)
|
|
|
|
mpeg4_encode_picture_header(s, picture_number);
|
|
|
|
else if (s->h263_rv10)
|
|
|
|
rv10_encode_picture_header(s, picture_number);
|
|
|
|
else
|
|
|
|
h263_encode_picture_header(s, picture_number);
|
|
|
|
break;
|
|
|
|
case FMT_MPEG1:
|
|
|
|
mpeg1_encode_picture_header(s, picture_number);
|
|
|
|
break;
|
|
|
|
}
|
2002-03-23 18:43:30 +01:00
|
|
|
bits= get_bit_count(&s->pb);
|
|
|
|
s->header_bits= bits - s->last_bits;
|
|
|
|
s->last_bits= bits;
|
|
|
|
s->mv_bits=0;
|
|
|
|
s->misc_bits=0;
|
|
|
|
s->i_tex_bits=0;
|
|
|
|
s->p_tex_bits=0;
|
|
|
|
s->i_count=0;
|
2002-09-12 14:26:09 +02:00
|
|
|
s->f_count=0;
|
|
|
|
s->b_count=0;
|
2002-03-23 18:43:30 +01:00
|
|
|
s->skip_count=0;
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
/* init last dc values */
|
|
|
|
/* note: quant matrix value (8) is implied here */
|
|
|
|
s->last_dc[0] = 128;
|
|
|
|
s->last_dc[1] = 128;
|
|
|
|
s->last_dc[2] = 128;
|
|
|
|
s->mb_incr = 1;
|
|
|
|
s->last_mv[0][0][0] = 0;
|
|
|
|
s->last_mv[0][0][1] = 0;
|
|
|
|
|
2001-11-19 03:13:14 +01:00
|
|
|
/* Get the GOB height based on picture height */
|
2002-02-09 17:59:56 +01:00
|
|
|
if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4) {
|
2001-11-19 03:13:14 +01:00
|
|
|
if (s->height <= 400)
|
|
|
|
s->gob_index = 1;
|
|
|
|
else if (s->height <= 800)
|
|
|
|
s->gob_index = 2;
|
|
|
|
else
|
|
|
|
s->gob_index = 4;
|
2002-06-02 14:20:39 +02:00
|
|
|
}else if(s->codec_id==CODEC_ID_MPEG4){
|
|
|
|
s->gob_index = 1;
|
2001-11-19 03:13:14 +01:00
|
|
|
}
|
2002-06-02 14:20:39 +02:00
|
|
|
|
|
|
|
if(s->codec_id==CODEC_ID_MPEG4 && s->data_partitioning && s->pict_type!=B_TYPE)
|
|
|
|
ff_mpeg4_init_partitions(s);
|
|
|
|
|
|
|
|
s->resync_mb_x=0;
|
|
|
|
s->resync_mb_y=0;
|
2002-02-10 07:10:50 +01:00
|
|
|
for(mb_y=0; mb_y < s->mb_height; mb_y++) {
|
2002-06-02 14:20:39 +02:00
|
|
|
/* Put GOB header based on RTP MTU for formats which support it per line (H263*)*/
|
2002-02-10 07:10:50 +01:00
|
|
|
/* TODO: Put all this stuff in a separate generic function */
|
|
|
|
if (s->rtp_mode) {
|
|
|
|
if (!mb_y) {
|
|
|
|
s->ptr_lastgob = s->pb.buf;
|
|
|
|
s->ptr_last_mb_line = s->pb.buf;
|
|
|
|
} else if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4 && !(mb_y % s->gob_index)) {
|
2002-06-02 14:20:39 +02:00
|
|
|
// MN: we could move the space check from h263 -> here, as its not h263 specific
|
2002-02-10 07:10:50 +01:00
|
|
|
last_gob = h263_encode_gob_header(s, mb_y);
|
|
|
|
if (last_gob) {
|
2002-06-02 14:20:39 +02:00
|
|
|
s->first_slice_line = 1;
|
|
|
|
}else{
|
|
|
|
/*MN: we reset it here instead at the end of each line cuz mpeg4 can have
|
|
|
|
slice lines starting & ending in the middle*/
|
|
|
|
s->first_slice_line = 0;
|
2002-02-10 07:10:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-06-18 02:49:00 +02:00
|
|
|
|
|
|
|
s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
|
|
|
|
s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
|
2002-02-10 07:10:50 +01:00
|
|
|
|
2002-03-15 15:57:17 +01:00
|
|
|
s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
|
|
|
|
s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
|
|
|
|
s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
|
|
|
|
s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
|
|
|
|
s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
|
|
|
|
s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
|
2002-02-10 02:56:50 +01:00
|
|
|
for(mb_x=0; mb_x < s->mb_width; mb_x++) {
|
2002-04-19 05:25:20 +02:00
|
|
|
const int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];
|
|
|
|
const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
|
2002-06-02 14:20:39 +02:00
|
|
|
// int d;
|
2002-03-27 22:25:22 +01:00
|
|
|
int dmin=10000000;
|
2002-02-10 02:56:50 +01:00
|
|
|
|
|
|
|
s->mb_x = mb_x;
|
|
|
|
s->mb_y = mb_y;
|
2002-03-15 15:57:17 +01:00
|
|
|
s->block_index[0]+=2;
|
|
|
|
s->block_index[1]+=2;
|
|
|
|
s->block_index[2]+=2;
|
|
|
|
s->block_index[3]+=2;
|
|
|
|
s->block_index[4]++;
|
|
|
|
s->block_index[5]++;
|
2002-06-02 14:20:39 +02:00
|
|
|
|
|
|
|
/* write gob / video packet header for formats which support it at any MB (MPEG4) */
|
|
|
|
if(s->rtp_mode && s->mb_y>0 && s->codec_id==CODEC_ID_MPEG4){
|
|
|
|
int pdif= pbBufPtr(&s->pb) - s->ptr_lastgob;
|
|
|
|
|
|
|
|
//the *2 is there so we stay below the requested size
|
|
|
|
if(pdif + s->mb_line_avgsize/s->mb_width >= s->rtp_payload_size){
|
|
|
|
if(s->codec_id==CODEC_ID_MPEG4){
|
|
|
|
if(s->data_partitioning && s->pict_type!=B_TYPE){
|
|
|
|
ff_mpeg4_merge_partitions(s);
|
|
|
|
ff_mpeg4_init_partitions(s);
|
|
|
|
}
|
|
|
|
ff_mpeg4_encode_video_packet_header(s);
|
|
|
|
|
|
|
|
if(s->flags&CODEC_FLAG_PASS1){
|
|
|
|
int bits= get_bit_count(&s->pb);
|
|
|
|
s->misc_bits+= bits - s->last_bits;
|
|
|
|
s->last_bits= bits;
|
|
|
|
}
|
|
|
|
ff_mpeg4_clean_buffers(s);
|
|
|
|
}
|
|
|
|
s->ptr_lastgob = pbBufPtr(&s->pb);
|
|
|
|
s->first_slice_line=1;
|
|
|
|
s->resync_mb_x=mb_x;
|
|
|
|
s->resync_mb_y=mb_y;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( (s->resync_mb_x == s->mb_x)
|
|
|
|
&& s->resync_mb_y+1 == s->mb_y){
|
|
|
|
s->first_slice_line=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-27 22:25:22 +01:00
|
|
|
if(mb_type & (mb_type-1)){ // more than 1 MB type possible
|
2002-04-19 05:25:20 +02:00
|
|
|
int next_block=0;
|
2002-06-02 14:20:39 +02:00
|
|
|
int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
|
2002-04-17 20:19:50 +02:00
|
|
|
|
|
|
|
copy_context_before_encode(&backup_s, s, -1);
|
2002-06-02 14:20:39 +02:00
|
|
|
backup_s.pb= s->pb;
|
|
|
|
best_s.data_partitioning= s->data_partitioning;
|
|
|
|
if(s->data_partitioning){
|
|
|
|
backup_s.pb2= s->pb2;
|
|
|
|
backup_s.tex_pb= s->tex_pb;
|
|
|
|
}
|
2002-04-17 20:19:50 +02:00
|
|
|
|
2002-03-27 22:25:22 +01:00
|
|
|
if(mb_type&MB_TYPE_INTER){
|
2002-04-19 05:25:20 +02:00
|
|
|
s->mv_dir = MV_DIR_FORWARD;
|
2002-03-28 05:25:35 +01:00
|
|
|
s->mv_type = MV_TYPE_16X16;
|
2002-03-27 22:25:22 +01:00
|
|
|
s->mb_intra= 0;
|
2002-04-17 06:32:12 +02:00
|
|
|
s->mv[0][0][0] = s->p_mv_table[xy][0];
|
|
|
|
s->mv[0][0][1] = s->p_mv_table[xy][1];
|
2002-06-02 14:20:39 +02:00
|
|
|
encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER, pb, pb2, tex_pb,
|
|
|
|
&dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
2002-04-17 20:19:50 +02:00
|
|
|
if(mb_type&MB_TYPE_INTER4V){
|
2002-04-19 05:25:20 +02:00
|
|
|
s->mv_dir = MV_DIR_FORWARD;
|
2002-03-28 05:25:35 +01:00
|
|
|
s->mv_type = MV_TYPE_8X8;
|
|
|
|
s->mb_intra= 0;
|
|
|
|
for(i=0; i<4; i++){
|
|
|
|
s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
|
|
|
|
s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
|
|
|
|
}
|
2002-06-02 14:20:39 +02:00
|
|
|
encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER4V, pb, pb2, tex_pb,
|
|
|
|
&dmin, &next_block, 0, 0);
|
2002-04-19 05:25:20 +02:00
|
|
|
}
|
|
|
|
if(mb_type&MB_TYPE_FORWARD){
|
|
|
|
s->mv_dir = MV_DIR_FORWARD;
|
|
|
|
s->mv_type = MV_TYPE_16X16;
|
|
|
|
s->mb_intra= 0;
|
|
|
|
s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
|
|
|
|
s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
|
2002-06-02 14:20:39 +02:00
|
|
|
encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_FORWARD, pb, pb2, tex_pb,
|
|
|
|
&dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
|
2002-04-19 05:25:20 +02:00
|
|
|
}
|
|
|
|
if(mb_type&MB_TYPE_BACKWARD){
|
|
|
|
s->mv_dir = MV_DIR_BACKWARD;
|
|
|
|
s->mv_type = MV_TYPE_16X16;
|
|
|
|
s->mb_intra= 0;
|
|
|
|
s->mv[1][0][0] = s->b_back_mv_table[xy][0];
|
|
|
|
s->mv[1][0][1] = s->b_back_mv_table[xy][1];
|
2002-06-02 14:20:39 +02:00
|
|
|
encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BACKWARD, pb, pb2, tex_pb,
|
|
|
|
&dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
|
2002-04-19 05:25:20 +02:00
|
|
|
}
|
|
|
|
if(mb_type&MB_TYPE_BIDIR){
|
|
|
|
s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
|
|
|
|
s->mv_type = MV_TYPE_16X16;
|
|
|
|
s->mb_intra= 0;
|
|
|
|
s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
|
|
|
|
s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
|
|
|
|
s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
|
|
|
|
s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
|
2002-06-02 14:20:39 +02:00
|
|
|
encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BIDIR, pb, pb2, tex_pb,
|
|
|
|
&dmin, &next_block, 0, 0);
|
2002-04-19 05:25:20 +02:00
|
|
|
}
|
|
|
|
if(mb_type&MB_TYPE_DIRECT){
|
|
|
|
s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
|
|
|
|
s->mv_type = MV_TYPE_16X16; //FIXME
|
|
|
|
s->mb_intra= 0;
|
|
|
|
s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
|
|
|
|
s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
|
|
|
|
s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
|
|
|
|
s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
|
2002-06-02 14:20:39 +02:00
|
|
|
encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_DIRECT, pb, pb2, tex_pb,
|
|
|
|
&dmin, &next_block, s->b_direct_mv_table[xy][0], s->b_direct_mv_table[xy][1]);
|
2002-03-28 05:25:35 +01:00
|
|
|
}
|
2002-03-27 22:25:22 +01:00
|
|
|
if(mb_type&MB_TYPE_INTRA){
|
2002-04-19 05:25:20 +02:00
|
|
|
s->mv_dir = MV_DIR_FORWARD;
|
2002-03-28 05:25:35 +01:00
|
|
|
s->mv_type = MV_TYPE_16X16;
|
2002-03-27 22:25:22 +01:00
|
|
|
s->mb_intra= 1;
|
|
|
|
s->mv[0][0][0] = 0;
|
|
|
|
s->mv[0][0][1] = 0;
|
2002-06-02 14:20:39 +02:00
|
|
|
encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb,
|
|
|
|
&dmin, &next_block, 0, 0);
|
2002-04-17 20:19:50 +02:00
|
|
|
/* force cleaning of ac/dc pred stuff if needed ... */
|
|
|
|
if(s->h263_pred || s->h263_aic)
|
|
|
|
s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
|
2002-03-28 05:25:35 +01:00
|
|
|
}
|
2002-04-17 20:19:50 +02:00
|
|
|
copy_context_after_encode(s, &best_s, -1);
|
2002-06-02 14:20:39 +02:00
|
|
|
|
|
|
|
pb_bits_count= get_bit_count(&s->pb);
|
|
|
|
flush_put_bits(&s->pb);
|
|
|
|
ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
|
|
|
|
s->pb= backup_s.pb;
|
|
|
|
|
|
|
|
if(s->data_partitioning){
|
|
|
|
pb2_bits_count= get_bit_count(&s->pb2);
|
|
|
|
flush_put_bits(&s->pb2);
|
|
|
|
ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
|
|
|
|
s->pb2= backup_s.pb2;
|
|
|
|
|
|
|
|
tex_pb_bits_count= get_bit_count(&s->tex_pb);
|
|
|
|
flush_put_bits(&s->tex_pb);
|
|
|
|
ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
|
|
|
|
s->tex_pb= backup_s.tex_pb;
|
|
|
|
}
|
2002-04-21 23:18:17 +02:00
|
|
|
s->last_bits= get_bit_count(&s->pb);
|
2001-07-22 16:18:56 +02:00
|
|
|
} else {
|
2002-04-17 06:32:12 +02:00
|
|
|
int motion_x, motion_y;
|
|
|
|
s->mv_type=MV_TYPE_16X16;
|
2002-03-27 22:25:22 +01:00
|
|
|
// only one MB-Type possible
|
2002-04-19 05:25:20 +02:00
|
|
|
switch(mb_type){
|
|
|
|
case MB_TYPE_INTRA:
|
2002-04-17 06:32:12 +02:00
|
|
|
s->mv_dir = MV_DIR_FORWARD;
|
2002-03-27 22:25:22 +01:00
|
|
|
s->mb_intra= 1;
|
2002-04-17 06:32:12 +02:00
|
|
|
motion_x= s->mv[0][0][0] = 0;
|
|
|
|
motion_y= s->mv[0][0][1] = 0;
|
2002-04-19 05:25:20 +02:00
|
|
|
break;
|
|
|
|
case MB_TYPE_INTER:
|
2002-04-17 06:32:12 +02:00
|
|
|
s->mv_dir = MV_DIR_FORWARD;
|
|
|
|
s->mb_intra= 0;
|
|
|
|
motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
|
|
|
|
motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
|
2002-04-19 05:25:20 +02:00
|
|
|
break;
|
2002-06-02 14:20:39 +02:00
|
|
|
case MB_TYPE_INTER4V:
|
|
|
|
s->mv_dir = MV_DIR_FORWARD;
|
|
|
|
s->mv_type = MV_TYPE_8X8;
|
|
|
|
s->mb_intra= 0;
|
|
|
|
for(i=0; i<4; i++){
|
|
|
|
s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
|
|
|
|
s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
|
|
|
|
}
|
|
|
|
motion_x= motion_y= 0;
|
|
|
|
break;
|
2002-04-19 05:25:20 +02:00
|
|
|
case MB_TYPE_DIRECT:
|
2002-04-17 06:32:12 +02:00
|
|
|
s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
|
|
|
|
s->mb_intra= 0;
|
2002-04-19 05:25:20 +02:00
|
|
|
motion_x=s->b_direct_mv_table[xy][0];
|
|
|
|
motion_y=s->b_direct_mv_table[xy][1];
|
|
|
|
s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0];
|
|
|
|
s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
|
|
|
|
s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
|
|
|
|
s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
|
|
|
|
break;
|
|
|
|
case MB_TYPE_BIDIR:
|
2002-04-17 06:32:12 +02:00
|
|
|
s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
|
2002-03-27 22:25:22 +01:00
|
|
|
s->mb_intra= 0;
|
2002-04-17 06:32:12 +02:00
|
|
|
motion_x=0;
|
|
|
|
motion_y=0;
|
|
|
|
s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
|
|
|
|
s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
|
|
|
|
s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
|
|
|
|
s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
|
2002-04-19 05:25:20 +02:00
|
|
|
break;
|
|
|
|
case MB_TYPE_BACKWARD:
|
2002-04-17 06:32:12 +02:00
|
|
|
s->mv_dir = MV_DIR_BACKWARD;
|
|
|
|
s->mb_intra= 0;
|
|
|
|
motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
|
|
|
|
motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
|
2002-04-19 05:25:20 +02:00
|
|
|
break;
|
|
|
|
case MB_TYPE_FORWARD:
|
2002-04-17 06:32:12 +02:00
|
|
|
s->mv_dir = MV_DIR_FORWARD;
|
|
|
|
s->mb_intra= 0;
|
|
|
|
motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
|
|
|
|
motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
|
|
|
|
// printf(" %d %d ", motion_x, motion_y);
|
2002-04-19 05:25:20 +02:00
|
|
|
break;
|
|
|
|
default:
|
2002-04-17 06:32:12 +02:00
|
|
|
motion_x=motion_y=0; //gcc warning fix
|
|
|
|
printf("illegal MB type\n");
|
2002-03-27 22:25:22 +01:00
|
|
|
}
|
2002-04-17 06:32:12 +02:00
|
|
|
encode_mb(s, motion_x, motion_y);
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
2002-04-19 05:25:20 +02:00
|
|
|
/* clean the MV table in IPS frames for direct mode in B frames */
|
|
|
|
if(s->mb_intra /* && I,P,S_TYPE */){
|
|
|
|
s->p_mv_table[xy][0]=0;
|
|
|
|
s->p_mv_table[xy][1]=0;
|
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2001-07-31 01:26:26 +02:00
|
|
|
MPV_decode_mb(s, s->block);
|
2002-06-02 14:20:39 +02:00
|
|
|
//printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_width, get_bit_count(&s->pb));
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
2002-02-12 16:43:16 +01:00
|
|
|
|
|
|
|
|
2002-02-09 17:59:56 +01:00
|
|
|
/* Obtain average GOB size for RTP */
|
|
|
|
if (s->rtp_mode) {
|
|
|
|
if (!mb_y)
|
2002-02-12 16:43:16 +01:00
|
|
|
s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line;
|
2002-02-09 17:59:56 +01:00
|
|
|
else if (!(mb_y % s->gob_index)) {
|
2002-02-12 16:43:16 +01:00
|
|
|
s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1;
|
|
|
|
s->ptr_last_mb_line = pbBufPtr(&s->pb);
|
2002-02-09 17:59:56 +01:00
|
|
|
}
|
|
|
|
//fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y,
|
|
|
|
// (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize);
|
2002-06-02 14:20:39 +02:00
|
|
|
if(s->codec_id!=CODEC_ID_MPEG4) s->first_slice_line = 0; //FIXME clean
|
2002-02-09 17:59:56 +01:00
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
2002-03-27 22:25:22 +01:00
|
|
|
emms_c();
|
2002-03-23 18:43:30 +01:00
|
|
|
|
2002-06-02 14:20:39 +02:00
|
|
|
if(s->codec_id==CODEC_ID_MPEG4 && s->data_partitioning && s->pict_type!=B_TYPE)
|
|
|
|
ff_mpeg4_merge_partitions(s);
|
|
|
|
|
|
|
|
if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
|
2002-01-15 23:22:41 +01:00
|
|
|
msmpeg4_encode_ext_header(s);
|
|
|
|
|
2002-06-02 14:20:39 +02:00
|
|
|
if(s->codec_id==CODEC_ID_MPEG4)
|
|
|
|
ff_mpeg4_stuffing(&s->pb);
|
|
|
|
|
2001-11-19 03:13:14 +01:00
|
|
|
//if (s->gob_number)
|
|
|
|
// fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
|
2002-02-09 17:59:56 +01:00
|
|
|
|
|
|
|
/* Send the last GOB if RTP */
|
|
|
|
if (s->rtp_mode) {
|
|
|
|
flush_put_bits(&s->pb);
|
2002-02-12 16:43:16 +01:00
|
|
|
pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
|
2002-02-09 17:59:56 +01:00
|
|
|
/* Call the RTP callback to send the last GOB */
|
|
|
|
if (s->rtp_callback)
|
|
|
|
s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
|
2002-02-12 16:43:16 +01:00
|
|
|
s->ptr_lastgob = pbBufPtr(&s->pb);
|
2002-02-09 17:59:56 +01:00
|
|
|
//fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
|
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
|
2002-01-27 14:30:18 +01:00
|
|
|
static int dct_quantize_c(MpegEncContext *s,
|
2001-07-22 16:18:56 +02:00
|
|
|
DCTELEM *block, int n,
|
2002-04-27 14:30:26 +02:00
|
|
|
int qscale, int *overflow)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
|
|
|
int i, j, level, last_non_zero, q;
|
|
|
|
const int *qmat;
|
2002-04-27 14:30:26 +02:00
|
|
|
int bias;
|
|
|
|
int max=0;
|
|
|
|
unsigned int threshold1, threshold2;
|
2002-06-02 14:20:39 +02:00
|
|
|
|
2002-08-30 01:55:32 +02:00
|
|
|
s->fdct (block);
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2002-09-28 04:18:34 +02:00
|
|
|
#ifndef ARCH_ALPHA /* Alpha uses unpermuted matrix */
|
2001-08-13 23:48:05 +02:00
|
|
|
/* we need this permutation so that we correct the IDCT
|
|
|
|
permutation. will be moved into DCT code */
|
2002-09-30 00:44:22 +02:00
|
|
|
block_permute(block, s->idct_permutation); //FIXME remove
|
2002-09-28 04:18:34 +02:00
|
|
|
#endif
|
2001-08-13 23:48:05 +02:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
if (s->mb_intra) {
|
2002-05-02 06:39:45 +02:00
|
|
|
if (!s->h263_aic) {
|
|
|
|
if (n < 4)
|
|
|
|
q = s->y_dc_scale;
|
|
|
|
else
|
|
|
|
q = s->c_dc_scale;
|
|
|
|
q = q << 3;
|
|
|
|
} else
|
|
|
|
/* For AIC we skip quant/dequant of INTRADC */
|
|
|
|
q = 1 << 3;
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
/* note: block[0] is assumed to be positive */
|
|
|
|
block[0] = (block[0] + (q >> 1)) / q;
|
|
|
|
i = 1;
|
|
|
|
last_non_zero = 0;
|
2002-04-27 14:30:26 +02:00
|
|
|
qmat = s->q_intra_matrix[qscale];
|
2002-09-01 18:52:33 +02:00
|
|
|
bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
|
2001-07-22 16:18:56 +02:00
|
|
|
} else {
|
|
|
|
i = 0;
|
|
|
|
last_non_zero = -1;
|
2002-04-27 14:30:26 +02:00
|
|
|
qmat = s->q_inter_matrix[qscale];
|
2002-09-01 18:52:33 +02:00
|
|
|
bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
2002-09-01 18:52:33 +02:00
|
|
|
threshold1= (1<<QMAT_SHIFT) - bias - 1;
|
|
|
|
threshold2= (threshold1<<1);
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
for(;i<64;i++) {
|
2002-09-30 00:44:22 +02:00
|
|
|
j = s->intra_scantable.permutated[i];
|
2001-07-22 16:18:56 +02:00
|
|
|
level = block[j];
|
|
|
|
level = level * qmat[j];
|
|
|
|
|
2002-04-27 14:30:26 +02:00
|
|
|
// if( bias+level >= (1<<(QMAT_SHIFT - 3))
|
|
|
|
// || bias-level >= (1<<(QMAT_SHIFT - 3))){
|
|
|
|
if(((unsigned)(level+threshold1))>threshold2){
|
|
|
|
if(level>0){
|
2002-09-01 18:52:33 +02:00
|
|
|
level= (bias + level)>>QMAT_SHIFT;
|
2002-04-27 14:30:26 +02:00
|
|
|
block[j]= level;
|
|
|
|
}else{
|
2002-09-01 18:52:33 +02:00
|
|
|
level= (bias - level)>>QMAT_SHIFT;
|
2002-04-27 14:30:26 +02:00
|
|
|
block[j]= -level;
|
|
|
|
}
|
|
|
|
max |=level;
|
2001-07-22 16:18:56 +02:00
|
|
|
last_non_zero = i;
|
2002-04-27 14:30:26 +02:00
|
|
|
}else{
|
|
|
|
block[j]=0;
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
}
|
2002-04-27 14:30:26 +02:00
|
|
|
*overflow= s->max_qcoeff < max; //overflow might have happend
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
return last_non_zero;
|
|
|
|
}
|
|
|
|
|
2001-07-31 01:26:26 +02:00
|
|
|
static void dct_unquantize_mpeg1_c(MpegEncContext *s,
|
|
|
|
DCTELEM *block, int n, int qscale)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
2002-01-13 05:59:37 +01:00
|
|
|
int i, level, nCoeffs;
|
2001-07-22 16:18:56 +02:00
|
|
|
const UINT16 *quant_matrix;
|
|
|
|
|
2002-09-30 00:44:22 +02:00
|
|
|
nCoeffs= s->block_last_index[n];
|
2002-01-13 05:59:37 +01:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
if (s->mb_intra) {
|
|
|
|
if (n < 4)
|
|
|
|
block[0] = block[0] * s->y_dc_scale;
|
|
|
|
else
|
|
|
|
block[0] = block[0] * s->c_dc_scale;
|
|
|
|
/* XXX: only mpeg1 */
|
|
|
|
quant_matrix = s->intra_matrix;
|
2002-09-30 00:44:22 +02:00
|
|
|
for(i=1;i<=nCoeffs;i++) {
|
|
|
|
int j= s->intra_scantable.permutated[i];
|
2002-01-13 05:59:37 +01:00
|
|
|
level = block[j];
|
2001-07-22 16:18:56 +02:00
|
|
|
if (level) {
|
|
|
|
if (level < 0) {
|
|
|
|
level = -level;
|
2002-01-13 05:59:37 +01:00
|
|
|
level = (int)(level * qscale * quant_matrix[j]) >> 3;
|
2001-07-22 16:18:56 +02:00
|
|
|
level = (level - 1) | 1;
|
|
|
|
level = -level;
|
|
|
|
} else {
|
2002-01-13 05:59:37 +01:00
|
|
|
level = (int)(level * qscale * quant_matrix[j]) >> 3;
|
2001-07-22 16:18:56 +02:00
|
|
|
level = (level - 1) | 1;
|
|
|
|
}
|
|
|
|
#ifdef PARANOID
|
|
|
|
if (level < -2048 || level > 2047)
|
|
|
|
fprintf(stderr, "unquant error %d %d\n", i, level);
|
|
|
|
#endif
|
2002-01-13 05:59:37 +01:00
|
|
|
block[j] = level;
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
i = 0;
|
2002-04-27 14:30:26 +02:00
|
|
|
quant_matrix = s->inter_matrix;
|
2002-09-30 00:44:22 +02:00
|
|
|
for(;i<=nCoeffs;i++) {
|
|
|
|
int j= s->intra_scantable.permutated[i];
|
2002-01-13 05:59:37 +01:00
|
|
|
level = block[j];
|
2001-07-22 16:18:56 +02:00
|
|
|
if (level) {
|
|
|
|
if (level < 0) {
|
|
|
|
level = -level;
|
|
|
|
level = (((level << 1) + 1) * qscale *
|
2002-01-13 05:59:37 +01:00
|
|
|
((int) (quant_matrix[j]))) >> 4;
|
2001-07-22 16:18:56 +02:00
|
|
|
level = (level - 1) | 1;
|
|
|
|
level = -level;
|
|
|
|
} else {
|
|
|
|
level = (((level << 1) + 1) * qscale *
|
2002-01-13 05:59:37 +01:00
|
|
|
((int) (quant_matrix[j]))) >> 4;
|
2001-07-22 16:18:56 +02:00
|
|
|
level = (level - 1) | 1;
|
|
|
|
}
|
|
|
|
#ifdef PARANOID
|
|
|
|
if (level < -2048 || level > 2047)
|
|
|
|
fprintf(stderr, "unquant error %d %d\n", i, level);
|
|
|
|
#endif
|
2002-01-13 05:59:37 +01:00
|
|
|
block[j] = level;
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-07-31 01:26:26 +02:00
|
|
|
|
2002-04-17 18:30:31 +02:00
|
|
|
static void dct_unquantize_mpeg2_c(MpegEncContext *s,
|
|
|
|
DCTELEM *block, int n, int qscale)
|
|
|
|
{
|
|
|
|
int i, level, nCoeffs;
|
|
|
|
const UINT16 *quant_matrix;
|
|
|
|
|
2002-09-30 00:44:22 +02:00
|
|
|
if(s->alternate_scan) nCoeffs= 63;
|
|
|
|
else nCoeffs= s->block_last_index[n];
|
2002-04-17 18:30:31 +02:00
|
|
|
|
|
|
|
if (s->mb_intra) {
|
|
|
|
if (n < 4)
|
|
|
|
block[0] = block[0] * s->y_dc_scale;
|
|
|
|
else
|
|
|
|
block[0] = block[0] * s->c_dc_scale;
|
|
|
|
quant_matrix = s->intra_matrix;
|
2002-09-30 00:44:22 +02:00
|
|
|
for(i=1;i<=nCoeffs;i++) {
|
|
|
|
int j= s->intra_scantable.permutated[i];
|
2002-04-17 18:30:31 +02:00
|
|
|
level = block[j];
|
|
|
|
if (level) {
|
|
|
|
if (level < 0) {
|
|
|
|
level = -level;
|
|
|
|
level = (int)(level * qscale * quant_matrix[j]) >> 3;
|
|
|
|
level = -level;
|
|
|
|
} else {
|
|
|
|
level = (int)(level * qscale * quant_matrix[j]) >> 3;
|
|
|
|
}
|
|
|
|
#ifdef PARANOID
|
|
|
|
if (level < -2048 || level > 2047)
|
|
|
|
fprintf(stderr, "unquant error %d %d\n", i, level);
|
|
|
|
#endif
|
|
|
|
block[j] = level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int sum=-1;
|
|
|
|
i = 0;
|
2002-04-27 14:30:26 +02:00
|
|
|
quant_matrix = s->inter_matrix;
|
2002-09-30 00:44:22 +02:00
|
|
|
for(;i<=nCoeffs;i++) {
|
|
|
|
int j= s->intra_scantable.permutated[i];
|
2002-04-17 18:30:31 +02:00
|
|
|
level = block[j];
|
|
|
|
if (level) {
|
|
|
|
if (level < 0) {
|
|
|
|
level = -level;
|
|
|
|
level = (((level << 1) + 1) * qscale *
|
|
|
|
((int) (quant_matrix[j]))) >> 4;
|
|
|
|
level = -level;
|
|
|
|
} else {
|
|
|
|
level = (((level << 1) + 1) * qscale *
|
|
|
|
((int) (quant_matrix[j]))) >> 4;
|
|
|
|
}
|
|
|
|
#ifdef PARANOID
|
|
|
|
if (level < -2048 || level > 2047)
|
|
|
|
fprintf(stderr, "unquant error %d %d\n", i, level);
|
|
|
|
#endif
|
|
|
|
block[j] = level;
|
|
|
|
sum+=level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
block[63]^=sum&1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-31 01:26:26 +02:00
|
|
|
static void dct_unquantize_h263_c(MpegEncContext *s,
|
|
|
|
DCTELEM *block, int n, int qscale)
|
|
|
|
{
|
|
|
|
int i, level, qmul, qadd;
|
2002-01-13 05:59:37 +01:00
|
|
|
int nCoeffs;
|
2002-02-19 20:23:34 +01:00
|
|
|
|
2002-09-30 00:44:22 +02:00
|
|
|
assert(s->block_last_index[n]>=0);
|
|
|
|
|
|
|
|
qadd = (qscale - 1) | 1;
|
|
|
|
qmul = qscale << 1;
|
|
|
|
|
2001-07-31 01:26:26 +02:00
|
|
|
if (s->mb_intra) {
|
2002-02-19 20:23:34 +01:00
|
|
|
if (!s->h263_aic) {
|
|
|
|
if (n < 4)
|
|
|
|
block[0] = block[0] * s->y_dc_scale;
|
|
|
|
else
|
|
|
|
block[0] = block[0] * s->c_dc_scale;
|
2002-09-30 00:44:22 +02:00
|
|
|
}else
|
|
|
|
qadd = 0;
|
2001-07-31 01:26:26 +02:00
|
|
|
i = 1;
|
2002-09-30 00:44:22 +02:00
|
|
|
nCoeffs= 63; //does not allways use zigzag table
|
2001-07-31 01:26:26 +02:00
|
|
|
} else {
|
|
|
|
i = 0;
|
2002-09-30 00:44:22 +02:00
|
|
|
nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
|
2001-07-31 01:26:26 +02:00
|
|
|
}
|
|
|
|
|
2002-09-30 00:44:22 +02:00
|
|
|
for(;i<=nCoeffs;i++) {
|
2001-07-31 01:26:26 +02:00
|
|
|
level = block[i];
|
|
|
|
if (level) {
|
|
|
|
if (level < 0) {
|
|
|
|
level = level * qmul - qadd;
|
|
|
|
} else {
|
|
|
|
level = level * qmul + qadd;
|
|
|
|
}
|
|
|
|
#ifdef PARANOID
|
|
|
|
if (level < -2048 || level > 2047)
|
|
|
|
fprintf(stderr, "unquant error %d %d\n", i, level);
|
|
|
|
#endif
|
|
|
|
block[i] = level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2002-06-02 14:20:39 +02:00
|
|
|
static void remove_ac(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int mb_x, int mb_y)
|
|
|
|
{
|
|
|
|
int dc, dcb, dcr, y, i;
|
|
|
|
for(i=0; i<4; i++){
|
|
|
|
dc= s->dc_val[0][mb_x*2+1 + (i&1) + (mb_y*2+1 + (i>>1))*(s->mb_width*2+2)];
|
|
|
|
for(y=0; y<8; y++){
|
|
|
|
int x;
|
|
|
|
for(x=0; x<8; x++){
|
|
|
|
dest_y[x + (i&1)*8 + (y + (i>>1)*8)*s->linesize]= dc/8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dcb = s->dc_val[1][mb_x+1 + (mb_y+1)*(s->mb_width+2)];
|
|
|
|
dcr= s->dc_val[2][mb_x+1 + (mb_y+1)*(s->mb_width+2)];
|
|
|
|
for(y=0; y<8; y++){
|
|
|
|
int x;
|
|
|
|
for(x=0; x<8; x++){
|
2002-07-15 16:15:10 +02:00
|
|
|
dest_cb[x + y*(s->uvlinesize)]= dcb/8;
|
|
|
|
dest_cr[x + y*(s->uvlinesize)]= dcr/8;
|
2002-06-02 14:20:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* will conceal past errors, and allso drop b frames if needed
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void ff_conceal_past_errors(MpegEncContext *s, int unknown_pos)
|
|
|
|
{
|
|
|
|
int mb_x= s->mb_x;
|
|
|
|
int mb_y= s->mb_y;
|
|
|
|
int mb_dist=0;
|
|
|
|
int i, intra_count=0, inter_count=0;
|
|
|
|
int intra_conceal= s->msmpeg4_version ? 50 : 50; //FIXME finetune
|
|
|
|
int inter_conceal= s->msmpeg4_version ? 50 : 50;
|
2002-08-25 23:19:50 +02:00
|
|
|
|
2002-06-02 14:20:39 +02:00
|
|
|
// for last block
|
|
|
|
if(mb_x>=s->mb_width) mb_x= s->mb_width -1;
|
|
|
|
if(mb_y>=s->mb_height) mb_y= s->mb_height-1;
|
|
|
|
|
|
|
|
if(s->decoding_error==0 && unknown_pos){
|
|
|
|
if(s->data_partitioning && s->pict_type!=B_TYPE)
|
|
|
|
s->decoding_error= DECODING_AC_LOST;
|
|
|
|
else
|
|
|
|
s->decoding_error= DECODING_DESYNC;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(s->decoding_error==DECODING_DESYNC && s->pict_type!=B_TYPE) s->next_p_frame_damaged=1;
|
|
|
|
|
|
|
|
for(i=mb_x + mb_y*s->mb_width; i>=0; i--){
|
|
|
|
if(s->mbintra_table[i]) intra_count++;
|
|
|
|
else inter_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(s->decoding_error==DECODING_AC_LOST){
|
|
|
|
intra_conceal*=2;
|
|
|
|
inter_conceal*=2;
|
|
|
|
}else if(s->decoding_error==DECODING_ACDC_LOST){
|
|
|
|
intra_conceal*=2;
|
|
|
|
inter_conceal*=2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(unknown_pos && (intra_count<inter_count)){
|
|
|
|
intra_conceal= inter_conceal= s->mb_num;
|
|
|
|
// printf("%d %d\n",intra_count, inter_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "concealing errors\n");
|
|
|
|
|
|
|
|
/* for all MBs from the current one back until the last resync marker */
|
|
|
|
for(; mb_y>=0 && mb_y>=s->resync_mb_y; mb_y--){
|
|
|
|
for(; mb_x>=0; mb_x--){
|
|
|
|
uint8_t *dest_y = s->current_picture[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
|
2002-07-15 16:15:10 +02:00
|
|
|
uint8_t *dest_cb = s->current_picture[1] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
|
|
|
|
uint8_t *dest_cr = s->current_picture[2] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
|
2002-06-02 14:20:39 +02:00
|
|
|
int mb_x_backup= s->mb_x; //FIXME pass xy to mpeg_motion
|
|
|
|
int mb_y_backup= s->mb_y;
|
|
|
|
s->mb_x=mb_x;
|
|
|
|
s->mb_y=mb_y;
|
|
|
|
if(s->mbintra_table[mb_y*s->mb_width + mb_x] && mb_dist<intra_conceal){
|
|
|
|
if(s->decoding_error==DECODING_AC_LOST){
|
|
|
|
remove_ac(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
|
|
|
|
// printf("remove ac to %d %d\n", mb_x, mb_y);
|
|
|
|
}else{
|
|
|
|
mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
|
|
|
|
s->last_picture, 0, 0, put_pixels_tab,
|
|
|
|
0/*mx*/, 0/*my*/, 16);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(!s->mbintra_table[mb_y*s->mb_width + mb_x] && mb_dist<inter_conceal){
|
|
|
|
int mx=0;
|
|
|
|
int my=0;
|
|
|
|
|
|
|
|
if(s->decoding_error!=DECODING_DESYNC){
|
|
|
|
int xy= mb_x*2+1 + (mb_y*2+1)*(s->mb_width*2+2);
|
|
|
|
mx= s->motion_val[ xy ][0];
|
|
|
|
my= s->motion_val[ xy ][1];
|
|
|
|
}
|
|
|
|
|
|
|
|
mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
|
|
|
|
s->last_picture, 0, 0, put_pixels_tab,
|
|
|
|
mx, my, 16);
|
|
|
|
}
|
|
|
|
s->mb_x= mb_x_backup;
|
|
|
|
s->mb_y= mb_y_backup;
|
|
|
|
|
|
|
|
if(mb_x== s->resync_mb_x && mb_y== s->resync_mb_y) return;
|
|
|
|
if(!s->mbskip_table[mb_x + mb_y*s->mb_width]) mb_dist++;
|
|
|
|
}
|
|
|
|
mb_x=s->mb_width-1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
AVCodec mpeg1video_encoder = {
|
|
|
|
"mpeg1video",
|
|
|
|
CODEC_TYPE_VIDEO,
|
|
|
|
CODEC_ID_MPEG1VIDEO,
|
|
|
|
sizeof(MpegEncContext),
|
|
|
|
MPV_encode_init,
|
|
|
|
MPV_encode_picture,
|
|
|
|
MPV_encode_end,
|
|
|
|
};
|
|
|
|
|
|
|
|
AVCodec h263_encoder = {
|
|
|
|
"h263",
|
|
|
|
CODEC_TYPE_VIDEO,
|
|
|
|
CODEC_ID_H263,
|
|
|
|
sizeof(MpegEncContext),
|
|
|
|
MPV_encode_init,
|
|
|
|
MPV_encode_picture,
|
|
|
|
MPV_encode_end,
|
|
|
|
};
|
|
|
|
|
|
|
|
AVCodec h263p_encoder = {
|
|
|
|
"h263p",
|
|
|
|
CODEC_TYPE_VIDEO,
|
|
|
|
CODEC_ID_H263P,
|
|
|
|
sizeof(MpegEncContext),
|
|
|
|
MPV_encode_init,
|
|
|
|
MPV_encode_picture,
|
|
|
|
MPV_encode_end,
|
|
|
|
};
|
|
|
|
|
|
|
|
AVCodec rv10_encoder = {
|
|
|
|
"rv10",
|
|
|
|
CODEC_TYPE_VIDEO,
|
|
|
|
CODEC_ID_RV10,
|
|
|
|
sizeof(MpegEncContext),
|
|
|
|
MPV_encode_init,
|
|
|
|
MPV_encode_picture,
|
|
|
|
MPV_encode_end,
|
|
|
|
};
|
|
|
|
|
|
|
|
AVCodec mjpeg_encoder = {
|
|
|
|
"mjpeg",
|
|
|
|
CODEC_TYPE_VIDEO,
|
|
|
|
CODEC_ID_MJPEG,
|
|
|
|
sizeof(MpegEncContext),
|
|
|
|
MPV_encode_init,
|
|
|
|
MPV_encode_picture,
|
|
|
|
MPV_encode_end,
|
|
|
|
};
|
|
|
|
|
2001-08-15 15:12:23 +02:00
|
|
|
AVCodec mpeg4_encoder = {
|
|
|
|
"mpeg4",
|
2001-07-22 16:18:56 +02:00
|
|
|
CODEC_TYPE_VIDEO,
|
2001-08-15 15:12:23 +02:00
|
|
|
CODEC_ID_MPEG4,
|
2001-07-22 16:18:56 +02:00
|
|
|
sizeof(MpegEncContext),
|
|
|
|
MPV_encode_init,
|
|
|
|
MPV_encode_picture,
|
|
|
|
MPV_encode_end,
|
|
|
|
};
|
|
|
|
|
2002-04-05 06:09:04 +02:00
|
|
|
AVCodec msmpeg4v1_encoder = {
|
|
|
|
"msmpeg4v1",
|
|
|
|
CODEC_TYPE_VIDEO,
|
|
|
|
CODEC_ID_MSMPEG4V1,
|
|
|
|
sizeof(MpegEncContext),
|
|
|
|
MPV_encode_init,
|
|
|
|
MPV_encode_picture,
|
|
|
|
MPV_encode_end,
|
|
|
|
};
|
|
|
|
|
|
|
|
AVCodec msmpeg4v2_encoder = {
|
|
|
|
"msmpeg4v2",
|
|
|
|
CODEC_TYPE_VIDEO,
|
|
|
|
CODEC_ID_MSMPEG4V2,
|
|
|
|
sizeof(MpegEncContext),
|
|
|
|
MPV_encode_init,
|
|
|
|
MPV_encode_picture,
|
|
|
|
MPV_encode_end,
|
|
|
|
};
|
|
|
|
|
|
|
|
AVCodec msmpeg4v3_encoder = {
|
2001-07-22 16:18:56 +02:00
|
|
|
"msmpeg4",
|
|
|
|
CODEC_TYPE_VIDEO,
|
2002-04-05 06:09:04 +02:00
|
|
|
CODEC_ID_MSMPEG4V3,
|
2001-07-22 16:18:56 +02:00
|
|
|
sizeof(MpegEncContext),
|
|
|
|
MPV_encode_init,
|
|
|
|
MPV_encode_picture,
|
|
|
|
MPV_encode_end,
|
|
|
|
};
|
2002-06-18 02:49:00 +02:00
|
|
|
|
|
|
|
AVCodec wmv1_encoder = {
|
|
|
|
"wmv1",
|
|
|
|
CODEC_TYPE_VIDEO,
|
|
|
|
CODEC_ID_WMV1,
|
|
|
|
sizeof(MpegEncContext),
|
|
|
|
MPV_encode_init,
|
|
|
|
MPV_encode_picture,
|
|
|
|
MPV_encode_end,
|
|
|
|
};
|
|
|
|
|
|
|
|
AVCodec wmv2_encoder = {
|
|
|
|
"wmv2",
|
|
|
|
CODEC_TYPE_VIDEO,
|
|
|
|
CODEC_ID_WMV2,
|
|
|
|
sizeof(MpegEncContext),
|
|
|
|
MPV_encode_init,
|
|
|
|
MPV_encode_picture,
|
|
|
|
MPV_encode_end,
|
|
|
|
};
|