diff --git a/ffmpeg_videotoolbox.c b/ffmpeg_videotoolbox.c
index 79a8503..6688452 100644
--- a/ffmpeg_videotoolbox.c
+++ b/ffmpeg_videotoolbox.c
@@ -16,41 +16,56 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include <CoreServices/CoreServices.h>
+
+#include "config.h"
 #include "libavcodec/avcodec.h"
-#include "libavcodec/vda.h"
+#if CONFIG_VDA
+#  include "libavcodec/vda.h"
+#endif
+#if CONFIG_VIDEOTOOLBOX
+#  include "libavcodec/videotoolbox.h"
+#endif
 #include "libavutil/imgutils.h"
-
 #include "ffmpeg.h"
 
-typedef struct VDAContext {
+typedef struct VTContext {
     AVFrame *tmp_frame;
-} VDAContext;
+} VTContext;
+
+char *videotoolbox_pixfmt;
 
-static int vda_retrieve_data(AVCodecContext *s, AVFrame *frame)
+static int videotoolbox_retrieve_data(AVCodecContext *s, AVFrame *frame)
 {
     InputStream *ist = s->opaque;
-    VDAContext  *vda = ist->hwaccel_ctx;
+    VTContext  *vt = ist->hwaccel_ctx;
     CVPixelBufferRef pixbuf = (CVPixelBufferRef)frame->data[3];
     OSType pixel_format = CVPixelBufferGetPixelFormatType(pixbuf);
     CVReturn err;
     uint8_t *data[4] = { 0 };
     int linesize[4] = { 0 };
     int planes, ret, i;
+    char codec_str[32];
 
-    av_frame_unref(vda->tmp_frame);
+    av_frame_unref(vt->tmp_frame);
 
     switch (pixel_format) {
-    case kCVPixelFormatType_420YpCbCr8Planar: vda->tmp_frame->format = AV_PIX_FMT_YUV420P; break;
-    case kCVPixelFormatType_422YpCbCr8:       vda->tmp_frame->format = AV_PIX_FMT_UYVY422; break;
+    case kCVPixelFormatType_420YpCbCr8Planar: vt->tmp_frame->format = AV_PIX_FMT_YUV420P; break;
+    case kCVPixelFormatType_422YpCbCr8:       vt->tmp_frame->format = AV_PIX_FMT_UYVY422; break;
+    case kCVPixelFormatType_32BGRA:           vt->tmp_frame->format = AV_PIX_FMT_BGRA; break;
+#ifdef kCFCoreFoundationVersionNumber10_7
+    case kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange: vt->tmp_frame->format = AV_PIX_FMT_NV12; break;
+#endif
     default:
+        av_get_codec_tag_string(codec_str, sizeof(codec_str), s->codec_tag);
         av_log(NULL, AV_LOG_ERROR,
-               "Unsupported pixel format: %u\n", pixel_format);
+               "%s: Unsupported pixel format: %s\n", codec_str, videotoolbox_pixfmt);
         return AVERROR(ENOSYS);
     }
 
-    vda->tmp_frame->width  = frame->width;
-    vda->tmp_frame->height = frame->height;
-    ret = av_frame_get_buffer(vda->tmp_frame, 32);
+    vt->tmp_frame->width  = frame->width;
+    vt->tmp_frame->height = frame->height;
+    ret = av_frame_get_buffer(vt->tmp_frame, 32);
     if (ret < 0)
         return ret;
 
@@ -72,66 +87,101 @@ static int vda_retrieve_data(AVCodecContext *s, AVFrame *frame)
         linesize[0] = CVPixelBufferGetBytesPerRow(pixbuf);
     }
 
-    av_image_copy(vda->tmp_frame->data, vda->tmp_frame->linesize,
-                  (const uint8_t **)data, linesize, vda->tmp_frame->format,
+    av_image_copy(vt->tmp_frame->data, vt->tmp_frame->linesize,
+                  (const uint8_t **)data, linesize, vt->tmp_frame->format,
                   frame->width, frame->height);
 
+    ret = av_frame_copy_props(vt->tmp_frame, frame);
     CVPixelBufferUnlockBaseAddress(pixbuf, kCVPixelBufferLock_ReadOnly);
-
-    ret = av_frame_copy_props(vda->tmp_frame, frame);
-
     if (ret < 0)
         return ret;
 
     av_frame_unref(frame);
-    av_frame_move_ref(frame, vda->tmp_frame);
+    av_frame_move_ref(frame, vt->tmp_frame);
 
     return 0;
 }
 
-static void vda_uninit(AVCodecContext *s)
+static void videotoolbox_uninit(AVCodecContext *s)
 {
     InputStream *ist = s->opaque;
-    VDAContext  *vda = ist->hwaccel_ctx;
+    VTContext  *vt = ist->hwaccel_ctx;
 
     ist->hwaccel_uninit        = NULL;
     ist->hwaccel_retrieve_data = NULL;
 
-    av_frame_free(&vda->tmp_frame);
+    av_frame_free(&vt->tmp_frame);
 
-    av_vda_default_free(s);
+    if (ist->hwaccel_id == HWACCEL_VIDEOTOOLBOX) {
+#if CONFIG_VIDEOTOOLBOX
+        av_videotoolbox_default_free(s);
+#endif
+    } else {
+#if CONFIG_VDA
+        av_vda_default_free(s);
+#endif
+    }
     av_freep(&ist->hwaccel_ctx);
 }
 
-int vda_init(AVCodecContext *s)
+int videotoolbox_init(AVCodecContext *s)
 {
     InputStream *ist = s->opaque;
     int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
-    VDAContext *vda;
-    int ret;
+    int ret = 0;
+    VTContext *vt;
 
-    vda = av_mallocz(sizeof(*vda));
-    if (!vda)
+    vt = av_mallocz(sizeof(*vt));
+    if (!vt)
         return AVERROR(ENOMEM);
 
-    ist->hwaccel_ctx           = vda;
-    ist->hwaccel_uninit        = vda_uninit;
-    ist->hwaccel_retrieve_data = vda_retrieve_data;
+    ist->hwaccel_ctx           = vt;
+    ist->hwaccel_uninit        = videotoolbox_uninit;
+    ist->hwaccel_retrieve_data = videotoolbox_retrieve_data;
 
-    vda->tmp_frame = av_frame_alloc();
-    if (!vda->tmp_frame) {
+    vt->tmp_frame = av_frame_alloc();
+    if (!vt->tmp_frame) {
         ret = AVERROR(ENOMEM);
         goto fail;
     }
 
-    ret = av_vda_default_init(s);
+    if (ist->hwaccel_id == HWACCEL_VIDEOTOOLBOX) {
+#if CONFIG_VIDEOTOOLBOX
+        if (!videotoolbox_pixfmt) {
+            ret = av_videotoolbox_default_init(s);
+        } else {
+            AVVideotoolboxContext *vtctx = av_videotoolbox_alloc_context();
+            CFStringRef pixfmt_str = CFStringCreateWithCString(kCFAllocatorDefault,
+                                                               videotoolbox_pixfmt,
+                                                               kCFStringEncodingUTF8);
+            vtctx->cv_pix_fmt_type = UTGetOSTypeFromString(pixfmt_str);
+            ret = av_videotoolbox_default_init2(s, vtctx);
+            CFRelease(pixfmt_str);
+        }
+#endif
+    } else {
+#if CONFIG_VDA
+        if (!videotoolbox_pixfmt) {
+            ret = av_vda_default_init(s);
+        } else {
+            AVVDAContext *vdactx = av_vda_alloc_context();
+            CFStringRef pixfmt_str = CFStringCreateWithCString(kCFAllocatorDefault,
+                                                               videotoolbox_pixfmt,
+                                                               kCFStringEncodingUTF8);
+            vdactx->cv_pix_fmt_type = UTGetOSTypeFromString(pixfmt_str);
+            ret = av_vda_default_init2(s, vdactx);
+            CFRelease(pixfmt_str);
+        }
+#endif
+    }
     if (ret < 0) {
-        av_log(NULL, loglevel, "Error creating VDA decoder.\n");
+        av_log(NULL, loglevel,
+               "Error creating %s decoder.\n", ist->hwaccel_id == HWACCEL_VIDEOTOOLBOX ? "Videotoolbox" : "VDA");
         goto fail;
     }
 
     return 0;
 fail:
-    vda_uninit(s);
+    videotoolbox_uninit(s);
     return ret;
 }
