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.

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