| @@ -30,6 +30,7 @@ · 29 unmodified lines |
| 30 | 30 | #include "libavutil/stereo3d.h" |
| 31 | 31 | #include "libavutil/time.h" |
| 32 | 32 | #include "libavutil/intreadwrite.h" |
| 33 | +#include "libavutil/video_hint.h" |
| 33 | 34 | #include "avcodec.h" |
| 34 | 35 | #include "codec_internal.h" |
| 35 | 36 | #include "encode.h" |
| @@ -48,6 +49,9 @@ · 12 unmodified lines |
| 48 | 49 | // from x264.h, for quant_offsets, Macroblocks are 16x16 |
| 49 | 50 | // blocks of pixels (with respect to the luma plane) |
| 50 | 51 | #define MB_SIZE 16 |
| 52 | +#define MB_LSIZE 4 |
| 53 | +#define MB_FLOOR(x) ((x) >> (MB_LSIZE)) |
| 54 | +#define MB_CEIL(x) MB_FLOOR((x) + (MB_SIZE - 1)) |
| 51 | 55 | |
| 52 | 56 | typedef struct X264Opaque { |
| 53 | 57 | #if FF_API_REORDERED_OPAQUE |
| @@ -123,6 +127,8 @@ typedef struct X264Context { · 69 unmodified lines |
| 123 | 127 | * encounter a frame with ROI side data. |
| 124 | 128 | */ |
| 125 | 129 | int roi_warned; |
| 130 | + |
| 131 | + int mb_info; |
| 126 | 132 | } X264Context; |
| 127 | 133 | |
| 128 | 134 | static void X264_log(void *p, int level, const char *fmt, va_list args) |
| @@ -295,6 +301,7 @@ static void free_picture(x264_picture_t *pic) · 166 unmodified lines |
| 295 | 301 | av_free(pic->extra_sei.payloads[i].payload); |
| 296 | 302 | av_freep(&pic->extra_sei.payloads); |
| 297 | 303 | av_freep(&pic->prop.quant_offsets); |
| 304 | + av_freep(&pic->prop.mb_info); |
| 298 | 305 | pic->extra_sei.num_payloads = 0; |
| 299 | 306 | } |
| 300 | 307 | |
| @@ -320,6 +327,74 @@ static enum AVPixelFormat csp_to_pixfmt(int csp) · 19 unmodified lines |
| 320 | 327 | return AV_PIX_FMT_NONE; |
| 321 | 328 | } |
| 322 | 329 | |
| 330 | +static void av_always_inline mbinfo_compute_changed_coords(const AVVideoRect *rect, |
| 331 | + int *min_x, |
| 332 | + int *max_x, |
| 333 | + int *min_y, |
| 334 | + int *max_y) |
| 335 | +{ |
| 336 | + *min_y = MB_FLOOR(rect->y); |
| 337 | + *max_y = MB_CEIL(rect->y + rect->height); |
| 338 | + *min_x = MB_FLOOR(rect->x); |
| 339 | + *max_x = MB_CEIL(rect->x + rect->width); |
| 340 | +} |
| 341 | + |
| 342 | +static void av_always_inline mbinfo_compute_constant_coords(const AVVideoRect *rect, |
| 343 | + int *min_x, |
| 344 | + int *max_x, |
| 345 | + int *min_y, |
| 346 | + int *max_y) |
| 347 | +{ |
| 348 | + *min_y = MB_CEIL(rect->y); |
| 349 | + *max_y = MB_FLOOR(rect->y + rect->height); |
| 350 | + *min_x = MB_CEIL(rect->x); |
| 351 | + *max_x = MB_FLOOR(rect->x + rect->width); |
| 352 | +} |
| 353 | + |
| 354 | +static int setup_mb_info(AVCodecContext *ctx, x264_picture_t *pic, |
| 355 | + const AVFrame *frame, |
| 356 | + const AVVideoHint *info) |
| 357 | +{ |
| 358 | + int mb_width = (frame->width + MB_SIZE - 1) / MB_SIZE; |
| 359 | + int mb_height = (frame->height + MB_SIZE - 1) / MB_SIZE; |
| 360 | + |
| 361 | + const AVVideoRect *mbinfo_rects; |
| 362 | + int nb_rects; |
| 363 | + uint8_t *mbinfo; |
| 364 | + |
| 365 | + mbinfo_rects = (const AVVideoRect *)av_video_hint_rects(info); |
| 366 | + nb_rects = info->nb_rects; |
| 367 | + |
| 368 | + mbinfo = av_calloc(mb_width * mb_height, sizeof(*mbinfo)); |
| 369 | + if (!mbinfo) |
| 370 | + return AVERROR(ENOMEM); |
| 371 | + |
| 372 | +#define COMPUTE_MBINFO(mbinfo_filler_, mbinfo_marker_, compute_coords_fn_) \ |
| 373 | + memset(mbinfo, mbinfo_filler_, sizeof(*mbinfo) * mb_width * mb_height); \ |
| 374 | + \ |
| 375 | + for (int i = 0; i < nb_rects; i++) { \ |
| 376 | + int min_x, max_x, min_y, max_y; \ |
| 377 | + \ |
| 378 | + compute_coords_fn_(mbinfo_rects, &min_x, &max_x, &min_y, &max_y); \ |
| 379 | + for (int mb_y = min_y; mb_y < max_y; ++mb_y) { \ |
| 380 | + memset(mbinfo + mb_y * mb_width + min_x, mbinfo_marker_, max_x - min_x); \ |
| 381 | + } \ |
| 382 | + \ |
| 383 | + mbinfo_rects++; \ |
| 384 | + } \ |
| 385 | + |
| 386 | + if (info->type == AV_VIDEO_HINT_TYPE_CHANGED) { |
| 387 | + COMPUTE_MBINFO(X264_MBINFO_CONSTANT, 0, mbinfo_compute_changed_coords); |
| 388 | + } else /* if (info->type == AV_VIDEO_HINT_TYPE_CHANGED) */ { |
| 389 | + COMPUTE_MBINFO(0, X264_MBINFO_CONSTANT, mbinfo_compute_constant_coords); |
| 390 | + } |
| 391 | + |
| 392 | + pic->prop.mb_info = mbinfo; |
| 393 | + pic->prop.mb_info_free = av_free; |
| 394 | + |
| 395 | + return 0; |
| 396 | +} |
| 397 | + |
| 323 | 398 | static int setup_roi(AVCodecContext *ctx, x264_picture_t *pic, int bit_depth, |
| 324 | 399 | const AVFrame *frame, const uint8_t *data, size_t size) |
| 325 | 400 | { |
| @@ -404,6 +479,7 @@ static int setup_frame(AVCodecContext *ctx, const AVFrame *frame, · 78 unmodified lines |
| 404 | 479 | int64_t wallclock = 0; |
| 405 | 480 | int bit_depth, ret; |
| 406 | 481 | AVFrameSideData *sd; |
| 482 | + AVFrameSideData *mbinfo_sd; |
| 407 | 483 | |
| 408 | 484 | *ppic = NULL; |
| 409 | 485 | if (!frame) |
| @@ -499,6 +575,17 @@ FF_ENABLE_DEPRECATION_WARNINGS · 89 unmodified lines |
| 499 | 575 | goto fail; |
| 500 | 576 | } |
| 501 | 577 | |
| 578 | + mbinfo_sd = av_frame_get_side_data(frame, AV_FRAME_DATA_VIDEO_HINT); |
| 579 | + if (mbinfo_sd) { |
| 580 | + int ret = setup_mb_info(ctx, pic, frame, (const AVVideoHint *)mbinfo_sd->data); |
| 581 | + if (ret < 0) { |
| 582 | + /* No need to fail here, this is not fatal. We just proceed with no |
| 583 | + * mb_info and log a message */ |
| 584 | + |
| 585 | + av_log(ctx, AV_LOG_WARNING, "setup_mb_info failed with error: %s\n", av_err2str(ret)); |
| 586 | + } |
| 587 | + } |
| 588 | + |
| 502 | 589 | if (x4->udu_sei) { |
| 503 | 590 | for (int j = 0; j < frame->nb_side_data; j++) { |
| 504 | 591 | AVFrameSideData *side_data = frame->side_data[j]; |
| @@ -1102,6 +1189,9 @@ FF_ENABLE_DEPRECATION_WARNINGS · 597 unmodified lines |
| 1102 | 1189 | } |
| 1103 | 1190 | } |
| 1104 | 1191 | |
| 1192 | + x4->params.analyse.b_mb_info = x4->mb_info; |
| 1193 | + x4->params.analyse.b_fast_pskip = 1; |
| 1194 | + |
| 1105 | 1195 | // update AVCodecContext with x264 parameters |
| 1106 | 1196 | avctx->has_b_frames = x4->params.i_bframe ? |
| 1107 | 1197 | x4->params.i_bframe_pyramid ? 2 : 1 : 0; |
| @@ -1311,6 +1401,7 @@ static const AVOption options[] = { · 203 unmodified lines |
| 1311 | 1401 | { "noise_reduction", "Noise reduction", OFFSET(noise_reduction), AV_OPT_TYPE_INT, { .i64 = -1 }, INT_MIN, INT_MAX, VE }, |
| 1312 | 1402 | { "udu_sei", "Use user data unregistered SEI if available", OFFSET(udu_sei), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, |
| 1313 | 1403 | { "x264-params", "Override the x264 configuration using a :-separated list of key=value parameters", OFFSET(x264_params), AV_OPT_TYPE_DICT, { 0 }, 0, 0, VE }, |
| 1404 | + { "mb_info", "Set mb_info data through AVSideData, only useful when used from the API", OFFSET(mb_info), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, |
| 1314 | 1405 | { NULL }, |
| 1315 | 1406 | }; |
| 1316 | 1407 | |