Commit 5a55d5e4 authored by Michael Niedermayer's avatar Michael Niedermayer

Merge commit '63fd0d86'

* commit '63fd0d86':
  output example: allocate the audio frame only once

Conflicts:
	doc/examples/muxing.c
Merged-by: 's avatarMichael Niedermayer <michaelni@gmx.at>
parents b9bfd888 63fd0d86
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
#include <libavutil/avassert.h>
#include <libavutil/channel_layout.h> #include <libavutil/channel_layout.h>
#include <libavutil/opt.h> #include <libavutil/opt.h>
#include <libavutil/mathematics.h> #include <libavutil/mathematics.h>
...@@ -58,7 +59,6 @@ typedef struct OutputStream { ...@@ -58,7 +59,6 @@ typedef struct OutputStream {
AVFrame *tmp_frame; AVFrame *tmp_frame;
float t, tincr, tincr2; float t, tincr, tincr2;
int audio_input_frame_size;
} OutputStream; } OutputStream;
static void log_packet(const AVFormatContext *fmt_ctx, const AVPacket *pkt) static void log_packet(const AVFormatContext *fmt_ctx, const AVPacket *pkt)
...@@ -179,10 +179,27 @@ static void open_audio(AVFormatContext *oc, AVCodec *codec, OutputStream *ost) ...@@ -179,10 +179,27 @@ static void open_audio(AVFormatContext *oc, AVCodec *codec, OutputStream *ost)
/* increment frequency by 110 Hz per second */ /* increment frequency by 110 Hz per second */
ost->tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate; ost->tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate;
ost->frame = av_frame_alloc();
if (!ost->frame)
exit(1);
ost->frame->sample_rate = c->sample_rate;
ost->frame->format = AV_SAMPLE_FMT_S16;
ost->frame->channel_layout = c->channel_layout;
if (c->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) if (c->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)
ost->audio_input_frame_size = 10000; ost->frame->nb_samples = 10000;
else else
ost->audio_input_frame_size = c->frame_size; ost->frame->nb_samples = c->frame_size;
ost->tmp_frame = av_frame_alloc();
if (!ost->frame)
exit(1);
ost->tmp_frame->sample_rate = c->sample_rate;
ost->tmp_frame->format = c->sample_fmt;
ost->tmp_frame->channel_layout = c->channel_layout;
ost->tmp_frame->nb_samples = ost->frame->nb_samples;
/* create resampler context */ /* create resampler context */
if (c->sample_fmt != AV_SAMPLE_FMT_S16) { if (c->sample_fmt != AV_SAMPLE_FMT_S16) {
...@@ -206,6 +223,17 @@ static void open_audio(AVFormatContext *oc, AVCodec *codec, OutputStream *ost) ...@@ -206,6 +223,17 @@ static void open_audio(AVFormatContext *oc, AVCodec *codec, OutputStream *ost)
exit(1); exit(1);
} }
} }
ret = av_frame_get_buffer(ost->frame, 0);
if (ret < 0) {
fprintf(stderr, "Could not allocate an audio frame.\n");
exit(1);
}
ret = av_frame_get_buffer(ost->tmp_frame, 0);
if (ret < 0) {
fprintf(stderr, "Could not allocate an audio frame.\n");
exit(1);
}
} }
/* Prepare a 16 bit dummy audio frame of 'frame_size' samples and /* Prepare a 16 bit dummy audio frame of 'frame_size' samples and
...@@ -236,58 +264,37 @@ static void write_audio_frame(AVFormatContext *oc, OutputStream *ost, int flush) ...@@ -236,58 +264,37 @@ static void write_audio_frame(AVFormatContext *oc, OutputStream *ost, int flush)
{ {
AVCodecContext *c; AVCodecContext *c;
AVPacket pkt = { 0 }; // data and size must be 0; AVPacket pkt = { 0 }; // data and size must be 0;
AVFrame *frame = av_frame_alloc();
int got_packet, ret; int got_packet, ret;
int dst_nb_samples; int dst_nb_samples;
AVFrame *frame;
av_init_packet(&pkt); av_init_packet(&pkt);
c = ost->st->codec; c = ost->st->codec;
if (!flush) { if (!flush) {
frame->sample_rate = c->sample_rate; get_audio_frame(ost, ost->frame, c->channels);
frame->nb_samples = ost->audio_input_frame_size;
frame->format = AV_SAMPLE_FMT_S16;
frame->channel_layout = c->channel_layout;
ret = av_frame_get_buffer(frame, 0);
if (ret < 0) {
fprintf(stderr, "Could not allocate an audio frame.\n");
exit(1);
}
get_audio_frame(ost, frame, c->channels);
/* convert samples from native format to destination codec format, using the resampler */ /* convert samples from native format to destination codec format, using the resampler */
if (swr_ctx) { if (swr_ctx) {
AVFrame *tmp_frame = av_frame_alloc();
/* compute destination number of samples */ /* compute destination number of samples */
dst_nb_samples = av_rescale_rnd(swr_get_delay(swr_ctx, c->sample_rate) + ost->audio_input_frame_size, dst_nb_samples = av_rescale_rnd(swr_get_delay(swr_ctx, c->sample_rate) + ost->frame->nb_samples,
c->sample_rate, c->sample_rate, AV_ROUND_UP); c->sample_rate, c->sample_rate, AV_ROUND_UP);
tmp_frame->sample_rate = c->sample_rate; av_assert0(dst_nb_samples == ost->frame->nb_samples);
tmp_frame->nb_samples = dst_nb_samples;
tmp_frame->format = c->sample_fmt;
tmp_frame->channel_layout = c->channel_layout;
ret = av_frame_get_buffer(tmp_frame, 0);
if (ret < 0) {
fprintf(stderr, "Could not allocate an audio frame.\n");
exit(1);
}
/* convert to destination format */ /* convert to destination format */
ret = swr_convert(swr_ctx, ret = swr_convert(swr_ctx,
tmp_frame->data, dst_nb_samples, ost->tmp_frame->data, dst_nb_samples,
(const uint8_t **)frame->data, ost->audio_input_frame_size); (const uint8_t **)ost->frame->data, ost->frame->nb_samples);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "Error while converting\n"); fprintf(stderr, "Error while converting\n");
exit(1); exit(1);
} }
av_frame_free(&frame); frame = ost->tmp_frame;
frame = tmp_frame;
} else { } else {
dst_nb_samples = ost->audio_input_frame_size; dst_nb_samples = ost->frame->nb_samples;
frame = ost->frame;
} }
frame->nb_samples = dst_nb_samples;
frame->pts = av_rescale_q(samples_count, (AVRational){1, c->sample_rate}, c->time_base); frame->pts = av_rescale_q(samples_count, (AVRational){1, c->sample_rate}, c->time_base);
samples_count += dst_nb_samples; samples_count += dst_nb_samples;
} }
...@@ -301,6 +308,7 @@ static void write_audio_frame(AVFormatContext *oc, OutputStream *ost, int flush) ...@@ -301,6 +308,7 @@ static void write_audio_frame(AVFormatContext *oc, OutputStream *ost, int flush)
if (!got_packet) { if (!got_packet) {
if (flush) if (flush)
audio_is_eof = 1; audio_is_eof = 1;
return; return;
} }
...@@ -312,11 +320,6 @@ static void write_audio_frame(AVFormatContext *oc, OutputStream *ost, int flush) ...@@ -312,11 +320,6 @@ static void write_audio_frame(AVFormatContext *oc, OutputStream *ost, int flush)
} }
} }
static void close_audio(AVFormatContext *oc, OutputStream *ost)
{
avcodec_close(ost->st->codec);
}
/**************************************************************/ /**************************************************************/
/* video output */ /* video output */
...@@ -477,7 +480,7 @@ static void write_video_frame(AVFormatContext *oc, OutputStream *ost, int flush) ...@@ -477,7 +480,7 @@ static void write_video_frame(AVFormatContext *oc, OutputStream *ost, int flush)
frame_count++; frame_count++;
} }
static void close_video(AVFormatContext *oc, OutputStream *ost) static void close_stream(AVFormatContext *oc, OutputStream *ost)
{ {
avcodec_close(ost->st->codec); avcodec_close(ost->st->codec);
av_frame_free(&ost->frame); av_frame_free(&ost->frame);
...@@ -489,7 +492,7 @@ static void close_video(AVFormatContext *oc, OutputStream *ost) ...@@ -489,7 +492,7 @@ static void close_video(AVFormatContext *oc, OutputStream *ost)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
OutputStream video_st, audio_st; OutputStream video_st = { 0 }, audio_st = { 0 };
const char *filename; const char *filename;
AVOutputFormat *fmt; AVOutputFormat *fmt;
AVFormatContext *oc; AVFormatContext *oc;
...@@ -592,9 +595,9 @@ int main(int argc, char **argv) ...@@ -592,9 +595,9 @@ int main(int argc, char **argv)
/* Close each codec. */ /* Close each codec. */
if (have_video) if (have_video)
close_video(oc, &video_st); close_stream(oc, &video_st);
if (have_audio) if (have_audio)
close_audio(oc, &audio_st); close_stream(oc, &audio_st);
if (!(fmt->flags & AVFMT_NOFILE)) if (!(fmt->flags & AVFMT_NOFILE))
/* Close the output file. */ /* Close the output file. */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment