/src/ffmpeg/libavcodec/mpeg4video.c
Line | Count | Source |
1 | | /* |
2 | | * MPEG-4 decoder / encoder common code |
3 | | * Copyright (c) 2000,2001 Fabrice Bellard |
4 | | * Copyright (c) 2002-2010 Michael Niedermayer <michaelni@gmx.at> |
5 | | * |
6 | | * This file is part of FFmpeg. |
7 | | * |
8 | | * FFmpeg is free software; you can redistribute it and/or |
9 | | * modify it under the terms of the GNU Lesser General Public |
10 | | * License as published by the Free Software Foundation; either |
11 | | * version 2.1 of the License, or (at your option) any later version. |
12 | | * |
13 | | * FFmpeg is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | | * Lesser General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public |
19 | | * License along with FFmpeg; if not, write to the Free Software |
20 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21 | | */ |
22 | | |
23 | | #include "mpegutils.h" |
24 | | #include "mpegvideo.h" |
25 | | #include "mpeg4video.h" |
26 | | #include "mpeg4data.h" |
27 | | |
28 | | int ff_mpeg4_get_video_packet_prefix_length(enum AVPictureType pict_type, |
29 | | int f_code, int b_code) |
30 | 1.46M | { |
31 | 1.46M | switch (pict_type) { |
32 | 107k | case AV_PICTURE_TYPE_I: |
33 | 107k | return 16; |
34 | 52.7k | case AV_PICTURE_TYPE_P: |
35 | 1.33M | case AV_PICTURE_TYPE_S: |
36 | 1.33M | return f_code + 15; |
37 | 13.4k | case AV_PICTURE_TYPE_B: |
38 | 13.4k | return FFMAX3(f_code, b_code, 2) + 15; |
39 | 0 | default: |
40 | 0 | return -1; |
41 | 1.46M | } |
42 | 1.46M | } |
43 | | |
44 | | void ff_mpeg4_clean_buffers(MpegEncContext *s) |
45 | 387k | { |
46 | 387k | const int mb_height = s->mb_height; |
47 | 387k | int c_wrap, l_wrap, l_xy; |
48 | | |
49 | 387k | l_wrap = s->b8_stride; |
50 | 387k | l_xy = (2 * s->mb_y - 1) * l_wrap + s->mb_x * 2 - 1; |
51 | 387k | c_wrap = s->mb_stride; |
52 | 387k | int u_xy = 2 * mb_height * l_wrap + s->mb_y * c_wrap + s->mb_x - 1; |
53 | 387k | int v_xy = u_xy + c_wrap * (mb_height + 1); |
54 | 387k | int16_t (*ac_val)[16] = s->ac_val; |
55 | | |
56 | | /* clean AC */ |
57 | 387k | memset(ac_val + l_xy, 0, (l_wrap * 2 + 1) * sizeof(*ac_val)); |
58 | 387k | memset(ac_val + u_xy, 0, (c_wrap + 1) * sizeof(*ac_val)); |
59 | 387k | memset(ac_val + v_xy, 0, (c_wrap + 1) * sizeof(*ac_val)); |
60 | | |
61 | | /* clean MV */ |
62 | | // we can't clear the MVs as they might be needed by a B-frame |
63 | 387k | s->last_mv[0][0][0] = |
64 | 387k | s->last_mv[0][0][1] = |
65 | 387k | s->last_mv[1][0][0] = |
66 | 387k | s->last_mv[1][0][1] = 0; |
67 | 387k | } |
68 | | |
69 | 14.6M | #define tab_size ((signed)FF_ARRAY_ELEMS(s->direct_scale_mv[0])) |
70 | 9.75M | #define tab_bias (tab_size / 2) |
71 | | |
72 | | // used by MPEG-4 and rv10 decoder |
73 | | void ff_mpeg4_init_direct_mv(MpegEncContext *s) |
74 | 73.3k | { |
75 | 73.3k | int i; |
76 | 4.77M | for (i = 0; i < tab_size; i++) { |
77 | 4.69M | s->direct_scale_mv[0][i] = (i - tab_bias) * s->pb_time / s->pp_time; |
78 | 4.69M | s->direct_scale_mv[1][i] = (i - tab_bias) * (s->pb_time - s->pp_time) / |
79 | 4.69M | s->pp_time; |
80 | 4.69M | } |
81 | 73.3k | } |
82 | | |
83 | | static inline void ff_mpeg4_set_one_direct_mv(MpegEncContext *s, int mx, |
84 | | int my, int i) |
85 | 71.6k | { |
86 | 71.6k | int xy = s->block_index[i]; |
87 | 71.6k | uint16_t time_pp = s->pp_time; |
88 | 71.6k | uint16_t time_pb = s->pb_time; |
89 | 71.6k | int p_mx, p_my; |
90 | | |
91 | 71.6k | p_mx = s->next_pic.motion_val[0][xy][0]; |
92 | 71.6k | if ((unsigned)(p_mx + tab_bias) < tab_size) { |
93 | 57.9k | s->mv[0][i][0] = s->direct_scale_mv[0][p_mx + tab_bias] + mx; |
94 | 57.9k | s->mv[1][i][0] = mx ? s->mv[0][i][0] - p_mx |
95 | 57.9k | : s->direct_scale_mv[1][p_mx + tab_bias]; |
96 | 57.9k | } else { |
97 | 13.6k | s->mv[0][i][0] = p_mx * time_pb / time_pp + mx; |
98 | 13.6k | s->mv[1][i][0] = mx ? s->mv[0][i][0] - p_mx |
99 | 13.6k | : p_mx * (time_pb - time_pp) / time_pp; |
100 | 13.6k | } |
101 | 71.6k | p_my = s->next_pic.motion_val[0][xy][1]; |
102 | 71.6k | if ((unsigned)(p_my + tab_bias) < tab_size) { |
103 | 57.5k | s->mv[0][i][1] = s->direct_scale_mv[0][p_my + tab_bias] + my; |
104 | 57.5k | s->mv[1][i][1] = my ? s->mv[0][i][1] - p_my |
105 | 57.5k | : s->direct_scale_mv[1][p_my + tab_bias]; |
106 | 57.5k | } else { |
107 | 14.1k | s->mv[0][i][1] = p_my * time_pb / time_pp + my; |
108 | 14.1k | s->mv[1][i][1] = my ? s->mv[0][i][1] - p_my |
109 | 14.1k | : p_my * (time_pb - time_pp) / time_pp; |
110 | 14.1k | } |
111 | 71.6k | } |
112 | | |
113 | | #undef tab_size |
114 | | #undef tab_bias |
115 | | |
116 | | /** |
117 | | * @return the mb_type |
118 | | */ |
119 | | int ff_mpeg4_set_direct_mv(MpegEncContext *s, int mx, int my) |
120 | 64.4k | { |
121 | 64.4k | const int mb_index = s->mb_x + s->mb_y * s->mb_stride; |
122 | 64.4k | const int colocated_mb_type = s->next_pic.mb_type[mb_index]; |
123 | 64.4k | uint16_t time_pp; |
124 | 64.4k | uint16_t time_pb; |
125 | 64.4k | int i; |
126 | | |
127 | | // FIXME avoid divides |
128 | | // try special case with shifts for 1 and 3 B-frames? |
129 | | |
130 | 64.4k | if (IS_8X8(colocated_mb_type)) { |
131 | 3.05k | s->mv_type = MV_TYPE_8X8; |
132 | 15.2k | for (i = 0; i < 4; i++) |
133 | 12.2k | ff_mpeg4_set_one_direct_mv(s, mx, my, i); |
134 | 3.05k | return MB_TYPE_DIRECT2 | MB_TYPE_8x8 | MB_TYPE_BIDIR_MV; |
135 | 61.3k | } else if (IS_INTERLACED(colocated_mb_type)) { |
136 | 1.93k | s->mv_type = MV_TYPE_FIELD; |
137 | 5.80k | for (i = 0; i < 2; i++) { |
138 | 3.86k | int field_select = s->next_pic.ref_index[0][4 * mb_index + 2 * i]; |
139 | 3.86k | s->field_select[0][i] = field_select; |
140 | 3.86k | s->field_select[1][i] = i; |
141 | 3.86k | if (s->top_field_first) { |
142 | 2.40k | time_pp = s->pp_field_time - field_select + i; |
143 | 2.40k | time_pb = s->pb_field_time - field_select + i; |
144 | 2.40k | } else { |
145 | 1.46k | time_pp = s->pp_field_time + field_select - i; |
146 | 1.46k | time_pb = s->pb_field_time + field_select - i; |
147 | 1.46k | } |
148 | 3.86k | s->mv[0][i][0] = s->p_field_mv_table[i][0][mb_index][0] * |
149 | 3.86k | time_pb / time_pp + mx; |
150 | 3.86k | s->mv[0][i][1] = s->p_field_mv_table[i][0][mb_index][1] * |
151 | 3.86k | time_pb / time_pp + my; |
152 | 3.86k | s->mv[1][i][0] = mx ? s->mv[0][i][0] - |
153 | 378 | s->p_field_mv_table[i][0][mb_index][0] |
154 | 3.86k | : s->p_field_mv_table[i][0][mb_index][0] * |
155 | 3.49k | (time_pb - time_pp) / time_pp; |
156 | 3.86k | s->mv[1][i][1] = my ? s->mv[0][i][1] - |
157 | 558 | s->p_field_mv_table[i][0][mb_index][1] |
158 | 3.86k | : s->p_field_mv_table[i][0][mb_index][1] * |
159 | 3.31k | (time_pb - time_pp) / time_pp; |
160 | 3.86k | } |
161 | 1.93k | return MB_TYPE_DIRECT2 | MB_TYPE_16x8 | |
162 | 1.93k | MB_TYPE_BIDIR_MV | MB_TYPE_INTERLACED; |
163 | 59.4k | } else { |
164 | 59.4k | ff_mpeg4_set_one_direct_mv(s, mx, my, 0); |
165 | 59.4k | s->mv[0][1][0] = |
166 | 59.4k | s->mv[0][2][0] = |
167 | 59.4k | s->mv[0][3][0] = s->mv[0][0][0]; |
168 | 59.4k | s->mv[0][1][1] = |
169 | 59.4k | s->mv[0][2][1] = |
170 | 59.4k | s->mv[0][3][1] = s->mv[0][0][1]; |
171 | 59.4k | s->mv[1][1][0] = |
172 | 59.4k | s->mv[1][2][0] = |
173 | 59.4k | s->mv[1][3][0] = s->mv[1][0][0]; |
174 | 59.4k | s->mv[1][1][1] = |
175 | 59.4k | s->mv[1][2][1] = |
176 | 59.4k | s->mv[1][3][1] = s->mv[1][0][1]; |
177 | 59.4k | if ((s->avctx->workaround_bugs & FF_BUG_DIRECT_BLOCKSIZE) || |
178 | 43.6k | !s->quarter_sample) |
179 | 27.9k | s->mv_type = MV_TYPE_16X16; |
180 | 31.4k | else |
181 | 31.4k | s->mv_type = MV_TYPE_8X8; |
182 | | // Note see prev line |
183 | 59.4k | return MB_TYPE_DIRECT2 | MB_TYPE_16x16 | MB_TYPE_BIDIR_MV; |
184 | 59.4k | } |
185 | 64.4k | } |