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.

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