History

lavf/mxfdec: Speed up mxf_edit_unit_absolute_offset()

This involves computing the approximate location of the desired index table segment and linearly searching from there.

Tomas Härdin committed 2 years ago

Commit 649ac171 parent e3fa4691 file · +53 −14Browse files

Unified · Split

libavformat/mxfdec.c+53−14
@@ -262,6 +262,7 @@ typedef struct MXFIndexTableSegment { · 261 unmodified lines
262262 int *flag_entries;
263263 uint64_t *stream_offset_entries;
264264 int nb_index_entries;
265+ int64_t offset;
265266 } MXFIndexTableSegment;
266267
267268 typedef struct MXFPackage {
@@ -1899,18 +1900,44 @@ static int64_t mxf_essence_container_end(MXFContext *mxf, int body_sid) · 1631 unmodified lines
18991900 /* EditUnit -> absolute offset */
19001901 static int mxf_edit_unit_absolute_offset(MXFContext *mxf, MXFIndexTable *index_table, int64_t edit_unit, AVRational edit_rate, int64_t *edit_unit_out, int64_t *offset_out, MXFPartition **partition_out, int nag)
19011902 {
1902− int i;
1903− int64_t offset_temp = 0;
1903+ int i = 0;
1904+ int64_t index_duration, index_end;
1905+ MXFIndexTableSegment *first_segment, *last_segment;
1906+
1907+ if (!index_table->nb_segments) {
1908+ av_log(mxf->fc, AV_LOG_ERROR, "no index table segments\n");
1909+ return AVERROR_INVALIDDATA;
1910+ }
19041911
19051912 edit_unit = av_rescale_q(edit_unit, index_table->segments[0]->index_edit_rate, edit_rate);
19061913
1907− for (i = 0; i < index_table->nb_segments; i++) {
1908− MXFIndexTableSegment *s = index_table->segments[i];
1914+ first_segment = index_table->segments[0];
1915+ last_segment = index_table->segments[index_table->nb_segments - 1];
1916+
1917+ // clamp to actual range of index
1918+ index_end = av_sat_add64(last_segment->index_start_position, last_segment->index_duration);
1919+ edit_unit = FFMAX(FFMIN(edit_unit, index_end), first_segment->index_start_position);
1920+
1921+ // guess which table segment this edit unit is in
1922+ // saturation is fine since it's just a guess
1923+ // if the guess is wrong we revert to a linear search
1924+ index_duration = av_sat_sub64(index_end, first_segment->index_start_position);
1925+
1926+ // compute the guess, taking care not to cause overflow or division by zero
1927+ if (index_duration > 0 && edit_unit <= INT64_MAX / index_table->nb_segments) {
1928+ // a simple linear guesstimate
1929+ // this is accurate to within +-1 when partitions are generated at a constant rate like mxfenc does
1930+ int64_t i64 = index_table->nb_segments * edit_unit / index_duration;
1931+ // clamp and downcast to 32-bit
1932+ i = FFMAX(0, FFMIN(index_table->nb_segments - 1, i64));
1933+ }
19091934
1910− edit_unit = FFMAX(edit_unit, s->index_start_position); /* clamp if trying to seek before start */
1935+ for (; i >= 0 && i < index_table->nb_segments;) {
1936+ MXFIndexTableSegment *s = index_table->segments[i];
19111937
1912− if (edit_unit < s->index_start_position + s->index_duration) {
1938+ if (s->index_start_position <= edit_unit && edit_unit < s->index_start_position + s->index_duration) {
19131939 int64_t index = edit_unit - s->index_start_position;
1940+ int64_t offset_temp = s->offset;
19141941
19151942 if (s->edit_unit_byte_count) {
19161943 if (index > INT64_MAX / s->edit_unit_byte_count ||
@@ -1935,14 +1962,12 @@ static int mxf_edit_unit_absolute_offset(MXFContext *mxf, MXFIndexTable *index_t · 18 unmodified lines
19351962 *edit_unit_out = av_rescale_q(edit_unit, edit_rate, s->index_edit_rate);
19361963
19371964 return mxf_absolute_bodysid_offset(mxf, index_table->body_sid, offset_temp, offset_out, partition_out);
1965+ } else if (edit_unit < s->index_start_position) {
1966+ // the segments are sorted by IndexStartPosition, so this is guaranteed to terminate
1967+ i--;
19381968 } else {
1939− /* EditUnitByteCount == 0 for VBR indexes, which is fine since they use explicit StreamOffsets */
1940− if (s->edit_unit_byte_count && (s->index_duration > INT64_MAX / s->edit_unit_byte_count ||
1941− s->edit_unit_byte_count * s->index_duration > INT64_MAX - offset_temp)
1942− )
1943− return AVERROR_INVALIDDATA;
1944−
1945− offset_temp += s->edit_unit_byte_count * s->index_duration;
1969+ // edit_unit >= s->index_start_position + s->index_duration
1970+ i++;
19461971 }
19471972 }
19481973
@@ -2127,6 +2152,7 @@ static int mxf_compute_index_tables(MXFContext *mxf) · 178 unmodified lines
21272152 for (int i = 0, j = 0; j < mxf->nb_index_tables; i += mxf->index_tables[j++].nb_segments) {
21282153 MXFIndexTable *t = &mxf->index_tables[j];
21292154 MXFTrack *mxf_track = NULL;
2155+ int64_t offset_temp = 0;
21302156
21312157 t->segments = av_calloc(t->nb_segments, sizeof(*t->segments));
21322158 if (!t->segments) {
@@ -2155,8 +2181,10 @@ static int mxf_compute_index_tables(MXFContext *mxf) · 22 unmodified lines
21552181 }
21562182 }
21572183
2158− /* fix zero IndexDurations */
2184+ /* fix zero IndexDurations and compute segment offsets */
21592185 for (int k = 0; k < t->nb_segments; k++) {
2186+ MXFIndexTableSegment *s = t->segments[k];
2187+
21602188 if (!t->segments[k]->index_edit_rate.num || !t->segments[k]->index_edit_rate.den) {
21612189 av_log(mxf->fc, AV_LOG_WARNING, "IndexSID %i segment %i has invalid IndexEditRate\n",
21622190 t->index_sid, k);
@@ -2164,6 +2192,17 @@ static int mxf_compute_index_tables(MXFContext *mxf) · 1 unmodified line
21642192 t->segments[k]->index_edit_rate = mxf_track->edit_rate;
21652193 }
21662194
2195+ s->offset = offset_temp;
2196+
2197+ /* EditUnitByteCount == 0 for VBR indexes, which is fine since they use explicit StreamOffsets */
2198+ if (s->edit_unit_byte_count && (s->index_duration > INT64_MAX / s->edit_unit_byte_count ||
2199+ s->edit_unit_byte_count * s->index_duration > INT64_MAX - offset_temp)) {
2200+ ret = AVERROR_INVALIDDATA;
2201+ goto finish_decoding_index;
2202+ }
2203+
2204+ offset_temp += t->segments[k]->edit_unit_byte_count * t->segments[k]->index_duration;
2205+
21672206 if (t->segments[k]->index_duration)
21682207 continue;
21692208

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