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.

8277 lines
313KB

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