1 /*
2 * Mpeg video formats-related defines and utility functions
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <stdint.h>
22
31
34
36 int dst_x, int dst_y,
37 int motion_x, int motion_y, int motion_scale,
38 int direction)
39 {
42 mb->motion_x = motion_x;
43 mb->motion_y = motion_y;
44 mb->motion_scale = motion_scale;
47 mb->src_x = dst_x + motion_x / motion_scale;
48 mb->src_y = dst_y + motion_y / motion_scale;
49 mb->source = direction ? 1 : -1;
50 mb->flags = 0;
// XXX: does mb_type contain extra information that could be exported here?
51 return 1;
52 }
53
56 int y,
int h,
int picture_structure,
58 {
59 const int field_pic = picture_structure !=
PICT_FRAME;
63
65 return;
66
67 if (field_pic) {
69 y <<= 1;
70 }
71
73
76 return;
77
81 else if (last)
83 else
84 return;
85
87
93
95
97 y, picture_structure,
h);
98 }
99
100 #define HAS_MV_EXT(mb_type, flags, dir) ((mb_type) & flags[(dir)])
101
103 {
104 // Type & MV direction
106 return 'P';
108 return 'A';
110 return 'i';
112 return 'I';
114 return 'd';
116 return 'D';
118 return 'g';
120 return 'G';
122 return 'S';
123 else if (!
HAS_MV_EXT(mb_type, 1, mb_type_mv_flags))
124 return '>';
125 else if (!
HAS_MV_EXT(mb_type, 0, mb_type_mv_flags))
126 return '<';
127 else {
129 return 'X';
130 }
131 }
132
134 {
136 return '+';
138 return '-';
140 return '|';
142 return ' ';
143
144 return '?';
145 }
146
148 {
150 return '=';
151 else
152 return ' ';
153 }
154
156 const uint32_t *mbtype_table,
157 const int8_t *qscale_table, int16_t (*const motion_val[2])[2],
158 int mb_width, int mb_height, int mb_stride, int quarter_sample)
159 {
163
165 const int shift = 1 + quarter_sample;
167 const int mv_sample_log2 = is_h264 ? 2 : 1;
168 const int mv_stride = (mb_width << mv_sample_log2) + !is_h264;
169 int mb_x, mb_y, mbcount = 0;
170
171 /* size is width * height * 2 * 4 where 2 is for directions and 4 is
172 * for the maximum number of MB (4 MB in case of IS_8x8) */
174 if (!mvs)
175 return;
176
177 for (mb_y = 0; mb_y < mb_height; mb_y++) {
178 for (mb_x = 0; mb_x < mb_width; mb_x++) {
179 int i, direction, mb_type = mbtype_table[mb_x + mb_y * mb_stride];
180 for (direction = 0; direction < 2; direction++) {
181 if (!
HAS_MV_EXT(mb_type, direction, mb_type_mv_flags))
182 continue;
184 for (
i = 0;
i < 4;
i++) {
185 int sx = mb_x * 16 + 4 + 8 * (
i & 1);
186 int sy = mb_y * 16 + 4 + 8 * (
i >> 1);
187 int xy = (mb_x * 2 + (
i & 1) +
188 (mb_y * 2 + (
i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
189 int mx = motion_val[direction][xy][0];
190 int my = motion_val[direction][xy][1];
191 mbcount +=
add_mb(mvs + mbcount, mb_type, sx, sy,
mx,
my,
scale, direction);
192 }
194 for (
i = 0;
i < 2;
i++) {
195 int sx = mb_x * 16 + 8;
196 int sy = mb_y * 16 + 4 + 8 *
i;
197 int xy = (mb_x * 2 + (mb_y * 2 +
i) * mv_stride) << (mv_sample_log2 - 1);
198 int mx = motion_val[direction][xy][0];
199 int my = motion_val[direction][xy][1];
200
203
204 mbcount +=
add_mb(mvs + mbcount, mb_type, sx, sy,
mx,
my,
scale, direction);
205 }
207 for (
i = 0;
i < 2;
i++) {
208 int sx = mb_x * 16 + 4 + 8 *
i;
209 int sy = mb_y * 16 + 8;
210 int xy = (mb_x * 2 +
i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
211 int mx = motion_val[direction][xy][0];
212 int my = motion_val[direction][xy][1];
213
216
217 mbcount +=
add_mb(mvs + mbcount, mb_type, sx, sy,
mx,
my,
scale, direction);
218 }
219 } else {
220 int sx = mb_x * 16 + 8;
221 int sy = mb_y * 16 + 8;
222 int xy = (mb_x + mb_y * mv_stride) << mv_sample_log2;
223 int mx = motion_val[direction][xy][0];
224 int my = motion_val[direction][xy][1];
225 mbcount +=
add_mb(mvs + mbcount, mb_type, sx, sy,
mx,
my,
scale, direction);
226 }
227 }
228 }
229 }
230
231 if (mbcount) {
233
236 if (!sd) {
238 return;
239 }
241 }
242
244 }
245
246 /* TODO: export all the following to make them accessible for users (and filters) */
247 if (avctx->
hwaccel || !mbtype_table)
248 return;
249
250
252 int x,y;
253 AVBPrint buf;
254 int n;
255 int margin_left;
256 int x_step;
257
260
261 margin_left = 2;
262 n = mb_width << 4;
263 while ((n /= 10))
264 margin_left++;
265
268
269 n = 0;
271 n += 2;
273 n += 3;
274 x_step = (mb_width * 16 > 999) ? 8 : 4;
275 for (x = 0; x < mb_width; x += x_step)
277
279
280 for (y = 0; y < mb_height; y++) {
282 for (x = 0; x < mb_width; x++) {
283 if (x == 0)
284 av_bprintf(&buf,
"%*d ", margin_left - 1, y << 4);
286 av_bprintf(&buf,
"%2d", qscale_table[x + y * mb_stride]);
287 }
289 int mb_type = mbtype_table[x + y * mb_stride];
290
295 }
296 }
297
299 }
301 }
302 }