diff --git a/ffmpeg.c b/ffmpeg.c
index 22a6b9dbf8ae535d50254eb03dd9683ca679f542..3011501c5835cbcdfa9df49e9628ef71a874cd84 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -847,20 +847,10 @@ static void do_video_out(AVFormatContext *s,
             }
         }
     } else if (ost->video_crop) {
-        picture_crop_temp.data[0] = formatted_picture->data[0] +
-                (ost->topBand * formatted_picture->linesize[0]) + ost->leftBand;
-
-        picture_crop_temp.data[1] = formatted_picture->data[1] +
-                ((ost->topBand >> 1) * formatted_picture->linesize[1]) +
-                (ost->leftBand >> 1);
-
-        picture_crop_temp.data[2] = formatted_picture->data[2] +
-                ((ost->topBand >> 1) * formatted_picture->linesize[2]) +
-                (ost->leftBand >> 1);
-
-        picture_crop_temp.linesize[0] = formatted_picture->linesize[0];
-        picture_crop_temp.linesize[1] = formatted_picture->linesize[1];
-        picture_crop_temp.linesize[2] = formatted_picture->linesize[2];
+        if (img_crop((AVPicture *)&picture_crop_temp, (AVPicture *)formatted_picture, enc->pix_fmt, ost->topBand, ost->leftBand) < 0) {
+            av_log(NULL, AV_LOG_ERROR, "error cropping picture\n");
+            goto the_end;
+        }
         final_picture = &picture_crop_temp;
     } else if (ost->video_pad) {
         final_picture = &ost->pict_tmp;
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index a9f3164dee260c5cde0ad6624446509e16dbf362..6c33373143c367be821d9f913681d029ea47d031 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -2555,6 +2555,9 @@ int is_adx(const unsigned char *buf,size_t bufsize);
 void img_copy(AVPicture *dst, const AVPicture *src,
               int pix_fmt, int width, int height);
 
+int img_crop(AVPicture *dst, const AVPicture *src,
+             int pix_fmt, int top_band, int left_band);
+
 /* av_log API */
 
 #include <stdarg.h>
diff --git a/libavcodec/imgconvert.c b/libavcodec/imgconvert.c
index 850f9b04f4088fe774a8604a676edb145c059c12..e03706d44021e5d4ec523ec2594ef73d03148c44 100644
--- a/libavcodec/imgconvert.c
+++ b/libavcodec/imgconvert.c
@@ -1951,6 +1951,31 @@ static inline int is_yuv_planar(PixFmtInfo *ps)
         ps->pixel_type == FF_PIXEL_PLANAR;
 }
 
+/**
+ * Crop image top and left side
+ */
+int img_crop(AVPicture *dst, const AVPicture *src,
+              int pix_fmt, int top_band, int left_band)
+{
+    int y_shift;
+    int x_shift;
+
+    if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
+        return -1;
+
+    y_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
+    x_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
+
+    dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
+    dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
+    dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
+
+    dst->linesize[0] = src->linesize[0];
+    dst->linesize[1] = src->linesize[1];
+    dst->linesize[2] = src->linesize[2];
+    return 0;
+}
+
 /* XXX: always use linesize. Return -1 if not supported */
 int img_convert(AVPicture *dst, int dst_pix_fmt,
                 const AVPicture *src, int src_pix_fmt,