You've already forked FFmpeg
							
							
				mirror of
				https://github.com/FFmpeg/FFmpeg.git
				synced 2025-10-30 23:18:11 +02:00 
			
		
		
		
	lavfi: add a new function av_buffersrc_buffer().
It can be used to directly pass a AVFilterBufferRef to lavfi, avoiding a memcpy.
This commit is contained in:
		| @@ -3,7 +3,7 @@ FFLIBS = avutil | |||||||
| FFLIBS-$(CONFIG_MOVIE_FILTER) += avformat avcodec | FFLIBS-$(CONFIG_MOVIE_FILTER) += avformat avcodec | ||||||
| FFLIBS-$(CONFIG_SCALE_FILTER) += swscale | FFLIBS-$(CONFIG_SCALE_FILTER) += swscale | ||||||
|  |  | ||||||
| HEADERS = avfilter.h avfiltergraph.h vsrc_buffer.h | HEADERS = avfilter.h avfiltergraph.h buffersrc.h vsrc_buffer.h | ||||||
|  |  | ||||||
| OBJS = allfilters.o                                                     \ | OBJS = allfilters.o                                                     \ | ||||||
|        avfilter.o                                                       \ |        avfilter.o                                                       \ | ||||||
|   | |||||||
							
								
								
									
										38
									
								
								libavfilter/buffersrc.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								libavfilter/buffersrc.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,38 @@ | |||||||
|  | /* | ||||||
|  |  * | ||||||
|  |  * This file is part of Libav. | ||||||
|  |  * | ||||||
|  |  * Libav is free software; you can redistribute it and/or | ||||||
|  |  * modify it under the terms of the GNU Lesser General Public | ||||||
|  |  * License as published by the Free Software Foundation; either | ||||||
|  |  * version 2.1 of the License, or (at your option) any later version. | ||||||
|  |  * | ||||||
|  |  * Libav is distributed in the hope that it will be useful, | ||||||
|  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||
|  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | ||||||
|  |  * Lesser General Public License for more details. | ||||||
|  |  * | ||||||
|  |  * You should have received a copy of the GNU Lesser General Public | ||||||
|  |  * License along with Libav; if not, write to the Free Software | ||||||
|  |  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | #ifndef AVFILTER_BUFFERSRC_H | ||||||
|  | #define AVFILTER_BUFFERSRC_H | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * @file | ||||||
|  |  * Memory buffer source API. | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | #include "avfilter.h" | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Add a buffer to the filtergraph s. | ||||||
|  |  * | ||||||
|  |  * @param buf buffer containing frame data to be passed down the filtergraph. | ||||||
|  |  * This function will take ownership of buf, the user must not free it. | ||||||
|  |  */ | ||||||
|  | int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf); | ||||||
|  |  | ||||||
|  | #endif /* AVFILTER_BUFFERSRC_H */ | ||||||
| @@ -24,13 +24,12 @@ | |||||||
|  */ |  */ | ||||||
|  |  | ||||||
| #include "avfilter.h" | #include "avfilter.h" | ||||||
|  | #include "buffersrc.h" | ||||||
| #include "vsrc_buffer.h" | #include "vsrc_buffer.h" | ||||||
| #include "libavutil/imgutils.h" | #include "libavutil/imgutils.h" | ||||||
|  |  | ||||||
| typedef struct { | typedef struct { | ||||||
|     int64_t           pts; |     AVFilterBufferRef *buf; | ||||||
|     AVFrame           frame; |  | ||||||
|     int               has_frame; |  | ||||||
|     int               h, w; |     int               h, w; | ||||||
|     enum PixelFormat  pix_fmt; |     enum PixelFormat  pix_fmt; | ||||||
|     AVRational        time_base;     ///< time_base to set in the output link |     AVRational        time_base;     ///< time_base to set in the output link | ||||||
| @@ -42,7 +41,7 @@ int av_vsrc_buffer_add_frame(AVFilterContext *buffer_filter, AVFrame *frame, | |||||||
| { | { | ||||||
|     BufferSourceContext *c = buffer_filter->priv; |     BufferSourceContext *c = buffer_filter->priv; | ||||||
|  |  | ||||||
|     if (c->has_frame) { |     if (c->buf) { | ||||||
|         av_log(buffer_filter, AV_LOG_ERROR, |         av_log(buffer_filter, AV_LOG_ERROR, | ||||||
|                "Buffering several frames is not supported. " |                "Buffering several frames is not supported. " | ||||||
|                "Please consume all available frames before adding a new one.\n" |                "Please consume all available frames before adding a new one.\n" | ||||||
| @@ -50,15 +49,31 @@ int av_vsrc_buffer_add_frame(AVFilterContext *buffer_filter, AVFrame *frame, | |||||||
|         //return -1; |         //return -1; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     memcpy(c->frame.data    , frame->data    , sizeof(frame->data)); |     c->buf = avfilter_get_video_buffer(buffer_filter->outputs[0], AV_PERM_WRITE, | ||||||
|     memcpy(c->frame.linesize, frame->linesize, sizeof(frame->linesize)); |                                        c->w, c->h); | ||||||
|     c->frame.interlaced_frame= frame->interlaced_frame; |     av_image_copy(c->buf->data, c->buf->linesize, frame->data, frame->linesize, | ||||||
|     c->frame.top_field_first = frame->top_field_first; |                   c->pix_fmt, c->w, c->h); | ||||||
|     c->frame.key_frame = frame->key_frame; |  | ||||||
|     c->frame.pict_type = frame->pict_type; |     avfilter_copy_frame_props(c->buf, frame); | ||||||
|     c->pts = pts; |     c->buf->pts                    = pts; | ||||||
|     c->pixel_aspect = pixel_aspect; |     c->buf->video->pixel_aspect    = pixel_aspect; | ||||||
|     c->has_frame = 1; |  | ||||||
|  |     return 0; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf) | ||||||
|  | { | ||||||
|  |     BufferSourceContext *c = s->priv; | ||||||
|  |  | ||||||
|  |     if (c->buf) { | ||||||
|  |         av_log(s, AV_LOG_ERROR, | ||||||
|  |                "Buffering several frames is not supported. " | ||||||
|  |                "Please consume all available frames before adding a new one.\n" | ||||||
|  |             ); | ||||||
|  |         return AVERROR(EINVAL); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     c->buf = buf; | ||||||
|  |  | ||||||
|     return 0; |     return 0; | ||||||
| } | } | ||||||
| @@ -113,33 +128,18 @@ static int config_props(AVFilterLink *link) | |||||||
| static int request_frame(AVFilterLink *link) | static int request_frame(AVFilterLink *link) | ||||||
| { | { | ||||||
|     BufferSourceContext *c = link->src->priv; |     BufferSourceContext *c = link->src->priv; | ||||||
|     AVFilterBufferRef *picref; |  | ||||||
|  |  | ||||||
|     if (!c->has_frame) { |     if (!c->buf) { | ||||||
|         av_log(link->src, AV_LOG_ERROR, |         av_log(link->src, AV_LOG_ERROR, | ||||||
|                "request_frame() called with no available frame!\n"); |                "request_frame() called with no available frame!\n"); | ||||||
|         //return -1; |         //return -1; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /* This picture will be needed unmodified later for decoding the next |     avfilter_start_frame(link, avfilter_ref_buffer(c->buf, ~0)); | ||||||
|      * frame */ |  | ||||||
|     picref = avfilter_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE | |  | ||||||
|                                        AV_PERM_REUSE2, |  | ||||||
|                                        link->w, link->h); |  | ||||||
|  |  | ||||||
|     av_image_copy(picref->data, picref->linesize, |  | ||||||
|                   c->frame.data, c->frame.linesize, |  | ||||||
|                   picref->format, link->w, link->h); |  | ||||||
|  |  | ||||||
|     avfilter_copy_frame_props(picref, &c->frame); |  | ||||||
|     picref->pts                    = c->pts; |  | ||||||
|     picref->video->pixel_aspect    = c->pixel_aspect; |  | ||||||
|     avfilter_start_frame(link, avfilter_ref_buffer(picref, ~0)); |  | ||||||
|     avfilter_draw_slice(link, 0, link->h, 1); |     avfilter_draw_slice(link, 0, link->h, 1); | ||||||
|     avfilter_end_frame(link); |     avfilter_end_frame(link); | ||||||
|     avfilter_unref_buffer(picref); |     avfilter_unref_buffer(c->buf); | ||||||
|  |     c->buf = NULL; | ||||||
|     c->has_frame = 0; |  | ||||||
|  |  | ||||||
|     return 0; |     return 0; | ||||||
| } | } | ||||||
| @@ -147,7 +147,7 @@ static int request_frame(AVFilterLink *link) | |||||||
| static int poll_frame(AVFilterLink *link) | static int poll_frame(AVFilterLink *link) | ||||||
| { | { | ||||||
|     BufferSourceContext *c = link->src->priv; |     BufferSourceContext *c = link->src->priv; | ||||||
|     return !!(c->has_frame); |     return !!(c->buf); | ||||||
| } | } | ||||||
|  |  | ||||||
| AVFilter avfilter_vsrc_buffer = { | AVFilter avfilter_vsrc_buffer = { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user