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.

1345 lines
48KB

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