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.

1675 lines
59KB

  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 "h264_mvpred.h"
  48. #include "h264data.h"
  49. #include "golomb.h"
  50. #include "hpeldsp.h"
  51. #include "mathops.h"
  52. #include "rectangle.h"
  53. #include "tpeldsp.h"
  54. #include "vdpau_internal.h"
  55. #if CONFIG_ZLIB
  56. #include <zlib.h>
  57. #endif
  58. #include "svq1.h"
  59. /**
  60. * @file
  61. * svq3 decoder.
  62. */
  63. typedef struct SVQ3Context {
  64. H264Context h;
  65. H264DSPContext h264dsp;
  66. H264PredContext hpc;
  67. HpelDSPContext hdsp;
  68. TpelDSPContext tdsp;
  69. VideoDSPContext vdsp;
  70. H264Picture *cur_pic;
  71. H264Picture *next_pic;
  72. H264Picture *last_pic;
  73. GetBitContext gb;
  74. uint8_t *slice_buf;
  75. int slice_size;
  76. int halfpel_flag;
  77. int thirdpel_flag;
  78. int has_watermark;
  79. uint32_t watermark_key;
  80. uint8_t *buf;
  81. int buf_size;
  82. int adaptive_quant;
  83. int next_p_frame_damaged;
  84. int h_edge_pos;
  85. int v_edge_pos;
  86. int last_frame_output;
  87. enum AVPictureType pict_type;
  88. int mb_x, mb_y;
  89. int mb_xy;
  90. int mb_width, mb_height;
  91. int mb_stride, mb_num;
  92. int b_stride;
  93. uint32_t *mb2br_xy;
  94. int chroma_pred_mode;
  95. int intra16x16_pred_mode;
  96. int8_t intra4x4_pred_mode_cache[5 * 8];
  97. int8_t (*intra4x4_pred_mode);
  98. unsigned int top_samples_available;
  99. unsigned int topright_samples_available;
  100. unsigned int left_samples_available;
  101. uint8_t *edge_emu_buffer;
  102. DECLARE_ALIGNED(16, int16_t, mv_cache)[2][5 * 8][2];
  103. DECLARE_ALIGNED(8, int8_t, ref_cache)[2][5 * 8];
  104. uint32_t dequant4_coeff[QP_MAX_NUM + 1][16];
  105. } SVQ3Context;
  106. #define FULLPEL_MODE 1
  107. #define HALFPEL_MODE 2
  108. #define THIRDPEL_MODE 3
  109. #define PREDICT_MODE 4
  110. /* dual scan (from some older h264 draft)
  111. * o-->o-->o o
  112. * | /|
  113. * o o o / o
  114. * | / | |/ |
  115. * o o o o
  116. * /
  117. * o-->o-->o-->o
  118. */
  119. static const uint8_t svq3_scan[16] = {
  120. 0 + 0 * 4, 1 + 0 * 4, 2 + 0 * 4, 2 + 1 * 4,
  121. 2 + 2 * 4, 3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4,
  122. 0 + 1 * 4, 0 + 2 * 4, 1 + 1 * 4, 1 + 2 * 4,
  123. 0 + 3 * 4, 1 + 3 * 4, 2 + 3 * 4, 3 + 3 * 4,
  124. };
  125. static const uint8_t luma_dc_zigzag_scan[16] = {
  126. 0 * 16 + 0 * 64, 1 * 16 + 0 * 64, 2 * 16 + 0 * 64, 0 * 16 + 2 * 64,
  127. 3 * 16 + 0 * 64, 0 * 16 + 1 * 64, 1 * 16 + 1 * 64, 2 * 16 + 1 * 64,
  128. 1 * 16 + 2 * 64, 2 * 16 + 2 * 64, 3 * 16 + 2 * 64, 0 * 16 + 3 * 64,
  129. 3 * 16 + 1 * 64, 1 * 16 + 3 * 64, 2 * 16 + 3 * 64, 3 * 16 + 3 * 64,
  130. };
  131. static const uint8_t svq3_pred_0[25][2] = {
  132. { 0, 0 },
  133. { 1, 0 }, { 0, 1 },
  134. { 0, 2 }, { 1, 1 }, { 2, 0 },
  135. { 3, 0 }, { 2, 1 }, { 1, 2 }, { 0, 3 },
  136. { 0, 4 }, { 1, 3 }, { 2, 2 }, { 3, 1 }, { 4, 0 },
  137. { 4, 1 }, { 3, 2 }, { 2, 3 }, { 1, 4 },
  138. { 2, 4 }, { 3, 3 }, { 4, 2 },
  139. { 4, 3 }, { 3, 4 },
  140. { 4, 4 }
  141. };
  142. static const int8_t svq3_pred_1[6][6][5] = {
  143. { { 2, -1, -1, -1, -1 }, { 2, 1, -1, -1, -1 }, { 1, 2, -1, -1, -1 },
  144. { 2, 1, -1, -1, -1 }, { 1, 2, -1, -1, -1 }, { 1, 2, -1, -1, -1 } },
  145. { { 0, 2, -1, -1, -1 }, { 0, 2, 1, 4, 3 }, { 0, 1, 2, 4, 3 },
  146. { 0, 2, 1, 4, 3 }, { 2, 0, 1, 3, 4 }, { 0, 4, 2, 1, 3 } },
  147. { { 2, 0, -1, -1, -1 }, { 2, 1, 0, 4, 3 }, { 1, 2, 4, 0, 3 },
  148. { 2, 1, 0, 4, 3 }, { 2, 1, 4, 3, 0 }, { 1, 2, 4, 0, 3 } },
  149. { { 2, 0, -1, -1, -1 }, { 2, 0, 1, 4, 3 }, { 1, 2, 0, 4, 3 },
  150. { 2, 1, 0, 4, 3 }, { 2, 1, 3, 4, 0 }, { 2, 4, 1, 0, 3 } },
  151. { { 0, 2, -1, -1, -1 }, { 0, 2, 1, 3, 4 }, { 1, 2, 3, 0, 4 },
  152. { 2, 0, 1, 3, 4 }, { 2, 1, 3, 0, 4 }, { 2, 0, 4, 3, 1 } },
  153. { { 0, 2, -1, -1, -1 }, { 0, 2, 4, 1, 3 }, { 1, 4, 2, 0, 3 },
  154. { 4, 2, 0, 1, 3 }, { 2, 0, 1, 4, 3 }, { 4, 2, 1, 0, 3 } },
  155. };
  156. static const struct {
  157. uint8_t run;
  158. uint8_t level;
  159. } svq3_dct_tables[2][16] = {
  160. { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 2, 1 }, { 0, 2 }, { 3, 1 }, { 4, 1 }, { 5, 1 },
  161. { 0, 3 }, { 1, 2 }, { 2, 2 }, { 6, 1 }, { 7, 1 }, { 8, 1 }, { 9, 1 }, { 0, 4 } },
  162. { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 0, 2 }, { 2, 1 }, { 0, 3 }, { 0, 4 }, { 0, 5 },
  163. { 3, 1 }, { 4, 1 }, { 1, 2 }, { 1, 3 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, { 0, 9 } }
  164. };
  165. static const uint32_t svq3_dequant_coeff[32] = {
  166. 3881, 4351, 4890, 5481, 6154, 6914, 7761, 8718,
  167. 9781, 10987, 12339, 13828, 15523, 17435, 19561, 21873,
  168. 24552, 27656, 30847, 34870, 38807, 43747, 49103, 54683,
  169. 61694, 68745, 77615, 89113, 100253, 109366, 126635, 141533
  170. };
  171. static int svq3_decode_end(AVCodecContext *avctx);
  172. static void svq3_luma_dc_dequant_idct_c(int16_t *output, int16_t *input, int qp)
  173. {
  174. const int qmul = svq3_dequant_coeff[qp];
  175. #define stride 16
  176. int i;
  177. int temp[16];
  178. static const uint8_t x_offset[4] = { 0, 1 * stride, 4 * stride, 5 * stride };
  179. for (i = 0; i < 4; i++) {
  180. const int z0 = 13 * (input[4 * i + 0] + input[4 * i + 2]);
  181. const int z1 = 13 * (input[4 * i + 0] - input[4 * i + 2]);
  182. const int z2 = 7 * input[4 * i + 1] - 17 * input[4 * i + 3];
  183. const int z3 = 17 * input[4 * i + 1] + 7 * input[4 * i + 3];
  184. temp[4 * i + 0] = z0 + z3;
  185. temp[4 * i + 1] = z1 + z2;
  186. temp[4 * i + 2] = z1 - z2;
  187. temp[4 * i + 3] = z0 - z3;
  188. }
  189. for (i = 0; i < 4; i++) {
  190. const int offset = x_offset[i];
  191. const int z0 = 13 * (temp[4 * 0 + i] + temp[4 * 2 + i]);
  192. const int z1 = 13 * (temp[4 * 0 + i] - temp[4 * 2 + i]);
  193. const int z2 = 7 * temp[4 * 1 + i] - 17 * temp[4 * 3 + i];
  194. const int z3 = 17 * temp[4 * 1 + i] + 7 * temp[4 * 3 + i];
  195. output[stride * 0 + offset] = (z0 + z3) * qmul + 0x80000 >> 20;
  196. output[stride * 2 + offset] = (z1 + z2) * qmul + 0x80000 >> 20;
  197. output[stride * 8 + offset] = (z1 - z2) * qmul + 0x80000 >> 20;
  198. output[stride * 10 + offset] = (z0 - z3) * qmul + 0x80000 >> 20;
  199. }
  200. }
  201. #undef stride
  202. static void svq3_add_idct_c(uint8_t *dst, int16_t *block,
  203. int stride, int qp, int dc)
  204. {
  205. const int qmul = svq3_dequant_coeff[qp];
  206. int i;
  207. if (dc) {
  208. dc = 13 * 13 * (dc == 1 ? 1538 * block[0]
  209. : qmul * (block[0] >> 3) / 2);
  210. block[0] = 0;
  211. }
  212. for (i = 0; i < 4; i++) {
  213. const int z0 = 13 * (block[0 + 4 * i] + block[2 + 4 * i]);
  214. const int z1 = 13 * (block[0 + 4 * i] - block[2 + 4 * i]);
  215. const int z2 = 7 * block[1 + 4 * i] - 17 * block[3 + 4 * i];
  216. const int z3 = 17 * block[1 + 4 * i] + 7 * block[3 + 4 * i];
  217. block[0 + 4 * i] = z0 + z3;
  218. block[1 + 4 * i] = z1 + z2;
  219. block[2 + 4 * i] = z1 - z2;
  220. block[3 + 4 * i] = z0 - z3;
  221. }
  222. for (i = 0; i < 4; i++) {
  223. const int z0 = 13 * (block[i + 4 * 0] + block[i + 4 * 2]);
  224. const int z1 = 13 * (block[i + 4 * 0] - block[i + 4 * 2]);
  225. const int z2 = 7 * block[i + 4 * 1] - 17 * block[i + 4 * 3];
  226. const int z3 = 17 * block[i + 4 * 1] + 7 * block[i + 4 * 3];
  227. const int rr = (dc + 0x80000);
  228. dst[i + stride * 0] = av_clip_uint8(dst[i + stride * 0] + ((z0 + z3) * qmul + rr >> 20));
  229. dst[i + stride * 1] = av_clip_uint8(dst[i + stride * 1] + ((z1 + z2) * qmul + rr >> 20));
  230. dst[i + stride * 2] = av_clip_uint8(dst[i + stride * 2] + ((z1 - z2) * qmul + rr >> 20));
  231. dst[i + stride * 3] = av_clip_uint8(dst[i + stride * 3] + ((z0 - z3) * qmul + rr >> 20));
  232. }
  233. memset(block, 0, 16 * sizeof(int16_t));
  234. }
  235. static inline int svq3_decode_block(GetBitContext *gb, int16_t *block,
  236. int index, const int type)
  237. {
  238. static const uint8_t *const scan_patterns[4] = {
  239. luma_dc_zigzag_scan, ff_zigzag_scan, svq3_scan, ff_h264_chroma_dc_scan
  240. };
  241. int run, level, sign, limit;
  242. unsigned vlc;
  243. const int intra = 3 * type >> 2;
  244. const uint8_t *const scan = scan_patterns[type];
  245. for (limit = (16 >> intra); index < 16; index = limit, limit += 8) {
  246. for (; (vlc = svq3_get_ue_golomb(gb)) != 0; index++) {
  247. if ((int32_t)vlc < 0)
  248. return -1;
  249. sign = (vlc & 1) ? 0 : -1;
  250. vlc = vlc + 1 >> 1;
  251. if (type == 3) {
  252. if (vlc < 3) {
  253. run = 0;
  254. level = vlc;
  255. } else if (vlc < 4) {
  256. run = 1;
  257. level = 1;
  258. } else {
  259. run = vlc & 0x3;
  260. level = (vlc + 9 >> 2) - run;
  261. }
  262. } else {
  263. if (vlc < 16U) {
  264. run = svq3_dct_tables[intra][vlc].run;
  265. level = svq3_dct_tables[intra][vlc].level;
  266. } else if (intra) {
  267. run = vlc & 0x7;
  268. level = (vlc >> 3) + ((run == 0) ? 8 : ((run < 2) ? 2 : ((run < 5) ? 0 : -1)));
  269. } else {
  270. run = vlc & 0xF;
  271. level = (vlc >> 4) + ((run == 0) ? 4 : ((run < 3) ? 2 : ((run < 10) ? 1 : 0)));
  272. }
  273. }
  274. if ((index += run) >= limit)
  275. return -1;
  276. block[scan[index]] = (level ^ sign) - sign;
  277. }
  278. if (type != 2) {
  279. break;
  280. }
  281. }
  282. return 0;
  283. }
  284. static av_always_inline int
  285. svq3_fetch_diagonal_mv(const SVQ3Context *s, const int16_t **C,
  286. int i, int list, int part_width)
  287. {
  288. const int topright_ref = s->ref_cache[list][i - 8 + part_width];
  289. if (topright_ref != PART_NOT_AVAILABLE) {
  290. *C = s->mv_cache[list][i - 8 + part_width];
  291. return topright_ref;
  292. } else {
  293. *C = s->mv_cache[list][i - 8 - 1];
  294. return s->ref_cache[list][i - 8 - 1];
  295. }
  296. }
  297. /**
  298. * Get the predicted MV.
  299. * @param n the block index
  300. * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
  301. * @param mx the x component of the predicted motion vector
  302. * @param my the y component of the predicted motion vector
  303. */
  304. static av_always_inline void svq3_pred_motion(const SVQ3Context *s, int n,
  305. int part_width, int list,
  306. int ref, int *const mx, int *const my)
  307. {
  308. const int index8 = scan8[n];
  309. const int top_ref = s->ref_cache[list][index8 - 8];
  310. const int left_ref = s->ref_cache[list][index8 - 1];
  311. const int16_t *const A = s->mv_cache[list][index8 - 1];
  312. const int16_t *const B = s->mv_cache[list][index8 - 8];
  313. const int16_t *C;
  314. int diagonal_ref, match_count;
  315. /* mv_cache
  316. * B . . A T T T T
  317. * U . . L . . , .
  318. * U . . L . . . .
  319. * U . . L . . , .
  320. * . . . L . . . .
  321. */
  322. diagonal_ref = svq3_fetch_diagonal_mv(s, &C, index8, list, part_width);
  323. match_count = (diagonal_ref == ref) + (top_ref == ref) + (left_ref == ref);
  324. if (match_count > 1) { //most common
  325. *mx = mid_pred(A[0], B[0], C[0]);
  326. *my = mid_pred(A[1], B[1], C[1]);
  327. } else if (match_count == 1) {
  328. if (left_ref == ref) {
  329. *mx = A[0];
  330. *my = A[1];
  331. } else if (top_ref == ref) {
  332. *mx = B[0];
  333. *my = B[1];
  334. } else {
  335. *mx = C[0];
  336. *my = C[1];
  337. }
  338. } else {
  339. if (top_ref == PART_NOT_AVAILABLE &&
  340. diagonal_ref == PART_NOT_AVAILABLE &&
  341. left_ref != PART_NOT_AVAILABLE) {
  342. *mx = A[0];
  343. *my = A[1];
  344. } else {
  345. *mx = mid_pred(A[0], B[0], C[0]);
  346. *my = mid_pred(A[1], B[1], C[1]);
  347. }
  348. }
  349. }
  350. static inline void svq3_mc_dir_part(SVQ3Context *s,
  351. int x, int y, int width, int height,
  352. int mx, int my, int dxy,
  353. int thirdpel, int dir, int avg)
  354. {
  355. H264Context *h = &s->h;
  356. H264SliceContext *sl = &h->slice_ctx[0];
  357. const H264Picture *pic = (dir == 0) ? s->last_pic : s->next_pic;
  358. uint8_t *src, *dest;
  359. int i, emu = 0;
  360. int blocksize = 2 - (width >> 3); // 16->0, 8->1, 4->2
  361. mx += x;
  362. my += y;
  363. if (mx < 0 || mx >= s->h_edge_pos - width - 1 ||
  364. my < 0 || my >= s->v_edge_pos - height - 1) {
  365. emu = 1;
  366. mx = av_clip(mx, -16, s->h_edge_pos - width + 15);
  367. my = av_clip(my, -16, s->v_edge_pos - height + 15);
  368. }
  369. /* form component predictions */
  370. dest = h->cur_pic.f->data[0] + x + y * sl->linesize;
  371. src = pic->f->data[0] + mx + my * sl->linesize;
  372. if (emu) {
  373. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src,
  374. sl->linesize, sl->linesize,
  375. width + 1, height + 1,
  376. mx, my, s->h_edge_pos, s->v_edge_pos);
  377. src = s->edge_emu_buffer;
  378. }
  379. if (thirdpel)
  380. (avg ? s->tdsp.avg_tpel_pixels_tab
  381. : s->tdsp.put_tpel_pixels_tab)[dxy](dest, src, sl->linesize,
  382. width, height);
  383. else
  384. (avg ? s->hdsp.avg_pixels_tab
  385. : s->hdsp.put_pixels_tab)[blocksize][dxy](dest, src, sl->linesize,
  386. height);
  387. if (!(h->flags & AV_CODEC_FLAG_GRAY)) {
  388. mx = mx + (mx < (int) x) >> 1;
  389. my = my + (my < (int) y) >> 1;
  390. width = width >> 1;
  391. height = height >> 1;
  392. blocksize++;
  393. for (i = 1; i < 3; i++) {
  394. dest = h->cur_pic.f->data[i] + (x >> 1) + (y >> 1) * sl->uvlinesize;
  395. src = pic->f->data[i] + mx + my * sl->uvlinesize;
  396. if (emu) {
  397. s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src,
  398. sl->uvlinesize, sl->uvlinesize,
  399. width + 1, height + 1,
  400. mx, my, (s->h_edge_pos >> 1),
  401. s->v_edge_pos >> 1);
  402. src = s->edge_emu_buffer;
  403. }
  404. if (thirdpel)
  405. (avg ? s->tdsp.avg_tpel_pixels_tab
  406. : s->tdsp.put_tpel_pixels_tab)[dxy](dest, src,
  407. sl->uvlinesize,
  408. width, height);
  409. else
  410. (avg ? s->hdsp.avg_pixels_tab
  411. : s->hdsp.put_pixels_tab)[blocksize][dxy](dest, src,
  412. sl->uvlinesize,
  413. height);
  414. }
  415. }
  416. }
  417. static inline int svq3_mc_dir(SVQ3Context *s, int size, int mode,
  418. int dir, int avg)
  419. {
  420. int i, j, k, mx, my, dx, dy, x, y;
  421. H264Context *h = &s->h;
  422. const int part_width = ((size & 5) == 4) ? 4 : 16 >> (size & 1);
  423. const int part_height = 16 >> ((unsigned)(size + 1) / 3);
  424. const int extra_width = (mode == PREDICT_MODE) ? -16 * 6 : 0;
  425. const int h_edge_pos = 6 * (s->h_edge_pos - part_width) - extra_width;
  426. const int v_edge_pos = 6 * (s->v_edge_pos - part_height) - extra_width;
  427. for (i = 0; i < 16; i += part_height)
  428. for (j = 0; j < 16; j += part_width) {
  429. const int b_xy = (4 * s->mb_x + (j >> 2)) +
  430. (4 * s->mb_y + (i >> 2)) * s->b_stride;
  431. int dxy;
  432. x = 16 * s->mb_x + j;
  433. y = 16 * s->mb_y + i;
  434. k = (j >> 2 & 1) + (i >> 1 & 2) +
  435. (j >> 1 & 4) + (i & 8);
  436. if (mode != PREDICT_MODE) {
  437. svq3_pred_motion(s, k, part_width >> 2, dir, 1, &mx, &my);
  438. } else {
  439. mx = s->next_pic->motion_val[0][b_xy][0] << 1;
  440. my = s->next_pic->motion_val[0][b_xy][1] << 1;
  441. if (dir == 0) {
  442. mx = mx * h->frame_num_offset /
  443. h->prev_frame_num_offset + 1 >> 1;
  444. my = my * h->frame_num_offset /
  445. h->prev_frame_num_offset + 1 >> 1;
  446. } else {
  447. mx = mx * (h->frame_num_offset - h->prev_frame_num_offset) /
  448. h->prev_frame_num_offset + 1 >> 1;
  449. my = my * (h->frame_num_offset - h->prev_frame_num_offset) /
  450. h->prev_frame_num_offset + 1 >> 1;
  451. }
  452. }
  453. /* clip motion vector prediction to frame border */
  454. mx = av_clip(mx, extra_width - 6 * x, h_edge_pos - 6 * x);
  455. my = av_clip(my, extra_width - 6 * y, v_edge_pos - 6 * y);
  456. /* get (optional) motion vector differential */
  457. if (mode == PREDICT_MODE) {
  458. dx = dy = 0;
  459. } else {
  460. dy = svq3_get_se_golomb(&h->gb);
  461. dx = svq3_get_se_golomb(&h->gb);
  462. if (dx == INVALID_VLC || dy == INVALID_VLC) {
  463. av_log(h->avctx, AV_LOG_ERROR, "invalid MV vlc\n");
  464. return -1;
  465. }
  466. }
  467. /* compute motion vector */
  468. if (mode == THIRDPEL_MODE) {
  469. int fx, fy;
  470. mx = (mx + 1 >> 1) + dx;
  471. my = (my + 1 >> 1) + dy;
  472. fx = (unsigned)(mx + 0x3000) / 3 - 0x1000;
  473. fy = (unsigned)(my + 0x3000) / 3 - 0x1000;
  474. dxy = (mx - 3 * fx) + 4 * (my - 3 * fy);
  475. svq3_mc_dir_part(s, x, y, part_width, part_height,
  476. fx, fy, dxy, 1, dir, avg);
  477. mx += mx;
  478. my += my;
  479. } else if (mode == HALFPEL_MODE || mode == PREDICT_MODE) {
  480. mx = (unsigned)(mx + 1 + 0x3000) / 3 + dx - 0x1000;
  481. my = (unsigned)(my + 1 + 0x3000) / 3 + dy - 0x1000;
  482. dxy = (mx & 1) + 2 * (my & 1);
  483. svq3_mc_dir_part(s, x, y, part_width, part_height,
  484. mx >> 1, my >> 1, dxy, 0, dir, avg);
  485. mx *= 3;
  486. my *= 3;
  487. } else {
  488. mx = (unsigned)(mx + 3 + 0x6000) / 6 + dx - 0x1000;
  489. my = (unsigned)(my + 3 + 0x6000) / 6 + dy - 0x1000;
  490. svq3_mc_dir_part(s, x, y, part_width, part_height,
  491. mx, my, 0, 0, dir, avg);
  492. mx *= 6;
  493. my *= 6;
  494. }
  495. /* update mv_cache */
  496. if (mode != PREDICT_MODE) {
  497. int32_t mv = pack16to32(mx, my);
  498. if (part_height == 8 && i < 8) {
  499. AV_WN32A(s->mv_cache[dir][scan8[k] + 1 * 8], mv);
  500. if (part_width == 8 && j < 8)
  501. AV_WN32A(s->mv_cache[dir][scan8[k] + 1 + 1 * 8], mv);
  502. }
  503. if (part_width == 8 && j < 8)
  504. AV_WN32A(s->mv_cache[dir][scan8[k] + 1], mv);
  505. if (part_width == 4 || part_height == 4)
  506. AV_WN32A(s->mv_cache[dir][scan8[k]], mv);
  507. }
  508. /* write back motion vectors */
  509. fill_rectangle(h->cur_pic.motion_val[dir][b_xy],
  510. part_width >> 2, part_height >> 2, s->b_stride,
  511. pack16to32(mx, my), 4);
  512. }
  513. return 0;
  514. }
  515. static av_always_inline void hl_decode_mb_idct_luma(const H264Context *h, H264SliceContext *sl,
  516. int mb_type, const int *block_offset,
  517. int linesize, uint8_t *dest_y)
  518. {
  519. int i;
  520. if (!IS_INTRA4x4(mb_type)) {
  521. for (i = 0; i < 16; i++)
  522. if (sl->non_zero_count_cache[scan8[i]] || sl->mb[i * 16]) {
  523. uint8_t *const ptr = dest_y + block_offset[i];
  524. svq3_add_idct_c(ptr, sl->mb + i * 16, linesize,
  525. sl->qscale, IS_INTRA(mb_type) ? 1 : 0);
  526. }
  527. }
  528. }
  529. static av_always_inline int dctcoef_get(int16_t *mb, int index)
  530. {
  531. return AV_RN16A(mb + index);
  532. }
  533. static av_always_inline void hl_decode_mb_predict_luma(SVQ3Context *s,
  534. const H264Context *h,
  535. H264SliceContext *sl,
  536. int mb_type,
  537. const int *block_offset,
  538. int linesize,
  539. uint8_t *dest_y)
  540. {
  541. int i;
  542. int qscale = sl->qscale;
  543. if (IS_INTRA4x4(mb_type)) {
  544. for (i = 0; i < 16; i++) {
  545. uint8_t *const ptr = dest_y + block_offset[i];
  546. const int dir = s->intra4x4_pred_mode_cache[scan8[i]];
  547. uint8_t *topright;
  548. int nnz, tr;
  549. if (dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED) {
  550. const int topright_avail = (s->topright_samples_available << i) & 0x8000;
  551. av_assert2(s->mb_y || linesize <= block_offset[i]);
  552. if (!topright_avail) {
  553. tr = ptr[3 - linesize] * 0x01010101u;
  554. topright = (uint8_t *)&tr;
  555. } else
  556. topright = ptr + 4 - linesize;
  557. } else
  558. topright = NULL;
  559. s->hpc.pred4x4[dir](ptr, topright, linesize);
  560. nnz = sl->non_zero_count_cache[scan8[i]];
  561. if (nnz) {
  562. svq3_add_idct_c(ptr, sl->mb + i * 16, linesize, qscale, 0);
  563. }
  564. }
  565. } else {
  566. s->hpc.pred16x16[s->intra16x16_pred_mode](dest_y, linesize);
  567. svq3_luma_dc_dequant_idct_c(sl->mb, sl->mb_luma_dc[0], qscale);
  568. }
  569. }
  570. static void hl_decode_mb(SVQ3Context *s, const H264Context *h, H264SliceContext *sl)
  571. {
  572. const int mb_x = s->mb_x;
  573. const int mb_y = s->mb_y;
  574. const int mb_xy = s->mb_xy;
  575. const int mb_type = h->cur_pic.mb_type[mb_xy];
  576. uint8_t *dest_y, *dest_cb, *dest_cr;
  577. int linesize, uvlinesize;
  578. int i, j;
  579. const int *block_offset = &h->block_offset[0];
  580. const int block_h = 16 >> h->chroma_y_shift;
  581. dest_y = h->cur_pic.f->data[0] + (mb_x + mb_y * sl->linesize) * 16;
  582. dest_cb = h->cur_pic.f->data[1] + mb_x * 8 + mb_y * sl->uvlinesize * block_h;
  583. dest_cr = h->cur_pic.f->data[2] + mb_x * 8 + mb_y * sl->uvlinesize * block_h;
  584. s->vdsp.prefetch(dest_y + (s->mb_x & 3) * 4 * sl->linesize + 64, sl->linesize, 4);
  585. s->vdsp.prefetch(dest_cb + (s->mb_x & 7) * sl->uvlinesize + 64, dest_cr - dest_cb, 2);
  586. linesize = sl->mb_linesize = sl->linesize;
  587. uvlinesize = sl->mb_uvlinesize = sl->uvlinesize;
  588. if (IS_INTRA(mb_type)) {
  589. s->hpc.pred8x8[s->chroma_pred_mode](dest_cb, uvlinesize);
  590. s->hpc.pred8x8[s->chroma_pred_mode](dest_cr, uvlinesize);
  591. hl_decode_mb_predict_luma(s, h, sl, mb_type, block_offset, linesize, dest_y);
  592. }
  593. hl_decode_mb_idct_luma(h, sl, mb_type, block_offset, linesize, dest_y);
  594. if (sl->cbp & 0x30) {
  595. uint8_t *dest[2] = { dest_cb, dest_cr };
  596. s->h264dsp.h264_chroma_dc_dequant_idct(sl->mb + 16 * 16 * 1,
  597. s->dequant4_coeff[sl->chroma_qp[0]][0]);
  598. s->h264dsp.h264_chroma_dc_dequant_idct(sl->mb + 16 * 16 * 2,
  599. s->dequant4_coeff[sl->chroma_qp[1]][0]);
  600. for (j = 1; j < 3; j++) {
  601. for (i = j * 16; i < j * 16 + 4; i++)
  602. if (sl->non_zero_count_cache[scan8[i]] || sl->mb[i * 16]) {
  603. uint8_t *const ptr = dest[j - 1] + block_offset[i];
  604. svq3_add_idct_c(ptr, sl->mb + i * 16,
  605. uvlinesize, ff_h264_chroma_qp[0][sl->qscale + 12] - 12, 2);
  606. }
  607. }
  608. }
  609. }
  610. static int svq3_decode_mb(SVQ3Context *s, unsigned int mb_type)
  611. {
  612. H264Context *h = &s->h;
  613. H264SliceContext *sl = &h->slice_ctx[0];
  614. int i, j, k, m, dir, mode;
  615. int cbp = 0;
  616. uint32_t vlc;
  617. int8_t *top, *left;
  618. const int mb_xy = s->mb_xy;
  619. const int b_xy = 4 * s->mb_x + 4 * s->mb_y * s->b_stride;
  620. s->top_samples_available = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
  621. s->left_samples_available = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
  622. s->topright_samples_available = 0xFFFF;
  623. if (mb_type == 0) { /* SKIP */
  624. if (s->pict_type == AV_PICTURE_TYPE_P ||
  625. s->next_pic->mb_type[mb_xy] == -1) {
  626. svq3_mc_dir_part(s, 16 * s->mb_x, 16 * s->mb_y, 16, 16,
  627. 0, 0, 0, 0, 0, 0);
  628. if (s->pict_type == AV_PICTURE_TYPE_B)
  629. svq3_mc_dir_part(s, 16 * s->mb_x, 16 * s->mb_y, 16, 16,
  630. 0, 0, 0, 0, 1, 1);
  631. mb_type = MB_TYPE_SKIP;
  632. } else {
  633. mb_type = FFMIN(s->next_pic->mb_type[mb_xy], 6);
  634. if (svq3_mc_dir(s, mb_type, PREDICT_MODE, 0, 0) < 0)
  635. return -1;
  636. if (svq3_mc_dir(s, mb_type, PREDICT_MODE, 1, 1) < 0)
  637. return -1;
  638. mb_type = MB_TYPE_16x16;
  639. }
  640. } else if (mb_type < 8) { /* INTER */
  641. if (s->thirdpel_flag && s->halfpel_flag == !get_bits1(&h->gb))
  642. mode = THIRDPEL_MODE;
  643. else if (s->halfpel_flag &&
  644. s->thirdpel_flag == !get_bits1(&h->gb))
  645. mode = HALFPEL_MODE;
  646. else
  647. mode = FULLPEL_MODE;
  648. /* fill caches */
  649. /* note ref_cache should contain here:
  650. * ????????
  651. * ???11111
  652. * N??11111
  653. * N??11111
  654. * N??11111
  655. */
  656. for (m = 0; m < 2; m++) {
  657. if (s->mb_x > 0 && s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - 1] + 6] != -1) {
  658. for (i = 0; i < 4; i++)
  659. AV_COPY32(s->mv_cache[m][scan8[0] - 1 + i * 8],
  660. h->cur_pic.motion_val[m][b_xy - 1 + i * s->b_stride]);
  661. } else {
  662. for (i = 0; i < 4; i++)
  663. AV_ZERO32(s->mv_cache[m][scan8[0] - 1 + i * 8]);
  664. }
  665. if (s->mb_y > 0) {
  666. memcpy(s->mv_cache[m][scan8[0] - 1 * 8],
  667. h->cur_pic.motion_val[m][b_xy - s->b_stride],
  668. 4 * 2 * sizeof(int16_t));
  669. memset(&s->ref_cache[m][scan8[0] - 1 * 8],
  670. (s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1, 4);
  671. if (s->mb_x < s->mb_width - 1) {
  672. AV_COPY32(s->mv_cache[m][scan8[0] + 4 - 1 * 8],
  673. h->cur_pic.motion_val[m][b_xy - s->b_stride + 4]);
  674. s->ref_cache[m][scan8[0] + 4 - 1 * 8] =
  675. (s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride + 1] + 6] == -1 ||
  676. s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1;
  677. } else
  678. s->ref_cache[m][scan8[0] + 4 - 1 * 8] = PART_NOT_AVAILABLE;
  679. if (s->mb_x > 0) {
  680. AV_COPY32(s->mv_cache[m][scan8[0] - 1 - 1 * 8],
  681. h->cur_pic.motion_val[m][b_xy - s->b_stride - 1]);
  682. s->ref_cache[m][scan8[0] - 1 - 1 * 8] =
  683. (s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride - 1] + 3] == -1) ? PART_NOT_AVAILABLE : 1;
  684. } else
  685. s->ref_cache[m][scan8[0] - 1 - 1 * 8] = PART_NOT_AVAILABLE;
  686. } else
  687. memset(&s->ref_cache[m][scan8[0] - 1 * 8 - 1],
  688. PART_NOT_AVAILABLE, 8);
  689. if (s->pict_type != AV_PICTURE_TYPE_B)
  690. break;
  691. }
  692. /* decode motion vector(s) and form prediction(s) */
  693. if (s->pict_type == AV_PICTURE_TYPE_P) {
  694. if (svq3_mc_dir(s, mb_type - 1, mode, 0, 0) < 0)
  695. return -1;
  696. } else { /* AV_PICTURE_TYPE_B */
  697. if (mb_type != 2) {
  698. if (svq3_mc_dir(s, 0, mode, 0, 0) < 0)
  699. return -1;
  700. } else {
  701. for (i = 0; i < 4; i++)
  702. memset(h->cur_pic.motion_val[0][b_xy + i * s->b_stride],
  703. 0, 4 * 2 * sizeof(int16_t));
  704. }
  705. if (mb_type != 1) {
  706. if (svq3_mc_dir(s, 0, mode, 1, mb_type == 3) < 0)
  707. return -1;
  708. } else {
  709. for (i = 0; i < 4; i++)
  710. memset(h->cur_pic.motion_val[1][b_xy + i * s->b_stride],
  711. 0, 4 * 2 * sizeof(int16_t));
  712. }
  713. }
  714. mb_type = MB_TYPE_16x16;
  715. } else if (mb_type == 8 || mb_type == 33) { /* INTRA4x4 */
  716. int8_t *i4x4 = s->intra4x4_pred_mode + s->mb2br_xy[s->mb_xy];
  717. int8_t *i4x4_cache = s->intra4x4_pred_mode_cache;
  718. memset(s->intra4x4_pred_mode_cache, -1, 8 * 5 * sizeof(int8_t));
  719. if (mb_type == 8) {
  720. if (s->mb_x > 0) {
  721. for (i = 0; i < 4; i++)
  722. s->intra4x4_pred_mode_cache[scan8[0] - 1 + i * 8] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - 1] + 6 - i];
  723. if (s->intra4x4_pred_mode_cache[scan8[0] - 1] == -1)
  724. s->left_samples_available = 0x5F5F;
  725. }
  726. if (s->mb_y > 0) {
  727. s->intra4x4_pred_mode_cache[4 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 0];
  728. s->intra4x4_pred_mode_cache[5 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 1];
  729. s->intra4x4_pred_mode_cache[6 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 2];
  730. s->intra4x4_pred_mode_cache[7 + 8 * 0] = s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride] + 3];
  731. if (s->intra4x4_pred_mode_cache[4 + 8 * 0] == -1)
  732. s->top_samples_available = 0x33FF;
  733. }
  734. /* decode prediction codes for luma blocks */
  735. for (i = 0; i < 16; i += 2) {
  736. vlc = svq3_get_ue_golomb(&h->gb);
  737. if (vlc >= 25U) {
  738. av_log(h->avctx, AV_LOG_ERROR,
  739. "luma prediction:%"PRIu32"\n", vlc);
  740. return -1;
  741. }
  742. left = &s->intra4x4_pred_mode_cache[scan8[i] - 1];
  743. top = &s->intra4x4_pred_mode_cache[scan8[i] - 8];
  744. left[1] = svq3_pred_1[top[0] + 1][left[0] + 1][svq3_pred_0[vlc][0]];
  745. left[2] = svq3_pred_1[top[1] + 1][left[1] + 1][svq3_pred_0[vlc][1]];
  746. if (left[1] == -1 || left[2] == -1) {
  747. av_log(h->avctx, AV_LOG_ERROR, "weird prediction\n");
  748. return -1;
  749. }
  750. }
  751. } else { /* mb_type == 33, DC_128_PRED block type */
  752. for (i = 0; i < 4; i++)
  753. memset(&s->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_PRED, 4);
  754. }
  755. AV_COPY32(i4x4, i4x4_cache + 4 + 8 * 4);
  756. i4x4[4] = i4x4_cache[7 + 8 * 3];
  757. i4x4[5] = i4x4_cache[7 + 8 * 2];
  758. i4x4[6] = i4x4_cache[7 + 8 * 1];
  759. if (mb_type == 8) {
  760. ff_h264_check_intra4x4_pred_mode(s->intra4x4_pred_mode_cache,
  761. h->avctx, s->top_samples_available,
  762. s->left_samples_available);
  763. s->top_samples_available = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
  764. s->left_samples_available = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
  765. } else {
  766. for (i = 0; i < 4; i++)
  767. memset(&s->intra4x4_pred_mode_cache[scan8[0] + 8 * i], DC_128_PRED, 4);
  768. s->top_samples_available = 0x33FF;
  769. s->left_samples_available = 0x5F5F;
  770. }
  771. mb_type = MB_TYPE_INTRA4x4;
  772. } else { /* INTRA16x16 */
  773. dir = ff_h264_i_mb_type_info[mb_type - 8].pred_mode;
  774. dir = (dir >> 1) ^ 3 * (dir & 1) ^ 1;
  775. if ((s->intra16x16_pred_mode = ff_h264_check_intra_pred_mode(h->avctx, s->top_samples_available,
  776. s->left_samples_available, dir, 0)) < 0) {
  777. av_log(h->avctx, AV_LOG_ERROR, "ff_h264_check_intra_pred_mode < 0\n");
  778. return s->intra16x16_pred_mode;
  779. }
  780. cbp = ff_h264_i_mb_type_info[mb_type - 8].cbp;
  781. mb_type = MB_TYPE_INTRA16x16;
  782. }
  783. if (!IS_INTER(mb_type) && s->pict_type != AV_PICTURE_TYPE_I) {
  784. for (i = 0; i < 4; i++)
  785. memset(h->cur_pic.motion_val[0][b_xy + i * s->b_stride],
  786. 0, 4 * 2 * sizeof(int16_t));
  787. if (s->pict_type == AV_PICTURE_TYPE_B) {
  788. for (i = 0; i < 4; i++)
  789. memset(h->cur_pic.motion_val[1][b_xy + i * s->b_stride],
  790. 0, 4 * 2 * sizeof(int16_t));
  791. }
  792. }
  793. if (!IS_INTRA4x4(mb_type)) {
  794. memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy], DC_PRED, 8);
  795. }
  796. if (!IS_SKIP(mb_type) || s->pict_type == AV_PICTURE_TYPE_B) {
  797. memset(sl->non_zero_count_cache + 8, 0, 14 * 8 * sizeof(uint8_t));
  798. }
  799. if (!IS_INTRA16x16(mb_type) &&
  800. (!IS_SKIP(mb_type) || s->pict_type == AV_PICTURE_TYPE_B)) {
  801. if ((vlc = svq3_get_ue_golomb(&h->gb)) >= 48U){
  802. av_log(h->avctx, AV_LOG_ERROR, "cbp_vlc=%"PRIu32"\n", vlc);
  803. return -1;
  804. }
  805. cbp = IS_INTRA(mb_type) ? ff_h264_golomb_to_intra4x4_cbp[vlc]
  806. : ff_h264_golomb_to_inter_cbp[vlc];
  807. }
  808. if (IS_INTRA16x16(mb_type) ||
  809. (s->pict_type != AV_PICTURE_TYPE_I && s->adaptive_quant && cbp)) {
  810. sl->qscale += svq3_get_se_golomb(&h->gb);
  811. if (sl->qscale > 31u) {
  812. av_log(h->avctx, AV_LOG_ERROR, "qscale:%d\n", sl->qscale);
  813. return -1;
  814. }
  815. }
  816. if (IS_INTRA16x16(mb_type)) {
  817. AV_ZERO128(sl->mb_luma_dc[0] + 0);
  818. AV_ZERO128(sl->mb_luma_dc[0] + 8);
  819. if (svq3_decode_block(&h->gb, sl->mb_luma_dc[0], 0, 1)) {
  820. av_log(h->avctx, AV_LOG_ERROR,
  821. "error while decoding intra luma dc\n");
  822. return -1;
  823. }
  824. }
  825. if (cbp) {
  826. const int index = IS_INTRA16x16(mb_type) ? 1 : 0;
  827. const int type = ((sl->qscale < 24 && IS_INTRA4x4(mb_type)) ? 2 : 1);
  828. for (i = 0; i < 4; i++)
  829. if ((cbp & (1 << i))) {
  830. for (j = 0; j < 4; j++) {
  831. k = index ? (1 * (j & 1) + 2 * (i & 1) +
  832. 2 * (j & 2) + 4 * (i & 2))
  833. : (4 * i + j);
  834. sl->non_zero_count_cache[scan8[k]] = 1;
  835. if (svq3_decode_block(&h->gb, &sl->mb[16 * k], index, type)) {
  836. av_log(h->avctx, AV_LOG_ERROR,
  837. "error while decoding block\n");
  838. return -1;
  839. }
  840. }
  841. }
  842. if ((cbp & 0x30)) {
  843. for (i = 1; i < 3; ++i)
  844. if (svq3_decode_block(&h->gb, &sl->mb[16 * 16 * i], 0, 3)) {
  845. av_log(h->avctx, AV_LOG_ERROR,
  846. "error while decoding chroma dc block\n");
  847. return -1;
  848. }
  849. if ((cbp & 0x20)) {
  850. for (i = 1; i < 3; i++) {
  851. for (j = 0; j < 4; j++) {
  852. k = 16 * i + j;
  853. sl->non_zero_count_cache[scan8[k]] = 1;
  854. if (svq3_decode_block(&h->gb, &sl->mb[16 * k], 1, 1)) {
  855. av_log(h->avctx, AV_LOG_ERROR,
  856. "error while decoding chroma ac block\n");
  857. return -1;
  858. }
  859. }
  860. }
  861. }
  862. }
  863. }
  864. sl->cbp = cbp;
  865. h->cur_pic.mb_type[mb_xy] = mb_type;
  866. if (IS_INTRA(mb_type))
  867. s->chroma_pred_mode = ff_h264_check_intra_pred_mode(h->avctx, s->top_samples_available,
  868. s->left_samples_available, DC_PRED8x8, 1);
  869. return 0;
  870. }
  871. static int svq3_decode_slice_header(AVCodecContext *avctx)
  872. {
  873. SVQ3Context *s = avctx->priv_data;
  874. H264Context *h = &s->h;
  875. H264SliceContext *sl = &h->slice_ctx[0];
  876. const int mb_xy = s->mb_xy;
  877. int i, header;
  878. unsigned slice_id;
  879. header = get_bits(&s->gb, 8);
  880. if (((header & 0x9F) != 1 && (header & 0x9F) != 2) || (header & 0x60) == 0) {
  881. /* TODO: what? */
  882. av_log(avctx, AV_LOG_ERROR, "unsupported slice header (%02X)\n", header);
  883. return -1;
  884. } else {
  885. int slice_bits, slice_bytes, slice_length;
  886. int length = header >> 5 & 3;
  887. slice_length = show_bits(&s->gb, 8 * length);
  888. slice_bits = slice_length * 8;
  889. slice_bytes = slice_length + length - 1;
  890. if (slice_bytes > get_bits_left(&s->gb)) {
  891. av_log(avctx, AV_LOG_ERROR, "slice after bitstream end\n");
  892. return -1;
  893. }
  894. skip_bits(&s->gb, 8);
  895. av_fast_malloc(&s->slice_buf, &s->slice_size, slice_bytes + AV_INPUT_BUFFER_PADDING_SIZE);
  896. if (!s->slice_buf)
  897. return AVERROR(ENOMEM);
  898. memcpy(s->slice_buf, s->gb.buffer + s->gb.index / 8, slice_bytes);
  899. init_get_bits(&h->gb, s->slice_buf, slice_bits);
  900. if (s->watermark_key) {
  901. uint32_t header = AV_RL32(&h->gb.buffer[1]);
  902. AV_WL32(&h->gb.buffer[1], header ^ s->watermark_key);
  903. }
  904. if (length > 0) {
  905. memmove(s->slice_buf, &s->slice_buf[slice_length], length - 1);
  906. }
  907. skip_bits_long(&s->gb, slice_bytes * 8);
  908. }
  909. if ((slice_id = svq3_get_ue_golomb(&h->gb)) >= 3) {
  910. av_log(h->avctx, AV_LOG_ERROR, "illegal slice type %u \n", slice_id);
  911. return -1;
  912. }
  913. s->pict_type = ff_h264_golomb_to_pict_type[slice_id];
  914. if ((header & 0x9F) == 2) {
  915. i = (s->mb_num < 64) ? 6 : (1 + av_log2(s->mb_num - 1));
  916. sl->mb_skip_run = get_bits(&h->gb, i) -
  917. (s->mb_y * s->mb_width + s->mb_x);
  918. } else {
  919. skip_bits1(&h->gb);
  920. sl->mb_skip_run = 0;
  921. }
  922. sl->slice_num = get_bits(&h->gb, 8);
  923. sl->qscale = get_bits(&h->gb, 5);
  924. s->adaptive_quant = get_bits1(&h->gb);
  925. /* unknown fields */
  926. skip_bits1(&h->gb);
  927. if (s->has_watermark)
  928. skip_bits1(&h->gb);
  929. skip_bits1(&h->gb);
  930. skip_bits(&h->gb, 2);
  931. if (skip_1stop_8data_bits(&h->gb) < 0)
  932. return AVERROR_INVALIDDATA;
  933. /* reset intra predictors and invalidate motion vector references */
  934. if (s->mb_x > 0) {
  935. memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - 1] + 3,
  936. -1, 4 * sizeof(int8_t));
  937. memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - s->mb_x],
  938. -1, 8 * sizeof(int8_t) * s->mb_x);
  939. }
  940. if (s->mb_y > 0) {
  941. memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - s->mb_stride],
  942. -1, 8 * sizeof(int8_t) * (s->mb_width - s->mb_x));
  943. if (s->mb_x > 0)
  944. s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride - 1] + 3] = -1;
  945. }
  946. return 0;
  947. }
  948. static void init_dequant4_coeff_table(SVQ3Context *s)
  949. {
  950. int q, x;
  951. const int max_qp = 51;
  952. for (q = 0; q < max_qp + 1; q++) {
  953. int shift = ff_h264_quant_div6[q] + 2;
  954. int idx = ff_h264_quant_rem6[q];
  955. for (x = 0; x < 16; x++)
  956. s->dequant4_coeff[q][(x >> 2) | ((x << 2) & 0xF)] =
  957. ((uint32_t)ff_h264_dequant4_coeff_init[idx][(x & 1) + ((x >> 2) & 1)] * 16) << shift;
  958. }
  959. }
  960. static av_cold int svq3_decode_init(AVCodecContext *avctx)
  961. {
  962. SVQ3Context *s = avctx->priv_data;
  963. H264Context *h = &s->h;
  964. H264SliceContext *sl;
  965. int m, x, y;
  966. unsigned char *extradata;
  967. unsigned char *extradata_end;
  968. unsigned int size;
  969. int marker_found = 0;
  970. int ret;
  971. s->cur_pic = av_mallocz(sizeof(*s->cur_pic));
  972. s->last_pic = av_mallocz(sizeof(*s->last_pic));
  973. s->next_pic = av_mallocz(sizeof(*s->next_pic));
  974. if (!s->next_pic || !s->last_pic || !s->cur_pic) {
  975. ret = AVERROR(ENOMEM);
  976. goto fail;
  977. }
  978. s->cur_pic->f = av_frame_alloc();
  979. s->last_pic->f = av_frame_alloc();
  980. s->next_pic->f = av_frame_alloc();
  981. if (!s->cur_pic->f || !s->last_pic->f || !s->next_pic->f)
  982. return AVERROR(ENOMEM);
  983. if ((ret = ff_h264_decode_init(avctx)) < 0)
  984. goto fail;
  985. // we will overwrite it later during decoding
  986. av_frame_free(&h->cur_pic.f);
  987. av_frame_free(&h->last_pic_for_ec.f);
  988. ff_h264dsp_init(&s->h264dsp, 8, 1);
  989. av_assert0(h->sps.bit_depth_chroma == 0);
  990. ff_h264_pred_init(&s->hpc, AV_CODEC_ID_SVQ3, 8, 1);
  991. ff_videodsp_init(&s->vdsp, 8);
  992. avctx->bits_per_raw_sample = 8;
  993. h->sps.bit_depth_luma = 8;
  994. h->chroma_format_idc = 1;
  995. ff_hpeldsp_init(&s->hdsp, avctx->flags);
  996. ff_tpeldsp_init(&s->tdsp);
  997. sl = h->slice_ctx;
  998. h->flags = avctx->flags;
  999. sl->is_complex = 1;
  1000. h->sps.chroma_format_idc = 1;
  1001. h->picture_structure = PICT_FRAME;
  1002. avctx->pix_fmt = AV_PIX_FMT_YUVJ420P;
  1003. avctx->color_range = AVCOL_RANGE_JPEG;
  1004. h->slice_ctx[0].chroma_qp[0] = h->slice_ctx[0].chroma_qp[1] = 4;
  1005. h->chroma_x_shift = h->chroma_y_shift = 1;
  1006. s->halfpel_flag = 1;
  1007. s->thirdpel_flag = 1;
  1008. s->has_watermark = 0;
  1009. /* prowl for the "SEQH" marker in the extradata */
  1010. extradata = (unsigned char *)avctx->extradata;
  1011. extradata_end = avctx->extradata + avctx->extradata_size;
  1012. if (extradata) {
  1013. for (m = 0; m + 8 < avctx->extradata_size; m++) {
  1014. if (!memcmp(extradata, "SEQH", 4)) {
  1015. marker_found = 1;
  1016. break;
  1017. }
  1018. extradata++;
  1019. }
  1020. }
  1021. /* if a match was found, parse the extra data */
  1022. if (marker_found) {
  1023. GetBitContext gb;
  1024. int frame_size_code;
  1025. int unk0, unk1, unk2, unk3, unk4;
  1026. size = AV_RB32(&extradata[4]);
  1027. if (size > extradata_end - extradata - 8) {
  1028. ret = AVERROR_INVALIDDATA;
  1029. goto fail;
  1030. }
  1031. init_get_bits(&gb, extradata + 8, size * 8);
  1032. /* 'frame size code' and optional 'width, height' */
  1033. frame_size_code = get_bits(&gb, 3);
  1034. switch (frame_size_code) {
  1035. case 0:
  1036. avctx->width = 160;
  1037. avctx->height = 120;
  1038. break;
  1039. case 1:
  1040. avctx->width = 128;
  1041. avctx->height = 96;
  1042. break;
  1043. case 2:
  1044. avctx->width = 176;
  1045. avctx->height = 144;
  1046. break;
  1047. case 3:
  1048. avctx->width = 352;
  1049. avctx->height = 288;
  1050. break;
  1051. case 4:
  1052. avctx->width = 704;
  1053. avctx->height = 576;
  1054. break;
  1055. case 5:
  1056. avctx->width = 240;
  1057. avctx->height = 180;
  1058. break;
  1059. case 6:
  1060. avctx->width = 320;
  1061. avctx->height = 240;
  1062. break;
  1063. case 7:
  1064. avctx->width = get_bits(&gb, 12);
  1065. avctx->height = get_bits(&gb, 12);
  1066. break;
  1067. }
  1068. s->halfpel_flag = get_bits1(&gb);
  1069. s->thirdpel_flag = get_bits1(&gb);
  1070. /* unknown fields */
  1071. unk0 = get_bits1(&gb);
  1072. unk1 = get_bits1(&gb);
  1073. unk2 = get_bits1(&gb);
  1074. unk3 = get_bits1(&gb);
  1075. h->low_delay = get_bits1(&gb);
  1076. /* unknown field */
  1077. unk4 = get_bits1(&gb);
  1078. av_log(avctx, AV_LOG_DEBUG, "Unknown fields %d %d %d %d %d\n",
  1079. unk0, unk1, unk2, unk3, unk4);
  1080. if (skip_1stop_8data_bits(&gb) < 0) {
  1081. ret = AVERROR_INVALIDDATA;
  1082. goto fail;
  1083. }
  1084. s->has_watermark = get_bits1(&gb);
  1085. avctx->has_b_frames = !h->low_delay;
  1086. if (s->has_watermark) {
  1087. #if CONFIG_ZLIB
  1088. unsigned watermark_width = svq3_get_ue_golomb(&gb);
  1089. unsigned watermark_height = svq3_get_ue_golomb(&gb);
  1090. int u1 = svq3_get_ue_golomb(&gb);
  1091. int u2 = get_bits(&gb, 8);
  1092. int u3 = get_bits(&gb, 2);
  1093. int u4 = svq3_get_ue_golomb(&gb);
  1094. unsigned long buf_len = watermark_width *
  1095. watermark_height * 4;
  1096. int offset = get_bits_count(&gb) + 7 >> 3;
  1097. uint8_t *buf;
  1098. if (watermark_height <= 0 ||
  1099. (uint64_t)watermark_width * 4 > UINT_MAX / watermark_height) {
  1100. ret = -1;
  1101. goto fail;
  1102. }
  1103. buf = av_malloc(buf_len);
  1104. if (!buf) {
  1105. ret = AVERROR(ENOMEM);
  1106. goto fail;
  1107. }
  1108. av_log(avctx, AV_LOG_DEBUG, "watermark size: %ux%u\n",
  1109. watermark_width, watermark_height);
  1110. av_log(avctx, AV_LOG_DEBUG,
  1111. "u1: %x u2: %x u3: %x compressed data size: %d offset: %d\n",
  1112. u1, u2, u3, u4, offset);
  1113. if (uncompress(buf, &buf_len, extradata + 8 + offset,
  1114. size - offset) != Z_OK) {
  1115. av_log(avctx, AV_LOG_ERROR,
  1116. "could not uncompress watermark logo\n");
  1117. av_free(buf);
  1118. ret = -1;
  1119. goto fail;
  1120. }
  1121. s->watermark_key = ff_svq1_packet_checksum(buf, buf_len, 0);
  1122. s->watermark_key = s->watermark_key << 16 | s->watermark_key;
  1123. av_log(avctx, AV_LOG_DEBUG,
  1124. "watermark key %#"PRIx32"\n", s->watermark_key);
  1125. av_free(buf);
  1126. #else
  1127. av_log(avctx, AV_LOG_ERROR,
  1128. "this svq3 file contains watermark which need zlib support compiled in\n");
  1129. ret = -1;
  1130. goto fail;
  1131. #endif
  1132. }
  1133. }
  1134. s->mb_width = (avctx->width + 15) / 16;
  1135. s->mb_height = (avctx->height + 15) / 16;
  1136. s->mb_stride = s->mb_width + 1;
  1137. s->mb_num = s->mb_width * s->mb_height;
  1138. s->b_stride = 4 * s->mb_width;
  1139. s->h_edge_pos = s->mb_width * 16;
  1140. s->v_edge_pos = s->mb_height * 16;
  1141. s->intra4x4_pred_mode = av_mallocz(s->mb_stride * 2 * 8);
  1142. if (!s->intra4x4_pred_mode)
  1143. return AVERROR(ENOMEM);
  1144. s->mb2br_xy = av_mallocz(s->mb_stride * (s->mb_height + 1) *
  1145. sizeof(*s->mb2br_xy));
  1146. if (!s->mb2br_xy)
  1147. return AVERROR(ENOMEM);
  1148. for (y = 0; y < s->mb_height; y++)
  1149. for (x = 0; x < s->mb_width; x++) {
  1150. const int mb_xy = x + y * s->mb_stride;
  1151. s->mb2br_xy[mb_xy] = 8 * (mb_xy % (2 * s->mb_stride));
  1152. }
  1153. init_dequant4_coeff_table(s);
  1154. return 0;
  1155. fail:
  1156. svq3_decode_end(avctx);
  1157. return ret;
  1158. }
  1159. static void free_picture(AVCodecContext *avctx, H264Picture *pic)
  1160. {
  1161. int i;
  1162. for (i = 0; i < 2; i++) {
  1163. av_buffer_unref(&pic->motion_val_buf[i]);
  1164. av_buffer_unref(&pic->ref_index_buf[i]);
  1165. }
  1166. av_buffer_unref(&pic->mb_type_buf);
  1167. av_frame_unref(pic->f);
  1168. }
  1169. static int get_buffer(AVCodecContext *avctx, H264Picture *pic)
  1170. {
  1171. SVQ3Context *s = avctx->priv_data;
  1172. H264Context *h = &s->h;
  1173. H264SliceContext *sl = &h->slice_ctx[0];
  1174. const int big_mb_num = s->mb_stride * (s->mb_height + 1) + 1;
  1175. const int mb_array_size = s->mb_stride * s->mb_height;
  1176. const int b4_stride = s->mb_width * 4 + 1;
  1177. const int b4_array_size = b4_stride * s->mb_height * 4;
  1178. int ret;
  1179. if (!pic->motion_val_buf[0]) {
  1180. int i;
  1181. pic->mb_type_buf = av_buffer_allocz((big_mb_num + s->mb_stride) * sizeof(uint32_t));
  1182. if (!pic->mb_type_buf)
  1183. return AVERROR(ENOMEM);
  1184. pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * s->mb_stride + 1;
  1185. for (i = 0; i < 2; i++) {
  1186. pic->motion_val_buf[i] = av_buffer_allocz(2 * (b4_array_size + 4) * sizeof(int16_t));
  1187. pic->ref_index_buf[i] = av_buffer_allocz(4 * mb_array_size);
  1188. if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i]) {
  1189. ret = AVERROR(ENOMEM);
  1190. goto fail;
  1191. }
  1192. pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
  1193. pic->ref_index[i] = pic->ref_index_buf[i]->data;
  1194. }
  1195. }
  1196. pic->reference = !(s->pict_type == AV_PICTURE_TYPE_B);
  1197. ret = ff_get_buffer(avctx, pic->f,
  1198. pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
  1199. if (ret < 0)
  1200. goto fail;
  1201. if (!s->edge_emu_buffer) {
  1202. s->edge_emu_buffer = av_mallocz_array(pic->f->linesize[0], 17);
  1203. if (!s->edge_emu_buffer)
  1204. return AVERROR(ENOMEM);
  1205. }
  1206. sl->linesize = pic->f->linesize[0];
  1207. sl->uvlinesize = pic->f->linesize[1];
  1208. return 0;
  1209. fail:
  1210. free_picture(avctx, pic);
  1211. return ret;
  1212. }
  1213. static int svq3_decode_frame(AVCodecContext *avctx, void *data,
  1214. int *got_frame, AVPacket *avpkt)
  1215. {
  1216. SVQ3Context *s = avctx->priv_data;
  1217. H264Context *h = &s->h;
  1218. H264SliceContext *sl = &h->slice_ctx[0];
  1219. int buf_size = avpkt->size;
  1220. int left;
  1221. uint8_t *buf;
  1222. int ret, m, i;
  1223. /* special case for last picture */
  1224. if (buf_size == 0) {
  1225. if (s->next_pic->f->data[0] && !h->low_delay && !s->last_frame_output) {
  1226. ret = av_frame_ref(data, s->next_pic->f);
  1227. if (ret < 0)
  1228. return ret;
  1229. s->last_frame_output = 1;
  1230. *got_frame = 1;
  1231. }
  1232. return 0;
  1233. }
  1234. sl->mb_x = sl->mb_y = sl->mb_xy = 0;
  1235. if (s->watermark_key) {
  1236. av_fast_padded_malloc(&s->buf, &s->buf_size, buf_size);
  1237. if (!s->buf)
  1238. return AVERROR(ENOMEM);
  1239. memcpy(s->buf, avpkt->data, buf_size);
  1240. buf = s->buf;
  1241. } else {
  1242. buf = avpkt->data;
  1243. }
  1244. ret = init_get_bits(&s->gb, buf, 8 * buf_size);
  1245. if (ret < 0)
  1246. return ret;
  1247. if (svq3_decode_slice_header(avctx))
  1248. return -1;
  1249. if (s->pict_type != AV_PICTURE_TYPE_B)
  1250. FFSWAP(H264Picture*, s->next_pic, s->last_pic);
  1251. av_frame_unref(s->cur_pic->f);
  1252. /* for skipping the frame */
  1253. s->cur_pic->f->pict_type = s->pict_type;
  1254. s->cur_pic->f->key_frame = (s->pict_type == AV_PICTURE_TYPE_I);
  1255. ret = get_buffer(avctx, s->cur_pic);
  1256. if (ret < 0)
  1257. return ret;
  1258. h->cur_pic_ptr = s->cur_pic;
  1259. h->cur_pic = *s->cur_pic;
  1260. for (i = 0; i < 16; i++) {
  1261. h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * sl->linesize * ((scan8[i] - scan8[0]) >> 3);
  1262. h->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * sl->linesize * ((scan8[i] - scan8[0]) >> 3);
  1263. }
  1264. for (i = 0; i < 16; i++) {
  1265. h->block_offset[16 + i] =
  1266. h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * sl->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
  1267. h->block_offset[48 + 16 + i] =
  1268. h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * sl->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
  1269. }
  1270. if (s->pict_type != AV_PICTURE_TYPE_I) {
  1271. if (!s->last_pic->f->data[0]) {
  1272. av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
  1273. av_frame_unref(s->last_pic->f);
  1274. ret = get_buffer(avctx, s->last_pic);
  1275. if (ret < 0)
  1276. return ret;
  1277. memset(s->last_pic->f->data[0], 0, avctx->height * s->last_pic->f->linesize[0]);
  1278. memset(s->last_pic->f->data[1], 0x80, (avctx->height / 2) *
  1279. s->last_pic->f->linesize[1]);
  1280. memset(s->last_pic->f->data[2], 0x80, (avctx->height / 2) *
  1281. s->last_pic->f->linesize[2]);
  1282. }
  1283. if (s->pict_type == AV_PICTURE_TYPE_B && !s->next_pic->f->data[0]) {
  1284. av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
  1285. av_frame_unref(s->next_pic->f);
  1286. ret = get_buffer(avctx, s->next_pic);
  1287. if (ret < 0)
  1288. return ret;
  1289. memset(s->next_pic->f->data[0], 0, avctx->height * s->next_pic->f->linesize[0]);
  1290. memset(s->next_pic->f->data[1], 0x80, (avctx->height / 2) *
  1291. s->next_pic->f->linesize[1]);
  1292. memset(s->next_pic->f->data[2], 0x80, (avctx->height / 2) *
  1293. s->next_pic->f->linesize[2]);
  1294. }
  1295. }
  1296. if (avctx->debug & FF_DEBUG_PICT_INFO)
  1297. av_log(h->avctx, AV_LOG_DEBUG,
  1298. "%c hpel:%d, tpel:%d aqp:%d qp:%d, slice_num:%02X\n",
  1299. av_get_picture_type_char(s->pict_type),
  1300. s->halfpel_flag, s->thirdpel_flag,
  1301. s->adaptive_quant, h->slice_ctx[0].qscale, sl->slice_num);
  1302. if (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B ||
  1303. avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I ||
  1304. avctx->skip_frame >= AVDISCARD_ALL)
  1305. return 0;
  1306. if (s->next_p_frame_damaged) {
  1307. if (s->pict_type == AV_PICTURE_TYPE_B)
  1308. return 0;
  1309. else
  1310. s->next_p_frame_damaged = 0;
  1311. }
  1312. if (s->pict_type == AV_PICTURE_TYPE_B) {
  1313. h->frame_num_offset = sl->slice_num - h->prev_frame_num;
  1314. if (h->frame_num_offset < 0)
  1315. h->frame_num_offset += 256;
  1316. if (h->frame_num_offset == 0 ||
  1317. h->frame_num_offset >= h->prev_frame_num_offset) {
  1318. av_log(h->avctx, AV_LOG_ERROR, "error in B-frame picture id\n");
  1319. return -1;
  1320. }
  1321. } else {
  1322. h->prev_frame_num = h->frame_num;
  1323. h->frame_num = sl->slice_num;
  1324. h->prev_frame_num_offset = h->frame_num - h->prev_frame_num;
  1325. if (h->prev_frame_num_offset < 0)
  1326. h->prev_frame_num_offset += 256;
  1327. }
  1328. for (m = 0; m < 2; m++) {
  1329. int i;
  1330. for (i = 0; i < 4; i++) {
  1331. int j;
  1332. for (j = -1; j < 4; j++)
  1333. s->ref_cache[m][scan8[0] + 8 * i + j] = 1;
  1334. if (i < 3)
  1335. s->ref_cache[m][scan8[0] + 8 * i + j] = PART_NOT_AVAILABLE;
  1336. }
  1337. }
  1338. for (s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
  1339. for (s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
  1340. unsigned mb_type;
  1341. s->mb_xy = s->mb_x + s->mb_y * s->mb_stride;
  1342. if ((get_bits_left(&h->gb)) <= 7) {
  1343. if (((get_bits_count(&h->gb) & 7) == 0 ||
  1344. show_bits(&h->gb, get_bits_left(&h->gb) & 7) == 0)) {
  1345. if (svq3_decode_slice_header(avctx))
  1346. return -1;
  1347. }
  1348. /* TODO: support s->mb_skip_run */
  1349. }
  1350. mb_type = svq3_get_ue_golomb(&h->gb);
  1351. if (s->pict_type == AV_PICTURE_TYPE_I)
  1352. mb_type += 8;
  1353. else if (s->pict_type == AV_PICTURE_TYPE_B && mb_type >= 4)
  1354. mb_type += 4;
  1355. if (mb_type > 33 || svq3_decode_mb(s, mb_type)) {
  1356. av_log(h->avctx, AV_LOG_ERROR,
  1357. "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  1358. return -1;
  1359. }
  1360. if (mb_type != 0 || sl->cbp)
  1361. hl_decode_mb(s, h, &h->slice_ctx[0]);
  1362. if (s->pict_type != AV_PICTURE_TYPE_B && !h->low_delay)
  1363. h->cur_pic.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
  1364. (s->pict_type == AV_PICTURE_TYPE_P && mb_type < 8) ? (mb_type - 1) : -1;
  1365. }
  1366. ff_draw_horiz_band(avctx, s->cur_pic->f,
  1367. s->last_pic->f->data[0] ? s->last_pic->f : NULL,
  1368. 16 * s->mb_y, 16, h->picture_structure, 0,
  1369. h->low_delay);
  1370. }
  1371. left = buf_size*8 - get_bits_count(&h->gb);
  1372. if (sl->mb_y != h->mb_height || sl->mb_x != h->mb_width) {
  1373. av_log(avctx, AV_LOG_INFO, "frame num %d incomplete pic x %d y %d left %d\n", avctx->frame_number, sl->mb_y, sl->mb_x, left);
  1374. //av_hex_dump(stderr, buf+buf_size-8, 8);
  1375. }
  1376. if (left < 0) {
  1377. av_log(avctx, AV_LOG_ERROR, "frame num %d left %d\n", avctx->frame_number, left);
  1378. return -1;
  1379. }
  1380. if (s->pict_type == AV_PICTURE_TYPE_B || h->low_delay)
  1381. ret = av_frame_ref(data, s->cur_pic->f);
  1382. else if (s->last_pic->f->data[0])
  1383. ret = av_frame_ref(data, s->last_pic->f);
  1384. if (ret < 0)
  1385. return ret;
  1386. /* Do not output the last pic after seeking. */
  1387. if (s->last_pic->f->data[0] || h->low_delay)
  1388. *got_frame = 1;
  1389. if (s->pict_type != AV_PICTURE_TYPE_B) {
  1390. FFSWAP(H264Picture*, s->cur_pic, s->next_pic);
  1391. } else {
  1392. av_frame_unref(s->cur_pic->f);
  1393. }
  1394. return buf_size;
  1395. }
  1396. static av_cold int svq3_decode_end(AVCodecContext *avctx)
  1397. {
  1398. SVQ3Context *s = avctx->priv_data;
  1399. H264Context *h = &s->h;
  1400. free_picture(avctx, s->cur_pic);
  1401. free_picture(avctx, s->next_pic);
  1402. free_picture(avctx, s->last_pic);
  1403. av_frame_free(&s->cur_pic->f);
  1404. av_frame_free(&s->next_pic->f);
  1405. av_frame_free(&s->last_pic->f);
  1406. av_freep(&s->cur_pic);
  1407. av_freep(&s->next_pic);
  1408. av_freep(&s->last_pic);
  1409. av_freep(&s->slice_buf);
  1410. av_freep(&s->intra4x4_pred_mode);
  1411. av_freep(&s->edge_emu_buffer);
  1412. av_freep(&s->mb2br_xy);
  1413. memset(&h->cur_pic, 0, sizeof(h->cur_pic));
  1414. ff_h264_free_context(h);
  1415. av_freep(&s->buf);
  1416. s->buf_size = 0;
  1417. return 0;
  1418. }
  1419. AVCodec ff_svq3_decoder = {
  1420. .name = "svq3",
  1421. .long_name = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 3 / Sorenson Video 3 / SVQ3"),
  1422. .type = AVMEDIA_TYPE_VIDEO,
  1423. .id = AV_CODEC_ID_SVQ3,
  1424. .priv_data_size = sizeof(SVQ3Context),
  1425. .init = svq3_decode_init,
  1426. .close = svq3_decode_end,
  1427. .decode = svq3_decode_frame,
  1428. .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND |
  1429. AV_CODEC_CAP_DR1 |
  1430. AV_CODEC_CAP_DELAY,
  1431. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P,
  1432. AV_PIX_FMT_NONE},
  1433. };