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.

7728 lines
296KB

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