Generate synthetic video data and encode it to an output file.
/*
* Copyright (c) 2001 Fabrice Bellard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* @file libavcodec encoding video API usage example
* @example encode_video.c
*
* Generate synthetic video data and encode it to an output file.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
{
/* send the frame to the encoder */
fprintf(stderr, "Error sending a frame for encoding\n");
exit(1);
}
return;
fprintf(stderr, "Error during encoding\n");
exit(1);
}
}
}
int main(
int argc,
char **argv)
{
const char *filename, *codec_name;
uint8_t endcode[] = { 0, 0, 1, 0xb7 };
if (argc <= 2) {
fprintf(stderr, "Usage: %s <output file> <codec name>\n", argv[0]);
exit(0);
}
filename = argv[1];
codec_name = argv[2];
/* find the mpeg1video encoder */
if (!codec) {
fprintf(stderr, "Codec '%s' not found\n", codec_name);
exit(1);
}
fprintf(stderr, "Could not allocate video codec context\n");
exit(1);
}
exit(1);
/* put sample parameters */
/* resolution must be a multiple of two */
/* frames per second */
/* emit one intra frame every ten frames
* check frame pict_type before passing frame
* to encoder, if frame->pict_type is AV_PICTURE_TYPE_I
* then gop_size is ignored and the output of encoder
* will always be I frame irrespective to gop_size
*/
/* open it */
exit(1);
}
f = fopen(filename,
"wb");
fprintf(stderr, "Could not open %s\n", filename);
exit(1);
}
fprintf(stderr, "Could not allocate video frame\n");
exit(1);
}
fprintf(stderr, "Could not allocate the video frame data\n");
exit(1);
}
/* encode 1 second of video */
for (
i = 0;
i < 25;
i++) {
fflush(stdout);
/* Make sure the frame data is writable.
On the first round, the frame is fresh from av_frame_get_buffer()
and therefore we know it is writable.
But on the next rounds, encode() will have called
avcodec_send_frame(), and the codec may have kept a reference to
the frame in its internal structures, that makes the frame
unwritable.
av_frame_make_writable() checks that and allocates a new buffer
for the frame only if necessary.
*/
exit(1);
/* Prepare a dummy image.
In real code, this is where you would have your own logic for
filling the frame. FFmpeg does not care what you put in the
frame.
*/
/* Y */
for (y = 0; y <
c->height; y++) {
for (x = 0; x <
c->width; x++) {
frame->data[0][y *
frame->linesize[0] + x] = x + y +
i * 3;
}
}
/* Cb and Cr */
for (y = 0; y <
c->height/2; y++) {
for (x = 0; x <
c->width/2; x++) {
frame->data[1][y *
frame->linesize[1] + x] = 128 + y +
i * 2;
frame->data[2][y *
frame->linesize[2] + x] = 64 + x +
i * 5;
}
}
/* encode the image */
}
/* flush the encoder */
/* Add sequence end code to have a real MPEG file.
It makes only sense because this tiny examples writes packets
directly. This is called "elementary stream" and only works for some
codecs. To create a valid file, you usually need to write packets
into a proper file format or protocol; see mux.c.
*/
fwrite(endcode, 1,
sizeof(endcode),
f);
return 0;
}