You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1242 lines
53KB

  1. /*
  2. *
  3. * Copyright (C) 2002 the xine project
  4. * Copyright (C) 2002 the ffmpeg project
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * Ported to mplayer by Arpi <arpi@thot.banki.hu>
  21. * Ported to libavcodec by Nick Kurshev <nickols_k@mail.ru>
  22. *
  23. */
  24. /**
  25. * @file svq1.c
  26. * svq1 decoder.
  27. */
  28. //#define DEBUG_SVQ1
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #include "common.h"
  34. #include "avcodec.h"
  35. #include "dsputil.h"
  36. #include "mpegvideo.h"
  37. #include "bswap.h"
  38. #define bit_buffer_t GetBitContext
  39. static inline unsigned int get_bit_cache(GetBitContext *s){
  40. OPEN_READER(re, s)
  41. UPDATE_CACHE(re, s)
  42. return GET_CACHE(re, s);
  43. // CLOSE_READER(re, s)
  44. }
  45. /* variable length (bit) code */
  46. typedef struct vlc_code_s {
  47. int16_t value :10,
  48. length :6;
  49. } vlc_code_t;
  50. #define MEDIAN(a,b,c) (((a < b) != (b >= c)) ? b : (((a < c) != (c > b)) ? c : a))
  51. #define SVQ1_BLOCK_SKIP 0
  52. #define SVQ1_BLOCK_INTER 1
  53. #define SVQ1_BLOCK_INTER_4V 2
  54. #define SVQ1_BLOCK_INTRA 3
  55. /* motion vector (prediction) */
  56. typedef struct svq1_pmv_s {
  57. int x;
  58. int y;
  59. } svq1_pmv_t;
  60. #include "svq1_cb.h"
  61. /* block type, codes 000 .. 1xx */
  62. static vlc_code_t svq1_block_type_table[8] = {
  63. { SVQ1_BLOCK_INTRA, 3 }, { SVQ1_BLOCK_INTER_4V, 3 },
  64. { SVQ1_BLOCK_INTER, 2 }, { SVQ1_BLOCK_INTER, 2 },
  65. { SVQ1_BLOCK_SKIP, 1 }, { SVQ1_BLOCK_SKIP, 1 },
  66. { SVQ1_BLOCK_SKIP, 1 }, { SVQ1_BLOCK_SKIP, 1 }
  67. };
  68. /* motion vector, codes 0000011 .. 011xxxx */
  69. static vlc_code_t svq1_motion_table_0[61] = {
  70. { 7, 8 }, { 6, 8 }, { 5, 8 }, { 4, 7 }, { 4, 7 },
  71. { 3, 5 }, { 3, 5 }, { 3, 5 }, { 3, 5 }, { 3, 5 }, { 3, 5 }, { 3, 5 }, { 3, 5 },
  72. { 2, 4 }, { 2, 4 }, { 2, 4 }, { 2, 4 }, { 2, 4 }, { 2, 4 }, { 2, 4 }, { 2, 4 },
  73. { 2, 4 }, { 2, 4 }, { 2, 4 }, { 2, 4 }, { 2, 4 }, { 2, 4 }, { 2, 4 }, { 2, 4 },
  74. { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 },
  75. { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 },
  76. { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 },
  77. { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }
  78. };
  79. /* motion vector, codes 000000000010 ... 0000010111xx */
  80. static vlc_code_t svq1_motion_table_1[94] = {
  81. {32, 13}, {31, 13}, {30, 12}, {30, 12}, {29, 12}, {29, 12},
  82. {28, 12}, {28, 12}, {27, 12}, {27, 12}, {26, 12}, {26, 12}, {25, 12}, {25, 12},
  83. {24, 11}, {24, 11}, {24, 11}, {24, 11}, {23, 11}, {23, 11}, {23, 11}, {23, 11},
  84. {22, 11}, {22, 11}, {22, 11}, {22, 11}, {21, 11}, {21, 11}, {21, 11}, {21, 11},
  85. {20, 11}, {20, 11}, {20, 11}, {20, 11}, {19, 11}, {19, 11}, {19, 11}, {19, 11},
  86. {18, 11}, {18, 11}, {18, 11}, {18, 11}, {17, 11}, {17, 11}, {17, 11}, {17, 11},
  87. {16, 11}, {16, 11}, {16, 11}, {16, 11}, {15, 11}, {15, 11}, {15, 11}, {15, 11},
  88. {14, 11}, {14, 11}, {14, 11}, {14, 11}, {13, 11}, {13, 11}, {13, 11}, {13, 11},
  89. {12, 11}, {12, 11}, {12, 11}, {12, 11}, {11, 11}, {11, 11}, {11, 11}, {11, 11},
  90. {10, 10}, {10, 10}, {10, 10}, {10, 10}, {10, 10}, {10, 10}, {10, 10}, {10, 10},
  91. { 9, 10}, { 9, 10}, { 9, 10}, { 9, 10}, { 9, 10}, { 9, 10}, { 9, 10}, { 9, 10},
  92. { 8, 10}, { 8, 10}, { 8, 10}, { 8, 10}, { 8, 10}, { 8, 10}, { 8, 10}, { 8, 10},
  93. };
  94. /* inter-coded vector codebook count tables, codes 000000 ... 111111 */
  95. static vlc_code_t svq1_inter_vector_tables[6][64] = {
  96. /* 4x2 vector, codes 0000xxx ... 11xxxxx */
  97. { { 6, 4 }, { 6, 4 }, { 6, 4 }, { 6, 4 }, { 5, 4 }, { 5, 4 }, { 5, 4 }, { 5, 4 },
  98. { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 },
  99. { 3, 3 }, { 3, 3 }, { 3, 3 }, { 3, 3 }, { 3, 3 }, { 3, 3 }, { 3, 3 }, { 3, 3 },
  100. { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 },
  101. { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 },
  102. { 0, 3 }, { 0, 3 }, { 0, 3 }, { 0, 3 }, { 0, 3 }, { 0, 3 }, { 0, 3 }, { 0, 3 },
  103. {-1, 2 }, {-1, 2 }, {-1, 2 }, {-1, 2 }, {-1, 2 }, {-1, 2 }, {-1, 2 }, {-1, 2 },
  104. {-1, 2 }, {-1, 2 }, {-1, 2 }, {-1, 2 }, {-1, 2 }, {-1, 2 }, {-1, 2 }, {-1, 2 } },
  105. /* 4x4 vector, codes 0000xxx ... 11xxxxx */
  106. { { 6, 4 }, { 6, 4 }, { 6, 4 }, { 6, 4 }, { 5, 4 }, { 5, 4 }, { 5, 4 }, { 5, 4 },
  107. { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 },
  108. { 3, 3 }, { 3, 3 }, { 3, 3 }, { 3, 3 }, { 3, 3 }, { 3, 3 }, { 3, 3 }, { 3, 3 },
  109. { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 },
  110. { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 },
  111. { 0, 3 }, { 0, 3 }, { 0, 3 }, { 0, 3 }, { 0, 3 }, { 0, 3 }, { 0, 3 }, { 0, 3 },
  112. {-1, 2 }, {-1, 2 }, {-1, 2 }, {-1, 2 }, {-1, 2 }, {-1, 2 }, {-1, 2 }, {-1, 2 },
  113. {-1, 2 }, {-1, 2 }, {-1, 2 }, {-1, 2 }, {-1, 2 }, {-1, 2 }, {-1, 2 }, {-1, 2 } },
  114. /* 8x4 vector, codes 00000xx ... 1xxxxxx */
  115. { { 6, 5 }, { 6, 5 }, { 5, 5 }, { 5, 5 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 },
  116. { 3, 4 }, { 3, 4 }, { 3, 4 }, { 3, 4 }, { 2, 4 }, { 2, 4 }, { 2, 4 }, { 2, 4 },
  117. { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 },
  118. { 0, 3 }, { 0, 3 }, { 0, 3 }, { 0, 3 }, { 0, 3 }, { 0, 3 }, { 0, 3 }, { 0, 3 },
  119. {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 },
  120. {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 },
  121. {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 },
  122. {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 } },
  123. /* 8x8 vector, codes 00000xx ... 1xxxxxx */
  124. { { 6, 5 }, { 6, 5 }, { 5, 5 }, { 5, 5 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 },
  125. { 3, 4 }, { 3, 4 }, { 3, 4 }, { 3, 4 }, { 2, 4 }, { 2, 4 }, { 2, 4 }, { 2, 4 },
  126. { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 },
  127. { 0, 3 }, { 0, 3 }, { 0, 3 }, { 0, 3 }, { 0, 3 }, { 0, 3 }, { 0, 3 }, { 0, 3 },
  128. {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 },
  129. {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 },
  130. {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 },
  131. {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 } },
  132. /* 16x8 vector, codes 00000xx ... 1xxxxxx */
  133. { { 6, 5 }, { 6, 5 }, { 5, 5 }, { 5, 5 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 },
  134. { 3, 4 }, { 3, 4 }, { 3, 4 }, { 3, 4 }, { 2, 4 }, { 2, 4 }, { 2, 4 }, { 2, 4 },
  135. { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 },
  136. { 0, 3 }, { 0, 3 }, { 0, 3 }, { 0, 3 }, { 0, 3 }, { 0, 3 }, { 0, 3 }, { 0, 3 },
  137. {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 },
  138. {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 },
  139. {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 },
  140. {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 } },
  141. /* 16x16 vector, codes 000000x ... 1xxxxxx */
  142. { { 6, 6 }, { 5, 6 }, { 4, 5 }, { 4, 5 }, { 3, 5 }, { 3, 5 }, { 2, 5 }, { 2, 5 },
  143. { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 },
  144. { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 },
  145. { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 },
  146. {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 },
  147. {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 },
  148. {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 },
  149. {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 }, {-1, 1 } }
  150. };
  151. /* vector codebook count tables, codes 0000000 ... 1111111 */
  152. static vlc_code_t svq1_intra_vector_tables[6][128] = {
  153. /* 4x2 vector, codes 00000xx ... 1xxxxxx */
  154. { { 5, 5 }, { 5, 5 }, { 5, 5 }, { 5, 5 }, {-1, 5 }, {-1, 5 }, {-1, 5 }, {-1, 5 },
  155. { 6, 4 }, { 6, 4 }, { 6, 4 }, { 6, 4 }, { 6, 4 }, { 6, 4 }, { 6, 4 }, { 6, 4 },
  156. { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 },
  157. { 3, 4 }, { 3, 4 }, { 3, 4 }, { 3, 4 }, { 3, 4 }, { 3, 4 }, { 3, 4 }, { 3, 4 },
  158. { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 },
  159. { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 },
  160. { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 },
  161. { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 },
  162. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  163. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  164. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  165. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  166. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  167. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  168. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  169. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 } },
  170. /* 4x4 vector, codes 0000xxx ... 11xxxxx */
  171. { { 5, 4 }, { 5, 4 }, { 5, 4 }, { 5, 4 }, { 5, 4 }, { 5, 4 }, { 5, 4 }, { 5, 4 },
  172. {-1, 4 }, {-1, 4 }, {-1, 4 }, {-1, 4 }, {-1, 4 }, {-1, 4 }, {-1, 4 }, {-1, 4 },
  173. { 6, 3 }, { 6, 3 }, { 6, 3 }, { 6, 3 }, { 6, 3 }, { 6, 3 }, { 6, 3 }, { 6, 3 },
  174. { 6, 3 }, { 6, 3 }, { 6, 3 }, { 6, 3 }, { 6, 3 }, { 6, 3 }, { 6, 3 }, { 6, 3 },
  175. { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 },
  176. { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 },
  177. { 3, 3 }, { 3, 3 }, { 3, 3 }, { 3, 3 }, { 3, 3 }, { 3, 3 }, { 3, 3 }, { 3, 3 },
  178. { 3, 3 }, { 3, 3 }, { 3, 3 }, { 3, 3 }, { 3, 3 }, { 3, 3 }, { 3, 3 }, { 3, 3 },
  179. { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 },
  180. { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 },
  181. { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 },
  182. { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 },
  183. { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 },
  184. { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 },
  185. { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 },
  186. { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 } },
  187. /* 8x4 vector, codes 00000xx ... 1xxxxxx */
  188. { { 2, 5 }, { 2, 5 }, { 2, 5 }, { 2, 5 }, {-1, 5 }, {-1, 5 }, {-1, 5 }, {-1, 5 },
  189. { 6, 4 }, { 6, 4 }, { 6, 4 }, { 6, 4 }, { 6, 4 }, { 6, 4 }, { 6, 4 }, { 6, 4 },
  190. { 5, 4 }, { 5, 4 }, { 5, 4 }, { 5, 4 }, { 5, 4 }, { 5, 4 }, { 5, 4 }, { 5, 4 },
  191. { 3, 4 }, { 3, 4 }, { 3, 4 }, { 3, 4 }, { 3, 4 }, { 3, 4 }, { 3, 4 }, { 3, 4 },
  192. { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 },
  193. { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 },
  194. { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 },
  195. { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 },
  196. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  197. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  198. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  199. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  200. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  201. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  202. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  203. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 } },
  204. /* 8x8 vector, codes 000000x ... 1xxxxxx */
  205. { { 2, 6 }, { 2, 6 }, {-1, 6 }, {-1, 6 }, { 5, 5 }, { 5, 5 }, { 5, 5 }, { 5, 5 },
  206. { 6, 4 }, { 6, 4 }, { 6, 4 }, { 6, 4 }, { 6, 4 }, { 6, 4 }, { 6, 4 }, { 6, 4 },
  207. { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 },
  208. { 3, 4 }, { 3, 4 }, { 3, 4 }, { 3, 4 }, { 3, 4 }, { 3, 4 }, { 3, 4 }, { 3, 4 },
  209. { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 },
  210. { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 },
  211. { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 },
  212. { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 },
  213. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  214. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  215. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  216. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  217. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  218. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  219. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  220. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 } },
  221. /* 16x8 vector, codes 000000x ... 1xxxxxx */
  222. { { 4, 6 }, { 4, 6 }, {-1, 6 }, {-1, 6 }, { 5, 5 }, { 5, 5 }, { 5, 5 }, { 5, 5 },
  223. { 3, 5 }, { 3, 5 }, { 3, 5 }, { 3, 5 }, { 2, 5 }, { 2, 5 }, { 2, 5 }, { 2, 5 },
  224. { 6, 3 }, { 6, 3 }, { 6, 3 }, { 6, 3 }, { 6, 3 }, { 6, 3 }, { 6, 3 }, { 6, 3 },
  225. { 6, 3 }, { 6, 3 }, { 6, 3 }, { 6, 3 }, { 6, 3 }, { 6, 3 }, { 6, 3 }, { 6, 3 },
  226. { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 },
  227. { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 },
  228. { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 },
  229. { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 },
  230. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  231. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  232. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  233. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  234. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  235. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  236. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  237. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 } },
  238. /* 16x16 vector, codes 0000000 ... 1xxxxxx */
  239. { { 5, 7 }, {-1, 7 }, { 4, 6 }, { 4, 6 }, { 6, 5 }, { 6, 5 }, { 6, 5 }, { 6, 5 },
  240. { 3, 4 }, { 3, 4 }, { 3, 4 }, { 3, 4 }, { 3, 4 }, { 3, 4 }, { 3, 4 }, { 3, 4 },
  241. { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 },
  242. { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 },
  243. { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 },
  244. { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 },
  245. { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 },
  246. { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 },
  247. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  248. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  249. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  250. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  251. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  252. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  253. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  254. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 } }
  255. };
  256. /* intra mean value, codes 00100101 ... 1111xxxx */
  257. static vlc_code_t svq1_intra_mean_table_0[219] = {
  258. {135, 8 }, {136, 8 }, {165, 8 },
  259. {134, 8 }, {129, 8 }, {164, 8 }, {163, 8 }, {133, 8 }, {162, 8 }, {174, 8 }, {175, 8 },
  260. {161, 8 }, {160, 8 }, {159, 8 }, {158, 8 }, {157, 8 }, {156, 8 }, {155, 8 }, {154, 8 },
  261. {153, 8 }, {151, 8 }, {152, 8 }, {132, 8 }, {110, 8 }, {131, 8 }, {108, 8 }, {130, 8 },
  262. {166, 8 }, {105, 8 }, {104, 8 }, {103, 8 }, {127, 8 }, {101, 8 }, {167, 8 }, {168, 8 },
  263. { 98, 8 }, {128, 8 }, { 48, 8 }, { 95, 8 }, { 62, 8 }, { 93, 8 }, { 92, 8 }, { 91, 8 },
  264. { 90, 8 }, { 89, 8 }, { 60, 8 }, { 87, 8 }, { 86, 8 }, { 57, 8 }, { 84, 8 }, { 83, 8 },
  265. { 82, 8 }, { 81, 8 }, { 80, 8 }, { 79, 8 }, { 78, 8 }, { 77, 8 }, { 76, 8 }, { 75, 8 },
  266. { 74, 8 }, { 73, 8 }, { 72, 8 }, { 71, 8 }, { 70, 8 }, { 69, 8 }, { 68, 8 }, { 67, 8 },
  267. { 66, 8 }, { 65, 8 }, { 56, 8 }, { 63, 8 }, { 23, 8 }, { 61, 8 }, { 30, 8 }, { 59, 8 },
  268. { 58, 8 }, { 52, 8 }, { 29, 8 }, { 55, 8 }, { 54, 8 }, { 53, 8 }, { 50, 8 }, { 51, 8 },
  269. { 22, 8 }, { 49, 8 }, { 85, 7 }, { 85, 7 }, { 97, 7 }, { 97, 7 }, { 88, 7 }, { 88, 7 },
  270. { 64, 7 }, { 64, 7 }, { 94, 7 }, { 94, 7 }, {106, 7 }, {106, 7 }, {107, 7 }, {107, 7 },
  271. {109, 7 }, {109, 7 }, {111, 7 }, {111, 7 }, {112, 7 }, {112, 7 }, {113, 7 }, {113, 7 },
  272. {114, 7 }, {114, 7 }, {115, 7 }, {115, 7 }, {116, 7 }, {116, 7 }, {117, 7 }, {117, 7 },
  273. {118, 7 }, {118, 7 }, {119, 7 }, {119, 7 }, {120, 7 }, {120, 7 }, { 99, 7 }, { 99, 7 },
  274. {102, 7 }, {102, 7 }, { 28, 7 }, { 28, 7 }, {100, 7 }, {100, 7 }, { 96, 7 }, { 96, 7 },
  275. {139, 7 }, {139, 7 }, { 24, 7 }, { 24, 7 }, { 1, 7 }, { 1, 7 }, {138, 7 }, {138, 7 },
  276. {121, 7 }, {121, 7 }, {122, 7 }, {122, 7 }, {123, 7 }, {123, 7 }, {124, 7 }, {124, 7 },
  277. {125, 7 }, {125, 7 }, {126, 7 }, {126, 7 }, {137, 7 }, {137, 7 }, {140, 7 }, {140, 7 },
  278. {150, 7 }, {150, 7 }, {144, 7 }, {144, 7 }, {141, 7 }, {141, 7 }, {142, 7 }, {142, 7 },
  279. {143, 7 }, {143, 7 }, {145, 7 }, {145, 7 }, {147, 7 }, {147, 7 }, {146, 7 }, {146, 7 },
  280. { 27, 6 }, { 27, 6 }, { 27, 6 }, { 27, 6 }, {148, 6 }, {148, 6 }, {148, 6 }, {148, 6 },
  281. {149, 6 }, {149, 6 }, {149, 6 }, {149, 6 }, { 0, 6 }, { 0, 6 }, { 0, 6 }, { 0, 6 },
  282. { 26, 4 }, { 26, 4 }, { 26, 4 }, { 26, 4 }, { 26, 4 }, { 26, 4 }, { 26, 4 }, { 26, 4 },
  283. { 26, 4 }, { 26, 4 }, { 26, 4 }, { 26, 4 }, { 26, 4 }, { 26, 4 }, { 26, 4 }, { 26, 4 },
  284. { 25, 4 }, { 25, 4 }, { 25, 4 }, { 25, 4 }, { 25, 4 }, { 25, 4 }, { 25, 4 }, { 25, 4 },
  285. { 25, 4 }, { 25, 4 }, { 25, 4 }, { 25, 4 }, { 25, 4 }, { 25, 4 }, { 25, 4 }, { 25, 4 }
  286. };
  287. /* intra mean value, codes 0000001101 ... 001001001x */
  288. static vlc_code_t svq1_intra_mean_table_1[135] = {
  289. {218, 10}, {219, 10}, {220, 10},
  290. {221, 10}, {222, 10}, {217, 10}, {230, 10}, {215, 10}, {208, 10}, {207, 10}, {206, 10},
  291. {214, 10}, {204, 10}, {223, 10}, {224, 10}, {225, 10}, {226, 10}, {227, 10}, {228, 10},
  292. {229, 10}, {213, 10}, {212, 10}, {231, 10}, {232, 10}, {211, 10}, {210, 10}, {236, 10},
  293. {209, 10}, {216, 10}, {205, 10}, { 18, 10}, {186, 9 }, {186, 9 }, {185, 9 }, {185, 9 },
  294. {184, 9 }, {184, 9 }, {182, 9 }, {182, 9 }, {183, 9 }, {183, 9 }, {180, 9 }, {180, 9 },
  295. {181, 9 }, {181, 9 }, {178, 9 }, {178, 9 }, {187, 9 }, {187, 9 }, {176, 9 }, {176, 9 },
  296. {188, 9 }, {188, 9 }, {179, 9 }, {179, 9 }, {173, 9 }, {173, 9 }, {172, 9 }, {172, 9 },
  297. {171, 9 }, {171, 9 }, {170, 9 }, {170, 9 }, {169, 9 }, {169, 9 }, {189, 9 }, {189, 9 },
  298. {190, 9 }, {190, 9 }, {191, 9 }, {191, 9 }, {192, 9 }, {192, 9 }, {193, 9 }, {193, 9 },
  299. {194, 9 }, {194, 9 }, {195, 9 }, {195, 9 }, {196, 9 }, {196, 9 }, {197, 9 }, {197, 9 },
  300. {198, 9 }, {198, 9 }, {200, 9 }, {200, 9 }, {201, 9 }, {201, 9 }, {202, 9 }, {202, 9 },
  301. {203, 9 }, {203, 9 }, {199, 9 }, {199, 9 }, {177, 9 }, {177, 9 }, { 40, 9 }, { 40, 9 },
  302. { 39, 9 }, { 39, 9 }, { 38, 9 }, { 38, 9 }, { 37, 9 }, { 37, 9 }, { 36, 9 }, { 36, 9 },
  303. { 35, 9 }, { 35, 9 }, { 34, 9 }, { 34, 9 }, { 33, 9 }, { 33, 9 }, { 32, 9 }, { 32, 9 },
  304. { 31, 9 }, { 31, 9 }, { 21, 9 }, { 21, 9 }, { 11, 9 }, { 11, 9 }, { 41, 9 }, { 41, 9 },
  305. { 45, 9 }, { 45, 9 }, { 44, 9 }, { 44, 9 }, { 42, 9 }, { 42, 9 }, { 43, 9 }, { 43, 9 },
  306. { 47, 9 }, { 47, 9 }, { 46, 9 }, { 46, 9 }
  307. };
  308. /* intra mean value, codes 00000000000001 ... 00000011001xxx */
  309. static vlc_code_t svq1_intra_mean_table_2[207] = {
  310. {255, 14}, { 14, 14}, { 13, 14}, { 17, 12}, { 17, 12}, { 17, 12}, { 17, 12},
  311. {243, 11}, {243, 11}, {243, 11}, {243, 11}, {243, 11}, {243, 11}, {243, 11}, {243, 11},
  312. {242, 11}, {242, 11}, {242, 11}, {242, 11}, {242, 11}, {242, 11}, {242, 11}, {242, 11},
  313. {241, 11}, {241, 11}, {241, 11}, {241, 11}, {241, 11}, {241, 11}, {241, 11}, {241, 11},
  314. {240, 11}, {240, 11}, {240, 11}, {240, 11}, {240, 11}, {240, 11}, {240, 11}, {240, 11},
  315. {237, 11}, {237, 11}, {237, 11}, {237, 11}, {237, 11}, {237, 11}, {237, 11}, {237, 11},
  316. {239, 11}, {239, 11}, {239, 11}, {239, 11}, {239, 11}, {239, 11}, {239, 11}, {239, 11},
  317. {235, 11}, {235, 11}, {235, 11}, {235, 11}, {235, 11}, {235, 11}, {235, 11}, {235, 11},
  318. {234, 11}, {234, 11}, {234, 11}, {234, 11}, {234, 11}, {234, 11}, {234, 11}, {234, 11},
  319. {233, 11}, {233, 11}, {233, 11}, {233, 11}, {233, 11}, {233, 11}, {233, 11}, {233, 11},
  320. {244, 11}, {244, 11}, {244, 11}, {244, 11}, {244, 11}, {244, 11}, {244, 11}, {244, 11},
  321. {238, 11}, {238, 11}, {238, 11}, {238, 11}, {238, 11}, {238, 11}, {238, 11}, {238, 11},
  322. { 20, 11}, { 20, 11}, { 20, 11}, { 20, 11}, { 20, 11}, { 20, 11}, { 20, 11}, { 20, 11},
  323. {248, 11}, {248, 11}, {248, 11}, {248, 11}, {248, 11}, {248, 11}, {248, 11}, {248, 11},
  324. {249, 11}, {249, 11}, {249, 11}, {249, 11}, {249, 11}, {249, 11}, {249, 11}, {249, 11},
  325. {250, 11}, {250, 11}, {250, 11}, {250, 11}, {250, 11}, {250, 11}, {250, 11}, {250, 11},
  326. {251, 11}, {251, 11}, {251, 11}, {251, 11}, {251, 11}, {251, 11}, {251, 11}, {251, 11},
  327. {252, 11}, {252, 11}, {252, 11}, {252, 11}, {252, 11}, {252, 11}, {252, 11}, {252, 11},
  328. {253, 11}, {253, 11}, {253, 11}, {253, 11}, {253, 11}, {253, 11}, {253, 11}, {253, 11},
  329. {254, 11}, {254, 11}, {254, 11}, {254, 11}, {254, 11}, {254, 11}, {254, 11}, {254, 11},
  330. { 12, 11}, { 12, 11}, { 12, 11}, { 12, 11}, { 12, 11}, { 12, 11}, { 12, 11}, { 12, 11},
  331. { 10, 11}, { 10, 11}, { 10, 11}, { 10, 11}, { 10, 11}, { 10, 11}, { 10, 11}, { 10, 11},
  332. {245, 11}, {245, 11}, {245, 11}, {245, 11}, {245, 11}, {245, 11}, {245, 11}, {245, 11},
  333. {247, 11}, {247, 11}, {247, 11}, {247, 11}, {247, 11}, {247, 11}, {247, 11}, {247, 11},
  334. { 19, 11}, { 19, 11}, { 19, 11}, { 19, 11}, { 19, 11}, { 19, 11}, { 19, 11}, { 19, 11},
  335. {246, 11}, {246, 11}, {246, 11}, {246, 11}, {246, 11}, {246, 11}, {246, 11}, {246, 11}
  336. };
  337. /* intra mean value, codes 00000000000000000000 ... 000000000000001xxxxx */
  338. static vlc_code_t svq1_intra_mean_table_3[64] = {
  339. { 6, 20}, { 3, 20}, { 4, 20}, { 5, 20}, { 7, 20}, { 8, 20}, { 9, 19}, { 9, 19},
  340. { 2, 17}, { 2, 17}, { 2, 17}, { 2, 17}, { 2, 17}, { 2, 17}, { 2, 17}, { 2, 17},
  341. { 16, 16}, { 16, 16}, { 16, 16}, { 16, 16}, { 16, 16}, { 16, 16}, { 16, 16}, { 16, 16},
  342. { 16, 16}, { 16, 16}, { 16, 16}, { 16, 16}, { 16, 16}, { 16, 16}, { 16, 16}, { 16, 16},
  343. { 15, 15}, { 15, 15}, { 15, 15}, { 15, 15}, { 15, 15}, { 15, 15}, { 15, 15}, { 15, 15},
  344. { 15, 15}, { 15, 15}, { 15, 15}, { 15, 15}, { 15, 15}, { 15, 15}, { 15, 15}, { 15, 15},
  345. { 15, 15}, { 15, 15}, { 15, 15}, { 15, 15}, { 15, 15}, { 15, 15}, { 15, 15}, { 15, 15},
  346. { 15, 15}, { 15, 15}, { 15, 15}, { 15, 15}, { 15, 15}, { 15, 15}, { 15, 15}, { 15, 15}
  347. };
  348. /* inter mean value, codes 00001011 ... 1xxxxxxx */
  349. static vlc_code_t svq1_inter_mean_table_0[245] = {
  350. { 10, 8 }, { 12, 8 }, { 11, 8 }, {-11, 8 }, {-12, 8 },
  351. {-10, 8 }, { -9, 8 }, { -7, 7 }, { -7, 7 }, { -6, 7 }, { -6, 7 }, { 8, 7 }, { 8, 7 },
  352. { -8, 7 }, { -8, 7 }, { 9, 7 }, { 9, 7 }, { 6, 7 }, { 6, 7 }, { 7, 7 }, { 7, 7 },
  353. { -5, 6 }, { -5, 6 }, { -5, 6 }, { -5, 6 }, { -4, 6 }, { -4, 6 }, { -4, 6 }, { -4, 6 },
  354. { 5, 6 }, { 5, 6 }, { 5, 6 }, { 5, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 },
  355. { 3, 5 }, { 3, 5 }, { 3, 5 }, { 3, 5 }, { 3, 5 }, { 3, 5 }, { 3, 5 }, { 3, 5 },
  356. { 2, 5 }, { 2, 5 }, { 2, 5 }, { 2, 5 }, { 2, 5 }, { 2, 5 }, { 2, 5 }, { 2, 5 },
  357. { -2, 5 }, { -2, 5 }, { -2, 5 }, { -2, 5 }, { -2, 5 }, { -2, 5 }, { -2, 5 }, { -2, 5 },
  358. { -3, 5 }, { -3, 5 }, { -3, 5 }, { -3, 5 }, { -3, 5 }, { -3, 5 }, { -3, 5 }, { -3, 5 },
  359. { -1, 4 }, { -1, 4 }, { -1, 4 }, { -1, 4 }, { -1, 4 }, { -1, 4 }, { -1, 4 }, { -1, 4 },
  360. { -1, 4 }, { -1, 4 }, { -1, 4 }, { -1, 4 }, { -1, 4 }, { -1, 4 }, { -1, 4 }, { -1, 4 },
  361. { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 },
  362. { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 },
  363. { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 },
  364. { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 }, { 1, 3 },
  365. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  366. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  367. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  368. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  369. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  370. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  371. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  372. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  373. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  374. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  375. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  376. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  377. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  378. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  379. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 },
  380. { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }
  381. };
  382. /* inter mean value, codes 000000010010 ... 00001011xxxx */
  383. static vlc_code_t svq1_inter_mean_table_1[158] = {
  384. {-30, 12}, {-31, 12}, {-32, 12}, {-33, 12}, { 31, 12}, {-34, 12},
  385. {-35, 12}, { 29, 12}, { 30, 12}, { 33, 12}, { 34, 12}, { 32, 12}, {-29, 11}, {-29, 11},
  386. {-28, 11}, {-28, 11}, { 28, 11}, { 28, 11}, {-27, 11}, {-27, 11}, {-26, 11}, {-26, 11},
  387. { 27, 11}, { 27, 11}, { 26, 11}, { 26, 11}, { 25, 11}, { 25, 11}, { 24, 11}, { 24, 11},
  388. { 23, 11}, { 23, 11}, { 22, 11}, { 22, 11}, {-24, 11}, {-24, 11}, {-25, 11}, {-25, 11},
  389. {-23, 10}, {-23, 10}, {-23, 10}, {-23, 10}, {-21, 10}, {-21, 10}, {-21, 10}, {-21, 10},
  390. {-20, 10}, {-20, 10}, {-20, 10}, {-20, 10}, {-19, 10}, {-19, 10}, {-19, 10}, {-19, 10},
  391. {-18, 10}, {-18, 10}, {-18, 10}, {-18, 10}, {-22, 10}, {-22, 10}, {-22, 10}, {-22, 10},
  392. { 19, 10}, { 19, 10}, { 19, 10}, { 19, 10}, { 21, 10}, { 21, 10}, { 21, 10}, { 21, 10},
  393. { 20, 10}, { 20, 10}, { 20, 10}, { 20, 10}, { 18, 10}, { 18, 10}, { 18, 10}, { 18, 10},
  394. {-14, 9 }, {-14, 9 }, {-14, 9 }, {-14, 9 }, {-14, 9 }, {-14, 9 }, {-14, 9 }, {-14, 9 },
  395. {-17, 9 }, {-17, 9 }, {-17, 9 }, {-17, 9 }, {-17, 9 }, {-17, 9 }, {-17, 9 }, {-17, 9 },
  396. { 16, 9 }, { 16, 9 }, { 16, 9 }, { 16, 9 }, { 16, 9 }, { 16, 9 }, { 16, 9 }, { 16, 9 },
  397. { 13, 9 }, { 13, 9 }, { 13, 9 }, { 13, 9 }, { 13, 9 }, { 13, 9 }, { 13, 9 }, { 13, 9 },
  398. { 14, 9 }, { 14, 9 }, { 14, 9 }, { 14, 9 }, { 14, 9 }, { 14, 9 }, { 14, 9 }, { 14, 9 },
  399. { 15, 9 }, { 15, 9 }, { 15, 9 }, { 15, 9 }, { 15, 9 }, { 15, 9 }, { 15, 9 }, { 15, 9 },
  400. { 17, 9 }, { 17, 9 }, { 17, 9 }, { 17, 9 }, { 17, 9 }, { 17, 9 }, { 17, 9 }, { 17, 9 },
  401. {-13, 9 }, {-13, 9 }, {-13, 9 }, {-13, 9 }, {-13, 9 }, {-13, 9 }, {-13, 9 }, {-13, 9 },
  402. {-16, 9 }, {-16, 9 }, {-16, 9 }, {-16, 9 }, {-16, 9 }, {-16, 9 }, {-16, 9 }, {-16, 9 },
  403. {-15, 9 }, {-15, 9 }, {-15, 9 }, {-15, 9 }, {-15, 9 }, {-15, 9 }, {-15, 9 }, {-15, 9 }
  404. };
  405. /* inter mean value, codes 000000000010111 ... 0000000100011xx */
  406. static vlc_code_t svq1_inter_mean_table_2[121] = {
  407. { 61, 15},
  408. { 52, 15}, { 58, 15}, {-56, 15}, {-57, 15}, { 59, 15}, {-55, 15}, { 60, 15}, {-54, 15},
  409. { 53, 15}, {-62, 15}, {-60, 15}, {-59, 15}, {-58, 15}, { 57, 15}, { 56, 15}, {-53, 15},
  410. { 55, 15}, { 54, 15}, { 50, 14}, { 50, 14}, { 45, 14}, { 45, 14}, {-52, 14}, {-52, 14},
  411. {-51, 14}, {-51, 14}, {-50, 14}, {-50, 14}, { 46, 14}, { 46, 14}, {-49, 14}, {-49, 14},
  412. {-48, 14}, {-48, 14}, { 48, 14}, { 48, 14}, {-47, 14}, {-47, 14}, { 49, 14}, { 49, 14},
  413. {-45, 14}, {-45, 14}, {-44, 14}, {-44, 14}, { 47, 14}, { 47, 14}, { 51, 14}, { 51, 14},
  414. { 44, 14}, { 44, 14}, {-46, 14}, {-46, 14}, {-43, 13}, {-43, 13}, {-43, 13}, {-43, 13},
  415. {-42, 13}, {-42, 13}, {-42, 13}, {-42, 13}, {-41, 13}, {-41, 13}, {-41, 13}, {-41, 13},
  416. {-40, 13}, {-40, 13}, {-40, 13}, {-40, 13}, {-39, 13}, {-39, 13}, {-39, 13}, {-39, 13},
  417. {-38, 13}, {-38, 13}, {-38, 13}, {-38, 13}, {-37, 13}, {-37, 13}, {-37, 13}, {-37, 13},
  418. {-36, 13}, {-36, 13}, {-36, 13}, {-36, 13}, { 42, 13}, { 42, 13}, { 42, 13}, { 42, 13},
  419. { 36, 13}, { 36, 13}, { 36, 13}, { 36, 13}, { 43, 13}, { 43, 13}, { 43, 13}, { 43, 13},
  420. { 41, 13}, { 41, 13}, { 41, 13}, { 41, 13}, { 40, 13}, { 40, 13}, { 40, 13}, { 40, 13},
  421. { 35, 13}, { 35, 13}, { 35, 13}, { 35, 13}, { 39, 13}, { 39, 13}, { 39, 13}, { 39, 13},
  422. { 38, 13}, { 38, 13}, { 38, 13}, { 38, 13}, { 37, 13}, { 37, 13}, { 37, 13}, { 37, 13}
  423. };
  424. /* inter mean value, codes 000000000000100101 ... 0000000000101101xx */
  425. static vlc_code_t svq1_inter_mean_table_3[147] = {
  426. {111, 18}, {102, 18}, { 99, 18},
  427. {-86, 18}, { 97, 18}, {-97, 18}, { 96, 18}, {-95, 18}, {-76, 18}, {-77, 18}, {-78, 18},
  428. {-85, 18}, {-80, 18}, {-81, 18}, { 89, 18}, { 90, 18}, {-84, 18}, {-89, 18}, { 80, 18},
  429. {-90, 18}, {-88, 18}, { 92, 18}, { 93, 18}, { 95, 18}, {-109,18}, {-79, 17}, {-79, 17},
  430. {-83, 17}, {-83, 17}, {-75, 17}, {-75, 17}, {-74, 17}, {-74, 17}, {-73, 17}, {-73, 17},
  431. {-72, 17}, {-72, 17}, { 87, 17}, { 87, 17}, { 86, 17}, { 86, 17}, { 75, 17}, { 75, 17},
  432. { 85, 17}, { 85, 17}, { 84, 17}, { 84, 17}, { 81, 17}, { 81, 17}, { 79, 17}, { 79, 17},
  433. { 74, 17}, { 74, 17}, { 72, 17}, { 72, 17}, { 82, 17}, { 82, 17}, { 78, 17}, { 78, 17},
  434. { 83, 17}, { 83, 17}, { 76, 17}, { 76, 17}, { 73, 17}, { 73, 17}, { 77, 17}, { 77, 17},
  435. { 70, 16}, { 70, 16}, { 70, 16}, { 70, 16}, { 69, 16}, { 69, 16}, { 69, 16}, { 69, 16},
  436. { 64, 16}, { 64, 16}, { 64, 16}, { 64, 16}, {-68, 16}, {-68, 16}, {-68, 16}, {-68, 16},
  437. {-66, 16}, {-66, 16}, {-66, 16}, {-66, 16}, { 68, 16}, { 68, 16}, { 68, 16}, { 68, 16},
  438. {-69, 16}, {-69, 16}, {-69, 16}, {-69, 16}, { 65, 16}, { 65, 16}, { 65, 16}, { 65, 16},
  439. { 71, 16}, { 71, 16}, { 71, 16}, { 71, 16}, {-70, 16}, {-70, 16}, {-70, 16}, {-70, 16},
  440. {-65, 16}, {-65, 16}, {-65, 16}, {-65, 16}, {-67, 16}, {-67, 16}, {-67, 16}, {-67, 16},
  441. {-63, 16}, {-63, 16}, {-63, 16}, {-63, 16}, {-71, 16}, {-71, 16}, {-71, 16}, {-71, 16},
  442. { 67, 16}, { 67, 16}, { 67, 16}, { 67, 16}, {-61, 16}, {-61, 16}, {-61, 16}, {-61, 16},
  443. {-64, 16}, {-64, 16}, {-64, 16}, {-64, 16}, { 63, 16}, { 63, 16}, { 63, 16}, { 63, 16},
  444. { 62, 16}, { 62, 16}, { 62, 16}, { 62, 16}, { 66, 16}, { 66, 16}, { 66, 16}, { 66, 16}
  445. };
  446. /* inter mean value, codes 00000000000001001001 ... 0000000000001001001x */
  447. static vlc_code_t svq1_inter_mean_table_4[75] = {
  448. {142, 20}, {135, 20}, {125, 20}, {123, 20}, {122, 20}, {119, 20}, {117, 20},
  449. {113, 20}, {104, 20}, {103, 20}, {-120,20}, {-114,20}, {-108,20}, {-104,20}, {-102,20},
  450. {-101,20}, {-93, 20}, {-91, 19}, {-91, 19}, {-96, 19}, {-96, 19}, { 88, 19}, { 88, 19},
  451. { 91, 19}, { 91, 19}, { 94, 19}, { 94, 19}, {121, 19}, {121, 19}, {120, 19}, {120, 19},
  452. {-110,19}, {-110,19}, {118, 19}, {118, 19}, {115, 19}, {115, 19}, {114, 19}, {114, 19},
  453. {112, 19}, {112, 19}, {107, 19}, {107, 19}, {110, 19}, {110, 19}, {109, 19}, {109, 19},
  454. {108, 19}, {108, 19}, {-103,19}, {-103,19}, {106, 19}, {106, 19}, {105, 19}, {105, 19},
  455. {-100,19}, {-100,19}, {101, 19}, {101, 19}, {100, 19}, {100, 19}, {-99, 19}, {-99, 19},
  456. {-82, 19}, {-82, 19}, {-87, 19}, {-87, 19}, {-94, 19}, {-94, 19}, {-98, 19}, {-98, 19},
  457. { 98, 19}, { 98, 19}, {-92, 19}, {-92, 19}
  458. };
  459. /* inter mean value, codes 0000000000000000000000 ... 000000000000010010001x */
  460. static vlc_code_t svq1_inter_mean_table_5[292] = {
  461. {255, 22}, {254, 22}, {253, 22}, {252, 22}, {251, 22}, {250, 22}, {249, 22}, {248, 22},
  462. {247, 22}, {246, 22}, {245, 22}, {244, 22}, {243, 22}, {242, 22}, {241, 22}, {240, 22},
  463. {239, 22}, {238, 22}, {237, 22}, {236, 22}, {235, 22}, {234, 22}, {233, 22}, {232, 22},
  464. {231, 22}, {230, 22}, {229, 22}, {228, 22}, {227, 22}, {226, 22}, {225, 22}, {224, 22},
  465. {223, 22}, {222, 22}, {221, 22}, {220, 22}, {219, 22}, {218, 22}, {217, 22}, {216, 22},
  466. {215, 22}, {214, 22}, {213, 22}, {212, 22}, {211, 22}, {210, 22}, {209, 22}, {208, 22},
  467. {207, 22}, {206, 22}, {205, 22}, {204, 22}, {203, 22}, {202, 22}, {201, 22}, {200, 22},
  468. {199, 22}, {198, 22}, {197, 22}, {196, 22}, {195, 22}, {194, 22}, {193, 22}, {192, 22},
  469. {191, 22}, {190, 22}, {189, 22}, {188, 22}, {187, 22}, {186, 22}, {185, 22}, {184, 22},
  470. {183, 22}, {182, 22}, {181, 22}, {180, 22}, {179, 22}, {178, 22}, {177, 22}, {176, 22},
  471. {175, 22}, {174, 22}, {173, 22}, {172, 22}, {171, 22}, {170, 22}, {169, 22}, {168, 22},
  472. {167, 22}, {166, 22}, {-256,22}, {164, 22}, {-211,22}, {162, 22}, {161, 22}, {160, 22},
  473. {-210,22}, {-168,22}, {157, 22}, {156, 22}, {155, 22}, {154, 22}, {153, 22}, {152, 22},
  474. {151, 22}, {150, 22}, {149, 22}, {148, 22}, {147, 22}, {146, 22}, {145, 22}, {144, 22},
  475. {143, 22}, {-208,22}, {141, 22}, {140, 22}, {139, 22}, {-200,22}, {137, 22}, {136, 22},
  476. {-198,22}, {134, 22}, {133, 22}, {-196,22}, {-201,22}, {130, 22}, {129, 22}, {128, 22},
  477. {127, 22}, {126, 22}, {-195,22}, {124, 22}, {-167,22}, {-166,22}, {-165,22}, {-164,22},
  478. {-163,22}, {-162,22}, {-161,22}, {-180,22}, {-160,22}, {-159,22}, {-158,22}, {-157,22},
  479. {-156,22}, {-155,22}, {-154,22}, {-153,22}, {-152,22}, {-151,22}, {-150,22}, {-149,22},
  480. {-148,22}, {-147,22}, {-146,22}, {-145,22}, {-144,22}, {-143,22}, {-142,22}, {-141,22},
  481. {-140,22}, {-139,22}, {-138,22}, {-137,22}, {-136,22}, {-135,22}, {-134,22}, {-133,22},
  482. {-132,22}, {-131,22}, {-130,22}, {-129,22}, {-126,22}, {-125,22}, {-124,22}, {-123,22},
  483. {-122,22}, {-121,22}, {-118,22}, {-116,22}, {-115,22}, {-113,22}, {-112,22}, {-107,22},
  484. {-106,22}, {-169,22}, {-170,22}, {-171,22}, {-172,22}, {-173,22}, {-174,22}, {-175,22},
  485. {-176,22}, {-177,22}, {-178,22}, {-187,22}, {-179,22}, {-181,22}, {-182,22}, {-183,22},
  486. {-184,22}, {-185,22}, {-186,22}, {-235,22}, {-188,22}, {-189,22}, {-190,22}, {-191,22},
  487. {-192,22}, {-193,22}, {-194,22}, {-197,22}, {-255,22}, {-254,22}, {-253,22}, {-252,22},
  488. {-251,22}, {-250,22}, {-249,22}, {-248,22}, {-247,22}, {-246,22}, {-245,22}, {-244,22},
  489. {-243,22}, {-242,22}, {-241,22}, {-240,22}, {-239,22}, {-238,22}, {-237,22}, {-232,22},
  490. {-236,22}, {-234,22}, {-233,22}, {-217,22}, {-231,22}, {-230,22}, {-229,22}, {-228,22},
  491. {-227,22}, {-226,22}, {-225,22}, {-224,22}, {-223,22}, {-222,22}, {-221,22}, {-220,22},
  492. {-219,22}, {-216,22}, {-202,22}, {-205,22}, {-215,22}, {-214,22}, {-213,22}, {-204,22},
  493. {-212,22}, {-209,22}, {-218,22}, {-199,22}, {-207,22}, {-206,22}, {165, 21}, {165, 21},
  494. {131, 21}, {131, 21}, {163, 21}, {163, 21}, {-203,21}, {-203,21}, {116, 21}, {116, 21},
  495. {159, 21}, {159, 21}, {138, 21}, {138, 21}, {158, 21}, {158, 21}, {-105,21}, {-105,21},
  496. {-111,21}, {-111,21}, {132, 21}, {132, 21}, {128, 21}, {128, 21}, {-127,21}, {-127,21},
  497. {-119,21}, {-119,21}, {-117,21}, {-117,21}
  498. };
  499. #define SVQ1_PROCESS_VECTOR()\
  500. for (; level > 0; i++) {\
  501. /* process next depth */\
  502. if (i == m) {\
  503. m = n;\
  504. if (--level == 0)\
  505. break;\
  506. }\
  507. /* divide block if next bit set */\
  508. if (get_bits (bitbuf, 1) == 0)\
  509. break;\
  510. /* add child nodes */\
  511. list[n++] = list[i];\
  512. list[n++] = list[i] + (((level & 1) ? pitch : 1) << ((level / 2) + 1));\
  513. }
  514. #define SVQ1_ADD_CODEBOOK()\
  515. /* add codebook entries to vector */\
  516. for (j=0; j < stages; j++) {\
  517. n3 = codebook[entries[j]] ^ 0x80808080;\
  518. n1 += ((n3 & 0xFF00FF00) >> 8);\
  519. n2 += (n3 & 0x00FF00FF);\
  520. }\
  521. \
  522. /* clip to [0..255] */\
  523. if (n1 & 0xFF00FF00) {\
  524. n3 = ((( n1 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
  525. n1 += 0x7F007F00;\
  526. n1 |= (((~n1 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
  527. n1 &= (n3 & 0x00FF00FF);\
  528. }\
  529. \
  530. if (n2 & 0xFF00FF00) {\
  531. n3 = ((( n2 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
  532. n2 += 0x7F007F00;\
  533. n2 |= (((~n2 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
  534. n2 &= (n3 & 0x00FF00FF);\
  535. }
  536. #define SVQ1_DO_CODEBOOK_INTRA()\
  537. for (y=0; y < height; y++) {\
  538. for (x=0; x < (width / 4); x++, codebook++) {\
  539. n1 = n4;\
  540. n2 = n4;\
  541. SVQ1_ADD_CODEBOOK()\
  542. /* store result */\
  543. dst[x] = (n1 << 8) | n2;\
  544. }\
  545. dst += (pitch / 4);\
  546. }
  547. #define SVQ1_DO_CODEBOOK_NONINTRA()\
  548. for (y=0; y < height; y++) {\
  549. for (x=0; x < (width / 4); x++, codebook++) {\
  550. n3 = dst[x];\
  551. /* add mean value to vector */\
  552. n1 = ((n3 & 0xFF00FF00) >> 8) + n4;\
  553. n2 = (n3 & 0x00FF00FF) + n4;\
  554. SVQ1_ADD_CODEBOOK()\
  555. /* store result */\
  556. dst[x] = (n1 << 8) | n2;\
  557. }\
  558. dst += (pitch / 4);\
  559. }
  560. #define SVQ1_CALC_CODEBOOK_ENTRIES(cbook)\
  561. codebook = (const uint32_t *) cbook[level];\
  562. bit_cache = get_bits (bitbuf, 4*stages);\
  563. /* calculate codebook entries for this vector */\
  564. for (j=0; j < stages; j++) {\
  565. entries[j] = (((bit_cache >> (4*(stages - j - 1))) & 0xF) + 16*j) << (level + 1);\
  566. }\
  567. mean -= (stages * 128);\
  568. n4 = ((mean + (mean >> 31)) << 16) | (mean & 0xFFFF);
  569. static int svq1_decode_block_intra (bit_buffer_t *bitbuf, uint8_t *pixels, int pitch ) {
  570. uint32_t bit_cache;
  571. vlc_code_t *vlc;
  572. uint8_t *list[63];
  573. uint32_t *dst;
  574. const uint32_t *codebook;
  575. int entries[6];
  576. int i, j, m, n;
  577. int mean, stages;
  578. unsigned x, y, width, height, level;
  579. uint32_t n1, n2, n3, n4;
  580. /* initialize list for breadth first processing of vectors */
  581. list[0] = pixels;
  582. /* recursively process vector */
  583. for (i=0, m=1, n=1, level=5; i < n; i++) {
  584. SVQ1_PROCESS_VECTOR();
  585. /* destination address and vector size */
  586. dst = (uint32_t *) list[i];
  587. width = 1 << ((4 + level) /2);
  588. height = 1 << ((3 + level) /2);
  589. /* get number of stages (-1 skips vector, 0 for mean only) */
  590. bit_cache = get_bit_cache (bitbuf);
  591. vlc = &svq1_intra_vector_tables[level][bit_cache >> (32 - 7)];
  592. /* flush bits */
  593. stages = vlc->value;
  594. skip_bits(bitbuf,vlc->length);
  595. if (stages == -1) {
  596. for (y=0; y < height; y++) {
  597. memset (&dst[y*(pitch / 4)], 0, width);
  598. }
  599. continue; /* skip vector */
  600. }
  601. if ((stages > 0) && (level >= 4)) {
  602. #ifdef DEBUG_SVQ1
  603. printf("Error (svq1_decode_block_intra): invalid vector: stages=%i level=%i\n",stages,level);
  604. #endif
  605. return -1; /* invalid vector */
  606. }
  607. /* get mean value for vector */
  608. bit_cache = get_bit_cache (bitbuf);
  609. if (bit_cache >= 0x25000000)
  610. vlc = &svq1_intra_mean_table_0[(bit_cache >> (32 - 8)) - 37];
  611. else if (bit_cache >= 0x03400000)
  612. vlc = &svq1_intra_mean_table_1[(bit_cache >> (32 - 10)) - 13];
  613. else if (bit_cache >= 0x00040000)
  614. vlc = &svq1_intra_mean_table_2[(bit_cache >> (32 - 14)) - 1];
  615. else
  616. vlc = &svq1_intra_mean_table_3[bit_cache >> (32 - 20)];
  617. /* flush bits */
  618. mean = vlc->value;
  619. skip_bits(bitbuf,vlc->length);
  620. if (stages == 0) {
  621. for (y=0; y < height; y++) {
  622. memset (&dst[y*(pitch / 4)], mean, width);
  623. }
  624. } else {
  625. SVQ1_CALC_CODEBOOK_ENTRIES(svq1_intra_codebooks);
  626. SVQ1_DO_CODEBOOK_INTRA()
  627. }
  628. }
  629. return 0;
  630. }
  631. static int svq1_decode_block_non_intra (bit_buffer_t *bitbuf, uint8_t *pixels, int pitch ) {
  632. uint32_t bit_cache;
  633. vlc_code_t *vlc;
  634. uint8_t *list[63];
  635. uint32_t *dst;
  636. const uint32_t *codebook;
  637. int entries[6];
  638. int i, j, m, n;
  639. int mean, stages;
  640. int x, y, width, height, level;
  641. uint32_t n1, n2, n3, n4;
  642. /* initialize list for breadth first processing of vectors */
  643. list[0] = pixels;
  644. /* recursively process vector */
  645. for (i=0, m=1, n=1, level=5; i < n; i++) {
  646. SVQ1_PROCESS_VECTOR();
  647. /* destination address and vector size */
  648. dst = (uint32_t *) list[i];
  649. width = 1 << ((4 + level) /2);
  650. height = 1 << ((3 + level) /2);
  651. /* get number of stages (-1 skips vector, 0 for mean only) */
  652. bit_cache = get_bit_cache (bitbuf);
  653. vlc = &svq1_inter_vector_tables[level][bit_cache >> (32 - 6)];
  654. /* flush bits */
  655. stages = vlc->value;
  656. skip_bits(bitbuf,vlc->length);
  657. if (stages == -1) continue; /* skip vector */
  658. if ((stages > 0) && (level >= 4)) {
  659. #ifdef DEBUG_SVQ1
  660. printf("Error (svq1_decode_block_non_intra): invalid vector: stages=%i level=%i\n",stages,level);
  661. #endif
  662. return -1; /* invalid vector */
  663. }
  664. /* get mean value for vector */
  665. bit_cache = get_bit_cache (bitbuf);
  666. if (bit_cache >= 0x0B000000)
  667. vlc = &svq1_inter_mean_table_0[(bit_cache >> (32 - 8)) - 11];
  668. else if (bit_cache >= 0x01200000)
  669. vlc = &svq1_inter_mean_table_1[(bit_cache >> (32 - 12)) - 18];
  670. else if (bit_cache >= 0x002E0000)
  671. vlc = &svq1_inter_mean_table_2[(bit_cache >> (32 - 15)) - 23];
  672. else if (bit_cache >= 0x00094000)
  673. vlc = &svq1_inter_mean_table_3[(bit_cache >> (32 - 18)) - 37];
  674. else if (bit_cache >= 0x00049000)
  675. vlc = &svq1_inter_mean_table_4[(bit_cache >> (32 - 20)) - 73];
  676. else
  677. vlc = &svq1_inter_mean_table_5[bit_cache >> (32 - 22)];
  678. /* flush bits */
  679. mean = vlc->value;
  680. skip_bits(bitbuf,vlc->length);
  681. SVQ1_CALC_CODEBOOK_ENTRIES(svq1_inter_codebooks);
  682. SVQ1_DO_CODEBOOK_NONINTRA()
  683. }
  684. return 0;
  685. }
  686. static int svq1_decode_motion_vector (bit_buffer_t *bitbuf, svq1_pmv_t *mv, svq1_pmv_t **pmv) {
  687. uint32_t bit_cache;
  688. vlc_code_t *vlc;
  689. int diff, sign;
  690. int i;
  691. for (i=0; i < 2; i++) {
  692. /* get motion code */
  693. bit_cache = get_bit_cache (bitbuf);
  694. if (!(bit_cache & 0xFFE00000))
  695. return -1; /* invalid vlc code */
  696. if (bit_cache & 0x80000000) {
  697. diff = 0;
  698. /* flush bit */
  699. skip_bits(bitbuf,1);
  700. } else {
  701. if (bit_cache >= 0x06000000) {
  702. vlc = &svq1_motion_table_0[(bit_cache >> (32 - 7)) - 3];
  703. } else {
  704. vlc = &svq1_motion_table_1[(bit_cache >> (32 - 12)) - 2];
  705. }
  706. /* decode motion vector differential */
  707. sign = (int) (bit_cache << (vlc->length - 1)) >> 31;
  708. diff = (vlc->value ^ sign) - sign;
  709. /* flush bits */
  710. skip_bits(bitbuf,vlc->length);
  711. }
  712. /* add median of motion vector predictors and clip result */
  713. if (i == 1)
  714. mv->y = ((diff + MEDIAN(pmv[0]->y, pmv[1]->y, pmv[2]->y)) << 26) >> 26;
  715. else
  716. mv->x = ((diff + MEDIAN(pmv[0]->x, pmv[1]->x, pmv[2]->x)) << 26) >> 26;
  717. }
  718. return 0;
  719. }
  720. static void svq1_skip_block (uint8_t *current, uint8_t *previous, int pitch, int x, int y) {
  721. uint8_t *src;
  722. uint8_t *dst;
  723. int i;
  724. src = &previous[x + y*pitch];
  725. dst = current;
  726. for (i=0; i < 16; i++) {
  727. memcpy (dst, src, 16);
  728. src += pitch;
  729. dst += pitch;
  730. }
  731. }
  732. static int svq1_motion_inter_block (MpegEncContext *s, bit_buffer_t *bitbuf,
  733. uint8_t *current, uint8_t *previous, int pitch,
  734. svq1_pmv_t *motion, int x, int y) {
  735. uint8_t *src;
  736. uint8_t *dst;
  737. svq1_pmv_t mv;
  738. svq1_pmv_t *pmv[3];
  739. int result;
  740. /* predict and decode motion vector */
  741. pmv[0] = &motion[0];
  742. if (y == 0) {
  743. pmv[1] =
  744. pmv[2] = pmv[0];
  745. }
  746. else {
  747. pmv[1] = &motion[(x / 8) + 2];
  748. pmv[2] = &motion[(x / 8) + 4];
  749. }
  750. result = svq1_decode_motion_vector (bitbuf, &mv, pmv);
  751. if (result != 0)
  752. return result;
  753. motion[0].x =
  754. motion[(x / 8) + 2].x =
  755. motion[(x / 8) + 3].x = mv.x;
  756. motion[0].y =
  757. motion[(x / 8) + 2].y =
  758. motion[(x / 8) + 3].y = mv.y;
  759. if(y + (mv.y >> 1)<0)
  760. mv.y= 0;
  761. if(x + (mv.x >> 1)<0)
  762. mv.x= 0;
  763. #if 0
  764. int w= (s->width+15)&~15;
  765. int h= (s->height+15)&~15;
  766. if(x + (mv.x >> 1)<0 || y + (mv.y >> 1)<0 || x + (mv.x >> 1) + 16 > w || y + (mv.y >> 1) + 16> h)
  767. printf("%d %d %d %d\n", x, y, x + (mv.x >> 1), y + (mv.y >> 1));
  768. #endif
  769. src = &previous[(x + (mv.x >> 1)) + (y + (mv.y >> 1))*pitch];
  770. dst = current;
  771. s->dsp.put_pixels_tab[0][((mv.y & 1) << 1) | (mv.x & 1)](dst,src,pitch,16);
  772. return 0;
  773. }
  774. static int svq1_motion_inter_4v_block (MpegEncContext *s, bit_buffer_t *bitbuf,
  775. uint8_t *current, uint8_t *previous, int pitch,
  776. svq1_pmv_t *motion,int x, int y) {
  777. uint8_t *src;
  778. uint8_t *dst;
  779. svq1_pmv_t mv;
  780. svq1_pmv_t *pmv[4];
  781. int i, result;
  782. /* predict and decode motion vector (0) */
  783. pmv[0] = &motion[0];
  784. if (y == 0) {
  785. pmv[1] =
  786. pmv[2] = pmv[0];
  787. }
  788. else {
  789. pmv[1] = &motion[(x / 8) + 2];
  790. pmv[2] = &motion[(x / 8) + 4];
  791. }
  792. result = svq1_decode_motion_vector (bitbuf, &mv, pmv);
  793. if (result != 0)
  794. return result;
  795. /* predict and decode motion vector (1) */
  796. pmv[0] = &mv;
  797. if (y == 0) {
  798. pmv[1] =
  799. pmv[2] = pmv[0];
  800. }
  801. else {
  802. pmv[1] = &motion[(x / 8) + 3];
  803. }
  804. result = svq1_decode_motion_vector (bitbuf, &motion[0], pmv);
  805. if (result != 0)
  806. return result;
  807. /* predict and decode motion vector (2) */
  808. pmv[1] = &motion[0];
  809. pmv[2] = &motion[(x / 8) + 1];
  810. result = svq1_decode_motion_vector (bitbuf, &motion[(x / 8) + 2], pmv);
  811. if (result != 0)
  812. return result;
  813. /* predict and decode motion vector (3) */
  814. pmv[2] = &motion[(x / 8) + 2];
  815. pmv[3] = &motion[(x / 8) + 3];
  816. result = svq1_decode_motion_vector (bitbuf, pmv[3], pmv);
  817. if (result != 0)
  818. return result;
  819. /* form predictions */
  820. for (i=0; i < 4; i++) {
  821. int mvx= pmv[i]->x + (i&1)*16;
  822. int mvy= pmv[i]->y + (i>>1)*16;
  823. ///XXX /FIXME cliping or padding?
  824. if(y + (mvy >> 1)<0)
  825. mvy= 0;
  826. if(x + (mvx >> 1)<0)
  827. mvx= 0;
  828. #if 0
  829. int w= (s->width+15)&~15;
  830. int h= (s->height+15)&~15;
  831. if(x + (mvx >> 1)<0 || y + (mvy >> 1)<0 || x + (mvx >> 1) + 8 > w || y + (mvy >> 1) + 8> h)
  832. printf("%d %d %d %d\n", x, y, x + (mvx >> 1), y + (mvy >> 1));
  833. #endif
  834. src = &previous[(x + (mvx >> 1)) + (y + (mvy >> 1))*pitch];
  835. dst = current;
  836. s->dsp.put_pixels_tab[1][((mvy & 1) << 1) | (mvx & 1)](dst,src,pitch,8);
  837. /* select next block */
  838. if (i & 1) {
  839. current += 8*(pitch - 1);
  840. } else {
  841. current += 8;
  842. }
  843. }
  844. return 0;
  845. }
  846. static int svq1_decode_delta_block (MpegEncContext *s, bit_buffer_t *bitbuf,
  847. uint8_t *current, uint8_t *previous, int pitch,
  848. svq1_pmv_t *motion, int x, int y) {
  849. uint32_t bit_cache;
  850. uint32_t block_type;
  851. int result = 0;
  852. /* get block type */
  853. bit_cache = get_bit_cache (bitbuf);
  854. bit_cache >>= (32 - 3);
  855. block_type = svq1_block_type_table[bit_cache].value;
  856. skip_bits(bitbuf,svq1_block_type_table[bit_cache].length);
  857. /* reset motion vectors */
  858. if (block_type == SVQ1_BLOCK_SKIP || block_type == SVQ1_BLOCK_INTRA) {
  859. motion[0].x =
  860. motion[0].y =
  861. motion[(x / 8) + 2].x =
  862. motion[(x / 8) + 2].y =
  863. motion[(x / 8) + 3].x =
  864. motion[(x / 8) + 3].y = 0;
  865. }
  866. switch (block_type) {
  867. case SVQ1_BLOCK_SKIP:
  868. svq1_skip_block (current, previous, pitch, x, y);
  869. break;
  870. case SVQ1_BLOCK_INTER:
  871. result = svq1_motion_inter_block (s, bitbuf, current, previous, pitch, motion, x, y);
  872. if (result != 0)
  873. {
  874. #ifdef DEBUG_SVQ1
  875. printf("Error in svq1_motion_inter_block %i\n",result);
  876. #endif
  877. break;
  878. }
  879. result = svq1_decode_block_non_intra (bitbuf, current, pitch);
  880. break;
  881. case SVQ1_BLOCK_INTER_4V:
  882. result = svq1_motion_inter_4v_block (s, bitbuf, current, previous, pitch, motion, x, y);
  883. if (result != 0)
  884. {
  885. #ifdef DEBUG_SVQ1
  886. printf("Error in svq1_motion_inter_4v_block %i\n",result);
  887. #endif
  888. break;
  889. }
  890. result = svq1_decode_block_non_intra (bitbuf, current, pitch);
  891. break;
  892. case SVQ1_BLOCK_INTRA:
  893. result = svq1_decode_block_intra (bitbuf, current, pitch);
  894. break;
  895. }
  896. return result;
  897. }
  898. /* standard video sizes */
  899. static struct { int width; int height; } svq1_frame_size_table[8] = {
  900. { 160, 120 }, { 128, 96 }, { 176, 144 }, { 352, 288 },
  901. { 704, 576 }, { 240, 180 }, { 320, 240 }, { -1, -1 }
  902. };
  903. static int svq1_decode_frame_header (bit_buffer_t *bitbuf,MpegEncContext *s) {
  904. int frame_size_code;
  905. /* unknown field */
  906. get_bits (bitbuf, 8);
  907. /* frame type */
  908. s->pict_type= get_bits (bitbuf, 2)+1;
  909. if(s->pict_type==4)
  910. return -1;
  911. if (s->pict_type == I_TYPE) {
  912. /* unknown fields */
  913. if (s->f_code == 0x50 || s->f_code == 0x60) {
  914. get_bits (bitbuf, 16);
  915. }
  916. if ((s->f_code ^ 0x10) >= 0x50) {
  917. skip_bits(bitbuf,8*get_bits (bitbuf, 8));
  918. }
  919. get_bits (bitbuf, 2);
  920. get_bits (bitbuf, 2);
  921. get_bits (bitbuf, 1);
  922. /* load frame size */
  923. frame_size_code = get_bits (bitbuf, 3);
  924. if (frame_size_code == 7) {
  925. /* load width, height (12 bits each) */
  926. s->width = get_bits (bitbuf, 12);
  927. s->height = get_bits (bitbuf, 12);
  928. if (!s->width || !s->height)
  929. return -1;
  930. } else {
  931. /* get width, height from table */
  932. s->width = svq1_frame_size_table[frame_size_code].width;
  933. s->height = svq1_frame_size_table[frame_size_code].height;
  934. }
  935. }
  936. /* unknown fields */
  937. if (get_bits (bitbuf, 1) == 1) {
  938. get_bits (bitbuf, 1);
  939. get_bits (bitbuf, 1);
  940. if (get_bits (bitbuf, 2) != 0)
  941. return -1;
  942. }
  943. if (get_bits (bitbuf, 1) == 1) {
  944. get_bits (bitbuf, 1);
  945. get_bits (bitbuf, 4);
  946. get_bits (bitbuf, 1);
  947. get_bits (bitbuf, 2);
  948. while (get_bits (bitbuf, 1) == 1) {
  949. get_bits (bitbuf, 8);
  950. }
  951. }
  952. return 0;
  953. }
  954. static int svq1_decode_frame(AVCodecContext *avctx,
  955. void *data, int *data_size,
  956. uint8_t *buf, int buf_size)
  957. {
  958. MpegEncContext *s=avctx->priv_data;
  959. uint8_t *current, *previous;
  960. int result, i, x, y, width, height;
  961. AVFrame *pict = data;
  962. /* initialize bit buffer */
  963. init_get_bits(&s->gb,buf,buf_size*8);
  964. /* decode frame header */
  965. s->f_code = get_bits (&s->gb, 22);
  966. if ((s->f_code & ~0x70) || !(s->f_code & 0x60))
  967. return -1;
  968. /* swap some header bytes (why?) */
  969. if (s->f_code != 0x20) {
  970. uint32_t *src = (uint32_t *) (buf + 4);
  971. for (i=0; i < 4; i++) {
  972. src[i] = ((src[i] << 16) | (src[i] >> 16)) ^ src[7 - i];
  973. }
  974. }
  975. result = svq1_decode_frame_header (&s->gb, s);
  976. if (result != 0)
  977. {
  978. #ifdef DEBUG_SVQ1
  979. printf("Error in svq1_decode_frame_header %i\n",result);
  980. #endif
  981. return result;
  982. }
  983. //FIXME this avoids some confusion for "B frames" without 2 references
  984. //this should be removed after libavcodec can handle more flaxible picture types & ordering
  985. if(s->pict_type==B_TYPE && s->last_picture_ptr==NULL) return buf_size;
  986. if(avctx->hurry_up && s->pict_type==B_TYPE) return buf_size;
  987. if(MPV_frame_start(s, avctx) < 0)
  988. return -1;
  989. /* decode y, u and v components */
  990. for (i=0; i < 3; i++) {
  991. int linesize;
  992. if (i == 0) {
  993. width = (s->width+15)&~15;
  994. height = (s->height+15)&~15;
  995. linesize= s->linesize;
  996. } else {
  997. if(s->flags&CODEC_FLAG_GRAY) break;
  998. width = (s->width/4+15)&~15;
  999. height = (s->height/4+15)&~15;
  1000. linesize= s->uvlinesize;
  1001. }
  1002. current = s->current_picture.data[i];
  1003. if(s->pict_type==B_TYPE){
  1004. previous = s->next_picture.data[i];
  1005. }else{
  1006. previous = s->last_picture.data[i];
  1007. }
  1008. if (s->pict_type == I_TYPE) {
  1009. /* keyframe */
  1010. for (y=0; y < height; y+=16) {
  1011. for (x=0; x < width; x+=16) {
  1012. result = svq1_decode_block_intra (&s->gb, &current[x], linesize);
  1013. if (result != 0)
  1014. {
  1015. #ifdef DEBUG_SVQ1
  1016. printf("Error in svq1_decode_block %i (keyframe)\n",result);
  1017. #endif
  1018. return result;
  1019. }
  1020. }
  1021. current += 16*linesize;
  1022. }
  1023. } else {
  1024. svq1_pmv_t pmv[width/8+3];
  1025. /* delta frame */
  1026. memset (pmv, 0, ((width / 8) + 3) * sizeof(svq1_pmv_t));
  1027. for (y=0; y < height; y+=16) {
  1028. for (x=0; x < width; x+=16) {
  1029. result = svq1_decode_delta_block (s, &s->gb, &current[x], previous,
  1030. linesize, pmv, x, y);
  1031. if (result != 0)
  1032. {
  1033. #ifdef DEBUG_SVQ1
  1034. printf("Error in svq1_decode_delta_block %i\n",result);
  1035. #endif
  1036. return result;
  1037. }
  1038. }
  1039. pmv[0].x =
  1040. pmv[0].y = 0;
  1041. current += 16*linesize;
  1042. }
  1043. }
  1044. }
  1045. *pict = *(AVFrame*)&s->current_picture;
  1046. MPV_frame_end(s);
  1047. *data_size=sizeof(AVFrame);
  1048. return buf_size;
  1049. }
  1050. static int svq1_decode_init(AVCodecContext *avctx)
  1051. {
  1052. MpegEncContext *s = avctx->priv_data;
  1053. s->avctx = avctx;
  1054. s->width = (avctx->width+3)&~3;
  1055. s->height = (avctx->height+3)&~3;
  1056. s->codec_id= avctx->codec->id;
  1057. avctx->pix_fmt = PIX_FMT_YUV410P;
  1058. avctx->has_b_frames= 1; // not true, but DP frames and these behave like unidirectional b frames
  1059. s->flags= avctx->flags;
  1060. if (MPV_common_init(s) < 0) return -1;
  1061. return 0;
  1062. }
  1063. static int svq1_decode_end(AVCodecContext *avctx)
  1064. {
  1065. MpegEncContext *s = avctx->priv_data;
  1066. MPV_common_end(s);
  1067. return 0;
  1068. }
  1069. AVCodec svq1_decoder = {
  1070. "svq1",
  1071. CODEC_TYPE_VIDEO,
  1072. CODEC_ID_SVQ1,
  1073. sizeof(MpegEncContext),
  1074. svq1_decode_init,
  1075. NULL,
  1076. svq1_decode_end,
  1077. svq1_decode_frame,
  1078. CODEC_CAP_DR1,
  1079. };