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.

1353 lines
48KB

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