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.

8233 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 "dsputil.h"
  28. #include "avcodec.h"
  29. #include "mpegvideo.h"
  30. #include "h264.h"
  31. #include "h264data.h"
  32. #include "h264_parser.h"
  33. #include "golomb.h"
  34. #include "cabac.h"
  35. //#undef NDEBUG
  36. #include <assert.h>
  37. static VLC coeff_token_vlc[4];
  38. static VLC chroma_dc_coeff_token_vlc;
  39. static VLC total_zeros_vlc[15];
  40. static VLC chroma_dc_total_zeros_vlc[3];
  41. static VLC run_vlc[6];
  42. static VLC run7_vlc;
  43. static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
  44. static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
  45. 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);
  46. 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);
  47. static av_always_inline uint32_t pack16to32(int a, int b){
  48. #ifdef WORDS_BIGENDIAN
  49. return (b&0xFFFF) + (a<<16);
  50. #else
  51. return (a&0xFFFF) + (b<<16);
  52. #endif
  53. }
  54. const uint8_t ff_rem6[52]={
  55. 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,
  56. };
  57. const uint8_t ff_div6[52]={
  58. 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,
  59. };
  60. /**
  61. * fill a rectangle.
  62. * @param h height of the rectangle, should be a constant
  63. * @param w width of the rectangle, should be a constant
  64. * @param size the size of val (1 or 4), should be a constant
  65. */
  66. static av_always_inline void fill_rectangle(void *vp, int w, int h, int stride, uint32_t val, int size){
  67. uint8_t *p= (uint8_t*)vp;
  68. assert(size==1 || size==4);
  69. assert(w<=4);
  70. w *= size;
  71. stride *= size;
  72. assert((((long)vp)&(FFMIN(w, STRIDE_ALIGN)-1)) == 0);
  73. assert((stride&(w-1))==0);
  74. if(w==2){
  75. const uint16_t v= size==4 ? val : val*0x0101;
  76. *(uint16_t*)(p + 0*stride)= v;
  77. if(h==1) return;
  78. *(uint16_t*)(p + 1*stride)= v;
  79. if(h==2) return;
  80. *(uint16_t*)(p + 2*stride)=
  81. *(uint16_t*)(p + 3*stride)= v;
  82. }else if(w==4){
  83. const uint32_t v= size==4 ? val : val*0x01010101;
  84. *(uint32_t*)(p + 0*stride)= v;
  85. if(h==1) return;
  86. *(uint32_t*)(p + 1*stride)= v;
  87. if(h==2) return;
  88. *(uint32_t*)(p + 2*stride)=
  89. *(uint32_t*)(p + 3*stride)= v;
  90. }else if(w==8){
  91. //gcc can't optimize 64bit math on x86_32
  92. #if defined(ARCH_X86_64) || (defined(MP_WORDSIZE) && MP_WORDSIZE >= 64)
  93. const uint64_t v= val*0x0100000001ULL;
  94. *(uint64_t*)(p + 0*stride)= v;
  95. if(h==1) return;
  96. *(uint64_t*)(p + 1*stride)= v;
  97. if(h==2) return;
  98. *(uint64_t*)(p + 2*stride)=
  99. *(uint64_t*)(p + 3*stride)= v;
  100. }else if(w==16){
  101. const uint64_t v= val*0x0100000001ULL;
  102. *(uint64_t*)(p + 0+0*stride)=
  103. *(uint64_t*)(p + 8+0*stride)=
  104. *(uint64_t*)(p + 0+1*stride)=
  105. *(uint64_t*)(p + 8+1*stride)= v;
  106. if(h==2) return;
  107. *(uint64_t*)(p + 0+2*stride)=
  108. *(uint64_t*)(p + 8+2*stride)=
  109. *(uint64_t*)(p + 0+3*stride)=
  110. *(uint64_t*)(p + 8+3*stride)= v;
  111. #else
  112. *(uint32_t*)(p + 0+0*stride)=
  113. *(uint32_t*)(p + 4+0*stride)= val;
  114. if(h==1) return;
  115. *(uint32_t*)(p + 0+1*stride)=
  116. *(uint32_t*)(p + 4+1*stride)= val;
  117. if(h==2) return;
  118. *(uint32_t*)(p + 0+2*stride)=
  119. *(uint32_t*)(p + 4+2*stride)=
  120. *(uint32_t*)(p + 0+3*stride)=
  121. *(uint32_t*)(p + 4+3*stride)= val;
  122. }else if(w==16){
  123. *(uint32_t*)(p + 0+0*stride)=
  124. *(uint32_t*)(p + 4+0*stride)=
  125. *(uint32_t*)(p + 8+0*stride)=
  126. *(uint32_t*)(p +12+0*stride)=
  127. *(uint32_t*)(p + 0+1*stride)=
  128. *(uint32_t*)(p + 4+1*stride)=
  129. *(uint32_t*)(p + 8+1*stride)=
  130. *(uint32_t*)(p +12+1*stride)= val;
  131. if(h==2) return;
  132. *(uint32_t*)(p + 0+2*stride)=
  133. *(uint32_t*)(p + 4+2*stride)=
  134. *(uint32_t*)(p + 8+2*stride)=
  135. *(uint32_t*)(p +12+2*stride)=
  136. *(uint32_t*)(p + 0+3*stride)=
  137. *(uint32_t*)(p + 4+3*stride)=
  138. *(uint32_t*)(p + 8+3*stride)=
  139. *(uint32_t*)(p +12+3*stride)= val;
  140. #endif
  141. }else
  142. assert(0);
  143. assert(h==4);
  144. }
  145. static void fill_caches(H264Context *h, int mb_type, int for_deblock){
  146. MpegEncContext * const s = &h->s;
  147. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  148. int topleft_xy, top_xy, topright_xy, left_xy[2];
  149. int topleft_type, top_type, topright_type, left_type[2];
  150. int left_block[8];
  151. int i;
  152. //FIXME deblocking could skip the intra and nnz parts.
  153. if(for_deblock && (h->slice_num == 1 || h->slice_table[mb_xy] == h->slice_table[mb_xy-s->mb_stride]) && !FRAME_MBAFF)
  154. return;
  155. //wow what a mess, why didn't they simplify the interlacing&intra stuff, i can't imagine that these complex rules are worth it
  156. top_xy = mb_xy - s->mb_stride;
  157. topleft_xy = top_xy - 1;
  158. topright_xy= top_xy + 1;
  159. left_xy[1] = left_xy[0] = mb_xy-1;
  160. left_block[0]= 0;
  161. left_block[1]= 1;
  162. left_block[2]= 2;
  163. left_block[3]= 3;
  164. left_block[4]= 7;
  165. left_block[5]= 10;
  166. left_block[6]= 8;
  167. left_block[7]= 11;
  168. if(FRAME_MBAFF){
  169. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  170. const int top_pair_xy = pair_xy - s->mb_stride;
  171. const int topleft_pair_xy = top_pair_xy - 1;
  172. const int topright_pair_xy = top_pair_xy + 1;
  173. const int topleft_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]);
  174. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  175. const int topright_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topright_pair_xy]);
  176. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  177. const int curr_mb_frame_flag = !IS_INTERLACED(mb_type);
  178. const int bottom = (s->mb_y & 1);
  179. 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);
  180. if (bottom
  181. ? !curr_mb_frame_flag // bottom macroblock
  182. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  183. ) {
  184. top_xy -= s->mb_stride;
  185. }
  186. if (bottom
  187. ? !curr_mb_frame_flag // bottom macroblock
  188. : (!curr_mb_frame_flag && !topleft_mb_frame_flag) // top macroblock
  189. ) {
  190. topleft_xy -= s->mb_stride;
  191. }
  192. if (bottom
  193. ? !curr_mb_frame_flag // bottom macroblock
  194. : (!curr_mb_frame_flag && !topright_mb_frame_flag) // top macroblock
  195. ) {
  196. topright_xy -= s->mb_stride;
  197. }
  198. if (left_mb_frame_flag != curr_mb_frame_flag) {
  199. left_xy[1] = left_xy[0] = pair_xy - 1;
  200. if (curr_mb_frame_flag) {
  201. if (bottom) {
  202. left_block[0]= 2;
  203. left_block[1]= 2;
  204. left_block[2]= 3;
  205. left_block[3]= 3;
  206. left_block[4]= 8;
  207. left_block[5]= 11;
  208. left_block[6]= 8;
  209. left_block[7]= 11;
  210. } else {
  211. left_block[0]= 0;
  212. left_block[1]= 0;
  213. left_block[2]= 1;
  214. left_block[3]= 1;
  215. left_block[4]= 7;
  216. left_block[5]= 10;
  217. left_block[6]= 7;
  218. left_block[7]= 10;
  219. }
  220. } else {
  221. left_xy[1] += s->mb_stride;
  222. //left_block[0]= 0;
  223. left_block[1]= 2;
  224. left_block[2]= 0;
  225. left_block[3]= 2;
  226. //left_block[4]= 7;
  227. left_block[5]= 10;
  228. left_block[6]= 7;
  229. left_block[7]= 10;
  230. }
  231. }
  232. }
  233. h->top_mb_xy = top_xy;
  234. h->left_mb_xy[0] = left_xy[0];
  235. h->left_mb_xy[1] = left_xy[1];
  236. if(for_deblock){
  237. topleft_type = 0;
  238. topright_type = 0;
  239. top_type = h->slice_table[top_xy ] < 255 ? s->current_picture.mb_type[top_xy] : 0;
  240. left_type[0] = h->slice_table[left_xy[0] ] < 255 ? s->current_picture.mb_type[left_xy[0]] : 0;
  241. left_type[1] = h->slice_table[left_xy[1] ] < 255 ? s->current_picture.mb_type[left_xy[1]] : 0;
  242. if(FRAME_MBAFF && !IS_INTRA(mb_type)){
  243. int list;
  244. int v = *(uint16_t*)&h->non_zero_count[mb_xy][14];
  245. for(i=0; i<16; i++)
  246. h->non_zero_count_cache[scan8[i]] = (v>>i)&1;
  247. for(list=0; list<h->list_count; list++){
  248. if(USES_LIST(mb_type,list)){
  249. uint32_t *src = (uint32_t*)s->current_picture.motion_val[list][h->mb2b_xy[mb_xy]];
  250. uint32_t *dst = (uint32_t*)h->mv_cache[list][scan8[0]];
  251. int8_t *ref = &s->current_picture.ref_index[list][h->mb2b8_xy[mb_xy]];
  252. for(i=0; i<4; i++, dst+=8, src+=h->b_stride){
  253. dst[0] = src[0];
  254. dst[1] = src[1];
  255. dst[2] = src[2];
  256. dst[3] = src[3];
  257. }
  258. *(uint32_t*)&h->ref_cache[list][scan8[ 0]] =
  259. *(uint32_t*)&h->ref_cache[list][scan8[ 2]] = pack16to32(ref[0],ref[1])*0x0101;
  260. ref += h->b8_stride;
  261. *(uint32_t*)&h->ref_cache[list][scan8[ 8]] =
  262. *(uint32_t*)&h->ref_cache[list][scan8[10]] = pack16to32(ref[0],ref[1])*0x0101;
  263. }else{
  264. fill_rectangle(&h-> mv_cache[list][scan8[ 0]], 4, 4, 8, 0, 4);
  265. fill_rectangle(&h->ref_cache[list][scan8[ 0]], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);
  266. }
  267. }
  268. }
  269. }else{
  270. topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;
  271. top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0;
  272. topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;
  273. left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;
  274. left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;
  275. }
  276. if(IS_INTRA(mb_type)){
  277. h->topleft_samples_available=
  278. h->top_samples_available=
  279. h->left_samples_available= 0xFFFF;
  280. h->topright_samples_available= 0xEEEA;
  281. if(!IS_INTRA(top_type) && (top_type==0 || h->pps.constrained_intra_pred)){
  282. h->topleft_samples_available= 0xB3FF;
  283. h->top_samples_available= 0x33FF;
  284. h->topright_samples_available= 0x26EA;
  285. }
  286. for(i=0; i<2; i++){
  287. if(!IS_INTRA(left_type[i]) && (left_type[i]==0 || h->pps.constrained_intra_pred)){
  288. h->topleft_samples_available&= 0xDF5F;
  289. h->left_samples_available&= 0x5F5F;
  290. }
  291. }
  292. if(!IS_INTRA(topleft_type) && (topleft_type==0 || h->pps.constrained_intra_pred))
  293. h->topleft_samples_available&= 0x7FFF;
  294. if(!IS_INTRA(topright_type) && (topright_type==0 || h->pps.constrained_intra_pred))
  295. h->topright_samples_available&= 0xFBFF;
  296. if(IS_INTRA4x4(mb_type)){
  297. if(IS_INTRA4x4(top_type)){
  298. h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];
  299. h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];
  300. h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];
  301. h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];
  302. }else{
  303. int pred;
  304. if(!top_type || (IS_INTER(top_type) && h->pps.constrained_intra_pred))
  305. pred= -1;
  306. else{
  307. pred= 2;
  308. }
  309. h->intra4x4_pred_mode_cache[4+8*0]=
  310. h->intra4x4_pred_mode_cache[5+8*0]=
  311. h->intra4x4_pred_mode_cache[6+8*0]=
  312. h->intra4x4_pred_mode_cache[7+8*0]= pred;
  313. }
  314. for(i=0; i<2; i++){
  315. if(IS_INTRA4x4(left_type[i])){
  316. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];
  317. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];
  318. }else{
  319. int pred;
  320. if(!left_type[i] || (IS_INTER(left_type[i]) && h->pps.constrained_intra_pred))
  321. pred= -1;
  322. else{
  323. pred= 2;
  324. }
  325. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
  326. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;
  327. }
  328. }
  329. }
  330. }
  331. /*
  332. 0 . T T. T T T T
  333. 1 L . .L . . . .
  334. 2 L . .L . . . .
  335. 3 . T TL . . . .
  336. 4 L . .L . . . .
  337. 5 L . .. . . . .
  338. */
  339. //FIXME constraint_intra_pred & partitioning & nnz (lets hope this is just a typo in the spec)
  340. if(top_type){
  341. h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][4];
  342. h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][5];
  343. h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][6];
  344. h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3];
  345. h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][9];
  346. h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8];
  347. h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][12];
  348. h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11];
  349. }else{
  350. h->non_zero_count_cache[4+8*0]=
  351. h->non_zero_count_cache[5+8*0]=
  352. h->non_zero_count_cache[6+8*0]=
  353. h->non_zero_count_cache[7+8*0]=
  354. h->non_zero_count_cache[1+8*0]=
  355. h->non_zero_count_cache[2+8*0]=
  356. h->non_zero_count_cache[1+8*3]=
  357. h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  358. }
  359. for (i=0; i<2; i++) {
  360. if(left_type[i]){
  361. h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[0+2*i]];
  362. h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[1+2*i]];
  363. h->non_zero_count_cache[0+8*1 + 8*i]= h->non_zero_count[left_xy[i]][left_block[4+2*i]];
  364. h->non_zero_count_cache[0+8*4 + 8*i]= h->non_zero_count[left_xy[i]][left_block[5+2*i]];
  365. }else{
  366. h->non_zero_count_cache[3+8*1 + 2*8*i]=
  367. h->non_zero_count_cache[3+8*2 + 2*8*i]=
  368. h->non_zero_count_cache[0+8*1 + 8*i]=
  369. h->non_zero_count_cache[0+8*4 + 8*i]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  370. }
  371. }
  372. if( h->pps.cabac ) {
  373. // top_cbp
  374. if(top_type) {
  375. h->top_cbp = h->cbp_table[top_xy];
  376. } else if(IS_INTRA(mb_type)) {
  377. h->top_cbp = 0x1C0;
  378. } else {
  379. h->top_cbp = 0;
  380. }
  381. // left_cbp
  382. if (left_type[0]) {
  383. h->left_cbp = h->cbp_table[left_xy[0]] & 0x1f0;
  384. } else if(IS_INTRA(mb_type)) {
  385. h->left_cbp = 0x1C0;
  386. } else {
  387. h->left_cbp = 0;
  388. }
  389. if (left_type[0]) {
  390. h->left_cbp |= ((h->cbp_table[left_xy[0]]>>((left_block[0]&(~1))+1))&0x1) << 1;
  391. }
  392. if (left_type[1]) {
  393. h->left_cbp |= ((h->cbp_table[left_xy[1]]>>((left_block[2]&(~1))+1))&0x1) << 3;
  394. }
  395. }
  396. #if 1
  397. if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
  398. int list;
  399. for(list=0; list<h->list_count; list++){
  400. if(!USES_LIST(mb_type, list) && !IS_DIRECT(mb_type) && !h->deblocking_filter){
  401. /*if(!h->mv_cache_clean[list]){
  402. memset(h->mv_cache [list], 0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all?
  403. memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
  404. h->mv_cache_clean[list]= 1;
  405. }*/
  406. continue;
  407. }
  408. h->mv_cache_clean[list]= 0;
  409. if(USES_LIST(top_type, list)){
  410. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  411. const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;
  412. *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0];
  413. *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1];
  414. *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2];
  415. *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3];
  416. h->ref_cache[list][scan8[0] + 0 - 1*8]=
  417. h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];
  418. h->ref_cache[list][scan8[0] + 2 - 1*8]=
  419. h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];
  420. }else{
  421. *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]=
  422. *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]=
  423. *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]=
  424. *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0;
  425. *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;
  426. }
  427. for(i=0; i<2; i++){
  428. int cache_idx = scan8[0] - 1 + i*2*8;
  429. if(USES_LIST(left_type[i], list)){
  430. const int b_xy= h->mb2b_xy[left_xy[i]] + 3;
  431. const int b8_xy= h->mb2b8_xy[left_xy[i]] + 1;
  432. *(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]];
  433. *(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]];
  434. h->ref_cache[list][cache_idx ]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0+i*2]>>1)];
  435. h->ref_cache[list][cache_idx+8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[1+i*2]>>1)];
  436. }else{
  437. *(uint32_t*)h->mv_cache [list][cache_idx ]=
  438. *(uint32_t*)h->mv_cache [list][cache_idx+8]= 0;
  439. h->ref_cache[list][cache_idx ]=
  440. h->ref_cache[list][cache_idx+8]= left_type[i] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  441. }
  442. }
  443. if((for_deblock || (IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred)) && !FRAME_MBAFF)
  444. continue;
  445. if(USES_LIST(topleft_type, list)){
  446. const int b_xy = h->mb2b_xy[topleft_xy] + 3 + 3*h->b_stride;
  447. const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + h->b8_stride;
  448. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  449. h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  450. }else{
  451. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;
  452. h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  453. }
  454. if(USES_LIST(topright_type, list)){
  455. const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
  456. const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;
  457. *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  458. h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  459. }else{
  460. *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;
  461. h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  462. }
  463. if((IS_SKIP(mb_type) || IS_DIRECT(mb_type)) && !FRAME_MBAFF)
  464. continue;
  465. h->ref_cache[list][scan8[5 ]+1] =
  466. h->ref_cache[list][scan8[7 ]+1] =
  467. h->ref_cache[list][scan8[13]+1] = //FIXME remove past 3 (init somewhere else)
  468. h->ref_cache[list][scan8[4 ]] =
  469. h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
  470. *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=
  471. *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=
  472. *(uint32_t*)h->mv_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  473. *(uint32_t*)h->mv_cache [list][scan8[4 ]]=
  474. *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;
  475. if( h->pps.cabac ) {
  476. /* XXX beurk, Load mvd */
  477. if(USES_LIST(top_type, list)){
  478. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  479. *(uint32_t*)h->mvd_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 0];
  480. *(uint32_t*)h->mvd_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 1];
  481. *(uint32_t*)h->mvd_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 2];
  482. *(uint32_t*)h->mvd_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 3];
  483. }else{
  484. *(uint32_t*)h->mvd_cache [list][scan8[0] + 0 - 1*8]=
  485. *(uint32_t*)h->mvd_cache [list][scan8[0] + 1 - 1*8]=
  486. *(uint32_t*)h->mvd_cache [list][scan8[0] + 2 - 1*8]=
  487. *(uint32_t*)h->mvd_cache [list][scan8[0] + 3 - 1*8]= 0;
  488. }
  489. if(USES_LIST(left_type[0], list)){
  490. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  491. *(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]];
  492. *(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]];
  493. }else{
  494. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 0*8]=
  495. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 1*8]= 0;
  496. }
  497. if(USES_LIST(left_type[1], list)){
  498. const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
  499. *(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]];
  500. *(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]];
  501. }else{
  502. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 2*8]=
  503. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 3*8]= 0;
  504. }
  505. *(uint32_t*)h->mvd_cache [list][scan8[5 ]+1]=
  506. *(uint32_t*)h->mvd_cache [list][scan8[7 ]+1]=
  507. *(uint32_t*)h->mvd_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  508. *(uint32_t*)h->mvd_cache [list][scan8[4 ]]=
  509. *(uint32_t*)h->mvd_cache [list][scan8[12]]= 0;
  510. if(h->slice_type == B_TYPE){
  511. fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1);
  512. if(IS_DIRECT(top_type)){
  513. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101;
  514. }else if(IS_8X8(top_type)){
  515. int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride;
  516. h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy];
  517. h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1];
  518. }else{
  519. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0;
  520. }
  521. if(IS_DIRECT(left_type[0]))
  522. h->direct_cache[scan8[0] - 1 + 0*8]= 1;
  523. else if(IS_8X8(left_type[0]))
  524. 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)];
  525. else
  526. h->direct_cache[scan8[0] - 1 + 0*8]= 0;
  527. if(IS_DIRECT(left_type[1]))
  528. h->direct_cache[scan8[0] - 1 + 2*8]= 1;
  529. else if(IS_8X8(left_type[1]))
  530. 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)];
  531. else
  532. h->direct_cache[scan8[0] - 1 + 2*8]= 0;
  533. }
  534. }
  535. if(FRAME_MBAFF){
  536. #define MAP_MVS\
  537. MAP_F2F(scan8[0] - 1 - 1*8, topleft_type)\
  538. MAP_F2F(scan8[0] + 0 - 1*8, top_type)\
  539. MAP_F2F(scan8[0] + 1 - 1*8, top_type)\
  540. MAP_F2F(scan8[0] + 2 - 1*8, top_type)\
  541. MAP_F2F(scan8[0] + 3 - 1*8, top_type)\
  542. MAP_F2F(scan8[0] + 4 - 1*8, topright_type)\
  543. MAP_F2F(scan8[0] - 1 + 0*8, left_type[0])\
  544. MAP_F2F(scan8[0] - 1 + 1*8, left_type[0])\
  545. MAP_F2F(scan8[0] - 1 + 2*8, left_type[1])\
  546. MAP_F2F(scan8[0] - 1 + 3*8, left_type[1])
  547. if(MB_FIELD){
  548. #define MAP_F2F(idx, mb_type)\
  549. if(!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  550. h->ref_cache[list][idx] <<= 1;\
  551. h->mv_cache[list][idx][1] /= 2;\
  552. h->mvd_cache[list][idx][1] /= 2;\
  553. }
  554. MAP_MVS
  555. #undef MAP_F2F
  556. }else{
  557. #define MAP_F2F(idx, mb_type)\
  558. if(IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  559. h->ref_cache[list][idx] >>= 1;\
  560. h->mv_cache[list][idx][1] <<= 1;\
  561. h->mvd_cache[list][idx][1] <<= 1;\
  562. }
  563. MAP_MVS
  564. #undef MAP_F2F
  565. }
  566. }
  567. }
  568. }
  569. #endif
  570. h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);
  571. }
  572. static inline void write_back_intra_pred_mode(H264Context *h){
  573. MpegEncContext * const s = &h->s;
  574. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  575. h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
  576. h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
  577. h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
  578. h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
  579. h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
  580. h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
  581. h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
  582. }
  583. /**
  584. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  585. */
  586. static inline int check_intra4x4_pred_mode(H264Context *h){
  587. MpegEncContext * const s = &h->s;
  588. static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
  589. static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
  590. int i;
  591. if(!(h->top_samples_available&0x8000)){
  592. for(i=0; i<4; i++){
  593. int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
  594. if(status<0){
  595. 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);
  596. return -1;
  597. } else if(status){
  598. h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
  599. }
  600. }
  601. }
  602. if(!(h->left_samples_available&0x8000)){
  603. for(i=0; i<4; i++){
  604. int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
  605. if(status<0){
  606. 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);
  607. return -1;
  608. } else if(status){
  609. h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
  610. }
  611. }
  612. }
  613. return 0;
  614. } //FIXME cleanup like next
  615. /**
  616. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  617. */
  618. static inline int check_intra_pred_mode(H264Context *h, int mode){
  619. MpegEncContext * const s = &h->s;
  620. static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
  621. static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
  622. if(mode > 6U) {
  623. 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);
  624. return -1;
  625. }
  626. if(!(h->top_samples_available&0x8000)){
  627. mode= top[ mode ];
  628. if(mode<0){
  629. 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);
  630. return -1;
  631. }
  632. }
  633. if(!(h->left_samples_available&0x8000)){
  634. mode= left[ mode ];
  635. if(mode<0){
  636. 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);
  637. return -1;
  638. }
  639. }
  640. return mode;
  641. }
  642. /**
  643. * gets the predicted intra4x4 prediction mode.
  644. */
  645. static inline int pred_intra_mode(H264Context *h, int n){
  646. const int index8= scan8[n];
  647. const int left= h->intra4x4_pred_mode_cache[index8 - 1];
  648. const int top = h->intra4x4_pred_mode_cache[index8 - 8];
  649. const int min= FFMIN(left, top);
  650. tprintf(h->s.avctx, "mode:%d %d min:%d\n", left ,top, min);
  651. if(min<0) return DC_PRED;
  652. else return min;
  653. }
  654. static inline void write_back_non_zero_count(H264Context *h){
  655. MpegEncContext * const s = &h->s;
  656. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  657. h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[7+8*1];
  658. h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[7+8*2];
  659. h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[7+8*3];
  660. h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4];
  661. h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[4+8*4];
  662. h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[5+8*4];
  663. h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[6+8*4];
  664. h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[1+8*2];
  665. h->non_zero_count[mb_xy][8]= h->non_zero_count_cache[2+8*2];
  666. h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[2+8*1];
  667. h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[1+8*5];
  668. h->non_zero_count[mb_xy][11]=h->non_zero_count_cache[2+8*5];
  669. h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[2+8*4];
  670. if(FRAME_MBAFF){
  671. // store all luma nnzs, for deblocking
  672. int v = 0, i;
  673. for(i=0; i<16; i++)
  674. v += (!!h->non_zero_count_cache[scan8[i]]) << i;
  675. *(uint16_t*)&h->non_zero_count[mb_xy][14] = v;
  676. }
  677. }
  678. /**
  679. * gets the predicted number of non zero coefficients.
  680. * @param n block index
  681. */
  682. static inline int pred_non_zero_count(H264Context *h, int n){
  683. const int index8= scan8[n];
  684. const int left= h->non_zero_count_cache[index8 - 1];
  685. const int top = h->non_zero_count_cache[index8 - 8];
  686. int i= left + top;
  687. if(i<64) i= (i+1)>>1;
  688. tprintf(h->s.avctx, "pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
  689. return i&31;
  690. }
  691. static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
  692. const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
  693. MpegEncContext *s = &h->s;
  694. /* there is no consistent mapping of mvs to neighboring locations that will
  695. * make mbaff happy, so we can't move all this logic to fill_caches */
  696. if(FRAME_MBAFF){
  697. const uint32_t *mb_types = s->current_picture_ptr->mb_type;
  698. const int16_t *mv;
  699. *(uint32_t*)h->mv_cache[list][scan8[0]-2] = 0;
  700. *C = h->mv_cache[list][scan8[0]-2];
  701. if(!MB_FIELD
  702. && (s->mb_y&1) && i < scan8[0]+8 && topright_ref != PART_NOT_AVAILABLE){
  703. int topright_xy = s->mb_x + (s->mb_y-1)*s->mb_stride + (i == scan8[0]+3);
  704. if(IS_INTERLACED(mb_types[topright_xy])){
  705. #define SET_DIAG_MV(MV_OP, REF_OP, X4, Y4)\
  706. const int x4 = X4, y4 = Y4;\
  707. const int mb_type = mb_types[(x4>>2)+(y4>>2)*s->mb_stride];\
  708. if(!USES_LIST(mb_type,list) && !IS_8X8(mb_type))\
  709. return LIST_NOT_USED;\
  710. mv = s->current_picture_ptr->motion_val[list][x4 + y4*h->b_stride];\
  711. h->mv_cache[list][scan8[0]-2][0] = mv[0];\
  712. h->mv_cache[list][scan8[0]-2][1] = mv[1] MV_OP;\
  713. return s->current_picture_ptr->ref_index[list][(x4>>1) + (y4>>1)*h->b8_stride] REF_OP;
  714. SET_DIAG_MV(*2, >>1, s->mb_x*4+(i&7)-4+part_width, s->mb_y*4-1);
  715. }
  716. }
  717. if(topright_ref == PART_NOT_AVAILABLE
  718. && ((s->mb_y&1) || i >= scan8[0]+8) && (i&7)==4
  719. && h->ref_cache[list][scan8[0]-1] != PART_NOT_AVAILABLE){
  720. if(!MB_FIELD
  721. && IS_INTERLACED(mb_types[h->left_mb_xy[0]])){
  722. SET_DIAG_MV(*2, >>1, s->mb_x*4-1, (s->mb_y|1)*4+(s->mb_y&1)*2+(i>>4)-1);
  723. }
  724. if(MB_FIELD
  725. && !IS_INTERLACED(mb_types[h->left_mb_xy[0]])
  726. && i >= scan8[0]+8){
  727. // leftshift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's ok.
  728. SET_DIAG_MV(>>1, <<1, s->mb_x*4-1, (s->mb_y&~1)*4 - 1 + ((i-scan8[0])>>3)*2);
  729. }
  730. }
  731. #undef SET_DIAG_MV
  732. }
  733. if(topright_ref != PART_NOT_AVAILABLE){
  734. *C= h->mv_cache[list][ i - 8 + part_width ];
  735. return topright_ref;
  736. }else{
  737. tprintf(s->avctx, "topright MV not available\n");
  738. *C= h->mv_cache[list][ i - 8 - 1 ];
  739. return h->ref_cache[list][ i - 8 - 1 ];
  740. }
  741. }
  742. /**
  743. * gets the predicted MV.
  744. * @param n the block index
  745. * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
  746. * @param mx the x component of the predicted motion vector
  747. * @param my the y component of the predicted motion vector
  748. */
  749. static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
  750. const int index8= scan8[n];
  751. const int top_ref= h->ref_cache[list][ index8 - 8 ];
  752. const int left_ref= h->ref_cache[list][ index8 - 1 ];
  753. const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
  754. const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
  755. const int16_t * C;
  756. int diagonal_ref, match_count;
  757. assert(part_width==1 || part_width==2 || part_width==4);
  758. /* mv_cache
  759. B . . A T T T T
  760. U . . L . . , .
  761. U . . L . . . .
  762. U . . L . . , .
  763. . . . L . . . .
  764. */
  765. diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
  766. match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
  767. tprintf(h->s.avctx, "pred_motion match_count=%d\n", match_count);
  768. if(match_count > 1){ //most common
  769. *mx= mid_pred(A[0], B[0], C[0]);
  770. *my= mid_pred(A[1], B[1], C[1]);
  771. }else if(match_count==1){
  772. if(left_ref==ref){
  773. *mx= A[0];
  774. *my= A[1];
  775. }else if(top_ref==ref){
  776. *mx= B[0];
  777. *my= B[1];
  778. }else{
  779. *mx= C[0];
  780. *my= C[1];
  781. }
  782. }else{
  783. if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
  784. *mx= A[0];
  785. *my= A[1];
  786. }else{
  787. *mx= mid_pred(A[0], B[0], C[0]);
  788. *my= mid_pred(A[1], B[1], C[1]);
  789. }
  790. }
  791. 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);
  792. }
  793. /**
  794. * gets the directionally predicted 16x8 MV.
  795. * @param n the block index
  796. * @param mx the x component of the predicted motion vector
  797. * @param my the y component of the predicted motion vector
  798. */
  799. static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  800. if(n==0){
  801. const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
  802. const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
  803. 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);
  804. if(top_ref == ref){
  805. *mx= B[0];
  806. *my= B[1];
  807. return;
  808. }
  809. }else{
  810. const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
  811. const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
  812. 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);
  813. if(left_ref == ref){
  814. *mx= A[0];
  815. *my= A[1];
  816. return;
  817. }
  818. }
  819. //RARE
  820. pred_motion(h, n, 4, list, ref, mx, my);
  821. }
  822. /**
  823. * gets the directionally predicted 8x16 MV.
  824. * @param n the block index
  825. * @param mx the x component of the predicted motion vector
  826. * @param my the y component of the predicted motion vector
  827. */
  828. static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  829. if(n==0){
  830. const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
  831. const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
  832. 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);
  833. if(left_ref == ref){
  834. *mx= A[0];
  835. *my= A[1];
  836. return;
  837. }
  838. }else{
  839. const int16_t * C;
  840. int diagonal_ref;
  841. diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
  842. 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);
  843. if(diagonal_ref == ref){
  844. *mx= C[0];
  845. *my= C[1];
  846. return;
  847. }
  848. }
  849. //RARE
  850. pred_motion(h, n, 2, list, ref, mx, my);
  851. }
  852. static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
  853. const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
  854. const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
  855. tprintf(h->s.avctx, "pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
  856. if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
  857. || (top_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ] == 0)
  858. || (left_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ] == 0)){
  859. *mx = *my = 0;
  860. return;
  861. }
  862. pred_motion(h, 0, 4, 0, 0, mx, my);
  863. return;
  864. }
  865. static inline void direct_dist_scale_factor(H264Context * const h){
  866. const int poc = h->s.current_picture_ptr->poc;
  867. const int poc1 = h->ref_list[1][0].poc;
  868. int i;
  869. for(i=0; i<h->ref_count[0]; i++){
  870. int poc0 = h->ref_list[0][i].poc;
  871. int td = av_clip(poc1 - poc0, -128, 127);
  872. if(td == 0 /* FIXME || pic0 is a long-term ref */){
  873. h->dist_scale_factor[i] = 256;
  874. }else{
  875. int tb = av_clip(poc - poc0, -128, 127);
  876. int tx = (16384 + (FFABS(td) >> 1)) / td;
  877. h->dist_scale_factor[i] = av_clip((tb*tx + 32) >> 6, -1024, 1023);
  878. }
  879. }
  880. if(FRAME_MBAFF){
  881. for(i=0; i<h->ref_count[0]; i++){
  882. h->dist_scale_factor_field[2*i] =
  883. h->dist_scale_factor_field[2*i+1] = h->dist_scale_factor[i];
  884. }
  885. }
  886. }
  887. static inline void direct_ref_list_init(H264Context * const h){
  888. MpegEncContext * const s = &h->s;
  889. Picture * const ref1 = &h->ref_list[1][0];
  890. Picture * const cur = s->current_picture_ptr;
  891. int list, i, j;
  892. if(cur->pict_type == I_TYPE)
  893. cur->ref_count[0] = 0;
  894. if(cur->pict_type != B_TYPE)
  895. cur->ref_count[1] = 0;
  896. for(list=0; list<2; list++){
  897. cur->ref_count[list] = h->ref_count[list];
  898. for(j=0; j<h->ref_count[list]; j++)
  899. cur->ref_poc[list][j] = h->ref_list[list][j].poc;
  900. }
  901. if(cur->pict_type != B_TYPE || h->direct_spatial_mv_pred)
  902. return;
  903. for(list=0; list<2; list++){
  904. for(i=0; i<ref1->ref_count[list]; i++){
  905. const int poc = ref1->ref_poc[list][i];
  906. h->map_col_to_list0[list][i] = 0; /* bogus; fills in for missing frames */
  907. for(j=0; j<h->ref_count[list]; j++)
  908. if(h->ref_list[list][j].poc == poc){
  909. h->map_col_to_list0[list][i] = j;
  910. break;
  911. }
  912. }
  913. }
  914. if(FRAME_MBAFF){
  915. for(list=0; list<2; list++){
  916. for(i=0; i<ref1->ref_count[list]; i++){
  917. j = h->map_col_to_list0[list][i];
  918. h->map_col_to_list0_field[list][2*i] = 2*j;
  919. h->map_col_to_list0_field[list][2*i+1] = 2*j+1;
  920. }
  921. }
  922. }
  923. }
  924. static inline void pred_direct_motion(H264Context * const h, int *mb_type){
  925. MpegEncContext * const s = &h->s;
  926. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  927. const int b8_xy = 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  928. const int b4_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  929. const int mb_type_col = h->ref_list[1][0].mb_type[mb_xy];
  930. const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[0][b4_xy];
  931. const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[1][b4_xy];
  932. const int8_t *l1ref0 = &h->ref_list[1][0].ref_index[0][b8_xy];
  933. const int8_t *l1ref1 = &h->ref_list[1][0].ref_index[1][b8_xy];
  934. const int is_b8x8 = IS_8X8(*mb_type);
  935. unsigned int sub_mb_type;
  936. int i8, i4;
  937. #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
  938. if(IS_8X8(mb_type_col) && !h->sps.direct_8x8_inference_flag){
  939. /* FIXME save sub mb types from previous frames (or derive from MVs)
  940. * so we know exactly what block size to use */
  941. sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
  942. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
  943. }else if(!is_b8x8 && (mb_type_col & MB_TYPE_16x16_OR_INTRA)){
  944. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  945. *mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
  946. }else{
  947. sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
  948. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
  949. }
  950. if(!is_b8x8)
  951. *mb_type |= MB_TYPE_DIRECT2;
  952. if(MB_FIELD)
  953. *mb_type |= MB_TYPE_INTERLACED;
  954. 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);
  955. if(h->direct_spatial_mv_pred){
  956. int ref[2];
  957. int mv[2][2];
  958. int list;
  959. /* FIXME interlacing + spatial direct uses wrong colocated block positions */
  960. /* ref = min(neighbors) */
  961. for(list=0; list<2; list++){
  962. int refa = h->ref_cache[list][scan8[0] - 1];
  963. int refb = h->ref_cache[list][scan8[0] - 8];
  964. int refc = h->ref_cache[list][scan8[0] - 8 + 4];
  965. if(refc == -2)
  966. refc = h->ref_cache[list][scan8[0] - 8 - 1];
  967. ref[list] = refa;
  968. if(ref[list] < 0 || (refb < ref[list] && refb >= 0))
  969. ref[list] = refb;
  970. if(ref[list] < 0 || (refc < ref[list] && refc >= 0))
  971. ref[list] = refc;
  972. if(ref[list] < 0)
  973. ref[list] = -1;
  974. }
  975. if(ref[0] < 0 && ref[1] < 0){
  976. ref[0] = ref[1] = 0;
  977. mv[0][0] = mv[0][1] =
  978. mv[1][0] = mv[1][1] = 0;
  979. }else{
  980. for(list=0; list<2; list++){
  981. if(ref[list] >= 0)
  982. pred_motion(h, 0, 4, list, ref[list], &mv[list][0], &mv[list][1]);
  983. else
  984. mv[list][0] = mv[list][1] = 0;
  985. }
  986. }
  987. if(ref[1] < 0){
  988. *mb_type &= ~MB_TYPE_P0L1;
  989. sub_mb_type &= ~MB_TYPE_P0L1;
  990. }else if(ref[0] < 0){
  991. *mb_type &= ~MB_TYPE_P0L0;
  992. sub_mb_type &= ~MB_TYPE_P0L0;
  993. }
  994. if(IS_16X16(*mb_type)){
  995. int a=0, b=0;
  996. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
  997. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
  998. if(!IS_INTRA(mb_type_col)
  999. && ( (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
  1000. || (l1ref0[0] < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
  1001. && (h->x264_build>33 || !h->x264_build)))){
  1002. if(ref[0] > 0)
  1003. a= pack16to32(mv[0][0],mv[0][1]);
  1004. if(ref[1] > 0)
  1005. b= pack16to32(mv[1][0],mv[1][1]);
  1006. }else{
  1007. a= pack16to32(mv[0][0],mv[0][1]);
  1008. b= pack16to32(mv[1][0],mv[1][1]);
  1009. }
  1010. fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
  1011. fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
  1012. }else{
  1013. for(i8=0; i8<4; i8++){
  1014. const int x8 = i8&1;
  1015. const int y8 = i8>>1;
  1016. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1017. continue;
  1018. h->sub_mb_type[i8] = sub_mb_type;
  1019. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4);
  1020. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4);
  1021. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
  1022. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
  1023. /* col_zero_flag */
  1024. if(!IS_INTRA(mb_type_col) && ( l1ref0[x8 + y8*h->b8_stride] == 0
  1025. || (l1ref0[x8 + y8*h->b8_stride] < 0 && l1ref1[x8 + y8*h->b8_stride] == 0
  1026. && (h->x264_build>33 || !h->x264_build)))){
  1027. const int16_t (*l1mv)[2]= l1ref0[x8 + y8*h->b8_stride] == 0 ? l1mv0 : l1mv1;
  1028. if(IS_SUB_8X8(sub_mb_type)){
  1029. const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
  1030. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  1031. if(ref[0] == 0)
  1032. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1033. if(ref[1] == 0)
  1034. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1035. }
  1036. }else
  1037. for(i4=0; i4<4; i4++){
  1038. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
  1039. if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
  1040. if(ref[0] == 0)
  1041. *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
  1042. if(ref[1] == 0)
  1043. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
  1044. }
  1045. }
  1046. }
  1047. }
  1048. }
  1049. }else{ /* direct temporal mv pred */
  1050. const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
  1051. const int *dist_scale_factor = h->dist_scale_factor;
  1052. if(FRAME_MBAFF){
  1053. if(IS_INTERLACED(*mb_type)){
  1054. map_col_to_list0[0] = h->map_col_to_list0_field[0];
  1055. map_col_to_list0[1] = h->map_col_to_list0_field[1];
  1056. dist_scale_factor = h->dist_scale_factor_field;
  1057. }
  1058. if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col)){
  1059. /* FIXME assumes direct_8x8_inference == 1 */
  1060. const int pair_xy = s->mb_x + (s->mb_y&~1)*s->mb_stride;
  1061. int mb_types_col[2];
  1062. int y_shift;
  1063. *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1
  1064. | (is_b8x8 ? 0 : MB_TYPE_DIRECT2)
  1065. | (*mb_type & MB_TYPE_INTERLACED);
  1066. sub_mb_type = MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_16x16;
  1067. if(IS_INTERLACED(*mb_type)){
  1068. /* frame to field scaling */
  1069. mb_types_col[0] = h->ref_list[1][0].mb_type[pair_xy];
  1070. mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
  1071. if(s->mb_y&1){
  1072. l1ref0 -= 2*h->b8_stride;
  1073. l1ref1 -= 2*h->b8_stride;
  1074. l1mv0 -= 4*h->b_stride;
  1075. l1mv1 -= 4*h->b_stride;
  1076. }
  1077. y_shift = 0;
  1078. if( (mb_types_col[0] & MB_TYPE_16x16_OR_INTRA)
  1079. && (mb_types_col[1] & MB_TYPE_16x16_OR_INTRA)
  1080. && !is_b8x8)
  1081. *mb_type |= MB_TYPE_16x8;
  1082. else
  1083. *mb_type |= MB_TYPE_8x8;
  1084. }else{
  1085. /* field to frame scaling */
  1086. /* col_mb_y = (mb_y&~1) + (topAbsDiffPOC < bottomAbsDiffPOC ? 0 : 1)
  1087. * but in MBAFF, top and bottom POC are equal */
  1088. int dy = (s->mb_y&1) ? 1 : 2;
  1089. mb_types_col[0] =
  1090. mb_types_col[1] = h->ref_list[1][0].mb_type[pair_xy+s->mb_stride];
  1091. l1ref0 += dy*h->b8_stride;
  1092. l1ref1 += dy*h->b8_stride;
  1093. l1mv0 += 2*dy*h->b_stride;
  1094. l1mv1 += 2*dy*h->b_stride;
  1095. y_shift = 2;
  1096. if((mb_types_col[0] & (MB_TYPE_16x16_OR_INTRA|MB_TYPE_16x8))
  1097. && !is_b8x8)
  1098. *mb_type |= MB_TYPE_16x16;
  1099. else
  1100. *mb_type |= MB_TYPE_8x8;
  1101. }
  1102. for(i8=0; i8<4; i8++){
  1103. const int x8 = i8&1;
  1104. const int y8 = i8>>1;
  1105. int ref0, scale;
  1106. const int16_t (*l1mv)[2]= l1mv0;
  1107. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1108. continue;
  1109. h->sub_mb_type[i8] = sub_mb_type;
  1110. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1111. if(IS_INTRA(mb_types_col[y8])){
  1112. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  1113. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1114. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1115. continue;
  1116. }
  1117. ref0 = l1ref0[x8 + (y8*2>>y_shift)*h->b8_stride];
  1118. if(ref0 >= 0)
  1119. ref0 = map_col_to_list0[0][ref0*2>>y_shift];
  1120. else{
  1121. ref0 = map_col_to_list0[1][l1ref1[x8 + (y8*2>>y_shift)*h->b8_stride]*2>>y_shift];
  1122. l1mv= l1mv1;
  1123. }
  1124. scale = dist_scale_factor[ref0];
  1125. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  1126. {
  1127. const int16_t *mv_col = l1mv[x8*3 + (y8*6>>y_shift)*h->b_stride];
  1128. int my_col = (mv_col[1]<<y_shift)/2;
  1129. int mx = (scale * mv_col[0] + 128) >> 8;
  1130. int my = (scale * my_col + 128) >> 8;
  1131. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  1132. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
  1133. }
  1134. }
  1135. return;
  1136. }
  1137. }
  1138. /* one-to-one mv scaling */
  1139. if(IS_16X16(*mb_type)){
  1140. int ref, mv0, mv1;
  1141. fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
  1142. if(IS_INTRA(mb_type_col)){
  1143. ref=mv0=mv1=0;
  1144. }else{
  1145. const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0]]
  1146. : map_col_to_list0[1][l1ref1[0]];
  1147. const int scale = dist_scale_factor[ref0];
  1148. const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
  1149. int mv_l0[2];
  1150. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  1151. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  1152. ref= ref0;
  1153. mv0= pack16to32(mv_l0[0],mv_l0[1]);
  1154. mv1= pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
  1155. }
  1156. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
  1157. fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
  1158. fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
  1159. }else{
  1160. for(i8=0; i8<4; i8++){
  1161. const int x8 = i8&1;
  1162. const int y8 = i8>>1;
  1163. int ref0, scale;
  1164. const int16_t (*l1mv)[2]= l1mv0;
  1165. if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
  1166. continue;
  1167. h->sub_mb_type[i8] = sub_mb_type;
  1168. fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
  1169. if(IS_INTRA(mb_type_col)){
  1170. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
  1171. fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
  1172. fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
  1173. continue;
  1174. }
  1175. ref0 = l1ref0[x8 + y8*h->b8_stride];
  1176. if(ref0 >= 0)
  1177. ref0 = map_col_to_list0[0][ref0];
  1178. else{
  1179. ref0 = map_col_to_list0[1][l1ref1[x8 + y8*h->b8_stride]];
  1180. l1mv= l1mv1;
  1181. }
  1182. scale = dist_scale_factor[ref0];
  1183. fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
  1184. if(IS_SUB_8X8(sub_mb_type)){
  1185. const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
  1186. int mx = (scale * mv_col[0] + 128) >> 8;
  1187. int my = (scale * mv_col[1] + 128) >> 8;
  1188. fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
  1189. fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
  1190. }else
  1191. for(i4=0; i4<4; i4++){
  1192. const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
  1193. int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
  1194. mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
  1195. mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
  1196. *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =
  1197. pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
  1198. }
  1199. }
  1200. }
  1201. }
  1202. }
  1203. static inline void write_back_motion(H264Context *h, int mb_type){
  1204. MpegEncContext * const s = &h->s;
  1205. const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  1206. const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  1207. int list;
  1208. if(!USES_LIST(mb_type, 0))
  1209. fill_rectangle(&s->current_picture.ref_index[0][b8_xy], 2, 2, h->b8_stride, (uint8_t)LIST_NOT_USED, 1);
  1210. for(list=0; list<h->list_count; list++){
  1211. int y;
  1212. if(!USES_LIST(mb_type, list))
  1213. continue;
  1214. for(y=0; y<4; y++){
  1215. *(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];
  1216. *(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];
  1217. }
  1218. if( h->pps.cabac ) {
  1219. if(IS_SKIP(mb_type))
  1220. fill_rectangle(h->mvd_table[list][b_xy], 4, 4, h->b_stride, 0, 4);
  1221. else
  1222. for(y=0; y<4; y++){
  1223. *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+0 + 8*y];
  1224. *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+2 + 8*y];
  1225. }
  1226. }
  1227. {
  1228. int8_t *ref_index = &s->current_picture.ref_index[list][b8_xy];
  1229. ref_index[0+0*h->b8_stride]= h->ref_cache[list][scan8[0]];
  1230. ref_index[1+0*h->b8_stride]= h->ref_cache[list][scan8[4]];
  1231. ref_index[0+1*h->b8_stride]= h->ref_cache[list][scan8[8]];
  1232. ref_index[1+1*h->b8_stride]= h->ref_cache[list][scan8[12]];
  1233. }
  1234. }
  1235. if(h->slice_type == B_TYPE && h->pps.cabac){
  1236. if(IS_8X8(mb_type)){
  1237. uint8_t *direct_table = &h->direct_table[b8_xy];
  1238. direct_table[1+0*h->b8_stride] = IS_DIRECT(h->sub_mb_type[1]) ? 1 : 0;
  1239. direct_table[0+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[2]) ? 1 : 0;
  1240. direct_table[1+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[3]) ? 1 : 0;
  1241. }
  1242. }
  1243. }
  1244. /**
  1245. * Decodes a network abstraction layer unit.
  1246. * @param consumed is the number of bytes used as input
  1247. * @param length is the length of the array
  1248. * @param dst_length is the number of decoded bytes FIXME here or a decode rbsp tailing?
  1249. * @returns decoded bytes, might be src+1 if no escapes
  1250. */
  1251. static uint8_t *decode_nal(H264Context *h, uint8_t *src, int *dst_length, int *consumed, int length){
  1252. int i, si, di;
  1253. uint8_t *dst;
  1254. // src[0]&0x80; //forbidden bit
  1255. h->nal_ref_idc= src[0]>>5;
  1256. h->nal_unit_type= src[0]&0x1F;
  1257. src++; length--;
  1258. #if 0
  1259. for(i=0; i<length; i++)
  1260. printf("%2X ", src[i]);
  1261. #endif
  1262. for(i=0; i+1<length; i+=2){
  1263. if(src[i]) continue;
  1264. if(i>0 && src[i-1]==0) i--;
  1265. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  1266. if(src[i+2]!=3){
  1267. /* startcode, so we must be past the end */
  1268. length=i;
  1269. }
  1270. break;
  1271. }
  1272. }
  1273. if(i>=length-1){ //no escaped 0
  1274. *dst_length= length;
  1275. *consumed= length+1; //+1 for the header
  1276. return src;
  1277. }
  1278. h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length);
  1279. dst= h->rbsp_buffer;
  1280. if (dst == NULL){
  1281. return NULL;
  1282. }
  1283. //printf("decoding esc\n");
  1284. si=di=0;
  1285. while(si<length){
  1286. //remove escapes (very rare 1:2^22)
  1287. if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
  1288. if(src[si+2]==3){ //escape
  1289. dst[di++]= 0;
  1290. dst[di++]= 0;
  1291. si+=3;
  1292. continue;
  1293. }else //next start code
  1294. break;
  1295. }
  1296. dst[di++]= src[si++];
  1297. }
  1298. *dst_length= di;
  1299. *consumed= si + 1;//+1 for the header
  1300. //FIXME store exact number of bits in the getbitcontext (its needed for decoding)
  1301. return dst;
  1302. }
  1303. /**
  1304. * identifies the exact end of the bitstream
  1305. * @return the length of the trailing, or 0 if damaged
  1306. */
  1307. static int decode_rbsp_trailing(H264Context *h, uint8_t *src){
  1308. int v= *src;
  1309. int r;
  1310. tprintf(h->s.avctx, "rbsp trailing %X\n", v);
  1311. for(r=1; r<9; r++){
  1312. if(v&1) return r;
  1313. v>>=1;
  1314. }
  1315. return 0;
  1316. }
  1317. /**
  1318. * idct tranforms the 16 dc values and dequantize them.
  1319. * @param qp quantization parameter
  1320. */
  1321. static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1322. #define stride 16
  1323. int i;
  1324. int temp[16]; //FIXME check if this is a good idea
  1325. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1326. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1327. //memset(block, 64, 2*256);
  1328. //return;
  1329. for(i=0; i<4; i++){
  1330. const int offset= y_offset[i];
  1331. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1332. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1333. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1334. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1335. temp[4*i+0]= z0+z3;
  1336. temp[4*i+1]= z1+z2;
  1337. temp[4*i+2]= z1-z2;
  1338. temp[4*i+3]= z0-z3;
  1339. }
  1340. for(i=0; i<4; i++){
  1341. const int offset= x_offset[i];
  1342. const int z0= temp[4*0+i] + temp[4*2+i];
  1343. const int z1= temp[4*0+i] - temp[4*2+i];
  1344. const int z2= temp[4*1+i] - temp[4*3+i];
  1345. const int z3= temp[4*1+i] + temp[4*3+i];
  1346. block[stride*0 +offset]= ((((z0 + z3)*qmul + 128 ) >> 8)); //FIXME think about merging this into decode_resdual
  1347. block[stride*2 +offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
  1348. block[stride*8 +offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
  1349. block[stride*10+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
  1350. }
  1351. }
  1352. #if 0
  1353. /**
  1354. * dct tranforms the 16 dc values.
  1355. * @param qp quantization parameter ??? FIXME
  1356. */
  1357. static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
  1358. // const int qmul= dequant_coeff[qp][0];
  1359. int i;
  1360. int temp[16]; //FIXME check if this is a good idea
  1361. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  1362. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  1363. for(i=0; i<4; i++){
  1364. const int offset= y_offset[i];
  1365. const int z0= block[offset+stride*0] + block[offset+stride*4];
  1366. const int z1= block[offset+stride*0] - block[offset+stride*4];
  1367. const int z2= block[offset+stride*1] - block[offset+stride*5];
  1368. const int z3= block[offset+stride*1] + block[offset+stride*5];
  1369. temp[4*i+0]= z0+z3;
  1370. temp[4*i+1]= z1+z2;
  1371. temp[4*i+2]= z1-z2;
  1372. temp[4*i+3]= z0-z3;
  1373. }
  1374. for(i=0; i<4; i++){
  1375. const int offset= x_offset[i];
  1376. const int z0= temp[4*0+i] + temp[4*2+i];
  1377. const int z1= temp[4*0+i] - temp[4*2+i];
  1378. const int z2= temp[4*1+i] - temp[4*3+i];
  1379. const int z3= temp[4*1+i] + temp[4*3+i];
  1380. block[stride*0 +offset]= (z0 + z3)>>1;
  1381. block[stride*2 +offset]= (z1 + z2)>>1;
  1382. block[stride*8 +offset]= (z1 - z2)>>1;
  1383. block[stride*10+offset]= (z0 - z3)>>1;
  1384. }
  1385. }
  1386. #endif
  1387. #undef xStride
  1388. #undef stride
  1389. static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  1390. const int stride= 16*2;
  1391. const int xStride= 16;
  1392. int a,b,c,d,e;
  1393. a= block[stride*0 + xStride*0];
  1394. b= block[stride*0 + xStride*1];
  1395. c= block[stride*1 + xStride*0];
  1396. d= block[stride*1 + xStride*1];
  1397. e= a-b;
  1398. a= a+b;
  1399. b= c-d;
  1400. c= c+d;
  1401. block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
  1402. block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
  1403. block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
  1404. block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;
  1405. }
  1406. #if 0
  1407. static void chroma_dc_dct_c(DCTELEM *block){
  1408. const int stride= 16*2;
  1409. const int xStride= 16;
  1410. int a,b,c,d,e;
  1411. a= block[stride*0 + xStride*0];
  1412. b= block[stride*0 + xStride*1];
  1413. c= block[stride*1 + xStride*0];
  1414. d= block[stride*1 + xStride*1];
  1415. e= a-b;
  1416. a= a+b;
  1417. b= c-d;
  1418. c= c+d;
  1419. block[stride*0 + xStride*0]= (a+c);
  1420. block[stride*0 + xStride*1]= (e+b);
  1421. block[stride*1 + xStride*0]= (a-c);
  1422. block[stride*1 + xStride*1]= (e-b);
  1423. }
  1424. #endif
  1425. /**
  1426. * gets the chroma qp.
  1427. */
  1428. static inline int get_chroma_qp(int chroma_qp_index_offset, int qscale){
  1429. return chroma_qp[av_clip(qscale + chroma_qp_index_offset, 0, 51)];
  1430. }
  1431. //FIXME need to check that this doesnt overflow signed 32 bit for low qp, i am not sure, it's very close
  1432. //FIXME check that gcc inlines this (and optimizes intra & separate_dc stuff away)
  1433. static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int separate_dc){
  1434. int i;
  1435. const int * const quant_table= quant_coeff[qscale];
  1436. const int bias= intra ? (1<<QUANT_SHIFT)/3 : (1<<QUANT_SHIFT)/6;
  1437. const unsigned int threshold1= (1<<QUANT_SHIFT) - bias - 1;
  1438. const unsigned int threshold2= (threshold1<<1);
  1439. int last_non_zero;
  1440. if(separate_dc){
  1441. if(qscale<=18){
  1442. //avoid overflows
  1443. const int dc_bias= intra ? (1<<(QUANT_SHIFT-2))/3 : (1<<(QUANT_SHIFT-2))/6;
  1444. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT-2)) - dc_bias - 1;
  1445. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1446. int level= block[0]*quant_coeff[qscale+18][0];
  1447. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1448. if(level>0){
  1449. level= (dc_bias + level)>>(QUANT_SHIFT-2);
  1450. block[0]= level;
  1451. }else{
  1452. level= (dc_bias - level)>>(QUANT_SHIFT-2);
  1453. block[0]= -level;
  1454. }
  1455. // last_non_zero = i;
  1456. }else{
  1457. block[0]=0;
  1458. }
  1459. }else{
  1460. const int dc_bias= intra ? (1<<(QUANT_SHIFT+1))/3 : (1<<(QUANT_SHIFT+1))/6;
  1461. const unsigned int dc_threshold1= (1<<(QUANT_SHIFT+1)) - dc_bias - 1;
  1462. const unsigned int dc_threshold2= (dc_threshold1<<1);
  1463. int level= block[0]*quant_table[0];
  1464. if(((unsigned)(level+dc_threshold1))>dc_threshold2){
  1465. if(level>0){
  1466. level= (dc_bias + level)>>(QUANT_SHIFT+1);
  1467. block[0]= level;
  1468. }else{
  1469. level= (dc_bias - level)>>(QUANT_SHIFT+1);
  1470. block[0]= -level;
  1471. }
  1472. // last_non_zero = i;
  1473. }else{
  1474. block[0]=0;
  1475. }
  1476. }
  1477. last_non_zero= 0;
  1478. i=1;
  1479. }else{
  1480. last_non_zero= -1;
  1481. i=0;
  1482. }
  1483. for(; i<16; i++){
  1484. const int j= scantable[i];
  1485. int level= block[j]*quant_table[j];
  1486. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  1487. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  1488. if(((unsigned)(level+threshold1))>threshold2){
  1489. if(level>0){
  1490. level= (bias + level)>>QUANT_SHIFT;
  1491. block[j]= level;
  1492. }else{
  1493. level= (bias - level)>>QUANT_SHIFT;
  1494. block[j]= -level;
  1495. }
  1496. last_non_zero = i;
  1497. }else{
  1498. block[j]=0;
  1499. }
  1500. }
  1501. return last_non_zero;
  1502. }
  1503. static void pred4x4_vertical_c(uint8_t *src, uint8_t *topright, int stride){
  1504. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1505. ((uint32_t*)(src+0*stride))[0]= a;
  1506. ((uint32_t*)(src+1*stride))[0]= a;
  1507. ((uint32_t*)(src+2*stride))[0]= a;
  1508. ((uint32_t*)(src+3*stride))[0]= a;
  1509. }
  1510. static void pred4x4_horizontal_c(uint8_t *src, uint8_t *topright, int stride){
  1511. ((uint32_t*)(src+0*stride))[0]= src[-1+0*stride]*0x01010101;
  1512. ((uint32_t*)(src+1*stride))[0]= src[-1+1*stride]*0x01010101;
  1513. ((uint32_t*)(src+2*stride))[0]= src[-1+2*stride]*0x01010101;
  1514. ((uint32_t*)(src+3*stride))[0]= src[-1+3*stride]*0x01010101;
  1515. }
  1516. static void pred4x4_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1517. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride]
  1518. + src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 4) >>3;
  1519. ((uint32_t*)(src+0*stride))[0]=
  1520. ((uint32_t*)(src+1*stride))[0]=
  1521. ((uint32_t*)(src+2*stride))[0]=
  1522. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1523. }
  1524. static void pred4x4_left_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1525. const int dc= ( src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 2) >>2;
  1526. ((uint32_t*)(src+0*stride))[0]=
  1527. ((uint32_t*)(src+1*stride))[0]=
  1528. ((uint32_t*)(src+2*stride))[0]=
  1529. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1530. }
  1531. static void pred4x4_top_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1532. const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride] + 2) >>2;
  1533. ((uint32_t*)(src+0*stride))[0]=
  1534. ((uint32_t*)(src+1*stride))[0]=
  1535. ((uint32_t*)(src+2*stride))[0]=
  1536. ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
  1537. }
  1538. static void pred4x4_128_dc_c(uint8_t *src, uint8_t *topright, int stride){
  1539. ((uint32_t*)(src+0*stride))[0]=
  1540. ((uint32_t*)(src+1*stride))[0]=
  1541. ((uint32_t*)(src+2*stride))[0]=
  1542. ((uint32_t*)(src+3*stride))[0]= 128U*0x01010101U;
  1543. }
  1544. #define LOAD_TOP_RIGHT_EDGE\
  1545. const int attribute_unused t4= topright[0];\
  1546. const int attribute_unused t5= topright[1];\
  1547. const int attribute_unused t6= topright[2];\
  1548. const int attribute_unused t7= topright[3];\
  1549. #define LOAD_LEFT_EDGE\
  1550. const int attribute_unused l0= src[-1+0*stride];\
  1551. const int attribute_unused l1= src[-1+1*stride];\
  1552. const int attribute_unused l2= src[-1+2*stride];\
  1553. const int attribute_unused l3= src[-1+3*stride];\
  1554. #define LOAD_TOP_EDGE\
  1555. const int attribute_unused t0= src[ 0-1*stride];\
  1556. const int attribute_unused t1= src[ 1-1*stride];\
  1557. const int attribute_unused t2= src[ 2-1*stride];\
  1558. const int attribute_unused t3= src[ 3-1*stride];\
  1559. static void pred4x4_down_right_c(uint8_t *src, uint8_t *topright, int stride){
  1560. const int lt= src[-1-1*stride];
  1561. LOAD_TOP_EDGE
  1562. LOAD_LEFT_EDGE
  1563. src[0+3*stride]=(l3 + 2*l2 + l1 + 2)>>2;
  1564. src[0+2*stride]=
  1565. src[1+3*stride]=(l2 + 2*l1 + l0 + 2)>>2;
  1566. src[0+1*stride]=
  1567. src[1+2*stride]=
  1568. src[2+3*stride]=(l1 + 2*l0 + lt + 2)>>2;
  1569. src[0+0*stride]=
  1570. src[1+1*stride]=
  1571. src[2+2*stride]=
  1572. src[3+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1573. src[1+0*stride]=
  1574. src[2+1*stride]=
  1575. src[3+2*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1576. src[2+0*stride]=
  1577. src[3+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1578. src[3+0*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1579. }
  1580. static void pred4x4_down_left_c(uint8_t *src, uint8_t *topright, int stride){
  1581. LOAD_TOP_EDGE
  1582. LOAD_TOP_RIGHT_EDGE
  1583. // LOAD_LEFT_EDGE
  1584. src[0+0*stride]=(t0 + t2 + 2*t1 + 2)>>2;
  1585. src[1+0*stride]=
  1586. src[0+1*stride]=(t1 + t3 + 2*t2 + 2)>>2;
  1587. src[2+0*stride]=
  1588. src[1+1*stride]=
  1589. src[0+2*stride]=(t2 + t4 + 2*t3 + 2)>>2;
  1590. src[3+0*stride]=
  1591. src[2+1*stride]=
  1592. src[1+2*stride]=
  1593. src[0+3*stride]=(t3 + t5 + 2*t4 + 2)>>2;
  1594. src[3+1*stride]=
  1595. src[2+2*stride]=
  1596. src[1+3*stride]=(t4 + t6 + 2*t5 + 2)>>2;
  1597. src[3+2*stride]=
  1598. src[2+3*stride]=(t5 + t7 + 2*t6 + 2)>>2;
  1599. src[3+3*stride]=(t6 + 3*t7 + 2)>>2;
  1600. }
  1601. static void pred4x4_vertical_right_c(uint8_t *src, uint8_t *topright, int stride){
  1602. const int lt= src[-1-1*stride];
  1603. LOAD_TOP_EDGE
  1604. LOAD_LEFT_EDGE
  1605. src[0+0*stride]=
  1606. src[1+2*stride]=(lt + t0 + 1)>>1;
  1607. src[1+0*stride]=
  1608. src[2+2*stride]=(t0 + t1 + 1)>>1;
  1609. src[2+0*stride]=
  1610. src[3+2*stride]=(t1 + t2 + 1)>>1;
  1611. src[3+0*stride]=(t2 + t3 + 1)>>1;
  1612. src[0+1*stride]=
  1613. src[1+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1614. src[1+1*stride]=
  1615. src[2+3*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1616. src[2+1*stride]=
  1617. src[3+3*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1618. src[3+1*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1619. src[0+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  1620. src[0+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1621. }
  1622. static void pred4x4_vertical_left_c(uint8_t *src, uint8_t *topright, int stride){
  1623. LOAD_TOP_EDGE
  1624. LOAD_TOP_RIGHT_EDGE
  1625. src[0+0*stride]=(t0 + t1 + 1)>>1;
  1626. src[1+0*stride]=
  1627. src[0+2*stride]=(t1 + t2 + 1)>>1;
  1628. src[2+0*stride]=
  1629. src[1+2*stride]=(t2 + t3 + 1)>>1;
  1630. src[3+0*stride]=
  1631. src[2+2*stride]=(t3 + t4+ 1)>>1;
  1632. src[3+2*stride]=(t4 + t5+ 1)>>1;
  1633. src[0+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1634. src[1+1*stride]=
  1635. src[0+3*stride]=(t1 + 2*t2 + t3 + 2)>>2;
  1636. src[2+1*stride]=
  1637. src[1+3*stride]=(t2 + 2*t3 + t4 + 2)>>2;
  1638. src[3+1*stride]=
  1639. src[2+3*stride]=(t3 + 2*t4 + t5 + 2)>>2;
  1640. src[3+3*stride]=(t4 + 2*t5 + t6 + 2)>>2;
  1641. }
  1642. static void pred4x4_horizontal_up_c(uint8_t *src, uint8_t *topright, int stride){
  1643. LOAD_LEFT_EDGE
  1644. src[0+0*stride]=(l0 + l1 + 1)>>1;
  1645. src[1+0*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1646. src[2+0*stride]=
  1647. src[0+1*stride]=(l1 + l2 + 1)>>1;
  1648. src[3+0*stride]=
  1649. src[1+1*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  1650. src[2+1*stride]=
  1651. src[0+2*stride]=(l2 + l3 + 1)>>1;
  1652. src[3+1*stride]=
  1653. src[1+2*stride]=(l2 + 2*l3 + l3 + 2)>>2;
  1654. src[3+2*stride]=
  1655. src[1+3*stride]=
  1656. src[0+3*stride]=
  1657. src[2+2*stride]=
  1658. src[2+3*stride]=
  1659. src[3+3*stride]=l3;
  1660. }
  1661. static void pred4x4_horizontal_down_c(uint8_t *src, uint8_t *topright, int stride){
  1662. const int lt= src[-1-1*stride];
  1663. LOAD_TOP_EDGE
  1664. LOAD_LEFT_EDGE
  1665. src[0+0*stride]=
  1666. src[2+1*stride]=(lt + l0 + 1)>>1;
  1667. src[1+0*stride]=
  1668. src[3+1*stride]=(l0 + 2*lt + t0 + 2)>>2;
  1669. src[2+0*stride]=(lt + 2*t0 + t1 + 2)>>2;
  1670. src[3+0*stride]=(t0 + 2*t1 + t2 + 2)>>2;
  1671. src[0+1*stride]=
  1672. src[2+2*stride]=(l0 + l1 + 1)>>1;
  1673. src[1+1*stride]=
  1674. src[3+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
  1675. src[0+2*stride]=
  1676. src[2+3*stride]=(l1 + l2+ 1)>>1;
  1677. src[1+2*stride]=
  1678. src[3+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
  1679. src[0+3*stride]=(l2 + l3 + 1)>>1;
  1680. src[1+3*stride]=(l1 + 2*l2 + l3 + 2)>>2;
  1681. }
  1682. void ff_pred16x16_vertical_c(uint8_t *src, int stride){
  1683. int i;
  1684. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1685. const uint32_t b= ((uint32_t*)(src-stride))[1];
  1686. const uint32_t c= ((uint32_t*)(src-stride))[2];
  1687. const uint32_t d= ((uint32_t*)(src-stride))[3];
  1688. for(i=0; i<16; i++){
  1689. ((uint32_t*)(src+i*stride))[0]= a;
  1690. ((uint32_t*)(src+i*stride))[1]= b;
  1691. ((uint32_t*)(src+i*stride))[2]= c;
  1692. ((uint32_t*)(src+i*stride))[3]= d;
  1693. }
  1694. }
  1695. void ff_pred16x16_horizontal_c(uint8_t *src, int stride){
  1696. int i;
  1697. for(i=0; i<16; i++){
  1698. ((uint32_t*)(src+i*stride))[0]=
  1699. ((uint32_t*)(src+i*stride))[1]=
  1700. ((uint32_t*)(src+i*stride))[2]=
  1701. ((uint32_t*)(src+i*stride))[3]= src[-1+i*stride]*0x01010101;
  1702. }
  1703. }
  1704. void ff_pred16x16_dc_c(uint8_t *src, int stride){
  1705. int i, dc=0;
  1706. for(i=0;i<16; i++){
  1707. dc+= src[-1+i*stride];
  1708. }
  1709. for(i=0;i<16; i++){
  1710. dc+= src[i-stride];
  1711. }
  1712. dc= 0x01010101*((dc + 16)>>5);
  1713. for(i=0; i<16; i++){
  1714. ((uint32_t*)(src+i*stride))[0]=
  1715. ((uint32_t*)(src+i*stride))[1]=
  1716. ((uint32_t*)(src+i*stride))[2]=
  1717. ((uint32_t*)(src+i*stride))[3]= dc;
  1718. }
  1719. }
  1720. void ff_pred16x16_left_dc_c(uint8_t *src, int stride){
  1721. int i, dc=0;
  1722. for(i=0;i<16; i++){
  1723. dc+= src[-1+i*stride];
  1724. }
  1725. dc= 0x01010101*((dc + 8)>>4);
  1726. for(i=0; i<16; i++){
  1727. ((uint32_t*)(src+i*stride))[0]=
  1728. ((uint32_t*)(src+i*stride))[1]=
  1729. ((uint32_t*)(src+i*stride))[2]=
  1730. ((uint32_t*)(src+i*stride))[3]= dc;
  1731. }
  1732. }
  1733. void ff_pred16x16_top_dc_c(uint8_t *src, int stride){
  1734. int i, dc=0;
  1735. for(i=0;i<16; i++){
  1736. dc+= src[i-stride];
  1737. }
  1738. dc= 0x01010101*((dc + 8)>>4);
  1739. for(i=0; i<16; i++){
  1740. ((uint32_t*)(src+i*stride))[0]=
  1741. ((uint32_t*)(src+i*stride))[1]=
  1742. ((uint32_t*)(src+i*stride))[2]=
  1743. ((uint32_t*)(src+i*stride))[3]= dc;
  1744. }
  1745. }
  1746. void ff_pred16x16_128_dc_c(uint8_t *src, int stride){
  1747. int i;
  1748. for(i=0; i<16; i++){
  1749. ((uint32_t*)(src+i*stride))[0]=
  1750. ((uint32_t*)(src+i*stride))[1]=
  1751. ((uint32_t*)(src+i*stride))[2]=
  1752. ((uint32_t*)(src+i*stride))[3]= 0x01010101U*128U;
  1753. }
  1754. }
  1755. static inline void pred16x16_plane_compat_c(uint8_t *src, int stride, const int svq3){
  1756. int i, j, k;
  1757. int a;
  1758. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  1759. const uint8_t * const src0 = src+7-stride;
  1760. const uint8_t *src1 = src+8*stride-1;
  1761. const uint8_t *src2 = src1-2*stride; // == src+6*stride-1;
  1762. int H = src0[1] - src0[-1];
  1763. int V = src1[0] - src2[ 0];
  1764. for(k=2; k<=8; ++k) {
  1765. src1 += stride; src2 -= stride;
  1766. H += k*(src0[k] - src0[-k]);
  1767. V += k*(src1[0] - src2[ 0]);
  1768. }
  1769. if(svq3){
  1770. H = ( 5*(H/4) ) / 16;
  1771. V = ( 5*(V/4) ) / 16;
  1772. /* required for 100% accuracy */
  1773. i = H; H = V; V = i;
  1774. }else{
  1775. H = ( 5*H+32 ) >> 6;
  1776. V = ( 5*V+32 ) >> 6;
  1777. }
  1778. a = 16*(src1[0] + src2[16] + 1) - 7*(V+H);
  1779. for(j=16; j>0; --j) {
  1780. int b = a;
  1781. a += V;
  1782. for(i=-16; i<0; i+=4) {
  1783. src[16+i] = cm[ (b ) >> 5 ];
  1784. src[17+i] = cm[ (b+ H) >> 5 ];
  1785. src[18+i] = cm[ (b+2*H) >> 5 ];
  1786. src[19+i] = cm[ (b+3*H) >> 5 ];
  1787. b += 4*H;
  1788. }
  1789. src += stride;
  1790. }
  1791. }
  1792. void ff_pred16x16_plane_c(uint8_t *src, int stride){
  1793. pred16x16_plane_compat_c(src, stride, 0);
  1794. }
  1795. void ff_pred8x8_vertical_c(uint8_t *src, int stride){
  1796. int i;
  1797. const uint32_t a= ((uint32_t*)(src-stride))[0];
  1798. const uint32_t b= ((uint32_t*)(src-stride))[1];
  1799. for(i=0; i<8; i++){
  1800. ((uint32_t*)(src+i*stride))[0]= a;
  1801. ((uint32_t*)(src+i*stride))[1]= b;
  1802. }
  1803. }
  1804. void ff_pred8x8_horizontal_c(uint8_t *src, int stride){
  1805. int i;
  1806. for(i=0; i<8; i++){
  1807. ((uint32_t*)(src+i*stride))[0]=
  1808. ((uint32_t*)(src+i*stride))[1]= src[-1+i*stride]*0x01010101;
  1809. }
  1810. }
  1811. void ff_pred8x8_128_dc_c(uint8_t *src, int stride){
  1812. int i;
  1813. for(i=0; i<8; i++){
  1814. ((uint32_t*)(src+i*stride))[0]=
  1815. ((uint32_t*)(src+i*stride))[1]= 0x01010101U*128U;
  1816. }
  1817. }
  1818. void ff_pred8x8_left_dc_c(uint8_t *src, int stride){
  1819. int i;
  1820. int dc0, dc2;
  1821. dc0=dc2=0;
  1822. for(i=0;i<4; i++){
  1823. dc0+= src[-1+i*stride];
  1824. dc2+= src[-1+(i+4)*stride];
  1825. }
  1826. dc0= 0x01010101*((dc0 + 2)>>2);
  1827. dc2= 0x01010101*((dc2 + 2)>>2);
  1828. for(i=0; i<4; i++){
  1829. ((uint32_t*)(src+i*stride))[0]=
  1830. ((uint32_t*)(src+i*stride))[1]= dc0;
  1831. }
  1832. for(i=4; i<8; i++){
  1833. ((uint32_t*)(src+i*stride))[0]=
  1834. ((uint32_t*)(src+i*stride))[1]= dc2;
  1835. }
  1836. }
  1837. void ff_pred8x8_top_dc_c(uint8_t *src, int stride){
  1838. int i;
  1839. int dc0, dc1;
  1840. dc0=dc1=0;
  1841. for(i=0;i<4; i++){
  1842. dc0+= src[i-stride];
  1843. dc1+= src[4+i-stride];
  1844. }
  1845. dc0= 0x01010101*((dc0 + 2)>>2);
  1846. dc1= 0x01010101*((dc1 + 2)>>2);
  1847. for(i=0; i<4; i++){
  1848. ((uint32_t*)(src+i*stride))[0]= dc0;
  1849. ((uint32_t*)(src+i*stride))[1]= dc1;
  1850. }
  1851. for(i=4; i<8; i++){
  1852. ((uint32_t*)(src+i*stride))[0]= dc0;
  1853. ((uint32_t*)(src+i*stride))[1]= dc1;
  1854. }
  1855. }
  1856. void ff_pred8x8_dc_c(uint8_t *src, int stride){
  1857. int i;
  1858. int dc0, dc1, dc2, dc3;
  1859. dc0=dc1=dc2=0;
  1860. for(i=0;i<4; i++){
  1861. dc0+= src[-1+i*stride] + src[i-stride];
  1862. dc1+= src[4+i-stride];
  1863. dc2+= src[-1+(i+4)*stride];
  1864. }
  1865. dc3= 0x01010101*((dc1 + dc2 + 4)>>3);
  1866. dc0= 0x01010101*((dc0 + 4)>>3);
  1867. dc1= 0x01010101*((dc1 + 2)>>2);
  1868. dc2= 0x01010101*((dc2 + 2)>>2);
  1869. for(i=0; i<4; i++){
  1870. ((uint32_t*)(src+i*stride))[0]= dc0;
  1871. ((uint32_t*)(src+i*stride))[1]= dc1;
  1872. }
  1873. for(i=4; i<8; i++){
  1874. ((uint32_t*)(src+i*stride))[0]= dc2;
  1875. ((uint32_t*)(src+i*stride))[1]= dc3;
  1876. }
  1877. }
  1878. void ff_pred8x8_plane_c(uint8_t *src, int stride){
  1879. int j, k;
  1880. int a;
  1881. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  1882. const uint8_t * const src0 = src+3-stride;
  1883. const uint8_t *src1 = src+4*stride-1;
  1884. const uint8_t *src2 = src1-2*stride; // == src+2*stride-1;
  1885. int H = src0[1] - src0[-1];
  1886. int V = src1[0] - src2[ 0];
  1887. for(k=2; k<=4; ++k) {
  1888. src1 += stride; src2 -= stride;
  1889. H += k*(src0[k] - src0[-k]);
  1890. V += k*(src1[0] - src2[ 0]);
  1891. }
  1892. H = ( 17*H+16 ) >> 5;
  1893. V = ( 17*V+16 ) >> 5;
  1894. a = 16*(src1[0] + src2[8]+1) - 3*(V+H);
  1895. for(j=8; j>0; --j) {
  1896. int b = a;
  1897. a += V;
  1898. src[0] = cm[ (b ) >> 5 ];
  1899. src[1] = cm[ (b+ H) >> 5 ];
  1900. src[2] = cm[ (b+2*H) >> 5 ];
  1901. src[3] = cm[ (b+3*H) >> 5 ];
  1902. src[4] = cm[ (b+4*H) >> 5 ];
  1903. src[5] = cm[ (b+5*H) >> 5 ];
  1904. src[6] = cm[ (b+6*H) >> 5 ];
  1905. src[7] = cm[ (b+7*H) >> 5 ];
  1906. src += stride;
  1907. }
  1908. }
  1909. #define SRC(x,y) src[(x)+(y)*stride]
  1910. #define PL(y) \
  1911. const int l##y = (SRC(-1,y-1) + 2*SRC(-1,y) + SRC(-1,y+1) + 2) >> 2;
  1912. #define PREDICT_8x8_LOAD_LEFT \
  1913. const int l0 = ((has_topleft ? SRC(-1,-1) : SRC(-1,0)) \
  1914. + 2*SRC(-1,0) + SRC(-1,1) + 2) >> 2; \
  1915. PL(1) PL(2) PL(3) PL(4) PL(5) PL(6) \
  1916. const int l7 attribute_unused = (SRC(-1,6) + 3*SRC(-1,7) + 2) >> 2
  1917. #define PT(x) \
  1918. const int t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
  1919. #define PREDICT_8x8_LOAD_TOP \
  1920. const int t0 = ((has_topleft ? SRC(-1,-1) : SRC(0,-1)) \
  1921. + 2*SRC(0,-1) + SRC(1,-1) + 2) >> 2; \
  1922. PT(1) PT(2) PT(3) PT(4) PT(5) PT(6) \
  1923. const int t7 attribute_unused = ((has_topright ? SRC(8,-1) : SRC(7,-1)) \
  1924. + 2*SRC(7,-1) + SRC(6,-1) + 2) >> 2
  1925. #define PTR(x) \
  1926. t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
  1927. #define PREDICT_8x8_LOAD_TOPRIGHT \
  1928. int t8, t9, t10, t11, t12, t13, t14, t15; \
  1929. if(has_topright) { \
  1930. PTR(8) PTR(9) PTR(10) PTR(11) PTR(12) PTR(13) PTR(14) \
  1931. t15 = (SRC(14,-1) + 3*SRC(15,-1) + 2) >> 2; \
  1932. } else t8=t9=t10=t11=t12=t13=t14=t15= SRC(7,-1);
  1933. #define PREDICT_8x8_LOAD_TOPLEFT \
  1934. const int lt = (SRC(-1,0) + 2*SRC(-1,-1) + SRC(0,-1) + 2) >> 2
  1935. #define PREDICT_8x8_DC(v) \
  1936. int y; \
  1937. for( y = 0; y < 8; y++ ) { \
  1938. ((uint32_t*)src)[0] = \
  1939. ((uint32_t*)src)[1] = v; \
  1940. src += stride; \
  1941. }
  1942. static void pred8x8l_128_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  1943. {
  1944. PREDICT_8x8_DC(0x80808080);
  1945. }
  1946. static void pred8x8l_left_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  1947. {
  1948. PREDICT_8x8_LOAD_LEFT;
  1949. const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7+4) >> 3) * 0x01010101;
  1950. PREDICT_8x8_DC(dc);
  1951. }
  1952. static void pred8x8l_top_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  1953. {
  1954. PREDICT_8x8_LOAD_TOP;
  1955. const uint32_t dc = ((t0+t1+t2+t3+t4+t5+t6+t7+4) >> 3) * 0x01010101;
  1956. PREDICT_8x8_DC(dc);
  1957. }
  1958. static void pred8x8l_dc_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  1959. {
  1960. PREDICT_8x8_LOAD_LEFT;
  1961. PREDICT_8x8_LOAD_TOP;
  1962. const uint32_t dc = ((l0+l1+l2+l3+l4+l5+l6+l7
  1963. +t0+t1+t2+t3+t4+t5+t6+t7+8) >> 4) * 0x01010101;
  1964. PREDICT_8x8_DC(dc);
  1965. }
  1966. static void pred8x8l_horizontal_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  1967. {
  1968. PREDICT_8x8_LOAD_LEFT;
  1969. #define ROW(y) ((uint32_t*)(src+y*stride))[0] =\
  1970. ((uint32_t*)(src+y*stride))[1] = 0x01010101 * l##y
  1971. ROW(0); ROW(1); ROW(2); ROW(3); ROW(4); ROW(5); ROW(6); ROW(7);
  1972. #undef ROW
  1973. }
  1974. static void pred8x8l_vertical_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  1975. {
  1976. int y;
  1977. PREDICT_8x8_LOAD_TOP;
  1978. src[0] = t0;
  1979. src[1] = t1;
  1980. src[2] = t2;
  1981. src[3] = t3;
  1982. src[4] = t4;
  1983. src[5] = t5;
  1984. src[6] = t6;
  1985. src[7] = t7;
  1986. for( y = 1; y < 8; y++ )
  1987. *(uint64_t*)(src+y*stride) = *(uint64_t*)src;
  1988. }
  1989. static void pred8x8l_down_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  1990. {
  1991. PREDICT_8x8_LOAD_TOP;
  1992. PREDICT_8x8_LOAD_TOPRIGHT;
  1993. SRC(0,0)= (t0 + 2*t1 + t2 + 2) >> 2;
  1994. SRC(0,1)=SRC(1,0)= (t1 + 2*t2 + t3 + 2) >> 2;
  1995. SRC(0,2)=SRC(1,1)=SRC(2,0)= (t2 + 2*t3 + t4 + 2) >> 2;
  1996. SRC(0,3)=SRC(1,2)=SRC(2,1)=SRC(3,0)= (t3 + 2*t4 + t5 + 2) >> 2;
  1997. SRC(0,4)=SRC(1,3)=SRC(2,2)=SRC(3,1)=SRC(4,0)= (t4 + 2*t5 + t6 + 2) >> 2;
  1998. SRC(0,5)=SRC(1,4)=SRC(2,3)=SRC(3,2)=SRC(4,1)=SRC(5,0)= (t5 + 2*t6 + t7 + 2) >> 2;
  1999. 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;
  2000. 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;
  2001. 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;
  2002. SRC(2,7)=SRC(3,6)=SRC(4,5)=SRC(5,4)=SRC(6,3)=SRC(7,2)= (t9 + 2*t10 + t11 + 2) >> 2;
  2003. SRC(3,7)=SRC(4,6)=SRC(5,5)=SRC(6,4)=SRC(7,3)= (t10 + 2*t11 + t12 + 2) >> 2;
  2004. SRC(4,7)=SRC(5,6)=SRC(6,5)=SRC(7,4)= (t11 + 2*t12 + t13 + 2) >> 2;
  2005. SRC(5,7)=SRC(6,6)=SRC(7,5)= (t12 + 2*t13 + t14 + 2) >> 2;
  2006. SRC(6,7)=SRC(7,6)= (t13 + 2*t14 + t15 + 2) >> 2;
  2007. SRC(7,7)= (t14 + 3*t15 + 2) >> 2;
  2008. }
  2009. static void pred8x8l_down_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2010. {
  2011. PREDICT_8x8_LOAD_TOP;
  2012. PREDICT_8x8_LOAD_LEFT;
  2013. PREDICT_8x8_LOAD_TOPLEFT;
  2014. SRC(0,7)= (l7 + 2*l6 + l5 + 2) >> 2;
  2015. SRC(0,6)=SRC(1,7)= (l6 + 2*l5 + l4 + 2) >> 2;
  2016. SRC(0,5)=SRC(1,6)=SRC(2,7)= (l5 + 2*l4 + l3 + 2) >> 2;
  2017. SRC(0,4)=SRC(1,5)=SRC(2,6)=SRC(3,7)= (l4 + 2*l3 + l2 + 2) >> 2;
  2018. SRC(0,3)=SRC(1,4)=SRC(2,5)=SRC(3,6)=SRC(4,7)= (l3 + 2*l2 + l1 + 2) >> 2;
  2019. SRC(0,2)=SRC(1,3)=SRC(2,4)=SRC(3,5)=SRC(4,6)=SRC(5,7)= (l2 + 2*l1 + l0 + 2) >> 2;
  2020. 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;
  2021. 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;
  2022. 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;
  2023. SRC(2,0)=SRC(3,1)=SRC(4,2)=SRC(5,3)=SRC(6,4)=SRC(7,5)= (t0 + 2*t1 + t2 + 2) >> 2;
  2024. SRC(3,0)=SRC(4,1)=SRC(5,2)=SRC(6,3)=SRC(7,4)= (t1 + 2*t2 + t3 + 2) >> 2;
  2025. SRC(4,0)=SRC(5,1)=SRC(6,2)=SRC(7,3)= (t2 + 2*t3 + t4 + 2) >> 2;
  2026. SRC(5,0)=SRC(6,1)=SRC(7,2)= (t3 + 2*t4 + t5 + 2) >> 2;
  2027. SRC(6,0)=SRC(7,1)= (t4 + 2*t5 + t6 + 2) >> 2;
  2028. SRC(7,0)= (t5 + 2*t6 + t7 + 2) >> 2;
  2029. }
  2030. static void pred8x8l_vertical_right_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2031. {
  2032. PREDICT_8x8_LOAD_TOP;
  2033. PREDICT_8x8_LOAD_LEFT;
  2034. PREDICT_8x8_LOAD_TOPLEFT;
  2035. SRC(0,6)= (l5 + 2*l4 + l3 + 2) >> 2;
  2036. SRC(0,7)= (l6 + 2*l5 + l4 + 2) >> 2;
  2037. SRC(0,4)=SRC(1,6)= (l3 + 2*l2 + l1 + 2) >> 2;
  2038. SRC(0,5)=SRC(1,7)= (l4 + 2*l3 + l2 + 2) >> 2;
  2039. SRC(0,2)=SRC(1,4)=SRC(2,6)= (l1 + 2*l0 + lt + 2) >> 2;
  2040. SRC(0,3)=SRC(1,5)=SRC(2,7)= (l2 + 2*l1 + l0 + 2) >> 2;
  2041. SRC(0,1)=SRC(1,3)=SRC(2,5)=SRC(3,7)= (l0 + 2*lt + t0 + 2) >> 2;
  2042. SRC(0,0)=SRC(1,2)=SRC(2,4)=SRC(3,6)= (lt + t0 + 1) >> 1;
  2043. SRC(1,1)=SRC(2,3)=SRC(3,5)=SRC(4,7)= (lt + 2*t0 + t1 + 2) >> 2;
  2044. SRC(1,0)=SRC(2,2)=SRC(3,4)=SRC(4,6)= (t0 + t1 + 1) >> 1;
  2045. SRC(2,1)=SRC(3,3)=SRC(4,5)=SRC(5,7)= (t0 + 2*t1 + t2 + 2) >> 2;
  2046. SRC(2,0)=SRC(3,2)=SRC(4,4)=SRC(5,6)= (t1 + t2 + 1) >> 1;
  2047. SRC(3,1)=SRC(4,3)=SRC(5,5)=SRC(6,7)= (t1 + 2*t2 + t3 + 2) >> 2;
  2048. SRC(3,0)=SRC(4,2)=SRC(5,4)=SRC(6,6)= (t2 + t3 + 1) >> 1;
  2049. SRC(4,1)=SRC(5,3)=SRC(6,5)=SRC(7,7)= (t2 + 2*t3 + t4 + 2) >> 2;
  2050. SRC(4,0)=SRC(5,2)=SRC(6,4)=SRC(7,6)= (t3 + t4 + 1) >> 1;
  2051. SRC(5,1)=SRC(6,3)=SRC(7,5)= (t3 + 2*t4 + t5 + 2) >> 2;
  2052. SRC(5,0)=SRC(6,2)=SRC(7,4)= (t4 + t5 + 1) >> 1;
  2053. SRC(6,1)=SRC(7,3)= (t4 + 2*t5 + t6 + 2) >> 2;
  2054. SRC(6,0)=SRC(7,2)= (t5 + t6 + 1) >> 1;
  2055. SRC(7,1)= (t5 + 2*t6 + t7 + 2) >> 2;
  2056. SRC(7,0)= (t6 + t7 + 1) >> 1;
  2057. }
  2058. static void pred8x8l_horizontal_down_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2059. {
  2060. PREDICT_8x8_LOAD_TOP;
  2061. PREDICT_8x8_LOAD_LEFT;
  2062. PREDICT_8x8_LOAD_TOPLEFT;
  2063. SRC(0,7)= (l6 + l7 + 1) >> 1;
  2064. SRC(1,7)= (l5 + 2*l6 + l7 + 2) >> 2;
  2065. SRC(0,6)=SRC(2,7)= (l5 + l6 + 1) >> 1;
  2066. SRC(1,6)=SRC(3,7)= (l4 + 2*l5 + l6 + 2) >> 2;
  2067. SRC(0,5)=SRC(2,6)=SRC(4,7)= (l4 + l5 + 1) >> 1;
  2068. SRC(1,5)=SRC(3,6)=SRC(5,7)= (l3 + 2*l4 + l5 + 2) >> 2;
  2069. SRC(0,4)=SRC(2,5)=SRC(4,6)=SRC(6,7)= (l3 + l4 + 1) >> 1;
  2070. SRC(1,4)=SRC(3,5)=SRC(5,6)=SRC(7,7)= (l2 + 2*l3 + l4 + 2) >> 2;
  2071. SRC(0,3)=SRC(2,4)=SRC(4,5)=SRC(6,6)= (l2 + l3 + 1) >> 1;
  2072. SRC(1,3)=SRC(3,4)=SRC(5,5)=SRC(7,6)= (l1 + 2*l2 + l3 + 2) >> 2;
  2073. SRC(0,2)=SRC(2,3)=SRC(4,4)=SRC(6,5)= (l1 + l2 + 1) >> 1;
  2074. SRC(1,2)=SRC(3,3)=SRC(5,4)=SRC(7,5)= (l0 + 2*l1 + l2 + 2) >> 2;
  2075. SRC(0,1)=SRC(2,2)=SRC(4,3)=SRC(6,4)= (l0 + l1 + 1) >> 1;
  2076. SRC(1,1)=SRC(3,2)=SRC(5,3)=SRC(7,4)= (lt + 2*l0 + l1 + 2) >> 2;
  2077. SRC(0,0)=SRC(2,1)=SRC(4,2)=SRC(6,3)= (lt + l0 + 1) >> 1;
  2078. SRC(1,0)=SRC(3,1)=SRC(5,2)=SRC(7,3)= (l0 + 2*lt + t0 + 2) >> 2;
  2079. SRC(2,0)=SRC(4,1)=SRC(6,2)= (t1 + 2*t0 + lt + 2) >> 2;
  2080. SRC(3,0)=SRC(5,1)=SRC(7,2)= (t2 + 2*t1 + t0 + 2) >> 2;
  2081. SRC(4,0)=SRC(6,1)= (t3 + 2*t2 + t1 + 2) >> 2;
  2082. SRC(5,0)=SRC(7,1)= (t4 + 2*t3 + t2 + 2) >> 2;
  2083. SRC(6,0)= (t5 + 2*t4 + t3 + 2) >> 2;
  2084. SRC(7,0)= (t6 + 2*t5 + t4 + 2) >> 2;
  2085. }
  2086. static void pred8x8l_vertical_left_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2087. {
  2088. PREDICT_8x8_LOAD_TOP;
  2089. PREDICT_8x8_LOAD_TOPRIGHT;
  2090. SRC(0,0)= (t0 + t1 + 1) >> 1;
  2091. SRC(0,1)= (t0 + 2*t1 + t2 + 2) >> 2;
  2092. SRC(0,2)=SRC(1,0)= (t1 + t2 + 1) >> 1;
  2093. SRC(0,3)=SRC(1,1)= (t1 + 2*t2 + t3 + 2) >> 2;
  2094. SRC(0,4)=SRC(1,2)=SRC(2,0)= (t2 + t3 + 1) >> 1;
  2095. SRC(0,5)=SRC(1,3)=SRC(2,1)= (t2 + 2*t3 + t4 + 2) >> 2;
  2096. SRC(0,6)=SRC(1,4)=SRC(2,2)=SRC(3,0)= (t3 + t4 + 1) >> 1;
  2097. SRC(0,7)=SRC(1,5)=SRC(2,3)=SRC(3,1)= (t3 + 2*t4 + t5 + 2) >> 2;
  2098. SRC(1,6)=SRC(2,4)=SRC(3,2)=SRC(4,0)= (t4 + t5 + 1) >> 1;
  2099. SRC(1,7)=SRC(2,5)=SRC(3,3)=SRC(4,1)= (t4 + 2*t5 + t6 + 2) >> 2;
  2100. SRC(2,6)=SRC(3,4)=SRC(4,2)=SRC(5,0)= (t5 + t6 + 1) >> 1;
  2101. SRC(2,7)=SRC(3,5)=SRC(4,3)=SRC(5,1)= (t5 + 2*t6 + t7 + 2) >> 2;
  2102. SRC(3,6)=SRC(4,4)=SRC(5,2)=SRC(6,0)= (t6 + t7 + 1) >> 1;
  2103. SRC(3,7)=SRC(4,5)=SRC(5,3)=SRC(6,1)= (t6 + 2*t7 + t8 + 2) >> 2;
  2104. SRC(4,6)=SRC(5,4)=SRC(6,2)=SRC(7,0)= (t7 + t8 + 1) >> 1;
  2105. SRC(4,7)=SRC(5,5)=SRC(6,3)=SRC(7,1)= (t7 + 2*t8 + t9 + 2) >> 2;
  2106. SRC(5,6)=SRC(6,4)=SRC(7,2)= (t8 + t9 + 1) >> 1;
  2107. SRC(5,7)=SRC(6,5)=SRC(7,3)= (t8 + 2*t9 + t10 + 2) >> 2;
  2108. SRC(6,6)=SRC(7,4)= (t9 + t10 + 1) >> 1;
  2109. SRC(6,7)=SRC(7,5)= (t9 + 2*t10 + t11 + 2) >> 2;
  2110. SRC(7,6)= (t10 + t11 + 1) >> 1;
  2111. SRC(7,7)= (t10 + 2*t11 + t12 + 2) >> 2;
  2112. }
  2113. static void pred8x8l_horizontal_up_c(uint8_t *src, int has_topleft, int has_topright, int stride)
  2114. {
  2115. PREDICT_8x8_LOAD_LEFT;
  2116. SRC(0,0)= (l0 + l1 + 1) >> 1;
  2117. SRC(1,0)= (l0 + 2*l1 + l2 + 2) >> 2;
  2118. SRC(0,1)=SRC(2,0)= (l1 + l2 + 1) >> 1;
  2119. SRC(1,1)=SRC(3,0)= (l1 + 2*l2 + l3 + 2) >> 2;
  2120. SRC(0,2)=SRC(2,1)=SRC(4,0)= (l2 + l3 + 1) >> 1;
  2121. SRC(1,2)=SRC(3,1)=SRC(5,0)= (l2 + 2*l3 + l4 + 2) >> 2;
  2122. SRC(0,3)=SRC(2,2)=SRC(4,1)=SRC(6,0)= (l3 + l4 + 1) >> 1;
  2123. SRC(1,3)=SRC(3,2)=SRC(5,1)=SRC(7,0)= (l3 + 2*l4 + l5 + 2) >> 2;
  2124. SRC(0,4)=SRC(2,3)=SRC(4,2)=SRC(6,1)= (l4 + l5 + 1) >> 1;
  2125. SRC(1,4)=SRC(3,3)=SRC(5,2)=SRC(7,1)= (l4 + 2*l5 + l6 + 2) >> 2;
  2126. SRC(0,5)=SRC(2,4)=SRC(4,3)=SRC(6,2)= (l5 + l6 + 1) >> 1;
  2127. SRC(1,5)=SRC(3,4)=SRC(5,3)=SRC(7,2)= (l5 + 2*l6 + l7 + 2) >> 2;
  2128. SRC(0,6)=SRC(2,5)=SRC(4,4)=SRC(6,3)= (l6 + l7 + 1) >> 1;
  2129. SRC(1,6)=SRC(3,5)=SRC(5,4)=SRC(7,3)= (l6 + 3*l7 + 2) >> 2;
  2130. SRC(0,7)=SRC(1,7)=SRC(2,6)=SRC(2,7)=SRC(3,6)=
  2131. SRC(3,7)=SRC(4,5)=SRC(4,6)=SRC(4,7)=SRC(5,5)=
  2132. SRC(5,6)=SRC(5,7)=SRC(6,4)=SRC(6,5)=SRC(6,6)=
  2133. SRC(6,7)=SRC(7,4)=SRC(7,5)=SRC(7,6)=SRC(7,7)= l7;
  2134. }
  2135. #undef PREDICT_8x8_LOAD_LEFT
  2136. #undef PREDICT_8x8_LOAD_TOP
  2137. #undef PREDICT_8x8_LOAD_TOPLEFT
  2138. #undef PREDICT_8x8_LOAD_TOPRIGHT
  2139. #undef PREDICT_8x8_DC
  2140. #undef PTR
  2141. #undef PT
  2142. #undef PL
  2143. #undef SRC
  2144. static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
  2145. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2146. int src_x_offset, int src_y_offset,
  2147. qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
  2148. MpegEncContext * const s = &h->s;
  2149. const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
  2150. int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
  2151. const int luma_xy= (mx&3) + ((my&3)<<2);
  2152. uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->mb_linesize;
  2153. uint8_t * src_cb, * src_cr;
  2154. int extra_width= h->emu_edge_width;
  2155. int extra_height= h->emu_edge_height;
  2156. int emu=0;
  2157. const int full_mx= mx>>2;
  2158. const int full_my= my>>2;
  2159. const int pic_width = 16*s->mb_width;
  2160. const int pic_height = 16*s->mb_height >> MB_MBAFF;
  2161. if(!pic->data[0]) //FIXME this is unacceptable, some senseable error concealment must be done for missing reference frames
  2162. return;
  2163. if(mx&7) extra_width -= 3;
  2164. if(my&7) extra_height -= 3;
  2165. if( full_mx < 0-extra_width
  2166. || full_my < 0-extra_height
  2167. || full_mx + 16/*FIXME*/ > pic_width + extra_width
  2168. || full_my + 16/*FIXME*/ > pic_height + extra_height){
  2169. 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);
  2170. src_y= s->edge_emu_buffer + 2 + 2*h->mb_linesize;
  2171. emu=1;
  2172. }
  2173. qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); //FIXME try variable height perhaps?
  2174. if(!square){
  2175. qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
  2176. }
  2177. if(s->flags&CODEC_FLAG_GRAY) return;
  2178. if(MB_MBAFF){
  2179. // chroma offset when predicting from a field of opposite parity
  2180. my += 2 * ((s->mb_y & 1) - (h->ref_cache[list][scan8[n]] & 1));
  2181. emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
  2182. }
  2183. src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  2184. src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  2185. if(emu){
  2186. 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);
  2187. src_cb= s->edge_emu_buffer;
  2188. }
  2189. chroma_op(dest_cb, src_cb, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  2190. if(emu){
  2191. 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);
  2192. src_cr= s->edge_emu_buffer;
  2193. }
  2194. chroma_op(dest_cr, src_cr, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  2195. }
  2196. static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
  2197. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2198. int x_offset, int y_offset,
  2199. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2200. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  2201. int list0, int list1){
  2202. MpegEncContext * const s = &h->s;
  2203. qpel_mc_func *qpix_op= qpix_put;
  2204. h264_chroma_mc_func chroma_op= chroma_put;
  2205. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  2206. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  2207. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  2208. x_offset += 8*s->mb_x;
  2209. y_offset += 8*(s->mb_y >> MB_MBAFF);
  2210. if(list0){
  2211. Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
  2212. mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
  2213. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2214. qpix_op, chroma_op);
  2215. qpix_op= qpix_avg;
  2216. chroma_op= chroma_avg;
  2217. }
  2218. if(list1){
  2219. Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
  2220. mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
  2221. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2222. qpix_op, chroma_op);
  2223. }
  2224. }
  2225. static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
  2226. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2227. int x_offset, int y_offset,
  2228. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2229. h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
  2230. h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
  2231. int list0, int list1){
  2232. MpegEncContext * const s = &h->s;
  2233. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  2234. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  2235. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  2236. x_offset += 8*s->mb_x;
  2237. y_offset += 8*(s->mb_y >> MB_MBAFF);
  2238. if(list0 && list1){
  2239. /* don't optimize for luma-only case, since B-frames usually
  2240. * use implicit weights => chroma too. */
  2241. uint8_t *tmp_cb = s->obmc_scratchpad;
  2242. uint8_t *tmp_cr = s->obmc_scratchpad + 8;
  2243. uint8_t *tmp_y = s->obmc_scratchpad + 8*h->mb_uvlinesize;
  2244. int refn0 = h->ref_cache[0][ scan8[n] ];
  2245. int refn1 = h->ref_cache[1][ scan8[n] ];
  2246. mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
  2247. dest_y, dest_cb, dest_cr,
  2248. x_offset, y_offset, qpix_put, chroma_put);
  2249. mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
  2250. tmp_y, tmp_cb, tmp_cr,
  2251. x_offset, y_offset, qpix_put, chroma_put);
  2252. if(h->use_weight == 2){
  2253. int weight0 = h->implicit_weight[refn0][refn1];
  2254. int weight1 = 64 - weight0;
  2255. luma_weight_avg( dest_y, tmp_y, h-> mb_linesize, 5, weight0, weight1, 0);
  2256. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, 5, weight0, weight1, 0);
  2257. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, 5, weight0, weight1, 0);
  2258. }else{
  2259. luma_weight_avg(dest_y, tmp_y, h->mb_linesize, h->luma_log2_weight_denom,
  2260. h->luma_weight[0][refn0], h->luma_weight[1][refn1],
  2261. h->luma_offset[0][refn0] + h->luma_offset[1][refn1]);
  2262. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2263. h->chroma_weight[0][refn0][0], h->chroma_weight[1][refn1][0],
  2264. h->chroma_offset[0][refn0][0] + h->chroma_offset[1][refn1][0]);
  2265. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2266. h->chroma_weight[0][refn0][1], h->chroma_weight[1][refn1][1],
  2267. h->chroma_offset[0][refn0][1] + h->chroma_offset[1][refn1][1]);
  2268. }
  2269. }else{
  2270. int list = list1 ? 1 : 0;
  2271. int refn = h->ref_cache[list][ scan8[n] ];
  2272. Picture *ref= &h->ref_list[list][refn];
  2273. mc_dir_part(h, ref, n, square, chroma_height, delta, list,
  2274. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2275. qpix_put, chroma_put);
  2276. luma_weight_op(dest_y, h->mb_linesize, h->luma_log2_weight_denom,
  2277. h->luma_weight[list][refn], h->luma_offset[list][refn]);
  2278. if(h->use_weight_chroma){
  2279. chroma_weight_op(dest_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2280. h->chroma_weight[list][refn][0], h->chroma_offset[list][refn][0]);
  2281. chroma_weight_op(dest_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  2282. h->chroma_weight[list][refn][1], h->chroma_offset[list][refn][1]);
  2283. }
  2284. }
  2285. }
  2286. static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
  2287. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2288. int x_offset, int y_offset,
  2289. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  2290. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  2291. h264_weight_func *weight_op, h264_biweight_func *weight_avg,
  2292. int list0, int list1){
  2293. if((h->use_weight==2 && list0 && list1
  2294. && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ] != 32))
  2295. || h->use_weight==1)
  2296. mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  2297. x_offset, y_offset, qpix_put, chroma_put,
  2298. weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
  2299. else
  2300. mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  2301. x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
  2302. }
  2303. static inline void prefetch_motion(H264Context *h, int list){
  2304. /* fetch pixels for estimated mv 4 macroblocks ahead
  2305. * optimized for 64byte cache lines */
  2306. MpegEncContext * const s = &h->s;
  2307. const int refn = h->ref_cache[list][scan8[0]];
  2308. if(refn >= 0){
  2309. const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
  2310. const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
  2311. uint8_t **src= h->ref_list[list][refn].data;
  2312. int off= mx + (my + (s->mb_x&3)*4)*h->mb_linesize + 64;
  2313. s->dsp.prefetch(src[0]+off, s->linesize, 4);
  2314. off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
  2315. s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
  2316. }
  2317. }
  2318. static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2319. qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
  2320. qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
  2321. h264_weight_func *weight_op, h264_biweight_func *weight_avg){
  2322. MpegEncContext * const s = &h->s;
  2323. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2324. const int mb_type= s->current_picture.mb_type[mb_xy];
  2325. assert(IS_INTER(mb_type));
  2326. prefetch_motion(h, 0);
  2327. if(IS_16X16(mb_type)){
  2328. mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
  2329. qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
  2330. &weight_op[0], &weight_avg[0],
  2331. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2332. }else if(IS_16X8(mb_type)){
  2333. mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
  2334. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  2335. &weight_op[1], &weight_avg[1],
  2336. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2337. mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
  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, 1, 0), IS_DIR(mb_type, 1, 1));
  2341. }else if(IS_8X16(mb_type)){
  2342. mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
  2343. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2344. &weight_op[2], &weight_avg[2],
  2345. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  2346. mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 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, 1, 0), IS_DIR(mb_type, 1, 1));
  2350. }else{
  2351. int i;
  2352. assert(IS_8X8(mb_type));
  2353. for(i=0; i<4; i++){
  2354. const int sub_mb_type= h->sub_mb_type[i];
  2355. const int n= 4*i;
  2356. int x_offset= (i&1)<<2;
  2357. int y_offset= (i&2)<<1;
  2358. if(IS_SUB_8X8(sub_mb_type)){
  2359. mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2360. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  2361. &weight_op[3], &weight_avg[3],
  2362. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2363. }else if(IS_SUB_8X4(sub_mb_type)){
  2364. mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2365. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  2366. &weight_op[4], &weight_avg[4],
  2367. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2368. mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
  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. }else if(IS_SUB_4X8(sub_mb_type)){
  2373. mc_part(h, n , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  2374. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2375. &weight_op[5], &weight_avg[5],
  2376. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2377. mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, 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. }else{
  2382. int j;
  2383. assert(IS_SUB_4X4(sub_mb_type));
  2384. for(j=0; j<4; j++){
  2385. int sub_x_offset= x_offset + 2*(j&1);
  2386. int sub_y_offset= y_offset + (j&2);
  2387. mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
  2388. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  2389. &weight_op[6], &weight_avg[6],
  2390. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  2391. }
  2392. }
  2393. }
  2394. }
  2395. prefetch_motion(h, 1);
  2396. }
  2397. static void decode_init_vlc(void){
  2398. static int done = 0;
  2399. if (!done) {
  2400. int i;
  2401. done = 1;
  2402. init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5,
  2403. &chroma_dc_coeff_token_len [0], 1, 1,
  2404. &chroma_dc_coeff_token_bits[0], 1, 1, 1);
  2405. for(i=0; i<4; i++){
  2406. init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17,
  2407. &coeff_token_len [i][0], 1, 1,
  2408. &coeff_token_bits[i][0], 1, 1, 1);
  2409. }
  2410. for(i=0; i<3; i++){
  2411. init_vlc(&chroma_dc_total_zeros_vlc[i], CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
  2412. &chroma_dc_total_zeros_len [i][0], 1, 1,
  2413. &chroma_dc_total_zeros_bits[i][0], 1, 1, 1);
  2414. }
  2415. for(i=0; i<15; i++){
  2416. init_vlc(&total_zeros_vlc[i], TOTAL_ZEROS_VLC_BITS, 16,
  2417. &total_zeros_len [i][0], 1, 1,
  2418. &total_zeros_bits[i][0], 1, 1, 1);
  2419. }
  2420. for(i=0; i<6; i++){
  2421. init_vlc(&run_vlc[i], RUN_VLC_BITS, 7,
  2422. &run_len [i][0], 1, 1,
  2423. &run_bits[i][0], 1, 1, 1);
  2424. }
  2425. init_vlc(&run7_vlc, RUN7_VLC_BITS, 16,
  2426. &run_len [6][0], 1, 1,
  2427. &run_bits[6][0], 1, 1, 1);
  2428. }
  2429. }
  2430. /**
  2431. * Sets the intra prediction function pointers.
  2432. */
  2433. static void init_pred_ptrs(H264Context *h){
  2434. // MpegEncContext * const s = &h->s;
  2435. h->pred4x4[VERT_PRED ]= pred4x4_vertical_c;
  2436. h->pred4x4[HOR_PRED ]= pred4x4_horizontal_c;
  2437. h->pred4x4[DC_PRED ]= pred4x4_dc_c;
  2438. h->pred4x4[DIAG_DOWN_LEFT_PRED ]= pred4x4_down_left_c;
  2439. h->pred4x4[DIAG_DOWN_RIGHT_PRED]= pred4x4_down_right_c;
  2440. h->pred4x4[VERT_RIGHT_PRED ]= pred4x4_vertical_right_c;
  2441. h->pred4x4[HOR_DOWN_PRED ]= pred4x4_horizontal_down_c;
  2442. h->pred4x4[VERT_LEFT_PRED ]= pred4x4_vertical_left_c;
  2443. h->pred4x4[HOR_UP_PRED ]= pred4x4_horizontal_up_c;
  2444. h->pred4x4[LEFT_DC_PRED ]= pred4x4_left_dc_c;
  2445. h->pred4x4[TOP_DC_PRED ]= pred4x4_top_dc_c;
  2446. h->pred4x4[DC_128_PRED ]= pred4x4_128_dc_c;
  2447. h->pred8x8l[VERT_PRED ]= pred8x8l_vertical_c;
  2448. h->pred8x8l[HOR_PRED ]= pred8x8l_horizontal_c;
  2449. h->pred8x8l[DC_PRED ]= pred8x8l_dc_c;
  2450. h->pred8x8l[DIAG_DOWN_LEFT_PRED ]= pred8x8l_down_left_c;
  2451. h->pred8x8l[DIAG_DOWN_RIGHT_PRED]= pred8x8l_down_right_c;
  2452. h->pred8x8l[VERT_RIGHT_PRED ]= pred8x8l_vertical_right_c;
  2453. h->pred8x8l[HOR_DOWN_PRED ]= pred8x8l_horizontal_down_c;
  2454. h->pred8x8l[VERT_LEFT_PRED ]= pred8x8l_vertical_left_c;
  2455. h->pred8x8l[HOR_UP_PRED ]= pred8x8l_horizontal_up_c;
  2456. h->pred8x8l[LEFT_DC_PRED ]= pred8x8l_left_dc_c;
  2457. h->pred8x8l[TOP_DC_PRED ]= pred8x8l_top_dc_c;
  2458. h->pred8x8l[DC_128_PRED ]= pred8x8l_128_dc_c;
  2459. h->pred8x8[DC_PRED8x8 ]= ff_pred8x8_dc_c;
  2460. h->pred8x8[VERT_PRED8x8 ]= ff_pred8x8_vertical_c;
  2461. h->pred8x8[HOR_PRED8x8 ]= ff_pred8x8_horizontal_c;
  2462. h->pred8x8[PLANE_PRED8x8 ]= ff_pred8x8_plane_c;
  2463. h->pred8x8[LEFT_DC_PRED8x8]= ff_pred8x8_left_dc_c;
  2464. h->pred8x8[TOP_DC_PRED8x8 ]= ff_pred8x8_top_dc_c;
  2465. h->pred8x8[DC_128_PRED8x8 ]= ff_pred8x8_128_dc_c;
  2466. h->pred16x16[DC_PRED8x8 ]= ff_pred16x16_dc_c;
  2467. h->pred16x16[VERT_PRED8x8 ]= ff_pred16x16_vertical_c;
  2468. h->pred16x16[HOR_PRED8x8 ]= ff_pred16x16_horizontal_c;
  2469. h->pred16x16[PLANE_PRED8x8 ]= ff_pred16x16_plane_c;
  2470. h->pred16x16[LEFT_DC_PRED8x8]= ff_pred16x16_left_dc_c;
  2471. h->pred16x16[TOP_DC_PRED8x8 ]= ff_pred16x16_top_dc_c;
  2472. h->pred16x16[DC_128_PRED8x8 ]= ff_pred16x16_128_dc_c;
  2473. }
  2474. static void free_tables(H264Context *h){
  2475. av_freep(&h->intra4x4_pred_mode);
  2476. av_freep(&h->chroma_pred_mode_table);
  2477. av_freep(&h->cbp_table);
  2478. av_freep(&h->mvd_table[0]);
  2479. av_freep(&h->mvd_table[1]);
  2480. av_freep(&h->direct_table);
  2481. av_freep(&h->non_zero_count);
  2482. av_freep(&h->slice_table_base);
  2483. av_freep(&h->top_borders[1]);
  2484. av_freep(&h->top_borders[0]);
  2485. h->slice_table= NULL;
  2486. av_freep(&h->mb2b_xy);
  2487. av_freep(&h->mb2b8_xy);
  2488. av_freep(&h->s.obmc_scratchpad);
  2489. }
  2490. static void init_dequant8_coeff_table(H264Context *h){
  2491. int i,q,x;
  2492. const int transpose = (h->s.dsp.h264_idct8_add != ff_h264_idct8_add_c); //FIXME ugly
  2493. h->dequant8_coeff[0] = h->dequant8_buffer[0];
  2494. h->dequant8_coeff[1] = h->dequant8_buffer[1];
  2495. for(i=0; i<2; i++ ){
  2496. if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
  2497. h->dequant8_coeff[1] = h->dequant8_buffer[0];
  2498. break;
  2499. }
  2500. for(q=0; q<52; q++){
  2501. int shift = ff_div6[q];
  2502. int idx = ff_rem6[q];
  2503. for(x=0; x<64; x++)
  2504. h->dequant8_coeff[i][q][transpose ? (x>>3)|((x&7)<<3) : x] =
  2505. ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
  2506. h->pps.scaling_matrix8[i][x]) << shift;
  2507. }
  2508. }
  2509. }
  2510. static void init_dequant4_coeff_table(H264Context *h){
  2511. int i,j,q,x;
  2512. const int transpose = (h->s.dsp.h264_idct_add != ff_h264_idct_add_c); //FIXME ugly
  2513. for(i=0; i<6; i++ ){
  2514. h->dequant4_coeff[i] = h->dequant4_buffer[i];
  2515. for(j=0; j<i; j++){
  2516. if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
  2517. h->dequant4_coeff[i] = h->dequant4_buffer[j];
  2518. break;
  2519. }
  2520. }
  2521. if(j<i)
  2522. continue;
  2523. for(q=0; q<52; q++){
  2524. int shift = ff_div6[q] + 2;
  2525. int idx = ff_rem6[q];
  2526. for(x=0; x<16; x++)
  2527. h->dequant4_coeff[i][q][transpose ? (x>>2)|((x<<2)&0xF) : x] =
  2528. ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
  2529. h->pps.scaling_matrix4[i][x]) << shift;
  2530. }
  2531. }
  2532. }
  2533. static void init_dequant_tables(H264Context *h){
  2534. int i,x;
  2535. init_dequant4_coeff_table(h);
  2536. if(h->pps.transform_8x8_mode)
  2537. init_dequant8_coeff_table(h);
  2538. if(h->sps.transform_bypass){
  2539. for(i=0; i<6; i++)
  2540. for(x=0; x<16; x++)
  2541. h->dequant4_coeff[i][0][x] = 1<<6;
  2542. if(h->pps.transform_8x8_mode)
  2543. for(i=0; i<2; i++)
  2544. for(x=0; x<64; x++)
  2545. h->dequant8_coeff[i][0][x] = 1<<6;
  2546. }
  2547. }
  2548. /**
  2549. * allocates tables.
  2550. * needs width/height
  2551. */
  2552. static int alloc_tables(H264Context *h){
  2553. MpegEncContext * const s = &h->s;
  2554. const int big_mb_num= s->mb_stride * (s->mb_height+1);
  2555. int x,y;
  2556. CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t))
  2557. CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t))
  2558. CHECKED_ALLOCZ(h->slice_table_base , (big_mb_num+s->mb_stride) * sizeof(uint8_t))
  2559. CHECKED_ALLOCZ(h->top_borders[0] , s->mb_width * (16+8+8) * sizeof(uint8_t))
  2560. CHECKED_ALLOCZ(h->top_borders[1] , s->mb_width * (16+8+8) * sizeof(uint8_t))
  2561. CHECKED_ALLOCZ(h->cbp_table, big_mb_num * sizeof(uint16_t))
  2562. if( h->pps.cabac ) {
  2563. CHECKED_ALLOCZ(h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t))
  2564. CHECKED_ALLOCZ(h->mvd_table[0], 32*big_mb_num * sizeof(uint16_t));
  2565. CHECKED_ALLOCZ(h->mvd_table[1], 32*big_mb_num * sizeof(uint16_t));
  2566. CHECKED_ALLOCZ(h->direct_table, 32*big_mb_num * sizeof(uint8_t));
  2567. }
  2568. memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride) * sizeof(uint8_t));
  2569. h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
  2570. CHECKED_ALLOCZ(h->mb2b_xy , big_mb_num * sizeof(uint32_t));
  2571. CHECKED_ALLOCZ(h->mb2b8_xy , big_mb_num * sizeof(uint32_t));
  2572. for(y=0; y<s->mb_height; y++){
  2573. for(x=0; x<s->mb_width; x++){
  2574. const int mb_xy= x + y*s->mb_stride;
  2575. const int b_xy = 4*x + 4*y*h->b_stride;
  2576. const int b8_xy= 2*x + 2*y*h->b8_stride;
  2577. h->mb2b_xy [mb_xy]= b_xy;
  2578. h->mb2b8_xy[mb_xy]= b8_xy;
  2579. }
  2580. }
  2581. s->obmc_scratchpad = NULL;
  2582. if(!h->dequant4_coeff[0])
  2583. init_dequant_tables(h);
  2584. return 0;
  2585. fail:
  2586. free_tables(h);
  2587. return -1;
  2588. }
  2589. static void common_init(H264Context *h){
  2590. MpegEncContext * const s = &h->s;
  2591. s->width = s->avctx->width;
  2592. s->height = s->avctx->height;
  2593. s->codec_id= s->avctx->codec->id;
  2594. init_pred_ptrs(h);
  2595. h->dequant_coeff_pps= -1;
  2596. s->unrestricted_mv=1;
  2597. s->decode=1; //FIXME
  2598. memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  2599. memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  2600. }
  2601. static int decode_init(AVCodecContext *avctx){
  2602. H264Context *h= avctx->priv_data;
  2603. MpegEncContext * const s = &h->s;
  2604. MPV_decode_defaults(s);
  2605. s->avctx = avctx;
  2606. common_init(h);
  2607. s->out_format = FMT_H264;
  2608. s->workaround_bugs= avctx->workaround_bugs;
  2609. // set defaults
  2610. // s->decode_mb= ff_h263_decode_mb;
  2611. s->low_delay= 1;
  2612. avctx->pix_fmt= PIX_FMT_YUV420P;
  2613. decode_init_vlc();
  2614. if(avctx->extradata_size > 0 && avctx->extradata &&
  2615. *(char *)avctx->extradata == 1){
  2616. h->is_avc = 1;
  2617. h->got_avcC = 0;
  2618. } else {
  2619. h->is_avc = 0;
  2620. }
  2621. return 0;
  2622. }
  2623. static int frame_start(H264Context *h){
  2624. MpegEncContext * const s = &h->s;
  2625. int i;
  2626. if(MPV_frame_start(s, s->avctx) < 0)
  2627. return -1;
  2628. ff_er_frame_start(s);
  2629. assert(s->linesize && s->uvlinesize);
  2630. for(i=0; i<16; i++){
  2631. h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  2632. h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
  2633. }
  2634. for(i=0; i<4; i++){
  2635. h->block_offset[16+i]=
  2636. h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  2637. h->block_offset[24+16+i]=
  2638. h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  2639. }
  2640. /* can't be in alloc_tables because linesize isn't known there.
  2641. * FIXME: redo bipred weight to not require extra buffer? */
  2642. if(!s->obmc_scratchpad)
  2643. s->obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
  2644. /* some macroblocks will be accessed before they're available */
  2645. if(FRAME_MBAFF)
  2646. memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(uint8_t));
  2647. // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
  2648. return 0;
  2649. }
  2650. static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  2651. MpegEncContext * const s = &h->s;
  2652. int i;
  2653. src_y -= linesize;
  2654. src_cb -= uvlinesize;
  2655. src_cr -= uvlinesize;
  2656. // There are two lines saved, the line above the the top macroblock of a pair,
  2657. // and the line above the bottom macroblock
  2658. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  2659. for(i=1; i<17; i++){
  2660. h->left_border[i]= src_y[15+i* linesize];
  2661. }
  2662. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 16*linesize);
  2663. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize);
  2664. if(!(s->flags&CODEC_FLAG_GRAY)){
  2665. h->left_border[17 ]= h->top_borders[0][s->mb_x][16+7];
  2666. h->left_border[17+9]= h->top_borders[0][s->mb_x][24+7];
  2667. for(i=1; i<9; i++){
  2668. h->left_border[i+17 ]= src_cb[7+i*uvlinesize];
  2669. h->left_border[i+17+9]= src_cr[7+i*uvlinesize];
  2670. }
  2671. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize);
  2672. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize);
  2673. }
  2674. }
  2675. 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){
  2676. MpegEncContext * const s = &h->s;
  2677. int temp8, i;
  2678. uint64_t temp64;
  2679. int deblock_left = (s->mb_x > 0);
  2680. int deblock_top = (s->mb_y > 0);
  2681. src_y -= linesize + 1;
  2682. src_cb -= uvlinesize + 1;
  2683. src_cr -= uvlinesize + 1;
  2684. #define XCHG(a,b,t,xchg)\
  2685. t= a;\
  2686. if(xchg)\
  2687. a= b;\
  2688. b= t;
  2689. if(deblock_left){
  2690. for(i = !deblock_top; i<17; i++){
  2691. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  2692. }
  2693. }
  2694. if(deblock_top){
  2695. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  2696. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  2697. if(s->mb_x+1 < s->mb_width){
  2698. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  2699. }
  2700. }
  2701. if(!(s->flags&CODEC_FLAG_GRAY)){
  2702. if(deblock_left){
  2703. for(i = !deblock_top; i<9; i++){
  2704. XCHG(h->left_border[i+17 ], src_cb[i*uvlinesize], temp8, xchg);
  2705. XCHG(h->left_border[i+17+9], src_cr[i*uvlinesize], temp8, xchg);
  2706. }
  2707. }
  2708. if(deblock_top){
  2709. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  2710. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  2711. }
  2712. }
  2713. }
  2714. static inline void backup_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
  2715. MpegEncContext * const s = &h->s;
  2716. int i;
  2717. src_y -= 2 * linesize;
  2718. src_cb -= 2 * uvlinesize;
  2719. src_cr -= 2 * uvlinesize;
  2720. // There are two lines saved, the line above the the top macroblock of a pair,
  2721. // and the line above the bottom macroblock
  2722. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  2723. h->left_border[1]= h->top_borders[1][s->mb_x][15];
  2724. for(i=2; i<34; i++){
  2725. h->left_border[i]= src_y[15+i* linesize];
  2726. }
  2727. *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 32*linesize);
  2728. *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+32*linesize);
  2729. *(uint64_t*)(h->top_borders[1][s->mb_x]+0)= *(uint64_t*)(src_y + 33*linesize);
  2730. *(uint64_t*)(h->top_borders[1][s->mb_x]+8)= *(uint64_t*)(src_y +8+33*linesize);
  2731. if(!(s->flags&CODEC_FLAG_GRAY)){
  2732. h->left_border[34 ]= h->top_borders[0][s->mb_x][16+7];
  2733. h->left_border[34+ 1]= h->top_borders[1][s->mb_x][16+7];
  2734. h->left_border[34+18 ]= h->top_borders[0][s->mb_x][24+7];
  2735. h->left_border[34+18+1]= h->top_borders[1][s->mb_x][24+7];
  2736. for(i=2; i<18; i++){
  2737. h->left_border[i+34 ]= src_cb[7+i*uvlinesize];
  2738. h->left_border[i+34+18]= src_cr[7+i*uvlinesize];
  2739. }
  2740. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+16*uvlinesize);
  2741. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+16*uvlinesize);
  2742. *(uint64_t*)(h->top_borders[1][s->mb_x]+16)= *(uint64_t*)(src_cb+17*uvlinesize);
  2743. *(uint64_t*)(h->top_borders[1][s->mb_x]+24)= *(uint64_t*)(src_cr+17*uvlinesize);
  2744. }
  2745. }
  2746. 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){
  2747. MpegEncContext * const s = &h->s;
  2748. int temp8, i;
  2749. uint64_t temp64;
  2750. int deblock_left = (s->mb_x > 0);
  2751. int deblock_top = (s->mb_y > 1);
  2752. 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);
  2753. src_y -= 2 * linesize + 1;
  2754. src_cb -= 2 * uvlinesize + 1;
  2755. src_cr -= 2 * uvlinesize + 1;
  2756. #define XCHG(a,b,t,xchg)\
  2757. t= a;\
  2758. if(xchg)\
  2759. a= b;\
  2760. b= t;
  2761. if(deblock_left){
  2762. for(i = (!deblock_top)<<1; i<34; i++){
  2763. XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
  2764. }
  2765. }
  2766. if(deblock_top){
  2767. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  2768. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  2769. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+0), *(uint64_t*)(src_y +1 +linesize), temp64, xchg);
  2770. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+8), *(uint64_t*)(src_y +9 +linesize), temp64, 1);
  2771. if(s->mb_x+1 < s->mb_width){
  2772. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  2773. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x+1]), *(uint64_t*)(src_y +17 +linesize), temp64, 1);
  2774. }
  2775. }
  2776. if(!(s->flags&CODEC_FLAG_GRAY)){
  2777. if(deblock_left){
  2778. for(i = (!deblock_top) << 1; i<18; i++){
  2779. XCHG(h->left_border[i+34 ], src_cb[i*uvlinesize], temp8, xchg);
  2780. XCHG(h->left_border[i+34+18], src_cr[i*uvlinesize], temp8, xchg);
  2781. }
  2782. }
  2783. if(deblock_top){
  2784. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  2785. XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  2786. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+16), *(uint64_t*)(src_cb+1 +uvlinesize), temp64, 1);
  2787. XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+24), *(uint64_t*)(src_cr+1 +uvlinesize), temp64, 1);
  2788. }
  2789. }
  2790. }
  2791. static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple){
  2792. MpegEncContext * const s = &h->s;
  2793. const int mb_x= s->mb_x;
  2794. const int mb_y= s->mb_y;
  2795. const int mb_xy= mb_x + mb_y*s->mb_stride;
  2796. const int mb_type= s->current_picture.mb_type[mb_xy];
  2797. uint8_t *dest_y, *dest_cb, *dest_cr;
  2798. int linesize, uvlinesize /*dct_offset*/;
  2799. int i;
  2800. int *block_offset = &h->block_offset[0];
  2801. const unsigned int bottom = mb_y & 1;
  2802. const int transform_bypass = (s->qscale == 0 && h->sps.transform_bypass), is_h264 = (simple || s->codec_id == CODEC_ID_H264);
  2803. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  2804. void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  2805. dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  2806. dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  2807. dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  2808. s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + 64, s->linesize, 4);
  2809. s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + 64, dest_cr - dest_cb, 2);
  2810. if (!simple && MB_FIELD) {
  2811. linesize = h->mb_linesize = s->linesize * 2;
  2812. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  2813. block_offset = &h->block_offset[24];
  2814. if(mb_y&1){ //FIXME move out of this func?
  2815. dest_y -= s->linesize*15;
  2816. dest_cb-= s->uvlinesize*7;
  2817. dest_cr-= s->uvlinesize*7;
  2818. }
  2819. if(FRAME_MBAFF) {
  2820. int list;
  2821. for(list=0; list<h->list_count; list++){
  2822. if(!USES_LIST(mb_type, list))
  2823. continue;
  2824. if(IS_16X16(mb_type)){
  2825. int8_t *ref = &h->ref_cache[list][scan8[0]];
  2826. fill_rectangle(ref, 4, 4, 8, 16+*ref^(s->mb_y&1), 1);
  2827. }else{
  2828. for(i=0; i<16; i+=4){
  2829. //FIXME can refs be smaller than 8x8 when !direct_8x8_inference ?
  2830. int ref = h->ref_cache[list][scan8[i]];
  2831. if(ref >= 0)
  2832. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, 16+ref^(s->mb_y&1), 1);
  2833. }
  2834. }
  2835. }
  2836. }
  2837. } else {
  2838. linesize = h->mb_linesize = s->linesize;
  2839. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  2840. // dct_offset = s->linesize * 16;
  2841. }
  2842. if(transform_bypass){
  2843. idct_dc_add =
  2844. idct_add = IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
  2845. }else if(IS_8x8DCT(mb_type)){
  2846. idct_dc_add = s->dsp.h264_idct8_dc_add;
  2847. idct_add = s->dsp.h264_idct8_add;
  2848. }else{
  2849. idct_dc_add = s->dsp.h264_idct_dc_add;
  2850. idct_add = s->dsp.h264_idct_add;
  2851. }
  2852. if(!simple && FRAME_MBAFF && h->deblocking_filter && IS_INTRA(mb_type)
  2853. && (!bottom || !IS_INTRA(s->current_picture.mb_type[mb_xy-s->mb_stride]))){
  2854. int mbt_y = mb_y&~1;
  2855. uint8_t *top_y = s->current_picture.data[0] + (mbt_y * 16* s->linesize ) + mb_x * 16;
  2856. uint8_t *top_cb = s->current_picture.data[1] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
  2857. uint8_t *top_cr = s->current_picture.data[2] + (mbt_y * 8 * s->uvlinesize) + mb_x * 8;
  2858. xchg_pair_border(h, top_y, top_cb, top_cr, s->linesize, s->uvlinesize, 1);
  2859. }
  2860. if (!simple && IS_INTRA_PCM(mb_type)) {
  2861. unsigned int x, y;
  2862. // The pixels are stored in h->mb array in the same order as levels,
  2863. // copy them in output in the correct order.
  2864. for(i=0; i<16; i++) {
  2865. for (y=0; y<4; y++) {
  2866. for (x=0; x<4; x++) {
  2867. *(dest_y + block_offset[i] + y*linesize + x) = h->mb[i*16+y*4+x];
  2868. }
  2869. }
  2870. }
  2871. for(i=16; i<16+4; i++) {
  2872. for (y=0; y<4; y++) {
  2873. for (x=0; x<4; x++) {
  2874. *(dest_cb + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  2875. }
  2876. }
  2877. }
  2878. for(i=20; i<20+4; i++) {
  2879. for (y=0; y<4; y++) {
  2880. for (x=0; x<4; x++) {
  2881. *(dest_cr + block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
  2882. }
  2883. }
  2884. }
  2885. } else {
  2886. if(IS_INTRA(mb_type)){
  2887. if(h->deblocking_filter && (simple || !FRAME_MBAFF))
  2888. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1);
  2889. if(simple || !(s->flags&CODEC_FLAG_GRAY)){
  2890. h->pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  2891. h->pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  2892. }
  2893. if(IS_INTRA4x4(mb_type)){
  2894. if(simple || !s->encoding){
  2895. if(IS_8x8DCT(mb_type)){
  2896. for(i=0; i<16; i+=4){
  2897. uint8_t * const ptr= dest_y + block_offset[i];
  2898. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  2899. const int nnz = h->non_zero_count_cache[ scan8[i] ];
  2900. h->pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
  2901. (h->topright_samples_available<<i)&0x4000, linesize);
  2902. if(nnz){
  2903. if(nnz == 1 && h->mb[i*16])
  2904. idct_dc_add(ptr, h->mb + i*16, linesize);
  2905. else
  2906. idct_add(ptr, h->mb + i*16, linesize);
  2907. }
  2908. }
  2909. }else
  2910. for(i=0; i<16; i++){
  2911. uint8_t * const ptr= dest_y + block_offset[i];
  2912. uint8_t *topright;
  2913. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  2914. int nnz, tr;
  2915. if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
  2916. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  2917. assert(mb_y || linesize <= block_offset[i]);
  2918. if(!topright_avail){
  2919. tr= ptr[3 - linesize]*0x01010101;
  2920. topright= (uint8_t*) &tr;
  2921. }else
  2922. topright= ptr + 4 - linesize;
  2923. }else
  2924. topright= NULL;
  2925. h->pred4x4[ dir ](ptr, topright, linesize);
  2926. nnz = h->non_zero_count_cache[ scan8[i] ];
  2927. if(nnz){
  2928. if(is_h264){
  2929. if(nnz == 1 && h->mb[i*16])
  2930. idct_dc_add(ptr, h->mb + i*16, linesize);
  2931. else
  2932. idct_add(ptr, h->mb + i*16, linesize);
  2933. }else
  2934. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
  2935. }
  2936. }
  2937. }
  2938. }else{
  2939. h->pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  2940. if(is_h264){
  2941. if(!transform_bypass)
  2942. h264_luma_dc_dequant_idct_c(h->mb, s->qscale, h->dequant4_coeff[IS_INTRA(mb_type) ? 0:3][s->qscale][0]);
  2943. }else
  2944. svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
  2945. }
  2946. if(h->deblocking_filter && (simple || !FRAME_MBAFF))
  2947. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
  2948. }else if(is_h264){
  2949. hl_motion(h, dest_y, dest_cb, dest_cr,
  2950. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  2951. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  2952. s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
  2953. }
  2954. if(!IS_INTRA4x4(mb_type)){
  2955. if(is_h264){
  2956. if(IS_INTRA16x16(mb_type)){
  2957. for(i=0; i<16; i++){
  2958. if(h->non_zero_count_cache[ scan8[i] ])
  2959. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  2960. else if(h->mb[i*16])
  2961. idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  2962. }
  2963. }else{
  2964. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  2965. for(i=0; i<16; i+=di){
  2966. int nnz = h->non_zero_count_cache[ scan8[i] ];
  2967. if(nnz){
  2968. if(nnz==1 && h->mb[i*16])
  2969. idct_dc_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  2970. else
  2971. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  2972. }
  2973. }
  2974. }
  2975. }else{
  2976. for(i=0; i<16; i++){
  2977. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  2978. uint8_t * const ptr= dest_y + block_offset[i];
  2979. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  2980. }
  2981. }
  2982. }
  2983. }
  2984. if(simple || !(s->flags&CODEC_FLAG_GRAY)){
  2985. uint8_t *dest[2] = {dest_cb, dest_cr};
  2986. if(transform_bypass){
  2987. idct_add = idct_dc_add = s->dsp.add_pixels4;
  2988. }else{
  2989. idct_add = s->dsp.h264_idct_add;
  2990. idct_dc_add = s->dsp.h264_idct_dc_add;
  2991. 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]);
  2992. 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]);
  2993. }
  2994. if(is_h264){
  2995. for(i=16; i<16+8; i++){
  2996. if(h->non_zero_count_cache[ scan8[i] ])
  2997. idct_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  2998. else if(h->mb[i*16])
  2999. idct_dc_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  3000. }
  3001. }else{
  3002. for(i=16; i<16+8; i++){
  3003. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  3004. uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
  3005. svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
  3006. }
  3007. }
  3008. }
  3009. }
  3010. }
  3011. if(h->deblocking_filter) {
  3012. if (!simple && FRAME_MBAFF) {
  3013. //FIXME try deblocking one mb at a time?
  3014. // the reduction in load/storing mvs and such might outweigh the extra backup/xchg_border
  3015. const int mb_y = s->mb_y - 1;
  3016. uint8_t *pair_dest_y, *pair_dest_cb, *pair_dest_cr;
  3017. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3018. const int mb_type_top = s->current_picture.mb_type[mb_xy];
  3019. const int mb_type_bottom= s->current_picture.mb_type[mb_xy+s->mb_stride];
  3020. if (!bottom) return;
  3021. pair_dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  3022. pair_dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3023. pair_dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  3024. if(IS_INTRA(mb_type_top | mb_type_bottom))
  3025. xchg_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize, 0);
  3026. backup_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize);
  3027. // deblock a pair
  3028. // top
  3029. s->mb_y--;
  3030. 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);
  3031. fill_caches(h, mb_type_top, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3032. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy]);
  3033. filter_mb(h, mb_x, mb_y, pair_dest_y, pair_dest_cb, pair_dest_cr, linesize, uvlinesize);
  3034. // bottom
  3035. s->mb_y++;
  3036. tprintf(h->s.avctx, "call mbaff filter_mb\n");
  3037. fill_caches(h, mb_type_bottom, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3038. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy+s->mb_stride]);
  3039. filter_mb(h, mb_x, mb_y+1, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3040. } else {
  3041. tprintf(h->s.avctx, "call filter_mb\n");
  3042. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3043. fill_caches(h, mb_type, 1); //FIXME don't fill stuff which isn't used by filter_mb
  3044. filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  3045. }
  3046. }
  3047. }
  3048. /**
  3049. * Process a macroblock; this case avoids checks for expensive uncommon cases.
  3050. */
  3051. static void hl_decode_mb_simple(H264Context *h){
  3052. hl_decode_mb_internal(h, 1);
  3053. }
  3054. /**
  3055. * Process a macroblock; this handles edge cases, such as interlacing.
  3056. */
  3057. static void av_noinline hl_decode_mb_complex(H264Context *h){
  3058. hl_decode_mb_internal(h, 0);
  3059. }
  3060. static void hl_decode_mb(H264Context *h){
  3061. MpegEncContext * const s = &h->s;
  3062. const int mb_x= s->mb_x;
  3063. const int mb_y= s->mb_y;
  3064. const int mb_xy= mb_x + mb_y*s->mb_stride;
  3065. const int mb_type= s->current_picture.mb_type[mb_xy];
  3066. 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;
  3067. if(!s->decode)
  3068. return;
  3069. if (is_complex)
  3070. hl_decode_mb_complex(h);
  3071. else hl_decode_mb_simple(h);
  3072. }
  3073. /**
  3074. * fills the default_ref_list.
  3075. */
  3076. static int fill_default_ref_list(H264Context *h){
  3077. MpegEncContext * const s = &h->s;
  3078. int i;
  3079. int smallest_poc_greater_than_current = -1;
  3080. Picture sorted_short_ref[32];
  3081. if(h->slice_type==B_TYPE){
  3082. int out_i;
  3083. int limit= INT_MIN;
  3084. /* sort frame according to poc in B slice */
  3085. for(out_i=0; out_i<h->short_ref_count; out_i++){
  3086. int best_i=INT_MIN;
  3087. int best_poc=INT_MAX;
  3088. for(i=0; i<h->short_ref_count; i++){
  3089. const int poc= h->short_ref[i]->poc;
  3090. if(poc > limit && poc < best_poc){
  3091. best_poc= poc;
  3092. best_i= i;
  3093. }
  3094. }
  3095. assert(best_i != INT_MIN);
  3096. limit= best_poc;
  3097. sorted_short_ref[out_i]= *h->short_ref[best_i];
  3098. 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);
  3099. if (-1 == smallest_poc_greater_than_current) {
  3100. if (h->short_ref[best_i]->poc >= s->current_picture_ptr->poc) {
  3101. smallest_poc_greater_than_current = out_i;
  3102. }
  3103. }
  3104. }
  3105. }
  3106. if(s->picture_structure == PICT_FRAME){
  3107. if(h->slice_type==B_TYPE){
  3108. int list;
  3109. tprintf(h->s.avctx, "current poc: %d, smallest_poc_greater_than_current: %d\n", s->current_picture_ptr->poc, smallest_poc_greater_than_current);
  3110. // find the largest poc
  3111. for(list=0; list<2; list++){
  3112. int index = 0;
  3113. int j= -99;
  3114. int step= list ? -1 : 1;
  3115. for(i=0; i<h->short_ref_count && index < h->ref_count[list]; i++, j+=step) {
  3116. while(j<0 || j>= h->short_ref_count){
  3117. if(j != -99 && step == (list ? -1 : 1))
  3118. return -1;
  3119. step = -step;
  3120. j= smallest_poc_greater_than_current + (step>>1);
  3121. }
  3122. if(sorted_short_ref[j].reference != 3) continue;
  3123. h->default_ref_list[list][index ]= sorted_short_ref[j];
  3124. h->default_ref_list[list][index++].pic_id= sorted_short_ref[j].frame_num;
  3125. }
  3126. for(i = 0; i < 16 && index < h->ref_count[ list ]; i++){
  3127. if(h->long_ref[i] == NULL) continue;
  3128. if(h->long_ref[i]->reference != 3) continue;
  3129. h->default_ref_list[ list ][index ]= *h->long_ref[i];
  3130. h->default_ref_list[ list ][index++].pic_id= i;;
  3131. }
  3132. if(list && (smallest_poc_greater_than_current<=0 || smallest_poc_greater_than_current>=h->short_ref_count) && (1 < index)){
  3133. // swap the two first elements of L1 when
  3134. // L0 and L1 are identical
  3135. Picture temp= h->default_ref_list[1][0];
  3136. h->default_ref_list[1][0] = h->default_ref_list[1][1];
  3137. h->default_ref_list[1][1] = temp;
  3138. }
  3139. if(index < h->ref_count[ list ])
  3140. memset(&h->default_ref_list[list][index], 0, sizeof(Picture)*(h->ref_count[ list ] - index));
  3141. }
  3142. }else{
  3143. int index=0;
  3144. for(i=0; i<h->short_ref_count; i++){
  3145. if(h->short_ref[i]->reference != 3) continue; //FIXME refernce field shit
  3146. h->default_ref_list[0][index ]= *h->short_ref[i];
  3147. h->default_ref_list[0][index++].pic_id= h->short_ref[i]->frame_num;
  3148. }
  3149. for(i = 0; i < 16; i++){
  3150. if(h->long_ref[i] == NULL) continue;
  3151. if(h->long_ref[i]->reference != 3) continue;
  3152. h->default_ref_list[0][index ]= *h->long_ref[i];
  3153. h->default_ref_list[0][index++].pic_id= i;;
  3154. }
  3155. if(index < h->ref_count[0])
  3156. memset(&h->default_ref_list[0][index], 0, sizeof(Picture)*(h->ref_count[0] - index));
  3157. }
  3158. }else{ //FIELD
  3159. if(h->slice_type==B_TYPE){
  3160. }else{
  3161. //FIXME second field balh
  3162. }
  3163. }
  3164. #ifdef TRACE
  3165. for (i=0; i<h->ref_count[0]; i++) {
  3166. 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]);
  3167. }
  3168. if(h->slice_type==B_TYPE){
  3169. for (i=0; i<h->ref_count[1]; i++) {
  3170. 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]);
  3171. }
  3172. }
  3173. #endif
  3174. return 0;
  3175. }
  3176. static void print_short_term(H264Context *h);
  3177. static void print_long_term(H264Context *h);
  3178. static int decode_ref_pic_list_reordering(H264Context *h){
  3179. MpegEncContext * const s = &h->s;
  3180. int list, index;
  3181. print_short_term(h);
  3182. print_long_term(h);
  3183. if(h->slice_type==I_TYPE || h->slice_type==SI_TYPE) return 0; //FIXME move before func
  3184. for(list=0; list<h->list_count; list++){
  3185. memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
  3186. if(get_bits1(&s->gb)){
  3187. int pred= h->curr_pic_num;
  3188. for(index=0; ; index++){
  3189. unsigned int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);
  3190. unsigned int pic_id;
  3191. int i;
  3192. Picture *ref = NULL;
  3193. if(reordering_of_pic_nums_idc==3)
  3194. break;
  3195. if(index >= h->ref_count[list]){
  3196. av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
  3197. return -1;
  3198. }
  3199. if(reordering_of_pic_nums_idc<3){
  3200. if(reordering_of_pic_nums_idc<2){
  3201. const unsigned int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
  3202. if(abs_diff_pic_num >= h->max_pic_num){
  3203. av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
  3204. return -1;
  3205. }
  3206. if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
  3207. else pred+= abs_diff_pic_num;
  3208. pred &= h->max_pic_num - 1;
  3209. for(i= h->short_ref_count-1; i>=0; i--){
  3210. ref = h->short_ref[i];
  3211. assert(ref->reference == 3);
  3212. assert(!ref->long_ref);
  3213. if(ref->data[0] != NULL && ref->frame_num == pred && ref->long_ref == 0) // ignore non existing pictures by testing data[0] pointer
  3214. break;
  3215. }
  3216. if(i>=0)
  3217. ref->pic_id= ref->frame_num;
  3218. }else{
  3219. pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
  3220. if(pic_id>31){
  3221. av_log(h->s.avctx, AV_LOG_ERROR, "long_term_pic_idx overflow\n");
  3222. return -1;
  3223. }
  3224. ref = h->long_ref[pic_id];
  3225. if(ref){
  3226. ref->pic_id= pic_id;
  3227. assert(ref->reference == 3);
  3228. assert(ref->long_ref);
  3229. i=0;
  3230. }else{
  3231. i=-1;
  3232. }
  3233. }
  3234. if (i < 0) {
  3235. av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
  3236. memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
  3237. } else {
  3238. for(i=index; i+1<h->ref_count[list]; i++){
  3239. if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
  3240. break;
  3241. }
  3242. for(; i > index; i--){
  3243. h->ref_list[list][i]= h->ref_list[list][i-1];
  3244. }
  3245. h->ref_list[list][index]= *ref;
  3246. }
  3247. }else{
  3248. av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
  3249. return -1;
  3250. }
  3251. }
  3252. }
  3253. }
  3254. for(list=0; list<h->list_count; list++){
  3255. for(index= 0; index < h->ref_count[list]; index++){
  3256. if(!h->ref_list[list][index].data[0])
  3257. h->ref_list[list][index]= s->current_picture;
  3258. }
  3259. }
  3260. if(h->slice_type==B_TYPE && !h->direct_spatial_mv_pred)
  3261. direct_dist_scale_factor(h);
  3262. direct_ref_list_init(h);
  3263. return 0;
  3264. }
  3265. static void fill_mbaff_ref_list(H264Context *h){
  3266. int list, i, j;
  3267. for(list=0; list<2; list++){ //FIXME try list_count
  3268. for(i=0; i<h->ref_count[list]; i++){
  3269. Picture *frame = &h->ref_list[list][i];
  3270. Picture *field = &h->ref_list[list][16+2*i];
  3271. field[0] = *frame;
  3272. for(j=0; j<3; j++)
  3273. field[0].linesize[j] <<= 1;
  3274. field[1] = field[0];
  3275. for(j=0; j<3; j++)
  3276. field[1].data[j] += frame->linesize[j];
  3277. h->luma_weight[list][16+2*i] = h->luma_weight[list][16+2*i+1] = h->luma_weight[list][i];
  3278. h->luma_offset[list][16+2*i] = h->luma_offset[list][16+2*i+1] = h->luma_offset[list][i];
  3279. for(j=0; j<2; j++){
  3280. h->chroma_weight[list][16+2*i][j] = h->chroma_weight[list][16+2*i+1][j] = h->chroma_weight[list][i][j];
  3281. h->chroma_offset[list][16+2*i][j] = h->chroma_offset[list][16+2*i+1][j] = h->chroma_offset[list][i][j];
  3282. }
  3283. }
  3284. }
  3285. for(j=0; j<h->ref_count[1]; j++){
  3286. for(i=0; i<h->ref_count[0]; i++)
  3287. h->implicit_weight[j][16+2*i] = h->implicit_weight[j][16+2*i+1] = h->implicit_weight[j][i];
  3288. memcpy(h->implicit_weight[16+2*j], h->implicit_weight[j], sizeof(*h->implicit_weight));
  3289. memcpy(h->implicit_weight[16+2*j+1], h->implicit_weight[j], sizeof(*h->implicit_weight));
  3290. }
  3291. }
  3292. static int pred_weight_table(H264Context *h){
  3293. MpegEncContext * const s = &h->s;
  3294. int list, i;
  3295. int luma_def, chroma_def;
  3296. h->use_weight= 0;
  3297. h->use_weight_chroma= 0;
  3298. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  3299. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  3300. luma_def = 1<<h->luma_log2_weight_denom;
  3301. chroma_def = 1<<h->chroma_log2_weight_denom;
  3302. for(list=0; list<2; list++){
  3303. for(i=0; i<h->ref_count[list]; i++){
  3304. int luma_weight_flag, chroma_weight_flag;
  3305. luma_weight_flag= get_bits1(&s->gb);
  3306. if(luma_weight_flag){
  3307. h->luma_weight[list][i]= get_se_golomb(&s->gb);
  3308. h->luma_offset[list][i]= get_se_golomb(&s->gb);
  3309. if( h->luma_weight[list][i] != luma_def
  3310. || h->luma_offset[list][i] != 0)
  3311. h->use_weight= 1;
  3312. }else{
  3313. h->luma_weight[list][i]= luma_def;
  3314. h->luma_offset[list][i]= 0;
  3315. }
  3316. chroma_weight_flag= get_bits1(&s->gb);
  3317. if(chroma_weight_flag){
  3318. int j;
  3319. for(j=0; j<2; j++){
  3320. h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
  3321. h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
  3322. if( h->chroma_weight[list][i][j] != chroma_def
  3323. || h->chroma_offset[list][i][j] != 0)
  3324. h->use_weight_chroma= 1;
  3325. }
  3326. }else{
  3327. int j;
  3328. for(j=0; j<2; j++){
  3329. h->chroma_weight[list][i][j]= chroma_def;
  3330. h->chroma_offset[list][i][j]= 0;
  3331. }
  3332. }
  3333. }
  3334. if(h->slice_type != B_TYPE) break;
  3335. }
  3336. h->use_weight= h->use_weight || h->use_weight_chroma;
  3337. return 0;
  3338. }
  3339. static void implicit_weight_table(H264Context *h){
  3340. MpegEncContext * const s = &h->s;
  3341. int ref0, ref1;
  3342. int cur_poc = s->current_picture_ptr->poc;
  3343. if( h->ref_count[0] == 1 && h->ref_count[1] == 1
  3344. && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
  3345. h->use_weight= 0;
  3346. h->use_weight_chroma= 0;
  3347. return;
  3348. }
  3349. h->use_weight= 2;
  3350. h->use_weight_chroma= 2;
  3351. h->luma_log2_weight_denom= 5;
  3352. h->chroma_log2_weight_denom= 5;
  3353. for(ref0=0; ref0 < h->ref_count[0]; ref0++){
  3354. int poc0 = h->ref_list[0][ref0].poc;
  3355. for(ref1=0; ref1 < h->ref_count[1]; ref1++){
  3356. int poc1 = h->ref_list[1][ref1].poc;
  3357. int td = av_clip(poc1 - poc0, -128, 127);
  3358. if(td){
  3359. int tb = av_clip(cur_poc - poc0, -128, 127);
  3360. int tx = (16384 + (FFABS(td) >> 1)) / td;
  3361. int dist_scale_factor = av_clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
  3362. if(dist_scale_factor < -64 || dist_scale_factor > 128)
  3363. h->implicit_weight[ref0][ref1] = 32;
  3364. else
  3365. h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor;
  3366. }else
  3367. h->implicit_weight[ref0][ref1] = 32;
  3368. }
  3369. }
  3370. }
  3371. static inline void unreference_pic(H264Context *h, Picture *pic){
  3372. int i;
  3373. pic->reference=0;
  3374. if(pic == h->delayed_output_pic)
  3375. pic->reference=1;
  3376. else{
  3377. for(i = 0; h->delayed_pic[i]; i++)
  3378. if(pic == h->delayed_pic[i]){
  3379. pic->reference=1;
  3380. break;
  3381. }
  3382. }
  3383. }
  3384. /**
  3385. * instantaneous decoder refresh.
  3386. */
  3387. static void idr(H264Context *h){
  3388. int i;
  3389. for(i=0; i<16; i++){
  3390. if (h->long_ref[i] != NULL) {
  3391. unreference_pic(h, h->long_ref[i]);
  3392. h->long_ref[i]= NULL;
  3393. }
  3394. }
  3395. h->long_ref_count=0;
  3396. for(i=0; i<h->short_ref_count; i++){
  3397. unreference_pic(h, h->short_ref[i]);
  3398. h->short_ref[i]= NULL;
  3399. }
  3400. h->short_ref_count=0;
  3401. }
  3402. /* forget old pics after a seek */
  3403. static void flush_dpb(AVCodecContext *avctx){
  3404. H264Context *h= avctx->priv_data;
  3405. int i;
  3406. for(i=0; i<16; i++) {
  3407. if(h->delayed_pic[i])
  3408. h->delayed_pic[i]->reference= 0;
  3409. h->delayed_pic[i]= NULL;
  3410. }
  3411. if(h->delayed_output_pic)
  3412. h->delayed_output_pic->reference= 0;
  3413. h->delayed_output_pic= NULL;
  3414. idr(h);
  3415. if(h->s.current_picture_ptr)
  3416. h->s.current_picture_ptr->reference= 0;
  3417. }
  3418. /**
  3419. *
  3420. * @return the removed picture or NULL if an error occurs
  3421. */
  3422. static Picture * remove_short(H264Context *h, int frame_num){
  3423. MpegEncContext * const s = &h->s;
  3424. int i;
  3425. if(s->avctx->debug&FF_DEBUG_MMCO)
  3426. av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
  3427. for(i=0; i<h->short_ref_count; i++){
  3428. Picture *pic= h->short_ref[i];
  3429. if(s->avctx->debug&FF_DEBUG_MMCO)
  3430. av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
  3431. if(pic->frame_num == frame_num){
  3432. h->short_ref[i]= NULL;
  3433. memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i - 1)*sizeof(Picture*));
  3434. h->short_ref_count--;
  3435. return pic;
  3436. }
  3437. }
  3438. return NULL;
  3439. }
  3440. /**
  3441. *
  3442. * @return the removed picture or NULL if an error occurs
  3443. */
  3444. static Picture * remove_long(H264Context *h, int i){
  3445. Picture *pic;
  3446. pic= h->long_ref[i];
  3447. h->long_ref[i]= NULL;
  3448. if(pic) h->long_ref_count--;
  3449. return pic;
  3450. }
  3451. /**
  3452. * print short term list
  3453. */
  3454. static void print_short_term(H264Context *h) {
  3455. uint32_t i;
  3456. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3457. av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
  3458. for(i=0; i<h->short_ref_count; i++){
  3459. Picture *pic= h->short_ref[i];
  3460. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3461. }
  3462. }
  3463. }
  3464. /**
  3465. * print long term list
  3466. */
  3467. static void print_long_term(H264Context *h) {
  3468. uint32_t i;
  3469. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  3470. av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
  3471. for(i = 0; i < 16; i++){
  3472. Picture *pic= h->long_ref[i];
  3473. if (pic) {
  3474. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  3475. }
  3476. }
  3477. }
  3478. }
  3479. /**
  3480. * Executes the reference picture marking (memory management control operations).
  3481. */
  3482. static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
  3483. MpegEncContext * const s = &h->s;
  3484. int i, j;
  3485. int current_is_long=0;
  3486. Picture *pic;
  3487. if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
  3488. av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
  3489. for(i=0; i<mmco_count; i++){
  3490. if(s->avctx->debug&FF_DEBUG_MMCO)
  3491. 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);
  3492. switch(mmco[i].opcode){
  3493. case MMCO_SHORT2UNUSED:
  3494. pic= remove_short(h, mmco[i].short_frame_num);
  3495. if(pic)
  3496. unreference_pic(h, pic);
  3497. else if(s->avctx->debug&FF_DEBUG_MMCO)
  3498. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_short() failure\n");
  3499. break;
  3500. case MMCO_SHORT2LONG:
  3501. pic= remove_long(h, mmco[i].long_index);
  3502. if(pic) unreference_pic(h, pic);
  3503. h->long_ref[ mmco[i].long_index ]= remove_short(h, mmco[i].short_frame_num);
  3504. if (h->long_ref[ mmco[i].long_index ]){
  3505. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3506. h->long_ref_count++;
  3507. }
  3508. break;
  3509. case MMCO_LONG2UNUSED:
  3510. pic= remove_long(h, mmco[i].long_index);
  3511. if(pic)
  3512. unreference_pic(h, pic);
  3513. else if(s->avctx->debug&FF_DEBUG_MMCO)
  3514. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: remove_long() failure\n");
  3515. break;
  3516. case MMCO_LONG:
  3517. pic= remove_long(h, mmco[i].long_index);
  3518. if(pic) unreference_pic(h, pic);
  3519. h->long_ref[ mmco[i].long_index ]= s->current_picture_ptr;
  3520. h->long_ref[ mmco[i].long_index ]->long_ref=1;
  3521. h->long_ref_count++;
  3522. current_is_long=1;
  3523. break;
  3524. case MMCO_SET_MAX_LONG:
  3525. assert(mmco[i].long_index <= 16);
  3526. // just remove the long term which index is greater than new max
  3527. for(j = mmco[i].long_index; j<16; j++){
  3528. pic = remove_long(h, j);
  3529. if (pic) unreference_pic(h, pic);
  3530. }
  3531. break;
  3532. case MMCO_RESET:
  3533. while(h->short_ref_count){
  3534. pic= remove_short(h, h->short_ref[0]->frame_num);
  3535. if(pic) unreference_pic(h, pic);
  3536. }
  3537. for(j = 0; j < 16; j++) {
  3538. pic= remove_long(h, j);
  3539. if(pic) unreference_pic(h, pic);
  3540. }
  3541. break;
  3542. default: assert(0);
  3543. }
  3544. }
  3545. if(!current_is_long){
  3546. pic= remove_short(h, s->current_picture_ptr->frame_num);
  3547. if(pic){
  3548. unreference_pic(h, pic);
  3549. av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
  3550. }
  3551. if(h->short_ref_count)
  3552. memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
  3553. h->short_ref[0]= s->current_picture_ptr;
  3554. h->short_ref[0]->long_ref=0;
  3555. h->short_ref_count++;
  3556. }
  3557. print_short_term(h);
  3558. print_long_term(h);
  3559. return 0;
  3560. }
  3561. static int decode_ref_pic_marking(H264Context *h){
  3562. MpegEncContext * const s = &h->s;
  3563. int i;
  3564. if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
  3565. s->broken_link= get_bits1(&s->gb) -1;
  3566. h->mmco[0].long_index= get_bits1(&s->gb) - 1; // current_long_term_idx
  3567. if(h->mmco[0].long_index == -1)
  3568. h->mmco_index= 0;
  3569. else{
  3570. h->mmco[0].opcode= MMCO_LONG;
  3571. h->mmco_index= 1;
  3572. }
  3573. }else{
  3574. if(get_bits1(&s->gb)){ // adaptive_ref_pic_marking_mode_flag
  3575. for(i= 0; i<MAX_MMCO_COUNT; i++) {
  3576. MMCOOpcode opcode= get_ue_golomb(&s->gb);;
  3577. h->mmco[i].opcode= opcode;
  3578. if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
  3579. 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
  3580. /* if(h->mmco[i].short_frame_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_frame_num ] == NULL){
  3581. av_log(s->avctx, AV_LOG_ERROR, "illegal short ref in memory management control operation %d\n", mmco);
  3582. return -1;
  3583. }*/
  3584. }
  3585. if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
  3586. unsigned int long_index= get_ue_golomb(&s->gb);
  3587. if(/*h->mmco[i].long_index >= h->long_ref_count || h->long_ref[ h->mmco[i].long_index ] == NULL*/ long_index >= 16){
  3588. av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
  3589. return -1;
  3590. }
  3591. h->mmco[i].long_index= long_index;
  3592. }
  3593. if(opcode > (unsigned)MMCO_LONG){
  3594. av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
  3595. return -1;
  3596. }
  3597. if(opcode == MMCO_END)
  3598. break;
  3599. }
  3600. h->mmco_index= i;
  3601. }else{
  3602. assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
  3603. if(h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count){ //FIXME fields
  3604. h->mmco[0].opcode= MMCO_SHORT2UNUSED;
  3605. h->mmco[0].short_frame_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
  3606. h->mmco_index= 1;
  3607. }else
  3608. h->mmco_index= 0;
  3609. }
  3610. }
  3611. return 0;
  3612. }
  3613. static int init_poc(H264Context *h){
  3614. MpegEncContext * const s = &h->s;
  3615. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  3616. int field_poc[2];
  3617. if(h->nal_unit_type == NAL_IDR_SLICE){
  3618. h->frame_num_offset= 0;
  3619. }else{
  3620. if(h->frame_num < h->prev_frame_num)
  3621. h->frame_num_offset= h->prev_frame_num_offset + max_frame_num;
  3622. else
  3623. h->frame_num_offset= h->prev_frame_num_offset;
  3624. }
  3625. if(h->sps.poc_type==0){
  3626. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  3627. if(h->nal_unit_type == NAL_IDR_SLICE){
  3628. h->prev_poc_msb=
  3629. h->prev_poc_lsb= 0;
  3630. }
  3631. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  3632. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  3633. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  3634. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  3635. else
  3636. h->poc_msb = h->prev_poc_msb;
  3637. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  3638. field_poc[0] =
  3639. field_poc[1] = h->poc_msb + h->poc_lsb;
  3640. if(s->picture_structure == PICT_FRAME)
  3641. field_poc[1] += h->delta_poc_bottom;
  3642. }else if(h->sps.poc_type==1){
  3643. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  3644. int i;
  3645. if(h->sps.poc_cycle_length != 0)
  3646. abs_frame_num = h->frame_num_offset + h->frame_num;
  3647. else
  3648. abs_frame_num = 0;
  3649. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  3650. abs_frame_num--;
  3651. expected_delta_per_poc_cycle = 0;
  3652. for(i=0; i < h->sps.poc_cycle_length; i++)
  3653. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  3654. if(abs_frame_num > 0){
  3655. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  3656. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  3657. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  3658. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  3659. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  3660. } else
  3661. expectedpoc = 0;
  3662. if(h->nal_ref_idc == 0)
  3663. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  3664. field_poc[0] = expectedpoc + h->delta_poc[0];
  3665. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  3666. if(s->picture_structure == PICT_FRAME)
  3667. field_poc[1] += h->delta_poc[1];
  3668. }else{
  3669. int poc;
  3670. if(h->nal_unit_type == NAL_IDR_SLICE){
  3671. poc= 0;
  3672. }else{
  3673. if(h->nal_ref_idc) poc= 2*(h->frame_num_offset + h->frame_num);
  3674. else poc= 2*(h->frame_num_offset + h->frame_num) - 1;
  3675. }
  3676. field_poc[0]= poc;
  3677. field_poc[1]= poc;
  3678. }
  3679. if(s->picture_structure != PICT_BOTTOM_FIELD)
  3680. s->current_picture_ptr->field_poc[0]= field_poc[0];
  3681. if(s->picture_structure != PICT_TOP_FIELD)
  3682. s->current_picture_ptr->field_poc[1]= field_poc[1];
  3683. if(s->picture_structure == PICT_FRAME) // FIXME field pix?
  3684. s->current_picture_ptr->poc= FFMIN(field_poc[0], field_poc[1]);
  3685. return 0;
  3686. }
  3687. /**
  3688. * decodes a slice header.
  3689. * this will allso call MPV_common_init() and frame_start() as needed
  3690. */
  3691. static int decode_slice_header(H264Context *h){
  3692. MpegEncContext * const s = &h->s;
  3693. unsigned int first_mb_in_slice;
  3694. unsigned int pps_id;
  3695. int num_ref_idx_active_override_flag;
  3696. static const uint8_t slice_type_map[5]= {P_TYPE, B_TYPE, I_TYPE, SP_TYPE, SI_TYPE};
  3697. unsigned int slice_type, tmp;
  3698. int default_ref_list_done = 0;
  3699. s->current_picture.reference= h->nal_ref_idc != 0;
  3700. s->dropable= h->nal_ref_idc == 0;
  3701. first_mb_in_slice= get_ue_golomb(&s->gb);
  3702. if((s->flags2 & CODEC_FLAG2_CHUNKS) && first_mb_in_slice == 0){
  3703. h->slice_num = 0;
  3704. s->current_picture_ptr= NULL;
  3705. }
  3706. slice_type= get_ue_golomb(&s->gb);
  3707. if(slice_type > 9){
  3708. 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);
  3709. return -1;
  3710. }
  3711. if(slice_type > 4){
  3712. slice_type -= 5;
  3713. h->slice_type_fixed=1;
  3714. }else
  3715. h->slice_type_fixed=0;
  3716. slice_type= slice_type_map[ slice_type ];
  3717. if (slice_type == I_TYPE
  3718. || (h->slice_num != 0 && slice_type == h->slice_type) ) {
  3719. default_ref_list_done = 1;
  3720. }
  3721. h->slice_type= slice_type;
  3722. s->pict_type= h->slice_type; // to make a few old func happy, it's wrong though
  3723. pps_id= get_ue_golomb(&s->gb);
  3724. if(pps_id>=MAX_PPS_COUNT){
  3725. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  3726. return -1;
  3727. }
  3728. h->pps= h->pps_buffer[pps_id];
  3729. if(h->pps.slice_group_count == 0){
  3730. av_log(h->s.avctx, AV_LOG_ERROR, "non existing PPS referenced\n");
  3731. return -1;
  3732. }
  3733. h->sps= h->sps_buffer[ h->pps.sps_id ];
  3734. if(h->sps.log2_max_frame_num == 0){
  3735. av_log(h->s.avctx, AV_LOG_ERROR, "non existing SPS referenced\n");
  3736. return -1;
  3737. }
  3738. if(h->dequant_coeff_pps != pps_id){
  3739. h->dequant_coeff_pps = pps_id;
  3740. init_dequant_tables(h);
  3741. }
  3742. s->mb_width= h->sps.mb_width;
  3743. s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  3744. h->b_stride= s->mb_width*4;
  3745. h->b8_stride= s->mb_width*2;
  3746. s->width = 16*s->mb_width - 2*(h->sps.crop_left + h->sps.crop_right );
  3747. if(h->sps.frame_mbs_only_flag)
  3748. s->height= 16*s->mb_height - 2*(h->sps.crop_top + h->sps.crop_bottom);
  3749. else
  3750. s->height= 16*s->mb_height - 4*(h->sps.crop_top + h->sps.crop_bottom); //FIXME recheck
  3751. if (s->context_initialized
  3752. && ( s->width != s->avctx->width || s->height != s->avctx->height)) {
  3753. free_tables(h);
  3754. MPV_common_end(s);
  3755. }
  3756. if (!s->context_initialized) {
  3757. if (MPV_common_init(s) < 0)
  3758. return -1;
  3759. if(s->dsp.h264_idct_add == ff_h264_idct_add_c){ //FIXME little ugly
  3760. memcpy(h->zigzag_scan, zigzag_scan, 16*sizeof(uint8_t));
  3761. memcpy(h-> field_scan, field_scan, 16*sizeof(uint8_t));
  3762. }else{
  3763. int i;
  3764. for(i=0; i<16; i++){
  3765. #define T(x) (x>>2) | ((x<<2) & 0xF)
  3766. h->zigzag_scan[i] = T(zigzag_scan[i]);
  3767. h-> field_scan[i] = T( field_scan[i]);
  3768. #undef T
  3769. }
  3770. }
  3771. if(s->dsp.h264_idct8_add == ff_h264_idct8_add_c){
  3772. memcpy(h->zigzag_scan8x8, zigzag_scan8x8, 64*sizeof(uint8_t));
  3773. memcpy(h->zigzag_scan8x8_cavlc, zigzag_scan8x8_cavlc, 64*sizeof(uint8_t));
  3774. memcpy(h->field_scan8x8, field_scan8x8, 64*sizeof(uint8_t));
  3775. memcpy(h->field_scan8x8_cavlc, field_scan8x8_cavlc, 64*sizeof(uint8_t));
  3776. }else{
  3777. int i;
  3778. for(i=0; i<64; i++){
  3779. #define T(x) (x>>3) | ((x&7)<<3)
  3780. h->zigzag_scan8x8[i] = T(zigzag_scan8x8[i]);
  3781. h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
  3782. h->field_scan8x8[i] = T(field_scan8x8[i]);
  3783. h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
  3784. #undef T
  3785. }
  3786. }
  3787. if(h->sps.transform_bypass){ //FIXME same ugly
  3788. h->zigzag_scan_q0 = zigzag_scan;
  3789. h->zigzag_scan8x8_q0 = zigzag_scan8x8;
  3790. h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
  3791. h->field_scan_q0 = field_scan;
  3792. h->field_scan8x8_q0 = field_scan8x8;
  3793. h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
  3794. }else{
  3795. h->zigzag_scan_q0 = h->zigzag_scan;
  3796. h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
  3797. h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
  3798. h->field_scan_q0 = h->field_scan;
  3799. h->field_scan8x8_q0 = h->field_scan8x8;
  3800. h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
  3801. }
  3802. alloc_tables(h);
  3803. s->avctx->width = s->width;
  3804. s->avctx->height = s->height;
  3805. s->avctx->sample_aspect_ratio= h->sps.sar;
  3806. if(!s->avctx->sample_aspect_ratio.den)
  3807. s->avctx->sample_aspect_ratio.den = 1;
  3808. if(h->sps.timing_info_present_flag){
  3809. s->avctx->time_base= (AVRational){h->sps.num_units_in_tick * 2, h->sps.time_scale};
  3810. if(h->x264_build > 0 && h->x264_build < 44)
  3811. s->avctx->time_base.den *= 2;
  3812. av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
  3813. s->avctx->time_base.num, s->avctx->time_base.den, 1<<30);
  3814. }
  3815. }
  3816. if(h->slice_num == 0){
  3817. if(frame_start(h) < 0)
  3818. return -1;
  3819. }
  3820. s->current_picture_ptr->frame_num= //FIXME frame_num cleanup
  3821. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  3822. h->mb_mbaff = 0;
  3823. h->mb_aff_frame = 0;
  3824. if(h->sps.frame_mbs_only_flag){
  3825. s->picture_structure= PICT_FRAME;
  3826. }else{
  3827. if(get_bits1(&s->gb)) { //field_pic_flag
  3828. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  3829. av_log(h->s.avctx, AV_LOG_ERROR, "PAFF interlacing is not implemented\n");
  3830. } else {
  3831. s->picture_structure= PICT_FRAME;
  3832. h->mb_aff_frame = h->sps.mb_aff;
  3833. }
  3834. }
  3835. assert(s->mb_num == s->mb_width * s->mb_height);
  3836. if(first_mb_in_slice << h->mb_aff_frame >= s->mb_num ||
  3837. first_mb_in_slice >= s->mb_num){
  3838. av_log(h->s.avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
  3839. return -1;
  3840. }
  3841. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  3842. s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << h->mb_aff_frame;
  3843. assert(s->mb_y < s->mb_height);
  3844. if(s->picture_structure==PICT_FRAME){
  3845. h->curr_pic_num= h->frame_num;
  3846. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  3847. }else{
  3848. h->curr_pic_num= 2*h->frame_num;
  3849. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  3850. }
  3851. if(h->nal_unit_type == NAL_IDR_SLICE){
  3852. get_ue_golomb(&s->gb); /* idr_pic_id */
  3853. }
  3854. if(h->sps.poc_type==0){
  3855. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  3856. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  3857. h->delta_poc_bottom= get_se_golomb(&s->gb);
  3858. }
  3859. }
  3860. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  3861. h->delta_poc[0]= get_se_golomb(&s->gb);
  3862. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  3863. h->delta_poc[1]= get_se_golomb(&s->gb);
  3864. }
  3865. init_poc(h);
  3866. if(h->pps.redundant_pic_cnt_present){
  3867. h->redundant_pic_count= get_ue_golomb(&s->gb);
  3868. }
  3869. //set defaults, might be overriden a few line later
  3870. h->ref_count[0]= h->pps.ref_count[0];
  3871. h->ref_count[1]= h->pps.ref_count[1];
  3872. if(h->slice_type == P_TYPE || h->slice_type == SP_TYPE || h->slice_type == B_TYPE){
  3873. if(h->slice_type == B_TYPE){
  3874. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  3875. if(h->sps.mb_aff && h->direct_spatial_mv_pred)
  3876. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF + spatial direct mode is not implemented\n");
  3877. }
  3878. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  3879. if(num_ref_idx_active_override_flag){
  3880. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  3881. if(h->slice_type==B_TYPE)
  3882. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  3883. if(h->ref_count[0]-1 > 32-1 || h->ref_count[1]-1 > 32-1){
  3884. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  3885. h->ref_count[0]= h->ref_count[1]= 1;
  3886. return -1;
  3887. }
  3888. }
  3889. if(h->slice_type == B_TYPE)
  3890. h->list_count= 2;
  3891. else
  3892. h->list_count= 1;
  3893. }else
  3894. h->list_count= 0;
  3895. if(!default_ref_list_done){
  3896. fill_default_ref_list(h);
  3897. }
  3898. if(decode_ref_pic_list_reordering(h) < 0)
  3899. return -1;
  3900. if( (h->pps.weighted_pred && (h->slice_type == P_TYPE || h->slice_type == SP_TYPE ))
  3901. || (h->pps.weighted_bipred_idc==1 && h->slice_type==B_TYPE ) )
  3902. pred_weight_table(h);
  3903. else if(h->pps.weighted_bipred_idc==2 && h->slice_type==B_TYPE)
  3904. implicit_weight_table(h);
  3905. else
  3906. h->use_weight = 0;
  3907. if(s->current_picture.reference)
  3908. decode_ref_pic_marking(h);
  3909. if(FRAME_MBAFF)
  3910. fill_mbaff_ref_list(h);
  3911. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE && h->pps.cabac ){
  3912. tmp = get_ue_golomb(&s->gb);
  3913. if(tmp > 2){
  3914. av_log(s->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
  3915. return -1;
  3916. }
  3917. h->cabac_init_idc= tmp;
  3918. }
  3919. h->last_qscale_diff = 0;
  3920. tmp = h->pps.init_qp + get_se_golomb(&s->gb);
  3921. if(tmp>51){
  3922. av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
  3923. return -1;
  3924. }
  3925. s->qscale= tmp;
  3926. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  3927. //FIXME qscale / qp ... stuff
  3928. if(h->slice_type == SP_TYPE){
  3929. get_bits1(&s->gb); /* sp_for_switch_flag */
  3930. }
  3931. if(h->slice_type==SP_TYPE || h->slice_type == SI_TYPE){
  3932. get_se_golomb(&s->gb); /* slice_qs_delta */
  3933. }
  3934. h->deblocking_filter = 1;
  3935. h->slice_alpha_c0_offset = 0;
  3936. h->slice_beta_offset = 0;
  3937. if( h->pps.deblocking_filter_parameters_present ) {
  3938. tmp= get_ue_golomb(&s->gb);
  3939. if(tmp > 2){
  3940. av_log(s->avctx, AV_LOG_ERROR, "deblocking_filter_idc %u out of range\n", tmp);
  3941. return -1;
  3942. }
  3943. h->deblocking_filter= tmp;
  3944. if(h->deblocking_filter < 2)
  3945. h->deblocking_filter^= 1; // 1<->0
  3946. if( h->deblocking_filter ) {
  3947. h->slice_alpha_c0_offset = get_se_golomb(&s->gb) << 1;
  3948. h->slice_beta_offset = get_se_golomb(&s->gb) << 1;
  3949. }
  3950. }
  3951. if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
  3952. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type != I_TYPE)
  3953. ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type == B_TYPE)
  3954. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  3955. h->deblocking_filter= 0;
  3956. #if 0 //FMO
  3957. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  3958. slice_group_change_cycle= get_bits(&s->gb, ?);
  3959. #endif
  3960. h->slice_num++;
  3961. h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
  3962. h->emu_edge_height= FRAME_MBAFF ? 0 : h->emu_edge_width;
  3963. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  3964. 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",
  3965. h->slice_num,
  3966. (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
  3967. first_mb_in_slice,
  3968. av_get_pict_type_char(h->slice_type),
  3969. pps_id, h->frame_num,
  3970. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  3971. h->ref_count[0], h->ref_count[1],
  3972. s->qscale,
  3973. h->deblocking_filter, h->slice_alpha_c0_offset/2, h->slice_beta_offset/2,
  3974. h->use_weight,
  3975. h->use_weight==1 && h->use_weight_chroma ? "c" : ""
  3976. );
  3977. }
  3978. if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !s->current_picture.reference){
  3979. s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
  3980. s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
  3981. }else{
  3982. s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
  3983. s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
  3984. }
  3985. return 0;
  3986. }
  3987. /**
  3988. *
  3989. */
  3990. static inline int get_level_prefix(GetBitContext *gb){
  3991. unsigned int buf;
  3992. int log;
  3993. OPEN_READER(re, gb);
  3994. UPDATE_CACHE(re, gb);
  3995. buf=GET_CACHE(re, gb);
  3996. log= 32 - av_log2(buf);
  3997. #ifdef TRACE
  3998. print_bin(buf>>(32-log), log);
  3999. 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__);
  4000. #endif
  4001. LAST_SKIP_BITS(re, gb, log);
  4002. CLOSE_READER(re, gb);
  4003. return log-1;
  4004. }
  4005. static inline int get_dct8x8_allowed(H264Context *h){
  4006. int i;
  4007. for(i=0; i<4; i++){
  4008. if(!IS_SUB_8X8(h->sub_mb_type[i])
  4009. || (!h->sps.direct_8x8_inference_flag && IS_DIRECT(h->sub_mb_type[i])))
  4010. return 0;
  4011. }
  4012. return 1;
  4013. }
  4014. /**
  4015. * decodes a residual block.
  4016. * @param n block index
  4017. * @param scantable scantable
  4018. * @param max_coeff number of coefficients in the block
  4019. * @return <0 if an error occured
  4020. */
  4021. static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff){
  4022. MpegEncContext * const s = &h->s;
  4023. 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};
  4024. int level[16];
  4025. int zeros_left, coeff_num, coeff_token, total_coeff, i, j, trailing_ones, run_before;
  4026. //FIXME put trailing_onex into the context
  4027. if(n == CHROMA_DC_BLOCK_INDEX){
  4028. coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);
  4029. total_coeff= coeff_token>>2;
  4030. }else{
  4031. if(n == LUMA_DC_BLOCK_INDEX){
  4032. total_coeff= pred_non_zero_count(h, 0);
  4033. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  4034. total_coeff= coeff_token>>2;
  4035. }else{
  4036. total_coeff= pred_non_zero_count(h, n);
  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. h->non_zero_count_cache[ scan8[n] ]= total_coeff;
  4040. }
  4041. }
  4042. //FIXME set last_non_zero?
  4043. if(total_coeff==0)
  4044. return 0;
  4045. if(total_coeff > (unsigned)max_coeff) {
  4046. av_log(h->s.avctx, AV_LOG_ERROR, "corrupted macroblock %d %d (total_coeff=%d)\n", s->mb_x, s->mb_y, total_coeff);
  4047. return -1;
  4048. }
  4049. trailing_ones= coeff_token&3;
  4050. tprintf(h->s.avctx, "trailing:%d, total:%d\n", trailing_ones, total_coeff);
  4051. assert(total_coeff<=16);
  4052. for(i=0; i<trailing_ones; i++){
  4053. level[i]= 1 - 2*get_bits1(gb);
  4054. }
  4055. if(i<total_coeff) {
  4056. int level_code, mask;
  4057. int suffix_length = total_coeff > 10 && trailing_ones < 3;
  4058. int prefix= get_level_prefix(gb);
  4059. //first coefficient has suffix_length equal to 0 or 1
  4060. if(prefix<14){ //FIXME try to build a large unified VLC table for all this
  4061. if(suffix_length)
  4062. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  4063. else
  4064. level_code= (prefix<<suffix_length); //part
  4065. }else if(prefix==14){
  4066. if(suffix_length)
  4067. level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part
  4068. else
  4069. level_code= prefix + get_bits(gb, 4); //part
  4070. }else if(prefix==15){
  4071. level_code= (prefix<<suffix_length) + get_bits(gb, 12); //part
  4072. if(suffix_length==0) level_code+=15; //FIXME doesn't make (much)sense
  4073. }else{
  4074. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  4075. return -1;
  4076. }
  4077. if(trailing_ones < 3) level_code += 2;
  4078. suffix_length = 1;
  4079. if(level_code > 5)
  4080. suffix_length++;
  4081. mask= -(level_code&1);
  4082. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  4083. i++;
  4084. //remaining coefficients have suffix_length > 0
  4085. for(;i<total_coeff;i++) {
  4086. static const int suffix_limit[7] = {0,5,11,23,47,95,INT_MAX };
  4087. prefix = get_level_prefix(gb);
  4088. if(prefix<15){
  4089. level_code = (prefix<<suffix_length) + get_bits(gb, suffix_length);
  4090. }else if(prefix==15){
  4091. level_code = (prefix<<suffix_length) + get_bits(gb, 12);
  4092. }else{
  4093. av_log(h->s.avctx, AV_LOG_ERROR, "prefix too large at %d %d\n", s->mb_x, s->mb_y);
  4094. return -1;
  4095. }
  4096. mask= -(level_code&1);
  4097. level[i]= (((2+level_code)>>1) ^ mask) - mask;
  4098. if(level_code > suffix_limit[suffix_length])
  4099. suffix_length++;
  4100. }
  4101. }
  4102. if(total_coeff == max_coeff)
  4103. zeros_left=0;
  4104. else{
  4105. if(n == CHROMA_DC_BLOCK_INDEX)
  4106. zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
  4107. else
  4108. zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);
  4109. }
  4110. coeff_num = zeros_left + total_coeff - 1;
  4111. j = scantable[coeff_num];
  4112. if(n > 24){
  4113. block[j] = level[0];
  4114. for(i=1;i<total_coeff;i++) {
  4115. if(zeros_left <= 0)
  4116. run_before = 0;
  4117. else if(zeros_left < 7){
  4118. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  4119. }else{
  4120. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  4121. }
  4122. zeros_left -= run_before;
  4123. coeff_num -= 1 + run_before;
  4124. j= scantable[ coeff_num ];
  4125. block[j]= level[i];
  4126. }
  4127. }else{
  4128. block[j] = (level[0] * qmul[j] + 32)>>6;
  4129. for(i=1;i<total_coeff;i++) {
  4130. if(zeros_left <= 0)
  4131. run_before = 0;
  4132. else if(zeros_left < 7){
  4133. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  4134. }else{
  4135. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  4136. }
  4137. zeros_left -= run_before;
  4138. coeff_num -= 1 + run_before;
  4139. j= scantable[ coeff_num ];
  4140. block[j]= (level[i] * qmul[j] + 32)>>6;
  4141. }
  4142. }
  4143. if(zeros_left<0){
  4144. av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);
  4145. return -1;
  4146. }
  4147. return 0;
  4148. }
  4149. static void predict_field_decoding_flag(H264Context *h){
  4150. MpegEncContext * const s = &h->s;
  4151. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4152. int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
  4153. ? s->current_picture.mb_type[mb_xy-1]
  4154. : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
  4155. ? s->current_picture.mb_type[mb_xy-s->mb_stride]
  4156. : 0;
  4157. h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  4158. }
  4159. /**
  4160. * decodes a P_SKIP or B_SKIP macroblock
  4161. */
  4162. static void decode_mb_skip(H264Context *h){
  4163. MpegEncContext * const s = &h->s;
  4164. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4165. int mb_type=0;
  4166. memset(h->non_zero_count[mb_xy], 0, 16);
  4167. memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
  4168. if(MB_FIELD)
  4169. mb_type|= MB_TYPE_INTERLACED;
  4170. if( h->slice_type == B_TYPE )
  4171. {
  4172. // just for fill_caches. pred_direct_motion will set the real mb_type
  4173. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_SKIP;
  4174. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  4175. pred_direct_motion(h, &mb_type);
  4176. mb_type|= MB_TYPE_SKIP;
  4177. }
  4178. else
  4179. {
  4180. int mx, my;
  4181. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
  4182. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  4183. pred_pskip_motion(h, &mx, &my);
  4184. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  4185. fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
  4186. }
  4187. write_back_motion(h, mb_type);
  4188. s->current_picture.mb_type[mb_xy]= mb_type;
  4189. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4190. h->slice_table[ mb_xy ]= h->slice_num;
  4191. h->prev_mb_skipped= 1;
  4192. }
  4193. /**
  4194. * decodes a macroblock
  4195. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  4196. */
  4197. static int decode_mb_cavlc(H264Context *h){
  4198. MpegEncContext * const s = &h->s;
  4199. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4200. int partition_count;
  4201. unsigned int mb_type, cbp;
  4202. int dct8x8_allowed= h->pps.transform_8x8_mode;
  4203. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?
  4204. tprintf(s->avctx, "pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  4205. cbp = 0; /* avoid warning. FIXME: find a solution without slowing
  4206. down the code */
  4207. if(h->slice_type != I_TYPE && h->slice_type != SI_TYPE){
  4208. if(s->mb_skip_run==-1)
  4209. s->mb_skip_run= get_ue_golomb(&s->gb);
  4210. if (s->mb_skip_run--) {
  4211. if(FRAME_MBAFF && (s->mb_y&1) == 0){
  4212. if(s->mb_skip_run==0)
  4213. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  4214. else
  4215. predict_field_decoding_flag(h);
  4216. }
  4217. decode_mb_skip(h);
  4218. return 0;
  4219. }
  4220. }
  4221. if(FRAME_MBAFF){
  4222. if( (s->mb_y&1) == 0 )
  4223. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  4224. }else
  4225. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  4226. h->prev_mb_skipped= 0;
  4227. mb_type= get_ue_golomb(&s->gb);
  4228. if(h->slice_type == B_TYPE){
  4229. if(mb_type < 23){
  4230. partition_count= b_mb_type_info[mb_type].partition_count;
  4231. mb_type= b_mb_type_info[mb_type].type;
  4232. }else{
  4233. mb_type -= 23;
  4234. goto decode_intra_mb;
  4235. }
  4236. }else if(h->slice_type == P_TYPE /*|| h->slice_type == SP_TYPE */){
  4237. if(mb_type < 5){
  4238. partition_count= p_mb_type_info[mb_type].partition_count;
  4239. mb_type= p_mb_type_info[mb_type].type;
  4240. }else{
  4241. mb_type -= 5;
  4242. goto decode_intra_mb;
  4243. }
  4244. }else{
  4245. assert(h->slice_type == I_TYPE);
  4246. decode_intra_mb:
  4247. if(mb_type > 25){
  4248. 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);
  4249. return -1;
  4250. }
  4251. partition_count=0;
  4252. cbp= i_mb_type_info[mb_type].cbp;
  4253. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  4254. mb_type= i_mb_type_info[mb_type].type;
  4255. }
  4256. if(MB_FIELD)
  4257. mb_type |= MB_TYPE_INTERLACED;
  4258. h->slice_table[ mb_xy ]= h->slice_num;
  4259. if(IS_INTRA_PCM(mb_type)){
  4260. unsigned int x, y;
  4261. // we assume these blocks are very rare so we dont optimize it
  4262. align_get_bits(&s->gb);
  4263. // The pixels are stored in the same order as levels in h->mb array.
  4264. for(y=0; y<16; y++){
  4265. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  4266. for(x=0; x<16; x++){
  4267. tprintf(s->avctx, "LUMA ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4268. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= get_bits(&s->gb, 8);
  4269. }
  4270. }
  4271. for(y=0; y<8; y++){
  4272. const int index= 256 + 4*(y&3) + 32*(y>>2);
  4273. for(x=0; x<8; x++){
  4274. tprintf(s->avctx, "CHROMA U ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4275. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  4276. }
  4277. }
  4278. for(y=0; y<8; y++){
  4279. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  4280. for(x=0; x<8; x++){
  4281. tprintf(s->avctx, "CHROMA V ICPM LEVEL (%3d)\n", show_bits(&s->gb, 8));
  4282. h->mb[index + (x&3) + 16*(x>>2)]= get_bits(&s->gb, 8);
  4283. }
  4284. }
  4285. // In deblocking, the quantizer is 0
  4286. s->current_picture.qscale_table[mb_xy]= 0;
  4287. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
  4288. // All coeffs are present
  4289. memset(h->non_zero_count[mb_xy], 16, 16);
  4290. s->current_picture.mb_type[mb_xy]= mb_type;
  4291. return 0;
  4292. }
  4293. if(MB_MBAFF){
  4294. h->ref_count[0] <<= 1;
  4295. h->ref_count[1] <<= 1;
  4296. }
  4297. fill_caches(h, mb_type, 0);
  4298. //mb_pred
  4299. if(IS_INTRA(mb_type)){
  4300. int pred_mode;
  4301. // init_top_left_availability(h);
  4302. if(IS_INTRA4x4(mb_type)){
  4303. int i;
  4304. int di = 1;
  4305. if(dct8x8_allowed && get_bits1(&s->gb)){
  4306. mb_type |= MB_TYPE_8x8DCT;
  4307. di = 4;
  4308. }
  4309. // fill_intra4x4_pred_table(h);
  4310. for(i=0; i<16; i+=di){
  4311. int mode= pred_intra_mode(h, i);
  4312. if(!get_bits1(&s->gb)){
  4313. const int rem_mode= get_bits(&s->gb, 3);
  4314. mode = rem_mode + (rem_mode >= mode);
  4315. }
  4316. if(di==4)
  4317. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  4318. else
  4319. h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
  4320. }
  4321. write_back_intra_pred_mode(h);
  4322. if( check_intra4x4_pred_mode(h) < 0)
  4323. return -1;
  4324. }else{
  4325. h->intra16x16_pred_mode= check_intra_pred_mode(h, h->intra16x16_pred_mode);
  4326. if(h->intra16x16_pred_mode < 0)
  4327. return -1;
  4328. }
  4329. pred_mode= check_intra_pred_mode(h, get_ue_golomb(&s->gb));
  4330. if(pred_mode < 0)
  4331. return -1;
  4332. h->chroma_pred_mode= pred_mode;
  4333. }else if(partition_count==4){
  4334. int i, j, sub_partition_count[4], list, ref[2][4];
  4335. if(h->slice_type == B_TYPE){
  4336. for(i=0; i<4; i++){
  4337. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  4338. if(h->sub_mb_type[i] >=13){
  4339. 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);
  4340. return -1;
  4341. }
  4342. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4343. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4344. }
  4345. if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
  4346. || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3])) {
  4347. pred_direct_motion(h, &mb_type);
  4348. h->ref_cache[0][scan8[4]] =
  4349. h->ref_cache[1][scan8[4]] =
  4350. h->ref_cache[0][scan8[12]] =
  4351. h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
  4352. }
  4353. }else{
  4354. assert(h->slice_type == P_TYPE || h->slice_type == SP_TYPE); //FIXME SP correct ?
  4355. for(i=0; i<4; i++){
  4356. h->sub_mb_type[i]= get_ue_golomb(&s->gb);
  4357. if(h->sub_mb_type[i] >=4){
  4358. 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);
  4359. return -1;
  4360. }
  4361. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  4362. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  4363. }
  4364. }
  4365. for(list=0; list<h->list_count; list++){
  4366. int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  4367. for(i=0; i<4; i++){
  4368. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  4369. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4370. unsigned int tmp = get_te0_golomb(&s->gb, ref_count); //FIXME init to 0 before and skip?
  4371. if(tmp>=ref_count){
  4372. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", tmp);
  4373. return -1;
  4374. }
  4375. ref[list][i]= tmp;
  4376. }else{
  4377. //FIXME
  4378. ref[list][i] = -1;
  4379. }
  4380. }
  4381. }
  4382. if(dct8x8_allowed)
  4383. dct8x8_allowed = get_dct8x8_allowed(h);
  4384. for(list=0; list<h->list_count; list++){
  4385. for(i=0; i<4; i++){
  4386. if(IS_DIRECT(h->sub_mb_type[i])) {
  4387. h->ref_cache[list][ scan8[4*i] ] = h->ref_cache[list][ scan8[4*i]+1 ];
  4388. continue;
  4389. }
  4390. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
  4391. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  4392. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  4393. const int sub_mb_type= h->sub_mb_type[i];
  4394. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  4395. for(j=0; j<sub_partition_count[i]; j++){
  4396. int mx, my;
  4397. const int index= 4*i + block_width*j;
  4398. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  4399. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
  4400. mx += get_se_golomb(&s->gb);
  4401. my += get_se_golomb(&s->gb);
  4402. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4403. if(IS_SUB_8X8(sub_mb_type)){
  4404. mv_cache[ 1 ][0]=
  4405. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  4406. mv_cache[ 1 ][1]=
  4407. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  4408. }else if(IS_SUB_8X4(sub_mb_type)){
  4409. mv_cache[ 1 ][0]= mx;
  4410. mv_cache[ 1 ][1]= my;
  4411. }else if(IS_SUB_4X8(sub_mb_type)){
  4412. mv_cache[ 8 ][0]= mx;
  4413. mv_cache[ 8 ][1]= my;
  4414. }
  4415. mv_cache[ 0 ][0]= mx;
  4416. mv_cache[ 0 ][1]= my;
  4417. }
  4418. }else{
  4419. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  4420. p[0] = p[1]=
  4421. p[8] = p[9]= 0;
  4422. }
  4423. }
  4424. }
  4425. }else if(IS_DIRECT(mb_type)){
  4426. pred_direct_motion(h, &mb_type);
  4427. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  4428. }else{
  4429. int list, mx, my, i;
  4430. //FIXME we should set ref_idx_l? to 0 if we use that later ...
  4431. if(IS_16X16(mb_type)){
  4432. for(list=0; list<h->list_count; list++){
  4433. unsigned int val;
  4434. if(IS_DIR(mb_type, 0, list)){
  4435. val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4436. if(val >= h->ref_count[list]){
  4437. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  4438. return -1;
  4439. }
  4440. }else
  4441. val= LIST_NOT_USED&0xFF;
  4442. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
  4443. }
  4444. for(list=0; list<h->list_count; list++){
  4445. unsigned int val;
  4446. if(IS_DIR(mb_type, 0, list)){
  4447. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
  4448. mx += get_se_golomb(&s->gb);
  4449. my += get_se_golomb(&s->gb);
  4450. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4451. val= pack16to32(mx,my);
  4452. }else
  4453. val=0;
  4454. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, val, 4);
  4455. }
  4456. }
  4457. else if(IS_16X8(mb_type)){
  4458. for(list=0; list<h->list_count; list++){
  4459. for(i=0; i<2; i++){
  4460. unsigned int val;
  4461. if(IS_DIR(mb_type, i, list)){
  4462. val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4463. if(val >= h->ref_count[list]){
  4464. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  4465. return -1;
  4466. }
  4467. }else
  4468. val= LIST_NOT_USED&0xFF;
  4469. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);
  4470. }
  4471. }
  4472. for(list=0; list<h->list_count; list++){
  4473. for(i=0; i<2; i++){
  4474. unsigned int val;
  4475. if(IS_DIR(mb_type, i, list)){
  4476. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
  4477. mx += get_se_golomb(&s->gb);
  4478. my += get_se_golomb(&s->gb);
  4479. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4480. val= pack16to32(mx,my);
  4481. }else
  4482. val=0;
  4483. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 4);
  4484. }
  4485. }
  4486. }else{
  4487. assert(IS_8X16(mb_type));
  4488. for(list=0; list<h->list_count; list++){
  4489. for(i=0; i<2; i++){
  4490. unsigned int val;
  4491. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  4492. val= get_te0_golomb(&s->gb, h->ref_count[list]);
  4493. if(val >= h->ref_count[list]){
  4494. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  4495. return -1;
  4496. }
  4497. }else
  4498. val= LIST_NOT_USED&0xFF;
  4499. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
  4500. }
  4501. }
  4502. for(list=0; list<h->list_count; list++){
  4503. for(i=0; i<2; i++){
  4504. unsigned int val;
  4505. if(IS_DIR(mb_type, i, list)){
  4506. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
  4507. mx += get_se_golomb(&s->gb);
  4508. my += get_se_golomb(&s->gb);
  4509. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4510. val= pack16to32(mx,my);
  4511. }else
  4512. val=0;
  4513. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 4);
  4514. }
  4515. }
  4516. }
  4517. }
  4518. if(IS_INTER(mb_type))
  4519. write_back_motion(h, mb_type);
  4520. if(!IS_INTRA16x16(mb_type)){
  4521. cbp= get_ue_golomb(&s->gb);
  4522. if(cbp > 47){
  4523. av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%u) at %d %d\n", cbp, s->mb_x, s->mb_y);
  4524. return -1;
  4525. }
  4526. if(IS_INTRA4x4(mb_type))
  4527. cbp= golomb_to_intra4x4_cbp[cbp];
  4528. else
  4529. cbp= golomb_to_inter_cbp[cbp];
  4530. }
  4531. h->cbp = cbp;
  4532. if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){
  4533. if(get_bits1(&s->gb))
  4534. mb_type |= MB_TYPE_8x8DCT;
  4535. }
  4536. s->current_picture.mb_type[mb_xy]= mb_type;
  4537. if(cbp || IS_INTRA16x16(mb_type)){
  4538. int i8x8, i4x4, chroma_idx;
  4539. int chroma_qp, dquant;
  4540. GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
  4541. const uint8_t *scan, *scan8x8, *dc_scan;
  4542. // fill_non_zero_count_cache(h);
  4543. if(IS_INTERLACED(mb_type)){
  4544. scan8x8= s->qscale ? h->field_scan8x8_cavlc : h->field_scan8x8_cavlc_q0;
  4545. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  4546. dc_scan= luma_dc_field_scan;
  4547. }else{
  4548. scan8x8= s->qscale ? h->zigzag_scan8x8_cavlc : h->zigzag_scan8x8_cavlc_q0;
  4549. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  4550. dc_scan= luma_dc_zigzag_scan;
  4551. }
  4552. dquant= get_se_golomb(&s->gb);
  4553. if( dquant > 25 || dquant < -26 ){
  4554. av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
  4555. return -1;
  4556. }
  4557. s->qscale += dquant;
  4558. if(((unsigned)s->qscale) > 51){
  4559. if(s->qscale<0) s->qscale+= 52;
  4560. else s->qscale-= 52;
  4561. }
  4562. h->chroma_qp= chroma_qp= get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  4563. if(IS_INTRA16x16(mb_type)){
  4564. if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, h->dequant4_coeff[0][s->qscale], 16) < 0){
  4565. return -1; //FIXME continue if partitioned and other return -1 too
  4566. }
  4567. assert((cbp&15) == 0 || (cbp&15) == 15);
  4568. if(cbp&15){
  4569. for(i8x8=0; i8x8<4; i8x8++){
  4570. for(i4x4=0; i4x4<4; i4x4++){
  4571. const int index= i4x4 + 4*i8x8;
  4572. if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 ){
  4573. return -1;
  4574. }
  4575. }
  4576. }
  4577. }else{
  4578. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  4579. }
  4580. }else{
  4581. for(i8x8=0; i8x8<4; i8x8++){
  4582. if(cbp & (1<<i8x8)){
  4583. if(IS_8x8DCT(mb_type)){
  4584. DCTELEM *buf = &h->mb[64*i8x8];
  4585. uint8_t *nnz;
  4586. for(i4x4=0; i4x4<4; i4x4++){
  4587. if( decode_residual(h, gb, buf, i4x4+4*i8x8, scan8x8+16*i4x4,
  4588. h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 16) <0 )
  4589. return -1;
  4590. }
  4591. nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4592. nnz[0] += nnz[1] + nnz[8] + nnz[9];
  4593. }else{
  4594. for(i4x4=0; i4x4<4; i4x4++){
  4595. const int index= i4x4 + 4*i8x8;
  4596. if( decode_residual(h, gb, h->mb + 16*index, index, scan, h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale], 16) <0 ){
  4597. return -1;
  4598. }
  4599. }
  4600. }
  4601. }else{
  4602. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4603. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  4604. }
  4605. }
  4606. }
  4607. if(cbp&0x30){
  4608. for(chroma_idx=0; chroma_idx<2; chroma_idx++)
  4609. if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, NULL, 4) < 0){
  4610. return -1;
  4611. }
  4612. }
  4613. if(cbp&0x20){
  4614. for(chroma_idx=0; chroma_idx<2; chroma_idx++){
  4615. for(i4x4=0; i4x4<4; i4x4++){
  4616. const int index= 16 + 4*chroma_idx + i4x4;
  4617. 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){
  4618. return -1;
  4619. }
  4620. }
  4621. }
  4622. }else{
  4623. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4624. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4625. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4626. }
  4627. }else{
  4628. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4629. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  4630. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4631. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4632. }
  4633. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4634. write_back_non_zero_count(h);
  4635. if(MB_MBAFF){
  4636. h->ref_count[0] >>= 1;
  4637. h->ref_count[1] >>= 1;
  4638. }
  4639. return 0;
  4640. }
  4641. static int decode_cabac_field_decoding_flag(H264Context *h) {
  4642. MpegEncContext * const s = &h->s;
  4643. const int mb_x = s->mb_x;
  4644. const int mb_y = s->mb_y & ~1;
  4645. const int mba_xy = mb_x - 1 + mb_y *s->mb_stride;
  4646. const int mbb_xy = mb_x + (mb_y-2)*s->mb_stride;
  4647. unsigned int ctx = 0;
  4648. if( h->slice_table[mba_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) ) {
  4649. ctx += 1;
  4650. }
  4651. if( h->slice_table[mbb_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) ) {
  4652. ctx += 1;
  4653. }
  4654. return get_cabac_noinline( &h->cabac, &h->cabac_state[70 + ctx] );
  4655. }
  4656. static int decode_cabac_intra_mb_type(H264Context *h, int ctx_base, int intra_slice) {
  4657. uint8_t *state= &h->cabac_state[ctx_base];
  4658. int mb_type;
  4659. if(intra_slice){
  4660. MpegEncContext * const s = &h->s;
  4661. const int mba_xy = h->left_mb_xy[0];
  4662. const int mbb_xy = h->top_mb_xy;
  4663. int ctx=0;
  4664. if( h->slice_table[mba_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mba_xy] ) )
  4665. ctx++;
  4666. if( h->slice_table[mbb_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mbb_xy] ) )
  4667. ctx++;
  4668. if( get_cabac_noinline( &h->cabac, &state[ctx] ) == 0 )
  4669. return 0; /* I4x4 */
  4670. state += 2;
  4671. }else{
  4672. if( get_cabac_noinline( &h->cabac, &state[0] ) == 0 )
  4673. return 0; /* I4x4 */
  4674. }
  4675. if( get_cabac_terminate( &h->cabac ) )
  4676. return 25; /* PCM */
  4677. mb_type = 1; /* I16x16 */
  4678. mb_type += 12 * get_cabac_noinline( &h->cabac, &state[1] ); /* cbp_luma != 0 */
  4679. if( get_cabac_noinline( &h->cabac, &state[2] ) ) /* cbp_chroma */
  4680. mb_type += 4 + 4 * get_cabac_noinline( &h->cabac, &state[2+intra_slice] );
  4681. mb_type += 2 * get_cabac_noinline( &h->cabac, &state[3+intra_slice] );
  4682. mb_type += 1 * get_cabac_noinline( &h->cabac, &state[3+2*intra_slice] );
  4683. return mb_type;
  4684. }
  4685. static int decode_cabac_mb_type( H264Context *h ) {
  4686. MpegEncContext * const s = &h->s;
  4687. if( h->slice_type == I_TYPE ) {
  4688. return decode_cabac_intra_mb_type(h, 3, 1);
  4689. } else if( h->slice_type == P_TYPE ) {
  4690. if( get_cabac_noinline( &h->cabac, &h->cabac_state[14] ) == 0 ) {
  4691. /* P-type */
  4692. if( get_cabac_noinline( &h->cabac, &h->cabac_state[15] ) == 0 ) {
  4693. /* P_L0_D16x16, P_8x8 */
  4694. return 3 * get_cabac_noinline( &h->cabac, &h->cabac_state[16] );
  4695. } else {
  4696. /* P_L0_D8x16, P_L0_D16x8 */
  4697. return 2 - get_cabac_noinline( &h->cabac, &h->cabac_state[17] );
  4698. }
  4699. } else {
  4700. return decode_cabac_intra_mb_type(h, 17, 0) + 5;
  4701. }
  4702. } else if( h->slice_type == B_TYPE ) {
  4703. const int mba_xy = h->left_mb_xy[0];
  4704. const int mbb_xy = h->top_mb_xy;
  4705. int ctx = 0;
  4706. int bits;
  4707. if( h->slice_table[mba_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mba_xy] ) )
  4708. ctx++;
  4709. if( h->slice_table[mbb_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mbb_xy] ) )
  4710. ctx++;
  4711. if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+ctx] ) )
  4712. return 0; /* B_Direct_16x16 */
  4713. if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+3] ) ) {
  4714. return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ); /* B_L[01]_16x16 */
  4715. }
  4716. bits = get_cabac_noinline( &h->cabac, &h->cabac_state[27+4] ) << 3;
  4717. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 2;
  4718. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 1;
  4719. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
  4720. if( bits < 8 )
  4721. return bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */
  4722. else if( bits == 13 ) {
  4723. return decode_cabac_intra_mb_type(h, 32, 0) + 23;
  4724. } else if( bits == 14 )
  4725. return 11; /* B_L1_L0_8x16 */
  4726. else if( bits == 15 )
  4727. return 22; /* B_8x8 */
  4728. bits= ( bits<<1 ) | get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
  4729. return bits - 4; /* B_L0_Bi_* through B_Bi_Bi_* */
  4730. } else {
  4731. /* TODO SI/SP frames? */
  4732. return -1;
  4733. }
  4734. }
  4735. static int decode_cabac_mb_skip( H264Context *h, int mb_x, int mb_y ) {
  4736. MpegEncContext * const s = &h->s;
  4737. int mba_xy, mbb_xy;
  4738. int ctx = 0;
  4739. if(FRAME_MBAFF){ //FIXME merge with the stuff in fill_caches?
  4740. int mb_xy = mb_x + (mb_y&~1)*s->mb_stride;
  4741. mba_xy = mb_xy - 1;
  4742. if( (mb_y&1)
  4743. && h->slice_table[mba_xy] == h->slice_num
  4744. && MB_FIELD == !!IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) )
  4745. mba_xy += s->mb_stride;
  4746. if( MB_FIELD ){
  4747. mbb_xy = mb_xy - s->mb_stride;
  4748. if( !(mb_y&1)
  4749. && h->slice_table[mbb_xy] == h->slice_num
  4750. && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) )
  4751. mbb_xy -= s->mb_stride;
  4752. }else
  4753. mbb_xy = mb_x + (mb_y-1)*s->mb_stride;
  4754. }else{
  4755. int mb_xy = mb_x + mb_y*s->mb_stride;
  4756. mba_xy = mb_xy - 1;
  4757. mbb_xy = mb_xy - s->mb_stride;
  4758. }
  4759. if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] ))
  4760. ctx++;
  4761. if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] ))
  4762. ctx++;
  4763. if( h->slice_type == B_TYPE )
  4764. ctx += 13;
  4765. return get_cabac_noinline( &h->cabac, &h->cabac_state[11+ctx] );
  4766. }
  4767. static int decode_cabac_mb_intra4x4_pred_mode( H264Context *h, int pred_mode ) {
  4768. int mode = 0;
  4769. if( get_cabac( &h->cabac, &h->cabac_state[68] ) )
  4770. return pred_mode;
  4771. mode += 1 * get_cabac( &h->cabac, &h->cabac_state[69] );
  4772. mode += 2 * get_cabac( &h->cabac, &h->cabac_state[69] );
  4773. mode += 4 * get_cabac( &h->cabac, &h->cabac_state[69] );
  4774. if( mode >= pred_mode )
  4775. return mode + 1;
  4776. else
  4777. return mode;
  4778. }
  4779. static int decode_cabac_mb_chroma_pre_mode( H264Context *h) {
  4780. const int mba_xy = h->left_mb_xy[0];
  4781. const int mbb_xy = h->top_mb_xy;
  4782. int ctx = 0;
  4783. /* No need to test for IS_INTRA4x4 and IS_INTRA16x16, as we set chroma_pred_mode_table to 0 */
  4784. if( h->slice_table[mba_xy] == h->slice_num && h->chroma_pred_mode_table[mba_xy] != 0 )
  4785. ctx++;
  4786. if( h->slice_table[mbb_xy] == h->slice_num && h->chroma_pred_mode_table[mbb_xy] != 0 )
  4787. ctx++;
  4788. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+ctx] ) == 0 )
  4789. return 0;
  4790. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  4791. return 1;
  4792. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  4793. return 2;
  4794. else
  4795. return 3;
  4796. }
  4797. static const uint8_t block_idx_x[16] = {
  4798. 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
  4799. };
  4800. static const uint8_t block_idx_y[16] = {
  4801. 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
  4802. };
  4803. static const uint8_t block_idx_xy[4][4] = {
  4804. { 0, 2, 8, 10},
  4805. { 1, 3, 9, 11},
  4806. { 4, 6, 12, 14},
  4807. { 5, 7, 13, 15}
  4808. };
  4809. static int decode_cabac_mb_cbp_luma( H264Context *h) {
  4810. int cbp = 0;
  4811. int cbp_b = -1;
  4812. int i8x8;
  4813. if( h->slice_table[h->top_mb_xy] == h->slice_num ) {
  4814. cbp_b = h->top_cbp;
  4815. tprintf(h->s.avctx, "cbp_b = top_cbp = %x\n", cbp_b);
  4816. }
  4817. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  4818. int cbp_a = -1;
  4819. int x, y;
  4820. int ctx = 0;
  4821. x = block_idx_x[4*i8x8];
  4822. y = block_idx_y[4*i8x8];
  4823. if( x > 0 )
  4824. cbp_a = cbp;
  4825. else if( h->slice_table[h->left_mb_xy[0]] == h->slice_num ) {
  4826. cbp_a = h->left_cbp;
  4827. tprintf(h->s.avctx, "cbp_a = left_cbp = %x\n", cbp_a);
  4828. }
  4829. if( y > 0 )
  4830. cbp_b = cbp;
  4831. /* No need to test for skip as we put 0 for skip block */
  4832. /* No need to test for IPCM as we put 1 for IPCM block */
  4833. if( cbp_a >= 0 ) {
  4834. int i8x8a = block_idx_xy[(x-1)&0x03][y]/4;
  4835. if( ((cbp_a >> i8x8a)&0x01) == 0 )
  4836. ctx++;
  4837. }
  4838. if( cbp_b >= 0 ) {
  4839. int i8x8b = block_idx_xy[x][(y-1)&0x03]/4;
  4840. if( ((cbp_b >> i8x8b)&0x01) == 0 )
  4841. ctx += 2;
  4842. }
  4843. if( get_cabac( &h->cabac, &h->cabac_state[73 + ctx] ) ) {
  4844. cbp |= 1 << i8x8;
  4845. }
  4846. }
  4847. return cbp;
  4848. }
  4849. static int decode_cabac_mb_cbp_chroma( H264Context *h) {
  4850. int ctx;
  4851. int cbp_a, cbp_b;
  4852. cbp_a = (h->left_cbp>>4)&0x03;
  4853. cbp_b = (h-> top_cbp>>4)&0x03;
  4854. ctx = 0;
  4855. if( cbp_a > 0 ) ctx++;
  4856. if( cbp_b > 0 ) ctx += 2;
  4857. if( get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] ) == 0 )
  4858. return 0;
  4859. ctx = 4;
  4860. if( cbp_a == 2 ) ctx++;
  4861. if( cbp_b == 2 ) ctx += 2;
  4862. return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] );
  4863. }
  4864. static int decode_cabac_mb_dqp( H264Context *h) {
  4865. MpegEncContext * const s = &h->s;
  4866. int mbn_xy;
  4867. int ctx = 0;
  4868. int val = 0;
  4869. if( s->mb_x > 0 )
  4870. mbn_xy = s->mb_x + s->mb_y*s->mb_stride - 1;
  4871. else
  4872. mbn_xy = s->mb_width - 1 + (s->mb_y-1)*s->mb_stride;
  4873. if( h->last_qscale_diff != 0 )
  4874. ctx++;
  4875. while( get_cabac_noinline( &h->cabac, &h->cabac_state[60 + ctx] ) ) {
  4876. if( ctx < 2 )
  4877. ctx = 2;
  4878. else
  4879. ctx = 3;
  4880. val++;
  4881. if(val > 102) //prevent infinite loop
  4882. return INT_MIN;
  4883. }
  4884. if( val&0x01 )
  4885. return (val + 1)/2;
  4886. else
  4887. return -(val + 1)/2;
  4888. }
  4889. static int decode_cabac_p_mb_sub_type( H264Context *h ) {
  4890. if( get_cabac( &h->cabac, &h->cabac_state[21] ) )
  4891. return 0; /* 8x8 */
  4892. if( !get_cabac( &h->cabac, &h->cabac_state[22] ) )
  4893. return 1; /* 8x4 */
  4894. if( get_cabac( &h->cabac, &h->cabac_state[23] ) )
  4895. return 2; /* 4x8 */
  4896. return 3; /* 4x4 */
  4897. }
  4898. static int decode_cabac_b_mb_sub_type( H264Context *h ) {
  4899. int type;
  4900. if( !get_cabac( &h->cabac, &h->cabac_state[36] ) )
  4901. return 0; /* B_Direct_8x8 */
  4902. if( !get_cabac( &h->cabac, &h->cabac_state[37] ) )
  4903. return 1 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L0_8x8, B_L1_8x8 */
  4904. type = 3;
  4905. if( get_cabac( &h->cabac, &h->cabac_state[38] ) ) {
  4906. if( get_cabac( &h->cabac, &h->cabac_state[39] ) )
  4907. return 11 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L1_4x4, B_Bi_4x4 */
  4908. type += 4;
  4909. }
  4910. type += 2*get_cabac( &h->cabac, &h->cabac_state[39] );
  4911. type += get_cabac( &h->cabac, &h->cabac_state[39] );
  4912. return type;
  4913. }
  4914. static inline int decode_cabac_mb_transform_size( H264Context *h ) {
  4915. return get_cabac_noinline( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] );
  4916. }
  4917. static int decode_cabac_mb_ref( H264Context *h, int list, int n ) {
  4918. int refa = h->ref_cache[list][scan8[n] - 1];
  4919. int refb = h->ref_cache[list][scan8[n] - 8];
  4920. int ref = 0;
  4921. int ctx = 0;
  4922. if( h->slice_type == B_TYPE) {
  4923. if( refa > 0 && !h->direct_cache[scan8[n] - 1] )
  4924. ctx++;
  4925. if( refb > 0 && !h->direct_cache[scan8[n] - 8] )
  4926. ctx += 2;
  4927. } else {
  4928. if( refa > 0 )
  4929. ctx++;
  4930. if( refb > 0 )
  4931. ctx += 2;
  4932. }
  4933. while( get_cabac( &h->cabac, &h->cabac_state[54+ctx] ) ) {
  4934. ref++;
  4935. if( ctx < 4 )
  4936. ctx = 4;
  4937. else
  4938. ctx = 5;
  4939. if(ref >= 32 /*h->ref_list[list]*/){
  4940. av_log(h->s.avctx, AV_LOG_ERROR, "overflow in decode_cabac_mb_ref\n");
  4941. return 0; //FIXME we should return -1 and check the return everywhere
  4942. }
  4943. }
  4944. return ref;
  4945. }
  4946. static int decode_cabac_mb_mvd( H264Context *h, int list, int n, int l ) {
  4947. int amvd = abs( h->mvd_cache[list][scan8[n] - 1][l] ) +
  4948. abs( h->mvd_cache[list][scan8[n] - 8][l] );
  4949. int ctxbase = (l == 0) ? 40 : 47;
  4950. int ctx, mvd;
  4951. if( amvd < 3 )
  4952. ctx = 0;
  4953. else if( amvd > 32 )
  4954. ctx = 2;
  4955. else
  4956. ctx = 1;
  4957. if(!get_cabac(&h->cabac, &h->cabac_state[ctxbase+ctx]))
  4958. return 0;
  4959. mvd= 1;
  4960. ctx= 3;
  4961. while( mvd < 9 && get_cabac( &h->cabac, &h->cabac_state[ctxbase+ctx] ) ) {
  4962. mvd++;
  4963. if( ctx < 6 )
  4964. ctx++;
  4965. }
  4966. if( mvd >= 9 ) {
  4967. int k = 3;
  4968. while( get_cabac_bypass( &h->cabac ) ) {
  4969. mvd += 1 << k;
  4970. k++;
  4971. if(k>24){
  4972. av_log(h->s.avctx, AV_LOG_ERROR, "overflow in decode_cabac_mb_mvd\n");
  4973. return INT_MIN;
  4974. }
  4975. }
  4976. while( k-- ) {
  4977. if( get_cabac_bypass( &h->cabac ) )
  4978. mvd += 1 << k;
  4979. }
  4980. }
  4981. return get_cabac_bypass_sign( &h->cabac, -mvd );
  4982. }
  4983. static inline int get_cabac_cbf_ctx( H264Context *h, int cat, int idx ) {
  4984. int nza, nzb;
  4985. int ctx = 0;
  4986. if( cat == 0 ) {
  4987. nza = h->left_cbp&0x100;
  4988. nzb = h-> top_cbp&0x100;
  4989. } else if( cat == 1 || cat == 2 ) {
  4990. nza = h->non_zero_count_cache[scan8[idx] - 1];
  4991. nzb = h->non_zero_count_cache[scan8[idx] - 8];
  4992. } else if( cat == 3 ) {
  4993. nza = (h->left_cbp>>(6+idx))&0x01;
  4994. nzb = (h-> top_cbp>>(6+idx))&0x01;
  4995. } else {
  4996. assert(cat == 4);
  4997. nza = h->non_zero_count_cache[scan8[16+idx] - 1];
  4998. nzb = h->non_zero_count_cache[scan8[16+idx] - 8];
  4999. }
  5000. if( nza > 0 )
  5001. ctx++;
  5002. if( nzb > 0 )
  5003. ctx += 2;
  5004. return ctx + 4 * cat;
  5005. }
  5006. static const attribute_used uint8_t last_coeff_flag_offset_8x8[63] = {
  5007. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  5008. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  5009. 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  5010. 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
  5011. };
  5012. static int decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff) {
  5013. const int mb_xy = h->s.mb_x + h->s.mb_y*h->s.mb_stride;
  5014. static const int significant_coeff_flag_offset[2][6] = {
  5015. { 105+0, 105+15, 105+29, 105+44, 105+47, 402 },
  5016. { 277+0, 277+15, 277+29, 277+44, 277+47, 436 }
  5017. };
  5018. static const int last_coeff_flag_offset[2][6] = {
  5019. { 166+0, 166+15, 166+29, 166+44, 166+47, 417 },
  5020. { 338+0, 338+15, 338+29, 338+44, 338+47, 451 }
  5021. };
  5022. static const int coeff_abs_level_m1_offset[6] = {
  5023. 227+0, 227+10, 227+20, 227+30, 227+39, 426
  5024. };
  5025. static const uint8_t significant_coeff_flag_offset_8x8[2][63] = {
  5026. { 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
  5027. 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
  5028. 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
  5029. 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12 },
  5030. { 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
  5031. 6, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11,
  5032. 9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,
  5033. 9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14 }
  5034. };
  5035. int index[64];
  5036. int last;
  5037. int coeff_count = 0;
  5038. int abslevel1 = 1;
  5039. int abslevelgt1 = 0;
  5040. uint8_t *significant_coeff_ctx_base;
  5041. uint8_t *last_coeff_ctx_base;
  5042. uint8_t *abs_level_m1_ctx_base;
  5043. #ifndef ARCH_X86
  5044. #define CABAC_ON_STACK
  5045. #endif
  5046. #ifdef CABAC_ON_STACK
  5047. #define CC &cc
  5048. CABACContext cc;
  5049. cc.range = h->cabac.range;
  5050. cc.low = h->cabac.low;
  5051. cc.bytestream= h->cabac.bytestream;
  5052. #else
  5053. #define CC &h->cabac
  5054. #endif
  5055. /* cat: 0-> DC 16x16 n = 0
  5056. * 1-> AC 16x16 n = luma4x4idx
  5057. * 2-> Luma4x4 n = luma4x4idx
  5058. * 3-> DC Chroma n = iCbCr
  5059. * 4-> AC Chroma n = 4 * iCbCr + chroma4x4idx
  5060. * 5-> Luma8x8 n = 4 * luma8x8idx
  5061. */
  5062. /* read coded block flag */
  5063. if( cat != 5 ) {
  5064. if( get_cabac( CC, &h->cabac_state[85 + get_cabac_cbf_ctx( h, cat, n ) ] ) == 0 ) {
  5065. if( cat == 1 || cat == 2 )
  5066. h->non_zero_count_cache[scan8[n]] = 0;
  5067. else if( cat == 4 )
  5068. h->non_zero_count_cache[scan8[16+n]] = 0;
  5069. #ifdef CABAC_ON_STACK
  5070. h->cabac.range = cc.range ;
  5071. h->cabac.low = cc.low ;
  5072. h->cabac.bytestream= cc.bytestream;
  5073. #endif
  5074. return 0;
  5075. }
  5076. }
  5077. significant_coeff_ctx_base = h->cabac_state
  5078. + significant_coeff_flag_offset[MB_FIELD][cat];
  5079. last_coeff_ctx_base = h->cabac_state
  5080. + last_coeff_flag_offset[MB_FIELD][cat];
  5081. abs_level_m1_ctx_base = h->cabac_state
  5082. + coeff_abs_level_m1_offset[cat];
  5083. if( cat == 5 ) {
  5084. #define DECODE_SIGNIFICANCE( coefs, sig_off, last_off ) \
  5085. for(last= 0; last < coefs; last++) { \
  5086. uint8_t *sig_ctx = significant_coeff_ctx_base + sig_off; \
  5087. if( get_cabac( CC, sig_ctx )) { \
  5088. uint8_t *last_ctx = last_coeff_ctx_base + last_off; \
  5089. index[coeff_count++] = last; \
  5090. if( get_cabac( CC, last_ctx ) ) { \
  5091. last= max_coeff; \
  5092. break; \
  5093. } \
  5094. } \
  5095. }\
  5096. if( last == max_coeff -1 ) {\
  5097. index[coeff_count++] = last;\
  5098. }
  5099. const uint8_t *sig_off = significant_coeff_flag_offset_8x8[MB_FIELD];
  5100. #if defined(ARCH_X86) && defined(CONFIG_7REGS) && defined(HAVE_EBX_AVAILABLE) && !defined(BROKEN_RELOCATIONS)
  5101. coeff_count= decode_significance_8x8_x86(CC, significant_coeff_ctx_base, index, sig_off);
  5102. } else {
  5103. coeff_count= decode_significance_x86(CC, max_coeff, significant_coeff_ctx_base, index);
  5104. #else
  5105. DECODE_SIGNIFICANCE( 63, sig_off[last], last_coeff_flag_offset_8x8[last] );
  5106. } else {
  5107. DECODE_SIGNIFICANCE( max_coeff - 1, last, last );
  5108. #endif
  5109. }
  5110. assert(coeff_count > 0);
  5111. if( cat == 0 )
  5112. h->cbp_table[mb_xy] |= 0x100;
  5113. else if( cat == 1 || cat == 2 )
  5114. h->non_zero_count_cache[scan8[n]] = coeff_count;
  5115. else if( cat == 3 )
  5116. h->cbp_table[mb_xy] |= 0x40 << n;
  5117. else if( cat == 4 )
  5118. h->non_zero_count_cache[scan8[16+n]] = coeff_count;
  5119. else {
  5120. assert( cat == 5 );
  5121. fill_rectangle(&h->non_zero_count_cache[scan8[n]], 2, 2, 8, coeff_count, 1);
  5122. }
  5123. for( coeff_count--; coeff_count >= 0; coeff_count-- ) {
  5124. uint8_t *ctx = (abslevelgt1 != 0 ? 0 : FFMIN( 4, abslevel1 )) + abs_level_m1_ctx_base;
  5125. int j= scantable[index[coeff_count]];
  5126. if( get_cabac( CC, ctx ) == 0 ) {
  5127. if( !qmul ) {
  5128. block[j] = get_cabac_bypass_sign( CC, -1);
  5129. }else{
  5130. block[j] = (get_cabac_bypass_sign( CC, -qmul[j]) + 32) >> 6;;
  5131. }
  5132. abslevel1++;
  5133. } else {
  5134. int coeff_abs = 2;
  5135. ctx = 5 + FFMIN( 4, abslevelgt1 ) + abs_level_m1_ctx_base;
  5136. while( coeff_abs < 15 && get_cabac( CC, ctx ) ) {
  5137. coeff_abs++;
  5138. }
  5139. if( coeff_abs >= 15 ) {
  5140. int j = 0;
  5141. while( get_cabac_bypass( CC ) ) {
  5142. j++;
  5143. }
  5144. coeff_abs=1;
  5145. while( j-- ) {
  5146. coeff_abs += coeff_abs + get_cabac_bypass( CC );
  5147. }
  5148. coeff_abs+= 14;
  5149. }
  5150. if( !qmul ) {
  5151. if( get_cabac_bypass( CC ) ) block[j] = -coeff_abs;
  5152. else block[j] = coeff_abs;
  5153. }else{
  5154. if( get_cabac_bypass( CC ) ) block[j] = (-coeff_abs * qmul[j] + 32) >> 6;
  5155. else block[j] = ( coeff_abs * qmul[j] + 32) >> 6;
  5156. }
  5157. abslevelgt1++;
  5158. }
  5159. }
  5160. #ifdef CABAC_ON_STACK
  5161. h->cabac.range = cc.range ;
  5162. h->cabac.low = cc.low ;
  5163. h->cabac.bytestream= cc.bytestream;
  5164. #endif
  5165. return 0;
  5166. }
  5167. static inline void compute_mb_neighbors(H264Context *h)
  5168. {
  5169. MpegEncContext * const s = &h->s;
  5170. const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  5171. h->top_mb_xy = mb_xy - s->mb_stride;
  5172. h->left_mb_xy[0] = mb_xy - 1;
  5173. if(FRAME_MBAFF){
  5174. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  5175. const int top_pair_xy = pair_xy - s->mb_stride;
  5176. const int top_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  5177. const int left_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  5178. const int curr_mb_frame_flag = !MB_FIELD;
  5179. const int bottom = (s->mb_y & 1);
  5180. if (bottom
  5181. ? !curr_mb_frame_flag // bottom macroblock
  5182. : (!curr_mb_frame_flag && !top_mb_frame_flag) // top macroblock
  5183. ) {
  5184. h->top_mb_xy -= s->mb_stride;
  5185. }
  5186. if (left_mb_frame_flag != curr_mb_frame_flag) {
  5187. h->left_mb_xy[0] = pair_xy - 1;
  5188. }
  5189. }
  5190. return;
  5191. }
  5192. /**
  5193. * decodes a macroblock
  5194. * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  5195. */
  5196. static int decode_mb_cabac(H264Context *h) {
  5197. MpegEncContext * const s = &h->s;
  5198. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  5199. int mb_type, partition_count, cbp = 0;
  5200. int dct8x8_allowed= h->pps.transform_8x8_mode;
  5201. s->dsp.clear_blocks(h->mb); //FIXME avoid if already clear (move after skip handlong?)
  5202. tprintf(s->avctx, "pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  5203. if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE ) {
  5204. int skip;
  5205. /* a skipped mb needs the aff flag from the following mb */
  5206. if( FRAME_MBAFF && s->mb_x==0 && (s->mb_y&1)==0 )
  5207. predict_field_decoding_flag(h);
  5208. if( FRAME_MBAFF && (s->mb_y&1)==1 && h->prev_mb_skipped )
  5209. skip = h->next_mb_skipped;
  5210. else
  5211. skip = decode_cabac_mb_skip( h, s->mb_x, s->mb_y );
  5212. /* read skip flags */
  5213. if( skip ) {
  5214. if( FRAME_MBAFF && (s->mb_y&1)==0 ){
  5215. s->current_picture.mb_type[mb_xy] = MB_TYPE_SKIP;
  5216. h->next_mb_skipped = decode_cabac_mb_skip( h, s->mb_x, s->mb_y+1 );
  5217. if(h->next_mb_skipped)
  5218. predict_field_decoding_flag(h);
  5219. else
  5220. h->mb_mbaff = h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  5221. }
  5222. decode_mb_skip(h);
  5223. h->cbp_table[mb_xy] = 0;
  5224. h->chroma_pred_mode_table[mb_xy] = 0;
  5225. h->last_qscale_diff = 0;
  5226. return 0;
  5227. }
  5228. }
  5229. if(FRAME_MBAFF){
  5230. if( (s->mb_y&1) == 0 )
  5231. h->mb_mbaff =
  5232. h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  5233. }else
  5234. h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
  5235. h->prev_mb_skipped = 0;
  5236. compute_mb_neighbors(h);
  5237. if( ( mb_type = decode_cabac_mb_type( h ) ) < 0 ) {
  5238. av_log( h->s.avctx, AV_LOG_ERROR, "decode_cabac_mb_type failed\n" );
  5239. return -1;
  5240. }
  5241. if( h->slice_type == B_TYPE ) {
  5242. if( mb_type < 23 ){
  5243. partition_count= b_mb_type_info[mb_type].partition_count;
  5244. mb_type= b_mb_type_info[mb_type].type;
  5245. }else{
  5246. mb_type -= 23;
  5247. goto decode_intra_mb;
  5248. }
  5249. } else if( h->slice_type == P_TYPE ) {
  5250. if( mb_type < 5) {
  5251. partition_count= p_mb_type_info[mb_type].partition_count;
  5252. mb_type= p_mb_type_info[mb_type].type;
  5253. } else {
  5254. mb_type -= 5;
  5255. goto decode_intra_mb;
  5256. }
  5257. } else {
  5258. assert(h->slice_type == I_TYPE);
  5259. decode_intra_mb:
  5260. partition_count = 0;
  5261. cbp= i_mb_type_info[mb_type].cbp;
  5262. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  5263. mb_type= i_mb_type_info[mb_type].type;
  5264. }
  5265. if(MB_FIELD)
  5266. mb_type |= MB_TYPE_INTERLACED;
  5267. h->slice_table[ mb_xy ]= h->slice_num;
  5268. if(IS_INTRA_PCM(mb_type)) {
  5269. const uint8_t *ptr;
  5270. unsigned int x, y;
  5271. // We assume these blocks are very rare so we dont optimize it.
  5272. // FIXME The two following lines get the bitstream position in the cabac
  5273. // decode, I think it should be done by a function in cabac.h (or cabac.c).
  5274. ptr= h->cabac.bytestream;
  5275. if(h->cabac.low&0x1) ptr--;
  5276. if(CABAC_BITS==16){
  5277. if(h->cabac.low&0x1FF) ptr--;
  5278. }
  5279. // The pixels are stored in the same order as levels in h->mb array.
  5280. for(y=0; y<16; y++){
  5281. const int index= 4*(y&3) + 32*((y>>2)&1) + 128*(y>>3);
  5282. for(x=0; x<16; x++){
  5283. tprintf(s->avctx, "LUMA ICPM LEVEL (%3d)\n", *ptr);
  5284. h->mb[index + (x&3) + 16*((x>>2)&1) + 64*(x>>3)]= *ptr++;
  5285. }
  5286. }
  5287. for(y=0; y<8; y++){
  5288. const int index= 256 + 4*(y&3) + 32*(y>>2);
  5289. for(x=0; x<8; x++){
  5290. tprintf(s->avctx, "CHROMA U ICPM LEVEL (%3d)\n", *ptr);
  5291. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  5292. }
  5293. }
  5294. for(y=0; y<8; y++){
  5295. const int index= 256 + 64 + 4*(y&3) + 32*(y>>2);
  5296. for(x=0; x<8; x++){
  5297. tprintf(s->avctx, "CHROMA V ICPM LEVEL (%3d)\n", *ptr);
  5298. h->mb[index + (x&3) + 16*(x>>2)]= *ptr++;
  5299. }
  5300. }
  5301. ff_init_cabac_decoder(&h->cabac, ptr, h->cabac.bytestream_end - ptr);
  5302. // All blocks are present
  5303. h->cbp_table[mb_xy] = 0x1ef;
  5304. h->chroma_pred_mode_table[mb_xy] = 0;
  5305. // In deblocking, the quantizer is 0
  5306. s->current_picture.qscale_table[mb_xy]= 0;
  5307. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
  5308. // All coeffs are present
  5309. memset(h->non_zero_count[mb_xy], 16, 16);
  5310. s->current_picture.mb_type[mb_xy]= mb_type;
  5311. return 0;
  5312. }
  5313. if(MB_MBAFF){
  5314. h->ref_count[0] <<= 1;
  5315. h->ref_count[1] <<= 1;
  5316. }
  5317. fill_caches(h, mb_type, 0);
  5318. if( IS_INTRA( mb_type ) ) {
  5319. int i, pred_mode;
  5320. if( IS_INTRA4x4( mb_type ) ) {
  5321. if( dct8x8_allowed && decode_cabac_mb_transform_size( h ) ) {
  5322. mb_type |= MB_TYPE_8x8DCT;
  5323. for( i = 0; i < 16; i+=4 ) {
  5324. int pred = pred_intra_mode( h, i );
  5325. int mode = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  5326. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  5327. }
  5328. } else {
  5329. for( i = 0; i < 16; i++ ) {
  5330. int pred = pred_intra_mode( h, i );
  5331. h->intra4x4_pred_mode_cache[ scan8[i] ] = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  5332. //av_log( s->avctx, AV_LOG_ERROR, "i4x4 pred=%d mode=%d\n", pred, h->intra4x4_pred_mode_cache[ scan8[i] ] );
  5333. }
  5334. }
  5335. write_back_intra_pred_mode(h);
  5336. if( check_intra4x4_pred_mode(h) < 0 ) return -1;
  5337. } else {
  5338. h->intra16x16_pred_mode= check_intra_pred_mode( h, h->intra16x16_pred_mode );
  5339. if( h->intra16x16_pred_mode < 0 ) return -1;
  5340. }
  5341. h->chroma_pred_mode_table[mb_xy] =
  5342. pred_mode = decode_cabac_mb_chroma_pre_mode( h );
  5343. pred_mode= check_intra_pred_mode( h, pred_mode );
  5344. if( pred_mode < 0 ) return -1;
  5345. h->chroma_pred_mode= pred_mode;
  5346. } else if( partition_count == 4 ) {
  5347. int i, j, sub_partition_count[4], list, ref[2][4];
  5348. if( h->slice_type == B_TYPE ) {
  5349. for( i = 0; i < 4; i++ ) {
  5350. h->sub_mb_type[i] = decode_cabac_b_mb_sub_type( h );
  5351. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5352. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5353. }
  5354. if( IS_DIRECT(h->sub_mb_type[0] | h->sub_mb_type[1] |
  5355. h->sub_mb_type[2] | h->sub_mb_type[3]) ) {
  5356. pred_direct_motion(h, &mb_type);
  5357. if( h->ref_count[0] > 1 || h->ref_count[1] > 1 ) {
  5358. for( i = 0; i < 4; i++ )
  5359. if( IS_DIRECT(h->sub_mb_type[i]) )
  5360. fill_rectangle( &h->direct_cache[scan8[4*i]], 2, 2, 8, 1, 1 );
  5361. }
  5362. }
  5363. } else {
  5364. for( i = 0; i < 4; i++ ) {
  5365. h->sub_mb_type[i] = decode_cabac_p_mb_sub_type( h );
  5366. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  5367. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  5368. }
  5369. }
  5370. for( list = 0; list < h->list_count; list++ ) {
  5371. for( i = 0; i < 4; i++ ) {
  5372. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  5373. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  5374. if( h->ref_count[list] > 1 )
  5375. ref[list][i] = decode_cabac_mb_ref( h, list, 4*i );
  5376. else
  5377. ref[list][i] = 0;
  5378. } else {
  5379. ref[list][i] = -1;
  5380. }
  5381. h->ref_cache[list][ scan8[4*i]+1 ]=
  5382. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  5383. }
  5384. }
  5385. if(dct8x8_allowed)
  5386. dct8x8_allowed = get_dct8x8_allowed(h);
  5387. for(list=0; list<h->list_count; list++){
  5388. for(i=0; i<4; i++){
  5389. if(IS_DIRECT(h->sub_mb_type[i])){
  5390. fill_rectangle(h->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 4);
  5391. continue;
  5392. }
  5393. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ];
  5394. if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
  5395. const int sub_mb_type= h->sub_mb_type[i];
  5396. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  5397. for(j=0; j<sub_partition_count[i]; j++){
  5398. int mpx, mpy;
  5399. int mx, my;
  5400. const int index= 4*i + block_width*j;
  5401. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  5402. int16_t (* mvd_cache)[2]= &h->mvd_cache[list][ scan8[index] ];
  5403. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mpx, &mpy);
  5404. mx = mpx + decode_cabac_mb_mvd( h, list, index, 0 );
  5405. my = mpy + decode_cabac_mb_mvd( h, list, index, 1 );
  5406. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  5407. if(IS_SUB_8X8(sub_mb_type)){
  5408. mv_cache[ 1 ][0]=
  5409. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  5410. mv_cache[ 1 ][1]=
  5411. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  5412. mvd_cache[ 1 ][0]=
  5413. mvd_cache[ 8 ][0]= mvd_cache[ 9 ][0]= mx - mpx;
  5414. mvd_cache[ 1 ][1]=
  5415. mvd_cache[ 8 ][1]= mvd_cache[ 9 ][1]= my - mpy;
  5416. }else if(IS_SUB_8X4(sub_mb_type)){
  5417. mv_cache[ 1 ][0]= mx;
  5418. mv_cache[ 1 ][1]= my;
  5419. mvd_cache[ 1 ][0]= mx - mpx;
  5420. mvd_cache[ 1 ][1]= my - mpy;
  5421. }else if(IS_SUB_4X8(sub_mb_type)){
  5422. mv_cache[ 8 ][0]= mx;
  5423. mv_cache[ 8 ][1]= my;
  5424. mvd_cache[ 8 ][0]= mx - mpx;
  5425. mvd_cache[ 8 ][1]= my - mpy;
  5426. }
  5427. mv_cache[ 0 ][0]= mx;
  5428. mv_cache[ 0 ][1]= my;
  5429. mvd_cache[ 0 ][0]= mx - mpx;
  5430. mvd_cache[ 0 ][1]= my - mpy;
  5431. }
  5432. }else{
  5433. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  5434. uint32_t *pd= (uint32_t *)&h->mvd_cache[list][ scan8[4*i] ][0];
  5435. p[0] = p[1] = p[8] = p[9] = 0;
  5436. pd[0]= pd[1]= pd[8]= pd[9]= 0;
  5437. }
  5438. }
  5439. }
  5440. } else if( IS_DIRECT(mb_type) ) {
  5441. pred_direct_motion(h, &mb_type);
  5442. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  5443. fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  5444. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  5445. } else {
  5446. int list, mx, my, i, mpx, mpy;
  5447. if(IS_16X16(mb_type)){
  5448. for(list=0; list<h->list_count; list++){
  5449. if(IS_DIR(mb_type, 0, list)){
  5450. const int ref = h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 0 ) : 0;
  5451. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1);
  5452. }else
  5453. 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
  5454. }
  5455. for(list=0; list<h->list_count; list++){
  5456. if(IS_DIR(mb_type, 0, list)){
  5457. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mpx, &mpy);
  5458. mx = mpx + decode_cabac_mb_mvd( h, list, 0, 0 );
  5459. my = mpy + decode_cabac_mb_mvd( h, list, 0, 1 );
  5460. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  5461. fill_rectangle(h->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5462. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  5463. }else
  5464. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  5465. }
  5466. }
  5467. else if(IS_16X8(mb_type)){
  5468. for(list=0; list<h->list_count; list++){
  5469. for(i=0; i<2; i++){
  5470. if(IS_DIR(mb_type, i, list)){
  5471. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 8*i ) : 0;
  5472. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, ref, 1);
  5473. }else
  5474. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  5475. }
  5476. }
  5477. for(list=0; list<h->list_count; list++){
  5478. for(i=0; i<2; i++){
  5479. if(IS_DIR(mb_type, i, list)){
  5480. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mpx, &mpy);
  5481. mx = mpx + decode_cabac_mb_mvd( h, list, 8*i, 0 );
  5482. my = mpy + decode_cabac_mb_mvd( h, list, 8*i, 1 );
  5483. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  5484. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx-mpx,my-mpy), 4);
  5485. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  5486. }else{
  5487. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5488. fill_rectangle(h-> mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  5489. }
  5490. }
  5491. }
  5492. }else{
  5493. assert(IS_8X16(mb_type));
  5494. for(list=0; list<h->list_count; list++){
  5495. for(i=0; i<2; i++){
  5496. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  5497. const int ref= h->ref_count[list] > 1 ? decode_cabac_mb_ref( h, list, 4*i ) : 0;
  5498. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, ref, 1);
  5499. }else
  5500. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  5501. }
  5502. }
  5503. for(list=0; list<h->list_count; list++){
  5504. for(i=0; i<2; i++){
  5505. if(IS_DIR(mb_type, i, list)){
  5506. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mpx, &mpy);
  5507. mx = mpx + decode_cabac_mb_mvd( h, list, 4*i, 0 );
  5508. my = mpy + decode_cabac_mb_mvd( h, list, 4*i, 1 );
  5509. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  5510. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  5511. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  5512. }else{
  5513. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5514. fill_rectangle(h-> mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  5515. }
  5516. }
  5517. }
  5518. }
  5519. }
  5520. if( IS_INTER( mb_type ) ) {
  5521. h->chroma_pred_mode_table[mb_xy] = 0;
  5522. write_back_motion( h, mb_type );
  5523. }
  5524. if( !IS_INTRA16x16( mb_type ) ) {
  5525. cbp = decode_cabac_mb_cbp_luma( h );
  5526. cbp |= decode_cabac_mb_cbp_chroma( h ) << 4;
  5527. }
  5528. h->cbp_table[mb_xy] = h->cbp = cbp;
  5529. if( dct8x8_allowed && (cbp&15) && !IS_INTRA( mb_type ) ) {
  5530. if( decode_cabac_mb_transform_size( h ) )
  5531. mb_type |= MB_TYPE_8x8DCT;
  5532. }
  5533. s->current_picture.mb_type[mb_xy]= mb_type;
  5534. if( cbp || IS_INTRA16x16( mb_type ) ) {
  5535. const uint8_t *scan, *scan8x8, *dc_scan;
  5536. int dqp;
  5537. if(IS_INTERLACED(mb_type)){
  5538. scan8x8= s->qscale ? h->field_scan8x8 : h->field_scan8x8_q0;
  5539. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  5540. dc_scan= luma_dc_field_scan;
  5541. }else{
  5542. scan8x8= s->qscale ? h->zigzag_scan8x8 : h->zigzag_scan8x8_q0;
  5543. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  5544. dc_scan= luma_dc_zigzag_scan;
  5545. }
  5546. h->last_qscale_diff = dqp = decode_cabac_mb_dqp( h );
  5547. if( dqp == INT_MIN ){
  5548. av_log(h->s.avctx, AV_LOG_ERROR, "cabac decode of qscale diff failed at %d %d\n", s->mb_x, s->mb_y);
  5549. return -1;
  5550. }
  5551. s->qscale += dqp;
  5552. if(((unsigned)s->qscale) > 51){
  5553. if(s->qscale<0) s->qscale+= 52;
  5554. else s->qscale-= 52;
  5555. }
  5556. h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
  5557. if( IS_INTRA16x16( mb_type ) ) {
  5558. int i;
  5559. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 DC\n" );
  5560. if( decode_cabac_residual( h, h->mb, 0, 0, dc_scan, NULL, 16) < 0)
  5561. return -1;
  5562. if( cbp&15 ) {
  5563. for( i = 0; i < 16; i++ ) {
  5564. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 AC:%d\n", i );
  5565. if( decode_cabac_residual(h, h->mb + 16*i, 1, i, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 )
  5566. return -1;
  5567. }
  5568. } else {
  5569. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  5570. }
  5571. } else {
  5572. int i8x8, i4x4;
  5573. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  5574. if( cbp & (1<<i8x8) ) {
  5575. if( IS_8x8DCT(mb_type) ) {
  5576. if( decode_cabac_residual(h, h->mb + 64*i8x8, 5, 4*i8x8,
  5577. scan8x8, h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 64) < 0 )
  5578. return -1;
  5579. } else
  5580. for( i4x4 = 0; i4x4 < 4; i4x4++ ) {
  5581. const int index = 4*i8x8 + i4x4;
  5582. //av_log( s->avctx, AV_LOG_ERROR, "Luma4x4: %d\n", index );
  5583. //START_TIMER
  5584. 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 )
  5585. return -1;
  5586. //STOP_TIMER("decode_residual")
  5587. }
  5588. } else {
  5589. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  5590. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  5591. }
  5592. }
  5593. }
  5594. if( cbp&0x30 ){
  5595. int c;
  5596. for( c = 0; c < 2; c++ ) {
  5597. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-DC\n",c );
  5598. if( decode_cabac_residual(h, h->mb + 256 + 16*4*c, 3, c, chroma_dc_scan, NULL, 4) < 0)
  5599. return -1;
  5600. }
  5601. }
  5602. if( cbp&0x20 ) {
  5603. int c, i;
  5604. for( c = 0; c < 2; c++ ) {
  5605. for( i = 0; i < 4; i++ ) {
  5606. const int index = 16 + 4 * c + i;
  5607. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-AC %d\n",c, index - 16 );
  5608. 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)
  5609. return -1;
  5610. }
  5611. }
  5612. } else {
  5613. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5614. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5615. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5616. }
  5617. } else {
  5618. uint8_t * const nnz= &h->non_zero_count_cache[0];
  5619. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  5620. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  5621. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  5622. h->last_qscale_diff = 0;
  5623. }
  5624. s->current_picture.qscale_table[mb_xy]= s->qscale;
  5625. write_back_non_zero_count(h);
  5626. if(MB_MBAFF){
  5627. h->ref_count[0] >>= 1;
  5628. h->ref_count[1] >>= 1;
  5629. }
  5630. return 0;
  5631. }
  5632. static void filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5633. int i, d;
  5634. const int index_a = qp + h->slice_alpha_c0_offset;
  5635. const int alpha = (alpha_table+52)[index_a];
  5636. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5637. if( bS[0] < 4 ) {
  5638. int8_t tc[4];
  5639. for(i=0; i<4; i++)
  5640. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] : -1;
  5641. h->s.dsp.h264_h_loop_filter_luma(pix, stride, alpha, beta, tc);
  5642. } else {
  5643. /* 16px edge length, because bS=4 is triggered by being at
  5644. * the edge of an intra MB, so all 4 bS are the same */
  5645. for( d = 0; d < 16; d++ ) {
  5646. const int p0 = pix[-1];
  5647. const int p1 = pix[-2];
  5648. const int p2 = pix[-3];
  5649. const int q0 = pix[0];
  5650. const int q1 = pix[1];
  5651. const int q2 = pix[2];
  5652. if( FFABS( p0 - q0 ) < alpha &&
  5653. FFABS( p1 - p0 ) < beta &&
  5654. FFABS( q1 - q0 ) < beta ) {
  5655. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5656. if( FFABS( p2 - p0 ) < beta)
  5657. {
  5658. const int p3 = pix[-4];
  5659. /* p0', p1', p2' */
  5660. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5661. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5662. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5663. } else {
  5664. /* p0' */
  5665. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5666. }
  5667. if( FFABS( q2 - q0 ) < beta)
  5668. {
  5669. const int q3 = pix[3];
  5670. /* q0', q1', q2' */
  5671. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5672. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5673. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5674. } else {
  5675. /* q0' */
  5676. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5677. }
  5678. }else{
  5679. /* p0', q0' */
  5680. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5681. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5682. }
  5683. 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]);
  5684. }
  5685. pix += stride;
  5686. }
  5687. }
  5688. }
  5689. static void filter_mb_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5690. int i;
  5691. const int index_a = qp + h->slice_alpha_c0_offset;
  5692. const int alpha = (alpha_table+52)[index_a];
  5693. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5694. if( bS[0] < 4 ) {
  5695. int8_t tc[4];
  5696. for(i=0; i<4; i++)
  5697. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] + 1 : 0;
  5698. h->s.dsp.h264_h_loop_filter_chroma(pix, stride, alpha, beta, tc);
  5699. } else {
  5700. h->s.dsp.h264_h_loop_filter_chroma_intra(pix, stride, alpha, beta);
  5701. }
  5702. }
  5703. static void filter_mb_mbaff_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
  5704. int i;
  5705. for( i = 0; i < 16; i++, pix += stride) {
  5706. int index_a;
  5707. int alpha;
  5708. int beta;
  5709. int qp_index;
  5710. int bS_index = (i >> 1);
  5711. if (!MB_FIELD) {
  5712. bS_index &= ~1;
  5713. bS_index |= (i & 1);
  5714. }
  5715. if( bS[bS_index] == 0 ) {
  5716. continue;
  5717. }
  5718. qp_index = MB_FIELD ? (i >> 3) : (i & 1);
  5719. index_a = qp[qp_index] + h->slice_alpha_c0_offset;
  5720. alpha = (alpha_table+52)[index_a];
  5721. beta = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
  5722. if( bS[bS_index] < 4 ) {
  5723. const int tc0 = (tc0_table+52)[index_a][bS[bS_index] - 1];
  5724. const int p0 = pix[-1];
  5725. const int p1 = pix[-2];
  5726. const int p2 = pix[-3];
  5727. const int q0 = pix[0];
  5728. const int q1 = pix[1];
  5729. const int q2 = pix[2];
  5730. if( FFABS( p0 - q0 ) < alpha &&
  5731. FFABS( p1 - p0 ) < beta &&
  5732. FFABS( q1 - q0 ) < beta ) {
  5733. int tc = tc0;
  5734. int i_delta;
  5735. if( FFABS( p2 - p0 ) < beta ) {
  5736. pix[-2] = p1 + av_clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
  5737. tc++;
  5738. }
  5739. if( FFABS( q2 - q0 ) < beta ) {
  5740. pix[1] = q1 + av_clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
  5741. tc++;
  5742. }
  5743. i_delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  5744. pix[-1] = av_clip_uint8( p0 + i_delta ); /* p0' */
  5745. pix[0] = av_clip_uint8( q0 - i_delta ); /* q0' */
  5746. 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);
  5747. }
  5748. }else{
  5749. const int p0 = pix[-1];
  5750. const int p1 = pix[-2];
  5751. const int p2 = pix[-3];
  5752. const int q0 = pix[0];
  5753. const int q1 = pix[1];
  5754. const int q2 = pix[2];
  5755. if( FFABS( p0 - q0 ) < alpha &&
  5756. FFABS( p1 - p0 ) < beta &&
  5757. FFABS( q1 - q0 ) < beta ) {
  5758. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5759. if( FFABS( p2 - p0 ) < beta)
  5760. {
  5761. const int p3 = pix[-4];
  5762. /* p0', p1', p2' */
  5763. pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5764. pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5765. pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5766. } else {
  5767. /* p0' */
  5768. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5769. }
  5770. if( FFABS( q2 - q0 ) < beta)
  5771. {
  5772. const int q3 = pix[3];
  5773. /* q0', q1', q2' */
  5774. pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5775. pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5776. pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5777. } else {
  5778. /* q0' */
  5779. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5780. }
  5781. }else{
  5782. /* p0', q0' */
  5783. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5784. pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5785. }
  5786. 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]);
  5787. }
  5788. }
  5789. }
  5790. }
  5791. static void filter_mb_mbaff_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
  5792. int i;
  5793. for( i = 0; i < 8; i++, pix += stride) {
  5794. int index_a;
  5795. int alpha;
  5796. int beta;
  5797. int qp_index;
  5798. int bS_index = i;
  5799. if( bS[bS_index] == 0 ) {
  5800. continue;
  5801. }
  5802. qp_index = MB_FIELD ? (i >> 2) : (i & 1);
  5803. index_a = qp[qp_index] + h->slice_alpha_c0_offset;
  5804. alpha = (alpha_table+52)[index_a];
  5805. beta = (beta_table+52)[qp[qp_index] + h->slice_beta_offset];
  5806. if( bS[bS_index] < 4 ) {
  5807. const int tc = (tc0_table+52)[index_a][bS[bS_index] - 1] + 1;
  5808. const int p0 = pix[-1];
  5809. const int p1 = pix[-2];
  5810. const int q0 = pix[0];
  5811. const int q1 = pix[1];
  5812. if( FFABS( p0 - q0 ) < alpha &&
  5813. FFABS( p1 - p0 ) < beta &&
  5814. FFABS( q1 - q0 ) < beta ) {
  5815. const int i_delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  5816. pix[-1] = av_clip_uint8( p0 + i_delta ); /* p0' */
  5817. pix[0] = av_clip_uint8( q0 - i_delta ); /* q0' */
  5818. 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);
  5819. }
  5820. }else{
  5821. const int p0 = pix[-1];
  5822. const int p1 = pix[-2];
  5823. const int q0 = pix[0];
  5824. const int q1 = pix[1];
  5825. if( FFABS( p0 - q0 ) < alpha &&
  5826. FFABS( p1 - p0 ) < beta &&
  5827. FFABS( q1 - q0 ) < beta ) {
  5828. pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
  5829. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
  5830. 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]);
  5831. }
  5832. }
  5833. }
  5834. }
  5835. static void filter_mb_edgeh( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5836. int i, d;
  5837. const int index_a = qp + h->slice_alpha_c0_offset;
  5838. const int alpha = (alpha_table+52)[index_a];
  5839. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5840. const int pix_next = stride;
  5841. if( bS[0] < 4 ) {
  5842. int8_t tc[4];
  5843. for(i=0; i<4; i++)
  5844. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] : -1;
  5845. h->s.dsp.h264_v_loop_filter_luma(pix, stride, alpha, beta, tc);
  5846. } else {
  5847. /* 16px edge length, see filter_mb_edgev */
  5848. for( d = 0; d < 16; d++ ) {
  5849. const int p0 = pix[-1*pix_next];
  5850. const int p1 = pix[-2*pix_next];
  5851. const int p2 = pix[-3*pix_next];
  5852. const int q0 = pix[0];
  5853. const int q1 = pix[1*pix_next];
  5854. const int q2 = pix[2*pix_next];
  5855. if( FFABS( p0 - q0 ) < alpha &&
  5856. FFABS( p1 - p0 ) < beta &&
  5857. FFABS( q1 - q0 ) < beta ) {
  5858. const int p3 = pix[-4*pix_next];
  5859. const int q3 = pix[ 3*pix_next];
  5860. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  5861. if( FFABS( p2 - p0 ) < beta) {
  5862. /* p0', p1', p2' */
  5863. pix[-1*pix_next] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  5864. pix[-2*pix_next] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  5865. pix[-3*pix_next] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  5866. } else {
  5867. /* p0' */
  5868. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5869. }
  5870. if( FFABS( q2 - q0 ) < beta) {
  5871. /* q0', q1', q2' */
  5872. pix[0*pix_next] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  5873. pix[1*pix_next] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  5874. pix[2*pix_next] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  5875. } else {
  5876. /* q0' */
  5877. pix[0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5878. }
  5879. }else{
  5880. /* p0', q0' */
  5881. pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  5882. pix[ 0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  5883. }
  5884. 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]);
  5885. }
  5886. pix++;
  5887. }
  5888. }
  5889. }
  5890. static void filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
  5891. int i;
  5892. const int index_a = qp + h->slice_alpha_c0_offset;
  5893. const int alpha = (alpha_table+52)[index_a];
  5894. const int beta = (beta_table+52)[qp + h->slice_beta_offset];
  5895. if( bS[0] < 4 ) {
  5896. int8_t tc[4];
  5897. for(i=0; i<4; i++)
  5898. tc[i] = bS[i] ? (tc0_table+52)[index_a][bS[i] - 1] + 1 : 0;
  5899. h->s.dsp.h264_v_loop_filter_chroma(pix, stride, alpha, beta, tc);
  5900. } else {
  5901. h->s.dsp.h264_v_loop_filter_chroma_intra(pix, stride, alpha, beta);
  5902. }
  5903. }
  5904. 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) {
  5905. MpegEncContext * const s = &h->s;
  5906. int mb_xy, mb_type;
  5907. int qp, qp0, qp1, qpc, qpc0, qpc1, qp_thresh;
  5908. if(mb_x==0 || mb_y==0 || !s->dsp.h264_loop_filter_strength) {
  5909. filter_mb(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize);
  5910. return;
  5911. }
  5912. assert(!FRAME_MBAFF);
  5913. mb_xy = mb_x + mb_y*s->mb_stride;
  5914. mb_type = s->current_picture.mb_type[mb_xy];
  5915. qp = s->current_picture.qscale_table[mb_xy];
  5916. qp0 = s->current_picture.qscale_table[mb_xy-1];
  5917. qp1 = s->current_picture.qscale_table[h->top_mb_xy];
  5918. qpc = get_chroma_qp( h->pps.chroma_qp_index_offset, qp );
  5919. qpc0 = get_chroma_qp( h->pps.chroma_qp_index_offset, qp0 );
  5920. qpc1 = get_chroma_qp( h->pps.chroma_qp_index_offset, qp1 );
  5921. qp0 = (qp + qp0 + 1) >> 1;
  5922. qp1 = (qp + qp1 + 1) >> 1;
  5923. qpc0 = (qpc + qpc0 + 1) >> 1;
  5924. qpc1 = (qpc + qpc1 + 1) >> 1;
  5925. qp_thresh = 15 - h->slice_alpha_c0_offset;
  5926. if(qp <= qp_thresh && qp0 <= qp_thresh && qp1 <= qp_thresh &&
  5927. qpc <= qp_thresh && qpc0 <= qp_thresh && qpc1 <= qp_thresh)
  5928. return;
  5929. if( IS_INTRA(mb_type) ) {
  5930. int16_t bS4[4] = {4,4,4,4};
  5931. int16_t bS3[4] = {3,3,3,3};
  5932. if( IS_8x8DCT(mb_type) ) {
  5933. filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
  5934. filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
  5935. filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bS4, qp1 );
  5936. filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
  5937. } else {
  5938. filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
  5939. filter_mb_edgev( h, &img_y[4*1], linesize, bS3, qp );
  5940. filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
  5941. filter_mb_edgev( h, &img_y[4*3], linesize, bS3, qp );
  5942. filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bS4, qp1 );
  5943. filter_mb_edgeh( h, &img_y[4*1*linesize], linesize, bS3, qp );
  5944. filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
  5945. filter_mb_edgeh( h, &img_y[4*3*linesize], linesize, bS3, qp );
  5946. }
  5947. filter_mb_edgecv( h, &img_cb[2*0], uvlinesize, bS4, qpc0 );
  5948. filter_mb_edgecv( h, &img_cb[2*2], uvlinesize, bS3, qpc );
  5949. filter_mb_edgecv( h, &img_cr[2*0], uvlinesize, bS4, qpc0 );
  5950. filter_mb_edgecv( h, &img_cr[2*2], uvlinesize, bS3, qpc );
  5951. filter_mb_edgech( h, &img_cb[2*0*uvlinesize], uvlinesize, bS4, qpc1 );
  5952. filter_mb_edgech( h, &img_cb[2*2*uvlinesize], uvlinesize, bS3, qpc );
  5953. filter_mb_edgech( h, &img_cr[2*0*uvlinesize], uvlinesize, bS4, qpc1 );
  5954. filter_mb_edgech( h, &img_cr[2*2*uvlinesize], uvlinesize, bS3, qpc );
  5955. return;
  5956. } else {
  5957. DECLARE_ALIGNED_8(int16_t, bS[2][4][4]);
  5958. uint64_t (*bSv)[4] = (uint64_t(*)[4])bS;
  5959. int edges;
  5960. if( IS_8x8DCT(mb_type) && (h->cbp&7) == 7 ) {
  5961. edges = 4;
  5962. bSv[0][0] = bSv[0][2] = bSv[1][0] = bSv[1][2] = 0x0002000200020002ULL;
  5963. } else {
  5964. int mask_edge1 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16)) ? 3 :
  5965. (mb_type & MB_TYPE_16x8) ? 1 : 0;
  5966. int mask_edge0 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16))
  5967. && (s->current_picture.mb_type[mb_xy-1] & (MB_TYPE_16x16 | MB_TYPE_8x16))
  5968. ? 3 : 0;
  5969. int step = IS_8x8DCT(mb_type) ? 2 : 1;
  5970. edges = (mb_type & MB_TYPE_16x16) && !(h->cbp & 15) ? 1 : 4;
  5971. s->dsp.h264_loop_filter_strength( bS, h->non_zero_count_cache, h->ref_cache, h->mv_cache,
  5972. (h->slice_type == B_TYPE), edges, step, mask_edge0, mask_edge1 );
  5973. }
  5974. if( IS_INTRA(s->current_picture.mb_type[mb_xy-1]) )
  5975. bSv[0][0] = 0x0004000400040004ULL;
  5976. if( IS_INTRA(s->current_picture.mb_type[h->top_mb_xy]) )
  5977. bSv[1][0] = 0x0004000400040004ULL;
  5978. #define FILTER(hv,dir,edge)\
  5979. if(bSv[dir][edge]) {\
  5980. filter_mb_edge##hv( h, &img_y[4*edge*(dir?linesize:1)], linesize, bS[dir][edge], edge ? qp : qp##dir );\
  5981. if(!(edge&1)) {\
  5982. filter_mb_edgec##hv( h, &img_cb[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
  5983. filter_mb_edgec##hv( h, &img_cr[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
  5984. }\
  5985. }
  5986. if( edges == 1 ) {
  5987. FILTER(v,0,0);
  5988. FILTER(h,1,0);
  5989. } else if( IS_8x8DCT(mb_type) ) {
  5990. FILTER(v,0,0);
  5991. FILTER(v,0,2);
  5992. FILTER(h,1,0);
  5993. FILTER(h,1,2);
  5994. } else {
  5995. FILTER(v,0,0);
  5996. FILTER(v,0,1);
  5997. FILTER(v,0,2);
  5998. FILTER(v,0,3);
  5999. FILTER(h,1,0);
  6000. FILTER(h,1,1);
  6001. FILTER(h,1,2);
  6002. FILTER(h,1,3);
  6003. }
  6004. #undef FILTER
  6005. }
  6006. }
  6007. 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) {
  6008. MpegEncContext * const s = &h->s;
  6009. const int mb_xy= mb_x + mb_y*s->mb_stride;
  6010. const int mb_type = s->current_picture.mb_type[mb_xy];
  6011. const int mvy_limit = IS_INTERLACED(mb_type) ? 2 : 4;
  6012. int first_vertical_edge_done = 0;
  6013. int dir;
  6014. /* FIXME: A given frame may occupy more than one position in
  6015. * the reference list. So ref2frm should be populated with
  6016. * frame numbers, not indices. */
  6017. static const int ref2frm[34] = {-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
  6018. 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
  6019. //for sufficiently low qp, filtering wouldn't do anything
  6020. //this is a conservative estimate: could also check beta_offset and more accurate chroma_qp
  6021. if(!FRAME_MBAFF){
  6022. int qp_thresh = 15 - h->slice_alpha_c0_offset - FFMAX(0, h->pps.chroma_qp_index_offset);
  6023. int qp = s->current_picture.qscale_table[mb_xy];
  6024. if(qp <= qp_thresh
  6025. && (mb_x == 0 || ((qp + s->current_picture.qscale_table[mb_xy-1] + 1)>>1) <= qp_thresh)
  6026. && (mb_y == 0 || ((qp + s->current_picture.qscale_table[h->top_mb_xy] + 1)>>1) <= qp_thresh)){
  6027. return;
  6028. }
  6029. }
  6030. if (FRAME_MBAFF
  6031. // left mb is in picture
  6032. && h->slice_table[mb_xy-1] != 255
  6033. // and current and left pair do not have the same interlaced type
  6034. && (IS_INTERLACED(mb_type) != IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]))
  6035. // and left mb is in the same slice if deblocking_filter == 2
  6036. && (h->deblocking_filter!=2 || h->slice_table[mb_xy-1] == h->slice_table[mb_xy])) {
  6037. /* First vertical edge is different in MBAFF frames
  6038. * There are 8 different bS to compute and 2 different Qp
  6039. */
  6040. const int pair_xy = mb_x + (mb_y&~1)*s->mb_stride;
  6041. const int left_mb_xy[2] = { pair_xy-1, pair_xy-1+s->mb_stride };
  6042. int16_t bS[8];
  6043. int qp[2];
  6044. int chroma_qp[2];
  6045. int mb_qp, mbn0_qp, mbn1_qp;
  6046. int i;
  6047. first_vertical_edge_done = 1;
  6048. if( IS_INTRA(mb_type) )
  6049. bS[0] = bS[1] = bS[2] = bS[3] = bS[4] = bS[5] = bS[6] = bS[7] = 4;
  6050. else {
  6051. for( i = 0; i < 8; i++ ) {
  6052. int mbn_xy = MB_FIELD ? left_mb_xy[i>>2] : left_mb_xy[i&1];
  6053. if( IS_INTRA( s->current_picture.mb_type[mbn_xy] ) )
  6054. bS[i] = 4;
  6055. else if( h->non_zero_count_cache[12+8*(i>>1)] != 0 ||
  6056. /* FIXME: with 8x8dct + cavlc, should check cbp instead of nnz */
  6057. h->non_zero_count[mbn_xy][MB_FIELD ? i&3 : (i>>2)+(mb_y&1)*2] )
  6058. bS[i] = 2;
  6059. else
  6060. bS[i] = 1;
  6061. }
  6062. }
  6063. mb_qp = s->current_picture.qscale_table[mb_xy];
  6064. mbn0_qp = s->current_picture.qscale_table[left_mb_xy[0]];
  6065. mbn1_qp = s->current_picture.qscale_table[left_mb_xy[1]];
  6066. qp[0] = ( mb_qp + mbn0_qp + 1 ) >> 1;
  6067. chroma_qp[0] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, mb_qp ) +
  6068. get_chroma_qp( h->pps.chroma_qp_index_offset, mbn0_qp ) + 1 ) >> 1;
  6069. qp[1] = ( mb_qp + mbn1_qp + 1 ) >> 1;
  6070. chroma_qp[1] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, mb_qp ) +
  6071. get_chroma_qp( h->pps.chroma_qp_index_offset, mbn1_qp ) + 1 ) >> 1;
  6072. /* Filter edge */
  6073. 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);
  6074. { int i; for (i = 0; i < 8; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
  6075. filter_mb_mbaff_edgev ( h, &img_y [0], linesize, bS, qp );
  6076. filter_mb_mbaff_edgecv( h, &img_cb[0], uvlinesize, bS, chroma_qp );
  6077. filter_mb_mbaff_edgecv( h, &img_cr[0], uvlinesize, bS, chroma_qp );
  6078. }
  6079. /* dir : 0 -> vertical edge, 1 -> horizontal edge */
  6080. for( dir = 0; dir < 2; dir++ )
  6081. {
  6082. int edge;
  6083. const int mbm_xy = dir == 0 ? mb_xy -1 : h->top_mb_xy;
  6084. const int mbm_type = s->current_picture.mb_type[mbm_xy];
  6085. int start = h->slice_table[mbm_xy] == 255 ? 1 : 0;
  6086. const int edges = (mb_type & (MB_TYPE_16x16|MB_TYPE_SKIP))
  6087. == (MB_TYPE_16x16|MB_TYPE_SKIP) ? 1 : 4;
  6088. // how often to recheck mv-based bS when iterating between edges
  6089. const int mask_edge = (mb_type & (MB_TYPE_16x16 | (MB_TYPE_16x8 << dir))) ? 3 :
  6090. (mb_type & (MB_TYPE_8x16 >> dir)) ? 1 : 0;
  6091. // how often to recheck mv-based bS when iterating along each edge
  6092. const int mask_par0 = mb_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir));
  6093. if (first_vertical_edge_done) {
  6094. start = 1;
  6095. first_vertical_edge_done = 0;
  6096. }
  6097. if (h->deblocking_filter==2 && h->slice_table[mbm_xy] != h->slice_table[mb_xy])
  6098. start = 1;
  6099. if (FRAME_MBAFF && (dir == 1) && ((mb_y&1) == 0) && start == 0
  6100. && !IS_INTERLACED(mb_type)
  6101. && IS_INTERLACED(mbm_type)
  6102. ) {
  6103. // This is a special case in the norm where the filtering must
  6104. // be done twice (one each of the field) even if we are in a
  6105. // frame macroblock.
  6106. //
  6107. static const int nnz_idx[4] = {4,5,6,3};
  6108. unsigned int tmp_linesize = 2 * linesize;
  6109. unsigned int tmp_uvlinesize = 2 * uvlinesize;
  6110. int mbn_xy = mb_xy - 2 * s->mb_stride;
  6111. int qp, chroma_qp;
  6112. int i, j;
  6113. int16_t bS[4];
  6114. for(j=0; j<2; j++, mbn_xy += s->mb_stride){
  6115. if( IS_INTRA(mb_type) ||
  6116. IS_INTRA(s->current_picture.mb_type[mbn_xy]) ) {
  6117. bS[0] = bS[1] = bS[2] = bS[3] = 3;
  6118. } else {
  6119. const uint8_t *mbn_nnz = h->non_zero_count[mbn_xy];
  6120. for( i = 0; i < 4; i++ ) {
  6121. if( h->non_zero_count_cache[scan8[0]+i] != 0 ||
  6122. mbn_nnz[nnz_idx[i]] != 0 )
  6123. bS[i] = 2;
  6124. else
  6125. bS[i] = 1;
  6126. }
  6127. }
  6128. // Do not use s->qscale as luma quantizer because it has not the same
  6129. // value in IPCM macroblocks.
  6130. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  6131. 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);
  6132. { int i; for (i = 0; i < 4; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
  6133. filter_mb_edgeh( h, &img_y[j*linesize], tmp_linesize, bS, qp );
  6134. chroma_qp = ( h->chroma_qp +
  6135. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6136. filter_mb_edgech( h, &img_cb[j*uvlinesize], tmp_uvlinesize, bS, chroma_qp );
  6137. filter_mb_edgech( h, &img_cr[j*uvlinesize], tmp_uvlinesize, bS, chroma_qp );
  6138. }
  6139. start = 1;
  6140. }
  6141. /* Calculate bS */
  6142. for( edge = start; edge < edges; edge++ ) {
  6143. /* mbn_xy: neighbor macroblock */
  6144. const int mbn_xy = edge > 0 ? mb_xy : mbm_xy;
  6145. const int mbn_type = s->current_picture.mb_type[mbn_xy];
  6146. int16_t bS[4];
  6147. int qp;
  6148. if( (edge&1) && IS_8x8DCT(mb_type) )
  6149. continue;
  6150. if( IS_INTRA(mb_type) ||
  6151. IS_INTRA(mbn_type) ) {
  6152. int value;
  6153. if (edge == 0) {
  6154. if ( (!IS_INTERLACED(mb_type) && !IS_INTERLACED(mbm_type))
  6155. || ((FRAME_MBAFF || (s->picture_structure != PICT_FRAME)) && (dir == 0))
  6156. ) {
  6157. value = 4;
  6158. } else {
  6159. value = 3;
  6160. }
  6161. } else {
  6162. value = 3;
  6163. }
  6164. bS[0] = bS[1] = bS[2] = bS[3] = value;
  6165. } else {
  6166. int i, l;
  6167. int mv_done;
  6168. if( edge & mask_edge ) {
  6169. bS[0] = bS[1] = bS[2] = bS[3] = 0;
  6170. mv_done = 1;
  6171. }
  6172. else if( FRAME_MBAFF && IS_INTERLACED(mb_type ^ mbn_type)) {
  6173. bS[0] = bS[1] = bS[2] = bS[3] = 1;
  6174. mv_done = 1;
  6175. }
  6176. else if( mask_par0 && (edge || (mbn_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir)))) ) {
  6177. int b_idx= 8 + 4 + edge * (dir ? 8:1);
  6178. int bn_idx= b_idx - (dir ? 8:1);
  6179. int v = 0;
  6180. for( l = 0; !v && l < 1 + (h->slice_type == B_TYPE); l++ ) {
  6181. v |= ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  6182. FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  6183. FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit;
  6184. }
  6185. bS[0] = bS[1] = bS[2] = bS[3] = v;
  6186. mv_done = 1;
  6187. }
  6188. else
  6189. mv_done = 0;
  6190. for( i = 0; i < 4; i++ ) {
  6191. int x = dir == 0 ? edge : i;
  6192. int y = dir == 0 ? i : edge;
  6193. int b_idx= 8 + 4 + x + 8*y;
  6194. int bn_idx= b_idx - (dir ? 8:1);
  6195. if( h->non_zero_count_cache[b_idx] != 0 ||
  6196. h->non_zero_count_cache[bn_idx] != 0 ) {
  6197. bS[i] = 2;
  6198. }
  6199. else if(!mv_done)
  6200. {
  6201. bS[i] = 0;
  6202. for( l = 0; l < 1 + (h->slice_type == B_TYPE); l++ ) {
  6203. if( ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
  6204. FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
  6205. FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit ) {
  6206. bS[i] = 1;
  6207. break;
  6208. }
  6209. }
  6210. }
  6211. }
  6212. if(bS[0]+bS[1]+bS[2]+bS[3] == 0)
  6213. continue;
  6214. }
  6215. /* Filter edge */
  6216. // Do not use s->qscale as luma quantizer because it has not the same
  6217. // value in IPCM macroblocks.
  6218. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  6219. //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]);
  6220. 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);
  6221. { int i; for (i = 0; i < 4; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
  6222. if( dir == 0 ) {
  6223. filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp );
  6224. if( (edge&1) == 0 ) {
  6225. int chroma_qp = ( h->chroma_qp +
  6226. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6227. filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS, chroma_qp );
  6228. filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS, chroma_qp );
  6229. }
  6230. } else {
  6231. filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp );
  6232. if( (edge&1) == 0 ) {
  6233. int chroma_qp = ( h->chroma_qp +
  6234. get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
  6235. filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  6236. filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
  6237. }
  6238. }
  6239. }
  6240. }
  6241. }
  6242. static int decode_slice(H264Context *h){
  6243. MpegEncContext * const s = &h->s;
  6244. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  6245. s->mb_skip_run= -1;
  6246. if( h->pps.cabac ) {
  6247. int i;
  6248. /* realign */
  6249. align_get_bits( &s->gb );
  6250. /* init cabac */
  6251. ff_init_cabac_states( &h->cabac);
  6252. ff_init_cabac_decoder( &h->cabac,
  6253. s->gb.buffer + get_bits_count(&s->gb)/8,
  6254. ( s->gb.size_in_bits - get_bits_count(&s->gb) + 7)/8);
  6255. /* calculate pre-state */
  6256. for( i= 0; i < 460; i++ ) {
  6257. int pre;
  6258. if( h->slice_type == I_TYPE )
  6259. pre = av_clip( ((cabac_context_init_I[i][0] * s->qscale) >>4 ) + cabac_context_init_I[i][1], 1, 126 );
  6260. else
  6261. 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 );
  6262. if( pre <= 63 )
  6263. h->cabac_state[i] = 2 * ( 63 - pre ) + 0;
  6264. else
  6265. h->cabac_state[i] = 2 * ( pre - 64 ) + 1;
  6266. }
  6267. for(;;){
  6268. //START_TIMER
  6269. int ret = decode_mb_cabac(h);
  6270. int eos;
  6271. //STOP_TIMER("decode_mb_cabac")
  6272. if(ret>=0) hl_decode_mb(h);
  6273. if( ret >= 0 && FRAME_MBAFF ) { //FIXME optimal? or let mb_decode decode 16x32 ?
  6274. s->mb_y++;
  6275. if(ret>=0) ret = decode_mb_cabac(h);
  6276. if(ret>=0) hl_decode_mb(h);
  6277. s->mb_y--;
  6278. }
  6279. eos = get_cabac_terminate( &h->cabac );
  6280. if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
  6281. 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);
  6282. 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);
  6283. return -1;
  6284. }
  6285. if( ++s->mb_x >= s->mb_width ) {
  6286. s->mb_x = 0;
  6287. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6288. ++s->mb_y;
  6289. if(FRAME_MBAFF) {
  6290. ++s->mb_y;
  6291. }
  6292. }
  6293. if( eos || s->mb_y >= s->mb_height ) {
  6294. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6295. 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);
  6296. return 0;
  6297. }
  6298. }
  6299. } else {
  6300. for(;;){
  6301. int ret = decode_mb_cavlc(h);
  6302. if(ret>=0) hl_decode_mb(h);
  6303. if(ret>=0 && FRAME_MBAFF){ //FIXME optimal? or let mb_decode decode 16x32 ?
  6304. s->mb_y++;
  6305. ret = decode_mb_cavlc(h);
  6306. if(ret>=0) hl_decode_mb(h);
  6307. s->mb_y--;
  6308. }
  6309. if(ret<0){
  6310. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6311. 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);
  6312. return -1;
  6313. }
  6314. if(++s->mb_x >= s->mb_width){
  6315. s->mb_x=0;
  6316. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6317. ++s->mb_y;
  6318. if(FRAME_MBAFF) {
  6319. ++s->mb_y;
  6320. }
  6321. if(s->mb_y >= s->mb_height){
  6322. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6323. if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
  6324. 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);
  6325. return 0;
  6326. }else{
  6327. 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);
  6328. return -1;
  6329. }
  6330. }
  6331. }
  6332. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  6333. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  6334. if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
  6335. 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);
  6336. return 0;
  6337. }else{
  6338. 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);
  6339. return -1;
  6340. }
  6341. }
  6342. }
  6343. }
  6344. #if 0
  6345. for(;s->mb_y < s->mb_height; s->mb_y++){
  6346. for(;s->mb_x < s->mb_width; s->mb_x++){
  6347. int ret= decode_mb(h);
  6348. hl_decode_mb(h);
  6349. if(ret<0){
  6350. av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  6351. 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);
  6352. return -1;
  6353. }
  6354. if(++s->mb_x >= s->mb_width){
  6355. s->mb_x=0;
  6356. if(++s->mb_y >= s->mb_height){
  6357. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6358. 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);
  6359. return 0;
  6360. }else{
  6361. 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);
  6362. return -1;
  6363. }
  6364. }
  6365. }
  6366. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  6367. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  6368. 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);
  6369. return 0;
  6370. }else{
  6371. 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);
  6372. return -1;
  6373. }
  6374. }
  6375. }
  6376. s->mb_x=0;
  6377. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  6378. }
  6379. #endif
  6380. return -1; //not reached
  6381. }
  6382. static int decode_unregistered_user_data(H264Context *h, int size){
  6383. MpegEncContext * const s = &h->s;
  6384. uint8_t user_data[16+256];
  6385. int e, build, i;
  6386. if(size<16)
  6387. return -1;
  6388. for(i=0; i<sizeof(user_data)-1 && i<size; i++){
  6389. user_data[i]= get_bits(&s->gb, 8);
  6390. }
  6391. user_data[i]= 0;
  6392. e= sscanf(user_data+16, "x264 - core %d"/*%s - H.264/MPEG-4 AVC codec - Copyleft 2005 - http://www.videolan.org/x264.html*/, &build);
  6393. if(e==1 && build>=0)
  6394. h->x264_build= build;
  6395. if(s->avctx->debug & FF_DEBUG_BUGS)
  6396. av_log(s->avctx, AV_LOG_DEBUG, "user data:\"%s\"\n", user_data+16);
  6397. for(; i<size; i++)
  6398. skip_bits(&s->gb, 8);
  6399. return 0;
  6400. }
  6401. static int decode_sei(H264Context *h){
  6402. MpegEncContext * const s = &h->s;
  6403. while(get_bits_count(&s->gb) + 16 < s->gb.size_in_bits){
  6404. int size, type;
  6405. type=0;
  6406. do{
  6407. type+= show_bits(&s->gb, 8);
  6408. }while(get_bits(&s->gb, 8) == 255);
  6409. size=0;
  6410. do{
  6411. size+= show_bits(&s->gb, 8);
  6412. }while(get_bits(&s->gb, 8) == 255);
  6413. switch(type){
  6414. case 5:
  6415. if(decode_unregistered_user_data(h, size) < 0)
  6416. return -1;
  6417. break;
  6418. default:
  6419. skip_bits(&s->gb, 8*size);
  6420. }
  6421. //FIXME check bits here
  6422. align_get_bits(&s->gb);
  6423. }
  6424. return 0;
  6425. }
  6426. static inline void decode_hrd_parameters(H264Context *h, SPS *sps){
  6427. MpegEncContext * const s = &h->s;
  6428. int cpb_count, i;
  6429. cpb_count = get_ue_golomb(&s->gb) + 1;
  6430. get_bits(&s->gb, 4); /* bit_rate_scale */
  6431. get_bits(&s->gb, 4); /* cpb_size_scale */
  6432. for(i=0; i<cpb_count; i++){
  6433. get_ue_golomb(&s->gb); /* bit_rate_value_minus1 */
  6434. get_ue_golomb(&s->gb); /* cpb_size_value_minus1 */
  6435. get_bits1(&s->gb); /* cbr_flag */
  6436. }
  6437. get_bits(&s->gb, 5); /* initial_cpb_removal_delay_length_minus1 */
  6438. get_bits(&s->gb, 5); /* cpb_removal_delay_length_minus1 */
  6439. get_bits(&s->gb, 5); /* dpb_output_delay_length_minus1 */
  6440. get_bits(&s->gb, 5); /* time_offset_length */
  6441. }
  6442. static inline int decode_vui_parameters(H264Context *h, SPS *sps){
  6443. MpegEncContext * const s = &h->s;
  6444. int aspect_ratio_info_present_flag;
  6445. unsigned int aspect_ratio_idc;
  6446. int nal_hrd_parameters_present_flag, vcl_hrd_parameters_present_flag;
  6447. aspect_ratio_info_present_flag= get_bits1(&s->gb);
  6448. if( aspect_ratio_info_present_flag ) {
  6449. aspect_ratio_idc= get_bits(&s->gb, 8);
  6450. if( aspect_ratio_idc == EXTENDED_SAR ) {
  6451. sps->sar.num= get_bits(&s->gb, 16);
  6452. sps->sar.den= get_bits(&s->gb, 16);
  6453. }else if(aspect_ratio_idc < 14){
  6454. sps->sar= pixel_aspect[aspect_ratio_idc];
  6455. }else{
  6456. av_log(h->s.avctx, AV_LOG_ERROR, "illegal aspect ratio\n");
  6457. return -1;
  6458. }
  6459. }else{
  6460. sps->sar.num=
  6461. sps->sar.den= 0;
  6462. }
  6463. // s->avctx->aspect_ratio= sar_width*s->width / (float)(s->height*sar_height);
  6464. if(get_bits1(&s->gb)){ /* overscan_info_present_flag */
  6465. get_bits1(&s->gb); /* overscan_appropriate_flag */
  6466. }
  6467. if(get_bits1(&s->gb)){ /* video_signal_type_present_flag */
  6468. get_bits(&s->gb, 3); /* video_format */
  6469. get_bits1(&s->gb); /* video_full_range_flag */
  6470. if(get_bits1(&s->gb)){ /* colour_description_present_flag */
  6471. get_bits(&s->gb, 8); /* colour_primaries */
  6472. get_bits(&s->gb, 8); /* transfer_characteristics */
  6473. get_bits(&s->gb, 8); /* matrix_coefficients */
  6474. }
  6475. }
  6476. if(get_bits1(&s->gb)){ /* chroma_location_info_present_flag */
  6477. get_ue_golomb(&s->gb); /* chroma_sample_location_type_top_field */
  6478. get_ue_golomb(&s->gb); /* chroma_sample_location_type_bottom_field */
  6479. }
  6480. sps->timing_info_present_flag = get_bits1(&s->gb);
  6481. if(sps->timing_info_present_flag){
  6482. sps->num_units_in_tick = get_bits_long(&s->gb, 32);
  6483. sps->time_scale = get_bits_long(&s->gb, 32);
  6484. sps->fixed_frame_rate_flag = get_bits1(&s->gb);
  6485. }
  6486. nal_hrd_parameters_present_flag = get_bits1(&s->gb);
  6487. if(nal_hrd_parameters_present_flag)
  6488. decode_hrd_parameters(h, sps);
  6489. vcl_hrd_parameters_present_flag = get_bits1(&s->gb);
  6490. if(vcl_hrd_parameters_present_flag)
  6491. decode_hrd_parameters(h, sps);
  6492. if(nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag)
  6493. get_bits1(&s->gb); /* low_delay_hrd_flag */
  6494. get_bits1(&s->gb); /* pic_struct_present_flag */
  6495. sps->bitstream_restriction_flag = get_bits1(&s->gb);
  6496. if(sps->bitstream_restriction_flag){
  6497. unsigned int num_reorder_frames;
  6498. get_bits1(&s->gb); /* motion_vectors_over_pic_boundaries_flag */
  6499. get_ue_golomb(&s->gb); /* max_bytes_per_pic_denom */
  6500. get_ue_golomb(&s->gb); /* max_bits_per_mb_denom */
  6501. get_ue_golomb(&s->gb); /* log2_max_mv_length_horizontal */
  6502. get_ue_golomb(&s->gb); /* log2_max_mv_length_vertical */
  6503. num_reorder_frames= get_ue_golomb(&s->gb);
  6504. get_ue_golomb(&s->gb); /*max_dec_frame_buffering*/
  6505. if(num_reorder_frames > 16 /*max_dec_frame_buffering || max_dec_frame_buffering > 16*/){
  6506. av_log(h->s.avctx, AV_LOG_ERROR, "illegal num_reorder_frames %d\n", num_reorder_frames);
  6507. return -1;
  6508. }
  6509. sps->num_reorder_frames= num_reorder_frames;
  6510. }
  6511. return 0;
  6512. }
  6513. static void decode_scaling_list(H264Context *h, uint8_t *factors, int size,
  6514. const uint8_t *jvt_list, const uint8_t *fallback_list){
  6515. MpegEncContext * const s = &h->s;
  6516. int i, last = 8, next = 8;
  6517. const uint8_t *scan = size == 16 ? zigzag_scan : zigzag_scan8x8;
  6518. if(!get_bits1(&s->gb)) /* matrix not written, we use the predicted one */
  6519. memcpy(factors, fallback_list, size*sizeof(uint8_t));
  6520. else
  6521. for(i=0;i<size;i++){
  6522. if(next)
  6523. next = (last + get_se_golomb(&s->gb)) & 0xff;
  6524. if(!i && !next){ /* matrix not written, we use the preset one */
  6525. memcpy(factors, jvt_list, size*sizeof(uint8_t));
  6526. break;
  6527. }
  6528. last = factors[scan[i]] = next ? next : last;
  6529. }
  6530. }
  6531. static void decode_scaling_matrices(H264Context *h, SPS *sps, PPS *pps, int is_sps,
  6532. uint8_t (*scaling_matrix4)[16], uint8_t (*scaling_matrix8)[64]){
  6533. MpegEncContext * const s = &h->s;
  6534. int fallback_sps = !is_sps && sps->scaling_matrix_present;
  6535. const uint8_t *fallback[4] = {
  6536. fallback_sps ? sps->scaling_matrix4[0] : default_scaling4[0],
  6537. fallback_sps ? sps->scaling_matrix4[3] : default_scaling4[1],
  6538. fallback_sps ? sps->scaling_matrix8[0] : default_scaling8[0],
  6539. fallback_sps ? sps->scaling_matrix8[1] : default_scaling8[1]
  6540. };
  6541. if(get_bits1(&s->gb)){
  6542. sps->scaling_matrix_present |= is_sps;
  6543. decode_scaling_list(h,scaling_matrix4[0],16,default_scaling4[0],fallback[0]); // Intra, Y
  6544. decode_scaling_list(h,scaling_matrix4[1],16,default_scaling4[0],scaling_matrix4[0]); // Intra, Cr
  6545. decode_scaling_list(h,scaling_matrix4[2],16,default_scaling4[0],scaling_matrix4[1]); // Intra, Cb
  6546. decode_scaling_list(h,scaling_matrix4[3],16,default_scaling4[1],fallback[1]); // Inter, Y
  6547. decode_scaling_list(h,scaling_matrix4[4],16,default_scaling4[1],scaling_matrix4[3]); // Inter, Cr
  6548. decode_scaling_list(h,scaling_matrix4[5],16,default_scaling4[1],scaling_matrix4[4]); // Inter, Cb
  6549. if(is_sps || pps->transform_8x8_mode){
  6550. decode_scaling_list(h,scaling_matrix8[0],64,default_scaling8[0],fallback[2]); // Intra, Y
  6551. decode_scaling_list(h,scaling_matrix8[1],64,default_scaling8[1],fallback[3]); // Inter, Y
  6552. }
  6553. } else if(fallback_sps) {
  6554. memcpy(scaling_matrix4, sps->scaling_matrix4, 6*16*sizeof(uint8_t));
  6555. memcpy(scaling_matrix8, sps->scaling_matrix8, 2*64*sizeof(uint8_t));
  6556. }
  6557. }
  6558. static inline int decode_seq_parameter_set(H264Context *h){
  6559. MpegEncContext * const s = &h->s;
  6560. int profile_idc, level_idc;
  6561. unsigned int sps_id, tmp, mb_width, mb_height;
  6562. int i;
  6563. SPS *sps;
  6564. profile_idc= get_bits(&s->gb, 8);
  6565. get_bits1(&s->gb); //constraint_set0_flag
  6566. get_bits1(&s->gb); //constraint_set1_flag
  6567. get_bits1(&s->gb); //constraint_set2_flag
  6568. get_bits1(&s->gb); //constraint_set3_flag
  6569. get_bits(&s->gb, 4); // reserved
  6570. level_idc= get_bits(&s->gb, 8);
  6571. sps_id= get_ue_golomb(&s->gb);
  6572. if (sps_id >= MAX_SPS_COUNT){
  6573. // ok it has gone out of hand, someone is sending us bad stuff.
  6574. av_log(h->s.avctx, AV_LOG_ERROR, "illegal sps_id (%d)\n", sps_id);
  6575. return -1;
  6576. }
  6577. sps= &h->sps_buffer[ sps_id ];
  6578. sps->profile_idc= profile_idc;
  6579. sps->level_idc= level_idc;
  6580. if(sps->profile_idc >= 100){ //high profile
  6581. if(get_ue_golomb(&s->gb) == 3) //chroma_format_idc
  6582. get_bits1(&s->gb); //residual_color_transform_flag
  6583. get_ue_golomb(&s->gb); //bit_depth_luma_minus8
  6584. get_ue_golomb(&s->gb); //bit_depth_chroma_minus8
  6585. sps->transform_bypass = get_bits1(&s->gb);
  6586. decode_scaling_matrices(h, sps, NULL, 1, sps->scaling_matrix4, sps->scaling_matrix8);
  6587. }else
  6588. sps->scaling_matrix_present = 0;
  6589. sps->log2_max_frame_num= get_ue_golomb(&s->gb) + 4;
  6590. sps->poc_type= get_ue_golomb(&s->gb);
  6591. if(sps->poc_type == 0){ //FIXME #define
  6592. sps->log2_max_poc_lsb= get_ue_golomb(&s->gb) + 4;
  6593. } else if(sps->poc_type == 1){//FIXME #define
  6594. sps->delta_pic_order_always_zero_flag= get_bits1(&s->gb);
  6595. sps->offset_for_non_ref_pic= get_se_golomb(&s->gb);
  6596. sps->offset_for_top_to_bottom_field= get_se_golomb(&s->gb);
  6597. tmp= get_ue_golomb(&s->gb);
  6598. if(tmp >= sizeof(sps->offset_for_ref_frame) / sizeof(sps->offset_for_ref_frame[0])){
  6599. av_log(h->s.avctx, AV_LOG_ERROR, "poc_cycle_length overflow %u\n", tmp);
  6600. return -1;
  6601. }
  6602. sps->poc_cycle_length= tmp;
  6603. for(i=0; i<sps->poc_cycle_length; i++)
  6604. sps->offset_for_ref_frame[i]= get_se_golomb(&s->gb);
  6605. }else if(sps->poc_type != 2){
  6606. av_log(h->s.avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
  6607. return -1;
  6608. }
  6609. tmp= get_ue_golomb(&s->gb);
  6610. if(tmp > MAX_PICTURE_COUNT-2){
  6611. av_log(h->s.avctx, AV_LOG_ERROR, "too many reference frames\n");
  6612. }
  6613. sps->ref_frame_count= tmp;
  6614. sps->gaps_in_frame_num_allowed_flag= get_bits1(&s->gb);
  6615. mb_width= get_ue_golomb(&s->gb) + 1;
  6616. mb_height= get_ue_golomb(&s->gb) + 1;
  6617. if(mb_width >= INT_MAX/16 || mb_height >= INT_MAX/16 ||
  6618. avcodec_check_dimensions(NULL, 16*mb_width, 16*mb_height)){
  6619. av_log(h->s.avctx, AV_LOG_ERROR, "mb_width/height overflow\n");
  6620. return -1;
  6621. }
  6622. sps->mb_width = mb_width;
  6623. sps->mb_height= mb_height;
  6624. sps->frame_mbs_only_flag= get_bits1(&s->gb);
  6625. if(!sps->frame_mbs_only_flag)
  6626. sps->mb_aff= get_bits1(&s->gb);
  6627. else
  6628. sps->mb_aff= 0;
  6629. sps->direct_8x8_inference_flag= get_bits1(&s->gb);
  6630. #ifndef ALLOW_INTERLACE
  6631. if(sps->mb_aff)
  6632. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF support not included; enable it at compile-time.\n");
  6633. #endif
  6634. if(!sps->direct_8x8_inference_flag && sps->mb_aff)
  6635. av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF + !direct_8x8_inference is not implemented\n");
  6636. sps->crop= get_bits1(&s->gb);
  6637. if(sps->crop){
  6638. sps->crop_left = get_ue_golomb(&s->gb);
  6639. sps->crop_right = get_ue_golomb(&s->gb);
  6640. sps->crop_top = get_ue_golomb(&s->gb);
  6641. sps->crop_bottom= get_ue_golomb(&s->gb);
  6642. if(sps->crop_left || sps->crop_top){
  6643. av_log(h->s.avctx, AV_LOG_ERROR, "insane cropping not completely supported, this could look slightly wrong ...\n");
  6644. }
  6645. }else{
  6646. sps->crop_left =
  6647. sps->crop_right =
  6648. sps->crop_top =
  6649. sps->crop_bottom= 0;
  6650. }
  6651. sps->vui_parameters_present_flag= get_bits1(&s->gb);
  6652. if( sps->vui_parameters_present_flag )
  6653. decode_vui_parameters(h, sps);
  6654. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  6655. 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",
  6656. sps_id, sps->profile_idc, sps->level_idc,
  6657. sps->poc_type,
  6658. sps->ref_frame_count,
  6659. sps->mb_width, sps->mb_height,
  6660. sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
  6661. sps->direct_8x8_inference_flag ? "8B8" : "",
  6662. sps->crop_left, sps->crop_right,
  6663. sps->crop_top, sps->crop_bottom,
  6664. sps->vui_parameters_present_flag ? "VUI" : ""
  6665. );
  6666. }
  6667. return 0;
  6668. }
  6669. static inline int decode_picture_parameter_set(H264Context *h, int bit_length){
  6670. MpegEncContext * const s = &h->s;
  6671. unsigned int tmp, pps_id= get_ue_golomb(&s->gb);
  6672. PPS *pps;
  6673. if(pps_id>=MAX_PPS_COUNT){
  6674. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  6675. return -1;
  6676. }
  6677. pps = &h->pps_buffer[pps_id];
  6678. tmp= get_ue_golomb(&s->gb);
  6679. if(tmp>=MAX_SPS_COUNT){
  6680. av_log(h->s.avctx, AV_LOG_ERROR, "sps_id out of range\n");
  6681. return -1;
  6682. }
  6683. pps->sps_id= tmp;
  6684. pps->cabac= get_bits1(&s->gb);
  6685. pps->pic_order_present= get_bits1(&s->gb);
  6686. pps->slice_group_count= get_ue_golomb(&s->gb) + 1;
  6687. if(pps->slice_group_count > 1 ){
  6688. pps->mb_slice_group_map_type= get_ue_golomb(&s->gb);
  6689. av_log(h->s.avctx, AV_LOG_ERROR, "FMO not supported\n");
  6690. switch(pps->mb_slice_group_map_type){
  6691. case 0:
  6692. #if 0
  6693. | for( i = 0; i <= num_slice_groups_minus1; i++ ) | | |
  6694. | run_length[ i ] |1 |ue(v) |
  6695. #endif
  6696. break;
  6697. case 2:
  6698. #if 0
  6699. | for( i = 0; i < num_slice_groups_minus1; i++ ) | | |
  6700. |{ | | |
  6701. | top_left_mb[ i ] |1 |ue(v) |
  6702. | bottom_right_mb[ i ] |1 |ue(v) |
  6703. | } | | |
  6704. #endif
  6705. break;
  6706. case 3:
  6707. case 4:
  6708. case 5:
  6709. #if 0
  6710. | slice_group_change_direction_flag |1 |u(1) |
  6711. | slice_group_change_rate_minus1 |1 |ue(v) |
  6712. #endif
  6713. break;
  6714. case 6:
  6715. #if 0
  6716. | slice_group_id_cnt_minus1 |1 |ue(v) |
  6717. | for( i = 0; i <= slice_group_id_cnt_minus1; i++ | | |
  6718. |) | | |
  6719. | slice_group_id[ i ] |1 |u(v) |
  6720. #endif
  6721. break;
  6722. }
  6723. }
  6724. pps->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  6725. pps->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  6726. if(pps->ref_count[0]-1 > 32-1 || pps->ref_count[1]-1 > 32-1){
  6727. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
  6728. pps->ref_count[0]= pps->ref_count[1]= 1;
  6729. return -1;
  6730. }
  6731. pps->weighted_pred= get_bits1(&s->gb);
  6732. pps->weighted_bipred_idc= get_bits(&s->gb, 2);
  6733. pps->init_qp= get_se_golomb(&s->gb) + 26;
  6734. pps->init_qs= get_se_golomb(&s->gb) + 26;
  6735. pps->chroma_qp_index_offset= get_se_golomb(&s->gb);
  6736. pps->deblocking_filter_parameters_present= get_bits1(&s->gb);
  6737. pps->constrained_intra_pred= get_bits1(&s->gb);
  6738. pps->redundant_pic_cnt_present = get_bits1(&s->gb);
  6739. pps->transform_8x8_mode= 0;
  6740. h->dequant_coeff_pps= -1; //contents of sps/pps can change even if id doesn't, so reinit
  6741. memset(pps->scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  6742. memset(pps->scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  6743. if(get_bits_count(&s->gb) < bit_length){
  6744. pps->transform_8x8_mode= get_bits1(&s->gb);
  6745. decode_scaling_matrices(h, &h->sps_buffer[pps->sps_id], pps, 0, pps->scaling_matrix4, pps->scaling_matrix8);
  6746. get_se_golomb(&s->gb); //second_chroma_qp_index_offset
  6747. }
  6748. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  6749. 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",
  6750. pps_id, pps->sps_id,
  6751. pps->cabac ? "CABAC" : "CAVLC",
  6752. pps->slice_group_count,
  6753. pps->ref_count[0], pps->ref_count[1],
  6754. pps->weighted_pred ? "weighted" : "",
  6755. pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset,
  6756. pps->deblocking_filter_parameters_present ? "LPAR" : "",
  6757. pps->constrained_intra_pred ? "CONSTR" : "",
  6758. pps->redundant_pic_cnt_present ? "REDU" : "",
  6759. pps->transform_8x8_mode ? "8x8DCT" : ""
  6760. );
  6761. }
  6762. return 0;
  6763. }
  6764. static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){
  6765. MpegEncContext * const s = &h->s;
  6766. AVCodecContext * const avctx= s->avctx;
  6767. int buf_index=0;
  6768. #if 0
  6769. int i;
  6770. for(i=0; i<50; i++){
  6771. av_log(NULL, AV_LOG_ERROR,"%02X ", buf[i]);
  6772. }
  6773. #endif
  6774. if(!(s->flags2 & CODEC_FLAG2_CHUNKS)){
  6775. h->slice_num = 0;
  6776. s->current_picture_ptr= NULL;
  6777. }
  6778. for(;;){
  6779. int consumed;
  6780. int dst_length;
  6781. int bit_length;
  6782. uint8_t *ptr;
  6783. int i, nalsize = 0;
  6784. if(h->is_avc) {
  6785. if(buf_index >= buf_size) break;
  6786. nalsize = 0;
  6787. for(i = 0; i < h->nal_length_size; i++)
  6788. nalsize = (nalsize << 8) | buf[buf_index++];
  6789. if(nalsize <= 1 || (nalsize+buf_index > buf_size)){
  6790. if(nalsize == 1){
  6791. buf_index++;
  6792. continue;
  6793. }else{
  6794. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  6795. break;
  6796. }
  6797. }
  6798. } else {
  6799. // start code prefix search
  6800. for(; buf_index + 3 < buf_size; buf_index++){
  6801. // This should always succeed in the first iteration.
  6802. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  6803. break;
  6804. }
  6805. if(buf_index+3 >= buf_size) break;
  6806. buf_index+=3;
  6807. }
  6808. ptr= decode_nal(h, buf + buf_index, &dst_length, &consumed, h->is_avc ? nalsize : buf_size - buf_index);
  6809. if (ptr==NULL || dst_length < 0){
  6810. return -1;
  6811. }
  6812. while(ptr[dst_length - 1] == 0 && dst_length > 0)
  6813. dst_length--;
  6814. bit_length= !dst_length ? 0 : (8*dst_length - decode_rbsp_trailing(h, ptr + dst_length - 1));
  6815. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  6816. 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);
  6817. }
  6818. if (h->is_avc && (nalsize != consumed))
  6819. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
  6820. buf_index += consumed;
  6821. if( (s->hurry_up == 1 && h->nal_ref_idc == 0) //FIXME dont discard SEI id
  6822. ||(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  6823. continue;
  6824. switch(h->nal_unit_type){
  6825. case NAL_IDR_SLICE:
  6826. idr(h); //FIXME ensure we don't loose some frames if there is reordering
  6827. case NAL_SLICE:
  6828. init_get_bits(&s->gb, ptr, bit_length);
  6829. h->intra_gb_ptr=
  6830. h->inter_gb_ptr= &s->gb;
  6831. s->data_partitioning = 0;
  6832. if(decode_slice_header(h) < 0){
  6833. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  6834. break;
  6835. }
  6836. s->current_picture_ptr->key_frame= (h->nal_unit_type == NAL_IDR_SLICE);
  6837. if(h->redundant_pic_count==0 && s->hurry_up < 5
  6838. && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
  6839. && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
  6840. && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
  6841. && avctx->skip_frame < AVDISCARD_ALL)
  6842. decode_slice(h);
  6843. break;
  6844. case NAL_DPA:
  6845. init_get_bits(&s->gb, ptr, bit_length);
  6846. h->intra_gb_ptr=
  6847. h->inter_gb_ptr= NULL;
  6848. s->data_partitioning = 1;
  6849. if(decode_slice_header(h) < 0){
  6850. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  6851. }
  6852. break;
  6853. case NAL_DPB:
  6854. init_get_bits(&h->intra_gb, ptr, bit_length);
  6855. h->intra_gb_ptr= &h->intra_gb;
  6856. break;
  6857. case NAL_DPC:
  6858. init_get_bits(&h->inter_gb, ptr, bit_length);
  6859. h->inter_gb_ptr= &h->inter_gb;
  6860. if(h->redundant_pic_count==0 && h->intra_gb_ptr && s->data_partitioning
  6861. && s->context_initialized
  6862. && s->hurry_up < 5
  6863. && (avctx->skip_frame < AVDISCARD_NONREF || h->nal_ref_idc)
  6864. && (avctx->skip_frame < AVDISCARD_BIDIR || h->slice_type!=B_TYPE)
  6865. && (avctx->skip_frame < AVDISCARD_NONKEY || h->slice_type==I_TYPE)
  6866. && avctx->skip_frame < AVDISCARD_ALL)
  6867. decode_slice(h);
  6868. break;
  6869. case NAL_SEI:
  6870. init_get_bits(&s->gb, ptr, bit_length);
  6871. decode_sei(h);
  6872. break;
  6873. case NAL_SPS:
  6874. init_get_bits(&s->gb, ptr, bit_length);
  6875. decode_seq_parameter_set(h);
  6876. if(s->flags& CODEC_FLAG_LOW_DELAY)
  6877. s->low_delay=1;
  6878. if(avctx->has_b_frames < 2)
  6879. avctx->has_b_frames= !s->low_delay;
  6880. break;
  6881. case NAL_PPS:
  6882. init_get_bits(&s->gb, ptr, bit_length);
  6883. decode_picture_parameter_set(h, bit_length);
  6884. break;
  6885. case NAL_AUD:
  6886. case NAL_END_SEQUENCE:
  6887. case NAL_END_STREAM:
  6888. case NAL_FILLER_DATA:
  6889. case NAL_SPS_EXT:
  6890. case NAL_AUXILIARY_SLICE:
  6891. break;
  6892. default:
  6893. av_log(avctx, AV_LOG_ERROR, "Unknown NAL code: %d\n", h->nal_unit_type);
  6894. }
  6895. }
  6896. return buf_index;
  6897. }
  6898. /**
  6899. * returns the number of bytes consumed for building the current frame
  6900. */
  6901. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  6902. if(s->flags&CODEC_FLAG_TRUNCATED){
  6903. pos -= s->parse_context.last_index;
  6904. if(pos<0) pos=0; // FIXME remove (unneeded?)
  6905. return pos;
  6906. }else{
  6907. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  6908. if(pos+10>buf_size) pos=buf_size; // oops ;)
  6909. return pos;
  6910. }
  6911. }
  6912. static int decode_frame(AVCodecContext *avctx,
  6913. void *data, int *data_size,
  6914. uint8_t *buf, int buf_size)
  6915. {
  6916. H264Context *h = avctx->priv_data;
  6917. MpegEncContext *s = &h->s;
  6918. AVFrame *pict = data;
  6919. int buf_index;
  6920. s->flags= avctx->flags;
  6921. s->flags2= avctx->flags2;
  6922. /* no supplementary picture */
  6923. if (buf_size == 0) {
  6924. Picture *out;
  6925. int i, out_idx;
  6926. //FIXME factorize this with the output code below
  6927. out = h->delayed_pic[0];
  6928. out_idx = 0;
  6929. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame; i++)
  6930. if(h->delayed_pic[i]->poc < out->poc){
  6931. out = h->delayed_pic[i];
  6932. out_idx = i;
  6933. }
  6934. for(i=out_idx; h->delayed_pic[i]; i++)
  6935. h->delayed_pic[i] = h->delayed_pic[i+1];
  6936. if(out){
  6937. *data_size = sizeof(AVFrame);
  6938. *pict= *(AVFrame*)out;
  6939. }
  6940. return 0;
  6941. }
  6942. if(s->flags&CODEC_FLAG_TRUNCATED){
  6943. int next= ff_h264_find_frame_end(h, buf, buf_size);
  6944. if( ff_combine_frame(&s->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0 )
  6945. return buf_size;
  6946. //printf("next:%d buf_size:%d last_index:%d\n", next, buf_size, s->parse_context.last_index);
  6947. }
  6948. if(h->is_avc && !h->got_avcC) {
  6949. int i, cnt, nalsize;
  6950. unsigned char *p = avctx->extradata;
  6951. if(avctx->extradata_size < 7) {
  6952. av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
  6953. return -1;
  6954. }
  6955. if(*p != 1) {
  6956. av_log(avctx, AV_LOG_ERROR, "Unknown avcC version %d\n", *p);
  6957. return -1;
  6958. }
  6959. /* sps and pps in the avcC always have length coded with 2 bytes,
  6960. so put a fake nal_length_size = 2 while parsing them */
  6961. h->nal_length_size = 2;
  6962. // Decode sps from avcC
  6963. cnt = *(p+5) & 0x1f; // Number of sps
  6964. p += 6;
  6965. for (i = 0; i < cnt; i++) {
  6966. nalsize = AV_RB16(p) + 2;
  6967. if(decode_nal_units(h, p, nalsize) < 0) {
  6968. av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
  6969. return -1;
  6970. }
  6971. p += nalsize;
  6972. }
  6973. // Decode pps from avcC
  6974. cnt = *(p++); // Number of pps
  6975. for (i = 0; i < cnt; i++) {
  6976. nalsize = AV_RB16(p) + 2;
  6977. if(decode_nal_units(h, p, nalsize) != nalsize) {
  6978. av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
  6979. return -1;
  6980. }
  6981. p += nalsize;
  6982. }
  6983. // Now store right nal length size, that will be use to parse all other nals
  6984. h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
  6985. // Do not reparse avcC
  6986. h->got_avcC = 1;
  6987. }
  6988. if(avctx->frame_number==0 && !h->is_avc && s->avctx->extradata_size){
  6989. if(decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) < 0)
  6990. return -1;
  6991. }
  6992. buf_index=decode_nal_units(h, buf, buf_size);
  6993. if(buf_index < 0)
  6994. return -1;
  6995. if(!(s->flags2 & CODEC_FLAG2_CHUNKS) && !s->current_picture_ptr){
  6996. av_log(avctx, AV_LOG_ERROR, "no frame!\n");
  6997. return -1;
  6998. }
  6999. if(!(s->flags2 & CODEC_FLAG2_CHUNKS) || (s->mb_y >= s->mb_height && s->mb_height)){
  7000. Picture *out = s->current_picture_ptr;
  7001. Picture *cur = s->current_picture_ptr;
  7002. Picture *prev = h->delayed_output_pic;
  7003. int i, pics, cross_idr, out_of_order, out_idx;
  7004. s->mb_y= 0;
  7005. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
  7006. s->current_picture_ptr->pict_type= s->pict_type;
  7007. h->prev_frame_num_offset= h->frame_num_offset;
  7008. h->prev_frame_num= h->frame_num;
  7009. if(s->current_picture_ptr->reference){
  7010. h->prev_poc_msb= h->poc_msb;
  7011. h->prev_poc_lsb= h->poc_lsb;
  7012. }
  7013. if(s->current_picture_ptr->reference)
  7014. execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  7015. ff_er_frame_end(s);
  7016. MPV_frame_end(s);
  7017. //FIXME do something with unavailable reference frames
  7018. #if 0 //decode order
  7019. *data_size = sizeof(AVFrame);
  7020. #else
  7021. /* Sort B-frames into display order */
  7022. if(h->sps.bitstream_restriction_flag
  7023. && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  7024. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  7025. s->low_delay = 0;
  7026. }
  7027. pics = 0;
  7028. while(h->delayed_pic[pics]) pics++;
  7029. assert(pics+1 < sizeof(h->delayed_pic) / sizeof(h->delayed_pic[0]));
  7030. h->delayed_pic[pics++] = cur;
  7031. if(cur->reference == 0)
  7032. cur->reference = 1;
  7033. cross_idr = 0;
  7034. for(i=0; h->delayed_pic[i]; i++)
  7035. if(h->delayed_pic[i]->key_frame || h->delayed_pic[i]->poc==0)
  7036. cross_idr = 1;
  7037. out = h->delayed_pic[0];
  7038. out_idx = 0;
  7039. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame; i++)
  7040. if(h->delayed_pic[i]->poc < out->poc){
  7041. out = h->delayed_pic[i];
  7042. out_idx = i;
  7043. }
  7044. out_of_order = !cross_idr && prev && out->poc < prev->poc;
  7045. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
  7046. { }
  7047. else if(prev && pics <= s->avctx->has_b_frames)
  7048. out = prev;
  7049. else if((out_of_order && pics-1 == s->avctx->has_b_frames && pics < 15)
  7050. || (s->low_delay &&
  7051. ((!cross_idr && prev && out->poc > prev->poc + 2)
  7052. || cur->pict_type == B_TYPE)))
  7053. {
  7054. s->low_delay = 0;
  7055. s->avctx->has_b_frames++;
  7056. out = prev;
  7057. }
  7058. else if(out_of_order)
  7059. out = prev;
  7060. if(out_of_order || pics > s->avctx->has_b_frames){
  7061. for(i=out_idx; h->delayed_pic[i]; i++)
  7062. h->delayed_pic[i] = h->delayed_pic[i+1];
  7063. }
  7064. if(prev == out)
  7065. *data_size = 0;
  7066. else
  7067. *data_size = sizeof(AVFrame);
  7068. if(prev && prev != out && prev->reference == 1)
  7069. prev->reference = 0;
  7070. h->delayed_output_pic = out;
  7071. #endif
  7072. if(out)
  7073. *pict= *(AVFrame*)out;
  7074. else
  7075. av_log(avctx, AV_LOG_DEBUG, "no picture\n");
  7076. }
  7077. assert(pict->data[0] || !*data_size);
  7078. ff_print_debug_info(s, pict);
  7079. //printf("out %d\n", (int)pict->data[0]);
  7080. #if 0 //?
  7081. /* Return the Picture timestamp as the frame number */
  7082. /* we substract 1 because it is added on utils.c */
  7083. avctx->frame_number = s->picture_number - 1;
  7084. #endif
  7085. return get_consumed_bytes(s, buf_index, buf_size);
  7086. }
  7087. #if 0
  7088. static inline void fill_mb_avail(H264Context *h){
  7089. MpegEncContext * const s = &h->s;
  7090. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  7091. if(s->mb_y){
  7092. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  7093. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  7094. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  7095. }else{
  7096. h->mb_avail[0]=
  7097. h->mb_avail[1]=
  7098. h->mb_avail[2]= 0;
  7099. }
  7100. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  7101. h->mb_avail[4]= 1; //FIXME move out
  7102. h->mb_avail[5]= 0; //FIXME move out
  7103. }
  7104. #endif
  7105. #if 0 //selftest
  7106. #define COUNT 8000
  7107. #define SIZE (COUNT*40)
  7108. int main(){
  7109. int i;
  7110. uint8_t temp[SIZE];
  7111. PutBitContext pb;
  7112. GetBitContext gb;
  7113. // int int_temp[10000];
  7114. DSPContext dsp;
  7115. AVCodecContext avctx;
  7116. dsputil_init(&dsp, &avctx);
  7117. init_put_bits(&pb, temp, SIZE);
  7118. printf("testing unsigned exp golomb\n");
  7119. for(i=0; i<COUNT; i++){
  7120. START_TIMER
  7121. set_ue_golomb(&pb, i);
  7122. STOP_TIMER("set_ue_golomb");
  7123. }
  7124. flush_put_bits(&pb);
  7125. init_get_bits(&gb, temp, 8*SIZE);
  7126. for(i=0; i<COUNT; i++){
  7127. int j, s;
  7128. s= show_bits(&gb, 24);
  7129. START_TIMER
  7130. j= get_ue_golomb(&gb);
  7131. if(j != i){
  7132. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  7133. // return -1;
  7134. }
  7135. STOP_TIMER("get_ue_golomb");
  7136. }
  7137. init_put_bits(&pb, temp, SIZE);
  7138. printf("testing signed exp golomb\n");
  7139. for(i=0; i<COUNT; i++){
  7140. START_TIMER
  7141. set_se_golomb(&pb, i - COUNT/2);
  7142. STOP_TIMER("set_se_golomb");
  7143. }
  7144. flush_put_bits(&pb);
  7145. init_get_bits(&gb, temp, 8*SIZE);
  7146. for(i=0; i<COUNT; i++){
  7147. int j, s;
  7148. s= show_bits(&gb, 24);
  7149. START_TIMER
  7150. j= get_se_golomb(&gb);
  7151. if(j != i - COUNT/2){
  7152. printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  7153. // return -1;
  7154. }
  7155. STOP_TIMER("get_se_golomb");
  7156. }
  7157. printf("testing 4x4 (I)DCT\n");
  7158. DCTELEM block[16];
  7159. uint8_t src[16], ref[16];
  7160. uint64_t error= 0, max_error=0;
  7161. for(i=0; i<COUNT; i++){
  7162. int j;
  7163. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  7164. for(j=0; j<16; j++){
  7165. ref[j]= random()%255;
  7166. src[j]= random()%255;
  7167. }
  7168. h264_diff_dct_c(block, src, ref, 4);
  7169. //normalize
  7170. for(j=0; j<16; j++){
  7171. // printf("%d ", block[j]);
  7172. block[j]= block[j]*4;
  7173. if(j&1) block[j]= (block[j]*4 + 2)/5;
  7174. if(j&4) block[j]= (block[j]*4 + 2)/5;
  7175. }
  7176. // printf("\n");
  7177. s->dsp.h264_idct_add(ref, block, 4);
  7178. /* for(j=0; j<16; j++){
  7179. printf("%d ", ref[j]);
  7180. }
  7181. printf("\n");*/
  7182. for(j=0; j<16; j++){
  7183. int diff= FFABS(src[j] - ref[j]);
  7184. error+= diff*diff;
  7185. max_error= FFMAX(max_error, diff);
  7186. }
  7187. }
  7188. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  7189. #if 0
  7190. printf("testing quantizer\n");
  7191. for(qp=0; qp<52; qp++){
  7192. for(i=0; i<16; i++)
  7193. src1_block[i]= src2_block[i]= random()%255;
  7194. }
  7195. #endif
  7196. printf("Testing NAL layer\n");
  7197. uint8_t bitstream[COUNT];
  7198. uint8_t nal[COUNT*2];
  7199. H264Context h;
  7200. memset(&h, 0, sizeof(H264Context));
  7201. for(i=0; i<COUNT; i++){
  7202. int zeros= i;
  7203. int nal_length;
  7204. int consumed;
  7205. int out_length;
  7206. uint8_t *out;
  7207. int j;
  7208. for(j=0; j<COUNT; j++){
  7209. bitstream[j]= (random() % 255) + 1;
  7210. }
  7211. for(j=0; j<zeros; j++){
  7212. int pos= random() % COUNT;
  7213. while(bitstream[pos] == 0){
  7214. pos++;
  7215. pos %= COUNT;
  7216. }
  7217. bitstream[pos]=0;
  7218. }
  7219. START_TIMER
  7220. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  7221. if(nal_length<0){
  7222. printf("encoding failed\n");
  7223. return -1;
  7224. }
  7225. out= decode_nal(&h, nal, &out_length, &consumed, nal_length);
  7226. STOP_TIMER("NAL")
  7227. if(out_length != COUNT){
  7228. printf("incorrect length %d %d\n", out_length, COUNT);
  7229. return -1;
  7230. }
  7231. if(consumed != nal_length){
  7232. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  7233. return -1;
  7234. }
  7235. if(memcmp(bitstream, out, COUNT)){
  7236. printf("missmatch\n");
  7237. return -1;
  7238. }
  7239. }
  7240. printf("Testing RBSP\n");
  7241. return 0;
  7242. }
  7243. #endif
  7244. static int decode_end(AVCodecContext *avctx)
  7245. {
  7246. H264Context *h = avctx->priv_data;
  7247. MpegEncContext *s = &h->s;
  7248. av_freep(&h->rbsp_buffer);
  7249. free_tables(h); //FIXME cleanup init stuff perhaps
  7250. MPV_common_end(s);
  7251. // memset(h, 0, sizeof(H264Context));
  7252. return 0;
  7253. }
  7254. AVCodec h264_decoder = {
  7255. "h264",
  7256. CODEC_TYPE_VIDEO,
  7257. CODEC_ID_H264,
  7258. sizeof(H264Context),
  7259. decode_init,
  7260. NULL,
  7261. decode_end,
  7262. decode_frame,
  7263. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  7264. .flush= flush_dpb,
  7265. };
  7266. #include "svq3.c"