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.

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