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.

1489 lines
53KB

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