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.

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