$NetBSD$

Free close the descriptor only in case the open was successful
Add some verbose error messages.

--- src/avformat_writer.c.orig	2007-05-11 15:15:40.000000000 +0200
+++ src/avformat_writer.c
@@ -37,12 +37,14 @@ AVFWRITER* avfwriter_init(const char* fn
     p->fmt = guess_format(NULL, fn, NULL);
     if (!p->fmt) 
 	{
+		fprintf (stderr, "Could not guess the output format.\n");
 		avfwriter_release(p);
 		return NULL;
 	}	
 	
 	if (p->fmt->video_codec != CODEC_ID_MPEG4)
 	{
+		fprintf (stderr, "The video codec of the output format has to be MPEG4.\n");
 		avfwriter_release(p);
 		return NULL;
 	}
@@ -50,6 +52,7 @@ AVFWRITER* avfwriter_init(const char* fn
 	p->oc = av_alloc_format_context();
     if (!p->oc) 
 	{
+		fprintf (stderr, "Could not allocate format context structure.\n");
  		avfwriter_release(p);
 		return NULL;
 	}
@@ -58,6 +61,7 @@ AVFWRITER* avfwriter_init(const char* fn
 
     if (url_fopen(&p->oc->pb, fn, URL_WRONLY) < 0) 
 	{
+		perror (fn);
  		avfwriter_release(p);
 		return NULL;
     }
@@ -191,7 +195,11 @@ int avfwriter_release(AVFWRITER* p)
 			av_freep(&p->oc->streams[i]);
 		}
 
-		url_fclose(&p->oc->pb);
+		/* Let's assume that if it has a buffer it has been opened. */
+		if (p->oc->pb.buffer != NULL)
+		{
+			url_fclose(&p->oc->pb);
+		}
 		av_free(p->oc);
 	}
 	
