[MPlayer-dev-eng] [PATCH] fix overflow for libaf frac_t type
Reimar Döffinger
Reimar.Doeffinger at stud.uni-karlsruhe.de
Sun Dec 26 13:09:08 CET 2004
Hi,
the attached patch should fix the bug described at
http://mplayerhq.hu/pipermail/mplayer-users/2004-December/050058.html,
which is caused by an integer overflow. I proposed there to just change
the types to long long (maybe this should be done anyway?).
Here is a patch that I would prefer.
Please comment.
Greetings,
Reimar Döffinger
-------------- next part --------------
Index: libaf/af.c
===================================================================
RCS file: /cvsroot/mplayer/main/libaf/af.c,v
retrieving revision 1.35
diff -u -r1.35 af.c
--- libaf/af.c 23 Dec 2004 02:09:52 -0000 1.35
+++ libaf/af.c 26 Dec 2004 11:01:11 -0000
@@ -510,8 +511,7 @@
register frac_t mul = {1,1};
// Iterate through all filters
do{
- mul.n *= af->mul.n;
- mul.d *= af->mul.d;
+ af_frac_mul(&mul, &af->mul);
af=af->next;
}while(af);
return t * (((len/t)*mul.n + 1)/mul.d);
@@ -528,8 +528,7 @@
register frac_t mul = {1,1};
// Iterate through all filters
do{
- mul.n *= af->mul.n;
- mul.d *= af->mul.d;
+ af_frac_mul(&mul, &af->mul);
af=af->next;
}while(af);
return t * (((len/t) * mul.d - 1)/mul.n);
@@ -553,8 +552,7 @@
register frac_t mul = {1,1};
// Iterate through all filters and calculate total multiplication factor
do{
- mul.n *= af->mul.n;
- mul.d *= af->mul.d;
+ af_frac_mul(&mul, &af->mul);
af=af->next;
}while(af);
// Sanity check
@@ -626,6 +624,45 @@
return (res == AF_OK);
}
+/**
+ * \brief calculate greatest common divisior of a and b.
+ * Extended for negative and 0 values. If a or b are 0 the result is 1.
+ * The sign of the result will be so that it has the same sign as b.
+ */
+int af_gcd(register int a, register int b) {
+ int b_org = b;
+ if (!a || !b)
+ return 1;
+ while (b != 0) {
+ a = b;
+ b = a % b;
+ }
+ if (a * b_org < 0)
+ return -a;
+ return a;
+}
+
+/**
+ * \brief cancel down a fraction f
+ */
+void af_frac_cancel(frac_t *f) {
+ int gcd = af_gcd(f->n, f->d);
+ f->n /= gcd;
+ f->d /= gcd;
+}
+
+/**
+ * \brief multiply out by in and store result in out.
+ * the resulting fraction wil be cancelled down
+ * if in and out were.
+ */
+void af_frac_mul(frac_t *out, const frac_t *in) {
+ int gcd1 = af_gcd(out->n, in->d);
+ int gcd2 = af_gcd(in->n, out->d);
+ out->n = (out->n / gcd1) * (in->n / gcd2);
+ out->d = (out->d / gcd2) * (in->d / gcd1);
+}
+
void af_help (void) {
int i = 0;
af_msg(AF_MSG_INFO, "Available audio filters:\n");
Index: libaf/af.h
===================================================================
RCS file: /cvsroot/mplayer/main/libaf/af.h,v
retrieving revision 1.21
diff -u -r1.21 af.h
--- libaf/af.h 6 Sep 2004 22:27:08 -0000 1.21
+++ libaf/af.h 26 Dec 2004 11:01:11 -0000
@@ -28,6 +28,10 @@
int d; // Denominator
} frac_t;
+int af_gcd(register int a, register int b);
+void af_frac_cancel(frac_t *f);
+void af_frac_mul(frac_t *out, const frac_t *in);
+
// Flags used for defining the behavior of an audio filter
#define AF_FLAGS_REENTRANT 0x00000000
#define AF_FLAGS_NOT_REENTRANT 0x00000001
Index: libaf/af_channels.c
===================================================================
RCS file: /cvsroot/mplayer/main/libaf/af_channels.c,v
retrieving revision 1.12
diff -u -r1.12 af_channels.c
--- libaf/af_channels.c 29 Jul 2004 16:23:16 -0000 1.12
+++ libaf/af_channels.c 26 Dec 2004 11:01:12 -0000
@@ -151,6 +151,7 @@
af->data->bps = ((af_data_t*)arg)->bps;
af->mul.n = af->data->nch;
af->mul.d = ((af_data_t*)arg)->nch;
+ af_frac_cancel(&af->mul);
return check_routes(s,((af_data_t*)arg)->nch,af->data->nch);
case AF_CONTROL_COMMAND_LINE:{
int nch = 0;
Index: libaf/af_format.c
===================================================================
RCS file: /cvsroot/mplayer/main/libaf/af_format.c,v
retrieving revision 1.16
diff -u -r1.16 af_format.c
--- libaf/af_format.c 20 Nov 2004 10:32:52 -0000 1.16
+++ libaf/af_format.c 26 Dec 2004 11:01:13 -0000
@@ -181,6 +181,7 @@
af->data->nch = ((af_data_t*)arg)->nch;
af->mul.n = af->data->bps;
af->mul.d = ((af_data_t*)arg)->bps;
+ af_frac_cancel(&af->mul);
return AF_OK;
}
case AF_CONTROL_COMMAND_LINE:{
Index: libaf/af_lavcresample.c
===================================================================
RCS file: /cvsroot/mplayer/main/libaf/af_lavcresample.c,v
retrieving revision 1.5
diff -u -r1.5 af_lavcresample.c
--- libaf/af_lavcresample.c 22 Dec 2004 00:12:00 -0000 1.5
+++ libaf/af_lavcresample.c 26 Dec 2004 11:01:13 -0000
@@ -54,9 +54,9 @@
if (af->data->nch > CHANS) af->data->nch = CHANS;
af->data->format = AF_FORMAT_SI | AF_FORMAT_NE;
af->data->bps = 2;
- g= ff_gcd(af->data->rate, data->rate);
- af->mul.n = af->data->rate/g;
- af->mul.d = data->rate/g;
+ af->mul.n = af->data->rate;
+ af->mul.d = data->rate;
+ af_frac_cancel(&af->mul);
af->delay = 500*s->filter_length/(double)min(af->data->rate, data->rate);
if(s->avrctx) av_resample_close(s->avrctx);
Index: libaf/af_pan.c
===================================================================
RCS file: /cvsroot/mplayer/main/libaf/af_pan.c,v
retrieving revision 1.4
diff -u -r1.4 af_pan.c
--- libaf/af_pan.c 10 Oct 2004 14:20:42 -0000 1.4
+++ libaf/af_pan.c 26 Dec 2004 11:01:14 -0000
@@ -41,6 +41,7 @@
af->data->bps = 4;
af->mul.n = af->data->nch;
af->mul.d = ((af_data_t*)arg)->nch;
+ af_frac_cancel(&af->mul);
if((af->data->format != ((af_data_t*)arg)->format) ||
(af->data->bps != ((af_data_t*)arg)->bps)){
Index: libaf/af_resample.c
===================================================================
RCS file: /cvsroot/mplayer/main/libaf/af_resample.c,v
retrieving revision 1.21
diff -u -r1.21 af_resample.c
--- libaf/af_resample.c 10 Oct 2004 14:20:42 -0000 1.21
+++ libaf/af_resample.c 26 Dec 2004 11:01:15 -0000
@@ -62,22 +62,6 @@
int setup; // Setup parameters cmdline or through postcreate
} af_resample_t;
-// Euclids algorithm for calculating Greatest Common Divisor GCD(a,b)
-static inline int gcd(register int a, register int b)
-{
- register int r = min(a,b);
- a=max(a,b);
- b=r;
-
- r=a%b;
- while(r!=0){
- a=b;
- b=r;
- r=a%b;
- }
- return b;
-}
-
// Fast linear interpolation resample with modest audio quality
static int linint(af_data_t* c,af_data_t* l, af_resample_t* s)
{
@@ -203,11 +187,12 @@
s->step);
af->mul.n = af->data->rate;
af->mul.d = n->rate;
+ af_frac_cancel(&af->mul);
return rv;
}
// Calculate up and down sampling factors
- d=gcd(af->data->rate,n->rate);
+ d=af_gcd(af->data->rate,n->rate);
// If sloppy resampling is enabled limit the upsampling factor
if(((s->setup & FREQ_MASK) == FREQ_SLOPPY) && (af->data->rate/d > 5000)){
@@ -215,7 +200,7 @@
int dn=n->rate/2;
int m=2;
while(af->data->rate/(d*m) > 5000){
- d=gcd(up,dn);
+ d=af_gcd(up,dn);
up/=2; dn/=2; m*=2;
}
d*=m;
More information about the MPlayer-dev-eng
mailing list