FFmpeg
Loading...
Searching...
No Matches
mpegutils.c
Go to the documentation of this file.
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/bprint.h"
24#include "libavutil/common.h"
25#include "libavutil/frame.h"
26#include "libavutil/mem.h"
27#include "libavutil/pixdesc.h"
29#include "libavutil/avassert.h"
30
31#include "avcodec.h"
32#include "mpegutils.h"
33
34static int add_mb(AVMotionVector *mb, uint32_t mb_type,
35 int dst_x, int dst_y,
36 int motion_x, int motion_y, int motion_scale,
37 int direction)
38{
39 mb->w = IS_8X8(mb_type) || IS_8X16(mb_type) ? 8 : 16;
40 mb->h = IS_8X8(mb_type) || IS_16X8(mb_type) ? 8 : 16;
41 mb->motion_x = motion_x;
42 mb->motion_y = motion_y;
43 mb->motion_scale = motion_scale;
44 mb->dst_x = dst_x;
45 mb->dst_y = dst_y;
46 mb->src_x = dst_x + motion_x / motion_scale;
47 mb->src_y = dst_y + motion_y / motion_scale;
48 mb->source = direction ? 1 : -1;
49 mb->flags = 0; // XXX: does mb_type contain extra information that could be exported here?
50 return 1;
51}
52
54 const AVFrame *cur, const AVFrame *last,
55 int y, int h, int picture_structure,
56 int first_field, int low_delay)
57{
58 const int field_pic = picture_structure != PICT_FRAME;
60 const AVFrame *src;
62
63 if (!avctx->draw_horiz_band)
64 return;
65
66 if (field_pic) {
67 h <<= 1;
68 y <<= 1;
69 }
70
71 h = FFMIN(h, avctx->height - y);
72
73 if (field_pic && first_field &&
75 return;
76
77 if (cur->pict_type == AV_PICTURE_TYPE_B || low_delay ||
79 src = cur;
80 else if (last)
81 src = last;
82 else
83 return;
84
86
87 offset[0] = y * src->linesize[0];
88 offset[1] =
89 offset[2] = (y >> desc->log2_chroma_h) * src->linesize[1];
90 for (int i = 3; i < AV_NUM_DATA_POINTERS; i++)
91 offset[i] = 0;
92
93 avctx->draw_horiz_band(avctx, src, offset,
94 y, picture_structure, h);
95}
96
97#define HAS_MV_EXT(mb_type, flags, dir) ((mb_type) & flags[(dir)])
98
99static char get_type_mv_char(int mb_type, const int mb_type_mv_flags[2])
100{
101 // Type & MV direction
102 if (IS_PCM(mb_type))
103 return 'P';
104 else if (IS_ACPRED(mb_type))
105 return 'A';
106 else if (IS_INTRA4x4(mb_type))
107 return 'i';
108 else if (IS_INTRA16x16(mb_type))
109 return 'I';
110 else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
111 return 'd';
112 else if (IS_DIRECT(mb_type))
113 return 'D';
114 else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
115 return 'g';
116 else if (IS_GMC(mb_type))
117 return 'G';
118 else if (IS_SKIP(mb_type))
119 return 'S';
120 else if (!HAS_MV_EXT(mb_type, 1, mb_type_mv_flags))
121 return '>';
122 else if (!HAS_MV_EXT(mb_type, 0, mb_type_mv_flags))
123 return '<';
124 else {
125 av_assert2(HAS_MV_EXT(mb_type, 0, mb_type_mv_flags) && HAS_MV_EXT(mb_type, 1, mb_type_mv_flags));
126 return 'X';
127 }
128}
129
130static char get_segmentation_char(int mb_type)
131{
132 if (IS_8X8(mb_type))
133 return '+';
134 else if (IS_16X8(mb_type))
135 return '-';
136 else if (IS_8X16(mb_type))
137 return '|';
138 else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
139 return ' ';
140
141 return '?';
142}
143
144static char get_interlacement_char(int mb_type)
145{
146 if (IS_INTERLACED(mb_type))
147 return '=';
148 else
149 return ' ';
150}
151
153 const uint32_t *mbtype_table,
154 const int8_t *qscale_table, int16_t (*const motion_val[2])[2],
155 int mb_width, int mb_height, int mb_stride, int quarter_sample)
156{
157 const int is_h264 = avctx->codec_id == AV_CODEC_ID_H264;
158 const int mb_type_mv_flags[2] = { is_h264 ? MB_TYPE_L0 : MB_TYPE_FORWARD_MV,
159 is_h264 ? MB_TYPE_L1 : MB_TYPE_BACKWARD_MV };
160
161 if ((avctx->export_side_data & AV_CODEC_EXPORT_DATA_MVS) && mbtype_table && motion_val[0]) {
162 const int shift = 1 + quarter_sample;
163 const int scale = 1 << shift;
164 const int mv_sample_log2 = is_h264 ? 2 : 1;
165 const int mv_stride = (mb_width << mv_sample_log2) + !is_h264;
166 int mb_x, mb_y, mbcount = 0;
167
168 /* size is width * height * 2 * 4 where 2 is for directions and 4 is
169 * for the maximum number of MB (4 MB in case of IS_8x8) */
170 AVMotionVector *mvs = av_malloc_array(mb_width * mb_height, 2 * 4 * sizeof(AVMotionVector));
171 if (!mvs)
172 return;
173
174 for (mb_y = 0; mb_y < mb_height; mb_y++) {
175 for (mb_x = 0; mb_x < mb_width; mb_x++) {
176 int i, direction, mb_type = mbtype_table[mb_x + mb_y * mb_stride];
177 for (direction = 0; direction < 2; direction++) {
178 if (!HAS_MV_EXT(mb_type, direction, mb_type_mv_flags))
179 continue;
180 if (IS_8X8(mb_type)) {
181 for (i = 0; i < 4; i++) {
182 int sx = mb_x * 16 + 4 + 8 * (i & 1);
183 int sy = mb_y * 16 + 4 + 8 * (i >> 1);
184 int xy = (mb_x * 2 + (i & 1) +
185 (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
186 int mx = motion_val[direction][xy][0];
187 int my = motion_val[direction][xy][1];
188 mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, scale, direction);
189 }
190 } else if (IS_16X8(mb_type)) {
191 for (i = 0; i < 2; i++) {
192 int sx = mb_x * 16 + 8;
193 int sy = mb_y * 16 + 4 + 8 * i;
194 int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
195 int mx = motion_val[direction][xy][0];
196 int my = motion_val[direction][xy][1];
197
198 if (IS_INTERLACED(mb_type))
199 my *= 2;
200
201 mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, scale, direction);
202 }
203 } else if (IS_8X16(mb_type)) {
204 for (i = 0; i < 2; i++) {
205 int sx = mb_x * 16 + 4 + 8 * i;
206 int sy = mb_y * 16 + 8;
207 int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
208 int mx = motion_val[direction][xy][0];
209 int my = motion_val[direction][xy][1];
210
211 if (IS_INTERLACED(mb_type))
212 my *= 2;
213
214 mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, scale, direction);
215 }
216 } else {
217 int sx = mb_x * 16 + 8;
218 int sy = mb_y * 16 + 8;
219 int xy = (mb_x + mb_y * mv_stride) << mv_sample_log2;
220 int mx = motion_val[direction][xy][0];
221 int my = motion_val[direction][xy][1];
222 mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, scale, direction);
223 }
224 }
225 }
226 }
227
228 if (mbcount) {
229 AVFrameSideData *sd;
230
231 av_log(avctx, AV_LOG_DEBUG, "Adding %d MVs info to frame %"PRId64"\n", mbcount, avctx->frame_num);
233 if (!sd) {
234 av_freep(&mvs);
235 return;
236 }
237 memcpy(sd->data, mvs, mbcount * sizeof(AVMotionVector));
238 }
239
240 av_freep(&mvs);
241 }
242
243 /* TODO: export all the following to make them accessible for users (and filters) */
244 if (avctx->hwaccel || !mbtype_table)
245 return;
246
247
248 if (avctx->debug & (FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
249 int x,y;
250 AVBPrint buf;
251 int n;
252 int margin_left;
253 int x_step;
254
255 av_log(avctx, AV_LOG_DEBUG, "New frame, type: %c\n",
257
258 margin_left = 2;
259 n = mb_width << 4;
260 while ((n /= 10))
261 margin_left++;
262
264 av_bprint_chars(&buf, ' ', margin_left);
265
266 n = 0;
267 if (avctx->debug & FF_DEBUG_QP)
268 n += 2;
269 if (avctx->debug & FF_DEBUG_MB_TYPE)
270 n += 3;
271 x_step = (mb_width * 16 > 999) ? 8 : 4;
272 for (x = 0; x < mb_width; x += x_step)
273 av_bprintf(&buf, "%-*d", n * x_step, x << 4);
274
275 av_log(avctx, AV_LOG_DEBUG, "%s\n", buf.str);
276
277 for (y = 0; y < mb_height; y++) {
278 av_bprint_clear(&buf);
279 for (x = 0; x < mb_width; x++) {
280 if (x == 0)
281 av_bprintf(&buf, "%*d ", margin_left - 1, y << 4);
282 if (avctx->debug & FF_DEBUG_QP) {
283 av_bprintf(&buf, "%2d", qscale_table[x + y * mb_stride]);
284 }
285 if (avctx->debug & FF_DEBUG_MB_TYPE) {
286 int mb_type = mbtype_table[x + y * mb_stride];
287
288 av_bprintf(&buf, "%c%c%c",
289 get_type_mv_char(mb_type, mb_type_mv_flags),
290 get_segmentation_char(mb_type),
291 get_interlacement_char(mb_type));
292 }
293 }
294
295 av_log(avctx, AV_LOG_DEBUG, "%s\n", buf.str);
296 }
298 }
299}
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t my
Definition dsp.h:67
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t mx
Definition dsp.h:67
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition avassert.h:68
Libavcodec external API header.
#define SLICE_FLAG_ALLOW_FIELD
allow draw_horiz_band() with field slices (MPEG-2 field pics)
Definition avcodec.h:722
#define SLICE_FLAG_CODED_ORDER
draw_horiz_band() is called in coded order instead of display
Definition avcodec.h:721
#define FF_DEBUG_QP
Definition avcodec.h:1403
#define FF_DEBUG_MB_TYPE
Definition avcodec.h:1402
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition bprint.c:121
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition bprint.c:68
AVBPrint public header.
#define AV_BPRINT_SIZE_UNLIMITED
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define mb(name)
Definition cbs_lcevc.c:95
common internal and external API header
#define NULL
Definition coverity.c:32
reference-counted frame API
#define AV_NUM_DATA_POINTERS
Definition frame.h:480
#define AV_CODEC_EXPORT_DATA_MVS
Export motion vectors through frame side data.
Definition avcodec.h:390
@ AV_CODEC_ID_H264
Definition codec_id.h:77
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition bprint.c:234
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
Definition bprint.c:129
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition bprint.c:226
AVFrameSideData * av_frame_new_side_data(AVFrame *frame, enum AVFrameSideDataType type, size_t size)
Add a new side data to a frame.
Definition frame.c:650
@ AV_FRAME_DATA_MOTION_VECTORS
Motion vectors exported by some codecs (on demand through the export_mvs flag set in the libavcodec A...
Definition frame.h:97
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
char av_get_picture_type_char(enum AVPictureType pict_type)
Return a single letter to describe the given picture type pict_type.
Definition utils.c:40
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition avutil.h:280
static av_always_inline void scale(vec_s32 res[4], vec_s16 res_packed[2], const int shift)
unsigned offset
Definition libaomenc.c:763
static int shift(int a, int b)
Definition bonk.c:261
const char * desc
Definition libsvtav1.c:83
#define FFMIN(a, b)
Definition macros.h:49
Memory handling functions.
static char get_segmentation_char(int mb_type)
Definition mpegutils.c:130
#define HAS_MV_EXT(mb_type, flags, dir)
Definition mpegutils.c:97
static char get_type_mv_char(int mb_type, const int mb_type_mv_flags[2])
Definition mpegutils.c:99
void ff_print_debug_info2(AVCodecContext *avctx, AVFrame *pict, const uint32_t *mbtype_table, const int8_t *qscale_table, int16_t(*const motion_val[2])[2], int mb_width, int mb_height, int mb_stride, int quarter_sample)
Print debugging info for the given picture.
Definition mpegutils.c:152
static char get_interlacement_char(int mb_type)
Definition mpegutils.c:144
static int 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)
Definition mpegutils.c:34
void ff_draw_horiz_band(AVCodecContext *avctx, const AVFrame *cur, const AVFrame *last, int y, int h, int picture_structure, int first_field, int low_delay)
Draw a horizontal band if supported.
Definition mpegutils.c:53
#define IS_GMC(a)
Definition mpegutils.h:79
#define IS_ACPRED(a)
Definition mpegutils.h:84
#define MB_TYPE_BACKWARD_MV
Definition mpegutils.h:50
#define IS_INTERLACED(a)
Definition mpegutils.h:77
#define IS_DIRECT(a)
Definition mpegutils.h:78
#define IS_INTRA4x4(a)
Definition mpegutils.h:69
#define MB_TYPE_L1
Definition mpegutils.h:58
#define IS_16X8(a)
Definition mpegutils.h:81
#define IS_8X16(a)
Definition mpegutils.h:82
#define IS_16X16(a)
Definition mpegutils.h:80
#define IS_PCM(a)
Definition mpegutils.h:71
#define IS_8X8(a)
Definition mpegutils.h:83
#define MB_TYPE_L0
Definition mpegutils.h:57
#define MB_TYPE_FORWARD_MV
Definition mpegutils.h:49
#define IS_SKIP(a)
Definition mpegutils.h:75
#define IS_INTRA16x16(a)
Definition mpegutils.h:70
#define PICT_FRAME
Definition mpegutils.h:33
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3484
#define IS_INTRA(x, y)
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:644
int debug
debug
Definition avcodec.h:1398
int slice_flags
slice flags
Definition avcodec.h:720
int64_t frame_num
Frame counter, set by libavcodec.
Definition avcodec.h:1893
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition avcodec.h:1429
void(* draw_horiz_band)(struct AVCodecContext *s, const AVFrame *src, int offset[AV_NUM_DATA_POINTERS], int y, int type, int height)
If non NULL, 'draw_horiz_band' is called by the libavcodec decoder to draw a horizontal band.
Definition avcodec.h:748
int export_side_data
Bit set of AV_CODEC_EXPORT_DATA_* flags, which affects the kind of metadata exported in frame,...
Definition avcodec.h:1789
enum AVCodecID codec_id
Definition avcodec.h:453
Structure to hold side data for an AVFrame.
Definition frame.h:334
uint8_t * data
Definition frame.h:336
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
enum AVPictureType pict_type
Picture type of the frame.
Definition frame.h:571
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
#define av_malloc_array(a, b)
#define av_freep(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
static int first_field(const struct video_data *s)
Definition v4l2.c:260