Ensure we always write to output stream 0

If an input has multiple streams, then the packet's stream index for the
video stream we are reading may not match our output's stream index.
This can lead to errors such as:

[mp4 @ 0x5631c5836000] Invalid packet stream index: 1

Update the packet's stream index, prior to writing it, to match what it
is in our output.

I also update a comment (around line 294) to clarify that while we're
skipping a packet, it's not because it's not video.
This commit is contained in:
Will Storey
2017-03-14 11:07:53 -07:00
parent 763dc8fd38
commit 215b19773a

View File

@@ -91,9 +91,7 @@ vs_open_input(const char * const input_format_name,
continue; continue;
} }
// I take the first video stream only.
input->video_stream_index = (int) i; input->video_stream_index = (int) i;
break; break;
} }
@@ -296,7 +294,8 @@ vs_read_packet(const struct VSInput * input, AVPacket * const pkt,
if (pkt->stream_index != input->video_stream_index) { if (pkt->stream_index != input->video_stream_index) {
if (verbose) { if (verbose) {
printf("skipping non video packet\n"); printf("skipping packet from input stream %d, our video is from stream %d\n",
pkt->stream_index, input->video_stream_index);
} }
av_packet_unref(pkt); av_packet_unref(pkt);
@@ -317,7 +316,7 @@ vs_read_packet(const struct VSInput * input, AVPacket * const pkt,
// //
// Returns: // Returns:
// -1 if error // -1 if error
// 1 if wrote packet // 1 if we wrote the packet
int int
vs_write_packet(const struct VSInput * const input, vs_write_packet(const struct VSInput * const input,
const struct VSOutput * const output, AVPacket * const pkt, const struct VSOutput * const output, AVPacket * const pkt,
@@ -329,7 +328,34 @@ vs_write_packet(const struct VSInput * const input,
} }
AVStream * const in_stream = input->format_ctx->streams[pkt->stream_index]; AVStream * const in_stream = input->format_ctx->streams[pkt->stream_index];
if (!in_stream) {
printf("input stream not found with stream index %d\n", pkt->stream_index);
return -1;
}
// If there are multiple input streams, then the stream index on the packet
// may not match the stream index in our output. We need to ensure the index
// matches. Note by this point we have checked that it is indeed a packet
// from the stream we want (we do this when reading the packet).
//
// As we only ever have a single output stream (one, video), the index will
// be 0.
if (pkt->stream_index != 0) {
if (verbose) {
printf("updating packet stream index to 0 (from %d)\n",
pkt->stream_index);
}
pkt->stream_index = 0;
}
AVStream * const out_stream = output->format_ctx->streams[pkt->stream_index]; AVStream * const out_stream = output->format_ctx->streams[pkt->stream_index];
if (!out_stream) {
printf("output stream not found with stream index %d\n", pkt->stream_index);
return -1;
}
// Set pts/dts if not set. Otherwise we will receive warnings like // Set pts/dts if not set. Otherwise we will receive warnings like
// //