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