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.

5719 lines
216KB

  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 libavcodec/h264.c
  23. * H.264 / AVC / MPEG4 part10 codec.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include "internal.h"
  27. #include "dsputil.h"
  28. #include "avcodec.h"
  29. #include "mpegvideo.h"
  30. #include "h264.h"
  31. #include "h264data.h"
  32. #include "h264_parser.h"
  33. #include "golomb.h"
  34. #include "mathops.h"
  35. #include "rectangle.h"
  36. #include "vdpau_internal.h"
  37. #include "cabac.h"
  38. #if ARCH_X86
  39. #include "x86/h264_i386.h"
  40. #endif
  41. //#undef NDEBUG
  42. #include <assert.h>
  43. static VLC coeff_token_vlc[4];
  44. static VLC_TYPE coeff_token_vlc_tables[520+332+280+256][2];
  45. static const int coeff_token_vlc_tables_size[4]={520,332,280,256};
  46. static VLC chroma_dc_coeff_token_vlc;
  47. static VLC_TYPE chroma_dc_coeff_token_vlc_table[256][2];
  48. static const int chroma_dc_coeff_token_vlc_table_size = 256;
  49. static VLC total_zeros_vlc[15];
  50. static VLC_TYPE total_zeros_vlc_tables[15][512][2];
  51. static const int total_zeros_vlc_tables_size = 512;
  52. static VLC chroma_dc_total_zeros_vlc[3];
  53. static VLC_TYPE chroma_dc_total_zeros_vlc_tables[3][8][2];
  54. static const int chroma_dc_total_zeros_vlc_tables_size = 8;
  55. static VLC run_vlc[6];
  56. static VLC_TYPE run_vlc_tables[6][8][2];
  57. static const int run_vlc_tables_size = 8;
  58. static VLC run7_vlc;
  59. static VLC_TYPE run7_vlc_table[96][2];
  60. static const int run7_vlc_table_size = 96;
  61. static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
  62. static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
  63. static const uint8_t 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. static const uint8_t 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 uint8_t 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. #define LEVEL_TAB_BITS 8
  76. static int8_t cavlc_level_tab[7][1<<LEVEL_TAB_BITS][2];
  77. static void fill_caches(H264Context *h, int mb_type, int for_deblock){
  78. MpegEncContext * const s = &h->s;
  79. const int mb_xy= h->mb_xy;
  80. int topleft_xy, top_xy, topright_xy, left_xy[2];
  81. int topleft_type, top_type, topright_type, left_type[2];
  82. const uint8_t * left_block;
  83. int topleft_partition= -1;
  84. int i;
  85. top_xy = mb_xy - (s->mb_stride << FIELD_PICTURE);
  86. //FIXME deblocking could skip the intra and nnz parts.
  87. if(for_deblock && (h->slice_num == 1 || h->slice_table[mb_xy] == h->slice_table[top_xy]) && !FRAME_MBAFF)
  88. return;
  89. /* Wow, what a mess, why didn't they simplify the interlacing & intra
  90. * stuff, I can't imagine that these complex rules are worth it. */
  91. topleft_xy = top_xy - 1;
  92. topright_xy= top_xy + 1;
  93. left_xy[1] = left_xy[0] = mb_xy-1;
  94. left_block = left_block_options[0];
  95. if(FRAME_MBAFF){
  96. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  97. const int top_pair_xy = pair_xy - s->mb_stride;
  98. const int topleft_pair_xy = top_pair_xy - 1;
  99. const int topright_pair_xy = top_pair_xy + 1;
  100. const int topleft_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]);
  101. const int top_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  102. const int topright_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[topright_pair_xy]);
  103. const int left_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  104. const int curr_mb_field_flag = IS_INTERLACED(mb_type);
  105. const int bottom = (s->mb_y & 1);
  106. tprintf(s->avctx, "fill_caches: curr_mb_field_flag:%d, left_mb_field_flag:%d, topleft_mb_field_flag:%d, top_mb_field_flag:%d, topright_mb_field_flag:%d\n", curr_mb_field_flag, left_mb_field_flag, topleft_mb_field_flag, top_mb_field_flag, topright_mb_field_flag);
  107. if (curr_mb_field_flag && (bottom || top_mb_field_flag)){
  108. top_xy -= s->mb_stride;
  109. }
  110. if (curr_mb_field_flag && (bottom || topleft_mb_field_flag)){
  111. topleft_xy -= s->mb_stride;
  112. } else if(bottom && !curr_mb_field_flag && left_mb_field_flag) {
  113. topleft_xy += s->mb_stride;
  114. // take top left mv from the middle of the mb, as opposed to all other modes which use the bottom right partition
  115. topleft_partition = 0;
  116. }
  117. if (curr_mb_field_flag && (bottom || topright_mb_field_flag)){
  118. topright_xy -= s->mb_stride;
  119. }
  120. if (left_mb_field_flag != curr_mb_field_flag) {
  121. left_xy[1] = left_xy[0] = pair_xy - 1;
  122. if (curr_mb_field_flag) {
  123. left_xy[1] += s->mb_stride;
  124. left_block = left_block_options[3];
  125. } else {
  126. left_block= left_block_options[2 - bottom];
  127. }
  128. }
  129. }
  130. h->top_mb_xy = top_xy;
  131. h->left_mb_xy[0] = left_xy[0];
  132. h->left_mb_xy[1] = left_xy[1];
  133. if(for_deblock){
  134. topleft_type = 0;
  135. topright_type = 0;
  136. top_type = h->slice_table[top_xy ] < 0xFFFF ? s->current_picture.mb_type[top_xy] : 0;
  137. left_type[0] = h->slice_table[left_xy[0] ] < 0xFFFF ? s->current_picture.mb_type[left_xy[0]] : 0;
  138. left_type[1] = h->slice_table[left_xy[1] ] < 0xFFFF ? s->current_picture.mb_type[left_xy[1]] : 0;
  139. if(MB_MBAFF && !IS_INTRA(mb_type)){
  140. int list;
  141. for(list=0; list<h->list_count; list++){
  142. //These values where changed for ease of performing MC, we need to change them back
  143. //FIXME maybe we can make MC and loop filter use the same values or prevent
  144. //the MC code from changing ref_cache and rather use a temporary array.
  145. if(USES_LIST(mb_type,list)){
  146. int8_t *ref = &s->current_picture.ref_index[list][h->mb2b8_xy[mb_xy]];
  147. *(uint32_t*)&h->ref_cache[list][scan8[ 0]] =
  148. *(uint32_t*)&h->ref_cache[list][scan8[ 2]] = (pack16to32(ref[0],ref[1])&0x00FF00FF)*0x0101;
  149. ref += h->b8_stride;
  150. *(uint32_t*)&h->ref_cache[list][scan8[ 8]] =
  151. *(uint32_t*)&h->ref_cache[list][scan8[10]] = (pack16to32(ref[0],ref[1])&0x00FF00FF)*0x0101;
  152. }
  153. }
  154. }
  155. }else{
  156. topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;
  157. top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0;
  158. topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;
  159. left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;
  160. left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;
  161. if(IS_INTRA(mb_type)){
  162. int type_mask= h->pps.constrained_intra_pred ? IS_INTRA(-1) : -1;
  163. h->topleft_samples_available=
  164. h->top_samples_available=
  165. h->left_samples_available= 0xFFFF;
  166. h->topright_samples_available= 0xEEEA;
  167. if(!(top_type & type_mask)){
  168. h->topleft_samples_available= 0xB3FF;
  169. h->top_samples_available= 0x33FF;
  170. h->topright_samples_available= 0x26EA;
  171. }
  172. if(IS_INTERLACED(mb_type) != IS_INTERLACED(left_type[0])){
  173. if(IS_INTERLACED(mb_type)){
  174. if(!(left_type[0] & type_mask)){
  175. h->topleft_samples_available&= 0xDFFF;
  176. h->left_samples_available&= 0x5FFF;
  177. }
  178. if(!(left_type[1] & type_mask)){
  179. h->topleft_samples_available&= 0xFF5F;
  180. h->left_samples_available&= 0xFF5F;
  181. }
  182. }else{
  183. int left_typei = h->slice_table[left_xy[0] + s->mb_stride ] == h->slice_num
  184. ? s->current_picture.mb_type[left_xy[0] + s->mb_stride] : 0;
  185. assert(left_xy[0] == left_xy[1]);
  186. if(!((left_typei & type_mask) && (left_type[0] & type_mask))){
  187. h->topleft_samples_available&= 0xDF5F;
  188. h->left_samples_available&= 0x5F5F;
  189. }
  190. }
  191. }else{
  192. if(!(left_type[0] & type_mask)){
  193. h->topleft_samples_available&= 0xDF5F;
  194. h->left_samples_available&= 0x5F5F;
  195. }
  196. }
  197. if(!(topleft_type & type_mask))
  198. h->topleft_samples_available&= 0x7FFF;
  199. if(!(topright_type & type_mask))
  200. h->topright_samples_available&= 0xFBFF;
  201. if(IS_INTRA4x4(mb_type)){
  202. if(IS_INTRA4x4(top_type)){
  203. h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];
  204. h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];
  205. h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];
  206. h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];
  207. }else{
  208. int pred;
  209. if(!(top_type & type_mask))
  210. pred= -1;
  211. else{
  212. pred= 2;
  213. }
  214. h->intra4x4_pred_mode_cache[4+8*0]=
  215. h->intra4x4_pred_mode_cache[5+8*0]=
  216. h->intra4x4_pred_mode_cache[6+8*0]=
  217. h->intra4x4_pred_mode_cache[7+8*0]= pred;
  218. }
  219. for(i=0; i<2; i++){
  220. if(IS_INTRA4x4(left_type[i])){
  221. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];
  222. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];
  223. }else{
  224. int pred;
  225. if(!(left_type[i] & type_mask))
  226. pred= -1;
  227. else{
  228. pred= 2;
  229. }
  230. h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
  231. h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;
  232. }
  233. }
  234. }
  235. }
  236. }
  237. /*
  238. 0 . T T. T T T T
  239. 1 L . .L . . . .
  240. 2 L . .L . . . .
  241. 3 . T TL . . . .
  242. 4 L . .L . . . .
  243. 5 L . .. . . . .
  244. */
  245. //FIXME constraint_intra_pred & partitioning & nnz (let us hope this is just a typo in the spec)
  246. if(top_type){
  247. h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][4];
  248. h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][5];
  249. h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][6];
  250. h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3];
  251. h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][9];
  252. h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8];
  253. h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][12];
  254. h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11];
  255. }else{
  256. h->non_zero_count_cache[4+8*0]=
  257. h->non_zero_count_cache[5+8*0]=
  258. h->non_zero_count_cache[6+8*0]=
  259. h->non_zero_count_cache[7+8*0]=
  260. h->non_zero_count_cache[1+8*0]=
  261. h->non_zero_count_cache[2+8*0]=
  262. h->non_zero_count_cache[1+8*3]=
  263. h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  264. }
  265. for (i=0; i<2; i++) {
  266. if(left_type[i]){
  267. h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[0+2*i]];
  268. h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[1+2*i]];
  269. h->non_zero_count_cache[0+8*1 + 8*i]= h->non_zero_count[left_xy[i]][left_block[4+2*i]];
  270. h->non_zero_count_cache[0+8*4 + 8*i]= h->non_zero_count[left_xy[i]][left_block[5+2*i]];
  271. }else{
  272. h->non_zero_count_cache[3+8*1 + 2*8*i]=
  273. h->non_zero_count_cache[3+8*2 + 2*8*i]=
  274. h->non_zero_count_cache[0+8*1 + 8*i]=
  275. h->non_zero_count_cache[0+8*4 + 8*i]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
  276. }
  277. }
  278. if( h->pps.cabac ) {
  279. // top_cbp
  280. if(top_type) {
  281. h->top_cbp = h->cbp_table[top_xy];
  282. } else if(IS_INTRA(mb_type)) {
  283. h->top_cbp = 0x1C0;
  284. } else {
  285. h->top_cbp = 0;
  286. }
  287. // left_cbp
  288. if (left_type[0]) {
  289. h->left_cbp = h->cbp_table[left_xy[0]] & 0x1f0;
  290. } else if(IS_INTRA(mb_type)) {
  291. h->left_cbp = 0x1C0;
  292. } else {
  293. h->left_cbp = 0;
  294. }
  295. if (left_type[0]) {
  296. h->left_cbp |= ((h->cbp_table[left_xy[0]]>>((left_block[0]&(~1))+1))&0x1) << 1;
  297. }
  298. if (left_type[1]) {
  299. h->left_cbp |= ((h->cbp_table[left_xy[1]]>>((left_block[2]&(~1))+1))&0x1) << 3;
  300. }
  301. }
  302. #if 1
  303. if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
  304. int list;
  305. for(list=0; list<h->list_count; list++){
  306. if(!USES_LIST(mb_type, list) && !IS_DIRECT(mb_type) && !h->deblocking_filter){
  307. /*if(!h->mv_cache_clean[list]){
  308. memset(h->mv_cache [list], 0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all?
  309. memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
  310. h->mv_cache_clean[list]= 1;
  311. }*/
  312. continue;
  313. }
  314. h->mv_cache_clean[list]= 0;
  315. if(USES_LIST(top_type, list)){
  316. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  317. const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;
  318. *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0];
  319. *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1];
  320. *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2];
  321. *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3];
  322. h->ref_cache[list][scan8[0] + 0 - 1*8]=
  323. h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];
  324. h->ref_cache[list][scan8[0] + 2 - 1*8]=
  325. h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];
  326. }else{
  327. *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]=
  328. *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]=
  329. *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]=
  330. *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0;
  331. *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;
  332. }
  333. for(i=0; i<2; i++){
  334. int cache_idx = scan8[0] - 1 + i*2*8;
  335. if(USES_LIST(left_type[i], list)){
  336. const int b_xy= h->mb2b_xy[left_xy[i]] + 3;
  337. const int b8_xy= h->mb2b8_xy[left_xy[i]] + 1;
  338. *(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]];
  339. *(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]];
  340. h->ref_cache[list][cache_idx ]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0+i*2]>>1)];
  341. h->ref_cache[list][cache_idx+8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[1+i*2]>>1)];
  342. }else{
  343. *(uint32_t*)h->mv_cache [list][cache_idx ]=
  344. *(uint32_t*)h->mv_cache [list][cache_idx+8]= 0;
  345. h->ref_cache[list][cache_idx ]=
  346. h->ref_cache[list][cache_idx+8]= left_type[i] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  347. }
  348. }
  349. if(for_deblock || ((IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred) && !FRAME_MBAFF))
  350. continue;
  351. if(USES_LIST(topleft_type, list)){
  352. const int b_xy = h->mb2b_xy[topleft_xy] + 3 + h->b_stride + (topleft_partition & 2*h->b_stride);
  353. const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + (topleft_partition & h->b8_stride);
  354. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  355. h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  356. }else{
  357. *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;
  358. h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  359. }
  360. if(USES_LIST(topright_type, list)){
  361. const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
  362. const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;
  363. *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
  364. h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];
  365. }else{
  366. *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;
  367. h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
  368. }
  369. if((IS_SKIP(mb_type) || IS_DIRECT(mb_type)) && !FRAME_MBAFF)
  370. continue;
  371. h->ref_cache[list][scan8[5 ]+1] =
  372. h->ref_cache[list][scan8[7 ]+1] =
  373. h->ref_cache[list][scan8[13]+1] = //FIXME remove past 3 (init somewhere else)
  374. h->ref_cache[list][scan8[4 ]] =
  375. h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
  376. *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=
  377. *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=
  378. *(uint32_t*)h->mv_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  379. *(uint32_t*)h->mv_cache [list][scan8[4 ]]=
  380. *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;
  381. if( h->pps.cabac ) {
  382. /* XXX beurk, Load mvd */
  383. if(USES_LIST(top_type, list)){
  384. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  385. *(uint32_t*)h->mvd_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 0];
  386. *(uint32_t*)h->mvd_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 1];
  387. *(uint32_t*)h->mvd_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 2];
  388. *(uint32_t*)h->mvd_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 3];
  389. }else{
  390. *(uint32_t*)h->mvd_cache [list][scan8[0] + 0 - 1*8]=
  391. *(uint32_t*)h->mvd_cache [list][scan8[0] + 1 - 1*8]=
  392. *(uint32_t*)h->mvd_cache [list][scan8[0] + 2 - 1*8]=
  393. *(uint32_t*)h->mvd_cache [list][scan8[0] + 3 - 1*8]= 0;
  394. }
  395. if(USES_LIST(left_type[0], list)){
  396. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  397. *(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]];
  398. *(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]];
  399. }else{
  400. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 0*8]=
  401. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 1*8]= 0;
  402. }
  403. if(USES_LIST(left_type[1], list)){
  404. const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
  405. *(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]];
  406. *(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]];
  407. }else{
  408. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 2*8]=
  409. *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 3*8]= 0;
  410. }
  411. *(uint32_t*)h->mvd_cache [list][scan8[5 ]+1]=
  412. *(uint32_t*)h->mvd_cache [list][scan8[7 ]+1]=
  413. *(uint32_t*)h->mvd_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewhere else)
  414. *(uint32_t*)h->mvd_cache [list][scan8[4 ]]=
  415. *(uint32_t*)h->mvd_cache [list][scan8[12]]= 0;
  416. if(h->slice_type_nos == FF_B_TYPE){
  417. fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1);
  418. if(IS_DIRECT(top_type)){
  419. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101;
  420. }else if(IS_8X8(top_type)){
  421. int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride;
  422. h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy];
  423. h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1];
  424. }else{
  425. *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0;
  426. }
  427. if(IS_DIRECT(left_type[0]))
  428. h->direct_cache[scan8[0] - 1 + 0*8]= 1;
  429. else if(IS_8X8(left_type[0]))
  430. 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)];
  431. else
  432. h->direct_cache[scan8[0] - 1 + 0*8]= 0;
  433. if(IS_DIRECT(left_type[1]))
  434. h->direct_cache[scan8[0] - 1 + 2*8]= 1;
  435. else if(IS_8X8(left_type[1]))
  436. 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)];
  437. else
  438. h->direct_cache[scan8[0] - 1 + 2*8]= 0;
  439. }
  440. }
  441. if(FRAME_MBAFF){
  442. #define MAP_MVS\
  443. MAP_F2F(scan8[0] - 1 - 1*8, topleft_type)\
  444. MAP_F2F(scan8[0] + 0 - 1*8, top_type)\
  445. MAP_F2F(scan8[0] + 1 - 1*8, top_type)\
  446. MAP_F2F(scan8[0] + 2 - 1*8, top_type)\
  447. MAP_F2F(scan8[0] + 3 - 1*8, top_type)\
  448. MAP_F2F(scan8[0] + 4 - 1*8, topright_type)\
  449. MAP_F2F(scan8[0] - 1 + 0*8, left_type[0])\
  450. MAP_F2F(scan8[0] - 1 + 1*8, left_type[0])\
  451. MAP_F2F(scan8[0] - 1 + 2*8, left_type[1])\
  452. MAP_F2F(scan8[0] - 1 + 3*8, left_type[1])
  453. if(MB_FIELD){
  454. #define MAP_F2F(idx, mb_type)\
  455. if(!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  456. h->ref_cache[list][idx] <<= 1;\
  457. h->mv_cache[list][idx][1] /= 2;\
  458. h->mvd_cache[list][idx][1] /= 2;\
  459. }
  460. MAP_MVS
  461. #undef MAP_F2F
  462. }else{
  463. #define MAP_F2F(idx, mb_type)\
  464. if(IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
  465. h->ref_cache[list][idx] >>= 1;\
  466. h->mv_cache[list][idx][1] <<= 1;\
  467. h->mvd_cache[list][idx][1] <<= 1;\
  468. }
  469. MAP_MVS
  470. #undef MAP_F2F
  471. }
  472. }
  473. }
  474. }
  475. #endif
  476. h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);
  477. }
  478. void ff_h264_write_back_intra_pred_mode(H264Context *h){
  479. const int mb_xy= h->mb_xy;
  480. h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
  481. h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
  482. h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
  483. h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
  484. h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
  485. h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
  486. h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
  487. }
  488. /**
  489. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  490. */
  491. int ff_h264_check_intra4x4_pred_mode(H264Context *h){
  492. MpegEncContext * const s = &h->s;
  493. static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
  494. static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
  495. int i;
  496. if(!(h->top_samples_available&0x8000)){
  497. for(i=0; i<4; i++){
  498. int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
  499. if(status<0){
  500. 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);
  501. return -1;
  502. } else if(status){
  503. h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
  504. }
  505. }
  506. }
  507. if((h->left_samples_available&0x8888)!=0x8888){
  508. static const int mask[4]={0x8000,0x2000,0x80,0x20};
  509. for(i=0; i<4; i++){
  510. if(!(h->left_samples_available&mask[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. }
  521. return 0;
  522. } //FIXME cleanup like ff_h264_check_intra_pred_mode
  523. /**
  524. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  525. */
  526. int ff_h264_check_intra_pred_mode(H264Context *h, int mode){
  527. MpegEncContext * const s = &h->s;
  528. static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
  529. static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
  530. if(mode > 6U) {
  531. 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);
  532. return -1;
  533. }
  534. if(!(h->top_samples_available&0x8000)){
  535. mode= top[ mode ];
  536. if(mode<0){
  537. 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);
  538. return -1;
  539. }
  540. }
  541. if((h->left_samples_available&0x8080) != 0x8080){
  542. mode= left[ mode ];
  543. if(h->left_samples_available&0x8080){ //mad cow disease mode, aka MBAFF + constrained_intra_pred
  544. mode= ALZHEIMER_DC_L0T_PRED8x8 + (!(h->left_samples_available&0x8000)) + 2*(mode == DC_128_PRED8x8);
  545. }
  546. if(mode<0){
  547. 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);
  548. return -1;
  549. }
  550. }
  551. return mode;
  552. }
  553. /**
  554. * gets the predicted intra4x4 prediction mode.
  555. */
  556. static inline int pred_intra_mode(H264Context *h, int n){
  557. const int index8= scan8[n];
  558. const int left= h->intra4x4_pred_mode_cache[index8 - 1];
  559. const int top = h->intra4x4_pred_mode_cache[index8 - 8];
  560. const int min= FFMIN(left, top);
  561. tprintf(h->s.avctx, "mode:%d %d min:%d\n", left ,top, min);
  562. if(min<0) return DC_PRED;
  563. else return min;
  564. }
  565. static inline void write_back_non_zero_count(H264Context *h){
  566. const int mb_xy= h->mb_xy;
  567. h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[7+8*1];
  568. h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[7+8*2];
  569. h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[7+8*3];
  570. h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4];
  571. h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[4+8*4];
  572. h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[5+8*4];
  573. h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[6+8*4];
  574. h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[1+8*2];
  575. h->non_zero_count[mb_xy][8]= h->non_zero_count_cache[2+8*2];
  576. h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[2+8*1];
  577. h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[1+8*5];
  578. h->non_zero_count[mb_xy][11]=h->non_zero_count_cache[2+8*5];
  579. h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[2+8*4];
  580. }
  581. /**
  582. * gets the predicted number of non-zero coefficients.
  583. * @param n block index
  584. */
  585. static inline int pred_non_zero_count(H264Context *h, int n){
  586. const int index8= scan8[n];
  587. const int left= h->non_zero_count_cache[index8 - 1];
  588. const int top = h->non_zero_count_cache[index8 - 8];
  589. int i= left + top;
  590. if(i<64) i= (i+1)>>1;
  591. tprintf(h->s.avctx, "pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
  592. return i&31;
  593. }
  594. /**
  595. * gets the directionally predicted 16x8 MV.
  596. * @param n the block index
  597. * @param mx the x component of the predicted motion vector
  598. * @param my the y component of the predicted motion vector
  599. */
  600. static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  601. if(n==0){
  602. const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
  603. const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
  604. 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);
  605. if(top_ref == ref){
  606. *mx= B[0];
  607. *my= B[1];
  608. return;
  609. }
  610. }else{
  611. const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
  612. const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
  613. 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);
  614. if(left_ref == ref){
  615. *mx= A[0];
  616. *my= A[1];
  617. return;
  618. }
  619. }
  620. //RARE
  621. pred_motion(h, n, 4, list, ref, mx, my);
  622. }
  623. /**
  624. * gets the directionally predicted 8x16 MV.
  625. * @param n the block index
  626. * @param mx the x component of the predicted motion vector
  627. * @param my the y component of the predicted motion vector
  628. */
  629. static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
  630. if(n==0){
  631. const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
  632. const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
  633. 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);
  634. if(left_ref == ref){
  635. *mx= A[0];
  636. *my= A[1];
  637. return;
  638. }
  639. }else{
  640. const int16_t * C;
  641. int diagonal_ref;
  642. diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
  643. 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);
  644. if(diagonal_ref == ref){
  645. *mx= C[0];
  646. *my= C[1];
  647. return;
  648. }
  649. }
  650. //RARE
  651. pred_motion(h, n, 2, list, ref, mx, my);
  652. }
  653. static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
  654. const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
  655. const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
  656. tprintf(h->s.avctx, "pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
  657. if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
  658. || !( top_ref | *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ])
  659. || !(left_ref | *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ])){
  660. *mx = *my = 0;
  661. return;
  662. }
  663. pred_motion(h, 0, 4, 0, 0, mx, my);
  664. return;
  665. }
  666. static inline void write_back_motion(H264Context *h, int mb_type){
  667. MpegEncContext * const s = &h->s;
  668. const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
  669. const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride;
  670. int list;
  671. if(!USES_LIST(mb_type, 0))
  672. fill_rectangle(&s->current_picture.ref_index[0][b8_xy], 2, 2, h->b8_stride, (uint8_t)LIST_NOT_USED, 1);
  673. for(list=0; list<h->list_count; list++){
  674. int y;
  675. if(!USES_LIST(mb_type, list))
  676. continue;
  677. for(y=0; y<4; y++){
  678. *(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];
  679. *(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];
  680. }
  681. if( h->pps.cabac ) {
  682. if(IS_SKIP(mb_type))
  683. fill_rectangle(h->mvd_table[list][b_xy], 4, 4, h->b_stride, 0, 4);
  684. else
  685. for(y=0; y<4; y++){
  686. *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+0 + 8*y];
  687. *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+2 + 8*y];
  688. }
  689. }
  690. {
  691. int8_t *ref_index = &s->current_picture.ref_index[list][b8_xy];
  692. ref_index[0+0*h->b8_stride]= h->ref_cache[list][scan8[0]];
  693. ref_index[1+0*h->b8_stride]= h->ref_cache[list][scan8[4]];
  694. ref_index[0+1*h->b8_stride]= h->ref_cache[list][scan8[8]];
  695. ref_index[1+1*h->b8_stride]= h->ref_cache[list][scan8[12]];
  696. }
  697. }
  698. if(h->slice_type_nos == FF_B_TYPE && h->pps.cabac){
  699. if(IS_8X8(mb_type)){
  700. uint8_t *direct_table = &h->direct_table[b8_xy];
  701. direct_table[1+0*h->b8_stride] = IS_DIRECT(h->sub_mb_type[1]) ? 1 : 0;
  702. direct_table[0+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[2]) ? 1 : 0;
  703. direct_table[1+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[3]) ? 1 : 0;
  704. }
  705. }
  706. }
  707. const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src, int *dst_length, int *consumed, int length){
  708. int i, si, di;
  709. uint8_t *dst;
  710. int bufidx;
  711. // src[0]&0x80; //forbidden bit
  712. h->nal_ref_idc= src[0]>>5;
  713. h->nal_unit_type= src[0]&0x1F;
  714. src++; length--;
  715. #if 0
  716. for(i=0; i<length; i++)
  717. printf("%2X ", src[i]);
  718. #endif
  719. #if HAVE_FAST_UNALIGNED
  720. # if HAVE_FAST_64BIT
  721. # define RS 7
  722. for(i=0; i+1<length; i+=9){
  723. if(!((~*(const uint64_t*)(src+i) & (*(const uint64_t*)(src+i) - 0x0100010001000101ULL)) & 0x8000800080008080ULL))
  724. # else
  725. # define RS 3
  726. for(i=0; i+1<length; i+=5){
  727. if(!((~*(const uint32_t*)(src+i) & (*(const uint32_t*)(src+i) - 0x01000101U)) & 0x80008080U))
  728. # endif
  729. continue;
  730. if(i>0 && !src[i]) i--;
  731. while(src[i]) i++;
  732. #else
  733. # define RS 0
  734. for(i=0; i+1<length; i+=2){
  735. if(src[i]) continue;
  736. if(i>0 && src[i-1]==0) i--;
  737. #endif
  738. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  739. if(src[i+2]!=3){
  740. /* startcode, so we must be past the end */
  741. length=i;
  742. }
  743. break;
  744. }
  745. i-= RS;
  746. }
  747. if(i>=length-1){ //no escaped 0
  748. *dst_length= length;
  749. *consumed= length+1; //+1 for the header
  750. return src;
  751. }
  752. bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0; // use second escape buffer for inter data
  753. av_fast_malloc(&h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx], length+FF_INPUT_BUFFER_PADDING_SIZE);
  754. dst= h->rbsp_buffer[bufidx];
  755. if (dst == NULL){
  756. return NULL;
  757. }
  758. //printf("decoding esc\n");
  759. memcpy(dst, src, i);
  760. si=di=i;
  761. while(si+2<length){
  762. //remove escapes (very rare 1:2^22)
  763. if(src[si+2]>3){
  764. dst[di++]= src[si++];
  765. dst[di++]= src[si++];
  766. }else if(src[si]==0 && src[si+1]==0){
  767. if(src[si+2]==3){ //escape
  768. dst[di++]= 0;
  769. dst[di++]= 0;
  770. si+=3;
  771. continue;
  772. }else //next start code
  773. goto nsc;
  774. }
  775. dst[di++]= src[si++];
  776. }
  777. while(si<length)
  778. dst[di++]= src[si++];
  779. nsc:
  780. memset(dst+di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  781. *dst_length= di;
  782. *consumed= si + 1;//+1 for the header
  783. //FIXME store exact number of bits in the getbitcontext (it is needed for decoding)
  784. return dst;
  785. }
  786. int ff_h264_decode_rbsp_trailing(H264Context *h, const uint8_t *src){
  787. int v= *src;
  788. int r;
  789. tprintf(h->s.avctx, "rbsp trailing %X\n", v);
  790. for(r=1; r<9; r++){
  791. if(v&1) return r;
  792. v>>=1;
  793. }
  794. return 0;
  795. }
  796. /**
  797. * IDCT transforms the 16 dc values and dequantizes them.
  798. * @param qp quantization parameter
  799. */
  800. static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  801. #define stride 16
  802. int i;
  803. int temp[16]; //FIXME check if this is a good idea
  804. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  805. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  806. //memset(block, 64, 2*256);
  807. //return;
  808. for(i=0; i<4; i++){
  809. const int offset= y_offset[i];
  810. const int z0= block[offset+stride*0] + block[offset+stride*4];
  811. const int z1= block[offset+stride*0] - block[offset+stride*4];
  812. const int z2= block[offset+stride*1] - block[offset+stride*5];
  813. const int z3= block[offset+stride*1] + block[offset+stride*5];
  814. temp[4*i+0]= z0+z3;
  815. temp[4*i+1]= z1+z2;
  816. temp[4*i+2]= z1-z2;
  817. temp[4*i+3]= z0-z3;
  818. }
  819. for(i=0; i<4; i++){
  820. const int offset= x_offset[i];
  821. const int z0= temp[4*0+i] + temp[4*2+i];
  822. const int z1= temp[4*0+i] - temp[4*2+i];
  823. const int z2= temp[4*1+i] - temp[4*3+i];
  824. const int z3= temp[4*1+i] + temp[4*3+i];
  825. block[stride*0 +offset]= ((((z0 + z3)*qmul + 128 ) >> 8)); //FIXME think about merging this into decode_residual
  826. block[stride*2 +offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
  827. block[stride*8 +offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
  828. block[stride*10+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
  829. }
  830. }
  831. #if 0
  832. /**
  833. * DCT transforms the 16 dc values.
  834. * @param qp quantization parameter ??? FIXME
  835. */
  836. static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
  837. // const int qmul= dequant_coeff[qp][0];
  838. int i;
  839. int temp[16]; //FIXME check if this is a good idea
  840. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  841. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  842. for(i=0; i<4; i++){
  843. const int offset= y_offset[i];
  844. const int z0= block[offset+stride*0] + block[offset+stride*4];
  845. const int z1= block[offset+stride*0] - block[offset+stride*4];
  846. const int z2= block[offset+stride*1] - block[offset+stride*5];
  847. const int z3= block[offset+stride*1] + block[offset+stride*5];
  848. temp[4*i+0]= z0+z3;
  849. temp[4*i+1]= z1+z2;
  850. temp[4*i+2]= z1-z2;
  851. temp[4*i+3]= z0-z3;
  852. }
  853. for(i=0; i<4; i++){
  854. const int offset= x_offset[i];
  855. const int z0= temp[4*0+i] + temp[4*2+i];
  856. const int z1= temp[4*0+i] - temp[4*2+i];
  857. const int z2= temp[4*1+i] - temp[4*3+i];
  858. const int z3= temp[4*1+i] + temp[4*3+i];
  859. block[stride*0 +offset]= (z0 + z3)>>1;
  860. block[stride*2 +offset]= (z1 + z2)>>1;
  861. block[stride*8 +offset]= (z1 - z2)>>1;
  862. block[stride*10+offset]= (z0 - z3)>>1;
  863. }
  864. }
  865. #endif
  866. #undef xStride
  867. #undef stride
  868. static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  869. const int stride= 16*2;
  870. const int xStride= 16;
  871. int a,b,c,d,e;
  872. a= block[stride*0 + xStride*0];
  873. b= block[stride*0 + xStride*1];
  874. c= block[stride*1 + xStride*0];
  875. d= block[stride*1 + xStride*1];
  876. e= a-b;
  877. a= a+b;
  878. b= c-d;
  879. c= c+d;
  880. block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
  881. block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
  882. block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
  883. block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;
  884. }
  885. #if 0
  886. static void chroma_dc_dct_c(DCTELEM *block){
  887. const int stride= 16*2;
  888. const int xStride= 16;
  889. int a,b,c,d,e;
  890. a= block[stride*0 + xStride*0];
  891. b= block[stride*0 + xStride*1];
  892. c= block[stride*1 + xStride*0];
  893. d= block[stride*1 + xStride*1];
  894. e= a-b;
  895. a= a+b;
  896. b= c-d;
  897. c= c+d;
  898. block[stride*0 + xStride*0]= (a+c);
  899. block[stride*0 + xStride*1]= (e+b);
  900. block[stride*1 + xStride*0]= (a-c);
  901. block[stride*1 + xStride*1]= (e-b);
  902. }
  903. #endif
  904. static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
  905. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  906. int src_x_offset, int src_y_offset,
  907. qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
  908. MpegEncContext * const s = &h->s;
  909. const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
  910. int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
  911. const int luma_xy= (mx&3) + ((my&3)<<2);
  912. uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->mb_linesize;
  913. uint8_t * src_cb, * src_cr;
  914. int extra_width= h->emu_edge_width;
  915. int extra_height= h->emu_edge_height;
  916. int emu=0;
  917. const int full_mx= mx>>2;
  918. const int full_my= my>>2;
  919. const int pic_width = 16*s->mb_width;
  920. const int pic_height = 16*s->mb_height >> MB_FIELD;
  921. if(mx&7) extra_width -= 3;
  922. if(my&7) extra_height -= 3;
  923. if( full_mx < 0-extra_width
  924. || full_my < 0-extra_height
  925. || full_mx + 16/*FIXME*/ > pic_width + extra_width
  926. || full_my + 16/*FIXME*/ > pic_height + extra_height){
  927. 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);
  928. src_y= s->edge_emu_buffer + 2 + 2*h->mb_linesize;
  929. emu=1;
  930. }
  931. qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); //FIXME try variable height perhaps?
  932. if(!square){
  933. qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
  934. }
  935. if(CONFIG_GRAY && s->flags&CODEC_FLAG_GRAY) return;
  936. if(MB_FIELD){
  937. // chroma offset when predicting from a field of opposite parity
  938. my += 2 * ((s->mb_y & 1) - (pic->reference - 1));
  939. emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
  940. }
  941. src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  942. src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  943. if(emu){
  944. 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);
  945. src_cb= s->edge_emu_buffer;
  946. }
  947. chroma_op(dest_cb, src_cb, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  948. if(emu){
  949. 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);
  950. src_cr= s->edge_emu_buffer;
  951. }
  952. chroma_op(dest_cr, src_cr, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  953. }
  954. static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
  955. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  956. int x_offset, int y_offset,
  957. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  958. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  959. int list0, int list1){
  960. MpegEncContext * const s = &h->s;
  961. qpel_mc_func *qpix_op= qpix_put;
  962. h264_chroma_mc_func chroma_op= chroma_put;
  963. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  964. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  965. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  966. x_offset += 8*s->mb_x;
  967. y_offset += 8*(s->mb_y >> MB_FIELD);
  968. if(list0){
  969. Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
  970. mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
  971. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  972. qpix_op, chroma_op);
  973. qpix_op= qpix_avg;
  974. chroma_op= chroma_avg;
  975. }
  976. if(list1){
  977. Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
  978. mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
  979. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  980. qpix_op, chroma_op);
  981. }
  982. }
  983. static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
  984. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  985. int x_offset, int y_offset,
  986. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  987. h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
  988. h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
  989. int list0, int list1){
  990. MpegEncContext * const s = &h->s;
  991. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  992. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  993. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  994. x_offset += 8*s->mb_x;
  995. y_offset += 8*(s->mb_y >> MB_FIELD);
  996. if(list0 && list1){
  997. /* don't optimize for luma-only case, since B-frames usually
  998. * use implicit weights => chroma too. */
  999. uint8_t *tmp_cb = s->obmc_scratchpad;
  1000. uint8_t *tmp_cr = s->obmc_scratchpad + 8;
  1001. uint8_t *tmp_y = s->obmc_scratchpad + 8*h->mb_uvlinesize;
  1002. int refn0 = h->ref_cache[0][ scan8[n] ];
  1003. int refn1 = h->ref_cache[1][ scan8[n] ];
  1004. mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
  1005. dest_y, dest_cb, dest_cr,
  1006. x_offset, y_offset, qpix_put, chroma_put);
  1007. mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
  1008. tmp_y, tmp_cb, tmp_cr,
  1009. x_offset, y_offset, qpix_put, chroma_put);
  1010. if(h->use_weight == 2){
  1011. int weight0 = h->implicit_weight[refn0][refn1];
  1012. int weight1 = 64 - weight0;
  1013. luma_weight_avg( dest_y, tmp_y, h-> mb_linesize, 5, weight0, weight1, 0);
  1014. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, 5, weight0, weight1, 0);
  1015. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, 5, weight0, weight1, 0);
  1016. }else{
  1017. luma_weight_avg(dest_y, tmp_y, h->mb_linesize, h->luma_log2_weight_denom,
  1018. h->luma_weight[0][refn0], h->luma_weight[1][refn1],
  1019. h->luma_offset[0][refn0] + h->luma_offset[1][refn1]);
  1020. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  1021. h->chroma_weight[0][refn0][0], h->chroma_weight[1][refn1][0],
  1022. h->chroma_offset[0][refn0][0] + h->chroma_offset[1][refn1][0]);
  1023. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  1024. h->chroma_weight[0][refn0][1], h->chroma_weight[1][refn1][1],
  1025. h->chroma_offset[0][refn0][1] + h->chroma_offset[1][refn1][1]);
  1026. }
  1027. }else{
  1028. int list = list1 ? 1 : 0;
  1029. int refn = h->ref_cache[list][ scan8[n] ];
  1030. Picture *ref= &h->ref_list[list][refn];
  1031. mc_dir_part(h, ref, n, square, chroma_height, delta, list,
  1032. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1033. qpix_put, chroma_put);
  1034. luma_weight_op(dest_y, h->mb_linesize, h->luma_log2_weight_denom,
  1035. h->luma_weight[list][refn], h->luma_offset[list][refn]);
  1036. if(h->use_weight_chroma){
  1037. chroma_weight_op(dest_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  1038. h->chroma_weight[list][refn][0], h->chroma_offset[list][refn][0]);
  1039. chroma_weight_op(dest_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  1040. h->chroma_weight[list][refn][1], h->chroma_offset[list][refn][1]);
  1041. }
  1042. }
  1043. }
  1044. static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
  1045. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1046. int x_offset, int y_offset,
  1047. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  1048. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  1049. h264_weight_func *weight_op, h264_biweight_func *weight_avg,
  1050. int list0, int list1){
  1051. if((h->use_weight==2 && list0 && list1
  1052. && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ] != 32))
  1053. || h->use_weight==1)
  1054. mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  1055. x_offset, y_offset, qpix_put, chroma_put,
  1056. weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
  1057. else
  1058. mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  1059. x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
  1060. }
  1061. static inline void prefetch_motion(H264Context *h, int list){
  1062. /* fetch pixels for estimated mv 4 macroblocks ahead
  1063. * optimized for 64byte cache lines */
  1064. MpegEncContext * const s = &h->s;
  1065. const int refn = h->ref_cache[list][scan8[0]];
  1066. if(refn >= 0){
  1067. const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
  1068. const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
  1069. uint8_t **src= h->ref_list[list][refn].data;
  1070. int off= mx + (my + (s->mb_x&3)*4)*h->mb_linesize + 64;
  1071. s->dsp.prefetch(src[0]+off, s->linesize, 4);
  1072. off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
  1073. s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
  1074. }
  1075. }
  1076. static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1077. qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
  1078. qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
  1079. h264_weight_func *weight_op, h264_biweight_func *weight_avg){
  1080. MpegEncContext * const s = &h->s;
  1081. const int mb_xy= h->mb_xy;
  1082. const int mb_type= s->current_picture.mb_type[mb_xy];
  1083. assert(IS_INTER(mb_type));
  1084. prefetch_motion(h, 0);
  1085. if(IS_16X16(mb_type)){
  1086. mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
  1087. qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
  1088. &weight_op[0], &weight_avg[0],
  1089. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  1090. }else if(IS_16X8(mb_type)){
  1091. mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
  1092. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  1093. &weight_op[1], &weight_avg[1],
  1094. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  1095. mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
  1096. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  1097. &weight_op[1], &weight_avg[1],
  1098. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  1099. }else if(IS_8X16(mb_type)){
  1100. mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
  1101. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  1102. &weight_op[2], &weight_avg[2],
  1103. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  1104. mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
  1105. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  1106. &weight_op[2], &weight_avg[2],
  1107. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  1108. }else{
  1109. int i;
  1110. assert(IS_8X8(mb_type));
  1111. for(i=0; i<4; i++){
  1112. const int sub_mb_type= h->sub_mb_type[i];
  1113. const int n= 4*i;
  1114. int x_offset= (i&1)<<2;
  1115. int y_offset= (i&2)<<1;
  1116. if(IS_SUB_8X8(sub_mb_type)){
  1117. mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1118. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  1119. &weight_op[3], &weight_avg[3],
  1120. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1121. }else if(IS_SUB_8X4(sub_mb_type)){
  1122. mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1123. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  1124. &weight_op[4], &weight_avg[4],
  1125. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1126. mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
  1127. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  1128. &weight_op[4], &weight_avg[4],
  1129. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1130. }else if(IS_SUB_4X8(sub_mb_type)){
  1131. mc_part(h, n , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  1132. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  1133. &weight_op[5], &weight_avg[5],
  1134. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1135. mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
  1136. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  1137. &weight_op[5], &weight_avg[5],
  1138. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1139. }else{
  1140. int j;
  1141. assert(IS_SUB_4X4(sub_mb_type));
  1142. for(j=0; j<4; j++){
  1143. int sub_x_offset= x_offset + 2*(j&1);
  1144. int sub_y_offset= y_offset + (j&2);
  1145. mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
  1146. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  1147. &weight_op[6], &weight_avg[6],
  1148. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  1149. }
  1150. }
  1151. }
  1152. }
  1153. prefetch_motion(h, 1);
  1154. }
  1155. static av_cold void init_cavlc_level_tab(void){
  1156. int suffix_length, mask;
  1157. unsigned int i;
  1158. for(suffix_length=0; suffix_length<7; suffix_length++){
  1159. for(i=0; i<(1<<LEVEL_TAB_BITS); i++){
  1160. int prefix= LEVEL_TAB_BITS - av_log2(2*i);
  1161. int level_code= (prefix<<suffix_length) + (i>>(LEVEL_TAB_BITS-prefix-1-suffix_length)) - (1<<suffix_length);
  1162. mask= -(level_code&1);
  1163. level_code= (((2+level_code)>>1) ^ mask) - mask;
  1164. if(prefix + 1 + suffix_length <= LEVEL_TAB_BITS){
  1165. cavlc_level_tab[suffix_length][i][0]= level_code;
  1166. cavlc_level_tab[suffix_length][i][1]= prefix + 1 + suffix_length;
  1167. }else if(prefix + 1 <= LEVEL_TAB_BITS){
  1168. cavlc_level_tab[suffix_length][i][0]= prefix+100;
  1169. cavlc_level_tab[suffix_length][i][1]= prefix + 1;
  1170. }else{
  1171. cavlc_level_tab[suffix_length][i][0]= LEVEL_TAB_BITS+100;
  1172. cavlc_level_tab[suffix_length][i][1]= LEVEL_TAB_BITS;
  1173. }
  1174. }
  1175. }
  1176. }
  1177. static av_cold void decode_init_vlc(void){
  1178. static int done = 0;
  1179. if (!done) {
  1180. int i;
  1181. int offset;
  1182. done = 1;
  1183. chroma_dc_coeff_token_vlc.table = chroma_dc_coeff_token_vlc_table;
  1184. chroma_dc_coeff_token_vlc.table_allocated = chroma_dc_coeff_token_vlc_table_size;
  1185. init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5,
  1186. &chroma_dc_coeff_token_len [0], 1, 1,
  1187. &chroma_dc_coeff_token_bits[0], 1, 1,
  1188. INIT_VLC_USE_NEW_STATIC);
  1189. offset = 0;
  1190. for(i=0; i<4; i++){
  1191. coeff_token_vlc[i].table = coeff_token_vlc_tables+offset;
  1192. coeff_token_vlc[i].table_allocated = coeff_token_vlc_tables_size[i];
  1193. init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17,
  1194. &coeff_token_len [i][0], 1, 1,
  1195. &coeff_token_bits[i][0], 1, 1,
  1196. INIT_VLC_USE_NEW_STATIC);
  1197. offset += coeff_token_vlc_tables_size[i];
  1198. }
  1199. /*
  1200. * This is a one time safety check to make sure that
  1201. * the packed static coeff_token_vlc table sizes
  1202. * were initialized correctly.
  1203. */
  1204. assert(offset == FF_ARRAY_ELEMS(coeff_token_vlc_tables));
  1205. for(i=0; i<3; i++){
  1206. chroma_dc_total_zeros_vlc[i].table = chroma_dc_total_zeros_vlc_tables[i];
  1207. chroma_dc_total_zeros_vlc[i].table_allocated = chroma_dc_total_zeros_vlc_tables_size;
  1208. init_vlc(&chroma_dc_total_zeros_vlc[i],
  1209. CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
  1210. &chroma_dc_total_zeros_len [i][0], 1, 1,
  1211. &chroma_dc_total_zeros_bits[i][0], 1, 1,
  1212. INIT_VLC_USE_NEW_STATIC);
  1213. }
  1214. for(i=0; i<15; i++){
  1215. total_zeros_vlc[i].table = total_zeros_vlc_tables[i];
  1216. total_zeros_vlc[i].table_allocated = total_zeros_vlc_tables_size;
  1217. init_vlc(&total_zeros_vlc[i],
  1218. TOTAL_ZEROS_VLC_BITS, 16,
  1219. &total_zeros_len [i][0], 1, 1,
  1220. &total_zeros_bits[i][0], 1, 1,
  1221. INIT_VLC_USE_NEW_STATIC);
  1222. }
  1223. for(i=0; i<6; i++){
  1224. run_vlc[i].table = run_vlc_tables[i];
  1225. run_vlc[i].table_allocated = run_vlc_tables_size;
  1226. init_vlc(&run_vlc[i],
  1227. RUN_VLC_BITS, 7,
  1228. &run_len [i][0], 1, 1,
  1229. &run_bits[i][0], 1, 1,
  1230. INIT_VLC_USE_NEW_STATIC);
  1231. }
  1232. run7_vlc.table = run7_vlc_table,
  1233. run7_vlc.table_allocated = run7_vlc_table_size;
  1234. init_vlc(&run7_vlc, RUN7_VLC_BITS, 16,
  1235. &run_len [6][0], 1, 1,
  1236. &run_bits[6][0], 1, 1,
  1237. INIT_VLC_USE_NEW_STATIC);
  1238. init_cavlc_level_tab();
  1239. }
  1240. }
  1241. static void free_tables(H264Context *h){
  1242. int i;
  1243. H264Context *hx;
  1244. av_freep(&h->intra4x4_pred_mode);
  1245. av_freep(&h->chroma_pred_mode_table);
  1246. av_freep(&h->cbp_table);
  1247. av_freep(&h->mvd_table[0]);
  1248. av_freep(&h->mvd_table[1]);
  1249. av_freep(&h->direct_table);
  1250. av_freep(&h->non_zero_count);
  1251. av_freep(&h->slice_table_base);
  1252. h->slice_table= NULL;
  1253. av_freep(&h->mb2b_xy);
  1254. av_freep(&h->mb2b8_xy);
  1255. for(i = 0; i < MAX_THREADS; i++) {
  1256. hx = h->thread_context[i];
  1257. if(!hx) continue;
  1258. av_freep(&hx->top_borders[1]);
  1259. av_freep(&hx->top_borders[0]);
  1260. av_freep(&hx->s.obmc_scratchpad);
  1261. av_freep(&hx->rbsp_buffer[1]);
  1262. av_freep(&hx->rbsp_buffer[0]);
  1263. hx->rbsp_buffer_size[0] = 0;
  1264. hx->rbsp_buffer_size[1] = 0;
  1265. if (i) av_freep(&h->thread_context[i]);
  1266. }
  1267. }
  1268. static void init_dequant8_coeff_table(H264Context *h){
  1269. int i,q,x;
  1270. const int transpose = (h->s.dsp.h264_idct8_add != ff_h264_idct8_add_c); //FIXME ugly
  1271. h->dequant8_coeff[0] = h->dequant8_buffer[0];
  1272. h->dequant8_coeff[1] = h->dequant8_buffer[1];
  1273. for(i=0; i<2; i++ ){
  1274. if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
  1275. h->dequant8_coeff[1] = h->dequant8_buffer[0];
  1276. break;
  1277. }
  1278. for(q=0; q<52; q++){
  1279. int shift = div6[q];
  1280. int idx = rem6[q];
  1281. for(x=0; x<64; x++)
  1282. h->dequant8_coeff[i][q][transpose ? (x>>3)|((x&7)<<3) : x] =
  1283. ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
  1284. h->pps.scaling_matrix8[i][x]) << shift;
  1285. }
  1286. }
  1287. }
  1288. static void init_dequant4_coeff_table(H264Context *h){
  1289. int i,j,q,x;
  1290. const int transpose = (h->s.dsp.h264_idct_add != ff_h264_idct_add_c); //FIXME ugly
  1291. for(i=0; i<6; i++ ){
  1292. h->dequant4_coeff[i] = h->dequant4_buffer[i];
  1293. for(j=0; j<i; j++){
  1294. if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
  1295. h->dequant4_coeff[i] = h->dequant4_buffer[j];
  1296. break;
  1297. }
  1298. }
  1299. if(j<i)
  1300. continue;
  1301. for(q=0; q<52; q++){
  1302. int shift = div6[q] + 2;
  1303. int idx = rem6[q];
  1304. for(x=0; x<16; x++)
  1305. h->dequant4_coeff[i][q][transpose ? (x>>2)|((x<<2)&0xF) : x] =
  1306. ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
  1307. h->pps.scaling_matrix4[i][x]) << shift;
  1308. }
  1309. }
  1310. }
  1311. static void init_dequant_tables(H264Context *h){
  1312. int i,x;
  1313. init_dequant4_coeff_table(h);
  1314. if(h->pps.transform_8x8_mode)
  1315. init_dequant8_coeff_table(h);
  1316. if(h->sps.transform_bypass){
  1317. for(i=0; i<6; i++)
  1318. for(x=0; x<16; x++)
  1319. h->dequant4_coeff[i][0][x] = 1<<6;
  1320. if(h->pps.transform_8x8_mode)
  1321. for(i=0; i<2; i++)
  1322. for(x=0; x<64; x++)
  1323. h->dequant8_coeff[i][0][x] = 1<<6;
  1324. }
  1325. }
  1326. int ff_h264_alloc_tables(H264Context *h){
  1327. MpegEncContext * const s = &h->s;
  1328. const int big_mb_num= s->mb_stride * (s->mb_height+1);
  1329. int x,y;
  1330. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t), fail)
  1331. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t), fail)
  1332. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->slice_table_base , (big_mb_num+s->mb_stride) * sizeof(*h->slice_table_base), fail)
  1333. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->cbp_table, big_mb_num * sizeof(uint16_t), fail)
  1334. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t), fail)
  1335. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[0], 32*big_mb_num * sizeof(uint16_t), fail);
  1336. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[1], 32*big_mb_num * sizeof(uint16_t), fail);
  1337. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->direct_table, 32*big_mb_num * sizeof(uint8_t) , fail);
  1338. memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride) * sizeof(*h->slice_table_base));
  1339. h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
  1340. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2b_xy , big_mb_num * sizeof(uint32_t), fail);
  1341. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2b8_xy , big_mb_num * sizeof(uint32_t), fail);
  1342. for(y=0; y<s->mb_height; y++){
  1343. for(x=0; x<s->mb_width; x++){
  1344. const int mb_xy= x + y*s->mb_stride;
  1345. const int b_xy = 4*x + 4*y*h->b_stride;
  1346. const int b8_xy= 2*x + 2*y*h->b8_stride;
  1347. h->mb2b_xy [mb_xy]= b_xy;
  1348. h->mb2b8_xy[mb_xy]= b8_xy;
  1349. }
  1350. }
  1351. s->obmc_scratchpad = NULL;
  1352. if(!h->dequant4_coeff[0])
  1353. init_dequant_tables(h);
  1354. return 0;
  1355. fail:
  1356. free_tables(h);
  1357. return -1;
  1358. }
  1359. /**
  1360. * Mimic alloc_tables(), but for every context thread.
  1361. */
  1362. static void clone_tables(H264Context *dst, H264Context *src){
  1363. dst->intra4x4_pred_mode = src->intra4x4_pred_mode;
  1364. dst->non_zero_count = src->non_zero_count;
  1365. dst->slice_table = src->slice_table;
  1366. dst->cbp_table = src->cbp_table;
  1367. dst->mb2b_xy = src->mb2b_xy;
  1368. dst->mb2b8_xy = src->mb2b8_xy;
  1369. dst->chroma_pred_mode_table = src->chroma_pred_mode_table;
  1370. dst->mvd_table[0] = src->mvd_table[0];
  1371. dst->mvd_table[1] = src->mvd_table[1];
  1372. dst->direct_table = src->direct_table;
  1373. dst->s.obmc_scratchpad = NULL;
  1374. ff_h264_pred_init(&dst->hpc, src->s.codec_id);
  1375. }
  1376. /**
  1377. * Init context
  1378. * Allocate buffers which are not shared amongst multiple threads.
  1379. */
  1380. static int context_init(H264Context *h){
  1381. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[0], h->s.mb_width * (16+8+8) * sizeof(uint8_t), fail)
  1382. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[1], h->s.mb_width * (16+8+8) * sizeof(uint8_t), fail)
  1383. return 0;
  1384. fail:
  1385. return -1; // free_tables will clean up for us
  1386. }
  1387. static av_cold void common_init(H264Context *h){
  1388. MpegEncContext * const s = &h->s;
  1389. s->width = s->avctx->width;
  1390. s->height = s->avctx->height;
  1391. s->codec_id= s->avctx->codec->id;
  1392. ff_h264_pred_init(&h->hpc, s->codec_id);
  1393. h->dequant_coeff_pps= -1;
  1394. s->unrestricted_mv=1;
  1395. s->decode=1; //FIXME
  1396. dsputil_init(&s->dsp, s->avctx); // needed so that idct permutation is known early
  1397. memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  1398. memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  1399. }
  1400. av_cold int ff_h264_decode_init(AVCodecContext *avctx){
  1401. H264Context *h= avctx->priv_data;
  1402. MpegEncContext * const s = &h->s;
  1403. MPV_decode_defaults(s);
  1404. s->avctx = avctx;
  1405. common_init(h);
  1406. s->out_format = FMT_H264;
  1407. s->workaround_bugs= avctx->workaround_bugs;
  1408. // set defaults
  1409. // s->decode_mb= ff_h263_decode_mb;
  1410. s->quarter_sample = 1;
  1411. if(!avctx->has_b_frames)
  1412. s->low_delay= 1;
  1413. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  1414. decode_init_vlc();
  1415. if(avctx->extradata_size > 0 && avctx->extradata &&
  1416. *(char *)avctx->extradata == 1){
  1417. h->is_avc = 1;
  1418. h->got_avcC = 0;
  1419. } else {
  1420. h->is_avc = 0;
  1421. }
  1422. h->thread_context[0] = h;
  1423. h->outputed_poc = INT_MIN;
  1424. h->prev_poc_msb= 1<<16;
  1425. ff_h264_reset_sei(h);
  1426. if(avctx->codec_id == CODEC_ID_H264){
  1427. if(avctx->ticks_per_frame == 1){
  1428. s->avctx->time_base.den *=2;
  1429. }
  1430. avctx->ticks_per_frame = 2;
  1431. }
  1432. return 0;
  1433. }
  1434. int ff_h264_frame_start(H264Context *h){
  1435. MpegEncContext * const s = &h->s;
  1436. int i;
  1437. if(MPV_frame_start(s, s->avctx) < 0)
  1438. return -1;
  1439. ff_er_frame_start(s);
  1440. /*
  1441. * MPV_frame_start uses pict_type to derive key_frame.
  1442. * This is incorrect for H.264; IDR markings must be used.
  1443. * Zero here; IDR markings per slice in frame or fields are ORed in later.
  1444. * See decode_nal_units().
  1445. */
  1446. s->current_picture_ptr->key_frame= 0;
  1447. s->current_picture_ptr->mmco_reset= 0;
  1448. assert(s->linesize && s->uvlinesize);
  1449. for(i=0; i<16; i++){
  1450. h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  1451. h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
  1452. }
  1453. for(i=0; i<4; i++){
  1454. h->block_offset[16+i]=
  1455. h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  1456. h->block_offset[24+16+i]=
  1457. h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  1458. }
  1459. /* can't be in alloc_tables because linesize isn't known there.
  1460. * FIXME: redo bipred weight to not require extra buffer? */
  1461. for(i = 0; i < s->avctx->thread_count; i++)
  1462. if(!h->thread_context[i]->s.obmc_scratchpad)
  1463. h->thread_context[i]->s.obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
  1464. /* some macroblocks will be accessed before they're available */
  1465. if(FRAME_MBAFF || s->avctx->thread_count > 1)
  1466. memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(*h->slice_table));
  1467. // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
  1468. // We mark the current picture as non-reference after allocating it, so
  1469. // that if we break out due to an error it can be released automatically
  1470. // in the next MPV_frame_start().
  1471. // SVQ3 as well as most other codecs have only last/next/current and thus
  1472. // get released even with set reference, besides SVQ3 and others do not
  1473. // mark frames as reference later "naturally".
  1474. if(s->codec_id != CODEC_ID_SVQ3)
  1475. s->current_picture_ptr->reference= 0;
  1476. s->current_picture_ptr->field_poc[0]=
  1477. s->current_picture_ptr->field_poc[1]= INT_MAX;
  1478. assert(s->current_picture_ptr->long_ref==0);
  1479. return 0;
  1480. }
  1481. 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){
  1482. MpegEncContext * const s = &h->s;
  1483. int i;
  1484. int step = 1;
  1485. int offset = 1;
  1486. int uvoffset= 1;
  1487. int top_idx = 1;
  1488. int skiplast= 0;
  1489. src_y -= linesize;
  1490. src_cb -= uvlinesize;
  1491. src_cr -= uvlinesize;
  1492. if(!simple && FRAME_MBAFF){
  1493. if(s->mb_y&1){
  1494. offset = MB_MBAFF ? 1 : 17;
  1495. uvoffset= MB_MBAFF ? 1 : 9;
  1496. if(!MB_MBAFF){
  1497. *(uint64_t*)(h->top_borders[0][s->mb_x]+ 0)= *(uint64_t*)(src_y + 15*linesize);
  1498. *(uint64_t*)(h->top_borders[0][s->mb_x]+ 8)= *(uint64_t*)(src_y +8+15*linesize);
  1499. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1500. *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+7*uvlinesize);
  1501. *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+7*uvlinesize);
  1502. }
  1503. }
  1504. }else{
  1505. if(!MB_MBAFF){
  1506. h->left_border[0]= h->top_borders[0][s->mb_x][15];
  1507. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1508. h->left_border[34 ]= h->top_borders[0][s->mb_x][16+7 ];
  1509. h->left_border[34+18]= h->top_borders[0][s->mb_x][16+8+7];
  1510. }
  1511. skiplast= 1;
  1512. }
  1513. offset =
  1514. uvoffset=
  1515. top_idx = MB_MBAFF ? 0 : 1;
  1516. }
  1517. step= MB_MBAFF ? 2 : 1;
  1518. }
  1519. // There are two lines saved, the line above the the top macroblock of a pair,
  1520. // and the line above the bottom macroblock
  1521. h->left_border[offset]= h->top_borders[top_idx][s->mb_x][15];
  1522. for(i=1; i<17 - skiplast; i++){
  1523. h->left_border[offset+i*step]= src_y[15+i* linesize];
  1524. }
  1525. *(uint64_t*)(h->top_borders[top_idx][s->mb_x]+0)= *(uint64_t*)(src_y + 16*linesize);
  1526. *(uint64_t*)(h->top_borders[top_idx][s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize);
  1527. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1528. h->left_border[uvoffset+34 ]= h->top_borders[top_idx][s->mb_x][16+7];
  1529. h->left_border[uvoffset+34+18]= h->top_borders[top_idx][s->mb_x][24+7];
  1530. for(i=1; i<9 - skiplast; i++){
  1531. h->left_border[uvoffset+34 +i*step]= src_cb[7+i*uvlinesize];
  1532. h->left_border[uvoffset+34+18+i*step]= src_cr[7+i*uvlinesize];
  1533. }
  1534. *(uint64_t*)(h->top_borders[top_idx][s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize);
  1535. *(uint64_t*)(h->top_borders[top_idx][s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize);
  1536. }
  1537. }
  1538. 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){
  1539. MpegEncContext * const s = &h->s;
  1540. int temp8, i;
  1541. uint64_t temp64;
  1542. int deblock_left;
  1543. int deblock_top;
  1544. int mb_xy;
  1545. int step = 1;
  1546. int offset = 1;
  1547. int uvoffset= 1;
  1548. int top_idx = 1;
  1549. if(!simple && FRAME_MBAFF){
  1550. if(s->mb_y&1){
  1551. offset = MB_MBAFF ? 1 : 17;
  1552. uvoffset= MB_MBAFF ? 1 : 9;
  1553. }else{
  1554. offset =
  1555. uvoffset=
  1556. top_idx = MB_MBAFF ? 0 : 1;
  1557. }
  1558. step= MB_MBAFF ? 2 : 1;
  1559. }
  1560. if(h->deblocking_filter == 2) {
  1561. mb_xy = h->mb_xy;
  1562. deblock_left = h->slice_table[mb_xy] == h->slice_table[mb_xy - 1];
  1563. deblock_top = h->slice_table[mb_xy] == h->slice_table[h->top_mb_xy];
  1564. } else {
  1565. deblock_left = (s->mb_x > 0);
  1566. deblock_top = (s->mb_y > !!MB_FIELD);
  1567. }
  1568. src_y -= linesize + 1;
  1569. src_cb -= uvlinesize + 1;
  1570. src_cr -= uvlinesize + 1;
  1571. #define XCHG(a,b,t,xchg)\
  1572. t= a;\
  1573. if(xchg)\
  1574. a= b;\
  1575. b= t;
  1576. if(deblock_left){
  1577. for(i = !deblock_top; i<16; i++){
  1578. XCHG(h->left_border[offset+i*step], src_y [i* linesize], temp8, xchg);
  1579. }
  1580. XCHG(h->left_border[offset+i*step], src_y [i* linesize], temp8, 1);
  1581. }
  1582. if(deblock_top){
  1583. XCHG(*(uint64_t*)(h->top_borders[top_idx][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
  1584. XCHG(*(uint64_t*)(h->top_borders[top_idx][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
  1585. if(s->mb_x+1 < s->mb_width){
  1586. XCHG(*(uint64_t*)(h->top_borders[top_idx][s->mb_x+1]), *(uint64_t*)(src_y +17), temp64, 1);
  1587. }
  1588. }
  1589. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1590. if(deblock_left){
  1591. for(i = !deblock_top; i<8; i++){
  1592. XCHG(h->left_border[uvoffset+34 +i*step], src_cb[i*uvlinesize], temp8, xchg);
  1593. XCHG(h->left_border[uvoffset+34+18+i*step], src_cr[i*uvlinesize], temp8, xchg);
  1594. }
  1595. XCHG(h->left_border[uvoffset+34 +i*step], src_cb[i*uvlinesize], temp8, 1);
  1596. XCHG(h->left_border[uvoffset+34+18+i*step], src_cr[i*uvlinesize], temp8, 1);
  1597. }
  1598. if(deblock_top){
  1599. XCHG(*(uint64_t*)(h->top_borders[top_idx][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
  1600. XCHG(*(uint64_t*)(h->top_borders[top_idx][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
  1601. }
  1602. }
  1603. }
  1604. static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple){
  1605. MpegEncContext * const s = &h->s;
  1606. const int mb_x= s->mb_x;
  1607. const int mb_y= s->mb_y;
  1608. const int mb_xy= h->mb_xy;
  1609. const int mb_type= s->current_picture.mb_type[mb_xy];
  1610. uint8_t *dest_y, *dest_cb, *dest_cr;
  1611. int linesize, uvlinesize /*dct_offset*/;
  1612. int i;
  1613. int *block_offset = &h->block_offset[0];
  1614. const int transform_bypass = !simple && (s->qscale == 0 && h->sps.transform_bypass);
  1615. /* is_h264 should always be true if SVQ3 is disabled. */
  1616. const int is_h264 = !CONFIG_SVQ3_DECODER || simple || s->codec_id == CODEC_ID_H264;
  1617. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  1618. void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  1619. dest_y = s->current_picture.data[0] + (mb_x + mb_y * s->linesize ) * 16;
  1620. dest_cb = s->current_picture.data[1] + (mb_x + mb_y * s->uvlinesize) * 8;
  1621. dest_cr = s->current_picture.data[2] + (mb_x + mb_y * s->uvlinesize) * 8;
  1622. s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + 64, s->linesize, 4);
  1623. s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + 64, dest_cr - dest_cb, 2);
  1624. if (!simple && MB_FIELD) {
  1625. linesize = h->mb_linesize = s->linesize * 2;
  1626. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  1627. block_offset = &h->block_offset[24];
  1628. if(mb_y&1){ //FIXME move out of this function?
  1629. dest_y -= s->linesize*15;
  1630. dest_cb-= s->uvlinesize*7;
  1631. dest_cr-= s->uvlinesize*7;
  1632. }
  1633. if(FRAME_MBAFF) {
  1634. int list;
  1635. for(list=0; list<h->list_count; list++){
  1636. if(!USES_LIST(mb_type, list))
  1637. continue;
  1638. if(IS_16X16(mb_type)){
  1639. int8_t *ref = &h->ref_cache[list][scan8[0]];
  1640. fill_rectangle(ref, 4, 4, 8, (16+*ref)^(s->mb_y&1), 1);
  1641. }else{
  1642. for(i=0; i<16; i+=4){
  1643. int ref = h->ref_cache[list][scan8[i]];
  1644. if(ref >= 0)
  1645. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, (16+ref)^(s->mb_y&1), 1);
  1646. }
  1647. }
  1648. }
  1649. }
  1650. } else {
  1651. linesize = h->mb_linesize = s->linesize;
  1652. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  1653. // dct_offset = s->linesize * 16;
  1654. }
  1655. if (!simple && IS_INTRA_PCM(mb_type)) {
  1656. for (i=0; i<16; i++) {
  1657. memcpy(dest_y + i* linesize, h->mb + i*8, 16);
  1658. }
  1659. for (i=0; i<8; i++) {
  1660. memcpy(dest_cb+ i*uvlinesize, h->mb + 128 + i*4, 8);
  1661. memcpy(dest_cr+ i*uvlinesize, h->mb + 160 + i*4, 8);
  1662. }
  1663. } else {
  1664. if(IS_INTRA(mb_type)){
  1665. if(h->deblocking_filter)
  1666. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1, simple);
  1667. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1668. h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  1669. h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  1670. }
  1671. if(IS_INTRA4x4(mb_type)){
  1672. if(simple || !s->encoding){
  1673. if(IS_8x8DCT(mb_type)){
  1674. if(transform_bypass){
  1675. idct_dc_add =
  1676. idct_add = s->dsp.add_pixels8;
  1677. }else{
  1678. idct_dc_add = s->dsp.h264_idct8_dc_add;
  1679. idct_add = s->dsp.h264_idct8_add;
  1680. }
  1681. for(i=0; i<16; i+=4){
  1682. uint8_t * const ptr= dest_y + block_offset[i];
  1683. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  1684. if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
  1685. h->hpc.pred8x8l_add[dir](ptr, h->mb + i*16, linesize);
  1686. }else{
  1687. const int nnz = h->non_zero_count_cache[ scan8[i] ];
  1688. h->hpc.pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
  1689. (h->topright_samples_available<<i)&0x4000, linesize);
  1690. if(nnz){
  1691. if(nnz == 1 && h->mb[i*16])
  1692. idct_dc_add(ptr, h->mb + i*16, linesize);
  1693. else
  1694. idct_add (ptr, h->mb + i*16, linesize);
  1695. }
  1696. }
  1697. }
  1698. }else{
  1699. if(transform_bypass){
  1700. idct_dc_add =
  1701. idct_add = s->dsp.add_pixels4;
  1702. }else{
  1703. idct_dc_add = s->dsp.h264_idct_dc_add;
  1704. idct_add = s->dsp.h264_idct_add;
  1705. }
  1706. for(i=0; i<16; i++){
  1707. uint8_t * const ptr= dest_y + block_offset[i];
  1708. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  1709. if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
  1710. h->hpc.pred4x4_add[dir](ptr, h->mb + i*16, linesize);
  1711. }else{
  1712. uint8_t *topright;
  1713. int nnz, tr;
  1714. if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
  1715. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  1716. assert(mb_y || linesize <= block_offset[i]);
  1717. if(!topright_avail){
  1718. tr= ptr[3 - linesize]*0x01010101;
  1719. topright= (uint8_t*) &tr;
  1720. }else
  1721. topright= ptr + 4 - linesize;
  1722. }else
  1723. topright= NULL;
  1724. h->hpc.pred4x4[ dir ](ptr, topright, linesize);
  1725. nnz = h->non_zero_count_cache[ scan8[i] ];
  1726. if(nnz){
  1727. if(is_h264){
  1728. if(nnz == 1 && h->mb[i*16])
  1729. idct_dc_add(ptr, h->mb + i*16, linesize);
  1730. else
  1731. idct_add (ptr, h->mb + i*16, linesize);
  1732. }else
  1733. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
  1734. }
  1735. }
  1736. }
  1737. }
  1738. }
  1739. }else{
  1740. h->hpc.pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  1741. if(is_h264){
  1742. if(!transform_bypass)
  1743. h264_luma_dc_dequant_idct_c(h->mb, s->qscale, h->dequant4_coeff[0][s->qscale][0]);
  1744. }else
  1745. svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
  1746. }
  1747. if(h->deblocking_filter)
  1748. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0, simple);
  1749. }else if(is_h264){
  1750. hl_motion(h, dest_y, dest_cb, dest_cr,
  1751. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  1752. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  1753. s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
  1754. }
  1755. if(!IS_INTRA4x4(mb_type)){
  1756. if(is_h264){
  1757. if(IS_INTRA16x16(mb_type)){
  1758. if(transform_bypass){
  1759. if(h->sps.profile_idc==244 && (h->intra16x16_pred_mode==VERT_PRED8x8 || h->intra16x16_pred_mode==HOR_PRED8x8)){
  1760. h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset, h->mb, linesize);
  1761. }else{
  1762. for(i=0; i<16; i++){
  1763. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16])
  1764. s->dsp.add_pixels4(dest_y + block_offset[i], h->mb + i*16, linesize);
  1765. }
  1766. }
  1767. }else{
  1768. s->dsp.h264_idct_add16intra(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
  1769. }
  1770. }else if(h->cbp&15){
  1771. if(transform_bypass){
  1772. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  1773. idct_add= IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
  1774. for(i=0; i<16; i+=di){
  1775. if(h->non_zero_count_cache[ scan8[i] ]){
  1776. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  1777. }
  1778. }
  1779. }else{
  1780. if(IS_8x8DCT(mb_type)){
  1781. s->dsp.h264_idct8_add4(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
  1782. }else{
  1783. s->dsp.h264_idct_add16(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
  1784. }
  1785. }
  1786. }
  1787. }else{
  1788. for(i=0; i<16; i++){
  1789. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  1790. uint8_t * const ptr= dest_y + block_offset[i];
  1791. svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  1792. }
  1793. }
  1794. }
  1795. }
  1796. if((simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)) && (h->cbp&0x30)){
  1797. uint8_t *dest[2] = {dest_cb, dest_cr};
  1798. if(transform_bypass){
  1799. if(IS_INTRA(mb_type) && h->sps.profile_idc==244 && (h->chroma_pred_mode==VERT_PRED8x8 || h->chroma_pred_mode==HOR_PRED8x8)){
  1800. h->hpc.pred8x8_add[h->chroma_pred_mode](dest[0], block_offset + 16, h->mb + 16*16, uvlinesize);
  1801. h->hpc.pred8x8_add[h->chroma_pred_mode](dest[1], block_offset + 20, h->mb + 20*16, uvlinesize);
  1802. }else{
  1803. idct_add = s->dsp.add_pixels4;
  1804. for(i=16; i<16+8; i++){
  1805. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16])
  1806. idct_add (dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  1807. }
  1808. }
  1809. }else{
  1810. 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]);
  1811. 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]);
  1812. if(is_h264){
  1813. idct_add = s->dsp.h264_idct_add;
  1814. idct_dc_add = s->dsp.h264_idct_dc_add;
  1815. for(i=16; i<16+8; i++){
  1816. if(h->non_zero_count_cache[ scan8[i] ])
  1817. idct_add (dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  1818. else if(h->mb[i*16])
  1819. idct_dc_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  1820. }
  1821. }else{
  1822. for(i=16; i<16+8; i++){
  1823. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  1824. uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
  1825. svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, ff_h264_chroma_qp[s->qscale + 12] - 12, 2);
  1826. }
  1827. }
  1828. }
  1829. }
  1830. }
  1831. }
  1832. if(h->cbp || IS_INTRA(mb_type))
  1833. s->dsp.clear_blocks(h->mb);
  1834. if(h->deblocking_filter) {
  1835. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, simple);
  1836. fill_caches(h, mb_type, 1); //FIXME don't fill stuff which isn't used by filter_mb
  1837. h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy]);
  1838. h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy]);
  1839. if (!simple && FRAME_MBAFF) {
  1840. ff_h264_filter_mb (h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  1841. } else {
  1842. ff_h264_filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  1843. }
  1844. }
  1845. }
  1846. /**
  1847. * Process a macroblock; this case avoids checks for expensive uncommon cases.
  1848. */
  1849. static void hl_decode_mb_simple(H264Context *h){
  1850. hl_decode_mb_internal(h, 1);
  1851. }
  1852. /**
  1853. * Process a macroblock; this handles edge cases, such as interlacing.
  1854. */
  1855. static void av_noinline hl_decode_mb_complex(H264Context *h){
  1856. hl_decode_mb_internal(h, 0);
  1857. }
  1858. void ff_h264_hl_decode_mb(H264Context *h){
  1859. MpegEncContext * const s = &h->s;
  1860. const int mb_xy= h->mb_xy;
  1861. const int mb_type= s->current_picture.mb_type[mb_xy];
  1862. int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || s->qscale == 0;
  1863. if (is_complex)
  1864. hl_decode_mb_complex(h);
  1865. else hl_decode_mb_simple(h);
  1866. }
  1867. static int pred_weight_table(H264Context *h){
  1868. MpegEncContext * const s = &h->s;
  1869. int list, i;
  1870. int luma_def, chroma_def;
  1871. h->use_weight= 0;
  1872. h->use_weight_chroma= 0;
  1873. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  1874. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  1875. luma_def = 1<<h->luma_log2_weight_denom;
  1876. chroma_def = 1<<h->chroma_log2_weight_denom;
  1877. for(list=0; list<2; list++){
  1878. h->luma_weight_flag[list] = 0;
  1879. h->chroma_weight_flag[list] = 0;
  1880. for(i=0; i<h->ref_count[list]; i++){
  1881. int luma_weight_flag, chroma_weight_flag;
  1882. luma_weight_flag= get_bits1(&s->gb);
  1883. if(luma_weight_flag){
  1884. h->luma_weight[list][i]= get_se_golomb(&s->gb);
  1885. h->luma_offset[list][i]= get_se_golomb(&s->gb);
  1886. if( h->luma_weight[list][i] != luma_def
  1887. || h->luma_offset[list][i] != 0) {
  1888. h->use_weight= 1;
  1889. h->luma_weight_flag[list]= 1;
  1890. }
  1891. }else{
  1892. h->luma_weight[list][i]= luma_def;
  1893. h->luma_offset[list][i]= 0;
  1894. }
  1895. if(CHROMA){
  1896. chroma_weight_flag= get_bits1(&s->gb);
  1897. if(chroma_weight_flag){
  1898. int j;
  1899. for(j=0; j<2; j++){
  1900. h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
  1901. h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
  1902. if( h->chroma_weight[list][i][j] != chroma_def
  1903. || h->chroma_offset[list][i][j] != 0) {
  1904. h->use_weight_chroma= 1;
  1905. h->chroma_weight_flag[list]= 1;
  1906. }
  1907. }
  1908. }else{
  1909. int j;
  1910. for(j=0; j<2; j++){
  1911. h->chroma_weight[list][i][j]= chroma_def;
  1912. h->chroma_offset[list][i][j]= 0;
  1913. }
  1914. }
  1915. }
  1916. }
  1917. if(h->slice_type_nos != FF_B_TYPE) break;
  1918. }
  1919. h->use_weight= h->use_weight || h->use_weight_chroma;
  1920. return 0;
  1921. }
  1922. static void implicit_weight_table(H264Context *h){
  1923. MpegEncContext * const s = &h->s;
  1924. int ref0, ref1, i;
  1925. int cur_poc = s->current_picture_ptr->poc;
  1926. for (i = 0; i < 2; i++) {
  1927. h->luma_weight_flag[i] = 0;
  1928. h->chroma_weight_flag[i] = 0;
  1929. }
  1930. if( h->ref_count[0] == 1 && h->ref_count[1] == 1
  1931. && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
  1932. h->use_weight= 0;
  1933. h->use_weight_chroma= 0;
  1934. return;
  1935. }
  1936. h->use_weight= 2;
  1937. h->use_weight_chroma= 2;
  1938. h->luma_log2_weight_denom= 5;
  1939. h->chroma_log2_weight_denom= 5;
  1940. for(ref0=0; ref0 < h->ref_count[0]; ref0++){
  1941. int poc0 = h->ref_list[0][ref0].poc;
  1942. for(ref1=0; ref1 < h->ref_count[1]; ref1++){
  1943. int poc1 = h->ref_list[1][ref1].poc;
  1944. int td = av_clip(poc1 - poc0, -128, 127);
  1945. if(td){
  1946. int tb = av_clip(cur_poc - poc0, -128, 127);
  1947. int tx = (16384 + (FFABS(td) >> 1)) / td;
  1948. int dist_scale_factor = av_clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
  1949. if(dist_scale_factor < -64 || dist_scale_factor > 128)
  1950. h->implicit_weight[ref0][ref1] = 32;
  1951. else
  1952. h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor;
  1953. }else
  1954. h->implicit_weight[ref0][ref1] = 32;
  1955. }
  1956. }
  1957. }
  1958. /**
  1959. * instantaneous decoder refresh.
  1960. */
  1961. static void idr(H264Context *h){
  1962. ff_h264_remove_all_refs(h);
  1963. h->prev_frame_num= 0;
  1964. h->prev_frame_num_offset= 0;
  1965. h->prev_poc_msb=
  1966. h->prev_poc_lsb= 0;
  1967. }
  1968. /* forget old pics after a seek */
  1969. static void flush_dpb(AVCodecContext *avctx){
  1970. H264Context *h= avctx->priv_data;
  1971. int i;
  1972. for(i=0; i<MAX_DELAYED_PIC_COUNT; i++) {
  1973. if(h->delayed_pic[i])
  1974. h->delayed_pic[i]->reference= 0;
  1975. h->delayed_pic[i]= NULL;
  1976. }
  1977. h->outputed_poc= INT_MIN;
  1978. h->prev_interlaced_frame = 1;
  1979. idr(h);
  1980. if(h->s.current_picture_ptr)
  1981. h->s.current_picture_ptr->reference= 0;
  1982. h->s.first_field= 0;
  1983. ff_h264_reset_sei(h);
  1984. ff_mpeg_flush(avctx);
  1985. }
  1986. static int init_poc(H264Context *h){
  1987. MpegEncContext * const s = &h->s;
  1988. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  1989. int field_poc[2];
  1990. Picture *cur = s->current_picture_ptr;
  1991. h->frame_num_offset= h->prev_frame_num_offset;
  1992. if(h->frame_num < h->prev_frame_num)
  1993. h->frame_num_offset += max_frame_num;
  1994. if(h->sps.poc_type==0){
  1995. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  1996. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  1997. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  1998. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  1999. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  2000. else
  2001. h->poc_msb = h->prev_poc_msb;
  2002. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  2003. field_poc[0] =
  2004. field_poc[1] = h->poc_msb + h->poc_lsb;
  2005. if(s->picture_structure == PICT_FRAME)
  2006. field_poc[1] += h->delta_poc_bottom;
  2007. }else if(h->sps.poc_type==1){
  2008. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  2009. int i;
  2010. if(h->sps.poc_cycle_length != 0)
  2011. abs_frame_num = h->frame_num_offset + h->frame_num;
  2012. else
  2013. abs_frame_num = 0;
  2014. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  2015. abs_frame_num--;
  2016. expected_delta_per_poc_cycle = 0;
  2017. for(i=0; i < h->sps.poc_cycle_length; i++)
  2018. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  2019. if(abs_frame_num > 0){
  2020. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  2021. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  2022. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  2023. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  2024. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  2025. } else
  2026. expectedpoc = 0;
  2027. if(h->nal_ref_idc == 0)
  2028. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  2029. field_poc[0] = expectedpoc + h->delta_poc[0];
  2030. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  2031. if(s->picture_structure == PICT_FRAME)
  2032. field_poc[1] += h->delta_poc[1];
  2033. }else{
  2034. int poc= 2*(h->frame_num_offset + h->frame_num);
  2035. if(!h->nal_ref_idc)
  2036. poc--;
  2037. field_poc[0]= poc;
  2038. field_poc[1]= poc;
  2039. }
  2040. if(s->picture_structure != PICT_BOTTOM_FIELD)
  2041. s->current_picture_ptr->field_poc[0]= field_poc[0];
  2042. if(s->picture_structure != PICT_TOP_FIELD)
  2043. s->current_picture_ptr->field_poc[1]= field_poc[1];
  2044. cur->poc= FFMIN(cur->field_poc[0], cur->field_poc[1]);
  2045. return 0;
  2046. }
  2047. /**
  2048. * initialize scan tables
  2049. */
  2050. static void init_scan_tables(H264Context *h){
  2051. MpegEncContext * const s = &h->s;
  2052. int i;
  2053. if(s->dsp.h264_idct_add == ff_h264_idct_add_c){ //FIXME little ugly
  2054. memcpy(h->zigzag_scan, zigzag_scan, 16*sizeof(uint8_t));
  2055. memcpy(h-> field_scan, field_scan, 16*sizeof(uint8_t));
  2056. }else{
  2057. for(i=0; i<16; i++){
  2058. #define T(x) (x>>2) | ((x<<2) & 0xF)
  2059. h->zigzag_scan[i] = T(zigzag_scan[i]);
  2060. h-> field_scan[i] = T( field_scan[i]);
  2061. #undef T
  2062. }
  2063. }
  2064. if(s->dsp.h264_idct8_add == ff_h264_idct8_add_c){
  2065. memcpy(h->zigzag_scan8x8, ff_zigzag_direct, 64*sizeof(uint8_t));
  2066. memcpy(h->zigzag_scan8x8_cavlc, zigzag_scan8x8_cavlc, 64*sizeof(uint8_t));
  2067. memcpy(h->field_scan8x8, field_scan8x8, 64*sizeof(uint8_t));
  2068. memcpy(h->field_scan8x8_cavlc, field_scan8x8_cavlc, 64*sizeof(uint8_t));
  2069. }else{
  2070. for(i=0; i<64; i++){
  2071. #define T(x) (x>>3) | ((x&7)<<3)
  2072. h->zigzag_scan8x8[i] = T(ff_zigzag_direct[i]);
  2073. h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
  2074. h->field_scan8x8[i] = T(field_scan8x8[i]);
  2075. h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
  2076. #undef T
  2077. }
  2078. }
  2079. if(h->sps.transform_bypass){ //FIXME same ugly
  2080. h->zigzag_scan_q0 = zigzag_scan;
  2081. h->zigzag_scan8x8_q0 = ff_zigzag_direct;
  2082. h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
  2083. h->field_scan_q0 = field_scan;
  2084. h->field_scan8x8_q0 = field_scan8x8;
  2085. h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
  2086. }else{
  2087. h->zigzag_scan_q0 = h->zigzag_scan;
  2088. h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
  2089. h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
  2090. h->field_scan_q0 = h->field_scan;
  2091. h->field_scan8x8_q0 = h->field_scan8x8;
  2092. h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
  2093. }
  2094. }
  2095. static void field_end(H264Context *h){
  2096. MpegEncContext * const s = &h->s;
  2097. AVCodecContext * const avctx= s->avctx;
  2098. s->mb_y= 0;
  2099. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
  2100. s->current_picture_ptr->pict_type= s->pict_type;
  2101. if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  2102. ff_vdpau_h264_set_reference_frames(s);
  2103. if(!s->dropable) {
  2104. ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  2105. h->prev_poc_msb= h->poc_msb;
  2106. h->prev_poc_lsb= h->poc_lsb;
  2107. }
  2108. h->prev_frame_num_offset= h->frame_num_offset;
  2109. h->prev_frame_num= h->frame_num;
  2110. if (avctx->hwaccel) {
  2111. if (avctx->hwaccel->end_frame(avctx) < 0)
  2112. av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
  2113. }
  2114. if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  2115. ff_vdpau_h264_picture_complete(s);
  2116. /*
  2117. * FIXME: Error handling code does not seem to support interlaced
  2118. * when slices span multiple rows
  2119. * The ff_er_add_slice calls don't work right for bottom
  2120. * fields; they cause massive erroneous error concealing
  2121. * Error marking covers both fields (top and bottom).
  2122. * This causes a mismatched s->error_count
  2123. * and a bad error table. Further, the error count goes to
  2124. * INT_MAX when called for bottom field, because mb_y is
  2125. * past end by one (callers fault) and resync_mb_y != 0
  2126. * causes problems for the first MB line, too.
  2127. */
  2128. if (!FIELD_PICTURE)
  2129. ff_er_frame_end(s);
  2130. MPV_frame_end(s);
  2131. h->current_slice=0;
  2132. }
  2133. /**
  2134. * Replicates H264 "master" context to thread contexts.
  2135. */
  2136. static void clone_slice(H264Context *dst, H264Context *src)
  2137. {
  2138. memcpy(dst->block_offset, src->block_offset, sizeof(dst->block_offset));
  2139. dst->s.current_picture_ptr = src->s.current_picture_ptr;
  2140. dst->s.current_picture = src->s.current_picture;
  2141. dst->s.linesize = src->s.linesize;
  2142. dst->s.uvlinesize = src->s.uvlinesize;
  2143. dst->s.first_field = src->s.first_field;
  2144. dst->prev_poc_msb = src->prev_poc_msb;
  2145. dst->prev_poc_lsb = src->prev_poc_lsb;
  2146. dst->prev_frame_num_offset = src->prev_frame_num_offset;
  2147. dst->prev_frame_num = src->prev_frame_num;
  2148. dst->short_ref_count = src->short_ref_count;
  2149. memcpy(dst->short_ref, src->short_ref, sizeof(dst->short_ref));
  2150. memcpy(dst->long_ref, src->long_ref, sizeof(dst->long_ref));
  2151. memcpy(dst->default_ref_list, src->default_ref_list, sizeof(dst->default_ref_list));
  2152. memcpy(dst->ref_list, src->ref_list, sizeof(dst->ref_list));
  2153. memcpy(dst->dequant4_coeff, src->dequant4_coeff, sizeof(src->dequant4_coeff));
  2154. memcpy(dst->dequant8_coeff, src->dequant8_coeff, sizeof(src->dequant8_coeff));
  2155. }
  2156. /**
  2157. * decodes a slice header.
  2158. * This will also call MPV_common_init() and frame_start() as needed.
  2159. *
  2160. * @param h h264context
  2161. * @param h0 h264 master context (differs from 'h' when doing sliced based parallel decoding)
  2162. *
  2163. * @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded
  2164. */
  2165. static int decode_slice_header(H264Context *h, H264Context *h0){
  2166. MpegEncContext * const s = &h->s;
  2167. MpegEncContext * const s0 = &h0->s;
  2168. unsigned int first_mb_in_slice;
  2169. unsigned int pps_id;
  2170. int num_ref_idx_active_override_flag;
  2171. unsigned int slice_type, tmp, i, j;
  2172. int default_ref_list_done = 0;
  2173. int last_pic_structure;
  2174. s->dropable= h->nal_ref_idc == 0;
  2175. if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !h->nal_ref_idc){
  2176. s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
  2177. s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
  2178. }else{
  2179. s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
  2180. s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
  2181. }
  2182. first_mb_in_slice= get_ue_golomb(&s->gb);
  2183. if(first_mb_in_slice == 0){ //FIXME better field boundary detection
  2184. if(h0->current_slice && FIELD_PICTURE){
  2185. field_end(h);
  2186. }
  2187. h0->current_slice = 0;
  2188. if (!s0->first_field)
  2189. s->current_picture_ptr= NULL;
  2190. }
  2191. slice_type= get_ue_golomb_31(&s->gb);
  2192. if(slice_type > 9){
  2193. 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);
  2194. return -1;
  2195. }
  2196. if(slice_type > 4){
  2197. slice_type -= 5;
  2198. h->slice_type_fixed=1;
  2199. }else
  2200. h->slice_type_fixed=0;
  2201. slice_type= golomb_to_pict_type[ slice_type ];
  2202. if (slice_type == FF_I_TYPE
  2203. || (h0->current_slice != 0 && slice_type == h0->last_slice_type) ) {
  2204. default_ref_list_done = 1;
  2205. }
  2206. h->slice_type= slice_type;
  2207. h->slice_type_nos= slice_type & 3;
  2208. s->pict_type= h->slice_type; // to make a few old functions happy, it's wrong though
  2209. if (s->pict_type == FF_B_TYPE && s0->last_picture_ptr == NULL) {
  2210. av_log(h->s.avctx, AV_LOG_ERROR,
  2211. "B picture before any references, skipping\n");
  2212. return -1;
  2213. }
  2214. pps_id= get_ue_golomb(&s->gb);
  2215. if(pps_id>=MAX_PPS_COUNT){
  2216. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  2217. return -1;
  2218. }
  2219. if(!h0->pps_buffers[pps_id]) {
  2220. av_log(h->s.avctx, AV_LOG_ERROR, "non-existing PPS %u referenced\n", pps_id);
  2221. return -1;
  2222. }
  2223. h->pps= *h0->pps_buffers[pps_id];
  2224. if(!h0->sps_buffers[h->pps.sps_id]) {
  2225. av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS %u referenced\n", h->pps.sps_id);
  2226. return -1;
  2227. }
  2228. h->sps = *h0->sps_buffers[h->pps.sps_id];
  2229. if(h == h0 && h->dequant_coeff_pps != pps_id){
  2230. h->dequant_coeff_pps = pps_id;
  2231. init_dequant_tables(h);
  2232. }
  2233. s->mb_width= h->sps.mb_width;
  2234. s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  2235. h->b_stride= s->mb_width*4;
  2236. h->b8_stride= s->mb_width*2;
  2237. s->width = 16*s->mb_width - 2*FFMIN(h->sps.crop_right, 7);
  2238. if(h->sps.frame_mbs_only_flag)
  2239. s->height= 16*s->mb_height - 2*FFMIN(h->sps.crop_bottom, 7);
  2240. else
  2241. s->height= 16*s->mb_height - 4*FFMIN(h->sps.crop_bottom, 3);
  2242. if (s->context_initialized
  2243. && ( s->width != s->avctx->width || s->height != s->avctx->height)) {
  2244. if(h != h0)
  2245. return -1; // width / height changed during parallelized decoding
  2246. free_tables(h);
  2247. flush_dpb(s->avctx);
  2248. MPV_common_end(s);
  2249. }
  2250. if (!s->context_initialized) {
  2251. if(h != h0)
  2252. return -1; // we cant (re-)initialize context during parallel decoding
  2253. avcodec_set_dimensions(s->avctx, s->width, s->height);
  2254. s->avctx->sample_aspect_ratio= h->sps.sar;
  2255. if(!s->avctx->sample_aspect_ratio.den)
  2256. s->avctx->sample_aspect_ratio.den = 1;
  2257. if(h->sps.video_signal_type_present_flag){
  2258. s->avctx->color_range = h->sps.full_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
  2259. if(h->sps.colour_description_present_flag){
  2260. s->avctx->color_primaries = h->sps.color_primaries;
  2261. s->avctx->color_trc = h->sps.color_trc;
  2262. s->avctx->colorspace = h->sps.colorspace;
  2263. }
  2264. }
  2265. if(h->sps.timing_info_present_flag){
  2266. s->avctx->time_base= (AVRational){h->sps.num_units_in_tick, h->sps.time_scale};
  2267. if(h->x264_build > 0 && h->x264_build < 44)
  2268. s->avctx->time_base.den *= 2;
  2269. av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
  2270. s->avctx->time_base.num, s->avctx->time_base.den, 1<<30);
  2271. }
  2272. s->avctx->pix_fmt = s->avctx->get_format(s->avctx, s->avctx->codec->pix_fmts);
  2273. s->avctx->hwaccel = ff_find_hwaccel(s->avctx->codec->id, s->avctx->pix_fmt);
  2274. if (MPV_common_init(s) < 0)
  2275. return -1;
  2276. s->first_field = 0;
  2277. h->prev_interlaced_frame = 1;
  2278. init_scan_tables(h);
  2279. ff_h264_alloc_tables(h);
  2280. for(i = 1; i < s->avctx->thread_count; i++) {
  2281. H264Context *c;
  2282. c = h->thread_context[i] = av_malloc(sizeof(H264Context));
  2283. memcpy(c, h->s.thread_context[i], sizeof(MpegEncContext));
  2284. memset(&c->s + 1, 0, sizeof(H264Context) - sizeof(MpegEncContext));
  2285. c->sps = h->sps;
  2286. c->pps = h->pps;
  2287. init_scan_tables(c);
  2288. clone_tables(c, h);
  2289. }
  2290. for(i = 0; i < s->avctx->thread_count; i++)
  2291. if(context_init(h->thread_context[i]) < 0)
  2292. return -1;
  2293. }
  2294. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  2295. h->mb_mbaff = 0;
  2296. h->mb_aff_frame = 0;
  2297. last_pic_structure = s0->picture_structure;
  2298. if(h->sps.frame_mbs_only_flag){
  2299. s->picture_structure= PICT_FRAME;
  2300. }else{
  2301. if(get_bits1(&s->gb)) { //field_pic_flag
  2302. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  2303. } else {
  2304. s->picture_structure= PICT_FRAME;
  2305. h->mb_aff_frame = h->sps.mb_aff;
  2306. }
  2307. }
  2308. h->mb_field_decoding_flag= s->picture_structure != PICT_FRAME;
  2309. if(h0->current_slice == 0){
  2310. while(h->frame_num != h->prev_frame_num &&
  2311. h->frame_num != (h->prev_frame_num+1)%(1<<h->sps.log2_max_frame_num)){
  2312. av_log(NULL, AV_LOG_DEBUG, "Frame num gap %d %d\n", h->frame_num, h->prev_frame_num);
  2313. if (ff_h264_frame_start(h) < 0)
  2314. return -1;
  2315. h->prev_frame_num++;
  2316. h->prev_frame_num %= 1<<h->sps.log2_max_frame_num;
  2317. s->current_picture_ptr->frame_num= h->prev_frame_num;
  2318. ff_h264_execute_ref_pic_marking(h, NULL, 0);
  2319. }
  2320. /* See if we have a decoded first field looking for a pair... */
  2321. if (s0->first_field) {
  2322. assert(s0->current_picture_ptr);
  2323. assert(s0->current_picture_ptr->data[0]);
  2324. assert(s0->current_picture_ptr->reference != DELAYED_PIC_REF);
  2325. /* figure out if we have a complementary field pair */
  2326. if (!FIELD_PICTURE || s->picture_structure == last_pic_structure) {
  2327. /*
  2328. * Previous field is unmatched. Don't display it, but let it
  2329. * remain for reference if marked as such.
  2330. */
  2331. s0->current_picture_ptr = NULL;
  2332. s0->first_field = FIELD_PICTURE;
  2333. } else {
  2334. if (h->nal_ref_idc &&
  2335. s0->current_picture_ptr->reference &&
  2336. s0->current_picture_ptr->frame_num != h->frame_num) {
  2337. /*
  2338. * This and previous field were reference, but had
  2339. * different frame_nums. Consider this field first in
  2340. * pair. Throw away previous field except for reference
  2341. * purposes.
  2342. */
  2343. s0->first_field = 1;
  2344. s0->current_picture_ptr = NULL;
  2345. } else {
  2346. /* Second field in complementary pair */
  2347. s0->first_field = 0;
  2348. }
  2349. }
  2350. } else {
  2351. /* Frame or first field in a potentially complementary pair */
  2352. assert(!s0->current_picture_ptr);
  2353. s0->first_field = FIELD_PICTURE;
  2354. }
  2355. if((!FIELD_PICTURE || s0->first_field) && ff_h264_frame_start(h) < 0) {
  2356. s0->first_field = 0;
  2357. return -1;
  2358. }
  2359. }
  2360. if(h != h0)
  2361. clone_slice(h, h0);
  2362. s->current_picture_ptr->frame_num= h->frame_num; //FIXME frame_num cleanup
  2363. assert(s->mb_num == s->mb_width * s->mb_height);
  2364. if(first_mb_in_slice << FIELD_OR_MBAFF_PICTURE >= s->mb_num ||
  2365. first_mb_in_slice >= s->mb_num){
  2366. av_log(h->s.avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
  2367. return -1;
  2368. }
  2369. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  2370. s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << FIELD_OR_MBAFF_PICTURE;
  2371. if (s->picture_structure == PICT_BOTTOM_FIELD)
  2372. s->resync_mb_y = s->mb_y = s->mb_y + 1;
  2373. assert(s->mb_y < s->mb_height);
  2374. if(s->picture_structure==PICT_FRAME){
  2375. h->curr_pic_num= h->frame_num;
  2376. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  2377. }else{
  2378. h->curr_pic_num= 2*h->frame_num + 1;
  2379. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  2380. }
  2381. if(h->nal_unit_type == NAL_IDR_SLICE){
  2382. get_ue_golomb(&s->gb); /* idr_pic_id */
  2383. }
  2384. if(h->sps.poc_type==0){
  2385. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  2386. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  2387. h->delta_poc_bottom= get_se_golomb(&s->gb);
  2388. }
  2389. }
  2390. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  2391. h->delta_poc[0]= get_se_golomb(&s->gb);
  2392. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  2393. h->delta_poc[1]= get_se_golomb(&s->gb);
  2394. }
  2395. init_poc(h);
  2396. if(h->pps.redundant_pic_cnt_present){
  2397. h->redundant_pic_count= get_ue_golomb(&s->gb);
  2398. }
  2399. //set defaults, might be overridden a few lines later
  2400. h->ref_count[0]= h->pps.ref_count[0];
  2401. h->ref_count[1]= h->pps.ref_count[1];
  2402. if(h->slice_type_nos != FF_I_TYPE){
  2403. if(h->slice_type_nos == FF_B_TYPE){
  2404. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  2405. }
  2406. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  2407. if(num_ref_idx_active_override_flag){
  2408. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  2409. if(h->slice_type_nos==FF_B_TYPE)
  2410. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  2411. if(h->ref_count[0]-1 > 32-1 || h->ref_count[1]-1 > 32-1){
  2412. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  2413. h->ref_count[0]= h->ref_count[1]= 1;
  2414. return -1;
  2415. }
  2416. }
  2417. if(h->slice_type_nos == FF_B_TYPE)
  2418. h->list_count= 2;
  2419. else
  2420. h->list_count= 1;
  2421. }else
  2422. h->list_count= 0;
  2423. if(!default_ref_list_done){
  2424. ff_h264_fill_default_ref_list(h);
  2425. }
  2426. if(h->slice_type_nos!=FF_I_TYPE && ff_h264_decode_ref_pic_list_reordering(h) < 0)
  2427. return -1;
  2428. if(h->slice_type_nos!=FF_I_TYPE){
  2429. s->last_picture_ptr= &h->ref_list[0][0];
  2430. ff_copy_picture(&s->last_picture, s->last_picture_ptr);
  2431. }
  2432. if(h->slice_type_nos==FF_B_TYPE){
  2433. s->next_picture_ptr= &h->ref_list[1][0];
  2434. ff_copy_picture(&s->next_picture, s->next_picture_ptr);
  2435. }
  2436. if( (h->pps.weighted_pred && h->slice_type_nos == FF_P_TYPE )
  2437. || (h->pps.weighted_bipred_idc==1 && h->slice_type_nos== FF_B_TYPE ) )
  2438. pred_weight_table(h);
  2439. else if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== FF_B_TYPE)
  2440. implicit_weight_table(h);
  2441. else {
  2442. h->use_weight = 0;
  2443. for (i = 0; i < 2; i++) {
  2444. h->luma_weight_flag[i] = 0;
  2445. h->chroma_weight_flag[i] = 0;
  2446. }
  2447. }
  2448. if(h->nal_ref_idc)
  2449. ff_h264_decode_ref_pic_marking(h0, &s->gb);
  2450. if(FRAME_MBAFF)
  2451. ff_h264_fill_mbaff_ref_list(h);
  2452. if(h->slice_type_nos==FF_B_TYPE && !h->direct_spatial_mv_pred)
  2453. ff_h264_direct_dist_scale_factor(h);
  2454. ff_h264_direct_ref_list_init(h);
  2455. if( h->slice_type_nos != FF_I_TYPE && h->pps.cabac ){
  2456. tmp = get_ue_golomb_31(&s->gb);
  2457. if(tmp > 2){
  2458. av_log(s->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
  2459. return -1;
  2460. }
  2461. h->cabac_init_idc= tmp;
  2462. }
  2463. h->last_qscale_diff = 0;
  2464. tmp = h->pps.init_qp + get_se_golomb(&s->gb);
  2465. if(tmp>51){
  2466. av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
  2467. return -1;
  2468. }
  2469. s->qscale= tmp;
  2470. h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
  2471. h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
  2472. //FIXME qscale / qp ... stuff
  2473. if(h->slice_type == FF_SP_TYPE){
  2474. get_bits1(&s->gb); /* sp_for_switch_flag */
  2475. }
  2476. if(h->slice_type==FF_SP_TYPE || h->slice_type == FF_SI_TYPE){
  2477. get_se_golomb(&s->gb); /* slice_qs_delta */
  2478. }
  2479. h->deblocking_filter = 1;
  2480. h->slice_alpha_c0_offset = 0;
  2481. h->slice_beta_offset = 0;
  2482. if( h->pps.deblocking_filter_parameters_present ) {
  2483. tmp= get_ue_golomb_31(&s->gb);
  2484. if(tmp > 2){
  2485. av_log(s->avctx, AV_LOG_ERROR, "deblocking_filter_idc %u out of range\n", tmp);
  2486. return -1;
  2487. }
  2488. h->deblocking_filter= tmp;
  2489. if(h->deblocking_filter < 2)
  2490. h->deblocking_filter^= 1; // 1<->0
  2491. if( h->deblocking_filter ) {
  2492. h->slice_alpha_c0_offset = get_se_golomb(&s->gb) << 1;
  2493. h->slice_beta_offset = get_se_golomb(&s->gb) << 1;
  2494. }
  2495. }
  2496. if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
  2497. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type_nos != FF_I_TYPE)
  2498. ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type_nos == FF_B_TYPE)
  2499. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  2500. h->deblocking_filter= 0;
  2501. if(h->deblocking_filter == 1 && h0->max_contexts > 1) {
  2502. if(s->avctx->flags2 & CODEC_FLAG2_FAST) {
  2503. /* Cheat slightly for speed:
  2504. Do not bother to deblock across slices. */
  2505. h->deblocking_filter = 2;
  2506. } else {
  2507. h0->max_contexts = 1;
  2508. if(!h0->single_decode_warning) {
  2509. av_log(s->avctx, AV_LOG_INFO, "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
  2510. h0->single_decode_warning = 1;
  2511. }
  2512. if(h != h0)
  2513. return 1; // deblocking switched inside frame
  2514. }
  2515. }
  2516. #if 0 //FMO
  2517. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  2518. slice_group_change_cycle= get_bits(&s->gb, ?);
  2519. #endif
  2520. h0->last_slice_type = slice_type;
  2521. h->slice_num = ++h0->current_slice;
  2522. if(h->slice_num >= MAX_SLICES){
  2523. av_log(s->avctx, AV_LOG_ERROR, "Too many slices, increase MAX_SLICES and recompile\n");
  2524. }
  2525. for(j=0; j<2; j++){
  2526. int *ref2frm= h->ref2frm[h->slice_num&(MAX_SLICES-1)][j];
  2527. ref2frm[0]=
  2528. ref2frm[1]= -1;
  2529. for(i=0; i<16; i++)
  2530. ref2frm[i+2]= 4*h->ref_list[j][i].frame_num
  2531. +(h->ref_list[j][i].reference&3);
  2532. ref2frm[18+0]=
  2533. ref2frm[18+1]= -1;
  2534. for(i=16; i<48; i++)
  2535. ref2frm[i+4]= 4*h->ref_list[j][i].frame_num
  2536. +(h->ref_list[j][i].reference&3);
  2537. }
  2538. h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
  2539. h->emu_edge_height= (FRAME_MBAFF || FIELD_PICTURE) ? 0 : h->emu_edge_width;
  2540. s->avctx->refs= h->sps.ref_frame_count;
  2541. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  2542. av_log(h->s.avctx, AV_LOG_DEBUG, "slice:%d %s mb:%d %c%s%s pps:%u frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s %s\n",
  2543. h->slice_num,
  2544. (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
  2545. first_mb_in_slice,
  2546. av_get_pict_type_char(h->slice_type), h->slice_type_fixed ? " fix" : "", h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
  2547. pps_id, h->frame_num,
  2548. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  2549. h->ref_count[0], h->ref_count[1],
  2550. s->qscale,
  2551. h->deblocking_filter, h->slice_alpha_c0_offset/2, h->slice_beta_offset/2,
  2552. h->use_weight,
  2553. h->use_weight==1 && h->use_weight_chroma ? "c" : "",
  2554. h->slice_type == FF_B_TYPE ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : ""
  2555. );
  2556. }
  2557. return 0;
  2558. }
  2559. int ff_h264_get_slice_type(H264Context *h)
  2560. {
  2561. switch (h->slice_type) {
  2562. case FF_P_TYPE: return 0;
  2563. case FF_B_TYPE: return 1;
  2564. case FF_I_TYPE: return 2;
  2565. case FF_SP_TYPE: return 3;
  2566. case FF_SI_TYPE: return 4;
  2567. default: return -1;
  2568. }
  2569. }
  2570. /**
  2571. *
  2572. */
  2573. static inline int get_level_prefix(GetBitContext *gb){
  2574. unsigned int buf;
  2575. int log;
  2576. OPEN_READER(re, gb);
  2577. UPDATE_CACHE(re, gb);
  2578. buf=GET_CACHE(re, gb);
  2579. log= 32 - av_log2(buf);
  2580. #ifdef TRACE
  2581. print_bin(buf>>(32-log), log);
  2582. 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__);
  2583. #endif
  2584. LAST_SKIP_BITS(re, gb, log);
  2585. CLOSE_READER(re, gb);
  2586. return log-1;
  2587. }
  2588. static inline int get_dct8x8_allowed(H264Context *h){
  2589. if(h->sps.direct_8x8_inference_flag)
  2590. return !(*(uint64_t*)h->sub_mb_type & ((MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_8x8 )*0x0001000100010001ULL));
  2591. else
  2592. return !(*(uint64_t*)h->sub_mb_type & ((MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_8x8|MB_TYPE_DIRECT2)*0x0001000100010001ULL));
  2593. }
  2594. /**
  2595. * decodes a residual block.
  2596. * @param n block index
  2597. * @param scantable scantable
  2598. * @param max_coeff number of coefficients in the block
  2599. * @return <0 if an error occurred
  2600. */
  2601. static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff){
  2602. MpegEncContext * const s = &h->s;
  2603. 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};
  2604. int level[16];
  2605. int zeros_left, coeff_num, coeff_token, total_coeff, i, j, trailing_ones, run_before;
  2606. //FIXME put trailing_onex into the context
  2607. if(n == CHROMA_DC_BLOCK_INDEX){
  2608. coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);
  2609. total_coeff= coeff_token>>2;
  2610. }else{
  2611. if(n == LUMA_DC_BLOCK_INDEX){
  2612. total_coeff= pred_non_zero_count(h, 0);
  2613. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  2614. total_coeff= coeff_token>>2;
  2615. }else{
  2616. total_coeff= pred_non_zero_count(h, n);
  2617. coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
  2618. total_coeff= coeff_token>>2;
  2619. h->non_zero_count_cache[ scan8[n] ]= total_coeff;
  2620. }
  2621. }
  2622. //FIXME set last_non_zero?
  2623. if(total_coeff==0)
  2624. return 0;
  2625. if(total_coeff > (unsigned)max_coeff) {
  2626. av_log(h->s.avctx, AV_LOG_ERROR, "corrupted macroblock %d %d (total_coeff=%d)\n", s->mb_x, s->mb_y, total_coeff);
  2627. return -1;
  2628. }
  2629. trailing_ones= coeff_token&3;
  2630. tprintf(h->s.avctx, "trailing:%d, total:%d\n", trailing_ones, total_coeff);
  2631. assert(total_coeff<=16);
  2632. i = show_bits(gb, 3);
  2633. skip_bits(gb, trailing_ones);
  2634. level[0] = 1-((i&4)>>1);
  2635. level[1] = 1-((i&2) );
  2636. level[2] = 1-((i&1)<<1);
  2637. if(trailing_ones<total_coeff) {
  2638. int mask, prefix;
  2639. int suffix_length = total_coeff > 10 && trailing_ones < 3;
  2640. int bitsi= show_bits(gb, LEVEL_TAB_BITS);
  2641. int level_code= cavlc_level_tab[suffix_length][bitsi][0];
  2642. skip_bits(gb, cavlc_level_tab[suffix_length][bitsi][1]);
  2643. if(level_code >= 100){
  2644. prefix= level_code - 100;
  2645. if(prefix == LEVEL_TAB_BITS)
  2646. prefix += get_level_prefix(gb);
  2647. //first coefficient has suffix_length equal to 0 or 1
  2648. if(prefix<14){ //FIXME try to build a large unified VLC table for all this
  2649. if(suffix_length)
  2650. level_code= (prefix<<1) + get_bits1(gb); //part
  2651. else
  2652. level_code= prefix; //part
  2653. }else if(prefix==14){
  2654. if(suffix_length)
  2655. level_code= (prefix<<1) + get_bits1(gb); //part
  2656. else
  2657. level_code= prefix + get_bits(gb, 4); //part
  2658. }else{
  2659. level_code= 30 + get_bits(gb, prefix-3); //part
  2660. if(prefix>=16)
  2661. level_code += (1<<(prefix-3))-4096;
  2662. }
  2663. if(trailing_ones < 3) level_code += 2;
  2664. suffix_length = 2;
  2665. mask= -(level_code&1);
  2666. level[trailing_ones]= (((2+level_code)>>1) ^ mask) - mask;
  2667. }else{
  2668. if(trailing_ones < 3) level_code += (level_code>>31)|1;
  2669. suffix_length = 1;
  2670. if(level_code + 3U > 6U)
  2671. suffix_length++;
  2672. level[trailing_ones]= level_code;
  2673. }
  2674. //remaining coefficients have suffix_length > 0
  2675. for(i=trailing_ones+1;i<total_coeff;i++) {
  2676. static const unsigned int suffix_limit[7] = {0,3,6,12,24,48,INT_MAX };
  2677. int bitsi= show_bits(gb, LEVEL_TAB_BITS);
  2678. level_code= cavlc_level_tab[suffix_length][bitsi][0];
  2679. skip_bits(gb, cavlc_level_tab[suffix_length][bitsi][1]);
  2680. if(level_code >= 100){
  2681. prefix= level_code - 100;
  2682. if(prefix == LEVEL_TAB_BITS){
  2683. prefix += get_level_prefix(gb);
  2684. }
  2685. if(prefix<15){
  2686. level_code = (prefix<<suffix_length) + get_bits(gb, suffix_length);
  2687. }else{
  2688. level_code = (15<<suffix_length) + get_bits(gb, prefix-3);
  2689. if(prefix>=16)
  2690. level_code += (1<<(prefix-3))-4096;
  2691. }
  2692. mask= -(level_code&1);
  2693. level_code= (((2+level_code)>>1) ^ mask) - mask;
  2694. }
  2695. level[i]= level_code;
  2696. if(suffix_limit[suffix_length] + level_code > 2U*suffix_limit[suffix_length])
  2697. suffix_length++;
  2698. }
  2699. }
  2700. if(total_coeff == max_coeff)
  2701. zeros_left=0;
  2702. else{
  2703. if(n == CHROMA_DC_BLOCK_INDEX)
  2704. zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
  2705. else
  2706. zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);
  2707. }
  2708. coeff_num = zeros_left + total_coeff - 1;
  2709. j = scantable[coeff_num];
  2710. if(n > 24){
  2711. block[j] = level[0];
  2712. for(i=1;i<total_coeff;i++) {
  2713. if(zeros_left <= 0)
  2714. run_before = 0;
  2715. else if(zeros_left < 7){
  2716. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  2717. }else{
  2718. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  2719. }
  2720. zeros_left -= run_before;
  2721. coeff_num -= 1 + run_before;
  2722. j= scantable[ coeff_num ];
  2723. block[j]= level[i];
  2724. }
  2725. }else{
  2726. block[j] = (level[0] * qmul[j] + 32)>>6;
  2727. for(i=1;i<total_coeff;i++) {
  2728. if(zeros_left <= 0)
  2729. run_before = 0;
  2730. else if(zeros_left < 7){
  2731. run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
  2732. }else{
  2733. run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
  2734. }
  2735. zeros_left -= run_before;
  2736. coeff_num -= 1 + run_before;
  2737. j= scantable[ coeff_num ];
  2738. block[j]= (level[i] * qmul[j] + 32)>>6;
  2739. }
  2740. }
  2741. if(zeros_left<0){
  2742. av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);
  2743. return -1;
  2744. }
  2745. return 0;
  2746. }
  2747. static void predict_field_decoding_flag(H264Context *h){
  2748. MpegEncContext * const s = &h->s;
  2749. const int mb_xy= h->mb_xy;
  2750. int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
  2751. ? s->current_picture.mb_type[mb_xy-1]
  2752. : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
  2753. ? s->current_picture.mb_type[mb_xy-s->mb_stride]
  2754. : 0;
  2755. h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  2756. }
  2757. /**
  2758. * decodes a P_SKIP or B_SKIP macroblock
  2759. */
  2760. static void decode_mb_skip(H264Context *h){
  2761. MpegEncContext * const s = &h->s;
  2762. const int mb_xy= h->mb_xy;
  2763. int mb_type=0;
  2764. memset(h->non_zero_count[mb_xy], 0, 16);
  2765. memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
  2766. if(MB_FIELD)
  2767. mb_type|= MB_TYPE_INTERLACED;
  2768. if( h->slice_type_nos == FF_B_TYPE )
  2769. {
  2770. // just for fill_caches. pred_direct_motion will set the real mb_type
  2771. mb_type|= MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_SKIP;
  2772. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  2773. ff_h264_pred_direct_motion(h, &mb_type);
  2774. mb_type|= MB_TYPE_SKIP;
  2775. }
  2776. else
  2777. {
  2778. int mx, my;
  2779. mb_type|= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
  2780. fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
  2781. pred_pskip_motion(h, &mx, &my);
  2782. fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
  2783. fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
  2784. }
  2785. write_back_motion(h, mb_type);
  2786. s->current_picture.mb_type[mb_xy]= mb_type;
  2787. s->current_picture.qscale_table[mb_xy]= s->qscale;
  2788. h->slice_table[ mb_xy ]= h->slice_num;
  2789. h->prev_mb_skipped= 1;
  2790. }
  2791. /**
  2792. * decodes a macroblock
  2793. * @returns 0 if OK, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  2794. */
  2795. static int decode_mb_cavlc(H264Context *h){
  2796. MpegEncContext * const s = &h->s;
  2797. int mb_xy;
  2798. int partition_count;
  2799. unsigned int mb_type, cbp;
  2800. int dct8x8_allowed= h->pps.transform_8x8_mode;
  2801. mb_xy = h->mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  2802. tprintf(s->avctx, "pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  2803. cbp = 0; /* avoid warning. FIXME: find a solution without slowing
  2804. down the code */
  2805. if(h->slice_type_nos != FF_I_TYPE){
  2806. if(s->mb_skip_run==-1)
  2807. s->mb_skip_run= get_ue_golomb(&s->gb);
  2808. if (s->mb_skip_run--) {
  2809. if(FRAME_MBAFF && (s->mb_y&1) == 0){
  2810. if(s->mb_skip_run==0)
  2811. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  2812. else
  2813. predict_field_decoding_flag(h);
  2814. }
  2815. decode_mb_skip(h);
  2816. return 0;
  2817. }
  2818. }
  2819. if(FRAME_MBAFF){
  2820. if( (s->mb_y&1) == 0 )
  2821. h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
  2822. }
  2823. h->prev_mb_skipped= 0;
  2824. mb_type= get_ue_golomb(&s->gb);
  2825. if(h->slice_type_nos == FF_B_TYPE){
  2826. if(mb_type < 23){
  2827. partition_count= b_mb_type_info[mb_type].partition_count;
  2828. mb_type= b_mb_type_info[mb_type].type;
  2829. }else{
  2830. mb_type -= 23;
  2831. goto decode_intra_mb;
  2832. }
  2833. }else if(h->slice_type_nos == FF_P_TYPE){
  2834. if(mb_type < 5){
  2835. partition_count= p_mb_type_info[mb_type].partition_count;
  2836. mb_type= p_mb_type_info[mb_type].type;
  2837. }else{
  2838. mb_type -= 5;
  2839. goto decode_intra_mb;
  2840. }
  2841. }else{
  2842. assert(h->slice_type_nos == FF_I_TYPE);
  2843. if(h->slice_type == FF_SI_TYPE && mb_type)
  2844. mb_type--;
  2845. decode_intra_mb:
  2846. if(mb_type > 25){
  2847. 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);
  2848. return -1;
  2849. }
  2850. partition_count=0;
  2851. cbp= i_mb_type_info[mb_type].cbp;
  2852. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  2853. mb_type= i_mb_type_info[mb_type].type;
  2854. }
  2855. if(MB_FIELD)
  2856. mb_type |= MB_TYPE_INTERLACED;
  2857. h->slice_table[ mb_xy ]= h->slice_num;
  2858. if(IS_INTRA_PCM(mb_type)){
  2859. unsigned int x;
  2860. // We assume these blocks are very rare so we do not optimize it.
  2861. align_get_bits(&s->gb);
  2862. // The pixels are stored in the same order as levels in h->mb array.
  2863. for(x=0; x < (CHROMA ? 384 : 256); x++){
  2864. ((uint8_t*)h->mb)[x]= get_bits(&s->gb, 8);
  2865. }
  2866. // In deblocking, the quantizer is 0
  2867. s->current_picture.qscale_table[mb_xy]= 0;
  2868. // All coeffs are present
  2869. memset(h->non_zero_count[mb_xy], 16, 16);
  2870. s->current_picture.mb_type[mb_xy]= mb_type;
  2871. return 0;
  2872. }
  2873. if(MB_MBAFF){
  2874. h->ref_count[0] <<= 1;
  2875. h->ref_count[1] <<= 1;
  2876. }
  2877. fill_caches(h, mb_type, 0);
  2878. //mb_pred
  2879. if(IS_INTRA(mb_type)){
  2880. int pred_mode;
  2881. // init_top_left_availability(h);
  2882. if(IS_INTRA4x4(mb_type)){
  2883. int i;
  2884. int di = 1;
  2885. if(dct8x8_allowed && get_bits1(&s->gb)){
  2886. mb_type |= MB_TYPE_8x8DCT;
  2887. di = 4;
  2888. }
  2889. // fill_intra4x4_pred_table(h);
  2890. for(i=0; i<16; i+=di){
  2891. int mode= pred_intra_mode(h, i);
  2892. if(!get_bits1(&s->gb)){
  2893. const int rem_mode= get_bits(&s->gb, 3);
  2894. mode = rem_mode + (rem_mode >= mode);
  2895. }
  2896. if(di==4)
  2897. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  2898. else
  2899. h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
  2900. }
  2901. ff_h264_write_back_intra_pred_mode(h);
  2902. if( ff_h264_check_intra4x4_pred_mode(h) < 0)
  2903. return -1;
  2904. }else{
  2905. h->intra16x16_pred_mode= ff_h264_check_intra_pred_mode(h, h->intra16x16_pred_mode);
  2906. if(h->intra16x16_pred_mode < 0)
  2907. return -1;
  2908. }
  2909. if(CHROMA){
  2910. pred_mode= ff_h264_check_intra_pred_mode(h, get_ue_golomb_31(&s->gb));
  2911. if(pred_mode < 0)
  2912. return -1;
  2913. h->chroma_pred_mode= pred_mode;
  2914. }
  2915. }else if(partition_count==4){
  2916. int i, j, sub_partition_count[4], list, ref[2][4];
  2917. if(h->slice_type_nos == FF_B_TYPE){
  2918. for(i=0; i<4; i++){
  2919. h->sub_mb_type[i]= get_ue_golomb_31(&s->gb);
  2920. if(h->sub_mb_type[i] >=13){
  2921. 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);
  2922. return -1;
  2923. }
  2924. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  2925. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  2926. }
  2927. if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
  2928. || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3])) {
  2929. ff_h264_pred_direct_motion(h, &mb_type);
  2930. h->ref_cache[0][scan8[4]] =
  2931. h->ref_cache[1][scan8[4]] =
  2932. h->ref_cache[0][scan8[12]] =
  2933. h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
  2934. }
  2935. }else{
  2936. assert(h->slice_type_nos == FF_P_TYPE); //FIXME SP correct ?
  2937. for(i=0; i<4; i++){
  2938. h->sub_mb_type[i]= get_ue_golomb_31(&s->gb);
  2939. if(h->sub_mb_type[i] >=4){
  2940. 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);
  2941. return -1;
  2942. }
  2943. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  2944. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  2945. }
  2946. }
  2947. for(list=0; list<h->list_count; list++){
  2948. int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
  2949. for(i=0; i<4; i++){
  2950. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  2951. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  2952. unsigned int tmp;
  2953. if(ref_count == 1){
  2954. tmp= 0;
  2955. }else if(ref_count == 2){
  2956. tmp= get_bits1(&s->gb)^1;
  2957. }else{
  2958. tmp= get_ue_golomb_31(&s->gb);
  2959. if(tmp>=ref_count){
  2960. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", tmp);
  2961. return -1;
  2962. }
  2963. }
  2964. ref[list][i]= tmp;
  2965. }else{
  2966. //FIXME
  2967. ref[list][i] = -1;
  2968. }
  2969. }
  2970. }
  2971. if(dct8x8_allowed)
  2972. dct8x8_allowed = get_dct8x8_allowed(h);
  2973. for(list=0; list<h->list_count; list++){
  2974. for(i=0; i<4; i++){
  2975. if(IS_DIRECT(h->sub_mb_type[i])) {
  2976. h->ref_cache[list][ scan8[4*i] ] = h->ref_cache[list][ scan8[4*i]+1 ];
  2977. continue;
  2978. }
  2979. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
  2980. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  2981. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  2982. const int sub_mb_type= h->sub_mb_type[i];
  2983. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  2984. for(j=0; j<sub_partition_count[i]; j++){
  2985. int mx, my;
  2986. const int index= 4*i + block_width*j;
  2987. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  2988. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
  2989. mx += get_se_golomb(&s->gb);
  2990. my += get_se_golomb(&s->gb);
  2991. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  2992. if(IS_SUB_8X8(sub_mb_type)){
  2993. mv_cache[ 1 ][0]=
  2994. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  2995. mv_cache[ 1 ][1]=
  2996. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  2997. }else if(IS_SUB_8X4(sub_mb_type)){
  2998. mv_cache[ 1 ][0]= mx;
  2999. mv_cache[ 1 ][1]= my;
  3000. }else if(IS_SUB_4X8(sub_mb_type)){
  3001. mv_cache[ 8 ][0]= mx;
  3002. mv_cache[ 8 ][1]= my;
  3003. }
  3004. mv_cache[ 0 ][0]= mx;
  3005. mv_cache[ 0 ][1]= my;
  3006. }
  3007. }else{
  3008. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  3009. p[0] = p[1]=
  3010. p[8] = p[9]= 0;
  3011. }
  3012. }
  3013. }
  3014. }else if(IS_DIRECT(mb_type)){
  3015. ff_h264_pred_direct_motion(h, &mb_type);
  3016. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  3017. }else{
  3018. int list, mx, my, i;
  3019. //FIXME we should set ref_idx_l? to 0 if we use that later ...
  3020. if(IS_16X16(mb_type)){
  3021. for(list=0; list<h->list_count; list++){
  3022. unsigned int val;
  3023. if(IS_DIR(mb_type, 0, list)){
  3024. if(h->ref_count[list]==1){
  3025. val= 0;
  3026. }else if(h->ref_count[list]==2){
  3027. val= get_bits1(&s->gb)^1;
  3028. }else{
  3029. val= get_ue_golomb_31(&s->gb);
  3030. if(val >= h->ref_count[list]){
  3031. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  3032. return -1;
  3033. }
  3034. }
  3035. }else
  3036. val= LIST_NOT_USED&0xFF;
  3037. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
  3038. }
  3039. for(list=0; list<h->list_count; list++){
  3040. unsigned int val;
  3041. if(IS_DIR(mb_type, 0, list)){
  3042. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
  3043. mx += get_se_golomb(&s->gb);
  3044. my += get_se_golomb(&s->gb);
  3045. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  3046. val= pack16to32(mx,my);
  3047. }else
  3048. val=0;
  3049. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, val, 4);
  3050. }
  3051. }
  3052. else if(IS_16X8(mb_type)){
  3053. for(list=0; list<h->list_count; list++){
  3054. for(i=0; i<2; i++){
  3055. unsigned int val;
  3056. if(IS_DIR(mb_type, i, list)){
  3057. if(h->ref_count[list] == 1){
  3058. val= 0;
  3059. }else if(h->ref_count[list] == 2){
  3060. val= get_bits1(&s->gb)^1;
  3061. }else{
  3062. val= get_ue_golomb_31(&s->gb);
  3063. if(val >= h->ref_count[list]){
  3064. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  3065. return -1;
  3066. }
  3067. }
  3068. }else
  3069. val= LIST_NOT_USED&0xFF;
  3070. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);
  3071. }
  3072. }
  3073. for(list=0; list<h->list_count; list++){
  3074. for(i=0; i<2; i++){
  3075. unsigned int val;
  3076. if(IS_DIR(mb_type, i, list)){
  3077. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
  3078. mx += get_se_golomb(&s->gb);
  3079. my += get_se_golomb(&s->gb);
  3080. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  3081. val= pack16to32(mx,my);
  3082. }else
  3083. val=0;
  3084. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 4);
  3085. }
  3086. }
  3087. }else{
  3088. assert(IS_8X16(mb_type));
  3089. for(list=0; list<h->list_count; list++){
  3090. for(i=0; i<2; i++){
  3091. unsigned int val;
  3092. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  3093. if(h->ref_count[list]==1){
  3094. val= 0;
  3095. }else if(h->ref_count[list]==2){
  3096. val= get_bits1(&s->gb)^1;
  3097. }else{
  3098. val= get_ue_golomb_31(&s->gb);
  3099. if(val >= h->ref_count[list]){
  3100. av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
  3101. return -1;
  3102. }
  3103. }
  3104. }else
  3105. val= LIST_NOT_USED&0xFF;
  3106. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
  3107. }
  3108. }
  3109. for(list=0; list<h->list_count; list++){
  3110. for(i=0; i<2; i++){
  3111. unsigned int val;
  3112. if(IS_DIR(mb_type, i, list)){
  3113. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
  3114. mx += get_se_golomb(&s->gb);
  3115. my += get_se_golomb(&s->gb);
  3116. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  3117. val= pack16to32(mx,my);
  3118. }else
  3119. val=0;
  3120. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 4);
  3121. }
  3122. }
  3123. }
  3124. }
  3125. if(IS_INTER(mb_type))
  3126. write_back_motion(h, mb_type);
  3127. if(!IS_INTRA16x16(mb_type)){
  3128. cbp= get_ue_golomb(&s->gb);
  3129. if(cbp > 47){
  3130. av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%u) at %d %d\n", cbp, s->mb_x, s->mb_y);
  3131. return -1;
  3132. }
  3133. if(CHROMA){
  3134. if(IS_INTRA4x4(mb_type)) cbp= golomb_to_intra4x4_cbp[cbp];
  3135. else cbp= golomb_to_inter_cbp [cbp];
  3136. }else{
  3137. if(IS_INTRA4x4(mb_type)) cbp= golomb_to_intra4x4_cbp_gray[cbp];
  3138. else cbp= golomb_to_inter_cbp_gray[cbp];
  3139. }
  3140. }
  3141. h->cbp = cbp;
  3142. if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){
  3143. if(get_bits1(&s->gb)){
  3144. mb_type |= MB_TYPE_8x8DCT;
  3145. h->cbp_table[mb_xy]= cbp;
  3146. }
  3147. }
  3148. s->current_picture.mb_type[mb_xy]= mb_type;
  3149. if(cbp || IS_INTRA16x16(mb_type)){
  3150. int i8x8, i4x4, chroma_idx;
  3151. int dquant;
  3152. GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
  3153. const uint8_t *scan, *scan8x8, *dc_scan;
  3154. // fill_non_zero_count_cache(h);
  3155. if(IS_INTERLACED(mb_type)){
  3156. scan8x8= s->qscale ? h->field_scan8x8_cavlc : h->field_scan8x8_cavlc_q0;
  3157. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  3158. dc_scan= luma_dc_field_scan;
  3159. }else{
  3160. scan8x8= s->qscale ? h->zigzag_scan8x8_cavlc : h->zigzag_scan8x8_cavlc_q0;
  3161. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  3162. dc_scan= luma_dc_zigzag_scan;
  3163. }
  3164. dquant= get_se_golomb(&s->gb);
  3165. if( dquant > 25 || dquant < -26 ){
  3166. av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
  3167. return -1;
  3168. }
  3169. s->qscale += dquant;
  3170. if(((unsigned)s->qscale) > 51){
  3171. if(s->qscale<0) s->qscale+= 52;
  3172. else s->qscale-= 52;
  3173. }
  3174. h->chroma_qp[0]= get_chroma_qp(h, 0, s->qscale);
  3175. h->chroma_qp[1]= get_chroma_qp(h, 1, s->qscale);
  3176. if(IS_INTRA16x16(mb_type)){
  3177. if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, h->dequant4_coeff[0][s->qscale], 16) < 0){
  3178. return -1; //FIXME continue if partitioned and other return -1 too
  3179. }
  3180. assert((cbp&15) == 0 || (cbp&15) == 15);
  3181. if(cbp&15){
  3182. for(i8x8=0; i8x8<4; i8x8++){
  3183. for(i4x4=0; i4x4<4; i4x4++){
  3184. const int index= i4x4 + 4*i8x8;
  3185. if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 ){
  3186. return -1;
  3187. }
  3188. }
  3189. }
  3190. }else{
  3191. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  3192. }
  3193. }else{
  3194. for(i8x8=0; i8x8<4; i8x8++){
  3195. if(cbp & (1<<i8x8)){
  3196. if(IS_8x8DCT(mb_type)){
  3197. DCTELEM *buf = &h->mb[64*i8x8];
  3198. uint8_t *nnz;
  3199. for(i4x4=0; i4x4<4; i4x4++){
  3200. if( decode_residual(h, gb, buf, i4x4+4*i8x8, scan8x8+16*i4x4,
  3201. h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 16) <0 )
  3202. return -1;
  3203. }
  3204. nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  3205. nnz[0] += nnz[1] + nnz[8] + nnz[9];
  3206. }else{
  3207. for(i4x4=0; i4x4<4; i4x4++){
  3208. const int index= i4x4 + 4*i8x8;
  3209. if( decode_residual(h, gb, h->mb + 16*index, index, scan, h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale], 16) <0 ){
  3210. return -1;
  3211. }
  3212. }
  3213. }
  3214. }else{
  3215. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  3216. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  3217. }
  3218. }
  3219. }
  3220. if(cbp&0x30){
  3221. for(chroma_idx=0; chroma_idx<2; chroma_idx++)
  3222. if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, NULL, 4) < 0){
  3223. return -1;
  3224. }
  3225. }
  3226. if(cbp&0x20){
  3227. for(chroma_idx=0; chroma_idx<2; chroma_idx++){
  3228. const uint32_t *qmul = h->dequant4_coeff[chroma_idx+1+(IS_INTRA( mb_type ) ? 0:3)][h->chroma_qp[chroma_idx]];
  3229. for(i4x4=0; i4x4<4; i4x4++){
  3230. const int index= 16 + 4*chroma_idx + i4x4;
  3231. if( decode_residual(h, gb, h->mb + 16*index, index, scan + 1, qmul, 15) < 0){
  3232. return -1;
  3233. }
  3234. }
  3235. }
  3236. }else{
  3237. uint8_t * const nnz= &h->non_zero_count_cache[0];
  3238. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  3239. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  3240. }
  3241. }else{
  3242. uint8_t * const nnz= &h->non_zero_count_cache[0];
  3243. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  3244. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  3245. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  3246. }
  3247. s->current_picture.qscale_table[mb_xy]= s->qscale;
  3248. write_back_non_zero_count(h);
  3249. if(MB_MBAFF){
  3250. h->ref_count[0] >>= 1;
  3251. h->ref_count[1] >>= 1;
  3252. }
  3253. return 0;
  3254. }
  3255. static int decode_cabac_field_decoding_flag(H264Context *h) {
  3256. MpegEncContext * const s = &h->s;
  3257. const int mb_x = s->mb_x;
  3258. const int mb_y = s->mb_y & ~1;
  3259. const int mba_xy = mb_x - 1 + mb_y *s->mb_stride;
  3260. const int mbb_xy = mb_x + (mb_y-2)*s->mb_stride;
  3261. unsigned int ctx = 0;
  3262. if( h->slice_table[mba_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) ) {
  3263. ctx += 1;
  3264. }
  3265. if( h->slice_table[mbb_xy] == h->slice_num && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) ) {
  3266. ctx += 1;
  3267. }
  3268. return get_cabac_noinline( &h->cabac, &h->cabac_state[70 + ctx] );
  3269. }
  3270. static int decode_cabac_intra_mb_type(H264Context *h, int ctx_base, int intra_slice) {
  3271. uint8_t *state= &h->cabac_state[ctx_base];
  3272. int mb_type;
  3273. if(intra_slice){
  3274. MpegEncContext * const s = &h->s;
  3275. const int mba_xy = h->left_mb_xy[0];
  3276. const int mbb_xy = h->top_mb_xy;
  3277. int ctx=0;
  3278. if( h->slice_table[mba_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mba_xy] ) )
  3279. ctx++;
  3280. if( h->slice_table[mbb_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mbb_xy] ) )
  3281. ctx++;
  3282. if( get_cabac_noinline( &h->cabac, &state[ctx] ) == 0 )
  3283. return 0; /* I4x4 */
  3284. state += 2;
  3285. }else{
  3286. if( get_cabac_noinline( &h->cabac, &state[0] ) == 0 )
  3287. return 0; /* I4x4 */
  3288. }
  3289. if( get_cabac_terminate( &h->cabac ) )
  3290. return 25; /* PCM */
  3291. mb_type = 1; /* I16x16 */
  3292. mb_type += 12 * get_cabac_noinline( &h->cabac, &state[1] ); /* cbp_luma != 0 */
  3293. if( get_cabac_noinline( &h->cabac, &state[2] ) ) /* cbp_chroma */
  3294. mb_type += 4 + 4 * get_cabac_noinline( &h->cabac, &state[2+intra_slice] );
  3295. mb_type += 2 * get_cabac_noinline( &h->cabac, &state[3+intra_slice] );
  3296. mb_type += 1 * get_cabac_noinline( &h->cabac, &state[3+2*intra_slice] );
  3297. return mb_type;
  3298. }
  3299. static int decode_cabac_mb_type_b( H264Context *h ) {
  3300. MpegEncContext * const s = &h->s;
  3301. const int mba_xy = h->left_mb_xy[0];
  3302. const int mbb_xy = h->top_mb_xy;
  3303. int ctx = 0;
  3304. int bits;
  3305. assert(h->slice_type_nos == FF_B_TYPE);
  3306. if( h->slice_table[mba_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mba_xy] ) )
  3307. ctx++;
  3308. if( h->slice_table[mbb_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mbb_xy] ) )
  3309. ctx++;
  3310. if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+ctx] ) )
  3311. return 0; /* B_Direct_16x16 */
  3312. if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+3] ) ) {
  3313. return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ); /* B_L[01]_16x16 */
  3314. }
  3315. bits = get_cabac_noinline( &h->cabac, &h->cabac_state[27+4] ) << 3;
  3316. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 2;
  3317. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 1;
  3318. bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
  3319. if( bits < 8 )
  3320. return bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */
  3321. else if( bits == 13 ) {
  3322. return decode_cabac_intra_mb_type(h, 32, 0) + 23;
  3323. } else if( bits == 14 )
  3324. return 11; /* B_L1_L0_8x16 */
  3325. else if( bits == 15 )
  3326. return 22; /* B_8x8 */
  3327. bits= ( bits<<1 ) | get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
  3328. return bits - 4; /* B_L0_Bi_* through B_Bi_Bi_* */
  3329. }
  3330. static int decode_cabac_mb_skip( H264Context *h, int mb_x, int mb_y ) {
  3331. MpegEncContext * const s = &h->s;
  3332. int mba_xy, mbb_xy;
  3333. int ctx = 0;
  3334. if(FRAME_MBAFF){ //FIXME merge with the stuff in fill_caches?
  3335. int mb_xy = mb_x + (mb_y&~1)*s->mb_stride;
  3336. mba_xy = mb_xy - 1;
  3337. if( (mb_y&1)
  3338. && h->slice_table[mba_xy] == h->slice_num
  3339. && MB_FIELD == !!IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) )
  3340. mba_xy += s->mb_stride;
  3341. if( MB_FIELD ){
  3342. mbb_xy = mb_xy - s->mb_stride;
  3343. if( !(mb_y&1)
  3344. && h->slice_table[mbb_xy] == h->slice_num
  3345. && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) )
  3346. mbb_xy -= s->mb_stride;
  3347. }else
  3348. mbb_xy = mb_x + (mb_y-1)*s->mb_stride;
  3349. }else{
  3350. int mb_xy = h->mb_xy;
  3351. mba_xy = mb_xy - 1;
  3352. mbb_xy = mb_xy - (s->mb_stride << FIELD_PICTURE);
  3353. }
  3354. if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] ))
  3355. ctx++;
  3356. if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] ))
  3357. ctx++;
  3358. if( h->slice_type_nos == FF_B_TYPE )
  3359. ctx += 13;
  3360. return get_cabac_noinline( &h->cabac, &h->cabac_state[11+ctx] );
  3361. }
  3362. static int decode_cabac_mb_intra4x4_pred_mode( H264Context *h, int pred_mode ) {
  3363. int mode = 0;
  3364. if( get_cabac( &h->cabac, &h->cabac_state[68] ) )
  3365. return pred_mode;
  3366. mode += 1 * get_cabac( &h->cabac, &h->cabac_state[69] );
  3367. mode += 2 * get_cabac( &h->cabac, &h->cabac_state[69] );
  3368. mode += 4 * get_cabac( &h->cabac, &h->cabac_state[69] );
  3369. if( mode >= pred_mode )
  3370. return mode + 1;
  3371. else
  3372. return mode;
  3373. }
  3374. static int decode_cabac_mb_chroma_pre_mode( H264Context *h) {
  3375. const int mba_xy = h->left_mb_xy[0];
  3376. const int mbb_xy = h->top_mb_xy;
  3377. int ctx = 0;
  3378. /* No need to test for IS_INTRA4x4 and IS_INTRA16x16, as we set chroma_pred_mode_table to 0 */
  3379. if( h->slice_table[mba_xy] == h->slice_num && h->chroma_pred_mode_table[mba_xy] != 0 )
  3380. ctx++;
  3381. if( h->slice_table[mbb_xy] == h->slice_num && h->chroma_pred_mode_table[mbb_xy] != 0 )
  3382. ctx++;
  3383. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+ctx] ) == 0 )
  3384. return 0;
  3385. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  3386. return 1;
  3387. if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
  3388. return 2;
  3389. else
  3390. return 3;
  3391. }
  3392. static int decode_cabac_mb_cbp_luma( H264Context *h) {
  3393. int cbp_b, cbp_a, ctx, cbp = 0;
  3394. cbp_a = h->slice_table[h->left_mb_xy[0]] == h->slice_num ? h->left_cbp : -1;
  3395. cbp_b = h->slice_table[h->top_mb_xy] == h->slice_num ? h->top_cbp : -1;
  3396. ctx = !(cbp_a & 0x02) + 2 * !(cbp_b & 0x04);
  3397. cbp |= get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]);
  3398. ctx = !(cbp & 0x01) + 2 * !(cbp_b & 0x08);
  3399. cbp |= get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]) << 1;
  3400. ctx = !(cbp_a & 0x08) + 2 * !(cbp & 0x01);
  3401. cbp |= get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]) << 2;
  3402. ctx = !(cbp & 0x04) + 2 * !(cbp & 0x02);
  3403. cbp |= get_cabac_noinline(&h->cabac, &h->cabac_state[73 + ctx]) << 3;
  3404. return cbp;
  3405. }
  3406. static int decode_cabac_mb_cbp_chroma( H264Context *h) {
  3407. int ctx;
  3408. int cbp_a, cbp_b;
  3409. cbp_a = (h->left_cbp>>4)&0x03;
  3410. cbp_b = (h-> top_cbp>>4)&0x03;
  3411. ctx = 0;
  3412. if( cbp_a > 0 ) ctx++;
  3413. if( cbp_b > 0 ) ctx += 2;
  3414. if( get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] ) == 0 )
  3415. return 0;
  3416. ctx = 4;
  3417. if( cbp_a == 2 ) ctx++;
  3418. if( cbp_b == 2 ) ctx += 2;
  3419. return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] );
  3420. }
  3421. static int decode_cabac_mb_dqp( H264Context *h) {
  3422. int ctx= h->last_qscale_diff != 0;
  3423. int val = 0;
  3424. while( get_cabac_noinline( &h->cabac, &h->cabac_state[60 + ctx] ) ) {
  3425. ctx= 2+(ctx>>1);
  3426. val++;
  3427. if(val > 102) //prevent infinite loop
  3428. return INT_MIN;
  3429. }
  3430. if( val&0x01 )
  3431. return (val + 1)>>1 ;
  3432. else
  3433. return -((val + 1)>>1);
  3434. }
  3435. static int decode_cabac_p_mb_sub_type( H264Context *h ) {
  3436. if( get_cabac( &h->cabac, &h->cabac_state[21] ) )
  3437. return 0; /* 8x8 */
  3438. if( !get_cabac( &h->cabac, &h->cabac_state[22] ) )
  3439. return 1; /* 8x4 */
  3440. if( get_cabac( &h->cabac, &h->cabac_state[23] ) )
  3441. return 2; /* 4x8 */
  3442. return 3; /* 4x4 */
  3443. }
  3444. static int decode_cabac_b_mb_sub_type( H264Context *h ) {
  3445. int type;
  3446. if( !get_cabac( &h->cabac, &h->cabac_state[36] ) )
  3447. return 0; /* B_Direct_8x8 */
  3448. if( !get_cabac( &h->cabac, &h->cabac_state[37] ) )
  3449. return 1 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L0_8x8, B_L1_8x8 */
  3450. type = 3;
  3451. if( get_cabac( &h->cabac, &h->cabac_state[38] ) ) {
  3452. if( get_cabac( &h->cabac, &h->cabac_state[39] ) )
  3453. return 11 + get_cabac( &h->cabac, &h->cabac_state[39] ); /* B_L1_4x4, B_Bi_4x4 */
  3454. type += 4;
  3455. }
  3456. type += 2*get_cabac( &h->cabac, &h->cabac_state[39] );
  3457. type += get_cabac( &h->cabac, &h->cabac_state[39] );
  3458. return type;
  3459. }
  3460. static inline int decode_cabac_mb_transform_size( H264Context *h ) {
  3461. return get_cabac_noinline( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] );
  3462. }
  3463. static int decode_cabac_mb_ref( H264Context *h, int list, int n ) {
  3464. int refa = h->ref_cache[list][scan8[n] - 1];
  3465. int refb = h->ref_cache[list][scan8[n] - 8];
  3466. int ref = 0;
  3467. int ctx = 0;
  3468. if( h->slice_type_nos == FF_B_TYPE) {
  3469. if( refa > 0 && !h->direct_cache[scan8[n] - 1] )
  3470. ctx++;
  3471. if( refb > 0 && !h->direct_cache[scan8[n] - 8] )
  3472. ctx += 2;
  3473. } else {
  3474. if( refa > 0 )
  3475. ctx++;
  3476. if( refb > 0 )
  3477. ctx += 2;
  3478. }
  3479. while( get_cabac( &h->cabac, &h->cabac_state[54+ctx] ) ) {
  3480. ref++;
  3481. ctx = (ctx>>2)+4;
  3482. if(ref >= 32 /*h->ref_list[list]*/){
  3483. return -1;
  3484. }
  3485. }
  3486. return ref;
  3487. }
  3488. static int decode_cabac_mb_mvd( H264Context *h, int list, int n, int l ) {
  3489. int amvd = abs( h->mvd_cache[list][scan8[n] - 1][l] ) +
  3490. abs( h->mvd_cache[list][scan8[n] - 8][l] );
  3491. int ctxbase = (l == 0) ? 40 : 47;
  3492. int mvd;
  3493. int ctx = (amvd>2) + (amvd>32);
  3494. if(!get_cabac(&h->cabac, &h->cabac_state[ctxbase+ctx]))
  3495. return 0;
  3496. mvd= 1;
  3497. ctx= 3;
  3498. while( mvd < 9 && get_cabac( &h->cabac, &h->cabac_state[ctxbase+ctx] ) ) {
  3499. mvd++;
  3500. if( ctx < 6 )
  3501. ctx++;
  3502. }
  3503. if( mvd >= 9 ) {
  3504. int k = 3;
  3505. while( get_cabac_bypass( &h->cabac ) ) {
  3506. mvd += 1 << k;
  3507. k++;
  3508. if(k>24){
  3509. av_log(h->s.avctx, AV_LOG_ERROR, "overflow in decode_cabac_mb_mvd\n");
  3510. return INT_MIN;
  3511. }
  3512. }
  3513. while( k-- ) {
  3514. if( get_cabac_bypass( &h->cabac ) )
  3515. mvd += 1 << k;
  3516. }
  3517. }
  3518. return get_cabac_bypass_sign( &h->cabac, -mvd );
  3519. }
  3520. static av_always_inline int get_cabac_cbf_ctx( H264Context *h, int cat, int idx, int is_dc ) {
  3521. int nza, nzb;
  3522. int ctx = 0;
  3523. if( is_dc ) {
  3524. if( cat == 0 ) {
  3525. nza = h->left_cbp&0x100;
  3526. nzb = h-> top_cbp&0x100;
  3527. } else {
  3528. nza = (h->left_cbp>>(6+idx))&0x01;
  3529. nzb = (h-> top_cbp>>(6+idx))&0x01;
  3530. }
  3531. } else {
  3532. assert(cat == 1 || cat == 2 || cat == 4);
  3533. nza = h->non_zero_count_cache[scan8[idx] - 1];
  3534. nzb = h->non_zero_count_cache[scan8[idx] - 8];
  3535. }
  3536. if( nza > 0 )
  3537. ctx++;
  3538. if( nzb > 0 )
  3539. ctx += 2;
  3540. return ctx + 4 * cat;
  3541. }
  3542. DECLARE_ASM_CONST(1, uint8_t, last_coeff_flag_offset_8x8[63]) = {
  3543. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  3544. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  3545. 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  3546. 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
  3547. };
  3548. 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 ) {
  3549. static const int significant_coeff_flag_offset[2][6] = {
  3550. { 105+0, 105+15, 105+29, 105+44, 105+47, 402 },
  3551. { 277+0, 277+15, 277+29, 277+44, 277+47, 436 }
  3552. };
  3553. static const int last_coeff_flag_offset[2][6] = {
  3554. { 166+0, 166+15, 166+29, 166+44, 166+47, 417 },
  3555. { 338+0, 338+15, 338+29, 338+44, 338+47, 451 }
  3556. };
  3557. static const int coeff_abs_level_m1_offset[6] = {
  3558. 227+0, 227+10, 227+20, 227+30, 227+39, 426
  3559. };
  3560. static const uint8_t significant_coeff_flag_offset_8x8[2][63] = {
  3561. { 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
  3562. 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
  3563. 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
  3564. 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12 },
  3565. { 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
  3566. 6, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11,
  3567. 9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,
  3568. 9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14 }
  3569. };
  3570. /* node ctx: 0..3: abslevel1 (with abslevelgt1 == 0).
  3571. * 4..7: abslevelgt1 + 3 (and abslevel1 doesn't matter).
  3572. * map node ctx => cabac ctx for level=1 */
  3573. static const uint8_t coeff_abs_level1_ctx[8] = { 1, 2, 3, 4, 0, 0, 0, 0 };
  3574. /* map node ctx => cabac ctx for level>1 */
  3575. static const uint8_t coeff_abs_levelgt1_ctx[8] = { 5, 5, 5, 5, 6, 7, 8, 9 };
  3576. static const uint8_t coeff_abs_level_transition[2][8] = {
  3577. /* update node ctx after decoding a level=1 */
  3578. { 1, 2, 3, 3, 4, 5, 6, 7 },
  3579. /* update node ctx after decoding a level>1 */
  3580. { 4, 4, 4, 4, 5, 6, 7, 7 }
  3581. };
  3582. int index[64];
  3583. int av_unused last;
  3584. int coeff_count = 0;
  3585. int node_ctx = 0;
  3586. uint8_t *significant_coeff_ctx_base;
  3587. uint8_t *last_coeff_ctx_base;
  3588. uint8_t *abs_level_m1_ctx_base;
  3589. #if !ARCH_X86
  3590. #define CABAC_ON_STACK
  3591. #endif
  3592. #ifdef CABAC_ON_STACK
  3593. #define CC &cc
  3594. CABACContext cc;
  3595. cc.range = h->cabac.range;
  3596. cc.low = h->cabac.low;
  3597. cc.bytestream= h->cabac.bytestream;
  3598. #else
  3599. #define CC &h->cabac
  3600. #endif
  3601. /* cat: 0-> DC 16x16 n = 0
  3602. * 1-> AC 16x16 n = luma4x4idx
  3603. * 2-> Luma4x4 n = luma4x4idx
  3604. * 3-> DC Chroma n = iCbCr
  3605. * 4-> AC Chroma n = 16 + 4 * iCbCr + chroma4x4idx
  3606. * 5-> Luma8x8 n = 4 * luma8x8idx
  3607. */
  3608. /* read coded block flag */
  3609. if( is_dc || cat != 5 ) {
  3610. if( get_cabac( CC, &h->cabac_state[85 + get_cabac_cbf_ctx( h, cat, n, is_dc ) ] ) == 0 ) {
  3611. if( !is_dc )
  3612. h->non_zero_count_cache[scan8[n]] = 0;
  3613. #ifdef CABAC_ON_STACK
  3614. h->cabac.range = cc.range ;
  3615. h->cabac.low = cc.low ;
  3616. h->cabac.bytestream= cc.bytestream;
  3617. #endif
  3618. return;
  3619. }
  3620. }
  3621. significant_coeff_ctx_base = h->cabac_state
  3622. + significant_coeff_flag_offset[MB_FIELD][cat];
  3623. last_coeff_ctx_base = h->cabac_state
  3624. + last_coeff_flag_offset[MB_FIELD][cat];
  3625. abs_level_m1_ctx_base = h->cabac_state
  3626. + coeff_abs_level_m1_offset[cat];
  3627. if( !is_dc && cat == 5 ) {
  3628. #define DECODE_SIGNIFICANCE( coefs, sig_off, last_off ) \
  3629. for(last= 0; last < coefs; last++) { \
  3630. uint8_t *sig_ctx = significant_coeff_ctx_base + sig_off; \
  3631. if( get_cabac( CC, sig_ctx )) { \
  3632. uint8_t *last_ctx = last_coeff_ctx_base + last_off; \
  3633. index[coeff_count++] = last; \
  3634. if( get_cabac( CC, last_ctx ) ) { \
  3635. last= max_coeff; \
  3636. break; \
  3637. } \
  3638. } \
  3639. }\
  3640. if( last == max_coeff -1 ) {\
  3641. index[coeff_count++] = last;\
  3642. }
  3643. const uint8_t *sig_off = significant_coeff_flag_offset_8x8[MB_FIELD];
  3644. #if ARCH_X86 && HAVE_7REGS && HAVE_EBX_AVAILABLE && !defined(BROKEN_RELOCATIONS)
  3645. coeff_count= decode_significance_8x8_x86(CC, significant_coeff_ctx_base, index, sig_off);
  3646. } else {
  3647. coeff_count= decode_significance_x86(CC, max_coeff, significant_coeff_ctx_base, index);
  3648. #else
  3649. DECODE_SIGNIFICANCE( 63, sig_off[last], last_coeff_flag_offset_8x8[last] );
  3650. } else {
  3651. DECODE_SIGNIFICANCE( max_coeff - 1, last, last );
  3652. #endif
  3653. }
  3654. assert(coeff_count > 0);
  3655. if( is_dc ) {
  3656. if( cat == 0 )
  3657. h->cbp_table[h->mb_xy] |= 0x100;
  3658. else
  3659. h->cbp_table[h->mb_xy] |= 0x40 << n;
  3660. } else {
  3661. if( cat == 5 )
  3662. fill_rectangle(&h->non_zero_count_cache[scan8[n]], 2, 2, 8, coeff_count, 1);
  3663. else {
  3664. assert( cat == 1 || cat == 2 || cat == 4 );
  3665. h->non_zero_count_cache[scan8[n]] = coeff_count;
  3666. }
  3667. }
  3668. do {
  3669. uint8_t *ctx = coeff_abs_level1_ctx[node_ctx] + abs_level_m1_ctx_base;
  3670. int j= scantable[index[--coeff_count]];
  3671. if( get_cabac( CC, ctx ) == 0 ) {
  3672. node_ctx = coeff_abs_level_transition[0][node_ctx];
  3673. if( is_dc ) {
  3674. block[j] = get_cabac_bypass_sign( CC, -1);
  3675. }else{
  3676. block[j] = (get_cabac_bypass_sign( CC, -qmul[j]) + 32) >> 6;
  3677. }
  3678. } else {
  3679. int coeff_abs = 2;
  3680. ctx = coeff_abs_levelgt1_ctx[node_ctx] + abs_level_m1_ctx_base;
  3681. node_ctx = coeff_abs_level_transition[1][node_ctx];
  3682. while( coeff_abs < 15 && get_cabac( CC, ctx ) ) {
  3683. coeff_abs++;
  3684. }
  3685. if( coeff_abs >= 15 ) {
  3686. int j = 0;
  3687. while( get_cabac_bypass( CC ) ) {
  3688. j++;
  3689. }
  3690. coeff_abs=1;
  3691. while( j-- ) {
  3692. coeff_abs += coeff_abs + get_cabac_bypass( CC );
  3693. }
  3694. coeff_abs+= 14;
  3695. }
  3696. if( is_dc ) {
  3697. block[j] = get_cabac_bypass_sign( CC, -coeff_abs );
  3698. }else{
  3699. block[j] = (get_cabac_bypass_sign( CC, -coeff_abs ) * qmul[j] + 32) >> 6;
  3700. }
  3701. }
  3702. } while( coeff_count );
  3703. #ifdef CABAC_ON_STACK
  3704. h->cabac.range = cc.range ;
  3705. h->cabac.low = cc.low ;
  3706. h->cabac.bytestream= cc.bytestream;
  3707. #endif
  3708. }
  3709. #if !CONFIG_SMALL
  3710. 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 ) {
  3711. decode_cabac_residual_internal(h, block, cat, n, scantable, qmul, max_coeff, 1);
  3712. }
  3713. 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 ) {
  3714. decode_cabac_residual_internal(h, block, cat, n, scantable, qmul, max_coeff, 0);
  3715. }
  3716. #endif
  3717. static void decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff ) {
  3718. #if CONFIG_SMALL
  3719. decode_cabac_residual_internal(h, block, cat, n, scantable, qmul, max_coeff, cat == 0 || cat == 3);
  3720. #else
  3721. if( cat == 0 || cat == 3 ) decode_cabac_residual_dc(h, block, cat, n, scantable, qmul, max_coeff);
  3722. else decode_cabac_residual_nondc(h, block, cat, n, scantable, qmul, max_coeff);
  3723. #endif
  3724. }
  3725. static inline void compute_mb_neighbors(H264Context *h)
  3726. {
  3727. MpegEncContext * const s = &h->s;
  3728. const int mb_xy = h->mb_xy;
  3729. h->top_mb_xy = mb_xy - s->mb_stride;
  3730. h->left_mb_xy[0] = mb_xy - 1;
  3731. if(FRAME_MBAFF){
  3732. const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
  3733. const int top_pair_xy = pair_xy - s->mb_stride;
  3734. const int top_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
  3735. const int left_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
  3736. const int curr_mb_field_flag = MB_FIELD;
  3737. const int bottom = (s->mb_y & 1);
  3738. if (curr_mb_field_flag && (bottom || top_mb_field_flag)){
  3739. h->top_mb_xy -= s->mb_stride;
  3740. }
  3741. if (!left_mb_field_flag == curr_mb_field_flag) {
  3742. h->left_mb_xy[0] = pair_xy - 1;
  3743. }
  3744. } else if (FIELD_PICTURE) {
  3745. h->top_mb_xy -= s->mb_stride;
  3746. }
  3747. return;
  3748. }
  3749. /**
  3750. * decodes a macroblock
  3751. * @returns 0 if OK, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
  3752. */
  3753. static int decode_mb_cabac(H264Context *h) {
  3754. MpegEncContext * const s = &h->s;
  3755. int mb_xy;
  3756. int mb_type, partition_count, cbp = 0;
  3757. int dct8x8_allowed= h->pps.transform_8x8_mode;
  3758. mb_xy = h->mb_xy = s->mb_x + s->mb_y*s->mb_stride;
  3759. tprintf(s->avctx, "pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
  3760. if( h->slice_type_nos != FF_I_TYPE ) {
  3761. int skip;
  3762. /* a skipped mb needs the aff flag from the following mb */
  3763. if( FRAME_MBAFF && s->mb_x==0 && (s->mb_y&1)==0 )
  3764. predict_field_decoding_flag(h);
  3765. if( FRAME_MBAFF && (s->mb_y&1)==1 && h->prev_mb_skipped )
  3766. skip = h->next_mb_skipped;
  3767. else
  3768. skip = decode_cabac_mb_skip( h, s->mb_x, s->mb_y );
  3769. /* read skip flags */
  3770. if( skip ) {
  3771. if( FRAME_MBAFF && (s->mb_y&1)==0 ){
  3772. s->current_picture.mb_type[mb_xy] = MB_TYPE_SKIP;
  3773. h->next_mb_skipped = decode_cabac_mb_skip( h, s->mb_x, s->mb_y+1 );
  3774. if(!h->next_mb_skipped)
  3775. h->mb_mbaff = h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  3776. }
  3777. decode_mb_skip(h);
  3778. h->cbp_table[mb_xy] = 0;
  3779. h->chroma_pred_mode_table[mb_xy] = 0;
  3780. h->last_qscale_diff = 0;
  3781. return 0;
  3782. }
  3783. }
  3784. if(FRAME_MBAFF){
  3785. if( (s->mb_y&1) == 0 )
  3786. h->mb_mbaff =
  3787. h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
  3788. }
  3789. h->prev_mb_skipped = 0;
  3790. compute_mb_neighbors(h);
  3791. if( h->slice_type_nos == FF_B_TYPE ) {
  3792. mb_type = decode_cabac_mb_type_b( h );
  3793. if( mb_type < 23 ){
  3794. partition_count= b_mb_type_info[mb_type].partition_count;
  3795. mb_type= b_mb_type_info[mb_type].type;
  3796. }else{
  3797. mb_type -= 23;
  3798. goto decode_intra_mb;
  3799. }
  3800. } else if( h->slice_type_nos == FF_P_TYPE ) {
  3801. if( get_cabac_noinline( &h->cabac, &h->cabac_state[14] ) == 0 ) {
  3802. /* P-type */
  3803. if( get_cabac_noinline( &h->cabac, &h->cabac_state[15] ) == 0 ) {
  3804. /* P_L0_D16x16, P_8x8 */
  3805. mb_type= 3 * get_cabac_noinline( &h->cabac, &h->cabac_state[16] );
  3806. } else {
  3807. /* P_L0_D8x16, P_L0_D16x8 */
  3808. mb_type= 2 - get_cabac_noinline( &h->cabac, &h->cabac_state[17] );
  3809. }
  3810. partition_count= p_mb_type_info[mb_type].partition_count;
  3811. mb_type= p_mb_type_info[mb_type].type;
  3812. } else {
  3813. mb_type= decode_cabac_intra_mb_type(h, 17, 0);
  3814. goto decode_intra_mb;
  3815. }
  3816. } else {
  3817. mb_type= decode_cabac_intra_mb_type(h, 3, 1);
  3818. if(h->slice_type == FF_SI_TYPE && mb_type)
  3819. mb_type--;
  3820. assert(h->slice_type_nos == FF_I_TYPE);
  3821. decode_intra_mb:
  3822. partition_count = 0;
  3823. cbp= i_mb_type_info[mb_type].cbp;
  3824. h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
  3825. mb_type= i_mb_type_info[mb_type].type;
  3826. }
  3827. if(MB_FIELD)
  3828. mb_type |= MB_TYPE_INTERLACED;
  3829. h->slice_table[ mb_xy ]= h->slice_num;
  3830. if(IS_INTRA_PCM(mb_type)) {
  3831. const uint8_t *ptr;
  3832. // We assume these blocks are very rare so we do not optimize it.
  3833. // FIXME The two following lines get the bitstream position in the cabac
  3834. // decode, I think it should be done by a function in cabac.h (or cabac.c).
  3835. ptr= h->cabac.bytestream;
  3836. if(h->cabac.low&0x1) ptr--;
  3837. if(CABAC_BITS==16){
  3838. if(h->cabac.low&0x1FF) ptr--;
  3839. }
  3840. // The pixels are stored in the same order as levels in h->mb array.
  3841. memcpy(h->mb, ptr, 256); ptr+=256;
  3842. if(CHROMA){
  3843. memcpy(h->mb+128, ptr, 128); ptr+=128;
  3844. }
  3845. ff_init_cabac_decoder(&h->cabac, ptr, h->cabac.bytestream_end - ptr);
  3846. // All blocks are present
  3847. h->cbp_table[mb_xy] = 0x1ef;
  3848. h->chroma_pred_mode_table[mb_xy] = 0;
  3849. // In deblocking, the quantizer is 0
  3850. s->current_picture.qscale_table[mb_xy]= 0;
  3851. // All coeffs are present
  3852. memset(h->non_zero_count[mb_xy], 16, 16);
  3853. s->current_picture.mb_type[mb_xy]= mb_type;
  3854. h->last_qscale_diff = 0;
  3855. return 0;
  3856. }
  3857. if(MB_MBAFF){
  3858. h->ref_count[0] <<= 1;
  3859. h->ref_count[1] <<= 1;
  3860. }
  3861. fill_caches(h, mb_type, 0);
  3862. if( IS_INTRA( mb_type ) ) {
  3863. int i, pred_mode;
  3864. if( IS_INTRA4x4( mb_type ) ) {
  3865. if( dct8x8_allowed && decode_cabac_mb_transform_size( h ) ) {
  3866. mb_type |= MB_TYPE_8x8DCT;
  3867. for( i = 0; i < 16; i+=4 ) {
  3868. int pred = pred_intra_mode( h, i );
  3869. int mode = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  3870. fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
  3871. }
  3872. } else {
  3873. for( i = 0; i < 16; i++ ) {
  3874. int pred = pred_intra_mode( h, i );
  3875. h->intra4x4_pred_mode_cache[ scan8[i] ] = decode_cabac_mb_intra4x4_pred_mode( h, pred );
  3876. //av_log( s->avctx, AV_LOG_ERROR, "i4x4 pred=%d mode=%d\n", pred, h->intra4x4_pred_mode_cache[ scan8[i] ] );
  3877. }
  3878. }
  3879. ff_h264_write_back_intra_pred_mode(h);
  3880. if( ff_h264_check_intra4x4_pred_mode(h) < 0 ) return -1;
  3881. } else {
  3882. h->intra16x16_pred_mode= ff_h264_check_intra_pred_mode( h, h->intra16x16_pred_mode );
  3883. if( h->intra16x16_pred_mode < 0 ) return -1;
  3884. }
  3885. if(CHROMA){
  3886. h->chroma_pred_mode_table[mb_xy] =
  3887. pred_mode = decode_cabac_mb_chroma_pre_mode( h );
  3888. pred_mode= ff_h264_check_intra_pred_mode( h, pred_mode );
  3889. if( pred_mode < 0 ) return -1;
  3890. h->chroma_pred_mode= pred_mode;
  3891. }
  3892. } else if( partition_count == 4 ) {
  3893. int i, j, sub_partition_count[4], list, ref[2][4];
  3894. if( h->slice_type_nos == FF_B_TYPE ) {
  3895. for( i = 0; i < 4; i++ ) {
  3896. h->sub_mb_type[i] = decode_cabac_b_mb_sub_type( h );
  3897. sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  3898. h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  3899. }
  3900. if( IS_DIRECT(h->sub_mb_type[0] | h->sub_mb_type[1] |
  3901. h->sub_mb_type[2] | h->sub_mb_type[3]) ) {
  3902. ff_h264_pred_direct_motion(h, &mb_type);
  3903. h->ref_cache[0][scan8[4]] =
  3904. h->ref_cache[1][scan8[4]] =
  3905. h->ref_cache[0][scan8[12]] =
  3906. h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
  3907. if( h->ref_count[0] > 1 || h->ref_count[1] > 1 ) {
  3908. for( i = 0; i < 4; i++ )
  3909. if( IS_DIRECT(h->sub_mb_type[i]) )
  3910. fill_rectangle( &h->direct_cache[scan8[4*i]], 2, 2, 8, 1, 1 );
  3911. }
  3912. }
  3913. } else {
  3914. for( i = 0; i < 4; i++ ) {
  3915. h->sub_mb_type[i] = decode_cabac_p_mb_sub_type( h );
  3916. sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
  3917. h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
  3918. }
  3919. }
  3920. for( list = 0; list < h->list_count; list++ ) {
  3921. for( i = 0; i < 4; i++ ) {
  3922. if(IS_DIRECT(h->sub_mb_type[i])) continue;
  3923. if(IS_DIR(h->sub_mb_type[i], 0, list)){
  3924. if( h->ref_count[list] > 1 ){
  3925. ref[list][i] = decode_cabac_mb_ref( h, list, 4*i );
  3926. if(ref[list][i] >= (unsigned)h->ref_count[list]){
  3927. av_log(s->avctx, AV_LOG_ERROR, "Reference %d >= %d\n", ref[list][i], h->ref_count[list]);
  3928. return -1;
  3929. }
  3930. }else
  3931. ref[list][i] = 0;
  3932. } else {
  3933. ref[list][i] = -1;
  3934. }
  3935. h->ref_cache[list][ scan8[4*i]+1 ]=
  3936. h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
  3937. }
  3938. }
  3939. if(dct8x8_allowed)
  3940. dct8x8_allowed = get_dct8x8_allowed(h);
  3941. for(list=0; list<h->list_count; list++){
  3942. for(i=0; i<4; i++){
  3943. h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ];
  3944. if(IS_DIRECT(h->sub_mb_type[i])){
  3945. fill_rectangle(h->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 4);
  3946. continue;
  3947. }
  3948. if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){
  3949. const int sub_mb_type= h->sub_mb_type[i];
  3950. const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
  3951. for(j=0; j<sub_partition_count[i]; j++){
  3952. int mpx, mpy;
  3953. int mx, my;
  3954. const int index= 4*i + block_width*j;
  3955. int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
  3956. int16_t (* mvd_cache)[2]= &h->mvd_cache[list][ scan8[index] ];
  3957. pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mpx, &mpy);
  3958. mx = mpx + decode_cabac_mb_mvd( h, list, index, 0 );
  3959. my = mpy + decode_cabac_mb_mvd( h, list, index, 1 );
  3960. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  3961. if(IS_SUB_8X8(sub_mb_type)){
  3962. mv_cache[ 1 ][0]=
  3963. mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
  3964. mv_cache[ 1 ][1]=
  3965. mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
  3966. mvd_cache[ 1 ][0]=
  3967. mvd_cache[ 8 ][0]= mvd_cache[ 9 ][0]= mx - mpx;
  3968. mvd_cache[ 1 ][1]=
  3969. mvd_cache[ 8 ][1]= mvd_cache[ 9 ][1]= my - mpy;
  3970. }else if(IS_SUB_8X4(sub_mb_type)){
  3971. mv_cache[ 1 ][0]= mx;
  3972. mv_cache[ 1 ][1]= my;
  3973. mvd_cache[ 1 ][0]= mx - mpx;
  3974. mvd_cache[ 1 ][1]= my - mpy;
  3975. }else if(IS_SUB_4X8(sub_mb_type)){
  3976. mv_cache[ 8 ][0]= mx;
  3977. mv_cache[ 8 ][1]= my;
  3978. mvd_cache[ 8 ][0]= mx - mpx;
  3979. mvd_cache[ 8 ][1]= my - mpy;
  3980. }
  3981. mv_cache[ 0 ][0]= mx;
  3982. mv_cache[ 0 ][1]= my;
  3983. mvd_cache[ 0 ][0]= mx - mpx;
  3984. mvd_cache[ 0 ][1]= my - mpy;
  3985. }
  3986. }else{
  3987. uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
  3988. uint32_t *pd= (uint32_t *)&h->mvd_cache[list][ scan8[4*i] ][0];
  3989. p[0] = p[1] = p[8] = p[9] = 0;
  3990. pd[0]= pd[1]= pd[8]= pd[9]= 0;
  3991. }
  3992. }
  3993. }
  3994. } else if( IS_DIRECT(mb_type) ) {
  3995. ff_h264_pred_direct_motion(h, &mb_type);
  3996. fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
  3997. fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);
  3998. dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
  3999. } else {
  4000. int list, mx, my, i, mpx, mpy;
  4001. if(IS_16X16(mb_type)){
  4002. for(list=0; list<h->list_count; list++){
  4003. if(IS_DIR(mb_type, 0, list)){
  4004. int ref;
  4005. if(h->ref_count[list] > 1){
  4006. ref= decode_cabac_mb_ref(h, list, 0);
  4007. if(ref >= (unsigned)h->ref_count[list]){
  4008. av_log(s->avctx, AV_LOG_ERROR, "Reference %d >= %d\n", ref, h->ref_count[list]);
  4009. return -1;
  4010. }
  4011. }else
  4012. ref=0;
  4013. fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1);
  4014. }else
  4015. 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
  4016. }
  4017. for(list=0; list<h->list_count; list++){
  4018. if(IS_DIR(mb_type, 0, list)){
  4019. pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mpx, &mpy);
  4020. mx = mpx + decode_cabac_mb_mvd( h, list, 0, 0 );
  4021. my = mpy + decode_cabac_mb_mvd( h, list, 0, 1 );
  4022. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4023. fill_rectangle(h->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  4024. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);
  4025. }else
  4026. fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 4);
  4027. }
  4028. }
  4029. else if(IS_16X8(mb_type)){
  4030. for(list=0; list<h->list_count; list++){
  4031. for(i=0; i<2; i++){
  4032. if(IS_DIR(mb_type, i, list)){
  4033. int ref;
  4034. if(h->ref_count[list] > 1){
  4035. ref= decode_cabac_mb_ref( h, list, 8*i );
  4036. if(ref >= (unsigned)h->ref_count[list]){
  4037. av_log(s->avctx, AV_LOG_ERROR, "Reference %d >= %d\n", ref, h->ref_count[list]);
  4038. return -1;
  4039. }
  4040. }else
  4041. ref=0;
  4042. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, ref, 1);
  4043. }else
  4044. fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
  4045. }
  4046. }
  4047. for(list=0; list<h->list_count; list++){
  4048. for(i=0; i<2; i++){
  4049. if(IS_DIR(mb_type, i, list)){
  4050. pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mpx, &mpy);
  4051. mx = mpx + decode_cabac_mb_mvd( h, list, 8*i, 0 );
  4052. my = mpy + decode_cabac_mb_mvd( h, list, 8*i, 1 );
  4053. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4054. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx-mpx,my-mpy), 4);
  4055. fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
  4056. }else{
  4057. fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  4058. fill_rectangle(h-> mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
  4059. }
  4060. }
  4061. }
  4062. }else{
  4063. assert(IS_8X16(mb_type));
  4064. for(list=0; list<h->list_count; list++){
  4065. for(i=0; i<2; i++){
  4066. if(IS_DIR(mb_type, i, list)){ //FIXME optimize
  4067. int ref;
  4068. if(h->ref_count[list] > 1){
  4069. ref= decode_cabac_mb_ref( h, list, 4*i );
  4070. if(ref >= (unsigned)h->ref_count[list]){
  4071. av_log(s->avctx, AV_LOG_ERROR, "Reference %d >= %d\n", ref, h->ref_count[list]);
  4072. return -1;
  4073. }
  4074. }else
  4075. ref=0;
  4076. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, ref, 1);
  4077. }else
  4078. fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
  4079. }
  4080. }
  4081. for(list=0; list<h->list_count; list++){
  4082. for(i=0; i<2; i++){
  4083. if(IS_DIR(mb_type, i, list)){
  4084. pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mpx, &mpy);
  4085. mx = mpx + decode_cabac_mb_mvd( h, list, 4*i, 0 );
  4086. my = mpy + decode_cabac_mb_mvd( h, list, 4*i, 1 );
  4087. tprintf(s->avctx, "final mv:%d %d\n", mx, my);
  4088. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx-mpx,my-mpy), 4);
  4089. fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
  4090. }else{
  4091. fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  4092. fill_rectangle(h-> mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
  4093. }
  4094. }
  4095. }
  4096. }
  4097. }
  4098. if( IS_INTER( mb_type ) ) {
  4099. h->chroma_pred_mode_table[mb_xy] = 0;
  4100. write_back_motion( h, mb_type );
  4101. }
  4102. if( !IS_INTRA16x16( mb_type ) ) {
  4103. cbp = decode_cabac_mb_cbp_luma( h );
  4104. if(CHROMA)
  4105. cbp |= decode_cabac_mb_cbp_chroma( h ) << 4;
  4106. }
  4107. h->cbp_table[mb_xy] = h->cbp = cbp;
  4108. if( dct8x8_allowed && (cbp&15) && !IS_INTRA( mb_type ) ) {
  4109. if( decode_cabac_mb_transform_size( h ) )
  4110. mb_type |= MB_TYPE_8x8DCT;
  4111. }
  4112. s->current_picture.mb_type[mb_xy]= mb_type;
  4113. if( cbp || IS_INTRA16x16( mb_type ) ) {
  4114. const uint8_t *scan, *scan8x8, *dc_scan;
  4115. const uint32_t *qmul;
  4116. int dqp;
  4117. if(IS_INTERLACED(mb_type)){
  4118. scan8x8= s->qscale ? h->field_scan8x8 : h->field_scan8x8_q0;
  4119. scan= s->qscale ? h->field_scan : h->field_scan_q0;
  4120. dc_scan= luma_dc_field_scan;
  4121. }else{
  4122. scan8x8= s->qscale ? h->zigzag_scan8x8 : h->zigzag_scan8x8_q0;
  4123. scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
  4124. dc_scan= luma_dc_zigzag_scan;
  4125. }
  4126. h->last_qscale_diff = dqp = decode_cabac_mb_dqp( h );
  4127. if( dqp == INT_MIN ){
  4128. av_log(h->s.avctx, AV_LOG_ERROR, "cabac decode of qscale diff failed at %d %d\n", s->mb_x, s->mb_y);
  4129. return -1;
  4130. }
  4131. s->qscale += dqp;
  4132. if(((unsigned)s->qscale) > 51){
  4133. if(s->qscale<0) s->qscale+= 52;
  4134. else s->qscale-= 52;
  4135. }
  4136. h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
  4137. h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
  4138. if( IS_INTRA16x16( mb_type ) ) {
  4139. int i;
  4140. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 DC\n" );
  4141. decode_cabac_residual( h, h->mb, 0, 0, dc_scan, NULL, 16);
  4142. if( cbp&15 ) {
  4143. qmul = h->dequant4_coeff[0][s->qscale];
  4144. for( i = 0; i < 16; i++ ) {
  4145. //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 AC:%d\n", i );
  4146. decode_cabac_residual(h, h->mb + 16*i, 1, i, scan + 1, qmul, 15);
  4147. }
  4148. } else {
  4149. fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
  4150. }
  4151. } else {
  4152. int i8x8, i4x4;
  4153. for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
  4154. if( cbp & (1<<i8x8) ) {
  4155. if( IS_8x8DCT(mb_type) ) {
  4156. decode_cabac_residual(h, h->mb + 64*i8x8, 5, 4*i8x8,
  4157. scan8x8, h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 64);
  4158. } else {
  4159. qmul = h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale];
  4160. for( i4x4 = 0; i4x4 < 4; i4x4++ ) {
  4161. const int index = 4*i8x8 + i4x4;
  4162. //av_log( s->avctx, AV_LOG_ERROR, "Luma4x4: %d\n", index );
  4163. //START_TIMER
  4164. decode_cabac_residual(h, h->mb + 16*index, 2, index, scan, qmul, 16);
  4165. //STOP_TIMER("decode_residual")
  4166. }
  4167. }
  4168. } else {
  4169. uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
  4170. nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
  4171. }
  4172. }
  4173. }
  4174. if( cbp&0x30 ){
  4175. int c;
  4176. for( c = 0; c < 2; c++ ) {
  4177. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-DC\n",c );
  4178. decode_cabac_residual(h, h->mb + 256 + 16*4*c, 3, c, chroma_dc_scan, NULL, 4);
  4179. }
  4180. }
  4181. if( cbp&0x20 ) {
  4182. int c, i;
  4183. for( c = 0; c < 2; c++ ) {
  4184. qmul = h->dequant4_coeff[c+1+(IS_INTRA( mb_type ) ? 0:3)][h->chroma_qp[c]];
  4185. for( i = 0; i < 4; i++ ) {
  4186. const int index = 16 + 4 * c + i;
  4187. //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-AC %d\n",c, index - 16 );
  4188. decode_cabac_residual(h, h->mb + 16*index, 4, index, scan + 1, qmul, 15);
  4189. }
  4190. }
  4191. } else {
  4192. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4193. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4194. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4195. }
  4196. } else {
  4197. uint8_t * const nnz= &h->non_zero_count_cache[0];
  4198. fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
  4199. nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
  4200. nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
  4201. h->last_qscale_diff = 0;
  4202. }
  4203. s->current_picture.qscale_table[mb_xy]= s->qscale;
  4204. write_back_non_zero_count(h);
  4205. if(MB_MBAFF){
  4206. h->ref_count[0] >>= 1;
  4207. h->ref_count[1] >>= 1;
  4208. }
  4209. return 0;
  4210. }
  4211. static int decode_slice(struct AVCodecContext *avctx, void *arg){
  4212. H264Context *h = *(void**)arg;
  4213. MpegEncContext * const s = &h->s;
  4214. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  4215. s->mb_skip_run= -1;
  4216. h->is_complex = FRAME_MBAFF || s->picture_structure != PICT_FRAME || s->codec_id != CODEC_ID_H264 ||
  4217. (CONFIG_GRAY && (s->flags&CODEC_FLAG_GRAY));
  4218. if( h->pps.cabac ) {
  4219. int i;
  4220. /* realign */
  4221. align_get_bits( &s->gb );
  4222. /* init cabac */
  4223. ff_init_cabac_states( &h->cabac);
  4224. ff_init_cabac_decoder( &h->cabac,
  4225. s->gb.buffer + get_bits_count(&s->gb)/8,
  4226. (get_bits_left(&s->gb) + 7)/8);
  4227. /* calculate pre-state */
  4228. for( i= 0; i < 460; i++ ) {
  4229. int pre;
  4230. if( h->slice_type_nos == FF_I_TYPE )
  4231. pre = av_clip( ((cabac_context_init_I[i][0] * s->qscale) >>4 ) + cabac_context_init_I[i][1], 1, 126 );
  4232. else
  4233. 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 );
  4234. if( pre <= 63 )
  4235. h->cabac_state[i] = 2 * ( 63 - pre ) + 0;
  4236. else
  4237. h->cabac_state[i] = 2 * ( pre - 64 ) + 1;
  4238. }
  4239. for(;;){
  4240. //START_TIMER
  4241. int ret = decode_mb_cabac(h);
  4242. int eos;
  4243. //STOP_TIMER("decode_mb_cabac")
  4244. if(ret>=0) ff_h264_hl_decode_mb(h);
  4245. if( ret >= 0 && FRAME_MBAFF ) { //FIXME optimal? or let mb_decode decode 16x32 ?
  4246. s->mb_y++;
  4247. ret = decode_mb_cabac(h);
  4248. if(ret>=0) ff_h264_hl_decode_mb(h);
  4249. s->mb_y--;
  4250. }
  4251. eos = get_cabac_terminate( &h->cabac );
  4252. if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
  4253. 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);
  4254. 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);
  4255. return -1;
  4256. }
  4257. if( ++s->mb_x >= s->mb_width ) {
  4258. s->mb_x = 0;
  4259. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  4260. ++s->mb_y;
  4261. if(FIELD_OR_MBAFF_PICTURE) {
  4262. ++s->mb_y;
  4263. }
  4264. }
  4265. if( eos || s->mb_y >= s->mb_height ) {
  4266. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  4267. 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);
  4268. return 0;
  4269. }
  4270. }
  4271. } else {
  4272. for(;;){
  4273. int ret = decode_mb_cavlc(h);
  4274. if(ret>=0) ff_h264_hl_decode_mb(h);
  4275. if(ret>=0 && FRAME_MBAFF){ //FIXME optimal? or let mb_decode decode 16x32 ?
  4276. s->mb_y++;
  4277. ret = decode_mb_cavlc(h);
  4278. if(ret>=0) ff_h264_hl_decode_mb(h);
  4279. s->mb_y--;
  4280. }
  4281. if(ret<0){
  4282. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  4283. 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);
  4284. return -1;
  4285. }
  4286. if(++s->mb_x >= s->mb_width){
  4287. s->mb_x=0;
  4288. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  4289. ++s->mb_y;
  4290. if(FIELD_OR_MBAFF_PICTURE) {
  4291. ++s->mb_y;
  4292. }
  4293. if(s->mb_y >= s->mb_height){
  4294. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  4295. if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
  4296. 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);
  4297. return 0;
  4298. }else{
  4299. 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);
  4300. return -1;
  4301. }
  4302. }
  4303. }
  4304. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  4305. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  4306. if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
  4307. 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);
  4308. return 0;
  4309. }else{
  4310. 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);
  4311. return -1;
  4312. }
  4313. }
  4314. }
  4315. }
  4316. #if 0
  4317. for(;s->mb_y < s->mb_height; s->mb_y++){
  4318. for(;s->mb_x < s->mb_width; s->mb_x++){
  4319. int ret= decode_mb(h);
  4320. ff_h264_hl_decode_mb(h);
  4321. if(ret<0){
  4322. av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  4323. 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);
  4324. return -1;
  4325. }
  4326. if(++s->mb_x >= s->mb_width){
  4327. s->mb_x=0;
  4328. if(++s->mb_y >= s->mb_height){
  4329. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  4330. 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);
  4331. return 0;
  4332. }else{
  4333. 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);
  4334. return -1;
  4335. }
  4336. }
  4337. }
  4338. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  4339. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  4340. 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);
  4341. return 0;
  4342. }else{
  4343. 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);
  4344. return -1;
  4345. }
  4346. }
  4347. }
  4348. s->mb_x=0;
  4349. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  4350. }
  4351. #endif
  4352. return -1; //not reached
  4353. }
  4354. /**
  4355. * Call decode_slice() for each context.
  4356. *
  4357. * @param h h264 master context
  4358. * @param context_count number of contexts to execute
  4359. */
  4360. static void execute_decode_slices(H264Context *h, int context_count){
  4361. MpegEncContext * const s = &h->s;
  4362. AVCodecContext * const avctx= s->avctx;
  4363. H264Context *hx;
  4364. int i;
  4365. if (s->avctx->hwaccel)
  4366. return;
  4367. if(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  4368. return;
  4369. if(context_count == 1) {
  4370. decode_slice(avctx, &h);
  4371. } else {
  4372. for(i = 1; i < context_count; i++) {
  4373. hx = h->thread_context[i];
  4374. hx->s.error_recognition = avctx->error_recognition;
  4375. hx->s.error_count = 0;
  4376. }
  4377. avctx->execute(avctx, (void *)decode_slice,
  4378. h->thread_context, NULL, context_count, sizeof(void*));
  4379. /* pull back stuff from slices to master context */
  4380. hx = h->thread_context[context_count - 1];
  4381. s->mb_x = hx->s.mb_x;
  4382. s->mb_y = hx->s.mb_y;
  4383. s->dropable = hx->s.dropable;
  4384. s->picture_structure = hx->s.picture_structure;
  4385. for(i = 1; i < context_count; i++)
  4386. h->s.error_count += h->thread_context[i]->s.error_count;
  4387. }
  4388. }
  4389. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size){
  4390. MpegEncContext * const s = &h->s;
  4391. AVCodecContext * const avctx= s->avctx;
  4392. int buf_index=0;
  4393. H264Context *hx; ///< thread context
  4394. int context_count = 0;
  4395. int next_avc= h->is_avc ? 0 : buf_size;
  4396. h->max_contexts = avctx->thread_count;
  4397. #if 0
  4398. int i;
  4399. for(i=0; i<50; i++){
  4400. av_log(NULL, AV_LOG_ERROR,"%02X ", buf[i]);
  4401. }
  4402. #endif
  4403. if(!(s->flags2 & CODEC_FLAG2_CHUNKS)){
  4404. h->current_slice = 0;
  4405. if (!s->first_field)
  4406. s->current_picture_ptr= NULL;
  4407. ff_h264_reset_sei(h);
  4408. }
  4409. for(;;){
  4410. int consumed;
  4411. int dst_length;
  4412. int bit_length;
  4413. const uint8_t *ptr;
  4414. int i, nalsize = 0;
  4415. int err;
  4416. if(buf_index >= next_avc) {
  4417. if(buf_index >= buf_size) break;
  4418. nalsize = 0;
  4419. for(i = 0; i < h->nal_length_size; i++)
  4420. nalsize = (nalsize << 8) | buf[buf_index++];
  4421. if(nalsize <= 1 || nalsize > buf_size - buf_index){
  4422. if(nalsize == 1){
  4423. buf_index++;
  4424. continue;
  4425. }else{
  4426. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  4427. break;
  4428. }
  4429. }
  4430. next_avc= buf_index + nalsize;
  4431. } else {
  4432. // start code prefix search
  4433. for(; buf_index + 3 < next_avc; buf_index++){
  4434. // This should always succeed in the first iteration.
  4435. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  4436. break;
  4437. }
  4438. if(buf_index+3 >= buf_size) break;
  4439. buf_index+=3;
  4440. if(buf_index >= next_avc) continue;
  4441. }
  4442. hx = h->thread_context[context_count];
  4443. ptr= ff_h264_decode_nal(hx, buf + buf_index, &dst_length, &consumed, next_avc - buf_index);
  4444. if (ptr==NULL || dst_length < 0){
  4445. return -1;
  4446. }
  4447. while(ptr[dst_length - 1] == 0 && dst_length > 0)
  4448. dst_length--;
  4449. bit_length= !dst_length ? 0 : (8*dst_length - ff_h264_decode_rbsp_trailing(h, ptr + dst_length - 1));
  4450. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  4451. 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);
  4452. }
  4453. if (h->is_avc && (nalsize != consumed) && nalsize){
  4454. av_log(h->s.avctx, AV_LOG_DEBUG, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
  4455. }
  4456. buf_index += consumed;
  4457. if( (s->hurry_up == 1 && h->nal_ref_idc == 0) //FIXME do not discard SEI id
  4458. ||(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  4459. continue;
  4460. again:
  4461. err = 0;
  4462. switch(hx->nal_unit_type){
  4463. case NAL_IDR_SLICE:
  4464. if (h->nal_unit_type != NAL_IDR_SLICE) {
  4465. av_log(h->s.avctx, AV_LOG_ERROR, "Invalid mix of idr and non-idr slices");
  4466. return -1;
  4467. }
  4468. idr(h); //FIXME ensure we don't loose some frames if there is reordering
  4469. case NAL_SLICE:
  4470. init_get_bits(&hx->s.gb, ptr, bit_length);
  4471. hx->intra_gb_ptr=
  4472. hx->inter_gb_ptr= &hx->s.gb;
  4473. hx->s.data_partitioning = 0;
  4474. if((err = decode_slice_header(hx, h)))
  4475. break;
  4476. if (s->avctx->hwaccel && h->current_slice == 1) {
  4477. if (s->avctx->hwaccel->start_frame(s->avctx, NULL, 0) < 0)
  4478. return -1;
  4479. }
  4480. s->current_picture_ptr->key_frame |=
  4481. (hx->nal_unit_type == NAL_IDR_SLICE) ||
  4482. (h->sei_recovery_frame_cnt >= 0);
  4483. if(hx->redundant_pic_count==0 && hx->s.hurry_up < 5
  4484. && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
  4485. && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=FF_B_TYPE)
  4486. && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==FF_I_TYPE)
  4487. && avctx->skip_frame < AVDISCARD_ALL){
  4488. if(avctx->hwaccel) {
  4489. if (avctx->hwaccel->decode_slice(avctx, &buf[buf_index - consumed], consumed) < 0)
  4490. return -1;
  4491. }else
  4492. if(CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
  4493. static const uint8_t start_code[] = {0x00, 0x00, 0x01};
  4494. ff_vdpau_add_data_chunk(s, start_code, sizeof(start_code));
  4495. ff_vdpau_add_data_chunk(s, &buf[buf_index - consumed], consumed );
  4496. }else
  4497. context_count++;
  4498. }
  4499. break;
  4500. case NAL_DPA:
  4501. init_get_bits(&hx->s.gb, ptr, bit_length);
  4502. hx->intra_gb_ptr=
  4503. hx->inter_gb_ptr= NULL;
  4504. if ((err = decode_slice_header(hx, h)) < 0)
  4505. break;
  4506. hx->s.data_partitioning = 1;
  4507. break;
  4508. case NAL_DPB:
  4509. init_get_bits(&hx->intra_gb, ptr, bit_length);
  4510. hx->intra_gb_ptr= &hx->intra_gb;
  4511. break;
  4512. case NAL_DPC:
  4513. init_get_bits(&hx->inter_gb, ptr, bit_length);
  4514. hx->inter_gb_ptr= &hx->inter_gb;
  4515. if(hx->redundant_pic_count==0 && hx->intra_gb_ptr && hx->s.data_partitioning
  4516. && s->context_initialized
  4517. && s->hurry_up < 5
  4518. && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
  4519. && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=FF_B_TYPE)
  4520. && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==FF_I_TYPE)
  4521. && avctx->skip_frame < AVDISCARD_ALL)
  4522. context_count++;
  4523. break;
  4524. case NAL_SEI:
  4525. init_get_bits(&s->gb, ptr, bit_length);
  4526. ff_h264_decode_sei(h);
  4527. break;
  4528. case NAL_SPS:
  4529. init_get_bits(&s->gb, ptr, bit_length);
  4530. ff_h264_decode_seq_parameter_set(h);
  4531. if(s->flags& CODEC_FLAG_LOW_DELAY)
  4532. s->low_delay=1;
  4533. if(avctx->has_b_frames < 2)
  4534. avctx->has_b_frames= !s->low_delay;
  4535. break;
  4536. case NAL_PPS:
  4537. init_get_bits(&s->gb, ptr, bit_length);
  4538. ff_h264_decode_picture_parameter_set(h, bit_length);
  4539. break;
  4540. case NAL_AUD:
  4541. case NAL_END_SEQUENCE:
  4542. case NAL_END_STREAM:
  4543. case NAL_FILLER_DATA:
  4544. case NAL_SPS_EXT:
  4545. case NAL_AUXILIARY_SLICE:
  4546. break;
  4547. default:
  4548. av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n", hx->nal_unit_type, bit_length);
  4549. }
  4550. if(context_count == h->max_contexts) {
  4551. execute_decode_slices(h, context_count);
  4552. context_count = 0;
  4553. }
  4554. if (err < 0)
  4555. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  4556. else if(err == 1) {
  4557. /* Slice could not be decoded in parallel mode, copy down
  4558. * NAL unit stuff to context 0 and restart. Note that
  4559. * rbsp_buffer is not transferred, but since we no longer
  4560. * run in parallel mode this should not be an issue. */
  4561. h->nal_unit_type = hx->nal_unit_type;
  4562. h->nal_ref_idc = hx->nal_ref_idc;
  4563. hx = h;
  4564. goto again;
  4565. }
  4566. }
  4567. if(context_count)
  4568. execute_decode_slices(h, context_count);
  4569. return buf_index;
  4570. }
  4571. /**
  4572. * returns the number of bytes consumed for building the current frame
  4573. */
  4574. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  4575. if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
  4576. if(pos+10>buf_size) pos=buf_size; // oops ;)
  4577. return pos;
  4578. }
  4579. static int decode_frame(AVCodecContext *avctx,
  4580. void *data, int *data_size,
  4581. AVPacket *avpkt)
  4582. {
  4583. const uint8_t *buf = avpkt->data;
  4584. int buf_size = avpkt->size;
  4585. H264Context *h = avctx->priv_data;
  4586. MpegEncContext *s = &h->s;
  4587. AVFrame *pict = data;
  4588. int buf_index;
  4589. s->flags= avctx->flags;
  4590. s->flags2= avctx->flags2;
  4591. /* end of stream, output what is still in the buffers */
  4592. if (buf_size == 0) {
  4593. Picture *out;
  4594. int i, out_idx;
  4595. //FIXME factorize this with the output code below
  4596. out = h->delayed_pic[0];
  4597. out_idx = 0;
  4598. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++)
  4599. if(h->delayed_pic[i]->poc < out->poc){
  4600. out = h->delayed_pic[i];
  4601. out_idx = i;
  4602. }
  4603. for(i=out_idx; h->delayed_pic[i]; i++)
  4604. h->delayed_pic[i] = h->delayed_pic[i+1];
  4605. if(out){
  4606. *data_size = sizeof(AVFrame);
  4607. *pict= *(AVFrame*)out;
  4608. }
  4609. return 0;
  4610. }
  4611. if(h->is_avc && !h->got_avcC) {
  4612. int i, cnt, nalsize;
  4613. unsigned char *p = avctx->extradata;
  4614. if(avctx->extradata_size < 7) {
  4615. av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
  4616. return -1;
  4617. }
  4618. if(*p != 1) {
  4619. av_log(avctx, AV_LOG_ERROR, "Unknown avcC version %d\n", *p);
  4620. return -1;
  4621. }
  4622. /* sps and pps in the avcC always have length coded with 2 bytes,
  4623. so put a fake nal_length_size = 2 while parsing them */
  4624. h->nal_length_size = 2;
  4625. // Decode sps from avcC
  4626. cnt = *(p+5) & 0x1f; // Number of sps
  4627. p += 6;
  4628. for (i = 0; i < cnt; i++) {
  4629. nalsize = AV_RB16(p) + 2;
  4630. if(decode_nal_units(h, p, nalsize) < 0) {
  4631. av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
  4632. return -1;
  4633. }
  4634. p += nalsize;
  4635. }
  4636. // Decode pps from avcC
  4637. cnt = *(p++); // Number of pps
  4638. for (i = 0; i < cnt; i++) {
  4639. nalsize = AV_RB16(p) + 2;
  4640. if(decode_nal_units(h, p, nalsize) != nalsize) {
  4641. av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
  4642. return -1;
  4643. }
  4644. p += nalsize;
  4645. }
  4646. // Now store right nal length size, that will be use to parse all other nals
  4647. h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
  4648. // Do not reparse avcC
  4649. h->got_avcC = 1;
  4650. }
  4651. if(!h->got_avcC && !h->is_avc && s->avctx->extradata_size){
  4652. if(decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) < 0)
  4653. return -1;
  4654. h->got_avcC = 1;
  4655. }
  4656. buf_index=decode_nal_units(h, buf, buf_size);
  4657. if(buf_index < 0)
  4658. return -1;
  4659. if(!(s->flags2 & CODEC_FLAG2_CHUNKS) && !s->current_picture_ptr){
  4660. if (avctx->skip_frame >= AVDISCARD_NONREF || s->hurry_up) return 0;
  4661. av_log(avctx, AV_LOG_ERROR, "no frame!\n");
  4662. return -1;
  4663. }
  4664. if(!(s->flags2 & CODEC_FLAG2_CHUNKS) || (s->mb_y >= s->mb_height && s->mb_height)){
  4665. Picture *out = s->current_picture_ptr;
  4666. Picture *cur = s->current_picture_ptr;
  4667. int i, pics, out_of_order, out_idx;
  4668. field_end(h);
  4669. if (cur->field_poc[0]==INT_MAX || cur->field_poc[1]==INT_MAX) {
  4670. /* Wait for second field. */
  4671. *data_size = 0;
  4672. } else {
  4673. cur->interlaced_frame = 0;
  4674. cur->repeat_pict = 0;
  4675. /* Signal interlacing information externally. */
  4676. /* Prioritize picture timing SEI information over used decoding process if it exists. */
  4677. if(h->sps.pic_struct_present_flag){
  4678. switch (h->sei_pic_struct)
  4679. {
  4680. case SEI_PIC_STRUCT_FRAME:
  4681. break;
  4682. case SEI_PIC_STRUCT_TOP_FIELD:
  4683. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  4684. cur->interlaced_frame = 1;
  4685. break;
  4686. case SEI_PIC_STRUCT_TOP_BOTTOM:
  4687. case SEI_PIC_STRUCT_BOTTOM_TOP:
  4688. if (FIELD_OR_MBAFF_PICTURE)
  4689. cur->interlaced_frame = 1;
  4690. else
  4691. // try to flag soft telecine progressive
  4692. cur->interlaced_frame = h->prev_interlaced_frame;
  4693. break;
  4694. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  4695. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  4696. // Signal the possibility of telecined film externally (pic_struct 5,6)
  4697. // From these hints, let the applications decide if they apply deinterlacing.
  4698. cur->repeat_pict = 1;
  4699. break;
  4700. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  4701. // Force progressive here, as doubling interlaced frame is a bad idea.
  4702. cur->repeat_pict = 2;
  4703. break;
  4704. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  4705. cur->repeat_pict = 4;
  4706. break;
  4707. }
  4708. if ((h->sei_ct_type & 3) && h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP)
  4709. cur->interlaced_frame = (h->sei_ct_type & (1<<1)) != 0;
  4710. }else{
  4711. /* Derive interlacing flag from used decoding process. */
  4712. cur->interlaced_frame = FIELD_OR_MBAFF_PICTURE;
  4713. }
  4714. h->prev_interlaced_frame = cur->interlaced_frame;
  4715. if (cur->field_poc[0] != cur->field_poc[1]){
  4716. /* Derive top_field_first from field pocs. */
  4717. cur->top_field_first = cur->field_poc[0] < cur->field_poc[1];
  4718. }else{
  4719. if(cur->interlaced_frame || h->sps.pic_struct_present_flag){
  4720. /* Use picture timing SEI information. Even if it is a information of a past frame, better than nothing. */
  4721. if(h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM
  4722. || h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
  4723. cur->top_field_first = 1;
  4724. else
  4725. cur->top_field_first = 0;
  4726. }else{
  4727. /* Most likely progressive */
  4728. cur->top_field_first = 0;
  4729. }
  4730. }
  4731. //FIXME do something with unavailable reference frames
  4732. /* Sort B-frames into display order */
  4733. if(h->sps.bitstream_restriction_flag
  4734. && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  4735. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  4736. s->low_delay = 0;
  4737. }
  4738. if( s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT
  4739. && !h->sps.bitstream_restriction_flag){
  4740. s->avctx->has_b_frames= MAX_DELAYED_PIC_COUNT;
  4741. s->low_delay= 0;
  4742. }
  4743. pics = 0;
  4744. while(h->delayed_pic[pics]) pics++;
  4745. assert(pics <= MAX_DELAYED_PIC_COUNT);
  4746. h->delayed_pic[pics++] = cur;
  4747. if(cur->reference == 0)
  4748. cur->reference = DELAYED_PIC_REF;
  4749. out = h->delayed_pic[0];
  4750. out_idx = 0;
  4751. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++)
  4752. if(h->delayed_pic[i]->poc < out->poc){
  4753. out = h->delayed_pic[i];
  4754. out_idx = i;
  4755. }
  4756. if(s->avctx->has_b_frames == 0 && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset))
  4757. h->outputed_poc= INT_MIN;
  4758. out_of_order = out->poc < h->outputed_poc;
  4759. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
  4760. { }
  4761. else if((out_of_order && pics-1 == s->avctx->has_b_frames && s->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT)
  4762. || (s->low_delay &&
  4763. ((h->outputed_poc != INT_MIN && out->poc > h->outputed_poc + 2)
  4764. || cur->pict_type == FF_B_TYPE)))
  4765. {
  4766. s->low_delay = 0;
  4767. s->avctx->has_b_frames++;
  4768. }
  4769. if(out_of_order || pics > s->avctx->has_b_frames){
  4770. out->reference &= ~DELAYED_PIC_REF;
  4771. for(i=out_idx; h->delayed_pic[i]; i++)
  4772. h->delayed_pic[i] = h->delayed_pic[i+1];
  4773. }
  4774. if(!out_of_order && pics > s->avctx->has_b_frames){
  4775. *data_size = sizeof(AVFrame);
  4776. if(out_idx==0 && h->delayed_pic[0] && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset)) {
  4777. h->outputed_poc = INT_MIN;
  4778. } else
  4779. h->outputed_poc = out->poc;
  4780. *pict= *(AVFrame*)out;
  4781. }else{
  4782. av_log(avctx, AV_LOG_DEBUG, "no picture\n");
  4783. }
  4784. }
  4785. }
  4786. assert(pict->data[0] || !*data_size);
  4787. ff_print_debug_info(s, pict);
  4788. //printf("out %d\n", (int)pict->data[0]);
  4789. return get_consumed_bytes(s, buf_index, buf_size);
  4790. }
  4791. #if 0
  4792. static inline void fill_mb_avail(H264Context *h){
  4793. MpegEncContext * const s = &h->s;
  4794. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  4795. if(s->mb_y){
  4796. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  4797. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  4798. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  4799. }else{
  4800. h->mb_avail[0]=
  4801. h->mb_avail[1]=
  4802. h->mb_avail[2]= 0;
  4803. }
  4804. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  4805. h->mb_avail[4]= 1; //FIXME move out
  4806. h->mb_avail[5]= 0; //FIXME move out
  4807. }
  4808. #endif
  4809. #ifdef TEST
  4810. #undef printf
  4811. #undef random
  4812. #define COUNT 8000
  4813. #define SIZE (COUNT*40)
  4814. int main(void){
  4815. int i;
  4816. uint8_t temp[SIZE];
  4817. PutBitContext pb;
  4818. GetBitContext gb;
  4819. // int int_temp[10000];
  4820. DSPContext dsp;
  4821. AVCodecContext avctx;
  4822. dsputil_init(&dsp, &avctx);
  4823. init_put_bits(&pb, temp, SIZE);
  4824. printf("testing unsigned exp golomb\n");
  4825. for(i=0; i<COUNT; i++){
  4826. START_TIMER
  4827. set_ue_golomb(&pb, i);
  4828. STOP_TIMER("set_ue_golomb");
  4829. }
  4830. flush_put_bits(&pb);
  4831. init_get_bits(&gb, temp, 8*SIZE);
  4832. for(i=0; i<COUNT; i++){
  4833. int j, s;
  4834. s= show_bits(&gb, 24);
  4835. START_TIMER
  4836. j= get_ue_golomb(&gb);
  4837. if(j != i){
  4838. printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  4839. // return -1;
  4840. }
  4841. STOP_TIMER("get_ue_golomb");
  4842. }
  4843. init_put_bits(&pb, temp, SIZE);
  4844. printf("testing signed exp golomb\n");
  4845. for(i=0; i<COUNT; i++){
  4846. START_TIMER
  4847. set_se_golomb(&pb, i - COUNT/2);
  4848. STOP_TIMER("set_se_golomb");
  4849. }
  4850. flush_put_bits(&pb);
  4851. init_get_bits(&gb, temp, 8*SIZE);
  4852. for(i=0; i<COUNT; i++){
  4853. int j, s;
  4854. s= show_bits(&gb, 24);
  4855. START_TIMER
  4856. j= get_se_golomb(&gb);
  4857. if(j != i - COUNT/2){
  4858. printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  4859. // return -1;
  4860. }
  4861. STOP_TIMER("get_se_golomb");
  4862. }
  4863. #if 0
  4864. printf("testing 4x4 (I)DCT\n");
  4865. DCTELEM block[16];
  4866. uint8_t src[16], ref[16];
  4867. uint64_t error= 0, max_error=0;
  4868. for(i=0; i<COUNT; i++){
  4869. int j;
  4870. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  4871. for(j=0; j<16; j++){
  4872. ref[j]= random()%255;
  4873. src[j]= random()%255;
  4874. }
  4875. h264_diff_dct_c(block, src, ref, 4);
  4876. //normalize
  4877. for(j=0; j<16; j++){
  4878. // printf("%d ", block[j]);
  4879. block[j]= block[j]*4;
  4880. if(j&1) block[j]= (block[j]*4 + 2)/5;
  4881. if(j&4) block[j]= (block[j]*4 + 2)/5;
  4882. }
  4883. // printf("\n");
  4884. s->dsp.h264_idct_add(ref, block, 4);
  4885. /* for(j=0; j<16; j++){
  4886. printf("%d ", ref[j]);
  4887. }
  4888. printf("\n");*/
  4889. for(j=0; j<16; j++){
  4890. int diff= FFABS(src[j] - ref[j]);
  4891. error+= diff*diff;
  4892. max_error= FFMAX(max_error, diff);
  4893. }
  4894. }
  4895. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  4896. printf("testing quantizer\n");
  4897. for(qp=0; qp<52; qp++){
  4898. for(i=0; i<16; i++)
  4899. src1_block[i]= src2_block[i]= random()%255;
  4900. }
  4901. printf("Testing NAL layer\n");
  4902. uint8_t bitstream[COUNT];
  4903. uint8_t nal[COUNT*2];
  4904. H264Context h;
  4905. memset(&h, 0, sizeof(H264Context));
  4906. for(i=0; i<COUNT; i++){
  4907. int zeros= i;
  4908. int nal_length;
  4909. int consumed;
  4910. int out_length;
  4911. uint8_t *out;
  4912. int j;
  4913. for(j=0; j<COUNT; j++){
  4914. bitstream[j]= (random() % 255) + 1;
  4915. }
  4916. for(j=0; j<zeros; j++){
  4917. int pos= random() % COUNT;
  4918. while(bitstream[pos] == 0){
  4919. pos++;
  4920. pos %= COUNT;
  4921. }
  4922. bitstream[pos]=0;
  4923. }
  4924. START_TIMER
  4925. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  4926. if(nal_length<0){
  4927. printf("encoding failed\n");
  4928. return -1;
  4929. }
  4930. out= ff_h264_decode_nal(&h, nal, &out_length, &consumed, nal_length);
  4931. STOP_TIMER("NAL")
  4932. if(out_length != COUNT){
  4933. printf("incorrect length %d %d\n", out_length, COUNT);
  4934. return -1;
  4935. }
  4936. if(consumed != nal_length){
  4937. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  4938. return -1;
  4939. }
  4940. if(memcmp(bitstream, out, COUNT)){
  4941. printf("mismatch\n");
  4942. return -1;
  4943. }
  4944. }
  4945. #endif
  4946. printf("Testing RBSP\n");
  4947. return 0;
  4948. }
  4949. #endif /* TEST */
  4950. av_cold void ff_h264_free_context(H264Context *h)
  4951. {
  4952. int i;
  4953. free_tables(h); //FIXME cleanup init stuff perhaps
  4954. for(i = 0; i < MAX_SPS_COUNT; i++)
  4955. av_freep(h->sps_buffers + i);
  4956. for(i = 0; i < MAX_PPS_COUNT; i++)
  4957. av_freep(h->pps_buffers + i);
  4958. }
  4959. av_cold int ff_h264_decode_end(AVCodecContext *avctx)
  4960. {
  4961. H264Context *h = avctx->priv_data;
  4962. MpegEncContext *s = &h->s;
  4963. ff_h264_free_context(h);
  4964. MPV_common_end(s);
  4965. // memset(h, 0, sizeof(H264Context));
  4966. return 0;
  4967. }
  4968. AVCodec h264_decoder = {
  4969. "h264",
  4970. CODEC_TYPE_VIDEO,
  4971. CODEC_ID_H264,
  4972. sizeof(H264Context),
  4973. ff_h264_decode_init,
  4974. NULL,
  4975. ff_h264_decode_end,
  4976. decode_frame,
  4977. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_DELAY,
  4978. .flush= flush_dpb,
  4979. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
  4980. .pix_fmts= ff_hwaccel_pixfmt_list_420,
  4981. };
  4982. #if CONFIG_H264_VDPAU_DECODER
  4983. AVCodec h264_vdpau_decoder = {
  4984. "h264_vdpau",
  4985. CODEC_TYPE_VIDEO,
  4986. CODEC_ID_H264,
  4987. sizeof(H264Context),
  4988. ff_h264_decode_init,
  4989. NULL,
  4990. ff_h264_decode_end,
  4991. decode_frame,
  4992. CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
  4993. .flush= flush_dpb,
  4994. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),
  4995. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_VDPAU_H264, PIX_FMT_NONE},
  4996. };
  4997. #endif