Statistics
| Branch: | Revision:

ffmpeg / libavfilter / vsrc_buffer.c @ e73127a4

History | View | Annotate | Download (5.61 KB)

1 cf13f204 Michael Niedermayer
/*
2
 * Copyright (c) 2008 Vitor Sessak
3
 *
4 2912e87a Mans Rullgard
 * This file is part of Libav.
5 cf13f204 Michael Niedermayer
 *
6 2912e87a Mans Rullgard
 * Libav is free software; you can redistribute it and/or
7 cf13f204 Michael Niedermayer
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11 2912e87a Mans Rullgard
 * Libav is distributed in the hope that it will be useful,
12 cf13f204 Michael Niedermayer
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17 2912e87a Mans Rullgard
 * License along with Libav; if not, write to the Free Software
18 cf13f204 Michael Niedermayer
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21 b5634e45 Stefano Sabatini
/**
22
 * @file
23
 * memory buffer source filter
24
 */
25
26 cf13f204 Michael Niedermayer
#include "avfilter.h"
27
#include "vsrc_buffer.h"
28 737eb597 Reinhard Tartler
#include "libavutil/imgutils.h"
29 cf13f204 Michael Niedermayer
30
typedef struct {
31
    int64_t           pts;
32
    AVFrame           frame;
33
    int               has_frame;
34 9164afcb Carl Eugen Hoyos
    int               h, w;
35
    enum PixelFormat  pix_fmt;
36 94498ec9 Stefano Sabatini
    AVRational        time_base;     ///< time_base to set in the output link
37 cf13f204 Michael Niedermayer
    AVRational        pixel_aspect;
38
} BufferSourceContext;
39
40
int av_vsrc_buffer_add_frame(AVFilterContext *buffer_filter, AVFrame *frame,
41
                             int64_t pts, AVRational pixel_aspect)
42
{
43
    BufferSourceContext *c = buffer_filter->priv;
44
45
    if (c->has_frame) {
46
        av_log(buffer_filter, AV_LOG_ERROR,
47
               "Buffering several frames is not supported. "
48
               "Please consume all available frames before adding a new one.\n"
49
            );
50
        //return -1;
51
    }
52
53
    memcpy(c->frame.data    , frame->data    , sizeof(frame->data));
54
    memcpy(c->frame.linesize, frame->linesize, sizeof(frame->linesize));
55 efdc74ef Michael Niedermayer
    c->frame.interlaced_frame= frame->interlaced_frame;
56
    c->frame.top_field_first = frame->top_field_first;
57 10d39405 Roger Pau Monné
    c->frame.key_frame = frame->key_frame;
58
    c->frame.pict_type = frame->pict_type;
59 cf13f204 Michael Niedermayer
    c->pts = pts;
60
    c->pixel_aspect = pixel_aspect;
61
    c->has_frame = 1;
62
63
    return 0;
64
}
65
66
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
67
{
68
    BufferSourceContext *c = ctx->priv;
69 59775b3c Stefano Sabatini
    char pix_fmt_str[128];
70 b8dddebf Stefano Sabatini
    int n = 0;
71 cf13f204 Michael Niedermayer
72 94498ec9 Stefano Sabatini
    if (!args ||
73 7a11c82f Michael Niedermayer
        (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d", &c->w, &c->h, pix_fmt_str,
74
                    &c->time_base.num, &c->time_base.den,
75
                    &c->pixel_aspect.num, &c->pixel_aspect.den)) != 7) {
76
        av_log(ctx, AV_LOG_ERROR, "Expected 7 arguments, but only %d found in '%s'\n", n, args);
77 b8dddebf Stefano Sabatini
        return AVERROR(EINVAL);
78
    }
79 59775b3c Stefano Sabatini
    if ((c->pix_fmt = av_get_pix_fmt(pix_fmt_str)) == PIX_FMT_NONE) {
80
        char *tail;
81
        c->pix_fmt = strtol(pix_fmt_str, &tail, 10);
82
        if (*tail || c->pix_fmt < 0 || c->pix_fmt >= PIX_FMT_NB) {
83
            av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", pix_fmt_str);
84
            return AVERROR(EINVAL);
85
        }
86
    }
87 cf13f204 Michael Niedermayer
88 e2f48c05 Stefano Sabatini
    av_log(ctx, AV_LOG_INFO, "w:%d h:%d pixfmt:%s\n", c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name);
89 b8dddebf Stefano Sabatini
    return 0;
90 cf13f204 Michael Niedermayer
}
91
92
static int query_formats(AVFilterContext *ctx)
93
{
94
    BufferSourceContext *c = ctx->priv;
95
    enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE };
96
97
    avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
98
    return 0;
99
}
100
101
static int config_props(AVFilterLink *link)
102
{
103
    BufferSourceContext *c = link->src->priv;
104
105
    link->w = c->w;
106
    link->h = c->h;
107 7a11c82f Michael Niedermayer
    link->sample_aspect_ratio = c->pixel_aspect;
108 94498ec9 Stefano Sabatini
    link->time_base = c->time_base;
109 cf13f204 Michael Niedermayer
110
    return 0;
111
}
112
113
static int request_frame(AVFilterLink *link)
114
{
115
    BufferSourceContext *c = link->src->priv;
116 ecc8dada S.N. Hemanth Meenakshisundaram
    AVFilterBufferRef *picref;
117 cf13f204 Michael Niedermayer
118
    if (!c->has_frame) {
119
        av_log(link->src, AV_LOG_ERROR,
120
               "request_frame() called with no available frame!\n");
121
        //return -1;
122
    }
123
124
    /* This picture will be needed unmodified later for decoding the next
125
     * frame */
126
    picref = avfilter_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
127
                                       AV_PERM_REUSE2,
128
                                       link->w, link->h);
129
130 34017fd9 Stefano Sabatini
    av_image_copy(picref->data, picref->linesize,
131 4afbcf46 Stefano Sabatini
                  c->frame.data, c->frame.linesize,
132
                  picref->format, link->w, link->h);
133 cf13f204 Michael Niedermayer
134 cc80caff S.N. Hemanth Meenakshisundaram
    picref->pts                    = c->pts;
135
    picref->video->pixel_aspect    = c->pixel_aspect;
136
    picref->video->interlaced      = c->frame.interlaced_frame;
137
    picref->video->top_field_first = c->frame.top_field_first;
138 10d39405 Roger Pau Monné
    picref->video->key_frame       = c->frame.key_frame;
139
    picref->video->pict_type       = c->frame.pict_type;
140 7fce481a S.N. Hemanth Meenakshisundaram
    avfilter_start_frame(link, avfilter_ref_buffer(picref, ~0));
141 cf13f204 Michael Niedermayer
    avfilter_draw_slice(link, 0, link->h, 1);
142
    avfilter_end_frame(link);
143 7fce481a S.N. Hemanth Meenakshisundaram
    avfilter_unref_buffer(picref);
144 cf13f204 Michael Niedermayer
145
    c->has_frame = 0;
146
147
    return 0;
148
}
149
150
static int poll_frame(AVFilterLink *link)
151
{
152
    BufferSourceContext *c = link->src->priv;
153
    return !!(c->has_frame);
154
}
155
156 4055438b Stefano Sabatini
AVFilter avfilter_vsrc_buffer = {
157 cf13f204 Michael Niedermayer
    .name      = "buffer",
158 ce1f8536 Stefano Sabatini
    .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
159 cf13f204 Michael Niedermayer
    .priv_size = sizeof(BufferSourceContext),
160
    .query_formats = query_formats,
161
162
    .init      = init,
163
164
    .inputs    = (AVFilterPad[]) {{ .name = NULL }},
165
    .outputs   = (AVFilterPad[]) {{ .name            = "default",
166
                                    .type            = AVMEDIA_TYPE_VIDEO,
167
                                    .request_frame   = request_frame,
168
                                    .poll_frame      = poll_frame,
169
                                    .config_props    = config_props, },
170
                                  { .name = NULL}},
171
};