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.

812 lines
27KB

  1. /*
  2. * H.263 decoder
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * H.263 decoder.
  25. */
  26. #define UNCHECKED_BITSTREAM_READER 1
  27. #include "libavutil/cpu.h"
  28. #include "internal.h"
  29. #include "avcodec.h"
  30. #include "error_resilience.h"
  31. #include "mpegvideo.h"
  32. #include "h263.h"
  33. #include "h263_parser.h"
  34. #include "mpeg4video_parser.h"
  35. #include "msmpeg4.h"
  36. #include "vdpau_internal.h"
  37. #include "thread.h"
  38. #include "flv.h"
  39. #include "mpeg4video.h"
  40. //#define DEBUG
  41. //#define PRINT_FRAME_TIME
  42. av_cold int ff_h263_decode_init(AVCodecContext *avctx)
  43. {
  44. MpegEncContext *s = avctx->priv_data;
  45. int ret;
  46. s->avctx = avctx;
  47. s->out_format = FMT_H263;
  48. s->width = avctx->coded_width;
  49. s->height = avctx->coded_height;
  50. s->workaround_bugs= avctx->workaround_bugs;
  51. // set defaults
  52. ff_MPV_decode_defaults(s);
  53. s->quant_precision=5;
  54. s->decode_mb= ff_h263_decode_mb;
  55. s->low_delay= 1;
  56. if (avctx->codec->id == AV_CODEC_ID_MSS2)
  57. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  58. else
  59. avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
  60. s->unrestricted_mv= 1;
  61. /* select sub codec */
  62. switch(avctx->codec->id) {
  63. case AV_CODEC_ID_H263:
  64. case AV_CODEC_ID_H263P:
  65. s->unrestricted_mv= 0;
  66. avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
  67. break;
  68. case AV_CODEC_ID_MPEG4:
  69. break;
  70. case AV_CODEC_ID_MSMPEG4V1:
  71. s->h263_pred = 1;
  72. s->msmpeg4_version=1;
  73. break;
  74. case AV_CODEC_ID_MSMPEG4V2:
  75. s->h263_pred = 1;
  76. s->msmpeg4_version=2;
  77. break;
  78. case AV_CODEC_ID_MSMPEG4V3:
  79. s->h263_pred = 1;
  80. s->msmpeg4_version=3;
  81. break;
  82. case AV_CODEC_ID_WMV1:
  83. s->h263_pred = 1;
  84. s->msmpeg4_version=4;
  85. break;
  86. case AV_CODEC_ID_WMV2:
  87. s->h263_pred = 1;
  88. s->msmpeg4_version=5;
  89. break;
  90. case AV_CODEC_ID_VC1:
  91. case AV_CODEC_ID_WMV3:
  92. case AV_CODEC_ID_VC1IMAGE:
  93. case AV_CODEC_ID_WMV3IMAGE:
  94. case AV_CODEC_ID_MSS2:
  95. s->h263_pred = 1;
  96. s->msmpeg4_version=6;
  97. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  98. break;
  99. case AV_CODEC_ID_H263I:
  100. break;
  101. case AV_CODEC_ID_FLV1:
  102. s->h263_flv = 1;
  103. break;
  104. default:
  105. return AVERROR(EINVAL);
  106. }
  107. s->codec_id= avctx->codec->id;
  108. avctx->hwaccel= ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
  109. /* for h263, we allocate the images after having read the header */
  110. if (avctx->codec->id != AV_CODEC_ID_H263 && avctx->codec->id != AV_CODEC_ID_H263P && avctx->codec->id != AV_CODEC_ID_MPEG4)
  111. if ((ret = ff_MPV_common_init(s)) < 0)
  112. return ret;
  113. ff_h263_decode_init_vlc();
  114. return 0;
  115. }
  116. av_cold int ff_h263_decode_end(AVCodecContext *avctx)
  117. {
  118. MpegEncContext *s = avctx->priv_data;
  119. ff_MPV_common_end(s);
  120. return 0;
  121. }
  122. /**
  123. * Return the number of bytes consumed for building the current frame.
  124. */
  125. static int get_consumed_bytes(MpegEncContext *s, int buf_size){
  126. int pos= (get_bits_count(&s->gb)+7)>>3;
  127. if(s->divx_packed || s->avctx->hwaccel){
  128. //we would have to scan through the whole buf to handle the weird reordering ...
  129. return buf_size;
  130. }else if(s->flags&CODEC_FLAG_TRUNCATED){
  131. pos -= s->parse_context.last_index;
  132. if(pos<0) pos=0; // padding is not really read so this might be -1
  133. return pos;
  134. }else{
  135. if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
  136. if(pos+10>buf_size) pos=buf_size; // oops ;)
  137. return pos;
  138. }
  139. }
  140. static int decode_slice(MpegEncContext *s){
  141. const int part_mask= s->partitioned_frame ? (ER_AC_END|ER_AC_ERROR) : 0x7F;
  142. const int mb_size= 16>>s->avctx->lowres;
  143. int ret;
  144. s->last_resync_gb= s->gb;
  145. s->first_slice_line= 1;
  146. s->resync_mb_x= s->mb_x;
  147. s->resync_mb_y= s->mb_y;
  148. ff_set_qscale(s, s->qscale);
  149. if (s->avctx->hwaccel) {
  150. const uint8_t *start= s->gb.buffer + get_bits_count(&s->gb)/8;
  151. const uint8_t *end = ff_h263_find_resync_marker(s, start + 1, s->gb.buffer_end);
  152. skip_bits_long(&s->gb, 8*(end - start));
  153. return s->avctx->hwaccel->decode_slice(s->avctx, start, end - start);
  154. }
  155. if(s->partitioned_frame){
  156. const int qscale= s->qscale;
  157. if(CONFIG_MPEG4_DECODER && s->codec_id==AV_CODEC_ID_MPEG4){
  158. if ((ret = ff_mpeg4_decode_partitions(s)) < 0)
  159. return ret;
  160. }
  161. /* restore variables which were modified */
  162. s->first_slice_line=1;
  163. s->mb_x= s->resync_mb_x;
  164. s->mb_y= s->resync_mb_y;
  165. ff_set_qscale(s, qscale);
  166. }
  167. for(; s->mb_y < s->mb_height; s->mb_y++) {
  168. /* per-row end of slice checks */
  169. if(s->msmpeg4_version){
  170. if(s->resync_mb_y + s->slice_height == s->mb_y){
  171. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_END);
  172. return 0;
  173. }
  174. }
  175. if(s->msmpeg4_version==1){
  176. s->last_dc[0]=
  177. s->last_dc[1]=
  178. s->last_dc[2]= 128;
  179. }
  180. ff_init_block_index(s);
  181. for(; s->mb_x < s->mb_width; s->mb_x++) {
  182. int ret;
  183. ff_update_block_index(s);
  184. if(s->resync_mb_x == s->mb_x && s->resync_mb_y+1 == s->mb_y){
  185. s->first_slice_line=0;
  186. }
  187. /* DCT & quantize */
  188. s->mv_dir = MV_DIR_FORWARD;
  189. s->mv_type = MV_TYPE_16X16;
  190. // s->mb_skipped = 0;
  191. av_dlog(s, "%d %d %06X\n",
  192. ret, get_bits_count(&s->gb), show_bits(&s->gb, 24));
  193. ret= s->decode_mb(s, s->block);
  194. if (s->pict_type!=AV_PICTURE_TYPE_B)
  195. ff_h263_update_motion_val(s);
  196. if(ret<0){
  197. const int xy= s->mb_x + s->mb_y*s->mb_stride;
  198. if(ret==SLICE_END){
  199. ff_MPV_decode_mb(s, s->block);
  200. if(s->loop_filter)
  201. ff_h263_loop_filter(s);
  202. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, ER_MB_END&part_mask);
  203. s->padding_bug_score--;
  204. if(++s->mb_x >= s->mb_width){
  205. s->mb_x=0;
  206. ff_mpeg_draw_horiz_band(s, s->mb_y*mb_size, mb_size);
  207. ff_MPV_report_decode_progress(s);
  208. s->mb_y++;
  209. }
  210. return 0;
  211. }else if(ret==SLICE_NOEND){
  212. av_log(s->avctx, AV_LOG_ERROR, "Slice mismatch at MB: %d\n", xy);
  213. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x+1, s->mb_y, ER_MB_END&part_mask);
  214. return AVERROR_INVALIDDATA;
  215. }
  216. av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", xy);
  217. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR&part_mask);
  218. return AVERROR_INVALIDDATA;
  219. }
  220. ff_MPV_decode_mb(s, s->block);
  221. if(s->loop_filter)
  222. ff_h263_loop_filter(s);
  223. }
  224. ff_mpeg_draw_horiz_band(s, s->mb_y*mb_size, mb_size);
  225. ff_MPV_report_decode_progress(s);
  226. s->mb_x= 0;
  227. }
  228. av_assert1(s->mb_x==0 && s->mb_y==s->mb_height);
  229. if(s->codec_id==AV_CODEC_ID_MPEG4
  230. && (s->workaround_bugs&FF_BUG_AUTODETECT)
  231. && get_bits_left(&s->gb) >= 48
  232. && show_bits(&s->gb, 24)==0x4010
  233. && !s->data_partitioning)
  234. s->padding_bug_score+=32;
  235. /* try to detect the padding bug */
  236. if( s->codec_id==AV_CODEC_ID_MPEG4
  237. && (s->workaround_bugs&FF_BUG_AUTODETECT)
  238. && get_bits_left(&s->gb) >=0
  239. && get_bits_left(&s->gb) < 137
  240. // && !s->resync_marker
  241. && !s->data_partitioning){
  242. const int bits_count= get_bits_count(&s->gb);
  243. const int bits_left = s->gb.size_in_bits - bits_count;
  244. if(bits_left==0){
  245. s->padding_bug_score+=16;
  246. } else if(bits_left != 1){
  247. int v= show_bits(&s->gb, 8);
  248. v|= 0x7F >> (7-(bits_count&7));
  249. if(v==0x7F && bits_left<=8)
  250. s->padding_bug_score--;
  251. else if(v==0x7F && ((get_bits_count(&s->gb)+8)&8) && bits_left<=16)
  252. s->padding_bug_score+= 4;
  253. else
  254. s->padding_bug_score++;
  255. }
  256. }
  257. if(s->workaround_bugs&FF_BUG_AUTODETECT){
  258. if(s->padding_bug_score > -2 && !s->data_partitioning /*&& (s->divx_version>=0 || !s->resync_marker)*/)
  259. s->workaround_bugs |= FF_BUG_NO_PADDING;
  260. else
  261. s->workaround_bugs &= ~FF_BUG_NO_PADDING;
  262. }
  263. // handle formats which don't have unique end markers
  264. if(s->msmpeg4_version || (s->workaround_bugs&FF_BUG_NO_PADDING)){ //FIXME perhaps solve this more cleanly
  265. int left= get_bits_left(&s->gb);
  266. int max_extra=7;
  267. /* no markers in M$ crap */
  268. if(s->msmpeg4_version && s->pict_type==AV_PICTURE_TYPE_I)
  269. max_extra+= 17;
  270. /* buggy padding but the frame should still end approximately at the bitstream end */
  271. if((s->workaround_bugs&FF_BUG_NO_PADDING) && (s->err_recognition&(AV_EF_BUFFER|AV_EF_AGGRESSIVE)))
  272. max_extra+= 48;
  273. else if((s->workaround_bugs&FF_BUG_NO_PADDING))
  274. max_extra+= 256*256*256*64;
  275. if(left>max_extra){
  276. av_log(s->avctx, AV_LOG_ERROR, "discarding %d junk bits at end, next would be %X\n", left, show_bits(&s->gb, 24));
  277. }
  278. else if(left<0){
  279. av_log(s->avctx, AV_LOG_ERROR, "overreading %d bits\n", -left);
  280. }else
  281. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_END);
  282. return 0;
  283. }
  284. av_log(s->avctx, AV_LOG_ERROR, "slice end not reached but screenspace end (%d left %06X, score= %d)\n",
  285. get_bits_left(&s->gb),
  286. show_bits(&s->gb, 24), s->padding_bug_score);
  287. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, ER_MB_END&part_mask);
  288. return AVERROR_INVALIDDATA;
  289. }
  290. int ff_h263_decode_frame(AVCodecContext *avctx,
  291. void *data, int *got_frame,
  292. AVPacket *avpkt)
  293. {
  294. const uint8_t *buf = avpkt->data;
  295. int buf_size = avpkt->size;
  296. MpegEncContext *s = avctx->priv_data;
  297. int ret;
  298. AVFrame *pict = data;
  299. #ifdef PRINT_FRAME_TIME
  300. uint64_t time= rdtsc();
  301. #endif
  302. s->flags= avctx->flags;
  303. s->flags2= avctx->flags2;
  304. /* no supplementary picture */
  305. if (buf_size == 0) {
  306. /* special case for last picture */
  307. if (s->low_delay==0 && s->next_picture_ptr) {
  308. if ((ret = av_frame_ref(pict, &s->next_picture_ptr->f)) < 0)
  309. return ret;
  310. s->next_picture_ptr= NULL;
  311. *got_frame = 1;
  312. }
  313. return 0;
  314. }
  315. if(s->flags&CODEC_FLAG_TRUNCATED){
  316. int next;
  317. if(CONFIG_MPEG4_DECODER && s->codec_id==AV_CODEC_ID_MPEG4){
  318. next= ff_mpeg4_find_frame_end(&s->parse_context, buf, buf_size);
  319. }else if(CONFIG_H263_DECODER && s->codec_id==AV_CODEC_ID_H263){
  320. next= ff_h263_find_frame_end(&s->parse_context, buf, buf_size);
  321. }else if(CONFIG_H263P_DECODER && s->codec_id==AV_CODEC_ID_H263P){
  322. next= ff_h263_find_frame_end(&s->parse_context, buf, buf_size);
  323. }else{
  324. av_log(s->avctx, AV_LOG_ERROR, "this codec does not support truncated bitstreams\n");
  325. return AVERROR(EINVAL);
  326. }
  327. if( ff_combine_frame(&s->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0 )
  328. return buf_size;
  329. }
  330. retry:
  331. if(s->divx_packed && s->bitstream_buffer_size){
  332. int i;
  333. for(i=0; i<buf_size-3; i++){
  334. if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1){
  335. if(buf[i+3]==0xB0){
  336. av_log(s->avctx, AV_LOG_WARNING, "Discarding excessive bitstream in packed xvid\n");
  337. s->bitstream_buffer_size=0;
  338. }
  339. break;
  340. }
  341. }
  342. }
  343. if(s->bitstream_buffer_size && (s->divx_packed || buf_size<20)){ //divx 5.01+/xvid frame reorder
  344. init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size*8);
  345. }else
  346. init_get_bits(&s->gb, buf, buf_size*8);
  347. s->bitstream_buffer_size=0;
  348. if (!s->context_initialized) {
  349. if ((ret = ff_MPV_common_init(s)) < 0) //we need the idct permutaton for reading a custom matrix
  350. return ret;
  351. }
  352. /* We need to set current_picture_ptr before reading the header,
  353. * otherwise we cannot store anyting in there */
  354. if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
  355. int i= ff_find_unused_picture(s, 0);
  356. if (i < 0)
  357. return i;
  358. s->current_picture_ptr= &s->picture[i];
  359. }
  360. /* let's go :-) */
  361. if (CONFIG_WMV2_DECODER && s->msmpeg4_version==5) {
  362. ret= ff_wmv2_decode_picture_header(s);
  363. } else if (CONFIG_MSMPEG4_DECODER && s->msmpeg4_version) {
  364. ret = ff_msmpeg4_decode_picture_header(s);
  365. } else if (CONFIG_MPEG4_DECODER && s->h263_pred) {
  366. if(s->avctx->extradata_size && s->picture_number==0){
  367. GetBitContext gb;
  368. init_get_bits(&gb, s->avctx->extradata, s->avctx->extradata_size*8);
  369. ret = ff_mpeg4_decode_picture_header(s, &gb);
  370. }
  371. ret = ff_mpeg4_decode_picture_header(s, &s->gb);
  372. } else if (CONFIG_H263I_DECODER && s->codec_id == AV_CODEC_ID_H263I) {
  373. ret = ff_intel_h263_decode_picture_header(s);
  374. } else if (CONFIG_FLV_DECODER && s->h263_flv) {
  375. ret = ff_flv_decode_picture_header(s);
  376. } else {
  377. ret = ff_h263_decode_picture_header(s);
  378. }
  379. if (ret < 0 || ret==FRAME_SKIPPED) {
  380. if ( s->width != avctx->coded_width
  381. || s->height != avctx->coded_height) {
  382. av_log(s->avctx, AV_LOG_WARNING, "Reverting picture dimensions change due to header decoding failure\n");
  383. s->width = avctx->coded_width;
  384. s->height= avctx->coded_height;
  385. }
  386. }
  387. if(ret==FRAME_SKIPPED) return get_consumed_bytes(s, buf_size);
  388. /* skip if the header was thrashed */
  389. if (ret < 0){
  390. av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
  391. return ret;
  392. }
  393. avctx->has_b_frames= !s->low_delay;
  394. if(s->xvid_build==-1 && s->divx_version==-1 && s->lavc_build==-1){
  395. if(s->stream_codec_tag == AV_RL32("XVID") ||
  396. s->codec_tag == AV_RL32("XVID") || s->codec_tag == AV_RL32("XVIX") ||
  397. s->codec_tag == AV_RL32("RMP4") || s->codec_tag == AV_RL32("ZMP4") ||
  398. s->codec_tag == AV_RL32("SIPP")
  399. )
  400. s->xvid_build= 0;
  401. #if 0
  402. if(s->codec_tag == AV_RL32("DIVX") && s->vo_type==0 && s->vol_control_parameters==1
  403. && s->padding_bug_score > 0 && s->low_delay) // XVID with modified fourcc
  404. s->xvid_build= 0;
  405. #endif
  406. }
  407. if(s->xvid_build==-1 && s->divx_version==-1 && s->lavc_build==-1){
  408. if(s->codec_tag == AV_RL32("DIVX") && s->vo_type==0 && s->vol_control_parameters==0)
  409. s->divx_version= 400; //divx 4
  410. }
  411. if(s->xvid_build>=0 && s->divx_version>=0){
  412. s->divx_version=
  413. s->divx_build= -1;
  414. }
  415. if(s->workaround_bugs&FF_BUG_AUTODETECT){
  416. if(s->codec_tag == AV_RL32("XVIX"))
  417. s->workaround_bugs|= FF_BUG_XVID_ILACE;
  418. if(s->codec_tag == AV_RL32("UMP4")){
  419. s->workaround_bugs|= FF_BUG_UMP4;
  420. }
  421. if(s->divx_version>=500 && s->divx_build<1814){
  422. s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
  423. }
  424. if(s->divx_version>502 && s->divx_build<1814){
  425. s->workaround_bugs|= FF_BUG_QPEL_CHROMA2;
  426. }
  427. if(s->xvid_build<=3U)
  428. s->padding_bug_score= 256*256*256*64;
  429. if(s->xvid_build<=1U)
  430. s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
  431. if(s->xvid_build<=12U)
  432. s->workaround_bugs|= FF_BUG_EDGE;
  433. if(s->xvid_build<=32U)
  434. s->workaround_bugs|= FF_BUG_DC_CLIP;
  435. #define SET_QPEL_FUNC(postfix1, postfix2) \
  436. s->dsp.put_ ## postfix1 = ff_put_ ## postfix2;\
  437. s->dsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2;\
  438. s->dsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
  439. if(s->lavc_build<4653U)
  440. s->workaround_bugs|= FF_BUG_STD_QPEL;
  441. if(s->lavc_build<4655U)
  442. s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
  443. if(s->lavc_build<4670U){
  444. s->workaround_bugs|= FF_BUG_EDGE;
  445. }
  446. if(s->lavc_build<=4712U)
  447. s->workaround_bugs|= FF_BUG_DC_CLIP;
  448. if(s->divx_version>=0)
  449. s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
  450. if(s->divx_version==501 && s->divx_build==20020416)
  451. s->padding_bug_score= 256*256*256*64;
  452. if(s->divx_version<500U){
  453. s->workaround_bugs|= FF_BUG_EDGE;
  454. }
  455. if(s->divx_version>=0)
  456. s->workaround_bugs|= FF_BUG_HPEL_CHROMA;
  457. #if 0
  458. if(s->divx_version==500)
  459. s->padding_bug_score= 256*256*256*64;
  460. /* very ugly XVID padding bug detection FIXME/XXX solve this differently
  461. * Let us hope this at least works.
  462. */
  463. if( s->resync_marker==0 && s->data_partitioning==0 && s->divx_version==-1
  464. && s->codec_id==AV_CODEC_ID_MPEG4 && s->vo_type==0)
  465. s->workaround_bugs|= FF_BUG_NO_PADDING;
  466. if(s->lavc_build<4609U) //FIXME not sure about the version num but a 4609 file seems ok
  467. s->workaround_bugs|= FF_BUG_NO_PADDING;
  468. #endif
  469. }
  470. if(s->workaround_bugs& FF_BUG_STD_QPEL){
  471. SET_QPEL_FUNC(qpel_pixels_tab[0][ 5], qpel16_mc11_old_c)
  472. SET_QPEL_FUNC(qpel_pixels_tab[0][ 7], qpel16_mc31_old_c)
  473. SET_QPEL_FUNC(qpel_pixels_tab[0][ 9], qpel16_mc12_old_c)
  474. SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
  475. SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
  476. SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
  477. SET_QPEL_FUNC(qpel_pixels_tab[1][ 5], qpel8_mc11_old_c)
  478. SET_QPEL_FUNC(qpel_pixels_tab[1][ 7], qpel8_mc31_old_c)
  479. SET_QPEL_FUNC(qpel_pixels_tab[1][ 9], qpel8_mc12_old_c)
  480. SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
  481. SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
  482. SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
  483. }
  484. if(avctx->debug & FF_DEBUG_BUGS)
  485. av_log(s->avctx, AV_LOG_DEBUG, "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
  486. s->workaround_bugs, s->lavc_build, s->xvid_build, s->divx_version, s->divx_build,
  487. s->divx_packed ? "p" : "");
  488. #if HAVE_MMX
  489. if (s->codec_id == AV_CODEC_ID_MPEG4 && s->xvid_build>=0 && avctx->idct_algo == FF_IDCT_AUTO && (av_get_cpu_flags() & AV_CPU_FLAG_MMX)) {
  490. avctx->idct_algo= FF_IDCT_XVIDMMX;
  491. ff_dct_common_init(s);
  492. goto retry;
  493. }
  494. #endif
  495. /* After H263 & mpeg4 header decode we have the height, width,*/
  496. /* and other parameters. So then we could init the picture */
  497. /* FIXME: By the way H263 decoder is evolving it should have */
  498. /* an H263EncContext */
  499. if ((!avctx->coded_width || !avctx->coded_height) && 0) {
  500. ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat
  501. s->parse_context.buffer=0;
  502. ff_MPV_common_end(s);
  503. s->parse_context= pc;
  504. avcodec_set_dimensions(avctx, s->width, s->height);
  505. goto retry;
  506. }
  507. if (s->width != avctx->coded_width ||
  508. s->height != avctx->coded_height ||
  509. s->context_reinit) {
  510. /* H.263 could change picture size any time */
  511. s->context_reinit = 0;
  512. avcodec_set_dimensions(avctx, s->width, s->height);
  513. if ((ret = ff_MPV_common_frame_size_change(s)))
  514. return ret;
  515. }
  516. if((s->codec_id==AV_CODEC_ID_H263 || s->codec_id==AV_CODEC_ID_H263P || s->codec_id == AV_CODEC_ID_H263I))
  517. s->gob_index = ff_h263_get_gob_height(s);
  518. // for skipping the frame
  519. s->current_picture.f.pict_type = s->pict_type;
  520. s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  521. /* skip B-frames if we don't have reference frames */
  522. if (s->last_picture_ptr == NULL &&
  523. (s->pict_type == AV_PICTURE_TYPE_B || s->droppable))
  524. return get_consumed_bytes(s, buf_size);
  525. if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==AV_PICTURE_TYPE_B)
  526. || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=AV_PICTURE_TYPE_I)
  527. || avctx->skip_frame >= AVDISCARD_ALL)
  528. return get_consumed_bytes(s, buf_size);
  529. if(s->next_p_frame_damaged){
  530. if(s->pict_type==AV_PICTURE_TYPE_B)
  531. return get_consumed_bytes(s, buf_size);
  532. else
  533. s->next_p_frame_damaged=0;
  534. }
  535. if((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
  536. s->me.qpel_put= s->dsp.put_qpel_pixels_tab;
  537. s->me.qpel_avg= s->dsp.avg_qpel_pixels_tab;
  538. }else{
  539. s->me.qpel_put= s->dsp.put_no_rnd_qpel_pixels_tab;
  540. s->me.qpel_avg= s->dsp.avg_qpel_pixels_tab;
  541. }
  542. if ((ret = ff_MPV_frame_start(s, avctx)) < 0)
  543. return ret;
  544. if (!s->divx_packed) ff_thread_finish_setup(avctx);
  545. if (CONFIG_MPEG4_VDPAU_DECODER && (s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)) {
  546. ff_vdpau_mpeg4_decode_picture(s, s->gb.buffer, s->gb.buffer_end - s->gb.buffer);
  547. goto frame_end;
  548. }
  549. if (avctx->hwaccel) {
  550. if ((ret = avctx->hwaccel->start_frame(avctx, s->gb.buffer, s->gb.buffer_end - s->gb.buffer)) < 0)
  551. return ret;
  552. }
  553. ff_mpeg_er_frame_start(s);
  554. //the second part of the wmv2 header contains the MB skip bits which are stored in current_picture->mb_type
  555. //which is not available before ff_MPV_frame_start()
  556. if (CONFIG_WMV2_DECODER && s->msmpeg4_version==5){
  557. ret = ff_wmv2_decode_secondary_picture_header(s);
  558. if(ret<0) return ret;
  559. if(ret==1) goto intrax8_decoded;
  560. }
  561. /* decode each macroblock */
  562. s->mb_x=0;
  563. s->mb_y=0;
  564. ret = decode_slice(s);
  565. while(s->mb_y<s->mb_height){
  566. if(s->msmpeg4_version){
  567. if(s->slice_height==0 || s->mb_x!=0 || (s->mb_y%s->slice_height)!=0 || get_bits_left(&s->gb)<0)
  568. break;
  569. }else{
  570. int prev_x=s->mb_x, prev_y=s->mb_y;
  571. if(ff_h263_resync(s)<0)
  572. break;
  573. if (prev_y * s->mb_width + prev_x < s->mb_y * s->mb_width + s->mb_x)
  574. s->er.error_occurred = 1;
  575. }
  576. if(s->msmpeg4_version<4 && s->h263_pred)
  577. ff_mpeg4_clean_buffers(s);
  578. if (decode_slice(s) < 0) ret = AVERROR_INVALIDDATA;
  579. }
  580. if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type==AV_PICTURE_TYPE_I)
  581. if(!CONFIG_MSMPEG4_DECODER || ff_msmpeg4_decode_ext_header(s, buf_size) < 0){
  582. s->er.error_status_table[s->mb_num - 1] = ER_MB_ERROR;
  583. }
  584. av_assert1(s->bitstream_buffer_size==0);
  585. frame_end:
  586. /* divx 5.01+ bitstream reorder stuff */
  587. if(s->codec_id==AV_CODEC_ID_MPEG4 && s->divx_packed){
  588. int current_pos= s->gb.buffer == s->bitstream_buffer ? 0 : (get_bits_count(&s->gb)>>3);
  589. int startcode_found=0;
  590. if(buf_size - current_pos > 7){
  591. int i;
  592. for(i=current_pos; i<buf_size-4; i++){
  593. if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1 && buf[i+3]==0xB6){
  594. startcode_found=!(buf[i+4]&0x40);
  595. break;
  596. }
  597. }
  598. }
  599. if(startcode_found){
  600. av_fast_malloc(
  601. &s->bitstream_buffer,
  602. &s->allocated_bitstream_buffer_size,
  603. buf_size - current_pos + FF_INPUT_BUFFER_PADDING_SIZE);
  604. if (!s->bitstream_buffer)
  605. return AVERROR(ENOMEM);
  606. memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
  607. s->bitstream_buffer_size= buf_size - current_pos;
  608. }
  609. }
  610. intrax8_decoded:
  611. ff_er_frame_end(&s->er);
  612. if (avctx->hwaccel) {
  613. if ((ret = avctx->hwaccel->end_frame(avctx)) < 0)
  614. return ret;
  615. }
  616. ff_MPV_frame_end(s);
  617. assert(s->current_picture.f.pict_type == s->current_picture_ptr->f.pict_type);
  618. assert(s->current_picture.f.pict_type == s->pict_type);
  619. if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
  620. if ((ret = av_frame_ref(pict, &s->current_picture_ptr->f)) < 0)
  621. return ret;
  622. ff_print_debug_info(s, s->current_picture_ptr);
  623. } else if (s->last_picture_ptr != NULL) {
  624. if ((ret = av_frame_ref(pict, &s->last_picture_ptr->f)) < 0)
  625. return ret;
  626. ff_print_debug_info(s, s->last_picture_ptr);
  627. }
  628. if(s->last_picture_ptr || s->low_delay){
  629. *got_frame = 1;
  630. }
  631. #ifdef PRINT_FRAME_TIME
  632. av_log(avctx, AV_LOG_DEBUG, "%"PRId64"\n", rdtsc()-time);
  633. #endif
  634. return (ret && (avctx->err_recognition & AV_EF_EXPLODE))?ret:get_consumed_bytes(s, buf_size);
  635. }
  636. const enum AVPixelFormat ff_h263_hwaccel_pixfmt_list_420[] = {
  637. #if CONFIG_VAAPI
  638. AV_PIX_FMT_VAAPI_VLD,
  639. #endif
  640. #if CONFIG_VDPAU
  641. AV_PIX_FMT_VDPAU,
  642. #endif
  643. AV_PIX_FMT_YUV420P,
  644. AV_PIX_FMT_NONE
  645. };
  646. AVCodec ff_h263_decoder = {
  647. .name = "h263",
  648. .type = AVMEDIA_TYPE_VIDEO,
  649. .id = AV_CODEC_ID_H263,
  650. .priv_data_size = sizeof(MpegEncContext),
  651. .init = ff_h263_decode_init,
  652. .close = ff_h263_decode_end,
  653. .decode = ff_h263_decode_frame,
  654. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
  655. CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  656. .flush = ff_mpeg_flush,
  657. .max_lowres = 3,
  658. .long_name = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
  659. .pix_fmts = ff_h263_hwaccel_pixfmt_list_420,
  660. };
  661. AVCodec ff_h263p_decoder = {
  662. .name = "h263p",
  663. .type = AVMEDIA_TYPE_VIDEO,
  664. .id = AV_CODEC_ID_H263P,
  665. .priv_data_size = sizeof(MpegEncContext),
  666. .init = ff_h263_decode_init,
  667. .close = ff_h263_decode_end,
  668. .decode = ff_h263_decode_frame,
  669. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
  670. CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  671. .flush = ff_mpeg_flush,
  672. .max_lowres = 3,
  673. .long_name = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
  674. .pix_fmts = ff_h263_hwaccel_pixfmt_list_420,
  675. };