| Line | Branch | Exec | Source |
|---|---|---|---|
| 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/mem.h" | ||
| 28 | #include "libavutil/pixdesc.h" | ||
| 29 | #include "libavutil/motion_vector.h" | ||
| 30 | #include "libavutil/avassert.h" | ||
| 31 | |||
| 32 | #include "avcodec.h" | ||
| 33 | #include "mpegutils.h" | ||
| 34 | |||
| 35 | 5065 | static int add_mb(AVMotionVector *mb, uint32_t mb_type, | |
| 36 | int dst_x, int dst_y, | ||
| 37 | int motion_x, int motion_y, int motion_scale, | ||
| 38 | int direction) | ||
| 39 | { | ||
| 40 |
4/4✓ Branch 0 taken 3381 times.
✓ Branch 1 taken 1684 times.
✓ Branch 2 taken 48 times.
✓ Branch 3 taken 3333 times.
|
5065 | mb->w = IS_8X8(mb_type) || IS_8X16(mb_type) ? 8 : 16; |
| 41 |
4/4✓ Branch 0 taken 3381 times.
✓ Branch 1 taken 1684 times.
✓ Branch 2 taken 86 times.
✓ Branch 3 taken 3295 times.
|
5065 | mb->h = IS_8X8(mb_type) || IS_16X8(mb_type) ? 8 : 16; |
| 42 | 5065 | mb->motion_x = motion_x; | |
| 43 | 5065 | mb->motion_y = motion_y; | |
| 44 | 5065 | mb->motion_scale = motion_scale; | |
| 45 | 5065 | mb->dst_x = dst_x; | |
| 46 | 5065 | mb->dst_y = dst_y; | |
| 47 | 5065 | mb->src_x = dst_x + motion_x / motion_scale; | |
| 48 | 5065 | mb->src_y = dst_y + motion_y / motion_scale; | |
| 49 |
2/2✓ Branch 0 taken 1480 times.
✓ Branch 1 taken 3585 times.
|
5065 | mb->source = direction ? 1 : -1; |
| 50 | 5065 | mb->flags = 0; // XXX: does mb_type contain extra information that could be exported here? | |
| 51 | 5065 | return 1; | |
| 52 | } | ||
| 53 | |||
| 54 | 218218 | void ff_draw_horiz_band(AVCodecContext *avctx, | |
| 55 | const AVFrame *cur, const AVFrame *last, | ||
| 56 | int y, int h, int picture_structure, | ||
| 57 | int first_field, int low_delay) | ||
| 58 | { | ||
| 59 | 218218 | const int field_pic = picture_structure != PICT_FRAME; | |
| 60 | const AVPixFmtDescriptor *desc; | ||
| 61 | const AVFrame *src; | ||
| 62 | int offset[AV_NUM_DATA_POINTERS]; | ||
| 63 | |||
| 64 |
2/2✓ Branch 0 taken 214868 times.
✓ Branch 1 taken 3350 times.
|
218218 | if (!avctx->draw_horiz_band) |
| 65 | 214868 | return; | |
| 66 | |||
| 67 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3350 times.
|
3350 | if (field_pic) { |
| 68 | ✗ | h <<= 1; | |
| 69 | ✗ | y <<= 1; | |
| 70 | } | ||
| 71 | |||
| 72 | 3350 | h = FFMIN(h, avctx->height - y); | |
| 73 | |||
| 74 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 3350 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
3350 | if (field_pic && first_field && |
| 75 | ✗ | !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) | |
| 76 | ✗ | return; | |
| 77 | |||
| 78 |
2/4✓ Branch 0 taken 3350 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3350 times.
|
3350 | if (cur->pict_type == AV_PICTURE_TYPE_B || low_delay || |
| 79 | ✗ | (avctx->slice_flags & SLICE_FLAG_CODED_ORDER)) | |
| 80 | 3350 | src = cur; | |
| 81 | ✗ | else if (last) | |
| 82 | ✗ | src = last; | |
| 83 | else | ||
| 84 | ✗ | return; | |
| 85 | |||
| 86 | 3350 | desc = av_pix_fmt_desc_get(avctx->pix_fmt); | |
| 87 | |||
| 88 | 3350 | offset[0] = y * src->linesize[0]; | |
| 89 | 3350 | offset[1] = | |
| 90 | 3350 | offset[2] = (y >> desc->log2_chroma_h) * src->linesize[1]; | |
| 91 |
2/2✓ Branch 0 taken 16750 times.
✓ Branch 1 taken 3350 times.
|
20100 | for (int i = 3; i < AV_NUM_DATA_POINTERS; i++) |
| 92 | 16750 | offset[i] = 0; | |
| 93 | |||
| 94 | 3350 | emms_c(); | |
| 95 | |||
| 96 | 3350 | avctx->draw_horiz_band(avctx, src, offset, | |
| 97 | y, picture_structure, h); | ||
| 98 | } | ||
| 99 | |||
| 100 | #define HAS_MV_EXT(mb_type, flags, dir) ((mb_type) & flags[(dir)]) | ||
| 101 | |||
| 102 | ✗ | static char get_type_mv_char(int mb_type, const int mb_type_mv_flags[2]) | |
| 103 | { | ||
| 104 | // Type & MV direction | ||
| 105 | ✗ | if (IS_PCM(mb_type)) | |
| 106 | ✗ | return 'P'; | |
| 107 | ✗ | else if (IS_ACPRED(mb_type)) | |
| 108 | ✗ | return 'A'; | |
| 109 | ✗ | else if (IS_INTRA4x4(mb_type)) | |
| 110 | ✗ | return 'i'; | |
| 111 | ✗ | else if (IS_INTRA16x16(mb_type)) | |
| 112 | ✗ | return 'I'; | |
| 113 | ✗ | else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type)) | |
| 114 | ✗ | return 'd'; | |
| 115 | ✗ | else if (IS_DIRECT(mb_type)) | |
| 116 | ✗ | return 'D'; | |
| 117 | ✗ | else if (IS_GMC(mb_type) && IS_SKIP(mb_type)) | |
| 118 | ✗ | return 'g'; | |
| 119 | ✗ | else if (IS_GMC(mb_type)) | |
| 120 | ✗ | return 'G'; | |
| 121 | ✗ | else if (IS_SKIP(mb_type)) | |
| 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 { | ||
| 128 | av_assert2(HAS_MV_EXT(mb_type, 0, mb_type_mv_flags) && HAS_MV_EXT(mb_type, 1, mb_type_mv_flags)); | ||
| 129 | ✗ | return 'X'; | |
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | ✗ | static char get_segmentation_char(int mb_type) | |
| 134 | { | ||
| 135 | ✗ | if (IS_8X8(mb_type)) | |
| 136 | ✗ | return '+'; | |
| 137 | ✗ | else if (IS_16X8(mb_type)) | |
| 138 | ✗ | return '-'; | |
| 139 | ✗ | else if (IS_8X16(mb_type)) | |
| 140 | ✗ | return '|'; | |
| 141 | ✗ | else if (IS_INTRA(mb_type) || IS_16X16(mb_type)) | |
| 142 | ✗ | return ' '; | |
| 143 | |||
| 144 | ✗ | return '?'; | |
| 145 | } | ||
| 146 | |||
| 147 | ✗ | static char get_interlacement_char(int mb_type) | |
| 148 | { | ||
| 149 | ✗ | if (IS_INTERLACED(mb_type)) | |
| 150 | ✗ | return '='; | |
| 151 | else | ||
| 152 | ✗ | return ' '; | |
| 153 | } | ||
| 154 | |||
| 155 | 35589 | void ff_print_debug_info2(AVCodecContext *avctx, AVFrame *pict, | |
| 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 | { | ||
| 160 | 35589 | const int is_h264 = avctx->codec_id == AV_CODEC_ID_H264; | |
| 161 |
2/2✓ Branch 0 taken 24372 times.
✓ Branch 1 taken 11217 times.
|
35589 | const int mb_type_mv_flags[2] = { is_h264 ? MB_TYPE_L0 : MB_TYPE_FORWARD_MV, |
| 162 |
2/2✓ Branch 0 taken 24372 times.
✓ Branch 1 taken 11217 times.
|
35589 | is_h264 ? MB_TYPE_L1 : MB_TYPE_BACKWARD_MV }; |
| 163 | |||
| 164 |
4/6✓ Branch 0 taken 69 times.
✓ Branch 1 taken 35520 times.
✓ Branch 2 taken 69 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 69 times.
✗ Branch 5 not taken.
|
35589 | if ((avctx->export_side_data & AV_CODEC_EXPORT_DATA_MVS) && mbtype_table && motion_val[0]) { |
| 165 | 69 | const int shift = 1 + quarter_sample; | |
| 166 | 69 | const int scale = 1 << shift; | |
| 167 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 69 times.
|
69 | const int mv_sample_log2 = is_h264 ? 2 : 1; |
| 168 | 69 | const int mv_stride = (mb_width << mv_sample_log2) + !is_h264; | |
| 169 | 69 | 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) */ | ||
| 173 | 69 | AVMotionVector *mvs = av_malloc_array(mb_width * mb_height, 2 * 4 * sizeof(AVMotionVector)); | |
| 174 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 69 times.
|
69 | if (!mvs) |
| 175 | ✗ | return; | |
| 176 | |||
| 177 |
2/2✓ Branch 0 taken 1366 times.
✓ Branch 1 taken 69 times.
|
1435 | for (mb_y = 0; mb_y < mb_height; mb_y++) { |
| 178 |
2/2✓ Branch 0 taken 47412 times.
✓ Branch 1 taken 1366 times.
|
48778 | for (mb_x = 0; mb_x < mb_width; mb_x++) { |
| 179 | 47412 | int i, direction, mb_type = mbtype_table[mb_x + mb_y * mb_stride]; | |
| 180 |
2/2✓ Branch 0 taken 94824 times.
✓ Branch 1 taken 47412 times.
|
142236 | for (direction = 0; direction < 2; direction++) { |
| 181 |
2/2✓ Branch 0 taken 91089 times.
✓ Branch 1 taken 3735 times.
|
94824 | if (!HAS_MV_EXT(mb_type, direction, mb_type_mv_flags)) |
| 182 | 91089 | continue; | |
| 183 |
2/2✓ Branch 0 taken 421 times.
✓ Branch 1 taken 3314 times.
|
3735 | if (IS_8X8(mb_type)) { |
| 184 |
2/2✓ Branch 0 taken 1684 times.
✓ Branch 1 taken 421 times.
|
2105 | for (i = 0; i < 4; i++) { |
| 185 | 1684 | int sx = mb_x * 16 + 4 + 8 * (i & 1); | |
| 186 | 1684 | int sy = mb_y * 16 + 4 + 8 * (i >> 1); | |
| 187 | 1684 | int xy = (mb_x * 2 + (i & 1) + | |
| 188 | 1684 | (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1); | |
| 189 | 1684 | int mx = motion_val[direction][xy][0]; | |
| 190 | 1684 | int my = motion_val[direction][xy][1]; | |
| 191 | 1684 | mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, scale, direction); | |
| 192 | } | ||
| 193 |
2/2✓ Branch 0 taken 43 times.
✓ Branch 1 taken 3271 times.
|
3314 | } else if (IS_16X8(mb_type)) { |
| 194 |
2/2✓ Branch 0 taken 86 times.
✓ Branch 1 taken 43 times.
|
129 | for (i = 0; i < 2; i++) { |
| 195 | 86 | int sx = mb_x * 16 + 8; | |
| 196 | 86 | int sy = mb_y * 16 + 4 + 8 * i; | |
| 197 | 86 | int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1); | |
| 198 | 86 | int mx = motion_val[direction][xy][0]; | |
| 199 | 86 | int my = motion_val[direction][xy][1]; | |
| 200 | |||
| 201 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 86 times.
|
86 | if (IS_INTERLACED(mb_type)) |
| 202 | ✗ | my *= 2; | |
| 203 | |||
| 204 | 86 | mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, scale, direction); | |
| 205 | } | ||
| 206 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 3247 times.
|
3271 | } else if (IS_8X16(mb_type)) { |
| 207 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 24 times.
|
72 | for (i = 0; i < 2; i++) { |
| 208 | 48 | int sx = mb_x * 16 + 4 + 8 * i; | |
| 209 | 48 | int sy = mb_y * 16 + 8; | |
| 210 | 48 | int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1); | |
| 211 | 48 | int mx = motion_val[direction][xy][0]; | |
| 212 | 48 | int my = motion_val[direction][xy][1]; | |
| 213 | |||
| 214 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if (IS_INTERLACED(mb_type)) |
| 215 | ✗ | my *= 2; | |
| 216 | |||
| 217 | 48 | mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, scale, direction); | |
| 218 | } | ||
| 219 | } else { | ||
| 220 | 3247 | int sx = mb_x * 16 + 8; | |
| 221 | 3247 | int sy = mb_y * 16 + 8; | |
| 222 | 3247 | int xy = (mb_x + mb_y * mv_stride) << mv_sample_log2; | |
| 223 | 3247 | int mx = motion_val[direction][xy][0]; | |
| 224 | 3247 | int my = motion_val[direction][xy][1]; | |
| 225 | 3247 | mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, scale, direction); | |
| 226 | } | ||
| 227 | } | ||
| 228 | } | ||
| 229 | } | ||
| 230 | |||
| 231 |
2/2✓ Branch 0 taken 35 times.
✓ Branch 1 taken 34 times.
|
69 | if (mbcount) { |
| 232 | AVFrameSideData *sd; | ||
| 233 | |||
| 234 | 35 | av_log(avctx, AV_LOG_DEBUG, "Adding %d MVs info to frame %"PRId64"\n", mbcount, avctx->frame_num); | |
| 235 | 35 | sd = av_frame_new_side_data(pict, AV_FRAME_DATA_MOTION_VECTORS, mbcount * sizeof(AVMotionVector)); | |
| 236 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
|
35 | if (!sd) { |
| 237 | ✗ | av_freep(&mvs); | |
| 238 | ✗ | return; | |
| 239 | } | ||
| 240 | 35 | memcpy(sd->data, mvs, mbcount * sizeof(AVMotionVector)); | |
| 241 | } | ||
| 242 | |||
| 243 | 69 | av_freep(&mvs); | |
| 244 | } | ||
| 245 | |||
| 246 | /* TODO: export all the following to make them accessible for users (and filters) */ | ||
| 247 |
2/4✓ Branch 0 taken 35589 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 35589 times.
|
35589 | if (avctx->hwaccel || !mbtype_table) |
| 248 | ✗ | return; | |
| 249 | |||
| 250 | |||
| 251 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 35589 times.
|
35589 | if (avctx->debug & (FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) { |
| 252 | int x,y; | ||
| 253 | AVBPrint buf; | ||
| 254 | int n; | ||
| 255 | int margin_left; | ||
| 256 | int x_step; | ||
| 257 | |||
| 258 | ✗ | av_log(avctx, AV_LOG_DEBUG, "New frame, type: %c\n", | |
| 259 | ✗ | av_get_picture_type_char(pict->pict_type)); | |
| 260 | |||
| 261 | ✗ | margin_left = 2; | |
| 262 | ✗ | n = mb_width << 4; | |
| 263 | ✗ | while ((n /= 10)) | |
| 264 | ✗ | margin_left++; | |
| 265 | |||
| 266 | ✗ | av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED); | |
| 267 | ✗ | av_bprint_chars(&buf, ' ', margin_left); | |
| 268 | |||
| 269 | ✗ | n = 0; | |
| 270 | ✗ | if (avctx->debug & FF_DEBUG_QP) | |
| 271 | ✗ | n += 2; | |
| 272 | ✗ | if (avctx->debug & FF_DEBUG_MB_TYPE) | |
| 273 | ✗ | n += 3; | |
| 274 | ✗ | x_step = (mb_width * 16 > 999) ? 8 : 4; | |
| 275 | ✗ | for (x = 0; x < mb_width; x += x_step) | |
| 276 | ✗ | av_bprintf(&buf, "%-*d", n * x_step, x << 4); | |
| 277 | |||
| 278 | ✗ | av_log(avctx, AV_LOG_DEBUG, "%s\n", buf.str); | |
| 279 | |||
| 280 | ✗ | for (y = 0; y < mb_height; y++) { | |
| 281 | ✗ | av_bprint_clear(&buf); | |
| 282 | ✗ | for (x = 0; x < mb_width; x++) { | |
| 283 | ✗ | if (x == 0) | |
| 284 | ✗ | av_bprintf(&buf, "%*d ", margin_left - 1, y << 4); | |
| 285 | ✗ | if (avctx->debug & FF_DEBUG_QP) { | |
| 286 | ✗ | av_bprintf(&buf, "%2d", qscale_table[x + y * mb_stride]); | |
| 287 | } | ||
| 288 | ✗ | if (avctx->debug & FF_DEBUG_MB_TYPE) { | |
| 289 | ✗ | int mb_type = mbtype_table[x + y * mb_stride]; | |
| 290 | |||
| 291 | ✗ | av_bprintf(&buf, "%c%c%c", | |
| 292 | ✗ | get_type_mv_char(mb_type, mb_type_mv_flags), | |
| 293 | ✗ | get_segmentation_char(mb_type), | |
| 294 | ✗ | get_interlacement_char(mb_type)); | |
| 295 | } | ||
| 296 | } | ||
| 297 | |||
| 298 | ✗ | av_log(avctx, AV_LOG_DEBUG, "%s\n", buf.str); | |
| 299 | } | ||
| 300 | ✗ | av_bprint_finalize(&buf, NULL); | |
| 301 | } | ||
| 302 | } | ||
| 303 |