diff --git a/configure b/configure
index 995161a3a0334b38c482b5974b9c0f31eae9377c..f498357f5b77ba727dfa987727284db5f8f2b96f 100755
--- a/configure
+++ b/configure
@@ -1239,6 +1239,7 @@ HAVE_LIST="
     memalign
     mkstemp
     mmap
+    nanosleep
     netinet_sctp_h
     PeekNamedPipe
     poll_h
@@ -1251,6 +1252,7 @@ HAVE_LIST="
     sdl_video_size
     setmode
     setrlimit
+    Sleep
     sndio_h
     socklen_t
     soundcard_h
@@ -1278,8 +1280,11 @@ HAVE_LIST="
     threads
     trunc
     truncf
+    unistd_h
+    usleep
     vfp_args
     VirtualAlloc
+    windows_h
     winsock2_h
     xform_asm
     xmm_clobbers
@@ -3159,6 +3164,7 @@ check_func  strptime
 check_func  sched_getaffinity
 check_func  sysconf
 check_func  sysctl
+check_func  usleep
 check_func_headers conio.h kbhit
 check_func_headers windows.h PeekNamedPipe
 check_func_headers io.h setmode
@@ -3168,6 +3174,7 @@ check_func_headers windows.h GetProcessAffinityMask
 check_func_headers windows.h GetProcessTimes
 check_func_headers windows.h GetSystemTimeAsFileTime
 check_func_headers windows.h MapViewOfFile
+check_func_headers windows.h Sleep
 check_func_headers windows.h VirtualAlloc
 check_func_headers glob.h glob
 
@@ -3181,8 +3188,10 @@ check_header sys/param.h
 check_header sys/resource.h
 check_header sys/select.h
 check_header termios.h
+check_header unistd.h
 check_header vdpau/vdpau.h
 check_header vdpau/vdpau_x11.h
+check_header windows.h
 check_header X11/extensions/XvMClib.h
 check_header asm/types.h
 
diff --git a/doc/APIchanges b/doc/APIchanges
index b36138ebaeaed5fa56fa5d0364d407ca38ee92c7..0b5ccc92163e341cc11506bbb939655c94061074 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -38,6 +38,9 @@ API changes, most recent first:
 2012-03-26 - a67d9cf - lavfi 2.66.100
   Add avfilter_fill_frame_from_{audio_,}buffer_ref() functions.
 
+2012-06-22 - xxxxxxx - lavu 51.34.0
+  Add av_usleep()
+
 2012-06-20 - ae0a301 - lavu 51.33.0
   Move av_gettime() to libavutil, add libavutil/time.h
 
diff --git a/ffmpeg.c b/ffmpeg.c
index 80f4fe4bebfda8115f5ec1ad2c2436889a0acb73..d7a44a84c36d16498a34208b431a8c9d0a00a83a 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -31,7 +31,9 @@
 #include <errno.h>
 #include <signal.h>
 #include <limits.h>
+#if HAVE_ISATTY
 #include <unistd.h>
+#endif
 #include "libavformat/avformat.h"
 #include "libavdevice/avdevice.h"
 #include "libswscale/swscale.h"
@@ -51,6 +53,7 @@
 #include "libavutil/imgutils.h"
 #include "libavutil/timestamp.h"
 #include "libavutil/bprint.h"
+#include "libavutil/time.h"
 #include "libavformat/os_support.h"
 
 #include "libavformat/ffm.h" // not public API
@@ -2303,7 +2306,7 @@ static void rate_emu_sleep(InputStream *ist)
         int64_t pts = av_rescale(ist->dts, 1000000, AV_TIME_BASE);
         int64_t now = av_gettime() - ist->start;
         if (pts > now)
-            usleep(pts - now);
+            av_usleep(pts - now);
     }
 }
 
@@ -3342,7 +3345,7 @@ static void *input_thread(void *arg)
         ret = av_read_frame(f->ctx, &pkt);
 
         if (ret == AVERROR(EAGAIN)) {
-            usleep(10000);
+            av_usleep(10000);
             ret = 0;
             continue;
         } else if (ret < 0)
@@ -3504,7 +3507,7 @@ static int transcode(void)
             if (no_packet_count) {
                 no_packet_count = 0;
                 memset(no_packet, 0, nb_input_files);
-                usleep(10000);
+                av_usleep(10000);
                 continue;
             }
             av_log(NULL, AV_LOG_VERBOSE, "No more inputs to read from, finishing.\n");
diff --git a/ffplay.c b/ffplay.c
index 55c7825ee3e120b1fd988041722ba7a146509782..753ff91a0819466c5830b32472ac2dc953aa57bc 100644
--- a/ffplay.c
+++ b/ffplay.c
@@ -37,6 +37,7 @@
 #include "libavutil/parseutils.h"
 #include "libavutil/samplefmt.h"
 #include "libavutil/avassert.h"
+#include "libavutil/time.h"
 #include "libavformat/avformat.h"
 #include "libavdevice/avdevice.h"
 #include "libswscale/swscale.h"
@@ -57,7 +58,6 @@
 
 #include "cmdutils.h"
 
-#include <unistd.h>
 #include <assert.h>
 
 const char program_name[] = "ffplay";
@@ -1015,7 +1015,7 @@ static int refresh_thread(void *opaque)
             SDL_PushEvent(&event);
         }
         //FIXME ideally we should wait the correct time but SDLs event passing is so slow it would be silly
-        usleep(is->audio_st && is->show_mode != SHOW_MODE_VIDEO ? rdftspeed*1000 : 5000);
+        av_usleep(is->audio_st && is->show_mode != SHOW_MODE_VIDEO ? rdftspeed*1000 : 5000);
     }
     return 0;
 }
diff --git a/libavcodec/libspeexenc.c b/libavcodec/libspeexenc.c
index b4a1853f6febb9ba77cab0b78d3b0941d9372795..898625c8556c7cb16fdedee827fa486069b971db 100644
--- a/libavcodec/libspeexenc.c
+++ b/libavcodec/libspeexenc.c
@@ -67,6 +67,8 @@
 #include <speex/speex.h>
 #include <speex/speex_header.h>
 #include <speex/speex_stereo.h>
+
+#include "libavutil/audioconvert.h"
 #include "libavutil/opt.h"
 #include "avcodec.h"
 #include "internal.h"
@@ -332,6 +334,10 @@ AVCodec ff_libspeex_encoder = {
     .capabilities   = CODEC_CAP_DELAY,
     .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
                                                      AV_SAMPLE_FMT_NONE },
+    .channel_layouts = (const uint64_t[]){ AV_CH_LAYOUT_MONO,
+                                           AV_CH_LAYOUT_STEREO,
+                                           0 },
+    .supported_samplerates = (const int[]){ 8000, 16000, 32000, 0 },
     .long_name      = NULL_IF_CONFIG_SMALL("libspeex Speex"),
     .priv_class     = &class,
     .defaults       = defaults,
diff --git a/libavcodec/utvideo.c b/libavcodec/utvideo.c
index 59ba7553e6f1f408003d88c95d14d660add1365c..ac4438ddf7e42ec768b294d5d8a5bc87a84b98cd 100644
--- a/libavcodec/utvideo.c
+++ b/libavcodec/utvideo.c
@@ -437,16 +437,17 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
                                plane_start[i], c->frame_pred == PRED_LEFT);
             if (ret)
                 return ret;
-            if (c->frame_pred == PRED_MEDIAN)
+            if (c->frame_pred == PRED_MEDIAN) {
                 if (!c->interlaced) {
-                restore_median(c->pic.data[0] + rgb_order[i], c->planes,
-                               c->pic.linesize[0], avctx->width, avctx->height,
-                               c->slices, 0);
+                    restore_median(c->pic.data[0] + rgb_order[i], c->planes,
+                                   c->pic.linesize[0], avctx->width,
+                                   avctx->height, c->slices, 0);
                 } else {
                     restore_median_il(c->pic.data[0] + rgb_order[i], c->planes,
-                                      c->pic.linesize[0], avctx->width, avctx->height,
-                                      c->slices, 0);
+                                      c->pic.linesize[0], avctx->width,
+                                      avctx->height, c->slices, 0);
                 }
+            }
         }
         restore_rgb_planes(c->pic.data[0], c->planes, c->pic.linesize[0],
                            avctx->width, avctx->height);
diff --git a/libavcodec/x86/rv40dsp_init.c b/libavcodec/x86/rv40dsp_init.c
index 7186492d9845ec17b07403672caad6066c5c8185..41ee6a74c6eec1ac56adedc16ec22ab15de7e5c3 100644
--- a/libavcodec/x86/rv40dsp_init.c
+++ b/libavcodec/x86/rv40dsp_init.c
@@ -26,8 +26,8 @@
  * 3,3 is bugged in the rv40 format and maps to _xy2 version
  */
 
-#include "libavcodec/x86/dsputil_mmx.h"
 #include "libavcodec/rv34dsp.h"
+#include "dsputil_mmx.h"
 
 void ff_put_rv40_chroma_mc8_mmx  (uint8_t *dst, uint8_t *src,
                                   int stride, int h, int x, int y);
diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index efab990f16f9f0ecb15fc21a1d475b52d0cdd43e..143c48e17dc00c05a8faf43d7dad832d53b7b0bc 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -201,6 +201,10 @@
 #include "avio.h"
 #include "libavformat/version.h"
 
+#if FF_API_AV_GETTIME
+#include "libavutil/time.h"
+#endif
+
 struct AVFormatContext;
 
 
@@ -1854,10 +1858,6 @@ void av_dump_format(AVFormatContext *ic,
                     const char *url,
                     int is_output);
 
-#if FF_API_AV_GETTIME
-int64_t av_gettime(void);
-#endif
-
 /**
  * Return in 'buf' the path with '%d' replaced by a number.
  *
diff --git a/libavformat/avio.c b/libavformat/avio.c
index 8b7e495e55b9af6eeaa9477950061cbebf432f0e..9dca8679e3ae2c37d8f8c40baef34ca7e6121105 100644
--- a/libavformat/avio.c
+++ b/libavformat/avio.c
@@ -19,11 +19,10 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include <unistd.h>
-
 #include "libavutil/avstring.h"
 #include "libavutil/dict.h"
 #include "libavutil/opt.h"
+#include "libavutil/time.h"
 #include "os_support.h"
 #include "avformat.h"
 #if CONFIG_NETWORK
@@ -268,7 +267,7 @@ static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int
             if (fast_retries)
                 fast_retries--;
             else
-                usleep(1000);
+                av_usleep(1000);
         } else if (ret < 1)
             return ret < 0 ? ret : len;
         if (ret)
diff --git a/libavformat/hls.c b/libavformat/hls.c
index 4b5cd074227512ec141835a72cafc433284f3fc2..40c417a199c38c069178034b0ff6aee542516006 100644
--- a/libavformat/hls.c
+++ b/libavformat/hls.c
@@ -30,9 +30,9 @@
 #include "libavutil/mathematics.h"
 #include "libavutil/opt.h"
 #include "libavutil/dict.h"
+#include "libavutil/time.h"
 #include "avformat.h"
 #include "internal.h"
-#include <unistd.h>
 #include "avio_internal.h"
 #include "url.h"
 
@@ -407,7 +407,7 @@ reload:
             while (av_gettime() - v->last_load_time < reload_interval) {
                 if (ff_check_interrupt(c->interrupt_callback))
                     return AVERROR_EXIT;
-                usleep(100*1000);
+                av_usleep(100*1000);
             }
             /* Enough time has elapsed since the last reload */
             goto reload;
diff --git a/libavformat/hlsproto.c b/libavformat/hlsproto.c
index 8c25689f91fadfbd75deaf6a91029ef4716ad72d..95bd04787754cf7bc430943484d085bc06e6d4f6 100644
--- a/libavformat/hlsproto.c
+++ b/libavformat/hlsproto.c
@@ -26,11 +26,11 @@
  */
 
 #include "libavutil/avstring.h"
+#include "libavutil/time.h"
 #include "avformat.h"
 #include "internal.h"
 #include "url.h"
 #include "version.h"
-#include <unistd.h>
 
 /*
  * An apple http stream consists of a playlist with media segment files,
@@ -308,7 +308,7 @@ retry:
         while (av_gettime() - s->last_load_time < reload_interval) {
             if (ff_check_interrupt(&h->interrupt_callback))
                 return AVERROR_EXIT;
-            usleep(100*1000);
+            av_usleep(100*1000);
         }
         goto retry;
     }
diff --git a/libavformat/rtmphttp.c b/libavformat/rtmphttp.c
index 5eb5007eb0e1062a1247fc3cf1e7bbc5c9090e85..4f94859c5eef1227487b4d163ddea51bf943c5c9 100644
--- a/libavformat/rtmphttp.c
+++ b/libavformat/rtmphttp.c
@@ -24,11 +24,10 @@
  * RTMP HTTP protocol
  */
 
-#include <unistd.h>
-
 #include "libavutil/avstring.h"
 #include "libavutil/intfloat.h"
 #include "libavutil/opt.h"
+#include "libavutil/time.h"
 #include "internal.h"
 #include "http.h"
 
@@ -126,7 +125,7 @@ static int rtmp_http_read(URLContext *h, uint8_t *buf, int size)
                 if (rt->nb_bytes_read == 0) {
                     /* Wait 50ms before retrying to read a server reply in
                      * order to reduce the number of idle requets. */
-                    usleep(50000);
+                    av_usleep(50000);
                 }
 
                 if ((ret = rtmp_http_write(h, "", 1)) < 0)
diff --git a/libavutil/arm/float_dsp_init_arm.c b/libavutil/arm/float_dsp_init_arm.c
index ab636dbcd2b99bd79a1ffeeeedc156bad02dc4e8..f721344b394971a6c49c7eddfd7b8c47470c5ec0 100644
--- a/libavutil/arm/float_dsp_init_arm.c
+++ b/libavutil/arm/float_dsp_init_arm.c
@@ -18,8 +18,8 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include "libavutil/arm/cpu.h"
 #include "libavutil/float_dsp.h"
+#include "cpu.h"
 #include "float_dsp_arm.h"
 
 void ff_float_dsp_init_arm(AVFloatDSPContext *fdsp)
diff --git a/libavutil/arm/float_dsp_init_vfp.c b/libavutil/arm/float_dsp_init_vfp.c
index dfde3fdb64b72c2f154be83a6bd20b57188ad4c5..7abc3322cf4bf13442984a0b7f6c2ef4772bf9a3 100644
--- a/libavutil/arm/float_dsp_init_vfp.c
+++ b/libavutil/arm/float_dsp_init_vfp.c
@@ -18,8 +18,8 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include "libavutil/arm/cpu.h"
 #include "libavutil/float_dsp.h"
+#include "cpu.h"
 #include "float_dsp_arm.h"
 
 void ff_vector_fmul_vfp(float *dst, const float *src0, const float *src1,
diff --git a/libavutil/avutil.h b/libavutil/avutil.h
index b3f213a2405881a68291a7043e6bd5508a47fde0..0efaa952f42fa0c79fef0df0bbe68b7559e01cb3 100644
--- a/libavutil/avutil.h
+++ b/libavutil/avutil.h
@@ -153,7 +153,7 @@
  */
 
 #define LIBAVUTIL_VERSION_MAJOR 51
-#define LIBAVUTIL_VERSION_MINOR 60
+#define LIBAVUTIL_VERSION_MINOR 61
 #define LIBAVUTIL_VERSION_MICRO 100
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
diff --git a/libavutil/file.c b/libavutil/file.c
index c6228effcd912a055fbff71ddad2da46200618df..d52a2e803f747ed5a81928394eec0cd107ee16bb 100644
--- a/libavutil/file.c
+++ b/libavutil/file.c
@@ -20,7 +20,9 @@
 #include "log.h"
 #include <fcntl.h>
 #include <sys/stat.h>
+#if HAVE_UNISTD_H
 #include <unistd.h>
+#endif
 #if HAVE_MMAP
 #include <sys/mman.h>
 #elif HAVE_MAPVIEWOFFILE
diff --git a/libavutil/log.c b/libavutil/log.c
index f2272fb64843e43ed05d99e00cdf439c2397eca2..4464bd30311530ec97a94a2f72d07419ee83c42d 100644
--- a/libavutil/log.c
+++ b/libavutil/log.c
@@ -24,7 +24,11 @@
  * logging functions
  */
 
+#include "config.h"
+
+#if HAVE_ISATTY
 #include <unistd.h>
+#endif
 #include <stdlib.h>
 #include "avutil.h"
 #include "log.h"
diff --git a/libavutil/time.c b/libavutil/time.c
index 68f431e6951f80f950a84add768dfdf7d9669008..27feb0b7582fdb011b01dab1165d6b04e1db6cac 100644
--- a/libavutil/time.c
+++ b/libavutil/time.c
@@ -22,13 +22,19 @@
 
 #include <stddef.h>
 #include <stdint.h>
+#include <time.h>
 #if HAVE_GETTIMEOFDAY
 #include <sys/time.h>
-#elif HAVE_GETSYSTEMTIMEASFILETIME
+#endif
+#if HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#if HAVE_WINDOWS_H
 #include <windows.h>
 #endif
 
 #include "libavutil/time.h"
+#include "error.h"
 
 int64_t av_gettime(void)
 {
@@ -46,3 +52,19 @@ int64_t av_gettime(void)
     return -1;
 #endif
 }
+
+int av_usleep(unsigned usec)
+{
+#if HAVE_NANOSLEEP
+    struct timespec ts = { usec / 1000000, usec % 1000000 * 1000 };
+    while (nanosleep(&ts, &ts) < 0 && errno == EINTR);
+    return 0;
+#elif HAVE_USLEEP
+    return usleep(usec);
+#elif HAVE_SLEEP
+    Sleep(usec / 1000);
+    return 0;
+#else
+    return AVERROR(ENOSYS);
+#endif
+}
diff --git a/libavutil/time.h b/libavutil/time.h
index 27f4e6734af8f39eb900ad6d716c7ea56d9cd399..90eb436949265d178904467b4591b5dc8fc3889e 100644
--- a/libavutil/time.h
+++ b/libavutil/time.h
@@ -28,4 +28,14 @@
  */
 int64_t av_gettime(void);
 
+/**
+ * Sleep for a period of time.  Although the duration is expressed in
+ * microseconds, the actual delay may be rounded to the precision of the
+ * system timer.
+ *
+ * @param  usec Number of microseconds to sleep.
+ * @return zero on success or (negative) error code.
+ */
+int av_usleep(unsigned usec);
+
 #endif /* AVUTIL_TIME_H */
diff --git a/tools/aviocat.c b/tools/aviocat.c
index f5da526ba90a1242355200c79178e9abd312c0a6..52a96bde2f85d8f580360b151dd2e2a417984b24 100644
--- a/tools/aviocat.c
+++ b/tools/aviocat.c
@@ -20,8 +20,8 @@
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <unistd.h>
 
+#include "libavutil/time.h"
 #include "libavformat/avformat.h"
 
 static int usage(const char *argv0, int ret)
@@ -82,7 +82,7 @@ int main(int argc, char **argv)
         if (bps) {
             avio_flush(output);
             while ((av_gettime() - start_time) * bps / AV_TIME_BASE < stream_pos)
-                usleep(50 * 1000);
+                av_usleep(50 * 1000);
         }
     }
 
diff --git a/tools/pktdumper.c b/tools/pktdumper.c
index 0daa8e0941bc143d365537765bd4e345a2f8ae2f..1711210eb43ae51dde2941b0ea0cffba138186eb 100644
--- a/tools/pktdumper.c
+++ b/tools/pktdumper.c
@@ -25,6 +25,7 @@
 #include <string.h>
 #include <unistd.h>
 
+#include "libavutil/time.h"
 #include "libavformat/avformat.h"
 
 #define PKTFILESUFF "_%08" PRId64 "_%02d_%010" PRId64 "_%06d_%c.bin"
@@ -122,7 +123,7 @@ int main(int argc, char **argv)
     avformat_close_input(&fctx);
 
     while (donotquit)
-        usleep(60 * 1000000);
+        av_usleep(60 * 1000000);
 
     return 0;
 }