]> git.sesse.net Git - ffmpeg/blob - libavfilter/buffer.c
lavfi: add audio mix filter
[ffmpeg] / libavfilter / buffer.c
1 /*
2  * This file is part of Libav.
3  *
4  * Libav is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * Libav is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with Libav; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include "libavutil/audioconvert.h"
20 #include "libavcodec/avcodec.h"
21
22 #include "avfilter.h"
23 #include "internal.h"
24
25 /* TODO: buffer pool.  see comment for avfilter_default_get_video_buffer() */
26 void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
27 {
28     if (ptr->extended_data != ptr->data)
29         av_freep(&ptr->extended_data);
30     av_free(ptr->data[0]);
31     av_free(ptr);
32 }
33
34 AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
35 {
36     AVFilterBufferRef *ret = av_malloc(sizeof(AVFilterBufferRef));
37     if (!ret)
38         return NULL;
39     *ret = *ref;
40     if (ref->type == AVMEDIA_TYPE_VIDEO) {
41         ret->video = av_malloc(sizeof(AVFilterBufferRefVideoProps));
42         if (!ret->video) {
43             av_free(ret);
44             return NULL;
45         }
46         *ret->video = *ref->video;
47         ret->extended_data = ret->data;
48     } else if (ref->type == AVMEDIA_TYPE_AUDIO) {
49         ret->audio = av_malloc(sizeof(AVFilterBufferRefAudioProps));
50         if (!ret->audio) {
51             av_free(ret);
52             return NULL;
53         }
54         *ret->audio = *ref->audio;
55
56         if (ref->extended_data != ref->data) {
57             int nb_channels = av_get_channel_layout_nb_channels(ref->audio->channel_layout);
58             if (!(ret->extended_data = av_malloc(sizeof(*ret->extended_data) *
59                                                  nb_channels))) {
60                 av_freep(&ret->audio);
61                 av_freep(&ret);
62                 return NULL;
63             }
64             memcpy(ret->extended_data, ref->extended_data,
65                    sizeof(*ret->extended_data) * nb_channels);
66         } else
67             ret->extended_data = ret->data;
68     }
69     ret->perms &= pmask;
70     ret->buf->refcount ++;
71     return ret;
72 }
73
74 void avfilter_unref_buffer(AVFilterBufferRef *ref)
75 {
76     if (!ref)
77         return;
78     if (!(--ref->buf->refcount))
79         ref->buf->free(ref->buf);
80     if (ref->extended_data != ref->data)
81         av_freep(&ref->extended_data);
82     av_free(ref->video);
83     av_free(ref->audio);
84     av_free(ref);
85 }
86
87 int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src)
88 {
89     dst->pts    = src->pts;
90     dst->format = src->format;
91
92     switch (dst->type) {
93     case AVMEDIA_TYPE_VIDEO:
94         dst->video->w                   = src->width;
95         dst->video->h                   = src->height;
96         dst->video->pixel_aspect        = src->sample_aspect_ratio;
97         dst->video->interlaced          = src->interlaced_frame;
98         dst->video->top_field_first     = src->top_field_first;
99         dst->video->key_frame           = src->key_frame;
100         dst->video->pict_type           = src->pict_type;
101         break;
102     case AVMEDIA_TYPE_AUDIO:
103         dst->audio->sample_rate         = src->sample_rate;
104         dst->audio->channel_layout      = src->channel_layout;
105         break;
106     default:
107         return AVERROR(EINVAL);
108     }
109
110     return 0;
111 }
112
113 int avfilter_copy_buf_props(AVFrame *dst, const AVFilterBufferRef *src)
114 {
115     int planes, nb_channels;
116
117     memcpy(dst->data, src->data, sizeof(dst->data));
118     memcpy(dst->linesize, src->linesize, sizeof(dst->linesize));
119
120     dst->pts     = src->pts;
121     dst->format  = src->format;
122
123     switch (src->type) {
124     case AVMEDIA_TYPE_VIDEO:
125         dst->width               = src->video->w;
126         dst->height              = src->video->h;
127         dst->sample_aspect_ratio = src->video->pixel_aspect;
128         dst->interlaced_frame    = src->video->interlaced;
129         dst->top_field_first     = src->video->top_field_first;
130         dst->key_frame           = src->video->key_frame;
131         dst->pict_type           = src->video->pict_type;
132         break;
133     case AVMEDIA_TYPE_AUDIO:
134         nb_channels = av_get_channel_layout_nb_channels(src->audio->channel_layout);
135         planes      = av_sample_fmt_is_planar(src->format) ? nb_channels : 1;
136
137         if (planes > FF_ARRAY_ELEMS(dst->data)) {
138             dst->extended_data = av_mallocz(planes * sizeof(*dst->extended_data));
139             if (!dst->extended_data)
140                 return AVERROR(ENOMEM);
141             memcpy(dst->extended_data, src->extended_data,
142                    planes * sizeof(dst->extended_data));
143         } else
144             dst->extended_data = dst->data;
145
146         dst->sample_rate         = src->audio->sample_rate;
147         dst->channel_layout      = src->audio->channel_layout;
148         dst->nb_samples          = src->audio->nb_samples;
149         break;
150     default:
151         return AVERROR(EINVAL);
152     }
153
154     return 0;
155 }
156
157 void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src)
158 {
159     // copy common properties
160     dst->pts             = src->pts;
161     dst->pos             = src->pos;
162
163     switch (src->type) {
164     case AVMEDIA_TYPE_VIDEO: *dst->video = *src->video; break;
165     case AVMEDIA_TYPE_AUDIO: *dst->audio = *src->audio; break;
166     default: break;
167     }
168 }