00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include "avcodec.h"
00027 #include "libavutil/avassert.h"
00028 #include "libavutil/opt.h"
00029
00030 int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src)
00031 {
00032 dst->pts = src->pts;
00033 dst->pos = av_frame_get_pkt_pos(src);
00034 dst->format = src->format;
00035
00036 switch (dst->type) {
00037 case AVMEDIA_TYPE_VIDEO:
00038 dst->video->w = src->width;
00039 dst->video->h = src->height;
00040 dst->video->sample_aspect_ratio = src->sample_aspect_ratio;
00041 dst->video->interlaced = src->interlaced_frame;
00042 dst->video->top_field_first = src->top_field_first;
00043 dst->video->key_frame = src->key_frame;
00044 dst->video->pict_type = src->pict_type;
00045 av_freep(&dst->video->qp_table);
00046 dst->video->qp_table_linesize = 0;
00047 if (src->qscale_table) {
00048 int qsize = src->qstride ? src->qstride * ((src->height+15)/16) : (src->width+15)/16;
00049 dst->video->qp_table = av_malloc(qsize);
00050 if (!dst->video->qp_table)
00051 return AVERROR(ENOMEM);
00052 dst->video->qp_table_linesize = src->qstride;
00053 dst->video->qp_table_size = qsize;
00054 memcpy(dst->video->qp_table, src->qscale_table, qsize);
00055 }
00056 break;
00057 case AVMEDIA_TYPE_AUDIO:
00058 dst->audio->sample_rate = src->sample_rate;
00059 dst->audio->channel_layout = src->channel_layout;
00060 break;
00061 default:
00062 return AVERROR(EINVAL);
00063 }
00064
00065 return 0;
00066 }
00067
00068 AVFilterBufferRef *avfilter_get_video_buffer_ref_from_frame(const AVFrame *frame,
00069 int perms)
00070 {
00071 AVFilterBufferRef *picref =
00072 avfilter_get_video_buffer_ref_from_arrays(frame->data, frame->linesize, perms,
00073 frame->width, frame->height,
00074 frame->format);
00075 if (!picref)
00076 return NULL;
00077 avfilter_copy_frame_props(picref, frame);
00078 return picref;
00079 }
00080
00081 AVFilterBufferRef *avfilter_get_audio_buffer_ref_from_frame(const AVFrame *frame,
00082 int perms)
00083 {
00084 AVFilterBufferRef *samplesref =
00085 avfilter_get_audio_buffer_ref_from_arrays((uint8_t **)frame->data, frame->linesize[0], perms,
00086 frame->nb_samples, frame->format,
00087 av_frame_get_channel_layout(frame));
00088 if (!samplesref)
00089 return NULL;
00090 avfilter_copy_frame_props(samplesref, frame);
00091 return samplesref;
00092 }
00093
00094 AVFilterBufferRef *avfilter_get_buffer_ref_from_frame(enum AVMediaType type,
00095 const AVFrame *frame,
00096 int perms)
00097 {
00098 switch (type) {
00099 case AVMEDIA_TYPE_VIDEO:
00100 return avfilter_get_video_buffer_ref_from_frame(frame, perms);
00101 case AVMEDIA_TYPE_AUDIO:
00102 return avfilter_get_audio_buffer_ref_from_frame(frame, perms);
00103 default:
00104 return NULL;
00105 }
00106 }
00107
00108 int avfilter_copy_buf_props(AVFrame *dst, const AVFilterBufferRef *src)
00109 {
00110 int planes, nb_channels;
00111
00112 if (!dst)
00113 return AVERROR(EINVAL);
00114
00115 av_assert0(src);
00116
00117 memcpy(dst->data, src->data, sizeof(dst->data));
00118 memcpy(dst->linesize, src->linesize, sizeof(dst->linesize));
00119
00120 dst->pts = src->pts;
00121 dst->format = src->format;
00122 av_frame_set_pkt_pos(dst, src->pos);
00123
00124 switch (src->type) {
00125 case AVMEDIA_TYPE_VIDEO:
00126 av_assert0(src->video);
00127 dst->width = src->video->w;
00128 dst->height = src->video->h;
00129 dst->sample_aspect_ratio = src->video->sample_aspect_ratio;
00130 dst->interlaced_frame = src->video->interlaced;
00131 dst->top_field_first = src->video->top_field_first;
00132 dst->key_frame = src->video->key_frame;
00133 dst->pict_type = src->video->pict_type;
00134 break;
00135 case AVMEDIA_TYPE_AUDIO:
00136 av_assert0(src->audio);
00137 nb_channels = av_get_channel_layout_nb_channels(src->audio->channel_layout);
00138 planes = av_sample_fmt_is_planar(src->format) ? nb_channels : 1;
00139
00140 if (planes > FF_ARRAY_ELEMS(dst->data)) {
00141 dst->extended_data = av_mallocz(planes * sizeof(*dst->extended_data));
00142 if (!dst->extended_data)
00143 return AVERROR(ENOMEM);
00144 memcpy(dst->extended_data, src->extended_data,
00145 planes * sizeof(dst->extended_data));
00146 } else
00147 dst->extended_data = dst->data;
00148 dst->nb_samples = src->audio->nb_samples;
00149 av_frame_set_sample_rate (dst, src->audio->sample_rate);
00150 av_frame_set_channel_layout(dst, src->audio->channel_layout);
00151 break;
00152 default:
00153 return AVERROR(EINVAL);
00154 }
00155
00156 return 0;
00157 }
00158
00159 #ifdef FF_API_FILL_FRAME
00160 int avfilter_fill_frame_from_audio_buffer_ref(AVFrame *frame,
00161 const AVFilterBufferRef *samplesref)
00162 {
00163 return avfilter_copy_buf_props(frame, samplesref);
00164 }
00165
00166 int avfilter_fill_frame_from_video_buffer_ref(AVFrame *frame,
00167 const AVFilterBufferRef *picref)
00168 {
00169 return avfilter_copy_buf_props(frame, picref);
00170 }
00171
00172 int avfilter_fill_frame_from_buffer_ref(AVFrame *frame,
00173 const AVFilterBufferRef *ref)
00174 {
00175 return avfilter_copy_buf_props(frame, ref);
00176 }
00177 #endif