]> git.sesse.net Git - ffmpeg/blob - libavfilter/avcodec.c
Merge commit '61d8fa2a1ab76f0f3ac1442faa104ace4b29decc'
[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/channel_layout.h"
29 #include "libavutil/opt.h"
30
31 #if FF_API_AVFILTERBUFFER
32 FF_DISABLE_DEPRECATION_WARNINGS
33 AVFilterBufferRef *avfilter_get_video_buffer_ref_from_frame(const AVFrame *frame,
34                                                             int perms)
35 {
36     AVFilterBufferRef *picref =
37         avfilter_get_video_buffer_ref_from_arrays(frame->data, frame->linesize, perms,
38                                                   frame->width, frame->height,
39                                                   frame->format);
40     if (!picref)
41         return NULL;
42     if (avfilter_copy_frame_props(picref, frame) < 0) {
43         picref->buf->data[0] = NULL;
44         avfilter_unref_bufferp(&picref);
45     }
46     return picref;
47 }
48
49 AVFilterBufferRef *avfilter_get_audio_buffer_ref_from_frame(const AVFrame *frame,
50                                                             int perms)
51 {
52     AVFilterBufferRef *samplesref;
53     int channels = av_frame_get_channels(frame);
54     int64_t layout = av_frame_get_channel_layout(frame);
55
56     if (layout && av_get_channel_layout_nb_channels(layout) != av_frame_get_channels(frame)) {
57         av_log(NULL, AV_LOG_ERROR, "Layout indicates a different number of channels than actually present\n");
58         return NULL;
59     }
60
61     samplesref = avfilter_get_audio_buffer_ref_from_arrays_channels(
62         (uint8_t **)frame->extended_data, frame->linesize[0], perms,
63         frame->nb_samples, frame->format, channels, layout);
64     if (!samplesref)
65         return NULL;
66     if (avfilter_copy_frame_props(samplesref, frame) < 0) {
67         samplesref->buf->data[0] = NULL;
68         avfilter_unref_bufferp(&samplesref);
69     }
70     return samplesref;
71 }
72
73 AVFilterBufferRef *avfilter_get_buffer_ref_from_frame(enum AVMediaType type,
74                                                       const AVFrame *frame,
75                                                       int perms)
76 {
77     switch (type) {
78     case AVMEDIA_TYPE_VIDEO:
79         return avfilter_get_video_buffer_ref_from_frame(frame, perms);
80     case AVMEDIA_TYPE_AUDIO:
81         return avfilter_get_audio_buffer_ref_from_frame(frame, perms);
82     default:
83         return NULL;
84     }
85 }
86
87 int avfilter_copy_buf_props(AVFrame *dst, const AVFilterBufferRef *src)
88 {
89     int planes, nb_channels;
90
91     if (!dst)
92         return AVERROR(EINVAL);
93     /* abort in case the src is NULL and dst is not, avoid inconsistent state in dst */
94     av_assert0(src);
95
96     memcpy(dst->data, src->data, sizeof(dst->data));
97     memcpy(dst->linesize, src->linesize, sizeof(dst->linesize));
98
99     dst->pts     = src->pts;
100     dst->format  = src->format;
101     av_frame_set_pkt_pos(dst, src->pos);
102
103     switch (src->type) {
104     case AVMEDIA_TYPE_VIDEO:
105         av_assert0(src->video);
106         dst->width               = src->video->w;
107         dst->height              = src->video->h;
108         dst->sample_aspect_ratio = src->video->sample_aspect_ratio;
109         dst->interlaced_frame    = src->video->interlaced;
110         dst->top_field_first     = src->video->top_field_first;
111         dst->key_frame           = src->video->key_frame;
112         dst->pict_type           = src->video->pict_type;
113         break;
114     case AVMEDIA_TYPE_AUDIO:
115         av_assert0(src->audio);
116         nb_channels = av_get_channel_layout_nb_channels(src->audio->channel_layout);
117         planes      = av_sample_fmt_is_planar(src->format) ? nb_channels : 1;
118
119         if (planes > FF_ARRAY_ELEMS(dst->data)) {
120             dst->extended_data = av_mallocz_array(planes, sizeof(*dst->extended_data));
121             if (!dst->extended_data)
122                 return AVERROR(ENOMEM);
123             memcpy(dst->extended_data, src->extended_data,
124                    planes * sizeof(*dst->extended_data));
125         } else
126             dst->extended_data = dst->data;
127         dst->nb_samples          = src->audio->nb_samples;
128         av_frame_set_sample_rate   (dst, src->audio->sample_rate);
129         av_frame_set_channel_layout(dst, src->audio->channel_layout);
130         av_frame_set_channels      (dst, src->audio->channels);
131         break;
132     default:
133         return AVERROR(EINVAL);
134     }
135
136     return 0;
137 }
138 FF_ENABLE_DEPRECATION_WARNINGS
139 #endif