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.

777 lines
23KB

  1. /*
  2. * Copyright (c) 2003 The FFmpeg Project.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. *
  19. * How to use this decoder:
  20. * SVQ3 data is transported within Apple Quicktime files. Quicktime files
  21. * have stsd atoms to describe media trak properties. A stsd atom for a
  22. * video trak contains 1 or more ImageDescription atoms. These atoms begin
  23. * with the 4-byte length of the atom followed by the codec fourcc. Some
  24. * decoders need information in this atom to operate correctly. Such
  25. * is the case with SVQ3. In order to get the best use out of this decoder,
  26. * the calling app must make the SVQ3 ImageDescription atom available
  27. * via the AVCodecContext's extradata[_size] field:
  28. *
  29. * AVCodecContext.extradata = pointer to ImageDescription, first characters
  30. * are expected to be 'S', 'V', 'Q', and '3', NOT the 4-byte atom length
  31. * AVCodecContext.extradata_size = size of ImageDescription atom memory
  32. * buffer (which will be the same as the ImageDescription atom size field
  33. * from the QT file, minus 4 bytes since the length is missing)
  34. *
  35. * You will know you have these parameters passed correctly when the decoder
  36. * correctly decodes this file:
  37. * ftp://ftp.mplayerhq.hu/MPlayer/samples/V-codecs/SVQ3/Vertical400kbit.sorenson3.mov
  38. *
  39. */
  40. /**
  41. * @file svq3.c
  42. * svq3 decoder.
  43. */
  44. static const uint8_t svq3_scan[16]={
  45. 0+0*4, 1+0*4, 2+0*4, 2+1*4,
  46. 2+2*4, 3+0*4, 3+1*4, 3+2*4,
  47. 0+1*4, 0+2*4, 1+1*4, 1+2*4,
  48. 0+3*4, 1+3*4, 2+3*4, 3+3*4,
  49. };
  50. static const uint8_t svq3_pred_0[25][2] = {
  51. { 0, 0 },
  52. { 1, 0 }, { 0, 1 },
  53. { 0, 2 }, { 1, 1 }, { 2, 0 },
  54. { 3, 0 }, { 2, 1 }, { 1, 2 }, { 0, 3 },
  55. { 0, 4 }, { 1, 3 }, { 2, 2 }, { 3, 1 }, { 4, 0 },
  56. { 4, 1 }, { 3, 2 }, { 2, 3 }, { 1, 4 },
  57. { 2, 4 }, { 3, 3 }, { 4, 2 },
  58. { 4, 3 }, { 3, 4 },
  59. { 4, 4 }
  60. };
  61. static const int8_t svq3_pred_1[6][6][5] = {
  62. { { 2,-1,-1,-1,-1 }, { 2, 1,-1,-1,-1 }, { 1, 2,-1,-1,-1 },
  63. { 2, 1,-1,-1,-1 }, { 1, 2,-1,-1,-1 }, { 1, 2,-1,-1,-1 } },
  64. { { 0, 2,-1,-1,-1 }, { 0, 2, 1, 4, 3 }, { 0, 1, 2, 4, 3 },
  65. { 0, 2, 1, 4, 3 }, { 2, 0, 1, 3, 4 }, { 0, 4, 2, 1, 3 } },
  66. { { 2, 0,-1,-1,-1 }, { 2, 1, 0, 4, 3 }, { 1, 2, 4, 0, 3 },
  67. { 2, 1, 0, 4, 3 }, { 2, 1, 4, 3, 0 }, { 1, 2, 4, 0, 3 } },
  68. { { 2, 0,-1,-1,-1 }, { 2, 0, 1, 4, 3 }, { 1, 2, 0, 4, 3 },
  69. { 2, 1, 0, 4, 3 }, { 2, 1, 3, 4, 0 }, { 2, 4, 1, 0, 3 } },
  70. { { 0, 2,-1,-1,-1 }, { 0, 2, 1, 3, 4 }, { 1, 2, 3, 0, 4 },
  71. { 2, 0, 1, 3, 4 }, { 2, 1, 3, 0, 4 }, { 2, 0, 4, 3, 1 } },
  72. { { 0, 2,-1,-1,-1 }, { 0, 2, 4, 1, 3 }, { 1, 4, 2, 0, 3 },
  73. { 4, 2, 0, 1, 3 }, { 2, 0, 1, 4, 3 }, { 4, 2, 1, 0, 3 } },
  74. };
  75. static const struct { uint8_t run; uint8_t level; } svq3_dct_tables[2][16] = {
  76. { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 2, 1 }, { 0, 2 }, { 3, 1 }, { 4, 1 }, { 5, 1 },
  77. { 0, 3 }, { 1, 2 }, { 2, 2 }, { 6, 1 }, { 7, 1 }, { 8, 1 }, { 9, 1 }, { 0, 4 } },
  78. { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 0, 2 }, { 2, 1 }, { 0, 3 }, { 0, 4 }, { 0, 5 },
  79. { 3, 1 }, { 4, 1 }, { 1, 2 }, { 1, 3 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, { 0, 9 } }
  80. };
  81. static const uint32_t svq3_dequant_coeff[32] = {
  82. 3881, 4351, 4890, 5481, 6154, 6914, 7761, 8718,
  83. 9781, 10987, 12339, 13828, 15523, 17435, 19561, 21873,
  84. 24552, 27656, 30847, 34870, 38807, 43747, 49103, 54683,
  85. 61694, 68745, 77615, 89113,100253,109366,126635,141533
  86. };
  87. static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp){
  88. const int qmul= svq3_dequant_coeff[qp];
  89. #define stride 16
  90. int i;
  91. int temp[16];
  92. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  93. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  94. for(i=0; i<4; i++){
  95. const int offset= y_offset[i];
  96. const int z0= 13*(block[offset+stride*0] + block[offset+stride*4]);
  97. const int z1= 13*(block[offset+stride*0] - block[offset+stride*4]);
  98. const int z2= 7* block[offset+stride*1] - 17*block[offset+stride*5];
  99. const int z3= 17* block[offset+stride*1] + 7*block[offset+stride*5];
  100. temp[4*i+0]= z0+z3;
  101. temp[4*i+1]= z1+z2;
  102. temp[4*i+2]= z1-z2;
  103. temp[4*i+3]= z0-z3;
  104. }
  105. for(i=0; i<4; i++){
  106. const int offset= x_offset[i];
  107. const int z0= 13*(temp[4*0+i] + temp[4*2+i]);
  108. const int z1= 13*(temp[4*0+i] - temp[4*2+i]);
  109. const int z2= 7* temp[4*1+i] - 17*temp[4*3+i];
  110. const int z3= 17* temp[4*1+i] + 7*temp[4*3+i];
  111. block[stride*0 +offset]= ((z0 + z3)*qmul + 0x80000)>>20;
  112. block[stride*2 +offset]= ((z1 + z2)*qmul + 0x80000)>>20;
  113. block[stride*8 +offset]= ((z1 - z2)*qmul + 0x80000)>>20;
  114. block[stride*10+offset]= ((z0 - z3)*qmul + 0x80000)>>20;
  115. }
  116. }
  117. #undef stride
  118. static void svq3_add_idct_c (uint8_t *dst, DCTELEM *block, int stride, int qp, int dc){
  119. const int qmul= svq3_dequant_coeff[qp];
  120. int i;
  121. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  122. if (dc) {
  123. dc = 13*13*((dc == 1) ? 1538*block[0] : ((qmul*(block[0] >> 3)) / 2));
  124. block[0] = 0;
  125. }
  126. for (i=0; i < 4; i++) {
  127. const int z0= 13*(block[0 + 4*i] + block[2 + 4*i]);
  128. const int z1= 13*(block[0 + 4*i] - block[2 + 4*i]);
  129. const int z2= 7* block[1 + 4*i] - 17*block[3 + 4*i];
  130. const int z3= 17* block[1 + 4*i] + 7*block[3 + 4*i];
  131. block[0 + 4*i]= z0 + z3;
  132. block[1 + 4*i]= z1 + z2;
  133. block[2 + 4*i]= z1 - z2;
  134. block[3 + 4*i]= z0 - z3;
  135. }
  136. for (i=0; i < 4; i++) {
  137. const int z0= 13*(block[i + 4*0] + block[i + 4*2]);
  138. const int z1= 13*(block[i + 4*0] - block[i + 4*2]);
  139. const int z2= 7* block[i + 4*1] - 17*block[i + 4*3];
  140. const int z3= 17* block[i + 4*1] + 7*block[i + 4*3];
  141. const int rr= (dc + 0x80000);
  142. dst[i + stride*0]= cm[ dst[i + stride*0] + (((z0 + z3)*qmul + rr) >> 20) ];
  143. dst[i + stride*1]= cm[ dst[i + stride*1] + (((z1 + z2)*qmul + rr) >> 20) ];
  144. dst[i + stride*2]= cm[ dst[i + stride*2] + (((z1 - z2)*qmul + rr) >> 20) ];
  145. dst[i + stride*3]= cm[ dst[i + stride*3] + (((z0 - z3)*qmul + rr) >> 20) ];
  146. }
  147. }
  148. static void pred4x4_down_left_svq3_c(uint8_t *src, uint8_t *topright, int stride){
  149. LOAD_TOP_EDGE
  150. LOAD_LEFT_EDGE
  151. const __attribute__((unused)) int unu0= t0;
  152. const __attribute__((unused)) int unu1= l0;
  153. src[0+0*stride]=(l1 + t1)>>1;
  154. src[1+0*stride]=
  155. src[0+1*stride]=(l2 + t2)>>1;
  156. src[2+0*stride]=
  157. src[1+1*stride]=
  158. src[0+2*stride]=
  159. src[3+0*stride]=
  160. src[2+1*stride]=
  161. src[1+2*stride]=
  162. src[0+3*stride]=
  163. src[3+1*stride]=
  164. src[2+2*stride]=
  165. src[1+3*stride]=
  166. src[3+2*stride]=
  167. src[2+3*stride]=
  168. src[3+3*stride]=(l3 + t3)>>1;
  169. };
  170. static void pred16x16_plane_svq3_c(uint8_t *src, int stride){
  171. pred16x16_plane_compat_c(src, stride, 1);
  172. }
  173. static inline int svq3_decode_block (GetBitContext *gb, DCTELEM *block,
  174. int index, const int type) {
  175. static const uint8_t *const scan_patterns[4] =
  176. { luma_dc_zigzag_scan, zigzag_scan, svq3_scan, chroma_dc_scan };
  177. int run, level, sign, vlc, limit;
  178. const int intra = (3 * type) >> 2;
  179. const uint8_t *const scan = scan_patterns[type];
  180. for (limit=(16 >> intra); index < 16; index=limit, limit+=8) {
  181. for (; (vlc = svq3_get_ue_golomb (gb)) != 0; index++) {
  182. if (vlc == INVALID_VLC)
  183. return -1;
  184. sign = (vlc & 0x1) - 1;
  185. vlc = (vlc + 1) >> 1;
  186. if (type == 3) {
  187. if (vlc < 3) {
  188. run = 0;
  189. level = vlc;
  190. } else if (vlc < 4) {
  191. run = 1;
  192. level = 1;
  193. } else {
  194. run = (vlc & 0x3);
  195. level = ((vlc + 9) >> 2) - run;
  196. }
  197. } else {
  198. if (vlc < 16) {
  199. run = svq3_dct_tables[intra][vlc].run;
  200. level = svq3_dct_tables[intra][vlc].level;
  201. } else if (intra) {
  202. run = (vlc & 0x7);
  203. level = (vlc >> 3) + ((run == 0) ? 8 : ((run < 2) ? 2 : ((run < 5) ? 0 : -1)));
  204. } else {
  205. run = (vlc & 0xF);
  206. level = (vlc >> 4) + ((run == 0) ? 4 : ((run < 3) ? 2 : ((run < 10) ? 1 : 0)));
  207. }
  208. }
  209. if ((index += run) >= limit)
  210. return -1;
  211. block[scan[index]] = (level ^ sign) - sign;
  212. }
  213. if (type != 2) {
  214. break;
  215. }
  216. }
  217. return 0;
  218. }
  219. static void sixpel_mc_put (MpegEncContext *s,
  220. uint8_t *src, uint8_t *dst, int stride,
  221. int dxy, int width, int height) {
  222. int i, j;
  223. switch (dxy) {
  224. case 6*0+0:
  225. for (i=0; i < height; i++) {
  226. memcpy (dst, src, width);
  227. src += stride;
  228. dst += stride;
  229. }
  230. break;
  231. case 6*0+2:
  232. for (i=0; i < height; i++) {
  233. for (j=0; j < width; j++) {
  234. dst[j] = (683*(2*src[j] + src[j+1] + 1)) >> 11;
  235. }
  236. src += stride;
  237. dst += stride;
  238. }
  239. break;
  240. case 6*0+3:
  241. for (i=0; i < height; i++) {
  242. for (j=0; j < width; j++) {
  243. dst[j] = (src[j] + src[j+1] + 1) >> 1;
  244. }
  245. src += stride;
  246. dst += stride;
  247. }
  248. break;
  249. case 6*0+4:
  250. for (i=0; i < height; i++) {
  251. for (j=0; j < width; j++) {
  252. dst[j] = (683*(src[j] + 2*src[j+1] + 1)) >> 11;
  253. }
  254. src += stride;
  255. dst += stride;
  256. }
  257. break;
  258. case 6*2+0:
  259. for (i=0; i < height; i++) {
  260. for (j=0; j < width; j++) {
  261. dst[j] = (683*(2*src[j] + src[j+stride] + 1)) >> 11;
  262. }
  263. src += stride;
  264. dst += stride;
  265. }
  266. break;
  267. case 6*2+2:
  268. for (i=0; i < height; i++) {
  269. for (j=0; j < width; j++) {
  270. dst[j] = (2731*(4*src[j] + 3*src[j+1] + 3*src[j+stride] + 2*src[j+stride+1] + 6)) >> 15;
  271. }
  272. src += stride;
  273. dst += stride;
  274. }
  275. break;
  276. case 6*2+4:
  277. for (i=0; i < height; i++) {
  278. for (j=0; j < width; j++) {
  279. dst[j] = (2731*(3*src[j] + 4*src[j+1] + 2*src[j+stride] + 3*src[j+stride+1] + 6)) >> 15;
  280. }
  281. src += stride;
  282. dst += stride;
  283. }
  284. break;
  285. case 6*3+0:
  286. for (i=0; i < height; i++) {
  287. for (j=0; j < width; j++) {
  288. dst[j] = (src[j] + src[j+stride]+1) >> 1;
  289. }
  290. src += stride;
  291. dst += stride;
  292. }
  293. break;
  294. case 6*3+3:
  295. for (i=0; i < height; i++) {
  296. for (j=0; j < width; j++) {
  297. dst[j] = (src[j] + src[j+1] + src[j+stride] + src[j+stride+1] + 2) >> 2;
  298. }
  299. src += stride;
  300. dst += stride;
  301. }
  302. break;
  303. case 6*4+0:
  304. for (i=0; i < height; i++) {
  305. for (j=0; j < width; j++) {
  306. dst[j] = (683*(src[j] + 2*src[j+stride] + 1)) >> 11;
  307. }
  308. src += stride;
  309. dst += stride;
  310. }
  311. break;
  312. case 6*4+2:
  313. for (i=0; i < height; i++) {
  314. for (j=0; j < width; j++) {
  315. dst[j] = (2731*(3*src[j] + 2*src[j+1] + 4*src[j+stride] + 3*src[j+stride+1] + 6)) >> 15;
  316. }
  317. src += stride;
  318. dst += stride;
  319. }
  320. break;
  321. case 6*4+4:
  322. for (i=0; i < height; i++) {
  323. for (j=0; j < width; j++) {
  324. dst[j] = (2731*(2*src[j] + 3*src[j+1] + 3*src[j+stride] + 4*src[j+stride+1] + 6)) >> 15;
  325. }
  326. src += stride;
  327. dst += stride;
  328. }
  329. break;
  330. }
  331. }
  332. static inline void svq3_mc_dir_part (MpegEncContext *s, int x, int y,
  333. int width, int height, int mx, int my) {
  334. uint8_t *src, *dest;
  335. int i, emu = 0;
  336. const int sx = ((unsigned) (mx + 0x7FFFFFFE)) % 6;
  337. const int sy = ((unsigned) (my + 0x7FFFFFFE)) % 6;
  338. const int dxy= 6*sy + sx;
  339. /* decode and clip motion vector to frame border (+16) */
  340. mx = x + (mx - sx) / 6;
  341. my = y + (my - sy) / 6;
  342. if (mx < 0 || mx >= (s->width - width - 1) ||
  343. my < 0 || my >= (s->height - height - 1)) {
  344. if ((s->flags & CODEC_FLAG_EMU_EDGE)) {
  345. emu = 1;
  346. }
  347. mx = clip (mx, -16, (s->width - width + 15));
  348. my = clip (my, -16, (s->height - height + 15));
  349. }
  350. /* form component predictions */
  351. dest = s->current_picture.data[0] + x + y*s->linesize;
  352. src = s->last_picture.data[0] + mx + my*s->linesize;
  353. if (emu) {
  354. ff_emulated_edge_mc (s, src, s->linesize, (width + 1), (height + 1),
  355. mx, my, s->width, s->height);
  356. src = s->edge_emu_buffer;
  357. }
  358. sixpel_mc_put (s, src, dest, s->linesize, dxy, width, height);
  359. if (!(s->flags & CODEC_FLAG_GRAY)) {
  360. mx = (mx + (mx < (int) x)) >> 1;
  361. my = (my + (my < (int) y)) >> 1;
  362. width = (width >> 1);
  363. height = (height >> 1);
  364. for (i=1; i < 3; i++) {
  365. dest = s->current_picture.data[i] + (x >> 1) + (y >> 1)*s->uvlinesize;
  366. src = s->last_picture.data[i] + mx + my*s->uvlinesize;
  367. if (emu) {
  368. ff_emulated_edge_mc (s, src, s->uvlinesize, (width + 1), (height + 1),
  369. mx, my, (s->width >> 1), (s->height >> 1));
  370. src = s->edge_emu_buffer;
  371. }
  372. sixpel_mc_put (s, src, dest, s->uvlinesize, dxy, width, height);
  373. }
  374. }
  375. }
  376. static int svq3_decode_mb (H264Context *h, unsigned int mb_type) {
  377. int cbp, dir, mode, mx, my, dx, dy, x, y, part_width, part_height;
  378. int i, j, k, l, m;
  379. uint32_t vlc;
  380. int8_t *top, *left;
  381. MpegEncContext *const s = (MpegEncContext *) h;
  382. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  383. const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  384. h->top_samples_available = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
  385. h->left_samples_available = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
  386. h->topright_samples_available = 0xFFFF;
  387. if (mb_type == 0) { /* SKIP */
  388. svq3_mc_dir_part (s, 16*s->mb_x, 16*s->mb_y, 16, 16, 0, 0);
  389. cbp = 0;
  390. mb_type = MB_TYPE_SKIP;
  391. } else if (mb_type < 8) { /* INTER */
  392. if (h->thirdpel_flag && h->halfpel_flag == !get_bits (&s->gb, 1)) {
  393. mode = 3; /* thirdpel */
  394. } else if (h->halfpel_flag && h->thirdpel_flag == !get_bits (&s->gb, 1)) {
  395. mode = 2; /* halfpel */
  396. } else {
  397. mode = 1; /* fullpel */
  398. }
  399. /* fill caches */
  400. memset (h->ref_cache[0], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
  401. if (s->mb_x > 0) {
  402. for (i=0; i < 4; i++) {
  403. *(uint32_t *) h->mv_cache[0][scan8[0] - 1 + i*8] = *(uint32_t *) s->current_picture.motion_val[0][b_xy - 1 + i*h->b_stride];
  404. h->ref_cache[0][scan8[0] - 1 + i*8] = 1;
  405. }
  406. } else {
  407. for (i=0; i < 4; i++) {
  408. *(uint32_t *) h->mv_cache[0][scan8[0] - 1 + i*8] = 0;
  409. h->ref_cache[0][scan8[0] - 1 + i*8] = 1;
  410. }
  411. }
  412. if (s->mb_y > 0) {
  413. memcpy (h->mv_cache[0][scan8[0] - 1*8], s->current_picture.motion_val[0][b_xy - h->b_stride], 4*2*sizeof(int16_t));
  414. memset (&h->ref_cache[0][scan8[0] - 1*8], 1, 4);
  415. if (s->mb_x < (s->mb_width - 1)) {
  416. *(uint32_t *) h->mv_cache[0][scan8[0] + 4 - 1*8] = *(uint32_t *) s->current_picture.motion_val[0][b_xy - h->b_stride + 4];
  417. h->ref_cache[0][scan8[0] + 4 - 1*8] = 1;
  418. }
  419. if (s->mb_x > 0) {
  420. *(uint32_t *) h->mv_cache[0][scan8[0] - 1 - 1*8] = *(uint32_t *) s->current_picture.motion_val[0][b_xy - h->b_stride - 1];
  421. h->ref_cache[0][scan8[0] - 1 - 1*8] = 1;
  422. }
  423. }
  424. /* decode motion vector(s) and form prediction(s) */
  425. part_width = ((mb_type & 5) == 5) ? 4 : 8 << (mb_type & 1);
  426. part_height = 16 >> ((unsigned) mb_type / 3);
  427. for (i=0; i < 16; i+=part_height) {
  428. for (j=0; j < 16; j+=part_width) {
  429. x = 16*s->mb_x + j;
  430. y = 16*s->mb_y + i;
  431. k = ((j>>2)&1) + ((i>>1)&2) + ((j>>1)&4) + (i&8);
  432. pred_motion (h, k, (part_width >> 2), 0, 1, &mx, &my);
  433. /* clip motion vector prediction to frame border */
  434. mx = clip (mx, -6*x, 6*(s->width - part_width - x));
  435. my = clip (my, -6*y, 6*(s->height - part_height - y));
  436. /* get motion vector differential */
  437. dy = svq3_get_se_golomb (&s->gb);
  438. dx = svq3_get_se_golomb (&s->gb);
  439. if (dx == INVALID_VLC || dy == INVALID_VLC) {
  440. return -1;
  441. }
  442. /* compute motion vector */
  443. if (mode == 3) {
  444. mx = ((mx + 1) & ~0x1) + 2*dx;
  445. my = ((my + 1) & ~0x1) + 2*dy;
  446. } else if (mode == 2) {
  447. mx = (mx + 1) - ((unsigned) (0x7FFFFFFF + mx) % 3) + 3*dx;
  448. my = (my + 1) - ((unsigned) (0x7FFFFFFF + my) % 3) + 3*dy;
  449. } else if (mode == 1) {
  450. mx = (mx + 3) - ((unsigned) (0x7FFFFFFB + mx) % 6) + 6*dx;
  451. my = (my + 3) - ((unsigned) (0x7FFFFFFB + my) % 6) + 6*dy;
  452. }
  453. /* update mv_cache */
  454. for (l=0; l < part_height; l+=4) {
  455. for (m=0; m < part_width; m+=4) {
  456. k = scan8[0] + ((m + j) >> 2) + ((l + i) << 1);
  457. h->mv_cache [0][k][0] = mx;
  458. h->mv_cache [0][k][1] = my;
  459. h->ref_cache[0][k] = 1;
  460. }
  461. }
  462. svq3_mc_dir_part (s, x, y, part_width, part_height, mx, my);
  463. }
  464. }
  465. for (i=0; i < 4; i++) {
  466. memcpy (s->current_picture.motion_val[0][b_xy + i*h->b_stride], h->mv_cache[0][scan8[0] + 8*i], 4*2*sizeof(int16_t));
  467. }
  468. if ((vlc = svq3_get_ue_golomb (&s->gb)) >= 48)
  469. return -1;
  470. cbp = golomb_to_inter_cbp[vlc];
  471. mb_type = MB_TYPE_16x16;
  472. } else if (mb_type == 8) { /* INTRA4x4 */
  473. memset (h->intra4x4_pred_mode_cache, -1, 8*5*sizeof(int8_t));
  474. if (s->mb_x > 0) {
  475. for (i=0; i < 4; i++) {
  476. h->intra4x4_pred_mode_cache[scan8[0] - 1 + i*8] = h->intra4x4_pred_mode[mb_xy - 1][i];
  477. }
  478. }
  479. if (s->mb_y > 0) {
  480. h->intra4x4_pred_mode_cache[4+8*0] = h->intra4x4_pred_mode[mb_xy - s->mb_stride][4];
  481. h->intra4x4_pred_mode_cache[5+8*0] = h->intra4x4_pred_mode[mb_xy - s->mb_stride][5];
  482. h->intra4x4_pred_mode_cache[6+8*0] = h->intra4x4_pred_mode[mb_xy - s->mb_stride][6];
  483. h->intra4x4_pred_mode_cache[7+8*0] = h->intra4x4_pred_mode[mb_xy - s->mb_stride][3];
  484. }
  485. /* decode prediction codes for luma blocks */
  486. for (i=0; i < 16; i+=2) {
  487. vlc = svq3_get_ue_golomb (&s->gb);
  488. if (vlc >= 25)
  489. return -1;
  490. left = &h->intra4x4_pred_mode_cache[scan8[i] - 1];
  491. top = &h->intra4x4_pred_mode_cache[scan8[i] - 8];
  492. left[1] = svq3_pred_1[top[0] + 1][left[0] + 1][svq3_pred_0[vlc][0]];
  493. left[2] = svq3_pred_1[top[1] + 1][left[1] + 1][svq3_pred_0[vlc][1]];
  494. if (left[1] == -1 || left[2] == -1)
  495. return -1;
  496. }
  497. write_back_intra_pred_mode (h);
  498. check_intra4x4_pred_mode (h);
  499. if ((vlc = svq3_get_ue_golomb (&s->gb)) >= 48)
  500. return -1;
  501. cbp = golomb_to_intra4x4_cbp[vlc];
  502. mb_type = MB_TYPE_INTRA4x4;
  503. } else { /* INTRA16x16 */
  504. dir = i_mb_type_info[mb_type - 8].pred_mode;
  505. dir = (dir >> 1) ^ 3*(dir & 1) ^ 1;
  506. if ((h->intra16x16_pred_mode = check_intra_pred_mode (h, dir)) == -1)
  507. return -1;
  508. cbp = i_mb_type_info[mb_type - 8].cbp;
  509. mb_type = MB_TYPE_INTRA16x16;
  510. }
  511. if (!IS_INTER(mb_type) && s->pict_type != I_TYPE) {
  512. for (i=0; i < 4; i++) {
  513. memset (s->current_picture.motion_val[0][b_xy + i*h->b_stride], 0, 4*2*sizeof(int16_t));
  514. }
  515. }
  516. if (!IS_INTRA4x4(mb_type)) {
  517. memset (h->intra4x4_pred_mode[mb_xy], DC_PRED, 8);
  518. }
  519. if (!IS_SKIP(mb_type)) {
  520. memset (h->mb, 0, 24*16*sizeof(DCTELEM));
  521. memset (h->non_zero_count_cache, 0, 8*6*sizeof(uint8_t));
  522. }
  523. if (IS_INTRA16x16(mb_type) || (s->pict_type != I_TYPE && s->adaptive_quant && cbp)) {
  524. s->qscale += svq3_get_se_golomb (&s->gb);
  525. if (s->qscale > 31)
  526. return -1;
  527. }
  528. if (IS_INTRA16x16(mb_type)) {
  529. if (svq3_decode_block (&s->gb, h->mb, 0, 0))
  530. return -1;
  531. }
  532. if (!IS_SKIP(mb_type) && cbp) {
  533. l = IS_INTRA16x16(mb_type) ? 1 : 0;
  534. m = ((s->qscale < 24 && IS_INTRA4x4(mb_type)) ? 2 : 1);
  535. for (i=0; i < 4; i++) {
  536. if ((cbp & (1 << i))) {
  537. for (j=0; j < 4; j++) {
  538. k = l ? ((j&1) + 2*(i&1) + 2*(j&2) + 4*(i&2)) : (4*i + j);
  539. h->non_zero_count_cache[ scan8[k] ] = 1;
  540. if (svq3_decode_block (&s->gb, &h->mb[16*k], l, m))
  541. return -1;
  542. }
  543. }
  544. }
  545. if ((cbp & 0x30)) {
  546. for (i=0; i < 2; ++i) {
  547. if (svq3_decode_block (&s->gb, &h->mb[16*(16 + 4*i)], 0, 3))
  548. return -1;
  549. }
  550. if ((cbp & 0x20)) {
  551. for (i=0; i < 8; i++) {
  552. h->non_zero_count_cache[ scan8[16+i] ] = 1;
  553. if (svq3_decode_block (&s->gb, &h->mb[16*(16 + i)], 1, 1))
  554. return -1;
  555. }
  556. }
  557. }
  558. }
  559. s->current_picture.mb_type[mb_xy] = mb_type;
  560. if (IS_INTRA(mb_type)) {
  561. h->chroma_pred_mode = check_intra_pred_mode (h, DC_PRED8x8);
  562. }
  563. return 0;
  564. }
  565. static int svq3_decode_frame (AVCodecContext *avctx,
  566. void *data, int *data_size,
  567. uint8_t *buf, int buf_size) {
  568. MpegEncContext *const s = avctx->priv_data;
  569. H264Context *const h = avctx->priv_data;
  570. int i;
  571. s->flags = avctx->flags;
  572. if (!s->context_initialized) {
  573. s->width = (avctx->width + 15) & ~15;
  574. s->height = (avctx->height + 15) & ~15;
  575. h->b_stride = (s->width >> 2);
  576. h->pred4x4[DIAG_DOWN_LEFT_PRED] = pred4x4_down_left_svq3_c;
  577. h->pred16x16[PLANE_PRED8x8] = pred16x16_plane_svq3_c;
  578. h->halfpel_flag = 1;
  579. h->thirdpel_flag = 1;
  580. h->chroma_qp = 4;
  581. if (MPV_common_init (s) < 0)
  582. return -1;
  583. alloc_tables (h);
  584. }
  585. if (avctx->extradata && avctx->extradata_size >= 0x63
  586. && !memcmp (avctx->extradata, "SVQ3", 4)) {
  587. uint8_t *stsd = (uint8_t *) avctx->extradata + 0x62;
  588. if ((*stsd >> 5) != 7 || avctx->extradata_size >= 0x66) {
  589. if ((*stsd >> 5) == 7) {
  590. stsd += 3; /* skip width, height (12 bits each) */
  591. }
  592. h->halfpel_flag = (*stsd >> 4) & 1;
  593. h->thirdpel_flag = (*stsd >> 3) & 1;
  594. }
  595. }
  596. if ((buf[0] & 0x9F) != 1) {
  597. /* TODO: what? */
  598. fprintf (stderr, "unsupported header (%02X)\n", buf[0]);
  599. return -1;
  600. } else {
  601. int length = (buf[0] >> 5) & 3;
  602. int offset = 0;
  603. for (i=0; i < length; i++) {
  604. offset = (offset << 8) | buf[i + 1];
  605. }
  606. if (buf_size < (offset + length + 1) || length == 0)
  607. return -1;
  608. memcpy (&buf[2], &buf[offset + 2], (length - 1));
  609. }
  610. init_get_bits (&s->gb, &buf[2], 8*(buf_size - 2));
  611. if ((i = svq3_get_ue_golomb (&s->gb)) == INVALID_VLC || i >= 3)
  612. return -1;
  613. s->pict_type = golomb_to_pict_type[i];
  614. /* unknown fields */
  615. get_bits (&s->gb, 1);
  616. get_bits (&s->gb, 8);
  617. s->qscale = get_bits (&s->gb, 5);
  618. s->adaptive_quant = get_bits (&s->gb, 1);
  619. /* unknown fields */
  620. get_bits (&s->gb, 1);
  621. get_bits (&s->gb, 1);
  622. get_bits (&s->gb, 2);
  623. while (get_bits (&s->gb, 1)) {
  624. get_bits (&s->gb, 8);
  625. }
  626. /* B-frames are not supported */
  627. if (s->pict_type == B_TYPE/* && avctx->hurry_up*/)
  628. return buf_size;
  629. frame_start (h);
  630. for (s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
  631. for (s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
  632. int mb_type = svq3_get_ue_golomb (&s->gb);
  633. if (s->pict_type == I_TYPE) {
  634. mb_type += 8;
  635. }
  636. if (mb_type > 32 || svq3_decode_mb (h, mb_type)) {
  637. fprintf (stderr, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  638. return -1;
  639. }
  640. if (mb_type != 0) {
  641. hl_decode_mb (h);
  642. }
  643. }
  644. }
  645. *(AVFrame *) data = *(AVFrame *) &s->current_picture;
  646. *data_size = sizeof(AVFrame);
  647. MPV_frame_end(s);
  648. return buf_size;
  649. }
  650. AVCodec svq3_decoder = {
  651. "svq3",
  652. CODEC_TYPE_VIDEO,
  653. CODEC_ID_SVQ3,
  654. sizeof(H264Context),
  655. decode_init,
  656. NULL,
  657. decode_end,
  658. svq3_decode_frame,
  659. CODEC_CAP_DR1,
  660. };