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.

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