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.

3489 lines
126KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... 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
  23. * H.264 / AVC / MPEG4 part10 codec.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include "libavutil/imgutils.h"
  27. #include "internal.h"
  28. #include "dsputil.h"
  29. #include "avcodec.h"
  30. #include "mpegvideo.h"
  31. #include "h264.h"
  32. #include "h264data.h"
  33. #include "h264_mvpred.h"
  34. #include "golomb.h"
  35. #include "mathops.h"
  36. #include "rectangle.h"
  37. #include "thread.h"
  38. #include "vdpau_internal.h"
  39. #include "libavutil/avassert.h"
  40. #include "cabac.h"
  41. //#undef NDEBUG
  42. #include <assert.h>
  43. static const uint8_t rem6[QP_MAX_MAX+1]={
  44. 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, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
  45. };
  46. static const uint8_t div6[QP_MAX_MAX+1]={
  47. 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, 8, 8, 9, 9, 9, 9, 9, 9,10,10,10,10,
  48. };
  49. static const enum PixelFormat hwaccel_pixfmt_list_h264_jpeg_420[] = {
  50. PIX_FMT_DXVA2_VLD,
  51. PIX_FMT_VAAPI_VLD,
  52. PIX_FMT_YUVJ420P,
  53. PIX_FMT_NONE
  54. };
  55. void ff_h264_write_back_intra_pred_mode(H264Context *h){
  56. int8_t *mode= h->intra4x4_pred_mode + h->mb2br_xy[h->mb_xy];
  57. AV_COPY32(mode, h->intra4x4_pred_mode_cache + 4 + 8*4);
  58. mode[4]= h->intra4x4_pred_mode_cache[7+8*3];
  59. mode[5]= h->intra4x4_pred_mode_cache[7+8*2];
  60. mode[6]= h->intra4x4_pred_mode_cache[7+8*1];
  61. }
  62. /**
  63. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  64. */
  65. int ff_h264_check_intra4x4_pred_mode(H264Context *h){
  66. MpegEncContext * const s = &h->s;
  67. static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
  68. static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
  69. int i;
  70. if(!(h->top_samples_available&0x8000)){
  71. for(i=0; i<4; i++){
  72. int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
  73. if(status<0){
  74. 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);
  75. return -1;
  76. } else if(status){
  77. h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
  78. }
  79. }
  80. }
  81. if((h->left_samples_available&0x8888)!=0x8888){
  82. static const int mask[4]={0x8000,0x2000,0x80,0x20};
  83. for(i=0; i<4; i++){
  84. if(!(h->left_samples_available&mask[i])){
  85. int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
  86. if(status<0){
  87. 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);
  88. return -1;
  89. } else if(status){
  90. h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
  91. }
  92. }
  93. }
  94. }
  95. return 0;
  96. } //FIXME cleanup like ff_h264_check_intra_pred_mode
  97. /**
  98. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  99. */
  100. int ff_h264_check_intra_pred_mode(H264Context *h, int mode){
  101. MpegEncContext * const s = &h->s;
  102. static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
  103. static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
  104. if(mode > 6U) {
  105. 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);
  106. return -1;
  107. }
  108. if(!(h->top_samples_available&0x8000)){
  109. mode= top[ mode ];
  110. if(mode<0){
  111. 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);
  112. return -1;
  113. }
  114. }
  115. if((h->left_samples_available&0x8080) != 0x8080){
  116. mode= left[ mode ];
  117. if(h->left_samples_available&0x8080){ //mad cow disease mode, aka MBAFF + constrained_intra_pred
  118. mode= ALZHEIMER_DC_L0T_PRED8x8 + (!(h->left_samples_available&0x8000)) + 2*(mode == DC_128_PRED8x8);
  119. }
  120. if(mode<0){
  121. 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);
  122. return -1;
  123. }
  124. }
  125. return mode;
  126. }
  127. const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src, int *dst_length, int *consumed, int length){
  128. int i, si, di;
  129. uint8_t *dst;
  130. int bufidx;
  131. // src[0]&0x80; //forbidden bit
  132. h->nal_ref_idc= src[0]>>5;
  133. h->nal_unit_type= src[0]&0x1F;
  134. src++; length--;
  135. #if 0
  136. for(i=0; i<length; i++)
  137. printf("%2X ", src[i]);
  138. #endif
  139. #if HAVE_FAST_UNALIGNED
  140. # if HAVE_FAST_64BIT
  141. # define RS 7
  142. for(i=0; i+1<length; i+=9){
  143. if(!((~AV_RN64A(src+i) & (AV_RN64A(src+i) - 0x0100010001000101ULL)) & 0x8000800080008080ULL))
  144. # else
  145. # define RS 3
  146. for(i=0; i+1<length; i+=5){
  147. if(!((~AV_RN32A(src+i) & (AV_RN32A(src+i) - 0x01000101U)) & 0x80008080U))
  148. # endif
  149. continue;
  150. if(i>0 && !src[i]) i--;
  151. while(src[i]) i++;
  152. #else
  153. # define RS 0
  154. for(i=0; i+1<length; i+=2){
  155. if(src[i]) continue;
  156. if(i>0 && src[i-1]==0) i--;
  157. #endif
  158. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  159. if(src[i+2]!=3){
  160. /* startcode, so we must be past the end */
  161. length=i;
  162. }
  163. break;
  164. }
  165. i-= RS;
  166. }
  167. if(i>=length-1){ //no escaped 0
  168. *dst_length= length;
  169. *consumed= length+1; //+1 for the header
  170. return src;
  171. }
  172. bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0; // use second escape buffer for inter data
  173. av_fast_malloc(&h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx], length+FF_INPUT_BUFFER_PADDING_SIZE);
  174. dst= h->rbsp_buffer[bufidx];
  175. if (dst == NULL){
  176. return NULL;
  177. }
  178. //printf("decoding esc\n");
  179. memcpy(dst, src, i);
  180. si=di=i;
  181. while(si+2<length){
  182. //remove escapes (very rare 1:2^22)
  183. if(src[si+2]>3){
  184. dst[di++]= src[si++];
  185. dst[di++]= src[si++];
  186. }else if(src[si]==0 && src[si+1]==0){
  187. if(src[si+2]==3){ //escape
  188. dst[di++]= 0;
  189. dst[di++]= 0;
  190. si+=3;
  191. continue;
  192. }else //next start code
  193. goto nsc;
  194. }
  195. dst[di++]= src[si++];
  196. }
  197. while(si<length)
  198. dst[di++]= src[si++];
  199. nsc:
  200. memset(dst+di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  201. *dst_length= di;
  202. *consumed= si + 1;//+1 for the header
  203. //FIXME store exact number of bits in the getbitcontext (it is needed for decoding)
  204. return dst;
  205. }
  206. /**
  207. * Identify the exact end of the bitstream
  208. * @return the length of the trailing, or 0 if damaged
  209. */
  210. static int ff_h264_decode_rbsp_trailing(H264Context *h, const uint8_t *src){
  211. int v= *src;
  212. int r;
  213. tprintf(h->s.avctx, "rbsp trailing %X\n", v);
  214. for(r=1; r<9; r++){
  215. if(v&1) return r;
  216. v>>=1;
  217. }
  218. return 0;
  219. }
  220. #if 0
  221. /**
  222. * DCT transforms the 16 dc values.
  223. * @param qp quantization parameter ??? FIXME
  224. */
  225. static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
  226. // const int qmul= dequant_coeff[qp][0];
  227. int i;
  228. int temp[16]; //FIXME check if this is a good idea
  229. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  230. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  231. for(i=0; i<4; i++){
  232. const int offset= y_offset[i];
  233. const int z0= block[offset+stride*0] + block[offset+stride*4];
  234. const int z1= block[offset+stride*0] - block[offset+stride*4];
  235. const int z2= block[offset+stride*1] - block[offset+stride*5];
  236. const int z3= block[offset+stride*1] + block[offset+stride*5];
  237. temp[4*i+0]= z0+z3;
  238. temp[4*i+1]= z1+z2;
  239. temp[4*i+2]= z1-z2;
  240. temp[4*i+3]= z0-z3;
  241. }
  242. for(i=0; i<4; i++){
  243. const int offset= x_offset[i];
  244. const int z0= temp[4*0+i] + temp[4*2+i];
  245. const int z1= temp[4*0+i] - temp[4*2+i];
  246. const int z2= temp[4*1+i] - temp[4*3+i];
  247. const int z3= temp[4*1+i] + temp[4*3+i];
  248. block[stride*0 +offset]= (z0 + z3)>>1;
  249. block[stride*2 +offset]= (z1 + z2)>>1;
  250. block[stride*8 +offset]= (z1 - z2)>>1;
  251. block[stride*10+offset]= (z0 - z3)>>1;
  252. }
  253. }
  254. #endif
  255. #undef xStride
  256. #undef stride
  257. #if 0
  258. static void chroma_dc_dct_c(DCTELEM *block){
  259. const int stride= 16*2;
  260. const int xStride= 16;
  261. int a,b,c,d,e;
  262. a= block[stride*0 + xStride*0];
  263. b= block[stride*0 + xStride*1];
  264. c= block[stride*1 + xStride*0];
  265. d= block[stride*1 + xStride*1];
  266. e= a-b;
  267. a= a+b;
  268. b= c-d;
  269. c= c+d;
  270. block[stride*0 + xStride*0]= (a+c);
  271. block[stride*0 + xStride*1]= (e+b);
  272. block[stride*1 + xStride*0]= (a-c);
  273. block[stride*1 + xStride*1]= (e-b);
  274. }
  275. #endif
  276. static void free_tables(H264Context *h, int free_rbsp){
  277. int i;
  278. H264Context *hx;
  279. av_freep(&h->intra4x4_pred_mode);
  280. av_freep(&h->chroma_pred_mode_table);
  281. av_freep(&h->cbp_table);
  282. av_freep(&h->mvd_table[0]);
  283. av_freep(&h->mvd_table[1]);
  284. av_freep(&h->direct_table);
  285. av_freep(&h->non_zero_count);
  286. av_freep(&h->slice_table_base);
  287. h->slice_table= NULL;
  288. av_freep(&h->list_counts);
  289. av_freep(&h->mb2b_xy);
  290. av_freep(&h->mb2br_xy);
  291. for(i = 0; i < MAX_THREADS; i++) {
  292. hx = h->thread_context[i];
  293. if(!hx) continue;
  294. av_freep(&hx->top_borders[1]);
  295. av_freep(&hx->top_borders[0]);
  296. av_freep(&hx->s.obmc_scratchpad);
  297. if (free_rbsp){
  298. av_freep(&hx->rbsp_buffer[1]);
  299. av_freep(&hx->rbsp_buffer[0]);
  300. hx->rbsp_buffer_size[0] = 0;
  301. hx->rbsp_buffer_size[1] = 0;
  302. }
  303. if (i) av_freep(&h->thread_context[i]);
  304. }
  305. }
  306. static void init_dequant8_coeff_table(H264Context *h){
  307. int i,q,x;
  308. const int max_qp = 51 + 6*(h->sps.bit_depth_luma-8);
  309. h->dequant8_coeff[0] = h->dequant8_buffer[0];
  310. h->dequant8_coeff[1] = h->dequant8_buffer[1];
  311. for(i=0; i<2; i++ ){
  312. if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
  313. h->dequant8_coeff[1] = h->dequant8_buffer[0];
  314. break;
  315. }
  316. for(q=0; q<max_qp+1; q++){
  317. int shift = div6[q];
  318. int idx = rem6[q];
  319. for(x=0; x<64; x++)
  320. h->dequant8_coeff[i][q][(x>>3)|((x&7)<<3)] =
  321. ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
  322. h->pps.scaling_matrix8[i][x]) << shift;
  323. }
  324. }
  325. }
  326. static void init_dequant4_coeff_table(H264Context *h){
  327. int i,j,q,x;
  328. const int max_qp = 51 + 6*(h->sps.bit_depth_luma-8);
  329. for(i=0; i<6; i++ ){
  330. h->dequant4_coeff[i] = h->dequant4_buffer[i];
  331. for(j=0; j<i; j++){
  332. if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
  333. h->dequant4_coeff[i] = h->dequant4_buffer[j];
  334. break;
  335. }
  336. }
  337. if(j<i)
  338. continue;
  339. for(q=0; q<max_qp+1; q++){
  340. int shift = div6[q] + 2;
  341. int idx = rem6[q];
  342. for(x=0; x<16; x++)
  343. h->dequant4_coeff[i][q][(x>>2)|((x<<2)&0xF)] =
  344. ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
  345. h->pps.scaling_matrix4[i][x]) << shift;
  346. }
  347. }
  348. }
  349. static void init_dequant_tables(H264Context *h){
  350. int i,x;
  351. init_dequant4_coeff_table(h);
  352. if(h->pps.transform_8x8_mode)
  353. init_dequant8_coeff_table(h);
  354. if(h->sps.transform_bypass){
  355. for(i=0; i<6; i++)
  356. for(x=0; x<16; x++)
  357. h->dequant4_coeff[i][0][x] = 1<<6;
  358. if(h->pps.transform_8x8_mode)
  359. for(i=0; i<2; i++)
  360. for(x=0; x<64; x++)
  361. h->dequant8_coeff[i][0][x] = 1<<6;
  362. }
  363. }
  364. int ff_h264_alloc_tables(H264Context *h){
  365. MpegEncContext * const s = &h->s;
  366. const int big_mb_num= s->mb_stride * (s->mb_height+1);
  367. const int row_mb_num= 2*s->mb_stride*s->avctx->thread_count;
  368. int x,y;
  369. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->intra4x4_pred_mode, row_mb_num * 8 * sizeof(uint8_t), fail)
  370. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->non_zero_count , big_mb_num * 32 * sizeof(uint8_t), fail)
  371. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->slice_table_base , (big_mb_num+s->mb_stride) * sizeof(*h->slice_table_base), fail)
  372. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->cbp_table, big_mb_num * sizeof(uint16_t), fail)
  373. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t), fail)
  374. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[0], 16*row_mb_num * sizeof(uint8_t), fail);
  375. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[1], 16*row_mb_num * sizeof(uint8_t), fail);
  376. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->direct_table, 4*big_mb_num * sizeof(uint8_t) , fail);
  377. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->list_counts, big_mb_num * sizeof(uint8_t), fail)
  378. memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride) * sizeof(*h->slice_table_base));
  379. h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
  380. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2b_xy , big_mb_num * sizeof(uint32_t), fail);
  381. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2br_xy , big_mb_num * sizeof(uint32_t), fail);
  382. for(y=0; y<s->mb_height; y++){
  383. for(x=0; x<s->mb_width; x++){
  384. const int mb_xy= x + y*s->mb_stride;
  385. const int b_xy = 4*x + 4*y*h->b_stride;
  386. h->mb2b_xy [mb_xy]= b_xy;
  387. h->mb2br_xy[mb_xy]= 8*(FMO ? mb_xy : (mb_xy % (2*s->mb_stride)));
  388. }
  389. }
  390. s->obmc_scratchpad = NULL;
  391. if(!h->dequant4_coeff[0])
  392. init_dequant_tables(h);
  393. return 0;
  394. fail:
  395. free_tables(h, 1);
  396. return -1;
  397. }
  398. /**
  399. * Mimic alloc_tables(), but for every context thread.
  400. */
  401. static void clone_tables(H264Context *dst, H264Context *src, int i){
  402. MpegEncContext * const s = &src->s;
  403. dst->intra4x4_pred_mode = src->intra4x4_pred_mode + i*8*2*s->mb_stride;
  404. dst->non_zero_count = src->non_zero_count;
  405. dst->slice_table = src->slice_table;
  406. dst->cbp_table = src->cbp_table;
  407. dst->mb2b_xy = src->mb2b_xy;
  408. dst->mb2br_xy = src->mb2br_xy;
  409. dst->chroma_pred_mode_table = src->chroma_pred_mode_table;
  410. dst->mvd_table[0] = src->mvd_table[0] + i*8*2*s->mb_stride;
  411. dst->mvd_table[1] = src->mvd_table[1] + i*8*2*s->mb_stride;
  412. dst->direct_table = src->direct_table;
  413. dst->list_counts = src->list_counts;
  414. dst->s.obmc_scratchpad = NULL;
  415. ff_h264_pred_init(&dst->hpc, src->s.codec_id, src->sps.bit_depth_luma);
  416. }
  417. /**
  418. * Init context
  419. * Allocate buffers which are not shared amongst multiple threads.
  420. */
  421. static int context_init(H264Context *h){
  422. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[0], h->s.mb_width * (16+8+8) * sizeof(uint8_t)*2, fail)
  423. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[1], h->s.mb_width * (16+8+8) * sizeof(uint8_t)*2, fail)
  424. h->ref_cache[0][scan8[5 ]+1] = h->ref_cache[0][scan8[7 ]+1] = h->ref_cache[0][scan8[13]+1] =
  425. h->ref_cache[1][scan8[5 ]+1] = h->ref_cache[1][scan8[7 ]+1] = h->ref_cache[1][scan8[13]+1] = PART_NOT_AVAILABLE;
  426. return 0;
  427. fail:
  428. return -1; // free_tables will clean up for us
  429. }
  430. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size);
  431. static av_cold void common_init(H264Context *h){
  432. MpegEncContext * const s = &h->s;
  433. s->width = s->avctx->width;
  434. s->height = s->avctx->height;
  435. s->codec_id= s->avctx->codec->id;
  436. ff_h264dsp_init(&h->h264dsp, 8);
  437. ff_h264_pred_init(&h->hpc, s->codec_id, 8);
  438. h->dequant_coeff_pps= -1;
  439. s->unrestricted_mv=1;
  440. s->decode=1; //FIXME
  441. dsputil_init(&s->dsp, s->avctx); // needed so that idct permutation is known early
  442. memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  443. memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  444. }
  445. int ff_h264_decode_extradata(H264Context *h)
  446. {
  447. AVCodecContext *avctx = h->s.avctx;
  448. if(*(char *)avctx->extradata == 1){
  449. int i, cnt, nalsize;
  450. unsigned char *p = avctx->extradata;
  451. h->is_avc = 1;
  452. if(avctx->extradata_size < 7) {
  453. av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
  454. return -1;
  455. }
  456. /* sps and pps in the avcC always have length coded with 2 bytes,
  457. so put a fake nal_length_size = 2 while parsing them */
  458. h->nal_length_size = 2;
  459. // Decode sps from avcC
  460. cnt = *(p+5) & 0x1f; // Number of sps
  461. p += 6;
  462. for (i = 0; i < cnt; i++) {
  463. nalsize = AV_RB16(p) + 2;
  464. if(decode_nal_units(h, p, nalsize) < 0) {
  465. av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
  466. return -1;
  467. }
  468. p += nalsize;
  469. }
  470. // Decode pps from avcC
  471. cnt = *(p++); // Number of pps
  472. for (i = 0; i < cnt; i++) {
  473. nalsize = AV_RB16(p) + 2;
  474. if(decode_nal_units(h, p, nalsize) < 0) {
  475. av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
  476. return -1;
  477. }
  478. p += nalsize;
  479. }
  480. // Now store right nal length size, that will be use to parse all other nals
  481. h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
  482. } else {
  483. h->is_avc = 0;
  484. if(decode_nal_units(h, avctx->extradata, avctx->extradata_size) < 0)
  485. return -1;
  486. }
  487. return 0;
  488. }
  489. av_cold int ff_h264_decode_init(AVCodecContext *avctx){
  490. H264Context *h= avctx->priv_data;
  491. MpegEncContext * const s = &h->s;
  492. MPV_decode_defaults(s);
  493. s->avctx = avctx;
  494. common_init(h);
  495. s->out_format = FMT_H264;
  496. s->workaround_bugs= avctx->workaround_bugs;
  497. // set defaults
  498. // s->decode_mb= ff_h263_decode_mb;
  499. s->quarter_sample = 1;
  500. if(!avctx->has_b_frames)
  501. s->low_delay= 1;
  502. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  503. ff_h264_decode_init_vlc();
  504. h->sps.bit_depth_luma = avctx->bits_per_raw_sample = 8;
  505. h->pixel_shift = 0;
  506. h->thread_context[0] = h;
  507. h->outputed_poc = h->next_outputed_poc = INT_MIN;
  508. h->prev_poc_msb= 1<<16;
  509. h->x264_build = -1;
  510. ff_h264_reset_sei(h);
  511. if(avctx->codec_id == CODEC_ID_H264){
  512. if(avctx->ticks_per_frame == 1){
  513. s->avctx->time_base.den *=2;
  514. }
  515. avctx->ticks_per_frame = 2;
  516. }
  517. if(avctx->extradata_size > 0 && avctx->extradata &&
  518. ff_h264_decode_extradata(h))
  519. return -1;
  520. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  521. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  522. s->low_delay = 0;
  523. }
  524. return 0;
  525. }
  526. static void copy_picture_range(Picture **to, Picture **from, int count, MpegEncContext *new_base, MpegEncContext *old_base)
  527. {
  528. int i;
  529. for (i=0; i<count; i++){
  530. to[i] = REBASE_PICTURE(from[i], new_base, old_base);
  531. }
  532. }
  533. static void copy_parameter_set(void **to, void **from, int count, int size)
  534. {
  535. int i;
  536. for (i=0; i<count; i++){
  537. if (to[i] && !from[i]) av_freep(&to[i]);
  538. else if (from[i] && !to[i]) to[i] = av_malloc(size);
  539. if (from[i]) memcpy(to[i], from[i], size);
  540. }
  541. }
  542. static int decode_init_thread_copy(AVCodecContext *avctx){
  543. H264Context *h= avctx->priv_data;
  544. if (!avctx->is_copy) return 0;
  545. memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
  546. memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
  547. return 0;
  548. }
  549. #define copy_fields(to, from, start_field, end_field) memcpy(&to->start_field, &from->start_field, (char*)&to->end_field - (char*)&to->start_field)
  550. static int decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src){
  551. H264Context *h= dst->priv_data, *h1= src->priv_data;
  552. MpegEncContext * const s = &h->s, * const s1 = &h1->s;
  553. int inited = s->context_initialized, err;
  554. int i;
  555. if(dst == src || !s1->context_initialized) return 0;
  556. err = ff_mpeg_update_thread_context(dst, src);
  557. if(err) return err;
  558. //FIXME handle width/height changing
  559. if(!inited){
  560. for(i = 0; i < MAX_SPS_COUNT; i++)
  561. av_freep(h->sps_buffers + i);
  562. for(i = 0; i < MAX_PPS_COUNT; i++)
  563. av_freep(h->pps_buffers + i);
  564. memcpy(&h->s + 1, &h1->s + 1, sizeof(H264Context) - sizeof(MpegEncContext)); //copy all fields after MpegEnc
  565. memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
  566. memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
  567. ff_h264_alloc_tables(h);
  568. context_init(h);
  569. for(i=0; i<2; i++){
  570. h->rbsp_buffer[i] = NULL;
  571. h->rbsp_buffer_size[i] = 0;
  572. }
  573. h->thread_context[0] = h;
  574. // frame_start may not be called for the next thread (if it's decoding a bottom field)
  575. // so this has to be allocated here
  576. h->s.obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
  577. s->dsp.clear_blocks(h->mb);
  578. }
  579. //extradata/NAL handling
  580. h->is_avc = h1->is_avc;
  581. //SPS/PPS
  582. copy_parameter_set((void**)h->sps_buffers, (void**)h1->sps_buffers, MAX_SPS_COUNT, sizeof(SPS));
  583. h->sps = h1->sps;
  584. copy_parameter_set((void**)h->pps_buffers, (void**)h1->pps_buffers, MAX_PPS_COUNT, sizeof(PPS));
  585. h->pps = h1->pps;
  586. //Dequantization matrices
  587. //FIXME these are big - can they be only copied when PPS changes?
  588. copy_fields(h, h1, dequant4_buffer, dequant4_coeff);
  589. for(i=0; i<6; i++)
  590. h->dequant4_coeff[i] = h->dequant4_buffer[0] + (h1->dequant4_coeff[i] - h1->dequant4_buffer[0]);
  591. for(i=0; i<2; i++)
  592. h->dequant8_coeff[i] = h->dequant8_buffer[0] + (h1->dequant8_coeff[i] - h1->dequant8_buffer[0]);
  593. h->dequant_coeff_pps = h1->dequant_coeff_pps;
  594. //POC timing
  595. copy_fields(h, h1, poc_lsb, redundant_pic_count);
  596. //reference lists
  597. copy_fields(h, h1, ref_count, intra_gb);
  598. copy_fields(h, h1, short_ref, cabac_init_idc);
  599. copy_picture_range(h->short_ref, h1->short_ref, 32, s, s1);
  600. copy_picture_range(h->long_ref, h1->long_ref, 32, s, s1);
  601. copy_picture_range(h->delayed_pic, h1->delayed_pic, MAX_DELAYED_PIC_COUNT+2, s, s1);
  602. h->last_slice_type = h1->last_slice_type;
  603. if(!s->current_picture_ptr) return 0;
  604. if(!s->dropable) {
  605. ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  606. h->prev_poc_msb = h->poc_msb;
  607. h->prev_poc_lsb = h->poc_lsb;
  608. }
  609. h->prev_frame_num_offset= h->frame_num_offset;
  610. h->prev_frame_num = h->frame_num;
  611. h->outputed_poc = h->next_outputed_poc;
  612. return 0;
  613. }
  614. int ff_h264_frame_start(H264Context *h){
  615. MpegEncContext * const s = &h->s;
  616. int i;
  617. if(MPV_frame_start(s, s->avctx) < 0)
  618. return -1;
  619. ff_er_frame_start(s);
  620. /*
  621. * MPV_frame_start uses pict_type to derive key_frame.
  622. * This is incorrect for H.264; IDR markings must be used.
  623. * Zero here; IDR markings per slice in frame or fields are ORed in later.
  624. * See decode_nal_units().
  625. */
  626. s->current_picture_ptr->key_frame= 0;
  627. s->current_picture_ptr->mmco_reset= 0;
  628. assert(s->linesize && s->uvlinesize);
  629. for(i=0; i<16; i++){
  630. h->block_offset[i]= (4*((scan8[i] - scan8[0])&7)<<h->pixel_shift) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  631. h->block_offset[24+i]= (4*((scan8[i] - scan8[0])&7)<<h->pixel_shift) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
  632. }
  633. for(i=0; i<4; i++){
  634. h->block_offset[16+i]=
  635. h->block_offset[20+i]= (4*((scan8[i] - scan8[0])&7)<<h->pixel_shift) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  636. h->block_offset[24+16+i]=
  637. h->block_offset[24+20+i]= (4*((scan8[i] - scan8[0])&7)<<h->pixel_shift) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  638. }
  639. /* can't be in alloc_tables because linesize isn't known there.
  640. * FIXME: redo bipred weight to not require extra buffer? */
  641. for(i = 0; i < s->avctx->thread_count; i++)
  642. if(h->thread_context[i] && !h->thread_context[i]->s.obmc_scratchpad)
  643. h->thread_context[i]->s.obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
  644. /* some macroblocks can be accessed before they're available in case of lost slices, mbaff or threading*/
  645. memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(*h->slice_table));
  646. // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
  647. // We mark the current picture as non-reference after allocating it, so
  648. // that if we break out due to an error it can be released automatically
  649. // in the next MPV_frame_start().
  650. // SVQ3 as well as most other codecs have only last/next/current and thus
  651. // get released even with set reference, besides SVQ3 and others do not
  652. // mark frames as reference later "naturally".
  653. if(s->codec_id != CODEC_ID_SVQ3)
  654. s->current_picture_ptr->reference= 0;
  655. s->current_picture_ptr->field_poc[0]=
  656. s->current_picture_ptr->field_poc[1]= INT_MAX;
  657. h->next_output_pic = NULL;
  658. assert(s->current_picture_ptr->long_ref==0);
  659. return 0;
  660. }
  661. /**
  662. * Run setup operations that must be run after slice header decoding.
  663. * This includes finding the next displayed frame.
  664. *
  665. * @param h h264 master context
  666. */
  667. static void decode_postinit(H264Context *h){
  668. MpegEncContext * const s = &h->s;
  669. Picture *out = s->current_picture_ptr;
  670. Picture *cur = s->current_picture_ptr;
  671. int i, pics, out_of_order, out_idx;
  672. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
  673. s->current_picture_ptr->pict_type= s->pict_type;
  674. if (h->next_output_pic) return;
  675. if (cur->field_poc[0]==INT_MAX || cur->field_poc[1]==INT_MAX) {
  676. //FIXME this allows the next thread to start once we encounter the first field of a PAFF packet
  677. //This works if the next packet contains the second field. It does not work if both fields are
  678. //in the same packet.
  679. //ff_thread_finish_setup(s->avctx);
  680. return;
  681. }
  682. cur->interlaced_frame = 0;
  683. cur->repeat_pict = 0;
  684. /* Signal interlacing information externally. */
  685. /* Prioritize picture timing SEI information over used decoding process if it exists. */
  686. if(h->sps.pic_struct_present_flag){
  687. switch (h->sei_pic_struct)
  688. {
  689. case SEI_PIC_STRUCT_FRAME:
  690. break;
  691. case SEI_PIC_STRUCT_TOP_FIELD:
  692. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  693. cur->interlaced_frame = 1;
  694. break;
  695. case SEI_PIC_STRUCT_TOP_BOTTOM:
  696. case SEI_PIC_STRUCT_BOTTOM_TOP:
  697. if (FIELD_OR_MBAFF_PICTURE)
  698. cur->interlaced_frame = 1;
  699. else
  700. // try to flag soft telecine progressive
  701. cur->interlaced_frame = h->prev_interlaced_frame;
  702. break;
  703. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  704. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  705. // Signal the possibility of telecined film externally (pic_struct 5,6)
  706. // From these hints, let the applications decide if they apply deinterlacing.
  707. cur->repeat_pict = 1;
  708. break;
  709. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  710. // Force progressive here, as doubling interlaced frame is a bad idea.
  711. cur->repeat_pict = 2;
  712. break;
  713. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  714. cur->repeat_pict = 4;
  715. break;
  716. }
  717. if ((h->sei_ct_type & 3) && h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP)
  718. cur->interlaced_frame = (h->sei_ct_type & (1<<1)) != 0;
  719. }else{
  720. /* Derive interlacing flag from used decoding process. */
  721. cur->interlaced_frame = FIELD_OR_MBAFF_PICTURE;
  722. }
  723. h->prev_interlaced_frame = cur->interlaced_frame;
  724. if (cur->field_poc[0] != cur->field_poc[1]){
  725. /* Derive top_field_first from field pocs. */
  726. cur->top_field_first = cur->field_poc[0] < cur->field_poc[1];
  727. }else{
  728. if(cur->interlaced_frame || h->sps.pic_struct_present_flag){
  729. /* Use picture timing SEI information. Even if it is a information of a past frame, better than nothing. */
  730. if(h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM
  731. || h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
  732. cur->top_field_first = 1;
  733. else
  734. cur->top_field_first = 0;
  735. }else{
  736. /* Most likely progressive */
  737. cur->top_field_first = 0;
  738. }
  739. }
  740. //FIXME do something with unavailable reference frames
  741. /* Sort B-frames into display order */
  742. if(h->sps.bitstream_restriction_flag
  743. && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  744. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  745. s->low_delay = 0;
  746. }
  747. if( s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT
  748. && !h->sps.bitstream_restriction_flag){
  749. s->avctx->has_b_frames= MAX_DELAYED_PIC_COUNT;
  750. s->low_delay= 0;
  751. }
  752. pics = 0;
  753. while(h->delayed_pic[pics]) pics++;
  754. assert(pics <= MAX_DELAYED_PIC_COUNT);
  755. h->delayed_pic[pics++] = cur;
  756. if(cur->reference == 0)
  757. cur->reference = DELAYED_PIC_REF;
  758. out = h->delayed_pic[0];
  759. out_idx = 0;
  760. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++)
  761. if(h->delayed_pic[i]->poc < out->poc){
  762. out = h->delayed_pic[i];
  763. out_idx = i;
  764. }
  765. if(s->avctx->has_b_frames == 0 && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset))
  766. h->next_outputed_poc= INT_MIN;
  767. out_of_order = out->poc < h->next_outputed_poc;
  768. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
  769. { }
  770. else if((out_of_order && pics-1 == s->avctx->has_b_frames && s->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT)
  771. || (s->low_delay &&
  772. ((h->next_outputed_poc != INT_MIN && out->poc > h->next_outputed_poc + 2)
  773. || cur->pict_type == FF_B_TYPE)))
  774. {
  775. s->low_delay = 0;
  776. s->avctx->has_b_frames++;
  777. }
  778. if(out_of_order || pics > s->avctx->has_b_frames){
  779. out->reference &= ~DELAYED_PIC_REF;
  780. for(i=out_idx; h->delayed_pic[i]; i++)
  781. h->delayed_pic[i] = h->delayed_pic[i+1];
  782. }
  783. if(!out_of_order && pics > s->avctx->has_b_frames){
  784. h->next_output_pic = out;
  785. if(out_idx==0 && h->delayed_pic[0] && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset)) {
  786. h->next_outputed_poc = INT_MIN;
  787. } else
  788. h->next_outputed_poc = out->poc;
  789. }else{
  790. av_log(s->avctx, AV_LOG_DEBUG, "no picture\n");
  791. }
  792. ff_thread_finish_setup(s->avctx);
  793. }
  794. 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){
  795. MpegEncContext * const s = &h->s;
  796. uint8_t *top_border;
  797. int top_idx = 1;
  798. src_y -= linesize;
  799. src_cb -= uvlinesize;
  800. src_cr -= uvlinesize;
  801. if(!simple && FRAME_MBAFF){
  802. if(s->mb_y&1){
  803. if(!MB_MBAFF){
  804. top_border = h->top_borders[0][s->mb_x];
  805. AV_COPY128(top_border, src_y + 15*linesize);
  806. if (h->pixel_shift)
  807. AV_COPY128(top_border+16, src_y+15*linesize+16);
  808. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  809. if (h->pixel_shift) {
  810. AV_COPY128(top_border+32, src_cb+7*uvlinesize);
  811. AV_COPY128(top_border+48, src_cr+7*uvlinesize);
  812. } else {
  813. AV_COPY64(top_border+16, src_cb+7*uvlinesize);
  814. AV_COPY64(top_border+24, src_cr+7*uvlinesize);
  815. }
  816. }
  817. }
  818. }else if(MB_MBAFF){
  819. top_idx = 0;
  820. }else
  821. return;
  822. }
  823. top_border = h->top_borders[top_idx][s->mb_x];
  824. // There are two lines saved, the line above the the top macroblock of a pair,
  825. // and the line above the bottom macroblock
  826. AV_COPY128(top_border, src_y + 16*linesize);
  827. if (h->pixel_shift)
  828. AV_COPY128(top_border+16, src_y+16*linesize+16);
  829. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  830. if (h->pixel_shift) {
  831. AV_COPY128(top_border+32, src_cb+8*uvlinesize);
  832. AV_COPY128(top_border+48, src_cr+8*uvlinesize);
  833. } else {
  834. AV_COPY64(top_border+16, src_cb+8*uvlinesize);
  835. AV_COPY64(top_border+24, src_cr+8*uvlinesize);
  836. }
  837. }
  838. }
  839. 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, int pixel_shift){
  840. MpegEncContext * const s = &h->s;
  841. int deblock_left;
  842. int deblock_top;
  843. int top_idx = 1;
  844. uint8_t *top_border_m1;
  845. uint8_t *top_border;
  846. if(!simple && FRAME_MBAFF){
  847. if(s->mb_y&1){
  848. if(!MB_MBAFF)
  849. return;
  850. }else{
  851. top_idx = MB_MBAFF ? 0 : 1;
  852. }
  853. }
  854. if(h->deblocking_filter == 2) {
  855. deblock_left = h->left_type[0];
  856. deblock_top = h->top_type;
  857. } else {
  858. deblock_left = (s->mb_x > 0);
  859. deblock_top = (s->mb_y > !!MB_FIELD);
  860. }
  861. src_y -= linesize + 1 + pixel_shift;
  862. src_cb -= uvlinesize + 1 + pixel_shift;
  863. src_cr -= uvlinesize + 1 + pixel_shift;
  864. top_border_m1 = h->top_borders[top_idx][s->mb_x-1];
  865. top_border = h->top_borders[top_idx][s->mb_x];
  866. #define XCHG(a,b,xchg)\
  867. if (pixel_shift) {\
  868. if (xchg) {\
  869. AV_SWAP64(b+0,a+0);\
  870. AV_SWAP64(b+8,a+8);\
  871. } else {\
  872. AV_COPY128(b,a); \
  873. }\
  874. } else \
  875. if (xchg) AV_SWAP64(b,a);\
  876. else AV_COPY64(b,a);
  877. if(deblock_top){
  878. if(deblock_left){
  879. XCHG(top_border_m1+(8<<pixel_shift), src_y -(7<<h->pixel_shift), 1);
  880. }
  881. XCHG(top_border+(0<<pixel_shift), src_y +(1<<pixel_shift), xchg);
  882. XCHG(top_border+(8<<pixel_shift), src_y +(9<<pixel_shift), 1);
  883. if(s->mb_x+1 < s->mb_width){
  884. XCHG(h->top_borders[top_idx][s->mb_x+1], src_y +(17<<pixel_shift), 1);
  885. }
  886. }
  887. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  888. if(deblock_top){
  889. if(deblock_left){
  890. XCHG(top_border_m1+(16<<pixel_shift), src_cb -(7<<pixel_shift), 1);
  891. XCHG(top_border_m1+(24<<pixel_shift), src_cr -(7<<pixel_shift), 1);
  892. }
  893. XCHG(top_border+(16<<pixel_shift), src_cb+1+pixel_shift, 1);
  894. XCHG(top_border+(24<<pixel_shift), src_cr+1+pixel_shift, 1);
  895. }
  896. }
  897. }
  898. static av_always_inline int dctcoef_get(H264Context *h, DCTELEM *mb, int index, int pixel_shift) {
  899. if (!pixel_shift)
  900. return mb[index];
  901. else
  902. return ((int32_t*)mb)[index];
  903. }
  904. static av_always_inline void dctcoef_set(H264Context *h, DCTELEM *mb, int index, int value, int pixel_shift) {
  905. if (!pixel_shift)
  906. mb[index] = value;
  907. else
  908. ((int32_t*)mb)[index] = value;
  909. }
  910. static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple, int pixel_shift){
  911. MpegEncContext * const s = &h->s;
  912. const int mb_x= s->mb_x;
  913. const int mb_y= s->mb_y;
  914. const int mb_xy= h->mb_xy;
  915. const int mb_type= s->current_picture.mb_type[mb_xy];
  916. uint8_t *dest_y, *dest_cb, *dest_cr;
  917. int linesize, uvlinesize /*dct_offset*/;
  918. int i;
  919. int *block_offset = &h->block_offset[0];
  920. const int transform_bypass = !simple && (s->qscale == 0 && h->sps.transform_bypass);
  921. /* is_h264 should always be true if SVQ3 is disabled. */
  922. const int is_h264 = !CONFIG_SVQ3_DECODER || simple || s->codec_id == CODEC_ID_H264;
  923. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  924. void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  925. dest_y = s->current_picture.data[0] + ((mb_x<<pixel_shift) + mb_y * s->linesize ) * 16;
  926. dest_cb = s->current_picture.data[1] + ((mb_x<<pixel_shift) + mb_y * s->uvlinesize) * 8;
  927. dest_cr = s->current_picture.data[2] + ((mb_x<<pixel_shift) + mb_y * s->uvlinesize) * 8;
  928. s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + (64<<pixel_shift), s->linesize, 4);
  929. s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + (64<<pixel_shift), dest_cr - dest_cb, 2);
  930. h->list_counts[mb_xy]= h->list_count;
  931. if (!simple && MB_FIELD) {
  932. linesize = h->mb_linesize = s->linesize * 2;
  933. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  934. block_offset = &h->block_offset[24];
  935. if(mb_y&1){ //FIXME move out of this function?
  936. dest_y -= s->linesize*15;
  937. dest_cb-= s->uvlinesize*7;
  938. dest_cr-= s->uvlinesize*7;
  939. }
  940. if(FRAME_MBAFF) {
  941. int list;
  942. for(list=0; list<h->list_count; list++){
  943. if(!USES_LIST(mb_type, list))
  944. continue;
  945. if(IS_16X16(mb_type)){
  946. int8_t *ref = &h->ref_cache[list][scan8[0]];
  947. fill_rectangle(ref, 4, 4, 8, (16+*ref)^(s->mb_y&1), 1);
  948. }else{
  949. for(i=0; i<16; i+=4){
  950. int ref = h->ref_cache[list][scan8[i]];
  951. if(ref >= 0)
  952. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, (16+ref)^(s->mb_y&1), 1);
  953. }
  954. }
  955. }
  956. }
  957. } else {
  958. linesize = h->mb_linesize = s->linesize;
  959. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  960. // dct_offset = s->linesize * 16;
  961. }
  962. if (!simple && IS_INTRA_PCM(mb_type)) {
  963. if (pixel_shift) {
  964. const int bit_depth = h->sps.bit_depth_luma;
  965. int j;
  966. GetBitContext gb;
  967. init_get_bits(&gb, (uint8_t*)h->mb, 384*bit_depth);
  968. for (i = 0; i < 16; i++) {
  969. uint16_t *tmp_y = (uint16_t*)(dest_y + i*linesize);
  970. for (j = 0; j < 16; j++)
  971. tmp_y[j] = get_bits(&gb, bit_depth);
  972. }
  973. for (i = 0; i < 8; i++) {
  974. uint16_t *tmp_cb = (uint16_t*)(dest_cb + i*uvlinesize);
  975. for (j = 0; j < 8; j++)
  976. tmp_cb[j] = get_bits(&gb, bit_depth);
  977. }
  978. for (i = 0; i < 8; i++) {
  979. uint16_t *tmp_cr = (uint16_t*)(dest_cr + i*uvlinesize);
  980. for (j = 0; j < 8; j++)
  981. tmp_cr[j] = get_bits(&gb, bit_depth);
  982. }
  983. } else {
  984. for (i=0; i<16; i++) {
  985. memcpy(dest_y + i* linesize, h->mb + i*8, 16);
  986. }
  987. for (i=0; i<8; i++) {
  988. memcpy(dest_cb+ i*uvlinesize, h->mb + 128 + i*4, 8);
  989. memcpy(dest_cr+ i*uvlinesize, h->mb + 160 + i*4, 8);
  990. }
  991. }
  992. } else {
  993. if(IS_INTRA(mb_type)){
  994. if(h->deblocking_filter)
  995. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1, simple, pixel_shift);
  996. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  997. h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  998. h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  999. }
  1000. if(IS_INTRA4x4(mb_type)){
  1001. if(simple || !s->encoding){
  1002. if(IS_8x8DCT(mb_type)){
  1003. if(transform_bypass){
  1004. idct_dc_add =
  1005. idct_add = s->dsp.add_pixels8;
  1006. }else{
  1007. idct_dc_add = h->h264dsp.h264_idct8_dc_add;
  1008. idct_add = h->h264dsp.h264_idct8_add;
  1009. }
  1010. for(i=0; i<16; i+=4){
  1011. uint8_t * const ptr= dest_y + block_offset[i];
  1012. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  1013. if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
  1014. h->hpc.pred8x8l_add[dir](ptr, h->mb + (i*16<<pixel_shift), linesize);
  1015. }else{
  1016. const int nnz = h->non_zero_count_cache[ scan8[i] ];
  1017. h->hpc.pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
  1018. (h->topright_samples_available<<i)&0x4000, linesize);
  1019. if(nnz){
  1020. if(nnz == 1 && dctcoef_get(h, h->mb, i*16, pixel_shift))
  1021. idct_dc_add(ptr, h->mb + (i*16<<pixel_shift), linesize);
  1022. else
  1023. idct_add (ptr, h->mb + (i*16<<pixel_shift), linesize);
  1024. }
  1025. }
  1026. }
  1027. }else{
  1028. if(transform_bypass){
  1029. idct_dc_add =
  1030. idct_add = s->dsp.add_pixels4;
  1031. }else{
  1032. idct_dc_add = h->h264dsp.h264_idct_dc_add;
  1033. idct_add = h->h264dsp.h264_idct_add;
  1034. }
  1035. for(i=0; i<16; i++){
  1036. uint8_t * const ptr= dest_y + block_offset[i];
  1037. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  1038. if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
  1039. h->hpc.pred4x4_add[dir](ptr, h->mb + (i*16<<pixel_shift), linesize);
  1040. }else{
  1041. uint8_t *topright;
  1042. int nnz, tr;
  1043. uint64_t tr_high;
  1044. if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
  1045. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  1046. assert(mb_y || linesize <= block_offset[i]);
  1047. if(!topright_avail){
  1048. if (pixel_shift) {
  1049. tr_high= ((uint16_t*)ptr)[3 - linesize/2]*0x0001000100010001ULL;
  1050. topright= (uint8_t*) &tr_high;
  1051. } else {
  1052. tr= ptr[3 - linesize]*0x01010101;
  1053. topright= (uint8_t*) &tr;
  1054. }
  1055. }else
  1056. topright= ptr + (4<<pixel_shift) - linesize;
  1057. }else
  1058. topright= NULL;
  1059. h->hpc.pred4x4[ dir ](ptr, topright, linesize);
  1060. nnz = h->non_zero_count_cache[ scan8[i] ];
  1061. if(nnz){
  1062. if(is_h264){
  1063. if(nnz == 1 && dctcoef_get(h, h->mb, i*16, pixel_shift))
  1064. idct_dc_add(ptr, h->mb + (i*16<<pixel_shift), linesize);
  1065. else
  1066. idct_add (ptr, h->mb + (i*16<<pixel_shift), linesize);
  1067. }
  1068. #if CONFIG_SVQ3_DECODER
  1069. else
  1070. ff_svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
  1071. #endif
  1072. }
  1073. }
  1074. }
  1075. }
  1076. }
  1077. }else{
  1078. h->hpc.pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  1079. if(is_h264){
  1080. if(h->non_zero_count_cache[ scan8[LUMA_DC_BLOCK_INDEX] ]){
  1081. if(!transform_bypass)
  1082. h->h264dsp.h264_luma_dc_dequant_idct(h->mb, h->mb_luma_dc, h->dequant4_coeff[0][s->qscale][0]);
  1083. else{
  1084. static const uint8_t dc_mapping[16] = { 0*16, 1*16, 4*16, 5*16, 2*16, 3*16, 6*16, 7*16,
  1085. 8*16, 9*16,12*16,13*16,10*16,11*16,14*16,15*16};
  1086. for(i = 0; i < 16; i++)
  1087. dctcoef_set(h, h->mb, dc_mapping[i], dctcoef_get(h, h->mb_luma_dc, i,pixel_shift),pixel_shift);
  1088. }
  1089. }
  1090. }
  1091. #if CONFIG_SVQ3_DECODER
  1092. else
  1093. ff_svq3_luma_dc_dequant_idct_c(h->mb, h->mb_luma_dc, s->qscale);
  1094. #endif
  1095. }
  1096. if(h->deblocking_filter)
  1097. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0, simple, pixel_shift);
  1098. }else if(is_h264){
  1099. ff_hl_motion(h, dest_y, dest_cb, dest_cr,
  1100. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  1101. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  1102. h->h264dsp.weight_h264_pixels_tab, h->h264dsp.biweight_h264_pixels_tab);
  1103. }
  1104. if(!IS_INTRA4x4(mb_type)){
  1105. if(is_h264){
  1106. if(IS_INTRA16x16(mb_type)){
  1107. if(transform_bypass){
  1108. if(h->sps.profile_idc==244 && (h->intra16x16_pred_mode==VERT_PRED8x8 || h->intra16x16_pred_mode==HOR_PRED8x8)){
  1109. h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset, h->mb, linesize);
  1110. }else{
  1111. for(i=0; i<16; i++){
  1112. if(h->non_zero_count_cache[ scan8[i] ] || dctcoef_get(h, h->mb, i*16,pixel_shift))
  1113. s->dsp.add_pixels4(dest_y + block_offset[i], h->mb + (i*16<<pixel_shift), linesize);
  1114. }
  1115. }
  1116. }else{
  1117. h->h264dsp.h264_idct_add16intra(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
  1118. }
  1119. }else if(h->cbp&15){
  1120. if(transform_bypass){
  1121. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  1122. idct_add= IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
  1123. for(i=0; i<16; i+=di){
  1124. if(h->non_zero_count_cache[ scan8[i] ]){
  1125. idct_add(dest_y + block_offset[i], h->mb + (i*16<<pixel_shift), linesize);
  1126. }
  1127. }
  1128. }else{
  1129. if(IS_8x8DCT(mb_type)){
  1130. h->h264dsp.h264_idct8_add4(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
  1131. }else{
  1132. h->h264dsp.h264_idct_add16(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
  1133. }
  1134. }
  1135. }
  1136. }
  1137. #if CONFIG_SVQ3_DECODER
  1138. else{
  1139. for(i=0; i<16; i++){
  1140. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  1141. uint8_t * const ptr= dest_y + block_offset[i];
  1142. ff_svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  1143. }
  1144. }
  1145. }
  1146. #endif
  1147. }
  1148. if((simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)) && (h->cbp&0x30)){
  1149. uint8_t *dest[2] = {dest_cb, dest_cr};
  1150. if(transform_bypass){
  1151. if(IS_INTRA(mb_type) && h->sps.profile_idc==244 && (h->chroma_pred_mode==VERT_PRED8x8 || h->chroma_pred_mode==HOR_PRED8x8)){
  1152. h->hpc.pred8x8_add[h->chroma_pred_mode](dest[0], block_offset + 16, h->mb + (16*16<<pixel_shift), uvlinesize);
  1153. h->hpc.pred8x8_add[h->chroma_pred_mode](dest[1], block_offset + 20, h->mb + (20*16<<pixel_shift), uvlinesize);
  1154. }else{
  1155. idct_add = s->dsp.add_pixels4;
  1156. for(i=16; i<16+8; i++){
  1157. if(h->non_zero_count_cache[ scan8[i] ] || dctcoef_get(h, h->mb, i*16,pixel_shift))
  1158. idct_add (dest[(i&4)>>2] + block_offset[i], h->mb + (i*16<<pixel_shift), uvlinesize);
  1159. }
  1160. }
  1161. }else{
  1162. if(is_h264){
  1163. if(h->non_zero_count_cache[ scan8[CHROMA_DC_BLOCK_INDEX+0] ])
  1164. h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + (16*16<<pixel_shift) , h->dequant4_coeff[IS_INTRA(mb_type) ? 1:4][h->chroma_qp[0]][0]);
  1165. if(h->non_zero_count_cache[ scan8[CHROMA_DC_BLOCK_INDEX+1] ])
  1166. h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + ((16*16+4*16)<<pixel_shift), h->dequant4_coeff[IS_INTRA(mb_type) ? 2:5][h->chroma_qp[1]][0]);
  1167. h->h264dsp.h264_idct_add8(dest, block_offset,
  1168. h->mb, uvlinesize,
  1169. h->non_zero_count_cache);
  1170. }
  1171. #if CONFIG_SVQ3_DECODER
  1172. else{
  1173. h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16*16 , h->dequant4_coeff[IS_INTRA(mb_type) ? 1:4][h->chroma_qp[0]][0]);
  1174. h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16*16+4*16, h->dequant4_coeff[IS_INTRA(mb_type) ? 2:5][h->chroma_qp[1]][0]);
  1175. for(i=16; i<16+8; i++){
  1176. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  1177. uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
  1178. ff_svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, ff_h264_chroma_qp[0][s->qscale + 12] - 12, 2);
  1179. }
  1180. }
  1181. }
  1182. #endif
  1183. }
  1184. }
  1185. }
  1186. if(h->cbp || IS_INTRA(mb_type))
  1187. s->dsp.clear_blocks(h->mb);
  1188. }
  1189. /**
  1190. * Process a macroblock; this case avoids checks for expensive uncommon cases.
  1191. */
  1192. static void hl_decode_mb_simple8(H264Context *h){
  1193. hl_decode_mb_internal(h, 1, 0);
  1194. }
  1195. /**
  1196. * Process a macroblock; this handles edge cases, such as interlacing.
  1197. */
  1198. static void av_noinline hl_decode_mb_complex(H264Context *h){
  1199. hl_decode_mb_internal(h, 0, h->pixel_shift);
  1200. }
  1201. void ff_h264_hl_decode_mb(H264Context *h){
  1202. MpegEncContext * const s = &h->s;
  1203. const int mb_xy= h->mb_xy;
  1204. const int mb_type= s->current_picture.mb_type[mb_xy];
  1205. int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || s->qscale == 0;
  1206. if (is_complex || h->pixel_shift)
  1207. hl_decode_mb_complex(h);
  1208. else{
  1209. hl_decode_mb_simple8(h);
  1210. }
  1211. }
  1212. static int pred_weight_table(H264Context *h){
  1213. MpegEncContext * const s = &h->s;
  1214. int list, i;
  1215. int luma_def, chroma_def;
  1216. h->use_weight= 0;
  1217. h->use_weight_chroma= 0;
  1218. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  1219. if(CHROMA)
  1220. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  1221. luma_def = 1<<h->luma_log2_weight_denom;
  1222. chroma_def = 1<<h->chroma_log2_weight_denom;
  1223. for(list=0; list<2; list++){
  1224. h->luma_weight_flag[list] = 0;
  1225. h->chroma_weight_flag[list] = 0;
  1226. for(i=0; i<h->ref_count[list]; i++){
  1227. int luma_weight_flag, chroma_weight_flag;
  1228. luma_weight_flag= get_bits1(&s->gb);
  1229. if(luma_weight_flag){
  1230. h->luma_weight[i][list][0]= get_se_golomb(&s->gb);
  1231. h->luma_weight[i][list][1]= get_se_golomb(&s->gb);
  1232. if( h->luma_weight[i][list][0] != luma_def
  1233. || h->luma_weight[i][list][1] != 0) {
  1234. h->use_weight= 1;
  1235. h->luma_weight_flag[list]= 1;
  1236. }
  1237. }else{
  1238. h->luma_weight[i][list][0]= luma_def;
  1239. h->luma_weight[i][list][1]= 0;
  1240. }
  1241. if(CHROMA){
  1242. chroma_weight_flag= get_bits1(&s->gb);
  1243. if(chroma_weight_flag){
  1244. int j;
  1245. for(j=0; j<2; j++){
  1246. h->chroma_weight[i][list][j][0]= get_se_golomb(&s->gb);
  1247. h->chroma_weight[i][list][j][1]= get_se_golomb(&s->gb);
  1248. if( h->chroma_weight[i][list][j][0] != chroma_def
  1249. || h->chroma_weight[i][list][j][1] != 0) {
  1250. h->use_weight_chroma= 1;
  1251. h->chroma_weight_flag[list]= 1;
  1252. }
  1253. }
  1254. }else{
  1255. int j;
  1256. for(j=0; j<2; j++){
  1257. h->chroma_weight[i][list][j][0]= chroma_def;
  1258. h->chroma_weight[i][list][j][1]= 0;
  1259. }
  1260. }
  1261. }
  1262. }
  1263. if(h->slice_type_nos != FF_B_TYPE) break;
  1264. }
  1265. h->use_weight= h->use_weight || h->use_weight_chroma;
  1266. return 0;
  1267. }
  1268. /**
  1269. * Initialize implicit_weight table.
  1270. * @param field 0/1 initialize the weight for interlaced MBAFF
  1271. * -1 initializes the rest
  1272. */
  1273. static void implicit_weight_table(H264Context *h, int field){
  1274. MpegEncContext * const s = &h->s;
  1275. int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
  1276. for (i = 0; i < 2; i++) {
  1277. h->luma_weight_flag[i] = 0;
  1278. h->chroma_weight_flag[i] = 0;
  1279. }
  1280. if(field < 0){
  1281. cur_poc = s->current_picture_ptr->poc;
  1282. if( h->ref_count[0] == 1 && h->ref_count[1] == 1 && !FRAME_MBAFF
  1283. && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
  1284. h->use_weight= 0;
  1285. h->use_weight_chroma= 0;
  1286. return;
  1287. }
  1288. ref_start= 0;
  1289. ref_count0= h->ref_count[0];
  1290. ref_count1= h->ref_count[1];
  1291. }else{
  1292. cur_poc = s->current_picture_ptr->field_poc[field];
  1293. ref_start= 16;
  1294. ref_count0= 16+2*h->ref_count[0];
  1295. ref_count1= 16+2*h->ref_count[1];
  1296. }
  1297. h->use_weight= 2;
  1298. h->use_weight_chroma= 2;
  1299. h->luma_log2_weight_denom= 5;
  1300. h->chroma_log2_weight_denom= 5;
  1301. for(ref0=ref_start; ref0 < ref_count0; ref0++){
  1302. int poc0 = h->ref_list[0][ref0].poc;
  1303. for(ref1=ref_start; ref1 < ref_count1; ref1++){
  1304. int poc1 = h->ref_list[1][ref1].poc;
  1305. int td = av_clip(poc1 - poc0, -128, 127);
  1306. int w= 32;
  1307. if(td){
  1308. int tb = av_clip(cur_poc - poc0, -128, 127);
  1309. int tx = (16384 + (FFABS(td) >> 1)) / td;
  1310. int dist_scale_factor = (tb*tx + 32) >> 8;
  1311. if(dist_scale_factor >= -64 && dist_scale_factor <= 128)
  1312. w = 64 - dist_scale_factor;
  1313. }
  1314. if(field<0){
  1315. h->implicit_weight[ref0][ref1][0]=
  1316. h->implicit_weight[ref0][ref1][1]= w;
  1317. }else{
  1318. h->implicit_weight[ref0][ref1][field]=w;
  1319. }
  1320. }
  1321. }
  1322. }
  1323. /**
  1324. * instantaneous decoder refresh.
  1325. */
  1326. static void idr(H264Context *h){
  1327. ff_h264_remove_all_refs(h);
  1328. h->prev_frame_num= 0;
  1329. h->prev_frame_num_offset= 0;
  1330. h->prev_poc_msb=
  1331. h->prev_poc_lsb= 0;
  1332. }
  1333. /* forget old pics after a seek */
  1334. static void flush_dpb(AVCodecContext *avctx){
  1335. H264Context *h= avctx->priv_data;
  1336. int i;
  1337. for(i=0; i<MAX_DELAYED_PIC_COUNT; i++) {
  1338. if(h->delayed_pic[i])
  1339. h->delayed_pic[i]->reference= 0;
  1340. h->delayed_pic[i]= NULL;
  1341. }
  1342. h->outputed_poc=h->next_outputed_poc= INT_MIN;
  1343. h->prev_interlaced_frame = 1;
  1344. idr(h);
  1345. if(h->s.current_picture_ptr)
  1346. h->s.current_picture_ptr->reference= 0;
  1347. h->s.first_field= 0;
  1348. ff_h264_reset_sei(h);
  1349. ff_mpeg_flush(avctx);
  1350. }
  1351. static int init_poc(H264Context *h){
  1352. MpegEncContext * const s = &h->s;
  1353. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  1354. int field_poc[2];
  1355. Picture *cur = s->current_picture_ptr;
  1356. h->frame_num_offset= h->prev_frame_num_offset;
  1357. if(h->frame_num < h->prev_frame_num)
  1358. h->frame_num_offset += max_frame_num;
  1359. if(h->sps.poc_type==0){
  1360. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  1361. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  1362. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  1363. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  1364. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  1365. else
  1366. h->poc_msb = h->prev_poc_msb;
  1367. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  1368. field_poc[0] =
  1369. field_poc[1] = h->poc_msb + h->poc_lsb;
  1370. if(s->picture_structure == PICT_FRAME)
  1371. field_poc[1] += h->delta_poc_bottom;
  1372. }else if(h->sps.poc_type==1){
  1373. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  1374. int i;
  1375. if(h->sps.poc_cycle_length != 0)
  1376. abs_frame_num = h->frame_num_offset + h->frame_num;
  1377. else
  1378. abs_frame_num = 0;
  1379. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  1380. abs_frame_num--;
  1381. expected_delta_per_poc_cycle = 0;
  1382. for(i=0; i < h->sps.poc_cycle_length; i++)
  1383. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  1384. if(abs_frame_num > 0){
  1385. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  1386. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  1387. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  1388. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  1389. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  1390. } else
  1391. expectedpoc = 0;
  1392. if(h->nal_ref_idc == 0)
  1393. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  1394. field_poc[0] = expectedpoc + h->delta_poc[0];
  1395. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  1396. if(s->picture_structure == PICT_FRAME)
  1397. field_poc[1] += h->delta_poc[1];
  1398. }else{
  1399. int poc= 2*(h->frame_num_offset + h->frame_num);
  1400. if(!h->nal_ref_idc)
  1401. poc--;
  1402. field_poc[0]= poc;
  1403. field_poc[1]= poc;
  1404. }
  1405. if(s->picture_structure != PICT_BOTTOM_FIELD)
  1406. s->current_picture_ptr->field_poc[0]= field_poc[0];
  1407. if(s->picture_structure != PICT_TOP_FIELD)
  1408. s->current_picture_ptr->field_poc[1]= field_poc[1];
  1409. cur->poc= FFMIN(cur->field_poc[0], cur->field_poc[1]);
  1410. return 0;
  1411. }
  1412. /**
  1413. * initialize scan tables
  1414. */
  1415. static void init_scan_tables(H264Context *h){
  1416. int i;
  1417. for(i=0; i<16; i++){
  1418. #define T(x) (x>>2) | ((x<<2) & 0xF)
  1419. h->zigzag_scan[i] = T(zigzag_scan[i]);
  1420. h-> field_scan[i] = T( field_scan[i]);
  1421. #undef T
  1422. }
  1423. for(i=0; i<64; i++){
  1424. #define T(x) (x>>3) | ((x&7)<<3)
  1425. h->zigzag_scan8x8[i] = T(ff_zigzag_direct[i]);
  1426. h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
  1427. h->field_scan8x8[i] = T(field_scan8x8[i]);
  1428. h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
  1429. #undef T
  1430. }
  1431. if(h->sps.transform_bypass){ //FIXME same ugly
  1432. h->zigzag_scan_q0 = zigzag_scan;
  1433. h->zigzag_scan8x8_q0 = ff_zigzag_direct;
  1434. h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
  1435. h->field_scan_q0 = field_scan;
  1436. h->field_scan8x8_q0 = field_scan8x8;
  1437. h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
  1438. }else{
  1439. h->zigzag_scan_q0 = h->zigzag_scan;
  1440. h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
  1441. h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
  1442. h->field_scan_q0 = h->field_scan;
  1443. h->field_scan8x8_q0 = h->field_scan8x8;
  1444. h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
  1445. }
  1446. }
  1447. static void field_end(H264Context *h, int in_setup){
  1448. MpegEncContext * const s = &h->s;
  1449. AVCodecContext * const avctx= s->avctx;
  1450. s->mb_y= 0;
  1451. if (!in_setup && !s->dropable)
  1452. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, (16*s->mb_height >> FIELD_PICTURE) - 1,
  1453. s->picture_structure==PICT_BOTTOM_FIELD);
  1454. if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  1455. ff_vdpau_h264_set_reference_frames(s);
  1456. if(in_setup || !(avctx->active_thread_type&FF_THREAD_FRAME)){
  1457. if(!s->dropable) {
  1458. ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  1459. h->prev_poc_msb= h->poc_msb;
  1460. h->prev_poc_lsb= h->poc_lsb;
  1461. }
  1462. h->prev_frame_num_offset= h->frame_num_offset;
  1463. h->prev_frame_num= h->frame_num;
  1464. h->outputed_poc = h->next_outputed_poc;
  1465. }
  1466. if (avctx->hwaccel) {
  1467. if (avctx->hwaccel->end_frame(avctx) < 0)
  1468. av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
  1469. }
  1470. if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  1471. ff_vdpau_h264_picture_complete(s);
  1472. /*
  1473. * FIXME: Error handling code does not seem to support interlaced
  1474. * when slices span multiple rows
  1475. * The ff_er_add_slice calls don't work right for bottom
  1476. * fields; they cause massive erroneous error concealing
  1477. * Error marking covers both fields (top and bottom).
  1478. * This causes a mismatched s->error_count
  1479. * and a bad error table. Further, the error count goes to
  1480. * INT_MAX when called for bottom field, because mb_y is
  1481. * past end by one (callers fault) and resync_mb_y != 0
  1482. * causes problems for the first MB line, too.
  1483. */
  1484. if (!FIELD_PICTURE)
  1485. ff_er_frame_end(s);
  1486. MPV_frame_end(s);
  1487. h->current_slice=0;
  1488. }
  1489. /**
  1490. * Replicate H264 "master" context to thread contexts.
  1491. */
  1492. static void clone_slice(H264Context *dst, H264Context *src)
  1493. {
  1494. memcpy(dst->block_offset, src->block_offset, sizeof(dst->block_offset));
  1495. dst->s.current_picture_ptr = src->s.current_picture_ptr;
  1496. dst->s.current_picture = src->s.current_picture;
  1497. dst->s.linesize = src->s.linesize;
  1498. dst->s.uvlinesize = src->s.uvlinesize;
  1499. dst->s.first_field = src->s.first_field;
  1500. dst->prev_poc_msb = src->prev_poc_msb;
  1501. dst->prev_poc_lsb = src->prev_poc_lsb;
  1502. dst->prev_frame_num_offset = src->prev_frame_num_offset;
  1503. dst->prev_frame_num = src->prev_frame_num;
  1504. dst->short_ref_count = src->short_ref_count;
  1505. memcpy(dst->short_ref, src->short_ref, sizeof(dst->short_ref));
  1506. memcpy(dst->long_ref, src->long_ref, sizeof(dst->long_ref));
  1507. memcpy(dst->default_ref_list, src->default_ref_list, sizeof(dst->default_ref_list));
  1508. memcpy(dst->ref_list, src->ref_list, sizeof(dst->ref_list));
  1509. memcpy(dst->dequant4_coeff, src->dequant4_coeff, sizeof(src->dequant4_coeff));
  1510. memcpy(dst->dequant8_coeff, src->dequant8_coeff, sizeof(src->dequant8_coeff));
  1511. }
  1512. /**
  1513. * computes profile from profile_idc and constraint_set?_flags
  1514. *
  1515. * @param sps SPS
  1516. *
  1517. * @return profile as defined by FF_PROFILE_H264_*
  1518. */
  1519. int ff_h264_get_profile(SPS *sps)
  1520. {
  1521. int profile = sps->profile_idc;
  1522. switch(sps->profile_idc) {
  1523. case FF_PROFILE_H264_BASELINE:
  1524. // constraint_set1_flag set to 1
  1525. profile |= (sps->constraint_set_flags & 1<<1) ? FF_PROFILE_H264_CONSTRAINED : 0;
  1526. break;
  1527. case FF_PROFILE_H264_HIGH_10:
  1528. case FF_PROFILE_H264_HIGH_422:
  1529. case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
  1530. // constraint_set3_flag set to 1
  1531. profile |= (sps->constraint_set_flags & 1<<3) ? FF_PROFILE_H264_INTRA : 0;
  1532. break;
  1533. }
  1534. return profile;
  1535. }
  1536. /**
  1537. * decodes a slice header.
  1538. * This will also call MPV_common_init() and frame_start() as needed.
  1539. *
  1540. * @param h h264context
  1541. * @param h0 h264 master context (differs from 'h' when doing sliced based parallel decoding)
  1542. *
  1543. * @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded
  1544. */
  1545. static int decode_slice_header(H264Context *h, H264Context *h0){
  1546. MpegEncContext * const s = &h->s;
  1547. MpegEncContext * const s0 = &h0->s;
  1548. unsigned int first_mb_in_slice;
  1549. unsigned int pps_id;
  1550. int num_ref_idx_active_override_flag;
  1551. unsigned int slice_type, tmp, i, j;
  1552. int default_ref_list_done = 0;
  1553. int last_pic_structure;
  1554. s->dropable= h->nal_ref_idc == 0;
  1555. if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !h->nal_ref_idc){
  1556. s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
  1557. s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
  1558. }else{
  1559. s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
  1560. s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
  1561. }
  1562. first_mb_in_slice= get_ue_golomb(&s->gb);
  1563. if(first_mb_in_slice == 0){ //FIXME better field boundary detection
  1564. if(h0->current_slice && FIELD_PICTURE){
  1565. field_end(h, 1);
  1566. }
  1567. h0->current_slice = 0;
  1568. if (!s0->first_field)
  1569. s->current_picture_ptr= NULL;
  1570. }
  1571. slice_type= get_ue_golomb_31(&s->gb);
  1572. if(slice_type > 9){
  1573. 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);
  1574. return -1;
  1575. }
  1576. if(slice_type > 4){
  1577. slice_type -= 5;
  1578. h->slice_type_fixed=1;
  1579. }else
  1580. h->slice_type_fixed=0;
  1581. slice_type= golomb_to_pict_type[ slice_type ];
  1582. if (slice_type == FF_I_TYPE
  1583. || (h0->current_slice != 0 && slice_type == h0->last_slice_type) ) {
  1584. default_ref_list_done = 1;
  1585. }
  1586. h->slice_type= slice_type;
  1587. h->slice_type_nos= slice_type & 3;
  1588. s->pict_type= h->slice_type; // to make a few old functions happy, it's wrong though
  1589. pps_id= get_ue_golomb(&s->gb);
  1590. if(pps_id>=MAX_PPS_COUNT){
  1591. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  1592. return -1;
  1593. }
  1594. if(!h0->pps_buffers[pps_id]) {
  1595. av_log(h->s.avctx, AV_LOG_ERROR, "non-existing PPS %u referenced\n", pps_id);
  1596. return -1;
  1597. }
  1598. h->pps= *h0->pps_buffers[pps_id];
  1599. if(!h0->sps_buffers[h->pps.sps_id]) {
  1600. av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS %u referenced\n", h->pps.sps_id);
  1601. return -1;
  1602. }
  1603. h->sps = *h0->sps_buffers[h->pps.sps_id];
  1604. s->avctx->profile = ff_h264_get_profile(&h->sps);
  1605. s->avctx->level = h->sps.level_idc;
  1606. s->avctx->refs = h->sps.ref_frame_count;
  1607. if(h == h0 && h->dequant_coeff_pps != pps_id){
  1608. h->dequant_coeff_pps = pps_id;
  1609. init_dequant_tables(h);
  1610. }
  1611. s->mb_width= h->sps.mb_width;
  1612. s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  1613. h->b_stride= s->mb_width*4;
  1614. s->width = 16*s->mb_width - 2*FFMIN(h->sps.crop_right, 7);
  1615. if(h->sps.frame_mbs_only_flag)
  1616. s->height= 16*s->mb_height - 2*FFMIN(h->sps.crop_bottom, 7);
  1617. else
  1618. s->height= 16*s->mb_height - 4*FFMIN(h->sps.crop_bottom, 7);
  1619. if (s->context_initialized
  1620. && ( s->width != s->avctx->width || s->height != s->avctx->height
  1621. || av_cmp_q(h->sps.sar, s->avctx->sample_aspect_ratio))) {
  1622. if(h != h0) {
  1623. av_log_missing_feature(s->avctx, "Width/height changing with threads is", 0);
  1624. return -1; // width / height changed during parallelized decoding
  1625. }
  1626. free_tables(h, 0);
  1627. flush_dpb(s->avctx);
  1628. MPV_common_end(s);
  1629. }
  1630. if (!s->context_initialized) {
  1631. if(h != h0){
  1632. av_log(h->s.avctx, AV_LOG_ERROR, "we cant (re-)initialize context during parallel decoding\n");
  1633. return -1;
  1634. }
  1635. avcodec_set_dimensions(s->avctx, s->width, s->height);
  1636. s->avctx->sample_aspect_ratio= h->sps.sar;
  1637. av_assert0(s->avctx->sample_aspect_ratio.den);
  1638. if(h->sps.video_signal_type_present_flag){
  1639. s->avctx->color_range = h->sps.full_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
  1640. if(h->sps.colour_description_present_flag){
  1641. s->avctx->color_primaries = h->sps.color_primaries;
  1642. s->avctx->color_trc = h->sps.color_trc;
  1643. s->avctx->colorspace = h->sps.colorspace;
  1644. }
  1645. }
  1646. if(h->sps.timing_info_present_flag){
  1647. int64_t den= h->sps.time_scale;
  1648. if(h->x264_build < 44U)
  1649. den *= 2;
  1650. av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
  1651. h->sps.num_units_in_tick, den, 1<<30);
  1652. }
  1653. switch (h->sps.bit_depth_luma) {
  1654. case 9 :
  1655. s->avctx->pix_fmt = PIX_FMT_YUV420P9;
  1656. break;
  1657. case 10 :
  1658. s->avctx->pix_fmt = PIX_FMT_YUV420P10;
  1659. break;
  1660. default:
  1661. s->avctx->pix_fmt = s->avctx->get_format(s->avctx,
  1662. s->avctx->codec->pix_fmts ?
  1663. s->avctx->codec->pix_fmts :
  1664. s->avctx->color_range == AVCOL_RANGE_JPEG ?
  1665. hwaccel_pixfmt_list_h264_jpeg_420 :
  1666. ff_hwaccel_pixfmt_list_420);
  1667. }
  1668. s->avctx->hwaccel = ff_find_hwaccel(s->avctx->codec->id, s->avctx->pix_fmt);
  1669. if (MPV_common_init(s) < 0){
  1670. av_log(h->s.avctx, AV_LOG_ERROR, "MPV_common_init() failed\n");
  1671. return -1;
  1672. }
  1673. s->first_field = 0;
  1674. h->prev_interlaced_frame = 1;
  1675. init_scan_tables(h);
  1676. ff_h264_alloc_tables(h);
  1677. if (!HAVE_THREADS || !(s->avctx->active_thread_type&FF_THREAD_SLICE)) {
  1678. if (context_init(h) < 0){
  1679. av_log(h->s.avctx, AV_LOG_ERROR, "context_init() failed\n");
  1680. return -1;
  1681. }
  1682. } else {
  1683. for(i = 1; i < s->avctx->thread_count; i++) {
  1684. H264Context *c;
  1685. c = h->thread_context[i] = av_malloc(sizeof(H264Context));
  1686. memcpy(c, h->s.thread_context[i], sizeof(MpegEncContext));
  1687. memset(&c->s + 1, 0, sizeof(H264Context) - sizeof(MpegEncContext));
  1688. c->h264dsp = h->h264dsp;
  1689. c->sps = h->sps;
  1690. c->pps = h->pps;
  1691. init_scan_tables(c);
  1692. clone_tables(c, h, i);
  1693. }
  1694. for(i = 0; i < s->avctx->thread_count; i++)
  1695. if(context_init(h->thread_context[i]) < 0){
  1696. av_log(h->s.avctx, AV_LOG_ERROR, "context_init() failed\n");
  1697. return -1;
  1698. }
  1699. }
  1700. }
  1701. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  1702. h->mb_mbaff = 0;
  1703. h->mb_aff_frame = 0;
  1704. last_pic_structure = s0->picture_structure;
  1705. if(h->sps.frame_mbs_only_flag){
  1706. s->picture_structure= PICT_FRAME;
  1707. }else{
  1708. if(get_bits1(&s->gb)) { //field_pic_flag
  1709. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  1710. } else {
  1711. s->picture_structure= PICT_FRAME;
  1712. h->mb_aff_frame = h->sps.mb_aff;
  1713. }
  1714. }
  1715. h->mb_field_decoding_flag= s->picture_structure != PICT_FRAME;
  1716. if(h0->current_slice == 0){
  1717. if(h->frame_num != h->prev_frame_num &&
  1718. (h->prev_frame_num+1)%(1<<h->sps.log2_max_frame_num) < (h->frame_num - h->sps.ref_frame_count))
  1719. h->prev_frame_num = h->frame_num - h->sps.ref_frame_count - 1;
  1720. while(h->frame_num != h->prev_frame_num &&
  1721. h->frame_num != (h->prev_frame_num+1)%(1<<h->sps.log2_max_frame_num)){
  1722. Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
  1723. av_log(h->s.avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n", h->frame_num, h->prev_frame_num);
  1724. if (ff_h264_frame_start(h) < 0)
  1725. return -1;
  1726. h->prev_frame_num++;
  1727. h->prev_frame_num %= 1<<h->sps.log2_max_frame_num;
  1728. s->current_picture_ptr->frame_num= h->prev_frame_num;
  1729. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, INT_MAX, 0);
  1730. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, INT_MAX, 1);
  1731. ff_generate_sliding_window_mmcos(h);
  1732. ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  1733. /* Error concealment: if a ref is missing, copy the previous ref in its place.
  1734. * FIXME: avoiding a memcpy would be nice, but ref handling makes many assumptions
  1735. * about there being no actual duplicates.
  1736. * FIXME: this doesn't copy padding for out-of-frame motion vectors. Given we're
  1737. * concealing a lost frame, this probably isn't noticable by comparison, but it should
  1738. * be fixed. */
  1739. if (h->short_ref_count) {
  1740. if (prev) {
  1741. av_image_copy(h->short_ref[0]->data, h->short_ref[0]->linesize,
  1742. (const uint8_t**)prev->data, prev->linesize,
  1743. s->avctx->pix_fmt, s->mb_width*16, s->mb_height*16);
  1744. h->short_ref[0]->poc = prev->poc+2;
  1745. }
  1746. h->short_ref[0]->frame_num = h->prev_frame_num;
  1747. }
  1748. }
  1749. /* See if we have a decoded first field looking for a pair... */
  1750. if (s0->first_field) {
  1751. assert(s0->current_picture_ptr);
  1752. assert(s0->current_picture_ptr->data[0]);
  1753. assert(s0->current_picture_ptr->reference != DELAYED_PIC_REF);
  1754. /* figure out if we have a complementary field pair */
  1755. if (!FIELD_PICTURE || s->picture_structure == last_pic_structure) {
  1756. /*
  1757. * Previous field is unmatched. Don't display it, but let it
  1758. * remain for reference if marked as such.
  1759. */
  1760. s0->current_picture_ptr = NULL;
  1761. s0->first_field = FIELD_PICTURE;
  1762. } else {
  1763. if (h->nal_ref_idc &&
  1764. s0->current_picture_ptr->reference &&
  1765. s0->current_picture_ptr->frame_num != h->frame_num) {
  1766. /*
  1767. * This and previous field were reference, but had
  1768. * different frame_nums. Consider this field first in
  1769. * pair. Throw away previous field except for reference
  1770. * purposes.
  1771. */
  1772. s0->first_field = 1;
  1773. s0->current_picture_ptr = NULL;
  1774. } else {
  1775. /* Second field in complementary pair */
  1776. s0->first_field = 0;
  1777. }
  1778. }
  1779. } else {
  1780. /* Frame or first field in a potentially complementary pair */
  1781. assert(!s0->current_picture_ptr);
  1782. s0->first_field = FIELD_PICTURE;
  1783. }
  1784. if((!FIELD_PICTURE || s0->first_field) && ff_h264_frame_start(h) < 0) {
  1785. s0->first_field = 0;
  1786. return -1;
  1787. }
  1788. }
  1789. if(h != h0)
  1790. clone_slice(h, h0);
  1791. s->current_picture_ptr->frame_num= h->frame_num; //FIXME frame_num cleanup
  1792. assert(s->mb_num == s->mb_width * s->mb_height);
  1793. if(first_mb_in_slice << FIELD_OR_MBAFF_PICTURE >= s->mb_num ||
  1794. first_mb_in_slice >= s->mb_num){
  1795. av_log(h->s.avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
  1796. return -1;
  1797. }
  1798. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  1799. s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << FIELD_OR_MBAFF_PICTURE;
  1800. if (s->picture_structure == PICT_BOTTOM_FIELD)
  1801. s->resync_mb_y = s->mb_y = s->mb_y + 1;
  1802. assert(s->mb_y < s->mb_height);
  1803. if(s->picture_structure==PICT_FRAME){
  1804. h->curr_pic_num= h->frame_num;
  1805. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  1806. }else{
  1807. h->curr_pic_num= 2*h->frame_num + 1;
  1808. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  1809. }
  1810. if(h->nal_unit_type == NAL_IDR_SLICE){
  1811. get_ue_golomb(&s->gb); /* idr_pic_id */
  1812. }
  1813. if(h->sps.poc_type==0){
  1814. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  1815. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  1816. h->delta_poc_bottom= get_se_golomb(&s->gb);
  1817. }
  1818. }
  1819. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  1820. h->delta_poc[0]= get_se_golomb(&s->gb);
  1821. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  1822. h->delta_poc[1]= get_se_golomb(&s->gb);
  1823. }
  1824. init_poc(h);
  1825. if(h->pps.redundant_pic_cnt_present){
  1826. h->redundant_pic_count= get_ue_golomb(&s->gb);
  1827. }
  1828. //set defaults, might be overridden a few lines later
  1829. h->ref_count[0]= h->pps.ref_count[0];
  1830. h->ref_count[1]= h->pps.ref_count[1];
  1831. if(h->slice_type_nos != FF_I_TYPE){
  1832. if(h->slice_type_nos == FF_B_TYPE){
  1833. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  1834. }
  1835. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  1836. if(num_ref_idx_active_override_flag){
  1837. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  1838. if(h->slice_type_nos==FF_B_TYPE)
  1839. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  1840. if(h->ref_count[0]-1 > 32-1 || h->ref_count[1]-1 > 32-1){
  1841. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  1842. h->ref_count[0]= h->ref_count[1]= 1;
  1843. return -1;
  1844. }
  1845. }
  1846. if(h->slice_type_nos == FF_B_TYPE)
  1847. h->list_count= 2;
  1848. else
  1849. h->list_count= 1;
  1850. }else
  1851. h->list_count= 0;
  1852. if(!default_ref_list_done){
  1853. ff_h264_fill_default_ref_list(h);
  1854. }
  1855. if(h->slice_type_nos!=FF_I_TYPE && ff_h264_decode_ref_pic_list_reordering(h) < 0)
  1856. return -1;
  1857. if(h->slice_type_nos!=FF_I_TYPE){
  1858. s->last_picture_ptr= &h->ref_list[0][0];
  1859. ff_copy_picture(&s->last_picture, s->last_picture_ptr);
  1860. }
  1861. if(h->slice_type_nos==FF_B_TYPE){
  1862. s->next_picture_ptr= &h->ref_list[1][0];
  1863. ff_copy_picture(&s->next_picture, s->next_picture_ptr);
  1864. }
  1865. if( (h->pps.weighted_pred && h->slice_type_nos == FF_P_TYPE )
  1866. || (h->pps.weighted_bipred_idc==1 && h->slice_type_nos== FF_B_TYPE ) )
  1867. pred_weight_table(h);
  1868. else if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== FF_B_TYPE){
  1869. implicit_weight_table(h, -1);
  1870. }else {
  1871. h->use_weight = 0;
  1872. for (i = 0; i < 2; i++) {
  1873. h->luma_weight_flag[i] = 0;
  1874. h->chroma_weight_flag[i] = 0;
  1875. }
  1876. }
  1877. if(h->nal_ref_idc)
  1878. ff_h264_decode_ref_pic_marking(h0, &s->gb);
  1879. if(FRAME_MBAFF){
  1880. ff_h264_fill_mbaff_ref_list(h);
  1881. if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== FF_B_TYPE){
  1882. implicit_weight_table(h, 0);
  1883. implicit_weight_table(h, 1);
  1884. }
  1885. }
  1886. if(h->slice_type_nos==FF_B_TYPE && !h->direct_spatial_mv_pred)
  1887. ff_h264_direct_dist_scale_factor(h);
  1888. ff_h264_direct_ref_list_init(h);
  1889. if( h->slice_type_nos != FF_I_TYPE && h->pps.cabac ){
  1890. tmp = get_ue_golomb_31(&s->gb);
  1891. if(tmp > 2){
  1892. av_log(s->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
  1893. return -1;
  1894. }
  1895. h->cabac_init_idc= tmp;
  1896. }
  1897. h->last_qscale_diff = 0;
  1898. tmp = h->pps.init_qp + get_se_golomb(&s->gb);
  1899. if(tmp>51+6*(h->sps.bit_depth_luma-8)){
  1900. av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
  1901. return -1;
  1902. }
  1903. s->qscale= tmp;
  1904. h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
  1905. h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
  1906. //FIXME qscale / qp ... stuff
  1907. if(h->slice_type == FF_SP_TYPE){
  1908. get_bits1(&s->gb); /* sp_for_switch_flag */
  1909. }
  1910. if(h->slice_type==FF_SP_TYPE || h->slice_type == FF_SI_TYPE){
  1911. get_se_golomb(&s->gb); /* slice_qs_delta */
  1912. }
  1913. h->deblocking_filter = 1;
  1914. h->slice_alpha_c0_offset = 52;
  1915. h->slice_beta_offset = 52;
  1916. if( h->pps.deblocking_filter_parameters_present ) {
  1917. tmp= get_ue_golomb_31(&s->gb);
  1918. if(tmp > 2){
  1919. av_log(s->avctx, AV_LOG_ERROR, "deblocking_filter_idc %u out of range\n", tmp);
  1920. return -1;
  1921. }
  1922. h->deblocking_filter= tmp;
  1923. if(h->deblocking_filter < 2)
  1924. h->deblocking_filter^= 1; // 1<->0
  1925. if( h->deblocking_filter ) {
  1926. h->slice_alpha_c0_offset += get_se_golomb(&s->gb) << 1;
  1927. h->slice_beta_offset += get_se_golomb(&s->gb) << 1;
  1928. if( h->slice_alpha_c0_offset > 104U
  1929. || h->slice_beta_offset > 104U){
  1930. av_log(s->avctx, AV_LOG_ERROR, "deblocking filter parameters %d %d out of range\n", h->slice_alpha_c0_offset, h->slice_beta_offset);
  1931. return -1;
  1932. }
  1933. }
  1934. }
  1935. if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
  1936. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type_nos != FF_I_TYPE)
  1937. ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type_nos == FF_B_TYPE)
  1938. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  1939. h->deblocking_filter= 0;
  1940. if(h->deblocking_filter == 1 && h0->max_contexts > 1) {
  1941. if(s->avctx->flags2 & CODEC_FLAG2_FAST) {
  1942. /* Cheat slightly for speed:
  1943. Do not bother to deblock across slices. */
  1944. h->deblocking_filter = 2;
  1945. } else {
  1946. h0->max_contexts = 1;
  1947. if(!h0->single_decode_warning) {
  1948. av_log(s->avctx, AV_LOG_INFO, "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
  1949. h0->single_decode_warning = 1;
  1950. }
  1951. if(h != h0){
  1952. av_log(h->s.avctx, AV_LOG_ERROR, "deblocking switched inside frame\n");
  1953. return 1;
  1954. }
  1955. }
  1956. }
  1957. h->qp_thresh= 15 + 52 - FFMIN(h->slice_alpha_c0_offset, h->slice_beta_offset) - FFMAX3(0, h->pps.chroma_qp_index_offset[0], h->pps.chroma_qp_index_offset[1]);
  1958. #if 0 //FMO
  1959. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  1960. slice_group_change_cycle= get_bits(&s->gb, ?);
  1961. #endif
  1962. h0->last_slice_type = slice_type;
  1963. h->slice_num = ++h0->current_slice;
  1964. if(h->slice_num >= MAX_SLICES){
  1965. av_log(s->avctx, AV_LOG_ERROR, "Too many slices, increase MAX_SLICES and recompile\n");
  1966. }
  1967. for(j=0; j<2; j++){
  1968. int id_list[16];
  1969. int *ref2frm= h->ref2frm[h->slice_num&(MAX_SLICES-1)][j];
  1970. for(i=0; i<16; i++){
  1971. id_list[i]= 60;
  1972. if(h->ref_list[j][i].data[0]){
  1973. int k;
  1974. uint8_t *base= h->ref_list[j][i].base[0];
  1975. for(k=0; k<h->short_ref_count; k++)
  1976. if(h->short_ref[k]->base[0] == base){
  1977. id_list[i]= k;
  1978. break;
  1979. }
  1980. for(k=0; k<h->long_ref_count; k++)
  1981. if(h->long_ref[k] && h->long_ref[k]->base[0] == base){
  1982. id_list[i]= h->short_ref_count + k;
  1983. break;
  1984. }
  1985. }
  1986. }
  1987. ref2frm[0]=
  1988. ref2frm[1]= -1;
  1989. for(i=0; i<16; i++)
  1990. ref2frm[i+2]= 4*id_list[i]
  1991. +(h->ref_list[j][i].reference&3);
  1992. ref2frm[18+0]=
  1993. ref2frm[18+1]= -1;
  1994. for(i=16; i<48; i++)
  1995. ref2frm[i+4]= 4*id_list[(i-16)>>1]
  1996. +(h->ref_list[j][i].reference&3);
  1997. }
  1998. //FIXME: fix draw_edges+PAFF+frame threads
  1999. h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE || (!h->sps.frame_mbs_only_flag && s->avctx->active_thread_type&FF_THREAD_FRAME)) ? 0 : 16;
  2000. h->emu_edge_height= (FRAME_MBAFF || FIELD_PICTURE) ? 0 : h->emu_edge_width;
  2001. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  2002. 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",
  2003. h->slice_num,
  2004. (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
  2005. first_mb_in_slice,
  2006. av_get_pict_type_char(h->slice_type), h->slice_type_fixed ? " fix" : "", h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
  2007. pps_id, h->frame_num,
  2008. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  2009. h->ref_count[0], h->ref_count[1],
  2010. s->qscale,
  2011. h->deblocking_filter, h->slice_alpha_c0_offset/2-26, h->slice_beta_offset/2-26,
  2012. h->use_weight,
  2013. h->use_weight==1 && h->use_weight_chroma ? "c" : "",
  2014. h->slice_type == FF_B_TYPE ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : ""
  2015. );
  2016. }
  2017. return 0;
  2018. }
  2019. int ff_h264_get_slice_type(const H264Context *h)
  2020. {
  2021. switch (h->slice_type) {
  2022. case FF_P_TYPE: return 0;
  2023. case FF_B_TYPE: return 1;
  2024. case FF_I_TYPE: return 2;
  2025. case FF_SP_TYPE: return 3;
  2026. case FF_SI_TYPE: return 4;
  2027. default: return -1;
  2028. }
  2029. }
  2030. /**
  2031. *
  2032. * @return non zero if the loop filter can be skiped
  2033. */
  2034. static int fill_filter_caches(H264Context *h, int mb_type){
  2035. MpegEncContext * const s = &h->s;
  2036. const int mb_xy= h->mb_xy;
  2037. int top_xy, left_xy[2];
  2038. int top_type, left_type[2];
  2039. top_xy = mb_xy - (s->mb_stride << MB_FIELD);
  2040. //FIXME deblocking could skip the intra and nnz parts.
  2041. /* Wow, what a mess, why didn't they simplify the interlacing & intra
  2042. * stuff, I can't imagine that these complex rules are worth it. */
  2043. left_xy[1] = left_xy[0] = mb_xy-1;
  2044. if(FRAME_MBAFF){
  2045. const int left_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]);
  2046. const int curr_mb_field_flag = IS_INTERLACED(mb_type);
  2047. if(s->mb_y&1){
  2048. if (left_mb_field_flag != curr_mb_field_flag) {
  2049. left_xy[0] -= s->mb_stride;
  2050. }
  2051. }else{
  2052. if(curr_mb_field_flag){
  2053. top_xy += s->mb_stride & (((s->current_picture.mb_type[top_xy ]>>7)&1)-1);
  2054. }
  2055. if (left_mb_field_flag != curr_mb_field_flag) {
  2056. left_xy[1] += s->mb_stride;
  2057. }
  2058. }
  2059. }
  2060. h->top_mb_xy = top_xy;
  2061. h->left_mb_xy[0] = left_xy[0];
  2062. h->left_mb_xy[1] = left_xy[1];
  2063. {
  2064. //for sufficiently low qp, filtering wouldn't do anything
  2065. //this is a conservative estimate: could also check beta_offset and more accurate chroma_qp
  2066. int qp_thresh = h->qp_thresh; //FIXME strictly we should store qp_thresh for each mb of a slice
  2067. int qp = s->current_picture.qscale_table[mb_xy];
  2068. if(qp <= qp_thresh
  2069. && (left_xy[0]<0 || ((qp + s->current_picture.qscale_table[left_xy[0]] + 1)>>1) <= qp_thresh)
  2070. && (top_xy < 0 || ((qp + s->current_picture.qscale_table[top_xy ] + 1)>>1) <= qp_thresh)){
  2071. if(!FRAME_MBAFF)
  2072. return 1;
  2073. if( (left_xy[0]< 0 || ((qp + s->current_picture.qscale_table[left_xy[1] ] + 1)>>1) <= qp_thresh)
  2074. && (top_xy < s->mb_stride || ((qp + s->current_picture.qscale_table[top_xy -s->mb_stride] + 1)>>1) <= qp_thresh))
  2075. return 1;
  2076. }
  2077. }
  2078. top_type = s->current_picture.mb_type[top_xy] ;
  2079. left_type[0] = s->current_picture.mb_type[left_xy[0]];
  2080. left_type[1] = s->current_picture.mb_type[left_xy[1]];
  2081. if(h->deblocking_filter == 2){
  2082. if(h->slice_table[top_xy ] != h->slice_num) top_type= 0;
  2083. if(h->slice_table[left_xy[0] ] != h->slice_num) left_type[0]= left_type[1]= 0;
  2084. }else{
  2085. if(h->slice_table[top_xy ] == 0xFFFF) top_type= 0;
  2086. if(h->slice_table[left_xy[0] ] == 0xFFFF) left_type[0]= left_type[1] =0;
  2087. }
  2088. h->top_type = top_type ;
  2089. h->left_type[0]= left_type[0];
  2090. h->left_type[1]= left_type[1];
  2091. if(IS_INTRA(mb_type))
  2092. return 0;
  2093. AV_COPY64(&h->non_zero_count_cache[0+8*1], &h->non_zero_count[mb_xy][ 0]);
  2094. AV_COPY64(&h->non_zero_count_cache[0+8*2], &h->non_zero_count[mb_xy][ 8]);
  2095. AV_COPY32(&h->non_zero_count_cache[0+8*5], &h->non_zero_count[mb_xy][16]);
  2096. AV_COPY32(&h->non_zero_count_cache[4+8*3], &h->non_zero_count[mb_xy][20]);
  2097. AV_COPY64(&h->non_zero_count_cache[0+8*4], &h->non_zero_count[mb_xy][24]);
  2098. h->cbp= h->cbp_table[mb_xy];
  2099. {
  2100. int list;
  2101. for(list=0; list<h->list_count; list++){
  2102. int8_t *ref;
  2103. int y, b_stride;
  2104. int16_t (*mv_dst)[2];
  2105. int16_t (*mv_src)[2];
  2106. if(!USES_LIST(mb_type, list)){
  2107. fill_rectangle( h->mv_cache[list][scan8[0]], 4, 4, 8, pack16to32(0,0), 4);
  2108. AV_WN32A(&h->ref_cache[list][scan8[ 0]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2109. AV_WN32A(&h->ref_cache[list][scan8[ 2]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2110. AV_WN32A(&h->ref_cache[list][scan8[ 8]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2111. AV_WN32A(&h->ref_cache[list][scan8[10]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2112. continue;
  2113. }
  2114. ref = &s->current_picture.ref_index[list][4*mb_xy];
  2115. {
  2116. int (*ref2frm)[64] = h->ref2frm[ h->slice_num&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
  2117. AV_WN32A(&h->ref_cache[list][scan8[ 0]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
  2118. AV_WN32A(&h->ref_cache[list][scan8[ 2]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
  2119. ref += 2;
  2120. AV_WN32A(&h->ref_cache[list][scan8[ 8]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
  2121. AV_WN32A(&h->ref_cache[list][scan8[10]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
  2122. }
  2123. b_stride = h->b_stride;
  2124. mv_dst = &h->mv_cache[list][scan8[0]];
  2125. mv_src = &s->current_picture.motion_val[list][4*s->mb_x + 4*s->mb_y*b_stride];
  2126. for(y=0; y<4; y++){
  2127. AV_COPY128(mv_dst + 8*y, mv_src + y*b_stride);
  2128. }
  2129. }
  2130. }
  2131. /*
  2132. 0 . T T. T T T T
  2133. 1 L . .L . . . .
  2134. 2 L . .L . . . .
  2135. 3 . T TL . . . .
  2136. 4 L . .L . . . .
  2137. 5 L . .. . . . .
  2138. */
  2139. //FIXME constraint_intra_pred & partitioning & nnz (let us hope this is just a typo in the spec)
  2140. if(top_type){
  2141. AV_COPY32(&h->non_zero_count_cache[4+8*0], &h->non_zero_count[top_xy][4+3*8]);
  2142. }
  2143. if(left_type[0]){
  2144. h->non_zero_count_cache[3+8*1]= h->non_zero_count[left_xy[0]][7+0*8];
  2145. h->non_zero_count_cache[3+8*2]= h->non_zero_count[left_xy[0]][7+1*8];
  2146. h->non_zero_count_cache[3+8*3]= h->non_zero_count[left_xy[0]][7+2*8];
  2147. h->non_zero_count_cache[3+8*4]= h->non_zero_count[left_xy[0]][7+3*8];
  2148. }
  2149. // CAVLC 8x8dct requires NNZ values for residual decoding that differ from what the loop filter needs
  2150. if(!CABAC && h->pps.transform_8x8_mode){
  2151. if(IS_8x8DCT(top_type)){
  2152. h->non_zero_count_cache[4+8*0]=
  2153. h->non_zero_count_cache[5+8*0]= h->cbp_table[top_xy] & 4;
  2154. h->non_zero_count_cache[6+8*0]=
  2155. h->non_zero_count_cache[7+8*0]= h->cbp_table[top_xy] & 8;
  2156. }
  2157. if(IS_8x8DCT(left_type[0])){
  2158. h->non_zero_count_cache[3+8*1]=
  2159. h->non_zero_count_cache[3+8*2]= h->cbp_table[left_xy[0]]&2; //FIXME check MBAFF
  2160. }
  2161. if(IS_8x8DCT(left_type[1])){
  2162. h->non_zero_count_cache[3+8*3]=
  2163. h->non_zero_count_cache[3+8*4]= h->cbp_table[left_xy[1]]&8; //FIXME check MBAFF
  2164. }
  2165. if(IS_8x8DCT(mb_type)){
  2166. h->non_zero_count_cache[scan8[0 ]]= h->non_zero_count_cache[scan8[1 ]]=
  2167. h->non_zero_count_cache[scan8[2 ]]= h->non_zero_count_cache[scan8[3 ]]= h->cbp & 1;
  2168. h->non_zero_count_cache[scan8[0+ 4]]= h->non_zero_count_cache[scan8[1+ 4]]=
  2169. h->non_zero_count_cache[scan8[2+ 4]]= h->non_zero_count_cache[scan8[3+ 4]]= h->cbp & 2;
  2170. h->non_zero_count_cache[scan8[0+ 8]]= h->non_zero_count_cache[scan8[1+ 8]]=
  2171. h->non_zero_count_cache[scan8[2+ 8]]= h->non_zero_count_cache[scan8[3+ 8]]= h->cbp & 4;
  2172. h->non_zero_count_cache[scan8[0+12]]= h->non_zero_count_cache[scan8[1+12]]=
  2173. h->non_zero_count_cache[scan8[2+12]]= h->non_zero_count_cache[scan8[3+12]]= h->cbp & 8;
  2174. }
  2175. }
  2176. if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
  2177. int list;
  2178. for(list=0; list<h->list_count; list++){
  2179. if(USES_LIST(top_type, list)){
  2180. const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
  2181. const int b8_xy= 4*top_xy + 2;
  2182. int (*ref2frm)[64] = h->ref2frm[ h->slice_table[top_xy]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
  2183. AV_COPY128(h->mv_cache[list][scan8[0] + 0 - 1*8], s->current_picture.motion_val[list][b_xy + 0]);
  2184. h->ref_cache[list][scan8[0] + 0 - 1*8]=
  2185. h->ref_cache[list][scan8[0] + 1 - 1*8]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 0]];
  2186. h->ref_cache[list][scan8[0] + 2 - 1*8]=
  2187. h->ref_cache[list][scan8[0] + 3 - 1*8]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 1]];
  2188. }else{
  2189. AV_ZERO128(h->mv_cache[list][scan8[0] + 0 - 1*8]);
  2190. AV_WN32A(&h->ref_cache[list][scan8[0] + 0 - 1*8], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2191. }
  2192. if(!IS_INTERLACED(mb_type^left_type[0])){
  2193. if(USES_LIST(left_type[0], list)){
  2194. const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
  2195. const int b8_xy= 4*left_xy[0] + 1;
  2196. int (*ref2frm)[64] = h->ref2frm[ h->slice_table[left_xy[0]]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
  2197. AV_COPY32(h->mv_cache[list][scan8[0] - 1 + 0 ], s->current_picture.motion_val[list][b_xy + h->b_stride*0]);
  2198. AV_COPY32(h->mv_cache[list][scan8[0] - 1 + 8 ], s->current_picture.motion_val[list][b_xy + h->b_stride*1]);
  2199. AV_COPY32(h->mv_cache[list][scan8[0] - 1 +16 ], s->current_picture.motion_val[list][b_xy + h->b_stride*2]);
  2200. AV_COPY32(h->mv_cache[list][scan8[0] - 1 +24 ], s->current_picture.motion_val[list][b_xy + h->b_stride*3]);
  2201. h->ref_cache[list][scan8[0] - 1 + 0 ]=
  2202. h->ref_cache[list][scan8[0] - 1 + 8 ]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 2*0]];
  2203. h->ref_cache[list][scan8[0] - 1 +16 ]=
  2204. h->ref_cache[list][scan8[0] - 1 +24 ]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 2*1]];
  2205. }else{
  2206. AV_ZERO32(h->mv_cache [list][scan8[0] - 1 + 0 ]);
  2207. AV_ZERO32(h->mv_cache [list][scan8[0] - 1 + 8 ]);
  2208. AV_ZERO32(h->mv_cache [list][scan8[0] - 1 +16 ]);
  2209. AV_ZERO32(h->mv_cache [list][scan8[0] - 1 +24 ]);
  2210. h->ref_cache[list][scan8[0] - 1 + 0 ]=
  2211. h->ref_cache[list][scan8[0] - 1 + 8 ]=
  2212. h->ref_cache[list][scan8[0] - 1 + 16 ]=
  2213. h->ref_cache[list][scan8[0] - 1 + 24 ]= LIST_NOT_USED;
  2214. }
  2215. }
  2216. }
  2217. }
  2218. return 0;
  2219. }
  2220. static void loop_filter(H264Context *h){
  2221. MpegEncContext * const s = &h->s;
  2222. uint8_t *dest_y, *dest_cb, *dest_cr;
  2223. int linesize, uvlinesize, mb_x, mb_y;
  2224. const int end_mb_y= s->mb_y + FRAME_MBAFF;
  2225. const int old_slice_type= h->slice_type;
  2226. const int end_mb_x = s->mb_x;
  2227. if(h->deblocking_filter) {
  2228. int start_x= s->resync_mb_y == s->mb_y ? s->resync_mb_x : 0;
  2229. for(mb_x= start_x; mb_x<end_mb_x; mb_x++){
  2230. for(mb_y=end_mb_y - FRAME_MBAFF; mb_y<= end_mb_y; mb_y++){
  2231. int mb_xy, mb_type;
  2232. mb_xy = h->mb_xy = mb_x + mb_y*s->mb_stride;
  2233. h->slice_num= h->slice_table[mb_xy];
  2234. mb_type= s->current_picture.mb_type[mb_xy];
  2235. h->list_count= h->list_counts[mb_xy];
  2236. if(FRAME_MBAFF)
  2237. h->mb_mbaff = h->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
  2238. s->mb_x= mb_x;
  2239. s->mb_y= mb_y;
  2240. dest_y = s->current_picture.data[0] + ((mb_x<<h->pixel_shift) + mb_y * s->linesize ) * 16;
  2241. dest_cb = s->current_picture.data[1] + ((mb_x<<h->pixel_shift) + mb_y * s->uvlinesize) * 8;
  2242. dest_cr = s->current_picture.data[2] + ((mb_x<<h->pixel_shift) + mb_y * s->uvlinesize) * 8;
  2243. //FIXME simplify above
  2244. if (MB_FIELD) {
  2245. linesize = h->mb_linesize = s->linesize * 2;
  2246. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  2247. if(mb_y&1){ //FIXME move out of this function?
  2248. dest_y -= s->linesize*15;
  2249. dest_cb-= s->uvlinesize*7;
  2250. dest_cr-= s->uvlinesize*7;
  2251. }
  2252. } else {
  2253. linesize = h->mb_linesize = s->linesize;
  2254. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  2255. }
  2256. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
  2257. if(fill_filter_caches(h, mb_type))
  2258. continue;
  2259. h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy]);
  2260. h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy]);
  2261. if (FRAME_MBAFF) {
  2262. ff_h264_filter_mb (h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  2263. } else {
  2264. ff_h264_filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  2265. }
  2266. }
  2267. }
  2268. }
  2269. h->slice_type= old_slice_type;
  2270. s->mb_x= end_mb_x;
  2271. s->mb_y= end_mb_y - FRAME_MBAFF;
  2272. h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
  2273. h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
  2274. }
  2275. static void predict_field_decoding_flag(H264Context *h){
  2276. MpegEncContext * const s = &h->s;
  2277. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2278. int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
  2279. ? s->current_picture.mb_type[mb_xy-1]
  2280. : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
  2281. ? s->current_picture.mb_type[mb_xy-s->mb_stride]
  2282. : 0;
  2283. h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  2284. }
  2285. /**
  2286. * Draw edges and report progress for the last MB row.
  2287. */
  2288. static void decode_finish_row(H264Context *h){
  2289. MpegEncContext * const s = &h->s;
  2290. int top = 16*(s->mb_y >> FIELD_PICTURE);
  2291. int height = 16 << FRAME_MBAFF;
  2292. int deblock_border = (16 + 4) << FRAME_MBAFF;
  2293. int pic_height = 16*s->mb_height >> FIELD_PICTURE;
  2294. if (h->deblocking_filter) {
  2295. if((top + height) >= pic_height)
  2296. height += deblock_border;
  2297. top -= deblock_border;
  2298. }
  2299. if (top >= pic_height || (top + height) < h->emu_edge_height)
  2300. return;
  2301. height = FFMIN(height, pic_height - top);
  2302. if (top < h->emu_edge_height) {
  2303. height = top+height;
  2304. top = 0;
  2305. }
  2306. ff_draw_horiz_band(s, top, height);
  2307. if (s->dropable) return;
  2308. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, top + height - 1,
  2309. s->picture_structure==PICT_BOTTOM_FIELD);
  2310. }
  2311. static int decode_slice(struct AVCodecContext *avctx, void *arg){
  2312. H264Context *h = *(void**)arg;
  2313. MpegEncContext * const s = &h->s;
  2314. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  2315. s->mb_skip_run= -1;
  2316. h->is_complex = FRAME_MBAFF || s->picture_structure != PICT_FRAME || s->codec_id != CODEC_ID_H264 ||
  2317. (CONFIG_GRAY && (s->flags&CODEC_FLAG_GRAY));
  2318. if( h->pps.cabac ) {
  2319. /* realign */
  2320. align_get_bits( &s->gb );
  2321. /* init cabac */
  2322. ff_init_cabac_states( &h->cabac);
  2323. ff_init_cabac_decoder( &h->cabac,
  2324. s->gb.buffer + get_bits_count(&s->gb)/8,
  2325. (get_bits_left(&s->gb) + 7)/8);
  2326. ff_h264_init_cabac_states(h);
  2327. for(;;){
  2328. //START_TIMER
  2329. int ret = ff_h264_decode_mb_cabac(h);
  2330. int eos;
  2331. //STOP_TIMER("decode_mb_cabac")
  2332. if(ret>=0) ff_h264_hl_decode_mb(h);
  2333. if( ret >= 0 && FRAME_MBAFF ) { //FIXME optimal? or let mb_decode decode 16x32 ?
  2334. s->mb_y++;
  2335. ret = ff_h264_decode_mb_cabac(h);
  2336. if(ret>=0) ff_h264_hl_decode_mb(h);
  2337. s->mb_y--;
  2338. }
  2339. eos = get_cabac_terminate( &h->cabac );
  2340. if((s->workaround_bugs & FF_BUG_TRUNCATED) && h->cabac.bytestream > h->cabac.bytestream_end + 2){
  2341. 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);
  2342. return 0;
  2343. }
  2344. if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
  2345. 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);
  2346. 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);
  2347. return -1;
  2348. }
  2349. if( ++s->mb_x >= s->mb_width ) {
  2350. loop_filter(h);
  2351. s->mb_x = 0;
  2352. decode_finish_row(h);
  2353. ++s->mb_y;
  2354. if(FIELD_OR_MBAFF_PICTURE) {
  2355. ++s->mb_y;
  2356. if(FRAME_MBAFF && s->mb_y < s->mb_height)
  2357. predict_field_decoding_flag(h);
  2358. }
  2359. }
  2360. if( eos || s->mb_y >= s->mb_height ) {
  2361. if(s->mb_x)
  2362. loop_filter(h);
  2363. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  2364. 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);
  2365. return 0;
  2366. }
  2367. }
  2368. } else {
  2369. for(;;){
  2370. int ret = ff_h264_decode_mb_cavlc(h);
  2371. if(ret>=0) ff_h264_hl_decode_mb(h);
  2372. if(ret>=0 && FRAME_MBAFF){ //FIXME optimal? or let mb_decode decode 16x32 ?
  2373. s->mb_y++;
  2374. ret = ff_h264_decode_mb_cavlc(h);
  2375. if(ret>=0) ff_h264_hl_decode_mb(h);
  2376. s->mb_y--;
  2377. }
  2378. if(ret<0){
  2379. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  2380. 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);
  2381. return -1;
  2382. }
  2383. if(++s->mb_x >= s->mb_width){
  2384. loop_filter(h);
  2385. s->mb_x=0;
  2386. decode_finish_row(h);
  2387. ++s->mb_y;
  2388. if(FIELD_OR_MBAFF_PICTURE) {
  2389. ++s->mb_y;
  2390. if(FRAME_MBAFF && s->mb_y < s->mb_height)
  2391. predict_field_decoding_flag(h);
  2392. }
  2393. if(s->mb_y >= s->mb_height){
  2394. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  2395. if( get_bits_count(&s->gb) == s->gb.size_in_bits
  2396. || get_bits_count(&s->gb) < s->gb.size_in_bits && s->avctx->error_recognition < FF_ER_AGGRESSIVE) {
  2397. 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);
  2398. return 0;
  2399. }else{
  2400. 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);
  2401. return -1;
  2402. }
  2403. }
  2404. }
  2405. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  2406. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  2407. if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
  2408. if(s->mb_x)
  2409. loop_filter(h);
  2410. 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);
  2411. return 0;
  2412. }else{
  2413. 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);
  2414. return -1;
  2415. }
  2416. }
  2417. }
  2418. }
  2419. #if 0
  2420. for(;s->mb_y < s->mb_height; s->mb_y++){
  2421. for(;s->mb_x < s->mb_width; s->mb_x++){
  2422. int ret= decode_mb(h);
  2423. ff_h264_hl_decode_mb(h);
  2424. if(ret<0){
  2425. av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  2426. 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);
  2427. return -1;
  2428. }
  2429. if(++s->mb_x >= s->mb_width){
  2430. s->mb_x=0;
  2431. if(++s->mb_y >= s->mb_height){
  2432. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  2433. 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);
  2434. return 0;
  2435. }else{
  2436. 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);
  2437. return -1;
  2438. }
  2439. }
  2440. }
  2441. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  2442. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  2443. 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);
  2444. return 0;
  2445. }else{
  2446. 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);
  2447. return -1;
  2448. }
  2449. }
  2450. }
  2451. s->mb_x=0;
  2452. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  2453. }
  2454. #endif
  2455. return -1; //not reached
  2456. }
  2457. /**
  2458. * Call decode_slice() for each context.
  2459. *
  2460. * @param h h264 master context
  2461. * @param context_count number of contexts to execute
  2462. */
  2463. static void execute_decode_slices(H264Context *h, int context_count){
  2464. MpegEncContext * const s = &h->s;
  2465. AVCodecContext * const avctx= s->avctx;
  2466. H264Context *hx;
  2467. int i;
  2468. if (s->avctx->hwaccel)
  2469. return;
  2470. if(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  2471. return;
  2472. if(context_count == 1) {
  2473. decode_slice(avctx, &h);
  2474. } else {
  2475. for(i = 1; i < context_count; i++) {
  2476. hx = h->thread_context[i];
  2477. hx->s.error_recognition = avctx->error_recognition;
  2478. hx->s.error_count = 0;
  2479. hx->x264_build= h->x264_build;
  2480. }
  2481. avctx->execute(avctx, (void *)decode_slice,
  2482. h->thread_context, NULL, context_count, sizeof(void*));
  2483. /* pull back stuff from slices to master context */
  2484. hx = h->thread_context[context_count - 1];
  2485. s->mb_x = hx->s.mb_x;
  2486. s->mb_y = hx->s.mb_y;
  2487. s->dropable = hx->s.dropable;
  2488. s->picture_structure = hx->s.picture_structure;
  2489. for(i = 1; i < context_count; i++)
  2490. h->s.error_count += h->thread_context[i]->s.error_count;
  2491. }
  2492. }
  2493. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size){
  2494. MpegEncContext * const s = &h->s;
  2495. AVCodecContext * const avctx= s->avctx;
  2496. int buf_index=0;
  2497. H264Context *hx; ///< thread context
  2498. int context_count = 0;
  2499. int next_avc= h->is_avc ? 0 : buf_size;
  2500. h->max_contexts = (HAVE_THREADS && (s->avctx->active_thread_type&FF_THREAD_SLICE)) ? avctx->thread_count : 1;
  2501. #if 0
  2502. int i;
  2503. for(i=0; i<50; i++){
  2504. av_log(NULL, AV_LOG_ERROR,"%02X ", buf[i]);
  2505. }
  2506. #endif
  2507. if(!(s->flags2 & CODEC_FLAG2_CHUNKS)){
  2508. h->current_slice = 0;
  2509. if (!s->first_field)
  2510. s->current_picture_ptr= NULL;
  2511. ff_h264_reset_sei(h);
  2512. }
  2513. for(;;){
  2514. int consumed;
  2515. int dst_length;
  2516. int bit_length;
  2517. const uint8_t *ptr;
  2518. int i, nalsize = 0;
  2519. int err;
  2520. if(buf_index >= next_avc) {
  2521. if(buf_index >= buf_size) break;
  2522. nalsize = 0;
  2523. for(i = 0; i < h->nal_length_size; i++)
  2524. nalsize = (nalsize << 8) | buf[buf_index++];
  2525. if(nalsize <= 0 || nalsize > buf_size - buf_index){
  2526. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  2527. break;
  2528. }
  2529. next_avc= buf_index + nalsize;
  2530. } else {
  2531. // start code prefix search
  2532. for(; buf_index + 3 < next_avc; buf_index++){
  2533. // This should always succeed in the first iteration.
  2534. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  2535. break;
  2536. }
  2537. if(buf_index+3 >= buf_size) break;
  2538. buf_index+=3;
  2539. if(buf_index >= next_avc) continue;
  2540. }
  2541. hx = h->thread_context[context_count];
  2542. ptr= ff_h264_decode_nal(hx, buf + buf_index, &dst_length, &consumed, next_avc - buf_index);
  2543. if (ptr==NULL || dst_length < 0){
  2544. return -1;
  2545. }
  2546. i= buf_index + consumed;
  2547. if((s->workaround_bugs & FF_BUG_AUTODETECT) && i+3<next_avc &&
  2548. buf[i]==0x00 && buf[i+1]==0x00 && buf[i+2]==0x01 && buf[i+3]==0xE0)
  2549. s->workaround_bugs |= FF_BUG_TRUNCATED;
  2550. if(!(s->workaround_bugs & FF_BUG_TRUNCATED)){
  2551. while(ptr[dst_length - 1] == 0 && dst_length > 0)
  2552. dst_length--;
  2553. }
  2554. bit_length= !dst_length ? 0 : (8*dst_length - ff_h264_decode_rbsp_trailing(h, ptr + dst_length - 1));
  2555. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  2556. 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);
  2557. }
  2558. if (h->is_avc && (nalsize != consumed) && nalsize){
  2559. av_log(h->s.avctx, AV_LOG_DEBUG, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
  2560. }
  2561. buf_index += consumed;
  2562. //FIXME do not discard SEI id
  2563. if(
  2564. #if FF_API_HURRY_UP
  2565. (s->hurry_up == 1 && h->nal_ref_idc == 0) ||
  2566. #endif
  2567. (avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  2568. continue;
  2569. again:
  2570. err = 0;
  2571. switch(hx->nal_unit_type){
  2572. case NAL_IDR_SLICE:
  2573. if (h->nal_unit_type != NAL_IDR_SLICE) {
  2574. av_log(h->s.avctx, AV_LOG_ERROR, "Invalid mix of idr and non-idr slices");
  2575. return -1;
  2576. }
  2577. idr(h); //FIXME ensure we don't loose some frames if there is reordering
  2578. case NAL_SLICE:
  2579. init_get_bits(&hx->s.gb, ptr, bit_length);
  2580. hx->intra_gb_ptr=
  2581. hx->inter_gb_ptr= &hx->s.gb;
  2582. hx->s.data_partitioning = 0;
  2583. if((err = decode_slice_header(hx, h)))
  2584. break;
  2585. s->current_picture_ptr->key_frame |=
  2586. (hx->nal_unit_type == NAL_IDR_SLICE) ||
  2587. (h->sei_recovery_frame_cnt >= 0);
  2588. if (h->current_slice == 1) {
  2589. if(!(s->flags2 & CODEC_FLAG2_CHUNKS)) {
  2590. decode_postinit(h);
  2591. }
  2592. if (s->avctx->hwaccel && s->avctx->hwaccel->start_frame(s->avctx, NULL, 0) < 0)
  2593. return -1;
  2594. if(CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  2595. ff_vdpau_h264_picture_start(s);
  2596. }
  2597. if(hx->redundant_pic_count==0
  2598. #if FF_API_HURRY_UP
  2599. && hx->s.hurry_up < 5
  2600. #endif
  2601. && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
  2602. && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=FF_B_TYPE)
  2603. && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==FF_I_TYPE)
  2604. && avctx->skip_frame < AVDISCARD_ALL){
  2605. if(avctx->hwaccel) {
  2606. if (avctx->hwaccel->decode_slice(avctx, &buf[buf_index - consumed], consumed) < 0)
  2607. return -1;
  2608. }else
  2609. if(CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
  2610. static const uint8_t start_code[] = {0x00, 0x00, 0x01};
  2611. ff_vdpau_add_data_chunk(s, start_code, sizeof(start_code));
  2612. ff_vdpau_add_data_chunk(s, &buf[buf_index - consumed], consumed );
  2613. }else
  2614. context_count++;
  2615. }
  2616. break;
  2617. case NAL_DPA:
  2618. init_get_bits(&hx->s.gb, ptr, bit_length);
  2619. hx->intra_gb_ptr=
  2620. hx->inter_gb_ptr= NULL;
  2621. if ((err = decode_slice_header(hx, h)) < 0)
  2622. break;
  2623. hx->s.data_partitioning = 1;
  2624. break;
  2625. case NAL_DPB:
  2626. init_get_bits(&hx->intra_gb, ptr, bit_length);
  2627. hx->intra_gb_ptr= &hx->intra_gb;
  2628. break;
  2629. case NAL_DPC:
  2630. init_get_bits(&hx->inter_gb, ptr, bit_length);
  2631. hx->inter_gb_ptr= &hx->inter_gb;
  2632. if(hx->redundant_pic_count==0 && hx->intra_gb_ptr && hx->s.data_partitioning
  2633. && s->context_initialized
  2634. #if FF_API_HURRY_UP
  2635. && s->hurry_up < 5
  2636. #endif
  2637. && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
  2638. && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=FF_B_TYPE)
  2639. && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==FF_I_TYPE)
  2640. && avctx->skip_frame < AVDISCARD_ALL)
  2641. context_count++;
  2642. break;
  2643. case NAL_SEI:
  2644. init_get_bits(&s->gb, ptr, bit_length);
  2645. ff_h264_decode_sei(h);
  2646. break;
  2647. case NAL_SPS:
  2648. init_get_bits(&s->gb, ptr, bit_length);
  2649. ff_h264_decode_seq_parameter_set(h);
  2650. if(s->flags& CODEC_FLAG_LOW_DELAY)
  2651. s->low_delay=1;
  2652. if(avctx->has_b_frames < 2)
  2653. avctx->has_b_frames= !s->low_delay;
  2654. if (avctx->bits_per_raw_sample != h->sps.bit_depth_luma) {
  2655. if (h->sps.bit_depth_luma >= 8 && h->sps.bit_depth_luma <= 10) {
  2656. avctx->bits_per_raw_sample = h->sps.bit_depth_luma;
  2657. h->pixel_shift = h->sps.bit_depth_luma/9;
  2658. ff_h264dsp_init(&h->h264dsp, h->sps.bit_depth_luma);
  2659. ff_h264_pred_init(&h->hpc, s->codec_id, h->sps.bit_depth_luma);
  2660. dsputil_init(&s->dsp, s->avctx);
  2661. } else {
  2662. av_log(avctx, AV_LOG_DEBUG, "Unsupported bit depth: %d\n", h->sps.bit_depth_luma);
  2663. return -1;
  2664. }
  2665. }
  2666. break;
  2667. case NAL_PPS:
  2668. init_get_bits(&s->gb, ptr, bit_length);
  2669. ff_h264_decode_picture_parameter_set(h, bit_length);
  2670. break;
  2671. case NAL_AUD:
  2672. case NAL_END_SEQUENCE:
  2673. case NAL_END_STREAM:
  2674. case NAL_FILLER_DATA:
  2675. case NAL_SPS_EXT:
  2676. case NAL_AUXILIARY_SLICE:
  2677. break;
  2678. default:
  2679. av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n", hx->nal_unit_type, bit_length);
  2680. }
  2681. if(context_count == h->max_contexts) {
  2682. execute_decode_slices(h, context_count);
  2683. context_count = 0;
  2684. }
  2685. if (err < 0)
  2686. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  2687. else if(err == 1) {
  2688. /* Slice could not be decoded in parallel mode, copy down
  2689. * NAL unit stuff to context 0 and restart. Note that
  2690. * rbsp_buffer is not transferred, but since we no longer
  2691. * run in parallel mode this should not be an issue. */
  2692. h->nal_unit_type = hx->nal_unit_type;
  2693. h->nal_ref_idc = hx->nal_ref_idc;
  2694. hx = h;
  2695. goto again;
  2696. }
  2697. }
  2698. if(context_count)
  2699. execute_decode_slices(h, context_count);
  2700. return buf_index;
  2701. }
  2702. /**
  2703. * returns the number of bytes consumed for building the current frame
  2704. */
  2705. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  2706. if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
  2707. if(pos+10>buf_size) pos=buf_size; // oops ;)
  2708. return pos;
  2709. }
  2710. static int decode_frame(AVCodecContext *avctx,
  2711. void *data, int *data_size,
  2712. AVPacket *avpkt)
  2713. {
  2714. const uint8_t *buf = avpkt->data;
  2715. int buf_size = avpkt->size;
  2716. H264Context *h = avctx->priv_data;
  2717. MpegEncContext *s = &h->s;
  2718. AVFrame *pict = data;
  2719. int buf_index;
  2720. s->flags= avctx->flags;
  2721. s->flags2= avctx->flags2;
  2722. /* end of stream, output what is still in the buffers */
  2723. out:
  2724. if (buf_size == 0) {
  2725. Picture *out;
  2726. int i, out_idx;
  2727. s->current_picture_ptr = NULL;
  2728. //FIXME factorize this with the output code below
  2729. out = h->delayed_pic[0];
  2730. out_idx = 0;
  2731. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++)
  2732. if(h->delayed_pic[i]->poc < out->poc){
  2733. out = h->delayed_pic[i];
  2734. out_idx = i;
  2735. }
  2736. for(i=out_idx; h->delayed_pic[i]; i++)
  2737. h->delayed_pic[i] = h->delayed_pic[i+1];
  2738. if(out){
  2739. *data_size = sizeof(AVFrame);
  2740. *pict= *(AVFrame*)out;
  2741. }
  2742. return 0;
  2743. }
  2744. buf_index=decode_nal_units(h, buf, buf_size);
  2745. if(buf_index < 0)
  2746. return -1;
  2747. if (!s->current_picture_ptr && h->nal_unit_type == NAL_END_SEQUENCE) {
  2748. buf_size = 0;
  2749. goto out;
  2750. }
  2751. if(!(s->flags2 & CODEC_FLAG2_CHUNKS) && !s->current_picture_ptr){
  2752. if (avctx->skip_frame >= AVDISCARD_NONREF
  2753. #if FF_API_HURRY_UP
  2754. || s->hurry_up
  2755. #endif
  2756. )
  2757. return 0;
  2758. av_log(avctx, AV_LOG_ERROR, "no frame!\n");
  2759. return -1;
  2760. }
  2761. if(!(s->flags2 & CODEC_FLAG2_CHUNKS) || (s->mb_y >= s->mb_height && s->mb_height)){
  2762. if(s->flags2 & CODEC_FLAG2_CHUNKS) decode_postinit(h);
  2763. field_end(h, 0);
  2764. if (!h->next_output_pic) {
  2765. /* Wait for second field. */
  2766. *data_size = 0;
  2767. } else {
  2768. *data_size = sizeof(AVFrame);
  2769. *pict = *(AVFrame*)h->next_output_pic;
  2770. }
  2771. }
  2772. assert(pict->data[0] || !*data_size);
  2773. ff_print_debug_info(s, pict);
  2774. //printf("out %d\n", (int)pict->data[0]);
  2775. return get_consumed_bytes(s, buf_index, buf_size);
  2776. }
  2777. #if 0
  2778. static inline void fill_mb_avail(H264Context *h){
  2779. MpegEncContext * const s = &h->s;
  2780. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2781. if(s->mb_y){
  2782. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  2783. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  2784. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  2785. }else{
  2786. h->mb_avail[0]=
  2787. h->mb_avail[1]=
  2788. h->mb_avail[2]= 0;
  2789. }
  2790. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  2791. h->mb_avail[4]= 1; //FIXME move out
  2792. h->mb_avail[5]= 0; //FIXME move out
  2793. }
  2794. #endif
  2795. #ifdef TEST
  2796. #undef printf
  2797. #undef random
  2798. #define COUNT 8000
  2799. #define SIZE (COUNT*40)
  2800. int main(void){
  2801. int i;
  2802. uint8_t temp[SIZE];
  2803. PutBitContext pb;
  2804. GetBitContext gb;
  2805. // int int_temp[10000];
  2806. DSPContext dsp;
  2807. AVCodecContext avctx;
  2808. dsputil_init(&dsp, &avctx);
  2809. init_put_bits(&pb, temp, SIZE);
  2810. printf("testing unsigned exp golomb\n");
  2811. for(i=0; i<COUNT; i++){
  2812. START_TIMER
  2813. set_ue_golomb(&pb, i);
  2814. STOP_TIMER("set_ue_golomb");
  2815. }
  2816. flush_put_bits(&pb);
  2817. init_get_bits(&gb, temp, 8*SIZE);
  2818. for(i=0; i<COUNT; i++){
  2819. int j, s;
  2820. s= show_bits(&gb, 24);
  2821. START_TIMER
  2822. j= get_ue_golomb(&gb);
  2823. if(j != i){
  2824. printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  2825. // return -1;
  2826. }
  2827. STOP_TIMER("get_ue_golomb");
  2828. }
  2829. init_put_bits(&pb, temp, SIZE);
  2830. printf("testing signed exp golomb\n");
  2831. for(i=0; i<COUNT; i++){
  2832. START_TIMER
  2833. set_se_golomb(&pb, i - COUNT/2);
  2834. STOP_TIMER("set_se_golomb");
  2835. }
  2836. flush_put_bits(&pb);
  2837. init_get_bits(&gb, temp, 8*SIZE);
  2838. for(i=0; i<COUNT; i++){
  2839. int j, s;
  2840. s= show_bits(&gb, 24);
  2841. START_TIMER
  2842. j= get_se_golomb(&gb);
  2843. if(j != i - COUNT/2){
  2844. printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  2845. // return -1;
  2846. }
  2847. STOP_TIMER("get_se_golomb");
  2848. }
  2849. #if 0
  2850. printf("testing 4x4 (I)DCT\n");
  2851. DCTELEM block[16];
  2852. uint8_t src[16], ref[16];
  2853. uint64_t error= 0, max_error=0;
  2854. for(i=0; i<COUNT; i++){
  2855. int j;
  2856. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  2857. for(j=0; j<16; j++){
  2858. ref[j]= random()%255;
  2859. src[j]= random()%255;
  2860. }
  2861. h264_diff_dct_c(block, src, ref, 4);
  2862. //normalize
  2863. for(j=0; j<16; j++){
  2864. // printf("%d ", block[j]);
  2865. block[j]= block[j]*4;
  2866. if(j&1) block[j]= (block[j]*4 + 2)/5;
  2867. if(j&4) block[j]= (block[j]*4 + 2)/5;
  2868. }
  2869. // printf("\n");
  2870. h->h264dsp.h264_idct_add(ref, block, 4);
  2871. /* for(j=0; j<16; j++){
  2872. printf("%d ", ref[j]);
  2873. }
  2874. printf("\n");*/
  2875. for(j=0; j<16; j++){
  2876. int diff= FFABS(src[j] - ref[j]);
  2877. error+= diff*diff;
  2878. max_error= FFMAX(max_error, diff);
  2879. }
  2880. }
  2881. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  2882. printf("testing quantizer\n");
  2883. for(qp=0; qp<52; qp++){
  2884. for(i=0; i<16; i++)
  2885. src1_block[i]= src2_block[i]= random()%255;
  2886. }
  2887. printf("Testing NAL layer\n");
  2888. uint8_t bitstream[COUNT];
  2889. uint8_t nal[COUNT*2];
  2890. H264Context h;
  2891. memset(&h, 0, sizeof(H264Context));
  2892. for(i=0; i<COUNT; i++){
  2893. int zeros= i;
  2894. int nal_length;
  2895. int consumed;
  2896. int out_length;
  2897. uint8_t *out;
  2898. int j;
  2899. for(j=0; j<COUNT; j++){
  2900. bitstream[j]= (random() % 255) + 1;
  2901. }
  2902. for(j=0; j<zeros; j++){
  2903. int pos= random() % COUNT;
  2904. while(bitstream[pos] == 0){
  2905. pos++;
  2906. pos %= COUNT;
  2907. }
  2908. bitstream[pos]=0;
  2909. }
  2910. START_TIMER
  2911. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  2912. if(nal_length<0){
  2913. printf("encoding failed\n");
  2914. return -1;
  2915. }
  2916. out= ff_h264_decode_nal(&h, nal, &out_length, &consumed, nal_length);
  2917. STOP_TIMER("NAL")
  2918. if(out_length != COUNT){
  2919. printf("incorrect length %d %d\n", out_length, COUNT);
  2920. return -1;
  2921. }
  2922. if(consumed != nal_length){
  2923. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  2924. return -1;
  2925. }
  2926. if(memcmp(bitstream, out, COUNT)){
  2927. printf("mismatch\n");
  2928. return -1;
  2929. }
  2930. }
  2931. #endif
  2932. printf("Testing RBSP\n");
  2933. return 0;
  2934. }
  2935. #endif /* TEST */
  2936. av_cold void ff_h264_free_context(H264Context *h)
  2937. {
  2938. int i;
  2939. free_tables(h, 1); //FIXME cleanup init stuff perhaps
  2940. for(i = 0; i < MAX_SPS_COUNT; i++)
  2941. av_freep(h->sps_buffers + i);
  2942. for(i = 0; i < MAX_PPS_COUNT; i++)
  2943. av_freep(h->pps_buffers + i);
  2944. }
  2945. av_cold int ff_h264_decode_end(AVCodecContext *avctx)
  2946. {
  2947. H264Context *h = avctx->priv_data;
  2948. MpegEncContext *s = &h->s;
  2949. ff_h264_free_context(h);
  2950. MPV_common_end(s);
  2951. // memset(h, 0, sizeof(H264Context));
  2952. return 0;
  2953. }
  2954. static const AVProfile profiles[] = {
  2955. { FF_PROFILE_H264_BASELINE, "Baseline" },
  2956. { FF_PROFILE_H264_CONSTRAINED_BASELINE, "Constrained Baseline" },
  2957. { FF_PROFILE_H264_MAIN, "Main" },
  2958. { FF_PROFILE_H264_EXTENDED, "Extended" },
  2959. { FF_PROFILE_H264_HIGH, "High" },
  2960. { FF_PROFILE_H264_HIGH_10, "High 10" },
  2961. { FF_PROFILE_H264_HIGH_10_INTRA, "High 10 Intra" },
  2962. { FF_PROFILE_H264_HIGH_422, "High 4:2:2" },
  2963. { FF_PROFILE_H264_HIGH_422_INTRA, "High 4:2:2 Intra" },
  2964. { FF_PROFILE_H264_HIGH_444, "High 4:4:4" },
  2965. { FF_PROFILE_H264_HIGH_444_PREDICTIVE, "High 4:4:4 Predictive" },
  2966. { FF_PROFILE_H264_HIGH_444_INTRA, "High 4:4:4 Intra" },
  2967. { FF_PROFILE_H264_CAVLC_444, "CAVLC 4:4:4" },
  2968. { FF_PROFILE_UNKNOWN },
  2969. };
  2970. AVCodec ff_h264_decoder = {
  2971. "h264",
  2972. AVMEDIA_TYPE_VIDEO,
  2973. CODEC_ID_H264,
  2974. sizeof(H264Context),
  2975. ff_h264_decode_init,
  2976. NULL,
  2977. ff_h264_decode_end,
  2978. decode_frame,
  2979. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_FRAME_THREADS,
  2980. .flush= flush_dpb,
  2981. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
  2982. .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
  2983. .update_thread_context = ONLY_IF_THREADS_ENABLED(decode_update_thread_context),
  2984. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  2985. };
  2986. #if CONFIG_H264_VDPAU_DECODER
  2987. AVCodec ff_h264_vdpau_decoder = {
  2988. "h264_vdpau",
  2989. AVMEDIA_TYPE_VIDEO,
  2990. CODEC_ID_H264,
  2991. sizeof(H264Context),
  2992. ff_h264_decode_init,
  2993. NULL,
  2994. ff_h264_decode_end,
  2995. decode_frame,
  2996. CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
  2997. .flush= flush_dpb,
  2998. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),
  2999. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_VDPAU_H264, PIX_FMT_NONE},
  3000. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  3001. };
  3002. #endif