ce003abaa08b39699947805d90a64eb0ae0f205c
[ffmpeg.git] / libavfilter / avcodec.c
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 /**
20  * @file
21  * libavcodec/libavfilter gluing utilities
22  */
23
24 #include "avcodec.h"
25 #include "libavutil/opt.h"
26
27 AVFilterBufferRef *avfilter_get_video_buffer_ref_from_frame(const AVFrame *frame,
28                                                             int perms)
29 {
30     AVFilterBufferRef *picref =
31         avfilter_get_video_buffer_ref_from_arrays(frame->data, frame->linesize, perms,
32                                                   frame->width, frame->height,
33                                                   frame->format);
34     if (!picref)
35         return NULL;
36     avfilter_copy_frame_props(picref, frame);
37     return picref;
38 }
39
40 AVFilterBufferRef *avfilter_get_audio_buffer_ref_from_frame(const AVFrame *frame,
41                                                             int perms)
42 {
43     AVFilterBufferRef *picref =
44         avfilter_get_audio_buffer_ref_from_arrays((uint8_t **)frame->data, frame->linesize[0], perms,
45                                                   frame->nb_samples, frame->format,
46                                                   av_frame_get_channel_layout(frame));
47     if (!picref)
48         return NULL;
49     avfilter_copy_frame_props(picref, frame);
50     return picref;
51 }
52
53 int avfilter_fill_frame_from_audio_buffer_ref(AVFrame *frame,
54                                               const AVFilterBufferRef *samplesref)
55 {
56     if (!samplesref || !samplesref->audio || !frame)
57         return AVERROR(EINVAL);
58
59     memcpy(frame->data, samplesref->data, sizeof(frame->data));
60     frame->pkt_pos    = samplesref->pos;
61     frame->format     = samplesref->format;
62     frame->nb_samples = samplesref->audio->nb_samples;
63     frame->pts        = samplesref->pts;
64
65     return 0;
66 }
67
68 int avfilter_fill_frame_from_video_buffer_ref(AVFrame *frame,
69                                               const AVFilterBufferRef *picref)
70 {
71     if (!picref || !picref->video || !frame)
72         return AVERROR(EINVAL);
73
74     memcpy(frame->data,     picref->data,     sizeof(frame->data));
75     memcpy(frame->linesize, picref->linesize, sizeof(frame->linesize));
76     frame->pkt_pos          = picref->pos;
77     frame->interlaced_frame = picref->video->interlaced;
78     frame->top_field_first  = picref->video->top_field_first;
79     frame->key_frame        = picref->video->key_frame;
80     frame->pict_type        = picref->video->pict_type;
81     frame->sample_aspect_ratio = picref->video->sample_aspect_ratio;
82     frame->width            = picref->video->w;
83     frame->height           = picref->video->h;
84     frame->format           = picref->format;
85     frame->pts              = picref->pts;
86
87     return 0;
88 }
89
90 int avfilter_fill_frame_from_buffer_ref(AVFrame *frame,
91                                         const AVFilterBufferRef *ref)
92 {
93     if (!ref)
94         return AVERROR(EINVAL);
95     return ref->video ? avfilter_fill_frame_from_video_buffer_ref(frame, ref)
96                       : avfilter_fill_frame_from_audio_buffer_ref(frame, ref);
97 }