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.

780 lines
22KB

  1. /*
  2. * SVQ1 decoder
  3. * ported to MPlayer by Arpi <arpi@thot.banki.hu>
  4. * ported to libavcodec by Nick Kurshev <nickols_k@mail.ru>
  5. *
  6. * Copyright (C) 2002 the xine project
  7. * Copyright (C) 2002 the ffmpeg project
  8. *
  9. * SVQ1 Encoder (c) 2004 Mike Melanson <melanson@pcisys.net>
  10. *
  11. * This file is part of Libav.
  12. *
  13. * Libav is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU Lesser General Public
  15. * License as published by the Free Software Foundation; either
  16. * version 2.1 of the License, or (at your option) any later version.
  17. *
  18. * Libav is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with Libav; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. /**
  28. * @file
  29. * Sorenson Vector Quantizer #1 (SVQ1) video codec.
  30. * For more information of the SVQ1 algorithm, visit:
  31. * http://www.pcisys.net/~melanson/codecs/
  32. */
  33. #include "avcodec.h"
  34. #include "dsputil.h"
  35. #include "mpegvideo.h"
  36. #include "mathops.h"
  37. #include "svq1.h"
  38. #undef NDEBUG
  39. #include <assert.h>
  40. extern const uint8_t ff_mvtab[33][2];
  41. static VLC svq1_block_type;
  42. static VLC svq1_motion_component;
  43. static VLC svq1_intra_multistage[6];
  44. static VLC svq1_inter_multistage[6];
  45. static VLC svq1_intra_mean;
  46. static VLC svq1_inter_mean;
  47. /* motion vector (prediction) */
  48. typedef struct svq1_pmv_s {
  49. int x;
  50. int y;
  51. } svq1_pmv;
  52. static const uint8_t string_table[256] = {
  53. 0x00, 0xD5, 0x7F, 0xAA, 0xFE, 0x2B, 0x81, 0x54,
  54. 0x29, 0xFC, 0x56, 0x83, 0xD7, 0x02, 0xA8, 0x7D,
  55. 0x52, 0x87, 0x2D, 0xF8, 0xAC, 0x79, 0xD3, 0x06,
  56. 0x7B, 0xAE, 0x04, 0xD1, 0x85, 0x50, 0xFA, 0x2F,
  57. 0xA4, 0x71, 0xDB, 0x0E, 0x5A, 0x8F, 0x25, 0xF0,
  58. 0x8D, 0x58, 0xF2, 0x27, 0x73, 0xA6, 0x0C, 0xD9,
  59. 0xF6, 0x23, 0x89, 0x5C, 0x08, 0xDD, 0x77, 0xA2,
  60. 0xDF, 0x0A, 0xA0, 0x75, 0x21, 0xF4, 0x5E, 0x8B,
  61. 0x9D, 0x48, 0xE2, 0x37, 0x63, 0xB6, 0x1C, 0xC9,
  62. 0xB4, 0x61, 0xCB, 0x1E, 0x4A, 0x9F, 0x35, 0xE0,
  63. 0xCF, 0x1A, 0xB0, 0x65, 0x31, 0xE4, 0x4E, 0x9B,
  64. 0xE6, 0x33, 0x99, 0x4C, 0x18, 0xCD, 0x67, 0xB2,
  65. 0x39, 0xEC, 0x46, 0x93, 0xC7, 0x12, 0xB8, 0x6D,
  66. 0x10, 0xC5, 0x6F, 0xBA, 0xEE, 0x3B, 0x91, 0x44,
  67. 0x6B, 0xBE, 0x14, 0xC1, 0x95, 0x40, 0xEA, 0x3F,
  68. 0x42, 0x97, 0x3D, 0xE8, 0xBC, 0x69, 0xC3, 0x16,
  69. 0xEF, 0x3A, 0x90, 0x45, 0x11, 0xC4, 0x6E, 0xBB,
  70. 0xC6, 0x13, 0xB9, 0x6C, 0x38, 0xED, 0x47, 0x92,
  71. 0xBD, 0x68, 0xC2, 0x17, 0x43, 0x96, 0x3C, 0xE9,
  72. 0x94, 0x41, 0xEB, 0x3E, 0x6A, 0xBF, 0x15, 0xC0,
  73. 0x4B, 0x9E, 0x34, 0xE1, 0xB5, 0x60, 0xCA, 0x1F,
  74. 0x62, 0xB7, 0x1D, 0xC8, 0x9C, 0x49, 0xE3, 0x36,
  75. 0x19, 0xCC, 0x66, 0xB3, 0xE7, 0x32, 0x98, 0x4D,
  76. 0x30, 0xE5, 0x4F, 0x9A, 0xCE, 0x1B, 0xB1, 0x64,
  77. 0x72, 0xA7, 0x0D, 0xD8, 0x8C, 0x59, 0xF3, 0x26,
  78. 0x5B, 0x8E, 0x24, 0xF1, 0xA5, 0x70, 0xDA, 0x0F,
  79. 0x20, 0xF5, 0x5F, 0x8A, 0xDE, 0x0B, 0xA1, 0x74,
  80. 0x09, 0xDC, 0x76, 0xA3, 0xF7, 0x22, 0x88, 0x5D,
  81. 0xD6, 0x03, 0xA9, 0x7C, 0x28, 0xFD, 0x57, 0x82,
  82. 0xFF, 0x2A, 0x80, 0x55, 0x01, 0xD4, 0x7E, 0xAB,
  83. 0x84, 0x51, 0xFB, 0x2E, 0x7A, 0xAF, 0x05, 0xD0,
  84. 0xAD, 0x78, 0xD2, 0x07, 0x53, 0x86, 0x2C, 0xF9
  85. };
  86. #define SVQ1_PROCESS_VECTOR()\
  87. for (; level > 0; i++) {\
  88. /* process next depth */\
  89. if (i == m) {\
  90. m = n;\
  91. if (--level == 0)\
  92. break;\
  93. }\
  94. /* divide block if next bit set */\
  95. if (get_bits1 (bitbuf) == 0)\
  96. break;\
  97. /* add child nodes */\
  98. list[n++] = list[i];\
  99. list[n++] = list[i] + (((level & 1) ? pitch : 1) << ((level / 2) + 1));\
  100. }
  101. #define SVQ1_ADD_CODEBOOK()\
  102. /* add codebook entries to vector */\
  103. for (j=0; j < stages; j++) {\
  104. n3 = codebook[entries[j]] ^ 0x80808080;\
  105. n1 += ((n3 & 0xFF00FF00) >> 8);\
  106. n2 += (n3 & 0x00FF00FF);\
  107. }\
  108. \
  109. /* clip to [0..255] */\
  110. if (n1 & 0xFF00FF00) {\
  111. n3 = ((( n1 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
  112. n1 += 0x7F007F00;\
  113. n1 |= (((~n1 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
  114. n1 &= (n3 & 0x00FF00FF);\
  115. }\
  116. \
  117. if (n2 & 0xFF00FF00) {\
  118. n3 = ((( n2 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
  119. n2 += 0x7F007F00;\
  120. n2 |= (((~n2 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
  121. n2 &= (n3 & 0x00FF00FF);\
  122. }
  123. #define SVQ1_DO_CODEBOOK_INTRA()\
  124. for (y=0; y < height; y++) {\
  125. for (x=0; x < (width / 4); x++, codebook++) {\
  126. n1 = n4;\
  127. n2 = n4;\
  128. SVQ1_ADD_CODEBOOK()\
  129. /* store result */\
  130. dst[x] = (n1 << 8) | n2;\
  131. }\
  132. dst += (pitch / 4);\
  133. }
  134. #define SVQ1_DO_CODEBOOK_NONINTRA()\
  135. for (y=0; y < height; y++) {\
  136. for (x=0; x < (width / 4); x++, codebook++) {\
  137. n3 = dst[x];\
  138. /* add mean value to vector */\
  139. n1 = ((n3 & 0xFF00FF00) >> 8) + n4;\
  140. n2 = (n3 & 0x00FF00FF) + n4;\
  141. SVQ1_ADD_CODEBOOK()\
  142. /* store result */\
  143. dst[x] = (n1 << 8) | n2;\
  144. }\
  145. dst += (pitch / 4);\
  146. }
  147. #define SVQ1_CALC_CODEBOOK_ENTRIES(cbook)\
  148. codebook = (const uint32_t *) cbook[level];\
  149. if (stages > 0)\
  150. bit_cache = get_bits (bitbuf, 4*stages);\
  151. /* calculate codebook entries for this vector */\
  152. for (j=0; j < stages; j++) {\
  153. entries[j] = (((bit_cache >> (4*(stages - j - 1))) & 0xF) + 16*j) << (level + 1);\
  154. }\
  155. mean -= (stages * 128);\
  156. n4 = ((mean + (mean >> 31)) << 16) | (mean & 0xFFFF);
  157. static int svq1_decode_block_intra (GetBitContext *bitbuf, uint8_t *pixels, int pitch ) {
  158. uint32_t bit_cache;
  159. uint8_t *list[63];
  160. uint32_t *dst;
  161. const uint32_t *codebook;
  162. int entries[6];
  163. int i, j, m, n;
  164. int mean, stages;
  165. unsigned x, y, width, height, level;
  166. uint32_t n1, n2, n3, n4;
  167. /* initialize list for breadth first processing of vectors */
  168. list[0] = pixels;
  169. /* recursively process vector */
  170. for (i=0, m=1, n=1, level=5; i < n; i++) {
  171. SVQ1_PROCESS_VECTOR();
  172. /* destination address and vector size */
  173. dst = (uint32_t *) list[i];
  174. width = 1 << ((4 + level) /2);
  175. height = 1 << ((3 + level) /2);
  176. /* get number of stages (-1 skips vector, 0 for mean only) */
  177. stages = get_vlc2(bitbuf, svq1_intra_multistage[level].table, 3, 3) - 1;
  178. if (stages == -1) {
  179. for (y=0; y < height; y++) {
  180. memset (&dst[y*(pitch / 4)], 0, width);
  181. }
  182. continue; /* skip vector */
  183. }
  184. if ((stages > 0) && (level >= 4)) {
  185. av_dlog(NULL,
  186. "Error (svq1_decode_block_intra): invalid vector: stages=%i level=%i\n",
  187. stages, level);
  188. return -1; /* invalid vector */
  189. }
  190. mean = get_vlc2(bitbuf, svq1_intra_mean.table, 8, 3);
  191. if (stages == 0) {
  192. for (y=0; y < height; y++) {
  193. memset (&dst[y*(pitch / 4)], mean, width);
  194. }
  195. } else {
  196. SVQ1_CALC_CODEBOOK_ENTRIES(ff_svq1_intra_codebooks);
  197. SVQ1_DO_CODEBOOK_INTRA()
  198. }
  199. }
  200. return 0;
  201. }
  202. static int svq1_decode_block_non_intra (GetBitContext *bitbuf, uint8_t *pixels, int pitch ) {
  203. uint32_t bit_cache;
  204. uint8_t *list[63];
  205. uint32_t *dst;
  206. const uint32_t *codebook;
  207. int entries[6];
  208. int i, j, m, n;
  209. int mean, stages;
  210. int x, y, width, height, level;
  211. uint32_t n1, n2, n3, n4;
  212. /* initialize list for breadth first processing of vectors */
  213. list[0] = pixels;
  214. /* recursively process vector */
  215. for (i=0, m=1, n=1, level=5; i < n; i++) {
  216. SVQ1_PROCESS_VECTOR();
  217. /* destination address and vector size */
  218. dst = (uint32_t *) list[i];
  219. width = 1 << ((4 + level) /2);
  220. height = 1 << ((3 + level) /2);
  221. /* get number of stages (-1 skips vector, 0 for mean only) */
  222. stages = get_vlc2(bitbuf, svq1_inter_multistage[level].table, 3, 2) - 1;
  223. if (stages == -1) continue; /* skip vector */
  224. if ((stages > 0) && (level >= 4)) {
  225. av_dlog(NULL,
  226. "Error (svq1_decode_block_non_intra): invalid vector: stages=%i level=%i\n",
  227. stages, level);
  228. return -1; /* invalid vector */
  229. }
  230. mean = get_vlc2(bitbuf, svq1_inter_mean.table, 9, 3) - 256;
  231. SVQ1_CALC_CODEBOOK_ENTRIES(ff_svq1_inter_codebooks);
  232. SVQ1_DO_CODEBOOK_NONINTRA()
  233. }
  234. return 0;
  235. }
  236. static int svq1_decode_motion_vector (GetBitContext *bitbuf, svq1_pmv *mv, svq1_pmv **pmv) {
  237. int diff;
  238. int i;
  239. for (i=0; i < 2; i++) {
  240. /* get motion code */
  241. diff = get_vlc2(bitbuf, svq1_motion_component.table, 7, 2);
  242. if(diff<0)
  243. return -1;
  244. else if(diff){
  245. if(get_bits1(bitbuf)) diff= -diff;
  246. }
  247. /* add median of motion vector predictors and clip result */
  248. if (i == 1)
  249. mv->y = sign_extend(diff + mid_pred(pmv[0]->y, pmv[1]->y, pmv[2]->y), 6);
  250. else
  251. mv->x = sign_extend(diff + mid_pred(pmv[0]->x, pmv[1]->x, pmv[2]->x), 6);
  252. }
  253. return 0;
  254. }
  255. static void svq1_skip_block (uint8_t *current, uint8_t *previous, int pitch, int x, int y) {
  256. uint8_t *src;
  257. uint8_t *dst;
  258. int i;
  259. src = &previous[x + y*pitch];
  260. dst = current;
  261. for (i=0; i < 16; i++) {
  262. memcpy (dst, src, 16);
  263. src += pitch;
  264. dst += pitch;
  265. }
  266. }
  267. static int svq1_motion_inter_block (MpegEncContext *s, GetBitContext *bitbuf,
  268. uint8_t *current, uint8_t *previous, int pitch,
  269. svq1_pmv *motion, int x, int y) {
  270. uint8_t *src;
  271. uint8_t *dst;
  272. svq1_pmv mv;
  273. svq1_pmv *pmv[3];
  274. int result;
  275. /* predict and decode motion vector */
  276. pmv[0] = &motion[0];
  277. if (y == 0) {
  278. pmv[1] =
  279. pmv[2] = pmv[0];
  280. }
  281. else {
  282. pmv[1] = &motion[(x / 8) + 2];
  283. pmv[2] = &motion[(x / 8) + 4];
  284. }
  285. result = svq1_decode_motion_vector (bitbuf, &mv, pmv);
  286. if (result != 0)
  287. return result;
  288. motion[0].x =
  289. motion[(x / 8) + 2].x =
  290. motion[(x / 8) + 3].x = mv.x;
  291. motion[0].y =
  292. motion[(x / 8) + 2].y =
  293. motion[(x / 8) + 3].y = mv.y;
  294. if(y + (mv.y >> 1)<0)
  295. mv.y= 0;
  296. if(x + (mv.x >> 1)<0)
  297. mv.x= 0;
  298. src = &previous[(x + (mv.x >> 1)) + (y + (mv.y >> 1))*pitch];
  299. dst = current;
  300. s->dsp.put_pixels_tab[0][((mv.y & 1) << 1) | (mv.x & 1)](dst,src,pitch,16);
  301. return 0;
  302. }
  303. static int svq1_motion_inter_4v_block (MpegEncContext *s, GetBitContext *bitbuf,
  304. uint8_t *current, uint8_t *previous, int pitch,
  305. svq1_pmv *motion,int x, int y) {
  306. uint8_t *src;
  307. uint8_t *dst;
  308. svq1_pmv mv;
  309. svq1_pmv *pmv[4];
  310. int i, result;
  311. /* predict and decode motion vector (0) */
  312. pmv[0] = &motion[0];
  313. if (y == 0) {
  314. pmv[1] =
  315. pmv[2] = pmv[0];
  316. }
  317. else {
  318. pmv[1] = &motion[(x / 8) + 2];
  319. pmv[2] = &motion[(x / 8) + 4];
  320. }
  321. result = svq1_decode_motion_vector (bitbuf, &mv, pmv);
  322. if (result != 0)
  323. return result;
  324. /* predict and decode motion vector (1) */
  325. pmv[0] = &mv;
  326. if (y == 0) {
  327. pmv[1] =
  328. pmv[2] = pmv[0];
  329. }
  330. else {
  331. pmv[1] = &motion[(x / 8) + 3];
  332. }
  333. result = svq1_decode_motion_vector (bitbuf, &motion[0], pmv);
  334. if (result != 0)
  335. return result;
  336. /* predict and decode motion vector (2) */
  337. pmv[1] = &motion[0];
  338. pmv[2] = &motion[(x / 8) + 1];
  339. result = svq1_decode_motion_vector (bitbuf, &motion[(x / 8) + 2], pmv);
  340. if (result != 0)
  341. return result;
  342. /* predict and decode motion vector (3) */
  343. pmv[2] = &motion[(x / 8) + 2];
  344. pmv[3] = &motion[(x / 8) + 3];
  345. result = svq1_decode_motion_vector (bitbuf, pmv[3], pmv);
  346. if (result != 0)
  347. return result;
  348. /* form predictions */
  349. for (i=0; i < 4; i++) {
  350. int mvx= pmv[i]->x + (i&1)*16;
  351. int mvy= pmv[i]->y + (i>>1)*16;
  352. ///XXX /FIXME clipping or padding?
  353. if(y + (mvy >> 1)<0)
  354. mvy= 0;
  355. if(x + (mvx >> 1)<0)
  356. mvx= 0;
  357. src = &previous[(x + (mvx >> 1)) + (y + (mvy >> 1))*pitch];
  358. dst = current;
  359. s->dsp.put_pixels_tab[1][((mvy & 1) << 1) | (mvx & 1)](dst,src,pitch,8);
  360. /* select next block */
  361. if (i & 1) {
  362. current += 8*(pitch - 1);
  363. } else {
  364. current += 8;
  365. }
  366. }
  367. return 0;
  368. }
  369. static int svq1_decode_delta_block (MpegEncContext *s, GetBitContext *bitbuf,
  370. uint8_t *current, uint8_t *previous, int pitch,
  371. svq1_pmv *motion, int x, int y) {
  372. uint32_t block_type;
  373. int result = 0;
  374. /* get block type */
  375. block_type = get_vlc2(bitbuf, svq1_block_type.table, 2, 2);
  376. /* reset motion vectors */
  377. if (block_type == SVQ1_BLOCK_SKIP || block_type == SVQ1_BLOCK_INTRA) {
  378. motion[0].x =
  379. motion[0].y =
  380. motion[(x / 8) + 2].x =
  381. motion[(x / 8) + 2].y =
  382. motion[(x / 8) + 3].x =
  383. motion[(x / 8) + 3].y = 0;
  384. }
  385. switch (block_type) {
  386. case SVQ1_BLOCK_SKIP:
  387. svq1_skip_block (current, previous, pitch, x, y);
  388. break;
  389. case SVQ1_BLOCK_INTER:
  390. result = svq1_motion_inter_block (s, bitbuf, current, previous, pitch, motion, x, y);
  391. if (result != 0)
  392. {
  393. av_dlog(s->avctx, "Error in svq1_motion_inter_block %i\n", result);
  394. break;
  395. }
  396. result = svq1_decode_block_non_intra (bitbuf, current, pitch);
  397. break;
  398. case SVQ1_BLOCK_INTER_4V:
  399. result = svq1_motion_inter_4v_block (s, bitbuf, current, previous, pitch, motion, x, y);
  400. if (result != 0)
  401. {
  402. av_dlog(s->avctx, "Error in svq1_motion_inter_4v_block %i\n", result);
  403. break;
  404. }
  405. result = svq1_decode_block_non_intra (bitbuf, current, pitch);
  406. break;
  407. case SVQ1_BLOCK_INTRA:
  408. result = svq1_decode_block_intra (bitbuf, current, pitch);
  409. break;
  410. }
  411. return result;
  412. }
  413. static void svq1_parse_string (GetBitContext *bitbuf, uint8_t *out) {
  414. uint8_t seed;
  415. int i;
  416. out[0] = get_bits (bitbuf, 8);
  417. seed = string_table[out[0]];
  418. for (i=1; i <= out[0]; i++) {
  419. out[i] = get_bits (bitbuf, 8) ^ seed;
  420. seed = string_table[out[i] ^ seed];
  421. }
  422. }
  423. static int svq1_decode_frame_header (GetBitContext *bitbuf,MpegEncContext *s) {
  424. int frame_size_code;
  425. skip_bits(bitbuf, 8); /* temporal_reference */
  426. /* frame type */
  427. s->pict_type= get_bits (bitbuf, 2)+1;
  428. if(s->pict_type==4)
  429. return -1;
  430. if (s->pict_type == AV_PICTURE_TYPE_I) {
  431. /* unknown fields */
  432. if (s->f_code == 0x50 || s->f_code == 0x60) {
  433. int csum = get_bits (bitbuf, 16);
  434. csum = ff_svq1_packet_checksum (bitbuf->buffer, bitbuf->size_in_bits>>3, csum);
  435. av_dlog(s->avctx, "%s checksum (%02x) for packet data\n",
  436. (csum == 0) ? "correct" : "incorrect", csum);
  437. }
  438. if ((s->f_code ^ 0x10) >= 0x50) {
  439. uint8_t msg[256];
  440. svq1_parse_string (bitbuf, msg);
  441. av_log(s->avctx, AV_LOG_INFO, "embedded message: \"%s\"\n", (char *) msg);
  442. }
  443. skip_bits (bitbuf, 2);
  444. skip_bits (bitbuf, 2);
  445. skip_bits1 (bitbuf);
  446. /* load frame size */
  447. frame_size_code = get_bits (bitbuf, 3);
  448. if (frame_size_code == 7) {
  449. /* load width, height (12 bits each) */
  450. s->width = get_bits (bitbuf, 12);
  451. s->height = get_bits (bitbuf, 12);
  452. if (!s->width || !s->height)
  453. return -1;
  454. } else {
  455. /* get width, height from table */
  456. s->width = ff_svq1_frame_size_table[frame_size_code].width;
  457. s->height = ff_svq1_frame_size_table[frame_size_code].height;
  458. }
  459. }
  460. /* unknown fields */
  461. if (get_bits1 (bitbuf) == 1) {
  462. skip_bits1 (bitbuf); /* use packet checksum if (1) */
  463. skip_bits1 (bitbuf); /* component checksums after image data if (1) */
  464. if (get_bits (bitbuf, 2) != 0)
  465. return -1;
  466. }
  467. if (get_bits1 (bitbuf) == 1) {
  468. skip_bits1 (bitbuf);
  469. skip_bits (bitbuf, 4);
  470. skip_bits1 (bitbuf);
  471. skip_bits (bitbuf, 2);
  472. while (get_bits1 (bitbuf) == 1) {
  473. skip_bits (bitbuf, 8);
  474. }
  475. }
  476. return 0;
  477. }
  478. static int svq1_decode_frame(AVCodecContext *avctx,
  479. void *data, int *data_size,
  480. AVPacket *avpkt)
  481. {
  482. const uint8_t *buf = avpkt->data;
  483. int buf_size = avpkt->size;
  484. MpegEncContext *s=avctx->priv_data;
  485. uint8_t *current, *previous;
  486. int result, i, x, y, width, height;
  487. AVFrame *pict = data;
  488. svq1_pmv *pmv;
  489. /* initialize bit buffer */
  490. init_get_bits(&s->gb,buf,buf_size*8);
  491. /* decode frame header */
  492. s->f_code = get_bits (&s->gb, 22);
  493. if ((s->f_code & ~0x70) || !(s->f_code & 0x60))
  494. return -1;
  495. /* swap some header bytes (why?) */
  496. if (s->f_code != 0x20) {
  497. uint32_t *src = (uint32_t *) (buf + 4);
  498. for (i=0; i < 4; i++) {
  499. src[i] = ((src[i] << 16) | (src[i] >> 16)) ^ src[7 - i];
  500. }
  501. }
  502. result = svq1_decode_frame_header (&s->gb, s);
  503. if (result != 0)
  504. {
  505. av_dlog(s->avctx, "Error in svq1_decode_frame_header %i\n",result);
  506. return result;
  507. }
  508. avcodec_set_dimensions(avctx, s->width, s->height);
  509. //FIXME this avoids some confusion for "B frames" without 2 references
  510. //this should be removed after libavcodec can handle more flexible picture types & ordering
  511. if(s->pict_type==AV_PICTURE_TYPE_B && s->last_picture_ptr==NULL) return buf_size;
  512. if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==AV_PICTURE_TYPE_B)
  513. ||(avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=AV_PICTURE_TYPE_I)
  514. || avctx->skip_frame >= AVDISCARD_ALL)
  515. return buf_size;
  516. if(ff_MPV_frame_start(s, avctx) < 0)
  517. return -1;
  518. pmv = av_malloc((FFALIGN(s->width, 16)/8 + 3) * sizeof(*pmv));
  519. if (!pmv)
  520. return -1;
  521. /* decode y, u and v components */
  522. for (i=0; i < 3; i++) {
  523. int linesize;
  524. if (i == 0) {
  525. width = FFALIGN(s->width, 16);
  526. height = FFALIGN(s->height, 16);
  527. linesize= s->linesize;
  528. } else {
  529. if(s->flags&CODEC_FLAG_GRAY) break;
  530. width = FFALIGN(s->width/4, 16);
  531. height = FFALIGN(s->height/4, 16);
  532. linesize= s->uvlinesize;
  533. }
  534. current = s->current_picture.f.data[i];
  535. if(s->pict_type==AV_PICTURE_TYPE_B){
  536. previous = s->next_picture.f.data[i];
  537. }else{
  538. previous = s->last_picture.f.data[i];
  539. }
  540. if (s->pict_type == AV_PICTURE_TYPE_I) {
  541. /* keyframe */
  542. for (y=0; y < height; y+=16) {
  543. for (x=0; x < width; x+=16) {
  544. result = svq1_decode_block_intra (&s->gb, &current[x], linesize);
  545. if (result != 0)
  546. {
  547. av_log(s->avctx, AV_LOG_INFO, "Error in svq1_decode_block %i (keyframe)\n",result);
  548. goto err;
  549. }
  550. }
  551. current += 16*linesize;
  552. }
  553. } else {
  554. /* delta frame */
  555. memset (pmv, 0, ((width / 8) + 3) * sizeof(svq1_pmv));
  556. for (y=0; y < height; y+=16) {
  557. for (x=0; x < width; x+=16) {
  558. result = svq1_decode_delta_block (s, &s->gb, &current[x], previous,
  559. linesize, pmv, x, y);
  560. if (result != 0)
  561. {
  562. av_dlog(s->avctx, "Error in svq1_decode_delta_block %i\n",result);
  563. goto err;
  564. }
  565. }
  566. pmv[0].x =
  567. pmv[0].y = 0;
  568. current += 16*linesize;
  569. }
  570. }
  571. }
  572. *pict = s->current_picture.f;
  573. ff_MPV_frame_end(s);
  574. *data_size=sizeof(AVFrame);
  575. result = buf_size;
  576. err:
  577. av_free(pmv);
  578. return result;
  579. }
  580. static av_cold int svq1_decode_init(AVCodecContext *avctx)
  581. {
  582. MpegEncContext *s = avctx->priv_data;
  583. int i;
  584. int offset = 0;
  585. ff_MPV_decode_defaults(s);
  586. s->avctx = avctx;
  587. s->width = (avctx->width+3)&~3;
  588. s->height = (avctx->height+3)&~3;
  589. s->codec_id= avctx->codec->id;
  590. avctx->pix_fmt = PIX_FMT_YUV410P;
  591. avctx->has_b_frames= 1; // not true, but DP frames and these behave like unidirectional b frames
  592. s->flags= avctx->flags;
  593. if (ff_MPV_common_init(s) < 0) return -1;
  594. INIT_VLC_STATIC(&svq1_block_type, 2, 4,
  595. &ff_svq1_block_type_vlc[0][1], 2, 1,
  596. &ff_svq1_block_type_vlc[0][0], 2, 1, 6);
  597. INIT_VLC_STATIC(&svq1_motion_component, 7, 33,
  598. &ff_mvtab[0][1], 2, 1,
  599. &ff_mvtab[0][0], 2, 1, 176);
  600. for (i = 0; i < 6; i++) {
  601. static const uint8_t sizes[2][6] = {{14, 10, 14, 18, 16, 18}, {10, 10, 14, 14, 14, 16}};
  602. static VLC_TYPE table[168][2];
  603. svq1_intra_multistage[i].table = &table[offset];
  604. svq1_intra_multistage[i].table_allocated = sizes[0][i];
  605. offset += sizes[0][i];
  606. init_vlc(&svq1_intra_multistage[i], 3, 8,
  607. &ff_svq1_intra_multistage_vlc[i][0][1], 2, 1,
  608. &ff_svq1_intra_multistage_vlc[i][0][0], 2, 1, INIT_VLC_USE_NEW_STATIC);
  609. svq1_inter_multistage[i].table = &table[offset];
  610. svq1_inter_multistage[i].table_allocated = sizes[1][i];
  611. offset += sizes[1][i];
  612. init_vlc(&svq1_inter_multistage[i], 3, 8,
  613. &ff_svq1_inter_multistage_vlc[i][0][1], 2, 1,
  614. &ff_svq1_inter_multistage_vlc[i][0][0], 2, 1, INIT_VLC_USE_NEW_STATIC);
  615. }
  616. INIT_VLC_STATIC(&svq1_intra_mean, 8, 256,
  617. &ff_svq1_intra_mean_vlc[0][1], 4, 2,
  618. &ff_svq1_intra_mean_vlc[0][0], 4, 2, 632);
  619. INIT_VLC_STATIC(&svq1_inter_mean, 9, 512,
  620. &ff_svq1_inter_mean_vlc[0][1], 4, 2,
  621. &ff_svq1_inter_mean_vlc[0][0], 4, 2, 1434);
  622. return 0;
  623. }
  624. static av_cold int svq1_decode_end(AVCodecContext *avctx)
  625. {
  626. MpegEncContext *s = avctx->priv_data;
  627. ff_MPV_common_end(s);
  628. return 0;
  629. }
  630. AVCodec ff_svq1_decoder = {
  631. .name = "svq1",
  632. .type = AVMEDIA_TYPE_VIDEO,
  633. .id = AV_CODEC_ID_SVQ1,
  634. .priv_data_size = sizeof(MpegEncContext),
  635. .init = svq1_decode_init,
  636. .close = svq1_decode_end,
  637. .decode = svq1_decode_frame,
  638. .capabilities = CODEC_CAP_DR1,
  639. .flush = ff_mpeg_flush,
  640. .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_YUV410P, PIX_FMT_NONE },
  641. .long_name = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 1 / Sorenson Video 1 / SVQ1"),
  642. };