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.

4125 lines
159KB

  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. h->sync = h1->sync;
  1040. if(!s->current_picture_ptr) return 0;
  1041. if(!s->dropable) {
  1042. err = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  1043. h->prev_poc_msb = h->poc_msb;
  1044. h->prev_poc_lsb = h->poc_lsb;
  1045. }
  1046. h->prev_frame_num_offset= h->frame_num_offset;
  1047. h->prev_frame_num = h->frame_num;
  1048. h->outputed_poc = h->next_outputed_poc;
  1049. return err;
  1050. }
  1051. int ff_h264_frame_start(H264Context *h){
  1052. MpegEncContext * const s = &h->s;
  1053. int i;
  1054. const int pixel_shift = h->pixel_shift;
  1055. int thread_count = (s->avctx->active_thread_type & FF_THREAD_SLICE) ? s->avctx->thread_count : 1;
  1056. if(MPV_frame_start(s, s->avctx) < 0)
  1057. return -1;
  1058. ff_er_frame_start(s);
  1059. /*
  1060. * MPV_frame_start uses pict_type to derive key_frame.
  1061. * This is incorrect for H.264; IDR markings must be used.
  1062. * Zero here; IDR markings per slice in frame or fields are ORed in later.
  1063. * See decode_nal_units().
  1064. */
  1065. s->current_picture_ptr->f.key_frame = 0;
  1066. s->current_picture_ptr->mmco_reset= 0;
  1067. assert(s->linesize && s->uvlinesize);
  1068. for(i=0; i<16; i++){
  1069. h->block_offset[i]= (4*((scan8[i] - scan8[0])&7) << pixel_shift) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  1070. h->block_offset[48+i]= (4*((scan8[i] - scan8[0])&7) << pixel_shift) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
  1071. }
  1072. for(i=0; i<16; i++){
  1073. h->block_offset[16+i]=
  1074. h->block_offset[32+i]= (4*((scan8[i] - scan8[0])&7) << pixel_shift) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  1075. h->block_offset[48+16+i]=
  1076. h->block_offset[48+32+i]= (4*((scan8[i] - scan8[0])&7) << pixel_shift) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  1077. }
  1078. /* can't be in alloc_tables because linesize isn't known there.
  1079. * FIXME: redo bipred weight to not require extra buffer? */
  1080. for(i = 0; i < thread_count; i++)
  1081. if(h->thread_context[i] && !h->thread_context[i]->s.obmc_scratchpad)
  1082. h->thread_context[i]->s.obmc_scratchpad = av_malloc(16*6*s->linesize);
  1083. /* some macroblocks can be accessed before they're available in case of lost slices, mbaff or threading*/
  1084. memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(*h->slice_table));
  1085. // s->decode = (s->flags & CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.f.reference /*|| h->contains_intra*/ || 1;
  1086. // We mark the current picture as non-reference after allocating it, so
  1087. // that if we break out due to an error it can be released automatically
  1088. // in the next MPV_frame_start().
  1089. // SVQ3 as well as most other codecs have only last/next/current and thus
  1090. // get released even with set reference, besides SVQ3 and others do not
  1091. // mark frames as reference later "naturally".
  1092. if(s->codec_id != CODEC_ID_SVQ3)
  1093. s->current_picture_ptr->f.reference = 0;
  1094. s->current_picture_ptr->field_poc[0]=
  1095. s->current_picture_ptr->field_poc[1]= INT_MAX;
  1096. h->next_output_pic = NULL;
  1097. assert(s->current_picture_ptr->long_ref==0);
  1098. return 0;
  1099. }
  1100. /**
  1101. * Run setup operations that must be run after slice header decoding.
  1102. * This includes finding the next displayed frame.
  1103. *
  1104. * @param h h264 master context
  1105. * @param setup_finished enough NALs have been read that we can call
  1106. * ff_thread_finish_setup()
  1107. */
  1108. static void decode_postinit(H264Context *h, int setup_finished){
  1109. MpegEncContext * const s = &h->s;
  1110. Picture *out = s->current_picture_ptr;
  1111. Picture *cur = s->current_picture_ptr;
  1112. int i, pics, out_of_order, out_idx;
  1113. s->current_picture_ptr->f.qscale_type = FF_QSCALE_TYPE_H264;
  1114. s->current_picture_ptr->f.pict_type = s->pict_type;
  1115. if (h->next_output_pic) return;
  1116. if (cur->field_poc[0]==INT_MAX || cur->field_poc[1]==INT_MAX) {
  1117. //FIXME: if we have two PAFF fields in one packet, we can't start the next thread here.
  1118. //If we have one field per packet, we can. The check in decode_nal_units() is not good enough
  1119. //to find this yet, so we assume the worst for now.
  1120. //if (setup_finished)
  1121. // ff_thread_finish_setup(s->avctx);
  1122. return;
  1123. }
  1124. cur->f.interlaced_frame = 0;
  1125. cur->f.repeat_pict = 0;
  1126. /* Signal interlacing information externally. */
  1127. /* Prioritize picture timing SEI information over used decoding process if it exists. */
  1128. if(h->sps.pic_struct_present_flag){
  1129. switch (h->sei_pic_struct)
  1130. {
  1131. case SEI_PIC_STRUCT_FRAME:
  1132. break;
  1133. case SEI_PIC_STRUCT_TOP_FIELD:
  1134. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  1135. cur->f.interlaced_frame = 1;
  1136. break;
  1137. case SEI_PIC_STRUCT_TOP_BOTTOM:
  1138. case SEI_PIC_STRUCT_BOTTOM_TOP:
  1139. if (FIELD_OR_MBAFF_PICTURE)
  1140. cur->f.interlaced_frame = 1;
  1141. else
  1142. // try to flag soft telecine progressive
  1143. cur->f.interlaced_frame = h->prev_interlaced_frame;
  1144. break;
  1145. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  1146. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  1147. // Signal the possibility of telecined film externally (pic_struct 5,6)
  1148. // From these hints, let the applications decide if they apply deinterlacing.
  1149. cur->f.repeat_pict = 1;
  1150. break;
  1151. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  1152. // Force progressive here, as doubling interlaced frame is a bad idea.
  1153. cur->f.repeat_pict = 2;
  1154. break;
  1155. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  1156. cur->f.repeat_pict = 4;
  1157. break;
  1158. }
  1159. if ((h->sei_ct_type & 3) && h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP)
  1160. cur->f.interlaced_frame = (h->sei_ct_type & (1 << 1)) != 0;
  1161. }else{
  1162. /* Derive interlacing flag from used decoding process. */
  1163. cur->f.interlaced_frame = FIELD_OR_MBAFF_PICTURE;
  1164. }
  1165. h->prev_interlaced_frame = cur->f.interlaced_frame;
  1166. if (cur->field_poc[0] != cur->field_poc[1]){
  1167. /* Derive top_field_first from field pocs. */
  1168. cur->f.top_field_first = cur->field_poc[0] < cur->field_poc[1];
  1169. }else{
  1170. if (cur->f.interlaced_frame || h->sps.pic_struct_present_flag) {
  1171. /* Use picture timing SEI information. Even if it is a information of a past frame, better than nothing. */
  1172. if(h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM
  1173. || h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
  1174. cur->f.top_field_first = 1;
  1175. else
  1176. cur->f.top_field_first = 0;
  1177. }else{
  1178. /* Most likely progressive */
  1179. cur->f.top_field_first = 0;
  1180. }
  1181. }
  1182. //FIXME do something with unavailable reference frames
  1183. /* Sort B-frames into display order */
  1184. if(h->sps.bitstream_restriction_flag
  1185. && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  1186. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  1187. s->low_delay = 0;
  1188. }
  1189. if( s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT
  1190. && !h->sps.bitstream_restriction_flag){
  1191. s->avctx->has_b_frames= MAX_DELAYED_PIC_COUNT;
  1192. s->low_delay= 0;
  1193. }
  1194. pics = 0;
  1195. while(h->delayed_pic[pics]) pics++;
  1196. av_assert0(pics <= MAX_DELAYED_PIC_COUNT);
  1197. h->delayed_pic[pics++] = cur;
  1198. if (cur->f.reference == 0)
  1199. cur->f.reference = DELAYED_PIC_REF;
  1200. out = h->delayed_pic[0];
  1201. out_idx = 0;
  1202. for (i = 1; h->delayed_pic[i] && !h->delayed_pic[i]->f.key_frame && !h->delayed_pic[i]->mmco_reset; i++)
  1203. if(h->delayed_pic[i]->poc < out->poc){
  1204. out = h->delayed_pic[i];
  1205. out_idx = i;
  1206. }
  1207. if (s->avctx->has_b_frames == 0 && (h->delayed_pic[0]->f.key_frame || h->delayed_pic[0]->mmco_reset))
  1208. h->next_outputed_poc= INT_MIN;
  1209. out_of_order = out->poc < h->next_outputed_poc;
  1210. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
  1211. { }
  1212. else if((out_of_order && pics-1 == s->avctx->has_b_frames && s->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT)
  1213. || (s->low_delay &&
  1214. ((h->next_outputed_poc != INT_MIN && out->poc > h->next_outputed_poc + 2)
  1215. || cur->f.pict_type == AV_PICTURE_TYPE_B)))
  1216. {
  1217. s->low_delay = 0;
  1218. s->avctx->has_b_frames++;
  1219. }
  1220. if(out_of_order || pics > s->avctx->has_b_frames){
  1221. out->f.reference &= ~DELAYED_PIC_REF;
  1222. out->owner2 = s; // for frame threading, the owner must be the second field's thread
  1223. // or else the first thread can release the picture and reuse it unsafely
  1224. for(i=out_idx; h->delayed_pic[i]; i++)
  1225. h->delayed_pic[i] = h->delayed_pic[i+1];
  1226. }
  1227. if(!out_of_order && pics > s->avctx->has_b_frames){
  1228. h->next_output_pic = out;
  1229. if (out_idx == 0 && h->delayed_pic[0] && (h->delayed_pic[0]->f.key_frame || h->delayed_pic[0]->mmco_reset)) {
  1230. h->next_outputed_poc = INT_MIN;
  1231. } else
  1232. h->next_outputed_poc = out->poc;
  1233. }else{
  1234. av_log(s->avctx, AV_LOG_DEBUG, "no picture\n");
  1235. }
  1236. if (h->next_output_pic && h->next_output_pic->sync) {
  1237. h->sync |= 2*!!h->next_output_pic->f.key_frame;
  1238. }
  1239. if (setup_finished)
  1240. ff_thread_finish_setup(s->avctx);
  1241. }
  1242. 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){
  1243. MpegEncContext * const s = &h->s;
  1244. uint8_t *top_border;
  1245. int top_idx = 1;
  1246. const int pixel_shift = h->pixel_shift;
  1247. int chroma444 = CHROMA444;
  1248. int chroma422 = CHROMA422;
  1249. src_y -= linesize;
  1250. src_cb -= uvlinesize;
  1251. src_cr -= uvlinesize;
  1252. if(!simple && FRAME_MBAFF){
  1253. if(s->mb_y&1){
  1254. if(!MB_MBAFF){
  1255. top_border = h->top_borders[0][s->mb_x];
  1256. AV_COPY128(top_border, src_y + 15*linesize);
  1257. if (pixel_shift)
  1258. AV_COPY128(top_border+16, src_y+15*linesize+16);
  1259. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1260. if(chroma444){
  1261. if (pixel_shift){
  1262. AV_COPY128(top_border+32, src_cb + 15*uvlinesize);
  1263. AV_COPY128(top_border+48, src_cb + 15*uvlinesize+16);
  1264. AV_COPY128(top_border+64, src_cr + 15*uvlinesize);
  1265. AV_COPY128(top_border+80, src_cr + 15*uvlinesize+16);
  1266. } else {
  1267. AV_COPY128(top_border+16, src_cb + 15*uvlinesize);
  1268. AV_COPY128(top_border+32, src_cr + 15*uvlinesize);
  1269. }
  1270. } else if(chroma422){
  1271. if (pixel_shift) {
  1272. AV_COPY128(top_border+32, src_cb + 15*uvlinesize);
  1273. AV_COPY128(top_border+48, src_cr + 15*uvlinesize);
  1274. } else {
  1275. AV_COPY64(top_border+16, src_cb + 15*uvlinesize);
  1276. AV_COPY64(top_border+24, src_cr + 15*uvlinesize);
  1277. }
  1278. } else {
  1279. if (pixel_shift) {
  1280. AV_COPY128(top_border+32, src_cb+7*uvlinesize);
  1281. AV_COPY128(top_border+48, src_cr+7*uvlinesize);
  1282. } else {
  1283. AV_COPY64(top_border+16, src_cb+7*uvlinesize);
  1284. AV_COPY64(top_border+24, src_cr+7*uvlinesize);
  1285. }
  1286. }
  1287. }
  1288. }
  1289. }else if(MB_MBAFF){
  1290. top_idx = 0;
  1291. }else
  1292. return;
  1293. }
  1294. top_border = h->top_borders[top_idx][s->mb_x];
  1295. // There are two lines saved, the line above the the top macroblock of a pair,
  1296. // and the line above the bottom macroblock
  1297. AV_COPY128(top_border, src_y + 16*linesize);
  1298. if (pixel_shift)
  1299. AV_COPY128(top_border+16, src_y+16*linesize+16);
  1300. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1301. if(chroma444){
  1302. if (pixel_shift){
  1303. AV_COPY128(top_border+32, src_cb + 16*linesize);
  1304. AV_COPY128(top_border+48, src_cb + 16*linesize+16);
  1305. AV_COPY128(top_border+64, src_cr + 16*linesize);
  1306. AV_COPY128(top_border+80, src_cr + 16*linesize+16);
  1307. } else {
  1308. AV_COPY128(top_border+16, src_cb + 16*linesize);
  1309. AV_COPY128(top_border+32, src_cr + 16*linesize);
  1310. }
  1311. } else if(chroma422) {
  1312. if (pixel_shift) {
  1313. AV_COPY128(top_border+32, src_cb+16*uvlinesize);
  1314. AV_COPY128(top_border+48, src_cr+16*uvlinesize);
  1315. } else {
  1316. AV_COPY64(top_border+16, src_cb+16*uvlinesize);
  1317. AV_COPY64(top_border+24, src_cr+16*uvlinesize);
  1318. }
  1319. } else {
  1320. if (pixel_shift) {
  1321. AV_COPY128(top_border+32, src_cb+8*uvlinesize);
  1322. AV_COPY128(top_border+48, src_cr+8*uvlinesize);
  1323. } else {
  1324. AV_COPY64(top_border+16, src_cb+8*uvlinesize);
  1325. AV_COPY64(top_border+24, src_cr+8*uvlinesize);
  1326. }
  1327. }
  1328. }
  1329. }
  1330. static av_always_inline void xchg_mb_border(H264Context *h, uint8_t *src_y,
  1331. uint8_t *src_cb, uint8_t *src_cr,
  1332. int linesize, int uvlinesize,
  1333. int xchg, int chroma444,
  1334. int simple, int pixel_shift){
  1335. MpegEncContext * const s = &h->s;
  1336. int deblock_topleft;
  1337. int deblock_top;
  1338. int top_idx = 1;
  1339. uint8_t *top_border_m1;
  1340. uint8_t *top_border;
  1341. if(!simple && FRAME_MBAFF){
  1342. if(s->mb_y&1){
  1343. if(!MB_MBAFF)
  1344. return;
  1345. }else{
  1346. top_idx = MB_MBAFF ? 0 : 1;
  1347. }
  1348. }
  1349. if(h->deblocking_filter == 2) {
  1350. deblock_topleft = h->slice_table[h->mb_xy - 1 - s->mb_stride] == h->slice_num;
  1351. deblock_top = h->top_type;
  1352. } else {
  1353. deblock_topleft = (s->mb_x > 0);
  1354. deblock_top = (s->mb_y > !!MB_FIELD);
  1355. }
  1356. src_y -= linesize + 1 + pixel_shift;
  1357. src_cb -= uvlinesize + 1 + pixel_shift;
  1358. src_cr -= uvlinesize + 1 + pixel_shift;
  1359. top_border_m1 = h->top_borders[top_idx][s->mb_x-1];
  1360. top_border = h->top_borders[top_idx][s->mb_x];
  1361. #define XCHG(a,b,xchg)\
  1362. if (pixel_shift) {\
  1363. if (xchg) {\
  1364. AV_SWAP64(b+0,a+0);\
  1365. AV_SWAP64(b+8,a+8);\
  1366. } else {\
  1367. AV_COPY128(b,a); \
  1368. }\
  1369. } else \
  1370. if (xchg) AV_SWAP64(b,a);\
  1371. else AV_COPY64(b,a);
  1372. if(deblock_top){
  1373. if(deblock_topleft){
  1374. XCHG(top_border_m1 + (8 << pixel_shift), src_y - (7 << pixel_shift), 1);
  1375. }
  1376. XCHG(top_border + (0 << pixel_shift), src_y + (1 << pixel_shift), xchg);
  1377. XCHG(top_border + (8 << pixel_shift), src_y + (9 << pixel_shift), 1);
  1378. if(s->mb_x+1 < s->mb_width){
  1379. XCHG(h->top_borders[top_idx][s->mb_x+1], src_y + (17 << pixel_shift), 1);
  1380. }
  1381. }
  1382. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1383. if(chroma444){
  1384. if(deblock_topleft){
  1385. XCHG(top_border_m1 + (24 << pixel_shift), src_cb - (7 << pixel_shift), 1);
  1386. XCHG(top_border_m1 + (40 << pixel_shift), src_cr - (7 << pixel_shift), 1);
  1387. }
  1388. XCHG(top_border + (16 << pixel_shift), src_cb + (1 << pixel_shift), xchg);
  1389. XCHG(top_border + (24 << pixel_shift), src_cb + (9 << pixel_shift), 1);
  1390. XCHG(top_border + (32 << pixel_shift), src_cr + (1 << pixel_shift), xchg);
  1391. XCHG(top_border + (40 << pixel_shift), src_cr + (9 << pixel_shift), 1);
  1392. if(s->mb_x+1 < s->mb_width){
  1393. XCHG(h->top_borders[top_idx][s->mb_x+1] + (16 << pixel_shift), src_cb + (17 << pixel_shift), 1);
  1394. XCHG(h->top_borders[top_idx][s->mb_x+1] + (32 << pixel_shift), src_cr + (17 << pixel_shift), 1);
  1395. }
  1396. } else {
  1397. if(deblock_top){
  1398. if(deblock_topleft){
  1399. XCHG(top_border_m1 + (16 << pixel_shift), src_cb - (7 << pixel_shift), 1);
  1400. XCHG(top_border_m1 + (24 << pixel_shift), src_cr - (7 << pixel_shift), 1);
  1401. }
  1402. XCHG(top_border + (16 << pixel_shift), src_cb+1+pixel_shift, 1);
  1403. XCHG(top_border + (24 << pixel_shift), src_cr+1+pixel_shift, 1);
  1404. }
  1405. }
  1406. }
  1407. }
  1408. static av_always_inline int dctcoef_get(DCTELEM *mb, int high_bit_depth, int index) {
  1409. if (high_bit_depth) {
  1410. return AV_RN32A(((int32_t*)mb) + index);
  1411. } else
  1412. return AV_RN16A(mb + index);
  1413. }
  1414. static av_always_inline void dctcoef_set(DCTELEM *mb, int high_bit_depth, int index, int value) {
  1415. if (high_bit_depth) {
  1416. AV_WN32A(((int32_t*)mb) + index, value);
  1417. } else
  1418. AV_WN16A(mb + index, value);
  1419. }
  1420. static av_always_inline void hl_decode_mb_predict_luma(H264Context *h, int mb_type, int is_h264, int simple, int transform_bypass,
  1421. int pixel_shift, int *block_offset, int linesize, uint8_t *dest_y, int p)
  1422. {
  1423. MpegEncContext * const s = &h->s;
  1424. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  1425. void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  1426. int i;
  1427. int qscale = p == 0 ? s->qscale : h->chroma_qp[p-1];
  1428. block_offset += 16*p;
  1429. if(IS_INTRA4x4(mb_type)){
  1430. if(simple || !s->encoding){
  1431. if(IS_8x8DCT(mb_type)){
  1432. if(transform_bypass){
  1433. idct_dc_add =
  1434. idct_add = s->dsp.add_pixels8;
  1435. }else{
  1436. idct_dc_add = h->h264dsp.h264_idct8_dc_add;
  1437. idct_add = h->h264dsp.h264_idct8_add;
  1438. }
  1439. for(i=0; i<16; i+=4){
  1440. uint8_t * const ptr= dest_y + block_offset[i];
  1441. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  1442. if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
  1443. h->hpc.pred8x8l_add[dir](ptr, h->mb + (i*16+p*256 << pixel_shift), linesize);
  1444. }else{
  1445. const int nnz = h->non_zero_count_cache[ scan8[i+p*16] ];
  1446. h->hpc.pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
  1447. (h->topright_samples_available<<i)&0x4000, linesize);
  1448. if(nnz){
  1449. if(nnz == 1 && dctcoef_get(h->mb, pixel_shift, i*16+p*256))
  1450. idct_dc_add(ptr, h->mb + (i*16+p*256 << pixel_shift), linesize);
  1451. else
  1452. idct_add (ptr, h->mb + (i*16+p*256 << pixel_shift), linesize);
  1453. }
  1454. }
  1455. }
  1456. }else{
  1457. if(transform_bypass){
  1458. idct_dc_add =
  1459. idct_add = s->dsp.add_pixels4;
  1460. }else{
  1461. idct_dc_add = h->h264dsp.h264_idct_dc_add;
  1462. idct_add = h->h264dsp.h264_idct_add;
  1463. }
  1464. for(i=0; i<16; i++){
  1465. uint8_t * const ptr= dest_y + block_offset[i];
  1466. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  1467. if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
  1468. h->hpc.pred4x4_add[dir](ptr, h->mb + (i*16+p*256 << pixel_shift), linesize);
  1469. }else{
  1470. uint8_t *topright;
  1471. int nnz, tr;
  1472. uint64_t tr_high;
  1473. if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
  1474. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  1475. assert(s->mb_y || linesize <= block_offset[i]);
  1476. if(!topright_avail){
  1477. if (pixel_shift) {
  1478. tr_high= ((uint16_t*)ptr)[3 - linesize/2]*0x0001000100010001ULL;
  1479. topright= (uint8_t*) &tr_high;
  1480. } else {
  1481. tr= ptr[3 - linesize]*0x01010101;
  1482. topright= (uint8_t*) &tr;
  1483. }
  1484. }else
  1485. topright= ptr + (4 << pixel_shift) - linesize;
  1486. }else
  1487. topright= NULL;
  1488. h->hpc.pred4x4[ dir ](ptr, topright, linesize);
  1489. nnz = h->non_zero_count_cache[ scan8[i+p*16] ];
  1490. if(nnz){
  1491. if(is_h264){
  1492. if(nnz == 1 && dctcoef_get(h->mb, pixel_shift, i*16+p*256))
  1493. idct_dc_add(ptr, h->mb + (i*16+p*256 << pixel_shift), linesize);
  1494. else
  1495. idct_add (ptr, h->mb + (i*16+p*256 << pixel_shift), linesize);
  1496. }else
  1497. ff_svq3_add_idct_c(ptr, h->mb + i*16+p*256, linesize, qscale, 0);
  1498. }
  1499. }
  1500. }
  1501. }
  1502. }
  1503. }else{
  1504. h->hpc.pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  1505. if(is_h264){
  1506. if(h->non_zero_count_cache[ scan8[LUMA_DC_BLOCK_INDEX+p] ]){
  1507. if(!transform_bypass)
  1508. h->h264dsp.h264_luma_dc_dequant_idct(h->mb+(p*256 << pixel_shift), h->mb_luma_dc[p], h->dequant4_coeff[p][qscale][0]);
  1509. else{
  1510. static const uint8_t dc_mapping[16] = { 0*16, 1*16, 4*16, 5*16, 2*16, 3*16, 6*16, 7*16,
  1511. 8*16, 9*16,12*16,13*16,10*16,11*16,14*16,15*16};
  1512. for(i = 0; i < 16; i++)
  1513. dctcoef_set(h->mb+p*256, pixel_shift, dc_mapping[i], dctcoef_get(h->mb_luma_dc[p], pixel_shift, i));
  1514. }
  1515. }
  1516. }else
  1517. ff_svq3_luma_dc_dequant_idct_c(h->mb+p*256, h->mb_luma_dc[p], qscale);
  1518. }
  1519. }
  1520. static av_always_inline void hl_decode_mb_idct_luma(H264Context *h, int mb_type, int is_h264, int simple, int transform_bypass,
  1521. int pixel_shift, int *block_offset, int linesize, uint8_t *dest_y, int p)
  1522. {
  1523. MpegEncContext * const s = &h->s;
  1524. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  1525. int i;
  1526. block_offset += 16*p;
  1527. if(!IS_INTRA4x4(mb_type)){
  1528. if(is_h264){
  1529. if(IS_INTRA16x16(mb_type)){
  1530. if(transform_bypass){
  1531. if(h->sps.profile_idc==244 && (h->intra16x16_pred_mode==VERT_PRED8x8 || h->intra16x16_pred_mode==HOR_PRED8x8)){
  1532. h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset, h->mb + (p*256 << pixel_shift), linesize);
  1533. }else{
  1534. for(i=0; i<16; i++){
  1535. if(h->non_zero_count_cache[ scan8[i+p*16] ] || dctcoef_get(h->mb, pixel_shift, i*16+p*256))
  1536. s->dsp.add_pixels4(dest_y + block_offset[i], h->mb + (i*16+p*256 << pixel_shift), linesize);
  1537. }
  1538. }
  1539. }else{
  1540. h->h264dsp.h264_idct_add16intra(dest_y, block_offset, h->mb + (p*256 << pixel_shift), linesize, h->non_zero_count_cache+p*5*8);
  1541. }
  1542. }else if(h->cbp&15){
  1543. if(transform_bypass){
  1544. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  1545. idct_add= IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
  1546. for(i=0; i<16; i+=di){
  1547. if(h->non_zero_count_cache[ scan8[i+p*16] ]){
  1548. idct_add(dest_y + block_offset[i], h->mb + (i*16+p*256 << pixel_shift), linesize);
  1549. }
  1550. }
  1551. }else{
  1552. if(IS_8x8DCT(mb_type)){
  1553. h->h264dsp.h264_idct8_add4(dest_y, block_offset, h->mb + (p*256 << pixel_shift), linesize, h->non_zero_count_cache+p*5*8);
  1554. }else{
  1555. h->h264dsp.h264_idct_add16(dest_y, block_offset, h->mb + (p*256 << pixel_shift), linesize, h->non_zero_count_cache+p*5*8);
  1556. }
  1557. }
  1558. }
  1559. }else{
  1560. for(i=0; i<16; i++){
  1561. if(h->non_zero_count_cache[ scan8[i+p*16] ] || h->mb[i*16+p*256]){ //FIXME benchmark weird rule, & below
  1562. uint8_t * const ptr= dest_y + block_offset[i];
  1563. ff_svq3_add_idct_c(ptr, h->mb + i*16 + p*256, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  1564. }
  1565. }
  1566. }
  1567. }
  1568. }
  1569. static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple, int pixel_shift){
  1570. MpegEncContext * const s = &h->s;
  1571. const int mb_x= s->mb_x;
  1572. const int mb_y= s->mb_y;
  1573. const int mb_xy= h->mb_xy;
  1574. const int mb_type = s->current_picture.f.mb_type[mb_xy];
  1575. uint8_t *dest_y, *dest_cb, *dest_cr;
  1576. int linesize, uvlinesize /*dct_offset*/;
  1577. int i, j;
  1578. int *block_offset = &h->block_offset[0];
  1579. const int transform_bypass = !simple && (s->qscale == 0 && h->sps.transform_bypass);
  1580. /* is_h264 should always be true if SVQ3 is disabled. */
  1581. const int is_h264 = !CONFIG_SVQ3_DECODER || simple || s->codec_id == CODEC_ID_H264;
  1582. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  1583. const int block_h = 16>>s->chroma_y_shift;
  1584. dest_y = s->current_picture.f.data[0] + ((mb_x << pixel_shift) + mb_y * s->linesize ) * 16;
  1585. dest_cb = s->current_picture.f.data[1] + (mb_x << pixel_shift)*8 + mb_y * s->uvlinesize * block_h;
  1586. dest_cr = s->current_picture.f.data[2] + (mb_x << pixel_shift)*8 + mb_y * s->uvlinesize * block_h;
  1587. s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + (64 << pixel_shift), s->linesize, 4);
  1588. s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + (64 << pixel_shift), dest_cr - dest_cb, 2);
  1589. h->list_counts[mb_xy]= h->list_count;
  1590. if (!simple && MB_FIELD) {
  1591. linesize = h->mb_linesize = s->linesize * 2;
  1592. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  1593. block_offset = &h->block_offset[48];
  1594. if(mb_y&1){ //FIXME move out of this function?
  1595. dest_y -= s->linesize*15;
  1596. dest_cb-= s->uvlinesize*(block_h-1);
  1597. dest_cr-= s->uvlinesize*(block_h-1);
  1598. }
  1599. if(FRAME_MBAFF) {
  1600. int list;
  1601. for(list=0; list<h->list_count; list++){
  1602. if(!USES_LIST(mb_type, list))
  1603. continue;
  1604. if(IS_16X16(mb_type)){
  1605. int8_t *ref = &h->ref_cache[list][scan8[0]];
  1606. fill_rectangle(ref, 4, 4, 8, (16+*ref)^(s->mb_y&1), 1);
  1607. }else{
  1608. for(i=0; i<16; i+=4){
  1609. int ref = h->ref_cache[list][scan8[i]];
  1610. if(ref >= 0)
  1611. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, (16+ref)^(s->mb_y&1), 1);
  1612. }
  1613. }
  1614. }
  1615. }
  1616. } else {
  1617. linesize = h->mb_linesize = s->linesize;
  1618. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  1619. // dct_offset = s->linesize * 16;
  1620. }
  1621. if (!simple && IS_INTRA_PCM(mb_type)) {
  1622. const int bit_depth = h->sps.bit_depth_luma;
  1623. if (pixel_shift) {
  1624. int j;
  1625. GetBitContext gb;
  1626. init_get_bits(&gb, (uint8_t*)h->mb, 384*bit_depth);
  1627. for (i = 0; i < 16; i++) {
  1628. uint16_t *tmp_y = (uint16_t*)(dest_y + i*linesize);
  1629. for (j = 0; j < 16; j++)
  1630. tmp_y[j] = get_bits(&gb, bit_depth);
  1631. }
  1632. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1633. if (!h->sps.chroma_format_idc) {
  1634. for (i = 0; i < 8; i++) {
  1635. uint16_t *tmp_cb = (uint16_t*)(dest_cb + i*uvlinesize);
  1636. uint16_t *tmp_cr = (uint16_t*)(dest_cr + i*uvlinesize);
  1637. for (j = 0; j < 8; j++) {
  1638. tmp_cb[j] = tmp_cr[j] = 1 << (bit_depth - 1);
  1639. }
  1640. }
  1641. } else {
  1642. for (i = 0; i < block_h; i++) {
  1643. uint16_t *tmp_cb = (uint16_t*)(dest_cb + i*uvlinesize);
  1644. for (j = 0; j < 8; j++)
  1645. tmp_cb[j] = get_bits(&gb, bit_depth);
  1646. }
  1647. for (i = 0; i < block_h; i++) {
  1648. uint16_t *tmp_cr = (uint16_t*)(dest_cr + i*uvlinesize);
  1649. for (j = 0; j < 8; j++)
  1650. tmp_cr[j] = get_bits(&gb, bit_depth);
  1651. }
  1652. }
  1653. }
  1654. } else {
  1655. for (i=0; i<16; i++) {
  1656. memcpy(dest_y + i* linesize, h->mb + i*8, 16);
  1657. }
  1658. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1659. if (!h->sps.chroma_format_idc) {
  1660. for (i=0; i<8; i++) {
  1661. memset(dest_cb+ i*uvlinesize, 1 << (bit_depth - 1), 8);
  1662. memset(dest_cr+ i*uvlinesize, 1 << (bit_depth - 1), 8);
  1663. }
  1664. } else {
  1665. for (i=0; i<block_h; i++) {
  1666. memcpy(dest_cb+ i*uvlinesize, h->mb + 128 + i*4, 8);
  1667. memcpy(dest_cr+ i*uvlinesize, h->mb + 160 + i*4, 8);
  1668. }
  1669. }
  1670. }
  1671. }
  1672. } else {
  1673. if(IS_INTRA(mb_type)){
  1674. if(h->deblocking_filter)
  1675. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1, 0, simple, pixel_shift);
  1676. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1677. h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  1678. h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  1679. }
  1680. hl_decode_mb_predict_luma(h, mb_type, is_h264, simple, transform_bypass, pixel_shift, block_offset, linesize, dest_y, 0);
  1681. if(h->deblocking_filter)
  1682. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0, 0, simple, pixel_shift);
  1683. }else if(is_h264){
  1684. hl_motion(h, dest_y, dest_cb, dest_cr,
  1685. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  1686. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  1687. h->h264dsp.weight_h264_pixels_tab,
  1688. h->h264dsp.biweight_h264_pixels_tab, pixel_shift, 0);
  1689. }
  1690. hl_decode_mb_idct_luma(h, mb_type, is_h264, simple, transform_bypass, pixel_shift, block_offset, linesize, dest_y, 0);
  1691. if((simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)) && (h->cbp&0x30)){
  1692. uint8_t *dest[2] = {dest_cb, dest_cr};
  1693. if(transform_bypass){
  1694. if(IS_INTRA(mb_type) && h->sps.profile_idc==244 && (h->chroma_pred_mode==VERT_PRED8x8 || h->chroma_pred_mode==HOR_PRED8x8)){
  1695. h->hpc.pred8x8_add[h->chroma_pred_mode](dest[0], block_offset + 16, h->mb + (16*16*1 << pixel_shift), uvlinesize);
  1696. h->hpc.pred8x8_add[h->chroma_pred_mode](dest[1], block_offset + 32, h->mb + (16*16*2 << pixel_shift), uvlinesize);
  1697. }else{
  1698. idct_add = s->dsp.add_pixels4;
  1699. for(j=1; j<3; j++){
  1700. for(i=j*16; i<j*16+4; i++){
  1701. if(h->non_zero_count_cache[ scan8[i] ] || dctcoef_get(h->mb, pixel_shift, i*16))
  1702. idct_add (dest[j-1] + block_offset[i], h->mb + (i*16 << pixel_shift), uvlinesize);
  1703. }
  1704. }
  1705. }
  1706. }else{
  1707. if(is_h264){
  1708. int qp[2];
  1709. if (CHROMA422) {
  1710. qp[0] = h->chroma_qp[0]+3;
  1711. qp[1] = h->chroma_qp[1]+3;
  1712. } else {
  1713. qp[0] = h->chroma_qp[0];
  1714. qp[1] = h->chroma_qp[1];
  1715. }
  1716. if(h->non_zero_count_cache[ scan8[CHROMA_DC_BLOCK_INDEX+0] ])
  1717. 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]);
  1718. if(h->non_zero_count_cache[ scan8[CHROMA_DC_BLOCK_INDEX+1] ])
  1719. 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]);
  1720. h->h264dsp.h264_idct_add8(dest, block_offset,
  1721. h->mb, uvlinesize,
  1722. h->non_zero_count_cache);
  1723. }
  1724. #if CONFIG_SVQ3_DECODER
  1725. else{
  1726. 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]);
  1727. 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]);
  1728. for(j=1; j<3; j++){
  1729. for(i=j*16; i<j*16+4; i++){
  1730. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  1731. uint8_t * const ptr= dest[j-1] + block_offset[i];
  1732. ff_svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, ff_h264_chroma_qp[0][s->qscale + 12] - 12, 2);
  1733. }
  1734. }
  1735. }
  1736. }
  1737. #endif
  1738. }
  1739. }
  1740. }
  1741. if(h->cbp || IS_INTRA(mb_type))
  1742. {
  1743. s->dsp.clear_blocks(h->mb);
  1744. s->dsp.clear_blocks(h->mb+(24*16<<pixel_shift));
  1745. }
  1746. }
  1747. static av_always_inline void hl_decode_mb_444_internal(H264Context *h, int simple, int pixel_shift){
  1748. MpegEncContext * const s = &h->s;
  1749. const int mb_x= s->mb_x;
  1750. const int mb_y= s->mb_y;
  1751. const int mb_xy= h->mb_xy;
  1752. const int mb_type = s->current_picture.f.mb_type[mb_xy];
  1753. uint8_t *dest[3];
  1754. int linesize;
  1755. int i, j, p;
  1756. int *block_offset = &h->block_offset[0];
  1757. const int transform_bypass = !simple && (s->qscale == 0 && h->sps.transform_bypass);
  1758. const int plane_count = (simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)) ? 3 : 1;
  1759. for (p = 0; p < plane_count; p++)
  1760. {
  1761. dest[p] = s->current_picture.f.data[p] + ((mb_x << pixel_shift) + mb_y * s->linesize) * 16;
  1762. s->dsp.prefetch(dest[p] + (s->mb_x&3)*4*s->linesize + (64 << pixel_shift), s->linesize, 4);
  1763. }
  1764. h->list_counts[mb_xy]= h->list_count;
  1765. if (!simple && MB_FIELD) {
  1766. linesize = h->mb_linesize = h->mb_uvlinesize = s->linesize * 2;
  1767. block_offset = &h->block_offset[48];
  1768. if(mb_y&1) //FIXME move out of this function?
  1769. for (p = 0; p < 3; p++)
  1770. dest[p] -= s->linesize*15;
  1771. if(FRAME_MBAFF) {
  1772. int list;
  1773. for(list=0; list<h->list_count; list++){
  1774. if(!USES_LIST(mb_type, list))
  1775. continue;
  1776. if(IS_16X16(mb_type)){
  1777. int8_t *ref = &h->ref_cache[list][scan8[0]];
  1778. fill_rectangle(ref, 4, 4, 8, (16+*ref)^(s->mb_y&1), 1);
  1779. }else{
  1780. for(i=0; i<16; i+=4){
  1781. int ref = h->ref_cache[list][scan8[i]];
  1782. if(ref >= 0)
  1783. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, (16+ref)^(s->mb_y&1), 1);
  1784. }
  1785. }
  1786. }
  1787. }
  1788. } else {
  1789. linesize = h->mb_linesize = h->mb_uvlinesize = s->linesize;
  1790. }
  1791. if (!simple && IS_INTRA_PCM(mb_type)) {
  1792. if (pixel_shift) {
  1793. const int bit_depth = h->sps.bit_depth_luma;
  1794. GetBitContext gb;
  1795. init_get_bits(&gb, (uint8_t*)h->mb, 768*bit_depth);
  1796. for (p = 0; p < plane_count; p++) {
  1797. for (i = 0; i < 16; i++) {
  1798. uint16_t *tmp = (uint16_t*)(dest[p] + i*linesize);
  1799. for (j = 0; j < 16; j++)
  1800. tmp[j] = get_bits(&gb, bit_depth);
  1801. }
  1802. }
  1803. } else {
  1804. for (p = 0; p < plane_count; p++) {
  1805. for (i = 0; i < 16; i++) {
  1806. memcpy(dest[p] + i*linesize, h->mb + p*128 + i*8, 16);
  1807. }
  1808. }
  1809. }
  1810. } else {
  1811. if(IS_INTRA(mb_type)){
  1812. if(h->deblocking_filter)
  1813. xchg_mb_border(h, dest[0], dest[1], dest[2], linesize, linesize, 1, 1, simple, pixel_shift);
  1814. for (p = 0; p < plane_count; p++)
  1815. hl_decode_mb_predict_luma(h, mb_type, 1, simple, transform_bypass, pixel_shift, block_offset, linesize, dest[p], p);
  1816. if(h->deblocking_filter)
  1817. xchg_mb_border(h, dest[0], dest[1], dest[2], linesize, linesize, 0, 1, simple, pixel_shift);
  1818. }else{
  1819. hl_motion(h, dest[0], dest[1], dest[2],
  1820. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  1821. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  1822. h->h264dsp.weight_h264_pixels_tab,
  1823. h->h264dsp.biweight_h264_pixels_tab, pixel_shift, 1);
  1824. }
  1825. for (p = 0; p < plane_count; p++)
  1826. hl_decode_mb_idct_luma(h, mb_type, 1, simple, transform_bypass, pixel_shift, block_offset, linesize, dest[p], p);
  1827. }
  1828. if(h->cbp || IS_INTRA(mb_type))
  1829. {
  1830. s->dsp.clear_blocks(h->mb);
  1831. s->dsp.clear_blocks(h->mb+(24*16<<pixel_shift));
  1832. }
  1833. }
  1834. /**
  1835. * Process a macroblock; this case avoids checks for expensive uncommon cases.
  1836. */
  1837. #define hl_decode_mb_simple(sh, bits) \
  1838. static void hl_decode_mb_simple_ ## bits(H264Context *h){ \
  1839. hl_decode_mb_internal(h, 1, sh); \
  1840. }
  1841. hl_decode_mb_simple(0, 8);
  1842. hl_decode_mb_simple(1, 16);
  1843. /**
  1844. * Process a macroblock; this handles edge cases, such as interlacing.
  1845. */
  1846. static void av_noinline hl_decode_mb_complex(H264Context *h){
  1847. hl_decode_mb_internal(h, 0, h->pixel_shift);
  1848. }
  1849. static void av_noinline hl_decode_mb_444_complex(H264Context *h){
  1850. hl_decode_mb_444_internal(h, 0, h->pixel_shift);
  1851. }
  1852. static void av_noinline hl_decode_mb_444_simple(H264Context *h){
  1853. hl_decode_mb_444_internal(h, 1, 0);
  1854. }
  1855. void ff_h264_hl_decode_mb(H264Context *h){
  1856. MpegEncContext * const s = &h->s;
  1857. const int mb_xy= h->mb_xy;
  1858. const int mb_type = s->current_picture.f.mb_type[mb_xy];
  1859. int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || s->qscale == 0;
  1860. if (CHROMA444) {
  1861. if(is_complex || h->pixel_shift)
  1862. hl_decode_mb_444_complex(h);
  1863. else
  1864. hl_decode_mb_444_simple(h);
  1865. } else if (is_complex) {
  1866. hl_decode_mb_complex(h);
  1867. } else if (h->pixel_shift) {
  1868. hl_decode_mb_simple_16(h);
  1869. } else
  1870. hl_decode_mb_simple_8(h);
  1871. }
  1872. static int pred_weight_table(H264Context *h){
  1873. MpegEncContext * const s = &h->s;
  1874. int list, i;
  1875. int luma_def, chroma_def;
  1876. h->use_weight= 0;
  1877. h->use_weight_chroma= 0;
  1878. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  1879. if(h->sps.chroma_format_idc)
  1880. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  1881. luma_def = 1<<h->luma_log2_weight_denom;
  1882. chroma_def = 1<<h->chroma_log2_weight_denom;
  1883. for(list=0; list<2; list++){
  1884. h->luma_weight_flag[list] = 0;
  1885. h->chroma_weight_flag[list] = 0;
  1886. for(i=0; i<h->ref_count[list]; i++){
  1887. int luma_weight_flag, chroma_weight_flag;
  1888. luma_weight_flag= get_bits1(&s->gb);
  1889. if(luma_weight_flag){
  1890. h->luma_weight[i][list][0]= get_se_golomb(&s->gb);
  1891. h->luma_weight[i][list][1]= get_se_golomb(&s->gb);
  1892. if( h->luma_weight[i][list][0] != luma_def
  1893. || h->luma_weight[i][list][1] != 0) {
  1894. h->use_weight= 1;
  1895. h->luma_weight_flag[list]= 1;
  1896. }
  1897. }else{
  1898. h->luma_weight[i][list][0]= luma_def;
  1899. h->luma_weight[i][list][1]= 0;
  1900. }
  1901. if(h->sps.chroma_format_idc){
  1902. chroma_weight_flag= get_bits1(&s->gb);
  1903. if(chroma_weight_flag){
  1904. int j;
  1905. for(j=0; j<2; j++){
  1906. h->chroma_weight[i][list][j][0]= get_se_golomb(&s->gb);
  1907. h->chroma_weight[i][list][j][1]= get_se_golomb(&s->gb);
  1908. if( h->chroma_weight[i][list][j][0] != chroma_def
  1909. || h->chroma_weight[i][list][j][1] != 0) {
  1910. h->use_weight_chroma= 1;
  1911. h->chroma_weight_flag[list]= 1;
  1912. }
  1913. }
  1914. }else{
  1915. int j;
  1916. for(j=0; j<2; j++){
  1917. h->chroma_weight[i][list][j][0]= chroma_def;
  1918. h->chroma_weight[i][list][j][1]= 0;
  1919. }
  1920. }
  1921. }
  1922. }
  1923. if(h->slice_type_nos != AV_PICTURE_TYPE_B) break;
  1924. }
  1925. h->use_weight= h->use_weight || h->use_weight_chroma;
  1926. return 0;
  1927. }
  1928. /**
  1929. * Initialize implicit_weight table.
  1930. * @param field 0/1 initialize the weight for interlaced MBAFF
  1931. * -1 initializes the rest
  1932. */
  1933. static void implicit_weight_table(H264Context *h, int field){
  1934. MpegEncContext * const s = &h->s;
  1935. int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
  1936. for (i = 0; i < 2; i++) {
  1937. h->luma_weight_flag[i] = 0;
  1938. h->chroma_weight_flag[i] = 0;
  1939. }
  1940. if(field < 0){
  1941. cur_poc = s->current_picture_ptr->poc;
  1942. if( h->ref_count[0] == 1 && h->ref_count[1] == 1 && !FRAME_MBAFF
  1943. && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
  1944. h->use_weight= 0;
  1945. h->use_weight_chroma= 0;
  1946. return;
  1947. }
  1948. ref_start= 0;
  1949. ref_count0= h->ref_count[0];
  1950. ref_count1= h->ref_count[1];
  1951. }else{
  1952. cur_poc = s->current_picture_ptr->field_poc[field];
  1953. ref_start= 16;
  1954. ref_count0= 16+2*h->ref_count[0];
  1955. ref_count1= 16+2*h->ref_count[1];
  1956. }
  1957. h->use_weight= 2;
  1958. h->use_weight_chroma= 2;
  1959. h->luma_log2_weight_denom= 5;
  1960. h->chroma_log2_weight_denom= 5;
  1961. for(ref0=ref_start; ref0 < ref_count0; ref0++){
  1962. int poc0 = h->ref_list[0][ref0].poc;
  1963. for(ref1=ref_start; ref1 < ref_count1; ref1++){
  1964. int w = 32;
  1965. if (!h->ref_list[0][ref0].long_ref && !h->ref_list[1][ref1].long_ref) {
  1966. int poc1 = h->ref_list[1][ref1].poc;
  1967. int td = av_clip(poc1 - poc0, -128, 127);
  1968. if(td){
  1969. int tb = av_clip(cur_poc - poc0, -128, 127);
  1970. int tx = (16384 + (FFABS(td) >> 1)) / td;
  1971. int dist_scale_factor = (tb*tx + 32) >> 8;
  1972. if(dist_scale_factor >= -64 && dist_scale_factor <= 128)
  1973. w = 64 - dist_scale_factor;
  1974. }
  1975. }
  1976. if(field<0){
  1977. h->implicit_weight[ref0][ref1][0]=
  1978. h->implicit_weight[ref0][ref1][1]= w;
  1979. }else{
  1980. h->implicit_weight[ref0][ref1][field]=w;
  1981. }
  1982. }
  1983. }
  1984. }
  1985. /**
  1986. * instantaneous decoder refresh.
  1987. */
  1988. static void idr(H264Context *h){
  1989. ff_h264_remove_all_refs(h);
  1990. h->prev_frame_num= 0;
  1991. h->prev_frame_num_offset= 0;
  1992. h->prev_poc_msb=
  1993. h->prev_poc_lsb= 0;
  1994. }
  1995. /* forget old pics after a seek */
  1996. static void flush_dpb(AVCodecContext *avctx){
  1997. H264Context *h= avctx->priv_data;
  1998. int i;
  1999. for(i=0; i<=MAX_DELAYED_PIC_COUNT; i++) {
  2000. if(h->delayed_pic[i])
  2001. h->delayed_pic[i]->f.reference = 0;
  2002. h->delayed_pic[i]= NULL;
  2003. }
  2004. h->outputed_poc=h->next_outputed_poc= INT_MIN;
  2005. h->prev_interlaced_frame = 1;
  2006. idr(h);
  2007. if(h->s.current_picture_ptr)
  2008. h->s.current_picture_ptr->f.reference = 0;
  2009. h->s.first_field= 0;
  2010. ff_h264_reset_sei(h);
  2011. ff_mpeg_flush(avctx);
  2012. h->recovery_frame= -1;
  2013. h->sync= 0;
  2014. }
  2015. static int init_poc(H264Context *h){
  2016. MpegEncContext * const s = &h->s;
  2017. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  2018. int field_poc[2];
  2019. Picture *cur = s->current_picture_ptr;
  2020. h->frame_num_offset= h->prev_frame_num_offset;
  2021. if(h->frame_num < h->prev_frame_num)
  2022. h->frame_num_offset += max_frame_num;
  2023. if(h->sps.poc_type==0){
  2024. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  2025. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  2026. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  2027. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  2028. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  2029. else
  2030. h->poc_msb = h->prev_poc_msb;
  2031. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  2032. field_poc[0] =
  2033. field_poc[1] = h->poc_msb + h->poc_lsb;
  2034. if(s->picture_structure == PICT_FRAME)
  2035. field_poc[1] += h->delta_poc_bottom;
  2036. }else if(h->sps.poc_type==1){
  2037. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  2038. int i;
  2039. if(h->sps.poc_cycle_length != 0)
  2040. abs_frame_num = h->frame_num_offset + h->frame_num;
  2041. else
  2042. abs_frame_num = 0;
  2043. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  2044. abs_frame_num--;
  2045. expected_delta_per_poc_cycle = 0;
  2046. for(i=0; i < h->sps.poc_cycle_length; i++)
  2047. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  2048. if(abs_frame_num > 0){
  2049. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  2050. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  2051. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  2052. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  2053. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  2054. } else
  2055. expectedpoc = 0;
  2056. if(h->nal_ref_idc == 0)
  2057. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  2058. field_poc[0] = expectedpoc + h->delta_poc[0];
  2059. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  2060. if(s->picture_structure == PICT_FRAME)
  2061. field_poc[1] += h->delta_poc[1];
  2062. }else{
  2063. int poc= 2*(h->frame_num_offset + h->frame_num);
  2064. if(!h->nal_ref_idc)
  2065. poc--;
  2066. field_poc[0]= poc;
  2067. field_poc[1]= poc;
  2068. }
  2069. if(s->picture_structure != PICT_BOTTOM_FIELD)
  2070. s->current_picture_ptr->field_poc[0]= field_poc[0];
  2071. if(s->picture_structure != PICT_TOP_FIELD)
  2072. s->current_picture_ptr->field_poc[1]= field_poc[1];
  2073. cur->poc= FFMIN(cur->field_poc[0], cur->field_poc[1]);
  2074. return 0;
  2075. }
  2076. /**
  2077. * initialize scan tables
  2078. */
  2079. static void init_scan_tables(H264Context *h){
  2080. int i;
  2081. for(i=0; i<16; i++){
  2082. #define T(x) (x>>2) | ((x<<2) & 0xF)
  2083. h->zigzag_scan[i] = T(zigzag_scan[i]);
  2084. h-> field_scan[i] = T( field_scan[i]);
  2085. #undef T
  2086. }
  2087. for(i=0; i<64; i++){
  2088. #define T(x) (x>>3) | ((x&7)<<3)
  2089. h->zigzag_scan8x8[i] = T(ff_zigzag_direct[i]);
  2090. h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
  2091. h->field_scan8x8[i] = T(field_scan8x8[i]);
  2092. h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
  2093. #undef T
  2094. }
  2095. if(h->sps.transform_bypass){ //FIXME same ugly
  2096. h->zigzag_scan_q0 = zigzag_scan;
  2097. h->zigzag_scan8x8_q0 = ff_zigzag_direct;
  2098. h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
  2099. h->field_scan_q0 = field_scan;
  2100. h->field_scan8x8_q0 = field_scan8x8;
  2101. h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
  2102. }else{
  2103. h->zigzag_scan_q0 = h->zigzag_scan;
  2104. h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
  2105. h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
  2106. h->field_scan_q0 = h->field_scan;
  2107. h->field_scan8x8_q0 = h->field_scan8x8;
  2108. h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
  2109. }
  2110. }
  2111. static int field_end(H264Context *h, int in_setup){
  2112. MpegEncContext * const s = &h->s;
  2113. AVCodecContext * const avctx= s->avctx;
  2114. int err = 0;
  2115. s->mb_y= 0;
  2116. if (!in_setup && !s->dropable)
  2117. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, (16*s->mb_height >> FIELD_PICTURE) - 1,
  2118. s->picture_structure==PICT_BOTTOM_FIELD);
  2119. if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  2120. ff_vdpau_h264_set_reference_frames(s);
  2121. if(in_setup || !(avctx->active_thread_type&FF_THREAD_FRAME)){
  2122. if(!s->dropable) {
  2123. err = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  2124. h->prev_poc_msb= h->poc_msb;
  2125. h->prev_poc_lsb= h->poc_lsb;
  2126. }
  2127. h->prev_frame_num_offset= h->frame_num_offset;
  2128. h->prev_frame_num= h->frame_num;
  2129. h->outputed_poc = h->next_outputed_poc;
  2130. }
  2131. if (avctx->hwaccel) {
  2132. if (avctx->hwaccel->end_frame(avctx) < 0)
  2133. av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
  2134. }
  2135. if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  2136. ff_vdpau_h264_picture_complete(s);
  2137. /*
  2138. * FIXME: Error handling code does not seem to support interlaced
  2139. * when slices span multiple rows
  2140. * The ff_er_add_slice calls don't work right for bottom
  2141. * fields; they cause massive erroneous error concealing
  2142. * Error marking covers both fields (top and bottom).
  2143. * This causes a mismatched s->error_count
  2144. * and a bad error table. Further, the error count goes to
  2145. * INT_MAX when called for bottom field, because mb_y is
  2146. * past end by one (callers fault) and resync_mb_y != 0
  2147. * causes problems for the first MB line, too.
  2148. */
  2149. if (!FIELD_PICTURE)
  2150. ff_er_frame_end(s);
  2151. MPV_frame_end(s);
  2152. h->current_slice=0;
  2153. return err;
  2154. }
  2155. /**
  2156. * Replicate H264 "master" context to thread contexts.
  2157. */
  2158. static void clone_slice(H264Context *dst, H264Context *src)
  2159. {
  2160. memcpy(dst->block_offset, src->block_offset, sizeof(dst->block_offset));
  2161. dst->s.current_picture_ptr = src->s.current_picture_ptr;
  2162. dst->s.current_picture = src->s.current_picture;
  2163. dst->s.linesize = src->s.linesize;
  2164. dst->s.uvlinesize = src->s.uvlinesize;
  2165. dst->s.first_field = src->s.first_field;
  2166. dst->prev_poc_msb = src->prev_poc_msb;
  2167. dst->prev_poc_lsb = src->prev_poc_lsb;
  2168. dst->prev_frame_num_offset = src->prev_frame_num_offset;
  2169. dst->prev_frame_num = src->prev_frame_num;
  2170. dst->short_ref_count = src->short_ref_count;
  2171. memcpy(dst->short_ref, src->short_ref, sizeof(dst->short_ref));
  2172. memcpy(dst->long_ref, src->long_ref, sizeof(dst->long_ref));
  2173. memcpy(dst->default_ref_list, src->default_ref_list, sizeof(dst->default_ref_list));
  2174. memcpy(dst->ref_list, src->ref_list, sizeof(dst->ref_list));
  2175. memcpy(dst->dequant4_coeff, src->dequant4_coeff, sizeof(src->dequant4_coeff));
  2176. memcpy(dst->dequant8_coeff, src->dequant8_coeff, sizeof(src->dequant8_coeff));
  2177. }
  2178. /**
  2179. * computes profile from profile_idc and constraint_set?_flags
  2180. *
  2181. * @param sps SPS
  2182. *
  2183. * @return profile as defined by FF_PROFILE_H264_*
  2184. */
  2185. int ff_h264_get_profile(SPS *sps)
  2186. {
  2187. int profile = sps->profile_idc;
  2188. switch(sps->profile_idc) {
  2189. case FF_PROFILE_H264_BASELINE:
  2190. // constraint_set1_flag set to 1
  2191. profile |= (sps->constraint_set_flags & 1<<1) ? FF_PROFILE_H264_CONSTRAINED : 0;
  2192. break;
  2193. case FF_PROFILE_H264_HIGH_10:
  2194. case FF_PROFILE_H264_HIGH_422:
  2195. case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
  2196. // constraint_set3_flag set to 1
  2197. profile |= (sps->constraint_set_flags & 1<<3) ? FF_PROFILE_H264_INTRA : 0;
  2198. break;
  2199. }
  2200. return profile;
  2201. }
  2202. /**
  2203. * decodes a slice header.
  2204. * This will also call MPV_common_init() and frame_start() as needed.
  2205. *
  2206. * @param h h264context
  2207. * @param h0 h264 master context (differs from 'h' when doing sliced based parallel decoding)
  2208. *
  2209. * @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded
  2210. */
  2211. static int decode_slice_header(H264Context *h, H264Context *h0){
  2212. MpegEncContext * const s = &h->s;
  2213. MpegEncContext * const s0 = &h0->s;
  2214. unsigned int first_mb_in_slice;
  2215. unsigned int pps_id;
  2216. int num_ref_idx_active_override_flag;
  2217. unsigned int slice_type, tmp, i, j;
  2218. int default_ref_list_done = 0;
  2219. int last_pic_structure;
  2220. s->dropable= h->nal_ref_idc == 0;
  2221. /* FIXME: 2tap qpel isn't implemented for high bit depth. */
  2222. if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !h->nal_ref_idc && !h->pixel_shift){
  2223. s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
  2224. s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
  2225. }else{
  2226. s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
  2227. s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
  2228. }
  2229. first_mb_in_slice= get_ue_golomb(&s->gb);
  2230. if(first_mb_in_slice == 0){ //FIXME better field boundary detection
  2231. if(h0->current_slice && FIELD_PICTURE){
  2232. field_end(h, 1);
  2233. }
  2234. h0->current_slice = 0;
  2235. if (!s0->first_field)
  2236. s->current_picture_ptr= NULL;
  2237. }
  2238. slice_type= get_ue_golomb_31(&s->gb);
  2239. if(slice_type > 9){
  2240. 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);
  2241. return -1;
  2242. }
  2243. if(slice_type > 4){
  2244. slice_type -= 5;
  2245. h->slice_type_fixed=1;
  2246. }else
  2247. h->slice_type_fixed=0;
  2248. slice_type= golomb_to_pict_type[ slice_type ];
  2249. if (slice_type == AV_PICTURE_TYPE_I
  2250. || (h0->current_slice != 0 && slice_type == h0->last_slice_type) ) {
  2251. default_ref_list_done = 1;
  2252. }
  2253. h->slice_type= slice_type;
  2254. h->slice_type_nos= slice_type & 3;
  2255. s->pict_type= h->slice_type; // to make a few old functions happy, it's wrong though
  2256. pps_id= get_ue_golomb(&s->gb);
  2257. if(pps_id>=MAX_PPS_COUNT){
  2258. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  2259. return -1;
  2260. }
  2261. if(!h0->pps_buffers[pps_id]) {
  2262. av_log(h->s.avctx, AV_LOG_ERROR, "non-existing PPS %u referenced\n", pps_id);
  2263. return -1;
  2264. }
  2265. h->pps= *h0->pps_buffers[pps_id];
  2266. if(!h0->sps_buffers[h->pps.sps_id]) {
  2267. av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS %u referenced\n", h->pps.sps_id);
  2268. return -1;
  2269. }
  2270. h->sps = *h0->sps_buffers[h->pps.sps_id];
  2271. s->avctx->profile = ff_h264_get_profile(&h->sps);
  2272. s->avctx->level = h->sps.level_idc;
  2273. s->avctx->refs = h->sps.ref_frame_count;
  2274. if(h == h0 && h->dequant_coeff_pps != pps_id){
  2275. h->dequant_coeff_pps = pps_id;
  2276. init_dequant_tables(h);
  2277. }
  2278. s->mb_width= h->sps.mb_width;
  2279. s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  2280. h->b_stride= s->mb_width*4;
  2281. s->chroma_y_shift = h->sps.chroma_format_idc <= 1; // 400 uses yuv420p
  2282. s->width = 16*s->mb_width - (2>>CHROMA444)*FFMIN(h->sps.crop_right, (8<<CHROMA444)-1);
  2283. if(h->sps.frame_mbs_only_flag)
  2284. s->height= 16*s->mb_height - (1<<s->chroma_y_shift)*FFMIN(h->sps.crop_bottom, (16>>s->chroma_y_shift)-1);
  2285. else
  2286. s->height= 16*s->mb_height - (2<<s->chroma_y_shift)*FFMIN(h->sps.crop_bottom, (16>>s->chroma_y_shift)-1);
  2287. if (s->context_initialized
  2288. && ( s->width != s->avctx->width || s->height != s->avctx->height
  2289. || av_cmp_q(h->sps.sar, s->avctx->sample_aspect_ratio))) {
  2290. if(h != h0) {
  2291. av_log_missing_feature(s->avctx, "Width/height changing with threads is", 0);
  2292. return -1; // width / height changed during parallelized decoding
  2293. }
  2294. free_tables(h, 0);
  2295. flush_dpb(s->avctx);
  2296. MPV_common_end(s);
  2297. }
  2298. if (!s->context_initialized) {
  2299. if (h != h0) {
  2300. av_log(h->s.avctx, AV_LOG_ERROR, "Cannot (re-)initialize context during parallel decoding.\n");
  2301. return -1;
  2302. }
  2303. avcodec_set_dimensions(s->avctx, s->width, s->height);
  2304. s->avctx->sample_aspect_ratio= h->sps.sar;
  2305. av_assert0(s->avctx->sample_aspect_ratio.den);
  2306. if(h->sps.video_signal_type_present_flag){
  2307. s->avctx->color_range = h->sps.full_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
  2308. if(h->sps.colour_description_present_flag){
  2309. s->avctx->color_primaries = h->sps.color_primaries;
  2310. s->avctx->color_trc = h->sps.color_trc;
  2311. s->avctx->colorspace = h->sps.colorspace;
  2312. }
  2313. }
  2314. if(h->sps.timing_info_present_flag){
  2315. int64_t den= h->sps.time_scale;
  2316. if(h->x264_build < 44U)
  2317. den *= 2;
  2318. av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
  2319. h->sps.num_units_in_tick, den, 1<<30);
  2320. }
  2321. switch (h->sps.bit_depth_luma) {
  2322. case 9 :
  2323. if (CHROMA444)
  2324. s->avctx->pix_fmt = PIX_FMT_YUV444P9;
  2325. else
  2326. s->avctx->pix_fmt = PIX_FMT_YUV420P9;
  2327. break;
  2328. case 10 :
  2329. if (CHROMA444)
  2330. s->avctx->pix_fmt = PIX_FMT_YUV444P10;
  2331. else if (CHROMA422)
  2332. s->avctx->pix_fmt = PIX_FMT_YUV422P10;
  2333. else
  2334. s->avctx->pix_fmt = PIX_FMT_YUV420P10;
  2335. break;
  2336. default:
  2337. if (CHROMA444){
  2338. s->avctx->pix_fmt = s->avctx->color_range == AVCOL_RANGE_JPEG ? PIX_FMT_YUVJ444P : PIX_FMT_YUV444P;
  2339. }else if (CHROMA422) {
  2340. s->avctx->pix_fmt = s->avctx->color_range == AVCOL_RANGE_JPEG ? PIX_FMT_YUVJ422P : PIX_FMT_YUV422P;
  2341. }else{
  2342. s->avctx->pix_fmt = s->avctx->get_format(s->avctx,
  2343. s->avctx->codec->pix_fmts ?
  2344. s->avctx->codec->pix_fmts :
  2345. s->avctx->color_range == AVCOL_RANGE_JPEG ?
  2346. hwaccel_pixfmt_list_h264_jpeg_420 :
  2347. ff_hwaccel_pixfmt_list_420);
  2348. }
  2349. }
  2350. s->avctx->hwaccel = ff_find_hwaccel(s->avctx->codec->id, s->avctx->pix_fmt);
  2351. if (MPV_common_init(s) < 0) {
  2352. av_log(h->s.avctx, AV_LOG_ERROR, "MPV_common_init() failed.\n");
  2353. return -1;
  2354. }
  2355. s->first_field = 0;
  2356. h->prev_interlaced_frame = 1;
  2357. init_scan_tables(h);
  2358. if (ff_h264_alloc_tables(h) < 0) {
  2359. av_log(h->s.avctx, AV_LOG_ERROR, "Could not allocate memory for h264\n");
  2360. return AVERROR(ENOMEM);
  2361. }
  2362. if (!HAVE_THREADS || !(s->avctx->active_thread_type&FF_THREAD_SLICE)) {
  2363. if (context_init(h) < 0) {
  2364. av_log(h->s.avctx, AV_LOG_ERROR, "context_init() failed.\n");
  2365. return -1;
  2366. }
  2367. } else {
  2368. for(i = 1; i < s->avctx->thread_count; i++) {
  2369. H264Context *c;
  2370. c = h->thread_context[i] = av_malloc(sizeof(H264Context));
  2371. memcpy(c, h->s.thread_context[i], sizeof(MpegEncContext));
  2372. memset(&c->s + 1, 0, sizeof(H264Context) - sizeof(MpegEncContext));
  2373. c->h264dsp = h->h264dsp;
  2374. c->sps = h->sps;
  2375. c->pps = h->pps;
  2376. c->pixel_shift = h->pixel_shift;
  2377. init_scan_tables(c);
  2378. clone_tables(c, h, i);
  2379. }
  2380. for(i = 0; i < s->avctx->thread_count; i++)
  2381. if (context_init(h->thread_context[i]) < 0) {
  2382. av_log(h->s.avctx, AV_LOG_ERROR, "context_init() failed.\n");
  2383. return -1;
  2384. }
  2385. }
  2386. }
  2387. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  2388. h->mb_mbaff = 0;
  2389. h->mb_aff_frame = 0;
  2390. last_pic_structure = s0->picture_structure;
  2391. if(h->sps.frame_mbs_only_flag){
  2392. s->picture_structure= PICT_FRAME;
  2393. }else{
  2394. if(get_bits1(&s->gb)) { //field_pic_flag
  2395. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  2396. } else {
  2397. s->picture_structure= PICT_FRAME;
  2398. h->mb_aff_frame = h->sps.mb_aff;
  2399. }
  2400. }
  2401. h->mb_field_decoding_flag= s->picture_structure != PICT_FRAME;
  2402. if(h0->current_slice == 0){
  2403. // Shorten frame num gaps so we don't have to allocate reference frames just to throw them away
  2404. if(h->frame_num != h->prev_frame_num) {
  2405. int unwrap_prev_frame_num = h->prev_frame_num, max_frame_num = 1<<h->sps.log2_max_frame_num;
  2406. if (unwrap_prev_frame_num > h->frame_num) unwrap_prev_frame_num -= max_frame_num;
  2407. if ((h->frame_num - unwrap_prev_frame_num) > h->sps.ref_frame_count) {
  2408. unwrap_prev_frame_num = (h->frame_num - h->sps.ref_frame_count) - 1;
  2409. if (unwrap_prev_frame_num < 0)
  2410. unwrap_prev_frame_num += max_frame_num;
  2411. h->prev_frame_num = unwrap_prev_frame_num;
  2412. }
  2413. }
  2414. while(h->frame_num != h->prev_frame_num &&
  2415. h->frame_num != (h->prev_frame_num+1)%(1<<h->sps.log2_max_frame_num)){
  2416. Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
  2417. av_log(h->s.avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n", h->frame_num, h->prev_frame_num);
  2418. if (ff_h264_frame_start(h) < 0)
  2419. return -1;
  2420. h->prev_frame_num++;
  2421. h->prev_frame_num %= 1<<h->sps.log2_max_frame_num;
  2422. s->current_picture_ptr->frame_num= h->prev_frame_num;
  2423. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, INT_MAX, 0);
  2424. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, INT_MAX, 1);
  2425. ff_generate_sliding_window_mmcos(h);
  2426. if (ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index) < 0 &&
  2427. s->avctx->error_recognition >= FF_ER_EXPLODE)
  2428. return AVERROR_INVALIDDATA;
  2429. /* Error concealment: if a ref is missing, copy the previous ref in its place.
  2430. * FIXME: avoiding a memcpy would be nice, but ref handling makes many assumptions
  2431. * about there being no actual duplicates.
  2432. * FIXME: this doesn't copy padding for out-of-frame motion vectors. Given we're
  2433. * concealing a lost frame, this probably isn't noticable by comparison, but it should
  2434. * be fixed. */
  2435. if (h->short_ref_count) {
  2436. if (prev) {
  2437. av_image_copy(h->short_ref[0]->f.data, h->short_ref[0]->f.linesize,
  2438. (const uint8_t**)prev->f.data, prev->f.linesize,
  2439. s->avctx->pix_fmt, s->mb_width*16, s->mb_height*16);
  2440. h->short_ref[0]->poc = prev->poc+2;
  2441. }
  2442. h->short_ref[0]->frame_num = h->prev_frame_num;
  2443. }
  2444. }
  2445. /* See if we have a decoded first field looking for a pair... */
  2446. if (s0->first_field) {
  2447. assert(s0->current_picture_ptr);
  2448. assert(s0->current_picture_ptr->f.data[0]);
  2449. assert(s0->current_picture_ptr->reference != DELAYED_PIC_REF);
  2450. /* figure out if we have a complementary field pair */
  2451. if (!FIELD_PICTURE || s->picture_structure == last_pic_structure) {
  2452. /*
  2453. * Previous field is unmatched. Don't display it, but let it
  2454. * remain for reference if marked as such.
  2455. */
  2456. s0->current_picture_ptr = NULL;
  2457. s0->first_field = FIELD_PICTURE;
  2458. } else {
  2459. if (h->nal_ref_idc &&
  2460. s0->current_picture_ptr->f.reference &&
  2461. s0->current_picture_ptr->frame_num != h->frame_num) {
  2462. /*
  2463. * This and previous field were reference, but had
  2464. * different frame_nums. Consider this field first in
  2465. * pair. Throw away previous field except for reference
  2466. * purposes.
  2467. */
  2468. s0->first_field = 1;
  2469. s0->current_picture_ptr = NULL;
  2470. } else {
  2471. /* Second field in complementary pair */
  2472. s0->first_field = 0;
  2473. }
  2474. }
  2475. } else {
  2476. /* Frame or first field in a potentially complementary pair */
  2477. assert(!s0->current_picture_ptr);
  2478. s0->first_field = FIELD_PICTURE;
  2479. }
  2480. if(!FIELD_PICTURE || s0->first_field) {
  2481. if (ff_h264_frame_start(h) < 0) {
  2482. s0->first_field = 0;
  2483. return -1;
  2484. }
  2485. } else {
  2486. ff_release_unused_pictures(s, 0);
  2487. }
  2488. }
  2489. if(h != h0)
  2490. clone_slice(h, h0);
  2491. s->current_picture_ptr->frame_num= h->frame_num; //FIXME frame_num cleanup
  2492. assert(s->mb_num == s->mb_width * s->mb_height);
  2493. if(first_mb_in_slice << FIELD_OR_MBAFF_PICTURE >= s->mb_num ||
  2494. first_mb_in_slice >= s->mb_num){
  2495. av_log(h->s.avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
  2496. return -1;
  2497. }
  2498. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  2499. s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << FIELD_OR_MBAFF_PICTURE;
  2500. if (s->picture_structure == PICT_BOTTOM_FIELD)
  2501. s->resync_mb_y = s->mb_y = s->mb_y + 1;
  2502. assert(s->mb_y < s->mb_height);
  2503. if(s->picture_structure==PICT_FRAME){
  2504. h->curr_pic_num= h->frame_num;
  2505. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  2506. }else{
  2507. h->curr_pic_num= 2*h->frame_num + 1;
  2508. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  2509. }
  2510. if(h->nal_unit_type == NAL_IDR_SLICE){
  2511. get_ue_golomb(&s->gb); /* idr_pic_id */
  2512. }
  2513. if(h->sps.poc_type==0){
  2514. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  2515. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  2516. h->delta_poc_bottom= get_se_golomb(&s->gb);
  2517. }
  2518. }
  2519. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  2520. h->delta_poc[0]= get_se_golomb(&s->gb);
  2521. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  2522. h->delta_poc[1]= get_se_golomb(&s->gb);
  2523. }
  2524. init_poc(h);
  2525. if(h->pps.redundant_pic_cnt_present){
  2526. h->redundant_pic_count= get_ue_golomb(&s->gb);
  2527. }
  2528. //set defaults, might be overridden a few lines later
  2529. h->ref_count[0]= h->pps.ref_count[0];
  2530. h->ref_count[1]= h->pps.ref_count[1];
  2531. if(h->slice_type_nos != AV_PICTURE_TYPE_I){
  2532. if(h->slice_type_nos == AV_PICTURE_TYPE_B){
  2533. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  2534. }
  2535. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  2536. if(num_ref_idx_active_override_flag){
  2537. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  2538. if(h->slice_type_nos==AV_PICTURE_TYPE_B)
  2539. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  2540. if(h->ref_count[0]-1 > 32-1 || h->ref_count[1]-1 > 32-1){
  2541. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  2542. h->ref_count[0]= h->ref_count[1]= 1;
  2543. return -1;
  2544. }
  2545. }
  2546. if(h->slice_type_nos == AV_PICTURE_TYPE_B)
  2547. h->list_count= 2;
  2548. else
  2549. h->list_count= 1;
  2550. }else
  2551. h->list_count= 0;
  2552. if(!default_ref_list_done){
  2553. ff_h264_fill_default_ref_list(h);
  2554. }
  2555. if(h->slice_type_nos!=AV_PICTURE_TYPE_I && ff_h264_decode_ref_pic_list_reordering(h) < 0)
  2556. return -1;
  2557. if(h->slice_type_nos!=AV_PICTURE_TYPE_I){
  2558. s->last_picture_ptr= &h->ref_list[0][0];
  2559. ff_copy_picture(&s->last_picture, s->last_picture_ptr);
  2560. }
  2561. if(h->slice_type_nos==AV_PICTURE_TYPE_B){
  2562. s->next_picture_ptr= &h->ref_list[1][0];
  2563. ff_copy_picture(&s->next_picture, s->next_picture_ptr);
  2564. }
  2565. if( (h->pps.weighted_pred && h->slice_type_nos == AV_PICTURE_TYPE_P )
  2566. || (h->pps.weighted_bipred_idc==1 && h->slice_type_nos== AV_PICTURE_TYPE_B ) )
  2567. pred_weight_table(h);
  2568. else if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== AV_PICTURE_TYPE_B){
  2569. implicit_weight_table(h, -1);
  2570. }else {
  2571. h->use_weight = 0;
  2572. for (i = 0; i < 2; i++) {
  2573. h->luma_weight_flag[i] = 0;
  2574. h->chroma_weight_flag[i] = 0;
  2575. }
  2576. }
  2577. if(h->nal_ref_idc && ff_h264_decode_ref_pic_marking(h0, &s->gb) < 0 &&
  2578. s->avctx->error_recognition >= FF_ER_EXPLODE)
  2579. return AVERROR_INVALIDDATA;
  2580. if(FRAME_MBAFF){
  2581. ff_h264_fill_mbaff_ref_list(h);
  2582. if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== AV_PICTURE_TYPE_B){
  2583. implicit_weight_table(h, 0);
  2584. implicit_weight_table(h, 1);
  2585. }
  2586. }
  2587. if(h->slice_type_nos==AV_PICTURE_TYPE_B && !h->direct_spatial_mv_pred)
  2588. ff_h264_direct_dist_scale_factor(h);
  2589. ff_h264_direct_ref_list_init(h);
  2590. if( h->slice_type_nos != AV_PICTURE_TYPE_I && h->pps.cabac ){
  2591. tmp = get_ue_golomb_31(&s->gb);
  2592. if(tmp > 2){
  2593. av_log(s->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
  2594. return -1;
  2595. }
  2596. h->cabac_init_idc= tmp;
  2597. }
  2598. h->last_qscale_diff = 0;
  2599. tmp = h->pps.init_qp + get_se_golomb(&s->gb);
  2600. if(tmp>51+6*(h->sps.bit_depth_luma-8)){
  2601. av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
  2602. return -1;
  2603. }
  2604. s->qscale= tmp;
  2605. h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
  2606. h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
  2607. //FIXME qscale / qp ... stuff
  2608. if(h->slice_type == AV_PICTURE_TYPE_SP){
  2609. get_bits1(&s->gb); /* sp_for_switch_flag */
  2610. }
  2611. if(h->slice_type==AV_PICTURE_TYPE_SP || h->slice_type == AV_PICTURE_TYPE_SI){
  2612. get_se_golomb(&s->gb); /* slice_qs_delta */
  2613. }
  2614. h->deblocking_filter = 1;
  2615. h->slice_alpha_c0_offset = 52;
  2616. h->slice_beta_offset = 52;
  2617. if( h->pps.deblocking_filter_parameters_present ) {
  2618. tmp= get_ue_golomb_31(&s->gb);
  2619. if(tmp > 2){
  2620. av_log(s->avctx, AV_LOG_ERROR, "deblocking_filter_idc %u out of range\n", tmp);
  2621. return -1;
  2622. }
  2623. h->deblocking_filter= tmp;
  2624. if(h->deblocking_filter < 2)
  2625. h->deblocking_filter^= 1; // 1<->0
  2626. if( h->deblocking_filter ) {
  2627. h->slice_alpha_c0_offset += get_se_golomb(&s->gb) << 1;
  2628. h->slice_beta_offset += get_se_golomb(&s->gb) << 1;
  2629. if( h->slice_alpha_c0_offset > 104U
  2630. || h->slice_beta_offset > 104U){
  2631. 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);
  2632. return -1;
  2633. }
  2634. }
  2635. }
  2636. if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
  2637. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type_nos != AV_PICTURE_TYPE_I)
  2638. ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type_nos == AV_PICTURE_TYPE_B)
  2639. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  2640. h->deblocking_filter= 0;
  2641. if(h->deblocking_filter == 1 && h0->max_contexts > 1) {
  2642. if(s->avctx->flags2 & CODEC_FLAG2_FAST) {
  2643. /* Cheat slightly for speed:
  2644. Do not bother to deblock across slices. */
  2645. h->deblocking_filter = 2;
  2646. } else {
  2647. h0->max_contexts = 1;
  2648. if(!h0->single_decode_warning) {
  2649. av_log(s->avctx, AV_LOG_INFO, "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
  2650. h0->single_decode_warning = 1;
  2651. }
  2652. if (h != h0) {
  2653. av_log(h->s.avctx, AV_LOG_ERROR, "Deblocking switched inside frame.\n");
  2654. return 1;
  2655. }
  2656. }
  2657. }
  2658. h->qp_thresh = 15 + 52 - FFMIN(h->slice_alpha_c0_offset, h->slice_beta_offset)
  2659. - FFMAX3(0, h->pps.chroma_qp_index_offset[0], h->pps.chroma_qp_index_offset[1])
  2660. + 6 * (h->sps.bit_depth_luma - 8);
  2661. #if 0 //FMO
  2662. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  2663. slice_group_change_cycle= get_bits(&s->gb, ?);
  2664. #endif
  2665. h0->last_slice_type = slice_type;
  2666. h->slice_num = ++h0->current_slice;
  2667. if(h->slice_num)
  2668. h0->slice_row[(h->slice_num-1)&(MAX_SLICES-1)]= s->resync_mb_y;
  2669. if ( h0->slice_row[h->slice_num&(MAX_SLICES-1)] + 3 >= s->resync_mb_y
  2670. && h0->slice_row[h->slice_num&(MAX_SLICES-1)] <= s->resync_mb_y
  2671. && h->slice_num >= MAX_SLICES) {
  2672. //in case of ASO this check needs to be updated depending on how we decide to assign slice numbers in this case
  2673. 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);
  2674. }
  2675. for(j=0; j<2; j++){
  2676. int id_list[16];
  2677. int *ref2frm= h->ref2frm[h->slice_num&(MAX_SLICES-1)][j];
  2678. for(i=0; i<16; i++){
  2679. id_list[i]= 60;
  2680. if (h->ref_list[j][i].f.data[0]) {
  2681. int k;
  2682. uint8_t *base = h->ref_list[j][i].f.base[0];
  2683. for(k=0; k<h->short_ref_count; k++)
  2684. if (h->short_ref[k]->f.base[0] == base) {
  2685. id_list[i]= k;
  2686. break;
  2687. }
  2688. for(k=0; k<h->long_ref_count; k++)
  2689. if (h->long_ref[k] && h->long_ref[k]->f.base[0] == base) {
  2690. id_list[i]= h->short_ref_count + k;
  2691. break;
  2692. }
  2693. }
  2694. }
  2695. ref2frm[0]=
  2696. ref2frm[1]= -1;
  2697. for(i=0; i<16; i++)
  2698. ref2frm[i+2]= 4*id_list[i]
  2699. + (h->ref_list[j][i].f.reference & 3);
  2700. ref2frm[18+0]=
  2701. ref2frm[18+1]= -1;
  2702. for(i=16; i<48; i++)
  2703. ref2frm[i+4]= 4*id_list[(i-16)>>1]
  2704. + (h->ref_list[j][i].f.reference & 3);
  2705. }
  2706. //FIXME: fix draw_edges+PAFF+frame threads
  2707. h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE || (!h->sps.frame_mbs_only_flag && s->avctx->active_thread_type)) ? 0 : 16;
  2708. h->emu_edge_height= (FRAME_MBAFF || FIELD_PICTURE) ? 0 : h->emu_edge_width;
  2709. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  2710. 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",
  2711. h->slice_num,
  2712. (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
  2713. first_mb_in_slice,
  2714. av_get_picture_type_char(h->slice_type), h->slice_type_fixed ? " fix" : "", h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
  2715. pps_id, h->frame_num,
  2716. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  2717. h->ref_count[0], h->ref_count[1],
  2718. s->qscale,
  2719. h->deblocking_filter, h->slice_alpha_c0_offset/2-26, h->slice_beta_offset/2-26,
  2720. h->use_weight,
  2721. h->use_weight==1 && h->use_weight_chroma ? "c" : "",
  2722. h->slice_type == AV_PICTURE_TYPE_B ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : ""
  2723. );
  2724. }
  2725. return 0;
  2726. }
  2727. int ff_h264_get_slice_type(const H264Context *h)
  2728. {
  2729. switch (h->slice_type) {
  2730. case AV_PICTURE_TYPE_P: return 0;
  2731. case AV_PICTURE_TYPE_B: return 1;
  2732. case AV_PICTURE_TYPE_I: return 2;
  2733. case AV_PICTURE_TYPE_SP: return 3;
  2734. case AV_PICTURE_TYPE_SI: return 4;
  2735. default: return -1;
  2736. }
  2737. }
  2738. static av_always_inline void fill_filter_caches_inter(H264Context *h, MpegEncContext * const s, int mb_type, int top_xy,
  2739. int left_xy[LEFT_MBS], int top_type, int left_type[LEFT_MBS], int mb_xy, int list)
  2740. {
  2741. int b_stride = h->b_stride;
  2742. int16_t (*mv_dst)[2] = &h->mv_cache[list][scan8[0]];
  2743. int8_t *ref_cache = &h->ref_cache[list][scan8[0]];
  2744. if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
  2745. if(USES_LIST(top_type, list)){
  2746. const int b_xy= h->mb2b_xy[top_xy] + 3*b_stride;
  2747. const int b8_xy= 4*top_xy + 2;
  2748. int (*ref2frm)[64] = h->ref2frm[ h->slice_table[top_xy]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
  2749. AV_COPY128(mv_dst - 1*8, s->current_picture.f.motion_val[list][b_xy + 0]);
  2750. ref_cache[0 - 1*8]=
  2751. ref_cache[1 - 1*8]= ref2frm[list][s->current_picture.f.ref_index[list][b8_xy + 0]];
  2752. ref_cache[2 - 1*8]=
  2753. ref_cache[3 - 1*8]= ref2frm[list][s->current_picture.f.ref_index[list][b8_xy + 1]];
  2754. }else{
  2755. AV_ZERO128(mv_dst - 1*8);
  2756. AV_WN32A(&ref_cache[0 - 1*8], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2757. }
  2758. if(!IS_INTERLACED(mb_type^left_type[LTOP])){
  2759. if(USES_LIST(left_type[LTOP], list)){
  2760. const int b_xy= h->mb2b_xy[left_xy[LTOP]] + 3;
  2761. const int b8_xy= 4*left_xy[LTOP] + 1;
  2762. int (*ref2frm)[64] = h->ref2frm[ h->slice_table[left_xy[LTOP]]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
  2763. AV_COPY32(mv_dst - 1 + 0, s->current_picture.f.motion_val[list][b_xy + b_stride*0]);
  2764. AV_COPY32(mv_dst - 1 + 8, s->current_picture.f.motion_val[list][b_xy + b_stride*1]);
  2765. AV_COPY32(mv_dst - 1 + 16, s->current_picture.f.motion_val[list][b_xy + b_stride*2]);
  2766. AV_COPY32(mv_dst - 1 + 24, s->current_picture.f.motion_val[list][b_xy + b_stride*3]);
  2767. ref_cache[-1 + 0]=
  2768. ref_cache[-1 + 8]= ref2frm[list][s->current_picture.f.ref_index[list][b8_xy + 2*0]];
  2769. ref_cache[-1 + 16]=
  2770. ref_cache[-1 + 24]= ref2frm[list][s->current_picture.f.ref_index[list][b8_xy + 2*1]];
  2771. }else{
  2772. AV_ZERO32(mv_dst - 1 + 0);
  2773. AV_ZERO32(mv_dst - 1 + 8);
  2774. AV_ZERO32(mv_dst - 1 +16);
  2775. AV_ZERO32(mv_dst - 1 +24);
  2776. ref_cache[-1 + 0]=
  2777. ref_cache[-1 + 8]=
  2778. ref_cache[-1 + 16]=
  2779. ref_cache[-1 + 24]= LIST_NOT_USED;
  2780. }
  2781. }
  2782. }
  2783. if(!USES_LIST(mb_type, list)){
  2784. fill_rectangle(mv_dst, 4, 4, 8, pack16to32(0,0), 4);
  2785. AV_WN32A(&ref_cache[0*8], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2786. AV_WN32A(&ref_cache[1*8], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2787. AV_WN32A(&ref_cache[2*8], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2788. AV_WN32A(&ref_cache[3*8], ((LIST_NOT_USED)&0xFF)*0x01010101u);
  2789. return;
  2790. }
  2791. {
  2792. int8_t *ref = &s->current_picture.f.ref_index[list][4*mb_xy];
  2793. int (*ref2frm)[64] = h->ref2frm[ h->slice_num&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
  2794. uint32_t ref01 = (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101;
  2795. uint32_t ref23 = (pack16to32(ref2frm[list][ref[2]],ref2frm[list][ref[3]])&0x00FF00FF)*0x0101;
  2796. AV_WN32A(&ref_cache[0*8], ref01);
  2797. AV_WN32A(&ref_cache[1*8], ref01);
  2798. AV_WN32A(&ref_cache[2*8], ref23);
  2799. AV_WN32A(&ref_cache[3*8], ref23);
  2800. }
  2801. {
  2802. int16_t (*mv_src)[2] = &s->current_picture.f.motion_val[list][4*s->mb_x + 4*s->mb_y*b_stride];
  2803. AV_COPY128(mv_dst + 8*0, mv_src + 0*b_stride);
  2804. AV_COPY128(mv_dst + 8*1, mv_src + 1*b_stride);
  2805. AV_COPY128(mv_dst + 8*2, mv_src + 2*b_stride);
  2806. AV_COPY128(mv_dst + 8*3, mv_src + 3*b_stride);
  2807. }
  2808. }
  2809. /**
  2810. *
  2811. * @return non zero if the loop filter can be skiped
  2812. */
  2813. static int fill_filter_caches(H264Context *h, int mb_type){
  2814. MpegEncContext * const s = &h->s;
  2815. const int mb_xy= h->mb_xy;
  2816. int top_xy, left_xy[LEFT_MBS];
  2817. int top_type, left_type[LEFT_MBS];
  2818. uint8_t *nnz;
  2819. uint8_t *nnz_cache;
  2820. top_xy = mb_xy - (s->mb_stride << MB_FIELD);
  2821. /* Wow, what a mess, why didn't they simplify the interlacing & intra
  2822. * stuff, I can't imagine that these complex rules are worth it. */
  2823. left_xy[LBOT] = left_xy[LTOP] = mb_xy-1;
  2824. if(FRAME_MBAFF){
  2825. const int left_mb_field_flag = IS_INTERLACED(s->current_picture.f.mb_type[mb_xy - 1]);
  2826. const int curr_mb_field_flag = IS_INTERLACED(mb_type);
  2827. if(s->mb_y&1){
  2828. if (left_mb_field_flag != curr_mb_field_flag) {
  2829. left_xy[LTOP] -= s->mb_stride;
  2830. }
  2831. }else{
  2832. if(curr_mb_field_flag){
  2833. top_xy += s->mb_stride & (((s->current_picture.f.mb_type[top_xy] >> 7) & 1) - 1);
  2834. }
  2835. if (left_mb_field_flag != curr_mb_field_flag) {
  2836. left_xy[LBOT] += s->mb_stride;
  2837. }
  2838. }
  2839. }
  2840. h->top_mb_xy = top_xy;
  2841. h->left_mb_xy[LTOP] = left_xy[LTOP];
  2842. h->left_mb_xy[LBOT] = left_xy[LBOT];
  2843. {
  2844. //for sufficiently low qp, filtering wouldn't do anything
  2845. //this is a conservative estimate: could also check beta_offset and more accurate chroma_qp
  2846. int qp_thresh = h->qp_thresh; //FIXME strictly we should store qp_thresh for each mb of a slice
  2847. int qp = s->current_picture.f.qscale_table[mb_xy];
  2848. if(qp <= qp_thresh
  2849. && (left_xy[LTOP] < 0 || ((qp + s->current_picture.f.qscale_table[left_xy[LTOP]] + 1) >> 1) <= qp_thresh)
  2850. && (top_xy < 0 || ((qp + s->current_picture.f.qscale_table[top_xy ] + 1) >> 1) <= qp_thresh)) {
  2851. if(!FRAME_MBAFF)
  2852. return 1;
  2853. if ((left_xy[LTOP] < 0 || ((qp + s->current_picture.f.qscale_table[left_xy[LBOT] ] + 1) >> 1) <= qp_thresh) &&
  2854. (top_xy < s->mb_stride || ((qp + s->current_picture.f.qscale_table[top_xy - s->mb_stride] + 1) >> 1) <= qp_thresh))
  2855. return 1;
  2856. }
  2857. }
  2858. top_type = s->current_picture.f.mb_type[top_xy];
  2859. left_type[LTOP] = s->current_picture.f.mb_type[left_xy[LTOP]];
  2860. left_type[LBOT] = s->current_picture.f.mb_type[left_xy[LBOT]];
  2861. if(h->deblocking_filter == 2){
  2862. if(h->slice_table[top_xy ] != h->slice_num) top_type= 0;
  2863. if(h->slice_table[left_xy[LBOT]] != h->slice_num) left_type[LTOP]= left_type[LBOT]= 0;
  2864. }else{
  2865. if(h->slice_table[top_xy ] == 0xFFFF) top_type= 0;
  2866. if(h->slice_table[left_xy[LBOT]] == 0xFFFF) left_type[LTOP]= left_type[LBOT] =0;
  2867. }
  2868. h->top_type = top_type;
  2869. h->left_type[LTOP]= left_type[LTOP];
  2870. h->left_type[LBOT]= left_type[LBOT];
  2871. if(IS_INTRA(mb_type))
  2872. return 0;
  2873. fill_filter_caches_inter(h, s, mb_type, top_xy, left_xy, top_type, left_type, mb_xy, 0);
  2874. if(h->list_count == 2)
  2875. fill_filter_caches_inter(h, s, mb_type, top_xy, left_xy, top_type, left_type, mb_xy, 1);
  2876. nnz = h->non_zero_count[mb_xy];
  2877. nnz_cache = h->non_zero_count_cache;
  2878. AV_COPY32(&nnz_cache[4+8*1], &nnz[ 0]);
  2879. AV_COPY32(&nnz_cache[4+8*2], &nnz[ 4]);
  2880. AV_COPY32(&nnz_cache[4+8*3], &nnz[ 8]);
  2881. AV_COPY32(&nnz_cache[4+8*4], &nnz[12]);
  2882. h->cbp= h->cbp_table[mb_xy];
  2883. if(top_type){
  2884. nnz = h->non_zero_count[top_xy];
  2885. AV_COPY32(&nnz_cache[4+8*0], &nnz[3*4]);
  2886. }
  2887. if(left_type[LTOP]){
  2888. nnz = h->non_zero_count[left_xy[LTOP]];
  2889. nnz_cache[3+8*1]= nnz[3+0*4];
  2890. nnz_cache[3+8*2]= nnz[3+1*4];
  2891. nnz_cache[3+8*3]= nnz[3+2*4];
  2892. nnz_cache[3+8*4]= nnz[3+3*4];
  2893. }
  2894. // CAVLC 8x8dct requires NNZ values for residual decoding that differ from what the loop filter needs
  2895. if(!CABAC && h->pps.transform_8x8_mode){
  2896. if(IS_8x8DCT(top_type)){
  2897. nnz_cache[4+8*0]=
  2898. nnz_cache[5+8*0]= (h->cbp_table[top_xy] & 0x4000) >> 12;
  2899. nnz_cache[6+8*0]=
  2900. nnz_cache[7+8*0]= (h->cbp_table[top_xy] & 0x8000) >> 12;
  2901. }
  2902. if(IS_8x8DCT(left_type[LTOP])){
  2903. nnz_cache[3+8*1]=
  2904. nnz_cache[3+8*2]= (h->cbp_table[left_xy[LTOP]]&0x2000) >> 12; //FIXME check MBAFF
  2905. }
  2906. if(IS_8x8DCT(left_type[LBOT])){
  2907. nnz_cache[3+8*3]=
  2908. nnz_cache[3+8*4]= (h->cbp_table[left_xy[LBOT]]&0x8000) >> 12; //FIXME check MBAFF
  2909. }
  2910. if(IS_8x8DCT(mb_type)){
  2911. nnz_cache[scan8[0 ]]= nnz_cache[scan8[1 ]]=
  2912. nnz_cache[scan8[2 ]]= nnz_cache[scan8[3 ]]= (h->cbp & 0x1000) >> 12;
  2913. nnz_cache[scan8[0+ 4]]= nnz_cache[scan8[1+ 4]]=
  2914. nnz_cache[scan8[2+ 4]]= nnz_cache[scan8[3+ 4]]= (h->cbp & 0x2000) >> 12;
  2915. nnz_cache[scan8[0+ 8]]= nnz_cache[scan8[1+ 8]]=
  2916. nnz_cache[scan8[2+ 8]]= nnz_cache[scan8[3+ 8]]= (h->cbp & 0x4000) >> 12;
  2917. nnz_cache[scan8[0+12]]= nnz_cache[scan8[1+12]]=
  2918. nnz_cache[scan8[2+12]]= nnz_cache[scan8[3+12]]= (h->cbp & 0x8000) >> 12;
  2919. }
  2920. }
  2921. return 0;
  2922. }
  2923. static void loop_filter(H264Context *h, int start_x, int end_x){
  2924. MpegEncContext * const s = &h->s;
  2925. uint8_t *dest_y, *dest_cb, *dest_cr;
  2926. int linesize, uvlinesize, mb_x, mb_y;
  2927. const int end_mb_y= s->mb_y + FRAME_MBAFF;
  2928. const int old_slice_type= h->slice_type;
  2929. const int pixel_shift = h->pixel_shift;
  2930. const int block_h = 16>>s->chroma_y_shift;
  2931. if(h->deblocking_filter) {
  2932. for(mb_x= start_x; mb_x<end_x; mb_x++){
  2933. for(mb_y=end_mb_y - FRAME_MBAFF; mb_y<= end_mb_y; mb_y++){
  2934. int mb_xy, mb_type;
  2935. mb_xy = h->mb_xy = mb_x + mb_y*s->mb_stride;
  2936. h->slice_num= h->slice_table[mb_xy];
  2937. mb_type = s->current_picture.f.mb_type[mb_xy];
  2938. h->list_count= h->list_counts[mb_xy];
  2939. if(FRAME_MBAFF)
  2940. h->mb_mbaff = h->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
  2941. s->mb_x= mb_x;
  2942. s->mb_y= mb_y;
  2943. dest_y = s->current_picture.f.data[0] + ((mb_x << pixel_shift) + mb_y * s->linesize ) * 16;
  2944. dest_cb = s->current_picture.f.data[1] + (mb_x << pixel_shift)*(8<<CHROMA444) + mb_y * s->uvlinesize * block_h;
  2945. dest_cr = s->current_picture.f.data[2] + (mb_x << pixel_shift)*(8<<CHROMA444) + mb_y * s->uvlinesize * block_h;
  2946. //FIXME simplify above
  2947. if (MB_FIELD) {
  2948. linesize = h->mb_linesize = s->linesize * 2;
  2949. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  2950. if(mb_y&1){ //FIXME move out of this function?
  2951. dest_y -= s->linesize*15;
  2952. dest_cb-= s->uvlinesize*(block_h-1);
  2953. dest_cr-= s->uvlinesize*(block_h-1);
  2954. }
  2955. } else {
  2956. linesize = h->mb_linesize = s->linesize;
  2957. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  2958. }
  2959. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
  2960. if(fill_filter_caches(h, mb_type))
  2961. continue;
  2962. h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.f.qscale_table[mb_xy]);
  2963. h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.f.qscale_table[mb_xy]);
  2964. if (FRAME_MBAFF) {
  2965. ff_h264_filter_mb (h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  2966. } else {
  2967. ff_h264_filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  2968. }
  2969. }
  2970. }
  2971. }
  2972. h->slice_type= old_slice_type;
  2973. s->mb_x= end_x;
  2974. s->mb_y= end_mb_y - FRAME_MBAFF;
  2975. h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
  2976. h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
  2977. }
  2978. static void predict_field_decoding_flag(H264Context *h){
  2979. MpegEncContext * const s = &h->s;
  2980. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2981. int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
  2982. ? s->current_picture.f.mb_type[mb_xy - 1]
  2983. : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
  2984. ? s->current_picture.f.mb_type[mb_xy - s->mb_stride]
  2985. : 0;
  2986. h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  2987. }
  2988. /**
  2989. * Draw edges and report progress for the last MB row.
  2990. */
  2991. static void decode_finish_row(H264Context *h){
  2992. MpegEncContext * const s = &h->s;
  2993. int top = 16*(s->mb_y >> FIELD_PICTURE);
  2994. int height = 16 << FRAME_MBAFF;
  2995. int deblock_border = (16 + 4) << FRAME_MBAFF;
  2996. int pic_height = 16*s->mb_height >> FIELD_PICTURE;
  2997. if (h->deblocking_filter) {
  2998. if((top + height) >= pic_height)
  2999. height += deblock_border;
  3000. top -= deblock_border;
  3001. }
  3002. if (top >= pic_height || (top + height) < h->emu_edge_height)
  3003. return;
  3004. height = FFMIN(height, pic_height - top);
  3005. if (top < h->emu_edge_height) {
  3006. height = top+height;
  3007. top = 0;
  3008. }
  3009. ff_draw_horiz_band(s, top, height);
  3010. if (s->dropable) return;
  3011. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, top + height - 1,
  3012. s->picture_structure==PICT_BOTTOM_FIELD);
  3013. }
  3014. static int decode_slice(struct AVCodecContext *avctx, void *arg){
  3015. H264Context *h = *(void**)arg;
  3016. MpegEncContext * const s = &h->s;
  3017. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  3018. int lf_x_start = s->mb_x;
  3019. s->mb_skip_run= -1;
  3020. h->is_complex = FRAME_MBAFF || s->picture_structure != PICT_FRAME || s->codec_id != CODEC_ID_H264 ||
  3021. (CONFIG_GRAY && (s->flags&CODEC_FLAG_GRAY));
  3022. if( h->pps.cabac ) {
  3023. /* realign */
  3024. align_get_bits( &s->gb );
  3025. /* init cabac */
  3026. ff_init_cabac_states( &h->cabac);
  3027. ff_init_cabac_decoder( &h->cabac,
  3028. s->gb.buffer + get_bits_count(&s->gb)/8,
  3029. (get_bits_left(&s->gb) + 7)/8);
  3030. ff_h264_init_cabac_states(h);
  3031. for(;;){
  3032. //START_TIMER
  3033. int ret = ff_h264_decode_mb_cabac(h);
  3034. int eos;
  3035. //STOP_TIMER("decode_mb_cabac")
  3036. if(ret>=0) ff_h264_hl_decode_mb(h);
  3037. if( ret >= 0 && FRAME_MBAFF ) { //FIXME optimal? or let mb_decode decode 16x32 ?
  3038. s->mb_y++;
  3039. ret = ff_h264_decode_mb_cabac(h);
  3040. if(ret>=0) ff_h264_hl_decode_mb(h);
  3041. s->mb_y--;
  3042. }
  3043. eos = get_cabac_terminate( &h->cabac );
  3044. if((s->workaround_bugs & FF_BUG_TRUNCATED) && h->cabac.bytestream > h->cabac.bytestream_end + 2){
  3045. 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);
  3046. if (s->mb_x >= lf_x_start) loop_filter(h, lf_x_start, s->mb_x + 1);
  3047. return 0;
  3048. }
  3049. if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
  3050. 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);
  3051. 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);
  3052. return -1;
  3053. }
  3054. if( ++s->mb_x >= s->mb_width ) {
  3055. loop_filter(h, lf_x_start, s->mb_x);
  3056. s->mb_x = lf_x_start = 0;
  3057. decode_finish_row(h);
  3058. ++s->mb_y;
  3059. if(FIELD_OR_MBAFF_PICTURE) {
  3060. ++s->mb_y;
  3061. if(FRAME_MBAFF && s->mb_y < s->mb_height)
  3062. predict_field_decoding_flag(h);
  3063. }
  3064. }
  3065. if( eos || s->mb_y >= s->mb_height ) {
  3066. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  3067. 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);
  3068. if (s->mb_x > lf_x_start) loop_filter(h, lf_x_start, s->mb_x);
  3069. return 0;
  3070. }
  3071. }
  3072. } else {
  3073. for(;;){
  3074. int ret = ff_h264_decode_mb_cavlc(h);
  3075. if(ret>=0) ff_h264_hl_decode_mb(h);
  3076. if(ret>=0 && FRAME_MBAFF){ //FIXME optimal? or let mb_decode decode 16x32 ?
  3077. s->mb_y++;
  3078. ret = ff_h264_decode_mb_cavlc(h);
  3079. if(ret>=0) ff_h264_hl_decode_mb(h);
  3080. s->mb_y--;
  3081. }
  3082. if(ret<0){
  3083. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  3084. 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);
  3085. return -1;
  3086. }
  3087. if(++s->mb_x >= s->mb_width){
  3088. loop_filter(h, lf_x_start, s->mb_x);
  3089. s->mb_x = lf_x_start = 0;
  3090. decode_finish_row(h);
  3091. ++s->mb_y;
  3092. if(FIELD_OR_MBAFF_PICTURE) {
  3093. ++s->mb_y;
  3094. if(FRAME_MBAFF && s->mb_y < s->mb_height)
  3095. predict_field_decoding_flag(h);
  3096. }
  3097. if(s->mb_y >= s->mb_height){
  3098. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  3099. if( get_bits_count(&s->gb) == s->gb.size_in_bits
  3100. || get_bits_count(&s->gb) < s->gb.size_in_bits && s->avctx->error_recognition < FF_ER_AGGRESSIVE) {
  3101. 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);
  3102. return 0;
  3103. }else{
  3104. 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);
  3105. return -1;
  3106. }
  3107. }
  3108. }
  3109. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  3110. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  3111. if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
  3112. 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);
  3113. if (s->mb_x > lf_x_start) loop_filter(h, lf_x_start, s->mb_x);
  3114. return 0;
  3115. }else{
  3116. 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);
  3117. return -1;
  3118. }
  3119. }
  3120. }
  3121. }
  3122. }
  3123. /**
  3124. * Call decode_slice() for each context.
  3125. *
  3126. * @param h h264 master context
  3127. * @param context_count number of contexts to execute
  3128. */
  3129. static int execute_decode_slices(H264Context *h, int context_count){
  3130. MpegEncContext * const s = &h->s;
  3131. AVCodecContext * const avctx= s->avctx;
  3132. H264Context *hx;
  3133. int i;
  3134. if (s->avctx->hwaccel || s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  3135. return 0;
  3136. if(context_count == 1) {
  3137. return decode_slice(avctx, &h);
  3138. } else {
  3139. for(i = 1; i < context_count; i++) {
  3140. hx = h->thread_context[i];
  3141. hx->s.error_recognition = avctx->error_recognition;
  3142. hx->s.error_count = 0;
  3143. hx->x264_build= h->x264_build;
  3144. }
  3145. avctx->execute(avctx, (void *)decode_slice,
  3146. h->thread_context, NULL, context_count, sizeof(void*));
  3147. /* pull back stuff from slices to master context */
  3148. hx = h->thread_context[context_count - 1];
  3149. s->mb_x = hx->s.mb_x;
  3150. s->mb_y = hx->s.mb_y;
  3151. s->dropable = hx->s.dropable;
  3152. s->picture_structure = hx->s.picture_structure;
  3153. for(i = 1; i < context_count; i++)
  3154. h->s.error_count += h->thread_context[i]->s.error_count;
  3155. }
  3156. return 0;
  3157. }
  3158. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size){
  3159. MpegEncContext * const s = &h->s;
  3160. AVCodecContext * const avctx= s->avctx;
  3161. H264Context *hx; ///< thread context
  3162. int buf_index;
  3163. int context_count;
  3164. int next_avc;
  3165. int pass = !(avctx->active_thread_type & FF_THREAD_FRAME);
  3166. int nals_needed=0; ///< number of NALs that need decoding before the next frame thread starts
  3167. int nal_index;
  3168. h->max_contexts = (HAVE_THREADS && (s->avctx->active_thread_type&FF_THREAD_SLICE)) ? avctx->thread_count : 1;
  3169. if(!(s->flags2 & CODEC_FLAG2_CHUNKS)){
  3170. h->current_slice = 0;
  3171. if (!s->first_field)
  3172. s->current_picture_ptr= NULL;
  3173. ff_h264_reset_sei(h);
  3174. }
  3175. for(;pass <= 1;pass++){
  3176. buf_index = 0;
  3177. context_count = 0;
  3178. next_avc = h->is_avc ? 0 : buf_size;
  3179. nal_index = 0;
  3180. for(;;){
  3181. int consumed;
  3182. int dst_length;
  3183. int bit_length;
  3184. const uint8_t *ptr;
  3185. int i, nalsize = 0;
  3186. int err;
  3187. if(buf_index >= next_avc) {
  3188. if(buf_index >= buf_size) break;
  3189. nalsize = 0;
  3190. for(i = 0; i < h->nal_length_size; i++)
  3191. nalsize = (nalsize << 8) | buf[buf_index++];
  3192. if(nalsize <= 0 || nalsize > buf_size - buf_index){
  3193. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  3194. break;
  3195. }
  3196. next_avc= buf_index + nalsize;
  3197. } else {
  3198. // start code prefix search
  3199. for(; buf_index + 3 < next_avc; buf_index++){
  3200. // This should always succeed in the first iteration.
  3201. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  3202. break;
  3203. }
  3204. if(buf_index+3 >= buf_size) break;
  3205. buf_index+=3;
  3206. if(buf_index >= next_avc) continue;
  3207. }
  3208. hx = h->thread_context[context_count];
  3209. ptr= ff_h264_decode_nal(hx, buf + buf_index, &dst_length, &consumed, next_avc - buf_index);
  3210. if (ptr==NULL || dst_length < 0){
  3211. return -1;
  3212. }
  3213. i= buf_index + consumed;
  3214. if((s->workaround_bugs & FF_BUG_AUTODETECT) && i+3<next_avc &&
  3215. buf[i]==0x00 && buf[i+1]==0x00 && buf[i+2]==0x01 && buf[i+3]==0xE0)
  3216. s->workaround_bugs |= FF_BUG_TRUNCATED;
  3217. if(!(s->workaround_bugs & FF_BUG_TRUNCATED)){
  3218. while(dst_length > 0 && ptr[dst_length - 1] == 0)
  3219. dst_length--;
  3220. }
  3221. bit_length= !dst_length ? 0 : (8*dst_length - ff_h264_decode_rbsp_trailing(h, ptr + dst_length - 1));
  3222. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  3223. 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);
  3224. }
  3225. if (h->is_avc && (nalsize != consumed) && nalsize){
  3226. av_log(h->s.avctx, AV_LOG_DEBUG, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
  3227. }
  3228. buf_index += consumed;
  3229. nal_index++;
  3230. if(pass == 0) {
  3231. // packets can sometimes contain multiple PPS/SPS
  3232. // e.g. two PAFF field pictures in one packet, or a demuxer which splits NALs strangely
  3233. // if so, when frame threading we can't start the next thread until we've read all of them
  3234. switch (hx->nal_unit_type) {
  3235. case NAL_SPS:
  3236. case NAL_PPS:
  3237. case NAL_IDR_SLICE:
  3238. case NAL_SLICE:
  3239. nals_needed = nal_index;
  3240. }
  3241. continue;
  3242. }
  3243. //FIXME do not discard SEI id
  3244. if(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0)
  3245. continue;
  3246. again:
  3247. err = 0;
  3248. switch(hx->nal_unit_type){
  3249. case NAL_IDR_SLICE:
  3250. if (h->nal_unit_type != NAL_IDR_SLICE) {
  3251. av_log(h->s.avctx, AV_LOG_ERROR, "Invalid mix of idr and non-idr slices");
  3252. return -1;
  3253. }
  3254. idr(h); //FIXME ensure we don't loose some frames if there is reordering
  3255. case NAL_SLICE:
  3256. init_get_bits(&hx->s.gb, ptr, bit_length);
  3257. hx->intra_gb_ptr=
  3258. hx->inter_gb_ptr= &hx->s.gb;
  3259. hx->s.data_partitioning = 0;
  3260. if((err = decode_slice_header(hx, h)))
  3261. break;
  3262. if (h->sei_recovery_frame_cnt >= 0) {
  3263. h->recovery_frame = (h->frame_num + h->sei_recovery_frame_cnt) %
  3264. (1 << h->sps.log2_max_frame_num);
  3265. }
  3266. s->current_picture_ptr->f.key_frame |=
  3267. (hx->nal_unit_type == NAL_IDR_SLICE);
  3268. if (h->recovery_frame == h->frame_num) {
  3269. s->current_picture_ptr->f.key_frame |= 1;
  3270. h->recovery_frame = -1;
  3271. }
  3272. h->sync |= !!s->current_picture_ptr->f.key_frame;
  3273. h->sync |= 3*!!(s->flags2 & CODEC_FLAG2_SHOW_ALL);
  3274. s->current_picture_ptr->sync = h->sync;
  3275. if (h->current_slice == 1) {
  3276. if(!(s->flags2 & CODEC_FLAG2_CHUNKS)) {
  3277. decode_postinit(h, nal_index >= nals_needed);
  3278. }
  3279. if (s->avctx->hwaccel && s->avctx->hwaccel->start_frame(s->avctx, NULL, 0) < 0)
  3280. return -1;
  3281. if(CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  3282. ff_vdpau_h264_picture_start(s);
  3283. }
  3284. if(hx->redundant_pic_count==0
  3285. && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
  3286. && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=AV_PICTURE_TYPE_B)
  3287. && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==AV_PICTURE_TYPE_I)
  3288. && avctx->skip_frame < AVDISCARD_ALL){
  3289. if(avctx->hwaccel) {
  3290. if (avctx->hwaccel->decode_slice(avctx, &buf[buf_index - consumed], consumed) < 0)
  3291. return -1;
  3292. }else
  3293. if(CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
  3294. static const uint8_t start_code[] = {0x00, 0x00, 0x01};
  3295. ff_vdpau_add_data_chunk(s, start_code, sizeof(start_code));
  3296. ff_vdpau_add_data_chunk(s, &buf[buf_index - consumed], consumed );
  3297. }else
  3298. context_count++;
  3299. }
  3300. break;
  3301. case NAL_DPA:
  3302. init_get_bits(&hx->s.gb, ptr, bit_length);
  3303. hx->intra_gb_ptr=
  3304. hx->inter_gb_ptr= NULL;
  3305. if ((err = decode_slice_header(hx, h)) < 0)
  3306. break;
  3307. hx->s.data_partitioning = 1;
  3308. break;
  3309. case NAL_DPB:
  3310. init_get_bits(&hx->intra_gb, ptr, bit_length);
  3311. hx->intra_gb_ptr= &hx->intra_gb;
  3312. break;
  3313. case NAL_DPC:
  3314. init_get_bits(&hx->inter_gb, ptr, bit_length);
  3315. hx->inter_gb_ptr= &hx->inter_gb;
  3316. if(hx->redundant_pic_count==0 && hx->intra_gb_ptr && hx->s.data_partitioning
  3317. && s->context_initialized
  3318. && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
  3319. && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=AV_PICTURE_TYPE_B)
  3320. && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==AV_PICTURE_TYPE_I)
  3321. && avctx->skip_frame < AVDISCARD_ALL)
  3322. context_count++;
  3323. break;
  3324. case NAL_SEI:
  3325. init_get_bits(&s->gb, ptr, bit_length);
  3326. ff_h264_decode_sei(h);
  3327. break;
  3328. case NAL_SPS:
  3329. init_get_bits(&s->gb, ptr, bit_length);
  3330. ff_h264_decode_seq_parameter_set(h);
  3331. if (s->flags& CODEC_FLAG_LOW_DELAY ||
  3332. (h->sps.bitstream_restriction_flag && !h->sps.num_reorder_frames))
  3333. s->low_delay=1;
  3334. if(avctx->has_b_frames < 2)
  3335. avctx->has_b_frames= !s->low_delay;
  3336. if (avctx->bits_per_raw_sample != h->sps.bit_depth_luma ||
  3337. h->cur_chroma_format_idc != h->sps.chroma_format_idc) {
  3338. if (h->sps.bit_depth_luma >= 8 && h->sps.bit_depth_luma <= 10) {
  3339. avctx->bits_per_raw_sample = h->sps.bit_depth_luma;
  3340. h->cur_chroma_format_idc = h->sps.chroma_format_idc;
  3341. h->pixel_shift = h->sps.bit_depth_luma > 8;
  3342. ff_h264dsp_init(&h->h264dsp, h->sps.bit_depth_luma, h->sps.chroma_format_idc);
  3343. ff_h264_pred_init(&h->hpc, s->codec_id, h->sps.bit_depth_luma, h->sps.chroma_format_idc);
  3344. s->dsp.dct_bits = h->sps.bit_depth_luma > 8 ? 32 : 16;
  3345. dsputil_init(&s->dsp, s->avctx);
  3346. } else {
  3347. av_log(avctx, AV_LOG_DEBUG, "Unsupported bit depth: %d\n", h->sps.bit_depth_luma);
  3348. return -1;
  3349. }
  3350. }
  3351. break;
  3352. case NAL_PPS:
  3353. init_get_bits(&s->gb, ptr, bit_length);
  3354. ff_h264_decode_picture_parameter_set(h, bit_length);
  3355. break;
  3356. case NAL_AUD:
  3357. case NAL_END_SEQUENCE:
  3358. case NAL_END_STREAM:
  3359. case NAL_FILLER_DATA:
  3360. case NAL_SPS_EXT:
  3361. case NAL_AUXILIARY_SLICE:
  3362. break;
  3363. default:
  3364. av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n", hx->nal_unit_type, bit_length);
  3365. }
  3366. if(context_count == h->max_contexts) {
  3367. execute_decode_slices(h, context_count);
  3368. context_count = 0;
  3369. }
  3370. if (err < 0)
  3371. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  3372. else if(err == 1) {
  3373. /* Slice could not be decoded in parallel mode, copy down
  3374. * NAL unit stuff to context 0 and restart. Note that
  3375. * rbsp_buffer is not transferred, but since we no longer
  3376. * run in parallel mode this should not be an issue. */
  3377. h->nal_unit_type = hx->nal_unit_type;
  3378. h->nal_ref_idc = hx->nal_ref_idc;
  3379. hx = h;
  3380. goto again;
  3381. }
  3382. }
  3383. }
  3384. if(context_count)
  3385. execute_decode_slices(h, context_count);
  3386. return buf_index;
  3387. }
  3388. /**
  3389. * returns the number of bytes consumed for building the current frame
  3390. */
  3391. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  3392. if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
  3393. if(pos+10>buf_size) pos=buf_size; // oops ;)
  3394. return pos;
  3395. }
  3396. static int decode_frame(AVCodecContext *avctx,
  3397. void *data, int *data_size,
  3398. AVPacket *avpkt)
  3399. {
  3400. const uint8_t *buf = avpkt->data;
  3401. int buf_size = avpkt->size;
  3402. H264Context *h = avctx->priv_data;
  3403. MpegEncContext *s = &h->s;
  3404. AVFrame *pict = data;
  3405. int buf_index;
  3406. s->flags= avctx->flags;
  3407. s->flags2= avctx->flags2;
  3408. /* end of stream, output what is still in the buffers */
  3409. out:
  3410. if (buf_size == 0) {
  3411. Picture *out;
  3412. int i, out_idx;
  3413. s->current_picture_ptr = NULL;
  3414. //FIXME factorize this with the output code below
  3415. out = h->delayed_pic[0];
  3416. out_idx = 0;
  3417. for (i = 1; h->delayed_pic[i] && !h->delayed_pic[i]->f.key_frame && !h->delayed_pic[i]->mmco_reset; i++)
  3418. if(h->delayed_pic[i]->poc < out->poc){
  3419. out = h->delayed_pic[i];
  3420. out_idx = i;
  3421. }
  3422. for(i=out_idx; h->delayed_pic[i]; i++)
  3423. h->delayed_pic[i] = h->delayed_pic[i+1];
  3424. if(out){
  3425. *data_size = sizeof(AVFrame);
  3426. *pict= *(AVFrame*)out;
  3427. }
  3428. return 0;
  3429. }
  3430. buf_index=decode_nal_units(h, buf, buf_size);
  3431. if(buf_index < 0)
  3432. return -1;
  3433. if (!s->current_picture_ptr && h->nal_unit_type == NAL_END_SEQUENCE) {
  3434. buf_size = 0;
  3435. goto out;
  3436. }
  3437. if(!(s->flags2 & CODEC_FLAG2_CHUNKS) && !s->current_picture_ptr){
  3438. if (avctx->skip_frame >= AVDISCARD_NONREF)
  3439. return 0;
  3440. av_log(avctx, AV_LOG_ERROR, "no frame!\n");
  3441. return -1;
  3442. }
  3443. if(!(s->flags2 & CODEC_FLAG2_CHUNKS) || (s->mb_y >= s->mb_height && s->mb_height)){
  3444. if(s->flags2 & CODEC_FLAG2_CHUNKS) decode_postinit(h, 1);
  3445. field_end(h, 0);
  3446. *data_size = 0; /* Wait for second field. */
  3447. if (h->next_output_pic && h->next_output_pic->sync) {
  3448. if(h->sync>1 || h->next_output_pic->f.pict_type != AV_PICTURE_TYPE_B){
  3449. *data_size = sizeof(AVFrame);
  3450. *pict = *(AVFrame*)h->next_output_pic;
  3451. }
  3452. }
  3453. }
  3454. assert(pict->data[0] || !*data_size);
  3455. ff_print_debug_info(s, pict);
  3456. //printf("out %d\n", (int)pict->data[0]);
  3457. return get_consumed_bytes(s, buf_index, buf_size);
  3458. }
  3459. #if 0
  3460. static inline void fill_mb_avail(H264Context *h){
  3461. MpegEncContext * const s = &h->s;
  3462. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  3463. if(s->mb_y){
  3464. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  3465. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  3466. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  3467. }else{
  3468. h->mb_avail[0]=
  3469. h->mb_avail[1]=
  3470. h->mb_avail[2]= 0;
  3471. }
  3472. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  3473. h->mb_avail[4]= 1; //FIXME move out
  3474. h->mb_avail[5]= 0; //FIXME move out
  3475. }
  3476. #endif
  3477. #ifdef TEST
  3478. #undef printf
  3479. #undef random
  3480. #define COUNT 8000
  3481. #define SIZE (COUNT*40)
  3482. extern AVCodec ff_h264_decoder;
  3483. int main(void){
  3484. int i;
  3485. uint8_t temp[SIZE];
  3486. PutBitContext pb;
  3487. GetBitContext gb;
  3488. // int int_temp[10000];
  3489. DSPContext dsp;
  3490. AVCodecContext avctx;
  3491. avcodec_get_context_defaults3(&avctx, &ff_h264_decoder);
  3492. dsputil_init(&dsp, &avctx);
  3493. init_put_bits(&pb, temp, SIZE);
  3494. printf("testing unsigned exp golomb\n");
  3495. for(i=0; i<COUNT; i++){
  3496. START_TIMER
  3497. set_ue_golomb(&pb, i);
  3498. STOP_TIMER("set_ue_golomb");
  3499. }
  3500. flush_put_bits(&pb);
  3501. init_get_bits(&gb, temp, 8*SIZE);
  3502. for(i=0; i<COUNT; i++){
  3503. int j, s;
  3504. s= show_bits(&gb, 24);
  3505. START_TIMER
  3506. j= get_ue_golomb(&gb);
  3507. if(j != i){
  3508. printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  3509. // return -1;
  3510. }
  3511. STOP_TIMER("get_ue_golomb");
  3512. }
  3513. init_put_bits(&pb, temp, SIZE);
  3514. printf("testing signed exp golomb\n");
  3515. for(i=0; i<COUNT; i++){
  3516. START_TIMER
  3517. set_se_golomb(&pb, i - COUNT/2);
  3518. STOP_TIMER("set_se_golomb");
  3519. }
  3520. flush_put_bits(&pb);
  3521. init_get_bits(&gb, temp, 8*SIZE);
  3522. for(i=0; i<COUNT; i++){
  3523. int j, s;
  3524. s= show_bits(&gb, 24);
  3525. START_TIMER
  3526. j= get_se_golomb(&gb);
  3527. if(j != i - COUNT/2){
  3528. printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  3529. // return -1;
  3530. }
  3531. STOP_TIMER("get_se_golomb");
  3532. }
  3533. printf("Testing RBSP\n");
  3534. return 0;
  3535. }
  3536. #endif /* TEST */
  3537. av_cold void ff_h264_free_context(H264Context *h)
  3538. {
  3539. int i;
  3540. free_tables(h, 1); //FIXME cleanup init stuff perhaps
  3541. for(i = 0; i < MAX_SPS_COUNT; i++)
  3542. av_freep(h->sps_buffers + i);
  3543. for(i = 0; i < MAX_PPS_COUNT; i++)
  3544. av_freep(h->pps_buffers + i);
  3545. }
  3546. av_cold int ff_h264_decode_end(AVCodecContext *avctx)
  3547. {
  3548. H264Context *h = avctx->priv_data;
  3549. MpegEncContext *s = &h->s;
  3550. ff_h264_free_context(h);
  3551. MPV_common_end(s);
  3552. // memset(h, 0, sizeof(H264Context));
  3553. return 0;
  3554. }
  3555. static const AVProfile profiles[] = {
  3556. { FF_PROFILE_H264_BASELINE, "Baseline" },
  3557. { FF_PROFILE_H264_CONSTRAINED_BASELINE, "Constrained Baseline" },
  3558. { FF_PROFILE_H264_MAIN, "Main" },
  3559. { FF_PROFILE_H264_EXTENDED, "Extended" },
  3560. { FF_PROFILE_H264_HIGH, "High" },
  3561. { FF_PROFILE_H264_HIGH_10, "High 10" },
  3562. { FF_PROFILE_H264_HIGH_10_INTRA, "High 10 Intra" },
  3563. { FF_PROFILE_H264_HIGH_422, "High 4:2:2" },
  3564. { FF_PROFILE_H264_HIGH_422_INTRA, "High 4:2:2 Intra" },
  3565. { FF_PROFILE_H264_HIGH_444, "High 4:4:4" },
  3566. { FF_PROFILE_H264_HIGH_444_PREDICTIVE, "High 4:4:4 Predictive" },
  3567. { FF_PROFILE_H264_HIGH_444_INTRA, "High 4:4:4 Intra" },
  3568. { FF_PROFILE_H264_CAVLC_444, "CAVLC 4:4:4" },
  3569. { FF_PROFILE_UNKNOWN },
  3570. };
  3571. AVCodec ff_h264_decoder = {
  3572. .name = "h264",
  3573. .type = AVMEDIA_TYPE_VIDEO,
  3574. .id = CODEC_ID_H264,
  3575. .priv_data_size = sizeof(H264Context),
  3576. .init = ff_h264_decode_init,
  3577. .close = ff_h264_decode_end,
  3578. .decode = decode_frame,
  3579. .capabilities = /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_DELAY |
  3580. CODEC_CAP_SLICE_THREADS | CODEC_CAP_FRAME_THREADS,
  3581. .flush= flush_dpb,
  3582. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
  3583. .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
  3584. .update_thread_context = ONLY_IF_THREADS_ENABLED(decode_update_thread_context),
  3585. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  3586. };
  3587. #if CONFIG_H264_VDPAU_DECODER
  3588. AVCodec ff_h264_vdpau_decoder = {
  3589. .name = "h264_vdpau",
  3590. .type = AVMEDIA_TYPE_VIDEO,
  3591. .id = CODEC_ID_H264,
  3592. .priv_data_size = sizeof(H264Context),
  3593. .init = ff_h264_decode_init,
  3594. .close = ff_h264_decode_end,
  3595. .decode = decode_frame,
  3596. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
  3597. .flush= flush_dpb,
  3598. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),
  3599. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_VDPAU_H264, PIX_FMT_NONE},
  3600. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  3601. };
  3602. #endif