lavfi: Fill linesize, sample_rate and channel_layout fields in avfilter_fill_frame_fr...
[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/opt.h"
28
29 AVFilterBufferRef *avfilter_get_video_buffer_ref_from_frame(const AVFrame *frame,
30                                                             int perms)
31 {
32     AVFilterBufferRef *picref =
33         avfilter_get_video_buffer_ref_from_arrays(frame->data, frame->linesize, perms,
34                                                   frame->width, frame->height,
35                                                   frame->format);
36     if (!picref)
37         return NULL;
38     avfilter_copy_frame_props(picref, frame);
39     return picref;
40 }
41
42 AVFilterBufferRef *avfilter_get_audio_buffer_ref_from_frame(const AVFrame *frame,
43                                                             int perms)
44 {
45     AVFilterBufferRef *picref =
46         avfilter_get_audio_buffer_ref_from_arrays((uint8_t **)frame->data, frame->linesize[0], perms,
47                                                   frame->nb_samples, frame->format,
48                                                   av_frame_get_channel_layout(frame));
49     if (!picref)
50         return NULL;
51     avfilter_copy_frame_props(picref, frame);
52     return picref;
53 }
54
55 int avfilter_fill_frame_from_audio_buffer_ref(AVFrame *frame,
56                                               const AVFilterBufferRef *samplesref)
57 {
58     if (!samplesref || !samplesref->audio || !frame)
59         return AVERROR(EINVAL);
60
61     memcpy(frame->data, samplesref->data, sizeof(frame->data));
62     memcpy(frame->linesize, samplesref->linesize, sizeof(frame->linesize));
63     frame->pkt_pos        = samplesref->pos;
64     frame->format         = samplesref->format;
65     frame->nb_samples     = samplesref->audio->nb_samples;
66     frame->pts            = samplesref->pts;
67     frame->sample_rate    = samplesref->audio->sample_rate;
68     frame->channel_layout = samplesref->audio->channel_layout;
69
70     return 0;
71 }
72
73 int avfilter_fill_frame_from_video_buffer_ref(AVFrame *frame,
74                                               const AVFilterBufferRef *picref)
75 {
76     if (!picref || !picref->video || !frame)
77         return AVERROR(EINVAL);
78
79     memcpy(frame->data,     picref->data,     sizeof(frame->data));
80     memcpy(frame->linesize, picref->linesize, sizeof(frame->linesize));
81     frame->pkt_pos          = picref->pos;
82     frame->interlaced_frame = picref->video->interlaced;
83     frame->top_field_first  = picref->video->top_field_first;
84     frame->key_frame        = picref->video->key_frame;
85     frame->pict_type        = picref->video->pict_type;
86     frame->sample_aspect_ratio = picref->video->sample_aspect_ratio;
87     frame->width            = picref->video->w;
88     frame->height           = picref->video->h;
89     frame->format           = picref->format;
90     frame->pts              = picref->pts;
91
92     return 0;
93 }
94
95 int avfilter_fill_frame_from_buffer_ref(AVFrame *frame,
96                                         const AVFilterBufferRef *ref)
97 {
98     if (!ref)
99         return AVERROR(EINVAL);
100     return ref->video ? avfilter_fill_frame_from_video_buffer_ref(frame, ref)
101                       : avfilter_fill_frame_from_audio_buffer_ref(frame, ref);
102 }