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.

4256 lines
162KB

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