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.

1393 lines
49KB

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