Hi,
On Jan 26, 2008 5:59 PM, Michael Niedermayer <[EMAIL PROTECTED]> wrote:
> On Sat, Jan 26, 2008 at 04:39:58PM +0100, Víctor Paesa wrote:
> > Hi,
> >
> > Here is attached a source filter to read video files.
> >
> > Could you please review it?
> [...]
> i think unconditionally adding a dependancy on libavformat is a little extreem
I made it dependant on a --enable-avfilter-lavf flag in configure.
> [...]
> > +typedef struct {
> > + // Filter parameters
> > + double seek_point;
>
> no floats please, floats make regression testing harder
Changed to in64_t
> [...]
> > + if (av_open_input_file(&mv->pFormatCtx, mv->file_name, file_iformat,
> > 0, NULL) != 0) {
> > + av_log(NULL, AV_LOG_ERROR,
> > + "movie_init() Failed to av_open_input_file '%s'\n",
> > mv->file_name);
>
> no av_log(NULL
> please find some context!
Found some :)
[...]
> the {} placement is inconsistant
Hopefully now is not.
> [...]
> > + if(pCodec == NULL) {
>
> if(!pCodec)
Done.
> [...]
> > + // Hack to correct the wrong frame rates generated by some codecs
> > + if (mv->pCodecCtx->time_base.den>1000 &&
> > mv->pCodecCtx->time_base.num==1)
> > + mv->pCodecCtx->time_base.num=1000;
>
> sorry this is not ok
> you MUST NOT ever write into any time_base field for demuxing and decoding
Removed.
> [...]
> > + mv->pic->pts = packet.pts * AV_TIME_BASE *
> > + av_q2d(mv->pCodecCtx->time_base);
>
> its the timebase of the AVStream you must use!
Oops, yes, thanks.
I also fixed some cosmetics, and updated the copyright year.
Here is attached the next revision:
Regards,
Víctor
Index: diffs/00_build.diff
===================================================================
--- diffs/00_build.diff (revision 1843)
+++ diffs/00_build.diff (working copy)
@@ -2,31 +2,34 @@
===================================================================
--- configure (revision 11629)
+++ configure (working copy)
-@@ -72,6 +72,7 @@
+@@ -72,6 +72,8 @@
echo " and ffmpeg will be unredistributable [default=no]"
echo " --enable-pp enable GPLed postprocessing support [default=no]"
echo " --enable-swscaler software scaler support [default=no]"
+ echo " --enable-avfilter video filter support (replaces vhook) [default=no]"
++ echo " --enable-avfilter-lavf video filters dependant on avformat [default=no]"
echo " --enable-beosthreads use BeOS threads [default=no]"
echo " --enable-os2threads use OS/2 threads [default=no]"
echo " --enable-pthreads use pthreads [default=no]"
-@@ -624,6 +625,7 @@
+@@ -624,6 +626,8 @@
CONFIG_LIST="
$COMPONENT_LIST
+ avfilter
++ avfilter_lavf
avisynth
beos_netserver
ffmpeg
-@@ -1890,6 +1892,7 @@
+@@ -1890,6 +1894,8 @@
echo "shared ${shared-no}"
echo "postprocessing support ${pp-no}"
echo "software scaler enabled ${swscaler-no}"
+echo "new filter support ${avfilter-no}"
++echo "filters using lavformat ${avfilter-lavf-no}"
echo "video hooking ${vhook-no}"
if enabled vhook; then
echo "Imlib2 support ${imlib2-no}"
-@@ -2087,6 +2090,7 @@
+@@ -2087,6 +2093,7 @@
libavcodec/sh4 \
libavcodec/sparc \
libavdevice \
@@ -34,7 +37,7 @@
libavformat \
libavutil \
libpostproc \
-@@ -2101,6 +2105,7 @@
+@@ -2101,6 +2108,7 @@
doc/texi2pod.pl \
libavcodec/Makefile \
libavdevice/Makefile \
@@ -42,7 +45,7 @@
libavformat/Makefile \
libavutil/Makefile \
libpostproc/Makefile \
-@@ -2189,3 +2194,8 @@
+@@ -2189,3 +2197,8 @@
apply libswscale.pc sed s/^Libs:.*$/Libs:/
apply libswscale-uninstalled.pc sed s/^Libs:.*$/Libs:/
fi
Index: Makefile
===================================================================
--- Makefile (revision 1842)
+++ Makefile (working copy)
@@ -1,6 +1,6 @@
include ../config.mak
-CFLAGS+=-I$(SRC_PATH)/libavcodec -I$(SRC_PATH)/libswscale
+CFLAGS+=-I$(SRC_PATH)/libavformat -I$(SRC_PATH)/libavcodec -I$(SRC_PATH)/libswscale
OBJS = avfilter.o \
avfiltergraph.o \
@@ -25,12 +25,20 @@
vf_vflip.o \
avfiltergraphfile.o \
+ifeq ($(CONFIG_AVFILTER_LAVF),yes)
+OBJS-yes += vsrc_movie.o
+endif
+
HEADERS = avfilter.h
EXTRALIBS := -L$(BUILD_ROOT)/libavcodec -lavcodec$(BUILDSUF) \
-L$(BUILD_ROOT)/libswscale -lswscale$(BUILDSUF) \
-L$(BUILD_ROOT)/libavutil -lavutil$(BUILDSUF) $(EXTRALIBS)
+ifeq ($(CONFIG_AVFILTER_LAVF),yes)
+EXTRALIBS := -L$(BUILD_ROOT)/libavformat -lavformat$(BUILDSUF) $(EXTRALIBS)
+endif
+
NAME=avfilter
LIBVERSION=$(LAVFILTERVERSION)
LIBMAJOR=$(LAVFILTERMAJOR)
Index: allfilters.h
===================================================================
--- allfilters.h (revision 1842)
+++ allfilters.h (working copy)
@@ -39,3 +39,6 @@
extern AVFilter avfilter_vf_split;
extern AVFilter avfilter_vf_transpose;
extern AVFilter avfilter_vf_vflip;
+#ifdef CONFIG_AVFILTER_LAVF
+extern AVFilter avfilter_vsrc_movie;
+#endif //CONFIG_AVFILTER_LAVF
Index: avfilter.c
===================================================================
--- avfilter.c (revision 1842)
+++ avfilter.c (working copy)
@@ -308,6 +308,9 @@
avfilter_register(&avfilter_vf_split);
avfilter_register(&avfilter_vf_transpose);
avfilter_register(&avfilter_vf_vflip);
+#ifdef CONFIG_AVFILTER_LAVF
+ avfilter_register(&avfilter_vsrc_movie);
+#endif //CONFIG_AVFILTER_LAVF
}
void avfilter_uninit(void)
Index: vsrc_movie.c
===================================================================
--- vsrc_movie.c (revision 0)
+++ vsrc_movie.c (revision 0)
@@ -0,0 +1,303 @@
+/*
+ * Movie file video source filter
+ * Copyright (c) 2008 Victor Paesa
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg 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.
+ *
+ * FFmpeg 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 FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+ /*
+ # One usage example follows, usable too as test scenario.
+ #TODO Eventually move it into FFmpeg docs.
+
+;----------------------------movie.desc------------------------------
+;
+; Parameters of movie filter are
+; seekpoint in microseconds : string format : string filename
+;
+; We can overlay a second movie on top of a main one
+;
+; input -----------> deltapts0 --> overlay --> output
+; ^
+; movie --> scale--> deltapts1 ------|
+;
+
+[filters]
+movie=movie=3200000:avi:input.avi
+
+; We make both the main movie and the overlaid one start at the same time
+deltapts0=setpts=PTS-STARTPTS
+deltapts1=setpts=PTS-STARTPTS
+
+scale=scale=180:144
+overlay=overlay=16:16
+
+[links]
+deltapts0:0=overlay:0
+
+movie:0=scale:0
+scale:0=deltapts1:0
+deltapts1:0=overlay:1
+
+[inputs]
+default=deltapts0:0
+
+[outputs]
+default=overlay:0
+;----------------------------movie.desc------------------------------
+
+# Then launch it from command line
+ffmpeg -i input.avi -vfilters graph_file=movie.desc output.avi
+
+ */
+
+#include "avformat.h"
+#include "avfilter.h"
+
+typedef struct {
+ // Filter parameters
+ int64_t seek_point; //< seekpoint in microseconds
+ char format_name[16];
+ char file_name[255];
+ // Needed to load movies
+ AVFormatContext *pFormatCtx;
+ int videoStream;
+ AVCodecContext *pCodecCtx;
+ int is_done;
+ AVFrame *pFrame;
+
+ int w, h;
+ AVFilterPicRef *pic;
+} MovieContext;
+
+int movie_init(AVFilterContext *ctx)
+{
+ AVInputFormat *file_iformat = NULL;
+ int i;
+ AVCodec *pCodec;
+ int64_t timestamp;
+ MovieContext *mv = ctx->priv;
+
+ /* av_log(ctx, AV_LOG_INFO, "movie_init() seek:%lld format:%s file:%s\n",
+ mv->seek_point, mv->format_name, mv->file_name); */
+ av_register_all();
+ // Try to find the movie format (container)
+ if (*mv->format_name)
+ file_iformat = av_find_input_format(mv->format_name);
+ else
+ file_iformat = NULL;
+ mv->pFormatCtx = NULL;
+ if (av_open_input_file(&mv->pFormatCtx, mv->file_name, file_iformat, 0, NULL) != 0) {
+ av_log(ctx, AV_LOG_ERROR,
+ "movie_init() Failed to av_open_input_file '%s'\n", mv->file_name);
+ return -1;
+ }
+ if(av_find_stream_info(mv->pFormatCtx)<0) {
+ av_log(ctx, AV_LOG_ERROR, "movie_init() Failed to find stream info\n");
+ return -1;
+ }
+
+ // if seeking requested, we execute it
+ if (mv->seek_point > 0) {
+ timestamp = mv->seek_point;
+ // add the stream start time, should it exist
+ if (mv->pFormatCtx->start_time != AV_NOPTS_VALUE)
+ timestamp += mv->pFormatCtx->start_time;
+ if (av_seek_frame(mv->pFormatCtx, -1, timestamp, AVSEEK_FLAG_BACKWARD) < 0) {
+ av_log(ctx, AV_LOG_ERROR, "%s: could not seek to position %lld\n",
+ mv->file_name, timestamp);
+ }
+ }
+
+ // To make things nice and easy, we simply use the first video stream we find
+ mv->videoStream = -1;
+ for(i = 0; i < mv->pFormatCtx->nb_streams; i++)
+ if(mv->pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO) {
+ mv->videoStream = i;
+ break;
+ }
+ if(mv->videoStream == -1) {
+ av_log(ctx, AV_LOG_ERROR, "movie_init() No video stream found\n");
+ return -1;
+ }
+ // Get a pointer to the codec context for the video stream
+ mv->pCodecCtx = mv->pFormatCtx->streams[mv->videoStream]->codec;
+
+ /*
+ * So now we've got a pointer to the so-called codec context for our video
+ * stream, but we still have to find the actual codec and open it.
+ */
+ // Find the decoder for the video stream
+ pCodec = avcodec_find_decoder(mv->pCodecCtx->codec_id);
+ if(!pCodec) {
+ av_log(ctx, AV_LOG_ERROR, "movie_init() Failed to find any codec\n");
+ return -1;
+ }
+
+ // Open codec
+ if(avcodec_open(mv->pCodecCtx, pCodec)<0) {
+ av_log(ctx, AV_LOG_ERROR, "movie_init() Failed to open codec\n");
+ return -1;
+ }
+
+ // Allocate a video frame to store the decoded images in.
+ if(! (mv->pFrame = avcodec_alloc_frame()) ) {
+ av_log(ctx, AV_LOG_ERROR, "movie_init() Failed to alloc frame\n");
+ return -1;
+ }
+
+ mv->w = mv->pCodecCtx->width;
+ mv->h = mv->pCodecCtx->height;
+
+ return 0;
+}
+
+static int init(AVFilterContext *ctx, const char *args, void *opaque)
+{
+ MovieContext *mv = ctx->priv;
+ int num_fields;
+
+ if(args) {
+ num_fields = sscanf(args, "%lld:%15[^:]:%255s",
+ &mv->seek_point, mv->format_name, mv->file_name);
+ if (3 == num_fields)
+ /* av_log(ctx, AV_LOG_INFO,
+ "init() args:'%s'\n\tseek:%lld format:%s name:%s\n",
+ args, mv->seek_point, mv->format_name, mv->file_name); */
+ // sanity check parms
+ if (mv->seek_point >= 0 && *mv->file_name)
+ return movie_init(ctx);
+ }
+ else
+ av_log(ctx, AV_LOG_ERROR, "init() expected 3 arguments:'%s'\n", args);
+ return -1;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+ MovieContext *mv = ctx->priv;
+
+ avfilter_set_common_formats(ctx,
+ avfilter_make_format_list(1, mv->pCodecCtx->pix_fmt));
+ return 0;
+}
+
+static int config_props(AVFilterLink *link)
+{
+ MovieContext *mv = link->src->priv;
+
+ link->w = mv->w;
+ link->h = mv->h;
+
+ return 0;
+}
+
+int movie_get_frame(AVFilterLink *link)
+{
+ AVPacket packet;
+ int frameFinished;
+
+ MovieContext *mv = link->src->priv;
+
+ if (mv->is_done == 1) return 0;
+
+ if(!mv->pic)
+ mv->pic = avfilter_get_video_buffer(link, AV_PERM_WRITE );
+ //av_log(link->src, AV_LOG_INFO, "movie_get_frame() w:%d h:%d\n", mv->w, mv->h);
+
+ // Get frame
+ while(av_read_frame(mv->pFormatCtx, &packet)>=0)
+ {
+ // Is this a packet from the video stream?
+ if(packet.stream_index == mv->videoStream)
+ {
+ // Decode video frame
+ avcodec_decode_video(mv->pCodecCtx, mv->pFrame, &frameFinished,
+ packet.data, packet.size);
+
+ // Did we get a video frame?
+ if(frameFinished)
+ {
+ memcpy(mv->pic->data, mv->pFrame->data,
+ sizeof(mv->pFrame->data));
+ memcpy(mv->pic->linesize, mv->pFrame->linesize,
+ sizeof(mv->pFrame->linesize));
+
+ // Advance in the time line
+ mv->pic->pts = packet.pts * AV_TIME_BASE *
+ av_q2d(mv->pFormatCtx->streams[mv->videoStream]->time_base);
+ /* av_log(link->src, AV_LOG_INFO,
+ "movie_get_frame(%s) packet pts:%lld %lf vfpts:%lld\n",
+ mv->file_name, packet.pts, (double)packet.pts *
+ av_q2d(mv->pFormatCtx->streams[mv->videoStream]->time_base),
+ mv->pic->pts); */
+
+ // We got it. Free the packet since we are returning
+ av_free_packet(&packet);
+
+ return 0;
+ }
+ }
+ // Free the packet that was allocated by av_read_frame
+ av_free_packet(&packet);
+ }
+ // On multi-frame source we should stop the mixing process when
+ // the movie source does not have more frames
+ mv->is_done = 1;
+ return 0;
+}
+
+static int request_frame(AVFilterLink *link)
+{
+ AVFilterPicRef *out;
+ MovieContext *mv = link->src->priv;
+
+ movie_get_frame(link);
+
+ out = avfilter_ref_pic(mv->pic, ~AV_PERM_WRITE);
+ avfilter_start_frame(link, out);
+ avfilter_draw_slice(link, 0, out->h);
+ avfilter_end_frame(link);
+
+ return 0;
+}
+
+static void uninit(AVFilterContext *ctx)
+{
+ MovieContext *mv = ctx->priv;
+
+ if(mv->pic)
+ avfilter_unref_pic(mv->pic);
+}
+
+AVFilter avfilter_vsrc_movie =
+{
+ .name = "movie",
+ .author = "Victor Paesa",
+ .priv_size = sizeof(MovieContext),
+ .query_formats = query_formats,
+
+ .init = init,
+ .uninit = uninit,
+
+ .inputs = (AVFilterPad[]) {{ .name = NULL }},
+ .outputs = (AVFilterPad[]) {{ .name = "default",
+ .type = AV_PAD_VIDEO,
+ .request_frame = request_frame,
+ .config_props = config_props, },
+ { .name = NULL}},
+};
+
_______________________________________________
FFmpeg-soc mailing list
[email protected]
http://lists.mplayerhq.hu/mailman/listinfo/ffmpeg-soc