Index: motion.c
===================================================================
--- motion.c	(revisión: 339)
+++ motion.c	(copia de trabajo)
@@ -1906,6 +1906,8 @@
 	threads_running--;
 	pthread_mutex_unlock(&global_lock);
 
+	if (!cnt->restart)
+		cnt->watchdog=WATCHDOG_OFF;
 	cnt->running = 0;
 	cnt->finish = 0;
 
@@ -2342,8 +2344,13 @@
 				if (cnt_list[i]->running || cnt_list[i]->restart)
 					motion_threads_running++;
 			}
-			if ( (motion_threads_running == 0 ) && finish )
+			if ( ((motion_threads_running == 0 ) && finish ) || 
+			     ((motion_threads_running == 0 ) && (threads_running == 0)) ){
+			     if (debug_level >= CAMERA_DEBUG)
+				motion_log(LOG_INFO, 0, "DEBUG-1 threads_running %d motion_threads_running %d , finish %d", 
+				                         threads_running, motion_threads_running, finish);			     
 				break;
+			}	
 
 			for (i = (cnt_list[1] != NULL ? 1 : 0); cnt_list[i]; i++) {
 				/* Check if threads wants to be restarted */
@@ -2371,6 +2378,9 @@
 					}
 				}
 			}
+			if (debug_level >= CAMERA_DEBUG)
+				motion_log(LOG_INFO, 0, "DEBUG-2 threads_running %d motion_threads_running %d finish %d", 
+				                         threads_running, motion_threads_running, finish);
 		}
 		/* Reset end main loop flag */
 		finish = 0;
Index: event.c
===================================================================
--- event.c	(revisión: 339)
+++ event.c	(copia de trabajo)
@@ -502,7 +502,10 @@
 		u = img+width*height;
 	
 	v = u+(width*height)/4;
-	ffmpeg_put_other_image(cnt->ffmpeg_timelapse, y, u, v);
+	if (ffmpeg_put_other_image(cnt->ffmpeg_timelapse, y, u, v) == -1){
+		cnt->finish = 1;
+		cnt->restart = 0;
+	}	
 	
 }
 
@@ -523,11 +526,17 @@
 			u = y + (width * height);
 		
 		v = u + (width * height) / 4;
-		ffmpeg_put_other_image(cnt->ffmpeg_new, y, u, v);
+		if (ffmpeg_put_other_image(cnt->ffmpeg_new, y, u, v) == -1){
+			cnt->finish = 1;
+			cnt->restart = 0;
+		}	
 	}
 	
 	if (cnt->ffmpeg_motion) {
-		ffmpeg_put_image(cnt->ffmpeg_motion);
+		if (ffmpeg_put_image(cnt->ffmpeg_motion) == -1){
+			cnt->finish = 1;
+			cnt->restart = 0;
+		}	
 	}
 }
 
Index: ffmpeg.c
===================================================================
--- ffmpeg.c	(revisión: 339)
+++ ffmpeg.c	(copia de trabajo)
@@ -61,7 +61,7 @@
 #define APPEND_PROTO "appfile"
 
 /* Some forward-declarations. */
-void ffmpeg_put_frame(struct ffmpeg *, AVFrame *);
+int ffmpeg_put_frame(struct ffmpeg *, AVFrame *);
 void ffmpeg_cleanups(struct ffmpeg *);
 AVFrame *ffmpeg_prepare_frame(struct ffmpeg *, unsigned char *, 
                               unsigned char *, unsigned char *);
@@ -592,29 +592,34 @@
 }
 
 /* Puts the image pointed to by ffmpeg->picture. */
-void ffmpeg_put_image(struct ffmpeg *ffmpeg) 
+int ffmpeg_put_image(struct ffmpeg *ffmpeg) 
 {
-	ffmpeg_put_frame(ffmpeg, ffmpeg->picture);
+	return ffmpeg_put_frame(ffmpeg, ffmpeg->picture);
 }
 
 /* Puts an arbitrary picture defined by y, u and v. */
-void ffmpeg_put_other_image(struct ffmpeg *ffmpeg, unsigned char *y,
+int ffmpeg_put_other_image(struct ffmpeg *ffmpeg, unsigned char *y,
                             unsigned char *u, unsigned char *v)
 {
 	AVFrame *picture;
+	int ret = 0;
+
 	/* allocate the encoded raw picture */
 	picture = ffmpeg_prepare_frame(ffmpeg, y, u, v);
 
 	if (picture) {
-		ffmpeg_put_frame(ffmpeg, picture);
-		av_free(picture);
+		ret = ffmpeg_put_frame(ffmpeg, picture);
+		if (!ret)
+			av_free(picture);
 	}
+
+	return ret;
 }
 
 /* Encodes and writes a video frame using the av_write_frame API. This is
  * a helper function for ffmpeg_put_image and ffmpeg_put_other_image. 
  */
-void ffmpeg_put_frame(struct ffmpeg *ffmpeg, AVFrame *pic)
+int ffmpeg_put_frame(struct ffmpeg *ffmpeg, AVFrame *pic)
 {
 	int out_size, ret;
 #ifdef FFMPEG_AVWRITEFRAME_NEWAPI
@@ -664,8 +669,11 @@
 	
 	if (ret != 0) {
 		motion_log(LOG_ERR, 1, "Error while writing video frame");
-		return;
+		ffmpeg_cleanups(ffmpeg);
+		return (-1);
 	}
+
+	return ret; 
 }
 
 /* Allocates and prepares a picture frame by setting up the U, Y and V pointers in
Index: ffmpeg.h
===================================================================
--- ffmpeg.h	(revisión: 339)
+++ ffmpeg.h	(copia de trabajo)
@@ -69,10 +69,10 @@
 	);
 
 /* Puts the image pointed to by the picture member of struct ffmpeg. */
-void ffmpeg_put_image(struct ffmpeg *);
+int ffmpeg_put_image(struct ffmpeg *);
 
 /* Puts the image defined by u, y and v (YUV420 format). */
-void ffmpeg_put_other_image(
+int ffmpeg_put_other_image(
 	struct ffmpeg *ffmpeg, 
 	unsigned char *y, 
 	unsigned char *u, 
Index: picture.c
===================================================================
--- picture.c	(revisión: 339)
+++ picture.c	(copia de trabajo)
@@ -131,8 +131,8 @@
 	
 	jpeg_start_compress (&cinfo, TRUE);
 
-	for (j=0; j<height; j+=16) {
-		for (i=0; i<16; i++) {
+	for (j = 0; j < height; j += 16) {
+		for (i = 0; i < 16; i++) {
 			y[i] = input_image + width*(i+j);
 			if (i%2 == 0) {
 				cb[i/2] = input_image + width*height + width/2*((i+j)/2);
@@ -183,7 +183,7 @@
 
 	row_ptr[0] = input_image;
 	
-	for (y=0; y<height; y++) {
+	for (y = 0; y < height; y++) {
 		jpeg_write_scanlines(&cjpeg, row_ptr, 1);
 		row_ptr[0] += width;
 	}
@@ -243,8 +243,8 @@
 	jpeg_stdio_dest(&cinfo, fp);  	  // data written to file
 	jpeg_start_compress(&cinfo, TRUE);
 
-	for (j=0;j<height;j+=16) {
-		for (i=0;i<16;i++) {
+	for (j = 0; j < height; j += 16) {
+		for (i = 0; i < 16; i++) {
 			y[i] = image + width*(i+j);
 			if (i%2 == 0) {
 				cb[i/2] = image + width*height + width/2*((i+j)/2);
@@ -292,9 +292,9 @@
 	jpeg_start_compress(&cjpeg, TRUE);
 
 	row_ptr[0]=image;
-	for (y=0; y<height; y++) {
+	for (y = 0; y < height; y++) {
 		jpeg_write_scanlines(&cjpeg, row_ptr, 1);
-		row_ptr[0]+=width;
+		row_ptr[0] += width;
 	}
 	jpeg_finish_compress(&cjpeg);
 	jpeg_destroy_compress(&cjpeg);
@@ -313,9 +313,9 @@
 static void put_ppm_bgr24_file(FILE *picture, unsigned char *image, int width, int height)
 {
 	int x, y;
-	unsigned char *l=image;
-	unsigned char *u=image+width*height;
-	unsigned char *v=u+(width*height)/4;
+	unsigned char *l = image;
+	unsigned char *u = image+width*height;
+	unsigned char *v = u+(width*height)/4;
 	int r, g, b;
 	int warningkiller;
 	unsigned char rgb[3];
@@ -327,43 +327,43 @@
 	fprintf(picture, "P6\n");
 	fprintf(picture, "%d %d\n", width, height);
 	fprintf(picture, "%d\n", 255);
-	for (y=0; y<height; y++) {
+	for (y = 0; y < height; y++) {
 		
-		for (x=0; x<width; x++) {
+		for (x = 0; x < width; x++) {
 			r = 76283*(((int)*l)-16)+104595*(((int)*u)-128);
 			g = 76283*(((int)*l)-16)- 53281*(((int)*u)-128)-25625*(((int)*v)-128);
 			b = 76283*(((int)*l)-16)+132252*(((int)*v)-128);
 			r = r>>16;
 			g = g>>16;
 			b = b>>16;
-			if (r<0)
-				r=0;
-			else if (r>255)
-				r=255;
-			if (g<0)
-				g=0;
-			else if (g>255)
-				g=255;
-			if (b<0)
-				b=0;
-			else if (b>255)
-				b=255;
+			if (r < 0)
+				r = 0;
+			else if (r > 255)
+				r = 255;
+			if (g < 0)
+				g = 0;
+			else if (g > 255)
+				g = 255;
+			if (b < 0)
+				b = 0;
+			else if (b > 255)
+				b = 255;
 
-			rgb[0]=b;
-			rgb[1]=g;
-			rgb[2]=r;
+			rgb[0] = b;
+			rgb[1] = g;
+			rgb[2] = r;
 
 			l++;
-			if (x&1) {
+			if (x & 1) {
 				u++;
 				v++;
 			}
 			/* ppm is rgb not bgr */
-			warningkiller=fwrite(rgb, 1, 3, picture);
+			warningkiller = fwrite(rgb, 1, 3, picture);
 		}
-		if (y&1) {
-			u-=width/2;
-			v-=width/2;
+		if (y & 1) {
+			u -= width/2;
+			v -= width/2;
 		}
 	}
 }
@@ -384,7 +384,7 @@
 	/* set V to 255 to make smartmask appear red */
 	out_v = out + v;
 	out_u = out + i;
-	for ( i = 0; i < height; i += 2){
+	for (i = 0; i < height; i += 2){
 		line = i * width;
 		for (x = 0; x < width; x += 2){
 			if (smartmask[line + x] == 0 ||
@@ -411,18 +411,18 @@
 void overlay_fixed_mask(struct context *cnt, unsigned char *out)
 {
 	int i;
-	struct images *imgs=&cnt->imgs;
-	unsigned char *motion_img=imgs->out;
-	unsigned char *mask=imgs->mask;
+	struct images *imgs = &cnt->imgs;
+	unsigned char *motion_img = imgs->out;
+	unsigned char *mask = imgs->mask;
 	int pixel;
 	
 	/* set y to mask + motion-pixel to keep motion pixels visible on grey background*/
-	for (i=0; i<imgs->motionsize; i++){
-		pixel=255-mask[i]+motion_img[i];
-		if (pixel>255)
-			*out=255;
+	for (i = 0; i < imgs->motionsize; i++){
+		pixel = 255-mask[i]+motion_img[i];
+		if (pixel > 255)
+			*out = 255;
 		else
-			*out=pixel;
+			*out = pixel;
 		out++;
 	}
 }
@@ -431,37 +431,37 @@
 void overlay_largest_label(struct context *cnt, unsigned char *out)
 {
 	int i, x, v, width, height, line;
-	struct images *imgs=&cnt->imgs;
-	int *labels=imgs->labels;
+	struct images *imgs = &cnt->imgs;
+	int *labels = imgs->labels;
 	unsigned char *out_y, *out_u, *out_v;
 	
-	i=imgs->motionsize;
-	v=i+((imgs->motionsize)/4);
-	width=imgs->width;
-	height=imgs->height;
+	i = imgs->motionsize;
+	v = i+((imgs->motionsize)/4);
+	width = imgs->width;
+	height = imgs->height;
 
 	/* set U to 255 to make label appear blue */
-	out_u=out+i;
-	out_v=out+v;
-	for ( i=0; i<height; i+=2){
-		line=i*width;
-		for (x=0; x<width; x+=2){
+	out_u = out+i;
+	out_v = out+v;
+	for (i = 0; i < height; i += 2){
+		line = i*width;
+		for (x = 0; x < width; x += 2){
 			if (labels[line+x] & 32768 ||
 				labels[line+x+1] & 32768 ||
 				labels[line+width+x] & 32768 ||
 				labels[line+width+x+1] & 32768) {
-					*out_u=255;
-					*out_v=128;
+					*out_u = 255;
+					*out_v = 128;
 			}
 			out_u++;
 			out_v++;
 		}
 	}
-	out_y=out;
+	out_y = out;
 	/* set intensity for coloured label to have better visibility */
-	for (i=0; i<imgs->motionsize; i++) {
+	for (i = 0; i < imgs->motionsize; i++) {
 		if (*labels++ & 32768)
-			*out_y=0;
+			*out_y = 0;
 		out_y++;
 	}
 }
@@ -515,7 +515,6 @@
 	FILE *picture;
 
 	picture = myfopen(file, "w");
-	
 	if (!picture) {
 		/* Report to syslog - suggest solution if the problem is access rights to target dir */
 		if (errno ==  EACCES) {
@@ -523,6 +522,7 @@
 			           "Can't write picture to file %s - check access rights to target directory", file);
 			motion_log(LOG_ERR, 1, "Thread is going to finish due to this fatal error");
 			cnt->finish = 1;
+			cnt->restart = 0;
 			return;
 		} else {
 			/* If target dir is temporarily unavailable we may survive */
@@ -562,7 +562,7 @@
 			return NULL;
 
 	/* check size */
-	if (sscanf(line, "%d %d", &x, &y)!=2) {
+	if (sscanf(line, "%d %d", &x, &y) != 2) {
 		motion_log(LOG_ERR, 1, "Failed reading size in pgm file");
 		return NULL;
 	}
@@ -578,7 +578,7 @@
 		if (!fgets(line, 255, picture))
 			return NULL;
 	
-	if (sscanf(line, "%d", &maxval)!=1) {
+	if (sscanf(line, "%d", &maxval) != 1) {
 		motion_log(LOG_ERR, 1, "Failed reading maximum value in pgm file");
 		return NULL;
 	}
@@ -587,11 +587,11 @@
 	
 	image = mymalloc(width * height);
 	
-	for (y=0; y<height; y++) {
+	for (y = 0; y < height; y++) {
 		if ((int)fread(&image[y * width], 1, width, picture) != width)
 			motion_log(LOG_ERR, 1, "Failed reading image data from pgm file");
 		
-		for (x=0; x<width; x++) {
+		for (x = 0; x < width; x++) {
 			image[y * width + x] = (int)image[y * width + x] * 255 / maxval;
 		}
 	}	
@@ -607,7 +607,7 @@
 {
 	FILE *picture;
 
-	picture=myfopen(file, "w");
+	picture = myfopen(file, "w");
 	if (!picture) {
 		/* Report to syslog - suggest solution if the problem is access rights to target dir */
 		if (errno ==  EACCES) {
@@ -658,7 +658,7 @@
 
 #ifdef HAVE_FFMPEG
 		/* Use filename of movie i.o. jpeg_filename when set to 'preview' */
-		use_jpegpath=strcmp(cnt->conf.jpegpath, "preview");
+		use_jpegpath = strcmp(cnt->conf.jpegpath, "preview");
 	
 		if (cnt->ffmpeg_new && !use_jpegpath){
 			/* Replace avi/mpg with jpg/ppm and keep the rest of the filename */
