]> git.sesse.net Git - ffmpeg/blob - doc/examples/encode_video.c
Merge commit 'a0c443a3980dc22eb02b067ac4cb9ffa2f9b04d2'
[ffmpeg] / doc / examples / encode_video.c
1 /*
2  * Copyright (c) 2001 Fabrice Bellard
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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
20  * THE SOFTWARE.
21  */
22
23 /**
24  * @file
25  * video encoding with libavcodec API example
26  *
27  * @example encode_video.c
28  */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include <libavcodec/avcodec.h>
35
36 #include <libavutil/opt.h>
37 #include <libavutil/imgutils.h>
38
39 static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt,
40                    FILE *outfile)
41 {
42     int ret;
43
44     /* send the frame to the encoder */
45     ret = avcodec_send_frame(enc_ctx, frame);
46     if (ret < 0) {
47         fprintf(stderr, "Error sending a frame for encoding\n");
48         exit(1);
49     }
50
51     while (ret >= 0) {
52         ret = avcodec_receive_packet(enc_ctx, pkt);
53         if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
54             return;
55         else if (ret < 0) {
56             fprintf(stderr, "Error during encoding\n");
57             exit(1);
58         }
59
60         printf("Write frame %3"PRId64" (size=%5d)\n", pkt->pts, pkt->size);
61         fwrite(pkt->data, 1, pkt->size, outfile);
62         av_packet_unref(pkt);
63     }
64 }
65
66 int main(int argc, char **argv)
67 {
68     const char *filename, *codec_name;
69     const AVCodec *codec;
70     AVCodecContext *c= NULL;
71     int i, ret, x, y;
72     FILE *f;
73     AVFrame *frame;
74     AVPacket *pkt;
75     uint8_t endcode[] = { 0, 0, 1, 0xb7 };
76
77     if (argc <= 2) {
78         fprintf(stderr, "Usage: %s <output file> <codec name>\n", argv[0]);
79         exit(0);
80     }
81     filename = argv[1];
82     codec_name = argv[2];
83
84     avcodec_register_all();
85
86     /* find the mpeg1video encoder */
87     codec = avcodec_find_encoder_by_name(codec_name);
88     if (!codec) {
89         fprintf(stderr, "Codec not found\n");
90         exit(1);
91     }
92
93     c = avcodec_alloc_context3(codec);
94     if (!c) {
95         fprintf(stderr, "Could not allocate video codec context\n");
96         exit(1);
97     }
98
99     pkt = av_packet_alloc();
100     if (!pkt)
101         exit(1);
102
103     /* put sample parameters */
104     c->bit_rate = 400000;
105     /* resolution must be a multiple of two */
106     c->width = 352;
107     c->height = 288;
108     /* frames per second */
109     c->time_base = (AVRational){1, 25};
110     c->framerate = (AVRational){25, 1};
111
112     /* emit one intra frame every ten frames
113      * check frame pict_type before passing frame
114      * to encoder, if frame->pict_type is AV_PICTURE_TYPE_I
115      * then gop_size is ignored and the output of encoder
116      * will always be I frame irrespective to gop_size
117      */
118     c->gop_size = 10;
119     c->max_b_frames = 1;
120     c->pix_fmt = AV_PIX_FMT_YUV420P;
121
122     if (codec->id == AV_CODEC_ID_H264)
123         av_opt_set(c->priv_data, "preset", "slow", 0);
124
125     /* open it */
126     if (avcodec_open2(c, codec, NULL) < 0) {
127         fprintf(stderr, "Could not open codec\n");
128         exit(1);
129     }
130
131     f = fopen(filename, "wb");
132     if (!f) {
133         fprintf(stderr, "Could not open %s\n", filename);
134         exit(1);
135     }
136
137     frame = av_frame_alloc();
138     if (!frame) {
139         fprintf(stderr, "Could not allocate video frame\n");
140         exit(1);
141     }
142     frame->format = c->pix_fmt;
143     frame->width  = c->width;
144     frame->height = c->height;
145
146     ret = av_frame_get_buffer(frame, 32);
147     if (ret < 0) {
148         fprintf(stderr, "Could not allocate the video frame data\n");
149         exit(1);
150     }
151
152     /* encode 1 second of video */
153     for (i = 0; i < 25; i++) {
154         fflush(stdout);
155
156         /* make sure the frame data is writable */
157         ret = av_frame_make_writable(frame);
158         if (ret < 0)
159             exit(1);
160
161         /* prepare a dummy image */
162         /* Y */
163         for (y = 0; y < c->height; y++) {
164             for (x = 0; x < c->width; x++) {
165                 frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3;
166             }
167         }
168
169         /* Cb and Cr */
170         for (y = 0; y < c->height/2; y++) {
171             for (x = 0; x < c->width/2; x++) {
172                 frame->data[1][y * frame->linesize[1] + x] = 128 + y + i * 2;
173                 frame->data[2][y * frame->linesize[2] + x] = 64 + x + i * 5;
174             }
175         }
176
177         frame->pts = i;
178
179         /* encode the image */
180         encode(c, frame, pkt, f);
181     }
182
183     /* flush the encoder */
184     encode(c, NULL, pkt, f);
185
186     /* add sequence end code to have a real MPEG file */
187     fwrite(endcode, 1, sizeof(endcode), f);
188     fclose(f);
189
190     avcodec_free_context(&c);
191     av_frame_free(&frame);
192     av_packet_free(&pkt);
193
194     return 0;
195 }