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.

1423 lines
50KB

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