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.

1658 lines
58KB

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