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.

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