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.

1147 lines
41KB

  1. /*
  2. * Copyright (c) 2003 The FFmpeg Project
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /*
  21. * How to use this decoder:
  22. * SVQ3 data is transported within Apple Quicktime files. Quicktime files
  23. * have stsd atoms to describe media trak properties. A stsd atom for a
  24. * video trak contains 1 or more ImageDescription atoms. These atoms begin
  25. * with the 4-byte length of the atom followed by the codec fourcc. Some
  26. * decoders need information in this atom to operate correctly. Such
  27. * is the case with SVQ3. In order to get the best use out of this decoder,
  28. * the calling app must make the SVQ3 ImageDescription atom available
  29. * via the AVCodecContext's extradata[_size] field:
  30. *
  31. * AVCodecContext.extradata = pointer to ImageDescription, first characters
  32. * are expected to be 'S', 'V', 'Q', and '3', NOT the 4-byte atom length
  33. * AVCodecContext.extradata_size = size of ImageDescription atom memory
  34. * buffer (which will be the same as the ImageDescription atom size field
  35. * from the QT file, minus 4 bytes since the length is missing)
  36. *
  37. * You will know you have these parameters passed correctly when the decoder
  38. * correctly decodes this file:
  39. * http://samples.mplayerhq.hu/V-codecs/SVQ3/Vertical400kbit.sorenson3.mov
  40. */
  41. #include "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. /**
  56. * @file
  57. * svq3 decoder.
  58. */
  59. typedef struct {
  60. H264Context h;
  61. int halfpel_flag;
  62. int thirdpel_flag;
  63. int unknown_flag;
  64. int next_slice_index;
  65. uint32_t watermark_key;
  66. uint8_t *buf;
  67. int buf_size;
  68. } SVQ3Context;
  69. #define FULLPEL_MODE 1
  70. #define HALFPEL_MODE 2
  71. #define THIRDPEL_MODE 3
  72. #define PREDICT_MODE 4
  73. /* dual scan (from some older h264 draft)
  74. o-->o-->o o
  75. | /|
  76. o o o / o
  77. | / | |/ |
  78. o o o o
  79. /
  80. o-->o-->o-->o
  81. */
  82. static const uint8_t svq3_scan[16] = {
  83. 0+0*4, 1+0*4, 2+0*4, 2+1*4,
  84. 2+2*4, 3+0*4, 3+1*4, 3+2*4,
  85. 0+1*4, 0+2*4, 1+1*4, 1+2*4,
  86. 0+3*4, 1+3*4, 2+3*4, 3+3*4,
  87. };
  88. static const uint8_t svq3_pred_0[25][2] = {
  89. { 0, 0 },
  90. { 1, 0 }, { 0, 1 },
  91. { 0, 2 }, { 1, 1 }, { 2, 0 },
  92. { 3, 0 }, { 2, 1 }, { 1, 2 }, { 0, 3 },
  93. { 0, 4 }, { 1, 3 }, { 2, 2 }, { 3, 1 }, { 4, 0 },
  94. { 4, 1 }, { 3, 2 }, { 2, 3 }, { 1, 4 },
  95. { 2, 4 }, { 3, 3 }, { 4, 2 },
  96. { 4, 3 }, { 3, 4 },
  97. { 4, 4 }
  98. };
  99. static const int8_t svq3_pred_1[6][6][5] = {
  100. { { 2,-1,-1,-1,-1 }, { 2, 1,-1,-1,-1 }, { 1, 2,-1,-1,-1 },
  101. { 2, 1,-1,-1,-1 }, { 1, 2,-1,-1,-1 }, { 1, 2,-1,-1,-1 } },
  102. { { 0, 2,-1,-1,-1 }, { 0, 2, 1, 4, 3 }, { 0, 1, 2, 4, 3 },
  103. { 0, 2, 1, 4, 3 }, { 2, 0, 1, 3, 4 }, { 0, 4, 2, 1, 3 } },
  104. { { 2, 0,-1,-1,-1 }, { 2, 1, 0, 4, 3 }, { 1, 2, 4, 0, 3 },
  105. { 2, 1, 0, 4, 3 }, { 2, 1, 4, 3, 0 }, { 1, 2, 4, 0, 3 } },
  106. { { 2, 0,-1,-1,-1 }, { 2, 0, 1, 4, 3 }, { 1, 2, 0, 4, 3 },
  107. { 2, 1, 0, 4, 3 }, { 2, 1, 3, 4, 0 }, { 2, 4, 1, 0, 3 } },
  108. { { 0, 2,-1,-1,-1 }, { 0, 2, 1, 3, 4 }, { 1, 2, 3, 0, 4 },
  109. { 2, 0, 1, 3, 4 }, { 2, 1, 3, 0, 4 }, { 2, 0, 4, 3, 1 } },
  110. { { 0, 2,-1,-1,-1 }, { 0, 2, 4, 1, 3 }, { 1, 4, 2, 0, 3 },
  111. { 4, 2, 0, 1, 3 }, { 2, 0, 1, 4, 3 }, { 4, 2, 1, 0, 3 } },
  112. };
  113. static const struct { uint8_t run; uint8_t level; } svq3_dct_tables[2][16] = {
  114. { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 2, 1 }, { 0, 2 }, { 3, 1 }, { 4, 1 }, { 5, 1 },
  115. { 0, 3 }, { 1, 2 }, { 2, 2 }, { 6, 1 }, { 7, 1 }, { 8, 1 }, { 9, 1 }, { 0, 4 } },
  116. { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 0, 2 }, { 2, 1 }, { 0, 3 }, { 0, 4 }, { 0, 5 },
  117. { 3, 1 }, { 4, 1 }, { 1, 2 }, { 1, 3 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, { 0, 9 } }
  118. };
  119. static const uint32_t svq3_dequant_coeff[32] = {
  120. 3881, 4351, 4890, 5481, 6154, 6914, 7761, 8718,
  121. 9781, 10987, 12339, 13828, 15523, 17435, 19561, 21873,
  122. 24552, 27656, 30847, 34870, 38807, 43747, 49103, 54683,
  123. 61694, 68745, 77615, 89113,100253,109366,126635,141533
  124. };
  125. void ff_svq3_luma_dc_dequant_idct_c(DCTELEM *output, DCTELEM *input, int qp){
  126. const int qmul = svq3_dequant_coeff[qp];
  127. #define stride 16
  128. int i;
  129. int temp[16];
  130. static const uint8_t x_offset[4]={0, 1*stride, 4*stride, 5*stride};
  131. for(i=0; i<4; i++){
  132. const int z0 = 13*(input[4*i+0] + input[4*i+2]);
  133. const int z1 = 13*(input[4*i+0] - input[4*i+2]);
  134. const int z2 = 7* input[4*i+1] - 17*input[4*i+3];
  135. const int z3 = 17* input[4*i+1] + 7*input[4*i+3];
  136. temp[4*i+0] = z0+z3;
  137. temp[4*i+1] = z1+z2;
  138. temp[4*i+2] = z1-z2;
  139. temp[4*i+3] = z0-z3;
  140. }
  141. for(i=0; i<4; i++){
  142. const int offset= x_offset[i];
  143. const int z0= 13*(temp[4*0+i] + temp[4*2+i]);
  144. const int z1= 13*(temp[4*0+i] - temp[4*2+i]);
  145. const int z2= 7* temp[4*1+i] - 17*temp[4*3+i];
  146. const int z3= 17* temp[4*1+i] + 7*temp[4*3+i];
  147. output[stride* 0+offset] = ((z0 + z3)*qmul + 0x80000) >> 20;
  148. output[stride* 2+offset] = ((z1 + z2)*qmul + 0x80000) >> 20;
  149. output[stride* 8+offset] = ((z1 - z2)*qmul + 0x80000) >> 20;
  150. output[stride*10+offset] = ((z0 - z3)*qmul + 0x80000) >> 20;
  151. }
  152. }
  153. #undef stride
  154. void ff_svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp,
  155. int dc)
  156. {
  157. const int qmul = svq3_dequant_coeff[qp];
  158. int i;
  159. if (dc) {
  160. dc = 13*13*((dc == 1) ? 1538*block[0] : ((qmul*(block[0] >> 3)) / 2));
  161. block[0] = 0;
  162. }
  163. for (i = 0; i < 4; i++) {
  164. const int z0 = 13*(block[0 + 4*i] + block[2 + 4*i]);
  165. const int z1 = 13*(block[0 + 4*i] - block[2 + 4*i]);
  166. const int z2 = 7* block[1 + 4*i] - 17*block[3 + 4*i];
  167. const int z3 = 17* block[1 + 4*i] + 7*block[3 + 4*i];
  168. block[0 + 4*i] = z0 + z3;
  169. block[1 + 4*i] = z1 + z2;
  170. block[2 + 4*i] = z1 - z2;
  171. block[3 + 4*i] = z0 - z3;
  172. }
  173. for (i = 0; i < 4; i++) {
  174. const int z0 = 13*(block[i + 4*0] + block[i + 4*2]);
  175. const int z1 = 13*(block[i + 4*0] - block[i + 4*2]);
  176. const int z2 = 7* block[i + 4*1] - 17*block[i + 4*3];
  177. const int z3 = 17* block[i + 4*1] + 7*block[i + 4*3];
  178. const int rr = (dc + 0x80000);
  179. dst[i + stride*0] = av_clip_uint8( dst[i + stride*0] + (((z0 + z3)*qmul + rr) >> 20) );
  180. dst[i + stride*1] = av_clip_uint8( dst[i + stride*1] + (((z1 + z2)*qmul + rr) >> 20) );
  181. dst[i + stride*2] = av_clip_uint8( dst[i + stride*2] + (((z1 - z2)*qmul + rr) >> 20) );
  182. dst[i + stride*3] = av_clip_uint8( dst[i + stride*3] + (((z0 - z3)*qmul + rr) >> 20) );
  183. }
  184. }
  185. static inline int svq3_decode_block(GetBitContext *gb, DCTELEM *block,
  186. int index, const int type)
  187. {
  188. static const uint8_t *const scan_patterns[4] =
  189. { luma_dc_zigzag_scan, zigzag_scan, svq3_scan, chroma_dc_scan };
  190. int run, level, sign, vlc, limit;
  191. const int intra = (3 * type) >> 2;
  192. const uint8_t *const scan = scan_patterns[type];
  193. for (limit = (16 >> intra); index < 16; index = limit, limit += 8) {
  194. for (; (vlc = svq3_get_ue_golomb(gb)) != 0; index++) {
  195. if (vlc < 0)
  196. return -1;
  197. sign = (vlc & 0x1) - 1;
  198. vlc = (vlc + 1) >> 1;
  199. if (type == 3) {
  200. if (vlc < 3) {
  201. run = 0;
  202. level = vlc;
  203. } else if (vlc < 4) {
  204. run = 1;
  205. level = 1;
  206. } else {
  207. run = (vlc & 0x3);
  208. level = ((vlc + 9) >> 2) - run;
  209. }
  210. } else {
  211. if (vlc < 16U) {
  212. run = svq3_dct_tables[intra][vlc].run;
  213. level = svq3_dct_tables[intra][vlc].level;
  214. } else if (intra) {
  215. run = (vlc & 0x7);
  216. level = (vlc >> 3) + ((run == 0) ? 8 : ((run < 2) ? 2 : ((run < 5) ? 0 : -1)));
  217. } else {
  218. run = (vlc & 0xF);
  219. level = (vlc >> 4) + ((run == 0) ? 4 : ((run < 3) ? 2 : ((run < 10) ? 1 : 0)));
  220. }
  221. }
  222. if ((index += run) >= limit)
  223. return -1;
  224. block[scan[index]] = (level ^ sign) - sign;
  225. }
  226. if (type != 2) {
  227. break;
  228. }
  229. }
  230. return 0;
  231. }
  232. static inline void svq3_mc_dir_part(MpegEncContext *s,
  233. int x, int y, int width, int height,
  234. int mx, int my, int dxy,
  235. int thirdpel, int dir, int avg)
  236. {
  237. const Picture *pic = (dir == 0) ? &s->last_picture : &s->next_picture;
  238. uint8_t *src, *dest;
  239. int i, emu = 0;
  240. int blocksize = 2 - (width>>3); //16->0, 8->1, 4->2
  241. mx += x;
  242. my += y;
  243. if (mx < 0 || mx >= (s->h_edge_pos - width - 1) ||
  244. my < 0 || my >= (s->v_edge_pos - height - 1)) {
  245. if ((s->flags & CODEC_FLAG_EMU_EDGE)) {
  246. emu = 1;
  247. }
  248. mx = av_clip (mx, -16, (s->h_edge_pos - width + 15));
  249. my = av_clip (my, -16, (s->v_edge_pos - height + 15));
  250. }
  251. /* form component predictions */
  252. dest = s->current_picture.f.data[0] + x + y*s->linesize;
  253. src = pic->f.data[0] + mx + my*s->linesize;
  254. if (emu) {
  255. s->dsp.emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, (width + 1), (height + 1),
  256. mx, my, s->h_edge_pos, s->v_edge_pos);
  257. src = s->edge_emu_buffer;
  258. }
  259. if (thirdpel)
  260. (avg ? s->dsp.avg_tpel_pixels_tab : s->dsp.put_tpel_pixels_tab)[dxy](dest, src, s->linesize, width, height);
  261. else
  262. (avg ? s->dsp.avg_pixels_tab : s->dsp.put_pixels_tab)[blocksize][dxy](dest, src, s->linesize, height);
  263. if (!(s->flags & CODEC_FLAG_GRAY)) {
  264. mx = (mx + (mx < (int) x)) >> 1;
  265. my = (my + (my < (int) y)) >> 1;
  266. width = (width >> 1);
  267. height = (height >> 1);
  268. blocksize++;
  269. for (i = 1; i < 3; i++) {
  270. dest = s->current_picture.f.data[i] + (x >> 1) + (y >> 1) * s->uvlinesize;
  271. src = pic->f.data[i] + mx + my * s->uvlinesize;
  272. if (emu) {
  273. s->dsp.emulated_edge_mc(s->edge_emu_buffer, src, s->uvlinesize, (width + 1), (height + 1),
  274. mx, my, (s->h_edge_pos >> 1), (s->v_edge_pos >> 1));
  275. src = s->edge_emu_buffer;
  276. }
  277. if (thirdpel)
  278. (avg ? s->dsp.avg_tpel_pixels_tab : s->dsp.put_tpel_pixels_tab)[dxy](dest, src, s->uvlinesize, width, height);
  279. else
  280. (avg ? s->dsp.avg_pixels_tab : s->dsp.put_pixels_tab)[blocksize][dxy](dest, src, s->uvlinesize, height);
  281. }
  282. }
  283. }
  284. static inline int svq3_mc_dir(H264Context *h, int size, int mode, int dir,
  285. int avg)
  286. {
  287. int i, j, k, mx, my, dx, dy, x, y;
  288. MpegEncContext *const s = (MpegEncContext *) h;
  289. const int part_width = ((size & 5) == 4) ? 4 : 16 >> (size & 1);
  290. const int part_height = 16 >> ((unsigned) (size + 1) / 3);
  291. const int extra_width = (mode == PREDICT_MODE) ? -16*6 : 0;
  292. const int h_edge_pos = 6*(s->h_edge_pos - part_width ) - extra_width;
  293. const int v_edge_pos = 6*(s->v_edge_pos - part_height) - extra_width;
  294. for (i = 0; i < 16; i += part_height) {
  295. for (j = 0; j < 16; j += part_width) {
  296. const int b_xy = (4*s->mb_x + (j >> 2)) + (4*s->mb_y + (i >> 2))*h->b_stride;
  297. int dxy;
  298. x = 16*s->mb_x + j;
  299. y = 16*s->mb_y + i;
  300. k = ((j >> 2) & 1) + ((i >> 1) & 2) + ((j >> 1) & 4) + (i & 8);
  301. if (mode != PREDICT_MODE) {
  302. pred_motion(h, k, (part_width >> 2), dir, 1, &mx, &my);
  303. } else {
  304. mx = s->next_picture.f.motion_val[0][b_xy][0] << 1;
  305. my = s->next_picture.f.motion_val[0][b_xy][1] << 1;
  306. if (dir == 0) {
  307. mx = ((mx * h->frame_num_offset) / h->prev_frame_num_offset + 1) >> 1;
  308. my = ((my * h->frame_num_offset) / h->prev_frame_num_offset + 1) >> 1;
  309. } else {
  310. mx = ((mx * (h->frame_num_offset - h->prev_frame_num_offset)) / h->prev_frame_num_offset + 1) >> 1;
  311. my = ((my * (h->frame_num_offset - h->prev_frame_num_offset)) / h->prev_frame_num_offset + 1) >> 1;
  312. }
  313. }
  314. /* clip motion vector prediction to frame border */
  315. mx = av_clip(mx, extra_width - 6*x, h_edge_pos - 6*x);
  316. my = av_clip(my, extra_width - 6*y, v_edge_pos - 6*y);
  317. /* get (optional) motion vector differential */
  318. if (mode == PREDICT_MODE) {
  319. dx = dy = 0;
  320. } else {
  321. dy = svq3_get_se_golomb(&s->gb);
  322. dx = svq3_get_se_golomb(&s->gb);
  323. if (dx == INVALID_VLC || dy == INVALID_VLC) {
  324. av_log(h->s.avctx, AV_LOG_ERROR, "invalid MV vlc\n");
  325. return -1;
  326. }
  327. }
  328. /* compute motion vector */
  329. if (mode == THIRDPEL_MODE) {
  330. int fx, fy;
  331. mx = ((mx + 1)>>1) + dx;
  332. my = ((my + 1)>>1) + dy;
  333. fx = ((unsigned)(mx + 0x3000))/3 - 0x1000;
  334. fy = ((unsigned)(my + 0x3000))/3 - 0x1000;
  335. dxy = (mx - 3*fx) + 4*(my - 3*fy);
  336. svq3_mc_dir_part(s, x, y, part_width, part_height, fx, fy, dxy, 1, dir, avg);
  337. mx += mx;
  338. my += my;
  339. } else if (mode == HALFPEL_MODE || mode == PREDICT_MODE) {
  340. mx = ((unsigned)(mx + 1 + 0x3000))/3 + dx - 0x1000;
  341. my = ((unsigned)(my + 1 + 0x3000))/3 + dy - 0x1000;
  342. dxy = (mx&1) + 2*(my&1);
  343. svq3_mc_dir_part(s, x, y, part_width, part_height, mx>>1, my>>1, dxy, 0, dir, avg);
  344. mx *= 3;
  345. my *= 3;
  346. } else {
  347. mx = ((unsigned)(mx + 3 + 0x6000))/6 + dx - 0x1000;
  348. my = ((unsigned)(my + 3 + 0x6000))/6 + dy - 0x1000;
  349. svq3_mc_dir_part(s, x, y, part_width, part_height, mx, my, 0, 0, dir, avg);
  350. mx *= 6;
  351. my *= 6;
  352. }
  353. /* update mv_cache */
  354. if (mode != PREDICT_MODE) {
  355. int32_t mv = pack16to32(mx,my);
  356. if (part_height == 8 && i < 8) {
  357. *(int32_t *) h->mv_cache[dir][scan8[k] + 1*8] = mv;
  358. if (part_width == 8 && j < 8) {
  359. *(int32_t *) h->mv_cache[dir][scan8[k] + 1 + 1*8] = mv;
  360. }
  361. }
  362. if (part_width == 8 && j < 8) {
  363. *(int32_t *) h->mv_cache[dir][scan8[k] + 1] = mv;
  364. }
  365. if (part_width == 4 || part_height == 4) {
  366. *(int32_t *) h->mv_cache[dir][scan8[k]] = mv;
  367. }
  368. }
  369. /* write back motion vectors */
  370. fill_rectangle(s->current_picture.f.motion_val[dir][b_xy],
  371. part_width >> 2, part_height >> 2, h->b_stride,
  372. pack16to32(mx, my), 4);
  373. }
  374. }
  375. return 0;
  376. }
  377. static int svq3_decode_mb(SVQ3Context *svq3, unsigned int mb_type)
  378. {
  379. H264Context *h = &svq3->h;
  380. int i, j, k, m, dir, mode;
  381. int cbp = 0;
  382. uint32_t vlc;
  383. int8_t *top, *left;
  384. MpegEncContext *const s = (MpegEncContext *) h;
  385. const int mb_xy = h->mb_xy;
  386. const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  387. h->top_samples_available = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
  388. h->left_samples_available = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
  389. h->topright_samples_available = 0xFFFF;
  390. if (mb_type == 0) { /* SKIP */
  391. if (s->pict_type == AV_PICTURE_TYPE_P || s->next_picture.f.mb_type[mb_xy] == -1) {
  392. svq3_mc_dir_part(s, 16*s->mb_x, 16*s->mb_y, 16, 16, 0, 0, 0, 0, 0, 0);
  393. if (s->pict_type == AV_PICTURE_TYPE_B) {
  394. svq3_mc_dir_part(s, 16*s->mb_x, 16*s->mb_y, 16, 16, 0, 0, 0, 0, 1, 1);
  395. }
  396. mb_type = MB_TYPE_SKIP;
  397. } else {
  398. mb_type = FFMIN(s->next_picture.f.mb_type[mb_xy], 6);
  399. if (svq3_mc_dir(h, mb_type, PREDICT_MODE, 0, 0) < 0)
  400. return -1;
  401. if (svq3_mc_dir(h, mb_type, PREDICT_MODE, 1, 1) < 0)
  402. return -1;
  403. mb_type = MB_TYPE_16x16;
  404. }
  405. } else if (mb_type < 8) { /* INTER */
  406. if (svq3->thirdpel_flag && svq3->halfpel_flag == !get_bits1 (&s->gb)) {
  407. mode = THIRDPEL_MODE;
  408. } else if (svq3->halfpel_flag && svq3->thirdpel_flag == !get_bits1 (&s->gb)) {
  409. mode = HALFPEL_MODE;
  410. } else {
  411. mode = FULLPEL_MODE;
  412. }
  413. /* fill caches */
  414. /* note ref_cache should contain here:
  415. ????????
  416. ???11111
  417. N??11111
  418. N??11111
  419. N??11111
  420. */
  421. for (m = 0; m < 2; m++) {
  422. if (s->mb_x > 0 && h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - 1]+6] != -1) {
  423. for (i = 0; i < 4; i++) {
  424. *(uint32_t *) h->mv_cache[m][scan8[0] - 1 + i*8] = *(uint32_t *) s->current_picture.f.motion_val[m][b_xy - 1 + i*h->b_stride];
  425. }
  426. } else {
  427. for (i = 0; i < 4; i++) {
  428. *(uint32_t *) h->mv_cache[m][scan8[0] - 1 + i*8] = 0;
  429. }
  430. }
  431. if (s->mb_y > 0) {
  432. memcpy(h->mv_cache[m][scan8[0] - 1*8], s->current_picture.f.motion_val[m][b_xy - h->b_stride], 4*2*sizeof(int16_t));
  433. memset(&h->ref_cache[m][scan8[0] - 1*8], (h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1, 4);
  434. if (s->mb_x < (s->mb_width - 1)) {
  435. *(uint32_t *) h->mv_cache[m][scan8[0] + 4 - 1*8] = *(uint32_t *) s->current_picture.f.motion_val[m][b_xy - h->b_stride + 4];
  436. h->ref_cache[m][scan8[0] + 4 - 1*8] =
  437. (h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride + 1]+6] == -1 ||
  438. h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride ] ] == -1) ? PART_NOT_AVAILABLE : 1;
  439. }else
  440. h->ref_cache[m][scan8[0] + 4 - 1*8] = PART_NOT_AVAILABLE;
  441. if (s->mb_x > 0) {
  442. *(uint32_t *) h->mv_cache[m][scan8[0] - 1 - 1*8] = *(uint32_t *) s->current_picture.f.motion_val[m][b_xy - h->b_stride - 1];
  443. h->ref_cache[m][scan8[0] - 1 - 1*8] = (h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride - 1]+3] == -1) ? PART_NOT_AVAILABLE : 1;
  444. }else
  445. h->ref_cache[m][scan8[0] - 1 - 1*8] = PART_NOT_AVAILABLE;
  446. }else
  447. memset(&h->ref_cache[m][scan8[0] - 1*8 - 1], PART_NOT_AVAILABLE, 8);
  448. if (s->pict_type != AV_PICTURE_TYPE_B)
  449. break;
  450. }
  451. /* decode motion vector(s) and form prediction(s) */
  452. if (s->pict_type == AV_PICTURE_TYPE_P) {
  453. if (svq3_mc_dir(h, (mb_type - 1), mode, 0, 0) < 0)
  454. return -1;
  455. } else { /* AV_PICTURE_TYPE_B */
  456. if (mb_type != 2) {
  457. if (svq3_mc_dir(h, 0, mode, 0, 0) < 0)
  458. return -1;
  459. } else {
  460. for (i = 0; i < 4; i++) {
  461. memset(s->current_picture.f.motion_val[0][b_xy + i*h->b_stride], 0, 4*2*sizeof(int16_t));
  462. }
  463. }
  464. if (mb_type != 1) {
  465. if (svq3_mc_dir(h, 0, mode, 1, (mb_type == 3)) < 0)
  466. return -1;
  467. } else {
  468. for (i = 0; i < 4; i++) {
  469. memset(s->current_picture.f.motion_val[1][b_xy + i*h->b_stride], 0, 4*2*sizeof(int16_t));
  470. }
  471. }
  472. }
  473. mb_type = MB_TYPE_16x16;
  474. } else if (mb_type == 8 || mb_type == 33) { /* INTRA4x4 */
  475. memset(h->intra4x4_pred_mode_cache, -1, 8*5*sizeof(int8_t));
  476. if (mb_type == 8) {
  477. if (s->mb_x > 0) {
  478. for (i = 0; i < 4; i++) {
  479. h->intra4x4_pred_mode_cache[scan8[0] - 1 + i*8] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - 1]+6-i];
  480. }
  481. if (h->intra4x4_pred_mode_cache[scan8[0] - 1] == -1) {
  482. h->left_samples_available = 0x5F5F;
  483. }
  484. }
  485. if (s->mb_y > 0) {
  486. h->intra4x4_pred_mode_cache[4+8*0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride]+0];
  487. h->intra4x4_pred_mode_cache[5+8*0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride]+1];
  488. h->intra4x4_pred_mode_cache[6+8*0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride]+2];
  489. h->intra4x4_pred_mode_cache[7+8*0] = h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride]+3];
  490. if (h->intra4x4_pred_mode_cache[4+8*0] == -1) {
  491. h->top_samples_available = 0x33FF;
  492. }
  493. }
  494. /* decode prediction codes for luma blocks */
  495. for (i = 0; i < 16; i+=2) {
  496. vlc = svq3_get_ue_golomb(&s->gb);
  497. if (vlc >= 25U){
  498. av_log(h->s.avctx, AV_LOG_ERROR, "luma prediction:%d\n", vlc);
  499. return -1;
  500. }
  501. left = &h->intra4x4_pred_mode_cache[scan8[i] - 1];
  502. top = &h->intra4x4_pred_mode_cache[scan8[i] - 8];
  503. left[1] = svq3_pred_1[top[0] + 1][left[0] + 1][svq3_pred_0[vlc][0]];
  504. left[2] = svq3_pred_1[top[1] + 1][left[1] + 1][svq3_pred_0[vlc][1]];
  505. if (left[1] == -1 || left[2] == -1){
  506. av_log(h->s.avctx, AV_LOG_ERROR, "weird prediction\n");
  507. return -1;
  508. }
  509. }
  510. } else { /* mb_type == 33, DC_128_PRED block type */
  511. for (i = 0; i < 4; i++) {
  512. memset(&h->intra4x4_pred_mode_cache[scan8[0] + 8*i], DC_PRED, 4);
  513. }
  514. }
  515. write_back_intra_pred_mode(h);
  516. if (mb_type == 8) {
  517. ff_h264_check_intra4x4_pred_mode(h);
  518. h->top_samples_available = (s->mb_y == 0) ? 0x33FF : 0xFFFF;
  519. h->left_samples_available = (s->mb_x == 0) ? 0x5F5F : 0xFFFF;
  520. } else {
  521. for (i = 0; i < 4; i++) {
  522. memset(&h->intra4x4_pred_mode_cache[scan8[0] + 8*i], DC_128_PRED, 4);
  523. }
  524. h->top_samples_available = 0x33FF;
  525. h->left_samples_available = 0x5F5F;
  526. }
  527. mb_type = MB_TYPE_INTRA4x4;
  528. } else { /* INTRA16x16 */
  529. dir = i_mb_type_info[mb_type - 8].pred_mode;
  530. dir = (dir >> 1) ^ 3*(dir & 1) ^ 1;
  531. if ((h->intra16x16_pred_mode = ff_h264_check_intra_pred_mode(h, dir, 0)) == -1){
  532. av_log(h->s.avctx, AV_LOG_ERROR, "check_intra_pred_mode = -1\n");
  533. return -1;
  534. }
  535. cbp = i_mb_type_info[mb_type - 8].cbp;
  536. mb_type = MB_TYPE_INTRA16x16;
  537. }
  538. if (!IS_INTER(mb_type) && s->pict_type != AV_PICTURE_TYPE_I) {
  539. for (i = 0; i < 4; i++) {
  540. memset(s->current_picture.f.motion_val[0][b_xy + i*h->b_stride], 0, 4*2*sizeof(int16_t));
  541. }
  542. if (s->pict_type == AV_PICTURE_TYPE_B) {
  543. for (i = 0; i < 4; i++) {
  544. memset(s->current_picture.f.motion_val[1][b_xy + i*h->b_stride], 0, 4*2*sizeof(int16_t));
  545. }
  546. }
  547. }
  548. if (!IS_INTRA4x4(mb_type)) {
  549. memset(h->intra4x4_pred_mode+h->mb2br_xy[mb_xy], DC_PRED, 8);
  550. }
  551. if (!IS_SKIP(mb_type) || s->pict_type == AV_PICTURE_TYPE_B) {
  552. memset(h->non_zero_count_cache + 8, 0, 14*8*sizeof(uint8_t));
  553. s->dsp.clear_blocks(h->mb+ 0);
  554. s->dsp.clear_blocks(h->mb+384);
  555. }
  556. if (!IS_INTRA16x16(mb_type) && (!IS_SKIP(mb_type) || s->pict_type == AV_PICTURE_TYPE_B)) {
  557. if ((vlc = svq3_get_ue_golomb(&s->gb)) >= 48U){
  558. av_log(h->s.avctx, AV_LOG_ERROR, "cbp_vlc=%d\n", vlc);
  559. return -1;
  560. }
  561. cbp = IS_INTRA(mb_type) ? golomb_to_intra4x4_cbp[vlc] : golomb_to_inter_cbp[vlc];
  562. }
  563. if (IS_INTRA16x16(mb_type) || (s->pict_type != AV_PICTURE_TYPE_I && s->adaptive_quant && cbp)) {
  564. s->qscale += svq3_get_se_golomb(&s->gb);
  565. if (s->qscale > 31U){
  566. av_log(h->s.avctx, AV_LOG_ERROR, "qscale:%d\n", s->qscale);
  567. return -1;
  568. }
  569. }
  570. if (IS_INTRA16x16(mb_type)) {
  571. AV_ZERO128(h->mb_luma_dc[0]+0);
  572. AV_ZERO128(h->mb_luma_dc[0]+8);
  573. if (svq3_decode_block(&s->gb, *h->mb_luma_dc, 0, 1)){
  574. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding intra luma dc\n");
  575. return -1;
  576. }
  577. }
  578. if (cbp) {
  579. const int index = IS_INTRA16x16(mb_type) ? 1 : 0;
  580. const int type = ((s->qscale < 24 && IS_INTRA4x4(mb_type)) ? 2 : 1);
  581. for (i = 0; i < 4; i++) {
  582. if ((cbp & (1 << i))) {
  583. for (j = 0; j < 4; j++) {
  584. k = index ? ((j&1) + 2*(i&1) + 2*(j&2) + 4*(i&2)) : (4*i + j);
  585. h->non_zero_count_cache[ scan8[k] ] = 1;
  586. if (svq3_decode_block(&s->gb, &h->mb[16*k], index, type)){
  587. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding block\n");
  588. return -1;
  589. }
  590. }
  591. }
  592. }
  593. if ((cbp & 0x30)) {
  594. for (i = 1; i < 3; ++i) {
  595. if (svq3_decode_block(&s->gb, &h->mb[16*16*i], 0, 3)){
  596. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding chroma dc block\n");
  597. return -1;
  598. }
  599. }
  600. if ((cbp & 0x20)) {
  601. for (i = 1; i < 3; i++) {
  602. for (j = 0; j < 4; j++) {
  603. k = 16*i + j;
  604. h->non_zero_count_cache[ scan8[k] ] = 1;
  605. if (svq3_decode_block(&s->gb, &h->mb[16*k], 1, 1)){
  606. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding chroma ac block\n");
  607. return -1;
  608. }
  609. }
  610. }
  611. }
  612. }
  613. }
  614. h->cbp= cbp;
  615. s->current_picture.f.mb_type[mb_xy] = mb_type;
  616. if (IS_INTRA(mb_type)) {
  617. h->chroma_pred_mode = ff_h264_check_intra_pred_mode(h, DC_PRED8x8, 1);
  618. }
  619. return 0;
  620. }
  621. static int svq3_decode_slice_header(AVCodecContext *avctx)
  622. {
  623. SVQ3Context *svq3 = avctx->priv_data;
  624. H264Context *h = &svq3->h;
  625. MpegEncContext *s = &h->s;
  626. const int mb_xy = h->mb_xy;
  627. int i, header;
  628. header = get_bits(&s->gb, 8);
  629. if (((header & 0x9F) != 1 && (header & 0x9F) != 2) || (header & 0x60) == 0) {
  630. /* TODO: what? */
  631. av_log(avctx, AV_LOG_ERROR, "unsupported slice header (%02X)\n", header);
  632. return -1;
  633. } else {
  634. int length = (header >> 5) & 3;
  635. svq3->next_slice_index = get_bits_count(&s->gb) + 8*show_bits(&s->gb, 8*length) + 8*length;
  636. if (svq3->next_slice_index > s->gb.size_in_bits) {
  637. av_log(avctx, AV_LOG_ERROR, "slice after bitstream end\n");
  638. return -1;
  639. }
  640. s->gb.size_in_bits = svq3->next_slice_index - 8*(length - 1);
  641. skip_bits(&s->gb, 8);
  642. if (svq3->watermark_key) {
  643. uint32_t header = AV_RL32(&s->gb.buffer[(get_bits_count(&s->gb)>>3)+1]);
  644. AV_WL32(&s->gb.buffer[(get_bits_count(&s->gb)>>3)+1], header ^ svq3->watermark_key);
  645. }
  646. if (length > 0) {
  647. memcpy((uint8_t *) &s->gb.buffer[get_bits_count(&s->gb) >> 3],
  648. &s->gb.buffer[s->gb.size_in_bits >> 3], (length - 1));
  649. }
  650. skip_bits_long(&s->gb, 0);
  651. }
  652. if ((i = svq3_get_ue_golomb(&s->gb)) >= 3U){
  653. av_log(h->s.avctx, AV_LOG_ERROR, "illegal slice type %d \n", i);
  654. return -1;
  655. }
  656. h->slice_type = golomb_to_pict_type[i];
  657. if ((header & 0x9F) == 2) {
  658. i = (s->mb_num < 64) ? 6 : (1 + av_log2 (s->mb_num - 1));
  659. s->mb_skip_run = get_bits(&s->gb, i) - (s->mb_x + (s->mb_y * s->mb_width));
  660. } else {
  661. skip_bits1(&s->gb);
  662. s->mb_skip_run = 0;
  663. }
  664. h->slice_num = get_bits(&s->gb, 8);
  665. s->qscale = get_bits(&s->gb, 5);
  666. s->adaptive_quant = get_bits1(&s->gb);
  667. /* unknown fields */
  668. skip_bits1(&s->gb);
  669. if (svq3->unknown_flag) {
  670. skip_bits1(&s->gb);
  671. }
  672. skip_bits1(&s->gb);
  673. skip_bits(&s->gb, 2);
  674. while (get_bits1(&s->gb)) {
  675. skip_bits(&s->gb, 8);
  676. }
  677. /* reset intra predictors and invalidate motion vector references */
  678. if (s->mb_x > 0) {
  679. memset(h->intra4x4_pred_mode+h->mb2br_xy[mb_xy - 1 ]+3, -1, 4*sizeof(int8_t));
  680. memset(h->intra4x4_pred_mode+h->mb2br_xy[mb_xy - s->mb_x] , -1, 8*sizeof(int8_t)*s->mb_x);
  681. }
  682. if (s->mb_y > 0) {
  683. memset(h->intra4x4_pred_mode+h->mb2br_xy[mb_xy - s->mb_stride], -1, 8*sizeof(int8_t)*(s->mb_width - s->mb_x));
  684. if (s->mb_x > 0) {
  685. h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - s->mb_stride - 1]+3] = -1;
  686. }
  687. }
  688. return 0;
  689. }
  690. static av_cold int svq3_decode_init(AVCodecContext *avctx)
  691. {
  692. SVQ3Context *svq3 = avctx->priv_data;
  693. H264Context *h = &svq3->h;
  694. MpegEncContext *s = &h->s;
  695. int m;
  696. unsigned char *extradata;
  697. unsigned char *extradata_end;
  698. unsigned int size;
  699. int marker_found = 0;
  700. if (ff_h264_decode_init(avctx) < 0)
  701. return -1;
  702. s->flags = avctx->flags;
  703. s->flags2 = avctx->flags2;
  704. s->unrestricted_mv = 1;
  705. h->is_complex=1;
  706. h->sps.chroma_format_idc = 1;
  707. avctx->pix_fmt = avctx->codec->pix_fmts[0];
  708. if (!s->context_initialized) {
  709. h->chroma_qp[0] = h->chroma_qp[1] = 4;
  710. svq3->halfpel_flag = 1;
  711. svq3->thirdpel_flag = 1;
  712. svq3->unknown_flag = 0;
  713. /* prowl for the "SEQH" marker in the extradata */
  714. extradata = (unsigned char *)avctx->extradata;
  715. extradata_end = avctx->extradata + avctx->extradata_size;
  716. if (extradata) {
  717. for (m = 0; m + 8 < avctx->extradata_size; m++) {
  718. if (!memcmp(extradata, "SEQH", 4)) {
  719. marker_found = 1;
  720. break;
  721. }
  722. extradata++;
  723. }
  724. }
  725. /* if a match was found, parse the extra data */
  726. if (marker_found) {
  727. GetBitContext gb;
  728. int frame_size_code;
  729. size = AV_RB32(&extradata[4]);
  730. if (size > extradata_end - extradata - 8)
  731. return AVERROR_INVALIDDATA;
  732. init_get_bits(&gb, extradata + 8, size*8);
  733. /* 'frame size code' and optional 'width, height' */
  734. frame_size_code = get_bits(&gb, 3);
  735. switch (frame_size_code) {
  736. case 0: avctx->width = 160; avctx->height = 120; break;
  737. case 1: avctx->width = 128; avctx->height = 96; break;
  738. case 2: avctx->width = 176; avctx->height = 144; break;
  739. case 3: avctx->width = 352; avctx->height = 288; break;
  740. case 4: avctx->width = 704; avctx->height = 576; break;
  741. case 5: avctx->width = 240; avctx->height = 180; break;
  742. case 6: avctx->width = 320; avctx->height = 240; break;
  743. case 7:
  744. avctx->width = get_bits(&gb, 12);
  745. avctx->height = get_bits(&gb, 12);
  746. break;
  747. }
  748. svq3->halfpel_flag = get_bits1(&gb);
  749. svq3->thirdpel_flag = get_bits1(&gb);
  750. /* unknown fields */
  751. skip_bits1(&gb);
  752. skip_bits1(&gb);
  753. skip_bits1(&gb);
  754. skip_bits1(&gb);
  755. s->low_delay = get_bits1(&gb);
  756. /* unknown field */
  757. skip_bits1(&gb);
  758. while (get_bits1(&gb)) {
  759. skip_bits(&gb, 8);
  760. }
  761. svq3->unknown_flag = get_bits1(&gb);
  762. avctx->has_b_frames = !s->low_delay;
  763. if (svq3->unknown_flag) {
  764. #if CONFIG_ZLIB
  765. unsigned watermark_width = svq3_get_ue_golomb(&gb);
  766. unsigned watermark_height = svq3_get_ue_golomb(&gb);
  767. int u1 = svq3_get_ue_golomb(&gb);
  768. int u2 = get_bits(&gb, 8);
  769. int u3 = get_bits(&gb, 2);
  770. int u4 = svq3_get_ue_golomb(&gb);
  771. unsigned long buf_len = watermark_width*watermark_height*4;
  772. int offset = (get_bits_count(&gb)+7)>>3;
  773. uint8_t *buf;
  774. if (watermark_height<=0 || (uint64_t)watermark_width*4 > UINT_MAX/watermark_height)
  775. return -1;
  776. buf = av_malloc(buf_len);
  777. av_log(avctx, AV_LOG_DEBUG, "watermark size: %dx%d\n", watermark_width, watermark_height);
  778. av_log(avctx, AV_LOG_DEBUG, "u1: %x u2: %x u3: %x compressed data size: %d offset: %d\n", u1, u2, u3, u4, offset);
  779. if (uncompress(buf, &buf_len, extradata + 8 + offset, size - offset) != Z_OK) {
  780. av_log(avctx, AV_LOG_ERROR, "could not uncompress watermark logo\n");
  781. av_free(buf);
  782. return -1;
  783. }
  784. svq3->watermark_key = ff_svq1_packet_checksum(buf, buf_len, 0);
  785. svq3->watermark_key = svq3->watermark_key << 16 | svq3->watermark_key;
  786. av_log(avctx, AV_LOG_DEBUG, "watermark key %#x\n", svq3->watermark_key);
  787. av_free(buf);
  788. #else
  789. av_log(avctx, AV_LOG_ERROR, "this svq3 file contains watermark which need zlib support compiled in\n");
  790. return -1;
  791. #endif
  792. }
  793. }
  794. s->width = avctx->width;
  795. s->height = avctx->height;
  796. if (ff_MPV_common_init(s) < 0)
  797. return -1;
  798. h->b_stride = 4*s->mb_width;
  799. if (ff_h264_alloc_tables(h) < 0) {
  800. av_log(avctx, AV_LOG_ERROR, "svq3 memory allocation failed\n");
  801. return AVERROR(ENOMEM);
  802. }
  803. }
  804. return 0;
  805. }
  806. static int svq3_decode_frame(AVCodecContext *avctx,
  807. void *data, int *data_size,
  808. AVPacket *avpkt)
  809. {
  810. SVQ3Context *svq3 = avctx->priv_data;
  811. H264Context *h = &svq3->h;
  812. MpegEncContext *s = &h->s;
  813. int buf_size = avpkt->size;
  814. int m, mb_type, left;
  815. uint8_t *buf;
  816. /* special case for last picture */
  817. if (buf_size == 0) {
  818. if (s->next_picture_ptr && !s->low_delay) {
  819. *(AVFrame *) data = s->next_picture.f;
  820. s->next_picture_ptr = NULL;
  821. *data_size = sizeof(AVFrame);
  822. }
  823. return 0;
  824. }
  825. s->mb_x = s->mb_y = h->mb_xy = 0;
  826. if (svq3->watermark_key) {
  827. av_fast_malloc(&svq3->buf, &svq3->buf_size,
  828. buf_size+FF_INPUT_BUFFER_PADDING_SIZE);
  829. if (!svq3->buf)
  830. return AVERROR(ENOMEM);
  831. memcpy(svq3->buf, avpkt->data, buf_size);
  832. buf = svq3->buf;
  833. } else {
  834. buf = avpkt->data;
  835. }
  836. init_get_bits(&s->gb, buf, 8*buf_size);
  837. if (svq3_decode_slice_header(avctx))
  838. return -1;
  839. s->pict_type = h->slice_type;
  840. s->picture_number = h->slice_num;
  841. if (avctx->debug&FF_DEBUG_PICT_INFO){
  842. av_log(h->s.avctx, AV_LOG_DEBUG, "%c hpel:%d, tpel:%d aqp:%d qp:%d, slice_num:%02X\n",
  843. av_get_picture_type_char(s->pict_type), svq3->halfpel_flag, svq3->thirdpel_flag,
  844. s->adaptive_quant, s->qscale, h->slice_num);
  845. }
  846. /* for skipping the frame */
  847. s->current_picture.f.pict_type = s->pict_type;
  848. s->current_picture.f.key_frame = (s->pict_type == AV_PICTURE_TYPE_I);
  849. /* Skip B-frames if we do not have reference frames. */
  850. if (s->last_picture_ptr == NULL && s->pict_type == AV_PICTURE_TYPE_B)
  851. return 0;
  852. if ( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
  853. ||(avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
  854. || avctx->skip_frame >= AVDISCARD_ALL)
  855. return 0;
  856. if (s->next_p_frame_damaged) {
  857. if (s->pict_type == AV_PICTURE_TYPE_B)
  858. return 0;
  859. else
  860. s->next_p_frame_damaged = 0;
  861. }
  862. if (ff_h264_frame_start(h) < 0)
  863. return -1;
  864. if (s->pict_type == AV_PICTURE_TYPE_B) {
  865. h->frame_num_offset = (h->slice_num - h->prev_frame_num);
  866. if (h->frame_num_offset < 0) {
  867. h->frame_num_offset += 256;
  868. }
  869. if (h->frame_num_offset == 0 || h->frame_num_offset >= h->prev_frame_num_offset) {
  870. av_log(h->s.avctx, AV_LOG_ERROR, "error in B-frame picture id\n");
  871. return -1;
  872. }
  873. } else {
  874. h->prev_frame_num = h->frame_num;
  875. h->frame_num = h->slice_num;
  876. h->prev_frame_num_offset = (h->frame_num - h->prev_frame_num);
  877. if (h->prev_frame_num_offset < 0) {
  878. h->prev_frame_num_offset += 256;
  879. }
  880. }
  881. for (m = 0; m < 2; m++){
  882. int i;
  883. for (i = 0; i < 4; i++){
  884. int j;
  885. for (j = -1; j < 4; j++)
  886. h->ref_cache[m][scan8[0] + 8*i + j]= 1;
  887. if (i < 3)
  888. h->ref_cache[m][scan8[0] + 8*i + j]= PART_NOT_AVAILABLE;
  889. }
  890. }
  891. for (s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
  892. for (s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
  893. h->mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  894. if ( (get_bits_count(&s->gb) + 7) >= s->gb.size_in_bits &&
  895. ((get_bits_count(&s->gb) & 7) == 0 || show_bits(&s->gb, (-get_bits_count(&s->gb) & 7)) == 0)) {
  896. skip_bits(&s->gb, svq3->next_slice_index - get_bits_count(&s->gb));
  897. s->gb.size_in_bits = 8*buf_size;
  898. if (svq3_decode_slice_header(avctx))
  899. return -1;
  900. /* TODO: support s->mb_skip_run */
  901. }
  902. mb_type = svq3_get_ue_golomb(&s->gb);
  903. if (s->pict_type == AV_PICTURE_TYPE_I) {
  904. mb_type += 8;
  905. } else if (s->pict_type == AV_PICTURE_TYPE_B && mb_type >= 4) {
  906. mb_type += 4;
  907. }
  908. if ((unsigned)mb_type > 33 || svq3_decode_mb(svq3, mb_type)) {
  909. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  910. return -1;
  911. }
  912. if (mb_type != 0) {
  913. ff_h264_hl_decode_mb (h);
  914. }
  915. if (s->pict_type != AV_PICTURE_TYPE_B && !s->low_delay) {
  916. s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
  917. (s->pict_type == AV_PICTURE_TYPE_P && mb_type < 8) ? (mb_type - 1) : -1;
  918. }
  919. }
  920. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  921. }
  922. left = buf_size*8 - get_bits_count(&s->gb);
  923. if (s->mb_y != s->mb_height || s->mb_x != s->mb_width) {
  924. av_log(avctx, AV_LOG_INFO, "frame num %d incomplete pic x %d y %d left %d\n", avctx->frame_number, s->mb_y, s->mb_x, left);
  925. //av_hex_dump(stderr, buf+buf_size-8, 8);
  926. }
  927. if (left < 0) {
  928. av_log(avctx, AV_LOG_ERROR, "frame num %d left %d\n", avctx->frame_number, left);
  929. return -1;
  930. }
  931. ff_MPV_frame_end(s);
  932. if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
  933. *(AVFrame *) data = s->current_picture.f;
  934. } else {
  935. *(AVFrame *) data = s->last_picture.f;
  936. }
  937. /* Do not output the last pic after seeking. */
  938. if (s->last_picture_ptr || s->low_delay) {
  939. *data_size = sizeof(AVFrame);
  940. }
  941. return buf_size;
  942. }
  943. static int svq3_decode_end(AVCodecContext *avctx)
  944. {
  945. SVQ3Context *svq3 = avctx->priv_data;
  946. H264Context *h = &svq3->h;
  947. MpegEncContext *s = &h->s;
  948. ff_h264_free_context(h);
  949. ff_MPV_common_end(s);
  950. av_freep(&svq3->buf);
  951. svq3->buf_size = 0;
  952. return 0;
  953. }
  954. AVCodec ff_svq3_decoder = {
  955. .name = "svq3",
  956. .type = AVMEDIA_TYPE_VIDEO,
  957. .id = AV_CODEC_ID_SVQ3,
  958. .priv_data_size = sizeof(SVQ3Context),
  959. .init = svq3_decode_init,
  960. .close = svq3_decode_end,
  961. .decode = svq3_decode_frame,
  962. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
  963. CODEC_CAP_DELAY,
  964. .long_name = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 3 / Sorenson Video 3 / SVQ3"),
  965. .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_YUVJ420P, PIX_FMT_NONE },
  966. };