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.

1315 lines
47KB

  1. /*
  2. * Copyright (c) 2003 The Libav Project
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /*
  21. * How to use this decoder:
  22. * SVQ3 data is transported within Apple Quicktime files. Quicktime files
  23. * have stsd atoms to describe media trak properties. A stsd atom for a
  24. * video trak contains 1 or more ImageDescription atoms. These atoms begin
  25. * with the 4-byte length of the atom followed by the codec fourcc. Some
  26. * decoders need information in this atom to operate correctly. Such
  27. * is the case with SVQ3. In order to get the best use out of this decoder,
  28. * the calling app must make the SVQ3 ImageDescription atom available
  29. * via the AVCodecContext's extradata[_size] field:
  30. *
  31. * AVCodecContext.extradata = pointer to ImageDescription, first characters
  32. * are expected to be 'S', 'V', 'Q', and '3', NOT the 4-byte atom length
  33. * AVCodecContext.extradata_size = size of ImageDescription atom memory
  34. * buffer (which will be the same as the ImageDescription atom size field
  35. * from the QT file, minus 4 bytes since the length is missing)
  36. *
  37. * You will know you have these parameters passed correctly when the decoder
  38. * correctly decodes this file:
  39. * http://samples.libav.org/V-codecs/SVQ3/Vertical400kbit.sorenson3.mov
  40. */
  41. #include "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. memset(block, 0, 16 * sizeof(int16_t));
  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->motion_val[0][b_xy][0] << 1;
  332. my = s->next_pic->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.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->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->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.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.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.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.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.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.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.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.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. }
  586. if (!IS_INTRA16x16(mb_type) &&
  587. (!IS_SKIP(mb_type) || h->pict_type == AV_PICTURE_TYPE_B)) {
  588. if ((vlc = svq3_get_ue_golomb(&h->gb)) >= 48) {
  589. av_log(h->avctx, AV_LOG_ERROR, "cbp_vlc=%d\n", vlc);
  590. return -1;
  591. }
  592. cbp = IS_INTRA(mb_type) ? golomb_to_intra4x4_cbp[vlc]
  593. : golomb_to_inter_cbp[vlc];
  594. }
  595. if (IS_INTRA16x16(mb_type) ||
  596. (h->pict_type != AV_PICTURE_TYPE_I && s->adaptive_quant && cbp)) {
  597. h->qscale += svq3_get_se_golomb(&h->gb);
  598. if (h->qscale > 31u) {
  599. av_log(h->avctx, AV_LOG_ERROR, "qscale:%d\n", h->qscale);
  600. return -1;
  601. }
  602. }
  603. if (IS_INTRA16x16(mb_type)) {
  604. AV_ZERO128(h->mb_luma_dc[0] + 0);
  605. AV_ZERO128(h->mb_luma_dc[0] + 8);
  606. if (svq3_decode_block(&h->gb, h->mb_luma_dc[0], 0, 1)) {
  607. av_log(h->avctx, AV_LOG_ERROR,
  608. "error while decoding intra luma dc\n");
  609. return -1;
  610. }
  611. }
  612. if (cbp) {
  613. const int index = IS_INTRA16x16(mb_type) ? 1 : 0;
  614. const int type = ((h->qscale < 24 && IS_INTRA4x4(mb_type)) ? 2 : 1);
  615. for (i = 0; i < 4; i++)
  616. if ((cbp & (1 << i))) {
  617. for (j = 0; j < 4; j++) {
  618. k = index ? (1 * (j & 1) + 2 * (i & 1) +
  619. 2 * (j & 2) + 4 * (i & 2))
  620. : (4 * i + j);
  621. h->non_zero_count_cache[scan8[k]] = 1;
  622. if (svq3_decode_block(&h->gb, &h->mb[16 * k], index, type)) {
  623. av_log(h->avctx, AV_LOG_ERROR,
  624. "error while decoding block\n");
  625. return -1;
  626. }
  627. }
  628. }
  629. if ((cbp & 0x30)) {
  630. for (i = 1; i < 3; ++i)
  631. if (svq3_decode_block(&h->gb, &h->mb[16 * 16 * i], 0, 3)) {
  632. av_log(h->avctx, AV_LOG_ERROR,
  633. "error while decoding chroma dc block\n");
  634. return -1;
  635. }
  636. if ((cbp & 0x20)) {
  637. for (i = 1; i < 3; i++) {
  638. for (j = 0; j < 4; j++) {
  639. k = 16 * i + j;
  640. h->non_zero_count_cache[scan8[k]] = 1;
  641. if (svq3_decode_block(&h->gb, &h->mb[16 * k], 1, 1)) {
  642. av_log(h->avctx, AV_LOG_ERROR,
  643. "error while decoding chroma ac block\n");
  644. return -1;
  645. }
  646. }
  647. }
  648. }
  649. }
  650. }
  651. h->cbp = cbp;
  652. h->cur_pic.mb_type[mb_xy] = mb_type;
  653. if (IS_INTRA(mb_type))
  654. h->chroma_pred_mode = ff_h264_check_intra_pred_mode(h, DC_PRED8x8, 1);
  655. return 0;
  656. }
  657. static int svq3_decode_slice_header(AVCodecContext *avctx)
  658. {
  659. SVQ3Context *s = avctx->priv_data;
  660. H264Context *h = &s->h;
  661. const int mb_xy = h->mb_xy;
  662. int i, header;
  663. unsigned slice_id;
  664. header = get_bits(&h->gb, 8);
  665. if (((header & 0x9F) != 1 && (header & 0x9F) != 2) || (header & 0x60) == 0) {
  666. /* TODO: what? */
  667. av_log(avctx, AV_LOG_ERROR, "unsupported slice header (%02X)\n", header);
  668. return -1;
  669. } else {
  670. int length = header >> 5 & 3;
  671. s->next_slice_index = get_bits_count(&h->gb) +
  672. 8 * show_bits(&h->gb, 8 * length) +
  673. 8 * length;
  674. if (s->next_slice_index > h->gb.size_in_bits) {
  675. av_log(avctx, AV_LOG_ERROR, "slice after bitstream end\n");
  676. return -1;
  677. }
  678. h->gb.size_in_bits = s->next_slice_index - 8 * (length - 1);
  679. skip_bits(&h->gb, 8);
  680. if (s->watermark_key) {
  681. uint32_t header = AV_RL32(&h->gb.buffer[(get_bits_count(&h->gb) >> 3) + 1]);
  682. AV_WL32(&h->gb.buffer[(get_bits_count(&h->gb) >> 3) + 1],
  683. header ^ s->watermark_key);
  684. }
  685. if (length > 0) {
  686. memcpy((uint8_t *) &h->gb.buffer[get_bits_count(&h->gb) >> 3],
  687. &h->gb.buffer[h->gb.size_in_bits >> 3], length - 1);
  688. }
  689. skip_bits_long(&h->gb, 0);
  690. }
  691. if ((slice_id = svq3_get_ue_golomb(&h->gb)) >= 3) {
  692. av_log(h->avctx, AV_LOG_ERROR, "illegal slice type %d \n", slice_id);
  693. return -1;
  694. }
  695. h->slice_type = golomb_to_pict_type[slice_id];
  696. if ((header & 0x9F) == 2) {
  697. i = (h->mb_num < 64) ? 6 : (1 + av_log2(h->mb_num - 1));
  698. h->mb_skip_run = get_bits(&h->gb, i) -
  699. (h->mb_y * h->mb_width + h->mb_x);
  700. } else {
  701. skip_bits1(&h->gb);
  702. h->mb_skip_run = 0;
  703. }
  704. h->slice_num = get_bits(&h->gb, 8);
  705. h->qscale = get_bits(&h->gb, 5);
  706. s->adaptive_quant = get_bits1(&h->gb);
  707. /* unknown fields */
  708. skip_bits1(&h->gb);
  709. if (s->unknown_flag)
  710. skip_bits1(&h->gb);
  711. skip_bits1(&h->gb);
  712. skip_bits(&h->gb, 2);
  713. while (get_bits1(&h->gb))
  714. skip_bits(&h->gb, 8);
  715. /* reset intra predictors and invalidate motion vector references */
  716. if (h->mb_x > 0) {
  717. memset(h->intra4x4_pred_mode + h->mb2br_xy[mb_xy - 1] + 3,
  718. -1, 4 * sizeof(int8_t));
  719. memset(h->intra4x4_pred_mode + h->mb2br_xy[mb_xy - h->mb_x],
  720. -1, 8 * sizeof(int8_t) * h->mb_x);
  721. }
  722. if (h->mb_y > 0) {
  723. memset(h->intra4x4_pred_mode + h->mb2br_xy[mb_xy - h->mb_stride],
  724. -1, 8 * sizeof(int8_t) * (h->mb_width - h->mb_x));
  725. if (h->mb_x > 0)
  726. h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride - 1] + 3] = -1;
  727. }
  728. return 0;
  729. }
  730. static av_cold int svq3_decode_init(AVCodecContext *avctx)
  731. {
  732. SVQ3Context *s = avctx->priv_data;
  733. H264Context *h = &s->h;
  734. int m;
  735. unsigned char *extradata;
  736. unsigned char *extradata_end;
  737. unsigned int size;
  738. int marker_found = 0;
  739. s->cur_pic = av_mallocz(sizeof(*s->cur_pic));
  740. s->last_pic = av_mallocz(sizeof(*s->last_pic));
  741. s->next_pic = av_mallocz(sizeof(*s->next_pic));
  742. if (!s->next_pic || !s->last_pic || !s->cur_pic) {
  743. av_freep(&s->cur_pic);
  744. av_freep(&s->last_pic);
  745. av_freep(&s->next_pic);
  746. return AVERROR(ENOMEM);
  747. }
  748. if (ff_h264_decode_init(avctx) < 0)
  749. return -1;
  750. h->flags = avctx->flags;
  751. h->is_complex = 1;
  752. h->picture_structure = PICT_FRAME;
  753. avctx->pix_fmt = avctx->codec->pix_fmts[0];
  754. h->chroma_qp[0] = h->chroma_qp[1] = 4;
  755. h->chroma_x_shift = h->chroma_y_shift = 1;
  756. s->halfpel_flag = 1;
  757. s->thirdpel_flag = 1;
  758. s->unknown_flag = 0;
  759. /* prowl for the "SEQH" marker in the extradata */
  760. extradata = (unsigned char *)avctx->extradata;
  761. extradata_end = avctx->extradata + avctx->extradata_size;
  762. if (extradata) {
  763. for (m = 0; m + 8 < avctx->extradata_size; m++) {
  764. if (!memcmp(extradata, "SEQH", 4)) {
  765. marker_found = 1;
  766. break;
  767. }
  768. extradata++;
  769. }
  770. }
  771. /* if a match was found, parse the extra data */
  772. if (marker_found) {
  773. GetBitContext gb;
  774. int frame_size_code;
  775. size = AV_RB32(&extradata[4]);
  776. if (size > extradata_end - extradata - 8)
  777. return AVERROR_INVALIDDATA;
  778. init_get_bits(&gb, extradata + 8, size * 8);
  779. /* 'frame size code' and optional 'width, height' */
  780. frame_size_code = get_bits(&gb, 3);
  781. switch (frame_size_code) {
  782. case 0:
  783. avctx->width = 160;
  784. avctx->height = 120;
  785. break;
  786. case 1:
  787. avctx->width = 128;
  788. avctx->height = 96;
  789. break;
  790. case 2:
  791. avctx->width = 176;
  792. avctx->height = 144;
  793. break;
  794. case 3:
  795. avctx->width = 352;
  796. avctx->height = 288;
  797. break;
  798. case 4:
  799. avctx->width = 704;
  800. avctx->height = 576;
  801. break;
  802. case 5:
  803. avctx->width = 240;
  804. avctx->height = 180;
  805. break;
  806. case 6:
  807. avctx->width = 320;
  808. avctx->height = 240;
  809. break;
  810. case 7:
  811. avctx->width = get_bits(&gb, 12);
  812. avctx->height = get_bits(&gb, 12);
  813. break;
  814. }
  815. s->halfpel_flag = get_bits1(&gb);
  816. s->thirdpel_flag = get_bits1(&gb);
  817. /* unknown fields */
  818. skip_bits1(&gb);
  819. skip_bits1(&gb);
  820. skip_bits1(&gb);
  821. skip_bits1(&gb);
  822. h->low_delay = get_bits1(&gb);
  823. /* unknown field */
  824. skip_bits1(&gb);
  825. while (get_bits1(&gb))
  826. skip_bits(&gb, 8);
  827. s->unknown_flag = get_bits1(&gb);
  828. avctx->has_b_frames = !h->low_delay;
  829. if (s->unknown_flag) {
  830. #if CONFIG_ZLIB
  831. unsigned watermark_width = svq3_get_ue_golomb(&gb);
  832. unsigned watermark_height = svq3_get_ue_golomb(&gb);
  833. int u1 = svq3_get_ue_golomb(&gb);
  834. int u2 = get_bits(&gb, 8);
  835. int u3 = get_bits(&gb, 2);
  836. int u4 = svq3_get_ue_golomb(&gb);
  837. unsigned long buf_len = watermark_width *
  838. watermark_height * 4;
  839. int offset = get_bits_count(&gb) + 7 >> 3;
  840. uint8_t *buf;
  841. if ((uint64_t)watermark_width * 4 > UINT_MAX / watermark_height)
  842. return -1;
  843. buf = av_malloc(buf_len);
  844. av_log(avctx, AV_LOG_DEBUG, "watermark size: %dx%d\n",
  845. watermark_width, watermark_height);
  846. av_log(avctx, AV_LOG_DEBUG,
  847. "u1: %x u2: %x u3: %x compressed data size: %d offset: %d\n",
  848. u1, u2, u3, u4, offset);
  849. if (uncompress(buf, &buf_len, extradata + 8 + offset,
  850. size - offset) != Z_OK) {
  851. av_log(avctx, AV_LOG_ERROR,
  852. "could not uncompress watermark logo\n");
  853. av_free(buf);
  854. return -1;
  855. }
  856. s->watermark_key = ff_svq1_packet_checksum(buf, buf_len, 0);
  857. s->watermark_key = s->watermark_key << 16 | s->watermark_key;
  858. av_log(avctx, AV_LOG_DEBUG,
  859. "watermark key %#x\n", s->watermark_key);
  860. av_free(buf);
  861. #else
  862. av_log(avctx, AV_LOG_ERROR,
  863. "this svq3 file contains watermark which need zlib support compiled in\n");
  864. return -1;
  865. #endif
  866. }
  867. }
  868. h->width = avctx->width;
  869. h->height = avctx->height;
  870. h->mb_width = (h->width + 15) / 16;
  871. h->mb_height = (h->height + 15) / 16;
  872. h->mb_stride = h->mb_width + 1;
  873. h->mb_num = h->mb_width * h->mb_height;
  874. h->b_stride = 4 * h->mb_width;
  875. s->h_edge_pos = h->mb_width * 16;
  876. s->v_edge_pos = h->mb_height * 16;
  877. if (ff_h264_alloc_tables(h) < 0) {
  878. av_log(avctx, AV_LOG_ERROR, "svq3 memory allocation failed\n");
  879. return AVERROR(ENOMEM);
  880. }
  881. return 0;
  882. }
  883. static void free_picture(AVCodecContext *avctx, Picture *pic)
  884. {
  885. int i;
  886. for (i = 0; i < 2; i++) {
  887. av_buffer_unref(&pic->motion_val_buf[i]);
  888. av_buffer_unref(&pic->ref_index_buf[i]);
  889. }
  890. av_buffer_unref(&pic->mb_type_buf);
  891. av_frame_unref(&pic->f);
  892. }
  893. static int get_buffer(AVCodecContext *avctx, Picture *pic)
  894. {
  895. SVQ3Context *s = avctx->priv_data;
  896. H264Context *h = &s->h;
  897. const int big_mb_num = h->mb_stride * (h->mb_height + 1) + 1;
  898. const int mb_array_size = h->mb_stride * h->mb_height;
  899. const int b4_stride = h->mb_width * 4 + 1;
  900. const int b4_array_size = b4_stride * h->mb_height * 4;
  901. int ret;
  902. if (!pic->motion_val_buf[0]) {
  903. int i;
  904. pic->mb_type_buf = av_buffer_allocz((big_mb_num + h->mb_stride) * sizeof(uint32_t));
  905. if (!pic->mb_type_buf)
  906. return AVERROR(ENOMEM);
  907. pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * h->mb_stride + 1;
  908. for (i = 0; i < 2; i++) {
  909. pic->motion_val_buf[i] = av_buffer_allocz(2 * (b4_array_size + 4) * sizeof(int16_t));
  910. pic->ref_index_buf[i] = av_buffer_allocz(4 * mb_array_size);
  911. if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i]) {
  912. ret = AVERROR(ENOMEM);
  913. goto fail;
  914. }
  915. pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
  916. pic->ref_index[i] = pic->ref_index_buf[i]->data;
  917. }
  918. }
  919. pic->reference = !(h->pict_type == AV_PICTURE_TYPE_B);
  920. ret = ff_get_buffer(avctx, &pic->f,
  921. pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
  922. if (ret < 0)
  923. goto fail;
  924. if (!h->edge_emu_buffer) {
  925. h->edge_emu_buffer = av_mallocz(pic->f.linesize[0] * 17);
  926. if (!h->edge_emu_buffer)
  927. return AVERROR(ENOMEM);
  928. }
  929. h->linesize = pic->f.linesize[0];
  930. h->uvlinesize = pic->f.linesize[1];
  931. return 0;
  932. fail:
  933. free_picture(avctx, pic);
  934. return ret;
  935. }
  936. static int svq3_decode_frame(AVCodecContext *avctx, void *data,
  937. int *got_frame, AVPacket *avpkt)
  938. {
  939. const uint8_t *buf = avpkt->data;
  940. SVQ3Context *s = avctx->priv_data;
  941. H264Context *h = &s->h;
  942. int buf_size = avpkt->size;
  943. int ret, m, i;
  944. /* special case for last picture */
  945. if (buf_size == 0) {
  946. if (s->next_pic->f.data[0] && !h->low_delay && !s->last_frame_output) {
  947. ret = av_frame_ref(data, &s->next_pic->f);
  948. if (ret < 0)
  949. return ret;
  950. s->last_frame_output = 1;
  951. *got_frame = 1;
  952. }
  953. return 0;
  954. }
  955. init_get_bits(&h->gb, buf, 8 * buf_size);
  956. h->mb_x = h->mb_y = h->mb_xy = 0;
  957. if (svq3_decode_slice_header(avctx))
  958. return -1;
  959. h->pict_type = h->slice_type;
  960. if (h->pict_type != AV_PICTURE_TYPE_B)
  961. FFSWAP(Picture*, s->next_pic, s->last_pic);
  962. av_frame_unref(&s->cur_pic->f);
  963. /* for skipping the frame */
  964. s->cur_pic->f.pict_type = h->pict_type;
  965. s->cur_pic->f.key_frame = (h->pict_type == AV_PICTURE_TYPE_I);
  966. ret = get_buffer(avctx, s->cur_pic);
  967. if (ret < 0)
  968. return ret;
  969. h->cur_pic_ptr = s->cur_pic;
  970. av_frame_unref(&h->cur_pic.f);
  971. h->cur_pic = *s->cur_pic;
  972. ret = av_frame_ref(&h->cur_pic.f, &s->cur_pic->f);
  973. if (ret < 0)
  974. return ret;
  975. for (i = 0; i < 16; i++) {
  976. h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * h->linesize * ((scan8[i] - scan8[0]) >> 3);
  977. h->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * h->linesize * ((scan8[i] - scan8[0]) >> 3);
  978. }
  979. for (i = 0; i < 16; i++) {
  980. h->block_offset[16 + i] =
  981. h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * h->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
  982. h->block_offset[48 + 16 + i] =
  983. h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7)) + 8 * h->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
  984. }
  985. if (h->pict_type != AV_PICTURE_TYPE_I) {
  986. if (!s->last_pic->f.data[0]) {
  987. av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
  988. ret = get_buffer(avctx, s->last_pic);
  989. if (ret < 0)
  990. return ret;
  991. memset(s->last_pic->f.data[0], 0, avctx->height * s->last_pic->f.linesize[0]);
  992. memset(s->last_pic->f.data[1], 0x80, (avctx->height / 2) *
  993. s->last_pic->f.linesize[1]);
  994. memset(s->last_pic->f.data[2], 0x80, (avctx->height / 2) *
  995. s->last_pic->f.linesize[2]);
  996. }
  997. if (h->pict_type == AV_PICTURE_TYPE_B && !s->next_pic->f.data[0]) {
  998. av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
  999. ret = get_buffer(avctx, s->next_pic);
  1000. if (ret < 0)
  1001. return ret;
  1002. memset(s->next_pic->f.data[0], 0, avctx->height * s->next_pic->f.linesize[0]);
  1003. memset(s->next_pic->f.data[1], 0x80, (avctx->height / 2) *
  1004. s->next_pic->f.linesize[1]);
  1005. memset(s->next_pic->f.data[2], 0x80, (avctx->height / 2) *
  1006. s->next_pic->f.linesize[2]);
  1007. }
  1008. }
  1009. if (avctx->debug & FF_DEBUG_PICT_INFO)
  1010. av_log(h->avctx, AV_LOG_DEBUG,
  1011. "%c hpel:%d, tpel:%d aqp:%d qp:%d, slice_num:%02X\n",
  1012. av_get_picture_type_char(h->pict_type),
  1013. s->halfpel_flag, s->thirdpel_flag,
  1014. s->adaptive_quant, h->qscale, h->slice_num);
  1015. if (avctx->skip_frame >= AVDISCARD_NONREF && h->pict_type == AV_PICTURE_TYPE_B ||
  1016. avctx->skip_frame >= AVDISCARD_NONKEY && h->pict_type != AV_PICTURE_TYPE_I ||
  1017. avctx->skip_frame >= AVDISCARD_ALL)
  1018. return 0;
  1019. if (s->next_p_frame_damaged) {
  1020. if (h->pict_type == AV_PICTURE_TYPE_B)
  1021. return 0;
  1022. else
  1023. s->next_p_frame_damaged = 0;
  1024. }
  1025. if (h->pict_type == AV_PICTURE_TYPE_B) {
  1026. h->frame_num_offset = h->slice_num - h->prev_frame_num;
  1027. if (h->frame_num_offset < 0)
  1028. h->frame_num_offset += 256;
  1029. if (h->frame_num_offset == 0 ||
  1030. h->frame_num_offset >= h->prev_frame_num_offset) {
  1031. av_log(h->avctx, AV_LOG_ERROR, "error in B-frame picture id\n");
  1032. return -1;
  1033. }
  1034. } else {
  1035. h->prev_frame_num = h->frame_num;
  1036. h->frame_num = h->slice_num;
  1037. h->prev_frame_num_offset = h->frame_num - h->prev_frame_num;
  1038. if (h->prev_frame_num_offset < 0)
  1039. h->prev_frame_num_offset += 256;
  1040. }
  1041. for (m = 0; m < 2; m++) {
  1042. int i;
  1043. for (i = 0; i < 4; i++) {
  1044. int j;
  1045. for (j = -1; j < 4; j++)
  1046. h->ref_cache[m][scan8[0] + 8 * i + j] = 1;
  1047. if (i < 3)
  1048. h->ref_cache[m][scan8[0] + 8 * i + j] = PART_NOT_AVAILABLE;
  1049. }
  1050. }
  1051. for (h->mb_y = 0; h->mb_y < h->mb_height; h->mb_y++) {
  1052. for (h->mb_x = 0; h->mb_x < h->mb_width; h->mb_x++) {
  1053. unsigned mb_type;
  1054. h->mb_xy = h->mb_x + h->mb_y * h->mb_stride;
  1055. if ((get_bits_count(&h->gb) + 7) >= h->gb.size_in_bits &&
  1056. ((get_bits_count(&h->gb) & 7) == 0 ||
  1057. show_bits(&h->gb, -get_bits_count(&h->gb) & 7) == 0)) {
  1058. skip_bits(&h->gb, s->next_slice_index - get_bits_count(&h->gb));
  1059. h->gb.size_in_bits = 8 * buf_size;
  1060. if (svq3_decode_slice_header(avctx))
  1061. return -1;
  1062. /* TODO: support s->mb_skip_run */
  1063. }
  1064. mb_type = svq3_get_ue_golomb(&h->gb);
  1065. if (h->pict_type == AV_PICTURE_TYPE_I)
  1066. mb_type += 8;
  1067. else if (h->pict_type == AV_PICTURE_TYPE_B && mb_type >= 4)
  1068. mb_type += 4;
  1069. if (mb_type > 33 || svq3_decode_mb(s, mb_type)) {
  1070. av_log(h->avctx, AV_LOG_ERROR,
  1071. "error while decoding MB %d %d\n", h->mb_x, h->mb_y);
  1072. return -1;
  1073. }
  1074. if (mb_type != 0)
  1075. ff_h264_hl_decode_mb(h);
  1076. if (h->pict_type != AV_PICTURE_TYPE_B && !h->low_delay)
  1077. h->cur_pic.mb_type[h->mb_x + h->mb_y * h->mb_stride] =
  1078. (h->pict_type == AV_PICTURE_TYPE_P && mb_type < 8) ? (mb_type - 1) : -1;
  1079. }
  1080. ff_draw_horiz_band(avctx, NULL, s->cur_pic, s->last_pic->f.data[0] ? s->last_pic : NULL,
  1081. 16 * h->mb_y, 16, h->picture_structure, 0, 0,
  1082. h->low_delay, h->mb_height * 16, h->mb_width * 16);
  1083. }
  1084. if (h->pict_type == AV_PICTURE_TYPE_B || h->low_delay)
  1085. ret = av_frame_ref(data, &s->cur_pic->f);
  1086. else if (s->last_pic->f.data[0])
  1087. ret = av_frame_ref(data, &s->last_pic->f);
  1088. if (ret < 0)
  1089. return ret;
  1090. /* Do not output the last pic after seeking. */
  1091. if (s->last_pic->f.data[0] || h->low_delay)
  1092. *got_frame = 1;
  1093. if (h->pict_type != AV_PICTURE_TYPE_B) {
  1094. FFSWAP(Picture*, s->cur_pic, s->next_pic);
  1095. } else {
  1096. av_frame_unref(&s->cur_pic->f);
  1097. }
  1098. return buf_size;
  1099. }
  1100. static int svq3_decode_end(AVCodecContext *avctx)
  1101. {
  1102. SVQ3Context *s = avctx->priv_data;
  1103. H264Context *h = &s->h;
  1104. free_picture(avctx, s->cur_pic);
  1105. free_picture(avctx, s->next_pic);
  1106. free_picture(avctx, s->last_pic);
  1107. av_freep(&s->cur_pic);
  1108. av_freep(&s->next_pic);
  1109. av_freep(&s->last_pic);
  1110. av_frame_unref(&h->cur_pic.f);
  1111. ff_h264_free_context(h);
  1112. return 0;
  1113. }
  1114. AVCodec ff_svq3_decoder = {
  1115. .name = "svq3",
  1116. .type = AVMEDIA_TYPE_VIDEO,
  1117. .id = AV_CODEC_ID_SVQ3,
  1118. .priv_data_size = sizeof(SVQ3Context),
  1119. .init = svq3_decode_init,
  1120. .close = svq3_decode_end,
  1121. .decode = svq3_decode_frame,
  1122. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND |
  1123. CODEC_CAP_DR1 |
  1124. CODEC_CAP_DELAY,
  1125. .long_name = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 3 / Sorenson Video 3 / SVQ3"),
  1126. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P,
  1127. AV_PIX_FMT_NONE},
  1128. };