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.

1544 lines
55KB

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