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.

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