]> git.sesse.net Git - ffmpeg/blob - libavfilter/avcodec.c
Merge commit 'c68317ebbe4915035df0b08c23eea7a0b80ab881'
[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/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     av_dict_free(&dst->metadata);
37     av_dict_copy(&dst->metadata, av_frame_get_metadata(src), 0);
38
39     switch (dst->type) {
40     case AVMEDIA_TYPE_VIDEO:
41         dst->video->w                   = src->width;
42         dst->video->h                   = src->height;
43         dst->video->sample_aspect_ratio = src->sample_aspect_ratio;
44         dst->video->interlaced          = src->interlaced_frame;
45         dst->video->top_field_first     = src->top_field_first;
46         dst->video->key_frame           = src->key_frame;
47         dst->video->pict_type           = src->pict_type;
48         av_freep(&dst->video->qp_table);
49         dst->video->qp_table_linesize = 0;
50         if (src->qscale_table) {
51             int qsize = src->qstride ? src->qstride * ((src->height+15)/16) : (src->width+15)/16;
52             dst->video->qp_table = av_malloc(qsize);
53             if (!dst->video->qp_table)
54                 return AVERROR(ENOMEM);
55             dst->video->qp_table_linesize = src->qstride;
56             dst->video->qp_table_size     = qsize;
57             memcpy(dst->video->qp_table, src->qscale_table, qsize);
58         }
59         break;
60     case AVMEDIA_TYPE_AUDIO:
61         dst->audio->sample_rate         = src->sample_rate;
62         dst->audio->channel_layout      = src->channel_layout;
63         break;
64     default:
65         return AVERROR(EINVAL);
66     }
67
68     return 0;
69 }
70
71 AVFilterBufferRef *avfilter_get_video_buffer_ref_from_frame(const AVFrame *frame,
72                                                             int perms)
73 {
74     AVFilterBufferRef *picref =
75         avfilter_get_video_buffer_ref_from_arrays(frame->data, frame->linesize, perms,
76                                                   frame->width, frame->height,
77                                                   frame->format);
78     if (!picref)
79         return NULL;
80     avfilter_copy_frame_props(picref, frame);
81     return picref;
82 }
83
84 AVFilterBufferRef *avfilter_get_audio_buffer_ref_from_frame(const AVFrame *frame,
85                                                             int perms)
86 {
87     AVFilterBufferRef *samplesref =
88         avfilter_get_audio_buffer_ref_from_arrays((uint8_t **)frame->data, frame->linesize[0], perms,
89                                                   frame->nb_samples, frame->format,
90                                                   av_frame_get_channel_layout(frame));
91     if (!samplesref)
92         return NULL;
93     avfilter_copy_frame_props(samplesref, frame);
94     return samplesref;
95 }
96
97 AVFilterBufferRef *avfilter_get_buffer_ref_from_frame(enum AVMediaType type,
98                                                       const AVFrame *frame,
99                                                       int perms)
100 {
101     switch (type) {
102     case AVMEDIA_TYPE_VIDEO:
103         return avfilter_get_video_buffer_ref_from_frame(frame, perms);
104     case AVMEDIA_TYPE_AUDIO:
105         return avfilter_get_audio_buffer_ref_from_frame(frame, perms);
106     default:
107         return NULL;
108     }
109 }
110
111 int avfilter_copy_buf_props(AVFrame *dst, const AVFilterBufferRef *src)
112 {
113     int planes, nb_channels;
114
115     if (!dst)
116         return AVERROR(EINVAL);
117     /* abort in case the src is NULL and dst is not, avoid inconsistent state in dst */
118     av_assert0(src);
119
120     memcpy(dst->data, src->data, sizeof(dst->data));
121     memcpy(dst->linesize, src->linesize, sizeof(dst->linesize));
122
123     dst->pts     = src->pts;
124     dst->format  = src->format;
125     av_frame_set_pkt_pos(dst, src->pos);
126
127     switch (src->type) {
128     case AVMEDIA_TYPE_VIDEO:
129         av_assert0(src->video);
130         dst->width               = src->video->w;
131         dst->height              = src->video->h;
132         dst->sample_aspect_ratio = src->video->sample_aspect_ratio;
133         dst->interlaced_frame    = src->video->interlaced;
134         dst->top_field_first     = src->video->top_field_first;
135         dst->key_frame           = src->video->key_frame;
136         dst->pict_type           = src->video->pict_type;
137         break;
138     case AVMEDIA_TYPE_AUDIO:
139         av_assert0(src->audio);
140         nb_channels = av_get_channel_layout_nb_channels(src->audio->channel_layout);
141         planes      = av_sample_fmt_is_planar(src->format) ? nb_channels : 1;
142
143         if (planes > FF_ARRAY_ELEMS(dst->data)) {
144             dst->extended_data = av_mallocz(planes * sizeof(*dst->extended_data));
145             if (!dst->extended_data)
146                 return AVERROR(ENOMEM);
147             memcpy(dst->extended_data, src->extended_data,
148                    planes * sizeof(*dst->extended_data));
149         } else
150             dst->extended_data = dst->data;
151         dst->nb_samples          = src->audio->nb_samples;
152         av_frame_set_sample_rate   (dst, src->audio->sample_rate);
153         av_frame_set_channel_layout(dst, src->audio->channel_layout);
154         break;
155     default:
156         return AVERROR(EINVAL);
157     }
158
159     return 0;
160 }
161
162 #ifdef FF_API_FILL_FRAME
163 int avfilter_fill_frame_from_audio_buffer_ref(AVFrame *frame,
164                                               const AVFilterBufferRef *samplesref)
165 {
166     return avfilter_copy_buf_props(frame, samplesref);
167 }
168
169 int avfilter_fill_frame_from_video_buffer_ref(AVFrame *frame,
170                                               const AVFilterBufferRef *picref)
171 {
172     return avfilter_copy_buf_props(frame, picref);
173 }
174
175 int avfilter_fill_frame_from_buffer_ref(AVFrame *frame,
176                                         const AVFilterBufferRef *ref)
177 {
178     return avfilter_copy_buf_props(frame, ref);
179 }
180 #endif