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.

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