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.

4248 lines
164KB

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