1cabdff1aSopenharmony_ci/*
2cabdff1aSopenharmony_ci * Motion estimation
3cabdff1aSopenharmony_ci *
4cabdff1aSopenharmony_ci * This file is part of FFmpeg.
5cabdff1aSopenharmony_ci *
6cabdff1aSopenharmony_ci * FFmpeg is free software; you can redistribute it and/or
7cabdff1aSopenharmony_ci * modify it under the terms of the GNU Lesser General Public
8cabdff1aSopenharmony_ci * License as published by the Free Software Foundation; either
9cabdff1aSopenharmony_ci * version 2.1 of the License, or (at your option) any later version.
10cabdff1aSopenharmony_ci *
11cabdff1aSopenharmony_ci * FFmpeg is distributed in the hope that it will be useful,
12cabdff1aSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
13cabdff1aSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14cabdff1aSopenharmony_ci * Lesser General Public License for more details.
15cabdff1aSopenharmony_ci *
16cabdff1aSopenharmony_ci * You should have received a copy of the GNU Lesser General Public
17cabdff1aSopenharmony_ci * License along with FFmpeg; if not, write to the Free Software
18cabdff1aSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19cabdff1aSopenharmony_ci */
20cabdff1aSopenharmony_ci
21cabdff1aSopenharmony_ci#ifndef AVCODEC_MOTION_EST_H
22cabdff1aSopenharmony_ci#define AVCODEC_MOTION_EST_H
23cabdff1aSopenharmony_ci
24cabdff1aSopenharmony_ci#include <stdint.h>
25cabdff1aSopenharmony_ci
26cabdff1aSopenharmony_ci#include "avcodec.h"
27cabdff1aSopenharmony_ci#include "hpeldsp.h"
28cabdff1aSopenharmony_ci#include "qpeldsp.h"
29cabdff1aSopenharmony_ci
30cabdff1aSopenharmony_cistruct MpegEncContext;
31cabdff1aSopenharmony_ci
32cabdff1aSopenharmony_ci#if ARCH_IA64 // Limit static arrays to avoid gcc failing "short data segment overflowed"
33cabdff1aSopenharmony_ci#define MAX_MV 1024
34cabdff1aSopenharmony_ci#else
35cabdff1aSopenharmony_ci#define MAX_MV 4096
36cabdff1aSopenharmony_ci#endif
37cabdff1aSopenharmony_ci#define MAX_DMV (2*MAX_MV)
38cabdff1aSopenharmony_ci#define ME_MAP_SIZE 64
39cabdff1aSopenharmony_ci
40cabdff1aSopenharmony_ci#define FF_ME_ZERO 0
41cabdff1aSopenharmony_ci#define FF_ME_EPZS 1
42cabdff1aSopenharmony_ci#define FF_ME_XONE 2
43cabdff1aSopenharmony_ci
44cabdff1aSopenharmony_ci/**
45cabdff1aSopenharmony_ci * Motion estimation context.
46cabdff1aSopenharmony_ci */
47cabdff1aSopenharmony_citypedef struct MotionEstContext {
48cabdff1aSopenharmony_ci    AVCodecContext *avctx;
49cabdff1aSopenharmony_ci    int skip;                       ///< set if ME is skipped for the current MB
50cabdff1aSopenharmony_ci    int co_located_mv[4][2];        ///< mv from last P-frame for direct mode ME
51cabdff1aSopenharmony_ci    int direct_basis_mv[4][2];
52cabdff1aSopenharmony_ci    uint8_t *scratchpad;            /**< data area for the ME algo, so that
53cabdff1aSopenharmony_ci                                     * the ME does not need to malloc/free. */
54cabdff1aSopenharmony_ci    uint8_t *best_mb;
55cabdff1aSopenharmony_ci    uint8_t *temp_mb[2];
56cabdff1aSopenharmony_ci    uint8_t *temp;
57cabdff1aSopenharmony_ci    int best_bits;
58cabdff1aSopenharmony_ci    uint32_t *map;                  ///< map to avoid duplicate evaluations
59cabdff1aSopenharmony_ci    uint32_t *score_map;            ///< map to store the scores
60cabdff1aSopenharmony_ci    unsigned map_generation;
61cabdff1aSopenharmony_ci    int pre_penalty_factor;
62cabdff1aSopenharmony_ci    int penalty_factor;             /**< an estimate of the bits required to
63cabdff1aSopenharmony_ci                                     * code a given mv value, e.g. (1,0) takes
64cabdff1aSopenharmony_ci                                     * more bits than (0,0). We have to
65cabdff1aSopenharmony_ci                                     * estimate whether any reduction in
66cabdff1aSopenharmony_ci                                     * residual is worth the extra bits. */
67cabdff1aSopenharmony_ci    int sub_penalty_factor;
68cabdff1aSopenharmony_ci    int mb_penalty_factor;
69cabdff1aSopenharmony_ci    int flags;
70cabdff1aSopenharmony_ci    int sub_flags;
71cabdff1aSopenharmony_ci    int mb_flags;
72cabdff1aSopenharmony_ci    int pre_pass;                   ///< = 1 for the pre pass
73cabdff1aSopenharmony_ci    int dia_size;
74cabdff1aSopenharmony_ci    int xmin;
75cabdff1aSopenharmony_ci    int xmax;
76cabdff1aSopenharmony_ci    int ymin;
77cabdff1aSopenharmony_ci    int ymax;
78cabdff1aSopenharmony_ci    int pred_x;
79cabdff1aSopenharmony_ci    int pred_y;
80cabdff1aSopenharmony_ci    uint8_t *src[4][4];
81cabdff1aSopenharmony_ci    uint8_t *ref[4][4];
82cabdff1aSopenharmony_ci    int stride;
83cabdff1aSopenharmony_ci    int uvstride;
84cabdff1aSopenharmony_ci    /* temp variables for picture complexity calculation */
85cabdff1aSopenharmony_ci    int64_t mc_mb_var_sum_temp;
86cabdff1aSopenharmony_ci    int64_t mb_var_sum_temp;
87cabdff1aSopenharmony_ci    int scene_change_score;
88cabdff1aSopenharmony_ci
89cabdff1aSopenharmony_ci    op_pixels_func(*hpel_put)[4];
90cabdff1aSopenharmony_ci    op_pixels_func(*hpel_avg)[4];
91cabdff1aSopenharmony_ci    qpel_mc_func(*qpel_put)[16];
92cabdff1aSopenharmony_ci    qpel_mc_func(*qpel_avg)[16];
93cabdff1aSopenharmony_ci    const uint8_t (*mv_penalty)[MAX_DMV * 2 + 1]; ///< bit amount needed to encode a MV
94cabdff1aSopenharmony_ci    const uint8_t *current_mv_penalty;
95cabdff1aSopenharmony_ci    int (*sub_motion_search)(struct MpegEncContext *s,
96cabdff1aSopenharmony_ci                             int *mx_ptr, int *my_ptr, int dmin,
97cabdff1aSopenharmony_ci                             int src_index, int ref_index,
98cabdff1aSopenharmony_ci                             int size, int h);
99cabdff1aSopenharmony_ci} MotionEstContext;
100cabdff1aSopenharmony_ci
101cabdff1aSopenharmony_cistatic inline int ff_h263_round_chroma(int x)
102cabdff1aSopenharmony_ci{
103cabdff1aSopenharmony_ci    //FIXME static or not?
104cabdff1aSopenharmony_ci    static const uint8_t h263_chroma_roundtab[16] = {
105cabdff1aSopenharmony_ci    //  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
106cabdff1aSopenharmony_ci        0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1,
107cabdff1aSopenharmony_ci    };
108cabdff1aSopenharmony_ci    return h263_chroma_roundtab[x & 0xf] + (x >> 3);
109cabdff1aSopenharmony_ci}
110cabdff1aSopenharmony_ci
111cabdff1aSopenharmony_ciint ff_init_me(struct MpegEncContext *s);
112cabdff1aSopenharmony_ci
113cabdff1aSopenharmony_civoid ff_estimate_p_frame_motion(struct MpegEncContext *s, int mb_x, int mb_y);
114cabdff1aSopenharmony_civoid ff_estimate_b_frame_motion(struct MpegEncContext *s, int mb_x, int mb_y);
115cabdff1aSopenharmony_ci
116cabdff1aSopenharmony_ciint ff_pre_estimate_p_frame_motion(struct MpegEncContext *s,
117cabdff1aSopenharmony_ci                                   int mb_x, int mb_y);
118cabdff1aSopenharmony_ci
119cabdff1aSopenharmony_ciint ff_epzs_motion_search(struct MpegEncContext *s, int *mx_ptr, int *my_ptr,
120cabdff1aSopenharmony_ci                          int P[10][2], int src_index, int ref_index,
121cabdff1aSopenharmony_ci                          const int16_t (*last_mv)[2], int ref_mv_scale,
122cabdff1aSopenharmony_ci                          int size, int h);
123cabdff1aSopenharmony_ci
124cabdff1aSopenharmony_ciint ff_get_mb_score(struct MpegEncContext *s, int mx, int my, int src_index,
125cabdff1aSopenharmony_ci                    int ref_index, int size, int h, int add_rate);
126cabdff1aSopenharmony_ci
127cabdff1aSopenharmony_ciint ff_get_best_fcode(struct MpegEncContext *s,
128cabdff1aSopenharmony_ci                      const int16_t (*mv_table)[2], int type);
129cabdff1aSopenharmony_ci
130cabdff1aSopenharmony_civoid ff_fix_long_p_mvs(struct MpegEncContext *s, int type);
131cabdff1aSopenharmony_civoid ff_fix_long_mvs(struct MpegEncContext *s, uint8_t *field_select_table,
132cabdff1aSopenharmony_ci                     int field_select, int16_t (*mv_table)[2], int f_code,
133cabdff1aSopenharmony_ci                     int type, int truncate);
134cabdff1aSopenharmony_ci
135cabdff1aSopenharmony_ci#endif /* AVCODEC_MOTION_EST_H */
136