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 "bitstream.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. BitstreamContext bc;
  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 (bitstream_read_bit(bc) == 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 = bitstream_read(bc, 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(BitstreamContext *bc, uint8_t *pixels,
  146. ptrdiff_t 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 = bitstream_read_vlc(bc, 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 = bitstream_read_vlc(bc, 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(BitstreamContext *bc, uint8_t *pixels,
  200. ptrdiff_t 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 = bitstream_read_vlc(bc, 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 = bitstream_read_vlc(bc, 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(BitstreamContext *bc, 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 = bitstream_read_vlc(bc, svq1_motion_component.table, 7, 2);
  255. if (diff < 0)
  256. return AVERROR_INVALIDDATA;
  257. else if (diff) {
  258. if (bitstream_read_bit(bc))
  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. ptrdiff_t 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, BitstreamContext *bc,
  284. uint8_t *current, uint8_t *previous,
  285. ptrdiff_t 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(bc, &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, BitstreamContext *bc,
  319. uint8_t *current, uint8_t *previous,
  320. ptrdiff_t 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(bc, &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(bc, &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(bc, &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(bc, 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. BitstreamContext *bc,
  383. uint8_t *current, uint8_t *previous,
  384. ptrdiff_t 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 = bitstream_read_vlc(bc, 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, bc, 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(bc, current, pitch);
  412. break;
  413. case SVQ1_BLOCK_INTER_4V:
  414. result = svq1_motion_inter_4v_block(hdsp, bc, 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(bc, current, pitch);
  421. break;
  422. case SVQ1_BLOCK_INTRA:
  423. result = svq1_decode_block_intra(bc, current, pitch);
  424. break;
  425. }
  426. return result;
  427. }
  428. static void svq1_parse_string(BitstreamContext *bc, uint8_t *out)
  429. {
  430. uint8_t seed;
  431. int i;
  432. out[0] = bitstream_read(bc, 8);
  433. seed = string_table[out[0]];
  434. for (i = 1; i <= out[0]; i++) {
  435. out[i] = bitstream_read(bc, 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. BitstreamContext *bc = &s->bc;
  443. int frame_size_code;
  444. bitstream_skip(bc, 8); /* temporal_reference */
  445. /* frame type */
  446. s->nonref = 0;
  447. switch (bitstream_read(bc, 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 = bitstream_read(bc, 16);
  464. csum = ff_svq1_packet_checksum(bc->buffer, bc->size_in_bits >> 3,
  465. csum);
  466. ff_dlog(avctx, "%s checksum (%02x) for packet data\n",
  467. (csum == 0) ? "correct" : "incorrect", csum);
  468. }
  469. if ((s->frame_code ^ 0x10) >= 0x50) {
  470. uint8_t msg[256];
  471. svq1_parse_string(bc, msg);
  472. av_log(avctx, AV_LOG_INFO,
  473. "embedded message: \"%s\"\n", (char *)msg);
  474. }
  475. bitstream_skip(bc, 2);
  476. bitstream_skip(bc, 2);
  477. bitstream_skip(bc, 1);
  478. /* load frame size */
  479. frame_size_code = bitstream_read(bc, 3);
  480. if (frame_size_code == 7) {
  481. /* load width, height (12 bits each) */
  482. s->width = bitstream_read(bc, 12);
  483. s->height = bitstream_read(bc, 12);
  484. if (!s->width || !s->height)
  485. return AVERROR_INVALIDDATA;
  486. } else {
  487. /* get width, height from table */
  488. s->width = ff_svq1_frame_size_table[frame_size_code][0];
  489. s->height = ff_svq1_frame_size_table[frame_size_code][1];
  490. }
  491. }
  492. /* unknown fields */
  493. if (bitstream_read_bit(bc) == 1) {
  494. bitstream_skip(bc, 1); /* use packet checksum if (1) */
  495. bitstream_skip(bc, 1); /* component checksums after image data if (1) */
  496. if (bitstream_read(bc, 2) != 0)
  497. return AVERROR_INVALIDDATA;
  498. }
  499. if (bitstream_read_bit(bc) == 1) {
  500. bitstream_skip(bc, 1);
  501. bitstream_skip(bc, 4);
  502. bitstream_skip(bc, 1);
  503. bitstream_skip(bc, 2);
  504. while (bitstream_read_bit(bc) == 1)
  505. bitstream_skip(bc, 8);
  506. }
  507. return 0;
  508. }
  509. static int svq1_decode_frame(AVCodecContext *avctx, void *data,
  510. int *got_frame, AVPacket *avpkt)
  511. {
  512. const uint8_t *buf = avpkt->data;
  513. int buf_size = avpkt->size;
  514. SVQ1Context *s = avctx->priv_data;
  515. AVFrame *cur = data;
  516. uint8_t *current;
  517. int result, i, x, y, width, height;
  518. svq1_pmv *pmv;
  519. /* initialize bit buffer */
  520. bitstream_init8(&s->bc, buf, buf_size);
  521. /* decode frame header */
  522. s->frame_code = bitstream_read(&s->bc, 22);
  523. if ((s->frame_code & ~0x70) || !(s->frame_code & 0x60))
  524. return AVERROR_INVALIDDATA;
  525. /* swap some header bytes (why?) */
  526. if (s->frame_code != 0x20) {
  527. uint32_t *src;
  528. if (buf_size < 9 * 4) {
  529. av_log(avctx, AV_LOG_ERROR, "Input packet too small\n");
  530. return AVERROR_INVALIDDATA;
  531. }
  532. av_fast_padded_malloc(&s->pkt_swapped,
  533. &s->pkt_swapped_allocated,
  534. buf_size);
  535. if (!s->pkt_swapped)
  536. return AVERROR(ENOMEM);
  537. memcpy(s->pkt_swapped, buf, buf_size);
  538. buf = s->pkt_swapped;
  539. src = (uint32_t *)(s->pkt_swapped + 4);
  540. for (i = 0; i < 4; i++)
  541. src[i] = ((src[i] << 16) | (src[i] >> 16)) ^ src[7 - i];
  542. bitstream_init8(&s->bc, buf, buf_size);
  543. bitstream_skip(&s->bc, 22);
  544. }
  545. result = svq1_decode_frame_header(avctx, cur);
  546. if (result != 0) {
  547. ff_dlog(avctx, "Error in svq1_decode_frame_header %i\n", result);
  548. return result;
  549. }
  550. result = ff_set_dimensions(avctx, s->width, s->height);
  551. if (result < 0)
  552. return result;
  553. if ((avctx->skip_frame >= AVDISCARD_NONREF && s->nonref) ||
  554. (avctx->skip_frame >= AVDISCARD_NONKEY &&
  555. cur->pict_type != AV_PICTURE_TYPE_I) ||
  556. avctx->skip_frame >= AVDISCARD_ALL)
  557. return buf_size;
  558. result = ff_get_buffer(avctx, cur, s->nonref ? 0 : AV_GET_BUFFER_FLAG_REF);
  559. if (result < 0)
  560. return result;
  561. pmv = av_malloc((FFALIGN(s->width, 16) / 8 + 3) * sizeof(*pmv));
  562. if (!pmv)
  563. return AVERROR(ENOMEM);
  564. /* decode y, u and v components */
  565. for (i = 0; i < 3; i++) {
  566. int linesize = cur->linesize[i];
  567. if (i == 0) {
  568. width = FFALIGN(s->width, 16);
  569. height = FFALIGN(s->height, 16);
  570. } else {
  571. if (avctx->flags & AV_CODEC_FLAG_GRAY)
  572. break;
  573. width = FFALIGN(s->width / 4, 16);
  574. height = FFALIGN(s->height / 4, 16);
  575. }
  576. current = cur->data[i];
  577. if (cur->pict_type == AV_PICTURE_TYPE_I) {
  578. /* keyframe */
  579. for (y = 0; y < height; y += 16) {
  580. for (x = 0; x < width; x += 16) {
  581. result = svq1_decode_block_intra(&s->bc, &current[x],
  582. linesize);
  583. if (result != 0) {
  584. av_log(avctx, AV_LOG_INFO,
  585. "Error in svq1_decode_block %i (keyframe)\n",
  586. result);
  587. goto err;
  588. }
  589. }
  590. current += 16 * linesize;
  591. }
  592. } else {
  593. /* delta frame */
  594. uint8_t *previous = s->prev->data[i];
  595. if (!previous ||
  596. s->prev->width != s->width || s->prev->height != s->height) {
  597. av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
  598. result = AVERROR_INVALIDDATA;
  599. goto err;
  600. }
  601. memset(pmv, 0, ((width / 8) + 3) * sizeof(svq1_pmv));
  602. for (y = 0; y < height; y += 16) {
  603. for (x = 0; x < width; x += 16) {
  604. result = svq1_decode_delta_block(avctx, &s->hdsp,
  605. &s->bc, &current[x],
  606. previous, linesize,
  607. pmv, x, y, width, height);
  608. if (result != 0) {
  609. ff_dlog(avctx,
  610. "Error in svq1_decode_delta_block %i\n",
  611. result);
  612. goto err;
  613. }
  614. }
  615. pmv[0].x =
  616. pmv[0].y = 0;
  617. current += 16 * linesize;
  618. }
  619. }
  620. }
  621. if (!s->nonref) {
  622. av_frame_unref(s->prev);
  623. result = av_frame_ref(s->prev, cur);
  624. if (result < 0)
  625. goto err;
  626. }
  627. *got_frame = 1;
  628. result = buf_size;
  629. err:
  630. av_free(pmv);
  631. return result;
  632. }
  633. static av_cold int svq1_decode_init(AVCodecContext *avctx)
  634. {
  635. SVQ1Context *s = avctx->priv_data;
  636. int i;
  637. int offset = 0;
  638. s->prev = av_frame_alloc();
  639. if (!s->prev)
  640. return AVERROR(ENOMEM);
  641. s->width = avctx->width + 3 & ~3;
  642. s->height = avctx->height + 3 & ~3;
  643. avctx->pix_fmt = AV_PIX_FMT_YUV410P;
  644. ff_hpeldsp_init(&s->hdsp, avctx->flags);
  645. INIT_VLC_STATIC(&svq1_block_type, 2, 4,
  646. &ff_svq1_block_type_vlc[0][1], 2, 1,
  647. &ff_svq1_block_type_vlc[0][0], 2, 1, 6);
  648. INIT_VLC_STATIC(&svq1_motion_component, 7, 33,
  649. &ff_mvtab[0][1], 2, 1,
  650. &ff_mvtab[0][0], 2, 1, 176);
  651. for (i = 0; i < 6; i++) {
  652. static const uint8_t sizes[2][6] = { { 14, 10, 14, 18, 16, 18 },
  653. { 10, 10, 14, 14, 14, 16 } };
  654. static VLC_TYPE table[168][2];
  655. svq1_intra_multistage[i].table = &table[offset];
  656. svq1_intra_multistage[i].table_allocated = sizes[0][i];
  657. offset += sizes[0][i];
  658. init_vlc(&svq1_intra_multistage[i], 3, 8,
  659. &ff_svq1_intra_multistage_vlc[i][0][1], 2, 1,
  660. &ff_svq1_intra_multistage_vlc[i][0][0], 2, 1,
  661. INIT_VLC_USE_NEW_STATIC);
  662. svq1_inter_multistage[i].table = &table[offset];
  663. svq1_inter_multistage[i].table_allocated = sizes[1][i];
  664. offset += sizes[1][i];
  665. init_vlc(&svq1_inter_multistage[i], 3, 8,
  666. &ff_svq1_inter_multistage_vlc[i][0][1], 2, 1,
  667. &ff_svq1_inter_multistage_vlc[i][0][0], 2, 1,
  668. INIT_VLC_USE_NEW_STATIC);
  669. }
  670. INIT_VLC_STATIC(&svq1_intra_mean, 8, 256,
  671. &ff_svq1_intra_mean_vlc[0][1], 4, 2,
  672. &ff_svq1_intra_mean_vlc[0][0], 4, 2, 632);
  673. INIT_VLC_STATIC(&svq1_inter_mean, 9, 512,
  674. &ff_svq1_inter_mean_vlc[0][1], 4, 2,
  675. &ff_svq1_inter_mean_vlc[0][0], 4, 2, 1434);
  676. return 0;
  677. }
  678. static av_cold int svq1_decode_end(AVCodecContext *avctx)
  679. {
  680. SVQ1Context *s = avctx->priv_data;
  681. av_frame_free(&s->prev);
  682. av_freep(&s->pkt_swapped);
  683. return 0;
  684. }
  685. static void svq1_flush(AVCodecContext *avctx)
  686. {
  687. SVQ1Context *s = avctx->priv_data;
  688. av_frame_unref(s->prev);
  689. }
  690. AVCodec ff_svq1_decoder = {
  691. .name = "svq1",
  692. .long_name = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 1 / Sorenson Video 1 / SVQ1"),
  693. .type = AVMEDIA_TYPE_VIDEO,
  694. .id = AV_CODEC_ID_SVQ1,
  695. .priv_data_size = sizeof(SVQ1Context),
  696. .init = svq1_decode_init,
  697. .close = svq1_decode_end,
  698. .decode = svq1_decode_frame,
  699. .capabilities = AV_CODEC_CAP_DR1,
  700. .flush = svq1_flush,
  701. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV410P,
  702. AV_PIX_FMT_NONE },
  703. };