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.

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