instead of just resyncing and skipping a bunch of TS packets, leading to a loss of frames. Before this, a stray byte with the value of 0x47 in TP_extra_header would throw off the detection of where TS packets start. A typical file that could cause issues would look like this: 00000300: 238f 4780 4750 1110 0000 01e0 0000 84c0 ^^ ^^ The first four bytes here are TP_extra_header and the actual TS packet starts at offset 0x304 FFmpeg would try to read a packet at 0x300 but since nothing skips the 4 byte TP_extra_header, find that the first byte is not 0x47 and immediately go into mpegts_resync, and incorrectly detect the stray 0x47 in the TP_extra_header at 0x302 as the new sync byte. Fix this by correctly skipping the first 4 bytes if the source packet is 192 bytes.
Unified · Split
| @@ -2961,6 +2961,12 @@ static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size, · 2960 unmodified lines | ||
| 2961 | 2961 | AVIOContext *pb = s->pb; |
| 2962 | 2962 | int len; |
| 2963 | 2963 | |
| 2964 | + // 192 bytes source packet that start with a 4 bytes TP_extra_header | |
| 2965 | + // followed by 188 bytes of TS packet. The sync byte is at offset 4, so skip | |
| 2966 | + // the first 4 bytes otherwise we'll end up syncing to the wrong packet. | |
| 2967 | + if (raw_packet_size == TS_DVHS_PACKET_SIZE) | |
| 2968 | + avio_skip(pb, 4); | |
| 2969 | + | |
| 2964 | 2970 | for (;;) { |
| 2965 | 2971 | len = ffio_read_indirect(pb, buf, TS_PACKET_SIZE, data); |
| 2966 | 2972 | if (len != TS_PACKET_SIZE) |
| @@ -2983,7 +2989,11 @@ static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size, · 16 unmodified lines | ||
| 2983 | 2989 | static void finished_reading_packet(AVFormatContext *s, int raw_packet_size) |
| 2984 | 2990 | { |
| 2985 | 2991 | AVIOContext *pb = s->pb; |
| 2986 | − int skip = raw_packet_size - TS_PACKET_SIZE; | |
| 2992 | + int skip; | |
| 2993 | + if (raw_packet_size == TS_DVHS_PACKET_SIZE) | |
| 2994 | + skip = raw_packet_size - TS_DVHS_PACKET_SIZE; | |
| 2995 | + else | |
| 2996 | + skip = raw_packet_size - TS_PACKET_SIZE; | |
| 2987 | 2997 | if (skip > 0) |
| 2988 | 2998 | avio_skip(pb, skip); |
| 2989 | 2999 | } |