diff --git a/libavcodec/opt.c b/libavcodec/opt.c
index cfff0c19d9cb79509eb8692d2b4832a842084163..97b593cb2ff1095a63f6c66b68da4b5a04618529 100644
--- a/libavcodec/opt.c
+++ b/libavcodec/opt.c
@@ -36,7 +36,7 @@ const AVOption *av_find_opt(void *v, const char *name, const char *unit, int mas
     const AVOption *o= c->option;
 
     for(;o && o->name; o++){
-        if(!strcmp(o->name, name) && (!unit || !strcmp(o->unit, unit)) && (o->flags & mask) == flags )
+        if(!strcmp(o->name, name) && (!unit || (o->unit && !strcmp(o->unit, unit))) && (o->flags & mask) == flags )
             return o;
     }
     return NULL;