2001-07-22 17:18:56 +03:00
|
|
|
/*
|
|
|
|
* The simplest mpeg encoder (well, it was the simplest!)
|
|
|
|
* Copyright (c) 2000,2001 Gerard Lantau.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
2002-03-28 06:25:35 +02:00
|
|
|
*
|
2002-04-17 19:30:31 +03:00
|
|
|
* 4MV & hq & b-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
|
2001-07-22 17:18:56 +03:00
|
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "avcodec.h"
|
|
|
|
#include "dsputil.h"
|
|
|
|
#include "mpegvideo.h"
|
|
|
|
|
2001-08-02 11:29:38 +03:00
|
|
|
#ifdef USE_FASTMEMCPY
|
|
|
|
#include "fastmemcpy.h"
|
|
|
|
#endif
|
|
|
|
|
2001-07-31 02:26:26 +03:00
|
|
|
static void encode_picture(MpegEncContext *s, int picture_number);
|
|
|
|
static void rate_control_init(MpegEncContext *s);
|
|
|
|
static int rate_estimate_qscale(MpegEncContext *s);
|
|
|
|
static void dct_unquantize_mpeg1_c(MpegEncContext *s,
|
|
|
|
DCTELEM *block, int n, int qscale);
|
2002-04-17 19:30:31 +03:00
|
|
|
static void dct_unquantize_mpeg2_c(MpegEncContext *s,
|
|
|
|
DCTELEM *block, int n, int qscale);
|
2001-07-31 02:26:26 +03:00
|
|
|
static void dct_unquantize_h263_c(MpegEncContext *s,
|
|
|
|
DCTELEM *block, int n, int qscale);
|
2002-01-15 06:07:29 +02:00
|
|
|
static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w);
|
2002-01-27 15:30:18 +02:00
|
|
|
static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale);
|
2002-01-15 06:07:29 +02:00
|
|
|
|
2002-01-27 15:30:18 +02:00
|
|
|
int (*dct_quantize)(MpegEncContext *s, DCTELEM *block, int n, int qscale)= dct_quantize_c;
|
2002-01-15 06:07:29 +02:00
|
|
|
void (*draw_edges)(UINT8 *buf, int wrap, int width, int height, int w)= draw_edges_c;
|
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
#define EDGE_WIDTH 16
|
|
|
|
|
|
|
|
/* enable all paranoid tests for rounding, overflows, etc... */
|
|
|
|
//#define PARANOID
|
|
|
|
|
|
|
|
//#define DEBUG
|
|
|
|
|
2002-04-15 01:17:40 +03:00
|
|
|
|
2001-07-22 17:18:56 +03: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 01:45:13 +02:00
|
|
|
static UINT16 default_mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
|
2002-03-22 04:21:17 +02:00
|
|
|
static UINT8 default_fcode_tab[MAX_MV*2+1];
|
|
|
|
|
2002-01-13 06:59:37 +02:00
|
|
|
extern UINT8 zigzag_end[64];
|
|
|
|
|
2002-04-15 01:17:40 +03:00
|
|
|
/* default motion estimation */
|
|
|
|
int motion_estimation_method = ME_EPZS;
|
|
|
|
|
2002-01-27 15:30:18 +02:00
|
|
|
static void convert_matrix(int *qmat, UINT16 *qmat16, const UINT16 *quant_matrix, int qscale)
|
2001-07-22 17:18:56 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (av_fdct == jpeg_fdct_ifast) {
|
|
|
|
for(i=0;i<64;i++) {
|
|
|
|
/* 16 <= qscale * quant_matrix[i] <= 7905 */
|
2002-01-27 15:30:18 +02:00
|
|
|
/* 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 */
|
2001-07-22 17:18:56 +03:00
|
|
|
|
2002-01-27 15:30:18 +02:00
|
|
|
qmat[block_permute_op(i)] = (int)((UINT64_C(1) << (QMAT_SHIFT + 11)) /
|
|
|
|
(aanscales[i] * qscale * quant_matrix[block_permute_op(i)]));
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for(i=0;i<64;i++) {
|
|
|
|
/* We can safely suppose that 16 <= quant_matrix[i] <= 255
|
2002-01-27 15:30:18 +02:00
|
|
|
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
|
2001-07-22 17:18:56 +03:00
|
|
|
*/
|
2002-01-27 15:30:18 +02:00
|
|
|
qmat[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
|
|
|
|
qmat16[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[block_permute_op(i)]);
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* init common structure for both encoder and decoder */
|
|
|
|
int MPV_common_init(MpegEncContext *s)
|
|
|
|
{
|
|
|
|
int c_size, i;
|
|
|
|
UINT8 *pict;
|
|
|
|
|
2002-04-07 05:03:32 +03:00
|
|
|
s->dct_unquantize_h263 = dct_unquantize_h263_c;
|
2002-04-17 19:30:31 +03:00
|
|
|
s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_c;
|
|
|
|
s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_c;
|
2001-07-31 02:26:26 +03:00
|
|
|
|
|
|
|
#ifdef HAVE_MMX
|
|
|
|
MPV_common_init_mmx(s);
|
2001-07-30 12:04:34 +03:00
|
|
|
#endif
|
2002-04-07 05:03:32 +03:00
|
|
|
//setup default unquantizers (mpeg4 might change it later)
|
|
|
|
if(s->out_format == FMT_H263)
|
|
|
|
s->dct_unquantize = s->dct_unquantize_h263;
|
|
|
|
else
|
2002-04-17 19:30:31 +03:00
|
|
|
s->dct_unquantize = s->dct_unquantize_mpeg1;
|
2002-04-07 05:03:32 +03:00
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
s->mb_width = (s->width + 15) / 16;
|
|
|
|
s->mb_height = (s->height + 15) / 16;
|
2002-02-10 08:10:50 +02:00
|
|
|
s->mb_num = s->mb_width * s->mb_height;
|
2001-07-22 17:18:56 +03:00
|
|
|
s->linesize = s->mb_width * 16 + 2 * EDGE_WIDTH;
|
|
|
|
|
|
|
|
for(i=0;i<3;i++) {
|
|
|
|
int w, h, shift, pict_start;
|
|
|
|
|
|
|
|
w = s->linesize;
|
|
|
|
h = s->mb_height * 16 + 2 * EDGE_WIDTH;
|
|
|
|
shift = (i == 0) ? 0 : 1;
|
|
|
|
c_size = (w >> shift) * (h >> shift);
|
|
|
|
pict_start = (w >> shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift);
|
|
|
|
|
|
|
|
pict = av_mallocz(c_size);
|
|
|
|
if (pict == NULL)
|
|
|
|
goto fail;
|
|
|
|
s->last_picture_base[i] = pict;
|
|
|
|
s->last_picture[i] = pict + pict_start;
|
|
|
|
|
|
|
|
pict = av_mallocz(c_size);
|
|
|
|
if (pict == NULL)
|
|
|
|
goto fail;
|
|
|
|
s->next_picture_base[i] = pict;
|
|
|
|
s->next_picture[i] = pict + pict_start;
|
2002-04-17 07:32:12 +03:00
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
if (s->has_b_frames) {
|
|
|
|
pict = av_mallocz(c_size);
|
|
|
|
if (pict == NULL)
|
|
|
|
goto fail;
|
|
|
|
s->aux_picture_base[i] = pict;
|
|
|
|
s->aux_picture[i] = pict + pict_start;
|
|
|
|
}
|
|
|
|
}
|
2002-02-10 03:56:50 +02:00
|
|
|
|
|
|
|
if (s->encoding) {
|
2002-04-17 07:32:12 +03:00
|
|
|
int j;
|
|
|
|
int mv_table_size= (s->mb_width+2)*(s->mb_height+2);
|
|
|
|
|
2002-02-10 03:56:50 +02:00
|
|
|
/* Allocate MB type table */
|
2002-02-15 22:46:37 +02:00
|
|
|
s->mb_type = av_mallocz(s->mb_num * sizeof(char));
|
2002-02-10 03:56:50 +02:00
|
|
|
if (s->mb_type == NULL) {
|
|
|
|
perror("malloc");
|
|
|
|
goto fail;
|
|
|
|
}
|
2002-02-15 22:46:37 +02:00
|
|
|
|
|
|
|
s->mb_var = av_mallocz(s->mb_num * sizeof(INT16));
|
|
|
|
if (s->mb_var == NULL) {
|
|
|
|
perror("malloc");
|
|
|
|
goto fail;
|
|
|
|
}
|
2002-04-17 07:32:12 +03:00
|
|
|
|
|
|
|
/* Allocate MV tables */
|
|
|
|
s->p_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
|
|
|
|
if (s->p_mv_table == NULL) {
|
|
|
|
perror("malloc");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
s->last_p_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
|
|
|
|
if (s->last_p_mv_table == NULL) {
|
|
|
|
perror("malloc");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
s->b_forw_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
|
|
|
|
if (s->b_forw_mv_table == NULL) {
|
|
|
|
perror("malloc");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
s->b_back_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
|
|
|
|
if (s->b_back_mv_table == NULL) {
|
|
|
|
perror("malloc");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
s->b_bidir_forw_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
|
|
|
|
if (s->b_bidir_forw_mv_table == NULL) {
|
|
|
|
perror("malloc");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
s->b_bidir_back_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
|
|
|
|
if (s->b_bidir_back_mv_table == NULL) {
|
|
|
|
perror("malloc");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
s->b_direct_forw_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
|
|
|
|
if (s->b_direct_forw_mv_table == NULL) {
|
2002-02-10 03:56:50 +02:00
|
|
|
perror("malloc");
|
|
|
|
goto fail;
|
|
|
|
}
|
2002-04-17 07:32:12 +03:00
|
|
|
s->b_direct_back_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
|
|
|
|
if (s->b_direct_back_mv_table == NULL) {
|
|
|
|
perror("malloc");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
s->b_direct_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
|
|
|
|
if (s->b_direct_mv_table == NULL) {
|
|
|
|
perror("malloc");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
pict = av_mallocz(c_size);
|
|
|
|
if (pict == NULL)
|
|
|
|
goto fail;
|
|
|
|
s->picture_buffer[j][i] = pict;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-02-10 03:56:50 +02:00
|
|
|
}
|
|
|
|
|
2002-03-24 06:16:51 +02:00
|
|
|
if (s->out_format == FMT_H263 || s->encoding) {
|
2001-07-22 17:18:56 +03:00
|
|
|
int size;
|
|
|
|
/* MV prediction */
|
|
|
|
size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
|
|
|
|
s->motion_val = malloc(size * 2 * sizeof(INT16));
|
|
|
|
if (s->motion_val == NULL)
|
|
|
|
goto fail;
|
|
|
|
memset(s->motion_val, 0, size * 2 * sizeof(INT16));
|
|
|
|
}
|
|
|
|
|
2002-02-18 21:33:27 +02:00
|
|
|
if (s->h263_pred || s->h263_plus) {
|
2001-07-22 17:18:56 +03: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;
|
|
|
|
s->dc_val[0] = malloc(size * sizeof(INT16));
|
|
|
|
if (s->dc_val[0] == NULL)
|
|
|
|
goto fail;
|
|
|
|
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 */
|
|
|
|
s->ac_val[0] = av_mallocz(size * sizeof(INT16) * 16);
|
|
|
|
if (s->ac_val[0] == NULL)
|
|
|
|
goto fail;
|
|
|
|
s->ac_val[1] = s->ac_val[0] + y_size;
|
|
|
|
s->ac_val[2] = s->ac_val[1] + c_size;
|
|
|
|
|
|
|
|
/* cbp values */
|
|
|
|
s->coded_block = av_mallocz(y_size);
|
|
|
|
if (!s->coded_block)
|
|
|
|
goto fail;
|
2002-01-10 02:53:21 +02:00
|
|
|
|
|
|
|
/* which mb is a intra block */
|
2002-02-10 08:10:50 +02:00
|
|
|
s->mbintra_table = av_mallocz(s->mb_num);
|
2002-01-10 02:53:21 +02:00
|
|
|
if (!s->mbintra_table)
|
|
|
|
goto fail;
|
2002-02-10 08:10:50 +02:00
|
|
|
memset(s->mbintra_table, 1, s->mb_num);
|
2002-01-12 00:07:12 +02:00
|
|
|
}
|
2001-07-22 17:18:56 +03:00
|
|
|
/* default structure is frame */
|
|
|
|
s->picture_structure = PICT_FRAME;
|
|
|
|
|
2001-07-24 23:43:41 +03:00
|
|
|
/* init macroblock skip table */
|
2002-04-17 07:32:12 +03:00
|
|
|
s->mbskip_table = av_mallocz(s->mb_num);
|
|
|
|
if (!s->mbskip_table)
|
|
|
|
goto fail;
|
2002-03-27 23:25:22 +02:00
|
|
|
|
|
|
|
s->block= s->intra_block;
|
2001-07-24 23:43:41 +03:00
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
s->context_initialized = 1;
|
|
|
|
return 0;
|
|
|
|
fail:
|
2002-02-18 11:31:37 +02:00
|
|
|
MPV_common_end(s);
|
2001-07-22 17:18:56 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-04-17 07:32:12 +03:00
|
|
|
#define CHECK_FREE(p)\
|
|
|
|
{\
|
|
|
|
if(p) free(p);\
|
|
|
|
p= NULL;\
|
|
|
|
}
|
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
/* init common structure for both encoder and decoder */
|
|
|
|
void MPV_common_end(MpegEncContext *s)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2002-04-17 07:32:12 +03:00
|
|
|
CHECK_FREE(s->mb_type);
|
|
|
|
CHECK_FREE(s->mb_var);
|
|
|
|
CHECK_FREE(s->p_mv_table);
|
|
|
|
CHECK_FREE(s->last_p_mv_table);
|
|
|
|
CHECK_FREE(s->b_forw_mv_table);
|
|
|
|
CHECK_FREE(s->b_back_mv_table);
|
|
|
|
CHECK_FREE(s->b_bidir_forw_mv_table);
|
|
|
|
CHECK_FREE(s->b_bidir_back_mv_table);
|
|
|
|
CHECK_FREE(s->b_direct_forw_mv_table);
|
|
|
|
CHECK_FREE(s->b_direct_back_mv_table);
|
|
|
|
CHECK_FREE(s->b_direct_mv_table);
|
|
|
|
CHECK_FREE(s->motion_val);
|
|
|
|
CHECK_FREE(s->dc_val[0]);
|
|
|
|
CHECK_FREE(s->ac_val[0]);
|
|
|
|
CHECK_FREE(s->coded_block);
|
|
|
|
CHECK_FREE(s->mbintra_table);
|
|
|
|
|
|
|
|
CHECK_FREE(s->mbskip_table);
|
2001-07-22 17:18:56 +03:00
|
|
|
for(i=0;i<3;i++) {
|
2002-04-17 07:32:12 +03:00
|
|
|
int j;
|
|
|
|
CHECK_FREE(s->last_picture_base[i]);
|
|
|
|
CHECK_FREE(s->next_picture_base[i]);
|
|
|
|
CHECK_FREE(s->aux_picture_base[i]);
|
|
|
|
for(j=0; j<REORDER_BUFFER_SIZE; j++){
|
|
|
|
CHECK_FREE(s->picture_buffer[j][i]);
|
|
|
|
}
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
|
|
|
s->context_initialized = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* init video encoder */
|
|
|
|
int MPV_encode_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
MpegEncContext *s = avctx->priv_data;
|
2001-08-12 03:52:37 +03:00
|
|
|
int i;
|
2001-07-22 17:18:56 +03:00
|
|
|
|
2002-04-09 00:03:35 +03:00
|
|
|
avctx->pix_fmt = PIX_FMT_YUV420P;
|
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
s->bit_rate = avctx->bit_rate;
|
2002-03-17 18:31:38 +02:00
|
|
|
s->bit_rate_tolerance = avctx->bit_rate_tolerance;
|
2001-07-22 17:18:56 +03:00
|
|
|
s->frame_rate = avctx->frame_rate;
|
|
|
|
s->width = avctx->width;
|
|
|
|
s->height = avctx->height;
|
|
|
|
s->gop_size = avctx->gop_size;
|
2001-11-19 04:13:14 +02:00
|
|
|
s->rtp_mode = avctx->rtp_mode;
|
|
|
|
s->rtp_payload_size = avctx->rtp_payload_size;
|
2002-02-09 18:59:56 +02:00
|
|
|
if (avctx->rtp_callback)
|
|
|
|
s->rtp_callback = avctx->rtp_callback;
|
2002-03-17 18:31:38 +02: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 23:23:48 +02:00
|
|
|
s->avctx = avctx;
|
2002-03-23 01:22:08 +02:00
|
|
|
s->aspect_ratio_info= avctx->aspect_ratio_info;
|
2002-03-27 23:25:22 +02:00
|
|
|
s->flags= avctx->flags;
|
2002-04-17 07:32:12 +03:00
|
|
|
s->max_b_frames= avctx->max_b_frames;
|
2001-11-19 04:13:14 +02:00
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
if (s->gop_size <= 1) {
|
|
|
|
s->intra_only = 1;
|
|
|
|
s->gop_size = 12;
|
|
|
|
} else {
|
|
|
|
s->intra_only = 0;
|
|
|
|
}
|
2002-04-14 21:57:51 +03:00
|
|
|
|
|
|
|
/* ME algorithm */
|
2002-04-15 01:17:40 +03:00
|
|
|
if (avctx->me_method == 0)
|
|
|
|
/* For compatibility */
|
|
|
|
s->me_method = motion_estimation_method;
|
|
|
|
else
|
|
|
|
s->me_method = avctx->me_method;
|
|
|
|
|
2002-04-14 21:57:51 +03:00
|
|
|
/* Fixed QSCALE */
|
2001-07-22 17:18:56 +03:00
|
|
|
s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
|
2002-02-10 03:56:50 +02:00
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
switch(avctx->codec->id) {
|
|
|
|
case CODEC_ID_MPEG1VIDEO:
|
|
|
|
s->out_format = FMT_MPEG1;
|
|
|
|
break;
|
|
|
|
case CODEC_ID_MJPEG:
|
|
|
|
s->out_format = FMT_MJPEG;
|
|
|
|
s->intra_only = 1; /* force intra only for jpeg */
|
2002-02-10 03:56:50 +02:00
|
|
|
s->mjpeg_write_tables = 1; /* write all tables */
|
|
|
|
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;
|
|
|
|
s->mjpeg_hsample[0] = 2;
|
|
|
|
s->mjpeg_hsample[1] = 1;
|
|
|
|
s->mjpeg_hsample[2] = 1;
|
2001-07-22 17:18:56 +03:00
|
|
|
if (mjpeg_init(s) < 0)
|
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
case CODEC_ID_H263:
|
2002-02-10 03:56:50 +02: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 17:18:56 +03:00
|
|
|
return -1;
|
2002-02-10 03:56:50 +02:00
|
|
|
}
|
2001-07-22 17:18:56 +03:00
|
|
|
s->out_format = FMT_H263;
|
|
|
|
break;
|
|
|
|
case CODEC_ID_H263P:
|
|
|
|
s->out_format = FMT_H263;
|
2001-11-19 04:13:14 +02:00
|
|
|
s->rtp_mode = 1;
|
|
|
|
s->rtp_payload_size = 1200;
|
2001-07-22 17:18:56 +03:00
|
|
|
s->h263_plus = 1;
|
2001-08-29 02:32:34 +03:00
|
|
|
s->unrestricted_mv = 1;
|
2001-08-31 09:21:27 +03:00
|
|
|
|
|
|
|
/* These are just to be sure */
|
|
|
|
s->umvplus = 0;
|
|
|
|
s->umvplus_dec = 0;
|
2001-07-22 17:18:56 +03:00
|
|
|
break;
|
|
|
|
case CODEC_ID_RV10:
|
|
|
|
s->out_format = FMT_H263;
|
|
|
|
s->h263_rv10 = 1;
|
|
|
|
break;
|
2001-08-15 16:12:23 +03:00
|
|
|
case CODEC_ID_MPEG4:
|
2001-07-22 17:18:56 +03:00
|
|
|
s->out_format = FMT_H263;
|
|
|
|
s->h263_pred = 1;
|
|
|
|
s->unrestricted_mv = 1;
|
2002-04-17 07:32:12 +03:00
|
|
|
s->has_b_frames= s->max_b_frames ? 1 : 0;
|
2001-07-22 17:18:56 +03:00
|
|
|
break;
|
2002-04-05 07:09:04 +03:00
|
|
|
case CODEC_ID_MSMPEG4V1:
|
2001-07-22 17:18:56 +03:00
|
|
|
s->out_format = FMT_H263;
|
|
|
|
s->h263_msmpeg4 = 1;
|
|
|
|
s->h263_pred = 1;
|
|
|
|
s->unrestricted_mv = 1;
|
2002-04-05 07:09:04 +03:00
|
|
|
s->msmpeg4_version= 1;
|
|
|
|
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;
|
|
|
|
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;
|
2001-07-22 17:18:56 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
2002-03-28 06:25:35 +02:00
|
|
|
|
|
|
|
if((s->flags&CODEC_FLAG_4MV) && !(s->flags&CODEC_FLAG_HQ)){
|
|
|
|
printf("4MV is currently only supported in HQ mode\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2001-07-22 17:18:56 +03:00
|
|
|
|
2002-03-22 04:21:17 +02: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 01:45:13 +02:00
|
|
|
memset(default_mv_penalty, 0, sizeof(UINT16)*(MAX_FCODE+1)*(2*MAX_MV+1));
|
2002-03-22 04:21:17 +02: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;
|
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
if (s->out_format == FMT_H263)
|
2002-03-22 04:21:17 +02:00
|
|
|
h263_encode_init(s);
|
2002-03-23 01:22:08 +02:00
|
|
|
else if (s->out_format == FMT_MPEG1)
|
|
|
|
mpeg1_encode_init(s);
|
2001-07-22 17:18:56 +03:00
|
|
|
|
2002-03-23 22:21:33 +02:00
|
|
|
/* dont use mv_penalty table for crap MV as it would be confused */
|
2002-04-17 07:32:12 +03:00
|
|
|
if (s->me_method < ME_EPZS) s->mv_penalty = default_mv_penalty;
|
2002-03-23 22:21:33 +02:00
|
|
|
|
2001-07-24 23:43:41 +03:00
|
|
|
s->encoding = 1;
|
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
/* init */
|
|
|
|
if (MPV_common_init(s) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2001-08-12 03:52:37 +03:00
|
|
|
/* init default q matrix */
|
|
|
|
for(i=0;i<64;i++) {
|
|
|
|
s->intra_matrix[i] = default_intra_matrix[i];
|
|
|
|
s->non_intra_matrix[i] = default_non_intra_matrix[i];
|
|
|
|
}
|
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
/* rate control init */
|
|
|
|
rate_control_init(s);
|
|
|
|
|
|
|
|
s->picture_number = 0;
|
2002-03-22 04:21:17 +02:00
|
|
|
s->picture_in_gop_number = 0;
|
2001-07-22 17:18:56 +03:00
|
|
|
s->fake_picture_number = 0;
|
|
|
|
/* motion detector init */
|
|
|
|
s->f_code = 1;
|
2002-04-17 07:32:12 +03:00
|
|
|
s->b_code = 1;
|
2001-07-22 17:18:56 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int MPV_encode_end(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
MpegEncContext *s = avctx->priv_data;
|
|
|
|
|
|
|
|
#ifdef STATS
|
|
|
|
print_stats();
|
|
|
|
#endif
|
|
|
|
MPV_common_end(s);
|
|
|
|
if (s->out_format == FMT_MJPEG)
|
|
|
|
mjpeg_close(s);
|
2002-02-10 03:56:50 +02:00
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* draw the edges of width 'w' of an image of size width, height */
|
2002-01-15 06:07:29 +02:00
|
|
|
static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w)
|
2001-07-22 17:18:56 +03: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 */
|
|
|
|
void MPV_frame_start(MpegEncContext *s)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
UINT8 *tmp;
|
|
|
|
|
2001-08-09 00:57:07 +03:00
|
|
|
s->mb_skiped = 0;
|
2001-07-22 17:18:56 +03:00
|
|
|
if (s->pict_type == B_TYPE) {
|
|
|
|
for(i=0;i<3;i++) {
|
|
|
|
s->current_picture[i] = s->aux_picture[i];
|
|
|
|
}
|
|
|
|
} else {
|
2002-03-13 00:54:25 +02:00
|
|
|
s->last_non_b_pict_type= s->pict_type;
|
2001-07-22 17:18:56 +03:00
|
|
|
for(i=0;i<3;i++) {
|
|
|
|
/* swap next and last */
|
|
|
|
tmp = s->last_picture[i];
|
|
|
|
s->last_picture[i] = s->next_picture[i];
|
|
|
|
s->next_picture[i] = tmp;
|
|
|
|
s->current_picture[i] = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-07-31 02:26:26 +03:00
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
/* generic function for encode/decode called after a frame has been coded/decoded */
|
|
|
|
void MPV_frame_end(MpegEncContext *s)
|
|
|
|
{
|
|
|
|
/* draw edge for correct motion prediction if outside */
|
2002-01-27 15:30:18 +02:00
|
|
|
if (s->pict_type != B_TYPE && !s->intra_only) {
|
2002-03-10 05:17:53 +02:00
|
|
|
if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4 || s->divx_version==500){
|
2001-12-16 14:44:34 +02:00
|
|
|
draw_edges(s->current_picture[0], s->linesize, s->mb_width*16, s->mb_height*16, EDGE_WIDTH);
|
|
|
|
draw_edges(s->current_picture[1], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
|
|
|
|
draw_edges(s->current_picture[2], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
|
2002-01-10 02:46:40 +02:00
|
|
|
}else{
|
2002-03-10 05:17:53 +02:00
|
|
|
/* mpeg4? / opendivx / xvid */
|
2001-07-22 17:18:56 +03:00
|
|
|
draw_edges(s->current_picture[0], s->linesize, s->width, s->height, EDGE_WIDTH);
|
|
|
|
draw_edges(s->current_picture[1], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
|
|
|
|
draw_edges(s->current_picture[2], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
|
2002-01-10 02:46:40 +02:00
|
|
|
}
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
2002-01-15 07:02:26 +02:00
|
|
|
emms_c();
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
|
|
|
|
2002-04-17 07:32:12 +03: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
|
|
|
|
|
|
|
|
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
|
|
|
|
&& pict->linesize[1] == s->linesize>>1
|
|
|
|
&& pict->linesize[2] == s->linesize>>1){
|
|
|
|
//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++;
|
|
|
|
if(s->picture_buffer_index >= REORDER_BUFFER_SIZE-1) s->picture_buffer_index=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 17:18:56 +03: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 07:32:12 +03:00
|
|
|
s->input_qscale = avctx->quality;
|
2001-07-22 17:18:56 +03:00
|
|
|
|
|
|
|
init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
|
|
|
|
|
2002-04-17 07:32:12 +03:00
|
|
|
s->force_input_type= (avctx->flags&CODEC_FLAG_TYPE) ?
|
2002-03-29 01:07:54 +02:00
|
|
|
(avctx->key_frame ? I_TYPE : P_TYPE) : 0;
|
2001-07-22 17:18:56 +03:00
|
|
|
if (!s->intra_only) {
|
|
|
|
/* first picture of GOP is intra */
|
2002-04-17 07:32:12 +03:00
|
|
|
if (s->input_picture_in_gop_number % s->gop_size==0 || s->force_input_type==I_TYPE){
|
|
|
|
s->input_picture_in_gop_number=0;
|
|
|
|
s->input_pict_type = I_TYPE;
|
|
|
|
}else if(s->max_b_frames==0){
|
|
|
|
s->input_pict_type = P_TYPE;
|
|
|
|
}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;
|
|
|
|
}
|
2001-07-22 17:18:56 +03:00
|
|
|
} else {
|
2002-04-17 07:32:12 +03:00
|
|
|
s->input_pict_type = I_TYPE;
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
|
|
|
|
2002-04-17 07:32:12 +03: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;
|
|
|
|
|
|
|
|
MPV_frame_start(s);
|
|
|
|
|
|
|
|
encode_picture(s, s->picture_number);
|
|
|
|
avctx->key_frame = (s->pict_type == I_TYPE);
|
|
|
|
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;
|
|
|
|
avctx->p_count = s->p_count;
|
|
|
|
avctx->skip_count = s->skip_count;
|
|
|
|
|
|
|
|
MPV_frame_end(s);
|
|
|
|
|
|
|
|
if (s->out_format == FMT_MJPEG)
|
|
|
|
mjpeg_picture_trailer(s);
|
|
|
|
|
|
|
|
avctx->quality = s->qscale;
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
|
|
|
|
2002-04-17 07:32:12 +03:00
|
|
|
s->input_picture_number++;
|
|
|
|
s->input_picture_in_gop_number++;
|
2001-07-22 17:18:56 +03:00
|
|
|
|
|
|
|
flush_put_bits(&s->pb);
|
2002-03-17 18:31:38 +02:00
|
|
|
s->last_frame_bits= s->frame_bits;
|
|
|
|
s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
|
|
|
|
s->total_bits += s->frame_bits;
|
2002-03-23 19:43:30 +02: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-02-12 17:43:16 +02:00
|
|
|
|
2002-02-27 00:14:27 +02: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 07:32:12 +03:00
|
|
|
// printf("%f\n", avctx->psnr_y);
|
2002-02-27 00:14:27 +02:00
|
|
|
}
|
2002-02-12 17:43:16 +02:00
|
|
|
return pbBufPtr(&s->pb) - s->pb.buf;
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int clip(int a, int amin, int amax)
|
|
|
|
{
|
|
|
|
if (a < amin)
|
|
|
|
return amin;
|
|
|
|
else if (a > amax)
|
|
|
|
return amax;
|
|
|
|
else
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2002-03-09 15:01:16 +02: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-04-17 07:32:12 +03:00
|
|
|
int offset, src_x, src_y, linesize;
|
2002-03-09 15:01:16 +02:00
|
|
|
int motion_x, motion_y;
|
|
|
|
|
2002-03-15 06:10:53 +02:00
|
|
|
if(s->real_sprite_warping_points>1) printf("more than 1 warp point isnt supported\n");
|
2002-03-09 15:01:16 +02: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;
|
|
|
|
ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
|
|
|
|
|
|
|
|
dest_y+=dest_offset;
|
|
|
|
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;
|
|
|
|
|
|
|
|
offset = (src_y * linesize>>1) + src_x + (src_offset>>1);
|
|
|
|
ptr = ref_picture[1] + offset;
|
|
|
|
gmc1(dest_cb + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding);
|
|
|
|
ptr = ref_picture[2] + offset;
|
|
|
|
gmc1(dest_cr + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-07-22 17:18:56 +03: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,
|
|
|
|
int field_based, op_pixels_func *pix_op,
|
|
|
|
int motion_x, int motion_y, int h)
|
|
|
|
{
|
|
|
|
UINT8 *ptr;
|
|
|
|
int dxy, offset, mx, my, src_x, src_y, height, linesize;
|
2002-03-09 15:01:16 +02:00
|
|
|
if(s->quarter_sample)
|
|
|
|
{
|
|
|
|
motion_x>>=1;
|
|
|
|
motion_y>>=1;
|
|
|
|
}
|
2002-04-17 07:32:12 +03:00
|
|
|
|
2001-07-22 17:18:56 +03: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;
|
|
|
|
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;
|
|
|
|
linesize = s->linesize << field_based;
|
|
|
|
ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
|
|
|
|
dest_y += dest_offset;
|
|
|
|
pix_op[dxy](dest_y, ptr, linesize, h);
|
|
|
|
pix_op[dxy](dest_y + 8, ptr + 8, linesize, h);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
|
|
|
|
ptr = ref_picture[1] + offset;
|
|
|
|
pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
|
|
|
|
ptr = ref_picture[2] + offset;
|
|
|
|
pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
|
|
|
|
}
|
|
|
|
|
2002-03-09 15:01:16 +02: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,
|
|
|
|
int field_based, op_pixels_func *pix_op,
|
|
|
|
qpel_mc_func *qpix_op,
|
|
|
|
int motion_x, int motion_y, int h)
|
|
|
|
{
|
|
|
|
UINT8 *ptr;
|
|
|
|
int dxy, offset, mx, my, src_x, src_y, height, linesize;
|
|
|
|
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
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);
|
|
|
|
qpix_op[dxy](dest_y , ptr , linesize, linesize, motion_x&3, motion_y&3);
|
|
|
|
qpix_op[dxy](dest_y + 8, ptr + 8, linesize, linesize, motion_x&3, motion_y&3);
|
|
|
|
qpix_op[dxy](dest_y + linesize*8 , ptr + linesize*8 , linesize, linesize, motion_x&3, motion_y&3);
|
|
|
|
qpix_op[dxy](dest_y + linesize*8 + 8, ptr + linesize*8 + 8, linesize, linesize, motion_x&3, motion_y&3);
|
|
|
|
|
|
|
|
mx= (motion_x>>1) | (motion_x&1);
|
|
|
|
my= (motion_y>>1) | (motion_y&1);
|
|
|
|
|
|
|
|
dxy = 0;
|
|
|
|
if ((mx & 3) != 0)
|
|
|
|
dxy |= 1;
|
|
|
|
if ((my & 3) != 0)
|
|
|
|
dxy |= 2;
|
|
|
|
mx = mx >> 2;
|
|
|
|
my = my >> 2;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
|
|
|
|
ptr = ref_picture[1] + offset;
|
|
|
|
pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
|
|
|
|
ptr = ref_picture[2] + offset;
|
|
|
|
pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
static inline void MPV_motion(MpegEncContext *s,
|
|
|
|
UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
|
|
|
|
int dir, UINT8 **ref_picture,
|
2002-03-09 15:01:16 +02:00
|
|
|
op_pixels_func *pix_op, qpel_mc_func *qpix_op)
|
2001-07-22 17:18:56 +03:00
|
|
|
{
|
|
|
|
int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
|
|
|
|
int mb_x, mb_y, i;
|
|
|
|
UINT8 *ptr, *dest;
|
|
|
|
|
|
|
|
mb_x = s->mb_x;
|
|
|
|
mb_y = s->mb_y;
|
|
|
|
|
|
|
|
switch(s->mv_type) {
|
|
|
|
case MV_TYPE_16X16:
|
2002-03-09 15:01:16 +02:00
|
|
|
if(s->mcsel){
|
|
|
|
#if 0
|
|
|
|
mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
|
|
|
|
ref_picture, 0,
|
|
|
|
0, pix_op,
|
|
|
|
s->sprite_offset[0][0]>>3,
|
|
|
|
s->sprite_offset[0][1]>>3,
|
|
|
|
16);
|
|
|
|
#else
|
|
|
|
gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
|
|
|
|
ref_picture, 0,
|
|
|
|
16);
|
|
|
|
#endif
|
2002-03-13 00:54:25 +02:00
|
|
|
}else if(s->quarter_sample && dir==0){ //FIXME
|
2002-03-09 15:01:16 +02: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 17:18:56 +03:00
|
|
|
break;
|
|
|
|
case MV_TYPE_8X8:
|
|
|
|
for(i=0;i<4;i++) {
|
|
|
|
motion_x = s->mv[dir][i][0];
|
|
|
|
motion_y = s->mv[dir][i][1];
|
|
|
|
|
|
|
|
dxy = ((motion_y & 1) << 1) | (motion_x & 1);
|
|
|
|
src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
|
2002-03-28 06:25:35 +02:00
|
|
|
src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
|
2001-07-22 17:18:56 +03: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;
|
|
|
|
|
|
|
|
ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
|
|
|
|
dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
|
|
|
|
pix_op[dxy](dest, ptr, s->linesize, 8);
|
|
|
|
}
|
|
|
|
/* In case of 8X8, we construct a single chroma motion vector
|
|
|
|
with a special rounding */
|
|
|
|
mx = 0;
|
|
|
|
my = 0;
|
|
|
|
for(i=0;i<4;i++) {
|
|
|
|
mx += s->mv[dir][i][0];
|
|
|
|
my += s->mv[dir][i][1];
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
|
|
|
offset = (src_y * (s->linesize >> 1)) + src_x;
|
|
|
|
ptr = ref_picture[1] + offset;
|
|
|
|
pix_op[dxy](dest_cb, ptr, s->linesize >> 1, 8);
|
|
|
|
ptr = ref_picture[2] + offset;
|
|
|
|
pix_op[dxy](dest_cr, ptr, s->linesize >> 1, 8);
|
|
|
|
break;
|
|
|
|
case MV_TYPE_FIELD:
|
|
|
|
if (s->picture_structure == PICT_FRAME) {
|
|
|
|
/* 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);
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* put block[] to dest[] */
|
|
|
|
static inline void put_dct(MpegEncContext *s,
|
|
|
|
DCTELEM *block, int i, UINT8 *dest, int line_size)
|
|
|
|
{
|
|
|
|
if (!s->mpeg2)
|
2001-07-31 02:26:26 +03:00
|
|
|
s->dct_unquantize(s, block, i, s->qscale);
|
2001-08-03 21:33:03 +03:00
|
|
|
ff_idct (block);
|
2001-07-22 17:18:56 +03:00
|
|
|
put_pixels_clamped(block, dest, line_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add block[] to dest[] */
|
|
|
|
static inline void add_dct(MpegEncContext *s,
|
|
|
|
DCTELEM *block, int i, UINT8 *dest, int line_size)
|
|
|
|
{
|
|
|
|
if (s->block_last_index[i] >= 0) {
|
|
|
|
if (!s->mpeg2)
|
2002-01-15 06:07:29 +02:00
|
|
|
if(s->encoding || (!s->h263_msmpeg4))
|
2002-01-13 06:59:37 +02:00
|
|
|
s->dct_unquantize(s, block, i, s->qscale);
|
2002-04-17 07:32:12 +03:00
|
|
|
|
2001-08-03 21:33:03 +03:00
|
|
|
ff_idct (block);
|
2001-07-22 17:18:56 +03:00
|
|
|
add_pixels_clamped(block, dest, line_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 11:31:37 +02:00
|
|
|
int mb_x, mb_y;
|
2001-07-22 17:18:56 +03:00
|
|
|
int dct_linesize, dct_offset;
|
|
|
|
op_pixels_func *op_pix;
|
2002-03-09 15:01:16 +02:00
|
|
|
qpel_mc_func *op_qpix;
|
2001-07-22 17:18:56 +03:00
|
|
|
|
|
|
|
mb_x = s->mb_x;
|
|
|
|
mb_y = s->mb_y;
|
|
|
|
|
2001-10-17 02:10:38 +03:00
|
|
|
#ifdef FF_POSTPROCESS
|
|
|
|
quant_store[mb_y][mb_x]=s->qscale;
|
|
|
|
//printf("[%02d][%02d] %d\n",mb_x,mb_y,s->qscale);
|
|
|
|
#endif
|
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
/* update DC predictors for P macroblocks */
|
|
|
|
if (!s->mb_intra) {
|
2002-02-19 21:23:34 +02:00
|
|
|
if (s->h263_pred || s->h263_aic) {
|
2002-01-10 02:53:21 +02:00
|
|
|
if(s->mbintra_table[mb_x + mb_y*s->mb_width])
|
|
|
|
{
|
2002-02-18 11:31:37 +02:00
|
|
|
int wrap, xy, v;
|
2002-01-10 02:53:21 +02:00
|
|
|
s->mbintra_table[mb_x + mb_y*s->mb_width]=0;
|
2001-07-22 17:18:56 +03:00
|
|
|
wrap = 2 * s->mb_width + 2;
|
2002-02-18 11:31:37 +02:00
|
|
|
xy = 2 * mb_x + 1 + (2 * mb_y + 1) * wrap;
|
2001-07-22 17:18:56 +03:00
|
|
|
v = 1024;
|
2002-01-10 02:53:21 +02:00
|
|
|
|
2002-02-19 21:23:34 +02:00
|
|
|
s->dc_val[0][xy] = v;
|
2002-02-18 11:31:37 +02:00
|
|
|
s->dc_val[0][xy + 1] = v;
|
|
|
|
s->dc_val[0][xy + wrap] = v;
|
|
|
|
s->dc_val[0][xy + 1 + wrap] = v;
|
2001-07-22 17:18:56 +03:00
|
|
|
/* ac pred */
|
2002-02-18 11:31:37 +02:00
|
|
|
memset(s->ac_val[0][xy], 0, 16 * sizeof(INT16));
|
|
|
|
memset(s->ac_val[0][xy + 1], 0, 16 * sizeof(INT16));
|
|
|
|
memset(s->ac_val[0][xy + wrap], 0, 16 * sizeof(INT16));
|
|
|
|
memset(s->ac_val[0][xy + 1 + wrap], 0, 16 * sizeof(INT16));
|
2001-07-22 17:18:56 +03:00
|
|
|
if (s->h263_msmpeg4) {
|
2002-02-18 11:31:37 +02:00
|
|
|
s->coded_block[xy] = 0;
|
|
|
|
s->coded_block[xy + 1] = 0;
|
|
|
|
s->coded_block[xy + wrap] = 0;
|
|
|
|
s->coded_block[xy + 1 + wrap] = 0;
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
|
|
|
/* chroma */
|
|
|
|
wrap = s->mb_width + 2;
|
2002-02-18 11:31:37 +02:00
|
|
|
xy = mb_x + 1 + (mb_y + 1) * wrap;
|
|
|
|
s->dc_val[1][xy] = v;
|
|
|
|
s->dc_val[2][xy] = v;
|
2001-07-22 17:18:56 +03:00
|
|
|
/* ac pred */
|
2002-02-18 11:31:37 +02:00
|
|
|
memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16));
|
|
|
|
memset(s->ac_val[2][xy], 0, 16 * sizeof(INT16));
|
2002-01-10 02:53:21 +02:00
|
|
|
}
|
2001-07-22 17:18:56 +03:00
|
|
|
} else {
|
|
|
|
s->last_dc[0] = 128 << s->intra_dc_precision;
|
|
|
|
s->last_dc[1] = 128 << s->intra_dc_precision;
|
|
|
|
s->last_dc[2] = 128 << s->intra_dc_precision;
|
|
|
|
}
|
|
|
|
}
|
2002-02-19 21:23:34 +02:00
|
|
|
else if (s->h263_pred || s->h263_aic)
|
2002-01-10 02:53:21 +02:00
|
|
|
s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
|
|
|
|
|
2002-03-22 18:51:44 +02:00
|
|
|
/* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
|
2002-04-17 07:32:12 +03:00
|
|
|
if (s->out_format == FMT_H263) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here
|
2002-03-13 00:54:25 +02:00
|
|
|
if(s->pict_type!=B_TYPE){
|
2002-02-18 11:31:37 +02:00
|
|
|
int xy, wrap, motion_x, motion_y;
|
2001-07-22 17:18:56 +03:00
|
|
|
|
|
|
|
wrap = 2 * s->mb_width + 2;
|
2002-02-18 11:31:37 +02:00
|
|
|
xy = 2 * mb_x + 1 + (2 * mb_y + 1) * wrap;
|
2001-07-22 17:18:56 +03:00
|
|
|
if (s->mb_intra) {
|
|
|
|
motion_x = 0;
|
|
|
|
motion_y = 0;
|
|
|
|
goto motion_init;
|
|
|
|
} else if (s->mv_type == MV_TYPE_16X16) {
|
|
|
|
motion_x = s->mv[0][0][0];
|
|
|
|
motion_y = s->mv[0][0][1];
|
|
|
|
motion_init:
|
|
|
|
/* no update if 8X8 because it has been done during parsing */
|
2002-02-18 11:31:37 +02: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 17:18:56 +03:00
|
|
|
}
|
2002-03-13 00:54:25 +02:00
|
|
|
}
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
|
|
|
|
2002-04-17 07:32:12 +03:00
|
|
|
if (!(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) {
|
2001-07-22 17:18:56 +03:00
|
|
|
UINT8 *dest_y, *dest_cb, *dest_cr;
|
2001-07-24 23:43:41 +03:00
|
|
|
UINT8 *mbskip_ptr;
|
|
|
|
|
2002-04-17 07:32:12 +03: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) {
|
2001-07-24 23:43:41 +03:00
|
|
|
mbskip_ptr = &s->mbskip_table[s->mb_y * s->mb_width + s->mb_x];
|
|
|
|
if (s->mb_skiped) {
|
|
|
|
s->mb_skiped = 0;
|
2002-04-17 07:32:12 +03:00
|
|
|
/* if previous was skipped too, then nothing to do !
|
|
|
|
skip only during decoding as we might trash the buffers during encoding a bit */
|
|
|
|
if (*mbskip_ptr != 0 && !s->encoding)
|
2001-07-24 23:43:41 +03:00
|
|
|
goto the_end;
|
|
|
|
*mbskip_ptr = 1; /* indicate that this time we skiped it */
|
|
|
|
} else {
|
|
|
|
*mbskip_ptr = 0; /* not skipped */
|
|
|
|
}
|
|
|
|
}
|
2001-07-22 17:18:56 +03:00
|
|
|
|
|
|
|
dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize) + mb_x * 16;
|
|
|
|
dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
|
|
|
|
dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
|
|
|
|
|
|
|
|
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-04-17 07:32:12 +03:00
|
|
|
if((s->flags&CODEC_FLAG_HQ) || (!s->encoding)){
|
|
|
|
if (!s->no_rounding){
|
|
|
|
op_pix = put_pixels_tab;
|
|
|
|
op_qpix= qpel_mc_rnd_tab;
|
|
|
|
}else{
|
|
|
|
op_pix = put_no_rnd_pixels_tab;
|
|
|
|
op_qpix= qpel_mc_no_rnd_tab;
|
|
|
|
}
|
2001-07-22 17:18:56 +03:00
|
|
|
|
2002-04-17 07:32:12 +03: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);
|
|
|
|
if (!s->no_rounding)
|
|
|
|
op_pix = avg_pixels_tab;
|
|
|
|
else
|
|
|
|
op_pix = avg_no_rnd_pixels_tab;
|
|
|
|
}
|
|
|
|
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 17:18:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add dct residue */
|
|
|
|
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-08-12 01:07:21 +03:00
|
|
|
add_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
|
|
|
|
add_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
|
2001-07-22 17:18:56 +03:00
|
|
|
} else {
|
|
|
|
/* dct only in intra block */
|
|
|
|
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);
|
|
|
|
|
2001-08-12 01:07:21 +03:00
|
|
|
put_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
|
|
|
|
put_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
|
|
|
}
|
2001-07-24 23:43:41 +03:00
|
|
|
the_end:
|
2002-03-27 23:25:22 +02:00
|
|
|
emms_c(); //FIXME remove
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
|
|
|
|
2002-04-17 07:32:12 +03:00
|
|
|
|
|
|
|
static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
|
2001-07-22 17:18:56 +03:00
|
|
|
{
|
2002-03-27 23:25:22 +02:00
|
|
|
const int mb_x= s->mb_x;
|
|
|
|
const int mb_y= s->mb_y;
|
|
|
|
int i;
|
2002-04-17 07:32:12 +03:00
|
|
|
#if 0
|
|
|
|
if (s->interlaced_dct) {
|
|
|
|
dct_linesize = s->linesize * 2;
|
|
|
|
dct_offset = s->linesize;
|
|
|
|
} else {
|
|
|
|
dct_linesize = s->linesize;
|
|
|
|
dct_offset = s->linesize * 8;
|
|
|
|
}
|
|
|
|
#endif
|
2002-03-27 23:25:22 +02:00
|
|
|
|
2002-04-17 07:32:12 +03:00
|
|
|
if (s->mb_intra) {
|
|
|
|
UINT8 *ptr;
|
|
|
|
int wrap;
|
|
|
|
|
|
|
|
wrap = s->linesize;
|
|
|
|
ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
|
|
|
|
get_pixels(s->block[0], ptr , wrap);
|
|
|
|
get_pixels(s->block[1], ptr + 8, wrap);
|
|
|
|
get_pixels(s->block[2], ptr + 8 * wrap , wrap);
|
|
|
|
get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap);
|
|
|
|
|
|
|
|
wrap >>=1;
|
|
|
|
ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
|
|
|
|
get_pixels(s->block[4], ptr, wrap);
|
|
|
|
|
|
|
|
ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
|
|
|
|
get_pixels(s->block[5], ptr, wrap);
|
|
|
|
}else{
|
|
|
|
op_pixels_func *op_pix;
|
|
|
|
qpel_mc_func *op_qpix;
|
|
|
|
UINT8 *dest_y, *dest_cb, *dest_cr;
|
|
|
|
UINT8 *ptr;
|
|
|
|
int wrap;
|
|
|
|
|
|
|
|
dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize ) + mb_x * 16;
|
|
|
|
dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
|
|
|
|
dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
|
|
|
|
|
|
|
|
if (!s->no_rounding){
|
|
|
|
op_pix = put_pixels_tab;
|
|
|
|
op_qpix= qpel_mc_rnd_tab;
|
2002-03-28 06:25:35 +02:00
|
|
|
}else{
|
2002-04-17 07:32:12 +03:00
|
|
|
op_pix = put_no_rnd_pixels_tab;
|
|
|
|
op_qpix= qpel_mc_no_rnd_tab;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->mv_dir & MV_DIR_FORWARD) {
|
|
|
|
MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix);
|
|
|
|
if (!s->no_rounding)
|
|
|
|
op_pix = avg_pixels_tab;
|
|
|
|
else
|
|
|
|
op_pix = avg_no_rnd_pixels_tab;
|
|
|
|
}
|
|
|
|
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 23:25:22 +02:00
|
|
|
}
|
2002-04-17 07:32:12 +03:00
|
|
|
wrap = s->linesize;
|
|
|
|
ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
|
|
|
|
diff_pixels(s->block[0], ptr , dest_y , wrap);
|
|
|
|
diff_pixels(s->block[1], ptr + 8, dest_y + 8, wrap);
|
|
|
|
diff_pixels(s->block[2], ptr + 8 * wrap , dest_y + 8 * wrap , wrap);
|
|
|
|
diff_pixels(s->block[3], ptr + 8 * wrap + 8, dest_y + 8 * wrap + 8, wrap);
|
|
|
|
|
|
|
|
wrap >>=1;
|
|
|
|
ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
|
|
|
|
diff_pixels(s->block[4], ptr, dest_cb, wrap);
|
|
|
|
|
|
|
|
ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
|
|
|
|
diff_pixels(s->block[5], ptr, dest_cr, wrap);
|
2002-03-27 23:25:22 +02: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 */
|
|
|
|
if (s->h263_msmpeg4) {
|
|
|
|
msmpeg4_dc_scale(s);
|
|
|
|
} else if (s->h263_pred) {
|
|
|
|
h263_dc_scale(s);
|
|
|
|
} else {
|
|
|
|
/* default quantization values */
|
|
|
|
s->y_dc_scale = 8;
|
|
|
|
s->c_dc_scale = 8;
|
|
|
|
}
|
|
|
|
for(i=0;i<6;i++) {
|
|
|
|
s->block_last_index[i] = dct_quantize(s, s->block[i], i, s->qscale);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void copy_bits(PutBitContext *pb, UINT8 *src, int length)
|
|
|
|
{
|
2002-04-17 21:19:50 +03:00
|
|
|
#if 1
|
|
|
|
int bytes= length>>4;
|
|
|
|
int bits= length&15;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
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));
|
|
|
|
#else
|
2002-03-27 23:25:22 +02:00
|
|
|
int bytes= length>>3;
|
|
|
|
int bits= length&7;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i=0; i<bytes; i++) put_bits(pb, 8, src[i]);
|
|
|
|
put_bits(pb, bits, src[i]>>(8-bits));
|
2002-04-17 21:19:50 +03:00
|
|
|
#endif
|
2002-03-27 23:25:22 +02:00
|
|
|
}
|
|
|
|
|
2002-04-17 21:19:50 +03:00
|
|
|
static void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
|
|
|
|
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;
|
|
|
|
d->p_count= s->p_count;
|
|
|
|
d->skip_count= s->skip_count;
|
|
|
|
d->misc_bits= s->misc_bits;
|
|
|
|
d->last_bits= s->last_bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
|
|
|
|
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;
|
|
|
|
d->p_count= s->p_count;
|
|
|
|
d->skip_count= s->skip_count;
|
|
|
|
d->misc_bits= s->misc_bits;
|
|
|
|
d->last_bits= s->last_bits;
|
|
|
|
|
|
|
|
d->mb_intra= s->mb_intra;
|
|
|
|
d->mv_type= s->mv_type;
|
|
|
|
d->mv_dir= s->mv_dir;
|
|
|
|
d->pb= s->pb;
|
|
|
|
d->block= s->block;
|
|
|
|
for(i=0; i<6; i++)
|
|
|
|
d->block_last_index[i]= s->block_last_index[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-27 23:25:22 +02:00
|
|
|
static void encode_picture(MpegEncContext *s, int picture_number)
|
|
|
|
{
|
|
|
|
int mb_x, mb_y, last_gob, pdif = 0;
|
|
|
|
int i;
|
2002-03-23 19:43:30 +02:00
|
|
|
int bits;
|
2002-04-17 21:19:50 +03:00
|
|
|
MpegEncContext best_s, backup_s;
|
2002-03-27 23:25:22 +02:00
|
|
|
UINT8 bit_buf[4][3000]; //FIXME check that this is ALLWAYS large enogh for a MB
|
2001-07-22 17:18:56 +03:00
|
|
|
|
|
|
|
s->picture_number = picture_number;
|
2002-03-17 18:31:38 +02:00
|
|
|
|
2002-03-27 23:25:22 +02: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 18:31:38 +02:00
|
|
|
s->last_mc_mb_var = s->mc_mb_var;
|
|
|
|
/* Reset the average MB variance */
|
|
|
|
s->avg_mb_var = 0;
|
|
|
|
s->mc_mb_var = 0;
|
2002-04-17 07:32:12 +03:00
|
|
|
|
2002-03-17 18:31:38 +02:00
|
|
|
/* Estimate motion for every MB */
|
2002-04-17 07:32:12 +03:00
|
|
|
if(s->pict_type != I_TYPE){
|
|
|
|
// int16_t (*tmp)[2]= s->p_mv_table;
|
|
|
|
// s->p_mv_table= s->last_mv_table;
|
|
|
|
// s->last_mv_table= s->mv_table;
|
|
|
|
|
2002-03-27 23:25:22 +02: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 07:32:12 +03: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 23:25:22 +02:00
|
|
|
// s->mb_type[mb_y*s->mb_width + mb_x]=MB_TYPE_INTER;
|
2002-03-17 18:31:38 +02:00
|
|
|
}
|
|
|
|
}
|
2002-03-27 23:25:22 +02:00
|
|
|
emms_c();
|
2002-04-17 07:32:12 +03:00
|
|
|
}else if(s->pict_type == I_TYPE){
|
2002-03-27 23:25:22 +02: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 07:32:12 +03:00
|
|
|
memset(s->p_mv_table , 0, sizeof(INT16)*(s->mb_width+2)*(s->mb_height+2)*2);
|
2002-03-27 23:25:22 +02:00
|
|
|
memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
|
2002-03-17 18:31:38 +02:00
|
|
|
}
|
|
|
|
|
2002-04-17 07:32:12 +03:00
|
|
|
if(s->avg_mb_var < s->mc_mb_var && s->pict_type != B_TYPE && (!s->force_type) && s->max_b_frames==0){ //FIXME subtract MV bits
|
|
|
|
// FIXME b-frames & scene change detection
|
|
|
|
s->input_pict_type= I_TYPE;
|
2002-03-17 21:45:33 +02:00
|
|
|
s->pict_type= I_TYPE;
|
2002-04-17 07:32:12 +03:00
|
|
|
s->input_picture_in_gop_number=0;
|
2002-03-27 23:25:22 +02:00
|
|
|
memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
|
|
|
|
//printf("Scene change detected, encoding as I Frame\n");
|
2002-03-17 21:45:33 +02:00
|
|
|
}
|
2002-04-17 07:32:12 +03: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);
|
|
|
|
//FIXME if BIDIR != for&back
|
|
|
|
ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD |MB_TYPE_BIDIR);
|
|
|
|
ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD|MB_TYPE_BIDIR);
|
2002-03-22 04:21:17 +02:00
|
|
|
}
|
2002-04-17 07:32:12 +03:00
|
|
|
|
2002-03-22 04:21:17 +02:00
|
|
|
//printf("f_code %d ///\n", s->f_code);
|
|
|
|
|
2002-03-17 21:45:33 +02:00
|
|
|
// printf("%d %d\n", s->avg_mb_var, s->mc_mb_var);
|
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
if (!s->fixed_qscale)
|
|
|
|
s->qscale = rate_estimate_qscale(s);
|
|
|
|
|
|
|
|
/* precompute matrix */
|
|
|
|
if (s->out_format == FMT_MJPEG) {
|
|
|
|
/* for mjpeg, we do include qscale in the matrix */
|
|
|
|
s->intra_matrix[0] = default_intra_matrix[0];
|
|
|
|
for(i=1;i<64;i++)
|
|
|
|
s->intra_matrix[i] = (default_intra_matrix[i] * s->qscale) >> 3;
|
2002-01-27 15:30:18 +02:00
|
|
|
convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, 8);
|
2001-07-22 17:18:56 +03:00
|
|
|
} else {
|
2002-01-27 15:30:18 +02:00
|
|
|
convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, s->qscale);
|
|
|
|
convert_matrix(s->q_non_intra_matrix, s->q_non_intra_matrix16, s->non_intra_matrix, s->qscale);
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
|
|
|
|
2002-03-23 19:43:30 +02:00
|
|
|
s->last_bits= get_bit_count(&s->pb);
|
2001-07-22 17:18:56 +03: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 19:43:30 +02: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;
|
|
|
|
s->p_count=0;
|
|
|
|
s->skip_count=0;
|
|
|
|
|
2001-07-22 17:18:56 +03: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 04:13:14 +02:00
|
|
|
/* Get the GOB height based on picture height */
|
2002-02-09 18:59:56 +02:00
|
|
|
if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4) {
|
2001-11-19 04:13:14 +02:00
|
|
|
if (s->height <= 400)
|
|
|
|
s->gob_index = 1;
|
|
|
|
else if (s->height <= 800)
|
|
|
|
s->gob_index = 2;
|
|
|
|
else
|
|
|
|
s->gob_index = 4;
|
|
|
|
}
|
2002-03-17 18:31:38 +02:00
|
|
|
|
2002-02-10 08:10:50 +02:00
|
|
|
s->avg_mb_var = s->avg_mb_var / s->mb_num;
|
|
|
|
|
|
|
|
for(mb_y=0; mb_y < s->mb_height; mb_y++) {
|
|
|
|
/* Put GOB header based on RTP MTU */
|
|
|
|
/* 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)) {
|
|
|
|
last_gob = h263_encode_gob_header(s, mb_y);
|
|
|
|
if (last_gob) {
|
|
|
|
s->first_gob_line = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-15 16:57:17 +02: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 03:56:50 +02:00
|
|
|
for(mb_x=0; mb_x < s->mb_width; mb_x++) {
|
2002-04-17 07:32:12 +03:00
|
|
|
/*const */int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];
|
2002-03-27 23:25:22 +02:00
|
|
|
PutBitContext pb;
|
|
|
|
int d;
|
|
|
|
int dmin=10000000;
|
|
|
|
int best=0;
|
2002-02-10 03:56:50 +02:00
|
|
|
|
|
|
|
s->mb_x = mb_x;
|
|
|
|
s->mb_y = mb_y;
|
2002-03-15 16:57:17 +02: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-03-27 23:25:22 +02:00
|
|
|
if(mb_type & (mb_type-1)){ // more than 1 MB type possible
|
|
|
|
pb= s->pb;
|
2002-04-17 07:32:12 +03:00
|
|
|
s->mv_dir = MV_DIR_FORWARD;
|
2002-04-17 21:19:50 +03:00
|
|
|
|
|
|
|
copy_context_before_encode(&backup_s, s, -1);
|
|
|
|
|
2002-03-27 23:25:22 +02:00
|
|
|
if(mb_type&MB_TYPE_INTER){
|
2002-04-17 07:32:12 +03:00
|
|
|
int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
|
2002-03-28 06:25:35 +02:00
|
|
|
s->mv_type = MV_TYPE_16X16;
|
2002-03-27 23:25:22 +02:00
|
|
|
s->mb_intra= 0;
|
2002-04-17 07:32:12 +03:00
|
|
|
s->mv[0][0][0] = s->p_mv_table[xy][0];
|
|
|
|
s->mv[0][0][1] = s->p_mv_table[xy][1];
|
2002-03-27 23:25:22 +02:00
|
|
|
init_put_bits(&s->pb, bit_buf[1], 3000, NULL, NULL);
|
|
|
|
s->block= s->inter_block;
|
|
|
|
|
2002-04-17 07:32:12 +03:00
|
|
|
encode_mb(s, s->mv[0][0][0], s->mv[0][0][1]);
|
2002-03-27 23:25:22 +02:00
|
|
|
d= get_bit_count(&s->pb);
|
|
|
|
if(d<dmin){
|
|
|
|
flush_put_bits(&s->pb);
|
|
|
|
dmin=d;
|
2002-04-17 21:19:50 +03:00
|
|
|
copy_context_after_encode(&best_s, s, MB_TYPE_INTER);
|
2002-03-27 23:25:22 +02:00
|
|
|
best=1;
|
|
|
|
}
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
2002-04-17 21:19:50 +03:00
|
|
|
if(mb_type&MB_TYPE_INTER4V){
|
|
|
|
copy_context_before_encode(s, &backup_s, MB_TYPE_INTER4V);
|
2002-03-28 06:25:35 +02: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];
|
|
|
|
}
|
|
|
|
init_put_bits(&s->pb, bit_buf[2], 3000, NULL, NULL);
|
|
|
|
s->block= s->inter4v_block;
|
|
|
|
|
2002-04-17 07:32:12 +03:00
|
|
|
encode_mb(s, 0, 0);
|
2002-03-28 06:25:35 +02:00
|
|
|
d= get_bit_count(&s->pb);
|
2002-04-17 07:32:12 +03:00
|
|
|
if(d<dmin && 0){
|
2002-03-28 06:25:35 +02:00
|
|
|
flush_put_bits(&s->pb);
|
|
|
|
dmin=d;
|
2002-04-17 21:19:50 +03:00
|
|
|
copy_context_after_encode(&best_s, s, MB_TYPE_INTER4V);
|
2002-03-28 06:25:35 +02:00
|
|
|
best=2;
|
|
|
|
}
|
|
|
|
}
|
2002-03-27 23:25:22 +02:00
|
|
|
if(mb_type&MB_TYPE_INTRA){
|
2002-04-17 21:19:50 +03:00
|
|
|
copy_context_before_encode(s, &backup_s, MB_TYPE_INTRA);
|
2002-03-28 06:25:35 +02:00
|
|
|
s->mv_type = MV_TYPE_16X16;
|
2002-03-27 23:25:22 +02:00
|
|
|
s->mb_intra= 1;
|
|
|
|
s->mv[0][0][0] = 0;
|
|
|
|
s->mv[0][0][1] = 0;
|
|
|
|
init_put_bits(&s->pb, bit_buf[0], 3000, NULL, NULL);
|
|
|
|
s->block= s->intra_block;
|
|
|
|
|
2002-04-17 07:32:12 +03:00
|
|
|
encode_mb(s, 0, 0);
|
2002-03-27 23:25:22 +02:00
|
|
|
d= get_bit_count(&s->pb);
|
|
|
|
if(d<dmin){
|
|
|
|
flush_put_bits(&s->pb);
|
|
|
|
dmin=d;
|
2002-04-17 21:19:50 +03:00
|
|
|
copy_context_after_encode(&best_s, s, MB_TYPE_INTRA);
|
2002-03-27 23:25:22 +02:00
|
|
|
best=0;
|
|
|
|
}
|
2002-04-17 21:19:50 +03: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 06:25:35 +02:00
|
|
|
}
|
2002-04-17 21:19:50 +03:00
|
|
|
copy_context_after_encode(s, &best_s, -1);
|
2002-03-27 23:25:22 +02:00
|
|
|
copy_bits(&pb, bit_buf[best], dmin);
|
|
|
|
s->pb= pb;
|
2001-07-22 17:18:56 +03:00
|
|
|
} else {
|
2002-04-17 07:32:12 +03:00
|
|
|
int motion_x, motion_y;
|
|
|
|
s->mv_type=MV_TYPE_16X16;
|
2002-03-27 23:25:22 +02:00
|
|
|
// only one MB-Type possible
|
2002-04-17 07:32:12 +03:00
|
|
|
//FIXME convert to swicth()
|
2002-03-27 23:25:22 +02:00
|
|
|
if(mb_type&MB_TYPE_INTRA){
|
2002-04-17 07:32:12 +03:00
|
|
|
s->mv_dir = MV_DIR_FORWARD;
|
2002-03-27 23:25:22 +02:00
|
|
|
s->mb_intra= 1;
|
2002-04-17 07:32:12 +03:00
|
|
|
motion_x= s->mv[0][0][0] = 0;
|
|
|
|
motion_y= s->mv[0][0][1] = 0;
|
|
|
|
}else if(mb_type&MB_TYPE_INTER){
|
|
|
|
int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
|
|
|
|
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];
|
|
|
|
}else if(mb_type&MB_TYPE_DIRECT){
|
|
|
|
int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
|
|
|
|
s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
|
|
|
|
s->mb_intra= 0;
|
|
|
|
motion_x=0;
|
|
|
|
motion_y=0;
|
2002-03-27 23:25:22 +02:00
|
|
|
s->mv[0][0][0] = 0;
|
|
|
|
s->mv[0][0][1] = 0;
|
2002-04-17 07:32:12 +03:00
|
|
|
s->mv[1][0][0] = 0;
|
|
|
|
s->mv[1][0][1] = 0;
|
|
|
|
}else if(mb_type&MB_TYPE_BIDIR){
|
|
|
|
int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
|
|
|
|
s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
|
2002-03-27 23:25:22 +02:00
|
|
|
s->mb_intra= 0;
|
2002-04-17 07:32:12 +03: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];
|
|
|
|
}else if(mb_type&MB_TYPE_BACKWARD){
|
|
|
|
int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
|
|
|
|
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];
|
|
|
|
}else if(mb_type&MB_TYPE_FORWARD){
|
|
|
|
int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
|
|
|
|
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);
|
|
|
|
}else{
|
|
|
|
motion_x=motion_y=0; //gcc warning fix
|
|
|
|
printf("illegal MB type\n");
|
2002-03-27 23:25:22 +02:00
|
|
|
}
|
2002-04-17 07:32:12 +03:00
|
|
|
encode_mb(s, motion_x, motion_y);
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
|
|
|
|
2001-07-31 02:26:26 +03:00
|
|
|
MPV_decode_mb(s, s->block);
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
2002-02-12 17:43:16 +02:00
|
|
|
|
|
|
|
|
2002-02-09 18:59:56 +02:00
|
|
|
/* Obtain average GOB size for RTP */
|
|
|
|
if (s->rtp_mode) {
|
|
|
|
if (!mb_y)
|
2002-02-12 17:43:16 +02:00
|
|
|
s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line;
|
2002-02-09 18:59:56 +02:00
|
|
|
else if (!(mb_y % s->gob_index)) {
|
2002-02-12 17:43:16 +02: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 18:59:56 +02: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);
|
|
|
|
s->first_gob_line = 0;
|
|
|
|
}
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
2002-03-27 23:25:22 +02:00
|
|
|
emms_c();
|
2002-03-23 19:43:30 +02:00
|
|
|
|
2002-04-07 01:29:37 +03:00
|
|
|
if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
|
2002-01-16 00:22:41 +02:00
|
|
|
msmpeg4_encode_ext_header(s);
|
|
|
|
|
2001-11-19 04:13:14 +02:00
|
|
|
//if (s->gob_number)
|
|
|
|
// fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
|
2002-02-09 18:59:56 +02:00
|
|
|
|
|
|
|
/* Send the last GOB if RTP */
|
|
|
|
if (s->rtp_mode) {
|
|
|
|
flush_put_bits(&s->pb);
|
2002-02-12 17:43:16 +02:00
|
|
|
pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
|
2002-02-09 18:59:56 +02: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 17:43:16 +02:00
|
|
|
s->ptr_lastgob = pbBufPtr(&s->pb);
|
2002-02-09 18:59:56 +02:00
|
|
|
//fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
|
|
|
|
}
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
|
|
|
|
2002-01-27 15:30:18 +02:00
|
|
|
static int dct_quantize_c(MpegEncContext *s,
|
2001-07-22 17:18:56 +03:00
|
|
|
DCTELEM *block, int n,
|
|
|
|
int qscale)
|
|
|
|
{
|
|
|
|
int i, j, level, last_non_zero, q;
|
|
|
|
const int *qmat;
|
2002-01-24 16:34:03 +02:00
|
|
|
int minLevel, maxLevel;
|
|
|
|
|
|
|
|
if(s->avctx!=NULL && s->avctx->codec->id==CODEC_ID_MPEG4){
|
|
|
|
/* mpeg4 */
|
|
|
|
minLevel= -2048;
|
|
|
|
maxLevel= 2047;
|
|
|
|
}else if(s->out_format==FMT_MPEG1){
|
|
|
|
/* mpeg1 */
|
|
|
|
minLevel= -255;
|
|
|
|
maxLevel= 255;
|
2002-02-09 03:25:06 +02:00
|
|
|
}else if(s->out_format==FMT_MJPEG){
|
|
|
|
/* (m)jpeg */
|
|
|
|
minLevel= -1023;
|
|
|
|
maxLevel= 1023;
|
2002-01-24 16:34:03 +02:00
|
|
|
}else{
|
|
|
|
/* h263 / msmpeg4 */
|
|
|
|
minLevel= -128;
|
|
|
|
maxLevel= 127;
|
|
|
|
}
|
2001-07-22 17:18:56 +03:00
|
|
|
|
|
|
|
av_fdct (block);
|
|
|
|
|
2001-08-14 00:48:05 +03:00
|
|
|
/* we need this permutation so that we correct the IDCT
|
|
|
|
permutation. will be moved into DCT code */
|
|
|
|
block_permute(block);
|
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
if (s->mb_intra) {
|
|
|
|
if (n < 4)
|
|
|
|
q = s->y_dc_scale;
|
|
|
|
else
|
|
|
|
q = s->c_dc_scale;
|
|
|
|
q = q << 3;
|
|
|
|
|
|
|
|
/* note: block[0] is assumed to be positive */
|
|
|
|
block[0] = (block[0] + (q >> 1)) / q;
|
|
|
|
i = 1;
|
|
|
|
last_non_zero = 0;
|
|
|
|
if (s->out_format == FMT_H263) {
|
|
|
|
qmat = s->q_non_intra_matrix;
|
|
|
|
} else {
|
|
|
|
qmat = s->q_intra_matrix;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
i = 0;
|
|
|
|
last_non_zero = -1;
|
|
|
|
qmat = s->q_non_intra_matrix;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(;i<64;i++) {
|
|
|
|
j = zigzag_direct[i];
|
|
|
|
level = block[j];
|
|
|
|
level = level * qmat[j];
|
|
|
|
#ifdef PARANOID
|
|
|
|
{
|
|
|
|
static int count = 0;
|
|
|
|
int level1, level2, qmat1;
|
|
|
|
double val;
|
|
|
|
if (qmat == s->q_non_intra_matrix) {
|
|
|
|
qmat1 = default_non_intra_matrix[j] * s->qscale;
|
|
|
|
} else {
|
|
|
|
qmat1 = default_intra_matrix[j] * s->qscale;
|
|
|
|
}
|
|
|
|
if (av_fdct != jpeg_fdct_ifast)
|
|
|
|
val = ((double)block[j] * 8.0) / (double)qmat1;
|
|
|
|
else
|
|
|
|
val = ((double)block[j] * 8.0 * 2048.0) /
|
|
|
|
((double)qmat1 * aanscales[j]);
|
|
|
|
level1 = (int)val;
|
|
|
|
level2 = level / (1 << (QMAT_SHIFT - 3));
|
|
|
|
if (level1 != level2) {
|
|
|
|
fprintf(stderr, "%d: quant error qlevel=%d wanted=%d level=%d qmat1=%d qmat=%d wantedf=%0.6f\n",
|
|
|
|
count, level2, level1, block[j], qmat1, qmat[j],
|
|
|
|
val);
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* XXX: slight error for the low range. Test should be equivalent to
|
|
|
|
(level <= -(1 << (QMAT_SHIFT - 3)) || level >= (1 <<
|
|
|
|
(QMAT_SHIFT - 3)))
|
|
|
|
*/
|
|
|
|
if (((level << (31 - (QMAT_SHIFT - 3))) >> (31 - (QMAT_SHIFT - 3))) !=
|
|
|
|
level) {
|
|
|
|
level = level / (1 << (QMAT_SHIFT - 3));
|
|
|
|
/* XXX: currently, this code is not optimal. the range should be:
|
|
|
|
mpeg1: -255..255
|
|
|
|
mpeg2: -2048..2047
|
|
|
|
h263: -128..127
|
|
|
|
mpeg4: -2048..2047
|
|
|
|
*/
|
2002-01-24 16:34:03 +02:00
|
|
|
if (level > maxLevel)
|
|
|
|
level = maxLevel;
|
|
|
|
else if (level < minLevel)
|
|
|
|
level = minLevel;
|
2001-07-22 17:18:56 +03:00
|
|
|
|
|
|
|
block[j] = level;
|
|
|
|
last_non_zero = i;
|
|
|
|
} else {
|
|
|
|
block[j] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return last_non_zero;
|
|
|
|
}
|
|
|
|
|
2001-07-31 02:26:26 +03:00
|
|
|
static void dct_unquantize_mpeg1_c(MpegEncContext *s,
|
|
|
|
DCTELEM *block, int n, int qscale)
|
2001-07-22 17:18:56 +03:00
|
|
|
{
|
2002-01-13 06:59:37 +02:00
|
|
|
int i, level, nCoeffs;
|
2001-07-22 17:18:56 +03:00
|
|
|
const UINT16 *quant_matrix;
|
|
|
|
|
2002-01-13 06:59:37 +02:00
|
|
|
if(s->alternate_scan) nCoeffs= 64;
|
|
|
|
else nCoeffs= s->block_last_index[n]+1;
|
|
|
|
|
2001-07-22 17:18:56 +03: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-01-13 06:59:37 +02:00
|
|
|
for(i=1;i<nCoeffs;i++) {
|
|
|
|
int j= zigzag_direct[i];
|
|
|
|
level = block[j];
|
2001-07-22 17:18:56 +03:00
|
|
|
if (level) {
|
|
|
|
if (level < 0) {
|
|
|
|
level = -level;
|
2002-01-13 06:59:37 +02:00
|
|
|
level = (int)(level * qscale * quant_matrix[j]) >> 3;
|
2001-07-22 17:18:56 +03:00
|
|
|
level = (level - 1) | 1;
|
|
|
|
level = -level;
|
|
|
|
} else {
|
2002-01-13 06:59:37 +02:00
|
|
|
level = (int)(level * qscale * quant_matrix[j]) >> 3;
|
2001-07-22 17:18:56 +03: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 06:59:37 +02:00
|
|
|
block[j] = level;
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
i = 0;
|
|
|
|
quant_matrix = s->non_intra_matrix;
|
2002-01-24 17:25:11 +02:00
|
|
|
for(;i<nCoeffs;i++) {
|
2002-01-13 06:59:37 +02:00
|
|
|
int j= zigzag_direct[i];
|
|
|
|
level = block[j];
|
2001-07-22 17:18:56 +03:00
|
|
|
if (level) {
|
|
|
|
if (level < 0) {
|
|
|
|
level = -level;
|
|
|
|
level = (((level << 1) + 1) * qscale *
|
2002-01-13 06:59:37 +02:00
|
|
|
((int) (quant_matrix[j]))) >> 4;
|
2001-07-22 17:18:56 +03:00
|
|
|
level = (level - 1) | 1;
|
|
|
|
level = -level;
|
|
|
|
} else {
|
|
|
|
level = (((level << 1) + 1) * qscale *
|
2002-01-13 06:59:37 +02:00
|
|
|
((int) (quant_matrix[j]))) >> 4;
|
2001-07-22 17:18:56 +03: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 06:59:37 +02:00
|
|
|
block[j] = level;
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-07-31 02:26:26 +03:00
|
|
|
|
2002-04-17 19:30:31 +03:00
|
|
|
static void dct_unquantize_mpeg2_c(MpegEncContext *s,
|
|
|
|
DCTELEM *block, int n, int qscale)
|
|
|
|
{
|
|
|
|
int i, level, nCoeffs;
|
|
|
|
const UINT16 *quant_matrix;
|
|
|
|
|
|
|
|
if(s->alternate_scan) nCoeffs= 64;
|
|
|
|
else nCoeffs= s->block_last_index[n]+1;
|
|
|
|
|
|
|
|
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;
|
|
|
|
for(i=1;i<nCoeffs;i++) {
|
|
|
|
int j= zigzag_direct[i];
|
|
|
|
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;
|
|
|
|
quant_matrix = s->non_intra_matrix;
|
|
|
|
for(;i<nCoeffs;i++) {
|
|
|
|
int j= zigzag_direct[i];
|
|
|
|
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 02:26:26 +03:00
|
|
|
static void dct_unquantize_h263_c(MpegEncContext *s,
|
|
|
|
DCTELEM *block, int n, int qscale)
|
|
|
|
{
|
|
|
|
int i, level, qmul, qadd;
|
2002-01-13 06:59:37 +02:00
|
|
|
int nCoeffs;
|
2002-02-19 21:23:34 +02:00
|
|
|
|
2001-07-31 02:26:26 +03:00
|
|
|
if (s->mb_intra) {
|
2002-02-19 21:23:34 +02: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;
|
|
|
|
}
|
2001-07-31 02:26:26 +03:00
|
|
|
i = 1;
|
2002-01-13 06:59:37 +02:00
|
|
|
nCoeffs= 64; //does not allways use zigzag table
|
2001-07-31 02:26:26 +03:00
|
|
|
} else {
|
|
|
|
i = 0;
|
2002-01-13 06:59:37 +02:00
|
|
|
nCoeffs= zigzag_end[ s->block_last_index[n] ];
|
2001-07-31 02:26:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
qmul = s->qscale << 1;
|
2002-02-18 21:33:27 +02:00
|
|
|
if (s->h263_aic && s->mb_intra)
|
|
|
|
qadd = 0;
|
|
|
|
else
|
|
|
|
qadd = (s->qscale - 1) | 1;
|
2001-07-31 02:26:26 +03:00
|
|
|
|
2002-01-13 06:59:37 +02:00
|
|
|
for(;i<nCoeffs;i++) {
|
2001-07-31 02:26:26 +03: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 17:18:56 +03:00
|
|
|
|
|
|
|
/* rate control */
|
|
|
|
|
|
|
|
static void rate_control_init(MpegEncContext *s)
|
|
|
|
{
|
2002-03-17 18:31:38 +02:00
|
|
|
emms_c();
|
|
|
|
|
|
|
|
//initial values, they dont really matter as they will be totally different within a few frames
|
|
|
|
s->i_pred.coeff= s->p_pred.coeff= 7.0;
|
|
|
|
s->i_pred.count= s->p_pred.count= 1.0;
|
|
|
|
|
|
|
|
s->i_pred.decay= s->p_pred.decay= 0.4;
|
|
|
|
|
|
|
|
// use more bits at the beginning, otherwise high motion at the begin will look like shit
|
|
|
|
s->qsum=100;
|
|
|
|
s->qcount=100;
|
|
|
|
|
|
|
|
s->short_term_qsum=0.001;
|
|
|
|
s->short_term_qcount=0.001;
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
|
|
|
|
2002-03-17 18:31:38 +02:00
|
|
|
static double predict(Predictor *p, double q, double var)
|
|
|
|
{
|
|
|
|
return p->coeff*var / (q*p->count);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void update_predictor(Predictor *p, double q, double var, double size)
|
|
|
|
{
|
|
|
|
double new_coeff= size*q / (var + 1);
|
|
|
|
if(var<1000) return;
|
|
|
|
/*{
|
|
|
|
int pred= predict(p, q, var);
|
|
|
|
int error= abs(pred-size);
|
|
|
|
static double sum=0;
|
|
|
|
static int count=0;
|
|
|
|
if(count>5) sum+=error;
|
|
|
|
count++;
|
|
|
|
if(256*256*256*64%count==0){
|
|
|
|
printf("%d %f %f\n", count, sum/count, p->coeff);
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
p->count*= p->decay;
|
|
|
|
p->coeff*= p->decay;
|
|
|
|
p->count++;
|
|
|
|
p->coeff+= new_coeff;
|
|
|
|
}
|
2001-07-22 17:18:56 +03:00
|
|
|
|
|
|
|
static int rate_estimate_qscale(MpegEncContext *s)
|
|
|
|
{
|
2002-03-17 18:31:38 +02:00
|
|
|
int qmin= s->qmin;
|
|
|
|
int qmax= s->qmax;
|
|
|
|
int rate_q=5;
|
2001-07-22 17:18:56 +03:00
|
|
|
float q;
|
2002-03-17 18:31:38 +02:00
|
|
|
int qscale;
|
|
|
|
float br_compensation;
|
|
|
|
double diff;
|
|
|
|
double short_term_q;
|
|
|
|
double long_term_q;
|
|
|
|
int last_qscale= s->qscale;
|
|
|
|
double fps;
|
|
|
|
INT64 wanted_bits;
|
|
|
|
emms_c();
|
2002-03-29 00:50:22 +02:00
|
|
|
|
2002-03-17 18:31:38 +02:00
|
|
|
fps= (double)s->frame_rate / FRAME_RATE_BASE;
|
|
|
|
wanted_bits= s->bit_rate*(double)s->picture_number/fps;
|
|
|
|
|
|
|
|
|
|
|
|
if(s->picture_number>2){
|
|
|
|
/* update predictors */
|
|
|
|
if(s->last_pict_type == I_TYPE){
|
|
|
|
//FIXME
|
|
|
|
}else{ //P Frame
|
|
|
|
//printf("%d %d %d %f\n", s->qscale, s->last_mc_mb_var, s->frame_bits, s->p_pred.coeff);
|
|
|
|
update_predictor(&s->p_pred, s->qscale, s->last_mc_mb_var, s->frame_bits);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(s->pict_type == I_TYPE){
|
|
|
|
//FIXME
|
|
|
|
rate_q= s->qsum/s->qcount;
|
|
|
|
}else{ //P Frame
|
|
|
|
int i;
|
|
|
|
int diff, best_diff=1000000000;
|
|
|
|
for(i=1; i<=31; i++){
|
|
|
|
diff= predict(&s->p_pred, i, s->mc_mb_var) - (double)s->bit_rate/fps;
|
|
|
|
if(diff<0) diff= -diff;
|
|
|
|
if(diff<best_diff){
|
|
|
|
best_diff= diff;
|
|
|
|
rate_q= i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s->short_term_qsum*=s->qblur;
|
|
|
|
s->short_term_qcount*=s->qblur;
|
|
|
|
|
|
|
|
s->short_term_qsum+= rate_q;
|
|
|
|
s->short_term_qcount++;
|
|
|
|
short_term_q= s->short_term_qsum/s->short_term_qcount;
|
|
|
|
|
2002-03-30 23:33:14 +02:00
|
|
|
long_term_q= s->qsum/s->qcount*(s->total_bits+1)/(wanted_bits+1); //+1 to avoid nan & 0
|
2002-03-17 18:31:38 +02:00
|
|
|
|
|
|
|
// q= (long_term_q - short_term_q)*s->qcompress + short_term_q;
|
|
|
|
q= 1/((1/long_term_q - 1/short_term_q)*s->qcompress + 1/short_term_q);
|
|
|
|
|
|
|
|
diff= s->total_bits - wanted_bits;
|
|
|
|
br_compensation= (s->bit_rate_tolerance - diff)/s->bit_rate_tolerance;
|
2002-03-17 20:08:11 +02:00
|
|
|
if(br_compensation<=0.0) br_compensation=0.001;
|
2002-03-17 18:31:38 +02:00
|
|
|
q/=br_compensation;
|
|
|
|
|
|
|
|
qscale= (int)(q + 0.5);
|
|
|
|
if (qscale<qmin) qscale=qmin;
|
|
|
|
else if(qscale>qmax) qscale=qmax;
|
|
|
|
|
|
|
|
if (qscale<last_qscale-s->max_qdiff) qscale=last_qscale-s->max_qdiff;
|
|
|
|
else if(qscale>last_qscale+s->max_qdiff) qscale=last_qscale+s->max_qdiff;
|
|
|
|
|
|
|
|
s->qsum+= qscale;
|
|
|
|
s->qcount++;
|
2001-07-22 17:18:56 +03:00
|
|
|
|
2002-03-17 18:31:38 +02:00
|
|
|
s->last_pict_type= s->pict_type;
|
2002-03-30 23:33:14 +02:00
|
|
|
//printf("q:%d diff:%d comp:%f rate_q:%d st_q:%f fvar:%d last_size:%d\n", qscale, (int)diff, br_compensation,
|
|
|
|
// rate_q, short_term_q, s->mc_mb_var, s->frame_bits);
|
2002-03-17 18:31:38 +02:00
|
|
|
//printf("%d %d\n", s->bit_rate, (int)fps);
|
|
|
|
return qscale;
|
2001-07-22 17:18:56 +03: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 16:12:23 +03:00
|
|
|
AVCodec mpeg4_encoder = {
|
|
|
|
"mpeg4",
|
2001-07-22 17:18:56 +03:00
|
|
|
CODEC_TYPE_VIDEO,
|
2001-08-15 16:12:23 +03:00
|
|
|
CODEC_ID_MPEG4,
|
2001-07-22 17:18:56 +03:00
|
|
|
sizeof(MpegEncContext),
|
|
|
|
MPV_encode_init,
|
|
|
|
MPV_encode_picture,
|
|
|
|
MPV_encode_end,
|
|
|
|
};
|
|
|
|
|
2002-04-05 07:09:04 +03: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 17:18:56 +03:00
|
|
|
"msmpeg4",
|
|
|
|
CODEC_TYPE_VIDEO,
|
2002-04-05 07:09:04 +03:00
|
|
|
CODEC_ID_MSMPEG4V3,
|
2001-07-22 17:18:56 +03:00
|
|
|
sizeof(MpegEncContext),
|
|
|
|
MPV_encode_init,
|
|
|
|
MPV_encode_picture,
|
|
|
|
MPV_encode_end,
|
|
|
|
};
|