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.

1385 lines
49KB

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