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.

4106 lines
158KB

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