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
27- Bitstream filter for converting VVC from MP4 to Annex B27- Bitstream filter for converting VVC from MP4 to Annex B
28- scale_vt filter for videotoolbox28- scale_vt filter for videotoolbox
29- transpose_vt filter for videotoolbox29- transpose_vt filter for videotoolbox
30- support for the P_SKIP hinting to speed up libx264 encoding
30 31
31version 6.0:32version 6.0:
32- Radiance HDR image support33- 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
2 2
3API changes, most recent first:3API changes, most recent first:
4 4
52023-08-08 - xxxxxxxxxx - lavc 60.23.100 - libx264.c
6 Add mb_info option.
7
52023-08-08 - xxxxxxxxxx - lavu 58.15.100 - video_hint.h82023-08-08 - xxxxxxxxxx - lavu 58.15.100 - video_hint.h
6 Add AVVideoHint API.9 Add AVVideoHint API.
7 10
doc/encoders.texi+4−0
@@ -2738,6 +2738,10 @@ Only the mpeg2 and h264 decoders provide these. Default is 1 (on). · 2737 unmodified lines
2738@item udu_sei @var{boolean}2738@item udu_sei @var{boolean}
2739Import user data unregistered SEI if available into output. Default is 0 (off).2739Import user data unregistered SEI if available into output. Default is 0 (off).
2740 2740
2741@item mb_info @var{boolean}
2742Set mb_info data through AVFrameSideData, only useful when used from the
2743API. Default is 0 (off).
2744
2741@item x264-params (N.A.)2745@item x264-params (N.A.)
2742Override the x264 configuration using a :-separated list of key=value2746Override the x264 configuration using a :-separated list of key=value
2743parameters.2747parameters.
libavcodec/libx264.c+91−0
@@ -30,6 +30,7 @@ · 29 unmodified lines
30#include "libavutil/stereo3d.h"30#include "libavutil/stereo3d.h"
31#include "libavutil/time.h"31#include "libavutil/time.h"
32#include "libavutil/intreadwrite.h"32#include "libavutil/intreadwrite.h"
33#include "libavutil/video_hint.h"
33#include "avcodec.h"34#include "avcodec.h"
34#include "codec_internal.h"35#include "codec_internal.h"
35#include "encode.h"36#include "encode.h"
@@ -48,6 +49,9 @@ · 12 unmodified lines
48// from x264.h, for quant_offsets, Macroblocks are 16x1649// from x264.h, for quant_offsets, Macroblocks are 16x16
49// blocks of pixels (with respect to the luma plane)50// blocks of pixels (with respect to the luma plane)
50#define MB_SIZE 1651#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
52typedef struct X264Opaque {56typedef struct X264Opaque {
53#if FF_API_REORDERED_OPAQUE57#if FF_API_REORDERED_OPAQUE
@@ -123,6 +127,8 @@ typedef struct X264Context { · 69 unmodified lines
123 * encounter a frame with ROI side data.127 * encounter a frame with ROI side data.
124 */128 */
125 int roi_warned;129 int roi_warned;
130
131 int mb_info;
126} X264Context;132} X264Context;
127 133
128static void X264_log(void *p, int level, const char *fmt, va_list args)134static 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 av_free(pic->extra_sei.payloads[i].payload);301 av_free(pic->extra_sei.payloads[i].payload);
296 av_freep(&pic->extra_sei.payloads);302 av_freep(&pic->extra_sei.payloads);
297 av_freep(&pic->prop.quant_offsets);303 av_freep(&pic->prop.quant_offsets);
304 av_freep(&pic->prop.mb_info);
298 pic->extra_sei.num_payloads = 0;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 return AV_PIX_FMT_NONE;327 return AV_PIX_FMT_NONE;
321}328}
322 329
330static 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
342static 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
354static 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
323static int setup_roi(AVCodecContext *ctx, x264_picture_t *pic, int bit_depth,398static int setup_roi(AVCodecContext *ctx, x264_picture_t *pic, int bit_depth,
324 const AVFrame *frame, const uint8_t *data, size_t size)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 int64_t wallclock = 0;479 int64_t wallclock = 0;
405 int bit_depth, ret;480 int bit_depth, ret;
406 AVFrameSideData *sd;481 AVFrameSideData *sd;
482 AVFrameSideData *mbinfo_sd;
407 483
408 *ppic = NULL;484 *ppic = NULL;
409 if (!frame)485 if (!frame)
@@ -499,6 +575,17 @@ FF_ENABLE_DEPRECATION_WARNINGS · 89 unmodified lines
499 goto fail;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 if (x4->udu_sei) {589 if (x4->udu_sei) {
503 for (int j = 0; j < frame->nb_side_data; j++) {590 for (int j = 0; j < frame->nb_side_data; j++) {
504 AVFrameSideData *side_data = frame->side_data[j];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 // update AVCodecContext with x264 parameters1195 // update AVCodecContext with x264 parameters
1106 avctx->has_b_frames = x4->params.i_bframe ?1196 avctx->has_b_frames = x4->params.i_bframe ?
1107 x4->params.i_bframe_pyramid ? 2 : 1 : 0;1197 x4->params.i_bframe_pyramid ? 2 : 1 : 0;
@@ -1311,6 +1401,7 @@ static const AVOption options[] = { · 203 unmodified lines
1311 { "noise_reduction", "Noise reduction", OFFSET(noise_reduction), AV_OPT_TYPE_INT, { .i64 = -1 }, INT_MIN, INT_MAX, VE },1401 { "noise_reduction", "Noise reduction", OFFSET(noise_reduction), AV_OPT_TYPE_INT, { .i64 = -1 }, INT_MIN, INT_MAX, VE },
1312 { "udu_sei", "Use user data unregistered SEI if available", OFFSET(udu_sei), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },1402 { "udu_sei", "Use user data unregistered SEI if available", OFFSET(udu_sei), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
1313 { "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 },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 { NULL },1405 { NULL },
1315};1406};
1316 1407
libavcodec/version.h+1−1
@@ -29,7 +29,7 @@ · 28 unmodified lines
29 29
30#include "version_major.h"30#include "version_major.h"
31 31
32#define LIBAVCODEC_VERSION_MINOR 2232#define LIBAVCODEC_VERSION_MINOR 23
33#define LIBAVCODEC_VERSION_MICRO 10033#define LIBAVCODEC_VERSION_MICRO 100
34 34
35#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \35#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