This involves computing the approximate location of the desired index table segment and linearly searching from there.
Unified · Split
| @@ -262,6 +262,7 @@ typedef struct MXFIndexTableSegment { · 261 unmodified lines | |||
| 262 | int *flag_entries; | 262 | int *flag_entries; |
| 263 | uint64_t *stream_offset_entries; | 263 | uint64_t *stream_offset_entries; |
| 264 | int nb_index_entries; | 264 | int nb_index_entries; |
| 265 | int64_t offset; | ||
| 265 | } MXFIndexTableSegment; | 266 | } MXFIndexTableSegment; |
| 266 | 267 | ||
| 267 | typedef struct MXFPackage { | 268 | typedef struct MXFPackage { |
| @@ -1899,18 +1900,44 @@ static int64_t mxf_essence_container_end(MXFContext *mxf, int body_sid) · 1631 unmodified lines | |||
| 1899 | /* EditUnit -> absolute offset */ | 1900 | /* EditUnit -> absolute offset */ |
| 1900 | 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) | 1901 | 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) |
| 1901 | { | 1902 | { |
| 1902 | int i; | 1903 | int i = 0; |
| 1903 | int64_t offset_temp = 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 | } | ||
| 1904 | 1911 | ||
| 1905 | edit_unit = av_rescale_q(edit_unit, index_table->segments[0]->index_edit_rate, edit_rate); | 1912 | edit_unit = av_rescale_q(edit_unit, index_table->segments[0]->index_edit_rate, edit_rate); |
| 1906 | 1913 | ||
| 1907 | for (i = 0; i < index_table->nb_segments; i++) { | 1914 | first_segment = index_table->segments[0]; |
| 1908 | MXFIndexTableSegment *s = index_table->segments[i]; | 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 | } | ||
| 1909 | 1934 | ||
| 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]; | ||
| 1911 | 1937 | ||
| 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) { |
| 1913 | int64_t index = edit_unit - s->index_start_position; | 1939 | int64_t index = edit_unit - s->index_start_position; |
| 1940 | int64_t offset_temp = s->offset; | ||
| 1914 | 1941 | ||
| 1915 | if (s->edit_unit_byte_count) { | 1942 | if (s->edit_unit_byte_count) { |
| 1916 | if (index > INT64_MAX / s->edit_unit_byte_count || | 1943 | 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 | |||
| 1935 | *edit_unit_out = av_rescale_q(edit_unit, edit_rate, s->index_edit_rate); | 1962 | *edit_unit_out = av_rescale_q(edit_unit, edit_rate, s->index_edit_rate); |
| 1936 | 1963 | ||
| 1937 | return mxf_absolute_bodysid_offset(mxf, index_table->body_sid, offset_temp, offset_out, partition_out); | 1964 | 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--; | ||
| 1938 | } else { | 1968 | } else { |
| 1939 | /* EditUnitByteCount == 0 for VBR indexes, which is fine since they use explicit StreamOffsets */ | 1969 | // edit_unit >= s->index_start_position + s->index_duration |
| 1940 | if (s->edit_unit_byte_count && (s->index_duration > INT64_MAX / s->edit_unit_byte_count || | 1970 | i++; |
| 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; | ||
| 1946 | } | 1971 | } |
| 1947 | } | 1972 | } |
| 1948 | 1973 | ||
| @@ -2127,6 +2152,7 @@ static int mxf_compute_index_tables(MXFContext *mxf) · 178 unmodified lines | |||
| 2127 | for (int i = 0, j = 0; j < mxf->nb_index_tables; i += mxf->index_tables[j++].nb_segments) { | 2152 | for (int i = 0, j = 0; j < mxf->nb_index_tables; i += mxf->index_tables[j++].nb_segments) { |
| 2128 | MXFIndexTable *t = &mxf->index_tables[j]; | 2153 | MXFIndexTable *t = &mxf->index_tables[j]; |
| 2129 | MXFTrack *mxf_track = NULL; | 2154 | MXFTrack *mxf_track = NULL; |
| 2155 | int64_t offset_temp = 0; | ||
| 2130 | 2156 | ||
| 2131 | t->segments = av_calloc(t->nb_segments, sizeof(*t->segments)); | 2157 | t->segments = av_calloc(t->nb_segments, sizeof(*t->segments)); |
| 2132 | if (!t->segments) { | 2158 | if (!t->segments) { |
| @@ -2155,8 +2181,10 @@ static int mxf_compute_index_tables(MXFContext *mxf) · 22 unmodified lines | |||
| 2155 | } | 2181 | } |
| 2156 | } | 2182 | } |
| 2157 | 2183 | ||
| 2158 | /* fix zero IndexDurations */ | 2184 | /* fix zero IndexDurations and compute segment offsets */ |
| 2159 | for (int k = 0; k < t->nb_segments; k++) { | 2185 | for (int k = 0; k < t->nb_segments; k++) { |
| 2186 | MXFIndexTableSegment *s = t->segments[k]; | ||
| 2187 | |||
| 2160 | if (!t->segments[k]->index_edit_rate.num || !t->segments[k]->index_edit_rate.den) { | 2188 | if (!t->segments[k]->index_edit_rate.num || !t->segments[k]->index_edit_rate.den) { |
| 2161 | av_log(mxf->fc, AV_LOG_WARNING, "IndexSID %i segment %i has invalid IndexEditRate\n", | 2189 | av_log(mxf->fc, AV_LOG_WARNING, "IndexSID %i segment %i has invalid IndexEditRate\n", |
| 2162 | t->index_sid, k); | 2190 | t->index_sid, k); |
| @@ -2164,6 +2192,17 @@ static int mxf_compute_index_tables(MXFContext *mxf) · 1 unmodified line | |||
| 2164 | t->segments[k]->index_edit_rate = mxf_track->edit_rate; | 2192 | t->segments[k]->index_edit_rate = mxf_track->edit_rate; |
| 2165 | } | 2193 | } |
| 2166 | 2194 | ||
| 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 | |||
| 2167 | if (t->segments[k]->index_duration) | 2206 | if (t->segments[k]->index_duration) |
| 2168 | continue; | 2207 | continue; |
| 2169 | 2208 | ||