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.

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