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.

7724 lines
295KB

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