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.

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