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.

1320 lines
47KB

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