ffmpeg: set the video filters timebase to the demuxers.
[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 "vsrc_buffer.h"
34 #include "asrc_abuffer.h"
35 #include "libavutil/audioconvert.h"
36 #include "libavutil/avstring.h"
37 #include "libavutil/fifo.h"
38 #include "libavutil/imgutils.h"
39
40 typedef struct {
41     AVFifoBuffer     *fifo;
42     AVRational        time_base;     ///< time_base to set in the output link
43     int eof;
44     unsigned          nb_failed_requests;
45
46     /* Video only */
47     AVFilterContext  *scale;
48     int               h, w;
49     enum PixelFormat  pix_fmt;
50     AVRational        sample_aspect_ratio;
51     char              sws_param[256];
52
53     /* Audio only */
54     // Audio format of incoming buffers
55     int sample_rate;
56     unsigned int sample_format;
57     int64_t channel_layout;
58
59     // Normalization filters
60     AVFilterContext *aconvert;
61     AVFilterContext *aresample;
62 } BufferSourceContext;
63
64 static void buf_free(AVFilterBuffer *ptr)
65 {
66     av_free(ptr);
67     return;
68 }
69
70 int av_vsrc_buffer_add_video_buffer_ref(AVFilterContext *buffer_filter,
71                                         AVFilterBufferRef *picref, int flags)
72 {
73     return av_buffersrc_add_ref(buffer_filter, picref, 0);
74 }
75
76 #if CONFIG_AVCODEC
77 #include "avcodec.h"
78
79 int av_vsrc_buffer_add_frame(AVFilterContext *buffer_src,
80                              const AVFrame *frame, int flags)
81 {
82     return av_buffersrc_add_frame(buffer_src, frame, 0);
83 }
84 #endif
85
86 unsigned av_vsrc_buffer_get_nb_failed_requests(AVFilterContext *buffer_src)
87 {
88     return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
89 }
90
91 int av_asrc_buffer_add_audio_buffer_ref(AVFilterContext *ctx,
92                                         AVFilterBufferRef *samplesref,
93                                         int av_unused flags)
94 {
95     return av_buffersrc_add_ref(ctx, samplesref, AV_BUFFERSRC_FLAG_NO_COPY);
96 }
97
98 int av_asrc_buffer_add_samples(AVFilterContext *ctx,
99                                uint8_t *data[8], int linesize[8],
100                                int nb_samples, int sample_rate,
101                                int sample_fmt, int64_t channel_layout, int planar,
102                                int64_t pts, int av_unused flags)
103 {
104     AVFilterBufferRef *samplesref;
105
106     samplesref = avfilter_get_audio_buffer_ref_from_arrays(
107                      data, linesize[0], AV_PERM_WRITE,
108                      nb_samples,
109                      sample_fmt, channel_layout);
110     if (!samplesref)
111         return AVERROR(ENOMEM);
112
113     samplesref->buf->free  = buf_free;
114     samplesref->pts = pts;
115     samplesref->audio->sample_rate = sample_rate;
116
117     AV_NOWARN_DEPRECATED(
118     return av_asrc_buffer_add_audio_buffer_ref(ctx, samplesref, 0);
119     )
120 }
121
122 int av_asrc_buffer_add_buffer(AVFilterContext *ctx,
123                               uint8_t *buf, int buf_size, int sample_rate,
124                               int sample_fmt, int64_t channel_layout, int planar,
125                               int64_t pts, int av_unused flags)
126 {
127     uint8_t *data[8] = {0};
128     int linesize[8];
129     int nb_channels = av_get_channel_layout_nb_channels(channel_layout),
130         nb_samples  = buf_size / nb_channels / av_get_bytes_per_sample(sample_fmt);
131
132     av_samples_fill_arrays(data, linesize,
133                            buf, nb_channels, nb_samples,
134                            sample_fmt, 16);
135
136     AV_NOWARN_DEPRECATED(
137     return av_asrc_buffer_add_samples(ctx,
138                                       data, linesize, nb_samples,
139                                       sample_rate,
140                                       sample_fmt, channel_layout, planar,
141                                       pts, flags);
142     )
143 }