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.

1362 lines
48KB

  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 "libavutil/attributes.h"
  42. #include "internal.h"
  43. #include "avcodec.h"
  44. #include "mpegvideo.h"
  45. #include "h264.h"
  46. #include "h264data.h" // FIXME FIXME FIXME
  47. #include "h264_mvpred.h"
  48. #include "golomb.h"
  49. #include "hpeldsp.h"
  50. #include "rectangle.h"
  51. #include "vdpau_internal.h"
  52. #if CONFIG_ZLIB
  53. #include <zlib.h>
  54. #endif
  55. #include "svq1.h"
  56. #include "svq3.h"
  57. /**
  58. * @file
  59. * svq3 decoder.
  60. */
  61. typedef struct {
  62. H264Context h;
  63. HpelDSPContext hdsp;
  64. Picture *cur_pic;
  65. Picture *next_pic;
  66. Picture *last_pic;
  67. int halfpel_flag;
  68. int thirdpel_flag;
  69. int unknown_flag;
  70. int next_slice_index;
  71. uint32_t watermark_key;
  72. uint8_t *buf;
  73. int buf_size;
  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, sign, 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. if ((int32_t)vlc < 0)
  220. return -1;
  221. sign = (vlc & 1) ? 0 : -1;
  222. vlc = vlc + 1 >> 1;
  223. if (type == 3) {
  224. if (vlc < 3) {
  225. run = 0;
  226. level = vlc;
  227. } else if (vlc < 4) {
  228. run = 1;
  229. level = 1;
  230. } else {
  231. run = vlc & 0x3;
  232. level = (vlc + 9 >> 2) - run;
  233. }
  234. } else {
  235. if (vlc < 16U) {
  236. run = svq3_dct_tables[intra][vlc].run;
  237. level = svq3_dct_tables[intra][vlc].level;
  238. } else if (intra) {
  239. run = vlc & 0x7;
  240. level = (vlc >> 3) + ((run == 0) ? 8 : ((run < 2) ? 2 : ((run < 5) ? 0 : -1)));
  241. } else {
  242. run = vlc & 0xF;
  243. level = (vlc >> 4) + ((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. const Picture *pic = (dir == 0) ? s->last_pic : s->next_pic;
  263. uint8_t *src, *dest;
  264. int i, emu = 0;
  265. int blocksize = 2 - (width >> 3); // 16->0, 8->1, 4->2
  266. mx += x;
  267. my += y;
  268. if (mx < 0 || mx >= s->h_edge_pos - width - 1 ||
  269. my < 0 || my >= s->v_edge_pos - height - 1) {
  270. emu = 1;
  271. mx = av_clip(mx, -16, s->h_edge_pos - width + 15);
  272. my = av_clip(my, -16, s->v_edge_pos - height + 15);
  273. }
  274. /* form component predictions */
  275. dest = h->cur_pic.f.data[0] + x + y * h->linesize;
  276. src = pic->f.data[0] + mx + my * h->linesize;
  277. if (emu) {
  278. h->vdsp.emulated_edge_mc(h->edge_emu_buffer, src,
  279. h->linesize, h->linesize,
  280. width + 1, height + 1,
  281. mx, my, s->h_edge_pos, s->v_edge_pos);
  282. src = h->edge_emu_buffer;
  283. }
  284. if (thirdpel)
  285. (avg ? h->dsp.avg_tpel_pixels_tab
  286. : h->dsp.put_tpel_pixels_tab)[dxy](dest, src, h->linesize,
  287. width, height);
  288. else
  289. (avg ? s->hdsp.avg_pixels_tab
  290. : s->hdsp.put_pixels_tab)[blocksize][dxy](dest, src, h->linesize,
  291. height);
  292. if (!(h->flags & CODEC_FLAG_GRAY)) {
  293. mx = mx + (mx < (int) x) >> 1;
  294. my = my + (my < (int) y) >> 1;
  295. width = width >> 1;
  296. height = height >> 1;
  297. blocksize++;
  298. for (i = 1; i < 3; i++) {
  299. dest = h->cur_pic.f.data[i] + (x >> 1) + (y >> 1) * h->uvlinesize;
  300. src = pic->f.data[i] + mx + my * h->uvlinesize;
  301. if (emu) {
  302. h->vdsp.emulated_edge_mc(h->edge_emu_buffer, src,
  303. h->uvlinesize, h->uvlinesize,
  304. width + 1, height + 1,
  305. mx, my, (s->h_edge_pos >> 1),
  306. s->v_edge_pos >> 1);
  307. src = h->edge_emu_buffer;
  308. }
  309. if (thirdpel)
  310. (avg ? h->dsp.avg_tpel_pixels_tab
  311. : h->dsp.put_tpel_pixels_tab)[dxy](dest, src,
  312. h->uvlinesize,
  313. width, height);
  314. else
  315. (avg ? s->hdsp.avg_pixels_tab
  316. : s->hdsp.put_pixels_tab)[blocksize][dxy](dest, src,
  317. h->uvlinesize,
  318. height);
  319. }
  320. }
  321. }
  322. static inline int svq3_mc_dir(SVQ3Context *s, int size, int mode,
  323. int dir, int avg)
  324. {
  325. int i, j, k, mx, my, dx, dy, x, y;
  326. H264Context *h = &s->h;
  327. const int part_width = ((size & 5) == 4) ? 4 : 16 >> (size & 1);
  328. const int part_height = 16 >> ((unsigned)(size + 1) / 3);
  329. const int extra_width = (mode == PREDICT_MODE) ? -16 * 6 : 0;
  330. const int h_edge_pos = 6 * (s->h_edge_pos - part_width) - extra_width;
  331. const int v_edge_pos = 6 * (s->v_edge_pos - part_height) - extra_width;
  332. for (i = 0; i < 16; i += part_height)
  333. for (j = 0; j < 16; j += part_width) {
  334. const int b_xy = (4 * h->mb_x + (j >> 2)) +
  335. (4 * h->mb_y + (i >> 2)) * h->b_stride;
  336. int dxy;
  337. x = 16 * h->mb_x + j;
  338. y = 16 * h->mb_y + i;
  339. k = (j >> 2 & 1) + (i >> 1 & 2) +
  340. (j >> 1 & 4) + (i & 8);
  341. if (mode != PREDICT_MODE) {
  342. pred_motion(h, k, part_width >> 2, dir, 1, &mx, &my);
  343. } else {
  344. mx = s->next_pic->motion_val[0][b_xy][0] << 1;
  345. my = s->next_pic->motion_val[0][b_xy][1] << 1;
  346. if (dir == 0) {
  347. mx = mx * h->frame_num_offset /
  348. h->prev_frame_num_offset + 1 >> 1;
  349. my = my * h->frame_num_offset /
  350. h->prev_frame_num_offset + 1 >> 1;
  351. } else {
  352. mx = mx * (h->frame_num_offset - h->prev_frame_num_offset) /
  353. h->prev_frame_num_offset + 1 >> 1;
  354. my = my * (h->frame_num_offset - h->prev_frame_num_offset) /
  355. h->prev_frame_num_offset + 1 >> 1;
  356. }
  357. }
  358. /* clip motion vector prediction to frame border */
  359. mx = av_clip(mx, extra_width - 6 * x, h_edge_pos - 6 * x);
  360. my = av_clip(my, extra_width - 6 * y, v_edge_pos - 6 * y);
  361. /* get (optional) motion vector differential */
  362. if (mode == PREDICT_MODE) {
  363. dx = dy = 0;
  364. } else {
  365. dy = svq3_get_se_golomb(&h->gb);
  366. dx = svq3_get_se_golomb(&h->gb);
  367. if (dx == INVALID_VLC || dy == INVALID_VLC) {
  368. av_log(h->avctx, AV_LOG_ERROR, "invalid MV vlc\n");
  369. return -1;
  370. }
  371. }
  372. /* compute motion vector */
  373. if (mode == THIRDPEL_MODE) {
  374. int fx, fy;
  375. mx = (mx + 1 >> 1) + dx;
  376. my = (my + 1 >> 1) + dy;
  377. fx = (unsigned)(mx + 0x3000) / 3 - 0x1000;
  378. fy = (unsigned)(my + 0x3000) / 3 - 0x1000;
  379. dxy = (mx - 3 * fx) + 4 * (my - 3 * fy);
  380. svq3_mc_dir_part(s, x, y, part_width, part_height,
  381. fx, fy, dxy, 1, dir, avg);
  382. mx += mx;
  383. my += my;
  384. } else if (mode == HALFPEL_MODE || mode == PREDICT_MODE) {
  385. mx = (unsigned)(mx + 1 + 0x3000) / 3 + dx - 0x1000;
  386. my = (unsigned)(my + 1 + 0x3000) / 3 + dy - 0x1000;
  387. dxy = (mx & 1) + 2 * (my & 1);
  388. svq3_mc_dir_part(s, x, y, part_width, part_height,
  389. mx >> 1, my >> 1, dxy, 0, dir, avg);
  390. mx *= 3;
  391. my *= 3;
  392. } else {
  393. mx = (unsigned)(mx + 3 + 0x6000) / 6 + dx - 0x1000;
  394. my = (unsigned)(my + 3 + 0x6000) / 6 + dy - 0x1000;
  395. svq3_mc_dir_part(s, x, y, part_width, part_height,
  396. mx, my, 0, 0, dir, avg);
  397. mx *= 6;
  398. my *= 6;
  399. }
  400. /* update mv_cache */
  401. if (mode != PREDICT_MODE) {
  402. int32_t mv = pack16to32(mx, my);
  403. if (part_height == 8 && i < 8) {
  404. AV_WN32A(h->mv_cache[dir][scan8[k] + 1 * 8], mv);
  405. if (part_width == 8 && j < 8)
  406. AV_WN32A(h->mv_cache[dir][scan8[k] + 1 + 1 * 8], mv);
  407. }
  408. if (part_width == 8 && j < 8)
  409. AV_WN32A(h->mv_cache[dir][scan8[k] + 1], mv);
  410. if (part_width == 4 || part_height == 4)
  411. AV_WN32A(h->mv_cache[dir][scan8[k]], mv);
  412. }
  413. /* write back motion vectors */
  414. fill_rectangle(h->cur_pic.motion_val[dir][b_xy],
  415. part_width >> 2, part_height >> 2, h->b_stride,
  416. pack16to32(mx, my), 4);
  417. }
  418. return 0;
  419. }
  420. static int svq3_decode_mb(SVQ3Context *s, unsigned int mb_type)
  421. {
  422. H264Context *h = &s->h;
  423. int i, j, k, m, dir, mode;
  424. int cbp = 0;
  425. uint32_t vlc;
  426. int8_t *top, *left;
  427. const int mb_xy = h->mb_xy;
  428. const int b_xy = 4 * h->mb_x + 4 * h->mb_y * h->b_stride;
  429. h->top_samples_available = (h->mb_y == 0) ? 0x33FF : 0xFFFF;
  430. h->left_samples_available = (h->mb_x == 0) ? 0x5F5F : 0xFFFF;
  431. h->topright_samples_available = 0xFFFF;
  432. if (mb_type == 0) { /* SKIP */
  433. if (h->pict_type == AV_PICTURE_TYPE_P ||
  434. s->next_pic->mb_type[mb_xy] == -1) {
  435. svq3_mc_dir_part(s, 16 * h->mb_x, 16 * h->mb_y, 16, 16,
  436. 0, 0, 0, 0, 0, 0);
  437. if (h->pict_type == AV_PICTURE_TYPE_B)
  438. svq3_mc_dir_part(s, 16 * h->mb_x, 16 * h->mb_y, 16, 16,
  439. 0, 0, 0, 0, 1, 1);
  440. mb_type = MB_TYPE_SKIP;
  441. } else {
  442. mb_type = FFMIN(s->next_pic->mb_type[mb_xy], 6);
  443. if (svq3_mc_dir(s, mb_type, PREDICT_MODE, 0, 0) < 0)
  444. return -1;
  445. if (svq3_mc_dir(s, mb_type, PREDICT_MODE, 1, 1) < 0)
  446. return -1;
  447. mb_type = MB_TYPE_16x16;
  448. }
  449. } else if (mb_type < 8) { /* INTER */
  450. if (s->thirdpel_flag && s->halfpel_flag == !get_bits1(&h->gb))
  451. mode = THIRDPEL_MODE;
  452. else if (s->halfpel_flag &&
  453. s->thirdpel_flag == !get_bits1(&h->gb))
  454. mode = HALFPEL_MODE;
  455. else
  456. mode = FULLPEL_MODE;
  457. /* fill caches */
  458. /* note ref_cache should contain here:
  459. * ????????
  460. * ???11111
  461. * N??11111
  462. * N??11111
  463. * N??11111
  464. */
  465. for (m = 0; m < 2; m++) {
  466. if (h->mb_x > 0 && h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - 1] + 6] != -1) {
  467. for (i = 0; i < 4; i++)
  468. AV_COPY32(h->mv_cache[m][scan8[0] - 1 + i * 8],
  469. h->cur_pic.motion_val[m][b_xy - 1 + i * h->b_stride]);
  470. } else {
  471. for (i = 0; i < 4; i++)
  472. AV_ZERO32(h->mv_cache[m][scan8[0] - 1 + i * 8]);
  473. }
  474. if (h->mb_y > 0) {
  475. memcpy(h->mv_cache[m][scan8[0] - 1 * 8],
  476. h->cur_pic.motion_val[m][b_xy - h->b_stride],
  477. 4 * 2 * sizeof(int16_t));
  478. memset(&h->ref_cache[m][scan8[0] - 1 * 8],
  479. (h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1, 4);
  480. if (h->mb_x < h->mb_width - 1) {
  481. AV_COPY32(h->mv_cache[m][scan8[0] + 4 - 1 * 8],
  482. h->cur_pic.motion_val[m][b_xy - h->b_stride + 4]);
  483. h->ref_cache[m][scan8[0] + 4 - 1 * 8] =
  484. (h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride + 1] + 6] == -1 ||
  485. h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1;
  486. } else
  487. h->ref_cache[m][scan8[0] + 4 - 1 * 8] = PART_NOT_AVAILABLE;
  488. if (h->mb_x > 0) {
  489. AV_COPY32(h->mv_cache[m][scan8[0] - 1 - 1 * 8],
  490. h->cur_pic.motion_val[m][b_xy - h->b_stride - 1]);
  491. h->ref_cache[m][scan8[0] - 1 - 1 * 8] =
  492. (h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride - 1] + 3] == -1) ? PART_NOT_AVAILABLE : 1;
  493. } else
  494. h->ref_cache[m][scan8[0] - 1 - 1 * 8] = PART_NOT_AVAILABLE;
  495. } else
  496. memset(&h->ref_cache[m][scan8[0] - 1 * 8 - 1],
  497. PART_NOT_AVAILABLE, 8);
  498. if (h->pict_type != AV_PICTURE_TYPE_B)
  499. break;
  500. }
  501. /* decode motion vector(s) and form prediction(s) */
  502. if (h->pict_type == AV_PICTURE_TYPE_P) {
  503. if (svq3_mc_dir(s, mb_type - 1, mode, 0, 0) < 0)
  504. return -1;
  505. } else { /* AV_PICTURE_TYPE_B */
  506. if (mb_type != 2) {
  507. if (svq3_mc_dir(s, 0, mode, 0, 0) < 0)
  508. return -1;
  509. } else {
  510. for (i = 0; i < 4; i++)
  511. memset(h->cur_pic.motion_val[0][b_xy + i * h->b_stride],
  512. 0, 4 * 2 * sizeof(int16_t));
  513. }
  514. if (mb_type != 1) {
  515. if (svq3_mc_dir(s, 0, mode, 1, mb_type == 3) < 0)
  516. return -1;
  517. } else {
  518. for (i = 0; i < 4; i++)
  519. memset(h->cur_pic.motion_val[1][b_xy + i * h->b_stride],
  520. 0, 4 * 2 * sizeof(int16_t));
  521. }
  522. }
  523. mb_type = MB_TYPE_16x16;
  524. } else if (mb_type == 8 || mb_type == 33) { /* INTRA4x4 */
  525. memset(h->intra4x4_pred_mode_cache, -1, 8 * 5 * sizeof(int8_t));
  526. if (mb_type == 8) {
  527. if (h->mb_x > 0) {
  528. for (i = 0; i < 4; i++)
  529. h->intra4x4_pred_mode_cache[scan8[0] - 1 + i * 8] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - 1] + 6 - i];
  530. if (h->intra4x4_pred_mode_cache[scan8[0] - 1] == -1)
  531. h->left_samples_available = 0x5F5F;
  532. }
  533. if (h->mb_y > 0) {
  534. h->intra4x4_pred_mode_cache[4 + 8 * 0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride] + 0];
  535. h->intra4x4_pred_mode_cache[5 + 8 * 0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride] + 1];
  536. h->intra4x4_pred_mode_cache[6 + 8 * 0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride] + 2];
  537. h->intra4x4_pred_mode_cache[7 + 8 * 0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride] + 3];
  538. if (h->intra4x4_pred_mode_cache[4 + 8 * 0] == -1)
  539. h->top_samples_available = 0x33FF;
  540. }
  541. /* decode prediction codes for luma blocks */
  542. for (i = 0; i < 16; i += 2) {
  543. vlc = svq3_get_ue_golomb(&h->gb);
  544. if (vlc >= 25U) {
  545. av_log(h->avctx, AV_LOG_ERROR, "luma prediction:%d\n", vlc);
  546. return -1;
  547. }
  548. left = &h->intra4x4_pred_mode_cache[scan8[i] - 1];
  549. top = &h->intra4x4_pred_mode_cache[scan8[i] - 8];
  550. left[1] = svq3_pred_1[top[0] + 1][left[0] + 1][svq3_pred_0[vlc][0]];
  551. left[2] = svq3_pred_1[top[1] + 1][left[1] + 1][svq3_pred_0[vlc][1]];
  552. if (left[1] == -1 || left[2] == -1) {
  553. av_log(h->avctx, AV_LOG_ERROR, "weird prediction\n");
  554. return -1;
  555. }
  556. }
  557. } else { /* mb_type == 33, DC_128_PRED block type */
  558. for (i = 0; i < 4; i++)
  559. memset(&h->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_PRED, 4);
  560. }
  561. write_back_intra_pred_mode(h);
  562. if (mb_type == 8) {
  563. ff_h264_check_intra4x4_pred_mode(h);
  564. h->top_samples_available = (h->mb_y == 0) ? 0x33FF : 0xFFFF;
  565. h->left_samples_available = (h->mb_x == 0) ? 0x5F5F : 0xFFFF;
  566. } else {
  567. for (i = 0; i < 4; i++)
  568. memset(&h->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_128_PRED, 4);
  569. h->top_samples_available = 0x33FF;
  570. h->left_samples_available = 0x5F5F;
  571. }
  572. mb_type = MB_TYPE_INTRA4x4;
  573. } else { /* INTRA16x16 */
  574. dir = i_mb_type_info[mb_type - 8].pred_mode;
  575. dir = (dir >> 1) ^ 3 * (dir & 1) ^ 1;
  576. if ((h->intra16x16_pred_mode = ff_h264_check_intra_pred_mode(h, dir, 0)) < 0) {
  577. av_log(h->avctx, AV_LOG_ERROR, "ff_h264_check_intra_pred_mode < 0\n");
  578. return h->intra16x16_pred_mode;
  579. }
  580. cbp = i_mb_type_info[mb_type - 8].cbp;
  581. mb_type = MB_TYPE_INTRA16x16;
  582. }
  583. if (!IS_INTER(mb_type) && h->pict_type != AV_PICTURE_TYPE_I) {
  584. for (i = 0; i < 4; i++)
  585. memset(h->cur_pic.motion_val[0][b_xy + i * h->b_stride],
  586. 0, 4 * 2 * sizeof(int16_t));
  587. if (h->pict_type == AV_PICTURE_TYPE_B) {
  588. for (i = 0; i < 4; i++)
  589. memset(h->cur_pic.motion_val[1][b_xy + i * h->b_stride],
  590. 0, 4 * 2 * sizeof(int16_t));
  591. }
  592. }
  593. if (!IS_INTRA4x4(mb_type)) {
  594. memset(h->intra4x4_pred_mode + h->mb2br_xy[mb_xy], DC_PRED, 8);
  595. }
  596. if (!IS_SKIP(mb_type) || h->pict_type == AV_PICTURE_TYPE_B) {
  597. memset(h->non_zero_count_cache + 8, 0, 14 * 8 * sizeof(uint8_t));
  598. }
  599. if (!IS_INTRA16x16(mb_type) &&
  600. (!IS_SKIP(mb_type) || h->pict_type == AV_PICTURE_TYPE_B)) {
  601. if ((vlc = svq3_get_ue_golomb(&h->gb)) >= 48U){
  602. av_log(h->avctx, AV_LOG_ERROR, "cbp_vlc=%d\n", vlc);
  603. return -1;
  604. }
  605. cbp = IS_INTRA(mb_type) ? golomb_to_intra4x4_cbp[vlc]
  606. : golomb_to_inter_cbp[vlc];
  607. }
  608. if (IS_INTRA16x16(mb_type) ||
  609. (h->pict_type != AV_PICTURE_TYPE_I && s->adaptive_quant && cbp)) {
  610. h->qscale += svq3_get_se_golomb(&h->gb);
  611. if (h->qscale > 31u) {
  612. av_log(h->avctx, AV_LOG_ERROR, "qscale:%d\n", h->qscale);
  613. return -1;
  614. }
  615. }
  616. if (IS_INTRA16x16(mb_type)) {
  617. AV_ZERO128(h->mb_luma_dc[0] + 0);
  618. AV_ZERO128(h->mb_luma_dc[0] + 8);
  619. if (svq3_decode_block(&h->gb, h->mb_luma_dc[0], 0, 1)) {
  620. av_log(h->avctx, AV_LOG_ERROR,
  621. "error while decoding intra luma dc\n");
  622. return -1;
  623. }
  624. }
  625. if (cbp) {
  626. const int index = IS_INTRA16x16(mb_type) ? 1 : 0;
  627. const int type = ((h->qscale < 24 && IS_INTRA4x4(mb_type)) ? 2 : 1);
  628. for (i = 0; i < 4; i++)
  629. if ((cbp & (1 << i))) {
  630. for (j = 0; j < 4; j++) {
  631. k = index ? (1 * (j & 1) + 2 * (i & 1) +
  632. 2 * (j & 2) + 4 * (i & 2))
  633. : (4 * i + j);
  634. h->non_zero_count_cache[scan8[k]] = 1;
  635. if (svq3_decode_block(&h->gb, &h->mb[16 * k], index, type)) {
  636. av_log(h->avctx, AV_LOG_ERROR,
  637. "error while decoding block\n");
  638. return -1;
  639. }
  640. }
  641. }
  642. if ((cbp & 0x30)) {
  643. for (i = 1; i < 3; ++i)
  644. if (svq3_decode_block(&h->gb, &h->mb[16 * 16 * i], 0, 3)) {
  645. av_log(h->avctx, AV_LOG_ERROR,
  646. "error while decoding chroma dc block\n");
  647. return -1;
  648. }
  649. if ((cbp & 0x20)) {
  650. for (i = 1; i < 3; i++) {
  651. for (j = 0; j < 4; j++) {
  652. k = 16 * i + j;
  653. h->non_zero_count_cache[scan8[k]] = 1;
  654. if (svq3_decode_block(&h->gb, &h->mb[16 * k], 1, 1)) {
  655. av_log(h->avctx, AV_LOG_ERROR,
  656. "error while decoding chroma ac block\n");
  657. return -1;
  658. }
  659. }
  660. }
  661. }
  662. }
  663. }
  664. h->cbp = cbp;
  665. h->cur_pic.mb_type[mb_xy] = mb_type;
  666. if (IS_INTRA(mb_type))
  667. h->chroma_pred_mode = ff_h264_check_intra_pred_mode(h, DC_PRED8x8, 1);
  668. return 0;
  669. }
  670. static int svq3_decode_slice_header(AVCodecContext *avctx)
  671. {
  672. SVQ3Context *s = avctx->priv_data;
  673. H264Context *h = &s->h;
  674. const int mb_xy = h->mb_xy;
  675. int i, header;
  676. unsigned slice_id;
  677. header = get_bits(&h->gb, 8);
  678. if (((header & 0x9F) != 1 && (header & 0x9F) != 2) || (header & 0x60) == 0) {
  679. /* TODO: what? */
  680. av_log(avctx, AV_LOG_ERROR, "unsupported slice header (%02X)\n", header);
  681. return -1;
  682. } else {
  683. int length = header >> 5 & 3;
  684. s->next_slice_index = get_bits_count(&h->gb) +
  685. 8 * show_bits(&h->gb, 8 * length) +
  686. 8 * length;
  687. if (s->next_slice_index > h->gb.size_in_bits) {
  688. av_log(avctx, AV_LOG_ERROR, "slice after bitstream end\n");
  689. return -1;
  690. }
  691. h->gb.size_in_bits = s->next_slice_index - 8 * (length - 1);
  692. skip_bits(&h->gb, 8);
  693. if (s->watermark_key) {
  694. uint32_t header = AV_RL32(&h->gb.buffer[(get_bits_count(&h->gb) >> 3) + 1]);
  695. AV_WL32(&h->gb.buffer[(get_bits_count(&h->gb) >> 3) + 1],
  696. header ^ s->watermark_key);
  697. }
  698. if (length > 0) {
  699. memmove((uint8_t *) &h->gb.buffer[get_bits_count(&h->gb) >> 3],
  700. &h->gb.buffer[h->gb.size_in_bits >> 3], length - 1);
  701. }
  702. skip_bits_long(&h->gb, 0);
  703. }
  704. if ((slice_id = svq3_get_ue_golomb(&h->gb)) >= 3) {
  705. av_log(h->avctx, AV_LOG_ERROR, "illegal slice type %d \n", slice_id);
  706. return -1;
  707. }
  708. h->slice_type = golomb_to_pict_type[slice_id];
  709. if ((header & 0x9F) == 2) {
  710. i = (h->mb_num < 64) ? 6 : (1 + av_log2(h->mb_num - 1));
  711. h->mb_skip_run = get_bits(&h->gb, i) -
  712. (h->mb_y * h->mb_width + h->mb_x);
  713. } else {
  714. skip_bits1(&h->gb);
  715. h->mb_skip_run = 0;
  716. }
  717. h->slice_num = get_bits(&h->gb, 8);
  718. h->qscale = get_bits(&h->gb, 5);
  719. s->adaptive_quant = get_bits1(&h->gb);
  720. /* unknown fields */
  721. skip_bits1(&h->gb);
  722. if (s->unknown_flag)
  723. skip_bits1(&h->gb);
  724. skip_bits1(&h->gb);
  725. skip_bits(&h->gb, 2);
  726. if (skip_1stop_8data_bits(&h->gb) < 0)
  727. return AVERROR_INVALIDDATA;
  728. /* reset intra predictors and invalidate motion vector references */
  729. if (h->mb_x > 0) {
  730. memset(h->intra4x4_pred_mode + h->mb2br_xy[mb_xy - 1] + 3,
  731. -1, 4 * sizeof(int8_t));
  732. memset(h->intra4x4_pred_mode + h->mb2br_xy[mb_xy - h->mb_x],
  733. -1, 8 * sizeof(int8_t) * h->mb_x);
  734. }
  735. if (h->mb_y > 0) {
  736. memset(h->intra4x4_pred_mode + h->mb2br_xy[mb_xy - h->mb_stride],
  737. -1, 8 * sizeof(int8_t) * (h->mb_width - h->mb_x));
  738. if (h->mb_x > 0)
  739. h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride - 1] + 3] = -1;
  740. }
  741. return 0;
  742. }
  743. static av_cold int svq3_decode_init(AVCodecContext *avctx)
  744. {
  745. SVQ3Context *s = avctx->priv_data;
  746. H264Context *h = &s->h;
  747. int m;
  748. unsigned char *extradata;
  749. unsigned char *extradata_end;
  750. unsigned int size;
  751. int marker_found = 0;
  752. s->cur_pic = av_mallocz(sizeof(*s->cur_pic));
  753. s->last_pic = av_mallocz(sizeof(*s->last_pic));
  754. s->next_pic = av_mallocz(sizeof(*s->next_pic));
  755. if (!s->next_pic || !s->last_pic || !s->cur_pic) {
  756. av_freep(&s->cur_pic);
  757. av_freep(&s->last_pic);
  758. av_freep(&s->next_pic);
  759. return AVERROR(ENOMEM);
  760. }
  761. if (ff_h264_decode_init(avctx) < 0)
  762. return -1;
  763. ff_hpeldsp_init(&s->hdsp, avctx->flags);
  764. h->flags = avctx->flags;
  765. h->is_complex = 1;
  766. h->sps.chroma_format_idc = 1;
  767. h->picture_structure = PICT_FRAME;
  768. avctx->pix_fmt = avctx->codec->pix_fmts[0];
  769. h->chroma_qp[0] = h->chroma_qp[1] = 4;
  770. h->chroma_x_shift = h->chroma_y_shift = 1;
  771. s->halfpel_flag = 1;
  772. s->thirdpel_flag = 1;
  773. s->unknown_flag = 0;
  774. /* prowl for the "SEQH" marker in the extradata */
  775. extradata = (unsigned char *)avctx->extradata;
  776. extradata_end = avctx->extradata + avctx->extradata_size;
  777. if (extradata) {
  778. for (m = 0; m + 8 < avctx->extradata_size; m++) {
  779. if (!memcmp(extradata, "SEQH", 4)) {
  780. marker_found = 1;
  781. break;
  782. }
  783. extradata++;
  784. }
  785. }
  786. /* if a match was found, parse the extra data */
  787. if (marker_found) {
  788. GetBitContext gb;
  789. int frame_size_code;
  790. size = AV_RB32(&extradata[4]);
  791. if (size > extradata_end - extradata - 8)
  792. return AVERROR_INVALIDDATA;
  793. init_get_bits(&gb, extradata + 8, size * 8);
  794. /* 'frame size code' and optional 'width, height' */
  795. frame_size_code = get_bits(&gb, 3);
  796. switch (frame_size_code) {
  797. case 0:
  798. avctx->width = 160;
  799. avctx->height = 120;
  800. break;
  801. case 1:
  802. avctx->width = 128;
  803. avctx->height = 96;
  804. break;
  805. case 2:
  806. avctx->width = 176;
  807. avctx->height = 144;
  808. break;
  809. case 3:
  810. avctx->width = 352;
  811. avctx->height = 288;
  812. break;
  813. case 4:
  814. avctx->width = 704;
  815. avctx->height = 576;
  816. break;
  817. case 5:
  818. avctx->width = 240;
  819. avctx->height = 180;
  820. break;
  821. case 6:
  822. avctx->width = 320;
  823. avctx->height = 240;
  824. break;
  825. case 7:
  826. avctx->width = get_bits(&gb, 12);
  827. avctx->height = get_bits(&gb, 12);
  828. break;
  829. }
  830. s->halfpel_flag = get_bits1(&gb);
  831. s->thirdpel_flag = get_bits1(&gb);
  832. /* unknown fields */
  833. skip_bits1(&gb);
  834. skip_bits1(&gb);
  835. skip_bits1(&gb);
  836. skip_bits1(&gb);
  837. h->low_delay = get_bits1(&gb);
  838. /* unknown field */
  839. skip_bits1(&gb);
  840. if (skip_1stop_8data_bits(&gb) < 0)
  841. return AVERROR_INVALIDDATA;
  842. s->unknown_flag = get_bits1(&gb);
  843. avctx->has_b_frames = !h->low_delay;
  844. if (s->unknown_flag) {
  845. #if CONFIG_ZLIB
  846. unsigned watermark_width = svq3_get_ue_golomb(&gb);
  847. unsigned watermark_height = svq3_get_ue_golomb(&gb);
  848. int u1 = svq3_get_ue_golomb(&gb);
  849. int u2 = get_bits(&gb, 8);
  850. int u3 = get_bits(&gb, 2);
  851. int u4 = svq3_get_ue_golomb(&gb);
  852. unsigned long buf_len = watermark_width *
  853. watermark_height * 4;
  854. int offset = get_bits_count(&gb) + 7 >> 3;
  855. uint8_t *buf;
  856. if (watermark_height <= 0 ||
  857. (uint64_t)watermark_width * 4 > UINT_MAX / watermark_height)
  858. return -1;
  859. buf = av_malloc(buf_len);
  860. av_log(avctx, AV_LOG_DEBUG, "watermark size: %dx%d\n",
  861. watermark_width, watermark_height);
  862. av_log(avctx, AV_LOG_DEBUG,
  863. "u1: %x u2: %x u3: %x compressed data size: %d offset: %d\n",
  864. u1, u2, u3, u4, offset);
  865. if (uncompress(buf, &buf_len, extradata + 8 + offset,
  866. size - offset) != Z_OK) {
  867. av_log(avctx, AV_LOG_ERROR,
  868. "could not uncompress watermark logo\n");
  869. av_free(buf);
  870. return -1;
  871. }
  872. s->watermark_key = ff_svq1_packet_checksum(buf, buf_len, 0);
  873. s->watermark_key = s->watermark_key << 16 | s->watermark_key;
  874. av_log(avctx, AV_LOG_DEBUG,
  875. "watermark key %#x\n", s->watermark_key);
  876. av_free(buf);
  877. #else
  878. av_log(avctx, AV_LOG_ERROR,
  879. "this svq3 file contains watermark which need zlib support compiled in\n");
  880. return -1;
  881. #endif
  882. }
  883. }
  884. h->width = avctx->width;
  885. h->height = avctx->height;
  886. h->mb_width = (h->width + 15) / 16;
  887. h->mb_height = (h->height + 15) / 16;
  888. h->mb_stride = h->mb_width + 1;
  889. h->mb_num = h->mb_width * h->mb_height;
  890. h->b_stride = 4 * h->mb_width;
  891. s->h_edge_pos = h->mb_width * 16;
  892. s->v_edge_pos = h->mb_height * 16;
  893. if (ff_h264_alloc_tables(h) < 0) {
  894. av_log(avctx, AV_LOG_ERROR, "svq3 memory allocation failed\n");
  895. return AVERROR(ENOMEM);
  896. }
  897. return 0;
  898. }
  899. static void free_picture(AVCodecContext *avctx, Picture *pic)
  900. {
  901. int i;
  902. for (i = 0; i < 2; i++) {
  903. av_buffer_unref(&pic->motion_val_buf[i]);
  904. av_buffer_unref(&pic->ref_index_buf[i]);
  905. }
  906. av_buffer_unref(&pic->mb_type_buf);
  907. av_frame_unref(&pic->f);
  908. }
  909. static int get_buffer(AVCodecContext *avctx, Picture *pic)
  910. {
  911. SVQ3Context *s = avctx->priv_data;
  912. H264Context *h = &s->h;
  913. const int big_mb_num = h->mb_stride * (h->mb_height + 1) + 1;
  914. const int mb_array_size = h->mb_stride * h->mb_height;
  915. const int b4_stride = h->mb_width * 4 + 1;
  916. const int b4_array_size = b4_stride * h->mb_height * 4;
  917. int ret;
  918. if (!pic->motion_val_buf[0]) {
  919. int i;
  920. pic->mb_type_buf = av_buffer_allocz((big_mb_num + h->mb_stride) * sizeof(uint32_t));
  921. if (!pic->mb_type_buf)
  922. return AVERROR(ENOMEM);
  923. pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * h->mb_stride + 1;
  924. for (i = 0; i < 2; i++) {
  925. pic->motion_val_buf[i] = av_buffer_allocz(2 * (b4_array_size + 4) * sizeof(int16_t));
  926. pic->ref_index_buf[i] = av_buffer_allocz(4 * mb_array_size);
  927. if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i]) {
  928. ret = AVERROR(ENOMEM);
  929. goto fail;
  930. }
  931. pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
  932. pic->ref_index[i] = pic->ref_index_buf[i]->data;
  933. }
  934. }
  935. pic->reference = !(h->pict_type == AV_PICTURE_TYPE_B);
  936. ret = ff_get_buffer(avctx, &pic->f,
  937. pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
  938. if (ret < 0)
  939. goto fail;
  940. if (!h->edge_emu_buffer) {
  941. h->edge_emu_buffer = av_mallocz(pic->f.linesize[0] * 17);
  942. if (!h->edge_emu_buffer)
  943. return AVERROR(ENOMEM);
  944. }
  945. h->linesize = pic->f.linesize[0];
  946. h->uvlinesize = pic->f.linesize[1];
  947. return 0;
  948. fail:
  949. free_picture(avctx, pic);
  950. return ret;
  951. }
  952. static int svq3_decode_frame(AVCodecContext *avctx, void *data,
  953. int *got_frame, AVPacket *avpkt)
  954. {
  955. SVQ3Context *s = avctx->priv_data;
  956. H264Context *h = &s->h;
  957. int buf_size = avpkt->size;
  958. int left;
  959. uint8_t *buf;
  960. int ret, m, i;
  961. /* special case for last picture */
  962. if (buf_size == 0) {
  963. if (s->next_pic->f.data[0] && !h->low_delay && !s->last_frame_output) {
  964. ret = av_frame_ref(data, &s->next_pic->f);
  965. if (ret < 0)
  966. return ret;
  967. s->last_frame_output = 1;
  968. *got_frame = 1;
  969. }
  970. return 0;
  971. }
  972. h->mb_x = h->mb_y = h->mb_xy = 0;
  973. if (s->watermark_key) {
  974. av_fast_padded_malloc(&s->buf, &s->buf_size, buf_size);
  975. if (!s->buf)
  976. return AVERROR(ENOMEM);
  977. memcpy(s->buf, avpkt->data, buf_size);
  978. buf = s->buf;
  979. } else {
  980. buf = avpkt->data;
  981. }
  982. init_get_bits(&h->gb, buf, 8 * buf_size);
  983. if (svq3_decode_slice_header(avctx))
  984. return -1;
  985. h->pict_type = h->slice_type;
  986. if (h->pict_type != AV_PICTURE_TYPE_B)
  987. FFSWAP(Picture*, s->next_pic, s->last_pic);
  988. av_frame_unref(&s->cur_pic->f);
  989. /* for skipping the frame */
  990. s->cur_pic->f.pict_type = h->pict_type;
  991. s->cur_pic->f.key_frame = (h->pict_type == AV_PICTURE_TYPE_I);
  992. ret = get_buffer(avctx, s->cur_pic);
  993. if (ret < 0)
  994. return ret;
  995. h->cur_pic_ptr = s->cur_pic;
  996. av_frame_unref(&h->cur_pic.f);
  997. h->cur_pic = *s->cur_pic;
  998. ret = av_frame_ref(&h->cur_pic.f, &s->cur_pic->f);
  999. if (ret < 0)
  1000. return ret;
  1001. for (i = 0; i < 16; i++) {
  1002. h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * h->linesize * ((scan8[i] - scan8[0]) >> 3);
  1003. h->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * h->linesize * ((scan8[i] - scan8[0]) >> 3);
  1004. }
  1005. for (i = 0; i < 16; i++) {
  1006. h->block_offset[16 + i] =
  1007. h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * h->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
  1008. h->block_offset[48 + 16 + i] =
  1009. h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * h->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
  1010. }
  1011. if (h->pict_type != AV_PICTURE_TYPE_I) {
  1012. if (!s->last_pic->f.data[0]) {
  1013. av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
  1014. ret = get_buffer(avctx, s->last_pic);
  1015. if (ret < 0)
  1016. return ret;
  1017. memset(s->last_pic->f.data[0], 0, avctx->height * s->last_pic->f.linesize[0]);
  1018. memset(s->last_pic->f.data[1], 0x80, (avctx->height / 2) *
  1019. s->last_pic->f.linesize[1]);
  1020. memset(s->last_pic->f.data[2], 0x80, (avctx->height / 2) *
  1021. s->last_pic->f.linesize[2]);
  1022. }
  1023. if (h->pict_type == AV_PICTURE_TYPE_B && !s->next_pic->f.data[0]) {
  1024. av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
  1025. ret = get_buffer(avctx, s->next_pic);
  1026. if (ret < 0)
  1027. return ret;
  1028. memset(s->next_pic->f.data[0], 0, avctx->height * s->next_pic->f.linesize[0]);
  1029. memset(s->next_pic->f.data[1], 0x80, (avctx->height / 2) *
  1030. s->next_pic->f.linesize[1]);
  1031. memset(s->next_pic->f.data[2], 0x80, (avctx->height / 2) *
  1032. s->next_pic->f.linesize[2]);
  1033. }
  1034. }
  1035. if (avctx->debug & FF_DEBUG_PICT_INFO)
  1036. av_log(h->avctx, AV_LOG_DEBUG,
  1037. "%c hpel:%d, tpel:%d aqp:%d qp:%d, slice_num:%02X\n",
  1038. av_get_picture_type_char(h->pict_type),
  1039. s->halfpel_flag, s->thirdpel_flag,
  1040. s->adaptive_quant, h->qscale, h->slice_num);
  1041. if (avctx->skip_frame >= AVDISCARD_NONREF && h->pict_type == AV_PICTURE_TYPE_B ||
  1042. avctx->skip_frame >= AVDISCARD_NONKEY && h->pict_type != AV_PICTURE_TYPE_I ||
  1043. avctx->skip_frame >= AVDISCARD_ALL)
  1044. return 0;
  1045. if (s->next_p_frame_damaged) {
  1046. if (h->pict_type == AV_PICTURE_TYPE_B)
  1047. return 0;
  1048. else
  1049. s->next_p_frame_damaged = 0;
  1050. }
  1051. if (h->pict_type == AV_PICTURE_TYPE_B) {
  1052. h->frame_num_offset = h->slice_num - h->prev_frame_num;
  1053. if (h->frame_num_offset < 0)
  1054. h->frame_num_offset += 256;
  1055. if (h->frame_num_offset == 0 ||
  1056. h->frame_num_offset >= h->prev_frame_num_offset) {
  1057. av_log(h->avctx, AV_LOG_ERROR, "error in B-frame picture id\n");
  1058. return -1;
  1059. }
  1060. } else {
  1061. h->prev_frame_num = h->frame_num;
  1062. h->frame_num = h->slice_num;
  1063. h->prev_frame_num_offset = h->frame_num - h->prev_frame_num;
  1064. if (h->prev_frame_num_offset < 0)
  1065. h->prev_frame_num_offset += 256;
  1066. }
  1067. for (m = 0; m < 2; m++) {
  1068. int i;
  1069. for (i = 0; i < 4; i++) {
  1070. int j;
  1071. for (j = -1; j < 4; j++)
  1072. h->ref_cache[m][scan8[0] + 8 * i + j] = 1;
  1073. if (i < 3)
  1074. h->ref_cache[m][scan8[0] + 8 * i + j] = PART_NOT_AVAILABLE;
  1075. }
  1076. }
  1077. for (h->mb_y = 0; h->mb_y < h->mb_height; h->mb_y++) {
  1078. for (h->mb_x = 0; h->mb_x < h->mb_width; h->mb_x++) {
  1079. unsigned mb_type;
  1080. h->mb_xy = h->mb_x + h->mb_y * h->mb_stride;
  1081. if ((get_bits_count(&h->gb) + 7) >= h->gb.size_in_bits &&
  1082. ((get_bits_count(&h->gb) & 7) == 0 ||
  1083. show_bits(&h->gb, -get_bits_count(&h->gb) & 7) == 0)) {
  1084. skip_bits(&h->gb, s->next_slice_index - get_bits_count(&h->gb));
  1085. h->gb.size_in_bits = 8 * buf_size;
  1086. if (svq3_decode_slice_header(avctx))
  1087. return -1;
  1088. /* TODO: support s->mb_skip_run */
  1089. }
  1090. mb_type = svq3_get_ue_golomb(&h->gb);
  1091. if (h->pict_type == AV_PICTURE_TYPE_I)
  1092. mb_type += 8;
  1093. else if (h->pict_type == AV_PICTURE_TYPE_B && mb_type >= 4)
  1094. mb_type += 4;
  1095. if (mb_type > 33 || svq3_decode_mb(s, mb_type)) {
  1096. av_log(h->avctx, AV_LOG_ERROR,
  1097. "error while decoding MB %d %d\n", h->mb_x, h->mb_y);
  1098. return -1;
  1099. }
  1100. if (mb_type != 0 || h->cbp)
  1101. ff_h264_hl_decode_mb(h);
  1102. if (h->pict_type != AV_PICTURE_TYPE_B && !h->low_delay)
  1103. h->cur_pic.mb_type[h->mb_x + h->mb_y * h->mb_stride] =
  1104. (h->pict_type == AV_PICTURE_TYPE_P && mb_type < 8) ? (mb_type - 1) : -1;
  1105. }
  1106. ff_draw_horiz_band(avctx, NULL, s->cur_pic, s->last_pic->f.data[0] ? s->last_pic : NULL,
  1107. 16 * h->mb_y, 16, h->picture_structure, 0, 0,
  1108. h->low_delay, h->mb_height * 16, h->mb_width * 16);
  1109. }
  1110. left = buf_size*8 - get_bits_count(&h->gb);
  1111. if (h->mb_y != h->mb_height || h->mb_x != h->mb_width) {
  1112. 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);
  1113. //av_hex_dump(stderr, buf+buf_size-8, 8);
  1114. }
  1115. if (left < 0) {
  1116. av_log(avctx, AV_LOG_ERROR, "frame num %d left %d\n", avctx->frame_number, left);
  1117. return -1;
  1118. }
  1119. if (h->pict_type == AV_PICTURE_TYPE_B || h->low_delay)
  1120. ret = av_frame_ref(data, &s->cur_pic->f);
  1121. else if (s->last_pic->f.data[0])
  1122. ret = av_frame_ref(data, &s->last_pic->f);
  1123. if (ret < 0)
  1124. return ret;
  1125. /* Do not output the last pic after seeking. */
  1126. if (s->last_pic->f.data[0] || h->low_delay)
  1127. *got_frame = 1;
  1128. if (h->pict_type != AV_PICTURE_TYPE_B) {
  1129. FFSWAP(Picture*, s->cur_pic, s->next_pic);
  1130. } else {
  1131. av_frame_unref(&s->cur_pic->f);
  1132. }
  1133. return buf_size;
  1134. }
  1135. static av_cold int svq3_decode_end(AVCodecContext *avctx)
  1136. {
  1137. SVQ3Context *s = avctx->priv_data;
  1138. H264Context *h = &s->h;
  1139. free_picture(avctx, s->cur_pic);
  1140. free_picture(avctx, s->next_pic);
  1141. free_picture(avctx, s->last_pic);
  1142. av_freep(&s->cur_pic);
  1143. av_freep(&s->next_pic);
  1144. av_freep(&s->last_pic);
  1145. av_frame_unref(&h->cur_pic.f);
  1146. ff_h264_free_context(h);
  1147. av_freep(&s->buf);
  1148. s->buf_size = 0;
  1149. av_freep(&h->edge_emu_buffer);
  1150. return 0;
  1151. }
  1152. AVCodec ff_svq3_decoder = {
  1153. .name = "svq3",
  1154. .long_name = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 3 / Sorenson Video 3 / SVQ3"),
  1155. .type = AVMEDIA_TYPE_VIDEO,
  1156. .id = AV_CODEC_ID_SVQ3,
  1157. .priv_data_size = sizeof(SVQ3Context),
  1158. .init = svq3_decode_init,
  1159. .close = svq3_decode_end,
  1160. .decode = svq3_decode_frame,
  1161. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND |
  1162. CODEC_CAP_DR1 |
  1163. CODEC_CAP_DELAY,
  1164. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P,
  1165. AV_PIX_FMT_NONE},
  1166. };