Lines Matching refs:sc
197 MOVStreamContext *sc;
211 sc = av_mallocz(sizeof(*sc));
212 if (!sc)
216 av_free(sc);
220 st->priv_data = sc;
629 MOVStreamContext *sc;
635 sc = st->priv_data;
641 entries >= UINT_MAX / sizeof(*sc->drefs))
644 for (i = 0; i < sc->drefs_count; i++) {
645 MOVDref *dref = &sc->drefs[i];
649 av_free(sc->drefs);
650 sc->drefs_count = 0;
651 sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
652 if (!sc->drefs)
654 sc->drefs_count = entries;
657 MOVDref *dref = &sc->drefs[i];
1362 MOVStreamContext *sc = st->priv_data;
1363 if (sc->has_sidx)
1503 MOVStreamContext *sc;
1512 sc = st->priv_data;
1514 if (sc->time_scale) {
1534 sc->time_scale = avio_rb32(pb);
1535 if (sc->time_scale <= 0) {
1536 av_log(c->fc, AV_LOG_ERROR, "Invalid mdhd time scale %d, defaulting to 1\n", sc->time_scale);
1537 sc->time_scale = 1;
1547 st->time_scale = sc->time_scale;
2114 MOVStreamContext *sc;
2124 sc = st->priv_data;
2134 if (sc->chunk_offsets) {
2138 av_free(sc->chunk_offsets);
2139 sc->chunk_count = 0;
2140 sc->chunk_offsets = av_malloc_array(entries, sizeof(*sc->chunk_offsets));
2141 if (!sc->chunk_offsets)
2143 sc->chunk_count = entries;
2147 sc->chunk_offsets[i] = avio_rb32(pb);
2150 sc->chunk_offsets[i] = avio_rb64(pb);
2154 sc->chunk_count = i;
2206 AVStream *st, MOVStreamContext *sc)
2257 if (ff_get_qtpalette(st->codecpar->codec_id, pb, sc->palette)) {
2259 sc->has_palette = 1;
2264 AVStream *st, MOVStreamContext *sc)
2283 sc->audio_cid = avio_rb16(pb);
2292 (sc->stsd_version == 0 && version > 0)) {
2294 sc->samples_per_frame = avio_rb32(pb);
2296 sc->bytes_per_frame = avio_rb32(pb);
2308 sc->bytes_per_frame = avio_rb32(pb);
2309 sc->samples_per_frame = avio_rb32(pb);
2315 if (version == 0 || (version == 1 && sc->audio_cid != -2)) {
2326 if (sc->format == 0) {
2354 sc->samples_per_frame = 6;
2355 sc->bytes_per_frame = 2 * st->codecpar->ch_layout.nb_channels;
2358 sc->samples_per_frame = 6;
2359 sc->bytes_per_frame = 1 * st->codecpar->ch_layout.nb_channels;
2362 sc->samples_per_frame = 64;
2363 sc->bytes_per_frame = 34 * st->codecpar->ch_layout.nb_channels;
2366 sc->samples_per_frame = 160;
2367 sc->bytes_per_frame = 33;
2376 sc->sample_size = (bits_per_sample >> 3) * st->codecpar->ch_layout.nb_channels;
2381 AVStream *st, MOVStreamContext *sc,
2390 st->codecpar->width = sc->width;
2391 st->codecpar->height = sc->height;
2506 AVStream *st, MOVStreamContext *sc,
2552 AVStream *st, MOVStreamContext *sc)
2557 !st->codecpar->sample_rate && sc->time_scale > 1)
2558 st->codecpar->sample_rate = sc->time_scale;
2574 sc->dv_audio_container = 1;
2586 sc->samples_per_frame = 160;
2587 if (!sc->bytes_per_frame)
2588 sc->bytes_per_frame = 35;
2613 st->codecpar->block_align = sc->bytes_per_frame;
2675 MOVStreamContext *sc;
2680 sc = st->priv_data;
2706 sc->stsd_count++;
2710 sc->pseudo_stream_id = st->codecpar->codec_tag ? -1 : pseudo_stream_id;
2711 sc->dref_id = dref_id;
2712 sc->format = format;
2722 mov_parse_stsd_video(c, pb, st, sc);
2724 mov_parse_stsd_audio(c, pb, st, sc);
2734 mov_parse_stsd_subtitle(c, pb, st, sc,
2739 ret = mov_parse_stsd_data(c, pb, st, sc,
2748 ret = mov_parse_stsd_data(c, pb, st, sc,
2763 if (sc->extradata && st->codecpar->extradata) {
2767 sc->extradata_size[pseudo_stream_id] = extra_size;
2768 sc->extradata[pseudo_stream_id] = st->codecpar->extradata;
2772 sc->stsd_count++;
2786 MOVStreamContext *sc;
2792 sc = st->priv_data;
2794 sc->stsd_version = avio_r8(pb);
2804 if (sc->extradata) {
2811 sc->extradata = av_calloc(entries, sizeof(*sc->extradata));
2812 if (!sc->extradata)
2815 sc->extradata_size = av_calloc(entries, sizeof(*sc->extradata_size));
2816 if (!sc->extradata_size) {
2827 st->codecpar->extradata_size = sc->extradata_size[0];
2828 if (sc->extradata_size[0]) {
2829 st->codecpar->extradata = av_mallocz(sc->extradata_size[0] + AV_INPUT_BUFFER_PADDING_SIZE);
2832 memcpy(st->codecpar->extradata, sc->extradata[0], sc->extradata_size[0]);
2835 return mov_finalize_stsd_codec(c, pb, st, sc);
2837 if (sc->extradata) {
2839 for (j = 0; j < sc->stsd_count; j++)
2840 av_freep(&sc->extradata[j]);
2843 av_freep(&sc->extradata);
2844 av_freep(&sc->extradata_size);
2851 MOVStreamContext *sc;
2857 sc = st->priv_data;
2870 if (sc->stsc_data) {
2874 av_free(sc->stsc_data);
2875 sc->stsc_count = 0;
2876 sc->stsc_data = av_malloc_array(entries, sizeof(*sc->stsc_data));
2877 if (!sc->stsc_data)
2881 sc->stsc_data[i].first = avio_rb32(pb);
2882 sc->stsc_data[i].count = avio_rb32(pb);
2883 sc->stsc_data[i].id = avio_rb32(pb);
2886 sc->stsc_count = i;
2887 for (i = sc->stsc_count - 1; i < UINT_MAX; i--) {
2889 if ((i+1 < sc->stsc_count && sc->stsc_data[i].first >= sc->stsc_data[i+1].first) ||
2890 (i > 0 && sc->stsc_data[i].first <= sc->stsc_data[i-1].first) ||
2891 sc->stsc_data[i].first < first_min ||
2892 sc->stsc_data[i].count < 1 ||
2893 sc->stsc_data[i].id < 1) {
2894 av_log(c->fc, AV_LOG_WARNING, "STSC entry %d is invalid (first=%d count=%d id=%d)\n", i, sc->stsc_data[i].first, sc->stsc_data[i].count, sc->stsc_data[i].id);
2895 if (i+1 >= sc->stsc_count) {
2896 if (sc->stsc_data[i].count == 0 && i > 0) {
2897 sc->stsc_count --;
2900 sc->stsc_data[i].first = FFMAX(sc->stsc_data[i].first, first_min);
2901 if (i > 0 && sc->stsc_data[i].first <= sc->stsc_data[i-1].first)
2902 sc->stsc_data[i].first = FFMIN(sc->stsc_data[i-1].first + 1LL, INT_MAX);
2903 sc->stsc_data[i].count = FFMAX(sc->stsc_data[i].count, 1);
2904 sc->stsc_data[i].id = FFMAX(sc->stsc_data[i].id, 1);
2907 av_assert0(sc->stsc_data[i+1].first >= 2);
2909 sc->stsc_data[i].first = sc->stsc_data[i+1].first - 1;
2910 sc->stsc_data[i].count = sc->stsc_data[i+1].count;
2911 sc->stsc_data[i].id = sc->stsc_data[i+1].id;
2929 static inline int64_t mov_get_stsc_samples(MOVStreamContext *sc, unsigned int index)
2933 if (mov_stsc_index_valid(index, sc->stsc_count))
2934 chunk_count = sc->stsc_data[index + 1].first - sc->stsc_data[index].first;
2937 av_assert0(sc->stsc_data[index].first <= sc->chunk_count);
2938 chunk_count = sc->chunk_count - (sc->stsc_data[index].first - 1);
2941 return sc->stsc_data[index].count * (int64_t)chunk_count;
2947 MOVStreamContext *sc;
2953 sc = st->priv_data;
2958 if (sc->stps_data)
2960 av_free(sc->stps_data);
2961 sc->stps_count = 0;
2962 sc->stps_data = av_malloc_array(entries, sizeof(*sc->stps_data));
2963 if (!sc->stps_data)
2967 sc->stps_data[i] = avio_rb32(pb);
2970 sc->stps_count = i;
2984 MOVStreamContext *sc;
2991 sc = st->priv_data;
3001 sc->keyframe_absent = 1;
3006 if (sc->keyframes)
3010 av_freep(&sc->keyframes);
3011 sc->keyframe_count = 0;
3012 sc->keyframes = av_malloc_array(entries, sizeof(*sc->keyframes));
3013 if (!sc->keyframes)
3017 sc->keyframes[i] = avio_rb32(pb);
3020 sc->keyframe_count = i;
3033 MOVStreamContext *sc;
3042 sc = st->priv_data;
3049 if (!sc->sample_size) /* do not overwrite value computed in stsd */
3050 sc->sample_size = sample_size;
3051 sc->stsz_sample_size = sample_size;
3060 av_log(c->fc, AV_LOG_TRACE, "sample_size = %u sample_count = %u\n", sc->sample_size, entries);
3062 sc->sample_count = entries;
3075 if (sc->sample_sizes)
3077 av_free(sc->sample_sizes);
3078 sc->sample_count = 0;
3079 sc->sample_sizes = av_malloc_array(entries, sizeof(*sc->sample_sizes));
3080 if (!sc->sample_sizes)
3087 av_freep(&sc->sample_sizes);
3093 av_freep(&sc->sample_sizes);
3102 sc->sample_sizes[i] = get_bits_long(&gb, field_size);
3103 if (sc->sample_sizes[i] < 0) {
3105 av_log(c->fc, AV_LOG_ERROR, "Invalid sample size %d\n", sc->sample_sizes[i]);
3108 sc->data_size += sc->sample_sizes[i];
3111 sc->sample_count = i;
3121 MOVStreamContext *sc;
3131 sc = st->priv_data;
3140 if (sc->stts_data)
3142 av_freep(&sc->stts_data);
3143 sc->stts_count = 0;
3144 if (entries >= INT_MAX / sizeof(*sc->stts_data))
3151 MOVStts *stts_data = av_fast_realloc(sc->stts_data, &alloc_size,
3152 min_entries * sizeof(*sc->stts_data));
3154 av_freep(&sc->stts_data);
3155 sc->stts_count = 0;
3158 sc->stts_count = min_entries;
3159 sc->stts_data = stts_data;
3164 sc->stts_data[i].count= sample_count;
3165 sc->stts_data[i].duration= sample_duration;
3178 sc->stts_data[i].duration = 1;
3184 current_dts += sc->stts_data[i].duration * (int64_t)sample_count;
3188 uint32_t correction = (sc->stts_data[i].duration > drift) ? drift : sc->stts_data[i].duration - 1;
3190 sc->stts_data[i].duration -= correction;
3193 duration+=(int64_t)sc->stts_data[i].duration*(uint64_t)sc->stts_data[i].count;
3194 total_sample_count+=sc->stts_data[i].count;
3197 sc->stts_count = i;
3200 if (sc->stts_count > 0) {
3201 st->stts_count = sc->stts_count;
3204 memcpy(st->stts_data, sc->stts_data, st->stts_count * sizeof(AVMOVStts));
3213 duration <= INT64_MAX - sc->duration_for_fps &&
3214 total_sample_count <= INT_MAX - sc->nb_frames_for_fps) {
3215 sc->duration_for_fps += duration;
3216 sc->nb_frames_for_fps += total_sample_count;
3227 sc->track_end = duration;
3234 MOVStreamContext *sc;
3240 sc = st->priv_data;
3249 if (sc->sdtp_data)
3251 av_freep(&sc->sdtp_data);
3252 sc->sdtp_count = 0;
3254 sc->sdtp_data = av_malloc(entries);
3255 if (!sc->sdtp_data)
3259 sc->sdtp_data[i] = avio_r8(pb);
3260 sc->sdtp_count = i;
3265 static void mov_update_dts_shift(MOVStreamContext *sc, int duration, void *logctx)
3272 sc->dts_shift = FFMAX(sc->dts_shift, -duration);
3279 MOVStreamContext *sc;
3285 sc = st->priv_data;
3295 if (entries >= UINT_MAX / sizeof(*sc->ctts_data))
3297 av_freep(&sc->ctts_data);
3298 sc->ctts_data = av_fast_realloc(NULL, &sc->ctts_allocated_size, entries * sizeof(*sc->ctts_data));
3299 if (!sc->ctts_data)
3313 add_ctts_entry(&sc->ctts_data, &ctts_count, &sc->ctts_allocated_size,
3321 av_freep(&sc->ctts_data);
3322 sc->ctts_count = 0;
3327 mov_update_dts_shift(sc, duration, c->fc);
3330 sc->ctts_count = ctts_count;
3333 if (sc->ctts_count > 0) {
3334 st->ctts_count = sc->ctts_count;
3337 memcpy(st->ctts_data, sc->ctts_data, st->ctts_count * sizeof(AVMOVCtts));
3350 av_log(c->fc, AV_LOG_TRACE, "dts shift %d\n", sc->dts_shift);
3358 MOVStreamContext *sc;
3368 sc = st->priv_data;
3385 av_freep(&sc->sgpd_sync);
3386 sc->sgpd_sync_count = entry_count;
3387 sc->sgpd_sync = av_calloc(entry_count, sizeof(*sc->sgpd_sync));
3388 if (!sc->sgpd_sync)
3397 sc->sgpd_sync[i] = nal_unit_type;
3414 MOVStreamContext *sc;
3424 sc = st->priv_data;
3431 tablep = &sc->rap_group;
3432 table_count = &sc->rap_group_count;
3434 tablep = &sc->sync_group;
3435 table_count = &sc->sync_group_count;
3756 static void mov_current_sample_inc(MOVStreamContext *sc)
3758 sc->current_sample++;
3759 sc->current_index++;
3760 if (sc->index_ranges &&
3761 sc->current_index >= sc->current_index_range->end &&
3762 sc->current_index_range->end) {
3763 sc->current_index_range++;
3764 sc->current_index = sc->current_index_range->start;
3768 static void mov_current_sample_dec(MOVStreamContext *sc)
3770 sc->current_sample--;
3771 sc->current_index--;
3772 if (sc->index_ranges &&
3773 sc->current_index < sc->current_index_range->start &&
3774 sc->current_index_range > sc->index_ranges) {
3775 sc->current_index_range--;
3776 sc->current_index = sc->current_index_range->end - 1;
3780 static void mov_current_sample_set(MOVStreamContext *sc, int current_sample)
3784 sc->current_sample = current_sample;
3785 sc->current_index = current_sample;
3786 if (!sc->index_ranges) {
3790 for (sc->current_index_range = sc->index_ranges;
3791 sc->current_index_range->end;
3792 sc->current_index_range++) {
3793 range_size = sc->current_index_range->end - sc->current_index_range->start;
3795 sc->current_index = sc->current_index_range->start + current_sample;
4111 static uint32_t get_sgpd_sync_index(const MOVStreamContext *sc, int nal_unit_type)
4113 for (uint32_t i = 0; i < sc->sgpd_sync_count; i++)
4114 if (sc->sgpd_sync[i] == HEVC_NAL_CRA_NUT)
4124 MOVStreamContext *sc = st->priv_data;
4126 if (st->codecpar->codec_id != AV_CODEC_ID_HEVC || !sc->sync_group_count)
4130 sc->sample_offsets_count = 0;
4131 for (uint32_t i = 0; i < sc->ctts_count; i++) {
4132 if (sc->ctts_data[i].count > INT_MAX - sc->sample_offsets_count)
4134 sc->sample_offsets_count += sc->ctts_data[i].count;
4136 av_freep(&sc->sample_offsets);
4137 sc->sample_offsets = av_calloc(sc->sample_offsets_count, sizeof(*sc->sample_offsets));
4138 if (!sc->sample_offsets)
4141 for (uint32_t i = 0; i < sc->ctts_count; i++)
4142 for (int j = 0; j < sc->ctts_data[i].count; j++)
4143 sc->sample_offsets[k++] = sc->ctts_data[i].duration;
4147 cra_index = get_sgpd_sync_index(sc, HEVC_NAL_CRA_NUT); /* Clean Random Access */
4152 sc->open_key_samples_count = 0;
4153 for (uint32_t i = 0; i < sc->sync_group_count; i++)
4154 if (sc->sync_group[i].index == cra_index) {
4155 if (sc->sync_group[i].count > INT_MAX - sc->open_key_samples_count)
4157 sc->open_key_samples_count += sc->sync_group[i].count;
4159 av_freep(&sc->open_key_samples);
4160 sc->open_key_samples = av_calloc(sc->open_key_samples_count, sizeof(*sc->open_key_samples));
4161 if (!sc->open_key_samples)
4164 for (uint32_t i = 0; i < sc->sync_group_count; i++) {
4165 const MOVSbgp *sg = &sc->sync_group[i];
4168 sc->open_key_samples[k++] = sample_id;
4175 sc->min_sample_duration = UINT_MAX;
4176 for (uint32_t i = 0; i < sc->stts_count; i++)
4177 sc->min_sample_duration = FFMIN(sc->min_sample_duration, sc->stts_data[i].duration);
4184 MOVStreamContext *sc = st->priv_data;
4194 MOVCtts *ctts_data_old = sc->ctts_data;
4195 unsigned int ctts_count_old = sc->ctts_count;
4201 if (sc->elst_count) {
4206 for (i = 0; i < sc->elst_count; i++) {
4207 const MOVElst *e = &sc->elst_data[i];
4228 empty_duration = av_rescale(empty_duration, sc->time_scale, mov->time_scale);
4233 sc->time_offset = start_time - (uint64_t)empty_duration;
4234 sc->min_corrected_pts = start_time;
4236 current_dts = -sc->time_offset;
4241 sc->start_pad = start_time;
4246 sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
4253 int rap_group_present = sc->rap_group_count && sc->rap_group;
4254 int key_off = (sc->keyframe_count && sc->keyframes[0] > 0) || (sc->stps_count && sc->stps_data[0] > 0);
4256 current_dts -= sc->dts_shift;
4258 if (!sc->sample_count || sti->nb_index_entries)
4260 if (sc->sample_count >= UINT_MAX / sizeof(*sti->index_entries) - sti->nb_index_entries)
4263 sti->nb_index_entries + sc->sample_count,
4268 sti->index_entries_allocated_size = (sti->nb_index_entries + sc->sample_count) * sizeof(*sti->index_entries);
4272 if (sc->sample_count >= UINT_MAX / sizeof(*sc->ctts_data))
4274 sc->ctts_count = 0;
4275 sc->ctts_allocated_size = 0;
4276 sc->ctts_data = av_fast_realloc(NULL, &sc->ctts_allocated_size,
4277 sc->sample_count * sizeof(*sc->ctts_data));
4278 if (!sc->ctts_data) {
4283 memset((uint8_t*)(sc->ctts_data), 0, sc->ctts_allocated_size);
4286 sc->ctts_count < sc->sample_count; i++)
4288 sc->ctts_count < sc->sample_count; j++)
4289 add_ctts_entry(&sc->ctts_data, &sc->ctts_count,
4290 &sc->ctts_allocated_size, 1,
4295 for (i = 0; i < sc->chunk_count; i++) {
4296 int64_t next_offset = i+1 < sc->chunk_count ? sc->chunk_offsets[i+1] : INT64_MAX;
4297 current_offset = sc->chunk_offsets[i];
4298 while (mov_stsc_index_valid(stsc_index, sc->stsc_count) &&
4299 i + 1 == sc->stsc_data[stsc_index + 1].first)
4302 if (next_offset > current_offset && sc->sample_size>0 && sc->sample_size < sc->stsz_sample_size &&
4303 sc->stsc_data[stsc_index].count * (int64_t)sc->stsz_sample_size > next_offset - current_offset) {
4304 av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too large), ignoring\n", sc->stsz_sample_size);
4305 sc->stsz_sample_size = sc->sample_size;
4307 if (sc->stsz_sample_size>0 && sc->stsz_sample_size < sc->sample_size) {
4308 av_log(mov->fc, AV_LOG_WARNING, "STSZ sample size %d invalid (too small), ignoring\n", sc->stsz_sample_size);
4309 sc->stsz_sample_size = sc->sample_size;
4312 for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
4314 if (current_sample >= sc->sample_count) {
4319 if (!sc->keyframe_absent && (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index])) {
4321 if (stss_index + 1 < sc->keyframe_count)
4323 } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
4325 if (stps_index + 1 < sc->stps_count)
4328 if (rap_group_present && rap_group_index < sc->rap_group_count) {
4329 if (sc->rap_group[rap_group_index].index > 0)
4331 if (++rap_group_sample == sc->rap_group[rap_group_index].count) {
4336 if (sc->keyframe_absent
4337 && !sc->stps_count
4343 sample_size = sc->stsz_sample_size > 0 ? sc->stsz_sample_size : sc->sample_sizes[current_sample];
4351 if (sc->pseudo_stream_id == -1 ||
4352 sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
4374 current_dts += sc->stts_data[stts_index].duration;
4379 if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
4386 st->codecpar->bit_rate = stream_size*8*sc->time_scale/st->duration;
4390 if (!sc->chunk_count)
4394 for (i = 0; i < sc->stsc_count; i++) {
4397 chunk_samples = sc->stsc_data[i].count;
4398 if (i != sc->stsc_count - 1 &&
4399 sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
4404 if (sc->samples_per_frame >= 160) { // gsm
4405 count = chunk_samples / sc->samples_per_frame;
4406 } else if (sc->samples_per_frame > 1) {
4407 unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
4413 if (mov_stsc_index_valid(i, sc->stsc_count))
4414 chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
4416 chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
4432 for (i = 0; i < sc->chunk_count; i++) {
4433 current_offset = sc->chunk_offsets[i];
4434 if (mov_stsc_index_valid(stsc_index, sc->stsc_count) &&
4435 i + 1 == sc->stsc_data[stsc_index + 1].first)
4437 chunk_samples = sc->stsc_data[stsc_index].count;
4443 if (sc->samples_per_frame > 1 && !sc->bytes_per_frame) {
4446 sc->samples_per_frame);
4450 if (sc->samples_per_frame >= 160) { // gsm
4451 samples = sc->samples_per_frame;
4452 size = sc->bytes_per_frame;
4454 if (sc->samples_per_frame > 1) {
4455 samples = FFMIN((1024 / sc->samples_per_frame)*
4456 sc->samples_per_frame, chunk_samples);
4457 size = (samples / sc->samples_per_frame) * sc->bytes_per_frame;
4460 size = samples * sc->sample_size;
4496 st->start_time = sti->index_entries[0].timestamp + sc->dts_shift;
4497 if (sc->ctts_data) {
4498 st->start_time += sc->ctts_data[0].duration;
4606 static void fix_timescale(MOVContext *c, MOVStreamContext *sc)
4608 if (sc->time_scale <= 0) {
4609 av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", sc->ffindex);
4610 sc->time_scale = c->time_scale;
4611 if (sc->time_scale <= 0)
4612 sc->time_scale = 1;
4619 MOVStreamContext *sc;
4629 sc = av_mallocz(sizeof(MOVStreamContext));
4630 if (!sc) return AVERROR(ENOMEM);
4632 st->priv_data = sc;
4634 sc->ffindex = st->index;
4644 if (!sc->chunk_count && !sc->stts_count && sc->stsc_count) {
4645 sc->stsc_count = 0;
4646 av_freep(&sc->stsc_data);
4650 if ((sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
4651 (!sc->sample_size && !sc->sample_count))) ||
4652 (!sc->chunk_count && sc->sample_count)) {
4657 if (sc->stsc_count && sc->stsc_data[ sc->stsc_count - 1 ].first > sc->chunk_count) {
4663 fix_timescale(c, sc);
4665 avpriv_set_pts_info(st, 64, 1, sc->time_scale);
4669 if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
4670 MOVDref *dref = &sc->drefs[sc->dref_id - 1];
4672 if (mov_open_dref(c, &sc->pb, c->fc->url, dref) < 0)
4688 sc->pb = c->fc->pb;
4689 sc->pb_is_copied = 1;
4694 sc->height && sc->width &&
4695 (st->codecpar->width != sc->width || st->codecpar->height != sc->height)) {
4696 st->sample_aspect_ratio = av_d2q(((double)st->codecpar->height * sc->width) /
4697 ((double)st->codecpar->width * sc->height), INT_MAX);
4701 if (sc->stts_count == 1 || (sc->stts_count == 2 && sc->stts_data[1].count == 1))
4703 sc->time_scale, sc->stts_data[0].duration, INT_MAX);
4732 && sc->stts_count > 3
4733 && sc->stts_count*10 > st->nb_frames
4734 && sc->time_scale == st->codecpar->sample_rate) {
4738 av_freep(&sc->chunk_offsets);
4739 av_freep(&sc->sample_sizes);
4740 av_freep(&sc->keyframes);
4741 av_freep(&sc->stts_data);
4742 av_freep(&sc->stps_data);
4743 av_freep(&sc->elst_data);
4744 av_freep(&sc->rap_group);
4745 av_freep(&sc->sync_group);
4746 av_freep(&sc->sgpd_sync);
4820 MOVStreamContext *sc;
4825 sc = st->priv_data;
4874 sc->start_pad = priming;
4928 MOVStreamContext *sc;
4935 sc = st->priv_data;
4978 sc->width = width >> 16;
4979 sc->height = height >> 16;
4995 av_freep(&sc->display_matrix);
4996 sc->display_matrix = av_malloc(sizeof(int32_t) * 9);
4997 if (!sc->display_matrix)
5002 sc->display_matrix[i * 3 + j] = res_display_matrix[i][j];
5007 if (width && height && sc->display_matrix) {
5011 disp_transform[i] = hypot(sc->display_matrix[0 + i],
5012 sc->display_matrix[3 + i]);
5121 MOVStreamContext *sc;
5136 sc = st->priv_data;
5137 if (sc->pseudo_stream_id + 1 != frag->stsd_id && sc->pseudo_stream_id != -1)
5150 sc->track_end = base_media_decode_time;
5160 MOVStreamContext *sc;
5190 sc = st->priv_data;
5191 if (sc->pseudo_stream_id+1 != frag->stsd_id && sc->pseudo_stream_id != -1)
5216 if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
5224 dts = frag_stream_info->next_trun_dts - sc->time_offset;
5249 dts = frag_stream_info->tfdt_dts - sc->time_offset;
5255 dts = frag_stream_info->sidx_pts - sc->time_offset;
5259 dts = sc->track_end - sc->time_offset;
5265 dts = sc->track_end - sc->time_offset;
5289 requested_size = (sti->nb_index_entries + entries) * sizeof(*sc->ctts_data);
5290 old_ctts_allocated_size = sc->ctts_allocated_size;
5291 ctts_data = av_fast_realloc(sc->ctts_data, &sc->ctts_allocated_size,
5295 sc->ctts_data = ctts_data;
5300 memset((uint8_t*)(sc->ctts_data) + old_ctts_allocated_size, 0,
5301 sc->ctts_allocated_size - old_ctts_allocated_size);
5309 memmove(sc->ctts_data + index_entry_pos + entries,
5310 sc->ctts_data + index_entry_pos,
5311 sizeof(*sc->ctts_data) * (sc->ctts_count - index_entry_pos));
5312 if (index_entry_pos < sc->current_sample) {
5313 sc->current_sample += entries;
5318 sc->ctts_count = sti->nb_index_entries;
5340 mov_update_dts_shift(sc, ctts_duration, c->fc);
5342 dts = pts - sc->dts_shift;
5346 dts -= sc->time_offset;
5350 " sc->dts_shift %d ctts.duration %d"
5351 " sc->time_offset %"PRId64
5354 sc->dts_shift, ctts_duration,
5355 sc->time_offset, flags & MOV_TRUN_SAMPLE_CTS);
5380 sc->ctts_data[index_entry_pos].count = 1;
5381 sc->ctts_data[index_entry_pos].duration = ctts_duration;
5394 sc->data_size += sample_size;
5396 if (sample_duration <= INT64_MAX - sc->duration_for_fps &&
5397 1 <= INT_MAX - sc->nb_frames_for_fps
5399 sc->duration_for_fps += sample_duration;
5400 sc->nb_frames_for_fps ++;
5404 frag_stream_info->next_trun_dts = dts + sc->time_offset;
5413 memmove(sc->ctts_data + index_entry_pos,
5414 sc->ctts_data + index_entry_pos + gap,
5415 sizeof(*sc->ctts_data) *
5416 (sc->ctts_count - (index_entry_pos + gap)));
5419 sc->ctts_count -= gap;
5420 if (index_entry_pos < sc->current_sample) {
5421 sc->current_sample -= gap;
5451 sc->track_end = dts + sc->time_offset;
5452 if (st->duration < sc->track_end)
5453 st->duration = sc->track_end;
5467 MOVStreamContext *sc, *ref_sc = NULL;
5490 sc = st->priv_data;
5542 st->duration = sc->track_end = pts;
5544 sc->has_sidx = 1;
5580 sc = st->priv_data;
5581 if (!sc->has_sidx) {
5582 st->duration = sc->track_end = av_rescale(ref_st->duration, sc->time_scale, ref_sc->time_scale);
5670 MOVStreamContext *sc;
5676 sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
5699 if (sc->elst_data)
5701 av_free(sc->elst_data);
5702 sc->elst_count = 0;
5703 sc->elst_data = av_malloc_array(edit_count, sizeof(*sc->elst_data));
5704 if (!sc->elst_data)
5709 MOVElst *e = &sc->elst_data[i];
5732 sc->elst_count = i;
5739 MOVStreamContext *sc;
5743 sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
5744 sc->timecode_track = avio_rb32(pb);
5794 MOVStreamContext *sc;
5800 sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
5812 if (sc->mastering)
5817 sc->mastering = av_mastering_display_metadata_alloc();
5818 if (!sc->mastering)
5822 sc->mastering->display_primaries[i][0] = av_make_q(avio_rb16(pb), 1 << 16);
5823 sc->mastering->display_primaries[i][1] = av_make_q(avio_rb16(pb), 1 << 16);
5825 sc->mastering->white_point[0] = av_make_q(avio_rb16(pb), 1 << 16);
5826 sc->mastering->white_point[1] = av_make_q(avio_rb16(pb), 1 << 16);
5828 sc->mastering->max_luminance = av_make_q(avio_rb32(pb), 1 << 8);
5829 sc->mastering->min_luminance = av_make_q(avio_rb32(pb), 1 << 14);
5831 sc->mastering->has_primaries = 1;
5832 sc->mastering->has_luminance = 1;
5839 MOVStreamContext *sc;
5848 sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
5850 if (atom.size < 24 || sc->mastering) {
5855 sc->mastering = av_mastering_display_metadata_alloc();
5856 if (!sc->mastering)
5861 sc->mastering->display_primaries[j][0] = av_make_q(avio_rb16(pb), chroma_den);
5862 sc->mastering->display_primaries[j][1] = av_make_q(avio_rb16(pb), chroma_den);
5864 sc->mastering->white_point[0] = av_make_q(avio_rb16(pb), chroma_den);
5865 sc->mastering->white_point[1] = av_make_q(avio_rb16(pb), chroma_den);
5867 sc->mastering->max_luminance = av_make_q(avio_rb32(pb), luma_den);
5868 sc->mastering->min_luminance = av_make_q(avio_rb32(pb), luma_den);
5870 sc->mastering->has_luminance = 1;
5871 sc->mastering->has_primaries = 1;
5878 MOVStreamContext *sc;
5884 sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
5898 if (sc->coll){
5903 sc->coll = av_content_light_metadata_alloc(&sc->coll_size);
5904 if (!sc->coll)
5907 sc->coll->MaxCLL = avio_rb16(pb);
5908 sc->coll->MaxFALL = avio_rb16(pb);
5915 MOVStreamContext *sc;
5920 sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
5927 if (sc->coll){
5932 sc->coll = av_content_light_metadata_alloc(&sc->coll_size);
5933 if (!sc->coll)
5936 sc->coll->MaxCLL = avio_rb16(pb);
5937 sc->coll->MaxFALL = avio_rb16(pb);
5945 MOVStreamContext *sc;
5953 sc = st->priv_data;
5960 if (sc->stereo3d)
5981 sc->stereo3d = av_stereo3d_alloc();
5982 if (!sc->stereo3d)
5985 sc->stereo3d->type = type;
5992 MOVStreamContext *sc;
6003 sc = st->priv_data;
6106 sc->spherical = av_spherical_alloc(&sc->spherical_size);
6107 if (!sc->spherical)
6110 sc->spherical->projection = projection;
6112 sc->spherical->yaw = yaw;
6113 sc->spherical->pitch = pitch;
6114 sc->spherical->roll = roll;
6116 sc->spherical->padding = padding;
6118 sc->spherical->bound_left = l;
6119 sc->spherical->bound_top = t;
6120 sc->spherical->bound_right = r;
6121 sc->spherical->bound_bottom = b;
6126 static int mov_parse_uuid_spherical(MOVStreamContext *sc, AVIOContext *pb, size_t len)
6141 if (!sc->spherical &&
6149 sc->spherical = av_spherical_alloc(&sc->spherical_size);
6150 if (!sc->spherical)
6153 sc->spherical->projection = AV_SPHERICAL_EQUIRECTANGULAR;
6155 if (av_stristr(buffer, "<GSpherical:StereoMode>") && !sc->stereo3d) {
6165 sc->stereo3d = av_stereo3d_alloc();
6166 if (!sc->stereo3d)
6169 sc->stereo3d->type = mode;
6175 sc->spherical->yaw = strtol(val, NULL, 10) * (1 << 16);
6178 sc->spherical->pitch = strtol(val, NULL, 10) * (1 << 16);
6181 sc->spherical->roll = strtol(val, NULL, 10) * (1 << 16);
6192 MOVStreamContext *sc;
6214 sc = st->priv_data;
6284 ret = mov_parse_uuid_spherical(sc, pb, len);
6287 if (!sc->spherical)
6319 MOVStreamContext *sc;
6326 sc = st->priv_data;
6328 switch (sc->format)
6342 sc->format = format;
6346 if (format != sc->format) {
6349 (char*)&format, (char*)&sc->format);
6363 static int get_current_encryption_info(MOVContext *c, MOVEncryptionIndex **encryption_index, MOVStreamContext **sc)
6379 *sc = st->priv_data;
6383 if (!(*sc)->cenc.default_encrypted_sample)
6397 *sc = st->priv_data;
6399 if (!(*sc)->cenc.encryption_index) {
6401 if (!(*sc)->cenc.default_encrypted_sample)
6403 (*sc)->cenc.encryption_index = av_mallocz(sizeof(*frag_stream_info->encryption_index));
6404 if (!(*sc)->cenc.encryption_index)
6408 *encryption_index = (*sc)->cenc.encryption_index;
6413 static int mov_read_sample_encryption_info(MOVContext *c, AVIOContext *pb, MOVStreamContext *sc, AVEncryptionInfo **sample, int use_subsamples)
6419 if (!sc->cenc.default_encrypted_sample) {
6424 *sample = av_encryption_info_clone(sc->cenc.default_encrypted_sample);
6428 if (sc->cenc.per_sample_iv_size != 0) {
6429 if ((ret = ffio_read_size(pb, (*sample)->iv, sc->cenc.per_sample_iv_size)) < 0) {
6468 MOVStreamContext *sc;
6472 ret = get_current_encryption_info(c, &encryption_index, &sc);
6497 c, pb, sc, &encryption_index->encrypted_samples[i], use_subsamples);
6520 static int mov_parse_auxiliary_info(MOVContext *c, MOVStreamContext *sc, AVIOContext *pb, MOVEncryptionIndex *encryption_index)
6560 ret = mov_read_sample_encryption_info(c, pb, sc, sample, sample_info_size > sc->cenc.per_sample_iv_size);
6617 MOVStreamContext *sc;
6621 ret = get_current_encryption_info(c, &encryption_index, &sc);
6640 if (sc->cenc.default_encrypted_sample) {
6641 if (aux_info_type != sc->cenc.default_encrypted_sample->scheme) {
6662 } else if (!sc->cenc.default_encrypted_sample) {
6680 return mov_parse_auxiliary_info(c, sc, pb, encryption_index);
6690 MOVStreamContext *sc;
6695 ret = get_current_encryption_info(c, &encryption_index, &sc);
6714 if (sc->cenc.default_encrypted_sample) {
6715 if (aux_info_type != sc->cenc.default_encrypted_sample->scheme) {
6736 } else if (!sc->cenc.default_encrypted_sample) {
6775 return mov_parse_auxiliary_info(c, sc, pb, encryption_index);
6989 MOVStreamContext *sc;
6994 sc = st->priv_data;
6996 if (sc->pseudo_stream_id != 0) {
7006 if (!sc->cenc.default_encrypted_sample) {
7007 sc->cenc.default_encrypted_sample = av_encryption_info_alloc(0, 16, 16);
7008 if (!sc->cenc.default_encrypted_sample) {
7013 sc->cenc.default_encrypted_sample->scheme = avio_rb32(pb);
7020 MOVStreamContext *sc;
7026 sc = st->priv_data;
7028 if (sc->pseudo_stream_id != 0) {
7033 if (!sc->cenc.default_encrypted_sample) {
7034 sc->cenc.default_encrypted_sample = av_encryption_info_alloc(0, 16, 16);
7035 if (!sc->cenc.default_encrypted_sample) {
7050 sc->cenc.default_encrypted_sample->crypt_byte_block = pattern >> 4;
7051 sc->cenc.default_encrypted_sample->skip_byte_block = pattern & 0xf;
7055 if (is_protected && !sc->cenc.encryption_index) {
7057 sc->cenc.encryption_index = av_mallocz(sizeof(MOVEncryptionIndex));
7058 if (!sc->cenc.encryption_index)
7061 sc->cenc.per_sample_iv_size = avio_r8(pb);
7062 if (sc->cenc.per_sample_iv_size != 0 && sc->cenc.per_sample_iv_size != 8 &&
7063 sc->cenc.per_sample_iv_size != 16) {
7067 if (avio_read(pb, sc->cenc.default_encrypted_sample->key_id, 16) != 16) {
7072 if (is_protected && !sc->cenc.per_sample_iv_size) {
7079 if (avio_read(pb, sc->cenc.default_encrypted_sample->iv, iv_size) != iv_size) {
7128 static int cenc_scheme_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *sample, uint8_t *input, int size)
7133 if (!sc->cenc.aes_ctr) {
7135 sc->cenc.aes_ctr = av_aes_ctr_alloc();
7136 if (!sc->cenc.aes_ctr) {
7140 ret = av_aes_ctr_init(sc->cenc.aes_ctr, c->decryption_key);
7146 av_aes_ctr_set_full_iv(sc->cenc.aes_ctr, sample->iv);
7150 av_aes_ctr_crypt(sc->cenc.aes_ctr, input, input, size);
7167 av_aes_ctr_crypt(sc->cenc.aes_ctr, input, input, bytes_of_protected_data);
7181 static int cbc1_scheme_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *sample, uint8_t *input, int size)
7187 if (!sc->cenc.aes_ctx) {
7189 sc->cenc.aes_ctx = av_aes_alloc();
7190 if (!sc->cenc.aes_ctx) {
7194 ret = av_aes_init(sc->cenc.aes_ctx, c->decryption_key, 16 * 8, 1);
7205 av_aes_crypt(sc->cenc.aes_ctx, input, input, size/16, iv, 1);
7227 av_aes_crypt(sc->cenc.aes_ctx, input, input, num_of_encrypted_blocks, iv, 1);
7241 static int cens_scheme_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *sample, uint8_t *input, int size)
7246 if (!sc->cenc.aes_ctr) {
7248 sc->cenc.aes_ctr = av_aes_ctr_alloc();
7249 if (!sc->cenc.aes_ctr) {
7253 ret = av_aes_ctr_init(sc->cenc.aes_ctr, c->decryption_key);
7259 av_aes_ctr_set_full_iv(sc->cenc.aes_ctr, sample->iv);
7264 av_aes_ctr_crypt(sc->cenc.aes_ctr, input, input, size);
7288 av_aes_ctr_crypt(sc->cenc.aes_ctr, data, data, 16*sample->crypt_byte_block);
7306 static int cbcs_scheme_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *sample, uint8_t *input, int size)
7312 if (!sc->cenc.aes_ctx) {
7314 sc->cenc.aes_ctx = av_aes_alloc();
7315 if (!sc->cenc.aes_ctx) {
7319 ret = av_aes_init(sc->cenc.aes_ctx, c->decryption_key, 16 * 8, 1);
7329 av_aes_crypt(sc->cenc.aes_ctx, input, input, size/16, iv, 1);
7354 av_aes_crypt(sc->cenc.aes_ctx, data, data, sample->crypt_byte_block, iv, 1);
7372 static int cenc_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *sample, uint8_t *input, int size)
7375 return cenc_scheme_decrypt(c, sc, sample, input, size);
7377 return cbc1_scheme_decrypt(c, sc, sample, input, size);
7379 return cens_scheme_decrypt(c, sc, sample, input, size);
7381 return cbcs_scheme_decrypt(c, sc, sample, input, size);
7388 static int cenc_filter(MOVContext *mov, AVStream* st, MOVStreamContext *sc, AVPacket *pkt, int current_index)
7403 sc->cenc.frag_index_entry_base = frag_stream_info->index_entry;
7404 encrypted_index = current_index - (frag_stream_info->index_entry - sc->cenc.frag_index_entry_base);
7407 encryption_index = sc->cenc.encryption_index;
7411 encryption_index = sc->cenc.encryption_index;
7428 encrypted_sample = sc->cenc.default_encrypted_sample;
7438 return cenc_decrypt(mov, sc, encrypted_sample, pkt->data, pkt->size);
7781 MOVStreamContext *sc;
7799 sc = av_mallocz(sizeof(MOVStreamContext));
7800 if (!sc)
7803 st->priv_data = sc;
7806 sc->ffindex = st->index;
7811 sc->time_scale = 1;
7812 sc = st->priv_data;
7813 sc->pb = c->fc->pb;
7814 sc->pb_is_copied = 1;
7832 sc->stsc_count = 1;
7833 sc->stsc_data = av_malloc_array(1, sizeof(*sc->stsc_data));
7834 if (!sc->stsc_data)
7836 sc->stsc_data[0].first = 1;
7837 sc->stsc_data[0].count = 1;
7838 sc->stsc_data[0].id = 1;
7839 sc->chunk_count = 1;
7840 sc->chunk_offsets = av_malloc_array(1, sizeof(*sc->chunk_offsets));
7841 if (!sc->chunk_offsets)
7843 sc->sample_count = 1;
7844 sc->sample_sizes = av_malloc_array(1, sizeof(*sc->sample_sizes));
7845 if (!sc->sample_sizes)
7847 sc->stts_count = 1;
7848 sc->stts_data = av_malloc_array(1, sizeof(*sc->stts_data));
7849 if (!sc->stts_data)
7851 sc->stts_data[0].count = 1;
7853 sc->stts_data[0].duration = 0;
7876 sc->sample_sizes[0] = extent_length;
7877 sc->chunk_offsets[0] = base_offset + extent_offset;
8264 MOVStreamContext *sc;
8284 sc = st->priv_data;
8285 cur_pos = avio_tell(sc->pb);
8292 if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
8297 if (ff_add_attached_pic(s, st, sc->pb, NULL, sample->size) < 0)
8316 if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
8322 len = avio_rb16(sc->pb);
8335 ch = avio_rb16(sc->pb);
8337 avio_get_str16be(sc->pb, len, title, title_len);
8339 avio_get_str16le(sc->pb, len, title, title_len);
8345 avio_get_str(sc->pb, INT_MAX, title + 2, len - 1);
8354 avio_seek(sc->pb, cur_pos, SEEK_SET);
8374 MOVStreamContext *sc = st->priv_data;
8377 int64_t cur_pos = avio_tell(sc->pb);
8383 avio_seek(sc->pb, sti->index_entries->pos, SEEK_SET);
8394 avio_seek(sc->pb, cur_pos, SEEK_SET);
8400 MOVStreamContext *sc = st->priv_data;
8403 int64_t cur_pos = avio_tell(sc->pb);
8406 int tmcd_nb_frames = sc->tmcd_nb_frames;
8415 avio_seek(sc->pb, sti->index_entries->pos, SEEK_SET);
8418 if (sc->tmcd_flags & 0x0001) flags |= AV_TIMECODE_FLAG_DROPFRAME;
8419 if (sc->tmcd_flags & 0x0002) flags |= AV_TIMECODE_FLAG_24HOURSMAX;
8420 if (sc->tmcd_flags & 0x0004) flags |= AV_TIMECODE_FLAG_ALLOWNEGATIVE;
8441 avio_seek(sc->pb, cur_pos, SEEK_SET);
8464 MOVStreamContext *sc = st->priv_data;
8478 if (!sc)
8481 av_freep(&sc->ctts_data);
8482 for (j = 0; j < sc->drefs_count; j++) {
8483 av_freep(&sc->drefs[j].path);
8484 av_freep(&sc->drefs[j].dir);
8486 av_freep(&sc->drefs);
8488 sc->drefs_count = 0;
8490 if (!sc->pb_is_copied)
8491 ff_format_io_close(s, &sc->pb);
8493 sc->pb = NULL;
8494 av_freep(&sc->chunk_offsets);
8495 av_freep(&sc->stsc_data);
8496 av_freep(&sc->sample_sizes);
8497 av_freep(&sc->keyframes);
8498 av_freep(&sc->stts_data);
8499 av_freep(&sc->sdtp_data);
8500 av_freep(&sc->stps_data);
8501 av_freep(&sc->elst_data);
8502 av_freep(&sc->rap_group);
8503 av_freep(&sc->sync_group);
8504 av_freep(&sc->sgpd_sync);
8505 av_freep(&sc->sample_offsets);
8506 av_freep(&sc->open_key_samples);
8507 av_freep(&sc->display_matrix);
8508 av_freep(&sc->index_ranges);
8510 if (sc->extradata)
8511 for (j = 0; j < sc->stsd_count; j++)
8512 av_free(sc->extradata[j]);
8513 av_freep(&sc->extradata);
8514 av_freep(&sc->extradata_size);
8516 mov_free_encryption_index(&sc->cenc.encryption_index);
8517 av_encryption_info_free(sc->cenc.default_encrypted_sample);
8518 av_aes_ctr_free(sc->cenc.aes_ctr);
8520 av_freep(&sc->stereo3d);
8521 av_freep(&sc->spherical);
8522 av_freep(&sc->mastering);
8523 av_freep(&sc->coll);
8561 MOVStreamContext *sc = st->priv_data;
8564 sc->timecode_track == tmcd_id)
8741 MOVStreamContext *sc = st->priv_data;
8742 if (sc->timecode_track > 0) {
8747 if (s->streams[j]->id == sc->timecode_track)
8762 MOVStreamContext *sc = st->priv_data;
8763 fix_timescale(mov, sc);
8766 sti->skip_samples = sc->start_pad;
8768 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sc->nb_frames_for_fps > 0 && sc->duration_for_fps > 0)
8770 sc->time_scale*(int64_t)sc->nb_frames_for_fps, sc->duration_for_fps, INT_MAX);
8773 st->codecpar->width = sc->width;
8774 st->codecpar->height = sc->height;
8792 MOVStreamContext *sc = st->priv_data;
8794 /* Akin to sc->data_size * 8 * sc->time_scale / st->duration but accounting for overflows. */
8795 st->codecpar->bit_rate = av_rescale(sc->data_size, ((int64_t) sc->time_scale) * 8, st->duration);
8798 sc->data_size, sc->time_scale);
8810 MOVStreamContext *sc = st->priv_data;
8811 if (sc->duration_for_fps > 0) {
8812 /* Akin to sc->data_size * 8 * sc->time_scale / sc->duration_for_fps but accounting for overflows. */
8813 st->codecpar->bit_rate = av_rescale(sc->data_size, ((int64_t) sc->time_scale) * 8, sc->duration_for_fps);
8816 sc->data_size, sc->time_scale);
8835 MOVStreamContext *sc = st->priv_data;
8844 if (sc->display_matrix) {
8845 err = av_stream_add_side_data(st, AV_PKT_DATA_DISPLAYMATRIX, (uint8_t*)sc->display_matrix,
8850 sc->display_matrix = NULL;
8852 if (sc->stereo3d) {
8854 (uint8_t *)sc->stereo3d,
8855 sizeof(*sc->stereo3d));
8859 sc->stereo3d = NULL;
8861 if (sc->spherical) {
8863 (uint8_t *)sc->spherical,
8864 sc->spherical_size);
8868 sc->spherical = NULL;
8870 if (sc->mastering) {
8872 (uint8_t *)sc->mastering,
8873 sizeof(*sc->mastering));
8877 sc->mastering = NULL;
8879 if (sc->coll) {
8881 (uint8_t *)sc->coll,
8882 sc->coll_size);
8886 sc->coll = NULL;
8971 static int mov_change_extradata(MOVStreamContext *sc, AVPacket *pkt)
8977 sc->last_stsd_index = sc->stsc_data[sc->stsc_index].id - 1;
8980 extradata_size = sc->extradata_size[sc->last_stsd_index];
8981 extradata = sc->extradata[sc->last_stsd_index];
9018 MOVStreamContext *sc;
9033 sc = st->priv_data;
9035 current_index = sc->current_index;
9036 mov_current_sample_inc(sc);
9044 int64_t ret64 = avio_seek(sc->pb, sample->pos, SEEK_SET);
9047 sc->ffindex, sample->pos);
9048 if (should_retry(sc->pb, ret64)) {
9049 mov_current_sample_dec(sc);
9057 av_log(mov->fc, AV_LOG_DEBUG, "Nonkey frame from stream %d discarded due to AVDISCARD_NONKEY\n", sc->ffindex);
9062 ret = get_eia608_packet(sc->pb, pkt, sample->size);
9064 ret = av_get_packet(sc->pb, pkt, sample->size);
9066 if (should_retry(sc->pb, ret)) {
9067 mov_current_sample_dec(sc);
9107 if (mov->dv_demux && sc->dv_audio_container) {
9117 if (sc->has_palette) {
9124 memcpy(pal, sc->palette, AVPALETTE_SIZE);
9125 sc->has_palette = 0;
9134 pkt->stream_index = sc->ffindex;
9139 if (sc->ctts_data && sc->ctts_index < sc->ctts_count) {
9140 pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration;
9142 sc->ctts_sample++;
9143 if (sc->ctts_index < sc->ctts_count &&
9144 sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
9145 sc->ctts_index++;
9146 sc->ctts_sample = 0;
9149 int64_t next_dts = (sc->current_sample < ffstream(st)->nb_index_entries) ?
9150 ffstream(st)->index_entries[sc->current_sample].timestamp : st->duration;
9158 if (sc->sdtp_data && sc->current_sample <= sc->sdtp_count) {
9159 uint8_t sample_flags = sc->sdtp_data[sc->current_sample - 1];
9167 if (sc->stsc_data) {
9168 if (sc->stsc_data[sc->stsc_index].id > 0 &&
9169 sc->stsc_data[sc->stsc_index].id - 1 < sc->stsd_count &&
9170 sc->stsc_data[sc->stsc_index].id - 1 != sc->last_stsd_index) {
9171 ret = mov_change_extradata(sc, pkt);
9177 sc->stsc_sample++;
9178 if (mov_stsc_index_valid(sc->stsc_index, sc->stsc_count) &&
9179 mov_get_stsc_samples(sc, sc->stsc_index) == sc->stsc_sample) {
9180 sc->stsc_index++;
9181 sc->stsc_sample = 0;
9188 ret = cenc_filter(mov, st, sc, pkt, current_index);
9215 static int is_open_key_sample(const MOVStreamContext *sc, int sample)
9218 for (int i = 0; i < sc->open_key_samples_count; i++) {
9219 const int oks = sc->open_key_samples[i];
9234 MOVStreamContext *sc = st->priv_data;
9241 if (sample >= sc->sample_offsets_count)
9245 key_sample_pts = key_sample_dts + sc->sample_offsets[sample] + sc->dts_shift;
9252 if (is_open_key_sample(sc, sample) && key_sample_pts > requested_pts)
9260 MOVStreamContext *sc = st->priv_data;
9267 timestamp -= (sc->min_corrected_pts + sc->dts_shift);
9283 timestamp -= FFMAX(sc->min_sample_duration, 1);
9286 mov_current_sample_set(sc, sample);
9287 av_log(s, AV_LOG_TRACE, "stream %d, found sample %d\n", st->index, sc->current_sample);
9289 if (sc->ctts_data) {
9291 for (i = 0; i < sc->ctts_count; i++) {
9292 int next = time_sample + sc->ctts_data[i].count;
9293 if (next > sc->current_sample) {
9294 sc->ctts_index = i;
9295 sc->ctts_sample = sc->current_sample - time_sample;
9303 if (sc->chunk_count) {
9305 for (i = 0; i < sc->stsc_count; i++) {
9306 int64_t next = time_sample + mov_get_stsc_samples(sc, i);
9307 if (next > sc->current_sample) {
9308 sc->stsc_index = i;
9309 sc->stsc_sample = sc->current_sample - time_sample;
9322 MOVStreamContext *sc = st->priv_data;
9334 return FFMAX(sc->start_pad - off, 0);
9374 MOVStreamContext *sc;
9376 sc = st->priv_data;
9377 mov_current_sample_set(sc, 0);
9380 MOVStreamContext *sc;
9384 sc = st->priv_data;
9385 if (sc->ffindex == stream_index && sc->current_sample == sample)
9387 mov_current_sample_inc(sc);