Skip to content

Commit

Permalink
Closes #162: If not present, add time stamps to the copied streams
Browse files Browse the repository at this point in the history
  • Loading branch information
nschlia committed Aug 9, 2024
1 parent 44ed90c commit cae2930
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 12 deletions.
2 changes: 2 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ History
**New in 2.17 (2024-08-XX):**

- **Bugfix:** Wrong error message fixed when an invalid audio/video codec was selected. The message should rather say "unsupported codec" instead of talking about "sample format not supported.".
- **Bugfix:** Issue [#162](https://github.com/nschlia/ffmpegfs/issues/162): If not present, add time stamps to the copied streams.

**New in 2.16 (2024-06-10):**

- **Bugfix:** Issue [#160](https://github.com/nschlia/ffmpegfs/issues/160): Fix build with FFmpeg 7.0. [Debian Bug #1072412](https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1072412). write_packet() now with const buffer as of Libavformat 61+.
Expand Down
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Important changes in 2.17 (2024-08-XX):
* Bugfix: Wrong error message fixed when an invalid audio/video codec was
selected. The message should rather say "unsupported codec" instead of
talking about "sample format not supported.".
* Bugfix: Issue #162: If not present, add time stamps to the copied streams.

Important changes in 2.16 (2024-06-10):

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ To see what's been done so far, checkout the [windows](https://github.com/nschli
**New in 2.17 (2024-08-XX):**

- **Bugfix:** Wrong error message fixed when an invalid audio/video codec was selected. The message should rather say "unsupported codec" instead of talking about "sample format not supported.".
- **Bugfix:** Issue [#162](https://github.com/nschlia/ffmpegfs/issues/162): If not present, add time stamps to the copied streams.

**New in 2.16 (2024-06-10):**

Expand Down
54 changes: 42 additions & 12 deletions src/ffmpeg_transcoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ FFmpeg_Transcoder::FFmpeg_Transcoder()
, m_insert_keyframe(true)
, m_copy_audio(false)
, m_copy_video(false)
, m_cur_audio_ts(0)
, m_cur_video_ts(0)
, m_current_format(nullptr)
, m_buffer(nullptr)
, m_reset_pts(0)
Expand Down Expand Up @@ -3745,6 +3747,33 @@ int FFmpeg_Transcoder::store_packet(AVPacket *pkt, AVMediaType mediatype)
return ret;
}

void FFmpeg_Transcoder::make_pts(AVPacket *pkt, int64_t *cur_ts) const
{
if (pkt->pts != AV_NOPTS_VALUE)
{
*cur_ts = pkt->pts + pkt->duration;
}
else if (pkt->dts != AV_NOPTS_VALUE)
{
*cur_ts = pkt->dts;
}

if (pkt->duration)
{
if (pkt->pts == AV_NOPTS_VALUE)
{
pkt->pts = *cur_ts;
}

*cur_ts += pkt->duration;

if (pkt->dts == AV_NOPTS_VALUE)
{
pkt->dts = *cur_ts;
}
}
}

int FFmpeg_Transcoder::decode_frame(AVPacket *pkt)
{
int ret = 0;
Expand All @@ -3771,18 +3800,13 @@ int FFmpeg_Transcoder::decode_frame(AVPacket *pkt)
{
// Simply copy packet without recoding
pkt->stream_index = m_out.m_audio.m_stream_idx;
if (pkt->pts != AV_NOPTS_VALUE)
{
pkt->pts = ffmpeg_rescale_q_rnd(pkt->pts, m_in.m_audio.m_stream->time_base, m_out.m_audio.m_stream->time_base);
}
if (pkt->dts != AV_NOPTS_VALUE)
{
pkt->dts = ffmpeg_rescale_q_rnd(pkt->dts, m_in.m_audio.m_stream->time_base, m_out.m_audio.m_stream->time_base);
}
if (pkt->duration)
{
pkt->duration = static_cast<int>(ffmpeg_rescale_q(pkt->duration, m_in.m_audio.m_stream->time_base, m_out.m_audio.m_stream->time_base));
}

// Rescale packet
av_packet_rescale_ts(pkt, m_in.m_audio.m_stream->time_base, m_out.m_audio.m_stream->time_base);

// Make PTS/DTS if missing
make_pts(pkt, &m_cur_audio_ts);

pkt->pos = -1;

ret = store_packet(pkt, AVMEDIA_TYPE_AUDIO);
Expand Down Expand Up @@ -3859,7 +3883,13 @@ int FFmpeg_Transcoder::decode_frame(AVPacket *pkt)
{
// Simply copy packet without recoding
pkt->stream_index = m_out.m_video.m_stream_idx;

// Rescale packet
av_packet_rescale_ts(pkt, m_in.m_video.m_stream->time_base, m_out.m_video.m_stream->time_base);

// Make PTS/DTS if missing
make_pts(pkt, &m_cur_video_ts);

pkt->pos = -1;

ret = store_packet(pkt, AVMEDIA_TYPE_VIDEO);
Expand Down
12 changes: 12 additions & 0 deletions src/ffmpeg_transcoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,14 @@ class FFmpeg_Transcoder : public FFmpeg_Base, FFmpeg_Profiles
* @return On success, returns 0; on error, a negative AVERROR value.
*/
int decode_subtitle(AVCodecContext *codec_ctx, AVPacket *pkt, int *decoded, int out_stream_idx);
/**
* @brief Create PTS/DTS and update the packet.
* If the update packet lacks time stamps, create a fictitious PTS or DTS and update it.
* If the packet already has valid time stamps, nothing is changed.
* @param[inout] pkt - Audio/video packet to update.
* @param[inout] cur_ts - Current time stamp, will be updated to the next position.
*/
void make_pts(AVPacket *pkt, int64_t *cur_ts) const;
/**
* @brief Decode one frame.
* @param[in] pkt - Packet to decode.
Expand Down Expand Up @@ -1251,6 +1259,10 @@ class FFmpeg_Transcoder : public FFmpeg_Base, FFmpeg_Profiles
bool m_copy_audio; /**< @brief If true, copy audio stream from source to target (just remux, no recode). */
bool m_copy_video; /**< @brief If true, copy video stream from source to target (just remux, no recode). */

// Time stamps
int64_t m_cur_audio_ts; /**< @brief If the audio stream is copied and the time stamps are absent from the input stream, we have to generate them. */
int64_t m_cur_video_ts; /**< @brief If the video stream is copied and the time stamps are absent from the input stream, we have to generate them. */

const FFmpegfs_Format * m_current_format; /**< @brief Currently used output format(s) */

Buffer * m_buffer; /**< @brief Pointer to cache buffer object */
Expand Down

0 comments on commit cae2930

Please sign in to comment.