2 * Copyright (c) 2001 Fabrice Bellard
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * video encoding with libavcodec API example
27 * @example encode_video.c
34 #include <libavcodec/avcodec.h>
36 #include <libavutil/opt.h>
37 #include <libavutil/imgutils.h>
39 static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt,
44 /* send the frame to the encoder */
46 printf("Send frame %3"PRId64"\n", frame->pts);
48 ret = avcodec_send_frame(enc_ctx, frame);
50 fprintf(stderr, "Error sending a frame for encoding\n");
55 ret = avcodec_receive_packet(enc_ctx, pkt);
56 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
59 fprintf(stderr, "Error during encoding\n");
63 printf("Write packet %3"PRId64" (size=%5d)\n", pkt->pts, pkt->size);
64 fwrite(pkt->data, 1, pkt->size, outfile);
69 int main(int argc, char **argv)
71 const char *filename, *codec_name;
73 AVCodecContext *c= NULL;
78 uint8_t endcode[] = { 0, 0, 1, 0xb7 };
81 fprintf(stderr, "Usage: %s <output file> <codec name>\n", argv[0]);
87 avcodec_register_all();
89 /* find the mpeg1video encoder */
90 codec = avcodec_find_encoder_by_name(codec_name);
92 fprintf(stderr, "Codec '%s' not found\n", codec_name);
96 c = avcodec_alloc_context3(codec);
98 fprintf(stderr, "Could not allocate video codec context\n");
102 pkt = av_packet_alloc();
106 /* put sample parameters */
107 c->bit_rate = 400000;
108 /* resolution must be a multiple of two */
111 /* frames per second */
112 c->time_base = (AVRational){1, 25};
113 c->framerate = (AVRational){25, 1};
115 /* emit one intra frame every ten frames
116 * check frame pict_type before passing frame
117 * to encoder, if frame->pict_type is AV_PICTURE_TYPE_I
118 * then gop_size is ignored and the output of encoder
119 * will always be I frame irrespective to gop_size
123 c->pix_fmt = AV_PIX_FMT_YUV420P;
125 if (codec->id == AV_CODEC_ID_H264)
126 av_opt_set(c->priv_data, "preset", "slow", 0);
129 ret = avcodec_open2(c, codec, NULL);
131 fprintf(stderr, "Could not open codec: %s\n", av_err2str(ret));
135 f = fopen(filename, "wb");
137 fprintf(stderr, "Could not open %s\n", filename);
141 frame = av_frame_alloc();
143 fprintf(stderr, "Could not allocate video frame\n");
146 frame->format = c->pix_fmt;
147 frame->width = c->width;
148 frame->height = c->height;
150 ret = av_frame_get_buffer(frame, 32);
152 fprintf(stderr, "Could not allocate the video frame data\n");
156 /* encode 1 second of video */
157 for (i = 0; i < 25; i++) {
160 /* make sure the frame data is writable */
161 ret = av_frame_make_writable(frame);
165 /* prepare a dummy image */
167 for (y = 0; y < c->height; y++) {
168 for (x = 0; x < c->width; x++) {
169 frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3;
174 for (y = 0; y < c->height/2; y++) {
175 for (x = 0; x < c->width/2; x++) {
176 frame->data[1][y * frame->linesize[1] + x] = 128 + y + i * 2;
177 frame->data[2][y * frame->linesize[2] + x] = 64 + x + i * 5;
183 /* encode the image */
184 encode(c, frame, pkt, f);
187 /* flush the encoder */
188 encode(c, NULL, pkt, f);
190 /* add sequence end code to have a real MPEG file */
191 fwrite(endcode, 1, sizeof(endcode), f);
194 avcodec_free_context(&c);
195 av_frame_free(&frame);
196 av_packet_free(&pkt);