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.

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