v4l/v4l2 buffer size bug
This code in tvi_v4l.c and tvi_v4l2.c: sysinfo(&si); if (si.totalram<2*1024*1024) { bufsize = 1024*1024; } else { bufsize = si.totalram/2; } doesn't work correctly when si.mem_unit!=1 which will be the case whenever any element of the sysinfo is bigger than 4G. The fix would be to substitute si.totalram*si.mem_unit for si.totalram. mem_unit hasn't always been there. Detecting whether the running kernel supports mem_unit, and doing the right thing when <sys/sysinfo.h> contains the old struct definition without mem_unit, is left as an exercise for the v4l maintainer.
2006/4/25, Alan Curry <pacman@theworld.com>:
This code in tvi_v4l.c and tvi_v4l2.c:
sysinfo(&si); if (si.totalram<2*1024*1024) { bufsize = 1024*1024; } else { bufsize = si.totalram/2; }
doesn't work correctly when si.mem_unit!=1 which will be the case whenever any element of the sysinfo is bigger than 4G. The fix would be to substitute si.totalram*si.mem_unit for si.totalram.
mem_unit hasn't always been there. Detecting whether the running kernel supports mem_unit, and doing the right thing when <sys/sysinfo.h> contains the old struct definition without mem_unit, is left as an exercise for the v4l maintainer.
i don't see such person in docs/tech/maintainers. You'd better do the exercise yourself and send patch before commit. and yes, afair al3x wrote some of v4l(2) code and i'm sure he would comment if there is something wrong in your code ;)
Ivan Kalvachev writes the following:
2006/4/25, Alan Curry <pacman@theworld.com>:
mem_unit hasn't always been there. Detecting whether the running kernel supports mem_unit, and doing the right thing when <sys/sysinfo.h> contains the old struct definition without mem_unit, is left as an exercise for the v4l maintainer.
i don't see such person in docs/tech/maintainers.
* TV input/capture: Jindrich Makovicka
You'd better do the exercise yourself and send patch before commit.
All right then... There was no explicit upper limit on buffersize, which defaults to half of totalram. This patch caps it at 2G. Someone more familiar with real usage scenarios, please comment on this upper bound and propose a better one. I don't have the hardware, so I can't test this, but it compiles. Index: libmpdemux/tvi_v4l.c =================================================================== RCS file: /cvsroot/mplayer/main/libmpdemux/tvi_v4l.c,v retrieving revision 1.78 diff -u -r1.78 tvi_v4l.c --- libmpdemux/tvi_v4l.c 21 Apr 2006 12:47:37 -0000 1.78 +++ libmpdemux/tvi_v4l.c 25 Apr 2006 22:06:34 -0000 @@ -750,12 +750,20 @@ } else { #ifdef HAVE_SYS_SYSINFO_H struct sysinfo si; + unsigned long long totalram; sysinfo(&si); - if (si.totalram<2*1024*1024) { + totalram = si.totalram; +#ifdef HAVE_SYSINFO_MEM_UNIT + if (si.mem_unit) + totalram *= si.mem_unit; + if (totalram > (1ULL<<32) - 1) + totalram = (1ULL<<32) - 1; +#endif + if (totalram<2*1024*1024) { bufsize = 1024*1024; } else { - bufsize = si.totalram/2; + bufsize = totalram/2; } #else bufsize = 16*1024*1024; Index: libmpdemux/tvi_v4l2.c =================================================================== RCS file: /cvsroot/mplayer/main/libmpdemux/tvi_v4l2.c,v retrieving revision 1.39 diff -u -r1.39 tvi_v4l2.c --- libmpdemux/tvi_v4l2.c 22 Apr 2006 05:12:10 -0000 1.39 +++ libmpdemux/tvi_v4l2.c 25 Apr 2006 22:06:40 -0000 @@ -1131,12 +1131,20 @@ } else { #ifdef HAVE_SYS_SYSINFO_H struct sysinfo si; + unsigned long long totalram; sysinfo(&si); - if (si.totalram<2*1024*1024) { + totalram = si.totalram; +#ifdef HAVE_SYSINFO_MEM_UNIT + if (si.mem_unit) + totalram *= si.mem_unit; + if (totalram > (1ULL<<32) - 1) + totalram = (1ULL<<32) - 1; +#endif + if (totalram<2*1024*1024) { bufsize = 1024*1024; } else { - bufsize = si.totalram/2; + bufsize = totalram/2; } #else bufsize = 16*1024*1024; Index: configure =================================================================== RCS file: /cvsroot/mplayer/main/configure,v retrieving revision 1.1178 diff -u -r1.1178 configure --- configure 25 Apr 2006 01:38:02 -0000 1.1178 +++ configure 25 Apr 2006 22:07:11 -0000 @@ -3349,6 +3349,25 @@ fi echores "$_sys_sysinfo" +_sysinfo_mem_unit=no +if test "$_sys_sysinfo" = yes ; then + echocheck "mem_unit field in sysinfo" + cat > $TMPC << EOF +#include <sys/sysinfo.h> +int main(void) { + struct sysinfo s_info; + sysinfo(&s_info); + return s_info.mem_unit>0; +} +EOF + cc_check && _sysinfo_mem_unit=yes + if test "$_sysinfo_mem_unit" = yes ; then + _def_sysinfo_mem_unit='#define HAVE_SYSINFO_MEM_UNIT 1' + else + _def_sysinfo_mem_unit='#undef HAVE_SYSINFO_MEM_UNIT' + fi + echores "$_sysinfo_mem_unit" +fi echocheck "Mac OS X APIs" if test "$_macosx" = auto && darwin ; then @@ -7585,6 +7604,9 @@ /* Define this if your system has the sysinfo header */ $_def_sys_sysinfo +/* Define this if your struct sysinfo contains the mem_unit field */ +$_def_sysinfo_mem_unit + /* Define this if your system has ftello() */ $_def_ftello
participants (2)
-
Alan Curry -
Ivan Kalvachev