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.

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