source: libpipi/trunk/examples/makemovie.c @ 3413

Last change on this file since 3413 was 3413, checked in by Sam Hocevar, 11 years ago

makemovie: new example program; this utility reads a list of images from
stdin and encodes them into a movie (name specified on the command line).

  • Property svn:keywords set to Id
File size: 4.9 KB
Line 
1/*
2 *  makemovie     read image names from stdin and create a movie
3 *  Copyright (c) 2009 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: makemovie.c 3413 2009-03-28 23:26:38Z sam $
7 *
8 *  This program is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15#include "config.h"
16
17#include <stdlib.h>
18#include <stdio.h>
19#include <string.h>
20
21#include <avformat.h>
22#include <swscale.h>
23
24#include <pipi.h>
25
26/* These values define a 720x576 anamorphic widescreen image that gets
27 * scaled to 1024x576 (16:9). */
28#define WIDTH 720
29#define HEIGHT 576
30#define PAR_NUM 64
31#define PAR_DEN 45
32#define FPS 25
33#define BITRATE 8000000
34
35int main(int argc, char *argv[])
36{
37    char file[1024];
38    AVPacket packet;
39    AVFormatContext *fmt;
40    AVCodecContext *ctx;
41    AVStream *stream;
42    AVCodec *codec;
43    AVFrame *frame;
44    struct SwsContext *sws = NULL;
45    pipi_image_t *image;
46    pipi_pixels_t *p;
47    uint8_t *buffer, *tmp;
48    int len;
49
50    if(argc < 2)
51        return EXIT_FAILURE;
52
53    av_register_all();
54    fmt = avformat_alloc_context();
55    if(!fmt)
56        return EXIT_FAILURE;
57    snprintf(fmt->filename, sizeof(fmt->filename), "%s", argv[1]);
58
59    fmt->oformat = guess_format(NULL, argv[1], NULL);
60    if(!fmt->oformat)
61        fmt->oformat = guess_format("mpeg", NULL, NULL);
62    if(!fmt->oformat)
63        return EXIT_FAILURE;
64
65    stream = av_new_stream(fmt, 0);
66    if(!stream)
67        return EXIT_FAILURE;
68    stream->sample_aspect_ratio.num = PAR_NUM;
69    stream->sample_aspect_ratio.den = PAR_DEN;
70
71    ctx = stream->codec;
72    ctx->width = WIDTH;
73    ctx->height = HEIGHT;
74    ctx->sample_aspect_ratio.num = PAR_NUM;
75    ctx->sample_aspect_ratio.den = PAR_DEN;
76    ctx->codec_id = fmt->oformat->video_codec;
77    ctx->codec_type = CODEC_TYPE_VIDEO;
78    ctx->bit_rate = BITRATE;
79    ctx->time_base.num = 1;
80    ctx->time_base.den = FPS; /* 25 fps */
81    ctx->gop_size = FPS / 2;
82    ctx->pix_fmt = PIX_FMT_YUV420P; /* send YUV 420 */
83    if(ctx->codec_id == CODEC_ID_MPEG2VIDEO)
84        ctx->max_b_frames = 2;
85    if(ctx->codec_id == CODEC_ID_MPEG1VIDEO)
86        ctx->mb_decision = 2;
87    if(fmt->oformat->flags & AVFMT_GLOBALHEADER)
88        ctx->flags |= CODEC_FLAG_GLOBAL_HEADER;
89
90    if(av_set_parameters(fmt, NULL) < 0)
91        return EXIT_FAILURE;
92
93    codec = avcodec_find_encoder(ctx->codec_id);
94    if(!codec)
95        return EXIT_FAILURE;
96    if(avcodec_open(ctx, codec) < 0)
97        return EXIT_FAILURE;
98
99    frame = avcodec_alloc_frame();
100    if(!frame)
101        return EXIT_FAILURE;
102    tmp = av_malloc(avpicture_get_size(ctx->pix_fmt, ctx->width, ctx->height));
103    if(!tmp)
104        return EXIT_FAILURE;
105    avpicture_fill((AVPicture *)frame, tmp, ctx->pix_fmt,
106                   ctx->width, ctx->height);
107
108    if(!(fmt->flags & AVFMT_NOFILE))
109        if(url_fopen(&fmt->pb, argv[1], URL_WRONLY) < 0)
110            return EXIT_FAILURE;
111
112    len = 64 * 1024 * 1024;
113    buffer = av_malloc(len);
114
115    av_write_header(fmt);
116
117    for(;;)
118    {
119        uint8_t *start;
120        int w = 0, h = 0, bytes, pitch;
121
122        if(!fgets(file, sizeof(file), stdin))
123            break;
124        file[strlen(file) - 1] = '\0';
125
126        image = pipi_load(file);
127        if(!image)
128            return EXIT_FAILURE;
129        p = pipi_get_pixels(image, PIPI_PIXELS_RGBA_U8);
130        start = (uint8_t *)p->pixels;
131        pitch = p->w * 4;
132
133        if(w != p->w || h != p->h)
134        {
135            w = p->w;
136            h = p->h;
137            if(sws)
138                sws_freeContext(sws);
139            sws = NULL;
140        }
141
142        if(!sws)
143            sws = sws_getContext(w, h, PIX_FMT_RGB32, ctx->width, ctx->height,
144                                 ctx->pix_fmt, SWS_BICUBIC, NULL, NULL, NULL);
145
146        sws_scale(sws, &start, &pitch, 0, h, frame->data, frame->linesize);
147
148        pipi_free(image);
149
150        bytes = avcodec_encode_video(ctx, buffer, len, frame);
151        if(bytes <= 0)
152            continue;
153
154        av_init_packet(&packet);
155        if(ctx->coded_frame->pts != (int64_t)AV_NOPTS_VALUE)
156            packet.pts = av_rescale_q(ctx->coded_frame->pts,
157                                      ctx->time_base, stream->time_base);
158        if(ctx->coded_frame->key_frame)
159            packet.flags |= PKT_FLAG_KEY;
160        packet.stream_index = 0;
161        packet.data = buffer;
162        packet.size = bytes;
163        if(av_interleaved_write_frame(fmt, &packet) < 0)
164            return EXIT_FAILURE;
165    }
166
167    av_write_trailer(fmt);
168
169    avcodec_close(stream->codec);
170    av_free(frame->data[0]);
171    av_free(frame);
172    av_free(buffer);
173    av_freep(&fmt->streams[0]->codec);
174    av_freep(&fmt->streams[0]);
175
176    if(sws)
177        sws_freeContext(sws);
178    if(!(fmt->flags & AVFMT_NOFILE))
179        url_fclose(fmt->pb);
180
181    av_free(fmt);
182
183    return EXIT_SUCCESS;
184}
185
Note: See TracBrowser for help on using the repository browser.