FFmpeg
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/emms.h"
26 #include "libavutil/frame.h"
27 #include "libavutil/pixdesc.h"
29 #include "libavutil/avassert.h"
30 
31 #include "avcodec.h"
32 #include "mpegutils.h"
33 
34 static 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;
59  const AVFrame *src;
61 
62  if (!avctx->draw_horiz_band)
63  return;
64 
65  if (field_pic) {
66  h <<= 1;
67  y <<= 1;
68  }
69 
70  h = FFMIN(h, avctx->height - y);
71 
72  if (field_pic && first_field &&
74  return;
75 
76  if (cur->pict_type == AV_PICTURE_TYPE_B || low_delay ||
78  src = cur;
79  else if (last)
80  src = last;
81  else
82  return;
83 
84  if (cur->pict_type == AV_PICTURE_TYPE_B &&
85  picture_structure == PICT_FRAME &&
86  avctx->codec_id != AV_CODEC_ID_SVQ3) {
87  for (int i = 0; i < AV_NUM_DATA_POINTERS; i++)
88  offset[i] = 0;
89  } else {
91  int vshift = desc->log2_chroma_h;
92 
93  offset[0] = y * src->linesize[0];
94  offset[1] =
95  offset[2] = (y >> vshift) * src->linesize[1];
96  for (int i = 3; i < AV_NUM_DATA_POINTERS; i++)
97  offset[i] = 0;
98  }
99 
100  emms_c();
101 
102  avctx->draw_horiz_band(avctx, src, offset,
103  y, picture_structure, h);
104 }
105 
106 static char get_type_mv_char(int mb_type)
107 {
108  // Type & MV direction
109  if (IS_PCM(mb_type))
110  return 'P';
111  else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
112  return 'A';
113  else if (IS_INTRA4x4(mb_type))
114  return 'i';
115  else if (IS_INTRA16x16(mb_type))
116  return 'I';
117  else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
118  return 'd';
119  else if (IS_DIRECT(mb_type))
120  return 'D';
121  else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
122  return 'g';
123  else if (IS_GMC(mb_type))
124  return 'G';
125  else if (IS_SKIP(mb_type))
126  return 'S';
127  else if (!USES_LIST(mb_type, 1))
128  return '>';
129  else if (!USES_LIST(mb_type, 0))
130  return '<';
131  else {
132  av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
133  return 'X';
134  }
135 }
136 
137 static char get_segmentation_char(int mb_type)
138 {
139  if (IS_8X8(mb_type))
140  return '+';
141  else if (IS_16X8(mb_type))
142  return '-';
143  else if (IS_8X16(mb_type))
144  return '|';
145  else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
146  return ' ';
147 
148  return '?';
149 }
150 
151 static char get_interlacement_char(int mb_type)
152 {
153  if (IS_INTERLACED(mb_type))
154  return '=';
155  else
156  return ' ';
157 }
158 
160  const uint8_t *mbskip_table, const uint32_t *mbtype_table,
161  const int8_t *qscale_table, int16_t (*const motion_val[2])[2],
162  int mb_width, int mb_height, int mb_stride, int quarter_sample)
163 {
164  if ((avctx->export_side_data & AV_CODEC_EXPORT_DATA_MVS) && mbtype_table && motion_val[0]) {
165  const int shift = 1 + quarter_sample;
166  const int scale = 1 << shift;
167  const int mv_sample_log2 = avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_SVQ3 ? 2 : 1;
168  const int mv_stride = (mb_width << mv_sample_log2) +
169  (avctx->codec->id == AV_CODEC_ID_H264 ? 0 : 1);
170  int mb_x, mb_y, mbcount = 0;
171 
172  /* size is width * height * 2 * 4 where 2 is for directions and 4 is
173  * for the maximum number of MB (4 MB in case of IS_8x8) */
174  AVMotionVector *mvs = av_malloc_array(mb_width * mb_height, 2 * 4 * sizeof(AVMotionVector));
175  if (!mvs)
176  return;
177 
178  for (mb_y = 0; mb_y < mb_height; mb_y++) {
179  for (mb_x = 0; mb_x < mb_width; mb_x++) {
180  int i, direction, mb_type = mbtype_table[mb_x + mb_y * mb_stride];
181  for (direction = 0; direction < 2; direction++) {
182  if (!USES_LIST(mb_type, direction))
183  continue;
184  if (IS_8X8(mb_type)) {
185  for (i = 0; i < 4; i++) {
186  int sx = mb_x * 16 + 4 + 8 * (i & 1);
187  int sy = mb_y * 16 + 4 + 8 * (i >> 1);
188  int xy = (mb_x * 2 + (i & 1) +
189  (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
190  int mx = motion_val[direction][xy][0];
191  int my = motion_val[direction][xy][1];
192  mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, scale, direction);
193  }
194  } else if (IS_16X8(mb_type)) {
195  for (i = 0; i < 2; i++) {
196  int sx = mb_x * 16 + 8;
197  int sy = mb_y * 16 + 4 + 8 * i;
198  int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
199  int mx = motion_val[direction][xy][0];
200  int my = motion_val[direction][xy][1];
201 
202  if (IS_INTERLACED(mb_type))
203  my *= 2;
204 
205  mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, scale, direction);
206  }
207  } else if (IS_8X16(mb_type)) {
208  for (i = 0; i < 2; i++) {
209  int sx = mb_x * 16 + 4 + 8 * i;
210  int sy = mb_y * 16 + 8;
211  int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
212  int mx = motion_val[direction][xy][0];
213  int my = motion_val[direction][xy][1];
214 
215  if (IS_INTERLACED(mb_type))
216  my *= 2;
217 
218  mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, scale, direction);
219  }
220  } else {
221  int sx = mb_x * 16 + 8;
222  int sy = mb_y * 16 + 8;
223  int xy = (mb_x + mb_y * mv_stride) << mv_sample_log2;
224  int mx = motion_val[direction][xy][0];
225  int my = motion_val[direction][xy][1];
226  mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, scale, direction);
227  }
228  }
229  }
230  }
231 
232  if (mbcount) {
233  AVFrameSideData *sd;
234 
235  av_log(avctx, AV_LOG_DEBUG, "Adding %d MVs info to frame %"PRId64"\n", mbcount, avctx->frame_num);
237  if (!sd) {
238  av_freep(&mvs);
239  return;
240  }
241  memcpy(sd->data, mvs, mbcount * sizeof(AVMotionVector));
242  }
243 
244  av_freep(&mvs);
245  }
246 
247  /* TODO: export all the following to make them accessible for users (and filters) */
248  if (avctx->hwaccel || !mbtype_table)
249  return;
250 
251 
252  if (avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
253  int x,y;
254  AVBPrint buf;
255  int n;
256  int margin_left;
257  int x_step;
258 
259  av_log(avctx, AV_LOG_DEBUG, "New frame, type: %c\n",
261 
262  margin_left = 2;
263  n = mb_width << 4;
264  while ((n /= 10))
265  margin_left++;
266 
268  av_bprint_chars(&buf, ' ', margin_left);
269 
270  n = 0;
271  if (avctx->debug & FF_DEBUG_SKIP)
272  n++;
273  if (avctx->debug & FF_DEBUG_QP)
274  n += 2;
275  if (avctx->debug & FF_DEBUG_MB_TYPE)
276  n += 3;
277  x_step = (mb_width * 16 > 999) ? 8 : 4;
278  for (x = 0; x < mb_width; x += x_step)
279  av_bprintf(&buf, "%-*d", n * x_step, x << 4);
280 
281  av_log(avctx, AV_LOG_DEBUG, "%s\n", buf.str);
282 
283  for (y = 0; y < mb_height; y++) {
285  for (x = 0; x < mb_width; x++) {
286  if (x == 0)
287  av_bprintf(&buf, "%*d ", margin_left - 1, y << 4);
288  if (avctx->debug & FF_DEBUG_SKIP) {
289  int count = mbskip_table ? mbskip_table[x + y * mb_stride] : 0;
290  if (count > 9)
291  count = 9;
292  av_bprintf(&buf, "%1d", count);
293  }
294  if (avctx->debug & FF_DEBUG_QP) {
295  av_bprintf(&buf, "%2d", qscale_table[x + y * mb_stride]);
296  }
297  if (avctx->debug & FF_DEBUG_MB_TYPE) {
298  int mb_type = mbtype_table[x + y * mb_stride];
299 
300  av_bprintf(&buf, "%c%c%c",
301  get_type_mv_char(mb_type),
302  get_segmentation_char(mb_type),
303  get_interlacement_char(mb_type));
304  }
305  }
306 
307  av_log(avctx, AV_LOG_DEBUG, "%s\n", buf.str);
308  }
310  }
311 }
PICT_FRAME
#define PICT_FRAME
Definition: mpegutils.h:38
IS_INTRA4x4
#define IS_INTRA4x4(a)
Definition: mpegutils.h:68
ff_draw_horiz_band
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
AVCodecContext::hwaccel
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:1427
IS_8X8
#define IS_8X8(a)
Definition: mpegutils.h:82
AV_BPRINT_SIZE_UNLIMITED
#define AV_BPRINT_SIZE_UNLIMITED
IS_GMC
#define IS_GMC(a)
Definition: mpegutils.h:78
IS_ACPRED
#define IS_ACPRED(a)
Definition: mpegutils.h:87
av_bprint_init
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
av_frame_new_side_data
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:786
SLICE_FLAG_CODED_ORDER
#define SLICE_FLAG_CODED_ORDER
draw_horiz_band() is called in coded order instead of display
Definition: avcodec.h:731
AVMotionVector
Definition: motion_vector.h:24
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2962
SLICE_FLAG_ALLOW_FIELD
#define SLICE_FLAG_ALLOW_FIELD
allow draw_horiz_band() with field slices (MPEG-2 field pics)
Definition: avcodec.h:732
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:375
pixdesc.h
mpegutils.h
AVFrame::buf
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:588
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:454
USES_LIST
#define USES_LIST(a, list)
Definition: mpegutils.h:92
avassert.h
emms_c
#define emms_c()
Definition: emms.h:63
motion_vector.h
IS_16X8
#define IS_16X8(a)
Definition: mpegutils.h:80
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
IS_SKIP
#define IS_SKIP(a)
Definition: mpegutils.h:74
IS_INTRA
#define IS_INTRA(x, y)
AV_CODEC_ID_SVQ3
@ AV_CODEC_ID_SVQ3
Definition: codec_id.h:75
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:79
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:455
NULL
#define NULL
Definition: coverity.c:32
AVCodecContext::slice_flags
int slice_flags
slice flags
Definition: avcodec.h:730
get_type_mv_char
static char get_type_mv_char(int mb_type)
Definition: mpegutils.c:106
FF_DEBUG_MB_TYPE
#define FF_DEBUG_MB_TYPE
Definition: avcodec.h:1400
IS_INTERLACED
#define IS_INTERLACED(a)
Definition: mpegutils.h:76
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:477
scale
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition: vvc_intra.c:291
av_bprint_finalize
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:240
shift
static int shift(int a, int b)
Definition: bonk.c:262
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
AV_NUM_DATA_POINTERS
#define AV_NUM_DATA_POINTERS
Definition: frame.h:376
get_interlacement_char
static char get_interlacement_char(int mb_type)
Definition: mpegutils.c:151
AVFrameSideData::data
uint8_t * data
Definition: frame.h:252
frame.h
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
IS_INTRA16x16
#define IS_INTRA16x16(a)
Definition: mpegutils.h:69
mb
#define mb
Definition: vf_colormatrix.c:99
IS_DIRECT
#define IS_DIRECT(a)
Definition: mpegutils.h:77
IS_16X16
#define IS_16X16(a)
Definition: mpegutils.h:79
AVCodec::id
enum AVCodecID id
Definition: codec.h:201
av_get_picture_type_char
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
emms.h
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:67
bprint.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:31
FF_DEBUG_SKIP
#define FF_DEBUG_SKIP
Definition: avcodec.h:1403
common.h
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVCodecContext::height
int height
Definition: avcodec.h:618
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:657
avcodec.h
AVCodecContext::frame_num
int64_t frame_num
Frame counter, set by libavcodec.
Definition: avcodec.h:2030
av_bprintf
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition: bprint.c:99
FF_DEBUG_QP
#define FF_DEBUG_QP
Definition: avcodec.h:1401
AVCodecContext::draw_horiz_band
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:758
AVCodecContext
main external API structure.
Definition: avcodec.h:445
IS_PCM
#define IS_PCM(a)
Definition: mpegutils.h:70
AV_PICTURE_TYPE_B
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition: avutil.h:281
av_bprint_clear
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition: bprint.c:232
AVCodecContext::export_side_data
int export_side_data
Bit set of AV_CODEC_EXPORT_DATA_* flags, which affects the kind of metadata exported in frame,...
Definition: avcodec.h:1926
IS_8X16
#define IS_8X16(a)
Definition: mpegutils.h:81
AVCodecContext::debug
int debug
debug
Definition: avcodec.h:1396
desc
const char * desc
Definition: libsvtav1.c:75
AV_CODEC_EXPORT_DATA_MVS
#define AV_CODEC_EXPORT_DATA_MVS
Export motion vectors through frame side data.
Definition: avcodec.h:406
AVFrameSideData
Structure to hold side data for an AVFrame.
Definition: frame.h:250
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
add_mb
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
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_FRAME_DATA_MOTION_VECTORS
@ 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
h
h
Definition: vp9dsp_template.c:2038
av_bprint_chars
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
Definition: bprint.c:145
get_segmentation_char
static char get_segmentation_char(int mb_type)
Definition: mpegutils.c:137
first_field
static int first_field(const struct video_data *s)
Definition: v4l2.c:246
ff_print_debug_info2
void ff_print_debug_info2(AVCodecContext *avctx, AVFrame *pict, const uint8_t *mbskip_table, 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:159