History

lavc/libx264: add mb_info option

Pass the information about unchanged parts of the frame by means of the AVVideoHint side data.

Elias Carotti committed 3 years ago

Commit 418c9541 parent 5012b4a5 files · +100 −1Browse files

Unified · Split

5 files
  1. Changelog +1 −0
  2. doc/APIchanges +3 −0
  3. doc/encoders.texi +4 −0
  4. libavcodec/libx264.c +91 −0
  5. libavcodec/version.h +1 −1
Changelog+1−0
@@ -27,6 +27,7 @@ version <next>: · 26 unmodified lines
2727 - Bitstream filter for converting VVC from MP4 to Annex B
2828 - scale_vt filter for videotoolbox
2929 - transpose_vt filter for videotoolbox
30+- support for the P_SKIP hinting to speed up libx264 encoding
3031
3132 version 6.0:
3233 - Radiance HDR image support
doc/APIchanges+3−0
@@ -2,6 +2,9 @@ The last version increases of all libraries were on 2023-02-09 · 1 unmodified line
22
33 API changes, most recent first:
44
5+2023-08-08 - xxxxxxxxxx - lavc 60.23.100 - libx264.c
6+ Add mb_info option.
7+
58 2023-08-08 - xxxxxxxxxx - lavu 58.15.100 - video_hint.h
69 Add AVVideoHint API.
710
doc/encoders.texi+4−0
@@ -2738,6 +2738,10 @@ Only the mpeg2 and h264 decoders provide these. Default is 1 (on). · 2737 unmodified lines
27382738 @item udu_sei @var{boolean}
27392739 Import user data unregistered SEI if available into output. Default is 0 (off).
27402740
2741+@item mb_info @var{boolean}
2742+Set mb_info data through AVFrameSideData, only useful when used from the
2743+API. Default is 0 (off).
2744+
27412745 @item x264-params (N.A.)
27422746 Override the x264 configuration using a :-separated list of key=value
27432747 parameters.
libavcodec/libx264.c+91−0
@@ -30,6 +30,7 @@ · 29 unmodified lines
3030 #include "libavutil/stereo3d.h"
3131 #include "libavutil/time.h"
3232 #include "libavutil/intreadwrite.h"
33+#include "libavutil/video_hint.h"
3334 #include "avcodec.h"
3435 #include "codec_internal.h"
3536 #include "encode.h"
@@ -48,6 +49,9 @@ · 12 unmodified lines
4849 // from x264.h, for quant_offsets, Macroblocks are 16x16
4950 // blocks of pixels (with respect to the luma plane)
5051 #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))
5155
5256 typedef struct X264Opaque {
5357 #if FF_API_REORDERED_OPAQUE
@@ -123,6 +127,8 @@ typedef struct X264Context { · 69 unmodified lines
123127 * encounter a frame with ROI side data.
124128 */
125129 int roi_warned;
130+
131+ int mb_info;
126132 } X264Context;
127133
128134 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
295301 av_free(pic->extra_sei.payloads[i].payload);
296302 av_freep(&pic->extra_sei.payloads);
297303 av_freep(&pic->prop.quant_offsets);
304+ av_freep(&pic->prop.mb_info);
298305 pic->extra_sei.num_payloads = 0;
299306 }
300307
@@ -320,6 +327,74 @@ static enum AVPixelFormat csp_to_pixfmt(int csp) · 19 unmodified lines
320327 return AV_PIX_FMT_NONE;
321328 }
322329
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+
323398 static int setup_roi(AVCodecContext *ctx, x264_picture_t *pic, int bit_depth,
324399 const AVFrame *frame, const uint8_t *data, size_t size)
325400 {
@@ -404,6 +479,7 @@ static int setup_frame(AVCodecContext *ctx, const AVFrame *frame, · 78 unmodified lines
404479 int64_t wallclock = 0;
405480 int bit_depth, ret;
406481 AVFrameSideData *sd;
482+ AVFrameSideData *mbinfo_sd;
407483
408484 *ppic = NULL;
409485 if (!frame)
@@ -499,6 +575,17 @@ FF_ENABLE_DEPRECATION_WARNINGS · 89 unmodified lines
499575 goto fail;
500576 }
501577
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+
502589 if (x4->udu_sei) {
503590 for (int j = 0; j < frame->nb_side_data; j++) {
504591 AVFrameSideData *side_data = frame->side_data[j];
@@ -1102,6 +1189,9 @@ FF_ENABLE_DEPRECATION_WARNINGS · 597 unmodified lines
11021189 }
11031190 }
11041191
1192+ x4->params.analyse.b_mb_info = x4->mb_info;
1193+ x4->params.analyse.b_fast_pskip = 1;
1194+
11051195 // update AVCodecContext with x264 parameters
11061196 avctx->has_b_frames = x4->params.i_bframe ?
11071197 x4->params.i_bframe_pyramid ? 2 : 1 : 0;
@@ -1311,6 +1401,7 @@ static const AVOption options[] = { · 203 unmodified lines
13111401 { "noise_reduction", "Noise reduction", OFFSET(noise_reduction), AV_OPT_TYPE_INT, { .i64 = -1 }, INT_MIN, INT_MAX, VE },
13121402 { "udu_sei", "Use user data unregistered SEI if available", OFFSET(udu_sei), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
13131403 { "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 },
13141405 { NULL },
13151406 };
13161407
libavcodec/version.h+1−1
@@ -29,7 +29,7 @@ · 28 unmodified lines
2929
3030 #include "version_major.h"
3131
32−#define LIBAVCODEC_VERSION_MINOR 22
32+#define LIBAVCODEC_VERSION_MINOR 23
3333 #define LIBAVCODEC_VERSION_MICRO 100
3434
3535 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t