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.

1295 lines
46KB

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