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.

844 lines
27KB

  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 "get_bits.h"
  35. #include "h263.h"
  36. #include "hpeldsp.h"
  37. #include "internal.h"
  38. #include "mathops.h"
  39. #include "svq1.h"
  40. static VLC svq1_block_type;
  41. static VLC svq1_motion_component;
  42. static VLC svq1_intra_multistage[6];
  43. static VLC svq1_inter_multistage[6];
  44. static VLC svq1_intra_mean;
  45. static VLC svq1_inter_mean;
  46. /* motion vector (prediction) */
  47. typedef struct svq1_pmv_s {
  48. int x;
  49. int y;
  50. } svq1_pmv;
  51. typedef struct SVQ1Context {
  52. HpelDSPContext hdsp;
  53. GetBitContext gb;
  54. AVFrame *prev;
  55. uint8_t *pkt_swapped;
  56. int pkt_swapped_allocated;
  57. int width;
  58. int height;
  59. int frame_code;
  60. int nonref; // 1 if the current frame won't be referenced
  61. } SVQ1Context;
  62. static const uint8_t string_table[256] = {
  63. 0x00, 0xD5, 0x7F, 0xAA, 0xFE, 0x2B, 0x81, 0x54,
  64. 0x29, 0xFC, 0x56, 0x83, 0xD7, 0x02, 0xA8, 0x7D,
  65. 0x52, 0x87, 0x2D, 0xF8, 0xAC, 0x79, 0xD3, 0x06,
  66. 0x7B, 0xAE, 0x04, 0xD1, 0x85, 0x50, 0xFA, 0x2F,
  67. 0xA4, 0x71, 0xDB, 0x0E, 0x5A, 0x8F, 0x25, 0xF0,
  68. 0x8D, 0x58, 0xF2, 0x27, 0x73, 0xA6, 0x0C, 0xD9,
  69. 0xF6, 0x23, 0x89, 0x5C, 0x08, 0xDD, 0x77, 0xA2,
  70. 0xDF, 0x0A, 0xA0, 0x75, 0x21, 0xF4, 0x5E, 0x8B,
  71. 0x9D, 0x48, 0xE2, 0x37, 0x63, 0xB6, 0x1C, 0xC9,
  72. 0xB4, 0x61, 0xCB, 0x1E, 0x4A, 0x9F, 0x35, 0xE0,
  73. 0xCF, 0x1A, 0xB0, 0x65, 0x31, 0xE4, 0x4E, 0x9B,
  74. 0xE6, 0x33, 0x99, 0x4C, 0x18, 0xCD, 0x67, 0xB2,
  75. 0x39, 0xEC, 0x46, 0x93, 0xC7, 0x12, 0xB8, 0x6D,
  76. 0x10, 0xC5, 0x6F, 0xBA, 0xEE, 0x3B, 0x91, 0x44,
  77. 0x6B, 0xBE, 0x14, 0xC1, 0x95, 0x40, 0xEA, 0x3F,
  78. 0x42, 0x97, 0x3D, 0xE8, 0xBC, 0x69, 0xC3, 0x16,
  79. 0xEF, 0x3A, 0x90, 0x45, 0x11, 0xC4, 0x6E, 0xBB,
  80. 0xC6, 0x13, 0xB9, 0x6C, 0x38, 0xED, 0x47, 0x92,
  81. 0xBD, 0x68, 0xC2, 0x17, 0x43, 0x96, 0x3C, 0xE9,
  82. 0x94, 0x41, 0xEB, 0x3E, 0x6A, 0xBF, 0x15, 0xC0,
  83. 0x4B, 0x9E, 0x34, 0xE1, 0xB5, 0x60, 0xCA, 0x1F,
  84. 0x62, 0xB7, 0x1D, 0xC8, 0x9C, 0x49, 0xE3, 0x36,
  85. 0x19, 0xCC, 0x66, 0xB3, 0xE7, 0x32, 0x98, 0x4D,
  86. 0x30, 0xE5, 0x4F, 0x9A, 0xCE, 0x1B, 0xB1, 0x64,
  87. 0x72, 0xA7, 0x0D, 0xD8, 0x8C, 0x59, 0xF3, 0x26,
  88. 0x5B, 0x8E, 0x24, 0xF1, 0xA5, 0x70, 0xDA, 0x0F,
  89. 0x20, 0xF5, 0x5F, 0x8A, 0xDE, 0x0B, 0xA1, 0x74,
  90. 0x09, 0xDC, 0x76, 0xA3, 0xF7, 0x22, 0x88, 0x5D,
  91. 0xD6, 0x03, 0xA9, 0x7C, 0x28, 0xFD, 0x57, 0x82,
  92. 0xFF, 0x2A, 0x80, 0x55, 0x01, 0xD4, 0x7E, 0xAB,
  93. 0x84, 0x51, 0xFB, 0x2E, 0x7A, 0xAF, 0x05, 0xD0,
  94. 0xAD, 0x78, 0xD2, 0x07, 0x53, 0x86, 0x2C, 0xF9
  95. };
  96. #define SVQ1_PROCESS_VECTOR() \
  97. for (; level > 0; i++) { \
  98. /* process next depth */ \
  99. if (i == m) { \
  100. m = n; \
  101. if (--level == 0) \
  102. break; \
  103. } \
  104. /* divide block if next bit set */ \
  105. if (get_bits1(bitbuf) == 0) \
  106. break; \
  107. /* add child nodes */ \
  108. list[n++] = list[i]; \
  109. list[n++] = list[i] + \
  110. (((level & 1) ? pitch : 1) << (level / 2 + 1)); \
  111. }
  112. #define SVQ1_ADD_CODEBOOK() \
  113. /* add codebook entries to vector */ \
  114. for (j = 0; j < stages; j++) { \
  115. n3 = codebook[entries[j]] ^ 0x80808080; \
  116. n1 += (n3 & 0xFF00FF00) >> 8; \
  117. n2 += n3 & 0x00FF00FF; \
  118. } \
  119. \
  120. /* clip to [0..255] */ \
  121. if (n1 & 0xFF00FF00) { \
  122. n3 = (n1 >> 15 & 0x00010001 | 0x01000100) - 0x00010001; \
  123. n1 += 0x7F007F00; \
  124. n1 |= (~n1 >> 15 & 0x00010001 | 0x01000100) - 0x00010001; \
  125. n1 &= n3 & 0x00FF00FF; \
  126. } \
  127. \
  128. if (n2 & 0xFF00FF00) { \
  129. n3 = (n2 >> 15 & 0x00010001 | 0x01000100) - 0x00010001; \
  130. n2 += 0x7F007F00; \
  131. n2 |= (~n2 >> 15 & 0x00010001 | 0x01000100) - 0x00010001; \
  132. n2 &= n3 & 0x00FF00FF; \
  133. }
  134. #define SVQ1_CALC_CODEBOOK_ENTRIES(cbook) \
  135. codebook = (const uint32_t *)cbook[level]; \
  136. if (stages > 0) \
  137. bit_cache = get_bits(bitbuf, 4 * stages); \
  138. /* calculate codebook entries for this vector */ \
  139. for (j = 0; j < stages; j++) { \
  140. entries[j] = (((bit_cache >> (4 * (stages - j - 1))) & 0xF) + \
  141. 16 * j) << (level + 1); \
  142. } \
  143. mean -= stages * 128; \
  144. n4 = mean + (mean >> 31) << 16 | (mean & 0xFFFF);
  145. static int svq1_decode_block_intra(GetBitContext *bitbuf, uint8_t *pixels,
  146. int pitch)
  147. {
  148. uint32_t bit_cache;
  149. uint8_t *list[63];
  150. uint32_t *dst;
  151. const uint32_t *codebook;
  152. int entries[6];
  153. int i, j, m, n;
  154. int mean, stages;
  155. unsigned x, y, width, height, level;
  156. uint32_t n1, n2, n3, n4;
  157. /* initialize list for breadth first processing of vectors */
  158. list[0] = pixels;
  159. /* recursively process vector */
  160. for (i = 0, m = 1, n = 1, level = 5; i < n; i++) {
  161. SVQ1_PROCESS_VECTOR();
  162. /* destination address and vector size */
  163. dst = (uint32_t *)list[i];
  164. width = 1 << ((4 + level) / 2);
  165. height = 1 << ((3 + level) / 2);
  166. /* get number of stages (-1 skips vector, 0 for mean only) */
  167. stages = get_vlc2(bitbuf, svq1_intra_multistage[level].table, 3, 3) - 1;
  168. if (stages == -1) {
  169. for (y = 0; y < height; y++)
  170. memset(&dst[y * (pitch / 4)], 0, width);
  171. continue; /* skip vector */
  172. }
  173. if ((stages > 0 && level >= 4) || stages < 0) {
  174. ff_dlog(NULL,
  175. "Error (svq1_decode_block_intra): invalid vector: stages=%i level=%i\n",
  176. stages, level);
  177. return AVERROR_INVALIDDATA; /* invalid vector */
  178. }
  179. mean = get_vlc2(bitbuf, svq1_intra_mean.table, 8, 3);
  180. if (stages == 0) {
  181. for (y = 0; y < height; y++)
  182. memset(&dst[y * (pitch / 4)], mean, width);
  183. } else {
  184. SVQ1_CALC_CODEBOOK_ENTRIES(ff_svq1_intra_codebooks);
  185. for (y = 0; y < height; y++) {
  186. for (x = 0; x < width / 4; x++, codebook++) {
  187. n1 = n4;
  188. n2 = n4;
  189. SVQ1_ADD_CODEBOOK()
  190. /* store result */
  191. dst[x] = n1 << 8 | n2;
  192. }
  193. dst += pitch / 4;
  194. }
  195. }
  196. }
  197. return 0;
  198. }
  199. static int svq1_decode_block_non_intra(GetBitContext *bitbuf, uint8_t *pixels,
  200. int pitch)
  201. {
  202. uint32_t bit_cache;
  203. uint8_t *list[63];
  204. uint32_t *dst;
  205. const uint32_t *codebook;
  206. int entries[6];
  207. int i, j, m, n;
  208. int mean, stages;
  209. int x, y, width, height, level;
  210. uint32_t n1, n2, n3, n4;
  211. /* initialize list for breadth first processing of vectors */
  212. list[0] = pixels;
  213. /* recursively process vector */
  214. for (i = 0, m = 1, n = 1, level = 5; i < n; i++) {
  215. SVQ1_PROCESS_VECTOR();
  216. /* destination address and vector size */
  217. dst = (uint32_t *)list[i];
  218. width = 1 << ((4 + level) / 2);
  219. height = 1 << ((3 + level) / 2);
  220. /* get number of stages (-1 skips vector, 0 for mean only) */
  221. stages = get_vlc2(bitbuf, svq1_inter_multistage[level].table, 3, 2) - 1;
  222. if (stages == -1)
  223. continue; /* skip vector */
  224. if ((stages > 0 && level >= 4) || stages < 0) {
  225. ff_dlog(NULL,
  226. "Error (svq1_decode_block_non_intra): invalid vector: stages=%i level=%i\n",
  227. stages, level);
  228. return AVERROR_INVALIDDATA; /* 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. for (y = 0; y < height; y++) {
  233. for (x = 0; x < width / 4; x++, codebook++) {
  234. n3 = dst[x];
  235. /* add mean value to vector */
  236. n1 = n4 + ((n3 & 0xFF00FF00) >> 8);
  237. n2 = n4 + (n3 & 0x00FF00FF);
  238. SVQ1_ADD_CODEBOOK()
  239. /* store result */
  240. dst[x] = n1 << 8 | n2;
  241. }
  242. dst += pitch / 4;
  243. }
  244. }
  245. return 0;
  246. }
  247. static int svq1_decode_motion_vector(GetBitContext *bitbuf, svq1_pmv *mv,
  248. svq1_pmv **pmv)
  249. {
  250. int diff;
  251. int i;
  252. for (i = 0; i < 2; i++) {
  253. /* get motion code */
  254. diff = get_vlc2(bitbuf, svq1_motion_component.table, 7, 2);
  255. if (diff < 0)
  256. return AVERROR_INVALIDDATA;
  257. else if (diff) {
  258. if (get_bits1(bitbuf))
  259. diff = -diff;
  260. }
  261. /* add median of motion vector predictors and clip result */
  262. if (i == 1)
  263. mv->y = sign_extend(diff + mid_pred(pmv[0]->y, pmv[1]->y, pmv[2]->y), 6);
  264. else
  265. mv->x = sign_extend(diff + mid_pred(pmv[0]->x, pmv[1]->x, pmv[2]->x), 6);
  266. }
  267. return 0;
  268. }
  269. static void svq1_skip_block(uint8_t *current, uint8_t *previous,
  270. int pitch, int x, int y)
  271. {
  272. uint8_t *src;
  273. uint8_t *dst;
  274. int i;
  275. src = &previous[x + y * pitch];
  276. dst = current;
  277. for (i = 0; i < 16; i++) {
  278. memcpy(dst, src, 16);
  279. src += pitch;
  280. dst += pitch;
  281. }
  282. }
  283. static int svq1_motion_inter_block(HpelDSPContext *hdsp, GetBitContext *bitbuf,
  284. uint8_t *current, uint8_t *previous,
  285. int pitch, svq1_pmv *motion, int x, int y,
  286. int width, int height)
  287. {
  288. uint8_t *src;
  289. uint8_t *dst;
  290. svq1_pmv mv;
  291. svq1_pmv *pmv[3];
  292. int result;
  293. /* predict and decode motion vector */
  294. pmv[0] = &motion[0];
  295. if (y == 0) {
  296. pmv[1] =
  297. pmv[2] = pmv[0];
  298. } else {
  299. pmv[1] = &motion[x / 8 + 2];
  300. pmv[2] = &motion[x / 8 + 4];
  301. }
  302. result = svq1_decode_motion_vector(bitbuf, &mv, pmv);
  303. if (result != 0)
  304. return result;
  305. motion[0].x =
  306. motion[x / 8 + 2].x =
  307. motion[x / 8 + 3].x = mv.x;
  308. motion[0].y =
  309. motion[x / 8 + 2].y =
  310. motion[x / 8 + 3].y = mv.y;
  311. mv.x = av_clip(mv.x, -2 * x, 2 * (width - x - 16));
  312. mv.y = av_clip(mv.y, -2 * y, 2 * (height - y - 16));
  313. src = &previous[(x + (mv.x >> 1)) + (y + (mv.y >> 1)) * pitch];
  314. dst = current;
  315. hdsp->put_pixels_tab[0][(mv.y & 1) << 1 | (mv.x & 1)](dst, src, pitch, 16);
  316. return 0;
  317. }
  318. static int svq1_motion_inter_4v_block(HpelDSPContext *hdsp, GetBitContext *bitbuf,
  319. uint8_t *current, uint8_t *previous,
  320. int pitch, svq1_pmv *motion, int x, int y,
  321. int width, int height)
  322. {
  323. uint8_t *src;
  324. uint8_t *dst;
  325. svq1_pmv mv;
  326. svq1_pmv *pmv[4];
  327. int i, result;
  328. /* predict and decode motion vector (0) */
  329. pmv[0] = &motion[0];
  330. if (y == 0) {
  331. pmv[1] =
  332. pmv[2] = pmv[0];
  333. } else {
  334. pmv[1] = &motion[(x / 8) + 2];
  335. pmv[2] = &motion[(x / 8) + 4];
  336. }
  337. result = svq1_decode_motion_vector(bitbuf, &mv, pmv);
  338. if (result != 0)
  339. return result;
  340. /* predict and decode motion vector (1) */
  341. pmv[0] = &mv;
  342. if (y == 0) {
  343. pmv[1] =
  344. pmv[2] = pmv[0];
  345. } else {
  346. pmv[1] = &motion[(x / 8) + 3];
  347. }
  348. result = svq1_decode_motion_vector(bitbuf, &motion[0], pmv);
  349. if (result != 0)
  350. return result;
  351. /* predict and decode motion vector (2) */
  352. pmv[1] = &motion[0];
  353. pmv[2] = &motion[(x / 8) + 1];
  354. result = svq1_decode_motion_vector(bitbuf, &motion[(x / 8) + 2], pmv);
  355. if (result != 0)
  356. return result;
  357. /* predict and decode motion vector (3) */
  358. pmv[2] = &motion[(x / 8) + 2];
  359. pmv[3] = &motion[(x / 8) + 3];
  360. result = svq1_decode_motion_vector(bitbuf, pmv[3], pmv);
  361. if (result != 0)
  362. return result;
  363. /* form predictions */
  364. for (i = 0; i < 4; i++) {
  365. int mvx = pmv[i]->x + (i & 1) * 16;
  366. int mvy = pmv[i]->y + (i >> 1) * 16;
  367. // FIXME: clipping or padding?
  368. mvx = av_clip(mvx, -2 * x, 2 * (width - x - 8));
  369. mvy = av_clip(mvy, -2 * y, 2 * (height - y - 8));
  370. src = &previous[(x + (mvx >> 1)) + (y + (mvy >> 1)) * pitch];
  371. dst = current;
  372. hdsp->put_pixels_tab[1][((mvy & 1) << 1) | (mvx & 1)](dst, src, pitch, 8);
  373. /* select next block */
  374. if (i & 1)
  375. current += 8 * (pitch - 1);
  376. else
  377. current += 8;
  378. }
  379. return 0;
  380. }
  381. static int svq1_decode_delta_block(AVCodecContext *avctx, HpelDSPContext *hdsp,
  382. GetBitContext *bitbuf,
  383. uint8_t *current, uint8_t *previous,
  384. int pitch, svq1_pmv *motion, int x, int y,
  385. int width, int height)
  386. {
  387. uint32_t block_type;
  388. int result = 0;
  389. /* get block type */
  390. block_type = get_vlc2(bitbuf, svq1_block_type.table, 2, 2);
  391. /* reset motion vectors */
  392. if (block_type == SVQ1_BLOCK_SKIP || block_type == SVQ1_BLOCK_INTRA) {
  393. motion[0].x =
  394. motion[0].y =
  395. motion[x / 8 + 2].x =
  396. motion[x / 8 + 2].y =
  397. motion[x / 8 + 3].x =
  398. motion[x / 8 + 3].y = 0;
  399. }
  400. switch (block_type) {
  401. case SVQ1_BLOCK_SKIP:
  402. svq1_skip_block(current, previous, pitch, x, y);
  403. break;
  404. case SVQ1_BLOCK_INTER:
  405. result = svq1_motion_inter_block(hdsp, bitbuf, current, previous,
  406. pitch, motion, x, y, width, height);
  407. if (result != 0) {
  408. ff_dlog(avctx, "Error in svq1_motion_inter_block %i\n", result);
  409. break;
  410. }
  411. result = svq1_decode_block_non_intra(bitbuf, current, pitch);
  412. break;
  413. case SVQ1_BLOCK_INTER_4V:
  414. result = svq1_motion_inter_4v_block(hdsp, bitbuf, current, previous,
  415. pitch, motion, x, y, width, height);
  416. if (result != 0) {
  417. ff_dlog(avctx, "Error in svq1_motion_inter_4v_block %i\n", result);
  418. break;
  419. }
  420. result = svq1_decode_block_non_intra(bitbuf, current, pitch);
  421. break;
  422. case SVQ1_BLOCK_INTRA:
  423. result = svq1_decode_block_intra(bitbuf, current, pitch);
  424. break;
  425. }
  426. return result;
  427. }
  428. static void svq1_parse_string(GetBitContext *bitbuf, uint8_t *out)
  429. {
  430. uint8_t seed;
  431. int i;
  432. out[0] = get_bits(bitbuf, 8);
  433. seed = string_table[out[0]];
  434. for (i = 1; i <= out[0]; i++) {
  435. out[i] = get_bits(bitbuf, 8) ^ seed;
  436. seed = string_table[out[i] ^ seed];
  437. }
  438. }
  439. static int svq1_decode_frame_header(AVCodecContext *avctx, AVFrame *frame)
  440. {
  441. SVQ1Context *s = avctx->priv_data;
  442. GetBitContext *bitbuf = &s->gb;
  443. int frame_size_code;
  444. skip_bits(bitbuf, 8); /* temporal_reference */
  445. /* frame type */
  446. s->nonref = 0;
  447. switch (get_bits(bitbuf, 2)) {
  448. case 0:
  449. frame->pict_type = AV_PICTURE_TYPE_I;
  450. break;
  451. case 2:
  452. s->nonref = 1;
  453. case 1:
  454. frame->pict_type = AV_PICTURE_TYPE_P;
  455. break;
  456. default:
  457. av_log(avctx, AV_LOG_ERROR, "Invalid frame type.\n");
  458. return AVERROR_INVALIDDATA;
  459. }
  460. if (frame->pict_type == AV_PICTURE_TYPE_I) {
  461. /* unknown fields */
  462. if (s->frame_code == 0x50 || s->frame_code == 0x60) {
  463. int csum = get_bits(bitbuf, 16);
  464. csum = ff_svq1_packet_checksum(bitbuf->buffer,
  465. bitbuf->size_in_bits >> 3,
  466. csum);
  467. ff_dlog(avctx, "%s checksum (%02x) for packet data\n",
  468. (csum == 0) ? "correct" : "incorrect", csum);
  469. }
  470. if ((s->frame_code ^ 0x10) >= 0x50) {
  471. uint8_t msg[256];
  472. svq1_parse_string(bitbuf, msg);
  473. av_log(avctx, AV_LOG_INFO,
  474. "embedded message: \"%s\"\n", (char *)msg);
  475. }
  476. skip_bits(bitbuf, 2);
  477. skip_bits(bitbuf, 2);
  478. skip_bits1(bitbuf);
  479. /* load frame size */
  480. frame_size_code = get_bits(bitbuf, 3);
  481. if (frame_size_code == 7) {
  482. /* load width, height (12 bits each) */
  483. s->width = get_bits(bitbuf, 12);
  484. s->height = get_bits(bitbuf, 12);
  485. if (!s->width || !s->height)
  486. return AVERROR_INVALIDDATA;
  487. } else {
  488. /* get width, height from table */
  489. s->width = ff_svq1_frame_size_table[frame_size_code][0];
  490. s->height = ff_svq1_frame_size_table[frame_size_code][1];
  491. }
  492. }
  493. /* unknown fields */
  494. if (get_bits1(bitbuf) == 1) {
  495. skip_bits1(bitbuf); /* use packet checksum if (1) */
  496. skip_bits1(bitbuf); /* component checksums after image data if (1) */
  497. if (get_bits(bitbuf, 2) != 0)
  498. return AVERROR_INVALIDDATA;
  499. }
  500. if (get_bits1(bitbuf) == 1) {
  501. skip_bits1(bitbuf);
  502. skip_bits(bitbuf, 4);
  503. skip_bits1(bitbuf);
  504. skip_bits(bitbuf, 2);
  505. while (get_bits1(bitbuf) == 1)
  506. skip_bits(bitbuf, 8);
  507. }
  508. return 0;
  509. }
  510. static int svq1_decode_frame(AVCodecContext *avctx, void *data,
  511. int *got_frame, AVPacket *avpkt)
  512. {
  513. const uint8_t *buf = avpkt->data;
  514. int buf_size = avpkt->size;
  515. SVQ1Context *s = avctx->priv_data;
  516. AVFrame *cur = data;
  517. uint8_t *current;
  518. int result, i, x, y, width, height;
  519. svq1_pmv *pmv;
  520. /* initialize bit buffer */
  521. init_get_bits(&s->gb, buf, buf_size * 8);
  522. /* decode frame header */
  523. s->frame_code = get_bits(&s->gb, 22);
  524. if ((s->frame_code & ~0x70) || !(s->frame_code & 0x60))
  525. return AVERROR_INVALIDDATA;
  526. /* swap some header bytes (why?) */
  527. if (s->frame_code != 0x20) {
  528. uint32_t *src;
  529. if (buf_size < 9 * 4) {
  530. av_log(avctx, AV_LOG_ERROR, "Input packet too small\n");
  531. return AVERROR_INVALIDDATA;
  532. }
  533. av_fast_padded_malloc(&s->pkt_swapped,
  534. &s->pkt_swapped_allocated,
  535. buf_size);
  536. if (!s->pkt_swapped)
  537. return AVERROR(ENOMEM);
  538. memcpy(s->pkt_swapped, buf, buf_size);
  539. buf = s->pkt_swapped;
  540. init_get_bits(&s->gb, buf, buf_size * 8);
  541. skip_bits(&s->gb, 22);
  542. src = (uint32_t *)(s->pkt_swapped + 4);
  543. for (i = 0; i < 4; i++)
  544. src[i] = ((src[i] << 16) | (src[i] >> 16)) ^ src[7 - i];
  545. }
  546. result = svq1_decode_frame_header(avctx, cur);
  547. if (result != 0) {
  548. ff_dlog(avctx, "Error in svq1_decode_frame_header %i\n", result);
  549. return result;
  550. }
  551. result = ff_set_dimensions(avctx, s->width, s->height);
  552. if (result < 0)
  553. return result;
  554. if ((avctx->skip_frame >= AVDISCARD_NONREF && s->nonref) ||
  555. (avctx->skip_frame >= AVDISCARD_NONKEY &&
  556. cur->pict_type != AV_PICTURE_TYPE_I) ||
  557. avctx->skip_frame >= AVDISCARD_ALL)
  558. return buf_size;
  559. result = ff_get_buffer(avctx, cur, s->nonref ? 0 : AV_GET_BUFFER_FLAG_REF);
  560. if (result < 0)
  561. return result;
  562. pmv = av_malloc((FFALIGN(s->width, 16) / 8 + 3) * sizeof(*pmv));
  563. if (!pmv)
  564. return AVERROR(ENOMEM);
  565. /* decode y, u and v components */
  566. for (i = 0; i < 3; i++) {
  567. int linesize = cur->linesize[i];
  568. if (i == 0) {
  569. width = FFALIGN(s->width, 16);
  570. height = FFALIGN(s->height, 16);
  571. } else {
  572. if (avctx->flags & AV_CODEC_FLAG_GRAY)
  573. break;
  574. width = FFALIGN(s->width / 4, 16);
  575. height = FFALIGN(s->height / 4, 16);
  576. }
  577. current = cur->data[i];
  578. if (cur->pict_type == AV_PICTURE_TYPE_I) {
  579. /* keyframe */
  580. for (y = 0; y < height; y += 16) {
  581. for (x = 0; x < width; x += 16) {
  582. result = svq1_decode_block_intra(&s->gb, &current[x],
  583. linesize);
  584. if (result != 0) {
  585. av_log(avctx, AV_LOG_INFO,
  586. "Error in svq1_decode_block %i (keyframe)\n",
  587. result);
  588. goto err;
  589. }
  590. }
  591. current += 16 * linesize;
  592. }
  593. } else {
  594. /* delta frame */
  595. uint8_t *previous = s->prev->data[i];
  596. if (!previous ||
  597. s->prev->width != s->width || s->prev->height != s->height) {
  598. av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
  599. result = AVERROR_INVALIDDATA;
  600. goto err;
  601. }
  602. memset(pmv, 0, ((width / 8) + 3) * sizeof(svq1_pmv));
  603. for (y = 0; y < height; y += 16) {
  604. for (x = 0; x < width; x += 16) {
  605. result = svq1_decode_delta_block(avctx, &s->hdsp,
  606. &s->gb, &current[x],
  607. previous, linesize,
  608. pmv, x, y, width, height);
  609. if (result != 0) {
  610. ff_dlog(avctx,
  611. "Error in svq1_decode_delta_block %i\n",
  612. result);
  613. goto err;
  614. }
  615. }
  616. pmv[0].x =
  617. pmv[0].y = 0;
  618. current += 16 * linesize;
  619. }
  620. }
  621. }
  622. if (!s->nonref) {
  623. av_frame_unref(s->prev);
  624. result = av_frame_ref(s->prev, cur);
  625. if (result < 0)
  626. goto err;
  627. }
  628. *got_frame = 1;
  629. result = buf_size;
  630. err:
  631. av_free(pmv);
  632. return result;
  633. }
  634. static av_cold int svq1_decode_init(AVCodecContext *avctx)
  635. {
  636. SVQ1Context *s = avctx->priv_data;
  637. int i;
  638. int offset = 0;
  639. s->prev = av_frame_alloc();
  640. if (!s->prev)
  641. return AVERROR(ENOMEM);
  642. s->width = avctx->width + 3 & ~3;
  643. s->height = avctx->height + 3 & ~3;
  644. avctx->pix_fmt = AV_PIX_FMT_YUV410P;
  645. ff_hpeldsp_init(&s->hdsp, avctx->flags);
  646. INIT_VLC_STATIC(&svq1_block_type, 2, 4,
  647. &ff_svq1_block_type_vlc[0][1], 2, 1,
  648. &ff_svq1_block_type_vlc[0][0], 2, 1, 6);
  649. INIT_VLC_STATIC(&svq1_motion_component, 7, 33,
  650. &ff_mvtab[0][1], 2, 1,
  651. &ff_mvtab[0][0], 2, 1, 176);
  652. for (i = 0; i < 6; i++) {
  653. static const uint8_t sizes[2][6] = { { 14, 10, 14, 18, 16, 18 },
  654. { 10, 10, 14, 14, 14, 16 } };
  655. static VLC_TYPE table[168][2];
  656. svq1_intra_multistage[i].table = &table[offset];
  657. svq1_intra_multistage[i].table_allocated = sizes[0][i];
  658. offset += sizes[0][i];
  659. init_vlc(&svq1_intra_multistage[i], 3, 8,
  660. &ff_svq1_intra_multistage_vlc[i][0][1], 2, 1,
  661. &ff_svq1_intra_multistage_vlc[i][0][0], 2, 1,
  662. INIT_VLC_USE_NEW_STATIC);
  663. svq1_inter_multistage[i].table = &table[offset];
  664. svq1_inter_multistage[i].table_allocated = sizes[1][i];
  665. offset += sizes[1][i];
  666. init_vlc(&svq1_inter_multistage[i], 3, 8,
  667. &ff_svq1_inter_multistage_vlc[i][0][1], 2, 1,
  668. &ff_svq1_inter_multistage_vlc[i][0][0], 2, 1,
  669. INIT_VLC_USE_NEW_STATIC);
  670. }
  671. INIT_VLC_STATIC(&svq1_intra_mean, 8, 256,
  672. &ff_svq1_intra_mean_vlc[0][1], 4, 2,
  673. &ff_svq1_intra_mean_vlc[0][0], 4, 2, 632);
  674. INIT_VLC_STATIC(&svq1_inter_mean, 9, 512,
  675. &ff_svq1_inter_mean_vlc[0][1], 4, 2,
  676. &ff_svq1_inter_mean_vlc[0][0], 4, 2, 1434);
  677. return 0;
  678. }
  679. static av_cold int svq1_decode_end(AVCodecContext *avctx)
  680. {
  681. SVQ1Context *s = avctx->priv_data;
  682. av_frame_free(&s->prev);
  683. av_freep(&s->pkt_swapped);
  684. return 0;
  685. }
  686. static void svq1_flush(AVCodecContext *avctx)
  687. {
  688. SVQ1Context *s = avctx->priv_data;
  689. av_frame_unref(s->prev);
  690. }
  691. AVCodec ff_svq1_decoder = {
  692. .name = "svq1",
  693. .long_name = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 1 / Sorenson Video 1 / SVQ1"),
  694. .type = AVMEDIA_TYPE_VIDEO,
  695. .id = AV_CODEC_ID_SVQ1,
  696. .priv_data_size = sizeof(SVQ1Context),
  697. .init = svq1_decode_init,
  698. .close = svq1_decode_end,
  699. .decode = svq1_decode_frame,
  700. .capabilities = AV_CODEC_CAP_DR1,
  701. .flush = svq1_flush,
  702. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV410P,
  703. AV_PIX_FMT_NONE },
  704. };