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
23 #include "libavutil/common.h"
24 #include "libavutil/frame.h"
25 #include "libavutil/pixdesc.h"
26 #include "libavutil/motion_vector.h"
27 #include "libavutil/avassert.h"
28
29 #include "avcodec.h"
30 #include "mpegutils.h"
31
add_mb(AVMotionVector * mb,uint32_t mb_type,int dst_x,int dst_y,int motion_x,int motion_y,int motion_scale,int direction)32 static int add_mb(AVMotionVector *mb, uint32_t mb_type,
33 int dst_x, int dst_y,
34 int motion_x, int motion_y, int motion_scale,
35 int direction)
36 {
37 mb->w = IS_8X8(mb_type) || IS_8X16(mb_type) ? 8 : 16;
38 mb->h = IS_8X8(mb_type) || IS_16X8(mb_type) ? 8 : 16;
39 mb->motion_x = motion_x;
40 mb->motion_y = motion_y;
41 mb->motion_scale = motion_scale;
42 mb->dst_x = dst_x;
43 mb->dst_y = dst_y;
44 mb->src_x = dst_x + motion_x / motion_scale;
45 mb->src_y = dst_y + motion_y / motion_scale;
46 mb->source = direction ? 1 : -1;
47 mb->flags = 0; // XXX: does mb_type contain extra information that could be exported here?
48 return 1;
49 }
50
ff_draw_horiz_band(AVCodecContext * avctx,AVFrame * cur,AVFrame * last,int y,int h,int picture_structure,int first_field,int low_delay)51 void ff_draw_horiz_band(AVCodecContext *avctx,
52 AVFrame *cur, AVFrame *last,
53 int y, int h, int picture_structure,
54 int first_field, int low_delay)
55 {
56 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
57 int vshift = desc->log2_chroma_h;
58 const int field_pic = picture_structure != PICT_FRAME;
59 if (field_pic) {
60 h <<= 1;
61 y <<= 1;
62 }
63
64 h = FFMIN(h, avctx->height - y);
65
66 if (field_pic && first_field &&
67 !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD))
68 return;
69
70 if (avctx->draw_horiz_band) {
71 AVFrame *src;
72 int offset[AV_NUM_DATA_POINTERS];
73 int i;
74
75 if (cur->pict_type == AV_PICTURE_TYPE_B || low_delay ||
76 (avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
77 src = cur;
78 else if (last)
79 src = last;
80 else
81 return;
82
83 if (cur->pict_type == AV_PICTURE_TYPE_B &&
84 picture_structure == PICT_FRAME &&
85 avctx->codec_id != AV_CODEC_ID_SVQ3) {
86 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
87 offset[i] = 0;
88 } else {
89 offset[0]= y * src->linesize[0];
90 offset[1]=
91 offset[2]= (y >> vshift) * src->linesize[1];
92 for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
93 offset[i] = 0;
94 }
95
96 emms_c();
97
98 avctx->draw_horiz_band(avctx, src, offset,
99 y, picture_structure, h);
100 }
101 }
102
ff_print_debug_info2(AVCodecContext * avctx,AVFrame * pict,uint8_t * mbskip_table,uint32_t * mbtype_table,int8_t * qscale_table,int16_t (* motion_val[2])[2],int mb_width,int mb_height,int mb_stride,int quarter_sample)103 void ff_print_debug_info2(AVCodecContext *avctx, AVFrame *pict, uint8_t *mbskip_table,
104 uint32_t *mbtype_table, int8_t *qscale_table, int16_t (*motion_val[2])[2],
105 int mb_width, int mb_height, int mb_stride, int quarter_sample)
106 {
107 if ((avctx->export_side_data & AV_CODEC_EXPORT_DATA_MVS) && mbtype_table && motion_val[0]) {
108 const int shift = 1 + quarter_sample;
109 const int scale = 1 << shift;
110 const int mv_sample_log2 = avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_SVQ3 ? 2 : 1;
111 const int mv_stride = (mb_width << mv_sample_log2) +
112 (avctx->codec->id == AV_CODEC_ID_H264 ? 0 : 1);
113 int mb_x, mb_y, mbcount = 0;
114
115 /* size is width * height * 2 * 4 where 2 is for directions and 4 is
116 * for the maximum number of MB (4 MB in case of IS_8x8) */
117 AVMotionVector *mvs = av_malloc_array(mb_width * mb_height, 2 * 4 * sizeof(AVMotionVector));
118 if (!mvs)
119 return;
120
121 for (mb_y = 0; mb_y < mb_height; mb_y++) {
122 for (mb_x = 0; mb_x < mb_width; mb_x++) {
123 int i, direction, mb_type = mbtype_table[mb_x + mb_y * mb_stride];
124 for (direction = 0; direction < 2; direction++) {
125 if (!USES_LIST(mb_type, direction))
126 continue;
127 if (IS_8X8(mb_type)) {
128 for (i = 0; i < 4; i++) {
129 int sx = mb_x * 16 + 4 + 8 * (i & 1);
130 int sy = mb_y * 16 + 4 + 8 * (i >> 1);
131 int xy = (mb_x * 2 + (i & 1) +
132 (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
133 int mx = motion_val[direction][xy][0];
134 int my = motion_val[direction][xy][1];
135 mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, scale, direction);
136 }
137 } else if (IS_16X8(mb_type)) {
138 for (i = 0; i < 2; i++) {
139 int sx = mb_x * 16 + 8;
140 int sy = mb_y * 16 + 4 + 8 * i;
141 int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
142 int mx = motion_val[direction][xy][0];
143 int my = motion_val[direction][xy][1];
144
145 if (IS_INTERLACED(mb_type))
146 my *= 2;
147
148 mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, scale, direction);
149 }
150 } else if (IS_8X16(mb_type)) {
151 for (i = 0; i < 2; i++) {
152 int sx = mb_x * 16 + 4 + 8 * i;
153 int sy = mb_y * 16 + 8;
154 int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
155 int mx = motion_val[direction][xy][0];
156 int my = motion_val[direction][xy][1];
157
158 if (IS_INTERLACED(mb_type))
159 my *= 2;
160
161 mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, scale, direction);
162 }
163 } else {
164 int sx = mb_x * 16 + 8;
165 int sy = mb_y * 16 + 8;
166 int xy = (mb_x + mb_y * mv_stride) << mv_sample_log2;
167 int mx = motion_val[direction][xy][0];
168 int my = motion_val[direction][xy][1];
169 mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, scale, direction);
170 }
171 }
172 }
173 }
174
175 if (mbcount) {
176 AVFrameSideData *sd;
177
178 av_log(avctx, AV_LOG_DEBUG, "Adding %d MVs info to frame %d\n", mbcount, avctx->frame_number);
179 sd = av_frame_new_side_data(pict, AV_FRAME_DATA_MOTION_VECTORS, mbcount * sizeof(AVMotionVector));
180 if (!sd) {
181 av_freep(&mvs);
182 return;
183 }
184 memcpy(sd->data, mvs, mbcount * sizeof(AVMotionVector));
185 }
186
187 av_freep(&mvs);
188 }
189
190 /* TODO: export all the following to make them accessible for users (and filters) */
191 if (avctx->hwaccel || !mbtype_table)
192 return;
193
194
195 if (avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
196 int x,y;
197
198 av_log(avctx, AV_LOG_DEBUG, "New frame, type: %c\n",
199 av_get_picture_type_char(pict->pict_type));
200 for (y = 0; y < mb_height; y++) {
201 for (x = 0; x < mb_width; x++) {
202 if (avctx->debug & FF_DEBUG_SKIP) {
203 int count = mbskip_table ? mbskip_table[x + y * mb_stride] : 0;
204 if (count > 9)
205 count = 9;
206 av_log(avctx, AV_LOG_DEBUG, "%1d", count);
207 }
208 if (avctx->debug & FF_DEBUG_QP) {
209 av_log(avctx, AV_LOG_DEBUG, "%2d",
210 qscale_table[x + y * mb_stride]);
211 }
212 if (avctx->debug & FF_DEBUG_MB_TYPE) {
213 int mb_type = mbtype_table[x + y * mb_stride];
214 // Type & MV direction
215 if (IS_PCM(mb_type))
216 av_log(avctx, AV_LOG_DEBUG, "P");
217 else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
218 av_log(avctx, AV_LOG_DEBUG, "A");
219 else if (IS_INTRA4x4(mb_type))
220 av_log(avctx, AV_LOG_DEBUG, "i");
221 else if (IS_INTRA16x16(mb_type))
222 av_log(avctx, AV_LOG_DEBUG, "I");
223 else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
224 av_log(avctx, AV_LOG_DEBUG, "d");
225 else if (IS_DIRECT(mb_type))
226 av_log(avctx, AV_LOG_DEBUG, "D");
227 else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
228 av_log(avctx, AV_LOG_DEBUG, "g");
229 else if (IS_GMC(mb_type))
230 av_log(avctx, AV_LOG_DEBUG, "G");
231 else if (IS_SKIP(mb_type))
232 av_log(avctx, AV_LOG_DEBUG, "S");
233 else if (!USES_LIST(mb_type, 1))
234 av_log(avctx, AV_LOG_DEBUG, ">");
235 else if (!USES_LIST(mb_type, 0))
236 av_log(avctx, AV_LOG_DEBUG, "<");
237 else {
238 av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
239 av_log(avctx, AV_LOG_DEBUG, "X");
240 }
241
242 // segmentation
243 if (IS_8X8(mb_type))
244 av_log(avctx, AV_LOG_DEBUG, "+");
245 else if (IS_16X8(mb_type))
246 av_log(avctx, AV_LOG_DEBUG, "-");
247 else if (IS_8X16(mb_type))
248 av_log(avctx, AV_LOG_DEBUG, "|");
249 else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
250 av_log(avctx, AV_LOG_DEBUG, " ");
251 else
252 av_log(avctx, AV_LOG_DEBUG, "?");
253
254
255 if (IS_INTERLACED(mb_type))
256 av_log(avctx, AV_LOG_DEBUG, "=");
257 else
258 av_log(avctx, AV_LOG_DEBUG, " ");
259 }
260 }
261 av_log(avctx, AV_LOG_DEBUG, "\n");
262 }
263 }
264 }
265