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.

8237 lines
312KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. /**
  23. * @file h264.c
  24. * H.264 / AVC / MPEG4 part10 codec.
  25. * @author Michael Niedermayer <michaelni@gmx.at>
  26. */
  27. #include "common.h"
  28. #include "dsputil.h"
  29. #include "avcodec.h"
  30. #include "mpegvideo.h"
  31. #include "h264.h"
  32. #include "h264data.h"
  33. #include "h264_parser.h"
  34. #include "golomb.h"
  35. #include "cabac.h"
  36. //#undef NDEBUG
  37. #include <assert.h>
  38. static VLC coeff_token_vlc[4];
  39. static VLC chroma_dc_coeff_token_vlc;
  40. static VLC total_zeros_vlc[15];
  41. static VLC chroma_dc_total_zeros_vlc[3];
  42. static VLC run_vlc[6];
  43. static VLC run7_vlc;
  44. static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
  45. static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
  46. static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize);
  47. static void filter_mb_fast( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize);
  48. static av_always_inline uint32_t pack16to32(int a, int b){
  49. #ifdef WORDS_BIGENDIAN
  50. return (b&0xFFFF) + (a<<16);
  51. #else
  52. return (a&0xFFFF) + (b<<16);
  53. #endif
  54. }
  55. const uint8_t ff_rem6[52]={
  56. 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
  57. };
  58. const uint8_t ff_div6[52]={
  59. 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8,
  60. };
  61. /**
  62. * fill a rectangle.
  63. * @param h height of the rectangle, should be a constant
  64. * @param w width of the rectangle, should be a constant
  65. * @param size the size of val (1 or 4), should be a constant
  66. */
  67. static av_always_inline void fill_rectangle(void *vp, int w, int h, int stride, uint32_t val, int size){
  68. uint8_t *p= (uint8_t*)vp;
  69. assert(size==1 || size==4);
  70. assert(w<=4);
  71. w *= size;
  72. stride *= size;
  73. assert((((long)vp)&(FFMIN(w, STRIDE_ALIGN)-1)) == 0);
  74. assert((stride&(w-1))==0);
  75. if(w==2){
  76. const uint16_t v= size==4 ? val : val*0x0101;
  77. *(uint16_t*)(p + 0*stride)= v;
  78. if(h==1) return;
  79. *(uint16_t*)(p + 1*stride)= v;
  80. if(h==2) return;
  81. *(uint16_t*)(p + 2*stride)=
  82. *(uint16_t*)(p + 3*stride)= v;
  83. }else if(w==4){
  84. const uint32_t v= size==4 ? val : val*0x01010101;
  85. *(uint32_t*)(p + 0*stride)= v;
  86. if(h==1) return;
  87. *(uint32_t*)(p + 1*stride)= v;
  88. if(h==2) return;
  89. *(uint32_t*)(p + 2*stride)=
  90. *(uint32_t*)(p + 3*stride)= v;
  91. }else if(w==8){
  92. //gcc can't optimize 64bit math on x86_32
  93. #if defined(ARCH_X86_64) || (defined(MP_WORDSIZE) && MP_WORDSIZE >= 64)
  94. const uint64_t v= val*0x0100000001ULL;
  95. *(uint64_t*)(p + 0*stride)= v;
  96. if(h==1) return;
  97. *(uint64_t*)(p + 1*stride)= v;
  98. if(h==2) return;
  99. *(uint64_t*)(p + 2*stride)=
  100. *(uint64_t*)(p + 3*stride)= v;
  101. }else if(w==16){
  102. const uint64_t v= val*0x0100000001ULL;
  103. *(uint64_t*)(p + 0+0*stride)=
  104. *(uint64_t*)(p + 8+0*stride)=
  105. *(uint64_t*)(p + 0+1*stride)=
  106. *(uint64_t*)(p + 8+1*stride)= v;
  107. if(h==2) return;
  108. *(uint64_t*)(p + 0+2*stride)=
  109. *(uint64_t*)(p + 8+2*stride)=
  110. *(uint64_t*)(p + 0+3*stride)=
  111. *(uint64_t*)(p + 8+3*stride)= v;
  112. #else
  113. *(uint32_t*)(p + 0+0*stride)=
  114. *(uint32_t*)(p + 4+0*stride)= val;
  115. if(h==1) return;
  116. *(uint32_t*)(p + 0+1*stride)=
  117. *(uint32_t*)(p + 4+1*stride)= val;
  118. if(h==2) return;
  119. *(uint32_t*)(p + 0+2*stride)=
  120. *(uint32_t*)(p + 4+2*stride)=
  121. *(uint32_t*)(p + 0+3*stride)=
  122. *(uint32_t*)(p + 4+3*stride)= val;
  123. }else if(w==16){
  124. *(uint32_t*)(p + 0+0*stride)=
  125. *(uint32_t*)(p + 4+0*stride)=
  126. *(uint32_t*)(p + 8+0*stride)=
  127. *(uint32_t*)(p +12+0*stride)=
  128. *(uint32_t*)(p + 0+1*stride)=
  129. *(uint32_t*)(p + 4+1*stride)=
  130. *(uint32_t*)(p + 8+1*stride)=
  131. *(uint32_t*)(p +12+1*stride)= val;
  132. if(h==2) return;
  133. *(uint32_t*)(p + 0+2*stride)=
  134. *(uint32_t*)(p + 4+2*stride)=
  135. *(uint32_t*)(p + 8+2*stride)=
  136. *(uint32_t*)(p +12+2*stride)=
  137. *(uint32_t*)(p + 0+3*stride)=
  138. *(uint32_t*)(p + 4+3*stride)=
  139. *(uint32_t*)(p + 8+3*stride)=
  140. *(uint32_t*)(p +12+3*stride)= val;
  141. #endif
  142. }else
  143. assert(0);
  144. assert(h==4);
  145. }
  146. static void fill_caches(H264Context *h, int mb_type, int for_deblock){
  147. MpegEncContext * const s = &h->s;
  148. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  149. int topleft_xy, top_xy, topright_xy, left_xy[2];
  150. int topleft_type, top_type, topright_type, left_type[2];
  151. int left_block[8];
  152. int i;
  153. //FIXME deblocking could skip the intra and nnz parts.
  154. if(for_deblock && (h->slice_num == 1 || h->slice_table[mb_xy] == h->slice_table[mb_xy-s->mb_stride]) && !FRAME_MBAFF)
  155. return;
  156. //wow what a mess, why didn't they simplify the interlacing&intra stuff, i can't imagine that these complex rules are worth it
  157. top_xy = mb_xy - s->mb_stride;
  158. topleft_xy = top_xy - 1;
  159. topright_xy= top_xy + 1;
  160. left_xy[1] = left_xy[0] = mb_xy-1;
  161. left_block[0]= 0;
  162. left_block[1]= 1;
  163. left_block[2]= 2;
  164. left_block[3]= 3;
  165. left_block[4]= 7;
  166. left_block[5]= 10;
  167. left_block[6]= 8;
  168. left_block[7]= 11;
  169. if(FRAME_MBAFF){
  170. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  171. const int top_pair_xy = pair_xy - s->mb_stride;
  172. const int topleft_pair_xy = top_pair_xy - 1;
  173. const int topright_pair_xy = top_pair_xy + 1;
  174. const int topleft_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]);
  175. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  176. const int topright_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topright_pair_xy]);
  177. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  178. const int curr_mb_frame_flag = !IS_INTERLACED(mb_type);
  179. const int bottom = (s->mb_y & 1);
  180. tprintf(s->avctx, "fill_caches: curr_mb_frame_flag:%d, left_mb_frame_flag:%d, topleft_mb_frame_flag:%d, top_mb_frame_flag:%d, topright_mb_frame_flag:%d\n", curr_mb_frame_flag, left_mb_frame_flag, topleft_mb_frame_flag, top_mb_frame_flag, topright_mb_frame_flag);
  181. if (bottom
  182. ? !curr_mb_frame_flag // bottom macroblock
  183. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  184. ) {
  185. top_xy -= s->mb_stride;
  186. }
  187. if (bottom
  188. ? !curr_mb_frame_flag // bottom macroblock
  189. : (!curr_mb_frame_flag && !topleft_mb_frame_flag) // top macroblock
  190. ) {
  191. topleft_xy -= s->mb_stride;
  192. }
  193. if (bottom
  194. ? !curr_mb_frame_flag // bottom macroblock
  195. : (!curr_mb_frame_flag && !topright_mb_frame_flag) // top macroblock
  196. ) {
  197. topright_xy -= s->mb_stride;
  198. }
  199. if (left_mb_frame_flag != curr_mb_frame_flag) {
  200. left_xy[1] = left_xy[0] = pair_xy - 1;
  201. if (curr_mb_frame_flag) {
  202. if (bottom) {
  203. left_block[0]= 2;
  204. left_block[1]= 2;
  205. left_block[2]= 3;
  206. left_block[3]= 3;
  207. left_block[4]= 8;
  208. left_block[5]= 11;
  209. left_block[6]= 8;
  210. left_block[7]= 11;
  211. } else {
  212. left_block[0]= 0;
  213. left_block[1]= 0;
  214. left_block[2]= 1;
  215. left_block[3]= 1;
  216. left_block[4]= 7;
  217. left_block[5]= 10;
  218. left_block[6]= 7;
  219. left_block[7]= 10;
  220. }
  221. } else {
  222. left_xy[1] += s->mb_stride;
  223. //left_block[0]= 0;
  224. left_block[1]= 2;
  225. left_block[2]= 0;
  226. left_block[3]= 2;
  227. //left_block[4]= 7;
  228. left_block[5]= 10;
  229. left_block[6]= 7;
  230. left_block[7]= 10;
  231. }
  232. }
  233. }
  234. h->top_mb_xy = top_xy;
  235. h->left_mb_xy[0] = left_xy[0];
  236. h->left_mb_xy[1] = left_xy[1];
  237. if(for_deblock){
  238. topleft_type = 0;
  239. topright_type = 0;
  240. top_type = h->slice_table[top_xy ] < 255 ? s->current_picture.mb_type[top_xy] : 0;
  241. left_type[0] = h->slice_table[left_xy[0] ] < 255 ? s->current_picture.mb_type[left_xy[0]] : 0;
  242. left_type[1] = h->slice_table[left_xy[1] ] < 255 ? s->current_picture.mb_type[left_xy[1]] : 0;
  243. if(FRAME_MBAFF && !IS_INTRA(mb_type)){
  244. int list;
  245. int v = *(uint16_t*)&h->non_zero_count[mb_xy][14];
  246. for(i=0; i<16; i++)
  247. h->non_zero_count_cache[scan8[i]] = (v>>i)&1;
  248. for(list=0; list<h->list_count; list++){
  249. if(USES_LIST(mb_type,list)){
  250. uint32_t *src = (uint32_t*)s->current_picture.motion_val[list][h->mb2b_xy[mb_xy]];
  251. uint32_t *dst = (uint32_t*)h->mv_cache[list][scan8[0]];
  252. int8_t *ref = &s->current_picture.ref_index[list][h->mb2b8_xy[mb_xy]];
  253. for(i=0; i<4; i++, dst+=8, src+=h->b_stride){
  254. dst[0] = src[0];
  255. dst[1] = src[1];
  256. dst[2] = src[2];
  257. dst[3] = src[3];
  258. }
  259. *(uint32_t*)&h->ref_cache[list][scan8[ 0]] =
  260. *(uint32_t*)&h->ref_cache[list][scan8[ 2]] = pack16to32(ref[0],ref[1])*0x0101;
  261. ref += h->b8_stride;
  262. *(uint32_t*)&h->ref_cache[list][scan8[ 8]] =
  263. *(uint32_t*)&h->ref_cache[list][scan8[10]] = pack16to32(ref[0],ref[1])*0x0101;
  264. }else{
  265. fill_rectangle(&h-> mv_cache[list][scan8[ 0]], 4, 4, 8, 0, 4);
  266. fill_rectangle(&h->ref_cache[list][scan8[ 0]], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
  267. }
  268. }
  269. }
  270. }else{
  271. topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;
  272. top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0;
  273. topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;
  274. left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;
  275. left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;
  276. }
  277. if(IS_INTRA(mb_type)){
  278. h->topleft_samples_available=
  279. h->top_samples_available=
  280. h->left_samples_available= 0xFFFF;
  281. h->topright_samples_available= 0xEEEA;
  282. if(!IS_INTRA(top_type) && (top_type==0 || h->pps.constrained_intra_pred)){
  283. h->topleft_samples_available= 0xB3FF;
  284. h->top_samples_available= 0x33FF;
  285. h->topright_samples_available= 0x26EA;
  286. }
  287. for(i=0; i<2; i++){
  288. if(!IS_INTRA(left_type[i]) && (left_type[i]==0 || h->pps.constrained_intra_pred)){
  289. h->topleft_samples_available&= 0xDF5F;
  290. h->left_samples_available&= 0x5F5F;
  291. }
  292. }
  293. if(!IS_INTRA(topleft_type) && (topleft_type==0 || h->pps.constrained_intra_pred))
  294. h->topleft_samples_available&= 0x7FFF;
  295. if(!IS_INTRA(topright_type) && (topright_type==0 || h->pps.constrained_intra_pred))
  296. h->topright_samples_available&= 0xFBFF;
  297. if(IS_INTRA4x4(mb_type)){
  298. if(IS_INTRA4x4(top_type)){
  299. h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];
  300. h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];
  301. h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];
  302. h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];
  303. }else{
  304. int pred;
  305. if(!top_type || (IS_INTER(top_type) && h->pps.constrained_intra_pred))
  306. pred= -1;
  307. else{
  308. pred= 2;
  309. }
  310. h->intra4x4_pred_mode_cache[4+8*0]=
  311. h->intra4x4_pred_mode_cache[5+8*0]=
  312. h->intra4x4_pred_mode_cache[6+8*0]=
  313. h->intra4x4_pred_mode_cache[7+8*0]= pred;
  314. }
  315. for(i=0; i<2; i++){
  316. if(IS_INTRA4x4(left_type[i])){
  317. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];
  318. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];
  319. }else{
  320. int pred;
  321. if(!left_type[i] || (IS_INTER(left_type[i]) && h->pps.constrained_intra_pred))
  322. pred= -1;
  323. else{
  324. pred= 2;
  325. }
  326. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
  327. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;
  328. }
  329. }
  330. }
  331. }
  332. /*
  333. 0 . T T. T T T T
  334. 1 L . .L . . . .
  335. 2 L . .L . . . .
  336. 3 . T TL . . . .
  337. 4 L . .L . . . .
  338. 5 L . .. . . . .
  339. */
  340. //FIXME constraint_intra_pred & partitioning & nnz (lets hope this is just a typo in the spec)
  341. if(top_type){
  342. h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][4];
  343. h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][5];
  344. h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][6];
  345. h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3];
  346. h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][9];
  347. h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8];
  348. h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][12];
  349. h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11];
  350. }else{
  351. h->non_zero_count_cache[4+8*0]=
  352. h->non_zero_count_cache[5+8*0]=
  353. h->non_zero_count_cache[6+8*0]=
  354. h->non_zero_count_cache[7+8*0]=
  355. h->non_zero_count_cache[1+8*0]=
  356. h->non_zero_count_cache[2+8*0]=
  357. h->non_zero_count_cache[1+8*3]=
  358. h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  359. }
  360. for (i=0; i<2; i++) {
  361. if(left_type[i]){
  362. h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[0+2*i]];
  363. h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[1+2*i]];
  364. h->non_zero_count_cache[0+8*1 + 8*i]= h->non_zero_count[left_xy[i]][left_block[4+2*i]];
  365. h->non_zero_count_cache[0+8*4 + 8*i]= h->non_zero_count[left_xy[i]][left_block[5+2*i]];
  366. }else{
  367. h->non_zero_count_cache[3+8*1 + 2*8*i]=
  368. h->non_zero_count_cache[3+8*2 + 2*8*i]=
  369. h->non_zero_count_cache[0+8*1 + 8*i]=
  370. h->non_zero_count_cache[0+8*4 + 8*i]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  371. }
  372. }
  373. if( h->pps.cabac ) {
  374. // top_cbp
  375. if(top_type) {
  376. h->top_cbp = h->cbp_table[top_xy];
  377. } else if(IS_INTRA(mb_type)) {
  378. h->top_cbp = 0x1C0;
  379. } else {
  380. h->top_cbp = 0;
  381. }
  382. // left_cbp
  383. if (left_type[0]) {
  384. h->left_cbp = h->cbp_table[left_xy[0]] & 0x1f0;
  385. } else if(IS_INTRA(mb_type)) {
  386. h->left_cbp = 0x1C0;
  387. } else {
  388. h->left_cbp = 0;
  389. }
  390. if (left_type[0]) {
  391. h->left_cbp |= ((h->cbp_table[left_xy[0]]>>((left_block[0]&(~1))+1))&0x1) << 1;
  392. }
  393. if (left_type[1]) {
  394. h->left_cbp |= ((h->cbp_table[left_xy[1]]>>((left_block[2]&(~1))+1))&0x1) << 3;
  395. }
  396. }
  397. #if 1
  398. if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
  399. int list;
  400. for(list=0; list<h->list_count; list++){
  401. if(!USES_LIST(mb_type, list) && !IS_DIRECT(mb_type) && !h->deblocking_filter){
  402. /*if(!h->mv_cache_clean[list]){
  403. memset(h->mv_cache [list], 0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all?
  404. memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
  405. h->mv_cache_clean[list]= 1;
  406. }*/
  407. continue;
  408. }
  409. h->mv_cache_clean[list]= 0;
  410. if(USES_LIST(top_type, list)){
  411. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  412. const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;
  413. *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0];
  414. *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1];
  415. *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2];
  416. *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3];
  417. h->ref_cache[list][scan8[0] + 0 - 1*8]=
  418. h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];
  419. h->ref_cache[list][scan8[0] + 2 - 1*8]=
  420. h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];
  421. }else{
  422. *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]=
  423. *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]=
  424. *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]=
  425. *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0;
  426. *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;
  427. }
  428. for(i=0; i<2; i++){
  429. int cache_idx = scan8[0] - 1 + i*2*8;
  430. if(USES_LIST(left_type[i], list)){
  431. const int b_xy= h->mb2b_xy[left_xy[i]] + 3;
  432. const int b8_xy= h->mb2b8_xy[left_xy[i]] + 1;
  433. *(uint32_t*)h->mv_cache[list][cache_idx ]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[0+i*2]];
  434. *(uint32_t*)h->mv_cache[list][cache_idx+8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[1+i*2]];
  435. h->ref_cache[list][cache_idx ]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0+i*2]>>1)];
  436. h->ref_cache[list][cache_idx+8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[1+i*2]>>1)];
  437. }else{
  438. *(uint32_t*)h->mv_cache [list][cache_idx ]=
  439. *(uint32_t*)h->mv_cache [list][cache_idx+8]= 0;
  440. h->ref_cache[list][cache_idx ]=
  441. h->ref_cache[list][cache_idx+8]= left_type[i] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  442. }
  443. }
  444. if((for_deblock || (IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred)) && !FRAME_MBAFF)
  445. continue;
  446. if(USES_LIST(topleft_type, list)){
  447. const int b_xy = h->mb2b_xy[topleft_xy] + 3 + 3*h->b_stride;
  448. const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + h->b8_stride;
  449. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  450. h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  451. }else{
  452. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;
  453. h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  454. }
  455. if(USES_LIST(topright_type, list)){
  456. const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
  457. const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;
  458. *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  459. h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  460. }else{
  461. *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;
  462. h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  463. }
  464. if((IS_SKIP(mb_type) || IS_DIRECT(mb_type)) && !FRAME_MBAFF)
  465. continue;
  466. h->ref_cache[list][scan8[5 ]+1] =
  467. h->ref_cache[list][scan8[7 ]+1] =
  468. h->ref_cache[list][scan8[13]+1] = //FIXME remove past 3 (init somewhere else)
  469. h->ref_cache[list][scan8[4 ]] =
  470. h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
  471. *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=
  472. *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=
  473. *(uint32_t*)h->mv_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  474. *(uint32_t*)h->mv_cache [list][scan8[4 ]]=
  475. *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;
  476. if( h->pps.cabac ) {
  477. /* XXX beurk, Load mvd */
  478. if(USES_LIST(top_type, list)){
  479. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  480. *(uint32_t*)h->mvd_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 0];
  481. *(uint32_t*)h->mvd_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 1];
  482. *(uint32_t*)h->mvd_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 2];
  483. *(uint32_t*)h->mvd_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 3];
  484. }else{
  485. *(uint32_t*)h->mvd_cache [list][scan8[0] + 0 - 1*8]=
  486. *(uint32_t*)h->mvd_cache [list][scan8[0] + 1 - 1*8]=
  487. *(uint32_t*)h->mvd_cache [list][scan8[0] + 2 - 1*8]=
  488. *(uint32_t*)h->mvd_cache [list][scan8[0] + 3 - 1*8]= 0;
  489. }
  490. if(USES_LIST(left_type[0], list)){
  491. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  492. *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 0*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[0]];
  493. *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[1]];
  494. }else{
  495. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 0*8]=
  496. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 1*8]= 0;
  497. }
  498. if(USES_LIST(left_type[1], list)){
  499. const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
  500. *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 2*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[2]];
  501. *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 3*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[3]];
  502. }else{
  503. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 2*8]=
  504. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 3*8]= 0;
  505. }
  506. *(uint32_t*)h->mvd_cache [list][scan8[5 ]+1]=
  507. *(uint32_t*)h->mvd_cache [list][scan8[7 ]+1]=
  508. *(uint32_t*)h->mvd_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  509. *(uint32_t*)h->mvd_cache [list][scan8[4 ]]=
  510. *(uint32_t*)h->mvd_cache [list][scan8[12]]= 0;
  511. if(h->slice_type == B_TYPE){
  512. fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1);
  513. if(IS_DIRECT(top_type)){
  514. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101;
  515. }else if(IS_8X8(top_type)){
  516. int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride;
  517. h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy];
  518. h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1];
  519. }else{
  520. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0;
  521. }
  522. if(IS_DIRECT(left_type[0]))
  523. h->direct_cache[scan8[0] - 1 + 0*8]= 1;
  524. else if(IS_8X8(left_type[0]))
  525. h->direct_cache[scan8[0] - 1 + 0*8]= h->direct_table[h->mb2b8_xy[left_xy[0]] + 1 + h->b8_stride*(left_block[0]>>1)];
  526. else
  527. h->direct_cache[scan8[0] - 1 + 0*8]= 0;
  528. if(IS_DIRECT(left_type[1]))
  529. h->direct_cache[scan8[0] - 1 + 2*8]= 1;
  530. else if(IS_8X8(left_type[1]))
  531. h->direct_cache[scan8[0] - 1 + 2*8]= h->direct_table[h->mb2b8_xy[left_xy[1]] + 1 + h->b8_stride*(left_block[2]>>1)];
  532. else
  533. h->direct_cache[scan8[0] - 1 + 2*8]= 0;
  534. }
  535. }
  536. if(FRAME_MBAFF){
  537. #define MAP_MVS\
  538. MAP_F2F(scan8[0] - 1 - 1*8, topleft_type)\
  539. MAP_F2F(scan8[0] + 0 - 1*8, top_type)\
  540. MAP_F2F(scan8[0] + 1 - 1*8, top_type)\
  541. MAP_F2F(scan8[0] + 2 - 1*8, top_type)\
  542. MAP_F2F(scan8[0] + 3 - 1*8, top_type)\
  543. MAP_F2F(scan8[0] + 4 - 1*8, topright_type)\
  544. MAP_F2F(scan8[0] - 1 + 0*8, left_type[0])\
  545. MAP_F2F(scan8[0] - 1 + 1*8, left_type[0])\
  546. MAP_F2F(scan8[0] - 1 + 2*8, left_type[1])\
  547. MAP_F2F(scan8[0] - 1 + 3*8, left_type[1])
  548. if(MB_FIELD){
  549. #define MAP_F2F(idx, mb_type)\
  550. if(!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  551. h->ref_cache[list][idx] <<= 1;\
  552. h->mv_cache[list][idx][1] /= 2;\
  553. h->mvd_cache[list][idx][1] /= 2;\
  554. }
  555. MAP_MVS
  556. #undef MAP_F2F
  557. }else{
  558. #define MAP_F2F(idx, mb_type)\
  559. if(IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  560. h->ref_cache[list][idx] >>= 1;\
  561. h->mv_cache[list][idx][1] <<= 1;\
  562. h->mvd_cache[list][idx][1] <<= 1;\
  563. }
  564. MAP_MVS
  565. #undef MAP_F2F
  566. }
  567. }
  568. }
  569. }
  570. #endif
  571. h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);
  572. }
  573. static inline void write_back_intra_pred_mode(H264Context *h){
  574. MpegEncContext * const s = &h->s;
  575. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  576. h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
  577. h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
  578. h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
  579. h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
  580. h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
  581. h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
  582. h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
  583. }
  584. /**
  585. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  586. */
  587. static inline int check_intra4x4_pred_mode(H264Context *h){
  588. MpegEncContext * const s = &h->s;
  589. static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
  590. static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
  591. int i;
  592. if(!(h->top_samples_available&0x8000)){
  593. for(i=0; i<4; i++){
  594. int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
  595. if(status<0){
  596. av_log(h->s.avctx, AV_LOG_ERROR, "top block unavailable for requested intra4x4 mode %d at %d %d\n", status, s->mb_x, s->mb_y);
  597. return -1;
  598. } else if(status){
  599. h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
  600. }
  601. }
  602. }
  603. if(!(h->left_samples_available&0x8000)){
  604. for(i=0; i<4; i++){
  605. int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
  606. if(status<0){
  607. av_log(h->s.avctx, AV_LOG_ERROR, "left block unavailable for requested intra4x4 mode %d at %d %d\n", status, s->mb_x, s->mb_y);
  608. return -1;
  609. } else if(status){
  610. h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
  611. }
  612. }
  613. }
  614. return 0;
  615. } //FIXME cleanup like next
  616. /**
  617. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  618. */
  619. static inline int check_intra_pred_mode(H264Context *h, int mode){
  620. MpegEncContext * const s = &h->s;
  621. static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
  622. static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
  623. if(mode > 6U) {
  624. av_log(h->s.avctx, AV_LOG_ERROR, "out of range intra chroma pred mode at %d %d\n", s->mb_x, s->mb_y);
  625. return -1;
  626. }
  627. if(!(h->top_samples_available&0x8000)){
  628. mode= top[ mode ];
  629. if(mode<0){
  630. av_log(h->s.avctx, AV_LOG_ERROR, "top block unavailable for requested intra mode at %d %d\n", s->mb_x, s->mb_y);
  631. return -1;
  632. }
  633. }
  634. if(!(h->left_samples_available&0x8000)){
  635. mode= left[ mode ];
  636. if(mode<0){
  637. av_log(h->s.avctx, AV_LOG_ERROR, "left block unavailable for requested intra mode at %d %d\n", s->mb_x, s->mb_y);
  638. return -1;
  639. }
  640. }
  641. return mode;
  642. }
  643. /**
  644. * gets the predicted intra4x4 prediction mode.
  645. */
  646. static inline int pred_intra_mode(H264Context *h, int n){
  647. const int index8= scan8[n];
  648. const int left= h->intra4x4_pred_mode_cache[index8 - 1];
  649. const int top = h->intra4x4_pred_mode_cache[index8 - 8];
  650. const int min= FFMIN(left, top);
  651. tprintf(h->s.avctx, "mode:%d %d min:%d\n", left ,top, min);
  652. if(min<0) return DC_PRED;
  653. else return min;
  654. }
  655. static inline void write_back_non_zero_count(H264Context *h){
  656. MpegEncContext * const s = &h->s;
  657. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  658. h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[7+8*1];
  659. h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[7+8*2];
  660. h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[7+8*3];
  661. h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4];
  662. h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[4+8*4];
  663. h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[5+8*4];
  664. h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[6+8*4];
  665. h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[1+8*2];
  666. h->non_zero_count[mb_xy][8]= h->non_zero_count_cache[2+8*2];
  667. h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[2+8*1];
  668. h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[1+8*5];
  669. h->non_zero_count[mb_xy][11]=h->non_zero_count_cache[2+8*5];
  670. h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[2+8*4];
  671. if(FRAME_MBAFF){
  672. // store all luma nnzs, for deblocking
  673. int v = 0, i;
  674. for(i=0; i<16; i++)
  675. v += (!!h->non_zero_count_cache[scan8[i]]) << i;
  676. *(uint16_t*)&h->non_zero_count[mb_xy][14] = v;
  677. }
  678. }
  679. /**
  680. * gets the predicted number of non zero coefficients.
  681. * @param n block index
  682. */
  683. static inline int pred_non_zero_count(H264Context *h, int n){
  684. const int index8= scan8[n];
  685. const int left= h->non_zero_count_cache[index8 - 1];
  686. const int top = h->non_zero_count_cache[index8 - 8];
  687. int i= left + top;
  688. if(i<64) i= (i+1)>>1;
  689. tprintf(h->s.avctx, "pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
  690. return i&31;
  691. }
  692. static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
  693. const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
  694. MpegEncContext *s = &h->s;
  695. /* there is no consistent mapping of mvs to neighboring locations that will
  696. * make mbaff happy, so we can't move all this logic to fill_caches */
  697. if(FRAME_MBAFF){
  698. const uint32_t *mb_types = s->current_picture_ptr->mb_type;
  699. const int16_t *mv;
  700. *(uint32_t*)h->mv_cache[list][scan8[0]-2] = 0;
  701. *C = h->mv_cache[list][scan8[0]-2];
  702. if(!MB_FIELD
  703. && (s->mb_y&1) && i < scan8[0]+8 && topright_ref != PART_NOT_AVAILABLE){
  704. int topright_xy = s->mb_x + (s->mb_y-1)*s->mb_stride + (i == scan8[0]+3);
  705. if(IS_INTERLACED(mb_types[topright_xy])){
  706. #define SET_DIAG_MV(MV_OP, REF_OP, X4, Y4)\
  707. const int x4 = X4, y4 = Y4;\
  708. const int mb_type = mb_types[(x4>>2)+(y4>>2)*s->mb_stride];\
  709. if(!USES_LIST(mb_type,list) && !IS_8X8(mb_type))\
  710. return LIST_NOT_USED;\
  711. mv = s->current_picture_ptr->motion_val[list][x4 + y4*h->b_stride];\
  712. h->mv_cache[list][scan8[0]-2][0] = mv[0];\
  713. h->mv_cache[list][scan8[0]-2][1] = mv[1] MV_OP;\
  714. return s->current_picture_ptr->ref_index[list][(x4>>1) + (y4>>1)*h->b8_stride] REF_OP;
  715. SET_DIAG_MV(*2, >>1, s->mb_x*4+(i&7)-4+part_width, s->mb_y*4-1);
  716. }
  717. }
  718. if(topright_ref == PART_NOT_AVAILABLE
  719. && ((s->mb_y&1) || i >= scan8[0]+8) && (i&7)==4
  720. && h->ref_cache[list][scan8[0]-1] != PART_NOT_AVAILABLE){
  721. if(!MB_FIELD
  722. && IS_INTERLACED(mb_types[h->left_mb_xy[0]])){
  723. SET_DIAG_MV(*2, >>1, s->mb_x*4-1, (s->mb_y|1)*4+(s->mb_y&1)*2+(i>>4)-1);
  724. }
  725. if(MB_FIELD
  726. && !IS_INTERLACED(mb_types[h->left_mb_xy[0]])
  727. && i >= scan8[0]+8){
  728. // leftshift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's ok.
  729. SET_DIAG_MV(>>1, <<1, s->mb_x*4-1, (s->mb_y&~1)*4 - 1 + ((i-scan8[0])>>3)*2);
  730. }
  731. }
  732. #undef SET_DIAG_MV
  733. }
  734. if(topright_ref != PART_NOT_AVAILABLE){
  735. *C= h->mv_cache[list][ i - 8 + part_width ];
  736. return topright_ref;
  737. }else{
  738. tprintf(s->avctx, "topright MV not available\n");
  739. *C= h->mv_cache[list][ i - 8 - 1 ];
  740. return h->ref_cache[list][ i - 8 - 1 ];
  741. }
  742. }
  743. /**
  744. * gets the predicted MV.
  745. * @param n the block index
  746. * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
  747. * @param mx the x component of the predicted motion vector
  748. * @param my the y component of the predicted motion vector
  749. */
  750. static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
  751. const int index8= scan8[n];
  752. const int top_ref= h->ref_cache[list][ index8 - 8 ];
  753. const int left_ref= h->ref_cache[list][ index8 - 1 ];
  754. const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
  755. const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
  756. const int16_t * C;
  757. int diagonal_ref, match_count;
  758. assert(part_width==1 || part_width==2 || part_width==4);
  759. /* mv_cache
  760. B . . A T T T T
  761. U . . L . . , .
  762. U . . L . . . .
  763. U . . L . . , .
  764. . . . L . . . .
  765. */
  766. diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
  767. match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
  768. tprintf(h->s.avctx, "pred_motion match_count=%d\n", match_count);
  769. if(match_count > 1){ //most common
  770. *mx= mid_pred(A[0], B[0], C[0]);
  771. *my= mid_pred(A[1], B[1], C[1]);
  772. }else if(match_count==1){
  773. if(left_ref==ref){
  774. *mx= A[0];
  775. *my= A[1];
  776. }else if(top_ref==ref){
  777. *mx= B[0];
  778. *my= B[1];
  779. }else{
  780. *mx= C[0];
  781. *my= C[1];
  782. }
  783. }else{
  784. if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
  785. *mx= A[0];
  786. *my= A[1];
  787. }else{
  788. *mx= mid_pred(A[0], B[0], C[0]);
  789. *my= mid_pred(A[1], B[1], C[1]);
  790. }
  791. }
  792. tprintf(h->s.avctx, "pred_motion (%2d %2d %2d) (%2d %2d %2d) (%2d %2d %2d) -> (%2d %2d %2d) at %2d %2d %d list %d\n", top_ref, B[0], B[1], diagonal_ref, C[0], C[1], left_ref, A[0], A[1], ref, *mx, *my, h->s.mb_x, h->s.mb_y, n, list);
  793. }
  794. /**
  795. * gets the directionally predicted 16x8 MV.
  796. * @param n the block index
  797. * @param mx the x component of the predicted motion vector
  798. * @param my the y component of the predicted motion vector
  799. */
  800. static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  801. if(n==0){
  802. const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
  803. const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
  804. tprintf(h->s.avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n", top_ref, B[0], B[1], h->s.mb_x, h->s.mb_y, n, list);
  805. if(top_ref == ref){
  806. *mx= B[0];
  807. *my= B[1];
  808. return;
  809. }
  810. }else{
  811. const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
  812. const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
  813. tprintf(h->s.avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
  814. if(left_ref == ref){
  815. *mx= A[0];
  816. *my= A[1];
  817. return;
  818. }
  819. }
  820. //RARE
  821. pred_motion(h, n, 4, list, ref, mx, my);
  822. }
  823. /**
  824. * gets the directionally predicted 8x16 MV.
  825. * @param n the block index
  826. * @param mx the x component of the predicted motion vector
  827. * @param my the y component of the predicted motion vector
  828. */
  829. static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  830. if(n==0){
  831. const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
  832. const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
  833. tprintf(h->s.avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
  834. if(left_ref == ref){
  835. *mx= A[0];
  836. *my= A[1];
  837. return;
  838. }
  839. }else{
  840. const int16_t * C;
  841. int diagonal_ref;
  842. diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
  843. tprintf(h->s.avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n", diagonal_ref, C[0], C[1], h->s.mb_x, h->s.mb_y, n, list);
  844. if(diagonal_ref == ref){
  845. *mx= C[0];
  846. *my= C[1];
  847. return;
  848. }
  849. }
  850. //RARE
  851. pred_motion(h, n, 2, list, ref, mx, my);
  852. }
  853. static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
  854. const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
  855. const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
  856. tprintf(h->s.avctx, "pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
  857. if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
  858. || (top_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ] == 0)
  859. || (left_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ] == 0)){
  860. *mx = *my = 0;
  861. return;
  862. }
  863. pred_motion(h, 0, 4, 0, 0, mx, my);
  864. return;
  865. }
  866. static inline void direct_dist_scale_factor(H264Context * const h){
  867. const int poc = h->s.current_picture_ptr->poc;
  868. const int poc1 = h->ref_list[1][0].poc;
  869. int i;
  870. for(i=0; i<h->ref_count[0]; i++){
  871. int poc0 = h->ref_list[0][i].poc;
  872. int td = av_clip(poc1 - poc0, -128, 127);
  873. if(td == 0 /* FIXME || pic0 is a long-term ref */){
  874. h->dist_scale_factor[i] = 256;
  875. }else{
  876. int tb = av_clip(poc - poc0, -128, 127);
  877. int tx = (16384 + (FFABS(td) >> 1)) / td;
  878. h->dist_scale_factor[i] = av_clip((tb*tx + 32) >> 6, -1024, 1023);
  879. }
  880. }
  881. if(FRAME_MBAFF){
  882. for(i=0; i<h->ref_count[0]; i++){
  883. h->dist_scale_factor_field[2*i] =
  884. h->dist_scale_factor_field[2*i+1] = h->dist_scale_factor[i];
  885. }
  886. }
  887. }
  888. static inline void direct_ref_list_init(H264Context * const h){
  889. MpegEncContext * const s = &h->s;
  890. Picture * const ref1 = &h->ref_list[1][0];
  891. Picture * const cur = s->current_picture_ptr;
  892. int list, i, j;
  893. if(cur->pict_type == I_TYPE)
  894. cur->ref_count[0] = 0;
  895. if(cur->pict_type != B_TYPE)
  896. cur->ref_count[1] = 0;
  897. for(list=0; list<2; list++){
  898. cur->ref_count[list] = h->ref_count[list];
  899. for(j=0; j<h->ref_count[list]; j++)
  900. cur->ref_poc[list][j] = h->ref_list[list][j].poc;
  901. }
  902. if(cur->pict_type != B_TYPE || h->direct_spatial_mv_pred)
  903. return;
  904. for(list=0; list<2; list++){
  905. for(i=0; i<ref1->ref_count[list]; i++){
  906. const int poc = ref1->ref_poc[list][i];
  907. h->map_col_to_list0[list][i] = 0; /* bogus; fills in for missing frames */
  908. for(j=0; j<h->ref_count[list]; j++)
  909. if(h->ref_list[list][j].poc == poc){
  910. h->map_col_to_list0[list][i] = j;
  911. break;
  912. }
  913. }
  914. }
  915. if(FRAME_MBAFF){
  916. for(list=0; list<2; list++){
  917. for(i=0; i<ref1->ref_count[list]; i++){
  918. j = h->map_col_to_list0[list][i];
  919. h->map_col_to_list0_field[list][2*i] = 2*j;
  920. h->map_col_to_list0_field[list][2*i+1] = 2*j+1;
  921. }
  922. }
  923. }
  924. }
  925. static inline void pred_direct_motion(H264Context * const h, int *mb_type){
  926. MpegEncContext * const s = &h->s;
  927. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  928. const int b8_xy = 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  929. const int b4_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  930. const int mb_type_col = h->ref_list[1][0].mb_type[mb_xy];
  931. const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[0][b4_xy];
  932. const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[1][b4_xy];
  933. const int8_t *l1ref0 = &h->ref_list[1][0].ref_index[0][b8_xy];
  934. const int8_t *l1ref1 = &h->ref_list[1][0].ref_index[1][b8_xy];
  935. const int is_b8x8 = IS_8X8(*mb_type);
  936. unsigned int sub_mb_type;
  937. int i8, i4;
  938. #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
  939. if(IS_8X8(mb_type_col) && !h->sps.direct_8x8_inference_flag){
  940. /* FIXME save sub mb types from previous frames (or derive from MVs)
  941. * so we know exactly what block size to use */
  942. sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
  943. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
  944. }else if(!is_b8x8 && (mb_type_col & MB_TYPE_16x16_OR_INTRA)){
  945. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  946. *mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
  947. }else{
  948. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  949. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
  950. }
  951. if(!is_b8x8)
  952. *mb_type |= MB_TYPE_DIRECT2;
  953. if(MB_FIELD)
  954. *mb_type |= MB_TYPE_INTERLACED;
  955. tprintf(s->avctx, "mb_type = %08x, sub_mb_type = %08x, is_b8x8 = %d, mb_type_col = %08x\n", *mb_type, sub_mb_type, is_b8x8, mb_type_col);
  956. if(h->direct_spatial_mv_pred){
  957. int ref[2];
  958. int mv[2][2];
  959. int list;
  960. /* FIXME interlacing + spatial direct uses wrong colocated block positions */
  961. /* ref = min(neighbors) */
  962. for(list=0; list<2; list++){
  963. int refa = h->ref_cache[list][scan8[0] - 1];
  964. int refb = h->ref_cache[list][scan8[0] - 8];
  965. int refc = h->ref_cache[list][scan8[0] - 8 + 4];
  966. if(refc == -2)
  967. refc = h->ref_cache[list][scan8[0] - 8 - 1];
  968. ref[list] = refa;
  969. if(ref[list] < 0 || (refb < ref[list] && refb >= 0))
  970. ref[list] = refb;
  971. if(ref[list] < 0 || (refc < ref[list] && refc >= 0))
  972. ref[list] = refc;
  973. if(ref[list] < 0)
  974. ref[list] = -1;
  975. }
  976. if(ref[0] < 0 && ref[1] < 0){
  977. ref[0] = ref[1] = 0;
  978. mv[0][0] = mv[0][1] =
  979. mv[1][0] = mv[1][1] = 0;
  980. }else{
  981. for(list=0; list<2; list++){
  982. if(ref[list] >= 0)
  983. pred_motion(h, 0, 4, list, ref[list], &mv[list][0], &mv[list][1]);
  984. else
  985. mv[list][0] = mv[list][1] = 0;
  986. }
  987. }
  988. if(ref[1] < 0){
  989. *mb_type &= ~MB_TYPE_P0L1;
  990. sub_mb_type &= ~MB_TYPE_P0L1;
  991. }else if(ref[0] < 0){
  992. *mb_type &= ~MB_TYPE_P0L0;
  993. sub_mb_type &= ~MB_TYPE_P0L0;
  994. }
  995. if(IS_16X16(*mb_type)){
  996. int a=0, b=0;
  997. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
  998. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
  999. if(!IS_INTRA(mb_type_col)
  1000. && ( (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
  1001. || (l1ref0[0] < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
  1002. && (h->x264_build>33 || !h->x264_build)))){
  1003. if(ref[0] > 0)
  1004. a= pack16to32(mv[0][0],mv[0][1]);
  1005. if(ref[1] > 0)
  1006. b= pack16to32(mv[1][0],mv[1][1]);
  1007. }else{
  1008. a= pack16to32(mv[0][0],mv[0][1]);
  1009. b= pack16to32(mv[1][0],mv[1][1]);
  1010. }
  1011. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
  1012. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
  1013. }else{
  1014. for(i8=0; i8<4; i8++){
  1015. const int x8 = i8&1;
  1016. const int y8 = i8>>1;
  1017. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1018. continue;
  1019. h->sub_mb_type[i8] = sub_mb_type;
  1020. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1021. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1022. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
  1023. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
  1024. /* col_zero_flag */
  1025. if(!IS_INTRA(mb_type_col) && ( l1ref0[x8 + y8*h->b8_stride] == 0
  1026. || (l1ref0[x8 + y8*h->b8_stride] < 0 && l1ref1[x8 + y8*h->b8_stride] == 0
  1027. && (h->x264_build>33 || !h->x264_build)))){
  1028. const int16_t (*l1mv)[2]= l1ref0[x8 + y8*h->b8_stride] == 0 ? l1mv0 : l1mv1;
  1029. if(IS_SUB_8X8(sub_mb_type)){
  1030. const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
  1031. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  1032. if(ref[0] == 0)
  1033. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1034. if(ref[1] == 0)
  1035. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1036. }
  1037. }else
  1038. for(i4=0; i4<4; i4++){
  1039. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
  1040. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  1041. if(ref[0] == 0)
  1042. *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
  1043. if(ref[1] == 0)
  1044. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
  1045. }
  1046. }
  1047. }
  1048. }
  1049. }
  1050. }else{ /* direct temporal mv pred */
  1051. const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
  1052. const int *dist_scale_factor = h->dist_scale_factor;
  1053. if(FRAME_MBAFF){
  1054. if(IS_INTERLACED(*mb_type)){
  1055. map_col_to_list0[0] = h->map_col_to_list0_field[0];
  1056. map_col_to_list0[1] = h->map_col_to_list0_field[1];
  1057. dist_scale_factor = h->dist_scale_factor_field;
  1058. }
  1059. if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col)){
  1060. /* FIXME assumes direct_8x8_inference == 1 */
  1061. const int pair_xy = s->mb_x + (s->mb_y&~1)*s->mb_stride;
  1062. int mb_types_col[2];
  1063. int y_shift;
  1064. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1
  1065. | (is_b8x8 ? 0 : MB_TYPE_DIRECT2)
  1066. | (*mb_type & MB_TYPE_INTERLACED);
  1067. sub_mb_type = MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_16x16;
  1068. if(IS_INTERLACED(*mb_type)){
  1069. /* frame to field scaling */
  1070. mb_types_col[0] = h->ref_list[1][0].mb_type[pair_xy];
  1071. mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
  1072. if(s->mb_y&1){
  1073. l1ref0 -= 2*h->b8_stride;
  1074. l1ref1 -= 2*h->b8_stride;
  1075. l1mv0 -= 4*h->b_stride;
  1076. l1mv1 -= 4*h->b_stride;
  1077. }
  1078. y_shift = 0;
  1079. if( (mb_types_col[0] & MB_TYPE_16x16_OR_INTRA)
  1080. && (mb_types_col[1] & MB_TYPE_16x16_OR_INTRA)
  1081. && !is_b8x8)
  1082. *mb_type |= MB_TYPE_16x8;
  1083. else
  1084. *mb_type |= MB_TYPE_8x8;
  1085. }else{
  1086. /* field to frame scaling */
  1087. /* col_mb_y = (mb_y&~1) + (topAbsDiffPOC < bottomAbsDiffPOC ? 0 : 1)
  1088. * but in MBAFF, top and bottom POC are equal */
  1089. int dy = (s->mb_y&1) ? 1 : 2;
  1090. mb_types_col[0] =
  1091. mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
  1092. l1ref0 += dy*h->b8_stride;
  1093. l1ref1 += dy*h->b8_stride;
  1094. l1mv0 += 2*dy*h->b_stride;
  1095. l1mv1 += 2*dy*h->b_stride;
  1096. y_shift = 2;
  1097. if((mb_types_col[0] & (MB_TYPE_16x16_OR_INTRA|MB_TYPE_16x8))
  1098. && !is_b8x8)
  1099. *mb_type |= MB_TYPE_16x16;
  1100. else
  1101. *mb_type |= MB_TYPE_8x8;
  1102. }
  1103. for(i8=0; i8<4; i8++){
  1104. const int x8 = i8&1;
  1105. const int y8 = i8>>1;
  1106. int ref0, scale;
  1107. const int16_t (*l1mv)[2]= l1mv0;
  1108. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1109. continue;
  1110. h->sub_mb_type[i8] = sub_mb_type;
  1111. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1112. if(IS_INTRA(mb_types_col[y8])){
  1113. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  1114. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1115. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1116. continue;
  1117. }
  1118. ref0 = l1ref0[x8 + (y8*2>>y_shift)*h->b8_stride];
  1119. if(ref0 >= 0)
  1120. ref0 = map_col_to_list0[0][ref0*2>>y_shift];
  1121. else{
  1122. ref0 = map_col_to_list0[1][l1ref1[x8 + (y8*2>>y_shift)*h->b8_stride]*2>>y_shift];
  1123. l1mv= l1mv1;
  1124. }
  1125. scale = dist_scale_factor[ref0];
  1126. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  1127. {
  1128. const int16_t *mv_col = l1mv[x8*3 + (y8*6>>y_shift)*h->b_stride];
  1129. int my_col = (mv_col[1]<<y_shift)/2;
  1130. int mx = (scale * mv_col[0] + 128) >> 8;
  1131. int my = (scale * my_col + 128) >> 8;
  1132. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  1133. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
  1134. }
  1135. }
  1136. return;
  1137. }
  1138. }
  1139. /* one-to-one mv scaling */
  1140. if(IS_16X16(*mb_type)){
  1141. int ref, mv0, mv1;
  1142. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
  1143. if(IS_INTRA(mb_type_col)){
  1144. ref=mv0=mv1=0;
  1145. }else{
  1146. const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0]]
  1147. : map_col_to_list0[1][l1ref1[0]];
  1148. const int scale = dist_scale_factor[ref0];
  1149. const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
  1150. int mv_l0[2];
  1151. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  1152. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  1153. ref= ref0;
  1154. mv0= pack16to32(mv_l0[0],mv_l0[1]);
  1155. mv1= pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
  1156. }
  1157. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
  1158. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
  1159. fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
  1160. }else{
  1161. for(i8=0; i8<4; i8++){
  1162. const int x8 = i8&1;
  1163. const int y8 = i8>>1;
  1164. int ref0, scale;
  1165. const int16_t (*l1mv)[2]= l1mv0;
  1166. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1167. continue;
  1168. h->sub_mb_type[i8] = sub_mb_type;
  1169. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1170. if(IS_INTRA(mb_type_col)){
  1171. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  1172. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1173. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1174. continue;
  1175. }
  1176. ref0 = l1ref0[x8 + y8*h->b8_stride];
  1177. if(ref0 >= 0)
  1178. ref0 = map_col_to_list0[0][ref0];
  1179. else{
  1180. ref0 = map_col_to_list0[1][l1ref1[x8 + y8*h->b8_stride]];
  1181. l1mv= l1mv1;
  1182. }
  1183. scale = dist_scale_factor[ref0];
  1184. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  1185. if(IS_SUB_8X8(sub_mb_type)){
  1186. const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
  1187. int mx = (scale * mv_col[0] + 128) >> 8;
  1188. int my = (scale * mv_col[1] + 128) >> 8;
  1189. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  1190. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
  1191. }else
  1192. for(i4=0; i4<4; i4++){
  1193. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
  1194. int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
  1195. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  1196. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  1197. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =
  1198. pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
  1199. }
  1200. }
  1201. }
  1202. }
  1203. }
  1204. static inline void write_back_motion(H264Context *h, int mb_type){
  1205. MpegEncContext * const s = &h->s;
  1206. const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  1207. const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  1208. int list;
  1209. if(!USES_LIST(mb_type, 0))
  1210. fill_rectangle(&s->current_picture.ref_index[0][b8_xy], 2, 2, h->b8_stride, (uint8_t)LIST_NOT_USED, 1);
  1211. for(list=0; list<h->list_count; list++){
  1212. int y;
  1213. if(!USES_LIST(mb_type, list))
  1214. continue;
  1215. for(y=0; y<4; y++){
  1216. *(uint64_t*)s->current_picture.motion_val[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mv_cache[list][scan8[0]+0 + 8*y];
  1217. *(uint64_t*)s->current_picture.motion_val[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mv_cache[list][scan8[0]+2 + 8*y];
  1218. }
  1219. if( h->pps.cabac ) {
  1220. if(IS_SKIP(mb_type))
  1221. fill_rectangle(h->mvd_table[list][b_xy], 4, 4, h->b_stride, 0, 4);
  1222. else
  1223. for(y=0; y<4; y++){
  1224. *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+0 + 8*y];
  1225. *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+2 + 8*y];
  1226. }
  1227. }
  1228. {
  1229. int8_t *ref_index = &s->current_picture.ref_index[list][b8_xy];
  1230. ref_index[0+0*h->b8_stride]= h->ref_cache[list][scan8[0]];
  1231. ref_index[1+0*h->b8_stride]= h->ref_cache[list][scan8[4]];
  1232. ref_index[0+1*h->b8_stride]= h->ref_cache[list][scan8[8]];
  1233. ref_index[1+1*h->b8_stride]= h->ref_cache[list][scan8[12]];
  1234. }
  1235. }
  1236. if(h->slice_type == B_TYPE && h->pps.cabac){
  1237. if(IS_8X8(mb_type)){
  1238. uint8_t *direct_table = &h->direct_table[b8_xy];
  1239. direct_table[1+0*h->b8_stride] = IS_DIRECT(h->sub_mb_type[1]) ? 1 : 0;
  1240. direct_table[0+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[2]) ? 1 : 0;
  1241. direct_table[1+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[3]) ? 1 : 0;
  1242. }
  1243. }
  1244. }
  1245. /**
  1246. * Decodes a network abstraction layer unit.
  1247. * @param consumed is the number of bytes used as input
  1248. * @param length is the length of the array
  1249. * @param dst_length is the number of decoded bytes FIXME here or a decode rbsp tailing?
  1250. * @returns decoded bytes, might be src+1 if no escapes
  1251. */
  1252. static uint8_t *decode_nal(H264Context *h, uint8_t *src, int *dst_length, int *consumed, int length){
  1253. int i, si, di;
  1254. uint8_t *dst;
  1255. // src[0]&0x80; //forbidden bit
  1256. h->nal_ref_idc= src[0]>>5;
  1257. h->nal_unit_type= src[0]&0x1F;
  1258. src++; length--;
  1259. #if 0
  1260. for(i=0; i<length; i++)
  1261. printf("%2X ", src[i]);
  1262. #endif
  1263. for(i=0; i+1<length; i+=2){
  1264. if(src[i]) continue;
  1265. if(i>0 && src[i-1]==0) i--;
  1266. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  1267. if(src[i+2]!=3){
  1268. /* startcode, so we must be past the end */
  1269. length=i;
  1270. }
  1271. break;
  1272. }
  1273. }
  1274. if(i>=length-1){ //no escaped 0
  1275. *dst_length= length;
  1276. *consumed= length+1; //+1 for the header
  1277. return src;
  1278. }
  1279. h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length);
  1280. dst= h->rbsp_buffer;
  1281. if (dst == NULL){
  1282. return NULL;
  1283. }
  1284. //printf("decoding esc\n");
  1285. si=di=0;
  1286. while(si<length){
  1287. //remove escapes (very rare 1:2^22)
  1288. if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
  1289. if(src[si+2]==3){ //escape
  1290. dst[di++]= 0;
  1291. dst[di++]= 0;
  1292. si+=3;
  1293. continue;
  1294. }else //next start code
  1295. break;
  1296. }
  1297. dst[di++]= src[si++];
  1298. }
  1299. *dst_length= di;
  1300. *consumed= si + 1;//+1 for the header
  1301. //FIXME store exact number of bits in the getbitcontext (its needed for decoding)
  1302. return dst;
  1303. }
  1304. /**
  1305. * identifies the exact end of the bitstream
  1306. * @return the length of the trailing, or 0 if damaged
  1307. */
  1308. static int decode_rbsp_trailing(H264Context *h, uint8_t *src){
  1309. int v= *src;
  1310. int r;
  1311. tprintf(h->s.avctx, "rbsp trailing %X\n", v);
  1312. for(r=1; r<9; r++){
  1313. if(v&1) return r;
  1314. v>>=1;
  1315. }
  1316. return 0;
  1317. }
  1318. /**
  1319. * idct tranforms the 16 dc values and dequantize them.
  1320. * @param qp quantization parameter
  1321. */
  1322. static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1323. #define stride 16
  1324. int i;
  1325. int temp[16]; //FIXME check if this is a good idea
  1326. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1327. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1328. //memset(block, 64, 2*256);
  1329. //return;
  1330. for(i=0; i<4; i++){
  1331. const int offset= y_offset[i];
  1332. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1333. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1334. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1335. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1336. temp[4*i+0]= z0+z3;
  1337. temp[4*i+1]= z1+z2;
  1338. temp[4*i+2]= z1-z2;
  1339. temp[4*i+3]= z0-z3;
  1340. }
  1341. for(i=0; i<4; i++){
  1342. const int offset= x_offset[i];
  1343. const int z0= temp[4*0+i] + temp[4*2+i];
  1344. const int z1= temp[4*0+i] - temp[4*2+i];
  1345. const int z2= temp[4*1+i] - temp[4*3+i];
  1346. const int z3= temp[4*1+i] + temp[4*3+i];
  1347. block[stride*0 +offset]= ((((z0 + z3)*qmul + 128 ) >> 8)); //FIXME think about merging this into decode_resdual
  1348. block[stride*2 +offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
  1349. block[stride*8 +offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
  1350. block[stride*10+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
  1351. }
  1352. }
  1353. #if 0
  1354. /**
  1355. * dct tranforms the 16 dc values.
  1356. * @param qp quantization parameter ??? FIXME
  1357. */
  1358. static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
  1359. // const int qmul= dequant_coeff[qp][0];
  1360. int i;
  1361. int temp[16]; //FIXME check if this is a good idea
  1362. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1363. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1364. for(i=0; i<4; i++){
  1365. const int offset= y_offset[i];
  1366. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1367. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1368. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1369. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1370. temp[4*i+0]= z0+z3;
  1371. temp[4*i+1]= z1+z2;
  1372. temp[4*i+2]= z1-z2;
  1373. temp[4*i+3]= z0-z3;
  1374. }
  1375. for(i=0; i<4; i++){
  1376. const int offset= x_offset[i];
  1377. const int z0= temp[4*0+i] + temp[4*2+i];
  1378. const int z1= temp[4*0+i] - temp[4*2+i];
  1379. const int z2= temp[4*1+i] - temp[4*3+i];
  1380. const int z3= temp[4*1+i] + temp[4*3+i];
  1381. block[stride*0 +offset]= (z0 + z3)>>1;
  1382. block[stride*2 +offset]= (z1 + z2)>>1;
  1383. block[stride*8 +offset]= (z1 - z2)>>1;
  1384. block[stride*10+offset]= (z0 - z3)>>1;
  1385. }
  1386. }
  1387. #endif
  1388. #undef xStride
  1389. #undef stride
  1390. static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1391. const int stride= 16*2;
  1392. const int xStride= 16;
  1393. int a,b,c,d,e;
  1394. a= block[stride*0 + xStride*0];
  1395. b= block[stride*0 + xStride*1];
  1396. c= block[stride*1 + xStride*0];
  1397. d= block[stride*1 + xStride*1];
  1398. e= a-b;
  1399. a= a+b;
  1400. b= c-d;
  1401. c= c+d;
  1402. block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
  1403. block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
  1404. block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
  1405. block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;
  1406. }
  1407. #if 0
  1408. static void chroma_dc_dct_c(DCTELEM *block){
  1409. const int stride= 16*2;
  1410. const int xStride= 16;
  1411. int a,b,c,d,e;
  1412. a= block[stride*0 + xStride*0];
  1413. b= block[stride*0 + xStride*1];
  1414. c= block[stride*1 + xStride*0];
  1415. d= block[stride*1 + xStride*1];
  1416. e= a-b;
  1417. a= a+b;
  1418. b= c-d;
  1419. c= c+d;
  1420. block[stride*0 + xStride*0]= (a+c);
  1421. block[stride*0 + xStride*1]= (e+b);
  1422. block[stride*1 + xStride*0]= (a-c);
  1423. block[stride*1 + xStride*1]= (e-b);
  1424. }
  1425. #endif
  1426. /**
  1427. * gets the chroma qp.
  1428. */
  1429. static inline int get_chroma_qp(int chroma_qp_index_offset, int qscale){
  1430. return chroma_qp[av_clip(qscale + chroma_qp_index_offset, 0, 51)];
  1431. }
  1432. //FIXME need to check that this doesnt overflow signed 32 bit for low qp, i am not sure, it's very close
  1433. //FIXME check that gcc inlines this (and optimizes intra & separate_dc stuff away)
  1434. static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int separate_dc){
  1435. int i;
  1436. const int * const quant_table= quant_coeff[qscale];
  1437. const int bias= intra ? (1<<QUANT_SHIFT)/3 : (1<<QUANT_SHIFT)/6;
  1438. const unsigned int threshold1= (1<<QUANT_SHIFT) - bias - 1;
  1439. const unsigned int threshold2= (threshold1<<1);
  1440. int last_non_zero;
  1441. if(separate_dc){
  1442. if(qscale<=18){
  1443. //avoid overflows
  1444. const int dc_bias= intra ? (1<<(QUANT_SHIFT-2))/3 : (1<<(QUANT_SHIFT-2))/6;
  1445. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT-2)) - dc_bias - 1;
  1446. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1447. int level= block[0]*quant_coeff[qscale+18][0];
  1448. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1449. if(level>0){
  1450. level= (dc_bias + level)>>(QUANT_SHIFT-2);
  1451. block[0]= level;
  1452. }else{
  1453. level= (dc_bias - level)>>(QUANT_SHIFT-2);
  1454. block[0]= -level;
  1455. }
  1456. // last_non_zero = i;
  1457. }else{
  1458. block[0]=0;
  1459. }
  1460. }else{
  1461. const int dc_bias= intra ? (1<<(QUANT_SHIFT+1))/3 : (1<<(QUANT_SHIFT+1))/6;
  1462. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT+1)) - dc_bias - 1;
  1463. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1464. int level= block[0]*quant_table[0];
  1465. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1466. if(level>0){
  1467. level= (dc_bias + level)>>(QUANT_SHIFT+1);
  1468. block[0]= level;
  1469. }else{
  1470. level= (dc_bias - level)>>(QUANT_SHIFT+1);
  1471. block[0]= -level;
  1472. }
  1473. // last_non_zero = i;
  1474. }else{
  1475. block[0]=0;
  1476. }
  1477. }
  1478. last_non_zero= 0;
  1479. i=1;
  1480. }else{
  1481. last_non_zero= -1;
  1482. i=0;
  1483. }
  1484. for(; i<16; i++){
  1485. const int j= scantable[i];
  1486. int level= block[j]*quant_table[j];
  1487. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  1488. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  1489. if(((unsigned)(level+threshold1))>threshold2){
  1490. if(level>0){
  1491. level= (bias + level)>>QUANT_SHIFT;
  1492. block[j]= level;
  1493. }else{
  1494. level= (bias - level)>>QUANT_SHIFT;
  1495. block[j]= -level;
  1496. }
  1497. last_non_zero = i;
  1498. }else{
  1499. block[j]=0;
  1500. }
  1501. }
  1502. return last_non_zero;
  1503. }
  1504. static void pred4x4_vertical_c(uint8_t *src, uint8_t *topright, int stride){
  1505. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1506. ((uint32_t*)(src+0*stride))[0]= a;
  1507. ((uint32_t*)(src+1*stride))[0]= a;
  1508. ((uint32_t*)(src+2*stride))[0]= a;
  1509. ((uint32_t*)(src+3*stride))[0]= a;
  1510. }
  1511. static void pred4x4_horizontal_c(uint8_t *src, uint8_t *topright, int stride){
  1512. ((uint32_t*)(src+0*stride))[0]= src[-1+0*stride]*0x01010101;
  1513. ((uint32_t*)(src+1*stride))[0]= src[-1+1*stride]*0x01010101;
  1514. ((uint32_t*)(src+2*stride))[0]= src[-1+2*stride]*0x01010101;
  1515. ((uint32_t*)(src+3*stride))[0]= src[-1+3*stride]*0x01010101;
  1516. }
  1517. static void pred4x4_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1518. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride]
  1519. + src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 4) >>3;
  1520. ((uint32_t*)(src+0*stride))[0]=
  1521. ((uint32_t*)(src+1*stride))[0]=
  1522. ((uint32_t*)(src+2*stride))[0]=
  1523. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1524. }
  1525. static void pred4x4_left_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1526. const int dc= ( src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 2) >>2;
  1527. ((uint32_t*)(src+0*stride))[0]=
  1528. ((uint32_t*)(src+1*stride))[0]=
  1529. ((uint32_t*)(src+2*stride))[0]=
  1530. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1531. }
  1532. static void pred4x4_top_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1533. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride] + 2) >>2;
  1534. ((uint32_t*)(src+0*stride))[0]=
  1535. ((uint32_t*)(src+1*stride))[0]=
  1536. ((uint32_t*)(src+2*stride))[0]=
  1537. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1538. }
  1539. static void pred4x4_128_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1540. ((uint32_t*)(src+0*stride))[0]=
  1541. ((uint32_t*)(src+1*stride))[0]=
  1542. ((uint32_t*)(src+2*stride))[0]=
  1543. ((uint32_t*)(src+3*stride))[0]= 128U*0x01010101U;
  1544. }
  1545. #define LOAD_TOP_RIGHT_EDGE\
  1546. const int t4= topright[0];\
  1547. const int t5= topright[1];\
  1548. const int t6= topright[2];\
  1549. const int t7= topright[3];\
  1550. #define LOAD_LEFT_EDGE\
  1551. const int l0= src[-1+0*stride];\
  1552. const int l1= src[-1+1*stride];\
  1553. const int l2= src[-1+2*stride];\
  1554. const int l3= src[-1+3*stride];\
  1555. #define LOAD_TOP_EDGE\
  1556. const int t0= src[ 0-1*stride];\
  1557. const int t1= src[ 1-1*stride];\
  1558. const int t2= src[ 2-1*stride];\
  1559. const int t3= src[ 3-1*stride];\
  1560. static void pred4x4_down_right_c(uint8_t *src, uint8_t *topright, int stride){
  1561. const int lt= src[-1-1*stride];
  1562. LOAD_TOP_EDGE
  1563. LOAD_LEFT_EDGE
  1564. src[0+3*stride]=(l3 + 2*l2 + l1 + 2)>>2;
  1565. src[0+2*stride]=
  1566. src[1+3*stride]=(l2 + 2*l1 + l0 + 2)>>2;
  1567. src[0+1*stride]=
  1568. src[1+2*stride]=
  1569. src[2+3*stride]=(l1 + 2*l0 + lt + 2)>>2;
  1570. src[0+0*stride]=
  1571. src[1+1*stride]=
  1572. src[2+2*stride]=
  1573. src[3+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1574. src[1+0*stride]=
  1575. src[2+1*stride]=
  1576. src[3+2*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1577. src[2+0*stride]=
  1578. src[3+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1579. src[3+0*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1580. }
  1581. static void pred4x4_down_left_c(uint8_t *src, uint8_t *topright, int stride){
  1582. LOAD_TOP_EDGE
  1583. LOAD_TOP_RIGHT_EDGE
  1584. // LOAD_LEFT_EDGE
  1585. src[0+0*stride]=(t0 + t2 + 2*t1 + 2)>>2;
  1586. src[1+0*stride]=
  1587. src[0+1*stride]=(t1 + t3 + 2*t2 + 2)>>2;
  1588. src[2+0*stride]=
  1589. src[1+1*stride]=
  1590. src[0+2*stride]=(t2 + t4 + 2*t3 + 2)>>2;
  1591. src[3+0*stride]=
  1592. src[2+1*stride]=
  1593. src[1+2*stride]=
  1594. src[0+3*stride]=(t3 + t5 + 2*t4 + 2)>>2;
  1595. src[3+1*stride]=
  1596. src[2+2*stride]=
  1597. src[1+3*stride]=(t4 + t6 + 2*t5 + 2)>>2;
  1598. src[3+2*stride]=
  1599. src[2+3*stride]=(t5 + t7 + 2*t6 + 2)>>2;
  1600. src[3+3*stride]=(t6 + 3*t7 + 2)>>2;
  1601. }
  1602. static void pred4x4_vertical_right_c(uint8_t *src, uint8_t *topright, int stride){
  1603. const int lt= src[-1-1*stride];
  1604. LOAD_TOP_EDGE
  1605. LOAD_LEFT_EDGE
  1606. const __attribute__((unused)) int unu= l3;
  1607. src[0+0*stride]=
  1608. src[1+2*stride]=(lt + t0 + 1)>>1;
  1609. src[1+0*stride]=
  1610. src[2+2*stride]=(t0 + t1 + 1)>>1;
  1611. src[2+0*stride]=
  1612. src[3+2*stride]=(t1 + t2 + 1)>>1;
  1613. src[3+0*stride]=(t2 + t3 + 1)>>1;
  1614. src[0+1*stride]=
  1615. src[1+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1616. src[1+1*stride]=
  1617. src[2+3*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1618. src[2+1*stride]=
  1619. src[3+3*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1620. src[3+1*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1621. src[0+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  1622. src[0+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1623. }
  1624. static void pred4x4_vertical_left_c(uint8_t *src, uint8_t *topright, int stride){
  1625. LOAD_TOP_EDGE
  1626. LOAD_TOP_RIGHT_EDGE
  1627. const __attribute__((unused)) int unu= t7;
  1628. src[0+0*stride]=(t0 + t1 + 1)>>1;
  1629. src[1+0*stride]=
  1630. src[0+2*stride]=(t1 + t2 + 1)>>1;
  1631. src[2+0*stride]=
  1632. src[1+2*stride]=(t2 + t3 + 1)>>1;
  1633. src[3+0*stride]=
  1634. src[2+2*stride]=(t3 + t4+ 1)>>1;
  1635. src[3+2*stride]=(t4 + t5+ 1)>>1;
  1636. src[0+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1637. src[1+1*stride]=
  1638. src[0+3*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1639. src[2+1*stride]=
  1640. src[1+3*stride]=(t2 + 2*t3 + t4 + 2)>>2;
  1641. src[3+1*stride]=
  1642. src[2+3*stride]=(t3 + 2*t4 + t5 + 2)>>2;
  1643. src[3+3*stride]=(t4 + 2*t5 + t6 + 2)>>2;
  1644. }
  1645. static void pred4x4_horizontal_up_c(uint8_t *src, uint8_t *topright, int stride){
  1646. LOAD_LEFT_EDGE
  1647. src[0+0*stride]=(l0 + l1 + 1)>>1;
  1648. src[1+0*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1649. src[2+0*stride]=
  1650. src[0+1*stride]=(l1 + l2 + 1)>>1;
  1651. src[3+0*stride]=
  1652. src[1+1*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  1653. src[2+1*stride]=
  1654. src[0+2*stride]=(l2 + l3 + 1)>>1;
  1655. src[3+1*stride]=
  1656. src[1+2*stride]=(l2 + 2*l3 + l3 + 2)>>2;
  1657. src[3+2*stride]=
  1658. src[1+3*stride]=
  1659. src[0+3*stride]=
  1660. src[2+2*stride]=
  1661. src[2+3*stride]=
  1662. src[3+3*stride]=l3;
  1663. }
  1664. static void pred4x4_horizontal_down_c(uint8_t *src, uint8_t *topright, int stride){
  1665. const int lt= src[-1-1*stride];
  1666. LOAD_TOP_EDGE
  1667. LOAD_LEFT_EDGE
  1668. const __attribute__((unused)) int unu= t3;
  1669. src[0+0*stride]=
  1670. src[2+1*stride]=(lt + l0 + 1)>>1;
  1671. src[1+0*stride]=
  1672. src[3+1*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1673. src[2+0*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1674. src[3+0*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1675. src[0+1*stride]=
  1676. src[2+2*stride]=(l0 + l1 + 1)>>1;
  1677. src[1+1*stride]=
  1678. src[3+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  1679. src[0+2*stride]=
  1680. src[2+3*stride]=(l1 + l2+ 1)>>1;
  1681. src[1+2*stride]=
  1682. src[3+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1683. src[0+3*stride]=(l2 + l3 + 1)>>1;
  1684. src[1+3*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  1685. }
  1686. void ff_pred16x16_vertical_c(uint8_t *src, int stride){
  1687. int i;
  1688. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1689. const uint32_t b= ((uint32_t*)(src-stride))[1];
  1690. const uint32_t c= ((uint32_t*)(src-stride))[2];
  1691. const uint32_t d= ((uint32_t*)(src-stride))[3];
  1692. for(i=0; i<16; i++){
  1693. ((uint32_t*)(src+i*stride))[0]= a;
  1694. ((uint32_t*)(src+i*stride))[1]= b;
  1695. ((uint32_t*)(src+i*stride))[2]= c;
  1696. ((uint32_t*)(src+i*stride))[3]= d;
  1697. }
  1698. }
  1699. void ff_pred16x16_horizontal_c(uint8_t *src, int stride){
  1700. int i;
  1701. for(i=0; i<16; i++){
  1702. ((uint32_t*)(src+i*stride))[0]=
  1703. ((uint32_t*)(src+i*stride))[1]=
  1704. ((uint32_t*)(src+i*stride))[2]=
  1705. ((uint32_t*)(src+i*stride))[3]= src[-1+i*stride]*0x01010101;
  1706. }
  1707. }
  1708. void ff_pred16x16_dc_c(uint8_t *src, int stride){
  1709. int i, dc=0;
  1710. for(i=0;i<16; i++){
  1711. dc+= src[-1+i*stride];
  1712. }
  1713. for(i=0;i<16; i++){
  1714. dc+= src[i-stride];
  1715. }
  1716. dc= 0x01010101*((dc + 16)>>5);
  1717. for(i=0; i<16; i++){
  1718. ((uint32_t*)(src+i*stride))[0]=
  1719. ((uint32_t*)(src+i*stride))[1]=
  1720. ((uint32_t*)(src+i*stride))[2]=
  1721. ((uint32_t*)(src+i*stride))[3]= dc;
  1722. }
  1723. }
  1724. static void pred16x16_left_dc_c(uint8_t *src, int stride){
  1725. int i, dc=0;
  1726. for(i=0;i<16; i++){
  1727. dc+= src[-1+i*stride];
  1728. }
  1729. dc= 0x01010101*((dc + 8)>>4);
  1730. for(i=0; i<16; i++){
  1731. ((uint32_t*)(src+i*stride))[0]=
  1732. ((uint32_t*)(src+i*stride))[1]=
  1733. ((uint32_t*)(src+i*stride))[2]=
  1734. ((uint32_t*)(src+i*stride))[3]= dc;
  1735. }
  1736. }
  1737. static void pred16x16_top_dc_c(uint8_t *src, int stride){
  1738. int i, dc=0;
  1739. for(i=0;i<16; i++){
  1740. dc+= src[i-stride];
  1741. }
  1742. dc= 0x01010101*((dc + 8)>>4);
  1743. for(i=0; i<16; i++){
  1744. ((uint32_t*)(src+i*stride))[0]=
  1745. ((uint32_t*)(src+i*stride))[1]=
  1746. ((uint32_t*)(src+i*stride))[2]=
  1747. ((uint32_t*)(src+i*stride))[3]= dc;
  1748. }
  1749. }
  1750. void ff_pred16x16_128_dc_c(uint8_t *src, int stride){
  1751. int i;
  1752. for(i=0; i<16; i++){
  1753. ((uint32_t*)(src+i*stride))[0]=
  1754. ((uint32_t*)(src+i*stride))[1]=
  1755. ((uint32_t*)(src+i*stride))[2]=
  1756. ((uint32_t*)(src+i*stride))[3]= 0x01010101U*128U;
  1757. }
  1758. }
  1759. static inline void pred16x16_plane_compat_c(uint8_t *src, int stride, const int svq3){
  1760. int i, j, k;
  1761. int a;
  1762. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  1763. const uint8_t * const src0 = src+7-stride;
  1764. const uint8_t *src1 = src+8*stride-1;
  1765. const uint8_t *src2 = src1-2*stride; // == src+6*stride-1;
  1766. int H = src0[1] - src0[-1];
  1767. int V = src1[0] - src2[ 0];
  1768. for(k=2; k<=8; ++k) {
  1769. src1 += stride; src2 -= stride;
  1770. H += k*(src0[k] - src0[-k]);
  1771. V += k*(src1[0] - src2[ 0]);
  1772. }
  1773. if(svq3){
  1774. H = ( 5*(H/4) ) / 16;
  1775. V = ( 5*(V/4) ) / 16;
  1776. /* required for 100% accuracy */
  1777. i = H; H = V; V = i;
  1778. }else{
  1779. H = ( 5*H+32 ) >> 6;
  1780. V = ( 5*V+32 ) >> 6;
  1781. }
  1782. a = 16*(src1[0] + src2[16] + 1) - 7*(V+H);
  1783. for(j=16; j>0; --j) {
  1784. int b = a;
  1785. a += V;
  1786. for(i=-16; i<0; i+=4) {
  1787. src[16+i] = cm[ (b ) >> 5 ];
  1788. src[17+i] = cm[ (b+ H) >> 5 ];
  1789. src[18+i] = cm[ (b+2*H) >> 5 ];
  1790. src[19+i] = cm[ (b+3*H) >> 5 ];
  1791. b += 4*H;
  1792. }
  1793. src += stride;
  1794. }
  1795. }
  1796. void ff_pred16x16_plane_c(uint8_t *src, int stride){
  1797. pred16x16_plane_compat_c(src, stride, 0);
  1798. }
  1799. void ff_pred8x8_vertical_c(uint8_t *src, int stride){
  1800. int i;
  1801. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1802. const uint32_t b= ((uint32_t*)(src-stride))[1];
  1803. for(i=0; i<8; i++){
  1804. ((uint32_t*)(src+i*stride))[0]= a;
  1805. ((uint32_t*)(src+i*stride))[1]= b;
  1806. }
  1807. }
  1808. void ff_pred8x8_horizontal_c(uint8_t *src, int stride){
  1809. int i;
  1810. for(i=0; i<8; i++){
  1811. ((uint32_t*)(src+i*stride))[0]=
  1812. ((uint32_t*)(src+i*stride))[1]= src[-1+i*stride]*0x01010101;
  1813. }
  1814. }
  1815. void ff_pred8x8_128_dc_c(uint8_t *src, int stride){
  1816. int i;
  1817. for(i=0; i<8; i++){
  1818. ((uint32_t*)(src+i*stride))[0]=
  1819. ((uint32_t*)(src+i*stride))[1]= 0x01010101U*128U;
  1820. }
  1821. }
  1822. static void pred8x8_left_dc_c(uint8_t *src, int stride){
  1823. int i;
  1824. int dc0, dc2;
  1825. dc0=dc2=0;
  1826. for(i=0;i<4; i++){
  1827. dc0+= src[-1+i*stride];
  1828. dc2+= src[-1+(i+4)*stride];
  1829. }
  1830. dc0= 0x01010101*((dc0 + 2)>>2);
  1831. dc2= 0x01010101*((dc2 + 2)>>2);
  1832. for(i=0; i<4; i++){
  1833. ((uint32_t*)(src+i*stride))[0]=
  1834. ((uint32_t*)(src+i*stride))[1]= dc0;
  1835. }
  1836. for(i=4; i<8; i++){
  1837. ((uint32_t*)(src+i*stride))[0]=
  1838. ((uint32_t*)(src+i*stride))[1]= dc2;
  1839. }
  1840. }
  1841. static void pred8x8_top_dc_c(uint8_t *src, int stride){
  1842. int i;
  1843. int dc0, dc1;
  1844. dc0=dc1=0;
  1845. for(i=0;i<4; i++){
  1846. dc0+= src[i-stride];
  1847. dc1+= src[4+i-stride];
  1848. }
  1849. dc0= 0x01010101*((dc0 + 2)>>2);
  1850. dc1= 0x01010101*((dc1 + 2)>>2);
  1851. for(i=0; i<4; i++){
  1852. ((uint32_t*)(src+i*stride))[0]= dc0;
  1853. ((uint32_t*)(src+i*stride))[1]= dc1;
  1854. }
  1855. for(i=4; i<8; i++){
  1856. ((uint32_t*)(src+i*stride))[0]= dc0;
  1857. ((uint32_t*)(src+i*stride))[1]= dc1;
  1858. }
  1859. }
  1860. void ff_pred8x8_dc_c(uint8_t *src, int stride){
  1861. int i;
  1862. int dc0, dc1, dc2, dc3;
  1863. dc0=dc1=dc2=0;
  1864. for(i=0;i<4; i++){
  1865. dc0+= src[-1+i*stride] + src[i-stride];
  1866. dc1+= src[4+i-stride];
  1867. dc2+= src[-1+(i+4)*stride];
  1868. }
  1869. dc3= 0x01010101*((dc1 + dc2 + 4)>>3);
  1870. dc0= 0x01010101*((dc0 + 4)>>3);
  1871. dc1= 0x01010101*((dc1 + 2)>>2);
  1872. dc2= 0x01010101*((dc2 + 2)>>2);
  1873. for(i=0; i<4; i++){
  1874. ((uint32_t*)(src+i*stride))[0]= dc0;
  1875. ((uint32_t*)(src+i*stride))[1]= dc1;
  1876. }
  1877. for(i=4; i<8; i++){
  1878. ((uint32_t*)(src+i*stride))[0]= dc2;
  1879. ((uint32_t*)(src+i*stride))[1]= dc3;
  1880. }
  1881. }
  1882. void ff_pred8x8_plane_c(uint8_t *src, int stride){
  1883. int j, k;
  1884. int a;
  1885. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  1886. const uint8_t * const src0 = src+3-stride;
  1887. const uint8_t *src1 = src+4*stride-1;
  1888. const uint8_t *src2 = src1-2*stride; // == src+2*stride-1;
  1889. int H = src0[1] - src0[-1];
  1890. int V = src1[0] - src2[ 0];
  1891. for(k=2; k<=4; ++k) {
  1892. src1 += stride; src2 -= stride;
  1893. H += k*(src0[k] - src0[-k]);
  1894. V += k*(src1[0] - src2[ 0]);
  1895. }
  1896. H = ( 17*H+16 ) >> 5;
  1897. V = ( 17*V+16 ) >> 5;
  1898. a = 16*(src1[0] + src2[8]+1) - 3*(V+H);
  1899. for(j=8; j>0; --j) {
  1900. int b = a;
  1901. a += V;
  1902. src[0] = cm[ (b ) >> 5 ];
  1903. src[1] = cm[ (b+ H) >> 5 ];
  1904. src[2] = cm[ (b+2*H) >> 5 ];
  1905. src[3] = cm[ (b+3*H) >> 5 ];
  1906. src[4] = cm[ (b+4*H) >> 5 ];
  1907. src[5] = cm[ (b+5*H) >> 5 ];
  1908. src[6] = cm[ (b+6*H) >> 5 ];
  1909. src[7] = cm[ (b+7*H) >> 5 ];
  1910. src += stride;
  1911. }
  1912. }
  1913. #define SRC(x,y) src[(x)+(y)*stride]
  1914. #define PL(y) \
  1915. const int l##y = (SRC(-1,y-1) + 2*SRC(-1,y) + SRC(-1,y+1) + 2) >> 2;
  1916. #define PREDICT_8x8_LOAD_LEFT \
  1917. const int l0 = ((has_topleft ? SRC(-1,-1) : SRC(-1,0)) \
  1918. + 2*SRC(-1,0) + SRC(-1,1) + 2) >> 2; \
  1919. PL(1) PL(2) PL(3) PL(4) PL(5) PL(6) \
  1920. const int l7 attribute_unused = (SRC(-1,6) + 3*SRC(-1,7) + 2) >> 2
  1921. #define PT(x) \
  1922. const int t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
  1923. #define PREDICT_8x8_LOAD_TOP \
  1924. const int t0 = ((has_topleft ? SRC(-1,-1) : SRC(0,-1)) \
  1925. + 2*SRC(0,-1) + SRC(1,-1) + 2) >> 2; \
  1926. PT(1) PT(2) PT(3) PT(4) PT(5) PT(6) \
  1927. const int t7 attribute_unused = ((has_topright ? SRC(8,-1) : SRC(7,-1)) \
  1928. + 2*SRC(7,-1) + SRC(6,-1) + 2) >> 2
  1929. #define PTR(x) \
  1930. t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
  1931. #define PREDICT_8x8_LOAD_TOPRIGHT \
  1932. int t8, t9, t10, t11, t12, t13, t14, t15; \
  1933. if(has_topright) { \
  1934. PTR(8) PTR(9) PTR(10) PTR(11) PTR(12) PTR(13) PTR(14) \
  1935. t15 = (SRC(14,-1) + 3*SRC(15,-1) + 2) >> 2; \
  1936. } else t8=t9=t10=t11=t12=t13=t14=t15= SRC(7,-1);
  1937. #define PREDICT_8x8_LOAD_TOPLEFT \
  1938. const int lt = (SRC(-1,0) + 2*SRC(-1,-1) + SRC(0,-1) + 2) >> 2
  1939. #define PREDICT_8x8_DC(v) \
  1940. int y; \
  1941. for( y = 0; y < 8; y++ ) { \
  1942. ((uint32_t*)src)[0] = \
  1943. ((uint32_t*)src)[1] = v; \
  1944. src += stride; \
  1945. }
  1946. static void pred8x8l_128_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  1947. {
  1948. PREDICT_8x8_DC(0x80808080);
  1949. }
  1950. static void pred8x8l_left_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  1951. {
  1952. PREDICT_8x8_LOAD_LEFT;
  1953. const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7+4) >> 3) * 0x01010101;
  1954. PREDICT_8x8_DC(dc);
  1955. }
  1956. static void pred8x8l_top_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  1957. {
  1958. PREDICT_8x8_LOAD_TOP;
  1959. const uint32_t dc = ((t0+t1+t2+t3+t4+t5+t6+t7+4) >> 3) * 0x01010101;
  1960. PREDICT_8x8_DC(dc);
  1961. }
  1962. static void pred8x8l_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  1963. {
  1964. PREDICT_8x8_LOAD_LEFT;
  1965. PREDICT_8x8_LOAD_TOP;
  1966. const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7
  1967. +t0+t1+t2+t3+t4+t5+t6+t7+8) >> 4) * 0x01010101;
  1968. PREDICT_8x8_DC(dc);
  1969. }
  1970. static void pred8x8l_horizontal_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  1971. {
  1972. PREDICT_8x8_LOAD_LEFT;
  1973. #define ROW(y) ((uint32_t*)(src+y*stride))[0] =\
  1974. ((uint32_t*)(src+y*stride))[1] = 0x01010101 * l##y
  1975. ROW(0); ROW(1); ROW(2); ROW(3); ROW(4); ROW(5); ROW(6); ROW(7);
  1976. #undef ROW
  1977. }
  1978. static void pred8x8l_vertical_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  1979. {
  1980. int y;
  1981. PREDICT_8x8_LOAD_TOP;
  1982. src[0] = t0;
  1983. src[1] = t1;
  1984. src[2] = t2;
  1985. src[3] = t3;
  1986. src[4] = t4;
  1987. src[5] = t5;
  1988. src[6] = t6;
  1989. src[7] = t7;
  1990. for( y = 1; y < 8; y++ )
  1991. *(uint64_t*)(src+y*stride) = *(uint64_t*)src;
  1992. }
  1993. static void pred8x8l_down_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  1994. {
  1995. PREDICT_8x8_LOAD_TOP;
  1996. PREDICT_8x8_LOAD_TOPRIGHT;
  1997. SRC(0,0)= (t0 + 2*t1 + t2 + 2) >> 2;
  1998. SRC(0,1)=SRC(1,0)= (t1 + 2*t2 + t3 + 2) >> 2;
  1999. SRC(0,2)=SRC(1,1)=SRC(2,0)= (t2 + 2*t3 + t4 + 2) >> 2;
  2000. SRC(0,3)=SRC(1,2)=SRC(2,1)=SRC(3,0)= (t3 + 2*t4 + t5 + 2) >> 2;
  2001. SRC(0,4)=SRC(1,3)=SRC(2,2)=SRC(3,1)=SRC(4,0)= (t4 + 2*t5 + t6 + 2) >> 2;
  2002. SRC(0,5)=SRC(1,4)=SRC(2,3)=SRC(3,2)=SRC(4,1)=SRC(5,0)= (t5 + 2*t6 + t7 + 2) >> 2;
  2003. SRC(0,6)=SRC(1,5)=SRC(2,4)=SRC(3,3)=SRC(4,2)=SRC(5,1)=SRC(6,0)= (t6 + 2*t7 + t8 + 2) >> 2;
  2004. SRC(0,7)=SRC(1,6)=SRC(2,5)=SRC(3,4)=SRC(4,3)=SRC(5,2)=SRC(6,1)=SRC(7,0)= (t7 + 2*t8 + t9 + 2) >> 2;
  2005. SRC(1,7)=SRC(2,6)=SRC(3,5)=SRC(4,4)=SRC(5,3)=SRC(6,2)=SRC(7,1)= (t8 + 2*t9 + t10 + 2) >> 2;
  2006. SRC(2,7)=SRC(3,6)=SRC(4,5)=SRC(5,4)=SRC(6,3)=SRC(7,2)= (t9 + 2*t10 + t11 + 2) >> 2;
  2007. SRC(3,7)=SRC(4,6)=SRC(5,5)=SRC(6,4)=SRC(7,3)= (t10 + 2*t11 + t12 + 2) >> 2;
  2008. SRC(4,7)=SRC(5,6)=SRC(6,5)=SRC(7,4)= (t11 + 2*t12 + t13 + 2) >> 2;
  2009. SRC(5,7)=SRC(6,6)=SRC(7,5)= (t12 + 2*t13 + t14 + 2) >> 2;
  2010. SRC(6,7)=SRC(7,6)= (t13 + 2*t14 + t15 + 2) >> 2;
  2011. SRC(7,7)= (t14 + 3*t15 + 2) >> 2;
  2012. }
  2013. static void pred8x8l_down_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2014. {
  2015. PREDICT_8x8_LOAD_TOP;
  2016. PREDICT_8x8_LOAD_LEFT;
  2017. PREDICT_8x8_LOAD_TOPLEFT;
  2018. SRC(0,7)= (l7 + 2*l6 + l5 + 2) >> 2;
  2019. SRC(0,6)=SRC(1,7)= (l6 + 2*l5 + l4 + 2) >> 2;
  2020. SRC(0,5)=SRC(1,6)=SRC(2,7)= (l5 + 2*l4 + l3 + 2) >> 2;
  2021. SRC(0,4)=SRC(1,5)=SRC(2,6)=SRC(3,7)= (l4 + 2*l3 + l2 + 2) >> 2;
  2022. SRC(0,3)=SRC(1,4)=SRC(2,5)=SRC(3,6)=SRC(4,7)= (l3 + 2*l2 + l1 + 2) >> 2;
  2023. SRC(0,2)=SRC(1,3)=SRC(2,4)=SRC(3,5)=SRC(4,6)=SRC(5,7)= (l2 + 2*l1 + l0 + 2) >> 2;
  2024. SRC(0,1)=SRC(1,2)=SRC(2,3)=SRC(3,4)=SRC(4,5)=SRC(5,6)=SRC(6,7)= (l1 + 2*l0 + lt + 2) >> 2;
  2025. SRC(0,0)=SRC(1,1)=SRC(2,2)=SRC(3,3)=SRC(4,4)=SRC(5,5)=SRC(6,6)=SRC(7,7)= (l0 + 2*lt + t0 + 2) >> 2;
  2026. SRC(1,0)=SRC(2,1)=SRC(3,2)=SRC(4,3)=SRC(5,4)=SRC(6,5)=SRC(7,6)= (lt + 2*t0 + t1 + 2) >> 2;
  2027. SRC(2,0)=SRC(3,1)=SRC(4,2)=SRC(5,3)=SRC(6,4)=SRC(7,5)= (t0 + 2*t1 + t2 + 2) >> 2;
  2028. SRC(3,0)=SRC(4,1)=SRC(5,2)=SRC(6,3)=SRC(7,4)= (t1 + 2*t2 + t3 + 2) >> 2;
  2029. SRC(4,0)=SRC(5,1)=SRC(6,2)=SRC(7,3)= (t2 + 2*t3 + t4 + 2) >> 2;
  2030. SRC(5,0)=SRC(6,1)=SRC(7,2)= (t3 + 2*t4 + t5 + 2) >> 2;
  2031. SRC(6,0)=SRC(7,1)= (t4 + 2*t5 + t6 + 2) >> 2;
  2032. SRC(7,0)= (t5 + 2*t6 + t7 + 2) >> 2;
  2033. }
  2034. static void pred8x8l_vertical_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2035. {
  2036. PREDICT_8x8_LOAD_TOP;
  2037. PREDICT_8x8_LOAD_LEFT;
  2038. PREDICT_8x8_LOAD_TOPLEFT;
  2039. SRC(0,6)= (l5 + 2*l4 + l3 + 2) >> 2;
  2040. SRC(0,7)= (l6 + 2*l5 + l4 + 2) >> 2;
  2041. SRC(0,4)=SRC(1,6)= (l3 + 2*l2 + l1 + 2) >> 2;
  2042. SRC(0,5)=SRC(1,7)= (l4 + 2*l3 + l2 + 2) >> 2;
  2043. SRC(0,2)=SRC(1,4)=SRC(2,6)= (l1 + 2*l0 + lt + 2) >> 2;
  2044. SRC(0,3)=SRC(1,5)=SRC(2,7)= (l2 + 2*l1 + l0 + 2) >> 2;
  2045. SRC(0,1)=SRC(1,3)=SRC(2,5)=SRC(3,7)= (l0 + 2*lt + t0 + 2) >> 2;
  2046. SRC(0,0)=SRC(1,2)=SRC(2,4)=SRC(3,6)= (lt + t0 + 1) >> 1;
  2047. SRC(1,1)=SRC(2,3)=SRC(3,5)=SRC(4,7)= (lt + 2*t0 + t1 + 2) >> 2;
  2048. SRC(1,0)=SRC(2,2)=SRC(3,4)=SRC(4,6)= (t0 + t1 + 1) >> 1;
  2049. SRC(2,1)=SRC(3,3)=SRC(4,5)=SRC(5,7)= (t0 + 2*t1 + t2 + 2) >> 2;
  2050. SRC(2,0)=SRC(3,2)=SRC(4,4)=SRC(5,6)= (t1 + t2 + 1) >> 1;
  2051. SRC(3,1)=SRC(4,3)=SRC(5,5)=SRC(6,7)= (t1 + 2*t2 + t3 + 2) >> 2;
  2052. SRC(3,0)=SRC(4,2)=SRC(5,4)=SRC(6,6)= (t2 + t3 + 1) >> 1;
  2053. SRC(4,1)=SRC(5,3)=SRC(6,5)=SRC(7,7)= (t2 + 2*t3 + t4 + 2) >> 2;
  2054. SRC(4,0)=SRC(5,2)=SRC(6,4)=SRC(7,6)= (t3 + t4 + 1) >> 1;
  2055. SRC(5,1)=SRC(6,3)=SRC(7,5)= (t3 + 2*t4 + t5 + 2) >> 2;
  2056. SRC(5,0)=SRC(6,2)=SRC(7,4)= (t4 + t5 + 1) >> 1;
  2057. SRC(6,1)=SRC(7,3)= (t4 + 2*t5 + t6 + 2) >> 2;
  2058. SRC(6,0)=SRC(7,2)= (t5 + t6 + 1) >> 1;
  2059. SRC(7,1)= (t5 + 2*t6 + t7 + 2) >> 2;
  2060. SRC(7,0)= (t6 + t7 + 1) >> 1;
  2061. }
  2062. static void pred8x8l_horizontal_down_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2063. {
  2064. PREDICT_8x8_LOAD_TOP;
  2065. PREDICT_8x8_LOAD_LEFT;
  2066. PREDICT_8x8_LOAD_TOPLEFT;
  2067. SRC(0,7)= (l6 + l7 + 1) >> 1;
  2068. SRC(1,7)= (l5 + 2*l6 + l7 + 2) >> 2;
  2069. SRC(0,6)=SRC(2,7)= (l5 + l6 + 1) >> 1;
  2070. SRC(1,6)=SRC(3,7)= (l4 + 2*l5 + l6 + 2) >> 2;
  2071. SRC(0,5)=SRC(2,6)=SRC(4,7)= (l4 + l5 + 1) >> 1;
  2072. SRC(1,5)=SRC(3,6)=SRC(5,7)= (l3 + 2*l4 + l5 + 2) >> 2;
  2073. SRC(0,4)=SRC(2,5)=SRC(4,6)=SRC(6,7)= (l3 + l4 + 1) >> 1;
  2074. SRC(1,4)=SRC(3,5)=SRC(5,6)=SRC(7,7)= (l2 + 2*l3 + l4 + 2) >> 2;
  2075. SRC(0,3)=SRC(2,4)=SRC(4,5)=SRC(6,6)= (l2 + l3 + 1) >> 1;
  2076. SRC(1,3)=SRC(3,4)=SRC(5,5)=SRC(7,6)= (l1 + 2*l2 + l3 + 2) >> 2;
  2077. SRC(0,2)=SRC(2,3)=SRC(4,4)=SRC(6,5)= (l1 + l2 + 1) >> 1;
  2078. SRC(1,2)=SRC(3,3)=SRC(5,4)=SRC(7,5)= (l0 + 2*l1 + l2 + 2) >> 2;
  2079. SRC(0,1)=SRC(2,2)=SRC(4,3)=SRC(6,4)= (l0 + l1 + 1) >> 1;
  2080. SRC(1,1)=SRC(3,2)=SRC(5,3)=SRC(7,4)= (lt + 2*l0 + l1 + 2) >> 2;
  2081. SRC(0,0)=SRC(2,1)=SRC(4,2)=SRC(6,3)= (lt + l0 + 1) >> 1;
  2082. SRC(1,0)=SRC(3,1)=SRC(5,2)=SRC(7,3)= (l0 + 2*lt + t0 + 2) >> 2;
  2083. SRC(2,0)=SRC(4,1)=SRC(6,2)= (t1 + 2*t0 + lt + 2) >> 2;
  2084. SRC(3,0)=SRC(5,1)=SRC(7,2)= (t2 + 2*t1 + t0 + 2) >> 2;
  2085. SRC(4,0)=SRC(6,1)= (t3 + 2*t2 + t1 + 2) >> 2;
  2086. SRC(5,0)=SRC(7,1)= (t4 + 2*t3 + t2 + 2) >> 2;
  2087. SRC(6,0)= (t5 + 2*t4 + t3 + 2) >> 2;
  2088. SRC(7,0)= (t6 + 2*t5 + t4 + 2) >> 2;
  2089. }
  2090. static void pred8x8l_vertical_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2091. {
  2092. PREDICT_8x8_LOAD_TOP;
  2093. PREDICT_8x8_LOAD_TOPRIGHT;
  2094. SRC(0,0)= (t0 + t1 + 1) >> 1;
  2095. SRC(0,1)= (t0 + 2*t1 + t2 + 2) >> 2;
  2096. SRC(0,2)=SRC(1,0)= (t1 + t2 + 1) >> 1;
  2097. SRC(0,3)=SRC(1,1)= (t1 + 2*t2 + t3 + 2) >> 2;
  2098. SRC(0,4)=SRC(1,2)=SRC(2,0)= (t2 + t3 + 1) >> 1;
  2099. SRC(0,5)=SRC(1,3)=SRC(2,1)= (t2 + 2*t3 + t4 + 2) >> 2;
  2100. SRC(0,6)=SRC(1,4)=SRC(2,2)=SRC(3,0)= (t3 + t4 + 1) >> 1;
  2101. SRC(0,7)=SRC(1,5)=SRC(2,3)=SRC(3,1)= (t3 + 2*t4 + t5 + 2) >> 2;
  2102. SRC(1,6)=SRC(2,4)=SRC(3,2)=SRC(4,0)= (t4 + t5 + 1) >> 1;
  2103. SRC(1,7)=SRC(2,5)=SRC(3,3)=SRC(4,1)= (t4 + 2*t5 + t6 + 2) >> 2;
  2104. SRC(2,6)=SRC(3,4)=SRC(4,2)=SRC(5,0)= (t5 + t6 + 1) >> 1;
  2105. SRC(2,7)=SRC(3,5)=SRC(4,3)=SRC(5,1)= (t5 + 2*t6 + t7 + 2) >> 2;
  2106. SRC(3,6)=SRC(4,4)=SRC(5,2)=SRC(6,0)= (t6 + t7 + 1) >> 1;
  2107. SRC(3,7)=SRC(4,5)=SRC(5,3)=SRC(6,1)= (t6 + 2*t7 + t8 + 2) >> 2;
  2108. SRC(4,6)=SRC(5,4)=SRC(6,2)=SRC(7,0)= (t7 + t8 + 1) >> 1;
  2109. SRC(4,7)=SRC(5,5)=SRC(6,3)=SRC(7,1)= (t7 + 2*t8 + t9 + 2) >> 2;
  2110. SRC(5,6)=SRC(6,4)=SRC(7,2)= (t8 + t9 + 1) >> 1;
  2111. SRC(5,7)=SRC(6,5)=SRC(7,3)= (t8 + 2*t9 + t10 + 2) >> 2;
  2112. SRC(6,6)=SRC(7,4)= (t9 + t10 + 1) >> 1;
  2113. SRC(6,7)=SRC(7,5)= (t9 + 2*t10 + t11 + 2) >> 2;
  2114. SRC(7,6)= (t10 + t11 + 1) >> 1;
  2115. SRC(7,7)= (t10 + 2*t11 + t12 + 2) >> 2;
  2116. }
  2117. static void pred8x8l_horizontal_up_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2118. {
  2119. PREDICT_8x8_LOAD_LEFT;
  2120. SRC(0,0)= (l0 + l1 + 1) >> 1;
  2121. SRC(1,0)= (l0 + 2*l1 + l2 + 2) >> 2;
  2122. SRC(0,1)=SRC(2,0)= (l1 + l2 + 1) >> 1;
  2123. SRC(1,1)=SRC(3,0)= (l1 + 2*l2 + l3 + 2) >> 2;
  2124. SRC(0,2)=SRC(2,1)=SRC(4,0)= (l2 + l3 + 1) >> 1;
  2125. SRC(1,2)=SRC(3,1)=SRC(5,0)= (l2 + 2*l3 + l4 + 2) >> 2;
  2126. SRC(0,3)=SRC(2,2)=SRC(4,1)=SRC(6,0)= (l3 + l4 + 1) >> 1;
  2127. SRC(1,3)=SRC(3,2)=SRC(5,1)=SRC(7,0)= (l3 + 2*l4 + l5 + 2) >> 2;
  2128. SRC(0,4)=SRC(2,3)=SRC(4,2)=SRC(6,1)= (l4 + l5 + 1) >> 1;
  2129. SRC(1,4)=SRC(3,3)=SRC(5,2)=SRC(7,1)= (l4 + 2*l5 + l6 + 2) >> 2;
  2130. SRC(0,5)=SRC(2,4)=SRC(4,3)=SRC(6,2)= (l5 + l6 + 1) >> 1;
  2131. SRC(1,5)=SRC(3,4)=SRC(5,3)=SRC(7,2)= (l5 + 2*l6 + l7 + 2) >> 2;
  2132. SRC(0,6)=SRC(2,5)=SRC(4,4)=SRC(6,3)= (l6 + l7 + 1) >> 1;
  2133. SRC(1,6)=SRC(3,5)=SRC(5,4)=SRC(7,3)= (l6 + 3*l7 + 2) >> 2;
  2134. SRC(0,7)=SRC(1,7)=SRC(2,6)=SRC(2,7)=SRC(3,6)=
  2135. SRC(3,7)=SRC(4,5)=SRC(4,6)=SRC(4,7)=SRC(5,5)=
  2136. SRC(5,6)=SRC(5,7)=SRC(6,4)=SRC(6,5)=SRC(6,6)=
  2137. SRC(6,7)=SRC(7,4)=SRC(7,5)=SRC(7,6)=SRC(7,7)= l7;
  2138. }
  2139. #undef PREDICT_8x8_LOAD_LEFT
  2140. #undef PREDICT_8x8_LOAD_TOP
  2141. #undef PREDICT_8x8_LOAD_TOPLEFT
  2142. #undef PREDICT_8x8_LOAD_TOPRIGHT
  2143. #undef PREDICT_8x8_DC
  2144. #undef PTR
  2145. #undef PT
  2146. #undef PL
  2147. #undef SRC
  2148. static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
  2149. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2150. int src_x_offset, int src_y_offset,
  2151. qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
  2152. MpegEncContext * const s = &h->s;
  2153. const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
  2154. int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
  2155. const int luma_xy= (mx&3) + ((my&3)<<2);
  2156. uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->mb_linesize;
  2157. uint8_t * src_cb, * src_cr;
  2158. int extra_width= h->emu_edge_width;
  2159. int extra_height= h->emu_edge_height;
  2160. int emu=0;
  2161. const int full_mx= mx>>2;
  2162. const int full_my= my>>2;
  2163. const int pic_width = 16*s->mb_width;
  2164. const int pic_height = 16*s->mb_height >> MB_MBAFF;
  2165. if(!pic->data[0]) //FIXME this is unacceptable, some senseable error concealment must be done for missing reference frames
  2166. return;
  2167. if(mx&7) extra_width -= 3;
  2168. if(my&7) extra_height -= 3;
  2169. if( full_mx < 0-extra_width
  2170. || full_my < 0-extra_height
  2171. || full_mx + 16/*FIXME*/ > pic_width + extra_width
  2172. || full_my + 16/*FIXME*/ > pic_height + extra_height){
  2173. ff_emulated_edge_mc(s->edge_emu_buffer, src_y - 2 - 2*h->mb_linesize, h->mb_linesize, 16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height);
  2174. src_y= s->edge_emu_buffer + 2 + 2*h->mb_linesize;
  2175. emu=1;
  2176. }
  2177. qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); //FIXME try variable height perhaps?
  2178. if(!square){
  2179. qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
  2180. }
  2181. if(s->flags&CODEC_FLAG_GRAY) return;
  2182. if(MB_MBAFF){
  2183. // chroma offset when predicting from a field of opposite parity
  2184. my += 2 * ((s->mb_y & 1) - (h->ref_cache[list][scan8[n]] & 1));
  2185. emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
  2186. }
  2187. src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  2188. src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  2189. if(emu){
  2190. ff_emulated_edge_mc(s->edge_emu_buffer, src_cb, h->mb_uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
  2191. src_cb= s->edge_emu_buffer;
  2192. }
  2193. chroma_op(dest_cb, src_cb, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  2194. if(emu){
  2195. ff_emulated_edge_mc(s->edge_emu_buffer, src_cr, h->mb_uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
  2196. src_cr= s->edge_emu_buffer;
  2197. }
  2198. chroma_op(dest_cr, src_cr, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  2199. }
  2200. static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
  2201. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2202. int x_offset, int y_offset,
  2203. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2204. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  2205. int list0, int list1){
  2206. MpegEncContext * const s = &h->s;
  2207. qpel_mc_func *qpix_op= qpix_put;
  2208. h264_chroma_mc_func chroma_op= chroma_put;
  2209. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  2210. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  2211. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  2212. x_offset += 8*s->mb_x;
  2213. y_offset += 8*(s->mb_y >> MB_MBAFF);
  2214. if(list0){
  2215. Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
  2216. mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
  2217. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2218. qpix_op, chroma_op);
  2219. qpix_op= qpix_avg;
  2220. chroma_op= chroma_avg;
  2221. }
  2222. if(list1){
  2223. Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
  2224. mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
  2225. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2226. qpix_op, chroma_op);
  2227. }
  2228. }
  2229. static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
  2230. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2231. int x_offset, int y_offset,
  2232. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2233. h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
  2234. h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
  2235. int list0, int list1){
  2236. MpegEncContext * const s = &h->s;
  2237. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  2238. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  2239. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  2240. x_offset += 8*s->mb_x;
  2241. y_offset += 8*(s->mb_y >> MB_MBAFF);
  2242. if(list0 && list1){
  2243. /* don't optimize for luma-only case, since B-frames usually
  2244. * use implicit weights => chroma too. */
  2245. uint8_t *tmp_cb = s->obmc_scratchpad;
  2246. uint8_t *tmp_cr = s->obmc_scratchpad + 8;
  2247. uint8_t *tmp_y = s->obmc_scratchpad + 8*h->mb_uvlinesize;
  2248. int refn0 = h->ref_cache[0][ scan8[n] ];
  2249. int refn1 = h->ref_cache[1][ scan8[n] ];
  2250. mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
  2251. dest_y, dest_cb, dest_cr,
  2252. x_offset, y_offset, qpix_put, chroma_put);
  2253. mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
  2254. tmp_y, tmp_cb, tmp_cr,
  2255. x_offset, y_offset, qpix_put, chroma_put);
  2256. if(h->use_weight == 2){
  2257. int weight0 = h->implicit_weight[refn0][refn1];
  2258. int weight1 = 64 - weight0;
  2259. luma_weight_avg( dest_y, tmp_y, h-> mb_linesize, 5, weight0, weight1, 0);
  2260. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, 5, weight0, weight1, 0);
  2261. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, 5, weight0, weight1, 0);
  2262. }else{
  2263. luma_weight_avg(dest_y, tmp_y, h->mb_linesize, h->luma_log2_weight_denom,
  2264. h->luma_weight[0][refn0], h->luma_weight[1][refn1],
  2265. h->luma_offset[0][refn0] + h->luma_offset[1][refn1]);
  2266. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2267. h->chroma_weight[0][refn0][0], h->chroma_weight[1][refn1][0],
  2268. h->chroma_offset[0][refn0][0] + h->chroma_offset[1][refn1][0]);
  2269. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2270. h->chroma_weight[0][refn0][1], h->chroma_weight[1][refn1][1],
  2271. h->chroma_offset[0][refn0][1] + h->chroma_offset[1][refn1][1]);
  2272. }
  2273. }else{
  2274. int list = list1 ? 1 : 0;
  2275. int refn = h->ref_cache[list][ scan8[n] ];
  2276. Picture *ref= &h->ref_list[list][refn];
  2277. mc_dir_part(h, ref, n, square, chroma_height, delta, list,
  2278. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2279. qpix_put, chroma_put);
  2280. luma_weight_op(dest_y, h->mb_linesize, h->luma_log2_weight_denom,
  2281. h->luma_weight[list][refn], h->luma_offset[list][refn]);
  2282. if(h->use_weight_chroma){
  2283. chroma_weight_op(dest_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2284. h->chroma_weight[list][refn][0], h->chroma_offset[list][refn][0]);
  2285. chroma_weight_op(dest_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2286. h->chroma_weight[list][refn][1], h->chroma_offset[list][refn][1]);
  2287. }
  2288. }
  2289. }
  2290. static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
  2291. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2292. int x_offset, int y_offset,
  2293. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2294. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  2295. h264_weight_func *weight_op, h264_biweight_func *weight_avg,
  2296. int list0, int list1){
  2297. if((h->use_weight==2 && list0 && list1
  2298. && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ] != 32))
  2299. || h->use_weight==1)
  2300. mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  2301. x_offset, y_offset, qpix_put, chroma_put,
  2302. weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
  2303. else
  2304. mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  2305. x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
  2306. }
  2307. static inline void prefetch_motion(H264Context *h, int list){
  2308. /* fetch pixels for estimated mv 4 macroblocks ahead
  2309. * optimized for 64byte cache lines */
  2310. MpegEncContext * const s = &h->s;
  2311. const int refn = h->ref_cache[list][scan8[0]];
  2312. if(refn >= 0){
  2313. const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
  2314. const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
  2315. uint8_t **src= h->ref_list[list][refn].data;
  2316. int off= mx + (my + (s->mb_x&3)*4)*h->mb_linesize + 64;
  2317. s->dsp.prefetch(src[0]+off, s->linesize, 4);
  2318. off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
  2319. s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
  2320. }
  2321. }
  2322. static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2323. qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
  2324. qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
  2325. h264_weight_func *weight_op, h264_biweight_func *weight_avg){
  2326. MpegEncContext * const s = &h->s;
  2327. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2328. const int mb_type= s->current_picture.mb_type[mb_xy];
  2329. assert(IS_INTER(mb_type));
  2330. prefetch_motion(h, 0);
  2331. if(IS_16X16(mb_type)){
  2332. mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
  2333. qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
  2334. &weight_op[0], &weight_avg[0],
  2335. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2336. }else if(IS_16X8(mb_type)){
  2337. mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
  2338. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  2339. &weight_op[1], &weight_avg[1],
  2340. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2341. mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
  2342. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  2343. &weight_op[1], &weight_avg[1],
  2344. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  2345. }else if(IS_8X16(mb_type)){
  2346. mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
  2347. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2348. &weight_op[2], &weight_avg[2],
  2349. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2350. mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
  2351. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2352. &weight_op[2], &weight_avg[2],
  2353. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  2354. }else{
  2355. int i;
  2356. assert(IS_8X8(mb_type));
  2357. for(i=0; i<4; i++){
  2358. const int sub_mb_type= h->sub_mb_type[i];
  2359. const int n= 4*i;
  2360. int x_offset= (i&1)<<2;
  2361. int y_offset= (i&2)<<1;
  2362. if(IS_SUB_8X8(sub_mb_type)){
  2363. mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2364. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2365. &weight_op[3], &weight_avg[3],
  2366. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2367. }else if(IS_SUB_8X4(sub_mb_type)){
  2368. mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2369. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  2370. &weight_op[4], &weight_avg[4],
  2371. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2372. mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
  2373. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  2374. &weight_op[4], &weight_avg[4],
  2375. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2376. }else if(IS_SUB_4X8(sub_mb_type)){
  2377. mc_part(h, n , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2378. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2379. &weight_op[5], &weight_avg[5],
  2380. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2381. mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
  2382. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2383. &weight_op[5], &weight_avg[5],
  2384. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2385. }else{
  2386. int j;
  2387. assert(IS_SUB_4X4(sub_mb_type));
  2388. for(j=0; j<4; j++){
  2389. int sub_x_offset= x_offset + 2*(j&1);
  2390. int sub_y_offset= y_offset + (j&2);
  2391. mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
  2392. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2393. &weight_op[6], &weight_avg[6],
  2394. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2395. }
  2396. }
  2397. }
  2398. }
  2399. prefetch_motion(h, 1);
  2400. }
  2401. static void decode_init_vlc(void){
  2402. static int done = 0;
  2403. if (!done) {
  2404. int i;
  2405. done = 1;
  2406. init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5,
  2407. &chroma_dc_coeff_token_len [0], 1, 1,
  2408. &chroma_dc_coeff_token_bits[0], 1, 1, 1);
  2409. for(i=0; i<4; i++){
  2410. init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17,
  2411. &coeff_token_len [i][0], 1, 1,
  2412. &coeff_token_bits[i][0], 1, 1, 1);
  2413. }
  2414. for(i=0; i<3; i++){
  2415. init_vlc(&chroma_dc_total_zeros_vlc[i], CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
  2416. &chroma_dc_total_zeros_len [i][0], 1, 1,
  2417. &chroma_dc_total_zeros_bits[i][0], 1, 1, 1);
  2418. }
  2419. for(i=0; i<15; i++){
  2420. init_vlc(&total_zeros_vlc[i], TOTAL_ZEROS_VLC_BITS, 16,
  2421. &total_zeros_len [i][0], 1, 1,
  2422. &total_zeros_bits[i][0], 1, 1, 1);
  2423. }
  2424. for(i=0; i<6; i++){
  2425. init_vlc(&run_vlc[i], RUN_VLC_BITS, 7,
  2426. &run_len [i][0], 1, 1,
  2427. &run_bits[i][0], 1, 1, 1);
  2428. }
  2429. init_vlc(&run7_vlc, RUN7_VLC_BITS, 16,
  2430. &run_len [6][0], 1, 1,
  2431. &run_bits[6][0], 1, 1, 1);
  2432. }
  2433. }
  2434. /**
  2435. * Sets the intra prediction function pointers.
  2436. */
  2437. static void init_pred_ptrs(H264Context *h){
  2438. // MpegEncContext * const s = &h->s;
  2439. h->pred4x4[VERT_PRED ]= pred4x4_vertical_c;
  2440. h->pred4x4[HOR_PRED ]= pred4x4_horizontal_c;
  2441. h->pred4x4[DC_PRED ]= pred4x4_dc_c;
  2442. h->pred4x4[DIAG_DOWN_LEFT_PRED ]= pred4x4_down_left_c;
  2443. h->pred4x4[DIAG_DOWN_RIGHT_PRED]= pred4x4_down_right_c;
  2444. h->pred4x4[VERT_RIGHT_PRED ]= pred4x4_vertical_right_c;
  2445. h->pred4x4[HOR_DOWN_PRED ]= pred4x4_horizontal_down_c;
  2446. h->pred4x4[VERT_LEFT_PRED ]= pred4x4_vertical_left_c;
  2447. h->pred4x4[HOR_UP_PRED ]= pred4x4_horizontal_up_c;
  2448. h->pred4x4[LEFT_DC_PRED ]= pred4x4_left_dc_c;
  2449. h->pred4x4[TOP_DC_PRED ]= pred4x4_top_dc_c;
  2450. h->pred4x4[DC_128_PRED ]= pred4x4_128_dc_c;
  2451. h->pred8x8l[VERT_PRED ]= pred8x8l_vertical_c;
  2452. h->pred8x8l[HOR_PRED ]= pred8x8l_horizontal_c;
  2453. h->pred8x8l[DC_PRED ]= pred8x8l_dc_c;
  2454. h->pred8x8l[DIAG_DOWN_LEFT_PRED ]= pred8x8l_down_left_c;
  2455. h->pred8x8l[DIAG_DOWN_RIGHT_PRED]= pred8x8l_down_right_c;
  2456. h->pred8x8l[VERT_RIGHT_PRED ]= pred8x8l_vertical_right_c;
  2457. h->pred8x8l[HOR_DOWN_PRED ]= pred8x8l_horizontal_down_c;
  2458. h->pred8x8l[VERT_LEFT_PRED ]= pred8x8l_vertical_left_c;
  2459. h->pred8x8l[HOR_UP_PRED ]= pred8x8l_horizontal_up_c;
  2460. h->pred8x8l[LEFT_DC_PRED ]= pred8x8l_left_dc_c;
  2461. h->pred8x8l[TOP_DC_PRED ]= pred8x8l_top_dc_c;
  2462. h->pred8x8l[DC_128_PRED ]= pred8x8l_128_dc_c;
  2463. h->pred8x8[DC_PRED8x8 ]= ff_pred8x8_dc_c;
  2464. h->pred8x8[VERT_PRED8x8 ]= ff_pred8x8_vertical_c;
  2465. h->pred8x8[HOR_PRED8x8 ]= ff_pred8x8_horizontal_c;
  2466. h->pred8x8[PLANE_PRED8x8 ]= ff_pred8x8_plane_c;
  2467. h->pred8x8[LEFT_DC_PRED8x8]= pred8x8_left_dc_c;
  2468. h->pred8x8[TOP_DC_PRED8x8 ]= pred8x8_top_dc_c;
  2469. h->pred8x8[DC_128_PRED8x8 ]= ff_pred8x8_128_dc_c;
  2470. h->pred16x16[DC_PRED8x8 ]= ff_pred16x16_dc_c;
  2471. h->pred16x16[VERT_PRED8x8 ]= ff_pred16x16_vertical_c;
  2472. h->pred16x16[HOR_PRED8x8 ]= ff_pred16x16_horizontal_c;
  2473. h->pred16x16[PLANE_PRED8x8 ]= ff_pred16x16_plane_c;
  2474. h->pred16x16[LEFT_DC_PRED8x8]= pred16x16_left_dc_c;
  2475. h->pred16x16[TOP_DC_PRED8x8 ]= pred16x16_top_dc_c;
  2476. h->pred16x16[DC_128_PRED8x8 ]= ff_pred16x16_128_dc_c;
  2477. }
  2478. static void free_tables(H264Context *h){
  2479. av_freep(&h->intra4x4_pred_mode);
  2480. av_freep(&h->chroma_pred_mode_table);
  2481. av_freep(&h->cbp_table);
  2482. av_freep(&h->mvd_table[0]);
  2483. av_freep(&h->mvd_table[1]);
  2484. av_freep(&h->direct_table);
  2485. av_freep(&h->non_zero_count);
  2486. av_freep(&h->slice_table_base);
  2487. av_freep(&h->top_borders[1]);
  2488. av_freep(&h->top_borders[0]);
  2489. h->slice_table= NULL;
  2490. av_freep(&h->mb2b_xy);
  2491. av_freep(&h->mb2b8_xy);
  2492. av_freep(&h->s.obmc_scratchpad);
  2493. }
  2494. static void init_dequant8_coeff_table(H264Context *h){
  2495. int i,q,x;
  2496. const int transpose = (h->s.dsp.h264_idct8_add != ff_h264_idct8_add_c); //FIXME ugly
  2497. h->dequant8_coeff[0] = h->dequant8_buffer[0];
  2498. h->dequant8_coeff[1] = h->dequant8_buffer[1];
  2499. for(i=0; i<2; i++ ){
  2500. if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
  2501. h->dequant8_coeff[1] = h->dequant8_buffer[0];
  2502. break;
  2503. }
  2504. for(q=0; q<52; q++){
  2505. int shift = ff_div6[q];
  2506. int idx = ff_rem6[q];
  2507. for(x=0; x<64; x++)
  2508. h->dequant8_coeff[i][q][transpose ? (x>>3)|((x&7)<<3) : x] =
  2509. ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
  2510. h->pps.scaling_matrix8[i][x]) << shift;
  2511. }
  2512. }
  2513. }
  2514. static void init_dequant4_coeff_table(H264Context *h){
  2515. int i,j,q,x;
  2516. const int transpose = (h->s.dsp.h264_idct_add != ff_h264_idct_add_c); //FIXME ugly
  2517. for(i=0; i<6; i++ ){
  2518. h->dequant4_coeff[i] = h->dequant4_buffer[i];
  2519. for(j=0; j<i; j++){
  2520. if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
  2521. h->dequant4_coeff[i] = h->dequant4_buffer[j];
  2522. break;
  2523. }
  2524. }
  2525. if(j<i)
  2526. continue;
  2527. for(q=0; q<52; q++){
  2528. int shift = ff_div6[q] + 2;
  2529. int idx = ff_rem6[q];
  2530. for(x=0; x<16; x++)
  2531. h->dequant4_coeff[i][q][transpose ? (x>>2)|((x<<2)&0xF) : x] =
  2532. ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
  2533. h->pps.scaling_matrix4[i][x]) << shift;
  2534. }
  2535. }
  2536. }
  2537. static void init_dequant_tables(H264Context *h){
  2538. int i,x;
  2539. init_dequant4_coeff_table(h);
  2540. if(h->pps.transform_8x8_mode)
  2541. init_dequant8_coeff_table(h);
  2542. if(h->sps.transform_bypass){
  2543. for(i=0; i<6; i++)
  2544. for(x=0; x<16; x++)
  2545. h->dequant4_coeff[i][0][x] = 1<<6;
  2546. if(h->pps.transform_8x8_mode)
  2547. for(i=0; i<2; i++)
  2548. for(x=0; x<64; x++)
  2549. h->dequant8_coeff[i][0][x] = 1<<6;
  2550. }
  2551. }
  2552. /**
  2553. * allocates tables.
  2554. * needs width/height
  2555. */
  2556. static int alloc_tables(H264Context *h){
  2557. MpegEncContext * const s = &h->s;
  2558. const int big_mb_num= s->mb_stride * (s->mb_height+1);
  2559. int x,y;
  2560. CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t))
  2561. CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t))
  2562. CHECKED_ALLOCZ(h->slice_table_base , (big_mb_num+s->mb_stride) * sizeof(uint8_t))
  2563. CHECKED_ALLOCZ(h->top_borders[0] , s->mb_width * (16+8+8) * sizeof(uint8_t))
  2564. CHECKED_ALLOCZ(h->top_borders[1] , s->mb_width * (16+8+8) * sizeof(uint8_t))
  2565. CHECKED_ALLOCZ(h->cbp_table, big_mb_num * sizeof(uint16_t))
  2566. if( h->pps.cabac ) {
  2567. CHECKED_ALLOCZ(h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t))
  2568. CHECKED_ALLOCZ(h->mvd_table[0], 32*big_mb_num * sizeof(uint16_t));
  2569. CHECKED_ALLOCZ(h->mvd_table[1], 32*big_mb_num * sizeof(uint16_t));
  2570. CHECKED_ALLOCZ(h->direct_table, 32*big_mb_num * sizeof(uint8_t));
  2571. }
  2572. memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride) * sizeof(uint8_t));
  2573. h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
  2574. CHECKED_ALLOCZ(h->mb2b_xy , big_mb_num * sizeof(uint32_t));
  2575. CHECKED_ALLOCZ(h->mb2b8_xy , big_mb_num * sizeof(uint32_t));
  2576. for(y=0; y<s->mb_height; y++){
  2577. for(x=0; x<s->mb_width; x++){
  2578. const int mb_xy= x + y*s->mb_stride;
  2579. const int b_xy = 4*x + 4*y*h->b_stride;
  2580. const int b8_xy= 2*x + 2*y*h->b8_stride;
  2581. h->mb2b_xy [mb_xy]= b_xy;
  2582. h->mb2b8_xy[mb_xy]= b8_xy;
  2583. }
  2584. }
  2585. s->obmc_scratchpad = NULL;
  2586. if(!h->dequant4_coeff[0])
  2587. init_dequant_tables(h);
  2588. return 0;
  2589. fail:
  2590. free_tables(h);
  2591. return -1;
  2592. }
  2593. static void common_init(H264Context *h){
  2594. MpegEncContext * const s = &h->s;
  2595. s->width = s->avctx->width;
  2596. s->height = s->avctx->height;
  2597. s->codec_id= s->avctx->codec->id;
  2598. init_pred_ptrs(h);
  2599. h->dequant_coeff_pps= -1;
  2600. s->unrestricted_mv=1;
  2601. s->decode=1; //FIXME
  2602. memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  2603. memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  2604. }
  2605. static int decode_init(AVCodecContext *avctx){
  2606. H264Context *h= avctx->priv_data;
  2607. MpegEncContext * const s = &h->s;
  2608. MPV_decode_defaults(s);
  2609. s->avctx = avctx;
  2610. common_init(h);
  2611. s->out_format = FMT_H264;
  2612. s->workaround_bugs= avctx->workaround_bugs;
  2613. // set defaults
  2614. // s->decode_mb= ff_h263_decode_mb;
  2615. s->low_delay= 1;
  2616. avctx->pix_fmt= PIX_FMT_YUV420P;
  2617. decode_init_vlc();
  2618. if(avctx->extradata_size > 0 && avctx->extradata &&
  2619. *(char *)avctx->extradata == 1){
  2620. h->is_avc = 1;
  2621. h->got_avcC = 0;
  2622. } else {
  2623. h->is_avc = 0;
  2624. }
  2625. return 0;
  2626. }
  2627. static int frame_start(H264Context *h){
  2628. MpegEncContext * const s = &h->s;
  2629. int i;
  2630. if(MPV_frame_start(s, s->avctx) < 0)
  2631. return -1;
  2632. ff_er_frame_start(s);
  2633. assert(s->linesize && s->uvlinesize);
  2634. for(i=0; i<16; i++){
  2635. h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  2636. h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
  2637. }
  2638. for(i=0; i<4; i++){
  2639. h->block_offset[16+i]=
  2640. h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  2641. h->block_offset[24+16+i]=
  2642. h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  2643. }
  2644. /* can't be in alloc_tables because linesize isn't known there.
  2645. * FIXME: redo bipred weight to not require extra buffer? */
  2646. if(!s->obmc_scratchpad)
  2647. s->obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
  2648. /* some macroblocks will be accessed before they're available */
  2649. if(FRAME_MBAFF)
  2650. memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(uint8_t));
  2651. // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
  2652. return 0;
  2653. }
  2654. static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  2655. MpegEncContext * const s = &h->s;
  2656. int i;
  2657. src_y -= linesize;
  2658. src_cb -= uvlinesize;
  2659. src_cr -= uvlinesize;
  2660. // There are two lines saved, the line above the the top macroblock of a pair,
  2661. // and the line above the bottom macroblock
  2662. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  2663. for(i=1; i<17; i++){
  2664. h->left_border[i]= src_y[15+i* linesize];
  2665. }
  2666. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 16*linesize);
  2667. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize);
  2668. if(!(s->flags&CODEC_FLAG_GRAY)){
  2669. h->left_border[17 ]= h->top_borders[0][s->mb_x][16+7];
  2670. h->left_border[17+9]= h->top_borders[0][s->mb_x][24+7];
  2671. for(i=1; i<9; i++){
  2672. h->left_border[i+17 ]= src_cb[7+i*uvlinesize];
  2673. h->left_border[i+17+9]= src_cr[7+i*uvlinesize];
  2674. }
  2675. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize);
  2676. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize);
  2677. }
  2678. }
  2679. static inline void xchg_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int xchg){
  2680. MpegEncContext * const s = &h->s;
  2681. int temp8, i;
  2682. uint64_t temp64;
  2683. int deblock_left = (s->mb_x > 0);
  2684. int deblock_top = (s->mb_y > 0);
  2685. src_y -= linesize + 1;
  2686. src_cb -= uvlinesize + 1;
  2687. src_cr -= uvlinesize + 1;
  2688. #define XCHG(a,b,t,xchg)\
  2689. t= a;\
  2690. if(xchg)\
  2691. a= b;\
  2692. b= t;
  2693. if(deblock_left){
  2694. for(i = !deblock_top; i<17; i++){
  2695. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  2696. }
  2697. }
  2698. if(deblock_top){
  2699. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  2700. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  2701. if(s->mb_x+1 < s->mb_width){
  2702. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  2703. }
  2704. }
  2705. if(!(s->flags&CODEC_FLAG_GRAY)){
  2706. if(deblock_left){
  2707. for(i = !deblock_top; i<9; i++){
  2708. XCHG(h->left_border[i+17 ], src_cb[i*uvlinesize], temp8, xchg);
  2709. XCHG(h->left_border[i+17+9], src_cr[i*uvlinesize], temp8, xchg);
  2710. }
  2711. }
  2712. if(deblock_top){
  2713. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  2714. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  2715. }
  2716. }
  2717. }
  2718. static inline void backup_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  2719. MpegEncContext * const s = &h->s;
  2720. int i;
  2721. src_y -= 2 * linesize;
  2722. src_cb -= 2 * uvlinesize;
  2723. src_cr -= 2 * uvlinesize;
  2724. // There are two lines saved, the line above the the top macroblock of a pair,
  2725. // and the line above the bottom macroblock
  2726. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  2727. h->left_border[1]= h->top_borders[1][s->mb_x][15];
  2728. for(i=2; i<34; i++){
  2729. h->left_border[i]= src_y[15+i* linesize];
  2730. }
  2731. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 32*linesize);
  2732. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+32*linesize);
  2733. *(uint64_t*)(h->top_borders[1][s->mb_x]+0)= *(uint64_t*)(src_y + 33*linesize);
  2734. *(uint64_t*)(h->top_borders[1][s->mb_x]+8)= *(uint64_t*)(src_y +8+33*linesize);
  2735. if(!(s->flags&CODEC_FLAG_GRAY)){
  2736. h->left_border[34 ]= h->top_borders[0][s->mb_x][16+7];
  2737. h->left_border[34+ 1]= h->top_borders[1][s->mb_x][16+7];
  2738. h->left_border[34+18 ]= h->top_borders[0][s->mb_x][24+7];
  2739. h->left_border[34+18+1]= h->top_borders[1][s->mb_x][24+7];
  2740. for(i=2; i<18; i++){
  2741. h->left_border[i+34 ]= src_cb[7+i*uvlinesize];
  2742. h->left_border[i+34+18]= src_cr[7+i*uvlinesize];
  2743. }
  2744. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+16*uvlinesize);
  2745. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+16*uvlinesize);
  2746. *(uint64_t*)(h->top_borders[1][s->mb_x]+16)= *(uint64_t*)(src_cb+17*uvlinesize);
  2747. *(uint64_t*)(h->top_borders[1][s->mb_x]+24)= *(uint64_t*)(src_cr+17*uvlinesize);
  2748. }
  2749. }
  2750. static inline void xchg_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int xchg){
  2751. MpegEncContext * const s = &h->s;
  2752. int temp8, i;
  2753. uint64_t temp64;
  2754. int deblock_left = (s->mb_x > 0);
  2755. int deblock_top = (s->mb_y > 1);
  2756. tprintf(s->avctx, "xchg_pair_border: src_y:%p src_cb:%p src_cr:%p ls:%d uvls:%d\n", src_y, src_cb, src_cr, linesize, uvlinesize);
  2757. src_y -= 2 * linesize + 1;
  2758. src_cb -= 2 * uvlinesize + 1;
  2759. src_cr -= 2 * uvlinesize + 1;
  2760. #define XCHG(a,b,t,xchg)\
  2761. t= a;\
  2762. if(xchg)\
  2763. a= b;\
  2764. b= t;
  2765. if(deblock_left){
  2766. for(i = (!deblock_top)<<1; i<34; i++){
  2767. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  2768. }
  2769. }
  2770. if(deblock_top){
  2771. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  2772. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  2773. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+0), *(uint64_t*)(src_y +1 +linesize), temp64, xchg);
  2774. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+8), *(uint64_t*)(src_y +9 +linesize), temp64, 1);
  2775. if(s->mb_x+1 < s->mb_width){
  2776. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  2777. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x+1]), *(uint64_t*)(src_y +17 +linesize), temp64, 1);
  2778. }
  2779. }
  2780. if(!(s->flags&CODEC_FLAG_GRAY)){
  2781. if(deblock_left){
  2782. for(i = (!deblock_top) << 1; i<18; i++){
  2783. XCHG(h->left_border[i+34 ], src_cb[i*uvlinesize], temp8, xchg);
  2784. XCHG(h->left_border[i+34+18], src_cr[i*uvlinesize], temp8, xchg);
  2785. }
  2786. }
  2787. if(deblock_top){
  2788. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  2789. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  2790. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+16), *(uint64_t*)(src_cb+1 +uvlinesize), temp64, 1);
  2791. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+24), *(uint64_t*)(src_cr+1 +uvlinesize), temp64, 1);
  2792. }
  2793. }
  2794. }
  2795. static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple){
  2796. MpegEncContext * const s = &h->s;
  2797. const int mb_x= s->mb_x;
  2798. const int mb_y= s->mb_y;
  2799. const int mb_xy= mb_x + mb_y*s->mb_stride;
  2800. const int mb_type= s->current_picture.mb_type[mb_xy];
  2801. uint8_t *dest_y, *dest_cb, *dest_cr;
  2802. int linesize, uvlinesize /*dct_offset*/;
  2803. int i;
  2804. int *block_offset = &h->block_offset[0];
  2805. const unsigned int bottom = mb_y & 1;
  2806. const int transform_bypass = (s->qscale == 0 && h->sps.transform_bypass), is_h264 = (simple || s->codec_id == CODEC_ID_H264);
  2807. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  2808. void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  2809. dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  2810. dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  2811. dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  2812. s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + 64, s->linesize, 4);
  2813. s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + 64, dest_cr - dest_cb, 2);
  2814. if (!simple && MB_FIELD) {
  2815. linesize = h->mb_linesize = s->linesize * 2;
  2816. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  2817. block_offset = &h->block_offset[24];
  2818. if(mb_y&1){ //FIXME move out of this func?
  2819. dest_y -= s->linesize*15;
  2820. dest_cb-= s->uvlinesize*7;
  2821. dest_cr-= s->uvlinesize*7;
  2822. }
  2823. if(FRAME_MBAFF) {
  2824. int list;
  2825. for(list=0; list<h->list_count; list++){
  2826. if(!USES_LIST(mb_type, list))
  2827. continue;
  2828. if(IS_16X16(mb_type)){
  2829. int8_t *ref = &h->ref_cache[list][scan8[0]];
  2830. fill_rectangle(ref, 4, 4, 8, 16+*ref^(s->mb_y&1), 1);
  2831. }else{
  2832. for(i=0; i<16; i+=4){
  2833. //FIXME can refs be smaller than 8x8 when !direct_8x8_inference ?
  2834. int ref = h->ref_cache[list][scan8[i]];
  2835. if(ref >= 0)
  2836. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, 16+ref^(s->mb_y&1), 1);
  2837. }
  2838. }
  2839. }
  2840. }
  2841. } else {
  2842. linesize = h->mb_linesize = s->linesize;
  2843. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  2844. // dct_offset = s->linesize * 16;
  2845. }
  2846. if(transform_bypass){
  2847. idct_dc_add =
  2848. idct_add = IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
  2849. }else if(IS_8x8DCT(mb_type)){
  2850. idct_dc_add = s->dsp.h264_idct8_dc_add;
  2851. idct_add = s->dsp.h264_idct8_add;
  2852. }else{
  2853. idct_dc_add = s->dsp.h264_idct_dc_add;
  2854. idct_add = s->dsp.h264_idct_add;
  2855. }
  2856. if(!simple && FRAME_MBAFF && h->deblocking_filter && IS_INTRA(mb_type)
  2857. && (!bottom || !IS_INTRA(s->current_picture.mb_type[mb_xy-s->mb_stride]))){
  2858. int mbt_y = mb_y&~1;
  2859. uint8_t *top_y = s->current_picture.data[0] + (mbt_y * 16* s->linesize ) + mb_x * 16;
  2860. uint8_t *top_cb = s->current_picture.data[1] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
  2861. uint8_t *top_cr = s->current_picture.data[2] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
  2862. xchg_pair_border(h, top_y, top_cb, top_cr, s->linesize, s->uvlinesize, 1);
  2863. }
  2864. if (!simple && IS_INTRA_PCM(mb_type)) {
  2865. unsigned int x, y;
  2866. // The pixels are stored in h->mb array in the same order as levels,
  2867. // copy them in output in the correct order.
  2868. for(i=0; i<16; i++) {
  2869. for (y=0; y<4; y++) {
  2870. for (x=0; x<4; x++) {
  2871. *(dest_y + block_offset[i] + y*linesize + x) = h->mb[i*16+y*4+x];
  2872. }
  2873. }
  2874. }
  2875. for(i=16; i<16+4; i++) {
  2876. for (y=0; y<4; y++) {
  2877. for (x=0; x<4; x++) {
  2878. *(dest_cb + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  2879. }
  2880. }
  2881. }
  2882. for(i=20; i<20+4; i++) {
  2883. for (y=0; y<4; y++) {
  2884. for (x=0; x<4; x++) {
  2885. *(dest_cr + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  2886. }
  2887. }
  2888. }
  2889. } else {
  2890. if(IS_INTRA(mb_type)){
  2891. if(h->deblocking_filter && (simple || !FRAME_MBAFF))
  2892. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1);
  2893. if(simple || !(s->flags&CODEC_FLAG_GRAY)){
  2894. h->pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  2895. h->pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  2896. }
  2897. if(IS_INTRA4x4(mb_type)){
  2898. if(simple || !s->encoding){
  2899. if(IS_8x8DCT(mb_type)){
  2900. for(i=0; i<16; i+=4){
  2901. uint8_t * const ptr= dest_y + block_offset[i];
  2902. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  2903. const int nnz = h->non_zero_count_cache[ scan8[i] ];
  2904. h->pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
  2905. (h->topright_samples_available<<i)&0x4000, linesize);
  2906. if(nnz){
  2907. if(nnz == 1 && h->mb[i*16])
  2908. idct_dc_add(ptr, h->mb + i*16, linesize);
  2909. else
  2910. idct_add(ptr, h->mb + i*16, linesize);
  2911. }
  2912. }
  2913. }else
  2914. for(i=0; i<16; i++){
  2915. uint8_t * const ptr= dest_y + block_offset[i];
  2916. uint8_t *topright;
  2917. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  2918. int nnz, tr;
  2919. if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
  2920. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  2921. assert(mb_y || linesize <= block_offset[i]);
  2922. if(!topright_avail){
  2923. tr= ptr[3 - linesize]*0x01010101;
  2924. topright= (uint8_t*) &tr;
  2925. }else
  2926. topright= ptr + 4 - linesize;
  2927. }else
  2928. topright= NULL;
  2929. h->pred4x4[ dir ](ptr, topright, linesize);
  2930. nnz = h->non_zero_count_cache[ scan8[i] ];
  2931. if(nnz){
  2932. if(is_h264){
  2933. if(nnz == 1 && h->mb[i*16])
  2934. idct_dc_add(ptr, h->mb + i*16, linesize);
  2935. else
  2936. idct_add(ptr, h->mb + i*16, linesize);
  2937. }else
  2938. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
  2939. }
  2940. }
  2941. }
  2942. }else{
  2943. h->pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  2944. if(is_h264){
  2945. if(!transform_bypass)
  2946. h264_luma_dc_dequant_idct_c(h->mb, s->qscale, h->dequant4_coeff[IS_INTRA(mb_type) ? 0:3][s->qscale][0]);
  2947. }else
  2948. svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
  2949. }
  2950. if(h->deblocking_filter && (simple || !FRAME_MBAFF))
  2951. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
  2952. }else if(is_h264){
  2953. hl_motion(h, dest_y, dest_cb, dest_cr,
  2954. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  2955. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  2956. s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
  2957. }
  2958. if(!IS_INTRA4x4(mb_type)){
  2959. if(is_h264){
  2960. if(IS_INTRA16x16(mb_type)){
  2961. for(i=0; i<16; i++){
  2962. if(h->non_zero_count_cache[ scan8[i] ])
  2963. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  2964. else if(h->mb[i*16])
  2965. idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  2966. }
  2967. }else{
  2968. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  2969. for(i=0; i<16; i+=di){
  2970. int nnz = h->non_zero_count_cache[ scan8[i] ];
  2971. if(nnz){
  2972. if(nnz==1 && h->mb[i*16])
  2973. idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  2974. else
  2975. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  2976. }
  2977. }
  2978. }
  2979. }else{
  2980. for(i=0; i<16; i++){
  2981. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  2982. uint8_t * const ptr= dest_y + block_offset[i];
  2983. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  2984. }
  2985. }
  2986. }
  2987. }
  2988. if(simple || !(s->flags&CODEC_FLAG_GRAY)){
  2989. uint8_t *dest[2] = {dest_cb, dest_cr};
  2990. if(transform_bypass){
  2991. idct_add = idct_dc_add = s->dsp.add_pixels4;
  2992. }else{
  2993. idct_add = s->dsp.h264_idct_add;
  2994. idct_dc_add = s->dsp.h264_idct_dc_add;
  2995. chroma_dc_dequant_idct_c(h->mb + 16*16, h->chroma_qp, h->dequant4_coeff[IS_INTRA(mb_type) ? 1:4][h->chroma_qp][0]);
  2996. chroma_dc_dequant_idct_c(h->mb + 16*16+4*16, h->chroma_qp, h->dequant4_coeff[IS_INTRA(mb_type) ? 2:5][h->chroma_qp][0]);
  2997. }
  2998. if(is_h264){
  2999. for(i=16; i<16+8; i++){
  3000. if(h->non_zero_count_cache[ scan8[i] ])
  3001. idct_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  3002. else if(h->mb[i*16])
  3003. idct_dc_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  3004. }
  3005. }else{
  3006. for(i=16; i<16+8; i++){
  3007. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  3008. uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
  3009. svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
  3010. }
  3011. }
  3012. }
  3013. }
  3014. }
  3015. if(h->deblocking_filter) {
  3016. if (!simple && FRAME_MBAFF) {
  3017. //FIXME try deblocking one mb at a time?
  3018. // the reduction in load/storing mvs and such might outweigh the extra backup/xchg_border
  3019. const int mb_y = s->mb_y - 1;
  3020. uint8_t *pair_dest_y, *pair_dest_cb, *pair_dest_cr;
  3021. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3022. const int mb_type_top = s->current_picture.mb_type[mb_xy];
  3023. const int mb_type_bottom= s->current_picture.mb_type[mb_xy+s->mb_stride];
  3024. if (!bottom) return;
  3025. pair_dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  3026. pair_dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3027. pair_dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3028. if(IS_INTRA(mb_type_top | mb_type_bottom))
  3029. xchg_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize, 0);
  3030. backup_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize);
  3031. // deblock a pair
  3032. // top
  3033. s->mb_y--;
  3034. tprintf(h->s.avctx, "call mbaff filter_mb mb_x:%d mb_y:%d pair_dest_y = %p, dest_y = %p\n", mb_x, mb_y, pair_dest_y, dest_y);
  3035. fill_caches(h, mb_type_top, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3036. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy]);
  3037. filter_mb(h, mb_x, mb_y, pair_dest_y, pair_dest_cb, pair_dest_cr, linesize, uvlinesize);
  3038. // bottom
  3039. s->mb_y++;
  3040. tprintf(h->s.avctx, "call mbaff filter_mb\n");
  3041. fill_caches(h, mb_type_bottom, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3042. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy+s->mb_stride]);
  3043. filter_mb(h, mb_x, mb_y+1, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3044. } else {
  3045. tprintf(h->s.avctx, "call filter_mb\n");
  3046. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3047. fill_caches(h, mb_type, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3048. filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3049. }
  3050. }
  3051. }
  3052. /**
  3053. * Process a macroblock; this case avoids checks for expensive uncommon cases.
  3054. */
  3055. static void hl_decode_mb_simple(H264Context *h){
  3056. hl_decode_mb_internal(h, 1);
  3057. }
  3058. /**
  3059. * Process a macroblock; this handles edge cases, such as interlacing.
  3060. */
  3061. static void av_noinline hl_decode_mb_complex(H264Context *h){
  3062. hl_decode_mb_internal(h, 0);
  3063. }
  3064. static void hl_decode_mb(H264Context *h){
  3065. MpegEncContext * const s = &h->s;
  3066. const int mb_x= s->mb_x;
  3067. const int mb_y= s->mb_y;
  3068. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3069. const int mb_type= s->current_picture.mb_type[mb_xy];
  3070. int is_complex = FRAME_MBAFF || MB_FIELD || IS_INTRA_PCM(mb_type) || s->codec_id != CODEC_ID_H264 || (s->flags&CODEC_FLAG_GRAY) || s->encoding;
  3071. if(!s->decode)
  3072. return;
  3073. if (is_complex)
  3074. hl_decode_mb_complex(h);
  3075. else hl_decode_mb_simple(h);
  3076. }
  3077. /**
  3078. * fills the default_ref_list.
  3079. */
  3080. static int fill_default_ref_list(H264Context *h){
  3081. MpegEncContext * const s = &h->s;
  3082. int i;
  3083. int smallest_poc_greater_than_current = -1;
  3084. Picture sorted_short_ref[32];
  3085. if(h->slice_type==B_TYPE){
  3086. int out_i;
  3087. int limit= INT_MIN;
  3088. /* sort frame according to poc in B slice */
  3089. for(out_i=0; out_i<h->short_ref_count; out_i++){
  3090. int best_i=INT_MIN;
  3091. int best_poc=INT_MAX;
  3092. for(i=0; i<h->short_ref_count; i++){
  3093. const int poc= h->short_ref[i]->poc;
  3094. if(poc > limit && poc < best_poc){
  3095. best_poc= poc;
  3096. best_i= i;
  3097. }
  3098. }
  3099. assert(best_i != INT_MIN);
  3100. limit= best_poc;
  3101. sorted_short_ref[out_i]= *h->short_ref[best_i];
  3102. tprintf(h->s.avctx, "sorted poc: %d->%d poc:%d fn:%d\n", best_i, out_i, sorted_short_ref[out_i].poc, sorted_short_ref[out_i].frame_num);
  3103. if (-1 == smallest_poc_greater_than_current) {
  3104. if (h->short_ref[best_i]->poc >= s->current_picture_ptr->poc) {
  3105. smallest_poc_greater_than_current = out_i;
  3106. }
  3107. }
  3108. }
  3109. }
  3110. if(s->picture_structure == PICT_FRAME){
  3111. if(h->slice_type==B_TYPE){
  3112. int list;
  3113. tprintf(h->s.avctx, "current poc: %d, smallest_poc_greater_than_current: %d\n", s->current_picture_ptr->poc, smallest_poc_greater_than_current);
  3114. // find the largest poc
  3115. for(list=0; list<2; list++){
  3116. int index = 0;
  3117. int j= -99;
  3118. int step= list ? -1 : 1;
  3119. for(i=0; i<h->short_ref_count && index < h->ref_count[list]; i++, j+=step) {
  3120. while(j<0 || j>= h->short_ref_count){
  3121. if(j != -99 && step == (list ? -1 : 1))
  3122. return -1;
  3123. step = -step;
  3124. j= smallest_poc_greater_than_current + (step>>1);
  3125. }
  3126. if(sorted_short_ref[j].reference != 3) continue;
  3127. h->default_ref_list[list][index ]= sorted_short_ref[j];
  3128. h->default_ref_list[list][index++].pic_id= sorted_short_ref[j].frame_num;
  3129. }
  3130. for(i = 0; i < 16 && index < h->ref_count[ list ]; i++){
  3131. if(h->long_ref[i] == NULL) continue;
  3132. if(h->long_ref[i]->reference != 3) continue;
  3133. h->default_ref_list[ list ][index ]= *h->long_ref[i];
  3134. h->default_ref_list[ list ][index++].pic_id= i;;
  3135. }
  3136. if(list && (smallest_poc_greater_than_current<=0 || smallest_poc_greater_than_current>=h->short_ref_count) && (1 < index)){
  3137. // swap the two first elements of L1 when
  3138. // L0 and L1 are identical
  3139. Picture temp= h->default_ref_list[1][0];
  3140. h->default_ref_list[1][0] = h->default_ref_list[1][1];
  3141. h->default_ref_list[1][1] = temp;
  3142. }
  3143. if(index < h->ref_count[ list ])
  3144. memset(&h->default_ref_list[list][index], 0, sizeof(Picture)*(h->ref_count[ list ] - index));
  3145. }
  3146. }else{
  3147. int index=0;
  3148. for(i=0; i<h->short_ref_count; i++){
  3149. if(h->short_ref[i]->reference != 3) continue; //FIXME refernce field shit
  3150. h->default_ref_list[0][index ]= *h->short_ref[i];
  3151. h->default_ref_list[0][index++].pic_id= h->short_ref[i]->frame_num;
  3152. }
  3153. for(i = 0; i < 16; i++){
  3154. if(h->long_ref[i] == NULL) continue;
  3155. if(h->long_ref[i]->reference != 3) continue;
  3156. h->default_ref_list[0][index ]= *h->long_ref[i];
  3157. h->default_ref_list[0][index++].pic_id= i;;
  3158. }
  3159. if(index < h->ref_count[0])
  3160. memset(&h->default_ref_list[0][index], 0, sizeof(Picture)*(h->ref_count[0] - index));
  3161. }
  3162. }else{ //FIELD
  3163. if(h->slice_type==B_TYPE){
  3164. }else{
  3165. //FIXME second field balh
  3166. }
  3167. }
  3168. #ifdef TRACE
  3169. for (i=0; i<h->ref_count[0]; i++) {
  3170. tprintf(h->s.avctx, "List0: %s fn:%d 0x%p\n", (h->default_ref_list[0][i].long_ref ? "LT" : "ST"), h->default_ref_list[0][i].pic_id, h->default_ref_list[0][i].data[0]);
  3171. }
  3172. if(h->slice_type==B_TYPE){
  3173. for (i=0; i<h->ref_count[1]; i++) {
  3174. tprintf(h->s.avctx, "List1: %s fn:%d 0x%p\n", (h->default_ref_list[1][i].long_ref ? "LT" : "ST"), h->default_ref_list[1][i].pic_id, h->default_ref_list[0][i].data[0]);
  3175. }
  3176. }
  3177. #endif
  3178. return 0;
  3179. }
  3180. static void print_short_term(H264Context *h);
  3181. static void print_long_term(H264Context *h);
  3182. static int decode_ref_pic_list_reordering(H264Context *h){
  3183. MpegEncContext * const s = &h->s;
  3184. int list, index;
  3185. print_short_term(h);
  3186. print_long_term(h);
  3187. if(h->slice_type==I_TYPE || h->slice_type==SI_TYPE) return 0; //FIXME move before func
  3188. for(list=0; list<h->list_count; list++){
  3189. memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
  3190. if(get_bits1(&s->gb)){
  3191. int pred= h->curr_pic_num;
  3192. for(index=0; ; index++){
  3193. unsigned int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);
  3194. unsigned int pic_id;
  3195. int i;
  3196. Picture *ref = NULL;
  3197. if(reordering_of_pic_nums_idc==3)
  3198. break;
  3199. if(index >= h->ref_count[list]){
  3200. av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
  3201. return -1;
  3202. }
  3203. if(reordering_of_pic_nums_idc<3){
  3204. if(reordering_of_pic_nums_idc<2){
  3205. const unsigned int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
  3206. if(abs_diff_pic_num >= h->max_pic_num){
  3207. av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
  3208. return -1;
  3209. }
  3210. if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
  3211. else pred+= abs_diff_pic_num;
  3212. pred &= h->max_pic_num - 1;
  3213. for(i= h->short_ref_count-1; i>=0; i--){
  3214. ref = h->short_ref[i];
  3215. assert(ref->reference == 3);
  3216. assert(!ref->long_ref);
  3217. if(ref->data[0] != NULL && ref->frame_num == pred && ref->long_ref == 0) // ignore non existing pictures by testing data[0] pointer
  3218. break;
  3219. }
  3220. if(i>=0)
  3221. ref->pic_id= ref->frame_num;
  3222. }else{
  3223. pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
  3224. if(pic_id>31){
  3225. av_log(h->s.avctx, AV_LOG_ERROR, "long_term_pic_idx overflow\n");
  3226. return -1;
  3227. }
  3228. ref = h->long_ref[pic_id];
  3229. if(ref){
  3230. ref->pic_id= pic_id;
  3231. assert(ref->reference == 3);
  3232. assert(ref->long_ref);
  3233. i=0;
  3234. }else{
  3235. i=-1;
  3236. }
  3237. }
  3238. if (i < 0) {
  3239. av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
  3240. memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
  3241. } else {
  3242. for(i=index; i+1<h->ref_count[list]; i++){
  3243. if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
  3244. break;
  3245. }
  3246. for(; i > index; i--){
  3247. h->ref_list[list][i]= h->ref_list[list][i-1];
  3248. }
  3249. h->ref_list[list][index]= *ref;
  3250. }
  3251. }else{
  3252. av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
  3253. return -1;
  3254. }
  3255. }
  3256. }
  3257. }
  3258. for(list=0; list<h->list_count; list++){
  3259. for(index= 0; index < h->ref_count[list]; index++){
  3260. if(!h->ref_list[list][index].data[0])
  3261. h->ref_list[list][index]= s->current_picture;
  3262. }
  3263. }
  3264. if(h->slice_type==B_TYPE && !h->direct_spatial_mv_pred)
  3265. direct_dist_scale_factor(h);
  3266. direct_ref_list_init(h);
  3267. return 0;
  3268. }
  3269. static void fill_mbaff_ref_list(H264Context *h){
  3270. int list, i, j;
  3271. for(list=0; list<2; list++){ //FIXME try list_count
  3272. for(i=0; i<h->ref_count[list]; i++){
  3273. Picture *frame = &h->ref_list[list][i];
  3274. Picture *field = &h->ref_list[list][16+2*i];
  3275. field[0] = *frame;
  3276. for(j=0; j<3; j++)
  3277. field[0].linesize[j] <<= 1;
  3278. field[1] = field[0];
  3279. for(j=0; j<3; j++)
  3280. field[1].data[j] += frame->linesize[j];
  3281. h->luma_weight[list][16+2*i] = h->luma_weight[list][16+2*i+1] = h->luma_weight[list][i];
  3282. h->luma_offset[list][16+2*i] = h->luma_offset[list][16+2*i+1] = h->luma_offset[list][i];
  3283. for(j=0; j<2; j++){
  3284. h->chroma_weight[list][16+2*i][j] = h->chroma_weight[list][16+2*i+1][j] = h->chroma_weight[list][i][j];
  3285. h->chroma_offset[list][16+2*i][j] = h->chroma_offset[list][16+2*i+1][j] = h->chroma_offset[list][i][j];
  3286. }
  3287. }
  3288. }
  3289. for(j=0; j<h->ref_count[1]; j++){
  3290. for(i=0; i<h->ref_count[0]; i++)
  3291. h->implicit_weight[j][16+2*i] = h->implicit_weight[j][16+2*i+1] = h->implicit_weight[j][i];
  3292. memcpy(h->implicit_weight[16+2*j], h->implicit_weight[j], sizeof(*h->implicit_weight));
  3293. memcpy(h->implicit_weight[16+2*j+1], h->implicit_weight[j], sizeof(*h->implicit_weight));
  3294. }
  3295. }
  3296. static int pred_weight_table(H264Context *h){
  3297. MpegEncContext * const s = &h->s;
  3298. int list, i;
  3299. int luma_def, chroma_def;
  3300. h->use_weight= 0;
  3301. h->use_weight_chroma= 0;
  3302. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  3303. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  3304. luma_def = 1<<h->luma_log2_weight_denom;
  3305. chroma_def = 1<<h->chroma_log2_weight_denom;
  3306. for(list=0; list<2; list++){
  3307. for(i=0; i<h->ref_count[list]; i++){
  3308. int luma_weight_flag, chroma_weight_flag;
  3309. luma_weight_flag= get_bits1(&s->gb);
  3310. if(luma_weight_flag){
  3311. h->luma_weight[list][i]= get_se_golomb(&s->gb);
  3312. h->luma_offset[list][i]= get_se_golomb(&s->gb);
  3313. if( h->luma_weight[list][i] != luma_def
  3314. || h->luma_offset[list][i] != 0)
  3315. h->use_weight= 1;
  3316. }else{
  3317. h->luma_weight[list][i]= luma_def;
  3318. h->luma_offset[list][i]= 0;
  3319. }
  3320. chroma_weight_flag= get_bits1(&s->gb);
  3321. if(chroma_weight_flag){
  3322. int j;
  3323. for(j=0; j<2; j++){
  3324. h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
  3325. h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
  3326. if( h->chroma_weight[list][i][j] != chroma_def
  3327. || h->chroma_offset[list][i][j] != 0)
  3328. h->use_weight_chroma= 1;
  3329. }
  3330. }else{
  3331. int j;
  3332. for(j=0; j<2; j++){
  3333. h->chroma_weight[list][i][j]= chroma_def;
  3334. h->chroma_offset[list][i][j]= 0;
  3335. }
  3336. }
  3337. }
  3338. if(h->slice_type != B_TYPE) break;
  3339. }
  3340. h->use_weight= h->use_weight || h->use_weight_chroma;
  3341. return 0;
  3342. }
  3343. static void implicit_weight_table(H264Context *h){
  3344. MpegEncContext * const s = &h->s;
  3345. int ref0, ref1;
  3346. int cur_poc = s->current_picture_ptr->poc;
  3347. if( h->ref_count[0] == 1 && h->ref_count[1] == 1
  3348. && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
  3349. h->use_weight= 0;
  3350. h->use_weight_chroma= 0;
  3351. return;
  3352. }
  3353. h->use_weight= 2;
  3354. h->use_weight_chroma= 2;
  3355. h->luma_log2_weight_denom= 5;
  3356. h->chroma_log2_weight_denom= 5;
  3357. for(ref0=0; ref0 < h->ref_count[0]; ref0++){
  3358. int poc0 = h->ref_list[0][ref0].poc;
  3359. for(ref1=0; ref1 < h->ref_count[1]; ref1++){
  3360. int poc1 = h->ref_list[1][ref1].poc;
  3361. int td = av_clip(poc1 - poc0, -128, 127);
  3362. if(td){
  3363. int tb = av_clip(cur_poc - poc0, -128, 127);
  3364. int tx = (16384 + (FFABS(td) >> 1)) / td;
  3365. int dist_scale_factor = av_clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
  3366. if(dist_scale_factor < -64 || dist_scale_factor > 128)
  3367. h->implicit_weight[ref0][ref1] = 32;
  3368. else
  3369. h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor;
  3370. }else
  3371. h->implicit_weight[ref0][ref1] = 32;
  3372. }
  3373. }
  3374. }
  3375. static inline void unreference_pic(H264Context *h, Picture *pic){
  3376. int i;
  3377. pic->reference=0;
  3378. if(pic == h->delayed_output_pic)
  3379. pic->reference=1;
  3380. else{
  3381. for(i = 0; h->delayed_pic[i]; i++)
  3382. if(pic == h->delayed_pic[i]){
  3383. pic->reference=1;
  3384. break;
  3385. }
  3386. }
  3387. }
  3388. /**
  3389. * instantaneous decoder refresh.
  3390. */
  3391. static void idr(H264Context *h){
  3392. int i;
  3393. for(i=0; i<16; i++){
  3394. if (h->long_ref[i] != NULL) {
  3395. unreference_pic(h, h->long_ref[i]);
  3396. h->long_ref[i]= NULL;
  3397. }
  3398. }
  3399. h->long_ref_count=0;
  3400. for(i=0; i<h->short_ref_count; i++){
  3401. unreference_pic(h, h->short_ref[i]);
  3402. h->short_ref[i]= NULL;
  3403. }
  3404. h->short_ref_count=0;
  3405. }
  3406. /* forget old pics after a seek */
  3407. static void flush_dpb(AVCodecContext *avctx){
  3408. H264Context *h= avctx->priv_data;
  3409. int i;
  3410. for(i=0; i<16; i++) {
  3411. if(h->delayed_pic[i])
  3412. h->delayed_pic[i]->reference= 0;
  3413. h->delayed_pic[i]= NULL;
  3414. }
  3415. if(h->delayed_output_pic)
  3416. h->delayed_output_pic->reference= 0;
  3417. h->delayed_output_pic= NULL;
  3418. idr(h);
  3419. if(h->s.current_picture_ptr)
  3420. h->s.current_picture_ptr->reference= 0;
  3421. }
  3422. /**
  3423. *
  3424. * @return the removed picture or NULL if an error occurs
  3425. */
  3426. static Picture * remove_short(H264Context *h, int frame_num){
  3427. MpegEncContext * const s = &h->s;
  3428. int i;
  3429. if(s->avctx->debug&FF_DEBUG_MMCO)
  3430. av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
  3431. for(i=0; i<h->short_ref_count; i++){
  3432. Picture *pic= h->short_ref[i];
  3433. if(s->avctx->debug&FF_DEBUG_MMCO)
  3434. av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
  3435. if(pic->frame_num == frame_num){
  3436. h->short_ref[i]= NULL;
  3437. memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i - 1)*sizeof(Picture*));
  3438. h->short_ref_count--;
  3439. return pic;
  3440. }
  3441. }
  3442. return NULL;
  3443. }
  3444. /**
  3445. *
  3446. * @return the removed picture or NULL if an error occurs
  3447. */
  3448. static Picture * remove_long(H264Context *h, int i){
  3449. Picture *pic;
  3450. pic= h->long_ref[i];
  3451. h->long_ref[i]= NULL;
  3452. if(pic) h->long_ref_count--;
  3453. return pic;
  3454. }
  3455. /**
  3456. * print short term list
  3457. */
  3458. static void print_short_term(H264Context *h) {
  3459. uint32_t i;
  3460. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3461. av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
  3462. for(i=0; i<h->short_ref_count; i++){
  3463. Picture *pic= h->short_ref[i];
  3464. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3465. }
  3466. }
  3467. }
  3468. /**
  3469. * print long term list
  3470. */
  3471. static void print_long_term(H264Context *h) {
  3472. uint32_t i;
  3473. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3474. av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
  3475. for(i = 0; i < 16; i++){
  3476. Picture *pic= h->long_ref[i];
  3477. if (pic) {
  3478. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3479. }
  3480. }
  3481. }
  3482. }
  3483. /**
  3484. * Executes the reference picture marking (memory management control operations).
  3485. */
  3486. static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
  3487. MpegEncContext * const s = &h->s;
  3488. int i, j;
  3489. int current_is_long=0;
  3490. Picture *pic;
  3491. if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
  3492. av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
  3493. for(i=0; i<mmco_count; i++){
  3494. if(s->avctx->debug&FF_DEBUG_MMCO)
  3495. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco:%d %d %d\n", h->mmco[i].opcode, h->mmco[i].short_frame_num, h->mmco[i].long_index);
  3496. switch(mmco[i].opcode){
  3497. case MMCO_SHORT2UNUSED:
  3498. pic= remove_short(h, mmco[i].short_frame_num);
  3499. if(pic)
  3500. unreference_pic(h, pic);
  3501. else if(s->avctx->debug&FF_DEBUG_MMCO)
  3502. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_short() failure\n");
  3503. break;
  3504. case MMCO_SHORT2LONG:
  3505. pic= remove_long(h, mmco[i].long_index);
  3506. if(pic) unreference_pic(h, pic);
  3507. h->long_ref[ mmco[i].long_index ]= remove_short(h, mmco[i].short_frame_num);
  3508. if (h->long_ref[ mmco[i].long_index ]){
  3509. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3510. h->long_ref_count++;
  3511. }
  3512. break;
  3513. case MMCO_LONG2UNUSED:
  3514. pic= remove_long(h, mmco[i].long_index);
  3515. if(pic)
  3516. unreference_pic(h, pic);
  3517. else if(s->avctx->debug&FF_DEBUG_MMCO)
  3518. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_long() failure\n");
  3519. break;
  3520. case MMCO_LONG:
  3521. pic= remove_long(h, mmco[i].long_index);
  3522. if(pic) unreference_pic(h, pic);
  3523. h->long_ref[ mmco[i].long_index ]= s->current_picture_ptr;
  3524. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3525. h->long_ref_count++;
  3526. current_is_long=1;
  3527. break;
  3528. case MMCO_SET_MAX_LONG:
  3529. assert(mmco[i].long_index <= 16);
  3530. // just remove the long term which index is greater than new max
  3531. for(j = mmco[i].long_index; j<16; j++){
  3532. pic = remove_long(h, j);
  3533. if (pic) unreference_pic(h, pic);
  3534. }
  3535. break;
  3536. case MMCO_RESET:
  3537. while(h->short_ref_count){
  3538. pic= remove_short(h, h->short_ref[0]->frame_num);
  3539. if(pic) unreference_pic(h, pic);
  3540. }
  3541. for(j = 0; j < 16; j++) {
  3542. pic= remove_long(h, j);
  3543. if(pic) unreference_pic(h, pic);
  3544. }
  3545. break;
  3546. default: assert(0);
  3547. }
  3548. }
  3549. if(!current_is_long){
  3550. pic= remove_short(h, s->current_picture_ptr->frame_num);
  3551. if(pic){
  3552. unreference_pic(h, pic);
  3553. av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
  3554. }
  3555. if(h->short_ref_count)
  3556. memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
  3557. h->short_ref[0]= s->current_picture_ptr;
  3558. h->short_ref[0]->long_ref=0;
  3559. h->short_ref_count++;
  3560. }
  3561. print_short_term(h);
  3562. print_long_term(h);
  3563. return 0;
  3564. }
  3565. static int decode_ref_pic_marking(H264Context *h){
  3566. MpegEncContext * const s = &h->s;
  3567. int i;
  3568. if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
  3569. s->broken_link= get_bits1(&s->gb) -1;
  3570. h->mmco[0].long_index= get_bits1(&s->gb) - 1; // current_long_term_idx
  3571. if(h->mmco[0].long_index == -1)
  3572. h->mmco_index= 0;
  3573. else{
  3574. h->mmco[0].opcode= MMCO_LONG;
  3575. h->mmco_index= 1;
  3576. }
  3577. }else{
  3578. if(get_bits1(&s->gb)){ // adaptive_ref_pic_marking_mode_flag
  3579. for(i= 0; i<MAX_MMCO_COUNT; i++) {
  3580. MMCOOpcode opcode= get_ue_golomb(&s->gb);;
  3581. h->mmco[i].opcode= opcode;
  3582. if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
  3583. h->mmco[i].short_frame_num= (h->frame_num - get_ue_golomb(&s->gb) - 1) & ((1<<h->sps.log2_max_frame_num)-1); //FIXME fields
  3584. /* if(h->mmco[i].short_frame_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_frame_num ] == NULL){
  3585. av_log(s->avctx, AV_LOG_ERROR, "illegal short ref in memory management control operation %d\n", mmco);
  3586. return -1;
  3587. }*/
  3588. }
  3589. if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
  3590. unsigned int long_index= get_ue_golomb(&s->gb);
  3591. if(/*h->mmco[i].long_index >= h->long_ref_count || h->long_ref[ h->mmco[i].long_index ] == NULL*/ long_index >= 16){
  3592. av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
  3593. return -1;
  3594. }
  3595. h->mmco[i].long_index= long_index;
  3596. }
  3597. if(opcode > (unsigned)MMCO_LONG){
  3598. av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
  3599. return -1;
  3600. }
  3601. if(opcode == MMCO_END)
  3602. break;
  3603. }
  3604. h->mmco_index= i;
  3605. }else{
  3606. assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
  3607. if(h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count){ //FIXME fields
  3608. h->mmco[0].opcode= MMCO_SHORT2UNUSED;
  3609. h->mmco[0].short_frame_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
  3610. h->mmco_index= 1;
  3611. }else
  3612. h->mmco_index= 0;
  3613. }
  3614. }
  3615. return 0;
  3616. }
  3617. static int init_poc(H264Context *h){
  3618. MpegEncContext * const s = &h->s;
  3619. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  3620. int field_poc[2];
  3621. if(h->nal_unit_type == NAL_IDR_SLICE){
  3622. h->frame_num_offset= 0;
  3623. }else{
  3624. if(h->frame_num < h->prev_frame_num)
  3625. h->frame_num_offset= h->prev_frame_num_offset + max_frame_num;
  3626. else
  3627. h->frame_num_offset= h->prev_frame_num_offset;
  3628. }
  3629. if(h->sps.poc_type==0){
  3630. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  3631. if(h->nal_unit_type == NAL_IDR_SLICE){
  3632. h->prev_poc_msb=
  3633. h->prev_poc_lsb= 0;
  3634. }
  3635. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  3636. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  3637. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  3638. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  3639. else
  3640. h->poc_msb = h->prev_poc_msb;
  3641. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  3642. field_poc[0] =
  3643. field_poc[1] = h->poc_msb + h->poc_lsb;
  3644. if(s->picture_structure == PICT_FRAME)
  3645. field_poc[1] += h->delta_poc_bottom;
  3646. }else if(h->sps.poc_type==1){
  3647. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  3648. int i;
  3649. if(h->sps.poc_cycle_length != 0)
  3650. abs_frame_num = h->frame_num_offset + h->frame_num;
  3651. else
  3652. abs_frame_num = 0;
  3653. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  3654. abs_frame_num--;
  3655. expected_delta_per_poc_cycle = 0;
  3656. for(i=0; i < h->sps.poc_cycle_length; i++)
  3657. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  3658. if(abs_frame_num > 0){
  3659. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  3660. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  3661. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  3662. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  3663. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  3664. } else
  3665. expectedpoc = 0;
  3666. if(h->nal_ref_idc == 0)
  3667. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  3668. field_poc[0] = expectedpoc + h->delta_poc[0];
  3669. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  3670. if(s->picture_structure == PICT_FRAME)
  3671. field_poc[1] += h->delta_poc[1];
  3672. }else{
  3673. int poc;
  3674. if(h->nal_unit_type == NAL_IDR_SLICE){
  3675. poc= 0;
  3676. }else{
  3677. if(h->nal_ref_idc) poc= 2*(h->frame_num_offset + h->frame_num);
  3678. else poc= 2*(h->frame_num_offset + h->frame_num) - 1;
  3679. }
  3680. field_poc[0]= poc;
  3681. field_poc[1]= poc;
  3682. }
  3683. if(s->picture_structure != PICT_BOTTOM_FIELD)
  3684. s->current_picture_ptr->field_poc[0]= field_poc[0];
  3685. if(s->picture_structure != PICT_TOP_FIELD)
  3686. s->current_picture_ptr->field_poc[1]= field_poc[1];
  3687. if(s->picture_structure == PICT_FRAME) // FIXME field pix?
  3688. s->current_picture_ptr->poc= FFMIN(field_poc[0], field_poc[1]);
  3689. return 0;
  3690. }
  3691. /**
  3692. * decodes a slice header.
  3693. * this will allso call MPV_common_init() and frame_start() as needed
  3694. */
  3695. static int decode_slice_header(H264Context *h){
  3696. MpegEncContext * const s = &h->s;
  3697. unsigned int first_mb_in_slice;
  3698. unsigned int pps_id;
  3699. int num_ref_idx_active_override_flag;
  3700. static const uint8_t slice_type_map[5]= {P_TYPE, B_TYPE, I_TYPE, SP_TYPE, SI_TYPE};
  3701. unsigned int slice_type, tmp;
  3702. int default_ref_list_done = 0;
  3703. s->current_picture.reference= h->nal_ref_idc != 0;
  3704. s->dropable= h->nal_ref_idc == 0;
  3705. first_mb_in_slice= get_ue_golomb(&s->gb);
  3706. if((s->flags2 & CODEC_FLAG2_CHUNKS) && first_mb_in_slice == 0){
  3707. h->slice_num = 0;
  3708. s->current_picture_ptr= NULL;
  3709. }
  3710. slice_type= get_ue_golomb(&s->gb);
  3711. if(slice_type > 9){
  3712. av_log(h->s.avctx, AV_LOG_ERROR, "slice type too large (%d) at %d %d\n", h->slice_type, s->mb_x, s->mb_y);
  3713. return -1;
  3714. }
  3715. if(slice_type > 4){
  3716. slice_type -= 5;
  3717. h->slice_type_fixed=1;
  3718. }else
  3719. h->slice_type_fixed=0;
  3720. slice_type= slice_type_map[ slice_type ];
  3721. if (slice_type == I_TYPE
  3722. || (h->slice_num != 0 && slice_type == h->slice_type) ) {
  3723. default_ref_list_done = 1;
  3724. }
  3725. h->slice_type= slice_type;
  3726. s->pict_type= h->slice_type; // to make a few old func happy, it's wrong though
  3727. pps_id= get_ue_golomb(&s->gb);
  3728. if(pps_id>=MAX_PPS_COUNT){
  3729. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  3730. return -1;
  3731. }
  3732. h->pps= h->pps_buffer[pps_id];
  3733. if(h->pps.slice_group_count == 0){
  3734. av_log(h->s.avctx, AV_LOG_ERROR, "non existing PPS referenced\n");
  3735. return -1;
  3736. }
  3737. h->sps= h->sps_buffer[ h->pps.sps_id ];
  3738. if(h->sps.log2_max_frame_num == 0){
  3739. av_log(h->s.avctx, AV_LOG_ERROR, "non existing SPS referenced\n");
  3740. return -1;
  3741. }
  3742. if(h->dequant_coeff_pps != pps_id){
  3743. h->dequant_coeff_pps = pps_id;
  3744. init_dequant_tables(h);
  3745. }
  3746. s->mb_width= h->sps.mb_width;
  3747. s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  3748. h->b_stride= s->mb_width*4;
  3749. h->b8_stride= s->mb_width*2;
  3750. s->width = 16*s->mb_width - 2*(h->sps.crop_left + h->sps.crop_right );
  3751. if(h->sps.frame_mbs_only_flag)
  3752. s->height= 16*s->mb_height - 2*(h->sps.crop_top + h->sps.crop_bottom);
  3753. else
  3754. s->height= 16*s->mb_height - 4*(h->sps.crop_top + h->sps.crop_bottom); //FIXME recheck
  3755. if (s->context_initialized
  3756. && ( s->width != s->avctx->width || s->height != s->avctx->height)) {
  3757. free_tables(h);
  3758. MPV_common_end(s);
  3759. }
  3760. if (!s->context_initialized) {
  3761. if (MPV_common_init(s) < 0)
  3762. return -1;
  3763. if(s->dsp.h264_idct_add == ff_h264_idct_add_c){ //FIXME little ugly
  3764. memcpy(h->zigzag_scan, zigzag_scan, 16*sizeof(uint8_t));
  3765. memcpy(h-> field_scan, field_scan, 16*sizeof(uint8_t));
  3766. }else{
  3767. int i;
  3768. for(i=0; i<16; i++){
  3769. #define T(x) (x>>2) | ((x<<2) & 0xF)
  3770. h->zigzag_scan[i] = T(zigzag_scan[i]);
  3771. h-> field_scan[i] = T( field_scan[i]);
  3772. #undef T
  3773. }
  3774. }
  3775. if(s->dsp.h264_idct8_add == ff_h264_idct8_add_c){
  3776. memcpy(h->zigzag_scan8x8, zigzag_scan8x8, 64*sizeof(uint8_t));
  3777. memcpy(h->zigzag_scan8x8_cavlc, zigzag_scan8x8_cavlc, 64*sizeof(uint8_t));
  3778. memcpy(h->field_scan8x8, field_scan8x8, 64*sizeof(uint8_t));
  3779. memcpy(h->field_scan8x8_cavlc, field_scan8x8_cavlc, 64*sizeof(uint8_t));
  3780. }else{
  3781. int i;
  3782. for(i=0; i<64; i++){
  3783. #define T(x) (x>>3) | ((x&7)<<3)
  3784. h->zigzag_scan8x8[i] = T(zigzag_scan8x8[i]);
  3785. h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
  3786. h->field_scan8x8[i] = T(field_scan8x8[i]);
  3787. h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
  3788. #undef T
  3789. }
  3790. }
  3791. if(h->sps.transform_bypass){ //FIXME same ugly
  3792. h->zigzag_scan_q0 = zigzag_scan;
  3793. h->zigzag_scan8x8_q0 = zigzag_scan8x8;
  3794. h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
  3795. h->field_scan_q0 = field_scan;
  3796. h->field_scan8x8_q0 = field_scan8x8;
  3797. h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
  3798. }else{
  3799. h->zigzag_scan_q0 = h->zigzag_scan;
  3800. h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
  3801. h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
  3802. h->field_scan_q0 = h->field_scan;
  3803. h->field_scan8x8_q0 = h->field_scan8x8;
  3804. h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
  3805. }
  3806. alloc_tables(h);
  3807. s->avctx->width = s->width;
  3808. s->avctx->height = s->height;
  3809. s->avctx->sample_aspect_ratio= h->sps.sar;
  3810. if(!s->avctx->sample_aspect_ratio.den)
  3811. s->avctx->sample_aspect_ratio.den = 1;
  3812. if(h->sps.timing_info_present_flag){
  3813. s->avctx->time_base= (AVRational){h->sps.num_units_in_tick * 2, h->sps.time_scale};
  3814. if(h->x264_build > 0 && h->x264_build < 44)
  3815. s->avctx->time_base.den *= 2;
  3816. av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
  3817. s->avctx->time_base.num, s->avctx->time_base.den, 1<<30);
  3818. }
  3819. }
  3820. if(h->slice_num == 0){
  3821. if(frame_start(h) < 0)
  3822. return -1;
  3823. }
  3824. s->current_picture_ptr->frame_num= //FIXME frame_num cleanup
  3825. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  3826. h->mb_mbaff = 0;
  3827. h->mb_aff_frame = 0;
  3828. if(h->sps.frame_mbs_only_flag){
  3829. s->picture_structure= PICT_FRAME;
  3830. }else{
  3831. if(get_bits1(&s->gb)) { //field_pic_flag
  3832. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  3833. av_log(h->s.avctx, AV_LOG_ERROR, "PAFF interlacing is not implemented\n");
  3834. } else {
  3835. s->picture_structure= PICT_FRAME;
  3836. h->mb_aff_frame = h->sps.mb_aff;
  3837. }
  3838. }
  3839. assert(s->mb_num == s->mb_width * s->mb_height);
  3840. if(first_mb_in_slice << h->mb_aff_frame >= s->mb_num ||
  3841. first_mb_in_slice >= s->mb_num){
  3842. av_log(h->s.avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
  3843. return -1;
  3844. }
  3845. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  3846. s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << h->mb_aff_frame;
  3847. assert(s->mb_y < s->mb_height);
  3848. if(s->picture_structure==PICT_FRAME){
  3849. h->curr_pic_num= h->frame_num;
  3850. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  3851. }else{
  3852. h->curr_pic_num= 2*h->frame_num;
  3853. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  3854. }
  3855. if(h->nal_unit_type == NAL_IDR_SLICE){
  3856. get_ue_golomb(&s->gb); /* idr_pic_id */
  3857. }
  3858. if(h->sps.poc_type==0){
  3859. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  3860. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  3861. h->delta_poc_bottom= get_se_golomb(&s->gb);
  3862. }
  3863. }
  3864. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  3865. h->delta_poc[0]= get_se_golomb(&s->gb);
  3866. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  3867. h->delta_poc[1]= get_se_golomb(&s->gb);
  3868. }
  3869. init_poc(h);
  3870. if(h->pps.redundant_pic_cnt_present){
  3871. h->redundant_pic_count= get_ue_golomb(&s->gb);
  3872. }
  3873. //set defaults, might be overriden a few line later
  3874. h->ref_count[0]= h->pps.ref_count[0];
  3875. h->ref_count[1]= h->pps.ref_count[1];
  3876. if(h->slice_type == P_TYPE || h->slice_type == SP_TYPE || h->slice_type == B_TYPE){
  3877. if(h->slice_type == B_TYPE){
  3878. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  3879. if(h->sps.mb_aff && h->direct_spatial_mv_pred)
  3880. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF + spatial direct mode is not implemented\n");
  3881. }
  3882. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  3883. if(num_ref_idx_active_override_flag){
  3884. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  3885. if(h->slice_type==B_TYPE)
  3886. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  3887. if(h->ref_count[0]-1 > 32-1 || h->ref_count[1]-1 > 32-1){
  3888. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  3889. h->ref_count[0]= h->ref_count[1]= 1;
  3890. return -1;
  3891. }
  3892. }
  3893. if(h->slice_type == B_TYPE)
  3894. h->list_count= 2;
  3895. else
  3896. h->list_count= 1;
  3897. }else
  3898. h->list_count= 0;
  3899. if(!default_ref_list_done){
  3900. fill_default_ref_list(h);
  3901. }
  3902. if(decode_ref_pic_list_reordering(h) < 0)
  3903. return -1;
  3904. if( (h->pps.weighted_pred && (h->slice_type == P_TYPE || h->slice_type == SP_TYPE ))
  3905. || (h->pps.weighted_bipred_idc==1 && h->slice_type==B_TYPE ) )
  3906. pred_weight_table(h);
  3907. else if(h->pps.weighted_bipred_idc==2 && h->slice_type==B_TYPE)
  3908. implicit_weight_table(h);
  3909. else
  3910. h->use_weight = 0;
  3911. if(s->current_picture.reference)
  3912. decode_ref_pic_marking(h);
  3913. if(FRAME_MBAFF)
  3914. fill_mbaff_ref_list(h);
  3915. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE && h->pps.cabac ){
  3916. tmp = get_ue_golomb(&s->gb);
  3917. if(tmp > 2){
  3918. av_log(s->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
  3919. return -1;
  3920. }
  3921. h->cabac_init_idc= tmp;
  3922. }
  3923. h->last_qscale_diff = 0;
  3924. tmp = h->pps.init_qp + get_se_golomb(&s->gb);
  3925. if(tmp>51){
  3926. av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
  3927. return -1;
  3928. }
  3929. s->qscale= tmp;
  3930. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  3931. //FIXME qscale / qp ... stuff
  3932. if(h->slice_type == SP_TYPE){
  3933. get_bits1(&s->gb); /* sp_for_switch_flag */
  3934. }
  3935. if(h->slice_type==SP_TYPE || h->slice_type == SI_TYPE){
  3936. get_se_golomb(&s->gb); /* slice_qs_delta */
  3937. }
  3938. h->deblocking_filter = 1;
  3939. h->slice_alpha_c0_offset = 0;
  3940. h->slice_beta_offset = 0;
  3941. if( h->pps.deblocking_filter_parameters_present ) {
  3942. tmp= get_ue_golomb(&s->gb);
  3943. if(tmp > 2){
  3944. av_log(s->avctx, AV_LOG_ERROR, "deblocking_filter_idc %u out of range\n", tmp);
  3945. return -1;
  3946. }
  3947. h->deblocking_filter= tmp;
  3948. if(h->deblocking_filter < 2)
  3949. h->deblocking_filter^= 1; // 1<->0
  3950. if( h->deblocking_filter ) {
  3951. h->slice_alpha_c0_offset = get_se_golomb(&s->gb) << 1;
  3952. h->slice_beta_offset = get_se_golomb(&s->gb) << 1;
  3953. }
  3954. }
  3955. if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
  3956. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type != I_TYPE)
  3957. ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type == B_TYPE)
  3958. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  3959. h->deblocking_filter= 0;
  3960. #if 0 //FMO
  3961. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  3962. slice_group_change_cycle= get_bits(&s->gb, ?);
  3963. #endif
  3964. h->slice_num++;
  3965. h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
  3966. h->emu_edge_height= FRAME_MBAFF ? 0 : h->emu_edge_width;
  3967. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  3968. av_log(h->s.avctx, AV_LOG_DEBUG, "slice:%d %s mb:%d %c pps:%u frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s\n",
  3969. h->slice_num,
  3970. (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
  3971. first_mb_in_slice,
  3972. av_get_pict_type_char(h->slice_type),
  3973. pps_id, h->frame_num,
  3974. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  3975. h->ref_count[0], h->ref_count[1],
  3976. s->qscale,
  3977. h->deblocking_filter, h->slice_alpha_c0_offset/2, h->slice_beta_offset/2,
  3978. h->use_weight,
  3979. h->use_weight==1 && h->use_weight_chroma ? "c" : ""
  3980. );
  3981. }
  3982. if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !s->current_picture.reference){
  3983. s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
  3984. s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
  3985. }else{
  3986. s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
  3987. s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
  3988. }
  3989. return 0;
  3990. }
  3991. /**
  3992. *
  3993. */
  3994. static inline int get_level_prefix(GetBitContext *gb){
  3995. unsigned int buf;
  3996. int log;
  3997. OPEN_READER(re, gb);
  3998. UPDATE_CACHE(re, gb);
  3999. buf=GET_CACHE(re, gb);
  4000. log= 32 - av_log2(buf);
  4001. #ifdef TRACE
  4002. print_bin(buf>>(32-log), log);
  4003. av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d lpr @%5d in %s get_level_prefix\n", buf>>(32-log), log, log-1, get_bits_count(gb), __FILE__);
  4004. #endif
  4005. LAST_SKIP_BITS(re, gb, log);
  4006. CLOSE_READER(re, gb);
  4007. return log-1;
  4008. }
  4009. static inline int get_dct8x8_allowed(H264Context *h){
  4010. int i;
  4011. for(i=0; i<4; i++){
  4012. if(!IS_SUB_8X8(h->sub_mb_type[i])
  4013. || (!h->sps.direct_8x8_inference_flag && IS_DIRECT(h->sub_mb_type[i])))
  4014. return 0;
  4015. }
  4016. return 1;
  4017. }
  4018. /**
  4019. * decodes a residual block.
  4020. * @param n block index
  4021. * @param scantable scantable
  4022. * @param max_coeff number of coefficients in the block
  4023. * @return <0 if an error occured
  4024. */
  4025. static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff){
  4026. MpegEncContext * const s = &h->s;
  4027. static const int coeff_token_table_index[17]= {0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3};
  4028. int level[16];
  4029. int zeros_left, coeff_num, coeff_token, total_coeff, i, j, trailing_ones, run_before;
  4030. //FIXME put trailing_onex into the context
  4031. if(n == CHROMA_DC_BLOCK_INDEX){
  4032. coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);
  4033. total_coeff= coeff_token>>2;
  4034. }else{
  4035. if(n == LUMA_DC_BLOCK_INDEX){
  4036. total_coeff= pred_non_zero_count(h, 0);
  4037. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  4038. total_coeff= coeff_token>>2;
  4039. }else{
  4040. total_coeff= pred_non_zero_count(h, n);
  4041. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  4042. total_coeff= coeff_token>>2;
  4043. h->non_zero_count_cache[ scan8[n] ]= total_coeff;
  4044. }
  4045. }
  4046. //FIXME set last_non_zero?
  4047. if(total_coeff==0)
  4048. return 0;
  4049. if(total_coeff > (unsigned)max_coeff) {
  4050. av_log(h->s.avctx, AV_LOG_ERROR, "corrupted macroblock %d %d (total_coeff=%d)\n", s->mb_x, s->mb_y, total_coeff);
  4051. return -1;
  4052. }
  4053. trailing_ones= coeff_token&3;
  4054. tprintf(h->s.avctx, "trailing:%d, total:%d\n", trailing_ones, total_coeff);
  4055. assert(total_coeff<=16);
  4056. for(i=0; i<trailing_ones; i++){
  4057. level[i]= 1 - 2*get_bits1(gb);
  4058. }
  4059. if(i<total_coeff) {
  4060. int level_code, mask;
  4061. int suffix_length = total_coeff > 10 && trailing_ones < 3;
  4062. int prefix= get_level_prefix(gb);
  4063. //first coefficient has suffix_length equal to 0 or 1
  4064. if(prefix<14){ //FIXME try to build a large unified VLC table for all this
  4065. if(suffix_length)
  4066. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  4067. else
  4068. level_code= (prefix<<suffix_length); //part
  4069. }else if(prefix==14){
  4070. if(suffix_length)
  4071. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  4072. else
  4073. level_code= prefix + get_bits(gb, 4); //part
  4074. }else if(prefix==15){
  4075. level_code= (prefix<<suffix_length) + get_bits(gb, 12); //part
  4076. if(suffix_length==0) level_code+=15; //FIXME doesn't make (much)sense
  4077. }else{
  4078. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  4079. return -1;
  4080. }
  4081. if(trailing_ones < 3) level_code += 2;
  4082. suffix_length = 1;
  4083. if(level_code > 5)
  4084. suffix_length++;
  4085. mask= -(level_code&1);
  4086. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  4087. i++;
  4088. //remaining coefficients have suffix_length > 0
  4089. for(;i<total_coeff;i++) {
  4090. static const int suffix_limit[7] = {0,5,11,23,47,95,INT_MAX };
  4091. prefix = get_level_prefix(gb);
  4092. if(prefix<15){
  4093. level_code = (prefix<<suffix_length) + get_bits(gb, suffix_length);
  4094. }else if(prefix==15){
  4095. level_code = (prefix<<suffix_length) + get_bits(gb, 12);
  4096. }else{
  4097. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  4098. return -1;
  4099. }
  4100. mask= -(level_code&1);
  4101. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  4102. if(level_code > suffix_limit[suffix_length])
  4103. suffix_length++;
  4104. }
  4105. }
  4106. if(total_coeff == max_coeff)
  4107. zeros_left=0;
  4108. else{
  4109. if(n == CHROMA_DC_BLOCK_INDEX)
  4110. zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
  4111. else
  4112. zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);
  4113. }
  4114. coeff_num = zeros_left + total_coeff - 1;
  4115. j = scantable[coeff_num];
  4116. if(n > 24){
  4117. block[j] = level[0];
  4118. for(i=1;i<total_coeff;i++) {
  4119. if(zeros_left <= 0)
  4120. run_before = 0;
  4121. else if(zeros_left < 7){
  4122. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  4123. }else{
  4124. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  4125. }
  4126. zeros_left -= run_before;
  4127. coeff_num -= 1 + run_before;
  4128. j= scantable[ coeff_num ];
  4129. block[j]= level[i];
  4130. }
  4131. }else{
  4132. block[j] = (level[0] * qmul[j] + 32)>>6;
  4133. for(i=1;i<total_coeff;i++) {
  4134. if(zeros_left <= 0)
  4135. run_before = 0;
  4136. else if(zeros_left < 7){
  4137. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  4138. }else{
  4139. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  4140. }
  4141. zeros_left -= run_before;
  4142. coeff_num -= 1 + run_before;
  4143. j= scantable[ coeff_num ];
  4144. block[j]= (level[i] * qmul[j] + 32)>>6;
  4145. }
  4146. }
  4147. if(zeros_left<0){
  4148. av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);
  4149. return -1;
  4150. }
  4151. return 0;
  4152. }
  4153. static void predict_field_decoding_flag(H264Context *h){
  4154. MpegEncContext * const s = &h->s;
  4155. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4156. int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
  4157. ? s->current_picture.mb_type[mb_xy-1]
  4158. : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
  4159. ? s->current_picture.mb_type[mb_xy-s->mb_stride]
  4160. : 0;
  4161. h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  4162. }
  4163. /**
  4164. * decodes a P_SKIP or B_SKIP macroblock
  4165. */
  4166. static void decode_mb_skip(H264Context *h){
  4167. MpegEncContext * const s = &h->s;
  4168. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4169. int mb_type=0;
  4170. memset(h->non_zero_count[mb_xy], 0, 16);
  4171. memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
  4172. if(MB_FIELD)
  4173. mb_type|= MB_TYPE_INTERLACED;
  4174. if( h->slice_type == B_TYPE )
  4175. {
  4176. // just for fill_caches. pred_direct_motion will set the real mb_type
  4177. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_SKIP;
  4178. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  4179. pred_direct_motion(h, &mb_type);
  4180. mb_type|= MB_TYPE_SKIP;
  4181. }
  4182. else
  4183. {
  4184. int mx, my;
  4185. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
  4186. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  4187. pred_pskip_motion(h, &mx, &my);
  4188. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  4189. fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
  4190. }
  4191. write_back_motion(h, mb_type);
  4192. s->current_picture.mb_type[mb_xy]= mb_type;
  4193. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4194. h->slice_table[ mb_xy ]= h->slice_num;
  4195. h->prev_mb_skipped= 1;
  4196. }
  4197. /**
  4198. * decodes a macroblock
  4199. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  4200. */
  4201. static int decode_mb_cavlc(H264Context *h){
  4202. MpegEncContext * const s = &h->s;
  4203. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4204. int partition_count;
  4205. unsigned int mb_type, cbp;
  4206. int dct8x8_allowed= h->pps.transform_8x8_mode;
  4207. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?
  4208. tprintf(s->avctx, "pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  4209. cbp = 0; /* avoid warning. FIXME: find a solution without slowing
  4210. down the code */
  4211. if(h->slice_type != I_TYPE && h->slice_type != SI_TYPE){
  4212. if(s->mb_skip_run==-1)
  4213. s->mb_skip_run= get_ue_golomb(&s->gb);
  4214. if (s->mb_skip_run--) {
  4215. if(FRAME_MBAFF && (s->mb_y&1) == 0){
  4216. if(s->mb_skip_run==0)
  4217. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  4218. else
  4219. predict_field_decoding_flag(h);
  4220. }
  4221. decode_mb_skip(h);
  4222. return 0;
  4223. }
  4224. }
  4225. if(FRAME_MBAFF){
  4226. if( (s->mb_y&1) == 0 )
  4227. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  4228. }else
  4229. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  4230. h->prev_mb_skipped= 0;
  4231. mb_type= get_ue_golomb(&s->gb);
  4232. if(h->slice_type == B_TYPE){
  4233. if(mb_type < 23){
  4234. partition_count= b_mb_type_info[mb_type].partition_count;
  4235. mb_type= b_mb_type_info[mb_type].type;
  4236. }else{
  4237. mb_type -= 23;
  4238. goto decode_intra_mb;
  4239. }
  4240. }else if(h->slice_type == P_TYPE /*|| h->slice_type == SP_TYPE */){
  4241. if(mb_type < 5){
  4242. partition_count= p_mb_type_info[mb_type].partition_count;
  4243. mb_type= p_mb_type_info[mb_type].type;
  4244. }else{
  4245. mb_type -= 5;
  4246. goto decode_intra_mb;
  4247. }
  4248. }else{
  4249. assert(h->slice_type == I_TYPE);
  4250. decode_intra_mb:
  4251. if(mb_type > 25){
  4252. av_log(h->s.avctx, AV_LOG_ERROR, "mb_type %d in %c slice too large at %d %d\n", mb_type, av_get_pict_type_char(h->slice_type), s->mb_x, s->mb_y);
  4253. return -1;
  4254. }
  4255. partition_count=0;
  4256. cbp= i_mb_type_info[mb_type].cbp;
  4257. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  4258. mb_type= i_mb_type_info[mb_type].type;
  4259. }
  4260. if(MB_FIELD)
  4261. mb_type |= MB_TYPE_INTERLACED;
  4262. h->slice_table[ mb_xy ]= h->slice_num;
  4263. if(IS_INTRA_PCM(mb_type)){
  4264. unsigned int x, y;
  4265. // we assume these blocks are very rare so we dont optimize it
  4266. align_get_bits(&s->gb);
  4267. // The pixels are stored in the same order as levels in h->mb array.
  4268. for(y=0; y<16; y++){
  4269. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  4270. for(x=0; x<16; x++){
  4271. tprintf(s->avctx, "LUMA ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4272. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= get_bits(&s->gb, 8);
  4273. }
  4274. }
  4275. for(y=0; y<8; y++){
  4276. const int index= 256 + 4*(y&3) + 32*(y>>2);
  4277. for(x=0; x<8; x++){
  4278. tprintf(s->avctx, "CHROMA U ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4279. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  4280. }
  4281. }
  4282. for(y=0; y<8; y++){
  4283. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  4284. for(x=0; x<8; x++){
  4285. tprintf(s->avctx, "CHROMA V ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4286. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  4287. }
  4288. }
  4289. // In deblocking, the quantizer is 0
  4290. s->current_picture.qscale_table[mb_xy]= 0;
  4291. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
  4292. // All coeffs are present
  4293. memset(h->non_zero_count[mb_xy], 16, 16);
  4294. s->current_picture.mb_type[mb_xy]= mb_type;
  4295. return 0;
  4296. }
  4297. if(MB_MBAFF){
  4298. h->ref_count[0] <<= 1;
  4299. h->ref_count[1] <<= 1;
  4300. }
  4301. fill_caches(h, mb_type, 0);
  4302. //mb_pred
  4303. if(IS_INTRA(mb_type)){
  4304. int pred_mode;
  4305. // init_top_left_availability(h);
  4306. if(IS_INTRA4x4(mb_type)){
  4307. int i;
  4308. int di = 1;
  4309. if(dct8x8_allowed && get_bits1(&s->gb)){
  4310. mb_type |= MB_TYPE_8x8DCT;
  4311. di = 4;
  4312. }
  4313. // fill_intra4x4_pred_table(h);
  4314. for(i=0; i<16; i+=di){
  4315. int mode= pred_intra_mode(h, i);
  4316. if(!get_bits1(&s->gb)){
  4317. const int rem_mode= get_bits(&s->gb, 3);
  4318. mode = rem_mode + (rem_mode >= mode);
  4319. }
  4320. if(di==4)
  4321. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  4322. else
  4323. h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
  4324. }
  4325. write_back_intra_pred_mode(h);
  4326. if( check_intra4x4_pred_mode(h) < 0)
  4327. return -1;
  4328. }else{
  4329. h->intra16x16_pred_mode= check_intra_pred_mode(h, h->intra16x16_pred_mode);
  4330. if(h->intra16x16_pred_mode < 0)
  4331. return -1;
  4332. }
  4333. pred_mode= check_intra_pred_mode(h, get_ue_golomb(&s->gb));
  4334. if(pred_mode < 0)
  4335. return -1;
  4336. h->chroma_pred_mode= pred_mode;
  4337. }else if(partition_count==4){
  4338. int i, j, sub_partition_count[4], list, ref[2][4];
  4339. if(h->slice_type == B_TYPE){
  4340. for(i=0; i<4; i++){
  4341. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  4342. if(h->sub_mb_type[i] >=13){
  4343. av_log(h->s.avctx, AV_LOG_ERROR, "B sub_mb_type %u out of range at %d %d\n", h->sub_mb_type[i], s->mb_x, s->mb_y);
  4344. return -1;
  4345. }
  4346. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4347. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4348. }
  4349. if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
  4350. || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3])) {
  4351. pred_direct_motion(h, &mb_type);
  4352. h->ref_cache[0][scan8[4]] =
  4353. h->ref_cache[1][scan8[4]] =
  4354. h->ref_cache[0][scan8[12]] =
  4355. h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
  4356. }
  4357. }else{
  4358. assert(h->slice_type == P_TYPE || h->slice_type == SP_TYPE); //FIXME SP correct ?
  4359. for(i=0; i<4; i++){
  4360. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  4361. if(h->sub_mb_type[i] >=4){
  4362. av_log(h->s.avctx, AV_LOG_ERROR, "P sub_mb_type %u out of range at %d %d\n", h->sub_mb_type[i], s->mb_x, s->mb_y);
  4363. return -1;
  4364. }
  4365. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4366. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4367. }
  4368. }
  4369. for(list=0; list<h->list_count; list++){
  4370. int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  4371. for(i=0; i<4; i++){
  4372. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  4373. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4374. unsigned int tmp = get_te0_golomb(&s->gb, ref_count); //FIXME init to 0 before and skip?
  4375. if(tmp>=ref_count){
  4376. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", tmp);
  4377. return -1;
  4378. }
  4379. ref[list][i]= tmp;
  4380. }else{
  4381. //FIXME
  4382. ref[list][i] = -1;
  4383. }
  4384. }
  4385. }
  4386. if(dct8x8_allowed)
  4387. dct8x8_allowed = get_dct8x8_allowed(h);
  4388. for(list=0; list<h->list_count; list++){
  4389. for(i=0; i<4; i++){
  4390. if(IS_DIRECT(h->sub_mb_type[i])) {
  4391. h->ref_cache[list][ scan8[4*i] ] = h->ref_cache[list][ scan8[4*i]+1 ];
  4392. continue;
  4393. }
  4394. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
  4395. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  4396. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4397. const int sub_mb_type= h->sub_mb_type[i];
  4398. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  4399. for(j=0; j<sub_partition_count[i]; j++){
  4400. int mx, my;
  4401. const int index= 4*i + block_width*j;
  4402. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  4403. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
  4404. mx += get_se_golomb(&s->gb);
  4405. my += get_se_golomb(&s->gb);
  4406. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4407. if(IS_SUB_8X8(sub_mb_type)){
  4408. mv_cache[ 1 ][0]=
  4409. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  4410. mv_cache[ 1 ][1]=
  4411. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  4412. }else if(IS_SUB_8X4(sub_mb_type)){
  4413. mv_cache[ 1 ][0]= mx;
  4414. mv_cache[ 1 ][1]= my;
  4415. }else if(IS_SUB_4X8(sub_mb_type)){
  4416. mv_cache[ 8 ][0]= mx;
  4417. mv_cache[ 8 ][1]= my;
  4418. }
  4419. mv_cache[ 0 ][0]= mx;
  4420. mv_cache[ 0 ][1]= my;
  4421. }
  4422. }else{
  4423. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  4424. p[0] = p[1]=
  4425. p[8] = p[9]= 0;
  4426. }
  4427. }
  4428. }
  4429. }else if(IS_DIRECT(mb_type)){
  4430. pred_direct_motion(h, &mb_type);
  4431. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  4432. }else{
  4433. int list, mx, my, i;
  4434. //FIXME we should set ref_idx_l? to 0 if we use that later ...
  4435. if(IS_16X16(mb_type)){
  4436. for(list=0; list<h->list_count; list++){
  4437. unsigned int val;
  4438. if(IS_DIR(mb_type, 0, list)){
  4439. val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4440. if(val >= h->ref_count[list]){
  4441. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  4442. return -1;
  4443. }
  4444. }else
  4445. val= LIST_NOT_USED&0xFF;
  4446. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
  4447. }
  4448. for(list=0; list<h->list_count; list++){
  4449. unsigned int val;
  4450. if(IS_DIR(mb_type, 0, list)){
  4451. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
  4452. mx += get_se_golomb(&s->gb);
  4453. my += get_se_golomb(&s->gb);
  4454. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4455. val= pack16to32(mx,my);
  4456. }else
  4457. val=0;
  4458. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, val, 4);
  4459. }
  4460. }
  4461. else if(IS_16X8(mb_type)){
  4462. for(list=0; list<h->list_count; list++){
  4463. for(i=0; i<2; i++){
  4464. unsigned int val;
  4465. if(IS_DIR(mb_type, i, list)){
  4466. val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4467. if(val >= h->ref_count[list]){
  4468. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  4469. return -1;
  4470. }
  4471. }else
  4472. val= LIST_NOT_USED&0xFF;
  4473. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);
  4474. }
  4475. }
  4476. for(list=0; list<h->list_count; list++){
  4477. for(i=0; i<2; i++){
  4478. unsigned int val;
  4479. if(IS_DIR(mb_type, i, list)){
  4480. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
  4481. mx += get_se_golomb(&s->gb);
  4482. my += get_se_golomb(&s->gb);
  4483. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4484. val= pack16to32(mx,my);
  4485. }else
  4486. val=0;
  4487. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 4);
  4488. }
  4489. }
  4490. }else{
  4491. assert(IS_8X16(mb_type));
  4492. for(list=0; list<h->list_count; list++){
  4493. for(i=0; i<2; i++){
  4494. unsigned int val;
  4495. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  4496. val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4497. if(val >= h->ref_count[list]){
  4498. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  4499. return -1;
  4500. }
  4501. }else
  4502. val= LIST_NOT_USED&0xFF;
  4503. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
  4504. }
  4505. }
  4506. for(list=0; list<h->list_count; list++){
  4507. for(i=0; i<2; i++){
  4508. unsigned int val;
  4509. if(IS_DIR(mb_type, i, list)){
  4510. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
  4511. mx += get_se_golomb(&s->gb);
  4512. my += get_se_golomb(&s->gb);
  4513. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4514. val= pack16to32(mx,my);
  4515. }else
  4516. val=0;
  4517. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 4);
  4518. }
  4519. }
  4520. }
  4521. }
  4522. if(IS_INTER(mb_type))
  4523. write_back_motion(h, mb_type);
  4524. if(!IS_INTRA16x16(mb_type)){
  4525. cbp= get_ue_golomb(&s->gb);
  4526. if(cbp > 47){
  4527. av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%u) at %d %d\n", cbp, s->mb_x, s->mb_y);
  4528. return -1;
  4529. }
  4530. if(IS_INTRA4x4(mb_type))
  4531. cbp= golomb_to_intra4x4_cbp[cbp];
  4532. else
  4533. cbp= golomb_to_inter_cbp[cbp];
  4534. }
  4535. h->cbp = cbp;
  4536. if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){
  4537. if(get_bits1(&s->gb))
  4538. mb_type |= MB_TYPE_8x8DCT;
  4539. }
  4540. s->current_picture.mb_type[mb_xy]= mb_type;
  4541. if(cbp || IS_INTRA16x16(mb_type)){
  4542. int i8x8, i4x4, chroma_idx;
  4543. int chroma_qp, dquant;
  4544. GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
  4545. const uint8_t *scan, *scan8x8, *dc_scan;
  4546. // fill_non_zero_count_cache(h);
  4547. if(IS_INTERLACED(mb_type)){
  4548. scan8x8= s->qscale ? h->field_scan8x8_cavlc : h->field_scan8x8_cavlc_q0;
  4549. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  4550. dc_scan= luma_dc_field_scan;
  4551. }else{
  4552. scan8x8= s->qscale ? h->zigzag_scan8x8_cavlc : h->zigzag_scan8x8_cavlc_q0;
  4553. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  4554. dc_scan= luma_dc_zigzag_scan;
  4555. }
  4556. dquant= get_se_golomb(&s->gb);
  4557. if( dquant > 25 || dquant < -26 ){
  4558. av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
  4559. return -1;
  4560. }
  4561. s->qscale += dquant;
  4562. if(((unsigned)s->qscale) > 51){
  4563. if(s->qscale<0) s->qscale+= 52;
  4564. else s->qscale-= 52;
  4565. }
  4566. h->chroma_qp= chroma_qp= get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  4567. if(IS_INTRA16x16(mb_type)){
  4568. if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, h->dequant4_coeff[0][s->qscale], 16) < 0){
  4569. return -1; //FIXME continue if partitioned and other return -1 too
  4570. }
  4571. assert((cbp&15) == 0 || (cbp&15) == 15);
  4572. if(cbp&15){
  4573. for(i8x8=0; i8x8<4; i8x8++){
  4574. for(i4x4=0; i4x4<4; i4x4++){
  4575. const int index= i4x4 + 4*i8x8;
  4576. if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 ){
  4577. return -1;
  4578. }
  4579. }
  4580. }
  4581. }else{
  4582. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  4583. }
  4584. }else{
  4585. for(i8x8=0; i8x8<4; i8x8++){
  4586. if(cbp & (1<<i8x8)){
  4587. if(IS_8x8DCT(mb_type)){
  4588. DCTELEM *buf = &h->mb[64*i8x8];
  4589. uint8_t *nnz;
  4590. for(i4x4=0; i4x4<4; i4x4++){
  4591. if( decode_residual(h, gb, buf, i4x4+4*i8x8, scan8x8+16*i4x4,
  4592. h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 16) <0 )
  4593. return -1;
  4594. }
  4595. nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4596. nnz[0] += nnz[1] + nnz[8] + nnz[9];
  4597. }else{
  4598. for(i4x4=0; i4x4<4; i4x4++){
  4599. const int index= i4x4 + 4*i8x8;
  4600. if( decode_residual(h, gb, h->mb + 16*index, index, scan, h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale], 16) <0 ){
  4601. return -1;
  4602. }
  4603. }
  4604. }
  4605. }else{
  4606. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4607. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  4608. }
  4609. }
  4610. }
  4611. if(cbp&0x30){
  4612. for(chroma_idx=0; chroma_idx<2; chroma_idx++)
  4613. if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, NULL, 4) < 0){
  4614. return -1;
  4615. }
  4616. }
  4617. if(cbp&0x20){
  4618. for(chroma_idx=0; chroma_idx<2; chroma_idx++){
  4619. for(i4x4=0; i4x4<4; i4x4++){
  4620. const int index= 16 + 4*chroma_idx + i4x4;
  4621. if( decode_residual(h, gb, h->mb + 16*index, index, scan + 1, h->dequant4_coeff[chroma_idx+1+(IS_INTRA( mb_type ) ? 0:3)][chroma_qp], 15) < 0){
  4622. return -1;
  4623. }
  4624. }
  4625. }
  4626. }else{
  4627. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4628. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4629. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4630. }
  4631. }else{
  4632. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4633. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  4634. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4635. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4636. }
  4637. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4638. write_back_non_zero_count(h);
  4639. if(MB_MBAFF){
  4640. h->ref_count[0] >>= 1;
  4641. h->ref_count[1] >>= 1;
  4642. }
  4643. return 0;
  4644. }
  4645. static int decode_cabac_field_decoding_flag(H264Context *h) {
  4646. MpegEncContext * const s = &h->s;
  4647. const int mb_x = s->mb_x;
  4648. const int mb_y = s->mb_y & ~1;
  4649. const int mba_xy = mb_x - 1 + mb_y *s->mb_stride;
  4650. const int mbb_xy = mb_x + (mb_y-2)*s->mb_stride;
  4651. unsigned int ctx = 0;
  4652. if( h->slice_table[mba_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) ) {
  4653. ctx += 1;
  4654. }
  4655. if( h->slice_table[mbb_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) ) {
  4656. ctx += 1;
  4657. }
  4658. return get_cabac_noinline( &h->cabac, &h->cabac_state[70 + ctx] );
  4659. }
  4660. static int decode_cabac_intra_mb_type(H264Context *h, int ctx_base, int intra_slice) {
  4661. uint8_t *state= &h->cabac_state[ctx_base];
  4662. int mb_type;
  4663. if(intra_slice){
  4664. MpegEncContext * const s = &h->s;
  4665. const int mba_xy = h->left_mb_xy[0];
  4666. const int mbb_xy = h->top_mb_xy;
  4667. int ctx=0;
  4668. if( h->slice_table[mba_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mba_xy] ) )
  4669. ctx++;
  4670. if( h->slice_table[mbb_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mbb_xy] ) )
  4671. ctx++;
  4672. if( get_cabac_noinline( &h->cabac, &state[ctx] ) == 0 )
  4673. return 0; /* I4x4 */
  4674. state += 2;
  4675. }else{
  4676. if( get_cabac_noinline( &h->cabac, &state[0] ) == 0 )
  4677. return 0; /* I4x4 */
  4678. }
  4679. if( get_cabac_terminate( &h->cabac ) )
  4680. return 25; /* PCM */
  4681. mb_type = 1; /* I16x16 */
  4682. mb_type += 12 * get_cabac_noinline( &h->cabac, &state[1] ); /* cbp_luma != 0 */
  4683. if( get_cabac_noinline( &h->cabac, &state[2] ) ) /* cbp_chroma */
  4684. mb_type += 4 + 4 * get_cabac_noinline( &h->cabac, &state[2+intra_slice] );
  4685. mb_type += 2 * get_cabac_noinline( &h->cabac, &state[3+intra_slice] );
  4686. mb_type += 1 * get_cabac_noinline( &h->cabac, &state[3+2*intra_slice] );
  4687. return mb_type;
  4688. }
  4689. static int decode_cabac_mb_type( H264Context *h ) {
  4690. MpegEncContext * const s = &h->s;
  4691. if( h->slice_type == I_TYPE ) {
  4692. return decode_cabac_intra_mb_type(h, 3, 1);
  4693. } else if( h->slice_type == P_TYPE ) {
  4694. if( get_cabac_noinline( &h->cabac, &h->cabac_state[14] ) == 0 ) {
  4695. /* P-type */
  4696. if( get_cabac_noinline( &h->cabac, &h->cabac_state[15] ) == 0 ) {
  4697. /* P_L0_D16x16, P_8x8 */
  4698. return 3 * get_cabac_noinline( &h->cabac, &h->cabac_state[16] );
  4699. } else {
  4700. /* P_L0_D8x16, P_L0_D16x8 */
  4701. return 2 - get_cabac_noinline( &h->cabac, &h->cabac_state[17] );
  4702. }
  4703. } else {
  4704. return decode_cabac_intra_mb_type(h, 17, 0) + 5;
  4705. }
  4706. } else if( h->slice_type == B_TYPE ) {
  4707. const int mba_xy = h->left_mb_xy[0];
  4708. const int mbb_xy = h->top_mb_xy;
  4709. int ctx = 0;
  4710. int bits;
  4711. if( h->slice_table[mba_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mba_xy] ) )
  4712. ctx++;
  4713. if( h->slice_table[mbb_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mbb_xy] ) )
  4714. ctx++;
  4715. if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+ctx] ) )
  4716. return 0; /* B_Direct_16x16 */
  4717. if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+3] ) ) {
  4718. return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ); /* B_L[01]_16x16 */
  4719. }
  4720. bits = get_cabac_noinline( &h->cabac, &h->cabac_state[27+4] ) << 3;
  4721. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 2;
  4722. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 1;
  4723. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
  4724. if( bits < 8 )
  4725. return bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */
  4726. else if( bits == 13 ) {
  4727. return decode_cabac_intra_mb_type(h, 32, 0) + 23;
  4728. } else if( bits == 14 )
  4729. return 11; /* B_L1_L0_8x16 */
  4730. else if( bits == 15 )
  4731. return 22; /* B_8x8 */
  4732. bits= ( bits<<1 ) | get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
  4733. return bits - 4; /* B_L0_Bi_* through B_Bi_Bi_* */
  4734. } else {
  4735. /* TODO SI/SP frames? */
  4736. return -1;
  4737. }
  4738. }
  4739. static int decode_cabac_mb_skip( H264Context *h, int mb_x, int mb_y ) {
  4740. MpegEncContext * const s = &h->s;
  4741. int mba_xy, mbb_xy;
  4742. int ctx = 0;
  4743. if(FRAME_MBAFF){ //FIXME merge with the stuff in fill_caches?
  4744. int mb_xy = mb_x + (mb_y&~1)*s->mb_stride;
  4745. mba_xy = mb_xy - 1;
  4746. if( (mb_y&1)
  4747. && h->slice_table[mba_xy] == h->slice_num
  4748. && MB_FIELD == !!IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) )
  4749. mba_xy += s->mb_stride;
  4750. if( MB_FIELD ){
  4751. mbb_xy = mb_xy - s->mb_stride;
  4752. if( !(mb_y&1)
  4753. && h->slice_table[mbb_xy] == h->slice_num
  4754. && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) )
  4755. mbb_xy -= s->mb_stride;
  4756. }else
  4757. mbb_xy = mb_x + (mb_y-1)*s->mb_stride;
  4758. }else{
  4759. int mb_xy = mb_x + mb_y*s->mb_stride;
  4760. mba_xy = mb_xy - 1;
  4761. mbb_xy = mb_xy - s->mb_stride;
  4762. }
  4763. if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] ))
  4764. ctx++;
  4765. if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] ))
  4766. ctx++;
  4767. if( h->slice_type == B_TYPE )
  4768. ctx += 13;
  4769. return get_cabac_noinline( &h->cabac, &h->cabac_state[11+ctx] );
  4770. }
  4771. static int decode_cabac_mb_intra4x4_pred_mode( H264Context *h, int pred_mode ) {
  4772. int mode = 0;
  4773. if( get_cabac( &h->cabac, &h->cabac_state[68] ) )
  4774. return pred_mode;
  4775. mode += 1 * get_cabac( &h->cabac, &h->cabac_state[69] );
  4776. mode += 2 * get_cabac( &h->cabac, &h->cabac_state[69] );
  4777. mode += 4 * get_cabac( &h->cabac, &h->cabac_state[69] );
  4778. if( mode >= pred_mode )
  4779. return mode + 1;
  4780. else
  4781. return mode;
  4782. }
  4783. static int decode_cabac_mb_chroma_pre_mode( H264Context *h) {
  4784. const int mba_xy = h->left_mb_xy[0];
  4785. const int mbb_xy = h->top_mb_xy;
  4786. int ctx = 0;
  4787. /* No need to test for IS_INTRA4x4 and IS_INTRA16x16, as we set chroma_pred_mode_table to 0 */
  4788. if( h->slice_table[mba_xy] == h->slice_num && h->chroma_pred_mode_table[mba_xy] != 0 )
  4789. ctx++;
  4790. if( h->slice_table[mbb_xy] == h->slice_num && h->chroma_pred_mode_table[mbb_xy] != 0 )
  4791. ctx++;
  4792. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+ctx] ) == 0 )
  4793. return 0;
  4794. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  4795. return 1;
  4796. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  4797. return 2;
  4798. else
  4799. return 3;
  4800. }
  4801. static const uint8_t block_idx_x[16] = {
  4802. 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
  4803. };
  4804. static const uint8_t block_idx_y[16] = {
  4805. 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
  4806. };
  4807. static const uint8_t block_idx_xy[4][4] = {
  4808. { 0, 2, 8, 10},
  4809. { 1, 3, 9, 11},
  4810. { 4, 6, 12, 14},
  4811. { 5, 7, 13, 15}
  4812. };
  4813. static int decode_cabac_mb_cbp_luma( H264Context *h) {
  4814. int cbp = 0;
  4815. int cbp_b = -1;
  4816. int i8x8;
  4817. if( h->slice_table[h->top_mb_xy] == h->slice_num ) {
  4818. cbp_b = h->top_cbp;
  4819. tprintf(h->s.avctx, "cbp_b = top_cbp = %x\n", cbp_b);
  4820. }
  4821. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  4822. int cbp_a = -1;
  4823. int x, y;
  4824. int ctx = 0;
  4825. x = block_idx_x[4*i8x8];
  4826. y = block_idx_y[4*i8x8];
  4827. if( x > 0 )
  4828. cbp_a = cbp;
  4829. else if( h->slice_table[h->left_mb_xy[0]] == h->slice_num ) {
  4830. cbp_a = h->left_cbp;
  4831. tprintf(h->s.avctx, "cbp_a = left_cbp = %x\n", cbp_a);
  4832. }
  4833. if( y > 0 )
  4834. cbp_b = cbp;
  4835. /* No need to test for skip as we put 0 for skip block */
  4836. /* No need to test for IPCM as we put 1 for IPCM block */
  4837. if( cbp_a >= 0 ) {
  4838. int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
  4839. if( ((cbp_a >> i8x8a)&0x01) == 0 )
  4840. ctx++;
  4841. }
  4842. if( cbp_b >= 0 ) {
  4843. int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
  4844. if( ((cbp_b >> i8x8b)&0x01) == 0 )
  4845. ctx += 2;
  4846. }
  4847. if( get_cabac( &h->cabac, &h->cabac_state[73 + ctx] ) ) {
  4848. cbp |= 1 << i8x8;
  4849. }
  4850. }
  4851. return cbp;
  4852. }
  4853. static int decode_cabac_mb_cbp_chroma( H264Context *h) {
  4854. int ctx;
  4855. int cbp_a, cbp_b;
  4856. cbp_a = (h->left_cbp>>4)&0x03;
  4857. cbp_b = (h-> top_cbp>>4)&0x03;
  4858. ctx = 0;
  4859. if( cbp_a > 0 ) ctx++;
  4860. if( cbp_b > 0 ) ctx += 2;
  4861. if( get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] ) == 0 )
  4862. return 0;
  4863. ctx = 4;
  4864. if( cbp_a == 2 ) ctx++;
  4865. if( cbp_b == 2 ) ctx += 2;
  4866. return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] );
  4867. }
  4868. static int decode_cabac_mb_dqp( H264Context *h) {
  4869. MpegEncContext * const s = &h->s;
  4870. int mbn_xy;
  4871. int ctx = 0;
  4872. int val = 0;
  4873. if( s->mb_x > 0 )
  4874. mbn_xy = s->mb_x + s->mb_y*s->mb_stride - 1;
  4875. else
  4876. mbn_xy = s->mb_width - 1 + (s->mb_y-1)*s->mb_stride;
  4877. if( h->last_qscale_diff != 0 )
  4878. ctx++;
  4879. while( get_cabac_noinline( &h->cabac, &h->cabac_state[60 + ctx] ) ) {
  4880. if( ctx < 2 )
  4881. ctx = 2;
  4882. else
  4883. ctx = 3;
  4884. val++;
  4885. if(val > 102) //prevent infinite loop
  4886. return INT_MIN;
  4887. }
  4888. if( val&0x01 )
  4889. return (val + 1)/2;
  4890. else
  4891. return -(val + 1)/2;
  4892. }
  4893. static int decode_cabac_p_mb_sub_type( H264Context *h ) {
  4894. if( get_cabac( &h->cabac, &h->cabac_state[21] ) )
  4895. return 0; /* 8x8 */
  4896. if( !get_cabac( &h->cabac, &h->cabac_state[22] ) )
  4897. return 1; /* 8x4 */
  4898. if( get_cabac( &h->cabac, &h->cabac_state[23] ) )
  4899. return 2; /* 4x8 */
  4900. return 3; /* 4x4 */
  4901. }
  4902. static int decode_cabac_b_mb_sub_type( H264Context *h ) {
  4903. int type;
  4904. if( !get_cabac( &h->cabac, &h->cabac_state[36] ) )
  4905. return 0; /* B_Direct_8x8 */
  4906. if( !get_cabac( &h->cabac, &h->cabac_state[37] ) )
  4907. return 1 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L0_8x8, B_L1_8x8 */
  4908. type = 3;
  4909. if( get_cabac( &h->cabac, &h->cabac_state[38] ) ) {
  4910. if( get_cabac( &h->cabac, &h->cabac_state[39] ) )
  4911. return 11 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L1_4x4, B_Bi_4x4 */
  4912. type += 4;
  4913. }
  4914. type += 2*get_cabac( &h->cabac, &h->cabac_state[39] );
  4915. type += get_cabac( &h->cabac, &h->cabac_state[39] );
  4916. return type;
  4917. }
  4918. static inline int decode_cabac_mb_transform_size( H264Context *h ) {
  4919. return get_cabac_noinline( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] );
  4920. }
  4921. static int decode_cabac_mb_ref( H264Context *h, int list, int n ) {
  4922. int refa = h->ref_cache[list][scan8[n] - 1];
  4923. int refb = h->ref_cache[list][scan8[n] - 8];
  4924. int ref = 0;
  4925. int ctx = 0;
  4926. if( h->slice_type == B_TYPE) {
  4927. if( refa > 0 && !h->direct_cache[scan8[n] - 1] )
  4928. ctx++;
  4929. if( refb > 0 && !h->direct_cache[scan8[n] - 8] )
  4930. ctx += 2;
  4931. } else {
  4932. if( refa > 0 )
  4933. ctx++;
  4934. if( refb > 0 )
  4935. ctx += 2;
  4936. }
  4937. while( get_cabac( &h->cabac, &h->cabac_state[54+ctx] ) ) {
  4938. ref++;
  4939. if( ctx < 4 )
  4940. ctx = 4;
  4941. else
  4942. ctx = 5;
  4943. if(ref >= 32 /*h->ref_list[list]*/){
  4944. av_log(h->s.avctx, AV_LOG_ERROR, "overflow in decode_cabac_mb_ref\n");
  4945. return 0; //FIXME we should return -1 and check the return everywhere
  4946. }
  4947. }
  4948. return ref;
  4949. }
  4950. static int decode_cabac_mb_mvd( H264Context *h, int list, int n, int l ) {
  4951. int amvd = abs( h->mvd_cache[list][scan8[n] - 1][l] ) +
  4952. abs( h->mvd_cache[list][scan8[n] - 8][l] );
  4953. int ctxbase = (l == 0) ? 40 : 47;
  4954. int ctx, mvd;
  4955. if( amvd < 3 )
  4956. ctx = 0;
  4957. else if( amvd > 32 )
  4958. ctx = 2;
  4959. else
  4960. ctx = 1;
  4961. if(!get_cabac(&h->cabac, &h->cabac_state[ctxbase+ctx]))
  4962. return 0;
  4963. mvd= 1;
  4964. ctx= 3;
  4965. while( mvd < 9 && get_cabac( &h->cabac, &h->cabac_state[ctxbase+ctx] ) ) {
  4966. mvd++;
  4967. if( ctx < 6 )
  4968. ctx++;
  4969. }
  4970. if( mvd >= 9 ) {
  4971. int k = 3;
  4972. while( get_cabac_bypass( &h->cabac ) ) {
  4973. mvd += 1 << k;
  4974. k++;
  4975. if(k>24){
  4976. av_log(h->s.avctx, AV_LOG_ERROR, "overflow in decode_cabac_mb_mvd\n");
  4977. return INT_MIN;
  4978. }
  4979. }
  4980. while( k-- ) {
  4981. if( get_cabac_bypass( &h->cabac ) )
  4982. mvd += 1 << k;
  4983. }
  4984. }
  4985. return get_cabac_bypass_sign( &h->cabac, -mvd );
  4986. }
  4987. static inline int get_cabac_cbf_ctx( H264Context *h, int cat, int idx ) {
  4988. int nza, nzb;
  4989. int ctx = 0;
  4990. if( cat == 0 ) {
  4991. nza = h->left_cbp&0x100;
  4992. nzb = h-> top_cbp&0x100;
  4993. } else if( cat == 1 || cat == 2 ) {
  4994. nza = h->non_zero_count_cache[scan8[idx] - 1];
  4995. nzb = h->non_zero_count_cache[scan8[idx] - 8];
  4996. } else if( cat == 3 ) {
  4997. nza = (h->left_cbp>>(6+idx))&0x01;
  4998. nzb = (h-> top_cbp>>(6+idx))&0x01;
  4999. } else {
  5000. assert(cat == 4);
  5001. nza = h->non_zero_count_cache[scan8[16+idx] - 1];
  5002. nzb = h->non_zero_count_cache[scan8[16+idx] - 8];
  5003. }
  5004. if( nza > 0 )
  5005. ctx++;
  5006. if( nzb > 0 )
  5007. ctx += 2;
  5008. return ctx + 4 * cat;
  5009. }
  5010. static const __attribute((used)) uint8_t last_coeff_flag_offset_8x8[63] = {
  5011. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  5012. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  5013. 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  5014. 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
  5015. };
  5016. static int decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff) {
  5017. const int mb_xy = h->s.mb_x + h->s.mb_y*h->s.mb_stride;
  5018. static const int significant_coeff_flag_offset[2][6] = {
  5019. { 105+0, 105+15, 105+29, 105+44, 105+47, 402 },
  5020. { 277+0, 277+15, 277+29, 277+44, 277+47, 436 }
  5021. };
  5022. static const int last_coeff_flag_offset[2][6] = {
  5023. { 166+0, 166+15, 166+29, 166+44, 166+47, 417 },
  5024. { 338+0, 338+15, 338+29, 338+44, 338+47, 451 }
  5025. };
  5026. static const int coeff_abs_level_m1_offset[6] = {
  5027. 227+0, 227+10, 227+20, 227+30, 227+39, 426
  5028. };
  5029. static const uint8_t significant_coeff_flag_offset_8x8[2][63] = {
  5030. { 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
  5031. 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
  5032. 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
  5033. 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12 },
  5034. { 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
  5035. 6, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11,
  5036. 9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,
  5037. 9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14 }
  5038. };
  5039. int index[64];
  5040. int last;
  5041. int coeff_count = 0;
  5042. int abslevel1 = 1;
  5043. int abslevelgt1 = 0;
  5044. uint8_t *significant_coeff_ctx_base;
  5045. uint8_t *last_coeff_ctx_base;
  5046. uint8_t *abs_level_m1_ctx_base;
  5047. #ifndef ARCH_X86
  5048. #define CABAC_ON_STACK
  5049. #endif
  5050. #ifdef CABAC_ON_STACK
  5051. #define CC &cc
  5052. CABACContext cc;
  5053. cc.range = h->cabac.range;
  5054. cc.low = h->cabac.low;
  5055. cc.bytestream= h->cabac.bytestream;
  5056. #else
  5057. #define CC &h->cabac
  5058. #endif
  5059. /* cat: 0-> DC 16x16 n = 0
  5060. * 1-> AC 16x16 n = luma4x4idx
  5061. * 2-> Luma4x4 n = luma4x4idx
  5062. * 3-> DC Chroma n = iCbCr
  5063. * 4-> AC Chroma n = 4 * iCbCr + chroma4x4idx
  5064. * 5-> Luma8x8 n = 4 * luma8x8idx
  5065. */
  5066. /* read coded block flag */
  5067. if( cat != 5 ) {
  5068. if( get_cabac( CC, &h->cabac_state[85 + get_cabac_cbf_ctx( h, cat, n ) ] ) == 0 ) {
  5069. if( cat == 1 || cat == 2 )
  5070. h->non_zero_count_cache[scan8[n]] = 0;
  5071. else if( cat == 4 )
  5072. h->non_zero_count_cache[scan8[16+n]] = 0;
  5073. #ifdef CABAC_ON_STACK
  5074. h->cabac.range = cc.range ;
  5075. h->cabac.low = cc.low ;
  5076. h->cabac.bytestream= cc.bytestream;
  5077. #endif
  5078. return 0;
  5079. }
  5080. }
  5081. significant_coeff_ctx_base = h->cabac_state
  5082. + significant_coeff_flag_offset[MB_FIELD][cat];
  5083. last_coeff_ctx_base = h->cabac_state
  5084. + last_coeff_flag_offset[MB_FIELD][cat];
  5085. abs_level_m1_ctx_base = h->cabac_state
  5086. + coeff_abs_level_m1_offset[cat];
  5087. if( cat == 5 ) {
  5088. #define DECODE_SIGNIFICANCE( coefs, sig_off, last_off ) \
  5089. for(last= 0; last < coefs; last++) { \
  5090. uint8_t *sig_ctx = significant_coeff_ctx_base + sig_off; \
  5091. if( get_cabac( CC, sig_ctx )) { \
  5092. uint8_t *last_ctx = last_coeff_ctx_base + last_off; \
  5093. index[coeff_count++] = last; \
  5094. if( get_cabac( CC, last_ctx ) ) { \
  5095. last= max_coeff; \
  5096. break; \
  5097. } \
  5098. } \
  5099. }\
  5100. if( last == max_coeff -1 ) {\
  5101. index[coeff_count++] = last;\
  5102. }
  5103. const uint8_t *sig_off = significant_coeff_flag_offset_8x8[MB_FIELD];
  5104. #if defined(ARCH_X86) && defined(CONFIG_7REGS) && defined(HAVE_EBX_AVAILABLE) && !defined(BROKEN_RELOCATIONS)
  5105. coeff_count= decode_significance_8x8_x86(CC, significant_coeff_ctx_base, index, sig_off);
  5106. } else {
  5107. coeff_count= decode_significance_x86(CC, max_coeff, significant_coeff_ctx_base, index);
  5108. #else
  5109. DECODE_SIGNIFICANCE( 63, sig_off[last], last_coeff_flag_offset_8x8[last] );
  5110. } else {
  5111. DECODE_SIGNIFICANCE( max_coeff - 1, last, last );
  5112. #endif
  5113. }
  5114. assert(coeff_count > 0);
  5115. if( cat == 0 )
  5116. h->cbp_table[mb_xy] |= 0x100;
  5117. else if( cat == 1 || cat == 2 )
  5118. h->non_zero_count_cache[scan8[n]] = coeff_count;
  5119. else if( cat == 3 )
  5120. h->cbp_table[mb_xy] |= 0x40 << n;
  5121. else if( cat == 4 )
  5122. h->non_zero_count_cache[scan8[16+n]] = coeff_count;
  5123. else {
  5124. assert( cat == 5 );
  5125. fill_rectangle(&h->non_zero_count_cache[scan8[n]], 2, 2, 8, coeff_count, 1);
  5126. }
  5127. for( coeff_count--; coeff_count >= 0; coeff_count-- ) {
  5128. uint8_t *ctx = (abslevelgt1 != 0 ? 0 : FFMIN( 4, abslevel1 )) + abs_level_m1_ctx_base;
  5129. int j= scantable[index[coeff_count]];
  5130. if( get_cabac( CC, ctx ) == 0 ) {
  5131. if( !qmul ) {
  5132. block[j] = get_cabac_bypass_sign( CC, -1);
  5133. }else{
  5134. block[j] = (get_cabac_bypass_sign( CC, -qmul[j]) + 32) >> 6;;
  5135. }
  5136. abslevel1++;
  5137. } else {
  5138. int coeff_abs = 2;
  5139. ctx = 5 + FFMIN( 4, abslevelgt1 ) + abs_level_m1_ctx_base;
  5140. while( coeff_abs < 15 && get_cabac( CC, ctx ) ) {
  5141. coeff_abs++;
  5142. }
  5143. if( coeff_abs >= 15 ) {
  5144. int j = 0;
  5145. while( get_cabac_bypass( CC ) ) {
  5146. j++;
  5147. }
  5148. coeff_abs=1;
  5149. while( j-- ) {
  5150. coeff_abs += coeff_abs + get_cabac_bypass( CC );
  5151. }
  5152. coeff_abs+= 14;
  5153. }
  5154. if( !qmul ) {
  5155. if( get_cabac_bypass( CC ) ) block[j] = -coeff_abs;
  5156. else block[j] = coeff_abs;
  5157. }else{
  5158. if( get_cabac_bypass( CC ) ) block[j] = (-coeff_abs * qmul[j] + 32) >> 6;
  5159. else block[j] = ( coeff_abs * qmul[j] + 32) >> 6;
  5160. }
  5161. abslevelgt1++;
  5162. }
  5163. }
  5164. #ifdef CABAC_ON_STACK
  5165. h->cabac.range = cc.range ;
  5166. h->cabac.low = cc.low ;
  5167. h->cabac.bytestream= cc.bytestream;
  5168. #endif
  5169. return 0;
  5170. }
  5171. static inline void compute_mb_neighbors(H264Context *h)
  5172. {
  5173. MpegEncContext * const s = &h->s;
  5174. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  5175. h->top_mb_xy = mb_xy - s->mb_stride;
  5176. h->left_mb_xy[0] = mb_xy - 1;
  5177. if(FRAME_MBAFF){
  5178. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  5179. const int top_pair_xy = pair_xy - s->mb_stride;
  5180. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  5181. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  5182. const int curr_mb_frame_flag = !MB_FIELD;
  5183. const int bottom = (s->mb_y & 1);
  5184. if (bottom
  5185. ? !curr_mb_frame_flag // bottom macroblock
  5186. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  5187. ) {
  5188. h->top_mb_xy -= s->mb_stride;
  5189. }
  5190. if (left_mb_frame_flag != curr_mb_frame_flag) {
  5191. h->left_mb_xy[0] = pair_xy - 1;
  5192. }
  5193. }
  5194. return;
  5195. }
  5196. /**
  5197. * decodes a macroblock
  5198. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  5199. */
  5200. static int decode_mb_cabac(H264Context *h) {
  5201. MpegEncContext * const s = &h->s;
  5202. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  5203. int mb_type, partition_count, cbp = 0;
  5204. int dct8x8_allowed= h->pps.transform_8x8_mode;
  5205. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?)
  5206. tprintf(s->avctx, "pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  5207. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE ) {
  5208. int skip;
  5209. /* a skipped mb needs the aff flag from the following mb */
  5210. if( FRAME_MBAFF && s->mb_x==0 && (s->mb_y&1)==0 )
  5211. predict_field_decoding_flag(h);
  5212. if( FRAME_MBAFF && (s->mb_y&1)==1 && h->prev_mb_skipped )
  5213. skip = h->next_mb_skipped;
  5214. else
  5215. skip = decode_cabac_mb_skip( h, s->mb_x, s->mb_y );
  5216. /* read skip flags */
  5217. if( skip ) {
  5218. if( FRAME_MBAFF && (s->mb_y&1)==0 ){
  5219. s->current_picture.mb_type[mb_xy] = MB_TYPE_SKIP;
  5220. h->next_mb_skipped = decode_cabac_mb_skip( h, s->mb_x, s->mb_y+1 );
  5221. if(h->next_mb_skipped)
  5222. predict_field_decoding_flag(h);
  5223. else
  5224. h->mb_mbaff = h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  5225. }
  5226. decode_mb_skip(h);
  5227. h->cbp_table[mb_xy] = 0;
  5228. h->chroma_pred_mode_table[mb_xy] = 0;
  5229. h->last_qscale_diff = 0;
  5230. return 0;
  5231. }
  5232. }
  5233. if(FRAME_MBAFF){
  5234. if( (s->mb_y&1) == 0 )
  5235. h->mb_mbaff =
  5236. h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  5237. }else
  5238. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  5239. h->prev_mb_skipped = 0;
  5240. compute_mb_neighbors(h);
  5241. if( ( mb_type = decode_cabac_mb_type( h ) ) < 0 ) {
  5242. av_log( h->s.avctx, AV_LOG_ERROR, "decode_cabac_mb_type failed\n" );
  5243. return -1;
  5244. }
  5245. if( h->slice_type == B_TYPE ) {
  5246. if( mb_type < 23 ){
  5247. partition_count= b_mb_type_info[mb_type].partition_count;
  5248. mb_type= b_mb_type_info[mb_type].type;
  5249. }else{
  5250. mb_type -= 23;
  5251. goto decode_intra_mb;
  5252. }
  5253. } else if( h->slice_type == P_TYPE ) {
  5254. if( mb_type < 5) {
  5255. partition_count= p_mb_type_info[mb_type].partition_count;
  5256. mb_type= p_mb_type_info[mb_type].type;
  5257. } else {
  5258. mb_type -= 5;
  5259. goto decode_intra_mb;
  5260. }
  5261. } else {
  5262. assert(h->slice_type == I_TYPE);
  5263. decode_intra_mb:
  5264. partition_count = 0;
  5265. cbp= i_mb_type_info[mb_type].cbp;
  5266. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  5267. mb_type= i_mb_type_info[mb_type].type;
  5268. }
  5269. if(MB_FIELD)
  5270. mb_type |= MB_TYPE_INTERLACED;
  5271. h->slice_table[ mb_xy ]= h->slice_num;
  5272. if(IS_INTRA_PCM(mb_type)) {
  5273. const uint8_t *ptr;
  5274. unsigned int x, y;
  5275. // We assume these blocks are very rare so we dont optimize it.
  5276. // FIXME The two following lines get the bitstream position in the cabac
  5277. // decode, I think it should be done by a function in cabac.h (or cabac.c).
  5278. ptr= h->cabac.bytestream;
  5279. if(h->cabac.low&0x1) ptr--;
  5280. if(CABAC_BITS==16){
  5281. if(h->cabac.low&0x1FF) ptr--;
  5282. }
  5283. // The pixels are stored in the same order as levels in h->mb array.
  5284. for(y=0; y<16; y++){
  5285. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  5286. for(x=0; x<16; x++){
  5287. tprintf(s->avctx, "LUMA ICPM LEVEL (%3d)\n", *ptr);
  5288. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= *ptr++;
  5289. }
  5290. }
  5291. for(y=0; y<8; y++){
  5292. const int index= 256 + 4*(y&3) + 32*(y>>2);
  5293. for(x=0; x<8; x++){
  5294. tprintf(s->avctx, "CHROMA U ICPM LEVEL (%3d)\n", *ptr);
  5295. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  5296. }
  5297. }
  5298. for(y=0; y<8; y++){
  5299. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  5300. for(x=0; x<8; x++){
  5301. tprintf(s->avctx, "CHROMA V ICPM LEVEL (%3d)\n", *ptr);
  5302. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  5303. }
  5304. }
  5305. ff_init_cabac_decoder(&h->cabac, ptr, h->cabac.bytestream_end - ptr);
  5306. // All blocks are present
  5307. h->cbp_table[mb_xy] = 0x1ef;
  5308. h->chroma_pred_mode_table[mb_xy] = 0;
  5309. // In deblocking, the quantizer is 0
  5310. s->current_picture.qscale_table[mb_xy]= 0;
  5311. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
  5312. // All coeffs are present
  5313. memset(h->non_zero_count[mb_xy], 16, 16);
  5314. s->current_picture.mb_type[mb_xy]= mb_type;
  5315. return 0;
  5316. }
  5317. if(MB_MBAFF){
  5318. h->ref_count[0] <<= 1;
  5319. h->ref_count[1] <<= 1;
  5320. }
  5321. fill_caches(h, mb_type, 0);
  5322. if( IS_INTRA( mb_type ) ) {
  5323. int i, pred_mode;
  5324. if( IS_INTRA4x4( mb_type ) ) {
  5325. if( dct8x8_allowed && decode_cabac_mb_transform_size( h ) ) {
  5326. mb_type |= MB_TYPE_8x8DCT;
  5327. for( i = 0; i < 16; i+=4 ) {
  5328. int pred = pred_intra_mode( h, i );
  5329. int mode = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  5330. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  5331. }
  5332. } else {
  5333. for( i = 0; i < 16; i++ ) {
  5334. int pred = pred_intra_mode( h, i );
  5335. h->intra4x4_pred_mode_cache[ scan8[i] ] = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  5336. //av_log( s->avctx, AV_LOG_ERROR, "i4x4 pred=%d mode=%d\n", pred, h->intra4x4_pred_mode_cache[ scan8[i] ] );
  5337. }
  5338. }
  5339. write_back_intra_pred_mode(h);
  5340. if( check_intra4x4_pred_mode(h) < 0 ) return -1;
  5341. } else {
  5342. h->intra16x16_pred_mode= check_intra_pred_mode( h, h->intra16x16_pred_mode );
  5343. if( h->intra16x16_pred_mode < 0 ) return -1;
  5344. }
  5345. h->chroma_pred_mode_table[mb_xy] =
  5346. pred_mode = decode_cabac_mb_chroma_pre_mode( h );
  5347. pred_mode= check_intra_pred_mode( h, pred_mode );
  5348. if( pred_mode < 0 ) return -1;
  5349. h->chroma_pred_mode= pred_mode;
  5350. } else if( partition_count == 4 ) {
  5351. int i, j, sub_partition_count[4], list, ref[2][4];
  5352. if( h->slice_type == B_TYPE ) {
  5353. for( i = 0; i < 4; i++ ) {
  5354. h->sub_mb_type[i] = decode_cabac_b_mb_sub_type( h );
  5355. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5356. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5357. }
  5358. if( IS_DIRECT(h->sub_mb_type[0] | h->sub_mb_type[1] |
  5359. h->sub_mb_type[2] | h->sub_mb_type[3]) ) {
  5360. pred_direct_motion(h, &mb_type);
  5361. if( h->ref_count[0] > 1 || h->ref_count[1] > 1 ) {
  5362. for( i = 0; i < 4; i++ )
  5363. if( IS_DIRECT(h->sub_mb_type[i]) )
  5364. fill_rectangle( &h->direct_cache[scan8[4*i]], 2, 2, 8, 1, 1 );
  5365. }
  5366. }
  5367. } else {
  5368. for( i = 0; i < 4; i++ ) {
  5369. h->sub_mb_type[i] = decode_cabac_p_mb_sub_type( h );
  5370. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5371. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5372. }
  5373. }
  5374. for( list = 0; list < h->list_count; list++ ) {
  5375. for( i = 0; i < 4; i++ ) {
  5376. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  5377. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  5378. if( h->ref_count[list] > 1 )
  5379. ref[list][i] = decode_cabac_mb_ref( h, list, 4*i );
  5380. else
  5381. ref[list][i] = 0;
  5382. } else {
  5383. ref[list][i] = -1;
  5384. }
  5385. h->ref_cache[list][ scan8[4*i]+1 ]=
  5386. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  5387. }
  5388. }
  5389. if(dct8x8_allowed)
  5390. dct8x8_allowed = get_dct8x8_allowed(h);
  5391. for(list=0; list<h->list_count; list++){
  5392. for(i=0; i<4; i++){
  5393. if(IS_DIRECT(h->sub_mb_type[i])){
  5394. fill_rectangle(h->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 4);
  5395. continue;
  5396. }
  5397. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ];
  5398. if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
  5399. const int sub_mb_type= h->sub_mb_type[i];
  5400. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  5401. for(j=0; j<sub_partition_count[i]; j++){
  5402. int mpx, mpy;
  5403. int mx, my;
  5404. const int index= 4*i + block_width*j;
  5405. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  5406. int16_t (* mvd_cache)[2]= &h->mvd_cache[list][ scan8[index] ];
  5407. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mpx, &mpy);
  5408. mx = mpx + decode_cabac_mb_mvd( h, list, index, 0 );
  5409. my = mpy + decode_cabac_mb_mvd( h, list, index, 1 );
  5410. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  5411. if(IS_SUB_8X8(sub_mb_type)){
  5412. mv_cache[ 1 ][0]=
  5413. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  5414. mv_cache[ 1 ][1]=
  5415. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  5416. mvd_cache[ 1 ][0]=
  5417. mvd_cache[ 8 ][0]= mvd_cache[ 9 ][0]= mx - mpx;
  5418. mvd_cache[ 1 ][1]=
  5419. mvd_cache[ 8 ][1]= mvd_cache[ 9 ][1]= my - mpy;
  5420. }else if(IS_SUB_8X4(sub_mb_type)){
  5421. mv_cache[ 1 ][0]= mx;
  5422. mv_cache[ 1 ][1]= my;
  5423. mvd_cache[ 1 ][0]= mx - mpx;
  5424. mvd_cache[ 1 ][1]= my - mpy;
  5425. }else if(IS_SUB_4X8(sub_mb_type)){
  5426. mv_cache[ 8 ][0]= mx;
  5427. mv_cache[ 8 ][1]= my;
  5428. mvd_cache[ 8 ][0]= mx - mpx;
  5429. mvd_cache[ 8 ][1]= my - mpy;
  5430. }
  5431. mv_cache[ 0 ][0]= mx;
  5432. mv_cache[ 0 ][1]= my;
  5433. mvd_cache[ 0 ][0]= mx - mpx;
  5434. mvd_cache[ 0 ][1]= my - mpy;
  5435. }
  5436. }else{
  5437. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  5438. uint32_t *pd= (uint32_t *)&h->mvd_cache[list][ scan8[4*i] ][0];
  5439. p[0] = p[1] = p[8] = p[9] = 0;
  5440. pd[0]= pd[1]= pd[8]= pd[9]= 0;
  5441. }
  5442. }
  5443. }
  5444. } else if( IS_DIRECT(mb_type) ) {
  5445. pred_direct_motion(h, &mb_type);
  5446. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  5447. fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  5448. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  5449. } else {
  5450. int list, mx, my, i, mpx, mpy;
  5451. if(IS_16X16(mb_type)){
  5452. for(list=0; list<h->list_count; list++){
  5453. if(IS_DIR(mb_type, 0, list)){
  5454. const int ref = h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 0 ) : 0;
  5455. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1);
  5456. }else
  5457. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1); //FIXME factorize and the other fill_rect below too
  5458. }
  5459. for(list=0; list<h->list_count; list++){
  5460. if(IS_DIR(mb_type, 0, list)){
  5461. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mpx, &mpy);
  5462. mx = mpx + decode_cabac_mb_mvd( h, list, 0, 0 );
  5463. my = mpy + decode_cabac_mb_mvd( h, list, 0, 1 );
  5464. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  5465. fill_rectangle(h->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5466. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  5467. }else
  5468. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  5469. }
  5470. }
  5471. else if(IS_16X8(mb_type)){
  5472. for(list=0; list<h->list_count; list++){
  5473. for(i=0; i<2; i++){
  5474. if(IS_DIR(mb_type, i, list)){
  5475. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 8*i ) : 0;
  5476. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, ref, 1);
  5477. }else
  5478. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  5479. }
  5480. }
  5481. for(list=0; list<h->list_count; list++){
  5482. for(i=0; i<2; i++){
  5483. if(IS_DIR(mb_type, i, list)){
  5484. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mpx, &mpy);
  5485. mx = mpx + decode_cabac_mb_mvd( h, list, 8*i, 0 );
  5486. my = mpy + decode_cabac_mb_mvd( h, list, 8*i, 1 );
  5487. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  5488. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx-mpx,my-mpy), 4);
  5489. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  5490. }else{
  5491. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5492. fill_rectangle(h-> mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5493. }
  5494. }
  5495. }
  5496. }else{
  5497. assert(IS_8X16(mb_type));
  5498. for(list=0; list<h->list_count; list++){
  5499. for(i=0; i<2; i++){
  5500. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  5501. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 4*i ) : 0;
  5502. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, ref, 1);
  5503. }else
  5504. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  5505. }
  5506. }
  5507. for(list=0; list<h->list_count; list++){
  5508. for(i=0; i<2; i++){
  5509. if(IS_DIR(mb_type, i, list)){
  5510. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mpx, &mpy);
  5511. mx = mpx + decode_cabac_mb_mvd( h, list, 4*i, 0 );
  5512. my = mpy + decode_cabac_mb_mvd( h, list, 4*i, 1 );
  5513. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  5514. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5515. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  5516. }else{
  5517. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5518. fill_rectangle(h-> mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5519. }
  5520. }
  5521. }
  5522. }
  5523. }
  5524. if( IS_INTER( mb_type ) ) {
  5525. h->chroma_pred_mode_table[mb_xy] = 0;
  5526. write_back_motion( h, mb_type );
  5527. }
  5528. if( !IS_INTRA16x16( mb_type ) ) {
  5529. cbp = decode_cabac_mb_cbp_luma( h );
  5530. cbp |= decode_cabac_mb_cbp_chroma( h ) << 4;
  5531. }
  5532. h->cbp_table[mb_xy] = h->cbp = cbp;
  5533. if( dct8x8_allowed && (cbp&15) && !IS_INTRA( mb_type ) ) {
  5534. if( decode_cabac_mb_transform_size( h ) )
  5535. mb_type |= MB_TYPE_8x8DCT;
  5536. }
  5537. s->current_picture.mb_type[mb_xy]= mb_type;
  5538. if( cbp || IS_INTRA16x16( mb_type ) ) {
  5539. const uint8_t *scan, *scan8x8, *dc_scan;
  5540. int dqp;
  5541. if(IS_INTERLACED(mb_type)){
  5542. scan8x8= s->qscale ? h->field_scan8x8 : h->field_scan8x8_q0;
  5543. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  5544. dc_scan= luma_dc_field_scan;
  5545. }else{
  5546. scan8x8= s->qscale ? h->zigzag_scan8x8 : h->zigzag_scan8x8_q0;
  5547. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  5548. dc_scan= luma_dc_zigzag_scan;
  5549. }
  5550. h->last_qscale_diff = dqp = decode_cabac_mb_dqp( h );
  5551. if( dqp == INT_MIN ){
  5552. av_log(h->s.avctx, AV_LOG_ERROR, "cabac decode of qscale diff failed at %d %d\n", s->mb_x, s->mb_y);
  5553. return -1;
  5554. }
  5555. s->qscale += dqp;
  5556. if(((unsigned)s->qscale) > 51){
  5557. if(s->qscale<0) s->qscale+= 52;
  5558. else s->qscale-= 52;
  5559. }
  5560. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  5561. if( IS_INTRA16x16( mb_type ) ) {
  5562. int i;
  5563. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 DC\n" );
  5564. if( decode_cabac_residual( h, h->mb, 0, 0, dc_scan, NULL, 16) < 0)
  5565. return -1;
  5566. if( cbp&15 ) {
  5567. for( i = 0; i < 16; i++ ) {
  5568. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 AC:%d\n", i );
  5569. if( decode_cabac_residual(h, h->mb + 16*i, 1, i, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 )
  5570. return -1;
  5571. }
  5572. } else {
  5573. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  5574. }
  5575. } else {
  5576. int i8x8, i4x4;
  5577. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  5578. if( cbp & (1<<i8x8) ) {
  5579. if( IS_8x8DCT(mb_type) ) {
  5580. if( decode_cabac_residual(h, h->mb + 64*i8x8, 5, 4*i8x8,
  5581. scan8x8, h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 64) < 0 )
  5582. return -1;
  5583. } else
  5584. for( i4x4 = 0; i4x4 < 4; i4x4++ ) {
  5585. const int index = 4*i8x8 + i4x4;
  5586. //av_log( s->avctx, AV_LOG_ERROR, "Luma4x4: %d\n", index );
  5587. //START_TIMER
  5588. if( decode_cabac_residual(h, h->mb + 16*index, 2, index, scan, h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale], 16) < 0 )
  5589. return -1;
  5590. //STOP_TIMER("decode_residual")
  5591. }
  5592. } else {
  5593. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  5594. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  5595. }
  5596. }
  5597. }
  5598. if( cbp&0x30 ){
  5599. int c;
  5600. for( c = 0; c < 2; c++ ) {
  5601. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-DC\n",c );
  5602. if( decode_cabac_residual(h, h->mb + 256 + 16*4*c, 3, c, chroma_dc_scan, NULL, 4) < 0)
  5603. return -1;
  5604. }
  5605. }
  5606. if( cbp&0x20 ) {
  5607. int c, i;
  5608. for( c = 0; c < 2; c++ ) {
  5609. for( i = 0; i < 4; i++ ) {
  5610. const int index = 16 + 4 * c + i;
  5611. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-AC %d\n",c, index - 16 );
  5612. if( decode_cabac_residual(h, h->mb + 16*index, 4, index - 16, scan + 1, h->dequant4_coeff[c+1+(IS_INTRA( mb_type ) ? 0:3)][h->chroma_qp], 15) < 0)
  5613. return -1;
  5614. }
  5615. }
  5616. } else {
  5617. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5618. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5619. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5620. }
  5621. } else {
  5622. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5623. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  5624. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5625. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5626. h->last_qscale_diff = 0;
  5627. }
  5628. s->current_picture.qscale_table[mb_xy]= s->qscale;
  5629. write_back_non_zero_count(h);
  5630. if(MB_MBAFF){
  5631. h->ref_count[0] >>= 1;
  5632. h->ref_count[1] >>= 1;
  5633. }
  5634. return 0;
  5635. }
  5636. static void filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5637. int i, d;
  5638. const int index_a = qp + h->slice_alpha_c0_offset;
  5639. const int alpha = (alpha_table+52)[index_a];
  5640. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5641. if( bS[0] < 4 ) {
  5642. int8_t tc[4];
  5643. for(i=0; i<4; i++)
  5644. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] : -1;
  5645. h->s.dsp.h264_h_loop_filter_luma(pix, stride, alpha, beta, tc);
  5646. } else {
  5647. /* 16px edge length, because bS=4 is triggered by being at
  5648. * the edge of an intra MB, so all 4 bS are the same */
  5649. for( d = 0; d < 16; d++ ) {
  5650. const int p0 = pix[-1];
  5651. const int p1 = pix[-2];
  5652. const int p2 = pix[-3];
  5653. const int q0 = pix[0];
  5654. const int q1 = pix[1];
  5655. const int q2 = pix[2];
  5656. if( FFABS( p0 - q0 ) < alpha &&
  5657. FFABS( p1 - p0 ) < beta &&
  5658. FFABS( q1 - q0 ) < beta ) {
  5659. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5660. if( FFABS( p2 - p0 ) < beta)
  5661. {
  5662. const int p3 = pix[-4];
  5663. /* p0', p1', p2' */
  5664. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5665. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5666. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5667. } else {
  5668. /* p0' */
  5669. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5670. }
  5671. if( FFABS( q2 - q0 ) < beta)
  5672. {
  5673. const int q3 = pix[3];
  5674. /* q0', q1', q2' */
  5675. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5676. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5677. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5678. } else {
  5679. /* q0' */
  5680. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5681. }
  5682. }else{
  5683. /* p0', q0' */
  5684. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5685. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5686. }
  5687. tprintf(h->s.avctx, "filter_mb_edgev i:%d d:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, p2, p1, p0, q0, q1, q2, pix[-2], pix[-1], pix[0], pix[1]);
  5688. }
  5689. pix += stride;
  5690. }
  5691. }
  5692. }
  5693. static void filter_mb_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5694. int i;
  5695. const int index_a = qp + h->slice_alpha_c0_offset;
  5696. const int alpha = (alpha_table+52)[index_a];
  5697. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5698. if( bS[0] < 4 ) {
  5699. int8_t tc[4];
  5700. for(i=0; i<4; i++)
  5701. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] + 1 : 0;
  5702. h->s.dsp.h264_h_loop_filter_chroma(pix, stride, alpha, beta, tc);
  5703. } else {
  5704. h->s.dsp.h264_h_loop_filter_chroma_intra(pix, stride, alpha, beta);
  5705. }
  5706. }
  5707. static void filter_mb_mbaff_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
  5708. int i;
  5709. for( i = 0; i < 16; i++, pix += stride) {
  5710. int index_a;
  5711. int alpha;
  5712. int beta;
  5713. int qp_index;
  5714. int bS_index = (i >> 1);
  5715. if (!MB_FIELD) {
  5716. bS_index &= ~1;
  5717. bS_index |= (i & 1);
  5718. }
  5719. if( bS[bS_index] == 0 ) {
  5720. continue;
  5721. }
  5722. qp_index = MB_FIELD ? (i >> 3) : (i & 1);
  5723. index_a = qp[qp_index] + h->slice_alpha_c0_offset;
  5724. alpha = (alpha_table+52)[index_a];
  5725. beta = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
  5726. if( bS[bS_index] < 4 ) {
  5727. const int tc0 = (tc0_table+52)[index_a][bS[bS_index] - 1];
  5728. const int p0 = pix[-1];
  5729. const int p1 = pix[-2];
  5730. const int p2 = pix[-3];
  5731. const int q0 = pix[0];
  5732. const int q1 = pix[1];
  5733. const int q2 = pix[2];
  5734. if( FFABS( p0 - q0 ) < alpha &&
  5735. FFABS( p1 - p0 ) < beta &&
  5736. FFABS( q1 - q0 ) < beta ) {
  5737. int tc = tc0;
  5738. int i_delta;
  5739. if( FFABS( p2 - p0 ) < beta ) {
  5740. pix[-2] = p1 + av_clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
  5741. tc++;
  5742. }
  5743. if( FFABS( q2 - q0 ) < beta ) {
  5744. pix[1] = q1 + av_clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
  5745. tc++;
  5746. }
  5747. i_delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  5748. pix[-1] = av_clip_uint8( p0 + i_delta ); /* p0' */
  5749. pix[0] = av_clip_uint8( q0 - i_delta ); /* q0' */
  5750. tprintf(h->s.avctx, "filter_mb_mbaff_edgev i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, tc, bS[bS_index], pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1);
  5751. }
  5752. }else{
  5753. const int p0 = pix[-1];
  5754. const int p1 = pix[-2];
  5755. const int p2 = pix[-3];
  5756. const int q0 = pix[0];
  5757. const int q1 = pix[1];
  5758. const int q2 = pix[2];
  5759. if( FFABS( p0 - q0 ) < alpha &&
  5760. FFABS( p1 - p0 ) < beta &&
  5761. FFABS( q1 - q0 ) < beta ) {
  5762. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5763. if( FFABS( p2 - p0 ) < beta)
  5764. {
  5765. const int p3 = pix[-4];
  5766. /* p0', p1', p2' */
  5767. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5768. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5769. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5770. } else {
  5771. /* p0' */
  5772. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5773. }
  5774. if( FFABS( q2 - q0 ) < beta)
  5775. {
  5776. const int q3 = pix[3];
  5777. /* q0', q1', q2' */
  5778. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5779. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5780. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5781. } else {
  5782. /* q0' */
  5783. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5784. }
  5785. }else{
  5786. /* p0', q0' */
  5787. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5788. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5789. }
  5790. tprintf(h->s.avctx, "filter_mb_mbaff_edgev i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, p2, p1, p0, q0, q1, q2, pix[-3], pix[-2], pix[-1], pix[0], pix[1], pix[2]);
  5791. }
  5792. }
  5793. }
  5794. }
  5795. static void filter_mb_mbaff_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
  5796. int i;
  5797. for( i = 0; i < 8; i++, pix += stride) {
  5798. int index_a;
  5799. int alpha;
  5800. int beta;
  5801. int qp_index;
  5802. int bS_index = i;
  5803. if( bS[bS_index] == 0 ) {
  5804. continue;
  5805. }
  5806. qp_index = MB_FIELD ? (i >> 2) : (i & 1);
  5807. index_a = qp[qp_index] + h->slice_alpha_c0_offset;
  5808. alpha = (alpha_table+52)[index_a];
  5809. beta = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
  5810. if( bS[bS_index] < 4 ) {
  5811. const int tc = (tc0_table+52)[index_a][bS[bS_index] - 1] + 1;
  5812. const int p0 = pix[-1];
  5813. const int p1 = pix[-2];
  5814. const int q0 = pix[0];
  5815. const int q1 = pix[1];
  5816. if( FFABS( p0 - q0 ) < alpha &&
  5817. FFABS( p1 - p0 ) < beta &&
  5818. FFABS( q1 - q0 ) < beta ) {
  5819. const int i_delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  5820. pix[-1] = av_clip_uint8( p0 + i_delta ); /* p0' */
  5821. pix[0] = av_clip_uint8( q0 - i_delta ); /* q0' */
  5822. tprintf(h->s.avctx, "filter_mb_mbaff_edgecv i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, tc, bS[bS_index], pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1);
  5823. }
  5824. }else{
  5825. const int p0 = pix[-1];
  5826. const int p1 = pix[-2];
  5827. const int q0 = pix[0];
  5828. const int q1 = pix[1];
  5829. if( FFABS( p0 - q0 ) < alpha &&
  5830. FFABS( p1 - p0 ) < beta &&
  5831. FFABS( q1 - q0 ) < beta ) {
  5832. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
  5833. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
  5834. tprintf(h->s.avctx, "filter_mb_mbaff_edgecv i:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x, %02x, %02x]\n", i, pix[-3], p1, p0, q0, q1, pix[2], pix[-3], pix[-2], pix[-1], pix[0], pix[1], pix[2]);
  5835. }
  5836. }
  5837. }
  5838. }
  5839. static void filter_mb_edgeh( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5840. int i, d;
  5841. const int index_a = qp + h->slice_alpha_c0_offset;
  5842. const int alpha = (alpha_table+52)[index_a];
  5843. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5844. const int pix_next = stride;
  5845. if( bS[0] < 4 ) {
  5846. int8_t tc[4];
  5847. for(i=0; i<4; i++)
  5848. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] : -1;
  5849. h->s.dsp.h264_v_loop_filter_luma(pix, stride, alpha, beta, tc);
  5850. } else {
  5851. /* 16px edge length, see filter_mb_edgev */
  5852. for( d = 0; d < 16; d++ ) {
  5853. const int p0 = pix[-1*pix_next];
  5854. const int p1 = pix[-2*pix_next];
  5855. const int p2 = pix[-3*pix_next];
  5856. const int q0 = pix[0];
  5857. const int q1 = pix[1*pix_next];
  5858. const int q2 = pix[2*pix_next];
  5859. if( FFABS( p0 - q0 ) < alpha &&
  5860. FFABS( p1 - p0 ) < beta &&
  5861. FFABS( q1 - q0 ) < beta ) {
  5862. const int p3 = pix[-4*pix_next];
  5863. const int q3 = pix[ 3*pix_next];
  5864. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5865. if( FFABS( p2 - p0 ) < beta) {
  5866. /* p0', p1', p2' */
  5867. pix[-1*pix_next] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5868. pix[-2*pix_next] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5869. pix[-3*pix_next] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5870. } else {
  5871. /* p0' */
  5872. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5873. }
  5874. if( FFABS( q2 - q0 ) < beta) {
  5875. /* q0', q1', q2' */
  5876. pix[0*pix_next] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5877. pix[1*pix_next] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5878. pix[2*pix_next] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5879. } else {
  5880. /* q0' */
  5881. pix[0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5882. }
  5883. }else{
  5884. /* p0', q0' */
  5885. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5886. pix[ 0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5887. }
  5888. tprintf(h->s.avctx, "filter_mb_edgeh i:%d d:%d, qp:%d, indexA:%d, alpha:%d, beta:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, qp, index_a, alpha, beta, bS[i], p2, p1, p0, q0, q1, q2, pix[-2*pix_next], pix[-pix_next], pix[0], pix[pix_next]);
  5889. }
  5890. pix++;
  5891. }
  5892. }
  5893. }
  5894. static void filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5895. int i;
  5896. const int index_a = qp + h->slice_alpha_c0_offset;
  5897. const int alpha = (alpha_table+52)[index_a];
  5898. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5899. if( bS[0] < 4 ) {
  5900. int8_t tc[4];
  5901. for(i=0; i<4; i++)
  5902. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] + 1 : 0;
  5903. h->s.dsp.h264_v_loop_filter_chroma(pix, stride, alpha, beta, tc);
  5904. } else {
  5905. h->s.dsp.h264_v_loop_filter_chroma_intra(pix, stride, alpha, beta);
  5906. }
  5907. }
  5908. static void filter_mb_fast( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize) {
  5909. MpegEncContext * const s = &h->s;
  5910. int mb_xy, mb_type;
  5911. int qp, qp0, qp1, qpc, qpc0, qpc1, qp_thresh;
  5912. if(mb_x==0 || mb_y==0 || !s->dsp.h264_loop_filter_strength) {
  5913. filter_mb(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize);
  5914. return;
  5915. }
  5916. assert(!FRAME_MBAFF);
  5917. mb_xy = mb_x + mb_y*s->mb_stride;
  5918. mb_type = s->current_picture.mb_type[mb_xy];
  5919. qp = s->current_picture.qscale_table[mb_xy];
  5920. qp0 = s->current_picture.qscale_table[mb_xy-1];
  5921. qp1 = s->current_picture.qscale_table[h->top_mb_xy];
  5922. qpc = get_chroma_qp( h->pps.chroma_qp_index_offset, qp );
  5923. qpc0 = get_chroma_qp( h->pps.chroma_qp_index_offset, qp0 );
  5924. qpc1 = get_chroma_qp( h->pps.chroma_qp_index_offset, qp1 );
  5925. qp0 = (qp + qp0 + 1) >> 1;
  5926. qp1 = (qp + qp1 + 1) >> 1;
  5927. qpc0 = (qpc + qpc0 + 1) >> 1;
  5928. qpc1 = (qpc + qpc1 + 1) >> 1;
  5929. qp_thresh = 15 - h->slice_alpha_c0_offset;
  5930. if(qp <= qp_thresh && qp0 <= qp_thresh && qp1 <= qp_thresh &&
  5931. qpc <= qp_thresh && qpc0 <= qp_thresh && qpc1 <= qp_thresh)
  5932. return;
  5933. if( IS_INTRA(mb_type) ) {
  5934. int16_t bS4[4] = {4,4,4,4};
  5935. int16_t bS3[4] = {3,3,3,3};
  5936. if( IS_8x8DCT(mb_type) ) {
  5937. filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
  5938. filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
  5939. filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bS4, qp1 );
  5940. filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
  5941. } else {
  5942. filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
  5943. filter_mb_edgev( h, &img_y[4*1], linesize, bS3, qp );
  5944. filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
  5945. filter_mb_edgev( h, &img_y[4*3], linesize, bS3, qp );
  5946. filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bS4, qp1 );
  5947. filter_mb_edgeh( h, &img_y[4*1*linesize], linesize, bS3, qp );
  5948. filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
  5949. filter_mb_edgeh( h, &img_y[4*3*linesize], linesize, bS3, qp );
  5950. }
  5951. filter_mb_edgecv( h, &img_cb[2*0], uvlinesize, bS4, qpc0 );
  5952. filter_mb_edgecv( h, &img_cb[2*2], uvlinesize, bS3, qpc );
  5953. filter_mb_edgecv( h, &img_cr[2*0], uvlinesize, bS4, qpc0 );
  5954. filter_mb_edgecv( h, &img_cr[2*2], uvlinesize, bS3, qpc );
  5955. filter_mb_edgech( h, &img_cb[2*0*uvlinesize], uvlinesize, bS4, qpc1 );
  5956. filter_mb_edgech( h, &img_cb[2*2*uvlinesize], uvlinesize, bS3, qpc );
  5957. filter_mb_edgech( h, &img_cr[2*0*uvlinesize], uvlinesize, bS4, qpc1 );
  5958. filter_mb_edgech( h, &img_cr[2*2*uvlinesize], uvlinesize, bS3, qpc );
  5959. return;
  5960. } else {
  5961. DECLARE_ALIGNED_8(int16_t, bS[2][4][4]);
  5962. uint64_t (*bSv)[4] = (uint64_t(*)[4])bS;
  5963. int edges;
  5964. if( IS_8x8DCT(mb_type) && (h->cbp&7) == 7 ) {
  5965. edges = 4;
  5966. bSv[0][0] = bSv[0][2] = bSv[1][0] = bSv[1][2] = 0x0002000200020002ULL;
  5967. } else {
  5968. int mask_edge1 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16)) ? 3 :
  5969. (mb_type & MB_TYPE_16x8) ? 1 : 0;
  5970. int mask_edge0 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16))
  5971. && (s->current_picture.mb_type[mb_xy-1] & (MB_TYPE_16x16 | MB_TYPE_8x16))
  5972. ? 3 : 0;
  5973. int step = IS_8x8DCT(mb_type) ? 2 : 1;
  5974. edges = (mb_type & MB_TYPE_16x16) && !(h->cbp & 15) ? 1 : 4;
  5975. s->dsp.h264_loop_filter_strength( bS, h->non_zero_count_cache, h->ref_cache, h->mv_cache,
  5976. (h->slice_type == B_TYPE), edges, step, mask_edge0, mask_edge1 );
  5977. }
  5978. if( IS_INTRA(s->current_picture.mb_type[mb_xy-1]) )
  5979. bSv[0][0] = 0x0004000400040004ULL;
  5980. if( IS_INTRA(s->current_picture.mb_type[h->top_mb_xy]) )
  5981. bSv[1][0] = 0x0004000400040004ULL;
  5982. #define FILTER(hv,dir,edge)\
  5983. if(bSv[dir][edge]) {\
  5984. filter_mb_edge##hv( h, &img_y[4*edge*(dir?linesize:1)], linesize, bS[dir][edge], edge ? qp : qp##dir );\
  5985. if(!(edge&1)) {\
  5986. filter_mb_edgec##hv( h, &img_cb[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
  5987. filter_mb_edgec##hv( h, &img_cr[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
  5988. }\
  5989. }
  5990. if( edges == 1 ) {
  5991. FILTER(v,0,0);
  5992. FILTER(h,1,0);
  5993. } else if( IS_8x8DCT(mb_type) ) {
  5994. FILTER(v,0,0);
  5995. FILTER(v,0,2);
  5996. FILTER(h,1,0);
  5997. FILTER(h,1,2);
  5998. } else {
  5999. FILTER(v,0,0);
  6000. FILTER(v,0,1);
  6001. FILTER(v,0,2);
  6002. FILTER(v,0,3);
  6003. FILTER(h,1,0);
  6004. FILTER(h,1,1);
  6005. FILTER(h,1,2);
  6006. FILTER(h,1,3);
  6007. }
  6008. #undef FILTER
  6009. }
  6010. }
  6011. static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize) {
  6012. MpegEncContext * const s = &h->s;
  6013. const int mb_xy= mb_x + mb_y*s->mb_stride;
  6014. const int mb_type = s->current_picture.mb_type[mb_xy];
  6015. const int mvy_limit = IS_INTERLACED(mb_type) ? 2 : 4;
  6016. int first_vertical_edge_done = 0;
  6017. int dir;
  6018. /* FIXME: A given frame may occupy more than one position in
  6019. * the reference list. So ref2frm should be populated with
  6020. * frame numbers, not indices. */
  6021. static const int ref2frm[34] = {-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
  6022. 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
  6023. //for sufficiently low qp, filtering wouldn't do anything
  6024. //this is a conservative estimate: could also check beta_offset and more accurate chroma_qp
  6025. if(!FRAME_MBAFF){
  6026. int qp_thresh = 15 - h->slice_alpha_c0_offset - FFMAX(0, h->pps.chroma_qp_index_offset);
  6027. int qp = s->current_picture.qscale_table[mb_xy];
  6028. if(qp <= qp_thresh
  6029. && (mb_x == 0 || ((qp + s->current_picture.qscale_table[mb_xy-1] + 1)>>1) <= qp_thresh)
  6030. && (mb_y == 0 || ((qp + s->current_picture.qscale_table[h->top_mb_xy] + 1)>>1) <= qp_thresh)){
  6031. return;
  6032. }
  6033. }
  6034. if (FRAME_MBAFF
  6035. // left mb is in picture
  6036. && h->slice_table[mb_xy-1] != 255
  6037. // and current and left pair do not have the same interlaced type
  6038. && (IS_INTERLACED(mb_type) != IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]))
  6039. // and left mb is in the same slice if deblocking_filter == 2
  6040. && (h->deblocking_filter!=2 || h->slice_table[mb_xy-1] == h->slice_table[mb_xy])) {
  6041. /* First vertical edge is different in MBAFF frames
  6042. * There are 8 different bS to compute and 2 different Qp
  6043. */
  6044. const int pair_xy = mb_x + (mb_y&~1)*s->mb_stride;
  6045. const int left_mb_xy[2] = { pair_xy-1, pair_xy-1+s->mb_stride };
  6046. int16_t bS[8];
  6047. int qp[2];
  6048. int chroma_qp[2];
  6049. int mb_qp, mbn0_qp, mbn1_qp;
  6050. int i;
  6051. first_vertical_edge_done = 1;
  6052. if( IS_INTRA(mb_type) )
  6053. bS[0] = bS[1] = bS[2] = bS[3] = bS[4] = bS[5] = bS[6] = bS[7] = 4;
  6054. else {
  6055. for( i = 0; i < 8; i++ ) {
  6056. int mbn_xy = MB_FIELD ? left_mb_xy[i>>2] : left_mb_xy[i&1];
  6057. if( IS_INTRA( s->current_picture.mb_type[mbn_xy] ) )
  6058. bS[i] = 4;
  6059. else if( h->non_zero_count_cache[12+8*(i>>1)] != 0 ||
  6060. /* FIXME: with 8x8dct + cavlc, should check cbp instead of nnz */
  6061. h->non_zero_count[mbn_xy][MB_FIELD ? i&3 : (i>>2)+(mb_y&1)*2] )
  6062. bS[i] = 2;
  6063. else
  6064. bS[i] = 1;
  6065. }
  6066. }
  6067. mb_qp = s->current_picture.qscale_table[mb_xy];
  6068. mbn0_qp = s->current_picture.qscale_table[left_mb_xy[0]];
  6069. mbn1_qp = s->current_picture.qscale_table[left_mb_xy[1]];
  6070. qp[0] = ( mb_qp + mbn0_qp + 1 ) >> 1;
  6071. chroma_qp[0] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, mb_qp ) +
  6072. get_chroma_qp( h->pps.chroma_qp_index_offset, mbn0_qp ) + 1 ) >> 1;
  6073. qp[1] = ( mb_qp + mbn1_qp + 1 ) >> 1;
  6074. chroma_qp[1] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, mb_qp ) +
  6075. get_chroma_qp( h->pps.chroma_qp_index_offset, mbn1_qp ) + 1 ) >> 1;
  6076. /* Filter edge */
  6077. tprintf(s->avctx, "filter mb:%d/%d MBAFF, QPy:%d/%d, QPc:%d/%d ls:%d uvls:%d", mb_x, mb_y, qp[0], qp[1], chroma_qp[0], chroma_qp[1], linesize, uvlinesize);
  6078. { int i; for (i = 0; i < 8; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
  6079. filter_mb_mbaff_edgev ( h, &img_y [0], linesize, bS, qp );
  6080. filter_mb_mbaff_edgecv( h, &img_cb[0], uvlinesize, bS, chroma_qp );
  6081. filter_mb_mbaff_edgecv( h, &img_cr[0], uvlinesize, bS, chroma_qp );
  6082. }
  6083. /* dir : 0 -> vertical edge, 1 -> horizontal edge */
  6084. for( dir = 0; dir < 2; dir++ )
  6085. {
  6086. int edge;
  6087. const int mbm_xy = dir == 0 ? mb_xy -1 : h->top_mb_xy;
  6088. const int mbm_type = s->current_picture.mb_type[mbm_xy];
  6089. int start = h->slice_table[mbm_xy] == 255 ? 1 : 0;
  6090. const int edges = (mb_type & (MB_TYPE_16x16|MB_TYPE_SKIP))
  6091. == (MB_TYPE_16x16|MB_TYPE_SKIP) ? 1 : 4;
  6092. // how often to recheck mv-based bS when iterating between edges
  6093. const int mask_edge = (mb_type & (MB_TYPE_16x16 | (MB_TYPE_16x8 << dir))) ? 3 :
  6094. (mb_type & (MB_TYPE_8x16 >> dir)) ? 1 : 0;
  6095. // how often to recheck mv-based bS when iterating along each edge
  6096. const int mask_par0 = mb_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir));
  6097. if (first_vertical_edge_done) {
  6098. start = 1;
  6099. first_vertical_edge_done = 0;
  6100. }
  6101. if (h->deblocking_filter==2 && h->slice_table[mbm_xy] != h->slice_table[mb_xy])
  6102. start = 1;
  6103. if (FRAME_MBAFF && (dir == 1) && ((mb_y&1) == 0) && start == 0
  6104. && !IS_INTERLACED(mb_type)
  6105. && IS_INTERLACED(mbm_type)
  6106. ) {
  6107. // This is a special case in the norm where the filtering must
  6108. // be done twice (one each of the field) even if we are in a
  6109. // frame macroblock.
  6110. //
  6111. static const int nnz_idx[4] = {4,5,6,3};
  6112. unsigned int tmp_linesize = 2 * linesize;
  6113. unsigned int tmp_uvlinesize = 2 * uvlinesize;
  6114. int mbn_xy = mb_xy - 2 * s->mb_stride;
  6115. int qp, chroma_qp;
  6116. int i, j;
  6117. int16_t bS[4];
  6118. for(j=0; j<2; j++, mbn_xy += s->mb_stride){
  6119. if( IS_INTRA(mb_type) ||
  6120. IS_INTRA(s->current_picture.mb_type[mbn_xy]) ) {
  6121. bS[0] = bS[1] = bS[2] = bS[3] = 3;
  6122. } else {
  6123. const uint8_t *mbn_nnz = h->non_zero_count[mbn_xy];
  6124. for( i = 0; i < 4; i++ ) {
  6125. if( h->non_zero_count_cache[scan8[0]+i] != 0 ||
  6126. mbn_nnz[nnz_idx[i]] != 0 )
  6127. bS[i] = 2;
  6128. else
  6129. bS[i] = 1;
  6130. }
  6131. }
  6132. // Do not use s->qscale as luma quantizer because it has not the same
  6133. // value in IPCM macroblocks.
  6134. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  6135. tprintf(s->avctx, "filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, tmp_linesize, tmp_uvlinesize);
  6136. { int i; for (i = 0; i < 4; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
  6137. filter_mb_edgeh( h, &img_y[j*linesize], tmp_linesize, bS, qp );
  6138. chroma_qp = ( h->chroma_qp +
  6139. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6140. filter_mb_edgech( h, &img_cb[j*uvlinesize], tmp_uvlinesize, bS, chroma_qp );
  6141. filter_mb_edgech( h, &img_cr[j*uvlinesize], tmp_uvlinesize, bS, chroma_qp );
  6142. }
  6143. start = 1;
  6144. }
  6145. /* Calculate bS */
  6146. for( edge = start; edge < edges; edge++ ) {
  6147. /* mbn_xy: neighbor macroblock */
  6148. const int mbn_xy = edge > 0 ? mb_xy : mbm_xy;
  6149. const int mbn_type = s->current_picture.mb_type[mbn_xy];
  6150. int16_t bS[4];
  6151. int qp;
  6152. if( (edge&1) && IS_8x8DCT(mb_type) )
  6153. continue;
  6154. if( IS_INTRA(mb_type) ||
  6155. IS_INTRA(mbn_type) ) {
  6156. int value;
  6157. if (edge == 0) {
  6158. if ( (!IS_INTERLACED(mb_type) && !IS_INTERLACED(mbm_type))
  6159. || ((FRAME_MBAFF || (s->picture_structure != PICT_FRAME)) && (dir == 0))
  6160. ) {
  6161. value = 4;
  6162. } else {
  6163. value = 3;
  6164. }
  6165. } else {
  6166. value = 3;
  6167. }
  6168. bS[0] = bS[1] = bS[2] = bS[3] = value;
  6169. } else {
  6170. int i, l;
  6171. int mv_done;
  6172. if( edge & mask_edge ) {
  6173. bS[0] = bS[1] = bS[2] = bS[3] = 0;
  6174. mv_done = 1;
  6175. }
  6176. else if( FRAME_MBAFF && IS_INTERLACED(mb_type ^ mbn_type)) {
  6177. bS[0] = bS[1] = bS[2] = bS[3] = 1;
  6178. mv_done = 1;
  6179. }
  6180. else if( mask_par0 && (edge || (mbn_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir)))) ) {
  6181. int b_idx= 8 + 4 + edge * (dir ? 8:1);
  6182. int bn_idx= b_idx - (dir ? 8:1);
  6183. int v = 0;
  6184. for( l = 0; !v && l < 1 + (h->slice_type == B_TYPE); l++ ) {
  6185. v |= ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  6186. FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  6187. FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit;
  6188. }
  6189. bS[0] = bS[1] = bS[2] = bS[3] = v;
  6190. mv_done = 1;
  6191. }
  6192. else
  6193. mv_done = 0;
  6194. for( i = 0; i < 4; i++ ) {
  6195. int x = dir == 0 ? edge : i;
  6196. int y = dir == 0 ? i : edge;
  6197. int b_idx= 8 + 4 + x + 8*y;
  6198. int bn_idx= b_idx - (dir ? 8:1);
  6199. if( h->non_zero_count_cache[b_idx] != 0 ||
  6200. h->non_zero_count_cache[bn_idx] != 0 ) {
  6201. bS[i] = 2;
  6202. }
  6203. else if(!mv_done)
  6204. {
  6205. bS[i] = 0;
  6206. for( l = 0; l < 1 + (h->slice_type == B_TYPE); l++ ) {
  6207. if( ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  6208. FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  6209. FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit ) {
  6210. bS[i] = 1;
  6211. break;
  6212. }
  6213. }
  6214. }
  6215. }
  6216. if(bS[0]+bS[1]+bS[2]+bS[3] == 0)
  6217. continue;
  6218. }
  6219. /* Filter edge */
  6220. // Do not use s->qscale as luma quantizer because it has not the same
  6221. // value in IPCM macroblocks.
  6222. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  6223. //tprintf(s->avctx, "filter mb:%d/%d dir:%d edge:%d, QPy:%d, QPc:%d, QPcn:%d\n", mb_x, mb_y, dir, edge, qp, h->chroma_qp, s->current_picture.qscale_table[mbn_xy]);
  6224. tprintf(s->avctx, "filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, linesize, uvlinesize);
  6225. { int i; for (i = 0; i < 4; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
  6226. if( dir == 0 ) {
  6227. filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp );
  6228. if( (edge&1) == 0 ) {
  6229. int chroma_qp = ( h->chroma_qp +
  6230. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6231. filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS, chroma_qp );
  6232. filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS, chroma_qp );
  6233. }
  6234. } else {
  6235. filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp );
  6236. if( (edge&1) == 0 ) {
  6237. int chroma_qp = ( h->chroma_qp +
  6238. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6239. filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  6240. filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  6241. }
  6242. }
  6243. }
  6244. }
  6245. }
  6246. static int decode_slice(H264Context *h){
  6247. MpegEncContext * const s = &h->s;
  6248. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  6249. s->mb_skip_run= -1;
  6250. if( h->pps.cabac ) {
  6251. int i;
  6252. /* realign */
  6253. align_get_bits( &s->gb );
  6254. /* init cabac */
  6255. ff_init_cabac_states( &h->cabac);
  6256. ff_init_cabac_decoder( &h->cabac,
  6257. s->gb.buffer + get_bits_count(&s->gb)/8,
  6258. ( s->gb.size_in_bits - get_bits_count(&s->gb) + 7)/8);
  6259. /* calculate pre-state */
  6260. for( i= 0; i < 460; i++ ) {
  6261. int pre;
  6262. if( h->slice_type == I_TYPE )
  6263. pre = av_clip( ((cabac_context_init_I[i][0] * s->qscale) >>4 ) + cabac_context_init_I[i][1], 1, 126 );
  6264. else
  6265. pre = av_clip( ((cabac_context_init_PB[h->cabac_init_idc][i][0] * s->qscale) >>4 ) + cabac_context_init_PB[h->cabac_init_idc][i][1], 1, 126 );
  6266. if( pre <= 63 )
  6267. h->cabac_state[i] = 2 * ( 63 - pre ) + 0;
  6268. else
  6269. h->cabac_state[i] = 2 * ( pre - 64 ) + 1;
  6270. }
  6271. for(;;){
  6272. //START_TIMER
  6273. int ret = decode_mb_cabac(h);
  6274. int eos;
  6275. //STOP_TIMER("decode_mb_cabac")
  6276. if(ret>=0) hl_decode_mb(h);
  6277. if( ret >= 0 && FRAME_MBAFF ) { //FIXME optimal? or let mb_decode decode 16x32 ?
  6278. s->mb_y++;
  6279. if(ret>=0) ret = decode_mb_cabac(h);
  6280. if(ret>=0) hl_decode_mb(h);
  6281. s->mb_y--;
  6282. }
  6283. eos = get_cabac_terminate( &h->cabac );
  6284. if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
  6285. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d, bytestream (%d)\n", s->mb_x, s->mb_y, h->cabac.bytestream_end - h->cabac.bytestream);
  6286. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
  6287. return -1;
  6288. }
  6289. if( ++s->mb_x >= s->mb_width ) {
  6290. s->mb_x = 0;
  6291. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6292. ++s->mb_y;
  6293. if(FRAME_MBAFF) {
  6294. ++s->mb_y;
  6295. }
  6296. }
  6297. if( eos || s->mb_y >= s->mb_height ) {
  6298. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6299. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
  6300. return 0;
  6301. }
  6302. }
  6303. } else {
  6304. for(;;){
  6305. int ret = decode_mb_cavlc(h);
  6306. if(ret>=0) hl_decode_mb(h);
  6307. if(ret>=0 && FRAME_MBAFF){ //FIXME optimal? or let mb_decode decode 16x32 ?
  6308. s->mb_y++;
  6309. ret = decode_mb_cavlc(h);
  6310. if(ret>=0) hl_decode_mb(h);
  6311. s->mb_y--;
  6312. }
  6313. if(ret<0){
  6314. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6315. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
  6316. return -1;
  6317. }
  6318. if(++s->mb_x >= s->mb_width){
  6319. s->mb_x=0;
  6320. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6321. ++s->mb_y;
  6322. if(FRAME_MBAFF) {
  6323. ++s->mb_y;
  6324. }
  6325. if(s->mb_y >= s->mb_height){
  6326. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6327. if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
  6328. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
  6329. return 0;
  6330. }else{
  6331. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
  6332. return -1;
  6333. }
  6334. }
  6335. }
  6336. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  6337. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6338. if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
  6339. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
  6340. return 0;
  6341. }else{
  6342. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
  6343. return -1;
  6344. }
  6345. }
  6346. }
  6347. }
  6348. #if 0
  6349. for(;s->mb_y < s->mb_height; s->mb_y++){
  6350. for(;s->mb_x < s->mb_width; s->mb_x++){
  6351. int ret= decode_mb(h);
  6352. hl_decode_mb(h);
  6353. if(ret<0){
  6354. av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6355. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
  6356. return -1;
  6357. }
  6358. if(++s->mb_x >= s->mb_width){
  6359. s->mb_x=0;
  6360. if(++s->mb_y >= s->mb_height){
  6361. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6362. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
  6363. return 0;
  6364. }else{
  6365. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
  6366. return -1;
  6367. }
  6368. }
  6369. }
  6370. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  6371. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6372. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
  6373. return 0;
  6374. }else{
  6375. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
  6376. return -1;
  6377. }
  6378. }
  6379. }
  6380. s->mb_x=0;
  6381. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6382. }
  6383. #endif
  6384. return -1; //not reached
  6385. }
  6386. static int decode_unregistered_user_data(H264Context *h, int size){
  6387. MpegEncContext * const s = &h->s;
  6388. uint8_t user_data[16+256];
  6389. int e, build, i;
  6390. if(size<16)
  6391. return -1;
  6392. for(i=0; i<sizeof(user_data)-1 && i<size; i++){
  6393. user_data[i]= get_bits(&s->gb, 8);
  6394. }
  6395. user_data[i]= 0;
  6396. e= sscanf(user_data+16, "x264 - core %d"/*%s - H.264/MPEG-4 AVC codec - Copyleft 2005 - http://www.videolan.org/x264.html*/, &build);
  6397. if(e==1 && build>=0)
  6398. h->x264_build= build;
  6399. if(s->avctx->debug & FF_DEBUG_BUGS)
  6400. av_log(s->avctx, AV_LOG_DEBUG, "user data:\"%s\"\n", user_data+16);
  6401. for(; i<size; i++)
  6402. skip_bits(&s->gb, 8);
  6403. return 0;
  6404. }
  6405. static int decode_sei(H264Context *h){
  6406. MpegEncContext * const s = &h->s;
  6407. while(get_bits_count(&s->gb) + 16 < s->gb.size_in_bits){
  6408. int size, type;
  6409. type=0;
  6410. do{
  6411. type+= show_bits(&s->gb, 8);
  6412. }while(get_bits(&s->gb, 8) == 255);
  6413. size=0;
  6414. do{
  6415. size+= show_bits(&s->gb, 8);
  6416. }while(get_bits(&s->gb, 8) == 255);
  6417. switch(type){
  6418. case 5:
  6419. if(decode_unregistered_user_data(h, size) < 0)
  6420. return -1;
  6421. break;
  6422. default:
  6423. skip_bits(&s->gb, 8*size);
  6424. }
  6425. //FIXME check bits here
  6426. align_get_bits(&s->gb);
  6427. }
  6428. return 0;
  6429. }
  6430. static inline void decode_hrd_parameters(H264Context *h, SPS *sps){
  6431. MpegEncContext * const s = &h->s;
  6432. int cpb_count, i;
  6433. cpb_count = get_ue_golomb(&s->gb) + 1;
  6434. get_bits(&s->gb, 4); /* bit_rate_scale */
  6435. get_bits(&s->gb, 4); /* cpb_size_scale */
  6436. for(i=0; i<cpb_count; i++){
  6437. get_ue_golomb(&s->gb); /* bit_rate_value_minus1 */
  6438. get_ue_golomb(&s->gb); /* cpb_size_value_minus1 */
  6439. get_bits1(&s->gb); /* cbr_flag */
  6440. }
  6441. get_bits(&s->gb, 5); /* initial_cpb_removal_delay_length_minus1 */
  6442. get_bits(&s->gb, 5); /* cpb_removal_delay_length_minus1 */
  6443. get_bits(&s->gb, 5); /* dpb_output_delay_length_minus1 */
  6444. get_bits(&s->gb, 5); /* time_offset_length */
  6445. }
  6446. static inline int decode_vui_parameters(H264Context *h, SPS *sps){
  6447. MpegEncContext * const s = &h->s;
  6448. int aspect_ratio_info_present_flag;
  6449. unsigned int aspect_ratio_idc;
  6450. int nal_hrd_parameters_present_flag, vcl_hrd_parameters_present_flag;
  6451. aspect_ratio_info_present_flag= get_bits1(&s->gb);
  6452. if( aspect_ratio_info_present_flag ) {
  6453. aspect_ratio_idc= get_bits(&s->gb, 8);
  6454. if( aspect_ratio_idc == EXTENDED_SAR ) {
  6455. sps->sar.num= get_bits(&s->gb, 16);
  6456. sps->sar.den= get_bits(&s->gb, 16);
  6457. }else if(aspect_ratio_idc < 14){
  6458. sps->sar= pixel_aspect[aspect_ratio_idc];
  6459. }else{
  6460. av_log(h->s.avctx, AV_LOG_ERROR, "illegal aspect ratio\n");
  6461. return -1;
  6462. }
  6463. }else{
  6464. sps->sar.num=
  6465. sps->sar.den= 0;
  6466. }
  6467. // s->avctx->aspect_ratio= sar_width*s->width / (float)(s->height*sar_height);
  6468. if(get_bits1(&s->gb)){ /* overscan_info_present_flag */
  6469. get_bits1(&s->gb); /* overscan_appropriate_flag */
  6470. }
  6471. if(get_bits1(&s->gb)){ /* video_signal_type_present_flag */
  6472. get_bits(&s->gb, 3); /* video_format */
  6473. get_bits1(&s->gb); /* video_full_range_flag */
  6474. if(get_bits1(&s->gb)){ /* colour_description_present_flag */
  6475. get_bits(&s->gb, 8); /* colour_primaries */
  6476. get_bits(&s->gb, 8); /* transfer_characteristics */
  6477. get_bits(&s->gb, 8); /* matrix_coefficients */
  6478. }
  6479. }
  6480. if(get_bits1(&s->gb)){ /* chroma_location_info_present_flag */
  6481. get_ue_golomb(&s->gb); /* chroma_sample_location_type_top_field */
  6482. get_ue_golomb(&s->gb); /* chroma_sample_location_type_bottom_field */
  6483. }
  6484. sps->timing_info_present_flag = get_bits1(&s->gb);
  6485. if(sps->timing_info_present_flag){
  6486. sps->num_units_in_tick = get_bits_long(&s->gb, 32);
  6487. sps->time_scale = get_bits_long(&s->gb, 32);
  6488. sps->fixed_frame_rate_flag = get_bits1(&s->gb);
  6489. }
  6490. nal_hrd_parameters_present_flag = get_bits1(&s->gb);
  6491. if(nal_hrd_parameters_present_flag)
  6492. decode_hrd_parameters(h, sps);
  6493. vcl_hrd_parameters_present_flag = get_bits1(&s->gb);
  6494. if(vcl_hrd_parameters_present_flag)
  6495. decode_hrd_parameters(h, sps);
  6496. if(nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag)
  6497. get_bits1(&s->gb); /* low_delay_hrd_flag */
  6498. get_bits1(&s->gb); /* pic_struct_present_flag */
  6499. sps->bitstream_restriction_flag = get_bits1(&s->gb);
  6500. if(sps->bitstream_restriction_flag){
  6501. unsigned int num_reorder_frames;
  6502. get_bits1(&s->gb); /* motion_vectors_over_pic_boundaries_flag */
  6503. get_ue_golomb(&s->gb); /* max_bytes_per_pic_denom */
  6504. get_ue_golomb(&s->gb); /* max_bits_per_mb_denom */
  6505. get_ue_golomb(&s->gb); /* log2_max_mv_length_horizontal */
  6506. get_ue_golomb(&s->gb); /* log2_max_mv_length_vertical */
  6507. num_reorder_frames= get_ue_golomb(&s->gb);
  6508. get_ue_golomb(&s->gb); /*max_dec_frame_buffering*/
  6509. if(num_reorder_frames > 16 /*max_dec_frame_buffering || max_dec_frame_buffering > 16*/){
  6510. av_log(h->s.avctx, AV_LOG_ERROR, "illegal num_reorder_frames %d\n", num_reorder_frames);
  6511. return -1;
  6512. }
  6513. sps->num_reorder_frames= num_reorder_frames;
  6514. }
  6515. return 0;
  6516. }
  6517. static void decode_scaling_list(H264Context *h, uint8_t *factors, int size,
  6518. const uint8_t *jvt_list, const uint8_t *fallback_list){
  6519. MpegEncContext * const s = &h->s;
  6520. int i, last = 8, next = 8;
  6521. const uint8_t *scan = size == 16 ? zigzag_scan : zigzag_scan8x8;
  6522. if(!get_bits1(&s->gb)) /* matrix not written, we use the predicted one */
  6523. memcpy(factors, fallback_list, size*sizeof(uint8_t));
  6524. else
  6525. for(i=0;i<size;i++){
  6526. if(next)
  6527. next = (last + get_se_golomb(&s->gb)) & 0xff;
  6528. if(!i && !next){ /* matrix not written, we use the preset one */
  6529. memcpy(factors, jvt_list, size*sizeof(uint8_t));
  6530. break;
  6531. }
  6532. last = factors[scan[i]] = next ? next : last;
  6533. }
  6534. }
  6535. static void decode_scaling_matrices(H264Context *h, SPS *sps, PPS *pps, int is_sps,
  6536. uint8_t (*scaling_matrix4)[16], uint8_t (*scaling_matrix8)[64]){
  6537. MpegEncContext * const s = &h->s;
  6538. int fallback_sps = !is_sps && sps->scaling_matrix_present;
  6539. const uint8_t *fallback[4] = {
  6540. fallback_sps ? sps->scaling_matrix4[0] : default_scaling4[0],
  6541. fallback_sps ? sps->scaling_matrix4[3] : default_scaling4[1],
  6542. fallback_sps ? sps->scaling_matrix8[0] : default_scaling8[0],
  6543. fallback_sps ? sps->scaling_matrix8[1] : default_scaling8[1]
  6544. };
  6545. if(get_bits1(&s->gb)){
  6546. sps->scaling_matrix_present |= is_sps;
  6547. decode_scaling_list(h,scaling_matrix4[0],16,default_scaling4[0],fallback[0]); // Intra, Y
  6548. decode_scaling_list(h,scaling_matrix4[1],16,default_scaling4[0],scaling_matrix4[0]); // Intra, Cr
  6549. decode_scaling_list(h,scaling_matrix4[2],16,default_scaling4[0],scaling_matrix4[1]); // Intra, Cb
  6550. decode_scaling_list(h,scaling_matrix4[3],16,default_scaling4[1],fallback[1]); // Inter, Y
  6551. decode_scaling_list(h,scaling_matrix4[4],16,default_scaling4[1],scaling_matrix4[3]); // Inter, Cr
  6552. decode_scaling_list(h,scaling_matrix4[5],16,default_scaling4[1],scaling_matrix4[4]); // Inter, Cb
  6553. if(is_sps || pps->transform_8x8_mode){
  6554. decode_scaling_list(h,scaling_matrix8[0],64,default_scaling8[0],fallback[2]); // Intra, Y
  6555. decode_scaling_list(h,scaling_matrix8[1],64,default_scaling8[1],fallback[3]); // Inter, Y
  6556. }
  6557. } else if(fallback_sps) {
  6558. memcpy(scaling_matrix4, sps->scaling_matrix4, 6*16*sizeof(uint8_t));
  6559. memcpy(scaling_matrix8, sps->scaling_matrix8, 2*64*sizeof(uint8_t));
  6560. }
  6561. }
  6562. static inline int decode_seq_parameter_set(H264Context *h){
  6563. MpegEncContext * const s = &h->s;
  6564. int profile_idc, level_idc;
  6565. unsigned int sps_id, tmp, mb_width, mb_height;
  6566. int i;
  6567. SPS *sps;
  6568. profile_idc= get_bits(&s->gb, 8);
  6569. get_bits1(&s->gb); //constraint_set0_flag
  6570. get_bits1(&s->gb); //constraint_set1_flag
  6571. get_bits1(&s->gb); //constraint_set2_flag
  6572. get_bits1(&s->gb); //constraint_set3_flag
  6573. get_bits(&s->gb, 4); // reserved
  6574. level_idc= get_bits(&s->gb, 8);
  6575. sps_id= get_ue_golomb(&s->gb);
  6576. if (sps_id >= MAX_SPS_COUNT){
  6577. // ok it has gone out of hand, someone is sending us bad stuff.
  6578. av_log(h->s.avctx, AV_LOG_ERROR, "illegal sps_id (%d)\n", sps_id);
  6579. return -1;
  6580. }
  6581. sps= &h->sps_buffer[ sps_id ];
  6582. sps->profile_idc= profile_idc;
  6583. sps->level_idc= level_idc;
  6584. if(sps->profile_idc >= 100){ //high profile
  6585. if(get_ue_golomb(&s->gb) == 3) //chroma_format_idc
  6586. get_bits1(&s->gb); //residual_color_transform_flag
  6587. get_ue_golomb(&s->gb); //bit_depth_luma_minus8
  6588. get_ue_golomb(&s->gb); //bit_depth_chroma_minus8
  6589. sps->transform_bypass = get_bits1(&s->gb);
  6590. decode_scaling_matrices(h, sps, NULL, 1, sps->scaling_matrix4, sps->scaling_matrix8);
  6591. }else
  6592. sps->scaling_matrix_present = 0;
  6593. sps->log2_max_frame_num= get_ue_golomb(&s->gb) + 4;
  6594. sps->poc_type= get_ue_golomb(&s->gb);
  6595. if(sps->poc_type == 0){ //FIXME #define
  6596. sps->log2_max_poc_lsb= get_ue_golomb(&s->gb) + 4;
  6597. } else if(sps->poc_type == 1){//FIXME #define
  6598. sps->delta_pic_order_always_zero_flag= get_bits1(&s->gb);
  6599. sps->offset_for_non_ref_pic= get_se_golomb(&s->gb);
  6600. sps->offset_for_top_to_bottom_field= get_se_golomb(&s->gb);
  6601. tmp= get_ue_golomb(&s->gb);
  6602. if(tmp >= sizeof(sps->offset_for_ref_frame) / sizeof(sps->offset_for_ref_frame[0])){
  6603. av_log(h->s.avctx, AV_LOG_ERROR, "poc_cycle_length overflow %u\n", tmp);
  6604. return -1;
  6605. }
  6606. sps->poc_cycle_length= tmp;
  6607. for(i=0; i<sps->poc_cycle_length; i++)
  6608. sps->offset_for_ref_frame[i]= get_se_golomb(&s->gb);
  6609. }else if(sps->poc_type != 2){
  6610. av_log(h->s.avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
  6611. return -1;
  6612. }
  6613. tmp= get_ue_golomb(&s->gb);
  6614. if(tmp > MAX_PICTURE_COUNT-2){
  6615. av_log(h->s.avctx, AV_LOG_ERROR, "too many reference frames\n");
  6616. }
  6617. sps->ref_frame_count= tmp;
  6618. sps->gaps_in_frame_num_allowed_flag= get_bits1(&s->gb);
  6619. mb_width= get_ue_golomb(&s->gb) + 1;
  6620. mb_height= get_ue_golomb(&s->gb) + 1;
  6621. if(mb_width >= INT_MAX/16 || mb_height >= INT_MAX/16 ||
  6622. avcodec_check_dimensions(NULL, 16*mb_width, 16*mb_height)){
  6623. av_log(h->s.avctx, AV_LOG_ERROR, "mb_width/height overflow\n");
  6624. return -1;
  6625. }
  6626. sps->mb_width = mb_width;
  6627. sps->mb_height= mb_height;
  6628. sps->frame_mbs_only_flag= get_bits1(&s->gb);
  6629. if(!sps->frame_mbs_only_flag)
  6630. sps->mb_aff= get_bits1(&s->gb);
  6631. else
  6632. sps->mb_aff= 0;
  6633. sps->direct_8x8_inference_flag= get_bits1(&s->gb);
  6634. #ifndef ALLOW_INTERLACE
  6635. if(sps->mb_aff)
  6636. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF support not included; enable it at compile-time.\n");
  6637. #endif
  6638. if(!sps->direct_8x8_inference_flag && sps->mb_aff)
  6639. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF + !direct_8x8_inference is not implemented\n");
  6640. sps->crop= get_bits1(&s->gb);
  6641. if(sps->crop){
  6642. sps->crop_left = get_ue_golomb(&s->gb);
  6643. sps->crop_right = get_ue_golomb(&s->gb);
  6644. sps->crop_top = get_ue_golomb(&s->gb);
  6645. sps->crop_bottom= get_ue_golomb(&s->gb);
  6646. if(sps->crop_left || sps->crop_top){
  6647. av_log(h->s.avctx, AV_LOG_ERROR, "insane cropping not completely supported, this could look slightly wrong ...\n");
  6648. }
  6649. }else{
  6650. sps->crop_left =
  6651. sps->crop_right =
  6652. sps->crop_top =
  6653. sps->crop_bottom= 0;
  6654. }
  6655. sps->vui_parameters_present_flag= get_bits1(&s->gb);
  6656. if( sps->vui_parameters_present_flag )
  6657. decode_vui_parameters(h, sps);
  6658. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  6659. av_log(h->s.avctx, AV_LOG_DEBUG, "sps:%u profile:%d/%d poc:%d ref:%d %dx%d %s %s crop:%d/%d/%d/%d %s\n",
  6660. sps_id, sps->profile_idc, sps->level_idc,
  6661. sps->poc_type,
  6662. sps->ref_frame_count,
  6663. sps->mb_width, sps->mb_height,
  6664. sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
  6665. sps->direct_8x8_inference_flag ? "8B8" : "",
  6666. sps->crop_left, sps->crop_right,
  6667. sps->crop_top, sps->crop_bottom,
  6668. sps->vui_parameters_present_flag ? "VUI" : ""
  6669. );
  6670. }
  6671. return 0;
  6672. }
  6673. static inline int decode_picture_parameter_set(H264Context *h, int bit_length){
  6674. MpegEncContext * const s = &h->s;
  6675. unsigned int tmp, pps_id= get_ue_golomb(&s->gb);
  6676. PPS *pps;
  6677. if(pps_id>=MAX_PPS_COUNT){
  6678. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  6679. return -1;
  6680. }
  6681. pps = &h->pps_buffer[pps_id];
  6682. tmp= get_ue_golomb(&s->gb);
  6683. if(tmp>=MAX_SPS_COUNT){
  6684. av_log(h->s.avctx, AV_LOG_ERROR, "sps_id out of range\n");
  6685. return -1;
  6686. }
  6687. pps->sps_id= tmp;
  6688. pps->cabac= get_bits1(&s->gb);
  6689. pps->pic_order_present= get_bits1(&s->gb);
  6690. pps->slice_group_count= get_ue_golomb(&s->gb) + 1;
  6691. if(pps->slice_group_count > 1 ){
  6692. pps->mb_slice_group_map_type= get_ue_golomb(&s->gb);
  6693. av_log(h->s.avctx, AV_LOG_ERROR, "FMO not supported\n");
  6694. switch(pps->mb_slice_group_map_type){
  6695. case 0:
  6696. #if 0
  6697. | for( i = 0; i <= num_slice_groups_minus1; i++ ) | | |
  6698. | run_length[ i ] |1 |ue(v) |
  6699. #endif
  6700. break;
  6701. case 2:
  6702. #if 0
  6703. | for( i = 0; i < num_slice_groups_minus1; i++ ) | | |
  6704. |{ | | |
  6705. | top_left_mb[ i ] |1 |ue(v) |
  6706. | bottom_right_mb[ i ] |1 |ue(v) |
  6707. | } | | |
  6708. #endif
  6709. break;
  6710. case 3:
  6711. case 4:
  6712. case 5:
  6713. #if 0
  6714. | slice_group_change_direction_flag |1 |u(1) |
  6715. | slice_group_change_rate_minus1 |1 |ue(v) |
  6716. #endif
  6717. break;
  6718. case 6:
  6719. #if 0
  6720. | slice_group_id_cnt_minus1 |1 |ue(v) |
  6721. | for( i = 0; i <= slice_group_id_cnt_minus1; i++ | | |
  6722. |) | | |
  6723. | slice_group_id[ i ] |1 |u(v) |
  6724. #endif
  6725. break;
  6726. }
  6727. }
  6728. pps->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  6729. pps->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  6730. if(pps->ref_count[0]-1 > 32-1 || pps->ref_count[1]-1 > 32-1){
  6731. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
  6732. pps->ref_count[0]= pps->ref_count[1]= 1;
  6733. return -1;
  6734. }
  6735. pps->weighted_pred= get_bits1(&s->gb);
  6736. pps->weighted_bipred_idc= get_bits(&s->gb, 2);
  6737. pps->init_qp= get_se_golomb(&s->gb) + 26;
  6738. pps->init_qs= get_se_golomb(&s->gb) + 26;
  6739. pps->chroma_qp_index_offset= get_se_golomb(&s->gb);
  6740. pps->deblocking_filter_parameters_present= get_bits1(&s->gb);
  6741. pps->constrained_intra_pred= get_bits1(&s->gb);
  6742. pps->redundant_pic_cnt_present = get_bits1(&s->gb);
  6743. pps->transform_8x8_mode= 0;
  6744. h->dequant_coeff_pps= -1; //contents of sps/pps can change even if id doesn't, so reinit
  6745. memset(pps->scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  6746. memset(pps->scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  6747. if(get_bits_count(&s->gb) < bit_length){
  6748. pps->transform_8x8_mode= get_bits1(&s->gb);
  6749. decode_scaling_matrices(h, &h->sps_buffer[pps->sps_id], pps, 0, pps->scaling_matrix4, pps->scaling_matrix8);
  6750. get_se_golomb(&s->gb); //second_chroma_qp_index_offset
  6751. }
  6752. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  6753. av_log(h->s.avctx, AV_LOG_DEBUG, "pps:%u sps:%u %s slice_groups:%d ref:%d/%d %s qp:%d/%d/%d %s %s %s %s\n",
  6754. pps_id, pps->sps_id,
  6755. pps->cabac ? "CABAC" : "CAVLC",
  6756. pps->slice_group_count,
  6757. pps->ref_count[0], pps->ref_count[1],
  6758. pps->weighted_pred ? "weighted" : "",
  6759. pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset,
  6760. pps->deblocking_filter_parameters_present ? "LPAR" : "",
  6761. pps->constrained_intra_pred ? "CONSTR" : "",
  6762. pps->redundant_pic_cnt_present ? "REDU" : "",
  6763. pps->transform_8x8_mode ? "8x8DCT" : ""
  6764. );
  6765. }
  6766. return 0;
  6767. }
  6768. static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){
  6769. MpegEncContext * const s = &h->s;
  6770. AVCodecContext * const avctx= s->avctx;
  6771. int buf_index=0;
  6772. #if 0
  6773. int i;
  6774. for(i=0; i<50; i++){
  6775. av_log(NULL, AV_LOG_ERROR,"%02X ", buf[i]);
  6776. }
  6777. #endif
  6778. if(!(s->flags2 & CODEC_FLAG2_CHUNKS)){
  6779. h->slice_num = 0;
  6780. s->current_picture_ptr= NULL;
  6781. }
  6782. for(;;){
  6783. int consumed;
  6784. int dst_length;
  6785. int bit_length;
  6786. uint8_t *ptr;
  6787. int i, nalsize = 0;
  6788. if(h->is_avc) {
  6789. if(buf_index >= buf_size) break;
  6790. nalsize = 0;
  6791. for(i = 0; i < h->nal_length_size; i++)
  6792. nalsize = (nalsize << 8) | buf[buf_index++];
  6793. if(nalsize <= 1 || (nalsize+buf_index > buf_size)){
  6794. if(nalsize == 1){
  6795. buf_index++;
  6796. continue;
  6797. }else{
  6798. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  6799. break;
  6800. }
  6801. }
  6802. } else {
  6803. // start code prefix search
  6804. for(; buf_index + 3 < buf_size; buf_index++){
  6805. // This should always succeed in the first iteration.
  6806. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  6807. break;
  6808. }
  6809. if(buf_index+3 >= buf_size) break;
  6810. buf_index+=3;
  6811. }
  6812. ptr= decode_nal(h, buf + buf_index, &dst_length, &consumed, h->is_avc ? nalsize : buf_size - buf_index);
  6813. if (ptr==NULL || dst_length < 0){
  6814. return -1;
  6815. }
  6816. while(ptr[dst_length - 1] == 0 && dst_length > 0)
  6817. dst_length--;
  6818. bit_length= !dst_length ? 0 : (8*dst_length - decode_rbsp_trailing(h, ptr + dst_length - 1));
  6819. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  6820. av_log(h->s.avctx, AV_LOG_DEBUG, "NAL %d at %d/%d length %d\n", h->nal_unit_type, buf_index, buf_size, dst_length);
  6821. }
  6822. if (h->is_avc && (nalsize != consumed))
  6823. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
  6824. buf_index += consumed;
  6825. if( (s->hurry_up == 1 && h->nal_ref_idc == 0) //FIXME dont discard SEI id
  6826. ||(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  6827. continue;
  6828. switch(h->nal_unit_type){
  6829. case NAL_IDR_SLICE:
  6830. idr(h); //FIXME ensure we don't loose some frames if there is reordering
  6831. case NAL_SLICE:
  6832. init_get_bits(&s->gb, ptr, bit_length);
  6833. h->intra_gb_ptr=
  6834. h->inter_gb_ptr= &s->gb;
  6835. s->data_partitioning = 0;
  6836. if(decode_slice_header(h) < 0){
  6837. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  6838. break;
  6839. }
  6840. s->current_picture_ptr->key_frame= (h->nal_unit_type == NAL_IDR_SLICE);
  6841. if(h->redundant_pic_count==0 && s->hurry_up < 5
  6842. && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
  6843. && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
  6844. && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
  6845. && avctx->skip_frame < AVDISCARD_ALL)
  6846. decode_slice(h);
  6847. break;
  6848. case NAL_DPA:
  6849. init_get_bits(&s->gb, ptr, bit_length);
  6850. h->intra_gb_ptr=
  6851. h->inter_gb_ptr= NULL;
  6852. s->data_partitioning = 1;
  6853. if(decode_slice_header(h) < 0){
  6854. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  6855. }
  6856. break;
  6857. case NAL_DPB:
  6858. init_get_bits(&h->intra_gb, ptr, bit_length);
  6859. h->intra_gb_ptr= &h->intra_gb;
  6860. break;
  6861. case NAL_DPC:
  6862. init_get_bits(&h->inter_gb, ptr, bit_length);
  6863. h->inter_gb_ptr= &h->inter_gb;
  6864. if(h->redundant_pic_count==0 && h->intra_gb_ptr && s->data_partitioning
  6865. && s->context_initialized
  6866. && s->hurry_up < 5
  6867. && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
  6868. && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
  6869. && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
  6870. && avctx->skip_frame < AVDISCARD_ALL)
  6871. decode_slice(h);
  6872. break;
  6873. case NAL_SEI:
  6874. init_get_bits(&s->gb, ptr, bit_length);
  6875. decode_sei(h);
  6876. break;
  6877. case NAL_SPS:
  6878. init_get_bits(&s->gb, ptr, bit_length);
  6879. decode_seq_parameter_set(h);
  6880. if(s->flags& CODEC_FLAG_LOW_DELAY)
  6881. s->low_delay=1;
  6882. if(avctx->has_b_frames < 2)
  6883. avctx->has_b_frames= !s->low_delay;
  6884. break;
  6885. case NAL_PPS:
  6886. init_get_bits(&s->gb, ptr, bit_length);
  6887. decode_picture_parameter_set(h, bit_length);
  6888. break;
  6889. case NAL_AUD:
  6890. case NAL_END_SEQUENCE:
  6891. case NAL_END_STREAM:
  6892. case NAL_FILLER_DATA:
  6893. case NAL_SPS_EXT:
  6894. case NAL_AUXILIARY_SLICE:
  6895. break;
  6896. default:
  6897. av_log(avctx, AV_LOG_ERROR, "Unknown NAL code: %d\n", h->nal_unit_type);
  6898. }
  6899. }
  6900. return buf_index;
  6901. }
  6902. /**
  6903. * returns the number of bytes consumed for building the current frame
  6904. */
  6905. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  6906. if(s->flags&CODEC_FLAG_TRUNCATED){
  6907. pos -= s->parse_context.last_index;
  6908. if(pos<0) pos=0; // FIXME remove (unneeded?)
  6909. return pos;
  6910. }else{
  6911. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  6912. if(pos+10>buf_size) pos=buf_size; // oops ;)
  6913. return pos;
  6914. }
  6915. }
  6916. static int decode_frame(AVCodecContext *avctx,
  6917. void *data, int *data_size,
  6918. uint8_t *buf, int buf_size)
  6919. {
  6920. H264Context *h = avctx->priv_data;
  6921. MpegEncContext *s = &h->s;
  6922. AVFrame *pict = data;
  6923. int buf_index;
  6924. s->flags= avctx->flags;
  6925. s->flags2= avctx->flags2;
  6926. /* no supplementary picture */
  6927. if (buf_size == 0) {
  6928. Picture *out;
  6929. int i, out_idx;
  6930. //FIXME factorize this with the output code below
  6931. out = h->delayed_pic[0];
  6932. out_idx = 0;
  6933. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame; i++)
  6934. if(h->delayed_pic[i]->poc < out->poc){
  6935. out = h->delayed_pic[i];
  6936. out_idx = i;
  6937. }
  6938. for(i=out_idx; h->delayed_pic[i]; i++)
  6939. h->delayed_pic[i] = h->delayed_pic[i+1];
  6940. if(out){
  6941. *data_size = sizeof(AVFrame);
  6942. *pict= *(AVFrame*)out;
  6943. }
  6944. return 0;
  6945. }
  6946. if(s->flags&CODEC_FLAG_TRUNCATED){
  6947. int next= ff_h264_find_frame_end(h, buf, buf_size);
  6948. if( ff_combine_frame(&s->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0 )
  6949. return buf_size;
  6950. //printf("next:%d buf_size:%d last_index:%d\n", next, buf_size, s->parse_context.last_index);
  6951. }
  6952. if(h->is_avc && !h->got_avcC) {
  6953. int i, cnt, nalsize;
  6954. unsigned char *p = avctx->extradata;
  6955. if(avctx->extradata_size < 7) {
  6956. av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
  6957. return -1;
  6958. }
  6959. if(*p != 1) {
  6960. av_log(avctx, AV_LOG_ERROR, "Unknown avcC version %d\n", *p);
  6961. return -1;
  6962. }
  6963. /* sps and pps in the avcC always have length coded with 2 bytes,
  6964. so put a fake nal_length_size = 2 while parsing them */
  6965. h->nal_length_size = 2;
  6966. // Decode sps from avcC
  6967. cnt = *(p+5) & 0x1f; // Number of sps
  6968. p += 6;
  6969. for (i = 0; i < cnt; i++) {
  6970. nalsize = AV_RB16(p) + 2;
  6971. if(decode_nal_units(h, p, nalsize) < 0) {
  6972. av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
  6973. return -1;
  6974. }
  6975. p += nalsize;
  6976. }
  6977. // Decode pps from avcC
  6978. cnt = *(p++); // Number of pps
  6979. for (i = 0; i < cnt; i++) {
  6980. nalsize = AV_RB16(p) + 2;
  6981. if(decode_nal_units(h, p, nalsize) != nalsize) {
  6982. av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
  6983. return -1;
  6984. }
  6985. p += nalsize;
  6986. }
  6987. // Now store right nal length size, that will be use to parse all other nals
  6988. h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
  6989. // Do not reparse avcC
  6990. h->got_avcC = 1;
  6991. }
  6992. if(avctx->frame_number==0 && !h->is_avc && s->avctx->extradata_size){
  6993. if(decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) < 0)
  6994. return -1;
  6995. }
  6996. buf_index=decode_nal_units(h, buf, buf_size);
  6997. if(buf_index < 0)
  6998. return -1;
  6999. if(!(s->flags2 & CODEC_FLAG2_CHUNKS) && !s->current_picture_ptr){
  7000. av_log(avctx, AV_LOG_ERROR, "no frame!\n");
  7001. return -1;
  7002. }
  7003. if(!(s->flags2 & CODEC_FLAG2_CHUNKS) || (s->mb_y >= s->mb_height && s->mb_height)){
  7004. Picture *out = s->current_picture_ptr;
  7005. Picture *cur = s->current_picture_ptr;
  7006. Picture *prev = h->delayed_output_pic;
  7007. int i, pics, cross_idr, out_of_order, out_idx;
  7008. s->mb_y= 0;
  7009. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
  7010. s->current_picture_ptr->pict_type= s->pict_type;
  7011. h->prev_frame_num_offset= h->frame_num_offset;
  7012. h->prev_frame_num= h->frame_num;
  7013. if(s->current_picture_ptr->reference){
  7014. h->prev_poc_msb= h->poc_msb;
  7015. h->prev_poc_lsb= h->poc_lsb;
  7016. }
  7017. if(s->current_picture_ptr->reference)
  7018. execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  7019. ff_er_frame_end(s);
  7020. MPV_frame_end(s);
  7021. //FIXME do something with unavailable reference frames
  7022. #if 0 //decode order
  7023. *data_size = sizeof(AVFrame);
  7024. #else
  7025. /* Sort B-frames into display order */
  7026. if(h->sps.bitstream_restriction_flag
  7027. && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  7028. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  7029. s->low_delay = 0;
  7030. }
  7031. pics = 0;
  7032. while(h->delayed_pic[pics]) pics++;
  7033. assert(pics+1 < sizeof(h->delayed_pic) / sizeof(h->delayed_pic[0]));
  7034. h->delayed_pic[pics++] = cur;
  7035. if(cur->reference == 0)
  7036. cur->reference = 1;
  7037. cross_idr = 0;
  7038. for(i=0; h->delayed_pic[i]; i++)
  7039. if(h->delayed_pic[i]->key_frame || h->delayed_pic[i]->poc==0)
  7040. cross_idr = 1;
  7041. out = h->delayed_pic[0];
  7042. out_idx = 0;
  7043. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame; i++)
  7044. if(h->delayed_pic[i]->poc < out->poc){
  7045. out = h->delayed_pic[i];
  7046. out_idx = i;
  7047. }
  7048. out_of_order = !cross_idr && prev && out->poc < prev->poc;
  7049. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
  7050. { }
  7051. else if(prev && pics <= s->avctx->has_b_frames)
  7052. out = prev;
  7053. else if((out_of_order && pics-1 == s->avctx->has_b_frames && pics < 15)
  7054. || (s->low_delay &&
  7055. ((!cross_idr && prev && out->poc > prev->poc + 2)
  7056. || cur->pict_type == B_TYPE)))
  7057. {
  7058. s->low_delay = 0;
  7059. s->avctx->has_b_frames++;
  7060. out = prev;
  7061. }
  7062. else if(out_of_order)
  7063. out = prev;
  7064. if(out_of_order || pics > s->avctx->has_b_frames){
  7065. for(i=out_idx; h->delayed_pic[i]; i++)
  7066. h->delayed_pic[i] = h->delayed_pic[i+1];
  7067. }
  7068. if(prev == out)
  7069. *data_size = 0;
  7070. else
  7071. *data_size = sizeof(AVFrame);
  7072. if(prev && prev != out && prev->reference == 1)
  7073. prev->reference = 0;
  7074. h->delayed_output_pic = out;
  7075. #endif
  7076. if(out)
  7077. *pict= *(AVFrame*)out;
  7078. else
  7079. av_log(avctx, AV_LOG_DEBUG, "no picture\n");
  7080. }
  7081. assert(pict->data[0] || !*data_size);
  7082. ff_print_debug_info(s, pict);
  7083. //printf("out %d\n", (int)pict->data[0]);
  7084. #if 0 //?
  7085. /* Return the Picture timestamp as the frame number */
  7086. /* we substract 1 because it is added on utils.c */
  7087. avctx->frame_number = s->picture_number - 1;
  7088. #endif
  7089. return get_consumed_bytes(s, buf_index, buf_size);
  7090. }
  7091. #if 0
  7092. static inline void fill_mb_avail(H264Context *h){
  7093. MpegEncContext * const s = &h->s;
  7094. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  7095. if(s->mb_y){
  7096. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  7097. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  7098. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  7099. }else{
  7100. h->mb_avail[0]=
  7101. h->mb_avail[1]=
  7102. h->mb_avail[2]= 0;
  7103. }
  7104. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  7105. h->mb_avail[4]= 1; //FIXME move out
  7106. h->mb_avail[5]= 0; //FIXME move out
  7107. }
  7108. #endif
  7109. #if 0 //selftest
  7110. #define COUNT 8000
  7111. #define SIZE (COUNT*40)
  7112. int main(){
  7113. int i;
  7114. uint8_t temp[SIZE];
  7115. PutBitContext pb;
  7116. GetBitContext gb;
  7117. // int int_temp[10000];
  7118. DSPContext dsp;
  7119. AVCodecContext avctx;
  7120. dsputil_init(&dsp, &avctx);
  7121. init_put_bits(&pb, temp, SIZE);
  7122. printf("testing unsigned exp golomb\n");
  7123. for(i=0; i<COUNT; i++){
  7124. START_TIMER
  7125. set_ue_golomb(&pb, i);
  7126. STOP_TIMER("set_ue_golomb");
  7127. }
  7128. flush_put_bits(&pb);
  7129. init_get_bits(&gb, temp, 8*SIZE);
  7130. for(i=0; i<COUNT; i++){
  7131. int j, s;
  7132. s= show_bits(&gb, 24);
  7133. START_TIMER
  7134. j= get_ue_golomb(&gb);
  7135. if(j != i){
  7136. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  7137. // return -1;
  7138. }
  7139. STOP_TIMER("get_ue_golomb");
  7140. }
  7141. init_put_bits(&pb, temp, SIZE);
  7142. printf("testing signed exp golomb\n");
  7143. for(i=0; i<COUNT; i++){
  7144. START_TIMER
  7145. set_se_golomb(&pb, i - COUNT/2);
  7146. STOP_TIMER("set_se_golomb");
  7147. }
  7148. flush_put_bits(&pb);
  7149. init_get_bits(&gb, temp, 8*SIZE);
  7150. for(i=0; i<COUNT; i++){
  7151. int j, s;
  7152. s= show_bits(&gb, 24);
  7153. START_TIMER
  7154. j= get_se_golomb(&gb);
  7155. if(j != i - COUNT/2){
  7156. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  7157. // return -1;
  7158. }
  7159. STOP_TIMER("get_se_golomb");
  7160. }
  7161. printf("testing 4x4 (I)DCT\n");
  7162. DCTELEM block[16];
  7163. uint8_t src[16], ref[16];
  7164. uint64_t error= 0, max_error=0;
  7165. for(i=0; i<COUNT; i++){
  7166. int j;
  7167. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  7168. for(j=0; j<16; j++){
  7169. ref[j]= random()%255;
  7170. src[j]= random()%255;
  7171. }
  7172. h264_diff_dct_c(block, src, ref, 4);
  7173. //normalize
  7174. for(j=0; j<16; j++){
  7175. // printf("%d ", block[j]);
  7176. block[j]= block[j]*4;
  7177. if(j&1) block[j]= (block[j]*4 + 2)/5;
  7178. if(j&4) block[j]= (block[j]*4 + 2)/5;
  7179. }
  7180. // printf("\n");
  7181. s->dsp.h264_idct_add(ref, block, 4);
  7182. /* for(j=0; j<16; j++){
  7183. printf("%d ", ref[j]);
  7184. }
  7185. printf("\n");*/
  7186. for(j=0; j<16; j++){
  7187. int diff= FFABS(src[j] - ref[j]);
  7188. error+= diff*diff;
  7189. max_error= FFMAX(max_error, diff);
  7190. }
  7191. }
  7192. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  7193. #if 0
  7194. printf("testing quantizer\n");
  7195. for(qp=0; qp<52; qp++){
  7196. for(i=0; i<16; i++)
  7197. src1_block[i]= src2_block[i]= random()%255;
  7198. }
  7199. #endif
  7200. printf("Testing NAL layer\n");
  7201. uint8_t bitstream[COUNT];
  7202. uint8_t nal[COUNT*2];
  7203. H264Context h;
  7204. memset(&h, 0, sizeof(H264Context));
  7205. for(i=0; i<COUNT; i++){
  7206. int zeros= i;
  7207. int nal_length;
  7208. int consumed;
  7209. int out_length;
  7210. uint8_t *out;
  7211. int j;
  7212. for(j=0; j<COUNT; j++){
  7213. bitstream[j]= (random() % 255) + 1;
  7214. }
  7215. for(j=0; j<zeros; j++){
  7216. int pos= random() % COUNT;
  7217. while(bitstream[pos] == 0){
  7218. pos++;
  7219. pos %= COUNT;
  7220. }
  7221. bitstream[pos]=0;
  7222. }
  7223. START_TIMER
  7224. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  7225. if(nal_length<0){
  7226. printf("encoding failed\n");
  7227. return -1;
  7228. }
  7229. out= decode_nal(&h, nal, &out_length, &consumed, nal_length);
  7230. STOP_TIMER("NAL")
  7231. if(out_length != COUNT){
  7232. printf("incorrect length %d %d\n", out_length, COUNT);
  7233. return -1;
  7234. }
  7235. if(consumed != nal_length){
  7236. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  7237. return -1;
  7238. }
  7239. if(memcmp(bitstream, out, COUNT)){
  7240. printf("missmatch\n");
  7241. return -1;
  7242. }
  7243. }
  7244. printf("Testing RBSP\n");
  7245. return 0;
  7246. }
  7247. #endif
  7248. static int decode_end(AVCodecContext *avctx)
  7249. {
  7250. H264Context *h = avctx->priv_data;
  7251. MpegEncContext *s = &h->s;
  7252. av_freep(&h->rbsp_buffer);
  7253. free_tables(h); //FIXME cleanup init stuff perhaps
  7254. MPV_common_end(s);
  7255. // memset(h, 0, sizeof(H264Context));
  7256. return 0;
  7257. }
  7258. AVCodec h264_decoder = {
  7259. "h264",
  7260. CODEC_TYPE_VIDEO,
  7261. CODEC_ID_H264,
  7262. sizeof(H264Context),
  7263. decode_init,
  7264. NULL,
  7265. decode_end,
  7266. decode_frame,
  7267. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  7268. .flush= flush_dpb,
  7269. };
  7270. #include "svq3.c"