From 336034db271f7f293e4b2c601ffa6c31d82f7390 Mon Sep 17 00:00:00 2001
From: Christina Brien <christina.brien@polygonindustrial.com>
Date: Thu, 30 Mar 2017 23:38:03 +0100
Subject: [PATCH] H.261 chroma motion vectors round towards zero.

ITU-T H.261 (03/93) Section 3.2.2 states "The motion vector for both colour
difference blocks is derived by halving the component values of the macroblock
vector and truncating the magnitude parts towards zero to yield integer
components."

Simple integer division rounds towards negative infinity, not towards zero.
Negative numbers need to be rounded up, by adding (divisor/2) before division.
---
 Changelog                     | 1 +
 libavcodec/mpegvideo_motion.c | 4 ++--
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/Changelog b/Changelog
index ad53c9d..9591c98 100644
--- a/Changelog
+++ b/Changelog
@@ -32,6 +32,7 @@ version <next>:
 - Removed the legacy X11 screen grabber, use XCB instead
 - MPEG-7 Video Signature filter
 - Removed asyncts filter (use af_aresample instead)
+- Corrected H.261 chroma motion vector rounding
 
 
 version 3.2:
diff --git a/libavcodec/mpegvideo_motion.c b/libavcodec/mpegvideo_motion.c
index c913504..e92fb0d 100644
--- a/libavcodec/mpegvideo_motion.c
+++ b/libavcodec/mpegvideo_motion.c
@@ -268,8 +268,8 @@ void mpeg_motion_internal(MpegEncContext *s,
         }
     // Even chroma mv's are full pel in H261
     } else if (!is_mpeg12 && s->out_format == FMT_H261) {
-        mx      = motion_x / 4;
-        my      = motion_y / 4;
+        mx      = ((motion_x < 0) ? (motion_x + 2) : motion_x) / 4;
+        my      = ((motion_y < 0) ? (motion_y + 2) : motion_y) / 4;
         uvdxy   = 0;
         uvsrc_x = s->mb_x * 8 + mx;
         uvsrc_y = mb_y * 8 + my;
-- 
2.8.4 (Apple Git-73)

