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.

1376 lines
49KB

  1. /*
  2. * Copyright (c) 2003 The Libav Project
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /*
  21. * How to use this decoder:
  22. * SVQ3 data is transported within Apple Quicktime files. Quicktime files
  23. * have stsd atoms to describe media trak properties. A stsd atom for a
  24. * video trak contains 1 or more ImageDescription atoms. These atoms begin
  25. * with the 4-byte length of the atom followed by the codec fourcc. Some
  26. * decoders need information in this atom to operate correctly. Such
  27. * is the case with SVQ3. In order to get the best use out of this decoder,
  28. * the calling app must make the SVQ3 ImageDescription atom available
  29. * via the AVCodecContext's extradata[_size] field:
  30. *
  31. * AVCodecContext.extradata = pointer to ImageDescription, first characters
  32. * are expected to be 'S', 'V', 'Q', and '3', NOT the 4-byte atom length
  33. * AVCodecContext.extradata_size = size of ImageDescription atom memory
  34. * buffer (which will be the same as the ImageDescription atom size field
  35. * from the QT file, minus 4 bytes since the length is missing)
  36. *
  37. * You will know you have these parameters passed correctly when the decoder
  38. * correctly decodes this file:
  39. * http://samples.libav.org/V-codecs/SVQ3/Vertical400kbit.sorenson3.mov
  40. */
  41. #include <inttypes.h>
  42. #include "libavutil/attributes.h"
  43. #include "internal.h"
  44. #include "avcodec.h"
  45. #include "mpegutils.h"
  46. #include "h264.h"
  47. #include "h264data.h" // FIXME FIXME FIXME
  48. #include "h264_mvpred.h"
  49. #include "golomb.h"
  50. #include "hpeldsp.h"
  51. #include "rectangle.h"
  52. #include "tpeldsp.h"
  53. #if CONFIG_ZLIB
  54. #include <zlib.h>
  55. #endif
  56. #include "svq1.h"
  57. #include "svq3.h"
  58. /**
  59. * @file
  60. * svq3 decoder.
  61. */
  62. typedef struct SVQ3Context {
  63. H264Context h;
  64. HpelDSPContext hdsp;
  65. TpelDSPContext tdsp;
  66. H264Picture *cur_pic;
  67. H264Picture *next_pic;
  68. H264Picture *last_pic;
  69. GetBitContext gb;
  70. uint8_t *slice_buf;
  71. int slice_size;
  72. int halfpel_flag;
  73. int thirdpel_flag;
  74. int unknown_flag;
  75. uint32_t watermark_key;
  76. int adaptive_quant;
  77. int next_p_frame_damaged;
  78. int h_edge_pos;
  79. int v_edge_pos;
  80. int last_frame_output;
  81. } SVQ3Context;
  82. #define FULLPEL_MODE 1
  83. #define HALFPEL_MODE 2
  84. #define THIRDPEL_MODE 3
  85. #define PREDICT_MODE 4
  86. /* dual scan (from some older h264 draft)
  87. * o-->o-->o o
  88. * | /|
  89. * o o o / o
  90. * | / | |/ |
  91. * o o o o
  92. * /
  93. * o-->o-->o-->o
  94. */
  95. static const uint8_t svq3_scan[16] = {
  96. 0 + 0 * 4, 1 + 0 * 4, 2 + 0 * 4, 2 + 1 * 4,
  97. 2 + 2 * 4, 3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4,
  98. 0 + 1 * 4, 0 + 2 * 4, 1 + 1 * 4, 1 + 2 * 4,
  99. 0 + 3 * 4, 1 + 3 * 4, 2 + 3 * 4, 3 + 3 * 4,
  100. };
  101. static const uint8_t luma_dc_zigzag_scan[16] = {
  102. 0 * 16 + 0 * 64, 1 * 16 + 0 * 64, 2 * 16 + 0 * 64, 0 * 16 + 2 * 64,
  103. 3 * 16 + 0 * 64, 0 * 16 + 1 * 64, 1 * 16 + 1 * 64, 2 * 16 + 1 * 64,
  104. 1 * 16 + 2 * 64, 2 * 16 + 2 * 64, 3 * 16 + 2 * 64, 0 * 16 + 3 * 64,
  105. 3 * 16 + 1 * 64, 1 * 16 + 3 * 64, 2 * 16 + 3 * 64, 3 * 16 + 3 * 64,
  106. };
  107. static const uint8_t svq3_pred_0[25][2] = {
  108. { 0, 0 },
  109. { 1, 0 }, { 0, 1 },
  110. { 0, 2 }, { 1, 1 }, { 2, 0 },
  111. { 3, 0 }, { 2, 1 }, { 1, 2 }, { 0, 3 },
  112. { 0, 4 }, { 1, 3 }, { 2, 2 }, { 3, 1 }, { 4, 0 },
  113. { 4, 1 }, { 3, 2 }, { 2, 3 }, { 1, 4 },
  114. { 2, 4 }, { 3, 3 }, { 4, 2 },
  115. { 4, 3 }, { 3, 4 },
  116. { 4, 4 }
  117. };
  118. static const int8_t svq3_pred_1[6][6][5] = {
  119. { { 2, -1, -1, -1, -1 }, { 2, 1, -1, -1, -1 }, { 1, 2, -1, -1, -1 },
  120. { 2, 1, -1, -1, -1 }, { 1, 2, -1, -1, -1 }, { 1, 2, -1, -1, -1 } },
  121. { { 0, 2, -1, -1, -1 }, { 0, 2, 1, 4, 3 }, { 0, 1, 2, 4, 3 },
  122. { 0, 2, 1, 4, 3 }, { 2, 0, 1, 3, 4 }, { 0, 4, 2, 1, 3 } },
  123. { { 2, 0, -1, -1, -1 }, { 2, 1, 0, 4, 3 }, { 1, 2, 4, 0, 3 },
  124. { 2, 1, 0, 4, 3 }, { 2, 1, 4, 3, 0 }, { 1, 2, 4, 0, 3 } },
  125. { { 2, 0, -1, -1, -1 }, { 2, 0, 1, 4, 3 }, { 1, 2, 0, 4, 3 },
  126. { 2, 1, 0, 4, 3 }, { 2, 1, 3, 4, 0 }, { 2, 4, 1, 0, 3 } },
  127. { { 0, 2, -1, -1, -1 }, { 0, 2, 1, 3, 4 }, { 1, 2, 3, 0, 4 },
  128. { 2, 0, 1, 3, 4 }, { 2, 1, 3, 0, 4 }, { 2, 0, 4, 3, 1 } },
  129. { { 0, 2, -1, -1, -1 }, { 0, 2, 4, 1, 3 }, { 1, 4, 2, 0, 3 },
  130. { 4, 2, 0, 1, 3 }, { 2, 0, 1, 4, 3 }, { 4, 2, 1, 0, 3 } },
  131. };
  132. static const struct {
  133. uint8_t run;
  134. uint8_t level;
  135. } svq3_dct_tables[2][16] = {
  136. { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 2, 1 }, { 0, 2 }, { 3, 1 }, { 4, 1 }, { 5, 1 },
  137. { 0, 3 }, { 1, 2 }, { 2, 2 }, { 6, 1 }, { 7, 1 }, { 8, 1 }, { 9, 1 }, { 0, 4 } },
  138. { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 0, 2 }, { 2, 1 }, { 0, 3 }, { 0, 4 }, { 0, 5 },
  139. { 3, 1 }, { 4, 1 }, { 1, 2 }, { 1, 3 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, { 0, 9 } }
  140. };
  141. static const uint32_t svq3_dequant_coeff[32] = {
  142. 3881, 4351, 4890, 5481, 6154, 6914, 7761, 8718,
  143. 9781, 10987, 12339, 13828, 15523, 17435, 19561, 21873,
  144. 24552, 27656, 30847, 34870, 38807, 43747, 49103, 54683,
  145. 61694, 68745, 77615, 89113, 100253, 109366, 126635, 141533
  146. };
  147. void ff_svq3_luma_dc_dequant_idct_c(int16_t *output, int16_t *input, int qp)
  148. {
  149. const int qmul = svq3_dequant_coeff[qp];
  150. #define stride 16
  151. int i;
  152. int temp[16];
  153. static const uint8_t x_offset[4] = { 0, 1 * stride, 4 * stride, 5 * stride };
  154. for (i = 0; i < 4; i++) {
  155. const int z0 = 13 * (input[4 * i + 0] + input[4 * i + 2]);
  156. const int z1 = 13 * (input[4 * i + 0] - input[4 * i + 2]);
  157. const int z2 = 7 * input[4 * i + 1] - 17 * input[4 * i + 3];
  158. const int z3 = 17 * input[4 * i + 1] + 7 * input[4 * i + 3];
  159. temp[4 * i + 0] = z0 + z3;
  160. temp[4 * i + 1] = z1 + z2;
  161. temp[4 * i + 2] = z1 - z2;
  162. temp[4 * i + 3] = z0 - z3;
  163. }
  164. for (i = 0; i < 4; i++) {
  165. const int offset = x_offset[i];
  166. const int z0 = 13 * (temp[4 * 0 + i] + temp[4 * 2 + i]);
  167. const int z1 = 13 * (temp[4 * 0 + i] - temp[4 * 2 + i]);
  168. const int z2 = 7 * temp[4 * 1 + i] - 17 * temp[4 * 3 + i];
  169. const int z3 = 17 * temp[4 * 1 + i] + 7 * temp[4 * 3 + i];
  170. output[stride * 0 + offset] = (z0 + z3) * qmul + 0x80000 >> 20;
  171. output[stride * 2 + offset] = (z1 + z2) * qmul + 0x80000 >> 20;
  172. output[stride * 8 + offset] = (z1 - z2) * qmul + 0x80000 >> 20;
  173. output[stride * 10 + offset] = (z0 - z3) * qmul + 0x80000 >> 20;
  174. }
  175. }
  176. #undef stride
  177. void ff_svq3_add_idct_c(uint8_t *dst, int16_t *block,
  178. int stride, int qp, int dc)
  179. {
  180. const int qmul = svq3_dequant_coeff[qp];
  181. int i;
  182. if (dc) {
  183. dc = 13 * 13 * (dc == 1 ? 1538 * block[0]
  184. : qmul * (block[0] >> 3) / 2);
  185. block[0] = 0;
  186. }
  187. for (i = 0; i < 4; i++) {
  188. const int z0 = 13 * (block[0 + 4 * i] + block[2 + 4 * i]);
  189. const int z1 = 13 * (block[0 + 4 * i] - block[2 + 4 * i]);
  190. const int z2 = 7 * block[1 + 4 * i] - 17 * block[3 + 4 * i];
  191. const int z3 = 17 * block[1 + 4 * i] + 7 * block[3 + 4 * i];
  192. block[0 + 4 * i] = z0 + z3;
  193. block[1 + 4 * i] = z1 + z2;
  194. block[2 + 4 * i] = z1 - z2;
  195. block[3 + 4 * i] = z0 - z3;
  196. }
  197. for (i = 0; i < 4; i++) {
  198. const int z0 = 13 * (block[i + 4 * 0] + block[i + 4 * 2]);
  199. const int z1 = 13 * (block[i + 4 * 0] - block[i + 4 * 2]);
  200. const int z2 = 7 * block[i + 4 * 1] - 17 * block[i + 4 * 3];
  201. const int z3 = 17 * block[i + 4 * 1] + 7 * block[i + 4 * 3];
  202. const int rr = (dc + 0x80000);
  203. dst[i + stride * 0] = av_clip_uint8(dst[i + stride * 0] + ((z0 + z3) * qmul + rr >> 20));
  204. dst[i + stride * 1] = av_clip_uint8(dst[i + stride * 1] + ((z1 + z2) * qmul + rr >> 20));
  205. dst[i + stride * 2] = av_clip_uint8(dst[i + stride * 2] + ((z1 - z2) * qmul + rr >> 20));
  206. dst[i + stride * 3] = av_clip_uint8(dst[i + stride * 3] + ((z0 - z3) * qmul + rr >> 20));
  207. }
  208. memset(block, 0, 16 * sizeof(int16_t));
  209. }
  210. static inline int svq3_decode_block(GetBitContext *gb, int16_t *block,
  211. int index, const int type)
  212. {
  213. static const uint8_t *const scan_patterns[4] =
  214. { luma_dc_zigzag_scan, zigzag_scan, svq3_scan, chroma_dc_scan };
  215. int run, level, limit;
  216. unsigned vlc;
  217. const int intra = 3 * type >> 2;
  218. const uint8_t *const scan = scan_patterns[type];
  219. for (limit = (16 >> intra); index < 16; index = limit, limit += 8) {
  220. for (; (vlc = svq3_get_ue_golomb(gb)) != 0; index++) {
  221. int sign = (vlc & 1) ? 0 : -1;
  222. vlc = vlc + 1 >> 1;
  223. if (type == 3) {
  224. if (vlc < 3) {
  225. run = 0;
  226. level = vlc;
  227. } else if (vlc < 4) {
  228. run = 1;
  229. level = 1;
  230. } else {
  231. run = vlc & 0x3;
  232. level = (vlc + 9 >> 2) - run;
  233. }
  234. } else {
  235. if (vlc < 16) {
  236. run = svq3_dct_tables[intra][vlc].run;
  237. level = svq3_dct_tables[intra][vlc].level;
  238. } else if (intra) {
  239. run = vlc & 0x7;
  240. level = (vlc >> 3) +
  241. ((run == 0) ? 8 : ((run < 2) ? 2 : ((run < 5) ? 0 : -1)));
  242. } else {
  243. run = vlc & 0xF;
  244. level = (vlc >> 4) +
  245. ((run == 0) ? 4 : ((run < 3) ? 2 : ((run < 10) ? 1 : 0)));
  246. }
  247. }
  248. if ((index += run) >= limit)
  249. return -1;
  250. block[scan[index]] = (level ^ sign) - sign;
  251. }
  252. if (type != 2) {
  253. break;
  254. }
  255. }
  256. return 0;
  257. }
  258. static inline void svq3_mc_dir_part(SVQ3Context *s,
  259. int x, int y, int width, int height,
  260. int mx, int my, int dxy,
  261. int thirdpel, int dir, int avg)
  262. {
  263. H264Context *h = &s->h;
  264. H264SliceContext *sl = &h->slice_ctx[0];
  265. const H264Picture *pic = (dir == 0) ? s->last_pic : s->next_pic;
  266. uint8_t *src, *dest;
  267. int i, emu = 0;
  268. int blocksize = 2 - (width >> 3); // 16->0, 8->1, 4->2
  269. mx += x;
  270. my += y;
  271. if (mx < 0 || mx >= s->h_edge_pos - width - 1 ||
  272. my < 0 || my >= s->v_edge_pos - height - 1) {
  273. emu = 1;
  274. mx = av_clip(mx, -16, s->h_edge_pos - width + 15);
  275. my = av_clip(my, -16, s->v_edge_pos - height + 15);
  276. }
  277. /* form component predictions */
  278. dest = h->cur_pic.f->data[0] + x + y * sl->linesize;
  279. src = pic->f->data[0] + mx + my * sl->linesize;
  280. if (emu) {
  281. h->vdsp.emulated_edge_mc(sl->edge_emu_buffer, src,
  282. sl->linesize, sl->linesize,
  283. width + 1, height + 1,
  284. mx, my, s->h_edge_pos, s->v_edge_pos);
  285. src = sl->edge_emu_buffer;
  286. }
  287. if (thirdpel)
  288. (avg ? s->tdsp.avg_tpel_pixels_tab
  289. : s->tdsp.put_tpel_pixels_tab)[dxy](dest, src, sl->linesize,
  290. width, height);
  291. else
  292. (avg ? s->hdsp.avg_pixels_tab
  293. : s->hdsp.put_pixels_tab)[blocksize][dxy](dest, src, sl->linesize,
  294. height);
  295. if (!(h->flags & AV_CODEC_FLAG_GRAY)) {
  296. mx = mx + (mx < (int) x) >> 1;
  297. my = my + (my < (int) y) >> 1;
  298. width = width >> 1;
  299. height = height >> 1;
  300. blocksize++;
  301. for (i = 1; i < 3; i++) {
  302. dest = h->cur_pic.f->data[i] + (x >> 1) + (y >> 1) * sl->uvlinesize;
  303. src = pic->f->data[i] + mx + my * sl->uvlinesize;
  304. if (emu) {
  305. h->vdsp.emulated_edge_mc(sl->edge_emu_buffer, src,
  306. sl->uvlinesize, sl->uvlinesize,
  307. width + 1, height + 1,
  308. mx, my, (s->h_edge_pos >> 1),
  309. s->v_edge_pos >> 1);
  310. src = sl->edge_emu_buffer;
  311. }
  312. if (thirdpel)
  313. (avg ? s->tdsp.avg_tpel_pixels_tab
  314. : s->tdsp.put_tpel_pixels_tab)[dxy](dest, src,
  315. sl->uvlinesize,
  316. width, height);
  317. else
  318. (avg ? s->hdsp.avg_pixels_tab
  319. : s->hdsp.put_pixels_tab)[blocksize][dxy](dest, src,
  320. sl->uvlinesize,
  321. height);
  322. }
  323. }
  324. }
  325. static inline int svq3_mc_dir(SVQ3Context *s, int size, int mode,
  326. int dir, int avg)
  327. {
  328. int i, j, k, mx, my, dx, dy, x, y;
  329. H264Context *h = &s->h;
  330. H264SliceContext *sl = &h->slice_ctx[0];
  331. const int part_width = ((size & 5) == 4) ? 4 : 16 >> (size & 1);
  332. const int part_height = 16 >> ((unsigned)(size + 1) / 3);
  333. const int extra_width = (mode == PREDICT_MODE) ? -16 * 6 : 0;
  334. const int h_edge_pos = 6 * (s->h_edge_pos - part_width) - extra_width;
  335. const int v_edge_pos = 6 * (s->v_edge_pos - part_height) - extra_width;
  336. for (i = 0; i < 16; i += part_height)
  337. for (j = 0; j < 16; j += part_width) {
  338. const int b_xy = (4 * sl->mb_x + (j >> 2)) +
  339. (4 * sl->mb_y + (i >> 2)) * h->b_stride;
  340. int dxy;
  341. x = 16 * sl->mb_x + j;
  342. y = 16 * sl->mb_y + i;
  343. k = (j >> 2 & 1) + (i >> 1 & 2) +
  344. (j >> 1 & 4) + (i & 8);
  345. if (mode != PREDICT_MODE) {
  346. pred_motion(h, sl, k, part_width >> 2, dir, 1, &mx, &my);
  347. } else {
  348. mx = s->next_pic->motion_val[0][b_xy][0] << 1;
  349. my = s->next_pic->motion_val[0][b_xy][1] << 1;
  350. if (dir == 0) {
  351. mx = mx * h->frame_num_offset /
  352. h->prev_frame_num_offset + 1 >> 1;
  353. my = my * h->frame_num_offset /
  354. h->prev_frame_num_offset + 1 >> 1;
  355. } else {
  356. mx = mx * (h->frame_num_offset - h->prev_frame_num_offset) /
  357. h->prev_frame_num_offset + 1 >> 1;
  358. my = my * (h->frame_num_offset - h->prev_frame_num_offset) /
  359. h->prev_frame_num_offset + 1 >> 1;
  360. }
  361. }
  362. /* clip motion vector prediction to frame border */
  363. mx = av_clip(mx, extra_width - 6 * x, h_edge_pos - 6 * x);
  364. my = av_clip(my, extra_width - 6 * y, v_edge_pos - 6 * y);
  365. /* get (optional) motion vector differential */
  366. if (mode == PREDICT_MODE) {
  367. dx = dy = 0;
  368. } else {
  369. dy = svq3_get_se_golomb(&h->gb);
  370. dx = svq3_get_se_golomb(&h->gb);
  371. if (dx == INVALID_VLC || dy == INVALID_VLC) {
  372. av_log(h->avctx, AV_LOG_ERROR, "invalid MV vlc\n");
  373. return -1;
  374. }
  375. }
  376. /* compute motion vector */
  377. if (mode == THIRDPEL_MODE) {
  378. int fx, fy;
  379. mx = (mx + 1 >> 1) + dx;
  380. my = (my + 1 >> 1) + dy;
  381. fx = (unsigned)(mx + 0x3000) / 3 - 0x1000;
  382. fy = (unsigned)(my + 0x3000) / 3 - 0x1000;
  383. dxy = (mx - 3 * fx) + 4 * (my - 3 * fy);
  384. svq3_mc_dir_part(s, x, y, part_width, part_height,
  385. fx, fy, dxy, 1, dir, avg);
  386. mx += mx;
  387. my += my;
  388. } else if (mode == HALFPEL_MODE || mode == PREDICT_MODE) {
  389. mx = (unsigned)(mx + 1 + 0x3000) / 3 + dx - 0x1000;
  390. my = (unsigned)(my + 1 + 0x3000) / 3 + dy - 0x1000;
  391. dxy = (mx & 1) + 2 * (my & 1);
  392. svq3_mc_dir_part(s, x, y, part_width, part_height,
  393. mx >> 1, my >> 1, dxy, 0, dir, avg);
  394. mx *= 3;
  395. my *= 3;
  396. } else {
  397. mx = (unsigned)(mx + 3 + 0x6000) / 6 + dx - 0x1000;
  398. my = (unsigned)(my + 3 + 0x6000) / 6 + dy - 0x1000;
  399. svq3_mc_dir_part(s, x, y, part_width, part_height,
  400. mx, my, 0, 0, dir, avg);
  401. mx *= 6;
  402. my *= 6;
  403. }
  404. /* update mv_cache */
  405. if (mode != PREDICT_MODE) {
  406. int32_t mv = pack16to32(mx, my);
  407. if (part_height == 8 && i < 8) {
  408. AV_WN32A(sl->mv_cache[dir][scan8[k] + 1 * 8], mv);
  409. if (part_width == 8 && j < 8)
  410. AV_WN32A(sl->mv_cache[dir][scan8[k] + 1 + 1 * 8], mv);
  411. }
  412. if (part_width == 8 && j < 8)
  413. AV_WN32A(sl->mv_cache[dir][scan8[k] + 1], mv);
  414. if (part_width == 4 || part_height == 4)
  415. AV_WN32A(sl->mv_cache[dir][scan8[k]], mv);
  416. }
  417. /* write back motion vectors */
  418. fill_rectangle(h->cur_pic.motion_val[dir][b_xy],
  419. part_width >> 2, part_height >> 2, h->b_stride,
  420. pack16to32(mx, my), 4);
  421. }
  422. return 0;
  423. }
  424. static int svq3_decode_mb(SVQ3Context *s, unsigned int mb_type)
  425. {
  426. H264Context *h = &s->h;
  427. H264SliceContext *sl = &h->slice_ctx[0];
  428. int i, j, k, m, dir, mode;
  429. int cbp = 0;
  430. uint32_t vlc;
  431. int8_t *top, *left;
  432. const int mb_xy = sl->mb_xy;
  433. const int b_xy = 4 * sl->mb_x + 4 * sl->mb_y * h->b_stride;
  434. sl->top_samples_available = (sl->mb_y == 0) ? 0x33FF : 0xFFFF;
  435. sl->left_samples_available = (sl->mb_x == 0) ? 0x5F5F : 0xFFFF;
  436. sl->topright_samples_available = 0xFFFF;
  437. if (mb_type == 0) { /* SKIP */
  438. if (h->pict_type == AV_PICTURE_TYPE_P ||
  439. s->next_pic->mb_type[mb_xy] == -1) {
  440. svq3_mc_dir_part(s, 16 * sl->mb_x, 16 * sl->mb_y, 16, 16,
  441. 0, 0, 0, 0, 0, 0);
  442. if (h->pict_type == AV_PICTURE_TYPE_B)
  443. svq3_mc_dir_part(s, 16 * sl->mb_x, 16 * sl->mb_y, 16, 16,
  444. 0, 0, 0, 0, 1, 1);
  445. mb_type = MB_TYPE_SKIP;
  446. } else {
  447. mb_type = FFMIN(s->next_pic->mb_type[mb_xy], 6);
  448. if (svq3_mc_dir(s, mb_type, PREDICT_MODE, 0, 0) < 0)
  449. return -1;
  450. if (svq3_mc_dir(s, mb_type, PREDICT_MODE, 1, 1) < 0)
  451. return -1;
  452. mb_type = MB_TYPE_16x16;
  453. }
  454. } else if (mb_type < 8) { /* INTER */
  455. if (s->thirdpel_flag && s->halfpel_flag == !get_bits1(&h->gb))
  456. mode = THIRDPEL_MODE;
  457. else if (s->halfpel_flag &&
  458. s->thirdpel_flag == !get_bits1(&h->gb))
  459. mode = HALFPEL_MODE;
  460. else
  461. mode = FULLPEL_MODE;
  462. /* fill caches */
  463. /* note ref_cache should contain here:
  464. * ????????
  465. * ???11111
  466. * N??11111
  467. * N??11111
  468. * N??11111
  469. */
  470. for (m = 0; m < 2; m++) {
  471. if (sl->mb_x > 0 && sl->intra4x4_pred_mode[h->mb2br_xy[mb_xy - 1] + 6] != -1) {
  472. for (i = 0; i < 4; i++)
  473. AV_COPY32(sl->mv_cache[m][scan8[0] - 1 + i * 8],
  474. h->cur_pic.motion_val[m][b_xy - 1 + i * h->b_stride]);
  475. } else {
  476. for (i = 0; i < 4; i++)
  477. AV_ZERO32(sl->mv_cache[m][scan8[0] - 1 + i * 8]);
  478. }
  479. if (sl->mb_y > 0) {
  480. memcpy(sl->mv_cache[m][scan8[0] - 1 * 8],
  481. h->cur_pic.motion_val[m][b_xy - h->b_stride],
  482. 4 * 2 * sizeof(int16_t));
  483. memset(&sl->ref_cache[m][scan8[0] - 1 * 8],
  484. (sl->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1, 4);
  485. if (sl->mb_x < h->mb_width - 1) {
  486. AV_COPY32(sl->mv_cache[m][scan8[0] + 4 - 1 * 8],
  487. h->cur_pic.motion_val[m][b_xy - h->b_stride + 4]);
  488. sl->ref_cache[m][scan8[0] + 4 - 1 * 8] =
  489. (sl->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride + 1] + 6] == -1 ||
  490. sl->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1;
  491. } else
  492. sl->ref_cache[m][scan8[0] + 4 - 1 * 8] = PART_NOT_AVAILABLE;
  493. if (sl->mb_x > 0) {
  494. AV_COPY32(sl->mv_cache[m][scan8[0] - 1 - 1 * 8],
  495. h->cur_pic.motion_val[m][b_xy - h->b_stride - 1]);
  496. sl->ref_cache[m][scan8[0] - 1 - 1 * 8] =
  497. (sl->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride - 1] + 3] == -1) ? PART_NOT_AVAILABLE : 1;
  498. } else
  499. sl->ref_cache[m][scan8[0] - 1 - 1 * 8] = PART_NOT_AVAILABLE;
  500. } else
  501. memset(&sl->ref_cache[m][scan8[0] - 1 * 8 - 1],
  502. PART_NOT_AVAILABLE, 8);
  503. if (h->pict_type != AV_PICTURE_TYPE_B)
  504. break;
  505. }
  506. /* decode motion vector(s) and form prediction(s) */
  507. if (h->pict_type == AV_PICTURE_TYPE_P) {
  508. if (svq3_mc_dir(s, mb_type - 1, mode, 0, 0) < 0)
  509. return -1;
  510. } else { /* AV_PICTURE_TYPE_B */
  511. if (mb_type != 2) {
  512. if (svq3_mc_dir(s, 0, mode, 0, 0) < 0)
  513. return -1;
  514. } else {
  515. for (i = 0; i < 4; i++)
  516. memset(h->cur_pic.motion_val[0][b_xy + i * h->b_stride],
  517. 0, 4 * 2 * sizeof(int16_t));
  518. }
  519. if (mb_type != 1) {
  520. if (svq3_mc_dir(s, 0, mode, 1, mb_type == 3) < 0)
  521. return -1;
  522. } else {
  523. for (i = 0; i < 4; i++)
  524. memset(h->cur_pic.motion_val[1][b_xy + i * h->b_stride],
  525. 0, 4 * 2 * sizeof(int16_t));
  526. }
  527. }
  528. mb_type = MB_TYPE_16x16;
  529. } else if (mb_type == 8 || mb_type == 33) { /* INTRA4x4 */
  530. memset(sl->intra4x4_pred_mode_cache, -1, 8 * 5 * sizeof(int8_t));
  531. if (mb_type == 8) {
  532. if (sl->mb_x > 0) {
  533. for (i = 0; i < 4; i++)
  534. sl->intra4x4_pred_mode_cache[scan8[0] - 1 + i * 8] = sl->intra4x4_pred_mode[h->mb2br_xy[mb_xy - 1] + 6 - i];
  535. if (sl->intra4x4_pred_mode_cache[scan8[0] - 1] == -1)
  536. sl->left_samples_available = 0x5F5F;
  537. }
  538. if (sl->mb_y > 0) {
  539. sl->intra4x4_pred_mode_cache[4 + 8 * 0] = sl->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride] + 0];
  540. sl->intra4x4_pred_mode_cache[5 + 8 * 0] = sl->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride] + 1];
  541. sl->intra4x4_pred_mode_cache[6 + 8 * 0] = sl->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride] + 2];
  542. sl->intra4x4_pred_mode_cache[7 + 8 * 0] = sl->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride] + 3];
  543. if (sl->intra4x4_pred_mode_cache[4 + 8 * 0] == -1)
  544. sl->top_samples_available = 0x33FF;
  545. }
  546. /* decode prediction codes for luma blocks */
  547. for (i = 0; i < 16; i += 2) {
  548. vlc = svq3_get_ue_golomb(&h->gb);
  549. if (vlc >= 25) {
  550. av_log(h->avctx, AV_LOG_ERROR,
  551. "luma prediction:%"PRIu32"\n", vlc);
  552. return -1;
  553. }
  554. left = &sl->intra4x4_pred_mode_cache[scan8[i] - 1];
  555. top = &sl->intra4x4_pred_mode_cache[scan8[i] - 8];
  556. left[1] = svq3_pred_1[top[0] + 1][left[0] + 1][svq3_pred_0[vlc][0]];
  557. left[2] = svq3_pred_1[top[1] + 1][left[1] + 1][svq3_pred_0[vlc][1]];
  558. if (left[1] == -1 || left[2] == -1) {
  559. av_log(h->avctx, AV_LOG_ERROR, "weird prediction\n");
  560. return -1;
  561. }
  562. }
  563. } else { /* mb_type == 33, DC_128_PRED block type */
  564. for (i = 0; i < 4; i++)
  565. memset(&sl->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_PRED, 4);
  566. }
  567. write_back_intra_pred_mode(h, sl);
  568. if (mb_type == 8) {
  569. ff_h264_check_intra4x4_pred_mode(h, sl);
  570. sl->top_samples_available = (sl->mb_y == 0) ? 0x33FF : 0xFFFF;
  571. sl->left_samples_available = (sl->mb_x == 0) ? 0x5F5F : 0xFFFF;
  572. } else {
  573. for (i = 0; i < 4; i++)
  574. memset(&sl->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_128_PRED, 4);
  575. sl->top_samples_available = 0x33FF;
  576. sl->left_samples_available = 0x5F5F;
  577. }
  578. mb_type = MB_TYPE_INTRA4x4;
  579. } else { /* INTRA16x16 */
  580. dir = i_mb_type_info[mb_type - 8].pred_mode;
  581. dir = (dir >> 1) ^ 3 * (dir & 1) ^ 1;
  582. if ((sl->intra16x16_pred_mode = ff_h264_check_intra_pred_mode(h, sl, dir, 0)) < 0) {
  583. av_log(h->avctx, AV_LOG_ERROR, "ff_h264_check_intra_pred_mode < 0\n");
  584. return sl->intra16x16_pred_mode;
  585. }
  586. cbp = i_mb_type_info[mb_type - 8].cbp;
  587. mb_type = MB_TYPE_INTRA16x16;
  588. }
  589. if (!IS_INTER(mb_type) && h->pict_type != AV_PICTURE_TYPE_I) {
  590. for (i = 0; i < 4; i++)
  591. memset(h->cur_pic.motion_val[0][b_xy + i * h->b_stride],
  592. 0, 4 * 2 * sizeof(int16_t));
  593. if (h->pict_type == AV_PICTURE_TYPE_B) {
  594. for (i = 0; i < 4; i++)
  595. memset(h->cur_pic.motion_val[1][b_xy + i * h->b_stride],
  596. 0, 4 * 2 * sizeof(int16_t));
  597. }
  598. }
  599. if (!IS_INTRA4x4(mb_type)) {
  600. memset(sl->intra4x4_pred_mode + h->mb2br_xy[mb_xy], DC_PRED, 8);
  601. }
  602. if (!IS_SKIP(mb_type) || h->pict_type == AV_PICTURE_TYPE_B) {
  603. memset(sl->non_zero_count_cache + 8, 0, 14 * 8 * sizeof(uint8_t));
  604. }
  605. if (!IS_INTRA16x16(mb_type) &&
  606. (!IS_SKIP(mb_type) || h->pict_type == AV_PICTURE_TYPE_B)) {
  607. if ((vlc = svq3_get_ue_golomb(&h->gb)) >= 48) {
  608. av_log(h->avctx, AV_LOG_ERROR, "cbp_vlc=%"PRIu32"\n", vlc);
  609. return -1;
  610. }
  611. cbp = IS_INTRA(mb_type) ? golomb_to_intra4x4_cbp[vlc]
  612. : golomb_to_inter_cbp[vlc];
  613. }
  614. if (IS_INTRA16x16(mb_type) ||
  615. (h->pict_type != AV_PICTURE_TYPE_I && s->adaptive_quant && cbp)) {
  616. sl->qscale += svq3_get_se_golomb(&h->gb);
  617. if (sl->qscale > 31u) {
  618. av_log(h->avctx, AV_LOG_ERROR, "qscale:%d\n", sl->qscale);
  619. return -1;
  620. }
  621. }
  622. if (IS_INTRA16x16(mb_type)) {
  623. AV_ZERO128(sl->mb_luma_dc[0] + 0);
  624. AV_ZERO128(sl->mb_luma_dc[0] + 8);
  625. if (svq3_decode_block(&h->gb, sl->mb_luma_dc[0], 0, 1)) {
  626. av_log(h->avctx, AV_LOG_ERROR,
  627. "error while decoding intra luma dc\n");
  628. return -1;
  629. }
  630. }
  631. if (cbp) {
  632. const int index = IS_INTRA16x16(mb_type) ? 1 : 0;
  633. const int type = ((sl->qscale < 24 && IS_INTRA4x4(mb_type)) ? 2 : 1);
  634. for (i = 0; i < 4; i++)
  635. if ((cbp & (1 << i))) {
  636. for (j = 0; j < 4; j++) {
  637. k = index ? (1 * (j & 1) + 2 * (i & 1) +
  638. 2 * (j & 2) + 4 * (i & 2))
  639. : (4 * i + j);
  640. sl->non_zero_count_cache[scan8[k]] = 1;
  641. if (svq3_decode_block(&h->gb, &sl->mb[16 * k], index, type)) {
  642. av_log(h->avctx, AV_LOG_ERROR,
  643. "error while decoding block\n");
  644. return -1;
  645. }
  646. }
  647. }
  648. if ((cbp & 0x30)) {
  649. for (i = 1; i < 3; ++i)
  650. if (svq3_decode_block(&h->gb, &sl->mb[16 * 16 * i], 0, 3)) {
  651. av_log(h->avctx, AV_LOG_ERROR,
  652. "error while decoding chroma dc block\n");
  653. return -1;
  654. }
  655. if ((cbp & 0x20)) {
  656. for (i = 1; i < 3; i++) {
  657. for (j = 0; j < 4; j++) {
  658. k = 16 * i + j;
  659. sl->non_zero_count_cache[scan8[k]] = 1;
  660. if (svq3_decode_block(&h->gb, &sl->mb[16 * k], 1, 1)) {
  661. av_log(h->avctx, AV_LOG_ERROR,
  662. "error while decoding chroma ac block\n");
  663. return -1;
  664. }
  665. }
  666. }
  667. }
  668. }
  669. }
  670. sl->cbp = cbp;
  671. h->cur_pic.mb_type[mb_xy] = mb_type;
  672. if (IS_INTRA(mb_type))
  673. sl->chroma_pred_mode = ff_h264_check_intra_pred_mode(h, sl, DC_PRED8x8, 1);
  674. return 0;
  675. }
  676. static int svq3_decode_slice_header(AVCodecContext *avctx)
  677. {
  678. SVQ3Context *s = avctx->priv_data;
  679. H264Context *h = &s->h;
  680. H264SliceContext *sl = &h->slice_ctx[0];
  681. const int mb_xy = sl->mb_xy;
  682. int i, header;
  683. unsigned slice_id;
  684. header = get_bits(&s->gb, 8);
  685. if (((header & 0x9F) != 1 && (header & 0x9F) != 2) || (header & 0x60) == 0) {
  686. /* TODO: what? */
  687. av_log(avctx, AV_LOG_ERROR, "unsupported slice header (%02X)\n", header);
  688. return -1;
  689. } else {
  690. int slice_bits, slice_bytes, slice_length;
  691. int length = header >> 5 & 3;
  692. slice_length = show_bits(&s->gb, 8 * length);
  693. slice_bits = slice_length * 8;
  694. slice_bytes = slice_length + length - 1;
  695. if (slice_bytes > get_bits_left(&s->gb)) {
  696. av_log(avctx, AV_LOG_ERROR, "slice after bitstream end\n");
  697. return -1;
  698. }
  699. skip_bits(&s->gb, 8);
  700. av_fast_malloc(&s->slice_buf, &s->slice_size, slice_bytes + AV_INPUT_BUFFER_PADDING_SIZE);
  701. if (!s->slice_buf)
  702. return AVERROR(ENOMEM);
  703. memcpy(s->slice_buf, s->gb.buffer + s->gb.index / 8, slice_bytes);
  704. init_get_bits(&h->gb, s->slice_buf, slice_bits);
  705. if (s->watermark_key) {
  706. uint32_t header = AV_RL32(&h->gb.buffer[1]);
  707. AV_WL32(&h->gb.buffer[1], header ^ s->watermark_key);
  708. }
  709. if (length > 0) {
  710. memcpy(s->slice_buf, &s->slice_buf[slice_length], length - 1);
  711. }
  712. skip_bits_long(&s->gb, slice_bytes * 8);
  713. }
  714. if ((slice_id = svq3_get_ue_golomb(&h->gb)) >= 3) {
  715. av_log(h->avctx, AV_LOG_ERROR, "illegal slice type %u \n", slice_id);
  716. return -1;
  717. }
  718. sl->slice_type = golomb_to_pict_type[slice_id];
  719. if ((header & 0x9F) == 2) {
  720. i = (h->mb_num < 64) ? 6 : (1 + av_log2(h->mb_num - 1));
  721. sl->mb_skip_run = get_bits(&h->gb, i) -
  722. (sl->mb_y * h->mb_width + sl->mb_x);
  723. } else {
  724. skip_bits1(&h->gb);
  725. sl->mb_skip_run = 0;
  726. }
  727. sl->slice_num = get_bits(&h->gb, 8);
  728. sl->qscale = get_bits(&h->gb, 5);
  729. s->adaptive_quant = get_bits1(&h->gb);
  730. /* unknown fields */
  731. skip_bits1(&h->gb);
  732. if (s->unknown_flag)
  733. skip_bits1(&h->gb);
  734. skip_bits1(&h->gb);
  735. skip_bits(&h->gb, 2);
  736. while (get_bits1(&h->gb))
  737. skip_bits(&h->gb, 8);
  738. /* reset intra predictors and invalidate motion vector references */
  739. if (sl->mb_x > 0) {
  740. memset(sl->intra4x4_pred_mode + h->mb2br_xy[mb_xy - 1] + 3,
  741. -1, 4 * sizeof(int8_t));
  742. memset(sl->intra4x4_pred_mode + h->mb2br_xy[mb_xy - sl->mb_x],
  743. -1, 8 * sizeof(int8_t) * sl->mb_x);
  744. }
  745. if (sl->mb_y > 0) {
  746. memset(sl->intra4x4_pred_mode + h->mb2br_xy[mb_xy - h->mb_stride],
  747. -1, 8 * sizeof(int8_t) * (h->mb_width - sl->mb_x));
  748. if (sl->mb_x > 0)
  749. sl->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride - 1] + 3] = -1;
  750. }
  751. return 0;
  752. }
  753. static av_cold int svq3_decode_init(AVCodecContext *avctx)
  754. {
  755. SVQ3Context *s = avctx->priv_data;
  756. H264Context *h = &s->h;
  757. H264SliceContext *sl;
  758. int m;
  759. unsigned char *extradata;
  760. unsigned char *extradata_end;
  761. unsigned int size;
  762. int marker_found = 0;
  763. s->cur_pic = av_mallocz(sizeof(*s->cur_pic));
  764. s->last_pic = av_mallocz(sizeof(*s->last_pic));
  765. s->next_pic = av_mallocz(sizeof(*s->next_pic));
  766. if (!s->next_pic || !s->last_pic || !s->cur_pic) {
  767. av_freep(&s->cur_pic);
  768. av_freep(&s->last_pic);
  769. av_freep(&s->next_pic);
  770. return AVERROR(ENOMEM);
  771. }
  772. s->cur_pic->f = av_frame_alloc();
  773. s->last_pic->f = av_frame_alloc();
  774. s->next_pic->f = av_frame_alloc();
  775. if (!s->cur_pic->f || !s->last_pic->f || !s->next_pic->f)
  776. return AVERROR(ENOMEM);
  777. if (ff_h264_decode_init(avctx) < 0)
  778. return -1;
  779. // we will overwrite it later during decoding
  780. av_frame_free(&h->cur_pic.f);
  781. ff_h264dsp_init(&h->h264dsp, 8, 1);
  782. ff_h264chroma_init(&h->h264chroma, 8);
  783. ff_h264qpel_init(&h->h264qpel, 8);
  784. ff_h264_pred_init(&h->hpc, AV_CODEC_ID_SVQ3, 8, 1);
  785. ff_videodsp_init(&h->vdsp, 8);
  786. memset(h->pps.scaling_matrix4, 16, 6 * 16 * sizeof(uint8_t));
  787. memset(h->pps.scaling_matrix8, 16, 2 * 64 * sizeof(uint8_t));
  788. h->sps.bit_depth_luma = 8;
  789. h->chroma_format_idc = 1;
  790. ff_hpeldsp_init(&s->hdsp, avctx->flags);
  791. ff_tpeldsp_init(&s->tdsp);
  792. sl = h->slice_ctx;
  793. h->flags = avctx->flags;
  794. sl->is_complex = 1;
  795. h->picture_structure = PICT_FRAME;
  796. avctx->pix_fmt = AV_PIX_FMT_YUVJ420P;
  797. avctx->color_range = AVCOL_RANGE_JPEG;
  798. h->slice_ctx[0].chroma_qp[0] = h->slice_ctx[0].chroma_qp[1] = 4;
  799. h->chroma_x_shift = h->chroma_y_shift = 1;
  800. s->halfpel_flag = 1;
  801. s->thirdpel_flag = 1;
  802. s->unknown_flag = 0;
  803. /* prowl for the "SEQH" marker in the extradata */
  804. extradata = (unsigned char *)avctx->extradata;
  805. extradata_end = avctx->extradata + avctx->extradata_size;
  806. if (extradata) {
  807. for (m = 0; m + 8 < avctx->extradata_size; m++) {
  808. if (!memcmp(extradata, "SEQH", 4)) {
  809. marker_found = 1;
  810. break;
  811. }
  812. extradata++;
  813. }
  814. }
  815. /* if a match was found, parse the extra data */
  816. if (marker_found) {
  817. GetBitContext gb;
  818. int frame_size_code;
  819. size = AV_RB32(&extradata[4]);
  820. if (size > extradata_end - extradata - 8)
  821. return AVERROR_INVALIDDATA;
  822. init_get_bits(&gb, extradata + 8, size * 8);
  823. /* 'frame size code' and optional 'width, height' */
  824. frame_size_code = get_bits(&gb, 3);
  825. switch (frame_size_code) {
  826. case 0:
  827. avctx->width = 160;
  828. avctx->height = 120;
  829. break;
  830. case 1:
  831. avctx->width = 128;
  832. avctx->height = 96;
  833. break;
  834. case 2:
  835. avctx->width = 176;
  836. avctx->height = 144;
  837. break;
  838. case 3:
  839. avctx->width = 352;
  840. avctx->height = 288;
  841. break;
  842. case 4:
  843. avctx->width = 704;
  844. avctx->height = 576;
  845. break;
  846. case 5:
  847. avctx->width = 240;
  848. avctx->height = 180;
  849. break;
  850. case 6:
  851. avctx->width = 320;
  852. avctx->height = 240;
  853. break;
  854. case 7:
  855. avctx->width = get_bits(&gb, 12);
  856. avctx->height = get_bits(&gb, 12);
  857. break;
  858. }
  859. s->halfpel_flag = get_bits1(&gb);
  860. s->thirdpel_flag = get_bits1(&gb);
  861. /* unknown fields */
  862. skip_bits1(&gb);
  863. skip_bits1(&gb);
  864. skip_bits1(&gb);
  865. skip_bits1(&gb);
  866. h->low_delay = get_bits1(&gb);
  867. /* unknown field */
  868. skip_bits1(&gb);
  869. while (get_bits1(&gb))
  870. skip_bits(&gb, 8);
  871. s->unknown_flag = get_bits1(&gb);
  872. avctx->has_b_frames = !h->low_delay;
  873. if (s->unknown_flag) {
  874. #if CONFIG_ZLIB
  875. unsigned watermark_width = svq3_get_ue_golomb(&gb);
  876. unsigned watermark_height = svq3_get_ue_golomb(&gb);
  877. int u1 = svq3_get_ue_golomb(&gb);
  878. int u2 = get_bits(&gb, 8);
  879. int u3 = get_bits(&gb, 2);
  880. int u4 = svq3_get_ue_golomb(&gb);
  881. unsigned long buf_len = watermark_width *
  882. watermark_height * 4;
  883. int offset = get_bits_count(&gb) + 7 >> 3;
  884. uint8_t *buf;
  885. if (watermark_height > 0 &&
  886. (uint64_t)watermark_width * 4 > UINT_MAX / watermark_height)
  887. return -1;
  888. buf = av_malloc(buf_len);
  889. av_log(avctx, AV_LOG_DEBUG, "watermark size: %ux%u\n",
  890. watermark_width, watermark_height);
  891. av_log(avctx, AV_LOG_DEBUG,
  892. "u1: %x u2: %x u3: %x compressed data size: %d offset: %d\n",
  893. u1, u2, u3, u4, offset);
  894. if (uncompress(buf, &buf_len, extradata + 8 + offset,
  895. size - offset) != Z_OK) {
  896. av_log(avctx, AV_LOG_ERROR,
  897. "could not uncompress watermark logo\n");
  898. av_free(buf);
  899. return -1;
  900. }
  901. s->watermark_key = ff_svq1_packet_checksum(buf, buf_len, 0);
  902. s->watermark_key = s->watermark_key << 16 | s->watermark_key;
  903. av_log(avctx, AV_LOG_DEBUG,
  904. "watermark key %#"PRIx32"\n", s->watermark_key);
  905. av_free(buf);
  906. #else
  907. av_log(avctx, AV_LOG_ERROR,
  908. "this svq3 file contains watermark which need zlib support compiled in\n");
  909. return -1;
  910. #endif
  911. }
  912. }
  913. h->width = avctx->width;
  914. h->height = avctx->height;
  915. h->mb_width = (h->width + 15) / 16;
  916. h->mb_height = (h->height + 15) / 16;
  917. h->mb_stride = h->mb_width + 1;
  918. h->mb_num = h->mb_width * h->mb_height;
  919. h->b_stride = 4 * h->mb_width;
  920. s->h_edge_pos = h->mb_width * 16;
  921. s->v_edge_pos = h->mb_height * 16;
  922. if (ff_h264_alloc_tables(h) < 0) {
  923. av_log(avctx, AV_LOG_ERROR, "svq3 memory allocation failed\n");
  924. return AVERROR(ENOMEM);
  925. }
  926. return 0;
  927. }
  928. static void free_picture(AVCodecContext *avctx, H264Picture *pic)
  929. {
  930. int i;
  931. for (i = 0; i < 2; i++) {
  932. av_buffer_unref(&pic->motion_val_buf[i]);
  933. av_buffer_unref(&pic->ref_index_buf[i]);
  934. }
  935. av_buffer_unref(&pic->mb_type_buf);
  936. av_frame_unref(pic->f);
  937. }
  938. static int get_buffer(AVCodecContext *avctx, H264Picture *pic)
  939. {
  940. SVQ3Context *s = avctx->priv_data;
  941. H264Context *h = &s->h;
  942. H264SliceContext *sl = &h->slice_ctx[0];
  943. const int big_mb_num = h->mb_stride * (h->mb_height + 1) + 1;
  944. const int mb_array_size = h->mb_stride * h->mb_height;
  945. const int b4_stride = h->mb_width * 4 + 1;
  946. const int b4_array_size = b4_stride * h->mb_height * 4;
  947. int ret;
  948. if (!pic->motion_val_buf[0]) {
  949. int i;
  950. pic->mb_type_buf = av_buffer_allocz((big_mb_num + h->mb_stride) * sizeof(uint32_t));
  951. if (!pic->mb_type_buf)
  952. return AVERROR(ENOMEM);
  953. pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * h->mb_stride + 1;
  954. for (i = 0; i < 2; i++) {
  955. pic->motion_val_buf[i] = av_buffer_allocz(2 * (b4_array_size + 4) * sizeof(int16_t));
  956. pic->ref_index_buf[i] = av_buffer_allocz(4 * mb_array_size);
  957. if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i]) {
  958. ret = AVERROR(ENOMEM);
  959. goto fail;
  960. }
  961. pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
  962. pic->ref_index[i] = pic->ref_index_buf[i]->data;
  963. }
  964. }
  965. pic->reference = !(h->pict_type == AV_PICTURE_TYPE_B);
  966. ret = ff_get_buffer(avctx, pic->f,
  967. pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
  968. if (ret < 0)
  969. goto fail;
  970. if (!sl->edge_emu_buffer) {
  971. sl->edge_emu_buffer = av_mallocz(pic->f->linesize[0] * 17);
  972. if (!sl->edge_emu_buffer)
  973. return AVERROR(ENOMEM);
  974. }
  975. sl->linesize = pic->f->linesize[0];
  976. sl->uvlinesize = pic->f->linesize[1];
  977. return 0;
  978. fail:
  979. free_picture(avctx, pic);
  980. return ret;
  981. }
  982. static int svq3_decode_frame(AVCodecContext *avctx, void *data,
  983. int *got_frame, AVPacket *avpkt)
  984. {
  985. const uint8_t *buf = avpkt->data;
  986. SVQ3Context *s = avctx->priv_data;
  987. H264Context *h = &s->h;
  988. H264SliceContext *sl = &h->slice_ctx[0];
  989. int buf_size = avpkt->size;
  990. int ret, m, i;
  991. /* special case for last picture */
  992. if (buf_size == 0) {
  993. if (s->next_pic->f->data[0] && !h->low_delay && !s->last_frame_output) {
  994. ret = av_frame_ref(data, s->next_pic->f);
  995. if (ret < 0)
  996. return ret;
  997. s->last_frame_output = 1;
  998. *got_frame = 1;
  999. }
  1000. return 0;
  1001. }
  1002. ret = init_get_bits(&s->gb, buf, 8 * buf_size);
  1003. if (ret < 0)
  1004. return ret;
  1005. sl->mb_x = sl->mb_y = sl->mb_xy = 0;
  1006. if (svq3_decode_slice_header(avctx))
  1007. return -1;
  1008. h->pict_type = sl->slice_type;
  1009. if (h->pict_type != AV_PICTURE_TYPE_B)
  1010. FFSWAP(H264Picture*, s->next_pic, s->last_pic);
  1011. av_frame_unref(s->cur_pic->f);
  1012. /* for skipping the frame */
  1013. s->cur_pic->f->pict_type = h->pict_type;
  1014. s->cur_pic->f->key_frame = (h->pict_type == AV_PICTURE_TYPE_I);
  1015. ret = get_buffer(avctx, s->cur_pic);
  1016. if (ret < 0)
  1017. return ret;
  1018. h->cur_pic_ptr = s->cur_pic;
  1019. h->cur_pic = *s->cur_pic;
  1020. for (i = 0; i < 16; i++) {
  1021. h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * sl->linesize * ((scan8[i] - scan8[0]) >> 3);
  1022. h->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * sl->linesize * ((scan8[i] - scan8[0]) >> 3);
  1023. }
  1024. for (i = 0; i < 16; i++) {
  1025. h->block_offset[16 + i] =
  1026. h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * sl->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
  1027. h->block_offset[48 + 16 + i] =
  1028. h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * sl->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
  1029. }
  1030. if (h->pict_type != AV_PICTURE_TYPE_I) {
  1031. if (!s->last_pic->f->data[0]) {
  1032. av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
  1033. ret = get_buffer(avctx, s->last_pic);
  1034. if (ret < 0)
  1035. return ret;
  1036. memset(s->last_pic->f->data[0], 0, avctx->height * s->last_pic->f->linesize[0]);
  1037. memset(s->last_pic->f->data[1], 0x80, (avctx->height / 2) *
  1038. s->last_pic->f->linesize[1]);
  1039. memset(s->last_pic->f->data[2], 0x80, (avctx->height / 2) *
  1040. s->last_pic->f->linesize[2]);
  1041. }
  1042. if (h->pict_type == AV_PICTURE_TYPE_B && !s->next_pic->f->data[0]) {
  1043. av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
  1044. ret = get_buffer(avctx, s->next_pic);
  1045. if (ret < 0)
  1046. return ret;
  1047. memset(s->next_pic->f->data[0], 0, avctx->height * s->next_pic->f->linesize[0]);
  1048. memset(s->next_pic->f->data[1], 0x80, (avctx->height / 2) *
  1049. s->next_pic->f->linesize[1]);
  1050. memset(s->next_pic->f->data[2], 0x80, (avctx->height / 2) *
  1051. s->next_pic->f->linesize[2]);
  1052. }
  1053. }
  1054. if (avctx->debug & FF_DEBUG_PICT_INFO)
  1055. av_log(h->avctx, AV_LOG_DEBUG,
  1056. "%c hpel:%d, tpel:%d aqp:%d qp:%d, slice_num:%02X\n",
  1057. av_get_picture_type_char(h->pict_type),
  1058. s->halfpel_flag, s->thirdpel_flag,
  1059. s->adaptive_quant, h->slice_ctx[0].qscale, sl->slice_num);
  1060. if (avctx->skip_frame >= AVDISCARD_NONREF && h->pict_type == AV_PICTURE_TYPE_B ||
  1061. avctx->skip_frame >= AVDISCARD_NONKEY && h->pict_type != AV_PICTURE_TYPE_I ||
  1062. avctx->skip_frame >= AVDISCARD_ALL)
  1063. return 0;
  1064. if (s->next_p_frame_damaged) {
  1065. if (h->pict_type == AV_PICTURE_TYPE_B)
  1066. return 0;
  1067. else
  1068. s->next_p_frame_damaged = 0;
  1069. }
  1070. if (h->pict_type == AV_PICTURE_TYPE_B) {
  1071. h->frame_num_offset = sl->slice_num - h->prev_frame_num;
  1072. if (h->frame_num_offset < 0)
  1073. h->frame_num_offset += 256;
  1074. if (h->frame_num_offset == 0 ||
  1075. h->frame_num_offset >= h->prev_frame_num_offset) {
  1076. av_log(h->avctx, AV_LOG_ERROR, "error in B-frame picture id\n");
  1077. return -1;
  1078. }
  1079. } else {
  1080. h->prev_frame_num = h->frame_num;
  1081. h->frame_num = sl->slice_num;
  1082. h->prev_frame_num_offset = h->frame_num - h->prev_frame_num;
  1083. if (h->prev_frame_num_offset < 0)
  1084. h->prev_frame_num_offset += 256;
  1085. }
  1086. for (m = 0; m < 2; m++) {
  1087. int i;
  1088. for (i = 0; i < 4; i++) {
  1089. int j;
  1090. for (j = -1; j < 4; j++)
  1091. sl->ref_cache[m][scan8[0] + 8 * i + j] = 1;
  1092. if (i < 3)
  1093. sl->ref_cache[m][scan8[0] + 8 * i + j] = PART_NOT_AVAILABLE;
  1094. }
  1095. }
  1096. for (sl->mb_y = 0; sl->mb_y < h->mb_height; sl->mb_y++) {
  1097. for (sl->mb_x = 0; sl->mb_x < h->mb_width; sl->mb_x++) {
  1098. unsigned mb_type;
  1099. sl->mb_xy = sl->mb_x + sl->mb_y * h->mb_stride;
  1100. if ((get_bits_left(&h->gb)) <= 7) {
  1101. if (((get_bits_count(&h->gb) & 7) == 0 ||
  1102. show_bits(&h->gb, get_bits_left(&h->gb) & 7) == 0)) {
  1103. if (svq3_decode_slice_header(avctx))
  1104. return -1;
  1105. }
  1106. /* TODO: support s->mb_skip_run */
  1107. }
  1108. mb_type = svq3_get_ue_golomb(&h->gb);
  1109. if (h->pict_type == AV_PICTURE_TYPE_I)
  1110. mb_type += 8;
  1111. else if (h->pict_type == AV_PICTURE_TYPE_B && mb_type >= 4)
  1112. mb_type += 4;
  1113. if (mb_type > 33 || svq3_decode_mb(s, mb_type)) {
  1114. av_log(h->avctx, AV_LOG_ERROR,
  1115. "error while decoding MB %d %d\n", sl->mb_x, sl->mb_y);
  1116. return -1;
  1117. }
  1118. if (mb_type != 0)
  1119. ff_h264_hl_decode_mb(h, &h->slice_ctx[0]);
  1120. if (h->pict_type != AV_PICTURE_TYPE_B && !h->low_delay)
  1121. h->cur_pic.mb_type[sl->mb_x + sl->mb_y * h->mb_stride] =
  1122. (h->pict_type == AV_PICTURE_TYPE_P && mb_type < 8) ? (mb_type - 1) : -1;
  1123. }
  1124. ff_draw_horiz_band(avctx, s->cur_pic->f,
  1125. s->last_pic->f->data[0] ? s->last_pic->f : NULL,
  1126. 16 * sl->mb_y, 16, h->picture_structure, 0,
  1127. h->low_delay);
  1128. }
  1129. if (h->pict_type == AV_PICTURE_TYPE_B || h->low_delay)
  1130. ret = av_frame_ref(data, s->cur_pic->f);
  1131. else if (s->last_pic->f->data[0])
  1132. ret = av_frame_ref(data, s->last_pic->f);
  1133. if (ret < 0)
  1134. return ret;
  1135. /* Do not output the last pic after seeking. */
  1136. if (s->last_pic->f->data[0] || h->low_delay)
  1137. *got_frame = 1;
  1138. if (h->pict_type != AV_PICTURE_TYPE_B) {
  1139. FFSWAP(H264Picture*, s->cur_pic, s->next_pic);
  1140. } else {
  1141. av_frame_unref(s->cur_pic->f);
  1142. }
  1143. return buf_size;
  1144. }
  1145. static av_cold int svq3_decode_end(AVCodecContext *avctx)
  1146. {
  1147. SVQ3Context *s = avctx->priv_data;
  1148. H264Context *h = &s->h;
  1149. free_picture(avctx, s->cur_pic);
  1150. free_picture(avctx, s->next_pic);
  1151. free_picture(avctx, s->last_pic);
  1152. av_frame_free(&s->cur_pic->f);
  1153. av_frame_free(&s->next_pic->f);
  1154. av_frame_free(&s->last_pic->f);
  1155. av_freep(&s->cur_pic);
  1156. av_freep(&s->next_pic);
  1157. av_freep(&s->last_pic);
  1158. av_freep(&s->slice_buf);
  1159. memset(&h->cur_pic, 0, sizeof(h->cur_pic));
  1160. ff_h264_free_context(h);
  1161. return 0;
  1162. }
  1163. AVCodec ff_svq3_decoder = {
  1164. .name = "svq3",
  1165. .long_name = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 3 / Sorenson Video 3 / SVQ3"),
  1166. .type = AVMEDIA_TYPE_VIDEO,
  1167. .id = AV_CODEC_ID_SVQ3,
  1168. .priv_data_size = sizeof(SVQ3Context),
  1169. .init = svq3_decode_init,
  1170. .close = svq3_decode_end,
  1171. .decode = svq3_decode_frame,
  1172. .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND |
  1173. AV_CODEC_CAP_DR1 |
  1174. AV_CODEC_CAP_DELAY,
  1175. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P,
  1176. AV_PIX_FMT_NONE},
  1177. };