Open input/output separately, and read/write separately

This is a refactor of the library API so that we will be able to have
multiple outputs.
This commit is contained in:
Will Storey
2017-01-01 14:58:29 -08:00
parent 1ae87a3c72
commit 37325f57b0
4 changed files with 255 additions and 140 deletions

View File

@@ -34,77 +34,55 @@ vs_setup(void)
avformat_network_init();
}
struct Videostreamer *
vs_open(const char * const input_format_name, const char * const input_url,
const char * const output_format_name, const char * const output_url,
const bool verbose)
struct VSInput *
vs_open_input(const char * const input_format_name,
const char * const input_url, const bool verbose)
{
if (!input_format_name || strlen(input_format_name) == 0 ||
!input_url || strlen(input_url) == 0 ||
!output_format_name || strlen(output_format_name) == 0 ||
!output_url || strlen(output_url) == 0) {
!input_url || strlen(input_url) == 0) {
printf("%s\n", strerror(EINVAL));
return NULL;
}
struct Videostreamer * const vs = calloc(1, sizeof(struct Videostreamer));
if (!vs) {
struct VSInput * const input = calloc(1, sizeof(struct VSInput));
if (!input) {
printf("%s\n", strerror(errno));
return NULL;
}
// Open input.
AVInputFormat * const input_format = av_find_input_format(input_format_name);
if (!input_format) {
printf("input format not found\n");
vs_destroy(vs);
vs_destroy_input(input);
return NULL;
}
if (avformat_open_input(&vs->input_format_ctx, input_url, input_format,
if (avformat_open_input(&input->format_ctx, input_url, input_format,
NULL) != 0) {
printf("unable to open input\n");
vs_destroy(vs);
vs_destroy_input(input);
return NULL;
}
if (avformat_find_stream_info(vs->input_format_ctx, NULL) < 0) {
if (avformat_find_stream_info(input->format_ctx, NULL) < 0) {
printf("failed to find stream info\n");
vs_destroy(vs);
vs_destroy_input(input);
return NULL;
}
if (verbose) {
av_dump_format(vs->input_format_ctx, 0, input_url, 0);
av_dump_format(input->format_ctx, 0, input_url, 0);
}
// Open output.
// Find the first video stream.
AVOutputFormat * const output_format = av_guess_format(output_format_name,
NULL, NULL);
if (!output_format) {
printf("output format not found\n");
vs_destroy(vs);
return NULL;
}
input->video_stream_index = -1;
if (avformat_alloc_output_context2(&vs->output_format_ctx, output_format,
NULL, NULL) < 0) {
printf("unable to create output context\n");
vs_destroy(vs);
return NULL;
}
// Copy the first video stream.
vs->video_stream_index = -1;
for (unsigned int i = 0; i < vs->input_format_ctx->nb_streams; i++) {
AVStream * const in_stream = vs->input_format_ctx->streams[i];
for (unsigned int i = 0; i < input->format_ctx->nb_streams; i++) {
AVStream * const in_stream = input->format_ctx->streams[i];
if (in_stream->codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
if (verbose) {
@@ -113,41 +91,100 @@ vs_open(const char * const input_format_name, const char * const input_url,
continue;
}
AVStream * const out_stream = avformat_new_stream(vs->output_format_ctx,
NULL);
if (!out_stream) {
printf("unable to add stream\n");
vs_destroy(vs);
return NULL;
}
if (avcodec_parameters_copy(out_stream->codecpar,
in_stream->codecpar) < 0) {
printf("unable to copy codec parameters\n");
vs_destroy(vs);
return NULL;
}
// I take the first video stream only.
vs->video_stream_index = (int) i;
input->video_stream_index = (int) i;
break;
}
if (vs->video_stream_index == -1) {
if (input->video_stream_index == -1) {
printf("no video stream found\n");
vs_destroy(vs);
vs_destroy_input(input);
return NULL;
}
return input;
}
void
vs_destroy_input(struct VSInput * const input)
{
if (!input) {
return;
}
if (input->format_ctx) {
avformat_close_input(&input->format_ctx);
avformat_free_context(input->format_ctx);
}
free(input);
}
struct VSOutput *
vs_open_output(const char * const output_format_name,
const char * const output_url, const struct VSInput * const input,
const bool verbose)
{
if (!output_format_name || strlen(output_format_name) == 0 ||
!output_url || strlen(output_url) == 0 ||
!input) {
printf("%s\n", strerror(EINVAL));
return NULL;
}
struct VSOutput * const output = calloc(1, sizeof(struct VSOutput));
if (!output) {
printf("%s\n", strerror(errno));
return NULL;
}
AVOutputFormat * const output_format = av_guess_format(output_format_name,
NULL, NULL);
if (!output_format) {
printf("output format not found\n");
vs_destroy_output(output);
return NULL;
}
if (avformat_alloc_output_context2(&output->format_ctx, output_format,
NULL, NULL) < 0) {
printf("unable to create output context\n");
vs_destroy_output(output);
return NULL;
}
// Copy the video stream.
AVStream * const out_stream = avformat_new_stream(output->format_ctx, NULL);
if (!out_stream) {
printf("unable to add stream\n");
vs_destroy_output(output);
return NULL;
}
AVStream * const in_stream = input->format_ctx->streams[
input->video_stream_index];
if (avcodec_parameters_copy(out_stream->codecpar,
in_stream->codecpar) < 0) {
printf("unable to copy codec parameters\n");
vs_destroy_output(output);
return NULL;
}
if (verbose) {
av_dump_format(vs->output_format_ctx, 0, output_url, 1);
av_dump_format(output->format_ctx, 0, output_url, 1);
}
// Open output file.
if (avio_open(&vs->output_format_ctx->pb, output_url, AVIO_FLAG_WRITE) < 0) {
if (avio_open(&output->format_ctx->pb, output_url, AVIO_FLAG_WRITE) < 0) {
printf("unable to open output file\n");
vs_destroy(vs);
vs_destroy_output(output);
return NULL;
}
@@ -172,20 +209,20 @@ vs_open(const char * const input_format_name, const char * const input_url,
// empty_moov apparently writes some info at the start of the file.
if (av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov", 0) < 0) {
printf("unable to set movflags opt\n");
vs_destroy(vs);
vs_destroy_output(output);
return NULL;
}
if (av_dict_set_int(&opts, "flush_packets", 1, 0) < 0) {
printf("unable to set flush_packets opt\n");
vs_destroy(vs);
vs_destroy_output(output);
av_dict_free(&opts);
return NULL;
}
if (avformat_write_header(vs->output_format_ctx, &opts) < 0) {
if (avformat_write_header(output->format_ctx, &opts) < 0) {
printf("unable to write header\n");
vs_destroy(vs);
vs_destroy_output(output);
av_dict_free(&opts);
return NULL;
}
@@ -195,7 +232,7 @@ vs_open(const char * const input_format_name, const char * const input_url,
// appropriate to set through the avformat_write_header().
if (av_dict_count(opts) != 0) {
printf("some options not set\n");
vs_destroy(vs);
vs_destroy_output(output);
av_dict_free(&opts);
return NULL;
}
@@ -203,103 +240,120 @@ vs_open(const char * const input_format_name, const char * const input_url,
av_dict_free(&opts);
return vs;
return output;
}
void
vs_destroy(struct Videostreamer * const vs)
vs_destroy_output(struct VSOutput * const output)
{
if (!vs) {
if (!output) {
return;
}
if (vs->input_format_ctx) {
avformat_close_input(&vs->input_format_ctx);
avformat_free_context(vs->input_format_ctx);
}
if (vs->output_format_ctx) {
// Write file trailer.
if (av_write_trailer(vs->output_format_ctx) != 0) {
if (output->format_ctx) {
if (av_write_trailer(output->format_ctx) != 0) {
printf("unable to write trailer\n");
}
if (avio_closep(&vs->output_format_ctx->pb) != 0) {
if (avio_closep(&output->format_ctx->pb) != 0) {
printf("avio_closep failed\n");
}
avformat_free_context(vs->output_format_ctx);
avformat_free_context(output->format_ctx);
}
free(vs);
free(output);
}
// Read a compressed and encoded frame as a packet.
//
// Returns:
// -1 if error
// 0 if nothing useful read (e.g., non-video packet)
// 1 if read a packet
int
vs_read_write(const struct Videostreamer * vs, const bool verbose)
vs_read_packet(const struct VSInput * input, AVPacket * const pkt,
const bool verbose)
{
if (!vs) {
if (!input || !pkt) {
printf("%s\n", strerror(errno));
return -1;
}
memset(pkt, 0, sizeof(AVPacket));
// Read encoded frame (as a packet).
AVPacket pkt;
memset(&pkt, 0, sizeof(AVPacket));
if (av_read_frame(vs->input_format_ctx, &pkt) != 0) {
if (av_read_frame(input->format_ctx, pkt) != 0) {
printf("unable to read frame\n");
return -1;
}
// Ignore it if it's not our video stream.
if (pkt.stream_index != vs->video_stream_index) {
if (pkt->stream_index != input->video_stream_index) {
if (verbose) {
printf("skipping non video packet\n");
}
av_packet_unref(pkt);
return 0;
}
AVStream * const in_stream = vs->input_format_ctx->streams[pkt.stream_index];
AVStream * const out_stream =
vs->output_format_ctx->streams[pkt.stream_index];
if (verbose) {
__vs_log_packet(vs->input_format_ctx, &pkt, "in");
__vs_log_packet(input->format_ctx, pkt, "in");
}
return 1;
}
// Copy packet
pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base,
// We change the packet's pts, dts, duration, pos.
//
// We do not unref it.
//
// Returns:
// -1 if error
// 1 if wrote packet
int
vs_write_packet(const struct VSInput * const input,
const struct VSOutput * const output, AVPacket * const pkt,
const bool verbose)
{
if (!input || !output || !pkt) {
printf("%s\n", strerror(EINVAL));
return -1;
}
AVStream * const in_stream = input->format_ctx->streams[pkt->stream_index];
AVStream * const out_stream = output->format_ctx->streams[pkt->stream_index];
pkt->pts = av_rescale_q_rnd(pkt->pts, in_stream->time_base,
out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base,
pkt->dts = av_rescale_q_rnd(pkt->dts, in_stream->time_base,
out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base,
pkt->duration = av_rescale_q(pkt->duration, in_stream->time_base,
out_stream->time_base);
pkt.pos = -1;
pkt->pos = -1;
if (verbose) {
__vs_log_packet(vs->output_format_ctx, &pkt, "out");
__vs_log_packet(output->format_ctx, pkt, "out");
}
const int pkt_size = pkt.size;
// Write encoded frame (as a packet).
// av_interleaved_write_frame() works too, but I don't think it is needed.
// Using av_write_frame() skips buffering.
if (av_write_frame(vs->output_format_ctx, &pkt) != 0) {
if (av_write_frame(output->format_ctx, pkt) != 0) {
printf("unable to write frame\n");
av_packet_unref(&pkt);
return -1;
}
av_packet_unref(&pkt);
return pkt_size;
return 1;
}
static void