]> git.sesse.net Git - ffmpeg/blob - libavfilter/avcodec.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / 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 int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src)
30 {
31     dst->pts    = src->pts;
32     dst->pos    = av_frame_get_pkt_pos(src);
33     dst->format = src->format;
34
35     switch (dst->type) {
36     case AVMEDIA_TYPE_VIDEO:
37         dst->video->w                   = src->width;
38         dst->video->h                   = src->height;
39         dst->video->sample_aspect_ratio = src->sample_aspect_ratio;
40         dst->video->interlaced          = src->interlaced_frame;
41         dst->video->top_field_first     = src->top_field_first;
42         dst->video->key_frame           = src->key_frame;
43         dst->video->pict_type           = src->pict_type;
44         break;
45     case AVMEDIA_TYPE_AUDIO:
46         dst->audio->sample_rate         = src->sample_rate;
47         dst->audio->channel_layout      = src->channel_layout;
48         break;
49     default:
50         return AVERROR(EINVAL);
51     }
52
53     return 0;
54 }
55
56 AVFilterBufferRef *avfilter_get_video_buffer_ref_from_frame(const AVFrame *frame,
57                                                             int perms)
58 {
59     AVFilterBufferRef *picref =
60         avfilter_get_video_buffer_ref_from_arrays(frame->data, frame->linesize, perms,
61                                                   frame->width, frame->height,
62                                                   frame->format);
63     if (!picref)
64         return NULL;
65     avfilter_copy_frame_props(picref, frame);
66     return picref;
67 }
68
69 AVFilterBufferRef *avfilter_get_audio_buffer_ref_from_frame(const AVFrame *frame,
70                                                             int perms)
71 {
72     AVFilterBufferRef *picref =
73         avfilter_get_audio_buffer_ref_from_arrays((uint8_t **)frame->data, frame->linesize[0], perms,
74                                                   frame->nb_samples, frame->format,
75                                                   av_frame_get_channel_layout(frame));
76     if (!picref)
77         return NULL;
78     avfilter_copy_frame_props(picref, frame);
79     return picref;
80 }
81
82 int avfilter_fill_frame_from_audio_buffer_ref(AVFrame *frame,
83                                               const AVFilterBufferRef *samplesref)
84 {
85     if (!samplesref || !samplesref->audio || !frame)
86         return AVERROR(EINVAL);
87
88     memcpy(frame->data, samplesref->data, sizeof(frame->data));
89     memcpy(frame->linesize, samplesref->linesize, sizeof(frame->linesize));
90     av_frame_set_pkt_pos(frame, samplesref->pos);
91     frame->format         = samplesref->format;
92     frame->nb_samples     = samplesref->audio->nb_samples;
93     frame->pts            = samplesref->pts;
94     frame->sample_rate    = samplesref->audio->sample_rate;
95     frame->channel_layout = samplesref->audio->channel_layout;
96
97     return 0;
98 }
99
100 int avfilter_fill_frame_from_video_buffer_ref(AVFrame *frame,
101                                               const AVFilterBufferRef *picref)
102 {
103     if (!picref || !picref->video || !frame)
104         return AVERROR(EINVAL);
105
106     memcpy(frame->data,     picref->data,     sizeof(frame->data));
107     memcpy(frame->linesize, picref->linesize, sizeof(frame->linesize));
108     av_frame_set_pkt_pos(frame, picref->pos);
109     frame->interlaced_frame = picref->video->interlaced;
110     frame->top_field_first  = picref->video->top_field_first;
111     frame->key_frame        = picref->video->key_frame;
112     frame->pict_type        = picref->video->pict_type;
113     frame->sample_aspect_ratio = picref->video->sample_aspect_ratio;
114     frame->width            = picref->video->w;
115     frame->height           = picref->video->h;
116     frame->format           = picref->format;
117     frame->pts              = picref->pts;
118
119     return 0;
120 }
121
122 int avfilter_fill_frame_from_buffer_ref(AVFrame *frame,
123                                         const AVFilterBufferRef *ref)
124 {
125     if (!ref)
126         return AVERROR(EINVAL);
127     return ref->video ? avfilter_fill_frame_from_video_buffer_ref(frame, ref)
128                       : avfilter_fill_frame_from_audio_buffer_ref(frame, ref);
129 }