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.

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