2016-10-19 19:05:22 +00:00
|
|
|
/*
|
2017-03-29 12:07:39 +00:00
|
|
|
* Copyright (c) 2001 Fabrice Bellard
|
2016-10-19 19:05:22 +00:00
|
|
|
*
|
2017-03-29 12:07:39 +00:00
|
|
|
* 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:
|
2016-10-19 19:05:22 +00:00
|
|
|
*
|
2017-03-29 12:07:39 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2016-10-19 19:05:22 +00:00
|
|
|
*
|
2017-03-29 12:07:39 +00:00
|
|
|
* 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.
|
2016-10-19 19:05:22 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* video encoding with libavcodec API example
|
|
|
|
*
|
|
|
|
* @example encode_video.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2017-03-29 12:07:39 +00:00
|
|
|
#include <libavcodec/avcodec.h>
|
2016-10-19 19:05:22 +00:00
|
|
|
|
2017-03-29 12:07:39 +00:00
|
|
|
#include <libavutil/opt.h>
|
|
|
|
#include <libavutil/imgutils.h>
|
2016-10-19 19:05:22 +00:00
|
|
|
|
2016-10-20 09:03:20 +00:00
|
|
|
static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt,
|
|
|
|
FILE *outfile)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* send the frame to the encoder */
|
2017-05-23 09:50:40 +00:00
|
|
|
if (frame)
|
|
|
|
printf("Send frame %3"PRId64"\n", frame->pts);
|
|
|
|
|
2016-10-20 09:03:20 +00:00
|
|
|
ret = avcodec_send_frame(enc_ctx, frame);
|
|
|
|
if (ret < 0) {
|
2017-04-04 09:44:45 +00:00
|
|
|
fprintf(stderr, "Error sending a frame for encoding\n");
|
2016-10-20 09:03:20 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (ret >= 0) {
|
|
|
|
ret = avcodec_receive_packet(enc_ctx, pkt);
|
|
|
|
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
|
|
|
|
return;
|
|
|
|
else if (ret < 0) {
|
2017-04-04 09:44:45 +00:00
|
|
|
fprintf(stderr, "Error during encoding\n");
|
2016-10-20 09:03:20 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2017-05-23 09:50:40 +00:00
|
|
|
printf("Write packet %3"PRId64" (size=%5d)\n", pkt->pts, pkt->size);
|
2016-10-20 09:03:20 +00:00
|
|
|
fwrite(pkt->data, 1, pkt->size, outfile);
|
|
|
|
av_packet_unref(pkt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-19 19:05:22 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2017-03-29 12:07:39 +00:00
|
|
|
const char *filename, *codec_name;
|
2016-10-20 09:03:20 +00:00
|
|
|
const AVCodec *codec;
|
2016-10-19 19:05:22 +00:00
|
|
|
AVCodecContext *c= NULL;
|
2016-10-20 09:03:20 +00:00
|
|
|
int i, ret, x, y;
|
2016-10-19 19:05:22 +00:00
|
|
|
FILE *f;
|
2017-03-29 12:07:39 +00:00
|
|
|
AVFrame *frame;
|
2016-10-20 09:03:20 +00:00
|
|
|
AVPacket *pkt;
|
2016-10-19 19:05:22 +00:00
|
|
|
uint8_t endcode[] = { 0, 0, 1, 0xb7 };
|
|
|
|
|
2017-03-29 12:07:39 +00:00
|
|
|
if (argc <= 2) {
|
|
|
|
fprintf(stderr, "Usage: %s <output file> <codec name>\n", argv[0]);
|
2016-10-19 19:05:22 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
filename = argv[1];
|
2017-03-29 12:07:39 +00:00
|
|
|
codec_name = argv[2];
|
2016-10-19 19:05:22 +00:00
|
|
|
|
|
|
|
/* find the mpeg1video encoder */
|
2017-03-29 12:07:39 +00:00
|
|
|
codec = avcodec_find_encoder_by_name(codec_name);
|
2016-10-19 19:05:22 +00:00
|
|
|
if (!codec) {
|
2017-05-23 10:22:09 +00:00
|
|
|
fprintf(stderr, "Codec '%s' not found\n", codec_name);
|
2016-10-19 19:05:22 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
c = avcodec_alloc_context3(codec);
|
2017-03-29 12:07:39 +00:00
|
|
|
if (!c) {
|
|
|
|
fprintf(stderr, "Could not allocate video codec context\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2016-10-19 19:05:22 +00:00
|
|
|
|
2016-10-20 09:03:20 +00:00
|
|
|
pkt = av_packet_alloc();
|
|
|
|
if (!pkt)
|
|
|
|
exit(1);
|
|
|
|
|
2016-10-19 19:05:22 +00:00
|
|
|
/* put sample parameters */
|
|
|
|
c->bit_rate = 400000;
|
|
|
|
/* resolution must be a multiple of two */
|
|
|
|
c->width = 352;
|
|
|
|
c->height = 288;
|
|
|
|
/* frames per second */
|
2016-10-20 09:03:20 +00:00
|
|
|
c->time_base = (AVRational){1, 25};
|
|
|
|
c->framerate = (AVRational){25, 1};
|
|
|
|
|
2017-03-29 12:07:39 +00:00
|
|
|
/* 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
|
|
|
|
*/
|
|
|
|
c->gop_size = 10;
|
|
|
|
c->max_b_frames = 1;
|
2016-10-19 19:05:22 +00:00
|
|
|
c->pix_fmt = AV_PIX_FMT_YUV420P;
|
|
|
|
|
2017-03-29 12:07:39 +00:00
|
|
|
if (codec->id == AV_CODEC_ID_H264)
|
|
|
|
av_opt_set(c->priv_data, "preset", "slow", 0);
|
|
|
|
|
2016-10-19 19:05:22 +00:00
|
|
|
/* open it */
|
2017-05-23 10:22:09 +00:00
|
|
|
ret = avcodec_open2(c, codec, NULL);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "Could not open codec: %s\n", av_err2str(ret));
|
2016-10-19 19:05:22 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
f = fopen(filename, "wb");
|
|
|
|
if (!f) {
|
2017-03-29 12:07:39 +00:00
|
|
|
fprintf(stderr, "Could not open %s\n", filename);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
frame = av_frame_alloc();
|
|
|
|
if (!frame) {
|
|
|
|
fprintf(stderr, "Could not allocate video frame\n");
|
2016-10-19 19:05:22 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2017-03-29 12:07:39 +00:00
|
|
|
frame->format = c->pix_fmt;
|
|
|
|
frame->width = c->width;
|
|
|
|
frame->height = c->height;
|
2016-10-19 19:05:22 +00:00
|
|
|
|
2017-02-08 08:51:17 +00:00
|
|
|
ret = av_frame_get_buffer(frame, 0);
|
2016-10-19 19:05:22 +00:00
|
|
|
if (ret < 0) {
|
2017-03-29 12:20:25 +00:00
|
|
|
fprintf(stderr, "Could not allocate the video frame data\n");
|
2016-10-19 19:05:22 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* encode 1 second of video */
|
2017-03-29 12:07:39 +00:00
|
|
|
for (i = 0; i < 25; i++) {
|
2016-10-19 19:05:22 +00:00
|
|
|
fflush(stdout);
|
2016-10-20 09:03:20 +00:00
|
|
|
|
2021-08-16 13:05:59 +00:00
|
|
|
/* 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.
|
|
|
|
*/
|
2017-03-29 12:20:25 +00:00
|
|
|
ret = av_frame_make_writable(frame);
|
2016-10-20 09:03:20 +00:00
|
|
|
if (ret < 0)
|
|
|
|
exit(1);
|
|
|
|
|
2021-08-16 13:05:59 +00:00
|
|
|
/* 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.
|
|
|
|
*/
|
2016-10-19 19:05:22 +00:00
|
|
|
/* Y */
|
2017-03-29 12:07:39 +00:00
|
|
|
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;
|
2016-10-19 19:05:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cb and Cr */
|
2017-03-29 12:07:39 +00:00
|
|
|
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;
|
2016-10-19 19:05:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-29 12:07:39 +00:00
|
|
|
frame->pts = i;
|
2016-10-19 19:05:22 +00:00
|
|
|
|
|
|
|
/* encode the image */
|
2017-04-04 09:48:23 +00:00
|
|
|
encode(c, frame, pkt, f);
|
2016-10-19 19:05:22 +00:00
|
|
|
}
|
|
|
|
|
2016-10-20 09:03:20 +00:00
|
|
|
/* flush the encoder */
|
2016-10-20 09:03:20 +00:00
|
|
|
encode(c, NULL, pkt, f);
|
2016-10-19 19:05:22 +00:00
|
|
|
|
2021-08-16 13:05:59 +00:00
|
|
|
/* 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 muxing.c.
|
|
|
|
*/
|
2019-08-05 04:53:55 +00:00
|
|
|
if (codec->id == AV_CODEC_ID_MPEG1VIDEO || codec->id == AV_CODEC_ID_MPEG2VIDEO)
|
|
|
|
fwrite(endcode, 1, sizeof(endcode), f);
|
2016-10-19 19:05:22 +00:00
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
avcodec_free_context(&c);
|
2017-03-29 12:07:39 +00:00
|
|
|
av_frame_free(&frame);
|
2016-10-20 09:03:20 +00:00
|
|
|
av_packet_free(&pkt);
|
2016-10-19 19:05:22 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|