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.

3157 lines
115KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... encoder/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 libavcodec/h264.c
  23. * H.264 / AVC / MPEG4 part10 codec.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include "internal.h"
  27. #include "dsputil.h"
  28. #include "avcodec.h"
  29. #include "mpegvideo.h"
  30. #include "h264.h"
  31. #include "h264data.h"
  32. #include "h264_mvpred.h"
  33. #include "h264_parser.h"
  34. #include "golomb.h"
  35. #include "mathops.h"
  36. #include "rectangle.h"
  37. #include "vdpau_internal.h"
  38. #include "cabac.h"
  39. //#undef NDEBUG
  40. #include <assert.h>
  41. static const uint8_t rem6[52]={
  42. 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,
  43. };
  44. static const uint8_t div6[52]={
  45. 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,
  46. };
  47. void ff_h264_write_back_intra_pred_mode(H264Context *h){
  48. const int mb_xy= h->mb_xy;
  49. h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
  50. h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
  51. h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
  52. h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
  53. h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
  54. h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
  55. h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
  56. }
  57. /**
  58. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  59. */
  60. int ff_h264_check_intra4x4_pred_mode(H264Context *h){
  61. MpegEncContext * const s = &h->s;
  62. static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
  63. static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
  64. int i;
  65. if(!(h->top_samples_available&0x8000)){
  66. for(i=0; i<4; i++){
  67. int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
  68. if(status<0){
  69. 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);
  70. return -1;
  71. } else if(status){
  72. h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
  73. }
  74. }
  75. }
  76. if((h->left_samples_available&0x8888)!=0x8888){
  77. static const int mask[4]={0x8000,0x2000,0x80,0x20};
  78. for(i=0; i<4; i++){
  79. if(!(h->left_samples_available&mask[i])){
  80. int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
  81. if(status<0){
  82. 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);
  83. return -1;
  84. } else if(status){
  85. h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
  86. }
  87. }
  88. }
  89. }
  90. return 0;
  91. } //FIXME cleanup like ff_h264_check_intra_pred_mode
  92. /**
  93. * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
  94. */
  95. int ff_h264_check_intra_pred_mode(H264Context *h, int mode){
  96. MpegEncContext * const s = &h->s;
  97. static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
  98. static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
  99. if(mode > 6U) {
  100. 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);
  101. return -1;
  102. }
  103. if(!(h->top_samples_available&0x8000)){
  104. mode= top[ mode ];
  105. if(mode<0){
  106. 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);
  107. return -1;
  108. }
  109. }
  110. if((h->left_samples_available&0x8080) != 0x8080){
  111. mode= left[ mode ];
  112. if(h->left_samples_available&0x8080){ //mad cow disease mode, aka MBAFF + constrained_intra_pred
  113. mode= ALZHEIMER_DC_L0T_PRED8x8 + (!(h->left_samples_available&0x8000)) + 2*(mode == DC_128_PRED8x8);
  114. }
  115. if(mode<0){
  116. 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);
  117. return -1;
  118. }
  119. }
  120. return mode;
  121. }
  122. const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src, int *dst_length, int *consumed, int length){
  123. int i, si, di;
  124. uint8_t *dst;
  125. int bufidx;
  126. // src[0]&0x80; //forbidden bit
  127. h->nal_ref_idc= src[0]>>5;
  128. h->nal_unit_type= src[0]&0x1F;
  129. src++; length--;
  130. #if 0
  131. for(i=0; i<length; i++)
  132. printf("%2X ", src[i]);
  133. #endif
  134. #if HAVE_FAST_UNALIGNED
  135. # if HAVE_FAST_64BIT
  136. # define RS 7
  137. for(i=0; i+1<length; i+=9){
  138. if(!((~AV_RN64A(src+i) & (AV_RN64A(src+i) - 0x0100010001000101ULL)) & 0x8000800080008080ULL))
  139. # else
  140. # define RS 3
  141. for(i=0; i+1<length; i+=5){
  142. if(!((~AV_RN32A(src+i) & (AV_RN32A(src+i) - 0x01000101U)) & 0x80008080U))
  143. # endif
  144. continue;
  145. if(i>0 && !src[i]) i--;
  146. while(src[i]) i++;
  147. #else
  148. # define RS 0
  149. for(i=0; i+1<length; i+=2){
  150. if(src[i]) continue;
  151. if(i>0 && src[i-1]==0) i--;
  152. #endif
  153. if(i+2<length && src[i+1]==0 && src[i+2]<=3){
  154. if(src[i+2]!=3){
  155. /* startcode, so we must be past the end */
  156. length=i;
  157. }
  158. break;
  159. }
  160. i-= RS;
  161. }
  162. if(i>=length-1){ //no escaped 0
  163. *dst_length= length;
  164. *consumed= length+1; //+1 for the header
  165. return src;
  166. }
  167. bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0; // use second escape buffer for inter data
  168. av_fast_malloc(&h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx], length+FF_INPUT_BUFFER_PADDING_SIZE);
  169. dst= h->rbsp_buffer[bufidx];
  170. if (dst == NULL){
  171. return NULL;
  172. }
  173. //printf("decoding esc\n");
  174. memcpy(dst, src, i);
  175. si=di=i;
  176. while(si+2<length){
  177. //remove escapes (very rare 1:2^22)
  178. if(src[si+2]>3){
  179. dst[di++]= src[si++];
  180. dst[di++]= src[si++];
  181. }else if(src[si]==0 && src[si+1]==0){
  182. if(src[si+2]==3){ //escape
  183. dst[di++]= 0;
  184. dst[di++]= 0;
  185. si+=3;
  186. continue;
  187. }else //next start code
  188. goto nsc;
  189. }
  190. dst[di++]= src[si++];
  191. }
  192. while(si<length)
  193. dst[di++]= src[si++];
  194. nsc:
  195. memset(dst+di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  196. *dst_length= di;
  197. *consumed= si + 1;//+1 for the header
  198. //FIXME store exact number of bits in the getbitcontext (it is needed for decoding)
  199. return dst;
  200. }
  201. int ff_h264_decode_rbsp_trailing(H264Context *h, const uint8_t *src){
  202. int v= *src;
  203. int r;
  204. tprintf(h->s.avctx, "rbsp trailing %X\n", v);
  205. for(r=1; r<9; r++){
  206. if(v&1) return r;
  207. v>>=1;
  208. }
  209. return 0;
  210. }
  211. /**
  212. * IDCT transforms the 16 dc values and dequantizes them.
  213. * @param qp quantization parameter
  214. */
  215. static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  216. #define stride 16
  217. int i;
  218. int temp[16]; //FIXME check if this is a good idea
  219. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  220. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  221. //memset(block, 64, 2*256);
  222. //return;
  223. for(i=0; i<4; i++){
  224. const int offset= y_offset[i];
  225. const int z0= block[offset+stride*0] + block[offset+stride*4];
  226. const int z1= block[offset+stride*0] - block[offset+stride*4];
  227. const int z2= block[offset+stride*1] - block[offset+stride*5];
  228. const int z3= block[offset+stride*1] + block[offset+stride*5];
  229. temp[4*i+0]= z0+z3;
  230. temp[4*i+1]= z1+z2;
  231. temp[4*i+2]= z1-z2;
  232. temp[4*i+3]= z0-z3;
  233. }
  234. for(i=0; i<4; i++){
  235. const int offset= x_offset[i];
  236. const int z0= temp[4*0+i] + temp[4*2+i];
  237. const int z1= temp[4*0+i] - temp[4*2+i];
  238. const int z2= temp[4*1+i] - temp[4*3+i];
  239. const int z3= temp[4*1+i] + temp[4*3+i];
  240. block[stride*0 +offset]= ((((z0 + z3)*qmul + 128 ) >> 8)); //FIXME think about merging this into decode_residual
  241. block[stride*2 +offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
  242. block[stride*8 +offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
  243. block[stride*10+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
  244. }
  245. }
  246. #if 0
  247. /**
  248. * DCT transforms the 16 dc values.
  249. * @param qp quantization parameter ??? FIXME
  250. */
  251. static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
  252. // const int qmul= dequant_coeff[qp][0];
  253. int i;
  254. int temp[16]; //FIXME check if this is a good idea
  255. static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
  256. static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
  257. for(i=0; i<4; i++){
  258. const int offset= y_offset[i];
  259. const int z0= block[offset+stride*0] + block[offset+stride*4];
  260. const int z1= block[offset+stride*0] - block[offset+stride*4];
  261. const int z2= block[offset+stride*1] - block[offset+stride*5];
  262. const int z3= block[offset+stride*1] + block[offset+stride*5];
  263. temp[4*i+0]= z0+z3;
  264. temp[4*i+1]= z1+z2;
  265. temp[4*i+2]= z1-z2;
  266. temp[4*i+3]= z0-z3;
  267. }
  268. for(i=0; i<4; i++){
  269. const int offset= x_offset[i];
  270. const int z0= temp[4*0+i] + temp[4*2+i];
  271. const int z1= temp[4*0+i] - temp[4*2+i];
  272. const int z2= temp[4*1+i] - temp[4*3+i];
  273. const int z3= temp[4*1+i] + temp[4*3+i];
  274. block[stride*0 +offset]= (z0 + z3)>>1;
  275. block[stride*2 +offset]= (z1 + z2)>>1;
  276. block[stride*8 +offset]= (z1 - z2)>>1;
  277. block[stride*10+offset]= (z0 - z3)>>1;
  278. }
  279. }
  280. #endif
  281. #undef xStride
  282. #undef stride
  283. static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
  284. const int stride= 16*2;
  285. const int xStride= 16;
  286. int a,b,c,d,e;
  287. a= block[stride*0 + xStride*0];
  288. b= block[stride*0 + xStride*1];
  289. c= block[stride*1 + xStride*0];
  290. d= block[stride*1 + xStride*1];
  291. e= a-b;
  292. a= a+b;
  293. b= c-d;
  294. c= c+d;
  295. block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
  296. block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
  297. block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
  298. block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;
  299. }
  300. #if 0
  301. static void chroma_dc_dct_c(DCTELEM *block){
  302. const int stride= 16*2;
  303. const int xStride= 16;
  304. int a,b,c,d,e;
  305. a= block[stride*0 + xStride*0];
  306. b= block[stride*0 + xStride*1];
  307. c= block[stride*1 + xStride*0];
  308. d= block[stride*1 + xStride*1];
  309. e= a-b;
  310. a= a+b;
  311. b= c-d;
  312. c= c+d;
  313. block[stride*0 + xStride*0]= (a+c);
  314. block[stride*0 + xStride*1]= (e+b);
  315. block[stride*1 + xStride*0]= (a-c);
  316. block[stride*1 + xStride*1]= (e-b);
  317. }
  318. #endif
  319. static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
  320. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  321. int src_x_offset, int src_y_offset,
  322. qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
  323. MpegEncContext * const s = &h->s;
  324. const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
  325. int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
  326. const int luma_xy= (mx&3) + ((my&3)<<2);
  327. uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->mb_linesize;
  328. uint8_t * src_cb, * src_cr;
  329. int extra_width= h->emu_edge_width;
  330. int extra_height= h->emu_edge_height;
  331. int emu=0;
  332. const int full_mx= mx>>2;
  333. const int full_my= my>>2;
  334. const int pic_width = 16*s->mb_width;
  335. const int pic_height = 16*s->mb_height >> MB_FIELD;
  336. if(mx&7) extra_width -= 3;
  337. if(my&7) extra_height -= 3;
  338. if( full_mx < 0-extra_width
  339. || full_my < 0-extra_height
  340. || full_mx + 16/*FIXME*/ > pic_width + extra_width
  341. || full_my + 16/*FIXME*/ > pic_height + extra_height){
  342. ff_emulated_edge_mc(s->edge_emu_buffer, src_y - 2 - 2*h->mb_linesize, h->mb_linesize, 16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height);
  343. src_y= s->edge_emu_buffer + 2 + 2*h->mb_linesize;
  344. emu=1;
  345. }
  346. qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); //FIXME try variable height perhaps?
  347. if(!square){
  348. qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
  349. }
  350. if(CONFIG_GRAY && s->flags&CODEC_FLAG_GRAY) return;
  351. if(MB_FIELD){
  352. // chroma offset when predicting from a field of opposite parity
  353. my += 2 * ((s->mb_y & 1) - (pic->reference - 1));
  354. emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
  355. }
  356. src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  357. src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
  358. if(emu){
  359. ff_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);
  360. src_cb= s->edge_emu_buffer;
  361. }
  362. chroma_op(dest_cb, src_cb, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  363. if(emu){
  364. ff_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);
  365. src_cr= s->edge_emu_buffer;
  366. }
  367. chroma_op(dest_cr, src_cr, h->mb_uvlinesize, chroma_height, mx&7, my&7);
  368. }
  369. static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
  370. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  371. int x_offset, int y_offset,
  372. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  373. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  374. int list0, int list1){
  375. MpegEncContext * const s = &h->s;
  376. qpel_mc_func *qpix_op= qpix_put;
  377. h264_chroma_mc_func chroma_op= chroma_put;
  378. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  379. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  380. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  381. x_offset += 8*s->mb_x;
  382. y_offset += 8*(s->mb_y >> MB_FIELD);
  383. if(list0){
  384. Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
  385. mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
  386. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  387. qpix_op, chroma_op);
  388. qpix_op= qpix_avg;
  389. chroma_op= chroma_avg;
  390. }
  391. if(list1){
  392. Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
  393. mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
  394. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  395. qpix_op, chroma_op);
  396. }
  397. }
  398. static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
  399. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  400. int x_offset, int y_offset,
  401. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  402. h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
  403. h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
  404. int list0, int list1){
  405. MpegEncContext * const s = &h->s;
  406. dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
  407. dest_cb += x_offset + y_offset*h->mb_uvlinesize;
  408. dest_cr += x_offset + y_offset*h->mb_uvlinesize;
  409. x_offset += 8*s->mb_x;
  410. y_offset += 8*(s->mb_y >> MB_FIELD);
  411. if(list0 && list1){
  412. /* don't optimize for luma-only case, since B-frames usually
  413. * use implicit weights => chroma too. */
  414. uint8_t *tmp_cb = s->obmc_scratchpad;
  415. uint8_t *tmp_cr = s->obmc_scratchpad + 8;
  416. uint8_t *tmp_y = s->obmc_scratchpad + 8*h->mb_uvlinesize;
  417. int refn0 = h->ref_cache[0][ scan8[n] ];
  418. int refn1 = h->ref_cache[1][ scan8[n] ];
  419. mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
  420. dest_y, dest_cb, dest_cr,
  421. x_offset, y_offset, qpix_put, chroma_put);
  422. mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
  423. tmp_y, tmp_cb, tmp_cr,
  424. x_offset, y_offset, qpix_put, chroma_put);
  425. if(h->use_weight == 2){
  426. int weight0 = h->implicit_weight[refn0][refn1];
  427. int weight1 = 64 - weight0;
  428. luma_weight_avg( dest_y, tmp_y, h-> mb_linesize, 5, weight0, weight1, 0);
  429. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, 5, weight0, weight1, 0);
  430. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, 5, weight0, weight1, 0);
  431. }else{
  432. luma_weight_avg(dest_y, tmp_y, h->mb_linesize, h->luma_log2_weight_denom,
  433. h->luma_weight[0][refn0], h->luma_weight[1][refn1],
  434. h->luma_offset[0][refn0] + h->luma_offset[1][refn1]);
  435. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  436. h->chroma_weight[0][refn0][0], h->chroma_weight[1][refn1][0],
  437. h->chroma_offset[0][refn0][0] + h->chroma_offset[1][refn1][0]);
  438. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  439. h->chroma_weight[0][refn0][1], h->chroma_weight[1][refn1][1],
  440. h->chroma_offset[0][refn0][1] + h->chroma_offset[1][refn1][1]);
  441. }
  442. }else{
  443. int list = list1 ? 1 : 0;
  444. int refn = h->ref_cache[list][ scan8[n] ];
  445. Picture *ref= &h->ref_list[list][refn];
  446. mc_dir_part(h, ref, n, square, chroma_height, delta, list,
  447. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  448. qpix_put, chroma_put);
  449. luma_weight_op(dest_y, h->mb_linesize, h->luma_log2_weight_denom,
  450. h->luma_weight[list][refn], h->luma_offset[list][refn]);
  451. if(h->use_weight_chroma){
  452. chroma_weight_op(dest_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  453. h->chroma_weight[list][refn][0], h->chroma_offset[list][refn][0]);
  454. chroma_weight_op(dest_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
  455. h->chroma_weight[list][refn][1], h->chroma_offset[list][refn][1]);
  456. }
  457. }
  458. }
  459. static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
  460. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  461. int x_offset, int y_offset,
  462. qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
  463. qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
  464. h264_weight_func *weight_op, h264_biweight_func *weight_avg,
  465. int list0, int list1){
  466. if((h->use_weight==2 && list0 && list1
  467. && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ] != 32))
  468. || h->use_weight==1)
  469. mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  470. x_offset, y_offset, qpix_put, chroma_put,
  471. weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
  472. else
  473. mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
  474. x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
  475. }
  476. static inline void prefetch_motion(H264Context *h, int list){
  477. /* fetch pixels for estimated mv 4 macroblocks ahead
  478. * optimized for 64byte cache lines */
  479. MpegEncContext * const s = &h->s;
  480. const int refn = h->ref_cache[list][scan8[0]];
  481. if(refn >= 0){
  482. const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
  483. const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
  484. uint8_t **src= h->ref_list[list][refn].data;
  485. int off= mx + (my + (s->mb_x&3)*4)*h->mb_linesize + 64;
  486. s->dsp.prefetch(src[0]+off, s->linesize, 4);
  487. off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
  488. s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
  489. }
  490. }
  491. static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  492. qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
  493. qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
  494. h264_weight_func *weight_op, h264_biweight_func *weight_avg){
  495. MpegEncContext * const s = &h->s;
  496. const int mb_xy= h->mb_xy;
  497. const int mb_type= s->current_picture.mb_type[mb_xy];
  498. assert(IS_INTER(mb_type));
  499. prefetch_motion(h, 0);
  500. if(IS_16X16(mb_type)){
  501. mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
  502. qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
  503. weight_op, weight_avg,
  504. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  505. }else if(IS_16X8(mb_type)){
  506. mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
  507. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  508. &weight_op[1], &weight_avg[1],
  509. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  510. mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
  511. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  512. &weight_op[1], &weight_avg[1],
  513. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  514. }else if(IS_8X16(mb_type)){
  515. mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
  516. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  517. &weight_op[2], &weight_avg[2],
  518. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
  519. mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
  520. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  521. &weight_op[2], &weight_avg[2],
  522. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
  523. }else{
  524. int i;
  525. assert(IS_8X8(mb_type));
  526. for(i=0; i<4; i++){
  527. const int sub_mb_type= h->sub_mb_type[i];
  528. const int n= 4*i;
  529. int x_offset= (i&1)<<2;
  530. int y_offset= (i&2)<<1;
  531. if(IS_SUB_8X8(sub_mb_type)){
  532. mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  533. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  534. &weight_op[3], &weight_avg[3],
  535. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  536. }else if(IS_SUB_8X4(sub_mb_type)){
  537. mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  538. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  539. &weight_op[4], &weight_avg[4],
  540. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  541. mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
  542. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  543. &weight_op[4], &weight_avg[4],
  544. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  545. }else if(IS_SUB_4X8(sub_mb_type)){
  546. mc_part(h, n , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
  547. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  548. &weight_op[5], &weight_avg[5],
  549. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  550. mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
  551. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  552. &weight_op[5], &weight_avg[5],
  553. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  554. }else{
  555. int j;
  556. assert(IS_SUB_4X4(sub_mb_type));
  557. for(j=0; j<4; j++){
  558. int sub_x_offset= x_offset + 2*(j&1);
  559. int sub_y_offset= y_offset + (j&2);
  560. mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
  561. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  562. &weight_op[6], &weight_avg[6],
  563. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
  564. }
  565. }
  566. }
  567. }
  568. prefetch_motion(h, 1);
  569. }
  570. static void free_tables(H264Context *h){
  571. int i;
  572. H264Context *hx;
  573. av_freep(&h->intra4x4_pred_mode);
  574. av_freep(&h->chroma_pred_mode_table);
  575. av_freep(&h->cbp_table);
  576. av_freep(&h->mvd_table[0]);
  577. av_freep(&h->mvd_table[1]);
  578. av_freep(&h->direct_table);
  579. av_freep(&h->non_zero_count);
  580. av_freep(&h->slice_table_base);
  581. h->slice_table= NULL;
  582. av_freep(&h->list_counts);
  583. av_freep(&h->mb2b_xy);
  584. av_freep(&h->mb2br_xy);
  585. av_freep(&h->mb2b8_xy);
  586. for(i = 0; i < MAX_THREADS; i++) {
  587. hx = h->thread_context[i];
  588. if(!hx) continue;
  589. av_freep(&hx->top_borders[1]);
  590. av_freep(&hx->top_borders[0]);
  591. av_freep(&hx->s.obmc_scratchpad);
  592. av_freep(&hx->rbsp_buffer[1]);
  593. av_freep(&hx->rbsp_buffer[0]);
  594. hx->rbsp_buffer_size[0] = 0;
  595. hx->rbsp_buffer_size[1] = 0;
  596. if (i) av_freep(&h->thread_context[i]);
  597. }
  598. }
  599. static void init_dequant8_coeff_table(H264Context *h){
  600. int i,q,x;
  601. const int transpose = (h->s.dsp.h264_idct8_add != ff_h264_idct8_add_c); //FIXME ugly
  602. h->dequant8_coeff[0] = h->dequant8_buffer[0];
  603. h->dequant8_coeff[1] = h->dequant8_buffer[1];
  604. for(i=0; i<2; i++ ){
  605. if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
  606. h->dequant8_coeff[1] = h->dequant8_buffer[0];
  607. break;
  608. }
  609. for(q=0; q<52; q++){
  610. int shift = div6[q];
  611. int idx = rem6[q];
  612. for(x=0; x<64; x++)
  613. h->dequant8_coeff[i][q][transpose ? (x>>3)|((x&7)<<3) : x] =
  614. ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
  615. h->pps.scaling_matrix8[i][x]) << shift;
  616. }
  617. }
  618. }
  619. static void init_dequant4_coeff_table(H264Context *h){
  620. int i,j,q,x;
  621. const int transpose = (h->s.dsp.h264_idct_add != ff_h264_idct_add_c); //FIXME ugly
  622. for(i=0; i<6; i++ ){
  623. h->dequant4_coeff[i] = h->dequant4_buffer[i];
  624. for(j=0; j<i; j++){
  625. if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
  626. h->dequant4_coeff[i] = h->dequant4_buffer[j];
  627. break;
  628. }
  629. }
  630. if(j<i)
  631. continue;
  632. for(q=0; q<52; q++){
  633. int shift = div6[q] + 2;
  634. int idx = rem6[q];
  635. for(x=0; x<16; x++)
  636. h->dequant4_coeff[i][q][transpose ? (x>>2)|((x<<2)&0xF) : x] =
  637. ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
  638. h->pps.scaling_matrix4[i][x]) << shift;
  639. }
  640. }
  641. }
  642. static void init_dequant_tables(H264Context *h){
  643. int i,x;
  644. init_dequant4_coeff_table(h);
  645. if(h->pps.transform_8x8_mode)
  646. init_dequant8_coeff_table(h);
  647. if(h->sps.transform_bypass){
  648. for(i=0; i<6; i++)
  649. for(x=0; x<16; x++)
  650. h->dequant4_coeff[i][0][x] = 1<<6;
  651. if(h->pps.transform_8x8_mode)
  652. for(i=0; i<2; i++)
  653. for(x=0; x<64; x++)
  654. h->dequant8_coeff[i][0][x] = 1<<6;
  655. }
  656. }
  657. int ff_h264_alloc_tables(H264Context *h){
  658. MpegEncContext * const s = &h->s;
  659. const int big_mb_num= s->mb_stride * (s->mb_height+1);
  660. int x,y;
  661. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t), fail)
  662. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->non_zero_count , big_mb_num * 32 * sizeof(uint8_t), fail)
  663. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->slice_table_base , (big_mb_num+s->mb_stride) * sizeof(*h->slice_table_base), fail)
  664. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->cbp_table, big_mb_num * sizeof(uint16_t), fail)
  665. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t), fail)
  666. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[0], 16*big_mb_num * sizeof(uint8_t), fail);
  667. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[1], 16*big_mb_num * sizeof(uint8_t), fail);
  668. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->direct_table, 32*big_mb_num * sizeof(uint8_t) , fail);
  669. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->list_counts, big_mb_num * sizeof(uint8_t), fail)
  670. memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride) * sizeof(*h->slice_table_base));
  671. h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
  672. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2b_xy , big_mb_num * sizeof(uint32_t), fail);
  673. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2br_xy , big_mb_num * sizeof(uint32_t), fail);
  674. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2b8_xy , big_mb_num * sizeof(uint32_t), fail);
  675. for(y=0; y<s->mb_height; y++){
  676. for(x=0; x<s->mb_width; x++){
  677. const int mb_xy= x + y*s->mb_stride;
  678. const int b_xy = 4*x + 4*y*h->b_stride;
  679. const int b8_xy= 2*x + 2*y*h->b8_stride;
  680. h->mb2b_xy [mb_xy]= b_xy;
  681. h->mb2br_xy[mb_xy]= 8*(FMO ? mb_xy : (mb_xy % (2*s->mb_stride)));
  682. h->mb2b8_xy[mb_xy]= b8_xy;
  683. }
  684. }
  685. s->obmc_scratchpad = NULL;
  686. if(!h->dequant4_coeff[0])
  687. init_dequant_tables(h);
  688. return 0;
  689. fail:
  690. free_tables(h);
  691. return -1;
  692. }
  693. /**
  694. * Mimic alloc_tables(), but for every context thread.
  695. */
  696. static void clone_tables(H264Context *dst, H264Context *src){
  697. dst->intra4x4_pred_mode = src->intra4x4_pred_mode;
  698. dst->non_zero_count = src->non_zero_count;
  699. dst->slice_table = src->slice_table;
  700. dst->cbp_table = src->cbp_table;
  701. dst->mb2b_xy = src->mb2b_xy;
  702. dst->mb2br_xy = src->mb2br_xy;
  703. dst->mb2b8_xy = src->mb2b8_xy;
  704. dst->chroma_pred_mode_table = src->chroma_pred_mode_table;
  705. dst->mvd_table[0] = src->mvd_table[0];
  706. dst->mvd_table[1] = src->mvd_table[1];
  707. dst->direct_table = src->direct_table;
  708. dst->list_counts = src->list_counts;
  709. dst->s.obmc_scratchpad = NULL;
  710. ff_h264_pred_init(&dst->hpc, src->s.codec_id);
  711. }
  712. /**
  713. * Init context
  714. * Allocate buffers which are not shared amongst multiple threads.
  715. */
  716. static int context_init(H264Context *h){
  717. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[0], h->s.mb_width * (16+8+8) * sizeof(uint8_t), fail)
  718. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[1], h->s.mb_width * (16+8+8) * sizeof(uint8_t), fail)
  719. return 0;
  720. fail:
  721. return -1; // free_tables will clean up for us
  722. }
  723. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size);
  724. static av_cold void common_init(H264Context *h){
  725. MpegEncContext * const s = &h->s;
  726. s->width = s->avctx->width;
  727. s->height = s->avctx->height;
  728. s->codec_id= s->avctx->codec->id;
  729. ff_h264_pred_init(&h->hpc, s->codec_id);
  730. h->dequant_coeff_pps= -1;
  731. s->unrestricted_mv=1;
  732. s->decode=1; //FIXME
  733. dsputil_init(&s->dsp, s->avctx); // needed so that idct permutation is known early
  734. memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
  735. memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
  736. }
  737. av_cold int ff_h264_decode_init(AVCodecContext *avctx){
  738. H264Context *h= avctx->priv_data;
  739. MpegEncContext * const s = &h->s;
  740. MPV_decode_defaults(s);
  741. s->avctx = avctx;
  742. common_init(h);
  743. s->out_format = FMT_H264;
  744. s->workaround_bugs= avctx->workaround_bugs;
  745. // set defaults
  746. // s->decode_mb= ff_h263_decode_mb;
  747. s->quarter_sample = 1;
  748. if(!avctx->has_b_frames)
  749. s->low_delay= 1;
  750. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  751. ff_h264_decode_init_vlc();
  752. h->thread_context[0] = h;
  753. h->outputed_poc = INT_MIN;
  754. h->prev_poc_msb= 1<<16;
  755. h->x264_build = -1;
  756. ff_h264_reset_sei(h);
  757. if(avctx->codec_id == CODEC_ID_H264){
  758. if(avctx->ticks_per_frame == 1){
  759. s->avctx->time_base.den *=2;
  760. }
  761. avctx->ticks_per_frame = 2;
  762. }
  763. if(avctx->extradata_size > 0 && avctx->extradata && *(char *)avctx->extradata == 1){
  764. int i, cnt, nalsize;
  765. unsigned char *p = avctx->extradata;
  766. h->is_avc = 1;
  767. if(avctx->extradata_size < 7) {
  768. av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
  769. return -1;
  770. }
  771. /* sps and pps in the avcC always have length coded with 2 bytes,
  772. so put a fake nal_length_size = 2 while parsing them */
  773. h->nal_length_size = 2;
  774. // Decode sps from avcC
  775. cnt = *(p+5) & 0x1f; // Number of sps
  776. p += 6;
  777. for (i = 0; i < cnt; i++) {
  778. nalsize = AV_RB16(p) + 2;
  779. if(decode_nal_units(h, p, nalsize) < 0) {
  780. av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
  781. return -1;
  782. }
  783. p += nalsize;
  784. }
  785. // Decode pps from avcC
  786. cnt = *(p++); // Number of pps
  787. for (i = 0; i < cnt; i++) {
  788. nalsize = AV_RB16(p) + 2;
  789. if(decode_nal_units(h, p, nalsize) != nalsize) {
  790. av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
  791. return -1;
  792. }
  793. p += nalsize;
  794. }
  795. // Now store right nal length size, that will be use to parse all other nals
  796. h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
  797. } else {
  798. h->is_avc = 0;
  799. if(decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) < 0)
  800. return -1;
  801. }
  802. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  803. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  804. s->low_delay = 0;
  805. }
  806. return 0;
  807. }
  808. int ff_h264_frame_start(H264Context *h){
  809. MpegEncContext * const s = &h->s;
  810. int i;
  811. if(MPV_frame_start(s, s->avctx) < 0)
  812. return -1;
  813. ff_er_frame_start(s);
  814. /*
  815. * MPV_frame_start uses pict_type to derive key_frame.
  816. * This is incorrect for H.264; IDR markings must be used.
  817. * Zero here; IDR markings per slice in frame or fields are ORed in later.
  818. * See decode_nal_units().
  819. */
  820. s->current_picture_ptr->key_frame= 0;
  821. s->current_picture_ptr->mmco_reset= 0;
  822. assert(s->linesize && s->uvlinesize);
  823. for(i=0; i<16; i++){
  824. h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
  825. h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
  826. }
  827. for(i=0; i<4; i++){
  828. h->block_offset[16+i]=
  829. h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  830. h->block_offset[24+16+i]=
  831. h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
  832. }
  833. /* can't be in alloc_tables because linesize isn't known there.
  834. * FIXME: redo bipred weight to not require extra buffer? */
  835. for(i = 0; i < s->avctx->thread_count; i++)
  836. if(!h->thread_context[i]->s.obmc_scratchpad)
  837. h->thread_context[i]->s.obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
  838. /* some macroblocks will be accessed before they're available */
  839. if(FRAME_MBAFF || s->avctx->thread_count > 1)
  840. memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(*h->slice_table));
  841. // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
  842. // We mark the current picture as non-reference after allocating it, so
  843. // that if we break out due to an error it can be released automatically
  844. // in the next MPV_frame_start().
  845. // SVQ3 as well as most other codecs have only last/next/current and thus
  846. // get released even with set reference, besides SVQ3 and others do not
  847. // mark frames as reference later "naturally".
  848. if(s->codec_id != CODEC_ID_SVQ3)
  849. s->current_picture_ptr->reference= 0;
  850. s->current_picture_ptr->field_poc[0]=
  851. s->current_picture_ptr->field_poc[1]= INT_MAX;
  852. assert(s->current_picture_ptr->long_ref==0);
  853. return 0;
  854. }
  855. 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 simple){
  856. MpegEncContext * const s = &h->s;
  857. uint8_t *top_border;
  858. int top_idx = 1;
  859. src_y -= linesize;
  860. src_cb -= uvlinesize;
  861. src_cr -= uvlinesize;
  862. if(!simple && FRAME_MBAFF){
  863. if(s->mb_y&1){
  864. if(!MB_MBAFF){
  865. top_border = h->top_borders[0][s->mb_x];
  866. AV_COPY128(top_border, src_y + 15*linesize);
  867. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  868. AV_COPY64(top_border+16, src_cb+7*uvlinesize);
  869. AV_COPY64(top_border+24, src_cr+7*uvlinesize);
  870. }
  871. }
  872. }else if(MB_MBAFF){
  873. top_idx = 0;
  874. }else
  875. return;
  876. }
  877. top_border = h->top_borders[top_idx][s->mb_x];
  878. // There are two lines saved, the line above the the top macroblock of a pair,
  879. // and the line above the bottom macroblock
  880. AV_COPY128(top_border, src_y + 16*linesize);
  881. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  882. AV_COPY64(top_border+16, src_cb+8*uvlinesize);
  883. AV_COPY64(top_border+24, src_cr+8*uvlinesize);
  884. }
  885. }
  886. static inline void xchg_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int xchg, int simple){
  887. MpegEncContext * const s = &h->s;
  888. int deblock_left;
  889. int deblock_top;
  890. int top_idx = 1;
  891. uint8_t *top_border_m1;
  892. uint8_t *top_border;
  893. if(!simple && FRAME_MBAFF){
  894. if(s->mb_y&1){
  895. if(!MB_MBAFF)
  896. return;
  897. }else{
  898. top_idx = MB_MBAFF ? 0 : 1;
  899. }
  900. }
  901. if(h->deblocking_filter == 2) {
  902. deblock_left = h->left_type[0];
  903. deblock_top = h->top_type;
  904. } else {
  905. deblock_left = (s->mb_x > 0);
  906. deblock_top = (s->mb_y > !!MB_FIELD);
  907. }
  908. src_y -= linesize + 1;
  909. src_cb -= uvlinesize + 1;
  910. src_cr -= uvlinesize + 1;
  911. top_border_m1 = h->top_borders[top_idx][s->mb_x-1];
  912. top_border = h->top_borders[top_idx][s->mb_x];
  913. #define XCHG(a,b,xchg)\
  914. if (xchg) AV_SWAP64(b,a);\
  915. else AV_COPY64(b,a);
  916. if(deblock_top){
  917. if(deblock_left){
  918. XCHG(top_border_m1+8, src_y -7, 1);
  919. }
  920. XCHG(top_border+0, src_y +1, xchg);
  921. XCHG(top_border+8, src_y +9, 1);
  922. if(s->mb_x+1 < s->mb_width){
  923. XCHG(h->top_borders[top_idx][s->mb_x+1], src_y +17, 1);
  924. }
  925. }
  926. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  927. if(deblock_top){
  928. if(deblock_left){
  929. XCHG(top_border_m1+16, src_cb -7, 1);
  930. XCHG(top_border_m1+24, src_cr -7, 1);
  931. }
  932. XCHG(top_border+16, src_cb+1, 1);
  933. XCHG(top_border+24, src_cr+1, 1);
  934. }
  935. }
  936. }
  937. static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple){
  938. MpegEncContext * const s = &h->s;
  939. const int mb_x= s->mb_x;
  940. const int mb_y= s->mb_y;
  941. const int mb_xy= h->mb_xy;
  942. const int mb_type= s->current_picture.mb_type[mb_xy];
  943. uint8_t *dest_y, *dest_cb, *dest_cr;
  944. int linesize, uvlinesize /*dct_offset*/;
  945. int i;
  946. int *block_offset = &h->block_offset[0];
  947. const int transform_bypass = !simple && (s->qscale == 0 && h->sps.transform_bypass);
  948. /* is_h264 should always be true if SVQ3 is disabled. */
  949. const int is_h264 = !CONFIG_SVQ3_DECODER || simple || s->codec_id == CODEC_ID_H264;
  950. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  951. void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  952. dest_y = s->current_picture.data[0] + (mb_x + mb_y * s->linesize ) * 16;
  953. dest_cb = s->current_picture.data[1] + (mb_x + mb_y * s->uvlinesize) * 8;
  954. dest_cr = s->current_picture.data[2] + (mb_x + mb_y * s->uvlinesize) * 8;
  955. s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + 64, s->linesize, 4);
  956. s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + 64, dest_cr - dest_cb, 2);
  957. h->list_counts[mb_xy]= h->list_count;
  958. if (!simple && MB_FIELD) {
  959. linesize = h->mb_linesize = s->linesize * 2;
  960. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  961. block_offset = &h->block_offset[24];
  962. if(mb_y&1){ //FIXME move out of this function?
  963. dest_y -= s->linesize*15;
  964. dest_cb-= s->uvlinesize*7;
  965. dest_cr-= s->uvlinesize*7;
  966. }
  967. if(FRAME_MBAFF) {
  968. int list;
  969. for(list=0; list<h->list_count; list++){
  970. if(!USES_LIST(mb_type, list))
  971. continue;
  972. if(IS_16X16(mb_type)){
  973. int8_t *ref = &h->ref_cache[list][scan8[0]];
  974. fill_rectangle(ref, 4, 4, 8, (16+*ref)^(s->mb_y&1), 1);
  975. }else{
  976. for(i=0; i<16; i+=4){
  977. int ref = h->ref_cache[list][scan8[i]];
  978. if(ref >= 0)
  979. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, (16+ref)^(s->mb_y&1), 1);
  980. }
  981. }
  982. }
  983. }
  984. } else {
  985. linesize = h->mb_linesize = s->linesize;
  986. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  987. // dct_offset = s->linesize * 16;
  988. }
  989. if (!simple && IS_INTRA_PCM(mb_type)) {
  990. for (i=0; i<16; i++) {
  991. memcpy(dest_y + i* linesize, h->mb + i*8, 16);
  992. }
  993. for (i=0; i<8; i++) {
  994. memcpy(dest_cb+ i*uvlinesize, h->mb + 128 + i*4, 8);
  995. memcpy(dest_cr+ i*uvlinesize, h->mb + 160 + i*4, 8);
  996. }
  997. } else {
  998. if(IS_INTRA(mb_type)){
  999. if(h->deblocking_filter)
  1000. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1, simple);
  1001. if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1002. h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
  1003. h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
  1004. }
  1005. if(IS_INTRA4x4(mb_type)){
  1006. if(simple || !s->encoding){
  1007. if(IS_8x8DCT(mb_type)){
  1008. if(transform_bypass){
  1009. idct_dc_add =
  1010. idct_add = s->dsp.add_pixels8;
  1011. }else{
  1012. idct_dc_add = s->dsp.h264_idct8_dc_add;
  1013. idct_add = s->dsp.h264_idct8_add;
  1014. }
  1015. for(i=0; i<16; i+=4){
  1016. uint8_t * const ptr= dest_y + block_offset[i];
  1017. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  1018. if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
  1019. h->hpc.pred8x8l_add[dir](ptr, h->mb + i*16, linesize);
  1020. }else{
  1021. const int nnz = h->non_zero_count_cache[ scan8[i] ];
  1022. h->hpc.pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
  1023. (h->topright_samples_available<<i)&0x4000, linesize);
  1024. if(nnz){
  1025. if(nnz == 1 && h->mb[i*16])
  1026. idct_dc_add(ptr, h->mb + i*16, linesize);
  1027. else
  1028. idct_add (ptr, h->mb + i*16, linesize);
  1029. }
  1030. }
  1031. }
  1032. }else{
  1033. if(transform_bypass){
  1034. idct_dc_add =
  1035. idct_add = s->dsp.add_pixels4;
  1036. }else{
  1037. idct_dc_add = s->dsp.h264_idct_dc_add;
  1038. idct_add = s->dsp.h264_idct_add;
  1039. }
  1040. for(i=0; i<16; i++){
  1041. uint8_t * const ptr= dest_y + block_offset[i];
  1042. const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
  1043. if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
  1044. h->hpc.pred4x4_add[dir](ptr, h->mb + i*16, linesize);
  1045. }else{
  1046. uint8_t *topright;
  1047. int nnz, tr;
  1048. if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
  1049. const int topright_avail= (h->topright_samples_available<<i)&0x8000;
  1050. assert(mb_y || linesize <= block_offset[i]);
  1051. if(!topright_avail){
  1052. tr= ptr[3 - linesize]*0x01010101;
  1053. topright= (uint8_t*) &tr;
  1054. }else
  1055. topright= ptr + 4 - linesize;
  1056. }else
  1057. topright= NULL;
  1058. h->hpc.pred4x4[ dir ](ptr, topright, linesize);
  1059. nnz = h->non_zero_count_cache[ scan8[i] ];
  1060. if(nnz){
  1061. if(is_h264){
  1062. if(nnz == 1 && h->mb[i*16])
  1063. idct_dc_add(ptr, h->mb + i*16, linesize);
  1064. else
  1065. idct_add (ptr, h->mb + i*16, linesize);
  1066. }else
  1067. ff_svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
  1068. }
  1069. }
  1070. }
  1071. }
  1072. }
  1073. }else{
  1074. h->hpc.pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
  1075. if(is_h264){
  1076. if(!transform_bypass)
  1077. h264_luma_dc_dequant_idct_c(h->mb, s->qscale, h->dequant4_coeff[0][s->qscale][0]);
  1078. }else
  1079. ff_svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
  1080. }
  1081. if(h->deblocking_filter)
  1082. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0, simple);
  1083. }else if(is_h264){
  1084. hl_motion(h, dest_y, dest_cb, dest_cr,
  1085. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  1086. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  1087. s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
  1088. }
  1089. if(!IS_INTRA4x4(mb_type)){
  1090. if(is_h264){
  1091. if(IS_INTRA16x16(mb_type)){
  1092. if(transform_bypass){
  1093. if(h->sps.profile_idc==244 && (h->intra16x16_pred_mode==VERT_PRED8x8 || h->intra16x16_pred_mode==HOR_PRED8x8)){
  1094. h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset, h->mb, linesize);
  1095. }else{
  1096. for(i=0; i<16; i++){
  1097. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16])
  1098. s->dsp.add_pixels4(dest_y + block_offset[i], h->mb + i*16, linesize);
  1099. }
  1100. }
  1101. }else{
  1102. s->dsp.h264_idct_add16intra(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
  1103. }
  1104. }else if(h->cbp&15){
  1105. if(transform_bypass){
  1106. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  1107. idct_add= IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
  1108. for(i=0; i<16; i+=di){
  1109. if(h->non_zero_count_cache[ scan8[i] ]){
  1110. idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
  1111. }
  1112. }
  1113. }else{
  1114. if(IS_8x8DCT(mb_type)){
  1115. s->dsp.h264_idct8_add4(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
  1116. }else{
  1117. s->dsp.h264_idct_add16(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
  1118. }
  1119. }
  1120. }
  1121. }else{
  1122. for(i=0; i<16; i++){
  1123. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
  1124. uint8_t * const ptr= dest_y + block_offset[i];
  1125. ff_svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  1126. }
  1127. }
  1128. }
  1129. }
  1130. if((simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)) && (h->cbp&0x30)){
  1131. uint8_t *dest[2] = {dest_cb, dest_cr};
  1132. if(transform_bypass){
  1133. if(IS_INTRA(mb_type) && h->sps.profile_idc==244 && (h->chroma_pred_mode==VERT_PRED8x8 || h->chroma_pred_mode==HOR_PRED8x8)){
  1134. h->hpc.pred8x8_add[h->chroma_pred_mode](dest[0], block_offset + 16, h->mb + 16*16, uvlinesize);
  1135. h->hpc.pred8x8_add[h->chroma_pred_mode](dest[1], block_offset + 20, h->mb + 20*16, uvlinesize);
  1136. }else{
  1137. idct_add = s->dsp.add_pixels4;
  1138. for(i=16; i<16+8; i++){
  1139. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16])
  1140. idct_add (dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  1141. }
  1142. }
  1143. }else{
  1144. chroma_dc_dequant_idct_c(h->mb + 16*16, h->chroma_qp[0], h->dequant4_coeff[IS_INTRA(mb_type) ? 1:4][h->chroma_qp[0]][0]);
  1145. chroma_dc_dequant_idct_c(h->mb + 16*16+4*16, h->chroma_qp[1], h->dequant4_coeff[IS_INTRA(mb_type) ? 2:5][h->chroma_qp[1]][0]);
  1146. if(is_h264){
  1147. idct_add = s->dsp.h264_idct_add;
  1148. idct_dc_add = s->dsp.h264_idct_dc_add;
  1149. for(i=16; i<16+8; i++){
  1150. if(h->non_zero_count_cache[ scan8[i] ])
  1151. idct_add (dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  1152. else if(h->mb[i*16])
  1153. idct_dc_add(dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
  1154. }
  1155. }else{
  1156. for(i=16; i<16+8; i++){
  1157. if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
  1158. uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
  1159. ff_svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, ff_h264_chroma_qp[s->qscale + 12] - 12, 2);
  1160. }
  1161. }
  1162. }
  1163. }
  1164. }
  1165. }
  1166. if(h->cbp || IS_INTRA(mb_type))
  1167. s->dsp.clear_blocks(h->mb);
  1168. }
  1169. /**
  1170. * Process a macroblock; this case avoids checks for expensive uncommon cases.
  1171. */
  1172. static void hl_decode_mb_simple(H264Context *h){
  1173. hl_decode_mb_internal(h, 1);
  1174. }
  1175. /**
  1176. * Process a macroblock; this handles edge cases, such as interlacing.
  1177. */
  1178. static void av_noinline hl_decode_mb_complex(H264Context *h){
  1179. hl_decode_mb_internal(h, 0);
  1180. }
  1181. void ff_h264_hl_decode_mb(H264Context *h){
  1182. MpegEncContext * const s = &h->s;
  1183. const int mb_xy= h->mb_xy;
  1184. const int mb_type= s->current_picture.mb_type[mb_xy];
  1185. int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || s->qscale == 0;
  1186. if (is_complex)
  1187. hl_decode_mb_complex(h);
  1188. else hl_decode_mb_simple(h);
  1189. }
  1190. static int pred_weight_table(H264Context *h){
  1191. MpegEncContext * const s = &h->s;
  1192. int list, i;
  1193. int luma_def, chroma_def;
  1194. h->use_weight= 0;
  1195. h->use_weight_chroma= 0;
  1196. h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
  1197. h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
  1198. luma_def = 1<<h->luma_log2_weight_denom;
  1199. chroma_def = 1<<h->chroma_log2_weight_denom;
  1200. for(list=0; list<2; list++){
  1201. h->luma_weight_flag[list] = 0;
  1202. h->chroma_weight_flag[list] = 0;
  1203. for(i=0; i<h->ref_count[list]; i++){
  1204. int luma_weight_flag, chroma_weight_flag;
  1205. luma_weight_flag= get_bits1(&s->gb);
  1206. if(luma_weight_flag){
  1207. h->luma_weight[list][i]= get_se_golomb(&s->gb);
  1208. h->luma_offset[list][i]= get_se_golomb(&s->gb);
  1209. if( h->luma_weight[list][i] != luma_def
  1210. || h->luma_offset[list][i] != 0) {
  1211. h->use_weight= 1;
  1212. h->luma_weight_flag[list]= 1;
  1213. }
  1214. }else{
  1215. h->luma_weight[list][i]= luma_def;
  1216. h->luma_offset[list][i]= 0;
  1217. }
  1218. if(CHROMA){
  1219. chroma_weight_flag= get_bits1(&s->gb);
  1220. if(chroma_weight_flag){
  1221. int j;
  1222. for(j=0; j<2; j++){
  1223. h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
  1224. h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
  1225. if( h->chroma_weight[list][i][j] != chroma_def
  1226. || h->chroma_offset[list][i][j] != 0) {
  1227. h->use_weight_chroma= 1;
  1228. h->chroma_weight_flag[list]= 1;
  1229. }
  1230. }
  1231. }else{
  1232. int j;
  1233. for(j=0; j<2; j++){
  1234. h->chroma_weight[list][i][j]= chroma_def;
  1235. h->chroma_offset[list][i][j]= 0;
  1236. }
  1237. }
  1238. }
  1239. }
  1240. if(h->slice_type_nos != FF_B_TYPE) break;
  1241. }
  1242. h->use_weight= h->use_weight || h->use_weight_chroma;
  1243. return 0;
  1244. }
  1245. static void implicit_weight_table(H264Context *h){
  1246. MpegEncContext * const s = &h->s;
  1247. int ref0, ref1, i;
  1248. int cur_poc = s->current_picture_ptr->poc;
  1249. for (i = 0; i < 2; i++) {
  1250. h->luma_weight_flag[i] = 0;
  1251. h->chroma_weight_flag[i] = 0;
  1252. }
  1253. if( h->ref_count[0] == 1 && h->ref_count[1] == 1
  1254. && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
  1255. h->use_weight= 0;
  1256. h->use_weight_chroma= 0;
  1257. return;
  1258. }
  1259. h->use_weight= 2;
  1260. h->use_weight_chroma= 2;
  1261. h->luma_log2_weight_denom= 5;
  1262. h->chroma_log2_weight_denom= 5;
  1263. for(ref0=0; ref0 < h->ref_count[0]; ref0++){
  1264. int poc0 = h->ref_list[0][ref0].poc;
  1265. for(ref1=0; ref1 < h->ref_count[1]; ref1++){
  1266. int poc1 = h->ref_list[1][ref1].poc;
  1267. int td = av_clip(poc1 - poc0, -128, 127);
  1268. if(td){
  1269. int tb = av_clip(cur_poc - poc0, -128, 127);
  1270. int tx = (16384 + (FFABS(td) >> 1)) / td;
  1271. int dist_scale_factor = av_clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
  1272. if(dist_scale_factor < -64 || dist_scale_factor > 128)
  1273. h->implicit_weight[ref0][ref1] = 32;
  1274. else
  1275. h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor;
  1276. }else
  1277. h->implicit_weight[ref0][ref1] = 32;
  1278. }
  1279. }
  1280. }
  1281. /**
  1282. * instantaneous decoder refresh.
  1283. */
  1284. static void idr(H264Context *h){
  1285. ff_h264_remove_all_refs(h);
  1286. h->prev_frame_num= 0;
  1287. h->prev_frame_num_offset= 0;
  1288. h->prev_poc_msb=
  1289. h->prev_poc_lsb= 0;
  1290. }
  1291. /* forget old pics after a seek */
  1292. static void flush_dpb(AVCodecContext *avctx){
  1293. H264Context *h= avctx->priv_data;
  1294. int i;
  1295. for(i=0; i<MAX_DELAYED_PIC_COUNT; i++) {
  1296. if(h->delayed_pic[i])
  1297. h->delayed_pic[i]->reference= 0;
  1298. h->delayed_pic[i]= NULL;
  1299. }
  1300. h->outputed_poc= INT_MIN;
  1301. h->prev_interlaced_frame = 1;
  1302. idr(h);
  1303. if(h->s.current_picture_ptr)
  1304. h->s.current_picture_ptr->reference= 0;
  1305. h->s.first_field= 0;
  1306. ff_h264_reset_sei(h);
  1307. ff_mpeg_flush(avctx);
  1308. }
  1309. static int init_poc(H264Context *h){
  1310. MpegEncContext * const s = &h->s;
  1311. const int max_frame_num= 1<<h->sps.log2_max_frame_num;
  1312. int field_poc[2];
  1313. Picture *cur = s->current_picture_ptr;
  1314. h->frame_num_offset= h->prev_frame_num_offset;
  1315. if(h->frame_num < h->prev_frame_num)
  1316. h->frame_num_offset += max_frame_num;
  1317. if(h->sps.poc_type==0){
  1318. const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
  1319. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
  1320. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  1321. else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
  1322. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  1323. else
  1324. h->poc_msb = h->prev_poc_msb;
  1325. //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  1326. field_poc[0] =
  1327. field_poc[1] = h->poc_msb + h->poc_lsb;
  1328. if(s->picture_structure == PICT_FRAME)
  1329. field_poc[1] += h->delta_poc_bottom;
  1330. }else if(h->sps.poc_type==1){
  1331. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  1332. int i;
  1333. if(h->sps.poc_cycle_length != 0)
  1334. abs_frame_num = h->frame_num_offset + h->frame_num;
  1335. else
  1336. abs_frame_num = 0;
  1337. if(h->nal_ref_idc==0 && abs_frame_num > 0)
  1338. abs_frame_num--;
  1339. expected_delta_per_poc_cycle = 0;
  1340. for(i=0; i < h->sps.poc_cycle_length; i++)
  1341. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
  1342. if(abs_frame_num > 0){
  1343. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  1344. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  1345. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  1346. for(i = 0; i <= frame_num_in_poc_cycle; i++)
  1347. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
  1348. } else
  1349. expectedpoc = 0;
  1350. if(h->nal_ref_idc == 0)
  1351. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  1352. field_poc[0] = expectedpoc + h->delta_poc[0];
  1353. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  1354. if(s->picture_structure == PICT_FRAME)
  1355. field_poc[1] += h->delta_poc[1];
  1356. }else{
  1357. int poc= 2*(h->frame_num_offset + h->frame_num);
  1358. if(!h->nal_ref_idc)
  1359. poc--;
  1360. field_poc[0]= poc;
  1361. field_poc[1]= poc;
  1362. }
  1363. if(s->picture_structure != PICT_BOTTOM_FIELD)
  1364. s->current_picture_ptr->field_poc[0]= field_poc[0];
  1365. if(s->picture_structure != PICT_TOP_FIELD)
  1366. s->current_picture_ptr->field_poc[1]= field_poc[1];
  1367. cur->poc= FFMIN(cur->field_poc[0], cur->field_poc[1]);
  1368. return 0;
  1369. }
  1370. /**
  1371. * initialize scan tables
  1372. */
  1373. static void init_scan_tables(H264Context *h){
  1374. MpegEncContext * const s = &h->s;
  1375. int i;
  1376. if(s->dsp.h264_idct_add == ff_h264_idct_add_c){ //FIXME little ugly
  1377. memcpy(h->zigzag_scan, zigzag_scan, 16*sizeof(uint8_t));
  1378. memcpy(h-> field_scan, field_scan, 16*sizeof(uint8_t));
  1379. }else{
  1380. for(i=0; i<16; i++){
  1381. #define T(x) (x>>2) | ((x<<2) & 0xF)
  1382. h->zigzag_scan[i] = T(zigzag_scan[i]);
  1383. h-> field_scan[i] = T( field_scan[i]);
  1384. #undef T
  1385. }
  1386. }
  1387. if(s->dsp.h264_idct8_add == ff_h264_idct8_add_c){
  1388. memcpy(h->zigzag_scan8x8, ff_zigzag_direct, 64*sizeof(uint8_t));
  1389. memcpy(h->zigzag_scan8x8_cavlc, zigzag_scan8x8_cavlc, 64*sizeof(uint8_t));
  1390. memcpy(h->field_scan8x8, field_scan8x8, 64*sizeof(uint8_t));
  1391. memcpy(h->field_scan8x8_cavlc, field_scan8x8_cavlc, 64*sizeof(uint8_t));
  1392. }else{
  1393. for(i=0; i<64; i++){
  1394. #define T(x) (x>>3) | ((x&7)<<3)
  1395. h->zigzag_scan8x8[i] = T(ff_zigzag_direct[i]);
  1396. h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
  1397. h->field_scan8x8[i] = T(field_scan8x8[i]);
  1398. h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
  1399. #undef T
  1400. }
  1401. }
  1402. if(h->sps.transform_bypass){ //FIXME same ugly
  1403. h->zigzag_scan_q0 = zigzag_scan;
  1404. h->zigzag_scan8x8_q0 = ff_zigzag_direct;
  1405. h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
  1406. h->field_scan_q0 = field_scan;
  1407. h->field_scan8x8_q0 = field_scan8x8;
  1408. h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
  1409. }else{
  1410. h->zigzag_scan_q0 = h->zigzag_scan;
  1411. h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
  1412. h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
  1413. h->field_scan_q0 = h->field_scan;
  1414. h->field_scan8x8_q0 = h->field_scan8x8;
  1415. h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
  1416. }
  1417. }
  1418. static void field_end(H264Context *h){
  1419. MpegEncContext * const s = &h->s;
  1420. AVCodecContext * const avctx= s->avctx;
  1421. s->mb_y= 0;
  1422. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
  1423. s->current_picture_ptr->pict_type= s->pict_type;
  1424. if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  1425. ff_vdpau_h264_set_reference_frames(s);
  1426. if(!s->dropable) {
  1427. ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  1428. h->prev_poc_msb= h->poc_msb;
  1429. h->prev_poc_lsb= h->poc_lsb;
  1430. }
  1431. h->prev_frame_num_offset= h->frame_num_offset;
  1432. h->prev_frame_num= h->frame_num;
  1433. if (avctx->hwaccel) {
  1434. if (avctx->hwaccel->end_frame(avctx) < 0)
  1435. av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
  1436. }
  1437. if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  1438. ff_vdpau_h264_picture_complete(s);
  1439. /*
  1440. * FIXME: Error handling code does not seem to support interlaced
  1441. * when slices span multiple rows
  1442. * The ff_er_add_slice calls don't work right for bottom
  1443. * fields; they cause massive erroneous error concealing
  1444. * Error marking covers both fields (top and bottom).
  1445. * This causes a mismatched s->error_count
  1446. * and a bad error table. Further, the error count goes to
  1447. * INT_MAX when called for bottom field, because mb_y is
  1448. * past end by one (callers fault) and resync_mb_y != 0
  1449. * causes problems for the first MB line, too.
  1450. */
  1451. if (!FIELD_PICTURE)
  1452. ff_er_frame_end(s);
  1453. MPV_frame_end(s);
  1454. h->current_slice=0;
  1455. }
  1456. /**
  1457. * Replicates H264 "master" context to thread contexts.
  1458. */
  1459. static void clone_slice(H264Context *dst, H264Context *src)
  1460. {
  1461. memcpy(dst->block_offset, src->block_offset, sizeof(dst->block_offset));
  1462. dst->s.current_picture_ptr = src->s.current_picture_ptr;
  1463. dst->s.current_picture = src->s.current_picture;
  1464. dst->s.linesize = src->s.linesize;
  1465. dst->s.uvlinesize = src->s.uvlinesize;
  1466. dst->s.first_field = src->s.first_field;
  1467. dst->prev_poc_msb = src->prev_poc_msb;
  1468. dst->prev_poc_lsb = src->prev_poc_lsb;
  1469. dst->prev_frame_num_offset = src->prev_frame_num_offset;
  1470. dst->prev_frame_num = src->prev_frame_num;
  1471. dst->short_ref_count = src->short_ref_count;
  1472. memcpy(dst->short_ref, src->short_ref, sizeof(dst->short_ref));
  1473. memcpy(dst->long_ref, src->long_ref, sizeof(dst->long_ref));
  1474. memcpy(dst->default_ref_list, src->default_ref_list, sizeof(dst->default_ref_list));
  1475. memcpy(dst->ref_list, src->ref_list, sizeof(dst->ref_list));
  1476. memcpy(dst->dequant4_coeff, src->dequant4_coeff, sizeof(src->dequant4_coeff));
  1477. memcpy(dst->dequant8_coeff, src->dequant8_coeff, sizeof(src->dequant8_coeff));
  1478. }
  1479. /**
  1480. * decodes a slice header.
  1481. * This will also call MPV_common_init() and frame_start() as needed.
  1482. *
  1483. * @param h h264context
  1484. * @param h0 h264 master context (differs from 'h' when doing sliced based parallel decoding)
  1485. *
  1486. * @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded
  1487. */
  1488. static int decode_slice_header(H264Context *h, H264Context *h0){
  1489. MpegEncContext * const s = &h->s;
  1490. MpegEncContext * const s0 = &h0->s;
  1491. unsigned int first_mb_in_slice;
  1492. unsigned int pps_id;
  1493. int num_ref_idx_active_override_flag;
  1494. unsigned int slice_type, tmp, i, j;
  1495. int default_ref_list_done = 0;
  1496. int last_pic_structure;
  1497. s->dropable= h->nal_ref_idc == 0;
  1498. if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !h->nal_ref_idc){
  1499. s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
  1500. s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
  1501. }else{
  1502. s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
  1503. s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
  1504. }
  1505. first_mb_in_slice= get_ue_golomb(&s->gb);
  1506. if(first_mb_in_slice == 0){ //FIXME better field boundary detection
  1507. if(h0->current_slice && FIELD_PICTURE){
  1508. field_end(h);
  1509. }
  1510. h0->current_slice = 0;
  1511. if (!s0->first_field)
  1512. s->current_picture_ptr= NULL;
  1513. }
  1514. slice_type= get_ue_golomb_31(&s->gb);
  1515. if(slice_type > 9){
  1516. 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);
  1517. return -1;
  1518. }
  1519. if(slice_type > 4){
  1520. slice_type -= 5;
  1521. h->slice_type_fixed=1;
  1522. }else
  1523. h->slice_type_fixed=0;
  1524. slice_type= golomb_to_pict_type[ slice_type ];
  1525. if (slice_type == FF_I_TYPE
  1526. || (h0->current_slice != 0 && slice_type == h0->last_slice_type) ) {
  1527. default_ref_list_done = 1;
  1528. }
  1529. h->slice_type= slice_type;
  1530. h->slice_type_nos= slice_type & 3;
  1531. s->pict_type= h->slice_type; // to make a few old functions happy, it's wrong though
  1532. pps_id= get_ue_golomb(&s->gb);
  1533. if(pps_id>=MAX_PPS_COUNT){
  1534. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
  1535. return -1;
  1536. }
  1537. if(!h0->pps_buffers[pps_id]) {
  1538. av_log(h->s.avctx, AV_LOG_ERROR, "non-existing PPS %u referenced\n", pps_id);
  1539. return -1;
  1540. }
  1541. h->pps= *h0->pps_buffers[pps_id];
  1542. if(!h0->sps_buffers[h->pps.sps_id]) {
  1543. av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS %u referenced\n", h->pps.sps_id);
  1544. return -1;
  1545. }
  1546. h->sps = *h0->sps_buffers[h->pps.sps_id];
  1547. if(h == h0 && h->dequant_coeff_pps != pps_id){
  1548. h->dequant_coeff_pps = pps_id;
  1549. init_dequant_tables(h);
  1550. }
  1551. s->mb_width= h->sps.mb_width;
  1552. s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  1553. h->b_stride= s->mb_width*4;
  1554. h->b8_stride= s->mb_width*2;
  1555. s->width = 16*s->mb_width - 2*FFMIN(h->sps.crop_right, 7);
  1556. if(h->sps.frame_mbs_only_flag)
  1557. s->height= 16*s->mb_height - 2*FFMIN(h->sps.crop_bottom, 7);
  1558. else
  1559. s->height= 16*s->mb_height - 4*FFMIN(h->sps.crop_bottom, 3);
  1560. if (s->context_initialized
  1561. && ( s->width != s->avctx->width || s->height != s->avctx->height)) {
  1562. if(h != h0)
  1563. return -1; // width / height changed during parallelized decoding
  1564. free_tables(h);
  1565. flush_dpb(s->avctx);
  1566. MPV_common_end(s);
  1567. }
  1568. if (!s->context_initialized) {
  1569. if(h != h0)
  1570. return -1; // we cant (re-)initialize context during parallel decoding
  1571. avcodec_set_dimensions(s->avctx, s->width, s->height);
  1572. s->avctx->sample_aspect_ratio= h->sps.sar;
  1573. if(!s->avctx->sample_aspect_ratio.den)
  1574. s->avctx->sample_aspect_ratio.den = 1;
  1575. if(h->sps.video_signal_type_present_flag){
  1576. s->avctx->color_range = h->sps.full_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
  1577. if(h->sps.colour_description_present_flag){
  1578. s->avctx->color_primaries = h->sps.color_primaries;
  1579. s->avctx->color_trc = h->sps.color_trc;
  1580. s->avctx->colorspace = h->sps.colorspace;
  1581. }
  1582. }
  1583. if(h->sps.timing_info_present_flag){
  1584. int64_t den= h->sps.time_scale;
  1585. if(h->x264_build < 44U)
  1586. den *= 2;
  1587. av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
  1588. h->sps.num_units_in_tick, den, 1<<30);
  1589. }
  1590. s->avctx->pix_fmt = s->avctx->get_format(s->avctx, s->avctx->codec->pix_fmts);
  1591. s->avctx->hwaccel = ff_find_hwaccel(s->avctx->codec->id, s->avctx->pix_fmt);
  1592. if (MPV_common_init(s) < 0)
  1593. return -1;
  1594. s->first_field = 0;
  1595. h->prev_interlaced_frame = 1;
  1596. init_scan_tables(h);
  1597. ff_h264_alloc_tables(h);
  1598. for(i = 1; i < s->avctx->thread_count; i++) {
  1599. H264Context *c;
  1600. c = h->thread_context[i] = av_malloc(sizeof(H264Context));
  1601. memcpy(c, h->s.thread_context[i], sizeof(MpegEncContext));
  1602. memset(&c->s + 1, 0, sizeof(H264Context) - sizeof(MpegEncContext));
  1603. c->sps = h->sps;
  1604. c->pps = h->pps;
  1605. init_scan_tables(c);
  1606. clone_tables(c, h);
  1607. }
  1608. for(i = 0; i < s->avctx->thread_count; i++)
  1609. if(context_init(h->thread_context[i]) < 0)
  1610. return -1;
  1611. }
  1612. h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
  1613. h->mb_mbaff = 0;
  1614. h->mb_aff_frame = 0;
  1615. last_pic_structure = s0->picture_structure;
  1616. if(h->sps.frame_mbs_only_flag){
  1617. s->picture_structure= PICT_FRAME;
  1618. }else{
  1619. if(get_bits1(&s->gb)) { //field_pic_flag
  1620. s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
  1621. } else {
  1622. s->picture_structure= PICT_FRAME;
  1623. h->mb_aff_frame = h->sps.mb_aff;
  1624. }
  1625. }
  1626. h->mb_field_decoding_flag= s->picture_structure != PICT_FRAME;
  1627. if(h0->current_slice == 0){
  1628. while(h->frame_num != h->prev_frame_num &&
  1629. h->frame_num != (h->prev_frame_num+1)%(1<<h->sps.log2_max_frame_num)){
  1630. av_log(NULL, AV_LOG_DEBUG, "Frame num gap %d %d\n", h->frame_num, h->prev_frame_num);
  1631. if (ff_h264_frame_start(h) < 0)
  1632. return -1;
  1633. h->prev_frame_num++;
  1634. h->prev_frame_num %= 1<<h->sps.log2_max_frame_num;
  1635. s->current_picture_ptr->frame_num= h->prev_frame_num;
  1636. ff_h264_execute_ref_pic_marking(h, NULL, 0);
  1637. }
  1638. /* See if we have a decoded first field looking for a pair... */
  1639. if (s0->first_field) {
  1640. assert(s0->current_picture_ptr);
  1641. assert(s0->current_picture_ptr->data[0]);
  1642. assert(s0->current_picture_ptr->reference != DELAYED_PIC_REF);
  1643. /* figure out if we have a complementary field pair */
  1644. if (!FIELD_PICTURE || s->picture_structure == last_pic_structure) {
  1645. /*
  1646. * Previous field is unmatched. Don't display it, but let it
  1647. * remain for reference if marked as such.
  1648. */
  1649. s0->current_picture_ptr = NULL;
  1650. s0->first_field = FIELD_PICTURE;
  1651. } else {
  1652. if (h->nal_ref_idc &&
  1653. s0->current_picture_ptr->reference &&
  1654. s0->current_picture_ptr->frame_num != h->frame_num) {
  1655. /*
  1656. * This and previous field were reference, but had
  1657. * different frame_nums. Consider this field first in
  1658. * pair. Throw away previous field except for reference
  1659. * purposes.
  1660. */
  1661. s0->first_field = 1;
  1662. s0->current_picture_ptr = NULL;
  1663. } else {
  1664. /* Second field in complementary pair */
  1665. s0->first_field = 0;
  1666. }
  1667. }
  1668. } else {
  1669. /* Frame or first field in a potentially complementary pair */
  1670. assert(!s0->current_picture_ptr);
  1671. s0->first_field = FIELD_PICTURE;
  1672. }
  1673. if((!FIELD_PICTURE || s0->first_field) && ff_h264_frame_start(h) < 0) {
  1674. s0->first_field = 0;
  1675. return -1;
  1676. }
  1677. }
  1678. if(h != h0)
  1679. clone_slice(h, h0);
  1680. s->current_picture_ptr->frame_num= h->frame_num; //FIXME frame_num cleanup
  1681. assert(s->mb_num == s->mb_width * s->mb_height);
  1682. if(first_mb_in_slice << FIELD_OR_MBAFF_PICTURE >= s->mb_num ||
  1683. first_mb_in_slice >= s->mb_num){
  1684. av_log(h->s.avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
  1685. return -1;
  1686. }
  1687. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  1688. s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << FIELD_OR_MBAFF_PICTURE;
  1689. if (s->picture_structure == PICT_BOTTOM_FIELD)
  1690. s->resync_mb_y = s->mb_y = s->mb_y + 1;
  1691. assert(s->mb_y < s->mb_height);
  1692. if(s->picture_structure==PICT_FRAME){
  1693. h->curr_pic_num= h->frame_num;
  1694. h->max_pic_num= 1<< h->sps.log2_max_frame_num;
  1695. }else{
  1696. h->curr_pic_num= 2*h->frame_num + 1;
  1697. h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
  1698. }
  1699. if(h->nal_unit_type == NAL_IDR_SLICE){
  1700. get_ue_golomb(&s->gb); /* idr_pic_id */
  1701. }
  1702. if(h->sps.poc_type==0){
  1703. h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  1704. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
  1705. h->delta_poc_bottom= get_se_golomb(&s->gb);
  1706. }
  1707. }
  1708. if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
  1709. h->delta_poc[0]= get_se_golomb(&s->gb);
  1710. if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
  1711. h->delta_poc[1]= get_se_golomb(&s->gb);
  1712. }
  1713. init_poc(h);
  1714. if(h->pps.redundant_pic_cnt_present){
  1715. h->redundant_pic_count= get_ue_golomb(&s->gb);
  1716. }
  1717. //set defaults, might be overridden a few lines later
  1718. h->ref_count[0]= h->pps.ref_count[0];
  1719. h->ref_count[1]= h->pps.ref_count[1];
  1720. if(h->slice_type_nos != FF_I_TYPE){
  1721. if(h->slice_type_nos == FF_B_TYPE){
  1722. h->direct_spatial_mv_pred= get_bits1(&s->gb);
  1723. }
  1724. num_ref_idx_active_override_flag= get_bits1(&s->gb);
  1725. if(num_ref_idx_active_override_flag){
  1726. h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
  1727. if(h->slice_type_nos==FF_B_TYPE)
  1728. h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
  1729. if(h->ref_count[0]-1 > 32-1 || h->ref_count[1]-1 > 32-1){
  1730. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  1731. h->ref_count[0]= h->ref_count[1]= 1;
  1732. return -1;
  1733. }
  1734. }
  1735. if(h->slice_type_nos == FF_B_TYPE)
  1736. h->list_count= 2;
  1737. else
  1738. h->list_count= 1;
  1739. }else
  1740. h->list_count= 0;
  1741. if(!default_ref_list_done){
  1742. ff_h264_fill_default_ref_list(h);
  1743. }
  1744. if(h->slice_type_nos!=FF_I_TYPE && ff_h264_decode_ref_pic_list_reordering(h) < 0)
  1745. return -1;
  1746. if(h->slice_type_nos!=FF_I_TYPE){
  1747. s->last_picture_ptr= &h->ref_list[0][0];
  1748. ff_copy_picture(&s->last_picture, s->last_picture_ptr);
  1749. }
  1750. if(h->slice_type_nos==FF_B_TYPE){
  1751. s->next_picture_ptr= &h->ref_list[1][0];
  1752. ff_copy_picture(&s->next_picture, s->next_picture_ptr);
  1753. }
  1754. if( (h->pps.weighted_pred && h->slice_type_nos == FF_P_TYPE )
  1755. || (h->pps.weighted_bipred_idc==1 && h->slice_type_nos== FF_B_TYPE ) )
  1756. pred_weight_table(h);
  1757. else if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== FF_B_TYPE)
  1758. implicit_weight_table(h);
  1759. else {
  1760. h->use_weight = 0;
  1761. for (i = 0; i < 2; i++) {
  1762. h->luma_weight_flag[i] = 0;
  1763. h->chroma_weight_flag[i] = 0;
  1764. }
  1765. }
  1766. if(h->nal_ref_idc)
  1767. ff_h264_decode_ref_pic_marking(h0, &s->gb);
  1768. if(FRAME_MBAFF)
  1769. ff_h264_fill_mbaff_ref_list(h);
  1770. if(h->slice_type_nos==FF_B_TYPE && !h->direct_spatial_mv_pred)
  1771. ff_h264_direct_dist_scale_factor(h);
  1772. ff_h264_direct_ref_list_init(h);
  1773. if( h->slice_type_nos != FF_I_TYPE && h->pps.cabac ){
  1774. tmp = get_ue_golomb_31(&s->gb);
  1775. if(tmp > 2){
  1776. av_log(s->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
  1777. return -1;
  1778. }
  1779. h->cabac_init_idc= tmp;
  1780. }
  1781. h->last_qscale_diff = 0;
  1782. tmp = h->pps.init_qp + get_se_golomb(&s->gb);
  1783. if(tmp>51){
  1784. av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
  1785. return -1;
  1786. }
  1787. s->qscale= tmp;
  1788. h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
  1789. h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
  1790. //FIXME qscale / qp ... stuff
  1791. if(h->slice_type == FF_SP_TYPE){
  1792. get_bits1(&s->gb); /* sp_for_switch_flag */
  1793. }
  1794. if(h->slice_type==FF_SP_TYPE || h->slice_type == FF_SI_TYPE){
  1795. get_se_golomb(&s->gb); /* slice_qs_delta */
  1796. }
  1797. h->deblocking_filter = 1;
  1798. h->slice_alpha_c0_offset = 52;
  1799. h->slice_beta_offset = 52;
  1800. if( h->pps.deblocking_filter_parameters_present ) {
  1801. tmp= get_ue_golomb_31(&s->gb);
  1802. if(tmp > 2){
  1803. av_log(s->avctx, AV_LOG_ERROR, "deblocking_filter_idc %u out of range\n", tmp);
  1804. return -1;
  1805. }
  1806. h->deblocking_filter= tmp;
  1807. if(h->deblocking_filter < 2)
  1808. h->deblocking_filter^= 1; // 1<->0
  1809. if( h->deblocking_filter ) {
  1810. h->slice_alpha_c0_offset += get_se_golomb(&s->gb) << 1;
  1811. h->slice_beta_offset += get_se_golomb(&s->gb) << 1;
  1812. if( h->slice_alpha_c0_offset > 104U
  1813. || h->slice_beta_offset > 104U){
  1814. 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);
  1815. return -1;
  1816. }
  1817. }
  1818. }
  1819. if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
  1820. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type_nos != FF_I_TYPE)
  1821. ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type_nos == FF_B_TYPE)
  1822. ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  1823. h->deblocking_filter= 0;
  1824. if(h->deblocking_filter == 1 && h0->max_contexts > 1) {
  1825. if(s->avctx->flags2 & CODEC_FLAG2_FAST) {
  1826. /* Cheat slightly for speed:
  1827. Do not bother to deblock across slices. */
  1828. h->deblocking_filter = 2;
  1829. } else {
  1830. h0->max_contexts = 1;
  1831. if(!h0->single_decode_warning) {
  1832. av_log(s->avctx, AV_LOG_INFO, "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
  1833. h0->single_decode_warning = 1;
  1834. }
  1835. if(h != h0)
  1836. return 1; // deblocking switched inside frame
  1837. }
  1838. }
  1839. 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]);
  1840. #if 0 //FMO
  1841. if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
  1842. slice_group_change_cycle= get_bits(&s->gb, ?);
  1843. #endif
  1844. h0->last_slice_type = slice_type;
  1845. h->slice_num = ++h0->current_slice;
  1846. if(h->slice_num >= MAX_SLICES){
  1847. av_log(s->avctx, AV_LOG_ERROR, "Too many slices, increase MAX_SLICES and recompile\n");
  1848. }
  1849. for(j=0; j<2; j++){
  1850. int id_list[16];
  1851. int *ref2frm= h->ref2frm[h->slice_num&(MAX_SLICES-1)][j];
  1852. for(i=0; i<16; i++){
  1853. id_list[i]= 60;
  1854. if(h->ref_list[j][i].data[0]){
  1855. int k;
  1856. uint8_t *base= h->ref_list[j][i].base[0];
  1857. for(k=0; k<h->short_ref_count; k++)
  1858. if(h->short_ref[k]->base[0] == base){
  1859. id_list[i]= k;
  1860. break;
  1861. }
  1862. for(k=0; k<h->long_ref_count; k++)
  1863. if(h->long_ref[k] && h->long_ref[k]->base[0] == base){
  1864. id_list[i]= h->short_ref_count + k;
  1865. break;
  1866. }
  1867. }
  1868. }
  1869. ref2frm[0]=
  1870. ref2frm[1]= -1;
  1871. for(i=0; i<16; i++)
  1872. ref2frm[i+2]= 4*id_list[i]
  1873. +(h->ref_list[j][i].reference&3);
  1874. ref2frm[18+0]=
  1875. ref2frm[18+1]= -1;
  1876. for(i=16; i<48; i++)
  1877. ref2frm[i+4]= 4*id_list[(i-16)>>1]
  1878. +(h->ref_list[j][i].reference&3);
  1879. }
  1880. h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
  1881. h->emu_edge_height= (FRAME_MBAFF || FIELD_PICTURE) ? 0 : h->emu_edge_width;
  1882. s->avctx->refs= h->sps.ref_frame_count;
  1883. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  1884. 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",
  1885. h->slice_num,
  1886. (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
  1887. first_mb_in_slice,
  1888. av_get_pict_type_char(h->slice_type), h->slice_type_fixed ? " fix" : "", h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
  1889. pps_id, h->frame_num,
  1890. s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
  1891. h->ref_count[0], h->ref_count[1],
  1892. s->qscale,
  1893. h->deblocking_filter, h->slice_alpha_c0_offset/2-26, h->slice_beta_offset/2-26,
  1894. h->use_weight,
  1895. h->use_weight==1 && h->use_weight_chroma ? "c" : "",
  1896. h->slice_type == FF_B_TYPE ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : ""
  1897. );
  1898. }
  1899. return 0;
  1900. }
  1901. int ff_h264_get_slice_type(const H264Context *h)
  1902. {
  1903. switch (h->slice_type) {
  1904. case FF_P_TYPE: return 0;
  1905. case FF_B_TYPE: return 1;
  1906. case FF_I_TYPE: return 2;
  1907. case FF_SP_TYPE: return 3;
  1908. case FF_SI_TYPE: return 4;
  1909. default: return -1;
  1910. }
  1911. }
  1912. static void loop_filter(H264Context *h){
  1913. MpegEncContext * const s = &h->s;
  1914. uint8_t *dest_y, *dest_cb, *dest_cr;
  1915. int linesize, uvlinesize, mb_x, mb_y;
  1916. const int end_mb_y= s->mb_y + FRAME_MBAFF;
  1917. const int old_slice_type= h->slice_type;
  1918. if(h->deblocking_filter) {
  1919. for(mb_x= 0; mb_x<s->mb_width; mb_x++){
  1920. for(mb_y=end_mb_y - FRAME_MBAFF; mb_y<= end_mb_y; mb_y++){
  1921. int mb_xy, mb_type;
  1922. mb_xy = h->mb_xy = mb_x + mb_y*s->mb_stride;
  1923. h->slice_num= h->slice_table[mb_xy];
  1924. mb_type= s->current_picture.mb_type[mb_xy];
  1925. h->list_count= h->list_counts[mb_xy];
  1926. if(FRAME_MBAFF)
  1927. h->mb_mbaff = h->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
  1928. s->mb_x= mb_x;
  1929. s->mb_y= mb_y;
  1930. dest_y = s->current_picture.data[0] + (mb_x + mb_y * s->linesize ) * 16;
  1931. dest_cb = s->current_picture.data[1] + (mb_x + mb_y * s->uvlinesize) * 8;
  1932. dest_cr = s->current_picture.data[2] + (mb_x + mb_y * s->uvlinesize) * 8;
  1933. //FIXME simplify above
  1934. if (MB_FIELD) {
  1935. linesize = h->mb_linesize = s->linesize * 2;
  1936. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  1937. if(mb_y&1){ //FIXME move out of this function?
  1938. dest_y -= s->linesize*15;
  1939. dest_cb-= s->uvlinesize*7;
  1940. dest_cr-= s->uvlinesize*7;
  1941. }
  1942. } else {
  1943. linesize = h->mb_linesize = s->linesize;
  1944. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  1945. }
  1946. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
  1947. if(fill_filter_caches(h, mb_type))
  1948. continue;
  1949. h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy]);
  1950. h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy]);
  1951. if (FRAME_MBAFF) {
  1952. ff_h264_filter_mb (h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  1953. } else {
  1954. ff_h264_filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
  1955. }
  1956. }
  1957. }
  1958. }
  1959. h->slice_type= old_slice_type;
  1960. s->mb_x= 0;
  1961. s->mb_y= end_mb_y - FRAME_MBAFF;
  1962. h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
  1963. h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
  1964. }
  1965. static void predict_field_decoding_flag(H264Context *h){
  1966. MpegEncContext * const s = &h->s;
  1967. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  1968. int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
  1969. ? s->current_picture.mb_type[mb_xy-1]
  1970. : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
  1971. ? s->current_picture.mb_type[mb_xy-s->mb_stride]
  1972. : 0;
  1973. h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  1974. }
  1975. static int decode_slice(struct AVCodecContext *avctx, void *arg){
  1976. H264Context *h = *(void**)arg;
  1977. MpegEncContext * const s = &h->s;
  1978. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  1979. s->mb_skip_run= -1;
  1980. h->is_complex = FRAME_MBAFF || s->picture_structure != PICT_FRAME || s->codec_id != CODEC_ID_H264 ||
  1981. (CONFIG_GRAY && (s->flags&CODEC_FLAG_GRAY));
  1982. if( h->pps.cabac ) {
  1983. /* realign */
  1984. align_get_bits( &s->gb );
  1985. /* init cabac */
  1986. ff_init_cabac_states( &h->cabac);
  1987. ff_init_cabac_decoder( &h->cabac,
  1988. s->gb.buffer + get_bits_count(&s->gb)/8,
  1989. (get_bits_left(&s->gb) + 7)/8);
  1990. ff_h264_init_cabac_states(h);
  1991. for(;;){
  1992. //START_TIMER
  1993. int ret = ff_h264_decode_mb_cabac(h);
  1994. int eos;
  1995. //STOP_TIMER("decode_mb_cabac")
  1996. if(ret>=0) ff_h264_hl_decode_mb(h);
  1997. if( ret >= 0 && FRAME_MBAFF ) { //FIXME optimal? or let mb_decode decode 16x32 ?
  1998. s->mb_y++;
  1999. ret = ff_h264_decode_mb_cabac(h);
  2000. if(ret>=0) ff_h264_hl_decode_mb(h);
  2001. s->mb_y--;
  2002. }
  2003. eos = get_cabac_terminate( &h->cabac );
  2004. if((s->workaround_bugs & FF_BUG_TRUNCATED) && h->cabac.bytestream > h->cabac.bytestream_end + 2){
  2005. 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);
  2006. return 0;
  2007. }
  2008. if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
  2009. 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);
  2010. 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);
  2011. return -1;
  2012. }
  2013. if( ++s->mb_x >= s->mb_width ) {
  2014. s->mb_x = 0;
  2015. loop_filter(h);
  2016. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  2017. ++s->mb_y;
  2018. if(FIELD_OR_MBAFF_PICTURE) {
  2019. ++s->mb_y;
  2020. if(FRAME_MBAFF && s->mb_y < s->mb_height)
  2021. predict_field_decoding_flag(h);
  2022. }
  2023. }
  2024. if( eos || s->mb_y >= s->mb_height ) {
  2025. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  2026. 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);
  2027. return 0;
  2028. }
  2029. }
  2030. } else {
  2031. for(;;){
  2032. int ret = ff_h264_decode_mb_cavlc(h);
  2033. if(ret>=0) ff_h264_hl_decode_mb(h);
  2034. if(ret>=0 && FRAME_MBAFF){ //FIXME optimal? or let mb_decode decode 16x32 ?
  2035. s->mb_y++;
  2036. ret = ff_h264_decode_mb_cavlc(h);
  2037. if(ret>=0) ff_h264_hl_decode_mb(h);
  2038. s->mb_y--;
  2039. }
  2040. if(ret<0){
  2041. av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  2042. 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);
  2043. return -1;
  2044. }
  2045. if(++s->mb_x >= s->mb_width){
  2046. s->mb_x=0;
  2047. loop_filter(h);
  2048. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  2049. ++s->mb_y;
  2050. if(FIELD_OR_MBAFF_PICTURE) {
  2051. ++s->mb_y;
  2052. if(FRAME_MBAFF && s->mb_y < s->mb_height)
  2053. predict_field_decoding_flag(h);
  2054. }
  2055. if(s->mb_y >= s->mb_height){
  2056. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  2057. if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
  2058. 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);
  2059. return 0;
  2060. }else{
  2061. 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);
  2062. return -1;
  2063. }
  2064. }
  2065. }
  2066. if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
  2067. tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
  2068. if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
  2069. 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);
  2070. return 0;
  2071. }else{
  2072. 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);
  2073. return -1;
  2074. }
  2075. }
  2076. }
  2077. }
  2078. #if 0
  2079. for(;s->mb_y < s->mb_height; s->mb_y++){
  2080. for(;s->mb_x < s->mb_width; s->mb_x++){
  2081. int ret= decode_mb(h);
  2082. ff_h264_hl_decode_mb(h);
  2083. if(ret<0){
  2084. av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  2085. 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);
  2086. return -1;
  2087. }
  2088. if(++s->mb_x >= s->mb_width){
  2089. s->mb_x=0;
  2090. if(++s->mb_y >= s->mb_height){
  2091. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  2092. 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);
  2093. return 0;
  2094. }else{
  2095. 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);
  2096. return -1;
  2097. }
  2098. }
  2099. }
  2100. if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
  2101. if(get_bits_count(s->gb) == s->gb.size_in_bits){
  2102. 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);
  2103. return 0;
  2104. }else{
  2105. 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);
  2106. return -1;
  2107. }
  2108. }
  2109. }
  2110. s->mb_x=0;
  2111. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  2112. }
  2113. #endif
  2114. return -1; //not reached
  2115. }
  2116. /**
  2117. * Call decode_slice() for each context.
  2118. *
  2119. * @param h h264 master context
  2120. * @param context_count number of contexts to execute
  2121. */
  2122. static void execute_decode_slices(H264Context *h, int context_count){
  2123. MpegEncContext * const s = &h->s;
  2124. AVCodecContext * const avctx= s->avctx;
  2125. H264Context *hx;
  2126. int i;
  2127. if (s->avctx->hwaccel)
  2128. return;
  2129. if(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  2130. return;
  2131. if(context_count == 1) {
  2132. decode_slice(avctx, &h);
  2133. } else {
  2134. for(i = 1; i < context_count; i++) {
  2135. hx = h->thread_context[i];
  2136. hx->s.error_recognition = avctx->error_recognition;
  2137. hx->s.error_count = 0;
  2138. }
  2139. avctx->execute(avctx, (void *)decode_slice,
  2140. h->thread_context, NULL, context_count, sizeof(void*));
  2141. /* pull back stuff from slices to master context */
  2142. hx = h->thread_context[context_count - 1];
  2143. s->mb_x = hx->s.mb_x;
  2144. s->mb_y = hx->s.mb_y;
  2145. s->dropable = hx->s.dropable;
  2146. s->picture_structure = hx->s.picture_structure;
  2147. for(i = 1; i < context_count; i++)
  2148. h->s.error_count += h->thread_context[i]->s.error_count;
  2149. }
  2150. }
  2151. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size){
  2152. MpegEncContext * const s = &h->s;
  2153. AVCodecContext * const avctx= s->avctx;
  2154. int buf_index=0;
  2155. H264Context *hx; ///< thread context
  2156. int context_count = 0;
  2157. int next_avc= h->is_avc ? 0 : buf_size;
  2158. h->max_contexts = avctx->thread_count;
  2159. #if 0
  2160. int i;
  2161. for(i=0; i<50; i++){
  2162. av_log(NULL, AV_LOG_ERROR,"%02X ", buf[i]);
  2163. }
  2164. #endif
  2165. if(!(s->flags2 & CODEC_FLAG2_CHUNKS)){
  2166. h->current_slice = 0;
  2167. if (!s->first_field)
  2168. s->current_picture_ptr= NULL;
  2169. ff_h264_reset_sei(h);
  2170. }
  2171. for(;;){
  2172. int consumed;
  2173. int dst_length;
  2174. int bit_length;
  2175. const uint8_t *ptr;
  2176. int i, nalsize = 0;
  2177. int err;
  2178. if(buf_index >= next_avc) {
  2179. if(buf_index >= buf_size) break;
  2180. nalsize = 0;
  2181. for(i = 0; i < h->nal_length_size; i++)
  2182. nalsize = (nalsize << 8) | buf[buf_index++];
  2183. if(nalsize <= 1 || nalsize > buf_size - buf_index){
  2184. if(nalsize == 1){
  2185. buf_index++;
  2186. continue;
  2187. }else{
  2188. av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
  2189. break;
  2190. }
  2191. }
  2192. next_avc= buf_index + nalsize;
  2193. } else {
  2194. // start code prefix search
  2195. for(; buf_index + 3 < next_avc; buf_index++){
  2196. // This should always succeed in the first iteration.
  2197. if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
  2198. break;
  2199. }
  2200. if(buf_index+3 >= buf_size) break;
  2201. buf_index+=3;
  2202. if(buf_index >= next_avc) continue;
  2203. }
  2204. hx = h->thread_context[context_count];
  2205. ptr= ff_h264_decode_nal(hx, buf + buf_index, &dst_length, &consumed, next_avc - buf_index);
  2206. if (ptr==NULL || dst_length < 0){
  2207. return -1;
  2208. }
  2209. i= buf_index + consumed;
  2210. if((s->workaround_bugs & FF_BUG_AUTODETECT) && i+3<next_avc &&
  2211. buf[i]==0x00 && buf[i+1]==0x00 && buf[i+2]==0x01 && buf[i+3]==0xE0)
  2212. s->workaround_bugs |= FF_BUG_TRUNCATED;
  2213. if(!(s->workaround_bugs & FF_BUG_TRUNCATED)){
  2214. while(ptr[dst_length - 1] == 0 && dst_length > 0)
  2215. dst_length--;
  2216. }
  2217. bit_length= !dst_length ? 0 : (8*dst_length - ff_h264_decode_rbsp_trailing(h, ptr + dst_length - 1));
  2218. if(s->avctx->debug&FF_DEBUG_STARTCODE){
  2219. av_log(h->s.avctx, AV_LOG_DEBUG, "NAL %d at %d/%d length %d\n", hx->nal_unit_type, buf_index, buf_size, dst_length);
  2220. }
  2221. if (h->is_avc && (nalsize != consumed) && nalsize){
  2222. av_log(h->s.avctx, AV_LOG_DEBUG, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
  2223. }
  2224. buf_index += consumed;
  2225. if( (s->hurry_up == 1 && h->nal_ref_idc == 0) //FIXME do not discard SEI id
  2226. ||(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
  2227. continue;
  2228. again:
  2229. err = 0;
  2230. switch(hx->nal_unit_type){
  2231. case NAL_IDR_SLICE:
  2232. if (h->nal_unit_type != NAL_IDR_SLICE) {
  2233. av_log(h->s.avctx, AV_LOG_ERROR, "Invalid mix of idr and non-idr slices");
  2234. return -1;
  2235. }
  2236. idr(h); //FIXME ensure we don't loose some frames if there is reordering
  2237. case NAL_SLICE:
  2238. init_get_bits(&hx->s.gb, ptr, bit_length);
  2239. hx->intra_gb_ptr=
  2240. hx->inter_gb_ptr= &hx->s.gb;
  2241. hx->s.data_partitioning = 0;
  2242. if((err = decode_slice_header(hx, h)))
  2243. break;
  2244. avctx->profile = hx->sps.profile_idc;
  2245. avctx->level = hx->sps.level_idc;
  2246. if (s->avctx->hwaccel && h->current_slice == 1) {
  2247. if (s->avctx->hwaccel->start_frame(s->avctx, NULL, 0) < 0)
  2248. return -1;
  2249. }
  2250. s->current_picture_ptr->key_frame |=
  2251. (hx->nal_unit_type == NAL_IDR_SLICE) ||
  2252. (h->sei_recovery_frame_cnt >= 0);
  2253. if(hx->redundant_pic_count==0 && hx->s.hurry_up < 5
  2254. && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
  2255. && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=FF_B_TYPE)
  2256. && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==FF_I_TYPE)
  2257. && avctx->skip_frame < AVDISCARD_ALL){
  2258. if(avctx->hwaccel) {
  2259. if (avctx->hwaccel->decode_slice(avctx, &buf[buf_index - consumed], consumed) < 0)
  2260. return -1;
  2261. }else
  2262. if(CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
  2263. static const uint8_t start_code[] = {0x00, 0x00, 0x01};
  2264. ff_vdpau_add_data_chunk(s, start_code, sizeof(start_code));
  2265. ff_vdpau_add_data_chunk(s, &buf[buf_index - consumed], consumed );
  2266. }else
  2267. context_count++;
  2268. }
  2269. break;
  2270. case NAL_DPA:
  2271. init_get_bits(&hx->s.gb, ptr, bit_length);
  2272. hx->intra_gb_ptr=
  2273. hx->inter_gb_ptr= NULL;
  2274. if ((err = decode_slice_header(hx, h)) < 0)
  2275. break;
  2276. avctx->profile = hx->sps.profile_idc;
  2277. avctx->level = hx->sps.level_idc;
  2278. hx->s.data_partitioning = 1;
  2279. break;
  2280. case NAL_DPB:
  2281. init_get_bits(&hx->intra_gb, ptr, bit_length);
  2282. hx->intra_gb_ptr= &hx->intra_gb;
  2283. break;
  2284. case NAL_DPC:
  2285. init_get_bits(&hx->inter_gb, ptr, bit_length);
  2286. hx->inter_gb_ptr= &hx->inter_gb;
  2287. if(hx->redundant_pic_count==0 && hx->intra_gb_ptr && hx->s.data_partitioning
  2288. && s->context_initialized
  2289. && s->hurry_up < 5
  2290. && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
  2291. && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=FF_B_TYPE)
  2292. && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==FF_I_TYPE)
  2293. && avctx->skip_frame < AVDISCARD_ALL)
  2294. context_count++;
  2295. break;
  2296. case NAL_SEI:
  2297. init_get_bits(&s->gb, ptr, bit_length);
  2298. ff_h264_decode_sei(h);
  2299. break;
  2300. case NAL_SPS:
  2301. init_get_bits(&s->gb, ptr, bit_length);
  2302. ff_h264_decode_seq_parameter_set(h);
  2303. if(s->flags& CODEC_FLAG_LOW_DELAY)
  2304. s->low_delay=1;
  2305. if(avctx->has_b_frames < 2)
  2306. avctx->has_b_frames= !s->low_delay;
  2307. break;
  2308. case NAL_PPS:
  2309. init_get_bits(&s->gb, ptr, bit_length);
  2310. ff_h264_decode_picture_parameter_set(h, bit_length);
  2311. break;
  2312. case NAL_AUD:
  2313. case NAL_END_SEQUENCE:
  2314. case NAL_END_STREAM:
  2315. case NAL_FILLER_DATA:
  2316. case NAL_SPS_EXT:
  2317. case NAL_AUXILIARY_SLICE:
  2318. break;
  2319. default:
  2320. av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n", hx->nal_unit_type, bit_length);
  2321. }
  2322. if(context_count == h->max_contexts) {
  2323. execute_decode_slices(h, context_count);
  2324. context_count = 0;
  2325. }
  2326. if (err < 0)
  2327. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  2328. else if(err == 1) {
  2329. /* Slice could not be decoded in parallel mode, copy down
  2330. * NAL unit stuff to context 0 and restart. Note that
  2331. * rbsp_buffer is not transferred, but since we no longer
  2332. * run in parallel mode this should not be an issue. */
  2333. h->nal_unit_type = hx->nal_unit_type;
  2334. h->nal_ref_idc = hx->nal_ref_idc;
  2335. hx = h;
  2336. goto again;
  2337. }
  2338. }
  2339. if(context_count)
  2340. execute_decode_slices(h, context_count);
  2341. return buf_index;
  2342. }
  2343. /**
  2344. * returns the number of bytes consumed for building the current frame
  2345. */
  2346. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
  2347. if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
  2348. if(pos+10>buf_size) pos=buf_size; // oops ;)
  2349. return pos;
  2350. }
  2351. static int decode_frame(AVCodecContext *avctx,
  2352. void *data, int *data_size,
  2353. AVPacket *avpkt)
  2354. {
  2355. const uint8_t *buf = avpkt->data;
  2356. int buf_size = avpkt->size;
  2357. H264Context *h = avctx->priv_data;
  2358. MpegEncContext *s = &h->s;
  2359. AVFrame *pict = data;
  2360. int buf_index;
  2361. s->flags= avctx->flags;
  2362. s->flags2= avctx->flags2;
  2363. /* end of stream, output what is still in the buffers */
  2364. if (buf_size == 0) {
  2365. Picture *out;
  2366. int i, out_idx;
  2367. //FIXME factorize this with the output code below
  2368. out = h->delayed_pic[0];
  2369. out_idx = 0;
  2370. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++)
  2371. if(h->delayed_pic[i]->poc < out->poc){
  2372. out = h->delayed_pic[i];
  2373. out_idx = i;
  2374. }
  2375. for(i=out_idx; h->delayed_pic[i]; i++)
  2376. h->delayed_pic[i] = h->delayed_pic[i+1];
  2377. if(out){
  2378. *data_size = sizeof(AVFrame);
  2379. *pict= *(AVFrame*)out;
  2380. }
  2381. return 0;
  2382. }
  2383. buf_index=decode_nal_units(h, buf, buf_size);
  2384. if(buf_index < 0)
  2385. return -1;
  2386. if(!(s->flags2 & CODEC_FLAG2_CHUNKS) && !s->current_picture_ptr){
  2387. if (avctx->skip_frame >= AVDISCARD_NONREF || s->hurry_up) return 0;
  2388. av_log(avctx, AV_LOG_ERROR, "no frame!\n");
  2389. return -1;
  2390. }
  2391. if(!(s->flags2 & CODEC_FLAG2_CHUNKS) || (s->mb_y >= s->mb_height && s->mb_height)){
  2392. Picture *out = s->current_picture_ptr;
  2393. Picture *cur = s->current_picture_ptr;
  2394. int i, pics, out_of_order, out_idx;
  2395. field_end(h);
  2396. if (cur->field_poc[0]==INT_MAX || cur->field_poc[1]==INT_MAX) {
  2397. /* Wait for second field. */
  2398. *data_size = 0;
  2399. } else {
  2400. cur->interlaced_frame = 0;
  2401. cur->repeat_pict = 0;
  2402. /* Signal interlacing information externally. */
  2403. /* Prioritize picture timing SEI information over used decoding process if it exists. */
  2404. if(h->sps.pic_struct_present_flag){
  2405. switch (h->sei_pic_struct)
  2406. {
  2407. case SEI_PIC_STRUCT_FRAME:
  2408. break;
  2409. case SEI_PIC_STRUCT_TOP_FIELD:
  2410. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  2411. cur->interlaced_frame = 1;
  2412. break;
  2413. case SEI_PIC_STRUCT_TOP_BOTTOM:
  2414. case SEI_PIC_STRUCT_BOTTOM_TOP:
  2415. if (FIELD_OR_MBAFF_PICTURE)
  2416. cur->interlaced_frame = 1;
  2417. else
  2418. // try to flag soft telecine progressive
  2419. cur->interlaced_frame = h->prev_interlaced_frame;
  2420. break;
  2421. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  2422. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  2423. // Signal the possibility of telecined film externally (pic_struct 5,6)
  2424. // From these hints, let the applications decide if they apply deinterlacing.
  2425. cur->repeat_pict = 1;
  2426. break;
  2427. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  2428. // Force progressive here, as doubling interlaced frame is a bad idea.
  2429. cur->repeat_pict = 2;
  2430. break;
  2431. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  2432. cur->repeat_pict = 4;
  2433. break;
  2434. }
  2435. if ((h->sei_ct_type & 3) && h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP)
  2436. cur->interlaced_frame = (h->sei_ct_type & (1<<1)) != 0;
  2437. }else{
  2438. /* Derive interlacing flag from used decoding process. */
  2439. cur->interlaced_frame = FIELD_OR_MBAFF_PICTURE;
  2440. }
  2441. h->prev_interlaced_frame = cur->interlaced_frame;
  2442. if (cur->field_poc[0] != cur->field_poc[1]){
  2443. /* Derive top_field_first from field pocs. */
  2444. cur->top_field_first = cur->field_poc[0] < cur->field_poc[1];
  2445. }else{
  2446. if(cur->interlaced_frame || h->sps.pic_struct_present_flag){
  2447. /* Use picture timing SEI information. Even if it is a information of a past frame, better than nothing. */
  2448. if(h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM
  2449. || h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
  2450. cur->top_field_first = 1;
  2451. else
  2452. cur->top_field_first = 0;
  2453. }else{
  2454. /* Most likely progressive */
  2455. cur->top_field_first = 0;
  2456. }
  2457. }
  2458. //FIXME do something with unavailable reference frames
  2459. /* Sort B-frames into display order */
  2460. if(h->sps.bitstream_restriction_flag
  2461. && s->avctx->has_b_frames < h->sps.num_reorder_frames){
  2462. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  2463. s->low_delay = 0;
  2464. }
  2465. if( s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT
  2466. && !h->sps.bitstream_restriction_flag){
  2467. s->avctx->has_b_frames= MAX_DELAYED_PIC_COUNT;
  2468. s->low_delay= 0;
  2469. }
  2470. pics = 0;
  2471. while(h->delayed_pic[pics]) pics++;
  2472. assert(pics <= MAX_DELAYED_PIC_COUNT);
  2473. h->delayed_pic[pics++] = cur;
  2474. if(cur->reference == 0)
  2475. cur->reference = DELAYED_PIC_REF;
  2476. out = h->delayed_pic[0];
  2477. out_idx = 0;
  2478. for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++)
  2479. if(h->delayed_pic[i]->poc < out->poc){
  2480. out = h->delayed_pic[i];
  2481. out_idx = i;
  2482. }
  2483. if(s->avctx->has_b_frames == 0 && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset))
  2484. h->outputed_poc= INT_MIN;
  2485. out_of_order = out->poc < h->outputed_poc;
  2486. if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
  2487. { }
  2488. else if((out_of_order && pics-1 == s->avctx->has_b_frames && s->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT)
  2489. || (s->low_delay &&
  2490. ((h->outputed_poc != INT_MIN && out->poc > h->outputed_poc + 2)
  2491. || cur->pict_type == FF_B_TYPE)))
  2492. {
  2493. s->low_delay = 0;
  2494. s->avctx->has_b_frames++;
  2495. }
  2496. if(out_of_order || pics > s->avctx->has_b_frames){
  2497. out->reference &= ~DELAYED_PIC_REF;
  2498. for(i=out_idx; h->delayed_pic[i]; i++)
  2499. h->delayed_pic[i] = h->delayed_pic[i+1];
  2500. }
  2501. if(!out_of_order && pics > s->avctx->has_b_frames){
  2502. *data_size = sizeof(AVFrame);
  2503. if(out_idx==0 && h->delayed_pic[0] && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset)) {
  2504. h->outputed_poc = INT_MIN;
  2505. } else
  2506. h->outputed_poc = out->poc;
  2507. *pict= *(AVFrame*)out;
  2508. }else{
  2509. av_log(avctx, AV_LOG_DEBUG, "no picture\n");
  2510. }
  2511. }
  2512. }
  2513. assert(pict->data[0] || !*data_size);
  2514. ff_print_debug_info(s, pict);
  2515. //printf("out %d\n", (int)pict->data[0]);
  2516. return get_consumed_bytes(s, buf_index, buf_size);
  2517. }
  2518. #if 0
  2519. static inline void fill_mb_avail(H264Context *h){
  2520. MpegEncContext * const s = &h->s;
  2521. const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
  2522. if(s->mb_y){
  2523. h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
  2524. h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
  2525. h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
  2526. }else{
  2527. h->mb_avail[0]=
  2528. h->mb_avail[1]=
  2529. h->mb_avail[2]= 0;
  2530. }
  2531. h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
  2532. h->mb_avail[4]= 1; //FIXME move out
  2533. h->mb_avail[5]= 0; //FIXME move out
  2534. }
  2535. #endif
  2536. #ifdef TEST
  2537. #undef printf
  2538. #undef random
  2539. #define COUNT 8000
  2540. #define SIZE (COUNT*40)
  2541. int main(void){
  2542. int i;
  2543. uint8_t temp[SIZE];
  2544. PutBitContext pb;
  2545. GetBitContext gb;
  2546. // int int_temp[10000];
  2547. DSPContext dsp;
  2548. AVCodecContext avctx;
  2549. dsputil_init(&dsp, &avctx);
  2550. init_put_bits(&pb, temp, SIZE);
  2551. printf("testing unsigned exp golomb\n");
  2552. for(i=0; i<COUNT; i++){
  2553. START_TIMER
  2554. set_ue_golomb(&pb, i);
  2555. STOP_TIMER("set_ue_golomb");
  2556. }
  2557. flush_put_bits(&pb);
  2558. init_get_bits(&gb, temp, 8*SIZE);
  2559. for(i=0; i<COUNT; i++){
  2560. int j, s;
  2561. s= show_bits(&gb, 24);
  2562. START_TIMER
  2563. j= get_ue_golomb(&gb);
  2564. if(j != i){
  2565. printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  2566. // return -1;
  2567. }
  2568. STOP_TIMER("get_ue_golomb");
  2569. }
  2570. init_put_bits(&pb, temp, SIZE);
  2571. printf("testing signed exp golomb\n");
  2572. for(i=0; i<COUNT; i++){
  2573. START_TIMER
  2574. set_se_golomb(&pb, i - COUNT/2);
  2575. STOP_TIMER("set_se_golomb");
  2576. }
  2577. flush_put_bits(&pb);
  2578. init_get_bits(&gb, temp, 8*SIZE);
  2579. for(i=0; i<COUNT; i++){
  2580. int j, s;
  2581. s= show_bits(&gb, 24);
  2582. START_TIMER
  2583. j= get_se_golomb(&gb);
  2584. if(j != i - COUNT/2){
  2585. printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
  2586. // return -1;
  2587. }
  2588. STOP_TIMER("get_se_golomb");
  2589. }
  2590. #if 0
  2591. printf("testing 4x4 (I)DCT\n");
  2592. DCTELEM block[16];
  2593. uint8_t src[16], ref[16];
  2594. uint64_t error= 0, max_error=0;
  2595. for(i=0; i<COUNT; i++){
  2596. int j;
  2597. // printf("%d %d %d\n", r1, r2, (r2-r1)*16);
  2598. for(j=0; j<16; j++){
  2599. ref[j]= random()%255;
  2600. src[j]= random()%255;
  2601. }
  2602. h264_diff_dct_c(block, src, ref, 4);
  2603. //normalize
  2604. for(j=0; j<16; j++){
  2605. // printf("%d ", block[j]);
  2606. block[j]= block[j]*4;
  2607. if(j&1) block[j]= (block[j]*4 + 2)/5;
  2608. if(j&4) block[j]= (block[j]*4 + 2)/5;
  2609. }
  2610. // printf("\n");
  2611. s->dsp.h264_idct_add(ref, block, 4);
  2612. /* for(j=0; j<16; j++){
  2613. printf("%d ", ref[j]);
  2614. }
  2615. printf("\n");*/
  2616. for(j=0; j<16; j++){
  2617. int diff= FFABS(src[j] - ref[j]);
  2618. error+= diff*diff;
  2619. max_error= FFMAX(max_error, diff);
  2620. }
  2621. }
  2622. printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
  2623. printf("testing quantizer\n");
  2624. for(qp=0; qp<52; qp++){
  2625. for(i=0; i<16; i++)
  2626. src1_block[i]= src2_block[i]= random()%255;
  2627. }
  2628. printf("Testing NAL layer\n");
  2629. uint8_t bitstream[COUNT];
  2630. uint8_t nal[COUNT*2];
  2631. H264Context h;
  2632. memset(&h, 0, sizeof(H264Context));
  2633. for(i=0; i<COUNT; i++){
  2634. int zeros= i;
  2635. int nal_length;
  2636. int consumed;
  2637. int out_length;
  2638. uint8_t *out;
  2639. int j;
  2640. for(j=0; j<COUNT; j++){
  2641. bitstream[j]= (random() % 255) + 1;
  2642. }
  2643. for(j=0; j<zeros; j++){
  2644. int pos= random() % COUNT;
  2645. while(bitstream[pos] == 0){
  2646. pos++;
  2647. pos %= COUNT;
  2648. }
  2649. bitstream[pos]=0;
  2650. }
  2651. START_TIMER
  2652. nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
  2653. if(nal_length<0){
  2654. printf("encoding failed\n");
  2655. return -1;
  2656. }
  2657. out= ff_h264_decode_nal(&h, nal, &out_length, &consumed, nal_length);
  2658. STOP_TIMER("NAL")
  2659. if(out_length != COUNT){
  2660. printf("incorrect length %d %d\n", out_length, COUNT);
  2661. return -1;
  2662. }
  2663. if(consumed != nal_length){
  2664. printf("incorrect consumed length %d %d\n", nal_length, consumed);
  2665. return -1;
  2666. }
  2667. if(memcmp(bitstream, out, COUNT)){
  2668. printf("mismatch\n");
  2669. return -1;
  2670. }
  2671. }
  2672. #endif
  2673. printf("Testing RBSP\n");
  2674. return 0;
  2675. }
  2676. #endif /* TEST */
  2677. av_cold void ff_h264_free_context(H264Context *h)
  2678. {
  2679. int i;
  2680. free_tables(h); //FIXME cleanup init stuff perhaps
  2681. for(i = 0; i < MAX_SPS_COUNT; i++)
  2682. av_freep(h->sps_buffers + i);
  2683. for(i = 0; i < MAX_PPS_COUNT; i++)
  2684. av_freep(h->pps_buffers + i);
  2685. }
  2686. av_cold int ff_h264_decode_end(AVCodecContext *avctx)
  2687. {
  2688. H264Context *h = avctx->priv_data;
  2689. MpegEncContext *s = &h->s;
  2690. ff_h264_free_context(h);
  2691. MPV_common_end(s);
  2692. // memset(h, 0, sizeof(H264Context));
  2693. return 0;
  2694. }
  2695. AVCodec h264_decoder = {
  2696. "h264",
  2697. CODEC_TYPE_VIDEO,
  2698. CODEC_ID_H264,
  2699. sizeof(H264Context),
  2700. ff_h264_decode_init,
  2701. NULL,
  2702. ff_h264_decode_end,
  2703. decode_frame,
  2704. /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_DELAY,
  2705. .flush= flush_dpb,
  2706. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
  2707. .pix_fmts= ff_hwaccel_pixfmt_list_420,
  2708. };
  2709. #if CONFIG_H264_VDPAU_DECODER
  2710. AVCodec h264_vdpau_decoder = {
  2711. "h264_vdpau",
  2712. CODEC_TYPE_VIDEO,
  2713. CODEC_ID_H264,
  2714. sizeof(H264Context),
  2715. ff_h264_decode_init,
  2716. NULL,
  2717. ff_h264_decode_end,
  2718. decode_frame,
  2719. CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
  2720. .flush= flush_dpb,
  2721. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),
  2722. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_VDPAU_H264, PIX_FMT_NONE},
  2723. };
  2724. #endif