[FFmpeg-cvslog] dnxhd: 10-bit support

Joseph Artsimovich git at videolan.org
Fri Jul 22 12:16:58 CEST 2011


ffmpeg | branch: master | Joseph Artsimovich <joseph at mirriad.com> | Wed Jul 20 18:58:27 2011 +0100| [5ab21439fdfb8e239eb778128590d95967067b46] | committer: Mans Rullgard

dnxhd: 10-bit support

Signed-off-by: Mans Rullgard <mans at mansr.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=5ab21439fdfb8e239eb778128590d95967067b46
---

 libavcodec/dnxhddata.c     |  202 ++++++++++++++++++++++++++++++++++++-
 libavcodec/dnxhddata.h     |    2 +-
 libavcodec/dnxhddec.c      |   57 +++++++----
 libavcodec/dnxhdenc.c      |  240 +++++++++++++++++++++++++++++++++----------
 libavcodec/dnxhdenc.h      |    4 +
 libavcodec/x86/dnxhd_mmx.c |    3 +-
 6 files changed, 427 insertions(+), 81 deletions(-)

diff --git a/libavcodec/dnxhddata.c b/libavcodec/dnxhddata.c
index 677fb8f..576e85e 100644
--- a/libavcodec/dnxhddata.c
+++ b/libavcodec/dnxhddata.c
@@ -22,6 +22,28 @@
 #include "avcodec.h"
 #include "dnxhddata.h"
 
+static const uint8_t dnxhd_1235_luma_weight[] = {
+     0, 32, 32, 32, 33, 35, 38, 39,
+    32, 33, 32, 33, 36, 36, 39, 42,
+    32, 32, 33, 36, 35, 37, 41, 43,
+    31, 33, 34, 36, 36, 40, 42, 48,
+    32, 34, 36, 37, 39, 42, 46, 51,
+    36, 37, 37, 39, 41, 46, 51, 55,
+    37, 39, 41, 41, 47, 50, 55, 56,
+    41, 42, 41, 44, 50, 53, 60, 60
+};
+
+static const uint8_t dnxhd_1235_chroma_weight[] = {
+     0, 32, 33, 34, 39, 41, 54, 59,
+    33, 34, 35, 38, 43, 49, 58, 84,
+    34, 37, 39, 44, 46, 55, 74, 87,
+    40, 42, 47, 48, 58, 70, 87, 86,
+    43, 50, 56, 63, 72, 94, 91, 82,
+    55, 63, 65, 75, 93, 89, 85, 73,
+    61, 67, 82, 81, 83, 90, 79, 73,
+    74, 84, 75, 78, 90, 85, 73, 73
+};
+
 static const uint8_t dnxhd_1237_luma_weight[] = {
      0,  32,  33,  34, 34, 36, 37, 36,
     36,  37,  38,  38, 38, 39, 41, 44,
@@ -132,6 +154,28 @@ static const uint8_t dnxhd_1243_chroma_weight[] = {
     46, 45, 46, 47, 47, 48, 47, 47,
 };
 
+static const uint8_t dnxhd_1250_luma_weight[] = {
+     0, 32, 35, 35, 36, 36, 41, 43,
+    32, 34, 35, 36, 37, 39, 43, 47,
+    33, 34, 36, 38, 38, 42, 42, 50,
+    34, 36, 38, 38, 41, 40, 47, 54,
+    35, 38, 39, 40, 39, 45, 49, 58,
+    38, 39, 40, 39, 46, 47, 54, 60,
+    38, 39, 41, 46, 46, 48, 57, 62,
+    40, 41, 44, 45, 49, 54, 63, 63
+};
+
+static const uint8_t dnxhd_1250_chroma_weight[] = {
+     0, 32, 35, 36, 40, 42, 51, 51,
+    35, 36, 39, 39, 43, 51, 52, 55,
+    36, 41, 41, 43, 51, 53, 54, 56,
+    43, 44, 45, 50, 54, 54, 55, 57,
+    45, 48, 50, 51, 55, 58, 59, 58,
+    49, 52, 49, 57, 58, 62, 58, 60,
+    51, 51, 56, 58, 62, 61, 59, 62,
+    52, 52, 60, 61, 59, 59, 63, 63
+};
+
 static const uint8_t dnxhd_1251_luma_weight[] = {
      0, 32, 32, 34, 34, 34, 34, 35,
     35, 35, 36, 37, 36, 36, 35, 36,
@@ -604,6 +648,146 @@ static const uint8_t dnxhd_1235_1241_run[62] = {
     49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
 };
 
+static const uint8_t dnxhd_1250_dc_codes[14] = {
+    10, 62, 11, 12, 13, 0, 1, 2, 3, 4, 14, 30, 126, 127
+};
+static const uint8_t dnxhd_1250_dc_bits[14] = {
+    4, 6, 4, 4, 4, 3, 3, 3, 3, 3, 4, 5, 7, 7
+};
+static const uint16_t dnxhd_1250_ac_codes[257] = {
+        0,     1,     4,    10,    11,    24,    25,    26,
+       54,    55,    56,    57,   116,   117,   118,   119,
+      240,   241,   242,   243,   244,   245,   492,   493,
+      494,   495,   496,   497,   498,   998,   999,  1000,
+     1001,  1002,  1003,  1004,  1005,  1006,  2014,  2015,
+     2016,  2017,  2018,  2019,  2020,  2021,  2022,  2023,
+     2024,  2025,  4052,  4053,  4054,  4055,  4056,  4057,
+     4058,  4059,  4060,  4061,  4062,  4063,  4064,  4065,
+     4066,  4067,  8136,  8137,  8138,  8139,  8140,  8141,
+     8142,  8143,  8144,  8145,  8146,  8147,  8148,  8149,
+     8150,  8151,  8152,  8153,  8154,  8155,  8156, 16314,
+    16315, 16316, 16317, 16318, 16319, 16320, 16321, 16322,
+    16323, 16324, 16325, 16326, 16327, 16328, 16329, 16330,
+    16331, 16332, 16333, 16334, 16335, 16336, 16337, 16338,
+    32678, 32679, 32680, 32681, 32682, 32683, 32684, 32685,
+    32686, 32687, 32688, 32689, 32690, 32691, 32692, 32693,
+    32694, 32695, 32696, 32697, 32698, 32699, 32700, 32701,
+    32702, 32703, 32704, 32705, 32706, 32707, 32708, 32709,
+    32710, 32711, 32712, 65426, 65427, 65428, 65429, 65430,
+    65431, 65432, 65433, 65434, 65435, 65436, 65437, 65438,
+    65439, 65440, 65441, 65442, 65443, 65444, 65445, 65446,
+    65447, 65448, 65449, 65450, 65451, 65452, 65453, 65454,
+    65455, 65456, 65457, 65458, 65459, 65460, 65461, 65462,
+    65463, 65464, 65465, 65466, 65467, 65468, 65469, 65470,
+    65471, 65472, 65473, 65474, 65475, 65476, 65477, 65478,
+    65479, 65480, 65481, 65482, 65483, 65484, 65485, 65486,
+    65487, 65488, 65489, 65490, 65491, 65492, 65493, 65494,
+    65495, 65496, 65497, 65498, 65499, 65500, 65501, 65502,
+    65503, 65504, 65505, 65506, 65507, 65508, 65509, 65510,
+    65511, 65512, 65513, 65514, 65515, 65516, 65517, 65518,
+    65519, 65520, 65521, 65522, 65523, 65524, 65525, 65526,
+    65527, 65528, 65529, 65530, 65531, 65532, 65533, 65534,
+    65535
+};
+static const uint8_t dnxhd_1250_ac_bits[257] = {
+     2,  2,  3,  4,  4,  5,  5,  5,  6,  6,  6,  6,  7,  7,  7,  7,
+     8,  8,  8,  8,  8,  8,  9,  9,  9,  9,  9,  9,  9, 10, 10, 10,
+    10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
+    11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
+    12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
+    13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14,
+    14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
+    15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
+    15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
+    15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
+    16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
+    16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
+    16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
+    16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
+    16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
+    16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
+    16
+};
+static const uint8_t dnxhd_1250_ac_level[257] = {
+     1,  1,  2,  3,  0,  4,  5,  2,  6,  7,  8,  3,  9, 10, 11,  4,
+    12, 13, 14, 15, 16,  5, 17, 18, 19, 20, 21, 22,  6, 23, 24, 25,
+    26, 27, 28, 29,  7,  8, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
+     9, 10, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 11,
+    12, 13, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,  1,  2,
+     3,  4,  5, 14, 15, 16, 17,  6,  7,  8,  9, 10, 11, 12, 13, 14,
+    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 18, 19, 20, 21,
+    27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
+    43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 55, 56, 22, 23, 24,
+    25, 26, 27, 54, 57, 58, 59, 60, 61, 62, 63, 64, 28, 29, 30, 31,
+    32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
+    48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
+    64,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
+    16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
+    32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
+    48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
+    64
+};
+static const uint8_t dnxhd_1250_ac_run_flag[257] = {
+    0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1,
+    0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
+    0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+    1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
+    1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+    0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
+    1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
+    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+    1
+};
+static const uint8_t dnxhd_1250_ac_index_flag[257] = {
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
+    1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
+    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0,
+    0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+    1
+};
+static const uint16_t dnxhd_1250_run_codes[62] = {
+       0,    4,    5,   12,   26,   27,   28,   58,
+     118,  119,  120,  242,  486,  487,  976,  977,
+     978,  979,  980,  981,  982,  983,  984,  985,
+     986,  987,  988,  989,  990,  991,  992,  993,
+     994,  995,  996,  997,  998,  999, 1000, 1001,
+    1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009,
+    1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017,
+    1018, 1019, 1020, 1021, 1022, 1023
+};
+static const uint8_t dnxhd_1250_run_bits[62] = {
+     1,  3,  3,  4,  5,  5,  5,  6,  7,  7,  7,  8,  9,  9, 10, 10,
+    10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
+    10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
+    10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
+};
+static const uint8_t dnxhd_1250_run[62] = {
+     1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
+    17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
+    33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
+    49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62
+};
+
 static const uint8_t dnxhd_1251_dc_codes[12] = {
     0, 12, 13, 1, 2, 3, 4, 5, 14, 30, 62, 63,
 };
@@ -878,6 +1062,13 @@ static const uint8_t dnxhd_1252_ac_index_flag[257] = {
 };
 
 const CIDEntry ff_dnxhd_cid_table[] = {
+    { 1235, 1920, 1080, 0, 917504, 917504, 6, 10,
+      dnxhd_1235_luma_weight, dnxhd_1235_chroma_weight,
+      dnxhd_1235_1241_dc_codes, dnxhd_1235_1241_dc_bits,
+      dnxhd_1235_1241_ac_codes, dnxhd_1235_1241_ac_bits, dnxhd_1235_1241_ac_level,
+      dnxhd_1235_1241_ac_run_flag, dnxhd_1235_1241_ac_index_flag,
+      dnxhd_1235_1238_1241_run_codes, dnxhd_1235_1238_1241_run_bits, dnxhd_1235_1241_run,
+      { 175, 185, 365, 440 } },
     { 1237, 1920, 1080, 0, 606208, 606208, 4, 8,
       dnxhd_1237_luma_weight, dnxhd_1237_chroma_weight,
       dnxhd_1237_dc_codes, dnxhd_1237_dc_bits,
@@ -913,6 +1104,13 @@ const CIDEntry ff_dnxhd_cid_table[] = {
       dnxhd_1238_ac_run_flag, dnxhd_1238_ac_index_flag,
       dnxhd_1235_1238_1241_run_codes, dnxhd_1235_1238_1241_run_bits, dnxhd_1238_run,
       { 185, 220 } },
+    { 1250, 1280,  720, 0, 458752, 458752, 6, 10,
+      dnxhd_1250_luma_weight, dnxhd_1250_chroma_weight,
+      dnxhd_1250_dc_codes, dnxhd_1250_dc_bits,
+      dnxhd_1250_ac_codes, dnxhd_1250_ac_bits, dnxhd_1250_ac_level,
+      dnxhd_1250_ac_run_flag, dnxhd_1250_ac_index_flag,
+      dnxhd_1250_run_codes, dnxhd_1250_run_bits, dnxhd_1250_run,
+      { 90, 180, 220 } },
     { 1251, 1280,  720, 0, 458752, 458752, 4, 8,
       dnxhd_1251_luma_weight, dnxhd_1251_chroma_weight,
       dnxhd_1251_dc_codes, dnxhd_1251_dc_bits,
@@ -945,7 +1143,7 @@ int ff_dnxhd_get_cid_table(int cid)
     return -1;
 }
 
-int ff_dnxhd_find_cid(AVCodecContext *avctx)
+int ff_dnxhd_find_cid(AVCodecContext *avctx, int bit_depth)
 {
     int i, j;
     int mbs = avctx->bit_rate/1000000;
@@ -955,7 +1153,7 @@ int ff_dnxhd_find_cid(AVCodecContext *avctx)
         const CIDEntry *cid = &ff_dnxhd_cid_table[i];
         if (cid->width == avctx->width && cid->height == avctx->height &&
             cid->interlaced == !!(avctx->flags & CODEC_FLAG_INTERLACED_DCT) &&
-            cid->bit_depth == 8) { // until 10 bit is supported
+            cid->bit_depth == bit_depth) {
             for (j = 0; j < sizeof(cid->bit_rates); j++) {
                 if (cid->bit_rates[j] == mbs)
                     return cid->cid;
diff --git a/libavcodec/dnxhddata.h b/libavcodec/dnxhddata.h
index df84187..74cf0b5 100644
--- a/libavcodec/dnxhddata.h
+++ b/libavcodec/dnxhddata.h
@@ -46,6 +46,6 @@ typedef struct {
 extern const CIDEntry ff_dnxhd_cid_table[];
 
 int ff_dnxhd_get_cid_table(int cid);
-int ff_dnxhd_find_cid(AVCodecContext *avctx);
+int ff_dnxhd_find_cid(AVCodecContext *avctx, int bit_depth);
 
 #endif /* AVCODEC_DNXHDDATA_H */
diff --git a/libavcodec/dnxhddec.c b/libavcodec/dnxhddec.c
index 6928b32..7b32cd3 100644
--- a/libavcodec/dnxhddec.c
+++ b/libavcodec/dnxhddec.c
@@ -1,6 +1,9 @@
 /*
  * VC3/DNxHD decoder.
  * Copyright (c) 2007 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>
+ * Copyright (c) 2011 MirriAd Ltd
+ *
+ * 10 bit support added by MirriAd Ltd, Joseph Artsimovich <joseph at mirriad.com>
  *
  * This file is part of Libav.
  *
@@ -43,6 +46,7 @@ typedef struct {
     DECLARE_ALIGNED(16, DCTELEM, blocks)[8][64];
     ScanTable scantable;
     const CIDEntry *cid_table;
+    int bit_depth; // 8, 10 or 0 if not initialized at all.
 } DNXHDContext;
 
 #define DNXHD_VLC_BITS 9
@@ -53,7 +57,6 @@ static av_cold int dnxhd_decode_init(AVCodecContext *avctx)
     DNXHDContext *ctx = avctx->priv_data;
 
     ctx->avctx = avctx;
-    dsputil_init(&ctx->dsp, avctx);
     avctx->coded_frame = &ctx->picture;
     ctx->picture.type = AV_PICTURE_TYPE_I;
     ctx->picture.key_frame = 1;
@@ -73,7 +76,7 @@ static int dnxhd_init_vlc(DNXHDContext *ctx, int cid)
         init_vlc(&ctx->ac_vlc, DNXHD_VLC_BITS, 257,
                  ctx->cid_table->ac_bits, 1, 1,
                  ctx->cid_table->ac_codes, 2, 2, 0);
-        init_vlc(&ctx->dc_vlc, DNXHD_DC_VLC_BITS, ctx->cid_table->bit_depth+4,
+        init_vlc(&ctx->dc_vlc, DNXHD_DC_VLC_BITS, ctx->bit_depth + 4,
                  ctx->cid_table->dc_bits, 1, 1,
                  ctx->cid_table->dc_codes, 1, 1, 0);
         init_vlc(&ctx->run_vlc, DNXHD_VLC_BITS, 62,
@@ -110,8 +113,19 @@ static int dnxhd_decode_header(DNXHDContext *ctx, const uint8_t *buf, int buf_si
     av_dlog(ctx->avctx, "width %d, heigth %d\n", ctx->width, ctx->height);
 
     if (buf[0x21] & 0x40) {
-        av_log(ctx->avctx, AV_LOG_ERROR, "10 bit per component\n");
-        return -1;
+        ctx->avctx->pix_fmt = PIX_FMT_YUV422P10;
+        ctx->avctx->bits_per_raw_sample = 10;
+        if (ctx->bit_depth != 10) {
+            dsputil_init(&ctx->dsp, ctx->avctx);
+            ctx->bit_depth = 10;
+        }
+    } else {
+        ctx->avctx->pix_fmt = PIX_FMT_YUV422P;
+        ctx->avctx->bits_per_raw_sample = 8;
+        if (ctx->bit_depth != 8) {
+            dsputil_init(&ctx->dsp, ctx->avctx);
+            ctx->bit_depth = 8;
+        }
     }
 
     ctx->cid = AV_RB32(buf + 0x28);
@@ -204,7 +218,7 @@ static void dnxhd_decode_dct_block(DNXHDContext *ctx, DCTELEM *block, int n, int
         //av_log(ctx->avctx, AV_LOG_DEBUG, "j %d\n", j);
         //av_log(ctx->avctx, AV_LOG_DEBUG, "level %d, weigth %d\n", level, weigth_matrix[i]);
         level = (2*level+1) * qscale * weigth_matrix[i];
-        if (ctx->cid_table->bit_depth == 10) {
+        if (ctx->bit_depth == 10) {
             if (weigth_matrix[i] != 8)
                 level += 8;
             level >>= 4;
@@ -220,10 +234,11 @@ static void dnxhd_decode_dct_block(DNXHDContext *ctx, DCTELEM *block, int n, int
 
 static int dnxhd_decode_macroblock(DNXHDContext *ctx, int x, int y)
 {
+    int shift1 = ctx->bit_depth == 10;
     int dct_linesize_luma   = ctx->picture.linesize[0];
     int dct_linesize_chroma = ctx->picture.linesize[1];
     uint8_t *dest_y, *dest_u, *dest_v;
-    int dct_offset;
+    int dct_y_offset, dct_x_offset;
     int qscale, i;
 
     qscale = get_bits(&ctx->gb, 11);
@@ -240,9 +255,9 @@ static int dnxhd_decode_macroblock(DNXHDContext *ctx, int x, int y)
         dct_linesize_chroma <<= 1;
     }
 
-    dest_y = ctx->picture.data[0] + ((y * dct_linesize_luma)   << 4) + (x << 4);
-    dest_u = ctx->picture.data[1] + ((y * dct_linesize_chroma) << 4) + (x << 3);
-    dest_v = ctx->picture.data[2] + ((y * dct_linesize_chroma) << 4) + (x << 3);
+    dest_y = ctx->picture.data[0] + ((y * dct_linesize_luma)   << 4) + (x << (4 + shift1));
+    dest_u = ctx->picture.data[1] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1));
+    dest_v = ctx->picture.data[2] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1));
 
     if (ctx->cur_field) {
         dest_y += ctx->picture.linesize[0];
@@ -250,18 +265,19 @@ static int dnxhd_decode_macroblock(DNXHDContext *ctx, int x, int y)
         dest_v += ctx->picture.linesize[2];
     }
 
-    dct_offset = dct_linesize_luma << 3;
-    ctx->dsp.idct_put(dest_y,                  dct_linesize_luma, ctx->blocks[0]);
-    ctx->dsp.idct_put(dest_y + 8,              dct_linesize_luma, ctx->blocks[1]);
-    ctx->dsp.idct_put(dest_y + dct_offset,     dct_linesize_luma, ctx->blocks[4]);
-    ctx->dsp.idct_put(dest_y + dct_offset + 8, dct_linesize_luma, ctx->blocks[5]);
+    dct_y_offset = dct_linesize_luma << 3;
+    dct_x_offset = 8 << shift1;
+    ctx->dsp.idct_put(dest_y,                               dct_linesize_luma, ctx->blocks[0]);
+    ctx->dsp.idct_put(dest_y + dct_x_offset,                dct_linesize_luma, ctx->blocks[1]);
+    ctx->dsp.idct_put(dest_y + dct_y_offset,                dct_linesize_luma, ctx->blocks[4]);
+    ctx->dsp.idct_put(dest_y + dct_y_offset + dct_x_offset, dct_linesize_luma, ctx->blocks[5]);
 
     if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
-        dct_offset = dct_linesize_chroma << 3;
-        ctx->dsp.idct_put(dest_u,              dct_linesize_chroma, ctx->blocks[2]);
-        ctx->dsp.idct_put(dest_v,              dct_linesize_chroma, ctx->blocks[3]);
-        ctx->dsp.idct_put(dest_u + dct_offset, dct_linesize_chroma, ctx->blocks[6]);
-        ctx->dsp.idct_put(dest_v + dct_offset, dct_linesize_chroma, ctx->blocks[7]);
+        dct_y_offset = dct_linesize_chroma << 3;
+        ctx->dsp.idct_put(dest_u,                dct_linesize_chroma, ctx->blocks[2]);
+        ctx->dsp.idct_put(dest_v,                dct_linesize_chroma, ctx->blocks[3]);
+        ctx->dsp.idct_put(dest_u + dct_y_offset, dct_linesize_chroma, ctx->blocks[6]);
+        ctx->dsp.idct_put(dest_v + dct_y_offset, dct_linesize_chroma, ctx->blocks[7]);
     }
 
     return 0;
@@ -273,7 +289,7 @@ static int dnxhd_decode_macroblocks(DNXHDContext *ctx, const uint8_t *buf, int b
     for (y = 0; y < ctx->mb_height; y++) {
         ctx->last_dc[0] =
         ctx->last_dc[1] =
-        ctx->last_dc[2] = 1<<(ctx->cid_table->bit_depth+2); // for levels +2^(bitdepth-1)
+        ctx->last_dc[2] = 1 << (ctx->bit_depth + 2); // for levels +2^(bitdepth-1)
         init_get_bits(&ctx->gb, buf + ctx->mb_scan_index[y], (buf_size - ctx->mb_scan_index[y]) << 3);
         for (x = 0; x < ctx->mb_width; x++) {
             //START_TIMER;
@@ -306,7 +322,6 @@ static int dnxhd_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
         first_field = 1;
     }
 
-    avctx->pix_fmt = PIX_FMT_YUV422P;
     if (av_image_check_size(ctx->width, ctx->height, 0, avctx))
         return -1;
     avcodec_set_dimensions(avctx, ctx->width, ctx->height);
diff --git a/libavcodec/dnxhdenc.c b/libavcodec/dnxhdenc.c
index b65d0bf..3fb10d4 100644
--- a/libavcodec/dnxhdenc.c
+++ b/libavcodec/dnxhdenc.c
@@ -1,8 +1,10 @@
 /*
  * VC3/DNxHD encoder
  * Copyright (c) 2007 Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>
+ * Copyright (c) 2011 MirriAd Ltd
  *
  * VC-3 encoder funded by the British Broadcasting Corporation
+ * 10 bit support added by MirriAd Ltd, Joseph Artsimovich <joseph at mirriad.com>
  *
  * This file is part of Libav.
  *
@@ -32,6 +34,7 @@
 #include "dnxhdenc.h"
 
 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
+#define DNX10BIT_QMAT_SHIFT 18 // The largest value that will not lead to overflow for 10bit samples.
 
 static const AVOption options[]={
     {"nitris_compat", "encode with Avid Nitris compatibility", offsetof(DNXHDEncContext, nitris_compat), FF_OPT_TYPE_INT, {.dbl = 0}, 0, 1, VE},
@@ -41,7 +44,7 @@ static const AVClass class = { "dnxhd", av_default_item_name, options, LIBAVUTIL
 
 #define LAMBDA_FRAC_BITS 10
 
-static void dnxhd_get_pixels_8x4(DCTELEM *restrict block, const uint8_t *pixels, int line_size)
+static void dnxhd_8bit_get_pixels_8x4_sym(DCTELEM *restrict block, const uint8_t *pixels, int line_size)
 {
     int i;
     for (i = 0; i < 4; i++) {
@@ -58,6 +61,43 @@ static void dnxhd_get_pixels_8x4(DCTELEM *restrict block, const uint8_t *pixels,
     memcpy(block + 24, block - 32, sizeof(*block) * 8);
 }
 
+static av_always_inline void dnxhd_10bit_get_pixels_8x4_sym(DCTELEM *restrict block, const uint8_t *pixels, int line_size)
+{
+    int i;
+
+    block += 32;
+
+    for (i = 0; i < 4; i++) {
+        memcpy(block + i     * 8, pixels + i * line_size, 8 * sizeof(*block));
+        memcpy(block - (i+1) * 8, pixels + i * line_size, 8 * sizeof(*block));
+    }
+}
+
+static int dnxhd_10bit_dct_quantize(MpegEncContext *ctx, DCTELEM *block,
+                                    int n, int qscale, int *overflow)
+{
+    const uint8_t *scantable= ctx->intra_scantable.scantable;
+    const int *qmat = ctx->q_intra_matrix[qscale];
+    int last_non_zero = 0;
+
+    ctx->dsp.fdct(block);
+
+    // Divide by 4 with rounding, to compensate scaling of DCT coefficients
+    block[0] = (block[0] + 2) >> 2;
+
+    for (int i = 1; i < 64; ++i) {
+        int j = scantable[i];
+        int sign = block[j] >> 31;
+        int level = (block[j] ^ sign) - sign;
+        level = level * qmat[j] >> DNX10BIT_QMAT_SHIFT;
+        block[j] = (level ^ sign) - sign;
+        if (level)
+            last_non_zero = i;
+    }
+
+    return last_non_zero;
+}
+
 static int dnxhd_init_vlc(DNXHDEncContext *ctx)
 {
     int i, j, level, run;
@@ -118,31 +158,55 @@ static int dnxhd_init_qmat(DNXHDEncContext *ctx, int lbias, int cbias)
     // init first elem to 1 to avoid div by 0 in convert_matrix
     uint16_t weight_matrix[64] = {1,}; // convert_matrix needs uint16_t*
     int qscale, i;
+    const uint8_t *luma_weight_table   = ctx->cid_table->luma_weight;
+    const uint8_t *chroma_weight_table = ctx->cid_table->chroma_weight;
 
     FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->qmatrix_l,   (ctx->m.avctx->qmax+1) * 64 *     sizeof(int),      fail);
     FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->qmatrix_c,   (ctx->m.avctx->qmax+1) * 64 *     sizeof(int),      fail);
     FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->qmatrix_l16, (ctx->m.avctx->qmax+1) * 64 * 2 * sizeof(uint16_t), fail);
     FF_ALLOCZ_OR_GOTO(ctx->m.avctx, ctx->qmatrix_c16, (ctx->m.avctx->qmax+1) * 64 * 2 * sizeof(uint16_t), fail);
 
-    for (i = 1; i < 64; i++) {
-        int j = ctx->m.dsp.idct_permutation[ff_zigzag_direct[i]];
-        weight_matrix[j] = ctx->cid_table->luma_weight[i];
-    }
-    ff_convert_matrix(&ctx->m.dsp, ctx->qmatrix_l, ctx->qmatrix_l16, weight_matrix,
-                      ctx->m.intra_quant_bias, 1, ctx->m.avctx->qmax, 1);
-    for (i = 1; i < 64; i++) {
-        int j = ctx->m.dsp.idct_permutation[ff_zigzag_direct[i]];
-        weight_matrix[j] = ctx->cid_table->chroma_weight[i];
-    }
-    ff_convert_matrix(&ctx->m.dsp, ctx->qmatrix_c, ctx->qmatrix_c16, weight_matrix,
-                      ctx->m.intra_quant_bias, 1, ctx->m.avctx->qmax, 1);
-    for (qscale = 1; qscale <= ctx->m.avctx->qmax; qscale++) {
-        for (i = 0; i < 64; i++) {
-            ctx->qmatrix_l  [qscale]   [i] <<= 2; ctx->qmatrix_c  [qscale]   [i] <<= 2;
-            ctx->qmatrix_l16[qscale][0][i] <<= 2; ctx->qmatrix_l16[qscale][1][i] <<= 2;
-            ctx->qmatrix_c16[qscale][0][i] <<= 2; ctx->qmatrix_c16[qscale][1][i] <<= 2;
+    if (ctx->cid_table->bit_depth == 8) {
+        for (i = 1; i < 64; i++) {
+            int j = ctx->m.dsp.idct_permutation[ff_zigzag_direct[i]];
+            weight_matrix[j] = ctx->cid_table->luma_weight[i];
+        }
+        ff_convert_matrix(&ctx->m.dsp, ctx->qmatrix_l, ctx->qmatrix_l16, weight_matrix,
+                          ctx->m.intra_quant_bias, 1, ctx->m.avctx->qmax, 1);
+        for (i = 1; i < 64; i++) {
+            int j = ctx->m.dsp.idct_permutation[ff_zigzag_direct[i]];
+            weight_matrix[j] = ctx->cid_table->chroma_weight[i];
+        }
+        ff_convert_matrix(&ctx->m.dsp, ctx->qmatrix_c, ctx->qmatrix_c16, weight_matrix,
+                          ctx->m.intra_quant_bias, 1, ctx->m.avctx->qmax, 1);
+
+        for (qscale = 1; qscale <= ctx->m.avctx->qmax; qscale++) {
+            for (i = 0; i < 64; i++) {
+                ctx->qmatrix_l  [qscale]   [i] <<= 2; ctx->qmatrix_c  [qscale]   [i] <<= 2;
+                ctx->qmatrix_l16[qscale][0][i] <<= 2; ctx->qmatrix_l16[qscale][1][i] <<= 2;
+                ctx->qmatrix_c16[qscale][0][i] <<= 2; ctx->qmatrix_c16[qscale][1][i] <<= 2;
+            }
+        }
+    } else {
+        // 10-bit
+        for (qscale = 1; qscale <= ctx->m.avctx->qmax; qscale++) {
+            for (i = 1; i < 64; i++) {
+                int j = ctx->m.dsp.idct_permutation[ff_zigzag_direct[i]];
+
+                // The quantization formula from the VC-3 standard is:
+                // quantized = sign(block[i]) * floor(abs(block[i]/s) * p / (qscale * weight_table[i]))
+                // Where p is 32 for 8-bit samples and 8 for 10-bit ones.
+                // The s factor compensates scaling of DCT coefficients done by the DCT routines,
+                // and therefore is not present in standard.  It's 8 for 8-bit samples and 4 for 10-bit ones.
+                // We want values of ctx->qtmatrix_l and ctx->qtmatrix_r to be:
+                // ((1 << DNX10BIT_QMAT_SHIFT) * (p / s)) / (qscale * weight_table[i])
+                // For 10-bit samples, p / s == 2
+                ctx->qmatrix_l[qscale][j] = (1 << (DNX10BIT_QMAT_SHIFT + 1)) / (qscale * luma_weight_table[i]);
+                ctx->qmatrix_c[qscale][j] = (1 << (DNX10BIT_QMAT_SHIFT + 1)) / (qscale * chroma_weight_table[i]);
+            }
         }
     }
+
     return 0;
  fail:
     return -1;
@@ -165,10 +229,22 @@ static int dnxhd_init_rc(DNXHDEncContext *ctx)
 static int dnxhd_encode_init(AVCodecContext *avctx)
 {
     DNXHDEncContext *ctx = avctx->priv_data;
-    int i, index;
+    int i, index, bit_depth;
+
+    switch (avctx->pix_fmt) {
+    case PIX_FMT_YUV422P:
+        bit_depth = 8;
+        break;
+    case PIX_FMT_YUV422P10:
+        bit_depth = 10;
+        break;
+    default:
+        av_log(avctx, AV_LOG_ERROR, "pixel format is incompatible with DNxHD\n");
+        return -1;
+    }
 
-    ctx->cid = ff_dnxhd_find_cid(avctx);
-    if (!ctx->cid || avctx->pix_fmt != PIX_FMT_YUV422P) {
+    ctx->cid = ff_dnxhd_find_cid(avctx, bit_depth);
+    if (!ctx->cid) {
         av_log(avctx, AV_LOG_ERROR, "video parameters incompatible with DNxHD\n");
         return -1;
     }
@@ -181,15 +257,25 @@ static int dnxhd_encode_init(AVCodecContext *avctx)
     ctx->m.mb_intra = 1;
     ctx->m.h263_aic = 1;
 
-    ctx->get_pixels_8x4_sym = dnxhd_get_pixels_8x4;
+    avctx->bits_per_raw_sample = ctx->cid_table->bit_depth;
 
     dsputil_init(&ctx->m.dsp, avctx);
     ff_dct_common_init(&ctx->m);
+    if (!ctx->m.dct_quantize)
+        ctx->m.dct_quantize = dct_quantize_c;
+
+    if (ctx->cid_table->bit_depth == 10) {
+       ctx->m.dct_quantize = dnxhd_10bit_dct_quantize;
+       ctx->get_pixels_8x4_sym = dnxhd_10bit_get_pixels_8x4_sym;
+       ctx->block_width_l2 = 4;
+    } else {
+       ctx->get_pixels_8x4_sym = dnxhd_8bit_get_pixels_8x4_sym;
+       ctx->block_width_l2 = 3;
+    }
+
 #if HAVE_MMX
     ff_dnxhd_init_mmx(ctx);
 #endif
-    if (!ctx->m.dct_quantize)
-        ctx->m.dct_quantize = dct_quantize_c;
 
     ctx->m.mb_height = (avctx->height + 15) / 16;
     ctx->m.mb_width  = (avctx->width  + 15) / 16;
@@ -255,7 +341,7 @@ static int dnxhd_write_header(AVCodecContext *avctx, uint8_t *buf)
     AV_WB16(buf + 0x1a, avctx->width);  // SPL
     AV_WB16(buf + 0x1d, avctx->height>>ctx->interlaced); // NAL
 
-    buf[0x21] = 0x38; // FIXME 8 bit per comp
+    buf[0x21] = ctx->cid_table->bit_depth == 10 ? 0x58 : 0x38;
     buf[0x22] = 0x88 + (ctx->interlaced<<2);
     AV_WB32(buf + 0x28, ctx->cid); // CID
     buf[0x2c] = ctx->interlaced ? 0 : 0x80;
@@ -321,15 +407,27 @@ static av_always_inline void dnxhd_unquantize_c(DNXHDEncContext *ctx, DCTELEM *b
         if (level) {
             if (level < 0) {
                 level = (1-2*level) * qscale * weight_matrix[i];
-                if (weight_matrix[i] != 32)
-                    level += 32;
-                level >>= 6;
+                if (ctx->cid_table->bit_depth == 10) {
+                    if (weight_matrix[i] != 8)
+                        level += 8;
+                    level >>= 4;
+                } else {
+                    if (weight_matrix[i] != 32)
+                        level += 32;
+                    level >>= 6;
+                }
                 level = -level;
             } else {
                 level = (2*level+1) * qscale * weight_matrix[i];
-                if (weight_matrix[i] != 32)
-                    level += 32;
-                level >>= 6;
+                if (ctx->cid_table->bit_depth == 10) {
+                    if (weight_matrix[i] != 8)
+                        level += 8;
+                    level >>= 4;
+                } else {
+                    if (weight_matrix[i] != 32)
+                        level += 32;
+                    level >>= 6;
+                }
             }
             block[j] = level;
         }
@@ -364,22 +462,24 @@ static av_always_inline int dnxhd_calc_ac_bits(DNXHDEncContext *ctx, DCTELEM *bl
 
 static av_always_inline void dnxhd_get_blocks(DNXHDEncContext *ctx, int mb_x, int mb_y)
 {
-    const uint8_t *ptr_y = ctx->thread[0]->src[0] + ((mb_y << 4) * ctx->m.linesize)   + (mb_x << 4);
-    const uint8_t *ptr_u = ctx->thread[0]->src[1] + ((mb_y << 4) * ctx->m.uvlinesize) + (mb_x << 3);
-    const uint8_t *ptr_v = ctx->thread[0]->src[2] + ((mb_y << 4) * ctx->m.uvlinesize) + (mb_x << 3);
+    const int bs = ctx->block_width_l2;
+    const int bw = 1 << bs;
+    const uint8_t *ptr_y = ctx->thread[0]->src[0] + ((mb_y << 4) * ctx->m.linesize)   + (mb_x << bs+1);
+    const uint8_t *ptr_u = ctx->thread[0]->src[1] + ((mb_y << 4) * ctx->m.uvlinesize) + (mb_x << bs);
+    const uint8_t *ptr_v = ctx->thread[0]->src[2] + ((mb_y << 4) * ctx->m.uvlinesize) + (mb_x << bs);
     DSPContext *dsp = &ctx->m.dsp;
 
-    dsp->get_pixels(ctx->blocks[0], ptr_y,     ctx->m.linesize);
-    dsp->get_pixels(ctx->blocks[1], ptr_y + 8, ctx->m.linesize);
-    dsp->get_pixels(ctx->blocks[2], ptr_u,     ctx->m.uvlinesize);
-    dsp->get_pixels(ctx->blocks[3], ptr_v,     ctx->m.uvlinesize);
+    dsp->get_pixels(ctx->blocks[0], ptr_y,      ctx->m.linesize);
+    dsp->get_pixels(ctx->blocks[1], ptr_y + bw, ctx->m.linesize);
+    dsp->get_pixels(ctx->blocks[2], ptr_u,      ctx->m.uvlinesize);
+    dsp->get_pixels(ctx->blocks[3], ptr_v,      ctx->m.uvlinesize);
 
     if (mb_y+1 == ctx->m.mb_height && ctx->m.avctx->height == 1080) {
         if (ctx->interlaced) {
-            ctx->get_pixels_8x4_sym(ctx->blocks[4], ptr_y + ctx->dct_y_offset,     ctx->m.linesize);
-            ctx->get_pixels_8x4_sym(ctx->blocks[5], ptr_y + ctx->dct_y_offset + 8, ctx->m.linesize);
-            ctx->get_pixels_8x4_sym(ctx->blocks[6], ptr_u + ctx->dct_uv_offset,    ctx->m.uvlinesize);
-            ctx->get_pixels_8x4_sym(ctx->blocks[7], ptr_v + ctx->dct_uv_offset,    ctx->m.uvlinesize);
+            ctx->get_pixels_8x4_sym(ctx->blocks[4], ptr_y + ctx->dct_y_offset,      ctx->m.linesize);
+            ctx->get_pixels_8x4_sym(ctx->blocks[5], ptr_y + ctx->dct_y_offset + bw, ctx->m.linesize);
+            ctx->get_pixels_8x4_sym(ctx->blocks[6], ptr_u + ctx->dct_uv_offset,     ctx->m.uvlinesize);
+            ctx->get_pixels_8x4_sym(ctx->blocks[7], ptr_v + ctx->dct_uv_offset,     ctx->m.uvlinesize);
         } else {
             dsp->clear_block(ctx->blocks[4]);
             dsp->clear_block(ctx->blocks[5]);
@@ -387,10 +487,10 @@ static av_always_inline void dnxhd_get_blocks(DNXHDEncContext *ctx, int mb_x, in
             dsp->clear_block(ctx->blocks[7]);
         }
     } else {
-        dsp->get_pixels(ctx->blocks[4], ptr_y + ctx->dct_y_offset,     ctx->m.linesize);
-        dsp->get_pixels(ctx->blocks[5], ptr_y + ctx->dct_y_offset + 8, ctx->m.linesize);
-        dsp->get_pixels(ctx->blocks[6], ptr_u + ctx->dct_uv_offset,    ctx->m.uvlinesize);
-        dsp->get_pixels(ctx->blocks[7], ptr_v + ctx->dct_uv_offset,    ctx->m.uvlinesize);
+        dsp->get_pixels(ctx->blocks[4], ptr_y + ctx->dct_y_offset,      ctx->m.linesize);
+        dsp->get_pixels(ctx->blocks[5], ptr_y + ctx->dct_y_offset + bw, ctx->m.linesize);
+        dsp->get_pixels(ctx->blocks[6], ptr_u + ctx->dct_uv_offset,     ctx->m.uvlinesize);
+        dsp->get_pixels(ctx->blocks[7], ptr_v + ctx->dct_uv_offset,     ctx->m.uvlinesize);
     }
 }
 
@@ -417,7 +517,7 @@ static int dnxhd_calc_bits_thread(AVCodecContext *avctx, void *arg, int jobnr, i
 
     ctx->m.last_dc[0] =
     ctx->m.last_dc[1] =
-    ctx->m.last_dc[2] = 1024;
+    ctx->m.last_dc[2] = 1 << (ctx->cid_table->bit_depth + 2);
 
     for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
         unsigned mb = mb_y * ctx->m.mb_width + mb_x;
@@ -440,6 +540,8 @@ static int dnxhd_calc_bits_thread(AVCodecContext *avctx, void *arg, int jobnr, i
             diff = block[0] - ctx->m.last_dc[n];
             if (diff < 0) nbits = av_log2_16bit(-2*diff);
             else          nbits = av_log2_16bit( 2*diff);
+
+            assert(nbits < ctx->cid_table->bit_depth + 4);
             dc_bits += ctx->cid_table->dc_bits[nbits] + nbits;
 
             ctx->m.last_dc[n] = block[0];
@@ -465,7 +567,7 @@ static int dnxhd_encode_thread(AVCodecContext *avctx, void *arg, int jobnr, int
 
     ctx->m.last_dc[0] =
     ctx->m.last_dc[1] =
-    ctx->m.last_dc[2] = 1024;
+    ctx->m.last_dc[2] = 1 << (ctx->cid_table->bit_depth + 2);
     for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
         unsigned mb = mb_y * ctx->m.mb_width + mb_x;
         int qscale = ctx->mb_qscale[mb];
@@ -515,13 +617,39 @@ static int dnxhd_mb_var_thread(AVCodecContext *avctx, void *arg, int jobnr, int
     DNXHDEncContext *ctx = avctx->priv_data;
     int mb_y = jobnr, mb_x;
     ctx = ctx->thread[threadnr];
-    for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) {
-        unsigned mb  = mb_y * ctx->m.mb_width + mb_x;
-        uint8_t *pix = ctx->thread[0]->src[0] + ((mb_y<<4) * ctx->m.linesize) + (mb_x<<4);
-        int sum      = ctx->m.dsp.pix_sum(pix, ctx->m.linesize);
-        int varc     = (ctx->m.dsp.pix_norm1(pix, ctx->m.linesize) - (((unsigned)(sum*sum))>>8)+128)>>8;
-        ctx->mb_cmp[mb].value = varc;
-        ctx->mb_cmp[mb].mb = mb;
+    if (ctx->cid_table->bit_depth == 8) {
+        uint8_t *pix = ctx->thread[0]->src[0] + ((mb_y<<4) * ctx->m.linesize);
+        for (mb_x = 0; mb_x < ctx->m.mb_width; ++mb_x, pix += 16) {
+            unsigned mb  = mb_y * ctx->m.mb_width + mb_x;
+            int sum = ctx->m.dsp.pix_sum(pix, ctx->m.linesize);
+            int varc = (ctx->m.dsp.pix_norm1(pix, ctx->m.linesize) - (((unsigned)(sum*sum))>>8)+128)>>8;
+            ctx->mb_cmp[mb].value = varc;
+            ctx->mb_cmp[mb].mb = mb;
+        }
+    } else { // 10-bit
+        int const linesize = ctx->m.linesize >> 1;
+        for (mb_x = 0; mb_x < ctx->m.mb_width; ++mb_x) {
+            uint16_t *pix = (uint16_t*)ctx->thread[0]->src[0] + ((mb_y << 4) * linesize) + (mb_x << 4);
+            unsigned mb  = mb_y * ctx->m.mb_width + mb_x;
+            int sum = 0;
+            int sqsum = 0;
+            int mean, sqmean;
+            // Macroblocks are 16x16 pixels, unlike DCT blocks which are 8x8.
+            for (int i = 0; i < 16; ++i) {
+                for (int j = 0; j < 16; ++j) {
+                    // Turn 16-bit pixels into 10-bit ones.
+                    int const sample = (unsigned)pix[j] >> 6;
+                    sum += sample;
+                    sqsum += sample * sample;
+                    // 2^10 * 2^10 * 16 * 16 = 2^28, which is less than INT_MAX
+                }
+                pix += linesize;
+            }
+            mean = sum >> 8; // 16*16 == 2^8
+            sqmean = sqsum >> 8;
+            ctx->mb_cmp[mb].value = sqmean - mean * mean;
+            ctx->mb_cmp[mb].mb = mb;
+        }
     }
     return 0;
 }
@@ -871,7 +999,7 @@ AVCodec ff_dnxhd_encoder = {
     dnxhd_encode_picture,
     dnxhd_encode_end,
     .capabilities = CODEC_CAP_SLICE_THREADS,
-    .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV422P, PIX_FMT_NONE},
+    .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV422P, PIX_FMT_YUV422P10, PIX_FMT_NONE},
     .long_name = NULL_IF_CONFIG_SMALL("VC3/DNxHD"),
     .priv_class = &class,
 };
diff --git a/libavcodec/dnxhdenc.h b/libavcodec/dnxhdenc.h
index bb24540..861546a 100644
--- a/libavcodec/dnxhdenc.h
+++ b/libavcodec/dnxhdenc.h
@@ -52,8 +52,12 @@ typedef struct DNXHDEncContext {
 
     struct DNXHDEncContext *thread[MAX_THREADS];
 
+    // Because our samples are either 8 or 16 bits for 8-bit and 10-bit
+    // encoding respectively, these refer either to bytes or to two-byte words.
     unsigned dct_y_offset;
     unsigned dct_uv_offset;
+    unsigned block_width_l2;
+
     int interlaced;
     int cur_field;
 
diff --git a/libavcodec/x86/dnxhd_mmx.c b/libavcodec/x86/dnxhd_mmx.c
index f00ce9b..e193d62 100644
--- a/libavcodec/x86/dnxhd_mmx.c
+++ b/libavcodec/x86/dnxhd_mmx.c
@@ -53,6 +53,7 @@ static void get_pixels_8x4_sym_sse2(DCTELEM *block, const uint8_t *pixels, int l
 void ff_dnxhd_init_mmx(DNXHDEncContext *ctx)
 {
     if (av_get_cpu_flags() & AV_CPU_FLAG_SSE2) {
-        ctx->get_pixels_8x4_sym = get_pixels_8x4_sym_sse2;
+        if (ctx->cid_table->bit_depth == 8)
+            ctx->get_pixels_8x4_sym = get_pixels_8x4_sym_sse2;
     }
 }



More information about the ffmpeg-cvslog mailing list