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.

4251 lines
165KB

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