]> git.sesse.net Git - ffmpeg/blob - libavfilter/avcodec.c
Merge commit 'f1d8763a02b5fce9a7d9789e049d74a45b15e1e8'
[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         dst->audio->channels            = src->channels;
64         if(src->channels < av_get_channel_layout_nb_channels(src->channel_layout)) {
65             av_log(NULL, AV_LOG_ERROR, "libavfilter does not support this channel layout\n");
66             return AVERROR(EINVAL);
67         }
68         break;
69     default:
70         return AVERROR(EINVAL);
71     }
72
73     return 0;
74 }
75
76 AVFilterBufferRef *avfilter_get_video_buffer_ref_from_frame(const AVFrame *frame,
77                                                             int perms)
78 {
79     AVFilterBufferRef *picref =
80         avfilter_get_video_buffer_ref_from_arrays(frame->data, frame->linesize, perms,
81                                                   frame->width, frame->height,
82                                                   frame->format);
83     if (!picref)
84         return NULL;
85     if (avfilter_copy_frame_props(picref, frame) < 0) {
86         picref->buf->data[0] = NULL;
87         avfilter_unref_bufferp(&picref);
88     }
89     return picref;
90 }
91
92 AVFilterBufferRef *avfilter_get_audio_buffer_ref_from_frame(const AVFrame *frame,
93                                                             int perms)
94 {
95     AVFilterBufferRef *samplesref;
96     int64_t layout = av_frame_get_channel_layout(frame);
97
98     if(av_frame_get_channels(frame) > 8) // libavfilter does not suport more than 8 channels FIXME, remove once libavfilter is fixed
99         return NULL;
100
101     if (layout && av_get_channel_layout_nb_channels(layout) != av_frame_get_channels(frame)) {
102         av_log(0, AV_LOG_ERROR, "Layout indicates a differnt number of channels than actually present\n");
103         return NULL;
104     }
105
106     samplesref = avfilter_get_audio_buffer_ref_from_arrays((uint8_t **)frame->data, frame->linesize[0], perms,
107                                                   frame->nb_samples, frame->format,
108                                                   av_frame_get_channel_layout(frame));
109     if (!samplesref)
110         return NULL;
111     if (avfilter_copy_frame_props(samplesref, frame) < 0) {
112         samplesref->buf->data[0] = NULL;
113         avfilter_unref_bufferp(&samplesref);
114     }
115     return samplesref;
116 }
117
118 AVFilterBufferRef *avfilter_get_buffer_ref_from_frame(enum AVMediaType type,
119                                                       const AVFrame *frame,
120                                                       int perms)
121 {
122     switch (type) {
123     case AVMEDIA_TYPE_VIDEO:
124         return avfilter_get_video_buffer_ref_from_frame(frame, perms);
125     case AVMEDIA_TYPE_AUDIO:
126         return avfilter_get_audio_buffer_ref_from_frame(frame, perms);
127     default:
128         return NULL;
129     }
130 }
131
132 int avfilter_copy_buf_props(AVFrame *dst, const AVFilterBufferRef *src)
133 {
134     int planes, nb_channels;
135
136     if (!dst)
137         return AVERROR(EINVAL);
138     /* abort in case the src is NULL and dst is not, avoid inconsistent state in dst */
139     av_assert0(src);
140
141     memcpy(dst->data, src->data, sizeof(dst->data));
142     memcpy(dst->linesize, src->linesize, sizeof(dst->linesize));
143
144     dst->pts     = src->pts;
145     dst->format  = src->format;
146     av_frame_set_pkt_pos(dst, src->pos);
147
148     switch (src->type) {
149     case AVMEDIA_TYPE_VIDEO:
150         av_assert0(src->video);
151         dst->width               = src->video->w;
152         dst->height              = src->video->h;
153         dst->sample_aspect_ratio = src->video->sample_aspect_ratio;
154         dst->interlaced_frame    = src->video->interlaced;
155         dst->top_field_first     = src->video->top_field_first;
156         dst->key_frame           = src->video->key_frame;
157         dst->pict_type           = src->video->pict_type;
158         break;
159     case AVMEDIA_TYPE_AUDIO:
160         av_assert0(src->audio);
161         nb_channels = av_get_channel_layout_nb_channels(src->audio->channel_layout);
162         planes      = av_sample_fmt_is_planar(src->format) ? nb_channels : 1;
163
164         if (planes > FF_ARRAY_ELEMS(dst->data)) {
165             dst->extended_data = av_mallocz(planes * sizeof(*dst->extended_data));
166             if (!dst->extended_data)
167                 return AVERROR(ENOMEM);
168             memcpy(dst->extended_data, src->extended_data,
169                    planes * sizeof(*dst->extended_data));
170         } else
171             dst->extended_data = dst->data;
172         dst->nb_samples          = src->audio->nb_samples;
173         av_frame_set_sample_rate   (dst, src->audio->sample_rate);
174         av_frame_set_channel_layout(dst, src->audio->channel_layout);
175         av_frame_set_channels      (dst, src->audio->channels);
176         break;
177     default:
178         return AVERROR(EINVAL);
179     }
180
181     return 0;
182 }
183
184 #ifdef FF_API_FILL_FRAME
185 int avfilter_fill_frame_from_audio_buffer_ref(AVFrame *frame,
186                                               const AVFilterBufferRef *samplesref)
187 {
188     return avfilter_copy_buf_props(frame, samplesref);
189 }
190
191 int avfilter_fill_frame_from_video_buffer_ref(AVFrame *frame,
192                                               const AVFilterBufferRef *picref)
193 {
194     return avfilter_copy_buf_props(frame, picref);
195 }
196
197 int avfilter_fill_frame_from_buffer_ref(AVFrame *frame,
198                                         const AVFilterBufferRef *ref)
199 {
200     return avfilter_copy_buf_props(frame, ref);
201 }
202 #endif