History

lavu: add video_hint API

Add side data type to provide hint to the video encoders about unchanged portions of each frame.

Elias Carotti committed 3 years ago

Commit 5012b4a1 parent c0709706 files · +204 −1Browse files

Unified · Split

6 files
  1. doc/APIchanges +3 −0
  2. libavutil/Makefile +2 −0
  3. libavutil/frame.h +10 −0
  4. libavutil/version.h +1 −1
  5. libavutil/video_hint.c +81 −0
  6. libavutil/video_hint.h +107 −0
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 - lavu 58.15.100 - video_hint.h
6+ Add AVVideoHint API.
7+
58 2023-07-xx - xxxxxxxxxx - lavc 60 - avcodec.h
69 Deprecate AV_CODEC_FLAG_DROPCHANGED without replacement.
710
libavutil/Makefile+2−0
@@ -91,6 +91,7 @@ HEADERS = adler32.h \ · 90 unmodified lines
9191 tea.h \
9292 tx.h \
9393 film_grain_params.h \
94+ video_hint.h
9495
9596 ARCH_HEADERS = bswap.h \
9697 intmath.h \
@@ -181,6 +182,7 @@ OBJS = adler32.o \ · 84 unmodified lines
181182 uuid.o \
182183 version.o \
183184 video_enc_params.o \
185+ video_hint.o \
184186 film_grain_params.o \
185187
186188
libavutil/frame.h+10−0
@@ -214,6 +214,16 @@ enum AVFrameSideDataType { · 213 unmodified lines
214214 * Ambient viewing environment metadata, as defined by H.274.
215215 */
216216 AV_FRAME_DATA_AMBIENT_VIEWING_ENVIRONMENT,
217+
218+ /**
219+ * Provide encoder-specific hinting information about changed/unchanged
220+ * portions of a frame. It can be used to pass information about which
221+ * macroblocks can be skipped because they didn't change from the
222+ * corresponding ones in the previous frame. This could be useful for
223+ * applications which know this information in advance to speed up
224+ * encoding.
225+ */
226+ AV_FRAME_DATA_VIDEO_HINT,
217227 };
218228
219229 enum AVActiveFormatDescription {
libavutil/version.h+1−1
@@ -79,7 +79,7 @@ · 78 unmodified lines
7979 */
8080
8181 #define LIBAVUTIL_VERSION_MAJOR 58
82−#define LIBAVUTIL_VERSION_MINOR 14
82+#define LIBAVUTIL_VERSION_MINOR 15
8383 #define LIBAVUTIL_VERSION_MICRO 100
8484
8585 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
libavutil/video_hint.c+81−0
@@ -0,0 +1,81 @@
1+/*
2+ * Copyright 2023 Elias Carotti <eliascrt at amazon dot it>
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 <string.h>
22+
23+#include "avstring.h"
24+#include "frame.h"
25+#include "macros.h"
26+#include "mem.h"
27+#include "video_hint.h"
28+
29+AVVideoHint *av_video_hint_alloc(size_t nb_rects,
30+ size_t *out_size)
31+{
32+ struct TestStruct {
33+ AVVideoHint hint;
34+ AVVideoRect rect;
35+ };
36+ const size_t rect_offset = offsetof(struct TestStruct, rect);
37+ size_t size = rect_offset;
38+ AVVideoHint *hint;
39+
40+ *out_size = 0;
41+ if (nb_rects > (SIZE_MAX - size) / sizeof(AVVideoRect))
42+ return NULL;
43+ size += sizeof(AVVideoRect) * nb_rects;
44+
45+ hint = av_mallocz(size);
46+ if (!hint)
47+ return NULL;
48+
49+ hint->nb_rects = nb_rects;
50+ hint->rect_offset = rect_offset;
51+ hint->rect_size = sizeof(AVVideoRect);
52+
53+ *out_size = size;
54+
55+ return hint;
56+}
57+
58+AVVideoHint *av_video_hint_create_side_data(AVFrame *frame,
59+ size_t nb_rects)
60+{
61+ AVVideoHint *hint;
62+ AVBufferRef *buf;
63+ size_t size = 0;
64+
65+ hint = av_video_hint_alloc(nb_rects, &size);
66+ if (!hint)
67+ return NULL;
68+
69+ buf = av_buffer_create((uint8_t *)hint, size, NULL, NULL, 0);
70+ if (!buf) {
71+ av_freep(&hint);
72+ return NULL;
73+ }
74+
75+ if (!av_frame_new_side_data_from_buf(frame, AV_FRAME_DATA_VIDEO_HINT, buf)) {
76+ av_buffer_unref(&buf);
77+ return NULL;
78+ }
79+
80+ return hint;
81+}
libavutil/video_hint.h+107−0
@@ -0,0 +1,107 @@
1+/**
2+ * Copyright 2023 Elias Carotti <eliascrt at amazon dot it>
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+#ifndef AVUTIL_VIDEO_HINT_H
22+#define AVUTIL_VIDEO_HINT_H
23+
24+#include <stddef.h>
25+#include <stdint.h>
26+#include "libavutil/avassert.h"
27+#include "libavutil/frame.h"
28+
29+typedef struct AVVideoRect {
30+ uint32_t x, y;
31+ uint32_t width, height;
32+} AVVideoRect;
33+
34+typedef enum AVVideoHintType {
35+ /* rectangled delimit the constant areas (unchanged), default is changed */
36+ AV_VIDEO_HINT_TYPE_CONSTANT,
37+
38+ /* rectangled delimit the constant areas (changed), default is not changed */
39+ AV_VIDEO_HINT_TYPE_CHANGED,
40+} AVVideoHintType;
41+
42+typedef struct AVVideoHint {
43+ /**
44+ * Number of AVVideoRect present.
45+ *
46+ * May be 0, in which case no per-rectangle information is present. In this
47+ * case the values of rect_offset / rect_size are unspecified and should
48+ * not be accessed.
49+ */
50+ size_t nb_rects;
51+
52+ /**
53+ * Offset in bytes from the beginning of this structure at which the array
54+ * of AVVideoRect starts.
55+ */
56+ size_t rect_offset;
57+
58+ /**
59+ * Size in bytes of AVVideoRect.
60+ */
61+ size_t rect_size;
62+
63+ AVVideoHintType type;
64+} AVVideoHint;
65+
66+static av_always_inline AVVideoRect *
67+av_video_hint_rects(const AVVideoHint *hints) {
68+ return (AVVideoRect *)((uint8_t *)hints + hints->rect_offset);
69+}
70+
71+static av_always_inline AVVideoRect *
72+av_video_hint_get_rect(const AVVideoHint *hints, size_t idx) {
73+ return (AVVideoRect *)((uint8_t *)hints + hints->rect_offset + idx * hints->rect_size);
74+}
75+
76+/**
77+ * Allocate memory for the AVVideoHint struct along with an nb_rects-sized
78+ * arrays of AVVideoRect.
79+ *
80+ * The side data contains a list of rectangles for the portions of the frame
81+ * which changed from the last encoded one (and the remainder are assumed to be
82+ * changed), or, alternately (depending on the type parameter) the unchanged
83+ * ones (and the remanining ones are those which changed).
84+ * Macroblocks will thus be hinted either to be P_SKIP-ped or go through the
85+ * regular encoding procedure.
86+ *
87+ * It's responsibility of the caller to fill the AVRects accordingly, and to set
88+ * the proper AVVideoHintType field.
89+ *
90+ * @param out_size if non-NULL, the size in bytes of the resulting data array is
91+ * written here
92+ *
93+ * @return newly allocated AVVideoHint struct (must be freed by the caller using
94+ * av_free()) on success, NULL on memory allocation failure
95+ */
96+AVVideoHint *av_video_hint_alloc(size_t nb_rects,
97+ size_t *out_size);
98+
99+/**
100+ * Same as av_video_hint_alloc(), except newly-allocated AVVideoHint is attached
101+ * as side data of type AV_FRAME_DATA_VIDEO_HINT_INFO to frame.
102+ */
103+AVVideoHint *av_video_hint_create_side_data(AVFrame *frame,
104+ size_t nb_rects);
105+
106+
107+#endif /* AVUTIL_VIDEO_HINT_H */

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