History

lavc: add a decoder option for configuring side data preference

This and the following commits fix #10857

Anton Khirnov committed 2 years ago

Commit 68a8eca1 parent efe44785 files · +85 −1Browse files

Unified · Split

5 files
  1. doc/APIchanges +3 −0
  2. libavcodec/avcodec.h +34 −0
  3. libavcodec/decode.c +35 −0
  4. libavcodec/options_table.h +12 −0
  5. libavcodec/version.h +1 −1
doc/APIchanges+3−0
@@ -2,6 +2,9 @@ The last version increases of all libraries were on 2024-03-07 · 1 unmodified line
22
33 API changes, most recent first:
44
5+2024-03-08 - xxxxxxxxxx - lavc 61.1.100 - avcodec.h
6+ Add AVCodecContext.[nb_]side_data_prefer_packet.
7+
58 2024-03-08 - xxxxxxxxxx - lavu 59.1.100 - opt.h
69 Add AV_OPT_TYPE_FLAG_ARRAY and AVOptionArrayDef.
710
libavcodec/avcodec.h+34−0
@@ -2028,6 +2028,40 @@ typedef struct AVCodecContext { · 2027 unmodified lines
20282028 * an error.
20292029 */
20302030 int64_t frame_num;
2031+
2032+ /**
2033+ * Decoding only. May be set by the caller before avcodec_open2() to an
2034+ * av_malloc()'ed array (or via AVOptions). Owned and freed by the decoder
2035+ * afterwards.
2036+ *
2037+ * Side data attached to decoded frames may come from several sources:
2038+ * 1. coded_side_data, which the decoder will for certain types translate
2039+ * from packet-type to frame-type and attach to frames;
2040+ * 2. side data attached to an AVPacket sent for decoding (same
2041+ * considerations as above);
2042+ * 3. extracted from the coded bytestream.
2043+ * The first two cases are supplied by the caller and typically come from a
2044+ * container.
2045+ *
2046+ * This array configures decoder behaviour in cases when side data of the
2047+ * same type is present both in the coded bytestream and in the
2048+ * user-supplied side data (items 1. and 2. above). In all cases, at most
2049+ * one instance of each side data type will be attached to output frames. By
2050+ * default it will be the bytestream side data. Adding an
2051+ * AVPacketSideDataType value to this array will flip the preference for
2052+ * this type, thus making the decoder prefer user-supplied side data over
2053+ * bytestream. In case side data of the same type is present both in
2054+ * coded_data and attacked to a packet, the packet instance always has
2055+ * priority.
2056+ *
2057+ * The array may also contain a single -1, in which case the preference is
2058+ * switched for all side data types.
2059+ */
2060+ int *side_data_prefer_packet;
2061+ /**
2062+ * Number of entries in side_data_prefer_packet.
2063+ */
2064+ unsigned nb_side_data_prefer_packet;
20312065 } AVCodecContext;
20322066
20332067 /**
libavcodec/decode.c+35−0
@@ -65,6 +65,12 @@ typedef struct DecodeContext { · 64 unmodified lines
6565 int64_t pts_correction_num_faulty_dts; /// Number of incorrect DTS values so far
6666 int64_t pts_correction_last_pts; /// PTS of the last frame
6767 int64_t pts_correction_last_dts; /// DTS of the last frame
68+
69+ /**
70+ * Bitmask indicating for which side data types we prefer user-supplied
71+ * (global or attached to packets) side data over bytestream.
72+ */
73+ uint64_t side_data_pref_mask;
6874 } DecodeContext;
6975
7076 static DecodeContext *decode_ctx(AVCodecInternal *avci)
@@ -1739,6 +1745,35 @@ int ff_decode_preinit(AVCodecContext *avctx) · 1668 unmodified lines
17391745 avctx->export_side_data |= AV_CODEC_EXPORT_DATA_MVS;
17401746 }
17411747
1748+ if (avctx->nb_side_data_prefer_packet == 1 &&
1749+ avctx->side_data_prefer_packet[0] == -1)
1750+ dc->side_data_pref_mask = ~0ULL;
1751+ else {
1752+ for (unsigned i = 0; i < avctx->nb_side_data_prefer_packet; i++) {
1753+ int val = avctx->side_data_prefer_packet[i];
1754+
1755+ if (val < 0 || val >= AV_PKT_DATA_NB) {
1756+ av_log(avctx, AV_LOG_ERROR, "Invalid side data type: %d\n", val);
1757+ return AVERROR(EINVAL);
1758+ }
1759+
1760+ for (unsigned j = 0; j < FF_ARRAY_ELEMS(sd_global_map); j++) {
1761+ if (sd_global_map[j].packet == val) {
1762+ val = sd_global_map[j].frame;
1763+
1764+ // this code will need to be changed when we have more than
1765+ // 64 frame side data types
1766+ if (val >= 64) {
1767+ av_log(avctx, AV_LOG_ERROR, "Side data type too big\n");
1768+ return AVERROR_BUG;
1769+ }
1770+
1771+ dc->side_data_pref_mask |= 1ULL << val;
1772+ }
1773+ }
1774+ }
1775+ }
1776+
17421777 avci->in_pkt = av_packet_alloc();
17431778 avci->last_pkt_props = av_packet_alloc();
17441779 if (!avci->in_pkt || !avci->last_pkt_props)
libavcodec/options_table.h+12−0
@@ -42,6 +42,8 @@ · 41 unmodified lines
4242 #define D AV_OPT_FLAG_DECODING_PARAM
4343 #define CC AV_OPT_FLAG_CHILD_CONSTS
4444
45+#define AR AV_OPT_TYPE_FLAG_ARRAY
46+
4547 #define AV_CODEC_DEFAULT_BITRATE 200*1000
4648
4749 static const AVOption avcodec_options[] = {
@@ -395,6 +397,16 @@ static const AVOption avcodec_options[] = { · 347 unmodified lines
395397 {"unsafe_output", "allow potentially unsafe hwaccel frame output that might require special care to process successfully", 0, AV_OPT_TYPE_CONST, {.i64 = AV_HWACCEL_FLAG_UNSAFE_OUTPUT }, INT_MIN, INT_MAX, V | D, .unit = "hwaccel_flags"},
396398 {"extra_hw_frames", "Number of extra hardware frames to allocate for the user", OFFSET(extra_hw_frames), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, V|D },
397399 {"discard_damaged_percentage", "Percentage of damaged samples to discard a frame", OFFSET(discard_damaged_percentage), AV_OPT_TYPE_INT, {.i64 = 95 }, 0, 100, V|D },
400+{"side_data_prefer_packet", "Comma-separated list of side data types for which user-supplied (container) data is preferred over coded bytestream",
401+ OFFSET(side_data_prefer_packet), AV_OPT_TYPE_INT | AR, .min = -1, .max = INT_MAX, .flags = V|A|S|D, .unit = "side_data_pkt" },
402+ {"replaygain", .default_val.i64 = AV_PKT_DATA_REPLAYGAIN, .type = AV_OPT_TYPE_CONST, .flags = A|D, .unit = "side_data_pkt" },
403+ {"displaymatrix", .default_val.i64 = AV_PKT_DATA_DISPLAYMATRIX, .type = AV_OPT_TYPE_CONST, .flags = A|D, .unit = "side_data_pkt" },
404+ {"spherical", .default_val.i64 = AV_PKT_DATA_SPHERICAL, .type = AV_OPT_TYPE_CONST, .flags = A|D, .unit = "side_data_pkt" },
405+ {"stereo3d", .default_val.i64 = AV_PKT_DATA_STEREO3D, .type = AV_OPT_TYPE_CONST, .flags = A|D, .unit = "side_data_pkt" },
406+ {"audio_service_type", .default_val.i64 = AV_PKT_DATA_AUDIO_SERVICE_TYPE, .type = AV_OPT_TYPE_CONST, .flags = A|D, .unit = "side_data_pkt" },
407+ {"mastering_display_metadata", .default_val.i64 = AV_PKT_DATA_MASTERING_DISPLAY_METADATA, .type = AV_OPT_TYPE_CONST, .flags = A|D, .unit = "side_data_pkt" },
408+ {"content_light_level", .default_val.i64 = AV_PKT_DATA_CONTENT_LIGHT_LEVEL, .type = AV_OPT_TYPE_CONST, .flags = A|D, .unit = "side_data_pkt" },
409+ {"icc_profile", .default_val.i64 = AV_PKT_DATA_ICC_PROFILE, .type = AV_OPT_TYPE_CONST, .flags = A|D, .unit = "side_data_pkt" },
398410 {NULL},
399411 };
400412
libavcodec/version.h+1−1
@@ -29,7 +29,7 @@ · 28 unmodified lines
2929
3030 #include "version_major.h"
3131
32−#define LIBAVCODEC_VERSION_MINOR 0
32+#define LIBAVCODEC_VERSION_MINOR 1
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