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.

7960 lines
307KB

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