- if (ts_st->pcr_packet_period) {
- if (ts->mux_rate > 1 || is_start) // VBR pcr period is based on frames
- ts_st->pcr_packet_count++;
- if (ts_st->pcr_packet_count >=
- ts_st->pcr_packet_period) {
- ts_st->pcr_packet_count = 0;
+ if (ts->mux_rate > 1) {
+ /* Send PCR packets for all PCR streams if needed */
+ pcr = get_pcr(ts, s->pb);
+ if (pcr >= ts->next_pcr) {
+ int64_t next_pcr = INT64_MAX;
+ for (int i = 0; i < s->nb_streams; i++) {
+ /* Make the current stream the last, because for that we
+ * can insert the pcr into the payload later */
+ int st2_index = i < st->index ? i : (i + 1 == s->nb_streams ? st->index : i + 1);
+ AVStream *st2 = s->streams[st2_index];
+ MpegTSWriteStream *ts_st2 = st2->priv_data;
+ if (ts_st2->pcr_period) {
+ if (pcr - ts_st2->last_pcr >= ts_st2->pcr_period) {
+ ts_st2->last_pcr = FFMAX(pcr - ts_st2->pcr_period, ts_st2->last_pcr + ts_st2->pcr_period);
+ if (st2 != st) {
+ mpegts_insert_pcr_only(s, st2);
+ pcr = get_pcr(ts, s->pb);
+ } else {
+ write_pcr = 1;
+ }
+ }
+ next_pcr = FFMIN(next_pcr, ts_st2->last_pcr + ts_st2->pcr_period);
+ }
+ }
+ ts->next_pcr = next_pcr;
+ }
+ if (dts != AV_NOPTS_VALUE && (dts - pcr / 300) > delay) {
+ /* pcr insert gets priority over null packet insert */
+ if (write_pcr)
+ mpegts_insert_pcr_only(s, st);
+ else
+ mpegts_insert_null_packet(s);
+ /* recalculate write_pcr and possibly retransmit si_info */
+ continue;
+ }
+ } else if (ts_st->pcr_period && pcr != AV_NOPTS_VALUE) {
+ if (pcr - ts_st->last_pcr >= ts_st->pcr_period && is_start) {
+ ts_st->last_pcr = FFMAX(pcr - ts_st->pcr_period, ts_st->last_pcr + ts_st->pcr_period);