/*
- * Libavformat API example: Output a media file in any supported
- * libavformat format. The default codecs are used.
- *
* Copyright (c) 2003 Fabrice Bellard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
+
+/**
+ * @file
+ * libavformat API example.
+ *
+ * @example libavformat/output-example.c
+ * Output a media file in any supported libavformat format.
+ * The default codecs are used.
+ */
+
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
-#ifndef M_PI
-#define M_PI 3.14159265358979323846
-#endif
-
+#include "libavutil/mathematics.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
/**************************************************************/
/* audio output */
-float t, tincr, tincr2;
-int16_t *samples;
-uint8_t *audio_outbuf;
-int audio_outbuf_size;
-int audio_input_frame_size;
+static float t, tincr, tincr2;
+static int16_t *samples;
+static uint8_t *audio_outbuf;
+static int audio_outbuf_size;
+static int audio_input_frame_size;
/*
* add an audio output stream
c = st->codec;
c->codec_id = codec_id;
- c->codec_type = CODEC_TYPE_AUDIO;
+ c->codec_type = AVMEDIA_TYPE_AUDIO;
/* put sample parameters */
+ c->sample_fmt = AV_SAMPLE_FMT_S16;
c->bit_rate = 64000;
c->sample_rate = 44100;
c->channels = 2;
if (c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE)
pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base);
- pkt.flags |= PKT_FLAG_KEY;
+ pkt.flags |= AV_PKT_FLAG_KEY;
pkt.stream_index= st->index;
pkt.data= audio_outbuf;
/**************************************************************/
/* video output */
-AVFrame *picture, *tmp_picture;
-uint8_t *video_outbuf;
-int frame_count, video_outbuf_size;
+static AVFrame *picture, *tmp_picture;
+static uint8_t *video_outbuf;
+static int frame_count, video_outbuf_size;
/* add a video output stream */
static AVStream *add_video_stream(AVFormatContext *oc, enum CodecID codec_id)
AVCodecContext *c;
AVStream *st;
- st = av_new_stream(oc, 0);
+ st = avformat_new_stream(oc, NULL);
if (!st) {
fprintf(stderr, "Could not alloc stream\n");
exit(1);
c = st->codec;
c->codec_id = codec_id;
- c->codec_type = CODEC_TYPE_VIDEO;
+ c->codec_type = AVMEDIA_TYPE_VIDEO;
/* put sample parameters */
c->bit_rate = 400000;
AVPacket pkt;
av_init_packet(&pkt);
- pkt.flags |= PKT_FLAG_KEY;
+ pkt.flags |= AV_PKT_FLAG_KEY;
pkt.stream_index= st->index;
pkt.data= (uint8_t *)picture;
pkt.size= sizeof(AVPicture);
if (c->coded_frame->pts != AV_NOPTS_VALUE)
pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base);
if(c->coded_frame->key_frame)
- pkt.flags |= PKT_FLAG_KEY;
+ pkt.flags |= AV_PKT_FLAG_KEY;
pkt.stream_index= st->index;
pkt.data= video_outbuf;
pkt.size= out_size;
"The output format is automatically guessed according to the file extension.\n"
"Raw images can also be output by using '%%d' in the filename\n"
"\n", argv[0]);
- exit(1);
+ return 1;
}
filename = argv[1];
}
if (!fmt) {
fprintf(stderr, "Could not find suitable output format\n");
- exit(1);
+ return 1;
}
/* allocate the output media context */
oc = avformat_alloc_context();
if (!oc) {
fprintf(stderr, "Memory error\n");
- exit(1);
+ return 1;
}
oc->oformat = fmt;
snprintf(oc->filename, sizeof(oc->filename), "%s", filename);
parameters). */
if (av_set_parameters(oc, NULL) < 0) {
fprintf(stderr, "Invalid output format parameters\n");
- exit(1);
+ return 1;
}
- dump_format(oc, 0, filename, 1);
+ av_dump_format(oc, 0, filename, 1);
/* now that all the parameters are set, we can open the audio and
video codecs and allocate the necessary encode buffers */
/* open the output file, if needed */
if (!(fmt->flags & AVFMT_NOFILE)) {
- if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
+ if (avio_open(&oc->pb, filename, AVIO_FLAG_WRITE) < 0) {
fprintf(stderr, "Could not open '%s'\n", filename);
- exit(1);
+ return 1;
}
}
if (!(fmt->flags & AVFMT_NOFILE)) {
/* close the output file */
- url_fclose(oc->pb);
+ avio_close(oc->pb);
}
/* free the stream */