encode_video.c 5.44 KB
Newer Older
1
/*
2
 * Copyright (c) 2001 Fabrice Bellard
3
 *
4 5 6 7 8 9
 * 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:
10
 *
11 12
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
13
 *
14 15 16 17 18 19 20
 * 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.
21 22 23 24 25 26 27 28 29 30 31 32 33
 */

/**
 * @file
 * video encoding with libavcodec API example
 *
 * @example encode_video.c
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

34
#include <libavcodec/avcodec.h>
35

36 37
#include <libavutil/opt.h>
#include <libavutil/imgutils.h>
38

39 40 41 42 43 44
static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt,
                   FILE *outfile)
{
    int ret;

    /* send the frame to the encoder */
45 46 47
    if (frame)
        printf("Send frame %3"PRId64"\n", frame->pts);

48 49
    ret = avcodec_send_frame(enc_ctx, frame);
    if (ret < 0) {
50
        fprintf(stderr, "Error sending a frame for encoding\n");
51 52 53 54 55 56 57 58
        exit(1);
    }

    while (ret >= 0) {
        ret = avcodec_receive_packet(enc_ctx, pkt);
        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
            return;
        else if (ret < 0) {
59
            fprintf(stderr, "Error during encoding\n");
60 61 62
            exit(1);
        }

63
        printf("Write packet %3"PRId64" (size=%5d)\n", pkt->pts, pkt->size);
64 65 66 67 68
        fwrite(pkt->data, 1, pkt->size, outfile);
        av_packet_unref(pkt);
    }
}

69 70
int main(int argc, char **argv)
{
71
    const char *filename, *codec_name;
72
    const AVCodec *codec;
73
    AVCodecContext *c= NULL;
74
    int i, ret, x, y;
75
    FILE *f;
76
    AVFrame *frame;
77
    AVPacket *pkt;
78 79
    uint8_t endcode[] = { 0, 0, 1, 0xb7 };

80 81
    if (argc <= 2) {
        fprintf(stderr, "Usage: %s <output file> <codec name>\n", argv[0]);
82 83 84
        exit(0);
    }
    filename = argv[1];
85
    codec_name = argv[2];
86 87 88 89

    avcodec_register_all();

    /* find the mpeg1video encoder */
90
    codec = avcodec_find_encoder_by_name(codec_name);
91
    if (!codec) {
92
        fprintf(stderr, "Codec '%s' not found\n", codec_name);
93 94 95 96
        exit(1);
    }

    c = avcodec_alloc_context3(codec);
97 98 99 100
    if (!c) {
        fprintf(stderr, "Could not allocate video codec context\n");
        exit(1);
    }
101

102 103 104 105
    pkt = av_packet_alloc();
    if (!pkt)
        exit(1);

106 107 108 109 110 111
    /* put sample parameters */
    c->bit_rate = 400000;
    /* resolution must be a multiple of two */
    c->width = 352;
    c->height = 288;
    /* frames per second */
112 113 114
    c->time_base = (AVRational){1, 25};
    c->framerate = (AVRational){25, 1};

115 116 117 118 119 120 121 122
    /* 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;
123 124
    c->pix_fmt = AV_PIX_FMT_YUV420P;

125 126 127
    if (codec->id == AV_CODEC_ID_H264)
        av_opt_set(c->priv_data, "preset", "slow", 0);

128
    /* open it */
129 130 131
    ret = avcodec_open2(c, codec, NULL);
    if (ret < 0) {
        fprintf(stderr, "Could not open codec: %s\n", av_err2str(ret));
132 133 134 135 136
        exit(1);
    }

    f = fopen(filename, "wb");
    if (!f) {
137 138 139 140 141 142 143
        fprintf(stderr, "Could not open %s\n", filename);
        exit(1);
    }

    frame = av_frame_alloc();
    if (!frame) {
        fprintf(stderr, "Could not allocate video frame\n");
144 145
        exit(1);
    }
146 147 148
    frame->format = c->pix_fmt;
    frame->width  = c->width;
    frame->height = c->height;
149

150
    ret = av_frame_get_buffer(frame, 32);
151
    if (ret < 0) {
152
        fprintf(stderr, "Could not allocate the video frame data\n");
153 154 155 156
        exit(1);
    }

    /* encode 1 second of video */
157
    for (i = 0; i < 25; i++) {
158
        fflush(stdout);
159 160

        /* make sure the frame data is writable */
161
        ret = av_frame_make_writable(frame);
162 163 164
        if (ret < 0)
            exit(1);

165 166
        /* prepare a dummy image */
        /* Y */
167 168 169
        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;
170 171 172 173
            }
        }

        /* Cb and Cr */
174 175 176 177
        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;
178 179 180
            }
        }

181
        frame->pts = i;
182 183

        /* encode the image */
184
        encode(c, frame, pkt, f);
185 186
    }

187
    /* flush the encoder */
188
    encode(c, NULL, pkt, f);
189 190 191 192 193 194

    /* add sequence end code to have a real MPEG file */
    fwrite(endcode, 1, sizeof(endcode), f);
    fclose(f);

    avcodec_free_context(&c);
195
    av_frame_free(&frame);
196
    av_packet_free(&pkt);
197 198 199

    return 0;
}