FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
encode_video.c
Go to the documentation of this file.
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 int main(int argc, char **argv)
40 {
41  const char *filename, *codec_name;
42  const AVCodec *codec;
44  int i, ret, x, y, got_output;
45  FILE *f;
46  AVFrame *frame;
47  AVPacket pkt;
48  uint8_t endcode[] = { 0, 0, 1, 0xb7 };
49 
50  if (argc <= 2) {
51  fprintf(stderr, "Usage: %s <output file> <codec name>\n", argv[0]);
52  exit(0);
53  }
54  filename = argv[1];
55  codec_name = argv[2];
56 
58 
59  /* find the mpeg1video encoder */
60  codec = avcodec_find_encoder_by_name(codec_name);
61  if (!codec) {
62  fprintf(stderr, "Codec not found\n");
63  exit(1);
64  }
65 
66  c = avcodec_alloc_context3(codec);
67  if (!c) {
68  fprintf(stderr, "Could not allocate video codec context\n");
69  exit(1);
70  }
71 
72  /* put sample parameters */
73  c->bit_rate = 400000;
74  /* resolution must be a multiple of two */
75  c->width = 352;
76  c->height = 288;
77  /* frames per second */
78  c->time_base = (AVRational){1, 25};
79  c->framerate = (AVRational){25, 1};
80 
81  /* emit one intra frame every ten frames
82  * check frame pict_type before passing frame
83  * to encoder, if frame->pict_type is AV_PICTURE_TYPE_I
84  * then gop_size is ignored and the output of encoder
85  * will always be I frame irrespective to gop_size
86  */
87  c->gop_size = 10;
88  c->max_b_frames = 1;
90 
91  if (codec->id == AV_CODEC_ID_H264)
92  av_opt_set(c->priv_data, "preset", "slow", 0);
93 
94  /* open it */
95  if (avcodec_open2(c, codec, NULL) < 0) {
96  fprintf(stderr, "Could not open codec\n");
97  exit(1);
98  }
99 
100  f = fopen(filename, "wb");
101  if (!f) {
102  fprintf(stderr, "Could not open %s\n", filename);
103  exit(1);
104  }
105 
106  frame = av_frame_alloc();
107  if (!frame) {
108  fprintf(stderr, "Could not allocate video frame\n");
109  exit(1);
110  }
111  frame->format = c->pix_fmt;
112  frame->width = c->width;
113  frame->height = c->height;
114 
115  ret = av_frame_get_buffer(frame, 32);
116  if (ret < 0) {
117  fprintf(stderr, "Could not allocate the video frame data\n");
118  exit(1);
119  }
120 
121  /* encode 1 second of video */
122  for (i = 0; i < 25; i++) {
123  av_init_packet(&pkt);
124  pkt.data = NULL; // packet data will be allocated by the encoder
125  pkt.size = 0;
126 
127  fflush(stdout);
128 
129  /* make sure the frame data is writable */
130  ret = av_frame_make_writable(frame);
131  if (ret < 0)
132  exit(1);
133 
134  /* prepare a dummy image */
135  /* Y */
136  for (y = 0; y < c->height; y++) {
137  for (x = 0; x < c->width; x++) {
138  frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3;
139  }
140  }
141 
142  /* Cb and Cr */
143  for (y = 0; y < c->height/2; y++) {
144  for (x = 0; x < c->width/2; x++) {
145  frame->data[1][y * frame->linesize[1] + x] = 128 + y + i * 2;
146  frame->data[2][y * frame->linesize[2] + x] = 64 + x + i * 5;
147  }
148  }
149 
150  frame->pts = i;
151 
152  /* encode the image */
153  ret = avcodec_encode_video2(c, &pkt, frame, &got_output);
154  if (ret < 0) {
155  fprintf(stderr, "Error encoding frame\n");
156  exit(1);
157  }
158 
159  if (got_output) {
160  printf("Write frame %3d (size=%5d)\n", i, pkt.size);
161  fwrite(pkt.data, 1, pkt.size, f);
162  av_packet_unref(&pkt);
163  }
164  }
165 
166  /* get the delayed frames */
167  for (got_output = 1; got_output; i++) {
168  fflush(stdout);
169 
170  ret = avcodec_encode_video2(c, &pkt, NULL, &got_output);
171  if (ret < 0) {
172  fprintf(stderr, "Error encoding frame\n");
173  exit(1);
174  }
175 
176  if (got_output) {
177  printf("Write frame %3d (size=%5d)\n", i, pkt.size);
178  fwrite(pkt.data, 1, pkt.size, f);
179  av_packet_unref(&pkt);
180  }
181  }
182 
183  /* add sequence end code to have a real MPEG file */
184  fwrite(endcode, 1, sizeof(endcode), f);
185  fclose(f);
186 
188  av_frame_free(&frame);
189 
190  return 0;
191 }
#define NULL
Definition: coverity.c:32
AVRational framerate
Definition: avcodec.h:3429
This structure describes decoded (raw) audio or video data.
Definition: frame.h:187
misc image utilities
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1797
int max_b_frames
maximum number of B-frames between non-B-frames Note: The output will be delayed by max_b_frames+1 re...
Definition: avcodec.h:2018
int size
Definition: avcodec.h:1658
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1960
static AVPacket pkt
void avcodec_register_all(void)
Register all the codecs, parsers and bitstream filters which were enabled at configuration time...
Definition: allcodecs.c:720
AVCodec.
Definition: avcodec.h:3681
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1869
uint8_t
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:150
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:271
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1657
AVCodec * avcodec_find_encoder_by_name(const char *name)
Find a registered encoder with the specified name.
Definition: utils.c:3154
enum AVCodecID id
Definition: avcodec.h:3695
int width
width and height of the video frame
Definition: frame.h:239
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:163
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:157
int width
picture width / height.
Definition: avcodec.h:1919
int main(int argc, char **argv)
Definition: encode_video.c:39
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:251
Libavcodec external API header.
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer...
Definition: options.c:172
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:218
main external API structure.
Definition: avcodec.h:1732
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:589
Rational number (pair of numerator and denominator).
Definition: rational.h:58
attribute_deprecated int avcodec_encode_video2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Encode a frame of video.
Definition: utils.c:1968
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: utils.c:1242
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition: frame.c:280
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
Definition: frame.c:553
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:201
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1945
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
static double c[64]
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:33
void * priv_data
Definition: avcodec.h:1774
int height
Definition: frame.h:239
This structure stores compressed data.
Definition: avcodec.h:1634
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
Definition: opt.c:449