lavfi: add asetnsamples audio filter
[ffmpeg.git] / libavfilter / avcodec.c
1 /*
2  * Copyright 2011 Stefano Sabatini | stefasab at gmail.com
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * 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  * FFmpeg is distributed in the hope that it will be useful,
12  * 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  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * libavcodec/libavfilter gluing utilities
24  */
25
26 #include "avcodec.h"
27 #include "libavutil/avassert.h"
28 #include "libavutil/opt.h"
29
30 int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src)
31 {
32     dst->pts    = src->pts;
33     dst->pos    = av_frame_get_pkt_pos(src);
34     dst->format = src->format;
35
36     switch (dst->type) {
37     case AVMEDIA_TYPE_VIDEO:
38         dst->video->w                   = src->width;
39         dst->video->h                   = src->height;
40         dst->video->sample_aspect_ratio = src->sample_aspect_ratio;
41         dst->video->interlaced          = src->interlaced_frame;
42         dst->video->top_field_first     = src->top_field_first;
43         dst->video->key_frame           = src->key_frame;
44         dst->video->pict_type           = src->pict_type;
45         break;
46     case AVMEDIA_TYPE_AUDIO:
47         dst->audio->sample_rate         = src->sample_rate;
48         dst->audio->channel_layout      = src->channel_layout;
49         break;
50     default:
51         return AVERROR(EINVAL);
52     }
53
54     return 0;
55 }
56
57 AVFilterBufferRef *avfilter_get_video_buffer_ref_from_frame(const AVFrame *frame,
58                                                             int perms)
59 {
60     AVFilterBufferRef *picref =
61         avfilter_get_video_buffer_ref_from_arrays(frame->data, frame->linesize, perms,
62                                                   frame->width, frame->height,
63                                                   frame->format);
64     if (!picref)
65         return NULL;
66     avfilter_copy_frame_props(picref, frame);
67     return picref;
68 }
69
70 AVFilterBufferRef *avfilter_get_audio_buffer_ref_from_frame(const AVFrame *frame,
71                                                             int perms)
72 {
73     AVFilterBufferRef *picref =
74         avfilter_get_audio_buffer_ref_from_arrays((uint8_t **)frame->data, frame->linesize[0], perms,
75                                                   frame->nb_samples, frame->format,
76                                                   av_frame_get_channel_layout(frame));
77     if (!picref)
78         return NULL;
79     avfilter_copy_frame_props(picref, frame);
80     return picref;
81 }
82
83 int avfilter_copy_buf_props(AVFrame *dst, const AVFilterBufferRef *src)
84 {
85     int planes, nb_channels;
86
87     if (!dst)
88         return AVERROR(EINVAL);
89     /* abort in case the src is NULL and dst is not, avoid inconsistent state in dst */
90     av_assert0(src);
91
92     memcpy(dst->data, src->data, sizeof(dst->data));
93     memcpy(dst->linesize, src->linesize, sizeof(dst->linesize));
94
95     dst->pts     = src->pts;
96     dst->format  = src->format;
97
98     switch (src->type) {
99     case AVMEDIA_TYPE_VIDEO:
100         av_assert0(src->video);
101         dst->width               = src->video->w;
102         dst->height              = src->video->h;
103         dst->sample_aspect_ratio = src->video->sample_aspect_ratio;
104         dst->interlaced_frame    = src->video->interlaced;
105         dst->top_field_first     = src->video->top_field_first;
106         dst->key_frame           = src->video->key_frame;
107         dst->pict_type           = src->video->pict_type;
108         break;
109     case AVMEDIA_TYPE_AUDIO:
110         av_assert0(src->audio);
111         nb_channels = av_get_channel_layout_nb_channels(src->audio->channel_layout);
112         planes      = av_sample_fmt_is_planar(src->format) ? nb_channels : 1;
113
114         if (planes > FF_ARRAY_ELEMS(dst->data)) {
115             dst->extended_data = av_mallocz(planes * sizeof(*dst->extended_data));
116             if (!dst->extended_data)
117                 return AVERROR(ENOMEM);
118             memcpy(dst->extended_data, src->extended_data,
119                    planes * sizeof(dst->extended_data));
120         } else
121             dst->extended_data = dst->data;
122
123         dst->sample_rate         = src->audio->sample_rate;
124         dst->channel_layout      = src->audio->channel_layout;
125         dst->nb_samples          = src->audio->nb_samples;
126         break;
127     default:
128         return AVERROR(EINVAL);
129     }
130
131     return 0;
132 }
133
134 int avfilter_fill_frame_from_audio_buffer_ref(AVFrame *frame,
135                                               const AVFilterBufferRef *samplesref)
136 {
137     if (!samplesref || !samplesref->audio || !frame)
138         return AVERROR(EINVAL);
139
140     memcpy(frame->data, samplesref->data, sizeof(frame->data));
141     memcpy(frame->linesize, samplesref->linesize, sizeof(frame->linesize));
142     av_frame_set_pkt_pos(frame, samplesref->pos);
143     frame->format         = samplesref->format;
144     frame->nb_samples     = samplesref->audio->nb_samples;
145     frame->pts            = samplesref->pts;
146     frame->sample_rate    = samplesref->audio->sample_rate;
147     frame->channel_layout = samplesref->audio->channel_layout;
148
149     return 0;
150 }
151
152 int avfilter_fill_frame_from_video_buffer_ref(AVFrame *frame,
153                                               const AVFilterBufferRef *picref)
154 {
155     if (!picref || !picref->video || !frame)
156         return AVERROR(EINVAL);
157
158     memcpy(frame->data,     picref->data,     sizeof(frame->data));
159     memcpy(frame->linesize, picref->linesize, sizeof(frame->linesize));
160     av_frame_set_pkt_pos(frame, picref->pos);
161     frame->interlaced_frame = picref->video->interlaced;
162     frame->top_field_first  = picref->video->top_field_first;
163     frame->key_frame        = picref->video->key_frame;
164     frame->pict_type        = picref->video->pict_type;
165     frame->sample_aspect_ratio = picref->video->sample_aspect_ratio;
166     frame->width            = picref->video->w;
167     frame->height           = picref->video->h;
168     frame->format           = picref->format;
169     frame->pts              = picref->pts;
170
171     return 0;
172 }
173
174 int avfilter_fill_frame_from_buffer_ref(AVFrame *frame,
175                                         const AVFilterBufferRef *ref)
176 {
177     if (!ref)
178         return AVERROR(EINVAL);
179     return ref->video ? avfilter_fill_frame_from_video_buffer_ref(frame, ref)
180                       : avfilter_fill_frame_from_audio_buffer_ref(frame, ref);
181 }