Skip to content

Commit

Permalink
fix: fixed audio/subtitle timestamp rescaling
Browse files Browse the repository at this point in the history
Signed-off-by: k4yt3x <[email protected]>
  • Loading branch information
k4yt3x committed Oct 10, 2024
1 parent c023595 commit af5aa08
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
21 changes: 13 additions & 8 deletions src/encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ int init_encoder(
return AVERROR_UNKNOWN;
}

// Initialize the stream map
*stream_mapping = (int *)av_malloc_array(ifmt_ctx->nb_streams, sizeof(**stream_mapping));
if (!*stream_mapping) {
fprintf(stderr, "Could not allocate stream mapping\n");
return AVERROR(ENOMEM);
}

const AVCodec *encoder = avcodec_find_encoder(encoder_config->codec);
if (!encoder) {
fprintf(
Expand Down Expand Up @@ -121,6 +114,16 @@ int init_encoder(
out_stream->time_base = codec_ctx->time_base;

if (encoder_config->copy_streams) {
// Allocate the stream map
*stream_mapping = (int *)av_malloc_array(ifmt_ctx->nb_streams, sizeof(**stream_mapping));
if (!*stream_mapping) {
fprintf(stderr, "Could not allocate stream mapping\n");
return AVERROR(ENOMEM);
}

// Map the video stream
(*stream_mapping)[video_stream_index] = stream_index++;

// Loop through each stream in the input file
for (int i = 0; i < ifmt_ctx->nb_streams; i++) {
AVStream *in_stream = ifmt_ctx->streams[i];
Expand Down Expand Up @@ -218,7 +221,9 @@ int encode_and_write_frame(
}

// Rescale packet timestamps
av_packet_rescale_ts(enc_pkt, enc_ctx->time_base, ofmt_ctx->streams[0]->time_base);
av_packet_rescale_ts(
enc_pkt, enc_ctx->time_base, ofmt_ctx->streams[video_stream_index]->time_base
);
enc_pkt->stream_index = video_stream_index;

// Write the packet
Expand Down
12 changes: 3 additions & 9 deletions src/libvideo2x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,9 @@ int process_frames(
int out_stream_index = stream_mapping[packet.stream_index];
AVStream *out_stream = ofmt_ctx->streams[out_stream_index];

// Rescale packet pts
packet.pts = av_rescale_q_rnd(
packet.pts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF
);

// Rescale packet dts
packet.dts = av_rescale_q_rnd(
packet.dts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF
);
// Rescale packet timestamps
av_packet_rescale_ts(&packet, in_stream->time_base, out_stream->time_base);
packet.stream_index = out_stream_index;

// If copy streams is enabled, copy the packet to the output
ret = av_interleaved_write_frame(ofmt_ctx, &packet);
Expand Down

0 comments on commit af5aa08

Please sign in to comment.