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.

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