| 457 | } |
| 458 | |
| 459 | size_t FFMS_AudioSource::GetSeekablePacketNumber(FFMS_Track const& Frames, size_t PacketNumber) { |
| 460 | // Packets don't always have unique PTSes, so we may not be able to |
| 461 | // uniquely identify the packet we want. This function attempts to find |
| 462 | // a PTS we can seek to which will let us figure out which packet we're |
| 463 | // on before we get to the packet we actually wanted |
| 464 | |
| 465 | // MatroskaAudioSource doesn't need this, as it seeks by byte offset |
| 466 | // rather than PTS. LAVF theoretically can seek by byte offset, but we |
| 467 | // don't use it as not all demuxers support it and it's broken in some of |
| 468 | // those that claim to support it |
| 469 | |
| 470 | // However much we might wish to, we can't seek to before packet zero |
| 471 | if (PacketNumber == 0) return PacketNumber; |
| 472 | |
| 473 | // Desired packet's PTS is unique, so don't do anything |
| 474 | if (Frames[PacketNumber].PTS != Frames[PacketNumber - 1].PTS && |
| 475 | (PacketNumber + 1 == Frames.size() || Frames[PacketNumber].PTS != Frames[PacketNumber + 1].PTS)) |
| 476 | return PacketNumber; |
| 477 | |
| 478 | // When decoding, we only reliably know what packet we're at when the |
| 479 | // newly parsed packet has a different PTS from the previous one. As such, |
| 480 | // we walk backwards until we hit a different PTS and then seek to there, |
| 481 | // so that we can then decode until we hit the PTS group we actually wanted |
| 482 | // (and thereby know that we're at the first packet in the group rather |
| 483 | // than whatever the splitter happened to choose) |
| 484 | |
| 485 | // This doesn't work if our desired packet has the same PTS as the first |
| 486 | // packet, but this scenario should never come up anyway; we permanently |
| 487 | // cache the decoded results from those packets, so there's no need to ever |
| 488 | // seek to them |
| 489 | int64_t PTS = Frames[PacketNumber].PTS; |
| 490 | while (PacketNumber > 0 && PTS == Frames[PacketNumber].PTS) |
| 491 | --PacketNumber; |
| 492 | return PacketNumber; |
| 493 | } |
| 494 | |
| 495 | void FFMS_AudioSource::OpenFile() { |
| 496 | avcodec_free_context(&CodecContext); |