#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
-#undef exit
-
/* 5 seconds stream duration */
#define STREAM_DURATION 5.0
#define STREAM_FRAME_RATE 25 /* 25 images/s */
#define STREAM_NB_FRAMES ((int)(STREAM_DURATION * STREAM_FRAME_RATE))
-#define STREAM_PIX_FMT PIX_FMT_YUV420P /* default pix_fmt */
+#define STREAM_PIX_FMT AV_PIX_FMT_YUV420P /* default pix_fmt */
static int sws_flags = SWS_BICUBIC;
/*
* add an audio output stream
*/
-static AVStream *add_audio_stream(AVFormatContext *oc, enum CodecID codec_id)
+static AVStream *add_audio_stream(AVFormatContext *oc, enum AVCodecID codec_id)
{
AVCodecContext *c;
AVStream *st;
fprintf(stderr, "Error while writing audio frame\n");
exit(1);
}
+ avcodec_free_frame(&frame);
}
static void close_audio(AVFormatContext *oc, AVStream *st)
/* video output */
static AVFrame *picture, *tmp_picture;
-static uint8_t *video_outbuf;
-static int frame_count, video_outbuf_size;
+static int frame_count;
/* Add a video output stream. */
-static AVStream *add_video_stream(AVFormatContext *oc, enum CodecID codec_id)
+static AVStream *add_video_stream(AVFormatContext *oc, enum AVCodecID codec_id)
{
AVCodecContext *c;
AVStream *st;
c->time_base.num = 1;
c->gop_size = 12; /* emit one intra frame every twelve frames at most */
c->pix_fmt = STREAM_PIX_FMT;
- if (c->codec_id == CODEC_ID_MPEG2VIDEO) {
+ if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
/* just for testing, we also add B frames */
c->max_b_frames = 2;
}
- if (c->codec_id == CODEC_ID_MPEG1VIDEO) {
+ if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
/* Needed to avoid using macroblocks in which some coeffs overflow.
* This does not happen with normal video, it just happens here as
* the motion of the chroma plane does not match the luma plane. */
return st;
}
-static AVFrame *alloc_picture(enum PixelFormat pix_fmt, int width, int height)
+static AVFrame *alloc_picture(enum AVPixelFormat pix_fmt, int width, int height)
{
AVFrame *picture;
uint8_t *picture_buf;
exit(1);
}
- video_outbuf = NULL;
- if (!(oc->oformat->flags & AVFMT_RAWPICTURE)) {
- /* Allocate output buffer. */
- /* XXX: API change will be done. */
- /* Buffers passed into lav* can be allocated any way you prefer,
- * as long as they're aligned enough for the architecture, and
- * they're freed appropriately (such as using av_free for buffers
- * allocated with av_malloc). */
- video_outbuf_size = 200000;
- video_outbuf = av_malloc(video_outbuf_size);
- }
-
/* Allocate the encoded raw picture. */
picture = alloc_picture(c->pix_fmt, c->width, c->height);
if (!picture) {
* picture is needed too. It is then converted to the required
* output format. */
tmp_picture = NULL;
- if (c->pix_fmt != PIX_FMT_YUV420P) {
- tmp_picture = alloc_picture(PIX_FMT_YUV420P, c->width, c->height);
+ if (c->pix_fmt != AV_PIX_FMT_YUV420P) {
+ tmp_picture = alloc_picture(AV_PIX_FMT_YUV420P, c->width, c->height);
if (!tmp_picture) {
fprintf(stderr, "Could not allocate temporary picture\n");
exit(1);
static void write_video_frame(AVFormatContext *oc, AVStream *st)
{
- int out_size, ret;
+ int ret;
AVCodecContext *c;
static struct SwsContext *img_convert_ctx;
* frames if using B-frames, so we get the last frames by
* passing the same picture again. */
} else {
- if (c->pix_fmt != PIX_FMT_YUV420P) {
+ if (c->pix_fmt != AV_PIX_FMT_YUV420P) {
/* as we only generate a YUV420P picture, we must convert it
* to the codec pixel format if needed */
if (img_convert_ctx == NULL) {
img_convert_ctx = sws_getContext(c->width, c->height,
- PIX_FMT_YUV420P,
+ AV_PIX_FMT_YUV420P,
c->width, c->height,
c->pix_fmt,
sws_flags, NULL, NULL, NULL);
ret = av_interleaved_write_frame(oc, &pkt);
} else {
+ AVPacket pkt = { 0 };
+ int got_packet;
+ av_init_packet(&pkt);
+
/* encode the image */
- out_size = avcodec_encode_video(c, video_outbuf,
- video_outbuf_size, picture);
+ ret = avcodec_encode_video2(c, &pkt, picture, &got_packet);
/* If size is zero, it means the image was buffered. */
- if (out_size > 0) {
- AVPacket pkt;
- av_init_packet(&pkt);
-
- if (c->coded_frame->pts != AV_NOPTS_VALUE)
- pkt.pts = av_rescale_q(c->coded_frame->pts,
+ if (!ret && got_packet && pkt.size) {
+ if (pkt.pts != AV_NOPTS_VALUE) {
+ pkt.pts = av_rescale_q(pkt.pts,
+ c->time_base, st->time_base);
+ }
+ if (pkt.dts != AV_NOPTS_VALUE) {
+ pkt.dts = av_rescale_q(pkt.dts,
c->time_base, st->time_base);
- if (c->coded_frame->key_frame)
- pkt.flags |= AV_PKT_FLAG_KEY;
+ }
pkt.stream_index = st->index;
- pkt.data = video_outbuf;
- pkt.size = out_size;
/* Write the compressed frame to the media file. */
ret = av_interleaved_write_frame(oc, &pkt);
av_free(tmp_picture->data[0]);
av_free(tmp_picture);
}
- av_free(video_outbuf);
}
/**************************************************************/
* and initialize the codecs. */
video_st = NULL;
audio_st = NULL;
- if (fmt->video_codec != CODEC_ID_NONE) {
+ if (fmt->video_codec != AV_CODEC_ID_NONE) {
video_st = add_video_stream(oc, fmt->video_codec);
}
- if (fmt->audio_codec != CODEC_ID_NONE) {
+ if (fmt->audio_codec != AV_CODEC_ID_NONE) {
audio_st = add_audio_stream(oc, fmt->audio_codec);
}