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.

4225 lines
161KB

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