Merge commit '9897d9f4e074cdc6c7f2409885ddefe300f18dc7'
* commit '9897d9f4e074cdc6c7f2409885ddefe300f18dc7': examples/output: convert to codecpar Merged-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>
This commit is contained in:
commit
d76972b581
@ -52,6 +52,7 @@
|
|||||||
// a wrapper around a single output AVStream
|
// a wrapper around a single output AVStream
|
||||||
typedef struct OutputStream {
|
typedef struct OutputStream {
|
||||||
AVStream *st;
|
AVStream *st;
|
||||||
|
AVCodecContext *enc;
|
||||||
|
|
||||||
/* pts of the next frame that will be generated */
|
/* pts of the next frame that will be generated */
|
||||||
int64_t next_pts;
|
int64_t next_pts;
|
||||||
@ -104,13 +105,18 @@ static void add_stream(OutputStream *ost, AVFormatContext *oc,
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
ost->st = avformat_new_stream(oc, *codec);
|
ost->st = avformat_new_stream(oc, NULL);
|
||||||
if (!ost->st) {
|
if (!ost->st) {
|
||||||
fprintf(stderr, "Could not allocate stream\n");
|
fprintf(stderr, "Could not allocate stream\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
ost->st->id = oc->nb_streams-1;
|
ost->st->id = oc->nb_streams-1;
|
||||||
c = ost->st->codec;
|
c = avcodec_alloc_context3(*codec);
|
||||||
|
if (!c) {
|
||||||
|
fprintf(stderr, "Could not alloc an encoding context\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
ost->enc = c;
|
||||||
|
|
||||||
switch ((*codec)->type) {
|
switch ((*codec)->type) {
|
||||||
case AVMEDIA_TYPE_AUDIO:
|
case AVMEDIA_TYPE_AUDIO:
|
||||||
@ -213,7 +219,7 @@ static void open_audio(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, A
|
|||||||
int ret;
|
int ret;
|
||||||
AVDictionary *opt = NULL;
|
AVDictionary *opt = NULL;
|
||||||
|
|
||||||
c = ost->st->codec;
|
c = ost->enc;
|
||||||
|
|
||||||
/* open it */
|
/* open it */
|
||||||
av_dict_copy(&opt, opt_arg, 0);
|
av_dict_copy(&opt, opt_arg, 0);
|
||||||
@ -240,6 +246,13 @@ static void open_audio(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, A
|
|||||||
ost->tmp_frame = alloc_audio_frame(AV_SAMPLE_FMT_S16, c->channel_layout,
|
ost->tmp_frame = alloc_audio_frame(AV_SAMPLE_FMT_S16, c->channel_layout,
|
||||||
c->sample_rate, nb_samples);
|
c->sample_rate, nb_samples);
|
||||||
|
|
||||||
|
/* copy the stream parameters to the muxer */
|
||||||
|
ret = avcodec_parameters_from_context(ost->st->codecpar, c);
|
||||||
|
if (ret < 0) {
|
||||||
|
fprintf(stderr, "Could not copy the stream parameters\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
/* create resampler context */
|
/* create resampler context */
|
||||||
ost->swr_ctx = swr_alloc();
|
ost->swr_ctx = swr_alloc();
|
||||||
if (!ost->swr_ctx) {
|
if (!ost->swr_ctx) {
|
||||||
@ -271,13 +284,13 @@ static AVFrame *get_audio_frame(OutputStream *ost)
|
|||||||
int16_t *q = (int16_t*)frame->data[0];
|
int16_t *q = (int16_t*)frame->data[0];
|
||||||
|
|
||||||
/* check if we want to generate more frames */
|
/* check if we want to generate more frames */
|
||||||
if (av_compare_ts(ost->next_pts, ost->st->codec->time_base,
|
if (av_compare_ts(ost->next_pts, ost->enc->time_base,
|
||||||
STREAM_DURATION, (AVRational){ 1, 1 }) >= 0)
|
STREAM_DURATION, (AVRational){ 1, 1 }) >= 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
for (j = 0; j <frame->nb_samples; j++) {
|
for (j = 0; j <frame->nb_samples; j++) {
|
||||||
v = (int)(sin(ost->t) * 10000);
|
v = (int)(sin(ost->t) * 10000);
|
||||||
for (i = 0; i < ost->st->codec->channels; i++)
|
for (i = 0; i < ost->enc->channels; i++)
|
||||||
*q++ = v;
|
*q++ = v;
|
||||||
ost->t += ost->tincr;
|
ost->t += ost->tincr;
|
||||||
ost->tincr += ost->tincr2;
|
ost->tincr += ost->tincr2;
|
||||||
@ -303,7 +316,7 @@ static int write_audio_frame(AVFormatContext *oc, OutputStream *ost)
|
|||||||
int dst_nb_samples;
|
int dst_nb_samples;
|
||||||
|
|
||||||
av_init_packet(&pkt);
|
av_init_packet(&pkt);
|
||||||
c = ost->st->codec;
|
c = ost->enc;
|
||||||
|
|
||||||
frame = get_audio_frame(ost);
|
frame = get_audio_frame(ost);
|
||||||
|
|
||||||
@ -383,7 +396,7 @@ static AVFrame *alloc_picture(enum AVPixelFormat pix_fmt, int width, int height)
|
|||||||
static void open_video(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg)
|
static void open_video(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
AVCodecContext *c = ost->st->codec;
|
AVCodecContext *c = ost->enc;
|
||||||
AVDictionary *opt = NULL;
|
AVDictionary *opt = NULL;
|
||||||
|
|
||||||
av_dict_copy(&opt, opt_arg, 0);
|
av_dict_copy(&opt, opt_arg, 0);
|
||||||
@ -414,6 +427,13 @@ static void open_video(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, A
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* copy the stream parameters to the muxer */
|
||||||
|
ret = avcodec_parameters_from_context(ost->st->codecpar, c);
|
||||||
|
if (ret < 0) {
|
||||||
|
fprintf(stderr, "Could not copy the stream parameters\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Prepare a dummy image. */
|
/* Prepare a dummy image. */
|
||||||
@ -448,10 +468,10 @@ static void fill_yuv_image(AVFrame *pict, int frame_index,
|
|||||||
|
|
||||||
static AVFrame *get_video_frame(OutputStream *ost)
|
static AVFrame *get_video_frame(OutputStream *ost)
|
||||||
{
|
{
|
||||||
AVCodecContext *c = ost->st->codec;
|
AVCodecContext *c = ost->enc;
|
||||||
|
|
||||||
/* check if we want to generate more frames */
|
/* check if we want to generate more frames */
|
||||||
if (av_compare_ts(ost->next_pts, ost->st->codec->time_base,
|
if (av_compare_ts(ost->next_pts, c->time_base,
|
||||||
STREAM_DURATION, (AVRational){ 1, 1 }) >= 0)
|
STREAM_DURATION, (AVRational){ 1, 1 }) >= 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
@ -495,7 +515,7 @@ static int write_video_frame(AVFormatContext *oc, OutputStream *ost)
|
|||||||
int got_packet = 0;
|
int got_packet = 0;
|
||||||
AVPacket pkt = { 0 };
|
AVPacket pkt = { 0 };
|
||||||
|
|
||||||
c = ost->st->codec;
|
c = ost->enc;
|
||||||
|
|
||||||
frame = get_video_frame(ost);
|
frame = get_video_frame(ost);
|
||||||
|
|
||||||
@ -524,7 +544,7 @@ static int write_video_frame(AVFormatContext *oc, OutputStream *ost)
|
|||||||
|
|
||||||
static void close_stream(AVFormatContext *oc, OutputStream *ost)
|
static void close_stream(AVFormatContext *oc, OutputStream *ost)
|
||||||
{
|
{
|
||||||
avcodec_close(ost->st->codec);
|
avcodec_free_context(&ost->enc);
|
||||||
av_frame_free(&ost->frame);
|
av_frame_free(&ost->frame);
|
||||||
av_frame_free(&ost->tmp_frame);
|
av_frame_free(&ost->tmp_frame);
|
||||||
sws_freeContext(ost->sws_ctx);
|
sws_freeContext(ost->sws_ctx);
|
||||||
@ -620,8 +640,8 @@ int main(int argc, char **argv)
|
|||||||
while (encode_video || encode_audio) {
|
while (encode_video || encode_audio) {
|
||||||
/* select the stream to encode */
|
/* select the stream to encode */
|
||||||
if (encode_video &&
|
if (encode_video &&
|
||||||
(!encode_audio || av_compare_ts(video_st.next_pts, video_st.st->codec->time_base,
|
(!encode_audio || av_compare_ts(video_st.next_pts, video_st.enc->time_base,
|
||||||
audio_st.next_pts, audio_st.st->codec->time_base) <= 0)) {
|
audio_st.next_pts, audio_st.enc->time_base) <= 0)) {
|
||||||
encode_video = !write_video_frame(oc, &video_st);
|
encode_video = !write_video_frame(oc, &video_st);
|
||||||
} else {
|
} else {
|
||||||
encode_audio = !write_audio_frame(oc, &audio_st);
|
encode_audio = !write_audio_frame(oc, &audio_st);
|
||||||
|
Loading…
Reference in New Issue
Block a user