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.

1367 lines
49KB

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