46 printf(
"Send frame %3"PRId64
"\n",
frame->pts);
50 fprintf(stderr,
"Error sending a frame for encoding\n");
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);
69int main(
int argc,
char **argv)
71 const char *filename, *codec_name;
78 uint8_t endcode[] = { 0, 0, 1, 0xb7 };
81 fprintf(stderr,
"Usage: %s <output file> <codec name>\n", argv[0]);
90 fprintf(stderr,
"Codec '%s' not found\n", codec_name);
96 fprintf(stderr,
"Could not allocate video codec context\n");
129 fprintf(stderr,
"Could not open codec: %s\n",
av_err2str(ret));
133 f = fopen(filename,
"wb");
135 fprintf(stderr,
"Could not open %s\n", filename);
141 fprintf(stderr,
"Could not allocate video frame\n");
150 fprintf(stderr,
"Could not allocate the video frame data\n");
155 for (i = 0; i < 25; i++) {
178 for (y = 0; y < c->
height; y++) {
179 for (x = 0; x < c->
width; x++) {
180 frame->data[0][y *
frame->linesize[0] + x] = x + y + i * 3;
185 for (y = 0; y < c->
height/2; y++) {
186 for (x = 0; x < c->
width/2; x++) {
187 frame->data[1][y *
frame->linesize[1] + x] = 128 + y + i * 2;
188 frame->data[2][y *
frame->linesize[2] + x] = 64 + x + i * 5;
208 fwrite(endcode, 1,
sizeof(endcode), f);
Libavcodec external API header.
int main(int argc, char *argv[])
static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt, FILE *output)
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
const AVCodec * avcodec_find_encoder_by_name(const char *name)
Find a registered encoder with the specified name.
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer.
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Read encoded data from the encoder.
int avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
Supply a raw video or audio frame to the encoder.
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
#define AVERROR_EOF
End of file.
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
main external API structure.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
int width
picture width / height.
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...
int64_t bit_rate
the average bitrate
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
Rational number (pair of numerator and denominator).