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.

4047 lines
155KB

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