2001-07-22 17:18:56 +03:00
|
|
|
/*
|
2016-04-27 19:45:23 +02:00
|
|
|
* H.263/MPEG-4 backend for encoder and decoder
|
2009-01-19 17:46:40 +02:00
|
|
|
* Copyright (c) 2000,2001 Fabrice Bellard
|
2016-04-27 19:45:23 +02:00
|
|
|
* H.263+ support.
|
2009-01-19 17:46:40 +02:00
|
|
|
* Copyright (c) 2001 Juan J. Sierralta P
|
2004-01-10 18:04:55 +02:00
|
|
|
* Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
|
2001-07-22 17:18:56 +03:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2002-05-26 01:45:33 +03:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 18:30:46 +03:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2001-07-22 17:18:56 +03:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2001-07-22 17:18:56 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2002-05-26 01:45:33 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2001-07-22 17:18:56 +03:00
|
|
|
*
|
2002-05-26 01:45:33 +03:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2006-10-07 18:30:46 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-01-13 00:43:26 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2001-07-22 17:18:56 +03:00
|
|
|
*/
|
2003-01-27 01:09:12 +02:00
|
|
|
|
|
|
|
/**
|
2010-04-20 17:45:34 +03:00
|
|
|
* @file
|
2016-04-27 19:45:23 +02:00
|
|
|
* H.263/MPEG-4 codec.
|
2003-01-27 01:09:12 +02:00
|
|
|
*/
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2021-05-07 17:52:23 +02:00
|
|
|
#include "libavutil/thread.h"
|
2001-07-22 17:18:56 +03:00
|
|
|
#include "mpegvideo.h"
|
2010-01-08 01:53:49 +02:00
|
|
|
#include "h263.h"
|
2001-07-22 17:18:56 +03:00
|
|
|
#include "h263data.h"
|
2021-06-14 18:03:55 +02:00
|
|
|
#include "h263dsp.h"
|
|
|
|
#include "idctdsp.h"
|
2009-01-19 00:57:40 +02:00
|
|
|
#include "mathops.h"
|
2021-06-14 18:03:55 +02:00
|
|
|
#include "mpegpicture.h"
|
2014-02-28 15:40:54 +03:00
|
|
|
#include "mpegutils.h"
|
2021-06-14 18:03:55 +02:00
|
|
|
#include "rl.h"
|
2001-07-22 17:18:56 +03:00
|
|
|
|
2021-05-07 17:52:23 +02:00
|
|
|
static av_cold void h263_init_rl_inter(void)
|
|
|
|
{
|
|
|
|
static uint8_t h263_rl_inter_table[2][2 * MAX_RUN + MAX_LEVEL + 3];
|
|
|
|
ff_rl_init(&ff_h263_rl_inter, h263_rl_inter_table);
|
|
|
|
}
|
|
|
|
|
|
|
|
av_cold void ff_h263_init_rl_inter(void)
|
|
|
|
{
|
|
|
|
static AVOnce init_static_once = AV_ONCE_INIT;
|
|
|
|
ff_thread_once(&init_static_once, h263_init_rl_inter);
|
|
|
|
}
|
2002-09-23 17:56:11 +03:00
|
|
|
|
2003-07-29 05:09:12 +03:00
|
|
|
void ff_h263_update_motion_val(MpegEncContext * s){
|
|
|
|
const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
|
2005-05-05 17:34:47 +03:00
|
|
|
//FIXME a lot of that is only needed for !low_delay
|
2004-04-16 04:01:45 +03:00
|
|
|
const int wrap = s->b8_stride;
|
2003-07-29 05:09:12 +03:00
|
|
|
const int xy = s->block_index[0];
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2012-11-21 23:34:46 +03:00
|
|
|
s->current_picture.mbskip_table[mb_xy] = s->mb_skipped;
|
2003-07-29 05:09:12 +03:00
|
|
|
|
|
|
|
if(s->mv_type != MV_TYPE_8X8){
|
|
|
|
int motion_x, motion_y;
|
|
|
|
if (s->mb_intra) {
|
|
|
|
motion_x = 0;
|
|
|
|
motion_y = 0;
|
|
|
|
} else if (s->mv_type == MV_TYPE_16X16) {
|
|
|
|
motion_x = s->mv[0][0][0];
|
|
|
|
motion_y = s->mv[0][0][1];
|
|
|
|
} else /*if (s->mv_type == MV_TYPE_FIELD)*/ {
|
|
|
|
int i;
|
|
|
|
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);
|
|
|
|
for(i=0; i<2; i++){
|
2003-12-30 18:07:57 +02:00
|
|
|
s->p_field_mv_table[i][0][mb_xy][0]= s->mv[0][i][0];
|
|
|
|
s->p_field_mv_table[i][0][mb_xy][1]= s->mv[0][i][1];
|
2003-07-29 05:09:12 +03:00
|
|
|
}
|
2012-11-21 23:34:46 +03:00
|
|
|
s->current_picture.ref_index[0][4*mb_xy ] =
|
|
|
|
s->current_picture.ref_index[0][4*mb_xy + 1] = s->field_select[0][0];
|
|
|
|
s->current_picture.ref_index[0][4*mb_xy + 2] =
|
|
|
|
s->current_picture.ref_index[0][4*mb_xy + 3] = s->field_select[0][1];
|
2003-07-29 05:09:12 +03:00
|
|
|
}
|
2004-04-19 06:33:23 +03:00
|
|
|
|
2003-07-29 05:09:12 +03:00
|
|
|
/* no update if 8X8 because it has been done during parsing */
|
2012-11-21 23:34:46 +03:00
|
|
|
s->current_picture.motion_val[0][xy][0] = motion_x;
|
|
|
|
s->current_picture.motion_val[0][xy][1] = motion_y;
|
|
|
|
s->current_picture.motion_val[0][xy + 1][0] = motion_x;
|
|
|
|
s->current_picture.motion_val[0][xy + 1][1] = motion_y;
|
|
|
|
s->current_picture.motion_val[0][xy + wrap][0] = motion_x;
|
|
|
|
s->current_picture.motion_val[0][xy + wrap][1] = motion_y;
|
|
|
|
s->current_picture.motion_val[0][xy + 1 + wrap][0] = motion_x;
|
|
|
|
s->current_picture.motion_val[0][xy + 1 + wrap][1] = motion_y;
|
2003-07-29 05:09:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if(s->encoding){ //FIXME encoding MUST be cleaned up
|
2005-12-17 20:14:38 +02:00
|
|
|
if (s->mv_type == MV_TYPE_8X8)
|
2012-11-21 23:34:46 +03:00
|
|
|
s->current_picture.mb_type[mb_xy] = MB_TYPE_L0 | MB_TYPE_8x8;
|
2003-11-28 23:31:20 +02:00
|
|
|
else if(s->mb_intra)
|
2012-11-21 23:34:46 +03:00
|
|
|
s->current_picture.mb_type[mb_xy] = MB_TYPE_INTRA;
|
2003-07-29 05:09:12 +03:00
|
|
|
else
|
2012-11-21 23:34:46 +03:00
|
|
|
s->current_picture.mb_type[mb_xy] = MB_TYPE_L0 | MB_TYPE_16x16;
|
2003-07-29 05:09:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-04 20:46:49 +02:00
|
|
|
void ff_h263_loop_filter(MpegEncContext * s){
|
2003-12-01 17:23:14 +02:00
|
|
|
int qp_c;
|
|
|
|
const int linesize = s->linesize;
|
|
|
|
const int uvlinesize= s->uvlinesize;
|
|
|
|
const int xy = s->mb_y * s->mb_stride + s->mb_x;
|
|
|
|
uint8_t *dest_y = s->dest[0];
|
|
|
|
uint8_t *dest_cb= s->dest[1];
|
|
|
|
uint8_t *dest_cr= s->dest[2];
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2003-12-01 17:23:14 +02:00
|
|
|
/*
|
|
|
|
Diag Top
|
|
|
|
Left Center
|
|
|
|
*/
|
2012-11-21 23:34:46 +03:00
|
|
|
if (!IS_SKIP(s->current_picture.mb_type[xy])) {
|
2003-12-01 17:23:14 +02:00
|
|
|
qp_c= s->qscale;
|
2013-11-05 10:11:47 +03:00
|
|
|
s->h263dsp.h263_v_loop_filter(dest_y + 8 * linesize, linesize, qp_c);
|
|
|
|
s->h263dsp.h263_v_loop_filter(dest_y + 8 * linesize + 8, linesize, qp_c);
|
2003-12-01 17:23:14 +02:00
|
|
|
}else
|
|
|
|
qp_c= 0;
|
|
|
|
|
|
|
|
if(s->mb_y){
|
2008-12-11 23:45:41 +02:00
|
|
|
int qp_dt, qp_tt, qp_tc;
|
2003-12-01 17:23:14 +02:00
|
|
|
|
2012-11-21 23:34:46 +03:00
|
|
|
if (IS_SKIP(s->current_picture.mb_type[xy - s->mb_stride]))
|
2008-12-11 23:45:41 +02:00
|
|
|
qp_tt=0;
|
2005-12-17 20:14:38 +02:00
|
|
|
else
|
2012-11-21 23:34:46 +03:00
|
|
|
qp_tt = s->current_picture.qscale_table[xy - s->mb_stride];
|
2003-12-01 17:23:14 +02:00
|
|
|
|
2005-12-17 20:14:38 +02:00
|
|
|
if(qp_c)
|
2003-12-01 17:23:14 +02:00
|
|
|
qp_tc= qp_c;
|
|
|
|
else
|
2008-12-11 23:45:41 +02:00
|
|
|
qp_tc= qp_tt;
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2003-12-01 17:23:14 +02:00
|
|
|
if(qp_tc){
|
|
|
|
const int chroma_qp= s->chroma_qscale_table[qp_tc];
|
2013-11-05 10:11:47 +03:00
|
|
|
s->h263dsp.h263_v_loop_filter(dest_y, linesize, qp_tc);
|
|
|
|
s->h263dsp.h263_v_loop_filter(dest_y + 8, linesize, qp_tc);
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2013-11-05 10:11:47 +03:00
|
|
|
s->h263dsp.h263_v_loop_filter(dest_cb, uvlinesize, chroma_qp);
|
|
|
|
s->h263dsp.h263_v_loop_filter(dest_cr, uvlinesize, chroma_qp);
|
2003-12-01 17:23:14 +02:00
|
|
|
}
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2008-12-11 23:45:41 +02:00
|
|
|
if(qp_tt)
|
2013-11-05 10:11:47 +03:00
|
|
|
s->h263dsp.h263_h_loop_filter(dest_y - 8 * linesize + 8, linesize, qp_tt);
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2003-12-01 17:23:14 +02:00
|
|
|
if(s->mb_x){
|
2012-11-21 23:34:46 +03:00
|
|
|
if (qp_tt || IS_SKIP(s->current_picture.mb_type[xy - 1 - s->mb_stride]))
|
2008-12-11 23:45:41 +02:00
|
|
|
qp_dt= qp_tt;
|
2003-12-01 17:23:14 +02:00
|
|
|
else
|
2012-11-21 23:34:46 +03:00
|
|
|
qp_dt = s->current_picture.qscale_table[xy - 1 - s->mb_stride];
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2003-12-01 17:23:14 +02:00
|
|
|
if(qp_dt){
|
|
|
|
const int chroma_qp= s->chroma_qscale_table[qp_dt];
|
2013-11-05 10:11:47 +03:00
|
|
|
s->h263dsp.h263_h_loop_filter(dest_y - 8 * linesize, linesize, qp_dt);
|
|
|
|
s->h263dsp.h263_h_loop_filter(dest_cb - 8 * uvlinesize, uvlinesize, chroma_qp);
|
|
|
|
s->h263dsp.h263_h_loop_filter(dest_cr - 8 * uvlinesize, uvlinesize, chroma_qp);
|
2003-12-01 17:23:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(qp_c){
|
2013-11-05 10:11:47 +03:00
|
|
|
s->h263dsp.h263_h_loop_filter(dest_y + 8, linesize, qp_c);
|
2003-12-01 17:23:14 +02:00
|
|
|
if(s->mb_y + 1 == s->mb_height)
|
2013-11-05 10:11:47 +03:00
|
|
|
s->h263dsp.h263_h_loop_filter(dest_y + 8 * linesize + 8, linesize, qp_c);
|
2003-12-01 17:23:14 +02:00
|
|
|
}
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2003-12-01 17:23:14 +02:00
|
|
|
if(s->mb_x){
|
|
|
|
int qp_lc;
|
2012-11-21 23:34:46 +03:00
|
|
|
if (qp_c || IS_SKIP(s->current_picture.mb_type[xy - 1]))
|
2003-12-01 17:23:14 +02:00
|
|
|
qp_lc= qp_c;
|
|
|
|
else
|
2012-11-21 23:34:46 +03:00
|
|
|
qp_lc = s->current_picture.qscale_table[xy - 1];
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2003-12-01 17:23:14 +02:00
|
|
|
if(qp_lc){
|
2013-11-05 10:11:47 +03:00
|
|
|
s->h263dsp.h263_h_loop_filter(dest_y, linesize, qp_lc);
|
2003-12-01 17:23:14 +02:00
|
|
|
if(s->mb_y + 1 == s->mb_height){
|
|
|
|
const int chroma_qp= s->chroma_qscale_table[qp_lc];
|
2013-11-05 10:11:47 +03:00
|
|
|
s->h263dsp.h263_h_loop_filter(dest_y + 8 * linesize, linesize, qp_lc);
|
|
|
|
s->h263dsp.h263_h_loop_filter(dest_cb, uvlinesize, chroma_qp);
|
|
|
|
s->h263dsp.h263_h_loop_filter(dest_cr, uvlinesize, chroma_qp);
|
2003-12-01 17:23:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-09 12:28:46 +03:00
|
|
|
int16_t *ff_h263_pred_motion(MpegEncContext * s, int block, int dir,
|
|
|
|
int *px, int *py)
|
2001-07-22 17:18:56 +03:00
|
|
|
{
|
2004-04-16 04:01:45 +03:00
|
|
|
int wrap;
|
2003-12-04 20:34:47 +02:00
|
|
|
int16_t *A, *B, *C, (*mot_val)[2];
|
|
|
|
static const int off[4]= {2, 1, 1, -1};
|
|
|
|
|
|
|
|
wrap = s->b8_stride;
|
2012-11-21 23:34:46 +03:00
|
|
|
mot_val = s->current_picture.motion_val[dir] + s->block_index[block];
|
2003-12-04 20:34:47 +02:00
|
|
|
|
|
|
|
A = mot_val[ - 1];
|
|
|
|
/* special case for first (slice) line */
|
|
|
|
if (s->first_slice_line && block<3) {
|
2016-04-27 19:45:23 +02:00
|
|
|
// we can't just change some MVs to simulate that as we need them for the B-frames (and ME)
|
2003-12-04 20:34:47 +02:00
|
|
|
// and if we ever support non rectangular objects than we need to do a few ifs here anyway :(
|
|
|
|
if(block==0){ //most common case
|
|
|
|
if(s->mb_x == s->resync_mb_x){ //rare
|
|
|
|
*px= *py = 0;
|
|
|
|
}else if(s->mb_x + 1 == s->resync_mb_x && s->h263_pred){ //rare
|
|
|
|
C = mot_val[off[block] - wrap];
|
|
|
|
if(s->mb_x==0){
|
|
|
|
*px = C[0];
|
|
|
|
*py = C[1];
|
|
|
|
}else{
|
|
|
|
*px = mid_pred(A[0], 0, C[0]);
|
|
|
|
*py = mid_pred(A[1], 0, C[1]);
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
*px = A[0];
|
|
|
|
*py = A[1];
|
|
|
|
}
|
|
|
|
}else if(block==1){
|
|
|
|
if(s->mb_x + 1 == s->resync_mb_x && s->h263_pred){ //rare
|
|
|
|
C = mot_val[off[block] - wrap];
|
2010-01-08 01:53:49 +02:00
|
|
|
*px = mid_pred(A[0], 0, C[0]);
|
|
|
|
*py = mid_pred(A[1], 0, C[1]);
|
|
|
|
}else{
|
|
|
|
*px = A[0];
|
|
|
|
*py = A[1];
|
|
|
|
}
|
|
|
|
}else{ /* block==2*/
|
|
|
|
B = mot_val[ - wrap];
|
|
|
|
C = mot_val[off[block] - wrap];
|
|
|
|
if(s->mb_x == s->resync_mb_x) //rare
|
|
|
|
A[0]=A[1]=0;
|
2002-09-07 03:16:30 +03:00
|
|
|
|
2010-01-08 01:53:49 +02:00
|
|
|
*px = mid_pred(A[0], B[0], C[0]);
|
|
|
|
*py = mid_pred(A[1], B[1], C[1]);
|
|
|
|
}
|
2009-12-30 18:10:52 +02:00
|
|
|
} else {
|
2010-01-08 01:53:49 +02:00
|
|
|
B = mot_val[ - wrap];
|
|
|
|
C = mot_val[off[block] - wrap];
|
|
|
|
*px = mid_pred(A[0], B[0], C[0]);
|
|
|
|
*py = mid_pred(A[1], B[1], C[1]);
|
2004-09-24 23:35:59 +03:00
|
|
|
}
|
2010-01-08 01:53:49 +02:00
|
|
|
return *mot_val;
|
2004-09-24 23:35:59 +03:00
|
|
|
}
|