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.

4016 lines
153KB

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