vflip: switch to filter_frame
[ffmpeg.git] / libavfilter / src_buffer.c
1 /*
2  * Copyright (c) 2008 Vitor Sessak
3  * Copyright (c) 2010 S.N. Hemanth Meenakshisundaram
4  * Copyright (c) 2011 Mina Nagy Zaki
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /**
24  * @file
25  * memory buffer source filter
26  */
27
28 #include "avfilter.h"
29 #include "internal.h"
30 #include "audio.h"
31 #include "avcodec.h"
32 #include "buffersrc.h"
33 #include "asrc_abuffer.h"
34 #include "libavutil/avstring.h"
35 #include "libavutil/channel_layout.h"
36 #include "libavutil/fifo.h"
37 #include "libavutil/imgutils.h"
38
39 typedef struct {
40     AVFifoBuffer     *fifo;
41     AVRational        time_base;     ///< time_base to set in the output link
42     int eof;
43     unsigned          nb_failed_requests;
44
45     /* Video only */
46     AVFilterContext  *scale;
47     int               h, w;
48     enum AVPixelFormat  pix_fmt;
49     AVRational        sample_aspect_ratio;
50     char              sws_param[256];
51
52     /* Audio only */
53     // Audio format of incoming buffers
54     int sample_rate;
55     unsigned int sample_format;
56     int64_t channel_layout;
57
58     // Normalization filters
59     AVFilterContext *aconvert;
60     AVFilterContext *aresample;
61 } BufferSourceContext;
62
63 static void buf_free(AVFilterBuffer *ptr)
64 {
65     av_free(ptr);
66     return;
67 }
68
69 int av_asrc_buffer_add_audio_buffer_ref(AVFilterContext *ctx,
70                                         AVFilterBufferRef *samplesref,
71                                         int av_unused flags)
72 {
73     return av_buffersrc_add_ref(ctx, samplesref, AV_BUFFERSRC_FLAG_NO_COPY);
74 }
75
76 int av_asrc_buffer_add_samples(AVFilterContext *ctx,
77                                uint8_t *data[8], int linesize[8],
78                                int nb_samples, int sample_rate,
79                                int sample_fmt, int64_t channel_layout, int planar,
80                                int64_t pts, int av_unused flags)
81 {
82     AVFilterBufferRef *samplesref;
83
84     samplesref = avfilter_get_audio_buffer_ref_from_arrays(
85                      data, linesize[0], AV_PERM_WRITE,
86                      nb_samples,
87                      sample_fmt, channel_layout);
88     if (!samplesref)
89         return AVERROR(ENOMEM);
90
91     samplesref->buf->free  = buf_free;
92     samplesref->pts = pts;
93     samplesref->audio->sample_rate = sample_rate;
94
95     AV_NOWARN_DEPRECATED(
96     return av_asrc_buffer_add_audio_buffer_ref(ctx, samplesref, 0);
97     )
98 }
99
100 int av_asrc_buffer_add_buffer(AVFilterContext *ctx,
101                               uint8_t *buf, int buf_size, int sample_rate,
102                               int sample_fmt, int64_t channel_layout, int planar,
103                               int64_t pts, int av_unused flags)
104 {
105     uint8_t *data[8] = {0};
106     int linesize[8];
107     int nb_channels = av_get_channel_layout_nb_channels(channel_layout),
108         nb_samples  = buf_size / nb_channels / av_get_bytes_per_sample(sample_fmt);
109
110     av_samples_fill_arrays(data, linesize,
111                            buf, nb_channels, nb_samples,
112                            sample_fmt, 16);
113
114     AV_NOWARN_DEPRECATED(
115     return av_asrc_buffer_add_samples(ctx,
116                                       data, linesize, nb_samples,
117                                       sample_rate,
118                                       sample_fmt, channel_layout, planar,
119                                       pts, flags);
120     )
121 }