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.

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