diff --git a/libavcodec/adpcm.c b/libavcodec/adpcm.c
index d34fd6c6e1..80ce5b8518 100644
--- a/libavcodec/adpcm.c
+++ b/libavcodec/adpcm.c
@@ -775,7 +775,6 @@ static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
         case AV_CODEC_ID_ADPCM_IMA_DAT4:
         case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
         case AV_CODEC_ID_ADPCM_IMA_ISS:     header_size = 4 * ch;      break;
-        case AV_CODEC_ID_ADPCM_IMA_AMV:     header_size = 8;           break;
         case AV_CODEC_ID_ADPCM_IMA_SMJPEG:  header_size = 4 * ch;      break;
     }
     if (header_size > 0)
@@ -783,6 +782,13 @@ static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
 
     /* more complex formats */
     switch (avctx->codec->id) {
+    case AV_CODEC_ID_ADPCM_IMA_AMV:
+        bytestream2_skip(gb, 4);
+        has_coded_samples  = 1;
+        *coded_samples     = bytestream2_get_le32u(gb);
+        nb_samples         = FFMIN((buf_size - 8) * 2, *coded_samples);
+        bytestream2_seek(gb, -8, SEEK_CUR);
+        break;
     case AV_CODEC_ID_ADPCM_EA:
         has_coded_samples = 1;
         *coded_samples  = bytestream2_get_le32(gb);
@@ -1702,6 +1708,17 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
             *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
             *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3);
         }
+
+        if (nb_samples & 1) {
+            int v = bytestream2_get_byteu(&gb);
+            *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3);
+
+            if (v & 0x0F) {
+                /* Holds true on all the http://samples.mplayerhq.hu/amv samples. */
+                av_log(avctx, AV_LOG_WARNING, "Last nibble set on packet with odd sample count.\n");
+                av_log(avctx, AV_LOG_WARNING, "Sample will be skipped.\n");
+            }
+        }
         break;
     case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
         for (i = 0; i < avctx->channels; i++) {
diff --git a/tests/ref/fate/adpcm-ima-amv b/tests/ref/fate/adpcm-ima-amv
index eb174dfa81..2f8c0a2cdf 100644
--- a/tests/ref/fate/adpcm-ima-amv
+++ b/tests/ref/fate/adpcm-ima-amv
@@ -8,159 +8,159 @@
 0,       1378,       1378,     1378,     2756, 0x8462443f
 0,       2756,       2756,     1378,     2756, 0x9f493ba6
 0,       4134,       4134,     1378,     2756, 0x634e5f06
-0,       5512,       5512,     1380,     2760, 0x51f35cd4
-0,       6892,       6892,     1378,     2756, 0x011c51e5
-0,       8270,       8270,     1378,     2756, 0x8c2c198c
-0,       9648,       9648,     1378,     2756, 0x2b4a3397
-0,      11026,      11026,     1378,     2756, 0x63794f22
-0,      12404,      12404,     1378,     2756, 0xfc363898
-0,      13782,      13782,     1378,     2756, 0x0da5486e
-0,      15160,      15160,     1378,     2756, 0xbae17a5f
-0,      16538,      16538,     1380,     2760, 0xba266e1b
-0,      17918,      17918,     1378,     2756, 0xdfb61002
-0,      19296,      19296,     1378,     2756, 0x15d029da
-0,      20674,      20674,     1378,     2756, 0x7bc82012
-0,      22052,      22052,     1378,     2756, 0x002e6999
-0,      23430,      23430,     1378,     2756, 0x96346ba6
-0,      24808,      24808,     1378,     2756, 0x3d54543b
-0,      26186,      26186,     1380,     2760, 0x601786e1
-0,      27566,      27566,     1378,     2756, 0xf22a5793
-0,      28944,      28944,     1378,     2756, 0x21f54d49
-0,      30322,      30322,     1378,     2756, 0x0c6d4399
-0,      31700,      31700,     1378,     2756, 0x17282f8e
-0,      33078,      33078,     1378,     2756, 0xeb698f75
-0,      34456,      34456,     1378,     2756, 0x935e1de2
-0,      35834,      35834,     1380,     2760, 0xb6fb4293
-0,      37214,      37214,     1378,     2756, 0x485053dc
-0,      38592,      38592,     1378,     2756, 0x24c35027
-0,      39970,      39970,     1378,     2756, 0x09f323ee
-0,      41348,      41348,     1378,     2756, 0xbc7d58d5
-0,      42726,      42726,     1378,     2756, 0xaefd487c
-0,      44104,      44104,     1378,     2756, 0xaca16cc0
-0,      45482,      45482,     1380,     2760, 0x98a76091
-0,      46862,      46862,     1378,     2756, 0x5d357141
-0,      48240,      48240,     1378,     2756, 0x65ea2657
-0,      49618,      49618,     1378,     2756, 0xb5e1334a
-0,      50996,      50996,     1378,     2756, 0x32cd5d91
-0,      52374,      52374,     1378,     2756, 0xdc23722b
-0,      53752,      53752,     1378,     2756, 0x2ba34684
-0,      55130,      55130,     1378,     2756, 0xf9755ba8
-0,      56508,      56508,     1380,     2760, 0x24221ddb
-0,      57888,      57888,     1378,     2756, 0xef843aa4
-0,      59266,      59266,     1378,     2756, 0x420442fe
-0,      60644,      60644,     1378,     2756, 0x5a0933cb
-0,      62022,      62022,     1378,     2756, 0xef5f6d61
-0,      63400,      63400,     1378,     2756, 0xe57e6dc0
-0,      64778,      64778,     1378,     2756, 0xc0f0495a
-0,      66156,      66156,     1380,     2760, 0x2c3b55df
-0,      67536,      67536,     1378,     2756, 0x39c2586c
-0,      68914,      68914,     1378,     2756, 0x7ffc46e5
-0,      70292,      70292,     1378,     2756, 0xa2766664
-0,      71670,      71670,     1378,     2756, 0xacb50c6c
-0,      73048,      73048,     1378,     2756, 0x7f659084
-0,      74426,      74426,     1378,     2756, 0xc72e6a12
-0,      75804,      75804,     1380,     2760, 0xdb6944df
-0,      77184,      77184,     1378,     2756, 0x954f45c1
-0,      78562,      78562,     1378,     2756, 0xa9484240
-0,      79940,      79940,     1378,     2756, 0x1d595349
-0,      81318,      81318,     1378,     2756, 0xcf2a565e
-0,      82696,      82696,     1378,     2756, 0x391028d5
-0,      84074,      84074,     1378,     2756, 0x348db7ad
-0,      85452,      85452,     1380,     2760, 0xb69b5e3a
-0,      86832,      86832,     1378,     2756, 0xe3635fbe
-0,      88210,      88210,     1378,     2756, 0xdcad3654
-0,      89588,      89588,     1378,     2756, 0x5c17abef
-0,      90966,      90966,     1378,     2756, 0xb3235184
-0,      92344,      92344,     1378,     2756, 0xdabb64a6
-0,      93722,      93722,     1378,     2756, 0xa95dc58d
-0,      95100,      95100,     1380,     2760, 0x8e7ac9eb
-0,      96480,      96480,     1378,     2756, 0x492b658e
-0,      97858,      97858,     1378,     2756, 0x377483ab
-0,      99236,      99236,     1378,     2756, 0x2c250279
-0,     100614,     100614,     1378,     2756, 0x704dbdb3
-0,     101992,     101992,     1378,     2756, 0x800d7da2
-0,     103370,     103370,     1378,     2756, 0x872aa32e
-0,     104748,     104748,     1378,     2756, 0x2d4837fe
-0,     106126,     106126,     1380,     2760, 0xc89ea57e
-0,     107506,     107506,     1378,     2756, 0x6447d7ef
-0,     108884,     108884,     1378,     2756, 0x144f59cc
-0,     110262,     110262,     1378,     2756, 0xc667154e
-0,     111640,     111640,     1378,     2756, 0xf0de66ae
-0,     113018,     113018,     1378,     2756, 0xeabf3c32
-0,     114396,     114396,     1378,     2756, 0xe98e81d1
-0,     115774,     115774,     1380,     2760, 0x56aa5889
-0,     117154,     117154,     1378,     2756, 0x4fd34c0e
-0,     118532,     118532,     1378,     2756, 0x67cf6912
-0,     119910,     119910,     1378,     2756, 0xfa944def
-0,     121288,     121288,     1378,     2756, 0xc12f23b2
-0,     122666,     122666,     1378,     2756, 0x5ea325a2
-0,     124044,     124044,     1378,     2756, 0x2b245824
-0,     125422,     125422,     1380,     2760, 0x90ac533e
-0,     126802,     126802,     1378,     2756, 0xcca34d26
-0,     128180,     128180,     1378,     2756, 0xb5f820d0
-0,     129558,     129558,     1378,     2756, 0x27f24335
-0,     130936,     130936,     1378,     2756, 0x4a9e87b7
-0,     132314,     132314,     1378,     2756, 0xbd076129
-0,     133692,     133692,     1378,     2756, 0x2e0e3f2e
-0,     135070,     135070,     1380,     2760, 0xdf534478
-0,     136450,     136450,     1378,     2756, 0xca000a2e
-0,     137828,     137828,     1378,     2756, 0x87472df3
-0,     139206,     139206,     1378,     2756, 0x16733810
-0,     140584,     140584,     1378,     2756, 0xfa0734b4
-0,     141962,     141962,     1378,     2756, 0x5eff3fc4
-0,     143340,     143340,     1378,     2756, 0xf35346bd
-0,     144718,     144718,     1378,     2756, 0xac6411c5
-0,     146096,     146096,     1380,     2760, 0x478c3c56
-0,     147476,     147476,     1378,     2756, 0xebd30bdd
-0,     148854,     148854,     1378,     2756, 0xaef95a31
-0,     150232,     150232,     1378,     2756, 0x8aad29d1
-0,     151610,     151610,     1378,     2756, 0x626863f0
-0,     152988,     152988,     1378,     2756, 0x68c05707
-0,     154366,     154366,     1378,     2756, 0x437c5e8d
-0,     155744,     155744,     1380,     2760, 0x8eca4bdb
-0,     157124,     157124,     1378,     2756, 0x62bd4162
-0,     158502,     158502,     1378,     2756, 0x9f744aa4
-0,     159880,     159880,     1378,     2756, 0x0f3f6409
-0,     161258,     161258,     1378,     2756, 0x3fee827a
-0,     162636,     162636,     1378,     2756, 0x48a0ac19
-0,     164014,     164014,     1378,     2756, 0x8e4ce0d0
-0,     165392,     165392,     1380,     2760, 0xcda82236
-0,     166772,     166772,     1378,     2756, 0x0e523255
-0,     168150,     168150,     1378,     2756, 0x84103d30
-0,     169528,     169528,     1378,     2756, 0x13941cde
-0,     170906,     170906,     1378,     2756, 0x9fc834c5
-0,     172284,     172284,     1378,     2756, 0xc0217a77
-0,     173662,     173662,     1378,     2756, 0x3f643659
-0,     175040,     175040,     1380,     2760, 0x9dbd6002
-0,     176420,     176420,     1378,     2756, 0x94f046fb
-0,     177798,     177798,     1378,     2756, 0xab01fb12
-0,     179176,     179176,     1378,     2756, 0x04cffe5c
-0,     180554,     180554,     1378,     2756, 0xef661c5e
-0,     181932,     181932,     1378,     2756, 0x094c5fc5
-0,     183310,     183310,     1378,     2756, 0xe0c1486a
-0,     184688,     184688,     1380,     2760, 0x8c3535b7
-0,     186068,     186068,     1378,     2756, 0x594934aa
-0,     187446,     187446,     1378,     2756, 0x74007238
-0,     188824,     188824,     1378,     2756, 0x61f1394d
-0,     190202,     190202,     1378,     2756, 0x72584f07
-0,     191580,     191580,     1378,     2756, 0xced9acf9
-0,     192958,     192958,     1378,     2756, 0x7d2e3ea1
-0,     194336,     194336,     1378,     2756, 0x56c06897
-0,     195714,     195714,     1380,     2760, 0x19983bbf
-0,     197094,     197094,     1378,     2756, 0x4f884f27
-0,     198472,     198472,     1378,     2756, 0x81ab2f63
-0,     199850,     199850,     1378,     2756, 0x448e681d
-0,     201228,     201228,     1378,     2756, 0x0ba9826e
-0,     202606,     202606,     1378,     2756, 0x049f36fa
-0,     203984,     203984,     1378,     2756, 0x096a2b62
-0,     205362,     205362,     1380,     2760, 0x579e2035
-0,     206742,     206742,     1378,     2756, 0xd13e30e1
-0,     208120,     208120,     1378,     2756, 0x30b6412b
-0,     209498,     209498,     1378,     2756, 0xbb1c3268
-0,     210876,     210876,     1378,     2756, 0xbc175b6a
-0,     212254,     212254,     1378,     2756, 0xf8d160e2
-0,     213632,     213632,     1378,     2756, 0xc1048154
-0,     215010,     215010,     1380,     2760, 0xb83548f4
-0,     216390,     216390,     1378,     2756, 0x22647962
-0,     217768,     217768,     1378,     2756, 0x14ca54d3
-0,     219146,     219146,     1354,     2708, 0x85e82e8d
+0,       5512,       5512,     1379,     2758, 0x983d5ccb
+0,       6891,       6891,     1378,     2756, 0x011c51e5
+0,       8269,       8269,     1378,     2756, 0x8c2c198c
+0,       9647,       9647,     1378,     2756, 0x2b4a3397
+0,      11025,      11025,     1378,     2756, 0x63794f22
+0,      12403,      12403,     1378,     2756, 0xfc363898
+0,      13781,      13781,     1378,     2756, 0x0da5486e
+0,      15159,      15159,     1378,     2756, 0xbae17a5f
+0,      16537,      16537,     1379,     2758, 0xdde86dc6
+0,      17916,      17916,     1378,     2756, 0xdfb61002
+0,      19294,      19294,     1378,     2756, 0x15d029da
+0,      20672,      20672,     1378,     2756, 0x7bc82012
+0,      22050,      22050,     1378,     2756, 0x002e6999
+0,      23428,      23428,     1378,     2756, 0x96346ba6
+0,      24806,      24806,     1378,     2756, 0x3d54543b
+0,      26184,      26184,     1379,     2758, 0x5248862b
+0,      27563,      27563,     1378,     2756, 0xf22a5793
+0,      28941,      28941,     1378,     2756, 0x21f54d49
+0,      30319,      30319,     1378,     2756, 0x0c6d4399
+0,      31697,      31697,     1378,     2756, 0x17282f8e
+0,      33075,      33075,     1378,     2756, 0xeb698f75
+0,      34453,      34453,     1378,     2756, 0x935e1de2
+0,      35831,      35831,     1379,     2758, 0x31d5425d
+0,      37210,      37210,     1378,     2756, 0x485053dc
+0,      38588,      38588,     1378,     2756, 0x24c35027
+0,      39966,      39966,     1378,     2756, 0x09f323ee
+0,      41344,      41344,     1378,     2756, 0xbc7d58d5
+0,      42722,      42722,     1378,     2756, 0xaefd487c
+0,      44100,      44100,     1378,     2756, 0xaca16cc0
+0,      45478,      45478,     1379,     2758, 0xd8745ed3
+0,      46857,      46857,     1378,     2756, 0x5d357141
+0,      48235,      48235,     1378,     2756, 0x65ea2657
+0,      49613,      49613,     1378,     2756, 0xb5e1334a
+0,      50991,      50991,     1378,     2756, 0x32cd5d91
+0,      52369,      52369,     1378,     2756, 0xdc23722b
+0,      53747,      53747,     1378,     2756, 0x2ba34684
+0,      55125,      55125,     1378,     2756, 0xf9755ba8
+0,      56503,      56503,     1379,     2758, 0xe95c1ca8
+0,      57882,      57882,     1378,     2756, 0xef843aa4
+0,      59260,      59260,     1378,     2756, 0x420442fe
+0,      60638,      60638,     1378,     2756, 0x5a0933cb
+0,      62016,      62016,     1378,     2756, 0xef5f6d61
+0,      63394,      63394,     1378,     2756, 0xe57e6dc0
+0,      64772,      64772,     1378,     2756, 0xc0f0495a
+0,      66150,      66150,     1379,     2758, 0x806e55de
+0,      67529,      67529,     1378,     2756, 0x39c2586c
+0,      68907,      68907,     1378,     2756, 0x7ffc46e5
+0,      70285,      70285,     1378,     2756, 0xa2766664
+0,      71663,      71663,     1378,     2756, 0xacb50c6c
+0,      73041,      73041,     1378,     2756, 0x7f659084
+0,      74419,      74419,     1378,     2756, 0xc72e6a12
+0,      75797,      75797,     1379,     2758, 0x51ab446d
+0,      77176,      77176,     1378,     2756, 0x954f45c1
+0,      78554,      78554,     1378,     2756, 0xa9484240
+0,      79932,      79932,     1378,     2756, 0x1d595349
+0,      81310,      81310,     1378,     2756, 0xcf2a565e
+0,      82688,      82688,     1378,     2756, 0x391028d5
+0,      84066,      84066,     1378,     2756, 0x348db7ad
+0,      85444,      85444,     1379,     2758, 0xfa185e28
+0,      86823,      86823,     1378,     2756, 0xe3635fbe
+0,      88201,      88201,     1378,     2756, 0xdcad3654
+0,      89579,      89579,     1378,     2756, 0x5c17abef
+0,      90957,      90957,     1378,     2756, 0xb3235184
+0,      92335,      92335,     1378,     2756, 0xdabb64a6
+0,      93713,      93713,     1378,     2756, 0xa95dc58d
+0,      95091,      95091,     1379,     2758, 0xfa89c99b
+0,      96470,      96470,     1378,     2756, 0x492b658e
+0,      97848,      97848,     1378,     2756, 0x377483ab
+0,      99226,      99226,     1378,     2756, 0x2c250279
+0,     100604,     100604,     1378,     2756, 0x704dbdb3
+0,     101982,     101982,     1378,     2756, 0x800d7da2
+0,     103360,     103360,     1378,     2756, 0x872aa32e
+0,     104738,     104738,     1378,     2756, 0x2d4837fe
+0,     106116,     106116,     1379,     2758, 0x7d93a536
+0,     107495,     107495,     1378,     2756, 0x6447d7ef
+0,     108873,     108873,     1378,     2756, 0x144f59cc
+0,     110251,     110251,     1378,     2756, 0xc667154e
+0,     111629,     111629,     1378,     2756, 0xf0de66ae
+0,     113007,     113007,     1378,     2756, 0xeabf3c32
+0,     114385,     114385,     1378,     2756, 0xe98e81d1
+0,     115763,     115763,     1379,     2758, 0xa58a57f4
+0,     117142,     117142,     1378,     2756, 0x4fd34c0e
+0,     118520,     118520,     1378,     2756, 0x67cf6912
+0,     119898,     119898,     1378,     2756, 0xfa944def
+0,     121276,     121276,     1378,     2756, 0xc12f23b2
+0,     122654,     122654,     1378,     2756, 0x5ea325a2
+0,     124032,     124032,     1378,     2756, 0x2b245824
+0,     125410,     125410,     1379,     2758, 0xeb1f5203
+0,     126789,     126789,     1378,     2756, 0xcca34d26
+0,     128167,     128167,     1378,     2756, 0xb5f820d0
+0,     129545,     129545,     1378,     2756, 0x27f24335
+0,     130923,     130923,     1378,     2756, 0x4a9e87b7
+0,     132301,     132301,     1378,     2756, 0xbd076129
+0,     133679,     133679,     1378,     2756, 0x2e0e3f2e
+0,     135057,     135057,     1379,     2758, 0x5664442c
+0,     136436,     136436,     1378,     2756, 0xca000a2e
+0,     137814,     137814,     1378,     2756, 0x87472df3
+0,     139192,     139192,     1378,     2756, 0x16733810
+0,     140570,     140570,     1378,     2756, 0xfa0734b4
+0,     141948,     141948,     1378,     2756, 0x5eff3fc4
+0,     143326,     143326,     1378,     2756, 0xf35346bd
+0,     144704,     144704,     1378,     2756, 0xac6411c5
+0,     146082,     146082,     1379,     2758, 0xcfcf3ae7
+0,     147461,     147461,     1378,     2756, 0xebd30bdd
+0,     148839,     148839,     1378,     2756, 0xaef95a31
+0,     150217,     150217,     1378,     2756, 0x8aad29d1
+0,     151595,     151595,     1378,     2756, 0x626863f0
+0,     152973,     152973,     1378,     2756, 0x68c05707
+0,     154351,     154351,     1378,     2756, 0x437c5e8d
+0,     155729,     155729,     1379,     2758, 0xf7054b53
+0,     157108,     157108,     1378,     2756, 0x62bd4162
+0,     158486,     158486,     1378,     2756, 0x9f744aa4
+0,     159864,     159864,     1378,     2756, 0x0f3f6409
+0,     161242,     161242,     1378,     2756, 0x3fee827a
+0,     162620,     162620,     1378,     2756, 0x48a0ac19
+0,     163998,     163998,     1378,     2756, 0x8e4ce0d0
+0,     165376,     165376,     1379,     2758, 0x8a3b207f
+0,     166755,     166755,     1378,     2756, 0x0e523255
+0,     168133,     168133,     1378,     2756, 0x84103d30
+0,     169511,     169511,     1378,     2756, 0x13941cde
+0,     170889,     170889,     1378,     2756, 0x9fc834c5
+0,     172267,     172267,     1378,     2756, 0xc0217a77
+0,     173645,     173645,     1378,     2756, 0x3f643659
+0,     175023,     175023,     1379,     2758, 0xddac5fc3
+0,     176402,     176402,     1378,     2756, 0x94f046fb
+0,     177780,     177780,     1378,     2756, 0xab01fb12
+0,     179158,     179158,     1378,     2756, 0x04cffe5c
+0,     180536,     180536,     1378,     2756, 0xef661c5e
+0,     181914,     181914,     1378,     2756, 0x094c5fc5
+0,     183292,     183292,     1378,     2756, 0xe0c1486a
+0,     184670,     184670,     1379,     2758, 0x21c03448
+0,     186049,     186049,     1378,     2756, 0x594934aa
+0,     187427,     187427,     1378,     2756, 0x74007238
+0,     188805,     188805,     1378,     2756, 0x61f1394d
+0,     190183,     190183,     1378,     2756, 0x72584f07
+0,     191561,     191561,     1378,     2756, 0xced9acf9
+0,     192939,     192939,     1378,     2756, 0x7d2e3ea1
+0,     194317,     194317,     1378,     2756, 0x56c06897
+0,     195695,     195695,     1379,     2758, 0xa20b3b1b
+0,     197074,     197074,     1378,     2756, 0x4f884f27
+0,     198452,     198452,     1378,     2756, 0x81ab2f63
+0,     199830,     199830,     1378,     2756, 0x448e681d
+0,     201208,     201208,     1378,     2756, 0x0ba9826e
+0,     202586,     202586,     1378,     2756, 0x049f36fa
+0,     203964,     203964,     1378,     2756, 0x096a2b62
+0,     205342,     205342,     1379,     2758, 0x17361fbb
+0,     206721,     206721,     1378,     2756, 0xd13e30e1
+0,     208099,     208099,     1378,     2756, 0x30b6412b
+0,     209477,     209477,     1378,     2756, 0xbb1c3268
+0,     210855,     210855,     1378,     2756, 0xbc175b6a
+0,     212233,     212233,     1378,     2756, 0xf8d160e2
+0,     213611,     213611,     1378,     2756, 0xc1048154
+0,     214989,     214989,     1379,     2758, 0x26564885
+0,     216368,     216368,     1378,     2756, 0x22647962
+0,     217746,     217746,     1378,     2756, 0x14ca54d3
+0,     219124,     219124,     1376,     2752, 0x2c374add
