Skip to content

Commit

Permalink
fix(concat): fix stream alignments
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkness4 committed Aug 21, 2024
1 parent 8023372 commit d7a01a6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
36 changes: 31 additions & 5 deletions video/concat/concat.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ int concat(void *ctx, const char *output_file, size_t input_files_count,
// stream_mapping is mapping from input stream index to output stream index.
int **stream_mapping = NULL;
int *stream_mapping_size = NULL;
enum AVMediaType *stream_mapping_codec = NULL;

// last_pts and last_dts used for concatenation. Size is
// input_files_count*stream_mapping_size.
Expand Down Expand Up @@ -175,6 +176,15 @@ int concat(void *ctx, const char *output_file, size_t input_files_count,
ret = AVERROR(ENOMEM);
goto end;
}
if (input_idx == 0) {
stream_mapping_codec =
arena_alloc(&arena, stream_mapping_size[input_idx] *
sizeof(*stream_mapping_codec));
if (!stream_mapping_codec) {
ret = AVERROR(ENOMEM);
goto end;
}
}
dts_offset = arena_alloc(&arena, stream_mapping_size[input_idx] *
sizeof(*dts_offset));
if (!dts_offset) {
Expand Down Expand Up @@ -217,7 +227,22 @@ int concat(void *ctx, const char *output_file, size_t input_files_count,
continue;
}

stream_mapping[input_idx][i] = stream_index++;
if (input_idx == 0) { // Input 0 gets to choose the mapping.
stream_mapping[input_idx][i] = stream_index;
stream_mapping_codec[stream_index] = in_codecpar->codec_type;
stream_index++;
} else {
// Find the stream in the first input that matches the current stream.
for (unsigned int j = 0; j < stream_mapping_size[0]; j++) {
if (stream_mapping[0][j] >= 0 &&
stream_mapping_codec[stream_mapping[0][j]] ==
in_codecpar->codec_type) {
stream_mapping[input_idx][i] = stream_mapping[0][j];
break;
}
}
}

const int out_stream_index = stream_mapping[input_idx][i];
fprintf(stderr, "Input %zu, mapping stream %d (%s) to output stream %d\n",
input_idx, i, av_get_media_type_string(in_codecpar->codec_type),
Expand Down Expand Up @@ -303,14 +328,15 @@ int concat(void *ctx, const char *output_file, size_t input_files_count,

fix_ts(dts_offset, prev_dts, prev_duration, input_idx, pkt);

if ((ret = av_interleaved_write_frame(ofmt_ctx, pkt)) < 0) {
ret = av_interleaved_write_frame(ofmt_ctx, pkt);
/* pkt is now blank (av_interleaved_write_frame() takes ownership of
* its contents and resets pkt), so that no unreferencing is
* necessary. This would be different if one used av_write_frame(). */
if (ret < 0) {
fprintf(stderr, "Error writing packet to output file: %s\n",
av_err2str(ret));
av_packet_unref(pkt);
break;
}

av_packet_unref(pkt);
} // while packets.

goTraceProcessInputEnd(span);
Expand Down
1 change: 1 addition & 0 deletions video/concat/concat.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func Do(ctx context.Context, output string, inputs []string, opts ...Option) err

// If mixed formats (adts vs asc), we should remux the others first using intermediates or FIFO
if areFormatMixed(validInputs) {
log.Warn().Msg("mixed formats detected, using intermediates or FIFO to remux files first")
i, useFIFO, err := remuxMixedTS(ctx, validInputs, opts...)
if err != nil {
span.RecordError(err)
Expand Down
2 changes: 1 addition & 1 deletion video/concat/concat_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

func TestDo(t *testing.T) {
ctx := context.Background()
shut, err := telemetry.SetupOTELSDK(ctx, telemetry.WithStdout())
shut, err := telemetry.SetupOTELSDK(ctx)
defer shut(ctx)
require.NoError(t, err)
err = concat.Do(ctx, "output.mp4", []string{"input.ts", "input.mp4"})
Expand Down

0 comments on commit d7a01a6

Please sign in to comment.